generated from Sage-Bionetworks-Challenges/model-to-data-challenge-workflow
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_tool.py
240 lines (223 loc) · 8.65 KB
/
validate_tool.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""Run training synthetic docker models"""
import argparse
import json
import os
import docker
def remove_docker_container(container_name):
"""Remove docker container"""
client = docker.from_env()
try:
cont = client.containers.get(container_name)
cont.stop()
cont.remove()
except Exception:
print("Unable to remove container")
def remove_docker_image(image_name):
"""Remove docker image"""
client = docker.from_env()
try:
client.images.remove(image_name, force=True)
except Exception:
print("Unable to remove image")
def main(args):
"""Run docker model"""
client = docker.from_env()
invalid_reasons = []
print("Get submission container")
# Get container
submissionid = args.submissionid
container = client.containers.get(submissionid)
# This obtains the ip of each docker container only accesible to other
# docker containers on the same network
# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name
container_ip = container.attrs['NetworkSettings'][
'Networks'
]['submission']['IPAddress']
print(container_ip)
api_url_map = {
'nlpsandbox:date-annotator': "textDateAnnotations",
'nlpsandbox:person-name-annotator': "textPersonNameAnnotations",
'nlpsandbox:location-annotator': "textLocationAnnotations",
'nlpsandbox:id-annotator': "textIdAnnotations",
'nlpsandbox:contact-annotator': "textContactAnnotations",
'nlpsandbox:covid-symptom-annotator': "textCovidSymptomAnnotations"
}
annotator_client = "nlpsandbox/cli:4.2.0"
# validate that the root URL redirects to the service API endpoint
# exec_cmd = ["curl", "-s", "-L", "-X", "GET",
# f"http://{container_ip}:8080"]
exec_cmd = ["tool", "get-tool", '--annotator_host',
f"http://{container_ip}:8080/api/v1"]
# "--output", "/output/tool.json"]
# Incase getting tool info fails, add empty dict
new_tool_info = {}
output_dir = os.path.join(os.getcwd(), "output")
os.mkdir(output_dir)
print(output_dir)
volumes = {
output_dir: {
'bind': '/output',
'mode': 'rw'
}
}
import time
time.sleep(70)
try:
# auto_remove doesn't work when being run with the orchestrator
tool = client.containers.run(annotator_client, exec_cmd,
name=f"{args.submissionid}_curl_1",
network="submission", stderr=True,
volumes=volumes)
# auto_remove=True)
# with open("tool.json") as tool_f:
# tool_info = json.load(tool_f)
# Remove \n, and change appropriate single quotes to double quotes
# so that the string can be turned into a json
tool_info = json.loads(
tool.decode("utf-8")
.replace("\n", "")
.replace("', '", '", "')
.replace("': '", '": "')
.replace("{'", '{"')
.replace("'}", '"}')
.replace('", \'', '", "')
.replace('\': "', '": "')
)
# print(tool_info)
# Check that tool api version is correct
if tool_info.get('api_version') != args.schema_version:
invalid_reasons.append(
f"API api/v1/tool apiVersion is not {args.schema_version}"
)
# Create new dict key names
for key, value in tool_info.items():
new_key = f"tool__{key}"
new_tool_info[new_key] = value
except Exception as err:
print(err)
# TODO: Potentially add in more info
invalid_reasons.append(
"API api/v1/tool endpoint not implemented or implemented "
"incorrectly. Make sure correct tool object is returned.\n"
)
remove_docker_container(f"{args.submissionid}_curl_1")
# Check UI
exec_cmd = ["tool", "check-url", '--url',
f"http://{container_ip}:8080/api/v1/ui"]
try:
# auto_remove doesn't work when being run with the orchestrator
client.containers.run(annotator_client, exec_cmd,
name=f"{args.submissionid}_curl_2",
network="submission", stderr=True)
# auto_remove=True)
except Exception as err:
print(err)
invalid_reasons.append(
".../api/v1/ui not implemented or implemented incorrectly.\n"
)
remove_docker_container(f"{args.submissionid}_curl_2")
# validate that the note can be annotated by particular annotator
# example_note = [{
# "identifier": "awesome-note",
# "noteType": "loinc:LP29684-5",
# "patientId": "awesome-patient",
# "text": "On 12/26/2020, Ms. Chloe Price met with Dr. Prescott in \
# Seattle."
# }]
# with open("example_note.json", "w") as example_f:
# json.dump(example_note, example_f)
# TODO: need to support other annotators once implemented
exec_cmd = ["tool", "annotate-note", '--annotator_host',
f"http://{container_ip}:8080/api/v1", '--note_json',
'/example_note.json', '--tool_type',
args.annotator_type]
volumes = {
os.path.abspath(args.subset_data): {
'bind': '/example_note.json',
'mode': 'rw'
}
}
# Run first time
example_dict = {}
try:
example_post = client.containers.run(
annotator_client, exec_cmd,
volumes=volumes,
name=f"{args.submissionid}_curl_3",
network="submission", stderr=True,
# auto_remove=True
)
example_dict = json.loads(
example_post.decode("utf-8").replace("\n", "").replace("'", '"')
)
print(example_dict)
except Exception as err:
print(err)
invalid_reasons.append(
f"API /{api_url_map[args.annotator_type]} endpoint not implemented "
"or implemented incorrectly. Make sure correct Annotation "
"object is annotated.\n"
)
remove_docker_container(f"{args.submissionid}_curl_3")
# Run second time
example_dict_2 = {}
try:
example_post_2 = client.containers.run(
annotator_client, exec_cmd,
volumes=volumes,
name=f"{args.submissionid}_curl_4",
network="submission", stderr=True,
# auto_remove=True
)
example_dict_2 = json.loads(
example_post_2.decode("utf-8").replace("\n", "").replace("'", '"')
)
print(example_dict_2)
except Exception as err:
print(err)
invalid_reasons.append(
f"API /{api_url_map[args.annotator_type]} endpoint not implemented "
"or implemented incorrectly. Make sure correct Annotation "
"object is annotated.\n"
)
remove_docker_container(f"{args.submissionid}_curl_4")
if example_dict != example_dict_2:
invalid_reasons.append(
"Annotated results must be the same after running the annotator"
"twice on the same dataset"
)
print("finished")
print(invalid_reasons)
# If there are no invalid reasons -> Validated
if not invalid_reasons:
prediction_file_status = "VALIDATED"
else:
prediction_file_status = "INVALID"
# Try to remove the image if the service is invalid
remove_docker_container(args.submissionid)
remove_docker_image(container.image)
result = {'submission_errors': "\n".join(invalid_reasons),
'submission_status': prediction_file_status}
result.update(new_tool_info)
with open(args.results, 'w') as file_o:
file_o.write(json.dumps(result))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--submissionid", required=True,
help="Submission Id", type=str)
parser.add_argument("-c", "--synapse_config", required=True,
help="credentials file")
parser.add_argument("-r", "--results", required=True,
help="results file")
parser.add_argument("-a", "--annotator_type", required=True,
help="Annotation Type")
parser.add_argument(
"--subset_data", required=True,
help="The subset of data to validate reproducibility of notes."
)
parser.add_argument(
"--schema_version", required=True,
help="The API verison of the data node and annotator schemas."
)
args = parser.parse_args()
main(args)