Skip to content

Commit

Permalink
dicomfs fixes regarding encoding and other bits.
Browse files Browse the repository at this point in the history
(communicated via email)
  • Loading branch information
chaselgrove authored and mih committed Sep 7, 2011
1 parent 745ed90 commit e8e70cc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
11 changes: 6 additions & 5 deletions bin/dicomfs
Expand Up @@ -7,20 +7,22 @@ import os
import stat
import errno
import time
import locale
import fuse
import dft

uid = os.getuid()
gid = os.getgid()
encoding = locale.getdefaultlocale()[1]

fuse.fuse_python_api = (0, 2)

class FileHandle:

def __init__(self, fno):
self.fno = fno
# self.keep_cache = True
# self.direct_io = True
self.keep_cache = False
self.direct_io = False
return

def __str__(self):
Expand All @@ -36,8 +38,7 @@ class DICOMFS(fuse.Fuse):
def get_paths(self):
paths = {}
for study in dft.get_studies(self.dicom_path):
paths.setdefault(study.patient_name, {})
pd = paths[study.patient_name]
pd = paths.setdefault(study.patient_name_or_uid(), {})
patient_info = 'patient information\n'
patient_info = 'name: %s\n' % study.patient_name
patient_info += 'ID: %s\n' % study.patient_id
Expand Down Expand Up @@ -170,7 +171,7 @@ if len(sys.argv) != 3:

fs = DICOMFS(dash_s_do='setsingle')
fs.parse(['-f', '-s', sys.argv[2]])
fs.dicom_path = sys.argv[1]
fs.dicom_path = sys.argv[1].decode(encoding)
try:
fs.main()
except fuse.FuseError:
Expand Down
22 changes: 20 additions & 2 deletions nibabel/dft.py
Expand Up @@ -88,6 +88,11 @@ def __getattribute__(self, name):
self.series = val
return val

def patient_name_or_uid(self):
if self.patient_name == '':
return self.uid
return self.patient_name

class _Series(object):

def __init__(self, d):
Expand Down Expand Up @@ -372,7 +377,10 @@ def _update_file(c, dir, fname, studies, series, storage_instances):
study_uid = do[_study_instance_uid_tag].value
study_date = do[_study_date_tag].value
study_time = do[_study_time_tag].value
study_comments = do[_study_comments_tag].value
try:
study_comments = do[_study_comments_tag].value
except KeyError:
study_comments = ''
patient_name = do[_patients_name_tag].value
patient_id = do[_patient_id_tag].value
patient_birth_date = do[_patients_birth_date_tag].value
Expand All @@ -386,10 +394,19 @@ def _update_file(c, dir, fname, studies, series, storage_instances):
series_bits_stored = int(do[_bits_stored_tag].value)
instance_number = int(do[_instance_number_tag].value)
storage_instance_uid = do[_sop_instance_uid_tag].value
# except Exception, data:
# print 'exc', type(data), data, str(data)
# return None
except dicom.filereader.InvalidDicomError:
print ' not a DICOM file'
return None
except KeyError, data:
print ' missing tag %s' % str(data)
return None
except KeyError:
except Exception, data:
print ' error: %s' % str(data)
return None
print ' storage instance %s' % storage_instance_uid
if study_uid not in studies:
query = """INSERT INTO study (uid,
date,
Expand Down Expand Up @@ -477,6 +494,7 @@ def clear_cache():
)

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

with _db_change() as c:
Expand Down
19 changes: 9 additions & 10 deletions tools/data.wsgi
Expand Up @@ -55,15 +55,15 @@ def handler(environ):
def index(environ):
patients = {}
for s in dft.get_studies(base_dir):
patients.setdefault(s.patient_name, []).append(s)
patients.setdefault(s.patient_name_or_uid(), []).append(s)
output = ''
output += '<html><head><title>data</title></head>\n'
output += '<body>\n'
output += 'Home\n'
output += '<br />\n'
output += '<br />\n'
for p in sorted(patients):
output += 'Patient name: <a href="%s/">%s</a>\n' % (urllib.quote(p.encode('utf-8')), html_unicode(p))
output += 'Patient: <a href="%s/">%s</a>\n' % (urllib.quote(p.encode('utf-8')), html_unicode(p))
output += '<br />\n'
if len(patients[p]) == 1:
output += '1 study\n'
Expand All @@ -89,14 +89,14 @@ def html_unicode(u):
return cgi.escape(u.encode('utf-8'))

def patient(patient):
studies = [ s for s in dft.get_studies() if s.patient_name == patient ]
studies = [ s for s in dft.get_studies() if s.patient_name_or_uid() == patient ]
if len(studies) == 0:
raise HandlerError('404 Not Found', 'patient %s not found\n' % patient)
studies.sort(study_cmp)
output = ''
output += '<html><head><title>data</title></head>\n'
output += '<body>\n'
output += '<a href="../">Home</a> -&gt; Patient %s\n' % html_unicode(studies[0].patient_name)
output += '<a href="../">Home</a> -&gt; Patient %s\n' % html_unicode(studies[0].patient_name_or_uid())
output += '<br />\n'
output += '<br />\n'
output += 'Patient name: %s\n' % html_unicode(studies[0].patient_name)
Expand All @@ -123,7 +123,7 @@ def patient(patient):
def patient_date_time(patient, date_time):
study = None
for s in dft.get_studies():
if s.patient_name != patient:
if s.patient_name_or_uid() != patient:
continue
if date_time != '%s_%s' % (s.date, s.time):
continue
Expand All @@ -134,10 +134,10 @@ def patient_date_time(patient, date_time):
output = ''
output += '<html><head><title>data</title></head>\n'
output += '<body>\n'
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))
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))
output += '<br />\n'
output += '<br />\n'
output += 'Patient name: <a href="/../%s/">%s</a>\n' % (urllib.quote(study.patient_name), html_unicode(study.patient_name))
output += 'Patient name: <a href="/../%s/">%s</a>\n' % (urllib.quote(study.patient_name_or_uid()), html_unicode(study.patient_name))
output += '<br />\n'
output += 'Study UID: %s\n' % html_unicode(study.uid)
output += '<br />\n'
Expand Down Expand Up @@ -167,7 +167,7 @@ def patient_date_time(patient, date_time):
def nifti(patient, date_time, scan):
study = None
for s in dft.get_studies():
if s.patient_name != patient:
if s.patient_name_or_uid() != patient:
continue
if date_time != '%s_%s' % (s.date, s.time):
continue
Expand All @@ -188,7 +188,7 @@ def nifti(patient, date_time, scan):
def png(patient, date_time, scan):
study = None
for s in dft.get_studies():
if s.patient_name != patient:
if s.patient_name_or_uid() != patient:
continue
if date_time != '%s_%s' % (s.date, s.time):
continue
Expand All @@ -209,7 +209,6 @@ def png(patient, date_time, scan):

if __name__ == '__main__':
import wsgiref.simple_server
server_prefix = ''
httpd = wsgiref.simple_server.make_server('', 8080, application)
httpd.serve_forever()

Expand Down

0 comments on commit e8e70cc

Please sign in to comment.