Skip to content

Commit

Permalink
added a cmap function
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenThornquist committed Jan 3, 2025
1 parent 868ea5a commit 11aeda8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
20 changes: 14 additions & 6 deletions siffroi/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

if TYPE_CHECKING:
from .utils.types import PathLike, MaskLike, PolygonLike, ImageShapeLike
from matplotlib.colors import Colormap

def safe_load_attr(f : Union[h5File, Group], attr : str):
""" Returns empty if None """
Expand Down Expand Up @@ -83,14 +84,14 @@ def __init__(self,
if all([x is None for x in [mask, polygon, image_shape]]):
raise NoROIError("ROI must be defined with either a mask or a polygon and image")

if not name is None:
if name is not None:
self._name = name

self.slice_idx = slice_idx
if len(subROIs) > 0:
self.subROIs = subROIs

if not info_string is None:
if info_string is not None:
self.info_string = info_string

if not hasattr(self, 'subROIs'):
Expand All @@ -105,7 +106,7 @@ def center(self, plane : Optional[int] = None)->np.ndarray:
If slice_idx is an int, then plane is ignored.
"""
mask = self.mask
if not (self.slice_idx is None):
if self.slice_idx is not None:
plane = None
if plane is None:
return center_of_mass(mask)
Expand Down Expand Up @@ -148,7 +149,7 @@ def mask(self)->np.ndarray:
Returns a mask of the polygon, True inside and False outside.
Needs an image to define the bounds, if one hasn't been provided to the ROI before
"""
if not (self._mask is None):
if self._mask is not None:
return self._mask.astype(bool)

raise NotImplementedError("Mask from polygon not yet implemented")
Expand All @@ -157,15 +158,15 @@ def mask(self)->np.ndarray:
@property
def shape(self)->tuple[int]:
""" Returns the shape of the ROI mask """
if not (self._mask is None):
if self._mask is not None:
return self._mask.shape

return self._shape

@property
def polygon(self)->np.ndarray:
""" Returns the polygon of the ROI """
if not (self._polygon is None):
if self._polygon is not None:
return self._polygon

raise NotImplementedError("Generating a polygon from mask not yet implemented")
Expand Down Expand Up @@ -202,6 +203,13 @@ def rgba_subrois(self) -> np.ndarray:
as transparent using the heatmap 'turbo'
"""
return masks_to_rgba(self.labeled_subrois)

def rgba_subrois_cmap(self, cmap : 'Colormap') -> np.ndarray:
"""
Labeled_subrois as an RGBA image with the background
as transparent using the specified colormap
"""
return masks_to_rgba(self.labeled_subrois, cmap = cmap)


def save(self, save_path : 'PathLike')->None:
Expand Down
16 changes: 15 additions & 1 deletion siffroi/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
SCT Dec 29 2021
"""
from typing import Iterable, Optional
from typing import Iterable, Optional, TYPE_CHECKING

from matplotlib.path import Path as mplPath
from matplotlib.pyplot import get_cmap
import numpy as np

if TYPE_CHECKING:
from matplotlib.colors import Colormap

def polygon_area(vertices : np.ndarray) -> float:
"""
Computes area of a 2d polygon in n dimensions. Presumes
Expand All @@ -35,6 +38,17 @@ def masks_to_rgba(labeled_image_mask : np.ndarray, cmap_str : str = 'hsv')->np.n
)
return rgba

def masks_to_cmap(labeled_image_mask : np.ndarray, cmap : 'Colormap')->np.ndarray:
"""
Converts a labeled mask to an RGBA image with hsv cmap.
"""

rgba = cmap(
(labeled_image_mask.astype(float)/labeled_image_mask.max()) ,
alpha = labeled_image_mask > 0,
)
return rgba

def nth_largest_shape_in_list(
shapes : list[np.ndarray],
n : int = 1,
Expand Down

0 comments on commit 11aeda8

Please sign in to comment.