Swinging Door¶
Implementation of the Swinging Door algorithm in Python.
Example of usage¶
>>> from datetime import datetime
>>> from pandas import read_csv, DataFrame
>>> df = DataFrame(
... [
... {
... "Date": datetime.strptime(date, "%Y-%m-%d"),
... "Price": value
... }
... for date, value in read_csv(
... "https://datahub.io/core/oil-prices/r/wti-daily.csv"
... ).values.tolist()
... ]
... )
>>> print(len(df))
9895
>>> df.plot(x="Date", y="Price")
>>> from swinging_door import swinging_door
>>> compress = DataFrame(
... list(
... {
... "Date": datetime.fromtimestamp(date),
... "Price": value
... }
... for date, value in swinging_door(
... iter(
... (date.timestamp(), value)
... for date, value in df.values.tolist()
... ), deviation=.5
... )
... )
... )
>>> print(len(compress))
3392
>>> compress.plot(x="Date", y="Price")
Source¶
swinging_door¶
Implementation of the SwingingDoor algorithm in Python.
- swinging_door.swinging_door(data: List[Point], deviation: float = 0.1, mode: bool = False, step: int = 10) Generator[Point, None, None][source]¶
Implementation of the SwingingDoor algorithm.
- Parameters:
- Return type:
Generator[Point, None, None]
- Returns:
Compressed data.
>>> list(swinging_door([ ... (0., 5.0), (1., 5.5), (2., 4.2), ... (3., 5.8), (4., 5.2), (5., 6.8), ... ], deviation=1.)) [(0.0, 5.0), (4.5, 5.5), (5.0, 6.8)]
>>> list(swinging_door([ ... (0., 5.0), (1., 5.5), (2., 4.2), ... (3., 5.8), (4., 5.2), (5., 2.8), ... ], deviation=1.)) [(0.0, 5.0), (4.5, 3.5), (5.0, 2.8)]
>>> list(swinging_door([ ... (0., 5.0), (1., 5.5), (2., 4.2), ... (3., 5.8), (4., 5.2), (5., 6.8), ... ], deviation=1., mode=True)) [(0.0, 5.0), (4.0, 5.2), (5.0, 6.8)]
>>> list(swinging_door([ ... (0., 5.0), (1., 5.5), (2., 4.2), ... (3., 5.8), (4., 5.2), (5., 6.8), ... ], deviation=1., mode=True, step=2)) [(0.0, 5.0), (2.0, 4.2), (5.0, 6.8)]