HistoQC 2.0
repository·master·Indexed 43 days ago
https://github.com/choosehappy/histoqcAn open-source digital pathology quality control tool and modular pipeline for histological image quality control. It provides automated assessment of slide quality and allows developers to extend functionality via custom Python modules that interact with image metadata and masks.
What's inside HistoQC
- HistoQC is an open-source quality control tool designed for digital pathology slides. It provides automated assessment of slide quality to ensure consistency in digital pathology workflows.
Understand the HistoQC pipeline and mask refinement model
masterHistoQC operates as a sequential pipeline of modules applied to an image. The core mechanism is the refinement of a pixel mask called
img_mask_use.- Initial State: When an image is loaded, it is assigned a "True mask" (
img_mask_use) where every pixel is considered artifact-free and "useful" for analysis. - Sequential Refinement: As modules run, they update
img_mask_useto exclude pixels that fail certain criteria (e.g., background, artifacts, or low-intensity areas). - Module Interaction: Many modules include a
limit_to_maskoption. When enabled, the module only performs operations on pixels currently marked astruein theimg_mask_usemask.
Important: Because modules refine the same mask, the order of modules in your pipeline is critical. A module that identifies and masks out background (like
LightDarkModule.getIntensityThresholdPercent:tissue) should be placed before modules that calculate statistics (likeHistogramModule.getHistogram) to ensure the statistics are only calculated on valid tissue areas.- Initial State: When an image is loaded, it is assigned a "True mask" (
Apply a threshold to classification probability maps
masterAfter a classification model is trained, it generates a probability likelihood for each pixel belonging to the trained positive class. Because raw probability values are not suitable for downstream analysis, you must use thetreshparameter to apply a threshold to the probability map. This converts the probability map into a final binary value mask.Install HistoQC using Docker
masterDocker is the recommended installation method for HistoQC to ensure portability and reproducible behavior. This method avoids Python environment management issues and works on Windows, MacOS, and Linux.
To run HistoQC, pull the official image from Docker Hub and run it with the following command structure. You must map a local directory to the
/datadirectory inside the container to access your files and specify a local port to map to the container's port5000.docker run -v <local-path>:/data --name <container-name> -p <local-port>:5000 -it histotools/histoqc:master /bin/bash # Example: docker run -v /local/datadir:/data --name my_container -p 5000:5000 -it histotools/histoqc:master /bin/bashAugment classifier features using skimage filters
masterTo improve classifier robustness, you can augment the original RGB space by computing additional pixel-features. HistoQC uses features implemented in
skimage.filters.Available features include:
frangilaplacergblbp(commented out in default)gabor(commented out in default)median(commented out in default)gaussian(commented out in default)
Configuring Feature Parameters: You can set specific parameters for a filter by using the feature name as a prefix to the parameter name. For example, to set the
black_ridgesparameter of thefrangifilter toTrue, usefrangi_black_ridges: True.features: frangi laplace rgb #lbp #gabor #median #gaussian # Example of setting a specific filter parameter: frangi_black_ridges: TrueConfigure classification type modules with exemplar images
masterClassification modules in HistoQC use a model-based approach rather than simple statistics. To set this up, you must provide a set of exemplar images and their corresponding binary masks using the
examplesparameter.Each line in the
examplesparameter must specify one exemplar set using the format:path/to/original_image.png:path/to/binary_mask.png.Requirements for exemplars:
- Mask Format: The mask must be a binary image containing only values
{0, 1}. A value of1identifies pixels for the positive class, and0identifies pixels for the negative class. - Image Size: For optimal performance, exemplar images should be of the same magnification specified by the
image_work_sizeparameter. - Pathing: You can use either relative or absolute paths.
examples: ./pen/1k_version/pen_green.png:./pen/1k_version/pen_green_mask.png #./pen/1k_version/pen_red.png:./pen/1k_version/pen_red_mask.png- Mask Format: The mask must be a binary image containing only values
Configure the HistoQC pipeline order
masterThe HistoQC pipeline is defined using a configuration file following Python's
configparsersyntax. The core of the pipeline is the[pipeline]section, which defines the sequence of modules to be executed on each image.Module Naming and Reusability
You can use the same module multiple times in a single pipeline by assigning it a unique instance name using the syntax
ModuleName.method:instance_name.When you create an instance (e.g.,
LightDarkModule.getIntensityThresholdPercent:tissue), you must define a corresponding section in the config file named exactly after that instance (e.g.,[LightDarkModule.getIntensityThresholdPercent:tissue]). This section must contain aname:parameter, which determines the output name of the image and the column name in the resulting TSV file.Example Pipeline Configuration
[pipeline] steps= BasicModule.getBasicStats BasicModule.getMag ClassificationModule.byExampleWithFeatures:pen_markings ClassificationModule.byExampleWithFeatures:coverslip_edge LightDarkModule.getIntensityThresholdPercent:tissue LightDarkModule.getIntensityThresholdPercent:darktissue MorphologyModule.removeSmallObjects MorphologyModule.fillSmallHoles BlurDetectionModule.identifyBlurryRegions BasicModule.finalProcessingSpur BasicModule.finalProcessingArea HistogramModule.compareToTemplates HistogramModule.getHistogram BrightContrastModule.getContrast BrightContrastModule.getBrightness DeconvolutionModule.seperateStains SaveModule.saveFinalMask SaveModule.saveThumbnail BasicModule.finalComputationsSuggested HistoQC Workflow
masterThe recommended workflow for processing histology images with HistoQC involves a two-step approach to ensure data homogeneity and prevent computational errors (like out-of-memory issues).
Step 1: Discovery and Cohort Splitting
Run HistoQC using a minimal pipeline (e.g.,
config_first.ini). This step is used to:- Identify images scanned at different magnifications (e.g., 20x vs 40x).
- Perform basic tissue detection (white threshold).
- Compute histograms and differences to target templates.
Goal: Split your dataset into sub-cohorts that share the same magnification, the same number of internal storage levels, and similar appearance properties (lightness, stain intensity, etc.).
Using the Web UI:
- Use the parallel coordinates graph to create filters by clicking and dragging on axes.
- Use "Save Filtered" to save a specific subset of images based on your filters.
- Use "Delete" (after multi-selecting) to remove unsuitable images from the table.
- Click "Save" to generate an output file containing only the selected subset.
Step 2: Full Pipeline Execution
Once sub-cohorts are established, rerun the pipeline using an expanded set of models (e.g., using
config.inidesigned for 40x H&E images). This step allows you to:- Identify images with artifacts unsuitable for computation.
- Verify if suggested masks are appropriate for downstream tasks by using the "compare" dropdown and selecting
_fuse.pngto view the original image alongside fused images.
Create and register a new HistoQC module
masterTo extend HistoQC, create a new Python file where the filename represents the module class (e.g.,
HistogramModule.py). Inside this file, define functions that perform specific tasks.To integrate your module into the HistoQC pipeline:
- Add the function to the
[pipeline]section in the configuration file using the formatfilename.function(omitting the.pyextension). For example, if your file isHistogramModule.pyand your function iscompareTemplates, addHistogramModule.compareTemplatesto thestepslist. - To provide custom parameters for your function, add a section at the bottom of the configuration file named
[filename.function].
# Example configuration entry [pipeline] steps = [ "HistogramModule.compareTemplates", ... ] [HistogramModule.compareTemplates] threshold = 0.7- Add the function to the
Save output images from a module
masterTo save a new mask or output image, use the
s["outdir"],s["filename"], andos.septo construct the path. This ensures images are saved in the correct location for the pipeline.Naming Convention: Append an underscore followed by a descriptive name and the extension (e.g.,
_MyMask.png).Registering with the UI: To make your new image type visible in the HistoQC front-end, you must manually add the file extension suffix to the
DEFAULT_IMAGE_EXTENSIONSlist inglobal_config.js.import os from skimage import io import numpy as np # Example: saving a binary mask # Convert 0/1 mask to 0/255 for visibility io.imsave(s["outdir"] + os.sep + s["filename"] + "_BubbleBounds.png", mask.astype(np.uint8) * 255)Specify file paths in the HistoQC configuration file
masterWhen defining file paths in the HistoQC configuration file, note that filenames are resolved relative to the working directory where you execute the command, rather than the base directory of the HistoQC installation.
To avoid path resolution errors, you should either:
- Use absolute paths for all input files.
- (Preferred) Run
histoqcfrom the HistoQC directory and provide the remote location or relative path of the input files from that execution context.
Install HistoQC using pip
masterFor development or customization, you can install HistoQC from source using
pip. It is recommended to use a Python virtual environment to manage dependencies.Follow these steps:
- Clone the repository.
- (Optional) Create and activate a virtual environment.
- Install the package using
pip install .. - Verify the installation by running
histoqc --help.
# 1. Clone the repository git clone https://github.com/choosehappy/HistoQC.git cd HistoQC # 2. (Optional) Set Up a Virtual Environment python3 -m venv venv source venv/bin/activate # 3. Install HistoQC pip install . # 4. Verify Installation histoqc --help