#NEWLINE# def rankdata( data , axis = None , use_missing = False ) : #NEWLINE# #INDENT# 'Returns the rank (also known as order statistics) of each data point\n    along the given axis.\n\n    If some values are tied, their rank is averaged.\n    If some values are masked, their rank is set to 0 if use_missing is False,\n    or set to the average rank of the unmasked values if use_missing is True.\n\n    Parameters\n    ----------\n    data : sequence\n        Input data. The data is transformed to a masked array\n    axis : {None,int}, optional\n        Axis along which to perform the ranking.\n        If None, the array is first flattened. An exception is raised if\n        the axis is specified for arrays with a dimension larger than 2\n    use_missing : bool, optional\n        Whether the masked values have a rank of 0 (False) or equal to the\n        average rank of the unmasked values (True).\n\n    ' #NEWLINE# def _rank1d( data , use_missing = False ) : #NEWLINE# #INDENT# n = data . count ( ) #NEWLINE# rk = np . empty ( data . size , dtype = float ) #NEWLINE# idx = data . argsort ( ) #NEWLINE# rk [ idx [ : n ] ] = np . arange ( 1 , ( n + 1 ) ) #NEWLINE# if use_missing : #NEWLINE# #INDENT# rk [ idx [ n : ] ] = ( ( n + 1 ) / 2.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# rk [ idx [ n : ] ] = 0 #NEWLINE# #UNINDENT# repeats = find_repeats ( data . copy ( ) ) #NEWLINE# for r in repeats [ 0 ] : #NEWLINE# #INDENT# condition = ( data == r ) . filled ( False ) #NEWLINE# rk [ condition ] = rk [ condition ] . mean ( ) #NEWLINE# #UNINDENT# return rk #NEWLINE# #UNINDENT# data = ma . array ( data , copy = False ) #NEWLINE# if ( axis is None ) : #NEWLINE# #INDENT# if ( data . ndim > 1 ) : #NEWLINE# #INDENT# return _rank1d ( data . ravel ( ) , use_missing ) . reshape ( data . shape ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return _rank1d ( data , axis ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# return ma . apply_along_axis ( _rank1d , axis , data , use_missing ) . view ( ndarray )