Adding a new segmentation algorithm¶
HistoKit supports adding new segmentation algorithms through a common configuration and segmenter interface. To integrate a new algorithm, create two files containing the configuration and segmenter classes, and register the algorithm in registry.py.
1. Create a configuration class¶
The configuration class must inherit from BaseAlgorithmConfig, defined in src/histokit/segmentation/config.py.
The base class provides common configuration parameters, output collector management, loading from dictionaries and YAML files, and configuration serialization.
| Parameter | Default | Description |
|---|---|---|
saver |
"hdf5" |
Output saver identifier. |
out_dir |
"./outputs" |
Output directory. |
save_mag |
1.0 |
Magnification used for saving results. |
collectors |
None |
Enabled output collectors. |
algorithm_name |
"Base" |
Algorithm name used in serialization. |
collector_registry |
{} |
Mapping of supported collector names to classes. |
default_collectors |
[] |
Default collectors for the algorithm. |
Create a config.py file for your algorithm and define a dataclass with algorithm-specific parameters:
from dataclasses import dataclass
from typing import Any, ClassVar
from histokit.segmentation.config import BaseAlgorithmConfig
@dataclass
class CustomConfig(BaseAlgorithmConfig):
algorithm_name: ClassVar[str] = "custom"
threshold: float = 0.5
min_area: int = 100
def to_hdf5_dict(self) -> dict[str, Any]:
return {
**self.common_hdf5_dict(),
"threshold": self.threshold,
"min_area": self.min_area,
}
2. Implement the segmenter¶
The segmenter must inherit from the abstract Segmenter class, defined in src/histokit/segmentation/base.py, and implement the segment() method.
Create a segmenter.py file:
from __future__ import annotations
from histokit.segmentation.base import Segmenter
from .config import CustomConfig
class CustomSegmenter(Segmenter):
def __init__(self, config: CustomConfig):
super().__init__()
self.config = config
def segment(self, slide: Slide) -> dict:
# Implement the segmentation logic here.
raise NotImplementedError("Implement custom segmentation")
The segment() method receives a Slide object and must return a dictionary containing segmentation results.
Collecting intermediate outputs¶
The inherited _collect() method allows segmenters to emit intermediate or final outputs without directly managing their persistence.
For example, after computing a mask:
self._collect(
name="tissue_mask",
step="thresholding",
kind="mask",
data=mask,
threshold=self.config.threshold,
)
| Parameter | Description |
|---|---|
name |
Name of the generated output. |
step |
Pipeline stage that generated the output. |
kind |
Output data type or category. |
data |
Generated data. |
**metadata |
Additional information associated with the output. |
The method wraps the data in a PipelineOutput object and passes it to the output collector. Ensure that output_collector is initialized before calling _collect(), as the base segmenter initializes it to None.
Configuring output collectors¶
Algorithms can define supported collectors through collector_registry and default collectors through default_collectors.
| Configuration | Behavior |
|---|---|
collectors=None |
Use algorithm-specific defaults. |
collectors=[] |
Disable output collection. |
collectors=[...] |
Use selected collectors. |
Collectors can be specified by name or as dictionaries with optional parameters:
collectors:
- collector_name
- name: another_collector
params:
parameter: value
The names above are placeholders and must correspond to entries in the algorithm's collector registry.
The build_output_collector() method creates a CompositeOutputCollector containing the configured collectors. When no collectors are enabled, it returns a NoOpOutputCollector.
3. Register the algorithm¶
Import the new configuration and segmenter classes in registry.py and add an entry to SEGMENTER_REGISTRY:
SEGMENTER_REGISTRY = {
"gamred": {
"config": GaMRedConfig,
"segmenter": GaMRedSegmenter,
"type": "tissue",
},
"grandqc": {
"config": GrandQCConfig,
"segmenter": GrandQCSegmenter,
"type": "artifact",
},
"hethr": {
"config": HeThrConfig,
"segmenter": HeThrSegmenter,
"type": "tissue",
},
"custom": {
"config": CustomConfig,
"segmenter": CustomSegmenter,
"type": "tissue",
},
}
| Field | Description |
|---|---|
| Registry key | Unique identifier used to select the algorithm. |
config |
Algorithm configuration class. |
segmenter |
Class implementing segmentation logic. |
type |
Segmentation category, e.g. tissue or artifact. |