88from lib .db .queries .mri_scan_type import try_get_mri_scan_type_with_name
99from lib .env import Env
1010from lib .imaging_lib .bids .json import add_bids_json_file_parameters
11- from lib .imaging_lib .bids .mri .dataset import BIDSNifti
11+ from lib .imaging_lib .bids .mri .dataset import BIDSMRIAcquisition
1212from lib .imaging_lib .bids .tsv_scans import add_scan_tsv_file_parameters
1313from lib .imaging_lib .bids .util import determine_bids_file_type
1414from lib .imaging_lib .file import register_imaging_file
1919from lib .import_bids_dataset .env import BIDSImportEnv
2020from lib .logging import log , log_warning
2121from lib .util .crypto import compute_file_blake2b_hash
22- from lib .util .fs import get_file_extension
22+ from lib .util .fs import get_path_extension
2323
2424KNOWN_SUFFIXES_PER_MRI_DATA_TYPE = {
2525 'anat' : [
3838}
3939
4040
41- def import_bids_nifti (env : Env , import_env : BIDSImportEnv , session : DbSession , nifti : BIDSNifti ):
41+ def import_bids_nifti (env : Env , import_env : BIDSImportEnv , session : DbSession , acquisition : BIDSMRIAcquisition ):
4242 """
4343 Import a BIDS NIfTI file and its associated files in LORIS.
4444 """
4545
4646 log (
4747 env ,
4848 (
49- f"Importing MRI file '{ nifti .name } '... ({ import_env .processed_files_count + 1 } "
49+ f"Importing MRI acquisition '{ acquisition .name } '... ({ import_env .processed_files_count + 1 } "
5050 f" / { import_env .total_files_count } )"
5151 ),
5252 )
5353
5454 # Get the relevant `scans.tsv` row if there is one.
5555
56- tsv_scan = nifti .session .get_tsv_scan (nifti .name )
56+ tsv_scan = acquisition .session .get_tsv_scan (acquisition . nifti_path .name )
5757 if tsv_scan is None :
58- log_warning (env , f"No scans.tsv row found for file '{ nifti .name } ', scans.tsv data will be ignored." )
58+ log_warning (
59+ env ,
60+ f"No scans.tsv row found for acquisition '{ acquisition .name } ', scans.tsv data will be ignored." ,
61+ )
5962
6063 # Get the path at which to copy the file.
6164
@@ -64,10 +67,10 @@ def import_bids_nifti(env: Env, import_env: BIDSImportEnv, session: DbSession, n
6467 cast (Path , import_env .loris_bids_path )
6568 / f'sub-{ session .candidate .psc_id } '
6669 / f'ses-{ session .visit_label } '
67- / nifti .data_type .name
70+ / acquisition .data_type .name
6871 )
6972
70- loris_file_path = loris_file_dir_path / nifti .name
73+ loris_file_path = loris_file_dir_path / acquisition . nifti_path .name
7174
7275 loris_file_rel_path = loris_file_path .relative_to (import_env .data_dir_path )
7376
@@ -81,40 +84,36 @@ def import_bids_nifti(env: Env, import_env: BIDSImportEnv, session: DbSession, n
8184
8285 # Get information about the file.
8386
84- file_type = get_check_nifti_imaging_file_type (env , nifti )
85- file_hash = get_check_nifti_file_hash (env , nifti )
86- mri_scan_type = get_nifti_mri_scan_type (env , import_env , nifti )
87+ file_type = get_check_nifti_imaging_file_type (env , acquisition )
88+ file_hash = get_check_nifti_file_hash (env , acquisition )
89+ mri_scan_type = get_nifti_mri_scan_type (env , import_env , acquisition )
8790
8891 # Get the auxiliary files.
8992
9093 aux_file_paths : list [Path ] = []
9194
92- json_path = nifti .get_json_path ()
95+ if acquisition .bval_path is not None :
96+ aux_file_paths .append (acquisition .bval_path )
9397
94- bval_path = nifti .get_bval_path ()
95- if bval_path is not None :
96- aux_file_paths .append (bval_path )
97-
98- bvec_path = nifti .get_bvec_path ()
99- if bvec_path is not None :
100- aux_file_paths .append (bvec_path )
98+ if acquisition .bvec_path is not None :
99+ aux_file_paths .append (acquisition .bvec_path )
101100
102101 # Get the file parameters.
103102
104103 file_parameters : dict [str , Any ] = {}
105104
106- if json_path is not None :
107- json_loris_path = loris_file_dir_path / json_path .name
105+ if acquisition . sidecar_path is not None :
106+ json_loris_path = loris_file_dir_path / acquisition . sidecar_path .name
108107 json_loris_rel_path = json_loris_path .relative_to (import_env .data_dir_path )
109- add_bids_json_file_parameters (env , json_path , json_loris_rel_path , file_parameters )
108+ add_bids_json_file_parameters (env , acquisition . sidecar_path , json_loris_rel_path , file_parameters )
110109
111- add_nifti_file_parameters (nifti . path , file_hash , file_parameters )
110+ add_nifti_file_parameters (acquisition . nifti_path , file_hash , file_parameters )
112111
113- if nifti .session .tsv_scans_path is not None and tsv_scan is not None :
114- add_scan_tsv_file_parameters (tsv_scan , nifti .session .tsv_scans_path , file_parameters )
112+ if acquisition .session .tsv_scans_path is not None and tsv_scan is not None :
113+ add_scan_tsv_file_parameters (tsv_scan , acquisition .session .tsv_scans_path , file_parameters )
115114
116115 for aux_file_path in aux_file_paths :
117- aux_file_type = get_file_extension (aux_file_path . name )
116+ aux_file_type = get_path_extension (aux_file_path )
118117 aux_file_hash = compute_file_blake2b_hash (aux_file_path )
119118 aux_file_loris_path = loris_file_dir_path / aux_file_path .name
120119 aux_file_loris_rel_path = aux_file_loris_path .relative_to (import_env .data_dir_path )
@@ -123,10 +122,10 @@ def import_bids_nifti(env: Env, import_env: BIDSImportEnv, session: DbSession, n
123122
124123 # Copy the files on the file system.
125124
126- copy_bids_file (loris_file_dir_path , nifti . path )
125+ copy_bids_file (loris_file_dir_path , acquisition . nifti_path )
127126
128- if json_path is not None :
129- copy_bids_file (loris_file_dir_path , json_path )
127+ if acquisition . sidecar_path is not None :
128+ copy_bids_file (loris_file_dir_path , acquisition . sidecar_path )
130129
131130 for aux_file_path in aux_file_paths :
132131 copy_bids_file (loris_file_dir_path , aux_file_path )
@@ -159,26 +158,26 @@ def import_bids_nifti(env: Env, import_env: BIDSImportEnv, session: DbSession, n
159158 import_env .imported_files_count += 1
160159
161160
162- def get_check_nifti_imaging_file_type (env : Env , nifti : BIDSNifti ) -> str :
161+ def get_check_nifti_imaging_file_type (env : Env , acqusition : BIDSMRIAcquisition ) -> str :
163162 """
164163 Get the BIDS file type of a NIfTI file and raise an exception if that file type is not
165164 registered in the database.
166165 """
167166
168- file_type = determine_bids_file_type (env , nifti .name )
167+ file_type = determine_bids_file_type (env , acqusition . nifti_path .name )
169168 if file_type is None :
170169 raise Exception ("No matching file type found in the database." )
171170
172171 return file_type
173172
174173
175- def get_check_nifti_file_hash (env : Env , nifti : BIDSNifti ) -> str :
174+ def get_check_nifti_file_hash (env : Env , acquisition : BIDSMRIAcquisition ) -> str :
176175 """
177176 Compute the BLAKE2b hash of a NIfTI file and raise an exception if that hash is already
178177 registered in the database.
179178 """
180179
181- file_hash = compute_file_blake2b_hash (nifti . path )
180+ file_hash = compute_file_blake2b_hash (acquisition . nifti_path )
182181
183182 file = try_get_file_with_hash (env .db , file_hash )
184183 if file is not None :
@@ -187,27 +186,31 @@ def get_check_nifti_file_hash(env: Env, nifti: BIDSNifti) -> str:
187186 return file_hash
188187
189188
190- def get_nifti_mri_scan_type (env : Env , import_env : BIDSImportEnv , nifti : BIDSNifti ) -> DbMriScanType | None :
189+ def get_nifti_mri_scan_type (
190+ env : Env ,
191+ import_env : BIDSImportEnv ,
192+ acquisition : BIDSMRIAcquisition ,
193+ ) -> DbMriScanType | None :
191194 """
192- Get the MRI scan type corresponding to a NIfTI file using its BIDS suffix. Create the MRI scan
193- type in the database the suffix is a standard BIDS suffix and the scan type does not already
194- exist in the database, or raise an exception if no known scan type is found.
195+ Get the MRI scan type corresponding to a BIDS MRI acquisition using its BIDS suffix. Create the
196+ MRI scan type in the database the suffix is a standard BIDS suffix and the scan type does not
197+ already exist in the database, or raise an exception if no known scan type is found.
195198 """
196199
197- if nifti .suffix is None :
200+ if acquisition .suffix is None :
198201 raise Exception ("No BIDS suffix found in the NIfTI file name, cannot infer the file data type." )
199202
200- mri_scan_type = try_get_mri_scan_type_with_name (env .db , nifti .suffix )
203+ mri_scan_type = try_get_mri_scan_type_with_name (env .db , acquisition .suffix )
201204 if mri_scan_type is not None :
202205 return mri_scan_type
203206
204- if nifti .suffix not in KNOWN_SUFFIXES_PER_MRI_DATA_TYPE [nifti .data_type .name ]:
205- if nifti .suffix not in import_env .unknown_scan_types :
206- import_env .unknown_scan_types .append (nifti .suffix )
207+ if acquisition .suffix not in KNOWN_SUFFIXES_PER_MRI_DATA_TYPE [acquisition .data_type .name ]:
208+ if acquisition .suffix not in import_env .unknown_scan_types :
209+ import_env .unknown_scan_types .append (acquisition .suffix )
207210
208- raise Exception (f"Found unknown MRI file suffix '{ nifti .suffix } '." )
211+ raise Exception (f"Found unknown MRI file suffix '{ acquisition .suffix } '." )
209212
210- return create_mri_scan_type (env , nifti .suffix )
213+ return create_mri_scan_type (env , acquisition .suffix )
211214
212215
213216def copy_bids_file (loris_file_dir_path : Path , file_path : Path ):
0 commit comments