Skip to content

Commit 0209a6e

Browse files
author
Christopher J. Bailey
committed
cp and rename Query from MLDB
1 parent b44880e commit 0209a6e

1 file changed

Lines changed: 200 additions & 0 deletions

File tree

access.py

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
"""
2+
=========================
3+
Methods to interact with the STORM database
4+
=========================
5+
6+
"""
7+
# Author: Chris Bailey <cjb@cfin.au.dk>
8+
#
9+
# License: BSD (3-clause)
10+
11+
12+
import subprocess as subp
13+
from sys import exit as sysexit
14+
from getpass import getuser, getpass
15+
import os
16+
17+
18+
class Query():
19+
"""
20+
Query object for communicating with the STORM database
21+
"""
22+
23+
def __init__(self, proj_code, stormdblogin='~/.stormdblogin', username=None, verbose=None):
24+
25+
if os.path.exists('/projects/' + proj_code):
26+
pass
27+
else:
28+
print 'ERROR: Bad project code?'
29+
sysexit(-1)
30+
31+
try:
32+
with open(os.path.expanduser(stormdblogin)):
33+
if verbose:
34+
print 'Reading login credentials from ' + stormdblogin
35+
f = open(os.path.expanduser(stormdblogin))
36+
self._login_code = f.readline()
37+
f.close()
38+
except IOError:
39+
print 'Login credentials not found, please enter them here'
40+
print 'WARNING: This might not work if you\'re in an IDE (e.g. spyder)!'
41+
if username:
42+
usr = username
43+
else:
44+
usr = getuser()
45+
46+
prompt = 'User \"%s\", please enter your password: ' % usr
47+
pwd = getpass(prompt)
48+
49+
url = 'login/username/' + usr + '/password/' + pwd
50+
output = self._wget_system_call(url)
51+
self._login_code = output
52+
#stormdblogin='~/.stormdbdblogin'
53+
print "Code generated, writing to %s" % stormdblogin
54+
fout = open(os.path.expanduser(stormdblogin), 'w')
55+
fout.write(self._login_code)
56+
fout.close()
57+
os.chmod(os.path.expanduser(stormdblogin), 0400)
58+
59+
self.proj_code = proj_code
60+
self._server = 'http://hyades00.pet.auh.dk/modules/StormDb/extract/'
61+
self._wget_cmd = 'wget -qO - test ' + self._server
62+
63+
@staticmethod
64+
def _wget_error_handling(stdout):
65+
if 'error' in stdout:
66+
print "Something is wrong, database answers as follows (dying...):"
67+
print stdout
68+
return -1
69+
70+
return 0
71+
72+
def _wget_system_call(self, url, verbose=False):
73+
cmd = self._wget_cmd + url
74+
75+
if verbose:
76+
print cmd
77+
78+
pipe = subp.Popen(cmd, stdout=subp.PIPE, stderr=subp.PIPE, shell=True)
79+
output, stderr = pipe.communicate()
80+
#output = subp.call([cmd,opts], shell=True)
81+
82+
if self._wget_error_handling(output) < 0:
83+
sysexit(-1)
84+
85+
return output
86+
87+
def get_subjects(self, subj_type='included', verbose=False):
88+
89+
if subj_type == 'all': # Doesn't work yet!
90+
scode = 'subjectswithcode'
91+
elif subj_type == 'included':
92+
scode = 'subjectswithcode'
93+
elif subj_type == 'excluded':
94+
scode = 'excludedsubjectswithcode'
95+
else:
96+
scode = 'subjectswithcode'
97+
98+
url = scode + '?' + self._login_code + '\\&projectCode=' + self.proj_code
99+
output = self._wget_system_call(url)
100+
101+
# Split at '\n'
102+
subj_list = output.split('\n')
103+
# Remove any empty entries!
104+
subj_list = [x for x in subj_list if x]
105+
106+
if verbose:
107+
print "Found following subjects:"
108+
print subj_list
109+
110+
return subj_list
111+
112+
def get_studies(self, subj_id, modality=None, unique=True, verbose=False):
113+
114+
url = 'studies?' + self._login_code + '\\&projectCode=' + self.proj_code + '\\&subjectNo=' + subj_id
115+
output = self._wget_system_call(url)
116+
117+
# Split at '\n'
118+
stud_list = output.split('\n')
119+
# Remove any empty entries!
120+
stud_list = [x for x in stud_list if x]
121+
122+
if modality:
123+
for study in stud_list:
124+
url = 'modalities?' + self._login_code + '\\&projectCode=' + self.proj_code + '\\&subjectNo=' + \
125+
subj_id + '\\&study=' + study
126+
output = self._wget_system_call(url).split('\n')
127+
#print output, '==', modality
128+
129+
for entry in output:
130+
if entry == modality:
131+
if unique:
132+
return study
133+
### NB!! This only matches first hit! If subject contains several studies
134+
### with this modality,
135+
### only first one is returned... Fix me!
136+
else:
137+
# must re-write code a bit to accommodate the existence of
138+
# several studies containing the desired modality...
139+
print "Error: non-unique modalities not implemented yet!"
140+
sysexit(-1)
141+
142+
# If we get this far, no studies found with the desired modality
143+
if verbose:
144+
print "No studies found with the desired modality"
145+
return None
146+
147+
else:
148+
return stud_list
149+
150+
def get_series(self, subj_id, study, modality, verbose=False):
151+
152+
url = 'series?' + self._login_code + '\\&projectCode=' + self.proj_code + '\\&subjectNo=' + \
153+
subj_id + '\\&study=' + study + '\\&modality=' + modality
154+
output = self._wget_system_call(url, verbose=verbose)
155+
156+
# Split at '\n'
157+
series_list = output.split('\n')
158+
# Remove any empty entries!
159+
series_list = [x for x in series_list if x]
160+
161+
# Return a 2D list with series number (as string) in 1st column and name as 2nd column
162+
series_list_2d = [x.split(' ') for x in series_list]
163+
164+
if verbose:
165+
print "Found following series:"
166+
print series_list_2d
167+
168+
return series_list_2d
169+
170+
def get_files(self, subj_id, study, modality, series, verbose=False):
171+
# NB: Series can be either just the number (1) or number.name (001.VS_1b_1)
172+
173+
url = 'files?' + self._login_code + '\\&projectCode=' + self.proj_code + \
174+
'\\&subjectNo=' + subj_id + '\\&study=' + study + '\\&modality=' + \
175+
modality + '\\&serieNo=' + series
176+
output = self._wget_system_call(url)
177+
178+
# Split at '\n'
179+
file_list = output.split('\n')
180+
# Remove any empty entries!
181+
file_list = [x for x in file_list if x]
182+
183+
if verbose:
184+
print "Found following files:"
185+
print file_list
186+
187+
return file_list
188+
189+
def __str__(self):
190+
print "Print not implemented yet!"
191+
192+
193+
if __name__ == '__main__':
194+
195+
#test code
196+
197+
project_code = 'MEG_service'
198+
199+
Q = Query(proj_code=project_code)
200+
print Q

0 commit comments

Comments
 (0)