def extract_peripheral_features(df): 
    bio = nk.bio_process(ecg= df['ECG'], eda= df['EDA'], sampling_rate= 250)

        
    features = {}
    if (('HF' in bio['ECG']['HRV']) == False):
        return features
    features['CVSD'] = bio['ECG']['HRV']['CVSD']
    features['HF'] = bio['ECG']['HRV']['HF']
    features['HF/P'] = bio['ECG']['HRV']['HF/P']
    features['LF'] = bio['ECG']['HRV']['LF']
    features['LF/HF'] = bio['ECG']['HRV']['LF/HF']
    features['LF/P'] = bio['ECG']['HRV']['LF/P']
    features['LF/n'] = bio['ECG']['HRV']['LFn']
    features['RMSSD'] = bio['ECG']['HRV']['RMSSD']
    features['CVSD'] = bio['ECG']['HRV']['CVSD']
    features['HF'] = bio['ECG']['HRV']['HF']
    features['CVSD'] = bio['ECG']['HRV']['CVSD']
    features['HF'] = bio['ECG']['HRV']['HF']
    features['EDA_mean_amp'] = np.mean(bio['EDA']['SCR_Peaks_Amplitudes'])
#  Divide by length so longer and shorter segments are treated equally (5 peaks in 10 seconds is more than
#  5 peaks in 30 seconds
    features['EDA_num_peaks'] = len(bio['EDA']['SCR_Peaks_Amplitudes']) / len(df)

    return features
    



    
Beispiel #2
0
 def test_bio_process(self):
     df = self.test_read_acqknowledge()
     bio = nk.bio_process(ecg=df["ECG"], rsp=df["RSP"], eda=df["EDA"], sampling_rate=100, add=df["Photosensor"])
     self.assertEqual(len(bio), 3)
     self.assertEqual(len(bio["ECG"]["R_Peaks"]), 499)
     self.assertEqual(len(bio["EDA"]["SCR_Onsets"]), 5)
     return(bio)
Beispiel #3
0
def neuro_ecg(sig, sampling_rate=500):
    _data = None
    for col in range(sig.shape[1])[:1]:
        bio = nk.bio_process(ecg=sig[:, col],
                             sampling_rate=sampling_rate,
                             ecg_hrv_features=None)
        _data = bio['df'] if _data is None else np.hstack(
            (_data, bio['df'].values))

    return np.hstack((sig, _data))
Beispiel #4
0
def test_bio_process():

    df = test_read_acqknowledge()

    if run_tests_in_local is False:  # If travis
        ecg_quality_model = os.getcwd() + r"/neurokit/materials/heartbeat_classification.model"
    else:  # If local
        ecg_quality_model = "default"

    bio = nk.bio_process(ecg=df["ECG"], rsp=df["RSP"], eda=df["EDA"], sampling_rate=100, add=df["Photosensor"], ecg_quality_model=ecg_quality_model, age=24, sex="m", position="supine")

    assert len(bio) == 4
    return(bio)
Beispiel #5
0
def analyzeSCR(event_file, acq_file):
    import neurokit as nk
    import pandas as pd
    import numpy as np
    import seaborn as sns
    ## loading acq file
    df, sampling_rate = nk.read_acqknowledge(acq_file,
                                             return_sampling_rate=True)
    bio = nk.bio_process(eda=df["GSR100C"],
                         add=df["Script"],
                         sampling_rate=sampling_rate)

    # adding conditions
    events = pd.read_csv(event_file, sep=r'\s+')
    condition_list = events['trial_type']
    events = {
        'onset': np.array(events["onset"] * 1000),
        'duration': np.array(events["duration"] * 1000)
    }
    epochs = nk.create_epochs(
        bio["df"], events["onset"], duration=120000,
        onset=0)  # create epoch file with 120sec duration and -1sec begins

    data = {}  # Initialize an empty dict
    for epoch_index in epochs:
        data[epoch_index] = {
        }  # Initialize an empty dict for the current epoch
        epoch = epochs[epoch_index]

        # ECG
        #    baseline = epoch["ECG_RR_Interval"].ix[-100:0].mean()  # Baseline
        #    rr_max = epoch["ECG_RR_Interval"].ix[0:400].max()  # Maximum RR interval
        #    data[epoch_index]["HRV_MaxRR"] = rr_max - baseline  # Corrected for baseline

        # EDA - SCR
        scr_max = epoch["SCR_Peaks"].ix[100:15000].max(
        )  # Maximum SCR peak - now its 30sec after initiation of script
        if np.isnan(scr_max):
            scr_max = 0  # If no SCR, consider the magnitude, i.e.  that the value is 0
        data[epoch_index]["SCR_Magnitude"] = scr_max

    data = pd.DataFrame.from_dict(data,
                                  orient="index")  # Convert to a dataframe
    data["Condition"] = condition_list  # Add the conditions
    data  # Print
    return data
Beispiel #6
0
def getfeatures(df):
    features = [None for i in range(num_features)]
    bio = nk.bio_process(ecg=df[:]['ECG'],rsp=df[:]['RESP'], ecg_quality_model=None, sampling_rate=freq)
    
    # Temperature Features: Mean, STD, Slope, Range, Max, Min
    features[0] = df['TEMP'].mean()
    features[1] = df['TEMP'].std()  
    features[2] = (df['TEMP'].iloc[-1] - df['TEMP'].iloc[0]) / window_size
    features[3] = abs(df['TEMP'].iloc[-1] - df['TEMP'].iloc[0])
    features[4] = df['TEMP'].max()
    features[5] = df['TEMP'].min()
    
    # ECG Heart Rate Statistics:
    features[6] = bio['df']['Heart_Rate'].mean()
    features[7] = bio['df']['Heart_Rate'].std()
    features[8] = bio['df']['Heart_Rate'].mean()
    
    # RSA Statistics:
    features[9] = bio['ECG']['RSA']['RSA_P2T_Mean']
    features[10] = bio['ECG']['RSA']['RSA_P2T_Variability']
    
    # RESP Features
    features[11] = bio['RSP']['Respiratory_Variability']['RSPV_SD']
    features[12] = bio['RSP']['Respiratory_Variability']['RSPV_RMSSD']

    # EMG Features: STD
    features[13] = df['EMG'].std()
    
    # NOTE: NOT ABLE TO USE THESE FEATURES BECAUSE HRV IS NOT ABLE TO BE CALCULATED IN 60 SEC BY NEUROKIT
    # KEEPING HERE TO KNOW THAT THESE FEATURES CAN BE USED TO INCREASE ACCURACY/PERFORMANCE IF IMPLEMENTED SOME OTHER WAY
        # ECG Features: 
    # Heart Rate Variability Properties:
#     features[6] = bio['ECG']['HRV']['HF']
#     features[7] = bio['ECG']['HRV']['HF/P']
#     features[8] = bio['ECG']['HRV']['HFn']
#     features[9] = bio['ECG']['HRV']['LF']
#     features[10] = bio['ECG']['HRV']['LF/HF']
#     features[11] = bio['ECG']['HRV']['LF/P']
#     features[12] = bio['ECG']['HRV']['LFn']
#     features[13] = bio['ECG']['HRV']['Total_Power']
#     features[14] = bio['ECG']['HRV']['Triang']
#     features[15] = bio['ECG']['HRV']['ULF']
#     features[16] = bio['ECG']['HRV']['VHF']
#     features[17] = bio['ECG']['HRV']['VLF']

    return features
Beispiel #7
0
import matplotlib.pyplot as plt

# Download data
df = pd.read_csv(
    "https://raw.githubusercontent.com/neuropsychology/NeuroKit.py/master/examples/Bio/bio_100Hz.csv"
)

# Plot it
df.plot()
plt.title('All the data ploted')
plt.savefig('graphs/alldata')

# Process the signals
bio = nk.bio_process(ecg=df["ECG"],
                     rsp=df["RSP"],
                     eda=df["EDA"],
                     add=df["Photosensor"],
                     sampling_rate=100)

# Plot the processed dataframe, normalizing all variables for viewing purpose
nk.z_score(bio["df"]).plot()
plt.title('Signals plotted')
plt.savefig('graphs/signalsplot')

# Get average quality
print("AVG quality:")
print(bio["ECG"]["Average_Signal_Quality"])

# Plot all the heart beats
pd.DataFrame(bio["ECG"]["Cardiac_Cycles"]).plot(legend=False)
plt.title('Heart beats')
Beispiel #8
0
import pandas as pd
import neurokit as nk
import matplotlib.pyplot as plt
import scipy
import biosppy
import mne
import seaborn as sns




df = pd.read_csv("https://raw.githubusercontent.com/neuropsychology/NeuroKit.py/master/examples/Bio/bio_100Hz.csv")
ecg=df["ECG"]
rsp=df["RSP"]
sampling_rate=100
df = nk.bio_process(ecg=df["ECG"], rsp=df["RSP"], sampling_rate=100)

df["df"].plot()
#sampling_rate=100
#df["df"].plot()
#rpeaks = df["ECG"]["R_Peaks"]

#
#rri = df["ECG_RR_Interval"]
#rri = rri.dropna()
##df.plot()
#
#fbands = {
#      "ULF": [0.0001, 0.0033],
#      "VLF": [0.0033, 0.04],
#      "LF": [0.04, 0.15],
Beispiel #9
0
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import neurokit as nk
import biosppy
import matplotlib.pyplot as plt

df = pd.read_csv('data.csv')
df = df.loc[5000:10000]

sampling_rate = 1000
df = nk.bio_process(ecg=df["ECG"], sampling_rate=sampling_rate)
ecg = df["df"]["ECG_Filtered"]
rpeaks = df["ECG"]["R_Peaks"]

ecg_waves = ecg_wave_detector(ecg, rpeaks)
systole = ecg_systole(ecg, rpeaks, ecg_waves["T_Waves"])
df["df"]["Systole"] = systole

df["df"].plot()
Beispiel #10
0
    plt.xlabel('Data Point Number')
    plt.ylabel('Respiration (%)')
    plt.show()


# ## Extracting Features
# To understand the ECG and RESP signals, we must extract some features out of them. I am using a package called NeuroKit that does exactly that. It extracts information from the ECG and respiration signals to obtain simple values like heart rate, and heart beat positions. It also extracts some more complex values like Respiratory Sinus Arrhythmia, which is a naturally occurring variation in heart rate that occurs during a breathing cycle (inhalation and exhalation). This variation is directly affected by the autonomic nervous system, and thus, by measuring this variation of the heart rate during stress and non-stress conditions, we can capture useful information in classifying the two states.
# 
# To see which data is most useful, I have plotted it for all the subjects and analyzed all the features, as well as gotten their standard deviations and other metrics for analysis. For brevity, I have kept some findings for one subject and have only kept a few example plots that make it easy to visualize the type of things I was looking for in the features.

# In[353]:


# Process Example ECG Signals for Visual Analysis
i = 1
bio = nk.bio_process(ecg=df_respiban[i][:]['ECG'],rsp=df_respiban[i][:]['RESP'], ecg_quality_model=None, ecg_filter_frequency=[1,35], sampling_rate=100)

# Plot the processed dataframe, normalizing all variables for viewing purpose
nk.z_score(bio["df"][1000:-1000]).plot()
plt.show()


# In[551]:


nk.z_score(bio["df"][["ECG_Filtered", "RSP_Filtered", "RSA"]])[1000:2500].plot()
plt.show()


# In[552]:
import neurokit as nk

plt.ion()
"""
Biopac's notice:
The software automatically measures the minimum and maximum R-R intervals during each respiration cycle. Breath-to-breath values are reported for the Cycle number, Time of the cycle, Minimum R-R interval, Maximum R-R interval and the Respiratory Sinus Arrhythmia value (difference between the minimum and maximum R-R intervals) with option to show logarithmic scaling of the result
https://www.biopac.com/?app-advanced-feature=respiratory-sinus-arrhythmia-rsa-analysis
See also this:
http://ieeexplore.ieee.org.sci-hub.cc/document/470252/?reload=true
"""

#==============================================================================
# TEST
#==============================================================================
data = pd.read_csv('clean_rsp.csv')
bio = nk.bio_process(ecg=data["ECG"], rsp=data["RSP"])
df = bio["df"][["ECG_Filtered", "RSP_Raw", "RSA", "RSP_Inspiration"]]
#
## Check RSP cycles onsets
#cycles_onsets = bio["RSP"]["Cycles_Onsets"]
## Plot only the 15s of data
#df[0:15000].plot()
#plt.plot(df.index[cycles_onsets][0:15000],
#         df['RSP_Filtered'][cycles_onsets][0:15000], 'ro')
#
#
#
#
#
## RSA
#rpeaks = bio["ECG"]["R_Peaks"]
Beispiel #12
0
import neurokit as nk
import pandas as pd
import numpy as np
import scipy

df = pd.read_csv("EDA_RSP_Artifacts.csv")  # Sampled at 100Hz
conditions = pd.Series.from_csv("events.csv")

# Preprocessing
df = nk.bio_process(eda=df["EDA"], rsp=df["RSP"], add=df["Photosensor"], sampling_rate=100, scr_min_amplitude=0.02)
df = df["df"]


events = nk.find_events(df["Photosensor"], cut="lower")
epochs = nk.create_epochs(df, events["onsets"], duration=events["durations"]+800, onset=-400)


#==============================================================================
#
#==============================================================================
def eda_ERP(epoch, event_length, sampling_rate=1000, window_post=4):
    """
    Extract event-related EDA and Skin Conductance Response (SCR).

    Parameters
    ----------
    epoch : pandas.DataFrame
        An epoch contains in the epochs dict returned by :function:`nk.create_epochs()` on dataframe returned by :function:`nk.bio_process()`. Index must range from -4s to +4s (relatively to event onset and end).
    event_length : int
        In seconds.
    sampling_rate : int
    PPG= np.genfromtxt('%s_ppg.txt'%nombres[k], delimiter = ',')
    lastvalue=PPG.shape[0]-2500
    first=lastvalue-int(idx[-1])
    PPGcut=PPG[first:lastvalue]
    fecgcut,fppgcut=fil.filtrar(ECGcut,PPGcut)
#    if len(fecgcut)>len(ECGcut):
#        fecgcut=fecgcut[0:len(ECGcut)-1]
#    if len(fppgcut)>len(PPGcut):
#        fppgcut=fecgcut[0:len(PPGcut)-1]  
    if len(fecgcut)>len(fppgcut):
        fecgcut=fecgcut[0:len(fppgcut)]
    else:
        fppgcut=fppgcut[0:len(fecgcut)]
    #obtener picos RR
    t= np.arange(len(fecgcut))/250
    idx_peaksECG = nk.bio_process(ecg = fecgcut, sampling_rate=250)['ECG']['R_Peaks']
    t_RR = t[idx_peaksECG]
    diff=np.diff(t_RR)
    HR=60/diff
    
    idx_ppgsis = (find_peaks(fppgcut,height=0)[0])
    idx_ppgdia= (find_peaks(-fppgcut,height=0)[0])
#    plt.figure()
#    plt.plot(t,fecgcut,c='y')    
#    plt.plot(t,fppgcut,c='g')
#    plt.scatter(t[idx_ppgsis],fppgcut[idx_ppgsis],c='r')
#    plt.scatter(t[idx_ppgdia],fppgcut[idx_ppgdia],c='r')
#    plt.scatter(t_RR,fecgcut[idx_peaksECG],c='b')
    
    pttsis=[]
    pttdia=[]
t_HR = []
t_PPT = []
SBP = []
DBP = []

#########################################################################
for i in range(1000):
    data_PPG, data_ABP, data_ECG = np.genfromtxt(files_csv[i], delimiter=",")
    print(i)
    if len(data_PPG) > 5 * 60 * fr:
        data_ECG = scale(data_ECG)
        if np.min(data_ECG) < -3 and np.max(data_ECG) < 3:
            data_ECG = -data_ECG
        t = (np.arange(len(data_ECG)) / fr)
        idx_peaksECG = nk.bio_process(ecg=data_ECG,
                                      sampling_rate=125)['ECG']['R_Peaks']
        t_RR = t[idx_peaksECG]
        HR = 60 / np.diff(t_RR)
        idx_peaksPPG = []
        idx_peaksDBP = []
        idx_peaksSBP = []
        idx_del = []
        for i in range(len(idx_peaksECG) - 1):

            ran_0 = idx_peaksECG[i]
            ran_1 = idx_peaksECG[i + 1]

            if find_peaks(data_PPG[ran_0:ran_1],
                          distance=ran_1 - ran_0)[0].size > 0:
                idx_peaksPPG.append(
                    int(
Beispiel #15
0
plt.plot(b)

#%% analyze data using NeuroKit
import neurokit as nk
import pandas as pd
import numpy as np
import seaborn as sns

# loading BioPack file
df, sampling_rate = nk.read_acqknowledge(
    '/media/Data/PTSD_KPE/physio_data/raw/kpe1223/scan_2/kpe1223.2_scripts_2017-02-06T08_39_32.acq',
    return_sampling_rate=True)
df[['GSR100C', 'Script']].plot()
# Process the signals
bio = nk.bio_process(eda=df["GSR100C"], add=df["Script"], sampling_rate=1000.)
# Plot the processed dataframe, normalizing all variables for viewing purpose
#%%
condition_list = [
    "Trauma", "Relax", "Sad", "Relax", "Trauma", "Sad", "Relax", "Trauma",
    "Sad"
]


#%%
def analyzeSCR(event_file, acq_file):
    import neurokit as nk
    import pandas as pd
    import numpy as np
    import seaborn as sns
    ## loading acq file
Beispiel #16
0
    t_DBP_cu = []
    t_SBP_cu = []
    for j in idx:

        idx_d = j

        ECG_aux = ECG[idx_a:idx_d]
        PPG_aux = PPG[idx_a:idx_d]

        #        ECG_aux = filtfilt(b_ecg, a_ecg, ECG_aux, method='gust')
        #
        #        #PPG_aux = filtfilt(b_ppg, a_ppg, PPG_aux, method='gust')

        t = (np.arange(len(ECG_aux)) / 250)
        try:
            idx_peaksECG = nk.bio_process(ecg=ECG_aux,
                                          sampling_rate=250)['ECG']['R_Peaks']
            #idx_peaksECG = np.delete(idx_peaksECG, np.where(np.diff(t[idx_peaksECG])>1.1))

            t_RR = t[idx_peaksECG]

            HR = 60 / np.diff(t_RR)

            idx_peaksPPG = []
            idx_del = []

            for i in range(len(idx_peaksECG) - 1):

                ran_0 = idx_peaksECG[i]
                ran_1 = idx_peaksECG[i + 1]

                PPG_peak = (find_peaks(PPG_aux[ran_0:ran_1],
Beispiel #17
0
import seaborn as sns
import biosppy

df = pd.read_csv(
    "https://raw.githubusercontent.com/neuropsychology/NeuroKit.py/master/examples/Bio/data/bio_rest.csv",
    index_col=0)
events = nk.find_events(df["Photosensor"], cut="lower")

df = nk.create_epochs(df,
                      events["onsets"],
                      duration=events["durations"],
                      onset=0)
df = df[0]  # Select the first element of that list.

bio = nk.bio_process(ecg=df["ECG"],
                     rsp=df["RSP"],
                     eda=df["EDA"],
                     add=df["Photosensor"])

bio["df"].plot()

#
pd.DataFrame(bio["ECG"]["Heart_Beats"]).T.plot(
    legend=False)  # Plot all the heart beats

df["ECG"].plot()

biosppy_ecg = dict(biosppy.ecg.ecg(df["ECG"]))
biosppy_ecg["templates"]

templates, r = biosppy.ecg._extract_heartbeats(signal=df["ECG"],
                                               rpeaks=nk.ecg_find_peaks(
Beispiel #18
0
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import neurokit as nk

df = pd.read_csv('data.csv')
signal = df["ECG"].ix[0:5000]
df = nk.bio_process(ecg=signal, sampling_rate=1000)
events = df["ECG"]["Rpeaks"]
#
#
nk.visually_check_events_in_signal(signal, events)
Beispiel #19
0
    def calculartod(self, dt):
        self.ecg30, self.ppg30 = self.bcbB.ret30()
        if len(self.ecg30) > 7000:
            fecgcut, fppgcut = self.fil.filtrar(self.ecg30, self.ppg30)
            if len(fecgcut) > len(fppgcut):
                fecgcut = fecgcut[0:len(fppgcut)]
            else:
                fppgcut = fppgcut[0:len(fecgcut)]

            t = np.arange(len(fecgcut)) / 250
            idx_peaksECG = nk.bio_process(ecg=fecgcut,
                                          sampling_rate=250)['ECG']['R_Peaks']
            t_RR = t[idx_peaksECG]
            diff = np.diff(t_RR)
            HRs = 60 / diff

            HRs[np.isnan(HRs)] = 60

            print(HRs)
            self.HR.append(np.mean(HRs))
            pttsis = []
            pttdia = []
            ind_ppgsis = []
            ind_ppgdia = []
            for ind in range(len(idx_peaksECG) - 1):
                ini = idx_peaksECG[ind]
                fin = idx_peaksECG[ind + 1]

                ##ppg corte
                idx_ppgsis = (find_peaks(
                    fppgcut[ini:fin], height=0, distance=fin - ini)[0] + ini)
                idx_ppgdia = (find_peaks(
                    -fppgcut[ini:fin], height=0, distance=fin - ini)[0] + ini)
                ind_ppgsis.append(idx_ppgsis[0:-1])
                ind_ppgdia.append(idx_ppgdia[0:-1])
                if idx_ppgsis.size > 0:
                    ppgcutpeaksis = fppgcut[idx_ppgsis]
                    ma = np.argmax(ppgcutpeaksis)
                    ptt = np.abs(t[idx_ppgsis[ma]] - t[ini])
                    pttsis.append(np.absolute(ptt))

                if idx_ppgdia.size > 0:
                    ppgcutpeakdia = fppgcut[idx_ppgdia]
                    mi = np.argmin(ppgcutpeakdia)
                    ptt = np.abs(t[ini] - t[idx_ppgdia[mi]])
                    pttdia.append(np.absolute(ptt))

            HR_norm = scale(HRs)
            pttsis_norm = scale(np.log(pttsis))
            pttdia_norm = scale(np.log(pttdia))

            sizeHR = len(HR_norm)
            sizepttsis = len(pttsis_norm)
            sizepttdia = len(pttdia_norm)

            ma = np.min([sizeHR, sizepttsis, sizepttdia])
            HR_norm = HR_norm[0:ma]
            pttsis = pttsis_norm[0:ma]
            pttdia = pttdia_norm[0:ma]
            self.meanpttsis = np.mean(pttsis)
            self.meanpttdia = np.mean(pttdia)
            self.HRnormena = np.mean(HR_norm)
            diaAn = np.mean(self.DiaA)
            sisAn = np.mean(self.SisA)
            aSis, bSis, cSis, aDias, bDias, cDias = self.guesser.predecir(
                self.HRnormena, self.meanpttsis, self.meanpttdia, sisAn, diaAn)
            diaHoy = aDias * self.HRnormena + bDias * self.meanpttdia + cDias * diaAn
            sisHoy = aSis * self.HRnormena + bSis * self.meanpttdia + cSis * sisAn
            if diaHoy < 150 and sisHoy < 250:
                self.DiaA.append(int(diaHoy))
                self.SisA.append(int(sisHoy))
            print(diaHoy)
            print(sisHoy)