Exemple #1
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()
def get_RR_interval(rec_name, annotation, start_time, end_time):
    #setup wfdb (change annotator here)
    rr_int = 0
    t = 0
    beats = 0
    sig_time = []

    #change annotations
    nsamp, freq, annot, init_time, sdata = ws.setupWfdb(rec_name, annotation)
    RR_sec_func = []
    #num_sample_end=38400; # this translates to 230339 annot time which means 30 mins of data ## check this
    num_sample_start = start_time * 60 * freq
    num_sample_end = end_time * 60 * freq
    ###### check if its the same record or new. if new , shift annot.time

    #         annot.time

    #getann reads next annotation and returns 0 when successful
    # annot.time is time of the annotation, in samples from the beginning of the record.
    #when getting samples for cross-validation and testing, just adjust annot.time so it starts reading samples from there

    t = annot.time
    ##comment june 16
    #print ("annot.time at the beginning is: " + str(annot.time))

    #annot_file=open('/home/ubuntu/Documents/eclispe_workspace/test_one/my_first_pyproj/research_March17/annot_type' +str(record)+'.txt','a');

    while wfdb.getann(0, annot) == 0:
        if annot.time > num_sample_start and annot.time < num_sample_end:
            #code for extracting time:
            time = wfdb.timstr(-annot.time)
            split_time = time.split(":")
            hr_time = split_time[0]
            minute_time = split_time[1]
            if len(split_time) > 2:
                sec_time = split_time[2]

            if wfdb.wfdb_isqrs(annot.anntyp):
                #             if same_as_prev[record] == 1 and int(hr_time) >= 1 and int(hr_time) < 23:
                #annot.time=annot.time + prev_annot_time;
                rr_int = annot.time - t
                beats = beats + 1
                rr_sec = rr_int / freq
                rr_sec_rounded = round(rr_sec, 3)

                ###############testing here############################

                ############################################

                RR_sec_func.append(rr_sec_rounded)
                # sampling intervals (e.g., if the original recording was sampled at 128 samples per second, then an
                t = annot.time
                #print ("annot.time after rr interval is: " + str(t))

                #print ("-annot.time after rr interval is: " +  wfdb.timstr(-annot.time))
                #print ("annot.time in if is: " + str(annot.time))
                #prev_annot_time.append(annot.time)

    #print ("beats = "+ str(beats))
    return RR_sec_func
Exemple #3
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()
def get_RR_interval(rec_name,annotation,start_time,end_time):
        #setup wfdb (change annotator here)
    rr_int=0;
    t=0;
    beats=0;
    sig_time=[];
    
    #change annotations
    nsamp, freq, annot, init_time = setupWfdb(rec_name,annotation);
    RR_sec_func=[];
    #num_sample=38400; # this translates to 230339 annot time which means 30 mins of data ## check this
    num_sample=end_time*60*freq
    ###### check if its the same record or new. if new , shift annot.time

#         annot.time
    
    #getann reads next annotation and returns 0 when successful
    # annot.time is time of the annotation, in samples from the beginning of the record.
    #when getting samples for cross-validation and testing, just adjust annot.time so it starts reading samples from there
    
    
    t = annot.time;
    ##comment june 16
    #print ("annot.time at the beginning is: " + str(annot.time))
    
    #annot_file=open('/home/ubuntu/Documents/eclispe_workspace/test_one/my_first_pyproj/research_March17/annot_type' +str(record)+'.txt','a');
    
    while wfdb.getann(0, annot) == 0 and annot.time<num_sample:
        #code for extracting time:
        time=wfdb.timstr(-annot.time)
        split_time=time.split(":")
        hr_time=split_time[0];
        minute_time=split_time[1];
        if len(split_time) >2:
            sec_time=split_time[2];
        

        if wfdb.wfdb_isqrs(annot.anntyp):
#             if same_as_prev[record] == 1 and int(hr_time) >= 1 and int(hr_time) < 23: 
                #annot.time=annot.time + prev_annot_time;
            rr_int = annot.time - t
            beats=beats+1;
            rr_sec=rr_int/freq
            rr_sec_rounded=round(rr_sec,3);
            RR_sec_func.append(rr_sec_rounded);
            # sampling intervals (e.g., if the original recording was sampled at 128 samples per second, then an 
            t = annot.time
            #print ("annot.time after rr interval is: " + str(t))
            
            #print ("-annot.time after rr interval is: " +  wfdb.timstr(-annot.time))
            #print ("annot.time in if is: " + str(annot.time))
            #prev_annot_time.append(annot.time)
    
    
    #print ("beats = "+ str(beats))
    return RR_sec_func
Exemple #5
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()
Exemple #6
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()
Exemple #7
0
def get_ecg_signal(rec_name, annotation, start_time, end_time):
    #variables and arrays

    iteration = []
    sig_time = []
    #count=0;
    #ann_graph=[];
    #split_time0=[];
    #annotator_array=[];

    nsamp, freq, annot, init_time, sdata = ws.setupWfdb(rec_name, annotation)

    sig0 = []
    sig1 = []

    #physig0 is array with physical units
    physig0 = []
    physig1 = []

    print type(init_time)
    #print("strtim for starting value is: " + str(wfdb.strtim(init_time)));

    #print("total num of samples: " + str(nsamp));
    #print "Starting time of record is: "+ str(init_time);
    #print("sampling frequency is:"+ str(freq));

    #sample interval

    #required length of signal in seconds
    #num_sample_start=start_time*60*freq
    num_sample_end = end_time * 60 * freq
    loop_iteration = int(math.floor(num_sample_end))

    #print("loop iteration = " +str(loop_iteration));

    # loop runs for loop_iteration times to extract signal samples

    num_value = loop_iteration

    for i in range(0, num_value):
        if wfdb.getvec(sdata.cast()) < 0:
            print "ERROR: getvec() < 0"
            exit()
        else:
            #signal values in adu units:
            sig0.append(sdata[0])
            sig1.append(sdata[1])

            sig_time.append(gettime(i, freq, init_time))
            #print("time for sample " + str(i) + "is: " + str(sig_time[i]));
            #convert adu units to physical units and save in physig0 and 1 (later generalise it for n number of signals)
            physig0.append(aduphys(0, sig0[i]))
            physig1.append(aduphys(1, sig1[i]))

            #append iteration number as value in
            iteration.append(i)

    #getann reads next annotation and returns 0 when successful
    while wfdb.getann(0, annot) == 0:
        if annot.time > num_value:
            #print("annot.time>number of samples extracted");
            break
        #  annot.time is time of the annotation, in samples from the beginning of the record.
        print wfdb.timstr(-annot.time), "(" + str(
            annot.time) + ")", wfdb.annstr(
                annot.anntyp), annot.subtyp, annot.chan, annot.num
        print("signal value at this annotation is : " +
              str(physig0[annot.time]) + " " + str(sig_time[annot.time]))

    return (physig0, physig1, sig_time)
Exemple #8
0
#
#         #print("time for sample " + str(i) + "is: " + str(sig_time[i]));
#         #convert adu units to physical units and save in physig0 and 1 (later generalise it for n number of signals)
#         physig0.append(aduphys(0,sig0[i]));
#         physig1.append(aduphys(1,sig1[i]));
#         wfdb.timstr(-annot.time),"(" + str(annot.time)+ ")",wfdb.annstr(annot.anntyp), annot.subtyp,annot.chan, annot.num
#         #append iteration number as value in
#         iteration.append(i);

##########        READ ANNOTATION ##################
if wfdb.annopen(rec_name, a, 1) < 0:
    print("cannot open aanopen")
    exit()

#getann reads next annotation and returns 0 when successful
while wfdb.getann(0, annot) == 0:
    if annot.time > num_value:
        #if annot.time>=start_val and annot.time<=end_val:
        print("annot.time>number of samples extracted")
        break
    #  annot.time is time of the annotation, in samples from the beginning of the record.
    print wfdb.timstr(-annot.time), "(" + str(annot.time) + ")", wfdb.annstr(
        annot.anntyp), annot.subtyp, annot.chan, annot.num
# print ("signal value at this annotation is : " + str(physig0[annot.time])+" "+ str(sig_time[annot.time]));

#else:
#print("getann not working");
#exit();

#write signals to file
Exemple #9
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()
Exemple #10
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()
Exemple #11
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()
# 
#         #print("time for sample " + str(i) + "is: " + str(sig_time[i]));
#         #convert adu units to physical units and save in physig0 and 1 (later generalise it for n number of signals)
#         physig0.append(aduphys(0,sig0[i]));
#         physig1.append(aduphys(1,sig1[i]));
#         wfdb.timstr(-annot.time),"(" + str(annot.time)+ ")",wfdb.annstr(annot.anntyp), annot.subtyp,annot.chan, annot.num
#         #append iteration number as value in 
#         iteration.append(i);
        
##########        READ ANNOTATION ##################
if wfdb.annopen(rec_name, a, 1) < 0: 
    print("cannot open aanopen");
    exit();
    
#getann reads next annotation and returns 0 when successful
while wfdb.getann(0,annot) ==0:
    if annot.time>num_value:
    #if annot.time>=start_val and annot.time<=end_val: 
        print("annot.time>number of samples extracted");
        break;
    #  annot.time is time of the annotation, in samples from the beginning of the record.
    print wfdb.timstr(-annot.time),"(" + str(annot.time)+ ")",wfdb.annstr(annot.anntyp), annot.subtyp,annot.chan, annot.num
   # print ("signal value at this annotation is : " + str(physig0[annot.time])+" "+ str(sig_time[annot.time]));
    
    
#else:
    #print("getann not working");
    #exit();

#write signals to file
def get_ecg_signal(rec_name,annotation,start_time,end_time):
    #variables and arrays 
    
    iteration=[];
    sig_time=[];
    #count=0;
    #ann_graph=[];
    #split_time0=[];
    #annotator_array=[];
    
    
    nsamp, freq, annot, init_time,sdata = ws.setupWfdb(rec_name,annotation);
  
    sig0 = [];
    sig1 = [];
    
    #physig0 is array with physical units
    physig0=[];
    physig1=[];
    
    print type(init_time);
    #print("strtim for starting value is: " + str(wfdb.strtim(init_time)));
    
    #print("total num of samples: " + str(nsamp));
    #print "Starting time of record is: "+ str(init_time);
    #print("sampling frequency is:"+ str(freq));
    

    #sample interval
    
    #required length of signal in seconds
    #num_sample_start=start_time*60*freq
    num_sample_end=end_time*60*freq
    loop_iteration=int(math.floor(num_sample_end));
    
    #print("loop iteration = " +str(loop_iteration));
    
    
    # loop runs for loop_iteration times to extract signal samples
    
    num_value=loop_iteration;
    
    for i in range(0,num_value):
        if wfdb.getvec(sdata.cast()) < 0:
            print "ERROR: getvec() < 0";
            exit();
        else:
            #signal values in adu units:
            sig0.append(sdata[0]);
            sig1.append(sdata[1]);
            
            sig_time.append(gettime(i, freq, init_time));
            #print("time for sample " + str(i) + "is: " + str(sig_time[i]));
            #convert adu units to physical units and save in physig0 and 1 (later generalise it for n number of signals)
            physig0.append(aduphys(0,sig0[i]));
            physig1.append(aduphys(1,sig1[i]));
           
            #append iteration number as value in 
            iteration.append(i);
            
        
    #getann reads next annotation and returns 0 when successful
    while wfdb.getann(0,annot) ==0:
        if annot.time>num_value:
            #print("annot.time>number of samples extracted");
            break;
        #  annot.time is time of the annotation, in samples from the beginning of the record.
        print wfdb.timstr(-annot.time),"(" + str(annot.time)+ ")",wfdb.annstr(annot.anntyp), annot.subtyp,annot.chan, annot.num
        print ("signal value at this annotation is : " + str(physig0[annot.time])+" "+ str(sig_time[annot.time]));
    
    
    return(physig0,physig1,sig_time)