### Stumpy Integration Example Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Demonstrates integrating Stumpy with another library or framework. This requires careful setup of dependencies. ```python from my_framework import App import stumpy app = App() app.register_processor(stumpy.process) ``` -------------------------------- ### Install from Source with Pixi Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/install.md Install project dependencies using pixi. ```bash pixi install ``` -------------------------------- ### Setup STUMPY Development Environment Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Contribute.ipynb Installs STUMPY and its dependencies from source for development using venv. This script first uninstalls any existing version. ```bash ./setup.sh dev ``` -------------------------------- ### Install from Source with UV Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/install.md Synchronize project dependencies using uv. ```bash uv sync ``` -------------------------------- ### Install STUMPY and Dependencies Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Contribute.md Use these scripts to set up your development environment. './setup.sh dev' installs STUMPY and its dependencies, while './conda.sh' assists with conda environments. Running './setup.sh dev && ./test.sh' verifies the installation by running unit tests. ```bash ./setup.sh dev ``` ```bash ./conda.sh ``` ```bash ./setup.sh dev && ./test.sh ``` -------------------------------- ### Find Matrix Profile Index Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md This example shows how to find the index of the minimum value in the matrix profile, which corresponds to the start of the most repeating subsequence. ```python import stumpy import numpy as np # Assume matrix_profile is already computed # For demonstration, let's use a pre-computed one or recompute matrix_profile = np.random.rand(1000) matrix_profile = stumpy.stump(matrix_profile, m=50) # Find the index of the minimum value in the matrix profile min_idx = np.argmin(matrix_profile) print(f"The index of the minimum matrix profile value is: {min_idx}") ``` -------------------------------- ### Install Dependencies from Source with Pip Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/install.md Install required dependencies using pip and then install Stumpy from source. ```bash python -m pip install -r requirements.txt python -m pip install . ``` -------------------------------- ### Stumpy Configuration Example Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Illustrates how to configure Stumpy with specific parameters. Adjust settings based on your project's needs. ```python import stumpy config = { "param1": "value1", "param2": 123 } stumpy.configure(config) ``` -------------------------------- ### Install Stumpy Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/WIP/Tutorial_meter_swapping.ipynb Install the stumpy library using pip. This is a prerequisite for running the code in this tutorial. ```python !pip install stumpy ``` -------------------------------- ### STUMPY: Basic Time Series Analysis Setup Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md This is a minimal setup for using STUMPY, requiring only the time series data and the window size. It computes the Matrix Profile, which is the foundation for many time series analysis tasks. ```python import stumpy import numpy as np # Sample time series data # Replace this with your actual time series data time_series = np.sin(np.linspace(0, 100, 1000)) # Define the window size (number of data points in each subsequence) window_size = 64 # Compute the Matrix Profile matrix_profile = stumpy.stump(time_series, m=window_size) print(f"Matrix Profile computed with window size {window_size}.") print(f"Shape of Matrix Profile: {matrix_profile.shape}") ``` -------------------------------- ### Stumpy: Initialize and Use Stumpy Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Demonstrates the basic initialization and usage of the Stumpy library. This is a starting point for integrating Stumpy into your projects. ```python import stumpy import numpy as np # Create a dummy time series X = np.random.rand(100, 10) # Initialize Stumpy stumpy_instance = stumpy.Stumpy(X) # Perform some operation (example: compute nearest neighbors) neighbors = stumpy_instance.nearest_neighbors() print(neighbors) ``` -------------------------------- ### Stumpy Matrix Profile Computation with Index Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Annotation_Vectors.md This example computes the matrix profile and its corresponding index using `stumpy.stump`. The index indicates the start of the nearest neighbor subsequence. ```python import stumpy import numpy as np # Example time series data x = np.random.rand(1000) # Compute the matrix profile and index mp, mp_idx = stumpy.stump(x, m=50) print(mp_idx) ``` -------------------------------- ### Compute Matrix Profile with Seven Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with seven data points. Window sizes of 1 through 6 are possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]) # Compute the matrix profile with window size 6 matrix_profile = stumpy.stump(data, m=6) ``` -------------------------------- ### Compute Matrix Profile with Ten Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with ten data points. Window sizes of 1 through 9 are possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0]) # Compute the matrix profile with window size 9 matrix_profile = stumpy.stump(data, m=9) ``` -------------------------------- ### Install Dependencies from Source with Conda Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/install.md Install required dependencies using conda and then install Stumpy from source. ```bash conda install -c conda-forge -y --file requirements.txt python -m pip install . ``` -------------------------------- ### Compute Matrix Profile with Eight Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with eight data points. Window sizes of 1 through 7 are possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]) # Compute the matrix profile with window size 7 matrix_profile = stumpy.stump(data, m=7) ``` -------------------------------- ### Compute Matrix Profile with Nine Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with nine data points. Window sizes of 1 through 8 are possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0]) # Compute the matrix profile with window size 8 matrix_profile = stumpy.stump(data, m=8) ``` -------------------------------- ### Compute Matrix Profile with Different Data, Normalization, and Threshold Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md A comprehensive example with a different dataset, normalization enabled, and a specific threshold. ```python import stumpy import numpy as np data = np.asarray([ 10.0, 8.0, 6.0, 4.0, 2.0, 1.0, 3.0, 5.0, 7.0, 9.0 ]) # Compute the matrix profile with window size 4, normalization, and threshold 0.4 matrix_profile = stumpy.stump(data, m=4, normalize=True, threshold=0.4) ``` -------------------------------- ### STUMPY: Using `stumped` with `gpu_id` and `distances` Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md This example demonstrates using `stumped` with GPU acceleration and a pre-computed distance matrix. ```python import stumpy import numpy as np time_series = np.random.rand(1000) window_size = 64 # Assume 'dist_matrix' is pre-computed dist_matrix = np.random.rand(len(time_series) - window_size + 1, len(time_series) - window_size + 1) # Compute Matrix Profile using stumped with GPU and distances matrix_profile_dist_gpu = stumpy.stumped(time_series, m=window_size, gpu_id=0, distances=dist_matrix) print(f"Matrix Profile computed with GPU and distances. Shape: {matrix_profile_dist_gpu.shape}") ``` -------------------------------- ### STUMPY Basic Usage Example Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md A minimal example demonstrating the core functionality of STUMPY to compute the Matrix Profile for a given time series. This is the entry point for most STUMPY operations. ```python import stumpy import numpy as np # Sample time series data data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) window_size = 3 # Compute the Matrix Profile mp = stumpy.stump(data, m=window_size) print(mp) ``` -------------------------------- ### STUMPY Example with Real-World Data Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md An example demonstrating the use of STUMPY with a real-world dataset. This snippet shows how to apply STUMPY functions to practical time series data. ```python import stumpy import numpy as np # Assume 'real_world_data.csv' contains your time series t = np.loadtxt('real_world_data.csv', delimiter=',') # Calculate Matrix Profile for the real-world data mp = stumpy.stump(t, m=100) ``` -------------------------------- ### Compute Matrix Profile with Four Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with four data points. Window sizes of 1, 2, or 3 are possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0, 8.0]) # Compute the matrix profile with window size 3 matrix_profile = stumpy.stump(data, m=3) ``` -------------------------------- ### Compute Matrix Profile with Three Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with three data points. A window size of 1 or 2 is possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0]) # Compute the matrix profile with window size 2 matrix_profile = stumpy.stump(data, m=2) ``` -------------------------------- ### Stumpy Motif Discovery Example Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Consensus_Motif.md Demonstrates finding a common motif within multiple time series using Stumpy. This example assumes data is loaded and preprocessed. ```python import stumpy import numpy as np # Assume 'data' is a NumPy array where each row is a time series # Example: data = np.random.rand(6, 1000) m = 100 # Motif length # Calculate the Matrix Profile mp = stumpy.stump(data, m) # Find the index of the minimum value in the Matrix Profile min_idx = np.argmin(mp[:, 0]) # The motif is the subsequence starting at min_idx in the first time series motif_start_idx = int(mp[min_idx, 0]) motif = data[0, motif_start_idx : motif_start_idx + m] print(f"Motif found starting at index {motif_start_idx} in the first time series.") # You can then plot this motif or compare it across other series. ``` -------------------------------- ### Example Matrix Profile Array Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example demonstrates the structure of a matrix profile array, where each row represents a subsequence and the columns contain the distance to the nearest neighbor, the index of the nearest neighbor, the index of the left nearest neighbor, and the index of the right nearest neighbor. ```python array([[1.626257115121311, 202, -1, 202], [1.7138456780667977, 65, 0, 65], [1.880293454724256, 66, 0, 66], [1.796922109741226, 67, 0, 67], [1.4943082939628236, 11, 1, 11], [1.4504278114808016, 12, 2, 12], [1.6294354134867932, 19, 0, 19], [1.5349365731102185, 229, 0, 229], [1.3930265554289831, 186, 1, 186], [1.5265881687159586, 187, 2, 187], [1.8022253384245739, 33, 3, 33], [1.4943082939628236, 4, 4, 118], [1.4504278114808016, 5, 5, 137], [1.680920620705546, 201, 6, 201], [1.5625058007723722, 237, 8, 237], [1.2860008417613522, 66, 9, -1]]) ``` -------------------------------- ### Install Flake8 Linter Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/pull_request_template.md Install the Flake8 code linter using pip or conda. Flake8 helps identify programming errors and style guide violations. ```bash python -m pip install flake8 ``` ```bash conda install -c conda-forge flake8 ``` -------------------------------- ### Get Stumpy Version Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md Retrieves the currently installed version of the Stumpy library. ```python import stumpy version = stumpy.__version__ print(f'Stumpy version: {version}') ``` -------------------------------- ### Get STUMPY Version Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md A simple snippet to retrieve the installed version of the STUMPY library. ```python import stumpy # Get the STUMPY version version = stumpy.__version__ print(f"STUMPY version: {version}") ``` -------------------------------- ### Compute Matrix Profile with All Parameters Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md A comprehensive example showing the use of all common parameters for matrix profile computation: window size, normalization, and threshold. ```python import stumpy import numpy as np data = np.asarray([ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ]) # Compute the matrix profile with window size 4, normalization, and threshold 0.3 matrix_profile = stumpy.stump(data, m=4, normalize=True, threshold=0.3) ``` -------------------------------- ### Get Subsequence from Time Series Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Pattern_Matching.md Extracts a specific subsequence from the original time series given a starting index and window size. ```python import stumpy import numpy as np # Example time series time_series = np.array([1, 2, 3, 4, 5, 4, 3, 2, 1]) # Define window size and start index m = 3 start_index = 2 # Get the subsequence subsequence = stumpy.get_subsequence(time_series, m=m, index=start_index) print(subsequence) ``` -------------------------------- ### Setup and Run STUMPY Unit Tests Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Contribute.ipynb A combined command to set up the development environment and then run the unit tests. Ensures a clean state before testing. ```bash ./setup.sh dev && ./test.sh ``` -------------------------------- ### Get Stumpy Version Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Annotation_Vectors.md This utility function returns the currently installed version of the Stumpy library. It's useful for checking compatibility and reporting issues. ```python import stumpy # Get the Stumpy version version = stumpy.__version__ print(f"Stumpy version: {version}") ``` -------------------------------- ### STUMPY Configuration Options Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md Demonstrates how to use different configuration options for the STUMPY algorithm. ```python import stumpy import numpy as np # Example time series time_series = np.random.rand(200) # Calculate Matrix Profile with custom parameters # 'normalize' can be 'zscore' or 'minmax' # 'threshold' can be used to prune the search space matrix_profile = stumpy.stump(time_series, m=20, normalize='zscore', threshold=0.1) print(matrix_profile) ``` -------------------------------- ### Compute STUMPY with GPU Acceleration Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example shows how to leverage GPU acceleration for computing the STUMPY matrix profile. Ensure you have a compatible GPU and the necessary libraries installed. ```python import stumpy import numpy as np # Load your time series data time_series = np.random.rand(10000) # Define the window size m = 100 # Compute the matrix profile using GPU # The 'device' parameter can be set to 'cuda' or a specific device index matrix_profile_gpu = stumpy.stump(time_series, m=m, device='cuda') print(f"Matrix Profile computed on GPU with shape: {matrix_profile_gpu.shape}") ``` -------------------------------- ### Load Data and Initialize Parameters Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/WIP/Tutorial_DiscordMERLIN.ipynb Loads toy data from a .mat file and initializes parameters for finding discords. Ensure the 'MERLIN_datasets\NoisySine.mat' file is accessible. ```python data = loadmat("MERLIN_datasets\NoisySine.mat") #toy data T = data['T'].reshape(-1,) min_m = 500 max_m = 505 k=2 ``` -------------------------------- ### Compute Matrix Profile with Five Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with five data points. Window sizes of 1, 2, 3, or 4 are possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0, 8.0, 9.0]) # Compute the matrix profile with window size 4 matrix_profile = stumpy.stump(data, m=4) ``` -------------------------------- ### Extracting Subsequences with STUMPY Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example shows how to extract a specific subsequence from a time series given its starting index and the window size. This is useful for examining patterns identified by the Matrix Profile. ```python import stumpy import numpy as np # Assume X is your time series data and m is the window size np.random.seed(42) X = np.random.rand(1000) m = 50 # Extract a subsequence starting at index 100 subsequence = stumpy.extract_subsequence(X, index=100, m=m) print(subsequence) ``` -------------------------------- ### Compute STUMPY Matrix Profile and Index Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example computes both the matrix profile and its corresponding index array. The index array stores the starting index of the nearest neighbor subsequence for each subsequence in the time series. ```python import stumpy import numpy as np # Load your time series data time_series = np.random.rand(1500) # Calculate the matrix profile and the matrix profile index matrix_profile, matrix_profile_index = stumpy.stump(time_series, m=75, return_index=True) print("Matrix Profile:", matrix_profile) print("Matrix Profile Index:", matrix_profile_index) ``` -------------------------------- ### STUMPY with Different Window Sizes Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example shows how the Matrix Profile changes when using different window sizes (m). The choice of 'm' is crucial and depends on the expected length of patterns. ```python import stumpy import numpy as np np.random.seed(42) X = np.random.rand(200) # Matrix Profile with window size 10 mp_10 = stumpy.stump(X, m=10) # Matrix Profile with window size 20 mp_20 = stumpy.stump(X, m=20) print("Matrix Profile (m=10) shape:", mp_10.shape) print("Matrix Profile (m=20) shape:", mp_20.shape) ``` -------------------------------- ### Get Chunk Ranges for Ascending Arrays Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/WIP/Tutorial_DiscordMERLIN.ipynb This Python function calculates the inclusive start and exclusive stop indices for continuous segments within an ascending integer array. It can optionally shift the stop index. ```python def _get_chunks_ranges(a, shift=None): """ This function takes an array that contains only integer numbers in ascending order, and return the `(inclusive) start index` and `(exclusive) stop index + shift` for each continuous segment of array. Parameters -------- a : numpy.ndarray 1-dim array that contains integer numbers in ascending order. shift : int, default None an integer number by which the stop index of each segement should be shifted. If None, no shift will be applied. Returns ------- out : numpy.ndarray a 2-dim numpy array. The first column is the (inclusive) start index of each segment. The second column is the (exclusive) stop index shifted by `shift` units. """ repeats = np.full(len(a), 2) diff_is_one = np.diff(a) == 1 repeats[1:] -= diff_is_one repeats[:-1] -= diff_is_one out = np.repeat(a, repeats).reshape(-1, 2) out[:, 1] += 1 if shift is not None: out[:, 1] += shift return out ``` -------------------------------- ### Compute STUMPY's Matrix Profile with GPU Acceleration Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md This example demonstrates how to leverage GPU acceleration for computing the Matrix Profile using STUMPY. Ensure you have a compatible GPU and the necessary CUDA drivers installed. ```python import stumpy import numpy as np time_series = np.random.rand(1000) # Compute the Matrix Profile on the GPU # 'gpu_id' specifies the GPU to use (e.g., 0 for the first GPU) m_gpu = stumpy.stump(time_series, m=128, gpu_id=0) print(m_gpu) ``` -------------------------------- ### Compute Matrix Profile with Six Data Points Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example with six data points. Window sizes of 1, 2, 3, 4, or 5 are possible. ```python import stumpy import numpy as np data = np.array([5.0, 6.0, 7.0, 8.0, 9.0, 10.0]) # Compute the matrix profile with window size 5 matrix_profile = stumpy.stump(data, m=5) ``` -------------------------------- ### Compute STUMPY Matrix Profile with GPU Acceleration Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example shows how to leverage GPU acceleration for computing the STUMPY matrix profile, which can significantly speed up calculations for large datasets. Ensure you have a compatible GPU and the necessary CUDA drivers installed. ```python import stumpy import numpy as np # Load your time series data time_series = np.random.rand(10000) # Calculate the matrix profile using GPU # Set device='cuda:0' to use the GPU matrix_profile = stumpy.stump(time_series, m=100, device='cuda:0') print(matrix_profile) ``` -------------------------------- ### Compute STUMPY Matrix Profile with Multiple GPUs Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example shows how to utilize multiple GPUs for computing the STUMPY matrix profile, further accelerating the process for extremely large time series. This requires multiple compatible GPUs and appropriate CUDA setup. ```python import stumpy import numpy as np # Load your time series data time_series = np.random.rand(50000) # Calculate the matrix profile using multiple GPUs # Set device='cuda:0-1' to use the first two GPUs matrix_profile = stumpy.stump(time_series, m=200, device='cuda:0-1') print(matrix_profile) ``` -------------------------------- ### Install STUMPY with Pip Source: https://github.com/stumpy-dev/stumpy/blob/main/README.rst Install STUMPY using pip. This command installs the package directly from PyPI. ```bash python -m pip install stumpy ``` -------------------------------- ### Compute Matrix Profile with Different Data, Small Window, Normalization, and Threshold Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md A comprehensive example with a different dataset, small window size (m=2), normalization, and a specific threshold. ```python import stumpy import numpy as np data = np.asarray([ 5.0, 4.0, 3.0, 2.0, 1.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]) # Compute the matrix profile with window size 2, normalization, and threshold 0.3 matrix_profile = stumpy.stump(data, m=2, normalize=True, threshold=0.3) ``` -------------------------------- ### Install STUMPY with Conda Source: https://github.com/stumpy-dev/stumpy/blob/main/README.rst Install STUMPY using Conda from the conda-forge channel. This is a recommended installation method. ```bash conda install -c conda-forge stumpy ``` -------------------------------- ### Compute STUMPY's Matrix Profile with Different Window Sizes Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md This example shows how to compute the Matrix Profile with different window sizes (`m`). Varying the window size can reveal patterns at different scales. ```python import stumpy import numpy as np time_series = np.random.rand(1000) # Compute Matrix Profile with m=64 m_64 = stumpy.stump(time_series, m=64) # Compute Matrix Profile with m=256 m_256 = stumpy.stump(time_series, m=256) print("Matrix Profile (m=64) shape:", m_64.shape) print("Matrix Profile (m=256) shape:", m_256.shape) ``` -------------------------------- ### Compute Matrix Profile with Small Dataset Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example using a small dataset (less than twice the window size). Ensure your data is sufficiently large for meaningful results. ```python import stumpy import numpy as np data = np.asarray([1.0, 2.0, 3.0, 4.0, 5.0]) # Compute the matrix profile with window size 3 matrix_profile = stumpy.stump(data, m=3) ``` -------------------------------- ### STUMPY: Parallel Matrix Profile Computation Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md This example demonstrates how to utilize multiple CPU cores for computing the Matrix Profile, which can significantly speed up the process on multi-core systems. ```python import stumpy import numpy as np time_series = np.random.rand(1000) window_size = 64 # Compute the Matrix Profile using multiple threads (cores) # 'num_threads' specifies the number of threads to use matrix_profile_parallel = stumpy.stump(time_series, m=window_size, num_threads=4) # Use 4 threads print(f"Matrix Profile computed in parallel using 4 threads.") ``` -------------------------------- ### Install Pytest Coverage Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/pull_request_template.md Install the pytest-cov plugin using pip or conda. This tool is used for measuring code coverage during testing. ```bash python -m pip install pytest-cov ``` ```bash conda install -c conda-forge pytest-cov ``` -------------------------------- ### Install STUMPY with UV Source: https://github.com/stumpy-dev/stumpy/blob/main/README.rst Add STUMPY to your project dependencies using UV. UV is a fast Python package installer and resolver. ```bash uv add stumpy ``` -------------------------------- ### Compute Matrix Profile with Small Window Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example using a very small window size (m=2). This can be useful for detecting very short patterns or local variations. ```python import stumpy import numpy as np data = np.asarray([ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ]) # Compute the matrix profile with window size 2 matrix_profile = stumpy.stump(data, m=2) ``` -------------------------------- ### Install Black Formatter Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/pull_request_template.md Install the Black code formatter using pip or conda. This tool is used to automatically format Python code. ```bash python -m pip install black ``` ```bash conda install -c conda-forge black ``` -------------------------------- ### Reproduce Matlab Example - Print Parameters Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/REF/FLOSS_IAC_Derivation.ipynb Prints the calculated alpha and beta parameters obtained from fitting the beta distribution in the Matlab example reproduction. ```python print(f"alpha = {a}, beta = {b}") ``` -------------------------------- ### Compute Matrix Profile with Different Window Sizes Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Pattern_Matching.md Demonstrates computing the Matrix Profile for multiple window sizes. This allows for analysis at different temporal scales. ```python import stumpy import numpy as np t = np.random.rand(1000) stumpy.initialize() stumpy.load_data(t) # Compute Matrix Profile for m=30 mp_30 = stumpy.matrix_profile(m=30) # Compute Matrix Profile for m=60 mp_60 = stumpy.matrix_profile(m=60) ``` -------------------------------- ### Plotting Guided Motif Discovery Results Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Annotation_Vectors.md Visualizes the results of a guided motif search, highlighting the identified motifs on both the original data and the matrix profile. Requires matplotlib and numpy. ```python motif_idx = np.argmin(corrected_mp) nn_idx = mp[motif_idx, 1] n fig, axs = plt.subplots(2, sharex=True, gridspec_kw={'hspace': 0}) plt.suptitle('Motif (Pattern) Discovery', fontsize='30') axs[0].plot(df['Electric Potential']) axs[0].set_ylabel('Voltage', fontsize='20') rect = Rectangle((motif_idx, -150), m, 300, facecolor='lightgrey') axs[0].add_patch(rect) rect = Rectangle((nn_idx, -150), m, 300, facecolor='lightgrey') axs[0].add_patch(rect) axs[1].set_xlabel('Time', fontsize ='20') axs[1].set_ylabel('Matrix Profile', fontsize='20') axs[1].axvline(x=motif_idx, linestyle="dashed") axs[1].axvline(x=nn_idx, linestyle="dashed") axs[1].plot(corrected_mp) plt.show() plt.show() ``` -------------------------------- ### Example Usage: Finding Discords Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/WIP/Tutorial_DiscordMERLIN.ipynb Demonstrates how to use the approximate matrix profile and refined candidates functions to find discords in a time series. Prints the indices of discords, their distances to nearest neighbors, and the indices of those nearest neighbors. ```python s = int(0.001 * T.shape[0]) approx_P = _get_approx_P(T, m, M_T, Σ_T, s) discords_idx, discords_dist, discords_nn_idx = _refine_candidates(T, m, M_T, Σ_T, is_cands) print('the index of discord is: ', discords_idx) print('distance of discord to its NN is: ', discords_dist) print('the index of NearestNeighbor of the discord is: ', discords_nn_idx) ``` ```text the index of discord is: [718, 906] distance of discord to its NN is: [10.301397123538967, 10.271812911147354] the index of NearestNeighbor of the discord is: [278, 1283] ``` -------------------------------- ### Reproduce Matlab Example - Data Preparation Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/REF/FLOSS_IAC_Derivation.ipynb Prepares data for reproducing Michael Yeh's Matlab example for the 1-dimensional arc curve. This involves creating arrays based on the AC_1D values. ```python x = [None] * k for i in range(k): x[i] = np.ones(AC_1D[i], dtype=np.int64) * i x = np.concatenate(x) ``` -------------------------------- ### Verify Naive and MASS Output Equivalence (Python) Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Pattern_Matching.ipynb Asserts that the distance profiles computed by the naive method and the MASS algorithm are almost equal. This confirms the correctness of the MASS implementation. ```python npt.assert_almost_equal(naive_distance_profile, mass_distance_profile) ``` -------------------------------- ### Reproduce Matlab Example - Generate and Count Samples Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/REF/FLOSS_IAC_Derivation.ipynb Generates random samples from the fitted beta distribution and counts their occurrences to create a new distribution (AC_1D_Yeh). This is part of reproducing the Matlab example and can take time. ```python # This takes a while y = scipy.stats.beta.rvs(a, b, size=x.shape[0]) y = np.round(y * k).astype(np.int64) AC_1D_Yeh = np.zeros(k, dtype=np.int64) for i in range(k): AC_1D_Yeh[i] = np.sum(y == i) ``` -------------------------------- ### Example Usage of Stumpy Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md Demonstrates a basic usage pattern of the Stumpy library, likely for time series analysis or anomaly detection. Ensure necessary imports are present. ```python import stumpy import numpy as np # Example data matrix = np.random.rand(100, 5) # Compute the matrix profile matrix_profile = stumpy.stump(matrix, m=10) print(matrix_profile) ``` -------------------------------- ### Stumpy Matrix Profile Computation with Threshold and All Indices Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Annotation_Vectors.md This example computes the matrix profile and all corresponding indices using `stumpy.stump` with a threshold. This provides a comprehensive view of nearest neighbors while considering the threshold. ```python import stumpy import numpy as np # Example time series data x = np.random.rand(1000) # Compute the matrix profile and all indices with a threshold mp, mp_idx = stumpy.stump(x, m=50, include_idx=True, threshold=0.1) print(mp_idx) ``` -------------------------------- ### Initialize STIMP and Compute Initial Pan Matrix Profile Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/WIP/Tutorial_Pan_Matrix_Profile.ipynb Initialize the STIMP object with a time series and a range of window sizes. The `percentage` parameter controls the initial number of window sizes to compute the matrix profile for. Use the `update()` method to compute additional matrix profiles. ```python min_m, max_m = 100, 1000 eog = stumpy.stimp(eog_df["EOG"].values, min_m=min_m, max_m=max_m, percentage=0.01) # This percentage controls the extent of `stumpy.scrump` completion percent_m = 0.01 # The percentage of windows to compute n = np.ceil((max_m - min_m) * percent_m).astype(int) for _ in range(n): eog.update() ``` -------------------------------- ### Example Matrix Profile Array Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.ipynb This example matrix profile array demonstrates the four columns: nearest neighbor distance, nearest neighbor index, left nearest neighbor index, and right nearest neighbor index. Negative indices signify that a neighbor was not found. ```python array([[1.626257115121311, 202, -1, 202], [1.7138456780667977, 65, 0, 65], [1.880293454724256, 66, 0, 66], [1.796922109741226, 67, 0, 67], [1.4943082939628236, 11, 1, 11], [1.4504278114808016, 12, 2, 12], [1.6294354134867932, 19, 0, 19], [1.5349365731102185, 229, 0, 229], [1.3930265554289831, 186, 1, 186], [1.5265881687159586, 187, 2, 187], [1.8022253384245739, 33, 3, 33], [1.4943082939628236, 4, 4, 118], [1.4504278114808016, 5, 5, 137], [1.680920620705546, 201, 6, 201], [1.5625058007723722, 237, 8, 237], [1.2860008417613522, 66, 9, -1]] ``` -------------------------------- ### Plot Discovered Motifs Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/WIP/Tutorial_meter_swapping.ipynb This code plots the discovered motifs. It uses `matplotlib.pyplot` to create a figure with two subplots. The first subplot shows the time series segment from 'H_min_i' starting at `idx`, and the second subplot shows the corresponding segment from 'T_min_j' starting at `match_idx`. Ensure `matplotlib.pyplot` and `numpy` are imported. ```python fig, axs = plt.subplots(2, sharex=False, gridspec_kw={"hspace": 0}) plt.suptitle("Houses Consumptions", fontsize="30") axs[0].plot(heads[f'H_{min_i}'].iloc[idx:idx+m, 1], color = 'green') axs[1].plot(tails[f'T_{min_j}'].iloc[match_idx:match_idx+m, 1], color = 'red') ``` -------------------------------- ### Initialize Stumpy Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Pattern_Matching.md Initializes the Stumpy library. This is a foundational step before using other Stumpy functionalities. ```python import stumpy stumpy.initialize() ``` -------------------------------- ### Initialize Stumpy with Custom Parameters Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Shows how to initialize the Stumpy object with custom parameters for fine-grained control over its behavior. Adjust parameters like `threshold`, `threshold_type`, and `threshold_value` as needed. ```python import stumpy # Initialize Stumpy with custom parameters stumpy_obj = stumpy.Stumpy(threshold=0.1, threshold_type='value', threshold_value=0.05) ``` -------------------------------- ### Get Threshold Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Pattern_Matching.md Retrieves the current threshold (T) used by Stumpy. This parameter influences the computation of profiles. ```python import stumpy import numpy as np t = np.random.rand(1000) stumpy.initialize() stumpy.load_data(t) # Get the threshold T = stumpy.get_threshold() ``` -------------------------------- ### Get a new data point Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Matrix_Profiles_For_Streaming_Data.ipynb Simulate the arrival of a new data point for the time series. ```python t = np.random.rand() ``` -------------------------------- ### Compute STUMPY with Different Window Sizes Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.md This example shows how to compute the matrix profile for a time series using multiple window sizes. This can help in identifying patterns at different scales. ```python import stumpy import numpy as np # Load your time series data time_series = np.random.rand(2000) # Define multiple window sizes m_values = [50, 100, 150] # Compute matrix profiles for each window size all_matrix_profiles = {} for m in m_values: mp, mpi = stumpy.stump(time_series, m=m) all_matrix_profiles[m] = (mp, mpi) print(f"Computed matrix profiles for window sizes: {list(all_matrix_profiles.keys())}") ``` -------------------------------- ### Initialize Stump Matrix Calculation Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Fast_Approximate_Matrix_Profiles.md Initializes the Stump matrix calculation with a specified window size. ```python m = 640n ``` -------------------------------- ### Stumpy Cleanup Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Semantic_Segmentation.md Example of cleaning up resources used by Stumpy. This is important for releasing memory and other system resources. ```python import stumpy stumpy.cleanup() ``` -------------------------------- ### Get Nearest Neighbor Distance to Discord Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_STUMPY_Basics.ipynb Retrieves the distance of the nearest neighbor subsequence to the identified discord. ```python nearest_neighbor_distance = mp[discord_idx, 0] print(f"The nearest neighbor subsequence to this discord is {nearest_neighbor_distance} units away") ``` -------------------------------- ### Compute STUMPY's Matrix Profile with Custom Parameters Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Shapelet_Discovery.md This example shows how to compute the Matrix Profile with custom parameters, such as `ignore_self` to exclude self-matches and `normalize` for z-normalization. These parameters allow for fine-tuning the analysis. ```python import stumpy import numpy as np time_series = np.random.rand(1000) # Compute the Matrix Profile with custom parameters m_custom = stumpy.stump(time_series, m=128, ignore_self=True, normalize=True) print(m_custom) ``` -------------------------------- ### Install STUMPY with Pixi Source: https://github.com/stumpy-dev/stumpy/blob/main/README.rst Add STUMPY to your project dependencies using Pixi. This is useful for managing project environments. ```bash pixi add stumpy ``` -------------------------------- ### Import necessary libraries Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Matrix_Profiles_For_Streaming_Data.ipynb Import numpy for numerical operations, stumpy for matrix profile computation, and numpy.testing for validation. ```python import numpy as np import stumpy import numpy.testing as npt import time ``` -------------------------------- ### Stumpy Matrix Profile Computation with GPU Acceleration and Quantiles Source: https://github.com/stumpy-dev/stumpy/blob/main/docs/Tutorial_Annotation_Vectors.md This example computes the matrix profile using `stumpy.stump` with GPU acceleration and specified quantiles. This allows for fast computation and analysis of the distance distribution on the GPU. ```python import stumpy import numpy as np # Example time series data x = np.random.rand(1000) # Compute the matrix profile with GPU acceleration and quantiles mp = stumpy.stump(x, m=50, quantiles=(0.1, 0.9), device='cuda') print(mp) ```