How to use the ctapipe.image.tailcuts_clean function in ctapipe

To help you get started, we’ve selected a few ctapipe examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cta-observatory / ctapipe / ctapipe / tools / get_training_params.py View on Github external
pix_x, pix_y = event.inst.pixel_pos[tel_id]
            fl = event.inst.optical_foclen[tel_id]
            if tel_id not in self.geoms:
                self.geoms[tel_id] = CameraGeometry.guess(pix_x, pix_y,
                                                          event.inst.optical_foclen[
                                                              tel_id])

            # Transform the pixels positions into nominal coordinates
            camera_coord = CameraFrame(x=pix_x, y=pix_y, z=np.zeros(pix_x.shape) * u.m,
                                       focal_length=fl,
                                       rotation=-1 * self.geoms[tel_id].cam_rotation)
            nom_coord = camera_coord.transform_to(nom_system)
            tx, ty, tz = event.inst.tel_pos[tel_id]

            # Clean image using split level cleaning
            mask = tailcuts_clean(self.geoms[tel_id], pmt_signal,
                                  picture_thresh=self.tail_cut[self.geoms[
                                      tel_id].cam_id][1],
                                  boundary_thresh=self.tail_cut[self.geoms[
                                      tel_id].cam_id][0])

            grd_tel = GroundFrame(x=tx, y=ty, z=tz)
            tilt_tel = grd_tel.transform_to(tilted_system)
            # Perform Hillas parameterisation
            try:
                moments = hillas_parameters(nom_coord.x, nom_coord.y, pmt_signal * mask)

                # ImPACT reconstruction is performed in the tilted system,
                # so we need to transform tel positions


            except HillasParameterizationError as e:
github cta-observatory / ctapipe / examples / plot_hillas_parameters.py View on Github external
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()
github cta-observatory / ctapipe / ctapipe / tools / camdemo.py View on Github external
)

            image, _, _ = model.generate_image(geom, intensity=intens, nsb_level_pe=3,)

            # alternate between cleaned and raw images
            if self._counter == self.cleanframes:
                plt.suptitle("Image Cleaning ON")
                self.imclean = True
            if self._counter == self.cleanframes * 2:
                plt.suptitle("Image Cleaning OFF")
                self.imclean = False
                self._counter = 0
                disp.clear_overlays()

            if self.imclean:
                cleanmask = tailcuts_clean(
                    geom, image, picture_thresh=10.0, boundary_thresh=5.0
                )
                for ii in range(2):
                    dilate(geom, cleanmask)
                image[cleanmask == 0] = 0  # zero noise pixels
                try:
                    hillas = hillas_parameters(geom, image)
                    disp.overlay_moments(
                        hillas,
                        with_label=False,
                        color="red",
                        alpha=0.7,
                        linewidth=2,
                        linestyle="dashed",
                    )
                except HillasParameterizationError:
github cta-observatory / ctapipe / examples / simple_event_writer.py View on Github external
self.event_source,
            desc="EventWriter",
            total=self.event_source.max_events,
            disable=~self.progress,
        ):

            self.calibrator(event)

            for tel_id in event.dl0.tels_with_data:

                geom = self.event_source.subarray.tel[tel_id].camera.geometry
                dl1_tel = event.dl1.tel[tel_id]

                # Image cleaning
                image = dl1_tel.image  # Waiting for automatic gain selection
                mask = tailcuts_clean(geom, image, picture_thresh=10, boundary_thresh=5)
                cleaned = image.copy()
                cleaned[~mask] = 0

                # Image parametrisation
                params = hillas_parameters(geom, cleaned)

                # Save Ids, MC infos and Hillas informations
                self.writer.write(geom.camera_name, [event.r0, event.mc, params])
github cta-observatory / ctapipe / examples / camera_display.py View on Github external
# Load the camera
    geom = CameraGeometry.from_name("LSTCam")
    disp = CameraDisplay(geom)
    disp.add_colorbar()

    # Create a fake camera image to display:
    model = toymodel.generate_2d_shower_model(
        centroid=(0.2, 0.0), width=0.05, length=0.15, psi='35d'
    )

    image, sig, bg = toymodel.make_toymodel_shower_image(
        geom, model.pdf, 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()
github cta-observatory / ctapipe / ctapipe / image / extractor.py View on Github external
Samples in which the waveform peak has been recognized.
            Same specifications as above.
            Shape: (n_pix)
        """
        # STEP 2

        # Apply correction to 1st pass charges
        charge_1stpass = charge_1stpass_uncorrected * correction[selected_gain_channel]

        # Set thresholds for core-pixels depending on telescope
        core_th = self.core_threshold.tel[telid]
        # Boundary thresholds will be half of core thresholds.

        # Preliminary image cleaning with simple two-level tail-cut
        camera_geometry = self.subarray.tel[telid].camera.geometry
        mask_1 = tailcuts_clean(
            camera_geometry,
            charge_1stpass,
            picture_thresh=core_th,
            boundary_thresh=core_th / 2,
            keep_isolated_pixels=False,
            min_number_picture_neighbors=1,
        )
        image_1 = charge_1stpass.copy()
        image_1[~mask_1] = 0

        # STEP 3

        # find all islands using this cleaning
        num_islands, labels = number_of_islands(camera_geometry, mask_1)
        if num_islands == 0:
            image_2 = image_1.copy()  # no islands = image unchanged
github cta-observatory / ctapipe / examples / camera_display_multi.py View on Github external
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")
github cta-observatory / ctapipe / ctapipe / tools / ImPACT_reconstruction.py View on Github external
# Transform the pixels positions into nominal coordinates
            camera_coord = CameraFrame(x=pix_x, y=pix_y, z=np.zeros(pix_x.shape) * u.m,
                                       focal_length=fl,
                                       rotation= -1* self.geoms[tel_id].cam_rotation)

            nom_coord = camera_coord.transform_to(nom_system)
            tx, ty, tz = event.inst.tel_pos[tel_id]

            # ImPACT reconstruction is performed in the tilted system,
            # so we need to transform tel positions
            grd_tel = GroundFrame(x=tx, y=ty, z=tz)
            tilt_tel = grd_tel.transform_to(tilted_system)

            # Clean image using split level cleaning
            mask = tailcuts_clean(self.geoms[tel_id], pmt_signal,
                                  picture_thresh=self.tail_cut[self.geoms[
                                      tel_id].cam_id][1],
                                  boundary_thresh=self.tail_cut[self.geoms[
                                      tel_id].cam_id][0])

            # Perform Hillas parameterisation
            moments = None
            try:
                moments_cam = hillas_parameters(event.inst.pixel_pos[tel_id][0],
                                            event.inst.pixel_pos[tel_id][1],
                                            pmt_signal*mask)

                moments = hillas_parameters(nom_coord.x, nom_coord.y,pmt_signal*mask)

            except HillasParameterizationError as e:
                print(e)
github cta-observatory / ctapipe / examples / plot_array_hillas.py View on Github external
# calculate and plot the hillas params

        for tel_id in event.dl0.tels_with_data:

            # Camera Geometry required for hillas parametrization
            camgeom = subarray.tel[tel_id].camera.geometry

            # note the [0] is for channel 0 which is high-gain channel
            image = event.dl1.tel[tel_id].image
            time = event.dl1.tel[tel_id].peak_time

            # Cleaning  of the image
            cleaned_image = image.copy()

            # create a clean mask of pixels above the threshold
            cleanmask = tailcuts_clean(
                camgeom, image, picture_thresh=10, boundary_thresh=5
            )
            if np.count_nonzero(cleanmask) < 10:
                continue

            # set all rejected pixels to zero
            cleaned_image[~cleanmask] = 0

            # Calculate hillas parameters
            try:
                hillas_dict[tel_id] = hillas_parameters(camgeom, cleaned_image)
            except HillasParameterizationError:
                continue  # skip failed parameterization (normally no signal)

            timing_dict[tel_id] = timing_parameters(
                camgeom, image, time, hillas_dict[tel_id], cleanmask