예제 #1
0
df = pd.read_csv("data_1000keV_local.csv",
                 usecols=["event_number", "x", "y", "z"])

mask = df.event_number < 20
df = df[mask]

trk_lengths = []
for i in range(df.event_number.max() + 1):
    #df_i = df[df.event_number == i]
    hit_list = [
        BHit(row.x, row.y, row.z, 1.)
        for indx, row in df[df.event_number == i].iterrows()
    ]
    voxels = plf.voxelize_hits(hit_list, np.array((1., 1., 1.)))
    tracks = plf.make_track_graphs(voxels)

    all_lengths = []
    for j in np.arange(len(tracks)):
        all_lengths.append(plf.length(tracks[j]))

    trk_lengths.append(max(all_lengths))

plt.figure(0)
plt.hist(trk_lengths, range=(0, 200), bins=70)
plt.xlabel("Track length (mm)")
plt.savefig(
    "/data5/users/miryam/temp/voxel/img/voxel_track_length_1000keV_local_20events.pdf"
)
plt.close(0)
예제 #2
0
def create_tracks_with_skel_extremes(vox_size, energy_type, strict_vox_size,
                                     blob_radius, df_extr, hitc):
    voxels = plf.voxelize_hits(hitc.hits, vox_size, strict_vox_size,
                               energy_type)
    tracks = plf.make_track_graphs(voxels)

    df = pd.DataFrame(columns=[
        'event', 'trackID', 'energy', 'length', 'numb_of_voxels',
        'numb_of_hits', 'numb_of_tracks', 'x_min', 'y_min', 'z_min', 'x_max',
        'y_max', 'z_max', 'r_max', 'x_ave', 'y_ave', 'z_ave', 'extreme1_x',
        'extreme1_y', 'extreme1_z', 'extreme2_x', 'extreme2_y', 'extreme2_z',
        'blob1_x', 'blob1_y', 'blob1_z', 'blob2_x', 'blob2_y', 'blob2_z',
        'eblob1', 'eblob2', 'ovlp_blob_energy', 'vox_size_x', 'vox_size_y',
        'vox_size_z'
    ])

    if (len(voxels) == 0):
        return df

    vox_size_x = voxels[0].size[0]
    vox_size_y = voxels[0].size[1]
    vox_size_z = voxels[0].size[2]

    def get_track_energy(track):
        return sum([vox.Ehits for vox in track.nodes()])

    #sort tracks in energy
    tracks = sorted(tracks, key=get_track_energy, reverse=True)

    track_hits = []

    for c, t in enumerate(tracks, 0):
        tID = c
        energy = get_track_energy(t)
        length = plf.length(t)
        numb_of_hits = len([h for vox in t.nodes() for h in vox.hits])
        numb_of_voxels = len(t.nodes())
        numb_of_tracks = len(tracks)

        min_x = min([h.X for v in t.nodes() for h in v.hits])
        max_x = max([h.X for v in t.nodes() for h in v.hits])
        min_y = min([h.Y for v in t.nodes() for h in v.hits])
        max_y = max([h.Y for v in t.nodes() for h in v.hits])
        min_z = min([h.Z for v in t.nodes() for h in v.hits])
        max_z = max([h.Z for v in t.nodes() for h in v.hits])
        max_r = max([
            np.sqrt(h.X * h.X + h.Y * h.Y) for v in t.nodes() for h in v.hits
        ])

        pos = [h.pos for v in t.nodes() for h in v.hits]
        e = [getattr(h, energy_type.value) for v in t.nodes() for h in v.hits]
        ave_pos = np.average(pos, weights=e, axis=0)

        # classic paolina extremes
        extr1, extr2 = plf.find_extrema(t)
        extr1_pos = extr1.XYZ
        extr2_pos = extr2.XYZ

        t_extr = df_extr[df_extr.trackID == tID]
        if len(t_extr) == 0:
            blob_pos1 = np.array([1.e6, 1.e6, 1.e6])
            blob_pos2 = np.array([1.e6, 1.e6, 1.e6])
            eblob1 = -1
            eblob2 = -1
            overlap = -1
        else:
            blob_pos1 = np.array([
                t_extr.skel_extr1_x.values[0], t_extr.skel_extr1_y.values[0],
                t_extr.skel_extr1_z.values[0]
            ])
            blob_pos2 = np.array([
                t_extr.skel_extr2_x.values[0], t_extr.skel_extr2_y.values[0],
                t_extr.skel_extr2_z.values[0]
            ])
            e_blob1, e_blob2, hits_blob1, hits_blob2, blob_pos1, blob_pos2 = blob_energies_hits(
                t, blob_radius, blob_pos1, blob_pos2)
            overlap = sum(
                [h.E for h in set(hits_blob1).intersection(set(hits_blob2))])

        list_of_vars = [
            hitc.event, tID, energy, length, numb_of_voxels, numb_of_hits,
            numb_of_tracks, min_x, min_y, min_z, max_x, max_y, max_z, max_r,
            ave_pos[0], ave_pos[1], ave_pos[2], extr1_pos[0], extr1_pos[1],
            extr1_pos[2], extr2_pos[0], extr2_pos[1], extr2_pos[2],
            blob_pos1[0], blob_pos1[1], blob_pos1[2], blob_pos2[0],
            blob_pos2[1], blob_pos2[2], e_blob1, e_blob2, overlap, vox_size_x,
            vox_size_y, vox_size_z
        ]
        df.loc[c] = list_of_vars
        try:
            types_dict
        except NameError:
            types_dict = dict(zip(df.columns, [type(x) for x in list_of_vars]))

    #change dtype of columns to match type of variables
    df = df.apply(lambda x: x.astype(types_dict[x.name]))

    return df
예제 #3
0
    def create_extract_track_blob_info(hitc):
        df = pd.DataFrame(columns=list(types_dict_tracks.keys()))
        if len(hitc.hits) > max_num_hits:
            return df, hitc, True
        #track_hits is a new Hitcollection object that contains hits belonging to tracks, and hits that couldnt be corrected
        track_hitc = evm.HitCollection(hitc.event, hitc.time)
        out_of_map = np.any(np.isnan([h.Ep for h in hitc.hits]))
        if out_of_map:
            #add nan hits to track_hits, the track_id will be -1
            track_hitc.hits.extend([h for h in hitc.hits if np.isnan(h.Ep)])
            hits_without_nan = [h for h in hitc.hits if np.isfinite(h.Ep)]
            #create new Hitcollection object but keep the name hitc
            hitc = evm.HitCollection(hitc.event, hitc.time)
            hitc.hits = hits_without_nan

        if len(hitc.hits) > 0:
            voxels = plf.voxelize_hits(hitc.hits, vox_size, strict_vox_size,
                                       evm.HitEnergy.Ep)
            (mod_voxels, dropped_voxels) = plf.drop_end_point_voxels(
                voxels, energy_threshold, min_voxels)
            tracks = plf.make_track_graphs(mod_voxels)

            for v in dropped_voxels:
                track_hitc.hits.extend(v.hits)

            vox_size_x = voxels[0].size[0]
            vox_size_y = voxels[0].size[1]
            vox_size_z = voxels[0].size[2]
            del (voxels)
            #sort tracks in energy
            tracks = sorted(tracks, key=plf.get_track_energy, reverse=True)

            track_hits = []
            for c, t in enumerate(tracks, 0):
                tID = c
                energy = plf.get_track_energy(t)
                length = plf.length(t)
                numb_of_hits = len([h for vox in t.nodes() for h in vox.hits])
                numb_of_voxels = len(t.nodes())
                numb_of_tracks = len(tracks)
                pos = [h.pos for v in t.nodes() for h in v.hits]
                x, y, z = map(np.array, zip(*pos))
                r = np.sqrt(x**2 + y**2)

                e = [h.Ep for v in t.nodes() for h in v.hits]
                ave_pos = np.average(pos, weights=e, axis=0)
                ave_r = np.average(r, weights=e, axis=0)
                extr1, extr2 = plf.find_extrema(t)
                extr1_pos = extr1.XYZ
                extr2_pos = extr2.XYZ

                blob_pos1, blob_pos2 = plf.blob_centres(t, blob_radius)

                e_blob1, e_blob2, hits_blob1, hits_blob2 = plf.blob_energies_and_hits(
                    t, blob_radius)
                overlap = float(
                    sum(h.Ep for h in set(hits_blob1).intersection(
                        set(hits_blob2))))
                list_of_vars = [
                    hitc.event, tID, energy, length, numb_of_voxels,
                    numb_of_hits, numb_of_tracks,
                    min(x),
                    min(y),
                    min(z),
                    min(r),
                    max(x),
                    max(y),
                    max(z),
                    max(r), *ave_pos, ave_r, *extr1_pos, *extr2_pos,
                    *blob_pos1, *blob_pos2, e_blob1, e_blob2, overlap,
                    vox_size_x, vox_size_y, vox_size_z
                ]

                df.loc[c] = list_of_vars

                for vox in t.nodes():
                    for hit in vox.hits:
                        hit.track_id = tID
                        track_hits.append(hit)

            #change dtype of columns to match type of variables
            df = df.apply(lambda x: x.astype(types_dict_tracks[x.name]))
            track_hitc.hits.extend(track_hits)
        return df, mod_voxels, track_hitc, out_of_map
예제 #4
0
                        e_blob2_corr_bary += h.E

            if (e_blob2_corr_bary > e_blob1_corr_bary):
                e_blob1_corr_bary, e_blob2_corr_bary = e_blob2_corr_bary, e_blob1_corr_bary  


            ## event-related
            event += [nevt]
            numb_of_hits += [len(hitc)]
            v_size_x += [voxels[0].size[0]]
            v_size_y += [voxels[0].size[1]]
            v_size_z += [voxels[0].size[2]]

            ## track-related
            track_ID += [c]
            length += [plf.length(t)]
            numb_of_voxels += [len(t.nodes())]
            numb_of_tracks += [len(trks)]
            extreme1_x += [extr1.X]
            extreme1_y += [extr1.Y]
            extreme1_z += [extr1.Z]
            extreme2_x += [extr2.X]
            extreme2_y += [extr2.Y]
            extreme2_z += [extr2.Z]

            blob1_bary_x += [bary_pos1[0]]
            blob1_bary_y += [bary_pos1[1]]
            blob1_bary_z += [bary_pos1[2]]
            blob2_bary_x += [bary_pos2[0]]
            blob2_bary_y += [bary_pos2[1]]
            blob2_bary_z += [bary_pos2[2]]
예제 #5
0
    def create_extract_track_blob_info(hitc):
        voxels = plf.voxelize_hits(hitc.hits, vox_size, strict_vox_size,
                                   energy_type)
        tracks = plf.make_track_graphs(voxels)

        df = pd.DataFrame(columns=[
            'event', 'trackID', 'energy', 'length', 'numb_of_voxels',
            'numb_of_hits', 'numb_of_tracks', 'x_min', 'y_min', 'z_min',
            'x_max', 'y_max', 'z_max', 'r_max', 'x_ave', 'y_ave', 'z_ave',
            'extreme1_x', 'extreme1_y', 'extreme1_z', 'extreme2_x',
            'extreme2_y', 'extreme2_z', 'blob1_x', 'blob1_y', 'blob1_z',
            'blob2_x', 'blob2_y', 'blob2_z', 'eblob1', 'eblob2',
            'ovlp_blob_energy', 'vox_size_x', 'vox_size_y', 'vox_size_z'
        ])

        if (len(voxels) == 0):
            return df, None

        vox_size_x = voxels[0].size[0]
        vox_size_y = voxels[0].size[1]
        vox_size_z = voxels[0].size[2]

        def get_track_energy(track):
            return sum([vox.Ehits for vox in track.nodes()])

        #sort tracks in energy
        tracks = sorted(tracks, key=get_track_energy, reverse=True)

        track_hits = []

        for c, t in enumerate(tracks, 0):
            tID = c
            energy = get_track_energy(t)
            length = plf.length(t)
            numb_of_hits = len([h for vox in t.nodes() for h in vox.hits])
            numb_of_voxels = len(t.nodes())
            numb_of_tracks = len(tracks)

            min_x = min([h.X for v in t.nodes() for h in v.hits])
            max_x = max([h.X for v in t.nodes() for h in v.hits])
            min_y = min([h.Y for v in t.nodes() for h in v.hits])
            max_y = max([h.Y for v in t.nodes() for h in v.hits])
            min_z = min([h.Z for v in t.nodes() for h in v.hits])
            max_z = max([h.Z for v in t.nodes() for h in v.hits])
            max_r = max([
                np.sqrt(h.X * h.X + h.Y * h.Y) for v in t.nodes()
                for h in v.hits
            ])

            pos = [h.pos for v in t.nodes() for h in v.hits]
            e = [
                getattr(h, energy_type.value) for v in t.nodes()
                for h in v.hits
            ]
            ave_pos = np.average(pos, weights=e, axis=0)

            extr1, extr2 = plf.find_extrema(t)
            extr1_pos = extr1.XYZ
            extr2_pos = extr2.XYZ

            blob_pos1, blob_pos2 = plf.blob_centres(t, blob_radius)

            e_blob1, e_blob2, hits_blob1, hits_blob2 = plf.blob_energies_and_hits(
                t, blob_radius)
            overlap = False
            overlap = sum(
                [h.E for h in set(hits_blob1).intersection(hits_blob2)])
            list_of_vars = [
                hitc.event, tID, energy, length, numb_of_voxels, numb_of_hits,
                numb_of_tracks, min_x, min_y, min_z, max_x, max_y, max_z,
                max_r, ave_pos[0], ave_pos[1], ave_pos[2], extr1_pos[0],
                extr1_pos[1], extr1_pos[2], extr2_pos[0], extr2_pos[1],
                extr2_pos[2], blob_pos1[0], blob_pos1[1], blob_pos1[2],
                blob_pos2[0], blob_pos2[1], blob_pos2[2], e_blob1, e_blob2,
                overlap, vox_size_x, vox_size_y, vox_size_z
            ]

            df.loc[c] = list_of_vars
            try:
                types_dict
            except NameError:
                types_dict = dict(
                    zip(df.columns, [type(x) for x in list_of_vars]))

            for vox in t.nodes():
                for hit in vox.hits:
                    hit.track_id = tID
                    track_hits.append(hit)

        track_hitc = evm.HitCollection(hitc.event, hitc.time)
        track_hitc.hits = track_hits
        #change dtype of columns to match type of variables
        df = df.apply(lambda x: x.astype(types_dict[x.name]))

        return df, track_hitc