Skip to content

Commit

Permalink
Merge pull request #585 from grigorisg9gr/ln_importer_hotfix
Browse files Browse the repository at this point in the history
Hotfix: suffix change led to double path resolution.
  • Loading branch information
jabooth committed May 21, 2015
2 parents bbf95a6 + d90d771 commit e1d1069
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
7 changes: 3 additions & 4 deletions menpo/io/input/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,9 @@ def glob_with_suffix(pattern, extensions_map):
# we want to extract '.pkl.gz' as an extension - for this we need to
# use suffixes and join.
# .suffix only takes
if ''.join(path.suffixes) in extensions_map:
yield path
# try again in case the filename has a '.' in it, this time only with the suffix
if path.suffix in extensions_map:
# However, the filename might have a '.' in it, e.g. '1.1.png'.
# In this case, try again with just the suffix.
if ''.join(path.suffixes[-2:]) in extensions_map or path.suffix in extensions_map:
yield path


Expand Down
56 changes: 52 additions & 4 deletions menpo/io/test/io_import_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import numpy as np
from mock import patch
from mock import patch, MagicMock
from nose.tools import raises
from PIL import Image as PILImage
import menpo.io as mio
Expand Down Expand Up @@ -64,6 +64,45 @@ def test_path():
assert(img.path.name == 'einstein.jpg')


@patch('menpo.io.input.base._pathlib_glob_for_pattern')
def test_single_suffix_dot_in_path(pathlib_glob):
import menpo.io.input.base as mio_base
from pathlib import Path

fake_path = Path('fake_path.t0.t1.t2')
pathlib_glob.return_value = [fake_path]
ext_map = MagicMock()
ext_map.__contains__.side_effect = lambda x: x == '.t2'

ret_val = next(mio_base.glob_with_suffix('*.t0.t1.t2', ext_map))
assert (ret_val == fake_path)
ext_map.__contains__.assert_called_with('.t2')


def test_upper_extension_mapped_to_lower():
import menpo.io.input.base as mio_base
from pathlib import Path
ext_map = MagicMock()

mio_base.importer_for_filepath(Path('fake_path.JPG'), ext_map)
ext_map.get.assert_called_with('.jpg')


@patch('menpo.io.input.base._pathlib_glob_for_pattern')
def test_double_suffix(pathlib_glob):
import menpo.io.input.base as mio_base
from pathlib import Path

fake_path = Path('fake_path.t1.t2')
pathlib_glob.return_value = [fake_path]
ext_map = MagicMock()
ext_map.__contains__.side_effect = lambda x: x == '.t1.t2'

ret_val = next(mio_base.glob_with_suffix('*.t1.t2', ext_map))
assert (ret_val == fake_path)
ext_map.__contains__.assert_called_with('.t1.t2')


def test_import_image():
img_path = mio.data_dir_path() / 'einstein.jpg'
im = mio.import_image(img_path)
Expand All @@ -85,12 +124,21 @@ def test_import_landmark_file():
def test_import_images():
imgs = list(mio.import_images(mio.data_dir_path()))
imgs_filenames = set(i.path.stem for i in imgs)
exp_imgs_filenames = {'einstein', 'takeo', 'breakingbad', 'lenna',
exp_imgs_filenames = {'einstein', 'takeo', 'tongue', 'breakingbad', 'lenna',
'menpo_thumbnail'}
assert(len(exp_imgs_filenames - imgs_filenames) == 0)
assert exp_imgs_filenames == imgs_filenames


def test_import_images_are_ordered_and_unduplicated():
# we know that import_images returns images in path order
imgs = list(mio.import_images(mio.data_dir_path()))
imgs_filenames = [i.path.stem for i in imgs]
print(imgs_filenames)
exp_imgs_filenames = ['breakingbad', 'einstein', 'lenna', 'menpo_thumbnail', 'takeo', 'tongue']
assert exp_imgs_filenames == imgs_filenames


def test_ls_builtin_assets():
def test_lsimgs_filenamess():
assert(set(mio.ls_builtin_assets()) == {'breakingbad.jpg',
'einstein.jpg', 'einstein.pts',
'lenna.png', 'breakingbad.pts',
Expand Down

0 comments on commit e1d1069

Please sign in to comment.