<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> import collections <EOL> import scipy . ndimage . measurements as ndim <EOL> from skimage . draw import line <EOL> import numpy as np <EOL> from . import utils <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> def rectangles ( coords , shape ) : <EOL> """<STR_LIT>""" <EOL> labels_grid = np . zeros ( shape , dtype = np . int64 ) <EOL> for i , ( col_coor , row_coor , col_val , row_val ) in enumerate ( coords ) : <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> slc1 = slice ( left , right ) <EOL> slc2 = slice ( top , bottom ) <EOL> if np . any ( labels_grid [ slc1 , slc2 ] ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> labels_grid [ slc1 , slc2 ] = ( i + <NUM_LIT:1> ) <EOL> return labels_grid <EOL> def rings ( edges , center , shape ) : <EOL> """<STR_LIT>""" <EOL> edges = np . atleast_2d ( np . asarray ( edges ) ) . ravel ( ) <EOL> if not <NUM_LIT:0> == len ( edges ) % <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not np . all ( np . diff ( edges ) >= <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> r_coord = utils . radial_grid ( center , shape ) . ravel ( ) <EOL> return _make_roi ( r_coord , edges , shape ) <EOL> def ring_edges ( inner_radius , width , spacing = <NUM_LIT:0> , num_rings = None ) : <EOL> """<STR_LIT>""" <EOL> width_is_list = isinstance ( width , collections . Iterable ) <EOL> spacing_is_list = isinstance ( spacing , collections . Iterable ) <EOL> if ( width_is_list and spacing_is_list ) : <EOL> if len ( width ) != len ( spacing ) - <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if num_rings is None : <EOL> try : <EOL> num_rings = len ( width ) <EOL> except TypeError : <EOL> try : <EOL> num_rings = len ( spacing ) + <NUM_LIT:1> <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> if width_is_list : <EOL> if num_rings != len ( width ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if spacing_is_list : <EOL> if num_rings - <NUM_LIT:1> != len ( spacing ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not width_is_list : <EOL> width = np . ones ( num_rings ) * width <EOL> if not spacing_is_list : <EOL> spacing = np . ones ( num_rings - <NUM_LIT:1> ) * spacing <EOL> all_spacings = np . insert ( spacing , <NUM_LIT:0> , inner_radius ) <EOL> steps = np . array ( [ all_spacings , width ] ) . T . ravel ( ) <EOL> edges = np . cumsum ( steps ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> return edges <EOL> def segmented_rings ( edges , segments , center , shape , offset_angle = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> edges = np . asarray ( edges ) . ravel ( ) <EOL> if not <NUM_LIT:0> == len ( edges ) % <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not np . all ( np . diff ( edges ) >= <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> agrid = utils . angle_grid ( center , shape ) <EOL> agrid [ agrid < <NUM_LIT:0> ] = <NUM_LIT:2> * np . pi + agrid [ agrid < <NUM_LIT:0> ] <EOL> segments_is_list = isinstance ( segments , collections . Iterable ) <EOL> if segments_is_list : <EOL> segments = np . asarray ( segments ) + offset_angle <EOL> else : <EOL> segments = np . linspace ( <NUM_LIT:0> , <NUM_LIT:2> * np . pi , num = <NUM_LIT:1> + segments , endpoint = True ) <EOL> segments += offset_angle <EOL> ind_grid = ( np . digitize ( np . ravel ( agrid ) , segments , <EOL> right = False ) ) . reshape ( shape ) <EOL> label_array = np . zeros ( shape , dtype = np . int64 ) <EOL> rgrid = utils . radial_grid ( center , shape ) <EOL> len_segments = len ( segments ) <EOL> for i in range ( len ( edges ) // <NUM_LIT:2> ) : <EOL> indices = ( edges [ <NUM_LIT:2> * i ] <= rgrid ) & ( rgrid < edges [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ) <EOL> label_array [ indices ] = ind_grid [ indices ] + ( len_segments - <NUM_LIT:1> ) * i <EOL> return label_array <EOL> def roi_max_counts ( images_sets , label_array ) : <EOL> """<STR_LIT>""" <EOL> max_cts = <NUM_LIT:0> <EOL> for img_set in images_sets : <EOL> for img in img_set : <EOL> max_cts = max ( max_cts , ndim . maximum ( img , label_array ) ) <EOL> return max_cts <EOL> def roi_pixel_values ( image , labels , index = None ) : <EOL> """<STR_LIT>""" <EOL> if labels . shape != image . shape : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if index is None : <EOL> index = np . arange ( <NUM_LIT:1> , np . max ( labels ) + <NUM_LIT:1> ) <EOL> roi_pix = [ ] <EOL> for n in index : <EOL> roi_pix . append ( image [ labels == n ] ) <EOL> return roi_pix , index <EOL> def mean_intensity ( images , labeled_array , index = None ) : <EOL> """<STR_LIT>""" <EOL> if labeled_array . shape != images [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> : ] : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( images [ <NUM_LIT:0> ] . shape , labeled_array . shape ) ) <EOL> if index is None : <EOL> index = list ( np . unique ( labeled_array ) ) <EOL> index . remove ( <NUM_LIT:0> ) <EOL> try : <EOL> len ( index ) <EOL> except TypeError : <EOL> index = [ index ] <EOL> mean_intensity = np . zeros ( ( images . shape [ <NUM_LIT:0> ] , len ( index ) ) ) <EOL> for n , img in enumerate ( images ) : <EOL> mean_intensity [ n ] = ndim . mean ( img , labeled_array , index = index ) <EOL> return mean_intensity , index <EOL> def circular_average ( image , calibrated_center , threshold = <NUM_LIT:0> , nx = <NUM_LIT:100> , <EOL> pixel_size = ( <NUM_LIT:1> , <NUM_LIT:1> ) , min_x = None , max_x = None ) : <EOL> """<STR_LIT>""" <EOL> radial_val = utils . radial_grid ( calibrated_center , image . shape , pixel_size ) <EOL> bin_edges , sums , counts = utils . bin_1D ( np . ravel ( radial_val ) , <EOL> np . ravel ( image ) , nx , <EOL> min_x = min_x , <EOL> max_x = max_x ) <EOL> th_mask = counts > threshold <EOL> ring_averages = sums [ th_mask ] / counts [ th_mask ] <EOL> bin_centers = utils . bin_edges_to_centers ( bin_edges ) [ th_mask ] <EOL> return bin_centers , ring_averages <EOL> def kymograph ( images , labels , num ) : <EOL> """<STR_LIT>""" <EOL> kymo = [ ] <EOL> for n , img in enumerate ( images ) : <EOL> kymo . append ( ( roi_pixel_values ( img , labels == num ) [ <NUM_LIT:0> ] ) ) <EOL> return np . vstack ( kymo ) <EOL> def extract_label_indices ( labels ) : <EOL> """<STR_LIT>""" <EOL> img_dim = labels . shape <EOL> w = np . where ( np . ravel ( labels ) > <NUM_LIT:0> ) <EOL> grid = np . indices ( ( img_dim [ <NUM_LIT:0> ] , img_dim [ <NUM_LIT:1> ] ) ) <EOL> pixel_list = np . ravel ( ( grid [ <NUM_LIT:0> ] * img_dim [ <NUM_LIT:1> ] + grid [ <NUM_LIT:1> ] ) ) [ w ] <EOL> label_mask = labels [ labels > <NUM_LIT:0> ] <EOL> return label_mask , pixel_list <EOL> def _make_roi ( coords , edges , shape ) : <EOL> """<STR_LIT>""" <EOL> label_array = np . digitize ( coords , edges , right = False ) <EOL> label_array = ( np . where ( label_array % <NUM_LIT:2> != <NUM_LIT:0> , label_array , <NUM_LIT:0> ) + <NUM_LIT:1> ) // <NUM_LIT:2> <EOL> return label_array . reshape ( shape ) <EOL> def bar ( edges , shape , horizontal = True , values = None ) : <EOL> """<STR_LIT>""" <EOL> edges = np . atleast_2d ( np . asarray ( edges ) ) . ravel ( ) <EOL> if not <NUM_LIT:0> == len ( edges ) % <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not np . all ( np . diff ( edges ) >= <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if values is None : <EOL> values = np . repeat ( range ( shape [ <NUM_LIT:0> ] ) , shape [ <NUM_LIT:1> ] ) <EOL> if not horizontal : <EOL> values = np . tile ( range ( shape [ <NUM_LIT:1> ] ) , shape [ <NUM_LIT:0> ] ) <EOL> return _make_roi ( values , edges , shape ) <EOL> def box ( shape , v_edges , h_edges = None , h_values = None , v_values = None ) : <EOL> """<STR_LIT>""" <EOL> if h_edges is None : <EOL> h_edges = v_edges <EOL> if h_values is None and v_values is None : <EOL> v_values , h_values = np . mgrid [ : shape [ <NUM_LIT:0> ] , : shape [ <NUM_LIT:1> ] ] <EOL> elif h_values . shape != v_values . shape : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for edges in ( h_edges , v_edges ) : <EOL> edges = np . atleast_2d ( np . asarray ( edges ) ) . ravel ( ) <EOL> if not <NUM_LIT:0> == len ( edges ) % <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> coords = [ ] <EOL> for h in h_edges : <EOL> for v in v_edges : <EOL> coords . append ( ( h [ <NUM_LIT:0> ] , v [ <NUM_LIT:0> ] , h [ <NUM_LIT:1> ] - h [ <NUM_LIT:0> ] , v [ <NUM_LIT:1> ] - v [ <NUM_LIT:0> ] ) ) <EOL> return rectangles ( coords , v_values . shape ) <EOL> def lines ( end_points , shape ) : <EOL> """<STR_LIT>""" <EOL> label_array = np . zeros ( shape , dtype = np . int64 ) <EOL> label = <NUM_LIT:0> <EOL> for points in end_points : <EOL> if len ( points ) != <NUM_LIT:4> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> rr , cc = line ( np . max ( [ points [ <NUM_LIT:0> ] , <NUM_LIT:0> ] ) , np . max ( [ points [ <NUM_LIT:1> ] , <NUM_LIT:0> ] ) , <EOL> np . min ( [ points [ <NUM_LIT:2> ] , shape [ <NUM_LIT:0> ] - <NUM_LIT:1> ] ) , <EOL> np . min ( [ points [ <NUM_LIT:3> ] , shape [ <NUM_LIT:1> ] - <NUM_LIT:1> ] ) ) <EOL> label += <NUM_LIT:1> <EOL> label_array [ rr , cc ] = label <EOL> return label_array </s>
