@@ -195,6 +195,8 @@ def merge_mapsets_together(mapset_list=None):
195
195
'''Handle merging of multiple MapSets, when they come in
196
196
the shape of a dict
197
197
198
+ TODO: might not be required any longer (see various comments on
199
+ generalized_poisson_llh)
198
200
'''
199
201
200
202
if isinstance (mapset_list [0 ], Mapping ):
@@ -647,16 +649,27 @@ def __init__(
647
649
assert hypo_maker is not None , msg
648
650
assert data_dist is not None , msg
649
651
assert metric is not None , msg
652
+ # this passes through the setter method, but it should just pass through
653
+ # without actually doing anything
650
654
if hypo_maker .__class__ .__name__ == "Detectors" :
651
- # this passes through the setter method, but it should just pass through
652
- # without actually doing anything
653
655
self .detailed_metric_info = [self .get_detailed_metric_info (
654
656
data_dist = data_dist [i ], hypo_asimov_dist = self .hypo_asimov_dist [i ],
655
657
params = hypo_maker .distribution_makers [i ].params , metric = metric [i ],
656
- other_metrics = other_metrics , detector_name = hypo_maker .det_names [i ], hypo_maker = hypo_maker
658
+ other_metrics = other_metrics , detector_name = hypo_maker .det_names [i ], hypo_maker = hypo_maker ,
657
659
) for i in range (len (data_dist ))]
658
- else : # DistributionMaker object
659
- if 'generalized_poisson_llh' == metric [0 ]:
660
+ elif isinstance (data_dist , list ): # DistributionMaker object with variable binning
661
+ self .detailed_metric_info = [self .get_detailed_metric_info (
662
+ data_dist = data_dist [i ], hypo_asimov_dist = self .hypo_asimov_dist [i ],
663
+ params = hypo_maker .params , metric = metric [0 ],
664
+ other_metrics = other_metrics , detector_name = hypo_maker .detector_name , hypo_maker = hypo_maker ,
665
+ ) for i in range (len (data_dist ))]
666
+ else : # DistributionMaker object with regular binning
667
+ if metric [0 ] == 'generalized_poisson_llh' :
668
+ raise NotImplementedError (
669
+ "generalized_poisson_llh isn't correctly implemented any longer!"
670
+ )
671
+ # FIXME: these `output_mode` and `force_standard_output` kwargs were removed in
672
+ # https://github.com/icecube/pisa/commit/7a4e875aa7bdc52ea64a5270e9808d866d1395f3
660
673
generalized_poisson_dist = hypo_maker .get_outputs (return_sum = False , force_standard_output = False )
661
674
generalized_poisson_dist = merge_mapsets_together (mapset_list = generalized_poisson_dist )
662
675
else :
@@ -665,7 +678,7 @@ def __init__(
665
678
self .detailed_metric_info = self .get_detailed_metric_info (
666
679
data_dist = data_dist , hypo_asimov_dist = self .hypo_asimov_dist , generalized_poisson_hypo = generalized_poisson_dist ,
667
680
params = hypo_maker .params , metric = metric [0 ], other_metrics = other_metrics ,
668
- detector_name = hypo_maker .detector_name , hypo_maker = hypo_maker
681
+ detector_name = hypo_maker .detector_name , hypo_maker = hypo_maker ,
669
682
)
670
683
671
684
def __getitem__ (self , i ):
@@ -810,6 +823,7 @@ def get_detailed_metric_info(data_dist, hypo_maker, hypo_asimov_dist, params, me
810
823
detailed_metric_info [m ] = name_vals_d
811
824
812
825
else :
826
+ # TODO: remove this case?
813
827
if isinstance (hypo_asimov_dist , OrderedDict ):
814
828
hypo_asimov_dist = hypo_asimov_dist ['weights' ]
815
829
@@ -1180,17 +1194,6 @@ def fit_recursively(
1180
1194
1181
1195
"""
1182
1196
1183
- if isinstance (metric , str ):
1184
- metric = [metric ]
1185
-
1186
- # Before starting any fit, check if we already have a perfect match between data and template
1187
- # This can happen if using pseudodata that was generated with the nominal values for parameters
1188
- # (which will also be the initial values in the fit) and blah...
1189
- # If this is the case, don't both to fit and return results right away.
1190
-
1191
- if isinstance (metric , str ):
1192
- metric = [metric ]
1193
-
1194
1197
# Grab the hypo map
1195
1198
hypo_asimov_dist = hypo_maker .get_outputs (return_sum = True )
1196
1199
@@ -1201,12 +1204,20 @@ def fit_recursively(
1201
1204
if len (metric ) == 1 : # One metric for all detectors
1202
1205
metric = list (metric ) * len (hypo_maker .distribution_makers )
1203
1206
elif len (metric ) != len (hypo_maker .distribution_makers ):
1204
- raise IndexError ('Number of defined metrics does not match with number of detectors.' )
1205
- else : # DistributionMaker object
1206
- assert len (metric ) == 1
1207
+ raise IndexError (f"Number of defined metrics does not match with number of detectors." )
1208
+ elif isinstance (hypo_asimov_dist , MapSet ) or isinstance (hypo_asimov_dist , list ):
1209
+ # DistributionMaker object (list means variable binning)
1210
+ assert len (metric ) == 1 , f"Only one metric allowed for DistributionMaker"
1211
+ else :
1212
+ raise NotImplementedError (f"hypo_maker returned output of type { type (hypo_asimov_dist )} " )
1207
1213
1208
- # Check if the hypo matches data
1209
- if hypo_maker .__class__ .__name__ != "Detectors" and data_dist .allclose (hypo_asimov_dist ) :
1214
+ # Before starting any fit, check if we already have a perfect match
1215
+ # between data and template. This can happen if using pseudodata that
1216
+ # was generated with the nominal values for parameters (which will also
1217
+ # be the initial values in the fit). If this is the case, don't bother
1218
+ # to fit and return results right away. TODO: This speedup is currently
1219
+ # only enabled for a DistributionMaker with regular binning.
1220
+ if isinstance (data_dist , MapSet ) and data_dist .allclose (hypo_asimov_dist ):
1210
1221
1211
1222
msg = 'Initial hypo matches data, no need for fit'
1212
1223
logging .info (msg )
@@ -2756,12 +2767,18 @@ def _minimizer_callable(self, scaled_param_vals, hypo_maker, data_dist,
2756
2767
# Get the map set
2757
2768
try :
2758
2769
if metric [0 ] == 'generalized_poisson_llh' :
2770
+ raise NotImplementedError (
2771
+ "generalized_poisson_llh isn't correctly implemented any longer!"
2772
+ )
2773
+ # FIXME: these `output_mode` and `force_standard_output` kwargs were removed in
2774
+ # https://github.com/icecube/pisa/commit/7a4e875aa7bdc52ea64a5270e9808d866d1395f3
2759
2775
hypo_asimov_dist = hypo_maker .get_outputs (return_sum = False , output_mode = 'binned' , force_standard_output = False )
2760
2776
hypo_asimov_dist = merge_mapsets_together (mapset_list = hypo_asimov_dist )
2761
2777
data_dist = data_dist .maps [0 ] # Extract the map from the MapSet
2762
2778
metric_kwargs = {'empty_bins' :hypo_maker .empty_bin_indices }
2763
2779
else :
2764
2780
hypo_asimov_dist = hypo_maker .get_outputs (return_sum = True )
2781
+ # TODO: can be removed? (see same commit as above)
2765
2782
if isinstance (hypo_asimov_dist , OrderedDict ):
2766
2783
hypo_asimov_dist = hypo_asimov_dist ['weights' ]
2767
2784
metric_kwargs = {}
@@ -2785,11 +2802,17 @@ def _minimizer_callable(self, scaled_param_vals, hypo_maker, data_dist,
2785
2802
metric_val = 0
2786
2803
for i in range (len (hypo_maker .distribution_makers )):
2787
2804
data = data_dist [i ].metric_total (expected_values = hypo_asimov_dist [i ],
2788
- metric = metric [i ], metric_kwargs = metric_kwargs )
2805
+ metric = metric [i ], metric_kwargs = metric_kwargs )
2789
2806
metric_val += data
2790
2807
priors = hypo_maker .params .priors_penalty (metric = metric [0 ]) # uses just the "first" metric for prior
2791
2808
metric_val += priors
2792
- else : # DistributionMaker object
2809
+ elif isinstance (hypo_asimov_dist , list ): # DistributionMaker object with variable binning
2810
+ metric_val = 0
2811
+ for i in range (len (hypo_asimov_dist )):
2812
+ metric_val += data_dist [i ].metric_total (expected_values = hypo_asimov_dist [i ],
2813
+ metric = metric [0 ], metric_kwargs = metric_kwargs )
2814
+ metric_val += hypo_maker .params .priors_penalty (metric = metric [0 ])
2815
+ else : # DistributionMaker object with regular binning
2793
2816
if metric [0 ] == 'weighted_chi2' :
2794
2817
actual_values = data_dist .hist ['total' ]
2795
2818
expected_values = hypo_asimov_dist .hist ['total' ]
@@ -3121,9 +3144,9 @@ def fit_hypo(self, data_dist, hypo_maker, metric, minimizer_settings,
3121
3144
if len (metric ) == 1 : # One metric for all detectors
3122
3145
metric = list (metric ) * len (hypo_maker .distribution_makers )
3123
3146
elif len (metric ) != len (hypo_maker .distribution_makers ):
3124
- raise IndexError (' Number of defined metrics does not match with number of detectors.' )
3125
- else : # DistributionMaker object
3126
- assert len (metric ) == 1
3147
+ raise IndexError (f" Number of defined metrics does not match with number of detectors." )
3148
+ else :
3149
+ assert len (metric ) == 1 , f"Only one metric allowed for DistributionMaker"
3127
3150
3128
3151
if check_ordering :
3129
3152
if 'nh' in hypo_param_selections or 'ih' in hypo_param_selections :
@@ -3209,9 +3232,12 @@ def nofit_hypo(self, data_dist, hypo_maker, hypo_param_selections,
3209
3232
if len (metric ) == 1 : # One metric for all detectors
3210
3233
metric = list (metric ) * len (hypo_maker .distribution_makers )
3211
3234
elif len (metric ) != len (hypo_maker .distribution_makers ):
3212
- raise IndexError ('Number of defined metrics does not match with number of detectors.' )
3213
- else : # DistributionMaker object
3214
- assert len (metric ) == 1
3235
+ raise IndexError (f"Number of defined metrics does not match with number of detectors." )
3236
+ elif isinstance (hypo_asimov_dist , MapSet ) or isinstance (hypo_asimov_dist , list ):
3237
+ # DistributionMaker object (list means variable binning)
3238
+ assert len (metric ) == 1 , f"Only one metric allowed for DistributionMaker"
3239
+ else :
3240
+ raise NotImplementedError (f"hypo_maker returned output of type { type (hypo_asimov_dist )} " )
3215
3241
fit_info .metric = metric
3216
3242
3217
3243
# Assess the fit: whether the data came from the hypo_asimov_dist
@@ -3223,10 +3249,19 @@ def nofit_hypo(self, data_dist, hypo_maker, hypo_param_selections,
3223
3249
metric_val += data
3224
3250
priors = hypo_maker .params .priors_penalty (metric = metric [0 ]) # uses just the "first" metric for prior
3225
3251
metric_val += priors
3226
- else : # DistributionMaker object
3252
+ elif isinstance (data_dist , list ): # DistributionMaker object with VarBinning
3253
+ metric_val = 0
3254
+ for i in range (len (data_dist )):
3255
+ metric_val += data_dist [i ].metric_total (expected_values = hypo_asimov_dist [i ], metric = metric [0 ])
3256
+ metric_val += hypo_maker .params .priors_penalty (metric = metric [0 ])
3257
+ else : # DistributionMaker object with MultiDimBinning
3227
3258
3228
3259
if 'generalized_poisson_llh' == metric [0 ]:
3229
-
3260
+ raise NotImplementedError (
3261
+ "generalized_poisson_llh isn't correctly implemented any longer!"
3262
+ )
3263
+ # FIXME: these `output_mode` and `force_standard_output` kwargs were removed in
3264
+ # https://github.com/icecube/pisa/commit/7a4e875aa7bdc52ea64a5270e9808d866d1395f3
3230
3265
hypo_asimov_dist = hypo_maker .get_outputs (return_sum = False , output_mode = 'binned' , force_standard_output = False )
3231
3266
hypo_asimov_dist = merge_mapsets_together (mapset_list = hypo_asimov_dist )
3232
3267
data_dist = data_dist .maps [0 ] # Extract the map from the MapSet
@@ -3271,9 +3306,20 @@ def nofit_hypo(self, data_dist, hypo_maker, hypo_param_selections,
3271
3306
params = hypo_maker .distribution_makers [i ].params , metric = metric [i ],
3272
3307
other_metrics = other_metrics , detector_name = hypo_maker .det_names [i ]
3273
3308
) for i in range (len (data_dist ))]
3274
- else : # DistributionMaker object
3309
+ elif isinstance (data_dist , list ): # DistributionMaker object with VarBinning
3310
+ fit_info .detailed_metric_info = [fit_info .get_detailed_metric_info (
3311
+ data_dist = data_dist [i ], hypo_asimov_dist = hypo_asimov_dist [i ],
3312
+ params = hypo_maker .params , metric = metric [0 ], other_metrics = other_metrics ,
3313
+ detector_name = hypo_maker .detector_name
3314
+ ) for i in range (len (data_dist ))]
3315
+ else : # DistributionMaker object with MultiDimBinning
3275
3316
3276
3317
if 'generalized_poisson_llh' == metric [0 ]:
3318
+ raise NotImplementedError (
3319
+ "generalized_poisson_llh isn't correctly implemented any longer!"
3320
+ )
3321
+ # FIXME: these `output_mode` and `force_standard_output` kwargs were removed in
3322
+ # https://github.com/icecube/pisa/commit/7a4e875aa7bdc52ea64a5270e9808d866d1395f3
3277
3323
generalized_poisson_dist = hypo_maker .get_outputs (return_sum = False , force_standard_output = False )
3278
3324
generalized_poisson_dist = merge_mapsets_together (mapset_list = generalized_poisson_dist )
3279
3325
else :
0 commit comments