forked from jdhp-sap/tino_cta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify_and_reconstruct.py
More file actions
executable file
·420 lines (347 loc) · 16 KB
/
classify_and_reconstruct.py
File metadata and controls
executable file
·420 lines (347 loc) · 16 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
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
#!/usr/bin/env python
from sys import exit
from collections import namedtuple
from glob import glob
import numpy as np
import matplotlib.pyplot as plt
from astropy import units as u
from ctapipe.calib import CameraCalibrator
from ctapipe.io.hessio import hessio_event_source
from ctapipe.utils import linalg
from ctapipe.utils.CutFlow import CutFlow
from ctapipe.image.hillas import HillasParameterizationError, \
hillas_parameters_4 as hillas_parameters
from helper_functions import *
from modules.ImageCleaning import ImageCleaner, EdgeEvent
try:
from ctapipe.reco.event_classifier import *
print("using ctapipe event_classifier")
except ImportError:
from modules.event_classifier import *
print("using tino_cta event_classifier")
try:
from ctapipe.reco.energy_regressor import *
print("using ctapipe energy_regressor")
except:
from modules.energy_regressor import *
print("using tino_cta energy_regressor")
try:
from modules.HillasReconstructor import HillasReconstructor, \
TooFewTelescopesException
print("using tino_cta HillasReconstructor")
except:
from ctapipe.reco.HillasReconstructor import \
HillasReconstructor, TooFewTelescopesException
print("using ctapipe.reco.HillasReconstructor")
from modules.prepare_event import EventPreparator
# PyTables
try:
import tables as tb
except:
print("no pytables installed")
# which models to load for classifier/regressor
cam_id_list = [
# 'GATE',
# 'HESSII',
'NectarCam',
'LSTCam',
'DigiCam',
# 'SST-1m',
# 'FlashCam',
# 'ASTRICam',
# 'SCTCam',
]
def main():
# your favourite units here
energy_unit = u.TeV
angle_unit = u.deg
dist_unit = u.m
agree_threshold = .5
min_tel = 3
parser = make_argparser()
parser.add_argument('--classifier', type=str,
default='data/classifier_pickle/classifier'
'_prod3b_{mode}_{cam_id}_{classifier}.pkl')
parser.add_argument('--regressor', type=str,
default='data/classifier_pickle/regressor'
'_prod3b_{mode}_{cam_id}_{regressor}.pkl')
parser.add_argument('-o', '--out_file', type=str,
default="data/reconstructed_events/classified_events_{}_{}.h5",
help="location to write the classified events to. placeholders "
"are meant as {particle type} and {cleaning mode}")
parser.add_argument('--proton', action='store_true',
help="do protons instead of gammas")
parser.add_argument('--wave_dir', type=str, default=None,
help="directory where to find mr_filter. "
"if not set look in $PATH")
parser.add_argument('--wave_temp_dir', type=str, default='/tmp/',
help="directory where mr_filter to store the temporary fits files"
)
args = parser.parse_args()
if args.infile_list:
filenamelist = []
for f in args.infile_list:
filenamelist += glob("{}/{}".format(args.indir, f))
filenamelist.sort()
elif args.proton:
filenamelist = sorted(glob("{}/proton/*gz".format(args.indir)))[100:]
else:
filenamelist = sorted(glob("{}/gamma/*gz".format(args.indir)))[14:]
if not filenamelist:
print("no files found; check indir: {}".format(args.indir))
exit(-1)
# keeping track of events and where they were rejected
Eventcutflow = CutFlow("EventCutFlow")
Imagecutflow = CutFlow("ImageCutFlow")
# takes care of image cleaning
cleaner = ImageCleaner(mode=args.mode, cutflow=Imagecutflow,
wavelet_options=args.raw,
skip_edge_events=False, island_cleaning=True)
# the class that does the shower reconstruction
shower_reco = HillasReconstructor()
preper = EventPreparator(calib=None, cleaner=cleaner,
hillas_parameters=hillas_parameters, shower_reco=shower_reco,
event_cutflow=Eventcutflow, image_cutflow=Imagecutflow,
# event/image cuts:
allowed_cam_ids=[],
min_ntel=2,
min_charge=args.min_charge, min_pixel=3)
# wrapper for the scikit-learn classifier
classifier = EventClassifier.load(
args.classifier.format(**{
"mode": args.mode,
"wave_args": "mixed",
"classifier": 'RandomForestClassifier',
"cam_id": "{cam_id}"}),
cam_id_list=cam_id_list)
# wrapper for the scikit-learn regressor
regressor = EnergyRegressor.load(
args.regressor.format(**{
"mode": args.mode,
"wave_args": "mixed",
"regressor": "RandomForestRegressor",
"cam_id": "{cam_id}"}),
cam_id_list=cam_id_list)
ClassifierFeatures = namedtuple("ClassifierFeatures", (
"impact_dist",
"sum_signal_evt",
"max_signal_cam",
"sum_signal_cam",
"N_LST",
"N_MST",
"N_SST",
"width",
"length",
"skewness",
"kurtosis",
"h_max",
"err_est_pos",
"err_est_dir"))
EnergyFeatures = namedtuple("EnergyFeatures", (
"impact_dist",
"sum_signal_evt",
"max_signal_cam",
"sum_signal_cam",
"N_LST",
"N_MST",
"N_SST",
"width",
"length",
"skewness",
"kurtosis",
"h_max",
"err_est_pos",
"err_est_dir"))
# catch ctr-c signal to exit current loop and still display results
signal_handler = SignalHandler()
signal.signal(signal.SIGINT, signal_handler)
# this class defines the reconstruction parameters to keep track of
class RecoEvent(tb.IsDescription):
NTels_trig = tb.Int16Col(dflt=1, pos=0)
NTels_reco = tb.Int16Col(dflt=1, pos=1)
NTels_reco_lst = tb.Int16Col(dflt=1, pos=2)
NTels_reco_mst = tb.Int16Col(dflt=1, pos=3)
NTels_reco_sst = tb.Int16Col(dflt=1, pos=4)
MC_Energy = tb.Float32Col(dflt=1, pos=5)
reco_Energy = tb.Float32Col(dflt=1, pos=6)
phi = tb.Float32Col(dflt=1, pos=7)
theta = tb.Float32Col(dflt=1, pos=8)
off_angle = tb.Float32Col(dflt=1, pos=9)
ErrEstPos = tb.Float32Col(dflt=1, pos=10)
gammaness = tb.Float32Col(dflt=1, pos=11)
channel = "gamma" if "gamma" in " ".join(filenamelist) else "proton"
reco_outfile = tb.open_file(
# trying to put particle type and cleaning mode into the filename
# `format` puts in each argument as long as there is a free "{}" token
# if `out_file` was set without any "{}", nothing will be replaced
args.out_file.format(channel, args.mode), mode="w",
# if we don't want to write the event list to disk, need to add more arguments
**({} if args.store else {"driver": "H5FD_CORE",
"driver_core_backing_store": False}))
reco_table = reco_outfile.create_table("/", "reco_events", RecoEvent)
reco_event = reco_table.row
source_orig = None
allowed_tels = None # all telescopes
allowed_tels = prod3b_tel_ids("L+N+D")
for filename in filenamelist[:args.last]:
print("filename = {}".format(filename))
source = hessio_event_source(filename,
allowed_tels=allowed_tels,
max_events=args.max_events)
# loop that cleans and parametrises the images and performs the reconstruction
for (event, hillas_dict, n_tels,
tot_signal, max_signals, pos_fit, dir_fit, h_max,
err_est_pos, err_est_dir) in preper.prepare_event(source):
n_lst, n_mst, n_sst = n_tels["LST"], n_tels["MST"], n_tels["SST"]
# now prepare the features for the classifier
cls_features_evt = {}
reg_features_evt = {}
for tel_id in hillas_dict.keys():
Imagecutflow.count("pre-features")
tel_pos = np.array(event.inst.tel_pos[tel_id][:2]) * u.m
moments = hillas_dict[tel_id]
impact_dist = linalg.length(tel_pos-pos_fit)
cls_features_tel = ClassifierFeatures(
impact_dist/u.m,
tot_signal,
max_signals[tel_id],
moments.size,
n_tels["LST"],
n_tels["MST"],
n_tels["SST"],
moments.width/u.m,
moments.length/u.m,
moments.skewness,
moments.kurtosis,
h_max/u.m,
err_est_pos/u.m,
err_est_dir/u.deg
)
reg_features_tel = EnergyFeatures(
impact_dist/u.m,
tot_signal,
max_signals[tel_id],
moments.size,
n_tels["LST"],
n_tels["MST"],
n_tels["SST"],
moments.width/u.m,
moments.length/u.m,
moments.skewness,
moments.kurtosis,
h_max/u.m,
err_est_pos/u.m,
err_est_dir/u.deg
)
if np.isnan(cls_features_tel).any() or np.isnan(reg_features_tel).any():
continue
Imagecutflow.count("features nan")
cam_id = event.inst.subarray.tel[tel_id].camera.cam_id
try:
reg_features_evt[cam_id] += [reg_features_tel]
cls_features_evt[cam_id] += [cls_features_tel]
except KeyError:
reg_features_evt[cam_id] = [reg_features_tel]
cls_features_evt[cam_id] = [cls_features_tel]
if not cls_features_evt or not reg_features_evt:
continue
predict_energ = regressor.predict_by_event([reg_features_evt])["mean"][0]
predict_proba = classifier.predict_proba_by_event([cls_features_evt])
gammaness = predict_proba[0, 0]
# the MC direction of origin of the simulated particle
source_orig = linalg.set_phi_theta(
event.mc.tel[tel_id].azimuth_raw * u.rad,
(np.pi/2-event.mc.tel[tel_id].altitude_raw)*u.rad)
# and how the reconstructed direction compares to that
off_angle = linalg.angle(dir_fit, source_orig)
phi, theta = linalg.get_phi_theta(dir_fit)
phi = (phi if phi > 0 else phi+360*u.deg)
reco_event["NTels_trig"] = len(event.dl0.tels_with_data)
reco_event["NTels_reco"] = len(hillas_dict)
reco_event["NTels_reco_lst"] = n_lst
reco_event["NTels_reco_mst"] = n_mst
reco_event["NTels_reco_sst"] = n_sst
reco_event["MC_Energy"] = event.mc.energy.to(energy_unit).value
reco_event["reco_Energy"] = predict_energ.to(energy_unit).value
reco_event["phi"] = phi / angle_unit
reco_event["theta"] = theta / angle_unit
reco_event["off_angle"] = off_angle / angle_unit
reco_event["ErrEstPos"] = err_est_pos / dist_unit
reco_event["gammaness"] = gammaness
reco_event.append()
reco_table.flush()
if signal_handler.stop:
break
if signal_handler.stop:
break
print()
Eventcutflow()
print()
Imagecutflow()
# do some simple event selection and print the corresponding selection efficiency
N_selected = len([x for x in reco_table.where(
"""(NTels_reco > min_tel) & (gammaness > agree_threshold)""")])
N_total = len(reco_table)
print("\nfraction selected events:")
print("{} / {} = {} %".format(N_selected, N_total, N_selected/N_total*100))
print("\nlength filenamelist:", len(filenamelist[:args.last]))
# do some plotting if so desired
if args.plot:
gammaness = [x['gammaness'] for x in reco_table]
NTels_rec = [x['NTels_reco'] for x in reco_table]
NTel_bins = np.arange(np.min(NTels_rec), np.max(NTels_rec)+2) - .5
NTels_rec_lst = [x['NTels_reco_lst'] for x in reco_table]
NTels_rec_mst = [x['NTels_reco_mst'] for x in reco_table]
NTels_rec_sst = [x['NTels_reco_sst'] for x in reco_table]
reco_energy = np.array([x['reco_Energy'] for x in reco_table])
mc_energy = np.array([x['MC_Energy'] for x in reco_table])
fig = plt.figure(figsize=(15, 5))
plt.suptitle(" ** ".join([args.mode, "protons" if args.proton else "gamma"]))
plt.subplots_adjust(left=0.05, right=0.97, hspace=0.39, wspace=0.2)
ax = plt.subplot(131)
histo = np.histogram2d(NTels_rec, gammaness,
bins=(NTel_bins, np.linspace(0, 1, 11)))[0].T
histo_normed = histo / histo.max(axis=0)
im = ax.imshow(histo_normed, interpolation='none', origin='lower',
aspect='auto',
# extent=(*NTel_bins[[0, -1]], 0, 1),
cmap=plt.cm.inferno)
ax.set_xlabel("NTels")
ax.set_ylabel("drifted gammaness")
plt.title("Total Number of Telescopes")
# next subplot
ax = plt.subplot(132)
histo = np.histogram2d(NTels_rec_sst, gammaness,
bins=(NTel_bins, np.linspace(0, 1, 11)))[0].T
histo_normed = histo / histo.max(axis=0)
im = ax.imshow(histo_normed, interpolation='none', origin='lower',
aspect='auto',
# extent=(*NTel_bins[[0, -1]], 0, 1),
cmap=plt.cm.inferno)
ax.set_xlabel("NTels")
plt.setp(ax.get_yticklabels(), visible=False)
plt.title("Number of SSTs")
# next subplot
ax = plt.subplot(133)
histo = np.histogram2d(NTels_rec_mst, gammaness,
bins=(NTel_bins, np.linspace(0, 1, 11)))[0].T
histo_normed = histo / histo.max(axis=0)
im = ax.imshow(histo_normed, interpolation='none', origin='lower',
aspect='auto',
# extent=(*NTel_bins[[0, -1]], 0, 1),
cmap=plt.cm.inferno)
cb = fig.colorbar(im, ax=ax)
ax.set_xlabel("NTels")
plt.setp(ax.get_yticklabels(), visible=False)
plt.title("Number of MSTs")
plt.subplots_adjust(wspace=0.05)
# plot the energy migration matrix
plt.figure()
plt.hist2d(np.log10(reco_energy), np.log10(mc_energy), bins=20,
cmap=plt.cm.inferno)
plt.xlabel("E_MC / TeV")
plt.ylabel("E_rec / TeV")
plt.colorbar()
plt.show()
if __name__ == '__main__':
main()