示例#1
0
def compute_cycles(GlobalOptions, epsList):

    Filt = Geometry.Filtration()
    Parallel = GlobalOptions['parallel']
    monitorFile = GlobalOptions['monitor']

    if Parallel:

        # If necessary setup the monitoring system
        if monitorFile is not None:
            # setup the pipes
            pipes = [ Pipe(duplex=True) for _ in epsList]
            parents = [ e[0] for e in pipes]
            children = [ e[1] for e in pipes]
            del pipes
            # setup the monitor object

            Tracker = Monitor( parents , monitorFile , epsList  )

            setup = zip([GlobalOptions for i in epsList ],epsList, children) # list of tuples ( data , eps, pipe)

        else: # if monitor is none
            setup = zip([GlobalOptions for i in epsList ],epsList, [None for _ in epsList])
            Tracker = None

        pool = Pool(multiprocessing.cpu_count() - 1 )
        job = pool.map_async(parallel_rec_pipeline, setup)

        while job._number_left != 0:
            if Tracker is not None: # if monitoring is active
                if any( [ rec.poll() for rec in parents ] ):
                    Tracker.track()
                    #time.sleep(3)

        # then fetch the result
        res = job.get()

        # JUST IN CASE pull the last updates from the pipes, then close them
        if Tracker is not None: # if monitoring is active
            if any( [ rec.poll() for rec in parents ] ):
                Tracker.track()

            for p,c in zip(parents,children):
                p.close()
                c.close()

        # Now computing the filtration
        for b in res: # add to the filtration
            Filt.add( b[0] , b[1] )

        Filt.sort_by_eps() # sort the Filtration by epsilon

        maximal = GlobalOptions['EdgeList'] # maximal is (edgelist,weights)
        Filt.set_edgeList(maximal[0]) # set edgelist (without weights)

        return Filt

    else: # If not parallel
        W = GlobalOptions['Matrix']
        maximal = GlobalOptions['EdgeList'] # maximal is (edgelist,weights)
        allDraws = GlobalOptions['Draws']

        nVert = len(W)

        res = []

        if not epsList or len(epsList)==0 :
            raise ValueError("Empty List of Thresholds!")

        for e in epsList:

            Wstep = filterMatrix(W,e)

            G = SimplexGraph(nVert,Wstep, maximal)
            G.getCycleBase()
            d2 = getD2( Wstep , maximal[0] ) # qua c'era un bug!! Voglio solo
            # maximal[0], solo gli edges e non i pesi!
            (An, B1, Z1, H1) = G.getAnnotation(d2)

            G.initSup(allDraws) # init SPT
            G.ExtendBasis(1,H1) # Compute SHB!

            Filt.add( e , G.SHB )

            Filt.set_edgeList(maximal[0])

        return Filt