Last active
December 11, 2015 16:28
-
-
Save CaptSolo/4627494 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import graph_tool as gt | |
import numpy | |
import os | |
import sys | |
def get_all_graphs(g, v_prop, prune=False): | |
# iterate thru all cluster numbers | |
for cl_num in numpy.unique(v_prop.a): | |
# filter all vertices belonging to the cl_num cluster | |
gv1 = gt.GraphView(g, vfilt = lambda v: v_prop[v] == cl_num) | |
if not prune: | |
yield gv1 | |
else: | |
yield gt.Graph(gv1, prune=True) | |
def partition_graph(g, v_prop): | |
gv1 = gt.GraphView(g, efilt = lambda e: v_prop[e.source()] == v_prop[e.target()]) | |
return gv1 | |
def save_all_graphs(g, v_prop, fname = "graph"): | |
d = "output" | |
if not os.path.exists(d): | |
os.makedirs(d) | |
#tmpl = "./output/graph" | |
tmpl = os.path.join(d, fname) | |
graphs = get_all_graphs(g, v_prop, prune=True) | |
save_graphs(graphs, tmpl) | |
def save_graphs(graphs, fname_tmpl): | |
# NOTE: | |
# - need proper, pruned graphs (not views) here | |
# - or the whole graph would be saved, multiple times | |
for num, subgr in enumerate(graphs): | |
fname = fname_tmpl + "-%s-%s.xml" % (num + 1, subgr.num_vertices()) | |
subgr.save(fname) | |
print "Saved:", fname | |
sys.stdout.flush() | |
def main(): | |
print "Nothing to see here" | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment