-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalibrator.py
More file actions
367 lines (259 loc) · 10.1 KB
/
Calibrator.py
File metadata and controls
367 lines (259 loc) · 10.1 KB
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
###################################################################################################
#
# Calibrator.py
#
# Copyright (C) by Andreas Zoglauer & Shreya Sareen
# All rights reserved.
#
###################################################################################################
###################################################################################################
#from mpl_toolkits.mplot3d import Axes3D
#import matplotlib.pyplot as plt
import random
import signal
import sys
import time
import math
import csv
import os
import argparse
from datetime import datetime
from functools import reduce
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
print("\nA machine-learning based COSI calibrator")
print("========================+++++===========\n")
###################################################################################################
# Step 1: Input parameters
###################################################################################################
# Default parameters
NumberOfComptonEvents = 10000
OutputDirectory = "Output"
# Parse command line:
print("\nParsing the command line (if there is any)\n")
parser = argparse.ArgumentParser(description='Perform training and/or testing for gamma-ray burst localization')
parser.add_argument('-m', '--mode', default='toymodel', help='Choose an input data more: toymodel or simulations')
parser.add_argument('-t', '--toymodeloptions', default='10000:510.99:511.00', help='The toy-model options: source_events:energy_min:energy_max"')
parser.add_argument('-s', '--simulationoptions', default='', help='')
parser.add_argument('-b', '--batchsize', default='256', help='The number of GRBs in one training batch (default: 256 corresponsing to 5 degree grid resolution (64 for 3 degrees))')
parser.add_argument('-o', '--outputdirectory', default='Output', help='Name of the output directory. If it exists, the current data and time will be appended.')
parser
args = parser.parse_args()
Mode = (args.mode).lower()
if Mode != 'toymodel' and Mode != 'simulation':
print("Error: The mode must be either \'toymodel\' or \'simulation\'")
sys.exit(0)
if Mode == 'toymodel':
print("CMD-Line: Using toy model")
ToyModelOptions = args.toymodeloptions.split(":")
if len(ToyModelOptions) != 3:
print("Error: You need to give 3 toy model options. You gave {}. Options: {}".format(len(ToyModelOptions), ToyModelOptions))
sys.exit(0)
NumberOfComptonEvents = int(ToyModelOptions[0])
if NumberOfComptonEvents <= 10:
print("Error: You need at least 10 source events and not {}".format(NumberOfComptonEvents))
sys.exit(0)
print("CMD-Line: Toy model: Using {} source events per GRB".format(NumberOfComptonEvents))
MinimumEnergy = float(ToyModelOptions[1])
if MinimumEnergy < 0:
print("Error: You need a non-negative number for the energy, and not {}".format(MinimumEnergy))
sys.exit(0)
print("CMD-Line: Toy model: Using {} keV as minimum energy".format(MinimumEnergy))
MaximumEnergy = float(ToyModelOptions[2])
if MaximumEnergy < 0:
print("Error: You need a non-negative number for the energy, and not {}".format(MinimumEnergy))
sys.exit(0)
print("CMD-Line: Toy model: Using {} keV as minimum energy".format(MinimumEnergy))
'''
NumberOfTrainingBatches = int(ToyModelOptions[3])
if NumberOfTrainingBatches < 1:
print("Error: You need a positive number for the number of traing batches and not {}".format(NumberOfTrainingBatches))
sys.exit(0)
print("CMD-Line: Toy model: Using {} training batches".format(NumberOfTrainingBatches))
NumberOfTestingBatches = int(ToyModelOptions[4])
if NumberOfTestingBatches < 1:
print("Error: You need a positive number for the number of testing batches and not {}".format(NumberOfTestingBatches))
sys.exit(0)
print("CMD-Line: Toy model: Using {} testing batches".format(NumberOfTestingBatches))
'''
elif Mode == 'simulation':
print("Error: The simulation mode has not yet implemented")
sys.exit(0)
MaxBatchSize = int(args.batchsize)
if MaxBatchSize < 1 or MaxBatchSize > 1024:
print("Error: The batch size must be between 1 && 1024")
sys.exit(0)
print("CMD-Line: Using {} as batch size".format(MaxBatchSize))
OutputDirectory = args.outputdirectory
# TODO: Add checks
print("CMD-Line: Using \"{}\" as output directory".format(OutputDirectory))
print("\n\n")
# Determine derived parameters
NumberOfTrainingEvents = int(0.8*NumberOfComptonEvents)
NumberOfTestingEvents = NumberOfComptonEvents - NumberOfTrainingEvents
'''
if os.path.exists(OutputDirectory):
Now = datetime.now()
OutputDirectory += Now.strftime("_%Y%m%d_%H%M%S")
os.makedirs(OutputDirectory)
'''
###################################################################################################
# Step 2: Global functions
###################################################################################################
# Take care of Ctrl-C
Interrupted = False
NInterrupts = 0
def signal_handler(signal, frame):
global Interrupted
Interrupted = True
global NInterrupts
NInterrupts += 1
if NInterrupts >= 2:
print("Aborting!")
sys.exit(0)
print("You pressed Ctrl+C - waiting for graceful abort, or press Ctrl-C again, for quick exit.")
signal.signal(signal.SIGINT, signal_handler)
# Everything ROOT related can only be loaded here otherwise it interferes with the argparse
#from CalibrationCreatorToyModel import CalibrationCreatorToyModel
# Load MEGAlib into ROOT so that it is usable
import ROOT as M
M.gSystem.Load("$(MEGALIB)/lib/libMEGAlib.so")
M.PyConfig.IgnoreCommandLineOptions = True
###################################################################################################
# Step 3: Create some training, test & verification data sets
###################################################################################################
print("Info: Creating {} Compton events".format(NumberOfComptonEvents))
from CalibrationData import CalibrationData
def generateOneDataSet(_):
DataSet = CalibrationData()
DataSet.create()
return DataSet
# Create data sets
TimerCreation = time.time()
TrainingDataSets = []
for i in range(NumberOfTrainingEvents):
result = generateOneDataSet(i)
TrainingDataSets.append(generateOneDataSet(i))
print("Info: Created {:,} training data sets. ".format(NumberOfTrainingEvents))
TestingDataSets = []
for i in range(NumberOfTestingEvents):
result = generateOneDataSet(i)
TestingDataSets.append(generateOneDataSet(i))
print("Info: Created {:,} testing data sets. ".format(NumberOfTestingEvents))
TimeCreation = time.time() - TimerCreation
print("Info: Total time to create data sets: {:.1f} seconds (= {:,.0f} events/second)".format(TimeCreation, (NumberOfTrainingEvents + NumberOfTestingEvents) / TimeCreation))
###################################################################################################
# Step 4: Setting up the neural network
###################################################################################################
def dataset_to_tensors(datasets):
X = []
y = []
for data in datasets:
sh1 = data.StripHits[0]
sh2 = data.StripHits[1]
features = [
sh1.DetectorID,
sh1.StripID,
sh1.IsHV,
sh1.ADC,
sh1.TAC,
sh2.DetectorID,
sh2.StripID,
sh2.IsHV,
sh2.ADC,
sh2.TAC
]
hit = data.Hits[0]
target = [
hit.X,
hit.Y,
hit.Z,
hit.Energy
]
X.append(features)
y.append(target)
X = torch.tensor(X, dtype=torch.float32)
y = torch.tensor(y, dtype=torch.float32)
return X, y
# Neural neutral
# -------------------------------
# Converting datasets to tensors
# -------------------------------
train_X, train_y = dataset_to_tensors(TrainingDataSets)
test_X, test_y = dataset_to_tensors(TestingDataSets)
# -------------------------------
# Normalize inputs
# -------------------------------
X_mean = train_X.mean(0)
X_std = train_X.std(0) + 1e-8
train_X = (train_X - X_mean) / X_std
test_X = (test_X - X_mean) / X_std
# -------------------------------
# Normalize outputs
# -------------------------------
y_mean = train_y.mean(0)
y_std = train_y.std(0) + 1e-8
train_y_norm = (train_y - y_mean) / y_std
test_y_norm = (test_y - y_mean) / y_std
# -------------------------------
# Linear Model
# -------------------------------
model = nn.Sequential(
nn.Linear(train_X.shape[1], 64),
nn.ReLU(),
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, 4)
)
# -------------------------------
# Loss + optimizer
# -------------------------------
loss_fn = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
# -------------------------------
# Training
# -------------------------------
for epoch in range(2000):
pred = model(train_X)
loss = loss_fn(pred, train_y_norm)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# -------------------------------
# Evaluate
# -------------------------------
with torch.no_grad():
pred_norm = model(test_X)
# unnormalize predictions
pred = pred_norm * y_std + y_mean
# -------------------------------
# Root Mean Squared Error
# -------------------------------
rmse = torch.sqrt(((pred - test_y) ** 2).mean(dim=0))
print("RMSE per output:")
print("X =", rmse[0].item())
print("Y =", rmse[1].item())
print("Z =", rmse[2].item())
print("Energy =", rmse[3].item())
# -------------------------------
# Plot
# -------------------------------
true_vals = test_y.detach().numpy()
pred_vals = pred.detach().numpy()
labels = ["X", "Y", "Z", "Energy"]
residuals = true_vals - pred_vals
plt.figure(figsize=(12,10))
# add units
units = ["cm", "cm", "cm", "keV"]
plt.figure(figsize=(12,10))
for i in range(4):
plt.subplot(2,2,i+1)
plt.hist(residuals[:,i], bins=50, alpha=0.8)
plt.axvline(0, color='red', linestyle='--')
plt.xlabel(f"Measured - Reconstructed ({labels[i]} [{units[i]}])")
plt.ylabel("Counts")
plt.title(f"Residuals: {labels[i]} ({units[i]})")
plt.tight_layout()
plt.show()