Skip to content

Commit

Permalink
Use pandas.testing.assert_frame_equal and skimage.metrics.structural_…
Browse files Browse the repository at this point in the history
…similarity

for comparison when testing
  • Loading branch information
jochenklar committed Nov 7, 2023
1 parent 0f79123 commit b335881
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
36 changes: 0 additions & 36 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import hashlib
import shutil
from pathlib import Path

import pytest

from isimip_qa.main import init_settings


def pytest_addoption(parser):
parser.addoption('--init-files', dest='init_files', action='store_true', default=False)
Expand All @@ -17,33 +11,3 @@ class Config:
init_files = pytestconfig.getoption('init_files')

return Config


@pytest.fixture(scope='session')
def settings(config):
if config.init_files:
extractions_path = Path('testing') / 'extractions'
plots_path = Path('testing') / 'plots'
else:
extractions_path = plots_path = Path('testing') / 'tmp'

shutil.rmtree(extractions_path, ignore_errors=True)
shutil.rmtree(plots_path, ignore_errors=True)

return init_settings(
datasets_path=Path('testing') / 'datasets',
extractions_path=extractions_path,
plots_path=plots_path,
plots_format='png'
)


@pytest.fixture()
def checksum():
def sha1_checksum(file_path):
h = hashlib.sha1()
with open(file_path, 'rb') as fp:
h.update(fp.read())
return h.hexdigest()

return sha1_checksum
30 changes: 25 additions & 5 deletions isimip_qa/tests/test_extractions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import shutil
from pathlib import Path

import pandas as pd
import pytest

from isimip_qa.extractions import extraction_classes
from isimip_qa.main import init_settings
from isimip_qa.models import Dataset

datasets = [
Expand All @@ -12,9 +15,20 @@
'sine'
]

@pytest.fixture(scope='module')
def settings(config):
extractions_path = Path('testing') / ('extractions' if config.init_files else 'tmp')

shutil.rmtree(extractions_path, ignore_errors=True)

return init_settings(
datasets_path=Path('testing') / 'datasets',
extractions_path=extractions_path
)

@pytest.mark.parametrize('dataset_path', datasets)
@pytest.mark.parametrize('extraction_class', extraction_classes)
def test_extraction(config, settings, checksum, dataset_path, extraction_class):
def test_extraction(settings, dataset_path, extraction_class):
dataset = Dataset(dataset_path)
extraction = extraction_class(dataset)
extraction.path.unlink(missing_ok=True)
Expand All @@ -25,9 +39,15 @@ def test_extraction(config, settings, checksum, dataset_path, extraction_class):
file.close()

assert extraction.path.exists()
assert extraction.path.suffix in ['.csv', '.json']

template_path = Path('testing').joinpath('extractions') \
.joinpath(extraction.path.relative_to(settings.EXTRACTIONS_PATH))

if not config.init_files:
assert checksum(extraction.path) == checksum(
Path('testing').joinpath('extractions')
.joinpath(extraction.path.relative_to(settings.EXTRACTIONS_PATH))
if extraction.path.suffix == '.csv':
pd.testing.assert_frame_equal(
pd.read_csv(extraction.path),
pd.read_csv(template_path)
)
else:
assert extraction.path.read_text() == template_path.read_text()
34 changes: 28 additions & 6 deletions isimip_qa/tests/test_plots.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import shutil
from pathlib import Path

import pytest
from skimage.io import imread
from skimage.metrics import structural_similarity as ssim

from isimip_qa.extractions import extraction_classes
from isimip_qa.main import init_settings
from isimip_qa.models import Dataset
from isimip_qa.plots import plot_classes

Expand All @@ -13,10 +17,24 @@
'sine'
]

@pytest.fixture(scope='module')
def settings(config):
extractions_path = Path('testing') / 'extractions'
plots_path = Path('testing') / ('plots' if config.init_files else 'tmp')

shutil.rmtree(plots_path, ignore_errors=True)

return init_settings(
datasets_path=Path('testing') / 'datasets',
extractions_path=extractions_path,
plots_path=plots_path,
plots_format='png'
)

@pytest.mark.parametrize('dataset_path', datasets)
@pytest.mark.parametrize('extraction_class', extraction_classes)
@pytest.mark.parametrize('plot_class', plot_classes)
def test_mean_extraction(config, settings, checksum, dataset_path, extraction_class, plot_class):
def test_plot(settings, dataset_path, extraction_class, plot_class):
dataset = Dataset(dataset_path)
if plot_class.has_extraction(extraction_class):
plot = plot_class(extraction_class, [dataset], path=dataset.path.stem)
Expand All @@ -25,8 +43,12 @@ def test_mean_extraction(config, settings, checksum, dataset_path, extraction_cl
for sp in plot.get_subplots():
assert sp.path.exists()

if not config.init_files:
assert checksum(sp.path) == checksum(
Path('testing').joinpath('plots')
.joinpath(sp.path.relative_to(settings.PLOTS_PATH))
)
img = imread(sp.path, as_gray=True)

template_path = Path('testing').joinpath('plots') \
.joinpath(sp.path.relative_to(settings.PLOTS_PATH))
template_img = imread(template_path, as_gray=True)

similarity = ssim(img, template_img, data_range=template_img.max() - template_img.min())

assert similarity == 1.0
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dev = [
"pytest",
"pytest-cov",
"ruff",
"scikit-image",
"twine"
]

Expand Down

0 comments on commit b335881

Please sign in to comment.