Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Last active August 29, 2015 14:07
Show Gist options
  • Save sangheestyle/194df031ab2df3580871 to your computer and use it in GitHub Desktop.
Save sangheestyle/194df031ab2df3580871 to your computer and use it in GitHub Desktop.
Make biparted graph and projection
authors=(`git log --format='%ae' | sort -u`)
for author in ${author[*]}
do
files=(`git log --no-merges --stat --author="$author" --name-only --pretty=format:"" | sort -u`)
for file in ${files[*]}
do
echo $author $file
done
done
$ cd the_git_folder_you_want
$ git log --format='%ae' | sort -u | tee base_author.txt
$ bash generate_edge_list.sh | tee base_edge_list.txt
@sangheestyle
Copy link
Author

from graph_tool.all import *

g = load_graph("base.gml")
pos = sfdp_layout(g)
from graph_tool.all import *

dg = Graph(directed=False)
dg.add_vertex(len(authors))
f = open("base_ed.txt", "rb")
for line in f.readlines():
    e1 = line.split()[0]
    e2 = line.split()[1]
    try:
        dg.add_edge(dg.vertex(authors.index(e1)), dg.vertex(authors.index(e2)))
    except:
        continue
f.close()
pos = sfdp_layout(dg)
graph_draw(dg, pos, output_size=(1000, 1000), vertex_color=[1,1,1,0],
                    vertex_size=1, edge_pen_width=1.2,
                    vcmap=matplotlib.cm.gist_heat_r, output="price.png")

# “Soft” block partition of a political books network with B=3
state = minimize_blockmodel_dl(dg, deg_corr=True)

# route edges according to the hierarchy
bstack = state.get_bstack()
t = get_hierarchy_tree(bstack)[0]
tpos = pos = radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
cts = get_hierarchy_control_points(dg, t, tpos)
pos = dg.own_property(tpos)
graph_draw(g, pos=pos, vertex_fill_color=b, edge_control_points=cts,
               edge_color=[0, 0, 0, 0.3], vertex_anchor=0, output="power_nested_mdl.pdf")

pos = sfdp_layout(dg)
graph_draw(dg, pos=pos, output="graph-draw-sfdp.pdf")

state = gt.minimize_blockmodel_dl(g)
>>> b = state.b
>>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=b, vertex_shape=b, output="polbooks_blocks_mdl.pdf")

pos = sfdp_layout(dg)
graph_draw(dg, pos, output_size=(1500, 1500), vertex_size=1, edge_pen_width=1.2,
           vcmap=matplotlib.cm.gist_heat_r, output="price.png")

@sangheestyle
Copy link
Author

import networkx as nx

g = nx.read_edgelist("base_edge_list.txt")
with open('base_authors.txt') as f:
    authors = f.read().splitlines()

authors = [a for a in authors if a in g.nodes()]
g_projected = nx.algorithms.bipartite.projected_graph(g, authors)
nx.write_graphml(g_projected, "base.graphml")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment