def checkCoords(self, coords, kilos=MAX_ALERT_DISTANCE): if self.coords is not None: return utils.distance(coords, self.coords) < kilos elif self.polygon is not None: x, y = coords return utils.point_inside_polygon(x, y, self.polygon) else: return False
def checkCoords(self, coords, kilos=MAX_ALERT_DISTANCE): if self.coords is not None: return utils.distance(coords, self.coords) < kilos elif self.polygon is not None: x, y = coords return utils.point_inside_polygon(x, y, self.polygon) else: return False
def create_phantom(args): # assuming constant distance between cross-sectional shapes. num_splines = len(args.spline_files) parameter_values = np.linspace(args.x_min, args.x_max, num_splines) artery_model = InterpolatedTube(args.spline_files, parameter_values, scale=args.scale, interp_kind='cubic') # the cross-sectional curves are assumed to be in the yz-plane, # while the long axis is the x-axis. y_min, y_max, z_min, z_max = artery_model.get_limits() print 'Scatterer extent: x=%f..%f, y=%f...%f, z=%f..%f'\ % (args.x_min, args.x_max, y_min, y_max, z_min, z_max) y_length = y_max-y_min z_length = z_max-z_min y_extra = args.space_factor*y_length z_extra = args.space_factor*z_length # create random scatterers xs = np.random.uniform(low=args.x_min, high=args.x_max, size=(args.num_scatterers,)) ys = np.random.uniform(low=y_min-y_extra, high=y_max+y_extra, size=(args.num_scatterers,)) zs = np.random.uniform(low=z_min-z_extra, high=z_max+y_extra, size=(args.num_scatterers,)) _as = np.random.uniform(low=-1.0, high=1.0, size=(args.num_scatterers,)) for scatterer_no in range(args.num_scatterers): x = xs[scatterer_no] y = ys[scatterer_no] z = zs[scatterer_no] # evaluate interpolated curve for this x curve_pts = artery_model.evaluate_curve(x) if scatterer_no % 20000 == 0: print 'Processed scatterer %d of %d' % (scatterer_no, args.num_scatterers) # convert 2d array to list of (x,y) polygon = [(curve_pts[i,0],curve_pts[i,1]) for i in range(curve_pts.shape[0])] is_inside = point_inside_polygon(y, z, polygon) k = args.outside_factor if is_inside: k = args.inside_factor _as[scatterer_no] *= k data = np.empty((args.num_scatterers, 4), dtype='float32') data[:, 0] = xs data[:, 1] = ys data[:, 2] = zs data[:, 3] = _as with h5py.File(args.h5_out, 'w') as f: f["data"] = data print 'Data written to %s' % args.h5_out
def on_click(self, x, y, button, pressed): print('{0} at {1}'.format( 'Pressed' if pressed else 'Released', (x, y))) # If this is a left or right click left_click = (button == mouse.Button.left) this_click = BasicMouseEvent(x=x, y=y, time_stamp=self.tobii_controller.LastTimestamp, aoi=None, left_click=left_click, is_press=(pressed == 1)) for aoi in self.AOIS: if (utils.point_inside_polygon(x, y, self.AOIS[aoi])): this_click.aoi = aoi self.mouse_queue.put(this_click) break if not pressed: if self.last_release is not None: clicks_time_difference = this_click.time_stamp - self.last_release.time_stamp if clicks_time_difference < params.MAX_DOUBLE_CLICK_DUR: self.double_click_queue.put(DoubleClickMouseEvent(x=self.last_release.x, y=self.last_release.y, time_stamp=self.last_release.time_stamp, aoi=self.last_release.aoi, is_first_click=False)) self.double_click_queue.put(DoubleClickMouseEvent(x=this_click.x, y=this_click.y, time_stamp=this_click.time_stamp, aoi=this_click.aoi, is_first_click=True)) self.last_release = this_click
def run(self): """ Concurrently detects fixations, defined as consecutive samples with an inter-sample distance of less than a set amount of pixels (disregarding missing data). Uses params.MAXDIST and params.MINDUR for respectively the distance and the smallest possible time length of a fixation. The method is a coroutine, which means that it can pause its execution and give control to other components of the platform. """ print(self.AOIS, "Started fixation algorithm") #list of lists, each containing [starttime, endtime, duration, endx, endy] self.EndFixations = [] #Keep track of index in x,y,time array array_index = 0 #Used to get segments of size 7 array_iterator = 7 newX = [] newY = [] newTime = [] newValid = [] while (self.runOnlineFix): yield self.wait_for_new_data(array_index, array_iterator) if (not self.runOnlineFix): break #Get segments of size 7 curX, curY, curTime, curValid = self.get_data_batch( array_index, array_iterator) newX = curX newY = curY newTime = curTime newValid = curValid Sfix, Efix = self.fixation_detection(curX, curY, curTime, curValid, params.FIX_MAXDIST, params.FIX_MINDUR) #When there is no end fixation detected yet while (1): #If start of fixation has not been detected yet if (Sfix == []): array_index += array_iterator #Wait till array has filled with enough data yield self.wait_for_new_data(array_index, array_iterator) if (not self.runOnlineFix): break #Get next 7 element chunk of data nextX, nextY, nextTime, nextValid = self.get_data_batch( array_index, array_iterator) #Append next segment with current arrays of interest #If no more curX we can just newX.extend(nextX) newX = curX + nextX newY = curY + nextY newTime = curTime + nextTime newValid = curValid + nextValid #Run fixation algorithm again with extended array Sfix, Efix = self.fixation_detection( newX, newY, newTime, newValid, params.FIX_MAXDIST, params.FIX_MINDUR) #If no start detected, then we can use this to drop the first |array_iterator| items curX = nextX curY = nextY curTime = nextTime curValid = nextValid else: #Get that start fixation x and y values to display on front end SfixTime = Sfix[0] fixIndex = newTime.index(SfixTime) xVal = newX[fixIndex] yVal = newY[fixIndex] break #We are here because start fixation was detected while (1): if (Efix == []): array_index = array_index + array_iterator #Wait till array has enough data yield self.wait_for_new_data(array_index, array_iterator) if (not self.runOnlineFix): break #Get next segment of data to append to current array of interest nextX, nextY, nextTime, nextValid = self.get_data_batch( array_index, array_iterator) newX.extend(nextX) newY.extend(nextY) newTime.extend(nextTime) newValid.extend(nextValid) Sfix, Efix = self.fixation_detection( newX, newY, newTime, newValid, params.FIX_MAXDIST, params.FIX_MINDUR) #a genuine end fixation has been found! else: #Add the newly found end fixation to our collection of end fixations #Get time stamp for newly found end fixation EfixEndTime = Efix[0][1] #Update index to data points after newly found end fixation start_fix = self.tobii_controller.time.index(Sfix[0]) array_index = self.tobii_controller.time.index( EfixEndTime) + 1 points_in_fixation = array_index - 1 - start_fix x_fixation = 0 y_fixation = 0 arr_size = points_in_fixation for i in range(arr_size): if (self.tobii_controller.x[start_fix + i] > 0): x_fixation += self.tobii_controller.x[start_fix + i] y_fixation += self.tobii_controller.y[start_fix + i] else: points_in_fixation -= 1 # If for some reason detected fixation is outside of the screen if (points_in_fixation == 0): Efix = [] Sfix = [] break x_fixation /= points_in_fixation y_fixation /= points_in_fixation self.tobii_controller.add_fixation(Efix[0][3], Efix[0][4], Efix[0][2], Sfix[0]) ##### for aoi in self.AOIS: if (utils.point_inside_polygon(x_fixation, y_fixation, self.AOIS[aoi])): self.cur_fix_id += 1 self.notify_app_state_controller( aoi, int(Sfix[0]), int(EfixEndTime), int(EfixEndTime - Sfix[0])) print aoi break
def iter_int_points(self): # dumb but easy approach. fast enough when compared to neural network anyway :) for x in xrange(*self.xspan): for y in xrange(*self.yspan): if point_inside_polygon(x, y, self.points): yield (x,y)
def run(self): """ Concurrently detects fixations, defined as consecutive samples with an inter-sample distance of less than a set amount of pixels (disregarding missing data). Uses params.MAXDIST and params.MINDUR for respectively the distance and the smallest possible time length of a fixation. The method is a coroutine, which means that it can pause its execution and give control to other components of the platform. """ print(self.AOIS) #list of lists, each containing [starttime, endtime, duration, endx, endy] self.EndFixations = [] #Keep track of index in x,y,time array array_index = 0 #Used to get segments of size 7 array_iterator = 7 newX = [] newY = [] newTime = [] newValid = [] while(self.runOnlineFix): yield self.wait_for_new_data(array_index, array_iterator) if (not self.runOnlineFix): break #Get segments of size 7 curX, curY, curTime, curValid = self.get_data_batch(array_index, array_iterator) newX = curX newY = curY newTime = curTime newValid = curValid Sfix, Efix = self.fixation_detection(curX, curY, curTime, curValid, params.FIX_MAXDIST, params.FIX_MINDUR) #When there is no end fixation detected yet while(1): #If start of fixation has not been detected yet if(Sfix == []): array_index += array_iterator #Wait till array has filled with enough data yield self.wait_for_new_data(array_index, array_iterator) if (not self.runOnlineFix): break #Get next 7 element chunk of data nextX, nextY, nextTime, nextValid = self.get_data_batch(array_index, array_iterator) #Append next segment with current arrays of interest #If no more curX we can just newX.extend(nextX) newX = curX + nextX newY = curY + nextY newTime = curTime + nextTime newValid = curValid + nextValid #Run fixation algorithm again with extended array Sfix, Efix = self.fixation_detection(newX, newY, newTime, newValid, params.FIX_MAXDIST, params.FIX_MINDUR) #If no start detected, then we can use this to drop the first |array_iterator| items curX = nextX curY = nextY curTime = nextTime curValid = nextValid else: #Get that start fixation x and y values to display on front end SfixTime = Sfix[0] fixIndex = newTime.index(SfixTime) xVal = newX[fixIndex] yVal = newY[fixIndex] break #We are here because start fixation was detected while(1): if(Efix == []): array_index = array_index + array_iterator #Wait till array has enough data yield self.wait_for_new_data(array_index, array_iterator) if (not self.runOnlineFix): break #Get next segment of data to append to current array of interest nextX, nextY, nextTime, nextValid = self.get_data_batch(array_index, array_iterator) newX.extend(nextX) newY.extend(nextY) newTime.extend(nextTime) newValid.extend(nextValid) Sfix, Efix = self.fixation_detection(newX, newY, newTime, newValid, params.FIX_MAXDIST, params.FIX_MINDUR) #a genuine end fixation has been found! else: #Add the newly found end fixation to our collection of end fixations #Get time stamp for newly found end fixation EfixEndTime = Efix[0][1] #Update index to data points after newly found end fixation start_fix = self.tobii_controller.time.index(Sfix[0]) array_index = self.tobii_controller.time.index(EfixEndTime) + 1 points_in_fixation = array_index - 1 - start_fix x_fixation = 0 y_fixation = 0 arr_size = points_in_fixation for i in range(arr_size): if (self.tobii_controller.x[start_fix + i] > 0): x_fixation += self.tobii_controller.x[start_fix + i] y_fixation += self.tobii_controller.y[start_fix + i] else: points_in_fixation -= 1 # If for some reason detected fixation is outside of the screen if (points_in_fixation == 0): Efix = [] Sfix = [] break x_fixation /= points_in_fixation y_fixation /= points_in_fixation self.tobii_controller.add_fixation(Efix[0][3], Efix[0][4], Efix[0][2], Sfix[0]) ##### for aoi in self.AOIS: if (utils.point_inside_polygon(x_fixation, y_fixation, self.AOIS[aoi])): self.cur_fix_id += 1 self.notify_app_state_controller(aoi, int(Sfix[0]), int(EfixEndTime), int(EfixEndTime - Sfix[0])) break
def collide_point(self, x, y): x, y = self.to_local(x, y, True) # relatiu return point_inside_polygon(x, y, self.p1 + self.p2 + self.p3 + self.p4 + self.p5 + self.p6)
def collide_point(self, x, y): x, y = self.to_local(x, y, True) # relatiu return point_inside_polygon( x, y, self.p1 + self.p2 + self.p3 + self.p4 + self.p5 + self.p6)