def test_tag_propagation(): x = Collection.from_array([1, 2, 3], tags=Tags({quantity: 'acceleration', body_part: 'arm_left'})) y = Collection.from_array([1, 2, 3], tags=Tags({quantity: 'acceleration', body_part: 'arm_right'})) z = Collection.from_array([1, 2, 3], tags=Tags({quantity: 'position', body_part: 'leg_right'})) assert quantity in Ratio().transform(x, y).tags assert Ratio().transform(x, y).tags.quantity == 'acceleration' assert body_part in Ratio().transform(x, y).tags assert Ratio().transform(x, y).tags.body_part == 'arm' assert body_part in Ratio().transform(y, z).tags assert Ratio().transform(y, z).tags.body_part == 'right' assert quantity not in Ratio().transform(x, z).tags assert body_part not in Ratio().transform(x, z).tags
def test_entropy(x): x = Collection.from_array(np.abs(x.values) + 1) # ensure that values > 0 result = Entropy().transform(x) for i, a in series(x): actual = result.values[i] expected = stats.entropy(a) np.testing.assert_almost_equal(actual, expected)
def test_sample_entropy(x): x = Collection.from_array([1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5]) result = SampleEntropy().transform(x) actual = result.values expected = 0.55961579 # Computed using Physionet sample entropy implementation: # https://physionet.org/content/sampen/1.0.0/ np.testing.assert_almost_equal(actual, expected)
def test_tags(): x = Collection.from_array([1, 2, 3]) x.tags.add(quantity, 'acceleration') assert x.tags[quantity] == 'acceleration' assert x.tags['quantity'] == 'acceleration' assert x.tags.quantity == 'acceleration' x.tags.remove(quantity) assert x.tags[quantity] is None assert x.tags['quantity'] is None
def test_energy_ratio(): x = Collection.from_array([1, 2, 3, 4, 5, 6, 7, 8]) chunks = [[1, 2, 3], [4, 5, 6], [7, 8]] result = EnergyRatio(chunks=3).transform(x).values assert result.shape == (1, 1, 3) total = np.sum(np.square([1, 2, 3, 4, 5, 6, 7, 8])) np.testing.assert_almost_equal(result[0, 0, 0], np.sum(np.square(chunks[0])) / total) np.testing.assert_almost_equal(result[0, 0, 1], np.sum(np.square(chunks[1])) / total) np.testing.assert_almost_equal(result[0, 0, 2], np.sum(np.square(chunks[2])) / total)
def test_to_dataframe_attributes(): collection = Collection.from_array([ [[10, 20, 30]], [[40, 50, 60]], ], dims=['x', 'y', 'z']) print(collection.id) df = collection.to_dataframe() np.testing.assert_equal(df.columns.values, ['id', 'x', 'y', 'z']) np.testing.assert_equal(df.values, [ [0, 10, 20, 30], [1, 40, 50, 60], ])
def brownian(N=10, t=100, d=2, random_state=None, **kwargs): """ Generate Brownian motion data. The implementation is based on the SciPy Cookbook. [1]_ Parameters ---------- N : int, optional Number of windows. Default: 10 t : int, optional Number of time stamps in each window. Default: 100 d : int, optional Number of dimensions. Default: 2 random_state : int, optional Random state initialization. **kwargs Keyword arguments to pass to the :class:`~tsfuse.data.Collection` constructor. Returns ------- generated : Collection References ---------- .. [1] https://scipy-cookbook.readthedocs.io/items/BrownianMotion.html """ if random_state is not None: np.random.seed(random_state) def generate(n, delta=0.25, dt=0.1, initial=0.0): x = np.empty(n) x[0] = initial for k in range(1, n): x[k] = x[k - 1] + norm.rvs(scale=delta**2 * dt) return x values = np.empty((N, t, d), dtype=float) for i in range(N): for j in range(d): values[i, :, j] = generate(t) return Collection.from_array(values, **kwargs)
def test_to_dataframe_time_series_variable_length(): collection = Collection.from_array([ [[10, 20, 30], [11, 21, 31], [12, 22, 33],], [[40, 50, 60], [41, 51, 61],], ], time=[ [0, 1, 2], [0, 1], ], dims=['x', 'y', 'z']) df = collection.to_dataframe() np.testing.assert_equal(df.columns.values, ['id', 'time', 'x', 'y', 'z']) np.testing.assert_equal(df.values, [ [0, 0, 10, 20, 30], [0, 1, 11, 21, 31], [0, 2, 12, 22, 33], [1, 0, 40, 50, 60], [1, 1, 41, 51, 61], ])
def test_has_duplicate_max_false(): x = Collection.from_array([1, 1, 2, 3]) actual = HasDuplicateMax().transform(x).values np.testing.assert_equal(actual, False)
def test_roots(): x = Collection.from_array([-1, 0, 1]) actual = Roots().transform(x).values np.testing.assert_almost_equal(actual.flatten(), [1, -1])
def test_create_collection_series(): x = Collection.from_array([[1, 2, 3], [4, 5, 6]]) assert x.shape == (1, 2, 3) assert x.type == Type.SERIES
def test_number_of_peaks_support_1(): x = Collection.from_array([1, 2, 1, 2, 3, 2, 3]) actual = NumberPeaks(support=1).transform(x).values np.testing.assert_equal(actual, 2)
def test_arg_max(): x = Collection.from_array([1, 2, 3, 4, 3, 2, 1]) actual = ArgMax().transform(x).values np.testing.assert_equal(actual, 3)
def test_longest_strike_above_mean_three(): x = Collection.from_array([0, 0, 0, 2, 2, 0, 0, 2, 2, 2, 0, 0, 2, 0]) actual = LongestStrikeAboveMean().transform(x).values np.testing.assert_equal(actual, 3)
def test_number_crossings_one(): x = Collection.from_array([0, 2, 3, 4]) actual = NumberCrossings().transform(x).values np.testing.assert_equal(actual, 1)
def test_create_collection_windows(): x = Collection.from_array([[[1, 2, 3], [4, 5, 6]], [[2, 3, 4], [5, 6, 7]]]) assert x.shape == (2, 2, 3) assert x.type == Type.WINDOWS
def test_create_collection_windows_variable_length(): x = Collection.from_array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[2, 3, 4], [5, 6, 7]]]) assert x.shape == (2, (3, 2), 3) assert x.type == Type.WINDOWS
def test_has_duplicate_min_false(): x = Collection.from_array([2, 3, 4, 4]) actual = HasDuplicateMin().transform(x).values np.testing.assert_equal(actual, False)
def test_has_duplicate_empty(): x = Collection.from_array([np.nan]) actual = HasDuplicate().transform(x).values np.testing.assert_equal(actual, False)
def test_has_duplicate_true(): x = Collection.from_array([1, 2, 3, 3]) actual = HasDuplicate().transform(x).values np.testing.assert_equal(actual, True)
def test_value_count(): x = Collection.from_array([1, 1, 2, 3]) actual = ValueCount(value=1).transform(x).values np.testing.assert_equal(actual, 2)
def test_number_of_unique_values_1_rel(): x = Collection.from_array([2, 2, 2, 2]) actual = NumberUniqueValues(rel=True).transform(x).values np.testing.assert_equal(actual, 0.25)
def test_longest_strike_above_mean_zero(): x = Collection.from_array([1, 1, 1]) actual = LongestStrikeAboveMean().transform(x).values np.testing.assert_equal(actual, 0)
def test_number_of_unique_values_0_abs(): x = Collection.from_array([np.nan]) actual = NumberUniqueValues(rel=False).transform(x).values np.testing.assert_equal(actual, np.nan)
def test_min(): x = Collection.from_array([4, 3, 2, 1, 2, 3, 4]) actual = Min().transform(x).values np.testing.assert_equal(actual, 1)
def test_sum_of_reoccurring_data_points_0(): x = Collection.from_array([1, 2, 3, 4]) actual = SumReoccurringDataPoints().transform(x).values np.testing.assert_equal(actual, 0)
def test_angle_perpendicular(): p1 = Collection.from_array([1, 0]) p2 = Collection.from_array([0, 0]) p3 = Collection.from_array([0, 2]) actual = Angle().transform(p1, p2, p3, ignore_preconditions=True).values np.testing.assert_almost_equal(np.mod(actual, np.pi), np.pi / 2)
def test_sum_of_reoccurring_values(): x = Collection.from_array([1, 1, 2, 3, 3, 4]) actual = SumReoccurringValues().transform(x).values np.testing.assert_equal(actual, 4)
def test_number_of_peaks_support_2_zero(): x = Collection.from_array([3, 2, 1, 0]) actual = NumberPeaks(support=2).transform(x).values np.testing.assert_equal(actual, 0)
def test_create_collection_attributes(): x = Collection.from_array([1, 2, 3]) assert x.shape == (1, 1, 3) assert x.type == Type.ATTRIBUTES