T-MIDAS (Tissue Microscopy Image Data Analysis Suite) is a modular Python package for microscopy image processing and analysis. This documentation provides detailed information about the API components.
tmidas/
├── config.py # Configuration settings
├── processing/ # Domain-specific modules
│ ├── segmentation.py # Image segmentation utilities
│ └── tracking.py # Cell tracking utilities
├── utils/ # Shared utilities
│ ├── io_utils.py # File I/O operations
│ ├── argparse_utils.py # CLI argument handling
│ └── env_utils.py # Environment management
└── tests/ # Unit tests
Configuration settings for T-MIDAS.
Constants:
TMIDAS_PATH: Default installation pathMODELS_PATH: Path to pre-trained modelsDEFAULT_ENV: Default conda environment nameTRACKASTRA_ENV: TrackAstra environment nameDEFAULT_MIN_SIZE: Default minimum label sizeDEFAULT_COMPRESSION: Default TIFF compression
Common I/O utilities for image file operations.
Read an image from file.
Parameters:
file_path(str): Path to the image file
Returns:
- Image array or None if loading fails
Example:
from tmidas.utils.io_utils import read_image
image = read_image("path/to/image.tif")Write an image to file.
Parameters:
image(np.ndarray): Image array to savefile_path(str): Path where to save the image**kwargs: Additional arguments for tifffile.imwrite
Example:
from tmidas.utils.io_utils import write_image
write_image(image_array, "output.tif", compression="zlib")Get all files with a specific extension in a directory.
Parameters:
directory(str): Directory pathextension(str): File extension (with or without dot)
Returns:
- List of filenames
Find files matching a pattern.
Parameters:
directory(str): Directory pathpattern(str): Glob pattern
Returns:
- List of matching file paths
Common argument parsing utilities for CLI scripts.
create_parser(description: str, input_help: str = "Path to input folder") -> argparse.ArgumentParser
Create a basic argument parser.
Parameters:
description(str): Description for the parserinput_help(str): Help text for the input argument
Returns:
- Configured argument parser
Example:
from tmidas.utils.argparse_utils import create_parser
parser = create_parser("Process images", "Path to image folder")Add common arguments to parser.
Parameters:
parser(ArgumentParser): Argument parser to modify
Returns:
- Modified argument parser
Environment management utilities.
Run a shell command.
Parameters:
command(str): Command to executecheck(bool): Whether to raise exception on non-zero exit code
Returns:
- Command output
Check if a conda environment exists.
Parameters:
env_name(str): Name of the environment
Returns:
- True if environment exists
Create a conda environment.
Parameters:
env_name(str): Name of the environmentpython_version(str): Python version to install
Install packages in environment.
Parameters:
env_name(str): Name of the environmentpackages(list[str]): List of package names
Set up TrackAstra environment with all required dependencies.
Image segmentation utilities.
Label connected components in a binary image.
Parameters:
image(np.ndarray): Binary image array
Returns:
- Labeled image
get_region_properties(labeled_image: np.ndarray, intensity_image: Optional[np.ndarray] = None) -> list
Get region properties from labeled image.
Parameters:
labeled_image(np.ndarray): Labeled imageintensity_image(Optional[np.ndarray]): Optional intensity image for intensity measurements
Returns:
- List of region properties
filter_small_labels(labeled_image: np.ndarray, min_size: float, output_type: str = 'instance') -> np.ndarray
Remove labels smaller than min_size.
Parameters:
labeled_image(np.ndarray): Input labeled imagemin_size(float): Minimum size thresholdoutput_type(str): 'instance' or 'semantic'
Returns:
- Filtered labeled image
Example:
from tmidas.processing.segmentation import filter_small_labels
filtered = filter_small_labels(labeled_image, 100.0, 'instance')Cell tracking utilities.
Get pairs of images and their corresponding masks.
Parameters:
folder(str): Directory containing images and maskslabel_suffix(str): Suffix for mask files
Returns:
- List of (image_path, mask_path) tuples
track_cells_with_trackastra(img: np.ndarray, mask: np.ndarray, model_name: str = "general_2d", mode: str = "greedy") -> Optional[Any]
Track cells using TrackAstra.
Parameters:
img(np.ndarray): Image arraymask(np.ndarray): Mask arraymodel_name(str): TrackAstra model namemode(str): Tracking mode ('greedy', 'ilp', 'greedy_nodiv')
Returns:
- Track graph or None if failed
save_tracking_results(track_graph: Any, mask: np.ndarray, output_dir: str, output_format: str = 'ctc') -> Tuple[Optional[Any], Optional[np.ndarray]]
Save tracking results in specified format.
Parameters:
track_graph(Any): Track graph from TrackAstramask(np.ndarray): Original maskoutput_dir(str): Output directoryoutput_format(str): Output format ('ctc', 'napari', etc.)
Returns:
- Tuple of (tracks, tracked_masks)
import sys
sys.path.insert(0, '/opt/T-MIDAS')
from tmidas.utils.io_utils import read_image, write_image
from tmidas.processing.segmentation import filter_small_labels
# Read image
image = read_image("input.tif")
# Process image
filtered = filter_small_labels(image, 50.0)
# Save result
write_image(filtered, "output.tif")from tmidas.utils.env_utils import setup_trackastra_env
# Set up TrackAstra environment
setup_trackastra_env()from tmidas.processing.tracking import get_image_mask_pairs, track_cells_with_trackastra
# Get image pairs
pairs = get_image_mask_pairs("data/", "_mask.tif")
# Track cells
for img_path, mask_path in pairs:
img = read_image(img_path)
mask = read_image(mask_path)
track_graph = track_cells_with_trackastra(img, mask, model_name="general_2d")All functions include proper error handling:
- File I/O operations catch exceptions and return None or print error messages
- Environment operations check for command success
- Import errors are handled gracefully for optional dependencies
All functions include comprehensive type hints for better IDE support and documentation.
- numpy
- scikit-image
- tifffile
- tqdm (for progress bars)
- torch (for deep learning models)
- trackastra (for cell tracking)
Run tests with:
python run_tests.pyTests cover:
- I/O operations
- Segmentation utilities
- Error handling
- Type validation