Skip to content

fix: trend detection for normalised time series#79

Open
ChrisMarsden833 wants to merge 8 commits intodevelopfrom
chris
Open

fix: trend detection for normalised time series#79
ChrisMarsden833 wants to merge 8 commits intodevelopfrom
chris

Conversation

@ChrisMarsden833
Copy link
Copy Markdown
Collaborator

@ChrisMarsden833 ChrisMarsden833 commented Mar 31, 2026

Potential fix for 77. Needs review.


Copilot summary:

This pull request introduces a debug mode for the trend detection pipeline and refines the method for detecting trends in time series data. The debug mode provides additional plots and print statements for developers, while the trend detection logic is improved by using a data-driven threshold based on the interquartile range (IQR) of the signal. The most important changes are grouped below:

Debug Mode Enhancements:

  • Added a debug parameter to the detect_trends and process_signals functions, allowing developers to enable detailed debugging output, including additional plots and print statements. [1] [2] [3] [4] [5]
  • Implemented conditional plotting in process_signals that visualizes intermediate signal processing steps when debug=True, aiding in development and troubleshooting.

Trend Detection Improvements:

  • Changed the trend detection threshold (THRESHOLD_SMOOTH) from a fixed value to a fraction of the signal's IQR, making the detection logic more adaptive to the data's variability. [1] [2]
  • Updated the logic for assigning trend flags to use the new, data-driven threshold, improving robustness across different datasets.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@RussellSB RussellSB self-assigned this Mar 31, 2026
@RussellSB RussellSB changed the title Chris fix: adjusting trend detection logic to work for normalised time series Mar 31, 2026
@RussellSB RussellSB changed the title fix: adjusting trend detection logic to work for normalised time series fix: trend detection to work for normalised time series Mar 31, 2026
Copy link
Copy Markdown
Owner

@RussellSB RussellSB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks so much for this. I left some nitpick comments which shouldnt take much work. What might take some time though is adding tests.

For this change we will need to add 2 tests.

Test 1

Test for the main focus of this commit, your [0,1] signal.

  • Could make a new module to add tests to called something like tests/tests_normalised_trends
  • You can reference your test data in a local csv somewhere under tests. E.g. save the csv tests/tests_normalised_trends/normalised_data.csv and have a test file that calls it tests/test_normalised_trends.py
  • You can follow the similar pattern of most tests, e.g. test_core_cases.py: line 18 . The idea is to load the signal, detect trends, and then assess that the detected segments are as expected.

Test 2

Test for covering the lines of debug statements.

  • A bit silly, but there's hard enforced rules to maintain the maximized code coverage to avoid missing any new edge cases.
  • This test could work with any data really .... To make things simple could use same test data above and just make sure that once debug True is enabled that it runs plt show calls from within process_trends().
  • You can refer to test_plot_pytrendy_edgecases.py: line 114 on how to do this with monkey patching. By setting detect trends plot=False, debug=True you should expect plt show to be hit from the debug True lines.

Comment thread pytrendy/process_signals.py Outdated
Comment thread pytrendy/process_signals.py Outdated
@RussellSB RussellSB changed the title fix: trend detection to work for normalised time series fix: trend detection for normalised time series Mar 31, 2026
@ChrisMarsden833
Copy link
Copy Markdown
Collaborator Author

Just realised there are some other nitpick changes to be resolved here, will resolve these too.

@ChrisMarsden833
Copy link
Copy Markdown
Collaborator Author

I think I've addressed everything, see what you think.

Copy link
Copy Markdown
Owner

@RussellSB RussellSB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks Chris. The TestUncommonValues looks great. I have some feedback re the debug test. Also left some nitpick comments as I spotted some git diffs unrelated to the PR.

Comment thread pytrendy/__init__.py Outdated
Comment thread pytrendy/post_processing/segments_refine/abrupt_shaving.py
Comment thread tests/test_debug.py
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

📚 Docs preview deployed!

Your docs preview is available at: https://russellsb.github.io/pytrendy/pr-79/

This preview will be removed when the PR is closed.

github-actions bot added a commit that referenced this pull request Apr 13, 2026
@ChrisMarsden833
Copy link
Copy Markdown
Collaborator Author

I've made the suggested changes and removed the nitpicks.

I've left in a pair of tests that check that debug mode doesn't affect the actual results, which feels particuarly important to include.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants