Skip to content

Fix reading of latex_labels in prior file to avoid multiple escape characters

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

Currently when reading in a prior dictionary to file it adds additional \ escape characters to strings containing \s, e.g., LaTeX labels. For example, if I create:

import bilby
pd = {}
pd['iota'] = bilby.core.prior.Uniform(name='iota', minimum=0.0, maximum=1.0, latex_label=r"$\iota$")
pdd = bilby.core.prior.PriorDict(pd)
pdd.to_file('.', 'test')

then the output file looks like:

iota = Uniform(minimum=0.0, maximum=1.0, name='iota', latex_label='$\\iota$', unit=None, boundary=None)

which is fine. But, if I read this back in, e.g.:

pd2 = bilby.core.prior.PriorDict(filename='test.prior')

the latex label contains extra escape characters:

print(pd2)
PriorDict([('iota',
            Uniform(minimum=0.0, maximum=1.0, name='iota', latex_label='$\\\\iota$', unit=None, boundary=None))])

If you then use this read-in prior in a Results object, for example, the LaTeX label will cause problems in plotting.

This is caused by how open interprets escape characters and can be fixed by using the encoding='unicode_escape' argument to open. For Python 2 compatibility you have to use the open function from the io module (which in Python 3 is already the alias for the standard open function).

Merge request reports

Loading