Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Myspace button
Linkedin button
Webonews button
Delicious button
Digg button
Flickr button
Stumbleupon button
Newsvine button
Youtube button

Notes:

  • The site will be morphing over the next little while.
  • I am having some issues with tabs/spaces in the Python code. Sometimes they are correct, sometimes they get eaten. I am trying to figure it out.

Please feel free to leave a comment on a post if it interests you or if you have questions

Jan 042010

There is a great Python package pydicom that implements a nice interface in order to be able to access data within Dicom files.

One application which I wrote up was a dicom directory summarizer which goes through a list of dicom files and summarizes the types of MRI data in the directory.  I found myself getting frustrated trying to figure out which series of data was which given the huge number of dicom files (with really long names too!) in a directory.

The code below may be run within a Dicom directory and should run on Siemens Dicom data (IMA) files. It has been a while that I have run it so I can’t guarantee that it will work, but it should be a good place to start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/python

import dicom
import os
import re

def blah(val):
    return re.compile('[\-\w]+\.MR\.[\-\w]+\.\d+\.1\..*').match(val, 1)

# Get a list of all the files
files = []
for entry in os.listdir('.'):
    if ~os.path.isdir(entry) & entry.endswith('IMA'):
        files.append(entry)

# Filter to find the first of each series
firsts = filter( blah, files )

firsts.sort(key=lambda s: int( re.compile('[\-\w]+\.MR\.[\-\w]+\.(\d+)\.1\..*').search(s).group(1)) )

#  Read the first and output some interesting stuff
d = dicom.ReadFile(firsts[1])
print " Patient: " + d.PatientsName
print "Acquired: " + d.StudyDate[0:4]+"-"+d.StudyDate[4:6]+"-"+d.StudyDate[6:8] \
    + " " + d.StudyTime[0:2] + ":" + d.StudyTime[2:4] + ":" + d.StudyTime[4:6]
print "Comments: " + d.ImageComments

#  Run through the first file of each of the series
for entry in firsts:
    d = dicom.ReadFile(entry)

    num = re.compile('[\-\w]+\.MR\.[\-\w]+\.(\d+)\.1\..*').search(entry).group(1)

    out =  "\t" + str(num) + ") " +  d.SeriesDescription

    tt = '[_\-\w]+\.MR\.[_\-\w]+\.'+str(num)+'\..*'
    count = 0
    r = re.compile(tt)
    for f in files:
        if( r.match(f, 1) ):
            #print "%s matches %d" % (f, ii)
            count = count + 1

    if( not re.compile(".*(FA|TRACEW|TENSOR|ADC|MoCoSeries)$").match(d.SeriesDescription, 1 ) ):
        out += " (vols=" + str(count)

        if( 'RepetitionTime' in d ):
            out += ", TR=" + str(d.RepetitionTime)

        if( 'EchoTime' in d ):
            out += ", TE=" + str(d.EchoTime)
       
        out += ")"

    print out

2 Responses to “Python Dicom”

  1. Have you heard of quickdicom?
    http://sourceforge.net/projects/quickdicom/

    This toolkit was made in our lab (imaginginformatics.ca) and is super-useful for mac. Once this toolkit is installed Finder understands dicom files – so you can see thumbnails of the files as well as the dicom header, meaning that spotlight can search dicom headers. You can double-click dicom file sand quickdicom acts as a dicom viewer with standard viewing options like window/level. Included in the package is a framework that can be used by Objective C or Python.

    Another feature I found quite by accident is that quickdicom’s window/level function becomes available to pixelmator. I often use this filter now to quickly touch-up photos.

    Cheers!

    • craig says:

      I haven’t heard of it before but I’ll try it out. I don’t typically use Dicom images here but I’ll keep it in mind if I hear others need this type of thing. Thanks!

Leave a Reply

(required)

(required)

Spam Protection by WP-SpamFree

© 2010 Math, Computing and Research Please leave a comment or contact me craig@mri.brechmos.org if you have any questions. Suffusion WordPress theme by Sayontan Sinha