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

[test.test_cli] Test more variants; Use regexp for string matching everywhere

parent 97bc8179
No related branches found
No related tags found
1 merge request!12Streamlining
Pipeline #39123 passed
...@@ -64,11 +64,10 @@ def test_experiment_help(): ...@@ -64,11 +64,10 @@ def test_experiment_help():
def _str_counter(substr, s): def _str_counter(substr, s):
return sum(1 for _ in re.finditer(r"%s" % re.escape(substr), s)) return sum(1 for _ in re.finditer(substr, s, re.MULTILINE))
@rc_variable_set("bob.ip.binseg.stare.datadir") def _check_experiment_stare(overlay):
def test_experiment_stare():
from ..script.experiment import experiment from ..script.experiment import experiment
...@@ -88,18 +87,17 @@ def test_experiment_stare(): ...@@ -88,18 +87,17 @@ def test_experiment_stare():
config.flush() config.flush()
output_folder = "results" output_folder = "results"
result = runner.invoke( options = [
experiment,
[
"m2unet", "m2unet",
config.name, config.name,
"-vv", "-vv",
"--epochs=1", "--epochs=1",
"--batch-size=1", "--batch-size=1",
"--overlayed",
f"--output-folder={output_folder}", f"--output-folder={output_folder}",
], ]
) if overlay:
options += ["--overlayed"]
result = runner.invoke(experiment, options)
_assert_exit_0(result) _assert_exit_0(result)
# check command-line # check command-line
...@@ -118,55 +116,68 @@ def test_experiment_stare(): ...@@ -118,55 +116,68 @@ def test_experiment_stare():
assert os.path.exists(basedir) assert os.path.exists(basedir)
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.hdf5")), 20) nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.hdf5")), 20)
# check overlayed images are there (since we requested them)
overlay_folder = os.path.join(output_folder, "overlayed", "predictions") overlay_folder = os.path.join(output_folder, "overlayed", "predictions")
basedir = os.path.join(overlay_folder, "stare-images") basedir = os.path.join(overlay_folder, "stare-images")
assert os.path.exists(basedir) if overlay:
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 20) # check overlayed images are there (since we requested them)
assert os.path.exists(basedir)
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 20)
else:
assert not os.path.exists(basedir)
# check evaluation outputs # check evaluation outputs
eval_folder = os.path.join(output_folder, "analysis") eval_folder = os.path.join(output_folder, "analysis")
second_folder = os.path.join(eval_folder, "second-annotator") second_folder = os.path.join(eval_folder, "second-annotator")
assert os.path.exists(os.path.join(eval_folder, "train", "metrics.csv")) assert os.path.exists(os.path.join(eval_folder, "train", "metrics.csv"))
assert os.path.exists(os.path.join(eval_folder, "test", "metrics.csv")) assert os.path.exists(os.path.join(eval_folder, "test", "metrics.csv"))
assert os.path.exists(os.path.join(second_folder, "train", "metrics.csv")) assert os.path.exists(
assert os.path.exists(os.path.join(second_folder, "test", "metrics.csv")) os.path.join(second_folder, "train", "metrics.csv")
)
assert os.path.exists(
os.path.join(second_folder, "test", "metrics.csv")
)
# check overlayed images are there (since we requested them)
overlay_folder = os.path.join(output_folder, "overlayed", "analysis") overlay_folder = os.path.join(output_folder, "overlayed", "analysis")
basedir = os.path.join(overlay_folder, "stare-images") basedir = os.path.join(overlay_folder, "stare-images")
assert os.path.exists(basedir) if overlay:
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 20) # check overlayed images are there (since we requested them)
assert os.path.exists(basedir)
# check overlayed images from first-to-second annotator comparisons are nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 20)
# there (since we requested them) else:
assert not os.path.exists(basedir)
# check overlayed images from first-to-second annotator comparisons
# are there (since we requested them)
overlay_folder = os.path.join(output_folder, "overlayed", "analysis", overlay_folder = os.path.join(output_folder, "overlayed", "analysis",
"second-annotator") "second-annotator")
basedir = os.path.join(overlay_folder, "stare-images") basedir = os.path.join(overlay_folder, "stare-images")
assert os.path.exists(basedir) if overlay:
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 20) assert os.path.exists(basedir)
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 20)
else:
assert not os.path.exists(basedir)
# check outcomes of the comparison phase # check outcomes of the comparison phase
assert os.path.exists(os.path.join(output_folder, "comparison.pdf")) assert os.path.exists(os.path.join(output_folder, "comparison.pdf"))
keywords = { # from different logging systems keywords = {
"Started training": 1, # logging r"^Started training$": 1,
"Found (dedicated) '__train__' set for training": 1, # logging r"^Found \(dedicated\) '__train__' set for training$": 1,
"epoch: 1|total-time": 1, # logging r"^epoch: \d+\|total-time": 1,
"Saving checkpoint": 1, # logging r"^Saving checkpoint": 1,
"Ended training": 1, # logging r"^Ended training$": 1,
"Started prediction": 1, # logging r"^Started prediction$": 1,
"Loading checkpoint from": 2, # logging r"^Loading checkpoint from": 2,
# "Saving results/overlayed/probabilities": 1, #tqdm.write r"^Ended prediction$": 1,
"Ended prediction": 1, # logging r"^Started evaluation$": 1,
"Started evaluation": 1, # logging r"^Maximum F1-score of.*\(chosen \*a posteriori\*\)$": 3,
"Highest F1-score of": 4, # logging r"^F1-score of.*\(chosen \*a priori\*\)$": 2,
"Saving overall precision-recall plot": 2, # logging r"^Maximum F1-score of .* \(second annotator\)$": 2,
# "Saving results/overlayed/analysis": 1, #tqdm.write r"^Saving overall precision-recall plot at .*$": 2,
"Ended evaluation": 1, # logging r"^Ended evaluation$": 1,
"Started comparison": 1, # logging r"^Started comparison$": 1,
"Loading metrics from": 4, # logging r"^Loading metrics from": 4,
"Ended comparison": 1, # logging r"^Ended comparison.*$": 1,
} }
buf.seek(0) buf.seek(0)
logging_output = buf.read() logging_output = buf.read()
...@@ -182,6 +193,16 @@ def test_experiment_stare(): ...@@ -182,6 +193,16 @@ def test_experiment_stare():
) )
@rc_variable_set("bob.ip.binseg.stare.datadir")
def test_experiment_stare_with_overlay():
_check_experiment_stare(overlay=True)
@rc_variable_set("bob.ip.binseg.stare.datadir")
def test_experiment_stare_without_overlay():
_check_experiment_stare(overlay=False)
def _check_train(runner): def _check_train(runner):
from ..script.train import train from ..script.train import train
...@@ -202,8 +223,14 @@ def _check_train(runner): ...@@ -202,8 +223,14 @@ def _check_train(runner):
output_folder = "results" output_folder = "results"
result = runner.invoke( result = runner.invoke(
train, train,
["m2unet", config.name, "-vv", "--epochs=1", "--batch-size=1", [
f"--output-folder={output_folder}"], "m2unet",
config.name,
"-vv",
"--epochs=1",
"--batch-size=1",
f"--output-folder={output_folder}",
],
) )
_assert_exit_0(result) _assert_exit_0(result)
...@@ -211,11 +238,11 @@ def _check_train(runner): ...@@ -211,11 +238,11 @@ def _check_train(runner):
assert os.path.exists(os.path.join(output_folder, "last_checkpoint")) assert os.path.exists(os.path.join(output_folder, "last_checkpoint"))
assert os.path.exists(os.path.join(output_folder, "trainlog.csv")) assert os.path.exists(os.path.join(output_folder, "trainlog.csv"))
keywords = { # from different logging systems keywords = {
"Continuing from epoch 0": 1, # logging r"^Continuing from epoch 0$": 1,
"epoch: 1|total-time": 1, # logging r"^epoch: \d+\|total-time": 1,
f"Saving checkpoint to {output_folder}/model_final.pth": 1, # logging rf"^Saving checkpoint to {output_folder}/model_final.pth$": 1,
"Total training time:": 1, # logging r"^Total training time:": 1,
} }
buf.seek(0) buf.seek(0)
logging_output = buf.read() logging_output = buf.read()
...@@ -276,9 +303,9 @@ def _check_predict(runner): ...@@ -276,9 +303,9 @@ def _check_predict(runner):
assert os.path.exists(basedir) assert os.path.exists(basedir)
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 10) nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 10)
keywords = { # from different logging systems keywords = {
"Loading checkpoint from": 1, # logging r"^Loading checkpoint from.*$": 1,
"Total time:": 1, # logging r"^Total time:.*$": 1,
} }
buf.seek(0) buf.seek(0)
logging_output = buf.read() logging_output = buf.read()
...@@ -337,10 +364,12 @@ def _check_evaluate(runner): ...@@ -337,10 +364,12 @@ def _check_evaluate(runner):
assert os.path.exists(basedir) assert os.path.exists(basedir)
nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 10) nose.tools.eq_(len(fnmatch.filter(os.listdir(basedir), "*.png")), 10)
keywords = { # from different logging systems keywords = {
"Skipping dataset '__train__'": 0, # logging r"^Skipping dataset '__train__'": 0,
"Saving averages over all input images": 2, # logging r"^Saving averages over all input images.*$": 2,
"Highest F1-score": 2, # logging r"^Maximum F1-score of.*\(chosen \*a posteriori\*\)$": 1,
r"^F1-score of.*\(chosen \*a priori\*\)$": 1,
r"^Maximum F1-score of .* \(second annotator\)$": 1,
} }
buf.seek(0) buf.seek(0)
logging_output = buf.read() logging_output = buf.read()
...@@ -370,16 +399,18 @@ def _check_compare(runner): ...@@ -370,16 +399,18 @@ def _check_compare(runner):
[ [
"-vv", "-vv",
# label - path to metrics # label - path to metrics
"test", os.path.join(output_folder, "metrics.csv"), "test",
"test (2nd. human)", os.path.join(second_folder, "metrics.csv"), os.path.join(output_folder, "metrics.csv"),
"test (2nd. human)",
os.path.join(second_folder, "metrics.csv"),
], ],
) )
_assert_exit_0(result) _assert_exit_0(result)
assert os.path.exists("comparison.pdf") assert os.path.exists("comparison.pdf")
keywords = { # from different logging systems keywords = {
"Loading metrics from": 2, # logging r"^Loading metrics from": 2,
} }
buf.seek(0) buf.seek(0)
logging_output = buf.read() logging_output = buf.read()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment