Skip to content

Commit

Permalink
Added Webcast 4 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Mar 21, 2012
1 parent db51bf0 commit e8d1acc
Show file tree
Hide file tree
Showing 19 changed files with 193,056 additions and 0 deletions.
2,000 changes: 2,000 additions & 0 deletions webcast4/2000/campaign_short.csv

Large diffs are not rendered by default.

Binary file added webcast4/2012/cm12.zip
Binary file not shown.
Binary file added webcast4/2012/cn12.zip
Binary file not shown.
11,773 changes: 11,773 additions & 0 deletions webcast4/2012/foiacm.txt

Large diffs are not rendered by default.

4,654 changes: 4,654 additions & 0 deletions webcast4/2012/foiacn.txt

Large diffs are not rendered by default.

117,187 changes: 117,187 additions & 0 deletions webcast4/2012/itpas2.txt

Large diffs are not rendered by default.

Binary file added webcast4/2012/pas212.zip
Binary file not shown.
2,315 changes: 2,315 additions & 0 deletions webcast4/Demo2012.ipynb

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions webcast4/Untitled0.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"metadata": {
"name": "Untitled0"
},
"nbformat": 2,
"worksheets": []
}
44 changes: 44 additions & 0 deletions webcast4/multimode.py
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 added webcast4/multimode.pyc
Binary file not shown.
18 changes: 18 additions & 0 deletions webcast4/pac_types.py
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 added webcast4/pac_types.pyc
Binary file not shown.
73 changes: 73 additions & 0 deletions webcast4/two_mode.py
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)
133 changes: 133 additions & 0 deletions webcast4/webcast.py
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 added webcast4/webcast4.key
Binary file not shown.
Binary file added webcast4/webcast4.ppt
Binary file not shown.
Loading

0 comments on commit e8d1acc

Please sign in to comment.