<s> from __future__ import division , print_function , absolute_import <EOL> import copy <EOL> import numpy as np <EOL> from numpy . testing import ( TestCase , run_module_suite , assert_equal , <EOL> assert_array_equal , assert_ ) <EOL> from scipy . signal . _peak_finding import ( argrelmax , argrelmin , <EOL> find_peaks_cwt , _identify_ridge_lines ) <EOL> from scipy . _lib . six import xrange <EOL> def _gen_gaussians ( center_locs , sigmas , total_length ) : <EOL> xdata = np . arange ( <NUM_LIT:0> , total_length ) . astype ( float ) <EOL> out_data = np . zeros ( total_length , dtype = float ) <EOL> for ind , sigma in enumerate ( sigmas ) : <EOL> tmp = ( xdata - center_locs [ ind ] ) / sigma <EOL> out_data += np . exp ( - ( tmp ** <NUM_LIT:2> ) ) <EOL> return out_data <EOL> def _gen_gaussians_even ( sigmas , total_length ) : <EOL> num_peaks = len ( sigmas ) <EOL> delta = total_length / ( num_peaks + <NUM_LIT:1> ) <EOL> center_locs = np . linspace ( delta , total_length - delta , num = num_peaks ) . astype ( int ) <EOL> out_data = _gen_gaussians ( center_locs , sigmas , total_length ) <EOL> return out_data , center_locs <EOL> def _gen_ridge_line ( start_locs , max_locs , length , distances , gaps ) : <EOL> """<STR_LIT>""" <EOL> def keep_bounds ( num , max_val ) : <EOL> out = max ( num , <NUM_LIT:0> ) <EOL> out = min ( out , max_val ) <EOL> return out <EOL> gaps = copy . deepcopy ( gaps ) <EOL> distances = copy . deepcopy ( distances ) <EOL> locs = np . zeros ( [ length , <NUM_LIT:2> ] , dtype = int ) <EOL> locs [ <NUM_LIT:0> , : ] = start_locs <EOL> total_length = max_locs [ <NUM_LIT:0> ] - start_locs [ <NUM_LIT:0> ] - sum ( gaps ) <EOL> if total_length < length : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> dist_int = length / len ( distances ) - <NUM_LIT:1> <EOL> gap_int = length / len ( gaps ) - <NUM_LIT:1> <EOL> for ind in xrange ( <NUM_LIT:1> , length ) : <EOL> nextcol = locs [ ind - <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> nextrow = locs [ ind - <NUM_LIT:1> , <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> if ( ind % dist_int == <NUM_LIT:0> ) and ( len ( distances ) > <NUM_LIT:0> ) : <EOL> nextcol += ( ( - <NUM_LIT:1> ) ** ind ) * distances . pop ( ) <EOL> if ( ind % gap_int == <NUM_LIT:0> ) and ( len ( gaps ) > <NUM_LIT:0> ) : <EOL> nextrow += gaps . pop ( ) <EOL> nextrow = keep_bounds ( nextrow , max_locs [ <NUM_LIT:0> ] ) <EOL> nextcol = keep_bounds ( nextcol , max_locs [ <NUM_LIT:1> ] ) <EOL> locs [ ind , : ] = [ nextrow , nextcol ] <EOL> return [ locs [ : , <NUM_LIT:0> ] , locs [ : , <NUM_LIT:1> ] ] <EOL> class TestRidgeLines ( TestCase ) : <EOL> def test_empty ( self ) : <EOL> test_matr = np . zeros ( [ <NUM_LIT:20> , <NUM_LIT:100> ] ) <EOL> lines = _identify_ridge_lines ( test_matr , <NUM_LIT:2> * np . ones ( <NUM_LIT:20> ) , <NUM_LIT:1> ) <EOL> assert_ ( len ( lines ) == <NUM_LIT:0> ) <EOL> def test_minimal ( self ) : <EOL> test_matr = np . zeros ( [ <NUM_LIT:20> , <NUM_LIT:100> ] ) <EOL> test_matr [ <NUM_LIT:0> , <NUM_LIT:10> ] = <NUM_LIT:1> <EOL> lines = _identify_ridge_lines ( test_matr , <NUM_LIT:2> * np . ones ( <NUM_LIT:20> ) , <NUM_LIT:1> ) <EOL> assert_ ( len ( lines ) == <NUM_LIT:1> ) <EOL> test_matr = np . zeros ( [ <NUM_LIT:20> , <NUM_LIT:100> ] ) <EOL> test_matr [ <NUM_LIT:0> : <NUM_LIT:2> , <NUM_LIT:10> ] = <NUM_LIT:1> <EOL> lines = _identify_ridge_lines ( test_matr , <NUM_LIT:2> * np . ones ( <NUM_LIT:20> ) , <NUM_LIT:1> ) <EOL> assert_ ( len ( lines ) == <NUM_LIT:1> ) <EOL> def test_single_pass ( self ) : <EOL> distances = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:5> ] <EOL> gaps = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> test_matr = np . zeros ( [ <NUM_LIT:20> , <NUM_LIT:50> ] ) + <NUM_LIT> <EOL> length = <NUM_LIT:12> <EOL> line = _gen_ridge_line ( [ <NUM_LIT:0> , <NUM_LIT> ] , test_matr . shape , length , distances , gaps ) <EOL> test_matr [ line [ <NUM_LIT:0> ] , line [ <NUM_LIT:1> ] ] = <NUM_LIT:1> <EOL> max_distances = max ( distances ) * np . ones ( <NUM_LIT:20> ) <EOL> identified_lines = _identify_ridge_lines ( test_matr , max_distances , max ( gaps ) + <NUM_LIT:1> ) <EOL> assert_array_equal ( identified_lines , [ line ] ) <EOL> def test_single_bigdist ( self ) : <EOL> distances = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:5> ] <EOL> gaps = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> ] <EOL> test_matr = np . zeros ( [ <NUM_LIT:20> , <NUM_LIT:50> ] ) <EOL> length = <NUM_LIT:12> <EOL> line = _gen_ridge_line ( [ <NUM_LIT:0> , <NUM_LIT> ] , test_matr . shape , length , distances , gaps ) <EOL> test_matr [ line [ <NUM_LIT:0> ] , line [ <NUM_LIT:1> ] ] = <NUM_LIT:1> <EOL> max_dist = <NUM_LIT:3> <EOL> max_distances = max_dist * np . ones ( <NUM_LIT:20> ) <EOL> identified_lines = _identify_ridge_lines ( test_matr , max_distances , max ( gaps ) + <NUM_LIT:1> ) <EOL> assert_ ( len ( identified_lines ) == <NUM_LIT:2> ) <EOL> for iline in identified_lines : <EOL> adists = np . diff ( iline [ <NUM_LIT:1> ] ) <EOL> np . testing . assert_array_less ( np . abs ( adists ) , max_dist ) <EOL> agaps = np . diff ( iline [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_array_less ( np . abs ( agaps ) , max ( gaps ) + <NUM_LIT:0.1> ) <EOL> def test_single_biggap ( self ) : <EOL> distances = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:5> ] <EOL> max_gap = <NUM_LIT:3> <EOL> gaps = [ <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:1> ] <EOL> test_matr = np . zeros ( [ <NUM_LIT:20> , <NUM_LIT:50> ] ) <EOL> length = <NUM_LIT:12> <EOL> line = _gen_ridge_line ( [ <NUM_LIT:0> , <NUM_LIT> ] , test_matr . shape , length , distances , gaps ) <EOL> test_matr [ line [ <NUM_LIT:0> ] , line [ <NUM_LIT:1> ] ] = <NUM_LIT:1> <EOL> max_dist = <NUM_LIT:6> <EOL> max_distances = max_dist * np . ones ( <NUM_LIT:20> ) <EOL> identified_lines = _identify_ridge_lines ( test_matr , max_distances , max_gap ) <EOL> assert_ ( len ( identified_lines ) == <NUM_LIT:2> ) <EOL> for iline in identified_lines : <EOL> adists = np . diff ( iline [ <NUM_LIT:1> ] ) <EOL> np . testing . assert_array_less ( np . abs ( adists ) , max_dist ) <EOL> agaps = np . diff ( iline [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_array_less ( np . abs ( agaps ) , max ( gaps ) + <NUM_LIT:0.1> ) <EOL> def test_single_biggaps ( self ) : <EOL> distances = [ <NUM_LIT:0> ] <EOL> max_gap = <NUM_LIT:1> <EOL> gaps = [ <NUM_LIT:3> , <NUM_LIT:6> ] <EOL> test_matr = np . zeros ( [ <NUM_LIT:50> , <NUM_LIT:50> ] ) <EOL> length = <NUM_LIT:30> <EOL> line = _gen_ridge_line ( [ <NUM_LIT:0> , <NUM_LIT> ] , test_matr . shape , length , distances , gaps ) <EOL> test_matr [ line [ <NUM_LIT:0> ] , line [ <NUM_LIT:1> ] ] = <NUM_LIT:1> <EOL> max_dist = <NUM_LIT:1> <EOL> max_distances = max_dist * np . ones ( <NUM_LIT:50> ) <EOL> identified_lines = _identify_ridge_lines ( test_matr , max_distances , max_gap ) <EOL> assert_ ( len ( identified_lines ) == <NUM_LIT:3> ) <EOL> for iline in identified_lines : <EOL> adists = np . diff ( iline [ <NUM_LIT:1> ] ) <EOL> np . testing . assert_array_less ( np . abs ( adists ) , max_dist ) <EOL> agaps = np . diff ( iline [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_array_less ( np . abs ( agaps ) , max ( gaps ) + <NUM_LIT:0.1> ) <EOL> class TestArgrel ( TestCase ) : <EOL> def test_empty ( self ) : <EOL> empty_array = np . array ( [ ] , dtype = int ) <EOL> z1 = np . zeros ( <NUM_LIT:5> ) <EOL> i = argrelmin ( z1 ) <EOL> assert_equal ( len ( i ) , <NUM_LIT:1> ) <EOL> assert_array_equal ( i [ <NUM_LIT:0> ] , empty_array ) <EOL> z2 = np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:5> ) ) <EOL> row , col = argrelmin ( z2 , axis = <NUM_LIT:0> ) <EOL> assert_array_equal ( row , empty_array ) <EOL> assert_array_equal ( col , empty_array ) <EOL> row , col = argrelmin ( z2 , axis = <NUM_LIT:1> ) <EOL> assert_array_equal ( row , empty_array ) <EOL> assert_array_equal ( col , empty_array ) <EOL> def test_basic ( self ) : <EOL> x = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] ] ) <EOL> row , col = argrelmax ( x , axis = <NUM_LIT:0> ) <EOL> order = np . argsort ( row ) <EOL> assert_equal ( row [ order ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert_equal ( col [ order ] , [ <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> row , col = argrelmax ( x , axis = <NUM_LIT:1> ) <EOL> order = np . argsort ( row ) <EOL> assert_equal ( row [ order ] , [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert_equal ( col [ order ] , [ <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> row , col = argrelmin ( x , axis = <NUM_LIT:0> ) <EOL> order = np . argsort ( row ) <EOL> assert_equal ( row [ order ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert_equal ( col [ order ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> row , col = argrelmin ( x , axis = <NUM_LIT:1> ) <EOL> order = np . argsort ( row ) <EOL> assert_equal ( row [ order ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert_equal ( col [ order ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_highorder ( self ) : <EOL> order = <NUM_LIT:2> <EOL> sigmas = [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> test_data , act_locs = _gen_gaussians_even ( sigmas , <NUM_LIT> ) <EOL> test_data [ act_locs + order ] = test_data [ act_locs ] * <NUM_LIT> <EOL> test_data [ act_locs - order ] = test_data [ act_locs ] * <NUM_LIT> <EOL> rel_max_locs = argrelmax ( test_data , order = order , mode = '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> assert_ ( len ( rel_max_locs ) == len ( act_locs ) ) <EOL> assert_ ( ( rel_max_locs == act_locs ) . all ( ) ) <EOL> def test_2d_gaussians ( self ) : <EOL> sigmas = [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] <EOL> test_data , act_locs = _gen_gaussians_even ( sigmas , <NUM_LIT:100> ) <EOL> rot_factor = <NUM_LIT:20> <EOL> rot_range = np . arange ( <NUM_LIT:0> , len ( test_data ) ) - rot_factor <EOL> test_data_2 = np . vstack ( [ test_data , test_data [ rot_range ] ] ) <EOL> rel_max_rows , rel_max_cols = argrelmax ( test_data_2 , axis = <NUM_LIT:1> , order = <NUM_LIT:1> ) <EOL> for rw in xrange ( <NUM_LIT:0> , test_data_2 . shape [ <NUM_LIT:0> ] ) : <EOL> inds = ( rel_max_rows == rw ) <EOL> assert_ ( len ( rel_max_cols [ inds ] ) == len ( act_locs ) ) <EOL> assert_ ( ( act_locs == ( rel_max_cols [ inds ] - rot_factor * rw ) ) . all ( ) ) <EOL> class TestFindPeaks ( TestCase ) : <EOL> def test_find_peaks_exact ( self ) : <EOL> """<STR_LIT>""" <EOL> sigmas = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> num_points = <NUM_LIT> <EOL> test_data , act_locs = _gen_gaussians_even ( sigmas , num_points ) <EOL> widths = np . arange ( <NUM_LIT:0.1> , max ( sigmas ) ) <EOL> found_locs = find_peaks_cwt ( test_data , widths , gap_thresh = <NUM_LIT:2> , min_snr = <NUM_LIT:0> , <EOL> min_length = None ) <EOL> np . testing . assert_array_equal ( found_locs , act_locs , <EOL> "<STR_LIT>" ) <EOL> def test_find_peaks_withnoise ( self ) : <EOL> """<STR_LIT>""" <EOL> sigmas = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> num_points = <NUM_LIT> <EOL> test_data , act_locs = _gen_gaussians_even ( sigmas , num_points ) <EOL> widths = np . arange ( <NUM_LIT:0.1> , max ( sigmas ) ) <EOL> noise_amp = <NUM_LIT> <EOL> np . random . seed ( <NUM_LIT> ) <EOL> test_data += ( np . random . rand ( num_points ) - <NUM_LIT:0.5> ) * ( <NUM_LIT:2> * noise_amp ) <EOL> found_locs = find_peaks_cwt ( test_data , widths , min_length = <NUM_LIT:15> , <EOL> gap_thresh = <NUM_LIT:1> , min_snr = noise_amp / <NUM_LIT:5> ) <EOL> np . testing . assert_equal ( len ( found_locs ) , len ( act_locs ) , '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> diffs = np . abs ( found_locs - act_locs ) <EOL> max_diffs = np . array ( sigmas ) / <NUM_LIT:5> <EOL> np . testing . assert_array_less ( diffs , max_diffs , '<STR_LIT>' + <EOL> '<STR_LIT>' % ( max_diffs ) ) <EOL> def test_find_peaks_nopeak ( self ) : <EOL> """<STR_LIT>""" <EOL> noise_amp = <NUM_LIT:1.0> <EOL> num_points = <NUM_LIT:100> <EOL> np . random . seed ( <NUM_LIT> ) <EOL> test_data = ( np . random . rand ( num_points ) - <NUM_LIT:0.5> ) * ( <NUM_LIT:2> * noise_amp ) <EOL> widths = np . arange ( <NUM_LIT:10> , <NUM_LIT:50> ) <EOL> found_locs = find_peaks_cwt ( test_data , widths , min_snr = <NUM_LIT:5> , noise_perc = <NUM_LIT:30> ) <EOL> np . testing . assert_equal ( len ( found_locs ) , <NUM_LIT:0> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_module_suite ( ) </s>
