<s> from operator import itemgetter <EOL> import numpy as np <EOL> import pandas as pd <EOL> from zbox import toolz as tz <EOL> from . skim import Skims , Skims3D <EOL> from . mnl import utils_to_probs , make_choices , interaction_dataset <EOL> import os <EOL> import psutil <EOL> import gc <EOL> def usage ( when ) : <EOL> gc . collect ( ) <EOL> process = psutil . Process ( os . getpid ( ) ) <EOL> bytes = process . memory_info ( ) . rss <EOL> mb = ( bytes / ( <NUM_LIT> * <NUM_LIT> ) ) <EOL> gb = ( bytes / ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) ) <EOL> return "<STR_LIT>" % ( when , int ( mb ) , round ( gb , <NUM_LIT:2> ) ) <EOL> def random_rows ( df , n ) : <EOL> return df . take ( np . random . choice ( len ( df ) , size = n , replace = False ) ) <EOL> def read_model_spec ( fname , <EOL> description_name = "<STR_LIT>" , <EOL> expression_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> cfg = pd . read_csv ( fname , comment = '<STR_LIT:#>' ) <EOL> cfg = cfg . dropna ( subset = [ expression_name ] ) <EOL> cfg = cfg . drop ( description_name , axis = <NUM_LIT:1> ) . set_index ( expression_name ) <EOL> return cfg <EOL> def eval_variables ( exprs , df , locals_d = None ) : <EOL> """<STR_LIT>""" <EOL> if locals_d is None : <EOL> locals_d = { } <EOL> locals_d . update ( locals ( ) ) <EOL> def to_series ( x ) : <EOL> if np . isscalar ( x ) : <EOL> return pd . Series ( [ x ] * len ( df ) , index = df . index ) <EOL> return x <EOL> l = [ ] <EOL> for e in exprs : <EOL> try : <EOL> l . append ( ( e , to_series ( eval ( e [ <NUM_LIT:1> : ] , globals ( ) , locals_d ) ) <EOL> if e . startswith ( '<STR_LIT:@>' ) else df . eval ( e ) ) ) <EOL> except Exception as err : <EOL> print "<STR_LIT>" % str ( e ) <EOL> raise err <EOL> return pd . DataFrame . from_items ( l ) <EOL> def add_skims ( df , skims ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( skims , list ) : <EOL> assert isinstance ( skims , Skims ) or isinstance ( skims , Skims3D ) <EOL> skims . set_df ( df ) <EOL> else : <EOL> for skim in skims : <EOL> assert isinstance ( skim , Skims ) or isinstance ( skim , Skims3D ) <EOL> skim . set_df ( df ) <EOL> def _check_for_variability ( model_design ) : <EOL> """<STR_LIT>""" <EOL> l = min ( <NUM_LIT:1000> , len ( model_design ) ) <EOL> sample = random_rows ( model_design , l ) <EOL> sample = sample . astype ( '<STR_LIT:float>' ) <EOL> sample = sample . describe ( ) . transpose ( ) <EOL> error = sample [ sample [ "<STR_LIT>" ] == <NUM_LIT:0> ] <EOL> if len ( error ) : <EOL> print "<STR_LIT>" , error . index . values <EOL> error = sample [ sample [ "<STR_LIT:count>" ] < l ] <EOL> if len ( error ) : <EOL> print "<STR_LIT>" , error . index . values <EOL> def simple_simulate ( choosers , spec , skims = None , locals_d = None ) : <EOL> """<STR_LIT>""" <EOL> if skims : <EOL> add_skims ( choosers , skims ) <EOL> model_design = eval_variables ( spec . index , choosers , locals_d ) <EOL> _check_for_variability ( model_design ) <EOL> utilities = model_design . dot ( spec ) <EOL> probs = utils_to_probs ( utilities ) <EOL> choices = make_choices ( probs ) <EOL> return choices , model_design <EOL> def interaction_simulate ( <EOL> choosers , alternatives , spec , <EOL> skims = None , locals_d = None , sample_size = None ) : <EOL> """<STR_LIT>""" <EOL> if len ( spec . columns ) > <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> sample_size = sample_size or len ( alternatives ) <EOL> if sample_size > len ( alternatives ) : <EOL> print "<STR_LIT>" % ( sample_size , len ( alternatives ) ) <EOL> sample_size = min ( sample_size , len ( alternatives ) ) <EOL> alternatives [ alternatives . index . name ] = alternatives . index <EOL> df = interaction_dataset ( choosers , alternatives , sample_size ) <EOL> if skims : <EOL> add_skims ( df , skims ) <EOL> model_design = eval_variables ( spec . index , df , locals_d ) <EOL> _check_for_variability ( model_design ) <EOL> utilities = model_design . dot ( spec ) . astype ( '<STR_LIT:float>' ) <EOL> utilities = pd . DataFrame ( <EOL> utilities . as_matrix ( ) . reshape ( len ( choosers ) , sample_size ) , <EOL> index = choosers . index ) <EOL> probs = utils_to_probs ( utilities ) <EOL> positions = make_choices ( probs ) <EOL> offsets = np . arange ( len ( positions ) ) * sample_size <EOL> choices = model_design . index . take ( positions + offsets ) <EOL> return pd . Series ( choices , index = choosers . index ) , model_design <EOL> def other_than ( groups , bools ) : <EOL> """<STR_LIT>""" <EOL> counts = groups [ bools ] . value_counts ( ) <EOL> merge_col = groups . to_frame ( name = '<STR_LIT:right>' ) <EOL> pipeline = tz . compose ( <EOL> tz . curry ( pd . Series . fillna , value = False ) , <EOL> itemgetter ( '<STR_LIT:left>' ) , <EOL> tz . curry ( <EOL> pd . DataFrame . merge , right = merge_col , how = '<STR_LIT:right>' , left_index = True , <EOL> right_on = '<STR_LIT:right>' ) , <EOL> tz . curry ( pd . Series . to_frame , name = '<STR_LIT:left>' ) ) <EOL> gt0 = pipeline ( counts > <NUM_LIT:0> ) <EOL> gt1 = pipeline ( counts > <NUM_LIT:1> ) <EOL> return gt1 . where ( bools , other = gt0 ) </s>
