-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathconftest.py
441 lines (386 loc) · 13.9 KB
/
conftest.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
from ctapipe_io_lst import LSTEventSource
import pandas as pd
import pytest
from ctapipe.instrument import SubarrayDescription
import os
from lstchain.io.io import dl1_params_lstcam_key
from lstchain.scripts.tests.test_lstchain_scripts import run_program
from lstchain.tests.test_lstchain import (
test_calib_path,
test_data,
test_drive_report,
test_drs4_pedestal_path,
test_r0_path,
test_r0_path2,
test_time_calib_path,
)
# add a marker for the tests that need private data and don't run them
# by default
def pytest_configure(config):
if "private_data" not in config.option.markexpr:
if config.option.markexpr:
config.option.markexpr += " and "
config.option.markexpr += "not private_data"
@pytest.fixture(scope="session")
def lst1_subarray():
return LSTEventSource.create_subarray(tel_id=1)
@pytest.fixture(scope="session")
def temp_dir_simulated_files(tmp_path_factory):
"""Temporal common directory for processing simulated data."""
return tmp_path_factory.mktemp("simulated_files")
@pytest.fixture(scope="session")
def temp_dir_simulated_srcdep_files(tmp_path_factory):
"""Temporal common directory for processing simulated data for source-dependent analysis."""
return tmp_path_factory.mktemp("simulated_srcdep_files")
@pytest.mark.private_data
@pytest.fixture(scope="session")
def temp_dir_observed_files(tmp_path_factory):
"""Temporal common directory for processing observed data."""
return tmp_path_factory.mktemp("observed_files")
@pytest.mark.private_data
@pytest.fixture(scope="session")
def temp_dir_observed_srcdep_files(tmp_path_factory):
"""Temporal common directory for processing observed data."""
return tmp_path_factory.mktemp("observed_srcdep_files")
@pytest.fixture(scope="session")
def mc_gamma_testfile():
"""Get a simulated test file."""
return test_data / "mc/simtel_theta_20_az_180_gdiffuse_10evts.simtel.gz"
@pytest.fixture(scope="session")
def simulated_dl1_file(temp_dir_simulated_files, mc_gamma_testfile):
"""Produce a dl1 file from simulated data."""
output_dl1_path = temp_dir_simulated_files / "dl1_simtel_theta_20_az_180_gdiffuse_10evts.h5"
run_program(
"lstchain_mc_r0_to_dl1", "-f", mc_gamma_testfile, "-o", temp_dir_simulated_files
)
return output_dl1_path
@pytest.fixture(scope='session')
def run_summary_path(temp_dir_observed_files):
date = "20200218"
r0_path = test_data / "real/R0"
run_summary_path = temp_dir_observed_files / f"RunSummary_{date}.ecsv"
run_program(
"lstchain_create_run_summary",
"--date", date,
"--r0-path", r0_path,
"--output-dir", temp_dir_observed_files
)
return run_summary_path
@pytest.mark.private_data
@pytest.fixture(scope="session")
def observed_dl1_files(temp_dir_observed_files, run_summary_path):
"""
Produce dl1, datacheck and muons files from real observed data.
The initial timestamps and counters used for the first set of files
here are extracted from the night summary. In this case these values
correspond to the third event. A second set of files are produced
without using the first valid timestamps.
"""
# FIXME: naming criteria (suffixes, no stream) of dl1, dl2,
# muons and datacheck files should be coherent
# First set of files to be produced
dl1_output_path1 = temp_dir_observed_files / "dl1_LST-1.Run02008.0000.h5"
muons_file1 = temp_dir_observed_files / "muons_LST-1.Run02008.0000.fits"
datacheck_file1 = temp_dir_observed_files / "datacheck_dl1_LST-1.Run02008.0000.h5"
dvr_file1 = temp_dir_observed_files / "DVR_settings_LST-1.Run02008.h5"
pixmasks_file1 = temp_dir_observed_files / "Pixel_selection_LST-1.Run02008.0000.h5"
interleaved_file1 = temp_dir_observed_files / "interleaved/interleaved_LST-1.Run02008.0000.h5"
# Second set of files
dl1_output_path2 = temp_dir_observed_files / "dl1_LST-1.Run02008.0100.h5"
muons_file2 = temp_dir_observed_files / "muons_LST-1.Run02008.0100.fits"
datacheck_file2 = temp_dir_observed_files / "datacheck_dl1_LST-1.Run02008.0100.h5"
interleaved_file2 = temp_dir_observed_files / "interleaved/interleaved_LST-1.Run02008.0100.h5"
run_program(
"lstchain_data_r0_to_dl1",
"-f",
test_r0_path,
"-o",
temp_dir_observed_files,
"--pedestal-file",
test_drs4_pedestal_path,
"--calibration-file",
test_calib_path,
"--time-calibration-file",
test_time_calib_path,
"--pointing-file",
test_drive_report,
"--dragon-reference-time",
"1582059789516351903",
"--dragon-reference-counter",
"2516351600",
"--dragon-module-id",
"132",
"--default-trigger-type=tib",
)
run_program(
"lstchain_check_dl1",
"-b",
"--omit-pdf",
"--output-dir",
temp_dir_observed_files,
"--input-file",
dl1_output_path1
)
run_program(
"lstchain_dvr_pixselector",
"--dl1-files",
dl1_output_path1,
"--output-dir",
temp_dir_observed_files
)
run_program(
"lstchain_dvr_pixselector",
"--dl1-files",
dl1_output_path1,
"--output-dir",
temp_dir_observed_files,
"--action",
"create_pixel_masks"
)
run_program(
"lstchain_data_r0_to_dl1",
"-f",
test_r0_path2,
"-o",
temp_dir_observed_files,
"--pedestal-file",
test_drs4_pedestal_path,
"--calibration-file",
test_calib_path,
"--time-calibration-file",
test_time_calib_path,
"--pointing-file",
test_drive_report,
'--run-summary-path',
run_summary_path,
"--default-trigger-type=tib",
)
run_program(
"lstchain_check_dl1",
"-b",
"--omit-pdf",
"--output-dir",
temp_dir_observed_files,
"--input-file",
dl1_output_path2
)
return {
'dl1_file1': dl1_output_path1,
'muons1': muons_file1,
'datacheck1': datacheck_file1,
'dvr_file1': dvr_file1,
'pixmasks_file1': pixmasks_file1,
'interleaved_file1': interleaved_file1,
'dl1_file2': dl1_output_path2,
'muons2': muons_file2,
'datacheck2': datacheck_file2,
'interleaved_file2': interleaved_file2
}
@pytest.mark.private_data
@pytest.fixture(scope="session")
def interleaved_r1_file(temp_dir_observed_files, run_summary_path):
test_pedcal_run = test_data / 'real/R0/20200218/LST-1.1.Run02006.0000_first50.fits.fz'
run_program(
"lstchain_data_r0_to_dl1",
"-f",
test_pedcal_run,
"-o",
temp_dir_observed_files,
"--pedestal-file",
test_drs4_pedestal_path,
"--calibration-file",
test_calib_path,
"--time-calibration-file",
test_time_calib_path,
"--pointing-file",
test_drive_report,
'--run-summary-path',
run_summary_path,
"--default-trigger-type=tib"
)
return temp_dir_observed_files / "interleaved/interleaved_LST-1.Run02006.0000.h5"
@pytest.fixture(scope="session")
def simulated_dl2_file(temp_dir_simulated_files, simulated_dl1_file, rf_models):
"""
Produce the test dl2 file from the simulated dl1 test file
using the random forest test models.
"""
dl2_file = temp_dir_simulated_files / "dl2_simtel_theta_20_az_180_gdiffuse_10evts.h5"
run_program(
"lstchain_dl1_to_dl2",
"--input-file",
simulated_dl1_file,
"--path-models",
rf_models["path"],
"--output-dir",
temp_dir_simulated_files,
)
return dl2_file
@pytest.fixture(scope="session")
def simulated_srcdep_dl2_file(temp_dir_simulated_srcdep_files, simulated_dl1_file, rf_models_srcdep):
"""
Produce the test source-dependent dl2 file from the simulated dl1 test file
using the random forest test models.
"""
srcdep_dl2_file = temp_dir_simulated_srcdep_files / "dl2_simtel_theta_20_az_180_gdiffuse_10evts.h5"
srcdep_config_file = os.path.join(os.getcwd(), "./lstchain/data/lstchain_src_dep_config.json")
run_program(
"lstchain_dl1_to_dl2",
"--input-file",
simulated_dl1_file,
"--path-models",
rf_models_srcdep["path"],
"--output-dir",
temp_dir_simulated_srcdep_files,
"--config",
srcdep_config_file,
)
return srcdep_dl2_file
@pytest.fixture(scope="session")
def fake_dl1_proton_file(temp_dir_simulated_files, simulated_dl1_file):
"""
Produce a fake dl1 proton file by copying the dl1 gamma test file
and changing mc_type.
"""
dl1_proton_file = temp_dir_simulated_files / "dl1_fake_proton.simtel.h5"
events = pd.read_hdf(simulated_dl1_file, key=dl1_params_lstcam_key)
events.mc_type = 101
events.to_hdf(dl1_proton_file, key=dl1_params_lstcam_key)
return dl1_proton_file
@pytest.fixture(scope="session")
def simulated_dl1_srcdep_file(temp_dir_simulated_files, simulated_dl1_file):
"""
Produce a fake dl1 gamma file by copying the dl1 gamma test file
and changing src_x & src_y to zeros to keep events after src_r cuts.
"""
dl1_gamma_srcdep_file = temp_dir_simulated_files / "dl1_fake_gamma_srcdep.simtel.h5"
events = pd.read_hdf(simulated_dl1_file, key=dl1_params_lstcam_key)
subarray_info = SubarrayDescription.from_hdf(simulated_dl1_file)
events.src_x = 0
events.src_y = 0
events.to_hdf(dl1_gamma_srcdep_file, key=dl1_params_lstcam_key)
subarray_info.to_hdf(dl1_gamma_srcdep_file)
return dl1_gamma_srcdep_file
@pytest.fixture(scope="session")
def rf_models(temp_dir_simulated_files, simulated_dl1_file):
"""Produce test random forest models."""
gamma_file = simulated_dl1_file
proton_file = simulated_dl1_file
models_path = temp_dir_simulated_files
file_model_energy = models_path / "reg_energy.sav"
file_model_gh_sep = models_path / "cls_gh.sav"
file_model_disp_norm = models_path / "reg_disp_norm.sav"
file_model_disp_sign = models_path / "cls_disp_sign.sav"
run_program(
"lstchain_mc_trainpipe",
"--fg",
gamma_file,
"--fp",
proton_file,
"-o",
models_path,
)
return {
"energy": file_model_energy,
"gh_sep": file_model_gh_sep,
"path": models_path,
"disp_norm": file_model_disp_norm,
"disp_sign": file_model_disp_sign,
}
@pytest.fixture(scope="session")
def rf_models_srcdep(temp_dir_simulated_srcdep_files, simulated_dl1_file, simulated_dl1_srcdep_file):
"""Produce test random forest models for source-dependent analysis."""
gamma_file = simulated_dl1_srcdep_file
proton_file = simulated_dl1_file
models_srcdep_path = temp_dir_simulated_srcdep_files
file_model_energy_srcdep = models_srcdep_path / "reg_energy.sav"
file_model_gh_sep_srcdep = models_srcdep_path / "cls_gh.sav"
file_model_disp_norm_srcdep = models_srcdep_path / "reg_disp_norm.sav"
file_model_disp_sign_srcdep = models_srcdep_path / "cls_disp_sign.sav"
srcdep_config_file = os.path.join(os.getcwd(), "./lstchain/data/lstchain_src_dep_config.json")
run_program(
"lstchain_mc_trainpipe",
"--fg",
gamma_file,
"--fp",
proton_file,
"-o",
models_srcdep_path,
"-c",
srcdep_config_file,
)
return {
"energy": file_model_energy_srcdep,
"gh_sep": file_model_gh_sep_srcdep,
"path": models_srcdep_path,
"disp_norm": file_model_disp_norm_srcdep,
"disp_sign": file_model_disp_sign_srcdep,
}
@pytest.fixture(scope="session")
@pytest.mark.private_data
def observed_dl2_file(temp_dir_observed_files, observed_dl1_files, rf_models):
"""Produce a dl2 file from an observed dl1 file."""
real_data_dl2_file = temp_dir_observed_files / (observed_dl1_files["dl1_file1"].name.replace("dl1", "dl2"))
run_program(
"lstchain_dl1_to_dl2",
"--input-file",
observed_dl1_files["dl1_file1"],
"--path-models",
rf_models["path"],
"--output-dir",
temp_dir_observed_files
)
return real_data_dl2_file
@pytest.fixture(scope="session")
@pytest.mark.private_data
def observed_srcdep_dl2_file(temp_dir_observed_srcdep_files, observed_dl1_files, rf_models_srcdep):
"""Produce a source-dependent dl2 file from an observed dl1 file."""
real_data_srcdep_dl2_file = temp_dir_observed_srcdep_files / (observed_dl1_files["dl1_file1"].name.replace("dl1", "dl2"))
srcdep_config_file = os.path.join(os.getcwd(), "./lstchain/data/lstchain_src_dep_config.json")
run_program(
"lstchain_dl1_to_dl2",
"--input-file",
observed_dl1_files["dl1_file1"],
"--path-models",
rf_models_srcdep["path"],
"--output-dir",
temp_dir_observed_srcdep_files,
"--config",
srcdep_config_file,
)
return real_data_srcdep_dl2_file
@pytest.fixture(scope="session")
def simulated_irf_file(simulated_dl2_file):
"""
Produce test irf file from the simulated dl2 test file.
Using the same test file for gamma, proton and electron inputs
"""
irf_file = simulated_dl2_file.parent / "irf.fits.gz"
run_program(
"lstchain_create_irf_files",
"--input-gamma-dl2",
simulated_dl2_file,
"--input-proton-dl2",
simulated_dl2_file,
"--input-electron-dl2",
simulated_dl2_file,
"--output-irf-file",
irf_file
)
return irf_file
@pytest.fixture(scope="session")
def simulated_srcdep_irf_file(simulated_srcdep_dl2_file):
"""
Produce test source-dependent irf file from the simulated dl2 test file.
"""
srcdep_irf_file = simulated_srcdep_dl2_file.parent / "srcdep_irf.fits.gz"
run_program(
"lstchain_create_irf_files",
"--input-gamma-dl2",
simulated_srcdep_dl2_file,
"--output-irf-file",
srcdep_irf_file,
"--point-like",
"--source-dep"
)
return srcdep_irf_file