2020from fink_utils .test .tester import regular_unit_tests
2121
2222
23+ def sort_quantity_by_filter (filter , quantity ):
24+ """Sort a vector (quantity) by its corresponding filter under which it was measured
25+
26+ Parameters
27+ ----------
28+ filter: array-like (1xN)
29+ filters used to measure quantity (1,2,3,4...)
30+ quantity: array-like (1xN)
31+ quantity to be sorted according to the filters
32+
33+ Returns
34+ -------
35+ sorted_quantity: np.array of floats (1xN)
36+ quantity sorted by the filter
37+ """
38+ _ , sorted_quantity = zip (* sorted (zip (filter , quantity )))
39+ return np .array (sorted_quantity )
40+
41+
42+ def split_quantity_by_filter (list_of_filters , ordered_vector ):
43+ """
44+ Split an ordered (by filter) vector at each filter
45+
46+ Parameters
47+ ----------
48+ list_of_filters: array-like (1xN)
49+ filters used to measure quantity (1,2,3,4...)
50+ ordered_vector: array-like (1xN)
51+ quantity to be split according to the filters
52+
53+ Returns
54+ -------
55+ Sub-arrays containing the parts of the ordered_vector at the order of the list_of_filters
56+ """
57+ ufilters = np .unique (list_of_filters )
58+ split_at = []
59+ for filt in ufilters :
60+ mask = list_of_filters == filt
61+ split_at .append (list_of_filters [mask ].size )
62+ return np .array_split (ordered_vector , np .cumsum (split_at ))
63+
64+
2365def func_hg (ph , h , g ):
2466 """Return f(H, G) part of the lightcurve in mag space
2567
@@ -900,19 +942,23 @@ def fit_legacy_models(
900942 # raised if jacobian is degenerated
901943 outdic = {"fit" : 4 , "status" : res_lsq .status }
902944 return outdic
903-
904945 # For the chi2, we use the error estimate from the data directly
905- chisq = np .sum ((res_lsq .fun / sigmapsf ) ** 2 )
946+
947+ sorted_sigmapsf = sort_quantity_by_filter (filters , sigmapsf )
948+
949+ chisq = np .sum ((res_lsq .fun / sorted_sigmapsf ) ** 2 )
906950 chisq_red = chisq / (res_lsq .fun .size - res_lsq .x .size - 1 )
907951
908952 outdic = {"chi2red" : chisq_red , "status" : res_lsq .status , "fit" : 0 }
909953
910954 # Total RMS, and per-band
911955 rms = np .sqrt (np .mean (res_lsq .fun ** 2 ))
912956 outdic ["rms" ] = rms
913- for filt in ufilters :
914- mask = filters == filt
915- outdic ["rms_{}" .format (filt )] = np .sqrt (np .mean (res_lsq .fun [mask ] ** 2 ))
957+
958+ res_lsq_byfilter = split_quantity_by_filter (filters , res_lsq .fun )
959+
960+ for i , filt in enumerate (ufilters ):
961+ outdic ["rms_{}" .format (filt )] = np .sqrt (np .mean (res_lsq_byfilter [i ] ** 2 ))
916962
917963 median_error_phot = np .median (sigmapsf )
918964 outdic ["median_error_phot" ] = median_error_phot
@@ -1092,7 +1138,9 @@ def fit_spin(
10921138 return outdic
10931139
10941140 # For the chi2, we use the error estimate from the data directly
1095- chisq = np .sum ((res_lsq .fun / sigmapsf ) ** 2 )
1141+ sorted_sigmapsf = sort_quantity_by_filter (filters , sigmapsf )
1142+
1143+ chisq = np .sum ((res_lsq .fun / sorted_sigmapsf ) ** 2 )
10961144 chisq_red = chisq / (res_lsq .fun .size - res_lsq .x .size - 1 )
10971145
10981146 geo = cos_aspect_angle (
@@ -1113,9 +1161,11 @@ def fit_spin(
11131161 # Total RMS, and per-band
11141162 rms = np .sqrt (np .mean (res_lsq .fun ** 2 ))
11151163 outdic ["rms" ] = rms
1116- for filt in ufilters :
1117- mask = filters == filt
1118- outdic ["rms_{}" .format (filt )] = np .sqrt (np .mean (res_lsq .fun [mask ] ** 2 ))
1164+
1165+ res_lsq_byfilter = split_quantity_by_filter (filters , res_lsq .fun )
1166+
1167+ for i , filt in enumerate (ufilters ):
1168+ outdic ["rms_{}" .format (filt )] = np .sqrt (np .mean (res_lsq_byfilter [i ] ** 2 ))
11191169
11201170 median_error_phot = np .median (sigmapsf )
11211171 outdic ["median_error_phot" ] = median_error_phot
0 commit comments