Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouvin committed Jun 18, 2019
1 parent b8d3a39 commit 5d144a5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
90 changes: 44 additions & 46 deletions gammapy/spectrum/models.py
Expand Up @@ -227,14 +227,14 @@ def from_dict(cls, val):
return model

def plot(
self,
energy_range,
ax=None,
energy_unit="TeV",
flux_unit="cm-2 s-1 TeV-1",
energy_power=0,
n_points=100,
**kwargs
self,
energy_range,
ax=None,
energy_unit="TeV",
flux_unit="cm-2 s-1 TeV-1",
energy_power=0,
n_points=100,
**kwargs
):
"""Plot spectral model curve.
Expand Down Expand Up @@ -288,14 +288,14 @@ def plot(
return ax

def plot_error(
self,
energy_range,
ax=None,
energy_unit="TeV",
flux_unit="cm-2 s-1 TeV-1",
energy_power=0,
n_points=100,
**kwargs
self,
energy_range,
ax=None,
energy_unit="TeV",
flux_unit="cm-2 s-1 TeV-1",
energy_power=0,
n_points=100,
**kwargs
):
"""Plot spectral model error band.
Expand Down Expand Up @@ -420,7 +420,6 @@ def inverse(self, value, emin=0.1 * u.TeV, emax=100 * u.TeV):

energies = []
for val in np.atleast_1d(value):

def f(x):
# scale by 1e12 to achieve better precision
energy = u.Quantity(x, eunit, copy=False)
Expand Down Expand Up @@ -468,7 +467,7 @@ def __init__(self, model1, model2, operator):
self.model2 = model2
self.operator = operator
parameters = (
self.model1.parameters.parameters + self.model2.parameters.parameters
self.model1.parameters.parameters + self.model2.parameters.parameters
)
super().__init__(parameters)

Expand Down Expand Up @@ -624,7 +623,7 @@ def energy_flux(self, emin, emax):
return temp * np.log(emax / emin)
else:
prefactor = (
pars["amplitude"].quantity * pars["reference"].quantity ** 2 / val
pars["amplitude"].quantity * pars["reference"].quantity ** 2 / val
)
upper = (emax / pars["reference"].quantity) ** val
lower = (emin / pars["reference"].quantity) ** val
Expand Down Expand Up @@ -712,7 +711,7 @@ class PowerLaw2(SpectralModel):
__slots__ = ["index", "amplitude", "emin", "emax"]

def __init__(
self, amplitude="1e-12 cm-2 s-1", index=2, emin="0.1 TeV", emax="100 TeV"
self, amplitude="1e-12 cm-2 s-1", index=2, emin="0.1 TeV", emax="100 TeV"
):
self.amplitude = Parameter("amplitude", amplitude)
self.index = Parameter("index", index)
Expand Down Expand Up @@ -840,11 +839,11 @@ class ExponentialCutoffPowerLaw(SpectralModel):
__slots__ = ["index", "amplitude", "reference", "lambda_"]

def __init__(
self,
index=1.5,
amplitude="1e-12 cm-2 s-1 TeV-1",
reference="1 TeV",
lambda_="0.1 TeV-1",
self,
index=1.5,
amplitude="1e-12 cm-2 s-1 TeV-1",
reference="1 TeV",
lambda_="0.1 TeV-1",
):
self.index = Parameter("index", index)
self.amplitude = Parameter("amplitude", amplitude)
Expand Down Expand Up @@ -919,11 +918,11 @@ class ExponentialCutoffPowerLaw3FGL(SpectralModel):
__slots__ = ["index", "amplitude", "reference", "ecut"]

def __init__(
self,
index=1.5,
amplitude="1e-12 cm-2 s-1 TeV-1",
reference="1 TeV",
ecut="10 TeV",
self,
index=1.5,
amplitude="1e-12 cm-2 s-1 TeV-1",
reference="1 TeV",
ecut="10 TeV",
):
self.index = Parameter("index", index)
self.amplitude = Parameter("amplitude", amplitude)
Expand Down Expand Up @@ -982,12 +981,12 @@ class PLSuperExpCutoff3FGL(SpectralModel):
__slots__ = ["index_1", "index_2", "amplitude", "reference", "ecut"]

def __init__(
self,
index_1=1.5,
index_2=2,
amplitude="1e-12 cm-2 s-1 TeV-1",
reference="1 TeV",
ecut="10 TeV",
self,
index_1=1.5,
index_2=2,
amplitude="1e-12 cm-2 s-1 TeV-1",
reference="1 TeV",
ecut="10 TeV",
):
self.index_1 = Parameter("index_1", index_1)
self.index_2 = Parameter("index_2", index_2)
Expand Down Expand Up @@ -1056,7 +1055,7 @@ class LogParabola(SpectralModel):
__slots__ = ["amplitude", "reference", "alpha", "beta"]

def __init__(
self, amplitude="1e-12 cm-2 s-1 TeV-1", reference="10 TeV", alpha=2, beta=1
self, amplitude="1e-12 cm-2 s-1 TeV-1", reference="10 TeV", alpha=2, beta=1
):
self.amplitude = Parameter("amplitude", amplitude)
self.reference = Parameter("reference", reference, frozen=True)
Expand Down Expand Up @@ -1134,7 +1133,7 @@ class TableModel(SpectralModel):
__slots__ = ["energy", "values", "norm", "meta", "_evaluate"]

def __init__(
self, energy, values, norm=1, values_scale="log", interp_kwargs=None, meta=None
self, energy, values, norm=1, values_scale="log", interp_kwargs=None, meta=None
):
self.norm = Parameter("norm", norm, unit="")
self.energy = energy
Expand Down Expand Up @@ -1299,7 +1298,7 @@ class Absorption:
"""

def __init__(
self, energy_lo, energy_hi, param_lo, param_hi, data, interp_kwargs=None
self, energy_lo, energy_hi, param_lo, param_hi, data, interp_kwargs=None
):
self.data = data

Expand Down Expand Up @@ -1424,7 +1423,7 @@ class AbsorbedSpectralModel(SpectralModel):
__slots__ = ["spectral_model", "absorption", "parameter", "parameter_name"]

def __init__(
self, spectral_model, absorption, parameter, parameter_name="redshift"
self, spectral_model, absorption, parameter, parameter_name="redshift"
):
self.spectral_model = spectral_model
self.absorption = absorption
Expand Down Expand Up @@ -1623,7 +1622,7 @@ class Gaussian(SpectralModel):
"""

def __init__(
self, norm=1e-12 * u.Unit("cm-2 s-1"), mean=1 * u.TeV, sigma=2 * u.TeV
self, norm=1e-12 * u.Unit("cm-2 s-1"), mean=1 * u.TeV, sigma=2 * u.TeV
):
self.norm = Parameter("norm", norm)
self.mean = Parameter("mean", mean)
Expand All @@ -1634,9 +1633,9 @@ def __init__(
@staticmethod
def evaluate(energy, norm, mean, sigma):
return (
norm
/ (sigma * np.sqrt(2 * np.pi))
* np.exp((energy - mean) ** 2 / (2 * sigma ** 2))
norm
/ (sigma * np.sqrt(2 * np.pi))
* np.exp((energy - mean) ** 2 / (2 * sigma ** 2))
)

def integral(self, emin, emax, **kwargs):
Expand Down Expand Up @@ -1683,5 +1682,4 @@ def energy_flux(self, emin, emax):
a = pars["norm"].quantity * pars["sigma"].quantity / np.sqrt(2 * np.pi)
b = pars["norm"].quantity * pars["mean"].quantity / 2
return a * (np.exp(-u_min ** 2) - np.exp(-u_max ** 2)) + b * (
erf(u_max) - erf(u_min) )

erf(u_max) - erf(u_min))
6 changes: 3 additions & 3 deletions gammapy/spectrum/tests/test_models.py
Expand Up @@ -261,9 +261,9 @@ def test_models(spectrum):
# inverse for ConstantModel is irrelevant.
# inverse for Gaussian has a degeneracy
if not (
isinstance(model, ConstantModel)
or spectrum["name"] == "compound6"
or spectrum["name"] == "Gaussian"
isinstance(model, ConstantModel)
or spectrum["name"] == "compound6"
or spectrum["name"] == "Gaussian"
):
assert_quantity_allclose(model.inverse(value), 2 * u.TeV, rtol=0.01)

Expand Down

0 comments on commit 5d144a5

Please sign in to comment.