dicomsort: a tool to organize DICOM files
Reading DICOM files exported from the scanner or XNAT is not always intuitive. This script aims to ease confusion by organizing the files in a way that is clear and consistent.
Dicomsort reads information from the DICOM (Digital Imaging and Communications in Medicine) headers of each file and then renames and sorts the files alphabetically by series and slice number. It can also be used to create subdirectories for each series.
We maintain two versions of this utility:
dicomsort.py: this python implementation relies on the python pydicom package. If you need to sort MR spectroscopy DICOM data, this is the version for you!
dicomsort.sh: this bash script requires AFNI rather than python/pydicom. It functions nearly identically, but cannot sort spectroscopy data.
Accessing the script
dicomsort.py
and dicomsort.sh
can be accessed on Oscar in the public BNC scripts directory: /oscar/data/bnc/scripts/
. From there, they can be used standalone or can be integrated into larger pipelines on Oscar. Additionally, they are available for local download on the BNC's github found here.

1. dicomsort.py python script
First, we need to be in a python environment that contains the package pydicom. You can set up your own environment and pip install pydicom
, or on Oscar you can simply open a terminal and activate an environment we have already created:
source /oscar/data/bnc/src/python_venvs/pydicom/bin/activate
You will be able to tell that this environment is activated because it will say (pydicom)
at the beginning of your terminal command prompt.
Now, you'll be able to run the script with
python /oscar/data/bnc/scripts/dicomsort.py
usage: python dicomsort.py [-r] [-d destdir] [-s sourcedir] [-i] [-q] [-n] options: -r: Rename files. Default is to copy. -d: Destination directory. Default is ./renamed. -s: Source directory. Default is current directory. -i: Create subdirectories by subject ID. -q: Create subdirectories by series description. -n: Don't recurse into subdirectories.
When you are done, you can deactivate the python environment with deactivate
.
2. dicomsort.sh bash script:
This script relies on multiple AFNI commands. Before running it, ensure that AFNI is installed. On Oscar, this can be done with the command module load afni
To display the help message and see how to use the script, you can run /oscar/data/bnc/scripts/dicomsort.sh -?
in a terminal on Oscar.
Last updated
Was this helpful?