TS2G2 stands for "timeseries to graphs and back". The library implements a variety of strategies to convert timeseries into graphs, and convert graphs into sequences. Below, we provide a code snippet to generate a graph from timeseries:
from pipeline import T2G2Pipeline
from strategies.view import get_view_strategy
from strategies.ts2g.strategy_builder import get_ts2g_strategy_builder
from strategies.merge import get_link_multigraph_strategy_builder
import utils
ts_data = utils.CsvFile(some_file_path, "ColumnOfInterest").from_csv()
view_strategy = get_view_strategy("slice")(start_index=60, end_index=120)
view_window = get_view_strategy("sliding_window")(window_size=5)
builder_cls = get_ts2g_strategy_builder("natural_visibility")
ts2g_strategy = builder_cls().get_strategy()
link_builder = get_link_multigraph_strategy_builder()
link_strategy = link_builder().sliding_window()
pipeline = (
T2G2Pipeline()
.load_timeseries(ts_data)
.viewing(view_strategy)
.viewing(view_window)
.to_graph(ts2g_strategy)
.link_graphs(link_strategy)
.combine_identical_subgraphs()
)
G = pipeline.get_Graph()
For a more detailed example, look at the Amazon stocks demo.
Many of the methods implemented in this library are described in Silva, Vanessa Freitas, et al. "Time series analysis via network science: Concepts and algorithms." Wiley Interdisciplinary Reviews: Data Mining and Knowledge Discovery 11.3 (2021): e1404. Nevertheless, the library also includes additional techniques found in other works from the scientific literature.
This package is being developed as part of the Graph-Massivizer project. The package is a joint effort between the Jožef Stefan Institute, the University of Twente, the Vrije Universiteit Amsterdam, the University of Klagenfurt, the University of Bologna, and Peracton.
# | Visibility Graph | Graph type | Constraints | |||||
---|---|---|---|---|---|---|---|---|
Undirected | Directed | Weighted | ||||||
Penetration | Angle | |||||||
1 | Natural Visibility Graph | X | X | X | X | X | ||
2 | Horizontal Visibility Graph | X | X | X | X | X | ||
3 | Difference Visibility Graph | |||||||
4 | Quantile Graph | X | X | |||||
5 | Ordinal Partition Graph | X | X |
# | Visibility Graph | Graph type | Constraints | |||||
---|---|---|---|---|---|---|---|---|
Undirected | Directed | Weighted | ||||||
Penetration | Angle | |||||||
1 | Natural Visibility Graph | ref | ref | ref | ref, ref | |||
2 | Horizontal Visibility Graph | ref | ref | ref | ref, ref | |||
3 | Difference Visibility Graph | |||||||
4 | Quantile Graph | ref | ||||||
5 | Ordinal Partition Graph |
Graphs are converted back to timeseries by sampling node values from the graph following different strategies. Below, we provide a short snippet of code, to illustrate how this can be done.
from strategies.g2ts import get_g2ts_strategy
from strategies.g2ts import StrategySelectNextNodeRandomlyFromFirstGraph, StrategyNextValueInNodeRandomForSlidingWindow
from visualization import draw_sequences
g2ts_cls = get_g2ts_strategy("sliding_window")
g2ts = (
g2ts_cls()
.next_node_strategy(StrategySelectNextNodeRandomlyFromFirstGraph())
.next_value_strategy(StrategyNextValueInNodeRandomForSlidingWindow().skip_every_x_steps(1))
.ts_length(50)
)
sequences = pipeline.to_timeseries(g2ts).sequences
draw_sequences(sequences, node_strategy_name="RandomFromFirstGraph", value_strategy_name="RandomForSlidingWindow")
When choosing the next node, the following strategies have been implemented so far: random node, random node neighbour, random node degree, random walk, random walk with restart, random walk with jump.
When using this work for research purposes, we would appreciate it if the following references could be included:
Below we provide a curated list of papers related to our research in this area: