def movingrank(x, window, axis=-1): """Moving rank (normalized to -1 and 1) of a given window along axis. Normalized for missing (NaN) data. A data point with NaN data is returned as NaN If a window is all NaNs except last, this is returned as NaN """ if window > x.shape[axis]: raise ValueError, 'Window is too big.' if window < 2: raise ValueError, 'Window is too small.' nt = x.shape[axis] mr = np.nan * np.zeros(x.shape) for i in xrange(window-1, nt): index1 = [slice(None)] * x.ndim index1[axis] = i index2 = [slice(None)] * x.ndim index2[axis] = slice(i-window+1, i+1, None) mr[index1] = np.squeeze(lastrank(x[index2], axis=axis)) return mr
def test_lastrank_3(self): "farray.lastrank_3" s = lastrank(np.array([])) aae(s, np.nan, err_msg="size 0 lastrank fail")
def test_lastrank_2(self): "farray.lastrank_2" s = lastrank(np.array([[1, 3, 2], [1, 2, 3]])) aae(s, np.array([0.0, 1.0]), err_msg="2d lastrank fail")
def test_lastrank_1(self): "farray.lastrank_1" s = lastrank(np.array([1, 3, 2])) aae(s, 0.0, err_msg="1d lastrank fail")