a-renzini / pygwb

MIT License
3 stars 3 forks source link

Cannot generate CBC population #10

Closed cmbiwer closed 8 months ago

cmbiwer commented 9 months ago

I am trying to follow this section of the tutorial: https://pygwb.docs.ligo.org/pygwb/simulator.html#specifying-the-cbc-population

When I run this code:

import numpy as np
import gwpy.frequencyseries
from pygwb.detector import Interferometer
import bilby.gw.detector
from pygwb.network import Network
from pygwb.parameters import Parameters
from pygwb.baseline import Baseline

duration = 64 # duration of each segment of data (s)
N_segs = 5  # number of data segments to generate
sampling_frequency = 1024 # Hz

ifo_H1 = Interferometer.get_empty_interferometer('H1')
ifo_L1 = Interferometer.get_empty_interferometer('L1')

ifo_list = [ifo_H1, ifo_L1]

for ifo in ifo_list:
    ifo.duration = duration
    ifo.sampling_frequency = sampling_frequency
    ifo.power_spectral_density = bilby.gw.detector.PowerSpectralDensity(ifo.frequency_array, np.nan_to_num(ifo.power_spectral_density_array, posinf=1.e-41))
net_HL = Network('HL', ifo_list)

priors = bilby.gw.prior.BBHPriorDict(aligned_spin=True)
priors['chirp_mass'] = bilby.core.prior.Uniform(2, 30, name="chirp_mass")
priors['mass_ratio'] = 1.0
priors['chi_1'] = 0
priors['chi_2'] = 0
priors['luminosity_distance'] = bilby.core.prior.PowerLaw(alpha=2, name='luminosity_distance',
                                                      minimum=10, maximum=100,
                                                      unit='Mpc')
priors["geocent_time"] = bilby.core.prior.Uniform(0, duration*N_segs, name="geocent_time")

# create 20 injections
injections = priors.sample(20)

import json

with open("injections.json", "w") as file:
    json.dump(
        injections, file, indent=2, cls=bilby.core.result.BilbyJsonEncoder
    )

net_HL.set_interferometer_data_from_simulator(N_segs, CBC_dict=injections, sampling_frequency = sampling_frequency)

I get the following error:

Traceback (most recent call last):
  File "/Users/cmbiwer/src/pygwb/tutorials/test.py", line 115, in <module>
    priors = bilby.gw.prior.BBHPriorDict(aligned_spin=True)
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/gw/prior.py", line 886, in __init__
    super(BBHPriorDict, self).__init__(dictionary=dictionary, filename=filename,
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/core/prior/dict.py", line 667, in __init__
    super(ConditionalPriorDict, self).__init__(
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/core/prior/dict.py", line 44, in __init__
    self.from_file(filename)
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/core/prior/dict.py", line 158, in from_file
    self.from_dictionary(prior)
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/core/prior/dict.py", line 279, in from_dictionary
    dictionary[key] = cls.from_repr(args)
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/gw/prior.py", line 293, in from_repr
    return cls._from_repr(string)
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/core/prior/base.py", line 358, in _from_repr
    return cls(**kwargs)
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/gw/prior.py", line 151, in __init__
    self.minimum = minimum
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/gw/prior.py", line 173, in minimum
    self._set_limit(value=minimum, limit_dict=self._minimum)
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/gw/prior.py", line 210, in _set_limit
    limit_dict['redshift'] = z_at_value(
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/bilby/gw/cosmology.py", line 93, in z_at_value
    return z_at_value(func=func, fval=fval, **kwargs).value
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/astropy/cosmology/funcs/optimize.py", line 386, in z_at_value
    zs[...] = _z_at_scalar_value(
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/astropy/cosmology/funcs/optimize.py", line 89, in _z_at_scalar_value
    res = minimize_scalar(
  File "/Users/cmbiwer/anaconda3/envs/pygwb/lib/python3.9/site-packages/scipy/optimize/_minimize.py", line 908, in minimize_scalar
    raise ValueError(message)
ValueError: Use of `bounds` is incompatible with 'method=Brent'.

Is there something missing from the tutorial? Or something I missed?

kevinturbang commented 9 months ago

This is strange, just ran this and things seem to work fine for me. It would seem the issue is related to the generation of the bilby dictionary. Are you working in a conda environment with the most recent version of pygwb installed?

cmbiwer commented 8 months ago

I was able to get this to work now. I think the issue was I had a slightly older version of pygwb/dependencies than what's currently on master. If I install from what's on master it works. Must have been some change.

cmbiwer commented 8 months ago

I think in part it may have been because of installing jupyter. Because if I install jupyter from conda, it breaks the example.