-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_interface.py
More file actions
executable file
·189 lines (163 loc) · 7.21 KB
/
Copy pathexample_interface.py
File metadata and controls
executable file
·189 lines (163 loc) · 7.21 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
import subprocess
import os
import sys
def show_help(verbosity=1):
if verbosity == 1:
print('Basic usage for scan analysis:\n'
'. nstc.py <classifier_to_use> <absolute_path_to_scan_directory>\n'
'To see additional production classifer usage:\n'
'. nstc.py -h 2\n'
'To see training usage:\n'
'. nstc.py --help\n')
elif verbosity == 2:
print('Production classifier usage:\n'
'. nstc.py <classifier_to_use> <absolute_path_to_scan_directory>\n'
'ex: . nstc.py -1 /mypatient\n'
'To download project repository:\n'
'. nstc.py --setup\n'
'To install or update dependencies:\n'
'. nstc.py <classifier> -r\n'
'To make a competition submission:\n'
'. nstc.py <classifier> -c <absolute_path_to_competition_tld>\n'
'where competition_tld mirrors /testing structure from deployment_file_structure\n')
elif verbosity == 3:
print('Retraining usage:\n'
'WARNING: These operations require GPU acceleration,\n'
'approx ~1TB of storage, and days of operation for many functions.\n\n'
'To download repository and data:\n'
'. nstc.py --training --setup\n'
'To install or update dependencies:\n'
'. nstc.py --training -r\n'
'To retrain a classifier:'
'. nstc.py --training --retrain <classifier>\n'
'ex: . nstc.py --training --retrain -1\n')
else:
raise ValueError
def require_dir(usr_path):
if len(usr_path.split('/')) > 2:
pass
# TODO Upgrade to handle multiple directory creation
# i.e. require_dir('/storage/NSTC') first runs require_dir('/storage') then require_dir('/storage/NSTC')
if not os.path.exists(usr_path):
os.mkdir(usr_path)
def download_code():
require_dir('/storage')
require_dir('/storage/nstc')
os.chdir('/storage/nstc')
if not os.path.exists('/storage/nstc/DataScienceBowl17'):
subprocess.run(['git', 'clone', 'git@github.com:gioGats/DataScienceBowl17.git'])
else:
os.chdir('/storage/nstc/DataScienceBowl17')
subprocess.run(['git', 'stash'])
subprocess.run(['git', 'pull'])
def project_requirements(clf):
if clf == '1':
os.chdir('/storage/nstc/DataScienceBowl17/production_1')
elif clf == '2':
os.chdir('/storage/nstc/DataScienceBowl17/production_2')
elif clf == 'x':
os.chdir('/storage/nstc/DataScienceBowl17/experimentation')
else:
raise ValueError
subprocess.run(['pip3', 'install', '--upgrade', '-r', 'requirements.txt'])
def download_training_data():
require_dir('/storage/nstc')
os.chdir('/storage/nstc')
raise NotImplementedError
# Will need to identify some large file hosting capability
# subprocess.run(['scp', 'ftp@some.host:/storage/nstc/compressed_data.tar.lrz', 'raw_data.tar.lrz'])
# subprocess.run(['lrztar', '-d', 'raw_data.tar.lrz'])
# subprocess.run(['rm', 'raw_data.tar.lrz'])
def structure_training_data():
raise NotImplementedError
# os.chdir('/storage/nstc/raw_data')
# subprocess.run(['cp', '/storage/nstc/DataScienceBowl17/misc/sams_script.sh', '/storage/nstc/raw_data/'])
# subprocess.run(['.', 'sams_script.sh'])
# subprocess.run(['rm', 'sams_script.sh'])
# subprocess.run(['rm', '*.tar.gz'])
def retrain_production_1():
raise NotImplementedError
def retrain_production_2():
raise NotImplementedError
def classify(clf, patient_dir):
if clf == '1':
os.chdir('/storage/nstc/DataScienceBowl17/production_1')
elif clf == '2':
os.chdir('/storage/nstc/DataScienceBowl17/production_2')
# TODO Load the model and classify
else:
raise ValueError('Invalid classifier')
def make_submission(clf, competition_tld):
i = 0
while os.path.exists('/storage/nstc/testing/submission_clf%s_%d.csv' % (clf, i)):
i += 1
with open('', 'w') as f:
f.write('patient_id,cancer\n')
for patient_dir in os.listdir(competition_tld + '/test_images'):
prediction = classify(clf, '%s/test_images/%s' % (competition_tld, patient_dir))
f.write('%s,%.4f\n' % (patient_dir, prediction))
if __name__ == '__main__':
if '-h' in sys.argv or len(sys.argv) < 2:
try:
v = int(sys.argv[sys.argv.index('-h') + 1])
except ValueError:
v = 1
show_help(verbosity=v)
elif '--help' in sys.argv:
show_help(verbosity=3)
elif '--training' in sys.argv:
print('WARNING: You have selected training-level commands.\n'
'These operations require GPU acceleration, approx ~1TB of storage, '
'and days of operation for many functions.\n')
if input('Continue? [y/n] ').lower() == 'y':
if '--setup' in sys.argv:
print('WARNING: Initiating a ~120GB download.\n'
'Depending on connection speed, you need to either take a coffee break or a summer vacation.\n'
'Decompression and structuring will ultimately require ~500GB of disk space\n'
'YOU HAVE BEEN WARNED')
if input('Continue? [y/n]').lower() == 'y':
download_code()
download_training_data()
structure_training_data()
else:
sys.exit()
if '--setup' in sys.argv or '-r' in sys.argv:
project_requirements('x')
if '--retrain' in sys.argv:
print('WARNING: Initiating a classifer retraining.\n'
'This will overwrite the pre-trained model available via the git repository.\n'
'It will not alter model design or training parameters.\n'
'Such alterations must be done via source code modifications.\n'
'Depending on your hardware, retraining may require either a coffee break or a summer vacation.\n'
'YOU HAVE BEEN WARNED.')
if input('Continue? [y/n] ').lower() == 'y':
if '-1' in sys.argv:
retrain_production_1()
elif '-2' in sys.argv:
retrain_production_2()
else:
raise ValueError('Classifier not specified')
else:
sys.exit()
else:
sys.exit()
elif '--setup' in sys.argv:
download_code()
else:
if '-1' in sys.argv:
usr_clf = '1'
elif '-2' in sys.argv:
usr_clf = '2'
else:
raise ValueError('Classifier not specified')
if '-r' in sys.argv:
project_requirements(usr_clf)
elif '-c' in sys.argv:
usr_comp_tld = sys.argv[-1]
make_submission(usr_clf, usr_comp_tld)
else:
usr_patient_dir = sys.argv[-1]
usr_patient_id = usr_patient_dir.split('/')[-1]
usr_patient_prob = classify(usr_clf, usr_patient_dir)
print('Patient %s lung cancer probability is %.4f' % (usr_patient_id, usr_patient_prob))