miblab-plot 0.0.10__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.10
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.10"
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,18 +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,
247
+ colors=None,
248
+ show=False,
249
+ verbose=1,
226
250
  ):
227
251
 
228
252
  # Set defaults color window
@@ -231,8 +255,15 @@ def mosaic_overlay(
231
255
  if vmax is None:
232
256
  vmax=np.mean(img) + 2 * np.std(img)
233
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
+
234
264
  # Define RGBA colors (R, G, B, Alpha) — alpha controls transparency
235
- colors = get_distinct_colors(rois, colormap=colormap, opacity=opacity)
265
+ if colors is None:
266
+ colors = get_distinct_colors(rois, colormap=colormap, opacity=opacity)
236
267
 
237
268
  # Get all masks as boolean arrays
238
269
  masks = [m.astype(bool) for m in rois.values()]
@@ -242,7 +273,7 @@ def mosaic_overlay(
242
273
  for i in range(1, len(masks)):
243
274
  all_masks = np.logical_or(all_masks, masks[i])
244
275
  if np.sum(all_masks)==0:
245
- raise ValueError('Empty masks')
276
+ raise ValueError('Empty masks: nothing to overlay here.')
246
277
 
247
278
  # Find corners of cropped mask
248
279
  for x0 in range(all_masks.shape[0]):
@@ -301,7 +332,7 @@ def mosaic_overlay(
301
332
 
302
333
  # Build figure
303
334
  i = 0
304
- for row in tqdm(ax, desc='Building png'):
335
+ for row in tqdm(ax, desc='Building png', disable=verbose==0):
305
336
  for col in row:
306
337
 
307
338
  col.set_xticklabels([])
@@ -330,8 +361,14 @@ def mosaic_overlay(
330
361
  i += 1
331
362
 
332
363
  # fig.suptitle('Mask overlay', fontsize=14)
333
- fig.savefig(file, bbox_inches='tight', pad_inches=0)
334
- 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)
335
372
 
336
373
 
337
374
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: miblab-plot
3
- Version: 0.0.10
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