示例#1
0
    def extractNonLinearDomain(self, x):
        try:
            nni = self.extractRR(x)
            sampEntro = nn.sample_entropy(nni=nni, dim=1)
            lyapEx = self.lyapunov_exponent(nni=nni, emb_dim=3, matrix_dim=2)
            return np.array([sampEntro["sampen"], lyapEx["lyapex"]])

        except:
            return np.array([])
示例#2
0
    def extractNonLinearDomain(self, x):
        try:
            nni = self.extractRR(x)
            sampEntro = nn.sample_entropy(nni=nni, dim=2) #change dim from 1 to 2
            lyapEx = self.lyapunov_exponent(nni=nni, emb_dim=3, matrix_dim=2)
            pointCare = nn.poincare(nni=nni, show=False)
            # hrust = nolds.hurst_rs(nni)
            corrDim = nolds.corr_dim(nni, emb_dim=3)
            # csi = get_csi_cvi_features(nni) #caused worse results
            # geo = get_geometrical_features(nni) #caused worse results
            # dfa = nolds.dfa(nni)
            # return np.array([sampEntro["sampen"], lyapEx["lyapex"]])
            return np.array([sampEntro["sampen"], lyapEx["lyapex"], pointCare["sd1"], pointCare["sd2"], pointCare["sd_ratio"], pointCare["ellipse_area"],  corrDim])

        except:
            return np.array([])
示例#3
0
def compute_features(nni):
    features = {}
    features['mean_hr'] = tools.heart_rate(nni).mean()
    features['sdnn'] = td.sdnn(nni)[0]
    features['rmssd'] = td.rmssd(nni)[0]
    features['sdsd'] = td.sdsd(nni)[0]
    features['nn20'] = td.nn20(nni)[0]
    features['pnn20'] = td.nn20(nni)[1]
    features['nn50'] = td.nn50(nni)[0]
    features['pnn50'] = td.nn50(nni)[1]
    features['hf_lf_ratio'] = fd.welch_psd(nni, show=False)['fft_ratio']
    features['very_lf'] = fd.welch_psd(nni, show=False)['fft_peak'][0]
    features['lf'] = fd.welch_psd(nni, show=False)['fft_peak'][1]
    features['hf'] = fd.welch_psd(nni, show=False)['fft_peak'][2]
    features['log_very_lf'] = fd.welch_psd(nni, show=False)['fft_log'][0]
    features['log_lf'] = fd.welch_psd(nni, show=False)['fft_log'][1]
    features['log_hf'] = fd.welch_psd(nni, show=False)['fft_log'][2]
    features['sampen'] = nl.sample_entropy(nni)[0]

    return features
def cal_hrv(data_list):

    if (np.nan in data_list) or (len(data_list) <= 1):
        sdsd = np.nan
        rmssd = np.nan
        sd1 = np.nan
        sd2 = np.nan
        sd_ratio = np.nan
        a1 = np.nan
        a2 = np.nan
        a_ratio = np.nan
        sampen = np.nan

    else:
        # sdsd
        sdsd = round(td.sdsd(nni=data_list)['sdsd'], 5)
        # rmssd
        rmssd = round(td.rmssd(nni=data_list)['rmssd'], 5)
        # sd1, sd2
        nl_results = nl.poincare(nni=data_list)
        sd1 = round(nl_results['sd1'], 5)
        sd2 = round(nl_results['sd2'], 5)
        sd_ratio = round(sd2 / sd1, 5)
        # dfa a1, a2
        dfa_results = nl.dfa(data_list)
        try:
            a1 = round(dfa_results['dfa_alpha1'], 5)
            a2 = round(dfa_results['dfa_alpha2'], 5)
            a_ratio = round(a2 / a1, 5)
        except:
            a1 = np.nan
            print(a1, type(a1))
            a2 = np.nan
            print(a2, type(a2))
            a_ratio = np.nan
        # Sampen
        t = np.std(data_list)
        sampen = round(
            nl.sample_entropy(nni=data_list, tolerance=t)['sampen'], 6)

    return sdsd, rmssd, sd1, sd2, sd_ratio, a1, a2, a_ratio, sampen
示例#5
0
def updateInfo():

    # sql server setting and connect
    IPadr = 'DESKTOP-01CEBMI'  # Server ip address
    srcDatabase = ''  # DB name
    Database_ID = ''  # SQL ID
    Database_PWD = ''  # SQL PW

    q = pyodbc.connect(DRIVER='{SQL Server}',
                       SERVER=IPadr,
                       DATABASE=srcDatabase,
                       UID=Database_ID,
                       PWD=Database_PWD)

    Query = """       select * from matching_patient
                      order by delirium desc, CONVERT(int, subclass) asc
            """
    df = pd.io.sql.read_sql(Query, q)

    Query = """       select * from results_latest
                    order by delirium desc, CONVERT(int, subclass) asc
        """
    df_smoking = pd.io.sql.read_sql(Query, q)

    # numeric variable
    # numeric_vars = ['VITAL_HR', 'VITAL_SPO2', 'NIBP_DIAS', 'NIBP_SYS', 'VITAL_RESP']
    parameters = ['ECGII', 'RESPIMP', 'SPO2IRAC']

    onset_time = []
    delirium_id = []
    control_id = []

    for d in range(0, len(df['delirium_onset'])
                   ):  # devide delirium patient ID and Control patient ID
        if df['delirium_onset'][d] == '0':  # delirium_onset=0  :  control
            control_id.append(df['patientid'][d])
        else:  # else  :  delirium
            delirium_id.append(df['patientid'][d])

    for time_series in time_series_list:

        for parameter in parameters:

            if os.path.isfile(dstDir + "/" + parameter + "_data_" +
                              str(time_series // 60) + "H.json") != 1:
                open(
                    dstDir + "/" + parameter + "_data_" +
                    str(time_series // 60) + "H.json", 'a').close()
                data = dict()

                data['pid'] = []
                data['subclass'] = []
                data['delirium'] = []
                data['onset_time'] = []
                data['ICU_in'] = []
                data['LOS'] = []
                data['starttime'] = []
                data['endtime'] = []
                data['smoking'] = []

                # sample entropy
                data['sampen'] = []

                # DFA
                data['DFA_a1'] = []
                data['DFA_a2'] = []
                data['DFA_ratio'] = []

            else:
                f = open(
                    dstDir + "/" + parameter + "_data_" +
                    str(time_series // 60) + "H.json", "r")
                data = f.read()
                data = json.loads(data)
                f.close()

            idx = -1
            c_data = 0  # Control cohort data exist patient's number
            d_data = 0  # Delirium(Case) cohort data exist patient's number
            c_no_data = 0  # Control cohort no data patient's number
            d_no_data = 0  # Delirium(Case) cohort no data patient's number

            for i in df['patientid']:
                idx += 1
                p_id = i
                sub_class = df['subclass'][idx]

                smoking = int(df_smoking['smoking(ex0)']
                              [idx])  # 0: non-smoking, 1: smoking
                data['smoking'].append(smoking)

                if p_id in data['pid']:
                    new_id = p_id + "_2"
                    data['pid'].append(new_id)
                else:
                    data['pid'].append(p_id)
                data['subclass'].append(int(sub_class))

                # extract data each patients
                data_list = []

                if list(df['delirium_onset']
                        )[idx] == '0':  # control group patients
                    # create empty array
                    nni_result = np.array([])

                    ICU_in = list(df['Time_in'])[idx]
                    LOS = list(df['LOS'])[idx]
                    ICU_time = datetime.datetime.strptime(ICU_in, '%Y%m%d%H%M')
                    data['LOS'].append(float(LOS))
                    data['ICU_in'].append(int(ICU_in))

                    case_LOS = float(list(df['LOS'])[int(sub_class) - 1])
                    case_onset = float(onset_time[int(sub_class) - 1])

                    print('case_LOS : ', case_LOS)
                    print('case_onset : ', case_onset)

                    onset_ratio = round(case_onset / case_LOS, 3)

                    print('onset_ratio : ', onset_ratio)

                    control_LOS = float(LOS)
                    control_onset = round(control_LOS * onset_ratio, 3)
                    control_onset_time = round(control_onset * 24 * 60, 0)

                    print('control_LOS : ', control_LOS)
                    print('control_onset : ', control_onset)
                    print('')
                    # onset_control = round(onset_time[int(sub_class)-1] * 24 * 60,0)  # 24hour, 60minutes
                    data['onset_time'].append(control_onset)

                    t1 = (ICU_time +
                          timedelta(minutes=(control_onset_time - time_series))
                          ).strftime('%Y%m%d%H%M')  # ICU_in_time + onset_time
                    t1 = t1[:11] + '0'
                    t2 = datetime.datetime.strptime(t1, '%Y%m%d%H%M')
                    t3 = (t2 - timedelta(minutes=(measure_time))
                          ).strftime('%Y%m%d%H%M')
                    print('')
                    print('control onset time: ', t1)
                    print('onset - 30min : ', t3)
                    print('')

                    data['starttime'].append(int(t3))
                    data['endtime'].append(int(t1))
                    # select data ---> time 기준 이전 30분 데이터를 뽑아서 내림차순 정렬 후, 가장 최근 데이터를 추출  (직전 10분 데이터가 없는 경우를 대비)
                    Query = """
                                        select top 1 filepath 
                                        from waveform_data
                                        where patient_id= '""" + p_id + """'
                                        and left(starttime, 12) >= '""" + str(
                        t3) + """'
                                        and left(starttime, 12) < '""" + str(
                            t1) + """'
                                        and wavetype = '""" + parameter + """'
                                        order by id desc

                                    """
                    df1 = pd.io.sql.read_sql(Query, q)['filepath']

                    if df1.shape[0] == 0:  # if data not exist,
                        c_no_data += 1
                        print('')
                        print('no control data', 'pid: ', p_id)
                        print('')

                        if df['delirium'][idx] == '0':
                            data['delirium'].append(0)
                        else:
                            data['delirium'].append(1)

                        data['sampen'].append(0)

                        data['DFA_a1'].append(0)
                        data['DFA_a2'].append(0)
                        data['DFA_ratio'].append(0)  # alpha 1 / alpha 2

                    else:

                        if parameter == 'ECGII':
                            if df['delirium'][idx] == '0':
                                data['delirium'].append(0)
                            else:
                                data['delirium'].append(1)

                        c_data += 1

                        file_path = "E://backup/waveform_data/BiosignalRepository/BiosignalRepository/GZIP/" + str(
                            df1[0][28:]).replace("\\", "/")

                        patient_id = str(df1[0][28:48])
                        date = str(df1[0][49:57])
                        f_name = str(df1[0][58:])
                        print('')
                        print('file_path: ', file_path)
                        print('')
                        data_10m = read_nk(file_path, parameter)[:]

                        try:
                            if ((data_10m == 500).all()) or ((data_10m
                                                              == 0).all()):
                                print('data not Found', 'pid: ', p_id)
                                file_path = "E://backup/waveform_data/BiosignalRepository/BiosignalRepository/GZIP/" + str(
                                    df1[1][28:]).replace("\\", "/")
                                data_10m = read_nk(file_path, parameter)[:]
                            else:
                                data_10m = read_nk(file_path, parameter)[:]
                        except:
                            data_10 = read_nk(file_path, parameter)[:]

                        t = np.std(data_10m)  # tolerance = t * 0.2

                        sampen_result = nl.sample_entropy(data_10m,
                                                          dim=2,
                                                          tolerance=t * 0.2)
                        dfa_result = nl.dfa(data_10m, show=False)  # dim 바꿔봐야함

                        sampen = round(sampen_result[0], 5)

                        dfa_a1 = round(dfa_result[1], 5)  # [1] is alpha1
                        dfa_a2 = round(dfa_result[2], 5)  # [2] is alpha2
                        dfa_ratio = round(dfa_a1 / dfa_a2,
                                          5)  # ratio is a1 / a2

                        data['sampen'].append(sampen)

                        data['DFA_a1'].append(dfa_a1)
                        data['DFA_a2'].append(dfa_a2)
                        data['DFA_ratio'].append(dfa_ratio)

                        print('sampen: ', sampen)
                        print('')
                        print('a1: ', dfa_a1)
                        print('a2: ', dfa_a2)
                        print('ratio: ', dfa_ratio)

                else:  # case group patients
                    # create empty array
                    ICU_in = list(df['Time_in'])[idx]
                    ICU_time = datetime.datetime.strptime(
                        ICU_in[0:11] + '0', '%Y%m%d%H%M')
                    LOS = list(df['LOS'])[idx]
                    data['ICU_in'].append(ICU_in)
                    data['LOS'].append(float(LOS))

                    onset = list(
                        df['delirium_onset']
                    )[idx]  # onset > if delirium patient exist, find onset_time index
                    # print(onset)
                    delirium_onset = float(df['onset_time'][idx])
                    data['onset_time'].append(delirium_onset)
                    onset_time.append(delirium_onset)

                    onset_date = datetime.datetime.strptime(
                        onset[0:11] + '0', '%Y%m%d%H%M')  # delirium_onset_time

                    print('onset_date:', onset_date)
                    t4 = (onset_date -
                          timedelta(minutes=(time_series))).strftime(
                              '%Y%m%d%H%M')  # str (onset time)
                    t5 = datetime.datetime.strptime(
                        t4, '%Y%m%d%H%M'
                    )  # onset time        # datetime (modified onset time)
                    t6 = (t5 - timedelta(minutes=(measure_time))).strftime(
                        '%Y%m%d%H%M')  # str (onset_time - measure time)
                    # t5 = (onset_date).strftime('%Y%m%d%H%M')
                    print('')
                    print('delirium onset time: ', t4)
                    print('onset - 30min : ', t6)
                    print('')

                    data['starttime'].append(int(t6))
                    data['endtime'].append(int(t4))
                    # select data ---> time 기준 이전 30분 데이터를 뽑아서 내림차순 정렬 후, 가장 최근 데이터를 추출  (직전 10분 데이터가 없는 경우를 대비)
                    Query = """
                                        select top 1 filepath
                                        from waveform_data
                                        where patient_id= '""" + p_id + """'
                                        and left(starttime, 12) >= '""" + str(
                        t6) + """'
                                        and left(starttime, 12) < '""" + str(
                            t4) + """'
                                        and wavetype = '""" + parameter + """'
                                        order by id desc
                            """
                    df2 = pd.io.sql.read_sql(Query, q)['filepath']
                    # ob_value2 = list(map(float, df2[:]))
                    # ob_value2 = [i for i in ob_value2 if i != 0]

                    if df2.shape[
                            0] == 0:  # if exist less than 10% for measure period time series,
                        d_no_data += 1
                        print('')
                        print('no case data', 'pid: ', p_id)
                        print('')
                        if df['delirium'][idx] == '0':
                            data['delirium'].append(0)
                        else:
                            data['delirium'].append(1)

                        data['sampen'].append(0)

                        data['DFA_a1'].append(0)
                        data['DFA_a2'].append(0)
                        data['DFA_ratio'].append(0)  # alpha 1 / alpha 2

                    else:

                        if parameter == 'ECGII':
                            if df['delirium'][idx] == '0':
                                data['delirium'].append(0)
                            else:
                                data['delirium'].append(1)

                        d_data += 1

                        file_path = "E://backup/waveform_data/BiosignalRepository/BiosignalRepository/GZIP/" + str(
                            df2[0][28:]).replace("\\", "/")

                        patient_id = str(df2[0][28:48])
                        date = str(df2[0][49:57])
                        f_name = str(df2[0][58:])
                        print('')
                        print('file_path: ', file_path)
                        print('')

                        data_10m = read_nk(file_path, parameter)[:]
                        try:
                            if ((data_10m == 500).all()) or ((data_10m
                                                              == 0).all()):
                                print('data not Found', 'pid: ', p_id)
                                file_path = "E://backup/waveform_data/BiosignalRepository/BiosignalRepository/GZIP/" + str(
                                    df1[1][28:]).replace("\\", "/")
                                data_10m = read_nk(file_path, parameter)[:]
                            else:
                                data_10m = read_nk(file_path, parameter)[:]
                        except:
                            data_10 = read_nk(file_path, parameter)[:]

                        t = np.std(data_10m)  # tolerance = t * 0.2

                        sampen_result = nl.sample_entropy(data_10m,
                                                          dim=2,
                                                          tolerance=t * 0.2)
                        dfa_result = nl.dfa(data_10m, show=False)  # dim 바꿔봐야함

                        sampen = round(sampen_result[0], 5)

                        dfa_a1 = round(dfa_result[1], 5)  # [1] is alpha1
                        dfa_a2 = round(dfa_result[2], 5)  # [2] is alpha2
                        dfa_ratio = round(dfa_a1 / dfa_a2,
                                          5)  # ratio is a1 / a2

                        data['sampen'].append(sampen)
                        data['DFA_a1'].append(dfa_a1)
                        data['DFA_a2'].append(dfa_a2)
                        data['DFA_ratio'].append(dfa_ratio)

                        print('sampen: ', sampen)
                        print('a1: ', dfa_a1)
                        print('a2: ', dfa_a2)
                        print('ratio: ', dfa_ratio)
                        print('')

            print('delirium_data: ', d_data)
            print('control_data: ', c_data)
            print('delirium_data loss: ', d_no_data)
            print('control_data loss: ', c_no_data)
            print('origin_patient: ', len(df['delirium_onset']))
            print('total data: ', c_data + d_data + c_no_data + d_no_data)
            print('')
            print('')
            # data_df = pd.DataFrame(data)
            # data_df.to_csv (dstDir + "/spo2_data_" + str(measure_time // 60) +  ".csv", index = False, header=True)

            with open(dstDir + "/" + parameter + "_data_" +
                      str(time_series // 60) + "H.json",
                      'w',
                      encoding='utf-8') as f:
                json.dump(data, f, indent="\t")
            f.close()