def test_analyze_pmp_sample_pct(): ts = np.loadtxt(os.path.join(MODULE_PATH, '..', 'tests', 'sampledata.txt')) profile, figures = analyze(ts, sample_pct=0.1) assert (profile['algorithm'] == 'skimp') assert (profile['class'] == 'PMP') assert (profile['sample_pct'] == 0.1) assert (len(figures) == 6)
def test_analyze_pmp_windows(): ts = np.loadtxt(os.path.join(MODULE_PATH, '..', 'tests', 'sampledata.txt')) windows = np.arange(8, 32) profile, figures = analyze(ts, windows=windows, sample_pct=1) assert (profile['algorithm'] == 'skimp') assert (profile['class'] == 'PMP') assert (profile['sample_pct'] == 1) np.testing.assert_equal(profile['windows'], windows) assert (len(figures) == 6)
def test_analyze_mp_approximate(): ts = np.loadtxt(os.path.join(MODULE_PATH, '..', 'tests', 'sampledata.txt')) m = 32 profile, figures = analyze(ts, windows=m, sample_pct=0.5) assert (profile['algorithm'] == 'scrimp++') assert (profile['w'] == 32) assert (profile['data']['query'] == None) assert (profile['join'] == False) assert (profile['sample_pct'] == 0.5) assert (profile['class'] == 'MatrixProfile') assert (len(figures) == 4)
def test_analyze_mp_exact_no_query(): ts = np.loadtxt(os.path.join(MODULE_PATH, '..', 'tests', 'sampledata.txt')) m = 32 profile, figures = analyze(ts, windows=m) assert (profile['algorithm'] == 'mpx') assert (profile['w'] == 32) assert (profile['data']['query'] == None) assert (profile['join'] == False) assert (profile['sample_pct'] == 1) assert (profile['class'] == 'MatrixProfile') assert ('motifs' in profile) assert ('discords' in profile) assert (len(figures) == 4)
def test_analyze_mp_exact_with_query(): ts = np.loadtxt(os.path.join(MODULE_PATH, '..', 'tests', 'sampledata.txt')) query = ts[100:200] m = 32 profile, figures = analyze(ts, windows=m, query=query) assert (profile['algorithm'] == 'mpx') assert (profile['w'] == 32) np.testing.assert_equal(profile['data']['query'], query) assert (profile['join'] == True) assert (profile['sample_pct'] == 1) assert (profile['class'] == 'MatrixProfile') assert ('motifs' in profile) assert ('discords' in profile) assert (len(figures) == 4)
def test_preprocess(): ts = np.array([ 2, 3, 2, 3, 1, 2, 3, 4, 2, np.nan, np.inf, 4, 2, 3, 4, 5, 6, 7, 8, 3, 4, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, np.nan, np.nan, np.inf, np.nan, np.inf, np.nan, np.inf, np.nan, np.inf ]) m = 6 preprocessing_kwargs = { 'window': 5, 'impute_method': 'median', 'impute_direction': 'backward', 'add_noise': False } result = analyze(ts, windows=m, preprocessing_kwargs=preprocessing_kwargs) preprocessed_ts = result[0]['data']['ts'] assert (np.any(np.isnan(preprocessed_ts)) == False) assert (np.any(np.isinf(preprocessed_ts)) == False) # if preprocessing_kwargs=None, we disable the preprocessing procedure. result = analyze(ts, windows=m, preprocessing_kwargs=None) unprocessed_ts = result[0]['data']['ts'] assert (np.any(np.isnan(unprocessed_ts)) == True) assert (np.any(np.isinf(unprocessed_ts)) == True) # check if preprocessing_kwargs is None by default. result = analyze(ts, windows=m) unprocessed_ts = result[0]['data']['ts'] assert (np.any(np.isnan(unprocessed_ts)) == True) assert (np.any(np.isinf(unprocessed_ts)) == True) with pytest.raises(ValueError) as excinfo: analyze(ts, windows=m, preprocessing_kwargs=1) assert "The parameter 'preprocessing_kwargs' is not dict like!" \ in str(excinfo.value) with pytest.raises(ValueError) as excinfo: preprocessing_kwargs = { 'win': 5, 'impute_dir': 'backward', } analyze(ts, windows=m, preprocessing_kwargs=preprocessing_kwargs) assert "invalid key(s) for preprocessing_kwargs! valid key(s) should include " \ "{'impute_direction', 'add_noise', 'impute_method', 'window'}" \ in str(excinfo.value)
st = ShapeletTransform(window_sizes=[3, 5, 10, 14, 21], sort=True) X_new = st.fit_transform(X_train, y_train) plt.figure(figsize=(24, 12)) for i, index in enumerate(st.indices_[:4]): idx, start, end = index plt.plot(X_train[idx], color='C{}'.format(i), label='Sample {}'.format(idx)) plt.plot(np.arange(start, end), X_train[idx, start:end], lw=5, color='C{}'.format(i)) plt.xlabel('Time', fontsize=12) plt.title('The four more discriminative shapelets', fontsize=14) plt.legend(loc='best', fontsize=8) plt.show() # Matrix Profile import matrixprofile as mp vals = ticker.daily.adjclose.values profile, figures = mp.analyze(vals, windows=50) plt.show() df = ticker.intraday.copy() df['strend_signal'] = df.ta.supertrend(3, 20).iloc[:, 1] df[['close', 'strend_signal']].plot() plt.show()
def test_analyze_mp_invalid_windows(): ts = np.loadtxt(os.path.join(MODULE_PATH, '..', 'tests', 'sampledata.txt')) with pytest.raises(ValueError) as excinfo: windows = 0 analyze(ts, windows=windows) assert 'Analyze requires all window sizes to be greater than 3!' \ in str(excinfo.value) with pytest.raises(ValueError) as excinfo: windows = 3 analyze(ts, windows=windows) assert 'Analyze requires all window sizes to be greater than 3!' \ in str(excinfo.value) with pytest.raises(ValueError) as excinfo: windows = [4, 0] analyze(ts, windows=windows) assert 'Analyze requires all window sizes to be greater than 3!' \ in str(excinfo.value) with pytest.raises(ValueError) as excinfo: windows = [4, 3] analyze(ts, windows=windows) assert 'Analyze requires all window sizes to be greater than 3!' \ in str(excinfo.value) with pytest.raises(ValueError) as excinfo: windows = [4, 3] analyze(ts, windows=windows, sample_pct=1) assert 'Analyze requires all window sizes to be greater than 3!' \ in str(excinfo.value) with pytest.raises(ValueError) as excinfo: windows = 0 query = ts[100:200] profile, figures = analyze(ts, windows=windows, query=query) assert 'Analyze requires all window sizes to be greater than 3!' \ in str(excinfo.value)