Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions code/hrv/hrv_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
" - physio_type (str, optional): If 'bbsig_preproc=True', type of physiological signal used. Default is 'ecg', can also be 'ppg'.\n",
"\n",
" Returns: \n",
" - A dictionary with two keys: 'RRI' (array of RR intervals in ms); 'RRI_Time' (array of cumulative R-peaks timestamps in ms). \n",
" - A dictionary with two keys: 'RRI' (array of RR intervals in ms); 'RRI_Time' (array of cumulative R-peaks timestamps in seconds). \n",
" \"\"\"\n",
"\n",
" ############## Data loading: BBSIG preprocessing files ##############\n",
Expand Down Expand Up @@ -288,15 +288,18 @@
"\n",
" # Convert RR intervals from seconds to milliseconds, if necessary\n",
" if rri_unit == 's':\n",
" rr_s = rr_s*1000 \n",
" \n",
" rr_ms = rr_s * 1000\n",
" else:\n",
" rr_ms = rr_s\n",
"\n",
" # Convert data into appropriate formats for NeuroKit2\n",
" peaks = np.cumsum(rr_s) # define R-peaks as the cumulative sum of RR intervals\n",
" # NeuroKit2 expects RRI in milliseconds and RRI_Time in seconds.\n",
" peaks_s = np.cumsum(rr_ms) / 1000 # R-peak timestamps in seconds\n",
"\n",
" # Store data for participant in a dictionary\n",
" rrs_dict = {\n",
" 'RRI': np.array(rr_s), # RR intervals in miliseconds\n",
" 'RRI_Time': peaks} # R-peaks timestamps in seconds \n",
" 'RRI': np.array(rr_ms), # RR intervals in milliseconds\n",
" 'RRI_Time': peaks_s} # R-peaks timestamps in seconds\n",
" \n",
" return rrs_dict"
]
Expand Down Expand Up @@ -324,7 +327,7 @@
"def hrv_window_crop(rrs_dict, window_start=0, window_end=None, window_length=None):\n",
" \"\"\" Crop RR interval time series to a specified time window for HRV analysis.\n",
" Parameters:\n",
" - rrs_dict (dict): Dictionary storing RR intervals in ms R-peaks timestamps in ms, generated by load_rr_data().\n",
" - rrs_dict (dict): Dictionary storing RR intervals in ms and R-peaks timestamps in seconds, generated by load_rr_data().\n",
" - window_start (float): Start time of the window to be cropped (in seconds).\n",
" - window_end (float, optional): End time of the window to be cropped (in seconds).\n",
" - window_length (float, optional): Length of the window to be cropped (in seconds).\n",
Expand Down Expand Up @@ -355,7 +358,7 @@
"\n",
" # Selected the R-peaks comprised between window_start and window_end\n",
" rrs_dict['RRI_Time'] = rrs_dict['RRI_Time'].tolist()\n",
" rrt_cropped = [x for x in rrs_dict['RRI_Time'] if x>=window_start*1000 and x<=window_end*1000]\n",
" rrt_cropped = [x for x in rrs_dict['RRI_Time'] if x >= window_start and x <= window_end]\n",
"\n",
" # Get indices of window_start and window_end and extract RR interval durations comprised in-between\n",
" window_start_idx = rrs_dict['RRI_Time'].index(rrt_cropped[0])\n",
Expand Down Expand Up @@ -457,7 +460,10 @@
" # Calculate Frequency-domain HRV metrics using NeuroKit2\n",
" # Using Welch method and interpolation at 4Hz by default\n",
" # If plot is needed, set show=True\n",
" hrv_f = nk.hrv_frequency(rrs_dict, interpolation_rate=interpolation_freq, \n",
" # ulf=(0, 0) disables the ULF band, which otherwise returns spurious values\n",
" # for short (<5 min) recordings and dominates the PSD plot.\n",
" hrv_f = nk.hrv_frequency(rrs_dict, interpolation_rate=interpolation_freq,\n",
" ulf=(0, 0),\n",
" normalize=False, psd_method='welch', show=show_plots) \n",
" \n",
" # Extract and format metrics\n",
Expand Down
2 changes: 1 addition & 1 deletion code/preprocessing/ecg_preproc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@
"\n",
" # Interpolate HR values using Systole heart_rate()\n",
" # Hot fix @April 3rd, 2025: removed sfreq=sfreq from arguments due to bug with Systole mis-calculating HR (until it will be fixed)\n",
" hr_bpm, hr_time = heart_rate(x=rr_tmp, unit='bpm', kind=interpol_type, input_type='rr_s') \n",
" hr_bpm, hr_time = heart_rate(x=rr_tmp, output_unit='bpm', kind=interpol_type, input_type='rr_s') \n",
"\n",
" # Define directory for TSV file with interpolated HR values\n",
" hr_tsv_fname = f'{bids_base_fname}_hr-bpm-{rr_type}.tsv.gz'\n",
Expand Down