<s> from __future__ import division , print_function , absolute_import <EOL> from os . path import join , dirname <EOL> import numpy as np <EOL> from numpy . testing import assert_array_almost_equal , assert_equal , TestCase <EOL> from scipy . fftpack . realtransforms import dct , idct , dst , idst <EOL> MDATA = np . load ( join ( dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> X = [ MDATA [ '<STR_LIT>' % i ] for i in range ( <NUM_LIT:8> ) ] <EOL> Y = [ MDATA [ '<STR_LIT>' % i ] for i in range ( <NUM_LIT:8> ) ] <EOL> FFTWDATA_DOUBLE = np . load ( join ( dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> FFTWDATA_SINGLE = np . load ( join ( dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> FFTWDATA_SIZES = FFTWDATA_DOUBLE [ '<STR_LIT>' ] <EOL> def fftw_dct_ref ( type , size , dt ) : <EOL> x = np . linspace ( <NUM_LIT:0> , size - <NUM_LIT:1> , size ) . astype ( dt ) <EOL> dt = np . result_type ( np . float32 , dt ) <EOL> if dt == np . double : <EOL> data = FFTWDATA_DOUBLE <EOL> elif dt == np . float32 : <EOL> data = FFTWDATA_SINGLE <EOL> else : <EOL> raise ValueError ( ) <EOL> y = ( data [ '<STR_LIT>' % ( type , size ) ] ) . astype ( dt ) <EOL> return x , y , dt <EOL> def fftw_dst_ref ( type , size , dt ) : <EOL> x = np . linspace ( <NUM_LIT:0> , size - <NUM_LIT:1> , size ) . astype ( dt ) <EOL> dt = np . result_type ( np . float32 , dt ) <EOL> if dt == np . double : <EOL> data = FFTWDATA_DOUBLE <EOL> elif dt == np . float32 : <EOL> data = FFTWDATA_SINGLE <EOL> else : <EOL> raise ValueError ( ) <EOL> y = ( data [ '<STR_LIT>' % ( type , size ) ] ) . astype ( dt ) <EOL> return x , y , dt <EOL> class TestComplex ( TestCase ) : <EOL> def test_dct_complex64 ( self ) : <EOL> y = dct ( <NUM_LIT> * np . arange ( <NUM_LIT:5> , dtype = np . complex64 ) ) <EOL> x = <NUM_LIT> * dct ( np . arange ( <NUM_LIT:5> ) ) <EOL> assert_array_almost_equal ( x , y ) <EOL> def test_dct_complex ( self ) : <EOL> y = dct ( np . arange ( <NUM_LIT:5> ) * <NUM_LIT> ) <EOL> x = <NUM_LIT> * dct ( np . arange ( <NUM_LIT:5> ) ) <EOL> assert_array_almost_equal ( x , y ) <EOL> def test_idct_complex ( self ) : <EOL> y = idct ( np . arange ( <NUM_LIT:5> ) * <NUM_LIT> ) <EOL> x = <NUM_LIT> * idct ( np . arange ( <NUM_LIT:5> ) ) <EOL> assert_array_almost_equal ( x , y ) <EOL> def test_dst_complex64 ( self ) : <EOL> y = dst ( np . arange ( <NUM_LIT:5> , dtype = np . complex64 ) * <NUM_LIT> ) <EOL> x = <NUM_LIT> * dst ( np . arange ( <NUM_LIT:5> ) ) <EOL> assert_array_almost_equal ( x , y ) <EOL> def test_dst_complex ( self ) : <EOL> y = dst ( np . arange ( <NUM_LIT:5> ) * <NUM_LIT> ) <EOL> x = <NUM_LIT> * dst ( np . arange ( <NUM_LIT:5> ) ) <EOL> assert_array_almost_equal ( x , y ) <EOL> def test_idst_complex ( self ) : <EOL> y = idst ( np . arange ( <NUM_LIT:5> ) * <NUM_LIT> ) <EOL> x = <NUM_LIT> * idst ( np . arange ( <NUM_LIT:5> ) ) <EOL> assert_array_almost_equal ( x , y ) <EOL> class _TestDCTBase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = None <EOL> self . dec = <NUM_LIT> <EOL> self . type = None <EOL> def test_definition ( self ) : <EOL> for i in FFTWDATA_SIZES : <EOL> x , yr , dt = fftw_dct_ref ( self . type , i , self . rdt ) <EOL> y = dct ( x , type = self . type ) <EOL> assert_equal ( y . dtype , dt ) <EOL> assert_array_almost_equal ( y / np . max ( y ) , yr / np . max ( y ) , decimal = self . dec , <EOL> err_msg = "<STR_LIT>" % i ) <EOL> def test_axis ( self ) : <EOL> nt = <NUM_LIT:2> <EOL> for i in [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:64> ] : <EOL> x = np . random . randn ( nt , i ) <EOL> y = dct ( x , type = self . type ) <EOL> for j in range ( nt ) : <EOL> assert_array_almost_equal ( y [ j ] , dct ( x [ j ] , type = self . type ) , <EOL> decimal = self . dec ) <EOL> x = x . T <EOL> y = dct ( x , axis = <NUM_LIT:0> , type = self . type ) <EOL> for j in range ( nt ) : <EOL> assert_array_almost_equal ( y [ : , j ] , dct ( x [ : , j ] , type = self . type ) , <EOL> decimal = self . dec ) <EOL> class _TestDCTIIBase ( _TestDCTBase ) : <EOL> def test_definition_matlab ( self ) : <EOL> for i in range ( len ( X ) ) : <EOL> dt = np . result_type ( np . float32 , self . rdt ) <EOL> x = np . array ( X [ i ] , dtype = dt ) <EOL> yr = Y [ i ] <EOL> y = dct ( x , norm = "<STR_LIT>" , type = <NUM_LIT:2> ) <EOL> assert_equal ( y . dtype , dt ) <EOL> assert_array_almost_equal ( y , yr , decimal = self . dec ) <EOL> class _TestDCTIIIBase ( _TestDCTBase ) : <EOL> def test_definition_ortho ( self ) : <EOL> for i in range ( len ( X ) ) : <EOL> x = np . array ( X [ i ] , dtype = self . rdt ) <EOL> dt = np . result_type ( np . float32 , self . rdt ) <EOL> y = dct ( x , norm = '<STR_LIT>' , type = <NUM_LIT:2> ) <EOL> xi = dct ( y , norm = "<STR_LIT>" , type = <NUM_LIT:3> ) <EOL> assert_equal ( xi . dtype , dt ) <EOL> assert_array_almost_equal ( xi , x , decimal = self . dec ) <EOL> class TestDCTIDouble ( _TestDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT:10> <EOL> self . type = <NUM_LIT:1> <EOL> class TestDCTIFloat ( _TestDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:1> <EOL> class TestDCTIInt ( _TestDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:1> <EOL> class TestDCTIIDouble ( _TestDCTIIBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT:10> <EOL> self . type = <NUM_LIT:2> <EOL> class TestDCTIIFloat ( _TestDCTIIBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:2> <EOL> class TestDCTIIInt ( _TestDCTIIBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:2> <EOL> class TestDCTIIIDouble ( _TestDCTIIIBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT> <EOL> self . type = <NUM_LIT:3> <EOL> class TestDCTIIIFloat ( _TestDCTIIIBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:3> <EOL> class TestDCTIIIInt ( _TestDCTIIIBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:3> <EOL> class _TestIDCTBase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = None <EOL> self . dec = <NUM_LIT> <EOL> self . type = None <EOL> def test_definition ( self ) : <EOL> for i in FFTWDATA_SIZES : <EOL> xr , yr , dt = fftw_dct_ref ( self . type , i , self . rdt ) <EOL> x = idct ( yr , type = self . type ) <EOL> if self . type == <NUM_LIT:1> : <EOL> x /= <NUM_LIT:2> * ( i - <NUM_LIT:1> ) <EOL> else : <EOL> x /= <NUM_LIT:2> * i <EOL> assert_equal ( x . dtype , dt ) <EOL> assert_array_almost_equal ( x / np . max ( x ) , xr / np . max ( x ) , decimal = self . dec , <EOL> err_msg = "<STR_LIT>" % i ) <EOL> class TestIDCTIDouble ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT:10> <EOL> self . type = <NUM_LIT:1> <EOL> class TestIDCTIFloat ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:4> <EOL> self . type = <NUM_LIT:1> <EOL> class TestIDCTIInt ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:4> <EOL> self . type = <NUM_LIT:1> <EOL> class TestIDCTIIDouble ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT:10> <EOL> self . type = <NUM_LIT:2> <EOL> class TestIDCTIIFloat ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:2> <EOL> class TestIDCTIIInt ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:2> <EOL> class TestIDCTIIIDouble ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT> <EOL> self . type = <NUM_LIT:3> <EOL> class TestIDCTIIIFloat ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:3> <EOL> class TestIDCTIIIInt ( _TestIDCTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:3> <EOL> class _TestDSTBase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = None <EOL> self . dec = None <EOL> self . type = None <EOL> def test_definition ( self ) : <EOL> for i in FFTWDATA_SIZES : <EOL> xr , yr , dt = fftw_dst_ref ( self . type , i , self . rdt ) <EOL> y = dst ( xr , type = self . type ) <EOL> assert_equal ( y . dtype , dt ) <EOL> assert_array_almost_equal ( y / np . max ( y ) , yr / np . max ( y ) , decimal = self . dec , <EOL> err_msg = "<STR_LIT>" % i ) <EOL> class TestDSTIDouble ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT> <EOL> self . type = <NUM_LIT:1> <EOL> class TestDSTIFloat ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:1> <EOL> class TestDSTIInt ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:5> <EOL> self . type = <NUM_LIT:1> <EOL> class TestDSTIIDouble ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT> <EOL> self . type = <NUM_LIT:2> <EOL> class TestDSTIIFloat ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:6> <EOL> self . type = <NUM_LIT:2> <EOL> class TestDSTIIInt ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:6> <EOL> self . type = <NUM_LIT:2> <EOL> class TestDSTIIIDouble ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT> <EOL> self . type = <NUM_LIT:3> <EOL> class TestDSTIIIFloat ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:7> <EOL> self . type = <NUM_LIT:3> <EOL> class TestDSTIIIInt ( _TestDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:7> <EOL> self . type = <NUM_LIT:3> <EOL> class _TestIDSTBase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = None <EOL> self . dec = None <EOL> self . type = None <EOL> def test_definition ( self ) : <EOL> for i in FFTWDATA_SIZES : <EOL> xr , yr , dt = fftw_dst_ref ( self . type , i , self . rdt ) <EOL> x = idst ( yr , type = self . type ) <EOL> if self . type == <NUM_LIT:1> : <EOL> x /= <NUM_LIT:2> * ( i + <NUM_LIT:1> ) <EOL> else : <EOL> x /= <NUM_LIT:2> * i <EOL> assert_equal ( x . dtype , dt ) <EOL> assert_array_almost_equal ( x / np . max ( x ) , xr / np . max ( x ) , decimal = self . dec , <EOL> err_msg = "<STR_LIT>" % i ) <EOL> class TestIDSTIDouble ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT:12> <EOL> self . type = <NUM_LIT:1> <EOL> class TestIDSTIFloat ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:4> <EOL> self . type = <NUM_LIT:1> <EOL> class TestIDSTIInt ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:4> <EOL> self . type = <NUM_LIT:1> <EOL> class TestIDSTIIDouble ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT> <EOL> self . type = <NUM_LIT:2> <EOL> class TestIDSTIIFloat ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:6> <EOL> self . type = <NUM_LIT:2> <EOL> class TestIDSTIIInt ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:6> <EOL> self . type = <NUM_LIT:2> <EOL> class TestIDSTIIIDouble ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . double <EOL> self . dec = <NUM_LIT> <EOL> self . type = <NUM_LIT:3> <EOL> class TestIDSTIIIFloat ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = np . float32 <EOL> self . dec = <NUM_LIT:6> <EOL> self . type = <NUM_LIT:3> <EOL> class TestIDSTIIIInt ( _TestIDSTBase ) : <EOL> def setUp ( self ) : <EOL> self . rdt = int <EOL> self . dec = <NUM_LIT:6> <EOL> self . type = <NUM_LIT:3> <EOL> class TestOverwrite ( object ) : <EOL> """<STR_LIT>""" <EOL> real_dtypes = [ np . float32 , np . float64 ] <EOL> def _check ( self , x , routine , type , fftsize , axis , norm , overwrite_x , <EOL> should_overwrite , ** kw ) : <EOL> x2 = x . copy ( ) <EOL> routine ( x2 , type , fftsize , axis , norm , overwrite_x = overwrite_x ) <EOL> sig = "<STR_LIT>" % ( <EOL> routine . __name__ , x . dtype , x . shape , fftsize , axis , overwrite_x ) <EOL> if not should_overwrite : <EOL> assert_equal ( x2 , x , err_msg = "<STR_LIT>" % sig ) <EOL> def _check_1d ( self , routine , dtype , shape , axis , overwritable_dtypes ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> if np . issubdtype ( dtype , np . complexfloating ) : <EOL> data = np . random . randn ( * shape ) + <NUM_LIT> * np . random . randn ( * shape ) <EOL> else : <EOL> data = np . random . randn ( * shape ) <EOL> data = data . astype ( dtype ) <EOL> for type in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> for overwrite_x in [ True , False ] : <EOL> for norm in [ None , '<STR_LIT>' ] : <EOL> if type == <NUM_LIT:1> and norm == '<STR_LIT>' : <EOL> continue <EOL> should_overwrite = ( overwrite_x <EOL> and dtype in overwritable_dtypes <EOL> and ( len ( shape ) == <NUM_LIT:1> or <EOL> ( axis % len ( shape ) == len ( shape ) - <NUM_LIT:1> <EOL> ) ) ) <EOL> self . _check ( data , routine , type , None , axis , norm , <EOL> overwrite_x , should_overwrite ) <EOL> def test_dct ( self ) : <EOL> overwritable = self . real_dtypes <EOL> for dtype in self . real_dtypes : <EOL> self . _check_1d ( dct , dtype , ( <NUM_LIT:16> , ) , - <NUM_LIT:1> , overwritable ) <EOL> self . _check_1d ( dct , dtype , ( <NUM_LIT:16> , <NUM_LIT:2> ) , <NUM_LIT:0> , overwritable ) <EOL> self . _check_1d ( dct , dtype , ( <NUM_LIT:2> , <NUM_LIT:16> ) , <NUM_LIT:1> , overwritable ) <EOL> def test_idct ( self ) : <EOL> overwritable = self . real_dtypes <EOL> for dtype in self . real_dtypes : <EOL> self . _check_1d ( idct , dtype , ( <NUM_LIT:16> , ) , - <NUM_LIT:1> , overwritable ) <EOL> self . _check_1d ( idct , dtype , ( <NUM_LIT:16> , <NUM_LIT:2> ) , <NUM_LIT:0> , overwritable ) <EOL> self . _check_1d ( idct , dtype , ( <NUM_LIT:2> , <NUM_LIT:16> ) , <NUM_LIT:1> , overwritable ) <EOL> def test_dst ( self ) : <EOL> overwritable = self . real_dtypes <EOL> for dtype in self . real_dtypes : <EOL> self . _check_1d ( dst , dtype , ( <NUM_LIT:16> , ) , - <NUM_LIT:1> , overwritable ) <EOL> self . _check_1d ( dst , dtype , ( <NUM_LIT:16> , <NUM_LIT:2> ) , <NUM_LIT:0> , overwritable ) <EOL> self . _check_1d ( dst , dtype , ( <NUM_LIT:2> , <NUM_LIT:16> ) , <NUM_LIT:1> , overwritable ) <EOL> def test_idst ( self ) : <EOL> overwritable = self . real_dtypes <EOL> for dtype in self . real_dtypes : <EOL> self . _check_1d ( idst , dtype , ( <NUM_LIT:16> , ) , - <NUM_LIT:1> , overwritable ) <EOL> self . _check_1d ( idst , dtype , ( <NUM_LIT:16> , <NUM_LIT:2> ) , <NUM_LIT:0> , overwritable ) <EOL> self . _check_1d ( idst , dtype , ( <NUM_LIT:2> , <NUM_LIT:16> ) , <NUM_LIT:1> , overwritable ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> np . testing . run_module_suite ( ) </s>
