def make_set(df_data, channels, label_map, record_id, window_size=2048): n_windows = 0 for _, record in tqdm(df_data.iterrows()): n_windows += record['signal_length'] // window_size dataX = np.zeros((n_windows, len(channels), window_size)) dataY = np.zeros((n_windows, len(label_map))) record_list = [] nth_window = 0 for i, (patient, record) in enumerate(tqdm(df_data.iterrows())): # read the record, get the signal data and transpose it signal_data = io.rdrecord(os.path.join( 'data', record['name'])).p_signal.transpose() n_rows = signal_data.shape[-1] n_windows = n_rows // window_size dataX[nth_window:nth_window + n_windows] = np.array([ signal_data[:, i * window_size:(i + 1) * window_size] for i in range(n_windows) ]) dataY[nth_window:nth_window + n_windows][:, label_map[record.label]] = 1 nth_window += n_windows if record_id: record_list += n_windows * [record['name']] return dataX, dataY, record_list
def get_record(record_names, prefix=''): records = [] for record_name in tqdm(record_names): record = io.rdrecord(record_name=os.path.join(data_folder, record_name)) data = np.transpose(np.mean(record.p_signal, axis=1)) records.append(data) return np.array(records)
def extract_from_wfdb(wfdb_file_path, include_channels, header, **kwargs): """ TODO Returns: ndarray, shape NxC """ from wfdb.io import rdrecord rec = rdrecord(record_name=os.path.splitext(wfdb_file_path)[0], channel_names=include_channels) header["sample_rate"] = float(rec.fs) return rec.p_signal
def make_set(df_data, label_map, record_id, indices_channels, window_size=2048, taux_echant=1): n_channels = len(indices_channels) overlapping = 2 n_windows = get_len_set(df_data, overlapping, window_size, taux_echant) dataX = np.zeros((n_windows, n_channels, window_size)) dataY = np.zeros((n_windows, len(label_map))) record_list = [] nth_window = 0 for i, (patient, record) in tqdm(enumerate(df_data[:5].iterrows())): signal_data = io.rdrecord(os.path.join( data_folder, record['name'])).p_signal.transpose() data_for_patient, n_rows, n_windows = format_X(signal_data, taux_echant, overlapping, indices_channels, window_size) dataX[nth_window:nth_window + n_windows] = data_for_patient dataY[nth_window:nth_window + n_windows][:, label_map[record.label]] = 1 last_id = nth_window + n_windows nth_window += n_windows if record_id: record_list += n_windows * [record['name']] dataX = dataX[:nth_window] dataY = dataY[:nth_window] if not record_id: X_twa, Y_twa = get_twadb_db() print(X_twa.shape) print(Y_twa.shape) print(dataX.shape) print(dataY.shape) dataX = np.array(dataX.tolist() + X_twa.tolist()) dataY = np.array(dataY.tolist() + Y_twa.tolist()) print('nth_window', nth_window) print('last id', last_id) print('len dataX', len(dataX)) return dataX, dataY, record_list
def get_record(record_names, prefix='../'): records = [] for record_name in tqdm(record_names): record = io.rdrecord( record_name=os.path.join(data_folder, record_name)) label = record.comments[4].split(": ")[-1] patient = record_name.split('/')[0] signal_length = record.sig_len records.append({ 'name': record_name, 'label': label, 'patient': patient, 'signal_length': signal_length }) return pd.DataFrame(records)
def load_wfdb(file_path, load_channels, **kwargs): """ File loader function for .dat and .mat WFDB extension files. Redirects to the wfdb.io.rdrecord function. Returns: wfdb.io.record.Record object """ from wfdb.io import rdrecord rec = rdrecord(record_name=os.path.splitext(file_path)[0], channel_names=load_channels) chans = rec.sig_name load_channels = load_channels or chans if not chans or not (np.array(load_channels) == np.array(chans)).all(): from utime.errors import ChannelNotFoundError raise ChannelNotFoundError("Could not load channels {} " "from file {}. " "Actually loaded: {}".format( load_channels, file_path, chans)) return rec
row['signals'] = record.p_signal row['signal_length'] = record.sig_len channels = record.sig_name signals = record.p_signal.transpose() row['channels'] = channels for channel, signal in zip(channels, signals): row[channel] = signal return row records = [] for record_name in tqdm(record_names): record = io.rdrecord(record_name=os.path.join('data', record_name)) label = comments_to_dict(record.comments)['Reason for admission'][1:] patient = record_name.split('/')[0] signal_length = record.sig_len records.append({'name':record_name, 'label':label, 'patient':patient, 'signal_length':signal_length}) channels = record.sig_name df_records = pd.DataFrame(records) labels = df_records['label'].unique() df_records['label'].value_counts() selected_labels = [ 'Healthy control',
from tqdm import tqdm from wfdb import io, plot import pandas as pd # https://mc.ai/diagnosing-myocardial-infarction-using-long-short-term-memory-networks-lstms/ # The folder where you want to store your data data_folder = '../data/' # First get the list of available records and then download # those records and store them in data_folder. record_names = io.get_record_list('sddb') #io.dl_database('sddb', data_folder, record_names) # Read the first record record_name = record_names[0] print(record_name) record = io.rdrecord(record_name="../data/" + record_name) records = [] for record_name in tqdm(record_names): record = io.rdrecord(record_name=os.path.join('../data/', record_name)) print(record.sig_name) print(record.comments) # label = comments_to_dict(record.comments)['Reason for admission'][1:] label = record.comments patient = record_name.split('/')[0] signal_length = record.sig_len records.append({ 'name': record_name, 'label': label, 'patient': patient, 'signal_length': signal_length
def read_ecg(file_path): #read ecg record = io.rdrecord(file_path) return record
from tqdm import tqdm from wfdb import io, plot import pandas as pd # https://mc.ai/diagnosing-myocardial-infarction-using-long-short-term-memory-networks-lstms/ # The folder where you want to store your data data_folder = '../data/' # First get the list of available records and then download # those records and store them in data_folder. record_names = io.get_record_list('ptbdb') #io.dl_database('ptbdb', data_folder, record_names) # Read the first record record_name = record_names[0] print(record_name) record = io.rdrecord(record_name="../data/ptdb/" + record_name) records = [] for record_name in tqdm(record_names): record = io.rdrecord( record_name=os.path.join('../data/ptdb/', record_name)) print(record.sig_name) # label = comments_to_dict(record.comments)['Reason for admission'][1:] label = record.comments[4].split(": ")[-1] patient = record_name.split('/')[0] signal_length = record.sig_len records.append({ 'name': record_name, 'label': label, 'patient': patient, 'signal_length': signal_length
from keras.layers import CuDNNLSTM from sklearn.utils import shuffle from sklearn.metrics import confusion_matrix, classification_report from wfdb import io, plot # The folder where you want to store your data data_folder = '../../../data/ptdb/' # First get the list of available records and then download # those records and store them in data_folder. record_names = io.get_record_list('ptbdb') # Read the first record record_name = record_names[0] record = io.rdrecord(record_name=os.path.join(data_folder, record_name)) records = [] for record_name in tqdm(record_names): record = io.rdrecord(record_name=os.path.join(data_folder, record_name)) # label = comments_to_dict(record.comments)['Reason for admission'][1:] print(record.samps_per_frame) label = record.comments[4].split(": ")[-1] patient = record_name.split('/')[0] signal_length = record.sig_len records.append({ 'name': record_name, 'label': label, 'patient': patient, 'signal_length': signal_length })