-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.py
335 lines (268 loc) · 9.98 KB
/
variables.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
from numba import njit
import torch
import numpy as np
from numba import njit, prange
import math
from typing import Tuple
'''
-----------------------------------------------------
Calculations for plotting - operating on numpy arrays
-----------------------------------------------------
'''
@njit(parallel=True)
def calculate_edep_np(max_events: int, layers: np.ndarray) -> np.ndarray:
events_energy_deposit = np.zeros(max_events)
for event in prange(max_events):
event_layers = layers[event, :, :, :]
events_energy_deposit[event] = np.sum(event_layers)
return events_energy_deposit
@njit(parallel=True)
def calculate_non_zero_np(max_events: int, layers: np.ndarray) -> np.ndarray:
nonzero_portions = np.zeros(max_events)
total_cells = 30*30*30
for event in prange(max_events):
event_layers = layers[event, :, :, :]
nonzero_elements = len(np.nonzero(event_layers)[0])
nonzero_portion = nonzero_elements/total_cells
nonzero_portions[event] = nonzero_portion
return nonzero_portions
@njit(parallel=True)
def calculate_longitudinal_centroid_np(max_events: int, layers: np.ndarray) -> np.ndarray:
event_lcentroid = np.zeros(max_events)
# loop over events
for event in prange(max_events):
event_layers = layers[event, :, :, :]
energies_per_layer = np.zeros(30)
ewgt_idx_per_layer = np.zeros(30)
# loop over y-layers
for i in prange(30):
ylayer = event_layers[i, :, :]
layer_energy = np.sum(ylayer)
layer_idx = i+1
ewgt_idx = layer_idx*layer_energy
ewgt_idx_per_layer[i] = ewgt_idx
energies_per_layer[i] = layer_energy
event_energy = np.sum(energies_per_layer)
event_lcentroid[event] = np.sum(ewgt_idx_per_layer / event_energy)
return event_lcentroid
@njit(parallel=True)
def calculate_com(array: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
w_y = w_x = w_z = np.zeros(30*30*30)
norm = 0
# loop over cells
i = 0
for y in prange(30):
for x in prange(30):
for z in prange(30):
edep = array[y, x, z]
w_y[i]= edep*y
w_x[i]= edep*x
w_z[i]= edep*z
norm += edep
i += 1
w_y = w_y/norm
w_x = w_x/norm
w_z = w_z/norm
y_com = np.sum(w_y)
x_com = np.sum(w_x)
z_com = np.sum(w_z)
return y_com, x_com, z_com
@njit(parallel=True)
def calculate_r2_np(max_events: int, layers: np.ndarray) -> np.ndarray:
event_r2 = np.zeros(max_events)
# loop over events
for event in prange(max_events):
event_layers = layers[event, :, :, :]
# caclulate center of mass
y_com, x_com, z_com = calculate_com(event_layers)
event_edep = 0
event_ewgt_r2distance = 0
# loop over cells
for y in prange(30):
for x in prange(30):
for z in prange(30):
edep = event_layers[y,x,z]
y_cell = y
x_cell = x
z_cell = z
r_distance = math.sqrt((x_cell**2 + z_cell**2)) - math.sqrt((x_com**2 + z_com**2))
ewgt_r2distance = edep*(r_distance**2)
event_edep += edep
event_ewgt_r2distance += ewgt_r2distance
event_r2[event] = (event_ewgt_r2distance / event_edep)
return event_r2
@njit(parallel=True)
def calculate_Rz_np(max_events: int, layers: np.ndarray) -> np.ndarray:
event_Rz = np.zeros(max_events)
# loop over events
for event in prange(max_events):
edep = np.sum(layers[event, :, :, :])
Rz = np.zeros(30)
# loop over layers
for i in prange(30):
layer = layers[event, i, :, :]
layer_edep = np.sum(layer)
num = np.sum(layer[18:27, 13:17])
denom = np.sum(layer[18:27, 11:19])
Rz_ewgted = ((num/denom)*layer_edep) if denom else 0
Rz[i] = Rz_ewgted
# get the sum of Rz and normalize
event_Rz[event] = np.sum(Rz)/edep
return event_Rz
@njit(parallel=True)
def calculate_Rx_np(max_events: int, layers: np.ndarray) -> np.ndarray:
event_Rx = np.zeros(max_events)
# loop over events
for event in prange(max_events):
edep = np.sum(layers[event, :, :, :])
Rx = np.zeros(30)
# loop over layers
for i in range(30):
layer = layers[event, i, :, :]
layer_edep = np.sum(layer)
num = np.sum(layer[20:25, 12:18])
denom = np.sum(layer[17:28, 12:18])
Rx_ewgted = ((num/denom)*layer_edep) if denom else 0
Rx[i] = Rx_ewgted
# get the sum of Rx and normalize
event_Rx[event] = np.sum(Rx)/edep
return event_Rx
@njit(parallel=True)
def calculate_lambda2_np(max_events: int, layers: np.ndarray) -> np.ndarray:
event_lambda2 = np.zeros(max_events)
# loop over events
for event_num in prange(max_events):
event_layers = layers[event_num, :, :, :]
# caclulate center of mass
y_com, x_com, z_com = calculate_com(event_layers)
event_edep = 0
event_ewgt_l2distance = 0
# loop over cells
for y in prange(30):
for x in prange(30):
for z in prange(30):
edep = event_layers[y,x,z]
y_cell = y
x_cell = x
z_cell = z
l_distance = y_cell - y_com
ewgt_l2distance = edep*(l_distance**2)
event_edep += edep
event_ewgt_l2distance += ewgt_l2distance
event_lambda2[event_num] = event_ewgt_l2distance / event_edep
return event_lambda2
'''
------------------------------------------------------
Calculations for training - operating on torch tensors
------------------------------------------------------
in general torch layers shape would be: B x C x H x W x D
'''
def calculate_event_energy(layers: torch.FloatTensor) -> torch.FloatTensor:
'''
Event energy deposit
'''
return torch.sum( layers, dim=tuple(d for d in range(1,len(layers.size()))) )
def calculate_non_zero(layers: torch.FloatTensor) -> torch.FloatTensor:
'''
Event sparsity, i.e non-zero portion
'''
sparcities = torch.zeros(layers.size()[0])
for batch_idx, ilayer in enumerate(layers):
sparcities[batch_idx] = torch.nonzero(ilayer).size()[0] / (30*30*30)
return sparcities
global_features_funcs = {'edep': calculate_event_energy,
'sparsity': calculate_non_zero}
def calculate_longitudinal_centroid(layers):
'''
Event longitudinal centrod in the sense of how shower evolves, i.e. y-axix or array rows (axis=0)
WIP!
'''
tot_events = len(layers)
event_lcentroid = np.zeros(tot_events)
# loop over events
for event_num in range(tot_events):
event_layers = layers[event_num, :, :, :]
energies_per_layer = np.zeros(30)
ewgt_idx_per_layer = np.zeros(30)
# loop over y-layers
for i in range(30):
ylayer = event_layers[i, :, :]
layer_energy = np.sum(ylayer)
layer_idx = i+1
ewgt_idx = layer_idx*layer_energy
ewgt_idx_per_layer[i] = ewgt_idx
energies_per_layer[i] = layer_energy
event_energy = np.sum(energies_per_layer)
event_lcentroid[event_num] = np.sum(ewgt_idx_per_layer / event_energy)
return event_lcentroid
@njit(parallel=True)
def calculate_r2(layers):
'''
shower shape (r2)
WIP!
'''
tot_events = layers.shape[0]
event_r2 = np.zeros(tot_events)
# loop over events
for event_num in prange(tot_events):
event_layers = layers[event_num, :, :, :]
# caclulate center of mass
y_com, x_com, z_com = calculate_com(event_layers)
event_edep = 0
event_ewgt_r2distance = 0
# loop over cells
for y in prange(30):
for x in prange(30):
for z in prange(30):
edep = event_layers[y,x,z]
y_cell = y
x_cell = x
z_cell = z
r_distance = math.sqrt((x_cell**2 + z_cell**2)) - math.sqrt((x_com**2 + z_com**2))
ewgt_r2distance = edep*(r_distance**2)
event_edep += edep
event_ewgt_r2distance += ewgt_r2distance
event_r2[event_num] = event_ewgt_r2distance / event_edep
return event_r2
def calculate_Rz(layers):
'''
Rz - layer cell's ratio
WIP!
'''
event_Rz = []
# loop over variant events
for event in range(layers.shape[0]):
edep = np.sum(layers[event, :, :, :])
Rz = []
# loop over layers
for i in range(30):
layer = layers[event, i, :, :]
layer_edep = np.sum(layer)
num = np.sum(layer[18:27, 13:17])
denom = np.sum(layer[18:27, 11:19])
Rz_ewgted = ((num/denom)*layer_edep) if denom else 0
Rz.append(Rz_ewgted)
# get the sum of Rz and normalize
event_Rz.append(np.sum(Rz)/edep)
return np.array(event_Rz)
def calculate_Rx(layers):
'''
Rx - layer cell's ratio
WIP!
'''
event_Rx = []
# loop over variant events
for event in range(layers.shape[0]):
edep = np.sum(layers[event, :, :, :])
Rx = []
# loop over layers
for i in range(30):
layer = layers[event, i, :, :]
layer_edep = np.sum(layer)
num = np.sum(layer[20:25, 12:18])
denom = np.sum(layer[17:28, 12:18])
Rx_ewgted = ((num/denom)*layer_edep) if denom else 0
Rx.append(Rx_ewgted)
# get the sum of Rx and normalize
event_Rx.append(np.sum(Rx)/edep)
return np.array(event_Rx)