ts2vg

The Python ts2vg package provides high-performance algorithm implementations to build visibility graphs from time series data.

The visibility graphs and some of their properties (e.g. degree distributions) are computed quickly and efficiently, even for time series with millions of observations thanks to the use of NumPy and a custom C backend (via Cython) developed for the visibility algorithms.

The visibility graphs are provided according to the mathematical definitions described in:

  • Lucas Lacasa et al., "From time series to complex networks: The visibility graph", 2008.
  • Lucas Lacasa et al., "Horizontal visibility graphs: exact results for random time series", 2009.

An efficient divide-and-conquer algorithm is used to compute the graphs, as described in:

  • Xin Lan et al., "Fast transformation from time series to visibility graphs", 2015.

Installation

The latest released ts2vg version is available at the Python Package Index (PyPI) and can be easily installed by running:

pip install ts2vg

For other advanced uses, to build ts2vg from source Cython is required.

Basic usage

Visibility graph

Building visibility graphs from time series is very simple:

from ts2vg import NaturalVG

ts = [1.0, 0.5, 0.3, 0.7, 1.0, 0.5, 0.3, 0.8]

g = NaturalVG()
g.build(ts)

edges = g.edges

The time series passed can be a list, a tuple, or a numpy 1D array.

Horizontal visibility graph

We can also obtain horizontal visibility graphs in a very similar way:

from ts2vg import HorizontalVG

ts = [1.0, 0.5, 0.3, 0.7, 1.0, 0.5, 0.3, 0.8]

g = HorizontalVG()
g.build(ts)

edges = g.edges

Degree distribution

If we are only interested in the degree distribution of the visibility graph we can pass only_degrees=True to the build method. This will be more efficient in time and memory than computing the whole graph.

g = NaturalVG()
g.build(ts, only_degrees=True)

ks, ps = g.degree_distribution

Directed visibility graph

g = NaturalVG(directed='left_to_right')
g.build(ts)

Weighted visibility graph

g = NaturalVG(weighted='distance')
g.build(ts)

For more information and options see: Examples and API Reference.

Interoperability with other libraries

The graphs obtained can be easily converted to graph objects from other common Python graph libraries such as igraph, NetworkX and SNAP for further analysis.

The following methods are provided:

  • as_igraph()
  • as_networkx()
  • as_snap()

For example:

g = NaturalVG()
g.build(ts)

nx_g = g.as_networkx()

Command line interface

ts2vg can also be used as a command line program directly from the console:

ts2vg ./timeseries.txt -o out.edg

For more help and a list of options run:

ts2vg --help

GitHub

https://github.com/CarlosBergillos/ts2vg