<s> from future . builtins import str <EOL> import mock <EOL> import types <EOL> from unittest import TestCase <EOL> from featureforge . evaluator import FeatureEvaluator , TolerantFeatureEvaluator <EOL> from featureforge . feature import make_feature , input_schema , output_schema <EOL> @ make_feature <EOL> @ input_schema ( dict ) <EOL> @ output_schema ( str ) <EOL> def DumbFeatureA ( data_point ) : <EOL> return u'<STR_LIT:a>' <EOL> @ make_feature <EOL> @ input_schema ( dict ) <EOL> @ output_schema ( object ) <EOL> def EntireSampleFeature ( data_point ) : <EOL> return data_point <EOL> @ make_feature <EOL> @ input_schema ( { '<STR_LIT:description>' : str } ) <EOL> @ output_schema ( str ) <EOL> def DescriptionFeature ( data_point ) : <EOL> return data_point [ '<STR_LIT:description>' ] <EOL> @ make_feature <EOL> @ input_schema ( { '<STR_LIT>' : int } ) <EOL> @ output_schema ( str ) <EOL> def AgeFeature ( data_point ) : <EOL> return data_point [ '<STR_LIT>' ] <EOL> @ make_feature <EOL> @ input_schema ( { '<STR_LIT:description>' : str } ) <EOL> @ output_schema ( str ) <EOL> def BrokenFeature ( data_point ) : <EOL> raise RuntimeError ( ) <EOL> SAMPLES = [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:description>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:description>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT:description>' : u'<STR_LIT>' , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT:description>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT:description>' : u'<STR_LIT>' } <EOL> ] <EOL> class SimpleEvaluatorTests ( TestCase ) : <EOL> def test_fit_creates_alive_features_tuple ( self ) : <EOL> ev = FeatureEvaluator ( [ DumbFeatureA ] ) <EOL> self . assertFalse ( hasattr ( ev , '<STR_LIT>' ) ) <EOL> ev . fit ( [ ] ) <EOL> self . assertTrue ( hasattr ( ev , '<STR_LIT>' ) ) <EOL> def test_returns_generator ( self ) : <EOL> ev = FeatureEvaluator ( [ DumbFeatureA ] ) <EOL> ev . fit ( [ ] ) <EOL> Xt = ev . transform ( [ ] ) <EOL> self . assertIsInstance ( Xt , types . GeneratorType ) <EOL> def test_returns_tuples_of_features_length ( self ) : <EOL> features = [ DumbFeatureA , DumbFeatureA ] <EOL> ev = FeatureEvaluator ( features ) <EOL> ev . fit ( SAMPLES ) <EOL> Xt = ev . transform ( SAMPLES ) <EOL> x = next ( Xt ) <EOL> self . assertIsInstance ( x , tuple ) <EOL> self . assertEqual ( len ( x ) , len ( features ) ) <EOL> def test_returns_as_many_tuples_as_samples ( self ) : <EOL> ev = FeatureEvaluator ( [ DumbFeatureA ] ) <EOL> ev . fit ( SAMPLES ) <EOL> Xt = ev . transform ( SAMPLES ) <EOL> self . assertEqual ( len ( list ( Xt ) ) , len ( SAMPLES ) ) <EOL> def test_fit_transform_does_both_things ( self ) : <EOL> ev = FeatureEvaluator ( [ DumbFeatureA ] ) <EOL> Xt_1 = ev . fit_transform ( SAMPLES ) <EOL> Xt_2 = ev . transform ( SAMPLES ) <EOL> self . assertListEqual ( list ( Xt_1 ) , list ( Xt_2 ) ) <EOL> class TolerantFittingCases ( object ) : <EOL> fit_method_name = '<STR_LIT>' <EOL> def apply_fit ( self , * args , ** kwargs ) : <EOL> method = getattr ( self . ev , self . fit_method_name ) <EOL> return method ( * args , ** kwargs ) <EOL> def test_once_fitted_says_fitted ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DumbFeatureA ] ) <EOL> self . assertFalse ( self . ev . fitted ) <EOL> self . apply_fit ( [ ] ) <EOL> self . assertTrue ( self . ev . fitted ) <EOL> def test_fit_creates_alive_features_tuple ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DumbFeatureA ] ) <EOL> self . assertFalse ( hasattr ( self . ev , '<STR_LIT>' ) ) <EOL> self . apply_fit ( [ ] ) <EOL> self . assertTrue ( hasattr ( self . ev , '<STR_LIT>' ) ) <EOL> def test_feature_is_excluded_if_fails_on_firts_M_samples ( self ) : <EOL> actual_feature = DescriptionFeature <EOL> description_feature = mock . Mock ( wraps = actual_feature , <EOL> spec = actual_feature ) <EOL> self . ev = TolerantFeatureEvaluator ( [ description_feature , DumbFeatureA ] ) <EOL> self . ev . FEATURE_STRICT_UNTIL = <NUM_LIT:2> <EOL> samples = SAMPLES [ : ] <EOL> samples . insert ( <NUM_LIT:0> , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> self . apply_fit ( samples ) <EOL> self . assertNotIn ( description_feature , self . ev . alive_features ) <EOL> self . assertEqual ( description_feature . call_count , <NUM_LIT:1> ) <EOL> def test_feature_is_excluded_after_K_fails_no_matter_when ( self ) : <EOL> actual_feature = BrokenFeature <EOL> broken_feature = mock . Mock ( wraps = actual_feature , spec = actual_feature ) <EOL> self . ev = TolerantFeatureEvaluator ( [ broken_feature , DumbFeatureA ] ) <EOL> self . ev . FEATURE_STRICT_UNTIL = <NUM_LIT:0> <EOL> self . ev . FEATURE_MAX_ERRORS_ALLOWED = <NUM_LIT:2> <EOL> self . apply_fit ( SAMPLES [ : ] ) <EOL> self . assertNotIn ( broken_feature , self . ev . alive_features ) <EOL> self . assertEqual ( broken_feature . call_count , <EOL> self . ev . FEATURE_MAX_ERRORS_ALLOWED + <NUM_LIT:1> ) <EOL> def test_if_no_more_features_then_blows_up ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ BrokenFeature ] ) <EOL> self . ev . FEATURE_STRICT_UNTIL = <NUM_LIT:2> <EOL> with self . assertRaises ( self . ev . NoFeaturesLeftError ) : <EOL> self . apply_fit ( SAMPLES [ : ] ) <EOL> def test_alive_features_can_hide_bad_samples ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DescriptionFeature , AgeFeature , <EOL> DumbFeatureA ] ) <EOL> self . ev . FEATURE_STRICT_UNTIL = <NUM_LIT:0> <EOL> self . ev . FEATURE_MAX_ERRORS_ALLOWED = <NUM_LIT:1> <EOL> no_age = { '<STR_LIT:description>' : u'<STR_LIT>' } <EOL> bad_sample = { '<STR_LIT>' : u'<STR_LIT>' } <EOL> samples = [ bad_sample , no_age ] <EOL> self . apply_fit ( samples ) <EOL> assert DescriptionFeature in self . ev . alive_features <EOL> self . assertIn ( AgeFeature , self . ev . alive_features ) <EOL> def test_when_feature_is_excluded_discarded_sample_is_reconsidered ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DescriptionFeature , AgeFeature , <EOL> DumbFeatureA ] ) <EOL> self . ev . FEATURE_MAX_ERRORS_ALLOWED = <NUM_LIT:0> <EOL> bad_sample = { '<STR_LIT>' : u'<STR_LIT>' } <EOL> samples = [ bad_sample ] <EOL> self . apply_fit ( samples ) <EOL> self . assertEqual ( self . ev . alive_features , ( DumbFeatureA , ) ) <EOL> class TolerantEvaluatorFitTests ( TestCase , TolerantFittingCases ) : <EOL> fit_method_name = '<STR_LIT>' <EOL> def test_returns_itself ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DumbFeatureA ] ) <EOL> self . assertEqual ( self . ev . fit ( [ ] ) , self . ev ) <EOL> class TolerantEvaluatorFitTransformTests ( TestCase , TolerantFittingCases ) : <EOL> fit_method_name = '<STR_LIT>' <EOL> def test_sample_is_excluded_if_any_feature_fails_when_evaluating_it ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DescriptionFeature , <EOL> EntireSampleFeature ] ) <EOL> self . ev . FEATURE_STRICT_UNTIL = <NUM_LIT:0> <EOL> self . ev . FEATURE_MAX_ERRORS_ALLOWED = len ( SAMPLES ) + <NUM_LIT:1> <EOL> samples = SAMPLES [ : ] <EOL> nodescription = { '<STR_LIT>' : u'<STR_LIT>' } <EOL> samples . append ( nodescription ) <EOL> result = self . ev . fit_transform ( samples ) <EOL> self . assertTrue ( len ( list ( result ) ) < len ( samples ) ) <EOL> self . assertNotIn ( nodescription , [ r [ - <NUM_LIT:1> ] for r in result ] ) <EOL> def test_if_a_feature_is_excluded_all_results_doesnt_include_it ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DescriptionFeature , DumbFeatureA ] ) <EOL> self . ev . FEATURE_STRICT_UNTIL = <NUM_LIT:0> <EOL> self . ev . FEATURE_MAX_ERRORS_ALLOWED = <NUM_LIT:0> <EOL> result = self . ev . fit_transform ( SAMPLES + [ { '<STR_LIT>' : u'<STR_LIT>' } ] ) <EOL> self . assertTrue ( result ) <EOL> for r in result : <EOL> self . assertEqual ( len ( r ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( r [ <NUM_LIT:0> ] , '<STR_LIT:a>' ) <EOL> def test_when_feature_is_excluded_discarded_samples_are_reevaluated ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ DescriptionFeature , DumbFeatureA , <EOL> EntireSampleFeature ] ) <EOL> self . ev . FEATURE_MAX_ERRORS_ALLOWED = <NUM_LIT:0> <EOL> samples = SAMPLES [ : ] <EOL> nodescription = { '<STR_LIT>' : u'<STR_LIT>' } <EOL> samples . append ( nodescription ) <EOL> result = list ( self . ev . fit_transform ( samples ) ) <EOL> self . assertEqual ( len ( samples ) , len ( result ) ) <EOL> self . assertIn ( nodescription , [ r [ - <NUM_LIT:1> ] for r in result ] ) <EOL> def test_consumable_is_consumed_only_once ( self ) : <EOL> samples = ( s for s in SAMPLES ) <EOL> self . ev = TolerantFeatureEvaluator ( [ EntireSampleFeature ] ) <EOL> result = list ( self . ev . fit_transform ( samples ) ) <EOL> self . assertEqual ( len ( SAMPLES ) , len ( result ) ) <EOL> class TolerantEvaluatorTransformTests ( TestCase ) : <EOL> def test_errors_are_raised ( self ) : <EOL> self . ev = TolerantFeatureEvaluator ( [ BrokenFeature , DumbFeatureA ] ) <EOL> self . ev . fit ( [ ] ) <EOL> def transform ( ) : <EOL> list ( self . ev . transform ( SAMPLES ) ) <EOL> self . assertRaises ( RuntimeError , transform ) </s>
