-
Notifications
You must be signed in to change notification settings - Fork 6
/
minimisation.py
1635 lines (1156 loc) · 52.3 KB
/
minimisation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import numpy as np
import resource
import random
from sys import stdout
import os
import argparse
import pickle as Pickle
import scipy.optimize
from flarestack.core.injector import read_injector_dict
from flarestack.core.llh import LLH, generate_dynamic_flare_class, read_llh_dict
from flarestack.shared import name_pickle_output_dir, \
inj_dir_name, plot_output_dir, scale_shortener, flux_to_k
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize, ListedColormap
import matplotlib as mpl
from flarestack.core.time_pdf import TimePDF, Box, Steady
from flarestack.core.angular_error_modifier import BaseAngularErrorModifier
from flarestack.utils.catalogue_loader import load_catalogue, \
calculate_source_weight
from flarestack.utils.asimov_estimator import estimate_discovery_potential
logger = logging.getLogger(__name__)
def time_smear(inj):
inj_time = inj["injection_sig_time_pdf"]
max_length = inj_time["max_offset"] - inj_time["min_offset"]
offset = np.random.random() * max_length + inj_time["min_offset"]
inj_time["offset"] = offset
return inj_time
def read_mh_dict(mh_dict):
"""Ensure backwards compatibility of MinimisationHandler dictionary objects
:param mh_dict: MinimisationHandler dictionary
:return: MinimisationHandler dictionary compatible with new format
"""
# Ensure backwards compatibility
maps = [
("inj kwargs", "inj_dict"),
("datasets", "dataset"),
("background TS", "background_ts")
]
for (old_key, new_key) in maps:
if old_key in list(mh_dict.keys()):
logger.warning("Deprecated mh_dict key '{0}' was used. Please use '{1}' in future.".format(
old_key, new_key))
mh_dict[new_key] = mh_dict[old_key]
if "name" not in mh_dict.keys():
raise KeyError("mh_dict object is missing key 'name'."
"This should be the unique save path for results.")
elif mh_dict["name"][-1] != "/":
mh_dict["name"] += "/"
pairs = [
("inj_dict", read_injector_dict),
("llh_dict", read_llh_dict)
]
for (key, f) in pairs:
if key in list(mh_dict.keys()):
mh_dict[key] = f(mh_dict[key])
if np.logical_and("fixed_scale" in mh_dict.keys(), "n_steps" in mh_dict.keys()):
raise Exception(f"MinimisationHandler dictionary contained both 'fixed_scale' key for "
f"set injection flux, and 'n_steps' key for stepped injection flux."
f"Please use only one of these options. \n mh_dict: \n {mh_dict}")
return mh_dict
class MinimisationHandler(object):
"""Generic Class to handle both dataset creation and llh minimisation from
experimental data and Monte Carlo simulation. Initialised with a set of
IceCube datasets, a list of sources, and independent sets of arguments for
the injector and the likelihood.
"""
subclasses = {}
# Each MinimisationHandler must specify which LLH classes are compatible
compatible_llh = []
compatible_negative_n_s = False
def __init__(self, mh_dict):
mh_dict = read_mh_dict(mh_dict)
sources = load_catalogue(mh_dict["catalogue"])
self.name = mh_dict["name"]
self.pickle_output_dir = name_pickle_output_dir(self.name)
self._injectors = dict()
self._llhs = dict()
self._aem = dict()
self.seasons = mh_dict["dataset"]
self.sources = sources
self.mh_dict = mh_dict
if "inj_dict" in mh_dict.keys():
# Checks whether signal injection should be done with a sliding PDF
# within a larger window, or remain fixed at the specified time
inj = dict(mh_dict["inj_dict"])
try:
self.time_smear = inj["injection_sig_time_pdf"]["time_smear_bool"]
except KeyError:
self.time_smear = False
if self.time_smear:
inj["injection_sig_time_pdf"] = time_smear(inj)
self.inj_dict = inj
# An independent set of Season objects can be used for the injector
# This enables, for example, different MC sets to be used for
# injection, to test the impact of different systematics
try:
self.inj_seasons = mh_dict["inj_dict"]["injection_dataset"]
logger.debug("Using independent injection dataset.")
if self.inj_seasons.keys() != self.seasons.keys():
raise Exception("Key mismatch between injection and llh "
"Season objects. Injection Seasons have "
"keys:\n {0} \n and LLH Seasons have keys: \n"
"{1}". format(self.inj_seasons.keys(),
self.seasons.keys()))
except KeyError:
self.inj_seasons = self.seasons
self.llh_dict = mh_dict["llh_dict"]
# Check if the specified MinimisationHandler is compatible with the
# chosen LLH class
if self.llh_dict["llh_name"] not in self.compatible_llh:
raise ValueError("Specified LLH ({}) is not compatible with "
"selected MinimisationHandler".format(
self.llh_dict["llh_name"]))
else:
logger.info("Using '{0}' LLH class".format(self.llh_dict["llh_name"]))
# Checks if negative n_s is specified for use, and whether this is
# compatible with the chosen MinimisationHandler
try:
self.negative_n_s = self.llh_dict["negative_ns_bool"]
except KeyError:
self.negative_n_s = False
if self.negative_n_s and not self.compatible_negative_n_s:
raise ValueError("MinimisationHandler has been instructed to \n"
"allow negative n_s, but this is not compatible \n"
"with the selected MinimisationHandler.")
# Sets up whether what pull corrector should be used (default is
# none), and whether an angular error floor should be applied (
# default is a static floor.
try:
self.pull_name = self.llh_dict["pull_name"]
except KeyError:
self.pull_name = "no_pull"
try:
self.floor_name = self.llh_dict["floor_name"]
except KeyError:
self.floor_name = "static_floor"
p0, bounds, names = self.return_parameter_info(mh_dict)
self.p0 = p0
self.bounds = bounds
self.param_names = names
self.disc_guess = np.nan
@classmethod
def register_subclass(cls, mh_name):
"""Adds a new subclass of EnergyPDF, with class name equal to
"energy_pdf_name".
"""
def decorator(subclass):
cls.subclasses[mh_name] = subclass
return subclass
return decorator
@classmethod
def create(cls, mh_dict):
mh_dict = read_mh_dict(mh_dict)
mh_name = mh_dict["mh_name"]
if mh_name not in cls.subclasses:
raise ValueError('Bad MinimisationHandler name {}'.format(mh_name))
return cls.subclasses[mh_name](mh_dict)
@classmethod
def find_parameter_info(cls, mh_dict):
read_mh_dict(mh_dict)
mh_name = mh_dict["mh_name"]
if mh_name not in cls.subclasses:
raise ValueError('Bad MinimisationHandler name {}'.format(mh_name))
return cls.subclasses[mh_name].return_parameter_info(mh_dict)
def run_trial(self, full_dataset):
pass
def run(self, n_trials, scale=1., seed=None):
pass
@staticmethod
def trial_params(mh_dict):
if "fixed_scale" in list(mh_dict.keys()):
scale_range = [mh_dict["fixed_scale"]]
# elif mh_dict.get("background_only", False):
# # Only do the background trials
# # In this case only n_trials background trials are performed, not 10x n_trials!
# scale_range = np.array([0])
#
# elif mh_dict.get("injection_only", False):
# # Only do trials with signal injection
# scale = mh_dict["scale"]
# steps = int(mh_dict["n_steps"])
# scale_range = np.array(list(np.linspace(0., scale, steps)[1:]))
else:
scale = mh_dict["scale"]
steps = int(mh_dict["n_steps"])
background_ntrials_factor = mh_dict.get('background_ntrials_factor', 10)
scale_range = np.array(
[0. for _ in range(background_ntrials_factor)] +
list(np.linspace(0., scale, steps)[1:])
)
n_trials = int(mh_dict["n_trials"])
return scale_range, n_trials
def iterate_run(self, scale=1., n_steps=5, n_trials=50):
scale_range = np.linspace(0., scale, n_steps)[1:]
self.run(n_trials*10, scale=0.0)
for scale in scale_range:
self.run(n_trials, scale)
@staticmethod
def return_parameter_info(mh_dict):
seeds = []
bounds = []
names = []
return seeds, names, bounds
@staticmethod
def return_injected_parameters(mh_dict):
return {}
def add_likelihood(self, season):
return LLH.create(season, self.sources, self.llh_dict)
def get_likelihood(self, season_name):
if season_name not in self._llhs.keys():
self._llhs[season_name] = self.add_likelihood(self.seasons[season_name])
return self._llhs[season_name]
def add_injector(self, season, sources):
return season.make_injector(sources, **self.inj_dict)
def get_injector(self, season_name):
if season_name not in self._injectors.keys():
self._injectors[season_name] = self.add_injector(self.seasons[season_name], self.sources)
return self._injectors[season_name]
def add_angular_error_modifier(self, season):
return BaseAngularErrorModifier.create(
season, self.llh_dict["llh_energy_pdf"], self.floor_name,
self.pull_name,
gamma_precision=self.llh_dict.get('gamma_precision', 'flarestack')
)
def get_angular_error_modifier(self, season_name):
if season_name not in self._aem.keys():
self._aem[season_name] = self.add_angular_error_modifier(self.seasons[season_name])
return self._aem[season_name]
@staticmethod
def set_random_seed(seed):
np.random.seed(seed)
def guess_scale(self):
"""Method to guess flux scale for sensitivity + discovery potential
:return:
"""
return 1.5 * flux_to_k(self.guess_discovery_potential())
def guess_discovery_potential(self):
self.disc_guess = estimate_discovery_potential(
self.seasons, dict(self.inj_dict), self.sources, dict(self.llh_dict))
return self.disc_guess
@MinimisationHandler.register_subclass('fixed_weights')
class FixedWeightMinimisationHandler(MinimisationHandler):
"""Class to perform generic minimisations using a 'fixed weights' matrix.
Sources are assigned intrinsic weights based on their assumed luminosity
and/or distance, which are fixed. In addition, time weighting is used
assuming a fixed fluence per source. The detector acceptance continues to
vary as a function of the parameters given in minimisation step.
"""
compatible_llh = ["spatial", "fixed_energy", "standard",
"standard_overlapping", "standard_matrix"]
compatible_negative_n_s = True
def __init__(self, mh_dict):
MinimisationHandler.__init__(self, mh_dict)
self.fit_weights = False
# Checks if minimiser should be seeded from a brute scan
try:
self.brute = self.llh_dict["brute_seed"]
except KeyError:
self.brute = False
# self.clean_true_param_values()
def clear(self):
self._injectors.clear()
self._llhs.clear()
del self
def dump_results(self, results, scale, seed):
"""Takes the results of a set of trials, and saves the dictionary as
a pickle pkl_file. The flux scale is used as a parent directory, and the
pickle pkl_file itself is saved with a name equal to its random seed.
:param results: Dictionary of Minimisation results from trials
:param scale: Scale of inputted flux
:param seed: Random seed used for running of trials
"""
if self.name == " /":
logger.warning("No field 'name' was specified in mh_dict object. "
"Cannot save results without a unique directory"
" name being specified.")
else:
write_dir = os.path.join(self.pickle_output_dir, scale_shortener(scale))
# Tries to create the parent directory, unless it already exists
try:
os.makedirs(write_dir)
except OSError:
pass
file_name = os.path.join(write_dir, str(seed) + ".pkl")
logger.debug("Saving to {0}".format(file_name))
with open(file_name, "wb") as f:
Pickle.dump(results, f)
def dump_injection_values(self, scale):
if self.name == " /":
raise Exception("No field 'name' was specified in mh_dict object. "
"Cannot save results without a unique directory"
" name being specified.")
else:
inj_dict = self.return_injected_parameters(scale)
inj_dir = inj_dir_name(self.name)
# Tries to create the parent directory, unless it already exists
try:
os.makedirs(inj_dir)
except OSError:
pass
file_name = os.path.join(inj_dir, scale_shortener(scale) + ".pkl")
logger.debug(f"Dumping Injection values to {file_name}")
with open(file_name, "wb") as f:
Pickle.dump(inj_dict, f)
def run_trial(self, full_dataset):
raw_f = self.trial_function(full_dataset)
def llh_f(scale):
return -np.sum(raw_f(scale))
if self.brute:
brute_range = [
(max(x, -30), min(y, 30)) for (x, y) in self.bounds]
start_seed = scipy.optimize.brute(
llh_f, ranges=brute_range, finish=None, Ns=40)
else:
start_seed = self.p0
res = scipy.optimize.minimize(
llh_f, start_seed, bounds=self.bounds)
vals = res.x
flag = res.status
# If the minimiser does not converge, repeat with brute force
if flag == 1:
vals = scipy.optimize.brute(llh_f, ranges=self.bounds,
finish=None)
best_llh = raw_f(vals)
if np.logical_and(not res.x[0] > 0.0, self.negative_n_s):
bounds = list(self.bounds)
bounds[0] = (-1000., -0.)
start_seed = list(self.p0)
start_seed[0] = -1.
new_res = scipy.optimize.minimize(
llh_f, start_seed, bounds=bounds)
if new_res.status == 0:
res = new_res
vals = [res.x[0]]
best_llh = res.fun
ts = np.sum(best_llh)
if ts == -0.0:
ts = 0.0
parameters = dict()
for i, val in enumerate(vals):
parameters[self.param_names[i]] = val
res_dict = {
"res": res,
"Parameters": parameters,
"TS": ts,
"Flag": flag,
"f": llh_f
}
return res_dict
def run_single(self, full_dataset, scale, seed):
param_vals = {}
for key in self.param_names:
param_vals[key] = []
ts_vals = []
flags = []
res_dict = self.run_trial(full_dataset)
for (key, val) in res_dict["Parameters"].items():
param_vals[key].append(val)
ts_vals.append(res_dict["TS"])
flags.append(res_dict["Flag"])
mem_use = str(
float(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) / 1.e6)
logger.debug('Memory usage max: {0} (Gb)'.format(mem_use))
results = {
"TS": ts_vals,
"Parameters": param_vals,
"Flags": flags,
}
self.dump_results(results, scale, seed)
return res_dict
def simulate_and_run(self, scale, seed=None):
if seed is None:
seed = np.random.randint(low=0, high=99999999)
self.set_random_seed(seed)
full_dataset = self.prepare_dataset(scale, seed)
return self.run_single(full_dataset, scale, seed)
def run(self, n_trials, scale=1., seed=None):
if seed is None:
seed = int(random.random() * 10 ** 8)
np.random.seed(seed)
# param_vals = [[] for x in self.p0]
param_vals = {}
for key in self.param_names:
param_vals[key] = []
ts_vals = []
flags = []
logger.info("Generating {0} trials!".format(n_trials))
for i in range(int(n_trials)):
res_dict = self.simulate_and_run(scale)
for (key, val) in res_dict["Parameters"].items():
param_vals[key].append(val)
ts_vals.append(res_dict["TS"])
flags.append(res_dict["Flag"])
n_inj = 0
for season in self.seasons.keys():
inj = self.get_injector(season)
n_inj += np.sum(inj.n_exp["n_exp"] * scale)
logger.info("Injected with an expectation of {0} events.".format(n_inj))
logger.info("FIT RESULTS:")
for (key, param) in sorted(param_vals.items()):
if len(param) > 0:
logger.info("Parameter {0}: {1} {2} {3}".format(key, np.mean(param),
np.median(param), np.std(param)))
logger.info("Test Statistic: {0} {1} {2}".format(np.mean(ts_vals),
np.median(ts_vals), np.std(ts_vals)))
logger.info("FLAG STATISTICS:")
for i in sorted(np.unique(flags)):
logger.info("Flag {0}:{1}".format(i, flags.count(i)))
results = {
"TS": ts_vals,
"Parameters": param_vals,
"Flags": flags,
}
self.dump_results(results, scale, seed)
self.dump_injection_values(scale)
def make_season_weight(self, params, season):
src = self.sources
weight_scale = calculate_source_weight(src)
# dist_weight = src["distance_mpc"] ** -2
# base_weight = src["base_weight"]
llh = self.get_likelihood(season.season_name)
acc = []
time_weights = []
source_weights = []
for source in src:
time_weights.append(llh.sig_time_pdf.effective_injection_time(
source))
acc.append(llh.acceptance(source, params))
source_weights.append(calculate_source_weight(source) /
weight_scale)
time_weights = np.array(time_weights)
source_weights = np.array(source_weights)
acc = np.array(acc).T[0]
w = acc * time_weights
w *= source_weights
w = w[:, np.newaxis]
return w
def make_weight_matrix(self, params):
# Creates a matrix fixing the fraction of the total signal that
# is expected in each Source+Season pair. The matrix is
# normalised to 1, so that for a given total n_s, the expectation
# for the ith season for the jth source is given by:
# n_exp = n_s * weight_matrix[i][j]
weights_matrix = np.ones([len(self.seasons), len(self.sources)])
for i, season in enumerate(self.seasons.values()):
w = self.make_season_weight(params, season)
for j, ind_w in enumerate(w):
weights_matrix[i][j] = ind_w
return weights_matrix
def prepare_dataset(self, scale=1., seed=None):
if seed is None:
seed = int(random.random() * 10 ** 8)
np.random.seed(seed)
full_dataset = dict()
for name in self.seasons.keys():
full_dataset[name] = self.get_injector(name).create_dataset(
scale, self.get_angular_error_modifier(name)
)
return full_dataset
def trial_function(self, full_dataset):
llh_functions = dict()
n_all = dict()
for name in self.seasons:
dataset = full_dataset[name]
llh_f = self.get_likelihood(name).create_llh_function(
dataset, self.get_angular_error_modifier(name),
self.make_season_weight
)
llh_functions[name] = llh_f
n_all[name] = len(dataset)
def f_final(raw_params):
# If n_s is less than or equal to 0, set gamma to be 3.7 (equal to
# atmospheric background). This is continuous at n_s=0, but fixes
# relative weights of sources/seasons for negative n_s values.
params = list(raw_params)
if (len(params) > 1) and (params[0] < 0):
params[1] = 3.7
# Calculate relative contribution of each source/season
weights_matrix = self.make_weight_matrix(params)
weights_matrix /= np.sum(weights_matrix)
# Having created the weight matrix, loops over each season of
# data and evaluates the TS function for that season
ts_val = 0
for i, name in enumerate(self.seasons):
w = weights_matrix[i][:, np.newaxis]
ts_val += np.sum(llh_functions[name](params, w))
return ts_val
return f_final
def scan_likelihood(self, scale=0., scan_2d=False):
"""Generic wrapper to perform a likelihood scan a background scramble
with an injection of signal given by scale.
:param scale: Flux scale to inject
"""
res_dict = self.simulate_and_run(scale)
res = res_dict["res"]
g = res_dict["f"]
bounds = list(self.bounds)
if self.negative_n_s:
bounds[0] = (-30, 30)
# Scan 1D Likelihood
plt.figure(figsize=(8, 4 + 2*len(self.p0)))
u_ranges = []
for i, bound in enumerate(bounds):
ax = plt.subplot(len(self.p0), 1, 1 + i)
best = list(res.x)
min_llh = np.sum(float(g(best)))
factor = 0.9
if "n_s" in self.param_names[i]:
best[i] = bound[1]
while (g(best) > (min_llh + 5.0)):
best[i] *= factor
ur = min(bound[1], max(best[i], 0))
else:
ur = bound[1]
u_ranges.append(ur)
n_range = np.linspace(float(max(bound[0], -100)), ur, int(1e2))
# n_range = np.linspace(-30, 30, 1e2)
y = []
for n in n_range:
best[i] = n
new = g(best)/2.0
try:
y.append(new[0][0])
except IndexError:
y.append(new)
plt.plot(n_range, y - min(y))
plt.xlabel(self.param_names[i])
plt.ylabel(r"$\Delta \log(\mathcal{L}/\mathcal{L}_{0})$")
logger.info(f"PARAM: {self.param_names[i]}")
min_y = np.min(y)
min_index = y.index(min_y)
min_n = n_range[min_index]
logger.info(f"Minimum value of {min_y} at {min_n}")
logger.info("One Sigma interval between")
l_y = np.array(y[:min_index])
try:
l_y = min(l_y[l_y > (min_y + 0.5)])
l_lim = n_range[y.index(l_y)]
logger.info(l_lim)
except ValueError:
l_lim = min(n_range)
logger.info(f"<{l_lim}")
logger.info("and")
u_y = np.array(y[min_index:])
try:
u_y = min(u_y[u_y > (min_y + 0.5)])
u_lim = n_range[y.index(u_y)]
logger.info(u_lim)
except ValueError:
u_lim = max(n_range)
logger.info(f">{u_lim}")
ax.axvspan(l_lim, u_lim, facecolor="grey",
alpha=0.2)
ax.set_ylim(bottom=0.0)
path = plot_output_dir(self.name) + "llh_scan.pdf"
title = os.path.basename(
os.path.dirname(self.name[:-1])
).replace("_", " ") + " Likelihood Scans"
plt.suptitle(title, y=1.02)
try:
os.makedirs(os.path.dirname(path))
except OSError:
pass
plt.savefig(path)
plt.close()
logger.info("Saved to {0}".format(path))
# Scan 2D likelihood
if np.logical_and(scan_2d, "gamma" in self.param_names):
gamma_index = self.param_names.index("gamma")
gamma_bounds = bounds[gamma_index]
x = np.linspace(gamma_bounds[0], gamma_bounds[1])
mask = np.array(["n_s" in b for b in self.param_names])
n_s_bounds = np.array(self.bounds)[mask]
for j, bound in enumerate(n_s_bounds):
best = list(res.x)
plt.figure(figsize=(5.85, 3.6154988341868854))
ax = plt.subplot(111)
index = np.arange(len(self.param_names))[mask][j]
plt.xlabel(r"Spectral Index ($\gamma$)")
param_name = np.array(self.param_names)[mask][j]
ylabel = 'n$_{\mathrm{signal}}$' if param_name == 'n_s' else param_name
plt.ylabel(ylabel)
y = np.linspace(
float(max(bound[0], -100)),
np.array(u_ranges)[index],
int(1e2)
)
X, Y = np.meshgrid(x, y[::-1])
Z = []
for gamma in x:
best[gamma_index] = gamma
z_row = []
for n in y:
best[index] = n
z_row.append((g(best) - g(res.x))/2.0)
Z.append(z_row[::-1])
Z = np.array(Z).T
levels = 0.5 * np.array([1.0, 2.0, 5.0])**2
N = 2560
mmax = np.max(Z)
mmin = np.min(Z)
break_ind = int(round(N / (1 + mmax / abs(mmin))))
top = cm.get_cmap('gray')
bottom = cm.get_cmap('jet_r', N)
colorlist = np.empty((N, 4))
colorlist[break_ind:] = bottom(np.linspace(0, 1, N - break_ind))
colorlist[:break_ind] = top(np.linspace(1, 0, break_ind))
cmap = ListedColormap(colorlist)
norm = Normalize(vmin=mmin, vmax=mmax, clip=True)
plt.imshow(Z, aspect="auto", cmap=cmap, norm=norm,
extent=(x[0], x[-1], y[0], y[-1]),
interpolation='bilinear')
cbar = plt.colorbar()
CS = ax.contour(X, Y, Z, levels=levels, colors="white")
fmt = {}
strs = [r'1$\sigma$', r'2$\sigma$', r'5$\sigma$']
for l, s in zip(CS.levels, strs):
fmt[l] = s
try:
ax.clabel(CS, fmt=fmt, inline=1, fontsize=10, levels=levels,
colors="white")
except TypeError:
ax.clabel(CS, levels, fmt=fmt, inline=1, fontsize=10, #levels=levels,
colors="white")
ax.set_xlim((min(x), max(x)))
ax.set_ylim((min(y), max(y)))
cbar.set_label(r"$\Delta \log(\mathcal{L}/\mathcal{L}_{0})$",
rotation=90)
path = plot_output_dir(self.name) + (param_name + "_")[4:] + \
"contour_scan.pdf"
title = os.path.basename(
os.path.dirname(self.name[:-1])
).replace("_", " ") + " Contour Scans"
plt.scatter(res.x[gamma_index], res.x[index], color="white",
marker="*")
plt.grid(color="white", linestyle="--", alpha=0.5)
#plt.suptitle(title)
plt.tight_layout()
plt.savefig(path)
plt.close()
logger.info("Saved to {0}".format(path))
return res_dict
def neutrino_lightcurve(self, seed=None):
full_dataset = self.prepare_dataset(30., seed)
for source in self.sources:
f, (ax0, ax1) = plt.subplots(1, 2,
gridspec_kw={'width_ratios': [19, 1]})
logE = []
time = []
sig = []
for season in self.seasons:
# Generate a scrambled dataset, and save it to the datasets
# dictionary. Loads the llh for the season.
data = full_dataset[season]
llh = self.get_likelihood(season)
mask = llh.select_spatially_coincident_data(data, [source])
spatial_coincident_data = data[mask]
t_mask = np.logical_and(
np.greater(
spatial_coincident_data["time"],
llh.sig_time_pdf.sig_t0(source)),
np.less(
spatial_coincident_data["time"],
llh.sig_time_pdf.sig_t1(source))
)
coincident_data = spatial_coincident_data[t_mask]
SoB = llh.estimate_significance(coincident_data, source)
mask = SoB > 1.
y = np.log10(SoB[mask])
if np.sum(mask) > 0:
logE += list(10 ** (coincident_data["logE"][mask] - 3))
time += list(coincident_data["time"][mask])
sig += list(y)
if llh.sig_time_pdf.sig_t0(source) > llh.sig_time_pdf.t0:
ax0.axvline(llh.sig_time_pdf.sig_t0(source), color="k", linestyle="--", alpha=0.5)
if llh.sig_time_pdf.sig_t1(source) < llh.sig_time_pdf.t1:
ax0.axvline(llh.sig_time_pdf.sig_t1(source), color="k", linestyle="--", alpha=0.5)
cmap = cm.get_cmap('jet')
norm = mpl.colors.Normalize(vmin=min(logE), vmax=max(logE),
clip=True)
m = cm.ScalarMappable(norm=norm, cmap=cmap)
for i, val in enumerate(sig):
x = time[i]
ax0.plot([x, x], [0, val], color=m.to_rgba(logE[i]))
if hasattr(self, "res_dict"):
params = self.res_dict["Parameters"]
if len(params) > 1:
ax0.axvspan(
params[f"t_start ({source['source_name']})"],
params[f"t_end ({source['source_name']})"],
facecolor="grey",
alpha=0.2
)
ax0.set_xlabel("Arrival Time (MJD)")
ax0.set_ylabel("Log(Signal/Background)")
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,
norm=norm,
orientation='vertical')
ax1.set_ylabel("Muon Energy Proxy (TeV)")
ax0.set_ylim(bottom=0)
# plt.tight_layout()
path = f"{plot_output_dir(self.name)}neutrino_lightcurve.pdf"
try:
os.makedirs(os.path.dirname(path))
except OSError:
pass
logger.info(f"Saving to {path}")