Skip to content

Commit

Permalink
ENH: Add a useful __repr__ for itkMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
HastingsGreer committed Oct 16, 2021
1 parent f56cc7e commit 152ed00
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Wrapping/Generators/Python/PyBase/pyBase.i
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,13 @@ str = str
vnl_reference = self.__GetVnlMatrix_orig__()
vnl_copy = type(vnl_reference)(vnl_reference)
return vnl_copy
def __repr__(self):
vnl_mat = self.GetVnlMatrix()
python_list_mat = [
[vnl_mat.get(i, j) for j in range(vnl_mat.cols())]
for i in range(vnl_mat.rows())
]
return repr(type(self)).split("'")[1] + "(" + repr(python_list_mat) + ")"
%}
}
Expand Down
2 changes: 2 additions & 0 deletions Wrapping/Generators/Python/Tests/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ def custom_callback(name, progress):
assert arr2[0, 0] == 2
# and make sure that the matrix hasn't changed.
assert m_itk(0, 0) == 1
# Test __repr__
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]])"

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

0 comments on commit 152ed00

Please sign in to comment.