Remove OrderedDict from PriorDicts
From python3.6+, all dicts are ordered by design. On the otherhand, the collections OrderedDict has odd behaviour when pickled (see comment below). This changes to just using dicts. For users of python3.5 and earlier, this will only change the order of their output posterior. On the other hand, it will make pickling priors much more straight forward going forward.
Merge request reports
Activity
Test script
Running this simple script
import bilby import pickle priors = dict( chirp_mass=bilby.core.prior.Uniform(10, 20), mass_ratio=bilby.core.prior.Uniform(0.125, 1), ) priors = bilby.gw.prior.BBHPriorDict(priors) with open("test.pickle", "wb") as file: pickle.dump(priors, file) with open("test.pickle", "rb") as file: priors_loaded = pickle.load(file) print("\nPriors={}".format(priors)) print("\nLoaded={}".format(priors_loaded))
Old behaviour
This is the old behaviour. As you see, the original prior is set correctly with just chirp mass and mass ratio. But, when you load the file from pickle, it re initialises the prior which then uses the defaults. This gives you funky and unexpected behaviour.
python pickle_bbh_prior.py 11:48 bilby INFO : No prior given, using default BBH priors in /home/user1/anaconda3/envs/bilby/lib/python3.6/site-packages/bilby-0.5.9-py3.6.egg/bilby/gw/prior_files/binary_black_holes.prior. Priors=BBHPriorDict([('chirp_mass', Uniform(minimum=10, maximum=20, name=None, latex_label=None, unit=None, boundary=None)), ('mass_ratio', Uniform(minimum=0.125, maximum=1, name=None, latex_label=None, unit=None, boundary=None))]) Loaded=BBHPriorDict([('mass_1', Uniform(minimum=5, maximum=100, name='mass_1', latex_label='$m_1$', unit='$M_{\\odot}$', boundary=None)), ('mass_2', Uniform(minimum=5, maximum=100, name='mass_2', latex_label='$m_2$', unit='$M_{\\odot}$', boundary=None)), ('mass_ratio', Uniform(minimum=0.125, maximum=1, name=None, latex_label=None, unit=None, boundary=None)), ('a_1', Uniform(minimum=0, maximum=0.8, name='a_1', latex_label='$a_1$', unit=None, boundary='reflective')), ('a_2', Uniform(minimum=0, maximum=0.8, name='a_2', latex_label='$a_2$', unit=None, boundary='reflective')), ('tilt_1', Sine(name='tilt_1', latex_label='$\\theta_1$', unit=None, minimum=0, maximum=3.141592653589793, boundary='reflective')), ('tilt_2', Sine(name='tilt_2', latex_label='$\\theta_2$', unit=None, minimum=0, maximum=3.141592653589793, boundary='reflective')), ('phi_12', Uniform(minimum=0, maximum=6.283185307179586, name='phi_12', latex_label='$\\Delta\\phi$', unit=None, boundary='periodic')), ('phi_jl', Uniform(minimum=0, maximum=6.283185307179586, name='phi_jl', latex_label='$\\phi_{JL}$', unit=None, boundary='periodic')), ('luminosity_distance', UniformSourceFrame(minimum=100.0, maximum=5000.0, cosmology=FlatLambdaCDM(name="Planck15", H0=67.7 km / (Mpc s), Om0=0.307, Tcmb0=2.725 K, Neff=3.05, m_nu=[0. 0. 0.06] eV, Ob0=0.0486), name='luminosity_distance', latex_label='$d_L$', unit=Unit("Mpc"), boundary=None)), ('dec', Cosine(name='dec', latex_label='$\\mathrm{DEC}$', unit=None, minimum=-1.5707963267948966, maximum=1.5707963267948966, boundary='reflective')), ('ra', Uniform(minimum=0, maximum=6.283185307179586, name='ra', latex_label='$\\mathrm{RA}$', unit=None, boundary='periodic')), ('theta_jn', Sine(name='theta_jn', latex_label='$\\theta_{JN}$', unit=None, minimum=0, maximum=3.141592653589793, boundary='reflective')), ('psi', Uniform(minimum=0, maximum=3.141592653589793, name='psi', latex_label='$\\psi$', unit=None, boundary='periodic')), ('phase', Uniform(minimum=0, maximum=6.283185307179586, name='phase', latex_label='$\\phi$', unit=None, boundary='periodic')), ('chirp_mass', Uniform(minimum=10, maximum=20, name=None, latex_label=None, unit=None, boundary=None))])
New behaviour
By using
dict
rather thanOrderedDict
, this behaviour doesn't happen.$ python python pickle_bbh_prior.py Priors={'chirp_mass': Uniform(minimum=10, maximum=20, name=None, latex_label=None, unit=None, boundary=None), 'mass_ratio': Uniform(minimum=0.125, maximum=1, name=None, latex_label=None, unit=None, boundary=None)} Loaded={'chirp_mass': Uniform(minimum=10, maximum=20, name=None, latex_label=None, unit=None, boundary=None), 'mass_ratio': Uniform(minimum=0.125, maximum=1, name=None, latex_label=None, unit=None, boundary=None)}
changed milestone to %0.6.0
mentioned in commit 09eecef8