Input/Output¶
check_task_exists(subject_dir, task)
¶
Check for the existence of specific task files within a subject directory.
Scans the subject's BIDS folder to identify if any files associated with the given task label have already been generated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subject_dir
|
Path
|
The BIDS subject-level directory (e.g., 'sub-001/') to be searched. |
required |
task
|
str
|
The BIDS task label to search for (e.g., 'rest', 'faceprocessing'). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the directory exists and contains at least one file matching the task pattern; False otherwise. |
Source code in src/rs_bidsify/io.py
cleanup_participants_tsv(missing_ids, out_path)
¶
Remove missing or invalid participants from the participants.tsv file.
Ensures metadata integrity by pruning rows from the tabular participant index that correspond to subjects who were not successfully processed or are missing from the physical BIDS directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
missing_ids
|
list[str]
|
A list of BIDS subject identifiers (e.g., ['sub-01', 'sub-02']) to be removed from the dataset index. |
required |
out_path
|
Path
|
The root directory of the BIDS dataset containing the 'participants.tsv' file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Overwrites the existing 'participants.tsv' with the filtered content. |
Source code in src/rs_bidsify/io.py
read_bids_tsv(tsv_path)
¶
Read a BIDS-compliant TSV file into a pandas DataFrame.
Parses a tab-separated file located at the provided BIDSPath, automatically setting the first column as the index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tsv_path
|
Path
|
The Path object pointing to the target .tsv file. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The loaded data contained in the TSV file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the parsed DataFrame contains zero data columns, indicating either an invalid delimiter (e.g., commas or spaces instead of tabs) or a file missing required metadata properties. |
Notes
This function assumes a standard BIDS structure where files are tab-separated and contain a leading index column alongside at least one accompanying data/metadata column.
Source code in src/rs_bidsify/io.py
read_description_file(file_path)
¶
Read and validate a dataset description file.
Loads the raw text from the specified path and parses it into a validated Pydantic model. Logs a confirmation message upon successful validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
The path to the description file to be read. Must be JSON, YAML, or TOML. |
required |
Returns:
| Type | Description |
|---|---|
DescriptionSpec
|
The validated data model containing the dataset description metadata. |
Notes
This function utilises Pydantic's model_validate for schema enforcement.
If the metadata file structure does not match DescriptionSpec,
a validation error will be raised.
Source code in src/rs_bidsify/io.py
read_description_spreadsheet(sheet_path, sheet_info, sheet_type)
¶
Read and parse specific sheets from a metadata spreadsheet.
Iterates through the provided configuration to load Excel/ODS sheets
into pandas DataFrames. Each entry in sheet_info is passed as
keyword arguments to the underlying pandas reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sheet_path
|
Path
|
The path to the spreadsheet file (e.g., .xlsx, .ods). |
required |
sheet_info
|
dict[str, Any]
|
A mapping where keys are labels (e.g., 'datasheet', 'codebook')
and values are dictionaries of parameters for |
required |
sheet_type
|
str
|
A descriptive label for the data being loaded (e.g., 'participant'), used primarily for logging. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, DataFrame]
|
A dictionary where keys match |
Source code in src/rs_bidsify/io.py
read_eeg_recording(recording_path)
¶
Read a raw EEG recording and normalize its measurement date.
Loads the EEG data from the specified path and updates the internal measurement date to the current date in UTC format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recording_path
|
Path
|
The file path to the raw EEG recording. |
required |
Returns:
| Type | Description |
|---|---|
BaseRaw
|
The loaded MNE Raw object with the updated measurement date. |
Notes
This function utilizes the read_raw helper, which automatically
detects the appropriate MNE reader based on file extension.
Source code in src/rs_bidsify/io.py
rollback_recording_files(subject_dir, recording)
¶
Remove partial or corrupted files following an export failure.
Provides an automated cleanup mechanism to prevent data pollution. If a specific condition is provided, it targets only files associated with that task; otherwise, it removes the entire subject directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subject_dir
|
Path
|
The BIDS subject-level directory (e.g., 'sub-001/') where the failed export occurred. |
required |
recording
|
RecordingMetadata
|
Metadata for the failed recording, used to identify specific task labels and provide context for logging. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Deletes files or directories from the filesystem and logs the outcome. |
Source code in src/rs_bidsify/io.py
write_bids_tsv(tsv_path, tsv_df)
¶
Write a pandas DataFrame to a BIDS-compliant TSV file.
Exports the provided data to disk at the location specified by the BIDSPath, ensuring the use of tab separators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tsv_path
|
Path
|
The Path object defining the destination for the .tsv file. |
required |
tsv_df
|
DataFrame
|
The DataFrame containing the data to be written. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Writes the TSV file to disk. |
Source code in src/rs_bidsify/io.py
write_enriched_sidecar(bids_path, updates)
¶
Update the JSON sidecar file associated with a BIDS EEG recording.
Constructs the correct path for the EEG sidecar file by modifying the extension and suffix of the provided BIDSPath, then applies specified metadata updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bids_path
|
BIDSPath
|
The BIDSPath object corresponding to the recording. |
required |
updates
|
dict[str, Any]
|
A dictionary of key-value pairs to be added or updated in the target JSON sidecar. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Modifies the sidecar JSON file on disk. |
Notes
This function internally calls update_sidecar_json and assumes a
standard BIDS suffix of 'eeg' and extension '.json'.
Source code in src/rs_bidsify/io.py
write_phenotype_data(phenotype_data, root_path, missing_ids)
¶
Write filtered phenotype data and associated codebooks to the BIDS dataset.
Creates a 'phenotype' directory in the root path. Before exporting, it prunes the phenotype dataset to exclude any participants identified in 'missing_ids', ensuring the metadata remains synchronized with the available EEG recordings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phenotype_data
|
dict[str, DataFrame]
|
A dictionary containing the phenotype information. Must include 'dataset' (the actual values) and 'codebook' (metadata) as DataFrames. |
required |
root_path
|
Path
|
The root directory of the BIDS dataset where the '/phenotype' folder will be created. |
required |
missing_ids
|
list[str]
|
A list of subject identifiers to be filtered out of the phenotype dataset before writing to disk. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Writes 'phenotype.tsv' and 'phenotype.json' to the filesystem. |
Notes
The codebook is exported using a JSON 'index' orientation to map variable names to their respective descriptions and metadata, aligning with BIDS recommendations for sidecar files.