-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_elves_occnn_wholedir.py
executable file
·245 lines (206 loc) · 5.8 KB
/
find_elves_occnn_wholedir.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
#!/usr/bin/python
import numpy as np
import torch
from etoshelpers import *
import pickle
import uproot
import glob
import sys
import os
from etoshelpers import *
import ROOT
#mymlp = CNN()
#xp=cp
nl = 128
ncl = 128*2
ncl=16
norm = 1
anscombe=True
freemantukey=False
median=False
def main():
file_list = sorted(glob.glob("CPU*root"))
# print(file_list)
# exit()
# Init the neural network
from train_elves_occnn import CNN
elves_nn = CNN(nl, ncl)
if len(sys.argv)>1:
a = torch.load(sys.argv[1])
elves_nn.load_state_dict(torch.load(sys.argv[1]))
else:
elves_nn.load_state_dict(torch.load('/home/lewhoo/workspace/minieuso_elves_cnn/pytorch/elves_samples.pk/res_batchsize32/curve_snapshot_epoch90.model'))
elves_nn.eval()
device="cuda"
elves_nn.to(device)
#a = serializers.load_npz('81_100_0vs100_6cnn_largegap_single8bank/curve.model', elves_nn)
# a = serializers.load_npz('occnn_bg99.86_elf1/curve.model', elves_nn)
# a = serializers.load_npz('occnn_bg99.86_elf1/curve.model', elves_nn)
# a = serializers.load_npz('/home/lewhoo/workspace/minieuso_elves_cnn/occnn_bg99.84_elf1_better/curve.model', elves_nn)
#a = serializers.load_npz('curve.model', elves_nn)
# device = chainer.get_device(0)
# elves_nn.to_device(0)
# device.use()
# elves_nn.centre = chainer.Variable(cp.loadtxt("cur_centre.txt"))
flat = None
if os.path.exists("pdm.npy"):
flat = np.load("pdm.npy")
flat[flat<0.005]=1
flat = np.fliplr(np.rot90(flat, 3))
packets_count = 0
for fn in file_list:
# print(fn)
# Load the data from file to memory
try:
t = uproot.open(f"{fn}:tevent")
except:
print(f"FAILED TO OPEN {fn}")
continue
packets_count+=t.num_entries/128
pc = t["photon_count_data"].array().to_numpy().astype(np.float32)[:,0,0]
if flat is not None:
pc/=flat
pc = torch.as_tensor(pc).to(device)
# print(pc.shape)
# Reshape to have every packet separately and add dimension for channel
pc = pc.reshape((pc.shape[0]//128,1,128,48,48))
# No events in the file, continue to the next
if pc.shape[0]==0:
print(f"No events in {fn}")
continue
# print(fn, pc.shape)
# Normalize each packet
# mean = cp.mean(pc, axis=(1,2,3,4)).reshape(pc.shape[0], 1, 1, 1, 1)
# std = cp.std(pc, axis=(1,2,3,4)).reshape(pc.shape[0], 1, 1, 1, 1)
# print(mean.shape, std.shape)
# pc = (pc-mean)/std
#pc[pc>255]=255
for i,el in enumerate(pc):
if norm==1:
el[el>1000]=1000
el[el<0]=0
if anscombe: el = 2*torch.sqrt(el+3/8)
elif freemantukey: el = torch.sqrt(el+1)+torch.sqrt(el)
pmeans = torch.mean(el, axis=(0,1))
pstds = torch.std(el, axis=(0,1))
pstds[pstds==0]=1
#print(pmeans.shape)
el1 = (el-pmeans)/pstds
elif norm==2:
el[el>1000]=1000
el[el<0]=0
mean = torch.mean(el)
std = torch.std(el)
el1 = (el-mean)/std
el1/=torch.max(el1)
elif norm==3:
el[el>1000]=1000
el[el<0]=0
mean = torch.mean(el)
#std = torch.std(el)
el1 = (el-mean)
el1/=torch.max(el1)
elif norm==4:
el[el>1000]=1000
el[el<0]=0
mx = torch.max(el)
el1=el-mx/2
el1/=mx/2
elif norm==5:
el[el>1000]=1000
el[el<0]=0
mx = torch.max(el)
el1=el/mx
elif norm==6:
el[el>1000]=1000
el[el<0]=0
if anscombe: el = 2*torch.sqrt(el+3/8)
elif freemantukey: el = torch.sqrt(el+1)+torch.sqrt(el)
pmeans = torch.mean(el, axis=(0,1))
pstds = torch.std(el, axis=(0,1))
pstds[pstds==0]=1
#print(pmeans.shape)
el1 = (el-pmeans)/pstds
el1 /= torch.max(el1)
elif norm==7:
el[el>1000]=1000
el[el<0]=0
if median: pmeans = torch.as_tensor(np.median(el.to("cpu").data, axis=(0,1))).to("cuda")
else: pmeans = torch.mean(el, axis=(0,1))
pstds = torch.std(el, axis=(0,1))
pstds[pstds==0]=1
#print(pmeans.shape)
el1 = (el-pmeans)#/pstds
el1 /= torch.max(el1)
elif norm==8:
el[el>1000]=1000
el[el<0]=0
pmeans = torch.mean(el, axis=(0,1))
pstds = torch.std(el, axis=(0,1))
pstds[pstds==0]=1
#print(pmeans.shape)
el1 = (el-pmeans)#/pstds
el1 /= 1000
elif norm==9:
el[el>1000]=1000
el[el<0]=0
pmeans = torch.mean(el, axis=(0,1))
pstds = torch.std(el, axis=(0,1))
pstds[pstds==0]=1
#print(pmeans.shape)
el1 = (el-pmeans)#/pstds
elif norm==10:
el[el>1000]=1000
el[el<0]=0
pmeans = torch.as_tensor(np.median(el.to("cpu").data, axis=(0,1))).to("cuda")
pstds = torch.std(el, axis=(0,1))
pstds[pstds==0]=1
#print(pmeans.shape)
el1 = (el-pmeans)/pstds
el1 /= torch.max(el1)
pc[i]=el1
# print(pc.shape)
# Loop through packet batches
y = []
dists = []
# print(pc.shape[0]//10+1)
for batch in range(pc.shape[0]//10+1):
# print("in loop")
if batch*10>pc.shape[0]-1:
# print("breaking")
break
# Feed the NN model
labels = torch.ones((batch,1))
# print(elves_nn.predict(pc[batch*10:(batch+1)*10], labels))
#exit()
with torch.no_grad():
res, dist = elves_nn.predict_dist(pc[batch*10:(batch+1)*10], labels)
y.append(res)
dists.append(dist)
# print(batch, y, batch*10, (batch+1)*10, pc[batch*10:(batch+1)*10].shape)
#print(y)
y = torch.hstack(y)
# print(y)
dists = torch.hstack(dists)
if len(sys.argv)>2:
out_name = sys.argv[2]
else:
out_name = "tle_occnn_results.txt"
# If there are some events, open the file with ROOT to filter out the sunlight - do not know how to access this leaf with uproot :(
if len(y)>0:
rf = ROOT.TFile(fn, "read")
rt = rf.Get("tevent")
with open(out_name, "a") as fl:
for i,el in enumerate(y):
if el:
#print(fn, i*128, dists[i].item())
add = True
try:
rt.GetEntry(i*128)
if rt.Sun_from_ISS_alt>=-20.8: add=False
except:
pass
if add: print(fn, i*128, dists[i].item(), file=fl)
exit()
if __name__ == '__main__':
a = main()