Beispiel #1
0
def test_camera_display_single():
    """ test CameraDisplay functionality """
    from ..mpl_camera import CameraDisplay

    geom = CameraGeometry.from_name("LSTCam")
    disp = CameraDisplay(geom)
    image = np.random.normal(size=len(geom.pix_x))
    disp.image = image
    disp.add_colorbar()
    disp.cmap = "nipy_spectral"
    disp.set_limits_minmax(0, 10)
    disp.set_limits_percent(95)
    disp.enable_pixel_picker()
    disp.highlight_pixels([1, 2, 3, 4, 5])
    disp.norm = "log"
    disp.norm = "symlog"
    disp.cmap = "rainbow"

    with pytest.raises(ValueError):
        disp.image = np.ones(10)

    with pytest.raises(ValueError):
        disp.add_colorbar()

    disp.add_ellipse(centroid=(0, 0), width=0.1, length=0.1, angle=0.1)
    disp.clear_overlays()
def make_camera_binary_image(image, sigma, clean_bound, death_pixel_ids_list):
    fig, ax = plt.subplots(1, 2, figsize=(14, 7))

    geom = CameraGeometry.from_name('LSTCam-003')
    disp0 = CameraDisplay(geom, ax=ax[0])
    disp0.image = image
    disp0.cmap = plt.cm.gnuplot2
    disp0.add_colorbar(ax=ax[0])
    ax[0].set_title(
        "Cleaning threshold from interleaved pedestal. \n sigma = {}".format(
            sigma),
        fontsize=15)

    disp1 = CameraDisplay(geom, ax=ax[1])
    disp1.image = image
    cmap = matplotlib.colors.ListedColormap(['black', 'red'])
    bounds = [0, clean_bound, 2 * clean_bound]
    norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
    disp1.cmap = cmap
    disp1.add_colorbar(norm=norm,
                       boundaries=bounds,
                       ticks=[0, clean_bound, 2 * clean_bound])
    disp1.set_limits_minmax(0, 2 * clean_bound)
    disp1.highlight_pixels(death_pixel_ids_list, linewidth=3)
    ax[1].set_title("Red pixels - above cleaning tailcut threshold",
                    fontsize=15)
    plt.tight_layout()
    plt.show()
Beispiel #3
0
def transform_and_clean_hex_samples(pmt_samples, cam_geom):

    # rotate all samples in the image to a rectangular image
    rot_geom, rot_samples = convert_geometry_1d_to_2d(
        cam_geom, pmt_samples, cam_geom.cam_id)

    print("rot samples.shape:", rot_samples.shape)

    # rotate the samples back to hex image
    unrot_geom, unrot_samples = convert_geometry_back(rot_geom, rot_samples,
                                                      cam_geom.cam_id)

    global fig
    global cb1, ax1
    global cb2, ax2
    global cb3, ax3
    if fig is None:
        fig = plt.figure(figsize=(10, 10))
    else:
        fig.delaxes(ax1)
        fig.delaxes(ax2)
        fig.delaxes(ax3)
        cb1.remove()
        cb2.remove()
        cb3.remove()

    ax1 = fig.add_subplot(221)
    disp1 = CameraDisplay(rot_geom, image=np.sum(rot_samples, axis=-1), ax=ax1)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("rotated image")
    disp1.cmap = plt.cm.inferno
    disp1.add_colorbar()
    cb1 = disp1.colorbar

    ax2 = fig.add_subplot(222)
    disp2 = CameraDisplay(cam_geom, image=np.sum(pmt_samples, axis=-1), ax=ax2)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("original image")
    disp2.cmap = plt.cm.inferno
    disp2.add_colorbar()
    cb2 = disp2.colorbar

    ax3 = fig.add_subplot(223)
    disp3 = CameraDisplay(unrot_geom, image=np.sum(unrot_samples, axis=-1), ax=ax3)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("de-rotated image")
    disp3.cmap = plt.cm.inferno
    disp3.add_colorbar()
    cb3 = disp3.colorbar

    plt.pause(.1)
    response = input("press return to continue")
    if response != "":
        exit()
Beispiel #4
0
    def start(self):
        geom = None
        imsum = None
        disp = None

        for data in hessio_event_source(self.infile,
                                        allowed_tels=self._selected_tels,
                                        max_events=None):
            if geom is None:
                x, y = data.inst.pixel_pos[self._base_tel]
                flen = data.inst.optical_foclen[self._base_tel]
                geom = CameraGeometry.guess(x, y, flen)
                imsum = np.zeros(shape=x.shape, dtype=np.float)
                disp = CameraDisplay(geom, title=geom.cam_id)
                disp.add_colorbar()
                disp.cmap = 'viridis'

            if len(data.r0.tels_with_data) <= 2:
                continue

            imsum[:] = 0
            for telid in data.r0.tels_with_data:
                imsum += data.r0.tel[telid].adc_sums[0]

            self.log.info("event={} ntels={} energy={}" \
                          .format(data.r0.event_id,
                                  len(data.r0.tels_with_data),
                                  data.mc.energy))
            disp.image = imsum
            plt.pause(0.1)
Beispiel #5
0
    def draw_camera(self, tel, data, axes=None):
        """
        Draw a camera image using the correct geometry.

        Parameters
        ----------
        tel : int
            The telescope you want drawn.
        data : `np.array`
            1D array with length equal to npix.
        axes : `matplotlib.axes.Axes`
            A matplotlib axes object to plot on, or None to create a new one.

        Returns
        -------
        `ctapipe.visualization.CameraDisplay`
        """

        geom = self.get_geometry(tel)
        axes = axes if axes is not None else plt.gca()
        camera = CameraDisplay(geom, ax=axes)
        camera.image = data
        camera.cmap = plt.cm.viridis
        # camera.add_colorbar(ax=axes, label="Amplitude (ADC)")
        # camera.set_limits_percent(95)  # autoscale
        return camera
    def start(self):
        geom = None
        imsum = None
        disp = None

        for event in self.reader:

            self.calibrator(event)

            if geom is None:
                geom = event.inst.subarray.tel[self._base_tel].camera
                imsum = np.zeros(shape=geom.pix_x.shape, dtype=np.float)
                disp = CameraDisplay(geom, title=geom.cam_id)
                disp.add_colorbar()
                disp.cmap = 'viridis'

            if len(event.dl0.tels_with_data) <= 2:
                continue

            imsum[:] = 0
            for telid in event.dl0.tels_with_data:
                imsum += event.dl1.tel[telid].image

            self.log.info("event={} ntels={} energy={}".format(
                event.r0.event_id, len(event.dl0.tels_with_data),
                event.mc.energy))
            disp.image = imsum
            plt.pause(0.1)

            if self.output_suffix is not "":
                filename = "{:020d}{}".format(event.r0.event_id,
                                              self.output_suffix)
                self.log.info(f"saving: '{filename}'")
                plt.savefig(filename)
def camera_image(input_file, ax):
    first_event = input_file.get_event(0)
    geom = CameraGeometry.guess(*first_event.meta.pixel_pos[telid],
                                first_event.meta.optical_foclen[telid])
    camera = CameraDisplay(geom, ax=ax)
    camera.cmap = plt.cm.viridis
    import time

    def update(iframe, source):
        data = next(source)
        print(iframe)
        # data = np.zeros((2048,128))
        # data[iframe,0] = iframe
        camera.image = data
        return camera.pixels,

    source = get_data(input_file)

    anim = FuncAnimation(fig,
                         update,
                         fargs=[source],
                         blit=True,
                         frames=1000,
                         interval=1,
                         repeat=False)

    plt.show()
Beispiel #8
0
    def draw_camera(self, tel, data, axes=None):
        """
        Draw a camera image using the correct geometry.

        Parameters
        ----------
        tel : int
            The telescope you want drawn.
        data : `np.array`
            1D array with length equal to npix.
        axes : `matplotlib.axes.Axes`
            A matplotlib axes object to plot on, or None to create a new one.

        Returns
        -------
        `ctapipe.visualization.CameraDisplay`
        """

        geom = self.get_geometry(tel)
        axes = axes if axes is not None else plt.gca()
        camera = CameraDisplay(geom, ax=axes)
        camera.image = data
        camera.cmap = plt.cm.viridis
        # camera.add_colorbar(ax=axes, label="Amplitude (ADC)")
        # camera.set_limits_percent(95)  # autoscale
        return camera
Beispiel #9
0
def display_event(event):
    """an extremely inefficient display. It creates new instances of
    CameraDisplay for every event and every camera, and also new axes
    for each event. It's hacked, but it works
    """
    print("Displaying... please wait (this is an inefficient implementation)")
    global fig
    ntels = len(event.r0.tels_with_data)
    fig.clear()

    plt.suptitle("EVENT {}".format(event.r0.event_id))

    disps = []

    for ii, tel_id in enumerate(event.r0.tels_with_data):
        print("\t draw cam {}...".format(tel_id))
        nn = int(ceil(sqrt(ntels)))
        ax = plt.subplot(nn, nn, ii + 1)

        geom = event.inst.subarray.tel[tel_id].camera
        disp = CameraDisplay(geom, ax=ax, title="CT{0}".format(tel_id))
        disp.pixels.set_antialiaseds(False)
        disp.autoupdate = False
        disp.cmap = random.choice(cmaps)
        chan = 0
        signals = event.r0.tel[tel_id].adc_sums[chan].astype(float)
        signals -= signals.mean()
        disp.image = signals
        disp.set_limits_percent(95)
        disp.add_colorbar()
        disps.append(disp)

    return disps
Beispiel #10
0
def plot_muon_event(ax, geom, image, centroid, ringrad_camcoord, 
                    ringrad_inner, ringrad_outer, event_id):
    """
    

    Paramenters
    ---------
    ax:               `matplotlib.pyplot.axis`
    geom:             CameraGeometry  
    centroid:         `float` centroid of the muon ring
    ringrad_camcoord: `float` ring radius in camera coordinates
    ringrad_inner:    `float` inner ring radius in camera coordinates
    ringrad_outer:    `float` outer ring radius in camera coordinates
    event_id:         `int` id of the analyzed event

    Returns
    ---------
    ax:               `matplotlib.pyplot.axis`
    """

    disp0 = CameraDisplay(geom, ax=ax)
    disp0.image = image
    disp0.cmap = 'viridis'
    disp0.add_colorbar(ax=ax)
    disp0.add_ellipse(centroid, ringrad_camcoord.value,
                  ringrad_camcoord.value, 0., 0., color="red")
    disp0.add_ellipse(centroid, ringrad_inner.value,
                                    ringrad_inner.value, 0., 0.,
                                    color="magenta")
    disp0.add_ellipse(centroid, ringrad_outer.value,
                                    ringrad_outer.value, 0., 0.,
                                    color="magenta")
    ax.set_title(f"Event {event_id}")

    return ax
Beispiel #11
0
def display_event(event, geoms):
    """an extremely inefficient display. It creates new instances of
    CameraDisplay for every event and every camera, and also new axes
    for each event. It's hacked, but it works
    """
    print("Displaying... please wait (this is an inefficient implementation)")
    global fig
    ntels = len(event.r0.tels_with_data)
    fig.clear()

    plt.suptitle("EVENT {}".format(event.r0.event_id))

    disps = []

    for ii, tel_id in enumerate(event.r0.tels_with_data):
        print("\t draw cam {}...".format(tel_id))
        nn = int(ceil(sqrt(ntels)))
        ax = plt.subplot(nn, nn, ii + 1)

        x, y = event.inst.pixel_pos[tel_id]
        geom = geoms[tel_id]
        disp = CameraDisplay(geom, ax=ax, title="CT{0}".format(tel_id))
        disp.pixels.set_antialiaseds(False)
        disp.autoupdate = False
        disp.cmap = 'afmhot'
        chan = 0
        signals = event.r0.tel[tel_id].adc_sums[chan].astype(float)
        signals -= signals.mean()
        disp.image = signals
        disp.set_limits_percent(95)
        disp.add_colorbar()
        disps.append(disp)

    return disps
def make_camera_image(image):
    plt.figure(figsize=(6, 6))
    geom = CameraGeometry.from_name('LSTCam-003')
    disp = CameraDisplay(geom)
    disp.image = image
    disp.cmap = plt.cm.gnuplot2
    disp.add_colorbar()
    plt.show()
    def remove_star_and_run(self, list_of_file, max_events,
                            noise_pixels_id_list):
        signal_place_after_clean = np.zeros(1855)
        sum_ped_ev = 0
        alive_ped_ev = 0

        for input_file in list_of_file:
            print(input_file)

            r0_r1_calibrator = LSTR0Corrections(pedestal_path=None,
                                                r1_sample_start=3,
                                                r1_sample_end=39)
            reader = LSTEventSource(input_url=input_file,
                                    max_events=max_events)
            for i, ev in enumerate(reader):
                r0_r1_calibrator.calibrate(ev)
                if i % 10000 == 0:
                    print(ev.r0.event_id)

                if ev.lst.tel[1].evt.tib_masked_trigger == 32:
                    sum_ped_ev += 1
                    self.r1_dl1_calibrator(ev)

                    img = ev.dl1.tel[1].image
                    img[noise_pixels_id_list] = 0

                    geom = ev.inst.subarray.tel[1].camera
                    clean = tailcuts_clean(geom, img,
                                           **self.cleaning_parameters)

                    cleaned = img.copy()
                    cleaned[~clean] = 0.0

                    signal_place_after_clean[np.where(clean == True)] += 1

                    if np.sum(cleaned > 0) > 0:
                        alive_ped_ev += 1

        fig, ax = plt.subplots(figsize=(10, 8))
        geom = ev.inst.subarray.tel[1].camera

        disp0 = CameraDisplay(geom, ax=ax)
        disp0.image = signal_place_after_clean / sum_ped_ev
        disp0.highlight_pixels(noise_pixels_id_list, linewidth=3)
        disp0.add_colorbar(ax=ax,
                           label="N times signal remain after cleaning [%]")
        disp0.cmap = 'gnuplot2'
        ax.set_title("{} \n {}/{}".format(
            input_file.split("/")[-1][8:21], alive_ped_ev, sum_ped_ev),
                     fontsize=25)

        print("{}/{}".format(alive_ped_ev, sum_ped_ev))

        ax.set_xlabel(" ")
        ax.set_ylabel(" ")
        plt.tight_layout()
        plt.show()
Beispiel #14
0
    def _display_camera_animation(self):
        #plt.style.use("ggplot")
        fig = plt.figure(num="ctapipe Camera Demo", figsize=(7, 7))
        ax = plt.subplot(111)

        # load the camera
        geom = CameraGeometry.from_name(self.camera)
        disp = CameraDisplay(geom, ax=ax, autoupdate=True, )
        disp.cmap = plt.cm.terrain

        def update(frame):

            centroid = np.random.uniform(-0.5, 0.5, size=2)
            width = np.random.uniform(0, 0.01)
            length = np.random.uniform(0, 0.03) + width
            angle = np.random.uniform(0, 360)
            intens = np.random.exponential(2) * 50
            model = toymodel.generate_2d_shower_model(centroid=centroid,
                                                      width=width,
                                                      length=length,
                                                      psi=angle * u.deg)
            image, sig, bg = toymodel.make_toymodel_shower_image(geom, model.pdf,
                                                                 intensity=intens,
                                                                 nsb_level_pe=5000)

            # 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

            if self.imclean:
                cleanmask = tailcuts_clean(geom, image/80.0)
                for ii in range(3):
                    dilate(geom, cleanmask)
                image[cleanmask == 0] = 0  # zero noise pixels

            self.log.debug("count = {}, image sum={} max={}"
                .format(self._counter, image.sum(), image.max()))
            disp.image = image

            if self.autoscale:
                disp.set_limits_percent(95)
            else:
                disp.set_limits_minmax(-100, 4000)

            disp.axes.figure.canvas.draw()
            self._counter += 1
            return [ax,]

        self.anim = FuncAnimation(fig, update, interval=self.delay,
                                  blit=self.blit)
        plt.show()
Beispiel #15
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)
Beispiel #16
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")
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')
Beispiel #18
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')
    def plot_camera_display(self, image, input_file, noise_pixels_id_list,
                            alive_ped_ev, sum_ped_ev):
        fig, ax = plt.subplots(figsize=(10, 8))
        geom = CameraGeometry.from_name('LSTCam-003')

        disp0 = CameraDisplay(geom, ax=ax)
        disp0.image = image
        disp0.highlight_pixels(noise_pixels_id_list, linewidth=3)
        disp0.add_colorbar(ax=ax,
                           label="N times signal remain after cleaning [%]")
        disp0.cmap = 'gnuplot2'
        ax.set_title("{} \n {}/{}".format(
            input_file.split("/")[-1][8:21], alive_ped_ev, sum_ped_ev),
                     fontsize=25)

        print("{}/{}".format(alive_ped_ev, sum_ped_ev))

        ax.set_xlabel(" ")
        ax.set_ylabel(" ")
        plt.tight_layout()
        plt.show()
    def start(self):
        geom = None
        imsum = None
        disp = None

        for data in hessio_event_source(self.infile,
                                        allowed_tels=self._selected_tels,
                                        max_events=self.max_events):

            self.calibrator.calibrate(data)

            if geom is None:
                x, y = data.inst.pixel_pos[self._base_tel]
                flen = data.inst.optical_foclen[self._base_tel]
                geom = CameraGeometry.guess(x, y, flen)
                imsum = np.zeros(shape=x.shape, dtype=np.float)
                disp = CameraDisplay(geom, title=geom.cam_id)
                disp.add_colorbar()
                disp.cmap = 'viridis'

            if len(data.dl0.tels_with_data) <= 2:
                continue

            imsum[:] = 0
            for telid in data.dl0.tels_with_data:
                imsum += data.dl1.tel[telid].image[0]

            self.log.info("event={} ntels={} energy={}" \
                          .format(data.r0.event_id,
                                  len(data.dl0.tels_with_data),
                                  data.mc.energy))
            disp.image = imsum
            plt.pause(0.1)

            if self.output_suffix is not "":
                filename = "{:020d}{}".format(data.r0.event_id,
                                              self.output_suffix)
                self.log.info("saving: '{}'".format(filename))
                plt.savefig(filename)
    def start(self):
        geom = None
        imsum = None
        disp = None

        for data in hessio_event_source(self.infile,
                                        allowed_tels=self._selected_tels,
                                        max_events=self.max_events):

            self.calibrator.calibrate(data)

            if geom is None:
                x, y = data.inst.pixel_pos[self._base_tel]
                flen = data.inst.optical_foclen[self._base_tel]
                geom = CameraGeometry.guess(x, y, flen)
                imsum = np.zeros(shape=x.shape, dtype=np.float)
                disp = CameraDisplay(geom, title=geom.cam_id)
                disp.add_colorbar()
                disp.cmap = 'viridis'

            if len(data.dl0.tels_with_data) <= 2:
                continue

            imsum[:] = 0
            for telid in data.dl0.tels_with_data:
                imsum += data.dl1.tel[telid].image[0]

            self.log.info("event={} ntels={} energy={}" \
                          .format(data.r0.event_id,
                                  len(data.dl0.tels_with_data),
                                  data.mc.energy))
            disp.image = imsum
            plt.pause(0.1)

            if self.output_suffix is not "":
                filename = "{:020d}{}".format(data.r0.event_id,
                                              self.output_suffix)
                self.log.info("saving: '{}'".format(filename))
                plt.savefig(filename)
Beispiel #22
0
    def start(self):
        geom = None
        imsum = None
        disp = None

        for event in self.reader:

            self.calibrator(event)

            if geom is None:
                geom = event.inst.subarray.tel[self._base_tel].camera
                imsum = np.zeros(shape=geom.pix_x.shape, dtype=np.float)
                disp = CameraDisplay(geom, title=geom.cam_id)
                disp.add_colorbar()
                disp.cmap = 'viridis'

            if len(event.dl0.tels_with_data) <= 2:
                continue

            imsum[:] = 0
            for telid in event.dl0.tels_with_data:
                imsum += event.dl1.tel[telid].image[0]

            self.log.info(
                "event={} ntels={} energy={}".format(
                    event.r0.event_id, len(event.dl0.tels_with_data),
                    event.mc.energy
                )
            )
            disp.image = imsum
            plt.pause(0.1)

            if self.output_suffix is not "":
                filename = "{:020d}{}".format(
                    event.r0.event_id, self.output_suffix
                )
                self.log.info(f"saving: '{filename}'")
                plt.savefig(filename)
    def start(self):
        geom = None
        imsum = None
        disp = None

        for event in self.reader:

            self.calibrator(event)

            if geom is None:
                geom = self.reader.subarray.tel[self._base_tel].camera.geometry
                imsum = np.zeros(shape=geom.pix_x.shape, dtype=np.float64)
                disp = CameraDisplay(geom, title=geom.camera_name)
                disp.add_colorbar()
                disp.cmap = "viridis"

            if len(event.dl0.tel.keys()) <= 2:
                continue

            imsum[:] = 0
            for telid in event.dl0.tel.keys():
                imsum += event.dl1.tel[telid].image

            self.log.info(
                "event={} ntels={} energy={}".format(
                    event.index.event_id,
                    len(event.dl0.tel.keys()),
                    event.simulation.shower.energy,
                )
            )
            disp.image = imsum
            plt.pause(0.1)

            if self.output_suffix != "":
                filename = "{:020d}{}".format(event.index.event_id, self.output_suffix)
                self.log.info(f"saving: '{filename}'")
                plt.savefig(filename)
Beispiel #24
0
def test_convert_geometry():
    filename = get_path("gamma_test.simtel.gz")

    cam_geom = {}

    source = hessio_event_source(filename)

    # testing a few images just for the sake of being thorough
    counter = 5

    for event in source:

        for tel_id in event.dl0.tels_with_data:
            if tel_id not in cam_geom:
                cam_geom[tel_id] = CameraGeometry.guess(
                    event.inst.pixel_pos[tel_id][0],
                    event.inst.pixel_pos[tel_id][1],
                    event.inst.optical_foclen[tel_id])

            # we want to test conversion of hex to rectangular pixel grid
            if cam_geom[tel_id].pix_type is not "hexagonal":
                continue

            print(tel_id, cam_geom[tel_id].pix_type)

            pmt_signal = apply_mc_calibration(
                #event.dl0.tel[tel_id].adc_samples[0],
                event.dl0.tel[tel_id].adc_sums[0],
                event.mc.tel[tel_id].dc_to_pe[0],
                event.mc.tel[tel_id].pedestal[0])

            new_geom, new_signal = convert_geometry_1d_to_2d(
                cam_geom[tel_id],
                pmt_signal,
                cam_geom[tel_id].cam_id,
                add_rot=-2)

            unrot_geom, unrot_signal = convert_geometry_back(
                new_geom,
                new_signal,
                cam_geom[tel_id].cam_id,
                event.inst.optical_foclen[tel_id],
                add_rot=4)

            # if run as main, do some plotting
            if __name__ == "__main__":
                fig = plt.figure()
                plt.style.use('seaborn-talk')

                ax1 = fig.add_subplot(131)
                disp1 = CameraDisplay(
                    cam_geom[tel_id],
                    image=np.sum(pmt_signal, axis=1)
                    if pmt_signal.shape[-1] == 25 else pmt_signal,
                    ax=ax1)
                disp1.cmap = plt.cm.hot
                disp1.add_colorbar()
                plt.title("original geometry")

                ax2 = fig.add_subplot(132)
                disp2 = CameraDisplay(
                    new_geom,
                    image=np.sum(new_signal, axis=2)
                    if new_signal.shape[-1] == 25 else new_signal,
                    ax=ax2)
                disp2.cmap = plt.cm.hot
                disp2.add_colorbar()
                plt.title("slanted geometry")

                ax3 = fig.add_subplot(133)
                disp3 = CameraDisplay(
                    unrot_geom,
                    image=np.sum(unrot_signal, axis=1)
                    if unrot_signal.shape[-1] == 25 else unrot_signal,
                    ax=ax3)
                disp3.cmap = plt.cm.hot
                disp3.add_colorbar()
                plt.title("geometry converted back to hex")

                plt.show()

            # do some tailcuts cleaning
            mask1 = tailcuts_clean(cam_geom[tel_id],
                                   pmt_signal,
                                   1,
                                   picture_thresh=10.,
                                   boundary_thresh=5.)

            mask2 = tailcuts_clean(unrot_geom,
                                   unrot_signal,
                                   1,
                                   picture_thresh=10.,
                                   boundary_thresh=5.)
            pmt_signal[mask1 == False] = 0
            unrot_signal[mask2 == False] = 0
            '''
            testing back and forth conversion on hillas parameters... '''
            try:
                moments1 = hillas_parameters(cam_geom[tel_id].pix_x,
                                             cam_geom[tel_id].pix_y,
                                             pmt_signal)

                moments2 = hillas_parameters(unrot_geom.pix_x,
                                             unrot_geom.pix_y, unrot_signal)
            except (HillasParameterizationError, AssertionError) as e:
                '''
                we don't want this test to fail because the hillas code
                threw an error '''
                print(e)
                counter -= 1
                if counter < 0:
                    return
                else:
                    continue
            '''
            test if the hillas parameters from the original geometry and the
            forth-and-back rotated geometry are close '''
            assert np.allclose([
                moments1.length.value, moments1.width.value, moments1.phi.value
            ], [
                moments2.length.value, moments2.width.value, moments2.phi.value
            ],
                               rtol=1e-2,
                               atol=1e-2)
            counter -= 1
            if counter < 0:
                return
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()
Beispiel #26
0
def plot_pedestals(data_file,
                   pedestal_file,
                   run=0,
                   plot_file="none",
                   tel_id=1,
                   offset_value=400):
    """
     plot pedestal quantities quantities

     Parameters
     ----------
     data_file:   pedestal run

     pedestal_file:   file with drs4 corrections

     run: run number of data to be corrected

     plot_file:  name of output pdf file

     tel_id: id of the telescope

     offset_value: baseline off_set
     """

    # plot open pdf
    if plot_file != "none":
        pp = PdfPages(plot_file)

    plt.rc('font', size=15)

    # r0 calibrator
    r0_calib = LSTR0Corrections(pedestal_path=pedestal_file,
                                offset=offset_value,
                                tel_id=tel_id)

    # event_reader
    reader = event_source(data_file, max_events=1000)
    t = np.linspace(2, 37, 36)

    # configuration for the charge integrator
    charge_config = Config(
        {"FixedWindowSum": {
            "window_start": 12,
            "window_width": 12,
        }})
    # declare the pedestal component
    pedestal = PedestalIntegrator(tel_id=tel_id,
                                  sample_size=1000,
                                  sample_duration=1000000,
                                  charge_median_cut_outliers=[-10, 10],
                                  charge_std_cut_outliers=[-10, 10],
                                  charge_product="FixedWindowSum",
                                  config=charge_config,
                                  subarray=reader.subarray)

    for i, event in enumerate(reader):
        if tel_id != event.r0.tels_with_data[0]:
            raise Exception(
                f"Given wrong telescope id {tel_id}, files has id {event.r0.tels_with_data[0]}"
            )

        # move from R0 to R1
        r0_calib.calibrate(event)

        ok = pedestal.calculate_pedestals(event)
        if ok:
            ped_data = event.mon.tel[tel_id].pedestal
            break

    camera_geometry = reader.subarray.tels[tel_id].camera.geometry
    camera_geometry = camera_geometry.transform_to(EngineeringCameraFrame())
    # plot open pdf
    if plot_file != "none":
        pp = PdfPages(plot_file)

    plt.rc('font', size=15)

    ### first figure
    fig = plt.figure(1, figsize=(12, 24))
    plt.tight_layout()
    n_samples = charge_config["FixedWindowSum"]['window_width']
    fig.suptitle(f"Run {run}, integration on {n_samples} samples", fontsize=25)
    pad = 420

    image = ped_data.charge_median
    mask = ped_data.charge_median_outliers
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera_geometry)
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal [ADC]', rotation=90)
        plt.title(f'{channel[chan]} pedestal [ADC]')
        disp.add_colorbar()

    image = ped_data.charge_std
    mask = ped_data.charge_std_outliers
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera_geometry)
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        # disp.axes.text(lposx, 0, f'{channel[chan]} pedestal std [ADC]', rotation=90)
        plt.title(f'{channel[chan]} pedestal std [ADC]')
        disp.add_colorbar()

    ###  histograms
    for chan in np.arange(2):
        mean_ped = ped_data.charge_mean[chan]
        ped_std = ped_data.charge_std[chan]

        # select good pixels
        select = np.logical_not(mask[chan])

        #fig.suptitle(f"Run {run} channel: {channel[chan]}", fontsize=25)
        pad += 1
        # pedestal charge
        plt.subplot(pad)
        plt.tight_layout()
        plt.ylabel('pixels')
        plt.xlabel(f'{channel[chan]} pedestal')
        median = np.median(mean_ped[select])
        rms = np.std(mean_ped[select])
        label = f"{channel[chan]} Median {median:3.2f}, std {rms:3.2f}"
        plt.hist(mean_ped[select], bins=50, label=label)
        plt.legend()
        pad += 1
        # pedestal std
        plt.subplot(pad)
        plt.ylabel('pixels')
        plt.xlabel(f'{channel[chan]} pedestal std')
        median = np.median(ped_std[select])
        rms = np.std(ped_std[select])
        label = f" Median {median:3.2f}, std {rms:3.2f}"
        plt.hist(ped_std[select], bins=50, label=label)
        plt.legend()

    plt.subplots_adjust(top=0.94)
    if plot_file != "none":

        pp.savefig()

    pix = 0
    pad = 420
    # plot corrected waveforms of first 8 events
    for i, ev in enumerate(reader):
        for chan in np.arange(2):

            if pad == 420:
                # new figure

                fig = plt.figure(ev.index.event_id, figsize=(12, 24))
                fig.suptitle(f"Run {run}, pixel {pix}", fontsize=25)
                plt.tight_layout()
            pad += 1
            plt.subplot(pad)

            plt.subplots_adjust(top=0.92)
            label = f"event {ev.index.event_id}, {channel[chan]}: R0"
            plt.step(t,
                     ev.r0.tel[tel_id].waveform[chan, pix, 2:38],
                     color="blue",
                     label=label)

            r0_calib.subtract_pedestal(ev, tel_id)
            label = "+ pedestal substraction"
            plt.step(t,
                     ev.r1.tel[tel_id].waveform[chan, pix, 2:38],
                     color="red",
                     alpha=0.5,
                     label=label)

            r0_calib.time_lapse_corr(ev, tel_id)
            r0_calib.interpolate_spikes(ev, tel_id)
            label = "+ dt corr + interp. spikes"
            plt.step(t,
                     ev.r1.tel[tel_id].waveform[chan, pix, 2:38],
                     alpha=0.5,
                     color="green",
                     label=label)
            plt.plot([0, 40], [offset_value, offset_value],
                     'k--',
                     label="offset")
            plt.xlabel("time sample [ns]")
            plt.ylabel("counts [ADC]")
            plt.legend()
            plt.ylim([-50, 500])

        if plot_file != "none" and pad == 428:
            pad = 420
            plt.subplots_adjust(top=0.92)
            pp.savefig()

        if i == 8:
            break

    if plot_file != "none":
        pp.close()
def test_convert_geometry():
    filename = get_path("gamma_test.simtel.gz")

    cam_geom = {}

    source = hessio_event_source(filename)

    # testing a few images just for the sake of being thorough
    counter = 5

    for event in source:

        for tel_id in event.dl0.tels_with_data:
            if tel_id not in cam_geom:
                cam_geom[tel_id] = CameraGeometry.guess(
                                        event.inst.pixel_pos[tel_id][0],
                                        event.inst.pixel_pos[tel_id][1],
                                        event.inst.optical_foclen[tel_id])


            # we want to test conversion of hex to rectangular pixel grid
            if cam_geom[tel_id].pix_type is not "hexagonal":
                continue

            print(tel_id, cam_geom[tel_id].pix_type)

            pmt_signal = apply_mc_calibration(
                        #event.dl0.tel[tel_id].adc_samples[0],
                        event.dl0.tel[tel_id].adc_sums[0],
                        event.mc.tel[tel_id].dc_to_pe[0],
                        event.mc.tel[tel_id].pedestal[0])

            new_geom, new_signal = convert_geometry_1d_to_2d(
                cam_geom[tel_id], pmt_signal, cam_geom[tel_id].cam_id, add_rot=-2)

            unrot_geom, unrot_signal = convert_geometry_back(
                new_geom, new_signal, cam_geom[tel_id].cam_id,
                event.inst.optical_foclen[tel_id], add_rot=4)

            # if run as main, do some plotting
            if __name__ == "__main__":
                fig = plt.figure()
                plt.style.use('seaborn-talk')

                ax1 = fig.add_subplot(131)
                disp1 = CameraDisplay(cam_geom[tel_id],
                                      image=np.sum(pmt_signal, axis=1)
                                      if pmt_signal.shape[-1] == 25 else pmt_signal,
                                      ax=ax1)
                disp1.cmap = plt.cm.hot
                disp1.add_colorbar()
                plt.title("original geometry")

                ax2 = fig.add_subplot(132)
                disp2 = CameraDisplay(new_geom,
                                      image=np.sum(new_signal, axis=2)
                                      if new_signal.shape[-1] == 25 else new_signal,
                                      ax=ax2)
                disp2.cmap = plt.cm.hot
                disp2.add_colorbar()
                plt.title("slanted geometry")

                ax3 = fig.add_subplot(133)
                disp3 = CameraDisplay(unrot_geom, image=np.sum(unrot_signal, axis=1)
                                      if unrot_signal.shape[-1] == 25 else unrot_signal,
                                      ax=ax3)
                disp3.cmap = plt.cm.hot
                disp3.add_colorbar()
                plt.title("geometry converted back to hex")

                plt.show()


            # do some tailcuts cleaning
            mask1 = tailcuts_clean(cam_geom[tel_id], pmt_signal, 1,
                                   picture_thresh=10.,
                                   boundary_thresh=5.)

            mask2 = tailcuts_clean(unrot_geom, unrot_signal, 1,
                                   picture_thresh=10.,
                                   boundary_thresh=5.)
            pmt_signal[mask1==False] = 0
            unrot_signal[mask2==False] = 0

            '''
            testing back and forth conversion on hillas parameters... '''
            try:
                moments1 = hillas_parameters(cam_geom[tel_id].pix_x,
                                             cam_geom[tel_id].pix_y,
                                             pmt_signal)

                moments2 = hillas_parameters(unrot_geom.pix_x,
                                             unrot_geom.pix_y,
                                             unrot_signal)
            except (HillasParameterizationError, AssertionError) as e:
                '''
                we don't want this test to fail because the hillas code
                threw an error '''
                print(e)
                counter -= 1
                if counter < 0:
                    return
                else:
                    continue

            '''
            test if the hillas parameters from the original geometry and the
            forth-and-back rotated geometry are close '''
            assert np.allclose(
                [moments1.length.value, moments1.width.value,
                 moments1.phi.value],
                [moments2.length.value, moments2.width.value,
                 moments2.phi.value],
                rtol=1e-2, atol=1e-2)
            counter -= 1
            if counter < 0:
                return
Beispiel #28
0
    def plot(self, input_file, event, telid, chan, extractor_name, nei):
        # Extract required images
        dl0 = event.dl0.tel[telid].adc_samples[chan]
        t_pe = event.mc.tel[telid].photo_electron_image
        dl1 = event.dl1.tel[telid].image[chan]
        max_time = np.unravel_index(np.argmax(dl0), dl0.shape)[1]
        max_charges = np.max(dl0, axis=1)
        max_pix = int(np.argmax(max_charges))
        min_pix = int(np.argmin(max_charges))

        geom = CameraGeometry.guess(*event.inst.pixel_pos[telid],
                                    event.inst.optical_foclen[telid])

        # Get Neighbours
        max_pixel_nei = nei[max_pix]
        min_pixel_nei = nei[min_pix]

        # Get Windows
        windows = event.dl1.tel[telid].extracted_samples[chan]
        length = np.sum(windows, axis=1)
        start = np.argmax(windows, axis=1)
        end = start + length

        # Draw figures
        ax_max_nei = {}
        ax_min_nei = {}
        fig_waveforms = plt.figure(figsize=(18, 9))
        fig_waveforms.subplots_adjust(hspace=.5)
        fig_camera = plt.figure(figsize=(15, 12))

        ax_max_pix = fig_waveforms.add_subplot(4, 2, 1)
        ax_min_pix = fig_waveforms.add_subplot(4, 2, 2)
        ax_max_nei[0] = fig_waveforms.add_subplot(4, 2, 3)
        ax_min_nei[0] = fig_waveforms.add_subplot(4, 2, 4)
        ax_max_nei[1] = fig_waveforms.add_subplot(4, 2, 5)
        ax_min_nei[1] = fig_waveforms.add_subplot(4, 2, 6)
        ax_max_nei[2] = fig_waveforms.add_subplot(4, 2, 7)
        ax_min_nei[2] = fig_waveforms.add_subplot(4, 2, 8)

        ax_img_nei = fig_camera.add_subplot(2, 2, 1)
        ax_img_max = fig_camera.add_subplot(2, 2, 2)
        ax_img_true = fig_camera.add_subplot(2, 2, 3)
        ax_img_cal = fig_camera.add_subplot(2, 2, 4)

        # Draw max pixel traces
        ax_max_pix.plot(dl0[max_pix])
        ax_max_pix.set_xlabel("Time (ns)")
        ax_max_pix.set_ylabel("DL0 Samples (ADC)")
        ax_max_pix.set_title(
            "(Max) Pixel: {}, True: {}, Measured = {:.3f}".format(
                max_pix, t_pe[max_pix], dl1[max_pix]))
        max_ylim = ax_max_pix.get_ylim()
        ax_max_pix.plot([start[max_pix], start[max_pix]],
                        ax_max_pix.get_ylim(),
                        color='r',
                        alpha=1)
        ax_max_pix.plot([end[max_pix], end[max_pix]],
                        ax_max_pix.get_ylim(),
                        color='r',
                        alpha=1)
        for i, ax in ax_max_nei.items():
            if len(max_pixel_nei) > i:
                pix = max_pixel_nei[i]
                ax.plot(dl0[pix])
                ax.set_xlabel("Time (ns)")
                ax.set_ylabel("DL0 Samples (ADC)")
                ax.set_title(
                    "(Max Nei) Pixel: {}, True: {}, Measured = {:.3f}".format(
                        pix, t_pe[pix], dl1[pix]))
                ax.set_ylim(max_ylim)
                ax.plot([start[pix], start[pix]],
                        ax.get_ylim(),
                        color='r',
                        alpha=1)
                ax.plot([end[pix], end[pix]],
                        ax.get_ylim(),
                        color='r',
                        alpha=1)

        # Draw min pixel traces
        ax_min_pix.plot(dl0[min_pix])
        ax_min_pix.set_xlabel("Time (ns)")
        ax_min_pix.set_ylabel("DL0 Samples (ADC)")
        ax_min_pix.set_title(
            "(Min) Pixel: {}, True: {}, Measured = {:.3f}".format(
                min_pix, t_pe[min_pix], dl1[min_pix]))
        ax_min_pix.set_ylim(max_ylim)
        ax_min_pix.plot([start[min_pix], start[min_pix]],
                        ax_min_pix.get_ylim(),
                        color='r',
                        alpha=1)
        ax_min_pix.plot([end[min_pix], end[min_pix]],
                        ax_min_pix.get_ylim(),
                        color='r',
                        alpha=1)
        for i, ax in ax_min_nei.items():
            if len(min_pixel_nei) > i:
                pix = min_pixel_nei[i]
                ax.plot(dl0[pix])
                ax.set_xlabel("Time (ns)")
                ax.set_ylabel("DL0 Samples (ADC)")
                ax.set_title(
                    "(Min Nei) Pixel: {}, True: {}, Measured = {:.3f}".format(
                        pix, t_pe[pix], dl1[pix]))
                ax.set_ylim(max_ylim)
                ax.plot([start[pix], start[pix]],
                        ax.get_ylim(),
                        color='r',
                        alpha=1)
                ax.plot([end[pix], end[pix]],
                        ax.get_ylim(),
                        color='r',
                        alpha=1)

        # Draw cameras
        nei_camera = np.zeros_like(max_charges, dtype=np.int)
        nei_camera[min_pixel_nei] = 2
        nei_camera[min_pix] = 1
        nei_camera[max_pixel_nei] = 3
        nei_camera[max_pix] = 4
        camera = CameraDisplay(geom, ax=ax_img_nei)
        camera.image = nei_camera
        camera.cmap = plt.cm.viridis
        ax_img_nei.set_title("Neighbour Map")
        ax_img_nei.annotate("Pixel: {}".format(max_pix),
                            xy=(geom.pix_x.value[max_pix],
                                geom.pix_y.value[max_pix]),
                            xycoords='data',
                            xytext=(0.05, 0.98),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='red',
                                            width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        ax_img_nei.annotate("Pixel: {}".format(min_pix),
                            xy=(geom.pix_x.value[min_pix],
                                geom.pix_y.value[min_pix]),
                            xycoords='data',
                            xytext=(0.05, 0.94),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='orange',
                                            width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        camera = CameraDisplay(geom, ax=ax_img_max)
        camera.image = dl0[:, max_time]
        camera.cmap = plt.cm.viridis
        camera.add_colorbar(ax=ax_img_max, label="DL0 Samples (ADC)")
        ax_img_max.set_title("Max Timeslice (T = {})".format(max_time))
        ax_img_max.annotate("Pixel: {}".format(max_pix),
                            xy=(geom.pix_x.value[max_pix],
                                geom.pix_y.value[max_pix]),
                            xycoords='data',
                            xytext=(0.05, 0.98),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='red',
                                            width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        ax_img_max.annotate("Pixel: {}".format(min_pix),
                            xy=(geom.pix_x.value[min_pix],
                                geom.pix_y.value[min_pix]),
                            xycoords='data',
                            xytext=(0.05, 0.94),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='orange',
                                            width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')

        camera = CameraDisplay(geom, ax=ax_img_true)
        camera.image = t_pe
        camera.cmap = plt.cm.viridis
        camera.add_colorbar(ax=ax_img_true, label="True Charge (p.e.)")
        ax_img_true.set_title("True Charge")
        ax_img_true.annotate("Pixel: {}".format(max_pix),
                             xy=(geom.pix_x.value[max_pix],
                                 geom.pix_y.value[max_pix]),
                             xycoords='data',
                             xytext=(0.05, 0.98),
                             textcoords='axes fraction',
                             arrowprops=dict(facecolor='red',
                                             width=2,
                                             alpha=0.4),
                             horizontalalignment='left',
                             verticalalignment='top')
        ax_img_true.annotate("Pixel: {}".format(min_pix),
                             xy=(geom.pix_x.value[min_pix],
                                 geom.pix_y.value[min_pix]),
                             xycoords='data',
                             xytext=(0.05, 0.94),
                             textcoords='axes fraction',
                             arrowprops=dict(facecolor='orange',
                                             width=2,
                                             alpha=0.4),
                             horizontalalignment='left',
                             verticalalignment='top')

        camera = CameraDisplay(geom, ax=ax_img_cal)
        camera.image = dl1
        camera.cmap = plt.cm.viridis
        camera.add_colorbar(ax=ax_img_cal,
                            label="Calib Charge (Photo-electrons)")
        ax_img_cal.set_title("Charge (integrator={})".format(extractor_name))
        ax_img_cal.annotate("Pixel: {}".format(max_pix),
                            xy=(geom.pix_x.value[max_pix],
                                geom.pix_y.value[max_pix]),
                            xycoords='data',
                            xytext=(0.05, 0.98),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='red',
                                            width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        ax_img_cal.annotate("Pixel: {}".format(min_pix),
                            xy=(geom.pix_x.value[min_pix],
                                geom.pix_y.value[min_pix]),
                            xycoords='data',
                            xytext=(0.05, 0.94),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='orange',
                                            width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')

        fig_waveforms.suptitle("Integrator = {}".format(extractor_name))
        fig_camera.suptitle("Camera = {}".format(geom.cam_id))

        waveform_output_name = "e{}_t{}_c{}_extractor{}_waveform.pdf"\
            .format(event.count, telid, chan, extractor_name)
        camera_output_name = "e{}_t{}_c{}_extractor{}_camera.pdf"\
            .format(event.count, telid, chan, extractor_name)

        output_dir = self.output_dir
        if output_dir is None:
            output_dir = input_file.output_directory
        output_dir = os.path.join(output_dir, self.name)
        if not os.path.exists(output_dir):
            self.log.info("Creating directory: {}".format(output_dir))
            os.makedirs(output_dir)

        waveform_output_path = os.path.join(output_dir, waveform_output_name)
        self.log.info("Saving: {}".format(waveform_output_path))
        fig_waveforms.savefig(waveform_output_path,
                              format='pdf',
                              bbox_inches='tight')

        camera_output_path = os.path.join(output_dir, camera_output_name)
        self.log.info("Saving: {}".format(camera_output_path))
        fig_camera.savefig(camera_output_path,
                           format='pdf',
                           bbox_inches='tight')
Beispiel #29
0
    #Calculate source position
    mcAlt = hdu_list[1].data.field(4)[i]
    mcAz = hdu_list[1].data.field(5)[i]
    mcAlttel = hdu_list[1].data.field(19)[i]
    mcAztel = hdu_list[1].data.field(20)[i]

    srcpos = Disp.calc_CamSourcePos([mcAlt], [mcAz], [mcAlttel], [mcAztel],
                                    focal_length)

    Source_X = srcpos[0]
    Source_Y = srcpos[1]

    cen_x = hdu_list[1].data.field(16)[i]
    cen_y = hdu_list[1].data.field(17)[i]

    disp = Disp.calc_DISP(Source_X, Source_Y, cen_x, cen_y)

    display = CameraDisplay(geom)
    display.add_colorbar()

    image = hdu_list[2].data[i]

    display.image = image
    display.cmap = 'CMRmap'

    plt.plot([Source_X], [Source_Y], marker='o', markersize=10, color="green")
    plt.plot([cen_x], [cen_y], marker='x', markersize=10, color="blue")
    plt.plot([Source_X, cen_x], [Source_Y, cen_y], '-', color="red")
    plt.show()
Beispiel #30
0
def plot_all(ped_data, ff_data, calib_data, run=0, plot_file="none"):
    """
     plot camera calibration quantities

     Parameters
     ----------
     ped_data:   pedestal container PedestalContainer()

     ff_data:    flat-field container FlatFieldContainer()

     calib_data: calibration container WaveformCalibrationContainer()

     """
    camera = CameraGeometry.from_name("LSTCam", 2)

    # plot open pdf
    if plot_file != "none":
        pp = PdfPages(plot_file)

    plt.rc('font', size=15)

    ### first figure
    fig = plt.figure(1, figsize=(12, 24))
    plt.tight_layout()
    fig.suptitle(f"Run {run}", fontsize=25)
    pad = 420
    image = ff_data.charge_median
    mask = ff_data.charge_median_outliers
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera)
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        #disp.axes.text(lposx, 0, f'{channel[chan]} signal charge (ADC)', rotation=90)
        plt.title(f'{channel[chan]} signal charge [ADC]')
        disp.add_colorbar()

    image = ff_data.charge_std
    mask = ff_data.charge_std_outliers
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera)
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        #disp.axes.text(lposx, 0, f'{channel[chan]} signal std [ADC]', rotation=90)
        plt.title(f'{channel[chan]} signal std [ADC]')
        disp.add_colorbar()

    image = ped_data.charge_median
    mask = ped_data.charge_median_outliers
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera)
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        #disp.axes.text(lposx, 0, f'{channel[chan]} pedestal [ADC]', rotation=90)
        plt.title(f'{channel[chan]} pedestal [ADC]')
        disp.add_colorbar()

    image = ped_data.charge_std
    mask = ped_data.charge_std_outliers
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera)
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        #disp.axes.text(lposx, 0, f'{channel[chan]} pedestal std [ADC]', rotation=90)
        plt.title(f'{channel[chan]} pedestal std [ADC]')
        disp.add_colorbar()

    plt.subplots_adjust(top=0.92)

    if plot_file != "none":
        pp.savefig()

    ### second figure
    fig = plt.figure(2, figsize=(12, 24))
    plt.tight_layout()
    fig.suptitle(f"Run {run}", fontsize=25)
    pad = 420

    # time
    image = ff_data.time_median
    mask = ff_data.time_median_outliers
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        #disp.axes.text(lposx, 0, f'{channel[chan]} time', rotation=90)
        plt.title(f'{channel[chan]} time')
        disp.add_colorbar()

    image = ff_data.relative_gain_median
    mask = calib_data.unusable_pixels
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera)
        disp.highlight_pixels(mask[chan], linewidth=2)
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.image = image[chan]
        disp.cmap = plt.cm.coolwarm
        disp.set_limits_minmax(0.7, 1.3)
        plt.title(f'{channel[chan]} relative gain')
        #disp.axes.text(lposx, 0, f'{channel[chan]} relative gain', rotation=90)
        disp.add_colorbar()

    # pe
    image = calib_data.n_pe
    mask = calib_data.unusable_pixels
    image = np.where(np.isnan(image), 0, image)
    for chan in (np.arange(2)):
        pad += 1
        plt.subplot(pad)
        plt.tight_layout()
        disp = CameraDisplay(camera)
        disp.highlight_pixels(mask[chan], linewidth=2)
        disp.image = image[chan]
        mymin = np.median(image[chan]) - 2 * np.std(image[chan])
        mymax = np.median(image[chan]) + 2 * np.std(image[chan])
        disp.set_limits_minmax(mymin, mymax)
        disp.cmap = plt.cm.coolwarm
        plt.title(f'{channel[chan]} photon-electrons')
        #disp.axes.text(lposx, 0, f'{channel[chan]} photon-electrons', rotation=90)
        disp.add_colorbar()

    # pe histogram
    pad += 1
    plt.subplot(pad)
    plt.tight_layout()
    for chan in np.arange(2):
        n_pe = calib_data.n_pe[chan]
        # select good pixels
        select = np.logical_not(mask[chan])
        median = int(np.median(n_pe[select]))
        rms = np.std(n_pe[select])
        mymin = median - 4 * rms
        mymax = median + 4 * rms
        label = f"{channel[chan]} Median {median:3.2f}, std {rms:5.2f}"
        plt.hist(n_pe[select],
                 label=label,
                 histtype='step',
                 range=(mymin, mymax),
                 bins=50,
                 stacked=True,
                 alpha=0.5,
                 fill=True)
        plt.legend()
    plt.xlabel(f'pe', fontsize=20)
    plt.ylabel('pixels', fontsize=20)

    # pe scatter plot
    pad += 1
    plt.subplot(pad)
    plt.tight_layout()
    HG = calib_data.n_pe[0]
    LG = calib_data.n_pe[1]
    HG = np.where(np.isnan(HG), 0, HG)
    LG = np.where(np.isnan(LG), 0, LG)
    mymin = np.median(LG) - 2 * np.std(LG)
    mymax = np.median(LG) + 2 * np.std(LG)
    plt.hist2d(LG, HG, bins=[100, 100])
    plt.xlabel("LG", fontsize=20)
    plt.ylabel("HG", fontsize=20)

    x = np.arange(mymin, mymax)
    plt.plot(x, x)
    plt.ylim(mymin, mymax)
    plt.xlim(mymin, mymax)
    plt.subplots_adjust(top=0.92)
    if plot_file != "none":
        pp.savefig()

    ### figures 3 and 4 : histograms
    for chan in np.arange(2):
        n_pe = calib_data.n_pe[chan]

        gain_median = ff_data.relative_gain_median[chan]
        #charge_median = ff_data.charge_median[chan]
        charge_mean = ff_data.charge_mean[chan]
        charge_std = ff_data.charge_std[chan]
        #median_ped = ped_data.charge_median[chan]
        mean_ped = ped_data.charge_mean[chan]
        ped_std = ped_data.charge_std[chan]

        # select good pixels
        select = np.logical_not(mask[chan])
        fig = plt.figure(chan + 10, figsize=(12, 18))
        fig.tight_layout(rect=[0, 0.03, 1, 0.95])

        fig.suptitle(f"Run {run} channel: {channel[chan]}", fontsize=25)

        # charge
        plt.subplot(321)
        plt.tight_layout()
        median = int(np.median(charge_mean[select]))
        rms = np.std(charge_mean[select])
        label = f"Median {median:3.2f}, std {rms:5.0f}"
        plt.xlabel('charge (ADC)', fontsize=20)
        plt.ylabel('pixels', fontsize=20)
        plt.hist(charge_mean[select], bins=50, label=label)
        plt.legend()

        plt.subplot(322)
        plt.tight_layout()
        plt.ylabel('pixels', fontsize=20)
        plt.xlabel('charge std', fontsize=20)
        median = np.median(charge_std[select])
        rms = np.std(charge_std[select])
        label = f"Median {median:3.2f}, std {rms:3.2f}"
        plt.hist(charge_std[select], bins=50, label=label)
        plt.legend()

        # pedestal charge
        plt.subplot(323)
        plt.tight_layout()
        plt.ylabel('pixels', fontsize=20)
        plt.xlabel('pedestal', fontsize=20)
        median = np.median(mean_ped[select])
        rms = np.std(mean_ped[select])
        label = f"Median {median:3.2f}, std {rms:3.2f}"
        plt.hist(mean_ped[select], bins=50, label=label)
        plt.legend()

        # pedestal std
        plt.subplot(324)
        plt.ylabel('pixels', fontsize=20)
        plt.xlabel('pedestal std', fontsize=20)
        median = np.median(ped_std[select])
        rms = np.std(ped_std[select])
        label = f"Median {median:3.2f}, std {rms:3.2f}"
        plt.hist(ped_std[select], bins=50, label=label)
        plt.legend()

        # relative gain
        plt.subplot(325)
        plt.tight_layout()
        plt.ylabel('pixels', fontsize=20)
        plt.xlabel('relative gain', fontsize=20)
        median = np.median(gain_median[select])
        rms = np.std(gain_median[select])
        label = f"Relative gain {median:3.2f}, std {rms:5.2f}"
        plt.hist(gain_median[select], bins=50, label=label)
        plt.legend()

        # photon electrons
        plt.subplot(326)
        plt.tight_layout()
        plt.ylabel('pixels', fontsize=20)
        plt.xlabel('pe', fontsize=20)
        median = np.median(n_pe[select])
        rms = np.std(n_pe[select])
        label = f"Median {median:3.2f}, std {rms:3.2f}"
        plt.hist(n_pe[select], bins=50, label=label)
        plt.legend()
        plt.subplots_adjust(top=0.92)
        if plot_file != "none":
            pp.savefig(plt.gcf())

    if plot_file != "none":
        pp.close()
Beispiel #31
0
    def plot(self, input_file, event, telid, chan, extractor_name, nei):
        # Extract required images
        dl0 = event.dl0.tel[telid].adc_samples[chan]
        t_pe = event.mc.tel[telid].photo_electron_image
        dl1 = event.dl1.tel[telid].image[chan]
        max_time = np.unravel_index(np.argmax(dl0), dl0.shape)[1]
        max_charges = np.max(dl0, axis=1)
        max_pix = int(np.argmax(max_charges))
        min_pix = int(np.argmin(max_charges))

        geom = CameraGeometry.guess(*event.inst.pixel_pos[telid],
                                    event.inst.optical_foclen[telid])

        # Get Neighbours
        max_pixel_nei = nei[max_pix]
        min_pixel_nei = nei[min_pix]

        # Get Windows
        windows = event.dl1.tel[telid].extracted_samples[chan]
        length = np.sum(windows, axis=1)
        start = np.argmax(windows, axis=1)
        end = start + length

        # Draw figures
        ax_max_nei = {}
        ax_min_nei = {}
        fig_waveforms = plt.figure(figsize=(18, 9))
        fig_waveforms.subplots_adjust(hspace=.5)
        fig_camera = plt.figure(figsize=(15, 12))

        ax_max_pix = fig_waveforms.add_subplot(4, 2, 1)
        ax_min_pix = fig_waveforms.add_subplot(4, 2, 2)
        ax_max_nei[0] = fig_waveforms.add_subplot(4, 2, 3)
        ax_min_nei[0] = fig_waveforms.add_subplot(4, 2, 4)
        ax_max_nei[1] = fig_waveforms.add_subplot(4, 2, 5)
        ax_min_nei[1] = fig_waveforms.add_subplot(4, 2, 6)
        ax_max_nei[2] = fig_waveforms.add_subplot(4, 2, 7)
        ax_min_nei[2] = fig_waveforms.add_subplot(4, 2, 8)

        ax_img_nei = fig_camera.add_subplot(2, 2, 1)
        ax_img_max = fig_camera.add_subplot(2, 2, 2)
        ax_img_true = fig_camera.add_subplot(2, 2, 3)
        ax_img_cal = fig_camera.add_subplot(2, 2, 4)

        # Draw max pixel traces
        ax_max_pix.plot(dl0[max_pix])
        ax_max_pix.set_xlabel("Time (ns)")
        ax_max_pix.set_ylabel("DL0 Samples (ADC)")
        ax_max_pix.set_title("(Max) Pixel: {}, True: {}, Measured = {:.3f}"
                             .format(max_pix, t_pe[max_pix], dl1[max_pix]))
        max_ylim = ax_max_pix.get_ylim()
        ax_max_pix.plot([start[max_pix], start[max_pix]],
                        ax_max_pix.get_ylim(), color='r', alpha=1)
        ax_max_pix.plot([end[max_pix], end[max_pix]],
                        ax_max_pix.get_ylim(), color='r', alpha=1)
        for i, ax in ax_max_nei.items():
            if len(max_pixel_nei) > i:
                pix = max_pixel_nei[i]
                ax.plot(dl0[pix])
                ax.set_xlabel("Time (ns)")
                ax.set_ylabel("DL0 Samples (ADC)")
                ax.set_title("(Max Nei) Pixel: {}, True: {}, Measured = {:.3f}"
                             .format(pix, t_pe[pix], dl1[pix]))
                ax.set_ylim(max_ylim)
                ax.plot([start[pix], start[pix]],
                        ax.get_ylim(), color='r', alpha=1)
                ax.plot([end[pix], end[pix]],
                        ax.get_ylim(), color='r', alpha=1)

        # Draw min pixel traces
        ax_min_pix.plot(dl0[min_pix])
        ax_min_pix.set_xlabel("Time (ns)")
        ax_min_pix.set_ylabel("DL0 Samples (ADC)")
        ax_min_pix.set_title("(Min) Pixel: {}, True: {}, Measured = {:.3f}"
                             .format(min_pix, t_pe[min_pix], dl1[min_pix]))
        ax_min_pix.set_ylim(max_ylim)
        ax_min_pix.plot([start[min_pix], start[min_pix]],
                        ax_min_pix.get_ylim(), color='r', alpha=1)
        ax_min_pix.plot([end[min_pix], end[min_pix]],
                        ax_min_pix.get_ylim(), color='r', alpha=1)
        for i, ax in ax_min_nei.items():
            if len(min_pixel_nei) > i:
                pix = min_pixel_nei[i]
                ax.plot(dl0[pix])
                ax.set_xlabel("Time (ns)")
                ax.set_ylabel("DL0 Samples (ADC)")
                ax.set_title("(Min Nei) Pixel: {}, True: {}, Measured = {:.3f}"
                             .format(pix, t_pe[pix], dl1[pix]))
                ax.set_ylim(max_ylim)
                ax.plot([start[pix], start[pix]],
                        ax.get_ylim(), color='r', alpha=1)
                ax.plot([end[pix], end[pix]],
                        ax.get_ylim(), color='r', alpha=1)

        # Draw cameras
        nei_camera = np.zeros_like(max_charges, dtype=np.int)
        nei_camera[min_pixel_nei] = 2
        nei_camera[min_pix] = 1
        nei_camera[max_pixel_nei] = 3
        nei_camera[max_pix] = 4
        camera = CameraDisplay(geom, ax=ax_img_nei)
        camera.image = nei_camera
        camera.cmap = plt.cm.viridis
        ax_img_nei.set_title("Neighbour Map")
        ax_img_nei.annotate("Pixel: {}".format(max_pix),
                            xy=(geom.pix_x.value[max_pix],
                                geom.pix_y.value[max_pix]),
                            xycoords='data', xytext=(0.05, 0.98),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='red', width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        ax_img_nei.annotate("Pixel: {}".format(min_pix),
                            xy=(geom.pix_x.value[min_pix],
                                geom.pix_y.value[min_pix]),
                            xycoords='data', xytext=(0.05, 0.94),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='orange', width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        camera = CameraDisplay(geom, ax=ax_img_max)
        camera.image = dl0[:, max_time]
        camera.cmap = plt.cm.viridis
        camera.add_colorbar(ax=ax_img_max, label="DL0 Samples (ADC)")
        ax_img_max.set_title("Max Timeslice (T = {})".format(max_time))
        ax_img_max.annotate("Pixel: {}".format(max_pix),
                            xy=(geom.pix_x.value[max_pix],
                                geom.pix_y.value[max_pix]),
                            xycoords='data', xytext=(0.05, 0.98),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='red', width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        ax_img_max.annotate("Pixel: {}".format(min_pix),
                            xy=(geom.pix_x.value[min_pix],
                                geom.pix_y.value[min_pix]),
                            xycoords='data', xytext=(0.05, 0.94),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='orange', width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')

        camera = CameraDisplay(geom, ax=ax_img_true)
        camera.image = t_pe
        camera.cmap = plt.cm.viridis
        camera.add_colorbar(ax=ax_img_true, label="True Charge (p.e.)")
        ax_img_true.set_title("True Charge")
        ax_img_true.annotate("Pixel: {}".format(max_pix),
                             xy=(geom.pix_x.value[max_pix],
                                 geom.pix_y.value[max_pix]),
                             xycoords='data', xytext=(0.05, 0.98),
                             textcoords='axes fraction',
                             arrowprops=dict(facecolor='red', width=2,
                                             alpha=0.4),
                             horizontalalignment='left',
                             verticalalignment='top')
        ax_img_true.annotate("Pixel: {}".format(min_pix),
                             xy=(geom.pix_x.value[min_pix],
                                 geom.pix_y.value[min_pix]),
                             xycoords='data', xytext=(0.05, 0.94),
                             textcoords='axes fraction',
                             arrowprops=dict(facecolor='orange', width=2,
                                             alpha=0.4),
                             horizontalalignment='left',
                             verticalalignment='top')

        camera = CameraDisplay(geom, ax=ax_img_cal)
        camera.image = dl1
        camera.cmap = plt.cm.viridis
        camera.add_colorbar(ax=ax_img_cal,
                            label="Calib Charge (Photo-electrons)")
        ax_img_cal.set_title("Charge (integrator={})".format(extractor_name))
        ax_img_cal.annotate("Pixel: {}".format(max_pix),
                            xy=(geom.pix_x.value[max_pix],
                                geom.pix_y.value[max_pix]),
                            xycoords='data', xytext=(0.05, 0.98),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='red', width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')
        ax_img_cal.annotate("Pixel: {}".format(min_pix),
                            xy=(geom.pix_x.value[min_pix],
                                geom.pix_y.value[min_pix]),
                            xycoords='data', xytext=(0.05, 0.94),
                            textcoords='axes fraction',
                            arrowprops=dict(facecolor='orange', width=2,
                                            alpha=0.4),
                            horizontalalignment='left',
                            verticalalignment='top')

        fig_waveforms.suptitle("Integrator = {}".format(extractor_name))
        fig_camera.suptitle("Camera = {}".format(geom.cam_id))

        waveform_output_name = "e{}_t{}_c{}_extractor{}_waveform.pdf"\
            .format(event.count, telid, chan, extractor_name)
        camera_output_name = "e{}_t{}_c{}_extractor{}_camera.pdf"\
            .format(event.count, telid, chan, extractor_name)

        output_dir = self.output_dir
        if output_dir is None:
            output_dir = input_file.output_directory
        output_dir = os.path.join(output_dir, self.name)
        if not os.path.exists(output_dir):
            self.log.info("Creating directory: {}".format(output_dir))
            os.makedirs(output_dir)

        waveform_output_path = os.path.join(output_dir, waveform_output_name)
        self.log.info("Saving: {}".format(waveform_output_path))
        fig_waveforms.savefig(waveform_output_path, format='pdf',
                              bbox_inches='tight')

        camera_output_path = os.path.join(output_dir, camera_output_name)
        self.log.info("Saving: {}".format(camera_output_path))
        fig_camera.savefig(camera_output_path, format='pdf',
                           bbox_inches='tight')
Beispiel #32
0
    def _display_camera_animation(self):
        # plt.style.use("ggplot")
        fig = plt.figure(num="ctapipe Camera Demo", figsize=(7, 7))
        ax = plt.subplot(111)

        # load the camera
        tel = TelescopeDescription.from_name(optics_name=self.optics,
                                             camera_name=self.camera)
        geom = tel.camera

        # poor-man's coordinate transform from telscope to camera frame (it's
        # better to use ctapipe.coordiantes when they are stable)
        scale = tel.optics.effective_focal_length.to(geom.pix_x.unit).value
        fov = np.deg2rad(4.0)
        maxwid = np.deg2rad(0.01)
        maxlen = np.deg2rad(0.03)

        disp = CameraDisplay(geom,
                             ax=ax,
                             autoupdate=True,
                             title="{}, f={}".format(
                                 tel, tel.optics.effective_focal_length))
        disp.cmap = plt.cm.terrain

        def update(frame):

            centroid = np.random.uniform(-fov, fov, size=2) * scale
            width = np.random.uniform(0, maxwid) * scale
            length = np.random.uniform(0, maxlen) * scale + width
            angle = np.random.uniform(0, 360)
            intens = np.random.exponential(2) * 50
            model = toymodel.generate_2d_shower_model(centroid=centroid,
                                                      width=width,
                                                      length=length,
                                                      psi=angle * u.deg)
            image, sig, bg = toymodel.make_toymodel_shower_image(
                geom, model.pdf, intensity=intens, nsb_level_pe=5000)

            # 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

            if self.imclean:
                cleanmask = tailcuts_clean(geom, image / 80.0)
                for ii in range(3):
                    dilate(geom, cleanmask)
                image[cleanmask == 0] = 0  # zero noise pixels

            self.log.debug("count = {}, image sum={} max={}".format(
                self._counter, image.sum(), image.max()))
            disp.image = image

            if self.autoscale:
                disp.set_limits_percent(95)
            else:
                disp.set_limits_minmax(-100, 4000)

            disp.axes.figure.canvas.draw()
            self._counter += 1
            return [
                ax,
            ]

        self.anim = FuncAnimation(fig,
                                  update,
                                  interval=self.delay,
                                  blit=self.blit)
        plt.show()
Beispiel #33
0
if __name__ == '__main__':

    plt.style.use("ggplot")
    fig, ax = plt.subplots()

    # load the camera
    tel = TelescopeDescription.from_name("SST-1M", "DigiCam")
    geom = tel.camera

    fov = 0.3
    maxwid = 0.05
    maxlen = 0.1

    disp = CameraDisplay(geom, ax=ax)
    disp.cmap = 'inferno'
    disp.add_colorbar(ax=ax)

    def update(frame):
        x, y = np.random.uniform(-fov, fov, size=2)
        width = np.random.uniform(0.01, maxwid)
        length = np.random.uniform(width, maxlen)
        angle = np.random.uniform(0, 180)
        intens = width * length * (5e4 + 1e5 * np.random.exponential(2))

        model = toymodel.Gaussian(
            x=x * u.m,
            y=y * u.m,
            width=width * u.m,
            length=length * u.m,
            psi=angle * u.deg,
def transform_and_clean_hex_image(pmt_signal, cam_geom, photo_electrons):

    start_time = time.time()

    colors = cm.inferno(pmt_signal/max(pmt_signal))

    new_geom, new_signal = convert_geometry_1d_to_2d(
        cam_geom, pmt_signal, cam_geom.cam_id)

    print("rot_signal", np.count_nonzero(np.isnan(new_signal)))

    square_mask = new_geom.mask
    cleaned_img = wavelet_transform(new_signal,
                                    raw_option_string=args.raw)

    unrot_img = cleaned_img[square_mask]
    unrot_colors = cm.inferno(unrot_img/max(unrot_img))

    cleaned_img_ik = kill_isolpix(cleaned_img, threshold=.5)
    unrot_img_ik = cleaned_img_ik[square_mask]
    unrot_colors_ik = cm.inferno(unrot_img_ik/max(unrot_img_ik))

    square_image_add_noise = np.copy(new_signal)
    square_image_add_noise[~square_mask] = \
        np.random.normal(0.13, 5.77, np.count_nonzero(~square_mask))

    square_image_add_noise_cleaned = wavelet_transform(square_image_add_noise,
                                                       raw_option_string=args.raw)

    square_image_add_noise_cleaned_ik = kill_isolpix(square_image_add_noise_cleaned,
                                                     threshold=1.5)

    unrot_geom, unrot_noised_signal = convert_geometry_back(
        new_geom, square_image_add_noise_cleaned_ik, cam_geom.cam_id)

    end_time = time.time()
    print(end_time - start_time)

    global fig
    global cb1, ax1
    global cb2, ax2
    global cb3, ax3
    global cb4, ax4
    global cb5, ax5
    global cb6, ax6
    global cb7, ax7
    global cb8, ax8
    global cb9, ax9
    if fig is None:
        fig = plt.figure(figsize=(10, 10))
    else:
        fig.delaxes(ax1)
        fig.delaxes(ax2)
        fig.delaxes(ax3)
        fig.delaxes(ax4)
        fig.delaxes(ax5)
        fig.delaxes(ax6)
        fig.delaxes(ax7)
        fig.delaxes(ax8)
        fig.delaxes(ax9)
        cb1.remove()
        cb2.remove()
        cb3.remove()
        cb4.remove()
        cb5.remove()
        cb6.remove()
        cb7.remove()
        cb8.remove()
        cb9.remove()

    ax1 = fig.add_subplot(333)
    disp1 = CameraDisplay(cam_geom, image=photo_electrons, ax=ax1)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("photo-electron image")
    disp1.cmap = plt.cm.inferno
    disp1.add_colorbar()
    cb1 = disp1.colorbar

    ax2 = fig.add_subplot(336)
    disp2 = CameraDisplay(cam_geom, image=pmt_signal, ax=ax2)
    plt.gca().set_aspect('equal', adjustable='box')
    disp2.cmap = plt.cm.inferno
    disp2.add_colorbar()
    cb2 = disp2.colorbar
    plt.title("noisy image")

    ax3 = fig.add_subplot(331)
    plt.imshow(new_signal, interpolation='none', cmap=cm.inferno,
               origin='lower')
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("noisy, slanted image")
    cb3 = plt.colorbar()

    ax4 = fig.add_subplot(334)
    plt.imshow(cleaned_img, interpolation='none', cmap=cm.inferno,
               origin='lower')
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("cleaned, slanted image, islands not killed")
    cb4 = plt.colorbar()
    ax4.set_axis_off()

    ax5 = fig.add_subplot(337)
    plt.imshow(np.sqrt(cleaned_img_ik), interpolation='none', cmap=cm.inferno,
               origin='lower')
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("cleaned, slanted image, islands killed")
    cb5 = plt.colorbar()
    ax5.set_axis_off()

    #
    ax6 = fig.add_subplot(332)
    plt.imshow(square_image_add_noise, interpolation='none', cmap=cm.inferno,
               origin='lower')
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("slanted image, noise added")
    cb6 = plt.colorbar()
    ax6.set_axis_off()

    #
    ax7 = fig.add_subplot(335)
    plt.imshow(np.sqrt(square_image_add_noise_cleaned), interpolation='none',
               cmap=cm.inferno,
               origin='lower')
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("slanted image, noise added, cleaned")
    cb7 = plt.colorbar()
    ax7.set_axis_off()

    ax8 = fig.add_subplot(338)
    plt.imshow(square_image_add_noise_cleaned_ik, interpolation='none',
               cmap=cm.inferno,
               origin='lower')
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("slanted image, noise added, cleaned, islands killed")
    cb8 = plt.colorbar()
    ax8.set_axis_off()

    try:
        ax9 = fig.add_subplot(339)
        disp9 = CameraDisplay(unrot_geom, image=unrot_noised_signal,
                              ax=ax9)
        plt.gca().set_aspect('equal', adjustable='box')
        plt.title("cleaned, original geometry, islands killed")
        disp9.cmap = plt.cm.inferno
        disp9.add_colorbar()
        cb9 = disp9.colorbar
    except:
        pass

    plt.suptitle(cam_geom.cam_id)
    plt.subplots_adjust(top=0.94, bottom=.08, left=0, right=.96, hspace=.41, wspace=.08)

    plt.pause(.1)
    response = input("press return to continue")
    if response != "":
        exit()
Beispiel #35
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()
Beispiel #36
0
def check_interleave_pedestal_cleaning(path_list, calib_time_file, calib_file, max_events=10000):

    signal_place_after_clean = np.zeros(1855)
    sum_ped_ev = 0
    alive_ped_ev = 0

    for path in path_list:
        print(path)
        r0_r1_calibrator = LSTR0Corrections(pedestal_path=None,
                                            r1_sample_start=3,
                                            r1_sample_end=39)

        r1_dl1_calibrator = LSTCameraCalibrator(calibration_path=calib_file,
                                                time_calibration_path=calib_time_file,
                                                extractor_product="LocalPeakWindowSum",
                                                config=charge_config,
                                                allowed_tels=[1])

        reader = LSTEventSource(input_url=path, max_events=max_events)


        for i, ev in enumerate(reader):
            r0_r1_calibrator.calibrate(ev)
            if i%10000 == 0:
                print(ev.r0.event_id)

            if ev.lst.tel[1].evt.tib_masked_trigger == 32:
                sum_ped_ev += 1

                r1_dl1_calibrator(ev)

                img = ev.dl1.tel[1].image

                geom = ev.inst.subarray.tel[1].camera
                clean = tailcuts_clean(
                                    geom,
                                    img,
                                    picture_thresh=6,
                                    boundary_thresh=3,
                                    min_number_picture_neighbors=1,
                                    keep_isolated_pixels=False
                                    )

                cleaned = img.copy()
                cleaned[~clean] = 0.0

                signal_place_after_clean[np.where(clean == True)] += 1

                if np.sum(cleaned>0) > 0:
                    alive_ped_ev += 1

    fig, ax = plt.subplots(figsize=(8, 8))
    geom = ev.inst.subarray.tel[1].camera

    disp0 = CameraDisplay(geom, ax=ax)
    disp0.image = signal_place_after_clean/sum_ped_ev
    disp0.add_colorbar(ax=ax, label="N times signal remain after cleaning")
    disp0.cmap = 'gnuplot2'
    ax.set_title("{} \n {}/{}".format(path.split("/")[-1][8:21], alive_ped_ev, sum_ped_ev))

    print(path.split("/")[-1][8:21])
    print("{}/{}".format(alive_ped_ev, sum_ped_ev))

    ax.set_xlabel(" ")
    ax.set_ylabel(" ")
    plt.tight_layout()
    plt.show()

    return signal_place_after_clean, sum_ped_ev
Beispiel #37
0
    fig, ax = plt.subplots()

    # load the camera
    tel = TelescopeDescription.from_name("SST-1M","DigiCam")
    print(tel, tel.optics.effective_focal_length)
    geom = tel.camera

    # poor-man's coordinate transform from telscope to camera frame (it's
    # better to use ctapipe.coordiantes when they are stable)
    scale = tel.optics.effective_focal_length.to(geom.pix_x.unit).value
    fov = np.deg2rad(4.0)
    maxwid = np.deg2rad(0.01)
    maxlen = np.deg2rad(0.03)

    disp = CameraDisplay(geom, ax=ax)
    disp.cmap = plt.cm.terrain
    disp.add_colorbar(ax=ax)

    def update(frame):
        centroid = np.random.uniform(-fov, fov, size=2) * scale
        width = np.random.uniform(0, maxwid) * scale
        length = np.random.uniform(0, maxlen) * scale + width
        angle = np.random.uniform(0, 360)
        intens = np.random.exponential(2) * 50
        model = toymodel.generate_2d_shower_model(
            centroid=centroid,
            width=width,
            length=length,
            psi=angle * u.deg,
        )
        image, sig, bg = toymodel.make_toymodel_shower_image(
Beispiel #38
0
import numpy as np
from matplotlib import pyplot as plt

from ctapipe.instrument import CameraDescription, CameraGeometry
from ctapipe.visualization import CameraDisplay

if __name__ == "__main__":

    plt.style.use("bmh")

    camera_names = CameraDescription.get_known_camera_names()
    n_tels = len(camera_names)
    n_rows = np.trunc(np.sqrt(n_tels)).astype(int)
    n_cols = np.ceil(n_tels / n_rows).astype(int)
    plt.figure(figsize=(15, 6))

    for ii, name in enumerate(sorted(camera_names)):
        print("plotting", name)
        geom = CameraGeometry.from_name(name)
        ax = plt.subplot(n_rows, n_cols, ii + 1)
        disp = CameraDisplay(geom)
        disp.image = np.random.uniform(size=geom.pix_id.shape)
        disp.cmap = "viridis"
        plt.xlabel("")
        plt.ylabel("")

    plt.tight_layout()
    plt.show()
Beispiel #39
0
    def _display_camera_animation(self):
        # plt.style.use("ggplot")
        fig = plt.figure(num="ctapipe Camera Demo", figsize=(7, 7))
        ax = plt.subplot(111)

        # load the camera
        tel = TelescopeDescription.from_name(optics_name=self.optics,
                                             camera_name=self.camera)
        geom = tel.camera

        # poor-man's coordinate transform from telscope to camera frame (it's
        # better to use ctapipe.coordiantes when they are stable)
        foclen = tel.optics.equivalent_focal_length.to(geom.pix_x.unit).value
        fov = np.deg2rad(4.0)
        scale = foclen
        minwid = np.deg2rad(0.1)
        maxwid = np.deg2rad(0.3)
        maxlen = np.deg2rad(0.5)

        self.log.debug("scale={} m, wid=({}-{})".format(scale, minwid, maxwid))

        disp = CameraDisplay(
            geom, ax=ax, autoupdate=True,
            title="{}, f={}".format(tel, tel.optics.equivalent_focal_length)
        )
        disp.cmap = plt.cm.terrain

        def update(frame):


            centroid = np.random.uniform(-fov, fov, size=2) * scale
            width = np.random.uniform(0, maxwid-minwid) * scale + minwid
            length = np.random.uniform(0, maxlen) * scale + width
            angle = np.random.uniform(0, 360)
            intens = np.random.exponential(2) * 500
            model = toymodel.generate_2d_shower_model(centroid=centroid,
                                                      width=width,
                                                      length=length,
                                                      psi=angle * u.deg)
            self.log.debug(
                "Frame=%d width=%03f length=%03f intens=%03d",
                frame, width, length, intens
            )

            image, sig, bg = toymodel.make_toymodel_shower_image(
                geom,
                model.pdf,
                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:
                    disp.clear_overlays()
                    pass

            self.log.debug("Frame=%d  image_sum=%.3f max=%.3f",
                           self._counter, image.sum(), image.max())
            disp.image = image

            if self.autoscale:
                disp.set_limits_percent(95)
            else:
                disp.set_limits_minmax(-5, 200)

            disp.axes.figure.canvas.draw()
            self._counter += 1
            return [ax, ]

        frames = None if self.num_events == 0 else self.num_events
        repeat = True if self.num_events == 0 else False

        self.log.info("Running for {} frames".format(frames))
        self.anim = FuncAnimation(fig, update,
                                  interval=self.delay,
                                  frames=frames,
                                  repeat=repeat,
                                  blit=self.blit)

        if self.display:
            plt.show()
Beispiel #40
0
    fig, ax = plt.subplots()

    # load the camera
    tel = TelescopeDescription.from_name("SST-1M", "DigiCam")
    print(tel, tel.optics.effective_focal_length)
    geom = tel.camera

    # poor-man's coordinate transform from telscope to camera frame (it's
    # better to use ctapipe.coordiantes when they are stable)
    scale = tel.optics.effective_focal_length.to(geom.pix_x.unit).value
    fov = np.deg2rad(4.0)
    maxwid = np.deg2rad(0.01)
    maxlen = np.deg2rad(0.03)

    disp = CameraDisplay(geom, ax=ax)
    disp.cmap = plt.cm.terrain
    disp.add_colorbar(ax=ax)

    def update(frame):
        centroid = np.random.uniform(-fov, fov, size=2) * scale
        width = np.random.uniform(0, maxwid) * scale
        length = np.random.uniform(0, maxlen) * scale + width
        angle = np.random.uniform(0, 360)
        intens = np.random.exponential(2) * 50
        model = toymodel.generate_2d_shower_model(
            centroid=centroid,
            width=width,
            length=length,
            psi=angle * u.deg,
        )
        image, sig, bg = toymodel.make_toymodel_shower_image(
Beispiel #41
0
inferno = plt.get_cmap('inferno')
inferno.set_bad('gray')
rdbu = plt.get_cmap('RdBu_r')
rdbu.set_bad('gray')

for i in range(2):
    fig, axs = plt.subplots(1, 2, figsize=(5, 2), constrained_layout=True)

    if i == 1:
        clean = tailcuts_clean(cam, img, 9, 3)
        img[~clean] = np.nan
        time[~clean] = np.nan

    disp = CameraDisplay(cam, ax=axs[0])
    disp.image = img
    disp.cmap = inferno
    disp.add_colorbar(ax=axs[0])
    disp.set_limits_minmax(0, 45)
    disp.pixels.set_rasterized(True)

    disp2 = CameraDisplay(cam, ax=axs[1])
    disp2.image = time
    disp2.cmap = rdbu
    disp2.set_limits_minmax(10, 40)
    disp2.add_colorbar(ax=axs[1])
    disp2.pixels.set_rasterized(True)

    axs[0].set_title('\# Photons')
    axs[1].set_title('Time / ns')

    for ax in axs: