Replace bad DICOMs in an fMRI sequence

This page last updated 2022/06/15


This is my first attempt at resolving the 'File not large enough to store image data' warning from dcm2niix when converting a problematic set of DICOMs to NIfTI. This error seems to appear when one or more DICOM files are smaller than usual, often a result of incomplete or corrupted pixeldata. dcm2niix will not convert these files to NIfTI, and this script is a dirty solution to this problem.

Possible solutions to this problem involve simply deleting the corrupted DICOM files and continue converting the remaining fileset as normal. However, this will result in a file with uneven temporal resolution, and this is difficult if not impossible to handle in typical preprocessing streams. Initially, I simply replaced the offending files with empty pixeldata (i.e. an array of all zeroes), however this results in errors in motion correction / realignment. Interpolating the missing data would be ideal, but the data would need to be motion corrected, at the bare minimum, before interpolation. So, I settled on a solution that may be largely idiosyncratic, but if you are reading this, you may be able to scavenge my code and find something that works for your preprocessing pipeline. I imagine that there are better solutions to this problem that others have identified.

The script searches for outlier filesizes, and replaces the pixeldata of offending dicoms with the pixeldata from a DICOM file of the same sequence that is an expected size, except with doubled intensity. This allows dcm2niix to convert the file without error and for software packages to realign or motion correct the data, and these artificially-produced timepoints will get identified by any artifact-rejection script that identifies large changes in global signal, and their influence can then be removed with volume scrubbing. I think that ideally, these volumes would be interpolated after motion correction, but I'm not confident that interpolated volumes should not also be scrubbed or handled differently than non-interpolated volumes.



You can download the script that peforms the corrections from here. The script takes a single argument as an input, which should be the directory containing the sequence of DICOMs that includes one or more corrupted files.

python3 <input_folder>
#Example usage:
python3 /home/fordb/Desktop/sub1dicoms/fmri_rest

!!! IMPORTANT !!! This script overwrites any DICOMs it detects as corrupted. You should always make a backup of your original DICOMS and keep them protected to avoid data loss.

After the script has replaced the corrupted DICOM files, you should be able to call dcm2niix without error, and continue processing the files as usual, so long as you ensure you perform volume scrubbing.

Detailed Information

The script performs the following steps:

  1. Checks if argument is a directory
  2. Identifies the file sizes of the directory contents
  3. Any file that is 2 standard deviations from the mean file size is flagged for replacement
  4. The first file identified that is not flagged is considered the template file
  5. Flagged files are overwritten with pixeldata from the template file, elementwise multiplied by 2

Change log

2022/06/15: Initial version