Skip to content

Commit

Permalink
Test upload_dataset
Browse files Browse the repository at this point in the history
Add custom option --upload-only to pytest.

The test will only run when that argument is passed. Skipped otherwise.

Minor changes to the test_parameters file.
  • Loading branch information
phcerdan committed Oct 26, 2023
1 parent 1c69081 commit 6e13f0a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data/
tests/test_local_parameters.json

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ please read the [CONTRIBUTING](CONTRIBUTING.md) page.
Sample data for testing acquired from:
[PROJECT: HumanCT > SUBJECT: VHFCT1mm-Pelvis > 000-000-002](https://central.xnat.org/app/action/DisplayItemAction/search_element/xnat%3ActSessionData/search_field/xnat%3ActSessionData.ID/search_value/CENTRAL04_E04384/popup/false/project/HumanCT)
and manually segmented using [Slicer3D](https://www.slicer.org/).

## Tests

Create a copy of the test_parameters where you will provide the mdai ids needed
to upload and download the test data.

`cp tests/test_parameters.json tests/test_local_parameters.json`

We provide a tiny dataset that you can upload to a test dataset in your md.ai
instance. Only needed to run once: `pytest tests -rP --upload-only`.
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions upload_dataset.py → mdai_utils/upload_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def upload_dataset(mdai_dataset_id, dir_path, order_exams_by="default"):
f"{order_exams_by}",
f"{dir_path}",
]
return subprocess.run(
completed_process = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
encoding="utf-8",
)
if completed_process.returncode or completed_process.stderr:
raise Exception(f"Error uploading dataset: {completed_process}.")
return completed_process


if __name__ == "__main__":
Expand Down
59 changes: 51 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,66 @@
import json
import os
from pathlib import Path

import mdai
import pytest

_current_dir = Path(__file__).parent if "__file__" in locals() else Path(".")
from mdai_utils.common import get_mdai_access_token


def pytest_addoption(parser):
"""Add a command line option to pytest."""
parser.addoption(
"--upload-only",
action="store_true",
default=False,
help="run only tests marked as upload_only",
)


# Register the custom mark to avoid pytest warnings
def pytest_configure(config):
config.addinivalue_line(
"markers", "upload_only: Mark test to run only when --upload-only is provided"
)


@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item):
"""Hook that runs before a test is set up."""
is_upload_only_test = "upload_only" in item.keywords
upload_only_mode = item.config.getoption("--upload-only")

if upload_only_mode:
if not is_upload_only_test:
pytest.skip("Skipping non-upload tests in upload-only mode")
else:
if is_upload_only_test:
pytest.skip("Skipping upload-only tests in standard mode")


_current_dir = Path(__file__).parent


@pytest.fixture(scope="session")
def mdai_setup(parameters_file=_current_dir / "test_parameters.json", input_token=None):
token = input_token or os.getenv("MDAI_TOKEN", "")
if token == "":
raise ValueError(
"Please set the MDAI_TOKEN environment variable with your MDAI credentials."
def mdai_setup(
parameters_file=_current_dir / "test_local_parameters.json", input_token=None
):
# Check if the parameters file exists
print(f"Parameters file: {parameters_file}")
if not parameters_file.exists():
existing_parameters_file = _current_dir / "test_parameters.json"
raise FileNotFoundError(
f"Parameters file {parameters_file} not found. Please create one, using {existing_parameters_file} as a template."
)
token = input_token or get_mdai_access_token()
with open(parameters_file) as f:
parameters = json.load(f)

fixture_dir = _current_dir / "fixtures"
mdai_domain = parameters.get("mdai_domain") or "md.ai"
mdai_client = mdai.Client(domain=mdai_domain, access_token=token)
return {"mdai_client": mdai_client, "parameters": parameters}
return {
"mdai_client": mdai_client,
"parameters": parameters,
"fixture_dir": fixture_dir,
}
10 changes: 4 additions & 6 deletions tests/test_parameters.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
"mdai_domain": "uab.md.ai",
"mdai_domain": "md.ai",
"mdai_token_env_variable": "MDAI_TOKEN",
"mdai_project_id": "L1NprBvP",
"mdai_dataset_id": "D_odXMLm",
"mdai_label_group_id": "G_P914xZ",
"mdai_label_group_id": "G_2Jy2yZ",
"mdai_label_ids": {
"heart": "L_W4Xx64",
"inner-chest": "L_npal0d"
"mylabel": "L_W4XQKd"
},
"mdai_annotations_only": true,
"mdai_no_fixing_metadata": false,
"labels": [
"heart",
"inner-chest"
"mylabel",
]
}
26 changes: 24 additions & 2 deletions tests/upload_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
# from mdai_utils.upload import upload_data_annotation_slice
import pytest

from mdai_utils.upload_dataset import upload_dataset

def test_upload_mask_annotation(mdai_setup):

def test_pytest_fixture(mdai_setup):
mdai_parameters = mdai_setup["parameters"]
mdai_project_id = mdai_parameters.get("mdai_project_id")
assert mdai_project_id is not None


@pytest.mark.upload_only(
reason="Only need to upload once. run pytest tests with --upload-only to run it."
)
def test_upload_dataset(mdai_setup):
mdai_parameters = mdai_setup["parameters"]
mdai_dataset_id = mdai_parameters.get("mdai_dataset_id")
fixture_dir = mdai_setup["fixture_dir"]
dicom_dir = fixture_dir / "humanct_0002_1000_1004"
assert fixture_dir.exists()
completed_process = upload_dataset(mdai_dataset_id, dicom_dir)
process_message = completed_process.stdout.strip()
print(process_message)
# Check the status of subprocess
assert completed_process.returncode == 0


def test_upload_annotation(mdai_setup):
mdai_setup["parameters"]

0 comments on commit 6e13f0a

Please sign in to comment.