Skip to content

Commit

Permalink
Add utility functions module
Browse files Browse the repository at this point in the history
  • Loading branch information
martinateruzzi committed Jan 18, 2021
1 parent 3fce21a commit df2cfc3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 59 deletions.
64 changes: 5 additions & 59 deletions grape/general_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import copy
import networkx as nx

from .utils import chunk_it, merge_lists

warnings.simplefilter(action='ignore', category=FutureWarning)
logging.basicConfig(
filename="general_code_output.log", level=logging.DEBUG, filemode='w')
Expand Down Expand Up @@ -375,7 +377,7 @@ def floyd_warshall_predecessor_and_distance_parallel(self):

n = len(self.nodes())
chunk = [(0, int(n / self.num))]
node_chunks = self.chunk_it(list(self.nodes()), self.num)
node_chunks = chunk_it(list(self.nodes()), self.num)

for i in range(1, self.num):
chunk.append((chunk[i - 1][1],
Expand Down Expand Up @@ -514,28 +516,6 @@ def single_source_shortest_path_parallel(self, out_q, nodi):
ssspp = (n, nx.single_source_dijkstra(self, n, weight = 'weight'))
out_q.put(ssspp)

@staticmethod
def chunk_it(nodi, n):
"""
Divide graph nodes in chunks according to number of processes.
:param list nodi: list of nodes in the graph
:param int n: number of available processes
:return: list of graph nodes to be assigned to every process
:rtype: list
"""

avg = len(nodi) / n
out = []
last = 0.0

while last < len(nodi):
out.append(nodi[int(last):int(last + avg)])
last += avg
return out

def parallel_wrapper_proc(self):
"""
Expand All @@ -552,7 +532,7 @@ def parallel_wrapper_proc(self):

out_q = Queue()

node_chunks = self.chunk_it(list(self.nodes()), self.num)
node_chunks = chunk_it(list(self.nodes()), self.num)

processes = [
mp.Process( target=self.single_source_shortest_path_parallel,
Expand Down Expand Up @@ -1130,28 +1110,6 @@ def rm_nodes(self, node, visited=None):

return visited

@staticmethod
def merge_lists(l1, l2, key):
"""
Merge two lists of dictionaries according to their keys.
:param list l1: first list of dictionaries to be merged
:param list l2: second list of dictionaries to be merged
:param list key: key on which to merge the two lists of dictionaries
:return: the merged list of dictionaries
:rtype: list
"""

merged = {}
for item in l1 + l2:
if item[key] in merged:
merged[item[key]].update(item)
else:
merged[item[key]] = item
return [val for (_, val) in merged.items()]

def update_areas(self, deleted_nodes, damaged_areas):
"""
Expand Down Expand Up @@ -1350,7 +1308,7 @@ def service_paths_to_file(self, filename):
service paths situation
"""

rb_paths_p = self.merge_lists(self.lst0, self.lst, "ids")
rb_paths_p = merge_lists(self.lst0, self.lst, "ids")

with open(filename, "w") as csvFile:
fields = [
Expand Down Expand Up @@ -1500,15 +1458,3 @@ def compute_residual_service(self, graph, servicename):

graph.edges[head, tail]['splitting'] += \
1./self.nodes[head]['users_per_node']



if __name__ == '__main__':

g = GeneralGraph()
g.load(sys.argv[1])

g.check_input_with_gephi()
g.simulate_element_perturbation(["1"])
#g.simulate_area_perturbation(['area1'])
##g.simulate_area_perturbation(['area1','area2','area3'])
44 changes: 44 additions & 0 deletions grape/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Utility functions module"""


def chunk_it(nodi, n):
"""
Divide nodes in chunks according to number of processes.
:param list nodi: list of nodes
:param int n: number of available processes
:return: list of graph nodes to be assigned to every process
:rtype: list
"""

avg = len(nodi) / n
out = []
last = 0.0

while last < len(nodi):
out.append(nodi[int(last):int(last + avg)])
last += avg
return out

def merge_lists(l1, l2, key):
"""
Merge two lists of dictionaries according to their keys.
:param list l1: first list of dictionaries to be merged
:param list l2: second list of dictionaries to be merged
:param list key: key on which to merge the two lists of dictionaries
:return: the merged list of dictionaries
:rtype: list
"""

merged = {}
for item in l1 + l2:
if item[key] in merged:
merged[item[key]].update(item)
else:
merged[item[key]] = item
return [val for (_, val) in merged.items()]

0 comments on commit df2cfc3

Please sign in to comment.