In [39]:
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from threeML import *
from threeML.io.package_data import get_path_of_data_file
import warnings
warnings.simplefilter('ignore')
Many times we encounter event lists or sets of spectral histograms from which we would like to derive a single or set of plugins. For this purpose, we provide the TimeSeriesBuilder which provides a unified interface to time series data. Here we will demonstrate how to construct plugins from an different of data types.
The TimeSeriesBuilder currently supports reading of the following data type:
If you would like to build a time series from your own custom data, consider creating a TimeSeriesBuilder.from_your_data() class method.
Building plugins from GBM is achieved in the following fashion
In [40]:
cspec_file = get_path_of_data_file('datasets/glg_cspec_n3_bn080916009_v01.pha')
tte_file = get_path_of_data_file('datasets/glg_tte_n3_bn080916009_v01.fit.gz')
gbm_rsp = get_path_of_data_file('datasets/glg_cspec_n3_bn080916009_v00.rsp2')
gbm_cspec = TimeSeriesBuilder.from_gbm_cspec_or_ctime('nai3_cspec',
cspec_or_ctime_file=cspec_file,
rsp_file=gbm_rsp)
gbm_tte = TimeSeriesBuilder.from_gbm_tte('nai3_tte',
tte_file=tte_file,
rsp_file=gbm_rsp)
In [4]:
lle_file = get_path_of_data_file('datasets/gll_lle_bn080916009_v10.fit')
ft2_file = get_path_of_data_file('datasets/gll_pt_bn080916009_v10.fit')
lle_rsp = get_path_of_data_file('datasets/gll_cspec_bn080916009_v10.rsp')
lat_lle = TimeSeriesBuilder.from_lat_lle('lat_lle',
lle_file=lle_file,
ft2_file=ft2_file,
rsp_file=lle_rsp)
In [5]:
threeML_config['lightcurve']['lightcurve color'] = '#07AE44'
gbm_tte.view_lightcurve(start=-20,stop=200);
Perhaps we want to fit the time interval from 0-10 seconds. We make a selection like this:
In [41]:
threeML_config['lightcurve']['selection color'] = '#4C3CB7'
gbm_tte.set_active_time_interval('0-10')
gbm_tte.view_lightcurve(start=-20,stop=200);
For event list style data like time tagged events, the selection is exact. However, pre-binned data in the form of e.g. PHAII files will have the selection automatically adjusted to the underlying temporal bins.
Several discontinuous time selections can be made.
In order to get to a plugin, we need to model and create an estimated background in each channel ($ B_i$) for our interval of interest. The process that we have implemented is to fit temporal off-source regions to polynomials ($P(t;\vec{\theta})$) in time. First, a polynomial is fit to the total count rate. From this fit we determine the best polynomial order via a likelihood ratio test, unless the user supplies a polynomial order in the constructor or directly via the polynomial_order attribute. Then, this order of polynomial is fit to every channel in the data.
From the polynomial fit, the polynomial is integrated in time over the active source interval to estimate the count rate in each channel. The estimated background and background errors then stored for each channel.
$$ B_i = \int_{T_1}^{T_2}P(t;\vec{\theta}) {\rm d}t $$
In [42]:
threeML_config['lightcurve']['background color'] = '#FC2530'
gbm_tte.set_background_interval('-24--5','100-200')
gbm_tte.view_lightcurve(start=-20,stop=200);
For event list data, binned or unbinned background fits are possible. For pre-binned data, only a binned fit is possible.
In [10]:
gbm_tte.set_background_interval('-24--5','100-200',unbinned=False)
In [19]:
gbm_tte.save_background('background_store',overwrite=True)
In [20]:
gbm_tte_reloaded = TimeSeriesBuilder.from_gbm_tte('nai3_tte',
tte_file=tte_file,
rsp_file=gbm_rsp,
restore_background='background_store.h5'
)
In [22]:
gbm_tte_reloaded.view_lightcurve(-10,100);
In [11]:
gbm_plugin = gbm_tte.to_spectrumlike()
In [12]:
gbm_plugin.display()
In [24]:
gbm_tte.create_time_bins(start=0, stop=10, method='constant', dt=2.)
In [25]:
gbm_tte.bins.display()
In [26]:
time_edges = np.array([.5,.63,20.,21.])
starts = time_edges[:-1]
stops = time_edges[1:]
gbm_tte.create_time_bins(start=starts, stop=stops, method='custom')
In [27]:
gbm_tte.bins.display()
In [44]:
gbm_tte.create_time_bins(start=0., stop=50., method='significance', sigma=50)
In [45]:
gbm_tte.bins.display()
In [46]:
gbm_tte.create_time_bins(start=0., stop=50., method='bayesblocks', p0=.01, use_background=True)
In [47]:
gbm_tte.bins.display()
In [51]:
gbm_tte.view_lightcurve(use_binner=True);
The bins can all be writted to a PHAII file for analysis via OGIPLike.
In [53]:
gbm_tte.write_pha_from_binner(file_name='out'
force_write_rsp = False # if you need to write the RSP to a file. We try to choose the best option for you.
)
Similarly, we can create a list of plugins directly from the time series.
In [54]:
my_plugins = gbm_tte.to_spectrumlike(from_bins=True)
In [ ]: