コード例 #1
0
ファイル: test_time_series.py プロジェクト: giacomov/3ML
def test_read_gbm_cspec():
    with within_directory(datasets_directory):
        data_dir = os.path.join('gbm', 'bn080916009')

        nai3 = TimeSeriesBuilder.from_gbm_cspec_or_ctime('NAI3',
                                                         os.path.join(data_dir, "glg_cspec_n3_bn080916009_v01.pha"),
                                                         rsp_file=os.path.join(data_dir,
                                                                               "glg_cspec_n3_bn080916009_v00.rsp2"),
                                                         poly_order=-1)
        
        nai3.set_active_time_interval('0-1')
        
        assert not nai3.time_series.poly_fit_exists

        assert nai3.time_series.binned_spectrum_set.n_channels>0

        nai3.set_background_interval('-20--10', '100-200')

        assert nai3.time_series.poly_fit_exists


        speclike = nai3.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)

        assert not speclike.background_spectrum.is_poisson

        speclike = nai3.to_spectrumlike(extract_measured_background=True)

        assert isinstance(speclike, DispersionSpectrumLike)

        assert speclike.background_spectrum.is_poisson

        nai3.write_pha_from_binner('test_from_nai3', start=0, stop=2, overwrite=True)
コード例 #2
0
ファイル: test_time_series.py プロジェクト: Husky22/threeML
def test_read_gbm_cspec():
    with within_directory(datasets_directory):
        data_dir = os.path.join('gbm', 'bn080916009')

        nai3 = TimeSeriesBuilder.from_gbm_cspec_or_ctime('NAI3',
                                                         os.path.join(data_dir, "glg_cspec_n3_bn080916009_v01.pha"),
                                                         rsp_file=os.path.join(data_dir,
                                                                               "glg_cspec_n3_bn080916009_v00.rsp2"),
                                                         poly_order=-1)
        
        nai3.set_active_time_interval('0-1')
        
        assert not nai3.time_series.poly_fit_exists

        assert nai3.time_series.binned_spectrum_set.n_channels>0

        nai3.set_background_interval('-20--10', '100-200')

        assert nai3.time_series.poly_fit_exists


        speclike = nai3.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)

        assert not speclike.background_spectrum.is_poisson

        speclike = nai3.to_spectrumlike(extract_measured_background=True)

        assert isinstance(speclike, DispersionSpectrumLike)

        assert speclike.background_spectrum.is_poisson

        nai3.write_pha_from_binner('test_from_nai3', start=0, stop=2, overwrite=True)
コード例 #3
0
def test_read_lle():
    with within_directory(datasets_directory):
        data_dir = "lat"

        lle = TimeSeriesBuilder.from_lat_lle(
            "lle",
            os.path.join(data_dir, "gll_lle_bn080916009_v10.fit"),
            os.path.join(data_dir, "gll_pt_bn080916009_v10.fit"),
            rsp_file=os.path.join(data_dir, "gll_cspec_bn080916009_v10.rsp"),
            poly_order=-1,
        )

        lle.view_lightcurve()

        lle.set_active_time_interval("0-10")

        lle.set_background_interval("-150-0", "100-250", unbinned=False)

        speclike = lle.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)

        # will test background with lle data

        old_coefficients, old_errors = lle.get_background_parameters()

        old_tmin_list = lle._time_series.poly_intervals

        lle.save_background("temp_lle", overwrite=True)

        lle = TimeSeriesBuilder.from_lat_lle(
            "lle",
            os.path.join(data_dir, "gll_lle_bn080916009_v10.fit"),
            os.path.join(data_dir, "gll_pt_bn080916009_v10.fit"),
            rsp_file=os.path.join(data_dir, "gll_cspec_bn080916009_v10.rsp"),
            restore_background="temp_lle.h5",
        )

        new_coefficients, new_errors = lle.get_background_parameters()

        new_tmin_list = lle._time_series.poly_intervals

        assert new_coefficients == old_coefficients

        assert new_errors == old_errors

        assert old_tmin_list == new_tmin_list
コード例 #4
0
ファイル: test_time_series.py プロジェクト: giacomov/3ML
def test_read_lle():
    with within_directory(datasets_directory):
        data_dir = 'lat'

        lle = TimeSeriesBuilder.from_lat_lle('lle', os.path.join(data_dir, "gll_lle_bn080916009_v10.fit"),
                                             os.path.join(data_dir, "gll_pt_bn080916009_v10.fit"),
                                             rsp_file=os.path.join(data_dir, "gll_cspec_bn080916009_v10.rsp"),
                                             poly_order=-1)

        lle.view_lightcurve()

        lle.set_active_time_interval("0-10")

        lle.set_background_interval("-150-0", "100-250")

        speclike = lle.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)

        # will test background with lle data


        old_coefficients, old_errors = lle.get_background_parameters()

        old_tmin_list = lle._time_series.poly_intervals

        lle.save_background('temp_lle', overwrite=True)

        lle = TimeSeriesBuilder.from_lat_lle('lle', os.path.join(data_dir, "gll_lle_bn080916009_v10.fit"),
                                             os.path.join(data_dir, "gll_pt_bn080916009_v10.fit"),
                                             rsp_file=os.path.join(data_dir, "gll_cspec_bn080916009_v10.rsp"),
                                             restore_background='temp_lle.h5')

        new_coefficients, new_errors = lle.get_background_parameters()

        new_tmin_list = lle._time_series.poly_intervals

        assert new_coefficients == old_coefficients

        assert new_errors == old_errors

        assert old_tmin_list == new_tmin_list
コード例 #5
0
def test_read_gbm_cspec():
    with within_directory(datasets_directory):
        data_dir = os.path.join('gbm', 'bn080916009')

        nai3 = TimeSeriesBuilder.from_gbm_cspec_or_ctime('NAI3',
                                                         os.path.join(data_dir, "glg_cspec_n3_bn080916009_v01.pha"),
                                                         rsp_file=os.path.join(data_dir,
                                                                               "glg_cspec_n3_bn080916009_v00.rsp2"),
                                                         poly_order=-1)

        nai3.set_active_time_interval('0-1')
        nai3.set_background_interval('-200--10', '100-200')

        speclike = nai3.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)

        nai3.write_pha_from_binner('test_from_nai3', start=0, stop=2, overwrite=True)
コード例 #6
0
    def _create_timeseries(self):
        """
        create all the time series for each detector
        :return: None
        """

        self._time_series = collections.OrderedDict()

        for det_num in range(14):

            # detectors are arranged [time,det,channel]

            # for now just keep the normal exposure

            # we will create binned spectra for each time slice

            drm_gen = DRMGenTrig(
                self._qauts,
                self._sc_pos,
                det_num,  # det number
                tstart=self._tstart,
                tstop=self._tstop,
                mat_type=2,
                time=0,
                occult=True
            )

            # we will use a single response for each detector

            tmp_drm = BALROG_DRM(drm_gen, 0, 0)

            # extract the counts

            counts = self._rates[:, det_num, :] * self._time_intervals.widths.reshape(
                (len(self._time_intervals), 1)
            )

            # now create a binned spectrum for each interval

            binned_spectrum_list = []

            for c, start, stop in zip(counts, self._tstart, self._tstop):
                binned_spectrum_list.append(
                    BinnedSpectrumWithDispersion(
                        counts=c,
                        exposure=stop - start,
                        response=tmp_drm,
                        tstart=start,
                        tstop=stop,
                    )
                )

            # make a binned spectrum set

            bss = BinnedSpectrumSet(
                binned_spectrum_list,
                reference_time=0.0,
                time_intervals=self._time_intervals,
            )

            # convert that set to a series

            bss2 = BinnedSpectrumSeries(bss, first_channel=0)

            # now we need to get the name of the detector

            name = lu[det_num]

            if self._restore_poly_fit is not None:
                bkg_fit_file = self._restore_poly_fit.get(name, None)
            else:
                bkg_fit_file = None

            # create a time series builder which can produce plugins

            tsb = TimeSeriesBuilder(
                name,
                bss2,
                response=tmp_drm,
                verbose=self._verbose,
                poly_order=self._poly_order,
                restore_poly_fit=bkg_fit_file,
            )

            # attach that to the full list

            self._time_series[name] = tsb
コード例 #7
0
def test_read_gbm_tte():
    with within_directory(datasets_directory):
        data_dir = os.path.join("gbm", "bn080916009")

        nai3 = TimeSeriesBuilder.from_gbm_tte(
            "NAI3",
            os.path.join(data_dir, "glg_tte_n3_bn080916009_v01.fit.gz"),
            rsp_file=os.path.join(data_dir,
                                  "glg_cspec_n3_bn080916009_v00.rsp2"),
            poly_order=-1,
        )

        nai3.set_active_time_interval("0-1")
        nai3.set_background_interval("-20--10", "100-200", unbinned=False)

        speclike = nai3.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)

        assert not speclike.background_spectrum.is_poisson

        speclike = nai3.to_spectrumlike(extract_measured_background=True)

        assert isinstance(speclike, DispersionSpectrumLike)

        assert speclike.background_spectrum.is_poisson

        # test binning

        # should not have bins yet

        with pytest.raises(RuntimeError):
            nai3.bins

        # First catch the errors

        # This is without specifying the correct options name

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method="constant")

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method="significance")

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method="constant", p0=0.1)

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0,
                                  stop=10,
                                  method="significance",
                                  dt=1)

        # now incorrect options

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method="not_a_method")

        # Now test values

        nai3.create_time_bins(start=0, stop=10, method="constant", dt=1)

        assert len(nai3.bins) == 10

        assert nai3.bins.argsort() == list(range(len(nai3.bins)))

        nai3.create_time_bins(start=0, stop=10, method="bayesblocks", p0=0.1)

        assert nai3.bins.argsort() == list(range(len(nai3.bins)))

        assert len(nai3.bins) == 5

        nai3.create_time_bins(start=0,
                              stop=10,
                              method="significance",
                              sigma=40)

        assert nai3.bins.argsort() == list(range(len(nai3.bins)))

        assert len(nai3.bins) == 5

        nai3.view_lightcurve(use_binner=True)

        nai3.write_pha_from_binner("test_from_nai3",
                                   overwrite=True,
                                   force_rsp_write=True)
コード例 #8
0
ファイル: fit.py プロジェクト: grburgess/morgoth
    def _set_plugins(self):
        """
        Set the plugins using the saved background hdf5 files
        :return:
        """

        det_ts = []
        det_rsp = []

        for det in self._use_dets:

            # set up responses
            tte_file = f"{base_dir}/{self._grb_name}/tte/data/glg_tte_{det}_bn{self._grb_name[3:]}_{self._version}.fit"
            cspec_file = f"{base_dir}/{self._grb_name}/tte/data/glg_cspec_{det}_bn{self._grb_name[3:]}_{self._version}.pha"

            rsp = drm.DRMGenTTE(tte_file=tte_file,
                                trigdat=self._trigdat_file,
                                mat_type=2,
                                cspecfile=cspec_file,
                                occult=True)

            det_rsp.append(rsp)

            # Time Series
            gbm_tte_file = GBMTTEFile(tte_file)

            event_list = EventListWithDeadTime(
                arrival_times=gbm_tte_file.arrival_times -
                gbm_tte_file.trigger_time,
                measurement=gbm_tte_file.energies,
                n_channels=gbm_tte_file.n_channels,
                start_time=gbm_tte_file.tstart - gbm_tte_file.trigger_time,
                stop_time=gbm_tte_file.tstop - gbm_tte_file.trigger_time,
                dead_time=gbm_tte_file.deadtime,
                first_channel=0,
                instrument=gbm_tte_file.det_name,
                mission=gbm_tte_file.mission,
                verbose=True,
            )

            success_restore = False
            i = 0
            while not success_restore:
                try:
                    ts = TimeSeriesBuilder(
                        det,
                        event_list,
                        response=BALROG_DRM(rsp, 0.0, 0.0),
                        unbinned=False,
                        verbose=True,
                        container_type=BinnedSpectrumWithDispersion,
                        restore_poly_fit=self._bkg_fit_files[det],
                    )

                    success_restore = True
                    i = 0
                except:
                    time.sleep(1)
                i += 1
                if i == 50:
                    raise AssertionError("Can not restore background fit...")

            ts.set_active_time_interval(
                f"{self._active_time_start}-{self._active_time_stop}")
            det_ts.append(ts)

        # Mean of active time
        rsp_time = (float(self._active_time_start) +
                    float(self._active_time_stop)) / 2

        # Spectrum Like
        det_sl = []
        # set up energy range
        for series in det_ts:
            if series._name != "b0" and series._name != "b1":
                sl = series.to_spectrumlike()
                sl.set_active_measurements("8.1-700")
                det_sl.append(sl)
            else:
                sl = series.to_spectrumlike()
                sl.set_active_measurements("350-25000")
                det_sl.append(sl)

        # Make Balrog Like
        det_bl = []
        for i, det in enumerate(self._use_dets):
            det_bl.append(
                drm.BALROGLike.from_spectrumlike(det_sl[i],
                                                 rsp_time,
                                                 det_rsp[i],
                                                 free_position=True))

        self._data_list = DataList(*det_bl)
コード例 #9
0
def test_read_gbm_tte():
    with within_directory(datasets_directory):
        data_dir = os.path.join('gbm', 'bn080916009')

        nai3 = TimeSeriesBuilder.from_gbm_tte(
            'NAI3',
            os.path.join(data_dir, "glg_tte_n3_bn080916009_v01.fit.gz"),
            rsp_file=os.path.join(data_dir,
                                  "glg_cspec_n3_bn080916009_v00.rsp2"),
            poly_order=-1)

        nai3.set_active_time_interval('0-1')
        nai3.set_background_interval('-20--10', '100-200')

        speclike = nai3.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)

        assert not speclike.background_spectrum.is_poisson

        speclike = nai3.to_spectrumlike(extract_measured_background=True)

        assert isinstance(speclike, DispersionSpectrumLike)

        assert speclike.background_spectrum.is_poisson

        # test binning

        # should not have bins yet

        with pytest.raises(RuntimeError):
            nai3.bins

        # First catch the errors

        # This is without specifying the correct options name

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='constant')

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='significance')

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='constant', p0=.1)

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0,
                                  stop=10,
                                  method='significance',
                                  dt=1)

        # now incorrect options

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='not_a_method')

        # Now test values

        nai3.create_time_bins(start=0, stop=10, method='constant', dt=1)

        assert len(nai3.bins) == 10

        assert nai3.bins.argsort() == range(len(nai3.bins))

        nai3.create_time_bins(start=0, stop=10, method='bayesblocks', p0=.1)

        assert nai3.bins.argsort() == range(len(nai3.bins))

        assert len(nai3.bins) == 5

        nai3.create_time_bins(start=0,
                              stop=10,
                              method='significance',
                              sigma=40)

        assert nai3.bins.argsort() == range(len(nai3.bins))

        assert len(nai3.bins) == 5

        nai3.view_lightcurve(use_binner=True)

        nai3.write_pha_from_binner('test_from_nai3', overwrite=True)
コード例 #10
0
ファイル: test_time_series.py プロジェクト: giacomov/3ML
def test_read_gbm_tte():
    with within_directory(datasets_directory):
        data_dir = os.path.join('gbm', 'bn080916009')

        nai3 = TimeSeriesBuilder.from_gbm_tte('NAI3',
                                              os.path.join(data_dir, "glg_tte_n3_bn080916009_v01.fit.gz"),
                                              rsp_file=os.path.join(data_dir, "glg_cspec_n3_bn080916009_v00.rsp2"),
                                              poly_order=-1)

        nai3.set_active_time_interval('0-1')
        nai3.set_background_interval('-20--10', '100-200')




        speclike = nai3.to_spectrumlike()

        assert isinstance(speclike, DispersionSpectrumLike)


        assert not speclike.background_spectrum.is_poisson

        speclike = nai3.to_spectrumlike(extract_measured_background=True)

        assert isinstance(speclike, DispersionSpectrumLike)

        assert speclike.background_spectrum.is_poisson



        # test binning


        # should not have bins yet



        with pytest.raises(RuntimeError):
            nai3.bins

        # First catch the errors


        # This is without specifying the correct options name





        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='constant')

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='significance')

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='constant', p0=.1)

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='significance', dt=1)

        # now incorrect options

        with pytest.raises(RuntimeError):
            nai3.create_time_bins(start=0, stop=10, method='not_a_method')

        # Now test values



        nai3.create_time_bins(start=0, stop=10, method='constant', dt=1)

        assert len(nai3.bins) == 10

        assert nai3.bins.argsort() == range(len(nai3.bins))

        nai3.create_time_bins(start=0, stop=10, method='bayesblocks', p0=.1)

        assert nai3.bins.argsort() == range(len(nai3.bins))

        assert len(nai3.bins) == 5

        nai3.create_time_bins(start=0, stop=10, method='significance', sigma=40)

        assert nai3.bins.argsort() == range(len(nai3.bins))

        assert len(nai3.bins) == 5

        nai3.view_lightcurve(use_binner=True)

        nai3.write_pha_from_binner('test_from_nai3', overwrite=True)
コード例 #11
0
    def _build_bkg_plugins(self):
        """
        Build the background plugins for all dets
        :return:
        """
        # Time selection from yaml file
        with open(self._time_selection_file_path, "r") as f:
            data = yaml.safe_load(f)

            active_time = (
                f"{data['active_time']['start']}-{data['active_time']['stop']}"
            )
            background_time_neg = f"{data['background_time']['before']['start']}-{data['background_time']['before']['stop']}"
            background_time_pos = f"{data['background_time']['after']['start']}-{data['background_time']['after']['stop']}"
            poly_order = data["poly_order"]

        det_ts = []

        for det in _gbm_detectors:
            # Response Setup

            rsp = BALROG_DRM(
                drm.DRMGenTTE(
                    tte_file=self._tte_files[det],
                    trigdat=self._trigdat_file,
                    mat_type=2,
                    cspecfile=self._cspec_files[det],
                ),
                0.0,
                0.0,
            )

            # Time Series
            gbm_tte_file = GBMTTEFile(self._tte_files[det])
            event_list = EventListWithDeadTime(
                arrival_times=gbm_tte_file.arrival_times -
                gbm_tte_file.trigger_time,
                measurement=gbm_tte_file.energies,
                n_channels=gbm_tte_file.n_channels,
                start_time=gbm_tte_file.tstart - gbm_tte_file.trigger_time,
                stop_time=gbm_tte_file.tstop - gbm_tte_file.trigger_time,
                dead_time=gbm_tte_file.deadtime,
                first_channel=0,
                instrument=gbm_tte_file.det_name,
                mission=gbm_tte_file.mission,
                verbose=True,
            )

            ts = TimeSeriesBuilder(
                det,
                event_list,
                response=rsp,
                poly_order=poly_order,
                unbinned=False,
                verbose=True,
                container_type=BinnedSpectrumWithDispersion,
            )

            ts.set_background_interval(background_time_neg,
                                       background_time_pos)
            ts.set_active_time_interval(active_time)
            det_ts.append(ts)

        self._ts = det_ts