Metadata-Version: 2.1
Name: dtai-locomotif
Version: 0.2.0
Summary: Discovering variable-length motif sets in multivariate time series using time warping
Home-page: https://github.com/daanvw/locomotif
Author: Daan Van Wesenbeeck
Author-email: daan.vanwesenbeeck@kuleuven.be
License: MIT License
        
        Copyright (c) 2024 ML-KULeuven
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Keywords: time series,motif sets,variable-length,time warping,multivariate
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numba>=0.30.0
Requires-Dist: matplotlib<4.0,>=3.0

<p align="center">
  <picture>
    <source srcset="./logo_dark.png" media="(prefers-color-scheme: dark)" />
    <source srcset="./logo_light.png" media="(prefers-color-scheme: light)" />
    <img src="./logo_light.png" alt="LoCoMotif Logo" width="350" />
  </picture>
</p>

<h1 align="left">LoCoMotif: Discovering time-warped motifs in time series </h1>

This repository contains the implementation of LoCoMotif, a method for time series motif discovery (TSMD). LoCoMotif stands out from existing methods as it can detect variable-length motifs (motifs of different durations), time-warped motifs (with slight temporal misalignments, as handled by Dynamic Time Warping), and multivariate motifs (spanning multiple dimensions). It was introduced in this [publication](https://link.springer.com/article/10.1007/s10618-024-01032-z).


## 🛤️  Installation
The easiest way to install is to use pip.


### Install using pip

```
pip install dtai-locomotif
```

You can also install from source.

### Build from source
First, clone the repository:
```
git clone https://github.com/ML-KULeuven/locomotif.git
```
Then, navigate into the directory and build the package from source:
```
pip install .
```

## 🚂 Usage

A time series is representated as 2d numpy array of shape `(n, d)` where `n` is the length of the time series and `d` the number of dimensions:

```python
f = open(os.path.join("..", "examples", "datasets", "mitdb_patient214.csv"))
ts = np.array([line.split(',') for line in f.readlines()], dtype=np.double)

print(ts.shape)
>>> (3600, 2)
```

To apply LoCoMotif to the time series, simply import the `locomotif` module and call the ``apply_locomotif`` method with suitable parameter values. Note that, we highly advise you to first z-normalize the time series.
```python
import locomotif.locomotif as locomotif 
ts = (ts - np.mean(ts, axis=None)) / np.std(ts, axis=None)
motif_sets = locomotif.apply_locomotif(ts, l_min=216, l_max=360, rho=0.6)
```
The parameters `l_min` and `l_max` respectively represent the minimum and maximum motif length of the representative of a motif set. The parameter ``rho`` determines the ''strictness'' of the LoCoMotif method; or in other words, how similar the subsequences in a motif set are expected to be. The best value of ``rho`` depends heavily on the application; however, in most of our experiments, a value between ``0.6`` and ``0.8`` always works relatively well.  
Optionally, we allow you to choose the allowed overlap between motifs through the `overlap` parameter (which lies between `0.0` and `0.5`), the number of motif sets to be discovered through the `nb` parameter (by default, `nb=None` and LoCoMotif finds all motifs), and whether to use time warping or not through the `warping` parameter (either `True` or `False`)

The result of LoCoMotif is a list of ``(candidate, motif_set)`` tuples, where each `candidate` is the representative subsequence (the most "central" subsequence) of the corresponding `motif_set`. Each `candidate` is a tuple of two integers `(b, e)` representing the start- and endpoint of the corresponding time segment, while each `motif_set` is a list of such tuples.

```python
print(motif_sets)
>>> [((2666, 2931), [(2666, 2931), (1892, 2136), (1038, 1332), (2334, 2665), (628, 1035), (1589, 1892), (1, 260)]), ((2931, 3155), [(2931, 3155), (2136, 2333), (1332, 1558)])]
```

We also include a visualization module, ``visualize``, to plot the time series together with the found motifs:
```python
import locomotif.visualize as visualize
import matplotlib.pyplot as plt

fig, ax = visualize.plot_motif_sets(ts, motif_sets)
plt.show()
```
<div align="center">
	<img src="./examples/example.png">
</div>

More examples can be found in [this folder](https://github.com/ML-KULeuven/locomotif/tree/main/examples).

## 📃 License
This project is licensed under the MIT License - see the [LICENSE](https://github.com/ML-KULeuven/locomotif/blob/main/LICENSE) file for details.
