From 2b3bfc5a4aedbd91079a268b1ae3b77feb93b707 Mon Sep 17 00:00:00 2001 From: Oleg Sivokon Date: Sun, 22 Aug 2021 16:21:21 +0300 Subject: [PATCH] Add test for image extraction, closes #40 --- cleanX/cli/__init__.py | 4 +- cleanX/cli/dataset.py | 2 +- cleanX/cli/dicom.py | 7 ++-- cleanX/dicom_processing/pydicom_adapter.py | 2 +- cleanX/dicom_processing/simpleitk_adapter.py | 2 +- setup.py | 12 +++--- test/test_cli.py | 41 ++++++++++++++++++-- 7 files changed, 53 insertions(+), 17 deletions(-) diff --git a/cleanX/cli/__init__.py b/cleanX/cli/__init__.py index b8ec5c3..20dbde1 100644 --- a/cleanX/cli/__init__.py +++ b/cleanX/cli/__init__.py @@ -6,8 +6,8 @@ """ from .main import main -from .dicom import dicom, extract_images +from .dicom import dicom, extract, report from .images import images, run_pipeline, restore_pipeline -from .dataset import dataset, generate_report +from .dataset import dataset, report diff --git a/cleanX/cli/dataset.py b/cleanX/cli/dataset.py index 2f77110..05b0f83 100644 --- a/cleanX/cli/dataset.py +++ b/cleanX/cli/dataset.py @@ -112,7 +112,7 @@ def dataset(cfg): * txt ''', ) -def generate_report( +def report( cfg, train_source, test_source, diff --git a/cleanX/cli/dicom.py b/cleanX/cli/dicom.py index 057309a..81ec09a 100644 --- a/cleanX/cli/dicom.py +++ b/cleanX/cli/dicom.py @@ -68,7 +68,7 @@ def dicom(cfg): These will depend on the chosen reader. ''' ) -def generate_report(cfg, input, output, config_reader): +def report(cfg, input, output, config_reader): reader = create_reader(cfg, config_reader) df = reader.read(parse_sources(input, cfg)) df.to_csv(os.path.join(output, 'report.csv')) @@ -119,10 +119,9 @@ def generate_report(cfg, input, output, config_reader): These will depend on the chosen reader. ''' ) -def extract_images(cfg, input, output, config_reader): +def extract(cfg, input, output, config_reader): reader = create_reader(cfg, config_reader) - df = reader.read(parse_sources(input, cfg)) - df.to_csv(os.path.join(output, 'report.csv')) + reader.rip_out_jpgs(parse_sources(input, cfg), output) def create_reader(cfg, config_reader): diff --git a/cleanX/dicom_processing/pydicom_adapter.py b/cleanX/dicom_processing/pydicom_adapter.py index b092f52..6f803db 100644 --- a/cleanX/dicom_processing/pydicom_adapter.py +++ b/cleanX/dicom_processing/pydicom_adapter.py @@ -122,7 +122,7 @@ def rip_out_jpgs(self, source, destination): :type source: :class:`~cleanX.dicom_processing.Source` :param destination: The name of the directory where JPG files should be stored. - :type destination: Compatible with :fn:`os.path.join` + :type destination: Compatible with :func:`os.path.join` """ for key, parsed in source.items(dicom.dcmread): cv2.imwrite( diff --git a/cleanX/dicom_processing/simpleitk_adapter.py b/cleanX/dicom_processing/simpleitk_adapter.py index 514c150..eeea035 100644 --- a/cleanX/dicom_processing/simpleitk_adapter.py +++ b/cleanX/dicom_processing/simpleitk_adapter.py @@ -282,7 +282,7 @@ def rip_out_jpgs(self, source, destination): :type source: :class:`~cleanX.dicom_processing.Source` :param destination: The name of the directory where JPG files should be stored. - :type destination: Compatible with :fn:`os.path.join` + :type destination: Compatible with :func:`os.path.join` """ reader = sitk.ImageFileReader() m_reader = MetadataHelper(reader) diff --git a/setup.py b/setup.py index 7367522..72ea41a 100755 --- a/setup.py +++ b/setup.py @@ -128,13 +128,15 @@ def run(self): from sphinx.ext.apidoc import main src = os.path.join(project_dir, 'docs') + special = 'index.rst', 'cli.rst' for f in glob(os.path.join(src, '*.rst')): - if f.endswith('index.rst'): - continue - if f.endswith('cli.rst'): - continue - os.unlink(f) + for end in special: + if f.endswith(end): + os.utime(f, None) + break + else: + os.unlink(f) sys.exit(main([ '-o', src, diff --git a/test/test_cli.py b/test/test_cli.py index ec41741..fb90f3d 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -5,6 +5,7 @@ import subprocess from tempfile import TemporaryDirectory +from glob import glob import pandas as pd import pytest @@ -18,7 +19,7 @@ @skip_if_missing('no pydicom available', 'pydicom', 'click') -def test_cli_pydicom(): +def test_cli_pydicom_report(): dicomfile_directory1 = os.path.join( os.path.dirname(__file__), 'dicom_example_folder', @@ -27,7 +28,7 @@ def test_cli_pydicom(): result = subprocess.call( [ sys.executable, '-m', 'cleanX', - 'dicom', 'extract-images', + 'dicom', 'report', '-i', 'dir', dicomfile_directory1, '-o', td, ] @@ -38,6 +39,40 @@ def test_cli_pydicom(): assert len(os.listdir(dicomfile_directory1)) == len(df) +@skip_if_missing('no pydicom available', 'pydicom', 'click') +def test_cli_pydicom_extract(): + dicomfile_directory1 = os.path.join( + os.path.dirname(__file__), + 'dicom_example_folder', + ) + with TemporaryDirectory() as td: + result = subprocess.call( + [ + sys.executable, '-m', 'cleanX', + 'dicom', 'extract', + '-i', 'dir', dicomfile_directory1, + '-o', td, + ] + ) + assert not result + + jpegs = sorted(glob(os.path.join(td, '*.jpg'))) + dicoms = sorted(os.listdir(dicomfile_directory1)) + assert len(jpegs) == len(dicoms) + + mismatches = [] + for jpg, dcm in zip(jpegs, dicoms): + jpg = os.path.basename(jpg) + jpg = os.path.splitext(jpg)[0] + + dcm = os.path.basename(dcm) + dcm = os.path.splitext(dcm)[0] + + if jpg != dcm: + mismatches.append((jpg, dcm)) + assert not mismatches + + @skip_if_missing('no pydicom available', 'pydicom', 'click') def test_cli_datasets(): resources = os.path.dirname(__file__) @@ -45,7 +80,7 @@ def test_cli_datasets(): result = subprocess.check_output( [ sys.executable, '-m', 'cleanX', - 'dataset', 'generate-report', + 'dataset', 'report', '-r', os.path.join(resources, 'test_sample_df.csv'), '-t', os.path.join(resources, 'train_sample_df.csv'), # These two sets don't appear to have common columns