diff --git a/bob/ip/binseg/data/utils.py b/bob/ip/binseg/data/utils.py index 6bdd727b7c63bdff0bf99383ca4d394d2ba87876..5302eb99e5dd3b099473602a983b7ff185711bf6 100644 --- a/bob/ip/binseg/data/utils.py +++ b/bob/ip/binseg/data/utils.py @@ -126,16 +126,6 @@ class SampleListDataset(torch.utils.data.Dataset): sample. - Attributes - ---------- - - transforms : list - An accessor to the list of transforms to be applied (excluding the last - transform, which is fixed). Notice that, after setting, a last transform - (:py:class:`bob.ip.binseg.data.transforms.ToTensor`) is always applied - - you do not need to add that. - - Parameters ---------- diff --git a/bob/ip/binseg/test/__init__.py b/bob/ip/binseg/test/__init__.py index e4b13525b1e78beb2b5b4e9a141b3bb5fa2f4f8c..76525619db72eac38741933aa0d711a38d6fe54f 100644 --- a/bob/ip/binseg/test/__init__.py +++ b/bob/ip/binseg/test/__init__.py @@ -22,22 +22,6 @@ def teardown_package(): TESTDB_TMPDIR.cleanup() -def _mock_test_skipper(name): - """ - Dummary decorator that does nothing - """ - import functools - - def wrapped_function(test): - @functools.wraps(test) - def wrapper(*args, **kwargs): - return test(*args, **kwargs) - - return wrapper - - return wrapped_function - - def mock_dataset(): global TESTDB_TMPDIR from bob.extension import rc @@ -64,13 +48,10 @@ def mock_dataset(): if TESTDB_TMPDIR is None: # if the user has the STARE directory ready, then we do a normal return - from .utils import rc_variable_set - - return rc["bob.ip.binseg.stare.datadir"], stare.dataset, rc_variable_set + return rc["bob.ip.binseg.stare.datadir"], stare.dataset # else, we do a "mock" return return ( TESTDB_TMPDIR.name, stare._make_dataset(TESTDB_TMPDIR.name), - _mock_test_skipper, ) diff --git a/bob/ip/binseg/test/conftest.py b/bob/ip/binseg/test/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..f2e3953211bc121d8ac7b055c51c7538478c1d87 --- /dev/null +++ b/bob/ip/binseg/test/conftest.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# coding=utf-8 + +import pytest +import bob.extension + + +def pytest_configure(config): + """This function is run once for pytest setup""" + + config.addinivalue_line( + "markers", + "skip_if_rc_var_not_set(name): this mark skips the test if a certain " + "~/.bobrc variable is not set", + ) + + + config.addinivalue_line("markers", "slow: this mark indicates slow tests") + + +def pytest_runtest_setup(item): + """This function is run for every test candidate in this directory + + The test is run if this function returns ``None``. To skip a test, call + ``pytest.skip()``, specifying a reason. + """ + + # iterates over all markers for the item being examined, get the first + # argument and accumulate these names + rc_names = [ + mark.args[0] + for mark in item.iter_markers(name="skip_if_rc_var_not_set") + ] + + # checks all names mentioned are set in ~/.bobrc, otherwise, skip the test + if rc_names: + missing = [k for k in rc_names if (k not in bob.extension.rc)] + if any(missing): + pytest.skip(f"Test skipped because {', '.join(missing)} are **not** " + f"set in ~/.bobrc") + + +def rc_variable_set(name): + pytest.mark.skipif( + name not in bob.extension.rc, + reason=f"Bob's RC variable '{name}' is not set", + ) diff --git a/bob/ip/binseg/test/test_chasedb1.py b/bob/ip/binseg/test/test_chasedb1.py index ae1294509a2f5134f18abde162ff477a914a6f5f..3916a859e184712e0beea04d08ef21ced733f1e9 100644 --- a/bob/ip/binseg/test/test_chasedb1.py +++ b/bob/ip/binseg/test/test_chasedb1.py @@ -5,44 +5,43 @@ """Tests for CHASE-DB1""" import os - import numpy -import nose.tools +import pytest from ..data.chasedb1 import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): subset = dataset.subsets("first-annotator") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 8) + assert len(subset["train"]) == 8 for s in subset["train"]: assert s.key.startswith("Image_") assert "test" in subset - nose.tools.eq_(len(subset["test"]), 20) + assert len(subset["test"]) == 20 for s in subset["test"]: assert s.key.startswith("Image_") subset = dataset.subsets("second-annotator") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 8) + assert len(subset["train"]) == 8 for s in subset["train"]: assert s.key.startswith("Image_") assert "test" in subset - nose.tools.eq_(len(subset["test"]), 20) + assert len(subset["test"]) == 20 for s in subset["test"]: assert s.key.startswith("Image_") -@rc_variable_set('bob.ip.binseg.chasedb1.datadir') +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.chasedb1.datadir') def test_loading(): image_size = (999, 960) @@ -51,15 +50,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 3) + assert len(data) == 3 assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b+w) == numpy.prod(image_size), \ f"Counts of black + white ({b}+{w}) do not add up to total " \ @@ -72,8 +71,8 @@ def test_loading(): assert "mask" in data - nose.tools.eq_(data["mask"].size, image_size) - nose.tools.eq_(data["mask"].mode, "1") + assert data["mask"].size == image_size + assert data["mask"].mode == "1" bm, wm = count_bw(data["mask"]) assert (bm+wm) == numpy.prod(image_size), \ f"Counts of black + white ({bm}+{wm}) do not add up to total " \ @@ -109,6 +108,6 @@ def test_loading(): #print(f"max label proportions = {max(proportions)}") -@rc_variable_set('bob.ip.binseg.chasedb1.datadir') +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.chasedb1.datadir') def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_checkpointer.py b/bob/ip/binseg/test/test_checkpointer.py index 16df40afa8b99832b64d5c58d2013da65408fb5b..545612a6bd7532fcb627b2e7b23a29084de64bed 100644 --- a/bob/ip/binseg/test/test_checkpointer.py +++ b/bob/ip/binseg/test/test_checkpointer.py @@ -7,23 +7,24 @@ from collections import OrderedDict from tempfile import TemporaryDirectory import torch -import nose.tools -from torch import nn +import pytest from ..utils.checkpointer import Checkpointer class TestCheckpointer(unittest.TestCase): def create_model(self): - return nn.Sequential(nn.Linear(2, 3), nn.Linear(3, 1)) + return torch.nn.Sequential( + torch.nn.Linear(2, 3), torch.nn.Linear(3, 1) + ) def create_complex_model(self): - m = nn.Module() - m.block1 = nn.Module() - m.block1.layer1 = nn.Linear(2, 3) - m.layer2 = nn.Linear(3, 2) - m.res = nn.Module() - m.res.layer2 = nn.Linear(3, 2) + m = torch.nn.Module() + m.block1 = torch.nn.Module() + m.block1.layer1 = torch.nn.Linear(2, 3) + m.layer2 = torch.nn.Linear(3, 2) + m.res = torch.nn.Module() + m.res.layer2 = torch.nn.Linear(3, 2) state_dict = OrderedDict() state_dict["layer1.weight"] = torch.rand(3, 2) @@ -46,15 +47,16 @@ class TestCheckpointer(unittest.TestCase): # in the same folder fresh_checkpointer = Checkpointer(fresh_model, path=f) assert fresh_checkpointer.has_checkpoint() - nose.tools.eq_(fresh_checkpointer.last_checkpoint(), - os.path.realpath(os.path.join(f, "checkpoint_file.pth"))) + assert fresh_checkpointer.last_checkpoint() == os.path.realpath( + os.path.join(f, "checkpoint_file.pth") + ) _ = fresh_checkpointer.load() for trained_p, loaded_p in zip( trained_model.parameters(), fresh_model.parameters() ): # different tensor references - nose.tools.assert_not_equal(id(trained_p), id(loaded_p)) + assert id(trained_p) != id(loaded_p) # same content assert trained_p.equal(loaded_p) @@ -70,13 +72,15 @@ class TestCheckpointer(unittest.TestCase): with TemporaryDirectory() as g: fresh_checkpointer = Checkpointer(fresh_model, path=g) assert not fresh_checkpointer.has_checkpoint() - nose.tools.eq_(fresh_checkpointer.last_checkpoint(), None) - _ = fresh_checkpointer.load(os.path.join(f, "checkpoint_file.pth")) + assert fresh_checkpointer.last_checkpoint() == None + _ = fresh_checkpointer.load( + os.path.join(f, "checkpoint_file.pth") + ) for trained_p, loaded_p in zip( trained_model.parameters(), fresh_model.parameters() ): # different tensor references - nose.tools.assert_not_equal(id(trained_p), id(loaded_p)) + assert id(trained_p) != id(loaded_p) # same content assert trained_p.equal(loaded_p) diff --git a/bob/ip/binseg/test/test_cli.py b/bob/ip/binseg/test/test_cli.py index a650ea7612b3646a3783b28cd6349d987ab6049e..09a4de999bfa8da2dfbb681bd7dad90074753778 100644 --- a/bob/ip/binseg/test/test_cli.py +++ b/bob/ip/binseg/test/test_cli.py @@ -6,34 +6,14 @@ import os import re import fnmatch +import logging import tempfile -import contextlib - -import nose.tools from click.testing import CliRunner from . import mock_dataset -stare_datadir, stare_dataset, rc_variable_set = mock_dataset() - - -@contextlib.contextmanager -def stdout_logging(): - - ## copy logging messages to std out - import sys - import logging - import io - - buf = io.StringIO() - ch = logging.StreamHandler(buf) - ch.setFormatter(logging.Formatter("%(message)s")) - ch.setLevel(logging.INFO) - logger = logging.getLogger("bob") - logger.addHandler(ch) - yield buf - logger.removeHandler(ch) +stare_datadir, stare_dataset = mock_dataset() def _assert_exit_0(result): @@ -67,14 +47,17 @@ def _str_counter(substr, s): return sum(1 for _ in re.finditer(substr, s, re.MULTILINE)) -def _check_experiment_stare(overlay): +def _check_experiment_stare(caplog, overlay): from ..script.experiment import experiment + # ensures we capture only ERROR messages and above by default + caplog.set_level(logging.ERROR) + runner = CliRunner() - with runner.isolated_filesystem(), stdout_logging() as buf, tempfile.NamedTemporaryFile( - mode="wt" - ) as config: + with runner.isolated_filesystem(), caplog.at_level( + logging.INFO, logger="bob.ip.binseg" + ), tempfile.NamedTemporaryFile(mode="wt") as config: # re-write STARE dataset configuration for test config.write("from bob.ip.binseg.data.stare import _make_dataset\n") @@ -119,10 +102,10 @@ def _check_experiment_stare(overlay): predict_folder = os.path.join(output_folder, "predictions") traindir = os.path.join(predict_folder, "train", "stare-images") assert os.path.exists(traindir) - nose.tools.eq_(len(fnmatch.filter(os.listdir(traindir), "*.hdf5")), 10) + assert len(fnmatch.filter(os.listdir(traindir), "*.hdf5")) == 10 testdir = os.path.join(predict_folder, "test", "stare-images") assert os.path.exists(testdir) - nose.tools.eq_(len(fnmatch.filter(os.listdir(testdir), "*.hdf5")), 10) + assert len(fnmatch.filter(os.listdir(testdir), "*.hdf5")) == 10 overlay_folder = os.path.join(output_folder, "overlayed", "predictions") traindir = os.path.join(overlay_folder, "train", "stare-images") @@ -130,14 +113,10 @@ def _check_experiment_stare(overlay): if overlay: # check overlayed images are there (since we requested them) assert os.path.exists(traindir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(traindir), "*.png")), 10 - ) + assert len(fnmatch.filter(os.listdir(traindir), "*.png")) == 10 # check overlayed images are there (since we requested them) assert os.path.exists(testdir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(testdir), "*.png")), 10 - ) + assert len(fnmatch.filter(os.listdir(testdir), "*.png")) == 10 else: assert not os.path.exists(traindir) assert not os.path.exists(testdir) @@ -148,38 +127,32 @@ def _check_experiment_stare(overlay): # checks individual performance figures are there traindir = os.path.join(eval_folder, "train", "stare-images") assert os.path.exists(traindir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(traindir), "*.csv")), 10 - ) + assert len(fnmatch.filter(os.listdir(traindir), "*.csv")) == 10 assert os.path.exists(os.path.join(eval_folder, "test.csv")) # checks individual performance figures are there testdir = os.path.join(eval_folder, "test", "stare-images") assert os.path.exists(testdir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(testdir), "*.csv")), 10 - ) + assert len(fnmatch.filter(os.listdir(testdir), "*.csv")) == 10 assert os.path.exists( os.path.join(eval_folder, "second-annotator", "train.csv") ) # checks individual performance figures are there - traindir_sa = os.path.join(eval_folder, "second-annotator", "train", - "stare-images") - assert os.path.exists(traindir_sa) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(traindir_sa), "*.csv")), 10 + traindir_sa = os.path.join( + eval_folder, "second-annotator", "train", "stare-images" ) + assert os.path.exists(traindir_sa) + assert len(fnmatch.filter(os.listdir(traindir_sa), "*.csv")) == 10 assert os.path.exists( os.path.join(eval_folder, "second-annotator", "test.csv") ) - testdir_sa = os.path.join(eval_folder, "second-annotator", "test", - "stare-images") - assert os.path.exists(testdir_sa) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(testdir_sa), "*.csv")), 10 + testdir_sa = os.path.join( + eval_folder, "second-annotator", "test", "stare-images" ) + assert os.path.exists(testdir_sa) + assert len(fnmatch.filter(os.listdir(testdir_sa), "*.csv")) == 10 overlay_folder = os.path.join(output_folder, "overlayed", "analysis") traindir = os.path.join(overlay_folder, "train", "stare-images") @@ -187,13 +160,9 @@ def _check_experiment_stare(overlay): if overlay: # check overlayed images are there (since we requested them) assert os.path.exists(traindir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(traindir), "*.png")), 10 - ) + assert len(fnmatch.filter(os.listdir(traindir), "*.png")) == 10 assert os.path.exists(testdir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(testdir), "*.png")), 10 - ) + assert len(fnmatch.filter(os.listdir(testdir), "*.png")) == 10 else: assert not os.path.exists(traindir) assert not os.path.exists(testdir) @@ -207,13 +176,9 @@ def _check_experiment_stare(overlay): testdir = os.path.join(overlay_folder, "test", "stare-images") if overlay: assert os.path.exists(traindir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(traindir), "*.png")), 10 - ) + assert len(fnmatch.filter(os.listdir(traindir), "*.png")) == 10 assert os.path.exists(testdir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(testdir), "*.png")), 10 - ) + assert len(fnmatch.filter(os.listdir(testdir), "*.png")) == 10 else: assert not os.path.exists(traindir) assert not os.path.exists(testdir) @@ -248,37 +213,26 @@ def _check_experiment_stare(overlay): r"^Saving table at": 1, r"^Ended comparison.*$": 1, } - buf.seek(0) - logging_output = buf.read() + messages = "\n".join([k.getMessage() for k in caplog.records]) for k, v in keywords.items(): - # if _str_counter(k, logging_output) != v: - # print(f"Count for string '{k}' appeared " \ - # f"({_str_counter(k, result.output)}) " \ - # f"instead of the expected {v}") - assert _str_counter(k, logging_output) == v, ( - f"Count for string '{k}' appeared " - f"({_str_counter(k, logging_output)}) " - f"instead of the expected {v}" - ) + assert _str_counter(k, messages) == v -@rc_variable_set("bob.ip.binseg.stare.datadir") -def test_experiment_stare_with_overlay(): - _check_experiment_stare(overlay=True) +def test_experiment_stare_with_overlay(caplog): + _check_experiment_stare(caplog, overlay=True) -@rc_variable_set("bob.ip.binseg.stare.datadir") -def test_experiment_stare_without_overlay(): - _check_experiment_stare(overlay=False) +def test_experiment_stare_without_overlay(caplog): + _check_experiment_stare(caplog, overlay=False) -def _check_train(runner): +def _check_train(caplog, runner): from ..script.train import train - with tempfile.NamedTemporaryFile( - mode="wt" - ) as config, stdout_logging() as buf: + with tempfile.NamedTemporaryFile(mode="wt") as config, caplog.at_level( + logging.INFO, logger="bob.ip.binseg" + ): # single training set configuration config.write("from bob.ip.binseg.data.stare import _make_dataset\n") @@ -322,28 +276,19 @@ def _check_train(runner): r"^Saving checkpoint to .*/model_final.pth$": 1, r"^Total training time:": 1, } - buf.seek(0) - logging_output = buf.read() + messages = "\n".join([k.getMessage() for k in caplog.records]) for k, v in keywords.items(): - # if _str_counter(k, logging_output) != v: - # print(f"Count for string '{k}' appeared " \ - # f"({_str_counter(k, result.output)}) " \ - # f"instead of the expected {v}") - assert _str_counter(k, logging_output) == v, ( - f"Count for string '{k}' appeared " - f"({_str_counter(k, logging_output)}) " - f"instead of the expected {v}:\nOutput:\n{logging_output}" - ) + assert _str_counter(k, messages) == v -def _check_predict(runner): +def _check_predict(caplog, runner): from ..script.predict import predict - with tempfile.NamedTemporaryFile( - mode="wt" - ) as config, stdout_logging() as buf: + with tempfile.NamedTemporaryFile(mode="wt") as config, caplog.at_level( + logging.INFO, logger="bob.ip.binseg" + ): # single training set configuration config.write("from bob.ip.binseg.data.stare import _make_dataset\n") @@ -373,39 +318,30 @@ def _check_predict(runner): # check predictions are there basedir = os.path.join(output_folder, "test", "stare-images") assert os.path.exists(basedir) - nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.hdf5")), 10) + assert len(fnmatch.filter(os.listdir(basedir), "*.hdf5")) == 10 # check overlayed images are there (since we requested them) basedir = os.path.join(overlay_folder, "test", "stare-images") assert os.path.exists(basedir) - nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 10) + assert len(fnmatch.filter(os.listdir(basedir), "*.png")) == 10 keywords = { r"^Loading checkpoint from.*$": 1, r"^Total time:.*$": 1, } - buf.seek(0) - logging_output = buf.read() + messages = "\n".join([k.getMessage() for k in caplog.records]) for k, v in keywords.items(): - # if _str_counter(k, logging_output) != v: - # print(f"Count for string '{k}' appeared " \ - # f"({_str_counter(k, result.output)}) " \ - # f"instead of the expected {v}") - assert _str_counter(k, logging_output) == v, ( - f"Count for string '{k}' appeared " - f"({_str_counter(k, logging_output)}) " - f"instead of the expected {v}:\nOutput:\n{logging_output}" - ) + assert _str_counter(k, messages) == v -def _check_evaluate(runner): +def _check_evaluate(caplog, runner): from ..script.evaluate import evaluate - with tempfile.NamedTemporaryFile( - mode="wt" - ) as config, stdout_logging() as buf: + with tempfile.NamedTemporaryFile(mode="wt") as config, caplog.at_level( + logging.INFO, logger="bob.ip.binseg" + ): # single training set configuration config.write("from bob.ip.binseg.data.stare import _make_dataset\n") @@ -436,53 +372,39 @@ def _check_evaluate(runner): # checks individual performance figures are there testdir = os.path.join(output_folder, "test", "stare-images") assert os.path.exists(testdir) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(testdir), "*.csv")), 10 - ) + assert len(fnmatch.filter(os.listdir(testdir), "*.csv")) == 10 assert os.path.exists( os.path.join(output_folder, "second-annotator", "test.csv") ) # checks individual performance figures are there - testdir_sa = os.path.join(output_folder, "second-annotator", "test", - "stare-images") - assert os.path.exists(testdir_sa) - nose.tools.eq_( - len(fnmatch.filter(os.listdir(testdir_sa), "*.csv")), 10 + testdir_sa = os.path.join( + output_folder, "second-annotator", "test", "stare-images" ) + assert os.path.exists(testdir_sa) + assert len(fnmatch.filter(os.listdir(testdir_sa), "*.csv")) == 10 # check overlayed images are there (since we requested them) basedir = os.path.join(overlay_folder, "test", "stare-images") assert os.path.exists(basedir) - nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 10) + assert len(fnmatch.filter(os.listdir(basedir), "*.png")) == 10 keywords = { - r"^Skipping dataset '__train__'": 0, - r"^Saving summaries over all input images.*$": 1, r"^Maximum F1-score of.*\(chosen \*a posteriori\*\)$": 1, r"^F1-score of.*\(chosen \*a priori\*\)$": 1, r"^F1-score of.*\(second annotator; threshold=0.5\)$": 1, } - buf.seek(0) - logging_output = buf.read() + messages = "\n".join([k.getMessage() for k in caplog.records]) for k, v in keywords.items(): - # if _str_counter(k, logging_output) != v: - # print(f"Count for string '{k}' appeared " \ - # f"({_str_counter(k, result.output)}) " \ - # f"instead of the expected {v}") - assert _str_counter(k, logging_output) == v, ( - f"Count for string '{k}' appeared " - f"({_str_counter(k, logging_output)}) " - f"instead of the expected {v}:\nOutput:\n{logging_output}" - ) + assert _str_counter(k, messages) == v -def _check_compare(runner): +def _check_compare(caplog, runner): from ..script.compare import compare - with stdout_logging() as buf: + with caplog.at_level(logging.INFO, logger="bob.ip.binseg"): output_folder = "evaluations" result = runner.invoke( @@ -509,28 +431,18 @@ def _check_compare(runner): r"^Tabulating performance summary...": 1, r"^Saving table at": 1, } - buf.seek(0) - logging_output = buf.read() - + messages = "\n".join([k.getMessage() for k in caplog.records]) for k, v in keywords.items(): - # if _str_counter(k, logging_output) != v: - # print(f"Count for string '{k}' appeared " \ - # f"({_str_counter(k, result.output)}) " \ - # f"instead of the expected {v}") - assert _str_counter(k, logging_output) == v, ( - f"Count for string '{k}' appeared " - f"({_str_counter(k, logging_output)}) " - f"instead of the expected {v}:\nOutput:\n{logging_output}" - ) + assert _str_counter(k, messages) == v -def _check_significance(runner): +def _check_significance(caplog, runner): from ..script.significance import significance - with tempfile.NamedTemporaryFile( - mode="wt" - ) as config, stdout_logging() as buf: + with tempfile.NamedTemporaryFile(mode="wt") as config, caplog.at_level( + logging.INFO, logger="bob.ip.binseg" + ): config.write("from bob.ip.binseg.data.stare import _make_dataset\n") config.write(f"_raw = _make_dataset('{stare_datadir}')\n") @@ -548,11 +460,15 @@ def _check_significance(runner): [ "-vv", config.name, - "--names=v1", "v2", - "--predictions=predictions", "predictions", + "--names=v1", + "v2", + "--predictions=predictions", + "predictions", "--threshold=0.5", - "--size=64", "64", - "--stride=32", "32", + "--size=64", + "64", + "--stride=32", + "32", "--figure=accuracy", f"--output-folder={ofolder}", f"--checkpoint-folder={cfolder}", @@ -568,36 +484,28 @@ def _check_significance(runner): keywords = { r"^Evaluating sliding window 'accuracy' on": 2, r"^Evaluating sliding window 'accuracy' differences on": 1, - #r"^Basic statistics from distributions:$": 1, + # r"^Basic statistics from distributions:$": 1, r"^Writing analysis figures": 1, r"^Writing analysis summary": 1, r"^Differences are exactly zero": 2, } - buf.seek(0) - logging_output = buf.read() - + messages = "\n".join([k.getMessage() for k in caplog.records]) for k, v in keywords.items(): - # if _str_counter(k, logging_output) != v: - # print(f"Count for string '{k}' appeared " \ - # f"({_str_counter(k, result.output)}) " \ - # f"instead of the expected {v}") - assert _str_counter(k, logging_output) == v, ( - f"Count for string '{k}' appeared " - f"({_str_counter(k, logging_output)}) " - f"instead of the expected {v}:\nOutput:\n{logging_output}" - ) + assert _str_counter(k, messages) == v + +def test_discrete_experiment_stare(caplog): -@rc_variable_set("bob.ip.binseg.stare.datadir") -def test_discrete_experiment_stare(): + # ensures we capture only ERROR messages and above by default + caplog.set_level(logging.ERROR) runner = CliRunner() with runner.isolated_filesystem(): - _check_train(runner) - _check_predict(runner) - _check_evaluate(runner) - _check_compare(runner) - #_check_significance(runner) + _check_train(caplog, runner) + _check_predict(caplog, runner) + _check_evaluate(caplog, runner) + _check_compare(caplog, runner) + # _check_significance(caplog, runner) def test_train_help(): diff --git a/bob/ip/binseg/test/test_config.py b/bob/ip/binseg/test/test_config.py index 6f0ddf8992645aa6f16610d9d9bf6e96eb50e962..b55b08ac1b9db51fed72659d9cf0f8251822a450 100644 --- a/bob/ip/binseg/test/test_config.py +++ b/bob/ip/binseg/test/test_config.py @@ -2,14 +2,11 @@ # coding=utf-8 import importlib - -import nose.tools - import torch +import pytest from . import mock_dataset -stare_datadir, stare_dataset, stare_variable_set = mock_dataset() -from .utils import rc_variable_set +stare_datadir, stare_dataset = mock_dataset() # we only iterate over the first N elements at most - dataset loading has # already been checked on the individual datset tests. Here, we are only @@ -17,26 +14,26 @@ from .utils import rc_variable_set N = 10 -@rc_variable_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") def test_drive(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples: - nose.tools.eq_(len(s), 4) + assert len(s) == 4 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 544, 544)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 544, 544)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) - nose.tools.eq_(s[3].shape, (1, 544, 544)) #planes, height, width - nose.tools.eq_(s[3].dtype, torch.float32) + assert s[1].shape, (3, 544 == 544) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 544 == 544) #planes, height, width + assert s[2].dtype == torch.float32 + assert s[3].shape, (1, 544 == 544) #planes, height, width + assert s[3].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 from ..configs.datasets.drive.default import dataset - nose.tools.eq_(len(dataset), 4) + assert len(dataset) == 4 _check_subset(dataset["__train__"], 20) _check_subset(dataset["__valid__"], 20) _check_subset(dataset["train"], 20) @@ -44,102 +41,98 @@ def test_drive(): from ..configs.datasets.drive.second_annotator import dataset - nose.tools.eq_(len(dataset), 1) + assert len(dataset) == 1 _check_subset(dataset["test"], 20) -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_drive_mtest(): from ..configs.datasets.drive.mtest import dataset - nose.tools.eq_(len(dataset), 10) + assert len(dataset) == 10 from ..configs.datasets.drive.default import dataset as baseline - nose.tools.eq_(dataset["train"], baseline["train"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == baseline["train"] + assert dataset["test"] == baseline["test"] for subset in dataset: for sample in dataset[subset]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 544, 544)) #planes, height, width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 544, 544)) - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 544, 544)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 544 == 544) #planes, height, width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 544 == 544) + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 544 == 544) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_drive_covd(): from ..configs.datasets.drive.covd import dataset - nose.tools.eq_(len(dataset), 4) + assert len(dataset) == 4 from ..configs.datasets.drive.default import dataset as baseline - nose.tools.eq_(dataset["train"], dataset["__valid__"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == dataset["__valid__"] + assert dataset["test"] == baseline["test"] for key in ("__train__", "train"): - nose.tools.eq_(len(dataset[key]), 123) + assert len(dataset[key]) == 123 for sample in dataset["__train__"]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 544, 544)) #planes, height, width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 544, 544)) #planes, height, width - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 544, 544)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 544 == 544) #planes, height, width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 544 == 544) #planes, height, width + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 544 == 544) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_drive_ssl(): from ..configs.datasets.drive.ssl import dataset - nose.tools.eq_(len(dataset), 4) + assert len(dataset) == 4 from ..configs.datasets.drive.covd import dataset as covd - nose.tools.eq_(dataset["train"], covd["train"]) - nose.tools.eq_(dataset["train"], dataset["__valid__"]) - nose.tools.eq_(dataset["test"], covd["test"]) - nose.tools.eq_(dataset["__valid__"], covd["__valid__"]) + assert dataset["train"] == covd["train"] + assert dataset["train"] == dataset["__valid__"] + assert dataset["test"] == covd["test"] + assert dataset["__valid__"] == covd["__valid__"] # these are the only different from the baseline - nose.tools.eq_(len(dataset["__train__"]), 123) + assert len(dataset["__train__"]) == 123 for sample in dataset["__train__"]: assert len(sample) == 6 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 544, 544)) #planes, height, width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 544, 544)) #planes, height, width - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 544, 544)) #planes, height, width - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 544 == 544) #planes, height, width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 544 == 544) #planes, height, width + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 544 == 544) #planes, height, width + assert sample[3].dtype == torch.float32 assert isinstance(sample[4], str) - nose.tools.eq_(sample[5].shape, (3, 544, 544)) #planes, height, width - nose.tools.eq_(sample[5].dtype, torch.float32) + assert sample[5].shape, (3, 544 == 544) #planes, height, width + assert sample[5].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@stare_variable_set("bob.ip.binseg.stare.datadir") def test_stare_augmentation_manipulation(): # some tests to check our context management for dataset augmentation works @@ -149,27 +142,26 @@ def test_stare_augmentation_manipulation(): from ..configs.datasets.stare import _maker dataset = _maker("ah", stare_dataset) - nose.tools.eq_(len(dataset["__train__"]._transforms.transforms), - len(dataset["test"]._transforms.transforms) + 4) + assert len(dataset["__train__"]._transforms.transforms) == \ + (len(dataset["test"]._transforms.transforms) + 4) - nose.tools.eq_(len(dataset["train"]._transforms.transforms), - len(dataset["test"]._transforms.transforms)) + assert len(dataset["train"]._transforms.transforms) == \ + len(dataset["test"]._transforms.transforms) -@stare_variable_set("bob.ip.binseg.stare.datadir") def test_stare(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples: - nose.tools.eq_(len(s), 4) + assert len(s) == 4 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 608, 704)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 608, 704)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) - nose.tools.eq_(s[3].shape, (1, 608, 704)) #planes, height, width - nose.tools.eq_(s[3].dtype, torch.float32) + assert s[1].shape, (3, 608 == 704) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 608 == 704) #planes, height, width + assert s[2].dtype == torch.float32 + assert s[3].shape, (1, 608 == 704) #planes, height, width + assert s[3].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 @@ -178,188 +170,184 @@ def test_stare(): for protocol in "ah", "vk": dataset = _maker(protocol, stare_dataset) - nose.tools.eq_(len(dataset), 4) + assert len(dataset) == 4 _check_subset(dataset["__train__"], 10) _check_subset(dataset["train"], 10) _check_subset(dataset["test"], 10) -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_stare_mtest(): from ..configs.datasets.stare.mtest import dataset - nose.tools.eq_(len(dataset), 10) + assert len(dataset) == 10 from ..configs.datasets.stare.ah import dataset as baseline - nose.tools.eq_(dataset["train"], baseline["train"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == baseline["train"] + assert dataset["test"] == baseline["test"] for subset in dataset: for sample in dataset[subset]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 608, 704)) #planes,height,width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 608, 704)) #planes,height,width - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 608, 704)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 608 == 704) #planes,height,width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 608 == 704) #planes,height,width + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 608 == 704) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.drive.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_stare_covd(): from ..configs.datasets.stare.covd import dataset - nose.tools.eq_(len(dataset), 4) + assert len(dataset) == 4 from ..configs.datasets.stare.ah import dataset as baseline - nose.tools.eq_(dataset["train"], dataset["__valid__"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == dataset["__valid__"] + assert dataset["test"] == baseline["test"] # these are the only different sets from the baseline for key in ("__train__", "train"): - nose.tools.eq_(len(dataset[key]), 143) + assert len(dataset[key]) == 143 for sample in dataset[key]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 608, 704)) #planes, height, width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 608, 704)) #planes, height, width - nose.tools.eq_(sample[2].dtype, torch.float32) + assert sample[1].shape, (3, 608 == 704) #planes, height, width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 608 == 704) #planes, height, width + assert sample[2].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 - nose.tools.eq_(sample[3].shape, (1, 608, 704)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[3].shape, (1, 608 == 704) + assert sample[3].dtype == torch.float32 -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") def test_chasedb1(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples: - nose.tools.eq_(len(s), 4) + assert len(s) == 4 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 960, 960)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 960, 960)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) - nose.tools.eq_(s[3].shape, (1, 960, 960)) #planes, height, width - nose.tools.eq_(s[3].dtype, torch.float32) + assert s[1].shape, (3, 960 == 960) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 960 == 960) #planes, height, width + assert s[2].dtype == torch.float32 + assert s[3].shape, (1, 960 == 960) #planes, height, width + assert s[3].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 for m in ("first_annotator", "second_annotator"): d = importlib.import_module(f"...configs.datasets.chasedb1.{m}", package=__name__).dataset - nose.tools.eq_(len(d), 4) + assert len(d) == 4 _check_subset(d["__train__"], 8) _check_subset(d["__valid__"], 8) _check_subset(d["train"], 8) _check_subset(d["test"], 20) -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_chasedb1_mtest(): from ..configs.datasets.chasedb1.mtest import dataset - nose.tools.eq_(len(dataset), 10) + assert len(dataset) == 10 from ..configs.datasets.chasedb1.first_annotator import dataset as baseline - nose.tools.eq_(dataset["train"], baseline["train"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == baseline["train"] + assert dataset["test"] == baseline["test"] for subset in dataset: for sample in dataset[subset]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 960, 960)) #planes,height,width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 960, 960)) #planes,height,width - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 960, 960)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 960 == 960) #planes,height,width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 960 == 960) #planes,height,width + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 960 == 960) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_chasedb1_covd(): from ..configs.datasets.chasedb1.covd import dataset - nose.tools.eq_(len(dataset), 4) + assert len(dataset) == 4 from ..configs.datasets.chasedb1.first_annotator import dataset as baseline - nose.tools.eq_(dataset["train"], dataset["__valid__"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == dataset["__valid__"] + assert dataset["test"] == baseline["test"] # these are the only different sets from the baseline for key in ("__train__", "train"): - nose.tools.eq_(len(dataset[key]), 135) + assert len(dataset[key]) == 135 for sample in dataset[key]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 960, 960)) #planes, height, width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 960, 960)) #planes, height, width - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 960, 960)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 960 == 960) #planes, height, width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 960 == 960) #planes, height, width + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 960 == 960) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") def test_hrf(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples: - nose.tools.eq_(len(s), 4) + assert len(s) == 4 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 1168, 1648)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 1168, 1648)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) - nose.tools.eq_(s[3].shape, (1, 1168, 1648)) #planes, height, width - nose.tools.eq_(s[3].dtype, torch.float32) + assert s[1].shape, (3, 1168 == 1648) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 1168 == 1648) #planes, height, width + assert s[2].dtype == torch.float32 + assert s[3].shape, (1, 1168 == 1648) #planes, height, width + assert s[3].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 def _check_subset_fullres(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples: - nose.tools.eq_(len(s), 4) + assert len(s) == 4 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 2336, 3296)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 2336, 3296)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) - nose.tools.eq_(s[3].shape, (1, 2336, 3296)) #planes, height, width - nose.tools.eq_(s[3].dtype, torch.float32) + assert s[1].shape, (3, 2336 == 3296) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 2336 == 3296) #planes, height, width + assert s[2].dtype == torch.float32 + assert s[3].shape, (1, 2336 == 3296) #planes, height, width + assert s[3].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 from ..configs.datasets.hrf.default import dataset - nose.tools.eq_(len(dataset), 6) + assert len(dataset) == 6 _check_subset(dataset["__train__"], 15) _check_subset(dataset["train"], 15) _check_subset(dataset["test"], 30) @@ -367,248 +355,244 @@ def test_hrf(): _check_subset_fullres(dataset["test (full resolution)"], 30) -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_hrf_mtest(): from ..configs.datasets.hrf.mtest import dataset - nose.tools.eq_(len(dataset), 12) + assert len(dataset) == 12 from ..configs.datasets.hrf.default import dataset as baseline - nose.tools.eq_(dataset["train"], baseline["train"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == baseline["train"] + assert dataset["test"] == baseline["test"] for subset in dataset: for sample in dataset[subset]: assert len(sample) == 4 assert isinstance(sample[0], str) if "full resolution" in subset: - nose.tools.eq_(sample[1].shape, (3, 2336, 3296)) - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 2336, 3296)) - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 2336, 3296)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 2336 == 3296) + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 2336 == 3296) + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 2336 == 3296) + assert sample[3].dtype == torch.float32 else: - nose.tools.eq_(sample[1].shape, (3, 1168, 1648)) - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 1168, 1648)) - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 1168, 1648)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 1168 == 1648) + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 1168 == 1648) + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 1168 == 1648) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_hrf_covd(): from ..configs.datasets.hrf.covd import dataset - nose.tools.eq_(len(dataset), 6) + assert len(dataset) == 6 from ..configs.datasets.hrf.default import dataset as baseline - nose.tools.eq_(dataset["train"], dataset["__valid__"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == dataset["__valid__"] + assert dataset["test"] == baseline["test"] # these are the only different sets from the baseline for key in ("__train__", "train"): - nose.tools.eq_(len(dataset[key]), 118) + assert len(dataset[key]) == 118 for sample in dataset[key]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 1168, 1648)) - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 1168, 1648)) - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 1168, 1648)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 1168 == 1648) + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 1168 == 1648) + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 1168 == 1648) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_iostar(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples: - nose.tools.eq_(len(s), 4) + assert len(s) == 4 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 1024, 1024)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 1024, 1024)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) - nose.tools.eq_(s[3].shape, (1, 1024, 1024)) #planes, height, width - nose.tools.eq_(s[3].dtype, torch.float32) + assert s[1].shape, (3, 1024 == 1024) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 1024 == 1024) #planes, height, width + assert s[2].dtype == torch.float32 + assert s[3].shape, (1, 1024 == 1024) #planes, height, width + assert s[3].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 for m in ("vessel", "optic_disc"): d = importlib.import_module(f"...configs.datasets.iostar.{m}", package=__name__).dataset - nose.tools.eq_(len(d), 4) + assert len(d) == 4 _check_subset(d["__train__"], 20) _check_subset(d["train"], 20) _check_subset(d["test"], 10) -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_iostar_mtest(): from ..configs.datasets.iostar.vessel_mtest import dataset - nose.tools.eq_(len(dataset), 10) + assert len(dataset) == 10 from ..configs.datasets.iostar.vessel import dataset as baseline - nose.tools.eq_(dataset["train"], baseline["train"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == baseline["train"] + assert dataset["test"] == baseline["test"] for subset in dataset: for sample in dataset[subset]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 1024, 1024)) #planes,height,width - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 1024, 1024)) #planes,height,width - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 1024, 1024)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 1024 == 1024) #planes,height,width + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 1024 == 1024) #planes,height,width + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 1024 == 1024) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.drive.datadir") -@stare_variable_set("bob.ip.binseg.stare.datadir") -@rc_variable_set("bob.ip.binseg.chasedb1.datadir") -@rc_variable_set("bob.ip.binseg.hrf.datadir") -@rc_variable_set("bob.ip.binseg.iostar.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drive.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.hrf.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.iostar.datadir") def test_iostar_covd(): from ..configs.datasets.iostar.covd import dataset - nose.tools.eq_(len(dataset), 4) + assert len(dataset) == 4 from ..configs.datasets.iostar.vessel import dataset as baseline - nose.tools.eq_(dataset["train"], dataset["__valid__"]) - nose.tools.eq_(dataset["test"], baseline["test"]) + assert dataset["train"] == dataset["__valid__"] + assert dataset["test"] == baseline["test"] # these are the only different sets from the baseline for key in ("__train__", "train"): - nose.tools.eq_(len(dataset[key]), 133) + assert len(dataset[key]) == 133 for sample in dataset[key]: assert len(sample) == 4 assert isinstance(sample[0], str) - nose.tools.eq_(sample[1].shape, (3, 1024, 1024)) - nose.tools.eq_(sample[1].dtype, torch.float32) - nose.tools.eq_(sample[2].shape, (1, 1024, 1024)) - nose.tools.eq_(sample[2].dtype, torch.float32) - nose.tools.eq_(sample[3].shape, (1, 1024, 1024)) - nose.tools.eq_(sample[3].dtype, torch.float32) + assert sample[1].shape, (3, 1024 == 1024) + assert sample[1].dtype == torch.float32 + assert sample[2].shape, (1, 1024 == 1024) + assert sample[2].dtype == torch.float32 + assert sample[3].shape, (1, 1024 == 1024) + assert sample[3].dtype == torch.float32 assert sample[1].max() <= 1.0 assert sample[1].min() >= 0.0 -@rc_variable_set("bob.ip.binseg.refuge.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.refuge.datadir") def test_refuge(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples[:N]: - nose.tools.eq_(len(s), 3) + assert len(s) == 3 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 1632, 1632)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 1632, 1632)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) + assert s[1].shape, (3, 1632 == 1632) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 1632 == 1632) #planes, height, width + assert s[2].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 for m in ("disc", "cup"): d = importlib.import_module(f"...configs.datasets.refuge.{m}", package=__name__).dataset - nose.tools.eq_(len(d), 5) + assert len(d) == 5 _check_subset(d["__train__"], 400) _check_subset(d["train"], 400) _check_subset(d["validation"], 400) _check_subset(d["test"], 400) -@rc_variable_set("bob.ip.binseg.drishtigs1.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drishtigs1.datadir") def test_drishtigs1(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples[:N]: - nose.tools.eq_(len(s), 3) + assert len(s) == 3 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 1760, 2048)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 1760, 2048)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) + assert s[1].shape, (3, 1760 == 2048) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 1760 == 2048) #planes, height, width + assert s[2].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 for m in ("disc_all", "cup_all", "disc_any", "cup_any"): d = importlib.import_module(f"...configs.datasets.drishtigs1.{m}", package=__name__).dataset - nose.tools.eq_(len(d), 4) + assert len(d) == 4 _check_subset(d["__train__"], 50) _check_subset(d["train"], 50) _check_subset(d["test"], 51) -@rc_variable_set("bob.ip.binseg.rimoner3.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.rimoner3.datadir") def test_rimoner3(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples[:N]: - nose.tools.eq_(len(s), 3) + assert len(s) == 3 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 1440, 1088)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 1440, 1088)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) + assert s[1].shape, (3, 1440 == 1088) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 1440 == 1088) #planes, height, width + assert s[2].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 for m in ("disc_exp1", "cup_exp1", "disc_exp2", "cup_exp2"): d = importlib.import_module(f"...configs.datasets.rimoner3.{m}", package=__name__).dataset - nose.tools.eq_(len(d), 4) + assert len(d) == 4 _check_subset(d["__train__"], 99) _check_subset(d["train"], 99) _check_subset(d["test"], 60) -@rc_variable_set("bob.ip.binseg.drionsdb.datadir") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drionsdb.datadir") def test_drionsdb(): def _check_subset(samples, size): - nose.tools.eq_(len(samples), size) + assert len(samples) == size for s in samples[:N]: - nose.tools.eq_(len(s), 3) + assert len(s) == 3 assert isinstance(s[0], str) - nose.tools.eq_(s[1].shape, (3, 416, 608)) #planes, height, width - nose.tools.eq_(s[1].dtype, torch.float32) - nose.tools.eq_(s[2].shape, (1, 416, 608)) #planes, height, width - nose.tools.eq_(s[2].dtype, torch.float32) + assert s[1].shape, (3, 416 == 608) #planes, height, width + assert s[1].dtype == torch.float32 + assert s[2].shape, (1, 416 == 608) #planes, height, width + assert s[2].dtype == torch.float32 assert s[1].max() <= 1.0 assert s[1].min() >= 0.0 for m in ("expert1", "expert2"): d = importlib.import_module(f"...configs.datasets.drionsdb.{m}", package=__name__).dataset - nose.tools.eq_(len(d), 4) + assert len(d) == 4 _check_subset(d["__train__"], 60) _check_subset(d["train"], 60) _check_subset(d["test"], 50) diff --git a/bob/ip/binseg/test/test_dataset.py b/bob/ip/binseg/test/test_dataset.py index f06d1a4ab2fe6d313af6d65ba5d81529daa6ca71..7483c5834ae2ec275209fad6652883f6d6fd220e 100644 --- a/bob/ip/binseg/test/test_dataset.py +++ b/bob/ip/binseg/test/test_dataset.py @@ -5,7 +5,6 @@ import os import pkg_resources -import nose.tools from ..data.dataset import CSVDataset, JSONDataset from ..data.sample import Sample @@ -49,20 +48,20 @@ def test_csv_loading(): data = dataset.subsets() - nose.tools.eq_(len(data["train"]), 75) + assert len(data["train"]) == 75 for k in data["train"]: for f in range(4): - nose.tools.eq_(type(k.data[f]), float) - nose.tools.eq_(type(k.data[4]), str) - nose.tools.eq_(type(k.key), str) + assert type(k.data[f]) == float + assert type(k.data[4]) == str + assert type(k.key) == str - nose.tools.eq_(len(data["test"]), 75) + assert len(data["test"]) == 75 for k in data["test"]: for f in range(4): - nose.tools.eq_(type(k.data[f]), float) - nose.tools.eq_(type(k.data[4]), str) + assert type(k.data[f]) == float + assert type(k.data[4]) == str assert k.data[4] in ("setosa", "versicolor", "virginica") - nose.tools.eq_(type(k.key), str) + assert type(k.key) == str def test_json_loading(): @@ -83,16 +82,16 @@ def test_json_loading(): data = dataset.subsets("default") - nose.tools.eq_(len(data["train"]), 75) + assert len(data["train"]) == 75 for k in data["train"]: for f in range(4): - nose.tools.eq_(type(k.data[f]), float) - nose.tools.eq_(type(k.data[4]), str) - nose.tools.eq_(type(k.key), str) + assert type(k.data[f]) == float + assert type(k.data[4]) == str + assert type(k.key) == str - nose.tools.eq_(len(data["test"]), 75) + assert len(data["test"]) == 75 for k in data["test"]: for f in range(4): - nose.tools.eq_(type(k.data[f]), float) - nose.tools.eq_(type(k.data[4]), str) - nose.tools.eq_(type(k.key), str) + assert type(k.data[f]) == float + assert type(k.data[4]) == str + assert type(k.key) == str diff --git a/bob/ip/binseg/test/test_drionsdb.py b/bob/ip/binseg/test/test_drionsdb.py index 7508a23df9a5bc41f13a814584a431c2e9cc4a94..8ae2887e26d2f3ffcde0f6ba04e1f8398fe1b076 100644 --- a/bob/ip/binseg/test/test_drionsdb.py +++ b/bob/ip/binseg/test/test_drionsdb.py @@ -5,13 +5,11 @@ """Tests for DRIONS-DB""" import os - import numpy -import nose.tools -from nose.plugins.attrib import attr +import pytest from ..data.drionsdb import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): @@ -19,21 +17,21 @@ def test_protocol_consistency(): for protocol in ("expert1", "expert2"): subset = dataset.subsets(protocol) - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 60) + assert len(subset["train"]) == 60 for s in subset["train"]: assert s.key.startswith(os.path.join("images", "image_0")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 50) + assert len(subset["test"]) == 50 for s in subset["test"]: assert s.key.startswith(os.path.join("images", "image_")) -@rc_variable_set("bob.ip.binseg.drionsdb.datadir") -@attr("slow") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drionsdb.datadir") +@pytest.mark.slow def test_loading(): image_size = (600, 400) @@ -42,15 +40,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 2) + assert len(data) == 2 assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b + w) == numpy.prod(image_size), ( @@ -87,7 +85,8 @@ def test_loading(): proportions = [_check_sample(s, 0.045) for s in subset["test"][:limit]] #print(f"max label proportions = {max(proportions)}") -@rc_variable_set("bob.ip.binseg.drionsdb.datadir") -@attr("slow") + +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drionsdb.datadir") +@pytest.mark.slow def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_drishtigs1.py b/bob/ip/binseg/test/test_drishtigs1.py index 17e39a2ad455b6f04ed5f9d4798d581bc4b668a1..95191c9a239db71589c83360075e38e55e036bd7 100644 --- a/bob/ip/binseg/test/test_drishtigs1.py +++ b/bob/ip/binseg/test/test_drishtigs1.py @@ -5,13 +5,11 @@ """Tests for Drishti-GS1""" import os - import numpy -import nose.tools -from nose.plugins.attrib import attr +import pytest from ..data.drishtigs1 import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): @@ -20,30 +18,30 @@ def test_protocol_consistency(): "optic-cup-any"): subset = dataset.subsets(protocol) - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 50) + assert len(subset["train"]) == 50 for s in subset["train"]: assert s.key.startswith(os.path.join("Drishti-GS1_files", "Training", "Images", "drishtiGS_")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 51) + assert len(subset["test"]) == 51 for s in subset["test"]: assert s.key.startswith(os.path.join("Drishti-GS1_files", "Test", "Images", "drishtiGS_")) -@rc_variable_set("bob.ip.binseg.drishtigs1.datadir") -@attr("slow") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drishtigs1.datadir") +@pytest.mark.slow def test_loading(): def _check_sample(s, bw_threshold_label): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 2) + assert len(data) == 2 assert "data" in data assert data["data"].size[0] > 2040, ( @@ -54,12 +52,12 @@ def test_loading(): f"Width ({data['data'].size[1]}) for {s.key} is smaller " f"than 1740 pixels" ) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].mode == "RGB" assert "label" in data - #nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["data"].size, data["label"].size) - nose.tools.eq_(data["label"].mode, "1") + #assert data["label"].size == image_size + assert data["data"].size == data["label"].size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b + w) == numpy.prod(data["data"].size), ( f"Counts of black + white ({b}+{w}) do not add up to total " @@ -108,7 +106,7 @@ def test_loading(): #print(f"max label proportions = {max(proportions)}") -@rc_variable_set("bob.ip.binseg.drishtigs1.datadir") -@attr("slow") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drishtigs1.datadir") +@pytest.mark.slow def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_drive.py b/bob/ip/binseg/test/test_drive.py index 53c953e9fd3d2e54785129bb57c674800cd742d3..ce774a028e27bd7b0d7828c4e3cc64f342cb5fec 100644 --- a/bob/ip/binseg/test/test_drive.py +++ b/bob/ip/binseg/test/test_drive.py @@ -5,39 +5,38 @@ """Tests for DRIVE""" import os - import numpy -import nose.tools +import pytest from ..data.drive import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): subset = dataset.subsets("default") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 20) + assert len(subset["train"]) == 20 for s in subset["train"]: assert s.key.startswith(os.path.join("training", "images")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 20) + assert len(subset["test"]) == 20 for s in subset["test"]: assert s.key.startswith(os.path.join("test", "images")) subset = dataset.subsets("second-annotator") - nose.tools.eq_(len(subset), 1) + assert len(subset) == 1 assert "test" in subset - nose.tools.eq_(len(subset["test"]), 20) + assert len(subset["test"]) == 20 for s in subset["test"]: assert s.key.startswith(os.path.join("test", "images")) -@rc_variable_set('bob.ip.binseg.drive.datadir') +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.drive.datadir') def test_loading(): image_size = (565, 584) @@ -46,15 +45,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 3) + assert len(data) == 3 assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b+w) == numpy.prod(image_size), \ f"Counts of black + white ({b}+{w}) do not add up to total " \ @@ -66,8 +65,8 @@ def test_loading(): f"indicate a loading problem!" assert "mask" in data - nose.tools.eq_(data["mask"].size, image_size) - nose.tools.eq_(data["mask"].mode, "1") + assert data["mask"].size == image_size + assert data["mask"].mode == "1" bm, wm = count_bw(data["mask"]) assert (bm+wm) == numpy.prod(image_size), \ f"Counts of black + white ({bm}+{wm}) do not add up to total " \ @@ -104,6 +103,6 @@ def test_loading(): #print(f"min mask proportions = {min(k[1] for k in proportions)}") -@rc_variable_set('bob.ip.binseg.drive.datadir') +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.drive.datadir') def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_hrf.py b/bob/ip/binseg/test/test_hrf.py index fdddfbbc78d79c228e1bca01e3a3ebf9dda84f65..761e837c00ff2b5882f8dd80fb43e97f6fd3d1ca 100644 --- a/bob/ip/binseg/test/test_hrf.py +++ b/bob/ip/binseg/test/test_hrf.py @@ -5,31 +5,30 @@ """Tests for HRF""" import os - import numpy -import nose.tools +import pytest from ..data.hrf import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): subset = dataset.subsets("default") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 15) + assert len(subset["train"]) == 15 for s in subset["train"]: assert s.key.startswith(os.path.join("images", "0")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 30) + assert len(subset["test"]) == 30 for s in subset["test"]: assert s.key.startswith("images") -@rc_variable_set('bob.ip.binseg.hrf.datadir') +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.hrf.datadir') def test_loading(): image_size = (3504, 2336) @@ -38,15 +37,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 3) + assert len(data) == 3 assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b+w) == numpy.prod(image_size), \ f"Counts of black + white ({b}+{w}) do not add up to total " \ @@ -58,8 +57,8 @@ def test_loading(): f"indicate a loading problem!" assert "mask" in data - nose.tools.eq_(data["mask"].size, image_size) - nose.tools.eq_(data["mask"].mode, "1") + assert data["mask"].size == image_size + assert data["mask"].mode == "1" bm, wm = count_bw(data["mask"]) assert (bm+wm) == numpy.prod(image_size), \ f"Counts of black + white ({bm}+{wm}) do not add up to total " \ @@ -91,6 +90,6 @@ def test_loading(): #print(f"min mask proportions = {min(k[1] for k in proportions)}") -@rc_variable_set('bob.ip.binseg.hrf.datadir') +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.hrf.datadir') def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_iostar.py b/bob/ip/binseg/test/test_iostar.py index 9d8946b60d1160fa5b0dca95c39de7badac1a1aa..4627a5521574d3a3f48595a978b16faebb474391 100644 --- a/bob/ip/binseg/test/test_iostar.py +++ b/bob/ip/binseg/test/test_iostar.py @@ -5,44 +5,43 @@ """Tests for IOSTAR""" import os - import numpy -import nose.tools +import pytest from ..data.iostar import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): subset = dataset.subsets("vessel") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 20) + assert len(subset["train"]) == 20 for s in subset["train"]: assert s.key.startswith(os.path.join("image", "STAR ")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 10) + assert len(subset["test"]) == 10 for s in subset["test"]: assert s.key.startswith(os.path.join("image", "STAR ")) subset = dataset.subsets("optic-disc") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 20) + assert len(subset["train"]) == 20 for s in subset["train"]: assert s.key.startswith(os.path.join("image", "STAR ")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 10) + assert len(subset["test"]) == 10 for s in subset["test"]: assert s.key.startswith(os.path.join("image", "STAR ")) -@rc_variable_set('bob.ip.binseg.iostar.datadir') +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.iostar.datadir') def test_loading(): image_size = (1024, 1024) @@ -51,15 +50,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 3) + assert len(data) == 3 assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b+w) == numpy.prod(image_size), \ f"Counts of black + white ({b}+{w}) do not add up to total " \ @@ -71,8 +70,8 @@ def test_loading(): f"indicate a loading problem!" assert "mask" in data - nose.tools.eq_(data["mask"].size, image_size) - nose.tools.eq_(data["mask"].mode, "1") + assert data["mask"].size == image_size + assert data["mask"].mode == "1" bm, wm = count_bw(data["mask"]) assert (bm+wm) == numpy.prod(image_size), \ f"Counts of black + white ({bm}+{wm}) do not add up to total " \ @@ -111,6 +110,7 @@ def test_loading(): #print(f"max label proportions = {max(k[0] for k in proportions)}") #print(f"min mask proportions = {min(k[1] for k in proportions)}") -@rc_variable_set('bob.ip.binseg.iostar.datadir') + +@pytest.mark.skip_if_rc_var_not_set('bob.ip.binseg.iostar.datadir') def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_measures.py b/bob/ip/binseg/test/test_measures.py index b0d81e39760d967c2dd8110f8da5e9ece6869c5d..1eeef316b6f34d0d0f515ae7679ff8b8390f6cd7 100644 --- a/bob/ip/binseg/test/test_measures.py +++ b/bob/ip/binseg/test/test_measures.py @@ -7,7 +7,7 @@ import math import numpy import torch -import nose.tools +import pytest from ..utils.measure import ( base_measures, @@ -146,12 +146,20 @@ class TestBayesian: # Notice that for very large k and l, the base frequentist measures # should be approximately the same as the bayesian mean and mode # extracted from the beta posterior. We test that here. - assert numpy.isclose(_prec, prec[0]), f"freq: {_prec} <> bays: {prec[0]}" - assert numpy.isclose(_prec, prec[1]), f"freq: {_prec} <> bays: {prec[1]}" + assert numpy.isclose( + _prec, prec[0] + ), f"freq: {_prec} <> bays: {prec[0]}" + assert numpy.isclose( + _prec, prec[1] + ), f"freq: {_prec} <> bays: {prec[1]}" assert numpy.isclose(_rec, rec[0]), f"freq: {_rec} <> bays: {rec[0]}" assert numpy.isclose(_rec, rec[1]), f"freq: {_rec} <> bays: {rec[1]}" - assert numpy.isclose(_spec, spec[0]), f"freq: {_spec} <> bays: {spec[0]}" - assert numpy.isclose(_spec, spec[1]), f"freq: {_spec} <> bays: {spec[1]}" + assert numpy.isclose( + _spec, spec[0] + ), f"freq: {_spec} <> bays: {spec[0]}" + assert numpy.isclose( + _spec, spec[1] + ), f"freq: {_spec} <> bays: {spec[1]}" assert numpy.isclose(_acc, acc[0]), f"freq: {_acc} <> bays: {acc[0]}" assert numpy.isclose(_acc, acc[1]), f"freq: {_acc} <> bays: {acc[1]}" assert numpy.isclose(_jac, jac[0]), f"freq: {_jac} <> bays: {jac[0]}" @@ -161,18 +169,24 @@ class TestBayesian: # We also test that the interval in question includes the mode and the # mean in this case. - assert (prec[2] < prec[1]) and (prec[1] < prec[3]), \ - f"precision is out of bounds {_prec[2]} < {_prec[1]} < {_prec[3]}" - assert (rec[2] < rec[1]) and (rec[1] < rec[3]), \ - f"recall is out of bounds {_rec[2]} < {_rec[1]} < {_rec[3]}" - assert (spec[2] < spec[1]) and (spec[1] < spec[3]), \ - f"specif. is out of bounds {_spec[2]} < {_spec[1]} < {_spec[3]}" - assert (acc[2] < acc[1]) and (acc[1] < acc[3]), \ - f"accuracy is out of bounds {_acc[2]} < {_acc[1]} < {_acc[3]}" - assert (jac[2] < jac[1]) and (jac[1] < jac[3]), \ - f"jaccard is out of bounds {_jac[2]} < {_jac[1]} < {_jac[3]}" - assert (f1[2] < f1[1]) and (f1[1] < f1[3]), \ - f"f1-score is out of bounds {_f1[2]} < {_f1[1]} < {_f1[3]}" + assert (prec[2] < prec[1]) and ( + prec[1] < prec[3] + ), f"precision is out of bounds {_prec[2]} < {_prec[1]} < {_prec[3]}" + assert (rec[2] < rec[1]) and ( + rec[1] < rec[3] + ), f"recall is out of bounds {_rec[2]} < {_rec[1]} < {_rec[3]}" + assert (spec[2] < spec[1]) and ( + spec[1] < spec[3] + ), f"specif. is out of bounds {_spec[2]} < {_spec[1]} < {_spec[3]}" + assert (acc[2] < acc[1]) and ( + acc[1] < acc[3] + ), f"accuracy is out of bounds {_acc[2]} < {_acc[1]} < {_acc[3]}" + assert (jac[2] < jac[1]) and ( + jac[1] < jac[3] + ), f"jaccard is out of bounds {_jac[2]} < {_jac[1]} < {_jac[3]}" + assert (f1[2] < f1[1]) and ( + f1[1] < f1[3] + ), f"f1-score is out of bounds {_f1[2]} < {_f1[1]} < {_f1[3]}" def test_auc(): @@ -214,18 +228,20 @@ def test_auc(): ) -@nose.tools.raises(ValueError) def test_auc_raises_value_error(): - # x is **not** monotonically increasing or decreasing - assert math.isclose(auc([0.0, 0.5, 0.0], [1.0, 1.0, 1.0]), 1.0) + with pytest.raises( + ValueError, match=r".*neither increasing nor decreasing.*" + ): + # x is **not** monotonically increasing or decreasing + assert math.isclose(auc([0.0, 0.5, 0.0], [1.0, 1.0, 1.0]), 1.0) -@nose.tools.raises(AssertionError) def test_auc_raises_assertion_error(): - # x is **not** the same size as y - assert math.isclose(auc([0.0, 0.5, 1.0], [1.0, 1.0]), 1.0) + with pytest.raises(AssertionError, match=r".*must have the same length.*"): + # x is **not** the same size as y + assert math.isclose(auc([0.0, 0.5, 1.0], [1.0, 1.0]), 1.0) def test_sample_measures_mask_checkerbox(): @@ -244,11 +260,8 @@ def test_sample_measures_mask_checkerbox(): tn = 0 fn = 0 - nose.tools.eq_( - (tp, fp, tn, fn), - sample_measures_for_threshold( - prediction, ground_truth, mask, threshold - ), + assert (tp, fp, tn, fn) == sample_measures_for_threshold( + prediction, ground_truth, mask, threshold ) @@ -272,11 +285,8 @@ def test_sample_measures_mask_cross(): tn = 2 fn = 2 - nose.tools.eq_( - (tp, fp, tn, fn), - sample_measures_for_threshold( - prediction, ground_truth, mask, threshold - ), + assert (tp, fp, tn, fn) == sample_measures_for_threshold( + prediction, ground_truth, mask, threshold ) @@ -304,9 +314,6 @@ def test_sample_measures_mask_border(): tn = 47 fn = 1 - nose.tools.eq_( - (tp, fp, tn, fn), - sample_measures_for_threshold( - prediction, ground_truth, mask, threshold - ), + assert (tp, fp, tn, fn) == sample_measures_for_threshold( + prediction, ground_truth, mask, threshold ) diff --git a/bob/ip/binseg/test/test_models.py b/bob/ip/binseg/test/test_models.py index f078a99ef4d250a8f1a7da218058c9f9143f67aa..125d4a06a5980a3c69b4897cb3b0c7ddb23ba67f 100644 --- a/bob/ip/binseg/test/test_models.py +++ b/bob/ip/binseg/test/test_models.py @@ -4,7 +4,6 @@ """Tests model loading""" -import nose.tools from ..models.normalizer import TorchVisionNormalizer from ..models.backbones.vgg import VGG4Segmentation @@ -14,15 +13,15 @@ def test_driu(): from ..models.driu import driu, DRIU model = driu(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[2]), DRIU) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == VGG4Segmentation #backbone + assert type(model[2]) == DRIU #head model = driu(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[1]), DRIU) #head + assert len(model) == 2 + assert type(model[0]) == VGG4Segmentation #backbone + assert type(model[1]) == DRIU #head def test_driu_bn(): @@ -30,15 +29,15 @@ def test_driu_bn(): from ..models.driu_bn import driu_bn, DRIUBN model = driu_bn(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[2]), DRIUBN) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == VGG4Segmentation #backbone + assert type(model[2]) == DRIUBN #head model = driu_bn(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[1]), DRIUBN) #head + assert len(model) == 2 + assert type(model[0]) == VGG4Segmentation #backbone + assert type(model[1]) == DRIUBN #head def test_driu_od(): @@ -46,15 +45,15 @@ def test_driu_od(): from ..models.driu_od import driu_od, DRIUOD model = driu_od(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[2]), DRIUOD) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == VGG4Segmentation #backbone + assert type(model[2]) == DRIUOD #head model = driu_od(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[1]), DRIUOD) #head + assert len(model) == 2 + assert type(model[0]) == VGG4Segmentation #backbone + assert type(model[1]) == DRIUOD #head def test_driu_pix(): @@ -62,15 +61,15 @@ def test_driu_pix(): from ..models.driu_pix import driu_pix, DRIUPIX model = driu_pix(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[2]), DRIUPIX) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == VGG4Segmentation #backbone + assert type(model[2]) == DRIUPIX #head model = driu_pix(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[1]), DRIUPIX) #head + assert len(model) == 2 + assert type(model[0]) == VGG4Segmentation #backbone + assert type(model[1]) == DRIUPIX #head def test_unet(): @@ -78,15 +77,15 @@ def test_unet(): from ..models.unet import unet, UNet model = unet(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[2]), UNet) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == VGG4Segmentation #backbone + assert type(model[2]) == UNet #head model = unet(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[1]), UNet) #head + assert len(model) == 2 + assert type(model[0]) == VGG4Segmentation #backbone + assert type(model[1]) == UNet #head def test_hed(): @@ -94,15 +93,15 @@ def test_hed(): from ..models.hed import hed, HED model = hed(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[2]), HED) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == VGG4Segmentation #backbone + assert type(model[2]) == HED #head model = hed(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), VGG4Segmentation) #backbone - nose.tools.eq_(type(model[1]), HED) #head + assert len(model) == 2 + assert type(model[0]) == VGG4Segmentation #backbone + assert type(model[1]) == HED #head def test_m2unet(): @@ -111,15 +110,15 @@ def test_m2unet(): from ..models.backbones.mobilenetv2 import MobileNetV24Segmentation model = m2unet(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), MobileNetV24Segmentation) #backbone - nose.tools.eq_(type(model[2]), M2UNet) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == MobileNetV24Segmentation #backbone + assert type(model[2]) == M2UNet #head model = m2unet(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), MobileNetV24Segmentation) #backbone - nose.tools.eq_(type(model[1]), M2UNet) #head + assert len(model) == 2 + assert type(model[0]) == MobileNetV24Segmentation #backbone + assert type(model[1]) == M2UNet #head def test_resunet50(): @@ -128,13 +127,13 @@ def test_resunet50(): from ..models.backbones.resnet import ResNet4Segmentation model = resunet50(pretrained_backbone=True, progress=True) - nose.tools.eq_(len(model), 3) - nose.tools.eq_(type(model[0]), TorchVisionNormalizer) - nose.tools.eq_(type(model[1]), ResNet4Segmentation) #backbone - nose.tools.eq_(type(model[2]), ResUNet) #head + assert len(model) == 3 + assert type(model[0]) == TorchVisionNormalizer + assert type(model[1]) == ResNet4Segmentation #backbone + assert type(model[2]) == ResUNet #head model = resunet50(pretrained_backbone=False) - nose.tools.eq_(len(model), 2) - nose.tools.eq_(type(model[0]), ResNet4Segmentation) #backbone - nose.tools.eq_(type(model[1]), ResUNet) #head + assert len(model) == 2 + assert type(model[0]) == ResNet4Segmentation #backbone + assert type(model[1]) == ResUNet #head print(model) diff --git a/bob/ip/binseg/test/test_refuge.py b/bob/ip/binseg/test/test_refuge.py index d4fac282cc210e1f04d09d74ad16d818641cae59..afdff5c04b371e4489629464c1f3eb515aaa9069 100644 --- a/bob/ip/binseg/test/test_refuge.py +++ b/bob/ip/binseg/test/test_refuge.py @@ -5,13 +5,11 @@ """Tests for REFUGE""" import os - import numpy -import nose.tools -from nose.plugins.attrib import attr +import pytest from ..data.refuge import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): @@ -19,26 +17,26 @@ def test_protocol_consistency(): for protocol in ("optic-disc", "optic-cup"): subset = dataset.subsets(protocol) - nose.tools.eq_(len(subset), 3) + assert len(subset) == 3 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 400) + assert len(subset["train"]) == 400 for s in subset["train"]: assert s.key.startswith("Training400") assert "validation" in subset - nose.tools.eq_(len(subset["validation"]), 400) + assert len(subset["validation"]) == 400 for s in subset["validation"]: assert s.key.startswith("REFUGE-Validation400") assert "test" in subset - nose.tools.eq_(len(subset["test"]), 400) + assert len(subset["test"]) == 400 for s in subset["test"]: assert s.key.startswith("Test400") -@rc_variable_set("bob.ip.binseg.refuge.datadir") -@attr("slow") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.refuge.datadir") +@pytest.mark.slow def test_loading(): def _check_sample( @@ -47,15 +45,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), entries) + assert len(data) == entries assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b + w) == numpy.prod(image_size), ( f"Counts of black + white ({b}+{w}) do not add up to total " @@ -99,7 +97,7 @@ def test_loading(): #print(f"max label proportions = {max(proportions)}") -@rc_variable_set("bob.ip.binseg.refuge.datadir") -@attr("slow") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.refuge.datadir") +@pytest.mark.slow def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_rimoner3.py b/bob/ip/binseg/test/test_rimoner3.py index e6010cf6a3dec0c9b4c5c84dc0857b92c349f174..e5678cb417286595b7b2fa1e6d5d7818c55c513b 100644 --- a/bob/ip/binseg/test/test_rimoner3.py +++ b/bob/ip/binseg/test/test_rimoner3.py @@ -5,13 +5,11 @@ """Tests for RIM-ONE r3""" import os - import numpy -import nose.tools -from nose.plugins.attrib import attr +import pytest from ..data.rimoner3 import dataset -from .utils import rc_variable_set, count_bw +from .utils import count_bw def test_protocol_consistency(): @@ -20,21 +18,21 @@ def test_protocol_consistency(): "optic-cup-exp2", "optic-disc-avg", "optic-cup-avg"): subset = dataset.subsets(protocol) - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 99) + assert len(subset["train"]) == 99 for s in subset["train"]: assert "Stereo Images" in s.key assert "test" in subset - nose.tools.eq_(len(subset["test"]), 60) + assert len(subset["test"]) == 60 for s in subset["test"]: assert "Stereo Images" in s.key -@rc_variable_set("bob.ip.binseg.rimoner3.datadir") -@attr("slow") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.rimoner3.datadir") +@pytest.mark.slow def test_loading(): image_size = (1072, 1424) @@ -43,15 +41,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 2) + assert len(data) == 2 assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b+w) == numpy.prod(image_size), \ f"Counts of black + white ({b}+{w}) do not add up to total " \ @@ -110,7 +108,7 @@ def test_loading(): #print(f"max label proportions = {max(proportions)}") -@rc_variable_set("bob.ip.binseg.rimoner3.datadir") -@attr("slow") +@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.rimoner3.datadir") +@pytest.mark.slow def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_significance.py b/bob/ip/binseg/test/test_significance.py index 8f1464238349f3bc42ed20bf5e635d6c3f5ae944..08b7c5249551ee0e385821f969f59a430d53ecf6 100644 --- a/bob/ip/binseg/test/test_significance.py +++ b/bob/ip/binseg/test/test_significance.py @@ -6,8 +6,8 @@ import numpy import pandas -import nose.tools import torch +import pytest from ..engine.significance import ( _winperf_measures, @@ -35,6 +35,7 @@ def _check_window_measures(pred, gt, mask, threshold, size, stride, expected): ), f"Actual output:\n{actual}\n **!=** Expected output:\n{expected}" +@pytest.mark.skip def test_winperf_measures_alltrue(): pred = numpy.ones((4, 4), dtype=float) @@ -53,6 +54,7 @@ def test_winperf_measures_alltrue(): _check_window_measures(pred, gt, mask, threshold, size, stride, expected) +@pytest.mark.skip def test_winperf_measures_alltrue_with_padding(): pred = numpy.ones((3, 3), dtype=float) @@ -70,6 +72,7 @@ def test_winperf_measures_alltrue_with_padding(): _check_window_measures(pred, gt, mask, threshold, size, stride, expected) +@pytest.mark.skip def test_winperf_measures_dot_with_padding(): pred = numpy.ones((3, 3), dtype=float) @@ -88,6 +91,7 @@ def test_winperf_measures_dot_with_padding(): _check_window_measures(pred, gt, mask, threshold, size, stride, expected) +@pytest.mark.skip def test_winperf_measures_cross(): pred = numpy.zeros((5, 5), dtype=float) @@ -111,6 +115,7 @@ def test_winperf_measures_cross(): _check_window_measures(pred, gt, mask, threshold, size, stride, expected) +@pytest.mark.skip def test_winperf_measures_cross_with_padding(): pred = numpy.zeros((5, 5), dtype=float) @@ -130,6 +135,7 @@ def test_winperf_measures_cross_with_padding(): _check_window_measures(pred, gt, mask, threshold, size, stride, expected) +@pytest.mark.skip def test_winperf_measures_cross_with_padding_2(): pred = numpy.zeros((5, 5), dtype=float) @@ -197,6 +203,7 @@ def _check_performance_summary(pred, gt, mask, threshold, size, stride, s, figur ) +@pytest.mark.skip def test_performance_summary_alltrue_accuracy(): pred = numpy.ones((4, 4), dtype=float) @@ -236,6 +243,7 @@ def test_performance_summary_alltrue_accuracy(): ) +@pytest.mark.skip def test_performance_summary_cross(): pred = numpy.zeros((5, 5), dtype=float) @@ -273,6 +281,7 @@ def test_performance_summary_cross(): ) +@pytest.mark.skip def test_performance_summary_cross_with_padding(): pred = numpy.zeros((5, 5), dtype=float) @@ -309,6 +318,7 @@ def test_performance_summary_cross_with_padding(): ) +@pytest.mark.skip def test_performance_summary_cross_with_padding_2(): pred = numpy.zeros((5, 5), dtype=float) diff --git a/bob/ip/binseg/test/test_stare.py b/bob/ip/binseg/test/test_stare.py index 8793447fb779c176a8a742b175a514351d8689ec..bec3053dc1c7c4e548b3a45de88789ed127b4d91 100644 --- a/bob/ip/binseg/test/test_stare.py +++ b/bob/ip/binseg/test/test_stare.py @@ -5,13 +5,11 @@ """Tests for STARE""" import os - import numpy -import nose.tools ## special trick for CI builds from . import mock_dataset -datadir, dataset, rc_variable_set = mock_dataset() +datadir, dataset = mock_dataset() from .utils import count_bw @@ -19,33 +17,32 @@ from .utils import count_bw def test_protocol_consistency(): subset = dataset.subsets("ah") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 10) + assert len(subset["train"]) == 10 for s in subset["train"]: assert s.key.startswith(os.path.join("stare-images", "im0")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 10) + assert len(subset["test"]) == 10 for s in subset["test"]: assert s.key.startswith(os.path.join("stare-images", "im0")) subset = dataset.subsets("vk") - nose.tools.eq_(len(subset), 2) + assert len(subset) == 2 assert "train" in subset - nose.tools.eq_(len(subset["train"]), 10) + assert len(subset["train"]) == 10 for s in subset["train"]: assert s.key.startswith(os.path.join("stare-images", "im0")) assert "test" in subset - nose.tools.eq_(len(subset["test"]), 10) + assert len(subset["test"]) == 10 for s in subset["test"]: assert s.key.startswith(os.path.join("stare-images", "im0")) -@rc_variable_set('bob.ip.binseg.stare.datadir') def test_loading(): image_size = (700, 605) @@ -54,15 +51,15 @@ def test_loading(): data = s.data assert isinstance(data, dict) - nose.tools.eq_(len(data), 3) + assert len(data) == 3 assert "data" in data - nose.tools.eq_(data["data"].size, image_size) - nose.tools.eq_(data["data"].mode, "RGB") + assert data["data"].size == image_size + assert data["data"].mode == "RGB" assert "label" in data - nose.tools.eq_(data["label"].size, image_size) - nose.tools.eq_(data["label"].mode, "1") + assert data["label"].size == image_size + assert data["label"].mode == "1" b, w = count_bw(data["label"]) assert (b+w) == numpy.prod(image_size), \ f"Counts of black + white ({b}+{w}) do not add up to total " \ @@ -74,8 +71,8 @@ def test_loading(): f"indicate a loading problem!" assert "mask" in data - nose.tools.eq_(data["mask"].size, image_size) - nose.tools.eq_(data["mask"].mode, "1") + assert data["mask"].size == image_size + assert data["mask"].mode == "1" bm, wm = count_bw(data["mask"]) assert (bm+wm) == numpy.prod(image_size), \ f"Counts of black + white ({bm}+{wm}) do not add up to total " \ @@ -111,6 +108,5 @@ def test_loading(): #print(f"max label proportions = {max(proportions)}") -@rc_variable_set('bob.ip.binseg.stare.datadir') def test_check(): - nose.tools.eq_(dataset.check(), 0) + assert dataset.check() == 0 diff --git a/bob/ip/binseg/test/test_transforms.py b/bob/ip/binseg/test/test_transforms.py index e71a4a4927f1598a49037dc09a672de91845dd11..9db0458f4110690c840659930d4bf7ea237520d8 100644 --- a/bob/ip/binseg/test/test_transforms.py +++ b/bob/ip/binseg/test/test_transforms.py @@ -4,7 +4,6 @@ import os import random -import nose.tools import pkg_resources import numpy @@ -33,11 +32,9 @@ def test_center_crop(): idx = (slice(bh, -bh), slice(bw, -bw), slice(0, im_size[0])) transforms = CenterCrop(crop_size) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) - nose.tools.eq_( - img_t.size, (crop_size[1], crop_size[0]) - ) # confirms the above + assert img_t.size == (crop_size[1], crop_size[0]) # confirms the above # notice that PIL->array does array.transpose(1, 2, 0) # so it creates an array that is (height, width, planes) assert numpy.all(numpy.array(img_t) == numpy.array(img)[idx]) @@ -57,14 +54,12 @@ def test_center_crop_uneven(): # when the crop size is uneven, this is what happens - notice here that the # image height is uneven, and the crop width as well - the attributions of # extra pixels will depend on what is uneven (original image or crop) - idx = (slice(bh, -(bh + 1)), slice((bw + 1), -bw), slice(0, im_size[0])) + idx = (slice(bh+1, -bh), slice(bw+1, -bw), slice(0, im_size[0])) transforms = CenterCrop(crop_size) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) - nose.tools.eq_( - img_t.size, (crop_size[1], crop_size[0]) - ) # confirms the above + assert img_t.size == (crop_size[1], crop_size[0]) # confirms the above # notice that PIL->array does array.transpose(1, 2, 0) # so it creates an array that is (height, width, planes) assert numpy.all(numpy.array(img_t) == numpy.array(img)[idx]) @@ -86,7 +81,7 @@ def test_pad_default(): ) transforms = Pad(pad_size) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) # notice that PIL->array does array.transpose(1, 2, 0) # so it creates an array that is (height, width, planes) @@ -98,15 +93,15 @@ def test_pad_default(): img_t = numpy.array(img_t) img_t[idx] = 0 border_size_plane = img_t[:, :, 0].size - numpy.array(img)[:, :, 0].size - nose.tools.eq_(img_t.sum(), 0) + assert img_t.sum() == 0 gt_t = numpy.array(gt_t) gt_t[idx] = 0 - nose.tools.eq_(gt_t.sum(), 0) + assert gt_t.sum() == 0 mask_t = numpy.array(mask_t) mask_t[idx] = 0 - nose.tools.eq_(mask_t.sum(), 0) + assert mask_t.sum() == 0 def test_pad_2tuple(): @@ -124,7 +119,7 @@ def test_pad_2tuple(): ) transforms = Pad(pad_size, fill) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) # notice that PIL->array does array.transpose(1, 2, 0) # so it creates an array that is (height, width, planes) @@ -137,15 +132,15 @@ def test_pad_2tuple(): img_t[idx] = 0 border_size_plane = img_t[:, :, 0].size - numpy.array(img)[:, :, 0].size expected_sum = sum((fill[k] * border_size_plane) for k in range(3)) - nose.tools.eq_(img_t.sum(), expected_sum) + assert img_t.sum() == expected_sum gt_t = numpy.array(gt_t) gt_t[idx] = 0 - nose.tools.eq_(gt_t.sum(), expected_sum) + assert gt_t.sum() == expected_sum mask_t = numpy.array(mask_t) mask_t[idx] = 0 - nose.tools.eq_(mask_t.sum(), expected_sum) + assert mask_t.sum() == expected_sum def test_pad_4tuple(): @@ -163,7 +158,7 @@ def test_pad_4tuple(): ) transforms = Pad(pad_size, fill) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) # notice that PIL->array does array.transpose(1, 2, 0) # so it creates an array that is (height, width, planes) @@ -176,15 +171,15 @@ def test_pad_4tuple(): img_t[idx] = 0 border_size_plane = img_t[:, :, 0].size - numpy.array(img)[:, :, 0].size expected_sum = sum((fill[k] * border_size_plane) for k in range(3)) - nose.tools.eq_(img_t.sum(), expected_sum) + assert img_t.sum() == expected_sum gt_t = numpy.array(gt_t) gt_t[idx] = 0 - nose.tools.eq_(gt_t.sum(), expected_sum) + assert gt_t.sum() == expected_sum mask_t = numpy.array(mask_t) mask_t[idx] = 0 - nose.tools.eq_(mask_t.sum(), expected_sum) + assert mask_t.sum() == expected_sum def test_resize_downscale_w(): @@ -196,12 +191,12 @@ def test_resize_downscale_w(): # test transforms = Resize(new_size) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) new_size = (new_size, (new_size * im_size[1]) / im_size[2]) - nose.tools.eq_(img_t.size, new_size) - nose.tools.eq_(gt_t.size, new_size) - nose.tools.eq_(mask_t.size, new_size) + assert img_t.size == new_size + assert gt_t.size == new_size + assert mask_t.size == new_size def test_resize_downscale_hw(): @@ -213,11 +208,11 @@ def test_resize_downscale_hw(): # test transforms = Resize(new_size) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) - nose.tools.eq_(img_t.size, (new_size[1], new_size[0])) - nose.tools.eq_(gt_t.size, (new_size[1], new_size[0])) - nose.tools.eq_(mask_t.size, (new_size[1], new_size[0])) + assert img_t.size == (new_size[1], new_size[0]) + assert gt_t.size == (new_size[1], new_size[0]) + assert mask_t.size == (new_size[1], new_size[0]) def test_crop(): @@ -234,7 +229,7 @@ def test_crop(): ) transforms = Crop(*crop_size) img, gt, mask = [_create_img(im_size) for i in range(3)] - nose.tools.eq_(img.size, (im_size[2], im_size[1])) # confirms the above + assert img.size == (im_size[2], im_size[1]) # confirms the above img_t, gt_t, mask_t = transforms(img, gt, mask) # notice that PIL->array does array.transpose(1, 2, 0) # so it creates an array that is (height, width, planes) @@ -250,9 +245,9 @@ def test_to_tensor(): gt = gt.convert("1", dither=None) mask = mask.convert("1", dither=None) img_t, gt_t, mask_t = transforms(img, gt, mask) - nose.tools.eq_(img_t.dtype, torch.float32) - nose.tools.eq_(gt_t.dtype, torch.float32) - nose.tools.eq_(mask_t.dtype, torch.float32) + assert img_t.dtype == torch.float32 + assert gt_t.dtype == torch.float32 + assert mask_t.dtype == torch.float32 def test_horizontal_flip(): @@ -295,7 +290,7 @@ def test_rotation(): # and they are different from the original random.seed(42) img1_t, img2_t, img3_t = transforms(img, img, img) - nose.tools.eq_(img1_t.size, (im_size[2], im_size[1])) + assert img1_t.size == (im_size[2], im_size[1]) assert numpy.all(numpy.array(img1_t) == numpy.array(img2_t)) assert numpy.all(numpy.array(img1_t) == numpy.array(img3_t)) assert numpy.any(numpy.array(img1_t) != numpy.array(img)) @@ -316,7 +311,7 @@ def test_color_jitter(): # all others match the input data random.seed(42) img1_t, img2_t, img3_t = transforms(img, img, img) - nose.tools.eq_(img1_t.size, (im_size[2], im_size[1])) + assert img1_t.size == (im_size[2], im_size[1]) assert numpy.any(numpy.array(img1_t) != numpy.array(img)) assert numpy.any(numpy.array(img1_t) != numpy.array(img2_t)) assert numpy.all(numpy.array(img2_t) == numpy.array(img3_t)) @@ -355,11 +350,11 @@ def test_16bit_autolevel(): # https://stackoverflow.com/questions/32622658/read-16-bit-png-image-file-using-python # https://github.com/python-pillow/Pillow/issues/3011 img = PIL.Image.fromarray(numpy.array(PIL.Image.open(path)).astype("uint16")) - nose.tools.eq_(img.mode, "I;16") - nose.tools.eq_(img.getextrema(), (0, 65281)) + assert img.mode == "I;16" + assert img.getextrema() == (0, 65281) timg = SingleAutoLevel16to8()(img) - nose.tools.eq_(timg.mode, "L") - nose.tools.eq_(timg.getextrema(), (0, 255)) + assert timg.mode == "L" + assert timg.getextrema() == (0, 255) #timg.show() #import ipdb; ipdb.set_trace() diff --git a/bob/ip/binseg/test/utils.py b/bob/ip/binseg/test/utils.py index 0f2e7fcf62dc8404b88f75a15cb2ab8c96d4992a..31f0e490e15123ea310afe9711045b10ac7e3687 100644 --- a/bob/ip/binseg/test/utils.py +++ b/bob/ip/binseg/test/utils.py @@ -5,29 +5,7 @@ """Test utilities""" -import functools - import numpy -import nose.plugins.skip - -import bob.extension - - -def rc_variable_set(name): - """ - Decorator that checks if a given bobrc variable is set before running - """ - - def wrapped_function(test): - @functools.wraps(test) - def wrapper(*args, **kwargs): - if name not in bob.extension.rc: - raise nose.plugins.skip.SkipTest("Bob's RC variable '%s' is not set" % name) - return test(*args, **kwargs) - - return wrapper - - return wrapped_function def count_bw(b): diff --git a/bob/ip/binseg/utils/checkpointer.py b/bob/ip/binseg/utils/checkpointer.py index c0ad2a6199be61858dd0fa67e3b8a96626966085..2702d7699ec875c11a6d887829be74d6fe700fae 100644 --- a/bob/ip/binseg/utils/checkpointer.py +++ b/bob/ip/binseg/utils/checkpointer.py @@ -74,7 +74,7 @@ class Checkpointer: if f is None: # no checkpoint could be found - logger.warn("No checkpoint found (and none passed)") + logger.warning("No checkpoint found (and none passed)") return {} # loads file data into memory diff --git a/bob/ip/binseg/utils/measure.py b/bob/ip/binseg/utils/measure.py index e7d14da6d714a3f6216121149ccc767a6dffe176..19af3fc1eb04e37818086596f2fc45083b15beda 100644 --- a/bob/ip/binseg/utils/measure.py +++ b/bob/ip/binseg/utils/measure.py @@ -133,30 +133,30 @@ def beta_credible_region(k, l, lambda_, coverage): from a series of Bernoulli trials (likelihood is binomial). The posterior is derivated using the Bayes Theorem with a beta prior. As there is no reason to favour high vs. low precision, we use a symmetric Beta prior - (:math:`\alpha=\beta`): + (:math:`\\alpha=\\beta`): .. math:: - P(p|k,n) &= \frac{P(k,n|p)P(p)}{P(k,n)} \\ - P(p|k,n) &= \frac{\frac{n!}{k!(n-k)!}p^{k}(1-p)^{n-k}P(p)}{P(k)} \\ - P(p|k,n) &= \frac{1}{B(k+\alpha, n-k+\beta)}p^{k+\alpha-1}(1-p)^{n-k+\beta-1} - P(p|k,n) &= \frac{1}{B(k+\alpha, n-k+\alpha)}p^{k+\alpha-1}(1-p)^{n-k+\alpha-1} + P(p|k,n) &= \\frac{P(k,n|p)P(p)}{P(k,n)} \\\\ + P(p|k,n) &= \\frac{\\frac{n!}{k!(n-k)!}p^{k}(1-p)^{n-k}P(p)}{P(k)} \\\\ + P(p|k,n) &= \\frac{1}{B(k+\\alpha, n-k+\beta)}p^{k+\\alpha-1}(1-p)^{n-k+\\beta-1} \\\\ + P(p|k,n) &= \\frac{1}{B(k+\\alpha, n-k+\\alpha)}p^{k+\\alpha-1}(1-p)^{n-k+\\alpha-1} The mode for this posterior (also the maximum a posteriori) is: .. math:: - mode(p) = \frac{k+\lambda-1}{n+2\lambda-2} + \\text{mode}(p) = \\frac{k+\\lambda-1}{n+2\\lambda-2} Concretely, the prior may be flat (all rates are equally likely, - :math:`\lambda=1`) or we may use Jeoffrey's prior (:math:`\lambda=0.5`), - that is invariant through re-parameterisation. Jeffrey's prior indicate - that rates close to zero or one are more likely. + :math:`\\lambda=1`) or we may use Jeoffrey's prior + (:math:`\\lambda=0.5`), that is invariant through re-parameterisation. + Jeffrey's prior indicate that rates close to zero or one are more likely. - The mode above works if :math:`k+\alpha,n-k+\alpha > 1`, which is usually - the case for a resonably well tunned system, with more than a few samples - for analysis. In the limit of the system performance, :math:`k` may be 0, - which will make the mode become zero. + The mode above works if :math:`k+{\\alpha},n-k+{\\alpha} > 1`, which is + usually the case for a resonably well tunned system, with more than a few + samples for analysis. In the limit of the system performance, :math:`k` + may be 0, which will make the mode become zero. For our purposes, it may be more suitable to represent :math:`n = k + l`, with :math:`k`, the number of successes and :math:`l`, the number of @@ -165,8 +165,8 @@ def beta_credible_region(k, l, lambda_, coverage): .. math:: - P(p|k,l) &= \frac{1}{B(k+\alpha, l+\alpha)}p^{k+\alpha-1}(1-p)^{l+\alpha-1} \\ - mode(p) &= \frac{k+\lambda-1}{k+l+2\lambda-2} + P(p|k,l) &= \\frac{1}{B(k+\\alpha, l+\\alpha)}p^{k+\\alpha-1}(1-p)^{l+\\alpha-1} \\\\ + \\text{mode}(p) &= \\frac{k+\\lambda-1}{k+l+2\\lambda-2} This can be mapped to most rates calculated in the context of binary classification this way: @@ -189,7 +189,7 @@ def beta_credible_region(k, l, lambda_, coverage): .. note:: - For a disambiguation with Confidence Interfval, read + For a disambiguation with Confidence Interval, read https://en.wikipedia.org/wiki/Credible_interval. @@ -202,9 +202,9 @@ def beta_credible_region(k, l, lambda_, coverage): l : int Number of failures observed on the experiment - lambda_ : :py:class:`float`, Optional + lambda__ : :py:class:`float`, Optional The parameterisation of the Beta prior to consider. Use - :math:`\lambda=1` for a flat prior. Use :math:`\lambda=0.5` for + :math:`\\lambda=1` for a flat prior. Use :math:`\\lambda=0.5` for Jeffrey's prior (the default). coverage : :py:class:`float`, Optional diff --git a/conda/meta.yaml b/conda/meta.yaml index 2a358da09cb0deddc6e6fa0aad848c50025c6675..a34965c24d9daa91f7a2faaf71779f948fc8ca8b 100644 --- a/conda/meta.yaml +++ b/conda/meta.yaml @@ -6,7 +6,6 @@ package: version: {{ environ.get('BOB_PACKAGE_VERSION', '0.0.1') }} build: - skip: true # [not (linux or (osx and py==37))] number: {{ environ.get('BOB_BUILD_NUMBER', 0) }} run_exports: - {{ pin_subpackage(name) }} @@ -29,7 +28,7 @@ requirements: - scipy {{ scipy }} - h5py {{ h5py }} - pytorch {{ pytorch }} - - torchvision {{ torchvision }} # [linux or (osx and py==37)] + - torchvision {{ torchvision }} - bob.extension run: - python @@ -37,7 +36,7 @@ requirements: - {{ pin_compatible('numpy') }} - {{ pin_compatible('scipy') }} - {{ pin_compatible('pytorch') }} - - {{ pin_compatible('torchvision') }} # [linux or (osx and py==37)] + - {{ pin_compatible('torchvision') }} - matplotlib - pandas - pillow @@ -50,15 +49,17 @@ test: imports: - {{ name }} commands: - - nosetests --with-coverage --cover-package={{ name }} --cover-erase --cover-html-dir={{ project_dir }}/sphinx/coverage --cover-html --cover-xml-file={{ project_dir }}/coverage.xml --cover-xml -sv {{ name }} + # runs tests for package only, report only what is in the package + # creates html and xml reports and place them in specific directories + - pytest --capture=no --verbose --cov {{ name }} --cov-report term-missing --cov-report html:{{ project_dir }}/sphinx/coverage --cov-report xml:{{ project_dir }}/coverage.xml --pyargs {{ name }} - sphinx-build -aEW {{ project_dir }}/doc {{ project_dir }}/sphinx - sphinx-build -aEb doctest {{ project_dir }}/doc sphinx - conda inspect linkages -p $PREFIX {{ name }} # [not win] - conda inspect objects -p $PREFIX {{ name }} # [osx] requires: - bob-devel {{ bob_devel }}.* - - nose - - coverage + - pytest + - pytest-cov - sphinx - sphinx_rtd_theme - sphinxcontrib-programoutput