예제 #1
0
def plot_event(event, reco, pdf):
    cams = [
        event.inst.subarray.tels[i].camera for i in event.r0.tels_with_data
    ]
    cams = [c for c in cams if c.cam_id in allowed_cameras]
    n_tels = len(cams)

    p = 1
    params = {}
    pointing_azimuth = {}
    pointing_altitude = {}

    for telescope_id, dl1 in event.dl1.tel.items():
        camera = event.inst.subarray.tels[telescope_id].camera
        if camera.cam_id not in allowed_cameras:
            continue

        nn = int(np.ceil(np.sqrt(n_tels)))
        ax = plt.subplot(nn, nn, p)
        p += 1

        boundary_thresh, picture_thresh = cleaning_level[camera.cam_id]
        mask = tailcuts_clean(camera,
                              dl1.image[0],
                              boundary_thresh=boundary_thresh,
                              picture_thresh=picture_thresh,
                              min_number_picture_neighbors=1)
        #
        if mask.sum() < 3:  # only two pixel remaining. No luck anyways.
            continue

        h = hillas_parameters(
            camera[mask],
            dl1.image[0, mask],
        )

        disp = CameraDisplay(camera, ax=ax, title="CT{0}".format(telescope_id))
        disp.pixels.set_antialiaseds(False)
        disp.autoupdate = False
        disp.add_colorbar()

        # Show the camera image and overlay Hillas ellipse and clean pixels
        disp.image = dl1.image[0]
        disp.cmap = 'viridis'
        disp.highlight_pixels(mask, color='white')
        disp.overlay_moments(h, color='red', linewidth=5)

        pointing_azimuth[
            telescope_id] = event.mc.tel[telescope_id].azimuth_raw * u.rad
        pointing_altitude[
            telescope_id] = event.mc.tel[telescope_id].altitude_raw * u.rad
        params[telescope_id] = h

    return reco.predict(params, event.inst, pointing_altitude,
                        pointing_azimuth)
예제 #2
0
def test_hillas_overlay():
    from ctapipe.visualization import CameraDisplay

    disp = CameraDisplay(CameraGeometry.from_name("LSTCam"))
    hillas = CameraHillasParametersContainer(x=0.1 * u.m,
                                             y=-0.1 * u.m,
                                             length=0.5 * u.m,
                                             width=0.2 * u.m,
                                             psi=90 * u.deg)

    disp.overlay_moments(hillas)
예제 #3
0
def draw_several_cams(geom, ncams=4):

    cmaps = ["jet", "afmhot", "terrain", "autumn"]
    fig, axs = plt.subplots(
        1,
        ncams,
        figsize=(15, 4),
    )

    for ii in range(ncams):
        disp = CameraDisplay(
            geom,
            ax=axs[ii],
            title="CT{}".format(ii + 1),
        )
        disp.cmap = cmaps[ii]

        model = toymodel.Gaussian(
            x=(0.2 - ii * 0.1) * u.m,
            y=(-ii * 0.05) * u.m,
            width=(0.05 + 0.001 * ii) * u.m,
            length=(0.15 + 0.05 * ii) * u.m,
            psi=ii * 20 * u.deg,
        )

        image, _, _ = model.generate_image(
            geom,
            intensity=1500,
            nsb_level_pe=5,
        )

        mask = tailcuts_clean(
            geom,
            image,
            picture_thresh=6 * image.mean(),
            boundary_thresh=4 * image.mean(),
        )
        cleaned = image.copy()
        cleaned[~mask] = 0

        hillas = hillas_parameters(geom, cleaned)

        disp.image = image
        disp.add_colorbar(ax=axs[ii])

        disp.set_limits_percent(95)
        disp.overlay_moments(hillas, linewidth=3, color="blue")
예제 #4
0
def draw_several_cams(geom, ncams=4):

    cmaps = ['jet', 'afmhot', 'terrain', 'autumn']
    fig, axs = plt.subplots(
        1, ncams, figsize=(15, 4), sharey=True, sharex=True
    )

    for ii in range(ncams):
        disp = CameraDisplay(
            geom,
            ax=axs[ii],
            title="CT{}".format(ii + 1),
        )
        disp.cmap = cmaps[ii]

        model = toymodel.generate_2d_shower_model(
            centroid=(0.2 - ii * 0.1, -ii * 0.05),
            width=0.005 + 0.001 * ii,
            length=0.1 + 0.05 * ii,
            psi=ii * 20 * u.deg,
        )

        image, sig, bg = toymodel.make_toymodel_shower_image(
            geom,
            model.pdf,
            intensity=50,
            nsb_level_pe=1000,
        )

        mask = tailcuts_clean(
            geom,
            image,
            picture_thresh=6 * image.mean(),
            boundary_thresh=4 * image.mean()
        )
        cleaned = image.copy()
        cleaned[~mask] = 0

        hillas = hillas_parameters(geom, cleaned)

        disp.image = image
        disp.add_colorbar(ax=axs[ii])

        disp.set_limits_percent(95)
        disp.overlay_moments(hillas, linewidth=3, color='blue')
예제 #5
0
def draw_several_cams(geom, ncams=4):

    cmaps = ['jet', 'afmhot', 'terrain', 'autumn']
    fig, axs = plt.subplots(
        1, ncams, figsize=(15, 4),
    )

    for ii in range(ncams):
        disp = CameraDisplay(
            geom,
            ax=axs[ii],
            title="CT{}".format(ii + 1),
        )
        disp.cmap = cmaps[ii]

        model = toymodel.generate_2d_shower_model(
            centroid=(0.2 - ii * 0.1, -ii * 0.05),
            width=0.05 + 0.001 * ii,
            length=0.15 + 0.05 * ii,
            psi=ii * 20 * u.deg,
        )

        image, sig, bg = toymodel.make_toymodel_shower_image(
            geom,
            model.pdf,
            intensity=1500,
            nsb_level_pe=5,
        )

        mask = tailcuts_clean(
            geom,
            image,
            picture_thresh=6 * image.mean(),
            boundary_thresh=4 * image.mean()
        )
        cleaned = image.copy()
        cleaned[~mask] = 0

        hillas = hillas_parameters(geom, cleaned)

        disp.image = image
        disp.add_colorbar(ax=axs[ii])

        disp.set_limits_percent(95)
        disp.overlay_moments(hillas, linewidth=3, color='blue')
예제 #6
0
def main(filename, config_file=None):

    geom = read_camera_geometries(filename)["LSTCam"]

    dl1_parameters_table = Table.read(filename, path=dl1_params_lstcam_key)
    images_table = Table.read(filename, path=dl1_images_lstcam_key)

    dl1_table = join(
        dl1_parameters_table, images_table, keys=["event_id", "tel_id", "obs_id"]
    )

    params_cleaning = get_cleaning_config(config_file)

    selected_table = dl1_table[np.isfinite(dl1_table["intensity"])]
    selected_table = dl1_table[dl1_table["intensity"] > 500]

    with PdfPages("images_examples.pdf") as pp:

        for ii, row in enumerate(selected_table[:10]):
            h = get_hillas_container(row)

            image = row["image"]
            peak_time = row["peak_time"]

            clean_mask = tailcuts_clean(geom, image, **params_cleaning)

            fig, axes = plt.subplots(1, 2, figsize=(12, 6))
            fig.suptitle(f"event id : {row['event_id']}")

            ax = axes[0]
            display = CameraDisplay(geom, image, ax=ax)
            display.add_colorbar(ax=ax)
            ax.set_title("charges")
            display.highlight_pixels(clean_mask, color="red", alpha=0.33)
            display.overlay_moments(h)

            ax = axes[1]
            display = CameraDisplay(geom, peak_time, ax=ax)
            display.highlight_pixels(clean_mask, color="red", alpha=0.2)
            display.add_colorbar(ax=ax)
            ax.set_title("peak time")

            pp.savefig(dpi=100)
예제 #7
0
if __name__ == "__main__":

    # Load the camera
    geom = CameraGeometry.from_name("LSTCam")
    disp = CameraDisplay(geom)
    disp.add_colorbar()

    # Create a fake camera image to display:
    model = toymodel.Gaussian(
        x=0.2 * u.m, y=0.0 * u.m, width=0.05 * u.m, length=0.15 * u.m, psi="35d"
    )

    image, sig, bg = model.generate_image(geom, intensity=1500, nsb_level_pe=2)

    # Apply image cleaning
    cleanmask = tailcuts_clean(geom, image, picture_thresh=10, boundary_thresh=5)
    clean = image.copy()
    clean[~cleanmask] = 0.0

    # Calculate image parameters
    hillas = hillas_parameters(geom, clean)
    print(hillas)

    # Show the camera image and overlay Hillas ellipse and clean pixels
    disp.image = image
    disp.cmap = "inferno"
    disp.highlight_pixels(cleanmask, color="crimson")
    disp.overlay_moments(hillas, color="cyan", linewidth=1)

    plt.show()
예제 #8
0
                                              psi='35d')

    image, sig, bg = toymodel.make_toymodel_shower_image(geom, model.pdf,
                                                         intensity=50,
                                                         nsb_level_pe=1000)

    # Apply image cleaning
    cleanmask = tailcuts_clean(geom, image, picture_thresh=200,
                               boundary_thresh=100)
    clean = image.copy()
    clean[~cleanmask] = 0.0

    # Calculate image parameters
    hillas = hillas_parameters(geom.pix_x, geom.pix_y, clean)
    print(hillas)

    # Show the camera image and overlay Hillas ellipse and clean pixels
    disp.image = image
    disp.cmap = 'PuOr'
    disp.highlight_pixels(cleanmask, color='black')
    disp.overlay_moments(hillas, color='cyan', linewidth=3)

    # Draw the neighbors of pixel 100 in red, and the neighbor-neighbors in
    # green
    for ii in geom.neighbors[130]:
        draw_neighbors(geom, ii, color='green')

    draw_neighbors(geom, 130, color='cyan', lw=2)

    plt.show()
def main():
    args = parser.parse_args()
    event_generator = fact_event_generator(
        args.inputfile,
        args.drsfile,
        allowed_triggers={4},
    )

    fig = plt.figure(figsize=(12, 6))
    ax1 = fig.add_axes([0, 0, 0.4, 1])
    ax1.set_axis_off()
    divider = make_axes_locatable(ax1)
    cax1 = divider.append_axes('right', size="5%", pad=0.05)

    ax2 = fig.add_axes([0.5, 0.0, 0.4, 1])
    ax2.set_axis_off()
    divider = make_axes_locatable(ax2)
    cax2 = divider.append_axes('right', size="5%", pad=0.05)

    geom = CameraGeometry.from_name('FACT')

    disp1 = CameraDisplay(geom, ax=ax1)
    disp1.add_colorbar(cax=cax1, label='Photons')
    disp2 = CameraDisplay(geom, ax=ax2)
    disp2.add_colorbar(cax=cax2, label='ArrivalTime')

    ax1.set_title('Photons')
    ax2.set_title('Peak Position')

    for e in event_generator:

        dl1_calibrator.calibrate(e)

        image = e.dl1.tel[0].image[0]
        cleaning_mask = tailcuts_clean(geom, image, 5, 3.5)

        if sum(cleaning_mask) < 15:
            continue

        hillas_container = hillas_parameters(
            geom.pix_x[cleaning_mask],
            geom.pix_y[cleaning_mask],
            image[cleaning_mask],
        )

        disp1.overlay_moments(hillas_container,
                              linewidth=1.5,
                              color='c',
                              with_label=False)
        disp1.highlight_pixels(cleaning_mask)

        disp1.image = e.dl1.tel[0].image[0]
        disp2.image = e.dl1.tel[0].peakpos[0]

        for disp in (disp1, disp2):
            disp.highlight_pixels(cleaning_mask, color='r', linewidth=1.5)

        fig.suptitle('FACT Event {}'.format(e.trig.gps_time.iso))

        plt.pause(0.01)
        input('Press enter for next event')
    # get a single image
    for i, img in enumerate(source):
        # Pick a (random) good looking image
        if i > 147:
            break
    im = img.dl1.tel[0].image
    print(img)

    # Apply image cleaning
    cleanmask = tailcuts_clean(
        geom,
        im,
        picture_thresh=30,
        boundary_thresh=5,
        min_number_picture_neighbors=0,
    )

    if sum(cleanmask) == 0:
        pass
    else:
        # Calculate image parameters
        hillas = hillas_parameters(geom[cleanmask], im[cleanmask])

        # Show the camera image and overlay Hillas ellipse and clean pixels
        disp.image = im
        disp.highlight_pixels(cleanmask, color="crimson")
        disp.overlay_moments(hillas, color="red", linewidth=2)

        plt.savefig("build/hillas_example.png", dpi=200)
예제 #11
0
    def start(self):

        disp = None

        for event in tqdm(self.event_source,
                          desc='Tel{}'.format(self.tel),
                          total=self.event_source.max_events,
                          disable=~self.progress):

            self.log.debug(event.trig)
            self.log.debug("Energy: {}".format(event.mc.energy))

            self.calibrator.calibrate(event)

            if disp is None:
                geom = event.inst.subarray.tel[self.tel].camera
                self.log.info(geom)
                disp = CameraDisplay(geom)
                # disp.enable_pixel_picker()
                disp.add_colorbar()
                if self.display:
                    plt.show(block=False)

            # display the event
            disp.axes.set_title('CT{:03d} ({}), event {:06d}'.format(
                self.tel, geom.cam_id, event.r0.event_id))

            if self.samples:
                # display time-varying event
                data = event.dl0.tel[self.tel].pe_samples[self.channel]
                for ii in range(data.shape[1]):
                    disp.image = data[:, ii]
                    disp.set_limits_percent(70)
                    plt.suptitle("Sample {:03d}".format(ii))
                    if self.display:
                        plt.pause(self.delay)
                    if self.write:
                        plt.savefig('CT{:03d}_EV{:10d}_S{:02d}.png'.format(
                            self.tel, event.r0.event_id, ii))
            else:
                # display integrated event:
                im = event.dl1.tel[self.tel].image[self.channel]

                if self.clean:
                    mask = tailcuts_clean(geom,
                                          im,
                                          picture_thresh=10,
                                          boundary_thresh=7)
                    im[~mask] = 0.0

                disp.image = im

                if self.hillas:
                    try:
                        ellipses = disp.axes.findobj(Ellipse)
                        if len(ellipses) > 0:
                            ellipses[0].remove()

                        params = hillas_parameters(geom, image=im)
                        disp.overlay_moments(params,
                                             color='pink',
                                             lw=3,
                                             with_label=False)
                    except HillasParameterizationError:
                        pass

                if self.display:
                    plt.pause(self.delay)
                if self.write:
                    plt.savefig('CT{:03d}_EV{:010d}.png'.format(
                        self.tel, event.r0.event_id))

        self.log.info("FINISHED READING DATA FILE")

        if disp is None:
            self.log.warning(
                'No events for tel {} were found in {}. Try a '
                'different EventIO file or another telescope'.format(
                    self.tel, self.infile), )

        pass
                       boundary_thresh=boundary,
                       picture_thresh=picture,
                       min_number_picture_neighbors=min_num_neighbors)
cleaned_image = image.copy()
cleaned_image[~clean] = 0.0

hillas_c = hillas_parameters(camera[clean], image[clean])
hillas_containers[telescope_id] = hillas_c

if len(hillas_containers) < 2:
    print("break")

length = np.linspace(-1, 1, 30)
length = u.Quantity(length, u.m, copy=False)

d3 = CameraDisplay(camera, ax=axs[2])
d3.image = cleaned_image
d3.axes.scatter(0, 0, color="red", s=50)

d3.overlay_moments(hillas_containers[telescope_id],
                   color="red",
                   with_label=False,
                   lw=2,
                   keep_old=True)
x = hillas_containers[telescope_id].x + length * np.cos(
    hillas_containers[telescope_id].psi)
y = hillas_containers[telescope_id].y + length * np.sin(
    hillas_containers[telescope_id].psi)
d3.axes.plot(x, y)
plt.show()
예제 #13
0
    def start(self):

        # Get first event information
        first_event = self.reader.get_event(0)
        n_pixels = first_event.inst.num_pixels[0]
        n_samples = first_event.r0.tel[0].num_samples
        pos = first_event.inst.pixel_pos[0]
        foclen = first_event.inst.optical_foclen[0]
        geom = CameraGeometry.guess(*pos, foclen)

        # Setup Output
        output_dir = self.reader.output_directory
        title = self.reader.filename
        title = title[:title.find("_")]
        # Prepare Output
        if not exists(output_dir):
            self.log.info("Creating directory: {}".format(output_dir))
            makedirs(output_dir)
        output_path = join(output_dir, title + "_events.pdf")

        # Setup plot
        fig = plt.figure(figsize=(10, 10))
        ax_camera = fig.add_subplot(1, 1, 1)
        fig.patch.set_visible(False)
        ax_camera.axis('off')
        camera = CameraDisplay(geom,
                               ax=ax_camera,
                               image=np.zeros(2048),
                               cmap='viridis')
        camera.add_colorbar()
        cb = camera.colorbar
        camera.colorbar.set_label("Amplitude (p.e.)")
        fig.suptitle(title)

        source = self.reader.read()
        desc = "Looping through file"
        with PdfPages(output_path) as pdf:
            for event in tqdm(source, desc=desc):
                ev = event.count
                event_id = event.r0.event_id
                self.r1.calibrate(event)
                self.dl0.reduce(event)
                self.dl1.calibrate(event)
                for t in event.r0.tels_with_data:
                    dl1 = event.dl1.tel[t].image[0]

                    # Cleaning
                    tc = tailcuts_clean(geom, dl1, 20, 10)
                    if not tc.any():
                        continue
                    cleaned_dl1 = np.ma.masked_array(dl1, mask=~tc)

                    try:
                        # hillas = hillas_parameters(*pos, cleaned_tc)
                        hillas = hillas_parameters_4(*pos, cleaned_dl1)
                    except HillasParameterizationError:
                        continue

                    ax_camera.cla()
                    camera = CameraDisplay(geom,
                                           ax=ax_camera,
                                           image=np.zeros(2048),
                                           cmap='viridis')
                    camera.colorbar = cb
                    camera.image = dl1
                    max_ = cleaned_dl1.max()  # np.percentile(dl1, 99.9)
                    min_ = np.percentile(dl1, 0.1)
                    camera.set_limits_minmax(min_, max_)
                    camera.highlight_pixels(tc, 'white')
                    camera.overlay_moments(hillas, color='red')
                    camera.update(True)
                    ax_camera.set_title("Event: {}".format(event_id))
                    ax_camera.axis('off')

                    pdf.savefig(fig)

        self.log.info("Created images: {}".format(output_path))
예제 #14
0
                    # ax2 = fig.add_subplot(222)
                    ax2 = fig.add_subplot(131)
                    disp2 = CameraDisplay(camera, image=pmt_signal, ax=ax2)
                    disp2.cmap = plt.cm.inferno
                    disp2.add_colorbar(label="signal")
                    plt.title("calibrated noisy image")

                    # ax3 = fig.add_subplot(223)
                    ax3 = fig.add_subplot(132)
                    disp3 = CameraDisplay(new_geom_t,
                                          image=np.sqrt(pmt_signal_t),
                                          ax=ax3)
                    disp3.cmap = plt.cm.inferno
                    disp3.add_colorbar(label="sqrt(signal)")
                    disp3.overlay_moments(hillas['t'],
                                          color='seagreen',
                                          linewidth=3)
                    plt.title(
                        "tailcut cleaned ({},{}) ; alpha = {:4.3f}".format(
                            Cleaner['t'].tail_thresholds[new_geom_t.cam_id][0],
                            Cleaner['t'].tail_thresholds[new_geom_t.cam_id][1],
                            alpha['t']))

                    # ax4 = fig.add_subplot(224)
                    ax4 = fig.add_subplot(133)
                    disp4 = CameraDisplay(
                        new_geom_w,
                        image=np.sqrt(
                            np.sum(pmt_signal_w, axis=1) if pmt_signal_w.
                            shape[-1] == 25 else pmt_signal_w),
                        ax=ax4)
예제 #15
0
class EventViewer():
    def __init__(
            self,
            event_stream,
            camera=DigiCam,
            scale='lin',
            limits_colormap=None,
            limits_readout=None,
            time_bin_start=0,
            pixel_id_start=0
    ):

        matplotlib.figure.autolayout = False
        self.first_call = True
        self.event_stream = event_stream
        self.scale = scale
        if limits_colormap is not None:
            self.limits_colormap = limits_colormap
        else:
            self.limits_colormap = [-np.inf, np.inf]
        self.limits_readout = limits_readout
        self.time_bin = time_bin_start
        self.pixel_id = pixel_id_start
        self.mask_pixels = False
        self.hillas = False

        self.event_clicked_on = EventClicked(pixel_start=self.pixel_id)
        self.camera = camera
        self.n_pixels = len(self.camera.Pixels)
        self.cluster_matrix = np.zeros(
            (len(self.camera.Clusters_7), len(self.camera.Clusters_7))
        )
        for cluster in self.camera.Clusters_7:
            for patch in cluster.patchesID:
                self.cluster_matrix[cluster.ID, patch] = 1

        self.event_id = None
        self.r0_container = None
        self.r1_container = None
        self.dl0_container = None
        self.dl1_container = None
        self.dl2_container = None
        self.trigger_output = None
        self.trigger_input = None
        self.trigger_patch = None
        self.n_samples = None
        self.adc_samples = None
        self.nsb = [np.nan] * self.n_pixels
        self.gain_drop = [np.nan] * self.n_pixels
        self.baseline = [np.nan] * self.n_pixels
        self.std = [np.nan] * self.n_pixels
        self.flag = None
        self.readout_view_types = [
            'raw', 'baseline substracted', 'photon', 'trigger input',
            'trigger output', 'cluster 7', 'reconstructed charge'
        ]
        self.readout_view_type = 'raw'
        self.camera_view_types = ['sum', 'std', 'mean', 'max', 'time']
        self.camera_view_type = 'std'

        self.figure = plt.figure(figsize=(20, 10))
        self.axis_readout = self.figure.add_subplot(122)
        self.axis_camera = self.figure.add_subplot(121)
        self.axis_camera.axis('off')
        self.axis_readout.set_xlabel('t [ns]')
        self.axis_readout.set_ylabel('[ADC]')
        self.axis_readout.legend(loc='upper right')
        self.axis_readout.yaxis.set_major_formatter(FormatStrFormatter('%d'))
        self.axis_readout.yaxis.set_major_locator(
            MaxNLocator(integer=True, nbins=10)
        )
        self.trace_readout = None
        self.trace_time_plot, = self.axis_readout.plot(
            np.array([self.time_bin, self.time_bin]) * 4,
            np.ones(2),
            color='k',
            linestyle='--'
        )
        self.camera_visu = CameraDisplay(
            self.camera.geometry,
            ax=self.axis_camera,
            title='',
            norm=self.scale,
            cmap='viridis',
            allow_pick=True
        )
        self.camera_visu.image = np.zeros(self.n_pixels)
        self.camera_visu.cmap.set_bad(color='k')
        self.camera_visu.add_colorbar(
            orientation='horizontal', pad=0.03, fraction=0.05, shrink=.85
        )
        self.camera_visu.colorbar.set_label('[LSB]')
        self.camera_visu.axes.get_xaxis().set_visible(False)
        self.camera_visu.axes.get_yaxis().set_visible(False)
        self.camera_visu.on_pixel_clicked = self.draw_readout
        self.camera_visu.pixels.set_snap(False)  # snap cursor to pixel center

        # Buttons

        self.axis_next_event_button = self.figure.add_axes(
            [0.35, 0.9, 0.15, 0.07], zorder=np.inf
        )
        self.axis_next_camera_view_button = self.figure.add_axes(
            [0., 0.85, 0.1, 0.15], zorder=np.inf
        )
        self.axis_next_view_type_button = self.figure.add_axes(
            [0., 0.18, 0.1, 0.15], zorder=np.inf
        )
        self.axis_check_button = self.figure.add_axes(
            [0.35, 0.18, 0.1, 0.1], zorder=np.inf
        )
        self.axis_next_camera_view_button.axis('off')
        self.axis_next_view_type_button.axis('off')
        self.button_next_event = Button(self.axis_next_event_button, 'Next')
        self.radio_button_next_camera_view = RadioButtons(
            self.axis_next_camera_view_button,
            self.camera_view_types,
            active=self.camera_view_types.index(self.camera_view_type)
        )
        self.radio_button_next_view_type = RadioButtons(
            self.axis_next_view_type_button,
            self.readout_view_types,
            active=self.readout_view_types.index(self.readout_view_type)
        )
        self.check_button = CheckButtons(
            self.axis_check_button, ('mask', 'hillas'),
            (self.mask_pixels, self.hillas)
        )
        self.radio_button_next_view_type.set_active(
            self.readout_view_types.index(self.readout_view_type)
        )
        self.radio_button_next_camera_view.set_active(
            self.camera_view_types.index(self.camera_view_type)
        )

    def next(self, event=None, step=1):
        for i, event in zip(range(step), self.event_stream):
            pass
        telescope_id = event.r0.tels_with_data[0]
        self.event_id = event.r0.tel[telescope_id].camera_event_number
        self.r0_container = event.r0.tel[telescope_id]
        self.r1_container = event.r1.tel[telescope_id]
        self.dl0_container = event.dl0.tel[telescope_id]
        self.dl1_container = event.dl1.tel[telescope_id]
        self.dl2_container = event.dl2
        self.adc_samples = self.r0_container.adc_samples
        if hasattr(self.r0_container, 'trigger_output_patch7'):
            self.trigger_output = self.r0_container.trigger_output_patch7
        if hasattr(self.r0_container, 'trigger_input_traces'):
            self.trigger_input = self.r0_container.trigger_input_traces
        self.n_samples = self.adc_samples.shape[1]
        if self.trace_readout is None:
            self.trace_readout, = self.axis_readout.step(
                np.arange(self.n_samples) * 4,
                np.ones(self.n_samples),
                where='mid'
            )
        try:
            if np.isnan(self.r0_container.digicam_baseline).all():
                self.baseline = self.r0_container.baseline
            else:
                self.baseline = self.r0_container.digicam_baseline
            zero_image = np.zeros((self.n_pixels, self.n_samples))
            if self.r0_container.standard_deviation is not None:
                self.std = self.r0_container.standard_deviation
            else:
                self.std = np.nan * zero_image
            if self.r0_container.camera_event_type is not None:
                self.flag = self.r0_container.camera_event_type
            else:
                self.flag = np.nan
            if self.r1_container.nsb is not None:
                self.nsb = self.r1_container.nsb
            else:
                self.nsb = np.nan * zero_image
            if self.r1_container.gain_drop is not None:
                self.gain_drop = self.r1_container.gain_drop
            else:
                self.gain_drop = np.nan * zero_image
        except:
            pass
        if self.first_call:
            self.first_call = False
        self.update()

    def update(self):
        self.draw_readout(self.pixel_id)
        self.draw_camera()
        self.button_next_event.label.set_text(
            'Next : current event {}'.format(self.event_id))

    def draw(self):
        self.next()
        self.button_next_event.on_clicked(self.next)
        self.radio_button_next_camera_view.on_clicked(self.next_camera_view)
        self.radio_button_next_view_type.on_clicked(self.next_view_type)
        self.check_button.on_clicked(self.draw_on_camera)
        self.figure.canvas.mpl_connect('key_press_event', self.press)
        self.camera_visu._on_pick(self.event_clicked_on)
        plt.show()

    def draw_camera(self, plot_hillas=False):
        image = self.compute_image()
        if image is None:
            print("Warning: unable to compute image in draw_camera()")
            return
        self.camera_visu.image = image
        if plot_hillas:
            self.camera_visu.overlay_moments(self.dl2_container.shower)

    def draw_readout(self, pixel):
        trace = self.compute_trace()
        if trace is None:
            print('WARNING: unable to compute trace in draw_readout()')
            return
        y = trace[pixel]
        if self.limits_readout is not None:
            limits_y = self.limits_readout
        else:
            limits_y = [np.min(y), np.max(y) + 10]
        self.pixel_id = pixel
        self.event_clicked_on.ind[-1] = self.pixel_id
        self.trace_readout.set_ydata(y)

        legend = ''
        try:
            legend += ' flag = {},'.format(self.flag)
        except:
            pass
        legend += ' pixel = {},'.format(self.pixel_id)
        legend += ' bin = {} \n'.format(self.time_bin)
        try:
            legend += ' B = {:0.2f} [LSB],'.format(
                self.baseline[self.pixel_id]
            )
        except:
            pass
        try:
            legend += ' $\sigma = $ {:0.2f} [LSB] \n'.format(
                self.std[self.pixel_id]
            )
        except:
            pass
        try:
            legend += ' $G_{{drop}} = $ {:0.2f},'.format(
                self.gain_drop[self.pixel_id]
            )
            legend += ' $f_{{nsb}} = $ {:0.2f} [GHz]'.format(
                self.nsb[self.pixel_id]
            )
        except:
            pass
        self.trace_readout.set_label(legend)
        self.trace_time_plot.set_ydata(limits_y)
        self.trace_time_plot.set_xdata(self.time_bin * 4)
        self.axis_readout.set_ylim(limits_y)
        self.axis_readout.legend(loc='upper right')
        if self.readout_view_type in ['photon', 'reconstructed charge']:
            self.axis_readout.set_ylabel('[p.e.]')
        else:
            self.axis_readout.set_ylabel('[LSB]')

    def compute_trace(self):
        image = None
        if self.readout_view_type in self.readout_view_types:
            if self.readout_view_type == 'raw':
                image = self.adc_samples
            elif (
                            self.readout_view_type == 'trigger output' and
                            self.trigger_output is not None
            ):
                image = np.array(
                    [
                        self.trigger_output[pixel.patch]
                        for pixel in self.camera.Pixels
                    ]
                )
            elif (
                            self.readout_view_type == 'trigger input' and
                            self.trigger_input is not None
            ):
                image = np.array(
                    [
                        self.trigger_input[pixel.patch]
                        for pixel in self.camera.Pixels
                    ]
                )
            elif (
                            self.readout_view_type == 'cluster 7' and
                            self.trigger_input is not None
            ):
                trigger_input_patch = np.dot(
                    self.cluster_matrix, self.trigger_input
                )
                image = np.array(
                    [
                        trigger_input_patch[pixel.patch]
                        for pixel in self.camera.Pixels
                    ]
                )
            elif (
                            self.readout_view_type == 'photon' and
                            self.dl1_container.pe_samples_trace is not None
            ):
                image = self.dl1_container.pe_samples_trace
            elif (
                            self.readout_view_type == 'baseline substracted' and
                            self.r1_container.adc_samples is not None
            ):
                image = self.adc_samples - self.baseline[:, np.newaxis]
            elif (
                                self.readout_view_type == 'reconstructed charge' and
                                self.dl1_container.time_bin is not None or
                            self.dl1_container.pe_samples is not None
            ):
                image = np.zeros((self.n_pixels, self.n_samples))
                time_bins = self.dl1_container.time_bin
                image[time_bins] = self.dl1_container.pe_samples
            else:
                image = np.zeros((self.n_pixels, self.n_samples))
            if image is None:
                print('WARNING: unexpected error in compute_trace()',
                      'with readout_view_type=', self.readout_view_type)
        else:
            print('WARNING: requested view type (', self.readout_view_type,
                  'is not in the available view_types:',
                  self.readout_view_types)
        return image

    def next_camera_view(self, camera_view, event=None):
        self.camera_view_type = camera_view
        if self.readout_view_type in ['photon', 'reconstructed charge']:
            self.camera_visu.colorbar.set_label('[p.e.]')
        else:
            self.camera_visu.colorbar.set_label('[LSB]')
        self.update()

    def next_view_type(self, view_type, event=None):
        self.readout_view_type = view_type
        if view_type in ['photon', 'reconstructed charge']:
            self.camera_visu.colorbar.set_label('[p.e.]')
        else:
            self.camera_visu.colorbar.set_label('[LSB]')
        self.update()

    def draw_on_camera(self, to_draw_on, event=None):
        if to_draw_on == 'hillas':
            if self.hillas:
                self.hillas = False
            else:
                self.hillas = True
        if to_draw_on == 'mask':
            if self.mask_pixels:
                self.mask_pixels = False
            else:
                self.mask_pixels = True
        self.update()

    def set_time(self, time):
        if time < self.n_samples and time >= 0:
            self.time_bin = time
            self.update()

    def set_pixel(self, pixel_id):
        if pixel_id < self.n_samples and pixel_id >= 0:
            self.pixel_id = pixel_id
            self.update()

    def compute_image(self):
        image = self.compute_trace()
        if image is None:
            print('WARNING: unable to compute trace in compute_image()')
            return None
        if self.camera_view_type in self.camera_view_types:
            if self.camera_view_type == 'mean':
                self.image = np.mean(image, axis=1)
            elif self.camera_view_type == 'std':
                self.image = np.std(image, axis=1)
            elif self.camera_view_type == 'max':
                self.image = np.max(image, axis=1)
            elif self.camera_view_type == 'sum':
                self.image = np.sum(image, axis=1)
            elif self.camera_view_type == 'time':
                self.image = image[:, self.time_bin]
        else:
            print('Cannot compute for camera type : %s' %
                  self.camera_view_type)
        if self.limits_colormap is not None:
            mask = (self.image >= self.limits_colormap[0])
            if not self.limits_colormap[1] == np.inf:
                image[(self.image > self.limits_colormap[1])] = \
                    self.limits_colormap[1]
        if self.mask_pixels:
            mask = mask * self.dl1_container.cleaning_mask
        if self.hillas:
            self.camera_visu.overlay_moments(
                self.dl2_container.shower, color='r', linewidth=4
            )
        else:
            self.camera_visu.clear_overlays()
        return np.ma.masked_where(~mask, self.image)

    def press(self, event):
        sys.stdout.flush()
        if event.key == 'enter':
            self.next()
        if event.key == 'right':
            self.set_time(self.time_bin + 1)
        if event.key == 'left':
            self.set_time(self.time_bin - 1)
        if event.key == '+':
            self.set_pixel(self.pixel_id + 1)
        if event.key == '-':
            self.set_pixel(self.pixel_id - 1)
        if event.key == 'h':
            self.axis_next_event_button.set_visible(False)
        if event.key == 'v':
            self.axis_next_event_button.set_visible(True)
        self.update()
예제 #16
0
    def start(self):

        disp = None

        for event in tqdm(
                self.event_source,
                desc=f"Tel{self.tel}",
                total=self.event_source.max_events,
                disable=~self.progress,
        ):

            self.log.debug(event.trigger)
            self.log.debug(f"Energy: {event.simulation.shower.energy}")

            self.calibrator(event)

            if disp is None:
                geom = self.event_source.subarray.tel[self.tel].camera.geometry
                self.log.info(geom)
                disp = CameraDisplay(geom)
                # disp.enable_pixel_picker()
                disp.add_colorbar()
                if self.display:
                    plt.show(block=False)

            # display the event
            disp.axes.set_title("CT{:03d} ({}), event {:06d}".format(
                self.tel, geom.camera_name, event.index.event_id))

            if self.samples:
                # display time-varying event
                data = event.dl0.tel[self.tel].waveform
                for ii in range(data.shape[1]):
                    disp.image = data[:, ii]
                    disp.set_limits_percent(70)
                    plt.suptitle(f"Sample {ii:03d}")
                    if self.display:
                        plt.pause(self.delay)
                    if self.write:
                        plt.savefig(
                            f"CT{self.tel:03d}_EV{event.index.event_id:10d}"
                            f"_S{ii:02d}.png")
            else:
                # display integrated event:
                im = event.dl1.tel[self.tel].image

                if self.clean:
                    mask = tailcuts_clean(geom,
                                          im,
                                          picture_thresh=10,
                                          boundary_thresh=7)
                    im[~mask] = 0.0

                disp.image = im

                if self.hillas:
                    try:
                        ellipses = disp.axes.findobj(Ellipse)
                        if len(ellipses) > 0:
                            ellipses[0].remove()

                        params = hillas_parameters(geom, image=im)
                        disp.overlay_moments(params,
                                             color="pink",
                                             lw=3,
                                             with_label=False)
                    except HillasParameterizationError:
                        pass

                if self.display:
                    plt.pause(self.delay)
                if self.write:
                    plt.savefig(
                        f"CT{self.tel:03d}_EV{event.index.event_id:010d}.png")

        self.log.info("FINISHED READING DATA FILE")

        if disp is None:
            self.log.warning(
                "No events for tel {} were found in {}. Try a "
                "different EventIO file or another telescope".format(
                    self.tel, self.infile))
예제 #17
0
    def start(self):

        disp = None

        for event in tqdm(self.source,
                          desc='Tel{}'.format(self.tel),
                          total=self.reader.max_events,
                          disable=~self.progress):

            self.log.debug(event.trig)
            self.log.debug("Energy: {}".format(event.mc.energy))

            self.calibrator.calibrate(event)

            if disp is None:
                x, y = event.inst.pixel_pos[self.tel]
                focal_len = event.inst.optical_foclen[self.tel]
                geom = CameraGeometry.guess(x, y, focal_len)
                self.log.info(geom)
                disp = CameraDisplay(geom)
                # disp.enable_pixel_picker()
                disp.add_colorbar()
                if self.display:
                    plt.show(block=False)

            # display the event
            disp.axes.set_title('CT{:03d} ({}), event {:06d}'.format(
                self.tel, geom.cam_id, event.r0.event_id)
            )

            if self.samples:
                # display time-varying event
                data = event.dl0.tel[self.tel].pe_samples[self.channel]
                for ii in range(data.shape[1]):
                    disp.image = data[:, ii]
                    disp.set_limits_percent(70)
                    plt.suptitle("Sample {:03d}".format(ii))
                    if self.display:
                        plt.pause(self.delay)
                    if self.write:
                        plt.savefig('CT{:03d}_EV{:10d}_S{:02d}.png'
                                    .format(self.tel, event.r0.event_id, ii))
            else:
                # display integrated event:
                im = event.dl1.tel[self.tel].image[self.channel]

                if self.clean:
                    mask = tailcuts_clean(geom, im, picture_thresh=10,
                                          boundary_thresh=7)
                    im[~mask] = 0.0

                disp.image = im

                if self.hillas:
                    try:
                        ellipses = disp.axes.findobj(Ellipse)
                        if len(ellipses) > 0:
                            ellipses[0].remove()

                        params = hillas_parameters(pix_x=geom.pix_x,
                                                   pix_y=geom.pix_y, image=im)
                        disp.overlay_moments(params, color='pink', lw=3,
                                             with_label=False)
                    except HillasParameterizationError:
                        pass

                if self.display:
                    plt.pause(self.delay)
                if self.write:
                    plt.savefig('CT{:03d}_EV{:010d}.png'
                                .format(self.tel, event.r0.event_id))

        self.log.info("FINISHED READING DATA FILE")

        if disp is None:
            self.log.warning('No events for tel {} were found in {}. Try a '
                             'different EventIO file or another telescope'
                             .format(self.tel, self.infile),
                             )

        pass
예제 #18
0
def main(dl1_hipecta_filename, dl1_lstchain_filename):

    geom = CameraGeometry.from_name('LSTCam')

    dl1_hipecta = tables.open_file(dl1_hipecta_filename)
    dl1_lstchain = tables.open_file(dl1_lstchain_filename)

    with tables.open_file(dl1_hipecta_filename) as dl1_hipecta:
        hipecta_images = Table(dl1_hipecta.root.dl1.Tel_1.calib_pic.read())
        hipecta_parameters = Table(dl1_hipecta.root.dl1.Tel_1.parameters.read())

    with tables.open_file(dl1_lstchain_filename) as dl1_lstchain:
        simu_table = Table(dl1_lstchain.root.dl1.event.simulation.LST_LSTCam.read())
        lstchain_images = Table(dl1_lstchain.root[dl1_images_lstcam_key].read())


    hipecta = join(hipecta_images, hipecta_parameters, keys='event_id')

    lstchain_table = hstack([lstchain_images, simu_table], join_type='exact')
    lstchain_table.rename_column('tel_id_1', 'tel_id')
    lstchain_table.remove_column('tel_id_2')


    mega_table = join(lstchain_table[lstchain_table['tel_id']==1],
                     hipecta, 
                     uniq_col_name='{table_name}_{col_name}',
                     table_names = ['lstchain', 'hipecta'],
                     keys='event_id'
                    )



    selected_table = mega_table[:30]



    params_cleaning = dict(picture_thresh=6,
                           boundary_thresh=3,
                           keep_isolated_pixels=False,
                           min_number_picture_neighbors=2)




    lstchain_cleaning = np.apply_along_axis(tailcuts_clean_teltype, selected_table['image'], **params_cleaning)
    selected_table.add_column(Column(lstchain_cleaning, dtype=int), name='lstchain_clean_mask')


    with PdfPages(f'compare_lstchain_hipecta_images_{date.today()}.pdf') as pp:

        for ii, row in enumerate(selected_table[:10]):
            print(f"{ii}. event id : {row['event_id']}")
        #     print(row)
            h = get_hillas_container(row)

            image_lstchain = row['image']
            image_hipecta = row['signal']

            clean_mask_ctapipe_on_lstchain = row['lstchain_clean_mask']
            clean_mask_ctapipe_on_hipecta = tailcuts_clean(geom, image_hipecta, **params_cleaning)
            clean_mask_hipecta = row['clean_mask'].astype(bool)

            fig, axes = plt.subplots(2,3, figsize=(12,6))
        #     axes[0,2].remove()
            display = CameraDisplay(geom, image_lstchain, ax=axes[0,0])
            display.add_colorbar(ax=axes[0,0])
            axes[0,0].set_title('lstchain image')
            display = CameraDisplay(geom, clean_mask_ctapipe_on_lstchain, ax=axes[0,1])
        #     display.add_colorbar(ax=axes[0,1])
            display.highlight_pixels(clean_mask_ctapipe_on_lstchain.astype(bool), color='red')
            axes[0,1].set_title('lstchain clean mask')
            display = CameraDisplay(geom, image_hipecta, ax=axes[1,0])
            display.add_colorbar(ax=axes[1,0])
            axes[1,0].set_title('hipecta image')


            display = CameraDisplay(geom, clean_mask_hipecta, ax=axes[1,1])
        #     display.add_colorbar(ax=axes[1,1])
            display.highlight_pixels(clean_mask_ctapipe_on_hipecta, color='red')
            axes[1,1].set_title('hipecta clean mask')
            axes[1,1].text(0.88,0.88,s='cleaning mask\nfrom ctapipe',color='red')
            axes[1,1].text(-1.5, 0.88, s=f'n_islands={row["n_islands"]}', color='black')
            display.overlay_moments(h)


            display = CameraDisplay(geom, row['photo_electron_image'], ax=axes[0,2])
            display.add_colorbar(ax=axes[0,2])
            axes[0,2].set_title('true pe image')
            display.highlight_pixels(clean_mask_ctapipe_on_lstchain.astype(bool), color='red')
            axes[0,2].text(0.88, 0.88, s='cleaning mask\nfrom ctapipe', color='red')

            display = CameraDisplay(geom, row['photo_electron_image'], ax=axes[1,2])
            display.add_colorbar(ax=axes[1,2])
            axes[1,2].set_title('true pe image')
            display.highlight_pixels(clean_mask_hipecta, color='red')
            axes[1,2].text(0.88,0.88,s='cleaning mask\nfrom hipecta',color='red')

            plt.tight_layout()
            pp.savefig(dpi=100)