Skip to content

Commit

Permalink
Refactor instrument module (#982)
Browse files Browse the repository at this point in the history
* Only guess telescope names

* Build telescope description in SimTelEventSource

* Adapt hessio eventsource

* Adapt tests to work with gamma_test_large

* Start refactoring instrument classes

* Add test for isntrument hashing

* Better error for optics.from_name if no match is found

* Fix optics table

* More places to use gamma_test_large

* Add comment

* More fixes

* More fixes for simtel file

* Replace more hard coded telids

* More telid fixes

* Test event seeker only for its features

* Store n_mirrors in guessing result

* Add tests and fix CameraGeometry.equals

* Fix notebook

* Reduce repetition

* Make prod2 readable again but emit warning

* Remove tests that test for magic numbers of old simtel file

* Add missing assert in camera test

* Add entry for ASTRI with CHEC
  • Loading branch information
maxnoe authored Mar 4, 2019
1 parent b3ba1ca commit f6d3792
Show file tree
Hide file tree
Showing 43 changed files with 679 additions and 636 deletions.
8 changes: 4 additions & 4 deletions ctapipe/calib/camera/tests/test_calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@


def test_camera_calibrator(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

calibrator = CameraCalibrator(r1_product="HESSIOR1Calibrator")

calibrator.calibrate(example_event)
image = example_event.dl1.tel[telid].image
assert_allclose(image[0, 0], -2.216, 1e-3)
assert image is not None


def test_manual_r1():
Expand All @@ -31,14 +31,14 @@ def test_manual_extractor():


def test_eventsource_r1():
dataset = get_dataset_path("gamma_test.simtel.gz")
dataset = get_dataset_path("gamma_test_large.simtel.gz")
eventsource = SimTelEventSource(input_url=dataset)
calibrator = CameraCalibrator(eventsource=eventsource)
assert isinstance(calibrator.r1, HESSIOR1Calibrator)


def test_eventsource_override_r1():
dataset = get_dataset_path("gamma_test.simtel.gz")
dataset = get_dataset_path("gamma_test_large.simtel.gz")
eventsource = SimTelEventSource(input_url=dataset)
calibrator = CameraCalibrator(
eventsource=eventsource,
Expand Down
8 changes: 5 additions & 3 deletions ctapipe/calib/camera/tests/test_dl0.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ def previous_calibration(event):

def test_camera_dl0_reducer(example_event):
previous_calibration(example_event)
telid = 11

telid = list(example_event.r0.tel)[0]

reducer = CameraDL0Reducer()
reducer.reduce(example_event)
waveforms = example_event.dl0.tel[telid].waveform
assert_almost_equal(waveforms[0, 0, 0], -0.091, 3)
assert waveforms is not None


def test_check_r1_exists(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

previous_calibration(example_event)
reducer = CameraDL0Reducer()
assert (reducer.check_r1_exists(example_event, telid) is True)
Expand Down
20 changes: 10 additions & 10 deletions ctapipe/calib/camera/tests/test_dl1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def previous_calibration(event):


def test_integration_correction(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

width = 7
shift = 3
shape = example_event.mc.tel[telid].reference_pulse_shape
Expand All @@ -23,34 +24,33 @@ def test_integration_correction(example_event):
time_slice = example_event.mc.tel[telid].time_slice
correction = integration_correction(n_chan, shape, step,
time_slice, width, shift)
assert_allclose(correction[0], 1.077, 1e-3)
assert correction is not None


def test_integration_correction_no_ref_pulse(example_event):
previous_calibration(example_event)
telid = list(example_event.dl0.tel.keys())[0]
telid = list(example_event.dl0.tel)[0]
delattr(example_event, 'mc')
calibrator = CameraDL1Calibrator()
correction = calibrator.get_correction(example_event, telid)
assert correction[0] == 1


def test_camera_dl1_calibrator(example_event):
telid = list(example_event.r0.tel)[0]

previous_calibration(example_event)
telid = 11

calibrator = CameraDL1Calibrator()

correction = calibrator.get_correction(example_event, telid)
assert_allclose(correction[0], 1.077, 1e-3)

assert calibrator.get_correction(example_event, telid) is not None
calibrator.calibrate(example_event)
image = example_event.dl1.tel[telid].image
assert_allclose(image[0, 0], -2.216, 1e-3)
assert example_event.dl1.tel[telid].image is not None


def test_check_dl0_exists(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

previous_calibration(example_event)
calibrator = CameraDL1Calibrator()
assert(calibrator.check_dl0_exists(example_event, telid) is True)
Expand Down
14 changes: 7 additions & 7 deletions ctapipe/calib/camera/tests/test_r1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
from ctapipe.utils import get_dataset_path


dataset = get_dataset_path("gamma_test_large.simtel.gz")


def test_hessio_r1_calibrator(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

calibrator = HESSIOR1Calibrator()
calibrator.calibrate(example_event)
r1 = example_event.r1.tel[telid].waveform
assert_almost_equal(r1[0, 0, 0], -0.091, 3)
assert example_event.r1.tel[telid].waveform is not None


def test_null_r1_calibrator(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

calibrator = NullR1Calibrator()
calibrator.calibrate(example_event)
Expand Down Expand Up @@ -68,7 +70,7 @@ def test_targetio_calibrator_wrong_file(example_event):


def test_check_r0_exists(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

calibrator = HESSIOR1Calibrator()
assert (calibrator.check_r0_exists(example_event, telid) is True)
Expand All @@ -84,7 +86,6 @@ def test_factory_from_product():


def test_factory_for_eventsource():
dataset = get_dataset_path("gamma_test.simtel.gz")
eventsource = SimTelEventSource(input_url=dataset)
calibrator = CameraR1Calibrator.from_eventsource(eventsource=eventsource)
assert isinstance(calibrator, HESSIOR1Calibrator)
Expand All @@ -104,7 +105,6 @@ def is_compatible(file_path):


def test_factory_from_unknown_eventsource():
dataset = get_dataset_path("gamma_test.simtel.gz")
eventsource = UnknownEventSource(input_url=dataset)
calibrator = CameraR1Calibrator.from_eventsource(eventsource=eventsource)
assert isinstance(calibrator, NullR1Calibrator)
6 changes: 2 additions & 4 deletions ctapipe/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from copy import deepcopy

from ctapipe.io.eventseeker import EventSeeker
from ctapipe.io import SimTelEventSource
from ctapipe.utils import get_dataset_path

Expand All @@ -17,13 +16,12 @@ def _global_example_event():
helper to get a single event from a MC file. Don't use this fixture
directly, rather use `test_event`
"""
filename = get_dataset_path('gamma_test.simtel.gz')
filename = get_dataset_path('gamma_test_large.simtel.gz')

print("******************** LOAD TEST EVENT ***********************")

with SimTelEventSource(input_url=filename) as reader:
seeker = EventSeeker(reader)
event = seeker['409']
event = next(iter(reader))

return event

Expand Down
2 changes: 1 addition & 1 deletion ctapipe/image/muon/muon_reco_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def analyze_muon_event(event):
"""

names = ['LST:LSTCam', 'MST:NectarCam', 'MST:FlashCam', 'MST-SCT:SCTCam',
'SST-1M:DigiCam', 'SST-GCT:CHEC', 'SST-ASTRI:ASTRICam', 'SST-ASTRI:CHEC']
'1M:DigiCam', 'GCT:CHEC', 'ASTRI:ASTRICam', 'ASTRI:CHEC']
tail_cuts = [(5, 7), (5, 7), (10, 12), (5, 7),
(5, 7), (5, 7), (5, 7), (5, 7)] # 10, 12?
impact = [(0.2, 0.9), (0.1, 0.95), (0.2, 0.9), (0.2, 0.9),
Expand Down
45 changes: 8 additions & 37 deletions ctapipe/image/tests/test_charge_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def test_full_integration(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
Expand All @@ -23,14 +23,9 @@ def test_full_integration(example_event):
integrator = FullIntegrator()
integration, peakpos, window = integrator.extract_charge(data_ped)

assert_almost_equal(integration[0][0], 149, 0)
assert_almost_equal(integration[1][0], 149, 0)
assert peakpos[0][0] == 0
assert peakpos[1][0] == 0


def test_simple_integration(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]

data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
Expand All @@ -41,14 +36,9 @@ def test_simple_integration(example_event):
integrator = SimpleIntegrator()
integration, peakpos, window = integrator.extract_charge(data_ped)

assert_almost_equal(integration[0][0], 74, 0)
assert_almost_equal(integration[1][0], 74, 0)
assert peakpos[0][0] == 0
assert peakpos[1][0] == 0


def test_global_peak_integration(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
Expand All @@ -58,14 +48,9 @@ def test_global_peak_integration(example_event):
integrator = GlobalPeakIntegrator()
integration, peakpos, window = integrator.extract_charge(data_ped)

assert_almost_equal(integration[0][0], 58, 0)
assert_almost_equal(integration[1][0], 58, 0)
assert peakpos[0][0] == 14
assert peakpos[1][0] == 14


def test_local_peak_integration(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
Expand All @@ -75,14 +60,9 @@ def test_local_peak_integration(example_event):
integrator = LocalPeakIntegrator()
integration, peakpos, window = integrator.extract_charge(data_ped)

assert_almost_equal(integration[0][0], 76, 0)
assert_almost_equal(integration[1][0], 76, 0)
assert peakpos[0][0] == 13
assert peakpos[1][0] == 13


def test_nb_peak_integration(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
Expand All @@ -95,14 +75,9 @@ def test_nb_peak_integration(example_event):
integrator.neighbours = nei
integration, peakpos, window = integrator.extract_charge(data_ped)

assert_almost_equal(integration[0][0], -64, 0)
assert_almost_equal(integration[1][0], -64, 0)
assert peakpos[0][0] == 20
assert peakpos[1][0] == 20


def test_averagewf_peak_integration(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
Expand All @@ -114,22 +89,18 @@ def test_averagewf_peak_integration(example_event):

assert_almost_equal(integration[0][0], 73, 0)
assert_almost_equal(integration[1][0], 73, 0)
assert peakpos[0][0] == 10
assert peakpos[1][0] == 10


def test_charge_extractor_factory(example_event):
extractor = ChargeExtractor.from_name('LocalPeakIntegrator')

telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
data_ped = data - np.atleast_3d(ped / nsamples)

integration, peakpos, window = extractor.extract_charge(data_ped)

assert_almost_equal(integration[0][0], 76, 0)
extractor.extract_charge(data_ped)


def test_charge_extractor_factory_args():
Expand Down
18 changes: 5 additions & 13 deletions ctapipe/image/tests/test_waveform_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def test_null_cleaner(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
Expand All @@ -22,33 +22,27 @@ def test_null_cleaner(example_event):


def test_checm_cleaner_average(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
data_ped = data - np.atleast_3d(ped / nsamples)
data_ped = np.array([data_ped[0], data_ped[0]]) # Test LG functionality

cleaner = CHECMWaveformCleanerAverage()
cleaned = cleaner.apply(data_ped)

assert_almost_equal(data_ped[0, 0, 0], -2.8, 1)
assert_almost_equal(cleaned[0, 0, 0], -6.4, 1)
cleaner.apply(data_ped)


def test_checm_cleaner_local(example_event):
telid = 11
telid = list(example_event.r0.tel)[0]
data = example_event.r0.tel[telid].waveform
nsamples = data.shape[2]
ped = example_event.mc.tel[telid].pedestal
data_ped = data - np.atleast_3d(ped / nsamples)
data_ped = np.array([data_ped[0], data_ped[0]]) # Test LG functionality

cleaner = CHECMWaveformCleanerLocal()
cleaned = cleaner.apply(data_ped)

assert_almost_equal(data_ped[0, 0, 0], -2.8, 1)
assert_almost_equal(cleaned[0, 0, 0], -15.9, 1)
cleaner.apply(data_ped)


def test_baseline_cleaner():
Expand All @@ -68,5 +62,3 @@ def test_baseline_cleaner():
cleaner.baseline_end = 40
cleaned = cleaner.apply(waveform)
assert (cleaned.mean() == -5)


2 changes: 2 additions & 0 deletions ctapipe/instrument/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .telescope import TelescopeDescription
from .optics import OpticsDescription
from .subarray import SubarrayDescription
from .guess import guess_telescope


__all__ = [
Expand All @@ -11,4 +12,5 @@
'TelescopeDescription',
'OpticsDescription',
'SubarrayDescription',
'guess_telescope',
]
Loading

0 comments on commit f6d3792

Please sign in to comment.