ts2g2
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:
# load time series from a file
timegraph = model.Timeseries(CsvStock(some_file_path, "ColumnOfInterest").from_csv())\
# and preprocess the timeseries with multiple preprocessing strategies
.with_preprocessing(model.TimeseriesPreprocessingComposite()\
.add_strategy(model.TimeseriesPreprocessingSegmentation(60, 120))\
.add_strategy(model.TimeseriesPreprocessingSlidingWindow(5)))\
# then create a graph from the timeseries, following a particular strategy
.to_graph(tgs.BuildTimeseriesToGraphNaturalVisibilityStrategy().get_strategy())\
# link graphs that result from the same timeseries, but at different sliding window frames
.link(mgl.LinkGraphs().sliding_window())\
# and combine identical graphs that result from the abovementioned time windows into single nodes
.combine_identical_nodes_slid_win()\
# finally, draw the graph
.draw("red")
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.
Converting timeseries into graphs
Implemented features
# | 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 |
References table
# | 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 |
Converting graphs into sequences
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.
timegraph.to_sequence(model.ToSequenceVisitorSlidingWindow()\
.next_node_strategy(tts.StrategySelectNextNodeRandomlyFromFirstGraph())\
.next_value_strategy(tts.StrategyNextValueInNodeRandomForSlidingWindow().skip_every_x_steps(1))\
.ts_length(50))\
.draw_sequence()
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.