Skip to main content

DTW-SOM: Self-organizing map for time-series data

The method DTW-SOM (Dynamic Time Warping Self-Organizing Map) was built for the paper Exploring time-series motifs through DTW-SOM. In the github repository, you'll find all the work presented in the paper.

DTW-SOM is a vanilla Self-Organizing Map with three main differences, namely (1) the use the Dynamic Time Warping distance instead of the Euclidean distance, (2) the adoption of two new network initialization routines (a random sample initialization and an anchor initialization) and (3) the adjustment of the Adaptation phase of the training to work with variable-length time-series sequences.

In the paper, we argue that visually exploring time-series motifs computed by motif discovery algorithms can be useful to understand and debug results and we propose the use of DTW-SOM on the list of motif’s centers to conduct these explorations. We then test DTW-SOM in a synthetic motif dataset and a real time-series dataset called GunPoint. After an exploration of results, we conclude that DTW-SOM is capable of extracting relevant information from a set of motifs and display it in a visualization that is space-efficient.

Github project structure

├── notebooks          <- Jupyter notebooks use to test dtwsom and to run the anlsysis for the paper (including the plots)
├── paper              <- Folder the the PDF and latex project for the paper
├── src                <- Folder with the dtwsom module
├── README.md          <- The top-level README for this project
├── requirements.txt   <- The requirements file for reproducing the environment used in the paper

Prerequisites

In order to run the DTW-SOM package, you need the following packages:

dtaidistance==1.2.3
matplotlib==3.1.2
numpy==1.18.1
pyclustering==0.9.3.1
scipy==1.4.1

In addition to these, if you wish to run the notebooks in this repository, then you need the following packages:

jupyterlab==1.2.5
matplotlib==3.1.2
matrixprofile-ts==0.0.9

Installing

This packages is available on PyPI and thus can be directly installed with pip:

pip install dtw_som

Alternatively, this package can installed from source by cloning this repository and installing it manually with the command:

python setup.py install

Example Code

Import packages and generate a dummy dataset with 2 clusters, a noisy sine curve and a noise line centered at 10:

import dtwsom
import math
import random
import numpy as np
import matplotlib.pyplot as plt
from pyclustering.nnet.som import type_conn


def gen_noisy_sine_list(f0, fs, mean_dur, size):
    final_list = []
    for i in range(size):
        dur = random.sample([mean_dur-1, mean_dur, mean_dur+1], 1)[0]
        t = np.arange(dur)
        sinusoid = np.sin(2*np.pi*t*(f0/fs))
        noise = np.random.normal(0,0.3, dur)
        noisy_sinusoid = noise + sinusoid
        final_list.append(noisy_sinusoid)
    return final_list

def gen_noisy_list(mean_dur, size):
    final_list = []
    for i in range(size):
        dur = random.sample([mean_dur-1, mean_dur, mean_dur+1], 1)[0]
        noise = np.random.normal(0,0.3, dur)+10
        final_list.append(noise)
    return final_list


sin_dataset = gen_noisy_sine_list(1, 10, 25, 50) + gen_noisy_list(20, 50)
random.shuffle(sin_dataset)

Define and train the network:

rows = 3
cols = 3
structure = type_conn.grid_four
network = dtwsom.DtwSom(rows, cols, structure)

network.train(sin_dataset, 20)

After training, you can visualise the U-matrix the Winner matrix:

network.show_distance_matrix()
network.show_winner_matrix()

Finally, you can also visualize the each unit as a time-series:

n_neurons = network._size
fig, axs = plt.subplots(3,3,figsize=(20, 10), sharey=True)
for neuron_index in range(n_neurons):
    col = math.floor(neuron_index/3)
    row = neuron_index % 3
    neuron_weights = network._weights[neuron_index]
    axs[row, col].plot(np.arange(len(neuron_weights)), neuron_weights, label=str(neuron_index))
    axs[row, col].set_ylabel("Neuron: "+str(neuron_index))
plt.show()

To confirm the output of this example, check the following notebook.

Release files for dtw-som 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dtw-som 1.0.9
File Size Uploaded
dtw-som-1.0.9.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dtw-som 1.0.9
File Interpreter ABI Platform
dtw_som-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 kB

Release files / dtw-som-1.0.9.tar.gz

Download URL dtw-som-1.0.9.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e86490f4afc00cf1dfaa25f50190ed56f91997ee10d7efebba7b3f9f2befff60
BLAKE2b-256 checksum
How to use checksums
ee29f52a81dfb4ec8a09a35fcc3396b6b6e3bd02c255f97d2b6e3b9c1eaa7cee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

Release files / dtw_som-1.0.9-py3-none-any.whl

Download URL dtw_som-1.0.9-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b154cee711009d64a0469189d17ff22923a002491dbdbeb9dbcf3818ff11e52d
BLAKE2b-256 checksum
How to use checksums
6c8ad62016d18f22ab0d2bb9838caadbe28870fd16f5663c4c48f014b2d12ba0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page