Skip to content

Instantly share code, notes, and snippets.

@lauracodecreations
Last active May 10, 2017 01:26
Show Gist options
  • Save lauracodecreations/b580de13dbcdb29db4fc0645e2919c35 to your computer and use it in GitHub Desktop.
Save lauracodecreations/b580de13dbcdb29db4fc0645e2919c35 to your computer and use it in GitHub Desktop.
Getting scan date from dicoms (neuroimaging, FMRI, Structural)

Getting Scan Date from Dicoms

The Problem

You get two dicoms files from two different subjects to process, but you accidently rename the wrong one making two different subjects have the same filename. So, to tell them apart you can look into the scan date inside the dicoms file.

Or you accidently lost the information from the scan date, and you need to know when all the subjects in the study were scanned.

Solution

You can use the script below to get the scan date for all subjects in the project.

It needs two inputs: the timepoint where it needs to look into, and the path to the subjects' directory.

This code assumes that the subject ID is a 6 digits number.

This code will ouput a csv file in the temporary folder that contains the subject ID and the scan date in the format: month/day/year

Example

To use it:

  1. In the code, add the relative path from the subject directory to the variable 'path'

  2. Run the program:

     selectdate 2 /project/subjects/
    
#!/bin/bash
#Timepoint to be run
tp=$1
#Path to directory that contains all data
root=$2
#Write path to dicoms from the subject directory
${path}= [ insert path here ]
cd ${root}
TEMP=$(mktemp -d /tmp/meanXXXXXXXXX)
echo "IDNUM,SCAND4" >> $TEMP/test.csv
for subject in [1-9][0-9][0-9][0-9][0-9][0-9]*; do
cd $subject/${path}
date=`dcmdump *.dcm | grep StudyDate | awk '{ print $3 }'`
datew=`echo $date | sed 's/\[//;s/\]//'`
newDatef=`echo $datew | sed -n -e "s_\(....\)\(..\)\(..\)_\2/\3/\1_p"`
echo "${subject},${newDatef}" >> $TEMP/test.csv
cd ../../../
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment