ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
625 stars 161 forks source link

outprefix behaves improperly for motion_correction workflow #372

Closed poldrack closed 1 month ago

poldrack commented 2 years ago

Describe the bug It is sometimes necessary to set an output directory for ants transforms other than /tmp due to space limitations. However, the outprefix option does not seem to work properly with the motion_correction workflow. Instead of creating a different set of transforms for each timepoint, it outputs the same set of transforms for each of the timepoints.

To Reproduce

When the workflow is run with outprefix set, the output files for each step are identical.

In [28]:             mytx = ants.motion_correction(image=chunkdata_ants, fixed=ch1_meanbrain,
        verbose=args.verbose, type_of_transform=args.type_of_transform,
        total_sigma=args.total_sigma, flow_sigma=args.flow_sigma,
        outprefix=os.path.join(args.temp_dir, f'moco_chunk_{i}_'))

Progress:
0%.10%.20%.30%.40%.50%.60%.70%.80%.90%.Done

In [29]: mytx['motion_parameters']
Out[29]: 
[['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat'],
 ['/tmp/jyybadwf/moco_chunk_0_1Warp.nii.gz',
  '/tmp/jyybadwf/moco_chunk_0_0GenericAffine.mat']]

When the flag is unset, then the behavior is correct:

In [26]:  mytx = ants.motion_correction(image=chunkdata_ants, fixed=ch1_meanbrain,
             verbose=args.verbose, type_of_transform=args.type_of_transform,
             total_sigma=args.total_sigma, flow_sigma=args.flow_sigma)

Progress:
0%.10%.20%.30%.40%.50%.60%.70%.80%.90%.Done

In [27]: mytx['motion_parameters']
Out[27]: 
[['/tmp/tmpedejwz_t1Warp.nii.gz', '/tmp/tmpedejwz_t0GenericAffine.mat'],
 ['/tmp/tmpcfz8gmfh1Warp.nii.gz', '/tmp/tmpcfz8gmfh0GenericAffine.mat'],
 ['/tmp/tmpbah4p0_g1Warp.nii.gz', '/tmp/tmpbah4p0_g0GenericAffine.mat'],
 ['/tmp/tmpncsyby1f1Warp.nii.gz', '/tmp/tmpncsyby1f0GenericAffine.mat'],
 ['/tmp/tmp4pbieisd1Warp.nii.gz', '/tmp/tmp4pbieisd0GenericAffine.mat'],
 ['/tmp/tmpddw7ugol1Warp.nii.gz', '/tmp/tmpddw7ugol0GenericAffine.mat'],
 ['/tmp/tmpc6hjh0pu1Warp.nii.gz', '/tmp/tmpc6hjh0pu0GenericAffine.mat'],
 ['/tmp/tmpv4plz0nq1Warp.nii.gz', '/tmp/tmpv4plz0nq0GenericAffine.mat'],
 ['/tmp/tmp9f45yne21Warp.nii.gz', '/tmp/tmp9f45yne20GenericAffine.mat'],
 ['/tmp/tmpu5fmt_d51Warp.nii.gz', '/tmp/tmpu5fmt_d50GenericAffine.mat']]

Expected behavior I expect that the outprefix should simply prepend to the temporay file name (including its random hash).

Proposed solution

I will work on a PR to address this. I think that the easiest solution is to add a tempdir argument to the workflow, and then pass this pass as the prefix to the temporary file generator. (probably also a good idea to update from mktemp to mkstemp since mktemp is deprecated)

ntustison commented 2 years ago

Thanks @poldrack . Much appreciated.

stnava commented 2 years ago

I made a few recent commits that should address this - note : I also normalized images in [0,1] range for the registration itself ... output is like:

...
QQQ_00049_0GenericAffine.mat
QQQ_00050_0GenericAffine.mat
...
cookpa commented 1 month ago

Confirmed fixed as of 0.4.2