Skip to content

Commit 2262678

Browse files
committed
Merge remote branch 'christian/dft' into dft
2 parents 93fef75 + 831b9d9 commit 2262678

File tree

4 files changed

+172
-84
lines changed

4 files changed

+172
-84
lines changed

nibabel/dft/test_data/0.dcm

221 KB
Binary file not shown.

nibabel/dft/test_data/1.dcm

221 KB
Binary file not shown.

nibabel/tests/test_dft.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
""" Testing dft
2+
"""
3+
4+
import StringIO
5+
import PIL.Image
6+
from nose.tools import assert_true, assert_false, assert_equal, assert_raises
7+
from .. import dft
8+
from .. import nifti1
9+
10+
data_dir = 'dft_test_data/td'
11+
12+
def test_init():
13+
dft.clear_cache()
14+
dft.update_cache(data_dir)
15+
return
16+
17+
def test_study():
18+
studies = dft.get_studies(data_dir)
19+
assert_equal(len(studies), 1)
20+
assert_equal(studies[0].uid,
21+
'1.3.12.2.1107.5.2.32.35119.30000010011408520750000000022')
22+
assert_equal(studies[0].date, '20100114')
23+
assert_equal(studies[0].time, '121314.000000')
24+
assert_equal(studies[0].comments, 'dft study comments')
25+
assert_equal(studies[0].patient_name, 'dft patient name')
26+
assert_equal(studies[0].patient_id, '1234')
27+
assert_equal(studies[0].patient_birth_date, '19800102')
28+
assert_equal(studies[0].patient_sex, 'F')
29+
30+
31+
def test_series():
32+
studies = dft.get_studies(data_dir)
33+
assert_equal(len(studies[0].series), 1)
34+
ser = studies[0].series[0]
35+
assert_equal(ser.uid,
36+
'1.3.12.2.1107.5.2.32.35119.2010011420292594820699190.0.0.0')
37+
assert_equal(ser.number, '12')
38+
assert_equal(ser.description, 'CBU_DTI_64D_1A')
39+
assert_equal(ser.rows, 256)
40+
assert_equal(ser.columns, 256)
41+
assert_equal(ser.bits_allocated, 16)
42+
assert_equal(ser.bits_stored, 12)
43+
44+
def test_storage_instances():
45+
studies = dft.get_studies(data_dir)
46+
sis = studies[0].series[0].storage_instances
47+
assert_equal(len(sis), 2)
48+
assert_equal(sis[0].instance_number, 1)
49+
assert_equal(sis[1].instance_number, 2)
50+
assert_equal(sis[0].uid,
51+
'1.3.12.2.1107.5.2.32.35119.2010011420300180088599504.0')
52+
assert_equal(sis[1].uid,
53+
'1.3.12.2.1107.5.2.32.35119.2010011420300180088599504.1')
54+
55+
def test_storage_instance():
56+
return
57+
58+
def test_png():
59+
studies = dft.get_studies(data_dir)
60+
data = studies[0].series[0].as_png()
61+
im = PIL.Image.open(StringIO.StringIO(data))
62+
assert_equal(im.size, (256, 256))
63+
return
64+
65+
def test_nifti():
66+
studies = dft.get_studies(data_dir)
67+
data = studies[0].series[0].as_nifti()
68+
assert_equal(len(data), 352 + 2*256*256*2)
69+
h = nifti1.Nifti1Header(data[:348])
70+
assert_equal(h.get_data_shape(), (256, 256, 2))
71+
return
72+

tools/dicomfs.wsgi

Lines changed: 100 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,100 @@ import sys
1212
import traceback
1313
import urllib
1414
import cgi
15-
import dft
15+
import jinja2
16+
from nibabel import dft
17+
18+
def html_unicode(u):
19+
return cgi.escape(u.encode('utf-8'))
1620

1721
# this is the directory containing the DICOM data, or None for all cached data
18-
base_dir = '/Users/ch/Desktop/umms/dft/trunk/data/t'
22+
base_dir = '/path/to/DICOM'
1923
base_dir = None
2024

25+
template_env = jinja2.Environment(autoescape=True)
26+
template_env.filters['urlquote'] = urllib.quote
27+
28+
index_template = """<html><head><title>data</title></head>
29+
<body>
30+
Home
31+
<br />
32+
<br />
33+
{% for p in patients|sort %}
34+
Patient: <a href="{{ p|urlquote }}/">{{ p }}</a>
35+
<br />
36+
{% if patients[p]|length == 1 %}
37+
1 study
38+
{% else %}
39+
{{ patients[p]|length }} studies
40+
{% endif %}
41+
<br />
42+
{% endfor %}
43+
</body>
44+
</html>
45+
"""
46+
47+
patient_template = """<html><head><title>data</title></head>
48+
<body>
49+
<a href="../">Home</a> -&gt; Patient {{ studies[0].patient_name_or_uid() }}
50+
<br />
51+
<br />
52+
Patient name: {{ studies[0].patient_name }}
53+
<br />
54+
Patient ID: {{ studies[0].patient_id }}
55+
<br />
56+
Patient birth date: {{ studies[0].patient_birth_date }}
57+
<br />
58+
Patient sex: {{ studies[0].patient_sex }}
59+
<br />
60+
<ul>
61+
{% for s in studies %}
62+
<li><a href="{{ s.date|urlquote }}_{{ s.time|urlquote }}/">Study {{ s.uid }}</a></li>
63+
<ul>
64+
<li>Date: {{ s.date }}</li>
65+
<li>Time: {{ s.time }}</li>
66+
<li>Comments: {{ s.comments }}</li>
67+
<li>Series: {{ s.series|length }}</li>
68+
{% endfor %}
69+
</ul>
70+
</body>
71+
</html>
72+
"""
73+
74+
patient_date_time_template = """
75+
<html><head><title>data</title></head>
76+
<body>
77+
<a href="../../">Home</a> -&gt; <a href="../../{{ study.patient_name_or_uid() }}/">Patient {{ study.patient_name_or_uid() }}</a> -&gt; Study {{ study.date}} {{ study.time }}
78+
<br />
79+
<br />
80+
Patient name: <a href="../../{{ study.patient_name_or_uid() }}/">{{ study.patient_name }}</a>
81+
<br />
82+
Study UID: {{ study.uid }}
83+
<br />
84+
Study date: {{ study.date }}
85+
<br />
86+
Study time: {{ study.time }}
87+
<br />
88+
Study comments: {{ study.comments }}
89+
{% if study.series|length == 0 %}
90+
<br />
91+
No series.
92+
{% else %}
93+
<ul>
94+
{% for s in study.series %}
95+
<li>Series {{ s.number }} (<a href="{{ s.number }}/nifti">NIfTI</a>)</li>
96+
<ul>
97+
<li>Series UID: {{ s.uid }}</li>
98+
<li>Series description: {{ s.description }}</li>
99+
<li>Series dimensions: {{ s.rows }}x{{ s.columns }}x{{ s.storage_instances|length }}</li>
100+
</ul>
101+
<img src="{{ s.number }}/png" />
102+
{% endfor %}
103+
</ul>
104+
{% endif %}
105+
</body>
106+
</html>
107+
"""
108+
21109
class HandlerError:
22110

23111
def __init__(self, status, output):
@@ -60,28 +148,6 @@ def handler(environ):
60148
return ('200 OK', 'image/png', png(parts[0], parts[1], parts[2]))
61149
raise HandlerError('404 Not Found', "%s not found\n" % environ['PATH_INFO'])
62150

63-
def index(environ):
64-
patients = {}
65-
for s in dft.get_studies(base_dir):
66-
patients.setdefault(s.patient_name_or_uid(), []).append(s)
67-
output = ''
68-
output += '<html><head><title>data</title></head>\n'
69-
output += '<body>\n'
70-
output += 'Home\n'
71-
output += '<br />\n'
72-
output += '<br />\n'
73-
for p in sorted(patients):
74-
output += 'Patient: <a href="%s/">%s</a>\n' % (urllib.quote(p.encode('utf-8')), html_unicode(p))
75-
output += '<br />\n'
76-
if len(patients[p]) == 1:
77-
output += '1 study\n'
78-
else:
79-
output += '%d studies' % len(patients[p])
80-
output += '<br />\n'
81-
output += '</body>\n'
82-
output += '</html>\n'
83-
return output
84-
85151
def study_cmp(a, b):
86152
if a.date < b.date:
87153
return -1
@@ -93,40 +159,20 @@ def study_cmp(a, b):
93159
return 1
94160
return 0
95161

96-
def html_unicode(u):
97-
return cgi.escape(u.encode('utf-8'))
162+
def index(environ):
163+
patients = {}
164+
for s in dft.get_studies(base_dir):
165+
patients.setdefault(s.patient_name_or_uid(), []).append(s)
166+
template = template_env.from_string(index_template)
167+
return template.render(patients=patients).encode('utf-8')
98168

99169
def patient(patient):
100170
studies = [ s for s in dft.get_studies() if s.patient_name_or_uid() == patient ]
101171
if len(studies) == 0:
102172
raise HandlerError('404 Not Found', 'patient %s not found\n' % patient)
103173
studies.sort(study_cmp)
104-
output = ''
105-
output += '<html><head><title>data</title></head>\n'
106-
output += '<body>\n'
107-
output += '<a href="../">Home</a> -&gt; Patient %s\n' % html_unicode(studies[0].patient_name_or_uid())
108-
output += '<br />\n'
109-
output += '<br />\n'
110-
output += 'Patient name: %s\n' % html_unicode(studies[0].patient_name)
111-
output += '<br />\n'
112-
output += 'Patient ID: %s\n' % html_unicode(studies[0].patient_id)
113-
output += '<br />\n'
114-
output += 'Patient birth date: %s\n' % html_unicode(studies[0].patient_birth_date)
115-
output += '<br />\n'
116-
output += 'Patient sex: %s\n' % html_unicode(studies[0].patient_sex)
117-
output += '<br />\n'
118-
output += '<ul>\n'
119-
for s in studies:
120-
output += '<li><a href="%s_%s/">Study %s</a></li>\n' % (urllib.quote(s.date), urllib.quote(s.time), html_unicode(s.uid))
121-
output += '<ul>\n'
122-
output += '<li>Date: %s</li>\n' % html_unicode(s.date)
123-
output += '<li>Time: %s</li>\n' % html_unicode(s.time)
124-
output += '<li>Comments: %s</li>\n' % html_unicode(s.comments)
125-
output += '<li>Series: %d</li>\n' % len(s.series)
126-
output += '</ul>\n'
127-
output += '</body>\n'
128-
output += '</html>\n'
129-
return output
174+
template = template_env.from_string(patient_template)
175+
return template.render(studies=studies).encode('utf-8')
130176

131177
def patient_date_time(patient, date_time):
132178
study = None
@@ -139,38 +185,8 @@ def patient_date_time(patient, date_time):
139185
break
140186
if study is None:
141187
raise HandlerError, ('404 Not Found', 'study not found')
142-
output = ''
143-
output += '<html><head><title>data</title></head>\n'
144-
output += '<body>\n'
145-
output += '<a href="../../">Home</a> -&gt; <a href="../../%s/">Patient %s</a> -&gt; Study %s %s\n' % (urllib.quote(study.patient_name_or_uid()), html_unicode(study.patient_name_or_uid()), html_unicode(study.date), html_unicode(study.time))
146-
output += '<br />\n'
147-
output += '<br />\n'
148-
output += 'Patient name: <a href="/../%s/">%s</a>\n' % (urllib.quote(study.patient_name_or_uid()), html_unicode(study.patient_name))
149-
output += '<br />\n'
150-
output += 'Study UID: %s\n' % html_unicode(study.uid)
151-
output += '<br />\n'
152-
output += 'Study date: %s\n' % html_unicode(study.date)
153-
output += '<br />\n'
154-
output += 'Study time: %s\n' % html_unicode(study.time)
155-
output += '<br />\n'
156-
output += 'Study comments: %s\n' % html_unicode(study.comments)
157-
if len(study.series) == 0:
158-
output += '<br />\n'
159-
output += 'No series.\n'
160-
else:
161-
output += '<ul>\n'
162-
for s in study.series:
163-
output += '<li>Series %s (<a href="%s/nifti">NIfTI</a>)</li>\n' % (html_unicode(s.number), html_unicode(s.number))
164-
output += '<ul>\n'
165-
output += '<li>Series UID: %s</li>\n' % html_unicode(s.uid)
166-
output += '<li>Series description: %s</li>\n' % html_unicode(s.description)
167-
output += '<li>Series dimensions: %dx%dx%d</li>\n' % (s.rows, s.columns, len(s.storage_instances))
168-
output += '</ul>\n'
169-
output += '<img src="%s/png" />\n' % html_unicode(s.number)
170-
output += '</ul>\n'
171-
output += '</body>\n'
172-
output += '</html>\n'
173-
return output
188+
template = template_env.from_string(patient_date_time_template)
189+
return template.render(study=study).encode('utf-8')
174190

175191
def nifti(patient, date_time, scan):
176192
study = None

0 commit comments

Comments
 (0)