Feature-loader API¶
Patch bags¶
bag_features
¶
Single-scale bag-features dataset.
Used by SLIP, TOP, and any method that takes one tensor of patch features per slide.
CSV format
slide_id, label
BagFeaturesDataset
¶
BagFeaturesDataset(
csv_path: str | DataFrame,
feature_root: str,
label_dict: dict,
max_patches: int | None = None,
ext: str = ".pt",
feature_path_column: str | None = None,
feature_key: str = "features",
feature_dim: int | None = None,
include_metadata: bool = False,
random_subsampling: bool = True,
)
Bases: Dataset
Load one variable-length patch-feature bag per slide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
str | DataFrame
|
Manifest containing at least |
required |
feature_root
|
str
|
Root used when no explicit feature-path column is set. |
required |
label_dict
|
dict
|
Mapping from string labels to integer class indices. |
required |
max_patches
|
int | None
|
Optional cap applied after loading a bag. |
None
|
ext
|
str
|
Per-slide file suffix used with |
'.pt'
|
feature_path_column
|
str | None
|
Optional manifest column containing exact paths. |
None
|
feature_key
|
str
|
Tensor key used for mapping or HDF5 payloads. |
'features'
|
feature_dim
|
int | None
|
Expected patch width. A mismatch raises immediately. |
None
|
include_metadata
|
bool
|
Include slide/case identifiers in returned samples. |
False
|
Each sample returns (features, label) or
(features, metadata, label). Features have shape [patches, dim];
batching is normally restricted to one slide because bag lengths vary.
Source code in common/datasets/bag_features.py
build_bag_loader
¶
build_bag_loader(
cfg: Mapping[str, Any],
split: str = "train",
shuffle: bool = True,
fold: int | None = None,
) -> DataLoader
Construct a single-scale patch-bag loader from a run config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
Mapping[str, Any]
|
Run configuration containing split and feature-location fields. |
required |
split
|
str
|
Split filename stem, such as |
'train'
|
shuffle
|
bool
|
Request shuffling; only the training split is shuffled. |
True
|
fold
|
int | None
|
Fold index used for nested or wide split resolution. When omitted,
the private |
None
|
Returns:
| Type | Description |
|---|---|
DataLoader
|
A PyTorch data loader yielding the dataset's bag tuples. |
Source code in common/datasets/bag_features.py
Slide embeddings¶
slide_embeddings
¶
Shared one-vector-per-slide feature loading.
This module is method-agnostic. Any adapter whose backbone contract declares
FeatureLevel.SLIDE_EMBEDDING receives the same exact-key, exact-width,
provenance-aware HDF5/torch/pickle input behavior from the unified trainer.
Patch bags are intentionally handled elsewhere.
SlideEmbeddingSource
dataclass
¶
SlideEmbeddingSource(
source_type: str,
features_path: Path,
feature_path_column: str | None,
feature_key: str,
feature_dim: int,
slide_id_key: str,
)
Normalize runtime fields shared by slide-vector consumers.
The source separates offline slide encoder provenance from any runtime prompt encoder selected by methods such as SLDPC.
from_config
classmethod
¶
Validate and construct a source from a generated run config.
Source code in common/datasets/slide_embeddings.py
SlideEmbeddingDataset
¶
SlideEmbeddingDataset(
source_type: str,
features_path: str | Path,
csv_path: str | Path | DataFrame,
label_dict: Mapping[str, int],
feature_path_column: str | None = None,
feature_key: str = "features",
feature_dim: int | None = None,
slide_id_key: str = "filenames",
)
Bases: Dataset
Load one exact-width vector and label for every split slide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_type
|
str
|
|
required |
features_path
|
str | Path
|
Shared pickle file or root containing per-slide files. |
required |
csv_path
|
str | Path | DataFrame
|
Split CSV with slide IDs and labels. |
required |
label_dict
|
Mapping[str, int]
|
Mapping from string labels to integer class indices. |
required |
feature_path_column
|
str | None
|
Optional CSV column with exact per-slide paths. |
None
|
feature_key
|
str
|
Exact tensor key inside HDF5 or mapping payloads. |
'features'
|
feature_dim
|
int | None
|
Expected flattened vector width. |
None
|
slide_id_key
|
str
|
Identifier key used by a shared pickle payload. |
'filenames'
|
Samples are dictionaries with feat, label, slide_id, and
case_id. Every declared split row must match exactly one feature;
missing or ambiguous IDs are fatal rather than silently changing a split.
Source code in common/datasets/slide_embeddings.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
normalise_slide_id
¶
Decode an identifier and remove only known slide/feature suffixes.
NumPy and Python pickle producers commonly persist identifier arrays as
fixed-width byte strings. Calling str on those values produces text
such as "b'slide-a.svs'", which can never match a manifest slide ID.
Decode byte-like values explicitly and reject blank identifiers at the
source boundary instead.
Source code in common/datasets/slide_embeddings.py
infer_slide_embedding_source_type
¶
infer_slide_embedding_source_type(
path_template: str | Path | None = None,
storage: str | None = None,
) -> str
Map a registered storage declaration to the shared loader layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_template
|
str | Path | None
|
Representative source path or template. |
None
|
storage
|
str | None
|
Explicit storage name such as |
None
|
Returns:
| Type | Description |
|---|---|
str
|
One of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither the declaration nor suffix identifies a layout. |
Source code in common/datasets/slide_embeddings.py
split_csv
¶
Resolve a fold-specific or shared split CSV.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If neither supported split layout exists. |
Source code in common/datasets/slide_embeddings.py
build_slide_embedding_loader
¶
build_slide_embedding_loader(
cfg: Mapping[str, Any],
split: str,
fold: int,
shuffle: bool = True,
) -> DataLoader
Build the shared slide-embedding loader for a run and fold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
Mapping[str, Any]
|
Generated run configuration. |
required |
split
|
str
|
Split name such as |
required |
fold
|
int
|
Outer fold index used to resolve the split path. |
required |
shuffle
|
bool
|
Request shuffling; only training data is shuffled. |
True
|
Returns:
| Type | Description |
|---|---|
DataLoader
|
A data loader yielding slide-vector dictionaries. |