Leer las ondas

1) Imports


In [1]:
%load_ext oct2py.ipython
import psycopg2
from psycopg2.extensions import register_adapter, AsIs
import wfdb
import numpy as np
import matplotlib.pyplot as plt
from oct2py import octave
#para el tiempo
from datetime import datetime
import pandas as pd
octave.addpath('/home/scidb/HeartRatePatterns/Matlab/')
octave.eval('pkg load signal')

Leer la base de datos para decidir que Ondas vamos a traer


In [2]:
def obtainWaveFormFields(dbname="mimic") :
    labels='II'
    conn = psycopg2.connect("dbname="+dbname)
    cur = conn.cursor()
    wave = "4059"
    select_stament = "SELECT lef.subject_id,lef.recorddate,lef.signame,lef.fs FROM waveformfields lef  LEFT JOIN (SELECT MAX(recorddate) AS recorddate,subject_id FROM waveformFields GROUP BY subject_id) rig ON lef.subject_id = rig.subject_id AND lef.recorddate = rig.recorddate WHERE rig.subject_id IS NOT NULL AND signame @> ARRAY['"+labels+"']::varchar[] AND lef.subject_id = "+wave
    cur.execute(select_stament)
    waves = []
    for row in cur :
        patient = "p"+str(row[0]).zfill(6)
        onda = patient+'-'+row[1]
        carpeta = patient[:3]+"/"+patient
        waves.append({"subject_id":row[0],"recorddate":row[1],"fs":row[3],"onda":onda,"carpeta":carpeta,
                      "signalII":row[2].index("II")})
    conn.close()
    return waves

In [3]:
def notExistQRST(subject_id,recordDate,table="waveformQRST"):
    conn = psycopg2.connect("dbname=mimic")
    cur = conn.cursor()
    select_stament = 'select id from '+table+' where subject_id= %s and recorddate = %s'
    cur.execute(select_stament,(int(subject_id),recordDate))
    exist = cur.fetchone() is None
    conn.close()
    return exist

In [4]:
waves = obtainWaveFormFields()
wave = waves[0]
carpeta = wave["carpeta"]
onda = wave["onda"]
signalII = wave["signalII"]
fs = wave["fs"]
subject_id = wave["subject_id"]
recorddate = wave["recorddate"]
if notExistQRST(subject_id,recorddate) :
    signal = wfdb.rdsamp(onda,pbdir='mimic3wdb/matched/'+carpeta,channels =[signalII]).p_signals


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    379             try:  # Python 2.7, use buffering of HTTP responses
--> 380                 httplib_response = conn.getresponse(buffering=True)
    381             except TypeError:  # Python 2.6 and older, Python 3

TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-4-1a5b7fe81008> in <module>()
      8 recorddate = wave["recorddate"]
      9 if notExistQRST(subject_id,recorddate) :
---> 10     signal = wfdb.rdsamp(onda,pbdir='mimic3wdb/matched/'+carpeta,channels =[signalII]).p_signals

/usr/local/lib/python3.4/dist-packages/wfdb/readwrite/records.py in rdsamp(recordname, sampfrom, sampto, channels, physical, pbdir, m2s, smoothframes, ignoreskew, returnres)
    876         readsegs, segranges  = record.requiredsegments(sampfrom, sampto, channels)
    877         # The signals within each segment to read
--> 878         segsigs = record.requiredsignals(readsegs, channels, dirname, pbdir)
    879 
    880         # Read the desired samples in the relevant segments

/usr/local/lib/python3.4/dist-packages/wfdb/readwrite/records.py in requiredsignals(self, readsegs, channels, dirname, pbdir)
    623                 else:
    624                     # Get the signal names of the current segment
--> 625                     s_signames = rdheader(os.path.join(dirname, self.segname[readsegs[i]]), pbdir = pbdir).signame
    626                     readsigs.append(wanted_siginds(w_signames, s_signames))
    627 

/usr/local/lib/python3.4/dist-packages/wfdb/readwrite/records.py in rdheader(recordname, pbdir, rdsegments)
    929 
    930     # Read the header file. Separate comment and non-comment lines
--> 931     headerlines, commentlines = _headers.getheaderlines(recordname, pbdir)
    932 
    933     # Get fields from record line

/usr/local/lib/python3.4/dist-packages/wfdb/readwrite/_headers.py in getheaderlines(recordname, pbdir)
    456     # Read online header file
    457     else:
--> 458         headerlines, commentlines = downloads.streamheader(recordname, pbdir)
    459 
    460     return headerlines, commentlines

/usr/local/lib/python3.4/dist-packages/wfdb/readwrite/downloads.py in streamheader(recordname, pbdir)
     10     # Full url of header location
     11     url = posixpath.join(dbindexurl, pbdir, recordname+'.hea')
---> 12     r = requests.get(url)
     13 
     14     # Raise HTTPError if invalid url

/usr/local/lib/python3.4/dist-packages/requests/api.py in get(url, params, **kwargs)
     70 
     71     kwargs.setdefault('allow_redirects', True)
---> 72     return request('get', url, params=params, **kwargs)
     73 
     74 

/usr/local/lib/python3.4/dist-packages/requests/api.py in request(method, url, **kwargs)
     56     # cases, and look like a memory leak in others.
     57     with sessions.Session() as session:
---> 58         return session.request(method=method, url=url, **kwargs)
     59 
     60 

/usr/local/lib/python3.4/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    506         }
    507         send_kwargs.update(settings)
--> 508         resp = self.send(prep, **send_kwargs)
    509 
    510         return resp

/usr/local/lib/python3.4/dist-packages/requests/sessions.py in send(self, request, **kwargs)
    616 
    617         # Send the request
--> 618         r = adapter.send(request, **kwargs)
    619 
    620         # Total elapsed time of the request (approximately)

/usr/local/lib/python3.4/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    438                     decode_content=False,
    439                     retries=self.max_retries,
--> 440                     timeout=timeout
    441                 )
    442 

/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    599                                                   timeout=timeout_obj,
    600                                                   body=body, headers=headers,
--> 601                                                   chunked=chunked)
    602 
    603             # If we're going to release the connection in ``finally:``, then

/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    381             except TypeError:  # Python 2.6 and older, Python 3
    382                 try:
--> 383                     httplib_response = conn.getresponse()
    384                 except Exception as e:
    385                     # Remove the TypeError from the exception chain in Python 3;

/usr/lib/python3.4/http/client.py in getresponse(self)
   1206 
   1207         try:
-> 1208             response.begin()
   1209             assert response.will_close != _UNKNOWN
   1210             self.__state = _CS_IDLE

/usr/lib/python3.4/http/client.py in begin(self)
    378         # read until we get a non-100 response
    379         while True:
--> 380             version, status, reason = self._read_status()
    381             if status != CONTINUE:
    382                 break

/usr/lib/python3.4/http/client.py in _read_status(self)
    340 
    341     def _read_status(self):
--> 342         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    343         if len(line) > _MAXLINE:
    344             raise LineTooLong("status line")

/usr/lib/python3.4/socket.py in readinto(self, b)
    372         while True:
    373             try:
--> 374                 return self._sock.recv_into(b)
    375             except timeout:
    376                 self._timeout_occurred = True

KeyboardInterrupt: 

In [ ]:
def updateSignal(subject_id,recorddate,signallen,dbname="mimic") :
    conn = psycopg2.connect("dbname="+dbname)
    cur = conn.cursor()
    update_stament = "UPDATE waveformfields SET signallength=(%s) WHERE subject_id = (%s) AND recorddate = (%s)"
    cur.execute(update_stament,(signallen,subject_id,recorddate))
    conn.commit()

In [ ]:
def subSignal(signal) :
    signalNan = signal[~np.isnan(signal)]
    signalSize = 1800000
    print("La señal tiene ",len(signalNan)," registros extraemos las ultimas 4 horas")
    if len(signalNan)<signalSize :
        subsignal = signalNan
    else :
        subsignal = signalNan[-1800000:]
    return subsignal.tolist()

In [ ]:
updateSignal(subject_id,recorddate,len(signal))
subsignal = subSignal(signal)
len(subsignal)
#subsignal[1001500:1002000]

Extraemos las ultimas 4 horas osea 1'800.000 registros


In [ ]:
fig_size = [20,9]
plt.rcParams["figure.figsize"] = fig_size
plt.plot(subsignal)
#plt.plot(signal[:,1])
plt.show()

In [ ]:
def peakdetect(signal, fs,increment=0,previousResult=None):
    if previousResult is None:
        previousResult = {'Q_i':[],'Q_amp':[],'R_i':[],'R_amp':[],'S_i':[],'S_amp':[],'T_i':[],'T_amp':[]}
    (R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot) = octave.peakdetect(signal,fs,False, nout=10)
#    %octave [R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(signal,fs,false); -i signal,fs -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
    return {'Q_i'  :np.concatenate((previousResult['Q_i'],  Q_i[0]+increment)),
            'Q_amp':np.concatenate((previousResult['Q_amp'],Q_amp[0])),
            'R_i'  :np.concatenate((previousResult['R_i'],  R_i[0]+increment)),
            'R_amp':np.concatenate((previousResult['R_amp'],R_amp[0])),
            'S_i'  :np.concatenate((previousResult['S_i'],  S_i[0]+increment)),
            'S_amp':np.concatenate((previousResult['S_amp'],S_amp[0])),
            'T_i'  :np.concatenate((previousResult['T_i'],  T_i[0]+increment)),
            'T_amp':np.concatenate((previousResult['T_amp'],T_amp[0])),
           }

In [ ]:
def audit(before=datetime.now()):
    now = datetime.now()
    print("hasta aqui van",(now-before))
    return now

def checkwave(signal,fs):
    now = audit()
    windowsize = 10000*fs
    signalsize = len(signal)
    previousResult = {'Q_i':[],'Q_amp':[],'R_i':[],'R_amp':[],'S_i':[],'S_amp':[],'T_i':[],'T_amp':[]}
    termino = True
    fin=0
    while termino:
        ini=fin
        fin=fin+ (windowsize)
        if(fin>signalsize):
            fin=signalsize
        now = audit(now)
        print("ini",ini,"fin",fin)
        previousResult = peakdetect(signal[ini:fin],fs,increment=ini,previousResult=previousResult)
        now = audit(now)
        termino = fin<signalsize
    return previousResult

In [ ]:
previousResult = checkwave(subsignal,fs)

In [ ]:
plt.scatter(previousResult['Q_i'], previousResult['Q_amp'],c='yellow')
plt.scatter(previousResult['R_i'], previousResult['R_amp'],c='blue')
plt.scatter(previousResult['S_i'], previousResult['S_amp'],c='red')
plt.scatter(previousResult['T_i'], previousResult['T_amp'],c='black')
plt.plot(subsignal,c='lightgreen')
plt.show()

In [ ]:
init = 0
end = 1
plt.plot(subsignal[1734699:(1734699+125)],c='lightgreen')
#plt.scatter(previousResult['Q_i'][init:end]-int(previousResult['Q_i'][init]), previousResult['Q_amp'][init:end],c='yellow')
#plt.scatter(previousResult['R_i'][init:end]-int(previousResult['Q_i'][init]), previousResult['R_amp'][init:end],c='blue')
#plt.scatter(previousResult['S_i'][init:end]-int(previousResult['Q_i'][init]), previousResult['S_amp'][init:end],c='red')
#plt.scatter(previousResult['T_i'][init:end]-int(previousResult['Q_i'][init]), previousResult['T_amp'][init:end],c='black')
plt.show()

In [ ]:
print('Q',len(previousResult['Q_i']), len(previousResult['Q_amp']))
print('R',len(previousResult['R_i']), len(previousResult['R_amp']))
print('S',len(previousResult['S_i']), len(previousResult['S_amp']))
print('T',len(previousResult['T_i']), len(previousResult['T_amp']))

In [ ]:
def readsignal(next1,next2,next3,wave,amp,i,signal) :
    currentw = int(signal[wave][i]) if len(signal[wave])>i else None
    if currentw is None or (next2 is not None and currentw>next2) or (next3 is not None and currentw>next3):
        return None,None,i
    if(next1 is None or currentw<=next1) :
        currentw,waveamp,i = currentw,signal[amp][i],i+1
    else :
        currentw,waveamp = None,None
    return currentw,waveamp,i
def sortHeartBeats(signal) :
    q,s,t,r,result = 0,0,0,0,None
    index = 0
    while q<len(signal['Q_i']) and r<len(signal['R_i']) and s<len(signal['S_i'])  and t<len(signal['T_i']) :
        rinext = signal['R_i'][r]   if r  <len(signal['R_i']) else None
        sinext = signal['S_i'][s]   if s  <len(signal['S_i']) else None
        tinext = signal['T_i'][t]   if t  <len(signal['T_i']) else None
        qinext = signal['Q_i'][q+1] if q+1<len(signal['Q_i']) else None
        riNext = signal['R_i'][q+1] if q+1<len(signal['R_i']) else None
        siNext = signal['S_i'][q+1] if q+1<len(signal['S_i']) else None
        
        qi,qamp,q = readsignal(rinext,sinext,tinext,'Q_i','Q_amp',q,signal)
        ri,ramp,r = readsignal(sinext,tinext,qinext,'R_i','R_amp',r,signal)
        si,samp,s = readsignal(tinext,qinext,riNext,'S_i','S_amp',s,signal)
        ti,tamp,t = readsignal(qinext,riNext,siNext,'T_i','T_amp',t,signal)
  #      print("q",q,"r",r,"s",s,"t",t)
        dic = {'Q_i':qi,'Q_amp':qamp,
               'R_i':ri,'R_amp':ramp,
               'S_i':si,'S_amp':samp,
               'T_i':ti,'T_amp':tamp}
        if result is None :
            result = pd.DataFrame(dic, index=[index])
        else :
            result = pd.concat([result,  pd.DataFrame(dic, index=[index])])
        index=index+1
    return result
sortBeats = sortHeartBeats(previousResult)

In [ ]:
def checkQRST(qrst) :
    shape = qrst.shape[0]
    x = 0
    while x<shape :
        qiNotNone = qrst.iloc[x]["Q_i"] is not None
        riNotNone = qrst.iloc[x]["R_i"] is not None
        siNotNone = qrst.iloc[x]["S_i"] is not None
        tiNotNone = qrst.iloc[x]["T_i"] is not None
        if(qiNotNone and riNotNone and qrst.iloc[x]["Q_i"]>qrst.iloc[x]["R_i"]) :
            print("Q>R en",x)
        if(riNotNone and siNotNone and qrst.iloc[x]["R_i"]>qrst.iloc[x]["S_i"]) :
            print("R>S en",x)
        if(siNotNone and tiNotNone and qrst.iloc[x]["S_i"]>qrst.iloc[x]["T_i"]) :
            print("S>T en",x)
        if(qiNotNone and tiNotNone and qrst.iloc[x]["Q_i"]>qrst.iloc[x]["T_i"]) :
            print("Q>T en",x)
        x=x+1
checkQRST(sortBeats)

In [ ]:
print(sortBeats.iloc[2112:2114])

In [ ]:
def addapt_numpy_float64(numpy_float64):
  return AsIs(numpy_float64)
register_adapter(np.float64, addapt_numpy_float64)
def addapt_numpy_int64(numpy_int64):
  return AsIs(numpy_int64)
register_adapter(np.int64, addapt_numpy_int64)

In [ ]:
sortBeats = sortBeats.assign(subject_id=int(subject_id)).assign(recorddate=recorddate)
sortBeats['qrtsorder'] = sortBeats.index
def saveQRST(qrst) :
    conn = psycopg2.connect("dbname=mimic")
    cur = conn.cursor()
    table = "waveformQRST"
    cur.execute("CREATE TABLE IF NOT EXISTS "+table+
            " (id serial PRIMARY KEY,"+
            "Q_amp real,"+
            "Q_i integer,"+
            "R_amp real,"+
            "R_i integer,"+
            "S_amp real,"+
            "S_i integer,"+
            "T_amp real,"+
            "T_i integer,"+
            "subject_id integer,recorddate character varying(255),qrtsorder integer,"+
            'CONSTRAINT "uniqueQRST" UNIQUE (subject_id, recorddate, qrtsorder));')
    insert_statement = 'insert into '+table+' (%s) values %s'
    for index, row in qrst.iterrows():
        columns = row.keys()
        values = [row[column] for column in columns]
#        print(cur.mogrify(insert_statement, (AsIs(','.join(columns)), tuple(values))))
        cur.execute(insert_statement, (AsIs(','.join(columns)), tuple(values)))
    conn.commit()
    conn.close()
saveQRST(sortBeats)

In [ ]: