继承Python类中的Cython类

时间:2015-05-16 19:31:55

标签: python class inheritance cython

我有两个用cython编写的类,我想在python的类中使用它们。

position.pyx

import numpy as np
cimport numpy as np
cimport cython
cpdef double std_G,v=4.3e-9, 299792.458 

cdef class cosmo(object):
    cdef public double o_m, o_l, h, w, o_r, G, v
    def __init__(self,double o_m = 0.3, double o_l = 0.7, double h = 0.7, double w = -1, double o_r = 0., double G = std_G):

        self.o_m = o_m
        self.o_l = o_l
        self.o_r = o_r
        self.h = h
        self.w = w
        self.G = G
        self.v = v

    def __copy__(self):

        return cosmo(o_m = self.o_m, o_l = self.o_l, h = self.h, w = self.w, o_r = self.o_r, G = self.G)

    property H0:
        def __get__(self):
            return 100*self.h  

    property M_solar:
        def __get__(self):
            return 1.989e30 

    property Mpc_to_m:
        def __get__(self):
            return 3.0856e22; 

    def hubble2(self, double z):
        cdef double inv_a
        inv_a = 1.+z
        return (self.o_r*inv_a**4 + self.o_m*inv_a**3 + \
                  self.o_l*(inv_a**(3*(1+self.w))) + (1 - self.o_m - self.o_l - self.o_r)*inv_a**2)*self.H0**2

    property hubble_length:
        def __get__(self):
            return self.v / self.H0

    def rc(self, double z):

        return 3.*self.hubble2(z)/(8*np.pi*self.G)

cdef class PositionsD(object):

     cdef double [:] _x
     property x:
         def __get__(self):
             return np.array(self._x)
         def __set__(self, np.ndarray[DTYPE_T, ndim=1] x):
             self._x = x

     cdef double [:] _y
     property y:
         def __get__(self):
             return np.array(self._y)
         def __set__(self, np.ndarray[DTYPE_T, ndim=1, mode='c'] y):
             self._y = y

     def __init__(self, np.ndarray[DTYPE_T, ndim=2, mode='c'] positions):
         self._x = positions[:,0]
         self._y = positions[:,1]

虽然我想在PositionsD课程中使用modelfit课程,而modelfit会继承PositionsD的属性:

from position import *
import numpy as np
class modelfit(PositionsD):
    cosmo = cosmo()
    def __init__(self):
        super(modelfit,self).__init__(shear_pos)

        self.arcsec2rad = 2*np.pi/180./3600.
        self.shear_g = None
        self.shear_pos = shear_pos *self.arcsec2rad
        self.shear_z = None
        self.halo_pos = None
        self.halo_z = None
        self.sigma_g = np.sqrt(np.std(self.shear_g[:,1]**2+self.shear_g[:,2]**2))/np.sqrt(2)
        self.n_model_evals = 0
        self.gaussian_prior_theta = [{'mean' : 14, 'std': 0.5}]
        self.rho_c= cosmo.rc(self.halo_z)

但是我收到以下错误消息:

>>> x=np.array([[0.3,-0.1],[1,3.4]])
>>> mf=modelfit(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() takes exactly 1 argument (2 given)

我不知道我做错了什么。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

在当前的类定义中,__init__不期望任何参数。这就是您致电mf=modelfit(x)时出现错误的原因。由于shear_pos不会另外定义,我假设您打算在类初始化中将其作为参数传递。如果是这样,你应该写:

class modelfit(PositionsD):
    cosmo = cosmo()
    def __init__(self, shear_pos):
        super(modelfit,self).__init__(shear_pos)
        ...

答案 1 :(得分:0)

我发现我的应用程序使用PositionsD类的属性的最佳方法是使用它如下:

from position import *
import numpy as np
class modelfit(object):
    def __init__(self):
        self.cosmo = cosmo()
        self.arcsec2rad = 2*np.pi/180./3600.
        self.shear_g = None
        self.source_pos = None
        self.shear_z = None
        self.halo_pos = None
        self.halo_z = None
        self.gaussian_prior_theta = [{'mean' : 14, 'std': 0.5}]
    def get_shear_pos(self): 
        return PositionsD(self.source_pos*self.arcsec2rad)

    shear_pos = property(get_shear_pos)

    def get_halo_center(self): 
        return PositionsD(self.halo_pos*self.arcsec2rad)

    halo_center = property(get_halo_center)

    def get_sigma_g(self):
        return np.sqrt(np.std(self.shear_g[:,1]**2+self.shear_g[:,2]**2))/np.sqrt(2)

    sigma_g = property(get_sigma_g)
    def get_rho_c(self):
        return self.cosmo.rc(self.halo_z)

    rho_c = property(get_rho_c)

我正在寻找的应用程序示例如下:

>>> m=modelfit()
>>> r=np.array([[0.2,-0.5],[2.1,9.3],[3.1,-2.8],[0.01,0.211]])
>>> m.source_pos=r
>>> m.shear_pos
<position.PositionsD object at 0x7f91a40e81e0>
>>> m.shear_pos.x
array([  1.93925472e-06,   2.03621746e-05,   3.00584482e-05,
         9.69627362e-08])
>>> m.shear_pos.x/m.arcsec2rad
array([ 0.2 ,  2.1 ,  3.1 ,  0.01])
>>> m.shear_pos.y/m.arcsec2rad
array([-0.5  ,  9.3  , -2.8  ,  0.211])