diff --git a/ants/core/ants_transform_io.py b/ants/core/ants_transform_io.py index 3fa127d7..22a6ea5c 100644 --- a/ants/core/ants_transform_io.py +++ b/ants/core/ants_transform_io.py @@ -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) diff --git a/tests/test_core_ants_transform_io.py b/tests/test_core_ants_transform_io.py index 025685ec..9d3b0462 100644 --- a/tests/test_core_ants_transform_io.py +++ b/tests/test_core_ants_transform_io.py @@ -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__':