<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from scipy . stats import multivariate_normal <EOL> from numpy . testing import assert_array_almost_equal <EOL> import nmrglue as ng <EOL> from nmrglue . analysis . integration import integrate , ndintegrate <EOL> def _build_1d_ppm_scale ( ) : <EOL> n = <NUM_LIT> <EOL> sw = <NUM_LIT:1> / <NUM_LIT> <EOL> obs = <NUM_LIT:100> <EOL> dx = ( sw / n ) / obs <EOL> ppm_scale = np . arange ( dx , dx * n , dx ) <EOL> return ppm_scale [ : : - <NUM_LIT:1> ] <EOL> def test_1d_integrate ( ) : <EOL> """<STR_LIT>""" <EOL> ppm_scale = _build_1d_ppm_scale ( ) <EOL> uc = ng . fileio . fileiobase . uc_from_freqscale ( ppm_scale , <NUM_LIT:100> ) <EOL> data = ( multivariate_normal . pdf ( ppm_scale , mean = <NUM_LIT:5> , cov = <NUM_LIT> ) + <EOL> multivariate_normal . pdf ( ppm_scale , mean = <NUM_LIT:8> , cov = <NUM_LIT> ) * <NUM_LIT> ) <EOL> assert integrate ( data , uc , ( <NUM_LIT:4> , <NUM_LIT:6> ) ) - <NUM_LIT:1.0> <= np . finfo ( float ) . eps <EOL> assert integrate ( data , uc , ( <NUM_LIT:7> , <NUM_LIT:9> ) ) - <NUM_LIT> <= np . finfo ( float ) . eps <EOL> limits = [ ( <NUM_LIT:4> , <NUM_LIT:6> ) , ( <NUM_LIT:7> , <NUM_LIT:9> ) ] <EOL> values_1 = integrate ( data , uc , limits ) <EOL> assert_array_almost_equal ( values_1 , [ <NUM_LIT:1.> , <NUM_LIT> ] ) <EOL> values_2 = integrate ( data , uc , limits , norm_to_range = <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( values_2 , [ <NUM_LIT:0.5> , <NUM_LIT:1.> ] ) <EOL> values_2 = integrate ( data , uc , limits , norm_to_range = <NUM_LIT:1> , calibrate = <NUM_LIT:3> ) <EOL> assert_array_almost_equal ( values_2 , [ <NUM_LIT:0.5> * <NUM_LIT:3> , <NUM_LIT> ] ) <EOL> def test_1d_integrate_withnoise ( ) : <EOL> """<STR_LIT>""" <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> ppm_scale = _build_1d_ppm_scale ( ) <EOL> uc = ng . fileio . fileiobase . uc_from_freqscale ( ppm_scale , <NUM_LIT:100> ) <EOL> data = ( multivariate_normal . pdf ( ppm_scale , mean = <NUM_LIT:5> , cov = <NUM_LIT> ) + <EOL> multivariate_normal . pdf ( ppm_scale , mean = <NUM_LIT:8> , cov = <NUM_LIT> ) * <NUM_LIT:2> ) <EOL> noise = np . random . randn ( * data . shape ) * <NUM_LIT> <EOL> data = data + noise <EOL> limits = ( <NUM_LIT> , <NUM_LIT> ) <EOL> n = abs ( uc ( <NUM_LIT> , '<STR_LIT>' ) - uc ( <NUM_LIT> , '<STR_LIT>' ) ) <EOL> max_error = np . std ( noise ) * np . sqrt ( n ) <EOL> assert abs ( integrate ( data , uc , limits ) - <NUM_LIT:1> ) <= max_error <EOL> resutls = integrate ( data , uc , ( ( <NUM_LIT:4> , <NUM_LIT:6> ) , ( <NUM_LIT:7> , <NUM_LIT:9> ) ) , noise_limits = ( <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> norm_to_range = <NUM_LIT:1> ) <EOL> assert abs ( resutls [ <NUM_LIT:0> , <NUM_LIT:0> ] - <NUM_LIT:0.5> ) <= max_error <EOL> def test_1d_ndintegrate ( ) : <EOL> """<STR_LIT>""" <EOL> ppm_scale = _build_1d_ppm_scale ( ) <EOL> uc = ng . fileio . fileiobase . uc_from_freqscale ( ppm_scale , <NUM_LIT:100> ) <EOL> data = ( multivariate_normal . pdf ( ppm_scale , mean = <NUM_LIT:5> , cov = <NUM_LIT> ) + <EOL> multivariate_normal . pdf ( ppm_scale , mean = <NUM_LIT:8> , cov = <NUM_LIT> ) * <NUM_LIT> ) <EOL> assert abs ( ndintegrate ( data , uc , ( <NUM_LIT:4> , <NUM_LIT:6> ) ) - <NUM_LIT:1.0> ) <= <NUM_LIT> <EOL> assert abs ( ndintegrate ( data , uc , [ ( <NUM_LIT:7> , <NUM_LIT:9> ) , ] ) - <NUM_LIT> ) <= <NUM_LIT> <EOL> def test_2d_ndintegrate ( ) : <EOL> """<STR_LIT>""" <EOL> ppm_scale = _build_1d_ppm_scale ( ) <EOL> uc = ng . fileio . fileiobase . uc_from_freqscale ( ppm_scale , <NUM_LIT:100> ) <EOL> x , y = np . meshgrid ( ppm_scale , ppm_scale ) <EOL> pos = np . empty ( x . shape + ( <NUM_LIT:2> , ) ) <EOL> pos [ : , : , <NUM_LIT:0> ] = x <EOL> pos [ : , : , <NUM_LIT:1> ] = y <EOL> rv = multivariate_normal ( [ <NUM_LIT:5> , <NUM_LIT:8> ] , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> data = rv . pdf ( pos ) <EOL> limits = ( ( <NUM_LIT:4> , <NUM_LIT:6> ) , ( <NUM_LIT:7> , <NUM_LIT:9> ) ) <EOL> assert abs ( ndintegrate ( data , [ uc , uc ] , limits ) - <NUM_LIT:1.0> ) <= <NUM_LIT> </s>
