Skip to content

[BUG] center_nifti fails on Windows with WinError 32 when processing uncompressed .nii files #1693

@davidaquilue

Description

@davidaquilue

Describe the bug
When running clinica iotools center-nifti (or calling center_nifti programmatically) on a BIDS dataset containing uncompressed .nii files on Windows, the centering step fails withPermissionError: [WinError 32] The process cannot access the file because it is being used by another process for every file Clinica attempts to center. The pipeline copies the BIDS tree successfully via copytree, but each call to center_nifti_origin(f, f) fails when it tries to unlink() the existing file before writing the centered version.
The root cause appears to be that nib.load() memory-maps .nii files by default, and Windows does not allow deletion of a file while any process holds an open handle (including a memory-map) to it.

Because the failures are caught and accumulated into a list rather than raised immediately, the copytree step has already produced an output BIDS directory that looks complete but actually contains byte-identical copies of the original files (no centering applied). Users who don't read the final RuntimeError carefully may assume centering was performed when it wasn't.

To Reproduce
On a Windows machine, prepare a BIDS dataset containing uncompressed .nii files (e.g., a subset of OASIS3).
Run:

pythonfrom clinica.iotools.center_nifti import center_nifti

center_nifti(
    bids_directory=<path_to_bids>,
    output_bids_directory=<output_path>,
    centering_threshold=0,  # To ensure centering in as many cases as possible
)

Observe that the run terminates with a RuntimeError listing one WinError 32 per .nii file that should have been centered.

Expected behavior
Each NIfTI file whose center exceeds the threshold should be replaced in the output directory with its centered version, regardless of operating system or whether the file is .nii or .nii.gz.

Desktop (please complete the following information):

  • OS: Windows 11
  • Python: 3.10.16
  • 0.9.4

Note
Passing mmap=False to the nib.load here seems to fix the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions