【发布时间】:2016-04-29 16:41:50
【问题描述】:
我正在使用一个类,该类允许我分析我拥有的数据数组并将其作为数组返回。
这是我正在使用的类的一部分。
import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt
from scipy import interpolate
class MassFunction:
def __init__(self, h=0.7, Omega_m=.27, sigma8=.809, n=0.95, rho_c=1.35972365653e11, delta_c=1.686):
"""
@param h: hubble parameter.
@param omega_m: current matter fraction of universe.
@param sigma8: Value of variance of density field with spherical smoothing at 8 Mpc/h
@param n: spectral scalar index for primordial power spectrum
@param delta_c: amplitude of perturbation at collapse using linear theory.
@param rho_c: critical density of universe in Msun/Mpc
"""
self.h = h
self.Omega_m = Omega_m
self.n = n
self.delta_c = delta_c
self.rho_m = rho_c*Omega_m/self.h**2
self.sigma8 = sigma8
def NofM(self,masses, numbins, boxsize):
"""
Produce mass function data for N(m). Fractional number density of halos in
mass range (m,m+dm). Integrates to total number of halos/volume.
@param masses: list of all masses in Msun/h
@param numbins: number of bins to use in histogram
@param boxsize: Size of box in MPc/h.
@return: [x-axis in log10(mass), y-axis in log10(N(m)), xlabel, ylabel]
"""
logmasses = np.log10(masses)
hist, r_array = np.histogram(logmasses, numbins)
dlogM = r_array[1]-r_array[0]
x_array = r_array[1:] - .5*dlogM
dM = 10.**r_array[1:]-10.**r_array[0:numbins] #Mass size of bins in non-log space.
volume = np.float(boxsize**3) # in MPc^3
return [x_array, np.log10(hist/volume/dM)]
类的其余部分要长得多,但在这种情况下不需要使用其他函数。
在尝试调用它时,我导入了它并尝试将它与提供的质量数组一起使用。
import matplotlib.pyplot as plt
import numpy as np
import MassFunction
# This is just the array of data
halomass3 = halos3['GroupMass'] * 1e10 / 0.704 # in units of M_sol h^-1
MassFunction.MassFunction.NofM(halomass3,100, 75000 )
我返回此错误unbound method NofM() must be called with MassFunction instance as first argument (got ndarray instance instead)
我对使用类或调用类不是最熟悉的,因为这是我第一次使用它们。我应该打电话给__init__ 并设置参数还是我错过了更多?
如果我遗漏了任何必要的信息。请告诉我!
【问题讨论】:
标签: python class numpy scipy ipython