Skip to content
An implementation of piecewise linear time warping for multi-dimensional time series alignment
Branch: master
Clone or download
Latest commit 0edd0b8 Jun 10, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
affinewarp fix padval Jun 7, 2019
examples add multi-shift-warping prototype May 27, 2019
tests add multi-shift-warping prototype May 27, 2019
.gitignore update online example May 14, 2018 Update Jun 11, 2019
requirements.txt Update requirements.txt Jan 29, 2019 move to squared loss, fix spikedata May 1, 2018
tox.ini init commmit Jan 9, 2018

Piecewise Linear Time Warping

This repo contains research code for time warping multi-dimensional time series. This was developed as part of the following manuscript, which focuses on analysis of large-scale neural recordings (though this code can be also be applied to many other data types):

Discovering precise temporal patterns in large-scale neural recordings through robust and interpretable time warping.
Williams AH, Poole B, Maheswaranathan N, Dhawale AK, Fisher T, Wilson CD, Brann DH, Trautmann E, Ryu S, Shusterman R, Rinberg D, Ölveczky BP, Shenoy KV, Ganguli S (2019). bioRXiv. 661165

The code fits time warping models with either linear or piecewise linear warping functions. These models are more constrained than the classic Dynamic Time Warping (DTW) algorithm, and are thus less prone to overfit to data with high levels of noise. This is demonstrated below on synthethic data. Briefly, a 1-dimensional time series is measured over many repetitions (trials), and exhibits a similar temporal profile but with random jitter on each trial. Simply averaging across trials produces a poor description of the typical time series (red trace at bottom). A linear time warping model identifies a much better prototypical trace (labeled "template"), while accounting for the temporal translations on each trial with warping functions (blue to red linear functions at bottom). On the right, a nonlinear warping model based on DTW (called DBA) is shown for comparison. While DBA can work well on datasets with low noise, linear warping models can be easier to interpret and less likely to overfit.

screen shot 2018-11-05 at 2 03 55 pm

Getting started

After installing (see below), check out the demos in the examples/ folder.

  • Shift.ipynb - demonstrates the essential ideas on a very simple, synthethic dataset containing only one neuron.
  • OFC-2.ipynb - shows a more complete analysis on real data. The tutorial uses a publicly available dataset from, which can be found here. See OFC-2-wrangle-data.ipynb for instructions on downloading and organizing the dataset.

The code is fairly well-documented but the tutorials can still be improved, so let us know if you run into trouble.


This package isn't registered yet, so you need to install manually. Either download or clone the repo:

git clone

Then navigate to the downloaded folder:

cd /path/to/affinewarp

Install the package and requirements:

pip install .
pip install -r requirements.txt

You will need to repeat these steps if we update the code.

Other references / resources

  • tw-pca - Time-Warping Principal Components Analysis, also supports linear and shift-only warping functions. Does not support piecewise linear warping functions and assumes that time series are low-dimensional. Nonlinear warping methods are also supported. See our conference abstract and poster.

  • tslearn - A Python package supporting a variety of time series models, including DTW-based methods.

Contact (or open an issue here).

You can’t perform that action at this time.