pytrendy 2.0.0.dev4__py3-none-any.whl → 2.0.0.dev6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytrendy/detect_trends.py +29 -8
- pytrendy/io/plot_pytrendy.py +29 -2
- pytrendy/io/{prepare_index.py → prep_index.py} +1 -1
- pytrendy/io/prep_signal_params.py +44 -0
- pytrendy/io/results_pytrendy.py +17 -26
- pytrendy/post_processing/segments_analyse.py +7 -7
- pytrendy/post_processing/segments_get.py +14 -6
- pytrendy/post_processing/segments_refine/__init__.py +14 -9
- pytrendy/post_processing/segments_refine/artifact_cleanup.py +9 -10
- pytrendy/post_processing/segments_refine/segment_grouping.py +6 -11
- pytrendy/process_signals.py +23 -22
- {pytrendy-2.0.0.dev4.dist-info → pytrendy-2.0.0.dev6.dist-info}/METADATA +12 -12
- {pytrendy-2.0.0.dev4.dist-info → pytrendy-2.0.0.dev6.dist-info}/RECORD +15 -14
- {pytrendy-2.0.0.dev4.dist-info → pytrendy-2.0.0.dev6.dist-info}/WHEEL +0 -0
- {pytrendy-2.0.0.dev4.dist-info → pytrendy-2.0.0.dev6.dist-info}/licenses/LICENSE +0 -0
pytrendy/detect_trends.py
CHANGED
|
@@ -8,7 +8,7 @@ from .post_processing.segments_refine import refine_segments
|
|
|
8
8
|
from .post_processing.segments_analyse import analyse_segments
|
|
9
9
|
from .io.plot_pytrendy import plot_pytrendy
|
|
10
10
|
from .io.results_pytrendy import PyTrendyResults
|
|
11
|
-
from .io import
|
|
11
|
+
from .io import prep_index, prep_signal_params
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
def detect_trends(df: pd.DataFrame,
|
|
@@ -16,6 +16,7 @@ def detect_trends(df: pd.DataFrame,
|
|
|
16
16
|
date_col: str|None=None,
|
|
17
17
|
plot: bool=True,
|
|
18
18
|
method_params: dict|None=None,
|
|
19
|
+
signal_params: dict|None=None,
|
|
19
20
|
plot_params: dict|None=None,
|
|
20
21
|
debug: bool=False
|
|
21
22
|
) -> PyTrendyResults:
|
|
@@ -51,6 +52,22 @@ def detect_trends(df: pd.DataFrame,
|
|
|
51
52
|
- **abrupt_padding** (`int`): Number of days to pad after abrupt transitions. Defaults to `0`.
|
|
52
53
|
- **gradual_padding** (`int`): Number of days to pad after gradual trend ends. Defaults to `0`.
|
|
53
54
|
- **avoid_noise** (`bool`): Whether to avoid noisy segments in trend detection. Defaults to `True`.
|
|
55
|
+
signal_params (dict, optional):
|
|
56
|
+
Optional parameters to customize the signal-processing constants. Supported keys (independent from `method_params`):
|
|
57
|
+
|
|
58
|
+
- **window_smooth** (`int`): Savitzky-Golay smoothing window, in points. Defaults to `15`.
|
|
59
|
+
- **smooth_factor** (`float`): Fraction of `window_smooth` spanned by the derived `window_flat` window. Raising it smooths the flat baseline (fewer flat flags); lowering it responds more locally. Defaults to `0.5`.
|
|
60
|
+
- **noise_factor** (`float`): Fraction of `window_smooth` spanned by the derived `window_noise` window. Raising it smooths the noise (SNR) estimate (fewer noise flags); lowering it responds more locally. Defaults to `0.5`.
|
|
61
|
+
- **grouping_distance** (`int`): Maximum gap, in steps, for grouping nearby segments. Defaults to `7`.
|
|
62
|
+
- **min_trend_length** (`int`): Minimum length, in steps, for an Up/Down segment to be retained. Defaults to `3`.
|
|
63
|
+
- **min_flat_noise_length** (`int`): Minimum length, in steps, for a Flat/Noise segment to be retained. Defaults to `1`.
|
|
64
|
+
- **threshold_noise** (`float`): SNR threshold (dB) below which a region is classified as noise. Defaults to `2.5`.
|
|
65
|
+
- **threshold_smooth** (`float`): Derivative threshold, as a fraction of the signal IQR, below which motion counts as flat. Defaults to `0.001`.
|
|
66
|
+
- **threshold_flat** (`float`): Flat sensitivity, as a fraction of the minimum non-zero rolling std. Defaults to `0.835`.
|
|
67
|
+
|
|
68
|
+
This surface is independent from `method_params`, which controls the padding and noise heuristics instead.
|
|
69
|
+
Window sizes are in points and minimum lengths in steps; at non-daily spacing a given count spans a
|
|
70
|
+
different real-time duration.
|
|
54
71
|
plot_params (dict, optional):
|
|
55
72
|
Optional dict to customise plot appearance. Only used when `plot` is `True`. Supported keys:
|
|
56
73
|
|
|
@@ -73,7 +90,7 @@ def detect_trends(df: pd.DataFrame,
|
|
|
73
90
|
Use this object to access segment statistics, rankings, and export utilities.
|
|
74
91
|
"""
|
|
75
92
|
# Reject the deprecated positional order detect_trends(df, date_col, value_col).
|
|
76
|
-
if date_col is not None and
|
|
93
|
+
if date_col is not None and prep_index.is_legacy_positional_order(df, value_col, date_col):
|
|
77
94
|
raise TypeError(
|
|
78
95
|
"detect_trends received arguments in the deprecated (date_col, value_col) order. "
|
|
79
96
|
"Pass value_col first: detect_trends(df, value_col, date_col=...)."
|
|
@@ -81,7 +98,7 @@ def detect_trends(df: pd.DataFrame,
|
|
|
81
98
|
|
|
82
99
|
# Stage the DataFrame on an internal integer index, keeping the external index
|
|
83
100
|
# values and a lookup so boundaries can be remapped back later.
|
|
84
|
-
df, external_index, index_lookup, index_type =
|
|
101
|
+
df, external_index, index_lookup, index_type = prep_index.prep_index(df, date_col, value_col)
|
|
85
102
|
|
|
86
103
|
if method_params is None:
|
|
87
104
|
method_params = {} # Avoid mutable default argument by accepting None and constructing a new dict here
|
|
@@ -102,18 +119,22 @@ def detect_trends(df: pd.DataFrame,
|
|
|
102
119
|
'avoid_noise': method_params.get('avoid_noise', True),
|
|
103
120
|
}
|
|
104
121
|
|
|
122
|
+
# Configures signal-processing constants. Unknown keys are accepted and
|
|
123
|
+
# forwarded (validation is deliberately out of scope for now).
|
|
124
|
+
signal_params = prep_signal_params.prep_signal_params(signal_params)
|
|
125
|
+
|
|
105
126
|
# Core 5-step pipeline
|
|
106
|
-
df = process_signals(df, value_col, method_params, debug)
|
|
107
|
-
segments = get_segments(df)
|
|
108
|
-
segments = refine_segments(df, value_col, segments, method_params)
|
|
127
|
+
df = process_signals(df, value_col, method_params, signal_params, debug)
|
|
128
|
+
segments = get_segments(df, signal_params)
|
|
129
|
+
segments = refine_segments(df, value_col, segments, method_params, signal_params)
|
|
109
130
|
segments = analyse_segments(df, value_col, segments)
|
|
110
131
|
|
|
111
132
|
# Translate internal segment boundaries back to the user's external index values.
|
|
112
|
-
segments =
|
|
133
|
+
segments = prep_index.remap_boundaries(segments, index_lookup)
|
|
113
134
|
|
|
114
135
|
if plot:
|
|
115
136
|
# Restore the external index for plotting before rendering the segments.
|
|
116
|
-
plot_df =
|
|
137
|
+
plot_df = prep_index.prepare_plot_frame(df, date_col, external_index, index_type)
|
|
117
138
|
plot_pytrendy(df=plot_df, value_col=value_col, segments_enhanced=segments, index_type=index_type, plot_params=plot_params)
|
|
118
139
|
|
|
119
140
|
results = PyTrendyResults(segments=segments, index_type=index_type)
|
pytrendy/io/plot_pytrendy.py
CHANGED
|
@@ -5,6 +5,33 @@ import numpy as np
|
|
|
5
5
|
import matplotlib.pyplot as plt
|
|
6
6
|
import matplotlib.dates as mdates
|
|
7
7
|
import matplotlib.patches as mpatches
|
|
8
|
+
from matplotlib import colors as mcolors
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _annotation_color(color):
|
|
12
|
+
"""
|
|
13
|
+
Return a darker annotation colour for a segment fill colour.
|
|
14
|
+
|
|
15
|
+
Annotation text and vertical dividers historically derived their colour with
|
|
16
|
+
``color[5:]``, which only produced a valid colour for the default ``light*``
|
|
17
|
+
named colours (``lightgreen`` -> ``green``). Any other matplotlib colour
|
|
18
|
+
format -- short/full hex, ``tab:*`` names, tuples, ``None`` -- raised or
|
|
19
|
+
yielded nonsense. This preserves the exact legacy result for ``light*`` names
|
|
20
|
+
(keeping default plots pixel-identical) and otherwise darkens the colour in
|
|
21
|
+
RGBA space, which is well-defined for every valid matplotlib colour.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
color: A matplotlib colour, or ``None``.
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
A darker colour, or ``None`` (deferred to matplotlib's default).
|
|
28
|
+
"""
|
|
29
|
+
if color is None:
|
|
30
|
+
return None
|
|
31
|
+
if isinstance(color, str) and color.startswith('light'):
|
|
32
|
+
return color[5:]
|
|
33
|
+
r, g, b, _ = mcolors.to_rgba(color)
|
|
34
|
+
return (r * 0.7, g * 0.7, b * 0.7)
|
|
8
35
|
|
|
9
36
|
|
|
10
37
|
def _safe_adjacent(index, pos, offset):
|
|
@@ -270,7 +297,7 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
270
297
|
y_pos = ymax - (ymax - ymin) * 0.05
|
|
271
298
|
ax.text(mid_date, y_pos, str(seg['change_rank']), fontsize=12,
|
|
272
299
|
fontweight='bold', ha='center', va='top',
|
|
273
|
-
color=color
|
|
300
|
+
color=_annotation_color(color))
|
|
274
301
|
|
|
275
302
|
# Add vertical line if next seg is same & touching
|
|
276
303
|
if next_seg and next_neighbouring and next_seg['direction'] == seg['direction']:
|
|
@@ -278,7 +305,7 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
278
305
|
line_date = pd.to_datetime(seg['end'])
|
|
279
306
|
else:
|
|
280
307
|
line_date = seg['end']
|
|
281
|
-
ax.axvline(x=line_date, color=color
|
|
308
|
+
ax.axvline(x=line_date, color=_annotation_color(color), linewidth=0.5)
|
|
282
309
|
|
|
283
310
|
# Set limits
|
|
284
311
|
if index_type == 'string':
|
|
@@ -101,7 +101,7 @@ def build_index_lookup(external_index) -> dict:
|
|
|
101
101
|
return dict(zip(internal_index, np.asarray(external_index)))
|
|
102
102
|
|
|
103
103
|
|
|
104
|
-
def
|
|
104
|
+
def prep_index(df: pd.DataFrame, date_col: str | None, value_col: str) -> tuple:
|
|
105
105
|
"""
|
|
106
106
|
Prepare the internal index framework used by the pipeline.
|
|
107
107
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""**Signal-Parameter Preparation**
|
|
2
|
+
|
|
3
|
+
The signal-processing stages share a common set of constants — smoothing and
|
|
4
|
+
rolling windows, grouping distance, and the noise/flat thresholds. ``detect_trends``
|
|
5
|
+
is the single public entry point, so this module is the one place those constants
|
|
6
|
+
are defaulted before the pipeline runs.
|
|
7
|
+
|
|
8
|
+
Defaults are merged with any user-supplied ``signal_params`` overrides. The derived
|
|
9
|
+
windows ``window_flat`` and ``window_noise`` are not top-level defaults: unless
|
|
10
|
+
explicitly overridden they derive from ``window_smooth``. Every stage downstream
|
|
11
|
+
assumes :func:`prep_signal_params` returns a fully-populated dict.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
_SIGNAL_PARAMS_DEFAULTS = {
|
|
15
|
+
'window_smooth': 15, # Savitzky-Golay smoothing window, in points.
|
|
16
|
+
'smooth_factor': 0.5, # Fraction of window_smooth spanned by window_flat: raise to widen it (smoother flat baseline, fewer flags), lower to narrow it (more sensitive); scaled to window_smooth so it stays proportional.
|
|
17
|
+
'noise_factor': 0.5, # Fraction of window_smooth spanned by window_noise: raise to widen it (smoother SNR estimate, fewer flags), lower to narrow it (more sensitive); scaled to window_smooth so it stays proportional.
|
|
18
|
+
'grouping_distance': 7, # Maximum gap, in index steps, for grouping nearby segments.
|
|
19
|
+
'min_trend_length': 3, # Minimum length, in steps, for an Up/Down segment to be retained.
|
|
20
|
+
'min_flat_noise_length': 1, # Minimum length, in steps, for a Flat/Noise segment to be retained.
|
|
21
|
+
'threshold_noise': 2.5, # SNR threshold (dB) below which a region is classified as noise.
|
|
22
|
+
'threshold_smooth': 0.001, # Derivative threshold as a fraction of the signal IQR, below which motion counts as flat.
|
|
23
|
+
'threshold_flat': 0.835, # Flat sensitivity as a fraction of the minimum non-zero rolling std.
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def prep_signal_params(signal_params: dict|None=None) -> dict:
|
|
28
|
+
"""
|
|
29
|
+
Merge user-supplied `signal_params` over the defaults and derive window sizes.
|
|
30
|
+
|
|
31
|
+
`window_flat` and `window_noise` are not top-level defaults: unless explicitly
|
|
32
|
+
overridden they derive from `window_smooth`. Every stage downstream assumes
|
|
33
|
+
this returns a fully-populated dict.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
signal_params (dict, optional): User overrides. Unknown keys are forwarded silently.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
dict: Fully-populated signal_params with every key the stages read.
|
|
40
|
+
"""
|
|
41
|
+
resolved = {**_SIGNAL_PARAMS_DEFAULTS, **(signal_params or {})}
|
|
42
|
+
resolved.setdefault('window_flat', int(resolved['window_smooth'] * resolved['smooth_factor']))
|
|
43
|
+
resolved.setdefault('window_noise', int(resolved['window_smooth'] * resolved['noise_factor']))
|
|
44
|
+
return resolved
|
pytrendy/io/results_pytrendy.py
CHANGED
|
@@ -23,8 +23,8 @@ class PyTrendyResults:
|
|
|
23
23
|
List of dictionaries representing individual trend segments.
|
|
24
24
|
index_type (str):
|
|
25
25
|
The type of the index used for the segments (``'date'``, ``'datetime64'``,
|
|
26
|
-
``'integer'``, ``'float'``, or ``'string'``). Used to
|
|
27
|
-
|
|
26
|
+
``'integer'``, ``'float'``, or ``'string'``). Used to label segment boundaries
|
|
27
|
+
in ``print_summary`` (e.g. ``'dates'`` vs ``'indexes'``). Defaults to ``'date'``.
|
|
28
28
|
"""
|
|
29
29
|
self.segments = segments
|
|
30
30
|
self.trend_segments = [seg for seg in self.segments if 'trend_class' in seg] # Get segments that are trends (exclude flats and noise)
|
|
@@ -37,11 +37,11 @@ class PyTrendyResults:
|
|
|
37
37
|
|
|
38
38
|
def set_best(self) -> None:
|
|
39
39
|
"""
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
Selects the best trend segment by lowest ``change_rank``.
|
|
41
|
+
|
|
42
|
+
Sets ``self.best`` to the trend segment with the lowest ``change_rank`` (derived from
|
|
43
|
+
each segment's cumulative ``total_change``). Sets ``self.best`` to ``None`` when no
|
|
44
|
+
trend segments exist.
|
|
45
45
|
"""
|
|
46
46
|
if len(self.trend_segments) == 0:
|
|
47
47
|
self.best = None
|
|
@@ -50,10 +50,11 @@ class PyTrendyResults:
|
|
|
50
50
|
|
|
51
51
|
def set_summary(self) -> None:
|
|
52
52
|
"""
|
|
53
|
-
Computes and stores summary statistics for trend segments
|
|
53
|
+
Computes and stores summary statistics for trend segments.
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
Sets ``self.summary`` to counts by direction and trend class, plus the highest total
|
|
56
|
+
change. Sets ``self.df_summary`` to a tabular overview of the segments, indexed by
|
|
57
|
+
``time_index``.
|
|
57
58
|
"""
|
|
58
59
|
summary = {}
|
|
59
60
|
|
|
@@ -77,16 +78,7 @@ class PyTrendyResults:
|
|
|
77
78
|
# Set summary df (without extra details)
|
|
78
79
|
df = pd.DataFrame(self.segments)
|
|
79
80
|
|
|
80
|
-
|
|
81
|
-
'date': 'days',
|
|
82
|
-
'integer': 'index steps',
|
|
83
|
-
'float': 'index steps',
|
|
84
|
-
'string': 'index steps',
|
|
85
|
-
}.get(self.index_type, 'days')
|
|
86
|
-
|
|
87
|
-
df = df.rename({'days' : unit_descriptor}, axis = 1)
|
|
88
|
-
|
|
89
|
-
cols = ['time_index', 'direction', 'start', 'end', unit_descriptor, 'total_change', 'change_rank']
|
|
81
|
+
cols = ['time_index', 'direction', 'start', 'end', 'steps', 'total_change', 'change_rank']
|
|
90
82
|
if len(changes) > 1: # only include trend_class if atleast one trend exists
|
|
91
83
|
cols += ['trend_class']
|
|
92
84
|
df = df[cols]
|
|
@@ -129,11 +121,11 @@ class PyTrendyResults:
|
|
|
129
121
|
|
|
130
122
|
def set_df(self) -> pd.DataFrame | None:
|
|
131
123
|
"""
|
|
132
|
-
Converts
|
|
124
|
+
Converts the segment list into a pandas ``DataFrame`` for downstream analysis and export.
|
|
133
125
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
126
|
+
Returns:
|
|
127
|
+
pd.DataFrame | None: An empty ``DataFrame`` when there are no segments; otherwise
|
|
128
|
+
``None``, with ``self.df`` set as a side effect.
|
|
137
129
|
"""
|
|
138
130
|
# Exit if nothing to report on
|
|
139
131
|
if len(self.segments) == 0:
|
|
@@ -156,8 +148,7 @@ class PyTrendyResults:
|
|
|
156
148
|
Output format. `'df'` returns a DataFrame, `'dict'` returns a list of dictionaries.
|
|
157
149
|
|
|
158
150
|
Returns:
|
|
159
|
-
|
|
160
|
-
|
|
151
|
+
list | pd.DataFrame: Filtered and sorted segments in the specified format.
|
|
161
152
|
"""
|
|
162
153
|
segments = self.segments
|
|
163
154
|
if len(segments) == 0:
|
|
@@ -19,7 +19,7 @@ def analyse_segments(df: pd.DataFrame, value_col: str, segments: list[dict]) ->
|
|
|
19
19
|
|
|
20
20
|
- Percent change (based on start/end values)
|
|
21
21
|
|
|
22
|
-
- Duration in
|
|
22
|
+
- Duration in index steps
|
|
23
23
|
|
|
24
24
|
- Cumulative total change (sum of diffs)
|
|
25
25
|
|
|
@@ -40,7 +40,7 @@ def analyse_segments(df: pd.DataFrame, value_col: str, segments: list[dict]) ->
|
|
|
40
40
|
Returns:
|
|
41
41
|
list:
|
|
42
42
|
A list of enhanced segment dictionaries with additional keys:
|
|
43
|
-
- `'total_change'`, `'pct_change'`, `'mltp_change'`, `'
|
|
43
|
+
- `'total_change'`, `'pct_change'`, `'mltp_change'`, `'steps'`, `'SNR'`, `'change_rank'`
|
|
44
44
|
"""
|
|
45
45
|
segments_enhanced = []
|
|
46
46
|
for segment in segments:
|
|
@@ -53,11 +53,11 @@ def analyse_segments(df: pd.DataFrame, value_col: str, segments: list[dict]) ->
|
|
|
53
53
|
segment_enhanced['pct_change'] = (float(val_end / val_start - 1) if val_start != 0 else np.nan)
|
|
54
54
|
segment_enhanced['mltp_change'] = (float(val_end / val_start) if val_start != 0 else np.nan)
|
|
55
55
|
|
|
56
|
-
# Calculate
|
|
57
|
-
|
|
58
|
-
if
|
|
59
|
-
|
|
60
|
-
segment_enhanced['
|
|
56
|
+
# Calculate steps & cumulative total change
|
|
57
|
+
steps = segment['end'] - segment['start']
|
|
58
|
+
if steps == 0:
|
|
59
|
+
steps = 1 # edge case for 1 step flat between noise spike & trend
|
|
60
|
+
segment_enhanced['steps'] = steps # set steps
|
|
61
61
|
|
|
62
62
|
# Calculate cumulative total change
|
|
63
63
|
segment_enhanced['total_change'] = float(df_segment[value_col].diff().sum())
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import pandas as pd
|
|
4
4
|
|
|
5
|
-
def get_segments(df: pd.DataFrame) -> list[dict]:
|
|
5
|
+
def get_segments(df: pd.DataFrame, signal_params: dict) -> list[dict]:
|
|
6
6
|
"""
|
|
7
7
|
Extracts contiguous segments from a flagged series.
|
|
8
8
|
|
|
@@ -20,12 +20,17 @@ def get_segments(df: pd.DataFrame) -> list[dict]:
|
|
|
20
20
|
|
|
21
21
|
Only segments meeting the following minimum lengths are retained:
|
|
22
22
|
|
|
23
|
-
- Up/Down: ≥ 3
|
|
24
|
-
- Flat/Noise: ≥ 1
|
|
23
|
+
- Up/Down: ≥ 3 steps (``min_trend_length``)
|
|
24
|
+
- Flat/Noise: ≥ 1 step (``min_flat_noise_length``)
|
|
25
25
|
|
|
26
26
|
Args:
|
|
27
27
|
df (pd.DataFrame):
|
|
28
28
|
Time series DataFrame containing a `trend_flag` column.
|
|
29
|
+
signal_params (dict):
|
|
30
|
+
Signal-processing constants, populated by `detect_trends` (no defaults applied here). Supported keys:
|
|
31
|
+
|
|
32
|
+
- **min_trend_length** (`int`): Minimum length, in steps, for an Up/Down segment.
|
|
33
|
+
- **min_flat_noise_length** (`int`): Minimum length, in steps, for a Flat/Noise segment.
|
|
29
34
|
|
|
30
35
|
Returns:
|
|
31
36
|
list:
|
|
@@ -45,6 +50,9 @@ def get_segments(df: pd.DataFrame) -> list[dict]:
|
|
|
45
50
|
, -3: 'Noise'
|
|
46
51
|
}
|
|
47
52
|
|
|
53
|
+
min_trend_length = signal_params['min_trend_length']
|
|
54
|
+
min_flat_noise_length = signal_params['min_flat_noise_length']
|
|
55
|
+
|
|
48
56
|
segment_length = 0
|
|
49
57
|
segment_length_prev = 0
|
|
50
58
|
direction_prev = map_direction[0]
|
|
@@ -58,9 +66,9 @@ def get_segments(df: pd.DataFrame) -> list[dict]:
|
|
|
58
66
|
segment_length += 1
|
|
59
67
|
elif direction != direction_prev:
|
|
60
68
|
if ( # Save only when satisfies min window for up/down or flat respectively.
|
|
61
|
-
(direction_prev in ['Up', 'Down'] and (segment_length_prev >=
|
|
62
|
-
or (direction_prev == 'Noise' and (segment_length_prev >=
|
|
63
|
-
or (direction_prev == 'Flat' and (segment_length_prev >=
|
|
69
|
+
(direction_prev in ['Up', 'Down'] and (segment_length_prev >= min_trend_length)) \
|
|
70
|
+
or (direction_prev == 'Noise' and (segment_length_prev >= min_flat_noise_length)) \
|
|
71
|
+
or (direction_prev == 'Flat' and (segment_length_prev >= min_flat_noise_length)) \
|
|
64
72
|
):
|
|
65
73
|
start = index - (segment_length_prev+1)
|
|
66
74
|
end = index - 1
|
|
@@ -12,7 +12,7 @@ from .segment_grouping import group_segments
|
|
|
12
12
|
from .artifact_cleanup import clean_artifacts, fill_in_flats
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
def refine_segments(df: pd.DataFrame, value_col: str, segments: list[dict], method_params: dict) -> list[dict]:
|
|
15
|
+
def refine_segments(df: pd.DataFrame, value_col: str, segments: list[dict], method_params: dict, signal_params: dict) -> list[dict]:
|
|
16
16
|
"""
|
|
17
17
|
Full post-processing pipeline to refine detected trend segments.
|
|
18
18
|
|
|
@@ -31,33 +31,38 @@ def refine_segments(df: pd.DataFrame, value_col: str, segments: list[dict], meth
|
|
|
31
31
|
|
|
32
32
|
- **abrupt_padding** (`int`): Number of days to pad. Defaults to `0`.
|
|
33
33
|
- **gradual_padding** (`int`): Number of days to extend gradual segment ends forward. Defaults to `0`.
|
|
34
|
+
signal_params (dict): Signal-processing constants, populated by `detect_trends` (no defaults applied here). Supported keys:
|
|
35
|
+
|
|
36
|
+
- **grouping_distance** (`int`): Maximum gap, in index steps, for grouping.
|
|
34
37
|
|
|
35
38
|
Returns:
|
|
36
39
|
list: Final refined segment list.
|
|
37
40
|
"""
|
|
38
41
|
|
|
42
|
+
grouping_distance = signal_params['grouping_distance']
|
|
43
|
+
|
|
39
44
|
segments_refined = deepcopy(segments)
|
|
40
45
|
|
|
41
46
|
segments_refined = classify_trends(df, value_col, segments_refined)
|
|
42
|
-
segments_refined = group_segments(segments_refined) # grouping 1st pass: sporadic flats & noises
|
|
47
|
+
segments_refined = group_segments(segments_refined, grouping_distance) # grouping 1st pass: sporadic flats & noises
|
|
43
48
|
|
|
44
49
|
segments_refined = expand_contract_segments(df, value_col, segments_refined, method_params) # for gradual + gradual padding
|
|
45
50
|
segments_refined = shave_abrupt_trends(df, value_col, segments_refined, method_params) # for abrupt
|
|
46
51
|
|
|
47
|
-
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params) # cleans overlaps etc from expand/contract
|
|
48
|
-
segments_refined = group_segments(segments_refined) # grouping 2nd pass: after trend refine and cleanup
|
|
49
|
-
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params) # cleans overlaps again after grouping
|
|
52
|
+
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params, signal_params) # cleans overlaps etc from expand/contract
|
|
53
|
+
segments_refined = group_segments(segments_refined, grouping_distance) # grouping 2nd pass: after trend refine and cleanup
|
|
54
|
+
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params, signal_params) # cleans overlaps again after grouping
|
|
50
55
|
|
|
51
56
|
init_segments = deepcopy(segments_refined)
|
|
52
57
|
segments_refined = classify_trends(df, value_col, segments_refined) # reclassify after artifacts cleaned: some graduals to abrupt
|
|
53
58
|
if segments_refined != init_segments: # only trigger if any re-classifications
|
|
54
59
|
segments_refined = shave_abrupt_trends(df, value_col, segments_refined, method_params
|
|
55
60
|
, second_pass=True, init_segments=init_segments) # abrupt shave 2nd pass: newly converted abrupts
|
|
56
|
-
segments_refined = group_segments(segments_refined) # make sure re-classifications are grouped to build strong enough cases for gradual -> abrupts
|
|
57
|
-
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params) # cleans overlaps etc from shave abrupt (precaution even though second_pass=True handles this)
|
|
61
|
+
segments_refined = group_segments(segments_refined, grouping_distance) # make sure re-classifications are grouped to build strong enough cases for gradual -> abrupts
|
|
62
|
+
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params, signal_params) # cleans overlaps etc from shave abrupt (precaution even though second_pass=True handles this)
|
|
58
63
|
|
|
59
64
|
segments_refined = fill_in_flats(df, segments_refined) # fill uncovered gaps (leading, internal, trailing) with flats
|
|
60
|
-
segments_refined = group_segments(segments_refined) # grouping 3rd pass (final): after abrupt shave 2nd pass and/or flat fill in
|
|
61
|
-
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params, inverse_only=True) # cleans 1-day flats of any leading/trailing only
|
|
65
|
+
segments_refined = group_segments(segments_refined, grouping_distance) # grouping 3rd pass (final): after abrupt shave 2nd pass and/or flat fill in
|
|
66
|
+
segments_refined = clean_artifacts(df, value_col, segments_refined, method_params, signal_params, inverse_only=True) # cleans 1-day flats of any leading/trailing only
|
|
62
67
|
|
|
63
68
|
return segments_refined
|
|
@@ -6,10 +6,9 @@ Functions for removing invalid segments and filling in gaps with flat segments.
|
|
|
6
6
|
import pandas as pd
|
|
7
7
|
import numpy as np
|
|
8
8
|
from copy import deepcopy
|
|
9
|
-
from .segment_grouping import GROUPING_DISTANCE
|
|
10
9
|
|
|
11
10
|
|
|
12
|
-
def clean_artifacts(df: pd.DataFrame, value_col: str, segments_refined: list[dict], method_params: dict, inverse_only: bool = False) -> list[dict]:
|
|
11
|
+
def clean_artifacts(df: pd.DataFrame, value_col: str, segments_refined: list[dict], method_params: dict, signal_params: dict, inverse_only: bool = False) -> list[dict]:
|
|
13
12
|
"""
|
|
14
13
|
Removes segments any invalid segments, such as inversions or overlaps.
|
|
15
14
|
Typically to clean up after boundary adjustments introduced from noise or trend refinements.
|
|
@@ -19,6 +18,10 @@ def clean_artifacts(df: pd.DataFrame, value_col: str, segments_refined: list[dic
|
|
|
19
18
|
method_params (dict): Optional parameters for cleanup behaviour. Supported keys:
|
|
20
19
|
|
|
21
20
|
- **avoid_noise** (`bool`): Whether to avoid noisy segments in trend detection. Defaults to `True`.
|
|
21
|
+
signal_params (dict): Signal-processing constants, populated by `detect_trends` (no defaults applied here). Supported keys:
|
|
22
|
+
|
|
23
|
+
- **grouping_distance** (`int`): Maximum gap, in index steps, for considering neighbouring segments noise.
|
|
24
|
+
- **threshold_noise** (`float`): SNR threshold (dB) below which a trend is considered too noisy to retain.
|
|
22
25
|
inverse_only (bool): If True, only perform inverse checks and skip other artifact cleanups. Useful for final cleanup pass after flat fill ins.
|
|
23
26
|
|
|
24
27
|
Returns:
|
|
@@ -97,20 +100,16 @@ def clean_artifacts(df: pd.DataFrame, value_col: str, segments_refined: list[dic
|
|
|
97
100
|
prev_end = segment_prev['end']
|
|
98
101
|
prev_width = prev_end - prev_start
|
|
99
102
|
|
|
100
|
-
# Define conditions
|
|
103
|
+
# Define conditions
|
|
101
104
|
is_overlap_prev = (start <= prev_end)
|
|
102
105
|
is_curr_shorter = (width <= prev_width)
|
|
103
|
-
is_curr_similar = (prev_width <= 1.5 * width) and (prev_width >= 0.5 * width)
|
|
104
106
|
|
|
105
107
|
is_trend = (dir in ('Up', 'Down'))
|
|
106
108
|
is_prev_noise = (prev_dir == 'Noise')
|
|
107
109
|
is_prev_opposite_trend = (prev_dir in ('Up', 'Down') and prev_dir != dir)
|
|
108
|
-
is_prev_flat = (prev_dir == 'Flat')
|
|
109
110
|
|
|
110
111
|
if is_overlap_prev and (is_trend and (is_prev_noise or is_prev_opposite_trend) and is_curr_shorter):
|
|
111
112
|
return True # overlap when curr is trend and prev is noise of larger/equal window
|
|
112
|
-
if is_overlap_prev and (is_trend and is_prev_flat) and is_curr_similar:
|
|
113
|
-
return True # overlap when curr is trend and prev is flat (with similar enough size), edge case scenario 11
|
|
114
113
|
return False
|
|
115
114
|
|
|
116
115
|
def has_partial_overlap_next(segment: dict, segment_next: dict) -> bool:
|
|
@@ -247,11 +246,11 @@ def clean_artifacts(df: pd.DataFrame, value_col: str, segments_refined: list[dic
|
|
|
247
246
|
|
|
248
247
|
# Conditions for edge cases
|
|
249
248
|
left_is_noise = any(( # Consider segments within neighbour distance on left
|
|
250
|
-
0 <= (start - prev_seg['end']) <=
|
|
249
|
+
0 <= (start - prev_seg['end']) <= signal_params['grouping_distance']
|
|
251
250
|
and prev_seg.get('direction') == 'Noise'
|
|
252
251
|
) for k, prev_seg in enumerate(segments) if k != i)
|
|
253
252
|
right_is_noise = any(( # Consider segments within neighbour distance on right
|
|
254
|
-
0 <= (next_seg['start'] - end) <=
|
|
253
|
+
0 <= (next_seg['start'] - end) <= signal_params['grouping_distance']
|
|
255
254
|
and next_seg.get('direction') == 'Noise'
|
|
256
255
|
) for k, next_seg in enumerate(segments) if k != i)
|
|
257
256
|
|
|
@@ -268,7 +267,7 @@ def clean_artifacts(df: pd.DataFrame, value_col: str, segments_refined: list[dic
|
|
|
268
267
|
signal_power = np.mean(df_segment['signal']**2)
|
|
269
268
|
noise_power = np.mean(df_segment['noise']**2)
|
|
270
269
|
snr = float(10 * np.log10(signal_power / noise_power)) if noise_power != 0 else np.nan
|
|
271
|
-
threshold_noise =
|
|
270
|
+
threshold_noise = signal_params['threshold_noise']
|
|
272
271
|
if is_gradual: threshold_noise = 5
|
|
273
272
|
if is_flat: threshold_noise = 0
|
|
274
273
|
too_noisy = (snr < threshold_noise)
|
|
@@ -5,23 +5,18 @@ Functions for grouping consecutive segments with the same direction.
|
|
|
5
5
|
|
|
6
6
|
import pandas as pd
|
|
7
7
|
|
|
8
|
-
GROUPING_DISTANCE = 7 # Distance for grouping segments of same type in group_segments
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
def group_segments(segments: list[dict]) -> list[dict]:
|
|
9
|
+
def group_segments(segments: list[dict], grouping_distance: int) -> list[dict]:
|
|
12
10
|
"""
|
|
13
11
|
Groups consecutive segments with the same direction if their gap is small.
|
|
14
12
|
|
|
15
|
-
Segments are grouped
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- Their gap is ≤ `GROUPING_DISTANCE`
|
|
19
|
-
- They are not classified as `'abrupt'`
|
|
20
|
-
|
|
21
|
-
This reduces fragmentation caused by short, noisy segments.
|
|
13
|
+
Segments are grouped when they share the same direction, their gap is ≤
|
|
14
|
+
``grouping_distance``, and neither is classified as abrupt. This reduces
|
|
15
|
+
fragmentation caused by short, noisy segments.
|
|
22
16
|
|
|
23
17
|
Args:
|
|
24
18
|
segments (list): List of segment dictionaries.
|
|
19
|
+
grouping_distance (int): Maximum gap, in index steps, for grouping.
|
|
25
20
|
|
|
26
21
|
Returns:
|
|
27
22
|
list: Grouped segment list.
|
|
@@ -50,7 +45,7 @@ def group_segments(segments: list[dict]) -> list[dict]:
|
|
|
50
45
|
if (
|
|
51
46
|
direction == direction_prev
|
|
52
47
|
and segment_history
|
|
53
|
-
and (segment['start'] - segment_history[-1]['end']) <=
|
|
48
|
+
and (segment['start'] - segment_history[-1]['end']) <= grouping_distance
|
|
54
49
|
and ((not 'trend_class' in segment) or ('trend_class' in segment and segment['trend_class'] != 'abrupt')) # dont group up abrupt trends
|
|
55
50
|
):
|
|
56
51
|
# same direction and within allowed distance -> extend history
|
pytrendy/process_signals.py
CHANGED
|
@@ -5,9 +5,8 @@ import numpy as np
|
|
|
5
5
|
import math
|
|
6
6
|
from scipy.signal import savgol_filter
|
|
7
7
|
from scipy.stats import iqr
|
|
8
|
-
from .post_processing.segments_refine.segment_grouping import GROUPING_DISTANCE
|
|
9
8
|
|
|
10
|
-
def process_signals(df: pd.DataFrame, value_col: str, method_params: dict, debug: bool=False) -> pd.DataFrame:
|
|
9
|
+
def process_signals(df: pd.DataFrame, value_col: str, method_params: dict, signal_params: dict, debug: bool=False) -> pd.DataFrame:
|
|
11
10
|
"""
|
|
12
11
|
Applies signal processing techniques to classify regions of a time series.
|
|
13
12
|
|
|
@@ -37,6 +36,16 @@ def process_signals(df: pd.DataFrame, value_col: str, method_params: dict, debug
|
|
|
37
36
|
Optional parameters to customize detection heuristics. Supported keys:
|
|
38
37
|
|
|
39
38
|
- **avoid_noise** (`bool`): Whether to avoid noisy segments in trend detection. Defaults to `True`.
|
|
39
|
+
signal_params (dict):
|
|
40
|
+
Signal-processing constants, populated by `detect_trends` (no defaults applied here). Supported keys:
|
|
41
|
+
|
|
42
|
+
- **window_smooth** (`int`): Savitzky-Golay smoothing window, in points.
|
|
43
|
+
- **window_flat** (`int`): Window for rolling flat statistics, derived from `window_smooth`.
|
|
44
|
+
- **window_noise** (`int`): Window for the noise SNR estimate, derived from `window_smooth`.
|
|
45
|
+
- **grouping_distance** (`int`): Maximum gap, in steps, for grouping nearby noise segments.
|
|
46
|
+
- **threshold_noise** (`float`): SNR threshold (dB).
|
|
47
|
+
- **threshold_smooth** (`float`): Derivative threshold as a fraction of the signal IQR.
|
|
48
|
+
- **threshold_flat** (`float`): Flat sensitivity as a fraction of the minimum non-zero rolling std.
|
|
40
49
|
debug (bool, optional):
|
|
41
50
|
If `True` will run in debug mode, outputting various additional plots and print statements. Only recommended for developers of pytrendy. Defaults to `False`.
|
|
42
51
|
|
|
@@ -46,25 +55,17 @@ def process_signals(df: pd.DataFrame, value_col: str, method_params: dict, debug
|
|
|
46
55
|
- `'smoothed'`, `'smoothed_std'`, `'snr'`, `'smoothed_deriv'`
|
|
47
56
|
- `'flat_flag'`, `'noise_flag'`, `'trend_flag'`
|
|
48
57
|
"""
|
|
49
|
-
WINDOW_SMOOTH = 15
|
|
50
|
-
WINDOW_FLAT = int(WINDOW_SMOOTH*0.5)
|
|
51
|
-
WINDOW_NOISE = int(WINDOW_SMOOTH*0.5)
|
|
52
|
-
|
|
53
|
-
THRESHOLD_NOISE = 2.5 # Sensitivity to detecting noise (recommended 0-10)
|
|
54
|
-
THRESHOLD_SMOOTH = 0.001 # Sensitivity to detecting trends as fraction of iqr
|
|
55
|
-
THRESHOLD_FLAT = 0.835 # Sensitivity to detecting flats as a fraction of min std (non-zero)
|
|
56
|
-
|
|
57
58
|
assert pd.api.types.is_integer_dtype(df.index.dtype), f"Supplied Index has type {df.index.dtype}"
|
|
58
59
|
|
|
59
60
|
# 1. Noise detection via SNR.
|
|
60
61
|
# 1.1 Compute the SNR
|
|
61
|
-
df['signal'] = df[value_col].rolling(window=
|
|
62
|
+
df['signal'] = df[value_col].rolling(window=signal_params['window_noise'], center=True, min_periods=1).mean()
|
|
62
63
|
df['noise'] = df[value_col] - df['signal']
|
|
63
64
|
df['snr'] = 10 * np.log10(df['signal']**2 / df['noise']**2)
|
|
64
65
|
|
|
65
66
|
# 1.2 Define noise flag when SNR & not all zero
|
|
66
67
|
df['noise_flag'] = 0
|
|
67
|
-
df.loc[(df['snr'] <=
|
|
68
|
+
df.loc[(df['snr'] <= signal_params['threshold_noise']), 'noise_flag'] = 1
|
|
68
69
|
|
|
69
70
|
# Skip noise detection when user opts out.
|
|
70
71
|
if not method_params['avoid_noise']:
|
|
@@ -106,7 +107,7 @@ def process_signals(df: pd.DataFrame, value_col: str, method_params: dict, debug
|
|
|
106
107
|
prev_seg = noise_segments[0].copy()
|
|
107
108
|
for i, seg in enumerate(noise_segments[1:]):
|
|
108
109
|
width = (seg['start'] - prev_seg['end'])
|
|
109
|
-
if width <=
|
|
110
|
+
if width <= signal_params['grouping_distance']:
|
|
110
111
|
new_seg = {'start': prev_seg['start'], 'end': seg['end']}
|
|
111
112
|
noise_segments_grouped.append(new_seg)
|
|
112
113
|
else:
|
|
@@ -181,23 +182,23 @@ def process_signals(df: pd.DataFrame, value_col: str, method_params: dict, debug
|
|
|
181
182
|
# with leading and trailing to cater for periods centred windows doesnt cover
|
|
182
183
|
|
|
183
184
|
# 3.1 Compute smoothed signal rolling std to be used by following logic
|
|
184
|
-
df['smoothed'] = savgol_filter(df['value_cleaned'], window_length=
|
|
185
|
-
df['smoothed_std'] = df['smoothed'].rolling(
|
|
186
|
-
df['smoothed_std_leading'] = df['smoothed'].iloc[::-1].rolling(window=
|
|
187
|
-
df['smoothed_std_trailing'] = df['smoothed'].rolling(
|
|
185
|
+
df['smoothed'] = savgol_filter(df['value_cleaned'], window_length=signal_params['window_smooth'], polyorder=1)
|
|
186
|
+
df['smoothed_std'] = df['smoothed'].rolling(signal_params['window_flat'], center=True).std()
|
|
187
|
+
df['smoothed_std_leading'] = df['smoothed'].iloc[::-1].rolling(window=signal_params['window_flat']).std().iloc[::-1]
|
|
188
|
+
df['smoothed_std_trailing'] = df['smoothed'].rolling(signal_params['window_flat']).std()
|
|
188
189
|
df['smoothed_std'] = df['smoothed_std'].fillna(df['smoothed_std_leading']).fillna(df['smoothed_std_trailing'])
|
|
189
190
|
|
|
190
191
|
# 3.2 Compute derivative early so flat detection can check both smoothness and motion.
|
|
191
|
-
derivative_limit = abs(iqr(df[value_col])) *
|
|
192
|
-
df['smoothed_deriv'] = savgol_filter(df[value_col], window_length=
|
|
192
|
+
derivative_limit = abs(iqr(df[value_col])) * signal_params['threshold_smooth']
|
|
193
|
+
df['smoothed_deriv'] = savgol_filter(df[value_col], window_length=signal_params['window_smooth'], polyorder=1, deriv=1)
|
|
193
194
|
|
|
194
195
|
# 3.3 Compute Flat Flag using both std and deriv limits
|
|
195
196
|
df['flat_flag'] = 0
|
|
196
|
-
rolling_std = df['value_cleaned'].rolling(
|
|
197
|
+
rolling_std = df['value_cleaned'].rolling(signal_params['window_flat'], center=True).std()
|
|
197
198
|
nonzero_std = rolling_std[rolling_std > 0]
|
|
198
199
|
min_nonzero_std = nonzero_std.min() if not nonzero_std.empty else 0.0
|
|
199
200
|
derivative_near_zero = df['smoothed_deriv'].abs() <= derivative_limit
|
|
200
|
-
extremely_smooth = df['smoothed_std'] < (min_nonzero_std *
|
|
201
|
+
extremely_smooth = df['smoothed_std'] < (min_nonzero_std * signal_params['threshold_flat'])
|
|
201
202
|
df.loc[(df['smoothed_std'] <= min_nonzero_std) & (df['noise_flag'] == 0) & (derivative_near_zero | extremely_smooth), 'flat_flag'] = 1
|
|
202
203
|
|
|
203
204
|
# 4. Detect up/down trend.
|
|
@@ -216,7 +217,7 @@ def process_signals(df: pd.DataFrame, value_col: str, method_params: dict, debug
|
|
|
216
217
|
import matplotlib.pyplot as plt
|
|
217
218
|
|
|
218
219
|
ax = df[[value_col, 'snr']].plot(figsize=(20,3), secondary_y='snr')
|
|
219
|
-
ax.right_ax.axhline(y=
|
|
220
|
+
ax.right_ax.axhline(y=signal_params['threshold_noise'], color='gray', linestyle='--', linewidth=2)
|
|
220
221
|
plt.title("Signal-Noise Ratio (SNR)")
|
|
221
222
|
plt.show()
|
|
222
223
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytrendy
|
|
3
|
-
Version: 2.0.0.
|
|
3
|
+
Version: 2.0.0.dev6
|
|
4
4
|
Summary: Trend Detection in Python. Applicable for real-world industry use cases in time series.
|
|
5
5
|
License: MIT License
|
|
6
6
|
|
|
@@ -126,16 +126,16 @@ The best detected trend is Down between dates 2025-05-09 - 2025-06-17
|
|
|
126
126
|
|
|
127
127
|
Full Results:
|
|
128
128
|
-------------------------------------------------------------------------------
|
|
129
|
-
direction start end
|
|
130
|
-
time_index
|
|
131
|
-
1 Up 2025-01-02 2025-01-24
|
|
132
|
-
2 Down 2025-01-25 2025-02-05
|
|
133
|
-
3 Flat 2025-02-06 2025-02-09
|
|
134
|
-
4 Up 2025-02-10 2025-03-17
|
|
135
|
-
5 Down 2025-03-18 2025-04-01
|
|
136
|
-
6 Up 2025-04-02 2025-05-08
|
|
137
|
-
7 Down 2025-05-09 2025-06-17
|
|
138
|
-
8 Flat 2025-06-18 2025-06-30
|
|
129
|
+
direction start end steps total_change change_rank trend_class
|
|
130
|
+
time_index
|
|
131
|
+
1 Up 2025-01-02 2025-01-24 22 14.013348 5 gradual
|
|
132
|
+
2 Down 2025-01-25 2025-02-05 11 -13.564214 6 gradual
|
|
133
|
+
3 Flat 2025-02-06 2025-02-09 3 -1.168831 8 NaN
|
|
134
|
+
4 Up 2025-02-10 2025-03-17 35 26.190476 3 gradual
|
|
135
|
+
5 Down 2025-03-18 2025-04-01 14 -22.721861 4 gradual
|
|
136
|
+
6 Up 2025-04-02 2025-05-08 36 72.611833 2 gradual
|
|
137
|
+
7 Down 2025-05-09 2025-06-17 39 -73.253968 1 gradual
|
|
138
|
+
8 Flat 2025-06-18 2025-06-30 12 3.910534 7 NaN
|
|
139
139
|
-------------------------------------------------------------------------------
|
|
140
140
|
```
|
|
141
141
|
|
|
@@ -144,7 +144,7 @@ Explore the strongest uptrends:
|
|
|
144
144
|
results.filter_segments(direction='Up', sort_by='change_rank')[:3]
|
|
145
145
|
```
|
|
146
146
|
|
|
147
|
-
| time_index | direction | start | end | trend_class | pct_change |
|
|
147
|
+
| time_index | direction | start | end | trend_class | pct_change | steps | total_change | SNR | change_rank |
|
|
148
148
|
|---|---|---|---|---|---|---|---|---|---|
|
|
149
149
|
| 7 | Up | 2025-04-02 | 2025-05-08 | gradual | 367.50% | 36 | 72.61 | 21.70 | 2 |
|
|
150
150
|
| 4 | Up | 2025-02-10 | 2025-03-17 | gradual | 179.93% | 35 | 26.19 | 18.61 | 3 |
|
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
pytrendy/__init__.py,sha256=4AxqWpOsPuUiQriNb2cWXg10-zjPZEubrfKjqcqgRfc,5028
|
|
2
|
-
pytrendy/detect_trends.py,sha256=
|
|
2
|
+
pytrendy/detect_trends.py,sha256=8p4kVl-p9vxXivbQ2ka-hD-JZC9QXMXleg0LkQjHvNE,8861
|
|
3
3
|
pytrendy/io/__init__.py,sha256=-cUdMdV4Apjev8QH_6VOWISp4UPaJHu02BTNIYO3rb0,1357
|
|
4
4
|
pytrendy/io/data/classes_signals.csv,sha256=D9sPKQjSvAQc3ZCWUwahyzU4fKOjYMRbXPM4aSj8gsY,251
|
|
5
5
|
pytrendy/io/data/series_synthetic.csv,sha256=NHsAKmNu5uWYkOXg_uAJ3xZKEEFaXYsjT4dhcZJvjLY,8681
|
|
6
6
|
pytrendy/io/data_loader.py,sha256=p49OVeFw-39fc0BDD32w8s90N0UZnO3gHz_1hby4evE,1136
|
|
7
|
-
pytrendy/io/plot_pytrendy.py,sha256
|
|
8
|
-
pytrendy/io/
|
|
9
|
-
pytrendy/io/
|
|
7
|
+
pytrendy/io/plot_pytrendy.py,sha256=-U4xrOL0N9mmm6DNPRvF3EN7rgSbLwlVjVIcXzCbGwE,18033
|
|
8
|
+
pytrendy/io/prep_index.py,sha256=RG35V51mIi_dLo-Ee8UL6y4SAXW6l_Vpzeq8H3x9FOc,8342
|
|
9
|
+
pytrendy/io/prep_signal_params.py,sha256=RMG9sW6FXQg7TGdfPaCkOaOdYu6H6X-a3qHFiI9i2RQ,2670
|
|
10
|
+
pytrendy/io/results_pytrendy.py,sha256=tEF3H2jfQaosqNJxC1PPtO1-mTwE1gdk9K48pDVTJ_s,7858
|
|
10
11
|
pytrendy/post_processing/__init__.py,sha256=bvn1pcxRAoyq5C1P7v5gnb6HiPzteyxT3y9dOY8Pemc,2569
|
|
11
|
-
pytrendy/post_processing/segments_analyse.py,sha256=
|
|
12
|
-
pytrendy/post_processing/segments_get.py,sha256=
|
|
13
|
-
pytrendy/post_processing/segments_refine/__init__.py,sha256=
|
|
12
|
+
pytrendy/post_processing/segments_analyse.py,sha256=5HbEpLh2MFoVAxJZvvE8f5L1zuGTMSmG7faHpbuL8Pw,3204
|
|
13
|
+
pytrendy/post_processing/segments_get.py,sha256=HrerM02hTVFsRRLuQkwp7sAAYxuTcWRf21mqDSInqgI,3145
|
|
14
|
+
pytrendy/post_processing/segments_refine/__init__.py,sha256=5ynp4dX08DtgQ_HxV_yWMM06SpJZIMYs8OBFWNQhq8M,3939
|
|
14
15
|
pytrendy/post_processing/segments_refine/abrupt_shaving.py,sha256=5e0dS0yQ51wT2Hvogm0MdGHjq-3F7aG7jutygmu1VM4,7378
|
|
15
|
-
pytrendy/post_processing/segments_refine/artifact_cleanup.py,sha256=
|
|
16
|
+
pytrendy/post_processing/segments_refine/artifact_cleanup.py,sha256=Q3GCDExycubfE2nzRJse5KNeVcvX3b21je4MATpVMW4,17485
|
|
16
17
|
pytrendy/post_processing/segments_refine/gradual_expand_contract.py,sha256=dZiHa-wFonl85zG7-JhziUKGeLFyOAYI45PpdLX6HXE,7733
|
|
17
|
-
pytrendy/post_processing/segments_refine/segment_grouping.py,sha256=
|
|
18
|
+
pytrendy/post_processing/segments_refine/segment_grouping.py,sha256=TSM76XLWW1c59Y40AoHGcK73McnOvUBA8fqsJ04Br3E,2633
|
|
18
19
|
pytrendy/post_processing/segments_refine/trend_classify.py,sha256=aJwutjfMz64t7fcOGqbsgKpSt5FDuEMWIHtls-s9TE4,3211
|
|
19
20
|
pytrendy/post_processing/segments_refine/update_neighbours.py,sha256=4MqOEllUADv8W5nBwfGL3NZNlM9cEYwh3ohb3VWIWU8,3784
|
|
20
|
-
pytrendy/process_signals.py,sha256=
|
|
21
|
+
pytrendy/process_signals.py,sha256=xl_hGfigX4CgmCE4b97Y5yyHHDtaqUPjRRXllDCuiL4,12166
|
|
21
22
|
pytrendy/simpledtw.py,sha256=tGsbnRgh4VqnA5XVxPCfYHnJXjvLlVHVGHQ4cGX4eUw,2945
|
|
22
|
-
pytrendy-2.0.0.
|
|
23
|
-
pytrendy-2.0.0.
|
|
24
|
-
pytrendy-2.0.0.
|
|
25
|
-
pytrendy-2.0.0.
|
|
23
|
+
pytrendy-2.0.0.dev6.dist-info/METADATA,sha256=oHQmQRvVDYdSHe4tb6kc2ZVES8mCjkPNIc6cXIP_6ds,8695
|
|
24
|
+
pytrendy-2.0.0.dev6.dist-info/WHEEL,sha256=EGEvSphFYqXKs23-kQBeyNoJP1nrT8ZJKQoi5p5DYL8,88
|
|
25
|
+
pytrendy-2.0.0.dev6.dist-info/licenses/LICENSE,sha256=I00f93GihSG6Gqqw3qerxzO53oj_crz0sjynXCiHX_0,1079
|
|
26
|
+
pytrendy-2.0.0.dev6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|