### Install Synthcity Source: https://github.com/vanderschaarlab/synthcity/blob/main/docs/README.md Installation commands for the library via PyPI or from source. ```bash $ pip install synthcity ``` ```bash $ pip install . ``` -------------------------------- ### Install Synthcity Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial10_sequential_synthesis.ipynb Install the library via pip. ```bash !pip install synthcity ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/vanderschaarlab/synthcity/blob/main/CONTRIBUTING.MD Clones the repository and installs the package with testing dependencies. ```bash git clone https://github.com/vanderschaarlab/synthcity.git cd synthcity pip install -e .[testing] ``` -------------------------------- ### Install Synthcity and HyperImpute Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial9_dealing_with_missing_data.ipynb Install the necessary libraries for Synthcity and HyperImpute. This also includes uninstalling specific versions of torchaudio and torchdata if they are present. ```python !pip install synthcity !pip install hyperimpute !pip uninstall -y torchaudio torchdata ``` -------------------------------- ### Initialize Synthcity Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_arf.ipynb Setup warnings and import necessary plugins for the synthetic data generation workflow. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_iris, load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "arf" ``` -------------------------------- ### Install testing dependencies Source: https://github.com/vanderschaarlab/synthcity/blob/main/docs/README.md Use this command to install the necessary packages for running tests in the Synthcity environment. ```bash pip install .[testing] ``` -------------------------------- ### Install CTGAN library Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial1_add_a_new_plugin.ipynb Installs the CTGAN library, which is used for integrating its implementation into Synthcity. ```python !pip install ctgan ``` -------------------------------- ### Initialize Sequential Data Loader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_syn_seq.ipynb Setup the environment and initialize the Syn_SeqDataLoader with a diabetes dataset. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import Syn_SeqDataLoader eval_plugin = "syn_seq" ``` ```python # synthcity absolute from synthcity.plugins.core.dataloader import GenericDataLoader X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y loader = Syn_SeqDataLoader(X, target_column="target", sensitive_columns=["sex"]) loader.dataframe() ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/images/plugin_image_adsgan.ipynb Imports the required libraries for the Synthcity image GAN example, including warnings and the Plugins class. ```python # stdlib import warnings warnings.filterwarnings("ignore") # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "image_adsgan" ``` -------------------------------- ### Install synthcity and Plotly Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial8_hyperparameter_optimization.ipynb Installs the synthcity library and the plotly package, and uninstalls specific torchaudio and torchdata versions. ```python !pip install synthcity !pip install plotly !pip uninstall -y torchaudio torchdata ``` -------------------------------- ### Get and Train Sequence Model Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial10_sequential_synthesis.ipynb Instantiate the 'syn_seq' plugin and train it using the prepared data loader. ```python syn_model = Plugins().get("syn_seq") ``` ```python syn_model.fit(loader) ``` -------------------------------- ### Install Synthcity and Uninstall Conflicting Packages Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial3_survival_analysis.ipynb Installs the Synthcity library and removes potentially conflicting packages like torchaudio and torchdata. ```python !pip install synthcity !pip uninstall -y torchaudio torchdata ``` -------------------------------- ### Generate Data with Custom Constraints Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial1_add_a_new_plugin.ipynb This example illustrates how to generate synthetic data while applying custom constraints, ensuring specific conditions are met in the generated data. The 'Constraints' class is used to define rules, and an assertion verifies the constraint. ```python # Custom generation constraints # synthcity absolute from synthcity.plugins.core.constraints import Constraints constraints = Constraints(rules=[("worst radius", ">", 15)]) generated = gen.generate(count=10, constraints=constraints) assert (generated["worst radius"] > 15).any() generated.dataframe() ``` -------------------------------- ### SurvivalAnalysisDataLoader Setup Source: https://context7.com/vanderschaarlab/synthcity/llms.txt Load and manage survival analysis data using SurvivalAnalysisDataLoader. Specify target, time-to-event, and sensitive features. Unpack data into features, time, and event indicators. ```python from lifelines.datasets import load_rossi from synthcity.plugins.core.dataloader import SurvivalAnalysisDataLoader from synthcity.plugins import Plugins # Load survival dataset (recidivism data) data = load_rossi() # Create survival data loader loader = SurvivalAnalysisDataLoader( data, target_column="arrest", # Event indicator (1=event, 0=censored) time_to_event_column="week", # Time to event/censoring sensitive_features=["race"], train_size=0.8 ) ``` ```python X, T, E = loader.unpack() # X: features, T: time-to-event, E: event indicator print(f"Features shape: {X.shape}") print(f"Events: {E.sum()} / {len(E)}") ``` ```python # Train survival-specific generator survival_gan = Plugins(categories=["survival_analysis"]).get("survival_gan") survival_gan.fit(loader) ``` ```python # Generate synthetic survival data synthetic_survival = survival_gan.generate(count=100) X_syn, T_syn, E_syn = synthetic_survival.unpack() ``` -------------------------------- ### Initialize Synthcity Logger Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_great.ipynb Configures the Synthcity logger to output debug information to standard error. This setup is typically done at the beginning of a script. ```python # stdlib import warnings import sys warnings.filterwarnings("ignore") import synthcity.logger as log log.add(sink=sys.stderr, level="DEBUG") # third party from sklearn.datasets import load_iris, load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "great" ``` -------------------------------- ### Load a Generator Plugin with Custom Parameters Source: https://context7.com/vanderschaarlab/synthcity/llms.txt Instantiate a specific generative model using Plugins().get(). Custom hyperparameters can be provided to configure the model's architecture, training, and generation process. For example, CTGAN can be configured with specific layer sizes, learning rates, and batch sizes. ```python from synthcity.plugins import Plugins # Load CTGAN with custom parameters ctgan = Plugins().get( "ctgan", n_iter=2000, # Training iterations generator_n_layers_hidden=2, # Generator hidden layers generator_n_units_hidden=500, # Units per hidden layer discriminator_n_layers_hidden=2, # Discriminator hidden layers batch_size=200, # Training batch size lr=1e-3, # Learning rate encoder_max_clusters=10, # Max clusters for continuous encoding patience=5, # Early stopping patience device="cuda" # Use GPU if available ) # Load TVAE (Tabular VAE) tvae = Plugins().get( "tvae", n_iter=1000, n_units_embedding=128, n_units_latent=128, batch_size=500 ) # Load differentially private generator dpgan = Plugins().get( "dpgan", n_iter=1000, epsilon=1.0, # Privacy budget delta=1e-5 # Privacy parameter ) ``` -------------------------------- ### Initialize Image Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/images/plugin_image_cgan.ipynb Sets up the environment and selects the image generation plugin. ```python # stdlib import warnings warnings.filterwarnings("ignore") # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "image_cgan" ``` -------------------------------- ### Initialize Sequential Synthesis Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial10_sequential_synthesis.ipynb Import necessary modules and set up the plugin identifier. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import Syn_SeqDataLoader eval_plugin = "syn_seq" ``` -------------------------------- ### Initialize environment and imports Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial4_time_series.ipynb Configure logging and import necessary modules for time-series survival analysis. ```python # stdlib import sys import warnings # synthcity absolute import synthcity.logger as log from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import TimeSeriesSurvivalDataLoader log.add(sink=sys.stderr, level="INFO") warnings.filterwarnings("ignore") ``` -------------------------------- ### Initialize PATE-GAN Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/privacy/plugin_pategan.ipynb Imports necessary libraries and sets up the plugin identifier. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "pategan" ``` -------------------------------- ### Initialize Synthcity Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_nflow.ipynb Sets up the environment by suppressing warnings and importing necessary libraries. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "nflow" ``` -------------------------------- ### Initialize DPGAN Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/privacy/plugin_dpgan.ipynb Imports necessary libraries and sets up the plugin identifier for the DPGAN model. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "dpgan" ``` -------------------------------- ### Plot generated data Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/time_series/plugin_ctgan(generic).ipynb Visualizes the generated time-series data using a t-SNE plot. Requires matplotlib to be installed. ```python # plot # third party import matplotlib.pyplot as plt syn_model.plot(plt, loader, count=1000, plots=["tsne"]) plt.show() ``` -------------------------------- ### Initialize Synthcity Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial6_time_series_data_preparation.ipynb Configure logging and import necessary modules for time series data loading. ```python # stdlib import sys import warnings # synthcity absolute import synthcity.logger as log from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import TimeSeriesDataLoader log.add(sink=sys.stderr, level="INFO") warnings.filterwarnings("ignore") ``` -------------------------------- ### Evaluate model performance Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/time_series/plugin_ctgan(generic).ipynb Evaluates the performance of the trained CTGAN model using specified metrics. The 'detection_mlp' metric is used here as an example. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [ (f"eval_plugin_{eval_plugin}", eval_plugin, {"n_iter": 50}) ], # REPLACE {"n_iter" : 50}) with {} for better performance loader, task_type="time_series", repeats=2, metrics={"detection": ["detection_mlp"]}, # DELETE THIS LINE FOR ALL METRICS ) ``` -------------------------------- ### Initialize and Fit a Synthcity Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_syn_seq.ipynb Load a specific plugin from the registry and train it using a data loader. ```python from synthcity.plugins import Plugins syn_model = Plugins().get(eval_plugin) syn_model.fit(loader) ``` -------------------------------- ### Generate Image Data Source: https://github.com/vanderschaarlab/synthcity/blob/main/README.md Generates synthetic image data using the ImageCGAN model. This example uses the MNIST dataset and requires torchvision. ```python from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import ImageDataLoader from torchvision import datasets dataset = datasets.MNIST(".", download=True) loader = ImageDataLoader(dataset).sample(100) syn_model = Plugins().get("image_cgan") syn_model.fit(loader) syn_img, syn_labels = syn_model.generate(count=10).unpack().numpy() print(syn_img.shape) ``` -------------------------------- ### Initialize Bayesian Network Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_bayesian_network.ipynb Imports necessary libraries and sets up the plugin identifier. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "bayesian_network" ``` -------------------------------- ### Initialize Environment and Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/privacy/plugin_adsgan.ipynb Imports necessary libraries and sets the plugin identifier for ADS-GAN. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "adsgan" ``` -------------------------------- ### Generate Survival Analysis Data Source: https://github.com/vanderschaarlab/synthcity/blob/main/README.md Generates synthetic survival analysis data using a specified model after fitting it to a DataLoader. Ensure lifelines is installed for data loading. ```python from lifelines.datasets import load_rossi from synthcity.plugins.core.dataloader import SurvivalAnalysisDataLoader from synthcity.plugins import Plugins X = load_rossi() data = SurvivalAnalysisDataLoader( X, target_column="arrest", time_to_event_column="week", ) syn_model = Plugins().get("survival_gan") syn_model.fit(data) syn_model.generate(count=10) ``` -------------------------------- ### Generate Time Series Data Source: https://github.com/vanderschaarlab/synthcity/blob/main/README.md Generates synthetic time series data using the TimeGAN model. This example uses Google Stocks data and requires synthcity.utils.datasets. ```python # synthcity absolute from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import TimeSeriesDataLoader from synthcity.utils.datasets.time_series.google_stocks import GoogleStocksDataloader static_data, temporal_data, horizons, outcome = GoogleStocksDataloader().load() data = TimeSeriesDataLoader( temporal_data=temporal_data, observation_times=horizons, static_data=static_data, outcome=outcome, ) syn_model = Plugins().get("timegan") syn_model.fit(data) syn_model.generate(count=10) ``` -------------------------------- ### Instantiate and Configure a Synthcity Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_great.ipynb Retrieves a Synthcity plugin named 'great' with specified parameters for batch size, number of iterations, language model, and experiment directory. Includes custom training arguments for saving checkpoints. ```python # synthcity absolute from synthcity.plugins import Plugins syn_model = Plugins().get( eval_plugin, batch_size=8, n_iter=100, llm="distilgpt2", experiment_dir="trainer_great", train_kwargs={"save_steps": 40000}, ) ``` -------------------------------- ### Initialize AIM Plugin Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/privacy/plugin_aim.ipynb Imports necessary libraries and configures the logger for the AIM plugin. ```python # stdlib import warnings import sys warnings.filterwarnings("ignore") # synthcity absolute from synthcity.plugins import Plugins from synthcity.utils.datasets.categorical.categorical_adult import CategoricalAdultDataloader import synthcity.logger as log log.add(sink=sys.stderr, level="INFO") eval_plugin = "aim" ``` -------------------------------- ### Evaluate Synthetic Data Generation Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_uniform_sampler.ipynb Evaluates the performance of the synthetic data generation model using Synthcity's benchmark tools. This example specifically evaluates the 'detection' metric. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [(eval_plugin, eval_plugin, {})], # (testname, plugin, plugin_args). loader, repeats=2, metrics={"detection": ["detection_mlp"]}, # DELETE THIS LINE FOR ALL METRICS ) ``` -------------------------------- ### Load Data and Initialize DataLoader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial10_sequential_synthesis.ipynb Prepare the diabetes dataset for sequential synthesis. ```python # synthcity absolute from synthcity.plugins.core.dataloader import GenericDataLoader X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y loader = Syn_SeqDataLoader(X, target_column="target", sensitive_columns=["sex"]) loader.dataframe() ``` -------------------------------- ### Visualize Synthetic Data Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_great.ipynb Generates a plot visualizing the synthetic data against the original data using matplotlib. This helps in assessing the quality and similarity of the generated data. Requires matplotlib to be installed. ```python # third party import matplotlib.pyplot as plt syn_model.plot(plt, loader, count=100, max_length=1000) plt.show() ``` -------------------------------- ### Generate Data with Array Condition Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_ddpm.ipynb When an array is used as the `cond` argument during fitting, it must also be provided to the `generate` method. This example shows generating data based on a provided NumPy array. ```python outcome = np.array([3, 4, 5, 6, 7, 8, 9]) plugin.generate(len(outcome), cond=outcome) ``` -------------------------------- ### Create and run Optuna study Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial8_hyperparameter_optimization.ipynb Creates an Optuna study with a 'minimize' direction and optimizes it for 2 trials using the defined objective function. Displays the best parameters found. ```python study = optuna.create_study(direction="minimize") study.optimize(objective, n_trials=2) study.best_params ``` -------------------------------- ### Train a Synthetic Data Generator Source: https://context7.com/vanderschaarlab/synthcity/llms.txt The fit() method trains a synthetic data generator on the provided dataset. It automatically handles internal encoding and schema validation. This example shows preparing data and initializing the loader before fitting. ```python from sklearn.datasets import load_iris from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import GenericDataLoader # Prepare data X, y = load_iris(as_frame=True, return_X_y=True) X["target"] = y loader = GenericDataLoader(X, target_column="target") # Load a generator and fit it to the data # generator = Plugins().get("ctgan") # generator.fit(loader) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial8_hyperparameter_optimization.ipynb Imports standard libraries, optuna, scikit-learn for data loading, and synthcity components for logging, plugins, and data loading. ```python # stdlib import sys import warnings # third party import optuna from sklearn.datasets import load_diabetes # synthcity absolute import synthcity.logger as log from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import GenericDataLoader log.add(sink=sys.stderr, level="INFO") warnings.filterwarnings("ignore") ``` -------------------------------- ### Suggest hyperparameters using Optuna Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial8_hyperparameter_optimization.ipynb Uses the synthcity utility function `suggest_all` to sample hyperparameters from the plugin's space using an Optuna trial. Sets 'n_iter' to 100 for faster execution. ```python from synthcity.utils.optuna_sample import suggest_all trial = optuna.create_study().ask() params = suggest_all(trial, plugin_cls.hyperparameter_space()) params['n_iter'] = 100 # speed up params ``` -------------------------------- ### Run Pre-commit Hooks Source: https://github.com/vanderschaarlab/synthcity/blob/main/CONTRIBUTING.MD Verifies that the local environment is correctly configured to match project code style. ```bash pre-commit run --all ``` -------------------------------- ### Evaluate Synthcity Model Performance Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_great.ipynb Evaluates the performance of the Synthcity model using the Benchmarks module. It specifies the plugin to test, the data loader, the number of repeats for evaluation, and the metrics to compute. Note: 'detection_mlp' is used here as an example metric. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [ (eval_plugin, eval_plugin, {}) ], # (testname, plugin, plugin_args) The plugin_args are given are simply to illustrate some of the paramters that can be passed to the plugin loader, repeats=2, metrics={"detection": ["detection_mlp"]}, # DELETE THIS LINE FOR ALL METRICS ) ``` -------------------------------- ### Initialize FourierFlows Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/time_series/plugin_fourier_flows.ipynb Imports necessary modules and sets the plugin identifier for FourierFlows. ```python # stdlib import warnings # synthcity absolute from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import TimeSeriesDataLoader from synthcity.utils.datasets.time_series.google_stocks import GoogleStocksDataloader warnings.filterwarnings("ignore") eval_plugin = "fflows" ``` -------------------------------- ### Load and prepare survival data Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial4_time_series.ipynb Load the PBC dataset and initialize a TimeSeriesSurvivalDataLoader. ```python import numpy as np from synthcity.utils.datasets.time_series.pbc import PBCDataloader ( static_surv, temporal_surv, temporal_surv_horizons, outcome_surv, ) = PBCDataloader().load() T, E = outcome_surv horizons = [0.25, 0.5, 0.75] time_horizons = np.quantile(T, horizons).tolist() loader = TimeSeriesSurvivalDataLoader( temporal_data=temporal_surv, observation_times=temporal_surv_horizons, static_data=static_surv, T=T, E=E, time_horizons=time_horizons, ) loader.dataframe() ``` -------------------------------- ### Register and List SDV CTGAN Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial1_add_a_new_plugin.ipynb This snippet shows how to add the 'sdv_ctgan' plugin to the generators and then list all available generators. ```python generators.add("sdv_ctgan", sdv_ctgan_plugin) generators.list() ``` -------------------------------- ### Load and prepare time-series data Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/time_series/plugin_ctgan(generic).ipynb Loads the Google Stocks dataset and prepares it into a TimeSeriesDataLoader format for Synthcity. ```python static_data, temporal_data, horizons, outcome = GoogleStocksDataloader().load() loader = TimeSeriesDataLoader( temporal_data=temporal_data, observation_times=horizons, static_data=static_data, outcome=outcome, ) loader.dataframe() ``` -------------------------------- ### Load and Use TimeSeriesDataLoader Source: https://context7.com/vanderschaarlab/synthcity/llms.txt Demonstrates loading time series data using GoogleStocksDataloader and preparing it with TimeSeriesDataLoader. Includes training a TimeGAN model and generating synthetic data. ```python from synthcity.plugins.core.dataloader import TimeSeriesDataLoader from synthcity.utils.datasets.time_series.google_stocks import GoogleStocksDataloader from synthcity.plugins import Plugins import pandas as pd import numpy as np # Load example time series data static_data, temporal_data, horizons, outcome = GoogleStocksDataloader().load() # Create time series data loader loader = TimeSeriesDataLoader( temporal_data=temporal_data, # List of DataFrames (one per subject) observation_times=horizons, # Time points for each subject static_data=static_data, # Static features per subject outcome=outcome, # Outcome/label per subject train_size=0.8 ) # Access data components print(f"Number of subjects: {len(loader)}") print(f"Static features: {loader.static_features}") print(f"Temporal features: {loader.temporal_features}") # Unpack time series data static, temporal, times, outcomes = loader.unpack() # Train time series generator timegan = Plugins(categories=["time_series"]).get("timegan") timegan.fit(loader) # Generate synthetic time series synthetic_ts = timegan.generate(count=50) static_syn, temporal_syn, times_syn, outcome_syn = synthetic_ts.unpack() ``` -------------------------------- ### Load data for benchmarking Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial2_benchmarks.ipynb Initialize a GenericDataLoader with the iris dataset for model training. ```python # stdlib import sys import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_iris # synthcity absolute import synthcity.logger as log from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import GenericDataLoader X, y = load_iris(return_X_y=True, as_frame=True) X["target"] = y loader = GenericDataLoader(X, target_column="target", sensitive_columns=[]) loader.dataframe() ``` -------------------------------- ### Define and Configure DDPM Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_ddpm.ipynb Configure and initialize the DDPM plugin with specified hyperparameters for training. Ensure the loader is properly prepared before fitting. ```python plugin_params.update( is_classification = False, n_iter = 500, # epochs lr = 5e-4, weight_decay = 1e-4, batch_size = 1250, model_params = dict( n_layers_hidden = 3, n_units_hidden = 256, dropout = 0.0, ), num_timesteps = 100, # timesteps in diffusion ) plugin = Plugins().get("ddpm", **plugin_params) plugin.fit(loader) ``` -------------------------------- ### Run Tests Source: https://github.com/vanderschaarlab/synthcity/blob/main/CONTRIBUTING.MD Commands for executing the test suite, including options for skipping slow tests or targeting specific plugins. ```bash pytest -vvvsx -m "not slow" --durations=50 ``` ```bash pytest -vvvs --durations=50 ``` ```bash pytest -vvvs -k goggle --durations=50 ``` -------------------------------- ### Initialize and train CTGAN model Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/time_series/plugin_ctgan(generic).ipynb Initializes the CTGAN plugin from Synthcity and trains it on the prepared time-series data. ```python # synthcity absolute from synthcity.plugins import Plugins syn_model = Plugins().get(eval_plugin, n_iter=50) syn_model.fit(loader) ``` -------------------------------- ### Train SDV CTGAN Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial1_add_a_new_plugin.ipynb This code demonstrates how to instantiate the 'sdv_ctgan' generator with custom parameters and then fit it to a DataLoader. ```python # Train the new plugin gen = generators.get("sdv_ctgan", n_iter=100) gen.fit(loader) ``` -------------------------------- ### Plugin Serialization and Deserialization Source: https://context7.com/vanderschaarlab/synthcity/llms.txt Demonstrates how to save and load trained Synthcity plugins using built-in serialization methods. ```python from synthcity.plugins import load, save_to_file, load_from_file # Assuming 'plugin' is a trained Synthcity plugin object # and 'model_bytes' is its serialized representation # Deserialize from bytes loaded_plugin = load(model_bytes) assert loaded_plugin.name() == plugin.name() # Generate with loaded model synthetic = loaded_plugin.generate(count=50) # Save to file save_to_file("./trained_ctgan.pkl", plugin) # Load from file reloaded = load_from_file("./trained_ctgan.pkl") synthetic_reloaded = reloaded.generate(count=50) # Using Plugin's built-in serialization buff = plugin.save() restored = Plugins().load(buff) ``` -------------------------------- ### List Available Synthcity Plugins Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial10_sequential_synthesis.ipynb Retrieve and list all available plugins within the Synthcity framework. ```python # synthcity absolute from synthcity.plugins import Plugins generators = Plugins() generators.list() ``` -------------------------------- ### Highlight benchmark scores Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial2_benchmarks.ipynb Visualize the benchmark results to identify top-performing plugins. ```python Benchmarks.highlight(score) ``` -------------------------------- ### Create Conda Environment Source: https://github.com/vanderschaarlab/synthcity/blob/main/CONTRIBUTING.MD Initializes a new Python 3.9 environment for Synthcity development. ```bash conda create -n your-synthcity-env python=3.9 conda activate your-synthcity-env ``` -------------------------------- ### Benchmark Multiple Generators with Benchmarks.evaluate() Source: https://context7.com/vanderschaarlab/synthcity/llms.txt Demonstrates how to benchmark multiple synthetic data generators systematically using Benchmarks.evaluate(). This includes defining tests, specifying metrics, and running multiple repeats for statistical significance. Results can be cached. ```python from synthcity.benchmark import Benchmarks from synthcity.plugins.core.dataloader import GenericDataLoader from sklearn.datasets import load_diabetes from pathlib import Path # Prepare data X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y loader = GenericDataLoader(X, target_column="target", sensitive_columns=["sex"]) # Define tests: (test_name, plugin_name, plugin_kwargs) tests = [ ("ctgan_default", "ctgan", {}), ("ctgan_custom", "ctgan", {"n_iter": 500, "batch_size": 100}), ("tvae", "tvae", {"n_iter": 500}), ("bayesian_network", "bayesian_network", {}), ] # Run comprehensive benchmark results = Benchmarks.evaluate( tests=tests, X=loader, metrics={ "sanity": ["data_mismatch", "common_rows_proportion"], "stats": ["jensenshannon_dist", "max_mean_discrepancy"], "performance": ["linear_model"], "detection": ["detection_xgb"], }, repeats=3, # Statistical repeats synthetic_size=1000, # Samples to generate task_type="regression", workspace=Path("./benchmark_cache"), # Cache results ) # Print formatted results Benchmarks.print(results) # Highlight best/worst performers Benchmarks.highlight(results) ``` -------------------------------- ### Instantiate AIM Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/privacy/plugin_aim.ipynb Retrieves the AIM plugin instance from the Synthcity plugin registry. ```python # synthcity absolute from synthcity.plugins import Plugins syn_model = Plugins().get(eval_plugin) ``` -------------------------------- ### Create a GenericDataLoader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial8_hyperparameter_optimization.ipynb Initializes a GenericDataLoader with the dataset, specifying the target and sensitive columns, and then splits it into training and testing sets. ```python loader = GenericDataLoader( X, target_column="target", sensitive_columns=["sex"], ) train_loader, test_loader = loader.train(), loader.test() ``` -------------------------------- ### Configure and Train DDPM Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_ddpm.ipynb Initializes the DDPM plugin with specific hyperparameters and fits it to the provided data loader. ```python # define the model hyper-parameters plugin_params = dict( is_classification = True, n_iter = 1000, # epochs lr = 0.002, weight_decay = 1e-4, batch_size = 1000, model_type = "mlp", # or "resnet" model_params = dict( n_layers_hidden = 3, n_units_hidden = 256, dropout = 0.0, ), num_timesteps = 500, # timesteps in diffusion dim_embed = 128, # performance logging log_interval = 10, print_interval = 100, ) plugin = Plugins().get("ddpm", **plugin_params) plugin.fit(loader) ``` -------------------------------- ### Load and Prepare Dataset Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_uniform_sampler.ipynb Loads the diabetes dataset and prepares it for use with Synthcity. Ensure the 'target' column is present and sensitive columns are identified. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "uniform_sampler" ``` ```python # synthcity absolute from synthcity.plugins.core.dataloader import GenericDataLoader X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y loader = GenericDataLoader(X, target_column="target", sensitive_columns=["sex"]) loader.dataframe() ``` -------------------------------- ### Load and Describe Regression Dataset Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_ddpm.ipynb Loads a regression dataset from a CSV file using pandas and initializes a GenericDataLoader. The describe() method provides summary statistics for the dataset. ```python import pandas as pd df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/wine-quality-white.csv", sep=";") loader = GenericDataLoader(df, target_column="quality", sensitive_columns=[]) loader.dataframe().describe() ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/privacy/plugin_privbayes.ipynb Imports standard libraries and Synthcity plugins. It also sets up a warning filter. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "privbayes" ``` -------------------------------- ### Instantiate and Train Synthetic Model Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_arf.ipynb Retrieve a plugin model by name and fit it to the loaded data. ```python # synthcity absolute from synthcity.plugins import Plugins syn_model = Plugins().get(eval_plugin) ``` ```python syn_model.fit(loader) ``` -------------------------------- ### Benchmark Synthcity Plugins Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_nflow.ipynb Evaluates plugin performance using the Benchmarks utility and prints the results. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [ (eval_plugin, eval_plugin, {"n_iter": 50}) ], # (testname, plugin, plugin_args). REPLACE {"n_iter" : 50} with {} for better performance loader, repeats=2, metrics={"detection": ["detection_mlp"]}, # DELETE THIS LINE FOR ALL METRICS ) ``` ```python Benchmarks.print(score) ``` -------------------------------- ### Import Libraries and Load Data Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_dummy_sampler.ipynb Imports necessary libraries and loads the diabetes dataset. Sets up warnings and defines the evaluation plugin. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "dummy_sampler" ``` -------------------------------- ### Benchmark Plugins Source: https://github.com/vanderschaarlab/synthcity/blob/main/docs/README.md Evaluate the quality of multiple plugins using the Benchmarks utility. ```python # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.benchmark import Benchmarks from synthcity.plugins.core.constraints import Constraints from synthcity.plugins.core.dataloader import GenericDataLoader X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y loader = GenericDataLoader(X, target_column="target", sensitive_columns=["sex"]) score = Benchmarks.evaluate( [ (f"example_{model}", model, {}) # testname, plugin name, plugin args for model in ["adsgan", "ctgan", "tvae"] ], loader, synthetic_size=1000, metrics={"performance": ["linear_model"]}, repeats=3, ) Benchmarks.print(score) ``` -------------------------------- ### Benchmark Model Performance Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_bayesian_network.ipynb Evaluates the plugin using the Benchmarks utility and prints the resulting scores. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [(eval_plugin, eval_plugin, {})], # (testname, plugin, plugin_args) loader, repeats=2, metrics={"detection": ["detection_mlp"]}, # DELETE THIS LINE FOR ALL METRICS ) ``` ```python Benchmarks.print(score) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial9_dealing_with_missing_data.ipynb Imports essential libraries including numpy, pandas, scikit-learn for data loading, HyperImpute for simulating NaNs, and Synthcity's DataLoader. ```python import sys import warnings import numpy as np import pandas as pd from sklearn.datasets import load_diabetes from hyperimpute.plugins.utils.simulate import simulate_nan from IPython.display import display if not sys.warnoptions: warnings.simplefilter("ignore") from synthcity.plugins.core.dataloader import GenericDataLoader ``` -------------------------------- ### Benchmark Synthetic Plugins Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_arf.ipynb Evaluate plugin performance using the Benchmarks suite and print the resulting scores. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [ (eval_plugin, eval_plugin, {"num_trees": 100, "delta": 0, "max_iters":15, "early_stop": True, "verbose": True, "min_node_size": 3}) ], # (testname, plugin, plugin_args) The plugin_args are given are simply to illustrate some of the paramters that can be passed to the plugin loader, repeats=2, metrics={"detection": ["detection_mlp"]}, # DELETE THIS LINE FOR ALL METRICS ) ``` ```python Benchmarks.print(score) ``` -------------------------------- ### Initialize Sequence Data Loader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial10_sequential_synthesis.ipynb Create a Syn_SeqDataLoader instance with processed data, custom user settings, and specified target/sensitive columns. ```python loader = Syn_SeqDataLoader(X_processed, user_custom=user_custom, target_column="income>50K", sensitive_columns=["sex", "race"]) loader.dataframe().head() ``` -------------------------------- ### Import Libraries and Load Data Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_ctgan.ipynb Imports necessary libraries and loads the diabetes dataset. It also sets up a warning filter. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "ctgan" ``` -------------------------------- ### List Available Generative Models in Synthcity Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial0_basic_examples.ipynb Lists all available generative models within the synthcity library. This helps in choosing a model for data generation. ```python # synthcity absolute from synthcity.plugins import Plugins Plugins().list() ``` -------------------------------- ### Benchmark AIM Plugin Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/privacy/plugin_aim.ipynb Evaluates the plugin performance using specified metrics and prints the results. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [ (eval_plugin, eval_plugin, {"epsilon": 1.0, "delta": 1e-7, "max_model_size": 80, "degree": 2, "num_marginals": None, "max_cells": 1000}), ], # (testname, plugin, plugin_args) The plugin_args are given are simply to illustrate some of the paramters that can be passed to the plugin loader, repeats=2, metrics={ "detection": ["detection_mlp"], "privacy": ["distinct l-diversity", "k-anonymization", "k-map"], }, ) ``` ```python Benchmarks.print(score) ``` -------------------------------- ### Load and prepare time series data Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/time_series/plugin_timegan.ipynb Loads the Google Stocks dataset and prepares it into a TimeSeriesDataLoader object for use with Synthcity models. This includes temporal, static, and outcome data. ```python # Load data static_data, temporal_data, horizons, outcome = GoogleStocksDataloader().load() loader = TimeSeriesDataLoader( temporal_data=temporal_data, observation_times=horizons, static_data=static_data, outcome=outcome, ) loader.dataframe() ``` -------------------------------- ### Load MNIST dataset and create ImageDataLoader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/images/plugin_image_adsgan.ipynb Loads the MNIST dataset and prepares it for use with Synthcity's ImageDataLoader. The loader is then sampled to create a dataset of 1000 images. ```python # third party from torchvision import datasets, transforms # synthcity absolute from synthcity.plugins.core.dataloader import ImageDataLoader IMG_SIZE = 32 dataset = datasets.MNIST(".", download=True) loader = ImageDataLoader( dataset, height=IMG_SIZE, ).sample(1000) loader.shape ``` -------------------------------- ### Benchmark DP models for a fixed epsilon Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial5_differential_privacy.ipynb Compare different DP models like Pate-GAN and DP-GAN at a specific epsilon value. This helps in selecting the best model for a given privacy-utility trade-off. Configure synthetic_size and repeats as needed. ```python from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [ (f"test_{model}", model, {"epsilon": 0.1}) for model in ["pategan", "dpgan"] ], loader, synthetic_size=1000, repeats=2, synthetic_reuse_if_exists=False, ) ``` -------------------------------- ### Visualize Sample Images from MedNIST Dataset Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial7_image_generation_using_mednist.ipynb Displays a 3x3 grid of randomly selected images from the MedNIST dataset with their corresponding class labels. ```python # third party import matplotlib.pyplot as plt import numpy as np plt.subplots(3, 3, figsize=(8, 8)) for i, k in enumerate(np.random.randint(num_total, size=9)): im = PIL.Image.open(image_files_list[k]) arr = np.array(im) plt.subplot(3, 3, i + 1) plt.xlabel(class_names[image_class[k]]) plt.imshow(arr, cmap="gray", vmin=0, vmax=255) plt.tight_layout() plt.show() ``` -------------------------------- ### Create a GenericDataLoader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_ctgan.ipynb Prepares the loaded data into a GenericDataLoader format, specifying the target and sensitive columns. This is a prerequisite for training Synthcity models. ```python # synthcity absolute from synthcity.plugins.core.dataloader import GenericDataLoader X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y loader = GenericDataLoader(X, target_column="target", sensitive_columns=["sex"]) loader.dataframe() ``` -------------------------------- ### Initialize and train the image GAN model Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/images/plugin_image_adsgan.ipynb Initializes the image GAN plugin and trains it on the loaded dataset. The `plot_progress` and `n_iter` parameters control the training process. ```python # synthcity absolute from synthcity.plugins import Plugins syn_model = Plugins().get(eval_plugin, batch_size=100, plot_progress=True, n_iter=100) syn_model.fit(loader) ``` -------------------------------- ### Benchmark DP-GAN for various epsilons Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial5_differential_privacy.ipynb Use the Benchmarks.evaluate function to test DP-GAN with different epsilon values. Ensure the loader and synthetic_size are configured appropriately. Set synthetic_reuse_if_exists to False to force regeneration. ```python from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [(f"test_eps_{eps}", "dpgan", {"epsilon": eps}) for eps in [0.1, 1, 10]], loader, synthetic_size=1000, repeats=2, synthetic_reuse_if_exists=False ) ``` -------------------------------- ### Evaluate plugin with suggested hyperparameters Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial8_hyperparameter_optimization.ipynb Fits the plugin with the suggested hyperparameters and evaluates its performance using Benchmarks.evaluate. The evaluation is limited to 'detection_mlp' for speed. ```python from synthcity.benchmark import Benchmarks plugin = plugin_cls(**params).fit(train_loader) report = Benchmarks.evaluate( [("trial", PLUGIN, params)], train_loader, # Benchmarks.evaluate will split out a validation set repeats=1, metrics={"detection": ["detection_mlp"]}, # DELETE THIS LINE FOR ALL METRICS ) report['trial'] ``` -------------------------------- ### Load Diabetes Dataset and Initialize DataLoader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial0_basic_examples.ipynb Loads the diabetes dataset from scikit-learn and initializes a GenericDataLoader. Preprocessing like OneHotEncoder or StandardScaler is not needed as Synthcity handles it internally. ```python # stdlib import sys import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute import synthcity.logger as log from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import GenericDataLoader log.add(sink=sys.stderr, level="INFO") X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y X ``` ```python # Preprocessing data with OneHotEncoder or StandardScaler is not needed or recommended. Synthcity handles feature encoding and standardization internally. loader = GenericDataLoader( X, target_column="target", sensitive_columns=["sex"], ) ``` -------------------------------- ### Preprocess Data for Sequential Synthesis Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial10_sequential_synthesis.ipynb Configure data types and special values to improve synthesis quality. ```python # synthcity absolute from synthcity.plugins.core.models.syn_seq.syn_seq_preprocess import SynSeqPreprocessor prep = SynSeqPreprocessor( user_dtypes={ "workclass": "category", "occupation": "category", "relationship": "category", "native-country": "category", "race": "category", "martial-status": "category", "sex": "category", "income>50K": "category", }, user_special_values={ "capital-gain": [0], "capital-loss": [0] }, max_categories=15 ) # 2) Preprocess (date -> offset, numeric split 등) X_processed = prep.preprocess(X, oversample=True) ``` -------------------------------- ### Load Diabetes Dataset and Initialize DataLoader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial5_differential_privacy.ipynb Loads the diabetes dataset using scikit-learn and prepares it for Synthcity using GenericDataLoader. Sensitive columns like 'sex' are specified, and the target column is set. Note that Synthcity handles feature encoding and standardization internally. ```python # stdlib import sys import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute import synthcity.logger as log from synthcity.plugins import Plugins from synthcity.plugins.core.dataloader import GenericDataLoader log.add(sink=sys.stderr, level="INFO") X, y = load_diabetes(return_X_y=True, as_frame=True) X["target"] = y X ``` ```python # Note: preprocessing data with OneHotEncoder or StandardScaler is not needed or recommended. Synthcity handles feature encoding and standardization internally. loader = GenericDataLoader( X, target_column="target", sensitive_columns=["sex"], ) ``` -------------------------------- ### Prepare MedNIST Dataset for Synthcity Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial7_image_generation_using_mednist.ipynb Loads image file paths and labels from the MedNIST dataset, limiting samples per class and printing dataset information. ```python LIMIT = 1000 # samples per class class_names = sorted( x for x in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, x)) ) num_class = len(class_names) image_files = [ [ os.path.join(data_dir, class_names[i], x) for x in os.listdir(os.path.join(data_dir, class_names[i])) ] for i in range(num_class) ] num_each = [len(image_files[i]) for i in range(num_class)] image_files_list = [] image_class = [] for i in range(num_class): image_files_list.extend(image_files[i][:LIMIT]) image_class.extend([i] * min(num_each[i], LIMIT)) num_total = len(image_class) image_width, image_height = PIL.Image.open(image_files_list[0]).size print(f"Total image count: {num_total}") print(f"Image dimensions: {image_width} x {image_height}") print(f"Label names: {class_names}") print(f"Label counts: {num_each}") ``` -------------------------------- ### Print Benchmark Results Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_great.ipynb Prints the evaluation scores obtained from the Synthcity benchmark to the console in a formatted way. ```python Benchmarks.print(score) ``` -------------------------------- ### Train Synthcity Model Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_great.ipynb Trains the instantiated Synthcity model using the provided data loader. This step learns the data distribution to generate synthetic samples. ```python syn_model.fit(loader) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/plugins/generic/plugin_tvae.ipynb Imports required libraries for data loading and Synthcity plugins. It also sets up warning filters. ```python # stdlib import warnings warnings.filterwarnings("ignore") # third party from sklearn.datasets import load_diabetes # synthcity absolute from synthcity.plugins import Plugins eval_plugin = "tvae" ``` -------------------------------- ### Load reference data using GenericDataLoader Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial1_add_a_new_plugin.ipynb Loads the breast cancer dataset from scikit-learn and wraps it using Synthcity's GenericDataLoader for use with plugins. ```python # Load reference data # third party from sklearn.datasets import load_breast_cancer X, y = load_breast_cancer(return_X_y=True, as_frame=True) loader = GenericDataLoader(X) loader.dataframe() ``` -------------------------------- ### Evaluate synthetic data plugins Source: https://github.com/vanderschaarlab/synthcity/blob/main/tutorials/tutorial2_benchmarks.ipynb Use the Benchmarks.evaluate method to assess synthetic data generators against a provided loader. ```python # synthcity absolute from synthcity.benchmark import Benchmarks score = Benchmarks.evaluate( [("uniform_sampler", "uniform_sampler", {})], loader, synthetic_size=len(X), repeats=1, ) ```