<s> from __future__ import division , print_function , absolute_import <EOL> import math <EOL> import numpy <EOL> from . import _ni_support <EOL> from . import _nd_image <EOL> from scipy . misc import doccer <EOL> from scipy . _lib . _version import NumpyVersion <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> _input_doc = """<STR_LIT>""" <EOL> _axis_doc = """<STR_LIT>""" <EOL> _output_doc = """<STR_LIT>""" <EOL> _size_foot_doc = """<STR_LIT>""" <EOL> _mode_doc = """<STR_LIT>""" <EOL> _cval_doc = """<STR_LIT>""" <EOL> _origin_doc = """<STR_LIT>""" <EOL> _extra_arguments_doc = """<STR_LIT>""" <EOL> _extra_keywords_doc = """<STR_LIT>""" <EOL> docdict = { <EOL> '<STR_LIT:input>' : _input_doc , <EOL> '<STR_LIT>' : _axis_doc , <EOL> '<STR_LIT>' : _output_doc , <EOL> '<STR_LIT>' : _size_foot_doc , <EOL> '<STR_LIT>' : _mode_doc , <EOL> '<STR_LIT>' : _cval_doc , <EOL> '<STR_LIT>' : _origin_doc , <EOL> '<STR_LIT>' : _extra_arguments_doc , <EOL> '<STR_LIT>' : _extra_keywords_doc , <EOL> } <EOL> docfiller = doccer . filldoc ( docdict ) <EOL> @ docfiller <EOL> def correlate1d ( input , weights , axis = - <NUM_LIT:1> , output = None , mode = "<STR_LIT>" , <EOL> cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> weights = numpy . asarray ( weights , dtype = numpy . float64 ) <EOL> if weights . ndim != <NUM_LIT:1> or weights . shape [ <NUM_LIT:0> ] < <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if not weights . flags . contiguous : <EOL> weights = weights . copy ( ) <EOL> axis = _ni_support . _check_axis ( axis , input . ndim ) <EOL> if ( len ( weights ) // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( len ( weights ) // <NUM_LIT:2> + <EOL> origin > len ( weights ) ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . correlate1d ( input , weights , axis , output , mode , cval , <EOL> origin ) <EOL> return return_value <EOL> @ docfiller <EOL> def convolve1d ( input , weights , axis = - <NUM_LIT:1> , output = None , mode = "<STR_LIT>" , <EOL> cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> weights = weights [ : : - <NUM_LIT:1> ] <EOL> origin = - origin <EOL> if not len ( weights ) & <NUM_LIT:1> : <EOL> origin -= <NUM_LIT:1> <EOL> return correlate1d ( input , weights , axis , output , mode , cval , origin ) <EOL> @ docfiller <EOL> def gaussian_filter1d ( input , sigma , axis = - <NUM_LIT:1> , order = <NUM_LIT:0> , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , truncate = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if order not in range ( <NUM_LIT:4> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> sd = float ( sigma ) <EOL> lw = int ( truncate * sd + <NUM_LIT:0.5> ) <EOL> weights = [ <NUM_LIT:0.0> ] * ( <NUM_LIT:2> * lw + <NUM_LIT:1> ) <EOL> weights [ lw ] = <NUM_LIT:1.0> <EOL> sum = <NUM_LIT:1.0> <EOL> sd = sd * sd <EOL> for ii in range ( <NUM_LIT:1> , lw + <NUM_LIT:1> ) : <EOL> tmp = math . exp ( - <NUM_LIT:0.5> * float ( ii * ii ) / sd ) <EOL> weights [ lw + ii ] = tmp <EOL> weights [ lw - ii ] = tmp <EOL> sum += <NUM_LIT> * tmp <EOL> for ii in range ( <NUM_LIT:2> * lw + <NUM_LIT:1> ) : <EOL> weights [ ii ] /= sum <EOL> if order == <NUM_LIT:1> : <EOL> weights [ lw ] = <NUM_LIT:0.0> <EOL> for ii in range ( <NUM_LIT:1> , lw + <NUM_LIT:1> ) : <EOL> x = float ( ii ) <EOL> tmp = - x / sd * weights [ lw + ii ] <EOL> weights [ lw + ii ] = - tmp <EOL> weights [ lw - ii ] = tmp <EOL> elif order == <NUM_LIT:2> : <EOL> weights [ lw ] *= - <NUM_LIT:1.0> / sd <EOL> for ii in range ( <NUM_LIT:1> , lw + <NUM_LIT:1> ) : <EOL> x = float ( ii ) <EOL> tmp = ( x * x / sd - <NUM_LIT:1.0> ) * weights [ lw + ii ] / sd <EOL> weights [ lw + ii ] = tmp <EOL> weights [ lw - ii ] = tmp <EOL> elif order == <NUM_LIT:3> : <EOL> weights [ lw ] = <NUM_LIT:0.0> <EOL> sd2 = sd * sd <EOL> for ii in range ( <NUM_LIT:1> , lw + <NUM_LIT:1> ) : <EOL> x = float ( ii ) <EOL> tmp = ( <NUM_LIT> - x * x / sd ) * x * weights [ lw + ii ] / sd2 <EOL> weights [ lw + ii ] = - tmp <EOL> weights [ lw - ii ] = tmp <EOL> return correlate1d ( input , weights , axis , output , mode , cval , <NUM_LIT:0> ) <EOL> @ docfiller <EOL> def gaussian_filter ( input , sigma , order = <NUM_LIT:0> , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , truncate = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> orders = _ni_support . _normalize_sequence ( order , input . ndim ) <EOL> if not set ( orders ) . issubset ( set ( range ( <NUM_LIT:4> ) ) ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> sigmas = _ni_support . _normalize_sequence ( sigma , input . ndim ) <EOL> axes = list ( range ( input . ndim ) ) <EOL> axes = [ ( axes [ ii ] , sigmas [ ii ] , orders [ ii ] ) <EOL> for ii in range ( len ( axes ) ) if sigmas [ ii ] > <NUM_LIT> ] <EOL> if len ( axes ) > <NUM_LIT:0> : <EOL> for axis , sigma , order in axes : <EOL> gaussian_filter1d ( input , sigma , axis , order , output , <EOL> mode , cval , truncate ) <EOL> input = output <EOL> else : <EOL> output [ ... ] = input [ ... ] <EOL> return return_value <EOL> @ docfiller <EOL> def prewitt ( input , axis = - <NUM_LIT:1> , output = None , mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> axis = _ni_support . _check_axis ( axis , input . ndim ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> correlate1d ( input , [ - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , axis , output , mode , cval , <NUM_LIT:0> ) <EOL> axes = [ ii for ii in range ( input . ndim ) if ii != axis ] <EOL> for ii in axes : <EOL> correlate1d ( output , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , ii , output , mode , cval , <NUM_LIT:0> , ) <EOL> return return_value <EOL> @ docfiller <EOL> def sobel ( input , axis = - <NUM_LIT:1> , output = None , mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> axis = _ni_support . _check_axis ( axis , input . ndim ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> correlate1d ( input , [ - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , axis , output , mode , cval , <NUM_LIT:0> ) <EOL> axes = [ ii for ii in range ( input . ndim ) if ii != axis ] <EOL> for ii in axes : <EOL> correlate1d ( output , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ] , ii , output , mode , cval , <NUM_LIT:0> ) <EOL> return return_value <EOL> @ docfiller <EOL> def generic_laplace ( input , derivative2 , output = None , mode = "<STR_LIT>" , <EOL> cval = <NUM_LIT:0.0> , <EOL> extra_arguments = ( ) , <EOL> extra_keywords = None ) : <EOL> """<STR_LIT>""" <EOL> if extra_keywords is None : <EOL> extra_keywords = { } <EOL> input = numpy . asarray ( input ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> axes = list ( range ( input . ndim ) ) <EOL> if len ( axes ) > <NUM_LIT:0> : <EOL> derivative2 ( input , axes [ <NUM_LIT:0> ] , output , mode , cval , <EOL> * extra_arguments , ** extra_keywords ) <EOL> for ii in range ( <NUM_LIT:1> , len ( axes ) ) : <EOL> tmp = derivative2 ( input , axes [ ii ] , output . dtype , mode , cval , <EOL> * extra_arguments , ** extra_keywords ) <EOL> output += tmp <EOL> else : <EOL> output [ ... ] = input [ ... ] <EOL> return return_value <EOL> @ docfiller <EOL> def laplace ( input , output = None , mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> def derivative2 ( input , axis , output , mode , cval ) : <EOL> return correlate1d ( input , [ <NUM_LIT:1> , - <NUM_LIT:2> , <NUM_LIT:1> ] , axis , output , mode , cval , <NUM_LIT:0> ) <EOL> return generic_laplace ( input , derivative2 , output , mode , cval ) <EOL> @ docfiller <EOL> def gaussian_laplace ( input , sigma , output = None , mode = "<STR_LIT>" , <EOL> cval = <NUM_LIT:0.0> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> def derivative2 ( input , axis , output , mode , cval , sigma , ** kwargs ) : <EOL> order = [ <NUM_LIT:0> ] * input . ndim <EOL> order [ axis ] = <NUM_LIT:2> <EOL> return gaussian_filter ( input , sigma , order , output , mode , cval , <EOL> ** kwargs ) <EOL> return generic_laplace ( input , derivative2 , output , mode , cval , <EOL> extra_arguments = ( sigma , ) , <EOL> extra_keywords = kwargs ) <EOL> @ docfiller <EOL> def generic_gradient_magnitude ( input , derivative , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , <EOL> extra_arguments = ( ) , extra_keywords = None ) : <EOL> """<STR_LIT>""" <EOL> if extra_keywords is None : <EOL> extra_keywords = { } <EOL> input = numpy . asarray ( input ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> axes = list ( range ( input . ndim ) ) <EOL> if len ( axes ) > <NUM_LIT:0> : <EOL> derivative ( input , axes [ <NUM_LIT:0> ] , output , mode , cval , <EOL> * extra_arguments , ** extra_keywords ) <EOL> numpy . multiply ( output , output , output ) <EOL> for ii in range ( <NUM_LIT:1> , len ( axes ) ) : <EOL> tmp = derivative ( input , axes [ ii ] , output . dtype , mode , cval , <EOL> * extra_arguments , ** extra_keywords ) <EOL> numpy . multiply ( tmp , tmp , tmp ) <EOL> output += tmp <EOL> numpy . sqrt ( output , output , casting = '<STR_LIT>' ) <EOL> else : <EOL> output [ ... ] = input [ ... ] <EOL> return return_value <EOL> @ docfiller <EOL> def gaussian_gradient_magnitude ( input , sigma , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> def derivative ( input , axis , output , mode , cval , sigma , ** kwargs ) : <EOL> order = [ <NUM_LIT:0> ] * input . ndim <EOL> order [ axis ] = <NUM_LIT:1> <EOL> return gaussian_filter ( input , sigma , order , output , mode , <EOL> cval , ** kwargs ) <EOL> return generic_gradient_magnitude ( input , derivative , output , mode , <EOL> cval , extra_arguments = ( sigma , ) , <EOL> extra_keywords = kwargs ) <EOL> def _correlate_or_convolve ( input , weights , output , mode , cval , origin , <EOL> convolution ) : <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> origins = _ni_support . _normalize_sequence ( origin , input . ndim ) <EOL> weights = numpy . asarray ( weights , dtype = numpy . float64 ) <EOL> wshape = [ ii for ii in weights . shape if ii > <NUM_LIT:0> ] <EOL> if len ( wshape ) != input . ndim : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if convolution : <EOL> weights = weights [ tuple ( [ slice ( None , None , - <NUM_LIT:1> ) ] * weights . ndim ) ] <EOL> for ii in range ( len ( origins ) ) : <EOL> origins [ ii ] = - origins [ ii ] <EOL> if not weights . shape [ ii ] & <NUM_LIT:1> : <EOL> origins [ ii ] -= <NUM_LIT:1> <EOL> for origin , lenw in zip ( origins , wshape ) : <EOL> if ( lenw // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( lenw // <NUM_LIT:2> + origin > lenw ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not weights . flags . contiguous : <EOL> weights = weights . copy ( ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . correlate ( input , weights , output , mode , cval , origins ) <EOL> return return_value <EOL> @ docfiller <EOL> def correlate ( input , weights , output = None , mode = '<STR_LIT>' , cval = <NUM_LIT:0.0> , <EOL> origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return _correlate_or_convolve ( input , weights , output , mode , cval , <EOL> origin , False ) <EOL> @ docfiller <EOL> def convolve ( input , weights , output = None , mode = '<STR_LIT>' , cval = <NUM_LIT:0.0> , <EOL> origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return _correlate_or_convolve ( input , weights , output , mode , cval , <EOL> origin , True ) <EOL> @ docfiller <EOL> def uniform_filter1d ( input , size , axis = - <NUM_LIT:1> , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> axis = _ni_support . _check_axis ( axis , input . ndim ) <EOL> if size < <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> if ( size // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( size // <NUM_LIT:2> + origin >= size ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . uniform_filter1d ( input , size , axis , output , mode , cval , <EOL> origin ) <EOL> return return_value <EOL> @ docfiller <EOL> def uniform_filter ( input , size = <NUM_LIT:3> , output = None , mode = "<STR_LIT>" , <EOL> cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> sizes = _ni_support . _normalize_sequence ( size , input . ndim ) <EOL> origins = _ni_support . _normalize_sequence ( origin , input . ndim ) <EOL> axes = list ( range ( input . ndim ) ) <EOL> axes = [ ( axes [ ii ] , sizes [ ii ] , origins [ ii ] ) <EOL> for ii in range ( len ( axes ) ) if sizes [ ii ] > <NUM_LIT:1> ] <EOL> if len ( axes ) > <NUM_LIT:0> : <EOL> for axis , size , origin in axes : <EOL> uniform_filter1d ( input , int ( size ) , axis , output , mode , <EOL> cval , origin ) <EOL> input = output <EOL> else : <EOL> output [ ... ] = input [ ... ] <EOL> return return_value <EOL> @ docfiller <EOL> def minimum_filter1d ( input , size , axis = - <NUM_LIT:1> , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> axis = _ni_support . _check_axis ( axis , input . ndim ) <EOL> if size < <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> if ( size // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( size // <NUM_LIT:2> + origin >= size ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . min_or_max_filter1d ( input , size , axis , output , mode , cval , <EOL> origin , <NUM_LIT:1> ) <EOL> return return_value <EOL> @ docfiller <EOL> def maximum_filter1d ( input , size , axis = - <NUM_LIT:1> , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> axis = _ni_support . _check_axis ( axis , input . ndim ) <EOL> if size < <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> if ( size // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( size // <NUM_LIT:2> + origin >= size ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . min_or_max_filter1d ( input , size , axis , output , mode , cval , <EOL> origin , <NUM_LIT:0> ) <EOL> return return_value <EOL> def _min_or_max_filter ( input , size , footprint , structure , output , mode , <EOL> cval , origin , minimum ) : <EOL> if structure is None : <EOL> if footprint is None : <EOL> if size is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> separable = True <EOL> else : <EOL> footprint = numpy . asarray ( footprint ) <EOL> footprint = footprint . astype ( bool ) <EOL> if numpy . alltrue ( numpy . ravel ( footprint ) , axis = <NUM_LIT:0> ) : <EOL> size = footprint . shape <EOL> footprint = None <EOL> separable = True <EOL> else : <EOL> separable = False <EOL> else : <EOL> structure = numpy . asarray ( structure , dtype = numpy . float64 ) <EOL> separable = False <EOL> if footprint is None : <EOL> footprint = numpy . ones ( structure . shape , bool ) <EOL> else : <EOL> footprint = numpy . asarray ( footprint ) <EOL> footprint = footprint . astype ( bool ) <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> origins = _ni_support . _normalize_sequence ( origin , input . ndim ) <EOL> if separable : <EOL> sizes = _ni_support . _normalize_sequence ( size , input . ndim ) <EOL> axes = list ( range ( input . ndim ) ) <EOL> axes = [ ( axes [ ii ] , sizes [ ii ] , origins [ ii ] ) <EOL> for ii in range ( len ( axes ) ) if sizes [ ii ] > <NUM_LIT:1> ] <EOL> if minimum : <EOL> filter_ = minimum_filter1d <EOL> else : <EOL> filter_ = maximum_filter1d <EOL> if len ( axes ) > <NUM_LIT:0> : <EOL> for axis , size , origin in axes : <EOL> filter_ ( input , int ( size ) , axis , output , mode , cval , origin ) <EOL> input = output <EOL> else : <EOL> output [ ... ] = input [ ... ] <EOL> else : <EOL> fshape = [ ii for ii in footprint . shape if ii > <NUM_LIT:0> ] <EOL> if len ( fshape ) != input . ndim : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> for origin , lenf in zip ( origins , fshape ) : <EOL> if ( lenf // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( lenf // <NUM_LIT:2> + origin >= lenf ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not footprint . flags . contiguous : <EOL> footprint = footprint . copy ( ) <EOL> if structure is not None : <EOL> if len ( structure . shape ) != input . ndim : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if not structure . flags . contiguous : <EOL> structure = structure . copy ( ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . min_or_max_filter ( input , footprint , structure , output , <EOL> mode , cval , origins , minimum ) <EOL> return return_value <EOL> @ docfiller <EOL> def minimum_filter ( input , size = None , footprint = None , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return _min_or_max_filter ( input , size , footprint , None , output , mode , <EOL> cval , origin , <NUM_LIT:1> ) <EOL> @ docfiller <EOL> def maximum_filter ( input , size = None , footprint = None , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return _min_or_max_filter ( input , size , footprint , None , output , mode , <EOL> cval , origin , <NUM_LIT:0> ) <EOL> @ docfiller <EOL> def _rank_filter ( input , rank , size = None , footprint = None , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> , operation = '<STR_LIT>' ) : <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> origins = _ni_support . _normalize_sequence ( origin , input . ndim ) <EOL> if footprint is None : <EOL> if size is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> sizes = _ni_support . _normalize_sequence ( size , input . ndim ) <EOL> footprint = numpy . ones ( sizes , dtype = bool ) <EOL> else : <EOL> footprint = numpy . asarray ( footprint , dtype = bool ) <EOL> fshape = [ ii for ii in footprint . shape if ii > <NUM_LIT:0> ] <EOL> if len ( fshape ) != input . ndim : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> for origin , lenf in zip ( origins , fshape ) : <EOL> if ( lenf // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( lenf // <NUM_LIT:2> + origin >= lenf ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not footprint . flags . contiguous : <EOL> footprint = footprint . copy ( ) <EOL> filter_size = numpy . where ( footprint , <NUM_LIT:1> , <NUM_LIT:0> ) . sum ( ) <EOL> if operation == '<STR_LIT>' : <EOL> rank = filter_size // <NUM_LIT:2> <EOL> elif operation == '<STR_LIT>' : <EOL> percentile = rank <EOL> if percentile < <NUM_LIT:0.0> : <EOL> percentile += <NUM_LIT> <EOL> if percentile < <NUM_LIT:0> or percentile > <NUM_LIT:100> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if percentile == <NUM_LIT> : <EOL> rank = filter_size - <NUM_LIT:1> <EOL> else : <EOL> rank = int ( float ( filter_size ) * percentile / <NUM_LIT> ) <EOL> if rank < <NUM_LIT:0> : <EOL> rank += filter_size <EOL> if rank < <NUM_LIT:0> or rank >= filter_size : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if rank == <NUM_LIT:0> : <EOL> return minimum_filter ( input , None , footprint , output , mode , cval , <EOL> origins ) <EOL> elif rank == filter_size - <NUM_LIT:1> : <EOL> return maximum_filter ( input , None , footprint , output , mode , cval , <EOL> origins ) <EOL> else : <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . rank_filter ( input , rank , footprint , output , mode , cval , <EOL> origins ) <EOL> return return_value <EOL> @ docfiller <EOL> def rank_filter ( input , rank , size = None , footprint = None , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return _rank_filter ( input , rank , size , footprint , output , mode , cval , <EOL> origin , '<STR_LIT>' ) <EOL> @ docfiller <EOL> def median_filter ( input , size = None , footprint = None , output = None , <EOL> mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return _rank_filter ( input , <NUM_LIT:0> , size , footprint , output , mode , cval , <EOL> origin , '<STR_LIT>' ) <EOL> @ docfiller <EOL> def percentile_filter ( input , percentile , size = None , footprint = None , <EOL> output = None , mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return _rank_filter ( input , percentile , size , footprint , output , mode , <EOL> cval , origin , '<STR_LIT>' ) <EOL> @ docfiller <EOL> def generic_filter1d ( input , function , filter_size , axis = - <NUM_LIT:1> , <EOL> output = None , mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> , <EOL> extra_arguments = ( ) , extra_keywords = None ) : <EOL> """<STR_LIT>""" <EOL> if extra_keywords is None : <EOL> extra_keywords = { } <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> if filter_size < <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> axis = _ni_support . _check_axis ( axis , input . ndim ) <EOL> if ( filter_size // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( filter_size // <NUM_LIT:2> + origin >= <EOL> filter_size ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . generic_filter1d ( input , function , filter_size , axis , output , <EOL> mode , cval , origin , extra_arguments , extra_keywords ) <EOL> return return_value <EOL> @ docfiller <EOL> def generic_filter ( input , function , size = None , footprint = None , <EOL> output = None , mode = "<STR_LIT>" , cval = <NUM_LIT:0.0> , origin = <NUM_LIT:0> , <EOL> extra_arguments = ( ) , extra_keywords = None ) : <EOL> """<STR_LIT>""" <EOL> if extra_keywords is None : <EOL> extra_keywords = { } <EOL> input = numpy . asarray ( input ) <EOL> if numpy . iscomplexobj ( input ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> origins = _ni_support . _normalize_sequence ( origin , input . ndim ) <EOL> if footprint is None : <EOL> if size is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> sizes = _ni_support . _normalize_sequence ( size , input . ndim ) <EOL> footprint = numpy . ones ( sizes , dtype = bool ) <EOL> else : <EOL> footprint = numpy . asarray ( footprint ) <EOL> footprint = footprint . astype ( bool ) <EOL> fshape = [ ii for ii in footprint . shape if ii > <NUM_LIT:0> ] <EOL> if len ( fshape ) != input . ndim : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> for origin , lenf in zip ( origins , fshape ) : <EOL> if ( lenf // <NUM_LIT:2> + origin < <NUM_LIT:0> ) or ( lenf // <NUM_LIT:2> + origin >= lenf ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not footprint . flags . contiguous : <EOL> footprint = footprint . copy ( ) <EOL> output , return_value = _ni_support . _get_output ( output , input ) <EOL> mode = _ni_support . _extend_mode_to_code ( mode ) <EOL> _nd_image . generic_filter ( input , function , footprint , output , mode , <EOL> cval , origins , extra_arguments , extra_keywords ) <EOL> return return_value </s>
