end = time_end - ((j) * SPLIT_TIME)
                    start = time_end - ((j + 1) * SPLIT_TIME) - EXTENTION_TIME

                    resp = resp_data[(resp_data.iloc[:, 0].values >= start) & (
                        resp_data.iloc[:, 0].values <= end)]["resp"].values
                    ecg_resp = ecg_resp_data[
                        (ecg_resp_data.iloc[:, 0].values >= start) &
                        (ecg_resp_data.iloc[:, 0].values <= end)]["ecg"].values

                    status = 0
                    # extract ecg features
                    time_domain = ecg_features_exct.extractTimeDomain(ecg_resp)
                    freq_domain = ecg_features_exct.extractFrequencyDomain(
                        ecg_resp)
                    nonlinear_domain = ecg_features_exct.extractNonLinearDomain(
                        ecg_resp)
                    # extract resp features
                    resp_time_features = resp_features_exct.extractTimeDomain(
                        resp)
                    resp_freq_features = resp_features_exct.extractFrequencyDomain(
                        resp)
                    resp_nonlinear_features = resp_features_exct.extractNonLinear(
                        resp)
                    if resp_time_features.shape[
                            0] != 0 and resp_freq_features.shape[
                                0] != 0 and resp_nonlinear_features.shape[
                                    0] != 0 and time_domain.shape[
                                        0] != 0 and freq_domain.shape[
                                            0] != 0 and nonlinear_domain.shape[
                                                0] != 0:
                        resp_features = np.concatenate([
    tdelta = data_EmotionTest.iloc[i]["Time_End"] - data_EmotionTest.iloc[i]["Time_Start"]
    time_end = data_EmotionTest.iloc[i]["Time_End"]
    valence = data_EmotionTest.iloc[i]["Valence"]
    arousal = data_EmotionTest.iloc[i]["Arousal"]
    emotion = data_EmotionTest.iloc[i]["Emotion"]

    for j in np.arange(0, (tdelta // split_time), 0.4):
        end = time_end - (j * split_time)
        start = time_end - ((j + 1) * split_time)
        ecg = data[(data["timestamp"].values >= start) & (data["timestamp"].values <= end)]
        status = 0

        # extract ecg features
        time_domain = featuresExct.extractTimeDomain(ecg['ecg'].values)
        freq_domain = featuresExct.extractFrequencyDomain(ecg['ecg'].values)
        nonlinear_domain = featuresExct.extractNonLinearDomain(ecg['ecg'].values)
        if time_domain.shape[0] != 0 and freq_domain.shape[0] != 0 and nonlinear_domain.shape[0] != 0:
            featuresEachMin = np.concatenate([time_domain, freq_domain, nonlinear_domain])
            if np.sum(np.isinf(featuresEachMin)) == 0 | np.sum(np.isnan(featuresEachMin)) == 0:
                np.save(path_results + "ecg_" + str(idx) + ".npy", featuresEachMin)
                status = 1
            else:
                status = 0

        # add object to dataframes
        emotionTestResult = emotionTestResult.append(
            {"Idx": str(idx), "Start": str(start), "End": str(end), "Valence": valence, "Arousal": arousal, "Emotion": emotion, "Status": status},
            ignore_index=True)
        idx += 1

# save to csv
Example #3
0
class DataFetchRoad:

    def __init__(self, gps_file, ecg_file, mask_file, ecg_n=45, split_time = 45, stride=0.2):

        self.gps_file = gps_file
        self.ecg_file = ecg_file
        self.mask_file = mask_file
        self.ecg_n = ecg_n
        self.stride = stride

        self.featuresExct = ECGFeatures(FS_ECG)

        # normalization ecg features
        self.ecg_mean = np.array([8.69006926e+02, 5.36894839e+01, 5.13280806e+01, 3.60941889e+01,
       5.12644394e+01, 3.50184864e+01, 7.05304536e+01, 4.00354951e+00,
       1.93190808e+01, 2.37990616e+01, 4.74128624e+01, 1.79100193e+01,
       3.61127269e+01, 8.89897875e+00, 1.83440847e+01, 9.47694202e-03,
       2.99294977e-02, 8.32468992e-02, 2.66982160e-01, 1.05872866e+02,
       8.24120855e+02, 1.61071678e+03, 2.27217251e+03, 3.79534713e+00,
       2.62838417e+01, 3.42443566e+01, 3.56764545e+01, 3.36520073e+00,
       5.63147720e+00, 6.03246072e+00, 6.01429266e+00, 5.03771520e+01,
       4.96228480e+01, 1.88115762e+00, 1.67060736e+00, 1.47731287e-01,
       3.62288239e+01, 6.01129089e+01, 2.21647750e+00, 1.17607048e+04,
       4.32725792e-01])

        self.ecg_std = np.array([1.12849622e+02, 7.22749812e+00, 4.13239686e+01, 3.76508218e+01,
       6.06763869e+01, 4.94652050e+01, 9.08751400e+00, 2.91083561e+00,
       1.74801557e+01, 1.09281612e+01, 2.35429989e+01, 1.10034329e+01,
       2.36269451e+01, 9.38428850e+00, 2.01946583e+01, 1.47242980e-03,
       1.22646518e-02, 2.66829649e-02, 7.71102801e-02, 8.09024660e+02,
       3.84167641e+03, 1.11956783e+04, 1.61020832e+04, 3.85171123e+00,
       1.77196093e+01, 1.81367242e+01, 2.12639285e+01, 1.62165833e+00,
       1.37502961e+00, 1.34862368e+00, 1.51030700e+00, 2.27359887e+01,
       2.27359887e+01, 2.64173948e+00, 5.57174922e-01, 8.24678134e-02,
       4.28983320e+01, 4.14642962e+01, 9.86755656e-01, 4.09682862e+04,
       3.83966021e-01])

        self.data_set = self.readData()
        self.test_n = len(self.data_set)


    def fetch(self):
        i = 0
        while i < len(self.data_set):
            data_i = self.data_set[i]
            yield data_i
            i+=1

    def readData(self):
        data_set = []
        gps_data = pd.read_csv(self.gps_file)
        ecg_data = pd.read_csv(self.ecg_file)

        ecg_data.loc[:, 'timestamp'] = ecg_data.loc[:, 'timestamp'].apply(timeToInt)
        gps_data.loc[:, 'timestamp'] = gps_data.loc[:, 'timestamp'].apply(timeToInt)

        for j in range(1, len(gps_data)):
            start = gps_data.loc[j]["timestamp"]
            end = start + (SPLIT_TIME+4)
            ecg = ecg_data[(ecg_data["timestamp"].values >= start) & (ecg_data["timestamp"].values <= end)]["ecg"].values
            # print(len(ecg))
            if len(ecg) >= self.ecg_n:
                ecg = ecg[:self.ecg_n]

                #extract ECG features
                time_domain = self.featuresExct.extractTimeDomain(ecg)
                freq_domain =  self.featuresExct.extractFrequencyDomain(ecg)
                nonlinear_domain =  self.featuresExct.extractNonLinearDomain(ecg)
                concatenate_features = (np.concatenate([time_domain, freq_domain, nonlinear_domain]) - self.ecg_mean) / self.ecg_std
                data_set.append(concatenate_features)

                #raw ecg
                # ecg = (ecg - 2140.397356669409) / 370.95493558685325
                # ecg = (ecg - 2048.485947046843) / 156.5629266658633
                # ecg = ecg / (4095 - 0)
                # ecg = signal.resample(ecg, 200 * SPLIT_TIME)
                # data_set.append(ecg)
            # print(ecg)
        return data_set