Segmenter¶
Segmenter
¶
Facade for trend segmentation with multiple algorithm support.
This class provides a unified interface for segmenting time series data into regions with similar trends. It supports multiple detection algorithms and maintains backward compatibility with the legacy API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
list | ndarray | None
|
Array of x values (indices or timestamps). |
None
|
y
|
list | ndarray | None
|
Array of y values (signal values). |
None
|
df
|
DataFrame | None
|
Pandas DataFrame with time series data. |
None
|
column
|
str
|
Column name to use from DataFrame. |
'Close'
|
config
|
Config | None
|
Configuration for the sliding window detector (legacy). |
None
|
n
|
int | None
|
Window size shortcut (legacy, use config instead). |
None
|
detector
|
str | BaseDetector
|
Detection algorithm to use. Can be: - A string name: "sliding_window", "pelt", "bottom_up" - A BaseDetector instance for custom configuration |
'sliding_window'
|
detector_params
|
dict | None
|
Parameters to pass to detector constructor (only used when detector is a string). |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
x |
Input x values as numpy array. |
|
y |
Input y values as numpy array. |
|
segments |
SegmentList | None
|
Detected segments (after calling calculate_segments). |
y_de_trended |
list | ndarray | None
|
Detrended signal values. |
Example (Legacy API - still works): >>> seg = Segmenter(x=x, y=y, n=40) >>> seg.calculate_segments() >>> seg.plot_segments()
Example (New API - recommended): >>> from trend_classifier.detectors import PELTDetector >>> seg = Segmenter(x=x, y=y, detector="pelt", detector_params={"penalty": 5}) >>> result = seg.fit_detect() >>> print(f"Found {len(result.segments)} segments")
Example (Custom detector): >>> detector = PELTDetector(model="linear", penalty=3) >>> seg = Segmenter(x=x, y=y, detector=detector) >>> seg.calculate_segments()
Source code in src/trend_classifier/segmentation.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
__init__(x=None, y=None, df=None, column='Close', config=None, n=None, detector='sliding_window', detector_params=None)
¶
Source code in src/trend_classifier/segmentation.py
calculate_segments(progress_callback=None)
¶
Calculate segments with similar trend for the time series.
This is the main method for detecting trend segments. It uses the configured detector algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress_callback
|
Optional callback function(current, total) for progress reporting during long computations. |
None
|
Returns:
| Type | Description |
|---|---|
list[Segment]
|
List of detected Segment objects. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is not initialized or too short. |
Source code in src/trend_classifier/segmentation.py
fit_detect()
¶
Fit and detect segments in one call.
This is the new recommended API that returns a DetectionResult with additional metadata.
Returns:
| Type | Description |
|---|---|
DetectionResult
|
DetectionResult containing segments, breakpoints, and metadata. |
Example
seg = Segmenter(x=x, y=y, detector="pelt") result = seg.fit_detect() print(f"Algorithm: {result.metadata['algorithm']}") print(f"Breakpoints: {result.breakpoints}")
Source code in src/trend_classifier/segmentation.py
plot_segments(fig_size=(8, 4))
¶
Plot all segments with linear trend lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig_size
|
FigSize
|
Figure size tuple. |
(8, 4)
|
plot_segment(idx, col='red', fig_size=(10, 5))
¶
Plot segment with given index or multiple segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
list[int] | int
|
Index of segment or list of indices. |
required |
col
|
str
|
Color for the segment. |
'red'
|
fig_size
|
FigSize
|
Figure size tuple (width, height). |
(10, 5)
|
Source code in src/trend_classifier/segmentation.py
plot_segment_with_trendlines_no_context(idx, fig_size=(10, 5))
¶
Plot segment with trendlines, without surrounding context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of segment to plot. |
required |
fig_size
|
FigSize
|
Figure size tuple. |
(10, 5)
|
Source code in src/trend_classifier/segmentation.py
plot_detrended_signal(fig_size=(10, 5))
¶
Plot the detrended signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig_size
|
FigSize
|
Figure size tuple. |
(10, 5)
|
Source code in src/trend_classifier/segmentation.py
calc_area_outside_trend()
¶
Calculate area outside trend.
This metric measures how well the detected trends fit the data. Lower values indicate better fit.
Returns:
| Type | Description |
|---|---|
float
|
Normalized sum of absolute deviations from trend lines. |
Source code in src/trend_classifier/segmentation.py
list_detectors()
staticmethod
¶
List available detector algorithms.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of detector names that can be passed to the constructor. |