8000 Time Series Builder for COSI GRBs: by saurabhmittal23 · Pull Request #239 · cositools/cosipy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Time Series Builder for COSI GRBs: #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 224 additions & 0 deletions cosipy/data_builder/Time_series_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
from threeML.utils.time_series.binned_spectrum_series import BinnedSpectrumSeries
from threeML.utils.spectrum.binned_spectrum import BinnedSpectrumWithDispersion
from threeML.utils.data_builders.time_series_builder import TimeSeriesBuilder
from threeML.utils.spectrum.binned_spectrum import BinnedSpectrum
from threeML.utils.spectrum.binned_spectrum_set import BinnedSpectrumSet
from threeML.utils.time_interval import TimeIntervalSet
from threeML.utils.OGIP.response import OGIPResponse
from cosipy.spacecraftfile import SpacecraftFile
from cosipy import BinnedData
import numpy as np
from astropy.time import Time
from astropy.coordinates import SkyCoord

Check warning on line 12 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L1-L12

Added lines #L1 - L12 were not covered by tests



class COSIGRBData:
def __init__(self, time_energy_counts, time_bins, energy_bins, deadtime=None):

Check warning on line 17 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L16-L17

Added lines #L16 - L17 were not covered by tests
"""
Initialize COSI GRB data.

:param time_energy_counts: 2D array of counts (time, energy)
:param time_bins: Array of time bin edges
:param energy_bins: Array of energy bin edges
:param deadtime: Array of deadtime for each time bin (optional)
"""
self.counts = time_energy_counts
self.time_bins = time_bins
self.energy_bins = energy_bins
self.deadtime = deadtime if deadtime is not None else np.zeros(len(time_bins) - 1)

Check warning on line 29 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L26-L29

Added lines #L26 - L29 were not covered by tests

self.times = (time_bins[:-1] + time_bins[1:]) / 2
self.energies = (energy_bins[:-1] + energy_bins[1:]) / 2

Check warning on line 32 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L31-L32

Added lines #L31 - L32 were not covered by tests


@property
def n_channels(self):
return len(self.energy_bins) - 1

Check warning on line 37 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L35-L37

Added lines #L35 - L37 were not covered by tests

@property
def time_start(self):
return self.time_bins[0]

Check warning on line 41 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L39-L41

Added lines #L39 - L41 were not covered by tests

@property
def time_stop(self):
return self.time_bins[-1]

Check warning on line 45 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L43-L45

Added lines #L43 - L45 were not covered by tests

@property
def livetime(self):
bin_widths = np.diff(self.time_bins)
return bin_widths - self.deadtime

Check warning on line 50 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L47-L50

Added lines #L47 - L50 were not covered by tests

@property
def response_file(self):

Check warning on line 53 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L52-L53

Added lines #L52 - L53 were not covered by tests
"""
:returns: Path to the response file
"""
return self._response_file

Check warning on line 57 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L57

Added line #L57 was not covered by tests

class TimeSeriesBuilderCOSI(TimeSeriesBuilder):
def __init__(

Check warning on line 60 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L59-L60

Added lines #L59 - L60 were not covered by tests
self,
name,
cosi_data,
response_file,
arf_file = None,
l=None,
b=None,
ori_file=None,
poly_order=-1,
verbose=True,
restore_poly_fit=None,
):
"""
Initialize TimeSeriesBuilderCOSI.

:param name: Name of the time series
:param cosi_data: COSIGRBData object (.hdf5 containing signal + background)
:param response_file: path to response file (either a .hdf5 file or .rsp file)
:param l: Galactic longitude (optional if response_file is OGIP compatible)
:param b: Galactic latitude (optional if response_file is OGIP compatible)
:param ori_file: Path to orientation file (optional if response is OGIP compatible)
:param poly_order: Polynomial order for background fitting
:param verbose: Verbosity flag
:param restore_poly_fit: File to restore background fit from
"""


if (response_file.endswith(".rmf")):
if (arf_file == None):
response = OGIPResponse(rsp_file = response_file, arf_file= response_file[:-3] + "arf")

Check warning on line 90 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L88-L90

Added lines #L88 - L90 were not covered by tests
else:
response = OGIPResponse(rsp_file = response_file, arf_file= arf_file)

Check warning on line 92 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L92

Added line #L92 was not covered by tests
else:
if l is None or b is None:
raise ValueError("Galactic coordinates (l, b) are required when response_file is not OGIP compatible")
if ori_file is None:
raise ValueError("Orientation file is required when response_file is not OGIP compatible")
response = self.create_ogip_response(name, response_file, cosi_data.time_bins, l, b, ori_file)

Check warning on line 98 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L94-L98

Added lines #L94 - L98 were not covered by tests


reference_time = cosi_data.time_bins[0]
intervals = [TimeIntervalSet.INTERVAL_TYPE(start, stop)

Check warning on line 102 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L101-L102

Added lines #L101 - L102 were not covered by tests
for start, stop in zip(cosi_data.time_bins[:-1], cosi_data.time_bins[1:])]
time_intervals = TimeIntervalSet(intervals)

Check warning on line 104 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L104

Added line #L104 was not covered by tests

# Create a list of BinnedSpectrum objects
binned_spectrum_list = []
for i in range(len(cosi_data.times)):
binned_spectrum = BinnedSpectrum(

Check warning on line 109 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L107-L109

Added lines #L107 - L109 were not covered by tests
counts=cosi_data.counts[i],
exposure=cosi_data.livetime[i],
ebounds=cosi_data.energy_bins,
quality=None, # Add quality array if available
mission='COSI',
instrument=name,
is_poisson=True # Assuming Poisson statistics
)
binned_spectrum_list.append(binned_spectrum)

Check warning on line 118 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L118

Added line #L118 was not covered by tests

# Create BinnedSpectrumSet
binned_spectrum_set = BinnedSpectrumSet(

Check warning on line 121 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L121

Added line #L121 was not covered by tests
binned_spectrum_list=binned_spectrum_list,
time_intervals=time_intervals,
reference_time=reference_time
)



# Create a BinnedSpectrumSeries object
binned_spectrum_series = BinnedSpectrumSeries(

Check warning on line 130 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L130

Added line #L130 was not covered by tests
binned_spectrum_set,
first_channel=0,
mission='COSI',
instrument=name,
verbose=verbose
)

super(TimeSeriesBuilderCOSI, self).__init__(

Check warning on line 138 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L138

Added line #L138 was not covered by tests
name,
binned_spectrum_series,
response=response,
poly_order=poly_order,
unbinned=False,
verbose=verbose,
restore_poly_fit=restore_poly_fit,
container_type=BinnedSpectrumWithDispersion
)

self._cosi_data = cosi_data
self._response_file = response_file

Check warning on line 150 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L149-L150

Added lines #L149 - L150 were not covered by tests

@staticmethod
def create_ogip_response(name, response_file, time_bins, l, b, ori_file):
ori = SpacecraftFile.parse_from_file(ori_file)
tmin = Time(time_bins[0],format = 'unix')
tmax = Time(time_bins[-1],format = 'unix')

Check warning on line 156 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L152-L156

Added lines #L152 - L156 were not covered by tests

sc_orientation = ori.source_interval(tmin, tmax)
coord = SkyCoord( l = l, b=b, frame='galactic', unit='deg')
sc_orientation.get_target_in_sc_frame(target_name = name, target_coord=coord)

Check warning on line 160 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L158-L160

Added lines #L158 - L160 were not covered by tests

dwell_map = sc_orientation.get_dwell_map(response=response_file, save=False)
psr_rsp = sc_orientation.get_psr_rsp(response=response_file)

Check warning on line 163 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L162-L163

Added lines #L162 - L163 were not covered by tests

rmf = sc_orientation.get_rmf(out_name=name)
arf = sc_orientation.get_arf(out_name=name)

Check warning on line 166 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L165-L166

Added lines #L165 - L166 were not covered by tests

return OGIPResponse(rsp_file = name + '.rmf', arf_file= name + '.arf')

Check warning on line 168 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L168

Added line #L168 was not covered by tests




@classmethod
def from_cosi_grb_data(cls,

Check warning on line 174 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L173-L174

Added lines #L173 - L174 were not covered by tests
name,
yaml_file,
cosi_dataset,
response_file,
arf_file = None,
l=None,
b=None,
ori_file=None,
deadtime=None,
poly_order=-1,
verbose=True,
restore_poly_fit=None):
"""
Create a TimeSeriesBuilderCOSI object from COSI GRB data.

:param name: Name of the time series
:param yaml_file: Path to yaml file
:param cosi_dataset: COSIGRBData object (.hdf5 containing signal + background)
:param response_file: Path to response file (either a .hdf5 file or .rsp file)
:param arf_file: Path to arf file (only required when response is OGIP compatible and has a different name than the .rsp file)
:param l: Galactic longitude of the source (optional if response_file is OGIP compatible)
:param b: Galactic latitude of the source (optional if response_file is OGIP compatible)
:param ori: Path to orientation file (optional if response is OGIP compatible)
:param poly_order: Polynomial order for background fitting (optional)
:param verbose: Verbosity flag
:param restore_poly_fit: File to restore background fit from
"""

analysis = BinnedData(yaml_file)
analysis.load_binned_data_from_hdf5(binned_data=cosi_dataset)
time_energy_counts = analysis.binned_data.project(['Time', 'Em']).contents.todense()
time_bins = (analysis.binned_data.axes['Time'].edges).value
energy_bins = (analysis.binned_data.axes['Em'].edges).value

Check warning on line 207 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L203-L207

Added lines #L203 - L207 were not covered by tests

cosi_data = COSIGRBData(time_energy_counts, time_bins, energy_bins, deadtime)

Check warning on line 209 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L209

Added line #L209 was not covered by tests

return cls(

Check warning on line 211 in cosipy/data_builder/Time_series_builder.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/Time_series_builder.py#L211

Added line #L211 was not covered by tests
name,
cosi_data,
response_file,
arf_file,
l,
b,
ori_file,
poly_order=poly_order,
verbose=verbose,
restore_poly_fit=restore_poly_fit
)


1 change: 1 addition & 0 deletions cosipy/data_builder/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .Time_series_builder import TimeSeriesBuilderCOSI

Check warning on line 1 in cosipy/data_builder/__init__.py

View check run for this annotation

Codecov / codecov/patch

cosipy/data_builder/__init__.py#L1

Added line #L1 was not covered by tests
Loading
0