<s> from nose . tools import raises <EOL> import numpy as np <EOL> from numpy . testing import assert_equal , assert_allclose <EOL> from menpo . math import ( dot_inplace_left , dot_inplace_right , as_matrix , <EOL> from_matrix ) <EOL> from menpo . image import MaskedImage <EOL> n_big = <NUM_LIT> <EOL> k = <NUM_LIT:100> <EOL> n_small = <NUM_LIT> <EOL> n_images = <NUM_LIT:5> <EOL> image_shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> mask = np . zeros ( image_shape , dtype = np . bool ) <EOL> mask [ : <NUM_LIT:2> ] = True <EOL> template = MaskedImage . init_blank ( image_shape , mask = mask ) <EOL> matrix = np . random . random ( [ n_images , <NUM_LIT:20> ] ) <EOL> a_l = np . random . rand ( n_big , k ) <EOL> b_l = np . random . rand ( k , n_small ) <EOL> a_r = np . ascontiguousarray ( b_l . T ) <EOL> b_r = np . ascontiguousarray ( a_l . T ) <EOL> gt_l = a_l . dot ( b_l ) <EOL> gt_r = a_r . dot ( b_r ) <EOL> def test_dot_inplace_left ( ) : <EOL> a_l_tmp = a_l . copy ( ) <EOL> b_l_tmp = b_l . copy ( ) <EOL> left_result = dot_inplace_left ( a_l_tmp , b_l_tmp ) <EOL> assert_allclose ( left_result , gt_l ) <EOL> assert_equal ( left_result , a_l_tmp [ : , : n_small ] ) <EOL> assert_equal ( b_l_tmp , b_l ) <EOL> def test_dot_inplace_right ( ) : <EOL> a_r_tmp = a_r . copy ( ) <EOL> b_r_tmp = b_r . copy ( ) <EOL> right_result = dot_inplace_right ( a_r_tmp , b_r_tmp ) <EOL> assert_allclose ( right_result , gt_r ) <EOL> assert_equal ( right_result , b_r_tmp [ : n_small ] ) <EOL> assert_equal ( a_r_tmp , a_r ) <EOL> @ raises ( ValueError ) <EOL> def test_dot_inplace_left_n_small_too_big_raises_value_error ( ) : <EOL> a = np . zeros ( ( <NUM_LIT> , <NUM_LIT:100> ) ) <EOL> b = np . zeros ( ( <NUM_LIT:100> , <NUM_LIT> ) ) <EOL> dot_inplace_left ( a , b ) <EOL> @ raises ( ValueError ) <EOL> def test_dot_inplace_right_n_small_too_big_raises_value_error ( ) : <EOL> a = np . zeros ( ( <NUM_LIT> , <NUM_LIT:100> ) ) <EOL> b = np . zeros ( ( <NUM_LIT:100> , <NUM_LIT> ) ) <EOL> dot_inplace_right ( a , b ) <EOL> def test_as_matrix_list ( ) : <EOL> data = as_matrix ( [ template . copy ( ) for _ in range ( n_images ) ] ) <EOL> assert_equal ( data . shape , ( n_images , <NUM_LIT:20> ) ) <EOL> def test_as_matrix_generator ( ) : <EOL> data = as_matrix ( ( template . copy ( ) for _ in range ( n_images ) ) , <EOL> length = n_images ) <EOL> assert_equal ( data . shape , ( n_images , <NUM_LIT:20> ) ) <EOL> def test_as_matrix_short_length ( ) : <EOL> data = as_matrix ( ( template . copy ( ) for _ in range ( n_images ) ) , length = <NUM_LIT:1> ) <EOL> assert_equal ( data . shape , ( <NUM_LIT:1> , <NUM_LIT:20> ) ) <EOL> @ raises ( ValueError ) <EOL> def test_as_matrix_long_length_raises_value_error ( ) : <EOL> as_matrix ( ( template . copy ( ) for _ in range ( <NUM_LIT:4> ) ) , length = <NUM_LIT:5> ) <EOL> def test_as_matrix_return_template ( ) : <EOL> data , t = as_matrix ( ( template . copy ( ) for _ in range ( n_images ) ) , <EOL> length = <NUM_LIT:1> , return_template = True ) <EOL> assert_equal ( data . shape , ( <NUM_LIT:1> , <NUM_LIT:20> ) ) <EOL> assert_equal ( t . shape , image_shape ) <EOL> def test_from_matrix ( ) : <EOL> images = from_matrix ( matrix , template ) <EOL> assert isinstance ( next ( images ) , MaskedImage ) </s>
