Exemple #1
0
    def go(self):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        disp = None
        source = hessio_event_source(self.filename, requested_event=24)
        for event in source:
            self.calib.calibrate(event)
            for i in range(50):
                ipix = np.random.randint(0, 2048)
                samp = event.dl0.tel[1]['pe_samples'][0][ipix]
                # plt.plot(range(len(samp)),samp)

            plt.show()
            if disp is None:
                geom = event.inst.subarray.tel[1].camera
                disp = CameraDisplay(geom)
                # disp.enable_pixel_picker()
                disp.add_colorbar()
                plt.show(block=False)
            #
            im = event.dl1.tel[1].image[0]
            mask = tailcuts_clean(geom,
                                  im,
                                  picture_thresh=10,
                                  boundary_thresh=5)
            im[~mask] = 0.0
            maxpe = max(event.dl1.tel[1].image[0])
            disp.image = im
            print(np.mean(im), '+/-', np.std(im))

        plt.show()
    def start(self):
        self.log.info('Loop on events...')

        for event in tqdm(
                self.event_source,
                desc='EventWriter',
                total=self.event_source.max_events,
                disable=~self.progress):

            self.event_cutflow.count('no_sel')
            self.calibrator.calibrate(event)

            for tel_id in event.dl0.tels_with_data:
                self.image_cutflow.count('no_sel')

                camera = event.inst.subarray.tel[tel_id].camera
                dl1_tel = event.dl1.tel[tel_id]

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

                # Preselection cuts
                if self.image_cutflow.cut('n_pixel', cleaned):
                    continue
                if self.image_cutflow.cut('image_amplitude', np.sum(cleaned)):
                    continue

                # Image parametrisation
                params = hillas_parameters(camera, cleaned)

                # Save Ids, MC infos and Hillas informations
                self.writer.write(camera.cam_id, [event.r0, event.mc, params])
Exemple #3
0
def create_sample_image(
        psi='-30d',
        x=0.2 * u.m,
        y=0.3 * u.m,
        width=0.05 * u.m,
        length=0.15 * u.m,
        intensity=1500
):
    seed(10)

    geom = CameraGeometry.from_name('LSTCam')

    # make a toymodel shower model
    model = toymodel.Gaussian(x=x, y=y, width=width, length=length, psi=psi)

    # generate toymodel image in camera for this shower model.
    image, _, _ = model.generate_image(
        geom,
        intensity=1500,
        nsb_level_pe=3,
    )

    # calculate pixels likely containing signal
    clean_mask = tailcuts_clean(geom, image, 10, 5)

    return geom, image, clean_mask
Exemple #4
0
    def start(self):
        self.log.info("Loop on events...")

        for event in tqdm(
                self.event_source,
                desc="EventWriter",
                total=self.event_source.max_events,
                disable=~self.progress,
        ):

            self.calibrator(event)

            for tel_id in event.dl0.tel.keys():

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

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

                # Image parametrisation
                params = hillas_parameters(geom, cleaned)

                # Save Ids, MC infos and Hillas informations
                self.writer.write(geom.camera_name,
                                  [event.r0, event.simulation.shower, params])
Exemple #5
0
def create_sample_image(psi='-30d'):

    seed(10)

    # set up the sample image using a HESS camera geometry (since it's easy
    # to load)
    geom = CameraGeometry.from_name("HESS", 1)

    # make a toymodel shower model
    model = toymodel.generate_2d_shower_model(centroid=(0.2, 0.3),
                                              width=0.001,
                                              length=0.01,
                                              psi=psi)

    # generate toymodel image in camera for this shower model.
    image, signal, noise = toymodel.make_toymodel_shower_image(
        geom, model.pdf, intensity=50, nsb_level_pe=100)

    # denoise the image, so we can calculate hillas params
    clean_mask = tailcuts_clean(geom, image, 1, 10,
                                5)  # pedvars = 1 and core and boundary
    # threshold in pe
    image[~clean_mask] = 0

    # Pixel values in the camera
    pix_x = geom.pix_x.value
    pix_y = geom.pix_y.value

    return pix_x, pix_y, image
Exemple #6
0
def apply_cut(event, value, load_option=LoadOption.raw_sim):
    """
    Takes calibrated event, returns whether it survives the cut or not

    :param event: calibrated event
    :param value: value to cut off at

    returns bool: Whether the image is above the cut True or not False
    """

    tel = event.inst.subarray.tel[1]
    if load_option == LoadOption.raw_sim or load_option == LoadOption.cal_sim:
        image = event.dl1.tel[1].image[1]
    else:
        image = event.dl1.tel[1].image
        # print("Image: ",image)

    mask = tailcuts_clean(tel.camera,
                          image,
                          picture_thresh=6,
                          boundary_thresh=3.5)
    count = 0
    for i in range(len(image)):
        if mask[i]:
            count += image[i]

    # print(count)
    return count > value
Exemple #7
0
def test_MuonRingFitter(method):
    """test MuonRingFitter"""
    # flashCam example
    center_xs = 0.3 * u.m
    center_ys = 0.6 * u.m
    ring_radius = 0.3 * u.m
    ring_width = 0.05 * u.m

    muon_model = toymodel.RingGaussian(
        x=center_xs, y=center_ys, radius=ring_radius, sigma=ring_width,
    )

    # testing with flashcam
    geom = CameraGeometry.from_name("FlashCam")
    charge, _, _ = muon_model.generate_image(geom, intensity=1000, nsb_level_pe=5,)
    survivors = tailcuts_clean(geom, charge, 10, 12)

    muonfit = MuonRingFitter(fit_method=method)
    fit_result = muonfit(geom.pix_x, geom.pix_y, charge, survivors)

    print(fit_result)
    print(center_xs, center_ys, ring_radius)

    assert u.isclose(fit_result.ring_center_x, center_xs, 5e-2)
    assert u.isclose(fit_result.ring_center_y, center_ys, 5e-2)
    assert u.isclose(fit_result.ring_radius, ring_radius, 5e-2)
Exemple #8
0
def create_sample_image(psi='-30d'):

    seed(10)

    # set up the sample image using a HESS camera geometry (since it's easy
    # to load)
    geom = CameraGeometry.from_name("LSTCam")

    # make a toymodel shower model
    model = toymodel.generate_2d_shower_model(
        centroid=(0.2, 0.3),
        width=0.05,
        length=0.15,
        psi=psi,
    )

    # generate toymodel image in camera for this shower model.
    image, signal, noise = toymodel.make_toymodel_shower_image(
        geom,
        model.pdf,
        intensity=1500,
        nsb_level_pe=3,
    )

    # denoise the image, so we can calculate hillas params
    clean_mask = tailcuts_clean(geom, image, 10, 5)

    return geom, image, clean_mask
Exemple #9
0
def create_sample_image():
    # set up the sample image using a HESS camera geometry (since it's easy
    # to load)
    geom = CameraGeometry.from_name("HESS", 1)

    # make a toymodel shower model
    model = toymodel.generate_2d_shower_model(centroid=(0.2, 0.3),
                                              width=0.001, length=0.01,
                                              psi='30d')

    # generate toymodel image in camera for this shower model.
    image, signal, noise = toymodel.make_toymodel_shower_image(geom, model.pdf,
                                                               intensity=50,
                                                               nsb_level_pe=100)

    # denoise the image, so we can calculate hillas params
    clean_mask = tailcuts_clean(geom, image, 1, 10,
                                5)  # pedvars = 1 and core and boundary
    # threshold in pe
    image[~clean_mask] = 0

    # Pixel values in the camera
    pix_x = geom.pix_x.value
    pix_y = geom.pix_y.value

    return pix_x, pix_y, image
Exemple #10
0
def create_sample_image(
    psi="-30d",
    x=0.2 * u.m,
    y=0.3 * u.m,
    width=0.05 * u.m,
    length=0.15 * u.m,
    intensity=1500,
):

    geom = CameraGeometry.from_name("LSTCam")

    # make a toymodel shower model
    model = toymodel.Gaussian(x=x, y=y, width=width, length=length, psi=psi)

    # generate toymodel image in camera for this shower model.
    rng = np.random.default_rng(0)
    image, _, _ = model.generate_image(geom,
                                       intensity=1500,
                                       nsb_level_pe=3,
                                       rng=rng)

    # calculate pixels likely containing signal
    clean_mask = tailcuts_clean(geom, image, 10, 5)

    return geom, image, clean_mask
    def start(self):
        self.log.info('Loop on events...')

        for event in tqdm(
                self.event_source,
                desc='EventWriter',
                total=self.event_source.max_events,
                disable=~self.progress):

            self.event_cutflow.count('no_sel')
            self.calibrator.calibrate(event)

            for tel_id in event.dl0.tels_with_data:
                self.image_cutflow.count('no_sel')

                camera = event.inst.subarray.tel[tel_id].camera
                dl1_tel = event.dl1.tel[tel_id]

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

                # Preselection cuts
                if self.image_cutflow.cut('n_pixel', cleaned):
                    continue
                if self.image_cutflow.cut('image_amplitude', np.sum(cleaned)):
                    continue

                # Image parametrisation
                params = hillas_parameters(camera, cleaned)

                # Save Ids, MC infos and Hillas informations
                self.writer.write(camera.cam_id, [event.r0, event.mc, params])
Exemple #12
0
def create_sample_image(
        psi='-30d',
        x=0.2 * u.m,
        y=0.3 * u.m,
        width=0.05 * u.m,
        length=0.15 * u.m,
        intensity=1500
):
    seed(10)

    geom = CameraGeometry.from_name('LSTCam')

    # make a toymodel shower model
    model = toymodel.Gaussian(x=x, y=y, width=width, length=length, psi=psi)

    # generate toymodel image in camera for this shower model.
    image, _, _ = model.generate_image(
        geom,
        intensity=1500,
        nsb_level_pe=3,
    )

    # calculate pixels likely containing signal
    clean_mask = tailcuts_clean(geom, image, 10, 5)

    return geom, image, clean_mask
Exemple #13
0
    def waveform_loop(self, file_reader):
        global N_EVENTS
        global LIVE_DATA
        global FREEZE_DATA

        # Open file
        # N_EVENTS = file_reader.num_events

        with tqdm() as pbar:
            source = file_reader.read()
            for event in source:
                #  TODO: Remove telid loop once hillas tested
                for telid in event.r0.tels_with_data:
                    pbar.update(1)

                    self.r1.calibrate(event)
                    self.dl0.reduce(event)
                    self.dl1.calibrate(event)

                    index = event.count
                    image = event.dl1.tel[telid].image[CHAN]
                    mask = tailcuts_clean(self.geom, image, 1, 8, 5)
                    cleaned = np.ma.masked_array(image, ~mask)
                    pos = event.inst.pixel_pos[telid]
                    try:
                        hillas = hillas_parameters(*pos, cleaned)
                        # hillas = hillas_parameters_2(*pos, cleaned)
                    except HillasParameterizationError:
                        print('HillasParameterizationError')
                        continue

                    # print(hillas[0].length)

                    live_d = dict(index=index, image=intensity_to_hex(cleaned))
                    live_d = dict(index=index, image=intensity_to_hex(image))
                    LIVE_DATA = live_d

                    freeze_d = dict(index=index, event=copy.deepcopy(event))
                    FREEZE_DATA = freeze_d

                    width = hillas.width
                    length = hillas.length
                    size = hillas.size
                    phi = hillas.phi
                    miss = hillas.miss
                    r = hillas.r

                    HILLAS['width'] += \
                        np.histogram(width, bins=100, range=[0, 1])[0]
                    HILLAS['length'] += \
                        np.histogram(length, bins=100, range=[0, 1])[0]
                    HILLAS['size'] += \
                        np.histogram(size, bins=100, range=[0, 1000])[0]
                    HILLAS['phi'] += \
                        np.histogram(phi, bins=100, range=[0, 4])[0]
                    HILLAS['miss'] += \
                        np.histogram(miss, bins=100, range=[0, 1])[0]
                    HILLAS['r'] += \
                        np.histogram(r, bins=100, range=[0, 1])[0]
Exemple #14
0
def process_event(event, dl1, infos_save):
    Keylist = [
        "size", "cen_x", "cen_y", "length", "width", "r", "phi", "psi", "miss",
        "skewness", "kurtosis", "Reinheit", "Effizienz", "Genauigkeit"
    ]
    calibration(event, r1, dl0, dl1)

    eins_drinne = {}

    err = False
    image = event.dl1.tel[tel_id].image[0]
    geom = event.inst.subarray.tel[tel_id].camera

    for i in infos_save.keys():
        for j in infos_save[i].keys():
            if i not in eins_drinne.keys():
                eins_drinne[i] = {}
            if j not in eins_drinne[i].keys():
                eins_drinne[i][j] = False
            if i == "o":
                event_info = {}
                event_info = set_mc(event_info, event, tel_id)
                infos_save[i][j].append(event_info)
                eins_drinne[i][j] = True
                continue

            cleaning_mask = tailcuts_clean(geom,
                                           image,
                                           picture_thresh=i,
                                           boundary_thresh=j)
            if len(image[cleaning_mask]) == 0:
                continue
            clean = image.copy()
            clean[~cleaning_mask] = 0.0
            event_info = {}
            hillas = hillas_parameters(geom, image=clean)
            event_info = set_hillas(event_info, hillas)
            event_info = set_mc(event_info, event, tel_id)
            event_info = set_tp(event_info, cleaning_mask,
                                event.mc.tel[tel_id].photo_electron_image)
            for key in Keylist:
                if key not in event_info:
                    err = True
                else:
                    drinne = False
                    wert = event_info[key][0]
                    if wert >= 0:
                        drinne = True
                    elif wert < 0:
                        drinne = True
                    if drinne is False:
                        err = True
            if err:
                continue
            infos_save[i][j].append(event_info)
            eins_drinne[i][j] = True

    return infos_save
def test_ChaudhuriKunduRingFitter():

    geom = CameraGeometry.from_name('HESS-I')

    ring_rad = np.deg2rad(
        1. * u.deg) * 15.  # make sure this is in camera coordinates
    ring_width = np.deg2rad(0.05 * u.deg) * 15.
    geom_pixall = np.empty(geom.pix_x.shape + (2, ))
    geom_pixall[..., 0] = geom.pix_x.value
    geom_pixall[..., 1] = geom.pix_y.value

    # image = generate_muon_model(geom_pixall, ring_rad, ring_width, 0.3, 0.2)
    muon_model = partial(toymodel.generate_muon_model,
                         radius=ring_rad.value,
                         width=ring_width.value,
                         centre_x=-0.2,
                         centre_y=-0.3)

    toymodel_image, toy_signal, toy_noise = \
        toymodel.make_toymodel_shower_image(geom, muon_model)

    clean_toy_mask = tailcuts_clean(geom,
                                    toymodel_image,
                                    boundary_thresh=5,
                                    picture_thresh=10)

    # camera_coord = CameraFrame(x=x,y=y,z=np.zeros(x.shape)*u.m,
    # focal_length = event.inst.optical_foclen[telid], rotation=geom.pix_rotation)
    muonring = muon_ring_finder.ChaudhuriKunduRingFitter(None)

    x = np.rad2deg((geom.pix_x.value / 15.) * u.rad)  # .value
    y = np.rad2deg((geom.pix_y.value / 15.) * u.rad)  # .value

    muonringparam = muonring.fit(x, y, toymodel_image * clean_toy_mask)

    dist = np.sqrt(
        np.power(x - muonringparam.ring_center_x, 2) +
        np.power(y - muonringparam.ring_center_y, 2))
    ring_dist = np.abs(dist - muonringparam.ring_radius)
    muonringparam = muonring.fit(
        x, y, toymodel_image * (ring_dist < muonringparam.ring_radius * 0.4))

    dist = np.sqrt(
        np.power(x - muonringparam.ring_center_x, 2) +
        np.power(y - muonringparam.ring_center_y, 2))
    ring_dist = np.abs(dist - muonringparam.ring_radius)
    muonringparam = muonring.fit(
        x, y, toymodel_image * (ring_dist < muonringparam.ring_radius * 0.4))

    print('Fitted ring radius', muonringparam.ring_radius, 'c.f.', ring_rad)
    print('Fitted ring centre', muonringparam.ring_center_x,
          muonringparam.ring_center_y)

    assert muonringparam.ring_radius is not ring_rad  # .value
    assert muonringparam.ring_center_x is not -0.2
    assert muonringparam.ring_center_y is not -0.3
def test_ChaudhuriKunduRingFitter():

    geom = CameraGeometry.from_name('LSTCam')
    focal_length = u.Quantity(28, u.m)

    ring_radius = u.Quantity(0.4, u.m)  # make sure this is in camera coordinates
    ring_width = u.Quantity(0.03, u.m)
    center_x = u.Quantity(-0.2, u.m)
    center_y = u.Quantity(-0.3, u.m)

    muon_model = RingGaussian(
        x=center_x, y=center_y,
        sigma=ring_width, radius=ring_radius,
    )

    image, _, _ = muon_model.generate_image(
        geom, intensity=1000, nsb_level_pe=5,
    )

    clean_mask = tailcuts_clean(
        geom, image, boundary_thresh=5, picture_thresh=10
    )

    fitter = muon_ring_finder.ChaudhuriKunduRingFitter()

    x = geom.pix_x / focal_length * u.rad
    y = geom.pix_y / focal_length * u.rad

    # fit 3 times, first iteration use cleaning, after that use
    # distance to previous fit result
    result = None
    for _ in range(3):
        if result is None:
            mask = clean_mask
        else:
            dist = np.sqrt((x - result.ring_center_x)**2 + (y - result.ring_center_y)**2)
            ring_dist = np.abs(dist - result.ring_radius)
            mask = ring_dist < (result.ring_radius * 0.4)

        result = fitter.fit(x[mask], y[mask], image[mask])

    assert np.isclose(
        result.ring_radius.to_value(u.rad), ring_radius / focal_length,
        rtol=0.05,
    )
    assert np.isclose(
        result.ring_center_x.to_value(u.rad), center_x / focal_length,
        rtol=0.05
    )
    assert np.isclose(
        result.ring_center_y.to_value(u.rad), center_y / focal_length,
        rtol=0.05,
    )
    def start(self):
        # n_events = self.reader.num_events
        source = self.reader.read()
        desc = "Looping through file"
        for event in tqdm(source, desc=desc): #, total=n_events):
            ev = event.count
            self.calibrator.calibrate(event)

            for tel_id in event.r0.tels_with_data:
                geom = self.geometry.get_camera(tel_id)
                nom_geom = self.geometry.get_nominal(tel_id)

                image = event.dl1.tel[tel_id].image[0]

                # Cleaning
                cuts = self.tail_cut[geom.cam_id]
                tc = tailcuts_clean(geom, image, *cuts)
                if not tc.any():
                    # self.log.warning('No image')
                    continue
                # cleaned = np.ma.masked_array(image, mask=~tc)
                cleaned = image * tc

                # Hillas
                try:
                    hillas = hillas_parameters(nom_geom, cleaned)
                except HillasParameterizationError:
                    # self.log.warning('HillasParameterizationError')
                    continue

                # embed()

                fig = plt.figure(figsize=(10, 10))
                ax = fig.add_subplot(111)
                camera = CameraDisplay(nom_geom, ax=ax, image=image, cmap='viridis')
                camera.add_colorbar()
                cen_x = u.Quantity(hillas.cen_x).value
                cen_y = u.Quantity(hillas.cen_y).value
                length = u.Quantity(hillas.length).value
                width = u.Quantity(hillas.width).value

                print(cen_x, cen_y, length, width)

                camera.add_ellipse(centroid=(cen_x, cen_y),
                                   length=length * 2,
                                   width=width * 2,
                                   angle=hillas.psi.rad)

                plt.show()
Exemple #18
0
 def __call__(self, cameras):
     if not isinstance(cameras, list):
         cameras = [cameras]
     results = []
     for (charge, peak) in cameras:
         if self.tailcuts_clean_params is not None:
             cleanmask = tailcuts_clean(self.geometry, charge, **self.tailcuts_clean_params)
             charge[~cleanmask] = 0.0
             for _ in range(3):
                 cleanmask = dilate(self.geometry, cleanmask)
             peak[~cleanmask] = 0.0
         charge /= self.charge_scaler_value
         if self.peak_scaler is not None:
             peak  = self.peak_scaler.transform(peak.reshape((-1, 1))).flatten()
         results.append((charge, peak))
     return results
Exemple #19
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')
def test_ChaudhuriKunduRingFitter():

    geom = CameraGeometry.from_name('HESS-I')

    ring_rad = np.deg2rad(1. * u.deg) * 15.  # make sure this is in camera coordinates
    ring_width = np.deg2rad(0.05 * u.deg) * 15.
    geom_pixall = np.empty(geom.pix_x.shape + (2,))
    geom_pixall[..., 0] = geom.pix_x.value
    geom_pixall[..., 1] = geom.pix_y.value

    # image = generate_muon_model(geom_pixall, ring_rad, ring_width, 0.3, 0.2)
    muon_model = partial(toymodel.generate_muon_model, radius=ring_rad.value,
                         width=ring_width.value, centre_x=-0.2, centre_y=-0.3)

    toymodel_image, toy_signal, toy_noise = \
        toymodel.make_toymodel_shower_image(geom, muon_model)

    clean_toy_mask = tailcuts_clean(geom, toymodel_image,
                                    boundary_thresh=5, picture_thresh=10)

    muonring = muon_ring_finder.ChaudhuriKunduRingFitter(None)

    x = np.rad2deg((geom.pix_x.value / 15.) * u.rad)  # .value
    y = np.rad2deg((geom.pix_y.value / 15.) * u.rad)  # .value

    muonringparam = muonring.fit(x, y, toymodel_image * clean_toy_mask)

    dist = np.sqrt(np.power(x - muonringparam.ring_center_x, 2)
                   + np.power(y - muonringparam.ring_center_y, 2))
    ring_dist = np.abs(dist - muonringparam.ring_radius)
    muonringparam = muonring.fit(x, y, toymodel_image * (ring_dist <
                                                         muonringparam.ring_radius * 0.4))

    dist = np.sqrt(np.power(x - muonringparam.ring_center_x, 2) +
                   np.power(y - muonringparam.ring_center_y, 2))
    ring_dist = np.abs(dist - muonringparam.ring_radius)
    muonringparam = muonring.fit(x, y, toymodel_image * (ring_dist <
                                                         muonringparam.ring_radius * 0.4))

    print('Fitted ring radius', muonringparam.ring_radius, 'c.f.', ring_rad)
    print('Fitted ring centre', muonringparam.ring_center_x, muonringparam.ring_center_y)

    assert muonringparam.ring_radius is not ring_rad  # .value
    assert muonringparam.ring_center_x is not -0.2
    assert muonringparam.ring_center_y is not -0.3
Exemple #22
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')
Exemple #23
0
        def update(frame):

            self.log.debug("Frame=", 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,
            ]
Exemple #24
0
def main(filename, config_file=None):

    geom = read_camera_geometries(filename)["LSTCam"]

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

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

    params_cleaning = get_cleaning_config(config_file)

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

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

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

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

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

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

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

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

            pp.savefig(dpi=100)
Exemple #25
0
def clean_charge(charge: np.array,
                 cam_name: str,
                 boundary_threshold: float = None,
                 picture_threshold: float = None,
                 min_neighbours: float = None):
    geometry = get_camera_geometry(cam_name)
    clean_default_params = cleaning_level.get(cam_name)
    if boundary_threshold is None:
        boundary_threshold = clean_default_params[0]
    if picture_threshold is None:
        picture_threshold = clean_default_params[1]

    mask = tailcuts_clean(geometry,
                          charge,
                          boundary_thresh=boundary_threshold,
                          picture_thresh=picture_threshold)
    new_charge = np.copy(charge)
    new_charge[~mask] = 0
    return new_charge, mask
Exemple #26
0
        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,]
Exemple #27
0
def find_nice_event():
    filename = datasets.get_dataset_path("gamma_test_large.simtel.gz")
    source = event_source(filename)
    calib = CameraCalibrator()
    
    for i, event in tqdm(enumerate(source)):
        # from IPython import embed; embed()
        # if i in [0, 1, 2, 3, 4, 39]:  # skip ugly events
        #     print(i, event.mc.energy)
        #     continue 
        subarray = event.inst.subarray
        calib(event)
        for tel_id in event.dl0.tels_with_data:
            # Camera Geometry required for hillas parametrization
            camgeom = subarray.tel[tel_id].camera
            # note the [0] is for channel 0 which is high-gain channel
            image = event.dl1.tel[tel_id].image
            # Cleaning  of the image
            cleaned_image = image.copy()
            # create a clean mask of pixels above the threshold
            cleanmask = tailcuts_clean(
                camgeom, image, picture_thresh=10, boundary_thresh=5, min_number_picture_neighbors=3
            )
            # set all rejected pixels to zero
            cleaned_image[~cleanmask] = 0

            # Calculate hillas parameters
            try:
                d = hillas_parameters(camgeom, cleaned_image)
            except HillasParameterizationError:
                pass  # skip failed parameterization (normally no signal)
            # from IPython import embed; embed()
            tel_name = event.inst.subarray.tel[tel_id].name
            if tel_name == 'LST' and d.r < 1 * u.m and d.intensity > 400:
                print(i, d.intensity, event.mc.energy)
                return tel_id, d, event, cleanmask
    def start(self):

        disp = None

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

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

            self.calibrator.calibrate(event)

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

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

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

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

                disp.image = im

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

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

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

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

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

        pass
    def start(self):

        disp = None

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

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

            self.calibrator(event)

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

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

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

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

                disp.image = im

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

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

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

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

        if disp is None:
            self.log.warning(
                "No events for tel {} were found in {}. Try a "
                "different EventIO file or another telescope".format(
                    self.tel, self.infile))
Exemple #30
0
        for tel_id in event.dl0.tel.keys():

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

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

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

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

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

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

            timing_dict[tel_id] = timing_parameters(camgeom, image, time,
                                                    hillas_dict[tel_id],
Exemple #31
0
    def start(self):
        df_list = []

        it = self.reader_df.iterrows()
        n_rows = len(self.reader_df.index)
        desc = "Looping over files"
        for index, row in tqdm(it, total=n_rows, desc=desc):
            type_ = row['type']
            reader = row['reader']
            calibrator = row['calibrator']
            pos = row['pos']
            geom = row['geom']
            t1 = row['t1']
            t2 = row['t2']

            desc = "Processing Events"
            source = reader.read()
            n_events = reader.num_events
            for event in tqdm(source, total=n_events, desc=desc):
                for telid in event.r0.tels_with_data:
                    ev = event.count
                    event_id = event.r0.event_id
                    time = event.trig.gps_time.value
                    calibrator.calibrate(event)
                    image = event.dl1.tel[telid].image[0]

                    # Cleaning
                    tc = tailcuts_clean(geom, image, t1, t2)
                    if not tc.any():
                        # self.log.warning('No image')
                        continue
                    cleaned_dl1 = np.ma.masked_array(image, mask=~tc)

                    wf = event.dl1.tel[telid].cleaned[0]
                    peak_time = np.ma.argmax(wf, axis=1)
                    peak_time_m = np.ma.masked_array(peak_time, mask=~tc)
                    shower_duration = peak_time_m.max() - peak_time_m.min()

                    try:
                        hillas = hillas_parameters(*pos, cleaned_dl1)
                    except HillasParameterizationError:
                        # self.log.warning('HillasParameterizationError')
                        continue

                    if np.isnan(hillas.width):
                        # self.log.warning("Hillas width == NaN")
                        continue

                    d = dict(type=type_,
                             index=ev, id=event_id, time=time, tel=telid,
                             image=image, tc=tc, peak_time=peak_time,
                             duration=shower_duration,
                             hillas=hillas,
                             h_size=hillas.size,
                             h_cen_x=hillas.cen_x.value,
                             h_cen_y=hillas.cen_y.value,
                             h_length=hillas.length.value,
                             h_width=hillas.width.value,
                             h_r=hillas.r.value,
                             h_phi=hillas.phi.value,
                             h_psi=hillas.psi.value,
                             h_miss=hillas.miss.value,
                             h_skewness=hillas.skewness,
                             h_kurtosis=hillas.kurtosis
                             )
                    df_list.append(d)

        self.df = pd.DataFrame(df_list)
        store = pd.HDFStore('/Volumes/gct-jason/plots/checm_paper/df/hillas.h5')
        store['df'] = self.df

        store = pd.HDFStore('/Volumes/gct-jason/plots/checm_paper/df/hillas.h5')
        self.df = store['df']
        self.df.loc[:, 'h_width'] /= 40.344e-3
        self.df.loc[:, 'h_length'] /= 40.344e-3
Exemple #32
0
    disp = CameraDisplay(geom)
    disp.set_limits_minmax(0, 300)
    disp.add_colorbar()

    # Create a fake camera image to display:
    model = toymodel.generate_2d_shower_model(centroid=(0.2, 0.0),
                                              width=0.01,
                                              length=0.1,
                                              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
Exemple #33
0
def get_events(filename,
               storedata=False,
               concatenate=False,
               storeimg=False,
               outdir='./results/'):
    """
    Read a Simtelarray file, extract pixels charge, calculate image parameters and timing
    parameters and store the result in an hdf5 file. 
    
    Parameters:
    filename: str
    Name of the simtelarray file.

    storedata: boolean
    True: store extracted data in a hdf5 file

    concatenate: boolean
    True: store the extracted data at the end of an existing file

    storeimg: boolean
    True: store also pixel data
    
    outdir: srt
    Output directory
    
    
    
    """
    #Particle type:

    particle_type = guess_type(filename)

    #Create data frame where DL2 data will be stored:

    features = [
        'ObsID', 'EvID', 'mcEnergy', 'mcAlt', 'mcAz', 'mcCore_x', 'mcCore_y',
        'mcHfirst', 'mcType', 'GPStime', 'width', 'length', 'w/l', 'phi',
        'psi', 'r', 'x', 'y', 'intensity', 'skewness', 'kurtosis', 'mcAlttel',
        'mcAztel', 'impact', 'mcXmax', 'time_gradient', 'intercept', 'SrcX',
        'SrcY', 'disp', 'hadroness'
    ]
    output = pd.DataFrame(columns=features)

    #Read LST1 events:
    source = EventSourceFactory.produce(
        input_url=filename, allowed_tels={1})  #Open Simtelarray file

    #Cleaning levels:

    level1 = {'LSTCam': 6.}
    level2 = level1.copy()
    # We use as second cleaning level just half of the first cleaning level
    for key in level2:
        level2[key] *= 0.5

    log10pixelHGsignal = {}
    survived = {}

    imagedata = np.array([])

    for key in level1:

        log10pixelHGsignal[key] = []
        survived[key] = []
    i = 0
    for event in source:
        if i % 100 == 0:
            print("EVENT_ID: ", event.r0.event_id, "TELS: ",
                  event.r0.tels_with_data, "MC Energy:", event.mc.energy)
        i = i + 1
        ntels = len(event.r0.tels_with_data)
        '''
        if i > 100:   # for quick tests
            break
        '''
        for ii, tel_id in enumerate(event.r0.tels_with_data):

            geom = event.inst.subarray.tel[tel_id].camera  #Camera geometry
            tel_coords = event.inst.subarray.tel_coords[
                event.inst.subarray.tel_indices[tel_id]]
            data = event.r0.tel[tel_id].waveform
            ped = event.mc.tel[tel_id].pedestal
            # the pedestal is the average (for pedestal events) of the *sum* of all samples, from sim_telarray

            nsamples = data.shape[2]  # total number of samples
            pedcorrectedsamples = data - np.atleast_3d(
                ped
            ) / nsamples  # Subtract pedestal baseline. atleast_3d converts 2D to 3D matrix

            integrator = LocalPeakIntegrator(None, None)
            integration, peakpos, window = integrator.extract_charge(
                pedcorrectedsamples
            )  # these are 2D matrices num_gains * num_pixels

            chan = 0  # high gain used for now...
            signals = integration[chan].astype(float)

            dc2pe = event.mc.tel[tel_id].dc_to_pe  # numgains * numpixels
            signals *= dc2pe[chan]

            # Add all individual pixel signals to the numpy array of the corresponding camera inside the log10pixelsignal dictionary
            log10pixelHGsignal[str(geom)].extend(
                np.log10(signals)
            )  # This seems to be faster like this, with normal python lists

            # Apply image cleaning
            cleanmask = tailcuts_clean(geom,
                                       signals,
                                       picture_thresh=level1[str(geom)],
                                       boundary_thresh=level2[str(geom)],
                                       keep_isolated_pixels=False,
                                       min_number_picture_neighbors=1)
            survived[str(geom)].extend(
                cleanmask
            )  # This seems to be faster like this, with normal python lists

            clean = signals.copy()
            clean[
                ~cleanmask] = 0.0  # set to 0 pixels which did not survive cleaning
            if np.max(clean) < 1.e-6:  # skip images with no pixels
                continue

            # Calculate image parameters
            hillas = hillas_parameters(
                geom,
                clean)  # this one gives some warnings invalid value in sqrt
            foclen = event.inst.subarray.tel[
                tel_id].optics.equivalent_focal_length

            w = np.rad2deg(np.arctan2(hillas.width, foclen))
            l = np.rad2deg(np.arctan2(hillas.length, foclen))

            #Calculate Timing parameters

            peak_time = units.Quantity(peakpos[chan]) * units.Unit("ns")
            timepars = time.timing_parameters(geom.pix_x, geom.pix_y, clean,
                                              peak_time, hillas.psi)

            if w >= 0:

                if storeimg == True:
                    if imagedata.size == 0:
                        imagedata = clean
                    else:
                        imagedata = np.vstack([imagedata,
                                               clean])  #Pixel content

                width = w.value
                length = l.value
                phi = hillas.phi.value
                psi = hillas.psi.value
                r = hillas.r.value
                x = hillas.x.value
                y = hillas.y.value
                intensity = np.log10(hillas.intensity)
                skewness = hillas.skewness
                kurtosis = hillas.kurtosis

                #Store parameters from event and MC:
                ObsID = event.r0.obs_id
                EvID = event.r0.event_id

                mcEnergy = np.log10(event.mc.energy.value *
                                    1e3)  #Log10(Energy) in GeV
                mcAlt = event.mc.alt.value
                mcAz = event.mc.az.value
                mcCore_x = event.mc.core_x.value
                mcCore_y = event.mc.core_y.value
                mcHfirst = event.mc.h_first_int.value
                mcType = event.mc.shower_primary_id
                mcAztel = event.mcheader.run_array_direction[0].value
                mcAlttel = event.mcheader.run_array_direction[1].value
                mcXmax = event.mc.x_max.value
                GPStime = event.trig.gps_time.value

                impact = np.sqrt(
                    (tel_coords.x.value - event.mc.core_x.value)**2 +
                    (tel_coords.y.value - event.mc.core_y.value)**2)

                time_gradient = timepars[0].value
                intercept = timepars[1].value

                #Calculate Disp and Source position in camera coordinates
                tel = OpticsDescription.from_name(
                    'LST')  #Telescope description
                focal_length = tel.equivalent_focal_length.value
                sourcepos = transformations.calc_CamSourcePos(
                    mcAlt, mcAz, mcAlttel, mcAztel, focal_length)
                SrcX = sourcepos[0]
                SrcY = sourcepos[1]
                disp = transformations.calc_DISP(sourcepos[0], sourcepos[1], x,
                                                 y)

                hadroness = 0
                if particle_type == 'proton':
                    hadroness = 1

                eventdf = pd.DataFrame([[
                    ObsID, EvID, mcEnergy, mcAlt, mcAz, mcCore_x, mcCore_y,
                    mcHfirst, mcType, GPStime, width, length, width / length,
                    phi, psi, r, x, y, intensity, skewness, kurtosis, mcAlttel,
                    mcAztel, impact, mcXmax, time_gradient, intercept, SrcX,
                    SrcY, disp, hadroness
                ]],
                                       columns=features)

                output = output.append(eventdf, ignore_index=True)

    outfile = outdir + particle_type + '_events.hdf5'

    if storedata == True:

        if concatenate == False or (concatenate == True and
                                    np.DataSource().exists(outfile) == False):
            output.to_hdf(outfile, key=particle_type + "_events", mode="w")
            if storeimg == True:
                f = h5py.File(outfile, 'r+')
                f.create_dataset('images', data=imagedata)
                f.close()
        else:
            if storeimg == True:
                f = h5py.File(outfile, 'r')
                images = f['images']
                del f['images']
                images = np.vstack([images, imagedata])
                f.close()
                saved = pd.read_hdf(outfile, key=particle_type + '_events')
                output = saved.append(output, ignore_index=True)
                output.to_hdf(outfile, key=particle_type + "_events", mode="w")
                f = h5py.File(outfile, 'r+')
                f.create_dataset('images', data=images)
                f.close()
            else:
                saved = pd.read_hdf(outfile, key=particle_type + '_events')
                output = saved.append(output, ignore_index=True)
                output.to_hdf(outfile, key=particle_type + "_events", mode="w")
    del source
    return output
        # input_url="/data/20181011_M1_05075881.001_Y_CrabNebula-W0.40+359.root"
        input_url="/data/magic_test.root")

    # get a single image
    for i, img in enumerate(source):
        # Pick a (random) good looking image
        if i > 147:
            break
    im = img.dl1.tel[0].image
    print(img)

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

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

        # Show the camera image and overlay Hillas ellipse and clean pixels
        disp.image = im
        disp.highlight_pixels(cleanmask, color="crimson")
        disp.overlay_moments(hillas, color="red", linewidth=2)
Exemple #35
0
time_image = 0.2 * longi.to_value(u.mm) + 25

time = np.average(np.column_stack([time_noise, time_image]),
                  weights=np.column_stack([noise, signal]) + 1,
                  axis=1)

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])
Exemple #36
0
        source = hessio_event_source(filename, max_events=1000)
        for event in source:
            try:
                calib.calibrate(event)
                if display_each is True:
                    if disp is None:
                        geom = event.inst.subarray.tel[1].camera
                        disp = CameraDisplay(geom)
                        disp.add_colorbar()
                        plt.show(block=False)
                else:
                    geom = event.inst.subarray.tel[1].camera
                im = event.dl1.tel[1].image[0]
                mask = tailcuts_clean(geom,
                                      im,
                                      picture_thresh=10,
                                      boundary_thresh=5)
                im[~mask] = 0.0
                maxpe = max(im)
                mcenergy = event.mc.energy
                if display_each is True:
                    disp.image = im
                params = hillas_parameters(geom=geom, image=im)

                if params.cen_x.value > 0.2 or params.cen_x.value < -0.2 or params.cen_y.value > 0.2 or params.cen_y.value < -0.2:
                    continue

                if write is True:
                    outfile.write(
                        '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n'
                        %
Exemple #37
0
        # telescope pointing direction
        point_azimuth[tel_id] = event.mc.tel[tel_id].azimuth_raw * u.rad
        point_altitude[tel_id] = event.mc.tel[tel_id].altitude_raw * u.rad
        #        print(point_azimuth,point_altitude)

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

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

        # Cleaning  of the image
        cleaned_image = image
        # create a clean mask of pixels above the threshold
        cleanmask = tailcuts_clean(
            camgeom, image, picture_thresh=10, boundary_thresh=5
        )
        # set all rejected pixels to zero
        cleaned_image[~cleanmask] = 0

        # Calulate hillas parameters
        # It fails for empty pixels
        try:
            hillas_params[tel_id] = hillas_parameters(camgeom, cleaned_image)
        except:
            pass

    if len(hillas_params) < 2:
        continue

    reco_result = reco.predict(hillas_params, event.inst, point_altitude, point_azimuth)
Exemple #38
0
        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, ]
Exemple #39
0
    def start(self):

        disp = None

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

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

            self.calibrator.calibrate(event)

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

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

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

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

                disp.image = im

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

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

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

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

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

        pass
Exemple #40
0
    def loadFile_stereo(self, path1, path2):

        stereo_event_list = []

        print("path1: ", path1)
        print("path2: ", path2)

        event_factory1 = MAGICEventSourceMC(input_url=path1)
        event_factory2 = MAGICEventSourceMC(input_url=path2)

        event_generator1 = event_factory1._generator()
        event_generator2 = event_factory2._generator()

        cnt_t = 0
        cnt_id1 = 0
        cnt_id2 = 0

        end1 = False
        end2 = False

        event1 = next(event_generator1, None)
        event2 = next(event_generator2, None)

        camera = ctapipe.instrument.CameraGeometry.from_name("MAGICCam")

        while end1 == False and end2 == False:
            if event2 is None:
                end2 = True
            else:
                id2 = event2.r0.event_id
            if event1 is None:
                end1 = True
            else:
                id1 = event1.r0.event_id
            if end1 == False and end2 == False:
                if id1 == id2:
                    # print("True")
                    nxt = True

                    cnt_id1 += 1
                    cnt_id2 += 1
                    cnt_t += 1

                    image1 = event1.dl1.tel[1].image
                    image2 = event2.dl1.tel[2].image
                    # cut events with tailcuts_clean
                    boundary, picture, min_neighbors = (
                        3.5, 6, 1)  #(5, 10, 3)#cleaning_level[camera.cam_id]
                    clean1 = tailcuts_clean(
                        camera,
                        image1,
                        boundary_thresh=boundary,
                        picture_thresh=picture,
                        min_number_picture_neighbors=min_neighbors)
                    clean2 = tailcuts_clean(
                        camera,
                        image2,
                        boundary_thresh=boundary,
                        picture_thresh=picture,
                        min_number_picture_neighbors=min_neighbors)
                    #print("B1: ", clean1.sum(), " B2: ", clean2.sum() )
                    if clean1.sum() > 5 and clean2.sum() > 5:
                        self.eventList.append({
                            "M1": copy.deepcopy(event1),
                            "M2": copy.deepcopy(event2)
                        })
                    event1 = next(event_generator1, None)
                    event2 = next(event_generator2, None)

                elif id1 > id2:
                    event2 = next(event_generator2, None)
                    cnt_id2 += 1
                elif id1 < id2:
                    event1 = next(event_generator1, None)
                    cnt_id1 += 1

        # count to the end
        '''
        if True:  # end1 == True:
            while end2 == False:
                event2 = next(event_generator2, None)
                cnt_id2 += 1
                if event2 is None:
                    end2 = True
                # print("Counting 1...")
        if True:  # end2 == True:
            while end1 == False:
                event1 = next(event_generator1, None)
                cnt_id1 += 1
                if event1 is None:
                    end1 = True
                    # print("Counting 2...")
        '''
        print(cnt_t)
        # event_factory.pyhessio.close_file()
        print("New File loaded, file {:d} contains {:d} events".format(
            self.currentFileID, len(self.eventList)))
        return True