Skip to content

Commit 7df8070

Browse files
committed
ENH: Add "itkPointSetTest.py" unittest, testing SetPointsByCoordinates
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)`
1 parent e88be6e commit 7df8070

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Modules/Core/Common/wrapping/test/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ if(ITK_WRAP_PYTHON)
4747
itkPointSetSerializationTest
4848
COMMAND
4949
${CMAKE_CURRENT_SOURCE_DIR}/itkPointSetSerializationTest.py)
50+
itk_python_add_test(
51+
NAME
52+
itkPointSetTest
53+
COMMAND
54+
${CMAKE_CURRENT_SOURCE_DIR}/itkPointSetTest.py)
5055
itk_python_add_test(
5156
NAME
5257
itkSpatialOrientationAdapterTest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# ==========================================================================
2+
#
3+
# Copyright NumFOCUS
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0.txt
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# ==========================================================================*/
18+
19+
import itk
20+
import unittest
21+
import numpy as np
22+
23+
class PointSetTestCase(unittest.TestCase):
24+
"""Tests itk.PointSet"""
25+
26+
def test_SetPointsByCoordinates_from_NumPy_array(self) -> None:
27+
"""Tests the SetPointsByCoordinates method, having a NumPy array as input"""
28+
29+
# The pixel type is irrelevant for this test, just use unsigned char.
30+
PixelType = itk.UC
31+
32+
# Test the most common dimensions, and a few different numbers as number of points.
33+
for dimension in [2, 3]:
34+
for number_of_points in [1, 4, 9]:
35+
# Create a NumPy array of random coordinates between -1.0 and 1.0
36+
np_array = 2.0 * np.random.rand(number_of_points * dimension) - 1.0
37+
38+
point_set = itk.PointSet[PixelType, dimension].New()
39+
point_set.SetPointsByCoordinates(np_array)
40+
41+
points = point_set.GetPoints()
42+
43+
self.assertEqual(points.Size(), number_of_points)
44+
45+
for i in range(number_of_points):
46+
point = points.GetElement(i)
47+
for j in range(dimension):
48+
self.assertAlmostEqual(point[j], np_array[i * dimension + j])
49+
50+
51+
if __name__ == "__main__":
52+
unittest.main()

0 commit comments

Comments
 (0)