Skip to content

Commit

Permalink
Paint code black
Browse files Browse the repository at this point in the history
  • Loading branch information
adonath committed Nov 19, 2021
1 parent c2abc6b commit d6f8387
Show file tree
Hide file tree
Showing 130 changed files with 1,350 additions and 924 deletions.
4 changes: 3 additions & 1 deletion gammapy/analysis/config.py
Expand Up @@ -171,7 +171,9 @@ class SafeMaskConfig(GammapyBaseConfig):

class EnergyAxesConfig(GammapyBaseConfig):
energy: EnergyAxisConfig = EnergyAxisConfig(min="1 TeV", max="10 TeV", nbins=5)
energy_true: EnergyAxisConfig = EnergyAxisConfig(min="0.5 TeV", max="20 TeV", nbins=16)
energy_true: EnergyAxisConfig = EnergyAxisConfig(
min="0.5 TeV", max="20 TeV", nbins=16
)


class SelectionConfig(GammapyBaseConfig):
Expand Down
75 changes: 45 additions & 30 deletions gammapy/analysis/core.py
Expand Up @@ -7,7 +7,11 @@
from gammapy.analysis.config import AnalysisConfig
from gammapy.data import DataStore
from gammapy.datasets import Datasets, FluxPointsDataset, MapDataset, SpectrumDataset
from gammapy.estimators import FluxPointsEstimator, ExcessMapEstimator, LightCurveEstimator
from gammapy.estimators import (
FluxPointsEstimator,
ExcessMapEstimator,
LightCurveEstimator,
)
from gammapy.makers import (
FoVBackgroundMaker,
MapDatasetMaker,
Expand Down Expand Up @@ -83,14 +87,16 @@ def _make_obs_table_selection(self):
obs_settings = self.config.observations

# Reject configs with list of obs_ids and obs_file set at the same time
if (len(obs_settings.obs_ids) and obs_settings.obs_file is not None):
if len(obs_settings.obs_ids) and obs_settings.obs_file is not None:
raise ValueError(
"Values for both parameters obs_ids and obs_file are not accepted."
)

# First select input list of observations from obs_table
if len(obs_settings.obs_ids):
selected_obs_table = self.datastore.obs_table.select_obs_id(obs_settings.obs_ids)
selected_obs_table = self.datastore.obs_table.select_obs_id(
obs_settings.obs_ids
)
elif obs_settings.obs_file is not None:
path = make_path(obs_settings.obs_file)
ids = list(Table.read(path, format="ascii", data_start=0).columns[0])
Expand Down Expand Up @@ -120,8 +126,9 @@ def get_observations(self):
log.info("Fetching observations.")
ids = self._make_obs_table_selection()

self.observations = self.datastore.get_observations(ids, skip_missing=True,
required_irf=observations_settings.required_irf)
self.observations = self.datastore.get_observations(
ids, skip_missing=True, required_irf=observations_settings.required_irf
)

if observations_settings.obs_time.start is not None:
start = observations_settings.obs_time.start
Expand Down Expand Up @@ -249,7 +256,7 @@ def get_excess_map(self):
excess_map_estimator = ExcessMapEstimator(
correlation_radius=excess_settings.correlation_radius,
energy_edges=energy_edges,
**excess_settings.parameters
**excess_settings.parameters,
)
self.excess_map = excess_map_estimator.run(self.datasets[0])

Expand All @@ -259,12 +266,21 @@ def get_light_curve(self):
log.info("Computing light curve.")
energy_edges = self._make_energy_axis(lc_settings.energy_edges).edges

if lc_settings.time_intervals.start is None or lc_settings.time_intervals.stop is None:
log.info("Time intervals not defined. Extract light curve on datasets GTIs.")
if (
lc_settings.time_intervals.start is None
or lc_settings.time_intervals.stop is None
):
log.info(
"Time intervals not defined. Extract light curve on datasets GTIs."
)
time_intervals = None
else:
time_intervals = [(t1, t2) for t1, t2 in
zip(lc_settings.time_intervals.start, lc_settings.time_intervals.stop)]
time_intervals = [
(t1, t2)
for t1, t2 in zip(
lc_settings.time_intervals.start, lc_settings.time_intervals.stop
)
]

light_curve_estimator = LightCurveEstimator(
time_intervals=time_intervals,
Expand All @@ -275,7 +291,11 @@ def get_light_curve(self):
)
lc = light_curve_estimator.run(datasets=self.datasets)
self.light_curve = lc
log.info("\n{}".format(self.light_curve.to_table(format="lightcurve", sed_type="flux")))
log.info(
"\n{}".format(
self.light_curve.to_table(format="lightcurve", sed_type="flux")
)
)

def update_config(self, config):
self.config = self.config.update(config=config)
Expand All @@ -294,7 +314,9 @@ def _create_wcs_geometry(wcs_geom_settings, axes):
if skydir_settings.frame in ["icrs", "galactic"]:
geom_params["frame"] = skydir_settings.frame
else:
raise ValueError(f"Incorrect skydir frame: expect 'icrs' or 'galactic'. Got {skydir_settings.frame}")
raise ValueError(
f"Incorrect skydir frame: expect 'icrs' or 'galactic'. Got {skydir_settings.frame}"
)

geom_params["axes"] = axes
geom_params["binsz"] = wcs_geom_settings.binsize
Expand Down Expand Up @@ -325,7 +347,9 @@ def _create_geometry(self):
elif datasets_settings.type == "1d":
geom = self._create_region_geometry(datasets_settings.on_region, axes)
else:
raise ValueError(f"Incorrect dataset type. Expect '1d' or '3d'. Got {datasets_settings.type}.")
raise ValueError(
f"Incorrect dataset type. Expect '1d' or '3d'. Got {datasets_settings.type}."
)
return geom

def _create_reference_dataset(self, name=None):
Expand All @@ -342,7 +366,7 @@ def _create_reference_dataset(self, name=None):
if geom_settings.wcs.binsize_irf is not None:
geom_irf["binsz_irf"] = geom_settings.wcs.binsize_irf.to("deg").value

if self.config.datasets.type == '1d':
if self.config.datasets.type == "1d":
return SpectrumDataset.create(geom, name=name, **geom_irf)
else:
return MapDataset.create(geom, name=name, **geom_irf)
Expand All @@ -357,7 +381,9 @@ def _create_dataset_maker(self):
elif datasets_settings.type == "1d":
maker_config = {}
if datasets_settings.containment_correction:
maker_config["containment_correction"] = datasets_settings.containment_correction
maker_config[
"containment_correction"
] = datasets_settings.containment_correction

maker_config["selection"] = ["counts", "exposure", "edisp"]

Expand All @@ -371,9 +397,7 @@ def _create_safe_mask_maker(self):

safe_mask_selection = self.config.datasets.safe_mask.methods
safe_mask_settings = self.config.datasets.safe_mask.parameters
return SafeMaskMaker(
methods=safe_mask_selection, **safe_mask_settings
)
return SafeMaskMaker(methods=safe_mask_selection, **safe_mask_settings)

def _create_background_maker(self):
"""Create the Background maker."""
Expand Down Expand Up @@ -425,10 +449,7 @@ def _map_making(self):
log.info("Start the data reduction loop.")

if datasets_settings.stack:
for obs in progress_bar(
self.observations,
desc="Observations"
):
for obs in progress_bar(self.observations, desc="Observations"):
log.debug(f"Processing observation {obs.obs_id}")
cutout = stacked.cutout(obs.pointing_radec, width=2 * offset_max)
dataset = maker.run(cutout, obs)
Expand All @@ -445,10 +466,7 @@ def _map_making(self):
else:
datasets = []

for obs in progress_bar(
self.observations,
desc="Observations"
):
for obs in progress_bar(self.observations, desc="Observations"):
log.debug(f"Processing observation {obs.obs_id}")
cutout = stacked.cutout(obs.pointing_radec, width=2 * offset_max)
dataset = maker.run(cutout, obs)
Expand All @@ -470,10 +488,7 @@ def _spectrum_extraction(self):
reference = self._create_reference_dataset()

datasets = []
for obs in progress_bar(
self.observations,
desc="Observations"
):
for obs in progress_bar(self.observations, desc="Observations"):
log.debug(f"Processing observation {obs.obs_id}")
dataset = dataset_maker.run(reference.copy(), obs)
if bkg_maker is not None:
Expand Down
8 changes: 6 additions & 2 deletions gammapy/analysis/tests/test_config.py
Expand Up @@ -119,7 +119,9 @@ def test_time_range_iso():
"""
config = AnalysisConfig.from_yaml(cfg)

assert_allclose(config.observations.obs_time.start.mjd, [53343.92, 53343.935, 53343.954])
assert_allclose(
config.observations.obs_time.start.mjd, [53343.92, 53343.935, 53343.954]
)


def test_time_range_jyear():
Expand All @@ -134,4 +136,6 @@ def test_time_range_jyear():
"""
config = AnalysisConfig.from_yaml(cfg)

assert_allclose(config.observations.obs_time.start.mjd, [53343.92, 53343.935, 53343.954])
assert_allclose(
config.observations.obs_time.start.mjd, [53343.92, 53343.935, 53343.954]
)
8 changes: 5 additions & 3 deletions gammapy/catalog/core.py
Expand Up @@ -25,7 +25,7 @@ def format_flux_points_table(table):
if column.startswith(("dnde", "eflux", "flux", "e2dnde", "ref")):
table[column].format = ".3e"
elif column.startswith(
("e_min", "e_max", "e_ref", "sqrt_ts", "norm", "ts", "stat")
("e_min", "e_max", "e_ref", "sqrt_ts", "norm", "ts", "stat")
):
table[column].format = ".3f"

Expand Down Expand Up @@ -216,7 +216,9 @@ def _make_source_object(self, index):
data["time_axis"] = TimeMapAxis.from_table(hist_table, format="fermi-fgl")

if hist2_table:
data["time_axis_2"] = TimeMapAxis.from_table(hist2_table, format="fermi-fgl")
data["time_axis_2"] = TimeMapAxis.from_table(
hist2_table, format="fermi-fgl"
)

if "Extended_Source_Name" in data:
name_extended = data["Extended_Source_Name"].strip()
Expand Down Expand Up @@ -245,7 +247,7 @@ def positions(self):
return _skycoord_from_table(self.table)

def to_models(self, **kwargs):
""" Create Models object from catalogue"""
"""Create Models object from catalogue"""
return Models([_.sky_model(**kwargs) for _ in self])


Expand Down
10 changes: 6 additions & 4 deletions gammapy/catalog/fermi.py
Expand Up @@ -49,7 +49,7 @@ class SourceCatalogObjectFermiBase(SourceCatalogObject, abc.ABC):
"sed_type_init": "flux",
"n_sigma": 1,
"sqrt_ts_threshold_ul": 1,
"n_sigma_ul": 2
"n_sigma_ul": 2,
}

def __str__(self):
Expand Down Expand Up @@ -155,7 +155,9 @@ def _info_spectral_fit(self):

def _info_spectral_points(self):
ss = "\n*** Spectral points ***\n\n"
lines = format_flux_points_table(self.flux_points_table).pformat(max_width=-1, max_lines=-1)
lines = format_flux_points_table(self.flux_points_table).pformat(
max_width=-1, max_lines=-1
)
ss += "\n".join(lines)
return ss

Expand Down Expand Up @@ -522,7 +524,7 @@ def lightcurve(self, interval="1-year"):
maps=maps,
sed_type="flux",
reference_model=self.sky_model(),
meta=self.flux_points.meta.copy()
meta=self.flux_points.meta.copy(),
)


Expand Down Expand Up @@ -818,7 +820,7 @@ def lightcurve(self):
maps=maps,
sed_type="flux",
reference_model=self.sky_model(),
meta=self.flux_points_meta.copy()
meta=self.flux_points_meta.copy(),
)


Expand Down
2 changes: 1 addition & 1 deletion gammapy/catalog/gammacat.py
Expand Up @@ -34,7 +34,7 @@ def flux_points(self):
return FluxPoints.from_table(
table=self.flux_points_table,
reference_model=self.sky_model(),
format="gadf-sed"
format="gadf-sed",
)

def info(self, info="all"):
Expand Down
11 changes: 5 additions & 6 deletions gammapy/catalog/hess.py
Expand Up @@ -102,10 +102,7 @@ def __str__(self):
@property
def flux_points(self):
"""Flux points (`~gammapy.estimators.FluxPoints`)."""
reference_model = SkyModel(
spectral_model=self.spectral_model(),
name=self.name
)
reference_model = SkyModel(spectral_model=self.spectral_model(), name=self.name)
return FluxPoints.from_table(
self.flux_points_table,
reference_model=reference_model,
Expand Down Expand Up @@ -397,7 +394,9 @@ def _info_flux_points(self):
ss = "\n*** Flux points info ***\n\n"
ss += "Number of flux points: {}\n".format(d["N_Flux_Points"])
ss += "Flux points table: \n\n"
lines = format_flux_points_table(self.flux_points_table).pformat(max_width=-1, max_lines=-1)
lines = format_flux_points_table(self.flux_points_table).pformat(
max_width=-1, max_lines=-1
)
ss += "\n".join(lines)
return ss + "\n"

Expand Down Expand Up @@ -870,7 +869,7 @@ def gaussian_component(self, row_idx):
return SourceCatalogObjectHGPSComponent(data=data)

def to_models(self, which="best", components_status="independent"):
""" Create Models object from catalogue
"""Create Models object from catalogue
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions gammapy/catalog/tests/test_fermi.py
Expand Up @@ -259,7 +259,7 @@ def test_lightcurve_dr1(self):
"flux_ul",
"ts",
"sqrt_ts",
"is_ul"
"is_ul",
]
axis = lc.geom.axes["time"]
expected = Time(54682.6552835, format="mjd", scale="utc")
Expand Down Expand Up @@ -422,7 +422,7 @@ def test_lightcurve(self):
"flux_errp",
"flux_errn",
"flux_ul",
"is_ul"
"is_ul",
]

expected = Time(54680.02313657408, format="mjd", scale="utc")
Expand Down
2 changes: 1 addition & 1 deletion gammapy/catalog/tests/test_hess.py
Expand Up @@ -253,7 +253,7 @@ def test_sky_model_shell(cat):

@staticmethod
def test_flux_points_meta(cat):
source = cat['HESS J1843-033']
source = cat["HESS J1843-033"]
fp = source.flux_points

assert fp.sqrt_ts_threshold_ul == 1
Expand Down
4 changes: 3 additions & 1 deletion gammapy/data/data_store.py
Expand Up @@ -266,7 +266,9 @@ def get_observations(self, obs_id=None, skip_missing=False, required_irf="all"):

if not set(required_irf).issubset(available_irf):
difference = set(required_irf).difference(available_irf)
raise ValueError(f"{difference} is not a valid irf key. Choose from: {available_irf}")
raise ValueError(
f"{difference} is not a valid irf key. Choose from: {available_irf}"
)

if obs_id is None:
obs_id = self.obs_table["OBS_ID"].data
Expand Down
8 changes: 6 additions & 2 deletions gammapy/data/event_list.py
Expand Up @@ -494,10 +494,14 @@ def plot_energy_offset(self, ax=None, center=None, **kwargs):
energy_axis = self._default_plot_energy_axis

offset = center.separation(self.radec)
offset_axis = MapAxis.from_bounds(0 * u.deg, offset.max(), nbin=30, name="offset")
offset_axis = MapAxis.from_bounds(
0 * u.deg, offset.max(), nbin=30, name="offset"
)

counts = np.histogram2d(
x=self.energy, y=offset, bins=(energy_axis.edges, offset_axis.edges),
x=self.energy,
y=offset,
bins=(energy_axis.edges, offset_axis.edges),
)[0]

kwargs.setdefault("norm", LogNorm())
Expand Down
4 changes: 2 additions & 2 deletions gammapy/data/hdu_index_table.py
Expand Up @@ -30,7 +30,7 @@ class HDUIndexTable(Table):
"psf_king",
"bkg_2d",
"bkg_3d",
"rad_max_2d"
"rad_max_2d",
]
"""Valid values for `HDU_CLASS`."""

Expand All @@ -49,7 +49,7 @@ def read(cls, filename, **kwargs):

# TODO: this is a workaround for the joint-crab validation with astropy>4.0.
# TODO: Remove when handling of empty columns is clarified
table["FILE_DIR"].fill_value=''
table["FILE_DIR"].fill_value = ""

return table.filled()

Expand Down

0 comments on commit d6f8387

Please sign in to comment.