Skip to content

Commit

Permalink
Add upload_dataset script
Browse files Browse the repository at this point in the history
It requires the mdai CLI, there is no python API at the time of writting
  • Loading branch information
phcerdan committed Oct 26, 2023
1 parent 91966bc commit 03fac34
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions upload_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import subprocess


def upload_dataset(mdai_dataset_id, dir_path, order_exams_by="default"):
"""
Upload a dicom dataset to MD.ai via CLI.
A wrapper for the MD.ai CLI command:
mdai dataset load --dataset-id <mdai_dataset_id> --order-exams-by <order_exams_by> <dir_path>
Note There is no Python API for uploading a dicom (a dataset), so we use the cli.
Ensure you have the mdai CLI installed: https://docs.md.ai/cli/installation/
Args:
mdai_dataset_id (str): The dataset id provided by MD.ai to upload to.
dir_path (str): The path to the directory containing the dicom images.
order_exams_by (str, optional): The order of the exams. Defaults to "default".
Options: default, patient_id, study_date_time, study_desc, random
Returns:
subprocess.CompletedProcess: The result of the subprocess call.
"""
# Check that mdai CLI is installed
try:
subprocess.run(["mdai", "version"], check=True)
except FileNotFoundError:
raise FileNotFoundError(
"The mdai CLI is not installed. Please install it from https://docs.md.ai/cli/installation/"
)
command = [
"mdai",
"dataset",
"load",
"--dataset-id",
f"{mdai_dataset_id}",
"--order-exams-by",
f"{order_exams_by}",
f"{dir_path}",
]
return subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
encoding="utf-8",
)


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser(
description="Upload a dicom dataset to MD.ai via CLI."
)
parser.add_argument(
"-i",
"--mdai_dataset_id",
type=str,
help="The dataset id provided by MD.ai to upload to.",
required=True,
)
parser.add_argument(
"--dir_path",
type=str,
help="The path to the directory containing the dicom images.",
required=True,
)
parser.add_argument(
"-o",
"--order-exams-by",
type=str,
help="The order of the exams. Defaults to 'default'. Options: default, patient_id, study_date_time, study_desc, random",
default="default",
)
args = parser.parse_args()

mdai_dataset_id = args.mdai_dataset_id
dataset_path = args.dir_path

completed_process = upload_dataset(mdai_dataset_id, dataset_path)
print(f"To follow progress, run:\nmdai dataset progress -i {mdai_dataset_id}")
if completed_process.returncode or completed_process.stderr:
print(completed_process)

0 comments on commit 03fac34

Please sign in to comment.