Skip to content

Commit b38f40d

Browse files
committed
Fix register kwarg in chained expected datasets (issue #972)
1 parent bb2ceaa commit b38f40d

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

ibllib/oneibl/data_handlers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def find_files(self, session_path, register=False):
143143
- Currently if `unique` is true and multiple files are found, all files are returned without an exception raised
144144
although this may change in the future.
145145
- If `register` is false, all files are returned regardless of whether they are intended to be registered.
146+
- If `register` is true, an input with register=True may not be returned if part of an OR operation.
146147
- If `inverted` is true, and files are found, the glob pattern is returned as missing.
147148
- If XOR, returns all patterns if all are present when only one should be, otherwise returns all missing
148149
patterns.
@@ -163,22 +164,22 @@ def find_files(self, session_path, register=False):
163164
missing = self.glob_pattern
164165
elif self.operator == 'and':
165166
assert len(self._identifiers) == 2
166-
_ok, _actual_files, _missing = zip(*map(lambda x: x.find_files(session_path), self._identifiers))
167+
_ok, _actual_files, _missing = zip(*map(lambda x: x.find_files(session_path, register=register), self._identifiers))
167168
ok = all(_ok)
168169
actual_files = flatten(_actual_files)
169170
missing = set(filter(None, flatten(_missing)))
170171
elif self.operator == 'or':
171172
assert len(self._identifiers) == 2
172173
missing = set()
173174
for d in self._identifiers:
174-
ok, actual_files, _missing = d.find_files(session_path)
175+
ok, actual_files, _missing = d.find_files(session_path, register=register)
175176
if ok:
176177
break
177178
if missing is not None:
178179
missing.update(_missing) if isinstance(_missing, set) else missing.add(_missing)
179180
elif self.operator == 'xor':
180181
assert len(self._identifiers) == 2
181-
_ok, _actual_files, _missing = zip(*map(lambda x: x.find_files(session_path), self._identifiers))
182+
_ok, _actual_files, _missing = zip(*map(lambda x: x.find_files(session_path, register=register), self._identifiers))
182183
ok = sum(_ok) == 1 # and sum(map(bool, map(len, _actual_files))) == 1
183184
# Return only those datasets that are complete if OK
184185
actual_files = _actual_files[_ok.index(True)] if ok else flatten(_actual_files)

ibllib/oneibl/registration.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from iblutil.util import ensure_list
1717

1818
import ibllib
19-
import ibllib.io.extractors.base
2019
from ibllib.time import isostr2date
2120
import ibllib.io.raw_data_loaders as raw
2221
from ibllib.io import session_params

ibllib/tests/test_oneibl.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ def setUp(self):
854854
self.img_path.joinpath('imaging.frames.tar.bz2').touch()
855855

856856
def test_and(self):
857+
"""Test for ExpectedDataset input with AND operator."""
857858
I = handlers.ExpectedDataset.input # noqa
858859
sig = I('*.tif', 'raw_imaging_data_[0-9]*') & I('imaging.frames.tar.bz2', 'raw_imaging_data_[0-9]*')
859860
self.assertEqual('and', sig.operator)
@@ -863,6 +864,16 @@ def test_and(self):
863864
self.assertEqual('foo_00000.tif', files[0].name)
864865
self.assertEqual('imaging.frames.tar.bz2', files[-1].name)
865866
self.assertEqual(set(), missing)
867+
# Check with register arg
868+
ok, _files, missing = sig.find_files(self.session_path, register=True)
869+
self.assertTrue(ok)
870+
self.assertEqual([], _files)
871+
self.assertEqual(set(), missing)
872+
sig._identifiers[1].register = True
873+
ok, _files, missing = sig.find_files(self.session_path, register=True)
874+
self.assertTrue(ok)
875+
self.assertEqual(1, len(_files))
876+
self.assertEqual('imaging.frames.tar.bz2', _files[0].name)
866877
# Deleting one tif file shouldn't affect the signature
867878
files[0].unlink()
868879
ok, files, missing = sig.find_files(self.session_path)
@@ -878,6 +889,7 @@ def test_and(self):
878889
self.assertEqual({'raw_imaging_data_[0-9]*/imaging.frames.tar.bz2'}, missing)
879890

880891
def test_or(self):
892+
"""Test for ExpectedDataset input with OR operator."""
881893
I = handlers.ExpectedDataset.input # noqa
882894
sig = I('*.tif', 'raw_imaging_data_[0-9]*') | I('imaging.frames.tar.bz2', 'raw_imaging_data_[0-9]*')
883895
self.assertEqual('or', sig.operator)
@@ -886,6 +898,17 @@ def test_or(self):
886898
self.assertEqual(5, len(files))
887899
self.assertEqual({'.tif'}, set(x.suffix for x in files))
888900
self.assertEqual(set(), missing)
901+
# Test with register filter
902+
ok, _files, missing = sig.find_files(self.session_path, register=True)
903+
self.assertTrue(ok)
904+
self.assertEqual([], _files)
905+
self.assertEqual(set(), missing)
906+
sig._identifiers[1].register = True
907+
ok, _files, missing = sig.find_files(self.session_path, register=True)
908+
self.assertTrue(ok)
909+
# NB: Although 2nd file has register=True, the first is present so nothing is returned here
910+
self.assertEqual([], _files)
911+
self.assertEqual(set(), missing)
889912
for f in files:
890913
f.unlink()
891914
ok, files, missing = sig.find_files(self.session_path)
@@ -900,6 +923,7 @@ def test_or(self):
900923
self.assertEqual({'raw_imaging_data_[0-9]*/*.tif', 'raw_imaging_data_[0-9]*/imaging.frames.tar.bz2'}, missing)
901924

902925
def test_xor(self):
926+
"""Test for ExpectedDataset input with XOR operator."""
903927
I = handlers.ExpectedDataset.input # noqa
904928
sig = I('*.tif', 'raw_imaging_data_[0-9]*') ^ I('imaging.frames.tar.bz2', 'raw_imaging_data_[0-9]*')
905929
self.assertEqual('xor', sig.operator)
@@ -909,6 +933,17 @@ def test_xor(self):
909933
self.assertEqual({'.tif', '.bz2'}, set(x.suffix for x in files))
910934
expected_missing = {'raw_imaging_data_[0-9]*/*.tif', 'raw_imaging_data_[0-9]*/imaging.frames.tar.bz2'}
911935
self.assertEqual(expected_missing, missing)
936+
# Test register kwarg
937+
ok, _files, missing = sig.find_files(self.session_path, register=True)
938+
self.assertFalse(ok)
939+
self.assertEqual([], _files)
940+
self.assertEqual(expected_missing, missing)
941+
sig._identifiers[1].register = True
942+
ok, _files, _ = sig.find_files(self.session_path, register=True)
943+
self.assertFalse(ok)
944+
self.assertEqual(1, len(_files))
945+
self.assertEqual('imaging.frames.tar.bz2', _files[0].name)
946+
# Remove first expected dataset(s)
912947
for f in filter(lambda x: x.suffix == '.tif', files):
913948
f.unlink()
914949
ok, files, missing = sig.find_files(self.session_path)

0 commit comments

Comments
 (0)