Skip to content

Commit 6250e66

Browse files
pre-commit-ci[bot]hombit
authored andcommitted
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 2bd122b commit 6250e66

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

light-curve/light_curve/light_curve_py/features/rainbow/_base.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from abc import abstractmethod
2-
from copy import deepcopy
32
from dataclasses import dataclass
43
from typing import Dict, List, Tuple
54

@@ -155,7 +154,7 @@ def _parameter_scalings(self) -> Dict[str, str]:
155154
if self.with_baseline:
156155
for band_name in self.bands.names:
157156
baseline_name = self.p.baseline_parameter_name(band_name)
158-
rules[baseline_name] = 'baseline'
157+
rules[baseline_name] = "baseline"
159158

160159
return rules
161160

@@ -171,7 +170,7 @@ def _parameter_scale(self, name: str, t_scaler: Scaler, m_scaler: MultiBandScale
171170

172171
def _unscale_parameters(self, params, t_scaler: Scaler, m_scaler: MultiBandScaler) -> None:
173172
"""Unscale parameters from internal units, in-place."""
174-
for name,scaling in self._parameter_scalings().items():
173+
for name, scaling in self._parameter_scalings().items():
175174
if scaling == "time":
176175
params[self.p[name]] = t_scaler.undo_shift_scale(params[self.p[name]])
177176

@@ -305,12 +304,17 @@ def _eval_and_fill(self, *, t, m, sigma, band, fill_value):
305304
return super()._eval_and_fill(t=t, m=m, sigma=sigma, band=band, fill_value=fill_value)
306305

307306
def _eval_and_get_errors(
308-
self, *, t, m, sigma, band,
309-
upper_mask=None,
310-
get_initial=False,
311-
return_covariance=False,
312-
print_level=None,
313-
debug=False
307+
self,
308+
*,
309+
t,
310+
m,
311+
sigma,
312+
band,
313+
upper_mask=None,
314+
get_initial=False,
315+
return_covariance=False,
316+
print_level=None,
317+
debug=False,
314318
):
315319
# Initialize data scalers
316320
t_scaler = Scaler.from_time(t)
@@ -352,7 +356,7 @@ def _eval_and_get_errors(
352356
# TODO: expose these parameters through function arguments
353357
if print_level is not None:
354358
minuit.print_level = print_level
355-
minuit.strategy = 0 # We will need to manually call .hesse() on convergence anyway
359+
minuit.strategy = 0 # We will need to manually call .hesse() on convergence anyway
356360

357361
# Supposedly it is not the same as just setting iterate=10?..
358362
for i in range(10):
@@ -372,10 +376,17 @@ def _eval_and_get_errors(
372376
# Expose everything we have to outside, unscaled, for easier debugging
373377
self.minuit = minuit
374378
self.mparams = {
375-
't':t, 'band_idx':band_idx, 'wave_cm':wave_cm, 'm':m, 'sigma':sigma,
376-
'limits':limits, 'upper_mask':upper_mask,
377-
'initial_guesses':initial_guesses, 'values':minuit.values, 'errors':minuit.errors,
378-
'covariance':minuit.covariance,
379+
"t": t,
380+
"band_idx": band_idx,
381+
"wave_cm": wave_cm,
382+
"m": m,
383+
"sigma": sigma,
384+
"limits": limits,
385+
"upper_mask": upper_mask,
386+
"initial_guesses": initial_guesses,
387+
"values": minuit.values,
388+
"errors": minuit.errors,
389+
"covariance": minuit.covariance,
379390
}
380391

381392
if not minuit.valid and self.fail_on_divergence and not get_initial:

light-curve/light_curve/light_curve_py/features/rainbow/_parameters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def baseline_parameter_name(band: str) -> str:
1414

1515
def baseline_band_name(name: str) -> str:
1616
if name.startswith("baseline_"):
17-
return name[len("baseline_"):]
17+
return name[len("baseline_") :]
1818

1919
return None
2020

light-curve/light_curve/light_curve_py/features/rainbow/bolometric.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def limits(t, m, sigma, band):
9999
limits = {}
100100
limits["reference_time"] = (np.min(t) - 10 * t_amplitude, np.max(t) + 10 * t_amplitude)
101101
limits["amplitude"] = (0.0, 20 * m_amplitude)
102-
limits["rise_time"] = (0.1*mean_dt, 10*t_amplitude)
102+
limits["rise_time"] = (0.1 * mean_dt, 10 * t_amplitude)
103103

104104
return limits
105105

@@ -141,7 +141,7 @@ def value(t, t0, amplitude, rise_time, fall_time):
141141
def initial_guesses(t, m, sigma, band):
142142
A = np.ptp(m)
143143

144-
mc = m - np.min(m) # To avoid crashing on all-negative data
144+
mc = m - np.min(m) # To avoid crashing on all-negative data
145145

146146
# Naive peak position from the highest point
147147
t0 = t[np.argmax(m)]
@@ -176,8 +176,8 @@ def limits(t, m, sigma, band):
176176
limits = {}
177177
limits["reference_time"] = (np.min(t) - 10 * t_amplitude, np.max(t) + 10 * t_amplitude)
178178
limits["amplitude"] = (0.0, 20 * m_amplitude)
179-
limits["rise_time"] = (0.1*mean_dt, 10 * t_amplitude)
180-
limits["fall_time"] = (0.1*mean_dt, 10 * t_amplitude)
179+
limits["rise_time"] = (0.1 * mean_dt, 10 * t_amplitude)
180+
limits["fall_time"] = (0.1 * mean_dt, 10 * t_amplitude)
181181

182182
return limits
183183

light-curve/light_curve/light_curve_py/minuit_ml.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,45 @@
33
from typing import Callable, Dict, Tuple
44

55
import numpy as np
6-
from scipy.stats import norm
76
from scipy.special import erf
87

9-
import numba as nb
108

119
def logpdf(x, mu, sigma):
1210
# We do not need second term as it does not depend on parameters
13-
return -(((x - mu)/sigma)**2)/2 # - np.log(np.sqrt(2*np.pi) * sigma)
11+
return -(((x - mu) / sigma) ** 2) / 2 # - np.log(np.sqrt(2*np.pi) * sigma)
12+
1413

1514
def barrier(x):
16-
res = np.where(x>0, 1/x, np.inf) # FIXME: naive barrier function
15+
res = np.where(x > 0, 1 / x, np.inf) # FIXME: naive barrier function
1716

1817
return res
1918

19+
2020
def logcdf(x):
2121
# TODO: faster (maybe not so accurate, as we do not need it) implementation
2222
# return norm.logcdf(x)
2323

2424
result = np.zeros(len(x))
2525

2626
idx = x < -5
27-
result[idx] = -x[idx]**2/2 - 1/x[idx]**2 - 0.9189385336 - np.log(-x[idx])
28-
result[~idx] = np.log(0.5) + np.log1p(erf(x[~idx]/np.sqrt(2)))
27+
result[idx] = -x[idx] ** 2 / 2 - 1 / x[idx] ** 2 - 0.9189385336 - np.log(-x[idx])
28+
result[~idx] = np.log(0.5) + np.log1p(erf(x[~idx] / np.sqrt(2)))
2929

3030
return result
3131

32+
3233
try:
3334
from iminuit import Minuit
3435
except ImportError:
3536
MaximumLikelihood = None
3637
else:
38+
3739
class MaximumLikelihood:
3840
errordef = Minuit.LIKELIHOOD
3941

40-
def __init__(self, model: Callable, parameters: Dict[str, Tuple[float, float]], upper_mask=None, *, x, y, yerror):
42+
def __init__(
43+
self, model: Callable, parameters: Dict[str, Tuple[float, float]], upper_mask=None, *, x, y, yerror
44+
):
4145
self.model = model
4246
self.x = x
4347
self.y = y
@@ -59,22 +63,23 @@ def __call__(self, *par):
5963
ym = self.model(self.x, *par)
6064

6165
if self.upper_mask is None:
62-
result = -np.sum(logpdf(self.y, ym, self.yerror))
66+
result = -np.sum(logpdf(self.y, ym, self.yerror))
6367
else:
6468
# Measurements
65-
result = -np.sum(logpdf(self.y[~self.upper_mask], ym[~self.upper_mask], self.yerror[~self.upper_mask]))
69+
result = -np.sum(logpdf(self.y[~self.upper_mask], ym[~self.upper_mask], self.yerror[~self.upper_mask]))
6670
# Upper limits, Tobit model
6771
# https://stats.stackexchange.com/questions/49443/how-to-model-this-odd-shaped-distribution-almost-a-reverse-j
6872
result += -np.sum(
69-
logcdf((self.y[self.upper_mask] - ym[self.upper_mask])/self.yerror[self.upper_mask])
73+
logcdf((self.y[self.upper_mask] - ym[self.upper_mask]) / self.yerror[self.upper_mask])
7074
)
7175

7276
# Barriers around parameter ranges
7377
# Scale is selected so that for the most of the range it is much smaller
7478
# than 0.5 which corresponds to 1-sigma errors
75-
result += 0.0001 * np.sum(barrier((par - self.limits0)/self.limits_scale))
76-
result += 0.0001 * np.sum(barrier((self.limits1 - par)/self.limits_scale))
79+
result += 0.0001 * np.sum(barrier((par - self.limits0) / self.limits_scale))
80+
result += 0.0001 * np.sum(barrier((self.limits1 - par) / self.limits_scale))
7781

7882
return result
7983

84+
8085
__all__ = ["MaximumLikelihood"]

0 commit comments

Comments
 (0)