Skip to content
Snippets Groups Projects

Threshold exploration

Closed s212246 requested to merge threshold-exploration into main
+ 39
21
@@ -5,7 +5,7 @@ Provides a collection of visualization functions.
import math
import warnings
from typing import List, Optional, Union
from typing import List, Optional, Union, Tuple
import dask.array as da
import ipywidgets as widgets
@@ -818,21 +818,22 @@ def chunks(zarr_path: str, **kwargs):
def histogram(
vol: np.ndarray,
bins: Union[int, str] = "auto",
slice_idx: Union[int, str] = None,
slice_idx: Union[int, str, None] = None,
threshold: int = None,
axis: int = 0,
kde: bool = True,
log_scale: bool = False,
despine: bool = True,
show_title: bool = True,
color="qim3d",
edgecolor=None,
figsize=(8, 4.5),
element="step",
return_fig=False,
show=True,
ax=None, # New parameter for target axes
**sns_kwargs,
):
color: str = "qim3d",
edgecolor: Optional[str] = None,
figsize: Tuple[float, float] = (8, 4.5),
element: str = "step",
return_fig: bool = False,
show: bool = True,
ax: Optional[plt.Axes] = None,
**sns_kwargs: Union[str, float, int, bool]
) -> Optional[Union[plt.Figure, plt.Axes]]:
"""
Plots a histogram of voxel intensities from a 3D volume, with options to show a specific slice or the entire volume.
@@ -909,8 +910,28 @@ def histogram(
**sns_kwargs,
)
if threshold is not None:
ax.axvline(
x=threshold,
color='red',
linestyle="--",
linewidth=2,
label=f"Threshold = {round(threshold)}"
)
ax.legend()
if despine:
sns.despine(ax=ax, top=True, right=True)
sns.despine(
fig=None,
ax=ax,
top=True,
right=True,
left=False,
bottom=False,
offset={"left": 0, "bottom": 18},
trim=True,
)
ax.set_xlabel("Voxel Intensity")
ax.set_ylabel("Frequency")
@@ -933,7 +954,7 @@ def threshold(
cmap_overlay: str = 'gray',
vmin: float = None,
vmax: float = None,
):
) -> widgets.VBox:
"""
This function provides an interactive interface to explore thresholding on a
3D volume slice-by-slice. Users can either manually set the threshold value
@@ -1036,19 +1057,14 @@ def threshold(
vol=volume,
bins=32,
slice_idx=position,
threshold=threshold,
axis=1,
kde=False,
ax=axes[1],
show=False,
)
axes[1].axvline(
x=threshold,
color="red",
linestyle="--",
linewidth=2,
label=f"Threshold = {threshold}",
)
axes[1].set_title("Histogram")
axes[1].set_title(f'Histogram')
# Compute and add the binary mask to the plot
mask = slice_img > threshold
@@ -1056,6 +1072,8 @@ def threshold(
axes[2].set_title('Binary mask')
axes[2].axis('off')
# both mask and img should be rgb
# mask data in first channel and then black the other sure --> no cmap_overlay
# Compute and add the overlay to the plot
masked_volume = qim3d.processing.operations.overlay_rgb_images(
background = slice_img,
Loading