Skip to main content

A tool to find patterns in time series dataset and forecast

Project description

shapefinder

shapefinder is a historical pattern matching tool for forecasting purposes. Originially built for social science application like conflict and migration flow, it can be adapted for any purpose. This library includes various functions for pattern discovery, dynamic prediction, and data visualization. It identifies and matches temporal patterns in data to forecast future trajectories based on similarity to past episodes.

📦 Installation

Clone the repository and install dependencies:

git clone https://github.com/ThomasSchinca/shapefinder.git
cd shapefinder
pip install -r requirements.txt

🛠️ Usage

Shape

from shapefinder import Shape

### Init the Shape object 
shape = Shape()

### Three ways to set your shape wanted 

# 1. Random shape with a given window length 
shape.set_random_shape(window=10)   # in this exemple, the shape has 10 random values between 0 and 1

# 2. Shape drawn by user
shape.draw_shape(window=10) # in this exemple, a pop up appears to let the user draw the shape wanted with a 10 timestamp window. 

# 3. Shape based on values given by user (list, series or numpy format)
shape.set_shape([0,0.5,1,0.5,0])   # in this exemple, the shape has 5 values that draw an "up-and-down" shape. 


### Visualize the shape created 
shape.plot()    # A figure is created 
Input Shape

Once the Shape is defined, the finder is set to find the shape in the given dataset.

finder

from shapefinder import Shape
from shapefinder import finder
import pandas as pd 
import numpy as np

### Init the Shape object 
shape = Shape()
shape.set_shape([0,0.5,1,0.5,0])   # we take the "up-and-down" shape defined before

### Define the dataset to look into 
# We create a white noise time series 

data = pd.DataFrame(np.random.randn(1000), index=pd.date_range(start='2020-01-01', periods=1000, freq='D'))

### Init the finder object 
find = finder(data,Shape=shape)

### Find the 'up-and-down' pattern in the dataset

# Look for patterns with euclidean distance lower than 0.5
find.find_patterns() 
# Look for patterns with euclidean distance lower than 1
find.find_patterns(min_d=1) 
# Look for patterns with Dynamic Time Warping distance lower than 0.25
find.find_patterns(min_d=0.25,metric='dtw')
# Look for patterns with Dynamic Time Warping distance lower than 0.25 and allow window with 4, 5 and 6 timestamp 
# window to look = 5 +/- dtw_sel(1)
find.find_patterns(min_d=0.25,metric='dtw',dtw_sel=1)
# Same but allowing overlapping windows 
find.find_patterns(min_d=0.25,metric='dtw',dtw_sel=1,select=False)
# Force the minimum number of similar patterns to 5 
find.find_patterns(min_d=0.25,metric='dtw',dtw_sel=1,select=True,min_mat=5)

### Plot the patterns found
# Plot individual figures 
find.plot_sequences(how='units') 
# Plot global figure with all patterns
find.plot_sequences(how='total') 
Total similar sequences
The final step is then to create the future scenario and predict.

Scenario and prediction creation

### Predict the next three timestamps based on the found patterns
# First, we generate the scenarios using an horizon of three and a clustering threshold of 3. 
find.create_sce(horizon=3,clu_thres=3) 
# We can check how do the scenerio look like with their associated probabilities (in the legend).
find.plot_scenario()
Scenario with their associated probabilities
# We can also just predict a point estimate using the highest probable scenario
pred = find.predict(horizon=3,clu_thres=3)

📊 Input Data Format

  • Shape : list, pandas.Series or numpy.array

  • Shape3D : np.ndarray, with shape(x,y,time)

  • finder : pandas.DataFrame with index as Date.

  • finder_3D : np.ndarray with (x,y,time)

  • finder_multi : list of pandas.DataFrame with index as Date

  • finder_multi_static : list of pandas.DataFrame with index as Date

🧪 Examples

More examples specific for :

  • Shape_3D, finder_3D : examples/3D_example.ipynb
  • finder_multi : examples/Multi_Shape_example.ipynb
  • finder_multi_static : examples/Shape_with_covariates_example.ipynb

📚 Related Papers

ShapeFinder was developed as part of a research project on conflict forecasting and migration flow using historical pattern matching. The following papers use ShapeFinder's methods and code:

📄 Citation

If you use shapefinder in academic work, please cite:

@misc{shapefinder2025,
  author = {Thomas Schincariol},
  title = {ShapeFinder: Historical Pattern Matching for Forecasting},
  year = {2025},
  howpublished = {\url{https://github.com/ThomasSchinca/shapefinder}}
}

📬 Contact

For questions, reach out via GitHub Issues or email schincat@tcd.ie.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shapefinder-0.1.0.tar.gz (1.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shapefinder-0.1.0-py3-none-any.whl (40.3 kB view details)

Uploaded Python 3

File details

Details for the file shapefinder-0.1.0.tar.gz.

File metadata

  • Download URL: shapefinder-0.1.0.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.5

File hashes

Hashes for shapefinder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 feb26687f5730a68eef5056dafea897ff0df20ee50d65844e59bde1afcc0b9a5
MD5 378670c689bc11fbd3b29c1f3d89a546
BLAKE2b-256 b0a74ee9c68eaab9940317d8e34f399b970e4478b2a3255f56577c30a9a9cfb5

See more details on using hashes here.

File details

Details for the file shapefinder-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: shapefinder-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 40.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.5

File hashes

Hashes for shapefinder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd959de41ab75e3679119d03f378edd7a19f85d48ee213e12b06ab273c44bd86
MD5 0ae6ccf3ade2395b2fd4c86be496d75a
BLAKE2b-256 86f11cc4553b9419c38cbb1bf9226030fdeb3a67357d5eb8394b4a30b2ca6ae6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page