예제 #1
0
def plot_image_representations(event, generated_images=()):
    """
    Plot the default FACT representation, as well as different outputs from generators or preprocessors

    Currently, generated images are assumed to be 2D representations

    :param event:
    :return:
    """

    # Plot default FACT plot

    camera(event.photon_stream.list_of_lists)
    plt.show()

    # Plot generated_image
    for image in generated_images:
        plt.imshow(image)
예제 #2
0
def main(inputfile, output, threshold):
    df = pd.read_csv(inputfile)

    x, y = get_pixel_coords()
    df['x'] = x
    df['y'] = y
    df['r'] = np.sqrt(df.x**2 + df.y**2)

    psf = df.sigma.values
    psf[df.A.values < threshold] = np.nan

    sigma = np.ma.masked_invalid(df.sigma.values)
    cmap = plt.get_cmap('viridis')
    cmap.set_bad('lightgray')
    camera(sigma, cmap=cmap)

    df = df.query('A >= @threshold')

    plt.show()

    xplot = np.linspace(0, 190, 2)

    # only fit the main star going through the camera center
    df = df.loc[(df.y < 30) & (df.y > -30)].dropna()

    a, b = np.polyfit(df.r.values, df.sigma.values, deg=1)

    fig, ax = plt.subplots()

    ax.scatter('r', 'sigma', data=df)
    ax.plot(xplot, a * xplot + b, color='C1', label='Linear regression')

    ax.set_xlabel(r'$d \,\, / \,\, \mathrm{mm}$')
    ax.set_ylabel(r'$\sigma \,\, / \,\, \mathrm{mm}$')

    fig.tight_layout()
    if output:
        fig.savefig(output, dpi=300)
    else:
        plt.show()
예제 #3
0
import matplotlib.pyplot as plt
import fact.plotting as factplot

import numpy as np

data = np.random.normal(loc=5, scale=1, size=1440)
bad_pixels = [863, 868, 297, 927, 80, 873, 1093, 1094, 527, 528, 721, 722]

f, axes = plt.subplots(2, 2)

axes[0, 0].plot(data, ".")
axes[0, 0].set_title("Data vs pixel Id")

axes[0, 1].hist(data, bins=np.linspace(0, 10, 100))
axes[0, 1].set_title("distribution")

factplot.camera(data, ax=axes[1, 0])
factplot.mark_pixel(bad_pixels, ax=axes[1, 0], linewidth=1)
axes[1, 0].set_title("bad_pixels highlighted")

factplot.camera(data, ax=axes[1, 1])
factplot.mark_pixel(data > 6, ax=axes[1, 1], linewidth=1)
axes[1, 1].set_title("data > 6 highlighted")

plt.suptitle("Maximize me and see me adjust the pixel size")

plt.show()
예제 #4
0
def main(method, path, file, feat, number):

    border_pix = get_border_pixel_mask()

    print("Reading in facttools dl1 file...")
    t = Table.read('/net/big-tank/POOL/projects/fact/photon-stream/facttools/crab/{}_dl1.fits'.format(file))
    print("Reading in facttools dl2 file...")
    dl2 = read_data('/home/ksedlaczek/Packages/open_crab_sample_analysis/dl2/crab.hdf5', key='events')
    print("Reading in PhotonStream data file...")
    reader = ps.EventListReader('/net/big-tank/POOL/projects/fact/photon-stream/stream_data/{}/{}.phs.jsonl.gz'.format(path, file))
    print("Done...")
    event = next(reader)

    fig, axs = plt.subplots(3, 1, figsize=(4, 10), constrained_layout=True)

    plots = [camera(np.zeros(1440), cmap='inferno', ax=ax) for ax in axs]
    cbars = [fig.colorbar(plot, ax=ax) for plot, ax in zip(plots, axs)]
    plots[1].set_cmap('RdBu_r')

    with PdfPages('pe_difference_{}_{}.pdf'.format(feat, file)) as pdf:
        for i in tqdm(range(number)):

            if is_simulation_event(event):
                event_num_phs = event.simulation_truth.event
                reuse_phs = event.simulation_truth.reuse
                run_id_phs = event.simulation_truth.run
            else:
                event_num_phs = event.observation_info.event
                reuse_phs = 42
                run_id_phs = event.observation_info.run

            if path != 'crab':
                run_id = file
                event_num = t[i]['MCorsikaEvtHeader.fEvtNumber']
                reuse = t[i]['MCorsikaEvtHeader.fNumReuse']
            else:
                run_id = file
                event_num = t[i]['EventNum']
                reuse = 42

            assert run_id != run_id_phs

            while (event_num_phs != event_num or reuse != reuse_phs):
                event = next(reader)
                if is_simulation_event(event):
                    event_num_phs = event.simulation_truth.event
                    reuse_phs = event.simulation_truth.reuse
                    run_id_phs = event.simulation_truth.run
                else:
                    event_num_phs = event.observation_info.event
                    reuse_phs = 42
                    run_id_phs = event.observation_info.run

            lol = event.photon_stream.list_of_lists
            # cut away unwanted time slices
            # lol = [[t for t in l if ((35 <= t) & (t < 75))] for l in lol]
            image = phs2image(lol)

            if method == 'DBSCAN':
                clustering = ps.photon_cluster.PhotonStreamCluster(event.photon_stream)
                biggest_cluster = np.argmax(np.bincount(clustering.labels[clustering.labels != -1]))
                mask = clustering.labels == biggest_cluster

                cleaned_pix_phs = np.zeros(len(image), dtype=bool)
                k = 0
                cleaned_img = np.zeros(len(image))
                for h in range(len(lol)):
                    for j in range(len(lol[h])):
                        k += 1
                        if mask[k-1]:
                            cleaned_pix_phs[h] = True
            else:
                cleaned_pix_phs = facttools_cleaning(image, lol, picture_thresh, boundary_thresh)
                if sum(cleaned_pix_phs) < 1:
                    break

            cleaned_pix = t[i]['shower']

            t[i]['photoncharge'][t[i]['photoncharge'] < 0] = 0.0
            pe_difference = image - t[i]['photoncharge']

            max_abs = np.max(np.abs(pe_difference))

            plots[0].set_array(image)
            plots[1].set_array(pe_difference)
            plots[2].set_array(t[i]['photoncharge'])

            plots[0].autoscale()
            plots[1].set_clim(-max_abs, max_abs)
            plots[2].autoscale()
            # mark_pixel(t[i]['shower'], color=(128/255, 186/255, 38/255), linewidth=2.5)
            for ax in axs:
                ax.axis('off')

            for cbar, plot in zip(cbars, plots):
                cbar.update_bruteforce(plot)

            # embed()
            if is_simulation_event(event):
                fig.suptitle('run {} event {} reuse {} mean {:.2f}'.format(run_id, event_num, reuse, np.mean(pe_difference)))
            else:
                # fig.suptitle('{} event {} mean {:.2f}'.format(file, event_num, np.mean(pe_difference)))
                fig.suptitle('{} event {}'.format(file[:8] + ' ' + file[9:], event_num))
            pdf.savefig(fig)
def plot_event(
    event,
    path,
    pixel_edgecolor='#D0D0D0',
    cmap='Blues',
    dpi=256
):
    e = event
    print(
        'true_as', e['image_true'].sum(),
        'dc_as', e['image_dc'].sum(),
        'mp_as', e['image_mp'].sum(),
        'nsb rate MBq',  e['mean_nsb_rate']/1e6)

    fig_w = 9
    fig_h = 3
    im_w = fig_w/3
    dpi = 180
    fig = plt.figure(figsize=(fig_w, fig_h+0.6), dpi=dpi)
    ho = 0.25
    ax0 = fig.add_axes((0*im_w/fig_w,
                        (ho + 0)/fig_h,
                        im_w/fig_w,
                        fig_h/fig_h))

    ax1 = fig.add_axes((1*im_w/fig_w,
                        (ho + 0)/fig_h,
                        im_w/fig_w,
                        fig_h/fig_h))

    ax2 = fig.add_axes((2*im_w/fig_w,
                        (ho + 0)/fig_h,
                        im_w/fig_w,
                        fig_h/fig_h))

    add_ring_2_ax(x=0, y=0, r=IMAGE_SENSOR_RADIUS, ax=ax0)
    add_ring_2_ax(x=0, y=0, r=IMAGE_SENSOR_RADIUS, ax=ax1)
    add_ring_2_ax(x=0, y=0, r=IMAGE_SENSOR_RADIUS, ax=ax2)
    fa_plot.camera(
        e['image_dc'],
        cmap=cmap,
        edgecolor=pixel_edgecolor,
        ax=ax0)
    fa_plot.camera(
        e['image_true'],
        cmap=cmap,
        edgecolor=pixel_edgecolor,
        ax=ax1)
    fa_plot.camera(e['image_mp'],
        cmap=cmap,
        edgecolor=pixel_edgecolor,
        ax=ax2)

    #fig.suptitle(
    #    'NSB ' +
    #    '{:.1f}M photons/(pixel s)'.format(e['mean_nsb_rate']/1e6))

    #ax0.set_title('density-clustering\nin photon-stream', fontsize=16)
    ax0.set_xlabel(
        '{:d} photons\n'.format(e['image_dc'].sum()) +
        r'$\delta=${:.1f}$^\circ$, '.format(np.rad2deg(e['delta_dc'])) +
        r'$D=${:.1f} photons'.format(e['dist_dc']),
        fontsize=16
    )

    #ax1.set_title('Truth', fontsize=16)
    ax1.set_xlabel(
        '{:d} photons'.format(e['image_true'].sum()),
        fontsize=16)

    #ax2.set_title('two-level-time-neighbor\non main-pulses', fontsize=16)
    ax2.set_xlabel(
        '{:.1f} photon-equivalents\n'.format(e['image_mp'].sum()) +
        r'$\delta=${:.1f}$^\circ$, '.format(np.rad2deg(e['delta_mp'])) +
        r'$D=${:.1f} p.e.'.format(e['dist_mp']),
        fontsize=16
    )

    for side in ['bottom', 'right', 'top', 'left']:
        ax0.spines[side].set_visible(False)
        ax1.spines[side].set_visible(False)
        ax2.spines[side].set_visible(False)

    plt.setp(ax0.get_xticklabels(), visible=False)
    plt.setp(ax1.get_xticklabels(), visible=False)
    plt.setp(ax2.get_xticklabels(), visible=False)

    plt.setp(ax0.get_yticklabels(), visible=False)
    plt.setp(ax1.get_yticklabels(), visible=False)
    plt.setp(ax2.get_yticklabels(), visible=False)

    ax0.get_xaxis().set_ticks([])
    ax1.get_xaxis().set_ticks([])
    ax2.get_xaxis().set_ticks([])

    ax0.get_yaxis().set_ticks([])
    ax1.get_yaxis().set_ticks([])
    ax2.get_yaxis().set_ticks([])
    plt.savefig(path, dpi=dpi)
    plt.close('all')
예제 #6
0
def test_cameraplot():
    from fact.plotting import camera
    from numpy.random import uniform

    data = uniform(0, 20, 1440)
    camera(data)
def main(method, path, file, feat, number):

    border_pix = get_border_pixel_mask()
    if method == "thresholds":
        reader = ps.EventListReader(
            '/net/big-tank/POOL/projects/fact/photon-stream/stream_data/{}/{}.phs.jsonl.gz'
            .format(path, file))
        with PdfPages('cleaning_thresh_{}_{}.pdf'.format(feat, file)) as pdf:
            for i in tqdm(range(number)):
                fig = plt.figure()
                ax = fig.add_axes([0.05, 0.05, 0.9, 0.9])
                #ax.set_axis_off()
                event = next(reader)

                lol = event.photon_stream.list_of_lists
                lol = [[t for t in l if ((35 <= t) & (t < 75))] for l in lol]
                image = phs2image(lol)  #, lower=30, upper=70)
                cleaned_pix = facttools_cleaning(image, lol, 35, 75,
                                                 picture_thresh,
                                                 boundary_thresh)
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    arrival_times = np.array([np.nanmedian(l) for l in lol])
                # cleaned_pix = cleaning(image, lol, picture_thresh, boundary_thresh)
                if len(cleaned_pix[cleaned_pix != 0]) > 1:
                    # border_ph = [(border_pix[i] and cleaned_pix[i]) for i in range(1440)]
                    # leakage = image[border_ph].sum()/image[cleaned_pix].sum()
                    df = calc_hillas_features_image(image, cleaned_pix)
                    # ell = Ellipse(
                    #     [df['cog_x'], df['cog_y']],
                    #     df['length']*2,
                    #     df['width']*2,
                    #     angle=np.rad2deg(df['delta']),
                    #     fill=False, linewidth=2, color='b'
                    # )
                    # ax.add_patch(ell)
                    ell = Ellipse([df['cog_x'], df['cog_y']],
                                  df['length'] * 4,
                                  df['width'] * 4,
                                  angle=np.rad2deg(df['delta']),
                                  fill=False,
                                  linewidth=1.5,
                                  color='b')
                    # ax.add_patch(ell)
                    if is_simulation_event(event):
                        fig.suptitle('run {} event {} reuse {}'.format(
                            event.simulation_truth.run,
                            event.simulation_truth.event,
                            event.simulation_truth.reuse))
                    else:
                        fig.suptitle('{} event {} delta {}'.format(
                            file, event.observation_info.event, df['delta']))
                    if feat == 'arrival_times':
                        with warnings.catch_warnings():
                            warnings.simplefilter("ignore")
                            x = arrival_times - np.nanmean(arrival_times)
                        x[np.isnan(x)] = 0
                        c = camera(x, cmap='Spectral', ax=ax)
                        mark_pixel(cleaned_pix, color='k', linewidth=2.5)
                    else:
                        c = camera(image, cmap='viridis', ax=ax)
                        mark_pixel(cleaned_pix,
                                   color=(128 / 255, 186 / 255, 38 / 255),
                                   linewidth=2.5)
                    ax.axis('off')
                    fig.colorbar(c)
                    pdf.savefig(fig)
                    ax.cla()
                plt.close(fig)

    if method == "DBSCAN":
        reader = ps.EventListReader(
            '/net/big-tank/POOL/projects/fact/photon-stream/stream_data/{}/{}.phs.jsonl.gz'
            .format(path, file))
        with PdfPages('cleaning_DBSCAN_biggest_{}_{}.pdf'.format(feat,
                                                                 file)) as pdf:
            for i in tqdm(range(number)):
                fig = plt.figure()
                ax = fig.add_axes([0.05, 0.05, 0.9, 0.9])
                event = next(reader)

                # clustering of events
                clustering = ps.photon_cluster.PhotonStreamCluster(
                    event.photon_stream)

                if clustering.number > 0:

                    lol = event.photon_stream.list_of_lists
                    image = phs2image(lol)
                    with warnings.catch_warnings():
                        warnings.simplefilter("ignore")
                        arrival_times = np.array(
                            [np.nanmedian(l) for l in lol])
                    # biggest cluster:
                    biggest_cluster = np.argmax(
                        np.bincount(
                            clustering.labels[clustering.labels != -1]))

                    mask = clustering.labels == biggest_cluster
                    # mask = clustering.labels != -1

                    xyt = event.photon_stream.point_cloud
                    x, y, t = xyt.T
                    cleaned_pix = np.zeros(len(image), dtype=bool)
                    k = 0
                    cleaned_img = np.zeros(len(image))
                    for i in range(len(lol)):
                        for j in range(len(lol[i])):
                            k += 1
                            if mask[k - 1]:
                                cleaned_pix[i] = True
                                cleaned_img[i] += 1

                    cleaned_pix_perc = np.zeros(1440, dtype=bool)
                    for i in range(1440):
                        if cleaned_pix[i] and (cleaned_img[i] >
                                               mask.sum() / 200):
                            cleaned_pix_perc[i] = True
                    df = calc_hillas_features_phs(event.photon_stream,
                                                  clustering)
                    # ell = Ellipse(
                    #     [df['cog_x'], df['cog_y']],
                    #     df['length']*2,
                    #     df['width']*2,
                    #     angle=np.rad2deg(df['delta']),
                    #     fill=False, linewidth=2, color='b'
                    # )
                    # ax.add_patch(ell)
                    ell = Ellipse([df['cog_x'], df['cog_y']],
                                  df['length'] * 4,
                                  df['width'] * 4,
                                  angle=np.rad2deg(df['delta']),
                                  fill=False,
                                  linewidth=1.5,
                                  color='b')
                    # ax.add_patch(ell)
                    if is_simulation_event(event):
                        fig.suptitle('run {} event {} reuse {}'.format(
                            event.simulation_truth.run,
                            event.simulation_truth.event,
                            event.simulation_truth.reuse))
                    else:
                        fig.suptitle('{} event {} delta {:.2f}'.format(
                            file, event.observation_info.event,
                            np.rad2deg(df['delta'])))
                    if feat == 'arrival_times':
                        with warnings.catch_warnings():
                            warnings.simplefilter("ignore")
                            x = arrival_times - np.nanmean(arrival_times)
                        c = camera(x, cmap='viridis', ax=ax)
                        mark_pixel(cleaned_pix,
                                   color=(128 / 255, 186 / 255, 38 / 255),
                                   linewidth=2.5)
                    else:
                        c = camera(image, cmap='viridis', ax=ax)
                        mark_pixel(cleaned_pix,
                                   color=(128 / 255, 186 / 255, 38 / 255),
                                   linewidth=2.5)
                        mark_pixel(cleaned_pix_perc,
                                   color='red',
                                   linewidth=1.5)
                    ax.axis('off')
                    fig.colorbar(c)
                    pdf.savefig(fig)
                    ax.cla()
                plt.close(fig)

    if method == "facttools":
        print('facttools')
        with PdfPages('cleaning_facttools_{}_{}.pdf'.format(feat,
                                                            file)) as pdf:
            t = Table.read(
                '/net/big-tank/POOL/projects/fact/photon-stream/facttools/{}/{}_dl1.fits'
                .format(path, file))
            dl2 = read_data(
                '/home/ksedlaczek/Packages/open_crab_sample_analysis/dl2/crab.hdf5',
                key='events')

            for i in tqdm(range(number)):
                fig = plt.figure()
                ax = fig.add_axes([0.05, 0.05, 0.9, 0.9])
                #                if path != 'crab':
                #                    fig.suptitle('run {} event {} reuse {}'.format(file, t[i]['MCorsikaEvtHeader.fEvtNumber'], t[i]['MCorsikaEvtHeader.fNumReuse']))
                #                else:
                #
                #                    fig.suptitle('{} event {} delta {:.4f}'.format(file, t[i]['EventNum'], dl2.query('night == 20131104 & run_id == 162 & event_num == {}'.format(t[i]['EventNum']))['delta'].values[0]))

                t[i]['photoncharge'][t[i]['photoncharge'] < 0] = 0.0
                if feat == 'arrival_times':
                    c = camera(t[i]['arrivalTime'] -
                               t[i]['arrivalTime'].mean(),
                               cmap='Spectral',
                               ax=ax)
                    # mark_pixel(t[i]['shower'], color='k', linewidth=2.5)
                    ax.axis('off')
                    cb = fig.colorbar(c)
                    cb.set_label(label=r'$t-\bar{t}$ / ns', fontsize=16)
                else:
                    c = camera(t[i]['photoncharge'], cmap='viridis', ax=ax)
                    ax.axis('off')
                    cb = fig.colorbar(c)
                    cb.set_label(label=r'Number of Photons', fontsize=16)

                    #mark_pixel(t[i]['shower'], color=(128/255, 186/255, 38/255), linewidth=2.5)
                # mark_pixel(t[i]['shower'], color=(128/255, 186/255, 38/255), linewidth=2.5)
                pdf.savefig(fig)
                ax.cla()
                plt.close(fig)