neuropsychology / NeuroKit

NeuroKit2: The Python Toolbox for Neurophysiological Signal Processing
https://neuropsychology.github.io/NeuroKit
MIT License
1.47k stars 398 forks source link

ValueError: cannot convert float NaN to integer #1006

Open Khaled-Mohammed-Abdelgaber opened 2 weeks ago

Khaled-Mohammed-Abdelgaber commented 2 weeks ago

i am trying to get the peak of ecg signals. and for some signals i get the following error: Cell In[7], line 42 39 Beat_loc = correctPeaks(Beat_loc, signal, 30) 41 if sign_name == 'ecg': ---> 42 minLoc = nk.ecg_process(signal , sampling_rate = 125, method='neurokit')[1]['ECG_R_Peaks'] 43 Beat_loc = minLoc[1:-1] 44 Beat_loc = correctPeaks(Beat_loc, signal, 30)

File c:\Users\Khalid\anaconda3\envs\tf_gpu\lib\site-packages\neurokit2\ecg\ecg_process.py:111, in ecg_process(ecg_signal, sampling_rate, method) 106 rate = signal_rate( 107 info, sampling_rate=sampling_rate, desired_length=len(ecg_cleaned) 108 ) 110 # Assess signal quality --> 111 quality = ecg_quality( 112 ecg_cleaned, rpeaks=info["ECG_R_Peaks"], sampling_rate=sampling_rate 113 ) 115 # Merge signals in a DataFrame 116 signals = pd.DataFrame( 117 { 118 "ECG_Raw": ecg_signal, (...) 122 } 123 )

File c:\Users\Khalid\anaconda3\envs\tf_gpu\lib\site-packages\neurokit2\ecg\ecg_quality.py:105, in ecg_quality(ecg_cleaned, rpeaks, sampling_rate, method, approach) 103 # Run peak detection algorithm 104 if method in ["averageqrs"]: --> 105 quality = _ecg_quality_averageQRS( 106 ecg_cleaned, rpeaks=rpeaks, sampling_rate=sampling_rate 107 ) 108 elif method in ["zhao2018", "zhao", "SQI"]: 109 if approach is None:

File c:\Users\Khalid\anaconda3\envs\tf_gpu\lib\site-packages\neurokit2\ecg\ecg_quality.py:136, in _ecg_quality_averageQRS(ecg_cleaned, rpeaks, sampling_rate) 133 rpeaks = rpeaks["ECG_R_Peaks"] 135 # Get heartbeats --> 136 heartbeats = ecg_segment(ecg_cleaned, rpeaks, sampling_rate) 137 data = epochs_to_df(heartbeats).pivot( 138 index="Label", columns="Time", values="Signal" 139 ) 140 data.index = data.index.astype(int)

File c:\Users\Khalid\anaconda3\envs\tf_gpu\lib\site-packages\neurokit2\ecg\ecg_segment.py:64, in ecg_segment(ecg_cleaned, rpeaks, sampling_rate, show, **kwargs) 59 raise ValueError("The data length is too small to be segmented.") 61 epochs_start, epochs_end, average_hr = _ecg_segment_window( 62 rpeaks=rpeaks, sampling_rate=sampling_rate, desired_length=len(ecg_cleaned) 63 ) ---> 64 heartbeats = epochs_create( 65 ecg_cleaned, 66 rpeaks, 67 sampling_rate=sampling_rate, 68 epochs_start=epochs_start, 69 epochs_end=epochs_end, 70 ) 72 # Pad last heartbeats with nan so that segments are equal length 73 last_heartbeat_key = str(np.max(np.array(list(heartbeats.keys()), dtype=int)))

File c:\Users\Khalid\anaconda3\envs\tf_gpu\lib\site-packages\neurokit2\epochs\epochs_create.py:164, in epochs_create(data, events, sampling_rate, epochs_start, epochs_end, event_labels, event_conditions, baseline_correction) 162 # Find the maximum numbers of samples in an epoch 163 parameters["duration"] = list(np.array(parameters["end"]) - np.array(parameters["start"])) --> 164 epoch_max_duration = int(max((i sampling_rate for i in parameters["duration"]))) 166 # Extend data by the max samples in epochs NaN (to prevent non-complete data) 167 length_buffer = epoch_max_duration

ValueError: cannot convert float NaN to integer

welcome[bot] commented 2 weeks ago

Hi 👋 Thanks for reaching out and opening your first issue here! We'll try to come back to you as soon as possible. ❤️ kenobi

DerAndereJohannes commented 2 weeks ago

Are you sure that your input signal itself does not contain any NaN values? Assuming your input variable signal is a pd.Series, You can check by doing something like: print(signal.isna().sum()) to see if there are any.