Ejemplo n.º 1
0
def main(argv):
    an = wfdb.WFDB_AnninfoArray(2)
    annot = wfdb.WFDB_Annotation()
    record = raw_input("Type record name: ")
    iann = raw_input("Type input annotatior name")
    oann = raw_input("Type output annotator name: ")
    
    a = wfdb.WFDB_Anninfo()
    a.name = iann
    a.stat = wfdb.WFDB_READ
    an[0] = a
    
    a = wfdb.WFDB_Anninfo()
    a.name = oann
    a.stat = wfdb.WFDB_WRITE
    an[1] = a
    
    if wfdb.annopen(record ,an , 2) < 0:  sys.exit(1)
    
    while wfdb.getann(0 , annot) ==0:
        if wfdb.wfdb_isqrs(annot.anntyp):
            annot.anntyp = wfdb.NORMAL
            if wfdb.putann(0 , annot)<0: break
    
    wfdb.wfdbquit()
Ejemplo n.º 2
0
def main(argv):
    nc = len(argv) - 4

    if len(argv) < 4:
        print "usage:",  argv[0], "record start duration [ coefficients ... ]"
        sys.exit(1)
    if nc < 1: nc = 1
    c = [0.] * nc
    for i in range(0, nc):
        c[i] = float(argv[i+4])
    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig < 1: sys.exit(3)
    s = wfdb.WFDB_SiginfoArray(nsig)
    v = wfdb.WFDB_SampleArray(nsig)
    if wfdb.isigopen(argv[1], s.cast(), nsig) != nsig: sys.exit(3)
    if wfdb.isigsettime(wfdb.strtim(argv[2])) < 0: sys.exit(4)
    nsamp = wfdb.strtim(argv[3])
    if nsamp < 1:
        print argv[0]+ ": inappropriate value for duration"
        sys.exit(5)
    if wfdb.osigopen("16l", s.cast(), nsig) != nsig: sys.exit(6)
    wfdb.sample(0, 0)
    for t in range(0, nsamp):
        if not wfdb.sample_valid(): break
        for j in range(0, nsig):
            vv = 0
            for i in range(0, nc):
                if c[i] != 0.: vv += c[i] * wfdb.sample(j, t+i)
            v[j] = int(vv)
        if wfdb.putvec(v.cast()) < 0: break
    wfdb.newheader("out")
    wfdb.wfdbquit()
Ejemplo n.º 3
0
def main(argv):
    a = wfdb.WFDB_Anninfo()
    annot = wfdb.WFDB_Annotation()
    if len(argv) < 3:
        print "usage:", argv[0], "annotator record"
        sys.exit(1)
    a.name = argv[1]
    a.stat = wfdb.WFDB_READ    
    if wfdb.annopen(argv[2], a, 1) < 0: sys.exit(2)
    rrmax = 3 * wfdb.sampfreq(argv[2])
    if rrmax <= 0: sys.exit(3)
    rrhist = [0] * int(rrmax+1)
    while 1:
        if not (wfdb.getann(0,annot) == 0 and not wfdb.wfdb_isqrs(annot.anntyp)): break
    t = annot.time
    while wfdb.getann(0, annot) == 0:
        if wfdb.wfdb_isqrs(annot.anntyp):
            rr = annot.time - t
            if rr > rrmax: rr = rrmax
            rrhist[rr] += 1
            t = annot.time
    for rr in range(1, int(rrmax)):
        print '%(rr)4d %(time)s' % {'rr': rrhist[rr], 'time': wfdb.mstimstr(rr)}
    rr += 1
    print '%(rr)4d %(time)s (or longer)' % {'rr': rrhist[rr], 'time': wfdb.mstimstr(rr)}
    wfdb.wfdbquit()
Ejemplo n.º 4
0
 def close(self):
 #---------------
   global _WFDBLock, _CurrentRecord, _RecordCount
   with _WFDBLock:
     _RecordCount -= 1
     if _RecordCount <= 0:
       _CurrentRecord = None
   wfdb.wfdbquit()
Ejemplo n.º 5
0
 def read(self , record = None):
     if record == None:
         if self.record == None:
             sys.exit(1)
         else:
             record = self.record
     else:
         self.record = record
         
     sig0 = []
     sigt = []
     
     siarray = wfdb.WFDB_SiginfoArray(2)
     
     if (wfdb.isigopen(record , siarray.cast() , 2) < 1):
         sys.exit(1)
     
     v = wfdb.WFDB_SampleArray(2)
     
     
     #Getting X Axes Interval
     #Appending data to array
     X_INTERVAL = 20
     pause = 0
     for i in range(siarray.cast().nsamp):
         if ( wfdb.getvec(v.cast()) < 0 ):
             break
         sig0.append(v.__getitem__(0))
         sigt.append(pause)
         pause = pause + X_INTERVAL
         pass
     
     #High Pass Filtering
     highpass_filter = HighpassFilter()
     high_pass = highpass_filter.process(sig0,siarray.cast().nsamp)
     #Low pass Filtering
     lowpass_filter = LowpassFilter()
     low_pass = lowpass_filter.process(high_pass, len(high_pass))
     
     qrs = QRSDetector()
     qrs_data = qrs.process(low_pass, len(low_pass))
     
     feature_extractor = FeatureExtractor()
     features = feature_extractor.get_RR_interval(sig0, qrs_data)
     
     #sig0 = qrs_dat
       
     
     wfdb.wfdbquit() 
     
     return sigt , sig0 , features
Ejemplo n.º 6
0
    def read(self, record=None):
        if record == None:
            if self.record == None:
                sys.exit(1)
            else:
                record = self.record
        else:
            self.record = record

        sig0 = []
        sigt = []

        siarray = wfdb.WFDB_SiginfoArray(2)

        if (wfdb.isigopen(record, siarray.cast(), 2) < 1):
            sys.exit(1)

        v = wfdb.WFDB_SampleArray(2)

        #Getting X Axes Interval
        #Appending data to array
        X_INTERVAL = 20
        pause = 0
        for i in range(siarray.cast().nsamp):
            if (wfdb.getvec(v.cast()) < 0):
                break
            sig0.append(v.__getitem__(0))
            sigt.append(pause)
            pause = pause + X_INTERVAL
            pass

        #High Pass Filtering
        highpass_filter = HighpassFilter()
        high_pass = highpass_filter.process(sig0, siarray.cast().nsamp)
        #Low pass Filtering
        lowpass_filter = LowpassFilter()
        low_pass = lowpass_filter.process(high_pass, len(high_pass))

        qrs = QRSDetector()
        qrs_data = qrs.process(low_pass, len(low_pass))

        feature_extractor = FeatureExtractor()
        features = feature_extractor.get_RR_interval(sig0, qrs_data)

        #sig0 = qrs_dat

        wfdb.wfdbquit()

        return sigt, sig0, features
Ejemplo n.º 7
0
def main(argv):
    nsamp = 1000
    if len(argv) < 2:
        print "usage:", argv[0], "record"
        sys.exit(1)
    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig <= 0: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    vin = wfdb.WFDB_SampleArray(nsig)
    vout = wfdb.WFDB_SampleArray(nsig)
    if wfdb.isigopen(argv[1], s.cast(), nsig) != nsig: sys.exit(2)
    if wfdb.osigopen("8l", s.cast(), nsig) <= 0: sys.exit(3)
    while nsamp > 0 and wfdb.getvec(vin.cast()) > 0:
        nsamp -= 1
        for i in range(0, nsig): vout[i] -= vin[i]
        if wfdb.putvec(vout.cast()) < 0: break
        for i in range(0, nsig): vout[i] = vin[i]
    wfdb.newheader("dif")
    wfdb.wfdbquit()
Ejemplo n.º 8
0
def main(argv):
    an = wfdb.WFDB_AnninfoArray(2)
    annot = wfdb.WFDB_Annotation()
    if len(argv) < 2:
        print "usage:", argv[0], "record"
        sys.exit(1)
    a = an[0]
    a.name = "atr"
    a.stat = wfdb.WFDB_READ
    an[0] = a
    a = an[1]
    a.name = "aha"
    a.stat = wfdb.WFDB_AHA_WRITE
    an[1] = a
    if wfdb.annopen(argv[1], an.cast(), 2) < 0: sys.exit(2)
    while 1:
        if not (wfdb.getann(0, annot) == 0 and wfdb.putann(0,annot) == 0):
            break
    wfdb.wfdbquit()
Ejemplo n.º 9
0
def main(argv):
    a = wfdb.WFDB_Anninfo()
    annot = wfdb.WFDB_Annotation()
    if len(argv) < 3:
        print "usage:", argv[0], "annotator record"
        sys.exit(1)
    a.name = argv[1]
    a.stat = wfdb.WFDB_READ
    wfdb.sampfreq(argv[2])
    if wfdb.annopen(argv[2], a, 1) < 0:
        sys.exit(2)
    while wfdb.getann(0, annot) == 0:
        if annot.aux is not None:
            aux = annot.aux[1:]
        else:
            aux = ""
        print wfdb.timstr(-annot.time), "(" + str(annot.time) + ")", wfdb.annstr(
            annot.anntyp
        ), annot.subtyp, annot.chan, annot.num, aux
    wfdb.wfdbquit()
Ejemplo n.º 10
0
def main(argv):
    if len(argv) < 2:
        print "usage:", argv[0], "record"
        sys.exit(1)
    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig < 1: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    if wfdb.isigopen(argv[1], s.cast(), nsig) != nsig: sys.exit(2)
    print "Record", argv[1]
    print "Starting time:", wfdb.timstr(0)
    print "Sampling frequency: %(sf)g Hz" % {'sf': wfdb.sampfreq(argv[1])}
    print nsig, "signals"
    for i in range(0,nsig):
        print "Group %(g)d, Signal %(s)d:" % {'g': s[i].group, 's': i }
        print " File:", s[i].fname
        print " Description:", s[i].desc
        print " Gain:",
        if s[i].gain == 0.:
            print "uncalibrated; assume", wfdb.WFDB_DEFGAIN,
        else:
            print "%(gain)g" % {'gain': s[i].gain},
        if s[i].units is not None:
            print "adu/" + s[i].units
        else:
            print "adu/mV"
        print " Initial value:", s[i].initval
        print " Storage format:", s[i].fmt
        print " I/O:",
        if s[i].bsize == 0:
            print "can be unbuffered"
        else:
            print str(s[i].bsize) + "-byte blocks"
        print " ADC resolution:", s[i].adcres, "bits"
        print " ADC zero:", s[i].adczero
        if s[i].nsamp > 0:
            print " Length:", wfdb.timstr(s[i].nsamp), \
                  "(" + str(s[i].nsamp) + " sample intervals)"
            print " Checksum:", s[i].cksum
        else:
            print " Length undefined"
    wfdb.wfdbquit()
Ejemplo n.º 11
0
def main(argv):
    record = ''

    # Parse the arguments
    try:
        opts, args = getopt.getopt(argv, "hr:", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == '-r':
            record = arg

    # Read the number of signals in the record
    nsig = wfdb.isigopen(record, None, 0)

    # Exit if the record is not found, or there are no signals
    if nsig < 1:
        usage()
        sys.exit(2)

    siarray = wfdb.WFDB_SiginfoArray(nsig)
    wfdb.isigopen(record, siarray, nsig)

    n = 0
    v = wfdb.intArray(nsig)

    # Loop over each sample and print the signal values.
    while wfdb.getvec(v) > 0:
        print n,
        for i in range(0,nsig):
            print v[i],
        print
        n = n + 1
        
    wfdb.wfdbquit()
Ejemplo n.º 12
0
def main(argv):

    time = maxslope = nslope = scmin = 0
    a = wfdb.WFDB_Anninfo()
    annot = wfdb.WFDB_Annotation()

    if len(argv) < 2:
        print "usage:", argv[0], "record [threshold]"
        sys.exit(1)
    
    a.name = "qrs"
    a.stat = wfdb.WFDB_WRITE

    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig < 1: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    v = wfdb.WFDB_SampleArray(nsig)
    if wfdb.wfdbinit(argv[1], a, 1, s.cast(), nsig) != nsig: sys.exit(2)
    if wfdb.sampfreq(None) < 240. or wfdb.sampfreq(None) > 260.:
	wfdb.setifreq(250.)
    if len(argv) > 2: scmin = wfdb.muvadu(0, argv[2])
    if scmin < 1: scmin = wfdb.muvadu(0, 1000)
    slopecrit = scmax = 10 * scmin
    ms160 = wfdb.strtim("0.16")
    ms200 = wfdb.strtim("0.2")
    s2 = wfdb.strtim("2")
    annot.subtyp = annot.chan = annot.num = 0
    annot.aux = None
    wfdb.getvec(v.cast())
    t9 = t8 = t7 = t6 = t5 = t4 = t3 = t2 = t1 = v[0]

    while 1:
        t0 = v[0]
        filter = t0 + 4*t1 + 6*t2 + 4*t3 + t4 - t5 - 4*t6 - 6*t7 - 4*t8 - t9
        if time % s2 == 0:
            if nslope == 0:
                slopecrit -= slopecrit >> 4
                if slopecrit < scmin: slopecrit = scmin
            elif nslope >= 5:
                slopecrit += slopecrit >> 4
                if slopecrit > scmax: slopecrit = scmax
        if nslope == 0 and abs(filter) > slopecrit: 
            nslope = 1
            maxtime = ms160
            if filter > 0:
                sign = 1
            else:
                sign = -1
            qtime = time
        if nslope != 0:
            if filter * sign < -slopecrit: 
                sign = -sign
                nslope = nslope + 1
                if nslope > 4:       
                    maxtime = ms200
                else:
                    maxtime = ms160
            elif filter * sign > slopecrit and \
                     abs(filter) > maxslope:
                maxslope = abs(filter)
            if maxtime < 0:
                if 2 <= nslope and nslope <= 4:
                    slopecrit += ((maxslope>>2) - slopecrit) >> 3
                    if slopecrit < scmin:
                        slopecrit = scmin
                    elif slopecrit > scmax:
                        slopecrit = scmax
                    annot.time = wfdb.strtim("i") - (time - qtime) - 4
                    annot.anntyp = wfdb.NORMAL
                    wfdb.putann(0, annot)
                    time = 0
                elif nslope >= 5: 
                    annot.time = wfdb.strtim("i") - (time - qtime) - 4
                    annot.anntyp = wfdb.ARFCT
                    wfdb.putann(0, annot)
                nslope = 0
            maxtime = maxtime - 1
        t9 = t8
        t8 = t7
        t7 = t6
        t6 = t5
        t5 = t4
        t4 = t3
        t3 = t2
        t2 = t1
        t1 = t0
        time = time + 1
        if not wfdb.getvec(v.cast()) > 0: break

    wfdb.wfdbquit()
Ejemplo n.º 13
0
def main(argv):
    db_path = "/opt/physiobank/database"
    record = ''
    wfdb.setwfdb(db_path)
    # Parse the arguments
    try:
        opts, args = getopt.getopt(argv, "hr:", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == '-r':
            record = arg

    # Read the number of signals in the record
    nsig = wfdb.isigopen(record, None, 0)

    # Exit if the record is not found, or there are no signals
    if nsig < 1:
        usage()
        sys.exit(2)

    siarray = wfdb.WFDB_SiginfoArray(nsig)
    wfdb.isigopen(record, siarray, nsig)

    n = 0
    v = wfdb.intArray(nsig)
    lf = bf_math.intArray(siarray[0].nsamp)
    global sld_samp
    global ln
    global y_axes
    global sr_ds  
    global subplt
    sr = (int)(wfdb.sampfreq(record))
    print "sr:",sr
    sr_ds = 50
    # Loop over each sample and print the signal values.
    while wfdb.getvec(v) > 0:
        for i in range(0,1):
            lf[n] = v[i]
        n = n + 1
    bf_math.filtering(lf,siarray[0].nsamp,sr)
    _nsamp = siarray[0].nsamp
    
    x_axes = xrange(_nsamp*sr_ds/sr)
    print "x_axes:", _nsamp*sr_ds/sr
    y_axes = []
    for i in xrange(_nsamp):
         b, val = bf_math.down_sample(lf[i], sr, sr_ds)
         if (b):
             y_axes.append(val)
    print "y_axes:", len(y_axes)
    plt.figure(1)
    subplt = plt.subplot(111)
    ln, = plt.plot(xrange(0,5*sr_ds), y_axes[0:5*sr_ds] )
    #plt.axis([-100, 100])
    plt.ylim(-300,300)
    #subplt.autoscale_view(True,True,True)
    axcolor = 'lightgoldenrodyellow'
    axsamp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
    sld_samp = Slider(axsamp, 'samp', 0.1, 33*60, valinit=1)

    sld_samp.on_changed(update)
    plt.show()    
    wfdb.wfdbquit()
Ejemplo n.º 14
0
def main(argv):
    # First get a wrapper object to C array of Anninfo structures.
    an = wfdb.WFDB_AnninfoArray(2)
    # In early versions of the wrapper this would have also allocated
    # the WFDB_Anninfo C-structures in the array.  As of SWIG v2.0.8
    # it appears that only the array is allocated, not the Anninfo
    # structures within.


    # Get a wrapper object for an annotation structure 
    annot = wfdb.WFDB_Annotation()

    # Check to see if there is command line argument
    if len(argv) < 2:
        # no command line argument, so print usage and exit
        print "usage:", argv[0], "record"
        sys.exit(1)

    record = argv[1] # the record name is the first argument

    # Now we're going to setup the Anninfo structures so we can call
    # annopen later

    # First get a new Anninfo structure.
    a = wfdb.WFDB_Anninfo()   
    # In the early version this line was:
    #   a = an[0]
    # because calling wfdb.AnninfoArray(2), previously allocated
    # each Anninfo info the the array as well. Now we need to explicitly
    # allocate a new Anninfo struct instead of getting one from the 
    # AnninfoArray "an"

    # now assign the values of a
    a.name = "atr"
    a.stat = wfdb.WFDB_READ

    # finally assign the anninfo struct, "a", to its place in the
    # array "an"
    an[0] = a

    # repeat for output annotation
    a = wfdb.WFDB_Anninfo()
    a.name = "aha"
    a.stat = wfdb.WFDB_AHA_WRITE
    an[1] = a

    # We call annopen with the record name, given on the command line,
    # and the AnninfoArray, "an", we just setup.  
    if wfdb.annopen(record, an, 2) < 0: sys.exit(2)
    # note that in previous versions, it was necessary to use 'an.cast()',
    # rather then simply 'an', when passing an AnninfoArray to a wfdb
    # functoin.  Now the wrappers no longer require the 'cast()' in most
    # cases.

    # now iterate until all annotations have been ready
    while 1:
        # read an annotation from the input annotator and put it into
        # the output, AHA formatted, annotator
        if not (wfdb.getann(0, annot) == 0 and wfdb.putann(0,annot) == 0):
            break
    wfdb.wfdbquit()
Ejemplo n.º 15
0
def main(argv):
    nbeats = stoptime = 0
    a = wfdb.WFDB_Anninfo()
    annot = wfdb.WFDB_Annotation()

    if len(argv) < 3:
        print "usage:", argv[0], "annotator record [beat-type from to]",
        sys.exit(1)    
    a.name = argv[1]
    a.stat = wfdb.WFDB_READ
    nsig = wfdb.isigopen(argv[2], None, 0)
    if nsig < 1: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    v = wfdb.WFDB_SampleArray(nsig)
    vb = wfdb.WFDB_SampleArray(nsig)
    sum = [None] * nsig
    if wfdb.wfdbinit(argv[2], a, 1, s.cast(), nsig) != nsig: sys.exit(3)
    hwindow = wfdb.strtim(".05")
    window = 2*hwindow + 1
    for i in range(nsig):
        sum[i] = [0] * window
    if len(argv) > 3:
        btype = wfdb.strann(argv[3])
    else:
        btype = wfdb.NORMAL
    if len(argv) > 4: wfdb.iannsettime(wfdb.strtim(argv[4]))
    if len(argv) > 5:
        stoptime = wdfb.strtim(argv[5])
        if stoptime < 0:
            stoptime = -stoptime
        if s[0].nsamp > 0 and stoptime > s[0].nsamp:
            stoptime = s[0].nsamp    
    else:
        stoptime = s[0].nsamp
    if stoptime > 0: stoptime -= hwindow

    while 1:
        if not (wfdb.getann(0, annot) == 0 and annot.time < hwindow): break 
    while 1:        
        if annot.anntyp == btype:
            wfdb.isigsettime(annot.time - hwindow - 1)
            wfdb.getvec(vb.cast())
            j=0
            while j < window and wfdb.getvec(v.cast()) > 0:
                for i in range(nsig):
                    sum[i][j] += v[i] - vb[i]
                j += 1
            nbeats += 1
        if not (wfdb.getann(0, annot) == 0 and \
           (stoptime == 0L or annot.time < stoptime)): break
    
    if nbeats < 1:
        print argv[0] + ": no `" + wfdb.annstr(btype) + "' beats found"
        sys.exit(4)

    print "Average of", nbeats, "`" + wfdb.annstr(btype) + "' beats:"
    for j in range(window):
        for i in range(nsig):
            print "%(av)g" % {'av': float(sum[i][j])/nbeats},
            sys.stdout.write("") # surpress next space
            if i < nsig-1:
                print "\t",
            else:
                print
    wfdb.wfdbquit()
Ejemplo n.º 16
0
def main(argv):
    record = ''
    annotator = ''

    # parse the arguments
    try:
        opts, args = getopt.getopt(argv, "hr:a:", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == '-r':
            record = arg
        elif opt == '-a':
            annotator = arg

    if len(record) == 0 or len(annotator) == 0:
        usage()
        sys.exit(2)
        
    # set the sampling frequency
    sps = wfdb.sampfreq(record)
    if sps < 0:
        sps = wfdb.WFDB_DEFFREQ
        wfdb.setsampfreq(sps)
            
    # get a new anninfo object
    ai = wfdb.WFDB_Anninfo()
    
    # set ai fields
    ai.name = annotator
    ai.stat = wfdb.WFDB_READ
    
    # open the annotation
    result = wfdb.annopen( record, ai, 1 )
    
    if result < 0:
        usage()
        sys.exit(2)
        
    # get a new annotation object
    annot = wfdb.WFDB_Annotation()
    
    while wfdb.getann(0, annot) == 0:

        # remove first char from aux string, if there is one
        if annot.aux is not None:
            aux = annot.aux[1:]
        else:
            aux = ""
                
        print "%s\t%d\t%s\t%d\t%d\t%d\t%s" % ( \
              wfdb.mstimstr(-annot.time), \
              annot.time, \
              wfdb.annstr(annot.anntyp),
              annot.subtyp,
              annot.chan, \
              annot.num, \
              aux)

    wfdb.wfdbquit()