def get_delta_ap_en(ts):
    d2_coefficients = pywt.downcoef('d', ts, 'db4', level=2)
    surrogate_coefficients = generate_surrogate_series(d2_coefficients)
    app_entropy_sample = entropy.app_entropy(d2_coefficients,
                                             order=2,
                                             metric='chebyshev')
    app_entropy_surrogate = entropy.app_entropy(surrogate_coefficients,
                                                order=2,
                                                metric='chebyshev')

    # Return the delta
    delta_ap_en = app_entropy_surrogate - app_entropy_sample
    return delta_ap_en.item()
Exemple #2
0
def ApEn(RR_windows, **kwargs):
    feat = []
    for wRR in RR_windows:
        try:
            value = entropy.app_entropy(wRR, order=2, metric='chebyshev')
        except:
            value = np.nan
        feat.append(value)
    return feat
Exemple #3
0
 def test_app_entropy(self):
     ae = app_entropy(RANDOM_TS, order=2)
     ae_eu_3 = app_entropy(RANDOM_TS, order=3, metric='euclidean')
     # Compare with MNE-features
     self.assertEqual(np.round(ae, 3), 2.075)
     self.assertEqual(np.round(ae_eu_3, 3), 0.956)
     app_entropy(RANDOM_TS, order=3)
     with self.assertRaises(ValueError):
         app_entropy(RANDOM_TS, order=2, metric='wrong')
 def test_app_entropy(self):
     ae = app_entropy(RANDOM_TS, order=2)
     ae_eu_3 = app_entropy(RANDOM_TS, order=3, metric='euclidean')
     # Compare with MNE-features
     # Note that MNE-features uses the sample standard deviation
     # np.std(ddof=1) and not the population standard deviation to define r
     self.assertEqual(np.round(ae, 3), 2.076)
     self.assertEqual(np.round(ae_eu_3, 3), 0.956)
     app_entropy(RANDOM_TS, order=3)
     with self.assertRaises(ValueError):
         app_entropy(RANDOM_TS, order=2, metric='wrong')
    def createEntropyFeatureArray(self, epochSeries : pd.Series, samplingFreq : int) -> (np.ndarray, List[str]):
        ''' Creates 3d Numpy with a entropy features - also returns the feature names
        
        Creates the following features:
            - Approximate Entropy (AE)
            - Sample Entropy (SamE)
            - Spectral Entropy (SpeE)
            - Permutation Entropy (PE)
            - Singular Value Decomposition Entropy (SvdE)

        For each channel there are 5 features then

        NaN Values will be set to Zero (not good but it works for now)

        '''
        # Create np array, where the data will be stored
        d1 = len(epochSeries) # First Dimesion
        d2 = 1 # only one sample in that epoch
        
        channels = len(epochSeries[0].columns)
        d3 = channels * 5 # second dimension - 5 because we calculate five different entropies for each channel
        
        entropyFeatureArrayX = createEmptyNumpyArray(d1, d2, d3)
        
        # Create a list where all feature names are stored
        entropyFeatureList = [None] * d3
        
        stepSize = 5 # step is 5 because we calculate 5 different entropies
        for i in range (0, len(epochSeries)): # loop through the epochs
            
            # We start the the stepz size and loop through the columns, but we have to multiply by the stepzsize and add once the step size (because we don't start at 0)
            for j in range(stepSize, (len(epochSeries[i].columns)*stepSize)+stepSize, stepSize): # loop through the columns
                
                # j_epoch is the normalized index for the epoch series (like the step size would be 1)
                j_epoch = j/stepSize - 1
                
                # get the column name
                col = epochSeries[i].columns[j_epoch]
                
                # The values of the epoch of the current column
                colEpochList = epochSeries[i][col].tolist()
                
                ######################################
                # calculate Approximate Entropy
                # ------------------------------------
                val = entropy.app_entropy(colEpochList, order=2)
                # if the value is NaN, just set it to 0
                if np.isnan(val):
                    val = 0
                entropyFeatureArrayX[i][0][j-1] = val
                
                # add approximate entropy feature to the list
                entropyFeatureList = addFeatureToList(featureList = entropyFeatureList,
                                                    featureListIndex = j-1,
                                                    newFeatureName = "{col}_approximate_entropy".format(col=col))
                
                ######################################
                # calculate Sample Entropy
                # ------------------------------------
                val = entropy.sample_entropy(colEpochList, order=2)
                # if the value is NaN, just set it to 0
                if np.isnan(val):
                    val = 0
                entropyFeatureArrayX[i][0][j-2] = val
                
                entropyFeatureList = addFeatureToList(featureList = entropyFeatureList,
                                                    featureListIndex = j-2,
                                                    newFeatureName = "{col}_sample_entropy".format(col=col))
                
                ######################################
                # calculate Spectral Entropy
                # ------------------------------------
                val = entropy.spectral_entropy(colEpochList, sf=samplingFreq ,method='fft', normalize=True)
                # if the value is NaN, just set it to 0
                if np.isnan(val):
                    val = 0
                entropyFeatureArrayX[i][0][j-3] = val
                
                entropyFeatureList = addFeatureToList(featureList = entropyFeatureList,
                                                    featureListIndex = j-3,
                                                    newFeatureName = "{col}_spectral_entropy".format(col=col))
                
                ######################################
                # calculate Permutation Entropy
                # ------------------------------------
                val = entropy.perm_entropy(colEpochList, order=3, normalize=True, delay=1)
                # if the value is NaN, just set it to 0
                if np.isnan(val):
                    val = 0
                entropyFeatureArrayX[i][0][j-4] = val
                
                entropyFeatureList = addFeatureToList(featureList = entropyFeatureList,
                                                    featureListIndex = j-4,
                                                    newFeatureName = "{col}_permutation_entropy".format(col=col))
                
                ######################################
                # calculate Singular Value Decomposition entropy.
                # ------------------------------------
                val = entropy.svd_entropy(colEpochList, order=3, normalize=True, delay=1)
                # if the value is NaN, just set it to 0
                if np.isnan(val):
                    val = 0
                entropyFeatureArrayX[i][0][j-5] = val
                
                entropyFeatureList = addFeatureToList(featureList = entropyFeatureList,
                                                    featureListIndex = j-5,
                                                    newFeatureName = "{col}_svd_entropy".format(col=col))
                
                #break
            #break
        

        # Normalize everything to 0-1
        print("Normalizing the entropy features...")

        # Norm=max -> then it will normalize between 0-1, axis=0 is important too!
        # We need to reshape it to a 2d Array
        X_entropy_norm = preprocessing.normalize(entropyFeatureArrayX.reshape(entropyFeatureArrayX.shape[0], entropyFeatureArrayX.shape[2]), norm='max', axis=0)

        # Now reshape it back to a simple 3D array
        X_entropy_norm = X_entropy_norm.reshape(X_entropy_norm.shape[0], 1, X_entropy_norm.shape[1])


        return X_entropy_norm, entropyFeatureList
Exemple #6
0
def app_entropy(x):
    return entropy.app_entropy(x, order=2, metric='chebyshev')
Exemple #7
0
def construction_egonet_feature_vec(ego_features=None,
                                    ego_feature_names=None,
                                    time_steps_list=None,
                                    time_interval=None):
    """
    ego_features: a object, e.g., {_id:x, ego:x, features:{f1:[x,...], ...,fn:[x,...]}}
    ego_feature_names: ['num_alters', ...], 用于固定均值方差等5个指标值的拼接顺序.按照该列表中的特征顺序拼接,即num_alters的5个指标值+...
    time_steps_list: a time step list, e.g., [x, ..., x]. note that len(time_steps_list)==len(feature_sequence),
               and its indexes correspond to feature_sequence's time steps.
    time_interval: the specified time interval, a list, e.g., ['2000-03', '2001-04']
    :return: [ego_name, feature_vec], e.g, ['mao', [x, x, ...]]
    """
    ego_name = ego_features["ego"]
    ego_features_obj = ego_features[
        "features"]  # {f1:[x, ...], ..., fn:[x, ...]}
    ego_feature_vec_list = []  # [mean, ...]
    for each_feature in ego_feature_names:  # each feature
        feature_sequence = ego_features_obj[each_feature]  # [x, ...]
        # fixme: 某个时间区间
        if time_interval is not None:  # time_interval=[from, to]
            if time_interval[0] != time_interval[
                    1]:  # # fixme:区间 ['2000-03', '2001-04']
                t_from_index = time_steps_list.index(time_interval[0])
                t_to_index = time_steps_list.index(time_interval[1])
                feature_seq_slice = feature_sequence[
                    t_from_index:t_to_index +
                    1]  # feature_seq_slice=[x, ...,x]
                # fixme: 均值
                seq_mean = np.mean(feature_seq_slice)
                # fixme: 方差
                seq_var = np.var(feature_seq_slice)
                # fixme: 偏度
                pd_s = pd.Series(feature_seq_slice)
                seq_skew = pd_s.skew()
                # fixme: 峰度
                seq_kurt = pd_s.kurt()
                # fixme: 近似熵
                seq_app_entropy = app_entropy(feature_seq_slice)
                # fixme: 将5个指标装到列表中
                ego_feature_vec_list.append(seq_mean)
                ego_feature_vec_list.append(seq_var)
                ego_feature_vec_list.append(seq_skew)
                ego_feature_vec_list.append(seq_kurt)
                ego_feature_vec_list.append(seq_app_entropy)

            else:  # fixme: 某个时间点, e.g.,['2000-03', '2000-03']
                t_index = time_steps_list.index(time_interval[0])
                feature_val = feature_sequence[t_index]
                ego_feature_vec_list.append(feature_val)

        # fixme:整个时间轴
        else:  # the whole time series
            # fixme: 均值
            seq_mean = np.mean(feature_sequence)
            # fixme: 方差
            seq_var = np.var(feature_sequence)
            # fixme: 偏度
            pd_s = pd.Series(feature_sequence)
            seq_skew = pd_s.skew()
            # fixme: 峰度
            seq_kurt = pd_s.kurt()
            # fixme: 近似熵
            seq_app_entropy = app_entropy(feature_sequence)
            # fixme: 将5个指标装到列表中
            ego_feature_vec_list.append(seq_mean)
            ego_feature_vec_list.append(seq_var)
            ego_feature_vec_list.append(seq_skew)
            ego_feature_vec_list.append(seq_kurt)
            ego_feature_vec_list.append(seq_app_entropy)

    return [ego_name, ego_feature_vec_list]
# ax1.plot(s[si], color=color)
# ax1.tick_params(axis='y', labelcolor=color)
#
# ax2 = ax1.twinx()
# color = 'tab:blue'
# ax2.set_ylabel('S', color=color)
# ax2.plot(S[si], color=color)
# ax2.tick_params(axis='y', labelcolor=color)
#
# plt.show()

# Entropy:
print(entropy.perm_entropy(s[0], order=3,
                           normalize=True))  # Permutation entropy
print(entropy.spectral_entropy(s[0], 100, method='welch',
                               normalize=True))  # Spectral entropy
print(entropy.svd_entropy(
    s[0], order=3, delay=1,
    normalize=True))  # Singular value decomposition entropy
print(entropy.app_entropy(s[0], order=2,
                          metric='chebyshev'))  # Approximate entropy
print(entropy.sample_entropy(s[0], order=2,
                             metric='chebyshev'))  # Sample entropy

fpath_db = os.path.join(os.path.dirname(__file__), 'data',
                        '06-sir-gamma-beta.sqlite3')
te = TrajectoryEnsemble(fpath_db).stats()
s = te.traj[1].get_signal().series
print(entropy.app_entropy(s[0], order=2,
                          metric='chebyshev'))  # Approximate entropy
Exemple #9
0
def sampen(data):
    return app_entropy(signal_ch1[3000:-3000], 2, r=0.15)