def update_data(): global img, animation_grid, updateTime, fps, data1, ptr1, temp_index_y, temp_index_x data1 = np.roll(data1, -1) data = a.propagate(ecg=True) data = ani_convert(data, shape=a.shape, rp=a.rp, animation_grid=animation_grid) voltage = e.voltage(data, (temp_index_y, temp_index_x)) data1[-1] = voltage ptr1 += 1 curve.setData(data1) curve.setPos(ptr1, 0) time.sleep(1 / 120.) # gives larger more stable fps. img.setImage(data.T, levels=(0, 50)) QtCore.QTimer.singleShot(1, update_data) now = ptime.time() fps2 = 1.0 / (now - updateTime) updateTime = now fps = fps * 0.9 + fps2 * 0.1
constrainedx = [20, 179] ecg_num = 0 #Num of ECGs process_list = [] # process list final_rotor_position = None # Final rotor position tuple ECG_start_flag = False # Setting measurment flag to False (ecg measurments start when flag is triggered). ECG_located_flag = False # To keep the system propogating until the rotor is found or limit is reached. state = 0 # State for pipe work. while not ECG_located_flag: # Propogates model and converts into ani_data (used in ecg data processing) data = a.propagate(ecg=True) data = ani_convert(data, shape=a.shape, rp=a.rp, animation_grid=animation_grid) # If flag triggered, then start taking measurments. if ECG_start_flag: process_list.append(copy.deepcopy(data)) ptr1 += 1 if ptr1 >= stability_time: if not ECG_start_flag: ECG_start_flag = True if ptr1 % process_length == 0 and ptr1 != stability_time: sample = rt_ecg_gathering(process_list, sign_para='record_sign')
def update_data(): global updateTime, fps, ptr1, process_list, ECG_start_flag, state, vsign_short_memory, constrainedy, constrainedx global current_ecg_y_pos, current_ecg_x_pos, y_short_memory, x_short_memory, ecg_count, previousR, prev_y_vector global prev_x_vector, hsign_short_memory data = a.propagate(ecg=True) data = ani_convert(data, shape=a.shape, rp=a.rp, animation_grid=animation_grid) # Initial Crosshair drawing if ptr1 == 0: yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) # If flag triggered, then start taking measurments. if ECG_start_flag: process_list.append(copy.deepcopy(data)) ptr1 += 1 if ptr1 >= stability_time: if not ECG_start_flag: ECG_start_flag = True if ptr1 % process_length == 0 and ptr1 != stability_time: if state == 0: sample, bsigns = rt_ecg_gathering( process_list, sign_para="record_sign_plus" ) # ECG Recording and feature gathering ecg_count += 1 sample = sample.reshape( 1, -1) # Get deprication warning if this is not done. vsign = sample[0, :][-3] # hsign = sample[0, :][-2] # print hsign # constrainedx = autojump_constrainx(h_sign = hsign, constrained_ = constrainedx, x_pos = current_ecg_x_pos, constrainedy = constrainedy) # con_midpoint = int(np.mean(constrainedx)) # current_ecg_x_pos = con_midpoint # first potential x constraint # sample_ = sample[0, :][0:-3].reshape(1, -1) # Get sample without sign information. sample_ = sample[0, :][0:].reshape(1, -1) y_class_value = y_class.predict(sample_)[0] y_probarg = reg_predictor( y_classifier_full.predict_proba(sample_)[0, :]) if y_class_value == 1: state = 1 # Change to state 1 for y axis regression/classification. del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] # del constrainedy # constrainedy = [None, None] x_class_value = x_class.predict(sample_)[0] if x_class_value == 1: previousR = "******" del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] # del constrainedy # constrainedy = [None, None] current_ecg_y_pos = randint( 20, 179) # reseting the process. current_ecg_x_pos = randint(20, 179) state = 0 ecg_count = 0 constrainedy = [None, None] constrainedx = [20, 179] yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) if y_class_value == 0: y_short_memory.append(current_ecg_y_pos) vsign_short_memory.append(vsign) constrainedy, vsign_short_memory = constrained_finder( prev_y_vector, vsign_short_memory, current_ecg_y_pos, constrainedy) if condistance(constrainedy) == 1: state = 1 x_class_value = x_class.predict(sample_)[0] if x_class_value == 1: previousR = "******" # reseting the process. del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] # del constrainedy # constrainedy = [None, None] current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) state = 0 ecg_count = 0 constrainedy = [None, None] constrainedx = [20, 179] yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) else: likelyp = prediction(y_probarg, vector_constraint=vecdistance( current_ecg_y_pos, constrainedy), axis='x') y_vector = y_classifier_full.classes_[likelyp] prev_y_vector = y_vector # if np.abs(y_vector) > 3: # pad_place = np.where(np.array(constrainedy) == current_ecg_y_pos)[0][0] # if pad_place == 1: # cons current_ecg_y_pos -= y_vector # print y_vector if current_ecg_y_pos > 199 or current_ecg_y_pos < 0: current_ecg_y_pos %= 200 # if current_ecg_y_pos in y_short_memory: # print "Loop Y condition" # loop_place = np.where(np.array(constrainedy) == current_ecg_y_pos)[0][0] # if loop_place == 0: # current_ecg_y_pos += 1 # if current_ecg_y_pos > 199: # current_ecg_y_pos %= 200 # constrainedy[0] += 1 # if constrainedy[0] > 199: # constrainedy[0] %= 200 # if loop_place == 1: # current_ecg_y_pos -= 1 # if current_ecg_y_pos < 0: # current_ecg_y_pos %= 200 # constrainedy[1] -= 1 # if constrainedy[1] < 0: # constrainedy[1] %= 200 if state == 1: sample, bsigns = rt_ecg_gathering( process_list, sign_para='record_sign_plus') # ECG feature Recording ecg_count += 1 sample = sample.reshape( 1, -1) # Get deprication warning if this is not done. hsign = sample[0, :][-2] # Gets the h sign # sample_ = sample[0, :][0:-3].reshape(1, -1) # Takes a sample without sign sample_ = sample[0, :][:].reshape( 1, -1) # Takes a sample without sign information x_probarg = reg_predictor( x_classifier_full.predict_proba(sample_)[0, :]) # Prob map x_class_value = x_class.predict(sample_)[0] if x_class_value == 1: previousR = "******" current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) state = 0 ecg_count = 0 del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] del constrainedx constrainedy = [None, None] constrainedx = [20, 179] yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) if x_class_value == 0: x_short_memory.append(current_ecg_x_pos) hsign_short_memory.append(hsign) constrainedx, hsign_short_memory = constrained_finder( prev_x_vector, hsign_short_memory, current_ecg_x_pos, constrainedx) if condistance(constrainedx) == 1: previousR = "******" state = 1 del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] del constrainedx constrainedx = [20, 179] current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) state = 0 ecg_count = 0 constrainedy = [None, None] constrainedx = [20, 179] yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) else: likelyp = prediction(x_probarg, vector_constraint=vecdistance( current_ecg_x_pos, constrainedx), axis='y') x_vector = x_classifier_full.classes_[likelyp] prev_x_vector = x_vector current_ecg_x_pos -= x_vector if current_ecg_x_pos > 199 or current_ecg_x_pos < 0: current_ecg_x_pos %= 200 if current_ecg_x_pos in x_short_memory: print "X LOOP condition." loop_place = np.where( np.array(constrainedx) == current_ecg_x_pos)[0][0] if loop_place == 0: current_ecg_x_pos += 1 if current_ecg_x_pos > 199: current_ecg_x_pos %= 200 constrainedx[0] += 1 if constrainedx[0] > 199: constrainedx[0] %= 200 if loop_place == 1: current_ecg_x_pos -= 1 if current_ecg_x_pos < 0: current_ecg_x_pos %= 200 constrainedx[1] -= 1 if constrainedx[1] < 0: constrainedx[1] %= 200 ecg_processing.reset_singlegrid( (current_ecg_y_pos, current_ecg_x_pos)) if constrainedy[0] is not None: xLline.setPos(constrainedy[0]) if constrainedy[1] is not None: xUline.setPos(constrainedy[1]) yLline.setPos(constrainedx[0]) yUline.setPos(constrainedx[1]) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) del process_list process_list = [] update_label_text(cp_x_pos, cp_y_pos, current_ecg_x_pos, current_ecg_y_pos, ecg_count, previousR, constrainedy, constrainedx) time.sleep(1 / 120.) # gives more stable fps. img.setImage(data.T) # puts animation grid on image. # Stuff to do with time and fps. QtCore.QTimer.singleShot(1, update_data) now = ptime.time() fps2 = 1.0 / (now - updateTime) updateTime = now fps = fps * 0.9 + fps2 * 0.1
def update_data(): global updateTime, fps, ptr1, process_list, ECG_start_flag, state, vsign_short_memory, constrainedy, constrainedx global current_ecg_y_pos, current_ecg_x_pos, y_short_memory, x_short_memory, ecg_count, previousR, prev_y_vector global prev_x_vector, hsign_short_memory, num_ypos_class, num_xpos_class, num_Yloops, num_Xloops, num_yconstraint global num_xconstraint, num_xzjump, num_yzjump data = a.propagate(ecg=True) data = ani_convert(data, shape=a.shape, rp=a.rp, animation_grid=animation_grid) # Initial Crosshair drawing if ptr1 == 0: yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) # If flag triggered, then start taking measurments. if ECG_start_flag: process_list.append(copy.deepcopy(data)) ptr1 += 1 if ptr1 >= stability_time: if not ECG_start_flag: ECG_start_flag = True if ptr1 % process_length == 0 and ptr1 != stability_time: sample = rt_ecg_gathering( process_list, sign_para=args[5]) # ECG Recording and feature gathering ecg_count += 1 if state == 0: sample = sample.reshape( 1, -1) # Get deprication warning if this is not done. vsign = sample[0, :][-3] # first potential x constraint # sample_ = sample[0, :][0:-3].reshape(1, -1) # Get sample without sign information. y_class_value = y_class.predict(sample)[0] y_probarg = movingaverage( y_classifier_full.predict_proba(sample)[0, :], 10) if y_class_value == 1: state = 1 # Change to state 1 for y axis regression/classification. del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] x_class_value = x_class.predict(sample)[0] num_ypos_class += 1 if x_class_value == 1: previousR = "******" % (current_ecg_x_pos, current_ecg_y_pos) del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] del constrainedy constrainedy = [None, None] current_ecg_y_pos = randint( 20, 179) # reseting the process. current_ecg_x_pos = randint(20, 179) state = 0 ecg_count = 0 yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) num_xpos_class += 1 if y_class_value == 0: y_short_memory.append(current_ecg_y_pos) vsign_short_memory.append(vsign) constrainedy, vsign_short_memory = constrained_finder( prev_y_vector, vsign_short_memory, current_ecg_y_pos, constrainedy, axis='x') if condistance(constrainedy) == 1: state = 1 x_class_value = x_class.predict(sample)[0] num_yconstraint += 1 if x_class_value == 1: previousR = "******" % ( current_ecg_x_pos, current_ecg_y_pos) # reseting the process. del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] del constrainedy constrainedy = [None, None] current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) state = 0 ecg_count = 0 num_xpos_class += 1 yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) else: likelyp = prediction(y_probarg, vector_constraint=vecdistance( current_ecg_y_pos, constrainedy), axis='x') y_vector = y_classifier_full.classes_[likelyp] if y_vector == 0: state = 1 # Change to state 1 for y axis regression/classification. del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] x_class_value = x_class.predict(sample)[0] num_yzjump += 1 if x_class_value == 1: previousR = "******" % ( current_ecg_x_pos, current_ecg_y_pos) del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] del constrainedy constrainedy = [None, None] current_ecg_y_pos = randint( 20, 179) # reseting the process. current_ecg_x_pos = randint(20, 179) state = 0 ecg_count = 0 num_xpos_class += 1 yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos( 300 ) # If it predicts a jump of 0, then take the position it's on. prev_y_vector = y_vector current_ecg_y_pos -= y_vector if current_ecg_y_pos > 199 or current_ecg_y_pos < 0: current_ecg_y_pos %= 200 if state == 1: sample = sample.reshape( 1, -1) # Get deprication warning if this is not done. hsign = sample[0, :][-2] # Gets the h sign # sample_ = sample[0, :][0:-3].reshape(1, -1) # Takes a sample without sign information x_probarg = movingaverage( x_classifier_full.predict_proba(sample)[0, :], 10) # Prob map x_class_value = x_class.predict(sample)[0] if x_class_value == 1: previousR = "******" % (current_ecg_x_pos, current_ecg_y_pos) current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) state = 0 ecg_count = 0 del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] del constrainedx constrainedx = [20, 179] del constrainedy constrainedy = [None, None] num_xpos_class += 1 yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) if x_class_value == 0: x_short_memory.append(current_ecg_x_pos) hsign_short_memory.append(hsign) constrainedx, hsign_short_memory = constrained_finder( prev_x_vector, hsign_short_memory, current_ecg_x_pos, constrainedx, axis='y') if condistance( constrainedx ) == 1: # Row is constrained to be have distance 1, take position. previousR = "******" % ( current_ecg_x_pos, current_ecg_y_pos) state = 1 del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] del constrainedx constrainedx = [20, 179] del constrainedy constrainedy = [None, None] current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) state = 0 ecg_count = 0 num_xconstraint += 1 yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) else: likelyp = prediction(x_probarg, vector_constraint=vecdistance( current_ecg_x_pos, constrainedx), axis='y') x_vector = x_classifier_full.classes_[likelyp] if x_vector == 0: # If the predicted X jump is 0 previousR = "******" % ( current_ecg_x_pos, current_ecg_y_pos) current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) state = 0 ecg_count = 0 num_xzjump += 1 del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] del constrainedx constrainedx = [20, 179] del constrainedy constrainedy = [None, None] yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) prev_x_vector = x_vector current_ecg_x_pos -= x_vector if current_ecg_x_pos > 199 or current_ecg_x_pos < 0: current_ecg_x_pos %= 200 if current_ecg_x_pos in x_short_memory: previousR = '******' % current_ecg_y_pos state = 1 del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] del constrainedx constrainedx = [20, 179] del constrainedy constrainedy = [None, None] current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) state = 0 ecg_count = 0 num_Xloops += 1 yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) ecg_processing.reset_singlegrid( (current_ecg_y_pos, current_ecg_x_pos)) if constrainedy[0] is not None: xLline.setPos(constrainedy[0]) if constrainedy[1] is not None: xUline.setPos(constrainedy[1]) yLline.setPos(constrainedx[0]) yUline.setPos(constrainedx[1]) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) del process_list process_list = [] update_label_text(cp_x_pos, cp_y_pos, current_ecg_x_pos, current_ecg_y_pos, ecg_count, previousR, constrainedy, constrainedx, num_ypos_class, num_xpos_class, num_Yloops, num_Xloops, num_yconstraint, num_xconstraint, num_yzjump, num_xzjump) time.sleep(1 / 120.) # gives more stable fps. img.setImage(data.T) # puts animation grid on image. # Stuff to do with time and fps. QtCore.QTimer.singleShot(1, update_data) now = ptime.time() fps2 = 1.0 / (now - updateTime) updateTime = now fps = fps * 0.9 + fps2 * 0.1
def update_data(): global updateTime, fps, ptr1, process_list, ECG_start_flag, state, y_regress_treshold global current_ecg_y_pos, current_ecg_x_pos, y_short_memory, x_short_memory data = a.propagate(ecg=True) data = ani_convert(data, shape=a.shape, rp=a.rp, animation_grid=animation_grid) # Initial Crosshair drawing if ptr1 == 0: vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) # If flag triggered, then start taking measurments. if ECG_start_flag: process_list.append(copy.deepcopy(data)) ptr1 += 1 if ptr1 >= stability_time: if not ECG_start_flag: print "Starting Measurment Process" ECG_start_flag = True if ptr1 % process_length == 0 and ptr1 != stability_time: if state == 0: # ECG Recording and feature gathering sample = rt_ecg_gathering(process_list, "record sign") hsign = sample[-2] print hsign # Get deprication warning if this is not done. sample = sample.reshape(1, -1) y_class_value = y_estimator.predict(sample)[0] print "Y classification: %s" % y_class_value y_vector = int(y_regress.predict(sample)[0]) print "Y Vector prediction: %s" % y_vector if y_class_value == 1: # Change to state 1 for y axis regression/classification. state = 1 print "Found X-Axis" # Temporary del y_short_memory y_short_memory = [] x_class_value = x_class.predict(sample)[0] print "X classification: %s" % x_class_value if x_class_value == 1: print "Found the rotor!" print "Predicted Rotor position: (%s, %s)" % ( current_ecg_x_pos, current_ecg_y_pos) # reseting the process. current_ecg_x_pos = randint(30, 169) current_ecg_y_pos = randint(0, 199) state = 0 if y_class_value == 0: y_short_memory.append(current_ecg_y_pos) current_ecg_y_pos -= y_vector current_ecg_x_pos -= int(3 * hsign * np.absolute(hsign) * np.absolute(y_vector)) if current_ecg_y_pos > 199 or current_ecg_y_pos < 0: #Is this an error? current_ecg_y_pos %= 200 if current_ecg_x_pos > 199 or current_ecg_x_pos < 0: current_ecg_x_pos += int(3 * hsign * np.absolute(hsign) * np.absolute(y_vector)) if current_ecg_y_pos in y_short_memory: print "Entered Y Loop" # print "Loop: %s" % y_short_memory # loop_average = int((float(sum(y_short_memory))/len(y_short_memory))) # print "Loop Average: %s" % loop_average # del y_short_memory # y_short_memory = [] # current_ecg_y_pos = loop_average current_ecg_x_pos = randint(30, 169) current_ecg_y_pos = randint(0, 199) if state == 1: # ECG Recording and feature gathering sample = rt_ecg_gathering(process_list, "record sign") hsign = sample[-2] print hsign # Get deprication warning if this is not done. sample = sample.reshape(1, -1) x_class_value = x_class.predict(sample)[0] print "X classification: %s" % x_class_value x_vector = int(x_regress.predict(sample)[0]) print "X Vector prediction: %s" % x_vector if x_class_value == 1: print "Found the rotor!" print "Predicted Rotor position: (%s, %s)" % ( current_ecg_x_pos, current_ecg_y_pos) # Temporary del x_short_memory x_short_memory = [] # reseting the process. current_ecg_x_pos = randint(30, 169) current_ecg_y_pos = randint(0, 199) state = 0 if x_class_value == 0: x_short_memory.append(current_ecg_x_pos) current_ecg_x_pos -= x_vector + 13 if current_ecg_x_pos > 199 or current_ecg_x_pos < 0: current_ecg_x_pos %= 200 if current_ecg_x_pos in x_short_memory: print "Entered X Loop" # print "Loop: %s" % x_short_memory # loop_average = int((float(sum(x_short_memory))/len(x_short_memory))) # print "Loop Average: %s" % loop_average # del x_short_memory # x_short_memory = [] # current_ecg_x_pos = loop_average current_ecg_x_pos = randint(30, 169) current_ecg_y_pos = randint(0, 199) print "New ECG Probe position: (%s, %s)" % (current_ecg_x_pos, current_ecg_y_pos) print '\n' ecg_processing.reset_singlegrid( (current_ecg_y_pos, current_ecg_x_pos)) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) del process_list process_list = [] updating_text = """ECG Position: (%s, %s)<br>\n Rotor Position: (%s, %s)""" % ( current_ecg_x_pos, current_ecg_y_pos, cp_x_pos, cp_y_pos) label.setText(updating_text) # gives more stable fps. time.sleep(1 / 120.) # puts animation grid on image. img.setImage(data.T) # Stuff to do with time and fps. QtCore.QTimer.singleShot(1, update_data) now = ptime.time() fps2 = 1.0 / (now - updateTime) updateTime = now fps = fps * 0.9 + fps2 * 0.1
def update_data(): global updateTime, fps, ptr1, process_list, ECG_start_flag, state, vsign_short_memory, constrainedy, constrainedx global current_ecg_y_pos, current_ecg_x_pos, y_short_memory, x_short_memory, ecg_count, previousR, prev_y_vector global prev_x_vector, hsign_short_memory, num_ypos_class, num_xpos_class, num_Yloops, num_Xloops, num_yconstraint global num_xconstraint, num_xzjump, num_yzjump, rotors_found, perminant_constraints, N_rotors, total_sign_info, vconsistent, hconsistent, bconsistent global x_history, y_history, special_state, jump_x, special_vsign, special_y data = a.propagate(ecg=True) data = ani_convert(data, shape=a.shape, rp=a.rp, animation_grid=animation_grid) # Initial Crosshair drawing if ptr1 == 0: yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) xUline.setPos(300) xLline.setPos(300) pUline.setPos(-300) pLline.setPos(-300) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) # If flag triggered, then start taking measurments. if ECG_start_flag: process_list.append(copy.deepcopy(data)) ptr1 += 1 # CONDITION TO START THE LOCATING PROCESS if ptr1 >= stability_time: if not ECG_start_flag: ECG_start_flag = True # CONDITION TO TAKE A MEASURMENT if ptr1 % process_length == 0 and ptr1 != stability_time: sample, signs = rt_ecg_gathering( process_list, sign_para='record_sign_plus' ) # ECG Recording and feature gathering ecg_count += 1 total_sign_info.append(signs) x_history.append(current_ecg_x_pos) y_history.append(current_ecg_y_pos) # LOOKING FOR THE ROTORS X AXIS if state == 0: sample = sample.reshape( 1, -1) # Get deprication warning if this is not done. vsign = sample[0, :][-3] # first potential x constraint # sample_ = sample[0, :][0:-3].reshape(1, -1) # Get sample without sign information. y_class_value = y_class.predict(sample)[0] y_probarg = reg_predictor( y_classifier_full.predict_proba(sample)[0, :]) # POSITIVE CLASSIFICATION FOR Y if y_class_value == 1: if special_state: special_state = False state = 1 # Change to state 1 for y axis regression/classification. del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] x_class_value = x_class.predict(sample)[0] num_ypos_class += 1 # CHECKS IF ITS ON THE CORRECT Y AXIS if x_class_value == 1: previousR = "******" % (current_ecg_x_pos, current_ecg_y_pos) state = 0 ecg_count = 0 pred_rotor_y.append(current_ecg_y_pos) pred_rotor_x.append(current_ecg_x_pos) # ALL ROTORS ARE FOUND if rotors_found == N_rotors: current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) xUline.setPos(300) xLline.setPos(300) pUline.setPos(-300) pLline.setPos(-300) rotors_found = 0 perminant_constraints = [] # ONE OF THE ROTORS IS FOUND else: rotors_found += 1 upper = current_ecg_y_pos - 5 upper %= 200 lower = current_ecg_y_pos + 5 lower %= 200 perminant_constraints.append([lower, upper]) xUline.setPos(300) xLline.setPos(300) pUline.setPos(upper) pLline.setPos(lower) xvec, yvec = relative_vectors( x_history, y_history, current_ecg_x_pos, current_ecg_y_pos) xvec = np.array(xvec) yvec = np.array(yvec) for i in range(len(x_history)): vconsistent.append( check_signs(xvec[i], yvec[i], total_sign_info[i][0], vsign_check, thr=0.05)) hconsistent.append( check_signs(xvec[i], yvec[i], total_sign_info[i][1], hsign_check, thr=0.02)) bconsistent.append( check_bsign(total_sign_info[i][-2], total_sign_info[i][-1])) vconsistent = np.array(vconsistent)[ np.absolute(yvec) > 4] hconsistent = np.array(hconsistent)[ np.absolute(yvec) > 4] bconsistent = np.array(bconsistent)[ np.absolute(yvec) > 4] total_sign_info = np.array(total_sign_info) tsign = total_sign_info[np.absolute(yvec) > 4] yvec_use = yvec[np.absolute(yvec) > 4] xvec_use = xvec[np.absolute(yvec) > 4] special_y = (yvec_use[vconsistent == False] + current_ecg_y_pos) % 200 special_vsign = tsign[:, 0][vconsistent == False] current_ecg_y_pos = (current_ecg_y_pos + 100) % 200 special_state = True constrainedy = [None, None] constrainedx = [20, 179] yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) num_xpos_class += 1 del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] total_sign_info = [] vconsistent = [] hconsistent = [] bconsistent = [] x_history = [] y_history = [] # NEGATIVE CLASSIFIACTION FOR Y if y_class_value == 0: y_short_memory.append(current_ecg_y_pos) vsign_short_memory.append(vsign) if not special_state: constrainedy, vsign_short_memory = constrained_finder( prev_y_vector, vsign_short_memory, current_ecg_y_pos, constrainedy, 'x', perminant_constraints) if special_state: constrainedx, constrainedy, jump_x = special_constraint_finder( current_ecg_x_pos, current_ecg_y_pos, total_sign_info, constrainedy, constrainedx, perminant_constraints, special_y, special_vsign) # might need this to fix weird constraints vsign_short_memory = [] # CONSTRAINED CONDITION FOR Y if condistance(constrainedy) == 0: state = 0 ecg_count = 0 num_yconstraint += 1 xUline.setPos(300) xLline.setPos(300) constrainedy = [None, None] constrainedx = [20, 179] if rotors_found > 0: current_ecg_y_pos = (pred_rotor_y[-1] + 100) % 200 else: current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] total_sign_info = [] vconsistent = [] hconsistent = [] bconsistent = [] x_history = [] y_history = [] # MOVING THE PROBE IN THE Y AXIS else: likelyp = prediction(y_probarg, vector_constraint=vecdistance( current_ecg_y_pos, constrainedy), axis='x') y_vector = y_classifier_full.classes_[likelyp] # SPECIAL CONDITION if np.abs(y_vector) > 45 and special_state: print y_vector y_vector = int(45 * copysign(1, y_vector)) special_state = False # JUMPS IN THE X DIRECTION AS WELL UNDER SPECIAL CONDITION if jump_x: h = total_sign_info[-1][1] if np.abs(h) > 1.0: print 'constraint jump' if h > 0: current_ecg_x_pos = int( (current_ecg_x_pos + 20) / 2.) else: current_ecg_x_pos = int( (current_ecg_x_pos + 179) / 2.) else: if 179 - current_ecg_x_pos >= 80: current_ecg_x_pos += 80 else: current_ecg_x_pos -= 80 jump_x = False # IF THE PREDICTED Y JUMP IS ZERO if y_vector == 0: previousR = "******" % ( current_ecg_x_pos, current_ecg_y_pos) state = 0 ecg_count = 0 num_yzjump += 1 xUline.setPos(300) xLline.setPos(300) constrainedy = [None, None] constrainedx = [20, 179] if rotors_found > 0: current_ecg_y_pos = (pred_rotor_y[-1] + 100) % 200 else: current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] prev_y_vector = y_vector current_ecg_y_pos -= y_vector # WRAPPING AROUND THE BOUNDRY CONDITION if current_ecg_y_pos > 199 or current_ecg_y_pos < 0: current_ecg_y_pos %= 200 # Y LOOP FORM CHECK if current_ecg_y_pos in y_short_memory: previousR = '******' state = 0 ecg_count = 0 num_Yloops += 1 xUline.setPos(300) xLline.setPos(300) constrainedy = [None, None] constrainedx = [20, 179] if rotors_found > 0: current_ecg_y_pos = (current_ecg_y_pos + 100) % 200 else: current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) del y_short_memory y_short_memory = [] del vsign_short_memory vsign_short_memory = [] total_sign_info = [] vconsistent = [] hconsistent = [] bconsistent = [] x_history = [] y_history = [] # LOOKING FOR THE ROTORS Y AXIS if state == 1: sample = sample.reshape( 1, -1) # Get deprication warning if this is not done. hsign = sample[0, :][-2] # Gets the h sign # sample_ = sample[0, :][0:-3].reshape(1, -1) # Takes a sample without sign information x_probarg = reg_predictor( x_classifier_full.predict_proba(sample)[0, :]) # Prob map x_class_value = x_class.predict(sample)[0] # POSITIVE CLASSIFICATION FOR X if x_class_value == 1: previousR = "******" % (current_ecg_x_pos, current_ecg_y_pos) state = 0 ecg_count = 0 num_xpos_class += 1 pred_rotor_y.append(current_ecg_y_pos) pred_rotor_x.append(current_ecg_x_pos) if rotors_found == N_rotors: xUline.setPos(300) xLline.setPos(300) pUline.setPos(-300) pLline.setPos(-300) current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) rotors_found = 0 perminant_constraints = [] else: rotors_found += 1 upper = current_ecg_y_pos - 5 upper %= 200 lower = current_ecg_y_pos + 5 lower %= 200 perminant_constraints.append([lower, upper]) xUline.setPos(300) xLline.setPos(300) pUline.setPos(upper) pLline.setPos(lower) xvec, yvec = relative_vectors(x_history, y_history, current_ecg_x_pos, current_ecg_y_pos) xvec = np.array(xvec) yvec = np.array(yvec) for i in range(len(x_history)): vconsistent.append( check_signs(xvec[i], yvec[i], total_sign_info[i][0], vsign_check, thr=0.05)) hconsistent.append( check_signs(xvec[i], yvec[i], total_sign_info[i][1], hsign_check, thr=0.02)) bconsistent.append( check_bsign(total_sign_info[i][-2], total_sign_info[i][-1])) vconsistent = np.array(vconsistent)[ np.absolute(yvec) > 4] hconsistent = np.array(hconsistent)[ np.absolute(yvec) > 4] bconsistent = np.array(bconsistent)[ np.absolute(yvec) > 4] total_sign_info = np.array(total_sign_info) tsign = total_sign_info[np.absolute(yvec) > 4] yvec_use = yvec[np.absolute(yvec) > 4] xvec_use = xvec[np.absolute(yvec) > 4] special_y = (yvec_use[vconsistent == False] + current_ecg_y_pos) % 200 special_vsign = tsign[:, 0][vconsistent == False] current_ecg_y_pos = (current_ecg_y_pos + 100) % 200 special_state = True constrainedy = [None, None] constrainedx = [20, 179] yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] total_sign_info = [] vconsistent = [] hconsistent = [] bconsistent = [] x_history = [] y_history = [] # NEGATIVE CLASSIFICATION FOR X if x_class_value == 0: x_short_memory.append(current_ecg_x_pos) hsign_short_memory.append(hsign) constrainedx, hsign_short_memory = constrained_finder( prev_x_vector, hsign_short_memory, current_ecg_x_pos, constrainedx, 'y', perminant_constraints) # CONSTRAINED CONDITION FOR X if condistance( constrainedx ) == 0: # Row is constrained to be have distance 1, take position. previousR = "******" % ( current_ecg_x_pos, current_ecg_y_pos) state = 0 ecg_count = 0 num_xconstraint += 1 xUline.setPos(300) xLline.setPos(300) constrainedy = [None, None] constrainedx = [20, 179] if rotors_found > 0: current_ecg_y_pos = (pred_rotor_y[-1] + 100) % 200 else: current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] total_sign_info = [] x_history = [] y_history = [] vconsistent = [] hconsistent = [] bconsistent = [] # MOVING THE PROBE IN THE X AXIS else: likelyp = prediction(x_probarg, vector_constraint=vecdistance( current_ecg_x_pos, constrainedx), axis='y') x_vector = x_classifier_full.classes_[likelyp] # IF THE PREDICTED JUMP IS ZERO if x_vector == 0: # If the predicted X jump is 0 previousR = "******" % ( current_ecg_x_pos, current_ecg_y_pos) state = 0 ecg_count = 0 num_xzjump += 1 xUline.setPos(300) xLline.setPos(300) constrainedy = [None, None] constrainedx = [20, 179] if rotors_found > 0: current_ecg_y_pos = (pred_rotor_y[-1] + 100) % 200 else: current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] total_sign_info = [] x_history = [] y_history = [] vconsistent = [] hconsistent = [] bconsistent = [] prev_x_vector = x_vector current_ecg_x_pos -= x_vector # WRAPPING AROUND THE BOUNDRY if current_ecg_x_pos > 199 or current_ecg_x_pos < 0: current_ecg_x_pos %= 200 # X LOOP FORM CHECK if current_ecg_x_pos in x_short_memory: previousR = '******' % current_ecg_y_pos state = 0 ecg_count = 0 num_Xloops += 1 xUline.setPos(300) xLline.setPos(300) constrainedy = [None, None] constrainedx = [20, 179] if rotors_found > 0: current_ecg_y_pos = (pred_rotor_y[-1] + 100) % 200 else: current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) yUline.setPos(constrainedx[1]) yLline.setPos(constrainedx[0]) del x_short_memory x_short_memory = [] del hsign_short_memory hsign_short_memory = [] total_sign_info = [] x_history = [] y_history = [] vconsistent = [] hconsistent = [] bconsistent = [] # UPDATING LINES AND PREPARING FOR NEW MEASURMENT ecg_processing.reset_singlegrid( (current_ecg_y_pos, current_ecg_x_pos)) if constrainedy[0] is not None: xLline.setPos(constrainedy[0]) if constrainedy[1] is not None: xUline.setPos(constrainedy[1]) yLline.setPos(constrainedx[0]) yUline.setPos(constrainedx[1]) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) del process_list process_list = [] update_label_text(cp_x_pos, cp_y_pos, cp_x_pos2, cp_y_pos2, current_ecg_x_pos, current_ecg_y_pos, ecg_count, previousR, constrainedy, constrainedx, perminant_constraints, num_ypos_class, num_xpos_class, num_Yloops, num_Xloops, num_yconstraint, num_xconstraint, num_yzjump, num_xzjump) # time.sleep(1/120.) # gives more stable fps. img.setImage(data.T) # puts animation grid on image. # Stuff to do with time and fps. QtCore.QTimer.singleShot(1, update_data) now = ptime.time() fps2 = 1.0 / (now - updateTime) updateTime = now fps = fps * 0.9 + fps2 * 0.1
def update_data(): global updateTime, fps, ptr1, process_list, ECG_start_flag, state, y_regress_treshold global current_ecg_y_pos, current_ecg_x_pos, y_short_memory, x_short_memory, ecg_count, previousR # resets the tissue if rotor is found or loop is reached. (Needs work # (have to reset the time before a measurment is taken.)) # if tissue_reset: # a = ps.Heart(nu=0.2, delta=0.0, fakedata=True) # # Randomises the rotor x,y position # cp_x_pos = randint(0, 199) # cp_y_pos = randint(0, 199) # a.set_pulse(60, [[cp_y_pos], [cp_x_pos]]) # update_label_text(cp_x_pos, cp_y_pos, current_ecg_x_pos, current_ecg_y_pos, ecg_count) # tissue_reset = False data = a.propagate(ecg=True) data = ani_convert(data, shape=a.shape, rp=a.rp, animation_grid=animation_grid) # Initial Crosshair drawing if ptr1 == 0: vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) # If flag triggered, then start taking measurments. if ECG_start_flag: process_list.append(copy.deepcopy(data)) ptr1 += 1 if ptr1 >= stability_time: if not ECG_start_flag: ECG_start_flag = True if ptr1 % process_length == 0 and ptr1 != stability_time: if state == 0: # ECG Recording and feature gathering sample = rt_ecg_gathering(process_list) ecg_count += 1 # Get deprication warning if this is not done. sample = sample.reshape(1, -1) y_class_value = y_estimator.predict(sample)[0] y_vector = int(y_regress.predict(sample)[0]) if y_class_value == 1: # Change to state 1 for y axis regression/classification. state = 1 del y_short_memory y_short_memory = [] x_class_value = x_class.predict(sample)[0] if x_class_value == 1: previousR = "******" # reseting the process. del y_short_memory y_short_memory = [] current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) state = 0 ecg_count = 0 if y_class_value == 0: y_short_memory.append(current_ecg_y_pos) current_ecg_y_pos -= y_vector if current_ecg_y_pos > 199 or current_ecg_y_pos < 0: current_ecg_y_pos %= 200 if current_ecg_y_pos in y_short_memory: previousR = "******" ecg_count = 0 current_ecg_x_pos = randint(30, 169) current_ecg_y_pos = randint(0, 199) if state == 1: # ECG Recording and feature gathering sample = rt_ecg_gathering(process_list) print sample[441:445] ecg_count += 1 # Get deprication warning if this is not done. sample = sample.reshape(1, -1) x_class_value = x_class.predict(sample)[0] x_vector = int(x_regress.predict(sample)[0]) if x_class_value == 1: previousR = "******" del x_short_memory x_short_memory = [] # reseting the process. current_ecg_y_pos = randint(0, 199) current_ecg_x_pos = randint(20, 179) state = 0 ecg_count = 0 if x_class_value == 0: x_short_memory.append(current_ecg_x_pos) current_ecg_x_pos -= x_vector if current_ecg_x_pos > 199 or current_ecg_x_pos < 0: current_ecg_x_pos %= 200 if current_ecg_x_pos in x_short_memory: previousR = '******' ecg_count = 0 del x_short_memory x_short_memory = [] current_ecg_x_pos = randint(20, 179) current_ecg_y_pos = randint(0, 199) state = 0 ecg_processing.reset_singlegrid( (current_ecg_y_pos, current_ecg_x_pos)) vLine.setPos(current_ecg_x_pos + 0.5) hLine.setPos(current_ecg_y_pos + 0.5) del process_list process_list = [] update_label_text(cp_x_pos, cp_y_pos, current_ecg_x_pos, current_ecg_y_pos, ecg_count, previousR) # time.sleep(1/120.) # gives more stable fps. img.setImage(data.T) # puts animation grid on image. # Stuff to do with time and fps. QtCore.QTimer.singleShot(1, update_data) now = ptime.time() fps2 = 1.0 / (now - updateTime) updateTime = now fps = fps * 0.9 + fps2 * 0.1