diff --git a/pscl/rollcall.py b/pscl/rollcall.py index 817db75..bff1998 100644 --- a/pscl/rollcall.py +++ b/pscl/rollcall.py @@ -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 @@ -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) @@ -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 @@ -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. '''