biopsykit.io.ecg module¶
I/O functions for files related to ECG processing.
- biopsykit.io.ecg.load_hr_phase_dict(file_path, assert_format=True)[source]¶
Load Excel file containing time series heart rate data of one subject.
The returned dictionary will be a
HeartRatePhaseDict, i.e., a dict with heart rate data from one subject split into phases (as exported bywrite_hr_phase_dict()orwrite_hr_phase_dict_csv()).- Parameters
- Returns
Dict with heart rate data split into phases
- Return type
- Raises
ValidationError – if file in
file_pathis not aHeartRatePhaseDict(ifassert_formatisTrue)FileExtensionError – if file is no Excel file (.xls or .xlsx)
See also
HeartRatePhaseDictDictionary format
write_hr_phase_dictWrite
HeartRatePhaseDictto file
- biopsykit.io.ecg.load_hr_phase_dict_csv(folder_path, file_pattern, phase_order=None, assert_format=True)[source]¶
Load csv file with time series HR data of one subject from folder and combine it into a
HeartRatePhaseDict.The returned dictionary will be a
HeartRatePhaseDict, i.e., a dict with heart rate data from one subject split into phases (as exported bywrite_hr_phase_dict()).- Parameters
folder_path (
Pathor str) – folder path to export csv filesfile_pattern (
Pathor str) – file pattern of the csv files. file_pattern must include a regex capture group (see Examples) which is used to extract the phase name from the file name.phase_order (list of str, optional) – list of phase names to order resulting
HeartRatePhaseDictorNoneto order phases according to the file name ordering infolder_path. Default:Noneassert_format (bool, optional) – whether to check if the imported dict is in the right format or not
- Returns
Dict with heart rate data split into phases
- Return type
- Raises
ValidationError – if file in
file_pathis not aHeartRatePhaseDict(ifassert_formatisTrue)FileExtensionError – if file is no Excel file (.xls or .xlsx)
See also
HeartRatePhaseDictDictionary format
write_hr_phase_dict_csvWrite
HeartRatePhaseDictto a series of csv files
- biopsykit.io.ecg.load_hr_phase_dict_folder(base_path, filename_pattern, subfolder_pattern=None)[source]¶
Load a folder with multiple
HeartRatePhaseDictand concatenate them into aHeartRateSubjectDataDict.This functions looks for all files that match the
file_patternin the folder specified bybase_pathand loads the files that are all expected to beHeartRatePhaseDict.Subject IDs are extracted from the file name. Hence,
file_patternneeds to be a regex including a capture group, e.g. “ecg_results_(\w+).xlsx”.Alternatively, if the files are stored in subfolders, the name pattern of these subfolders can be specified by
subject_folder_pattern. Then, it is expected that the subfolder names correspond to the subject IDs.The returned dictionary will be a
HeartRateSubjectDataDictwith the following format:{
subject_id:HeartRatePhaseDict}- Parameters
base_path – path to top-level folder containing all subject folders
filename_pattern (str) – filename pattern of exported
HeartRatePhaseDict. Must be a regex string with capture group to extract subject IDs, or a regular regex string (without capture group) ifsubfolder_patternis specifiedsubfolder_pattern (str, optional) – subfolder name pattern if files are stored in subfolders. Then,
filename_patterndoes not need to be a regex with a capture group because it is assumed that the names of the subfolders correspond to the subject IDs.
- Returns
HeartRateSubjectDataDict, i.e., a dictionary withHeartRatePhaseDictof multiple subjects- Return type
- Raises
ValidationError – if any file that matches
filename_patternis not aHeartRatePhaseDictFileNotFoundError – if no files match
filename_patternor no subfolders matchsubfolder_pattern
See also
get_subject_dirsFilter for subject subfolders in a given folder
load_hr_phase_dictLoad
HeartRatePhaseDictfor one subject
Examples
>>> from biopsykit.io.ecg import load_hr_phase_dict_folder >>> base_path = "./ecg_results/" >>> # Option 1: all files are stored in `base_path`, subject IDs are extracted from the file names >>> dict_hr_subjects = load_hr_phase_dict_folder( >>> base_path, >>> filename_pattern=r"ecg_result_(\\w+).xlsx") >>> print(dict_hr_subjects) { 'Vp01': {}, # one single HeartRatePhaseDict 'Vp02': {}, # ... } >>> # Option 2: files are stored in subfolders, the name of the subfolders is the corresponding subject ID >>> dict_hr_subjects = load_hr_phase_dict_folder( >>> base_path, >>> filename_pattern=r"ecg_result*.xlsx", >>> subfolder_pattern="Vp*") >>> print(dict_hr_subjects) { 'Vp01': {}, # one single HeartRatePhaseDict 'Vp02': {}, # ... }
- biopsykit.io.ecg.write_hr_phase_dict(hr_phase_dict, file_path)[source]¶
Write
HeartRatePhaseDictto an Excel file.The
HeartRatePhaseDictis a dictionary with heart rate time series data split into phases.Each of the phases in the dictionary will be a separate sheet in the Excel file.
- Parameters
hr_phase_dict (
HeartRatePhaseDict) – aHeartRatePhaseDictcontaining pandas dataframes with heart rate datafile_path (
Pathor str) – path to export file
See also
HeartRatePhaseDictDictionary format
load_hr_phase_dictLoad
HeartRatePhaseDictwritten to filewrite_pandas_dict_excelWrite dictionary with pandas dataframes to Excel file
- biopsykit.io.ecg.write_hr_phase_dict_csv(hr_phase_dict, folder_path, file_pattern)[source]¶
Write
HeartRatePhaseDictto a series of csv files.The
HeartRatePhaseDictis a dictionary with heart rate time series data split into phases.Each of the phases in the dictionary will be a separate csv file.
- Parameters
hr_phase_dict (
HeartRatePhaseDict) – aHeartRatePhaseDictcontaining pandas dataframes with heart rate datafolder_path (
Pathor str) – folder path to export csv filesfile_pattern (
Pathor str) – file pattern to save. The file pattern must include a placeholder “{}” which will be filled with the name of the phase.
- Raises
ValueError – if
file_patterndoes not include a placeholder “{}” that can be filled with the phase nameValidationError – if
hr_phase_dictis not aHeartRatePhaseDictFileExtensionError – if
file_patternis no csv file
See also
HeartRatePhaseDictDictionary format
load_hr_phase_dict_csvLoad
HeartRatePhaseDictwritten to csv files