Skip to content

adding window size check to analyze function #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion matrixprofile/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion matrixprofile/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 43 additions & 1 deletion tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)