From 152ed0028f38c770d6cc920bb84a63990ecb486c Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Fri, 15 Oct 2021 21:33:06 -0400 Subject: [PATCH] ENH: Add a useful __repr__ for itkMatrix --- Wrapping/Generators/Python/PyBase/pyBase.i | 7 +++++++ Wrapping/Generators/Python/Tests/extras.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/Wrapping/Generators/Python/PyBase/pyBase.i b/Wrapping/Generators/Python/PyBase/pyBase.i index 7c48a1db038..7c35c0c869a 100644 --- a/Wrapping/Generators/Python/PyBase/pyBase.i +++ b/Wrapping/Generators/Python/PyBase/pyBase.i @@ -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) + ")" %} } diff --git a/Wrapping/Generators/Python/Tests/extras.py b/Wrapping/Generators/Python/Tests/extras.py index 16419b2ab11..bb6bc5b19fb 100644 --- a/Wrapping/Generators/Python/Tests/extras.py +++ b/Wrapping/Generators/Python/Tests/extras.py @@ -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)