In [1]:
import graph_tool as gt
from graph_tool.all import *
import motifwalk as mw


/home/gear/anaconda3/envs/network/lib/python3.6/site-packages/graph_tool/draw/cairo_draw.py:1480: RuntimeWarning: Error importing Gtk module: No module named 'gi'; GTK+ drawing will not work.
  warnings.warn(msg, RuntimeWarning)

In [8]:
from motifwalk import utils as u

In [12]:
from motifwalk.motifs import Motif

In [13]:
from motifwalk.motifs import *

In [14]:
from motifwalk.motifs import analysis

In [ ]:
analysis.count_motif()

In [144]:
g = random_graph(1000, lambda: (5,5))
result = motifs(g, m.num_vertices(), motif_list=[m], return_maps=True)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-144-fb7c023aa771> in <module>()
      1 g = random_graph(1000, lambda: (5,5))
----> 2 result = motifs(g, m.num_vertices(), motif_list=[m], return_maps=True)

TypeError: 'Graph' object is not callable

In [12]:
mo, count, vmap = result

In [15]:
len(vmap[0])


Out[15]:
72

In [25]:
p = vmap[0][15]

In [39]:
graph = p.get_graph()

In [71]:
gt.draw.graph_draw(graph, output_size=(100,100))


Out[71]:
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7f00ac222fd0, at 0x7f00aa8fb898>

In [44]:
p[0]


Out[44]:
151

In [45]:
p[1]


Out[45]:
383

In [46]:
p[2]


Out[46]:
562

In [70]:
g.get_out_edges(151)


Out[70]:
array([[ 151,  881, 2828],
       [ 151,  523, 2829],
       [ 151,  183, 2826],
       [ 151,  154, 2827],
       [ 151,  585, 2825]], dtype=uint64)

In [72]:
g.get_out_edges(383)


Out[72]:
array([[ 383,  603, 4511],
       [ 383,   55, 4510],
       [ 383,  562, 4513],
       [ 383,  955, 4512],
       [ 383,   22, 4514]], dtype=uint64)

In [74]:
g.get_out_edges(562)


Out[74]:
array([[562, 480, 365],
       [562, 337, 367],
       [562, 383, 366],
       [562, 690, 368],
       [562, 151, 369]], dtype=uint64)

In [75]:
m3_7.gt_motif.get_edges()


Out[75]:
array([[0, 1, 0],
       [0, 2, 1],
       [1, 0, 2]], dtype=uint64)

In [76]:
p.get_graph().get_edges()


Out[76]:
array([[0, 1, 0],
       [0, 2, 1],
       [1, 0, 2]], dtype=uint64)

In [77]:
p[0]


Out[77]:
151

In [79]:
g.get_out_edges(151)


Out[79]:
array([[ 151,  881, 2828],
       [ 151,  523, 2829],
       [ 151,  183, 2826],
       [ 151,  154, 2827],
       [ 151,  585, 2825]], dtype=uint64)

In [82]:
p.get_array()


Out[82]:
PropertyArray([151, 383, 562], dtype=int32)

In [83]:
m = m3_9.gt_motif
gt.draw.graph_draw(m, output_size=(200,200))


Out[83]:
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7f00ac209828, at 0x7f00aa902358>

In [84]:
result = motifs(g, m.num_vertices(), motif_list=[m], return_maps=True)

In [85]:
mo, count, vmap = result

In [86]:
p = vmap[0][39]

In [87]:
p.get_array()


Out[87]:
PropertyArray([303, 344, 994], dtype=int32)

In [89]:
m.get_edges()


Out[89]:
array([[0, 1, 0],
       [2, 1, 1],
       [2, 0, 2]], dtype=uint64)

In [148]:
re = motifs(g, k=4, return_maps=True)

In [149]:
len(re)


Out[149]:
3

In [115]:
len(re[1])


Out[115]:
44

In [123]:
gt.draw.graph_draw(re[0][43], output_size=(100,200))
gt.draw.graph_draw(re[0][42], output_size=(100,200))
gt.draw.graph_draw(re[0][40], output_size=(100,200))
gt.draw.graph_draw(re[0][41], output_size=(100,200))


Out[123]:
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7f00aa8b5358, at 0x7f00aa8c5c50>

In [111]:
re[0][3].get_edges()[:,0:2]


Out[111]:
array([[2, 3],
       [3, 1],
       [3, 0]], dtype=uint64)

In [112]:
[i for i in map(tuple, re[0][3].get_edges()[:,0:2])]


Out[112]:
[(2, 3), (3, 1), (3, 0)]

In [120]:
for i, motifs in enumerate(re[0]):
    arr = str([i for i in map(tuple, motifs.get_edges()[:,0:2])])
    s = "m4_{} = Motif({}, is_directed=True, name='m4_{}')".format(i, arr, i)
    print(s)


m4_0 = Motif([(1, 3), (1, 2), (1, 0)], is_directed=True, name='m4_0')
m4_1 = Motif([(0, 1), (0, 2), (3, 1)], is_directed=True, name='m4_1')
m4_2 = Motif([(0, 3), (2, 1), (2, 0)], is_directed=True, name='m4_2')
m4_3 = Motif([(2, 3), (3, 1), (3, 0)], is_directed=True, name='m4_3')
m4_4 = Motif([(0, 3), (1, 3), (2, 3)], is_directed=True, name='m4_4')
m4_5 = Motif([(0, 3), (1, 0), (2, 0)], is_directed=True, name='m4_5')
m4_6 = Motif([(1, 0), (2, 3), (3, 0)], is_directed=True, name='m4_6')
m4_7 = Motif([(0, 3), (1, 2), (2, 0)], is_directed=True, name='m4_7')
m4_8 = Motif([(1, 0), (1, 2), (1, 3), (2, 3)], is_directed=True, name='m4_8')
m4_9 = Motif([(2, 3), (3, 0), (3, 2), (3, 1)], is_directed=True, name='m4_9')
m4_10 = Motif([(0, 3), (0, 2), (1, 3), (1, 2)], is_directed=True, name='m4_10')
m4_11 = Motif([(0, 1), (0, 3), (3, 2), (3, 1)], is_directed=True, name='m4_11')
m4_12 = Motif([(1, 2), (1, 3), (3, 0), (3, 1)], is_directed=True, name='m4_12')
m4_13 = Motif([(0, 1), (0, 3), (2, 1), (3, 1)], is_directed=True, name='m4_13')
m4_14 = Motif([(0, 2), (1, 3), (1, 2), (2, 3)], is_directed=True, name='m4_14')
m4_15 = Motif([(0, 1), (1, 3), (2, 3), (2, 0)], is_directed=True, name='m4_15')
m4_16 = Motif([(0, 2), (2, 1), (2, 3), (3, 1)], is_directed=True, name='m4_16')
m4_17 = Motif([(0, 3), (1, 3), (2, 0), (2, 1)], is_directed=True, name='m4_17')
m4_18 = Motif([(0, 3), (1, 2), (1, 3), (2, 1)], is_directed=True, name='m4_18')
m4_19 = Motif([(1, 2), (2, 1), (3, 2), (3, 0)], is_directed=True, name='m4_19')
m4_20 = Motif([(1, 3), (1, 2), (2, 0), (3, 2)], is_directed=True, name='m4_20')
m4_21 = Motif([(0, 1), (1, 2), (1, 3), (3, 1)], is_directed=True, name='m4_21')
m4_22 = Motif([(0, 1), (0, 2), (2, 0), (3, 2)], is_directed=True, name='m4_22')
m4_23 = Motif([(0, 3), (0, 1), (1, 2), (2, 0)], is_directed=True, name='m4_23')
m4_24 = Motif([(1, 3), (2, 0), (3, 2), (3, 1)], is_directed=True, name='m4_24')
m4_25 = Motif([(0, 1), (1, 2), (2, 1), (3, 1)], is_directed=True, name='m4_25')
m4_26 = Motif([(0, 1), (1, 2), (2, 1), (3, 2)], is_directed=True, name='m4_26')
m4_27 = Motif([(0, 1), (1, 2), (2, 0), (3, 0)], is_directed=True, name='m4_27')
m4_28 = Motif([(0, 3), (1, 2), (2, 1), (3, 1)], is_directed=True, name='m4_28')
m4_29 = Motif([(0, 3), (1, 2), (2, 0), (3, 1)], is_directed=True, name='m4_29')
m4_30 = Motif([(2, 1), (2, 0), (3, 2), (3, 0), (3, 1)], is_directed=True, name='m4_30')
m4_31 = Motif([(1, 3), (1, 0), (1, 2), (2, 3), (3, 0)], is_directed=True, name='m4_31')
m4_32 = Motif([(0, 1), (0, 2), (2, 3), (2, 1), (3, 1)], is_directed=True, name='m4_32')
m4_33 = Motif([(0, 3), (2, 1), (2, 3), (3, 0), (3, 1)], is_directed=True, name='m4_33')
m4_34 = Motif([(1, 0), (2, 3), (2, 1), (3, 1), (3, 0)], is_directed=True, name='m4_34')
m4_35 = Motif([(0, 3), (1, 0), (1, 2), (3, 2), (3, 1)], is_directed=True, name='m4_35')
m4_36 = Motif([(0, 1), (2, 3), (2, 1), (3, 2), (3, 0)], is_directed=True, name='m4_36')
m4_37 = Motif([(0, 2), (1, 2), (1, 0), (2, 3), (3, 2)], is_directed=True, name='m4_37')
m4_38 = Motif([(0, 2), (1, 0), (2, 1), (3, 1), (3, 2)], is_directed=True, name='m4_38')
m4_39 = Motif([(0, 3), (1, 2), (2, 0), (2, 1), (3, 1)], is_directed=True, name='m4_39')
m4_40 = Motif([(0, 2), (1, 3), (2, 1), (2, 0), (3, 1)], is_directed=True, name='m4_40')
m4_41 = Motif([(0, 2), (0, 1), (1, 3), (2, 3), (3, 0)], is_directed=True, name='m4_41')
m4_42 = Motif([(0, 3), (1, 0), (1, 2), (2, 0), (3, 1)], is_directed=True, name='m4_42')
m4_43 = Motif([(0, 2), (1, 2), (2, 3), (2, 0), (3, 1)], is_directed=True, name='m4_43')

In [128]:
la = Graph()

In [129]:
na = nx.Graph()

In [130]:
na.size()


Out[130]:
0

In [134]:
from itertools import combinations

In [137]:
[i for i in combinations(p.get_array(), 2)]


Out[137]:
[(303, 344), (303, 994), (344, 994)]

In [158]:
def construct_motif_graph(graph_container, vertex_maps=None, motif=None):
    """Construct and return a undirected gt graph containing
    motif relationship. TODO: Add anchors nodes

    Parameters:
    graph_container - GraphContainer - Store the original network
    vertex_map - list - contains PropertyMap that maps to vertices in motif

    Returns:
    m_graph - gt.Graph - Undirected graph for motif cooccurence
    """
    if motif is not None and motif.anchors is not None:
        print("Warning: TODO refactor anchor code.")
    # graph_tool.Graph
    m_graph = Graph(directed=False)
    if vertex_maps is None:
        _, _, vertex_maps = count_motif(graph_container, motif)
    for prop in vertex_maps:
        edges = [i for i in combinations(prop.get_array(), 2)]
        m_graph.add_edge_listadd_edges_from(edges)
    return m_graph

In [159]:
la = construct_motif_graph(None, vertex_maps=re[2][0])

In [176]:
graph_draw(m4_0.gt_motif, output_size=(100,100))


Out[176]:
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7f00ac217f60, at 0x7f00aa9156d8>

In [177]:
graph_draw(m3_5.gt_motif, output_size=(100,100))


Out[177]:
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7f00ac1d4f98, at 0x7f00aa915f28>

In [184]:
m3_5_r = motifs(g,k=len(m3_5.gt_motif.get_vertices()),motif_list=[m3_5.gt_motif],return_maps=True)

In [189]:
m3_5_r[2][0][0].get_array()


Out[189]:
PropertyArray([216, 756, 938], dtype=int32)

In [191]:
g.get_out_edges(216)


Out[191]:
array([[ 216,  158, 4175],
       [ 216,  932, 4176],
       [ 216,  938, 4179],
       [ 216,   65, 4177],
       [ 216,   42, 4178]], dtype=uint64)

In [192]:
g.get_out_edges(938)


Out[192]:
array([[ 938,  889, 1882],
       [ 938,  138, 1881],
       [ 938,  216, 1883],
       [ 938,  446, 1880],
       [ 938,  876, 1884]], dtype=uint64)

In [202]:
re = m3_5_r[2][0][0]

In [207]:
re.shrink_to_fit()

In [210]:
re[0]


Out[210]:
216

Using isomorphism doesn't help


In [213]:
re = isomorphism(re.get_graph(), m3_5.gt_motif, isomap=True)

In [218]:
re[1][2]


Out[218]:
2

In [219]:
re = m3_5_r[2][0][0]

In [220]:
graph_draw(re.get_graph(), output_size=(100,100))


Out[220]:
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7f00aa8b5f98, at 0x7f00a28ec208>

In [221]:
re.get_graph().get_edges()


Out[221]:
array([[0, 1, 0],
       [1, 0, 1],
       [2, 0, 2],
       [2, 1, 3]], dtype=uint64)

In [222]:
re[0]


Out[222]:
216

In [223]:
re[1]


Out[223]:
756

In [224]:
re[2]


Out[224]:
938

In [228]:
g.get_out_edges(216)


Out[228]:
array([[ 216,  158, 4175],
       [ 216,  932, 4176],
       [ 216,  938, 4179],
       [ 216,   65, 4177],
       [ 216,   42, 4178]], dtype=uint64)

In [234]:
re.get_graph().get_edges()


Out[234]:
array([[0, 1, 0],
       [1, 0, 1],
       [2, 0, 2],
       [2, 1, 3]], dtype=uint64)

In [236]:
re[0], re[1], re[2]


Out[236]:
(216, 756, 938)

In [237]:
for i in _:
    print(g.get_out_edges(i))


[[ 216  158 4175]
 [ 216  932 4176]
 [ 216  938 4179]
 [ 216   65 4177]
 [ 216   42 4178]]
[[ 756  216 3690]
 [ 756  465 3693]
 [ 756  938 3691]
 [ 756   36 3694]
 [ 756   37 3692]]
[[ 938  889 1882]
 [ 938  138 1881]
 [ 938  216 1883]
 [ 938  446 1880]
 [ 938  876 1884]]

Since the Property map doesn't map exactly to the node id in the main graph, I have to use the induced subgraphs.


In [238]:
# select some vertices
vfilt = g.new_vertex_property('bool');
vfilt[216] = True
vfilt[756] = True
vfilt[938] = True

sub = GraphView(g, vfilt)

In [260]:
ka = isomorphism(sub, m3_5.gt_motif, isomap=True)

In [270]:
ka[1][216], ka[1][756], ka[1][938]


Out[270]:
(0, 2, 1)

In [271]:
[i for i in [216, 756, 938] if ka[1][i] in {0,1}]


Out[271]:
[216, 938]

In [ ]: