def test_container(): cont = Container("data") with pytest.raises(AttributeError): cont.x with pytest.raises(KeyError): cont["x"] with pytest.raises(AttributeError): cont.x = 10 with pytest.raises(TypeError): cont['x'] = 10 # test adding an item cont.add_item('y') assert cont.y is None # test adding item with value: cont.add_item('z', 10) assert cont.z == 10 assert cont['z'] == 10 # test iteration (turn it into a list) assert len(list(cont)) == 2
def init_dl1(event): # Load dl1 container container = Container("calibrated_hessio_container") container.add_item("dl1", RawData()) container.meta.add_item('pixel_pos', dict()) container.meta.pixel_pos = event.meta.pixel_pos return container
def mock_event_source( geoms, events=100, single_tel=False, n_channels=1, n_samples=25, p_trigger=0.3, ): """ An event source that produces array Parameters ---------- geoms : list of CameraGeometry instances Geometries for the telescopes to simulate events : int, default: 100 maximum number of events to create n_channels : int how many channels per telescope n_samples : int how many adc samples per pixel p_trigger : float mean trigger probability for the telescopes """ n_telescopes = len(geoms) container = Container("mock_container") container.meta.add_item('mock__max_events', events) container.meta.add_item('pixel_pos', dict()) container.add_item("dl0", RawData()) container.add_item("count") tel_ids = np.arange(n_telescopes) for event_id in range(events): n_triggered = np.random.poisson(n_telescopes * 0.3) if n_triggered > n_telescopes: n_triggered = n_telescopes triggered_tels = np.random.choice(tel_ids, n_triggered, replace=False) container.dl0.event_id = event_id container.dl0.tels_with_data = triggered_tels container.count = event_id # handle single-telescope case (ignore others: if single_tel: if single_tel not in container.dl0.tels_with_data: continue container.dl0.tels_with_data = [single_tel, ] container.dl0.tel = dict() # clear the previous telescopes t = np.arange(n_samples) for tel_id in container.dl0.tels_with_data: geom = geoms[tel_id] # fill pixel position dictionary, if not already done: if tel_id not in container.meta.pixel_pos: container.meta.pixel_pos[tel_id] = ( geom.pix_x.value, geom.pix_y.value, ) centroid = np.random.uniform(geom.pix_x.min(), geom.pix_y.max(), 2) length = np.random.uniform(0.02, 0.2) width = np.random.uniform(0.01, length) psi = np.random.randint(0, 360) intensity = np.random.poisson(int(10000 * width * length)) model = mock.generate_2d_shower_model( centroid, width, length, '{}d'.format(psi) ) image, _, _ = mock.make_mock_shower_image( geom, model.pdf, intensity, ) container.dl0.tel[tel_id] = RawCameraData(tel_id) container.dl0.tel[tel_id].num_channels = n_channels n_pix = len(geom.pix_id) samples = np.empty((n_pix, n_samples)) means = np.random.normal(15, 1, (n_pix, 1)) stds = np.random.uniform(3, 6, (n_pix, 1)) samples = image[:, np.newaxis] * norm.pdf(t, means, stds) for chan in range(n_channels): container.dl0.tel[tel_id].adc_samples[chan] = samples container.dl0.tel[tel_id].adc_sums[chan] = image yield container
def zfits_event_source(url, data_type='dl0', max_events=None, allowed_tels=None): """A generator that streams data from an EventIO/HESSIO MC data file (e.g. a standard CTA data file.) Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read allowed_tels : list[int] select only a subset of telescope, if None, all are read. This can be used for example emulate the final CTA data format, where there would be 1 telescope per file (whereas in current monte-carlo, they are all interleaved into one file) """ try: zfits = protozfitsreader.ZFile(url) except: raise RuntimeError( "zfits_event_source failed to open '{}'".format(url)) if data_type == 'dl0': counter = 0 eventstream = zfits.move_to_next_event() container = Container("zfits_container") container.meta.add_item('zfits__input', url) container.meta.add_item('zfits__max_events', max_events) container.meta.add_item('pixel_pos', dict()) container.add_item("dl0", RawData()) container.add_item("trig", CentralTriggerData()) container.add_item("count") for run_id, event_id in eventstream: container.dl0.run_id = run_id container.dl0.event_id = event_id #container.dl0.event_num = zfits.get_event_number() # We assume we are in the single-telescope case always: container.dl0.tels_with_data = [ zfits.event.telescopeID, ] container.trig.tels_with_trigger = container.dl0.tels_with_data time_s, time_ns = zfits.get_central_event_gps_time() container.trig.gps_time = Time(time_s * u.s, time_ns * u.ns, format='gps', scale='utc') container.count = counter t = np.arange(n_samples) container.dl0.tel = dict() # clear the previous telescopes # Depecrated loop, we keep it for clarity (similar structure than hessio and mock modules) for tel_id in container.dl0.tels_with_data: # fill pixel position dictionary, if not already done: #TODO: tel_id here is a dummy parameter, we are dealing with single-telescope data!. TBR. if tel_id not in container.meta.pixel_pos: container.meta.pixel_pos[tel_id] = \ zfits.get_pixel_position(tel_id) * u.m nchans = zfits.get_num_channels(tel_id) container.dl0.tel[tel_id] = RawCameraData(tel_id) container.dl0.tel[tel_id].num_channels = nchans for chan in range(nchans): samples = zfits.get_adc_sample(channel=chan, telescope_id=tel_id) integrated = zfits.get_adc_sum(channel=chan, telescope_id=tel_id) container.dl0.tel[tel_id].pixel_samples[ chan] = samples.keys() mask = np.zeros(zfits.get_number_of_pixels(), dtype=bool) mask[np.array(samples.keys())] = True container.dl0.tel[tel_id].adc_samples[chan] = \ ma.array(np.array(samples.values()),mask=mask) mask = np.zeros(zfits.get_number_of_pixels(), dtype=bool) mask[np.array(integrated.keys())] = True container.dl0.tel[tel_id].adc_integrated[chan] = \ ma.array(np.array(integrated.values()),mask=mask) yield container counter += 1 if data_type == 'r1': ''' Fill container with r1 data type ''' counter = 0 eventstream = zfits.move_to_next_event() ## The container container = Container("zfits_container") container.meta.add_item('zfits__input', url) container.meta.add_item('zfits__max_events', max_events) container.add_item("r1", RawData()) container.add_item("count") for run_id, event_id in eventstream: container.r1.run_id = run_id container.r1.event_id = event_id container.r1.tels_with_data = [ zfits.event.telescopeID, ] container.count = counter n_samples = zfits._get_numpyfield( zfits.event.hiGain.waveforms.samples ).shape[0] // zfits._get_numpyfield( zfits.event.hiGain.waveforms.pixelsIndices).shape[0] t = np.arange(n_samples) container.r1.tel = dict() # clear the previous telescopes for tel_id in container.r1.tels_with_data: # Create the camera raw data container container.r1.tel[tel_id] = RawCameraData(tel_id) # Fill it with original fields container.r1.tel[tel_id].num_channels = zfits.event.num_gains container.r1.tel[tel_id].num_pixels = zfits._get_numpyfield( zfits.event.hiGain.waveforms.pixelsIndices).shape[0] container.r1.tel[tel_id].num_samples = n_samples container.r1.tel[tel_id].adc_samples = zfits.get_adcs_samples( telescope_id=tel_id) # Add extra relevant fields # container.r1.tel[tel_id].add_item("pixel_flags") # container.r1.tel[tel_id].pixel_flags = zfits.get_pixel_flags(telescope_id=tel_id) yield container counter += 1 if max_events is not None and counter > max_events: return
""" peds, gains = get_mc_calibration_coeffs(tel_id) return (adcs - peds) * gains if __name__ == '__main__': parser = argparse.ArgumentParser(description='Perform simple Hillas Reco') parser.add_argument('filename', metavar='EVENTIO_FILE', nargs='?', default=get_example_simtelarray_file()) args = parser.parse_args() source = hessio_event_source(args.filename) container = Container("hessio_container") container.meta.add_item('pixel_pos', dict()) container.add_item("dl0", RawData()) container.add_item("mc", MCShowerData()) container.add_item("trig", CentralTriggerData()) container.add_item("count") tel,cam,opt = ID.load(filename=args.filename) ev = 0 efficiency = list() efficiency.append(list()) efficiency.append(list()) efficiency.append(list()) efficiency.append(list()) impact = list()
def zfits_event_source(url, max_events=None): """A generator that streams data from an EventIO/HESSIO MC data file (e.g. a standard CTA data file.) Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read """ try: zfits = protozfitsreader.ZFile(url) except: raise RuntimeError( "zfits_event_source failed to open '{}'".format(url)) counter = 0 eventstream = zfits.move_to_next_event() container = Container() container.meta.add_item('zfits__input', url) container.meta.add_item('zfits__max_events', events) container.meta.add_item('pixel_pos', dict()) container.add_item("r0", R0Container()) container.add_item("trig", CentralTriggerData()) container.add_item("count") for obs_id, event_id in eventstream: container.dl0.obs_id = obs_id container.dl0.event_id = event_id # container.r0.event_num = zfits.get_event_number() # We assume we are in the single-telescope case always: container.dl0.tels_with_data = [ zfits.get_telescope_id(), ] container.trig.tels_with_trigger = container.dl0.tels_with_data time_s, time_ns = zfits.get_central_event_gps_time() container.trig.gps_time = Time(time_s * u.s, time_ns * u.ns, format='gps', scale='utc') container.count = counter container.dl0.tel = dict() # clear the previous telescopes # Depecrated loop, we keep it for clarity (similar structure than hessio # and toymodel modules) for tel_id in container.dl0.tels_with_data: # fill pixel position dictionary, if not already done: # TODO: tel_id here is a dummy parameter, we are dealing with # single-telescope data!. TBR. nchans = zfits.get_num_channels(tel_id) for chan in range(nchans): tel = container.dl0.tel[tel_id] samples = zfits.get_adc_sample(channel=chan, telescope_id=tel_id) integrated = zfits.get_adc_sum(channel=chan, telescope_id=tel_id) tel.pixel_samples[chan] = samples.keys() mask = np.zeros(zfits.get_number_of_pixels(), dtype=bool) mask[np.array(samples.keys())] = True tel.waveform[chan] = ma.array(np.array(samples.values()), mask=mask) mask = np.zeros(zfits.get_number_of_pixels(), dtype=bool) mask[np.array(integrated.keys())] = True tel.adc_integrated[chan] = ma.array(np.array( integrated.values()), mask=mask) yield container counter += 1 if max_events is not None and counter > max_events: return
def test_container_metadata(): cont = Container("data") cont.meta.add_item("version", 2.0) assert cont.meta.version == 2.0
def hessio_event_source(url, max_events=None, single_tel=None): """A generator that streams data from an EventIO/HESSIO MC data file (e.g. a standard CTA data file.) Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read single_tel : int select only a single telescope, if None, all are read. This is to emulate the final CTA data format, where there would be 1 telescope per file (whereas in current monte-carlo, they are all interleaved into one file) """ ret = hessio.file_open(url) if ret is not 0: raise RuntimeError( "hessio_event_source failed to open '{}'".format(url)) counter = 0 eventstream = hessio.move_to_next_event() container = Container("hessio_container") container.meta.add_item('hessio__input', url) container.meta.add_item('hessio__max_events', max_events) container.meta.add_item('pixel_pos', dict()) container.add_item("dl0", RawData()) container.add_item("count") for run_id, event_id in eventstream: container.dl0.run_id = run_id container.dl0.event_id = event_id container.dl0.tels_with_data = hessio.get_teldata_list() container.count = counter # handle single-telescope case (ignore others: if single_tel is not None: if single_tel not in container.dl0.tels_with_data: continue container.dl0.tels_with_data = [ single_tel, ] # this should be done in a nicer way to not re-allocate the # data each time (right now it's just deleted and garbage # collected) container.dl0.tel = dict() # clear the previous telescopes for tel_id in container.dl0.tels_with_data: # fill pixel position dictionary, if not already done: if tel_id not in container.meta.pixel_pos: container.meta.pixel_pos[tel_id] = hessio.get_pixel_position( tel_id) nchans = hessio.get_num_channel(tel_id) container.dl0.tel[tel_id] = RawCameraData(tel_id) container.dl0.tel[tel_id].num_channels = nchans # load the data per telescope/chan for chan in range(nchans): container.dl0.tel[tel_id].adc_samples[chan] \ = hessio.get_adc_sample(channel=chan, telescope_id=tel_id) container.dl0.tel[tel_id].adc_sums[chan] \ = hessio.get_adc_sum(channel=chan, telescope_id=tel_id) yield container counter += 1 if max_events is not None and counter > max_events: return
def hessio_event_source(url, max_events=None, allowed_tels=None): """A generator that streams data from an EventIO/HESSIO MC data file (e.g. a standard CTA data file.) Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read allowed_tels : list[int] select only a subset of telescope, if None, all are read. This can be used for example emulate the final CTA data format, where there would be 1 telescope per file (whereas in current monte-carlo, they are all interleaved into one file) """ ret = pyhessio.file_open(url) if ret is not 0: raise RuntimeError( "hessio_event_source failed to open '{}'".format(url)) counter = 0 eventstream = pyhessio.move_to_next_event() if allowed_tels is not None: allowed_tels = set(allowed_tels) container = Container("hessio_container") container.meta.add_item('hessio__input', url) container.meta.add_item('hessio__max_events', max_events) container.meta.add_item('pixel_pos', dict()) container.meta.add_item('optical_foclen', dict()) container.add_item("dl0", RawData()) container.add_item("mc", MCShowerData()) container.add_item("trig", CentralTriggerData()) container.add_item("count") for run_id, event_id in eventstream: container.dl0.run_id = run_id container.dl0.event_id = event_id container.dl0.tels_with_data = set(pyhessio.get_teldata_list()) # handle telescope filtering by taking the intersection of # tels_with_data and allowed_tels if allowed_tels is not None: selected = container.dl0.tels_with_data & allowed_tels if len(selected) == 0: continue # skip event container.dl0.tels_with_data = selected container.trig.tels_with_trigger \ = pyhessio.get_central_event_teltrg_list() time_s, time_ns = pyhessio.get_central_event_gps_time() container.trig.gps_time = Time(time_s * u.s, time_ns * u.ns, format='gps', scale='utc') container.mc.energy = pyhessio.get_mc_shower_energy() * u.TeV container.mc.alt = Angle(pyhessio.get_mc_shower_altitude(), u.rad) container.mc.az = Angle(pyhessio.get_mc_shower_azimuth(), u.rad) container.mc.core_x = pyhessio.get_mc_event_xcore() * u.m container.mc.core_y = pyhessio.get_mc_event_ycore() * u.m container.count = counter # this should be done in a nicer way to not re-allocate the # data each time (right now it's just deleted and garbage # collected) container.dl0.tel = dict() # clear the previous telescopes for tel_id in container.dl0.tels_with_data: # fill pixel position dictionary, if not already done: if tel_id not in container.meta.pixel_pos: container.meta.pixel_pos[tel_id] \ = pyhessio.get_pixel_position(tel_id) * u.m container.meta.optical_foclen[ tel_id] = pyhessio.get_optical_foclen(tel_id) * u.m nchans = pyhessio.get_num_channel(tel_id) container.dl0.tel[tel_id] = RawCameraData(tel_id) container.dl0.tel[tel_id].num_channels = nchans # load the data per telescope/chan for chan in range(nchans): container.dl0.tel[tel_id].adc_samples[chan] \ = pyhessio.get_adc_sample(channel=chan, telescope_id=tel_id) container.dl0.tel[tel_id].adc_sums[chan] \ = pyhessio.get_adc_sum(channel=chan, telescope_id=tel_id) yield container counter += 1 if max_events is not None and counter > max_events: return