Skip to content
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

[ENH] Window based segmentation #262

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tsml_eval/_wip/series_transformer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Piecewise Linear Approximation."""

__all__ = [
"PiecewiseLinearApproximation",
]

from _pla import PiecewiseLinearApproximation
86 changes: 86 additions & 0 deletions tsml_eval/_wip/series_transformer/_bu_old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from base import BasePLA
import numpy as np
import math
__maintainer__ = []
__all__ = ["BottomUp"]

class BottomUp(BasePLA):
"""
Piecewise Linear Bottom-Up.

Uses a bottom-up algorithm to traverse the dataset in an offline manner.

Parameters
----------
max_error: float
The maximum error valuefor the function to find before segmenting the dataset

References
----------
.. [1] Keogh, E., Chu, S., Hart, D. and Pazzani, M., 2001, November.
An online algorithm for segmenting time series. (pp. 289-296).
"""

def __init__(self, max_error):
super().__init__(max_error)

#clean the code
def transform(self, time_series):
"""Transform a time series

Parameters
----------
time_series : np.array
1D time series to be transformed.

Returns
-------
list
List of transformed segmented time series
"""

seg_ts = []
merge_cost = []
for i in range(0, len(time_series), 2):
seg_ts.append(self.create_segment(time_series[i: i + 2]))
for i in range(len(seg_ts) - 1):
merge_cost.append(self.calculate_error(seg_ts[i] + seg_ts[i + 1]))

merge_cost = np.array(merge_cost)

while len(merge_cost) != 0 and min(merge_cost) < self.max_error:
pos = np.argmin(merge_cost)
seg_ts[pos] = self.create_segment(np.concatenate((seg_ts[pos], seg_ts[pos + 1])))
seg_ts.pop(pos + 1)
if (pos + 1) < len(merge_cost):
merge_cost = np.delete(merge_cost, pos + 1)
else:
merge_cost= np.delete(merge_cost, pos)

if pos != 0:
merge_cost[pos - 1] = self.calculate_error(np.concatenate((seg_ts[pos - 1], seg_ts[pos])))

if((pos + 1) < len(seg_ts)):
merge_cost[pos] = self.calculate_error(np.concatenate((seg_ts[pos], seg_ts[pos + 1])))


return seg_ts



def transform_flatten(self, time_series):
"""Transform a time series and return a 1d array

Parameters
----------
time_series : np.array
1D time series to be transformed.

Returns
-------
list
List of a flattened transformed time series
"""

pla_timeseries = self.transform(time_series)
return np.concatenate(pla_timeseries)
Loading