Skip to content

Prior fails to read in prior file with inf

Matthew Pitkin requested to merge matthew-pitkin/bilby:prior_read_npinf into master

If you create a prior dictionary that, for example, includes an infinity as one of its arguments, and then write it to a file you then cannot read that back in. E.g., for a TruncatedGaussian prior

from bilby.core.prior import PriorDict, TruncatedGaussian
import numpy as np
x = PriorDict({"val": TruncatedGaussian(mu=1, sigma=0.5, minimum=0, maximum=np.inf, name="val")})
x.to_file(outdir=".", label="test")

you will get a file containing:

val = TruncatedGaussian(mu=1, sigma=0.5, minimum=0, maximum=inf, name='val', latex_label='val', unit=None, boundary=None)

If you then try to read that back in you get the following error:

y = PriorDict("test.prior")
...
TypeError: Unable to parse prior, bad entry: val = TruncatedGaussian(mu=1, sigma=0.5, minimum=0, maximum=inf, name='val', latex_label='val', unit=None, boundary=None). Error message Cannot evaluate prior, failed to parse argument inf

In the _parse_argument_string method, it expects the file to contain, e.g., "np.inf" rather than "inf", which is what gets output. So, this MR just edits that function to allow it to parse an "inf" string or a "pi" string as np.inf and np.pi values.

Merge request reports

Loading