Skip to content

Commit

Permalink
Merge pull request #636 from ANTsX/test-write-transform
Browse files Browse the repository at this point in the history
ENH: check for transform instance before writing to file
  • Loading branch information
ncullen93 committed May 17, 2024
2 parents eb2045a + 98da26f commit 1c81298
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ants/core/ants_transform_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ def write_transform(transform, filename):
>>> ants.write_transform(tx, '~/desktop/tx.mat')
>>> tx2 = ants.read_transform('~/desktop/tx.mat')
"""
if not isinstance(transform, tio.ANTsTransform):
raise Exception('Only ANTsTransform instances can be written to file. Check that you are not passing in a filepath to a saved transform.')

filename = os.path.expanduser(filename)
libfn = utils.get_lib_fn("writeTransform")
libfn(transform.pointer, filename)
6 changes: 5 additions & 1 deletion tests/test_core_ants_transform_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ def test_to_displacement(self):
mytx = ants.registration(fixed=fi, moving=mi, type_of_transform = ('SyN') )
vec = ants.image_read( mytx['fwdtransforms'][0] )
atx = ants.transform_from_displacement_field( vec )
field = ants.transform_to_displacement_field( atx, fi )
field = ants.transform_to_displacement_field( atx, fi )

def test_catch_error(self):
with self.assertRaises(Exception):
ants.write_transform(123, 'test.mat')


if __name__ == '__main__':
Expand Down

0 comments on commit 1c81298

Please sign in to comment.