Exemplo n.º 1
0
def classify_etc(loadfile, savefile, v):
    """
    1. Moments algorithm
    2. HybridTrack algorithm
    3. Classify
    """

    progressflag = not file_vars()[0]

    pn = 'pix10_5noise15'
    HTname = 'python HT v1.52'
    MTname = 'moments v1.0'

    vprint(v, 1, 'Starting {} at {} with {}% mem usage'.format(
        loadfile, time.ctime(), psutil.virtual_memory().percent))

    pyobj_to_h5 = {}

    try:
        with h5py.File(loadfile, 'a', driver='core') as f, h5py.File(
                savefile, 'a', driver='core') as h5save:
            #
            n = 0
            if progressflag:
                pbar = progressbar.ProgressBar(
                    widgets=[progressbar.Percentage(), ' ',
                             progressbar.Bar(), ' ',
                             progressbar.ETA()], maxval=len(f))
                pbar.start()

            keylist = f.keys()
            keylist.sort()
            for ind in keylist:
                vprint(v, 3, 'Beginning track {} in {}'.format(ind, loadfile))
                if int(ind) % 50 == 0:
                    vprint(v, 2,
                           'Beginning track {} in {}'.format(ind, loadfile))

                # evt is for the G4Track object
                evt = f[ind]
                # shouldn't get a KeyError because we are looping over ind

                # trk is for the Track object
                try:
                    trk = evt[pn]
                except KeyError:
                    vprint(v, 1,
                           '**Missing key {} in {}{}, skipping'.format(
                               pn, loadfile, evt.name))
                    continue

                trkpath = ind + '/' + pn
                errorcode = 0
                n += 1

                if n > 50:
                    # testing
                    # vprint(v, 1, 'Finished 50 files, exiting')
                    # break
                    pass

                # load G4Track
                vprint(v, 3, 'Loading G4Track {} in {}'.format(ind, loadfile))
                try:
                    this_g4track = G4Track.from_dth5(evt)
                except trackio.InterfaceError:
                    vprint(v, 2, 'InterfaceError in G4Track at {}, {}'.format(
                        loadfile, ind))
                    continue

                # load Track
                vprint(v, 3, 'Loading Track {} in {}'.format(ind, loadfile))
                try:
                    this_track = Track.from_dth5(trk, g4track=this_g4track)
                except trackio.InterfaceError:
                    vprint(v, 2, 'InterfaceError in Track at {}, {}'.format(
                        loadfile, ind))
                    continue

                # check for error codes - Track.from_dth5 may return an int
                if isinstance(this_track, int):
                    this_save = h5save.create_group(trkpath)
                    this_save.attrs.create(
                        'errorcode', this_track, shape=np.shape(this_track))
                    continue

                # run moments algorithm
                vprint(v, 3, 'Running moments on track {} in {}'.format(
                    ind, loadfile))
                try:
                    mom = tm.MomentsReconstruction(this_track.image)
                    mom.reconstruct()
                except ht.NoEndsFound:
                    mom.alpha = None
                    errorcode = 4
                    this_save = h5save.create_group('mom_' + trkpath)
                    this_save.attrs.create(
                        'errorcode', errorcode, shape=np.shape(errorcode))
                else:
                    # write into savefile
                    trackio.write_object_to_hdf5(
                        mom, h5save, 'mom_' + trkpath, pyobj_to_h5=pyobj_to_h5)
                # write into track object
                if mom.alpha:
                    this_track.add_algorithm(
                        MTname,
                        alpha_deg=mom.alpha * 180 / np.pi,
                        beta_deg=np.nan, info=None)
                else:
                    this_track.add_algorithm(
                        MTname,
                        alpha_deg=np.nan,
                        beta_deg=np.nan, info=None)

                # run HT algorithm (v1.52)
                vprint(v, 3, 'Running HT on track {} in {}'.format(
                    ind, loadfile))
                try:
                    _, HTinfo = ht.reconstruct(this_track)
                except (ht.InfiniteLoop, ht.NoEndsFound):
                    # write empty into track object
                    this_track.add_algorithm(
                        HTname,
                        alpha_deg=np.nan,
                        beta_deg=np.nan,
                        info=None)
                else:
                    # trim memory usage
                    if hasattr(HTinfo, 'ridge'):
                        if HTinfo.ridge:
                            for ridgept in HTinfo.ridge:
                                ridgept.cuts = None
                                ridgept.best_cut = None
                    # write into track object
                    this_track.add_algorithm(
                        HTname,
                        alpha_deg=HTinfo.alpha_deg,
                        beta_deg=HTinfo.beta_deg,
                        info=HTinfo)

                # write py HDF5 format into savefile (including alg outputs)
                try:
                    trackio.write_object_to_hdf5(
                        this_track, h5save, trkpath,
                        pyobj_to_h5=pyobj_to_h5)
                except trackio.InterfaceError:
                    vprint(v, 1, 'InterfaceError writing to file at ' +
                           '{}, {}'.format(savefile, ind))
                    continue

                # run classifier
                vprint(v, 3, 'Running MC classifier on track {} in {}'.format(
                    ind, loadfile))
                classifier = cl.Classifier(this_track.g4track)
                try:
                    classifier.mc_classify()
                except cl.TrackTooShortError:
                    classifier.error = 'TrackTooShortError'
                else:
                    vprint(v, 3,
                           'Running ends classifier on track {} in {}'.format(
                               ind, loadfile))
                    try:
                        classifier.end_classify(this_track, mom=mom)
                    except tp.G4TrackTooBigError:
                        classifier.error = 'G4TrackTooBigError'
                    except cl.NoEnds:
                        classifier.error = 'NoEnds'
                # write into savefile
                vprint(v, 3, 'Writing classifier into {} for track {}'.format(
                    savefile, ind))
                trackio.write_object_to_hdf5(
                    classifier, h5save, 'cl_' + trkpath,
                    pyobj_to_h5=pyobj_to_h5)

                if progressflag:
                    pbar.update(n)
            if progressflag:
                pbar.finish()
        # f gets closed here
    except NotImplementedError:
        pass
Exemplo n.º 2
0
    def from_pydict(cls, read_dict, pydict_to_pyobj=None, reconstruct=False):
        """
        Initialize a Classifier object from a pydict.
        """

        if pydict_to_pyobj is None:
            pydict_to_pyobj = {}

        if id(read_dict) in pydict_to_pyobj:
            return pydict_to_pyobj[id(read_dict)]

        # first, reconstruct g4track (if needed)
        if isinstance(read_dict['g4track'], G4Track):
            # g4track is already a G4Track object (not sure how)
            g4track = read_dict['g4track']
        elif (isinstance(read_dict['g4track'], dict) and
                id(read_dict['g4track']) in pydict_to_pyobj):
            # g4track is in the pydict table
            g4track = pydict_to_pyobj[id(read_dict['g4track'])]
        elif isinstance(read_dict['g4track'], dict):
            # g4track not in the pydict table. create and add it
            g4track = G4Track.from_pydict(
                read_dict['g4track'], pydict_to_pyobj=pydict_to_pyobj)
            pydict_to_pyobj[id(read_dict['g4track'])] = g4track
        else:
            raise Exception("Unexpected or missing 'g4track' in Classifier")

        new_obj = cls(g4track)

        # add entry to pydict_to_pyobj
        pydict_to_pyobj[id(read_dict)] = new_obj

        # fill in outputs
        if read_dict['error'] is not None:
            new_obj.error = read_dict['error']
        if read_dict['scatterlen_um'] is not None:
            # mc_classify ran
            new_obj.scatterlen_um = read_dict['scatterlen_um']
            new_obj.overlapdist_um = read_dict['overlapdist_um']
            new_obj.escaped = read_dict['escaped']
            new_obj.scatter_type = read_dict['scatter_type']
            new_obj.use2d_angle = read_dict['use2d_angle']
            new_obj.use2d_dist = read_dict['use2d_dist']
            new_obj.angle_threshold_deg = read_dict['angle_threshold_deg']
        if read_dict['early_scatter'] is not None:
            # no TrackTooShortError
            new_obj.early_scatter = read_dict['early_scatter']
            new_obj.total_scatter_angle = read_dict['total_scatter_angle']
            new_obj.overlap = read_dict['overlap']
        if read_dict['wrong_end'] is not None:
            # end_classify ran
            new_obj.wrong_end = read_dict['wrong_end']
            new_obj.n_ends = read_dict['n_ends']
            new_obj.max_end_energy = read_dict['max_end_energy']
            new_obj.min_end_energy = read_dict['min_end_energy']

        if reconstruct:
            new_obj.mc_classify()
            print(
                'Reconstructing Classifier.end_classify() not implemented yet')

        return new_obj
Exemplo n.º 3
0
    def from_pydict(cls, read_dict, pydict_to_pyobj=None, reconstruct=False):
        """
        Initialize a Classifier object from a pydict.
        """

        if pydict_to_pyobj is None:
            pydict_to_pyobj = {}

        if id(read_dict) in pydict_to_pyobj:
            return pydict_to_pyobj[id(read_dict)]

        # first, reconstruct g4track (if needed)
        if isinstance(read_dict['g4track'], G4Track):
            # g4track is already a G4Track object (not sure how)
            g4track = read_dict['g4track']
        elif (isinstance(read_dict['g4track'], dict) and
                id(read_dict['g4track']) in pydict_to_pyobj):
            # g4track is in the pydict table
            g4track = pydict_to_pyobj[id(read_dict['g4track'])]
        elif isinstance(read_dict['g4track'], dict):
            # g4track not in the pydict table. create and add it
            g4track = G4Track.from_pydict(
                read_dict['g4track'], pydict_to_pyobj=pydict_to_pyobj)
            pydict_to_pyobj[id(read_dict['g4track'])] = g4track
        else:
            raise Exception("Unexpected or missing 'g4track' in Classifier")

        new_obj = cls(g4track)

        # add entry to pydict_to_pyobj
        pydict_to_pyobj[id(read_dict)] = new_obj

        # fill in outputs
        if read_dict['error'] is not None:
            new_obj.error = read_dict['error']
        if read_dict['scatterlen_um'] is not None:
            # mc_classify ran
            new_obj.scatterlen_um = read_dict['scatterlen_um']
            new_obj.overlapdist_um = read_dict['overlapdist_um']
            new_obj.escaped = read_dict['escaped']
            new_obj.scatter_type = read_dict['scatter_type']
            new_obj.use2d_angle = read_dict['use2d_angle']
            new_obj.use2d_dist = read_dict['use2d_dist']
            new_obj.angle_threshold_deg = read_dict['angle_threshold_deg']
        if read_dict['early_scatter'] is not None:
            # no TrackTooShortError
            new_obj.early_scatter = read_dict['early_scatter']
            new_obj.total_scatter_angle = read_dict['total_scatter_angle']
            new_obj.overlap = read_dict['overlap']
        if read_dict['wrong_end'] is not None:
            # end_classify ran
            new_obj.wrong_end = read_dict['wrong_end']
            new_obj.n_ends = read_dict['n_ends']
            new_obj.max_end_energy = read_dict['max_end_energy']
            new_obj.min_end_energy = read_dict['min_end_energy']

        if reconstruct:
            new_obj.mc_classify()
            print(
                'Reconstructing Classifier.end_classify() not implemented yet')

        return new_obj