Skip to content

Commit

Permalink
adding string + flux points + spectral model
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Regeard <regeard@apc.in2p3.fr>
  • Loading branch information
MRegeard committed Oct 2, 2023
1 parent 645df1b commit 3dd9feb
Showing 1 changed file with 231 additions and 23 deletions.
254 changes: 231 additions & 23 deletions gammapy/catalog/fermi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings
import numpy as np
import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.table import Table
from astropy.wcs import FITSFixedWarning
from gammapy.estimators import FluxPoints
Expand Down Expand Up @@ -46,13 +47,6 @@ def compute_flux_points_ul(quantity, quantity_errp):
class SourceCatalogObjectFermiPCBase(SourceCatalogObject, abc.ABC):
"""Base class for Fermi-LAT Pulsar catalogs."""

def _auxiliary_filename(self):
return make_path(
"$GAMMAPY_DATA/catalogs/fermi/2PC_auxiliary/PSR"
+ self.name
+ "_2PC_data.fits"
)

def __str__(self):
return self.info()

Check warning on line 51 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L51

Added line #L51 was not covered by tests

Expand All @@ -75,7 +69,7 @@ def info(self, info="all"):
ss += self._info_spectral_fit()
ss += self._info_spectral_points()
if "lightcurve" in ops:
ss += self._info_lightcurve()
ss += self._info_phasogram()
return ss

Check warning on line 73 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L58-L73

Added lines #L58 - L73 were not covered by tests

def _info_basic(self):
Expand All @@ -85,37 +79,63 @@ def _info_basic(self):
return ss

Check warning on line 79 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L76-L79

Added lines #L76 - L79 were not covered by tests

def _info_more(self):
pass
return "\n"

Check warning on line 82 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L82

Added line #L82 was not covered by tests

def _info_pulsar(self):
pass
return "\n"

Check warning on line 85 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L85

Added line #L85 was not covered by tests

def _info_position(self):
d = self.data
source_pos = self.source_pos
ss = "\n*** Position info ***\n\n"
ss += "{:<20s} : {:.3f}\n".format("RA", d["RAJ2000"])
ss += "{:<20s} : {:.3f}\n".format("DEC", d["DEJ2000"])
ss += "{:<20s} : {:.3f}\n".format("GLON", d["GLON"])
ss += "{:<20s} : {:.3f}\n".format("GLAT", d["GLAT"])
ss += "{:<20s} : {:.3f}\n".format("RA", source_pos.ra)
ss += "{:<20s} : {:.3f}\n".format("DEC", source_pos.dec)
ss += "{:<20s} : {:.3f}\n".format("GLON", source_pos.galactic.l)
ss += "{:<20s} : {:.3f}\n".format("GLAT", source_pos.galactic.b)
return ss

Check warning on line 94 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L88-L94

Added lines #L88 - L94 were not covered by tests

def _info_spectral_fit(self):
pass
return "\n"

Check warning on line 97 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L97

Added line #L97 was not covered by tests

def _info_spectral_points(self):
pass
ss = "\n*** Spectral points ***\n\n"
lines = format_flux_points_table(self.flux_points_table).pformat(

Check warning on line 101 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L100-L101

Added lines #L100 - L101 were not covered by tests
max_width=-1, max_lines=-1
)
ss += "\n".join(lines)
ss += "\n"
return ss

Check warning on line 106 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L104-L106

Added lines #L104 - L106 were not covered by tests

def _info_lightcurve(self):
pass
def _info_phasogram(self):
return "\n"

Check warning on line 109 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L109

Added line #L109 was not covered by tests

def spatial_model(self):
d = self.data
ra = d["RAJ2000"]
dec = d["DEJ2000"]
ra = self.source_pos.ra
dec = self.source_pos.dec

Check warning on line 113 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L112-L113

Added lines #L112 - L113 were not covered by tests

model = PointSpatialModel(lon_0=ra, lat_0=dec, frame="icrs")
return model

Check warning on line 116 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L115-L116

Added lines #L115 - L116 were not covered by tests

def sky_model(self, name=None):
"""Sky model (`~gammapy.modeling.models.SkyModel`)."""
if name is None:
name = self.name

Check warning on line 121 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L120-L121

Added lines #L120 - L121 were not covered by tests

return SkyModel(

Check warning on line 123 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L123

Added line #L123 was not covered by tests
spatial_model=self.spatial_model(),
spectral_model=self.spectral_model(),
name=name,
)

@property
def flux_points(self):
"""Flux points (`~gammapy.estimators.FluxPoints`)."""

return FluxPoints.from_table(

Check warning on line 133 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L133

Added line #L133 was not covered by tests
table=self.flux_points_table,
reference_model=self.sky_model(),
format="gadf-sed",
)


class SourceCatalogObjectFermiBase(SourceCatalogObject, abc.ABC):
"""Base class for Fermi-LAT catalogs."""
Expand Down Expand Up @@ -1283,7 +1303,191 @@ def spatial_model(self):


class SourceCatalogObject2PC(SourceCatalogObjectFermiPCBase):
pass
@property
def _auxiliary_filename(self):
return make_path(

Check warning on line 1308 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1308

Added line #L1308 was not covered by tests
"$GAMMAPY_DATA/catalogs/fermi/2PC_auxiliary/PSR"
+ self.name
+ "_2PC_data.fits"
)

def _info_more(self):
d = self.data
ss = "\n*** Other info ***\n\n"
ss += "{:<20s} : {:s}\n".format("Binary", d["Binary"])
return ss

Check warning on line 1318 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1315-L1318

Added lines #L1315 - L1318 were not covered by tests

def _info_pulsar(self):
d = self.data
ss = "\n*** Pulsar info ***\n\n"
ss += "{:<20s} : {:.3f}\n".format("Period", d["Period"])
ss += "{:<20s} : {:.3e}\n".format("P_Dot", d["P_Dot"])
ss += "{:<20s} : {:.3e}\n".format("E_Dot", d["E_Dot"])
ss += "{:<20s} : {}\n".format("Type", d["Type"])
return ss

Check warning on line 1327 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1321-L1327

Added lines #L1321 - L1327 were not covered by tests

def _info_spectral_fit(self):
d = self.data_spectral
ss = "\n*** Spectral info ***\n\n"
ss += "{:<20s} : {}\n".format("On peak", d["On_Peak"])
ss += "{:<20s} : {:.0f}\n".format("TS DC", d["TS_DC"])
ss += "{:<20s} : {:.0f}\n".format("TS cutoff", d["TS_Cutoff"])
ss += "{:<20s} : {:.0f}\n".format("TS b free", d["TS_bfree"])

Check warning on line 1335 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1330-L1335

Added lines #L1330 - L1335 were not covered by tests

indentation = " " * 4
fmt_e = "{}{:<20s} : {:.3e} +- {:.3e}\n"
fmt_f = "{}{:<20s} : {:.3f} +- {:.3f}\n"

Check warning on line 1339 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1337-L1339

Added lines #L1337 - L1339 were not covered by tests

if not isinstance(d["PLEC1_Prefactor"], np.ma.core.MaskedConstant):

Check warning on line 1341 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1341

Added line #L1341 was not covered by tests

ss += "\n{}* PLSuperExpCutoff b = 1 *\n".format(indentation)
ss += fmt_e.format(

Check warning on line 1344 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1343-L1344

Added lines #L1343 - L1344 were not covered by tests
indentation, "Amplitude", d["PLEC1_Prefactor"], d["Unc_PLEC1_Prefactor"]
)
ss += fmt_f.format(

Check warning on line 1347 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1347

Added line #L1347 was not covered by tests
indentation,
"Index 1",
d["PLEC1_Photon_Index"],
d["Unc_PLEC1_Photon_Index"],
)
ss += "{}{:<20s} : {:.3f}\n".format(indentation, "Index 2", 1)
ss += "{}{:<20s} : {:.3f}\n".format(

Check warning on line 1354 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1353-L1354

Added lines #L1353 - L1354 were not covered by tests
indentation, "Reference", d["PLEC1_Scale"]
)
ss += fmt_f.format(

Check warning on line 1357 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1357

Added line #L1357 was not covered by tests
indentation, "Ecut", d["PLEC1_Cutoff"], d["Unc_PLEC1_Cutoff"]
)

if not isinstance(d["PLEC_Prefactor"], np.ma.core.MaskedConstant):

Check warning on line 1361 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1361

Added line #L1361 was not covered by tests

ss += "\n{}* PLSuperExpCutoff b free *\n".format(indentation)
ss += fmt_e.format(

Check warning on line 1364 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1363-L1364

Added lines #L1363 - L1364 were not covered by tests
indentation, "Amplitude", d["PLEC_Prefactor"], d["Unc_PLEC_Prefactor"]
)
ss += fmt_f.format(

Check warning on line 1367 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1367

Added line #L1367 was not covered by tests
indentation,
"Index 1",
d["PLEC_Photon_Index"],
d["Unc_PLEC_Photon_Index"],
)
ss += fmt_f.format(

Check warning on line 1373 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1373

Added line #L1373 was not covered by tests
indentation,
"Index 2",
d["PLEC_Exponential_Index"],
d["Unc_PLEC_Exponential_Index"],
)
ss += "{}{:<20s} : {:.3f}\n".format(

Check warning on line 1379 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1379

Added line #L1379 was not covered by tests
indentation, "Reference", d["PLEC_Scale"]
)
ss += fmt_f.format(

Check warning on line 1382 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1382

Added line #L1382 was not covered by tests
indentation, "Ecut", d["PLEC_Cutoff"], d["Unc_PLEC_Cutoff"]
)

if not isinstance(d["PL_Prefactor"], np.ma.core.MaskedConstant):

Check warning on line 1386 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1386

Added line #L1386 was not covered by tests

ss += "\n{}* PowerLaw *\n".format(indentation)
ss += fmt_e.format(

Check warning on line 1389 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1388-L1389

Added lines #L1388 - L1389 were not covered by tests
indentation, "Amplitude", d["PL_Prefactor"], d["Unc_PL_Prefactor"]
)
ss += fmt_f.format(

Check warning on line 1392 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1392

Added line #L1392 was not covered by tests
indentation, "Index", d["PL_Photon_Index"], d["Unc_PL_Photon_Index"]
)
ss += "{}{:<20s} : {:.3f}\n".format(indentation, "Reference", d["PL_Scale"])

Check warning on line 1395 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1395

Added line #L1395 was not covered by tests

return ss

Check warning on line 1397 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1397

Added line #L1397 was not covered by tests

def _info_phasogram(self):
d = self.data
ss = "\n*** Phasogram info ***\n\n"
ss += "{:<20s} : {:d}\n".format("Number of peaks", d["Num_Peaks"])
ss += "{:<20s} : {:.3f}\n".format("Peak separation", d["Peak_Sep"])
return ss

Check warning on line 1404 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1400-L1404

Added lines #L1400 - L1404 were not covered by tests

@property
def source_pos(self):
d = self.data
return SkyCoord(d["RAJ2000"], d["DEJ2000"], unit="deg", frame="icrs")

Check warning on line 1409 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1408-L1409

Added lines #L1408 - L1409 were not covered by tests

def spectral_model(self):
d = self.data_spectral
if d["TS_Cutoff"] < 9:
tag = "PowerLawSpectralModel"
pars = {

Check warning on line 1415 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1412-L1415

Added lines #L1412 - L1415 were not covered by tests
"reference": d["PL_Scale"],
"amplitude": d["PL_Prefactor"],
"index": d["PL_Photon_Index"],
}
errs = {

Check warning on line 1420 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1420

Added line #L1420 was not covered by tests
"amplitdue": d["Unc_PL_Prefactor"],
"index": d["Unc_PL_Photon_Index"],
}
elif d["TS_bfree"] >= 9:
tag = "SuperExpCutoffPowerLaw3FGLSpectralModel"
pars = {

Check warning on line 1426 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1424-L1426

Added lines #L1424 - L1426 were not covered by tests
"index_1": d["PLEC_Photon_Index"],
"index_2": d["PLEC_Exponential_Index"],
"amplitude": d["PLEC_Prefactor"],
"reference": d["PLEC_Scale"],
"ecut": d["PLEC_Cutoff"],
}
errs = {

Check warning on line 1433 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1433

Added line #L1433 was not covered by tests
"index_1": d["Unc_PLEC_Photon_Index"],
"index_2": d["Unc_PLEC_Exponential_Index"],
"amplitude": d["Unc_PLEC_Prefactor"],
"ecut": d["Unc_PLEC_Cutoff"],
}
elif d["TS_bfree"] < 9:
tag = "SuperExpCutoffPowerLaw3FGLSpectralModel"
pars = {

Check warning on line 1441 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1439-L1441

Added lines #L1439 - L1441 were not covered by tests
"index_1": d["PLEC1_Photon_Index"],
"index_2": 1,
"amplitude": d["PLEC1_Prefactor"],
"reference": d["PLEC1_Scale"],
"ecut": d["PLEC1_Cutoff"],
}
errs = {

Check warning on line 1448 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1448

Added line #L1448 was not covered by tests
"index_1": d["Unc_PLEC1_Photon_Index"],
"amplitude": d["Unc_PLEC1_Prefactor"],
"ecut": d["Unc_PLEC1_Cutoff"],
}
else:
raise ValueError("No spectral model found !")

Check warning on line 1454 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1454

Added line #L1454 was not covered by tests

model = Model.create(tag, "spectral", **pars)

Check warning on line 1456 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1456

Added line #L1456 was not covered by tests

for name, value in errs.items():
model.parameters[name].error = value

Check warning on line 1459 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1458-L1459

Added lines #L1458 - L1459 were not covered by tests

return model

Check warning on line 1461 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1461

Added line #L1461 was not covered by tests

@property
def flux_points_table(self):
"""Flux points (`~astropy.table.Table`)."""
fp_data = Table.read(self._auxiliary_filename, hdu="PULSAR_SED")
table = Table()

Check warning on line 1467 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1466-L1467

Added lines #L1466 - L1467 were not covered by tests

table["e_min"] = fp_data["Energy_Min"]
table["e_max"] = fp_data["Energy_Max"]
table["e_ref"] = fp_data["Center_Energy"]

Check warning on line 1471 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1469-L1471

Added lines #L1469 - L1471 were not covered by tests

table["flux"] = fp_data["PhotonFlux"]
table["flux_err"] = fp_data["Unc_PhotonFlux"]

Check warning on line 1474 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1473-L1474

Added lines #L1473 - L1474 were not covered by tests

table["e2dnde"] = fp_data["EnergyFlux"]
table["e2dnde_err"] = fp_data["Unc_EnergyFlux"]

Check warning on line 1477 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1476-L1477

Added lines #L1476 - L1477 were not covered by tests

is_ul = np.where(table["e2dnde_err"] == 0, True, False)
table["is_ul"] = is_ul

Check warning on line 1480 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1479-L1480

Added lines #L1479 - L1480 were not covered by tests

table["flux_ul"] = np.nan * table["flux_err"].unit
flux_ul = compute_flux_points_ul(table["flux"], table["flux_err"])
table["flux_ul"][is_ul] = flux_ul[is_ul]

Check warning on line 1484 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1482-L1484

Added lines #L1482 - L1484 were not covered by tests

table["e2dnde_ul"] = np.nan * table["e2dnde"].unit
e2dnde_ul = compute_flux_points_ul(table["e2dnde"], table["e2dnde_err"])
table["e2dnde_ul"][is_ul] = e2dnde_ul[is_ul]

Check warning on line 1488 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1486-L1488

Added lines #L1486 - L1488 were not covered by tests

return table

Check warning on line 1490 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1490

Added line #L1490 was not covered by tests


class SourceCatalog3FGL(SourceCatalog):
Expand Down Expand Up @@ -1465,6 +1669,8 @@ class SourceCatalog2PC(SourceCatalog):
- https://fermi.gsfc.nasa.gov/ssc/data/access/lat/2nd_PSR_catalog/
One source is represented by `~gammapy.catalog.SourceCatalogObject2PC`.
TODO : Fix the UnitsWarning here ...
"""

tag = "2PC"
Expand All @@ -1487,5 +1693,7 @@ def __init__(self, filename="$GAMMAPY_DATA/catalogs/fermi/2PC_catalog_v04.fits")
source_name_key = "PSR_Name"
super().__init__(table=table_psr, source_name_key=source_name_key)

Check warning on line 1694 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1693-L1694

Added lines #L1693 - L1694 were not covered by tests

self.source_object_class._source_name_key = source_name_key

Check warning on line 1696 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1696

Added line #L1696 was not covered by tests

self.spectral_table = table_spectral
self.off_peak_table = table_off_peak

Check warning on line 1699 in gammapy/catalog/fermi.py

View check run for this annotation

Codecov / codecov/patch

gammapy/catalog/fermi.py#L1698-L1699

Added lines #L1698 - L1699 were not covered by tests

0 comments on commit 3dd9feb

Please sign in to comment.