Ejemplo n.º 1
0
    def __init__(self, n_pixels, n_samples, mapping=None, **kwargs):
        super().__init__(n_pixels, n_samples, **kwargs)

        if mapping is None:
            raise ValueError("A mapping must be passed "
                             "to CtapipeNeighbourPeakIntegrator")

        try:
            from ctapipe.image.extractor import NeighborPeakWindowSum
        except ImportError:
            msg = ("ctapipe not found. Please either install ctapipe or "
                   "disable the columns from WaveformReducer {} ({})".format(
                       self.__class__.__name__, self.columns))
            raise ImportError(msg)

        camera = get_ctapipe_camera_geometry(mapping)

        self.window_size = self.kwargs.get("window_size", 6)
        self.window_shift = self.kwargs.get("window_shift", 3)
        self.integrator = NeighborPeakWindowSum(
            window_shift=self.window_shift,
            window_width=self.window_size,
            lwt=0,
        )
        self.integrator.neighbors = camera.neighbor_matrix_where
Ejemplo n.º 2
0
def main():
    paths = [
        "/Volumes/gct-jason/astri_onsky_archive/d2019-05-15_simulations/proton/run1_dl1.h5",
    ]

    df_list = []

    for ipath, path in enumerate(paths):
        with DL1Reader(path) as reader:
            n_events = reader.get_metadata()['n_events']
            mapping = reader.get_mapping()
            geom = get_ctapipe_camera_geometry(mapping, plate_scale=37.56e-3)

            desc = "Looping over events"
            it = reader.iterate_over_events()
            for df in tqdm(it, total=n_events, desc=desc):
                iev = df['iev'].values[0]

                image = df['photons'].values
                time = df['pulse_time'].values

                mask = obtain_cleaning_mask(geom, image, time)
                if not mask.any():
                    continue

                image_m = image[mask]
                time_m = time[mask]
                geom_m = geom[mask]

                try:
                    hillas = hillas_parameters(geom_m, image_m)
                except HillasParameterizationError:
                    continue

                # timing_parameters(geom_m, image_m, time_m, hillas)

                gt0 = image_m > 0
                pix_x = geom_m.pix_x[gt0]
                pix_y = geom_m.pix_y[gt0]
                peakpos = time_m[gt0]
                intensity = image_m[gt0]

                longi, trans = camera_to_shower_coordinates(
                    pix_x, pix_y, hillas.x, hillas.y, hillas.psi)
                longi = longi.value
                trans = trans.value

                # df_list.append(pd.DataFrame(dict(
                #     ipath=ipath,
                #     iev=iev,
                #     longi=longi,
                #     peakpos=peakpos,
                # )))

                p_relation = RelationPlotter()
                p_relation.plot(longi, peakpos, intensity)
                p_relation.save(
                    get_plot(
                        f"d190524_time_gradient/relation/i{ipath}_e{iev}.pdf"))
Ejemplo n.º 3
0
    def __init__(self, n_pixels, n_samples, mapping=None, **kwargs):
        super().__init__(n_pixels, n_samples, **kwargs)

        if mapping is None:
            raise ValueError("A mapping must be passed "
                             "to SlidingWindowNeighbour")

        try:
            from ctapipe.image.extractor import neighbor_average_waveform
        except ImportError:
            msg = ("ctapipe not found. Please either install ctapipe or "
                   "disable the columns from WaveformReducer {} ({})".format(
                       self.__class__.__name__, self.columns))
            raise ImportError(msg)

        camera = get_ctapipe_camera_geometry(mapping)
        self.neighbor_func = neighbor_average_waveform
        self.neighbors = camera.neighbor_matrix_where
Ejemplo n.º 4
0
def main():
    description = ('Plot the images of the events in a hillas '
                   'list from generate_list_from_hillas.py')
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=Formatter)
    parser.add_argument('-f',
                        '--file',
                        dest='input_path',
                        required=True,
                        help='path to a hillas list file created by '
                        'generate_list_from_hillas')
    parser.add_argument('-n',
                        '--max_events',
                        dest='max_events',
                        type=int,
                        help='number of events to plot')
    args = parser.parse_args()

    input_path = args.input_path
    output = splitext(input_path)[0]
    max_events = args.max_events

    df = pd.read_csv(input_path, sep='\t')

    # Open all files
    hillas_paths = set()
    for _, row in df.iterrows():
        hillas_paths.add(row['path'])
    readers = {
        path: DL1Reader(path.replace("_hillas.h5", "_dl1.h5"))
        for path in hillas_paths
    }

    mapping = readers[list(hillas_paths)[0]].mapping
    # mapping.metadata['size'] *= 1.01
    geom = get_ctapipe_camera_geometry(mapping)
    p_image = ImagePlotter(mapping)

    n_events = df.index.size
    if max_events is not None and n_events > max_events:
        n_events = max_events

    desc = "Looping over events"
    for i, row in tqdm(df.iterrows(), total=n_events, desc=desc):
        if i >= n_events:
            break

        hillas_path = row['path']
        iev = row['iev']
        iobs = row['iobs']
        reader = readers[hillas_path]

        df = reader[iev]
        image_c = df['photons'].values
        image_t = df['pulse_time'].values

        mask = obtain_cleaning_mask(geom, image_c, image_t)
        if not mask.any():
            continue

        try:
            hillas = hillas_parameters(geom[mask], image_c[mask])
        except HillasParameterizationError:
            continue

        p_image.set_image(iev, image_c, image_t, mask=mask, hillas=hillas)

        p_image.save(join(output, f"camera_images/i{i}_r{iobs}_e{iev}.pdf"))
Ejemplo n.º 5
0
def main():
    input_path = "event_list.txt"
    output_path = "cherenkov.npz"

    df = pd.read_csv(input_path, sep='\t')

    first_path = df.iloc[0]['path'].replace("_hillas.h5", "_r1.tio")
    first_reader = TIOReader(first_path)
    n_pixels = first_reader.n_pixels
    n_samples = first_reader.n_samples
    mapping = first_reader.mapping
    mapping.metadata['size'] *= 1.01  # TODO: WHY?!
    reference_pulse_path = first_reader.reference_pulse_path
    geom = get_ctapipe_camera_geometry(mapping)
    charge_extractor = OnskyExtractor(
        n_pixels,
        n_samples,
        mapping=mapping,
        reference_pulse_path=reference_pulse_path,
    )
    time_calibrator = TimeCalibrator()

    # Open all files
    hillas_paths = set()
    for _, row in df.iterrows():
        hillas_paths.add(row['path'])
    readers = dict()
    amplitude_calibrators = dict()
    for path in hillas_paths:
        r1_path = path.replace("_hillas.h5", "_r1.tio")
        reader = TIOReader(r1_path)
        nudge, temperature = get_nudge_and_temperature_from_reader(reader)
        amplitude_calibrator = OnskyAmplitudeCalibrator(nudge, temperature)
        readers[path] = reader
        amplitude_calibrators[path] = amplitude_calibrator

    n_events = df.index.size

    frames_array = []
    min_array = []
    max_array = []

    desc = "Looping over events"
    for i, row in tqdm(df.iterrows(), total=n_events, desc=desc):
        if i >= n_events:
            break

        hillas_path = row['path']
        iev = row['iev']
        iobs = row['iobs']

        reader = readers[hillas_path]
        amplitude_calibrator = amplitude_calibrators[hillas_path]

        waveforms = reader[iev]

        shifted = time_calibrator(waveforms)
        extracted = charge_extractor.process(shifted)
        charge = extracted['charge_onsky']
        time = extracted['t_onsky']
        photons = amplitude_calibrator(charge, np.arange(n_pixels))
        pe = photons * 0.25

        mask = obtain_cleaning_mask(geom, photons, time)
        if not mask.any():
            msg = f"No pixels survived cleaning for: RUN {iobs} IEV {iev}"
            raise ValueError(msg)

        photons_ma = np.ma.masked_array(photons, mask=~mask)

        min_pixel = photons_ma.argmin()
        max_pixel = photons_ma.argmax()

        min_image = -4
        max_image = 0.7 * pe.max()

        min_gf = shifted[max_pixel, :20].min()
        max_gf = shifted[max_pixel].max() * 0.8

        st = int(np.min(time[mask]) - 3)
        et = int(np.max(time[mask]) + 6)
        st = st if st > 0 else 0
        et = et if et < n_samples else n_samples

        frames = shifted[:, st:et:4]
        min_ = np.full(frames.shape[-1], min_gf)
        max_ = np.full(frames.shape[-1], max_gf)

        frames_array.append(frames)
        min_array.append(min_)
        max_array.append(max_)

    np.savez(
        output_path,
        frames=np.column_stack(frames_array),
        min=np.concatenate(min_array),
        max=np.concatenate(max_array),
    )
Ejemplo n.º 6
0
def main():
    description = 'Plot DL1 images'
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=Formatter)
    parser.add_argument('-f',
                        '--file',
                        dest='input_path',
                        required=True,
                        help='path to the HDF5 dl1 file')
    parser.add_argument('-o',
                        '--output',
                        dest='output_dir',
                        help='directory to save the plots')
    parser.add_argument('-s', dest='start_event', help='Start event', type=int)
    parser.add_argument('-m', dest='max_events', help='Max events', type=int)
    args = parser.parse_args()

    input_path = args.input_path
    output = args.output_dir
    start_event = args.start_event
    max_events = args.max_events
    plot_mask = True
    plot_hillas = True

    if output is None:
        output = input_path.replace("_dl1.h5", "_dl1images")

    if start_event is None:
        start_event = 0

    with DL1Reader(input_path) as reader:
        n_events = reader.get_metadata()['n_events']
        mapping = reader.get_mapping()
        geom = get_ctapipe_camera_geometry(mapping)
        if max_events and max_events < n_events:
            n_events = max_events

        p_event = EventPlotter(mapping, geom)
        desc = "Looping over events"
        events = range(start_event, n_events)
        it = enumerate(reader.iterate_over_selected_events(events))
        for ientry, df in tqdm(it, total=n_events - start_event, desc=desc):
            iev = df['iev'].values[0]
            if iev < start_event:
                continue
            if iev >= n_events:
                break

            image_c = df['photons'].values
            image_t = df['pulse_time'].values

            mask = obtain_cleaning_mask(geom, image_c, image_t)
            if not mask.any():
                continue

            try:
                hillas = hillas_parameters(geom[mask], image_c[mask])
            except HillasParameterizationError:
                continue

            p_event.set_image(iev, image_c, image_t, mask=mask, hillas=hillas)

            p_event.save(join(output, f"{iev}.png"))
def main():
    path = get_data("d190717_alpha/wobble.h5")
    with pd.HDFStore(path, mode='r') as store:
        df = store['data'].loc[::4]
        mapping = store['mapping']
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', UserWarning)
            mapping.metadata = store.get_storer('mapping').attrs.metadata

    tc = TimeCalibrator()
    geom = get_ctapipe_camera_geometry(mapping)

    n_row = df.index.size
    p_camera = CameraMovie(mapping, get_plot(
        "d190717_alpha/wobble_animation_goldfish/frames/{:04d}.png"
    ))
    for _, row in tqdm(df.iterrows(), total=n_row):
        timestamp = row['timestamp']
        iobs = row['iobs']
        iev = row['iev']
        x_src = row['x_src']
        y_src = row['y_src']
        dl1 = row['dl1'].values
        time = row['dl1_pulse_time'].values
        r1 = row['r1']
        x_cog = row['x_cog']
        y_cog = row['y_cog']
        psi = row['psi']
        p_camera.set_source_position(x_src, y_src)


        n_pixels, n_samples = r1.shape
        shifted = tc(r1)

        mask = obtain_cleaning_mask(geom, dl1, time)
        if not mask.any():
            msg = f"No pixels survived cleaning for: RUN {iobs} IEV {iev}"
            print(msg)
            continue
            # raise ValueError(msg)

        dl1_ma = np.ma.masked_array(dl1, mask=~mask)

        min_pixel = dl1_ma.argmin()
        max_pixel = dl1_ma.argmax()

        min_image = -4
        max_image = 0.7 * dl1.max()

        min_gf = shifted[max_pixel, :20].min()
        max_gf = shifted[max_pixel].max() * 0.8

        st = int(np.min(time[mask]) - 3)
        et = int(np.max(time[mask]) + 6)
        st = st if st > 0 else 0
        et = et if et < n_samples else n_samples

        # embed()
        p_camera.set_image(dl1, min_image, max_image)

        for t in range(st, et, 3):
            slice_ = shifted[:, t]
            p_camera.set_timestamp(timestamp + pd.Timedelta(f"{t}ns"))
            p_camera.set_goldfish(slice_, min_gf, max_gf)
            p_camera.save_frame()
Ejemplo n.º 8
0
def process(dataset):
    name = dataset.__class__.__name__
    directory = dataset.directory
    event_dict = dataset.events

    output_dir = get_plot(f"d190506_astri_publicity/{name}")
    goldfish_path = join(output_dir, 'goldfish.gif')
    image_path = join(output_dir, 'image.gif')

    first_run = list(event_dict.keys())[0]
    first_reader = TIOReader(join(directory, first_run + "_r1.tio"))
    mapping = first_reader.mapping

    p_image = CameraAnimation.from_mapping(mapping)
    p_image.add_colorbar("Amplitude (Photoelectons)")

    p_goldfish = CameraAnimation.from_mapping(mapping)
    p_goldfish.add_colorbar("Amplitude (mV)")

    for irun, run in enumerate(event_dict.keys()):
        print(f"Processing run: {run} ({irun+1}/{len(event_dict)})")
        path = join(directory, run + "_r1.tio")
        reader = TIOReader(path)
        n_events = reader.n_events
        n_pixels = reader.n_pixels
        n_samples = reader.n_samples
        reference_pulse_path = reader.reference_pulse_path
        geom = get_ctapipe_camera_geometry(mapping, plate_scale=37.56e-3)

        charge_extractor = CrossCorrelationNeighbour(
            n_pixels,
            n_samples,
            mapping=mapping,
            reference_pulse_path=reference_pulse_path,
        )
        time_calibrator = TimeCalibrator()
        nudge, temperature = get_nudge_and_temperature_from_reader(reader)
        amplitude_calibrator = AstriAmplitudeCalibrator(nudge, temperature)

        for event in tqdm(event_dict[run]):
            wfs = reader[event]

            shifted = time_calibrator(wfs)
            extracted = charge_extractor.process(shifted)
            charge = extracted['charge_cc_nn']
            time = extracted['t_cc_nn']
            photons = amplitude_calibrator(charge, np.arange(n_pixels))
            pe = photons * 0.25

            mask = tailcut(geom, photons, time)
            if not mask.any():
                msg = f"No pixels survived cleaning for: {run} {event}"
                # raise ValueError(msg)
                print(msg)
                three_largest = np.argsort(photons)[:-3]
                mask = np.zeros(n_pixels, dtype=np.bool)
                mask[three_largest] = True

            photons_nan = photons
            photons_nan[~mask] = np.nan

            min_pixel = np.nanargmin(photons_nan)
            max_pixel = np.nanargmax(photons_nan)

            min_image = 0
            max_image = 0.7 * pe.max()

            min_goldfish = shifted[max_pixel, :20].min()
            max_goldfish = shifted[max_pixel].max() * 0.8

            p_image.add_image(run, event, 0, pe, min_image, max_image)

            start_time = int(np.min(time[mask]) - 2)
            end_time = int(np.max(time[mask]) + 2)
            for t in range(start_time, end_time):
                if 0 <= t < n_samples:
                    p_goldfish.add_image(run, event, t, shifted[:, t],
                                         min_goldfish, max_goldfish)

    p_image.animate(300, image_path)
    p_goldfish.animate(20, goldfish_path)
Ejemplo n.º 9
0
def main():
    description = 'Loop over R0 or R1 file and plot camera images'
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=Formatter)
    parser.add_argument('-f',
                        '--file',
                        dest='input_path',
                        required=True,
                        help='path to a hillas list file created by '
                        'generate_list_from_hillas')
    parser.add_argument('-n',
                        '--max_events',
                        dest='max_events',
                        type=int,
                        help='number of events to plot')
    args = parser.parse_args()

    input_path = args.input_path
    output_dir = splitext(input_path)[0]
    max_events = args.max_events

    df = pd.read_csv(input_path, sep='\t')

    first_path = df.iloc[0]['path'].replace("_hillas.h5", "_r1.tio")
    first_reader = TIOReader(first_path)
    n_pixels = first_reader.n_pixels
    n_samples = first_reader.n_samples
    mapping = first_reader.mapping
    mapping.metadata['size'] *= 1.01  # TODO: WHY?!
    reference_pulse_path = first_reader.reference_pulse_path
    geom = get_ctapipe_camera_geometry(mapping, plate_scale=37.56e-3)
    charge_extractor = OnskyExtractor(
        n_pixels,
        n_samples,
        mapping=mapping,
        reference_pulse_path=reference_pulse_path,
    )
    time_calibrator = TimeCalibrator()

    # Open all files
    hillas_paths = set()
    for _, row in df.iterrows():
        hillas_paths.add(row['path'])
    readers = dict()
    amplitude_calibrators = dict()
    for path in hillas_paths:
        r1_path = path.replace("_hillas.h5", "_r1.tio")
        reader = TIOReader(r1_path)
        nudge, temperature = get_nudge_and_temperature_from_reader(reader)
        amplitude_calibrator = OnskyAmplitudeCalibrator(nudge, temperature)
        readers[path] = reader
        amplitude_calibrators[path] = amplitude_calibrator

    p_animation = CameraAnimation(mapping)

    n_events = df.index.size
    if max_events is not None and n_events > max_events:
        n_events = max_events

    desc = "Looping over events"
    for i, row in tqdm(df.iterrows(), total=n_events, desc=desc):
        if i >= n_events:
            break

        hillas_path = row['path']
        iev = row['iev']
        iobs = row['iobs']
        tduration = row['tduration']

        reader = readers[hillas_path]
        amplitude_calibrator = amplitude_calibrators[hillas_path]

        waveforms = reader[iev]

        shifted = time_calibrator(waveforms)
        extracted = charge_extractor.process(shifted)
        charge = extracted['charge_onsky']
        time = extracted['t_onsky']
        photons = amplitude_calibrator(charge, np.arange(n_pixels))
        pe = photons * 0.25

        mask = obtain_cleaning_mask(geom, photons, time)
        if not mask.any():
            msg = f"No pixels survived cleaning for: RUN {iobs} IEV {iev}"
            raise ValueError(msg)

        photons_ma = np.ma.masked_array(photons, mask=~mask)

        min_pixel = photons_ma.argmin()
        max_pixel = photons_ma.argmax()

        min_image = -4
        max_image = 0.7 * pe.max()

        min_gf = shifted[max_pixel, :20].min()
        max_gf = shifted[max_pixel].max() * 0.8

        st = int(np.min(time[mask]) - 3)
        et = int(np.max(time[mask]) + 6)
        st = st if st > 0 else 0
        et = et if et < n_samples else n_samples

        p_animation.set_meta(i, iobs, iev, tduration)
        p_animation.set_image(pe, min_image, max_image)
        p_animation.set_waveforms(shifted[:, st:et:4], min_gf, max_gf)
        p_animation.animate(output_dir, interval=50)