Sausage Links¶
Implementation of the Sausage Links algorithm base Swinging Door 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 sausage_links import sausage_links
>>> compress = DataFrame(
... list(
... {
... "Date": datetime.fromtimestamp(date),
... "Price": value
... }
... for date, value in sausage_links(
... iter(
... (date.timestamp(), value)
... for date, value in df.values.tolist()
... ), deviation=(1, .5), max_len=604_800,
... auto_dev_factor=200_000, ema_alpha=0.5
... )
... )
... )
>>> print(len(compress))
4177
>>> compress.plot(x="Date", y="Price")
Source¶
sausage_links¶
Implementation of the Sausage Links algorithm base Swinging Door in Python.
- sausage_links._sloping_calc(stretch: Stretch, deviation: Deviation) Slopings[source]¶
Calculate slopings upper and lower.
- Parameters:
stretch (Stretch) – stretch;
deviation (Deviation) – compression deflection.
- Return type:
Slopings
- Returns:
Slopings upper and lower.
- Raises:
ValueError – if division by 0 occurs during slope calculation.
>>> _sloping_calc(((1, 6), (2, 6.5)), 1) (1.5, -0.5)
>>> _sloping_calc(((1, 6), (2, 6.5)), (1, 1)) (1.5, -0.5)
>>> _sloping_calc(((1, 6), (1, 6.5)), 1) Traceback (most recent call last): ... ValueError: The division by 0 occurs during the calculation of the slope.
- sausage_links.sausage_links(source: Source, deviation: Deviation = 0.1, max_len: Number = 0, auto_dev_factor: Number = 0, ema_alpha: Number = 0.3) Generator[Point, None, None][source]¶
Implementation of the Sausage Links algorithm base Swinging Door in Python.
- Parameters:
source (Source) – source data;
deviation (Deviation) – compression deflection;
max_len (Number) – maximum corridor length;
auto_dev_factor (Number) – multiplier for EMA;
ema_alpha (Number) – smoothing coefficient for EMA. [0; 1].
- Return type:
Generator[Point, None, None]
- Returns:
Compressed data.
>>> list(sausage_links(iter([ ... (1, 6), (2, 6.5), (3, 5.5), ... (4, 6.5), (5, 8), (6, 7.5), ... (7, 8), (8, 9.5), ... ]), 1)) [(1, 6), (7, 8), (8, 9.5)]
>>> list(sausage_links(iter([ ... (1, 6), (2, 6.5), (3, 5.5), ... (4, 6.5), (5, 8), (6, 7.5), ... (7, 8), (8, 6), ... ]), 1)) [(1, 6), (7, 8), (8, 6)]
>>> list(sausage_links(iter([ ... (1, 6), (2, 6.5), (3, 5.5), ... (4, 6.5), (5, 8), (6, 7.5), ... (7, 8), ... ]), 1, 1)) [(1, 6), (2, 6.5), (3, 5.5), (4, 6.5), (5, 8), (6, 7.5), (7, 8)]
>>> list(sausage_links(iter([ ... (1, 6), (2, 6.5), (3, 5.5), ... (4, 6.5), (5, 8), (6, 7.5), ... (7, 9.5), (8, 8), ... ]), 1, auto_dev_factor=1)) [(1, 6), (4, 6.5), (5, 8), (8, 8)]
>>> list(sausage_links(iter([ ... (1, 6), (2, 6.5), (3, 5.5), ... (4, 6.5), (5, 8), (6, 7.5), ... (7, 3), (8, 8), ... ]), 1, auto_dev_factor=1)) [(1, 6), (4, 6.5), (5, 8), (6, 7.5), (7, 3), (8, 8)]
>>> list(sausage_links(iter([ ... (1, 6), (2, 6.5), (3, 5.5), ... (4, 6.5), (5, 8), (6, 7.5), ... (7, 8), ... ]), 0)) [(1, 6), (2, 6.5), (3, 5.5), (4, 6.5), (5, 8), (6, 7.5), (7, 8)]
>>> list(sausage_links(iter([]), 1)) []
>>> list(sausage_links(iter([(1, 6),]), 1)) [(1, 6)]
>>> list(sausage_links(iter([(1, 6),(1, 6.5),]), 1)) Traceback (most recent call last): ... ValueError: The division by 0 occurs during the calculation of the slope.