-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1115 lines (901 loc) · 41 KB
/
utils.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
from math import sqrt, log10, pi, exp
import os
import numpy as np
from functools import reduce
import sys
import json # config file
import pickle
from glob import glob # grabs all the files!
from numbers import Number
from scipy.integrate import quad
sqtwo = 1/sqrt(2*pi)
"""
Ben Smithers
This defines several utility functions and objects that are frequently used throughout this code base
"""
flavors = ['E', 'Mu', 'Tau']
neuts = ['nu', 'nuBar']
currents = ['NC', 'CC']
# load in the configuration file
# this defines a lot of... configuration... parameters
f = open(os.path.join(os.path.dirname(__file__), "config.json"), 'r')
config = json.load(f)
f.close()
import matplotlib
def shift_cmap(cmap, frac):
"""Shifts a colormap by a certain fraction.
Keyword arguments:
cmap -- the colormap to be shifted. Can be a colormap name or a Colormap object
frac -- the fraction of the colorbar by which to shift (must be between 0 and 1)
"""
N=256
if isinstance(cmap, str):
cmap = plt.get_cmap(cmap)
n = cmap.name
x = np.linspace(0,1,N)
out = np.roll(x, int(N*frac))
new_cmap = matplotlib.colors.LinearSegmentedColormap.from_list(f'{n}_s', cmap(out))
return new_cmap
import matplotlib.pyplot as plt
def get_color(n, colormax=3.0, cmap="viridis"):
"""
Discretize a colormap. Great getting nice colors for a series of trends on a single plot!
"""
this_cmap = plt.get_cmap(cmap)
return this_cmap(n/colormax)
def pathmaker(path):
folder = os.path.split(path)[0]
if not os.path.exists(path):
broken = path.split("/")
working = ""
for part in broken:
working = "/".join([working] + [part])
if not os.path.exists(working):
os.mkdir(working)
def make_bin_probs(cdf, bin_edges, normalize=False):
"""
We find the binned likelihoods based on a binned gaussian distribution.
As in, if we sample some quantity with a gaussian distribution centered at MEAN with a width SIGMA,
return the odds it will be measured in each of the bins defined by BIN_EDGES
"""
if not isinstance(bin_edges, (list, tuple, np.ndarray)):
raise TypeError()
if len(bin_edges)<2:
raise ValueError()
occupation = np.zeros(len(bin_edges)-1)
for i in range(len(occupation)):
occupation[i] = quad(cdf, a=bin_edges[i], b=bin_edges[i+1])[0]
if normalize:
# occupation = np.abs(occupation)
return occupation/sum(occupation)
else:
return occupation
def backup(filename):
"""
This function checks if a file exists by the given name
If the file exists, it renames the file by appending "Copy of [...]" to the file's name (considering full path too. This works recursively too! The newest is always the one with the least "Copy of"s
[File] [Copy of File]
becomes
[File] [Copy of File] [Copy of Copy of File]
Returns nothing.
"""
if not os.path.exists(filename):
return
else:
# such a file already exists
dirname, file_obj = os.path.split(filename)
# get the new destination for the file
# then, we call this function to check if something of the same name already exists
file_obj = "Copy of "+file_obj
backup(os.path.join(dirname, file_obj))
# Now rename the existing file to the new name
os.rename(filename, os.path.join(dirname, file_obj))
def savefile(name, **kwargs):
"""
This allows me to quickly files. You specify the filename and use keywords to provide the rest of the elements you want to save
Then it quickly pickles it
returns nothing
Respects the configured "overwrite" settings
I really like this script tbh
"""
if not config["overwrite"]:
backup(name)
f = open(name, 'wb')
pickle.dump( kwargs, f, -1)
f.close()
print("Saved {}".format(name))
def sep_by_flavor(nuflux):
"""
So this takes that nuflux object, a dictionary of 3D arrays, and separates it into two 3D arrays: one for muons and one for non-muons
"""
if not isinstance(nuflux, dict):
raise TypeError("nuflux should be a {}, not a {}".format(dict, type(nuflux)))
if not isinstance(nuflux[list(nuflux.keys())[0]], np.ndarray):
raise TypeError("Entries in nuflux should all be {}, not {}".format(np.ndarray, type(nuflux[list(nuflux.keys())[0]])))
entry_shape = np.shape(nuflux[list(nuflux.keys())[0]])
from_muons = np.zeros(shape=entry_shape)
from_not = np.zeros(shape=entry_shape)
for key in nuflux:
flavor = key.split('_')[0]
if flavor=="Mu":
from_muons+=nuflux[key]
else:
from_not+=nuflux[key]
return(from_muons, from_not)
def multimat(*args):
"""
Slick little function for multiplying an arbitrary number of matrices together!
"""
assert(len(args)>=2)
amt = args[0]
for arg in range(len(args)):
if arg==0:
continue
amt = np.matmul(amt, args[arg])
return(amt)
def sci(number, precision=4):
"""
Returns a string representing the number in scientific notation
"""
if not isinstance(number, (int, float)):
raise TypeError("Expected {}, not {}".format(float, type(number)))
if not isinstance(precision,int):
raise TypeError("Precision must be {}, not {}".format(int, type(precision)))
try:
power = int(log10(abs(number)))
except ValueError:
return("0.0")
return("{0:.{1}f}".format(number/(10**power), precision)+"e{}".format( power))
def check_valid_angle(angle,is_zenith, radians=True):
"""
Returns True if parameter "angle" is a valid angle.
"radians" flag used to specify if the value is radians. False means it's degrees
"""
if not isinstance(angle, (float,int)):
return False
if not isinstance(is_zenith, bool):
raise TypeError("Expected {}, got {} for is_zenith flag".format(bool, type(is_zenith)))
if not isinstance(radians, bool):
raise TypeError("Expected {}, got {} for radians specifier".format(bool, type(radians)))
# convert to radians if necessary
scaled = angle if radians else angle*pi/180
if is_zenith:
if angle<0 or angle>pi:
return False
else: # is azimuth angle
if angle<0 or angle>(2*pi):
return False
return True
class NewPhysicsParams:
def __init__(self, **kwargs) -> None:
pass
def __eq__(self, other):
if isinstance(other, NewPhysicsParams):
return True
return False
def __repr__(self):
return("New Physics Object")
def __str__(self):
raise NotImplementedError("")
class SterileParams(NewPhysicsParams):
"""
This object is used to pass around the new sterile physics parameters around
TODO:
- figure out a way to cleanly bring in the phase parameters without making uglier filenames
"""
def __init__(self, theta03=0.0, theta13=0.0, theta23=0.0, msq2=0.0, **kwargs):
if not isinstance(theta03, (int, float)):
raise TypeError("theta03 should be {}, not {}".format(float, type(theta03)))
if not isinstance(theta13, (int, float)):
raise TypeError("theta13 should be {}, not {}".format(float, type(theta13)))
if not isinstance(theta23, (int, float)):
raise TypeError("theta23 should be {}, not {}".format(float, type(theta23)))
if not isinstance(msq2, (int, float)):
raise TypeError("msq2 should be {}, not {}".format(float, type(msq2)))
if not check_valid_angle(theta03, True):
raise ValueError("Invalid theta03: {}".format(theta03))
if not check_valid_angle(theta13, True):
raise ValueError("Invalid theta03: {}".format(theta13))
if not check_valid_angle(theta23, True):
raise ValueError("Invalid theta03: {}".format(theta23))
if msq2<0:
raise ValueError("Mass-squared difference must be >=0, not {}".format(msq2))
self.theta03 = theta03
self.theta13 = theta13
self.theta23 = theta23
self.msq2 = msq2
allowed = ["maj_phase_1","maj_phase_2", "maj_phase_3", "ordering"]
for kwarg in kwargs:
if kwarg not in allowed:
raise ValueError("Unrecognized argument {}".format(kwarg))
self.majorana = False
if "maj_phase_1" in kwargs:
self.maj_phase_1 = kwargs["maj_phase_1"]
self.majorana = True
else:
self.maj_phase_1 = 0.0
if "maj_phase_2" in kwargs:
self.maj_phase_2 = kwargs["maj_phase_2"]
self.majorana = True
else:
self.maj_phase_2 = 0.0
if "maj_phase_3" in kwargs:
self.maj_phase_3 = kwargs["maj_phase_3"]
self.majorana = True
else:
self.maj_phase_3 = 0.0
if "ordering" in kwargs:
self.no_ordering = kwargs["ordering"] #NOrmal ordering
else:
self.no_ordering = False
def __eq__(self, other):
if isinstance(other, SterileParams):
return ( self.theta03==other.theta03 and self.theta13==other.theta13 and
self.theta23==other.theta23 and self.msq2==other.msq2)
return False
def __repr__(self):
return("Sterile-nu Params Object")
def __str__(self):
return("_".join((sci(self.theta03), sci(self.theta13), sci(self.theta23), sci(self.msq2))))
class NSIParams(NewPhysicsParams):
def __init__(self, eps_mutau:complex, **kwargs) -> None:
super().__init__(**kwargs)
self._epsmutau = eps_mutau
def __str__(self):
return "nsiparam_"+"_".join(["{:.6E}".format(entry) for entry in [self._epsmutau.real, self._epsmutau.imag]])
def parse_filename(path, as_params=True):
"""
Takes a filename made by 'gen_filename' and returns the oscillation parameters used to make the filename
If as_params is true, the parameters are returned as a SterileParameters object,
otherwise they are returned in a tuple
returns: three floats in a tuple
"""
dirname, filename = os.path.split(path)
name = ".".join(filename.split(".")[:-1])
entries = name.split("_")
theta03 = float(entries[2])
theta13 = float(entries[3])
theta23 = float(entries[4])
msq2 = float(entries[5])
params = SterileParams( theta03=theta03, theta13=theta13, theta23=theta23, msq2=msq2)
if as_params:
return(params)
else:
return(theta03, theta13, theta23, msq2)
def gen_filename(dirname, filename, params, subfolder=True):
"""
Takes a directory, a generic filename (like "datafile.dat"), and some physics parameters
returns a full path that is unique to that set of physics parameters
"""
if not isinstance(params, SterileParams):
raise TypeError("Expected {}, got {}".format(SterileParams, type(params)))
names = filename.split(".")
assert(len(names)==2) # name, ext
prefix = names[0]
suffix = names[1]
str_params = str(params)
filename_partial = prefix+"_"+str_params
filename = os.path.join( dirname, ".".join((filename_partial,suffix)))
if not subfolder:
return(filename)
# make a subfolder based on theta_24
th24 = str_params.split("_")[1]
new_path = os.path.join(dirname, th24, ".".join((filename_partial,suffix)))
new_dir = os.path.join(dirname, th24)
if not os.path.exists(new_dir):
os.mkdir(os.path.join(dirname, th24))
# pathmaker(new_dir) # default cascade import
return new_path
def enumerate_failures(dirname, filename):
"""
This function provides a list of the parameter points missing, assuming the normal spread of points
"""
n_grid=90
theta24s = np.concatenate(( [0], np.arcsin(np.sqrt(np.logspace(-3, 0, n_grid)))/2.0 ))
theta34s = np.concatenate(( [0], np.arcsin(np.sqrt(np.logspace(-3, 0, n_grid)))/2.0))
msqs = np.concatenate( ([0], np.logspace(-2,2,40)) )
pams = []
count = 0
found = 0
for i24 in theta24s:
for i34 in theta34s:
for msq in msqs:
if count%5000==0:
print("Done did {} many so far, found {}".format(count, found))
pam = SterileParams(theta13=i24, theta23=i34, msq2=msq)
fn = gen_filename(dirname, filename, pam)
if not os.path.exists(fn):
pams.append(pam)
else:
found +=1
count+=1
print("Found {} of {}, that's {:.1f}%".format(found, count, 100*float(found)/count))
return pams
def get_index( key, n_flavor=3 ):
'''
Returns the column in the atmosphere data file for a given key
'''
if not isinstance(key, str):
raise TypeError("Expected {}, got {}".format(str, type(key)))
split = key.split('_')
flavor = split[0] # E Mu Tau
variety = split[1] # nu or nu-bar
flav_index = flavors.index(flavor) # 0, 1, or 2
variety_index = neuts.index(variety) # 0 or 1
sterile_mod = n_flavor-3
return( 2 + int( flav_index + (len(flavors)+sterile_mod)*variety_index) )
def bad_get_loc(value, edges):
"""
Deprecated, don't use. Just use 'get_loc'
"""
if value<edges[0] or value>edges[-1]:
return None
else:
scan = 0
while not (value>=edges[scan] and value<=edges[scan+1]): # remember, the edges are sorted - so this should happen
scan += 1
if scan==len(edges)-1:
raise Exception("Something bad happened with logic")
return(scan)
def get_loc(x, domain,closest=False):
"""
Returns the indices of the entries in domain that border 'x'
Raises exception if x is outside the range of domain
Assumes 'domain' is sorted!! And this _only_ works if the domain is length 2 or above
This is made for finding bin numbers on a list of bin edges
"""
if not isinstance(domain, (tuple,list,np.ndarray)):
raise TypeError("'domain' has unrecognized type {}, try {}".format(type(domain), list))
if not isinstance(x, (float,int)):
raise TypeError("'x' should be number-like, not {}".format(type(x)))
if len(domain)<=1:
raise ValueError("get_loc function only works on domains of length>1. This is length {}".format(len(domain)))
if x<domain[0] or x>domain[-1]:
raise ValueError("x={} and is outside the domain: ({}, {})".format(sci(x), sci(domain[0]), sci(domain[-1])))
# I think this is a binary search
min_abs = 0
max_abs = len(domain)-1
lower_bin = int(abs(max_abs-min_abs)/2)
upper_bin = lower_bin+1
while not (domain[lower_bin]<=x and domain[upper_bin]>=x):
if abs(max_abs-min_abs)<=1:
print("{} in {}".format(x, domain))
raise Exception("Uh Oh")
if x<domain[lower_bin]:
max_abs = lower_bin
if x>domain[upper_bin]:
min_abs = upper_bin
# now choose a new middle point for the upper and lower things
lower_bin = min_abs + int(abs(max_abs-min_abs)/2)
upper_bin = lower_bin + 1
assert(x>=domain[lower_bin] and x<=domain[upper_bin])
if closest:
return( lower_bin if abs(domain[lower_bin]-x)<abs(domain[upper_bin]-x) else upper_bin )
else:
return(lower_bin, upper_bin)
def parse_folder(path, name_tmp = config["recon_flux"]+"*.dat"):
"""
Takes a folder, and creates lists of all the parameters used there
Optionally takes a name template "name_tmp" for the globbing
"""
if not os.path.exists(path):
raise ValueError("Path does not exist: {}".format(path))
# we want to scan over all these files and build ordered-lists of the paramters
all_files = glob( os.path.join( path, name_tmp) )
def insert( value, list_like ):
"""
This is a "smart insert" function I wrote
It adds in the value to the list so long as it's not already there, and it keeps the list ordered. So this really assumes that the list is ordered from the get-go
"""
if not isinstance(value, float):
raise TypeError("Value should be {}, not {}".format(float, type(value)))
if not isinstance(list_like, list):
raise TypeError("list_like should be {}, not {}".format(list, type(list_like)))
# these two cases aren't covered by the get_loc function
if len(list_like)==0:
return([value])
if value==list_like[0] or value==list_like[-1]:
return(list_like)
if value<list_like[0]:
list_like.insert(0, value)
return(list_like)
if value>list_like[-1]:
list_like.append(value)
return(list_like)
bot, top = get_loc(value, list_like) # O(log(N))
if value==list_like[bot] or value==list_like[top]: #O(1) ZOOM!
return(list_like)
else:
list_like.insert(top, value) #O(n) complexity >:(
return(list_like)
theta03s = []
theta13s = []
theta23s = []
msqs = []
for each in all_files:
theta03, theta13, theta23, msq = parse_filename(each, False)
theta03s = insert(theta03, theta03s)
theta13s = insert(theta13, theta13s)
theta23s = insert(theta23, theta23s)
msqs = insert(msq, msqs)
return(theta03s, theta13s, theta23s, msqs)
def get_closest(x, domain, mapped):
"""
We imagine some function maps from "domain" to "mapped"
We have several points evaluated for this function
domain - list-like of floats.
mapped - list-like of floats. Entries in domain, evaluated by the function
The user provides a value "x," and then we interpolate the mapped value on either side of 'x' to approximate the mapped value of 'x'
This is really just a linear interpolator
"""
if not isinstance(domain, (tuple,list,np.ndarray)):
raise TypeError("'domain' has unrecognized type {}, try {}".format(type(domain), list))
if not isinstance(mapped, (tuple,list,np.ndarray)):
print(mapped)
raise TypeError("'mapped' has unrecognized type {}, try {}".format(type(mapped), list))
if not isinstance(x, (float,int)):
raise TypeError("'x' should be number-like, not {}".format(type(x)))
if len(domain)!=len(mapped):
raise ValueError("'domain' and 'mapped' should have same length, got len(domain)={}, len(mapped)={}".format(len(domain), len(mapped)))
lower_bin, upper_bin = get_loc(x, domain)
# linear interp time
x1 = domain[lower_bin]
x2 = domain[upper_bin]
y1 = mapped[lower_bin]
y2 = mapped[upper_bin]
slope = (y2-y1)/(x2-x1)
value = (x*slope + y2 -x2*slope)
# print("({}, {}) to ({}, {}) gave {}".format(x1,y1,x2,y2, value))
return(value)
def bilinear_interp(p0, p1, p2, q11, q12, q21, q22):
"""
Performs a bilinear interpolation on a 2D surface
Four values are provided (the qs) relating to the values at the vertices of a square in the (x,y) domain
p0 - point at which we want a value (len-2 tuple)
p1 - coordinates bottom-left corner (1,1) of the square in the (x,y) domain (len-2 tuple)
p2 - upper-right corner (2,2) of the square in the (X,y) domain (len-2 tuple)
qs - values at the vertices of the square (See diagram), any value supporting +/-/*
right now: floats, ints, np.ndarrays
(1,2)----(2,2)
| |
| |
(1,1)----(2,1)
"""
for each in [p0,p1,p2]:
if not (isinstance(each, tuple) or isinstance(each, list) or isinstance(each, np.ndarray)):
# I /would/ like to print out the bad value, but it might not be castable to a str?
try:
print("Found this: {}".format(each))
except ValueError:
pass
raise TypeError("Expected {} for one of the points, got {}".format(tuple, type(each)))
if len(each)!=2:
raise ValueError("Points should be length {}, not {}!".format(2, len(each)))
for val in [q11, q12, q21, q22]:
if not isinstance(val, (float, int, np.ndarray)):
raise TypeError("Expected {} for a value, got {}".format(float, type(val)))
# check this out for the math
# https://en.wikipedia.org/wiki/Bilinear_interpolation
x0 = p0[0]
x1 = p1[0]
x2 = p2[0]
y0 = p0[1]
y1 = p1[1]
y2 = p2[1]
if not (x0>=x1 and x0<=x2):
raise ValueError("You're doing it wrong. x0 should be between {} and {}, got {}".format(x1,x2,x0))
if not (y0>=y1 and y0<=y2):
raise ValueError("You're doing it wrong. y0 should be between {} and {}, got {}".format(y1,y2,y0))
# this is some matrix multiplication. See the above link for details
# it's not magic, it's math. Mathemagic
mat_mult_1 = [q11*(y2-y0) + q12*(y0-y1) , q21*(y2-y0) + q22*(y0-y1)]
mat_mult_final = (x2-x0)*mat_mult_1[0] + (x0-x1)*mat_mult_1[1]
rval = mat_mult_final/((x2-x1)*(y2-y1))
return rval
class Calculable:
def __init__(self):
if not os.path.exists(self.filename):
self._obj = self.generate()
self._save(self.obj)
else:
self._obj = self._load()
@property
def obj(self):
return(self._obj)
@property
def filename(self):
raise NotImplemented("Override default function!")
def _save(self, obj):
f=open(self.filename,'wb')
pickle.dump(obj, f, -1)
f.close()
def _load(self):
f = open(self.filename,'rb')
all_data = pickle.load(f)
f.close()
return(all_data)
def generate(self):
raise NotImplemented("Override default function!")
def eval(self, value):
raise NotImplemented("Override default function!")
class Data:
"""
This is used as a container for the data loaded in from nuSQuIDS.
The main benefit of this is that the objects used by the interpolator are kept in a sterile scope,
and we don't have to worry about accidentally renaming an important object!
It loads it up into a convenient format for access, and provides a function for interpolating what is loaded.
"""
def __init__(self, filename=config["nu_flux"], n_flavor = 4):
"""
Loads in the specified nuSQuIDS datafile.
Creates a "flux" dictionary for each type of neutrino and interaction. This is in units of N/s/GeV/cm2/sr
"""
location = os.path.join(config["datapath"], filename)
nus_file = os.path.splitext(filename)[1] ==".hdf5"
# store the flavors, neutrinos, and currents
self.flavors = flavors
self.neuts = neuts
self.currents = currents
self._fluxes = {}
if nus_file:
import nuSQuIDS as nsq
from cascade.nus_utils import get_flavor, get_neut
data = nsq.nuSQUIDSAtm(location)
self._energies = data.GetERange()
self._angles = data.GetCosthRange()
for key in self.get_keys():
self._fluxes[ key ] = [[ data.EvalFlavor(get_flavor(key), angle, energy, get_neut(key)) for angle in self._angles] for energy in self._energies]
else:
print("Loading Neutrino Flux from {}".format(location))
data = np.loadtxt(location, dtype=float, comments='#',delimiter=' ')
n_energies = len(np.unique(data.T[0]))
n_angles = len(np.unique(data.T[1]))
GeV=1e9
if not (len(data)==n_energies*n_angles):
raise ValueError("Datafile length error? {}!={}".format(len(data), n_energies*n_angles))
# storing the energies and the angles...
# this was originally supposed to be agnostic to growing/shrinking energies/angles, but the code really assumes increasing.
self._energies = [10**data[i][0] for i in range(n_energies)]
self._angles = [data[n_energies*i][1] for i in range(n_angles)]
# let's fill out some flux functions
# in the data file, these data are written in a big list. But that's not a very handy format
# so I'm converting these into 2D arrays
for key in self.get_keys():
# indexed like [energy_bin][angle_bin]
# you may notice that NC and CC are treated as having separate fluxes, when really it'sthe same flux
# this is for the most part okay since the interactions are rare enough that the fluxes are unchanged
self._fluxes[ key ] = [[ data[energy+angle*n_energies][get_index(key, n_flavor)] for angle in range(n_angles)] for energy in range(n_energies)]
if np.min(self._fluxes[key])<0:
raise ValueError("Tried loading a flux with a negative value {}".format(np.min(self._fluxes[key])))
self.growing = self._energies[1]>self._energies[0]
en_width = get_width(self._energies)/GeV
self._ang_width = get_width(np.arccos(self._angles))
self.ang_grow = self._angles[1]>self._angles[0]
# define a few access functions to protect the important stuff
# the "@property" tag makes it so these are accessed like attributes, not functions!
@property
def energies(self):
return(self._energies)
@property
def angles(self):
return(self._angles)
@property
def ang_width(self):
return(self._ang_width)
@property
def fluxes(self):
return(self._fluxes)
def get_keys(self, just_casc = False, just_tracks=False):
"""
Returns a list of all the keys in the dictionary of fluxes
"""
keys = []
for flav in self.flavors:
for neut in self.neuts:
for curr in self.currents:
if just_casc and (flav.lower()=="mu" and curr.lower()=="cc"):
continue
elif just_tracks and not (flav.lower()=="mu" and curr.lower()=="cc"):
continue
key = flav+'_'+neut + '_'+curr
keys.append(key)
return(keys)
def get_flux(self, energy, key, use_overflow = False, angle=None):
'''
interpolates between entries in the flux dictionary to return the flux at arbitrary energy and angle
Energy should be in units of eV
Angle should be in units of cos(zenith)
If an angle is provided,
Flux is in units of /cm2/GeV/s/sr (incoming energy, bin width!)
If no angle is provided, the zenith angle is integrated over, and the
Flux is in units of /cm2/GeV/s
returns DOUBLE (0.0 if beyond scope of data)
'''
if not (key in self._fluxes):
raise ValueError("Bad key {}".format(key))
if not (isinstance(energy, float) or isinstance(energy, int)):
raise TypeError("Expected {}, not {}".format(float, type(energy)))
if angle is not None:
integrate = False
if not (isinstance(angle, float) or isinstance(angle, int)):
raise TypeError("Expected {}, not {}".format(float, type(angle)))
else:
integrate = True
raise NotImplementedError("This shouldn't be used. It is inaccurate")
if use_overflow:
raise NotImplementedError()
# check if it's outside the extents
if (energy < self._energies[0] and self.growing) or (energy > self._energies[0] and not self.growing):
# this overflow thing wasn't implemented right, so I'm disabling it for now .
if False: # use_overflow:
return(self._energies[0])
else:
return(0)
if (energy > self._energies[-1] and self.growing) or (energy < self._energies[-1] and not self.growing):
if False: #use_overflow:
return(self._energies[n_energies - 1])
else:
return(0)
if not integrate:
if (angle < self._angles[0] and self.ang_grow) or (angle > self._angles[0] and not self.growing):
return(0.)
if (angle > self._angles[-1] and self.ang_grow) or (angle < self._angles[-1] and not self.growing):
return(0.)
lower_boundary, upper_boundary = get_loc(energy, self._energies)
if not integrate:
ang_lower, ang_upper = get_loc(angle, self._angles)
# sanity check...
# essentially makes sure that the energies are monotonically increasing
if not ((self._energies[lower_boundary] <= energy) and (self._energies[upper_boundary] >= energy)):
print("energy: {}".format(energy))
print("lower bound: {}".format(self._energies[lower_boundary]))
print("upper bound: {}".format(self._energies[upper_boundary]))
print("indices: {}, {}".format(lower_boundary, upper_boundary))
raise Exception()
# if we're integrating, we get the flux at all the angles and scale it by width, otherwise it's bilinear interpolation time!
if integrate:
flux_value = 0.0
for angle_bin in range(len(self._angles)):
# linear interpolation
y2 = self._fluxes[key][upper_boundary][angle_bin]
y1 = self._fluxes[key][lower_boundary][angle_bin]
x2 = self._energies[upper_boundary]
x1 = self._energies[lower_boundary]
slope = (y2-y1)/(x2-x1)
# TODO: this is missing the factor of sin(theta) in the jacobian
flux_value += (energy*slope + y2 -x2*slope)*self._ang_width[angle_bin]*2*np.pi
return(flux_value)
else:
#bilinear_interp(p0, p1, p2, q11, q12, q21, q22):
p0 = (energy, angle)
p1 = (self._energies[lower_boundary], self._angles[ang_lower])
p2 = (self._energies[upper_boundary], self._angles[ang_upper])
q11 = self._fluxes[key][lower_boundary][ang_lower]
q21 = self._fluxes[key][upper_boundary][ang_lower]
q12 = self._fluxes[key][lower_boundary][ang_upper]
q22 = self._fluxes[key][upper_boundary][ang_upper]
value =bilinear_interp(p0,p1,p2,q11,q12,q21,q22)
#if value<0:
# raise ValueError("Somehow have negative flux value {}".format(value))
return value
def get_err(self, energy, key, angle):
"""
This assumes a Poisson distribution defines distributino for the number of observed events in each bin. So, the uncertainty is just the square root of the quantity
"""
return(sqrt(self.get_flux(energy=energy, key=key, angle=angle)))
class IllegalArguments(ValueError):
"""
Just using this to make it clear what the issue is!
"""
pass
class bhist:
"""
It's a 1D or 2D histogram! BHist is for "Ben Hist" or "Binned Hist" depending on who's asking.
I made this so I could have a binned histogram that could be used for adding more stuff at arbitrary places according to some "edges" it has. The object would handle figuring out which of its bins would hold the stuff.
Also made with the potential to store integers, floats, or whatever can be added together and has both an additive rule and some kind of identity element correlated with the default constructor.
If a non-dtype entry is given, it will be explicitly cast to the dtype.
"""
def __init__(self,edges, dtype=float):
"""
Arg 'edges' should be a tuple of length 1 or 2. Length 1 for 1D hist, and length 2 for 2D hist.
These edges represent the bin edges.
The type-checking could use a bit of work... Right now for 1D histograms you need to give it a length-1 list.
"""
if not (isinstance(edges, list) or isinstance(edges, tuple) or isinstance(edges, np.ndarray)):
raise TypeError("Arg 'edges' must be {}, got {}".format(list, type(edges)))
for entry in edges:
if not (isinstance(entry, list) or isinstance(entry, tuple) or isinstance(entry, np.ndarray)):
raise TypeError("Each entry in 'edges' should be list-like, found {}".format(type(entry)))
if len(entry)<2:
raise ValueError("Entries in 'edges' must be at least length 2, got {}".format(len(entry)))
self._edges = np.sort(edges) # each will now be increasing. I think this is Quicksort?
self._dtype = dtype
# Ostensibly you can bin strings... not sure why you would, but you could!
try:
x = dtype() + dtype()
except Exception:
raise TypeError("It appears impossible to add {} together.".format(dtype))
# build the function needed to register additions to the histograms.
dims = tuple([len(self._edges[i])-1 for i in range(len(self._edges))])
self._fill = np.zeros( shape=dims, dtype=self._dtype )
def register( amt, *args, density=True):
"""
Tries to bin some data passed to the bhist. Arbitrarily dimensioned cause I was moving from 2D-3D and this seemed like a good opportunity
amt is the amount to add
*args specifies the coordinates in our binned space
density specifies whether or not we want to divide out bin widths to make amt a density
As a note, the density thing is implemented as-is since when using this, you won't know how wide the target bin is when you call this register function. You also can't just divide it out afterwards. This needs to happen between getting the bin location and adding it to the bin!
"""
if not len(args)==len(self._edges):
raise ValueError("Wrong number of args to register! Got {}, not {}".format(len(args), len(self._edges)))
if not isinstance(amt, self._dtype):
try:
amount = self._dtype(amt)
except TypeError:
raise TypeError("Expected {}, got {}. Tried casting to {}, but failed.".format(self._dtype, type(amt), self._dtype))
else:
amount = amt
bin_loc = tuple([get_loc( args[i], self._edges[i])[0] for i in range(len(args))]) # get the bin for each dimension
# Verifies that nothing in the list is None-type
if all([x is not None for x in bin_loc]):
# itemset works like ( *bins, amount )
if density:
widths = self.widths
for dim in range(len(self._edges)):
amount/=widths[dim][bin_loc[dim]]
try:
self._fill.itemset(bin_loc, self._fill.item(tuple(bin_loc))+amount)
except TypeError:
print("bin_loc: {}".format(bin_loc))
print("amount: {}".format(amount))
print("previous: {}".format(self._fill.item(tuple(bin_loc))))
sys.exit()
return tuple(bin_loc)
self.register = register
# some access properties. Note these aren't function calls. They are accessed like "object.centers"
@property
def centers(self):
complete = [ [0.5*(subedge[i+1]+subedge[i]) for i in range(len(subedge)-1)] for subedge in self._edges]
return(complete[0] if len(self._edges)==1 else complete)
@property
def edges(self):
complete = [[value for value in subedge] for subedge in self._edges]
return(complete[0] if len(self._edges)==1 else complete)
@property
def widths(self):
complete = [[abs(subedges[i+1]-subedges[i]) for i in range(len(subedges)-1)] for subedges in self._edges]
return(complete[0] if len(self._edges)==1 else complete)
@property
def fill(self):
return(self._fill)
def extract_from_edges(edges):
"""
Takes list-like representing the edges of a bhist, returns the widths and centers
"""
if not (isinstance(edges, list) or isinstance(edges,np.ndarray) or isinstance(edges, tuple)):
raise TypeError("Can't make bhist from {}, need {}".format(type(edges, list)))
temp = bhist([edges])
centers = np.array(temp.centers)
widths = np.array(temp.widths)
return( centers, widths )
def get_nearest_entry_to( item, array_like):
"""
This function takes a quantity "item" and a list/array-like item "array_like"
It returns the index of the entry in "array_like" that is closest to "item"
Args:
item - int
array_like - list (or tuple, np.ndarray)
Returns:
index - int. =>0, <len(array_like)
"""
# verify datatypes
if not (isinstance(item,int) or isinstance(item, float)):
raise TypeError("Expected number-like for arg 'item', got {}".format(type(item)))
if not (isinstance(array_like, list) or isinstance(array_like, tuple) or isinstance(array_like,np.ndarray)):
raise TypeError("Expected an index-able for arg 'array_like', got {}".format(type(array_like)))
min_bin = None
mindist = None
# we can make no assumptions about the increasing/decreasing nature of 'array_like'
# so we scan over it
for index in range(len(array_like)):
if min_bin is None:
min_bin = index
mindist = abs(array_like[index] - item)
else:
new_distance = abs(array_like[index]-item)
if new_distance < mindist:
mindist = new_distance
min_bin = index
return(min_bin)
def get_width( which_list ):
"""
Takes a list 'which_list' of floats of length N, considered the centers of some bins
Returns a length N numpy array of floats for the widths of the bins these centers correspond to