<s> import unittest <EOL> import numpy as np <EOL> import scipy . sparse as sp <EOL> from multimodal . lib . array_utils import normalize_features <EOL> from multimodal . evaluation import ( evaluate_label_reco , <EOL> evaluate_NN_label , <EOL> chose_examples ) <EOL> class TestLabelEvaluation ( unittest . TestCase ) : <EOL> def test ( self ) : <EOL> labels = [ <NUM_LIT:2> , <NUM_LIT:0> ] <EOL> reco = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> good = evaluate_label_reco ( reco , labels ) <EOL> self . assertEqual ( good , <NUM_LIT:1.> ) <EOL> bad = evaluate_label_reco ( reco [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , : ] , labels ) <EOL> self . assertEqual ( bad , <NUM_LIT:0.> ) <EOL> medium = evaluate_label_reco ( reco [ [ <NUM_LIT:1> , <NUM_LIT:1> ] , : ] , labels ) <EOL> self . assertEqual ( medium , <NUM_LIT> ) <EOL> def test_fails_on_multiple_labels ( self ) : <EOL> labels = [ [ <NUM_LIT:2> ] , [ <NUM_LIT:0> ] ] <EOL> reco = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> evaluate_label_reco ( reco , labels ) <EOL> class TestNNEvaluation ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . labels_a = np . random . randint ( <NUM_LIT:10> , size = <NUM_LIT> ) <EOL> self . labels_b = [ i for i in reversed ( range ( <NUM_LIT:10> ) ) ] <EOL> self . a = np . random . random ( ( <NUM_LIT> , <NUM_LIT:5> ) ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> self . a [ i , <NUM_LIT:2> ] = self . labels_a [ i ] <EOL> self . b = np . random . random ( ( <NUM_LIT:10> , <NUM_LIT:5> ) ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> self . b [ i , <NUM_LIT:3> ] = self . labels_b [ i ] <EOL> def fake_metrics ( self , a , b , axis = - <NUM_LIT:1> ) : <EOL> assert ( axis == - <NUM_LIT:1> ) <EOL> return <NUM_LIT:1.> - ( a [ : , : , <NUM_LIT:2> ] == b [ : , : , <NUM_LIT:3> ] ) <EOL> def test_good_on_fake_measure ( self ) : <EOL> self . assertEqual ( evaluate_NN_label ( self . a , self . b , self . labels_a , <EOL> self . labels_b , self . fake_metrics <EOL> ) , <NUM_LIT:1.> ) <EOL> def test_bad_on_fake_measure ( self ) : <EOL> self . assertEqual ( evaluate_NN_label ( self . a , <NUM_LIT:1> + self . b , <EOL> self . labels_a , self . labels_b , <EOL> self . fake_metrics ) , <NUM_LIT:0.> ) <EOL> def test_on_fake_measure_sparse ( self ) : <EOL> a = sp . lil_matrix ( self . a ) . tocsr ( ) <EOL> b = sp . lil_matrix ( self . b ) . tocsr ( ) <EOL> self . assertEqual ( <EOL> evaluate_NN_label ( a , b , self . labels_a , self . labels_b , <EOL> self . fake_metrics ) , <EOL> <NUM_LIT:1.> ) <EOL> self . assertEqual ( <EOL> evaluate_NN_label ( a , <NUM_LIT:1> + self . b , self . labels_a , <EOL> self . labels_b , self . fake_metrics ) , <EOL> <NUM_LIT:0.> ) <EOL> class TestChoseExamples ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . label_set = list ( range ( <NUM_LIT:3> ) ) <EOL> self . labels = self . label_set * <NUM_LIT:5> <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> np . random . shuffle ( self . labels ) <EOL> def test_choses_as_many_examples_as_labels ( self ) : <EOL> r = chose_examples ( self . labels , self . label_set ) <EOL> self . assertEqual ( len ( r ) , len ( self . label_set ) ) <EOL> r = chose_examples ( self . labels ) <EOL> self . assertEqual ( len ( r ) , len ( self . label_set ) ) <EOL> def test_choses_twice_as_many_examples_as_labels ( self ) : <EOL> r = chose_examples ( self . labels , self . label_set , number = <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( r ) , <NUM_LIT:2> * len ( self . label_set ) ) <EOL> def test_all_chosen_are_indices ( self ) : <EOL> r = chose_examples ( self . labels , self . label_set , number = <NUM_LIT:2> ) <EOL> assert ( all ( [ <NUM_LIT:0> <= i < len ( self . labels ) for i in r ] ) ) <EOL> def test_all_labels_are_chosen_once ( self ) : <EOL> r = chose_examples ( self . labels , self . label_set ) <EOL> lab = [ self . labels [ i ] for i in r ] <EOL> assert ( all ( [ lab . count ( l ) == <NUM_LIT:1> for l in self . label_set ] ) ) <EOL> def test_all_labels_are_chosen_twice ( self ) : <EOL> r = chose_examples ( self . labels , self . label_set , number = <NUM_LIT:2> ) <EOL> lab = [ self . labels [ i ] for i in r ] <EOL> assert ( all ( [ lab . count ( l ) == <NUM_LIT:2> for l in self . label_set ] ) ) <EOL> class TestNormalizeFeatures ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mat = np . random . random ( ( <NUM_LIT:32> , <NUM_LIT> ) ) <EOL> self . mat = <NUM_LIT> * self . mat * ( self . mat < <NUM_LIT> ) <EOL> def test_on_sparse_same_shape ( self ) : <EOL> m = self . mat <EOL> m [ <NUM_LIT:0> , : ] += <NUM_LIT:1> <EOL> m = sp . csc_matrix ( m ) <EOL> norm = normalize_features ( m ) <EOL> assert ( np . allclose ( norm . sum ( axis = <NUM_LIT:0> ) , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( norm . shape , m . shape ) <EOL> def test_removes_columns_sparse ( self ) : <EOL> m = self . mat <EOL> m [ <NUM_LIT:0> , : ] += <NUM_LIT:1> <EOL> m [ : , [ <NUM_LIT:1> , <NUM_LIT:3> ] ] = <NUM_LIT:0> <EOL> m = sp . csc_matrix ( m ) <EOL> norm = normalize_features ( m ) <EOL> assert ( np . allclose ( norm . sum ( axis = <NUM_LIT:0> ) , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( norm . shape , ( m . shape [ <NUM_LIT:0> ] , m . shape [ <NUM_LIT:1> ] - <NUM_LIT:2> ) ) <EOL> def test_on_dense_same_shape ( self ) : <EOL> m = self . mat <EOL> m [ <NUM_LIT:0> , : ] += <NUM_LIT:1> <EOL> norm = normalize_features ( m ) <EOL> assert ( np . allclose ( norm . sum ( axis = <NUM_LIT:0> ) , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( norm . shape , m . shape ) <EOL> def test_removes_columns_dense ( self ) : <EOL> m = self . mat <EOL> m [ <NUM_LIT:0> , : ] += <NUM_LIT:1> <EOL> m [ : , [ <NUM_LIT:1> , <NUM_LIT:3> ] ] = <NUM_LIT:0> <EOL> norm = normalize_features ( m ) <EOL> assert ( np . allclose ( norm . sum ( axis = <NUM_LIT:0> ) , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( norm . shape , ( m . shape [ <NUM_LIT:0> ] , m . shape [ <NUM_LIT:1> ] - <NUM_LIT:2> ) ) <EOL> def test_same_on_dense_and_sparse ( self ) : <EOL> m1 = sp . csc_matrix ( self . mat ) <EOL> m2 = sp . csr_matrix ( self . mat ) <EOL> n = normalize_features ( self . mat ) <EOL> n1 = normalize_features ( m1 ) <EOL> n2 = normalize_features ( m2 ) <EOL> assert ( np . allclose ( n1 . todense ( ) , n ) ) <EOL> assert ( np . allclose ( n2 . todense ( ) , n ) ) <EOL> def test_does_not_modify ( self ) : <EOL> m = self . mat . copy ( ) <EOL> normalize_features ( m ) <EOL> ms = sp . csr_matrix ( m ) <EOL> normalize_features ( ms ) <EOL> assert ( np . allclose ( m , self . mat ) ) <EOL> assert ( np . allclose ( ms . todense ( ) , self . mat ) ) <EOL> def test_OK ( self ) : <EOL> n = normalize_features ( np . array ( [ [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> ] ] ) ) <EOL> ok = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert ( np . allclose ( n , ok ) ) </s>
