From 29f4984b978352fc2af619309be84be31482b6e7 Mon Sep 17 00:00:00 2001 From: "Hein, Simon" Date: Tue, 12 Mar 2024 09:27:35 +0100 Subject: [PATCH 1/2] Closes #917: points_to_spheres should now adds spheres in 3D correctly --- src/porespy/tools/_sphere_insertions.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/porespy/tools/_sphere_insertions.py b/src/porespy/tools/_sphere_insertions.py index 9b5d257ae..9bb489687 100644 --- a/src/porespy/tools/_sphere_insertions.py +++ b/src/porespy/tools/_sphere_insertions.py @@ -35,7 +35,7 @@ def points_to_spheres(im): from scipy.spatial import distance_matrix if im.ndim == 3: x, y, z = np.where(im > 0) - coords = np.vstack((x, y, z)).T + coords = np.vstack((x, y, z)) else: x, y = np.where(im > 0) coords = np.vstack((x, y)) @@ -45,7 +45,10 @@ def points_to_spheres(im): dmap[mask] = np.inf r = np.around(dmap.min(axis=0)/2, decimals=0).astype(int) else: - r = im[x, y].flatten() + if im.ndim == 3: + r = im[x, y, z].flatten() + else: + r = im[x, y].flatten() im_spheres = np.zeros_like(im, dtype=bool) im_spheres = _insert_disks_at_points_parallel( im_spheres, From 5487fe58b33396837516f5235423137b0cb2bda6 Mon Sep 17 00:00:00 2001 From: "Hein, Simon" Date: Wed, 13 Mar 2024 15:34:57 +0100 Subject: [PATCH 2/2] Added test cases for points_to_spheres in 2D and 3D. ! dtype=bool for im is still NOT TESTED ! --- test/unit/test_tools.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unit/test_tools.py b/test/unit/test_tools.py index ce302352c..5380c7635 100644 --- a/test/unit/test_tools.py +++ b/test/unit/test_tools.py @@ -428,6 +428,18 @@ def test_tic_toc(self): t = toc(quiet=True) assert t > 1 + def test_points_to_spheres_3D(self): + im=np.full((41,41,41),0) + im[20,20,20]=10 + res_ps_3D=ps.tools.points_to_spheres(im=im) + assert np.sum(res_ps_3D) == 4169 + + def test_points_to_spheres_2D(self): + im=np.full((41,41),0) + im[20,20]=10 + res_ps_2D=ps.tools.points_to_spheres(im=im) + assert np.sum(res_ps_2D) == 317 + if __name__ == '__main__': t = ToolsTest()