Exemple #1
0
def demedian(arr, axis=None):
    """
    Subtract the median along the specified axis.
    
    Parameters
    ----------
    arr : ndarray
        Input array.
    axis : {int, None}, optional
        The axis along which to remove the median. The default (None) is
        to subtract the median of the flattened array.
    
    Returns
    -------
    y : ndarray
        A copy with the median along the specified axis removed.
    
    Examples
    --------
    >>> arr = np.array([1, np.nan, 2, 10])
    >>> demedian(arr)
    array([ -1.,  NaN,   0.,   8.])        
    
    """
    # Adapted from pylab.demean
    if axis != 0 and not axis is None:
        ind = [slice(None)] * arr.ndim
        ind[axis] = np.newaxis
        arr = arr - nanmedian(arr, axis)[ind]
    else:
        arr = arr - nanmedian(arr, axis)   
    return arr
Exemple #2
0
def group_median(x, groups, axis=0):
    """
    Median with groups along an axis.
    
    Parameters
    ----------
    x : ndarray
        Input data.
    groups : list
        List of group membership of each element along the given axis.
    axis : int, {default: 0}
        axis along which the ranking is calculated.
        
    Returns
    -------
    idx : ndarray
        The group median of the data along axis 0.

    """

    # Find set of unique groups
    ugroups = unique_group(groups)
    
    # Convert groups to a numpy array
    groups = np.asarray(groups)    
  
    # Loop through unique groups and normalize
    xmedian = np.nan * np.zeros(x.shape)
    for group in ugroups:
        idx = groups == group
        idxall = [slice(None)] * x.ndim
        idxall[axis] = idx
        if idx.sum() > 0:
            ns = nanmedian(x[idxall], axis=axis)
            xmedian[idxall] = np.expand_dims(ns, axis)
            
    return xmedian
Exemple #3
0
def group_median(x, groups, axis=0):
    """
    Median with groups along an axis.
    
    Parameters
    ----------
    x : ndarray
        Input data.
    groups : list
        List of group membership of each element along the given axis.
    axis : int, {default: 0}
        axis along which the ranking is calculated.
        
    Returns
    -------
    idx : ndarray
        The group median of the data along axis 0.

    """

    # Find set of unique groups
    ugroups = unique_group(groups)

    # Convert groups to a numpy array
    groups = np.asarray(groups)

    # Loop through unique groups and normalize
    xmedian = np.nan * np.zeros(x.shape)
    for group in ugroups:
        idx = groups == group
        idxall = [slice(None)] * x.ndim
        idxall[axis] = idx
        if idx.sum() > 0:
            ns = nanmedian(x[idxall], axis=axis)
            xmedian[idxall] = np.expand_dims(ns, axis)

    return xmedian
Exemple #4
0
 def test_nanmedian_all(self):
     """Check nanmedian when all values are nan."""
     m = nanmedian(self.Xall)
     assert np.isnan(m)
Exemple #5
0
 def test_nanmedian_some(self):
     """Check nanmedian when some values only are nan."""
     m = nanmedian(self.Xsome)
     assert_approx_equal(m, np.median(self.Xsomet))
Exemple #6
0
 def test_nanmedian_none(self):
     """Check nanmedian when no values are nan."""
     m = nanmedian(self.X)
     assert_approx_equal(m, np.median(self.X))
Exemple #7
0
 def test_nanmedian_all(self):
     """Check nanmedian when all values are nan."""
     m = nanmedian(self.Xall)
     assert np.isnan(m)
Exemple #8
0
 def test_nanmedian_some(self):
     """Check nanmedian when some values only are nan."""
     m = nanmedian(self.Xsome)
     assert_approx_equal(m, np.median(self.Xsomet))
Exemple #9
0
 def test_nanmedian_none(self):
     """Check nanmedian when no values are nan."""
     m = nanmedian(self.X)
     assert_approx_equal(m, np.median(self.X))