Skip to content

Commit 4db52ff

Browse files
Add an option to parametrize the shift when computing ephemerides (#148)
* Add an option to parametrize the shift when computing ephemerides * PEP8 * change the arguments for sso.period.compute_residuals * Update the way we call compute_residuals * PEP8 * Fix typo * Assertion in case API does not answer correctly
1 parent de24576 commit 4db52ff

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

fink_utils/sso/periods.py

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,49 @@ def extract_physical_parameters(pdf, flavor):
9696
return outdic
9797

9898

99-
def compute_residuals(pdf, flavor, phyparam):
99+
def compute_residuals(
100+
mag_red, phase, filters, flavor, phyparam, ra=None, dec=None, times=None
101+
):
100102
"""Compute residuals between data and predictions
101103
102104
Parameters
103105
----------
104-
pdf: pandas DataFrame
105-
DataFrame with SSO data from Fink REST API
106+
mag_red: array of float
107+
Reduced magnitude (m-d_obs) for each observation
108+
phase: array of float
109+
Phase angle for each observation, in degree
110+
filters: array of int
111+
Filter ID for each observation
106112
flavor: str
107113
Model flavor: SHG1G2, HG1G2, HG12, or HG
108114
phyparam: dict
109115
Dictionary containing reduced chi2, and estimated parameters and
110116
error on each parameters.
117+
ra: array of float, optional
118+
Array of RA angles, in degree. Only used for flavor in ["SHG1G2", "SSHG1G2"]
119+
dec: array of float, optional
120+
Array of Dec angles, in degree. Only used for flavor in ["SHG1G2", "SSHG1G2"]
121+
times: array of float, optional
122+
Array of times, in JD with light travel correction applied.
123+
Only used for flavor="SSHG1G2"
111124
112125
Returns
113126
-------
114127
pd.Series
115128
Series containing `observation - model` in magnitude
116129
"""
117-
pdf["preds"] = 0.0
118-
for filtnum in pdf["i:fid"].unique():
119-
if filtnum == 3:
120-
continue
121-
cond = pdf["i:fid"] == filtnum
130+
model = np.zeros_like(phase, dtype=float)
131+
for filtnum in np.unique(filters):
132+
# if filtnum == 3:
133+
# continue
134+
cond = filters == filtnum
122135

123136
if flavor == "SSHG1G2":
124137
pha = [
125-
np.deg2rad(pdf["Phase"][cond]),
126-
np.deg2rad(pdf["i:ra"][cond]),
127-
np.deg2rad(pdf["i:dec"][cond]),
138+
np.deg2rad(phase[cond]),
139+
np.deg2rad(ra[cond]),
140+
np.deg2rad(dec[cond]),
141+
times[cond],
128142
]
129143
preds = func_sshg1g2(
130144
pha,
@@ -140,9 +154,9 @@ def compute_residuals(pdf, flavor, phyparam):
140154
)
141155
elif flavor == "SHG1G2":
142156
pha = [
143-
np.deg2rad(pdf["Phase"][cond]),
144-
np.deg2rad(pdf["i:ra"][cond]),
145-
np.deg2rad(pdf["i:dec"][cond]),
157+
np.deg2rad(phase[cond]),
158+
np.deg2rad(ra[cond]),
159+
np.deg2rad(dec[cond]),
146160
]
147161
preds = func_hg1g2_with_spin(
148162
pha,
@@ -155,26 +169,26 @@ def compute_residuals(pdf, flavor, phyparam):
155169
)
156170
elif flavor == "HG":
157171
preds = func_hg(
158-
np.deg2rad(pdf["Phase"][cond]),
172+
np.deg2rad(phase[cond]),
159173
phyparam["H_{}".format(filtnum)],
160174
phyparam["G_{}".format(filtnum)],
161175
)
162176
elif flavor == "HG12":
163177
preds = func_hg12(
164-
np.deg2rad(pdf["Phase"][cond]),
178+
np.deg2rad(phase[cond]),
165179
phyparam["H_{}".format(filtnum)],
166180
phyparam["G12_{}".format(filtnum)],
167181
)
168182
elif flavor == "HG1G2":
169183
preds = func_hg1g2(
170-
np.deg2rad(pdf["Phase"][cond]),
184+
np.deg2rad(phase[cond]),
171185
phyparam["H_{}".format(filtnum)],
172186
phyparam["G1_{}".format(filtnum)],
173187
phyparam["G2_{}".format(filtnum)],
174188
)
175-
pdf.loc[cond, "preds"] = preds
189+
model[cond] = preds
176190

177-
return pdf["i:magpsf_red"] - pdf["preds"]
191+
return mag_red - model
178192

179193

180194
@profile
@@ -278,6 +292,8 @@ def estimate_synodic_period(
278292
"https://api.fink-portal.org/api/v1/sso",
279293
json={"n_or_d": ssnamenr, "withEphem": True, "output-format": "json"},
280294
)
295+
296+
assert r.status_code == 200, r.content
281297
else:
282298
_LOG.error("You need to specify either `ssnamenr` or `pdf`.")
283299

@@ -287,17 +303,26 @@ def estimate_synodic_period(
287303
# get the physical parameters with the latest data
288304
phyparam = extract_physical_parameters(pdf, flavor)
289305

290-
# Compute the residuals (obs - model)
291-
residuals = compute_residuals(pdf, flavor, phyparam)
292-
293306
if lt_correction:
294307
# Speed of light in AU/day
295-
time = compute_light_travel_correction(pdf["i:jd"], pdf["Dobs"])
308+
times = compute_light_travel_correction(pdf["i:jd"], pdf["Dobs"])
296309
else:
297-
time = pdf["i:jd"]
310+
times = pdf["i:jd"]
311+
312+
# Compute the residuals (obs - model)
313+
residuals = compute_residuals(
314+
pdf["i:magpsf_red"],
315+
pdf["Phase"],
316+
pdf["i:fid"],
317+
flavor,
318+
phyparam,
319+
ra=pdf["i:ra"],
320+
dec=pdf["i:dec"],
321+
times=times,
322+
)
298323

299324
model = LombScargleMultiband(
300-
time,
325+
times,
301326
residuals,
302327
pdf["i:fid"],
303328
pdf["i:sigmapsf"],
@@ -316,7 +341,7 @@ def estimate_synodic_period(
316341
# TODO: I need to be convinced...
317342
best_period = 2 / freq_maxpower
318343

319-
out = model.model(time.to_numpy(), freq_maxpower)
344+
out = model.model(times.to_numpy(), freq_maxpower)
320345
prediction = np.zeros_like(residuals)
321346
for index, filt in enumerate(pdf["i:fid"].unique()):
322347
if filt == 3:

fink_utils/sso/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def get_miriade_data(
222222
method="rest",
223223
parameters=None,
224224
timeout=30,
225+
shift=15.0,
225226
uid=None,
226227
):
227228
"""Add ephemerides information from Miriade to a Pandas DataFrame with SSO lightcurve
@@ -278,6 +279,7 @@ def get_miriade_data(
278279
observer=observer,
279280
rplane=rplane,
280281
tcoor=tcoor,
282+
shift=shift,
281283
timeout=timeout,
282284
)
283285
elif method == "ephemcc":
@@ -287,6 +289,7 @@ def get_miriade_data(
287289
observer=observer,
288290
rplane=rplane,
289291
tcoor=tcoor,
292+
shift=shift,
290293
parameters=parameters,
291294
uid=uid,
292295
)
@@ -310,6 +313,7 @@ def get_miriade_data(
310313
pdf_sub["i:jd"],
311314
observer=observer,
312315
rplane="2",
316+
shift=shift,
313317
timeout=timeout,
314318
)
315319
elif method == "ephemcc":
@@ -318,6 +322,7 @@ def get_miriade_data(
318322
pdf_sub["i:jd"],
319323
observer=observer,
320324
rplane="2",
325+
shift=shift,
321326
parameters=parameters,
322327
uid=uid,
323328
)

0 commit comments

Comments
 (0)