<s> from __future__ import absolute_import , division , print_function <EOL> import logging <EOL> import numpy as np <EOL> from skbeam . core import roi <EOL> from skbeam . core import utils <EOL> import itertools <EOL> from skimage import morphology <EOL> from numpy . testing import ( assert_array_equal , assert_array_almost_equal , <EOL> assert_almost_equal ) <EOL> from nose . tools import assert_equal , assert_true , assert_raises <EOL> logger = logging . getLogger ( __name__ ) <EOL> def test_rectangles ( ) : <EOL> shape = ( <NUM_LIT:15> , <NUM_LIT> ) <EOL> roi_data = np . array ( ( [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:3> ] , [ <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:5> ] , [ <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:10> ] ) , <EOL> dtype = np . int64 ) <EOL> all_roi_inds = roi . rectangles ( roi_data , shape ) <EOL> roi_inds , pixel_list = roi . extract_label_indices ( all_roi_inds ) <EOL> ty = np . zeros ( shape ) . ravel ( ) <EOL> ty [ pixel_list ] = roi_inds <EOL> re_mesh = ty . reshape ( * shape ) <EOL> for i , ( col_coor , row_coor , col_val , row_val ) in enumerate ( roi_data , <NUM_LIT:0> ) : <EOL> ind_co = np . column_stack ( np . where ( re_mesh == i + <NUM_LIT:1> ) ) <EOL> left , right = np . max ( [ col_coor , <NUM_LIT:0> ] ) , np . min ( [ col_coor + col_val , <EOL> shape [ <NUM_LIT:0> ] ] ) <EOL> top , bottom = np . max ( [ row_coor , <NUM_LIT:0> ] ) , np . min ( [ row_coor + row_val , <EOL> shape [ <NUM_LIT:1> ] ] ) <EOL> assert_almost_equal ( left , ind_co [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> assert_almost_equal ( right - <NUM_LIT:1> , ind_co [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> assert_almost_equal ( top , ind_co [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> assert_almost_equal ( bottom - <NUM_LIT:1> , ind_co [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] ) <EOL> def test_rings ( ) : <EOL> center = ( <NUM_LIT> , <NUM_LIT> ) <EOL> img_dim = ( <NUM_LIT:200> , <NUM_LIT> ) <EOL> first_q = <NUM_LIT> <EOL> delta_q = <NUM_LIT> <EOL> num_rings = <NUM_LIT:7> <EOL> one_step_q = <NUM_LIT> <EOL> step_q = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> edges = roi . ring_edges ( first_q , width = delta_q , spacing = one_step_q , <EOL> num_rings = num_rings ) <EOL> print ( "<STR_LIT>" , edges ) <EOL> label_array = roi . rings ( edges , center , img_dim ) <EOL> print ( "<STR_LIT>" , label_array ) <EOL> label_mask , pixel_list = roi . extract_label_indices ( label_array ) <EOL> num_pixels = np . bincount ( label_mask , minlength = ( np . max ( label_mask ) + <NUM_LIT:1> ) ) <EOL> num_pixels = num_pixels [ <NUM_LIT:1> : ] <EOL> edges = roi . ring_edges ( first_q , width = delta_q , spacing = <NUM_LIT> , <EOL> num_rings = num_rings ) <EOL> print ( "<STR_LIT>" , edges ) <EOL> label_array = roi . rings ( edges , center , img_dim ) <EOL> print ( "<STR_LIT>" , label_array ) <EOL> label_mask , pixel_list = roi . extract_label_indices ( label_array ) <EOL> num_pixels = np . bincount ( label_mask , minlength = ( np . max ( label_mask ) + <NUM_LIT:1> ) ) <EOL> num_pixels = num_pixels [ <NUM_LIT:1> : ] <EOL> edges = roi . ring_edges ( first_q , width = delta_q , spacing = step_q , <EOL> num_rings = <NUM_LIT:4> ) <EOL> print ( "<STR_LIT>" , edges ) <EOL> label_array = roi . rings ( edges , center , img_dim ) <EOL> print ( "<STR_LIT>" , label_array ) <EOL> label_mask , pixel_list = roi . extract_label_indices ( label_array ) <EOL> num_pixels = np . bincount ( label_mask , minlength = ( np . max ( label_mask ) + <NUM_LIT:1> ) ) <EOL> num_pixels = num_pixels [ <NUM_LIT:1> : ] <EOL> edges = roi . ring_edges ( first_q , width = delta_q , num_rings = num_rings ) <EOL> print ( "<STR_LIT>" , edges ) <EOL> label_array = roi . rings ( edges , center , img_dim ) <EOL> print ( "<STR_LIT>" , label_array ) <EOL> label_mask , pixel_list = roi . extract_label_indices ( label_array ) <EOL> num_pixels = np . bincount ( label_mask , minlength = ( np . max ( label_mask ) + <NUM_LIT:1> ) ) <EOL> num_pixels = num_pixels [ <NUM_LIT:1> : ] <EOL> print ( np . unique ( label_array ) ) <EOL> actual_num_rings = len ( np . unique ( label_array ) ) - <NUM_LIT:1> <EOL> assert_equal ( actual_num_rings , num_rings ) <EOL> ring_areas = np . bincount ( label_array . ravel ( ) ) [ <NUM_LIT:1> : ] <EOL> area_comparison = np . diff ( ring_areas ) <EOL> print ( area_comparison ) <EOL> areas_monotonically_increasing = np . all ( area_comparison > <NUM_LIT:0> ) <EOL> assert_true ( areas_monotonically_increasing ) <EOL> assert_raises ( ValueError , <EOL> lambda : roi . ring_edges ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> assert_raises ( ValueError , <EOL> lambda : roi . ring_edges ( <NUM_LIT:1> , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , num_rings = <NUM_LIT:2> ) ) <EOL> assert_raises ( ValueError , <EOL> lambda : roi . ring_edges ( <NUM_LIT:1> , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> ] ) ) <EOL> assert_raises ( ValueError , <EOL> lambda : roi . ring_edges ( <NUM_LIT:1> , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> assert_raises ( ValueError , <EOL> lambda : roi . ring_edges ( <NUM_LIT:1> , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , <NUM_LIT:5> ) ) <EOL> w_edges = [ [ <NUM_LIT:5> , <NUM_LIT:7> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] <EOL> assert_raises ( ValueError , roi . rings , w_edges , center = ( <NUM_LIT:4> , <NUM_LIT:4> ) , <EOL> shape = ( <NUM_LIT:20> , <NUM_LIT:20> ) ) <EOL> def _helper_check ( pixel_list , inds , num_pix , edges , center , <EOL> img_dim , num_qs ) : <EOL> ty = np . zeros ( img_dim ) . ravel ( ) <EOL> ty [ pixel_list ] = inds <EOL> grid_values = utils . radial_grid ( img_dim , center ) <EOL> zero_grid = np . zeros ( ( img_dim [ <NUM_LIT:0> ] , img_dim [ <NUM_LIT:1> ] ) ) <EOL> for r in range ( num_qs ) : <EOL> vl = ( edges [ r ] [ <NUM_LIT:0> ] <= grid_values ) & ( grid_values < edges [ r ] [ <NUM_LIT:1> ] ) <EOL> zero_grid [ vl ] = r + <NUM_LIT:1> <EOL> num_pixels = [ ] <EOL> for r in range ( num_qs ) : <EOL> num_pixels . append ( int ( ( np . histogramdd ( np . ravel ( grid_values ) , bins = <NUM_LIT:1> , <EOL> range = [ [ edges [ r ] [ <NUM_LIT:0> ] , <EOL> ( edges [ r ] [ <NUM_LIT:1> ] - <EOL> <NUM_LIT> ) ] ] ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ) <EOL> assert_array_equal ( num_pix , num_pixels ) <EOL> def test_segmented_rings ( ) : <EOL> center = ( <NUM_LIT> , <NUM_LIT> ) <EOL> img_dim = ( <NUM_LIT> , <NUM_LIT> ) <EOL> first_q = <NUM_LIT:5> <EOL> delta_q = <NUM_LIT:5> <EOL> num_rings = <NUM_LIT:4> <EOL> slicing = <NUM_LIT:4> <EOL> edges = roi . ring_edges ( first_q , width = delta_q , spacing = <NUM_LIT:4> , <EOL> num_rings = num_rings ) <EOL> print ( "<STR_LIT>" , edges ) <EOL> label_array = roi . segmented_rings ( edges , slicing , center , <EOL> img_dim , offset_angle = <NUM_LIT:0> ) <EOL> print ( "<STR_LIT>" , label_array ) <EOL> label_list = np . unique ( label_array . ravel ( ) ) <EOL> actual_num_labels = len ( label_list ) - <NUM_LIT:1> <EOL> num_labels = num_rings * slicing <EOL> assert_equal ( actual_num_labels , num_labels ) <EOL> assert_array_equal ( label_list , np . arange ( num_labels + <NUM_LIT:1> ) ) <EOL> num_pixels = np . bincount ( label_array . ravel ( ) ) <EOL> expected_num_pixels = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT:200> , <NUM_LIT:200> , <NUM_LIT:200> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> assert_array_equal ( num_pixels , expected_num_pixels ) <EOL> def test_roi_pixel_values ( ) : <EOL> images = morphology . diamond ( <NUM_LIT:8> ) <EOL> label_array = np . zeros ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> assert_raises ( ValueError , <EOL> lambda : roi . roi_pixel_values ( images , label_array ) ) <EOL> center = ( <NUM_LIT> , <NUM_LIT> ) <EOL> inner_radius = <NUM_LIT> <EOL> width = <NUM_LIT:1> <EOL> spacing = <NUM_LIT:1> <EOL> edges = roi . ring_edges ( inner_radius , width , spacing , num_rings = <NUM_LIT:5> ) <EOL> rings = roi . rings ( edges , center , images . shape ) <EOL> intensity_data , index = roi . roi_pixel_values ( images , rings ) <EOL> assert_array_equal ( intensity_data [ <NUM_LIT:0> ] , ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) ) <EOL> assert_array_equal ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , index ) <EOL> def test_roi_max_counts ( ) : <EOL> img_stack1 = np . random . randint ( <NUM_LIT:0> , <NUM_LIT> , size = ( <NUM_LIT:50> , ) + ( <NUM_LIT:50> , <NUM_LIT:50> ) ) <EOL> img_stack2 = np . random . randint ( <NUM_LIT:0> , <NUM_LIT> , size = ( <NUM_LIT:100> , ) + ( <NUM_LIT:50> , <NUM_LIT:50> ) ) <EOL> img_stack1 [ <NUM_LIT:0> ] [ <NUM_LIT:20> , <NUM_LIT:20> ] = <NUM_LIT> <EOL> samples = ( img_stack1 , img_stack2 ) <EOL> label_array = np . zeros ( ( img_stack1 [ <NUM_LIT:0> ] . shape ) ) <EOL> label_array [ img_stack1 [ <NUM_LIT:0> ] < <NUM_LIT:20> ] = <NUM_LIT:1> <EOL> label_array [ img_stack1 [ <NUM_LIT:0> ] > <NUM_LIT> ] = <NUM_LIT:2> <EOL> assert_array_equal ( <NUM_LIT> , roi . roi_max_counts ( samples , label_array ) ) <EOL> def test_static_test_sets ( ) : <EOL> images1 = [ ] <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> int_array = np . tril ( i * np . ones ( <NUM_LIT:50> ) ) <EOL> int_array [ int_array == <NUM_LIT:0> ] = i * <NUM_LIT:100> <EOL> images1 . append ( int_array ) <EOL> images2 = [ ] <EOL> for i in range ( <NUM_LIT:20> ) : <EOL> int_array = np . triu ( i * np . ones ( <NUM_LIT:50> ) ) <EOL> int_array [ int_array == <NUM_LIT:0> ] = i * <NUM_LIT:100> <EOL> images2 . append ( int_array ) <EOL> samples = { '<STR_LIT>' : np . asarray ( images1 ) , '<STR_LIT>' : np . asarray ( images2 ) } <EOL> roi_data = np . array ( ( [ <NUM_LIT:2> , <NUM_LIT:30> , <NUM_LIT:12> , <NUM_LIT:15> ] , [ <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT:15> , <NUM_LIT:10> ] ) , dtype = np . int64 ) <EOL> label_array = roi . rectangles ( roi_data , shape = ( <NUM_LIT:50> , <NUM_LIT:50> ) ) <EOL> roi_data = [ ] <EOL> for k , v in sorted ( samples . items ( ) ) : <EOL> intensity , index_list = roi . mean_intensity ( v , label_array ) <EOL> roi_data . append ( intensity ) <EOL> return_values = [ roi_data [ <NUM_LIT:0> ] [ : , <NUM_LIT:0> ] , roi_data [ <NUM_LIT:0> ] [ : , <NUM_LIT:1> ] , <EOL> roi_data [ <NUM_LIT:1> ] [ : , <NUM_LIT:0> ] , roi_data [ <NUM_LIT:1> ] [ : , <NUM_LIT:1> ] , ] <EOL> expected_values = [ <EOL> np . asarray ( [ float ( x ) for x in range ( <NUM_LIT:0> , <NUM_LIT:1000> , <NUM_LIT:100> ) ] ) , <EOL> np . asarray ( [ float ( x ) for x in range ( <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT:1> ) ] ) , <EOL> np . asarray ( [ float ( x ) for x in range ( <NUM_LIT:0> , <NUM_LIT:20> , <NUM_LIT:1> ) ] ) , <EOL> np . asarray ( [ float ( x ) for x in range ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:100> ) ] ) <EOL> ] <EOL> err_msg = [ '<STR_LIT>' % ( i , j ) <EOL> for i , j in itertools . product ( ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) ] <EOL> for returned , expected , err in zip ( return_values , <EOL> expected_values , err_msg ) : <EOL> assert_array_equal ( returned , expected , <EOL> err_msg = err , verbose = True ) <EOL> def test_circular_average ( ) : <EOL> image = np . zeros ( ( <NUM_LIT:12> , <NUM_LIT:12> ) ) <EOL> calib_center = ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> inner_radius = <NUM_LIT:1> <EOL> edges = roi . ring_edges ( inner_radius , width = <NUM_LIT:1> , spacing = <NUM_LIT:1> , num_rings = <NUM_LIT:2> ) <EOL> labels = roi . rings ( edges , calib_center , image . shape ) <EOL> image [ labels == <NUM_LIT:1> ] = <NUM_LIT:10> <EOL> image [ labels == <NUM_LIT:2> ] = <NUM_LIT:10> <EOL> bin_cen , ring_avg = roi . circular_average ( image , calib_center , nx = <NUM_LIT:6> ) <EOL> assert_array_almost_equal ( bin_cen , [ <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] , decimal = <NUM_LIT:6> ) <EOL> assert_array_almost_equal ( ring_avg , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> , <EOL> <NUM_LIT:0.> , <NUM_LIT:0.> ] , decimal = <NUM_LIT:6> ) <EOL> bin_cen1 , ring_avg1 = roi . circular_average ( image , calib_center , min_x = <NUM_LIT:0> , <EOL> max_x = <NUM_LIT:10> , nx = None ) <EOL> assert_array_almost_equal ( bin_cen1 , [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> ] ) <EOL> def test_kymograph ( ) : <EOL> calib_center = ( <NUM_LIT> , <NUM_LIT> ) <EOL> inner_radius = <NUM_LIT:5> <EOL> edges = roi . ring_edges ( inner_radius , width = <NUM_LIT:2> , num_rings = <NUM_LIT:1> ) <EOL> labels = roi . rings ( edges , calib_center , ( <NUM_LIT:50> , <NUM_LIT:50> ) ) <EOL> images = [ ] <EOL> num_images = <NUM_LIT:100> <EOL> for i in range ( num_images ) : <EOL> int_array = i * np . ones ( labels . shape ) <EOL> images . append ( int_array ) <EOL> kymograph_data = roi . kymograph ( np . asarray ( images ) , labels , num = <NUM_LIT:1> ) <EOL> expected_shape = ( num_images , np . sum ( labels [ labels == <NUM_LIT:1> ] ) ) <EOL> assert kymograph_data . shape [ <NUM_LIT:0> ] == expected_shape [ <NUM_LIT:0> ] <EOL> assert kymograph_data . shape [ <NUM_LIT:1> ] == expected_shape [ <NUM_LIT:1> ] <EOL> assert np . all ( kymograph_data [ : , <NUM_LIT:0> ] == np . arange ( num_images ) ) <EOL> for row in kymograph_data : <EOL> assert np . all ( row == row [ <NUM_LIT:0> ] ) <EOL> def test_bars_boxes ( ) : <EOL> edges = [ [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:7> ] ] <EOL> shape = ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> h_label_array = roi . bar ( edges , shape ) <EOL> v_label_array = roi . bar ( edges , shape , horizontal = False ) <EOL> w_edges = [ [ <NUM_LIT:5> , <NUM_LIT:7> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] <EOL> assert_array_equal ( h_label_array [ <NUM_LIT:3> , : ] , np . ones ( ( <NUM_LIT:10> , ) , dtype = np . int ) ) <EOL> assert_array_equal ( v_label_array [ : , <NUM_LIT:3> ] , np . ones ( ( <NUM_LIT:10> , ) , dtype = np . int ) ) <EOL> assert_array_equal ( np . unique ( h_label_array ) [ <NUM_LIT:1> : ] , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) <EOL> assert_array_equal ( np . unique ( v_label_array ) [ <NUM_LIT:1> : ] , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) <EOL> assert_raises ( ValueError , roi . bar , w_edges , shape ) <EOL> box_array = roi . box ( shape , edges ) <EOL> assert_array_equal ( box_array [ <NUM_LIT:3> , <NUM_LIT:3> ] , <NUM_LIT:1> ) <EOL> assert_array_equal ( box_array [ <NUM_LIT:5> , <NUM_LIT:5> ] , <NUM_LIT:4> ) <EOL> assert_array_equal ( np . unique ( box_array ) [ <NUM_LIT:1> : ] , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) ) <EOL> n_edges = edges = [ [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:7> ] , [ <NUM_LIT:8> ] ] <EOL> h_values , v_values = np . mgrid [ <NUM_LIT:0> : <NUM_LIT:10> , <NUM_LIT:0> : <NUM_LIT:10> ] <EOL> h_val , v_val = np . mgrid [ <NUM_LIT:0> : <NUM_LIT:20> , <NUM_LIT:0> : <NUM_LIT:20> ] <EOL> assert_raises ( ValueError , roi . box , shape , n_edges ) <EOL> assert_raises ( ValueError , roi . box , shape , edges , h_values = h_val , <EOL> v_values = v_values ) <EOL> def test_lines ( ) : <EOL> points = ( [ <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> shape = ( <NUM_LIT> , <NUM_LIT> ) <EOL> label_array = roi . lines ( points , shape ) <EOL> assert_array_equal ( np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , np . unique ( label_array ) [ <NUM_LIT:1> : ] ) <EOL> assert_raises ( ValueError , roi . lines , <EOL> ( [ <NUM_LIT:10> , <NUM_LIT:12> , <NUM_LIT:30> ] , [ <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT> ] ) , shape ) </s>
