-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.py
624 lines (534 loc) · 19.3 KB
/
cli.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# coding=utf-8
# Copyright (c) Jonas Teuwen
"""The slidescore-CLI module.
This module contains the CLI utilities that can be used with slidescore in python.
"""
import argparse
import csv
import json
import logging
import os
import pathlib
import re
import sys
from collections import defaultdict
from enum import Enum
from pathlib import Path
from typing import Iterable, Optional
import shapely.geometry
from tqdm import tqdm
from slidescore_api.api import APIClient, SlideScoreResult, build_client
from slidescore_api.cli_logging import build_cli_logger
from slidescore_api.utils.annotations import SlideScoreAnnotations, save_shapely
logger = logging.getLogger(__name__)
ANNOSHAPE_TYPES = ["polygon", "rect", "ellipse", "brush", "heatmap"]
class LabelOutputType(Enum):
"""
Enumerated Class for label output casting.
"""
JSON: str = "json"
RAW: str = "raw"
GEOJSON: str = "geojson"
def parse_api_token(data: Optional[Path] = None) -> str:
"""
Parse the API token from file or from the SLIDESCORE_API_KEY. If file is given, this will overwrite the
environment variable.
Parameters
----------
data : str or pathlib.Path
Returns
-------
str
SlideScore API Token.
"""
if data is not None and pathlib.Path(data).is_file():
# load token
with open(data, "r", encoding="utf-8") as file:
api_token = file.read().strip()
else:
api_token = os.environ.get("SLIDESCORE_API_KEY", "")
if not api_token:
logging.error(
"SlideScore API token not properly set. "
"Either pass API token file, or set the SLIDESCORE_API_KEY environmental variable."
)
sys.exit()
return api_token
def _shapely_to_slidescore(shapely_object):
shapely_type = type(shapely_object)
if shapely_type == shapely.geometry.Polygon:
if len(shapely_object.interiors) != 0:
if any(interior.area > 0 for interior in shapely_object.interiors):
raise RuntimeError(f"Expected Polygon to have empty interior. Got {list(shapely_object.interiors)}.")
coordinates = shapely_object.exterior.coords
if len(coordinates) < 3:
raise RuntimeError(f"Malformed Polygon. Got {coordinates}.")
answer = [{"x": int(x), "y": int(y)} for x, y in coordinates]
output = [{"type": "polygon", "points": answer}]
elif shapely_type == shapely.geometry.Point:
output = [{"x": int(shapely_object.x), "y": int(shapely_object.y)}]
elif shapely_type == shapely.geometry.MultiPolygon:
output = []
for shape in shapely_object.geoms:
shape_coords = [{"x": int(x), "y": int(y)} for x, y in shape.exterior.coords]
output.append({"type": "polygon", "points": shape_coords})
else:
raise NotImplementedError
# THis can be useful for boxes
# x0, y0, x1, y1 = curr_shape.bounds
# h = x1 - x0
# w = y1 - y0
# output = {
# "type": "rect",
# "corner": {"x": int(x0), "y": int(y0)},
# "size": {"x": int(h), "y": int(w)},
# }
return output
def _upload_labels_from_geojson(args: argparse.Namespace) -> None:
"""Main function that uploads geojson labels to SlideScore.
Parameters
----------
args: argparse.Namespace
The arguments passed from the CLI. Run with `-h` to see the required parameters
Returns
-------
None
"""
url = args.slidescore_url
api_token = parse_api_token(args.token_path)
study_id = args.study_id
client = APIClient(url, api_token)
wsi_results = []
answers = defaultdict(list) # type: ignore
with open(args.geojson_file, "r", encoding="utf-8") as geo_json_file:
data = json.load(geo_json_file)["features"]
for row in data:
shapely_object = shapely.geometry.shape(row["geometry"])
answer = _shapely_to_slidescore(shapely_object)
answers[row["properties"]["classification"]["name"]] += answer
for question in answers:
wsi_result = SlideScoreResult(
{
"imageID": args.image_id,
"imageName": args.image_name,
"user": args.user,
"question": question,
"answer": str(answers[question]).replace("'", '"'),
}
)
wsi_results.append(wsi_result)
client.upload_results(study_id, wsi_results)
def _upload_labels_from_csv(args: argparse.Namespace) -> None:
"""Main function that uploads labels to SlideScore.
Parameters
----------
args: argparse.Namespace
The arguments passed from the CLI. Run with `-h` to see the required parameters
Returns
-------
None
"""
url = args.slidescore_url
api_token = parse_api_token(args.token_path)
study_id = args.study_id
client = APIClient(url, api_token)
wsi_results = []
# Increase csv field size limit for large rows
try:
csv.field_size_limit(sys.maxsize)
except OverflowError:
csv.field_size_limit(int(sys.maxsize / 10))
with open(args.results_file, "r", encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile, delimiter=args.csv_delimiter, fieldnames=args.csv_fieldnames)
for row in reader:
image_id = row["imageID"]
image_name = row["imageName"]
user = row["user"] if args.user is None else args.user
question = row["question"]
answer = row["answer"].replace("'", '"') + "\n"
wsi_result = SlideScoreResult(
{
"imageID": image_id,
"imageName": image_name,
"user": user,
"question": question,
"answer": answer,
}
)
wsi_results.append(wsi_result)
client.upload_results(study_id, wsi_results)
def retrieve_questions(
slidescore_url: str,
api_token: str,
study_id: int,
disable_certificate_check: bool = False,
) -> dict:
"""
Retrieve the questions for a given study from SlideScore.
This call requires specific permissions, as it obtains the configuration of a given study.
Parameters
----------
slidescore_url: str
Url of the slidescore server e.g.: https://rhpc.nki.nl/slidescore/ (without Api/).
api_token: str
SlideScore API token.
study_id: int
Study id as used by SlideScore.
disable_certificate_check : bool
Returns
-------
scores: dict
Returns scores corresponding to a particular question in the slidescore study.
"""
client = build_client(slidescore_url, api_token, disable_certificate_check)
# Get the configuration for this study. Requires specific permissions.
config = client.get_config(study_id)
scores = config["scores"]
return scores
def _save_label_as_json(save_dir, image_id, image, annotations):
annotation_data = {
"image_id": image_id,
"study_id": image["studyID"],
"image_name": image["name"],
"annotations": [],
}
for annotation in annotations:
data = annotation.points
if not data:
continue
annotation_data["annotations"].append({"user": annotation.user, "question": annotation.question, "data": data})
# Now save this to JSON.
with open(save_dir / f"{image_id}.json", "w", encoding="utf-8") as file:
json.dump(annotation_data, file, indent=2)
def _row_iterator(slidescore_annotations: Iterable[SlideScoreResult]):
for annotation in slidescore_annotations:
yield annotation.to_row()
def download_labels( # pylint: disable=too-many-arguments,too-many-locals,too-many-branches
slidescore_url: str,
api_token: str,
study_id: int,
save_dir: Path,
output_type: str,
email: Optional[str] = None,
question: Optional[str] = None,
disable_certificate_check: bool = False,
) -> None:
"""
Downloads all available annotations for a study on SlideScore from
one specific author and saves them in a JSON file per image.
Parameters
__________
slidescore_url: str
Url of the slidescore server e.g.: https://rhpc.nki.nl/slidescore/ (without Api/).
api_token: str
SlideScore API token.
study_id: int
Study id as used by SlideScore.
save_dir: Path
Directory to save the labels to.
output_type: str
User defined output format in which the annotations need to be saved.
email: str, optional
The author email/name as registered on SlideScore to download those specific annotations.
question : str
The question to obtain the labels for. If not given all labels will be obtained.
disable_certificate_check : bool
Disable HTTPS certificate check.
Returns
-------
None
"""
client = build_client(slidescore_url, api_token, disable_certificate_check)
if not save_dir.is_dir():
save_dir.mkdir()
extra_kwargs = {}
if email is not None:
extra_kwargs["email"] = email
if question is not None:
extra_kwargs["question"] = question
images = client.get_images(study_id)
for image in tqdm(images):
image_id = image["id"]
annotations = client.get_results(study_id, imageid=image_id, **extra_kwargs)
if LabelOutputType[output_type] == LabelOutputType.JSON:
_save_label_as_json(save_dir, image_id, image, annotations)
elif LabelOutputType[output_type] == LabelOutputType.RAW:
with open(save_dir / "annotations.txt", "a", encoding="utf-8") as file:
for annotation in annotations:
file.write(annotation.to_row() + "\n")
elif LabelOutputType[output_type] == LabelOutputType.GEOJSON:
annotation_parser = SlideScoreAnnotations()
row_iterator = _row_iterator(annotations)
for curr_annotation in annotation_parser.from_iterable(
row_iterator, filter_author=email, filter_label=question
):
save_shapely(curr_annotation, save_dir=save_dir)
else:
raise RuntimeError(f"Output type {output_type} not supported.")
def _download_labels(args: argparse.Namespace) -> None:
"""Main function that downloads labels from SlideScore.
Parameters
----------
args: argparse.Namespace
The arguments passed from the CLI. Run with `-h` to see the required parameters
Returns
-------
None
"""
build_cli_logger("download_labels", log_to_file=not args.no_log, verbosity_level=args.verbose)
api_token = parse_api_token(args.token_path)
download_labels(
args.slidescore_url,
api_token,
args.study_id,
args.output_dir,
output_type=args.output_type,
question=args.question,
email=args.user,
disable_certificate_check=args.disable_certificate_check,
)
def append_to_manifest(save_dir: pathlib.Path, image_id: int, filename: pathlib.Path) -> None:
"""
Create a manifest mapping image id to the filename.
Parameters
----------
save_dir : pathlib.Path
image_id : int
filename : pathlib.Path
Returns
-------
None
"""
with open(save_dir / "slidescore_mapping.txt", "a", encoding="utf-8") as file:
file.write(f"{image_id} {filename.name}\n")
def download_wsis(
slidescore_url: str,
api_token: str,
study_id: int,
save_dir: pathlib.Path,
disable_certificate_check: bool = False,
regex: str = None,
) -> None:
"""
Download all WSIs for a given study from SlideScore
Parameters
----------
slidescore_url : str
api_token : str
study_id : int
save_dir : pathlib.Path
disable_certificate_check : bool
regex: str
Regex to apply to the list of images in the given study
Returns
-------
None
"""
logger.info("Will write to: %s", save_dir)
# Set up client and directories
client = build_client(slidescore_url, api_token, disable_certificate_check)
save_dir.mkdir(exist_ok=True)
# Collect image metadata
images = client.get_images(study_id)
# This call doesn't have a regex filter and will return all images
if regex is not None:
pattern = re.compile(regex)
images = [item for item in images if pattern.match(item["name"])]
logger.info("Found %s images.", len(images))
# Download and save WSIs
for image in tqdm(images):
image_id = image["id"]
if (save_dir / image_id).exists():
logger.info("Skipping: image id folder already exists: %s", image_id)
continue
logger.info("Downloading image for id: %s", image_id)
filename = client.download_slide(study_id, image, save_dir=save_dir)
logger.info("Image with id %s has been saved to %s.", image_id, filename)
append_to_manifest(save_dir, image_id, filename)
def _download_wsi(args: argparse.Namespace):
"""Main function that downloads WSIs from SlideScore.
Parameters
----------
args: argparse.Namespace
The arguments passed from the CLI. Run with `-h` to see the required parameters
Returns
-------
None
"""
# build_cli_logger("download_wsis", log_to_file=not args.no_log, verbosity_level=args.verbose)
api_token = parse_api_token(args.token_path)
download_wsis(
args.slidescore_url,
api_token,
args.study_id,
args.output_dir,
disable_certificate_check=args.disable_certificate_check,
regex=args.regex,
)
def register_parser(parser: argparse._SubParsersAction):
# pylint: disable=protected-access
"""Register slidescore commands to a root parser."""
# Download slides to a subfolder
download_wsi_parser = parser.add_parser("download-wsis", help="Download WSIs from SlideScore.")
download_wsi_parser.add_argument(
"output_dir",
type=pathlib.Path,
help="Directory to save output too.",
)
download_wsi_parser.add_argument("--regex", default=None, help="Regex to apply to the list of images in the given study. For instance '^T' will only download images starting with a T.")
download_wsi_parser.set_defaults(subcommand=_download_wsi)
download_label_parser = parser.add_parser("download-labels", help="Download labels from SlideScore.")
download_label_parser.add_argument(
"-q",
"--question",
dest="question",
help="Question to save annotations for. If not set, will return all questions.",
type=str,
required=False,
)
download_label_parser.add_argument(
"-u",
"--user",
dest="user",
help="Email(-like) reference indicating submitted annotations on slidescore. "
"If not set, will return questions from all users.",
type=str,
required=False,
)
download_label_parser.add_argument(
"-o",
"--output-type",
dest="output_type",
help="Type of output. GeoJSON is a compliant GeoJSON output.",
type=str,
choices=LabelOutputType.__members__,
default="GEOJSON",
)
download_label_parser.add_argument(
"output_dir",
type=pathlib.Path,
help="Directory to save output too.",
)
download_label_parser.set_defaults(subcommand=_download_labels)
upload_csv_parser = parser.add_parser("upload-labels-from-csv", help="Upload labels to SlideScore.")
upload_csv_parser.add_argument(
"--csv-delimiter",
type=str,
help="The delimiter character used in the csv file.",
default="\t",
)
upload_csv_parser.add_argument(
"-u",
"--user",
dest="user",
help="Email(-like) reference indicating submitted annotations on SlideScore. "
"If not set, will use the one included in the results file.",
type=str,
required=False,
)
upload_csv_parser.add_argument(
"--csv-fieldnames",
nargs="*",
type=str,
default=["imageID", "imageName", "user", "question", "answer"],
)
upload_csv_parser.add_argument(
"-r",
"--results-file",
type=str,
required=True,
help="The results-file should be .csv file, separated with columns "
"imageID, imageNumber, user, question, answer (or as given by --csv-fieldnames). "
"User is the email(-like) address to register this "
"upload as a unique entry, question is the type of cell (e.g.: lymphocytes, tumour cells) that "
"pertains to the upload and answer contains a list of annotations (e.g.: ellipse, rectangle, polygon) "
"to be uploaded to SlideScore. See the documentation for some examples.",
)
upload_csv_parser.set_defaults(subcommand=_upload_labels_from_csv)
upload_json_parser = parser.add_parser("upload-labels-from-geojson", help="Upload labels to SlideScore.")
upload_json_parser.add_argument(
"-u",
"--user",
dest="user",
help="Email(-like) reference indicating submitted annotations on SlideScore. "
"If not set, will use the one included in the results file.",
type=str,
required=True,
)
upload_json_parser.add_argument(
"-i",
"--image-id",
type=str,
required=True,
help="SlideScore Image ID",
)
upload_json_parser.add_argument(
"-n",
"--image-name",
type=str,
required=True,
help="SlideScore Image Name",
)
upload_json_parser.add_argument(
"-g",
"--geojson-file",
type=str,
required=True,
help="GeoJSON object file",
)
upload_json_parser.set_defaults(subcommand=_upload_labels_from_geojson)
def cli():
"""
Console script for SlideScore API.
"""
# From https://stackoverflow.com/questions/17073688/how-to-use-argparse-subparsers-correctly
slidescore_parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
slidescore_parser.add_argument(
"--slidescore-url",
type=str,
help="URL for SlideScore",
default="https://slidescore.nki.nl/",
)
slidescore_parser.add_argument(
"-t",
"--token-path",
dest="token_path",
type=str,
required=os.environ.get("SLIDESCORE_API_KEY", None) is None,
help="Path to file with API token. Required if SLIDESCORE_API_KEY environment variable is not set. "
"Will overwrite the environment variable if set.",
)
slidescore_parser.add_argument(
"-s",
"--study",
dest="study_id",
help="SlideScore Study ID",
type=int,
required=True,
)
slidescore_parser.add_argument(
"--disable-certificate-check",
help="Disable the certificate check.",
action="store_true",
)
slidescore_parser.add_argument(
"--no-log",
help="Disable logging.",
action="store_true",
)
slidescore_parser.add_argument(
"-v",
"--verbose",
action="count",
help="Verbosity level, e.g. -v, -vv, -vvv",
default=0,
)
slidescore_subparsers = slidescore_parser.add_subparsers(help="Possible SlideScore CLI utils to run.")
slidescore_subparsers.required = True
slidescore_subparsers.dest = "subcommand"
# SlideScore related commands
register_parser(slidescore_subparsers)
args = slidescore_parser.parse_args()
args.subcommand(args)
if __name__ == "__main__":
cli()