コード例 #1
0
ファイル: part1.py プロジェクト: jim-bo/silp2
def run_orientation(paths, args):
    """ runs orientation
    Parameters
    ----------
    paths.bundle_file       : file
    paths.decomp_file       : file
    """

    # load the bundle graph.
    BG = nx.read_gpickle(paths.bundle_file)
    RG = nx.read_gpickle(paths.decomp_file)

    # sanity check bundle counts.
    for p, q in BG.edges():
        x = sum(BG[p][q]['bcnts'])
        if x == 0.0:
            logging.error("zero weight bundle still present")
            sys.exit(1)

    # sanity check self edges.
    for p, q in BG.edges():
        if p == q:
            #logging.error("self edge")
            #sys.exit(1)
            BG.remove_edge(p, q)

    # annotate graph before solving.
    for p, q in BG.edges():
        BG[p][q]['state'] = -1
    for p in BG.nodes():
        BG.node[p]['orien'] = -1

    # loop over each weakly connected component.
    #ILP = orient.OrientIlp("log.txt", "err.txt", "prg.txt", "sol.txt")
    ILP = orient.OrientIlp("/dev/null", "/dev/null", "/dev/null", "/dev/null", weight_mode=args.weight_mode)
    for subcc in nx.weakly_connected_component_subgraphs(RG):

        # check its consistency.
        _validate_comp_post(subcc)
        _validate_comp_pre(subcc)

        # solve the ILP using decomposition.
        sol = _decomp_solve(BG, subcc, ILP)

        # apply the solution.
        for n in sol['orien']:
            BG.node[n]['orien'] = sol['orien'][n]
        for p,q in sol['state']:
            BG[p][q]['state'] = sol['state'][(p,q)]

    # check whole status.
    for n in BG.nodes():
        if BG.node[n]['orien'] == -1:
            logging.error("orientatio not set")

    for p,q in BG.edges():
        if BG[p][q]['state'] == -1:
            logging.error("state not set")

    # compute the directed graph.
    DG = graphs.to_directed(BG)

    # write to disk.
    nx.write_gpickle(DG, paths.orient_file)
コード例 #2
0
ファイル: nochoice.py プロジェクト: jim-bo/silp2
def debug_orientation(paths, args):
    ''' runs the orientation '''

    # simplify.
    bundle_file = paths.bundle_file

    # load the bundle graph.
    BG = nx.read_gpickle(bundle_file)

    # sanity check self edges.
    for p, q in BG.edges():
        if p == q:
            BG.remove_edge(p, q)

    # annotate graph before solving.
    for n in BG.nodes():
        BG.node[n]['orien'] = -1
    for p, q in BG.edges():
        BG[p][q]['state'] = -1

    # remove branching nodes.
    for n in BG.nodes():
        if len(BG.neighbors(n)) > 2:
            for q in BG.neighbors(n):
                BG.remove_edge(n,q)

    # make edge assignment by connected componnent.
    in_it = False
    for comp in nx.connected_components(BG):

        # handle singelton.
        if len(comp) == 1:
            BG.node[comp[0]]['orien'] = 0
            continue

        # make subgraph.
        subg = BG.subgraph(comp)

        # find hanging node.
        for n in subg.nodes():
            if subg.neighbors(n) == 1:
                break

        if len(subg.edges()) == 2:
            for p, q in subg.edges():
                print p, q, BG[p][q]['bcnts'], p<q
            in_it = True

        # assign orientation of first.
        BG.node[n]['orien'] = 0

        # generate ordered edges.
        for p, q in nx.dfs_edges(subg, n):

            # query maximum state.
            state = np.argmax(BG[p][q]['bcnts'])
            BG[p][q]['state'] = state

            # assign orientation.
            if state == 0:
                if BG.node[p]['orien'] == 0:
                    BG.node[q]['orien'] = 0
                else:
                    BG.node[q]['orien'] = 1

            elif state == 1:
                if BG.node[p]['orien'] == 0:
                    BG.node[q]['orien'] = 1
                else:
                    BG.node[q]['orien'] = 0

            elif state == 2:
                if BG.node[p]['orien'] == 1:
                    BG.node[q]['orien'] = 0
                else:
                    BG.node[q]['orien'] = 1

            elif state == 3:
                if BG.node[p]['orien'] == 1:
                    BG.node[q]['orien'] = 1
                else:
                    BG.node[q]['orien'] = 0

        if in_it == True:
            for n in subg.nodes():
                print n, BG.node[n]['orien']
            for p, q in subg.edges():
                print p, q, BG[p][q]['state']

            print "--"
            DG = graphs.to_directed(BG.subgraph(comp))
            for p, q in DG.edges():
                print p, q
            sys.exit()

    sys.exit()