pytrendy 2.0.0.dev2__tar.gz → 2.0.0.dev4__tar.gz
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-2.0.0.dev2 → pytrendy-2.0.0.dev4}/PKG-INFO +7 -6
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/README.md +5 -5
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pyproject.toml +7 -3
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/io/plot_pytrendy.py +76 -27
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/LICENSE +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/__init__.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/detect_trends.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/io/__init__.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/io/data/classes_signals.csv +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/io/data/series_synthetic.csv +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/io/data_loader.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/io/prepare_index.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/io/results_pytrendy.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/__init__.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_analyse.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_get.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/__init__.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/abrupt_shaving.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/artifact_cleanup.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/gradual_expand_contract.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/segment_grouping.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/trend_classify.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/update_neighbours.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/process_signals.py +0 -0
- {pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/simpledtw.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytrendy
|
|
3
|
-
Version: 2.0.0.
|
|
3
|
+
Version: 2.0.0.dev4
|
|
4
4
|
Summary: Trend Detection in Python. Applicable for real-world industry use cases in time series.
|
|
5
5
|
License: MIT License
|
|
6
6
|
|
|
@@ -55,6 +55,7 @@ Requires-Dist: pytest ; extra == "dev"
|
|
|
55
55
|
Requires-Dist: pytest-cov ; extra == "dev"
|
|
56
56
|
Requires-Dist: pytest-mpl ; extra == "dev"
|
|
57
57
|
Requires-Dist: pytest-timeout ; extra == "dev"
|
|
58
|
+
Requires-Dist: ruff ; extra == "dev"
|
|
58
59
|
Requires-Dist: scipy
|
|
59
60
|
Project-URL: Documentation, https://russellsb.github.io/pytrendy/main/
|
|
60
61
|
Project-URL: Homepage, https://russellsb.github.io/pytrendy/main/
|
|
@@ -143,11 +144,11 @@ Explore the strongest uptrends:
|
|
|
143
144
|
results.filter_segments(direction='Up', sort_by='change_rank')[:3]
|
|
144
145
|
```
|
|
145
146
|
|
|
146
|
-
| time_index | direction | start | end | trend_class |
|
|
147
|
-
|
|
148
|
-
| 7 | Up | 2025-04-02 | 2025-05-08 | gradual |
|
|
149
|
-
| 4 | Up | 2025-02-10 | 2025-03-17 | gradual |
|
|
150
|
-
| 1 | Up | 2025-01-02 | 2025-01-24 | gradual |
|
|
147
|
+
| time_index | direction | start | end | trend_class | pct_change | days | total_change | SNR | change_rank |
|
|
148
|
+
|---|---|---|---|---|---|---|---|---|---|
|
|
149
|
+
| 7 | Up | 2025-04-02 | 2025-05-08 | gradual | 367.50% | 36 | 72.61 | 21.70 | 2 |
|
|
150
|
+
| 4 | Up | 2025-02-10 | 2025-03-17 | gradual | 179.93% | 35 | 26.19 | 18.61 | 3 |
|
|
151
|
+
| 1 | Up | 2025-01-02 | 2025-01-24 | gradual | 104.41% | 22 | 14.01 | 22.21 | 5 |
|
|
151
152
|
|
|
152
153
|
`filter_segments` ranks segments by magnitude (`change_rank`). See the [API reference](https://russellsb.github.io/pytrendy/main/reference/pytrendy/io/results_pytrendy/#pytrendy.io.results_pytrendy.PyTrendyResults.filter_segments) for all filter and sort options.
|
|
153
154
|
|
|
@@ -80,11 +80,11 @@ Explore the strongest uptrends:
|
|
|
80
80
|
results.filter_segments(direction='Up', sort_by='change_rank')[:3]
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
| time_index | direction | start | end | trend_class |
|
|
84
|
-
|
|
85
|
-
| 7 | Up | 2025-04-02 | 2025-05-08 | gradual |
|
|
86
|
-
| 4 | Up | 2025-02-10 | 2025-03-17 | gradual |
|
|
87
|
-
| 1 | Up | 2025-01-02 | 2025-01-24 | gradual |
|
|
83
|
+
| time_index | direction | start | end | trend_class | pct_change | days | total_change | SNR | change_rank |
|
|
84
|
+
|---|---|---|---|---|---|---|---|---|---|
|
|
85
|
+
| 7 | Up | 2025-04-02 | 2025-05-08 | gradual | 367.50% | 36 | 72.61 | 21.70 | 2 |
|
|
86
|
+
| 4 | Up | 2025-02-10 | 2025-03-17 | gradual | 179.93% | 35 | 26.19 | 18.61 | 3 |
|
|
87
|
+
| 1 | Up | 2025-01-02 | 2025-01-24 | gradual | 104.41% | 22 | 14.01 | 22.21 | 5 |
|
|
88
88
|
|
|
89
89
|
`filter_segments` ranks segments by magnitude (`change_rank`). See the [API reference](https://russellsb.github.io/pytrendy/main/reference/pytrendy/io/results_pytrendy/#pytrendy.io.results_pytrendy.PyTrendyResults.filter_segments) for all filter and sort options.
|
|
90
90
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pytrendy"
|
|
3
|
-
version = "2.0.0.
|
|
3
|
+
version = "2.0.0.dev4"
|
|
4
4
|
description = "Trend Detection in Python. Applicable for real-world industry use cases in time series."
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Russell Sammut Bonnici", email = "r.sammutbonnici@gmail.com" },
|
|
@@ -50,7 +50,8 @@ dev = [
|
|
|
50
50
|
"pytest",
|
|
51
51
|
"pytest-cov",
|
|
52
52
|
"pytest-mpl",
|
|
53
|
-
"pytest-timeout"
|
|
53
|
+
"pytest-timeout",
|
|
54
|
+
"ruff"
|
|
54
55
|
]
|
|
55
56
|
docs = [
|
|
56
57
|
"mkdocs>=1.6,<2",
|
|
@@ -76,4 +77,7 @@ timeout = 15
|
|
|
76
77
|
markers = [
|
|
77
78
|
"core: essential tests that must pass for core functionality",
|
|
78
79
|
"plot: tests for visualization/plotting functionality"
|
|
79
|
-
]
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
[tool.ruff]
|
|
83
|
+
exclude = ["**/*"]
|
|
@@ -24,6 +24,23 @@ def _safe_adjacent(index, pos, offset):
|
|
|
24
24
|
return index[new_pos]
|
|
25
25
|
return None
|
|
26
26
|
|
|
27
|
+
def _adjacent_to(index, value, offset):
|
|
28
|
+
"""
|
|
29
|
+
Return the index value adjacent to *value* (``offset`` of +1 or -1), or None.
|
|
30
|
+
|
|
31
|
+
Boundary displacement must follow the actual point spacing rather than a
|
|
32
|
+
hard-coded daily step, otherwise non-daily data (e.g. weekly dates) leaves
|
|
33
|
+
white gaps between shaded segments. For a contiguous daily index this is
|
|
34
|
+
equivalent to ``value +/- 1 day``.
|
|
35
|
+
"""
|
|
36
|
+
try:
|
|
37
|
+
pos = index.get_loc(value)
|
|
38
|
+
except KeyError:
|
|
39
|
+
return None
|
|
40
|
+
if not isinstance(pos, int):
|
|
41
|
+
return None # non-unique index: no well-defined adjacent point
|
|
42
|
+
return _safe_adjacent(index, pos, offset)
|
|
43
|
+
|
|
27
44
|
def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict], index_type: str = "date", suppress_show: bool = False, plot_params: dict = None) -> plt.Figure:
|
|
28
45
|
"""
|
|
29
46
|
Visualizes detected trend segments over the original time series signal.
|
|
@@ -102,9 +119,13 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
102
119
|
|
|
103
120
|
# Add shaded regions with fill_between
|
|
104
121
|
ymin, ymax = ax.get_ylim() # get plot's visible y-range
|
|
122
|
+
# 'date' and 'datetime64' are both real date axes: boundaries must be
|
|
123
|
+
# displaced by the actual point spacing (via _adjacent_to), not a hard-coded
|
|
124
|
+
# one-day step, or non-daily data leaves white gaps between shaded regions.
|
|
125
|
+
is_date_axis = index_type in ('date', 'datetime64')
|
|
105
126
|
for i, seg in enumerate(segments_enhanced):
|
|
106
127
|
|
|
107
|
-
if
|
|
128
|
+
if is_date_axis:
|
|
108
129
|
start = pd.to_datetime(seg['start'])
|
|
109
130
|
end = pd.to_datetime(seg['end'])
|
|
110
131
|
else:
|
|
@@ -115,12 +136,14 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
115
136
|
|
|
116
137
|
# Get context on prev seg if possible
|
|
117
138
|
prev_seg = segments_enhanced[i-1] if i-1 >= 0 else None
|
|
118
|
-
if
|
|
119
|
-
|
|
139
|
+
if is_date_axis:
|
|
140
|
+
prev_point = _adjacent_to(df.index, start, -1)
|
|
141
|
+
prev_neighbouring = prev_seg and (prev_point is not None) and (pd.to_datetime(prev_seg['end']) == prev_point)
|
|
120
142
|
elif index_type == 'string':
|
|
121
143
|
prev_neighbouring = prev_seg and (prev_seg['end'] == df.index[df.index.get_loc(start) - 1])
|
|
122
144
|
else:
|
|
123
|
-
|
|
145
|
+
prev_point = _adjacent_to(df.index, start, -1)
|
|
146
|
+
prev_neighbouring = prev_seg and (prev_point is not None) and (prev_seg['end'] == prev_point)
|
|
124
147
|
|
|
125
148
|
is_prev_not_trend = prev_seg and (not ('trend_class' in prev_seg))
|
|
126
149
|
|
|
@@ -131,13 +154,15 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
131
154
|
|
|
132
155
|
# Get context on next seg if possible
|
|
133
156
|
next_seg = segments_enhanced[i+1] if i+1 < len(segments_enhanced) else None
|
|
134
|
-
if
|
|
135
|
-
|
|
157
|
+
if is_date_axis:
|
|
158
|
+
next_point = _adjacent_to(df.index, end, 1)
|
|
159
|
+
next_neighbouring = next_seg and (next_point is not None) and (pd.to_datetime(next_seg['start']) == next_point)
|
|
136
160
|
elif index_type == 'string':
|
|
137
161
|
end_pos = df.index.get_loc(end)
|
|
138
162
|
next_neighbouring = next_seg and (next_seg['start'] == _safe_adjacent(df.index, end_pos, 1))
|
|
139
163
|
else:
|
|
140
|
-
|
|
164
|
+
next_point = _adjacent_to(df.index, end, 1)
|
|
165
|
+
next_neighbouring = next_seg and (next_point is not None) and (next_seg['start'] == next_point)
|
|
141
166
|
|
|
142
167
|
next_seg_abrupt = next_seg and (('trend_class' in next_seg) and (next_seg['trend_class'] == 'abrupt'))
|
|
143
168
|
next_seg_noise = next_seg and (next_seg['direction'] == 'Noise')
|
|
@@ -146,13 +171,13 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
146
171
|
if is_abrupt or is_noise:
|
|
147
172
|
pass # Keep start as-is for abrupt/noise segments
|
|
148
173
|
else:
|
|
149
|
-
if
|
|
150
|
-
new_start = start -
|
|
174
|
+
if is_date_axis:
|
|
175
|
+
new_start = _adjacent_to(df.index, start, -1) # Everything else displaced left start
|
|
151
176
|
elif index_type == 'string':
|
|
152
177
|
start_pos = df.index.get_loc(start)
|
|
153
178
|
new_start = _safe_adjacent(df.index, start_pos, -1)
|
|
154
179
|
else:
|
|
155
|
-
new_start = start -
|
|
180
|
+
new_start = _adjacent_to(df.index, start, -1) # Everything else displaced left start
|
|
156
181
|
|
|
157
182
|
# Check validity of plot start adjustment
|
|
158
183
|
value_new_start = df.loc[new_start, value_col] if new_start is not None and new_start in df.index else None
|
|
@@ -161,21 +186,22 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
161
186
|
|
|
162
187
|
valid_up_start = (value_new_start) and (seg['direction'] == 'Up') and (value_new_start < value)
|
|
163
188
|
valid_down_start = (value_new_start) and (seg['direction'] == 'Down') and (value_new_start > value)
|
|
164
|
-
if valid_up_start or valid_down_start or is_not_trend:
|
|
189
|
+
if (valid_up_start or valid_down_start or is_not_trend) and new_start is not None:
|
|
165
190
|
start = new_start # Apply left displacement only if valid
|
|
166
191
|
else:
|
|
167
192
|
# if not displaced and prev is not trend, adjust by plotting (as prev has already been drawn)
|
|
168
193
|
if is_prev_not_trend and prev_neighbouring:
|
|
169
|
-
if
|
|
194
|
+
if is_date_axis:
|
|
170
195
|
prev_end = pd.to_datetime(segments_enhanced[i-1]['end'])
|
|
171
|
-
|
|
196
|
+
prev_adj_end = _adjacent_to(df.index, prev_end, 1)
|
|
197
|
+
prev_new_end = prev_adj_end.strftime('%Y-%m-%d') if prev_adj_end is not None else None
|
|
172
198
|
elif index_type == 'string':
|
|
173
199
|
prev_end = segments_enhanced[i-1]['end']
|
|
174
200
|
prev_end_pos = df.index.get_loc(prev_end)
|
|
175
201
|
prev_new_end = _safe_adjacent(df.index, prev_end_pos, 1)
|
|
176
202
|
else:
|
|
177
203
|
prev_end = segments_enhanced[i-1]['end']
|
|
178
|
-
prev_new_end = prev_end
|
|
204
|
+
prev_new_end = _adjacent_to(df.index, prev_end, 1)
|
|
179
205
|
|
|
180
206
|
if prev_new_end is not None:
|
|
181
207
|
if index_type == 'string':
|
|
@@ -187,13 +213,13 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
187
213
|
|
|
188
214
|
# Adjust ends when appropriate
|
|
189
215
|
if (next_seg_abrupt or next_seg_noise) and next_neighbouring:
|
|
190
|
-
if
|
|
191
|
-
new_end = end
|
|
216
|
+
if is_date_axis:
|
|
217
|
+
new_end = _adjacent_to(df.index, end, 1)
|
|
192
218
|
elif index_type == 'string':
|
|
193
219
|
end_pos = df.index.get_loc(end)
|
|
194
220
|
new_end = _safe_adjacent(df.index, end_pos, 1)
|
|
195
221
|
else:
|
|
196
|
-
new_end = end
|
|
222
|
+
new_end = _adjacent_to(df.index, end, 1)
|
|
197
223
|
|
|
198
224
|
# Check validity of plot end adjustment
|
|
199
225
|
value_new_end = df.loc[new_end, value_col] if new_end is not None and new_end in df.index else None
|
|
@@ -202,18 +228,24 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
202
228
|
valid_up_end = (value_new_end) and (seg['direction'] == 'Up') and (value_new_end > value)
|
|
203
229
|
valid_down_end = (value_new_end) and (seg['direction'] == 'Down') and (value_new_end < value)
|
|
204
230
|
is_not_trend = not ('trend_class' in seg)
|
|
205
|
-
if valid_up_end or valid_down_end or is_not_trend:
|
|
231
|
+
if (valid_up_end or valid_down_end or is_not_trend) and new_end is not None:
|
|
206
232
|
end = new_end # Apply right displacement only if valid
|
|
207
233
|
else:
|
|
208
234
|
# if not displaced and next is noise, adjust for next plotting round
|
|
209
235
|
if next_seg_noise and next_neighbouring:
|
|
210
|
-
if
|
|
211
|
-
|
|
236
|
+
if is_date_axis:
|
|
237
|
+
next_start = pd.to_datetime(segments_enhanced[i+1]['start'])
|
|
238
|
+
next_adj_start = _adjacent_to(df.index, next_start, -1)
|
|
239
|
+
if next_adj_start is not None:
|
|
240
|
+
segments_enhanced[i+1]['start'] = next_adj_start.strftime('%Y-%m-%d')
|
|
212
241
|
elif index_type == 'string':
|
|
213
242
|
next_start_pos = df.index.get_loc(segments_enhanced[i+1]['start'])
|
|
214
243
|
segments_enhanced[i+1]['start'] = _safe_adjacent(df.index, next_start_pos, -1)
|
|
215
244
|
else:
|
|
216
|
-
|
|
245
|
+
next_start = segments_enhanced[i+1]['start']
|
|
246
|
+
next_adj_start = _adjacent_to(df.index, next_start, -1)
|
|
247
|
+
if next_adj_start is not None:
|
|
248
|
+
segments_enhanced[i+1]['start'] = next_adj_start
|
|
217
249
|
else:
|
|
218
250
|
pass # Keep end as-is
|
|
219
251
|
|
|
@@ -242,7 +274,7 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
242
274
|
|
|
243
275
|
# Add vertical line if next seg is same & touching
|
|
244
276
|
if next_seg and next_neighbouring and next_seg['direction'] == seg['direction']:
|
|
245
|
-
if
|
|
277
|
+
if is_date_axis:
|
|
246
278
|
line_date = pd.to_datetime(seg['end'])
|
|
247
279
|
else:
|
|
248
280
|
line_date = seg['end']
|
|
@@ -259,13 +291,30 @@ def plot_pytrendy(df: pd.DataFrame, value_col: str, segments_enhanced: list[dict
|
|
|
259
291
|
ax.set_xlim(first_date, last_date)
|
|
260
292
|
ax.set_ylim(ymin, ymax)
|
|
261
293
|
|
|
262
|
-
if index_type
|
|
263
|
-
|
|
264
|
-
|
|
294
|
+
if index_type in ('date', 'datetime64'):
|
|
295
|
+
index = df.index
|
|
296
|
+
gap_days = (
|
|
297
|
+
np.median(np.diff(index.values)) / np.timedelta64(1, 'D')
|
|
298
|
+
if len(index) > 1 else 1
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
if gap_days <= 1:
|
|
302
|
+
# Daily data: weekly majors on the default weekday, as before.
|
|
303
|
+
ax.xaxis.set_major_locator(mdates.WeekdayLocator(interval=1))
|
|
304
|
+
else:
|
|
305
|
+
# Non-daily spacing: pin majors to the data's own index positions so
|
|
306
|
+
# every major (and its 'major' gridline) lands exactly on an
|
|
307
|
+
# observation, for any cadence including irregular. A WeekdayLocator
|
|
308
|
+
# anchors its interval grid to the Unix epoch, which lands majors
|
|
309
|
+
# beside fortnightly / month-end observations.
|
|
310
|
+
ax.set_xticks(index)
|
|
265
311
|
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
|
266
312
|
|
|
267
|
-
# Minor ticks: every day
|
|
268
|
-
|
|
313
|
+
# Minor ticks: every day, but only when points are daily-or-finer. A daily
|
|
314
|
+
# ruler under non-daily data (e.g. weekly points) misrepresents the sampling,
|
|
315
|
+
# so non-daily spacing gets no minors at all.
|
|
316
|
+
if gap_days <= 1:
|
|
317
|
+
ax.xaxis.set_minor_locator(mdates.DayLocator())
|
|
269
318
|
|
|
270
319
|
# Rotate major tick labels
|
|
271
320
|
plt.setp(ax.get_xticklabels(), rotation=90, ha='right')
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pytrendy-2.0.0.dev2 → pytrendy-2.0.0.dev4}/pytrendy/post_processing/segments_refine/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|