Issue with array_like inputs for certain priors
A few of the priors in bilby
do not support all the input types mentioned in the corresponding doc-strings. As far as I can tell this only affects methods that treat floats and array_like inputs differently.
The are two different cases that fail for these priors:
- 0d-numpy arrays (e.g.
numpy.array(1)
) since these do not supportlen()
- array_like inputs that do not support indexing w.r.t a float (e.g.
idx = (x > 0)
), which includes lists and tuples
For example:
d = LogNormal(2, 2, 'x')
d.prob([1, 2])
raises:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-51-d5ec2ee1beb3> in <module>
----> 1 d.prob([1, 2])
/nfshome/store03/users/michael.williams/git_repos/bilby/bilby/core/prior/analytical.py in prob(self, val)
715 else:
716 _prob = np.zeros(len(val))
--> 717 idx = (val > self.minimum)
718 _prob[idx] = np.exp(-(np.log(val[idx]) - self.mu) ** 2 / self.sigma ** 2 / 2)\
719 / np.sqrt(2 * np.pi) / val[idx] / self.sigma
TypeError: '>' not supported between instances of 'list' and 'float'
and
d = LogNormal(2, 2, 'x')
d.prob(np.array(1))
raises:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-4ee2c86100db> in <module>
----> 1 d.prob(np.array(1))
/nfshome/store03/users/michael.williams/git_repos/bilby/bilby/core/prior/analytical.py in prob(self, val)
714 / np.sqrt(2 * np.pi) / val / self.sigma
715 else:
--> 716 _prob = np.zeros(len(val))
717 idx = (val > self.minimum)
718 _prob[idx] = np.exp(-(np.log(val[idx]) - self.mu) ** 2 / self.sigma ** 2 / 2)\
TypeError: len() of unsized object
Affected priors
I've identified the following priors that have this issue:
-
SymmetricLogUniform
(only lists): https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/core/prior/analytical.py#L269 -
LogNormal
: https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/core/prior/analytical.py#L659 -
Exponential
: https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/core/prior/analytical.py#L763 -
Logistic
: https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/core/prior/analytical.py#L1035 -
Gamma
: https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/core/prior/analytical.py#L1197 -
FermiDirac
: https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/core/prior/analytical.py#L1319
The methods in question are: rescale
, prob
and ln_prob
.
I've attached a jupyter-notebook that reproduces these errors for each prior: priors_bug.ipynb
Some of GW priors are also affected:
-
UniformInComponentsMassRatio
: https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/gw/prior.py#L333 -
ConditionalChiInPlane
: https://git.ligo.org/lscsoft/bilby/-/blob/master/bilby/gw/prior.py#L333
There may be some more priors that don't support lists because of other issues in the maths (e.g.: [1, 2] + 1
does not work) but these are less obvious just from looking that the code. Given this, maybe it's worth implementing a unittest to check this in the future?
Possible solutions
@colm.talbot pointed out that some of the other priors use numpy.atleast_1d
to get around but that is also doesn't preserve the input type. It does seem to fix both issues without requiring distinguishing between lists and 0d arrays.