Skip to content

Commit 152ed00

Browse files
committed
ENH: Add a useful __repr__ for itkMatrix
1 parent f56cc7e commit 152ed00

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Wrapping/Generators/Python/PyBase/pyBase.i

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ str = str
554554
vnl_reference = self.__GetVnlMatrix_orig__()
555555
vnl_copy = type(vnl_reference)(vnl_reference)
556556
return vnl_copy
557+
def __repr__(self):
558+
vnl_mat = self.GetVnlMatrix()
559+
python_list_mat = [
560+
[vnl_mat.get(i, j) for j in range(vnl_mat.cols())]
561+
for i in range(vnl_mat.rows())
562+
]
563+
return repr(type(self)).split("'")[1] + "(" + repr(python_list_mat) + ")"
557564
%}
558565
}
559566

Wrapping/Generators/Python/Tests/extras.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ def custom_callback(name, progress):
395395
assert arr2[0, 0] == 2
396396
# and make sure that the matrix hasn't changed.
397397
assert m_itk(0, 0) == 1
398+
# Test __repr__
399+
assert repr(m_itk) == "itk.itkMatrixPython.itkMatrixD33([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])"
398400

399401
# test .astype for itk.Image
400402
numpyImage = np.random.randint(0, 256, (8, 12, 5)).astype(np.uint8)

0 commit comments

Comments
 (0)