33from loris_bids_reader .info import BidsAcquisitionInfo
44from loris_bids_reader .meg .acquisition import MegAcquisition
55from loris_bids_reader .meg .reader import BidsMegDataTypeReader
6+ from loris_utils .archive import create_archive_with_file
67from loris_utils .error import group_errors_tuple
8+ from loris_utils .path import add_path_extension
79
8- from lib .config import get_eeg_viz_enabled_config
10+ from lib .config import (
11+ get_data_dir_path_config ,
12+ get_ephys_archive_dir_path_config ,
13+ get_ephys_visualization_enabled_config ,
14+ )
915from lib .db .models .meg_ctf_head_shape_file import DbMegCtfHeadShapeFile
1016from lib .db .models .session import DbSession
1117from lib .db .queries .physio_file import try_get_physio_file_with_path
1824from lib .import_bids_dataset .events import insert_events_metadata_file
1925from lib .import_bids_dataset .events_tsv import insert_bids_events_file
2026from lib .import_bids_dataset .file_type import get_check_bids_imaging_file_type
21- from lib .import_bids_dataset .head_shape import insert_head_shape_file
22- from lib .import_bids_dataset .meg_channels import read_meg_channels
23- from lib .import_bids_dataset .physio import get_check_bids_physio_modality , get_check_bids_physio_output_type
27+ from lib .import_bids_dataset .meg .ctf_head_shape import insert_head_shape_file
28+ from lib .import_bids_dataset .physio import (
29+ get_check_bids_physio_file_hash ,
30+ get_check_bids_physio_modality ,
31+ get_check_bids_physio_output_type ,
32+ )
2433from lib .logging import log , log_warning
2534from lib .physio .chunking import create_physio_channels_chunks
2635from lib .physio .events import FileSource
@@ -73,13 +82,12 @@ def import_bids_meg_acquisition(
7382 bids_info : BidsAcquisitionInfo ,
7483 head_shape_file : DbMegCtfHeadShapeFile | None ,
7584):
76- # TODO: The file is actually a directory, it should be tared before proceeding to the hash.
77- modality , output_type , file_type = group_errors_tuple (
85+ modality , output_type , file_type , file_hash = group_errors_tuple (
7886 f"Error while checking database information for MEG acquisition '{ bids_info .name } '." ,
7987 lambda : get_check_bids_physio_modality (env , bids_info .data_type ),
8088 lambda : get_check_bids_physio_output_type (env , args .type or 'raw' ),
8189 lambda : get_check_bids_imaging_file_type (env , 'ctf' ),
82- # lambda: get_check_bids_physio_file_hash(env, acquisition),
90+ lambda : get_check_bids_physio_file_hash (env , acquisition . ctf_path ),
8391 )
8492
8593 # The files to copy to LORIS, with the source path on the left and the LORIS path on the right.
@@ -96,6 +104,9 @@ def import_bids_meg_acquisition(
96104
97105 check_bids_meg_metadata_files (env , acquisition , bids_info )
98106
107+ ctf_archive_path = get_ctf_archive_path (env , loris_file_path )
108+ create_archive_with_file (import_env .data_dir_path / ctf_archive_path , acquisition .ctf_path )
109+
99110 physio_file = insert_physio_file (
100111 env ,
101112 session ,
@@ -104,11 +115,11 @@ def import_bids_meg_acquisition(
104115 modality ,
105116 output_type ,
106117 bids_info .scan_row .get_acquisition_time () if bids_info .scan_row is not None else None ,
107- None , # TODO: Use archive.
118+ ctf_archive_path ,
108119 head_shape_file ,
109120 )
110121
111- # insert_physio_file_parameter(env, physio_file, 'physiological_json_file_blake2b_hash ', file_hash) # ruff:noqa
122+ insert_physio_file_parameter (env , physio_file , 'physiological_file_blake2b_hash ' , file_hash )
112123 for name , value in acquisition .sidecar_file .data .items ():
113124 insert_physio_file_parameter (env , physio_file , name , value )
114125
@@ -139,17 +150,12 @@ def import_bids_meg_acquisition(
139150
140151 log (env , f"MEG file succesfully imported with ID: { physio_file .id } ." )
141152
142- # TODO: Remove the false.
143- if get_eeg_viz_enabled_config (env ):
153+ if get_ephys_visualization_enabled_config (env ):
144154 log (env , "Creating visualization chunks..." )
145155 create_physio_channels_chunks (env , physio_file )
146156
147- read_meg_channels (env , import_env , physio_file , acquisition , bids_info )
148-
149157 env .db .commit ()
150158
151- import_env .imported_files_count += 1
152-
153159
154160def check_bids_meg_metadata_files (env : Env , acquisition : MegAcquisition , bids_info : BidsAcquisitionInfo ):
155161 """
@@ -165,3 +171,19 @@ def check_bids_meg_metadata_files(env: Env, acquisition: MegAcquisition, bids_in
165171
166172 if acquisition .events_file is not None and acquisition .events_file .dictionary is not None :
167173 log_warning (env , f"No events dictionary file found for acquisition '{ bids_info .name } '." )
174+
175+
176+ def get_ctf_archive_path (env : Env , loris_ctf_path : Path ) -> Path :
177+ """
178+ Get the path of a CTF archive.
179+ """
180+
181+ archive_rel_path = add_path_extension (loris_ctf_path , 'tgz' )
182+ archive_dir_path = get_ephys_archive_dir_path_config (env )
183+ if archive_dir_path is not None :
184+ data_dir_path = get_data_dir_path_config (env )
185+ archive_path = archive_dir_path / 'ctf' / archive_rel_path .name
186+ archive_path .parent .mkdir (exist_ok = True , parents = True )
187+ return (archive_path ).relative_to (data_dir_path )
188+ else :
189+ return archive_rel_path
0 commit comments