Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: check for transform instance before writing to file #636

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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