-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmax_optimize_two.py
179 lines (127 loc) · 5.09 KB
/
lmax_optimize_two.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
import argparse
import pathlib
import sys
import yaml
import numpy as np
import best
import itertools
my_parser = argparse.ArgumentParser(description = 'Configuration file.')
my_parser.add_argument('Configuration',
metavar='configuration file',
type = str,
help = 'the path to configuration file')
my_parser.add_argument('fb',
metavar='bias enhancement',
type = float)
my_parser.add_argument('gtol',
metavar='gtol for diff-ev',
type = int)
my_parser.add_argument('noisebiasconstr',
metavar='if noise=bias constraint',
type = int,
help = '0 for False, 1 for True')
my_parser.add_argument('invvariance',
metavar='inverse variance weights',
type = int,
help = '0 for False, 1 for True')
my_parser.add_argument('h',
metavar='huok',
type = int)
my_parser.add_argument('s',
metavar='shear',
type = int)
my_parser.add_argument('b',
metavar='biashard',
type = int)
args = my_parser.parse_args()
values_file = args.Configuration
fb = args.fb
gtol = args.gtol
noisebiasconstr = bool(args.noisebiasconstr)
invvariance = bool(args.invvariance)
h = args.h
s = args.s
b = args.b
if not pathlib.Path(values_file).exists():
print('The file specified does not exist')
sys.exit()
with open(values_file, 'r') as stream:
data = yaml.safe_load(stream)
plots_directory = data['plotsdirectory']
analysis_directory = data['analysisdirectory']
results_directory = data['resultsdirectory']
PP = pathlib.Path(analysis_directory)
Pplots = pathlib.Path(plots_directory)
fgnamefiles = data['fgnamefiles']
estimators_dictionary = data['estimators']
estimators = list(estimators_dictionary.keys())
lista_lmaxes = []
names = {}
for e in estimators:
elemento = estimators_dictionary[e]
names[e] = elemento['direc_name']
lmaxes_configs = [(h, s, b)]
#CHOOSE nu
nu = estimators_dictionary[estimators[0]]['nu']
del estimators_dictionary
noisetag = data['noisekey']
trispectrumtag = data['trispectrumkey']
primarytag = data['primarykey']
secondarytag = data['secondarykey']
primarycrosstag = data['primarycrosskey']
kkkey = data['kkkey']
kgkey = data['kgkey']
ggkey = data['ggkey']
ellskey = data['ellskey']
thetakey = data['thetakey']
thetacrosskey = data['thetacrosskey']
totalabsbiaskey = data['totalabsbiaskey']
totalbiaskey = data['totalbiaskey']
sumalltotalabsbiaskey = data['sumalltotalabsbiaskey']
sumalltotalbiaskey = data['sumalltotalbiaskey']
sumallcrosstotalabsbiaskey = data['sumallcrosstotalabsbiaskey']
sumallcrosstotalbiaskey = data['sumallcrosstotalbiaskey']
lmin_sel, lmax_sel = data['lmin_sel'], data['lmax_sel']
optversion = data['optversion']
if noisebiasconstr:
n_equals_b_dir = 'noiseequalsbias'
else:
n_equals_b_dir = ''
if invvariance:
inv_variance_dir = 'inversevariance'
else:
inv_variance_dir = ''
bias_source = data['optimisation']['bias_source']
for fgnamefile in [fgnamefiles[0]]:
for lmaxes in lmaxes_configs:
lmaxes_dict = {}
lmax_directory = ''
for e_index, e in enumerate(estimators):
l = lmaxes[e_index]
lmaxes_dict[e] = l
lmax_directory += f'{names[e]}{l}'
print('Doing for', lmax_directory)
P = PP/lmax_directory
getoutname = lambda key: f'{key}_{nu}.npy'
noises = np.load(P/getoutname(noisetag))
getoutname2 = lambda key: f'{key}_total_{nu}.npy'
if bias_source == 'total':
biases = np.load(P/'total'/getoutname2(totalbiaskey)) #getoutname('sum_all_totalbias'))
biasescross = np.load(P/'total'/getoutname2(primarycrosstag)) #/getoutname('sum_all_crosstotalbias'))
elif bias_source == 'sum_bias':
biases = np.load(P/getoutname(totalbiaskey))
biasescross = np.load(P/getoutname(sum_all_crosstotalbias))
elif bias_source == 'sum_abs_bias':
biases = np.load(P/getoutname(sumalltotalabsbiaskey))
biasescross = np.load(P/getoutname(sumallcrosstotalabsbiaskey))
kg = np.load(P/getoutname(kgkey))
kk = np.load(P/getoutname(kkkey))
gg = np.load(P/getoutname(ggkey))
ells = np.load(P/getoutname(ellskey))
theta = np.load(P/getoutname(thetakey))
thetacross = np.load(P/getoutname(thetacrosskey))
estimators_to_ignore = 'hu_ok'
Optimizerkk = best.Opt(estimators, lmin_sel, lmax_sel, ells, kk, theta, biases, noises, estimators_to_ignore)
result = Optimizerkk.optimize(optversion, method = 'diff-ev', gtol = gtol, bounds = [0., 1.], noisebiasconstr = noisebiasconstr, fb = fb, inv_variance = invvariance)
result.save_all(pathlib.Path('twocases')/lmax_directory, f'auto_fb_{fb}')
result.save(Optimizerkk.biases_selected, pathlib.Path('twocases')/lmax_directory, f'biases')