ANTsX/ANTsPy

build_template creates "temporary files" but doesn't remove them

Closed this issue · 2 comments

I was running build_template on the Calgary Campinas dataset and suddenly noticed my disk was full.

Describe the bug

Looking at the code, I see a for each image in the image_list, a registration with no outprefix is run. When outprefix is not set, a temporary file name is created using mktemp. The temporary files saved by the registration are not deleted.

To reproduce

Run with large dataset and compare the disk space before/after.

Expected behavior

I would expect that temporary registration results are deleted, unless a "working directory" is specified by a user. In this case it would be nice to save the registration files in a structured way, e.g.

  • working dir
    • img000/outWarp.nii.gz, etc.
    • img001/outWarp.nii.gz, etc.
    • img002/outWarp.nii.gz, etc.

Screenshots

na

ANTsPy installation (please complete the following information):

  • Hardware: PC
  • OS: Windows
  • System details
    • OS Name: Microsoft Windows 11 Enterprise
    • OS Version: 10.0.26100 N/A Build 26100
    • System Type: x64-based PC
  • Sub-system: [ ]
  • ANTsPy version: 0.5.4
  • Installation type: pip

Additional context
Add any other context about the problem here. Many issues are specific to particular data
so please include example data if possible.

the temp files produced and not cleaned up:
image

proposed solution:

  1. add argument output_dir=None to build_template
  2. add helper function make_outprefix(k: int) -> str
  3. call ants.registration with outprefix=make_outprefix(k)
  4. at the end, if output_dir is None, delete work_dir
    work_dir = mktemp() if output_dir is None else output_dir

    def make_outprefix(k: int):
        os.makedirs(os.path.join(work_dir, f"img{k:04d}"), exist_ok=True)
        return os.path.join(work_dir, f"img{k:04d}", "out")

I am happy to submit a pull request.