示例#1
0
def read_data_pad_zeros(data_path, split="Train", preprocess=True, fs=500, newFs=256, winSecond=10, winNum=10, n_index=0,
              pre_type="sym"):
    """ Read data """

    # Fixed params
    # n_index = 0
    n_class = 9
    winSize = winSecond * fs
    new_winSize = 23296  # winSecond * newFs
    # Paths
    path_signals = data_path # os.path.join(data_path, split)

    # Read time-series data
    channel_files = os.listdir(path_signals)
    channel_files.sort()
    n_channels = 12  # len(channel_files)

    X = np.zeros((len(channel_files), new_winSize, n_channels)).astype('float32')

    channel_name = ['V6', 'aVF', 'I', 'V4', 'V2', 'aVL', 'V1', 'II', 'aVR', 'V3', 'III', 'V5']

    for i_ch, fil_ch in enumerate(channel_files[:]):  # tqdm

        if i_ch % 1000 == 0:
            print(i_ch)

        ecg = sio.loadmat(os.path.join(path_signals, fil_ch))
        ecg_length = ecg["I"].shape[1]

        if ecg_length > 45500:
            ecg_length = 45500

        ecg_channels = np.zeros((new_winSize, n_channels)).astype('float32')

        for i_n, ch_name in enumerate(channel_name):

            ecg_data = signal.resample(ecg[ch_name][:, :ecg_length].T,
                                       int(ecg[ch_name][:, :ecg_length].shape[1] / 500 * newFs)).T
            if preprocess:
                if pre_type == "sym":
                    ecg_channels[-ecg_data.shape[1]:, i_n] = ecg_preprocessing(ecg_data, 'sym8', 8, 3,
                                                                               newFs, removebaseline=False,
                                                                               normalize=False)[0]

                elif pre_type == "db4":
                    ecg_channels[-ecg_data.shape[1]:, i_n] = wavelet(ecg_data[0], 'db4', 4, 2, 4)
                elif pre_type == "db6":
                    ecg_channels[-ecg_data.shape[1]:, i_n] = wavelet_db6(ecg_data[0])

                # ecg_channels[:, i_n] = (ecg_channels[:, i_n]-np.mean(ecg_channels[:, i_n]))/np.std(ecg_channels[:, i_n])
            else:
                pass
                print(" no preprocess !!! ")

        X[i_ch, :, :] = ecg_channels

    return X
示例#2
0
def read_data(data_path, split = "TRAIN",preprocess=False):
    """ Read data """

    # Fixed params
    n_class = 2
    n_steps = 2560

    # Paths
    path_signals = os.path.join(data_path, split)

    # Read labels and one-hot encode
    label_path = os.path.join(data_path, "reference.txt")
    labels = pd.read_csv(label_path, sep='\t',header = None)

    # Read time-series data
    channel_files = os.listdir(path_signals)
    #print(channel_files)
    channel_files.sort()
    n_channels = 12#len(channel_files)
    #posix = len(split) + 5

    # Initiate array
    list_of_channels = []
    X = np.zeros((len(channel_files), n_steps, n_channels))
    i_ch = 0
    for i_ch,fil_ch in enumerate(channel_files):
        #channel_name = fil_ch[:-posix]
        #dat_ = pd.read_csv(os.path.join(path_signals,fil_ch), delim_whitespace = True, header = None)

        ecg = sio.loadmat(os.path.join(path_signals,fil_ch))
        ecg['data'] = signal.resample(ecg['data'].T,2560)
        if preprocess:
            data = ecg_preprocessing(ecg['data'].T, 'sym8', 8, 3, 256)
            X[i_ch,:,:] = data.T#ecg['data']
        else:
            X[i_ch,:,:] = ecg['data']
        
        # Record names
        #list_of_channels.append(channel_name)

        # iterate
        #i_ch += 1

    # Return 
    return X, labels, list_of_channels
def read_data_seg(data_path,
                  preprocess=True,
                  fs=500,
                  newFs=256,
                  winSecond=10,
                  winNum=10,
                  n_index=0,
                  pre_type="sym"):
    """ Read data """

    # Fixed params
    n_class = 9
    winSize = winSecond * fs
    new_winSize = winSecond * newFs

    # Paths
    path_signals = data_path  # os.path.join(data_path, split)

    # Read time-series data
    channel_files = os.listdir(path_signals)
    channel_files.sort()
    n_channels = 12

    X = np.zeros(
        (len(channel_files), new_winSize, n_channels)).astype('float32')

    channel_name = [
        'V6', 'aVF', 'I', 'V4', 'V2', 'aVL', 'V1', 'II', 'aVR', 'V3', 'III',
        'V5'
    ]

    for i_ch, fil_ch in enumerate(channel_files[:]):  # tqdm

        if i_ch % 1000 == 0:
            pass  #print(i_ch)

        ecg = sio.loadmat(os.path.join(path_signals, fil_ch))
        ecg_length = ecg["I"].shape[1]

        if ecg_length > fs * winNum * winSecond:
            print(" too long !!!", ecg_length)
            ecg_length = fs * winNum * winSecond
        if ecg_length < 4500:
            print(" too short !!!", ecg_length)
            break

        slide_steps = int((ecg_length - winSize) / winSecond)

        if ecg_length <= 4500:
            slide_steps = 0

        ecg_channels = np.zeros((new_winSize, n_channels)).astype('float32')

        for i_n, ch_name in enumerate(channel_name):

            ecg_channels[:, i_n] = signal.resample(
                ecg[ch_name][:, n_index * slide_steps:n_index * slide_steps +
                             winSize].T, new_winSize).T
            if preprocess:
                if pre_type == "sym":
                    ecg_channels[:, i_n] = ecg_preprocessing(
                        ecg_channels[:, i_n].reshape(1, new_winSize),
                        'sym8',
                        8,
                        3,
                        newFs,
                        removebaseline=False,
                        normalize=False)[0]
                elif pre_type == "db4":
                    ecg_channels[:, i_n] = wavelet(ecg_channels[:, i_n], 'db4',
                                                   4, 2, 4)
                elif pre_type == "db6":
                    ecg_channels[:, i_n] = wavelet_db6(ecg_channels[:, i_n])

                # ecg_channels[:, i_n] = (ecg_channels[:, i_n]-np.mean(ecg_channels[:, i_n]))/np.std(ecg_channels[:, i_n])
            else:
                pass
                print(" no preprocess !!! ")

        X[i_ch, :, :] = ecg_channels

    return X
def read_data_seg(data_path, split="Train", preprocess=False, fs=500, newFs=256, winSecond=10, winNum=10, n_index=0,pre_type="sym"):
    """ Read data """

    # Fixed params
    # n_index = 0
    n_class = 9
    winSize = winSecond * fs
    new_winSize = winSecond * newFs
    # Paths
    path_signals = os.path.join(data_path, split)

    # Read labels and one-hot encode
    # label_path = os.path.join(data_path, "reference.txt")
    # labels = pd.read_csv(label_path, sep='\t',header = None)
    # labels = pd.read_csv("reference.csv")

    # Read time-series data
    channel_files = os.listdir(path_signals)
    # print(channel_files)
    channel_files.sort()
    n_channels = 12  # len(channel_files)
    # posix = len(split) + 5

    # Initiate array
    list_of_channels = []

    X = np.zeros((len(channel_files), new_winSize, n_channels)).astype('float32')
    i_ch = 0

    channel_name = ['V6', 'aVF', 'I', 'V4', 'V2', 'aVL', 'V1', 'II', 'aVR', 'V3', 'III', 'V5']
    channel_mid_name = ['II', 'aVR', 'V2', 'V5']
    channel_post_name = ['III', 'aVF', 'V3', 'V6']

    for i_ch, fil_ch in enumerate(channel_files[:]):  # tqdm

        if i_ch % 1000 == 0:
            print(i_ch)

        ecg = sio.loadmat(os.path.join(path_signals, fil_ch))
        ecg_length = ecg["I"].shape[1]

        if ecg_length > fs * winNum * winSecond:
            print(" too long !!!", ecg_length)
            ecg_length = fs * winNum * winSecond
        if ecg_length < 4500:
            print(" too short !!!", ecg_length)
            break

        slide_steps = int((ecg_length - winSize) / winSecond)

        if ecg_length <= 4500:
            slide_steps = 0

        ecg_channels = np.zeros((new_winSize, n_channels)).astype('float32')

        for i_n, ch_name in enumerate(channel_name):

            ecg_channels[:, i_n] = signal.resample(ecg[ch_name]
                                                   [:, n_index * slide_steps:n_index * slide_steps + winSize].T
                                                   , new_winSize).T
            if preprocess:
                if pre_type == "sym":
                    ecg_channels[:, i_n] = ecg_preprocessing(ecg_channels[:, i_n].reshape(1, new_winSize), 'sym8', 8, 3,
                                                             newFs, removebaseline=False, normalize=False)[0]
                elif pre_type == "db4":
                    ecg_channels[:, i_n] = wavelet(ecg_channels[:, i_n], 'db4', 4, 2, 4)
                elif pre_type == "db6":
                    ecg_channels[:, i_n] = wavelet_db6(ecg_channels[:, i_n])
            else:
                pass
                print(" no preprocess !!! ")

        X[i_ch, :, :] = ecg_channels

    return X