miblab-plot 0.0.11__tar.gz → 0.0.12__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: miblab-plot
3
- Version: 0.0.11
3
+ Version: 0.0.12
4
4
  Summary: Plotting utilities for medical images
5
5
  Author-email: Steven Sourbron <s.sourbron@sheffield.ac.uk>
6
6
  License-Expression: Apache-2.0
@@ -6,7 +6,7 @@ requires = ['setuptools>=61.2']
6
6
 
7
7
  [project]
8
8
  name = "miblab-plot"
9
- version = "0.0.11"
9
+ version = "0.0.12"
10
10
  dependencies = [
11
11
  "numpy",
12
12
  "matplotlib",
@@ -24,7 +24,7 @@ def db_mosaic(series_to_display, pngfile, title="Center slices"):
24
24
  aspect_ratio = 16/9
25
25
  nrows = int(np.ceil(np.sqrt(n_imgs / aspect_ratio)))
26
26
  ncols = int(np.ceil(aspect_ratio * nrows))
27
- fig, ax = plt.subplots(nrows=nrows, ncols=ncols, gridspec_kw = {'wspace':0, 'hspace':0}, figsize=(ncols, nrows), dpi=300)
27
+ fig, ax = plt.subplots(nrows=nrows, ncols=ncols, squeeze=False, gridspec_kw = {'wspace':0, 'hspace':0}, figsize=(ncols, nrows), dpi=300)
28
28
 
29
29
  i=0
30
30
  for row in tqdm(ax, desc='Building png'):
@@ -211,19 +211,42 @@ def checkerboard(fixed: np.ndarray, coreg: np.ndarray, square_size: int) -> np.n
211
211
 
212
212
  return checker
213
213
 
214
-
214
+ def _label_to_binary_masks(label_image, background_value=0):
215
+ """
216
+ Converts a label image into a list of binary masks.
217
+
218
+ Parameters:
219
+ - label_image (np.ndarray): 2D array of labels.
220
+ - background_value (int): Value representing the background to ignore.
221
+
222
+ Returns:
223
+ - dict: A dict of label: 2D binary mask pairs.
224
+ """
225
+ # Find all unique labels excluding the background
226
+ unique_labels = np.unique(label_image)
227
+ unique_labels = unique_labels[unique_labels != background_value]
228
+
229
+ masks = {}
230
+ for label in unique_labels:
231
+ # Create a binary mask for the current label
232
+ mask = (label_image == label).astype(bool)
233
+ masks[label] = mask
234
+
235
+ return masks
215
236
 
216
237
  def mosaic_overlay(
217
- img,
218
- rois,
219
- file,
238
+ img: np.ndarray, # background greyscale image
239
+ rois: dict | np.ndarray, # Dictionary with masks or label image
240
+ file: str=None,
220
241
  colormap='tab20',
221
242
  aspect_ratio=1.8,
222
243
  margin=None,
223
244
  vmin=None,
224
245
  vmax=None,
225
246
  opacity=0.6,
226
- colors=None
247
+ colors=None,
248
+ show=False,
249
+ verbose=1,
227
250
  ):
228
251
 
229
252
  # Set defaults color window
@@ -232,6 +255,12 @@ def mosaic_overlay(
232
255
  if vmax is None:
233
256
  vmax=np.mean(img) + 2 * np.std(img)
234
257
 
258
+ # Convert label image to dictionary of masks
259
+ if not isinstance(rois, dict):
260
+ rois = _label_to_binary_masks(rois, background_value=0)
261
+ if rois == {}:
262
+ raise ValueError("Empty label image: nothing to overlay here..")
263
+
235
264
  # Define RGBA colors (R, G, B, Alpha) — alpha controls transparency
236
265
  if colors is None:
237
266
  colors = get_distinct_colors(rois, colormap=colormap, opacity=opacity)
@@ -244,7 +273,7 @@ def mosaic_overlay(
244
273
  for i in range(1, len(masks)):
245
274
  all_masks = np.logical_or(all_masks, masks[i])
246
275
  if np.sum(all_masks)==0:
247
- raise ValueError('Empty masks')
276
+ raise ValueError('Empty masks: nothing to overlay here.')
248
277
 
249
278
  # Find corners of cropped mask
250
279
  for x0 in range(all_masks.shape[0]):
@@ -303,7 +332,7 @@ def mosaic_overlay(
303
332
 
304
333
  # Build figure
305
334
  i = 0
306
- for row in tqdm(ax, desc='Building png'):
335
+ for row in tqdm(ax, desc='Building png', disable=verbose==0):
307
336
  for col in row:
308
337
 
309
338
  col.set_xticklabels([])
@@ -332,8 +361,14 @@ def mosaic_overlay(
332
361
  i += 1
333
362
 
334
363
  # fig.suptitle('Mask overlay', fontsize=14)
335
- fig.savefig(file, bbox_inches='tight', pad_inches=0)
336
- plt.close()
364
+ if file is None:
365
+ plt.show()
366
+ else:
367
+ fig.savefig(file, bbox_inches='tight', pad_inches=0)
368
+ if show:
369
+ plt.show()
370
+ else:
371
+ plt.close(fig)
337
372
 
338
373
 
339
374
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: miblab-plot
3
- Version: 0.0.11
3
+ Version: 0.0.12
4
4
  Summary: Plotting utilities for medical images
5
5
  Author-email: Steven Sourbron <s.sourbron@sheffield.ac.uk>
6
6
  License-Expression: Apache-2.0
File without changes
File without changes
File without changes