Skip to content

Commit b7c0e44

Browse files
Copilotthewtex
andcommitted
Address code review feedback
- Remove print statements from tests - Use np.zeros instead of np.random.randint - Add image_size variable to avoid magic numbers - Fix array dimension assertions (NumPy row-major vs ITK column-major) - Improve comment explaining list unpacking logic in set_inputs Co-authored-by: thewtex <[email protected]>
1 parent 04d1168 commit b7c0e44

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Wrapping/Generators/Python/Tests/test_imread_single_file_list.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,27 @@ def test_imread_single_element_list():
3838

3939
try:
4040
# Create and save a test image
41-
arr = np.random.randint(0, 256, (10, 10), dtype=np.uint8)
41+
image_size = (8, 10)
42+
arr = np.zeros(image_size, dtype=np.uint8)
4243
image = itk.image_from_array(arr)
4344
itk.imwrite(image, filename)
4445

4546
# Test 1: imread with a string (baseline - should always work)
4647
img_from_string = itk.imread(filename)
4748
assert img_from_string is not None
48-
assert itk.size(img_from_string)[0] == 10
49-
assert itk.size(img_from_string)[1] == 10
49+
# NumPy shape (8, 10) becomes ITK size [10, 8] due to row/column-major ordering
50+
assert itk.size(img_from_string)[0] == image_size[1]
51+
assert itk.size(img_from_string)[1] == image_size[0]
5052

5153
# Test 2: imread with a single-element list (the bug case)
5254
img_from_list = itk.imread([filename])
5355
assert img_from_list is not None
5456
# When reading as a series, dimension is increased by 1
5557
assert img_from_list.GetImageDimension() == 3
56-
assert itk.size(img_from_list)[0] == 10
57-
assert itk.size(img_from_list)[1] == 10
58+
assert itk.size(img_from_list)[0] == image_size[1]
59+
assert itk.size(img_from_list)[1] == image_size[0]
5860
assert itk.size(img_from_list)[2] == 1
5961

60-
print("✓ Test passed: imread works with single-element list")
61-
6262
finally:
6363
# Cleanup
6464
if os.path.exists(filename):
@@ -76,7 +76,8 @@ def test_imread_multi_element_list():
7676

7777
try:
7878
# Create and save test images
79-
arr = np.random.randint(0, 256, (10, 10), dtype=np.uint8)
79+
image_size = (8, 10)
80+
arr = np.zeros(image_size, dtype=np.uint8)
8081
image = itk.image_from_array(arr)
8182
itk.imwrite(image, filename1)
8283
itk.imwrite(image, filename2)
@@ -86,12 +87,11 @@ def test_imread_multi_element_list():
8687
assert img_from_list is not None
8788
# When reading as a series, dimension is increased by 1
8889
assert img_from_list.GetImageDimension() == 3
89-
assert itk.size(img_from_list)[0] == 10
90-
assert itk.size(img_from_list)[1] == 10
90+
# NumPy shape (8, 10) becomes ITK size [10, 8] due to row/column-major ordering
91+
assert itk.size(img_from_list)[0] == image_size[1]
92+
assert itk.size(img_from_list)[1] == image_size[0]
9193
assert itk.size(img_from_list)[2] == 2
9294

93-
print("✓ Test passed: imread works with multi-element list")
94-
9595
finally:
9696
# Cleanup
9797
if os.path.exists(filename1):
@@ -105,4 +105,3 @@ def test_imread_multi_element_list():
105105
if __name__ == "__main__":
106106
test_imread_single_element_list()
107107
test_imread_multi_element_list()
108-
print("\n✓ All imread list tests passed!")

Wrapping/Generators/Python/itk/support/extras.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,11 @@ def SetInputs(self, *args, **kargs):
16811681
import itk
16821682

16831683
if type(value) in [list, tuple]:
1684-
# Special case for FileNames which expects a container, not unpacked arguments
1684+
# Special case for FileNames which expects a container, not unpacked arguments.
1685+
# Most ITK Set* methods that take multiple values expect them as separate arguments
1686+
# (e.g., SetSpacing(x, y, z)), so lists are unpacked with *.
1687+
# However, SetFileNames expects a single container argument (vector<string>),
1688+
# so we pass the list directly without unpacking.
16851689
if attribName == "FileNames":
16861690
attrib(value)
16871691
else:

0 commit comments

Comments
 (0)