Skip to content
Snippets Groups Projects
Commit 982f75a4 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

[test] Centralize all tests; Mock a test dataset for CI tests

parent 0514d11d
No related branches found
No related tags found
1 merge request!12Streamlining
Pipeline #38953 passed
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
"""Common utilities""" """Common utilities"""
import numpy
import PIL.Image import PIL.Image
import PIL.ImageOps import PIL.ImageOps
import PIL.ImageChops import PIL.ImageChops
...@@ -15,32 +14,6 @@ import torch.utils.data ...@@ -15,32 +14,6 @@ import torch.utils.data
from .transforms import Compose, ToTensor from .transforms import Compose, ToTensor
def count_bw(b):
"""Calculates totals of black and white pixels in a binary image
Parameters
----------
b : PIL.Image.Image
A PIL image in mode "1" to be used for calculating positives and
negatives
Returns
-------
black : int
Number of black pixels in the binary image
white : int
Number of white pixels in the binary image
"""
boolean_array = numpy.array(b)
white = boolean_array.sum()
return (boolean_array.size-white), white
def invert_mode1_image(img): def invert_mode1_image(img):
"""Inverts a binary PIL image (mode == ``"1"``)""" """Inverts a binary PIL image (mode == ``"1"``)"""
......
#!/usr/bin/env python
# coding=utf-8
"""Unit tests"""
import tempfile
import logging
logger = logging.getLogger(__name__)
TESTDB_TMPDIR = None
_URL = "http://www.idiap.ch/software/bob/data/bob/bob.ip.binseg/master/_testdb.zip"
_RCKEY = "bob.ip.binseg.stare.datadir"
def teardown_package():
global TESTDB_TMPDIR
if TESTDB_TMPDIR is not None:
logger.info(f"Removing temporary directory {TESTDB_TMPDIR.name}...")
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
if (TESTDB_TMPDIR is not None) or (_RCKEY in rc):
logger.info("Test database already set up - not downloading")
else:
logger.info("Test database not available, downloading...")
import zipfile
import urllib.request
# Download the file from `url` and save it locally under `file_name`:
with urllib.request.urlopen(_URL) as r, tempfile.TemporaryFile() as f:
f.write(r.read())
f.flush()
f.seek(0)
TESTDB_TMPDIR = \
tempfile.TemporaryDirectory(prefix=__name__ + '-')
print(f"Creating test database at {TESTDB_TMPDIR.name}...")
logger.info(f"Creating test database at {TESTDB_TMPDIR.name}...")
with zipfile.ZipFile(f) as zf: zf.extractall(TESTDB_TMPDIR.name)
from ..data import stare
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 stare.dataset, rc_variable_set
# else, we do a "mock" return
return stare.JSONDataset(stare._protocols, TESTDB_TMPDIR.name,
stare._loader), _mock_test_skipper
...@@ -9,8 +9,8 @@ import os ...@@ -9,8 +9,8 @@ import os
import numpy import numpy
import nose.tools import nose.tools
from . import dataset from ..data.chasedb1 import dataset
from ...test.utils import rc_variable_set from .utils import rc_variable_set, count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -45,7 +45,6 @@ def test_protocol_consistency(): ...@@ -45,7 +45,6 @@ def test_protocol_consistency():
@rc_variable_set('bob.ip.binseg.chasedb1.datadir') @rc_variable_set('bob.ip.binseg.chasedb1.datadir')
def test_loading(): def test_loading():
from ..utils import count_bw
image_size = (999, 960) image_size = (999, 960)
def _check_sample(s, bw_threshold_label): def _check_sample(s, bw_threshold_label):
...@@ -74,7 +73,7 @@ def test_loading(): ...@@ -74,7 +73,7 @@ def test_loading():
# to visualize images, uncomment the folowing code # to visualize images, uncomment the folowing code
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels. # original data, blended with green labels.
#from ..utils import overlayed_image #from ..data.utils import overlayed_image
#display = overlayed_image(data["data"], data["label"]) #display = overlayed_image(data["data"], data["label"])
#display.show() #display.show()
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
......
...@@ -6,6 +6,8 @@ from nose.plugins.attrib import attr ...@@ -6,6 +6,8 @@ from nose.plugins.attrib import attr
import torch import torch
from . import mock_dataset
stare_dataset, stare_variable_set = mock_dataset()
from .utils import rc_variable_set from .utils import rc_variable_set
...@@ -41,10 +43,12 @@ def test_drive_default_test(): ...@@ -41,10 +43,12 @@ def test_drive_default_test():
nose.tools.eq_(sample[3].dtype, torch.float32) nose.tools.eq_(sample[3].dtype, torch.float32)
@rc_variable_set("bob.ip.binseg.stare.datadir") @stare_variable_set("bob.ip.binseg.stare.datadir")
def test_stare_default_train(): def test_stare_default_train():
from ..configs.datasets.stare import dataset from ..configs.datasets.stare import dataset
# hack to allow testing on the CI
dataset._samples = stare_dataset.subsets("default")["train"]
nose.tools.eq_(len(dataset), 10) nose.tools.eq_(len(dataset), 10)
for sample in dataset: for sample in dataset:
nose.tools.eq_(len(sample), 3) nose.tools.eq_(len(sample), 3)
...@@ -55,10 +59,12 @@ def test_stare_default_train(): ...@@ -55,10 +59,12 @@ def test_stare_default_train():
nose.tools.eq_(sample[2].dtype, torch.float32) nose.tools.eq_(sample[2].dtype, torch.float32)
@rc_variable_set("bob.ip.binseg.stare.datadir") @stare_variable_set("bob.ip.binseg.stare.datadir")
def test_stare_default_test(): def test_stare_default_test():
from ..configs.datasets.stare_test import dataset from ..configs.datasets.stare_test import dataset
# hack to allow testing on the CI
dataset._samples = stare_dataset.subsets("default")["test"]
nose.tools.eq_(len(dataset), 10) nose.tools.eq_(len(dataset), 10)
for sample in dataset: for sample in dataset:
nose.tools.eq_(len(sample), 3) nose.tools.eq_(len(sample), 3)
......
...@@ -10,8 +10,8 @@ import numpy ...@@ -10,8 +10,8 @@ import numpy
import nose.tools import nose.tools
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from . import dataset from ..data.drishtigs1 import dataset
from ...test.utils import rc_variable_set from .utils import rc_variable_set, count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -39,8 +39,6 @@ def test_protocol_consistency(): ...@@ -39,8 +39,6 @@ def test_protocol_consistency():
@attr("slow") @attr("slow")
def test_loading(): def test_loading():
from ..utils import count_bw
def _check_sample(s, bw_threshold_label): def _check_sample(s, bw_threshold_label):
data = s.data data = s.data
...@@ -77,8 +75,7 @@ def test_loading(): ...@@ -77,8 +75,7 @@ def test_loading():
# to visualize images, uncomment the folowing code # to visualize images, uncomment the folowing code
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels. # original data, blended with green labels.
#print(f"{s.key}: {data['data'].size}, w/b = {w/b:.3f}") #from ..data.utils import overlayed_image
#from ..utils import overlayed_image
#display = overlayed_image(data["data"], data["label"]) #display = overlayed_image(data["data"], data["label"])
#display.show() #display.show()
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
......
...@@ -9,8 +9,8 @@ import os ...@@ -9,8 +9,8 @@ import os
import numpy import numpy
import nose.tools import nose.tools
from . import dataset from ..data.drive import dataset
from ...test.utils import rc_variable_set from .utils import rc_variable_set, count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -40,7 +40,6 @@ def test_protocol_consistency(): ...@@ -40,7 +40,6 @@ def test_protocol_consistency():
@rc_variable_set('bob.ip.binseg.drive.datadir') @rc_variable_set('bob.ip.binseg.drive.datadir')
def test_loading(): def test_loading():
from ..utils import count_bw
image_size = (565, 584) image_size = (565, 584)
def _check_sample(s, bw_threshold_label, bw_threshold_mask): def _check_sample(s, bw_threshold_label, bw_threshold_mask):
...@@ -83,7 +82,7 @@ def test_loading(): ...@@ -83,7 +82,7 @@ def test_loading():
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels and blue area indicating the # original data, blended with green labels and blue area indicating the
# parts to be masked out. # parts to be masked out.
#from ..utils import overlayed_image #from ..data.utils import overlayed_image
#display = overlayed_image(data["data"], data["label"], data["mask"]) #display = overlayed_image(data["data"], data["label"], data["mask"])
#display.show() #display.show()
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
......
...@@ -9,8 +9,8 @@ import os ...@@ -9,8 +9,8 @@ import os
import numpy import numpy
import nose.tools import nose.tools
from . import dataset from ..data.hrf import dataset
from ...test.utils import rc_variable_set from .utils import rc_variable_set, count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -32,7 +32,6 @@ def test_protocol_consistency(): ...@@ -32,7 +32,6 @@ def test_protocol_consistency():
@rc_variable_set('bob.ip.binseg.hrf.datadir') @rc_variable_set('bob.ip.binseg.hrf.datadir')
def test_loading(): def test_loading():
from ..utils import count_bw
image_size = (3504, 2336) image_size = (3504, 2336)
def _check_sample(s, bw_threshold_label, bw_threshold_mask): def _check_sample(s, bw_threshold_label, bw_threshold_mask):
...@@ -75,7 +74,7 @@ def test_loading(): ...@@ -75,7 +74,7 @@ def test_loading():
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels and blue area indicating the # original data, blended with green labels and blue area indicating the
# parts to be masked out. # parts to be masked out.
#from ..utils import overlayed_image #from ..data.utils import overlayed_image
#display = overlayed_image(data["data"], data["label"], data["mask"]) #display = overlayed_image(data["data"], data["label"], data["mask"])
#display.show() #display.show()
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
......
...@@ -9,8 +9,8 @@ import os ...@@ -9,8 +9,8 @@ import os
import numpy import numpy
import nose.tools import nose.tools
from . import dataset from ..data.iostar import dataset
from ...test.utils import rc_variable_set from .utils import rc_variable_set, count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -45,7 +45,6 @@ def test_protocol_consistency(): ...@@ -45,7 +45,6 @@ def test_protocol_consistency():
@rc_variable_set('bob.ip.binseg.iostar.datadir') @rc_variable_set('bob.ip.binseg.iostar.datadir')
def test_loading(): def test_loading():
from ..utils import count_bw
image_size = (1024, 1024) image_size = (1024, 1024)
def _check_sample(s, bw_threshold_label, bw_threshold_mask): def _check_sample(s, bw_threshold_label, bw_threshold_mask):
...@@ -88,7 +87,7 @@ def test_loading(): ...@@ -88,7 +87,7 @@ def test_loading():
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels and blue area indicating the # original data, blended with green labels and blue area indicating the
# parts to be masked out. # parts to be masked out.
#from ..utils import overlayed_image #from ..data.utils import overlayed_image
#display = overlayed_image(data["data"], data["label"], data["mask"]) #display = overlayed_image(data["data"], data["label"], data["mask"])
#display.show() #display.show()
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
......
...@@ -10,8 +10,8 @@ import numpy ...@@ -10,8 +10,8 @@ import numpy
import nose.tools import nose.tools
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from . import dataset from ..data.refuge import dataset
from ...test.utils import rc_variable_set from .utils import rc_variable_set, count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -41,8 +41,6 @@ def test_protocol_consistency(): ...@@ -41,8 +41,6 @@ def test_protocol_consistency():
@attr("slow") @attr("slow")
def test_loading(): def test_loading():
from ..utils import count_bw
def _check_sample( def _check_sample(
s, image_size, glaucoma_label, entries, bw_threshold_label s, image_size, glaucoma_label, entries, bw_threshold_label
): ):
...@@ -76,11 +74,10 @@ def test_loading(): ...@@ -76,11 +74,10 @@ def test_loading():
# to visualize images, uncomment the folowing code # to visualize images, uncomment the folowing code
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels. # original data, blended with green labels.
# print(f"{s.key}: {data.get('glaucoma')}, w/b = {w/b:.3f}") #from ..data.utils import overlayed_image
# from ..utils import overlayed_image #display = overlayed_image(data["data"], data["label"])
# display = overlayed_image(data["data"], data["label"]) #display.show()
# display.show() #import ipdb; ipdb.set_trace()
# import ipdb; ipdb.set_trace()
return w/b return w/b
......
...@@ -10,8 +10,8 @@ import numpy ...@@ -10,8 +10,8 @@ import numpy
import nose.tools import nose.tools
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from . import dataset from ..data.rimoner3 import dataset
from ...test.utils import rc_variable_set from .utils import rc_variable_set, count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -37,7 +37,6 @@ def test_protocol_consistency(): ...@@ -37,7 +37,6 @@ def test_protocol_consistency():
@attr("slow") @attr("slow")
def test_loading(): def test_loading():
from ..utils import count_bw
image_size = (1072, 1424) image_size = (1072, 1424)
def _check_sample(s, bw_threshold_label): def _check_sample(s, bw_threshold_label):
...@@ -66,10 +65,10 @@ def test_loading(): ...@@ -66,10 +65,10 @@ def test_loading():
# to visualize images, uncomment the folowing code # to visualize images, uncomment the folowing code
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels. # original data, blended with green labels.
from ..utils import overlayed_image #from ..data.utils import overlayed_image
display = overlayed_image(data["data"], data["label"]) #display = overlayed_image(data["data"], data["label"])
display.show() #display.show()
import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
return w/b return w/b
......
...@@ -9,8 +9,11 @@ import os ...@@ -9,8 +9,11 @@ import os
import numpy import numpy
import nose.tools import nose.tools
from . import dataset ## special trick for CI builds
from ...test.utils import rc_variable_set from . import mock_dataset
dataset, rc_variable_set = mock_dataset()
from .utils import count_bw
def test_protocol_consistency(): def test_protocol_consistency():
...@@ -45,7 +48,6 @@ def test_protocol_consistency(): ...@@ -45,7 +48,6 @@ def test_protocol_consistency():
@rc_variable_set('bob.ip.binseg.stare.datadir') @rc_variable_set('bob.ip.binseg.stare.datadir')
def test_loading(): def test_loading():
from ..utils import count_bw
image_size = (700, 605) image_size = (700, 605)
def _check_sample(s, bw_threshold_label): def _check_sample(s, bw_threshold_label):
...@@ -74,7 +76,7 @@ def test_loading(): ...@@ -74,7 +76,7 @@ def test_loading():
# to visualize images, uncomment the folowing code # to visualize images, uncomment the folowing code
# it should display an image with a faded background representing the # it should display an image with a faded background representing the
# original data, blended with green labels. # original data, blended with green labels.
#from ..utils import overlayed_image #from ..data.utils import overlayed_image
#display = overlayed_image(data["data"], data["label"]) #display = overlayed_image(data["data"], data["label"])
#display.show() #display.show()
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
......
...@@ -6,7 +6,10 @@ ...@@ -6,7 +6,10 @@
import functools import functools
import numpy
import nose.plugins.skip import nose.plugins.skip
import bob.extension import bob.extension
...@@ -25,3 +28,29 @@ def rc_variable_set(name): ...@@ -25,3 +28,29 @@ def rc_variable_set(name):
return wrapper return wrapper
return wrapped_function return wrapped_function
def count_bw(b):
"""Calculates totals of black and white pixels in a binary image
Parameters
----------
b : PIL.Image.Image
A PIL image in mode "1" to be used for calculating positives and
negatives
Returns
-------
black : int
Number of black pixels in the binary image
white : int
Number of white pixels in the binary image
"""
boolean_array = numpy.array(b)
white = boolean_array.sum()
return (boolean_array.size-white), white
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment