Coder Social home page Coder Social logo

mwod / deeperhistreg Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 1.0 11.39 MB

DeeperHistReg - Source Code

License: Creative Commons Attribution Share Alike 4.0 International

Dockerfile 0.01% Python 4.09% Shell 0.04% Jupyter Notebook 95.86%
deep-learning digital-pathology histopathology image-registration wsi-images wsi-registration

deeperhistreg's Introduction

DeeperHistReg - Robust Whole Slide Images Registration Framework

Example

Example

The repository containts the source code the the DeeperHistReg library together with usage examples.

The library is dedicated to performing an automatic and robust affine/deformable registration of microscopy images, either WSIs acquired with different stains or images fluorescence microscopy.

  • The library was tested on the following stains / types of images acquired by various equipment:
    • H&E
    • ER
    • PGR
    • HER2
    • KI67
    • CC10
    • CD8
    • CD31
    • CD45
    • PR
    • PHH3
    • DAPI

The highest tested resolution was close to 220k x 220k pixels.

Methods available in the library won the ACROBAT 2023 challenge organized during the MICCAI conference and are among the best performing ones according to several benchmarks Article.

Installation

pip install deeperhistreg

Or clone the repository and install directly.

You can also run the registration using Docker (see below).

In case you decide to clone the repository and install directly - please download the "dhr_models" and copy it to the deeperhistreg folder (due to large file sizes): Models. This step is not required if installed via pip.

Examples

Affine Registration

import deeperhistreg

def run():
    ### Define Inputs/Outputs ###
    source_path : Union[str, pathlib.Path] = None # Path to the source image
    target_path : Union[str, pathlib.Path] = None # Path to the target image
    output_path : Union[str, pathlib.Path] = None # Path to the destination directory

    ### Define Params ###
    registration_params : dict = deeperhistreg.configs.default_affine()
    # Alternative: # registration_params = deeperhistreg.configs.load_parameters(config_path) # To load config from JSON file
    save_displacement_field : bool = True # Whether to save the displacement field (e.g. for further landmarks/segmentation warping)
    copy_target : bool = True # Whether to copy the target (e.g. to simplify the further analysis)
    delete_temporary_results : bool = True # Whether to keep the temporary results
    case_name : str = "Example_Affine" # Used only if the temporary_path is important, otherwise - provide whatever
    temporary_path : Union[str, pathlib.Path] = None # Will use default if set to None

    ### Create Config ###
    config = dict()
    config['source_path'] = source_path
    config['target_path'] = target_path
    config['output_path'] = output_path
    config['registration_parameters'] = registration_params
    config['case_name'] = case_name
    config['save_displacement_field'] = save_displacement_field
    config['copy_target'] = copy_target
    config['delete_temporary_results'] = delete_temporary_results
    config['temporary_path'] = temporary_path
    
    ### Run Registration ###
    deeperhistreg.run_registration(**config)

if __name__ == "__main__":
    run()

Please find the example here: Affine Registration.

Deformable Registration

import deeperhistreg

def run():
    ### Define Inputs/Outputs ###
    source_path : Union[str, pathlib.Path] = None # Path to the source image
    target_path : Union[str, pathlib.Path] = None # Path to the target image
    output_path : Union[str, pathlib.Path] = None # Path to the destination directory

    ### Define Params ###
    registration_params : dict = deeperhistreg.configs.default_initial_nonrigid()
    # Alternative: # registration_params = deeperhistreg.configs.load_parameters(config_path) # To load config from JSON file
    save_displacement_field : bool = True # Whether to save the displacement field (e.g. for further landmarks/segmentation warping)
    copy_target : bool = True # Whether to copy the target (e.g. to simplify the further analysis)
    delete_temporary_results : bool = True # Whether to keep the temporary results
    case_name : str = "Example_Nonrigid" # Used only if the temporary_path is important, otherwise - provide whatever
    temporary_path : Union[str, pathlib.Path] = None # Will use default if set to None

    ### Create Config ###
    config = dict()
    config['source_path'] = source_path
    config['target_path'] = target_path
    config['output_path'] = output_path
    config['registration_parameters'] = registration_params
    config['case_name'] = case_name
    config['save_displacement_field'] = save_displacement_field
    config['copy_target'] = copy_target
    config['delete_temporary_results'] = delete_temporary_results
    config['temporary_path'] = temporary_path
    
    ### Run Registration ###
    deeperhistreg.run_registration(**config)

if __name__ == "__main__":
    run()

Please find the example here: Deformable Registration.

Apply Deformation

    ### Run Registration ###
    deeperhistreg.run_registration(**config)

    ### Apply Warping ###
    source_image_path = source_path # Here the same as during the registration - can be replaced with path to the binary mask
    target_image_path = target_path # Here the same as during the registration - serves as a template
    warped_image_path = None  # Where to save the warped image
    displacement_field_path = pathlib.Path(output_path) / "displacement_field.mha"
    loader = deeperhistreg.loaders.OpenSlideLoader # Which backend should be used to load the slide
    saver = deeperhistreg.savers.TIFFSaver # What is the desired format of the output
    save_params = deeperhistreg.savers.tiff_saver.default_params
    level = 0 # Pyramid level to perform the warping - 0 is the highest possible
    pad_value = 255 # White in the RGB representation
    save_source_only = True # Whether to save only the warped image or also the corresponding target image
    to_template_shape = True # Whether to align the source shape to template shape (if initially different)
    to_save_target_path = None # Path where to save the target (if save_source_only set to False)

    deeperhistreg.apply_deformation(
        source_image_path = source_image_path,
        target_image_path = target_image_path,
        warped_image_path = warped_image_path, 
        displacement_field_path = displacement_field_path,
        loader = loader,
        saver = saver,
        save_params = save_params,
        level = level,
        pad_value = pad_value,
        save_source_only = save_source_only,
        to_template_shape = to_template_shape,
        to_save_target_path = to_save_target_path
    )

Please find the example here: Apply Deformation.

Jupyter Notebook

Please find the example here: Jupyter Notebook.

Docker Usage

You can download the Docker container here: Docker.

The usage is as follows:

#!/bin/bash
: '
This example shows how to use the the DeeperHistReg library using the Docker container (single registration).
You can use the provided image or build it by yourself.
To build the docker directly use: docker build --tag image_name . <- where image_name is the desired name.
'
IMAGE_NAME=deeperhistreg # The container name

SOURCE_PATH=/source_path  # Full path to the source image
TARGET_PATH=/target_path # Full path to the target image
PARAMS=/path_to_json_params.json # Full path to the registration config (see deeperhistreg_params) - Defaults to default_nonrigid.json
SAVE_PATH=/save_directory # Full path to the directory that will contain the results (will be created if does not exists)
CASE_NAME=Example # Name for the given registration pair
TEMP_DIR=/temporary_directory # Temporary results - contain intermediate registration steps (defaults to reasonable value)
# --sdf - Whether to save the displacement field (e.g. for further landmarks warping or segmentation transfer)
# --cpt - Whether to copy the target to the output folder (to simplify further analysis)
# --dtmp - Whether to delete the temporary results (presenting preprocessing, initial alignement, nonrigid registration - makes sense for Docker)

docker run --gpus all -v $SOURCE_PATH:$SOURCE_PATH -v $TARGET_PATH:$TARGET_PATH -v $PARAMS:$PARAMS -v $SAVE_PATH:$SAVE_PATH $IMAGE_NAME \
 --srcp $SOURCE_PATH --trgp $TARGET_PATH --out $SAVE_PATH --params $PARAMS --exp $CASE_NAME --temp $TEMP_DIR --sdf --cpt --dtmp

Please find the example here: Docker_Example.

Configuration Files

There are several configuration files available either as a dictionary (when using as a library) or JSON file (when using the Docker).

Supported formats

Currently the library supports all formats that can be loaded using:

  • OpenSlide OpenSlide
  • PyVips PyVips
  • tifffile tifffile
  • PIL - dedicated mainly to .jpg and .png, however still .tiff files are recommended rather than these formats for the high-resolution images

How to change the requested format?

Simply modify the configuration in the following lines:

    ### Loading Params ###
    loading_params = dict()
    loading_params['final_level'] = 0
    loading_params['pad_value'] = 255.0
    loading_params['loader'] = 'tiff' # <- modify the loader here (Supported: tiff, pil, vips, openslide, planned support for bioformats)
    loading_params['source_resample_ratio'] = 0.5
    loading_params['target_resample_ratio'] = 0.5
    params['loading_params'] = loading_params

Requests

Are you searching for another functionality related to the registration of microscopic or other images? Isn't your format supported or the registration results are not satisfactory? You don't have the computational capabilities to run the library?

If so - I am open to research collaboration, do not hestiate to contact me: [email protected]

License

We release the framework as is without warranty. The framework is licensed under CC-BY-SA. However, please note that to use the the full funcionality (that does depend on external modules) you must follow the appropriate license related to a particular module. Important mentions:

  • In case you use the SuperPoint: LICENSE - NONCOMMERCIAL, RESEARCH USE ONLY
  • In case you use the SuperGlue: LICENSE - NONCOMMERCIAL, RESEARCH USE ONLY

Publications

If you found the source code useful, please cite:

You may be also interested in:

Acknowledgements

This project has received partial funding from the Innovative Medicines Initiative 2 Joint Undertaking under grant agreement No 945358. This Joint Undertaking receives support from the European Union's Horizon 2020 research and innovation program and EFPIA, Belgium (www.imi.europe.eu).

We gratefully acknowledge Poland’s high-performance computing infrastructure PLGrid (HPC Centers: ACK Cyfronet AGH) for providing computer facilities and support within computational grant no. PLG/2023/016239.

deeperhistreg's People

Contributors

mwod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nourhanofterra

deeperhistreg's Issues

FileNotFoundError: 'Results_Final'

Hi, I try to register two histology slides (both png files) with wsl and installed the deeperhistreg pip package. When I run code from the jupyter notebook or the bash application 'run_registration.sh', the same error occurs:
Source shape: (3424, 4615, 3)
Target shape: (3529, 3659, 3)
Traceback (most recent call last):
File "/xxx/deeperhistreg_jupyter.py", line 68, in
deeperhistreg.run_registration(**config)
File "/yyy/run.py", line 50, in run_registration
warped_name = [item for item in os.listdir(pathlib.Path(save_path) / experiment_name / "Results_Final") if "warped_source" in item][0]
FileNotFoundError: [Errno 2] No such file or directory: '/zzz/Results/file1_file2_TEMP/file1_file2/Results_Final'

The Preprocessing folder and the Initial_Registration folder were successfully created. The logfile in the TEMP-folder states that Image loading, Preprocessing and Initial alignment are saved. 'Initial alignment saved.' is the last output there.
How can I proceed with the nonrigid registration to get the final results or what else should I consider? Thank you in advance!

error in the line pair_loader = pair_full_loader.PairFullLoader(

raise an error during executing "pair_loader = pair_full_loader.PairFullLoader(self.source_path, self.target_path, loader=loader, mode=pair_full_loader.LoadMode.PYTORCH)"

Both of my source_path and target_path are tiff files.

source_path: Union[str, pathlib.Path] = f"/disk_data/data/wsi_registration/src/217338/217338HE.tiff" # Path to the source image
target_path: Union[str, pathlib.Path] = f"/disk_data/data/wsi_registration/src/217338CD8.tiff" # Path to the target image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.