def test_high_n_bins_entropy(): """Test that an error is raised when 'n_bins' is too large.""" msg_error = ("The number of bins is too high for timestamp {0}. " "Consider trying with a smaller number of bins or " "removing this timestamp.".format(0)) mcb = MultipleCoefficientBinning(n_bins=9, strategy='entropy') with pytest.raises(ValueError, match=msg_error): mcb.fit(X, y)
def test_identical_bin_edges(): """Test that an error is raised when two consecutive bins are equal.""" msg_error = ("At least two consecutive quantiles are equal. " "Consider trying with a smaller number of bins or " "removing timestamps with low variation.") mcb = MultipleCoefficientBinning(n_bins=6, strategy='quantile') with pytest.raises(ValueError, match=msg_error): mcb.fit(np.r_[np.zeros((4, 2)), np.ones((4, 2))])
def test_consistent_length(): """Test that a ValueError is raised with a constant sample.""" msg_error = ("The number of timestamps in X must be the same as " "the number of timestamps when `fit` was called " "({0} != {1}).".format(3, 1)) mcb = MultipleCoefficientBinning() mcb.fit(X, y) with pytest.raises(ValueError, match=re.escape(msg_error)): mcb.transform(X[:, :1])
def _compute_expected_results(X, y=None, n_coefs=None, n_bins=4, strategy='quantile', drop_sum=False, anova=False, norm_mean=False, norm_std=False, alphabet=None): """Compute the expected results.""" X = np.asarray(X) if norm_mean: X -= X.mean(axis=1)[:, None] if norm_std: X /= X.std(axis=1)[:, None] X_fft = np.fft.rfft(X) X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)]) X_fft = X_fft.reshape(n_samples, -1, order='F') if drop_sum: X_fft = X_fft[:, 2:-1] else: X_fft = np.hstack([X_fft[:, :1], X_fft[:, 2:-1]]) if n_coefs is not None: if anova: _, p = f_classif(X_fft, y) support = np.argsort(p)[:n_coefs] X_fft = X_fft[:, support] else: X_fft = X_fft[:, :n_coefs] mcb = MultipleCoefficientBinning(n_bins=n_bins, strategy=strategy, alphabet=alphabet) arr_desired = mcb.fit_transform(X_fft) return arr_desired
# License: BSD-3-Clause import numpy as np import matplotlib.pyplot as plt from pyts.approximation import MultipleCoefficientBinning # Parameters n_samples, n_timestamps = 100, 12 # Toy dataset rng = np.random.RandomState(41) X = rng.randn(n_samples, n_timestamps) # MCB transformation n_bins = 3 mcb = MultipleCoefficientBinning(n_bins=n_bins, strategy='quantile') X_mcb = mcb.fit_transform(X) # Show the results for two time series plt.figure(figsize=(6, 4)) plt.plot(X[0], 'o--', ms=4, label='First time series') for x, y, s in zip(range(n_timestamps), X[0], X_mcb[0]): plt.text(x, y, s, ha='center', va='bottom', fontsize=14, color='C0') plt.plot(X[5], 'o--', ms=4, label='Second time series') for x, y, s in zip(range(n_timestamps), X[5], X_mcb[5]): plt.text(x, y, s, ha='center', va='bottom', fontsize=14, color='C1') plt.hlines(mcb.bin_edges_.T, np.arange(n_timestamps) - 0.5, np.arange(n_timestamps) + 0.5, color='g',
def test_constant_sample(): """Test that a ValueError is raised with a constant sample.""" discretizer = MultipleCoefficientBinning() msg_error = "At least one timestamp is constant." with pytest.raises(ValueError, match=msg_error): discretizer.fit_transform(np.ones((10, 15)))
def test_parameter_check(params, error, err_msg): """Test parameter validation.""" mcb = MultipleCoefficientBinning(**params) with pytest.raises(error, match=re.escape(err_msg)): mcb.fit(X, y)
def test_fit_transform(params): """Test that fit and transform yield the same results as fit_transform.""" arr_1 = MultipleCoefficientBinning(**params).fit(X, y).transform(X) arr_2 = MultipleCoefficientBinning(**params).fit_transform(X, y) np.testing.assert_array_equal(arr_1, arr_2)
def test_actual_results(params, X, y, arr_desired): """Test that the actual results are the expected ones.""" arr_actual = MultipleCoefficientBinning(**params).fit_transform(X, y) np.testing.assert_array_equal(arr_actual, arr_desired)
def test_y_none_entropy(): """Test that an error is raised when y is None and 'entropy' is used.""" msg_error = "y cannot be None if strategy='entropy'." mcb = MultipleCoefficientBinning(n_bins=2, strategy='entropy') with pytest.raises(ValueError, match=msg_error): mcb.fit(X, None)