1313
1414from bids .layout import BIDSLayout , Query
1515from bids .layout .models import Config
16- from bids .layout .index import BIDSLayoutIndexer
16+ from bids .layout .index import BIDSLayoutIndexer , _check_path_matches_patterns
1717from bids .layout .utils import PaddedInt
1818from bids .tests import get_test_data_path
1919from bids .utils import natural_sort
@@ -43,7 +43,11 @@ def test_layout_init(layout_7t_trt):
4343 ])
4444def test_index_metadata (index_metadata , query , result , mock_config ):
4545 data_dir = join (get_test_data_path (), '7t_trt' )
46- layout = BIDSLayout (data_dir , index_metadata = index_metadata , ** query )
46+ layout = BIDSLayout (
47+ data_dir ,
48+ indexer = BIDSLayoutIndexer (index_metadata = index_metadata ),
49+ ** query
50+ )
4751 sample_file = layout .get (task = 'rest' , extension = '.nii.gz' ,
4852 acquisition = 'fullbrain' )[0 ]
4953 metadata = sample_file .get_metadata ()
@@ -425,22 +429,39 @@ def test_nested_include_exclude():
425429 target1 = join (data_dir , 'models' , 'ds-005_type-test_model.json' )
426430 target2 = join (data_dir , 'models' , 'extras' , 'ds-005_type-test_model.json' )
427431
428- # Nest a directory exclusion within an inclusion
429- layout = BIDSLayout (data_dir , validate = True , force_index = ['models' ],
430- ignore = [os .path .join ('models' , 'extras' )])
431- assert layout .get_file (target1 )
432- assert not layout .get_file (target2 )
433-
434432 # Nest a directory inclusion within an exclusion
435- layout = BIDSLayout (data_dir , validate = True , ignore = ['models' ],
436- force_index = [os .path .join ('models' , 'extras' )])
433+ layout = BIDSLayout (
434+ data_dir ,
435+ indexer = BIDSLayoutIndexer (
436+ validate = True ,
437+ force_index = [os .path .join ('models' , 'extras' )],
438+ ignore = ['models' ],
439+ ),
440+ )
437441 assert not layout .get_file (target1 )
438442 assert layout .get_file (target2 )
439443
444+ # Nest a directory exclusion within an inclusion
445+ layout = BIDSLayout (
446+ data_dir ,
447+ indexer = BIDSLayoutIndexer (
448+ validate = True ,
449+ force_index = ['models' ],
450+ ignore = [os .path .join ('models' , 'extras' )],
451+ ),
452+ )
453+ assert layout .get_file (target1 )
454+ assert not layout .get_file (target2 )
455+
440456 # Force file inclusion despite directory-level exclusion
441- models = ['models' , target2 ]
442- layout = BIDSLayout (data_dir , validate = True , force_index = models ,
443- ignore = [os .path .join ('models' , 'extras' )])
457+ layout = BIDSLayout (
458+ data_dir ,
459+ indexer = BIDSLayoutIndexer (
460+ validate = True ,
461+ force_index = ['models' , target2 ],
462+ ignore = [os .path .join ('models' , 'extras' )],
463+ ),
464+ )
444465 assert layout .get_file (target1 )
445466 assert layout .get_file (target2 )
446467
@@ -453,11 +474,17 @@ def test_nested_include_exclude_with_regex():
453474 target1 = join (data_dir , 'models' , 'ds-005_type-test_model.json' )
454475 target2 = join (data_dir , 'models' , 'extras' , 'ds-005_type-test_model.json' )
455476
456- layout = BIDSLayout (data_dir , ignore = [patt2 ], force_index = [patt1 ])
477+ layout = BIDSLayout (
478+ data_dir ,
479+ indexer = BIDSLayoutIndexer (ignore = [patt2 ], force_index = [patt1 ])
480+ )
457481 assert layout .get_file (target1 )
458482 assert not layout .get_file (target2 )
459483
460- layout = BIDSLayout (data_dir , ignore = [patt1 ], force_index = [patt2 ])
484+ layout = BIDSLayout (
485+ data_dir ,
486+ indexer = BIDSLayoutIndexer (ignore = [patt1 ], force_index = [patt2 ])
487+ )
461488 assert not layout .get_file (target1 )
462489 assert layout .get_file (target2 )
463490
@@ -835,3 +862,33 @@ def test_padded_run_roundtrip(layout_ds005):
835862 assert ents ["run" ] == 1
836863 newpath = layout_ds005 .build_path (ents , absolute_paths = False )
837864 assert newpath == "sub-01/func/sub-01_task-mixedgamblestask_run-01_bold.nii.gz"
865+
866+ @pytest .mark .parametrize (
867+ "fname" , [
868+ "sub-01/anat/sub-01_T1w.nii.gz" ,
869+ ".datalad" ,
870+ "code" ,
871+ "sub-01/.datalad" ,
872+ ],
873+ )
874+ def test_indexer_patterns (fname ):
875+ root = Path ("/home/user/.cache/data/" )
876+ path = root / fname
877+
878+ assert bool (_check_path_matches_patterns (
879+ path ,
880+ ["code" ],
881+ root = None ,
882+ )) is (fname == "code" )
883+
884+ assert _check_path_matches_patterns (
885+ path ,
886+ [re .compile (r"/\." )],
887+ root = None ,
888+ ) is True
889+
890+ assert _check_path_matches_patterns (
891+ path ,
892+ [re .compile (r"/\." )],
893+ root = root ,
894+ ) is (".datalad" in fname )
0 commit comments