def test_get_event():
    dataset = get_datasets_path("gamma_test.simtel.gz")
    file = HessioFileReader(None, None, input_path=dataset)
    event = file.get_event(2)
    assert event.count == 2
    assert event.dl0.event_id == 803
    event = file.get_event(803, True)
    assert event.count == 2
    assert event.dl0.event_id == 803
def test_get_num_events():
    dataset = get_datasets_path("gamma_test.simtel.gz")
    file = HessioFileReader(None, None, input_path=dataset)
    num_events = file.num_events
    assert(num_events == 9)

    file.max_events = 2
    num_events = file.num_events
    assert (num_events == 2)
def test_hessio_file_reader():
    dataset = get_dataset("gamma_test.simtel.gz")
    file = HessioFileReader(None, None, input_path=dataset)
    assert file.directory == dirname(dataset)
    assert file.extension == ".gz"
    assert file.filename == "gamma_test.simtel"
    source = file.read()
    event = next(source)
    assert event.r0.tels_with_data == {38, 47}
def test_get_num_events():
    dataset = get_datasets_path("gamma_test.simtel.gz")
    file = HessioFileReader(None, None, input_path=dataset)
    num_events = file.num_events
    assert (num_events == 9)

    file.max_events = 2
    num_events = file.num_events
    assert (num_events == 2)
def test_get_event():
    dataset = get_datasets_path("gamma_test.simtel.gz")
    file = HessioFileReader(None, None, input_path=dataset)
    event = file.get_event(2)
    assert event.count == 2
    assert event.r0.event_id == 803
    event = file.get_event(803, True)
    assert event.count == 2
    assert event.r0.event_id == 803
Exemple #6
0
def test_hessio_file_reader():
    dataset = get_dataset("gamma_test.simtel.gz")
    file = HessioFileReader(None, None, input_path=dataset)
    assert file.directory == dirname(dataset)
    assert file.extension == ".gz"
    assert file.filename == "gamma_test.simtel"
    source = file.read()
    event = next(source)
    assert event.r0.tels_with_data == {38, 47}
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.file_reader = HessioFileReader(**kwargs)

        extractor_factory = ChargeExtractorFactory(**kwargs)
        extractor_class = extractor_factory.get_class()
        extractor = extractor_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.file_reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(extractor=extractor, **kwargs)

        self.calculator = ChargeResolutionCalculator(**kwargs)
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.file_reader = HessioFileReader(**kwargs)

        extractor_factory = ChargeExtractorFactory(**kwargs)
        extractor_class = extractor_factory.get_class()
        extractor = extractor_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.file_reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(extractor=extractor, **kwargs)

        self.calculator = ChargeResolutionCalculator(**kwargs)
Exemple #9
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"

        data_config = self.config.copy()
        data_config['WaveformCleanerFactory'] = Config(cleaner='CHECMWaveformCleanerLocal')
        mc_config = self.config.copy()

        data_kwargs = dict(config=data_config, tool=self)
        mc_kwargs = dict(config=mc_config, tool=self)

        filepath = '/Volumes/gct-jason/data/170330/onsky-mrk501/Run05477_r1.tio'
        reader = TargetioFileReader(input_path=filepath, **data_kwargs)
        filepath = '/Users/Jason/Software/outputs/sim_telarray/meudon_cr/simtel_proton_nsb50_thrs30_1petal_rndm015_heide.gz'
        # filepath = '/Users/Jason/Software/outputs/sim_telarray/meudon_cr/simtel_proton_nsb50_thrs30.gz'
        reader_mc = HessioFileReader(input_path=filepath, **mc_kwargs)

        calibrator = CameraCalibrator(origin=reader.origin,
                                      **data_kwargs)
        calibrator_mc = CameraCalibrator(origin=reader_mc.origin,
                                         **mc_kwargs)

        first_event = reader.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        pos = first_event.inst.pixel_pos[telid]
        foclen = first_event.inst.optical_foclen[telid]
        geom = CameraGeometry.guess(*pos, foclen)

        first_event = reader_mc.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        pos_mc = first_event.inst.pixel_pos[telid]
        foclen = first_event.inst.optical_foclen[telid]
        geom_mc = CameraGeometry.guess(*pos_mc, foclen)

        d1 = dict(type='Data', reader=reader, calibrator=calibrator,
                  pos=pos, geom=geom, t1=20, t2=10)
        d2 = dict(type='MC', reader=reader_mc, calibrator=calibrator_mc,
                  pos=pos_mc, geom=geom_mc, t1=20, t2=10)
        self.reader_df = pd.DataFrame([d1, d2])

        p_kwargs = data_kwargs
        p_kwargs['script'] = "checm_paper_hillas"
        p_kwargs['figure_name'] = "all_images"
        self.p_allimage = AllImagePlotter(**p_kwargs)
        p_kwargs['figure_name'] = "all_peak_time_images"
        self.p_alltimeimage = PeakTimePlotter(**p_kwargs)
        p_kwargs['figure_name'] = "all_mc_images"
        self.p_allmcimage = AllImagePlotter(**p_kwargs)
        p_kwargs['figure_name'] = "zero_width_images"
        self.p_zwimage = ZeroWidthImagePlotter(**p_kwargs)
        p_kwargs['figure_name'] = "zero_width_mc_images"
        self.p_zwmcimage = ZeroWidthImagePlotter(**p_kwargs)
        p_kwargs['figure_name'] = "muon_images"
        self.p_muonimage = MuonImagePlotter(**p_kwargs)
        p_kwargs['figure_name'] = "bright_images"
        self.p_brightimage = BrightImagePlotter(**p_kwargs)
        p_kwargs['figure_name'] = "count_image"
        self.p_countimage = CountPlotter(**p_kwargs)
        p_kwargs['figure_name'] = "whole_distribution"
        self.p_whole_dist = WholeDist(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "width_vs_length"
        self.p_widthvslength = WidthVsLength(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "size_vs_length"
        self.p_sizevslength = SizeVsLength(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "width_div_length"
        self.p_widthdivlength = WidthDivLength(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "length_div_size"
        self.p_lengthdivsize = LengthDivSize(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "pair_plot"
        self.p_pair = PairPlotter(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "pair_mc_plot"
        self.p_mc_pair = PairPlotter(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "length"
        self.p_length = LengthPlotter(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "width"
        self.p_width = WidthPlotter(**p_kwargs, shape='wide')
class ChargeResolutionGenerator(Tool):
    name = "ChargeResolutionGenerator"
    description = "Generate the a pickle file of ChargeResolutionFile for " \
                  "a MC file."

    telescopes = List(Int,
                      None,
                      allow_none=True,
                      help='Telescopes to include from the event file. '
                      'Default = All telescopes').tag(config=True)
    output_name = Unicode('charge_resolution',
                          help='Name of the output charge resolution pickle '
                          'file').tag(config=True)

    aliases = Dict(
        dict(
            f='HessioFileReader.input_path',
            max_events='HessioFileReader.max_events',
            extractor='ChargeExtractorFactory.extractor',
            window_width='ChargeExtractorFactory.window_width',
            window_start='ChargeExtractorFactory.window_start',
            window_shift='ChargeExtractorFactory.window_shift',
            sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG',
            sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG',
            lwt='ChargeExtractorFactory.lwt',
            clip_amplitude='CameraDL1Calibrator.clip_amplitude',
            radius='CameraDL1Calibrator.radius',
            max_pe='ChargeResolutionCalculator.max_pe',
            T='ChargeResolutionGenerator.telescopes',
            O='ChargeResolutionGenerator.output_name',
        ))
    classes = List([
        HessioFileReader, ChargeExtractorFactory, CameraDL1Calibrator,
        ChargeResolutionCalculator
    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.file_reader = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.calculator = None

    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.file_reader = HessioFileReader(**kwargs)

        extractor_factory = ChargeExtractorFactory(**kwargs)
        extractor_class = extractor_factory.get_class()
        extractor = extractor_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.file_reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(extractor=extractor, **kwargs)

        self.calculator = ChargeResolutionCalculator(**kwargs)

    def start(self):
        desc = "Filling Charge Resolution"
        with tqdm(desc=desc) as pbar:
            source = self.file_reader.read()
            for event in source:
                pbar.update(1)
                tels = list(event.dl0.tels_with_data)

                # Check events have true charge included
                if event.count == 0:
                    try:
                        if np.all(event.mc.tel[tels[0]].photo_electron_image ==
                                  0):
                            raise KeyError
                    except KeyError:
                        self.log.exception('Source does not contain '
                                           'true charge!')
                        raise

                self.r1.calibrate(event)
                self.dl0.reduce(event)
                self.dl1.calibrate(event)

                if self.telescopes:
                    tels = []
                    for tel in self.telescopes:
                        if tel in event.dl0.tels_with_data:
                            tels.append(tel)

                for telid in tels:
                    true_charge = event.mc.tel[telid].photo_electron_image
                    measured_charge = event.dl1.tel[telid].image[0]
                    self.calculator.add_charges(true_charge, measured_charge)

    def finish(self):
        directory = self.file_reader.output_directory
        name = "{}.pickle".format(self.output_name)
        ouput_path = os.path.join(directory, name)
        self.calculator.save(ouput_path)
class ChargeResolutionGenerator(Tool):
    name = "ChargeResolutionGenerator"
    description = "Generate the a pickle file of ChargeResolutionFile for " \
                  "a MC file."

    telescopes = List(Int, None, allow_none=True,
                      help='Telescopes to include from the event file. '
                           'Default = All telescopes').tag(config=True)
    output_name = Unicode('charge_resolution',
                          help='Name of the output charge resolution pickle '
                               'file').tag(config=True)

    aliases = Dict(dict(f='HessioFileReader.input_path',
                        max_events='HessioFileReader.max_events',
                        extractor='ChargeExtractorFactory.extractor',
                        window_width='ChargeExtractorFactory.window_width',
                        t0='ChargeExtractorFactory.t0',
                        window_shift='ChargeExtractorFactory.window_shift',
                        sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG',
                        sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG',
                        lwt='ChargeExtractorFactory.lwt',
                        clip_amplitude='CameraDL1Calibrator.clip_amplitude',
                        radius='CameraDL1Calibrator.radius',
                        max_pe='ChargeResolutionCalculator.max_pe',
                        T='ChargeResolutionGenerator.telescopes',
                        O='ChargeResolutionGenerator.output_name',
                        ))
    classes = List([HessioFileReader,
                    ChargeExtractorFactory,
                    CameraDL1Calibrator,
                    ChargeResolutionCalculator
                    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.file_reader = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.calculator = None

    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.file_reader = HessioFileReader(**kwargs)

        extractor_factory = ChargeExtractorFactory(**kwargs)
        extractor_class = extractor_factory.get_class()
        extractor = extractor_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.file_reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(extractor=extractor, **kwargs)

        self.calculator = ChargeResolutionCalculator(**kwargs)

    def start(self):
        desc = "Filling Charge Resolution"
        source = self.file_reader.read()
        for event in tqdm(source, desc=desc):
            tels = list(event.dl0.tels_with_data)

            # Check events have true charge included
            if event.count == 0:
                try:
                    if np.all(event.mc.tel[
                                  tels[0]].photo_electron_image == 0):
                        raise KeyError
                except KeyError:
                    self.log.exception('Source does not contain '
                                       'true charge!')
                    raise

            self.r1.calibrate(event)
            self.dl0.reduce(event)
            self.dl1.calibrate(event)

            if self.telescopes:
                tels = []
                for tel in self.telescopes:
                    if tel in event.dl0.tels_with_data:
                        tels.append(tel)

            for telid in tels:
                true_charge = event.mc.tel[telid].photo_electron_image
                measured_charge = event.dl1.tel[telid].image[0]
                self.calculator.add_charges(true_charge, measured_charge)

    def finish(self):
        directory = self.file_reader.output_directory
        name = "{}.h5".format(self.output_name)
        ouput_path = os.path.join(directory, name)
        self.calculator.save(ouput_path)