@@ -278,7 +278,7 @@ def fetch_and_insert_eeg_files(self, derivatives=False, detect=True):
278278 with open (eegjson_file .path ) as data_file :
279279 eeg_file_data = json .load (data_file )
280280
281- eegjson_file_path = eegjson_file .path .replace ( self . data_dir , '' )
281+ eegjson_file_path = os .path .relpath ( eegjson_file . path , self . data_dir )
282282 if self .loris_bids_root_dir :
283283 # copy the JSON file to the LORIS BIDS import directory
284284 eegjson_file_path = self .copy_file_to_loris_bids_dir (
@@ -317,7 +317,7 @@ def fetch_and_insert_eeg_files(self, derivatives=False, detect=True):
317317 # eeg_file_data dictionary
318318 fdt_file_path = None
319319 if file_type == 'set' and fdt_file :
320- fdt_file_path = fdt_file .path .replace ( self .data_dir , '' )
320+ fdt_file_path = os .path .relpath ( fdt_file , self .data_dir )
321321 if self .loris_bids_root_dir :
322322 # copy the fdt file to the LORIS BIDS import directory
323323 fdt_file_path = self .copy_file_to_loris_bids_dir (
@@ -344,7 +344,7 @@ def fetch_and_insert_eeg_files(self, derivatives=False, detect=True):
344344 # grep the modality ID from physiological_modality table
345345 modality_id = physiological_modality .grep_id_from_modality_value (self .data_type .name )
346346
347- eeg_path = eeg_file .path .replace ( self . data_dir , '' )
347+ eeg_path = os .path .relpath ( eeg_file . path , self . data_dir )
348348 if self .loris_bids_root_dir :
349349 # copy the eeg_file to the LORIS BIDS import directory
350350 eeg_path = self .copy_file_to_loris_bids_dir (
@@ -438,7 +438,7 @@ def fetch_and_insert_electrode_file(
438438 )
439439 if not result :
440440 electrode_data = utilities .read_tsv_file (electrode_file .path )
441- electrode_path = electrode_file .path .replace ( self . data_dir , '' )
441+ electrode_path = os .path .relpath ( electrode_file . path , self . data_dir )
442442 if self .loris_bids_root_dir :
443443 # copy the electrode file to the LORIS BIDS import directory
444444 electrode_path = self .copy_file_to_loris_bids_dir (
@@ -478,7 +478,7 @@ def fetch_and_insert_electrode_file(
478478 electrode_ids
479479 )
480480 else :
481- electrode_metadata_path = coordsystem_metadata_file .path .replace ( self .data_dir , '' )
481+ electrode_metadata_path = os .path .relpath ( coordsystem_metadata_file , self .data_dir )
482482 if self .loris_bids_root_dir :
483483 # copy the electrode metadata file to the LORIS BIDS import directory
484484 electrode_metadata_path = self .copy_file_to_loris_bids_dir (
@@ -547,7 +547,7 @@ def fetch_and_insert_channel_file(
547547 channel_path = result [0 ]['FilePath' ] if result else None
548548 channel_data = utilities .read_tsv_file (channel_file .path )
549549 if not result :
550- channel_path = channel_file .path .replace ( self . data_dir , '' )
550+ channel_path = os .path .relpath ( channel_file . path , self . data_dir )
551551 if self .loris_bids_root_dir :
552552 # copy the channel file to the LORIS BIDS import directory
553553 channel_path = self .copy_file_to_loris_bids_dir (
@@ -632,7 +632,7 @@ def fetch_and_insert_event_files(
632632 'with physiological file ID ' + str (physiological_file_id )
633633 print (message )
634634 else :
635- event_metadata_path = event_metadata_file .path .replace ( self . data_dir , '' )
635+ event_metadata_path = os .path .relpath ( event_metadata_file . path , self . data_dir )
636636 if self .loris_bids_root_dir :
637637 # copy the event file to the LORIS BIDS import directory
638638 event_metadata_path = self .copy_file_to_loris_bids_dir (
@@ -657,7 +657,7 @@ def fetch_and_insert_event_files(
657657
658658 # get events.tsv file and insert
659659 event_data = utilities .read_tsv_file (event_data_file .path )
660- event_path = event_data_file .path .replace ( self . data_dir , '' )
660+ event_path = os .path .relpath ( event_data_file . path , self . data_dir )
661661 if self .loris_bids_root_dir :
662662 # copy the event file to the LORIS BIDS import directory
663663 event_path = self .copy_file_to_loris_bids_dir (
@@ -701,42 +701,39 @@ def copy_file_to_loris_bids_dir(self, file, derivatives=False, inheritance=False
701701 # Handle derivatives differently
702702 # Data path structure is unpredictable, so keep the same relative path
703703 if derivatives :
704- copy_file = str .replace (
705- file ,
706- self .bids_layout .root ,
707- ""
708- )
709-
704+ copy_file = os .path .relpath (file , self .bids_layout .root )
710705 copy_file = os .path .join (self .loris_bids_root_dir , copy_file )
711-
712- # create derivative directories
713- lib .utilities .create_dir (
714- os .path .dirname (copy_file ),
715- self .verbose
716- )
717706 else :
718707 # determine the path of the copied file
719708 copy_file = ""
720709 if not inheritance :
721710 copy_file = self .loris_bids_eeg_rel_dir
722711 if self .data_type .session .label :
723- copy_file + = os .path .basename (file )
712+ copy_file = os .path .join ( copy_file , os . path . basename (file ) )
724713 else :
725714 # make sure the ses- is included in the new filename if using
726715 # default visit label from the LORIS config
727- copy_file += str .replace (
728- os .path .basename (file ),
729- "sub-" + self .data_type .subject .label ,
730- "sub-" + self .data_type .subject .label + "_ses-" + self .default_vl
716+ copy_file = os .path .join (
717+ copy_file ,
718+ os .path .basename (file ).replace (
719+ "sub-" + self .data_type .subject .label ,
720+ "sub-" + self .data_type .subject .label + "_ses-" + self .default_vl
721+ )
731722 )
732723
733724 copy_file = os .path .join (self .loris_bids_root_dir , copy_file )
734725
726+ # create the archive directory if it does not exist
727+ lib .utilities .create_dir (
728+ os .path .dirname (copy_file ),
729+ self .verbose
730+ )
731+
735732 # copy the file
736733 utilities .copy_file (file , copy_file , self .verbose )
737734
738735 # determine the relative path and return it
739- relative_path = copy_file . replace ( self .data_dir , "" )
736+ relative_path = os . path . relpath ( copy_file , self .data_dir )
740737
741738 return relative_path
742739
@@ -786,6 +783,12 @@ def create_and_insert_archive(self, files_to_archive, archive_rel_name,
786783 else :
787784 return
788785
786+ # create the file directory
787+ lib .utilities .create_dir (
788+ os .path .dirname (archive_full_path ),
789+ self .verbose
790+ )
791+
789792 # create the archive file
790793 utilities .create_archive (files_to_archive , archive_full_path )
791794
@@ -841,6 +844,12 @@ def create_and_insert_event_archive(self, files_to_archive, archive_rel_name, ee
841844 else :
842845 return
843846
847+ # create the archive directory if it does not exist
848+ lib .utilities .create_dir (
849+ os .path .dirname (archive_full_path ),
850+ self .verbose
851+ )
852+
844853 # create the archive file
845854 utilities .create_archive (files_to_archive , archive_full_path )
846855
0 commit comments