def test_area(self): area = Area(50, 60, 10, 20) self.assertEqual('area', area.isa) self.assertEqual(50, area.x) self.assertEqual(60, area.y) self.assertEqual(10, area.w) self.assertEqual(20, area.h) self.assertEqual(45, area.x1) self.assertEqual(55, area.x2) self.assertEqual(50, area.y1) self.assertEqual(70, area.y2) area.resize(50, 50) self.assertEqual(25, area.x1) self.assertEqual(75, area.x2) self.assertEqual(35, area.y1) self.assertEqual(85, area.y2) area.resize(10, 20) self.assertTrue(area.contains(Location(50, 60))) self.assertTrue(area.contains(Location(45, 50))) self.assertTrue(area.contains(Location(55, 70))) self.assertFalse(area.contains(Location(44, 50))) self.assertFalse(area.contains(Location(56, 70))) self.assertFalse(area.contains(Location(45, 49))) self.assertFalse(area.contains(Location(55, 71))) self.assertAlmostEqual( 10, area.approach_width_from(Location(0, 60)), 2) self.assertAlmostEqual( 20, area.approach_width_from(Location(50, 0)), 2)
def __init__(self, agent): super().__init__('eyes', agent) self.vision = None self.loc = Location(0, 0) self.last_prep_cancel = None self.fixate_fns = [] self.enc_time_f = .001 self.enc_time_exp = .4 self.prep_time = .150 self.exec_time_base = .070 self.exec_time_inc = .002
def test_location(self): loc = Location(10, 20) self.assertEqual('location', loc.isa) self.assertEqual(10, loc.x) self.assertEqual(20, loc.y) loc.move(50, 60) self.assertEqual(50, loc.x) self.assertEqual(60, loc.y) loc2 = Location(30, 40, 'loc') self.assertEqual('loc', loc2.get('isa')) self.assertAlmostEqual(28.28, loc2.distance_to(loc), 2)
def __init__(self, hands, vision): super().__init__("mouse", hands.agent) self.hands = hands self.vision = vision self.worker = hands.worker self.loc = Location(0, 0) self.move_fns = [] self.click_fns = [] self.init_time = .050 self.burst_time = .050 self.fitts_coeff = .100 self.min_fitts_time = .100
def __init__(self, agent, vision, env, pos=None): super().__init__('motor', agent) self.vision = vision self.display = env.display self.keyboard = env.keyboard self.mouse = env.mouse self.pos = pos or Motor.ON_KEYBOARD self.worker = Worker('motor', self) self.wpm(Motor.DEFAULT_TYPING_WPM) self.mouse_loc = Location(0, 0) self.mouse_init_time = .050 self.mouse_burst_time = .050 self.mouse_fitts_coeff = .100 self.mouse_min_fitts_time = .100
def __init__(self, agent, display, eyes=None): super().__init__('vision', agent) self.display = display.set_vision(self) self.eyes = eyes if eyes is not None: eyes.set_vision(self) self.find_buffer = Buffer('vision.find', self) self.encode_buffer = Buffer('vision.encode', self) self.encode_loc = Location(0, 0) self.wait_for_query = None self.last_encode_cancel = None self.attend_fns = [] self.encode_fns = [] self.find_time = .000 self.default_enc_time = .135
def prepare(self, visual, enc_start, enc_dur): if self.last_prep_cancel is not None: self.last_prep_cancel.try_cancel() sd = .1 * \ self.vision.display.degrees_to_pixels( self._compute_eccentricity(visual)) new_loc = Location(visual.x + random.gauss(0, sd), visual.y + random.gauss(0, sd)) self.log('prepare {}'.format(new_loc)) duration = self._rand_time(self.prep_time) def fn(): self.move(visual, new_loc, enc_start, enc_dur) self.last_prep_cancel = self.run_thread_can_cancel(fn, duration)
def __init__(self, agent, eyes=None, display=Display()): super().__init__("vision", agent) self.display = display self.eyes = eyes if eyes is not None: eyes.set_vision(self) self.visuals = {} self.find_buffer = Buffer("vision.find", self) self.encode_buffer = Buffer("vision.encode", self) self.encode_loc = Location(0, 0) self.wait_for_query = None self.last_encode_cancel = None self.attend_fns = [] self.encode_fns = [] self.find_time = .000 self.default_enc_time = .135
class Eyes(Module): def __init__(self, agent): super().__init__('eyes', agent) self.vision = None self.loc = Location(0, 0) self.last_prep_cancel = None self.fixate_fns = [] self.enc_time_f = .001 self.enc_time_exp = .4 self.prep_time = .150 self.exec_time_base = .070 self.exec_time_inc = .002 def set_vision(self, vision): self.vision = vision def add_fixate_fn(self, fn): self.fixate_fns.append(fn) def move_to(self, x, y): self.loc = Location(x, y) def _compute_eccentricity(self, visual): return self.vision.display.pixels_to_degrees( self.loc.distance_to(visual)) def compute_enc_time(self, visual): freq = visual.freq if freq is not None: return (self.enc_time_f * -math.log(freq) * math.exp( self.enc_time_exp * self._compute_eccentricity(visual))) else: return self.vision.default_enc_time def _rand_time(self, time): return max(time + random.gauss(0, time / 3), .001) def prepare(self, visual, enc_start, enc_dur): if self.last_prep_cancel is not None: self.last_prep_cancel.try_cancel() sd = .1 * \ self.vision.display.degrees_to_pixels( self._compute_eccentricity(visual)) new_loc = Location(visual.x + random.gauss(0, sd), visual.y + random.gauss(0, sd)) self.log('prepare {}'.format(new_loc)) duration = self._rand_time(self.prep_time) def fn(): self.move(visual, new_loc, enc_start, enc_dur) self.last_prep_cancel = self.run_thread_can_cancel(fn, duration) def move(self, visual, new_loc, enc_start, enc_dur): self.log('move {}'.format(new_loc)) duration = self._rand_time( self.exec_time_base ) + self.exec_time_inc * self._compute_eccentricity(visual) def fn(): self.log('moved {}'.format(new_loc)) self.loc = new_loc if enc_start + enc_dur > self.time(): completed = (self.time() - enc_start) / enc_dur new_time = self.compute_enc_time(visual) rem_dur = (1 - completed) * new_time if rem_dur > 0: self.vision.start_encode_thread(visual, rem_dur) self.prepare(visual, self.time(), rem_dur) self.vision.display.set_eye(self.loc) for fn in self.fixate_fns: fn(self.loc) self.run_thread(fn, duration)
def move_to(self, x, y): self.loc = Location(x, y)