'Creating 60s strips from ecg signal recording ( .bat ) file and give it a label as per the datapoint present in it

I am working with https://physionet.org/content/mitdb/1.0.0/ dataset where we have 48 ecg signal files.

Each recording is 30 minutes long and I Want to create 60s strips and give it a label. ie based on patient ECG condition and data points present in signal label it as normal, afib, ventricular or other

I have tried doing this but I doubt my logic is correct.

label2num={ 'A':1,'+':2, 'N':3, 'a':4, '"':5, '(AFIB':6}
import glob
#Here we assign label to strips as per the datapoint present in it
def PickLabel(row):
    if(label2num['A'] in row or label2num['a'] in row ): # If one label is A, assign it to A 
      return 'A'
    elif(np.allclose(row, label2num['N'])): ## If all labels are 'N'
      return "N"
    elif label2num['N'] in row and (label2num['+'] not in row or label2num['"']not in row) and  0  in row :
      return "N"
    else:  # Else other.
      return 'O'
#Read the recording,create the strips 
def ProcessOneFile(filename,Tw):
  
  sig, fields = wfdb.rdsamp(filename, channels=[0])
  sig=sig[:,0]
  lenn=int(len(sig)/19200)
  res=lenn*19200
  sig=sig[:res]
  sig_frames=np.reshape(sig,(-1,Tw*320)) # Tw- time window, 320-sample rate # frame signal (non-overlapping frames)
  annotation = wfdb.rdann(filename, 'atr')
  loc=annotation.__dict__['sample']
  valid_indicies=loc<sig.shape[0]  ## Just to make sure the label position doesn't exceed the length
  loc=loc[valid_indicies]
  symbol=np.array(annotation.__dict__['symbol'])
  print(np.unique(symbol))
  symbol=symbol[valid_indicies]
  print(len(annotation.__dict__['symbol']))
  print(len(annotation.__dict__['aux_note']))
  symbol_num=np.array([label2num.get(x,0) for x in symbol])  # To handle unknown symbol (the returned value will be zero)
  symbol_arr=np.zeros(sig.shape)
  symbol_arr[loc]=symbol_num
  lenn2=int(len(symbol_arr)/19200)
  res2=lenn2*19200
  symbol_arr=symbol_arr[:res]
  symbol_arr_frames=np.reshape(symbol_arr,(-1,Tw*320))  # frame symbols (non-overlapping frames)
  labels=np.apply_along_axis(PickLabel, 1, symbol_arr_frames)    # apply PickLabel function to each row
  return sig_frames,labels,symbol_arr_frames, symbol,valid_indicies

sig_frames,labels,symbol_arr_frames,symbol,valid_indicies=ProcessOneFile(files_dir+'/219',Tw)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source