pathforge.utils¶
Registries, optional package guards, constants, and helper utilities.
Registries¶
- class pathforge.utils.registries.BackendCatalogEntry[source]¶
Bases:
objectOne backend-aware catalog entry for user-selectable components.
- Variables:
name (str) – User-facing model or extractor name.
backend (str) – Backend required to use this entry, such as
native,torchmil,mil-lab,timm, orlazyslide.config_field (str) – Config field used to select this entry.
source (str) – Origin namespace that provides the entry.
available (bool) – Whether the required backend is currently installed and the entry can be selected in this environment.
Example
entries = list_mil_models() torchmil_names = [item.name for item in entries if item.backend == "torchmil"]
- name: str¶
- backend: str¶
- config_field: str¶
- source: str¶
- available: bool¶
- __init__(name: str, backend: str, config_field: str, source: str, available: bool) None¶
- Parameters:
name (str)
backend (str)
config_field (str)
source (str)
available (bool)
- Return type:
None
- pathforge.utils.registries.timm_model_names() set[str][source]¶
Return the TIMM model names visible in the current Python environment.
- Return type:
set[str]
- pathforge.utils.registries.lazyslide_model_names() set[str][source]¶
Return the LazySlide model names visible in the current Python environment.
- Return type:
set[str]
- pathforge.utils.registries.registered_feature_extractor_names() set[str][source]¶
Best-effort extraction of names currently registered in PathForge FEATURE_EXTRACTORS.
- Return type:
set[str]
- pathforge.utils.registries.available_feature_extractor_names() dict[str, set[str]][source]¶
Return extractor names grouped by the backend that provides them.
- Return type:
dict[str, set[str]]
- pathforge.utils.registries.all_feature_extractor_names() set[str][source]¶
Return the union of PathForge-native and dynamically discovered extractors.
- Return type:
set[str]
- pathforge.utils.registries.is_feature_extractor_available(name: str) bool[source]¶
Used by config validation without importing timm/torchvision at module import time.
- Parameters:
name (str)
- Return type:
bool
- pathforge.utils.registries.populate_dynamic_registries() None[source]¶
Populate optional backend registries with entries from installed packages.
IMPORTANT: - This is NOT called automatically at import time. - Call it explicitly in CLI/policy paths that require optional backends.
- Return type:
None
- pathforge.utils.registries.list_feature_extractors() list[BackendCatalogEntry][source]¶
List user-selectable feature extractors across supported backends.
- Returns:
- Catalog entries sorted by backend and name.
For optional backends such as
timmandlazyslide, only installed catalogs can be enumerated because their model lists come from the backend package itself.
- Return type:
list[BackendCatalogEntry]
Example
entries = list_feature_extractors() lazyslide_only = [item.name for item in entries if item.backend == "lazyslide"]
- pathforge.utils.registries.list_mil_models() list[BackendCatalogEntry][source]¶
List user-selectable MIL models across native and adapter backends.
- Returns:
- Catalog entries sorted by backend and name.
Native PathForge MIL models are always listed. Backend-adapter model catalogs are listed even when their backend is unavailable so the caller can present supported choices together with installation requirements.
- Return type:
list[BackendCatalogEntry]
Example
entries = list_mil_models() mil_lab_models = [item.name for item in entries if item.backend == "mil-lab"]
- pathforge.utils.registries.resolve_mil_model_backend(name: str) str[source]¶
Return the backend providing a selectable MIL model name.
The generic
torchmilandmil-labkeys remain supported for legacy configs. New benchmark grids should use a concrete name returned bylist_mil_models().- Parameters:
name (str)
- Return type:
str
- class pathforge.utils.registry.Registry[source]¶
Bases:
RegistryBaseMinimal string-to-callable registry used for runtime plugin lookup.
Available registries:
Registry |
Purpose |
|---|---|
|
MIL and slide-level model classes. |
|
Loss functions for all task types. |
|
Trainer implementations (e.g. |
|
Feature extraction backends. |
|
WSI loading backends. |
|
Classification metric backends. |
|
Survival metric backends. |
|
Survival loss backends. |
|
Heatmap/explainability methods. |
Optional Package Guards¶
- class pathforge.utils.optional.torchmil.TorchMILModules[source]¶
Bases:
objectLazy references to installed TorchMIL modules.
- Variables:
root (types.ModuleType) – Imported
torchmilpackage module.models (types.ModuleType) – Imported
torchmil.modelsmodule. Expected to expose model classes such asABMIL.data (types.ModuleType) – Imported
torchmil.datamodule. Expected to exposecollate_fnaccepting a list of bag dictionaries and returning a padded batch withXshaped[B, N_max, D]andmaskshaped[B, N_max].datasets (types.ModuleType) – Imported
torchmil.datasetsmodule.
Example
from pathforge.utils.optional.torchmil import load_torchmil_modules modules = load_torchmil_modules() batch = modules.data.collate_fn([{"X": x0, "Y": y0}, {"X": x1, "Y": y1}]) model_cls = getattr(modules.models, "ABMIL")
- Raises:
RuntimeError – If TorchMIL is not installed.
- root: ModuleType¶
- models: ModuleType¶
- data: ModuleType¶
- datasets: ModuleType¶
- __init__(root: ModuleType, models: ModuleType, data: ModuleType, datasets: ModuleType) None¶
- Parameters:
root (ModuleType)
models (ModuleType)
data (ModuleType)
datasets (ModuleType)
- Return type:
None
- pathforge.utils.optional.torchmil.is_torchmil_available() bool[source]¶
Return whether
torchmilcan be imported without importing it eagerly.- Return type:
bool
- pathforge.utils.optional.torchmil.is_torchmetrics_available() bool[source]¶
Return whether
torchmetricscan be imported without importing it eagerly.- Return type:
bool
- pathforge.utils.optional.torchmil.is_torchsurv_available() bool[source]¶
Return whether
torchsurvcan be imported without importing it eagerly.- Return type:
bool
- pathforge.utils.optional.torchmil.require_torchmil(feature: str) None[source]¶
Raise an install hint when a TorchMIL-only feature is requested.
- Parameters:
feature (str) – Human-readable feature name, for example
"MIL backend 'torchmil'"or"TorchMIL heatmap explainer".- Raises:
RuntimeError – If
torchmilis not installed.- Return type:
None
- pathforge.utils.optional.torchmil.require_torchmetrics(feature: str = 'Classification metrics backend') None[source]¶
Raise an install hint when TorchMetrics-backed classification metrics are used.
- Parameters:
feature (str)
- Return type:
None
- pathforge.utils.optional.torchmil.require_torchsurv(feature: str = 'Continuous survival backend') None[source]¶
Raise an install hint when TorchSurv-backed survival functionality is used.
- Parameters:
feature (str)
- Return type:
None
- pathforge.utils.optional.torchmil.load_torchmil_modules() TorchMILModules[source]¶
Import TorchMIL modules lazily and return stable module references.
- Returns:
Imported root, models, data, and datasets modules.
- Return type:
- Raises:
RuntimeError – If
torchmilor an expected TorchMIL submodule is missing.
- class pathforge.utils.optional.mil_lab.MILLabModules[source]¶
Bases:
objectLazy references to installed MIL-Lab modules.
- builder: ModuleType¶
- __init__(builder: ModuleType) None¶
- Parameters:
builder (ModuleType)
- Return type:
None
- pathforge.utils.optional.mil_lab.is_mil_lab_available() bool[source]¶
Return whether MIL-Lab can be imported without importing it eagerly.
- Return type:
bool
- pathforge.utils.optional.mil_lab.require_mil_lab(feature: str) None[source]¶
Raise an install hint when a MIL-Lab-only feature is requested.
- Parameters:
feature (str)
- Return type:
None
- pathforge.utils.optional.mil_lab.load_mil_lab_modules() MILLabModules[source]¶
Import MIL-Lab builder modules lazily and return stable references.
- Return type:
Constants¶
Shared constants used across PathForge.