def assert_zeroish_mean_per_segment(wf): data = wf.data values = data.values finite_segs = get_finite_segments(values) for bp in finite_segs.bp: ar = finite_segs.flat[bp[0]:bp[1]] seg_mean = np.nanmean(ar) assert np.allclose(seg_mean, 0)
def test_no_nan(self): """very simple test with No Nans""" ar = np.arange(100).reshape(10, 10) nns = get_finite_segments(ar) # no Nans so flat should be the same size assert nns.flat.size == ar.size for ind in nns.ind: # each segment should span exactly one row assert ind[0][1] - ind[0][0] == 1 # and 10 columns assert ind[1][1] - ind[1][0] == 10
def _linear_detrend_with_nan(self, values, finite, method): """ Apply linear detrend to data which have NaNs. """ # init array for output out = values.copy() # get finite segments nns = get_finite_segments(values, finite=finite) detrended = scipy.signal.detrend(nns.flat, type=method, bp=nns.bp[:, 1]) assert np.shape(detrended) == np.shape(nns.flat) # put humpty dumpty back together again for flat_ind, ind in zip(nns.bp, nns.ind): # get slice out of detrended result ar = detrended[flat_ind[0]:flat_ind[1]] out[ind[0][0]:ind[0][1], ind[1][0]:ind[1][1]] = ar return out
def test_with_nan(self): """test with one NaN.""" # get test data with one NaN in first row ar = np.arange(100).reshape(10, 10).astype(float) ar[0, 5] = np.NaN ar[-1, -1] = np.Inf nns = get_finite_segments(ar) assert nns.flat.size == ar.size - 2 # there should now be 11 segments (2 from first row) assert len(nns.bp) == 11 # first row should have two segments first, second = nns.ind[0], nns.ind[1] assert first[1][1] - first[1][0] <= 5 assert second[1][1] - second[1][0] <= 5 # last row should only be 9 long last = nns.ind[-1] assert last[1][1] - last[1][0] == 9
def test_all_nan(self): """ensure all NaN raises.""" ar = np.empty(100).reshape(10, 10) * np.NaN with pytest.raises(ValueError): get_finite_segments(ar)