Skip to content

Commit

Permalink
upload_annotations can be executed as script
Browse files Browse the repository at this point in the history
Improve README with the commands for download and upload
  • Loading branch information
phcerdan committed Oct 27, 2023
1 parent 0dda85d commit 99d2d29
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion mdai_utils/download_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
90 changes: 90 additions & 0 deletions mdai_utils/upload_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 99d2d29

Please sign in to comment.