<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from . . filters . _rank_order import rank_order <EOL> def reconstruction ( seed , mask , method = '<STR_LIT>' , selem = None , offset = None ) : <EOL> """<STR_LIT>""" <EOL> assert tuple ( seed . shape ) == tuple ( mask . shape ) <EOL> if method == '<STR_LIT>' and np . any ( seed > mask ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif method == '<STR_LIT>' and np . any ( seed < mask ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> from . _greyreconstruct import reconstruction_loop <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if selem is None : <EOL> selem = np . ones ( [ <NUM_LIT:3> ] * seed . ndim , dtype = bool ) <EOL> else : <EOL> selem = selem . astype ( bool ) <EOL> if offset is None : <EOL> if not all ( [ d % <NUM_LIT:2> == <NUM_LIT:1> for d in selem . shape ] ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> offset = np . array ( [ d // <NUM_LIT:2> for d in selem . shape ] ) <EOL> selem [ [ slice ( d , d + <NUM_LIT:1> ) for d in offset ] ] = False <EOL> padding = ( np . array ( selem . shape ) / <NUM_LIT:2> ) . astype ( int ) <EOL> dims = np . zeros ( seed . ndim + <NUM_LIT:1> , dtype = int ) <EOL> dims [ <NUM_LIT:1> : ] = np . array ( seed . shape ) + <NUM_LIT:2> * padding <EOL> dims [ <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> inside_slices = [ slice ( p , - p ) for p in padding ] <EOL> if method == '<STR_LIT>' : <EOL> pad_value = np . min ( seed ) <EOL> elif method == '<STR_LIT>' : <EOL> pad_value = np . max ( seed ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % method ) <EOL> images = np . ones ( dims ) * pad_value <EOL> images [ [ <NUM_LIT:0> ] + inside_slices ] = seed <EOL> images [ [ <NUM_LIT:1> ] + inside_slices ] = mask <EOL> value_stride = np . array ( images . strides [ <NUM_LIT:1> : ] ) // images . dtype . itemsize <EOL> image_stride = images . strides [ <NUM_LIT:0> ] // images . dtype . itemsize <EOL> selem_mgrid = np . mgrid [ [ slice ( - o , d - o ) <EOL> for d , o in zip ( selem . shape , offset ) ] ] <EOL> selem_offsets = selem_mgrid [ : , selem ] . transpose ( ) <EOL> nb_strides = np . array ( [ np . sum ( value_stride * selem_offset ) <EOL> for selem_offset in selem_offsets ] , np . int32 ) <EOL> images = images . flatten ( ) <EOL> index_sorted = np . argsort ( images ) . astype ( np . int32 ) <EOL> if method == '<STR_LIT>' : <EOL> index_sorted = index_sorted [ : : - <NUM_LIT:1> ] <EOL> prev = - np . ones ( len ( images ) , np . int32 ) <EOL> next = - np . ones ( len ( images ) , np . int32 ) <EOL> prev [ index_sorted [ <NUM_LIT:1> : ] ] = index_sorted [ : - <NUM_LIT:1> ] <EOL> next [ index_sorted [ : - <NUM_LIT:1> ] ] = index_sorted [ <NUM_LIT:1> : ] <EOL> if method == '<STR_LIT>' : <EOL> value_rank , value_map = rank_order ( images ) <EOL> elif method == '<STR_LIT>' : <EOL> value_rank , value_map = rank_order ( - images ) <EOL> value_map = - value_map <EOL> start = index_sorted [ <NUM_LIT:0> ] <EOL> reconstruction_loop ( value_rank , prev , next , nb_strides , start , <EOL> image_stride ) <EOL> rec_img = value_map [ value_rank [ : image_stride ] ] <EOL> rec_img . shape = np . array ( seed . shape ) + <NUM_LIT:2> * padding <EOL> return rec_img [ inside_slices ] </s>
