RR_sec_unclean = [] RR_sec = [] delta_RR_sec = [] x_val_sodp = [] y_val_sodp = [] rec_name = db_name + "/" + record #rec_name= "afpdb/p02c" ## time for which you want to read record ## start_time = 0 end_time = 30 #time in mins ##### Extract RR intervals here ####### RR_sec_unclean = pr.get_RR_interval(rec_name, annotation, start_time, end_time) ####DELETE THE FIRST RR_sec_unclean value##### del RR_sec_unclean[0] ##### APPLY FILTERS TO CLEAN DATA ####### #RR_sec=dc.detrend_data(dc.quotient_filt(dc.square_filt(RR_sec_unclean))) #RR_sec=dc.detrend_data((RR_sec_unclean) RR_sec = dc.quotient_filt(dc.square_filt(RR_sec_unclean)) #RR_sec=dc.square_filt(RR_sec_unclean) #RR_sec=RR_sec_unclean ##### Extract delta RR intervals here ####### delta_RR_sec = pr.get_delta_rr(RR_sec)
def calc_std_5min_sodp_measures(rec_name, annotation, total_min, radius_array): #this functions calculates features from 5 min of RR intervals for record , and calculates std of each feature for all 5min intervlas in a 30min record. #it returns all_features_6_intervals : each list corresponds to one feature and values within the list are denote the feature value per interval #it returns: std_dev_all_features :each value is the std dev of the feature in 6 intervals #it also returns feature_name_overall : names of features whose std dev can be found in std_dev_all_Features feature_list_5min = [] num_5min_interval = 1 #keeps track of which chunck of 5 min interval it is feature_name = [] start_time_arr = range(0, total_min, 5) end_time_arr = range(5, total_min + 5, 5) # everytime the loop runs it extracts feauters for those 5 mins for start_time, end_time in zip(start_time_arr, end_time_arr): error_rec_names = [] count_quad1 = 0 count_quad2 = 0 count_quad3 = 0 count_quad4 = 0 count_quad11 = 0 count_quad12 = 0 count_quad13 = 0 count_quad14 = 0 count_quad21 = 0 count_quad22 = 0 count_quad23 = 0 count_quad24 = 0 count_quad31 = 0 count_quad32 = 0 count_quad33 = 0 count_quad34 = 0 count_quad41 = 0 count_quad42 = 0 count_quad43 = 0 count_quad44 = 0 points_at_origin = 0 quad = 0 print("calculating std of 5 min sodp features") print("start time is: " + str(start_time)) print("end time is: " + str(end_time)) ##### Extract RR intervals here ####### RR_sec_unclean = pr.get_RR_interval(rec_name, annotation, start_time, end_time) ####DELETE THE FIRST RR_sec_unclean value##### del RR_sec_unclean[0] RR_sec = RR_sec_unclean #print("len of 5min RR_sec is: " + str(len(RR_sec))) num_RR_5min = len(RR_sec) #print("num_RR_5min_"+str(num_5min_interval)) x_val_sodp, y_val_sodp, ctm_array, ctm_feature_name, distance_array, dist_feature_name = calc_sodp_measures( rec_name, RR_sec, radius_array) ## calculate the number of poitns in each quadrant num_points_on_graph = len(x_val_sodp) ####plot sodp and save fig #### record_name = "n43" output_folder = "/home/ubuntu/Documents/Thesis_work/results/thesis_images/chapter_5/" fig_sodp, plot_sodp = graphs.plotScatter( record_name, x_val_sodp, y_val_sodp, "x[n+1]-x[n]", " x[n+2]-x[n+1] ", "SODP plot for 5min segment " + str(num_5min_interval), 'b', xlim_lo=-1, xlim_hi=1, ylim_lo=-1, ylim_hi=1, axline=1) fig_sodp.savefig(output_folder + "sodp_plot_" + record_name + "_" + str(num_5min_interval) + ".pdf", format='pdf') for x_val, y_val in zip(x_val_sodp, y_val_sodp): if x_val < -1 or x_val > 1 or y_val < -1 or y_val > 1: print("x_val is: " + str(x_val) + "y_val is: " + str(y_val)) print("errorneous value in: " + str(rec_name) + "_" + str(num_5min_interval)) error_rec_names.append(rec_name) elif x_val > 0 and y_val > 0: quad = 1 count_quad1 = count_quad1 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 11: count_quad11 = count_quad11 + 1 elif sub_quad is 12: count_quad12 = count_quad12 + 1 elif sub_quad is 13: count_quad13 = count_quad13 + 1 elif sub_quad is 14: count_quad14 = count_quad14 + 1 elif x_val < 0 and y_val > 0: quad = 2 count_quad2 = count_quad2 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 21: count_quad21 = count_quad21 + 1 elif sub_quad is 22: count_quad22 = count_quad22 + 1 elif sub_quad is 23: count_quad23 = count_quad23 + 1 elif sub_quad is 24: count_quad24 = count_quad24 + 1 elif x_val < 0 and y_val < 0: quad = 3 count_quad3 = count_quad3 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 31: count_quad31 = count_quad31 + 1 elif sub_quad is 32: count_quad32 = count_quad32 + 1 elif sub_quad is 33: count_quad33 = count_quad33 + 1 elif sub_quad is 34: count_quad34 = count_quad34 + 1 elif x_val > 0 and y_val < 0: quad = 4 count_quad4 = count_quad4 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 41: count_quad41 = count_quad41 + 1 elif sub_quad is 42: count_quad42 = count_quad42 + 1 elif sub_quad is 43: count_quad43 = count_quad43 + 1 elif sub_quad is 44: count_quad44 = count_quad44 + 1 else: points_at_origin = points_at_origin + 1 ratio_points_at_origin = float(points_at_origin) / num_points_on_graph #store the quad count values in features list feature_list_5min.append(ratio_points_at_origin) feature_name.append("ratio_points_at_origin_5min_" + str(num_5min_interval)) #calculate quad_ratio quad1_ratio = float(count_quad1) / num_points_on_graph quad2_ratio = float(count_quad2) / num_points_on_graph quad3_ratio = float(count_quad3) / num_points_on_graph quad4_ratio = float(count_quad4) / num_points_on_graph quad_11_ratio = float(count_quad11) / num_points_on_graph quad_12_ratio = float(count_quad12) / num_points_on_graph quad_13_ratio = float(count_quad13) / num_points_on_graph quad_14_ratio = float(count_quad14) / num_points_on_graph quad_21_ratio = float(count_quad21) / num_points_on_graph quad_22_ratio = float(count_quad22) / num_points_on_graph quad_23_ratio = float(count_quad23) / num_points_on_graph quad_24_ratio = float(count_quad24) / num_points_on_graph quad_31_ratio = float(count_quad31) / num_points_on_graph quad_32_ratio = float(count_quad32) / num_points_on_graph quad_33_ratio = float(count_quad33) / num_points_on_graph quad_34_ratio = float(count_quad34) / num_points_on_graph quad_41_ratio = float(count_quad41) / num_points_on_graph quad_42_ratio = float(count_quad42) / num_points_on_graph quad_43_ratio = float(count_quad43) / num_points_on_graph quad_44_ratio = float(count_quad44) / num_points_on_graph #store the quad ratio values in features list feature_list_5min.append(quad1_ratio) feature_name.append("std_quad1_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad2_ratio) feature_name.append("std_quad2_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad3_ratio) feature_name.append("std_quad3_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad4_ratio) feature_name.append("std_quad4_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_11_ratio) feature_name.append("std_quad11_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_12_ratio) feature_name.append("std_quad12_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_13_ratio) feature_name.append("std_quad13_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_14_ratio) feature_name.append("std_quad14_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_21_ratio) feature_name.append("std_quad21_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_22_ratio) feature_name.append("std_quad22_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_23_ratio) feature_name.append("std_quad23_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_24_ratio) feature_name.append("std_quad24_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_31_ratio) feature_name.append("std_quad31_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_32_ratio) feature_name.append("std_quad32_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_33_ratio) feature_name.append("std_quad33_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_34_ratio) feature_name.append("std_quad34_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_41_ratio) feature_name.append("std_quad41_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_42_ratio) feature_name.append("std_quad42_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_43_ratio) feature_name.append("std_quad43_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_44_ratio) feature_name.append("std_quad44_ratio_5min_" + str(num_5min_interval)) #print ("feature list for this 5 min interval is: " + str(feature_list_5min)) #print("indexes are: " + str(range(len(feature_list_5min)))) num_5min_interval = num_5min_interval + 1 print("rec_names with errornos records are : " + str(error_rec_names)) dist_bw_feature = 21 start_ind = 0 end_ind = 105 feature_name_overall = [] #print (len(feature_list_5min)) all_features_6_intervals = [] for i in range(0, 21): name = str(feature_name[i]) feature_name_overall.append(name[:-2]) #print("feature_name is: " + str(name[:-2])) feature_index_5min = range(start_ind, end_ind + 21, dist_bw_feature) one_feature_all_intervals = [] for val in feature_index_5min: #print("val is : "+str(val)) feature_val = feature_list_5min[val] #print("feature_val at index : " + str(val)+" is: "+str(feature_val)) one_feature_all_intervals.append(feature_val) all_features_6_intervals.append(one_feature_all_intervals) start_ind = start_ind + 1 end_ind = end_ind + 1 feature_index_5min = range(start_ind, end_ind + 21, dist_bw_feature) #print ("new indexes for feature index is: " + str(feature_index_5min)) std_dev_all_features = [] for list_feature_vals in all_features_6_intervals: std_dev_5min_feature = np.std(list_feature_vals) std_dev_all_features.append(std_dev_5min_feature) #for name,val in zip(feature_name_overall,std_dev_all_features): #print("name of feature is : " + str(name)) #print("val of feature is : " + str(val)) return all_features_6_intervals, std_dev_all_features, feature_name_overall
def calc_5min_sodp_measures(rec_name, annotation, total_min, radius_array): #this functions calculates features from 5 min of RR intervals for record and returns a list of all these features #for testing #start_time_arr=[0] #end_time_arr=[5] feature_list_5min = [] num_5min_interval = 1 #keeps track of which chunck of 5 min interval it is feature_name = [] start_time_arr = range(0, total_min, 5) end_time_arr = range(5, total_min + 5, 5) # everytime the loop runs it extracts feauters for those 5 mins for start_time, end_time in zip(start_time_arr, end_time_arr): error_rec_names = [] count_quad1 = 0 count_quad2 = 0 count_quad3 = 0 count_quad4 = 0 count_quad11 = 0 count_quad12 = 0 count_quad13 = 0 count_quad14 = 0 count_quad21 = 0 count_quad22 = 0 count_quad23 = 0 count_quad24 = 0 count_quad31 = 0 count_quad32 = 0 count_quad33 = 0 count_quad34 = 0 count_quad41 = 0 count_quad42 = 0 count_quad43 = 0 count_quad44 = 0 points_at_origin = 0 quad = 0 print("calculating 5 min sodp features") print("start time is: " + str(start_time)) print("end time is: " + str(end_time)) ##### Extract RR intervals here ####### RR_sec_unclean = pr.get_RR_interval(rec_name, annotation, start_time, end_time) ####DELETE THE FIRST RR_sec_unclean value##### del RR_sec_unclean[0] RR_sec = RR_sec_unclean print("len of 5min RR_sec is: " + str(len(RR_sec))) num_RR_5min = len(RR_sec) feature_list_5min.append(num_RR_5min) feature_name.append("num_RR_5min_" + str(num_5min_interval)) print("num_RR_5min_" + str(num_5min_interval)) x_val_sodp, y_val_sodp, ctm_array, ctm_feature_name, distance_array, dist_feature_name = calc_sodp_measures( rec_name, RR_sec, radius_array) ## calculate the number of poitns in each quadrant num_points_on_graph = len(x_val_sodp) for x_val, y_val in zip(x_val_sodp, y_val_sodp): if x_val < -1 or x_val > 1 or y_val < -1 or y_val > 1: print("x_val is: " + str(x_val) + "y_val is: " + str(y_val)) print("errorneous value in: " + str(rec_name) + "_" + str(num_5min_interval)) error_rec_names.append(rec_name) elif x_val > 0 and y_val > 0: quad = 1 count_quad1 = count_quad1 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 11: count_quad11 = count_quad11 + 1 elif sub_quad is 12: count_quad12 = count_quad12 + 1 elif sub_quad is 13: count_quad13 = count_quad13 + 1 elif sub_quad is 14: count_quad14 = count_quad14 + 1 elif x_val < 0 and y_val > 0: quad = 2 count_quad2 = count_quad2 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 21: count_quad21 = count_quad21 + 1 elif sub_quad is 22: count_quad22 = count_quad22 + 1 elif sub_quad is 23: count_quad23 = count_quad23 + 1 elif sub_quad is 24: count_quad24 = count_quad24 + 1 elif x_val < 0 and y_val < 0: quad = 3 count_quad3 = count_quad3 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 31: count_quad31 = count_quad31 + 1 elif sub_quad is 32: count_quad32 = count_quad32 + 1 elif sub_quad is 33: count_quad33 = count_quad33 + 1 elif sub_quad is 34: count_quad34 = count_quad34 + 1 elif x_val > 0 and y_val < 0: quad = 4 count_quad4 = count_quad4 + 1 sub_quad = count_points_in_16quad(x_val, y_val, quad) if sub_quad is 41: count_quad41 = count_quad41 + 1 elif sub_quad is 42: count_quad42 = count_quad42 + 1 elif sub_quad is 43: count_quad43 = count_quad43 + 1 elif sub_quad is 44: count_quad44 = count_quad44 + 1 else: points_at_origin = points_at_origin + 1 ratio_points_at_origin = float(points_at_origin) / num_points_on_graph #store the quad count values in features list feature_list_5min.append(points_at_origin) feature_name.append("points_at_origin_5min_" + str(num_5min_interval)) feature_list_5min.append(ratio_points_at_origin) feature_name.append("ratio_points_at_origin_5min_" + str(num_5min_interval)) feature_list_5min.append(count_quad1) feature_name.append("num_points_in_quad1_5min_" + str(num_5min_interval)) feature_list_5min.append(count_quad2) feature_name.append("num_points_in_quad2_5min_" + str(num_5min_interval)) feature_list_5min.append(count_quad3) feature_name.append("num_points_in_quad3_5min_" + str(num_5min_interval)) feature_list_5min.append(count_quad4) feature_name.append("num_points_in_quad4_5min_" + str(num_5min_interval)) #calculate quad_ratio quad1_ratio = float(count_quad1) / num_points_on_graph quad2_ratio = float(count_quad2) / num_points_on_graph quad3_ratio = float(count_quad3) / num_points_on_graph quad4_ratio = float(count_quad4) / num_points_on_graph quad_11_ratio = float(count_quad11) / num_points_on_graph quad_12_ratio = float(count_quad12) / num_points_on_graph quad_13_ratio = float(count_quad13) / num_points_on_graph quad_14_ratio = float(count_quad14) / num_points_on_graph quad_21_ratio = float(count_quad21) / num_points_on_graph quad_22_ratio = float(count_quad22) / num_points_on_graph quad_23_ratio = float(count_quad23) / num_points_on_graph quad_24_ratio = float(count_quad24) / num_points_on_graph quad_31_ratio = float(count_quad31) / num_points_on_graph quad_32_ratio = float(count_quad32) / num_points_on_graph quad_33_ratio = float(count_quad33) / num_points_on_graph quad_34_ratio = float(count_quad34) / num_points_on_graph quad_41_ratio = float(count_quad41) / num_points_on_graph quad_42_ratio = float(count_quad42) / num_points_on_graph quad_43_ratio = float(count_quad43) / num_points_on_graph quad_44_ratio = float(count_quad44) / num_points_on_graph #store the quad ratio values in features list feature_list_5min.append(quad1_ratio) feature_name.append("quad1_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad2_ratio) feature_name.append("quad2_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad3_ratio) feature_name.append("quad3_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad4_ratio) feature_name.append("quad4_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_11_ratio) feature_name.append("quad11_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_12_ratio) feature_name.append("quad12_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_13_ratio) feature_name.append("quad13_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_14_ratio) feature_name.append("quad14_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_21_ratio) feature_name.append("quad21_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_22_ratio) feature_name.append("quad22_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_23_ratio) feature_name.append("quad23_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_24_ratio) feature_name.append("quad24_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_31_ratio) feature_name.append("quad31_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_32_ratio) feature_name.append("quad32_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_33_ratio) feature_name.append("quad33_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_34_ratio) feature_name.append("quad34_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_41_ratio) feature_name.append("quad41_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_42_ratio) feature_name.append("quad42_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_43_ratio) feature_name.append("quad43_ratio_5min_" + str(num_5min_interval)) feature_list_5min.append(quad_44_ratio) feature_name.append("quad44_ratio_5min_" + str(num_5min_interval)) num_5min_interval = num_5min_interval + 1 print("rec_names with errornos records are : " + str(error_rec_names)) return feature_list_5min, feature_name
ann_graph=[]; RR_sec_unclean=[]; RR_sec=[]; delta_RR_sec=[]; x_val_sodp=[] y_val_sodp=[] rec_name=db_name+"/"+record; #rec_name= "afpdb/p02c" ## time for which you want to read record ## start_time=0; end_time=30; #time in mins ##### Extract RR intervals here ####### RR_sec_unclean=pr.get_RR_interval(rec_name,annotation,start_time,end_time) print RR_sec_unclean ####DELETE THE FIRST RR_sec_unclean value##### del RR_sec_unclean[0]; ##### APPLY FILTERS TO CLEAN DATA ####### #RR_sec=dc.detrend_data(dc.quotient_filt(dc.square_filt(RR_sec_unclean))) #RR_sec=dc.detrend_data((RR_sec_unclean) #RR_sec=dc.quotient_filt(dc.square_filt(RR_sec_unclean)) RR_sec=dc.square_filt(RR_sec_unclean)
def calc_std_5min_sodp_measures(rec_name,annotation, total_min, radius_array): #this functions calculates features from 5 min of RR intervals for record , and calculates std of each feature for all 5min intervlas in a 30min record. #it returns all_features_6_intervals : each list corresponds to one feature and values within the list are denote the feature value per interval #it returns: std_dev_all_features :each value is the std dev of the feature in 6 intervals #it also returns feature_name_overall : names of features whose std dev can be found in std_dev_all_Features feature_list_5min=[] num_5min_interval=1 #keeps track of which chunck of 5 min interval it is feature_name=[] start_time_arr=range(0,total_min,5) end_time_arr=range(5,total_min+5,5) # everytime the loop runs it extracts feauters for those 5 mins for start_time, end_time in zip(start_time_arr,end_time_arr): error_rec_names=[] count_quad1=0; count_quad2=0; count_quad3=0; count_quad4=0; count_quad11=0; count_quad12=0 count_quad13=0 count_quad14=0 count_quad21=0; count_quad22=0 count_quad23=0 count_quad24=0 count_quad31=0; count_quad32=0 count_quad33=0 count_quad34=0 count_quad41=0; count_quad42=0 count_quad43=0 count_quad44=0 points_at_origin=0; quad=0; print("calculating std of 5 min sodp features") print ("start time is: " +str(start_time)); print ("end time is: " +str(end_time)); ##### Extract RR intervals here ####### RR_sec_unclean=pr.get_RR_interval(rec_name,annotation,start_time,end_time) ####DELETE THE FIRST RR_sec_unclean value##### del RR_sec_unclean[0]; RR_sec=RR_sec_unclean #print("len of 5min RR_sec is: " + str(len(RR_sec))) num_RR_5min=len(RR_sec) #print("num_RR_5min_"+str(num_5min_interval)) x_val_sodp,y_val_sodp,ctm_array,ctm_feature_name,distance_array,dist_feature_name=calc_sodp_measures(rec_name,RR_sec, radius_array); ## calculate the number of poitns in each quadrant num_points_on_graph=len(x_val_sodp) ####plot sodp and save fig #### record_name="n43" output_folder="/home/ubuntu/Documents/Thesis_work/results/thesis_images/chapter_5/" fig_sodp,plot_sodp=graphs.plotScatter(record_name,x_val_sodp,y_val_sodp, "x[n+1]-x[n]", " x[n+2]-x[n+1] ", "SODP plot for 5min segment "+str(num_5min_interval) , 'b',xlim_lo=-1, xlim_hi=1, ylim_lo=-1, ylim_hi=1,axline=1) fig_sodp.savefig(output_folder+"sodp_plot_"+record_name+"_"+str(num_5min_interval)+".pdf",format='pdf') for x_val,y_val in zip(x_val_sodp,y_val_sodp): if x_val <-1 or x_val>1 or y_val<-1 or y_val>1: print("x_val is: " +str(x_val) + "y_val is: " + str(y_val)) print("errorneous value in: " + str(rec_name) +"_"+str(num_5min_interval)) error_rec_names.append(rec_name) elif x_val > 0 and y_val >0: quad=1; count_quad1=count_quad1+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 11: count_quad11=count_quad11+1; elif sub_quad is 12: count_quad12=count_quad12+1; elif sub_quad is 13: count_quad13=count_quad13+1; elif sub_quad is 14: count_quad14=count_quad14+1; elif x_val <0 and y_val >0: quad=2; count_quad2=count_quad2+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 21: count_quad21=count_quad21+1 elif sub_quad is 22: count_quad22=count_quad22+1 elif sub_quad is 23: count_quad23=count_quad23+1 elif sub_quad is 24: count_quad24=count_quad24+1 elif x_val <0 and y_val <0: quad=3; count_quad3=count_quad3+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 31: count_quad31=count_quad31+1 elif sub_quad is 32: count_quad32=count_quad32+1 elif sub_quad is 33: count_quad33=count_quad33+1 elif sub_quad is 34: count_quad34=count_quad34+1 elif x_val>0 and y_val<0: quad=4; count_quad4=count_quad4+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 41: count_quad41=count_quad41+1 elif sub_quad is 42: count_quad42=count_quad42+1 elif sub_quad is 43: count_quad43=count_quad43+1 elif sub_quad is 44: count_quad44=count_quad44+1 else: points_at_origin=points_at_origin+1 ratio_points_at_origin=float(points_at_origin)/num_points_on_graph #store the quad count values in features list feature_list_5min.append(ratio_points_at_origin) feature_name.append("ratio_points_at_origin_5min_"+str(num_5min_interval)) #calculate quad_ratio quad1_ratio=float(count_quad1)/num_points_on_graph; quad2_ratio=float(count_quad2)/num_points_on_graph; quad3_ratio=float(count_quad3)/num_points_on_graph; quad4_ratio=float(count_quad4)/num_points_on_graph; quad_11_ratio=float(count_quad11)/num_points_on_graph; quad_12_ratio=float(count_quad12)/num_points_on_graph; quad_13_ratio=float(count_quad13)/num_points_on_graph; quad_14_ratio=float(count_quad14)/num_points_on_graph; quad_21_ratio=float(count_quad21)/num_points_on_graph; quad_22_ratio=float(count_quad22)/num_points_on_graph; quad_23_ratio=float(count_quad23)/num_points_on_graph; quad_24_ratio=float(count_quad24)/num_points_on_graph; quad_31_ratio=float(count_quad31)/num_points_on_graph; quad_32_ratio=float(count_quad32)/num_points_on_graph; quad_33_ratio=float(count_quad33)/num_points_on_graph; quad_34_ratio=float(count_quad34)/num_points_on_graph; quad_41_ratio=float(count_quad41)/num_points_on_graph; quad_42_ratio=float(count_quad42)/num_points_on_graph; quad_43_ratio=float(count_quad43)/num_points_on_graph; quad_44_ratio=float(count_quad44)/num_points_on_graph; #store the quad ratio values in features list feature_list_5min.append(quad1_ratio) feature_name.append("std_quad1_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad2_ratio) feature_name.append("std_quad2_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad3_ratio) feature_name.append("std_quad3_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad4_ratio) feature_name.append("std_quad4_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_11_ratio) feature_name.append("std_quad11_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_12_ratio) feature_name.append("std_quad12_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_13_ratio) feature_name.append("std_quad13_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_14_ratio) feature_name.append("std_quad14_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_21_ratio) feature_name.append("std_quad21_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_22_ratio) feature_name.append("std_quad22_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_23_ratio) feature_name.append("std_quad23_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_24_ratio) feature_name.append("std_quad24_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_31_ratio) feature_name.append("std_quad31_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_32_ratio) feature_name.append("std_quad32_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_33_ratio) feature_name.append("std_quad33_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_34_ratio) feature_name.append("std_quad34_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_41_ratio) feature_name.append("std_quad41_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_42_ratio) feature_name.append("std_quad42_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_43_ratio) feature_name.append("std_quad43_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_44_ratio) feature_name.append("std_quad44_ratio_5min_"+str(num_5min_interval)) #print ("feature list for this 5 min interval is: " + str(feature_list_5min)) #print("indexes are: " + str(range(len(feature_list_5min)))) num_5min_interval=num_5min_interval+1; print ("rec_names with errornos records are : " + str(error_rec_names)) dist_bw_feature=21 start_ind=0 end_ind=105 feature_name_overall=[] #print (len(feature_list_5min)) all_features_6_intervals=[] for i in range(0,21): name=str(feature_name[i]) feature_name_overall.append(name[:-2]) #print("feature_name is: " + str(name[:-2])) feature_index_5min=range(start_ind,end_ind+21,dist_bw_feature) one_feature_all_intervals=[] for val in feature_index_5min: #print("val is : "+str(val)) feature_val=feature_list_5min[val] #print("feature_val at index : " + str(val)+" is: "+str(feature_val)) one_feature_all_intervals.append(feature_val) all_features_6_intervals.append(one_feature_all_intervals) start_ind=start_ind+1 end_ind=end_ind+1 feature_index_5min=range(start_ind,end_ind+21,dist_bw_feature) #print ("new indexes for feature index is: " + str(feature_index_5min)) std_dev_all_features=[] for list_feature_vals in all_features_6_intervals: std_dev_5min_feature=np.std(list_feature_vals) std_dev_all_features.append(std_dev_5min_feature) #for name,val in zip(feature_name_overall,std_dev_all_features): #print("name of feature is : " + str(name)) #print("val of feature is : " + str(val)) return all_features_6_intervals,std_dev_all_features,feature_name_overall
def calc_5min_sodp_measures(rec_name,annotation, total_min, radius_array): #this functions calculates features from 5 min of RR intervals for record and returns a list of all these features #for testing #start_time_arr=[0] #end_time_arr=[5] feature_list_5min=[] num_5min_interval=1 #keeps track of which chunck of 5 min interval it is feature_name=[] start_time_arr=range(0,total_min,5) end_time_arr=range(5,total_min+5,5) # everytime the loop runs it extracts feauters for those 5 mins for start_time, end_time in zip(start_time_arr,end_time_arr): error_rec_names=[] count_quad1=0; count_quad2=0; count_quad3=0; count_quad4=0; count_quad11=0; count_quad12=0 count_quad13=0 count_quad14=0 count_quad21=0; count_quad22=0 count_quad23=0 count_quad24=0 count_quad31=0; count_quad32=0 count_quad33=0 count_quad34=0 count_quad41=0; count_quad42=0 count_quad43=0 count_quad44=0 points_at_origin=0; quad=0; print("calculating 5 min sodp features") print ("start time is: " +str(start_time)); print ("end time is: " +str(end_time)); ##### Extract RR intervals here ####### RR_sec_unclean=pr.get_RR_interval(rec_name,annotation,start_time,end_time) ####DELETE THE FIRST RR_sec_unclean value##### del RR_sec_unclean[0]; RR_sec=RR_sec_unclean print("len of 5min RR_sec is: " + str(len(RR_sec))) num_RR_5min=len(RR_sec) feature_list_5min.append(num_RR_5min) feature_name.append("num_RR_5min_"+str(num_5min_interval)) print("num_RR_5min_"+str(num_5min_interval)) x_val_sodp,y_val_sodp,ctm_array,ctm_feature_name,distance_array,dist_feature_name=calc_sodp_measures(rec_name,RR_sec, radius_array); ## calculate the number of poitns in each quadrant num_points_on_graph=len(x_val_sodp) for x_val,y_val in zip(x_val_sodp,y_val_sodp): if x_val <-1 or x_val>1 or y_val<-1 or y_val>1: print("x_val is: " +str(x_val) + "y_val is: " + str(y_val)) print("errorneous value in: " + str(rec_name) +"_"+str(num_5min_interval)) error_rec_names.append(rec_name) elif x_val > 0 and y_val >0: quad=1; count_quad1=count_quad1+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 11: count_quad11=count_quad11+1; elif sub_quad is 12: count_quad12=count_quad12+1; elif sub_quad is 13: count_quad13=count_quad13+1; elif sub_quad is 14: count_quad14=count_quad14+1; elif x_val <0 and y_val >0: quad=2; count_quad2=count_quad2+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 21: count_quad21=count_quad21+1 elif sub_quad is 22: count_quad22=count_quad22+1 elif sub_quad is 23: count_quad23=count_quad23+1 elif sub_quad is 24: count_quad24=count_quad24+1 elif x_val <0 and y_val <0: quad=3; count_quad3=count_quad3+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 31: count_quad31=count_quad31+1 elif sub_quad is 32: count_quad32=count_quad32+1 elif sub_quad is 33: count_quad33=count_quad33+1 elif sub_quad is 34: count_quad34=count_quad34+1 elif x_val>0 and y_val<0: quad=4; count_quad4=count_quad4+1; sub_quad=count_points_in_16quad(x_val,y_val,quad) if sub_quad is 41: count_quad41=count_quad41+1 elif sub_quad is 42: count_quad42=count_quad42+1 elif sub_quad is 43: count_quad43=count_quad43+1 elif sub_quad is 44: count_quad44=count_quad44+1 else: points_at_origin=points_at_origin+1 ratio_points_at_origin=float(points_at_origin)/num_points_on_graph #store the quad count values in features list feature_list_5min.append(points_at_origin) feature_name.append("points_at_origin_5min_"+str(num_5min_interval)) feature_list_5min.append(ratio_points_at_origin) feature_name.append("ratio_points_at_origin_5min_"+str(num_5min_interval)) feature_list_5min.append(count_quad1) feature_name.append("num_points_in_quad1_5min_"+str(num_5min_interval)) feature_list_5min.append(count_quad2) feature_name.append("num_points_in_quad2_5min_"+str(num_5min_interval)) feature_list_5min.append(count_quad3) feature_name.append("num_points_in_quad3_5min_"+str(num_5min_interval)) feature_list_5min.append(count_quad4) feature_name.append("num_points_in_quad4_5min_"+str(num_5min_interval)) #calculate quad_ratio quad1_ratio=float(count_quad1)/num_points_on_graph; quad2_ratio=float(count_quad2)/num_points_on_graph; quad3_ratio=float(count_quad3)/num_points_on_graph; quad4_ratio=float(count_quad4)/num_points_on_graph; quad_11_ratio=float(count_quad11)/num_points_on_graph; quad_12_ratio=float(count_quad12)/num_points_on_graph; quad_13_ratio=float(count_quad13)/num_points_on_graph; quad_14_ratio=float(count_quad14)/num_points_on_graph; quad_21_ratio=float(count_quad21)/num_points_on_graph; quad_22_ratio=float(count_quad22)/num_points_on_graph; quad_23_ratio=float(count_quad23)/num_points_on_graph; quad_24_ratio=float(count_quad24)/num_points_on_graph; quad_31_ratio=float(count_quad31)/num_points_on_graph; quad_32_ratio=float(count_quad32)/num_points_on_graph; quad_33_ratio=float(count_quad33)/num_points_on_graph; quad_34_ratio=float(count_quad34)/num_points_on_graph; quad_41_ratio=float(count_quad41)/num_points_on_graph; quad_42_ratio=float(count_quad42)/num_points_on_graph; quad_43_ratio=float(count_quad43)/num_points_on_graph; quad_44_ratio=float(count_quad44)/num_points_on_graph; #store the quad ratio values in features list feature_list_5min.append(quad1_ratio) feature_name.append("quad1_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad2_ratio) feature_name.append("quad2_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad3_ratio) feature_name.append("quad3_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad4_ratio) feature_name.append("quad4_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_11_ratio) feature_name.append("quad11_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_12_ratio) feature_name.append("quad12_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_13_ratio) feature_name.append("quad13_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_14_ratio) feature_name.append("quad14_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_21_ratio) feature_name.append("quad21_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_22_ratio) feature_name.append("quad22_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_23_ratio) feature_name.append("quad23_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_24_ratio) feature_name.append("quad24_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_31_ratio) feature_name.append("quad31_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_32_ratio) feature_name.append("quad32_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_33_ratio) feature_name.append("quad33_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_34_ratio) feature_name.append("quad34_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_41_ratio) feature_name.append("quad41_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_42_ratio) feature_name.append("quad42_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_43_ratio) feature_name.append("quad43_ratio_5min_"+str(num_5min_interval)) feature_list_5min.append(quad_44_ratio) feature_name.append("quad44_ratio_5min_"+str(num_5min_interval)) num_5min_interval=num_5min_interval+1; print ("rec_names with errornos records are : " + str(error_rec_names)) return feature_list_5min,feature_name