Esempio n. 1
0
def RR_to_features(heart_data):
    from hrvanalysis import get_frequency_domain_features,get_time_domain_features, get_poincare_plot_features
    #chuyen heart_rate_list thanh RR_list
    for i in heart_data:
        RR_interval.append(60*1000/i)
    
    #tinh ra cac features
    feautures_1 = get_poincare_plot_features(RR_interval)
    SD1 = feautures_1['sd1']
    SD2 = feautures_1['sd2']
    feautures_2 = get_frequency_domain_features(RR_interval)
    LF = feautures_2['lf']
    HF = feautures_2['hf']
    LF_HF = feautures_2['lf_hf_ratio']
    HF_LF = 1/LF_HF
    LF_NU = feautures_2['lfnu']
    HF_NU = feautures_2['hfnu']
    TP = feautures_2['total_power']
    VLF = feautures_2['vlf']
    feautures_3 = get_time_domain_features(RR_interval)
    pNN50 = feautures_3['pnni_50']
    RMSSD = feautures_3['rmssd']
    MEAN_RR = feautures_3['mean_nni']
    MEDIAN_RR = feautures_3['median_nni']
    HR = feautures_3['mean_hr']
    SDRR = feautures_3['sdnn']
    SDRR_RMSSD = SDRR/RMSSD
    SDSD = feautures_3['sdsd']
    row_list = [["MEAN_RR", "MEDIAN_RR", "SDRR","RMSSD","SDSD","SDRR_RMSSD"
                 ,"HR","pNN50","SD1","SD2","VLF","LF","LF_NU","HF","HF_NU"
                 ,"TP","LF_HF","HF_LF"],
             [MEAN_RR,MEDIAN_RR,SDRR,RMSSD,SDSD,SDRR_RMSSD,HR,pNN50,SD1,SD2
              ,VLF,LF,LF_NU,HF,HF_NU,TP,LF_HF,HF_LF]]
    return row_list[1]
Esempio n. 2
0
def get_feature_names(recording):
    time_domain_features = hrv.get_time_domain_features(recording["Recording"]["RrInterval"])
    geometrical_features = hrv.get_geometrical_features(recording["Recording"]["RrInterval"])
    frequency_domain_features = hrv.get_frequency_domain_features(recording["Recording"]["RrInterval"])
    csi_cvi_features = hrv.get_csi_cvi_features(recording["Recording"]["RrInterval"])
    poincare_plot_features = hrv.get_poincare_plot_features(recording["Recording"]["RrInterval"])

    feature_dictionary = {
                            **time_domain_features,
                            **geometrical_features,
                            **frequency_domain_features,
                            **csi_cvi_features,
                            **poincare_plot_features
                         }
    
    return [key for key in feature_dictionary.keys()]
Esempio n. 3
0
def recording_to_x_y_feature_regression(recording):
    time_domain_features = hrv.get_time_domain_features(recording["Recording"]["RrInterval"])
    geometrical_features = hrv.get_geometrical_features(recording["Recording"]["RrInterval"])
    frequency_domain_features = hrv.get_frequency_domain_features(recording["Recording"]["RrInterval"])
    csi_cvi_features = hrv.get_csi_cvi_features(recording["Recording"]["RrInterval"])
    poincare_plot_features = hrv.get_poincare_plot_features(recording["Recording"]["RrInterval"])

    feature_dictionary = {
                            **time_domain_features,
                            **geometrical_features,
                            **frequency_domain_features,
                            **csi_cvi_features,
                            **poincare_plot_features
                         }
    
    x = [value for value in feature_dictionary.values()]
    y = decade_to_label(recording["AgeDecade"], False)
    
    return [y] + x
Esempio n. 4
0
def get_poincare_plot_features(RR_windows):
    assert isinstance(RR_windows, (list, np.ndarray))
    if isinstance(RR_windows, np.ndarray):
        assert RR_windows.ndim == 2, 'Must be 2D'

    if any([np.any(wRR > 1000) for wRR in RR_windows if len(wRR) > 0]):
        log.warn(
            'Values seem to be in ms instead of seconds! Algorithm migh fail.')

    feats = {x: [] for x in ['sd1', 'sd2', 'ratio_sd2_sd1']}
    for wRR in RR_windows:
        if len(wRR) < 2:
            for key, val in feats.items():
                feats[key].append(np.nan)
        else:
            mRR = hrvanalysis.get_poincare_plot_features(
                (wRR * 1000).astype(int))
            for key, val in mRR.items():
                feats[key].append(val)
    return feats
def compute_medium_term_features_on_interval(features, i, rr_timestamps, rrs,
                                             medium_window_offset):
    if (i * SHORT_WINDOW) > MEDIUM_WINDOW:
        rr_on_medium_intervals = get_rr_intervals_on_window(
            rr_timestamps, rrs, (i - medium_window_offset) * SHORT_WINDOW,
            MEDIUM_WINDOW)
        clean_rrs = get_clean_intervals(rr_on_medium_intervals)

        if len(rr_on_medium_intervals) == 0:
            raise ValueError("No RR intervals")

        # Compute non linear features
        cvi_csi_features = get_csi_cvi_features(clean_rrs)
        for key in cvi_csi_features.keys():
            features[i][FEATURES_KEY_TO_INDEX[key]] = cvi_csi_features[key]

        sampen = get_sampen(clean_rrs)
        features[i][FEATURES_KEY_TO_INDEX["sampen"]] = sampen["sampen"]

        poincare_features = get_poincare_plot_features(clean_rrs)
        for key in poincare_features.keys():
            features[i][FEATURES_KEY_TO_INDEX[key]] = poincare_features[key]
  def hrvAnalysis(self, times, samples, rrTimes, rrValues):

    def listSecToMsec(secs):
      msecs = []
      for i in range(len(secs)):
        msecs.append( int(secs[i] * 1000) )
      return msecs

    def listMsecToSec(msecs):
      secs = []
      for i in range(len(msecs)):
        secs.append( float(msecs[i]) / 1000)
      return secs

    rrValuesMsec = listSecToMsec(rrValues)

    results = {}
    results['time_domain'] = get_time_domain_features(rrValuesMsec)
    results['freq_domain'] = get_frequency_domain_features(rrValuesMsec)
    results['poincare_plot'] = get_poincare_plot_features(rrValuesMsec)

    return results
Esempio n. 7
0
def RR_to_features(heart_data):
            print(heart_data)
            from hrvanalysis import get_frequency_domain_features,get_time_domain_features, get_poincare_plot_features
            #chuyen heart_rate_list thanh RR_list
            RR_interval = []
            for i in heart_data:
                RR_interval.append(60*1000/int(i))
            
            #tinh ra cac features
            feautures_1 = get_poincare_plot_features(RR_interval)
            SD1 = feautures_1['sd1']
            SD2 = feautures_1['sd2']
            feautures_2 = get_frequency_domain_features(RR_interval)
            LF = feautures_2['lf']
            HF = feautures_2['hf']
            LF_HF = feautures_2['lf_hf_ratio']
            HF_LF = 1/LF_HF
            LF_NU = feautures_2['lfnu']
            HF_NU = feautures_2['hfnu']
            TP = feautures_2['total_power']
            VLF = feautures_2['vlf']
            feautures_3 = get_time_domain_features(RR_interval)
            pNN50 = feautures_3['pnni_50']
            RMSSD = feautures_3['rmssd']
            MEAN_RR = feautures_3['mean_nni']
            MEDIAN_RR = feautures_3['median_nni']
            HR = feautures_3['mean_hr']
            SDRR = feautures_3['sdnn']
            SDRR_RMSSD = SDRR/RMSSD
            SDSD = feautures_3['sdsd']
            row_list = [["MEAN_RR", "MEDIAN_RR", "SDRR","RMSSD","SDSD","SDRR_RMSSD"
                        ,"HR","pNN50","SD1","SD2","VLF","LF","LF_NU","HF","HF_NU"
                        ,"TP","LF_HF","HF_LF"],
                    [MEAN_RR,MEDIAN_RR,SDRR,RMSSD,SDSD,SDRR_RMSSD,HR,pNN50,SD1,SD2
                    ,VLF,LF,LF_NU,HF,HF_NU,TP,LF_HF,HF_LF]]
            with open('service/data/final/data_temp.csv', 'w', newline='') as file:
                writer = csv.writer(file)
                writer.writerows(row_list)  
            return row_list[1]
Esempio n. 8
0
def RR_to_features(RR_interval):
    feautures_1 = get_poincare_plot_features(RR_interval)
    SD1 = feautures_1['sd1']
    SD2 = feautures_1['sd2']
    feautures_2 = get_frequency_domain_features(RR_interval)
    LF = feautures_2['lf']
    HF = feautures_2['hf']
    LF_HF = feautures_2['lf_hf_ratio']
    HF_LF = 1 / LF_HF
    LF_NU = feautures_2['lfnu']
    HF_NU = feautures_2['hfnu']
    TP = feautures_2['total_power']
    VLF = feautures_2['vlf']
    feautures_3 = get_time_domain_features(RR_interval)
    pNN50 = feautures_3['pnni_50']
    RMSSD = feautures_3['rmssd']
    MEAN_RR = feautures_3['mean_nni']
    MEDIAN_RR = feautures_3['median_nni']
    HR = feautures_3['mean_hr']
    SDRR = feautures_3['sdnn']
    SDRR_RMSSD = SDRR / RMSSD
    SDSD = feautures_3['sdsd']
    import csv
    row_list = [[
        "MEAN_RR", "MEDIAN_RR", "SDRR", "RMSSD", "SDSD", "SDRR_RMSSD", "HR",
        "pNN50", "SD1", "SD2", "VLF", "LF", "LF_NU", "HF", "HF_NU", "TP",
        "LF_HF", "HF_LF"
    ],
                [
                    MEAN_RR, MEDIAN_RR, SDRR, RMSSD, SDSD, SDRR_RMSSD, HR,
                    pNN50, SD1, SD2, VLF, LF, LF_NU, HF, HF_NU, TP, LF_HF,
                    HF_LF
                ]]
    with open('data/final/data_user.csv', 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerows(row_list)
Esempio n. 9
0
def hrvAnalysis(times, samples, rrTimes, rrValues):
    def listSecToMsec(secs):
        msecs = []
        for i in range(len(secs)):
            msecs.append(int(secs[i] * 1000))
        return msecs

    def listMsecToSec(msecs):
        secs = []
        for i in range(len(msecs)):
            secs.append(float(msecs[i]) / 1000)
        return secs

    rrValuesMsec = listSecToMsec(rrValues)
    """
  # This remove outliers from signal
  rr_intervals_without_outliers = remove_outliers(rr_intervals=rrValuesMsec,
                                                  low_rri=300, high_rri=2000)

  # This replace outliers nan values with linear interpolation
  interpolated_rr_intervals = interpolate_nan_values(rr_intervals=rr_intervals_without_outliers,
                                                     interpolation_method="linear")


  # This remove ectopic beats from signal
  nn_intervals_list = remove_ectopic_beats(rr_intervals=interpolated_rr_intervals, method="malik")

  # This replace ectopic beats nan values with linear interpolation
  interpolated_nn_intervals = interpolate_nan_values(rr_intervals=nn_intervals_list)

  time_domain_features = get_time_domain_features(interpolated_nn_intervals)
  """

    time_domain_features = get_time_domain_features(rrValuesMsec)

    print("")
    print("TIME DOMAIN FEATURES:")
    for k in time_domain_features.keys():
        v = time_domain_features[k]
        print(" %s : %f" % (k, v))

    freq_domain_features = get_frequency_domain_features(rrValuesMsec)

    print("")
    print("FREQUENCY DOMAIN FEATURES:")
    for k in freq_domain_features.keys():
        v = freq_domain_features[k]
        print(" %s : %f" % (k, v))

    poincare_plot_features = get_poincare_plot_features(rrValuesMsec)

    print("")
    print("POINCARE PLOT FEATURES:")
    for k in poincare_plot_features.keys():
        v = poincare_plot_features[k]
        print(" %s : %f" % (k, v))

    plot_poincare(rrValuesMsec, plot_sd_features=True)
    """
  def rrToTimes(rrList):
    times = []
    prevTime = 0.0 
    for i in range(len(rrList)):
      t = prevTime + rrList[i]
      times.append(t)
      prevTime = t 
    return times

  timesInterpolatedNNIntervals = rrToTimes(interpolated_nn_intervals)
  """
    """
  plotLeadWithRR("leadII", times, samples,
                    "ECGPU", rrTimes, rrValues,
                    "clean", listMsecToSec(timesInterpolatedNNIntervals), rrValues)
  """

    return True
Esempio n. 10
0
    def process_all_files(self, is_test=False):
        '''
        This function will go through every subject overlapped data and extract the intersect set between hr and acc.
        the dataset quality control will filter out the RRI dataset with lower bound= 300, upper bound with 1000
        the output will be in either test output path or the actual output path.
        :param is_test: true is for test dataset
        :return:
        '''
        # load Acc, HR and overlap files
        if is_test:
            all_acc_files = []
            all_hr_files = []
        else:
            all_acc_files = os.listdir(self.acc_path)
            all_hr_files = os.listdir(self.hr_path)
        overlap_df = pd.read_csv(
            self.overlap_path
        )  # only do experiment if they have overlapped ECG and Actigraphy
        total_subjects_list = overlap_df['mesaid'].unique()
        valid_pids = pd.read_csv(
            self.cfg.TRAIN_TEST_SPLIT)['uids'].values.tolist()
        # here we set the valid subject IDs according to a snapshot of MESA data on 2019-05-01. In this
        # snapshot, we manually checked the aligned data making sure the pre-processing yield satisfied quality of data.
        # ##### The num of total valid subjects should be 1743
        total_subjects_list = list(
            set(total_subjects_list).intersection(set(valid_pids)))
        total_processed = []
        if not os.path.exists(self.processed_records):
            with open(self.processed_records, "w") as output:
                writer = csv.writer(output, lineterminator='\n')
                writer.writerows(total_processed)
        # tag = datetime.now().strftime("%Y%m%d-%H%M%S")
        for PID in total_subjects_list:
            mesa_id = "%04d" % PID
            # filter Acc and HR based on the overlap records
            print('*' * 100)
            print("Processing subject %s dataset" % mesa_id)
            acc_inlist_idx = [s for s in all_acc_files if mesa_id in s]
            hr_inlist_idx = [s for s in all_hr_files if mesa_id in s]
            feature_list = []
            if len(acc_inlist_idx) > 0 and len(hr_inlist_idx) > 0:
                # get the raw dataset file index
                acc_file_idx = all_acc_files.index(''.join(acc_inlist_idx))
                hr_file_idx = all_hr_files.index(''.join(hr_inlist_idx))
                # load Acc and HR into Pandas
                acc_df = pd.read_csv(
                    os.path.join(self.acc_path, all_acc_files[acc_file_idx]))
                hr_df = pd.read_csv(
                    os.path.join(self.hr_path, all_hr_files[hr_file_idx]))
                featnames = get_statistic_feature(acc_df,
                                                  column_name="activity",
                                                  windows_size=20)
                acc_start_idx = overlap_df[overlap_df['mesaid'] ==
                                           PID]['line'].values[0].astype(int)
                acc_epochs = hr_df['epoch'].max()
                # cut the dataset frame from the overlapped start index to the HR end index
                acc_df = acc_df[acc_start_idx - 1:acc_start_idx + acc_epochs -
                                1]
                # recalculate the line to the correct index
                acc_df['line'] = acc_df['line'] - acc_start_idx + 1
                acc_df = acc_df.reset_index(drop=True)
                # calculate the intersect set between HR and acc and cut HR to align the sequence
                # ################ Data quality control for Acc ########################
                # use marker and activity as the indicator column if the shape is different to 2-dim then drop

                list_size_chk = np.array(acc_df[['marker',
                                                 'activity']].values.tolist())
                # check whether the activity is empty
                if len(list_size_chk.shape) < 2:
                    print(
                        "File {f_name} doesn't meet dimension requirement, it's size is {wrong_dim}"
                        .format(f_name=all_acc_files[acc_file_idx],
                                wrong_dim=list_size_chk.shape))
                    continue

                # Cut HRV dataset based on length of Actigraphy dataset
                if (int(hr_df['epoch'].tail(1)) > acc_df.shape[0]):
                    hr_df = hr_df[hr_df['epoch'] <= acc_df.shape[0]]
                # remove the noise data points if two peaks overlapped or not wear
                hr_df = hr_df[hr_df['TPoint'] > 0]
                # Define RR intervals by taking the difference between each one of the measurements in seconds (*1k)
                hr_df['RR Intervals'] = hr_df['seconds'].diff() * 1000
                hr_df['RR Intervals'].fillna(
                    hr_df['RR Intervals'].mean(),
                    inplace=True)  # fill mean for first sample

                # old method for processing of RR intervals which is inappropriate
                # sampling_df = pd.concat([sampling_df, t1], axis =0 )
                # outlier_low = np.mean(hr_df['HR']) - 6 * np.std(hr_df['HR'])
                # outlier_high = np.mean(hr_df['HR']) + 6 * np.std(hr_df['HR'])
                # hr_df = hr_df[hr_df['HR'] >= outlier_low]
                # hr_df = hr_df[hr_df['HR'] <= outlier_high]

                # apply HRV-Analysis package
                # filter any hear rate over 60000/300 = 200, 60000/2000 = 30
                clean_rri = hr_df['RR Intervals'].values
                clean_rri = hrvana.remove_outliers(rr_intervals=clean_rri,
                                                   low_rri=300,
                                                   high_rri=2000)
                clean_rri = hrvana.interpolate_nan_values(
                    rr_intervals=clean_rri, interpolation_method="linear")
                clean_rri = hrvana.remove_ectopic_beats(rr_intervals=clean_rri,
                                                        method="malik")
                clean_rri = hrvana.interpolate_nan_values(
                    rr_intervals=clean_rri)

                hr_df["RR Intervals"] = clean_rri
                # calculate the Heart Rate
                hr_df['HR'] = np.round((60000.0 / hr_df['RR Intervals']), 0)
                # filter ACC
                acc_df = acc_df[acc_df['interval'] != 'EXCLUDED']
                # filter RRI
                t1 = hr_df.epoch.value_counts().reset_index().rename(
                    {
                        'index': 'epoch_idx',
                        'epoch': 'count'
                    }, axis=1)
                invalid_idx = set(t1[t1['count'] < 3]['epoch_idx'].values)
                del t1
                hr_df = hr_df[~hr_df['epoch'].isin(list(invalid_idx))]
                # get intersect epochs
                hr_epoch_set = set(hr_df['epoch'].values)
                acc_epoch_set = set(acc_df['line'])  # get acc epochs
                # only keep intersect dataset
                diff_epoch_set_a = acc_epoch_set.difference(hr_epoch_set)
                diff_epoch_set_b = hr_epoch_set.difference(acc_epoch_set)
                acc_df = acc_df[~acc_df['line'].isin(diff_epoch_set_a)]
                hr_df = hr_df[~hr_df['epoch'].isin(diff_epoch_set_b)]
                # check see if their epochs are equal
                assert acc_df.shape[0] == len(hr_df['epoch'].unique())
                # filter out any epochs with rri less than 3
                hr_epoch_set = set(hr_df['epoch'].values)
                hr_epoch_set = hr_epoch_set.difference(invalid_idx)
                for _, hr_epoch_idx in enumerate(list(hr_epoch_set)):
                    # sliding window
                    gt_label = hr_df[hr_df['epoch'] ==
                                     hr_epoch_idx]["stage"].values[0]
                    if self.hrv_win != 0:
                        offset = int(np.floor(self.hrv_win / 2))
                        tmp_hr_df = hr_df[hr_df['epoch'].isin(
                            np.arange(hr_epoch_idx - offset,
                                      hr_epoch_idx + offset))]
                    else:
                        tmp_hr_df = hr_df[hr_df['epoch'] == hr_epoch_idx]
                    try:  # check to see if the first time stamp is empty
                        start_sec = float(tmp_hr_df['seconds'].head(1) * 1.0)
                    except Exception as ee:
                        print("Exception %s, source dataset: %s" %
                              (ee, tmp_hr_df['seconds'].head(1)))
                    # calculate each epochs' HRV features
                    rr_epoch = tmp_hr_df['RR Intervals'].values
                    all_hr_features = {}
                    try:
                        all_hr_features.update(
                            hrvana.get_time_domain_features(rr_epoch))
                    except Exception as ee:
                        self.log_process(ee, PID, hr_epoch_idx)
                        print("processed time domain features: {}".format(
                            str(ee)))
                    try:
                        all_hr_features.update(
                            hrvana.get_frequency_domain_features(rr_epoch))
                    except Exception as ee:
                        self.log_process(ee, PID, hr_epoch_idx)
                        print("processed frequency domain features: {}".format(
                            str(ee)))
                    try:
                        all_hr_features.update(
                            hrvana.get_poincare_plot_features(rr_epoch))
                    except Exception as ee:
                        self.log_process(ee, PID, hr_epoch_idx)
                        print("processed poincare features: {}".format(
                            str(ee)))
                    try:
                        all_hr_features.update(
                            hrvana.get_csi_cvi_features(rr_epoch))
                    except Exception as ee:
                        self.log_process(ee, PID, hr_epoch_idx)
                        print("processed csi cvi domain features: {}".format(
                            str(ee)))
                    try:
                        all_hr_features.update(
                            hrvana.get_geometrical_features(rr_epoch))
                    except Exception as ee:
                        self.log_process(ee, PID, hr_epoch_idx)
                        print("processed geometrical features: {}".format(
                            str(ee)))

                    all_hr_features.update({
                        'stages':
                        gt_label,
                        'mesaid':
                        acc_df[acc_df['line'] ==
                               hr_epoch_idx]['mesaid'].values[0],
                        'linetime':
                        acc_df[acc_df['line'] ==
                               hr_epoch_idx]['linetime'].values[0],
                        'line':
                        acc_df[acc_df['line'] ==
                               hr_epoch_idx]['line'].values[0],
                        'wake':
                        acc_df[acc_df['line'] ==
                               hr_epoch_idx]['wake'].values[0],
                        'interval':
                        acc_df[acc_df['line'] ==
                               hr_epoch_idx]['interval'].values[0],
                        'activity':
                        acc_df[acc_df['line'] == hr_epoch_idx]
                        ['activity'].values[0]
                    })
                    feature_list.append(all_hr_features)

            #  If feature list is not empty
            if len(feature_list) > 0:
                hrv_acc_df = pd.DataFrame(feature_list)
                hrv_acc_df = hrv_acc_df.reset_index(drop=True)
                del hrv_acc_df['tinn']  # tinn is empty
                featnames = featnames + ["line"]
                combined_pd = pd.merge(acc_df[featnames],
                                       hrv_acc_df,
                                       on='line',
                                       how='inner')
                #combined_pd = combined_pd.reset_index(drop=True)
                combined_pd['timestamp'] = pd.to_datetime(
                    combined_pd['linetime'])
                combined_pd['base_time'] = pd.to_datetime('00:00:00')
                combined_pd['seconds'] = (combined_pd['timestamp'] -
                                          combined_pd['base_time'])
                combined_pd['seconds'] = combined_pd['seconds'].dt.seconds
                combined_pd.drop(['timestamp', 'base_time'],
                                 axis=1,
                                 inplace=True)
                combined_pd['two_stages'] = combined_pd["stages"].apply(
                    lambda x: 1.0 if x >= 1.0 else 0.0)
                combined_pd.loc[combined_pd['stages'] > 4,
                                'stages'] = 4  # make sure rem sleep label is 4
                combined_pd = combined_pd.fillna(combined_pd.median())
                combined_pd = combined_pd[
                    combined_pd['interval'] != 'EXCLUDED']
                aligned_data = self.output_path

                # standardise and normalise the df
                feature_list = combined_pd.columns.to_list()
                std_feature = [
                    x for x in feature_list if x not in [
                        'two_stages', 'seconds', 'interval', 'wake',
                        'linetime', 'mesaid', 'stages', 'line'
                    ]
                ]
                if self.standarize:
                    standardize_df_given_feature(combined_pd,
                                                 std_feature,
                                                 df_name='combined_df',
                                                 simple_method=False)
                combined_pd.to_csv(os.path.join(aligned_data,
                                                (mesa_id + '_combined.csv')),
                                   index=False)
                print("ID: {}, successed process".format(mesa_id))
                with open(self.processed_records, "a") as text_file:
                    text_file.write(
                        "ID: {}, successed process \n".format(mesa_id))
                total_processed.append(
                    "ID: {}, successed process".format(mesa_id))
            else:
                print("Acc is empty or HRV is empty!")
                total_processed.append(
                    "ID: {}, failed process".format(mesa_id))
                with open(self.processed_records, "a") as text_file:
                    text_file.write("ID: {}, failed process".format(mesa_id))
def extract_feature(data, raw_signal):
    feature = []
    ts = np.array(data['ts'])
    filtered = np.array(data['filtered'])
    rpeaks = np.array(data['rpeaks'])
    templates_ts = np.array(data['templates_ts'])
    templates = np.array(data['templates'])
    heart_rate_ts = np.array(data['heart_rate_ts'])
    heart_rate = np.array(data['heart_rate'])
    s_points = np.array(data['s_points'])
    q_points = np.array(data['q_points'])

    feature.append(np.std(raw_signal))
    feature.append(np.std(filtered))
    # RR interval
    rr_intervals = rpeaks[1:] - rpeaks[:-1]
    feature.append(np.min(rr_intervals))
    feature.append(np.max(rr_intervals))
    feature.append(np.mean(rr_intervals))
    feature.append(np.std(rr_intervals))

    # R amplitude
    r_apt = np.abs(filtered[rpeaks])
    feature.append(np.min(r_apt))
    feature.append(np.max(r_apt))
    feature.append(np.mean(r_apt))
    feature.append(np.std(r_apt))

    # Q amplitude
    q_apt = np.abs(filtered[q_points])
    feature.append(np.min(q_apt))
    feature.append(np.max(q_apt))
    feature.append(np.mean(q_apt))
    feature.append(np.std(q_apt))

    # QRS duration
    qrs_duration = s_points - q_points
    feature.append(np.min(qrs_duration))
    feature.append(np.max(qrs_duration))
    feature.append(np.mean(qrs_duration))
    feature.append(np.std(qrs_duration))

    interpolated_nn_intervals = rr_intervals * 10 / 3
    time_domain_feature = hrvanalysis.get_time_domain_features(
        interpolated_nn_intervals)
    for f_name in time_domain_feature:
        feature.append(time_domain_feature[f_name])

    # geometrical features
    geo_feature = hrvanalysis.get_geometrical_features(
        interpolated_nn_intervals)
    for f_name in geo_feature:
        if geo_feature[f_name] is None:
            feature.append(0)
        else:
            feature.append(geo_feature[f_name])

    # frequency domain features
    f_feature = hrvanalysis.get_frequency_domain_features(
        interpolated_nn_intervals)
    for f_name in f_feature:
        if f_feature[f_name] is None:
            feature.append(0)
        else:
            feature.append(f_feature[f_name])

    # csi cvi features
    csi_cvi_feature = hrvanalysis.get_csi_cvi_features(
        interpolated_nn_intervals)
    for f_name in csi_cvi_feature:
        if csi_cvi_feature[f_name] is None:
            feature.append(0)
        else:
            feature.append(csi_cvi_feature[f_name])

    # get_poincare_plot_features
    pp_feature = hrvanalysis.get_poincare_plot_features(
        interpolated_nn_intervals)
    for f_name in pp_feature:
        if pp_feature[f_name] is None:
            feature.append(0)
        else:
            feature.append(pp_feature[f_name])

    # wavelet energy
    coeffs = wavelet_decomposition(filtered)

    for k, coeff in enumerate(coeffs):
        b = coeffs[coeff]
        b = b / np.max(np.abs(b))
        feature.append(np.mean(b * b))

    # wavelet energy
    coeffs = wavelet_decomposition(raw_signal)

    for k, coeff in enumerate(coeffs):
        b = coeffs[coeff]
        b = b / np.max(np.abs(b))
        feature.append(np.mean(b * b))

    # template average
    templates_ave = np.mean(templates, axis=0)
    templates_ave = templates_ave / np.max(np.abs(templates_ave))
    for p in templates_ave:
        feature.append(p)

    # template std ave
    templates_std = np.std(templates, axis=0)
    templates_std = templates_std / np.max(np.abs(templates_std))
    for p in templates_std:
        feature.append(p)

    # wavelet 1
    wavelet_1 = coeffs['cA5']
    wavelet_1 = wavelet_1[:50]
    for p in wavelet_1:
        feature.append(p)

    return np.array(feature)
Esempio n. 12
0
def get_poincare_ratio(rr):
    return get_poincare_plot_features(rr)['ratio_sd2_sd1']
Esempio n. 13
0
        
for element in IBI_ts:
    if element < (meanIBI + (4 * stdIBI)) and (element > meanIBI - (4 * stdIBI)):
        IBI_cleaned.append(element)
    else:
        outliers += 1
percent_removed = (outliers/len(IBI_ts)) * 100;
        
#if we're removing more than 20% of the data then something is wrong and this file should be flagged
if percent_removed < 20:
            
    time_domain_features = hrv.get_time_domain_features(IBI_cleaned)
    geometrical_features = hrv.get_geometrical_features(IBI_cleaned)
    frequency_domain_features = hrv.get_frequency_domain_features(IBI_cleaned)
    csi_cvi_features = hrv.get_csi_cvi_features(IBI_cleaned)
    poincare_plot_features = hrv.get_poincare_plot_features(IBI_cleaned)
    sampen = hrv.get_sampen(IBI_cleaned)
            
    td_keys = list(time_domain_features.keys())
    geom_keys = list(geometrical_features.keys())
    frequency_keys = list(frequency_domain_features.keys())
    csi_keys = list(csi_cvi_features.keys())
    poincare_keys = list(poincare_plot_features.keys())
    samp_keys = list(sampen.keys())
    
    #format header of output
    header = []
    ID = ['ID']
                
    GSR_meanh = ['GSR_mean']  
    GSR_maxh = ['GSR_max']