Skip to content

Commit

Permalink
Add test for image extraction, closes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
wvxvw committed Aug 22, 2021
1 parent 92d7381 commit 2b3bfc5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cleanX/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


2 changes: 1 addition & 1 deletion cleanX/cli/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def dataset(cfg):
* txt
''',
)
def generate_report(
def report(
cfg,
train_source,
test_source,
Expand Down
7 changes: 3 additions & 4 deletions cleanX/cli/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion cleanX/dicom_processing/pydicom_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion cleanX/dicom_processing/simpleitk_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 38 additions & 3 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess

from tempfile import TemporaryDirectory
from glob import glob

import pandas as pd
import pytest
Expand All @@ -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',
Expand All @@ -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,
]
Expand All @@ -38,14 +39,48 @@ 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__)
with TemporaryDirectory() as td:
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
Expand Down

0 comments on commit 2b3bfc5

Please sign in to comment.