forked from maksim2042/SNABook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Max
committed
Mar 21, 2012
1 parent
db51bf0
commit e8d1acc
Showing
19 changed files
with
193,056 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"metadata": { | ||
"name": "Untitled0" | ||
}, | ||
"nbformat": 2, | ||
"worksheets": [] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import networkx as net | ||
import matplotlib.pyplot as plot | ||
from collections import defaultdict | ||
|
||
def plot_multimode(m,layout=net.spring_layout, type_string='type', with_labels=True, filename_prefix='',output_type='pdf'): | ||
|
||
## create a default color order and an empty color-map | ||
colors=['r','g','b','c','m','y','k'] | ||
colormap={} | ||
d=net.degree(m) #we use degree for sizing nodes | ||
pos=layout(m) #compute layout | ||
|
||
#Now we need to find groups of nodes that need to be colored differently | ||
nodesets=defaultdict(list) | ||
for n in m.nodes(): | ||
try: | ||
t=m.node[n][type_string] | ||
except KeyError: | ||
##this happens if a node doesn't have a type_string -- give it a None value | ||
t='None' | ||
nodesets[t].append(n) | ||
|
||
## Draw each group of nodes separately, using its own color settings | ||
print "drawing nodes..." | ||
i=0 | ||
for key in nodesets.keys(): | ||
#ns=[d[n]*100 for n in nodesets[key]] | ||
net.draw_networkx_nodes(m,pos,nodelist=nodesets[key], node_color=colors[i], alpha=0.6) #node_size=ns, | ||
colormap[key]=colors[i] | ||
i+=1 | ||
if i==len(colors): | ||
i=0 ### wrap around the colormap if we run out of colors | ||
print colormap | ||
|
||
## Draw edges using a default drawing mechanism | ||
print "drawing edges..." | ||
net.draw_networkx_edges(m,pos,width=0.5,alpha=0.5) | ||
|
||
print "drawing labels..." | ||
if with_labels: | ||
net.draw_networkx_labels(m,pos,font_size=12) | ||
plot.axis('off') | ||
if filename_prefix is not '': | ||
plot.savefig(filename_prefix+'.'+output_type) |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pac_types_str=""" | ||
C = Communication Cost | ||
D = Delegate | ||
E = Electioneering Communication | ||
H = House | ||
I = Independent Expenditor (Person or Group) | ||
N = PAC - Nonqualified | ||
O = Independent Expenditure-Only (Super PACs) | ||
P = Presidential | ||
Q = PAC - Qualified | ||
S = Senate | ||
U = Single Candidate Independent Expenditure | ||
X = Party Nonqualified | ||
Y = Party Qualified | ||
Z = National Party Nonfederal Account | ||
""".replace(' = ','=').strip().split('\n') | ||
|
||
pac_types=dict([tuple(row.split('=')) for row in pac_types_str]) |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env python | ||
# encoding: utf-8 | ||
""" | ||
two_mode.py | ||
Created by Maksim Tsvetovat on 2011-08-17. | ||
Copyright (c) 2011 __MyCompanyName__. All rights reserved. | ||
""" | ||
|
||
import sys | ||
import os | ||
import csv | ||
import math | ||
import networkx as net | ||
import matplotlib.pyplot as plot | ||
|
||
## Import bi-partite (bi-modal) functions | ||
from networkx.algorithms import bipartite as bi | ||
|
||
def trim_edges(g, weight=1): | ||
g2=net.Graph() | ||
for f, to, edata in g.edges(data=True): | ||
if edata['weight'] > weight: | ||
g2.add_edge(f,to,edata) | ||
return g2 | ||
|
||
|
||
## Read the data from a CSV file | ||
## We use the Universal new-line mode since many CSV files are created with Excel | ||
r=csv.reader(open('campaign_short.csv','rU')) | ||
|
||
## 2-mode graphs are usually directed. Here, their direction implies money flow | ||
g=net.Graph() | ||
|
||
## we need to keep track separately of nodes of all types | ||
pacs=[] | ||
candidates=[] | ||
|
||
## Construct a directed graph from edges in the CSV file | ||
for row in r: | ||
if row[0] not in pacs: | ||
pacs.append(row[0]) | ||
if row[12] not in candidates: | ||
candidates.append(row[12]) | ||
g.add_edge(row[0],row[12], weight=int(row[10])) | ||
|
||
## compute the projected graph | ||
pacnet=bi.weighted_projected_graph(g, pacs, ratio=False) | ||
pacnet=net.connected_component_subgraphs(pacnet)[0] | ||
weights=[math.log(edata['weight']) for f,t,edata in pacnet.edges(data=True)] | ||
|
||
net.draw_networkx(p,width=weights, edge_color=weights) | ||
|
||
|
||
|
||
## Compute the candidate network | ||
cannet=bi.weighted_projected_graph(g, candidates, ratio=False) | ||
cannet=net.connected_component_subgraphs(cannet)[0] | ||
weights=[math.log(edata['weight']) for f,t,edata in cannet.edges(data=True)] | ||
plot.figure(2) ## switch to a fresh canvas | ||
net.draw_networkx(cannet,width=weights, edge_color=weights) | ||
|
||
|
||
plot.figure(3) | ||
plot.hist(weights) | ||
|
||
## The weights histogram is logarithmic; we should compute the original weight = e^log_weight | ||
cannet_trim=trim_edges(cannet, weight=math.exp(0.9)) | ||
|
||
plot.figure(4) | ||
## re-calculate weights based on the new graph | ||
weights=[edata['weight'] for f,t,edata in cannet_trim.edges(data=True)] | ||
net.draw_networkx(cannet_trim,width=weights, edge_color=weights) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import networkx as net | ||
from networkx.algorithms import bipartite as bi | ||
|
||
from collections import Counter | ||
|
||
candidates={} | ||
can_file=open('2012/foiacn.txt','rb') | ||
for line in can_file: | ||
cid=line[0:9] | ||
name=line[9:47].strip() | ||
party=line[47:50] | ||
inc=line[56] | ||
zip=line[147:152] | ||
candidates[cid]={'name':name,'party':party,'type':ctype,'inc':inc,'zip':zip} | ||
|
||
[(k,pacs[k]) for k in pacs.keys()[:10]] | ||
|
||
ctype_counter=Counter() | ||
for can in candidates.values(): | ||
ctype_counter[can['type']]+=1 | ||
ctype_counter | ||
|
||
from pac_types import pac_types | ||
pacs={} | ||
pac_file=open('2012/foiacm.txt','rb') | ||
for line in pac_file: | ||
pid=line[0:9] | ||
ctype=line[0] | ||
name=line[9:99].strip() | ||
party=line[232-235] | ||
ctype=line[231] | ||
zip=line[225:230] | ||
pacs[pid]={'name':name,'party':party,'type':ctype,'zip':zip} | ||
|
||
ctype_counter=Counter() | ||
for pac in pacs.values(): | ||
ctype_counter[pac['type']]+=1 | ||
ctype_counter | ||
|
||
|
||
g=net.Graph() | ||
can_list=[] | ||
pac_list=[] | ||
contrib=open('2012/itpas2.txt','rb') | ||
for line in contrib: | ||
pid=line[0:9] | ||
cid=line[52:61] | ||
#amt=int(line[36:43]) | ||
g.add_edge(pid,cid) | ||
if cid not in can_list: can_list.append(cid) | ||
if pid not in pac_list: pac_list.append(pid) | ||
if pid in pacs: | ||
g.node[pid]=pacs[pid] | ||
else: | ||
pacs[pid]={'type':'unknown'} | ||
if cid in candidates: | ||
g.node[cid]=candidates[cid] | ||
else: | ||
candidates[cid]={'type':'unknown'} | ||
|
||
|
||
cannet=bi.weighted_projected_graph(g, can_list, ratio=False) | ||
|
||
def trim_edges(g, weight=1): | ||
g2=net.Graph() | ||
for f, to, edata in g.edges(data=True): | ||
if edata['weight'] > weight: | ||
g2.add_edge(f,to,edata) | ||
g2.node[f]=g.node[f] | ||
g2.node[to]=g.node[to] | ||
return g2 | ||
|
||
import multimode as mm | ||
|
||
cancore=trim_edges(cannet, weight=50) | ||
mm.plot_multimode(cancore, type_string='party') | ||
|
||
pacnet=bi.weighted_projected_graph(g, pac_list, ratio=False) | ||
paccore = trim_edges(pacnet, weight=50) | ||
|
||
|
||
def sorted_map(dct): | ||
ds = sorted(dct.iteritems(), key=lambda (k,v): (-v,k)) | ||
return ds | ||
|
||
d=sorted_map(net.degree(paccore)) | ||
c=sorted_map(net.closeness_centrality(paccore)) | ||
inf_pacs=[pacs[pid] for pid,deg in d[:10]] | ||
close_pacs=[pacs[pid] for pid,deg in c[:10]] | ||
|
||
|
||
""" | ||
[{'name': 'NATIONAL ASSOCIATION OF REALTORS POLITICAL ACTION COMMITTEE', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '60611'}, | ||
{'name': 'AT&T INC. FEDERAL POLITICAL ACTION COMMITTEE (AT&T FEDERAL PAC)', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '75202'}, | ||
{'name': 'UNITED PARCEL SERVICE INC. PAC', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '30328'}, | ||
{'name': 'HONEYWELL INTERNATIONAL POLITICAL ACTION COMMITTEE', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '20001'}, | ||
{'name': "LOCKHEED MARTIN CORPORATION EMPLOYEES' POLITICAL ACTION COMMITTEE", | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '22202'}, | ||
{'name': 'NATIONAL BEER WHOLESALERS ASSOCIATION POLITICAL ACTION COMMITTEE', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '22314'}, | ||
{'name': 'GENERAL ELECTRIC COMPANY POLITICAL ACTION COMMITTEE (GEPAC)', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '20004'}, | ||
{'name': 'COMCAST CORPORATION POLITICAL ACTION COMMITTEE- FEDERAL', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '19103'}, | ||
{'name': 'THE BOEING COMPANY POLITICAL ACTION COMMITTEE', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '22209'}, | ||
{'name': 'VERIZON COMMUNICATIONS INC./VERIZON WIRELESS GOOD GOVERNMENT CLUB (VERIZON/VERIZON WIRELES', | ||
'party': ' ', | ||
'type': 'Q', | ||
'zip': '20005'}] | ||
""" |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.