diff --git a/matrixprofile/analyze.py b/matrixprofile/analyze.py index 1ba50a7..a2cc272 100644 --- a/matrixprofile/analyze.py +++ b/matrixprofile/analyze.py @@ -7,8 +7,13 @@ range = getattr(__builtins__, 'xrange', range) # end of py2 compatability boilerplate +# Python native imports import math +# Third-party imports +import numpy as np + +# Library imports from matrixprofile import core from matrixprofile.discover import discords @@ -229,6 +234,10 @@ def analyze(ts, query=None, windows=None, sample_pct=1.0, threshold=0.98, n_jobs is_exact = sample_pct >= 1 is_approx = sample_pct > 0 and sample_pct < 1 + # Check to make sure all window sizes are greater than 3, return a ValueError if not. + if (single_window and windows < 4) or (many_windows and np.any(np.unique(windows) < 4)): + raise ValueError('Analyze requires all window sizes to be greater than 3!') + # use PMP with no window provided if no_window or many_windows: result = analyze_pmp(ts, query, sample_pct, threshold, windows=windows, n_jobs=n_jobs) @@ -237,6 +246,6 @@ def analyze(ts, query=None, windows=None, sample_pct=1.0, threshold=0.98, n_jobs elif single_window and is_approx: result = analyze_mp_approximate(ts, query, windows, sample_pct, n_jobs=n_jobs) else: - raise RuntimeError('Param combination resulted in an uknown operation') + raise RuntimeError('Param combination resulted in an unknown operation') return result diff --git a/matrixprofile/compute.py b/matrixprofile/compute.py index e9332ea..9e576eb 100644 --- a/matrixprofile/compute.py +++ b/matrixprofile/compute.py @@ -77,7 +77,7 @@ def compute(ts, windows=None, query=None, sample_pct=1, threshold=0.98, raise ValueError('compute requires a threshold or window(s) to be set!') # Check to make sure all window sizes are greater than 3, return a ValueError if not. - if (isinstance(windows, int) and windows < 4) or (multiple_windows and np.any(np.unique(windows) < 4)): + elif (isinstance(windows, int) and windows < 4) or (multiple_windows and np.any(np.unique(windows) < 4)): raise ValueError('Compute requires all window sizes to be greater than 3!') if core.is_array_like(windows) and len(windows) == 1: diff --git a/tests/test_analyze.py b/tests/test_analyze.py index 0dea8f5..bf87401 100644 --- a/tests/test_analyze.py +++ b/tests/test_analyze.py @@ -96,4 +96,46 @@ def test_analyze_pmp_windows(): assert(profile['class'] == 'PMP') assert(profile['sample_pct'] == 1) np.testing.assert_equal(profile['windows'], windows) - assert(len(figures) == 6) \ No newline at end of file + assert(len(figures) == 6) + + +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)