-
Notifications
You must be signed in to change notification settings - Fork 2
/
BIDS_process_physio_ses_2.py
1615 lines (1286 loc) · 78.6 KB
/
BIDS_process_physio_ses_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
BIDS_process_physio_ses_2.py
Description:
This script processes physiological data, specifically [ECG, respiration, EDA, PPG], acquired through
Biopac Acqknowledge during Task fMRI sessions, into BIDS-compliant files. It includes functions for loading data,
segmenting based on run and event types, writing metadata, and generating quality control plots. The script is designed
to work with data in .mat format format and expects a BIDS specific directory structure for input and output.
Usage:
Run the script from the command line with the physiological data directory and BIDS dataset directory as arguments.
e.g.,
python BIDS_process_physio_ses_2.py /path/to/physio_data /path/to/BIDS_dataset
Output files will be saved in the specified BIDS dataset directory.
Author: PAMcConnell
Created on: 20231111
Last Modified: 20231111
License: MIT License
Dependencies:
- Python 3.12
- numpy
- pandas
- matplotliby)
- scipy
Environment Setup:
Use the provided environment.yml (datalad.yml) file with Conda for setting up the required environment.
Change Log:
- 20231111: Initial version
Error Handling and Logging:
The script includes comprehensive error handling and logging. Errors during execution are logged, providing clarity for debugging.
Logs are saved in dataset_root/docs/logs.
"""
import os # Used for interacting with the operating system, like file path operations.
import re # Regular expressions, useful for text matching and manipulation.
import logging # Logging library, for tracking events that happen when running the software.
import argparse # Parser for command-line options, arguments, and sub-commands.
import scipy.io as sio # SciPy module for reading and writing MATLAB files.
import numpy as np # NumPy library for numerical operations on arrays and matrices.
import pandas as pd # Pandas library for data manipulation and analysis.
import matplotlib.pyplot as plt # Matplotlib's pyplot, a plotting library.
import json # Library for working with JSON data.
import glob # Used for Unix style pathname pattern expansion.
from collections import OrderedDict # Dictionary subclass that remembers the insertion order of keys.
import sys # System-specific parameters and functions.
from matplotlib.lines import Line2D # Import Line2D from matplotlib to create custom line objects.
from matplotlib.backends.backend_pdf import PdfPages # for creating multipage PDFs with matplotlib plots.
import shutil # for copying files and directories.
# Sets up archival logging for the script, directing log output to both a file and the console.
def setup_logging(subject_id, session_id, bids_root_dir):
"""
The function configures logging to capture informational, warning, and error messages. It creates a unique log file for each
subject-session combination, stored in a 'logs' directory within the 'doc' folder adjacent to the BIDS root directory.
Parameters:
- subject_id (str): The identifier for the subject.
- session_id (str): The identifier for the session.
- bids_root_dir (str): The root directory of the BIDS dataset.
Returns:
- log_file_path (str): The path to the log file.
This function sets up a logging system that writes logs to both a file and the console.
The log file is named based on the subject ID, session ID, and the script name.
It's stored in a 'logs' directory within the 'doc' folder by subject ID, which is located at the same
level as the BIDS root directory.
The logging level is set to INFO, meaning it captures all informational, warning, and error messages.
Usage Example:
setup_logging('sub-01', 'ses-1', '/path/to/bids_root_dir')
"""
try:
# Extract the base name of the script without the .py extension.
script_name = os.path.basename(__file__).replace('.py', '')
# Construct the log directory path within 'doc/logs'
log_dir = os.path.join(os.path.dirname(bids_root_dir), 'doc', 'logs', script_name, subject_id)
# Create the log directory if it doesn't exist.
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Construct the log file name using subject ID, session ID, and script name.
log_file_name = f"{subject_id}_{session_id}_{script_name}.log"
log_file_path = os.path.join(log_dir, log_file_name)
# Configure file logging.
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename=log_file_path,
filemode='w' # 'w' mode overwrites existing log file
)
# If you also want to log to console.
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logging.getLogger().addHandler(console_handler)
logging.info(f"Logging setup complete. Log file: {log_file_path}")
return log_file_path
except Exception as e:
print(f"Error setting up logging: {e}")
sys.exit(1) # Exiting the script due to logging setup failure.
# Extract the subject and session IDs from the provided physio root directory path.
def extract_subject_session(physio_root_dir):
"""
Parameters:
- physio_root_dir (str): The directory path that includes subject and session information.
This path should follow the BIDS convention, containing 'sub-' and 'ses-' prefixes.
Returns:
- subject_id (str): The extracted subject ID.
- session_id (str): The extracted session ID.
Raises:
- ValueError: If the subject_id and session_id cannot be extracted from the path.
This function assumes that the directory path follows the Brain Imaging Data Structure (BIDS) naming convention.
It uses regular expressions to find and extract the subject and session IDs from the path.
Usage Example:
subject_id, session_id = extract_subject_session('/path/to/data/sub-01/ses-1/physio')
Note: This function will raise an error if it cannot find a pattern matching the BIDS convention in the path.
"""
# Normalize the path to remove any trailing slashes for consistency.
physio_root_dir = os.path.normpath(physio_root_dir)
# The pattern looks for 'sub-' followed by any characters until a slash, and similar for 'ses-'.
match = re.search(r'(sub-[^/]+)/(ses-[^/]+)', physio_root_dir)
if not match:
raise ValueError("Unable to extract subject_id and session_id from path: %s", physio_root_dir)
subject_id, session_id = match.groups()
return subject_id, session_id
# Load a MATLAB (.mat) file containing physiological data and extracts labels, data, and units for physiological data.
def load_mat_file(mat_file_path):
"""
Loads a MATLAB (.mat) file and extracts physiological data labels, data, and units.
Parameters:
- mat_file_path (str): Path to the .mat file.
Returns:
- labels (ndarray): Array of data channel names.
- data (ndarray): Array containing physiological data.
- units (ndarray): Array of units corresponding to each data channel.
Raises:
- FileNotFoundError: If the .mat file is not found at the specified path.
- KeyError: If the .mat file lacks required keys ('labels', 'data', 'units').
- Exception: For any other issues encountered during loading.
The function verifies the existence of the .mat file, loads its contents, and checks for the presence
of required keys. It provides detailed logging for each step and potential errors for troubleshooting.
Usage Example:
labels, data, units = load_mat_file('/path/to/physio_data.mat')
Note:
- The .mat file must contain 'labels', 'data', and 'units' keys.
- Compatibility with MATLAB file formats should be verified for different versions.
"""
# Verify if the specified .mat file exists.
if not os.path.isfile(mat_file_path):
error_msg = f"MAT file does not exist at {mat_file_path}"
logging.error(error_msg)
raise FileNotFoundError(error_msg)
try:
# Attempt to load the .mat file.
logging.info(f"Loading MAT file from: {mat_file_path}")
mat_contents = sio.loadmat(mat_file_path)
# Verify that required keys are in the loaded .mat file.
required_keys = ['labels', 'data', 'units']
if not all(key in mat_contents for key in required_keys):
error_msg = f"MAT file at {mat_file_path} is missing required keys: {required_keys}"
logging.error(error_msg)
raise KeyError(error_msg)
# Extract labels, data, and units.
labels = mat_contents['labels'].flatten() # Flatten in case it's a 2D array.
data = mat_contents['data']
units = mat_contents['units'].flatten() # Flatten in case it's a 2D array
logging.info(f"Successfully loaded MAT file from: {mat_file_path}")
except Exception as e:
# Log the exception and re-raise to handle it upstream.
logging.error("Failed to load MAT file from %s: %s", mat_file_path, e)
raise
return labels, data, units
# Renames physiological data channels according to BIDS (Brain Imaging Data Structure) conventions.
def rename_channels(labels):
"""
Parameters:
- labels (array): Original names of the physiological data channels.
Returns:
- bids_labels_dictionary (dict): Mapping from original labels to BIDS-compliant labels.
- bids_labels_list (list): A list of the renamed, BIDS-compliant labels.
The function iterates through the provided original labels and renames them based on predefined
mapping to meet BIDS standards. Channels not recognized are omitted with a warning. This function
is essential for ensuring that physiological data aligns with the BIDS format for further processing.
Usage Example:
bids_labels_dict, bids_labels_list = rename_channels(['ECG', 'RSP', 'EDA', 'PPG', 'Digital input'])
Note: The function expects a specific set of channel names. Make sure to update the mapping
if different channels or naming conventions are used.
"""
logging.info("Renaming channels according to BIDS conventions")
# Define the mapping from original labels to BIDS labels.
original_label_mapping = {
'ECG': 'cardiac',
'RSP': 'respiratory',
'EDA': 'eda',
'Trigger': 'trigger',
'PPG': 'ppg', # Only if exists
# Add other mappings as required
}
# Initialize an empty dictionary and list to store the renamed labels.
bids_labels_dictionary = {}
bids_labels_list = []
# Initialize dictionary and list for storing BIDS-compliant labels.
for label in labels:
# Skip any labels for digital inputs
if 'Digital input' in label:
continue
# Check and rename the label if it matches one of the keys in original_label_mapping.
for original, bids in original_label_mapping.items():
if original in label:
bids_labels_dictionary[label] = bids
bids_labels_list.append(bids)
break
else:
logging.warning("Label '%s' does not match any BIDS convention and will be omitted.", label)
logging.info("BIDS labels list after renaming: %s", bids_labels_list)
# Debug log to print the renamed labels in the list.
return bids_labels_dictionary, bids_labels_list
# Extracts metadata from a JSON file and identifies the associated run.
def extract_metadata_from_json(json_file_path, processed_jsons):
"""
Extracts specific metadata from a JSON file and returns the run identifier and metadata.
Parameters:
- json_file_path (str): Path to the JSON file containing metadata for a specific fMRI run.
- processed_jsons (set): A set that tracks already processed JSON files to avoid duplication.
Returns:
- tuple: (run_id, run_metadata), where:
- run_id (str): The identifier of the fMRI run, extracted from the file name.
- run_metadata (dict): A dictionary containing extracted metadata.
Raises:
- FileNotFoundError: If the specified JSON file does not exist.
- ValueError: If the run_id cannot be determined or required metadata fields are missing.
- json.JSONDecodeError: If the JSON file contains invalid JSON.
This function is critical for parsing and organizing metadata necessary for subsequent data processing steps.
It verifies the existence of essential fields and logs detailed information for debugging and audit purposes.
Usage Example:
run_id, metadata = extract_metadata_from_json('/path/to/run-01_bold.json', processed_jsons_set)
"""
# Log the start of metadata extraction.
logging.info(f"Extracting metadata from JSON file: {json_file_path}")
# Avoid reprocessing the same file.
if json_file_path in processed_jsons:
logging.info("JSON file %s has already been processed.", json_file_path)
return None, None # No new metadata to return.
# Check if the JSON file exists.
if not os.path.isfile(json_file_path):
logging.error("JSON file does not exist at %s", json_file_path)
raise FileNotFoundError(f"No JSON file found at the specified path: {json_file_path}")
# Load the JSON file content.
try:
with open(json_file_path, 'r') as file:
metadata = json.load(file)
except json.JSONDecodeError as e:
logging.error(f"Error decoding JSON: {e}")
raise
try:
# Extract run_id from the file name.
run_id_match = re.search(r'run-\d+', json_file_path)
if not run_id_match:
raise ValueError(f"Run identifier not found in JSON file name: {json_file_path}")
run_id = run_id_match.group()
# Verify essential metadata fields are present.
required_fields = ['TaskName', 'RepetitionTime', 'NumVolumes', 'SeriesDescription']
run_metadata = {field: metadata.get(field) for field in required_fields}
if not all(run_metadata.values()):
missing_fields = [key for key, value in run_metadata.items() if value is None]
raise ValueError(f"JSON file {json_file_path} is missing required fields: {missing_fields}")
# Add the JSON file to the set of processed files.
processed_jsons.add(json_file_path)
logging.info(f"Successfully extracted metadata for {run_id}: {run_metadata}")
# Return the run ID and extracted metadata.
return run_id, run_metadata
except Exception as e:
logging.error(f"Unexpected error occurred while extracting metadata from {json_file_path}: {e}")
raise
# Extracts the starting points of triggers in MRI data based on a specified threshold and minimum consecutive points.
def extract_trigger_points(data, threshold, min_consecutive):
"""
Parameters:
- data (numpy.ndarray): The data array from the MRI trigger channel.
- threshold (float): The threshold value above which a data point is considered part of a trigger.
- min_consecutive (int): The minimum number of consecutive data points above the threshold required to confirm a trigger start.
Returns:
- list: A list of indices in the data array where valid trigger starts are detected.
This function is integral to preprocessing fMRI data, where accurate identification of trigger points is essential for aligning physiological data with MRI volumes. It uses a simple thresholding approach combined with a criterion for a minimum number of consecutive points to reduce false positives.
The function first converts the data to a binary sequence based on the threshold, then identifies changes
from 0 to 1 as potential trigger starts. It further checks these starts to ensure they have the specified
minimum number of consecutive points above the threshold.
Usage Example:
trigger_starts = extract_trigger_points(mri_trigger_data, threshold=5, min_consecutive=5)
Note:
- This function assumes the trigger channel data is a 1D numpy array.
- The choice of threshold and min_consecutive parameters may vary based on the specifics of the MRI acquisition and should be validated for each dataset.
"""
logging.info("Extracting trigger points with threshold: %s and minimum consecutive points: %s", threshold, min_consecutive)
# Convert the data points to binary values based on the threshold.
triggers = (data > threshold).astype(int)
logging.info(f"Trigger data converted to binary based on threshold {threshold}")
# Calculate the difference to identify rising edges of the trigger signal.
diff_triggers = np.diff(triggers, prepend=0)
potential_trigger_starts = np.where(diff_triggers == 1)[0]
# Validate trigger starts based on the minimum consecutive points criterion.
valid_trigger_starts = []
for start in potential_trigger_starts:
# Check if the start has the required minimum number of consecutive points above the threshold
if np.sum(triggers[start:start+min_consecutive]) == min_consecutive:
valid_trigger_starts.append(start)
logging.info("Identified %d valid trigger starts", len(valid_trigger_starts))
return valid_trigger_starts
# Finds the index of the next MRI trigger start after a given index in the sequence of trigger starts.
def find_trigger_start(trigger_starts, current_index):
"""
Parameters:
- trigger_starts (numpy.ndarray): An array of indices where MRI trigger starts are detected.
- current_index (int): The current index in the physiological data, used as a reference to find the next trigger start.
Returns:
- int or None: The index of the next trigger start if found; otherwise, None.
This function is essential for processing fMRI physiological data, where identifying the start of each scan run is crucial for accurate data analysis. It works by searching for the nearest index in the trigger_starts array that comes after the current_index.
Usage Example:
next_trigger = find_trigger_start(trigger_starts, current_index)
Note:
- Ensure trigger_starts is a sorted numpy array.
- If no trigger start is found after the current_index, the function returns None, indicating the end of the sequence or missing data.
"""
logging.info(f"Finding trigger start after index {current_index}.")
# Finding the appropriate index in the trigger_starts array.
next_trigger_index = np.searchsorted(trigger_starts, current_index, side='right')
# Verify if the next trigger index is within the bounds of the trigger_starts array.
if next_trigger_index < len(trigger_starts):
next_trigger_start = trigger_starts[next_trigger_index]
logging.info(f"Next trigger start found at index {next_trigger_start}.")
return next_trigger_start
else:
logging.error(f"No trigger start found after index {current_index}.")
return None # Indicate that there are no more triggers to process.
# Segments physiological data into individual fMRI runs based on metadata and trigger starts.
def find_runs(data, all_runs_metadata, trigger_starts, sampling_rate, invalid_runs):
"""
Parameters:
- data (numpy.ndarray): The complete physiological data set.
- all_runs_metadata (dict): Metadata for each run, keyed by run identifiers.
- trigger_starts (list): Indices marking the start of each potential fMRI run.
- sampling_rate (int): The frequency at which data points were sampled.
Returns:
- list of dicts: Each dictionary contains the start and end indices of a run and its metadata.
This function is critical for neuroimaging research where analyses are often conducted on data aligned with individual fMRI runs. It ensures that each run is accurately captured based on the timing information provided in the metadata.
Usage Example:
runs_info = find_runs(data, all_runs_metadata, trigger_starts, sampling_rate)
Note:
- Ensure the data array and all_runs_metadata are correctly formatted and complete.
- The function logs detailed information about the process, facilitating troubleshooting and verification.
"""
runs_info = []
start_from_index = 0
skipped_invalid_runs = set()
for run_id, metadata in sorted(all_runs_metadata.items()):
try:
repetition_time = metadata['RepetitionTime']
num_volumes = metadata['NumVolumes']
logging.info(f"Searching for {run_id} with {num_volumes} volumes and TR={repetition_time}s")
samples_per_volume = int(sampling_rate * repetition_time)
# Continue searching for the next valid run, or skip if it's marked as invalid
while start_from_index < len(trigger_starts):
valid_run_start_index = None
# Find the start index of the next valid run
for i, start in enumerate(trigger_starts[start_from_index:], start_from_index):
if i + num_volumes > len(trigger_starts):
break # Not enough triggers left for the full run
expected_end = trigger_starts[i] + (num_volumes - 1) * samples_per_volume
if trigger_starts[i + num_volumes - 1] <= expected_end:
valid_run_start_index = i
break
if valid_run_start_index is not None:
# Check if the run is marked as invalid and should be skipped
if run_id in invalid_runs and run_id not in skipped_invalid_runs:
logging.info(f"Skipping invalid run {run_id} starting at index {valid_run_start_index}.")
skipped_invalid_runs.add(run_id)
start_from_index = valid_run_start_index + num_volumes
continue
# If it's not invalid, or has already been skipped once, process it
start_idx = trigger_starts[valid_run_start_index]
end_idx = start_idx + num_volumes * samples_per_volume
runs_info.append({
'run_id': run_id,
'start_index': start_idx,
'end_index': end_idx,
'metadata': metadata
})
logging.info(f"Found valid run {run_id} from index {start_idx} to {end_idx}.")
start_from_index = valid_run_start_index + num_volumes
break
else:
logging.warning(f"No more valid trigger sequences found for {run_id} after index {start_from_index}.")
break
except KeyError as e:
logging.error(f"Key error for {run_id}: {e}")
except IndexError as e:
logging.error(f"Index error while processing {run_id}: {e}")
except Exception as e:
logging.error(f"Unexpected error while processing {run_id}: {e}")
logging.info(f"Total valid runs identified: {len(runs_info)}")
if not runs_info:
logging.warning("No valid runs found. Check the trigger starts and metadata.")
return runs_info
# Segments the runs based on the information provided by find_runs() and writes them to output files.
def segment_runs(runs_info, output_dir, metadata_dict, labels, subject_id, session_id):
"""
Parameters:
- runs_info (list): A list of dictionaries, each containing information for a segmented run.
- output_dir (str): Directory where output files will be saved.
- metadata_dict (dict): Additional metadata for all runs.
- labels (list of str): Labels of the data channels.
- subject_id (str): Identifier for the subject.
- session_id (str): Identifier for the session.
Returns:
- list: A list of file paths to the written output files.
This function plays a crucial role in organizing and saving segmented physiological data into individual files,
each corresponding to a specific fMRI run. It's a vital step for further data analysis in neuroimaging studies.
Usage Example:
output_files = segment_runs(runs_info, output_dir, metadata_dict, labels, subject_id, session_id)
Note:
- Ensure that the output directory exists and is writable.
- The function assumes that the data processing method (e.g., filtering, normalization) is defined elsewhere.
"""
output_files = []
for run in runs_info:
run_id = run['run_id']
data = run['data']
start_index = run['start_index']
end_index = run['end_index']
run_metadata = run['run_metadata']
task_name = run_metadata['TaskName'] # Extract the TaskName from the metadata.
logging.info("Segmenting full run %s from index %d to %d", run_id, start_index, end_index)
# Write the processed data to an output file.
try:
# Call the existing write_output_files function with the appropriate parameters.
output_file_path = os.path.join(output_dir, f"{subject_id}_{session_id}_task-learn_{run_id}_physio.tsv.gz")
write_output_files(data, run_metadata, metadata_dict, labels, output_dir, subject_id, session_id, run_id)
output_files.append(output_file_path)
logging.info("Output file for run %s written to %s", run_id, output_file_path)
except IOError as e:
logging.error("Failed to write output file for full run %s: %s", run_id, e, exc_info=True)
raise # Propagate the exception for further handling.
logging.info(f"Completed writing output files for all segmented full runs.")
return output_files
# Creates a metadata dictionary for a run with channel-specific information.
def create_metadata_dict(run_info, sampling_rate, bids_labels_list, units_dict):
"""
Parameters:
- run_info (dict): Information about the run, including start index and run ID.
- sampling_rate (int): The sampling rate of the physiological data.
- bids_labels_list (list): List of BIDS-compliant labels for data channels.
- units_dict (dict): Mapping from BIDS labels to their units.
Returns:
- dict: A dictionary containing metadata for the run.
This function is vital for associating physiological data with its metadata, ensuring the data's integrity
and facilitating its use in research and analysis.
Note:
- The function assumes that the run_info dictionary and units_dict are correctly formatted and provided.
- Channel-specific metadata is predefined and may need updates based on new information or different setups.
Usage Example:
run_metadata = create_metadata_dict(run_info, sampling_rate, bids_labels_list, units_dict)
"""
# Initialize the metadata dictionary with common information.
metadata_dict = {
"RunID": run_info['run_id'],
"SamplingFrequency": sampling_rate, # BIDS label
"SamplingRate": {
"Value": sampling_rate,
"Units": "Hz" # provide units
},
"StartTime": run_info['start_index'] / sampling_rate, # BIDS label
"StartTimeSec": {
"Value": run_info['start_index'] / sampling_rate,
"Description": "Start time of the current run relative to recording onset",
"Units": "seconds" # provide units
},
"StartTimeMin": {
"Value": (run_info['start_index'] / sampling_rate)/60,
"Description": "Start time of the current run relative to recording onset",
"Units": "minutes"
},
"Columns": bids_labels_list,
"Manufacturer": "Biopac",
"Acquisition Software": "Acqknowledge",
}
# Channel-specific metadata.
channel_metadata = {
"cardiac": {
"Description": "Continuous ECG measurement",
"Placement": "Lead 1",
"Gain": 500,
"35HzLPN": "off / 150HzLP",
"HPF": "0.05 Hz",
},
"respiratory": {
"Description": "Continuous measurements by respiration belt",
"Gain": 10,
"LPF": "10 Hz",
"HPF1": "DC",
"HPF2": "0.05 Hz",
},
"eda": {
"Description": "Continuous EDA measurement",
"Placement": "Right plantar instep",
"Gain": 5,
"LPF": "1.0 Hz",
"HPF1": "DC",
"HPF2": "DC",
},
"trigger": {
"Description": "fMRI Volume Marker",
},
"ppg": {
"Description": "Continuous PPG measurement",
"Placement": "Left index toe",
"Gain": 10,
"LPF": "3.0 Hz",
"HPF1": "0.5 Hz",
"HPF2": "0.05 Hz",
}
}
# Add channel-specific metadata to the dictionary if the channel is present.
for channel in channel_metadata:
# Assuming 'channel' is a variable in this function that holds the current channel being processed.
if channel in bids_labels_list:
channel_specific_metadata = channel_metadata[channel]
# Set the 'Units' dynamically based on the units_dict
channel_specific_metadata['Units'] = units_dict.get(channel, "Unknown")
metadata_dict[channel] = channel_specific_metadata
logging.info(f"Full run metadata dictionary created for {run_info['run_id']}")
return metadata_dict
# Creates metadata for an event segment, including run details and specific event information.
def create_event_metadata_dict(run_info, segment_length, sampling_rate, repetition_time,
bids_labels_list, units_dict, trial_type,
segment_start_time, event_onset, segment_duration):
"""
Parameters:
- run_info (dict): Information about the run, including the start index and run ID.
- segment_length (int): Length of the segment in data points.
- sampling_rate (int): Sampling rate of the data in Hz.
- repetition_time (float): Repetition time for the fMRI volumes in seconds.
- bids_labels_list (list): List of BIDS-compliant labels for data channels.
- units_dict (dict): Mapping from BIDS labels to their units.
- trial_type (str): Type of trial, e.g., 'sequence', 'random'.
- segment_start_time (float): Start time of the segment in seconds.
- event_onset (float): Onset time of the event relative to the start of the run.
- segment_duration (float): Duration of the segment in seconds.
Returns:
- dict: A dictionary containing metadata for the event segment.
The function meticulously details each aspect of an event segment, crucial for accurate data analysis
and subsequent research applications. It assumes the input parameters are correctly formatted and provided.
Note:
- This function can be adapted to different experimental setups and data structures.
- The channel-specific metadata may require updates to reflect specific measurement techniques or equipment used.
Usage Example:
event_metadata = create_event_metadata_dict(run_info, segment_length, sampling_rate, repetition_time,
bids_labels_list, units_dict, trial_type,
segment_start_time, event_onset, segment_duration)
"""
# Calculate the number of volumes and the duration of the segment.
num_volumes_events = int(segment_length / (sampling_rate * repetition_time))
segment_duration_min = (segment_length / sampling_rate) / 60
# Logging for debugging and verification.
logging.info(f"Number of volumes covered by '{trial_type}' block: {num_volumes_events}")
#logging.info(f"Segment length: {segment_length} data points")
logging.info(f"The total duration of the '{trial_type}' segment in {run_info['run_id']} is {segment_duration_min} minutes")
#logging.info((run_info['start_index'] / sampling_rate) + event_onset)
# Initialize the metadata dictionary with segment-specific information.
metadata_dict_events = {
"RunID": run_info['run_id'],
"NumVolumes": num_volumes_events,
"RepetitionTime": repetition_time,
"SamplingFrequency": sampling_rate,
"StartTime": (run_info['start_index'] / sampling_rate) + event_onset,
"StartTimeSec": {
"Value": (run_info['start_index'] / sampling_rate) + event_onset,
"Description": "Start time of the segment relative to recording onset",
"Units": "seconds"
},
"StartTimeMin": {
"Value": ((run_info['start_index'] / sampling_rate) + event_onset) / 60,
"Description": "Start time of the segment relative to recording onset",
"Units": "minutes"
},
"DurationMin": {
"Value": segment_duration_min,
"Description": "Duration of the segment",
"Units": "minutes"
},
"TrialType": trial_type, # Include trial type in the metadata.
"Columns": bids_labels_list
}
# Channel-specific metadata.
channel_event_metadata = {
"cardiac": {
"Description": "Continuous ECG measurement",
"Placement": "Lead 1",
"Gain": 500,
"35HzLPN": "off / 150HzLP",
"HPF": "0.05 Hz",
},
"respiratory": {
"Description": "Continuous measurements by respiration belt",
"Gain": 10,
"LPF": "10 Hz",
"HPF1": "DC",
"HPF2": "0.05 Hz",
},
"eda": {
"Description": "Continuous EDA measurement",
"Placement": "Right plantar instep",
"Gain": 5,
"LPF": "1.0 Hz",
"HPF1": "DC",
"HPF2": "DC",
},
"trigger": {
"Description": "fMRI Volume Marker",
},
"ppg": {
"Description": "Continuous PPG measurement",
"Placement": "Left index toe",
"Gain": 10,
"LPF": "3.0 Hz",
"HPF1": "0.5 Hz",
"HPF2": "0.05 Hz",
}
}
# Add channel-specific metadata to the dictionary if the channel is present.
for channel in channel_event_metadata:
# Assuming 'channel' is a variable in this function that holds the current channel being processed.
if channel in bids_labels_list:
channel_specific_event_metadata = channel_event_metadata[channel]
# Set the 'Units' dynamically based on the units_dict.
channel_specific_event_metadata['Units'] = units_dict.get(channel, "Unknown")
metadata_dict_events[channel] = channel_specific_event_metadata
logging.info(f"Event metadata dictionary created for {run_info['run_id']} {trial_type} segment.")
return metadata_dict_events
# Writes segmented data to TSV and JSON files in the Brain Imaging Data Structure (BIDS) format.
def write_output_files(segmented_data, run_metadata, metadata_dict, bids_labels_list, output_dir, subject_id, session_id, run_id):
"""
Parameters:
- segmented_data (numpy.ndarray): The data segmented for a run.
- run_metadata (dict): The metadata for the run.
- metadata_dict (dict): The additional metadata for the run.
- bids_labels_list (list of str): The BIDS-compliant labels of the data channels.
- output_dir (str): The directory to which the files should be written.
- subject_id (str): The identifier for the subject.
- session_id (str): The identifier for the session.
- run_id (str): The identifier for the run.
Raises:
- Exception: If any error occurs during the file writing process, it logs the error and re-raises the exception.
The function creates TSV and JSON files named according to BIDS naming conventions.
It writes the segmented data to a compressed TSV file and the combined metadata to a JSON file.
Usage Example:
write_output_files(segmented_data, run_meta, meta_dict, labels, '/output/path', 'sub-01', 'ses-1', 'run-01')
Example Usage:
write_output_files(segmented_data_array, run_metadata_dict, additional_metadata_dict, channel_labels, '/output/path', 'sub-01', 'sess-01', 'run-01')
"""
try:
# Move up four levels to get to dataset_root_dir
dataset_root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(output_dir))))
#logging.info(f"Dataset root directory: {dataset_root_dir}")
output_derivatives_dir = os.path.join(dataset_root_dir, 'derivatives', 'physio', 'learn', 'runs')
# Ensure the output directories exist
os.makedirs(output_dir, exist_ok=True)
os.makedirs(output_derivatives_dir, exist_ok=True)
# Define filenames based on the BIDS format.
tsv_filename = f"{subject_id}_{session_id}_task-learn_{run_id}_physio.tsv.gz"
json_filename = f"{subject_id}_{session_id}_task-learn_{run_id}_physio.json"
# Prepare the full file paths.
tsv_file_path = os.path.join(output_dir, tsv_filename)
json_file_path = os.path.join(output_dir, json_filename)
# Create a DataFrame with the segmented data and correct labels.
df = pd.DataFrame(segmented_data, columns=bids_labels_list)
# Save the DataFrame to a TSV file with GZip compression.
df.to_csv(tsv_file_path, sep='\t', index=False, compression='gzip')
logging.info(f"TSV file written: {tsv_file_path}")
# Copy the file
shutil.copy2(tsv_file_path, output_derivatives_dir)
# Log the action
logging.info(f"TSV file copied to: {output_derivatives_dir}")
# Merge the run-specific metadata with the additional metadata.
combined_metadata = {**run_metadata, **metadata_dict}
# Write the combined metadata to a JSON file.
with open(json_file_path, 'w') as json_file:
json.dump(combined_metadata, json_file, indent=4)
logging.info(f"JSON file written: {json_file_path}")
# Copy the file
shutil.copy2(json_file_path, output_derivatives_dir)
# Log the action
logging.info(f"JSON file copied to: {output_derivatives_dir}")
except Exception as e:
# Log any exceptions that occur during the file writing process.
logging.error(f"Failed to write output files for {run_id}: {e}", exc_info=True)
raise
# Log the successful writing of files.
logging.info(f"Full length run output files for {run_id} also written successfully to {output_dir}")
# Writes output files (.tsv and .json)for each event segment of physiological data in BIDS format.
def write_event_output_files(event_segments, run_metadata, metadata_dict_events, bids_labels_list, output_dir, subject_id, session_id, run_id):
"""
Parameters:
- event_segments (list of tuples): Each tuple contains a segment of data (numpy array) and its corresponding trial type (str), start time (float), and duration (float).
- run_metadata (dict): Metadata for the run.
- metadata_dict_events (dict): Additional metadata for the events.
- bids_labels_list (list of str): BIDS-compliant labels of the data channels.
- output_dir (str): Directory to write the files.
- subject_id (str): Identifier for the subject.
- session_id (str): Identifier for the session.
- run_id (str): Identifier for the run.
Raises:
- Exception: If an error occurs during file writing.
Notes:
- This function requires 'pandas' for DataFrame operations and 'os', 'json' for file handling.
- It assumes that event_segments contains tuples with exactly four elements: data, trial type, start time, and duration.
- The function creates TSV and JSON files for each event segment, naming them according to the BIDS format and including relevant metadata.
- Error handling is included to manage unexpected segment formats or file writing issues.
Example Usage:
write_event_output_files(event_segments, run_metadata, metadata_dict_events, channel_labels, '/output/path', 'sub-01', 'sess-01', 'run-01')
"""
try:
# Move up four levels to get to dataset_root_dir
dataset_root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(output_dir))))
logging.info(f"Dataset root directory: {dataset_root_dir}")
output_derivatives_dir = os.path.join(dataset_root_dir, 'derivatives', 'physio', 'learn', 'events')
# Ensure the output directories exist
os.makedirs(output_dir, exist_ok=True)
os.makedirs(output_derivatives_dir, exist_ok=True)
# Loop through each segment and write to individual files.
for i, segment_info in enumerate(event_segments):
# Adjust the segment index for logging and naming (starting from 1 instead of 0).
segment_index = i + 1
# Unpack the segment_info tuple.
if len(segment_info) == 4: # Assuming there are four elements in the tuple.
segment_data, trial_type, segment_start_time, segment_duration = segment_info
else:
logging.error(f"Unexpected event segment format for {run_id}: {segment_info}")
continue # Skip this segment.
# Define filenames based on the BIDS format.
tsv_event_filename = f"{subject_id}_{session_id}_task-learn_{run_id}_recording-{trial_type}_physio.tsv.gz"
json_event_filename = f"{subject_id}_{session_id}_task-learn_{run_id}_recording-{trial_type}_physio.json"
# Prepare file paths.
tsv_event_file_path = os.path.join(output_dir, tsv_event_filename)
json_event_file_path = os.path.join(output_dir, json_event_filename)
# Check if segment data is not empty.
if segment_data.size == 0:
logging.warning(f"No data found for event segment {segment_index} of trial type '{trial_type}' in run '{run_id}'. Skipping file writing.")
continue
# Create a DataFrame and save to a TSV file.
df = pd.DataFrame(segment_data, columns=bids_labels_list)
df.to_csv(tsv_event_file_path, sep='\t', index=False, compression='gzip')
logging.info(f"TSV file written: {tsv_event_file_path}")
# Copy the file
shutil.copy2(tsv_event_file_path, output_derivatives_dir)
# Log the action
logging.info(f"TSV file copied to: {output_derivatives_dir}")
# Write the event metadata to a JSON file.
with open(json_event_file_path, 'w') as json_file:
json.dump(metadata_dict_events, json_file, indent=4)
logging.info(f"JSON file written: {json_event_file_path}")
# Copy the file
shutil.copy2(json_event_file_path, output_derivatives_dir)
# Log the action
logging.info(f"JSON file copied to: {output_derivatives_dir}")
# Log the successful writing of files.
logging.info(f"Event segment output files for {run_id}, segment {segment_index}, trial type '{trial_type}' written successfully to {output_dir}")
except Exception as e:
# Log any exceptions during the file writing process.
logging.error(f"Failed to write event output files for {run_id}: {e}", exc_info=True)
raise
# Plots the segmented data for each run and saves the plots to a PNG file.
def plot_runs(original_data, segmented_data_list, runs_info, bids_labels_list, sampling_rate, plot_file_path, units_dict):
"""
Parameters:
- original_data (np.ndarray): The entire original data to be used as a background.
- segmented_data_list (list of np.ndarray): Each element is an array of data for a run.
- runs_info (list): Information about each run, including start and end indices and metadata.
- bids_labels_list (list of str): BIDS-compliant channel labels.
- sampling_rate (int): The rate at which data was sampled.
- plot_file_path (str): The file path to save the plot.
- units_dict (dict): A dictionary mapping labels to their units.
Raises:
- Exception: If an error occurs during plotting.
Notes:
- This function requires 'numpy' for numerical operations and 'matplotlib.pyplot' for plotting.
- It creates a multi-panel plot, each panel representing a channel of the data.
- The original data is plotted as a grey background, and segmented data are overlaid with different colors.
- The function handles the generation of a dynamic legend and ensures proper layout adjustment.
- Error handling includes logging the error and re-raising the exception for further handling.
Example Usage:
plot_runs(original_data_array, segmented_data_list, runs_info_list, channel_labels, 250, '/path/to/save/plot.png', units_dictionary)
"""
try:
# Define a list of colors for different runs.
colors = [
'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black', 'orange',
'pink', 'purple', 'lime', 'indigo', 'violet', 'gold', 'grey', 'brown'
]
# Create a figure and a set of subplots.
fig, axes = plt.subplots(nrows=len(bids_labels_list), ncols=1, figsize=(20, 10))
# Time axis for the original data.
time_axis_original = np.arange(original_data.shape[0]) / sampling_rate / 60
# Plot the entire original data as background.
for i, label in enumerate(bids_labels_list):
unit = units_dict.get(label, 'Unknown unit')
axes[i].plot(time_axis_original, original_data[:, i], color='grey', alpha=0.5, label='Background' if i == 0 else "")
axes[i].set_ylabel(f'{label}\n({unit})')
# Overlay each segmented run on the background.
for segment_index, (segment_data, run_info) in enumerate(zip(segmented_data_list, runs_info)):