### Install Development Prerequisites Source: https://github.com/alexandervnikitin/tsgm/blob/main/CONTRIBUTING.md Install the necessary Python packages for development, testing, and documentation. Ensure you have a backend (PyTorch, Jax, or TensorFlow) installed. ```bash pip install -r requirements/requirements.txt pip install -r requirements/tests_requirements.txt pip install -r requirements/docs_requirements.txt ``` -------------------------------- ### Install Optuna Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Model Selection.ipynb Installs the Optuna library using pip. This is a prerequisite for running the hyperparameter optimization. ```python import sys !{sys.executable} -m pip install optuna import optuna ``` -------------------------------- ### Install TSGM with TensorFlow Backend Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Install the TSGM library with TensorFlow as the default backend. This is the recommended installation method for most users. ```bash pip install tsgm[tensorflow] ``` -------------------------------- ### Clone TSGM Repository and Install Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Clone the TSGM repository from GitHub and install it in editable mode. This is the initial setup step for contributing to the project. ```bash git clone github.com/AlexanderVNikitin/tsgm cd tsgm pip install -e . ``` -------------------------------- ### KaggleHub and TSGM Installation Output Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb This output confirms the successful installation of the kagglehub and tsgm libraries, along with their dependencies, and lists the versions of installed packages. ```text Requirement already satisfied: kagglehub in /opt/software/lib/python3.10/site-packages (0.3.12) Requirement already satisfied: packaging in /opt/software/lib/python3.10/site-packages (from kagglehub) (23.2) Requirement already satisfied: pyyaml in /opt/software/lib/python3.10/site-packages (from kagglehub) (6.0.1) Requirement already satisfied: requests in /opt/software/lib/python3.10/site-packages (from kagglehub) (2.31.0) Requirement already satisfied: tqdm in /opt/software/lib/python3.10/site-packages (from kagglehub) (4.65.0) Requirement already satisfied: charset-normalizer<4,>=2 in /opt/software/lib/python3.10/site-packages (from requests->kagglehub) (3.1.0) Requirement already satisfied: idna<4,>=2.5 in /opt/software/lib/python3.10/site-packages (from requests->kagglehub) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/software/lib/python3.10/site-packages (from requests->kagglehub) (2.0.3) Requirement already satisfied: certifi>=2017.4.17 in /opt/software/lib/python3.10/site-packages (from requests->kagglehub) (2024.8.30) Requirement already satisfied: tsgm in /opt/software/lib/python3.10/site-packages (0.0.7) Requirement already satisfied: scipy in /opt/software/lib/python3.10/site-packages (from tsgm) (1.11.3) Requirement already satisfied: numpy in /opt/software/lib/python3.10/site-packages (from tsgm) (1.23.5) Requirement already satisfied: networkx in /opt/software/lib/python3.10/site-packages (from tsgm) (3.2.1) Requirement already satisfied: seaborn in /opt/software/lib/python3.10/site-packages (from tsgm) (0.13.0) Requirement already satisfied: scikit-learn in /opt/software/lib/python3.10/site-packages (from tsgm) (1.3.2) Requirement already satisfied: prettytable in /opt/software/lib/python3.10/site-packages (from tsgm) (3.16.0) Requirement already satisfied: antropy==0.1.6 in /opt/software/lib/python3.10/site-packages (from tsgm) (0.1.6) Requirement already satisfied: yfinance==0.2.28 in /opt/software/lib/python3.10/site-packages (from tsgm) (0.2.28) Requirement already satisfied: tqdm in /opt/software/lib/python3.10/site-packages (from tsgm) (4.65.0) Requirement already satisfied: dtaidistance>=2.3.10 in /opt/software/lib/python3.10/site-packages (from tsgm) (2.3.13) Requirement already satisfied: tensorflow<2.16 in /opt/software/lib/python3.10/site-packages (from tsgm) (2.14.0) Requirement already satisfied: tensorflow-probability<0.24.0 in /opt/software/lib/python3.10/site-packages (from tsgm) (0.22.1) Requirement already satisfied: statsmodels in /opt/software/lib/python3.10/site-packages (from tsgm) (0.14.0) Requirement already satisfied: numba>=0.57 in /opt/software/lib/python3.10/site-packages (from antropy==0.1.6->tsgm) (0.58.1) Requirement already satisfied: stochastic in /opt/software/lib/python3.10/site-packages (from antropy==0.1.6->tsgm) (0.7.0) Requirement already satisfied: pandas>=1.3.0 in /opt/software/lib/python3.10/site-packages (from yfinance==0.2.28->tsgm) (2.2.3) Requirement already satisfied: requests>=2.31 in /opt/software/lib/python3.10/site-packages (from yfinance==0.2.28->tsgm) (2.31.0) Requirement already satisfied: multitasking>=0.0.7 in /opt/software/lib/python3.10/site-packages (from yfinance==0.2.28->tsgm) (0.0.11) Requirement already satisfied: lxml>=4.9.1 in /opt/software/lib/python3.10/site-packages (from yfinance==0.2.28->tsgm) (4.9.3) Requirement already satisfied: appdirs>=1.4.4 in /opt/software/lib/python3.10/site-packages (from yfinance==0.2.28->tsgm) (1.4.4) ``` -------------------------------- ### Install TensorFlow Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb Installs the TensorFlow library, a prerequisite for using VAEs and other deep learning models. ```python #pip install tensorflow ``` -------------------------------- ### Install TSGM Library Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/getting_started_with_tsgm.ipynb Installs the TSGM library from GitHub. This is the first step before using any of its functionalities. ```python !pip install git+https://github.com/alexandervnikitin/tsgm ``` -------------------------------- ### Install TSGM with PyTorch Backend Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Install the TSGM library with PyTorch as the backend. Ensure you have PyTorch installed separately if needed. ```bash pip install tsgm[torch] ``` -------------------------------- ### Install JAX with Metal Support Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Install JAX with Metal support for optimal performance on M1/M2 chips. This is recommended for JAX users. ```bash pip install -U "jax[metal]" ``` -------------------------------- ### Basic TSGM Installation Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Install the core TSGM library without a specific backend. You will need to install your preferred backend separately. ```bash pip install tsgm # Then install your preferred backend: # For TensorFlow: pip install tensorflow tensorflow-probability # For PyTorch: pip install torch torchvision # For JAX: pip install jax jaxlib ``` -------------------------------- ### Install TSlearn Library Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb Installs the tslearn library, a toolkit for time series analysis and machine learning. ```python #pip install tslearn ``` -------------------------------- ### TPU Setup and Strategy Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Using Multiple GPUs or TPU.ipynb Initializes a TPU cluster resolver, connects to the TPU, initializes the TPU system, and defines a TPUStrategy for distributed training. ```python tpu = tf.distribute.cluster_resolver.TPUClusterResolver() print('Running on TPU ', tpu.cluster_spec().as_dict()['worker']) tf.config.experimental_connect_to_cluster(tpu) tf.tpu.experimental.initialize_tpu_system(tpu) strategy = tf.distribute.experimental.TPUStrategy(tpu) print("REPLICAS: ", strategy.num_replicas_in_sync) ``` -------------------------------- ### Install TSGM with JAX Backend Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Install the TSGM library with JAX as the backend. JAX offers excellent performance, especially on M1/M2 chips. ```bash pip install tsgm[jax] ``` -------------------------------- ### Install TSGM with All Backends Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Install the TSGM library with support for all available backends (TensorFlow, PyTorch, JAX). ```bash pip install tsgm[all] ``` -------------------------------- ### Install TSGM Package Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/index.md Install the TSGM package using pip. This is the first step to using the framework. ```bash pip install tsgm ``` -------------------------------- ### Install KaggleHub and TSGM Libraries Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb Installs the necessary libraries for interacting with Kaggle datasets and utilizing the TSGM framework. It also sets up TensorFlow to use legacy Keras. ```python !pip install kagglehub !pip install tsgm os.environ["TF_USE_LEGACY_KERAS"] = "1" import os import numpy as np import tsgm import tsgm.models.augmentations as augmentation import tsgm.models.architectures as arch import tsgm.models as models import matplotlib.pyplot as plt import csv import tensorflow as tf from tensorflow import keras import sklearn import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix from sklearn.metrics import f1_score from sklearn.metrics import accuracy_score import pandas as pd import math import kagglehub ``` -------------------------------- ### Install TSGM Library Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb Installs the tsgm library, which likely contains tools for time series data augmentation. ```python #pip install tsgm ``` -------------------------------- ### Install TSGM in Development Mode Source: https://github.com/alexandervnikitin/tsgm/blob/main/CONTRIBUTING.md Install the TSGM library in editable mode to allow for local development and testing. ```bash pip install -e . ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/RCGAN.ipynb Imports necessary libraries for RCGAN implementation, including TensorFlow, Keras, TSGM utilities, and plotting tools. Sets up autoreload for development. ```python %load_ext autoreload %autoreload 2 import functools import numpy as np from tensorflow import keras from tensorflow.keras import layers import tensorflow as tf import tensorflow_privacy as tf_privacy import tsgm import matplotlib.pyplot as plt ``` -------------------------------- ### Install TSGM on M1/M2 Chips Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Install TSGM on Apple M1 and M2 chips with your preferred backend. JAX is recommended for optimal performance. ```bash # Install with TensorFlow backend pip install tsgm[tensorflow] # Or install with PyTorch backend pip install tsgm[torch] # Or install with JAX backend (excellent performance on M1/M2) pip install tsgm[jax] ``` -------------------------------- ### Install tsgm via pip Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/guides/installation.md Use this command to install the package directly from the Python Package Index. ```bash $ pip install tsgm ``` -------------------------------- ### Generate Toy Dataset Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/guides/augmentations.md Generates a synthetic dataset for demonstrating time series augmentations. This is a prerequisite for most augmentation examples. ```python import tsgm X = tsgm.utils.gen_sine_dataset(100, 64, 2, max_value=20) ``` -------------------------------- ### Accessing and Instantiating Architectures from Zoo Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/guides/introduction.md Demonstrates how to retrieve a model architecture from the TSGM zoo and instantiate it with specified dimensions. The retrieved model can then be used for further operations, such as getting its configuration dictionary. ```python import tsgm model_type = tsgm.models.architectures.zoo["cgan_lstm_n"] arch = model_type( seq_len=seq_len, feat_dim=feat_dim, latent_dim=latent_dim, output_dim=output_dim) arch_dict = arch.get() # arch will store `.generator` and `.discriminator` fields for cGAN ``` -------------------------------- ### Train and Generate with Conditional GAN Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/index.md Example of initializing, compiling, training, and generating samples with a conditional GAN architecture from the TSGM zoo. Requires TensorFlow and Keras. Ensure dataset is preprocessed. ```python import tsgm # ... Define hyperparameters ... # dataset is a tensor of shape n_samples x seq_len x feature_dim # Zoo contains several prebuilt architectures: we choose a conditional GAN architecture architecture = tsgm.models.architectures.zoo["cgan_base_c4_l1"]( seq_len=seq_len, feat_dim=feature_dim, latent_dim=latent_dim, output_dim=0) discriminator, generator = architecture.discriminator, architecture.generator # Initialize GAN object with selected discriminator and generator gan = tsgm.models.cgan.GAN( discriminator=discriminator, generator=generator, latent_dim=latent_dim ) gan.compile( d_optimizer=keras.optimizers.Adam(learning_rate=0.0003), g_optimizer=keras.optimizers.Adam(learning_rate=0.0003), loss_fn=keras.losses.BinaryCrossentropy(from_logits=True), ) gan.fit(dataset, epochs=1) # Generate 10 synthetic samples result = gan.generate(10) ``` -------------------------------- ### Train a Generative Model with TSGM Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Example of initializing a conditional GAN architecture from the TSGM zoo and extracting the discriminator and generator models. Assumes hyperparameters and dataset are defined. ```python import tsgm # ... Define hyperparameters ... # dataset is a tensor of shape n_samples x seq_len x feature_dim # Zoo contains several prebuilt architectures: we choose a conditional GAN architecture architecture = tsgm.models.architectures.zoo["cgan_base_c4_l1"]( seq_len=seq_len, feat_dim=feature_dim, latent_dim=latent_dim, output_dim=0) discriminator, generator = architecture.discriminator, architecture.generator ``` -------------------------------- ### Build Documentation Source: https://github.com/alexandervnikitin/tsgm/blob/main/CONTRIBUTING.md Navigate to the docs directory and build the HTML documentation using the 'make html' command. ```bash cd docs make html ``` -------------------------------- ### Load necessary libraries and set up environment Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Metrics Tutorial.ipynb Imports essential libraries for data manipulation, plotting, and machine learning, and configures the environment for autoreload and inline plotting. ```python %load_ext autoreload %autoreload 2 import os import warnings import functools import numpy as np import matplotlib.pyplot as plt from tensorflow import keras warnings.filterwarnings('ignore') %matplotlib inline ``` -------------------------------- ### Select and Instantiate cVAE Architecture Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/VAEs/cVAE.ipynb Choose a pre-defined cVAE architecture from the zoo and instantiate it with specified dimensions. This sets up the encoder and decoder components. ```python model_type = tsgm.models.architectures.zoo["cvae_conv5"] architecture = model_type(seq_len=seq_len, feat_dim=feat_dim, latent_dim=latent_dim, output_dim=2) encoder, decoder = architecture._encoder, architecture._decoder ``` -------------------------------- ### Initialize Optuna study Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Model Selection.ipynb Creates an Optuna study object for minimization. This object will manage the optimization process. ```python # we aim at minimizing the discrepancy metric defined in next cell study = optuna.create_study(direction="minimize") ``` -------------------------------- ### Compile and Train VAE Model Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb This snippet shows how to compile a VAE model with a specified optimizer and loss, and then train it on a dataset for a set number of epochs. Ensure the dataset and model are defined prior to this step. ```python vae.compile(optimizer=tf.keras.optimizers.Adam()) vae.fit(x=x_train, y=None, epochs=100, batch_size=32, shuffle=True, validation_split=0.2) ``` -------------------------------- ### Download MIMIC-IV ECG Dataset Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb Initializes dictionaries for filtered and treatment data, then downloads the MIMIC-IV ECG dataset using kagglehub. ```python filtered_data = {} treatment_data = {} path = kagglehub.dataset_download("kirilltervoridor/mimic-iv-ecg-dataset") ``` -------------------------------- ### Prepare Test Data for ECG Classification Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb This snippet prepares the test dataset by applying modifications and labeling. It is used to get the data in the correct format for model evaluation. ```python x_test, y_test = modify_and_label(test_f1, ``` -------------------------------- ### Display best trial parameters Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Model Selection.ipynb Prints the parameters of the best trial found during the Optuna optimization process. ```python # parameters corresponding to best trial study.best_trial ``` -------------------------------- ### Apply Magnitude Warping Augmentation Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/guides/augmentations.md Transforms time series magnitude by multiplying with a cubic spline curve. This scales the time series amplitude, useful for synthetic examples with varying sine wave magnitudes. ```python aug_model = tsgm.models.augmentations.MagnitudeWarping() samples = aug_model.generate(X=X, n_samples=10, sigma=1) ``` -------------------------------- ### Load and Scale Toy Dataset Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/VAEs/cVAE.ipynb Generates a toy dataset and applies feature-wise scaling to ensure each feature is within the range of [0, 1]. This is a prerequisite for training the VAE model. ```python X, y_i = tsgm.utils.gen_sine_vs_const_dataset(5000, 256, 1, max_value=20, const=10) scaler = tsgm.utils.TSFeatureWiseScaler((0, 1)) X = scaler.fit_transform(X).astype(np.float32) y = keras.utils.to_categorical(y_i, output_dim).astype(np.float32) ``` -------------------------------- ### Prepare Training Data Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Using Multiple GPUs or TPU.ipynb Generates a synthetic dataset of sine waves and constants, scales the features, converts labels to categorical format, and creates a TensorFlow dataset for training. ```python X, y_i = tsgm.utils.gen_sine_vs_const_dataset(5000, seq_len, 1, max_value=20, const=10) scaler = tsgm.utils.TSFeatureWiseScaler((-1, 1)) X_train = scaler.fit_transform(X) y = keras.utils.to_categorical(y_i, 2) X_train = X_train.astype(np.float32) y = y.astype(np.float32) dataset = tf.data.Dataset.from_tensor_slices((X_train, y)) dataset = dataset.shuffle(buffer_size=1024).batch(batch_size) ``` -------------------------------- ### Train VAE and Generate Samples Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb Initializes a VAE model, compiles it with Adam optimizer, trains it on dataset X1 for 100 epochs, and generates 5*N samples. Requires TensorFlow and tsgm libraries. ```python latent_dim = 10 architecture = tsgm.models.zoo["vae_conv5"](seq_len, 1, latent_dim) encoder, decoder = architecture.encoder, architecture.decoder vae = tsgm.models.cvae.BetaVAE(encoder, decoder) vae.compile(optimizer=tf.keras.optimizers.Adam()) vae.fit(X1, epochs=100, batch_size=50) X1_gen = vae.generate(5*N).numpy() ``` -------------------------------- ### Define GAN Parameters Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/Temporal cGAN.ipynb Sets up key parameters for the GAN, including batch size, latent dimension, feature dimension, sequence length, and output dimension (number of classes). ```python batch_size = 128 latent_dim = 1 feature_dim = 1 seq_len = 123 output_dim = 1 generator_in_channels = latent_dim + output_dim discriminator_in_channels = feature_dim + output_dim ``` -------------------------------- ### Instantiate Consistency Metric Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Metrics Tutorial.ipynb Creates a ConsistencyMetric object using the list of evaluators. ```python consistency_metric = tsgm.metrics.ConsistencyMetric(evaluators=evaluators) ``` -------------------------------- ### Synthetic Data Generation with Beta-VAE Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/guides/introduction.md Illustrates synthetic data generation using a Beta-VAE model. The data is scaled, a VAE architecture is defined, compiled, trained, and then used for generation. ```python import tsgm from tensorflow import keras n, n_ts, n_features = 1000, 24, 5 data = tsgm.utils.gen_sine_dataset(n, n_ts, n_features) scaler = tsgm.utils.TSFeatureWiseScaler() scaled_data = scaler.fit_transform(data) architecture = tsgm.models.zoo["vae_conv5"](n_ts, n_features, 10) encoder, decoder = architecture.encoder, architecture.decoder vae = tsgm.models.cvae.BetaVAE(encoder, decoder) vae.compile(optimizer=keras.optimizers.Adam()) vae.fit(scaled_data, epochs=1, batch_size=64) vae.generate(10) ``` -------------------------------- ### Initialize Models and Evaluators Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Metrics Tutorial.ipynb Initializes several ConvLSTM models with varying numbers of blocks and compiles them. Then, creates an EvaluatorConvLSTM instance for each model. ```python seq_len, feat_dim, n_classes = *Xr.shape[1:], 2 models = [tsgm.models.zoo["clf_cl_n"](seq_len, feat_dim, n_classes, n_conv_lstm_blocks=i) for i in range(1, 4)] for m in models: m.model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) evaluators = [EvaluatorConvLSTM(m.model) for m in models] ``` -------------------------------- ### Initialize, Compile, and Train a TSGM GAN Model Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Initialize a GAN object with specified discriminator and generator, compile it with optimizers and a loss function, and then train it on a dataset. Ensure the dataset and epochs are defined before use. ```python gan = tsgm.models.cgan.GAN( discriminator=discriminator, generator=generator, latent_dim=latent_dim ) gan.compile( d_optimizer=keras.optimizers.Adam(learning_rate=0.0003), g_optimizer=keras.optimizers.Adam(learning_rate=0.0003), loss_fn=keras.losses.BinaryCrossentropy(from_logits=True), ) gan.fit(dataset, epochs=N_EPOCHS) ``` -------------------------------- ### Prepare Synthetic Data for Training Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb Prepares synthetic data by squeezing it and creating a specific pattern of values within a sequence. This is used as input for the initial training phase. ```python Synthetic_data = tf.squeeze(Synthetic_data) ind = np.arange(13,seq_length,13) x_ex = np.zeros([99,seq_length]) for item in ind: for i in range(0,99): x_ex[i,item] = 0.27 ``` -------------------------------- ### Set Training Parameters Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb Defines key parameters for the training process, including sequence length, number of classes, training stages, epochs, and batch size. ```python seq_length = 250 num_classes = 2 tr_stages = 2 + 1 #Amount +1, since printing start at 1 epochs = 50 batch_size = 13 ``` -------------------------------- ### Load and Scale Sine Dataset Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/TimeGAN.ipynb Generates a toy sine wave dataset and then applies feature-wise scaling to ensure each feature is within the [0, 1] range using TSFeatureWiseScaler. ```python data = tsgm.utils.gen_sine_dataset(10000, 24, 5) scaler = tsgm.utils.TSFeatureWiseScaler() scaled_data = scaler.fit_transform(data) ``` -------------------------------- ### Display First Few Rows of Physionet 2012 Training Features Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Datasets.ipynb Displays the first three rows of the training features for the Physionet 2012 dataset. This provides a glimpse into the structure and content of the measurement data. ```python train_X.head(3) ``` -------------------------------- ### Display Decoder Summary Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/VAEs/cVAE.ipynb Prints a summary of the decoder model, detailing its layers, output shapes, and parameter counts. This helps in understanding the decoder's configuration. ```python decoder.summary() ``` -------------------------------- ### Using Distance Metrics for Time Series Evaluation Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/guides/introduction.md Shows how to set up and use a DistanceMetric to compare real and synthetic time series datasets. It involves defining statistics, a discrepancy function, and then applying the metric to two dataset objects. ```python import tsgm import functools import numpy as np Xr, yr = tsgm.utils.gen_sine_vs_const_dataset(10, 100, 20, max_value=2, const=1) # real data Xs, ys = Xr + 1e-5, yr # synthetic data d_real = tsgm.dataset.Dataset(Xr, yr) d_syn = tsgm.dataset.Dataset(Xs, ys) statistics = [ functools.partial(tsgm.metrics.statistics.axis_max_s, axis=None), functools.partial(tsgm.metrics.statistics.axis_min_s, axis=None)] sim_metric = tsgm.metrics.DistanceMetric( statistics=statistics, discrepancy=lambda x, y: np.linalg.norm(x - y) ) sim_metric = tsgm.metrics.DistanceMetric( statistics=statistics, discrepancy=discrepancy_func ) sim_metric(d_real, d_syn) ``` -------------------------------- ### Configure and Use Discriminative Metric Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/evaluation.ipynb Sets up a classification model (LSTM) and compiles it, then uses the DiscriminativeMetric to evaluate how well the model can distinguish between real and synthetic data. ```python # use LSTM classification model from TSGM zoo. model = tsgm.models.zoo["clf_cl_n"]( seq_len=Xr.shape[1], feat_dim=Xr.shape[2], output_dim=2).model # set output_dim to 2 so that softmax can work properly model.compile( tf.keras.optimizers.Adam(), tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False) # SparseCategoricalCrossentropy for multiple classes ) discr_metric = tsgm.metrics.DiscriminativeMetric() print( discr_metric( d_hist=Xr, d_syn=Xs, model=model, test_size=0.2, random_seed=42, n_epochs=1 ) ) ``` -------------------------------- ### Run Linters with Ruff Source: https://github.com/alexandervnikitin/tsgm/blob/main/CONTRIBUTING.md Check the codebase for style and potential errors using the Ruff linter on the 'tsgm' directory. ```bash ruff check tsgm/ ``` -------------------------------- ### Manually Shuffle Features in a Sample Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/augmentations.ipynb Demonstrates feature shuffling by manually reordering the features of a sample time series. Useful for understanding the Shuffle augmentation. ```python timesteps = np.arange(0, 10, 0.1) sample = np.stack([np.sin(timesteps), np.cos(timesteps), np.log(timesteps + 1), np.log(timesteps + 2), timesteps / 10]) plt.subplot(1, 2, 1) for feature_id in range(5): plt.plot(timesteps, sample[feature_id], label=f"Feature #{feature_id + 1}") plt.title("Original sample") plt.legend() permutation = [1, 2, 3, 0, 4] sample = sample[permutation] plt.subplot(1, 2, 2) for feature_id in range(5): plt.plot(timesteps, sample[feature_id], label=f"Feature #{feature_id + 1}") plt.title("Augmented sample") plt.legend() plt.show() ``` -------------------------------- ### Prepare Data for t-SNE Visualization Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/VAEs/cVAE.ipynb Prepares a subset of the original data and generated samples for t-SNE visualization. It concatenates original and generated data, assigns class labels and point styles, and reshapes data for t-SNE transformation. ```python import numpy as np from sklearn.manifold import TSNE limit = 1000 X_sub, y_sub = X[:limit], y[:limit] X_gen, y_gen = vae.generate(y[:limit]) X_gen = X_gen.numpy() tsne = TSNE(n_components=2, learning_rate='auto', init='random') X_all = np.concatenate((X_sub, X_gen)) y_all = np.concatenate((y_sub, y_gen)) c = np.argmax(np.concatenate((y_sub, y_gen)), axis=1) colors = {0: "class 0", 1: "class 1"} c = [colors[el] for el in c] point_styles = ["hist"] * X_sub.shape[0] + ["gen"] * X_gen.shape[0] X_emb = tsne.fit_transform(np.resize(X_all, (X_all.shape[0], X_all.shape[1] * X_all.shape[2]))) ``` -------------------------------- ### Generate and Prepare Samples for Visualization Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/cGAN.ipynb Generates a specified number of samples from the trained Conditional GAN. The generated samples and their corresponding labels are prepared for visualization. ```python limit = 500 X_gen = cond_gan.generate(y[:limit]) X_gen = X_gen.numpy() y_gen = y[:limit] ``` -------------------------------- ### Import Core Libraries Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb Imports essential Python libraries for numerical operations, deep learning, machine learning metrics, and plotting. ```python import numpy as np import tensorflow as tf from sklearn.metrics import accuracy_score from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import kagglehub ``` -------------------------------- ### Define and Train Convolutional VAE Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb This snippet defines a convolutional VAE architecture, compiles it with Adam optimizer, and trains it on the provided dataset X0 for 100 epochs. It then generates 5*N samples using the trained VAE. ```python latent_dim = 10 architecture = tsgm.models.zoo["vae_conv5"](seq_len, 1, latent_dim) encoder, decoder = architecture.encoder, architecture.decoder vae = tsgm.models.cvae.BetaVAE(encoder, decoder) vae.compile(optimizer=tf.keras.optimizers.Adam()) vae.fit(X0, epochs=100, batch_size=50) X0_gen = vae.generate(5*N).numpy() ``` -------------------------------- ### Display First Few Rows of Physionet 2012 Training Labels Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Datasets.ipynb Displays the first three rows of the training labels for the Physionet 2012 dataset. This shows the target variable for patient mortality prediction. ```python train_y.head(3) ``` -------------------------------- ### Generate and Scale Toy Dataset Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/Temporal cGAN.ipynb Generates a synthetic time series dataset with sine and constant switch patterns and then applies feature-wise scaling to normalize each feature to the range [-1, 1]. ```python X, y = tsgm.utils.gen_sine_const_switch_dataset(50_000, seq_len, 1, max_value=20, const=10) scaler = tsgm.utils.TSFeatureWiseScaler((-1, 1)) X_train = scaler.fit_transform(X) X_train = X_train.astype(np.float32) y = y.astype(np.float32) dataset = tf.data.Dataset.from_tensor_slices((X_train, y)) dataset = dataset.shuffle(buffer_size=1024).batch(batch_size) ``` -------------------------------- ### Configure Keras Backend Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Set the Keras backend using the environment variable. This determines which deep learning library TSGM will use. ```bash # For TensorFlow backend export KERAS_BACKEND=tensorflow # For PyTorch backend export KERAS_BACKEND=torch # For JAX backend export KERAS_BACKEND=jax ``` -------------------------------- ### Define and Train Beta-VAE Model Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/VAEs/VAE.ipynb Initializes a Beta-Variational Autoencoder (Beta-VAE) using the chosen encoder and decoder. The model is compiled with the Adam optimizer and then trained on the scaled dataset for 100 epochs with a batch size of 64. ```python vae = tsgm.models.cvae.BetaVAE(encoder, decoder) vae.compile(optimizer=keras.optimizers.Adam()) vae.fit(scaled_data, epochs=100, batch_size=64) ``` -------------------------------- ### Import Libraries and Load Data Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/evaluation.ipynb Imports necessary libraries for time series generation and evaluation, and loads an exemplary dataset. It configures environment variables for TensorFlow and Keras. ```python %load_ext autoreload %autoreload 2 import os os.environ["KERAS_BACKEND"] = "tensorflow" from IPython.display import SVG, display, Image import numpy as np import functools import sklearn import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf import keras import tsgm ``` -------------------------------- ### Calculate Consistency Metric Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Metrics Tutorial.ipynb Calculates the consistency metric between the real and synthetic datasets using the initialized evaluators. ```python consistency_metric(d_real, d_syn) ``` -------------------------------- ### Initialize Downstream Performance Metric Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Metrics Tutorial.ipynb Initializes a downstream model, an evaluator, and the DownstreamPerformanceMetric object. This metric evaluates model performance on real data versus real data augmented with synthetic data. ```python downstream_model = tsgm.models.zoo["clf_cl_n"](seq_len, feat_dim, n_classes, n_conv_lstm_blocks=1).model downstream_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) evaluator = EvaluatorConvLSTM(downstream_model) downstream_perf_metric = tsgm.metrics.DownstreamPerformanceMetric(evaluator) ``` -------------------------------- ### Enable MPS Fallback for PyTorch on M1/M2 Source: https://github.com/alexandervnikitin/tsgm/blob/main/README.md Set this environment variable if you encounter MPS-related errors with PyTorch on M1/M2 chips to enable CPU fallback. ```bash export PYTORCH_ENABLE_MPS_FALLBACK=1 ``` -------------------------------- ### Display Generator Summary Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/RCGAN.ipynb Prints a summary of the generator model, detailing its layers, output shapes, and parameter count. ```python architecture._generator.summary() ``` -------------------------------- ### Plot Real and VAE-Generated Samples for Class 0 Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb Visualizes real and augmented time series samples for class 0 (no anomalies). Requires `matplotlib` and `numpy`. ```python plt.subplot(1, 2, 1) for i in range(5): plt.plot(np.linspace(0, 1, seq_len), X0[i, :, 0]) plt.title("real") plt.subplot(1, 2, 2) for i in range(5): plt.plot(np.linspace(0, 1, seq_len), X0_gen[i, :, 0]) plt.title("augmented") plt.show() ``` -------------------------------- ### Instantiate and Use Distance Metric Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/evaluation.ipynb Creates a DistanceMetric object using the defined statistics and discrepancy function, then calculates the distance between real and synthetic data. ```python dist_metric = tsgm.metrics.DistanceMetric( statistics=statistics, discrepancy=discrepancy_func ) print(dist_metric(d_real, d_syn)) ``` -------------------------------- ### Visualize Training Data Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/RCGAN.ipynb Visualizes a sample of the generated time series data along with their corresponding labels using a line plot. ```python tsgm.utils.visualize_ts_lineplot(X_train, y_i, num=2) ``` -------------------------------- ### Loading UCR Dataset with TSGM Source: https://github.com/alexandervnikitin/tsgm/blob/main/docs/guides/introduction.md Shows how to load a dataset from the UCR archive using TSGM's UCRDataManager. It asserts that the summary is None and retrieves training and testing data. ```python import tsgm ucr_data_manager = tsgm.utils.UCRDataManager(ds="gunpoint") assert ucr_data_manager.summary() is None X_train, y_train, X_test, y_test = ucr_data_manager.get() ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/getting_started_with_tsgm.ipynb Imports core libraries for data manipulation, plotting, and deep learning with TensorFlow and Keras. Sets the Keras backend to TensorFlow. ```python import numpy as np import matplotlib.pyplot as plt import keras from keras import layers import tensorflow as tf import os os.environ['KERAS_BACKEND'] = 'tensorflow' import tsgm ``` -------------------------------- ### Load and Preprocess ECG Data Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Classical Methods vs VAE.ipynb Loads ECG data from the MIT-BIH arrhythmia database, splits it into training and testing sets, and preprocesses it for anomaly detection. This involves separating samples by class and formatting them into tensors. ```python N = 1000 seq_len = 187 # Load data path = kagglehub.dataset_download("shayanfazeli/heartbeat") file = open(path + "/mitbih_test.csv", "r") lines = file.readlines() lines = sorted(lines[1:], key = lambda x: np.random.rand()) X0 = np.zeros((N, seq_len, 1)) X1 = np.zeros((N, seq_len, 1)) X_test = np.zeros((2*N, seq_len, 1)) y0 = tf.keras.utils.to_categorical(np.zeros((N,)), 2) y1 = tf.keras.utils.to_categorical(np.ones((N,)), 2) y_test = np.zeros((2*N, 2)) n0, n1, j = 0, 0, 0 for i in range(len(lines)): split = lines[i].split(",") if split[-1][0] == "0" and n0 < N: X0[n0, :, 0] = split[:-1] n0 += 1 elif split[-1][0] != "0" and n1 < N: X1[n1, :, 0] = split[:-1] n1 += 1 elif split[-1][0] == "0" and n0 < 2*N: X_test[j, :, 0] = split[:-1] y_test[j] = [1, 0] n0 += 1 j += 1 elif split[-1][0] != "0" and n1 < 2*N: X_test[j, :, 0] = split[:-1] y_test[j] = [0, 1] n1 += 1 j += 1 file.close() del lines ``` -------------------------------- ### Display Discriminator Summary Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/RCGAN.ipynb Prints a summary of the discriminator model, showing its layers, output shapes, and parameter count. ```python architecture._discriminator.summary() ``` -------------------------------- ### Define and Train TimeGAN Model Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/TimeGAN.ipynb Initializes a TimeGAN model with specified parameters (sequence length, module type, dimensions, feature count, layers, batch size, and gamma), compiles it (using default Adam optimizers), and trains it on the scaled data for a set number of epochs. ```python model = tsgm.models.timeGAN.TimeGAN( seq_len=24, module="gru", hidden_dim=24, n_features=5, n_layers=3, batch_size=256, gamma=1.0, ) # .compile() sets all optimizers to Adam by default model.compile() model.fit( data=scaled_data, epochs=100, ) ``` -------------------------------- ### Visualize Data Reconstruction Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/VAEs/VAE.ipynb Visualizes a sample of the original scaled time series data against their reconstructed counterparts. This helps in qualitatively assessing the performance of the VAE. ```python tsgm.utils.visualize_original_and_reconst_ts(scaled_data, x_decoded, num=10) ``` -------------------------------- ### Visualize Generated Samples with t-SNE Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/cGAN.ipynb Visualizes the real training data and the generated data in a 2D space using t-SNE. This helps in assessing the quality and distribution of the generated time series compared to the original data. ```python tsgm.utils.visualize_tsne(X_train[:limit], y[:limit], X_gen, y_gen) ``` -------------------------------- ### Display Encoder Summary Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/VAEs/cVAE.ipynb Prints a summary of the encoder model, detailing its layers, output shapes, and parameter counts. This is useful for verifying the encoder's structure. ```python encoder.summary() ``` -------------------------------- ### Visualize Original and Augmented Samples Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/augmentations.ipynb Plots original time series data alongside augmented samples. Used to visually inspect the effect of augmentation techniques. ```python plot_samples_aug(X, samples) ``` -------------------------------- ### Import Libraries Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/Temporal cGAN.ipynb Imports necessary libraries for building and training GANs, including TensorFlow, Keras, NumPy, Matplotlib, and Seaborn. ```python %load_ext autoreload %autoreload 2 from tensorflow import keras from tensorflow.keras import layers import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import seaborn as sns ``` -------------------------------- ### Import Libraries for GAN Training Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/GANs/cGAN.ipynb Imports necessary libraries for GAN development, including TensorFlow, Keras, NumPy, and Matplotlib. Sets the Keras backend to TensorFlow. ```python %load_ext autoreload %autoreload 2 import os os.environ["KERAS_BACKEND"] = "tensorflow" import numpy as np import matplotlib.pyplot as plt from tensorflow import keras from tensorflow.keras import layers import tensorflow as tf import tsgm ``` -------------------------------- ### Train GAN Model Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb Prepares the training data into a TensorFlow dataset and trains the GAN model for a specified number of epochs. The data should be batched before fitting. ```python x_data, dataset = get_started(aug_meth="",data=train_t) datum = tf.data.Dataset.from_tensor_slices((x_data,dataset)) datum = datum.batch(5) GAN_Control.fit(datum,epochs=3) ``` -------------------------------- ### Instantiate Distance Metric Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Metrics Tutorial.ipynb Combines the defined statistics and discrepancy function into a DistanceMetric object for evaluating dataset similarity. ```python sim_metric = tsgm.metrics.DistanceMetric( statistics=statistics, discrepancy=discrepancy_func ) ``` -------------------------------- ### Run hyperparameter optimization Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Model Selection.ipynb Executes the Optuna optimization process by calling the objective function for a specified number of trials. ```python study.optimize(objective, n_trials=10) ``` -------------------------------- ### Generate Samples with DTW Barycentric Averaging Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/augmentations.ipynb Generates synthetic time series samples using the DTW Barycentric Averaging method. Requires pre-defined or randomly sampled initial time series. ```python aug_model = tsgm.models.augmentations.DTWBarycentricAveraging() initial_timeseries = random.sample(range(X.shape[0]), 10) initial_timeseries = X[initial_timeseries] samples = aug_model.generate(X=X, n_samples=10, initial_timeseries=initial_timeseries ) ``` -------------------------------- ### Import Libraries Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/Using Multiple GPUs or TPU.ipynb Imports necessary libraries for data manipulation, model building, and distributed computing with TensorFlow. ```python #import functools import numpy as np from tensorflow import keras #from tensorflow.keras import layers import tensorflow as tf import tsgm import matplotlib.pyplot as plt ``` -------------------------------- ### Compile Classification Model Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/community/ECG_classification.ipynb Initializes the RNN classification model and compiles it with the Adam optimizer, Categorical Crossentropy loss, and accuracy/F1Score metrics. The optimizer is configured with specific learning rate and beta values. ```python Classif_model = RNN(seq_length, num_classes) loss_Fn = keras.losses.CategoricalCrossentropy() optimizer = keras.optimizers.Adam(learning_rate=0.05, beta_1=0.8, beta_2=0.999, epsilon=1e-7) Classif_model.compile(optimizer=optimizer, loss=loss_Fn, metrics=["categorical_accuracy","F1Score"]) ``` -------------------------------- ### Initialize and Use Privacy Membership Inference Metric Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/evaluation.ipynb Initializes the PrivacyMembershipInferenceMetric with a custom attacker model and calculates the privacy score using real, synthetic, and test datasets. ```python attacker = FlattenTSOneClassSVM(sklearn.svm.OneClassSVM()) privacy_metric = tsgm.metrics.PrivacyMembershipInferenceMetric( attacker=attacker ) ``` ```python X_test, y_test = tsgm.utils.gen_sine_vs_const_dataset(10, 100, 20, max_value=2, const=1) d_test = tsgm.dataset.Dataset(X_test, keras.utils.to_categorical(y_test)) # 1 indicates high privacy and 0 -- low privacy. privacy_metric(d_real, d_syn, d_test) ``` -------------------------------- ### Calculate and Print Entropy for Real and Synthetic Data Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/evaluation.ipynb Calculates the spectral entropy for both real (Xr) and synthetic (Xs) datasets and prints the results. This metric helps quantify the diversity of the datasets. ```python spec_entropy = tsgm.metrics.EntropyMetric() print(spec_entropy(Xr)) print(spec_entropy(Xs)) ``` -------------------------------- ### Display Diversity Visualization Source: https://github.com/alexandervnikitin/tsgm/blob/main/tutorials/evaluation.ipynb Displays a visualization comparing real and synthetic data points to illustrate the concept of data diversity. ```python Image("./images/diverse_synthetic_data.jpg", height=512, width=512) ```