nuclear-multimessenger-astronomy / nmma

A pythonic library for probing nuclear physics and cosmology with multimessenger analysis
https://nuclear-multimessenger-astronomy.github.io/nmma/
GNU General Public License v3.0
30 stars 58 forks source link

Timeshift in injection files not included when data is processed by utils.py #357

Open bking-astro opened 5 months ago

bking-astro commented 5 months ago

Please fill out relevant sections below; remove those that are unused.

Describe the bug When performing analysis on some injected light curves with the rubin-ToO flag all the light curve data is removed by the function processData() in utils.py. This is caused by the timeshift inside the injection file being ignored when processing data. Normally this does not cause an error because the data points span a large enough range to still be captured, but with rubin ToO, the data is bunched together within one day. Thus if the injected timeshift is >1day all the data gets filtered out.

To Reproduce Steps to reproduce the behavior:

  1. Create injection file with timeshift > 1day

  2. Run command:

    lightcurve-analysis \
        --model LANLTP2 \
        --svd-path /lustre/scratch5/bking/kn_only/svdmodels/ \
        --filters sdssu,ps1__g,ps1__r,ps1__i,ps1__z,ps1__y \
        --rubin-ToO \
        --rubin-ToO-type BNS \
        --detection-limit "{'sdssu':23.8,'ps1__g':24.5,'ps1__r':24,'ps1__i':23.4,'ps1__z':22.7,'ps1__y':23}" \
        --local-only \
        --interpolation-type tensorflow \
        --outdir /lustre/scratch5/bking/kn_only/outdir/TP1_rubin \
        --label TP1_rubin_$i \
        --prior /lustre/scratch5/bking/nmma/priors/LANL2022.prior \
        --tmin 0. \
        --tmax 12 \
        --dt 0.1 \
        --error-budget 1 \
        --nlive 1024 \
        --Ebv-max 0 \
        --injection /lustre/scratch5/bking/kn_only/ztf_inj/injections/TP1/inj_LANLTP1_${i}.json \
        --injection-num 0 \
        --injection-model LANLTP1 \
        --injection-outfile /lustre/scratch5/bking/kn_only/outdir/TP1_rubin/lcTP1_${i}.csv \
        --generation-seed 444 \
        --plot

    with the appropriate file paths

  3. See error

    UserWarning: genfromtxt: Empty input file: "/lustre/scratch5/bking/kn_only/outdir/TP1_rubin/pm_TP1_rubin_1//ev.dat"
    dead_points = np.genfromtxt(dir_ + "/ev.dat")
    Traceback (most recent call last):
    File "/usr/projects/nsmergers/nmma_0p2p0/bin/lightcurve-analysis", line 8, in <module>
    sys.exit(main())
             ^^^^^^
    File "/usr/projects/nsmergers/nmma_0p2p0/lib/python3.11/site-packages/nmma/em/analysis.py", line 1020, in main
    analysis(args)
    File "/usr/projects/nsmergers/nmma_0p2p0/lib/python3.11/site-packages/nmma/em/analysis.py", line 704, in analysis
    result = bilby.run_sampler(
             ^^^^^^^^^^^^^^^^^^
    File "/usr/projects/nsmergers/nmma_0p2p0/lib/python3.11/site-packages/bilby/core/sampler/__init__.py", line 234, in run_sampler
    result = sampler.run_sampler()
             ^^^^^^^^^^^^^^^^^^^^^
    File "/usr/projects/nsmergers/nmma_0p2p0/lib/python3.11/site-packages/bilby/core/sampler/base_sampler.py", line 97, in wrapped
    output = method(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/projects/nsmergers/nmma_0p2p0/lib/python3.11/site-packages/bilby/core/sampler/pymultinest.py", line 178, in run_sampler
    self.result.nested_samples = self._nested_samples
                                 ^^^^^^^^^^^^^^^^^^^^
    File "/usr/projects/nsmergers/nmma_0p2p0/lib/python3.11/site-packages/bilby/core/sampler/pymultinest.py", line 201, in _nested_samples
    np.vstack([dead_points, live_points]).copy(),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/projects/nsmergers/nmma_0p2p0/lib/python3.11/site-packages/numpy/core/shape_base.py", line 289, in vstack
    return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 11

Expected behavior All the injected light curve should be processed by NMMA and the likelihood doesn't evaluate to zero.

Platform information:

Additional context Easy fix I used was:

    # create the kilonova data if an injection set is given
    if args.injection:
        with open(args.injection, "r") as f:
            injection_dict = json.load(
                f, object_hook=bilby.core.utils.decode_bilby_json
            )
        injection_df = injection_dict["injections"]
        injection_parameters = injection_df.iloc[args.injection_num].to_dict()

        if "geocent_time" in injection_parameters:
            tc_gps = time.Time(injection_parameters["geocent_time"], format="gps")
        elif "geocent_time_x" in injection_parameters:
            tc_gps = time.Time(injection_parameters["geocent_time_x"], format="gps")
        else:
            print("Need either geocent_time or geocent_time_x")
            exit(1)

        **if "timeshift" in injection_parameters:
            timeshift = injection_parameters["timeshift"]
        else:
            timeshift = 0**

        **trigger_time = tc_gps.mjd + timeshift**