WriteImage output not consistent

I’ve written a script that iterates over some images, and analyses those images with different parameter sets, saving the output to file. When running the code the output saved to file is inconsistent, and sometimes looks like it has been truncated at a certain slice height.

The images I am working with are 3D nifi files, that are in the order of 2 GB. I have been running the script on my local computer, RAM 32 GB, while doing other things on the computer which are sometimes memory intensive (for example, image visualization). Regardless of the output, the script runs with no errors. A simplified code block is shown below, using dummy functions in place of the actual analysis. Running with a break point on the line … analysed_img . . . shows that the content of the analysis is always correct. The analysis of these images is highly memory intensive.

for im_path in im_paths:
    img = sitk.ReadImage(im_path)

    for param in parameters:    
        out_path = out_path_generator(im_path, param)

        analysed_img = im_analyse(img, param) # content of <analysed_img> always fine at this line
        sitk.WriteImage(analysed_img, out_path)

I have run the code remotely on a high performance computing node and it works and writes out data correctly. I am wondering if this behavior is a known issue, and what I could do to prevent it from happening when I am running code with limited memory availability (that is, if memory availability is the problem). If not, I’d love to get some ideas on ways to assess this problem.

Making software work correctly when you don’t have enough memory is practically impossible. Even robust detection and reporting could be very expensive to implement and maintain.

If you ever get close to running out of memory then you can increase your memory space by changing your system settings (virtual memory/swap space) or add more physical RAM.

Also worth a shot: keep your images in NRRD or MetaImage format. Nifti has ambiguous metadata specification and the original version is limited to 2 GB.

Thank you both for your replies, for now I’ll shelve the issue under too expensive to investigate and stick to running my code on high performance computing nodes.

Would you be willing to elaborate on what you mean by ambiguous metadata specifications? I have no particular attachment to the format I am using, and will certainly change to a more suitable format if there is good reason to.

One example is that there are multiple ways to store the image orientation, and which one to use and which one has the highest numeric precision is not consistent.

Also you may want to try the to use the latest nightly binaries form the SimpleITK Release page. There have been various nifty fixes recently.

1 Like