Skip to content

Commit e8e70cc

Browse files
chaselgrovemih
authored andcommitted
dicomfs fixes regarding encoding and other bits.
(communicated via email)
1 parent 745ed90 commit e8e70cc

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

bin/dicomfs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ import os
77
import stat
88
import errno
99
import time
10+
import locale
1011
import fuse
1112
import dft
1213

1314
uid = os.getuid()
1415
gid = os.getgid()
16+
encoding = locale.getdefaultlocale()[1]
1517

1618
fuse.fuse_python_api = (0, 2)
1719

1820
class FileHandle:
1921

2022
def __init__(self, fno):
2123
self.fno = fno
22-
# self.keep_cache = True
23-
# self.direct_io = True
24+
self.keep_cache = False
25+
self.direct_io = False
2426
return
2527

2628
def __str__(self):
@@ -36,8 +38,7 @@ class DICOMFS(fuse.Fuse):
3638
def get_paths(self):
3739
paths = {}
3840
for study in dft.get_studies(self.dicom_path):
39-
paths.setdefault(study.patient_name, {})
40-
pd = paths[study.patient_name]
41+
pd = paths.setdefault(study.patient_name_or_uid(), {})
4142
patient_info = 'patient information\n'
4243
patient_info = 'name: %s\n' % study.patient_name
4344
patient_info += 'ID: %s\n' % study.patient_id
@@ -170,7 +171,7 @@ if len(sys.argv) != 3:
170171

171172
fs = DICOMFS(dash_s_do='setsingle')
172173
fs.parse(['-f', '-s', sys.argv[2]])
173-
fs.dicom_path = sys.argv[1]
174+
fs.dicom_path = sys.argv[1].decode(encoding)
174175
try:
175176
fs.main()
176177
except fuse.FuseError:

nibabel/dft.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ def __getattribute__(self, name):
8888
self.series = val
8989
return val
9090

91+
def patient_name_or_uid(self):
92+
if self.patient_name == '':
93+
return self.uid
94+
return self.patient_name
95+
9196
class _Series(object):
9297

9398
def __init__(self, d):
@@ -372,7 +377,10 @@ def _update_file(c, dir, fname, studies, series, storage_instances):
372377
study_uid = do[_study_instance_uid_tag].value
373378
study_date = do[_study_date_tag].value
374379
study_time = do[_study_time_tag].value
375-
study_comments = do[_study_comments_tag].value
380+
try:
381+
study_comments = do[_study_comments_tag].value
382+
except KeyError:
383+
study_comments = ''
376384
patient_name = do[_patients_name_tag].value
377385
patient_id = do[_patient_id_tag].value
378386
patient_birth_date = do[_patients_birth_date_tag].value
@@ -386,10 +394,19 @@ def _update_file(c, dir, fname, studies, series, storage_instances):
386394
series_bits_stored = int(do[_bits_stored_tag].value)
387395
instance_number = int(do[_instance_number_tag].value)
388396
storage_instance_uid = do[_sop_instance_uid_tag].value
397+
# except Exception, data:
398+
# print 'exc', type(data), data, str(data)
399+
# return None
389400
except dicom.filereader.InvalidDicomError:
401+
print ' not a DICOM file'
402+
return None
403+
except KeyError, data:
404+
print ' missing tag %s' % str(data)
390405
return None
391-
except KeyError:
406+
except Exception, data:
407+
print ' error: %s' % str(data)
392408
return None
409+
print ' storage instance %s' % storage_instance_uid
393410
if study_uid not in studies:
394411
query = """INSERT INTO study (uid,
395412
date,
@@ -477,6 +494,7 @@ def clear_cache():
477494
)
478495

479496
_db_fname = '%s/dft.%d.sqlite' % (tempfile.gettempdir(), os.getuid())
497+
print 'db is %s' % _db_fname
480498
_db = sqlite3.connect(_db_fname, check_same_thread=False)
481499

482500
with _db_change() as c:

tools/data.wsgi

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ def handler(environ):
5555
def index(environ):
5656
patients = {}
5757
for s in dft.get_studies(base_dir):
58-
patients.setdefault(s.patient_name, []).append(s)
58+
patients.setdefault(s.patient_name_or_uid(), []).append(s)
5959
output = ''
6060
output += '<html><head><title>data</title></head>\n'
6161
output += '<body>\n'
6262
output += 'Home\n'
6363
output += '<br />\n'
6464
output += '<br />\n'
6565
for p in sorted(patients):
66-
output += 'Patient name: <a href="%s/">%s</a>\n' % (urllib.quote(p.encode('utf-8')), html_unicode(p))
66+
output += 'Patient: <a href="%s/">%s</a>\n' % (urllib.quote(p.encode('utf-8')), html_unicode(p))
6767
output += '<br />\n'
6868
if len(patients[p]) == 1:
6969
output += '1 study\n'
@@ -89,14 +89,14 @@ def html_unicode(u):
8989
return cgi.escape(u.encode('utf-8'))
9090

9191
def patient(patient):
92-
studies = [ s for s in dft.get_studies() if s.patient_name == patient ]
92+
studies = [ s for s in dft.get_studies() if s.patient_name_or_uid() == patient ]
9393
if len(studies) == 0:
9494
raise HandlerError('404 Not Found', 'patient %s not found\n' % patient)
9595
studies.sort(study_cmp)
9696
output = ''
9797
output += '<html><head><title>data</title></head>\n'
9898
output += '<body>\n'
99-
output += '<a href="../">Home</a> -&gt; Patient %s\n' % html_unicode(studies[0].patient_name)
99+
output += '<a href="../">Home</a> -&gt; Patient %s\n' % html_unicode(studies[0].patient_name_or_uid())
100100
output += '<br />\n'
101101
output += '<br />\n'
102102
output += 'Patient name: %s\n' % html_unicode(studies[0].patient_name)
@@ -123,7 +123,7 @@ def patient(patient):
123123
def patient_date_time(patient, date_time):
124124
study = None
125125
for s in dft.get_studies():
126-
if s.patient_name != patient:
126+
if s.patient_name_or_uid() != patient:
127127
continue
128128
if date_time != '%s_%s' % (s.date, s.time):
129129
continue
@@ -134,10 +134,10 @@ def patient_date_time(patient, date_time):
134134
output = ''
135135
output += '<html><head><title>data</title></head>\n'
136136
output += '<body>\n'
137-
output += '<a href="../../">Home</a> -&gt; <a href="../../%s/">Patient %s</a> -&gt; Study %s %s\n' % (urllib.quote(study.patient_name), html_unicode(study.patient_name), html_unicode(study.date), html_unicode(study.time))
137+
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))
138138
output += '<br />\n'
139139
output += '<br />\n'
140-
output += 'Patient name: <a href="/../%s/">%s</a>\n' % (urllib.quote(study.patient_name), html_unicode(study.patient_name))
140+
output += 'Patient name: <a href="/../%s/">%s</a>\n' % (urllib.quote(study.patient_name_or_uid()), html_unicode(study.patient_name))
141141
output += '<br />\n'
142142
output += 'Study UID: %s\n' % html_unicode(study.uid)
143143
output += '<br />\n'
@@ -167,7 +167,7 @@ def patient_date_time(patient, date_time):
167167
def nifti(patient, date_time, scan):
168168
study = None
169169
for s in dft.get_studies():
170-
if s.patient_name != patient:
170+
if s.patient_name_or_uid() != patient:
171171
continue
172172
if date_time != '%s_%s' % (s.date, s.time):
173173
continue
@@ -188,7 +188,7 @@ def nifti(patient, date_time, scan):
188188
def png(patient, date_time, scan):
189189
study = None
190190
for s in dft.get_studies():
191-
if s.patient_name != patient:
191+
if s.patient_name_or_uid() != patient:
192192
continue
193193
if date_time != '%s_%s' % (s.date, s.time):
194194
continue
@@ -209,7 +209,6 @@ def png(patient, date_time, scan):
209209

210210
if __name__ == '__main__':
211211
import wsgiref.simple_server
212-
server_prefix = ''
213212
httpd = wsgiref.simple_server.make_server('', 8080, application)
214213
httpd.serve_forever()
215214

0 commit comments

Comments
 (0)