Skip to content

Commit

Permalink
test(nbia): added test for downloadSeries
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Jan 20, 2024
1 parent c2b70b3 commit 0f502ce
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions tests/test_nbia.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#####
# pytest -v -n 8

# To run only one of the tests:
# pytest -v -n 8 tests/test_nbia.py::test_getCollections

import pytest
from nbiatoolkit import NBIAClient
from tempfile import TemporaryDirectory
import os

@pytest.fixture(scope="session")
def nbia_client():
Expand All @@ -25,6 +31,52 @@ def nbia_patients(nbia_client, nbia_collections):
Collection=nbia_collections[0], Modality = "CT")
return patients


def test_downloadSeries(nbia_client, nbia_collections, nbia_patients):
seriesList = nbia_client.getSeries(
Collection=nbia_collections[0],
PatientID=nbia_patients[0],
Modality="CT"
)
filePattern = '%PatientID/%Modality/%SeriesNumber-%SeriesInstanceUID/%InstanceNumber.dcm'
# create a temporary directory

tempdir = TemporaryDirectory()
tempdir = tempdir.name

nbia_client.downloadSeries(
SeriesInstanceUID=seriesList[0]["SeriesInstanceUID"],
downloadDir=tempdir,
filePattern=filePattern
)
dir = os.listdir(tempdir)

assert len(dir) == 1
assert dir[0] == nbia_patients[0]
assert os.path.isdir(os.path.join(tempdir, dir[0]))

modality_dir = os.listdir(os.path.join(tempdir, dir[0]))
assert len(modality_dir) == 1
assert modality_dir[0] == "CT"
assert os.path.isdir(os.path.join(tempdir, dir[0], modality_dir[0]))

series_dir = os.listdir(os.path.join(tempdir, dir[0], modality_dir[0]))
assert len(series_dir) == 1
# only last 5 digits of SeriesInstanceUID are used
assert series_dir[0] == "{}-{}".format(
seriesList[0]["SeriesNumber"], seriesList[0]["SeriesInstanceUID"][-5:])
assert os.path.isdir(os.path.join(tempdir, dir[0], modality_dir[0], series_dir[0]))

dicom_dir = os.listdir(os.path.join(
tempdir, dir[0], modality_dir[0], series_dir[0]))

assert len(dicom_dir) == int(seriesList[0]["ImageCount"])
for file in dicom_dir:
assert file.endswith(".dcm")
assert file[:-4].isdigit()



def test_nbiaclient_access_token(nbia_client):
assert nbia_client.api_headers is not None

Expand Down Expand Up @@ -65,7 +117,6 @@ def test_getPatients(nbia_patients):
assert isinstance(nbia_patients[0], str)
assert len(nbia_patients[0]) > 0


def test_getSeries(nbia_client, nbia_collections, nbia_patients):
seriesList = nbia_client.getSeries(
Collection=nbia_collections[0],
Expand All @@ -77,7 +128,6 @@ def test_getSeries(nbia_client, nbia_collections, nbia_patients):
assert len(seriesList) > 0
assert isinstance(seriesList[0], dict)


def test_fail_getSeries(nbia_client, nbia_collections, nbia_patients):
with pytest.raises(Exception):
seriesList = nbia_client.getSeries(
Expand All @@ -91,3 +141,6 @@ def test_fail_getSeries(nbia_client, nbia_collections, nbia_patients):
assert len(seriesList) > 0
assert isinstance(seriesList[0], dict)



# nbia_client.downloadSeries(

0 comments on commit 0f502ce

Please sign in to comment.