-
Notifications
You must be signed in to change notification settings - Fork 1
fix: trend detection for normalised time series #79
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
Open
ChrisMarsden833
wants to merge
8
commits into
develop
Choose a base branch
from
chris
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b0fdcc
(Potential) fix for bug where issue where no trends are detected when…
a86282c
Rebased with develop
4852b7f
Merge branch 'develop' into chris
ChrisMarsden833 9a369a0
Added testing for low value series, and debug mode.
b7eb3c4
Merge branch 'develop' into chris
ChrisMarsden833 56c996a
Fixed Nitpicks, better debug test solution, and dubious fix for 100% …
787f3db
Test fixes and more nitpicks
9b26ab0
Merge branch 'develop' into chris
ChrisMarsden833 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """ | ||
| Tests for debug functionality. | ||
|
|
||
| These tests verify that the debug process works as expected. | ||
|
|
||
| This is work in progress. | ||
| """ | ||
|
|
||
| import pytest | ||
| import numpy as np | ||
| import pandas as pd | ||
| import pytrendy as pt | ||
| from pytrendy.io.results_pytrendy import PyTrendyResults | ||
| from conftest import assert_segments_match | ||
| import matplotlib.pyplot as plt | ||
| import matplotlib | ||
|
|
||
| def compare_debug(monkeypatch: pytest.MonkeyPatch, df: pd.DataFrame, date_col: str='date', value_col: str='gradual') -> tuple[PyTrendyResults, PyTrendyResults]: | ||
| """Function that exists exclusively to test detect_trends in an identical way. Runs detect trends with supplied dataframe in both debug and normal mode and returns the results for later comparison. | ||
|
|
||
| Args: | ||
| monkeypatch (pytest.MonkeyPatch): | ||
| Monkeypatch for suppressing plotting. | ||
| df (pd.DataFrame): | ||
| Pandas dataframe containing the trend we are testing. Must contain date_col and value_col. | ||
| date_col (str, optional): | ||
| The date column. | ||
| Defaults to 'date'. | ||
| value_col (str, optional): | ||
| The value column. | ||
| Defaults to 'gradual'. | ||
|
|
||
| Returns: | ||
| tuple[pt.PyTrendyResults, pt.PyTrendyResults]: The Pytrendy results with and without debug mode activated, respectively. | ||
| """ | ||
|
|
||
| monkeypatch.setattr(plt, "show", lambda *a, **k: plt.close("all")) | ||
|
|
||
| results_debug = pt.detect_trends( | ||
| df, | ||
| date_col='date', | ||
| value_col='gradual', | ||
| plot=False, | ||
| debug=True, | ||
| method_params=dict(is_abrupt_padded=False) | ||
| ) | ||
|
|
||
| results_no_debug = pt.detect_trends( | ||
| df, | ||
| date_col='date', | ||
| value_col='gradual', | ||
| plot=False, | ||
| debug=False, | ||
| method_params=dict(is_abrupt_padded=False) | ||
| ) | ||
|
|
||
| return results_debug, results_no_debug | ||
|
|
||
|
|
||
| class TestDebug: | ||
| """Test cases for data loader functionality.""" | ||
|
|
||
| @pytest.mark.core | ||
| def test_debug_mode_equivalency(self, monkeypatch): | ||
| """Test that the series series_synthetic data produces identical outputs when in debug mode vs when not in debug mode.""" | ||
| df = pt.load_data('series_synthetic') | ||
| results_debug, results_no_debug = compare_debug(monkeypatch, df) | ||
| assert_segments_match(results_debug.segments, results_no_debug.segments) | ||
|
|
||
| @pytest.mark.core | ||
| def test_debug_mode_equivalency_noise(self, monkeypatch): | ||
| """Test that the series series_synthetic data produces identical outputs when in debug mode vs when not in debug mode.""" | ||
| df = pt.load_data('series_synthetic') | ||
| np.random.seed(42) # Deterministic Testing | ||
| df['gradual'] += np.random.normal(0, 10, size=len(df)) | ||
| results_debug, results_no_debug = compare_debug(monkeypatch, df) | ||
| assert_segments_match(results_debug.segments, results_no_debug.segments) | ||
|
|
||
| @pytest.mark.core | ||
| def test_debug_mode_plots(self, monkeypatch): | ||
| """ | ||
| Test that the correct number of plots are created. | ||
| We use monkeypatch to replace plt.show with a fake function that records calls. | ||
| """ | ||
|
|
||
| show_calls = [] | ||
| def fake_show(*args, **kwargs): | ||
| show_calls.append((args, kwargs)) | ||
| plt.close("all") | ||
| monkeypatch.setattr(plt, 'show', fake_show) | ||
|
|
||
| df = pt.load_data('series_synthetic') | ||
| _ = pt.detect_trends( | ||
| df, | ||
| date_col='date', | ||
| value_col='gradual', | ||
| plot=False, | ||
| debug=True, | ||
| method_params=dict(is_abrupt_padded=False) | ||
| ) | ||
| assert len(show_calls) == 7 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| This is test data that were initially derived from random noise generation for quick manual testing, | ||
| however they were found to crash or break logic of the algorithm in new and interesting ways. | ||
| This folder contains test data that was found to break the logic of the algorithm in new and interesting ways. | ||
| Some of this came from random noise generation for quick manual testing, some from synthetic 'hand drawn' data. | ||
| As such, they were backed up here and immortalised for a never ending cycle of automated testing. | ||
|
|
||
| - noisy_crashes.csv: edge cases that were found to crash pytrendy (either by hanging up, or throwing execution errors) | ||
| - noisy_edgecases.csv: would make trends get detected in weird unexpected way, or plot visualisation bugs, etc... | ||
| - noisy_edgecases.csv: would make trends get detected in weird unexpected way, or plot visualisation bugs, etc... | ||
| - low_value_series.csv: a hand drawn series that is in the domain [0, 1], which was found to break the algorithm. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.