上下文
我正在一个项目中使用相应的Cython代码包装一些C ++函数和类。
特别是,我有一个函数,它是一个名为meta_edm.pyx
的文件中的Cython类的成员,其定义如下:
# meta_edm.pyx
def integrate( self , meta_beta , num_MC_steps , ground_truth_partition , size_partition_pool = None , verbose = False ):
cdef Partition * ptr_ground_truth_partition
cdef RunningStats * ptr_rs_NMI_gt
cdef RunningStats * ptr_rs_AMI_gt
cdef RunningStats * ptr_rs_NMI_sg
cdef RunningStats * ptr_rs_AMI_sg
cdef RunningStats * ptr_rs_energy
cdef int l
cdef int ii
ptr_ground_truth_partition = new Partition( len( ground_truth_partition ) )
ptr_rs_NMI_gt = new RunningStats()
ptr_rs_AMI_gt = new RunningStats()
ptr_rs_NMI_sg = new RunningStats()
ptr_rs_AMI_sg = new RunningStats()
ptr_rs_energy = new RunningStats()
for l , part in enumerate( ground_truth_partition ):
for i in part:
ii = self._edm.node_enum( i )
# --- PAY ATTENTION TO THIS LINE ---
ptr_ground_truth_partition.change_label_of_element( ii , l )
if size_partition_pool is None:
size_partition_pool = self._c_edm.G().num_nodes()
self._c_meta_edm.integrate( meta_beta , num_MC_steps , deref( ptr_ground_truth_partition ) , size_partition_pool , deref( ptr_rs_NMI_gt ) , deref( ptr_rs_AMI_gt ) , deref( ptr_rs_NMI_sg ) , deref( ptr_rs_AMI_sg ) , deref( ptr_rs_energy ) , verbose )
rs_NMI_gt = {}
rs_AMI_gt = {}
rs_NMI_sg = {}
rs_AMI_sg = {}
rs_energy = {}
rs_NMI_gt[ 'mean' ] = ptr_rs_NMI_gt.mean();
rs_AMI_gt[ 'mean' ] = ptr_rs_AMI_gt.mean();
rs_NMI_sg[ 'mean' ] = ptr_rs_NMI_sg.mean();
rs_AMI_sg[ 'mean' ] = ptr_rs_AMI_sg.mean();
rs_energy[ 'mean' ] = ptr_rs_energy.mean();
rs_NMI_gt[ 'std' ] = ptr_rs_NMI_gt.std();
rs_AMI_gt[ 'std' ] = ptr_rs_AMI_gt.std();
rs_NMI_sg[ 'std' ] = ptr_rs_NMI_sg.std();
rs_AMI_sg[ 'std' ] = ptr_rs_AMI_sg.std();
rs_energy[ 'std' ] = ptr_rs_energy.std();
rs_NMI_gt[ 'sem' ] = ptr_rs_NMI_gt.sem();
rs_AMI_gt[ 'sem' ] = ptr_rs_AMI_gt.sem();
rs_NMI_sg[ 'sem' ] = ptr_rs_NMI_sg.sem();
rs_AMI_sg[ 'sem' ] = ptr_rs_AMI_sg.sem();
rs_energy[ 'sem' ] = ptr_rs_energy.sem();
rs_NMI_gt[ 'min' ] = ptr_rs_NMI_gt.min();
rs_AMI_gt[ 'min' ] = ptr_rs_AMI_gt.min();
rs_NMI_sg[ 'min' ] = ptr_rs_NMI_sg.min();
rs_AMI_sg[ 'min' ] = ptr_rs_AMI_sg.min();
rs_energy[ 'min' ] = ptr_rs_energy.min();
rs_NMI_gt[ 'max' ] = ptr_rs_NMI_gt.max();
rs_AMI_gt[ 'max' ] = ptr_rs_AMI_gt.max();
rs_NMI_sg[ 'max' ] = ptr_rs_NMI_sg.max();
rs_AMI_sg[ 'max' ] = ptr_rs_AMI_sg.max();
rs_energy[ 'max' ] = ptr_rs_energy.max();
rs_NMI_gt[ 'num_samples' ] = ptr_rs_NMI_gt.num_samples();
rs_AMI_gt[ 'num_samples' ] = ptr_rs_AMI_gt.num_samples();
rs_NMI_sg[ 'num_samples' ] = ptr_rs_NMI_sg.num_samples();
rs_AMI_sg[ 'num_samples' ] = ptr_rs_AMI_sg.num_samples();
rs_energy[ 'num_samples' ] = ptr_rs_energy.num_samples();
del ptr_rs_energy;
del ptr_rs_AMI_sg;
del ptr_rs_NMI_sg;
del ptr_rs_AMI_gt;
del ptr_rs_NMI_gt;
del ptr_ground_truth_partition;
return rs_NMI_gt , rs_AMI_gt , rs_NMI_sg , rs_AMI_sg , rs_energy
此外,我有一个相应的meta_edm.pxd
文件,其中包含代码:
# meta_edm.pxd
cdef extern from "cpp_partition.h":
cdef cppclass Partition:
Partition( int size ) except +
#~Partition()
int size()
int num_active_labels()
int label_count( int l )
int total_count()
bool is_label_active( int l )
int label_of_element( int i )
void change_label_of_element( int i , int new_l )
并且,有一个相应的cpp_partition.h
文件,其中定义了类Partition
。
问题
当我尝试编译时,我收到以下错误:
cythoning ExternalDegreeModel/meta_edm.pyx to ExternalDegreeModel/meta_edm.cpp
Error compiling Cython file:
------------------------------------------------------------
...
ptr_rs_energy = new RunningStats()
for l , part in enumerate( ground_truth_partition ):
for i in part:
ii = self._edm.node_enum( i )
ptr_ground_truth_partition.change_label_of_element( ii , l )
^
------------------------------------------------------------
ExternalDegreeModel/meta_edm.pyx:161:42: Object of type 'Partition' has no attribute 'change_label_of_element'
building 'ExternalDegreeModel.meta_edm' extension
我不确定Python是否将ptr_ground_truth_partition
解释为Python对象,或者它是否将其解释为指向C ++对象的指针。但是,无论如何,这很奇怪,因为Partition
似乎被正确地声明为meta_edm.pxd
文件中的C ++类及其成员函数change_label_of_element
。
我已经尝试了很多东西来解决这个问题但我找不到办法。有什么想法吗?