### Install Motiflets using pip Source: https://github.com/patrickzib/motiflets/blob/main/README.md Install the motiflets package directly using pip for quick setup. ```bash pip install motiflets ``` -------------------------------- ### Setup and Imports for Motiflets Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_physiodata_distance_measures.ipynb Initializes the environment by loading necessary libraries and configuring matplotlib for plotting. Ensures the Motiflets library is accessible. ```python %load_ext autoreload %autoreload 2 import os import sys sys.path.insert(0, "../") from motiflets.motiflets import * from motiflets.plotting import * import subprocess import matplotlib matplotlib.rcParams['pdf.fonttype'] = 42 matplotlib.rcParams['ps.fonttype'] = 42 %matplotlib inline %config InlineBackend.figure_formats = {'png', 'retina'} import warnings warnings.simplefilter("ignore") ``` -------------------------------- ### Setup and Plotting Function for Scalability Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/eval_scalability_old.ipynb Initializes environment, loads necessary libraries, and defines a function to plot scalability metrics (time, memory, extent) for different backends and datasets. This function normalizes extent and converts memory to GB for better visualization. ```python %load_ext autoreload %autoreload 2 import sys sys.path.insert(0, "../") import numpy as np import pandas as pd import seaborn as sns import matplotlib from matplotlib import pyplot as plt matplotlib.rcParams['pdf.fonttype'] = 42 matplotlib.rcParams['ps.fonttype'] = 42 %matplotlib inline %config InlineBackend.figure_formats = {'png', 'retina'} import warnings warnings.simplefilter("ignore") def plot_df(df, ds_name, l=125, k=20): df = df.sort_values(['length', 'backend'], ascending=[True, True]).reset_index() #drop=True df_normed = df.reset_index().groupby(["length"]).apply(lambda l: l["extent"] * 100 / (l["extent"].iloc[-1])).reset_index() # df_normed.set_index("length").head() df["extent_normed"] = df_normed["extent"] df["memory in MB"] = df["memory in MB"] / 1024 df.set_index("length", inplace=True, drop=True) df = df.sort_values(by="backend") mask = df.backend=="default" subset = df[mask] fig, axes = plt.subplots(1, 3, figsize=(20, 5)) sns.lineplot(x=df.index, hue=df.backend, y=df["time in s"], ax=axes[0]) axes[0].set_title("Time in s", fontsize=18) axes[0].tick_params(axis='x', labelrotation=-25) axes[0].set_xlabel("Length n") # axes[0].set_yscale('log') plt.yticks(fontsize=10) plt.ylabel("Time in s") sns.despine() sns.lineplot(x=df.index, hue=df.backend, y=df["memory in MB"], ax=axes[1]) axes[1].set_title("Memory", fontsize=18) axes[1].tick_params(axis='x', labelrotation=-25) axes[1].set_xlabel("Length n") axes[1].set_ylabel("Memory in GB") axes[1].set_yscale('log') plt.yticks(fontsize=10) sns.despine() df_filter = df[df['backend'].str.contains("pyattimo")] #.isin(["pyattimo (thres 0.1)", "pyattimo (thres 0.05)"])] # sns.barplot(x=df_filter.index, hue=df_filter.backend, y=df_filter["extent_normed"], ax=axes[2]) sns.lineplot(x=df_filter.index, hue=df_filter.backend, y=df_filter["extent_normed"], ax=axes[2]) axes[2].set_title("Extent as fraction of 'scalable'", fontsize=18) axes[2].tick_params(axis='x', labelrotation=-35) axes[2].set_xlabel("Length n") axes[2].set_ylabel("Extent (100% = default)") # axes[2].set_ylim(70, 200) axes[2].axhline(y=100, color='r', linestyle='--', label="scalable") plt.yticks(fontsize=10) sns.despine() plt.suptitle(f"Scalability for {ds_name} l={l}, k={k} using 64 cores", fontsize=20) plt.tight_layout() # plt.savefig(f"../tests/results/images/scalability_n_{ds_name}_{l}_{k}.pdf") plt.show() backends=["default", "scalable", "pyattimo (delta=0.25)", "pyattimo (delta=0.5)"] path = "../tests/results/scalability_experiments_old/" ``` -------------------------------- ### Run Set Finder Java Tool Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_synthetic.ipynb Executes the external 'set_finder.jar' Java program to discover motif sets using competitor algorithms. This requires Java to be installed and the JAR file to be accessible. ```python rs = np.array([r_top1*5]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) # mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/set_finder.jar', dataset, 'SetFinder', str(list(rs)), str(mls)]) ``` -------------------------------- ### Load and Initialize Libraries Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_winding.ipynb Loads necessary libraries and sets up the environment for analysis, including autoreload and matplotlib configurations. ```python %load_ext autoreload %autoreload 2 import os import sys sys.path.insert(0, "../") import motiflets.motiflets as ml from motiflets.competitors import * from motiflets.plotting import * import subprocess import matplotlib matplotlib.rcParams['pdf.fonttype'] = 42 matplotlib.rcParams['ps.fonttype'] = 42 %matplotlib inline %config InlineBackend.figure_formats = {'png', 'retina'} import warnings warnings.simplefilter("ignore") ``` -------------------------------- ### Get Flattened Motifs with Motiflets Source: https://context7.com/patrickzib/motiflets/llms.txt Retrieves a flattened representation of motif sets found by `fit_k_elbow`. Combines results across ranks into parallel arrays. Requires `motiflets`, `numpy`, `pandas`, and `scipy.stats`. ```python from motiflets.plotting import Motiflets import numpy as np import pandas as pd from scipy.stats import zscore data = pd.read_csv("datasets/ground_truth/vanilla_ice.csv", index_col=0).squeeze() data[:] = zscore(data) ml = Motiflets("Vanilla Ice", data) ml.fit_k_elbow(20, motif_length=360, top_N=3, plot_elbows=False, plot_motifs_as_grid=False) flat_dists, flat_candidates, flat_elbows = ml.get_flattened_motifs() # flat_dists[i] → extent of the i-th motif set # flat_candidates[i] → array of start positions for the i-th motif set # flat_elbows[i] → index into the flat arrays (0, 1, 2, ...) for i in flat_elbows: print(f"Motif Set {i+1}: k={len(flat_candidates[i])}, extent={flat_dists[i]:.4f}") ``` -------------------------------- ### Load and Initialize Motiflets Environment Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_case_multivariate.ipynb Sets up the environment by loading necessary libraries, configuring matplotlib, and ignoring warnings. This is boilerplate for running the motiflets analysis. ```python %load_ext autoreload %autoreload 2 import sys sys.path.insert(0, "../") from motiflets.motiflets import * from motiflets.plotting import * import matplotlib matplotlib.rcParams['pdf.fonttype'] = 42 matplotlib.rcParams['ps.fonttype'] = 42 %matplotlib inline %config InlineBackend.figure_formats = {'png', 'retina'} import warnings warnings.simplefilter("ignore") ``` -------------------------------- ### Compute Precision for Motif Detection Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_synthetic-all.ipynb Calculates the precision of predicted motif occurrences against ground truth. Assumes predictions and ground truth are start indices and uses a motif length 'm' for overlap checking. ```python def compute_precision(pred, gt, m): gt_found = np.zeros(len(gt)) for start in pred: for i, g_start in enumerate(gt): if (start <= g_start and start + m >= g_start) \ or (start <= g_start+m and start >= g_start): gt_found[i] = 1 break return np.average(gt_found) ``` -------------------------------- ### Initialize Motif Discovery Parameters Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_synthetic.ipynb Sets up initial parameters for motif discovery, including the maximum number of motifs to consider (ks) and the length of the motifs. This prepares for the elbow method analysis. ```python df = pd.DataFrame() # TOP-1 motifs rerun_all_jars = False ks=12 motif_length = 500 dataset = os.getcwd() + '/../datasets/' + file ``` -------------------------------- ### Initialize Motif Discovery Parameters Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_vanilla_ice.ipynb Sets up initial parameters for motif discovery, including the maximum number of motifs (ks), motif length, slack, and the dataset path. These parameters influence the motif search. ```python df = pd.DataFrame() # TOP-1 motifs rerun_all_jars = False ks=22 motif_length = 180 slack=0.6 dataset = os.getcwd() + '/../datasets/' + file ``` -------------------------------- ### Initialize Motif Discovery Parameters Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_muscle_activation.ipynb Sets up parameters for motif discovery, including the dataset path, series length, and the number of nearest neighbors (ks) to consider. This is a prerequisite for motif length and size selection. ```python df = pd.DataFrame() # TOP-1 df2 = pd.DataFrame() # TOP-2 rerun_all_jars = False ks = 15 dataset = os.getcwd() + '/../datasets/' + file print(dataset, len(series)) ``` -------------------------------- ### Initialize DataFrames and Parameters Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_winding.ipynb Initializes empty DataFrames for storing results and sets parameters for motif discovery, including the dataset path. ```python df = pd.DataFrame() # TOP-1 df2 = pd.DataFrame() # TOP-2 rerun_all_jars = False ks=12 dataset = os.getcwd() + '/../datasets/' + file print(dataset) ``` -------------------------------- ### Read Dataset and Initialize Motiflets Module Source: https://github.com/patrickzib/motiflets/blob/main/README.md Load a dataset and import necessary modules from the motiflets library to begin motif discovery analysis. Ensure the 'file' variable is defined with the path to your dataset. ```python from motiflets.motiflets import * from motiflets.plotting import * # the Motiflets module is located here series, df_gt = read_dataset_with_index(file) ``` -------------------------------- ### Initialize DataFrames and Parameters for Motif Discovery Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_fnirs.ipynb Sets up empty DataFrames for storing results and defines parameters like the maximum number of motifs (ks) and the dataset path. It also includes a flag to rerun Java processes. ```python df = pd.DataFrame() # TOP-1 df2 = pd.DataFrame() # TOP-2 rerun_all_jars = False ks = 20 dataset = os.getcwd() + '/../datasets/' + file print(dataset) ``` -------------------------------- ### Fit k-motiflets from CSV file Source: https://context7.com/patrickzib/motiflets/llms.txt Use the CLI to fit k-motiflets from a local CSV file. Specify the maximum k to search and the motif length. The output includes elbow points and motif set details. ```bash uvx motiflets fit_k data.csv --k-max 20 --motif-length 100 --top-n 3 ``` -------------------------------- ### Run Learning Motifs Java Jar Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_winding.ipynb Invokes the Learning Motifs Java JAR for motif discovery. Requires dataset, similarity thresholds (offset by 1), and motif lengths. ```python rs = np.array([r_top2*2, r_top1*2.2]) mls = motif_length if rerun_all_jars: output = subprocess.call(['java', '-jar', '../jars/latent_motifs.jar', dataset, str(list(rs+1)), str(mls)]) ``` -------------------------------- ### Compare multiple motif discovery methods Source: https://context7.com/patrickzib/motiflets/llms.txt Use `plot_all_competitors` to visualize motif sets from different discovery methods side-by-side. Provide data, motif sets for each method, method names, and motif length. The `slack` parameter controls the tolerance for motif alignment. ```python import numpy as np import pandas as pd from scipy.stats import zscore from motiflets.plotting import plot_all_competitors data = pd.read_csv("datasets/ground_truth/ecg-heartbeat-av.csv", index_col=0).squeeze() data[:] = zscore(data) # Motif sets from different methods (one array of positions per method) motif_sets = [ np.array([120, 245, 370, 495, 621, 747]), # k-Motiflet np.array([130, 250, 380, 510, 635, 760]), # Competitor A np.array([115, 240, 365, 492, 618, 744]), # Competitor B ] method_names = ["k-Motiflets", "EMMA", "SetFinder"] plot_all_competitors( data=data, ds_name="ECG Heartbeat", motifsets=motif_sets, motif_length=100, method_names=method_names, ground_truth=None, slack=0.5 ) ``` -------------------------------- ### Run Set Finder Java Jar Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_winding.ipynb Executes the Set Finder Java JAR to discover motif sets. Requires dataset name, motif lengths, and similarity thresholds as input. ```python rs = np.array([r_top2*3, r_top1*2]) mls = motif_length if rerun_all_jars: output = subprocess.call(['java', '-jar', '../jars/set_finder.jar', dataset, 'SetFinder', str(list(rs)), str(mls)]) ``` -------------------------------- ### Run Set-Finder Java Code Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_fnirs.ipynb This snippet prepares data for the Set-Finder algorithm and calls its Java implementation. Ensure Java code is run first. ```python ms_set_finder = [ [1570, 2767, 3988, 5083], [730, 304, 1139, 1301, 1891, 1998, 2114, 2634, 3206, 3352, 3513, 3646, 3814, 3894, 4358, 4598, 4872, 5061], ] motifset = plot_competitors(series, ds_name, ms_set_finder, motif_length, filter=False, prefix="Set-Finder") df["Set Finder Top-1"] = [motifset[-1]] df2["Set Finder Top-2"] = [motifset[-2]] ``` -------------------------------- ### Run Set Finder Java Code Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_ecg.ipynb Executes the Set Finder Java code with specified dataset, parameters, and motif length. Requires Java runtime and the set_finder.jar file. ```python rs = np.array([r_top2*10, r_top1*1.5]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/set_finder.jar', dataset, 'SetFinder', str(list(rs)), str(mls)]) ``` -------------------------------- ### Clone and Build Motiflets from Source Source: https://github.com/patrickzib/motiflets/blob/main/README.md Clone the Motiflets repository and build the package from source. This is useful for development or if you need the latest unreleased features. ```bash git clone https://github.com/patrickzib/motiflets.git cd motiflets pip install . ``` -------------------------------- ### Fit k-motiflets from URL with custom column Source: https://context7.com/patrickzib/motiflets/llms.txt Use the CLI to fit k-motiflets from a time series URL. Specify the maximum k, motif length, and the column to use for data. Parallel processing is enabled with --n-jobs. ```bash uvx motiflets fit_k https://example.com/timeseries.csv \ --k-max 15 --motif-length 200 --column 1 --n-jobs 4 ``` -------------------------------- ### Initialize Motiflets and Fit Motif Length Source: https://github.com/patrickzib/motiflets/blob/main/README.md Initializes the Motiflets class with time series data and parameters, then fits the optimal motif length. Ensure data, distance measure, ground truth (optional), and number of jobs are provided. ```python ml = Motiflets( ds_name, # the name of the series series, # the data distance, # Distance measure used, default: z-normed ED df_gt, # ground truth, if available n_jobs # number of jobs (cores) to be used. ) k_max = 20 # maxmimum number of repeats in each motif set top_N = 1 # number of motif sets to search length_range = np.arange(25,200,25) motif_length = ml.fit_motif_length(k_max, length_range) ``` -------------------------------- ### CLI: Search for k-Motiflets with motiflets fit_k Source: https://context7.com/patrickzib/motiflets/llms.txt Command-line tool to search for k-Motiflets at a fixed motif length and report elbow-point motif set positions. Requires specifying the motif length and k_max. ```bash # Detect motiflets with motif_length=128, k_max=6 uvx motiflets fit_k data.csv --k-max 6 --motif-length 128 # Output: # Loaded dataset 'data' with 3000 observations. # dataset: data # observations: 3000 # motif_length: 128 # elbow_points: 6 # k=6: distance=3.120000 motif_set=[120, 245, 370, 495, 621, 747] ``` -------------------------------- ### Prepare and Plot All Competitor Methods Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_physiodata-spindles.ipynb This code snippet concatenates dataframes, renames a column to 'offsets', and prepares a color palette and plot indices. It then plots all competitor methods using the 'plot_all_competitors' function, which is useful for comparing various motif discovery algorithms side-by-side. ```python df_all = (pd.concat([df, df2], axis=1)).T df_all.rename(columns={0:"offsets"}, inplace=True) index = np.array([0, 1, 2, 3, 4]) color_palette=np.array(sns.color_palette())[index] plot_index=[0, 1, 4, 7, 10, 13, 14, 17, 20, 23] motifsets = np.array(df_all["offsets"].values) plot_all_competitors(series, ds_name, motifsets, motif_length, color_palette=color_palette, method_names=df_all.index.values, plot_index=plot_index) ``` -------------------------------- ### Load and Configure Motiflets Environment Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_synthetic-all.ipynb Loads necessary libraries and configures matplotlib for plotting. Sets up autoreload for development. ```python %load_ext autoreload %autoreload 2 import sys import os sys.path.insert(0, "../") import motiflets.motiflets as ml from motiflets.competitors import * import motiflets.plotting as ml_plt import subprocess import matplotlib from matplotlib import pyplot as plt matplotlib.rcParams['pdf.fonttype'] = 42 matplotlib.rcParams['ps.fonttype'] = 42 %matplotlib inline %config InlineBackend.figure_formats = {'png', 'retina'} import warnings warnings.simplefilter("ignore") datasets = ["001_UCR_Anomaly_35000.txt", "002_UCR_Anomaly_35000.txt", "003_UCR_Anomaly_35000.txt", "004_UCR_Anomaly_2500.txt", "005_UCR_Anomaly_4000.txt", "006_UCR_Anomaly_4000.txt", "007_UCR_Anomaly_4000.txt", "008_UCR_Anomaly_4000.txt", "009_UCR_Anomaly_4000.txt", "010_UCR_Anomaly_4000.txt", "011_UCR_Anomaly_10000.txt", "012_UCR_Anomaly_15000.txt", "013_UCR_Anomaly_15000.txt", "014_UCR_Anomaly_8000.txt", "015_UCR_Anomaly_5000.txt", "016_UCR_Anomaly_5000.txt", "017_UCR_Anomaly_5000.txt", "018_UCR_Anomaly_8000.txt", "019_UCR_Anomaly_5000.txt", "020_UCR_Anomaly_5000.txt", "021_UCR_Anomaly_5000.txt", "022_UCR_Anomaly_4000.txt", "023_UCR_Anomaly_5000.txt", "024_UCR_Anomaly_3200.txt", "025_UCR_Anomaly_2800.txt", ] ml_plt.save_fig = False ``` -------------------------------- ### Process Set Finder Results Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_ecg.ipynb Processes and visualizes motif sets found by Set Finder. Assumes 'to_df' and 'plot_all_competitors' functions are defined. ```python # run jave code first ms_all = { "" : [ [671, 158, 286, 414, 542, 798], [1421, 1295, 1506, 1612, 1718, 1817, 1924, 2021, 2105, 2213, 2328, 2402, 2496, 2593, 2668, 2774], ], "-10%" :[ [406, 150, 278, 534, 662], [1410, 1284, 1495, 1601, 1707, 1807, 1913, 2010, 2094, 2202, 2317, 2391, 2486, 2583, 2657, 2763], ], "+10%" :[ [671, 158, 286, 414, 542, 798], [1207, 163, 291, 419, 547, 676, 803, 859, 1100, 1319, 1444, 1529, 1636, 1742, 1841, 1948, 2045, 2129, 2237, 2352, 2426, 2520, 2617, 2692, 2798], ] } motifsets = to_df(ms_all, "SF", df, df2) plot_all_competitors(series, ds_name, motifsets.offsets.values, motif_length, method_names=motifsets.index.values, ground_truth=df_gt) ``` -------------------------------- ### Load and Initialize Motiflets for Muscle Activation Data Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_paper.ipynb Loads the Muscle Activation dataset and initializes the Motiflets object for analysis, including visualizing the dataset. ```python file = 'muscle_activation.csv' ds_name = "Muscle Activation" series, df_gt = read_dataset_with_index(file) ml = Motiflets(ds_name, series, df_gt) fig, ax = ml.plot_dataset() ``` -------------------------------- ### Initialize Motif Set Variables Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_ecg.ipynb Initializes dataframes for storing top motifs and sets parameters for motif discovery. 'rerun_all_jars' controls re-computation, and 'ks' defines the maximum number of motifs to consider. ```python df = pd.DataFrame() # TOP-1 motifs df2 = pd.DataFrame() # TOP-2 motifs rerun_all_jars = False ks=20 dataset = os.getcwd() + '/../datasets/' + file ``` -------------------------------- ### Load and Plot Industrial Winding Process Data Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_paper.ipynb Loads the Industrial Winding Process dataset and visualizes it. Requires the 'motiflets' library and a CSV file. ```python file = "winding_col.csv" ds_name = "Industrial winding process" series = read_dataset_with_index(file) ml = Motiflets(ds_name, series) fig, ax = ml.plot_dataset() ``` -------------------------------- ### Run Set Finder Java Code with Parameter Variations Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_muscle_activation.ipynb Executes the Set Finder Java code with varying radii and motif lengths, simulating trial-and-error parameter tuning by introducing noise. This is used to compare Set Finder's motif discovery against k-Motiflets. ```python rs = np.array([r_top2 * 2.5, r_top1 * 1.145]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/set_finder.jar', dataset, 'SetFinder', str(list(rs)), str(mls)]) ``` -------------------------------- ### Run Learning Motifs Java Code Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_fnirs.ipynb This snippet prepares data for the Learning Motifs algorithm and calls its Java implementation. It requires the 'latent_motifs.jar' to be present. ```python rs = np.array([r_top2*1.2, r_top1*1.0]) mls = motif_length if rerun_all_jars: output = subprocess.call(['java', '-jar', '../jars/latent_motifs.jar', dataset, str(list(rs+1)), str(mls)]) ``` ```python ms_learning_motifs = np.array([ [239,3132,3758,4771,], [288,697,967,1276,1488,1893,1991,2108,2301,2512,2619,3181,3622,3798,4342,4565,4838,5054,], ]) motifset = plot_competitors(data, ds_name, ms_learning_motifs, motif_length, prefix="LM") df["LM Top-1"] = [motifset[-1]] df2["LM Top-2"] = [motifset[-2]] ``` -------------------------------- ### Run EMMA Java Code Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_fnirs.ipynb This snippet prepares data for the EMMA algorithm and calls its Java implementation. It requires the 'emma.jar' to be present. ```python rs = np.array([r_top2*2.5, r_top1*1.39]) mls = motif_length if rerun_all_jars: output = subprocess.call(['java', '-jar', '../jars/emma.jar', dataset, str(list(rs)), str(mls)]) ``` ```python # run jave code first ms_emma=[ [365, 2755, 3982, 5076], [228, 400, 653, 908, 1430, 2268, 2410, 2743, 2813, 3122, 3588, 3747, 4020, 4280, 4511, 4760, 4830, 6844] ] motifset = plot_competitors(series, ds_name, ms_emma, motif_length, prefix="EMMA") df["EMMA Top-1"] = [motifset[-1]] df2["EMMA Top-2"] = [motifset[-2]] ``` -------------------------------- ### Run Set Finder Java JAR Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_physiodata-spindles.ipynb Executes the Set Finder Java JAR to find motif sets. Adjusts parameters like motif range (rs) and length (mls) based on error values. Requires dataset, motif range, and motif length as input. ```python rs = np.array([r_top2, r_top1*0.94]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/set_finder.jar', dataset, 'SetFinder', str(list(rs)), str(mls)]) ``` -------------------------------- ### Run EMMA Java Jar Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_winding.ipynb Executes the EMMA Java JAR for motif set discovery. Takes dataset name, similarity thresholds, and motif lengths as parameters. ```python rs = np.array([r_top2*4, r_top1*6.5956]) mls = motif_length if rerun_all_jars: output = subprocess.call(['java', '-jar', '../jars/emma.jar', dataset, str(list(rs)), str(mls)]) ``` -------------------------------- ### Compare All Motif Discovery Methods Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_synthetic.ipynb This code prepares data and plots a comprehensive comparison of all motif discovery methods (SF, EMMA, VALMOD, LM) against ground truth. It uses custom color palettes and plot indices for clarity. ```python df_all = df.T df_all.rename(columns={0:"offsets"}, inplace=True) index = np.array([0, 1, 2, 3, 4]) color_palette=np.array(sns.color_palette())[index] plot_index=[0, 1, 4, 7, 10, 13, 14, 17, 20, 23] motifsets = np.array(df_all["offsets"].values) plot_all_competitors(series, ds_name, motifsets, motif_length, color_palette=color_palette, method_names=df_all.index.values, ground_truth=df_gt, plot_index=plot_index) ``` -------------------------------- ### Run Valmod Motif Sets Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_fnirs.ipynb This snippet calculates motif sets using the Valmod algorithm with a fixed-length implementation and plots the results. ```python mls = motif_length ms_valmod = list(get_valmod_motif_set_ranged(data, mls, max_r=r_top1*1.9)) motifset = plot_competitors(series, ds_name, ms_valmod, mls, prefix="Valmod") df["VALMOD Top-1"] = [motifset[-1]] df2["VALMOD Top-2"] = [motifset[0]] ``` -------------------------------- ### Learn Motif Set Size (k-Motiflets) Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_muscle_activation.ipynb Plots the similarity of motifs as a function of the cardinality of k-Motiflets to identify characteristic motifs. This helps in determining the size of the motif set, revealing two characteristic motifs: activations and recovery. ```python dists, motiflets, elbow_points = plot_elbow( ks, series, ds_name=ds_name, plot_elbows=True, motif_length=motif_length, method_name="k-Motiflets", ground_truth=df_gt) ``` -------------------------------- ### Load and Plot PAMAP Dataset Scalability Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/eval_scalability_old.ipynb Reads the PAMAP dataset, filters for relevant backends, and plots the scalability results. This is used to analyze performance on sensor data. ```python def read_pamap(): return "PAMAP", pd.read_csv(path + "scalability_n_PAMAP_200_10.csv", index_col=0) ds_name, df_new = read_pamap() df_new = df_new[df_new.backend.isin(backends)] plot_df(df_new, ds_name, l=200, k=10) ``` -------------------------------- ### CLI — `motiflets fit_k` Source: https://context7.com/patrickzib/motiflets/llms.txt Searches for k-Motiflets at a fixed motif length and reports the elbow-point motif set positions. ```APIDOC ## Command-Line Interface (CLI) — `motiflets fit_k` ### Description Searches for k-Motiflets at a fixed motif length and reports the elbow-point motif set positions. ### Usage ```bash # Detect motiflets with motif_length=128, k_max=6 uvx motiflets fit_k data.csv --k-max 6 --motif-length 128 # Output: # Loaded dataset 'data' with 3000 observations. # dataset: data # observations: 3000 # motif_length: 128 # elbow_points: 6 # k=6: distance=3.120000 motif_set=[120, 245, 370, 495, 621, 747] ``` ### Parameters - **data.csv**: Path or URL to the time series data file. - **--k-max** (int): The maximum number of motifs to consider. - **--motif-length** (int): The fixed motif length to use for detection. ``` -------------------------------- ### Run Set Finder Java Application Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_fnirs.ipynb Executes the SetFinder Java application to compare Motiflets with other motif set discovery methods. This requires the dataset path, method name, radii, and motif length as arguments. ```python rs = np.array([r_top2*1.6, r_top1*0.9]) mls = motif_length if rerun_all_jars: output = subprocess.call(['java', '-jar', '../jars/set_finder.jar', dataset, 'SetFinder', str(list(rs+1)), str(mls)]) ``` -------------------------------- ### Learn Top-N Motifs for Muscle Activation Data Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_paper.ipynb Identifies the top N motifs, specifically the recovery phase, in the Muscle Activation dataset by fitting the k-elbow method with a specified top_N parameter. ```python dists, candidates, elbow_points = ml.fit_k_elbow(k, motif_length, top_N=2) ``` -------------------------------- ### Run Valmod Motif Sets Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_physiodata-spindles.ipynb Implements a fixed-length motif discovery approach using Valmod. It calculates a maximum motif range (max_r) and iterates through error values to find motif sets, storing them in a dictionary. ```python mls = motif_length max_r=r_top1 *1.52 ms_all = {} for e in errors: rs = max_r * (1.0 * (1+e)) #mls = np.int32(motif_length * (1-e)) ms_valmod = list(get_valmod_motif_set_ranged(data, mls, max_r=rs)) ms_all[format_key(e)] = ms_valmod motifsets = to_df(ms_all, "VALMOD", df, df2) plot_all_competitors(series, ds_name, motifsets.offsets.values, motif_length, method_names=motifsets.index.values) ``` -------------------------------- ### Run VALMOD Motif Sets Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_ecg.ipynb Calculates motif sets using the VALMOD method with specified parameters and visualizes the results. Assumes helper functions like 'get_valmod_motif_set_ranged', 'format_key', 'to_df', and 'plot_all_competitors' are defined. ```python mls = motif_length max_r=r_top1 * 4.0 ms_all = {} for e in errors: rs = max_r * (1.0 * (1+e)) #mls = np.int32(motif_length * (1-e)) ms_valmod = list(get_valmod_motif_set_ranged(data, mls, max_r=rs)) ms_all[format_key(e)] = ms_valmod motifsets = to_df(ms_all, "VALMOD", df, df2) plot_all_competitors(series, ds_name, motifsets.offsets.values, motif_length, method_names=motifsets.index.values, ground_truth=df_gt) ``` -------------------------------- ### Load and Initialize Motiflets for ECG Data Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_paper.ipynb Reads the ECG heartbeats dataset and initializes the Motiflets object for analysis. This includes plotting the raw dataset. ```python file = 'ecg-heartbeat-av.csv' ds_name = "ECG Heartbeat" series, df_gt = read_dataset_with_index(file) ml = Motiflets(ds_name, series, df_gt) fig, ax = ml.plot_dataset() ``` -------------------------------- ### Load Muscle Activation Dataset Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_muscle_activation.ipynb Reads the 'muscle_activation.csv' dataset and plots it along with its ground truth. This step prepares the time series data for motif discovery. ```python file = 'muscle_activation.csv' ds_name = "Muscle Activation" series, df_gt = ml.read_dataset_with_index(file) data = series.values # series = ml.as_series(data, np.arange(0, 29899, 2) / 10000, 'Seconds') plot_dataset(ds_name, series, ground_truth=df_gt) ``` -------------------------------- ### Execute Learning Motifs (LM) Java Code Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_synthetic.ipynb This Python code prepares parameters and executes the Learning Motifs (LM) Java code using subprocess. It adjusts the motif set radius based on error values and calls the 'latent_motifs.jar' with dataset and parameter information. ```python rs = np.array([r_top1*15]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) # mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/latent_motifs.jar', dataset, str(list(rs)), str(mls)]) ``` -------------------------------- ### Run EMMA Java Code Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_ecg.ipynb Executes the EMMA Java code with specified dataset, parameters, and motif length. Requires Java runtime and the emma.jar file. ```python rs = np.array([r_top2*10, r_top1*2.92]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/emma.jar', dataset, str(list(rs)), str(mls)]) ``` -------------------------------- ### Find Motif Length for Penguin Hunting Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_case_multivariate.ipynb Initializes the Motiflets object and fits the optimal motif length for the penguin's hunting data. This step identifies potential lengths of recurring patterns during hunting. ```python length = 1_000 start = 3000 series = TS.iloc[497699 + start:497699 + start + length, [0, 1, 2]].T # Input Parameters k_max = 30 # expected number of repeats motif_length_range = np.arange(20, 30, 1) # motiflet length range # initialize ml = Motiflets( ds_name, series, n_jobs=8, # number of parallel jobs ) l = ml.fit_motif_length( k_max, motif_length_range, ) dists, motiflets, elbow_points = ml.fit_k_elbow( k_max, motif_length=l ) ``` -------------------------------- ### CLI: Fit k-Motiflet Locations Source: https://github.com/patrickzib/motiflets/blob/main/README.md Detects k-Motiflet locations from a time series dataset via the command line. This command is used after determining the motif length and focuses on finding the actual motif locations. ```bash uvx motiflets fit_k data.csv --k-max 6 --motif-length 128 --top-n 3 ``` -------------------------------- ### Load and Plot Semi-Synthetic Dataset Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_synthetic.ipynb Reads a semi-synthetic dataset from a CSV file and plots it, including ground truth labels if available. This step is crucial for visualizing the data before motif discovery. ```python ds_name = "Semi-Synthetic" file = 'synthetic.csv' series, df_gt = ml.read_dataset_with_index(file) plot_dataset(ds_name, series, ground_truth=df_gt) ``` -------------------------------- ### Load and Plot Dishwasher Dataset Scalability Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/eval_scalability_old.ipynb Loads the Dishwasher dataset, filters for the specified backends, and plots the scalability results. This function is specific to the Dishwasher dataset and its associated file. ```python def read_dishwasher(): return "Dishwasher", pd.read_csv(path + "scalability_n_Dishwasher_1000_20.csv", index_col=0) ds_name, df_new = read_dishwasher() df_new = df_new[df_new.backend.isin(backends)] plot_df(df_new, ds_name, l=1000, k=20) ``` -------------------------------- ### Plot All Competitor Motifs Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_winding.ipynb Generates a comprehensive plot comparing the motif sets found by all evaluated methods. Requires motif sets and method names as input. ```python motifsets = np.array(df_all["offsets"].values) plot_all_competitors(series, ds_name, motifsets, motif_length, method_names=df_all.index.values) ``` -------------------------------- ### Run EMMA Java Code with Parameter Variations Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_muscle_activation.ipynb Executes the EMMA Java code with adjusted radii and motif lengths, incorporating noise to simulate parameter tuning. This is part of the comparison with k-Motiflets and other methods. ```python rs = np.array([r_top2 * 2.8, r_top1 * 1.145]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/emma.jar', dataset, str(list(rs)), str(mls)]) ``` -------------------------------- ### Load and Plot Winding Dataset Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_winding.ipynb Reads the winding dataset from a CSV file and plots the original series. Ensure the 'winding_col.csv' file is accessible. ```python file = "winding_col.csv" ds_name = "Industrial winding process" series = ml.read_dataset_with_index(file) data = series.values #series = ml.as_series(data, np.arange(0, len(data), 1) / 10, 'Seconds') plot_dataset(ds_name, series) ``` -------------------------------- ### Set Finder (SF) Motif Set Generation Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_vanilla_ice.ipynb Generates motif sets using the Set Finder algorithm with different offset percentages. The results are converted to a DataFrame and plotted. ```python ms_all = { "" : [ [186, 391, 494, 701, 805, 2870, 3076, 3283, 6797, 7004, 7108, 7211, 7418, 9488, 9695, 9902, 10317, 10731, 10938], ], "-10%" : [ [7265, 446, 755, 2716, 2922, 3129, 3336, 6850, 7057, 7471, 9334, 9541, 9748, 9956], ], "+10%" : [ [176, 381, 484, 588, 692, 795, 2963, 3066, 3169, 3273, 3376, 6891, 6994, 7097, 7201, 7304, 7408, 9374, 9478, 9581, 9685, 9788, 9892, 10617, 10721], ] } motifsets = to_df(ms_all, "SF", df) display(motifsets) plot_all_competitors(series, ds_name, motifsets.offsets.values, motif_length, method_names=motifsets.index.values, ground_truth=df_gt) ``` -------------------------------- ### Load and Plot fNIRS Brain Imaging Data Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_paper.ipynb Loads the fNIRS brain imaging dataset and visualizes it. Requires the 'motiflets' library and a CSV file. ```python file = "fNIRS_subLen_600.csv" ds_name="fNIRS" series = read_dataset_with_index(file) ml = Motiflets(ds_name, series) fig, ax = ml.plot_dataset() ``` -------------------------------- ### Motiflets.fit_k_elbow: Discover k-Motiflets with Elbow Detection Source: https://context7.com/patrickzib/motiflets/llms.txt Employ `fit_k_elbow` to discover motif sets (k-Motiflets) for a given motif length. This method computes the extent function over k values, detects characteristic motif sizes using elbow points, and returns the positions of the discovered motifs. ```python import numpy as np import pandas as pd from scipy.stats import zscore from motiflets.plotting import Motiflets data = pd.read_csv("datasets/ground_truth/ecg-heartbeat-av.csv", index_col=0).squeeze() data[:] = zscore(data) ml = Motiflets("ECG", data, n_jobs=-1) ``` -------------------------------- ### Load and Plot Dataset Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_vanilla_ice.ipynb Reads a dataset from a CSV file and plots the time series with ground truth annotations. Ensure the dataset file and ground truth file are correctly placed. ```python ds_name = "Vanilla Ice - Ice Ice Baby" file = 'vanilla_ice.csv' series, df_gt = ml.read_dataset_with_index(file) # , sampling_factor=np.inf plot_dataset(ds_name, series, ground_truth=df_gt) ``` -------------------------------- ### Learning Motifs (LM) Motif Set Generation Source: https://github.com/patrickzib/motiflets/blob/main/notebooks/use_cases_motif_sets_vanilla_ice.ipynb Generates motif sets using the Learning Motifs (LM) algorithm, which involves calling a Java executable. It iterates through error values to adjust parameters and generate motif sets. ```python rs = np.array([r_top1*0.80]) mls = motif_length if rerun_all_jars: for e in errors: rs = rs * (1.0 * (1+e)) mls = np.int32(motif_length * (1-e)) output = subprocess.call(['java', '-jar', '../jars/latent_motifs.jar', dataset, str(list(rs)), str(mls)]) ``` ```python ms_all = { "" : [ [180,386,594,696,903,2091,2247,2658,2864,3071,3277,4364,6792,6998,7103,7205,7412,9276,9482,9689,9896,10311,], ], "-10%" : [ [241,447,551,757,2718,2924,3131,3338,6852,7059,7266,7473,9336,9543,9750,9957,], ], "+10%" : [ [159,466,570,775,2172,2736,2943,3150,3357,4155,4444,4702,6694,6871,7077,7183,7285,7493,9171,9355,9561,9667,9768,9975,10392,10599,10702,10806,], ], } motifsets = to_df(ms_all, "LM", df) plot_all_competitors(series, ds_name, motifsets.offsets.values, motif_length, method_names=motifsets.index.values, ground_truth=df_gt) ```