I get an error "TypeError: object of type 'Number' has no len()" in my laptop or any other system while the same code run fine in my system

37 Views Asked by At

This is the definition file which is being used to run another .py file. I do not know what's wrong here as the same code runs fine in my desktop system.

class scalarpow(object):
    """
    A Class to calculate cosmological observables for quintessence field
    with power law potential.

    parameters are:
    Ophi_i : initial value (at a = 0.001) of density parameter for scalar field
    li : initial value (at a = 0.001) for the slope of potential (-V'(phi)/V(phi))
    n : order of power law potential V(phi)=phi**n
    Ob0 : present day density parameter for baryons (or visible matter)
    ns : spectral index for primordial power spectrum
    h : dimensionless parameter related to Hubble constant H0 = 100*h km s^-1 Mpc^-1
    sigma_8 : r.m.s. mass fluctuation on 8h^-1 Mpc scale
    """

    def __init__(self, Ophi_i,  rd, li, mi, Orad_i, h=0.7, Ob0=0.045, ns=0.96, sigma_8=0.8):
        self.Ophi_i = float(Ophi_i)
        self.rd = float(rd)
        self.li = float(li)
        self.mi = float(mi)
        self.Orad_i = float(Orad_i)
        self.h = float(h)
        self.Ob0 = float(Ob0)
        self.ns = float(ns)
        self.sigma_8 = float(sigma_8)

    n1 = np.linspace(np.log(1./1101), 0.0, 1000)




    @hope.jit
    def f(self, x, efold):
        g = 1.
        h = 1.
        y = np.empty(7)
        y[0] = (2-x[0])*(3*x[0]*x[1])**(0.5)*(-(3*x[0]*x[1])**(0.5) + x[3]*x[1]+x[4]*(1-x[1]-x[2])) #for gamma
        y[1] = 3.0*(1-x[0])*x[1]*(1-x[1]) + x[1]*x[2] + x[4]*(3*x[0]*x[1])**(0.5)*(1-x[1]-x[2]) #for Ophi
        y[2] = x[2]*(x[2]-1) + 3.0*x[2]*x[1]*(x[0]-1)  #for Oradiation
        y[3] = -(3*x[0]*x[1])**(0.5)*(g-1.0)*x[3]**2.0 #for lambda
        y[4] = (3*x[0]*x[1])**(0.5)*(h-1.0)*x[4]**2.0 #for m
        y[5] = x[6]
        y[6] = -(0.5-1.5*(x[0]-1)*x[1]-0.5*x[2]-x[4]*(3*x[0]*x[1])**(0.5))*x[6] + 1.5*((1-x[1]-x[2]) - x[0]*x[1])*(1+2.0*x[4]**2.0)*x[5] #for matter perturbation
        return len(y)


    def sol(self):
        inic = [1e-4, self.Ophi_i*1e-9, self.Orad_i, self.li, self.mi, 1./1101, 1./1101]
        res = odeint(self.f, inic, self.n1)
        return res`

And The error is

Traceback (most recent call last):
  File "/mnt/d/Downloads/exp_scalar_emcee_1.py", line 205, in <module>
    print(chi2(1.15, 144.41, 1.54, 0.001, 0.147, 0.68, 0.75, -19.4))
  File "/mnt/d/Downloads/exp_scalar_emcee_1.py", line 201, in chi2
    return chipanthplus(Ophi,      rd, li, mi,  Orad_i,      h, sigma8, M)-chiH0(Ophi,      rd, li, mi,  Orad_i,      h, sigma8, M)#+chibao(Ophi,    rd, li, mi,  Orad_i,      h, sigma8)+chicmb(Ophi,     rd, li, mi,  Orad_i,      h, sigma8)+chiH(Ophi,     rd,li, mi,  Orad_i,     h,sigma8)+chifs8(Ophi,    rd, li, mi,  Orad_i,     h,sigma8)
  File "/mnt/d/Downloads/exp_scalar_emcee_1.py", line 82, in chipanthplus
    mod = 5.*np.log10(x(zcmb))+25.
  File "/home/trupti29/.local/lib/python3.10/site-packages/numpy/lib/function_base.py", line 2372, in __call__
    return self._call_as_normal(*args, **kwargs)
  File "/home/trupti29/.local/lib/python3.10/site-packages/numpy/lib/function_base.py", line 2365, in _call_as_normal
    return self._vectorize_call(func=func, args=vargs)
  File "/home/trupti29/.local/lib/python3.10/site-packages/numpy/lib/function_base.py", line 2450, in _vectorize_call
    ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
  File "/home/trupti29/.local/lib/python3.10/site-packages/numpy/lib/function_base.py", line 2410, in _get_ufunc_and_otypes
    outputs = func(*inputs)
  File "/mnt/d/Downloads/scalar_all_eqn_1.py", line 340, in luminosity_distance_z
    d_l = self.comoving_distance_z(z)*(1.+z)
  File "/mnt/d/Downloads/scalar_all_eqn_1.py", line 323, in comoving_distance_z
    return self.comoving_distance_n(np.log(1./(1.+z)))
  File "/mnt/d/Downloads/scalar_all_eqn_1.py", line 311, in comoving_distance_n
    H = ((1-self.sol()[999, 1]-self.sol()[999, 2])*np.exp(-3 *self.n1) * np.exp(-self.sol()[999, 4]*np.sqrt(3*self.sol()[999, 0]*self.sol()[999, 1])* self.n1 )/(1-self.sol()[:, 1]-self.sol()[:, 2]))**(0.5)
  File "/mnt/d/Downloads/scalar_all_eqn_1.py", line 63, in sol
    res = odeint(self.f, inic, self.n1)
  File "/home/trupti29/.local/lib/python3.10/site-packages/scipy/integrate/_odepack_py.py", line 242, in odeint
    output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_wrapper.py", line 73, in _hope_callback
    return self(*args) if self.cache is None else self.cache(*args)
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_wrapper.py", line 78, in __call__
    ASTTransformer(self.modtoken).module_visit(self.fkt, args)
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_transformer.py", line 697, in module_visit
    parsed = self.visit(fktast)
  File "/usr/lib/python3.10/ast.py", line 418, in visit
    return visitor(node)
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_transformer.py", line 634, in visit_FunctionDef
    self.token.body = self.body_visit(node.body)
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_transformer.py", line 647, in body_visit
    self.exprs.append(self.visit(stmt))
  File "/usr/lib/python3.10/ast.py", line 418, in visit
    return visitor(node)
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_transformer.py", line 289, in visit_Assign
    target, value = self.visit(node.targets[0]), self.visit(node.value)
  File "/usr/lib/python3.10/ast.py", line 418, in visit
    return visitor(node)
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_transformer.py", line 341, in visit_Subscript
    return View(self.visit(node.value), self.visit(node.slice))
  File "/home/trupti29/.local/lib/python3.10/site-packages/hope/_ast.py", line 135, in __init__
    if len(extents) == 0:
TypeError: object of type 'Number' has no len()
in line y[0] = 2 - (x[0]) * (3 * (x[0]) * (x[1]) ** (0.5)) * ((-3 * (x[0]) * (x[1]) ** (0.5)) + (x[3] * (x[1])) + (x[4] * (1 - (x[1]) - (x[2]))))
in body y[0] = 2 - (x[0]) * (3 * (x[0]) * (x[1]) ** (0.5)) * ((-3 * (x[0]) * (x[1]) ** (0.5)) + (x[3] * (x[1])) + (x[4] * (1 - (x[1]) - (x[2]))))

Any help from your side is appreciated. Thank you in advance.

0

There are 0 best solutions below