<s> """<STR_LIT>""" <EOL> import logging <EOL> import numbers <EOL> import math <EOL> LOGGER = logging . getLogger ( '<STR_LIT>' ) <EOL> PLURALITY = '<STR_LIT>' <EOL> CONFIDENCE = '<STR_LIT>' <EOL> PROBABILITY = '<STR_LIT>' <EOL> THRESHOLD = '<STR_LIT>' <EOL> PLURALITY_CODE = <NUM_LIT:0> <EOL> CONFIDENCE_CODE = <NUM_LIT:1> <EOL> PROBABILITY_CODE = <NUM_LIT:2> <EOL> THRESHOLD_CODE = <NUM_LIT:3> <EOL> PREDICTION_HEADERS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:count>' ] <EOL> COMBINATION_WEIGHTS = { <EOL> PLURALITY : None , <EOL> CONFIDENCE : '<STR_LIT>' , <EOL> PROBABILITY : '<STR_LIT>' , <EOL> THRESHOLD : None } <EOL> COMBINER_MAP = { <EOL> PLURALITY_CODE : PLURALITY , <EOL> CONFIDENCE_CODE : CONFIDENCE , <EOL> PROBABILITY_CODE : PROBABILITY , <EOL> THRESHOLD_CODE : THRESHOLD } <EOL> WEIGHT_KEYS = { <EOL> PLURALITY : None , <EOL> CONFIDENCE : [ '<STR_LIT>' ] , <EOL> PROBABILITY : [ '<STR_LIT>' , '<STR_LIT:count>' ] , <EOL> THRESHOLD : None } <EOL> DEFAULT_METHOD = <NUM_LIT:0> <EOL> BINS_LIMIT = <NUM_LIT:32> <EOL> def ws_confidence ( prediction , distribution , ws_z = <NUM_LIT> , ws_n = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( distribution , list ) : <EOL> distribution = dict ( distribution ) <EOL> ws_p = distribution [ prediction ] <EOL> if ws_p < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> ws_norm = float ( sum ( distribution . values ( ) ) ) <EOL> if ws_norm != <NUM_LIT:1.0> : <EOL> ws_p = ws_p / ws_norm <EOL> if ws_n is None : <EOL> ws_n = ws_norm <EOL> else : <EOL> ws_n = float ( ws_n ) <EOL> if ws_n < <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ws_z = float ( ws_z ) <EOL> ws_z2 = ws_z * ws_z <EOL> ws_factor = ws_z2 / ws_n <EOL> ws_sqrt = math . sqrt ( ( ws_p * ( <NUM_LIT:1> - ws_p ) + ws_factor / <NUM_LIT:4> ) / ws_n ) <EOL> return ( ws_p + ws_factor / <NUM_LIT:2> - ws_z * ws_sqrt ) / ( <NUM_LIT:1> + ws_factor ) <EOL> def merge_distributions ( distribution , new_distribution ) : <EOL> """<STR_LIT>""" <EOL> for value , instances in new_distribution . items ( ) : <EOL> if value not in distribution : <EOL> distribution [ value ] = <NUM_LIT:0> <EOL> distribution [ value ] += instances <EOL> return distribution <EOL> def merge_bins ( distribution , limit ) : <EOL> """<STR_LIT>""" <EOL> length = len ( distribution ) <EOL> if limit < <NUM_LIT:1> or length <= limit or length < <NUM_LIT:2> : <EOL> return distribution <EOL> index_to_merge = <NUM_LIT:2> <EOL> shortest = float ( '<STR_LIT>' ) <EOL> for index in range ( <NUM_LIT:1> , length ) : <EOL> distance = distribution [ index ] [ <NUM_LIT:0> ] - distribution [ index - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if distance < shortest : <EOL> shortest = distance <EOL> index_to_merge = index <EOL> new_distribution = distribution [ : index_to_merge - <NUM_LIT:1> ] <EOL> left = distribution [ index_to_merge - <NUM_LIT:1> ] <EOL> right = distribution [ index_to_merge ] <EOL> new_bin = [ ( left [ <NUM_LIT:0> ] * left [ <NUM_LIT:1> ] + right [ <NUM_LIT:0> ] * right [ <NUM_LIT:1> ] ) / <EOL> ( left [ <NUM_LIT:1> ] + right [ <NUM_LIT:1> ] ) , left [ <NUM_LIT:1> ] + right [ <NUM_LIT:1> ] ] <EOL> new_distribution . append ( new_bin ) <EOL> if index_to_merge < ( length - <NUM_LIT:1> ) : <EOL> new_distribution . extend ( distribution [ ( index_to_merge + <NUM_LIT:1> ) : ] ) <EOL> return merge_bins ( new_distribution , limit ) <EOL> class MultiVote ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def grouped_distribution ( cls , instance ) : <EOL> """<STR_LIT>""" <EOL> joined_distribution = { } <EOL> distribution_unit = '<STR_LIT>' <EOL> for prediction in instance . predictions : <EOL> joined_distribution = merge_distributions ( <EOL> joined_distribution , <EOL> dict ( ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] ) for x in prediction [ '<STR_LIT>' ] ) ) <EOL> distribution = [ list ( element ) for element in <EOL> sorted ( joined_distribution . items ( ) , <EOL> key = lambda x : x [ <NUM_LIT:0> ] ) ] <EOL> if distribution_unit == '<STR_LIT>' : <EOL> distribution_unit = ( '<STR_LIT>' if len ( distribution ) > BINS_LIMIT <EOL> else '<STR_LIT>' ) <EOL> distribution = merge_bins ( distribution , BINS_LIMIT ) <EOL> return { '<STR_LIT>' : distribution , <EOL> '<STR_LIT>' : distribution_unit } <EOL> @ classmethod <EOL> def avg ( cls , instance , with_confidence = False , <EOL> add_confidence = False , add_distribution = False , <EOL> add_count = False , add_median = False , add_min = False , add_max = False ) : <EOL> """<STR_LIT>""" <EOL> if ( instance . predictions and with_confidence and <EOL> not all ( [ '<STR_LIT>' in prediction <EOL> for prediction in instance . predictions ] ) ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> total = len ( instance . predictions ) <EOL> result = <NUM_LIT:0.0> <EOL> median_result = <NUM_LIT:0.0> <EOL> confidence = <NUM_LIT:0.0> <EOL> instances = <NUM_LIT:0> <EOL> d_min = float ( '<STR_LIT>' ) <EOL> d_max = float ( '<STR_LIT>' ) <EOL> for prediction in instance . predictions : <EOL> result += prediction [ '<STR_LIT>' ] <EOL> if add_median : <EOL> median_result += prediction [ '<STR_LIT>' ] <EOL> if with_confidence or add_confidence : <EOL> confidence += prediction [ '<STR_LIT>' ] <EOL> if add_count : <EOL> instances += prediction [ '<STR_LIT:count>' ] <EOL> if add_min and d_min > prediction [ '<STR_LIT>' ] : <EOL> d_min = prediction [ '<STR_LIT>' ] <EOL> if add_max and d_max < prediction [ '<STR_LIT>' ] : <EOL> d_max = prediction [ '<STR_LIT>' ] <EOL> if with_confidence : <EOL> return ( result / total , confidence / total ) if total > <NUM_LIT:0> else ( float ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> if ( add_confidence or add_distribution or add_count or <EOL> add_median or add_min or add_max ) : <EOL> output = { '<STR_LIT>' : result / total if total > <NUM_LIT:0> else float ( '<STR_LIT>' ) } <EOL> if add_confidence : <EOL> output . update ( <EOL> { '<STR_LIT>' : confidence / total if total > <NUM_LIT:0> else <NUM_LIT:0> } ) <EOL> if add_distribution : <EOL> output . update ( cls . grouped_distribution ( instance ) ) <EOL> if add_count : <EOL> output . update ( { '<STR_LIT:count>' : instances } ) <EOL> if add_median : <EOL> output . update ( <EOL> { '<STR_LIT>' : median_result / total if total > <NUM_LIT:0> else float ( '<STR_LIT>' ) } ) <EOL> if add_min : <EOL> output . update ( <EOL> { '<STR_LIT>' : d_min } ) <EOL> if add_max : <EOL> output . update ( <EOL> { '<STR_LIT>' : d_max } ) <EOL> return output <EOL> return result / total if total > <NUM_LIT:0> else float ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def error_weighted ( cls , instance , with_confidence = False , <EOL> add_confidence = False , add_distribution = False , <EOL> add_count = False , add_median = False , add_min = False , <EOL> add_max = False ) : <EOL> """<STR_LIT>""" <EOL> if ( instance . predictions and with_confidence and <EOL> not all ( [ '<STR_LIT>' in prediction <EOL> for prediction in instance . predictions ] ) ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> top_range = <NUM_LIT:10> <EOL> result = <NUM_LIT:0.0> <EOL> median_result = <NUM_LIT:0.0> <EOL> instances = <NUM_LIT:0> <EOL> d_min = float ( '<STR_LIT>' ) <EOL> d_max = float ( '<STR_LIT>' ) <EOL> normalization_factor = cls . normalize_error ( instance , top_range ) <EOL> if normalization_factor == <NUM_LIT:0> : <EOL> if with_confidence : <EOL> return float ( '<STR_LIT>' ) , <NUM_LIT:0> <EOL> else : <EOL> return float ( '<STR_LIT>' ) <EOL> if with_confidence or add_confidence : <EOL> combined_error = <NUM_LIT:0.0> <EOL> for prediction in instance . predictions : <EOL> result += prediction [ '<STR_LIT>' ] * prediction [ '<STR_LIT>' ] <EOL> if add_median : <EOL> median_result += ( prediction [ '<STR_LIT>' ] * <EOL> prediction [ '<STR_LIT>' ] ) <EOL> if add_count : <EOL> instances += prediction [ '<STR_LIT:count>' ] <EOL> if add_min and d_min > prediction [ '<STR_LIT>' ] : <EOL> d_min = prediction [ '<STR_LIT>' ] <EOL> if add_max and d_max < prediction [ '<STR_LIT>' ] : <EOL> d_max = prediction [ '<STR_LIT>' ] <EOL> if with_confidence or add_confidence : <EOL> combined_error += ( prediction [ '<STR_LIT>' ] * <EOL> prediction [ '<STR_LIT>' ] ) <EOL> del prediction [ '<STR_LIT>' ] <EOL> if with_confidence : <EOL> return ( result / normalization_factor , <EOL> combined_error / normalization_factor ) <EOL> if ( add_confidence or add_distribution or add_count or <EOL> add_median or add_min or add_max ) : <EOL> output = { '<STR_LIT>' : result / normalization_factor } <EOL> if add_confidence : <EOL> output . update ( { '<STR_LIT>' : <EOL> combined_error / normalization_factor } ) <EOL> if add_distribution : <EOL> output . update ( cls . grouped_distribution ( instance ) ) <EOL> if add_count : <EOL> output . update ( { '<STR_LIT:count>' : instances } ) <EOL> if add_median : <EOL> output . update ( { '<STR_LIT>' : median_result / normalization_factor } ) <EOL> if add_min : <EOL> output . update ( <EOL> { '<STR_LIT>' : d_min } ) <EOL> if add_max : <EOL> output . update ( <EOL> { '<STR_LIT>' : d_max } ) <EOL> return output <EOL> return result / normalization_factor <EOL> @ classmethod <EOL> def normalize_error ( cls , instance , top_range ) : <EOL> """<STR_LIT>""" <EOL> if instance . predictions and not all ( [ '<STR_LIT>' in prediction <EOL> for prediction <EOL> in instance . predictions ] ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> error_values = [ prediction [ '<STR_LIT>' ] <EOL> for prediction in instance . predictions ] <EOL> max_error = max ( error_values ) <EOL> min_error = min ( error_values ) <EOL> error_range = <NUM_LIT:1.0> * ( max_error - min_error ) <EOL> normalize_factor = <NUM_LIT:0> <EOL> if error_range > <NUM_LIT:0> : <EOL> for prediction in instance . predictions : <EOL> delta = ( min_error - prediction [ '<STR_LIT>' ] ) <EOL> prediction [ '<STR_LIT>' ] = math . exp ( delta / error_range * <EOL> top_range ) <EOL> normalize_factor += prediction [ '<STR_LIT>' ] <EOL> else : <EOL> for prediction in instance . predictions : <EOL> prediction [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> normalize_factor = len ( instance . predictions ) <EOL> return normalize_factor <EOL> def __init__ ( self , predictions ) : <EOL> """<STR_LIT>""" <EOL> self . predictions = [ ] <EOL> if isinstance ( predictions , list ) : <EOL> self . predictions . extend ( predictions ) <EOL> else : <EOL> self . predictions . append ( predictions ) <EOL> if not all ( [ '<STR_LIT>' in prediction for prediction in predictions ] ) : <EOL> for i in range ( len ( self . predictions ) ) : <EOL> self . predictions [ i ] [ '<STR_LIT>' ] = i <EOL> def is_regression ( self ) : <EOL> """<STR_LIT>""" <EOL> return all ( [ isinstance ( prediction [ '<STR_LIT>' ] , numbers . Number ) <EOL> for prediction in self . predictions ] ) <EOL> def next_order ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . predictions : <EOL> return self . predictions [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def combine ( self , method = DEFAULT_METHOD , with_confidence = False , <EOL> add_confidence = False , add_distribution = False , <EOL> add_count = False , add_median = False , add_min = False , <EOL> add_max = False , options = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . predictions : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> method = COMBINER_MAP . get ( method , COMBINER_MAP [ DEFAULT_METHOD ] ) <EOL> keys = WEIGHT_KEYS . get ( method , None ) <EOL> if keys is not None : <EOL> for key in keys : <EOL> if not all ( [ key in prediction for prediction <EOL> in self . predictions ] ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if self . is_regression ( ) : <EOL> for prediction in self . predictions : <EOL> if prediction [ '<STR_LIT>' ] is None : <EOL> prediction [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> function = NUMERICAL_COMBINATION_METHODS . get ( method , <EOL> self . __class__ . avg ) <EOL> return function ( self , with_confidence = with_confidence , <EOL> add_confidence = add_confidence , <EOL> add_distribution = add_distribution , <EOL> add_count = add_count , <EOL> add_median = add_median , <EOL> add_min = add_min , <EOL> add_max = add_max ) <EOL> else : <EOL> if method == THRESHOLD : <EOL> if options is None : <EOL> options = { } <EOL> predictions = self . single_out_category ( options ) <EOL> elif method == PROBABILITY : <EOL> predictions = MultiVote ( [ ] ) <EOL> predictions . predictions = self . probability_weight ( ) <EOL> else : <EOL> predictions = self <EOL> return predictions . combine_categorical ( <EOL> COMBINATION_WEIGHTS . get ( method , None ) , <EOL> with_confidence = with_confidence , <EOL> add_confidence = add_confidence , <EOL> add_distribution = add_distribution , <EOL> add_count = add_count ) <EOL> def probability_weight ( self ) : <EOL> """<STR_LIT>""" <EOL> predictions = [ ] <EOL> for prediction in self . predictions : <EOL> if '<STR_LIT>' not in prediction or '<STR_LIT:count>' not in prediction : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> total = prediction [ '<STR_LIT:count>' ] <EOL> if total < <NUM_LIT:1> or not isinstance ( total , int ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % total ) <EOL> order = prediction [ '<STR_LIT>' ] <EOL> for prediction , instances in prediction [ '<STR_LIT>' ] : <EOL> predictions . append ( { '<STR_LIT>' : prediction , <EOL> '<STR_LIT>' : float ( instances ) / total , <EOL> '<STR_LIT:count>' : instances , <EOL> '<STR_LIT>' : order } ) <EOL> return predictions <EOL> def combine_distribution ( self , weight_label = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not all ( [ weight_label in prediction <EOL> for prediction in self . predictions ] ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> distribution = { } <EOL> total = <NUM_LIT:0> <EOL> for prediction in self . predictions : <EOL> if prediction [ '<STR_LIT>' ] not in distribution : <EOL> distribution [ prediction [ '<STR_LIT>' ] ] = <NUM_LIT:0.0> <EOL> distribution [ prediction [ '<STR_LIT>' ] ] += prediction [ weight_label ] <EOL> total += prediction [ '<STR_LIT:count>' ] <EOL> if total > <NUM_LIT:0> : <EOL> distribution = [ [ key , value ] for key , value in <EOL> distribution . items ( ) ] <EOL> else : <EOL> distribution = [ ] <EOL> return distribution , total <EOL> def combine_categorical ( self , weight_label = None , with_confidence = False , <EOL> add_confidence = False , add_distribution = False , <EOL> add_count = False ) : <EOL> """<STR_LIT>""" <EOL> mode = { } <EOL> instances = <NUM_LIT:0> <EOL> if weight_label is None : <EOL> weight = <NUM_LIT:1> <EOL> for prediction in self . predictions : <EOL> if weight_label is not None : <EOL> if weight_label not in COMBINATION_WEIGHTS . values ( ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if weight_label not in prediction : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> weight = prediction [ weight_label ] <EOL> category = prediction [ '<STR_LIT>' ] <EOL> if add_count : <EOL> instances += prediction [ '<STR_LIT:count>' ] <EOL> if category in mode : <EOL> mode [ category ] = { "<STR_LIT:count>" : mode [ category ] [ "<STR_LIT:count>" ] + weight , <EOL> "<STR_LIT>" : mode [ category ] [ "<STR_LIT>" ] } <EOL> else : <EOL> mode [ category ] = { "<STR_LIT:count>" : weight , <EOL> "<STR_LIT>" : prediction [ '<STR_LIT>' ] } <EOL> prediction = sorted ( mode . items ( ) , key = lambda x : ( x [ <NUM_LIT:1> ] [ '<STR_LIT:count>' ] , <EOL> - x [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , <EOL> x [ <NUM_LIT:0> ] ) , <EOL> reverse = True ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if with_confidence or add_confidence : <EOL> if '<STR_LIT>' in self . predictions [ <NUM_LIT:0> ] : <EOL> prediction , combined_confidence = self . weighted_confidence ( <EOL> prediction , weight_label ) <EOL> else : <EOL> combined_distribution = self . combine_distribution ( ) <EOL> distribution , count = combined_distribution <EOL> combined_confidence = ws_confidence ( prediction , distribution , <EOL> ws_n = count ) <EOL> if with_confidence : <EOL> return prediction , combined_confidence <EOL> if add_confidence or add_distribution or add_count : <EOL> output = { '<STR_LIT>' : prediction } <EOL> if add_confidence : <EOL> output . update ( { '<STR_LIT>' : combined_confidence } ) <EOL> if add_distribution : <EOL> output . update ( self . __class__ . grouped_distribution ( self ) ) <EOL> if add_count : <EOL> output . update ( { '<STR_LIT:count>' : instances } ) <EOL> return output <EOL> return prediction <EOL> def weighted_confidence ( self , combined_prediction , weight_label ) : <EOL> """<STR_LIT>""" <EOL> predictions = [ prediction for prediction in self . predictions if prediction [ '<STR_LIT>' ] == combined_prediction ] <EOL> if ( weight_label is not None and <EOL> ( not isinstance ( weight_label , basestring ) or <EOL> any ( [ not '<STR_LIT>' or weight_label not in prediction <EOL> for prediction in predictions ] ) ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> weight_label ) <EOL> final_confidence = <NUM_LIT:0.0> <EOL> total_weight = <NUM_LIT:0.0> <EOL> weight = <NUM_LIT:1> <EOL> for prediction in predictions : <EOL> if weight_label is not None : <EOL> weight = prediction [ weight_label ] <EOL> final_confidence += weight * prediction [ '<STR_LIT>' ] <EOL> total_weight += weight <EOL> final_confidence = ( final_confidence / total_weight <EOL> if total_weight > <NUM_LIT:0> else float ( '<STR_LIT>' ) ) <EOL> return combined_prediction , final_confidence <EOL> def append ( self , prediction_info ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( prediction_info , dict ) and <EOL> '<STR_LIT>' in prediction_info ) : <EOL> order = self . next_order ( ) <EOL> prediction_info [ '<STR_LIT>' ] = order <EOL> self . predictions . append ( prediction_info ) <EOL> else : <EOL> LOGGER . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def single_out_category ( self , options ) : <EOL> """<STR_LIT>""" <EOL> if options is None or any ( option not in options for option in <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> length = len ( self . predictions ) <EOL> if options [ "<STR_LIT>" ] > length : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % length ) <EOL> if options [ "<STR_LIT>" ] < <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> category_predictions = [ ] <EOL> rest_of_predictions = [ ] <EOL> for prediction in self . predictions : <EOL> if prediction [ '<STR_LIT>' ] == options [ "<STR_LIT>" ] : <EOL> category_predictions . append ( prediction ) <EOL> else : <EOL> rest_of_predictions . append ( prediction ) <EOL> if len ( category_predictions ) >= options [ "<STR_LIT>" ] : <EOL> return MultiVote ( category_predictions ) <EOL> return MultiVote ( rest_of_predictions ) <EOL> def append_row ( self , prediction_row , <EOL> prediction_headers = PREDICTION_HEADERS ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( prediction_row , list ) and <EOL> isinstance ( prediction_headers , list ) and <EOL> len ( prediction_row ) == len ( prediction_headers ) and <EOL> '<STR_LIT>' in prediction_headers ) : <EOL> order = self . next_order ( ) <EOL> try : <EOL> index = prediction_headers . index ( '<STR_LIT>' ) <EOL> prediction_row [ index ] = order <EOL> except ValueError : <EOL> prediction_headers . append ( '<STR_LIT>' ) <EOL> prediction_row . append ( order ) <EOL> prediction_info = { } <EOL> for i in range ( <NUM_LIT:0> , len ( prediction_row ) ) : <EOL> prediction_info . update ( { prediction_headers [ i ] : <EOL> prediction_row [ i ] } ) <EOL> self . predictions . append ( prediction_info ) <EOL> else : <EOL> LOGGER . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def extend ( self , predictions_info ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( predictions_info , list ) : <EOL> order = self . next_order ( ) <EOL> for i in range ( <NUM_LIT:0> , len ( predictions_info ) ) : <EOL> prediction = predictions_info [ i ] <EOL> if isinstance ( prediction , dict ) : <EOL> prediction [ '<STR_LIT>' ] = order + i <EOL> self . append ( prediction ) <EOL> else : <EOL> LOGGER . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> LOGGER . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def extend_rows ( self , predictions_rows , <EOL> prediction_headers = PREDICTION_HEADERS ) : <EOL> """<STR_LIT>""" <EOL> order = self . next_order ( ) <EOL> try : <EOL> index = prediction_headers . index ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> index = len ( prediction_headers ) <EOL> prediction_headers . append ( '<STR_LIT>' ) <EOL> if isinstance ( predictions_rows , list ) : <EOL> for i in range ( <NUM_LIT:0> , len ( predictions_rows ) ) : <EOL> prediction = predictions_rows [ i ] <EOL> if isinstance ( prediction , list ) : <EOL> if index == len ( prediction ) : <EOL> prediction . append ( order + i ) <EOL> else : <EOL> prediction [ index ] = order + i <EOL> self . append_row ( prediction , prediction_headers ) <EOL> else : <EOL> LOGGER . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> LOGGER . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> NUMERICAL_COMBINATION_METHODS = { <EOL> PLURALITY : MultiVote . avg , <EOL> CONFIDENCE : MultiVote . error_weighted , <EOL> PROBABILITY : MultiVote . avg } </s>
