Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
cleaned feature description and manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
moerlemans committed Sep 24, 2024
1 parent ea8a330 commit 88dff49
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 81 deletions.
5 changes: 0 additions & 5 deletions ahcore/utils/database_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,12 @@ class FeatureDescription(Base):
tile_size_height = Column(Integer)
tile_overlap_width = Column(Integer)
tile_overlap_height = Column(Integer)
description = Column(String)

# use this to select which features we want to use
version = Column(String, unique=True, nullable=False)

model_name = Column(String)
model_path = Column(String)
feature_dimension = Column(Integer)
image_transforms_description = Column(String)
# it would be nice to have a way to track which transforms the feature extractors used,
# but maybe this is not the best way to do it

features: Mapped[List["ImageFeature"]] = relationship("ImageFeature", back_populates="feature_description")

Expand Down
141 changes: 65 additions & 76 deletions ahcore/utils/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,7 @@ def get_labels_from_record(record: Image | Patient) -> list[tuple[str, str]] | N

def get_relevant_feature_info_from_record(
record: ImageFeature, data_description: DataDescription, feature_description: FeatureDescription
) -> tuple[
Path,
PositiveFloat,
tuple[PositiveInt, PositiveInt],
tuple[PositiveInt, PositiveInt],
ImageBackend,
PositiveFloat,
]:
) -> ImageInfoDict:
"""Get the features from a record of type Image.
Parameters
Expand All @@ -200,7 +193,68 @@ def get_relevant_feature_info_from_record(

backend = ImageBackend[str(record.reader)].value
overwrite_mpp = float(feature_description.mpp)
return image_path, mpp, tile_size, tile_overlap, backend, overwrite_mpp

output_dict: ImageInfoDict = {
"image_path": image_path,
"tile_size": tile_size,
"tile_overlap": tile_overlap,
"backend": backend,
"mpp": mpp,
"overwrite_mpp": overwrite_mpp,
"tile_mode": TilingMode.skip,
"output_tile_size": None,
"mask": None,
"mask_threshold": None,
"rois": None,
"annotations": None,
}

return output_dict


def get_relevant_image_info_from_record(
image: Image, data_description: DataDescription, annotations_root: Path, stage: str
) -> ImageInfoDict:
if stage == "fit":
grid_description = data_description.training_grid
else:
grid_description = data_description.inference_grid

if grid_description is None:
raise ValueError(f"Grid (for stage {stage}) is not defined in the data description.")

mask, annotations = get_mask_and_annotations_from_record(annotations_root, image)
assert isinstance(mask, WsiAnnotations) or (mask is None) or isinstance(mask, SlideImage)
mask_threshold = 0.0 if stage != "fit" else data_description.mask_threshold
rois = _get_rois(mask, data_description, stage)

image_path = data_description.data_dir / image.filename
tile_size = grid_description.tile_size
tile_overlap = grid_description.tile_overlap
backend = DLUPImageBackend[str(image.reader)]
mpp = getattr(grid_description, "mpp", 1.0)
overwrite_mpp = float(image.mpp)
tile_mode = (
TilingMode(data_description.tiling_mode) if data_description.tiling_mode is not None else TilingMode.overflow
)
output_tile_size = getattr(grid_description, "output_tile_size", None)

output_dict: ImageInfoDict = {
"image_path": image_path,
"tile_size": tile_size,
"tile_overlap": tile_overlap,
"backend": backend,
"mpp": mpp,
"overwrite_mpp": overwrite_mpp,
"tile_mode": tile_mode,
"output_tile_size": output_tile_size,
"mask": mask,
"mask_threshold": mask_threshold,
"rois": rois,
"annotations": annotations,
}

return output_dict


def _get_rois(mask: Optional[_AnnotationReturnTypes], data_description: DataDescription, stage: str) -> Optional[Rois]:
Expand Down Expand Up @@ -450,79 +504,14 @@ def get_image_info(
# Directly return the initialized dictionary with None values
return image_info

(
image_path,
mpp,
tile_size,
tile_overlap,
backend,
overwrite_mpp,
) = get_relevant_feature_info_from_record(image_feature, data_description, feature_description)

# Update the dictionary with the actual values
image_info.update(
{
"image_path": image_path,
"tile_size": tile_size,
"tile_overlap": tile_overlap,
"backend": backend,
"mpp": mpp,
"overwrite_mpp": overwrite_mpp,
"tile_mode": TilingMode.skip,
"output_tile_size": None,
"mask": None,
"mask_threshold": None,
"rois": None,
"annotations": None,
}
)
image_info.update(get_relevant_feature_info_from_record(image_feature, data_description, feature_description))

return image_info

else:
if stage == "fit":
grid_description = data_description.training_grid
else:
grid_description = data_description.inference_grid

if grid_description is None:
raise ValueError(f"Grid (for stage {stage}) is not defined in the data description.")

mask, annotations = get_mask_and_annotations_from_record(annotations_root, image)
assert isinstance(mask, WsiAnnotations) or (mask is None) or isinstance(mask, SlideImage)
mask_threshold = 0.0 if stage != "fit" else data_description.mask_threshold
rois = _get_rois(mask, data_description, stage)

image_path = data_description.data_dir / image.filename
tile_size = grid_description.tile_size
tile_overlap = grid_description.tile_overlap
backend = DLUPImageBackend[str(image.reader)]
mpp = getattr(grid_description, "mpp", 1.0)
overwrite_mpp = float(image.mpp)
tile_mode = (
TilingMode(data_description.tiling_mode)
if data_description.tiling_mode is not None
else TilingMode.overflow
)
output_tile_size = getattr(grid_description, "output_tile_size", None)

# Update the dictionary with the actual values
image_info.update(
{
"image_path": image_path,
"tile_size": tile_size,
"tile_overlap": tile_overlap,
"backend": backend,
"mpp": mpp,
"overwrite_mpp": overwrite_mpp,
"tile_mode": tile_mode,
"output_tile_size": output_tile_size,
"mask": mask,
"mask_threshold": mask_threshold,
"rois": rois,
"annotations": annotations,
}
)
image_info.update(get_relevant_image_info_from_record(image, data_description, annotations_root, stage))

return image_info

Expand Down

0 comments on commit 88dff49

Please sign in to comment.