22import re
33from collections .abc import Iterator
44from functools import cached_property
5+ from pathlib import Path
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_names
12+ from lib .imaging_lib .nifti import find_dir_nifti_files
1213from lib .util .fs import replace_file_extension , search_dir_file_with_regex
1314from lib .util .iter import find
1415
1819
1920
2021class BIDSDataset :
21- path : str
22+ path : Path
2223 validate : bool
2324
24- def __init__ (self , bids_path : str , validate : bool ):
25+ def __init__ (self , bids_path : Path , validate : bool ):
2526 self .path = bids_path
2627 self .validate = validate
2728
@@ -68,8 +69,8 @@ def get_dataset_description(self) -> 'BidsDatasetDescription | None':
6869 does contains incorrect data.
6970 """
7071
71- dataset_description_path = os . path . join ( self .path , 'dataset_description.json' )
72- if not os . path . exists (dataset_description_path ):
72+ dataset_description_path = self .path / 'dataset_description.json'
73+ if not dataset_description_path . exists ():
7374 return None
7475
7576 return BidsDatasetDescription (dataset_description_path )
@@ -81,8 +82,8 @@ def tsv_participants(self) -> dict[str, BidsTsvParticipant] | None:
8182 present. This property might raise an exception if the file is present but incorrect.
8283 """
8384
84- tsv_participants_path = os . path . join ( self .path , 'participants.tsv' )
85- if not os . path . exists (tsv_participants_path ):
85+ tsv_participants_path = self .path / 'participants.tsv'
86+ if not tsv_participants_path . exists ():
8687 return None
8788
8889 return read_bids_participants_tsv_file (tsv_participants_path )
@@ -143,13 +144,13 @@ def layout(self) -> BIDSLayout:
143144
144145class BIDSSubject :
145146 root_dataset : BIDSDataset
147+ path : Path
146148 label : str
147- path : str
148149
149150 def __init__ (self , root_dataset : BIDSDataset , label : str ):
150151 self .root_dataset = root_dataset
151152 self .label = label
152- self .path = os . path . join ( self .root_dataset .path , f'sub-{ self .label } ' )
153+ self .path = self .root_dataset .path / f'sub-{ self .label } '
153154
154155 @property
155156 def data_types (self ) -> Iterator ['BIDSDataType' ]:
@@ -195,23 +196,19 @@ def get_session(self, session_label: str) -> 'BIDSSession | None':
195196
196197class BIDSSession :
197198 subject : BIDSSubject
199+ path : Path
198200 label : str | None
199- path : str
200- tsv_scans_path : str | None
201+ tsv_scans_path : Path | None
201202
202203 def __init__ (self , subject : BIDSSubject , label : str | None ):
203204 self .subject = subject
204205 self .label = label
205- if label is None :
206- self .path = self . subject .path
206+ if label is not None :
207+ self .path = subject .path / f'ses- { self . label } '
207208 else :
208- self .path = os . path . join ( self . subject .path , f'ses- { self . label } ' )
209+ self .path = subject .path
209210
210- tsv_scans_name = search_dir_file_with_regex (self .path , r'scans.tsv$' )
211- if tsv_scans_name is not None :
212- self .tsv_scans_path = os .path .join (self .path , tsv_scans_name )
213- else :
214- self .tsv_scans_path = None
211+ self .tsv_scans_path = search_dir_file_with_regex (self .path , r'scans.tsv$' )
215212
216213 @property
217214 def root_dataset (self ) -> BIDSDataset :
@@ -264,13 +261,15 @@ def get_tsv_scan(self, file_name: str) -> 'BidsTsvScan | None':
264261
265262class BIDSDataType :
266263 session : BIDSSession
267- name : str
268- path : str
264+ path : Path
269265
270266 def __init__ (self , session : BIDSSession , name : str ):
271267 self .session = session
272- self .name = name
273- self .path = os .path .join (self .session .path , self .name )
268+ self .path = session .path / name
269+
270+ @property
271+ def name (self ) -> str :
272+ return self .path .name
274273
275274 @property
276275 def root_dataset (self ) -> BIDSDataset :
@@ -288,29 +287,31 @@ def niftis(self) -> list['BIDSNifti']:
288287
289288 niftis : list [BIDSNifti ] = []
290289
291- for nifti_name in find_dir_nifti_names (self .path ):
292- niftis .append (BIDSNifti (self , nifti_name ))
290+ for nifti_path in find_dir_nifti_files (self .path ):
291+ niftis .append (BIDSNifti (self , nifti_path . name ))
293292
294293 return niftis
295294
296295
297296class BIDSNifti :
298297 data_type : BIDSDataType
299- name : str
300- path : str
298+ path : Path
301299 suffix : str | None
302300
303301 def __init__ (self , data_type : BIDSDataType , name : str ):
304302 self .data_type = data_type
305- self .path = os .path .join (self .data_type .path , name )
306- self .name = name
303+ self .path = data_type .path / name
307304
308305 suffix_match = re .search (r'_([a-zA-Z0-9]+)\.nii(\.gz)?$' , self .name )
309306 if suffix_match is not None :
310307 self .suffix = suffix_match .group (1 )
311308 else :
312309 self .suffix = None
313310
311+ @property
312+ def name (self ) -> str :
313+ return self .path .name
314+
314315 @property
315316 def root_dataset (self ) -> BIDSDataset :
316317 return self .data_type .root_dataset
@@ -323,38 +324,38 @@ def subject(self) -> BIDSSubject:
323324 def session (self ) -> BIDSSession :
324325 return self .data_type .session
325326
326- def get_json_path (self ) -> str | None :
327+ def get_json_path (self ) -> Path | None :
327328 """
328329 Get the JSON sidecar file path of this NIfTI file if it exists.
329330 """
330331
331332 json_name = replace_file_extension (self .name , 'json' )
332- json_path = os . path . join ( self .data_type .path , json_name )
333- if not os . path . exists (json_path ):
333+ json_path = self .data_type .path / json_name
334+ if not json_path . exists ():
334335 return None
335336
336337 return json_path
337338
338- def get_bval_path (self ) -> str | None :
339+ def get_bval_path (self ) -> Path | None :
339340 """
340341 Get the BVAL file path of this NIfTI file if it exists.
341342 """
342343
343344 bval_name = replace_file_extension (self .name , 'bval' )
344- bval_path = os . path . join ( self .data_type .path , bval_name )
345- if not os . path . exists (bval_path ):
345+ bval_path = self .data_type .path / bval_name
346+ if not bval_path . exists ():
346347 return None
347348
348349 return bval_path
349350
350- def get_bvec_path (self ) -> str | None :
351+ def get_bvec_path (self ) -> Path | None :
351352 """
352353 Get the BVEC file path of this NIfTI file if it exists.
353354 """
354355
355356 bvec_name = replace_file_extension (self .name , 'bvec' )
356- bvec_path = os . path . join ( self .data_type .path , bvec_name )
357- if not os . path . exists (bvec_path ):
357+ bvec_path = self .data_type .path / bvec_name
358+ if not bvec_path . exists ():
358359 return None
359360
360361 return bvec_path
0 commit comments