def index_move_timestamps_at_homebase(x, y, rect_hb): """returns a boolean array for t timestamps under condition 'mouse is at homebase'. homebase is given as a rectangle in a 2 x 4 dicretization of cage. note: different from method 'index_move_timestamps_at_location; in that niche coordinates are slightly corrected accounting for niche walls and enclosure. """ c = Cage() idx_list = [] for rect in rect_hb: tl, tr, bl, br = c.map_rectangle_to_cage_coordinates(rect) # print "Homebase location in original coordinates top_left, top_right, bot_left, bot_right: " # print tl, tr, bl, br if rect == c.activity_to_rectangle['N']: # these are correct cage boundaries -/+something to allow for match # with gridcells in a 2x4 discretization less_than_x = c.nestRightX - 1.2 greater_than_y = c.nestBottomY + 0.7 # 200x faster idx = (x < less_than_x) & (y > greater_than_y) idx_list.append(idx) else: idx1 = (x > tl[0]) & (x < tr[0]) idx2 = (y < tl[1]) & (y > bl[1]) idx = idx1 & idx2 idx_list.append(idx) if len(idx_list) == 1: # single homebase idx = idx_list[0] else: # domino homebase idx = idx_list[0] | idx_list[1] msg = "movement: {} events (off-homebase) vs {} (at-homebase)".format(x.shape[0] - sum(idx), sum(idx)) return idx, msg
def create_dq_figure(): C = Cage() figsize = (16, 5) gs1 = gridspec.GridSpec(3, 6) fig = plt.figure(figsize=figsize) fig.xlims = [4, 32] fig.ylims = [-18, 43] fig.xticks = range(6, 31, 2) fig.yticks = range(0, 45, 10) ax0 = fig.add_subplot(gs1[:, :4]) # distance raster ax1 = fig.add_subplot(gs1[1:, 4]) # position density xbins,ybins=12,24 ax2 = fig.add_subplot(gs1[1:, 5]) # position density xbins,ybins=2,4 # ax1.set_aspect(aspect=2, adjustable='box') # colormap and figure options setattr(fig, 'extent', [C.CMXLower, C.CMXUpper, C.CMYLower, C.CMYUpper]) cmap = get_optionD_cmap() setattr(ax1, 'cmap', cmap) setattr(ax2, 'cmap', truncate_colormap(cmap, 0.5, 1)) setattr(ax1, 'vmin', 0.0001) setattr(ax2, 'vmin', 0.01) setattr(ax1, 'vmax', 1) setattr(ax2, 'vmax', 1) # colorbar axes setattr(ax1, 'axCbar', fig.add_axes([0.75, 0.1, .01, 0.4])) # [left, bottom, width, height] setattr(ax2, 'axCbar', fig.add_axes([0.9, 0.1, .01, 0.4])) return gs1, fig, ax0, ax1, ax2
def display_HB_cell(ax, MD): varNames = ['rect_HB', 'obs_rect'] for var in varNames: if not hasattr(MD, var): MD.load(var) rect_HB, obs_rect = [getattr(MD, var) for var in varNames] C = Cage() color = MD.experiment.fcolors['M'][2] for rect in rect_HB: xy, w, h = C.map_rect4x2_to_cage_coordinates(rect) niche = patches.Rectangle(xy, w, h, fc=color, ec='0.85', alpha=0.5, zorder=0) ax.add_patch(niche)
def index_move_timestamps_at_location(x, y, loc='F'): """returns a boolean array for t timestamps under condition: 'mouse is at rectangle' (Niche/Feeder/Lickometer or other) in a (2, 4) cage discretization position given by rectangle coordinates: (0, 0) top-left (niche) (3, 0) feeder (3, 1) lickometer """ c = Cage() rect = c.activity_to_rectangle[loc] # check if single or domino cell idx = None if len(rect) == 1: tl, tr, bl, br = c.map_rectangle_to_cage_coordinates(rect[0]) if loc == 'N': # Niche less_than_x = tr[0] more_than_y = bl[1] idx = (x < less_than_x) & (y < more_than_y) elif loc == 'F': # Feeder less_than_x = tr[0] less_than_y = tl[1] idx = (x < less_than_x) & (y < less_than_y) elif loc == 'D': # Lickometer more_than_x = tl[0] less_than_y = tl[1] idx = (x > more_than_x) & (y < less_than_y) else: # generic (2, 4) cell location idx1 = (x > tl[0]) & (x < tr[0]) idx2 = (y < tl[1]) & (y > bl[1]) idx = idx1 & idx2 elif len(rect) == 2: # domino cell idxs = [] for r in rect: tl, tr, bl, br = c.map_rectangle_to_cage_coordinates(r) idx1 = (x > tl[0]) & (x < tr[0]) idx2 = (y < tl[1]) & (y > bl[1]) idxs.append(idx1 & idx2) idx = idxs[0] & idxs[1] return idx
def setUp(self): """ Initialize first cage for test 1 """ self.cage_1 = Cage() self.cage_1.height = 0 self.cage_1.width = 0 self.cage_1.length = 0 self.cage_1.volume = 0 self.cage_1.packages = [] """ Create packages for test 1 """ self.package_1 = [10, 10, 830, 83000] self.package_2 = [10, 10, 10, 1000] self.package_3 = [10, 10, 7, 700] """ Initialize second cage for test 2 """ self.cage_2 = Cage() self.cage_2.height = 0 self.cage_2.width = 0 self.cage_2.length = 0 self.cage_2.volume = 0 self.cage_2.packages = [] """ Create packages for test 2 """ self.package_4 = [10, 10, 10, 1000] self.package_5 = [10, 10, 10, 1000] """ Initialize second cage for test 3 """ self.cage_3 = Cage() self.package_6 = [300, 500, 400, 60000000] self.package_7 = [300, 500, 400, 60000000] self.package_8 = [153, 268, 398, 16319592] self.package_9 = [112, 325, 157, 5714800] self.package_10 = [112, 325, 157, 5714800] self.package_11 = [100, 100, 100, 1000000] self.package_12 = [60, 167, 100, 1002000]
def splitIntoCages(self) -> List[List[int]]: cages = [] cage_size = int(self.size**0.5) for i in range(0, cage_size): for j in range(0, cage_size): cage = [] rows = self.data[i * cage_size:(i + 1) * cage_size] for row in rows: cage.append(row[j * cage_size:(j + 1) * cage_size]) cages.append( Cage(cage, (i * cage_size, j * cage_size), cage_size)) return cages
def set_trajectory_layout(ax, SCALE=True): if SCALE: # display distance scale ax.plot([6, 12], [4, 4], color='k', clip_on=False) ax.text(7, 4.8, '5 cm', fontsize=8, clip_on=False) C = Cage() ax.set_xlim([C.CMXLower, C.CMXUpper]) ax.set_ylim([C.CMYLower, C.CMYUpper]) ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('equal') plotting_utils.draw_device_labels(ax) plotting_utils.draw_xbins_ybins_cage_grid(ax)
def initAgents(self, world): self.agentlayer = world.map.getLayer('TechdemoMapGroundObjectLayer') world.agentlayer = self.agentlayer self.boy = Boy(TDS, world, 'PC:boy', self.agentlayer) self.game.instance_to_agent[self.boy.agent.getFifeId()] = self.boy self.boy.start() self.agent_list.append(self.boy) self.girl = Girl(TDS, world, 'PC:girl', self.agentlayer, self) self.game.instance_to_agent[self.girl.agent.getFifeId()] = self.girl self.girl.start() self.agent_list.append(self.girl) self.wizard = Wizard(TDS, world, 'PC:wizard', self.agentlayer, self) self.game.instance_to_agent[self.wizard.agent.getFifeId()] = self.wizard self.wizard.start() self.agent_list.append(self.wizard) self.beekeepers = create_anonymous_agents(TDS, world, 'beekeeper', self.agentlayer, Beekeeper) for beekeeper in self.beekeepers: self.game.instance_to_agent[beekeeper.agent.getFifeId()] = beekeeper beekeeper.start() self.agent_list.append(beekeeper) self.cage = Cage(TDS, world, 'sword_crate', self.agentlayer) self.game.instance_to_agent[self.cage.agent.getFifeId()] = self.cage self.cage.start() self.agent_list.append(self.cage) self.bees = [] for i in range(1, 8): bee = code.agents.bee.Bee(TDS, world, 'NPC:bee:0{}'.format(i), self.agentlayer, self) self.bees.append(bee) self.game.instance_to_agent[bee.agent.getFifeId()] = bee bee.start() self.agent_list.append(bee) self.warrior = Warrior(TDS, world, 'PC:warrior', self.agentlayer) self.game.instance_to_agent[self.warrior.agent.getFifeId()] = self.warrior self.warrior.start() self.agent_list.append(self.warrior) self.chemist = Chemist(TDS, world, 'NPC:chemist', self.agentlayer) self.game.instance_to_agent[self.chemist.agent.getFifeId()] = self.chemist self.chemist.start() self.agent_list.append(self.chemist) self.playableAgent = [] self.reset()
def plot_distance_events(fig, ax, MD): C = Cage() for varName in ['CT', 'CX', 'CY']: if not hasattr(MD, varName): MD.load(varName) color = MD.experiment.fcolors['M'][0] print "plotting distance.." dist = np.sqrt((MD.CX - C.xy_o[0])**2 + (MD.CY - C.xy_o[1])**2) ax.plot(MD.CT / 3600. -7, dist, lw=0.5, color='0.3') plot_events(ax, MD) set_distance_raster_layout(fig, ax)
def parse(self, filename: str) -> Board: with open(filename) as fil: size_line = fil.readline() size_line.strip() size = int(size_line) cages = [] line = fil.readline() while line: row = line.split() goal = int(row[0]) op = row[1] squares = [] for i in range(2, len(row), 2): squares.append((int(row[i]), int(row[i + 1]))) cages.append(Cage(op, goal, squares)) line = fil.readline() data = [[0 for _ in range(size)] for _ in range(size)] board = Board(data, size, cages) return board
def plot_xy(ax, MD, CT_window): varNames = ['CT', 'CX', 'CY', 'velocity', 'MB_idx'] for var in varNames: if not hasattr(MD, var): MD.load(var) T, X, Y, vel, MB_idx = [getattr(MD, var) for var in varNames] C = Cage() MBVT, MBDT = [getattr(MD.experiment, x) for x in ['MBVT', 'MBDT']] # select window idx_start = np.searchsorted(T, CT_window[0]) idx_end = np.searchsorted(T, CT_window[1], side='right') # plot distance to W wdist = np.sqrt((X[idx_start : idx_end] - C.xy_o[0]) ** 2 + \ (Y[idx_start : idx_end] - C.xy_o[1]) ** 2) ax.plot(T[idx_start:idx_end], wdist, label='distance', marker='x', lw=1, color='k', ms=5) # velocity plot ax2 = ax.twinx() ax2.plot(T[idx_start:idx_end], vel[idx_start:idx_end], marker='o', lw=0.5, color='0.5', ms=3, label='velocity') # plot MBVT threshold ax2.axhline(MBVT, c='.5') set_xy_plot_layout(ax, ax2, CT_window)
def time_in_xbins_ybins(m, xbins, ybins, tbin): c = Cage() xlims, ylims = c.cage_boundaries pos_subset = pull_locom_tseries_subset(m, tbin[0], tbin[1]) return total_time_rectangle_bins(pos_subset, xlims, ylims, xbins, ybins)
class AgentManager(): def __init__(self, world): self.player = 0 self.player_faces = ['gui/images/hud_boy.png', 'gui/images/hud_girl.png', 'gui/images/hud_warrior.png', 'gui/images/hud_wizard.png'] self.agent_list = [] self.game = code.game.Game.getGame() """ Intializes all the agents. All these instances are also added to the self.agent_list list to simplify the searches by name or id. """ def initAgents(self, world): self.agentlayer = world.map.getLayer('TechdemoMapGroundObjectLayer') world.agentlayer = self.agentlayer self.boy = Boy(TDS, world, 'PC:boy', self.agentlayer) self.game.instance_to_agent[self.boy.agent.getFifeId()] = self.boy self.boy.start() self.agent_list.append(self.boy) self.girl = Girl(TDS, world, 'PC:girl', self.agentlayer, self) self.game.instance_to_agent[self.girl.agent.getFifeId()] = self.girl self.girl.start() self.agent_list.append(self.girl) self.wizard = Wizard(TDS, world, 'PC:wizard', self.agentlayer, self) self.game.instance_to_agent[self.wizard.agent.getFifeId()] = self.wizard self.wizard.start() self.agent_list.append(self.wizard) self.beekeepers = create_anonymous_agents(TDS, world, 'beekeeper', self.agentlayer, Beekeeper) for beekeeper in self.beekeepers: self.game.instance_to_agent[beekeeper.agent.getFifeId()] = beekeeper beekeeper.start() self.agent_list.append(beekeeper) self.cage = Cage(TDS, world, 'sword_crate', self.agentlayer) self.game.instance_to_agent[self.cage.agent.getFifeId()] = self.cage self.cage.start() self.agent_list.append(self.cage) self.bees = [] for i in range(1, 8): bee = code.agents.bee.Bee(TDS, world, 'NPC:bee:0{}'.format(i), self.agentlayer, self) self.bees.append(bee) self.game.instance_to_agent[bee.agent.getFifeId()] = bee bee.start() self.agent_list.append(bee) self.warrior = Warrior(TDS, world, 'PC:warrior', self.agentlayer) self.game.instance_to_agent[self.warrior.agent.getFifeId()] = self.warrior self.warrior.start() self.agent_list.append(self.warrior) self.chemist = Chemist(TDS, world, 'NPC:chemist', self.agentlayer) self.game.instance_to_agent[self.chemist.agent.getFifeId()] = self.chemist self.chemist.start() self.agent_list.append(self.chemist) self.playableAgent = [] self.reset() """ This method checks if the first 3 bees are near the beeboxes. """ def beesAtHome(self): for bee in self.bees: if int(bee.agentName[-2:]) <= 3 and bee.mode == code.agents.bee._MODE_WILD: return False return True """ This method checks if the bees whith id >= 4 are all dead. """ def beesDead(self): for bee in self.bees: if int(bee.agentName[-2:]) >= 4 and bee.mode != code.agents.bee._MODE_DEAD: return False return True def reset(self): for p in self.playableAgent: p.reset() self.playableAgent = [self.boy, self.girl] self.active_agent = self.boy """ Returns the current active agent. """ def getActiveAgent(self): return self.active_agent """ Returns the FIFE instance of the current active agent. """ def getActiveInstance(self): return self.active_agent.agent """ Returns the current active agent's location. """ def getActiveAgentLocation(self): return self.active_agent.agent.getLocation() def getHero(self): return self.active_agent def getGirl(self): return self.girl """ Changes the current active agent. The list self.playableAgent contains all the currently playable characters. """ def toggleAgent(self, world, face_button): self.player = (self.player + 1) % len(self.playableAgent) face_button.up_image = self.player_faces[self.player] face_button.down_image = self.player_faces[self.player] face_button.hover_image = self.player_faces[self.player] for i in range(len(self.playableAgent)): self.playableAgent[i].idle() if i == self.player: self.playableAgent[i].isActive = True world.cameras['main'].attach(self.playableAgent[i].agent) world.cameras['small'].attach(self.girl.agent) self.active_agent = self.playableAgent[i] else: self.playableAgent[i].isActive = False self.playableAgent[i].follow_hero() """ Returns the Agent to the agent with a specific fifeId. """ def getAgentFromId(self, fifeId): for ag in self.agent_list: if ag.agent.getFifeId() == fifeId: return ag return None """ Returns the Agent to the agent with a specific name. """ def getAgentByName(self, name): for ag in self.agent_list: if ag.agentName == name: return ag return None """ Adds a new playable agent if it's not yet present inside the playableAgent list. """ def addNewPlayableAgent(self, name): for a in self.playableAgent: if a.agentName == name: return for a in self.agent_list: if a.agentName == name: self.playableAgent.append(a) if a.agentName != self.active_agent.agentName: a.follow_hero() def destroy(self): for a in self.agent_list: a.destroy()
app.config['BASIC_AUTH_USERNAME'] = '******' app.config['BASIC_AUTH_PASSWORD'] = '******' global_time_start = datetime.time(9) # 9 am global_time_stop = datetime.time(21) # 9 pm # How often the tempurature/humidity are checked # to automatically switch the lamps/pumps update_interval = 60 # seconds cages = [ Cage(name='spider 1', sensor_chan=2, lamp_pin=29, pump_pin=32, temp_goal=30, temp_prec=2, hum_threshold=60, time_start=global_time_start, time_stop=global_time_stop), Cage(name='spider 2', sensor_chan=3, lamp_pin=31, pump_pin=36, temp_goal=27, temp_prec=2, hum_threshold=40, time_start=global_time_start, time_stop=global_time_stop), Cage(name='spider 3', sensor_chan=4,
class TestCage(unittest.TestCase): def setUp(self): """ Initialize first cage for test 1 """ self.cage_1 = Cage() self.cage_1.height = 0 self.cage_1.width = 0 self.cage_1.length = 0 self.cage_1.volume = 0 self.cage_1.packages = [] """ Create packages for test 1 """ self.package_1 = [10, 10, 830, 83000] self.package_2 = [10, 10, 10, 1000] self.package_3 = [10, 10, 7, 700] """ Initialize second cage for test 2 """ self.cage_2 = Cage() self.cage_2.height = 0 self.cage_2.width = 0 self.cage_2.length = 0 self.cage_2.volume = 0 self.cage_2.packages = [] """ Create packages for test 2 """ self.package_4 = [10, 10, 10, 1000] self.package_5 = [10, 10, 10, 1000] """ Initialize second cage for test 3 """ self.cage_3 = Cage() self.package_6 = [300, 500, 400, 60000000] self.package_7 = [300, 500, 400, 60000000] self.package_8 = [153, 268, 398, 16319592] self.package_9 = [112, 325, 157, 5714800] self.package_10 = [112, 325, 157, 5714800] self.package_11 = [100, 100, 100, 1000000] self.package_12 = [60, 167, 100, 1002000] def tearDown(self): pass def test_append(self): """ Test appending 3 boxes if they are put into correct place """ self.cage_1.append(self.package_1) self.assertEqual(self.cage_1.height, 10) self.assertEqual(self.cage_1.width, 10) self.assertEqual(self.cage_1.length, 830) self.assertEqual(self.cage_1.volume, 83000) self.cage_1.append(self.package_2) self.assertEqual(self.cage_1.height, 10) self.assertEqual(self.cage_1.width, 10) self.assertEqual(self.cage_1.length, 840) self.assertEqual(self.cage_1.volume, 84000) self.cage_1.append(self.package_3) self.assertEqual(self.cage_1.height, 10) self.assertEqual(self.cage_1.width, 20) self.assertEqual(self.cage_1.length, 840) self.assertEqual(self.cage_1.volume, 84700) """ Test appending 2 boxes if they are put into correct place """ self.cage_2.append(self.package_4) self.assertEqual(self.cage_2.height, 10) self.assertEqual(self.cage_2.width, 10) self.assertEqual(self.cage_2.length, 10) self.assertEqual(self.cage_2.volume, 1000) self.cage_2.append(self.package_4) self.assertEqual(self.cage_2.height, 10) self.assertEqual(self.cage_2.width, 10) self.assertEqual(self.cage_2.length, 20) self.assertEqual(self.cage_2.volume, 2000) """ Test appending 7 boxes if they are put into correct place """ self.cage_3.append(self.package_6) self.assertEqual(self.cage_3.height, 300) self.assertEqual(self.cage_3.width, 500) self.assertEqual(self.cage_3.length, 400) self.assertEqual(self.cage_3.volume, 60000000) self.cage_3.append(self.package_7) self.assertEqual(self.cage_3.height, 300) self.assertEqual(self.cage_3.width, 500) self.assertEqual(self.cage_3.length, 800) self.assertEqual(self.cage_3.volume, 120000000) # [153, 268, 398, 16319592] self.cage_3.append(self.package_8) self.assertEqual(self.cage_3.height, 453) self.assertEqual(self.cage_3.width, 268) self.assertEqual(self.cage_3.length, 398) self.assertEqual(self.cage_3.volume, 136319592) # [112, 325, 157, 5714800] h - w - l self.cage_3.append(self.package_9) self.assertEqual(self.cage_3.height, 453) self.assertEqual(self.cage_3.width, 593) self.assertEqual(self.cage_3.length, 398) self.assertEqual(self.cage_3.volume, 142034392) # [112, 325, 157, 5714800] h - w - l self.cage_3.append(self.package_10) self.assertEqual(self.cage_3.height, 453) self.assertEqual(self.cage_3.width, 593) self.assertEqual(self.cage_3.length, 555) self.assertEqual(self.cage_3.volume, 147749192) # [100, 100, 100, 1000000] h - w - l self.cage_3.append(self.package_11) self.assertEqual(self.cage_3.height, 453) self.assertEqual(self.cage_3.width, 593) self.assertEqual(self.cage_3.length, 655) self.assertEqual(self.cage_3.volume, 148749192) # [60, 167, 100, 1002000] h - w - l self.cage_3.append(self.package_12) self.assertEqual(self.cage_3.height, 453) self.assertEqual(self.cage_3.width, 593) self.assertEqual(self.cage_3.length, 755) self.assertEqual(self.cage_3.volume, 149751192)