Skip to content

Commit

Permalink
Merge pull request #81 from grabbles/fix/int
Browse files Browse the repository at this point in the history
Fix natural sort for non-strings
  • Loading branch information
tyarkoni authored Aug 21, 2018
2 parents 9ac2624 + ce0e7a6 commit a1938e9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
historical/
#
#
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -92,3 +92,5 @@ ENV/

*.DS_Store
*.orig

.pytest_cache
12 changes: 7 additions & 5 deletions grabbit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ def match_file(self, f, update_file=False):
m = self.regex.search(f.path)
val = m.group(1) if m is not None else None

if val is not None and self.dtype is not None:
val = self.dtype(val)

return val
return self._astype(val)

def add_file(self, filename, value):
""" Adds the specified filename to tracking. """
Expand All @@ -296,6 +293,11 @@ def count(self, files=False):
"""
return len(self.files) if files else len(self.unique())

def _astype(self, val):
if val is not None and self.dtype is not None:
val = self.dtype(val)
return val


class Layout(object):

Expand Down Expand Up @@ -860,7 +862,7 @@ def get_nearest(self, path, return_type='file', strict=True, all_=False,
for ent in self.entities.values():
m = ent.regex.search(path)
if m:
entities[ent.name] = m.group(1)
entities[ent.name] = ent._astype(m.group(1))

# Remove any entities we want to ignore when strict matching is on
if strict and ignore_strict_entities is not None:
Expand Down
7 changes: 7 additions & 0 deletions grabbit/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def test_dynamic_getters(self, data_dir, config):
layout = Layout([(data_dir, config)], dynamic_getters=True)
assert hasattr(layout, 'get_subjects')
assert '01' in getattr(layout, 'get_subjects')()
assert 1 in getattr(layout, 'get_runs')()

def test_querying(self, bids_layout):

Expand Down Expand Up @@ -312,6 +313,12 @@ def test_get_nearest(self, bids_layout):
assert len(nearest) == 3
assert nearest[0].subject == '01'

# Check for file with matching run (fails if types don't match)
nearest = bids_layout.get_nearest(
result, type='phasediff', extensions='.nii.gz')
assert nearest is not None
assert os.path.basename(nearest) == 'sub-01_ses-1_run-1_phasediff.nii.gz'

def test_index_regex(self, bids_layout, layout_include):
targ = join('derivatives', 'excluded.json')
assert targ not in bids_layout.files
Expand Down
2 changes: 2 additions & 0 deletions grabbit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def natural_sort(l, field=None):
def alphanum_key(key):
if field is not None:
key = getattr(key, field)
if not isinstance(key, str):
key = str(key)
return [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)

Expand Down

0 comments on commit a1938e9

Please sign in to comment.