Пример #1
0
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
Пример #2
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)
Пример #3
0
def extract_pwave(output_folder, record, rec_name, annotation, start_time,
                  end_time):
    ## time for which you want to read record ##
    nsamp, freq, annot, init_time, sdata = ws.setupWfdb(rec_name, annotation)

    #print("nsamp:"+str(nsamp)+" freq:"+str(freq));

    rdann_file = output_folder + "ecgpu_output.txt"
    s1_rdann_file = output_folder + "s1_ecgpu_output.txt"
    output_ann = "output_annotator"

    #ecgpu_path="/home/ubuntu/Documents/Thesis_work/ecgpuwave-1.3.2"
    #change directory to output_folder
    os.chdir(output_folder)

    signal_0 = "0"
    os.system("rm -f " + rdann_file)
    cmd_create_ann = "ecgpuwave -r " + rec_name + " " + "-a" + " " + output_ann + " -f " + start_time + " -t " + end_time + " -i " + annotation + " -s " + signal_0
    print(cmd_create_ann)
    os.system(cmd_create_ann)

    #use rdann to ouput annotations as a text file
    cmd_disp_ann = "rdann -r " + rec_name + " -a output_annot"
    #push output text to a file
    print(cmd_disp_ann)
    os.system(cmd_disp_ann + ">" + rdann_file)

    s0_l_p_wave_times = [
    ]  # this will contain p wave values for 1 rec and will be emptied everytime

    ## code for p wave time calculation goes here ####
    f = open(rdann_file, 'r')
    for line in f:

        temp = line.split()
        if (temp[2] == '(') and (temp[-1] == '0'):
            start_sample_num = float(temp[1])
            #print ("start sample num is : "+ str(start_sample_num))
        elif (temp[2] == ')') and (temp[-1] == '0'):
            end_sample_num = float(temp[1])
            #print("end_sample_num is: " +str(end_sample_num))
            p_duration_ms = (end_sample_num - start_sample_num) * (1000 / freq)
            s0_l_p_wave_times.append(p_duration_ms)
            #print ("pwave duration from signal 0 is : " + str(p_duration_ms))
    ##### extract p wave times for second signal
    signal_1 = "1"
    os.system("rm -f " + s1_rdann_file)
    cmd_create_ann = "ecgpuwave -r " + rec_name + " " + "-a" + " " + output_ann + " -f " + start_time + " -t " + end_time + " -i " + annotation + " -s " + signal_1
    print(cmd_create_ann)
    os.system(cmd_create_ann)

    #use rdann to ouput annotations as a text file
    cmd_disp_ann = "rdann -r " + rec_name + " -a output_annot"
    #push output text to a file
    print(cmd_disp_ann)
    os.system(cmd_disp_ann + ">" + s1_rdann_file)

    s1_l_p_wave_times = [
    ]  # this will contain p wave values for 1 rec and will be emptied everytime

    ## code for p wave time calculation goes here ####
    f = open(s1_rdann_file, 'r')
    for line in f:

        temp = line.split()
        if (temp[2] == '(') and (temp[-1] == '0'):
            start_sample_num = float(temp[1])
            #print ("start sample num is : "+ str(start_sample_num))
        elif (temp[2] == ')') and (temp[-1] == '0'):
            end_sample_num = float(temp[1])
            #print("end_sample_num is: " +str(end_sample_num))
            p_duration_ms = (end_sample_num - start_sample_num) * (1000 / freq)
            #print ("pwave duration from signal 1 is : " + str(p_duration_ms))
            s1_l_p_wave_times.append(p_duration_ms)
    return s0_l_p_wave_times, s1_l_p_wave_times
Пример #4
0
def extract_wave_times(output_folder, record, rec_name, annotation, start_time,
                       end_time, signal_num):
    ##The num field of each WFON and WFOFF annotation designates the type of waveform with which it is associated: 0 for a P wave, 1 for a QRS complex, or 2 for a T wave.

    #     ## doing wave num encoding here:
    #     if wave_name == 'p':
    #         wave_num = '0'
    #     elif wave_name == 'r':
    #         wave_num = '1'
    #     elif wave_name =='t':
    #         wave_num = '2'

    ## time for which you want to read record ##
    nsamp, freq, annot, init_time, sdata = ws.setupWfdb(rec_name, annotation)

    rdann_file = output_folder + "ecgpu_output.txt"
    output_ann = "output_annotator"

    os.chdir(output_folder)

    signal = signal_num
    os.system("rm -f " + rdann_file)
    cmd_create_ann = "ecgpuwave -r " + rec_name + " " + "-a" + " " + output_ann + " -f " + start_time + " -t " + end_time + " -i " + annotation + " -s " + signal
    print(cmd_create_ann)
    os.system(cmd_create_ann)

    #use rdann to ouput annotations as a text file
    cmd_disp_ann = "rdann -r " + rec_name + " -a output_annot"
    #push output text to a file
    print(cmd_disp_ann)
    os.system(cmd_disp_ann + ">" + rdann_file)

    ## features we want to extract:
    # p_dur=p_off-p_on
    # p_ini=p_p_peak-p_on
    # p_ter = p_off - p_peak
    # p_asy=p_ter/p_ini

    #pr_on
    #pr_peak
    #r_off

    l_wave_dur_time = [
    ]  # this will contain p/t wave values for 1 rec and will be emptied everytime
    l_wave_ini_time = []
    l_wave_ter_time = []
    l_wave_asy_time = []
    l_pr_on_time = []
    l_pr_peak_time = []
    l_pr_off_time = []
    l_pp_on_time = []

    ## code for p wave time calculation goes here ####
    f = open(rdann_file, 'r')
    p_true = 0
    for line in f:

        temp = line.split()
        if (temp[2] == '(') and (temp[-1] == '0'):
            start_sample_num = float(temp[1])
            #print ("start sample num is : "+ str(start_sample_num))
        elif (temp[2] == 'p') and (temp[-1] == '0'):
            peak_sample_num = float(temp[1])
            p_true = 1
        elif (temp[2] == ')') and (temp[-1] == '0'):
            end_sample_num = float(temp[1])
            #print("end_sample_num is: " +str(end_sample_num))
            wave_duration_ms = (end_sample_num - start_sample_num) * (1000 /
                                                                      freq)
            wave_ini_ms = (peak_sample_num - start_sample_num) * (1000 / freq)
            wave_ter_ms = (end_sample_num - peak_sample_num) * (1000 / freq)
            wave_asy_ms = wave_ter_ms / wave_ini_ms
            l_wave_dur_time.append(wave_duration_ms)
            l_wave_ini_time.append(wave_ini_ms)
            l_wave_ter_time.append(wave_ter_ms)
            l_wave_asy_time.append(wave_asy_ms)
        if (temp[2] == 'N') and (temp[-1] == '0') and (
                p_true == 1
        ):  # this is looking for r peakvand making sure that p wave was calculated previously
            r_peak_sample = float(temp[1])
            pr_on_ms = (r_peak_sample - start_sample_num) * (1000 / freq)
            pr_peak_ms = (r_peak_sample - peak_sample_num) * (1000 / freq)
            pr_off_ms = (r_peak_sample - end_sample_num) * (1000 / freq)
            p_true = 0
            l_pr_on_time.append(pr_on_ms)
            l_pr_peak_time.append(pr_peak_ms)
            l_pr_off_time.append(pr_off_ms)

    for i in range(len(l_wave_dur_time) - 1):
        pp_on_ms = l_wave_dur_time[i + 1] - l_wave_dur_time[i]
        l_pp_on_time.append(pp_on_ms)
    #print ("pwave duration from signal 0 is : " + str(p_duration_ms))
    ##### extract p wave times for second signal
    all_time_features = [
        l_wave_dur_time, l_wave_ini_time, l_wave_ter_time, l_wave_asy_time,
        l_pr_on_time, l_pr_peak_time, l_pr_off_time
    ]
    return all_time_features
Пример #5
0
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
Пример #6
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)    
Пример #7
0
def extract_pwave(output_folder,record,rec_name,annotation,start_time,end_time):
    ## time for which you want to read record ##
    nsamp, freq, annot, init_time,sdata=ws.setupWfdb(rec_name, annotation)
    
    
    #print("nsamp:"+str(nsamp)+" freq:"+str(freq));
    
    rdann_file=output_folder+"ecgpu_output.txt"
    s1_rdann_file=output_folder+"s1_ecgpu_output.txt"
    output_ann="output_annotator"
    
    
    #ecgpu_path="/home/ubuntu/Documents/Thesis_work/ecgpuwave-1.3.2"
    #change directory to output_folder
    os.chdir(output_folder) 
    
    signal_0="0";
    os.system("rm -f "+rdann_file)
    cmd_create_ann="ecgpuwave -r "+rec_name +" "+"-a"+" "+output_ann+ " -f "+start_time+" -t "+ end_time +" -i "+annotation+" -s "+signal_0
    print(cmd_create_ann)
    os.system(cmd_create_ann)
    
    #use rdann to ouput annotations as a text file
    cmd_disp_ann="rdann -r "+rec_name+" -a output_annot"
    #push output text to a file 
    print (cmd_disp_ann)
    os.system(cmd_disp_ann +">" +rdann_file)

    s0_l_p_wave_times=[] # this will contain p wave values for 1 rec and will be emptied everytime
    
    ## code for p wave time calculation goes here ####
    f=open(rdann_file,'r')
    for line in f:
        
        temp=line.split()
        if (temp[2] == '(') and (temp[-1] =='0'):
            start_sample_num=float(temp[1])
            #print ("start sample num is : "+ str(start_sample_num))
        elif (temp[2] == ')') and (temp[-1] =='0'):
            end_sample_num=float(temp[1])
            #print("end_sample_num is: " +str(end_sample_num))
            p_duration_ms=(end_sample_num-start_sample_num) *(1000/freq);
            s0_l_p_wave_times.append(p_duration_ms)
            #print ("pwave duration from signal 0 is : " + str(p_duration_ms))
    ##### extract p wave times for second signal
    signal_1="1";
    os.system("rm -f "+s1_rdann_file)
    cmd_create_ann="ecgpuwave -r "+rec_name +" "+"-a"+" "+output_ann+ " -f "+start_time+" -t "+ end_time +" -i "+annotation+" -s "+signal_1
    print(cmd_create_ann)
    os.system(cmd_create_ann)
     
    #use rdann to ouput annotations as a text file
    cmd_disp_ann="rdann -r "+rec_name+" -a output_annot"
    #push output text to a file 
    print (cmd_disp_ann)
    os.system(cmd_disp_ann +">" +s1_rdann_file)
 
    s1_l_p_wave_times=[] # this will contain p wave values for 1 rec and will be emptied everytime
     
    ## code for p wave time calculation goes here ####
    f=open(s1_rdann_file,'r')
    for line in f:
         
        temp=line.split()
        if (temp[2] == '(') and (temp[-1] =='0'):
            start_sample_num=float(temp[1])
            #print ("start sample num is : "+ str(start_sample_num))
        elif (temp[2] == ')') and (temp[-1] =='0'):
            end_sample_num=float(temp[1])
            #print("end_sample_num is: " +str(end_sample_num))
            p_duration_ms=(end_sample_num-start_sample_num) *(1000/freq);
            #print ("pwave duration from signal 1 is : " + str(p_duration_ms))
            s1_l_p_wave_times.append(p_duration_ms)
    return s0_l_p_wave_times,s1_l_p_wave_times
Пример #8
0
def extract_wave_times(output_folder,record,rec_name,annotation,start_time,end_time,signal_num):   
    ##The num field of each WFON and WFOFF annotation designates the type of waveform with which it is associated: 0 for a P wave, 1 for a QRS complex, or 2 for a T wave. 
    

#     ## doing wave num encoding here:
#     if wave_name == 'p':
#         wave_num = '0'
#     elif wave_name == 'r':
#         wave_num = '1'
#     elif wave_name =='t':
#         wave_num = '2'
    
    ## time for which you want to read record ##
    nsamp, freq, annot, init_time,sdata=ws.setupWfdb(rec_name, annotation)
    
    
    rdann_file=output_folder+"ecgpu_output.txt"
    output_ann="output_annotator"
    
    os.chdir(output_folder) 
    
    signal=signal_num;
    os.system("rm -f "+rdann_file)
    cmd_create_ann="ecgpuwave -r "+rec_name +" "+"-a"+" "+output_ann+ " -f "+start_time+" -t "+ end_time +" -i "+annotation+" -s "+signal
    print(cmd_create_ann)
    os.system(cmd_create_ann)
    
    #use rdann to ouput annotations as a text file
    cmd_disp_ann="rdann -r "+rec_name+" -a output_annot"
    #push output text to a file 
    print (cmd_disp_ann)
    os.system(cmd_disp_ann +">" +rdann_file)
    
    ## features we want to extract:
    # p_dur=p_off-p_on
    # p_ini=p_p_peak-p_on
    # p_ter = p_off - p_peak
    # p_asy=p_ter/p_ini
    
    #pr_on
    #pr_peak
    #r_off
   
    l_wave_dur_time=[]  # this will contain p/t wave values for 1 rec and will be emptied everytime
    l_wave_ini_time=[]
    l_wave_ter_time=[]
    l_wave_asy_time=[]
    l_pr_on_time=[]
    l_pr_peak_time=[]
    l_pr_off_time=[]
    l_pp_on_time=[]
    
    ## code for p wave time calculation goes here ####
    f=open(rdann_file,'r')
    p_true=0
    for line in f:
        
        temp=line.split()
        if (temp[2] == '(') and (temp[-1] =='0'):
            start_sample_num=float(temp[1])
            #print ("start sample num is : "+ str(start_sample_num))
        elif (temp[2] == 'p') and (temp[-1] =='0'):
            peak_sample_num=float(temp[1])
            p_true=1;
        elif (temp[2] == ')') and (temp[-1] =='0'):
            end_sample_num=float(temp[1])
            #print("end_sample_num is: " +str(end_sample_num))
            wave_duration_ms=(end_sample_num-start_sample_num) *(1000/freq);
            wave_ini_ms=(peak_sample_num-start_sample_num)*(1000/freq)
            wave_ter_ms=(end_sample_num-peak_sample_num)*(1000/freq)
            wave_asy_ms=wave_ter_ms/wave_ini_ms
            l_wave_dur_time.append(wave_duration_ms)
            l_wave_ini_time.append(wave_ini_ms)
            l_wave_ter_time.append(wave_ter_ms)
            l_wave_asy_time.append(wave_asy_ms)
        if (temp[2] == 'N') and (temp[-1] =='0') and (p_true== 1): # this is looking for r peakvand making sure that p wave was calculated previously
            r_peak_sample=float(temp[1])
            pr_on_ms=(r_peak_sample -start_sample_num)*(1000/freq)
            pr_peak_ms=(r_peak_sample-peak_sample_num)*(1000/freq)
            pr_off_ms=(r_peak_sample-end_sample_num)*(1000/freq)
            p_true=0;
            l_pr_on_time.append(pr_on_ms)
            l_pr_peak_time.append(pr_peak_ms)
            l_pr_off_time.append(pr_off_ms)
                    
    for i in range(len(l_wave_dur_time)-1):
        pp_on_ms=l_wave_dur_time[i+1]- l_wave_dur_time[i]
        l_pp_on_time.append(pp_on_ms)
    #print ("pwave duration from signal 0 is : " + str(p_duration_ms))
    ##### extract p wave times for second signal 
    all_time_features=[l_wave_dur_time,l_wave_ini_time,l_wave_ter_time,l_wave_asy_time,l_pr_on_time,l_pr_peak_time,l_pr_off_time]
    return all_time_features