Swinging Door¶
Implementation of the SwingingDoor algorithm in Python.
Example of usage¶
>>> from datetime import datetime
>>> from pandas import read_csv, DataFrame
>>> def get_data(data):
... for date, value in data.values.tolist():
... yield datetime.strptime(date, "%Y-%m-%d").timestamp(), value
>>> df = read_csv("https://datahub.io/core/oil-prices/r/wti-daily.csv")
>>> print(len(df))
8737
>>> df.plot(x="Date", y="Price")
>>> from swinging_door import swinging_door
>>> compress = DataFrame(
... tuple(
... {
... "Date": datetime.fromtimestamp(date),
... "Price": value
... }
... for date, value in swinging_door(
... get_data(df), deviation=.5
... )
... )
... )
>>> print(len(compress))
2584
>>> compress.plot(x="Date", y="Price")
Source¶
swinging_door¶
Implementation of the SwingingDoor algorithm in Python.
- class swinging_door.Point(abscissa: float, ordinate: float)[source]¶
Bases:
objectPoint in rectangular coordinate system.
- __call__() Tuple[float, float][source]¶
A call that returns the coordinates of a point.
>>> Point(1.0, 2.0)() (1.0, 2.0)
- __init__(abscissa: float, ordinate: float) None[source]¶
Class constructor specifying the coordinates of the point.
>>> Point(1.0, 2.0) Point(1.0, 2.0)
- swinging_door.swinging_door(data: Generator[Tuple[float, float], None, None], deviation: float = 0.1) Generator[Tuple[float, float], None, None][source]¶
Implementation of the SwingingDoor algorithm.
- Parameters
- Return type
- Returns
Compressed data.
>>> def data(values): ... x = 0.0 ... for y in values: ... yield x, y ... x += 1.0 >>> tuple(swinging_door(data([ ... 2.1, 3.1, 4.1, 4.6 ... ]), deviation=0.1)) ((0.0, 2.1), (2.444444444444445, 4.372222222222222))
>>> tuple(swinging_door(data([ ... 4.1, 3.1, 2.1, 4.6 ... ]), deviation=0.1)) ((0.0, 4.1), (2.088235294117647, 2.270588235294118))