Skip to main content

A package for measuring the sim-to-real gap in microscopy image simulations

Project description

Sim2RealGap

A Python package for measuring the simulation-to-real gap in microscopy image simulations.

Sim2RealGap provides tools to quantitatively and visually compare real microscopy images against simulated ones, using a range of feature extraction and statistical comparison methods.

Installation

pip install sim2realgap

Or install from source:

git clone https://github.com/Qjs209/Sim2RealGap
cd Sim2RealGap
pip install -e .

Features

  • Per-cell feature extraction (area, mean intensity, texture/std)
  • Pixel-level distribution comparison
  • GMM fitting and KL divergence (2D, 1D)
  • HOG feature extraction and visualization
  • SIFT keypoint extraction and visualization
  • Histogram and KL matrix plotting

Quick Start

import sim2realgap as s2r
import numpy as np

# Load real data
real_images = s2r.load_video("path/to/real/frames")
real_masks  = s2r.load_video("path/to/real/masks")

# Load simulated data (any numpy array of shape (n_frames, H, W))
sim_images = np.load("path/to/sim_images.npy")
sim_masks  = np.load("path/to/sim_masks.npy")

frames = [0, 1, 2]

# Extract features
areas_real, intensities_real = s2r.collect_features(real_images, real_masks, frames)
areas_sim,  intensities_sim  = s2r.collect_features(sim_images,  sim_masks,  frames)

# Fit GMMs and compute KL divergence
gmm_real = s2r.fit_gmm(areas_real, intensities_real)
gmm_sim  = s2r.fit_gmm(areas_sim,  intensities_sim)

kl = s2r.kl_gmm(gmm_real, gmm_sim)
print(f"KL(real || sim) = {kl:.2f}")

# Plot
s2r.plot_histograms(
    {"Real": areas_real, "Simulated": areas_sim},
    title="Cell area distribution",
    xlabel="Area"
)

API Reference

Data loading and analysis

Function Description
load_video(path, n_frames, frames) Load tiff stack from directory
analyze_mask(mask, image) Extract per-cell features from mask
get_masked_pixels(image, mask) Get cell pixel values excluding background

Feature collection

Function Description
collect_features(images, masks, frames) Collect areas and intensities across frames
collect_pixels(images, masks, frames) Collect masked cell pixel values
collect_full_pixels(images, frames) Collect all pixel values without masking
collect_cell_std(images, masks, frames) Collect per-cell texture (std)

GMM and KL divergence

Function Description
fit_gmm(areas, intensities) Fit 2D GMM on log(area) and log(intensity)
fit_gmm_1d(values) Fit 1D GMM on log(values)
fit_gmm_pixels(values) Fit 1D GMM on pixel values
kl_gmm(gmm_p, gmm_q) KL divergence KL(P || Q) between two GMMs
kl_gmm_1d(gmm_p, gmm_q) KL divergence between two 1D GMMs

HOG

Function Description
collect_hog_features(images, frames) Collect HOG feature vectors across frames
visualize_hog(image) Return HOG visualization image

SIFT

Function Description
extract_sift(image) Extract SIFT keypoints and descriptors
collect_sift_descriptors(images, frames) Collect SIFT descriptors across frames
visualize_sift_keypoints(images_dict, hog_size, title) Visualize SIFT keypoints for multiple images side by side

Plotting

Function Description
plot_kl_matrix(KL, names) Plot KL divergence matrix heatmap
plot_histograms(data_dict, title, xlabel) Plot overlapping histograms

Requirements

  • numpy
  • matplotlib
  • scikit-learn
  • scikit-image
  • Pillow

Project details


Download files

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

Source Distribution

sim2realgap-0.1.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

sim2realgap-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file sim2realgap-0.1.1.tar.gz.

File metadata

  • Download URL: sim2realgap-0.1.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for sim2realgap-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6b05c8e23121834253dc7024000e81622bce3adc45262cda69080c09b38e83dd
MD5 80e282d979f30dee2567c9ea0f4fb849
BLAKE2b-256 e0c6162b1aeafea2de654dc232e3af32a6b271bf34a86df1e7ce728469a529f1

See more details on using hashes here.

File details

Details for the file sim2realgap-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sim2realgap-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for sim2realgap-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a987d0ec4cb9b00a84712573f1882efe0b752fa65d2ab9affdddf8b84e2f3a2
MD5 65499898087cfbef3cabb467e7197ce5
BLAKE2b-256 9a61cb0aeea9610ba07fd0d5f30111589655093b1f6c038cb1dacb88ac1b350b

See more details on using hashes here.

Supported by

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