Skip to content
Snippets Groups Projects
Commit e1c15480 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

improve error handling

parent 2ec484bb
No related branches found
No related tags found
1 merge request!135Make missing annotations None instead of empty dict
Pipeline #
...@@ -26,7 +26,7 @@ class FailSafe(Annotator): ...@@ -26,7 +26,7 @@ class FailSafe(Annotator):
def annotate(self, sample, **kwargs): def annotate(self, sample, **kwargs):
if 'annotations' not in kwargs or kwargs['annotations'] is None: if 'annotations' not in kwargs or kwargs['annotations'] is None:
kwargs['annotations'] = None kwargs['annotations'] = {}
for annotator in self.annotators: for annotator in self.annotators:
try: try:
annotations = annotator(sample, **kwargs) annotations = annotator(sample, **kwargs)
......
...@@ -77,9 +77,9 @@ def annotate(database, annotator, output_dir, force, array, **kwargs): ...@@ -77,9 +77,9 @@ def annotate(database, annotator, output_dir, force, array, **kwargs):
outpath = biofile.make_path(output_dir, '.json') outpath = biofile.make_path(output_dir, '.json')
if isfile(outpath): if isfile(outpath):
if force: if force:
logger.debug("Overwriting the annotations file `%s'", outpath) logger.info("Overwriting the annotations file `%s'", outpath)
else: else:
logger.debug("The annotation `%s' already exists", outpath) logger.info("The annotation `%s' already exists", outpath)
continue continue
logger.info( logger.info(
......
from random import random
from bob.bio.base.annotator import FailSafe, Callable from bob.bio.base.annotator import FailSafe, Callable
def simple_annotator(image, **kwargs): def moody_annotator(image, **kwargs):
return { if random() < 0.5:
'topleft': (0, 0), return {
'bottomright': image.shape, 'topleft': (0, 0),
} }
else:
return {
'topleft': (0, 0),
'bottomright': image.shape,
}
def fail_annotator(image, **kwargs): def fail_annotator(image, **kwargs):
...@@ -14,6 +20,6 @@ def fail_annotator(image, **kwargs): ...@@ -14,6 +20,6 @@ def fail_annotator(image, **kwargs):
annotator = FailSafe( annotator = FailSafe(
[Callable(fail_annotator), [Callable(fail_annotator),
Callable(simple_annotator)], Callable(moody_annotator)],
required_keys=['topleft', 'bottomright'], required_keys=['topleft', 'bottomright'],
) )
...@@ -13,7 +13,13 @@ def test_annotate(): ...@@ -13,7 +13,13 @@ def test_annotate():
runner = CliRunner() runner = CliRunner()
result = runner.invoke(annotate, args=( result = runner.invoke(annotate, args=(
'-d', 'dummy', '-a', 'dummy', '-o', tmp_dir)) '-d', 'dummy', '-a', 'dummy', '-o', tmp_dir))
assert result.exit_code == 0, result.output assertion_error_message = (
'Command exited with this output: `{}\' \n'
'If the output is empty, you can run this script locally to see '
'what is wrong:\n'
'bin/bob bio annotate -vvv --force -d dummy -a dummy -o /tmp/temp_annotations'
''.format(result.output))
assert result.exit_code == 0, assertion_error_message
# test if annotations exist # test if annotations exist
for dirpath, dirnames, filenames in os.walk(tmp_dir): for dirpath, dirnames, filenames in os.walk(tmp_dir):
......
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