<s> import json <EOL> import os <EOL> from world import world , res_filename <EOL> from bigml . model import Model <EOL> from bigml . logistic import LogisticRegression <EOL> from bigml . cluster import Cluster <EOL> from bigml . anomaly import Anomaly <EOL> from bigml . multimodel import MultiModel <EOL> from bigml . multivote import MultiVote <EOL> from create_prediction_steps import check_prediction <EOL> def i_retrieve_a_list_of_remote_models ( step , tag ) : <EOL> world . list_of_models = [ world . api . get_model ( model [ '<STR_LIT>' ] ) for model in <EOL> world . api . list_models ( query_string = "<STR_LIT>" % tag ) [ '<STR_LIT>' ] ] <EOL> def i_create_a_local_model_from_file ( step , model_file ) : <EOL> world . local_model = Model ( res_filename ( model_file ) ) <EOL> def i_create_a_local_model ( step ) : <EOL> world . local_model = Model ( world . model ) <EOL> def i_create_a_multiple_local_prediction ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . predict ( data , multiple = '<STR_LIT:all>' ) <EOL> def i_create_a_local_prediction_with_confidence ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . predict ( data , add_confidence = True ) <EOL> def i_create_a_local_prediction ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . predict ( data ) <EOL> def i_create_a_local_median_prediction ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . predict ( data , median = True ) <EOL> def i_create_a_local_mm_median_batch_prediction ( self , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . batch_predict ( <EOL> [ data ] , to_file = False , use_median = True ) [ <NUM_LIT:0> ] . predictions [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> def i_create_a_local_proportional_median_prediction ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . predict ( data , missing_strategy = <NUM_LIT:1> , median = True ) <EOL> def i_create_a_local_cluster ( step ) : <EOL> world . local_cluster = Cluster ( world . cluster ) <EOL> def i_create_a_local_centroid ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> for key , value in data . items ( ) : <EOL> if value == "<STR_LIT>" : <EOL> del data [ key ] <EOL> world . local_centroid = world . local_cluster . centroid ( data ) <EOL> def the_local_centroid_is ( step , centroid , distance ) : <EOL> check_prediction ( world . local_centroid [ '<STR_LIT>' ] , centroid ) <EOL> check_prediction ( world . local_centroid [ '<STR_LIT>' ] , distance ) <EOL> def i_create_a_local_anomaly ( step ) : <EOL> world . local_anomaly = Anomaly ( world . anomaly [ '<STR_LIT>' ] ) <EOL> def i_create_a_local_anomaly_score ( step , input_data ) : <EOL> input_data = json . loads ( input_data ) <EOL> world . local_anomaly_score = world . local_anomaly . anomaly_score ( input_data , <EOL> by_name = False ) <EOL> def the_local_anomaly_score_is ( step , score ) : <EOL> if str ( round ( world . local_anomaly_score , <NUM_LIT:2> ) ) == str ( round ( float ( score ) , <NUM_LIT:2> ) ) : <EOL> assert True <EOL> else : <EOL> assert False , ( "<STR_LIT>" % <EOL> ( str ( round ( world . local_anomaly_score , <NUM_LIT:2> ) ) , <EOL> round ( float ( score ) , <NUM_LIT:2> ) ) ) <EOL> def i_create_a_proportional_local_prediction ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . predict ( <EOL> data , with_confidence = True , missing_strategy = <NUM_LIT:1> ) <EOL> def i_create_a_prediction_from_a_multi_model ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT:{}>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . predict ( data ) <EOL> def i_create_a_batch_prediction_from_a_multi_model ( step , data = None ) : <EOL> if data is None : <EOL> data = "<STR_LIT>" <EOL> data = json . loads ( data ) <EOL> world . local_prediction = world . local_model . batch_predict ( data , <EOL> to_file = False ) <EOL> def the_batch_mm_predictions_are ( step , predictions ) : <EOL> if predictions is None : <EOL> predictions = "<STR_LIT>" <EOL> predictions = json . loads ( predictions ) <EOL> for i in range ( len ( predictions ) ) : <EOL> multivote = world . local_prediction [ i ] <EOL> for prediction in multivote . predictions : <EOL> if prediction [ '<STR_LIT>' ] != predictions [ i ] : <EOL> assert False , ( "<STR_LIT>" % <EOL> ( predictions [ i ] , prediction [ '<STR_LIT>' ] ) ) <EOL> break <EOL> if i == len ( predictions ) : <EOL> assert True <EOL> def the_multiple_local_prediction_is ( step , prediction ) : <EOL> local_prediction = world . local_prediction <EOL> prediction = json . loads ( prediction ) <EOL> if local_prediction == prediction : <EOL> assert True <EOL> else : <EOL> assert False , "<STR_LIT>" % ( local_prediction , prediction ) <EOL> def the_local_prediction_confidence_is ( step , confidence ) : <EOL> if ( isinstance ( world . local_prediction , list ) or <EOL> isinstance ( world . local_prediction , tuple ) ) : <EOL> local_confidence = world . local_prediction [ <NUM_LIT:1> ] <EOL> else : <EOL> local_confidence = world . local_prediction [ '<STR_LIT>' ] <EOL> local_confidence = round ( float ( local_confidence ) , <NUM_LIT:4> ) <EOL> confidence = round ( float ( confidence ) , <NUM_LIT:4> ) <EOL> if local_confidence == confidence : <EOL> assert True <EOL> else : <EOL> assert False , "<STR_LIT>" % ( local_confidence , confidence ) <EOL> def the_local_prediction_is ( step , prediction ) : <EOL> if ( isinstance ( world . local_prediction , list ) or <EOL> isinstance ( world . local_prediction , tuple ) ) : <EOL> local_prediction = world . local_prediction [ <NUM_LIT:0> ] <EOL> elif isinstance ( world . local_prediction , dict ) : <EOL> local_prediction = world . local_prediction [ '<STR_LIT>' ] <EOL> else : <EOL> local_prediction = world . local_prediction <EOL> try : <EOL> local_model = world . local_model <EOL> if not isinstance ( world . local_model , LogisticRegression ) : <EOL> if isinstance ( local_model , MultiModel ) : <EOL> local_model = local_model . models [ <NUM_LIT:0> ] <EOL> if local_model . tree . regression : <EOL> local_prediction = round ( float ( local_prediction ) , <NUM_LIT:4> ) <EOL> prediction = round ( float ( prediction ) , <NUM_LIT:4> ) <EOL> except AttributeError : <EOL> local_model = world . local_ensemble . multi_model . models [ <NUM_LIT:0> ] <EOL> if local_model . tree . regression : <EOL> local_prediction = round ( float ( local_prediction ) , <NUM_LIT:4> ) <EOL> prediction = round ( float ( prediction ) , <NUM_LIT:4> ) <EOL> if local_prediction == prediction : <EOL> assert True <EOL> else : <EOL> assert False , "<STR_LIT>" % ( local_prediction , prediction ) <EOL> def the_local_probability_is ( step , probability ) : <EOL> probability = round ( float ( probability ) , <NUM_LIT:4> ) <EOL> local_probability = world . local_prediction [ "<STR_LIT>" ] <EOL> def i_create_a_local_multi_model ( step ) : <EOL> world . local_model = MultiModel ( world . list_of_models ) <EOL> def i_create_a_batch_prediction ( step , input_data_list , directory ) : <EOL> if len ( directory ) > <NUM_LIT:0> and not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> input_data_list = eval ( input_data_list ) <EOL> assert isinstance ( input_data_list , list ) <EOL> world . local_model . batch_predict ( input_data_list , directory ) <EOL> def i_combine_the_votes ( step , directory ) : <EOL> world . votes = world . local_model . batch_votes ( directory ) <EOL> def the_plurality_combined_prediction ( step , predictions ) : <EOL> predictions = eval ( predictions ) <EOL> for i in range ( len ( world . votes ) ) : <EOL> combined_prediction = world . votes [ i ] . combine ( ) <EOL> check_prediction ( combined_prediction , predictions [ i ] ) <EOL> def the_confidence_weighted_prediction ( step , predictions ) : <EOL> predictions = eval ( predictions ) <EOL> for i in range ( len ( world . votes ) ) : <EOL> combined_prediction = world . votes [ i ] . combine ( <NUM_LIT:1> ) <EOL> assert combined_prediction == predictions [ i ] <EOL> def i_create_a_local_logistic_model ( step ) : <EOL> world . local_model = LogisticRegression ( world . logistic_regression ) </s>
