@@ -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 :
0 commit comments