Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions histomics_stream/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,7 @@ def __init__(self, study, **kwargs):
f' tile_width ({study["tile_width"]}).'
)
if mask_filename != "":
mask_itk = itk.imread(mask_filename) # May throw exception
if mask_itk.GetImageDimension() != 2:
raise ValueError(
f"The mask ({mask_filename}) should be a 2-dimensional image."
)
mask_itk = self.check_mask_filename(mask_filename)
if not (
isinstance(mask_threshold, float)
and mask_threshold >= 0.0
Expand Down Expand Up @@ -587,6 +583,14 @@ def __call__(self, slide):
random.sample(slide["tiles"].items(), self.randomly_select)
)

def check_mask_filename(self, mask_filename):
mask_itk = itk.imread(mask_filename) # May throw exception
if mask_itk.GetImageDimension() != 2:
raise ValueError(
f"The mask ({mask_filename}) should be a 2-dimensional image."
)
return mask_itk

def compute_from_mask(self, top_left):
# Check that the input and output aspect ratios are pretty close
if (
Expand Down Expand Up @@ -614,9 +618,8 @@ def compute_from_mask(self, top_left):
cumulative_mask = np.zeros(
(self.mask_height + 2, self.mask_width + 2), dtype=np.int64
)
nonzero = np.vectorize(lambda x: int(x != 0))
cumulative_mask[1 : self.mask_height + 1, 1 : self.mask_width + 1] = nonzero(
itk.GetArrayViewFromImage(self.mask_itk)
cumulative_mask[1 : self.mask_height + 1, 1 : self.mask_width + 1] = (
itk.GetArrayViewFromImage(self.mask_itk).astype(bool).astype(np.int64)
)
cumulative_mask = np.cumsum(np.cumsum(cumulative_mask, axis=0), axis=1)

Expand Down