-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload_annotation_slice.py
188 lines (163 loc) · 5.8 KB
/
upload_annotation_slice.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
import itk
import mdai
import numpy as np
def read_data_image(image_path):
"""
Read an image using itk, and returns a numpy data array.
"""
image = itk.imread(image_path)
data_np = itk.array_from_image(image)
return data_np
def upload_data_annotation_slice(
data_np: np.ndarray,
sop_instance_uid: str,
mdai_client: mdai.Client,
mdai_project_id: str,
mdai_dataset_id: str,
mdai_label_id: str,
) -> list:
"""
Uploads the input data annotation (a 2D slice) to the serverwith the specified sop_instance_uid.
Args:
data_np (np.ndarray): numpy array with pixel data.
sop_instance_uid (str): SOPInstanceUID of the DICOM key-slice image. Returned from @inverse_transform
mdai_client (mdai.Client): Client to the MD.ai API. See @get_mdai_client
mdai_project_id (str): Project ID. Check in the MD.ai web interface.
mdai_dataset_id (str): Dataset ID. Check in the MD.ai web interface.
mdai_label_id (str): Label ID. Check in the MD.ai web interface.
Returns:
failed_annotations (list): List of failed annotations. If empty, all annotations were uploaded successfully.
"""
annotation_dict = {
"labelId": mdai_label_id,
"SOPInstanceUID": sop_instance_uid,
"data": mdai.common_utils.convert_mask_data(data_np),
}
failed_annotations = mdai_client.import_annotations(
[annotation_dict], mdai_project_id, mdai_dataset_id
)
return failed_annotations
def upload_image_annotation_slice(
segmentation_image_path: str,
sop_instance_uid: str,
mdai_client: mdai.Client,
mdai_project_id: str,
mdai_dataset_id: str,
mdai_label_id: str,
) -> list:
"""
Uploads an annotation to the server. It requires that the DICOM image is already
uploaded to the server. See upload_dicom_image.py for that.
The input image can be in any format supported by ITK.
Args:
segmentation_image_path (str): Path to the segmentation image.
sop_instance_uid (str): SOPInstanceUID of the DICOM key-slice image. Returned from @inverse_transform
mdai_client (mdai.Client): Client to the MD.ai API. See @get_mdai_client
mdai_project_id (str): Project ID. Check in the MD.ai web interface.
mdai_dataset_id (str): Dataset ID. Check in the MD.ai web interface.
mdai_label_id (str): Label ID. Check in the MD.ai web interface.
Returns:
failed_annotations (list): List of failed annotations. If empty, all annotations were uploaded successfully.
"""
data_np = read_data_image(segmentation_image_path)
if data_np.ndim == 3:
# The perpendicular dimension is at index 0 in the numpy array.
data_np = data_np.squeeze(0)
return upload_data_annotation_slice(
data_np=data_np,
sop_instance_uid=sop_instance_uid,
mdai_client=mdai_client,
mdai_project_id=mdai_project_id,
mdai_dataset_id=mdai_dataset_id,
mdai_label_id=mdai_label_id,
)
def _get_parser():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input_annotation",
type=str,
required=True,
help="Path to the segmentation image to upload.",
)
parser.add_argument(
"-l",
"--label_name",
type=str,
required=True,
help="label name corresponding to the annotation.",
)
parser.add_argument(
"--sop_instance_uid",
type=str,
default=None,
help="sop_instance_uid of the annotation file. Needed to match the annotation with the DICOM image in mdai.",
)
parser.add_argument(
"-p",
"--parameters",
type=str,
default=None,
help="""
Path to a json file containing the parameters for md.ai variables: mdai_project_id, mdai_dataset_id, mdai_label_ids, etc.
See example in tests/test_parameters.json.
""",
)
return parser
def main(
input_annotation,
label_name,
sop_instance_uid,
mdai_client,
mdai_project_id,
mdai_dataset_id,
mdai_label_ids,
):
mdai_label_id = mdai_label_ids[label_name]
failed_annotations = upload_image_annotation_slice(
segmentation_image_path=input_annotation,
sop_instance_uid=sop_instance_uid,
mdai_client=mdai_client,
mdai_project_id=mdai_project_id,
mdai_dataset_id=mdai_dataset_id,
mdai_label_id=mdai_label_id,
)
return failed_annotations
if __name__ == "__main__":
import json
from mdai_utils.common import get_mdai_access_token
parser = _get_parser()
args = parser.parse_args()
print(args)
with open(args.parameters, "r") as f:
parameters = json.load(f)
mdai_project_id = parameters["mdai_project_id"]
mdai_dataset_id = parameters["mdai_dataset_id"]
mdai_label_ids = parameters["mdai_label_ids"]
mdai_domain = parameters["mdai_domain"]
input_annotation = args.input_annotation
label_name = args.label_name
mdai_label_id = mdai_label_ids[label_name]
sop_instance_uid = args.sop_instance_uid
if sop_instance_uid is None:
raise ValueError(
"sop_instance_uid is required to match the annotation with the DICOM image in mdai."
)
token = get_mdai_access_token()
mdai_client = mdai.Client(domain=mdai_domain, access_token=token)
failed_annotations = main(
input_annotation=input_annotation,
label_name=label_name,
sop_instance_uid=sop_instance_uid,
mdai_client=mdai_client,
mdai_project_id=mdai_project_id,
mdai_dataset_id=mdai_dataset_id,
mdai_label_ids=mdai_label_ids,
)
if len(failed_annotations) == 0:
print("All annotations uploaded successfully.")
exit(0)
else:
print(f"Failed annotations: {failed_annotations}")
exit(1)