0

I am not very familiar with writing classes in python, I am trying to modify one written class for my application. the problem is that I want to give halo_pos parameter as an input to the following NFW class:

class NFW(object):
    _req_params = { 'mass' : float , 'conc' : float , 'redshift' : float }
    _opt_params = { 'halo_pos' :[float,float]  , 'omega_m' : float , 'omega_lam' : float }
    _single_params = []
    _takes_rng = False
    _takes_logger = False

    def __init__(self, mass, conc, redshift, halo_pos, 
                 omega_m=None, omega_lam=None, cosmo=None):
        self.M = float(mass)
        self.c = float(conc)
        self.z = float(redshift)
        self.halo_pos.x = float(halo_pos[0])
        self.halo_pos.y = float(halo_pos[1])  
        self.cosmo = cosmo

if I pass the input for halo_pos for the following parameters

>>> Xpos.value
array(235.0)
>>> type(Xpos.value)
<type 'numpy.ndarray'>
>>> Ypos.value
array(340.0)
omega_matter=0.23;omega_lambda=0.77

then when I try to call NFW class with the given inputs I get the following error message:

nfw = nfw_halo.NFWHalo(mass=M,conc=concentration,redshift=z_halo,halo_pos=[Xpos,Ypos],omega_m=omega_matter,omega_lam=omega_lambda)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "nfw_halo.py", line 156, in __init__
    self.halo_pos.x = float(halo_pos[0])
AttributeError: 'NFWHalo' object has no attribute 'halo_pos'

How should I define halo_pos for my class in order to avoid raise error message?

5
  • Could you give us a MWE? Commented Jun 4, 2014 at 15:52
  • @andi Most new users aren't going to have a clue what a MWE is. Avoid acronyms when posting with new users. Commented Jun 4, 2014 at 16:15
  • Sorry, I am new myself and saw that everyone used it. I will avoid that in future. Thanks for the hint. Commented Jun 4, 2014 at 16:19
  • @andi why doesn't class raise error message for mass or conc parameters? Commented Jun 4, 2014 at 16:44
  • Because for pos.x and pos.y you expect a type that has an x and a y component that can be adressed by ".". How should python know what you are about to creatE? Commented Jun 4, 2014 at 16:47

1 Answer 1

1

You're not declaring the halo_pos variable that you're assigning in your init section.

class NFW(object):
    _req_params = { 'mass' : float , 'conc' : float , 'redshift' : float }
    _opt_params = { 'halo_pos' :[float,float]  , 'omega_m' : float , 'omega_lam' : float }
    _single_params = []
    _takes_rng = False
    _takes_logger = False
    halo_pos = None

    def __init__(self, mass, conc, redshift, halo_pos, 
                 omega_m=None, omega_lam=None, cosmo=None):
        self.M = float(mass)
        self.c = float(conc)
        self.z = float(redshift)
        self.halo_pos = halo_positions(halo_pos)
        self.cosmo = cosmo

        print(str(self.halo_pos.x),str(self.halo_pos.y))

class halo_positions(object):
    x = None
    y = None
    def __init__(self,positions):
        self.x = positions[0]
        self.y = positions[1]

if __name__ == '__main__':
    nfw = NFW(mass=1.23,conc=2.34,redshift=3.45,halo_pos=[4.56,5.67],omega_m=1.111,omega_lam=1.222)

so your delclaration self.halo_pos.x and self.halo_pos.y are cleaned up once init finishes.

Sign up to request clarification or add additional context in comments.

10 Comments

Sorry I didn't notice earlier, the .x and the .y might be the problem. Have you created a halo_pos class as well? If not, then you would have to use something else in your variable names instead of the '.'. I've updated my example to show you.
You should change halo_pos.x = None and halo_pos.y = None to halo_pos = None then he can define halo_pos as any object he wants, even if he needs he could use halo_pos.x and halo_pos.y if defined accordingly.
@Zifendale what is the difference between these two declarations. I have changed according what you suggested but I get again similar error message ` File "nfw_halo.py", line 156, in init self.c = float(conc) AttributeError: 'NFWHalo' object has no attribute 'halo_pos'. `
Is the way I defined halo_pos in _opt_params fine, when each component of halo_pos is an numpy array?
@Dalek andi's answer is probably the best suggestion. The halo_pos object is not being defined for the class as I have noted. If you define it properly to so that it has .x and .y attributes then that should work. I'll update my answer accordingly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.