Exemplo n.º 1
0
def get_counts(outfile,infile,is_compressed=True,infile_start=0,infile_end=100,nodes_U=None,nodes_O=None):
    """Counts the subgraphs of the random graphs.

    Keyword arguments:
    outfile -- file where the subgraph counts of the random graphs are to be written
    outfile -- filename prefix of random graphs to be counted
    is_compressed -- whether the random graph gml files are compressed (default True)
    infile_start -- starting number of random graph filename (default 0)
    infile_end -- ending number of random graph filename (default 100)
    nodes_U -- unique list or set of nodes corresponding to bipartite node set U. This is necessary when there is no 'bipartite' node attribute in the random graph data. (default None)
    nodes_O -- unique list or set of nodes corresponding to bipartite node set O. This is necessary when there is no 'bipartite' node attribute in the random graph data. (default None)
    """
    num_subgraphs = 8
    num_rg = infile_end - infile_start
    ensemble_counts = np.zeros([num_rg,num_subgraphs],dtype=np.int64)
    i = 0
    while i < num_rg:
        nth = infile_start + i
        if not is_compressed:
            G = nx.read_gml(infile + str(nth) + ".gml")
        else:
            G = nx.read_gml(infile + str(nth) + ".gml.gz")
        ensemble_counts[i] = bc.count_subgraphs(G, nodes_U=nodes_U, nodes_O=nodes_O)
        print str(nth) +' '+ str(dt.now())
        i += 1
        del G

    h = ''
    for i in np.arange(num_subgraphs-1):
        h += 'subgraph_' + str(i) + ','
    h += 'subgraph_' + str(num_subgraphs-1)
    np.savetxt(outfile, ensemble_counts, header=h, delimiter=',')
Exemplo n.º 2
0
plt.xlim([-10, 1000])
plt.xlabel("k")
plt.ylabel("P(k)")
plt.savefig('plots/pk_music98-03_u.png')

# plotting degree distribution of users
plt.figure()
plt.xscale('log')
plt.yscale('log')
plt.scatter(kdist_O.keys(), kdist_O.values(), c='r')
plt.xlim([-10, 1000])
plt.xlabel("k")
plt.ylabel("P(k)")
plt.savefig('plots/pk_music98-03_o.png')

#---------------------------------------------------------------------
# subgraph counting
subgraphs = bc.count_subgraphs(B)
s = str(subgraphs)
s = s.replace('[', '')
s = s.replace(']', '')
s = s.split()

# Printing to file
mfile = open('subgraphdata_music.csv', mode='a')
mfile.write('1998-2003,' + str(K) + ',' + str(num_U) + ',' + str(num_O) + ',' +
            str(avg_kU) + ',' + str(avg_kO) + ',')
for i in np.arange(len(s) - 1):
    mfile.write(s[i] + ',')
mfile.write(s[-1])
mfile.write('\n')
Exemplo n.º 3
0
import numpy as np
import networkx as nx
import bip_counter as bc

# example 1
users = np.arange(0,10)
objects = np.arange(10,21)

B1 = nx.Graph()
B1.add_nodes_from(users,bipartite=0)
B1.add_nodes_from(objects,bipartite=1)

edges = [(0,10),(0,11),(1,10),(1,12),(2,12),(2,13),(3,14),(4,12),(4,13),(4,15),(5,15),(5,17),(6,16),(7,16),(8,18),(8,19),(8,20),(9,18),(9,19)]

B1.add_edges_from(edges)

print(bc.count_subgraphs(B1))

# example 2
B2 = nx.bipartite.gnmk_random_graph(500,300,1350)

#print(bc.count_subgraphs(B2))

edges2 = [(0,'a'),(0,'b'),(1,'a'),(1,'b'),(1,'c'),(2,'a'),(2,'b'),(2,'c'),(2,'d'),(3,'c')]
B3 = nx.Graph()
B3.add_edges_from(edges2)
print(bc.count_subgraphs(B3,[0,1,2,3],['a','b','c','d']))