From 56f7422cf4b03569f4e9f604f108c3d1a478045e Mon Sep 17 00:00:00 2001 From: Lee Newberg Date: Mon, 27 Mar 2023 13:05:46 -0400 Subject: [PATCH 1/2] PERF: Check for mask non-zero more efficiently --- histomics_stream/configure.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/histomics_stream/configure.py b/histomics_stream/configure.py index e6fe138..5351480 100644 --- a/histomics_stream/configure.py +++ b/histomics_stream/configure.py @@ -614,9 +614,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) From d135e90d99441fc0d081761506db629ec738045d Mon Sep 17 00:00:00 2001 From: Lee Newberg Date: Mon, 27 Mar 2023 13:19:21 -0400 Subject: [PATCH 2/2] PERF: Move functionality to new TilesByGridAndMask.check_mask_filename, for profiling --- histomics_stream/configure.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/histomics_stream/configure.py b/histomics_stream/configure.py index 5351480..5012de0 100644 --- a/histomics_stream/configure.py +++ b/histomics_stream/configure.py @@ -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 @@ -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 (