Exemple #1
0
 def plot(self):
     pos = nx_drawing.pygraphviz_layout(self.graph, prog='dot')
     for k in pos:
         x, y = pos[k]
         pos[k] = (x, y + np.random.randint(-10, 10))
     plt.figure(figsize=(20, 12))
     nx.draw(self.graph, pos, with_labels=True, arrows=True, )
     plt.show()
Exemple #2
0
 def draw(self, subset=None):
     if subset is None:
         dag = self.dag
     else:
         dag = self.dag.subgraph(subset)
     pos = pygraphviz_layout(dag, prog='dot')
     nx.draw(dag, pos, with_labels=True)
     plt.show()
Exemple #3
0
 def save_graph(self, graph, file_name):
     plt.title('draw_networkx')
     pos=pygraphviz_layout(graph, prog='dot')
     newpos = {}
     for k,v in pos.items():
         newpos[k] = (k,v[0],v[1])
     sorted_by_x = sorted(
         list(newpos.values()), key=lambda tup: (tup[2],tup[1]))
     rank = []
     nodesep = 1000
     for k, xx, yy in sorted_by_x:
         if yy not in rank:
             rank.append(yy)
             newpos[k] = (xx, yy)
             newxx = xx + nodesep
         else:
             newpos[k] = (newxx, yy)
             newxx += nodesep
     nx.draw(graph, newpos, node_size=30, with_labels=False, arrows=True)
     plt.savefig(file_name)
     plt.close()
Exemple #4
0
def to_hipathia_dfs(
    graph: BELGraph,
    draw_directory: Optional[str] = None,
) -> Union[Tuple[None, None], Tuple[pd.DataFrame, pd.DataFrame]]:
    """Get the ATT and SIF dataframes.

    :param graph: A BEL graph
    :param draw_directory: The directory in which a drawing should be output

    1. Identify nodes:
       1. Identify all proteins
       2. Identify all protein families
       3. Identify all complexes with just a protein or a protein family in them
    2. Identify interactions between any of those things that are causal
    3. Profit!
    """
    proteins = set()
    families = defaultdict(set)
    complexes = set()
    for node in sorted(graph, key=str):
        if isinstance(node, Protein):
            children = _is_node_family(graph, node)
            if children:
                families[node] = children
            else:
                proteins.add(node)
        elif isinstance(node, ComplexAbundance) and all(
                isinstance(m, Protein) for m in node.members):
            complexes.add(node)

    families = {
        node: sorted(values, key=str)
        for node, values in sorted(families.items(), key=itemgetter(0))
    }

    nodes = sorted(proteins.union(families).union(complexes), key=str)
    new_nodes = set()
    edges = []
    for u, v, _, d in sorted(graph.out_edges(nodes, keys=True, data=True),
                             key=lambda t: (str(t[0]), str(t[1]), t[2])):
        relation = d[RELATION]
        if relation not in CAUSAL_POLAR_RELATIONS:
            continue
        new_nodes.add(u)
        new_nodes.add(v)
        edges.append((u, 'activation' if relation in CAUSAL_INCREASE_RELATIONS
                      else 'inhibition', v))

    att = {}
    dsl_to_k = {}
    i = 0
    for node in sorted(new_nodes, key=str):
        if node in families:
            i += 1
            k = (i, )
            children = families[node]
            child_identifiers = [child.identifier for child in children]
            if not all(child_identifiers):
                logger.warning('not all children were grounded: %s',
                               child_identifiers)
                continue
            labels, genes_lists = [node.name], [child_identifiers]
        elif isinstance(node, Protein):
            if not node.identifier or not node.name:
                logger.warning('node was not grounded: %s', node)
                continue
            i += 1
            k = (i, )
            labels, genes_lists = [node.name], [[node.identifier]]
        elif isinstance(node, ComplexAbundance):
            k, labels, genes_lists = [], [], []
            for member in node.members:
                i += 1
                k.append(i)
                labels.append(member.name)
                if member in families:
                    children = families[member]
                    child_identifiers = [
                        child.identifier for child in children
                    ]
                    if not all(child_identifiers):
                        logger.warning('not all children were grounded: %s',
                                       child_identifiers)
                        continue
                    genes_lists.append(child_identifiers)
                else:
                    if not member.identifier:
                        logger.warning('member was not grounded: %s', member)
                        continue
                    genes_lists.append([member.identifier])
            k = tuple(k)
        else:
            logger.debug('skipping node {}'.format(node))
            continue

        k = 'N-{}-{}'.format(graph.name, ' '.join(map(str, k)))
        att[k] = labels, genes_lists
        dsl_to_k[node] = k

    edges = [(dsl_to_k[source], relation, dsl_to_k[target])
             for source, relation, target in edges
             if source in dsl_to_k and target in dsl_to_k]
    sif_df = pd.DataFrame(edges)  # DONE

    composite_graph = nx.Graph([(k_source, k_target)
                                for k_source, _, k_target in edges])

    try:
        from networkx.drawing.nx_agraph import pygraphviz_layout
        pos = pygraphviz_layout(composite_graph,
                                prog="neato",
                                args='-Gstart=5')
    except ImportError:
        logger.warning(
            'could not import pygraphviz. Falling back to force directed')
        pos = nx.fruchterman_reingold_layout(composite_graph, seed=5)

    if not pos:
        return None, None

    nx_labels = {}  # from k to label
    min_x = min(x for x, y in pos.values())
    min_y = min(y for x, y in pos.values())

    att_rows = []
    for k, (labels, genes_lists) in sorted(att.items()):
        if k not in pos:
            logger.warning('node not in graph: %s', k)
            continue

        nx_labels[k] = label = ' '.join(labels)
        types = ','.join(['gene'] * len(labels))
        gene_list = ',/,'.join(','.join(gene_list)
                               for gene_list in genes_lists)
        x, y = pos[k]
        att_rows.append((
            k,  # 1. ID
            label,  # 2. label
            int(100 * (x - min_x)),  # 3. X
            int(100 * (y - min_y)),  # 4. Y
            'white',  # 5. color
            'rectangle',  # 6. shape
            types,  # 7.
            0.5,  # 8. label.cex
            'black',  # 9. label.color
            46,  # 10. width
            17,  # 11. height
            gene_list,  # 12. gene list
        ))

    att_df = pd.DataFrame(att_rows,
                          columns=[
                              'ID', 'label', 'X', 'Y', 'color', 'shape',
                              'type', 'label.cex', 'label.color', 'width',
                              'height', 'genesList'
                          ])

    if draw_directory is not None:
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            logger.warning(
                'could not draw graph because matplotlib is not installed')
        else:
            plt.figure(figsize=(20, 20))
            nx.draw_networkx(composite_graph, pos, labels=nx_labels)
            plt.axis('off')
            plt.savefig(
                os.path.join(draw_directory, '{}.png'.format(graph.name)))

    return att_df, sif_df
Exemple #5
0
import networkx as nx
from networkx.drawing.nx_agraph import write_dot, graphviz_layout, pygraphviz_layout
import matplotlib.pyplot as plt
G = nx.DiGraph()

G.add_node("ROOT")

for i in range(5):
    G.add_node("Child_%i" % i)
    G.add_node("Grandchild_%i" % i)
    G.add_node("Greatgrandchild_%i" % i)

    G.add_edge("ROOT", "Child_%i" % i)
    G.add_edge("Child_%i" % i, "Grandchild_%i" % i)
    G.add_edge("Grandchild_%i" % i, "Greatgrandchild_%i" % i)

# write dot file to use with graphviz
# run "dot -Tpng test.dot >test.png"
write_dot(G, 'test.dot')

# same layout using matplotlib with no labels
plt.title('draw_networkx')
pos = pygraphviz_layout(G, prog='dot')
print(pos)
nx.draw(G, pos, with_labels=False, arrows=True)
plt.savefig('nx_test.png')
Exemple #6
0
    def plot_examples(self, atom_number, cutoff, number, feed_dict={}):
        import networkx as nx
        import matplotlib.pyplot as plt
        from networkx.drawing.nx_agraph import pygraphviz_layout

        def fit(g, n):
            if 'mask' not in g.nodes[n]:
                return 0
            if g.nodes[n]['mask'] > 0.1:
                return abs(g.nodes[n]['peak'] - g.nodes[n]['label'])
            return 0
        def color_fit(g, n, cmap, vmin, vmax):
            f = fit(g, n)
            if f == 0:
                return (1,1,1,0)
            else:
                return cmap((f - vmin) / vmax)

        mols = []
        vmax = 0
        candidates = self.to_networkx(number=number * 25)
        for g in candidates:
            for n in g.nodes():
                error = fit(g, n)
                if error > cutoff:
                    if error > vmax:
                        vmax = error
                    mols.append(g)
                    break

        # trim to size
        mols = mols[:number]
        grid = int(np.sqrt(len(mols)))
        fig, axs = plt.subplots(grid, grid, figsize=(32, 32))
        viridis = plt.get_cmap('viridis', 100)
        for i in range(grid):
            for j in range(grid):
                ax = axs[i,j]
                g = mols[i * grid + j]
                r = []
                if atom_number - 1 in g.nodes:
                    g.remove_node(atom_number - 1)
                c = [color_fit(g,n,viridis, 0, vmax) for n in g.nodes]

                # now remove all non single-bond edges
                remove = []
                for e in g.edges(data=True):
                    if not e[2]['bonded']:
                        if 'mask' not in g.nodes[e[1]] or 'mask' not in g.nodes[e[0]]:
                            #TODO how is this happening?
                            remove.append(e)
                        elif g.nodes[e[0]]['mask'] < .1 and g.nodes[e[1]]['mask'] < 0.1:
                            remove.append(e)
                g.remove_edges_from(remove)
                pos = pygraphviz_layout(g, prog='sfdp', args='-Gmaxiter=150 -Gnodesep=100 -Nshape=circle')
                edge_colors = [(0,0,0) if d['bonded'] else (0.8, 0.8, 0.8) for e1,e2,d in g.edges(data=True)]
                #pos = nx.layout.spring_layout(g, iterations=150, k=0.2)
                nx.draw(g,
                pos,
                node_size=60,
                fontsize=5,
                node_color=c,
                vmin=0,
                vmax=vmax,
                labels={n: g.nodes[n]['name'] if'mask' in g.nodes[n] and g.nodes[n]['mask'] < 0.1 else '' for n in g.nodes},
                with_labels=True,
                edge_color=edge_colors,
                ax=ax
                )
                ax.axis('on')
                ax.set_yticklabels([])
                ax.set_xticklabels([])
                ax.get_xaxis().set_ticks([])
                ax.get_yaxis().set_ticks([])
                ax.set_title(g.graph['class_label'] + ':' + ','.join([str(i) for i in g.graph['record_index']]))
        cm = plt.cm.ScalarMappable(cmap=viridis, norm=plt.Normalize(vmin = 0, vmax=vmax))
        cb = plt.colorbar(cm, shrink=0.8)
        cb.ax.get_yaxis().labelpad = 15
        cb.ax.tick_params(labelsize=16)
        cb.ax.set_ylabel('Prediction Error', rotation=270, fontsize=16)
        plt.savefig(self.model_path + '/plots/' + 'examples-{:.2f}.jpg'.format(cutoff), dpi=300, transparent=True)
        plt.close()
Exemple #7
0
def slimpass(bn: TWBayesianNetwork,
             budget: int = BUDGET,
             timeout: int = TIMEOUT,
             history: Counter = None,
             width_bound: int = None,
             debug=False):
    td = bn.td
    if USING_COMPLEXITY_WIDTH:
        final_width_bound = weight_from_domain_size(width_bound)
    else:
        final_width_bound = width_bound
    selected, seen = find_subtree(td, budget, history, debug=False)
    history.update(selected)
    prep_tuple = prepare_subtree(bn, selected, seen, debug)
    if prep_tuple is None: return
    forced_arcs, forced_cliques, data, pset_acyc = prep_tuple
    # if debug:
    #     print("filtered data:-")
    #     pprint(data)
    old_score = bn.compute_score(seen)
    max_score = compute_max_score(data, bn)
    if RELAXED_PARENTS:
        # too strict
        # assert max_score + EPSILON >= old_score, "max score less than old score"
        assert round(max_score + EPSILON, 4) >= round(
            old_score, 4), "max score less than old score"
        if max_score < old_score:
            print("#### max score smaller than old score modulo epsilon")
    cur_offset = sum(bn.offsets[node] for node in seen)
    if debug:
        print(
            f"potential max: {(max_score - cur_offset)/bn.best_norm_score:.5f}",
            end="")
    if (max_score - cur_offset) / bn.best_norm_score <= LAZY_THRESHOLD:
        if debug: print(" skipping because lazy threshold not met")
        SOLUTION.skipped += 1
        return
    pos = dict()  # placeholder layout
    if not CLUSTER and debug:
        pos = pygraphviz_layout(bn.dag, prog='dot')
        nx.draw(bn.dag, pos, with_labels=True)
        plt.suptitle("entire dag")
        plt.show()
        nx.draw(bn.dag.subgraph(seen), pos, with_labels=True)
        plt.suptitle("subdag before improvement")
        plt.show()
    if debug:
        print("old parents:-")
        pprint({node: par for node, par in bn.parents.items() if node in seen})
    domain_sizes = DOMAIN_SIZES if USING_COMPLEXITY_WIDTH else None
    try:
        replbn = solve_bn(data, final_width_bound, bn.input_file, forced_arcs,
                          forced_cliques, pset_acyc, timeout, domain_sizes,
                          debug)
    except NoSolutionException as err:
        SOLUTION.nosolution += 1
        print(f"no solution found by maxsat, skipping (reason: {err})")
        return
    new_score = replbn.compute_score()
    if not CLUSTER and debug:
        nx.draw(replbn.dag, pos, with_labels=True)
        plt.suptitle("replacement subdag")
        plt.show()
    if debug:
        print("new parents:-")
        pprint(replbn.parents)
    if debug: print(f"score change: {old_score:.3f} -> {new_score:.3f}")
    if USE_COMPLEXITY_WIDTH:
        old_cw = compute_complexity_width(td, DOMAIN_SIZES, include=selected)
        new_cw = compute_complexity_width(replbn.td, DOMAIN_SIZES)
        old_acw = compute_complexity_width(td,
                                           DOMAIN_SIZES,
                                           include=selected,
                                           approx=True)
        new_acw = compute_complexity_width(replbn.td,
                                           DOMAIN_SIZES,
                                           approx=True)
        # print(f"old: {old_cw}|{old_acw:.3f}\tnew: {new_cw}|{new_acw:.3f}")
        print(f"msss of local part: {old_cw} -> {new_cw}")
    # replacement criterion
    if USING_COMPLEXITY_WIDTH and old_cw > width_bound:
        if new_cw > width_bound:
            return False
    elif USING_COMPLEXITY_WIDTH and new_score == old_score and new_cw > old_cw:
        return False
    elif new_score < old_score:  # in case not using cw, then this is the only check
        return False
    print(f"score change: {old_score:.3f} -> {new_score:.3f}, replacing ...")
    td.replace(selected, forced_cliques, replbn.td)
    # update bn with new bn
    bn.replace(replbn)
    if __debug__: bn.verify(verify_treewidth=not USING_COMPLEXITY_WIDTH)
    return True
    networks = ['CRM'] # IDs of networks we are looking at
    layoutprog = "neato"   # sfdp gives more stretched out layout

    SCALE = 10

# CODE
    for network in networks:
        infile = "{}/{}_network.gexf".format(infolder, network)

        # Read in
        G = nx.read_gexf(infile) # Name lost?
        H = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0] # get giant component

        # Calculate node positions and centralities
        node_positions = pygraphviz_layout(H, prog=layoutprog) # See https://networkx.github.io/documentation/latest/reference/drawing.html#layout
        node_betweenness = nx.betweenness_centrality(H) # See https://networkx.github.io/documentation/latest/reference/algorithms.centrality.html
        i = 1
        for node, data in H.nodes_iter(data=True): # add IDs and scale
            data["id"] = i
            i += 1
            data["x"], data["y"] = tuple(x*SCALE for x in node_positions[node])
            data["betweenness"] = node_betweenness[node]

        # GRAPH VISUALIZATION
        # Generate JS
        nodes = {}
        # nodes for 'function'; groupnumber for groupvalue
        nodes['f'] = ['{id:%d,label:"%s",group:"%s",title:"%s<br></br>%s",x:%s,y:%s,value:0.5}'
            % (data["id"], data['label'], data["group"], data['label'], data["meta"], data["x"], data["y"])
            for node, data in H.nodes(data=True)]
Exemple #9
0
 def draw_graph(self, graph):
     plt.title('draw_networkx')
     pos=pygraphviz_layout(graph, prog='dot', args='-Gnodesep=1')
     nx.draw(graph, pos, node_size=30, with_labels=False, arrows=True)
     plt.savefig('nx_test.png')
     plt.close()