From cdb50fd10445f517aa58ab6e7b22c229b7f33174 Mon Sep 17 00:00:00 2001 From: ericbalkanski Date: Fri, 28 Nov 2014 15:44:14 -0500 Subject: Stanford Networks added method to input graph from Stanford Network Database --- jpa_test/cascade_creation.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'jpa_test') diff --git a/jpa_test/cascade_creation.py b/jpa_test/cascade_creation.py index ac094b9..f30b208 100644 --- a/jpa_test/cascade_creation.py +++ b/jpa_test/cascade_creation.py @@ -15,6 +15,24 @@ class InfluenceGraph(nx.Graph): self.add_nodes_from(G.nodes()) self.add_edges_from(G.edges()) + def createStanfordGraph(self, file): + """ + Takes a file from the Stanford collection of networks + Need to remove comments on top of the file + Graph still needs to be weighted on the edges + """ + f = open(file, 'r') + data = f.readlines() + G = nx.DiGraph() + for edge in data: + split1 = edge.split('\t') + split2 = split1[1].split('\n') + + u = int(split1[0]) + v = int(split2[0]) + G.add_edge(u,v) + return G + @property def mat(self): if not hasattr(self, '_mat'): @@ -37,7 +55,9 @@ def icc_cascade(G, p_init): p_init: proba that node in seed set """ susceptible = np.ones(G.number_of_nodes(), dtype=bool) + print susceptible active = np.random.rand(G.number_of_nodes()) < p_init + print active susceptible = susceptible - active cascade = [] while active.any() and susceptible.any(): @@ -53,8 +73,28 @@ def concat_cascades(cascades): """ Concatenate list of cascades into matrix """ + return np.vstack(cascades) + +def createStanfordGraph(file): + """ + Takes a file from the Stanford collection of networks + Need to remove comments on top of the file + Graph still needs to be weighted on the edges + """ + f = open(file, 'r') + data = f.readlines() + G = nx.DiGraph() + for edge in data: + split1 = edge.split('\t') + split2 = split1[1].split('\n') + + u = int(split1[0]) + v = int(split2[0]) + G.add_edge(u,v) + return G + def test(): """ unit test -- cgit v1.2.3-70-g09d2 From fb677abac04ddc697aa19109cef85c18c0f133ff Mon Sep 17 00:00:00 2001 From: ericbalkanski Date: Fri, 28 Nov 2014 15:46:13 -0500 Subject: Stanford Network, bugs fixed previous commit was wrong --- jpa_test/cascade_creation.py | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) (limited to 'jpa_test') diff --git a/jpa_test/cascade_creation.py b/jpa_test/cascade_creation.py index f30b208..c42ae7a 100644 --- a/jpa_test/cascade_creation.py +++ b/jpa_test/cascade_creation.py @@ -31,7 +31,8 @@ class InfluenceGraph(nx.Graph): u = int(split1[0]) v = int(split2[0]) G.add_edge(u,v) - return G + self.add_nodes_from(G.nodes()) + self.add_edges_from(G.edges()) @property def mat(self): @@ -55,9 +56,7 @@ def icc_cascade(G, p_init): p_init: proba that node in seed set """ susceptible = np.ones(G.number_of_nodes(), dtype=bool) - print susceptible active = np.random.rand(G.number_of_nodes()) < p_init - print active susceptible = susceptible - active cascade = [] while active.any() and susceptible.any(): @@ -69,31 +68,6 @@ def icc_cascade(G, p_init): return cascade -def concat_cascades(cascades): - """ - Concatenate list of cascades into matrix - """ - return np.vstack(cascades) - - - -def createStanfordGraph(file): - """ - Takes a file from the Stanford collection of networks - Need to remove comments on top of the file - Graph still needs to be weighted on the edges - """ - f = open(file, 'r') - data = f.readlines() - G = nx.DiGraph() - for edge in data: - split1 = edge.split('\t') - split2 = split1[1].split('\n') - - u = int(split1[0]) - v = int(split2[0]) - G.add_edge(u,v) - return G def test(): """ -- cgit v1.2.3-70-g09d2