Skip to content

Commit

Permalink
ENH: Add "itkPointSetTest.py" unittest, testing SetPointsByCoordinates
Browse files Browse the repository at this point in the history
Specifically tested `SetPointsByCoordinates` with a NumPy array as input
argument, as was suggested by Pranjal Sahu.

- Follow-up to pull request InsightSoftwareConsortium#4850
commit f43b1b8
ENH: Add `PointSet::SetPointsByCoordinates(coordinates)`
  • Loading branch information
N-Dekker committed Sep 24, 2024
1 parent e88be6e commit 7df8070
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Modules/Core/Common/wrapping/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ if(ITK_WRAP_PYTHON)
itkPointSetSerializationTest
COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/itkPointSetSerializationTest.py)
itk_python_add_test(
NAME
itkPointSetTest
COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/itkPointSetTest.py)
itk_python_add_test(
NAME
itkSpatialOrientationAdapterTest
Expand Down
52 changes: 52 additions & 0 deletions Modules/Core/Common/wrapping/test/itkPointSetTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# ==========================================================================
#
# Copyright NumFOCUS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==========================================================================*/

import itk
import unittest
import numpy as np

class PointSetTestCase(unittest.TestCase):
"""Tests itk.PointSet"""

def test_SetPointsByCoordinates_from_NumPy_array(self) -> None:
"""Tests the SetPointsByCoordinates method, having a NumPy array as input"""

# The pixel type is irrelevant for this test, just use unsigned char.
PixelType = itk.UC

# Test the most common dimensions, and a few different numbers as number of points.
for dimension in [2, 3]:
for number_of_points in [1, 4, 9]:
# Create a NumPy array of random coordinates between -1.0 and 1.0
np_array = 2.0 * np.random.rand(number_of_points * dimension) - 1.0

point_set = itk.PointSet[PixelType, dimension].New()
point_set.SetPointsByCoordinates(np_array)

points = point_set.GetPoints()

self.assertEqual(points.Size(), number_of_points)

for i in range(number_of_points):
point = points.GetElement(i)
for j in range(dimension):
self.assertAlmostEqual(point[j], np_array[i * dimension + j])


if __name__ == "__main__":
unittest.main()

0 comments on commit 7df8070

Please sign in to comment.