snap-stanford / snap

Stanford Network Analysis Platform (SNAP) is a general purpose network analysis and graph mining library.
Other
2.17k stars 797 forks source link

an error about community detection using the CNM method #101

Closed xiaoyangzai closed 7 years ago

xiaoyangzai commented 7 years ago

I have an error when I use the method provided by snap to detect the community of a social network dataset. You can see the detail in the appendix, which includes the file named "cnm_test.py" that the source code about my test program and another file named "ca-CondMat.txt" which is a social network dataset and the last file named "log.txt" recording the output error information in my computer. I am looking forward to your reply. Thank you for your help!

the text in the file named "cnm_test.py"

!/usr/bin/python

import sys import snap

def create_graph_from_file(filename): sf = open(filename) lines = sf.readlines() TG = snap.TUNGraph.New() for item in lines: if '#' in item: continue item = item[:-1] item = item.split('\t') if TG.IsNode(int(item[0])) == False: TG.AddNode(int(item[0])) if TG.IsNode(int(item[1])) == False: TG.AddNode(int(item[1])) TG.AddEdge(int(item[1]),int(item[0])) print "load nodes : %d" % TG.GetNodes() sf.close() return TG

def main(): print "create graph start!!" G = create_graph_from_file("./ca-CondMat.txt"); print "create graph finish!!"

CmtyV = snap.TCnComV()
modularity = snap.CommunityCNM(G,CmtyV)
ret_list = []
for Cmty in CmtyV:
    temp = []
    for NI in Cmty:
        temp.append(NI)
    ret_list.append(temp)
print "community size: %d " % len(ret_list)
return 0;

if name == "main": main()

the context of the file "log.txt"

create graph start!! load nodes : 23133 create graph finish!! Traceback (most recent call last): File "./cnm_test.py", line 40, in main() File "./cnm_test.py", line 29, in main modularity = snap.CommunityCNM(G,CmtyV) File "/usr/local/lib/python2.7/dist-packages/snap.py", line 35949, in CommunityCNM return _snap.CommunityCNM(*args) RuntimeError: Execution stopped: (0<=ValN)&&(ValN<Vals) [Reason:'Index:-1 Vals:23133 MxVals:23133 Type:4TVecI11THashKeyDatI4TIntN5TSnap11TSnapDetail11TCNMQMatrix8TCmtyDatEEiE'], file ../../snap/glib-core/ds.h, line 469

SJaved302 commented 6 years ago

Hi,

Have you got any response about this issue? I also got the same error.

Sajid

xiaoyangzai commented 6 years ago

I think the index of the node in snap must start from 1. There are some nodes in the file "Ca-CondMat.txt" whose index is 0. Thus I update the function "load_graph_from_file" as following:

`def load_graph_from_file(filename,comments = '#',delimiter = ' '): ''' load the graph from file to generate the networkx graph Parameters

    filename : string
            contain the nodes and edges of graph

    comments : char 
            the line begined with '#' is commentary

    delimiter : char
            separate and obstract the nodes or the edges in every vaild line  

    Returns
    -------

    G : networkx graph
            create the new graph with graph file
    '''
    print "start load graph from file: %s" % filename

    G = nx.Graph()
    start_time = time.time()
    sf = open(filename)
    line = sf.readline()
    while line != '':
            if comments not in line:
                    break
            line = sf.readline()
    while line != '':
            line = line.split(delimiter)
            # plus 1 reprents that the index of nodes is start from 1 at least(For sanp graph ,the nodes' index must start from 1 at least)
            if G.has_edge(int(line[0]) + 1,int(line[1][:-1]) + 1)==False and G.has_edge(int(line[1][:-1]) + 1,int(line[0]) + 1) == False:
                    G.add_edge(int(line[0]) + 1,int(line[1][:-1]) + 1)
            #G.add_edge(int(line[0]),int(line[1][:-1]))
            line = sf.readline()
    running_time = time.time() - start_time
    #print "G nodes: %d \t edges: %d" % (len(G.nodes()),len(G.edges()))
    #print "running time :%d mins %d secs" % (int(running_time / 60),int(running_time % 60))
    print "load graph finish!! "
    sf.close()
    return G

` You can try like this. Good luck for you!!

SJaved302 commented 6 years ago

Yes, It also resolves my problem and thank you very much for that.

But I am wondering how to process very large snap datasets, such as community one as the txt file is not is not written in the form to process it for snap