samvaughan / PyStaff

Spectral Fitting with CvD stellar population models
MIT License
5 stars 2 forks source link

Error with starting values #7

Closed afeldmei closed 6 years ago

afeldmei commented 6 years ago

I am running fit_example.py and I get an error message in the part where the code checks the bound values:


ValueError Traceback (most recent call last) /Users/afeldmei/software/PyStaff-master/fit_example.py in () 219 p0[p0<bounds[:, 0, None]]=bounds[np.any(p0<bounds[:, 0, None], axis=1), 0]+10np.finfo(np.float64).eps 220 #And the same for any values which are too low --> 221 p0[p0>bounds[:, 1, None]]=bounds[np.any(p0>bounds[:, 1, None], axis=1), 1]-10np.finfo(np.float64).eps 222 223

ValueError: NumPy boolean array indexing assignment cannot assign 2 input values to the 21 output values where the mask is true

I solved it by commenting the line

p0[p0>bounds[:, 1, None]]=bounds[np.any(p0>bounds[:, 1, None], axis=1), 1]-10*np.finfo(np.float64).eps

and inserting the following piece of code

np0=shape(p0) nit=np0[1] for i in range(nit): test=p0[:,i]>bounds[:, 1] if any(test)==True: p0[test,i]=bounds[test, 1]-10*np.finfo(np.float64).eps

But maybe you know a better way to deal with it? I am not a python expert.

samvaughan commented 6 years ago

Thanks! Turns out I hadn't tested the code where 2 parameters had their starting positions scatter outside the prior boundaries- Thanks for catching it! This should be fixed in 97a11f2f3d1abd4, where I've added a function called check_walker_starting_positions to SpectralFitting_functs.py which does this bounds checking.

Very simply, it just loops through each of the walkers which have bad starting positions and changes them to the prior limit:

walkers_too_low=np.where(p0<bounds[:, 0, None])
walkers_too_high=np.where(p0>bounds[:, 1, None])

for parameter, walker_number in zip(*walkers_too_low):
    p0[parameter, walker_number]=bounds[parameter, 0]+10*np.finfo(float).eps

for parameter, walker_number in zip(*walkers_too_high):
    p0[parameter, walker_number]=bounds[parameter, 1]-10*np.finfo(float).eps

I'll close this now.