From 99d2d294a79e486a821ade0f87baf577492103ef Mon Sep 17 00:00:00 2001 From: Pablo Hernandez-Cerdan Date: Fri, 27 Oct 2023 02:53:52 +0200 Subject: [PATCH] upload_annotations can be executed as script Improve README with the commands for download and upload --- README.md | 30 ++++++++++ mdai_utils/download_annotations.py | 4 +- mdai_utils/upload_annotations.py | 90 ++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 12381da..a8ac6ae 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,36 @@ Utility functions for MD.ai +## Download data + +- Download all data, dicom and annotations. Dicoms are only needed once, +or when data is added to the mdai dataset) + +```bash +python -m mdai_utils.download_annotations \ +--parameters myparameters.json \ +-o ./data \ +--download_dicoms +``` + +Once dicoms are downloaded, just download annotations, a json file will be generated in `./data`: + +```bash +python -m mdai_utils.download_annotations \ + --parameters myparameters.json \ + -o ./data +``` + +## Upload annotations / segmentations + +```bash +python -m mdai_utils.upload_annotations \ + --parameters ./tests/test_local_parameters.json \ + --sop_instance_uid "1.2.826.0.1.3680043.2.1125.1.75064541463040.2005072610414630768" \ + --label_name mylabel \ + -i ./tests/fixtures/humanct_0002_1000_seg.nii.gz +``` + ## Development For information about building, running, and contributing to this code base, diff --git a/mdai_utils/download_annotations.py b/mdai_utils/download_annotations.py index 5fa72d2..9c08955 100644 --- a/mdai_utils/download_annotations.py +++ b/mdai_utils/download_annotations.py @@ -163,9 +163,11 @@ def main(args): out_folder = args.out_folder or parameters.get("out_folder", DEFAULT_DATA_PATH) no_download = args.no_download or parameters.get("no_download", False) - mdai_annotations_only = not args.download_dicoms or parameters.get( + + download_dicoms = args.download_dicoms or not parameters.get( "mdai_annotations_only", True ) + mdai_annotations_only = not download_dicoms no_fixing_metadata = args.no_fixing_metadata or parameters.get( "mdai_no_fixing_metadata", False ) diff --git a/mdai_utils/upload_annotations.py b/mdai_utils/upload_annotations.py index 562dbbc..3d7224d 100644 --- a/mdai_utils/upload_annotations.py +++ b/mdai_utils/upload_annotations.py @@ -80,3 +80,93 @@ def upload_image_annotation_slice( mdai_label_id=mdai_label_id, sop_instance_uid=sop_instance_uid, ) + + +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( + "--sop_instance_metadata", + type=str, + default=None, + help="json file generated by storing the metadata resulting from dicom_utils.read_dicoms_into_volume", + ) + 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 + + +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_group_id = parameters["mdai_label_group_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 + sop_instance_metadata = args.sop_instance_metadata + if sop_instance_uid is None: + raise ValueError( + "Either sop_instance_uid or sop_instance_metadata must be set." + ) + token = get_mdai_access_token() + mdai_client = mdai.Client(domain=mdai_domain, access_token=token) + + 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, + ) + if len(failed_annotations) == 0: + print("All annotations uploaded successfully.") + exit(0) + else: + print(f"Failed annotations: {failed_annotations}") + exit(1)