From 18a53cf1e5d097413e1d3f34f7e9c91695890daa Mon Sep 17 00:00:00 2001
From: fima <fima@dtu.dk>
Date: Thu, 20 Jun 2024 15:57:30 +0200
Subject: [PATCH] Pre workshop refactoring

---
 qim3d/__init__.py             |  6 ++++--
 qim3d/viz/detection.py        |  4 ++--
 qim3d/viz/img.py              | 14 ++++++++++++++
 qim3d/viz/structure_tensor.py |  7 ++++---
 setup.py                      |  2 +-
 5 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/qim3d/__init__.py b/qim3d/__init__.py
index ebb4e7b2..cd6ab5c2 100644
--- a/qim3d/__init__.py
+++ b/qim3d/__init__.py
@@ -8,14 +8,16 @@ Documentation available at https://platform.qim.dk/qim3d/
 
 """
 
-__version__ = "0.3.7"
+__version__ = "0.3.8"
 
 from . import io
 from . import gui
 from . import viz
 from . import utils
 from . import processing
-from . import models
+
+# commented out to avoid torch import
+# from . import models
 
 examples = io.ImgExamples()
 io.logger.set_level_info()
diff --git a/qim3d/viz/detection.py b/qim3d/viz/detection.py
index f6ff1bf1..3e83a458 100644
--- a/qim3d/viz/detection.py
+++ b/qim3d/viz/detection.py
@@ -5,6 +5,7 @@ import ipywidgets as widgets
 from IPython.display import clear_output, display
 import qim3d
 
+
 def circles(blobs, vol, alpha=0.5, color="#ff9900", **kwargs):
     """
     Plots the blobs found on a slice of the volume.
@@ -32,10 +33,9 @@ def circles(blobs, vol, alpha=0.5, color="#ff9900", **kwargs):
             vol,
             n_slices=1,
             position=z_slice,
-            img_height=3,
-            img_width=3,
             cmap="gray",
             show_position=False,
+            **kwargs
         )
         # Add circles from deteced blobs
         for detected in blobs:
diff --git a/qim3d/viz/img.py b/qim3d/viz/img.py
index f2293565..f8b9bfee 100644
--- a/qim3d/viz/img.py
+++ b/qim3d/viz/img.py
@@ -232,6 +232,7 @@ def slices(
     show: bool = False,
     show_position: bool = True,
     interpolation: Optional[str] = "none",
+    img_size = None,
     **imshow_kwargs,
 ) -> plt.Figure:
     """Displays one or several slices from a 3d volume.
@@ -271,6 +272,9 @@ def slices(
         ```
         ![Grid of slices](assets/screenshots/viz-slices.png)
     """
+    if img_size:
+        img_height = img_size
+        img_width = img_size
 
     # Numpy array or Torch tensor input
     if not isinstance(vol, (np.ndarray, torch.Tensor, da.core.Array)):
@@ -409,6 +413,7 @@ def slicer(
     img_width: int = 3,
     show_position: bool = False,
     interpolation: Optional[str] = "none",
+    img_size = None,
     **imshow_kwargs,
 ) -> widgets.interactive:
     """Interactive widget for visualizing slices of a 3D volume.
@@ -435,6 +440,10 @@ def slicer(
         ![viz slicer](assets/screenshots/viz-slicer.gif)
     """
 
+    if img_size:
+        img_height = img_size
+        img_width = img_size
+
     # Create the interactive widget
     def _slicer(position):
         fig = slices(
@@ -472,6 +481,7 @@ def orthogonal(
     img_width: int = 3,
     show_position: bool = False,
     interpolation: Optional[str] = None,
+    img_size = None,
 ):
     """Interactive widget for visualizing orthogonal slices of a 3D volume.
 
@@ -496,6 +506,10 @@ def orthogonal(
         ![viz orthogonal](assets/screenshots/viz-orthogonal.gif)
     """
 
+    if img_size:
+        img_height = img_size
+        img_width = img_size
+
     z_slicer = slicer(
         vol,
         axis=0,
diff --git a/qim3d/viz/structure_tensor.py b/qim3d/viz/structure_tensor.py
index 77265aa6..fde3457b 100644
--- a/qim3d/viz/structure_tensor.py
+++ b/qim3d/viz/structure_tensor.py
@@ -5,6 +5,7 @@ from matplotlib.gridspec import GridSpec
 import ipywidgets as widgets
 import logging as log
 
+
 def vectors(
     volume: np.ndarray,
     vec: np.ndarray,
@@ -48,7 +49,7 @@ def vectors(
         # Visualize the structure tensor
         qim3d.viz.vectors(vol, vec, axis=2, slice_idx=0.5, interactive=True)
         ```
-        ![structure tensor](assets/screenshots/structure_tensor.gif)  
+        ![structure tensor](assets/screenshots/structure_tensor.gif)
 
     """
 
@@ -120,10 +121,10 @@ def vectors(
         # Orientations histogram
         nbins = 36
         angles = np.arctan2(vectors_slice_y, vectors_slice_x)  # angles from 0 to pi
-        distribution, bin_edges = np.histogram(angles, bins=nbins, range=(0.0, np.pi))
+        distribution, bin_edges = np.histogram(angles, bins=nbins, range=(0, np.pi))
 
         # Find the bin with the maximum count
-        peak_bin_idx = np.argmax(distribution)
+        peak_bin_idx = np.argmax(abs(distribution))
         # Calculate the center of the peak bin
         peak_angle_rad = (bin_edges[peak_bin_idx] + bin_edges[peak_bin_idx + 1]) / 2
         # Convert the peak angle to degrees
diff --git a/setup.py b/setup.py
index 5efe335a..bacc5a95 100644
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,7 @@ with open("README.md", "r", encoding="utf-8") as f:
 
 setup(
     name="qim3d",
-    version="0.3.7",
+    version="0.3.8",
     author="Felipe Delestro",
     author_email="fima@dtu.dk",
     description="QIM tools and user interfaces",
-- 
GitLab