diff --git a/BIDSTools/ProcessBIDSDATA.py b/BIDSTools/ProcessBIDSDATA.py index 894466df..4a47fcf9 100644 --- a/BIDSTools/ProcessBIDSDATA.py +++ b/BIDSTools/ProcessBIDSDATA.py @@ -5,6 +5,8 @@ import shutil import numpy as np +import yaml + from BIDSTools.Createfile import CreatFile import template_agnotic_file from BIDSTools import * @@ -14,6 +16,8 @@ from elab_bridge import server_interface +from convert_bids_data import ConvertedfSData + def generate_top_level_file(outpout_dir): """" @@ -291,7 +295,7 @@ def construct_bids_folders(output_dir, experiment): # Append the processed experiment to the list list_experiments_already_processed.append(experiment) - return current_dir, list_experiments_already_processed + return current_dir, list_experiments_already_processed, metadata_link def add_new_experiment_to_tsv(file_path, experiment): @@ -574,6 +578,26 @@ def simple_copy(source_path, destination_path): shutil.copytree(source_path, destination_path) +def convert_row_to_yml(row, temp_file_name): + mydict = dict(row) + with open(temp_file_name, 'w') as ymlfile: + yaml.dump(mydict, ymlfile, default_flow_style=False, sort_keys=False) + + +import tempfile + + +def edf_converter(row, raw_data, output_dir): + yml_file = tempfile.NamedTemporaryFile(suffix='.yml', delete=False) + yml_file_name = yml_file.name + convert_row_to_yml(row, yml_file_name) + + edf_converter = ConvertedfSData(raw_data, yml_file_name, output_dir) + edf_converter.convert_bids_data() + yml_file.close() + os.remove(yml_file_name) + + def main(config_file_path, metada_file_path, output_dir, tag): jsonformat = elab_bridge.server_interface.extended_download(metada_file_path, config_file_path, @@ -584,11 +608,13 @@ def main(config_file_path, metada_file_path, output_dir, tag): writeheader_tsv_json_files(output_dir) fill_static_files(output_dir) + with open(metada_file_path, mode='r', newline='') as file: reader = csv.DictReader(file) for row in reader: experiment = Experiment(**row) - construct_bids_folders(output_dir, experiment) + output_edf, t, raw_data = construct_bids_folders(output_dir, experiment) + edf_converter(row, raw_data, output_edf) fill_metadata_files(output_dir, experiment) @@ -599,4 +625,4 @@ def main(config_file_path, metada_file_path, output_dir, tag): parser.add_argument("output_dir", help="The path to the output directory") parser.add_argument("tag", help="The tag to write the output to") args = parser.parse_args() - main(args.config_file_path, args.metada_file_path, args.output_dir, args.tag) + main(args.config_file_path, args.metada_file_path, args.output_dir, args.tag) \ No newline at end of file diff --git a/BIDSTools/convert_bids_data.py b/BIDSTools/convert_bids_data.py new file mode 100644 index 00000000..c4efc455 --- /dev/null +++ b/BIDSTools/convert_bids_data.py @@ -0,0 +1,55 @@ +import os +import shutil +from abc import ABC, abstractmethod +from eye2bids.edf2bids import edf2bids +import tempfile + + +class ConvertBIDSData(ABC): + def __init__(self, raw_data): + self.raw_data = raw_data + + @abstractmethod + def convert_bids_data(self): + """Convert BIDS data. Must be overridden in subclasses.""" + pass + + +class ConvertedfSData(ConvertBIDSData): + def __init__(self, raw_data, metadata, path_to_store_convertfile): + super().__init__(raw_data) + + # Validate the raw data file extension + basename, extension = os.path.splitext(raw_data) + if extension.lower() != '.edf': + raise ValueError("Check the raw data file extension; it must be '.edf'") + + self.raw_data = raw_data + + # Validate the metadata file extension + basename, extension = os.path.splitext(metadata) + if extension.lower() != '.yml': + raise ValueError("Check the metadata file extension; it must be '.yml'") + + self.metadata = metadata + self.path_to_store_convertfile = path_to_store_convertfile + + def convert_bids_data(self): + # Call the conversion function + # add a tmp dir to store the converted file + with tempfile.TemporaryDirectory() as tmpdirname: + edf2bids(self.raw_data, self.metadata, tmpdirname) + for file in os.listdir(tmpdirname): + shutil.move(os.path.join(tmpdirname, file), + os.path.join(self.path_to_store_convertfile, file)) + + +if __name__ == "__main__": + # Define your parameters + raw_data = "/path/to/your/file.edf" # Path to the raw EDF data file + metadata = "/path/to/your/metadata.yml" # Path to the metadata YAML file + output_dir = "/path/to/store/converted/file" # Directory to store converted files + + # Instantiate the converter for EDF data + edf_converter = ConvertedfSData(raw_data, metadata, output_dir) + edf_converter.convert_bids_data() diff --git a/requirements.txt b/requirements.txt index 84138471..5b00f98c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ pandas~=2.2.2 python-dateutil~=2.9.0.post0 six~=1.16.0 DigLabTools~= 0.0.7 +git+https://github.com/bids-standard/eye2bids.git#egg=eye2bids \ No newline at end of file