Beispiel #1
0
 def track_agent(self,
                 agent,
                 coordinates,
                 location,
                 frame=-1,
                 time_stamp=None):
     if not self.active_episode():
         return Result(1, "there is not any active episode")
     if agent in self.agents_locations and self.agents_locations[
             agent] == coordinates:
         return Result(1, "duplicated coordinates given")
     if self.world and self.map.cell(coordinates)["occluded"]:
         return Result(0, "Coordinates correspond to an occluded cell")
     else:
         self.agents_locations[agent] = coordinates
         if time_stamp is None:
             time_stamp = (datetime.now() -
                           self.current_episode_start_time).total_seconds()
         step = {
             "time_stamp": time_stamp,
             "agent_name": agent,
             "coordinates": coordinates,
             "location": location,
             "frame": frame
         }
         self.episodes[-1]["trajectories"].append(step)
         if self.check:
             if agent == "mouse" and coordinates != {"x": -20, "y": 0}:
                 self.check(2)
                 self.check = None
         return Result(0, "%s coordinates recorded" % agent, step)
Beispiel #2
0
 def finish_episode(self):
     if not self.active_episode():
         return Result(1, "there is not any active episode")
     self.episodes[-1]["end_time"] = str(datetime.now())
     self.current_episode_start_time = None
     self.write()
     return Result(0, "episode %d finished" % len(self.episodes))
Beispiel #3
0
 def disable(self, feeder_number):
     if feeder_number not in self.pis.keys():
         return Result(1, "wrong feeder number (%d)" % feeder_number)
     ps = self.pis[feeder_number].get("status")
     is_enabled = ps.content["feeder_status"]["state"] == "disabled"
     if is_enabled:
         return Result(
             1, "feeder number (%d) is already disabled" % feeder_number)
     return self.pis[feeder_number].get("disable_feeder", [feeder_number])
Beispiel #4
0
 def feeder_reached(self, feeder_number):
     if feeder_number not in [1, 2]:
         return Result(1, "wrong feeder number (%d)" % feeder_number)
     if feeder_number == 1:
         if not self.experiment.is_active():
             self.doors.close(1)
             self.doors.close(2)
             self.doors.close(0)
             self.doors.close(3)
             return self.experiment.experiment_ended()
         else:
             self.tracker.write(self.current_experiment_video_folder + "/" +
                                self.experiment.name + "_ep" +
                                ('%02d' % len(self.experiment.episodes)))
             r = self.experiment.start_episode()
             self.experiment.check = self.doors.close
             self.doors.close(1)
             self.doors.open(2)
             self.doors.close(0)
             self.doors.open(3)
             self.feeders.enable(2)
             return r
     else:
         self.tracker.write("end")
         r = self.experiment.finish_episode()
         self.doors.close(3)
         self.doors.open(0)
         self.doors.close(2)
         self.doors.open(1)
         self.feeders.enable(1)
         return r
Beispiel #5
0
 def start_episode(self):
     self.agents_locations = {}
     self.current_episode_start_time = datetime.now()
     self.episodes.append({
         "trajectories": [],
         "start_time":
         str(self.current_episode_start_time),
         "time_stamp": (self.current_episode_start_time -
                        self.start_time).total_seconds()
     })
     return Result(0, "episode %d started" % len(self.episodes))
Beispiel #6
0
 def status(self):
     if self.is_active():
         if self.duration:
             n = self.remaining_time()
             if n <= 0:
                 message = "experiment %s finishing" % self.name
             else:
                 message = "experiment %s in progress" % self.name
                 message += "(%d seconds remaining)" % n
         else:
             message = "experiment %s in progress" % self.name
     else:
         message = "no active experiment"
     return Result(0, message)
Beispiel #7
0
 def start_experiment(self, experiment_name, world_name, duration=0):
     self.experiment = Experiment(experiment_name, world_name, duration)
     self.experiment.start()
     if experiment_name != "":
         self.current_experiment_video_folder = self.videos_folder + "/" + experiment_name
         if not os.path.exists(self.current_experiment_video_folder):
             os.makedirs(self.current_experiment_video_folder)
     message = "experiment '%s'" % experiment_name
     if duration > -1:
         message += " for %d minutes" % duration
     self.doors.close(0)
     self.doors.close(1)
     self.doors.close(2)
     self.doors.close(3)
     self.feeders.enable(1)
     return Result(0, message)
Beispiel #8
0
 def status(self):
     s = self.experiment.status()
     if s.code == 0:
         message = s.message
         for pi in Pi.get_pis():
             message += "\nPi at %s: " % pi.address
             ps = pi.get("status")
             if ps.code:
                 message += "error\n"
             else:
                 message += "ok\n"
                 for door_status in ps.content["door_status"]:
                     message += ("door %d: " % door_status["door_number"]
                                 ) + door_status["state"] + "\n"
                 message += ("feeder %d: " %
                             ps.content["feeder_status"]["feeder_number"]
                             ) + ps.content["feeder_status"]["state"]
         return Result(0, message)
     else:
         return s
Beispiel #9
0
 def quit(self):
     global app
     app.stop()
     return Result(0, "good bye!")
Beispiel #10
0
 def update_background(self):
     self.tracker.write("update_background")
     return Result(0, "Background updated")
Beispiel #11
0
 def calibrate(self, door_number, direction, opening_time, closing_time):
     if door_number not in self.pis.keys():
         return Result(1, "wrong door number (%d)" % door_number)
     return self.pis[door_number].get(
         "calibrate_door",
         [door_number, direction, opening_time, closing_time])
Beispiel #12
0
 def close(self, door_number):
     if door_number not in self.pis.keys():
         return Result(1, "wrong door number (%d)" % door_number)
     return self.pis[door_number].get("close", [door_number])
Beispiel #13
0
 def finish(self):
     self.active = False
     return Result(0, "experiment %s finished" % self.name)
 def GET(self, querystring):
     global habitat
     qs = querystring.split("/")
     command = qs[0]
     if command == "enable_feeder":
         feeder_number = int(qs[1])
         return habitat.enable_feeder(feeder_number).json()
     elif command == "disable_feeder":
         feeder_number = int(qs[1])
         return habitat.disable_feeder(feeder_number).json()
     elif command == "open_door":
         door_number = int(qs[1])
         return habitat.open_door(door_number)
     elif command == "close_door":
         door_number = int(qs[1])
         return habitat.close_door(door_number)
     elif command == "feeder_reached":
         feeder_number = int(qs[1])
         return habitat.feeder_reached(feeder_number).json()
     elif command == "test_feeder":
         feeder_number = int(qs[1])
         duration = int(qs[2])
         repetitions = int(qs[3])
         wait_time = int(qs[4])
         return habitat.test_feeder(feeder_number, duration, repetitions,
                                    wait_time).json()
     elif command == "start_experiment":
         experiment_name = qs[1]
         world_name = qs[2]
         if len(qs) > 3 and qs[3].isnumeric():
             duration = int(qs[3])
             return habitat.start_experiment(experiment_name, world_name,
                                             duration).json()
         else:
             return habitat.start_experiment(experiment_name,
                                             world_name).json()
     elif command == "finish_experiment":
         return habitat.finish_experiment().json()
     elif command == "update_background":
         return habitat.update_background().json()
     elif command == "save_doors_calibration":
         return habitat.save_doors_calibration()
     elif command == "load_doors_calibration":
         return habitat.load_doors_calibration()
     elif command == "test_door":
         door_number = int(qs[1])
         repetitions = int(qs[2])
         return habitat.test_door(door_number, repetitions)
     elif command == "calibrate_door":
         door_number = int(qs[1])
         direction = int(qs[2])
         opening_time = float(qs[3])
         closing_time = float(qs[4])
         return habitat.calibrate_door(door_number, direction, opening_time,
                                       closing_time)
     elif command == "track":
         agent = qs[1]
         x = int(qs[2])
         y = int(qs[3])
         return habitat.track(agent, x, y).json()
     elif command == "end":
         habitat.end()
         app.stop()
         return Result(0, "good bye!").json()
     elif command == "status":
         return habitat.status().json()
     else:
         return Result(1, "unknown command").json()
 def start_server(self):
     port = self.address.split(":")[1] if ":" in self.address else "8081"
     os.system("python3 server.py " + port + " 2>/dev/null &")
     time.sleep(4)
     return Result(0, "Server started")