Skip to content

Commit

Permalink
Fixes (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Zhiltsov authored Sep 10, 2020
1 parent fd67751 commit f72848b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
13 changes: 9 additions & 4 deletions datumaro/cli/contexts/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ def filter_command(args):
if not args.filter:
raise CliException("Expected a filter expression ('-e' argument)")

dataset.filter_project(save_dir=dst_dir, expr=args.filter, **filter_args)
dataset.filter_project(save_dir=dst_dir,
filter_expr=args.filter, **filter_args)

log.info("Subproject has been extracted to '%s'" % dst_dir)

Expand Down Expand Up @@ -565,6 +566,8 @@ def diff_command(args):

return 0

_ediff_default_if = ['id', 'group'] # avoid https://bugs.python.org/issue16399

def build_ediff_parser(parser_ctor=argparse.ArgumentParser):
parser = parser_ctor(help="Compare projects for equality",
description="""
Expand All @@ -583,9 +586,9 @@ def build_ediff_parser(parser_ctor=argparse.ArgumentParser):
help="Ignore item attribute (repeatable)")
parser.add_argument('-ia', '--ignore-attr', action='append',
help="Ignore annotation attribute (repeatable)")
parser.add_argument('-if', '--ignore-field',
action='append', default=['id', 'group'],
help="Ignore annotation field (repeatable, default: %(default)s)")
parser.add_argument('-if', '--ignore-field', action='append',
help="Ignore annotation field (repeatable, default: %s)" % \
_ediff_default_if)
parser.add_argument('--match-images', action='store_true',
help='Match dataset items by images instead of ids')
parser.add_argument('--all', action='store_true',
Expand All @@ -600,6 +603,8 @@ def ediff_command(args):
first_project = load_project(args.project_dir)
second_project = load_project(args.other_project_dir)

if args.ignore_field:
args.ignore_field = _ediff_default_if
comparator = ExactComparator(
match_images=args.match_images,
ignored_fields=args.ignore_field,
Expand Down
2 changes: 1 addition & 1 deletion datumaro/plugins/image_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def __init__(self, url):
for dirpath, _, filenames in os.walk(url):
for name in filenames:
path = osp.join(dirpath, name)
image = Image(path=path)
try:
image = Image(path)
# force loading
image.data # pylint: disable=pointless-statement
except Exception:
Expand Down
10 changes: 8 additions & 2 deletions datumaro/plugins/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ class RandomSplit(Transform, CliPlugin):
|s|s%(prog)s --subset train:.67 --subset test:.33
"""

# avoid https://bugs.python.org/issue16399
_default_split = [('train', 0.67), ('test', 0.33)]

@staticmethod
def _split_arg(s):
parts = s.split(':')
Expand All @@ -321,14 +324,17 @@ def build_cmdline_parser(cls, **kwargs):
parser = super().build_cmdline_parser(**kwargs)
parser.add_argument('-s', '--subset', action='append',
type=cls._split_arg, dest='splits',
default=[('train', 0.67), ('test', 0.33)],
help="Subsets in the form of: '<subset>:<ratio>' (repeatable)")
help="Subsets in the form: '<subset>:<ratio>' "
"(repeatable, default: %s)" % dict(cls._default_split))
parser.add_argument('--seed', type=int, help="Random seed")
return parser

def __init__(self, extractor, splits, seed=None):
super().__init__(extractor)

if splits is None:
splits = self._default_split

assert 0 < len(splits), "Expected at least one split"
assert all(0.0 <= r and r <= 1.0 for _, r in splits), \
"Ratios are expected to be in the range [0; 1], but got %s" % splits
Expand Down
4 changes: 4 additions & 0 deletions datumaro/util/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def load_image(path):
else:
raise NotImplementedError()

if image is None:
raise ValueError("Can't open image '%s'" % path)
assert len(image.shape) in {2, 3}
if len(image.shape) == 3:
assert image.shape[2] in {3, 4}
Expand Down Expand Up @@ -206,6 +208,8 @@ def __init__(self, data=None, path=None, loader=None, cache=None,
self._path = path

assert data is not None or path or loader, "Image can not be empty"
if data is not None:
assert callable(data) or isinstance(data, np.ndarray), type(data)
if data is None and (path or loader):
if osp.isfile(path) or loader:
data = lazy_image(path, loader=loader, cache=cache)
Expand Down

0 comments on commit f72848b

Please sign in to comment.