Esempio n. 1
0
def get_skel_extremes(vox_size, energy_type, strict_vox_size, 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', 'skel_extr1_x', 'skel_extr1_y',
        'skel_extr1_z', 'skel_extr2_x', 'skel_extr2_y', 'skel_extr2_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)

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

        list_of_vars = [
            hitc.event, tID, energy, extr1_pos[0], extr1_pos[1], extr1_pos[2],
            extr2_pos[0], extr2_pos[1], extr2_pos[2]
        ]

        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
Esempio n. 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
Esempio n. 3
0
        etmax = sum([vox.E for vox in trks[itmax].nodes()])

        # Construct the blobs.
        eblobs = plf.blob_energies(trks[itmax],blob_radius)
        iter_eblobs = iter(eblobs)
        Eblob1, Eblob2 = next(iter_eblobs),next(iter_eblobs)

        # Ensure blob1 always has higher energy.
        if(Eblob2 > Eblob1):
            eswap = Eblob1
            Eblob1 = Eblob2
            Eblob2 = eswap

        # Get the extremes.
        distances = plf.shortest_paths(trks[itmax])
        a,b = plf.find_extrema(trks[itmax]) #(distances)
        ltrk = distances[a][b]
        print("Found {0} tracks of {1}; max containing {2} voxels; total of {3} voxels, distance = {4}".format(len(trks),len(hitc_cevt),len(trks[itmax]),len(voxels),distances[a][b]))

    # Center the voxels.
    xmin = np.min([vox.X for vox in voxels]); xmax = np.max([vox.X for vox in voxels])
    ymin = np.min([vox.Y for vox in voxels]); ymax = np.max([vox.Y for vox in voxels])
    zmin = np.min([vox.Z for vox in voxels]); zmax = np.max([vox.Z for vox in voxels])
    x0 = ((xmin + xmax) / 2. - (rng / 2.))
    y0 = ((ymin + ymax) / 2. - (rng / 2.))
    z0 = ((zmin + zmax) / 2. - (rng / 2.))

    # Create the voxel array.
    varr = np.zeros([vdim[0],vdim[1],vdim[2]])

    # Iterate through the voxels, applying offsets.
Esempio n. 4
0
        tot_e = sum([hh.E for hh in hitc])
        voxels = plf.voxelize_hits(hitc, vox_size)
        trks = plf.make_track_graphs(voxels)

        while True:
            modified_voxels = 0

            trks = plf.make_track_graphs(voxels)
            vxl_size = voxels[0].size

            for t in trks:
                if len(t.nodes()) < 3:
                    continue

                extr1, extr2 = plf.find_extrema(t)

                if extr1.E < voxel_cut:
                    modified_voxels += 1
                    ### remove voxel from list of voxels
                    voxels.remove(extr1)
                    ### remove hits from the list of hits
                    pos1 = [h.pos for h in extr1.hits]
                    qs1 = [h.E for h in extr1.hits]
                    bary_extr1_pos = np.average(pos1, weights=qs1, axis=0)
                    for h in extr1.hits:
                        hitc.remove(h)

                    ### find hit with minimum distance, only among neighbours
                    min_dist = 1e+06
                    min_hit = hitc[0]
Esempio n. 5
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
    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