# enable automatic reloading of the notebook
%load_ext autoreload
%autoreload 2
The autoreload extension is already loaded. To reload it, use: %reload_ext autoreload
Compact tutorial¶
The following notebook demonstrates how the ts2g2 library can convert time series into graphs and graphs into time series. To that end, we use a dataset with the history of daily prices of Amazon stock (AMZN) and Apple stock (APPLE). All the column descriptions are provided. The currency is USD.
import os
import sys
nb_dir = os.path.split(os.getcwd())[0]
if nb_dir not in sys.path:
sys.path.append(nb_dir)
from core.model import Timeseries, TimeseriesPreprocessing, TimeseriesPreprocessingSegmentation, TimeseriesPreprocessingSlidingWindow, TimeseriesPreprocessingComposite, TimeseriesView, TimeGraph, ToSequenceVisitorSlidingWindow, ToSequenceVisitor, ToSequenceVisitorOrdinalPartition
from tsg_io.input import CsvFile
from from_graph.strategy_to_time_sequence import StrategyNextValueInNodeRandom, StrategyNextValueInNodeRandomForSlidingWindow, StrategyNextValueInNodeRoundRobin, StrategyNextValueInNodeRoundRobinForSlidingWindow, StrategySelectNextNodeRandomlyFromNeighboursAcrossGraphs, StrategySelectNextNodeRandomlyFromNeighboursFromFirstGraph, StrategySelectNextNodeRandomly, StrategySelectNextNodeRandomDegree, StrategySelectNextNodeRandomWithRestart, StrategyNextValueInNodeOrdinalPartition
from to_graph.strategy_linking_graph import StrategyLinkingGraphByValueWithinRange, LinkNodesWithinGraph
from to_graph.strategy_linking_multi_graphs import LinkGraphs
from to_graph.strategy_to_graph import BuildTimeseriesToGraphNaturalVisibilityStrategy, BuildTimeseriesToGraphHorizontalVisibilityStrategy, BuildTimeseriesToGraphOrdinalPartition, BuildTimeseriesToGraphQuantile
import warnings
warnings.filterwarnings('ignore')
Accessing data¶
We first get path to the data.
amazon_path = os.path.join(os.getcwd(), "amazon", "AMZN.csv")
apple_path = os.path.join(os.getcwd(), "apple", "APPLE.csv")
Converting time series to graph¶
Here are two examples of conversion of data from time serie to graph. Second one is inpeated using sliding window mechanism. That means its nodes are smaller graphs inpeated based on a segments of data (length = 5).
timegraph_1 = Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(60, 90))\
.to_graph(BuildTimeseriesToGraphNaturalVisibilityStrategy().with_limit(1).get_strategy())\
.add_edge(0,2)\
.add_edge(13, 21, weight = 17)\
.link(LinkNodesWithinGraph().by_value(StrategyLinkingGraphByValueWithinRange(2)).seasonalities(15))\
.draw()
timegraph_2 = Timeseries(CsvFile(apple_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingComposite()\
.add(TimeseriesPreprocessingSegmentation(60, 120))\
.add(TimeseriesPreprocessingSlidingWindow(5)))\
.to_graph(BuildTimeseriesToGraphNaturalVisibilityStrategy().get_strategy())\
.link(LinkGraphs().sliding_window())\
.combine_identical_subgraphs()\
.draw()
Combining multiple graphs¶
We can store and combine multiple of already inpeated objects TimeSeriesToGraph into one object MultivariateTimeSeriesToGraph
timegraph_3 = Timeseries(CsvFile(apple_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(60, 90))\
.add(Timeseries(CsvFile(apple_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(90, 120)))\
.add(Timeseries(CsvFile(apple_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(150, 180)))\
.to_graph(BuildTimeseriesToGraphNaturalVisibilityStrategy().with_limit(1).get_strategy())\
.link(LinkGraphs().time_cooccurrence())\
.link(LinkNodesWithinGraph().by_value(StrategyLinkingGraphByValueWithinRange(0.5)))\
.combine_identical_nodes()\
.draw()
timegraph_4 = Timeseries(CsvFile(apple_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingComposite()\
.add(TimeseriesPreprocessingSegmentation(60, 110))\
.add(TimeseriesPreprocessingSlidingWindow(5)))\
.add(Timeseries(CsvFile(apple_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingComposite()\
.add(TimeseriesPreprocessingSegmentation(120, 170))\
.add(TimeseriesPreprocessingSlidingWindow(5)))\
.add(Timeseries(CsvFile(apple_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingComposite()\
.add(TimeseriesPreprocessingSegmentation(190, 240))\
.add(TimeseriesPreprocessingSlidingWindow(5)))))\
.to_graph(BuildTimeseriesToGraphNaturalVisibilityStrategy().get_strategy())\
.link(LinkGraphs().sliding_window().time_cooccurrence())\
.combine_identical_subgraphs()\
.link(LinkNodesWithinGraph().seasonalities(15))\
.draw()
timegraph_5 = Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(60, 120))\
.add(Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(120, 180)))\
.add(Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(500, 560)))\
.add(Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(700, 760)))\
.add(Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(1000, 1060)))\
.to_histogram(15)\
.to_graph(BuildTimeseriesToGraphOrdinalPartition(10, 5).get_strategy())\
.link(LinkGraphs().time_cooccurrence())\
.add_edge(0,2)\
.link(LinkNodesWithinGraph().seasonalities(4))\
.draw()
timegraph_6 = Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(60, 120))\
.to_graph(BuildTimeseriesToGraphQuantile(4, 1).get_strategy())\
.add_edge(0,2)\
.link(LinkNodesWithinGraph().seasonalities(4))\
.draw()
timegraph_dtw = Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(60, 90))\
.add(Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(90, 130)))\
.add(Timeseries(CsvFile(amazon_path, "Close").from_csv())\
.with_preprocessing(TimeseriesPreprocessingSegmentation(130, 200)))\
.to_graph(BuildTimeseriesToGraphNaturalVisibilityStrategy().with_limit(1).get_strategy())\
.link(LinkGraphs().dynamic_timewarping())\
.draw()
Back to time series¶
We can convert existing graphs back to time series based on our strategy.
timegraph_1.to_sequence(ToSequenceVisitor()\
.next_node_strategy(StrategySelectNextNodeRandomDegree())\
.next_value_strategy(StrategyNextValueInNodeRoundRobin().skip_every_x_steps(1))\
.ts_length(100))\
.draw_sequence()
<core.model.TimeGraph at 0x260506f9700>
timegraph_2.to_sequence(ToSequenceVisitorSlidingWindow()\
.next_node_strategy(StrategySelectNextNodeRandomly())\
.next_value_strategy(StrategyNextValueInNodeRandomForSlidingWindow().skip_every_x_steps(1))\
.ts_length(50))\
.draw_sequence()
<core.model.TimeGraph at 0x26050348a40>
We can also convert our multivriate graphs back to multiple time series.
timegraph_3.to_sequence(ToSequenceVisitor()\
.next_node_strategy(StrategySelectNextNodeRandomWithRestart().change_graphs_every_x_steps(2))\
.next_value_strategy(StrategyNextValueInNodeRoundRobin().skip_every_x_steps(1))\
.ts_length(50))\
.draw_sequence()
<core.model.TimeGraph at 0x26050e59460>
timegraph_4.to_sequence(ToSequenceVisitorSlidingWindow()\
.next_node_strategy(StrategySelectNextNodeRandomlyFromNeighboursAcrossGraphs())\
.next_value_strategy(StrategyNextValueInNodeRoundRobinForSlidingWindow())\
.ts_length(100))\
.draw_sequence()
<core.model.TimeGraph at 0x26050f38f20>
timegraph_5.to_sequence(ToSequenceVisitorOrdinalPartition()\
.next_node_strategy(StrategySelectNextNodeRandomWithRestart())\
.next_value_strategy(StrategyNextValueInNodeOrdinalPartition())\
.ts_length(100))\
.draw_sequence()
<core.model.TimeGraph at 0x26050388e30>
timegraph_dtw.to_sequence(ToSequenceVisitor()\
.next_node_strategy(StrategySelectNextNodeRandomlyFromNeighboursAcrossGraphs())\
.next_value_strategy(StrategyNextValueInNodeRandom())\
.ts_length(100))\
.draw_sequence()
<core.model.TimeGraph at 0x26051002b40>