Skip to content

Commit

Permalink
added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
twneale committed Sep 6, 2013
1 parent 6a22dee commit 48e3307
Showing 1 changed file with 65 additions and 11 deletions.
76 changes: 65 additions & 11 deletions pscl/rollcall.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
import numpy as np
import matplotlib.pyplot as plt
from pandas.rpy import common as rpy_common
from scipy.stats import gaussian_kde

from rpy2.robjects.packages import importr

from .base import Field, Translator, Wrapper
from .accessors import ValueAccessor, VectorAccessor
from .ordfile import OrdFile
from .wnominate import wnominate

from .ideal import ideal

pscl = importr('pscl')


class RollcallSummary(Wrapper):
class NumberOfLegislators(VectorAccessor):
'''Number of legislators in the rollcall object, after processing the
dropList.
'''
key = 'n'


class NumberOfRollcalls(VectorAccessor):
'''Number of roll call votes in the rollcall object, after
processing the dropList.
'''
key = 'm'


class AllVotes(VectorAccessor):
'''A matrix containing a tabular breakdown of all votes in the
rollcall matrix (object$votes), after processing the
dropList.
'''
key = 'allVotes'

all_votes = VectorAccessor('allVotes')
n = ValueAccessor('n')
m = ValueAccessor('m')

class Votes(VectorAccessor):
'''
'''


class Source(VectorAccessor):
'''A fake, hard-coded C: filesystem location of the ord file. Useless.
'''

class RollcallSummary(Wrapper):

all_votes = AllVotes()
n = NumberOfLegislators()
m = NumberOfRollcalls()
eq_attrs = ('m', 'n', 'codes', 'all_votes')

@property
Expand All @@ -31,7 +65,8 @@ def codes(self):


class Rollcall(Wrapper):

'''A wrapper for the pscl rollcall object.
'''
# Wrapped R functions ---------------------------------------------------
def drop_unanimous(self, lop=0):
self.obj = pscl.dropUnanimous(self.obj, lop=0)
Expand All @@ -41,10 +76,10 @@ def summary(self):
return RollcallSummary(pscl.summary_rollcall(self.obj))

# Accessors ---------------------------------------------------------------
n = ValueAccessor('n')
m = ValueAccessor('m')
all_votes = VectorAccessor('votes')

n = NumberOfLegislators()
m = NumberOfRollcalls()
votes = Votes()
source = Source()
eq_attrs = ('m', 'n', 'codes', 'all_votes')

@property
Expand Down Expand Up @@ -76,13 +111,32 @@ def from_dataframe(cls, dataframe, **kwargs):
r_matrix = rpy_common.convert_to_r_matrix(dataframe)
return cls.from_matrix(r_matrix, **kwargs)

def ideal(self, polarity, *args, **kwargs):
@classmethod
def from_ordfile(cls, fp, **kwargs):
'''Instantiate a RollCall object from an ordfile.
'''
dataframe = OrdFile(fp).as_dataframe()
rollcall = cls.from_dataframe(dataframe,
yea=[1.0, 2.0, 3.0],
nay=[4.0, 5.0, 6.0],
missing=[7.0, 8.0, 9.0],
not_in_legis=0.0,
legis_names=tuple(dataframe.index), **kwargs)
return rollcall

# Analysis methods -------------------------------------------------------
def ideal(self, *args, **kwargs):
'''
'''
return ideal(self, *args, **kwargs)

def wnominate(self, polarity, *args, **kwargs):
'''
'''
return wnominate(self, polarity, *args, **kwargs)



class _RollcallTranslator(Translator):
'''A python wrapper around the R pscl pacakge's rollcall object.
'''
Expand Down

0 comments on commit 48e3307

Please sign in to comment.