22from collections .abc import Iterator
33from functools import cached_property
44from pathlib import Path
5+ from typing import TYPE_CHECKING
56
67from bids import BIDSLayout
78
89from lib .imaging_lib .bids .dataset_description import BidsDatasetDescription
910from lib .imaging_lib .bids .tsv_participants import BidsTsvParticipant , read_bids_participants_tsv_file
1011from lib .imaging_lib .bids .tsv_scans import BidsTsvScan , read_bids_scans_tsv_file
11- from lib .imaging_lib .nifti import find_dir_nifti_files
12- from lib .util .fs import replace_file_extension , search_dir_file_with_regex
12+ from lib .util .fs import search_dir_file_with_regex
1313from lib .util .iter import find
1414
15+ if TYPE_CHECKING :
16+ from lib .imaging_lib .bids .eeg .dataset import BIDSEEGDataType
17+ from lib .imaging_lib .bids .mri .dataset import BIDSMRIDataType , BIDSNifti
18+
19+
1520PYBIDS_IGNORE = ['code' , 'sourcedata' , 'log' , '.git' ]
1621
1722PYBIDS_FORCE = [re .compile (r"_annotations\.(tsv|json)$" )]
@@ -37,6 +42,7 @@ def data_types(self) -> Iterator['BIDSDataType']:
3742
3843 @property
3944 def niftis (self ) -> Iterator ['BIDSNifti' ]:
45+ from lib .imaging_lib .bids .mri .dataset import BIDSMRIDataType
4046 for data_type in self .data_types :
4147 if isinstance (data_type , BIDSMRIDataType ):
4248 yield from data_type .niftis
@@ -159,6 +165,7 @@ def data_types(self) -> Iterator['BIDSDataType']:
159165
160166 @property
161167 def niftis (self ) -> Iterator ['BIDSNifti' ]:
168+ from lib .imaging_lib .bids .mri .dataset import BIDSMRIDataType
162169 for data_type in self .data_types :
163170 if isinstance (data_type , BIDSMRIDataType ):
164171 yield from data_type .niftis
@@ -226,6 +233,8 @@ def mri_data_types(self) -> list['BIDSMRIDataType']:
226233 The MRI data type directories found in this session directory.
227234 """
228235
236+ from lib .imaging_lib .bids .mri .dataset import BIDSMRIDataType
237+
229238 data_types : list [BIDSMRIDataType ] = []
230239
231240 for data_type_name in ['anat' , 'dwi' , 'fmap' , 'func' ]:
@@ -241,6 +250,8 @@ def eeg_data_types(self) -> list['BIDSEEGDataType']:
241250 The MRI data type directories found in this session directory.
242251 """
243252
253+ from lib .imaging_lib .bids .eeg .dataset import BIDSEEGDataType
254+
244255 data_types : list [BIDSEEGDataType ] = []
245256
246257 for data_type_name in ['eeg' , 'ieeg' ]:
@@ -302,91 +313,3 @@ def root_dataset(self) -> BIDSDataset:
302313 @property
303314 def subject (self ) -> BIDSSubject :
304315 return self .session .subject
305-
306-
307- # TODO: Complete with EEG-specific content.
308- class BIDSEEGDataType (BIDSDataType ):
309- pass
310-
311-
312- class BIDSMRIDataType (BIDSDataType ):
313- @cached_property
314- def niftis (self ) -> list ['BIDSNifti' ]:
315- """
316- The NIfTI files found in this MRI data type directory.
317- """
318-
319- niftis : list [BIDSNifti ] = []
320-
321- for nifti_path in find_dir_nifti_files (self .path ):
322- niftis .append (BIDSNifti (self , nifti_path .name ))
323-
324- return niftis
325-
326-
327- class BIDSNifti :
328- data_type : BIDSDataType
329- path : Path
330- suffix : str | None
331-
332- def __init__ (self , data_type : BIDSDataType , name : str ):
333- self .data_type = data_type
334- self .path = data_type .path / name
335-
336- suffix_match = re .search (r'_([a-zA-Z0-9]+)\.nii(\.gz)?$' , self .name )
337- if suffix_match is not None :
338- self .suffix = suffix_match .group (1 )
339- else :
340- self .suffix = None
341-
342- @property
343- def name (self ) -> str :
344- return self .path .name
345-
346- @property
347- def root_dataset (self ) -> BIDSDataset :
348- return self .data_type .root_dataset
349-
350- @property
351- def subject (self ) -> BIDSSubject :
352- return self .data_type .subject
353-
354- @property
355- def session (self ) -> BIDSSession :
356- return self .data_type .session
357-
358- def get_json_path (self ) -> Path | None :
359- """
360- Get the JSON sidecar file path of this NIfTI file if it exists.
361- """
362-
363- json_name = replace_file_extension (self .name , 'json' )
364- json_path = self .data_type .path / json_name
365- if not json_path .exists ():
366- return None
367-
368- return json_path
369-
370- def get_bval_path (self ) -> Path | None :
371- """
372- Get the BVAL file path of this NIfTI file if it exists.
373- """
374-
375- bval_name = replace_file_extension (self .name , 'bval' )
376- bval_path = self .data_type .path / bval_name
377- if not bval_path .exists ():
378- return None
379-
380- return bval_path
381-
382- def get_bvec_path (self ) -> Path | None :
383- """
384- Get the BVEC file path of this NIfTI file if it exists.
385- """
386-
387- bvec_name = replace_file_extension (self .name , 'bvec' )
388- bvec_path = self .data_type .path / bvec_name
389- if not bvec_path .exists ():
390- return None
391-
392- return bvec_path
0 commit comments