def test_concatenate(): dm = DataMatrix(length=1) dm.s1 = SeriesColumn(depth=3) dm.s1[:] = 1, 2, 3 dm.s2 = SeriesColumn(depth=3) dm.s2[:] = 3, 2, 1 dm.s = series.concatenate(dm.s1, dm.s2) check_series(dm.s, [[1, 2, 3, 3, 2, 1]])
def test_window(): dm = DataMatrix(length=2) dm.series = SeriesColumn(depth=4) dm.series[0] = 0, 1, 1, 0 dm.series[1] = 0, 2, 2, 0 dm.window = series.window(dm.series, 1, 3) check_series(dm.window, [[1, 1], [2, 2]]) check_integrity(dm)
def test_group(): dm = DataMatrix(length=4) dm.a = 'b', 'b', 'a', 'a' dm.b = 'x', 'x', 'x', 'y' dm.c = IntColumn dm.c = 0, 1, 2, 3 dm = ops.group(dm, [dm.a, dm.b]) check_series(dm.c, [[2, np.nan], [3, np.nan], [0, 1]])
def test_group(): dm = DataMatrix(length=4) dm.a = 'b', 'b', 'a', 'a' dm.b = 'x', 'x', 'x', 'y' dm.c = IntColumn dm.c = 0, 1, 2, 3 dm = ops.group(dm, [dm.a, dm.b]) check_series(dm.c, [[3, np.nan], [2, np.nan], [0, 1]]) # Order guaranteed?
def test_lock(): dm = DataMatrix(length=2) dm.s = SeriesColumn(depth=3) dm.s[0] = 1, 2, 3 dm.s[1] = -1, -2, -3 dm.l, zero_point = series.lock(dm.s, [-1, 1]) assert zero_point == 1 check_series(dm.l, [[np.nan, np.nan, 1, 2, 3], [-1, -2, -3, np.nan, np.nan]])
def test_interpolate(): dm = DataMatrix(length=3) dm.s = SeriesColumn(depth=4) dm.s = 1, 2, 3, 4 dm.s[0] = np.nan dm.s[1, 0] = np.nan dm.s[1, 2] = np.nan dm.i = series.interpolate(dm.s) check_series(dm.i, [[np.nan] * 4, [2, 2, 3, 4], [1, 2, 3, 4]])
def test_downsample(): dm = DataMatrix(length=2) dm.series = SeriesColumn(depth=10) dm.series[0] = range(10) dm.series[1] = [0, 1] * 5 dm.d3 = series.downsample(dm.series, 3) dm.d5 = series.downsample(dm.series, 5) check_series(dm.d3, [[1, 4, 7], [1. / 3, 2. / 3, 1. / 3]]) check_series(dm.d5, [[2, 7], [.4, .6]]) check_integrity(dm)
def test_smooth(): dm = DataMatrix(length=2) dm.series = SeriesColumn(depth=6) dm.series[0] = range(6) dm.series[1] = [0, 1, 2] * 2 dm.s = series.smooth(dm.series, winlen=3, wintype='flat') check_series( dm.s, [[2. / 3, 1, 2, 3, 4, 4 + 1. / 3], [2. / 3, 1, 1, 1, 1, 1 + 1. / 3]]) check_integrity(dm)
def test_normalize_time(): dm = DataMatrix(length=2) dm.s = SeriesColumn(depth=2) dm.s[0] = 1, 2 dm.s[1] = np.nan, 3 dm.t = SeriesColumn(depth=2) dm.t[0] = 0, 3 dm.t[1] = 1, 2 dm.n = series.normalize_time(dm.s, dm.t) check_series(dm.n, [[1, np.nan, np.nan, 2], [np.nan, np.nan, 3, np.nan]])
def test_baseline(): dm = DataMatrix(length=2) dm.series = SeriesColumn(depth=3) dm.series[0] = range(3) dm.series[1] = range(1, 4) dm.baseline = SeriesColumn(depth=3) dm.baseline[0] = range(1, 4) dm.baseline[1] = range(3) dm.norm = series.baseline(dm.series, dm.baseline) check_series(dm.norm, [[-2, -1, 0], [0, 1, 2]]) check_integrity(dm)
def test_endlock(): dm = DataMatrix(length=4) dm.series = SeriesColumn(depth=3) dm.series[0] = 1, 2, 3 dm.series[1] = 1, np.nan, 3 dm.series[2] = 1, 2, np.nan dm.series[3] = np.nan, 2, np.nan dm.series = series.endlock(dm.series) check_series(dm.series, [ [1,2,3], [1,np.nan,3], [np.nan,1,2], [np.nan,np.nan,2], ])
def test_group(): dm = DataMatrix(length=4) dm.a = 'b', 'b', 'a', 'a' dm.b = 'x', 'x', 'x', 'y' dm.c = IntColumn dm.c = 0, 1, 2, 3 dm = ops.group(dm, [dm.a, dm.b]) # Assert that at least one of the permutations passes for ref in itertools.permutations([[3, np.nan], [2, np.nan], [0, 1]]): try: check_series(dm.c, ref) break except AssertionError: pass else: assert (False)
def test_replace(): dm = DataMatrix(length=3) dm.a = 0, 1, 2 dm.c = FloatColumn dm.c = np.nan, 1, 2 dm.s = SeriesColumn(depth=3) dm.s[0] = 0, 1, 2 dm.s[1] = np.nan, 1, 2 dm.s[2] = np.nan, 1, 2 dm.a = ops.replace(dm.a, {0: 100, 2: 200}) dm.c = ops.replace(dm.c, {np.nan: 100, 2: np.nan}) dm.s = ops.replace(dm.s, {np.nan: 100, 2: np.nan}) check_col(dm.a, [100, 1, 200]) check_col(dm.c, [100, 1, np.nan]) check_series(dm.s, [ [0, 1, np.nan], [100, 1, np.nan], [100, 1, np.nan], ])
def test_seriescolumn(): dm1 = DataMatrix(length=2) dm1.col1 = SeriesColumn(2) dm1.col1 = 1, 2 dm1.col_shared = SeriesColumn(2) dm1.col_shared = 3, 4 dm2 = DataMatrix(length=2) dm2.col2 = SeriesColumn(2) dm2.col2 = 5, 6 dm2.col_shared = SeriesColumn(2) dm2.col_shared = 7, 8 dm3 = dm1 << dm2 check_series(dm3.col1, [[1, 1], [2, 2], [np.nan, np.nan], [np.nan, np.nan]]) check_series(dm3.col_shared, [[3, 3], [4, 4], [7, 7], [8, 8]]) check_series(dm3.col2, [[np.nan, np.nan], [np.nan, np.nan], [5, 5], [6, 6]]) dm3.i = [4, 0, 2, 1] dm4 = dm3.i <= 2 dm5 = (dm3.i <= 2) | (dm3.i >= 3) check_integrity(dm1) check_integrity(dm2) check_integrity(dm3) check_integrity(dm4) check_integrity(dm5)
def test_threshold(): dm = DataMatrix(length=2) dm.series = SeriesColumn(depth=4) dm.series[0] = range(4) dm.series[1] = range(1, 5) dm.t1 = series.threshold(dm.series, lambda v: v > 1) dm.t2 = series.threshold(dm.series, lambda v: v > 1 and v < 3) dm.t3 = series.threshold(dm.series, lambda v: v < 3, min_length=3) check_series(dm.t1, [[0, 0, 1, 1], [0, 1, 1, 1]]) check_series(dm.t2, [[0, 0, 1, 0], [0, 1, 0, 0]]) check_series(dm.t3, [[1, 1, 1, 0], [0, 0, 0, 0]]) check_integrity(dm)
def test_seriescolumn(): dm1 = DataMatrix(length=2) dm1.col1 = SeriesColumn(2) dm1.col1 = 1, 2 dm1.col_shared = SeriesColumn(2) dm1.col_shared = 3, 4 dm2 = DataMatrix(length=2) dm2.col2 = SeriesColumn(2) dm2.col2 = 5, 6 dm2.col_shared = SeriesColumn(2) dm2.col_shared = 7, 8 dm3 = dm1 << dm2 check_series(dm3.col1, [[1,1],[2,2],[0,0],[0,0]]) check_series(dm3.col_shared, [[3,3],[4,4],[7,7],[8,8]]) check_series(dm3.col2, [[0,0],[0,0],[5,5],[6,6]]) dm3.i = [4,0,2,1] dm4 = dm3.i <= 2 dm5 = (dm3.i <= 2) | (dm3.i >= 3) check_integrity(dm1) check_integrity(dm2) check_integrity(dm3) check_integrity(dm4) check_integrity(dm5)
def test_seriescolumn(): dm = DataMatrix(length=2) dm.col = SeriesColumn(depth=3) # Set all rows to a single value dm.col = 1 check_series(dm.col, [[1, 1, 1], [1, 1, 1]]) # Set rows to different single values dm.col = 2, 3 check_series(dm.col, [[2, 2, 2], [3, 3, 3]]) # Set one row to a single value dm.col[0] = 4 check_series(dm.col, [[4, 4, 4], [3, 3, 3]]) # Set one row to different single values dm.col[1] = 5, 6, 7 check_series(dm.col, [[4, 4, 4], [5, 6, 7]]) # Set all rows to different single values dm.col.setallrows([8, 9, 10]) check_series(dm.col, [[8, 9, 10], [8, 9, 10]]) # Set the first value in all rows dm.col[:, 0] = 1 check_series(dm.col, [[1, 9, 10], [1, 9, 10]]) # Set all values in the first row dm.col[0, :] = 2 check_series(dm.col, [[2, 2, 2], [1, 9, 10]]) # Set all values dm.col[:, :] = 3 check_series(dm.col, [[3, 3, 3], [3, 3, 3]]) # Test shortening and lengthening dm.length = 0 check_series(dm.col, []) dm.length = 3 dm.col = 1, 2, 3 dm.col.depth = 1 check_series(dm.col, [[1], [2], [3]]) dm.col.depth = 3 check_series(dm.col, [[1, 0, 0], [2, 0, 0], [3, 0, 0]]) check_integrity(dm)
def test_seriescolumn(): _test_copying(SeriesColumn(depth=1)) dm = DataMatrix(length=2) dm.col = SeriesColumn(depth=3) # Set all rows to a single value dm.col = 1 check_series(dm.col, [[1, 1, 1], [1, 1, 1]]) # Set rows to different single values dm.col = 2, 3 check_series(dm.col, [[2, 2, 2], [3, 3, 3]]) # Set one row to a single value dm.col[0] = 4 check_series(dm.col, [[4, 4, 4], [3, 3, 3]]) # Set one row to different single values dm.col[1] = 5, 6, 7 check_series(dm.col, [[4, 4, 4], [5, 6, 7]]) # Set all rows to different single values dm.col.setallrows([8, 9, 10]) check_series(dm.col, [[8, 9, 10], [8, 9, 10]]) # Set the first value in all rows dm.col[:, 0] = 1 check_series(dm.col, [[1, 9, 10], [1, 9, 10]]) # Set all values in the first row dm.col[0, :] = 2 check_series(dm.col, [[2, 2, 2], [1, 9, 10]]) # Set all values dm.col[:, :] = 3 check_series(dm.col, [[3, 3, 3], [3, 3, 3]]) # Test shortening and lengthening dm.length = 0 check_series(dm.col, []) dm.length = 3 dm.col = 1, 2, 3 dm.col.depth = 1 check_series(dm.col, [[1], [2], [3]]) dm.col.depth = 3 check_series(dm.col, [[1, NAN, NAN], [2, NAN, NAN], [3, NAN, NAN]]) check_integrity(dm) # Test dm = DataMatrix(length=2) dm.col = SeriesColumn(depth=3) dm.col = 1, 2 check_series(dm.col, [[1, 1, 1], [2, 2, 2]]) dm.col = 3, 4, 5 check_series(dm.col, [[3, 4, 5]] * 2) dm.col.depth = 2 dm.col[:] = 1, 2 check_series(dm.col, [[1, 1], [2, 2]]) dm.col[:, :] = 3, 4 check_series(dm.col, [[3, 4], [3, 4]]) # Check if series return right type dm = DataMatrix(length=4) dm.col = SeriesColumn(depth=5) dm.col = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]] # (int, int) -> float val = dm.col[2, 2] eq_(val, 13) eq_(type(val), float) # (int) -> array val = dm.col[2] ok_(all(val == np.array([11, 12, 13, 14, 15]))) eq_(type(val), np.ndarray) # (int, slice) -> array val = dm.col[2, 1:-1] ok_(all(val == np.array([12, 13, 14]))) eq_(type(val), np.ndarray) # (int, (int, int)) -> array val = dm.col[2, (1, 3)] ok_(all(val == np.array([12, 14]))) eq_(type(val), np.ndarray) # (slice) -> SeriesColumn val = dm.col[1:-1] check_series(val, [ [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], ]) # (slice, int) -> FloatColumn val = dm.col[1:-1, 2] ok_(isinstance(val, FloatColumn)) check_col(val, [8, 13]) # ((int, int), int) -> FloatColumn val = dm.col[(1, 3), 2] ok_(isinstance(val, FloatColumn)) check_col(val, [8, 18]) # (slice, slice) -> SeriesColumn val = dm.col[1:-1, 1:-1] ok_(isinstance(val, _SeriesColumn)) check_series(val, [ [7, 8, 9], [12, 13, 14], ]) # ((int, int), slice) -> SeriesColumn val = dm.col[(1, 3), 1:-1] ok_(isinstance(val, _SeriesColumn)) check_series(val, [ [7, 8, 9], [17, 18, 19], ]) # ((int, int), (int int)) -> SeriesColumn val = dm.col[(1, 3), (1, 3)] ok_(isinstance(val, _SeriesColumn)) check_series(val, [ [7, 9], [17, 19], ])
def test_seriescolumn(): dm = DataMatrix(length=2) dm.col = SeriesColumn(depth=2) dm.col[0] = 1, 2 dm.col[1] = 3, 4 dm.col += 1 check_series(dm.col, [[2,3], [4,5]]) dm.col += 1, 2 check_series(dm.col, [[3,4], [6,7]]) dm.col -= 1 check_series(dm.col, [[2,3], [5,6]]) dm.col -= 1, 2 check_series(dm.col, [[1,2], [3,4]]) dm.col *= 2 check_series(dm.col, [[2,4], [6,8]]) dm.col *= 1.5, 3 check_series(dm.col, [[3,6], [18,24]]) dm.col /= 3 check_series(dm.col, [[1,2], [6,8]]) dm.col /= 1, 2 check_series(dm.col, [[1,2], [3,4]]) dm.col //= 1.5, 2.5 check_series(dm.col, [[0,1], [1,1]]) dm.col += np.array([ [0,0], [10, 10] ]) check_series(dm.col, [[0,1], [11,11]]) # Right-side operations dm.col[0] = 1, 2 dm.col[1] = 3, 4 dm.col = 1 + dm.col check_series(dm.col, [[2,3], [4,5]]) dm.col = (1, 2) + dm.col check_series(dm.col, [[3,4], [6,7]]) dm.col = 1 - dm.col check_series(dm.col, [[-2,-3], [-5,-6]]) dm.col = (1, 2) - dm.col check_series(dm.col, [[3, 4], [7, 8]]) dm.col = 2 * dm.col check_series(dm.col, [[6, 8], [14, 16]]) dm.col = (1.5, 3) * dm.col check_series(dm.col, [[9, 12], [42, 48]]) dm.col = 3 / dm.col check_series(dm.col, [[1./3, 1./4], [3./42, 1./16]]) dm.col = (1, 2) / dm.col check_series(dm.col, [[3, 4], [28, 32]]) dm.col = (1.5, 2.5) // dm.col check_series(dm.col, [[0, 0], [0, 0]]) dm.col = np.array([ [0, 0], [10, 10] ]) + dm.col check_series(dm.col, [[0, 0], [10, 10]])
def test_seriescolumn(): dm = DataMatrix(length=2) dm.col = SeriesColumn(depth=2) dm.col[0] = 1, 2 dm.col[1] = 3, 4 dm.col += 1 check_series(dm.col, [[2,3], [4,5]]) dm.col += 1, 2 check_series(dm.col, [[3,4], [6,7]]) dm.col -= 1 check_series(dm.col, [[2,3], [5,6]]) dm.col -= 1, 2 check_series(dm.col, [[1,2], [3,4]]) dm.col *= 2 check_series(dm.col, [[2,4], [6,8]]) dm.col *= 1.5, 3 check_series(dm.col, [[3,6], [18,24]]) dm.col /= 3 check_series(dm.col, [[1,2], [6,8]]) dm.col /= 1, 2 check_series(dm.col, [[1,2], [3,4]]) dm.col //= 1.5, 2.5 check_series(dm.col, [[0,1], [1,1]]) dm.col += np.array([ [0,0], [10, 10] ]) check_series(dm.col, [[0,1], [11,11]])