Skip to content

Commit

Permalink
14 cropping of regions for big scans (#15)
Browse files Browse the repository at this point in the history
* Bump up version

* Add cropping to relevant region

* Add cropping also for liver vessels

* The min/max was computed incorrectly
  • Loading branch information
giuliabaldini authored Nov 17, 2023
1 parent 1f5ae00 commit 5fb1104
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 20 additions & 6 deletions boa_contrast/features/feature_builder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import traceback
from pathlib import Path
from typing import Any, Dict, Generator, Optional
from typing import Any, Dict, Generator, Optional, Tuple

import cc3d
import cv2
Expand Down Expand Up @@ -54,6 +54,8 @@ def compute_features(
samples: Dict[str, Any] = {}
ct_image = sitk.ReadImage(str(ct_data_path))
ct_data = sitk.GetArrayViewFromImage(ct_image)
assert len(ct_data.shape) == 3, "The data should be a 3D CT scan."

body_region_all = None
if (
not self.one_mask_per_file
Expand Down Expand Up @@ -84,7 +86,7 @@ def compute_features(
region_mask = np.isin(body_region_all, self.label_map[region])
if np.sum(region_mask) == 0:
continue

region_mask, ct_data_region = crop_mask(region_mask, ct_data)
if region in ORGANS:
remove_small_connected_components(region_mask)

Expand All @@ -93,7 +95,7 @@ def compute_features(
new_region = get_pelvis_region(region_mask)
compute_statistics(
samples,
ct_data[new_region],
ct_data_region[new_region],
region_name=f"{region}_pelvis",
)
except Exception:
Expand All @@ -108,13 +110,13 @@ def compute_features(
):
compute_statistics(
samples,
ct_data[partial_region_mask],
ct_data_region[partial_region_mask],
region_name=f"{region}_part{i}",
)
else:
compute_statistics(
samples,
ct_data[region_mask],
ct_data_region[region_mask],
region_name=region,
)

Expand All @@ -123,15 +125,27 @@ def compute_features(
body_regions = sitk.ReadImage(str(liver_vessels_path))
region_mask = sitk.GetArrayViewFromImage(body_regions)
region_mask = region_mask.astype(bool)
region_mask, ct_data_region = crop_mask(region_mask, ct_data)
compute_statistics(
samples,
ct_data[region_mask],
ct_data_region[region_mask],
region_name="liver_vessels",
)
assert len(samples) > 0, f"No regions were found in {segmentation_path.name}."
return samples


def crop_mask(mask: np.ndarray, ct_data: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
coords = np.argwhere(mask)
x_min, y_min, z_min = [coords[:, i].min() for i in range(3)]
x_max, y_max, z_max = [coords[:, i].max() + 1 for i in range(3)]

return (
mask[x_min:x_max, y_min:y_max, z_min:z_max],
ct_data[x_min:x_max, y_min:y_max, z_min:z_max],
)


def create_split_regions(
mask: np.ndarray, n_regions: int = False
) -> Generator[np.ndarray, None, None]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "boa-contrast"
version = "0.1.3"
version = "0.1.4"
description = ""
authors = ["Giulia Baldini <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 5fb1104

Please sign in to comment.