VALIS: Automated Whole Slide Image Registration

repository·main·Indexed 42 days ago

https://github.com/mathonco/valis

An automated pipeline for registering whole slide images (WSI) using rigid and non-rigid transformations. VALIS handles large-scale pathology image series, supporting high-resolution micro-registration, 3D reconstruction, and the creation of multiplexed OME-TIFF images from immunofluorescence data. It includes specialized tools like AffineOptimizer and AffineOptimizerMattesMI for multi-modal registration and provides functionality to warp cell coordinates or masks into registered coordinate systems.

Tokens
15.3K
Snippets
41
Records
76
Agent score
53%

What's inside VALIS

  1. What is VALIS?

    main

    VALIS (Virtual Alignment of pathoLogy Image Series) is a fully automated pipeline designed to register whole slide images (WSI) using rigid and/or non-rigid transformations. It is capable of working with over 300 image formats by leveraging Bio-Formats, OpenSlide, libvips, and scikit-image.

    Key capabilities include:

    • Automated Registration: A multi-step pipeline including image normalization, feature detection, optimal ordering, rigid registration, and non-rigid registration.
    • High-Resolution Refinement: Support for micro-registration using higher resolution versions of images to align fine features.
    • Output Formats: Registered images can be saved as ome.tiff slides, which are widely supported in software like QuPath and HALO.
    • Data Transformation: Transformations can be used to warp full-resolution slides, merge non-RGB slides into multiplexed images, or warp point data (e.g., cell centroids, polygon vertices).
    • WSI Utilities: Tools for reading slides at multiple resolutions, converting them to numpy arrays or pyvips.Image objects, slicing regions of interest, and converting slides to ome.tiff while preserving metadata.
  2. What is VALIS (Virtual Alignment of pathoLogy Image Series)?

    main

    VALIS is a fully automated pipeline designed to register whole slide images (WSI) using rigid and/or non-rigid transformations. It is particularly useful for aligning pathology image series to a common reference, allowing for the creation of highly multiplexed images or the warping of point data (like cell centroids) across different slides.

    Key capabilities include:

    • Automated Registration: Handles image conversion, normalization, feature detection, and matching.
    • Multi-resolution Support: Performs registration across different pyramid levels, including optional 'micro-registration' for high-resolution micro-features.
    • Flexible Transformations: Supports rigid registration (serial alignment) and non-rigid registration (either sequential or groupwise via SimpleElastix).
    • WSI Handling: Uses Bio-Formats, OpenSlide, and libvips to support over 300 image formats.
    • Output Formations: Registered images can be saved as ome.tiff slides, which are widely supported by software like QuPath and HALO.
  3. Use Matcher classes for feature matching

    main

    The valis.feature_matcher module provides several matcher classes to find correspondences between images. All matcher classes implement a match_images method. You can choose between different algorithms depending on your requirements:

    • Matcher: The base class/standard implementation.
    • LightGlueMatcher: Uses the LightGlue algorithm.
    • SuperPointAndGlue: Uses SuperPoint and LightGlue.
    • SuperGlueMatcher: Uses the SuperGlue algorithm.
  4. How the VALIS registration pipeline works

    main

    The VALIS registration pipeline follows a specific automated sequence to align a stack of images:

    1. Conversion: Images/slides are converted to numpy arrays (typically using lower resolution pyramid levels to manage memory).
    2. Preprocessing: Images are converted to single-channel, normalized, and masked to focus on tissue.
    3. Feature Matching: Image features are detected and matched between all pairs.
    4. Ordering: If the input order is unknown, images are optimally ordered based on feature similarity.
    5. Reference Selection: Images are aligned towards a reference image. If no reference is specified, the image at the center of the stack is used.
    6. Rigid Registration: Performed serially. Each image is aligned towards the reference (e.g., if the 5th image is the reference, image 4 aligns to 5, then 3 aligns to the registered 4, etc.). This can be optionally updated using MicroRigidRegistrar with higher resolution images.
    7. Non-Rigid Registration: Uses a mask derived from rigid registration to extract high-resolution tissue. It supports two modes:
      • Sequential alignment towards the reference.
      • Groupwise registration (requires SimpleElastix to be installed).
    8. Micro-registration (Optional): A second non-rigid pass using even higher resolution images to align micro-features.
    9. Error Estimation: Calculated via the distance between registered matched features in full-resolution images.
  5. Understand Valis registration output directories

    main

    The results_dst_dir contains several subfolders providing insights into the registration process:

    • data/: Contains a summary spreadsheet of alignment results (errors, dimensions, etc.) and a pickled version of the registrar for later use.
    • overlaps/: Thumbnails showing unaligned, rigid-registered, and non-rigid-registered stacks.
    • rigid_registration/: Thumbnails of images after rigid registration.
    • non_rigid_registration/: Thumbnails of images after non-rigid registration.
    • deformation_fields/: Visualizations of non-rigid warping (color = direction, brightness = magnitude).
    • processed/: Thumbnails of the images used during the registration process.
    • masks/: Outlines of the rigid/non-rigid registration masks.
  6. Use SlideReader and its implementations to read slide data

    main

    The SlideReader class and its subclasses provide a unified interface for reading slide data into different formats. All readers support the following core methods:

    • slide2vips: Converts the slide to a VIPS image object.
    • slide2image: Converts the slide to a standard image object.
    • create_metadata: Generates a MetaData object for the slide.

    Available reader implementations:

    • BioFormatsSlideReader: Uses BioFormats (requires JVM management via init_jvm()).
    • VipsSlideReader: Uses libvips for efficient image handling.
    • FlattenedPyramidReader: A specialized VipsSlideReader for flattened pyramid structures.
    • ImageReader: A general-purpose reader inheriting from SlideReader.
    • get_slide_reader: A utility function to retrieve an appropriate reader instance.
    from valis.slide_io import get_slide_reader
    
    reader = get_slide_reader("path/to/slide")
    vips_img = reader.slide2vips()
    image = reader.slide2image()
    metadata = reader.create_metadata()
  7. VALIS WSI Reading and Transformation Tools

    main

    Beyond the core registration pipeline, VALIS provides utility tools for working with whole slide images:

    • Slide Reading: Read slides using Bio-Formats and OpenSlide at multiple resolutions, converting them to numpy arrays or pyvips.Image objects.
    • ROI Slicing: Slice specific regions of interest from slides.
    • Warping: Warp annotated images or point data (e.g., cell centroids, polygon vertices) using the calculated transformations.
    • Format Conversion: Convert slides to the ome.tiff format while preserving original metadata.
  8. Perform high-resolution registration

    main

    For improved quality, you can use higher resolution images for both rigid and non-rigid steps.

    1. High-Res Rigid Registration: Initialize Valis with micro_rigid_registrar_cls=MicroRigidRegistrar. This updates the rigid registration by finding matches in higher-resolution versions.
    2. High-Res Non-Rigid Registration: Use register_micro with a calculated max_non_rigid_registration_dim_px based on your desired resolution fraction.
    import numpy as np
    from valis import registration
    from valis.micro_rigid_registrar import MicroRigidRegistrar
    
    # 1. High resolution rigid registration
    registrar = registration.Valis(
        slide_src_dir=slide_src_dir, 
        results_dst_dir=results_dst_dir, 
        micro_rigid_registrar_cls=MicroRigidRegistrar
    )
    rigid_registrar, non_rigid_registrar, error_df = registrar.register()
    
    # 2. Calculate size for 25% full resolution non-rigid registration
    img_dims = np.array([s.slide_dimensions_wh[0] for s in registrar.slide_dict.values()])
    min_max_size = np.min([np.max(d) for d in img_dims])
    # ... (logic to find max_img_w/h) ...
    micro_reg_size = np.floor(min_max_size * 0.25).astype(int)
    
    # 3. High resolution non-rigid registration
    registrar.register_micro(max_non_rigid_registration_dim_px=micro_reg_size)
  9. Create a multiplex image from immunofluorescence images

    main

    After registration, VALIS can merge multiple slides into a single composite image. This is intended for non-RGB images like multi-channel immunofluorescence (e.g., CyCIF rounds).

    Key Considerations:

    • Channel Naming: You can provide a dictionary channel_name_dict where key=filename and value=list of channel names. If not provided, names default to "channel (filename)" (e.g., "DAPI (round1)").
    • Duplicate Handling: By default, if a channel (like DAPI) exists in multiple images, only the first instance is merged. To include all instances, set drop_duplicates=False in warp_and_merge_slides.

    Workflow:

    1. Initialize registration.Valis(slide_src_dir, results_dst_dir).
    2. Run registrar.register() to perform registration.
    3. Define a mapping for channel names.
    4. Call registrar.warp_and_merge_slides() to generate the merged OME-TIFF.
    import torch
    from valis import registration
    slide_src_dir = "/path/to/slides"
    results_dst_dir = "./slide_merging_example"
    merged_slide_dst_f = "./slide_merging_example/merged_slides.ome.tiff"
    
    registrar = registration.Valis(slide_src_dir, results_dst_dir)
    rigid_registrar, non_rigid_registrar, error_df = registrar.register()
    
    # Example channel name mapping function
    def cnames_from_filename(src_f):
        # Note: Implementation depends on your specific filenames
        return ["DAPI", "CD20", "FOXP3"]
    
    channel_name_dict = {f: cnames_from_filename(f) for f in registrar.original_img_list}
    merged_img, channel_names, ome_xml = \
        registrar.warp_and_merge_slides(merged_slide_dst_f,
                                        channel_name_dict=channel_name_dict,
                                        drop_duplicates=True)
    
    registration.kill_jvm()
  10. Configure system prerequisites for VALIS

    main

    VALIS relies on several system-level libraries for image reading and processing. Ensure the following are configured:

    1. Java Development Kit (JDK): Required for Bioformats (via jpype).
      • Install a JDK containing a JRE.
      • Set JAVA_HOME in your environment variables.
      • Verify with echo $JAVA_HOME.
    2. Maven: Required to use Bioformats.
    3. libvips: Required by pyvips for warping and saving WSIs as ome.tiffs.
      • Must be installed on your system library search path (not just the Python package).
      • Version must be >= 8.11.
    4. OpenSlide (Optional): Required if working with .vmu, .mrxs, or .svslide files.
      • Warning: Requires pixman version 0.40.0. Incorrect versions may cause slide distortion in pyramid levels other than 0.
    # Example: Setting JAVA_HOME
    $ export JAVA_HOME=/usr/libexec/java_home
    $ echo $JAVA_HOME
  11. Install SimpleElastix for advanced registration

    main

    VALIS provides optional classes for advanced registration that require SimpleElastix. If you need these features, you should uninstall your current SimpleITK and follow the SimpleElastix installation guide.

    Supported Classes:

    • affine_optimizer.AffineOptimizerMattesMI: Uses sitk.ElastixImageFilter to maximize Mattes Mutual Information and minimize spatial distance.
    • non_rigid_registrars.SimpleElastixWarper: Uses sitk.ElastixImageFilter for non-rigid transformations.
    • non_rigid_registrars.SimpleElastixGroupwiseWarper: Uses sitk.ElastixImageFilter for groupwise non-rigid registration.
  12. Use VALIS for slide reading and conversion

    main

    Beyond registration, VALIS provides utility tools for handling WSI files:

    • Slide Reading: Read slides using Bio-Formats or OpenSlide at multiple resolutions. These can be converted to numpy arrays or pyvips.Image objects.
    • ROI Slicing: Slice specific regions of interest from slides.
    • Warping: Warp annotated images using the calculated transformations.
    • Format Conversion: Convert slides to the ome.tiff format while preserving original metadata.