Last active
August 21, 2018 17:38
-
-
Save hhromic/2e93de406fb6b1e3de5f9ab6f5f84622 to your computer and use it in GitHub Desktop.
Example of using SNAP with the multiprocessing module and a progress bar (tqdm)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import multiprocessing | |
import tqdm | |
import snap | |
# the graph needs to be global so workers can access it after forking | |
GRAPH = None | |
# task: compute eccentricy of a node | |
def compute_ecc(node_id): | |
global GRAPH | |
return snap.GetNodeEcc(GRAPH, node_id) | |
# main function | |
def main(): | |
global GRAPH | |
GRAPH = snap.TUNGraph.Load(snap.TFIn(sys.argv[1])) | |
print "loaded %d nodes and %d edges" % (GRAPH.GetNodes(), GRAPH.GetEdges()) | |
# parallel compute eccentricity of all nodes | |
pool = None | |
try: | |
pool = multiprocessing.Pool() | |
num_nodes = GRAPH.GetNodes() | |
node_ids = (n.GetId() for n in GRAPH.Nodes()) | |
iterator = pool.imap_unordered(compute_ecc, node_ids) | |
accumulator = 0.0 | |
for ecc in tqdm.tqdm(iterator, total=num_nodes, desc="ecc", unit="node"): | |
accumulator += ecc | |
print "mean node eccentricity = %f" % (accumulator / num_nodes) | |
finally: | |
if pool: | |
pool.close() | |
pool.join() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment