예제 #1
0
 def setUp(self):
     self.task_name = 'a task'
     self.other_task_name = 'another task'
     self.third_task_name = 'yet another task'
     self.tk = TimeKeeper()
     self._fakeTask(self.task_name)
     self._fakeTask(self.task_name)
     self._fakeTask(self.other_task_name)
     self._fakeTask(self.other_task_name)
     self._fakeTask(self.other_task_name)
     self._fakeTask(self.third_task_name)
예제 #2
0
파일: plot.py 프로젝트: prashant94/server
	def __init__(self, figsize=(16,12), max_points=20, scaling=4, y_min=-2, y_max=2):
		super(Plot, self).__init__()
		self.figsize = figsize
		self.figure = plt.figure(figsize=self.figsize, facecolor='#ffffff')
		self.axes = {
			'x':self.figure.add_subplot(311),
			'y':self.figure.add_subplot(312),
			'z':self.figure.add_subplot(313)
		}
		self.max_points = max_points
		self.scaling = scaling
		self.A = AccelrationVector(max_points=self.max_points)
		self.T = TimeKeeper(max_points=self.max_points, scaling=self.scaling)

		if self.A.max_points != self.T.max_points :
			raise ValueError("TimeKeeper and AccelrationVector objects should have same max ")

		#draw the first version
		for acc, axis in self.axes.iteritems():
			axis.set_title(acc)
			axis.set_ylim([y_min, y_max])
			axis.set_xlim([1.0/self.T.scaling, self.T.max_points/self.T.scaling])

		plt.ion()
		plt.tight_layout()
		plt.show()
예제 #3
0
파일: plot.py 프로젝트: prashant94/server
class Plot(object):
	"""Hold the real plots"""
	def __init__(self, figsize=(16,12), max_points=20, scaling=4, y_min=-2, y_max=2):
		super(Plot, self).__init__()
		self.figsize = figsize
		self.figure = plt.figure(figsize=self.figsize, facecolor='#ffffff')
		self.axes = {
			'x':self.figure.add_subplot(311),
			'y':self.figure.add_subplot(312),
			'z':self.figure.add_subplot(313)
		}
		self.max_points = max_points
		self.scaling = scaling
		self.A = AccelrationVector(max_points=self.max_points)
		self.T = TimeKeeper(max_points=self.max_points, scaling=self.scaling)

		if self.A.max_points != self.T.max_points :
			raise ValueError("TimeKeeper and AccelrationVector objects should have same max ")

		#draw the first version
		for acc, axis in self.axes.iteritems():
			axis.set_title(acc)
			axis.set_ylim([y_min, y_max])
			axis.set_xlim([1.0/self.T.scaling, self.T.max_points/self.T.scaling])

		plt.ion()
		plt.tight_layout()
		plt.show()

	def update_axes(self):
		for direction, axis in self.axes.iteritems():
			change = self.T.max_points/self.T.scaling - 1.0/self.T.scaling
			xmin,xmax = axis.get_xlim()

			axis.set_xlim(xmin+change, xmax+change)
				
	def update(self, ax, ay, az):
		self.A.update([ax, ay, az]) 
		self.T.tick()
		i = 0  
		for direction, axis in self.axes.iteritems():
			if self.T.points[-1] >= self.max_points/self.scaling :
				axis.set_axis_bgcolor('white')
			axis.plot(self.T.points , self.A[direction], alpha=0.5, linewidth=0.05, color='red')
		plt.draw()
예제 #4
0
class TestBase(unittest.TestCase):
    def setUp(self):
        self.task_name = 'a task'
        self.other_task_name = 'another task'
        self.third_task_name = 'yet another task'
        self.tk = TimeKeeper()
        self._fakeTask(self.task_name)
        self._fakeTask(self.task_name)
        self._fakeTask(self.other_task_name)
        self._fakeTask(self.other_task_name)
        self._fakeTask(self.other_task_name)
        self._fakeTask(self.third_task_name)

    def _fakeTask(self, name):
        self.tk.start(name)
        j = 0
        for i in range(10000): j+=i%2
        self.tk.end(name)
예제 #5
0
def load_grid(agent):
    wait_time = 0
    keeper = TimeKeeper()
    while wait_time < 10:
        #sys.stdout.write(".")
        world_state = agent.getWorldState()
        if not world_state.is_mission_running:
            return None
        if len(world_state.errors) > 0:
            raise AssertionError('Could not load grid.')

        if world_state.number_of_observations_since_last_state > 0 and \
           json.loads(world_state.observations[-1].text):
            result = json.loads(world_state.observations[-1].text)
            result["time"] = time.time()
            return result

        keeper.advance_by(0.05)
        wait_time += 0.05
예제 #6
0
 def dead(self):
     """
     Checks whether a tamagotchi is dead
     :return: true if the tamagotchi is dead
     """
     self.tamagotchi.update_status(TimeKeeper.time_check())
     if self.tamagotchi.health == 0:
         return True
     else:
         return False
예제 #7
0
파일: tests.py 프로젝트: prashant94/server
class TestTimeKeeper(unittest.TestCase):
	def setUp(self):
		self.max_points = 100
		self.scaling = 4
		self.T = TimeKeeper(max_points=self.max_points, scaling=self.scaling)

	def test_tick(self):
		for i in range(self.max_points - 100):
			self.T.tick()
			self.assertTrue(len(self.T.points) == i+1)

	def test__fix_length(self):
		for i in range(self.max_points+10):
			self.T.tick()
		self.assertTrue(len(self.T.points) == self.max_points)

	def test_trucate(self):
		for i in range(self.max_points*self.scaling + 100):
			self.T.tick()
		self.T.truncate()
		self.assertTrue(len(self.T.points) == 0)
예제 #8
0
class Reshaper(object):
    '''
    Base Class for the PyReshaper general operations.  Specific operations
    should be defined as derived types.
    '''

    def __init__(self, specifier, serial=False, verbosity=1, once=False):
        '''
        Constructor

        @param specifier  An instance of the Specifier class, defining the
                          input specification for this reshaper operation.

        @param serial     True or False, indicating whether the operation
                          should be performed in serial (True) or parallel
                          (False).  The default is to assume parallel operation
                          (but serial will be chosen if the mpi4py cannot be
                          found when trying to initialize decomposition.

        @param verbosity  Level of printed output (stdout).  A value of 0 means
                          no output, and a higher value means more output.  The
                          default value is 1.

        @param once       True or False, indicating whether the Reshaper should
                          write all metadata to a 'once' file (separately).
        '''

        # Check types
        if (not isinstance(specifier, Specifier)):
            err_msg = "Input must be given in the form of a Specifier object"
            raise TypeError(err_msg)
        if (type(serial) is not bool):
            err_msg = "Serial indicator must be True or False."
            raise TypeError(err_msg)
        if (type(verbosity) is not int):
            err_msg = "Verbosity level must be an integer."
            raise TypeError(err_msg)
        if (type(once) is not bool):
            err_msg = "Once-file indicator must be True or False."
            raise TypeError(err_msg)

        ## Whether to write a once file
        self._use_once_file = once

        ## Internal timer data
        self._timer = TimeKeeper()

        ## Dictionary storing read/write data amounts
        self.assumed_block_size = float(4 * 1024 * 1024)
        self._byte_counts = {}

        self._timer.start('Initializing Messenger')
        ## Pointer to the messenger
        self._messenger = create_messenger(serial=serial)
        self._timer.stop('Initializing Messenger')

        # Set the verbosity in the messenger
        self._messenger.verbosity = verbosity

        # Debug output starting
        self._messenger.print_once('Initializing Reshaper', vlevel=1)

        ## Input specification
        self._specifier = specifier

        # Validate the user input data
        self._timer.start('Specifier Validation')
        self._specifier.validate()
        self._timer.stop('Specifier Validation')
        self._messenger.print_once('Specifier validated', vlevel=1)

    def convert(self, output_limit=0):
        '''
        Method to perform the Reshaper's designated operation.

        @param output_limit Limit on the number of output files to write
                            during the convert() operation.  If set to 0,
                            no limit is placed.  This limits the number of
                            output files produced by each processor in a
                            parallel run.
        '''
        pass

    def print_diagnostics(self):
        '''
        Print out timing and I/O information collected up to this point
        in the Reshaper's operation.
        '''

        # Get all totals and maxima
        my_times = self._timer.get_all_times()
        #print str(self._messenger.get_rank()) + ': all_times =', my_times
        max_times = self._messenger.max(my_times)
        #print str(self._messenger.get_rank()) + ': max_times =', max_times
        my_bytes = self._byte_counts
        #print str(self._messenger.get_rank()) + ': byte_counts =', my_bytes
        total_bytes = self._messenger.sum(my_bytes)
        #print str(self._messenger.get_rank()) + ': total_bytes =', total_bytes

        # Synchronize
        self._messenger.sync()

        # Print timing maxima
        o = self._timer.get_order()
        time_table_str = _pprint_dictionary('TIMING DATA', max_times, order=o)
        self._messenger.print_once(time_table_str, vlevel=0)

        # Convert byte count to MB
        for name in total_bytes:
            total_bytes[name] = total_bytes[name] / float(1024 * 1024)

        # Print byte count totals
        byte_count_str = _pprint_dictionary('BYTE COUNTS (MB)', total_bytes)
        self._messenger.print_once(byte_count_str, vlevel=0)
예제 #9
0
    def __init__(self, specifier, serial=False, verbosity=1, once=False):
        '''
        Constructor

        @param specifier  An instance of the Specifier class, defining the
                          input specification for this reshaper operation.

        @param serial     True or False, indicating whether the operation
                          should be performed in serial (True) or parallel
                          (False).  The default is to assume parallel operation
                          (but serial will be chosen if the mpi4py cannot be
                          found when trying to initialize decomposition.

        @param verbosity  Level of printed output (stdout).  A value of 0 means
                          no output, and a higher value means more output.  The
                          default value is 1.

        @param once       True or False, indicating whether the Reshaper should
                          write all metadata to a 'once' file (separately).
        '''

        # Check types
        if (not isinstance(specifier, Specifier)):
            err_msg = "Input must be given in the form of a Specifier object"
            raise TypeError(err_msg)
        if (type(serial) is not bool):
            err_msg = "Serial indicator must be True or False."
            raise TypeError(err_msg)
        if (type(verbosity) is not int):
            err_msg = "Verbosity level must be an integer."
            raise TypeError(err_msg)
        if (type(once) is not bool):
            err_msg = "Once-file indicator must be True or False."
            raise TypeError(err_msg)

        ## Whether to write a once file
        self._use_once_file = once

        ## Internal timer data
        self._timer = TimeKeeper()

        ## Dictionary storing read/write data amounts
        self.assumed_block_size = float(4 * 1024 * 1024)
        self._byte_counts = {}

        self._timer.start('Initializing Messenger')
        ## Pointer to the messenger
        self._messenger = create_messenger(serial=serial)
        self._timer.stop('Initializing Messenger')

        # Set the verbosity in the messenger
        self._messenger.verbosity = verbosity

        # Debug output starting
        self._messenger.print_once('Initializing Reshaper', vlevel=1)

        ## Input specification
        self._specifier = specifier

        # Validate the user input data
        self._timer.start('Specifier Validation')
        self._specifier.validate()
        self._timer.stop('Specifier Validation')
        self._messenger.print_once('Specifier validated', vlevel=1)
예제 #10
0
import secrets
from delay import Delay
from timekeeper import TimeKeeper

time = TimeKeeper(0, 0, 0, 0, 0)

dayCountActive = 7  # Max days left before reminder becomes active
ldrMinValue = 5  # LDR value under which screen turns off [0-1023]

HTTPupdateDelay = Delay(10 * 60 * 1000)  # delay time in milliseconds
screenUpdateDelay = Delay(1 * 1000)  # delay time in milliseconds
apiUrl = "http://api.lakitna.nl/?src=upy&key=" + secrets.apikey  # URL to API

HTTPupdateFlag = True
예제 #11
0
 def check_status(self):
     self.tamagotchi.update_status(TimeKeeper.time_check())
     if self.dead():
         return self.dead_message()
     else:
         return self.tamagotchi.check_status()
예제 #12
0
 def __init__(self):
     self.tamagotchi = self.generate_rand_tamagotchi()
     self.food_list = FoodList().get_food_list()
     self.act_list = ActivityList().get_act_list()
     TimeKeeper.time_check()
예제 #13
0
    for i in range(iterations):
        time.sleep(1)
        params = (random.randint(10, 30) * random.randrange(-1, 2, 2),
                  random.randint(10, 30) * random.randrange(-1, 2, 2),
                  random.randint(10, 30))
        mission = MalmoPython.MissionSpec(my_mission.get_mission_xml(params),
                                          True)
        my_mission.load_duo_mission(mission, agents)
        shoot_agent.reset()
        shoot_agent.reset_shoot_loop()
        move_agent.reset()

        # Loop until mission ends:
        record_cycle = 86
        total_time = 0
        keeper = TimeKeeper()

        my_mission.chat_command_init(shoot_agent, move_agent, params)
        shoot_agent.agent.sendCommand("use 1")

        world_state = shoot_agent.agent.peekWorldState()
        shoot_agent.reset_shoot_loop()
        target = Target()
        first_target_found = False
        initial_delay = 5
        while world_state.is_mission_running:

            shooter_obs = load_grid(shoot_agent.agent)
            mover_obs = load_grid(move_agent.agent)
            if not shooter_obs or not mover_obs:
                break
예제 #14
0
파일: app.py 프로젝트: ACBatz/server
        p2_loc = p2.get_propagation(now)[0]
    dist = distance(p1_loc, p2_loc)
    latency = dist / 1000 / 300000
    bps = upload_speed_mbps * 125000
    comp_latency = packet_size_bytes / bps
    return latency + comp_latency


uccs = Station('uccs', 38.893601, -104.800619, 0, size=10)
johann = Station('johann', -26.2041, 28.0473, 0, size=10)

sats = []
lines = []
stations = [uccs, johann]

time_keeper = TimeKeeper()


@app.route('/api/satellite', methods=['POST'])
def satellite():
    global sats
    if len(sats) == 0:
        sats = SatelliteTrackService.get_satellite_data()
    try:
        clock_time = request.get_json()['time']
        print('Received POST with jdate of [{}]'.format(clock_time))
        strptime = datetime.strptime(clock_time, '%Y-%m-%dT%H:%M:%S.%f%z')
        time_keeper.set_time(strptime)
        start = datetime.now()
        sats_ = list(
            filter(lambda y: y is not None,
예제 #15
0
    def __init__(self, specifier, serial=False, verbosity=1, once=False):
        '''
        Constructor

        @param specifier  An instance of the Specifier class, defining the
                          input specification for this reshaper operation.

        @param serial     True or False, indicating whether the operation
                          should be performed in serial (True) or parallel
                          (False).  The default is to assume parallel operation
                          (but serial will be chosen if the mpi4py cannot be
                          found when trying to initialize decomposition.

        @param verbosity  Level of printed output (stdout).  A value of 0 means
                          no output, and a higher value means more output.  The
                          default value is 1.

        @param once       True or False, indicating whether the Reshaper should
                          write all metadata to a 'once' file (separately).
        '''

        # Check types
        if (not isinstance(specifier, Specifier)):
            err_msg = "Input must be given in the form of a Specifier object"
            raise TypeError(err_msg)
        if (type(serial) is not bool):
            err_msg = "Serial indicator must be True or False."
            raise TypeError(err_msg)
        if (type(verbosity) is not int):
            err_msg = "Verbosity level must be an integer."
            raise TypeError(err_msg)
        if (type(once) is not bool):
            err_msg = "Once-file indicator must be True or False."
            raise TypeError(err_msg)

        ## Whether to write a once file
        self._use_once_file = once

        ## Internal timer data
        self._timer = TimeKeeper()

        ## Dictionary storing read/write data amounts
        self.assumed_block_size = float(4 * 1024 * 1024)
        self._byte_counts = {}

        self._timer.start('Initializing Messenger')
        ## Pointer to the messenger
        self._messenger = create_messenger(serial=serial)
        self._timer.stop('Initializing Messenger')

        # Set the verbosity in the messenger
        self._messenger.verbosity = verbosity

        # Debug output starting
        self._messenger.print_once('Initializing Reshaper', vlevel=1)

        ## Input specification
        self._specifier = specifier

        # Validate the user input data
        self._timer.start('Specifier Validation')
        self._specifier.validate()
        self._timer.stop('Specifier Validation')
        self._messenger.print_once('Specifier validated', vlevel=1)
예제 #16
0
import chatterbot_func
import chatterbot
import random

from timekeeper import TimeKeeper

print "Say something to our chatterbot!"

bots = [
    chatterbot.Bot("Etta"),
    chatterbot_func.create_bot("Gertrude"),
]

keeper = TimeKeeper()
keeper.start()

while True:
    inp = raw_input()
    print chatterbot_func.chat_loud(random.choice(bots), inp)
    print "Time elapsed: %d seconds" % int(keeper.elapsed())
예제 #17
0
from spacetrackservice import SatelliteTrackService
from station import Station
from datetime import datetime, timedelta
from lineofsightlibrary import do_routing
from timekeeper import TimeKeeper
import csv

sats = SatelliteTrackService.get_satellite_data()
uccs = Station('uccs', 38.893601, -104.800619, 0, size=20)
johann = Station('johann', -26.2041, 28.0473, 0, size=20)

time = datetime.now()
timekeeper = TimeKeeper(time)
with open('results-only-distance.csv', 'w', newline='') as csv_file:
    writer = csv.writer(csv_file)
    writer.writerow(['Hops', 'Time to Compute', 'Traveled', 'Ping'])
for i in range(10000):
    print('Testing with time={}'.format(timekeeper.get_time()))
    start = datetime.now()
    root = do_routing(uccs, johann, sats, timekeeper.get_time())
    node = root.root
    stop = datetime.now()
    hops = 0
    dist = 0
    while node.data is not johann:
        if node.child.data is johann:
            dist = node.child.distance
            pass
        else:
            hops = hops + 1
        node = node.child
예제 #18
0
파일: tests.py 프로젝트: prashant94/server
	def setUp(self):
		self.max_points = 100
		self.scaling = 4
		self.T = TimeKeeper(max_points=self.max_points, scaling=self.scaling)
예제 #19
0
    def run(self, fullscreen=fullscreen):
        last_state = current_state = None
        show_dbg = False
        minim_dbg = False
        dbgfont_name = "Monospace"
        dbgfont_size = 11
        self.dbgfont = fontutils.get_sysfont(dbgfont_name, dbgfont_size)
        force_show_dbg = False
        dbgcolor = Color.Red

        def get_dbg_text_render(text):
            mtr = fontutils.get_multiline_text_render
            return mtr(self.dbgfont,
                       text,
                       antialias=False,
                       color=dbgcolor,
                       background=Color.Black,
                       dolog=False,
                       cache=False)

        max_fps_vals = itertools.cycle((60, 0, 150))
        max_fps = next(max_fps_vals)
        act_max_fps = max_fps
        # Runtime profiling
        events_time, update_time, draw_time, screenupdate_time = [
            TValue() for _ in range(4)
        ]
        self.total_events_time = self.total_update_time = 0
        self.total_draw_time = self.total_screenupdate_time = 0
        self.profile_update_tick = self.profile_draw_tick = False
        self.recording = False
        self.record_writer = None
        self.capture_screenshot = False
        self.enable_autoscale = False
        self._get_mouse_pos = pygame.mouse.get_pos  # Overriden to scale while autoscaling
        console_enabled = False

        # Console functions
        def clear_caches():
            get = lambda mod: [
                getattr(mod, c) for c in dir(mod)
                if c.endswith("_cache") and hasattr(getattr(mod, c), "clear")
            ]
            for cache in get(imglib) + get(fontutils):
                cache.clear()

        def get_level():
            return current_state.level

        def get_player():
            return self.game.player

        def change_level(level):
            if isinstance(level, type): level = level()
            if hasattr(current_state, "level"):
                level.parent = current_state
                current_state.level = level
                get_player().level = level
            else:
                raise ValueError("Current state doesn't use a level")

        def spawn_enemy(cls, col, row, count=1):
            lvl = get_level()
            for _ in range(count):
                lvl.sprites.append(cls(lvl, lvl.layout[row][col]))

        def drop_item(item_cls, pos):
            lvl = get_level()
            lvl.sprites.append(playeritems.DroppedItem(lvl, pos, item_cls))

        def get_sprites_by_class(cls):
            return get_level().get_sprites_if(
                lambda sprite: type(sprite) is cls)

        def get_sprite_by_class(cls):
            return get_sprites_by_class(cls)[0]

        def give(arg):
            cls = getattr(playeritems, arg) if isinstance(arg, str) else arg
            return get_player().inventory.add_item(cls(get_player()))

        def ring_of_fire(count=360):
            l = get_level()
            c = get_player().rect.center
            P = projectiles.Fireball.from_angle
            q = 360 / count
            for i in range(count):
                l.sprites.append(P(l, c, q * i))

        def test_particle(n=100):
            l = get_level()
            p = get_player()
            P = particles.Particle.from_sprite
            for i in range(n):
                l.particles.append(
                    P(p, 5, utils.Vector.uniform(2), 300, Color.Green))

        def set_max_mana():
            get_player().mana_points = get_player().max_mana_points

        def set_max_hp():
            get_player().health_points = get_player().max_health_points

        autocalls = []
        autocalls_a = []
        bindings = {}
        if enable_recording:

            def start_record():
                self.recording = True
                self.record_writer = cv2.VideoWriter("record.mp4", 0x00000021,
                                                     60, (1024, 768), True)

            def stop_record():
                self.recording = False
                del self.record_writer
                self.record_writer = None

            bindings[pygame.K_F9] = start_record
            bindings[pygame.K_F10] = stop_record

        def autoscale(*resolution):
            """
            self.game.vars["screen"] and screen always refer to a WINDOW_SIZE
            surface that may or may not be the display, but will never change size.
            _screen always refers to the display and may have any size
            if enable_autoscale is True, otherwise it is WINDOW_SIZE.
            If enable_autoscale is False all of these values point to the same
            surface.
            """
            if fullscreen:
                return
            elif not resolution or resolution == WINDOW_SIZE or resolution == 1:
                disable_autoscale()
                return
            if len(resolution) == 1:  # as scale factor
                n = resolution[0]
                resolution = (int(WINDOW_SIZE[0] * n), int(WINDOW_SIZE[1] * n))
                print(resolution)
            try:
                print("Enabling autoscale with resolution:", resolution)
                self._screen = pygame.display.set_mode(resolution,
                                                       NORMAL_FLAGS)
                self.game.vars["screen"] = self.screen = pygame.Surface(
                    WINDOW_SIZE)
                a, b = resolution, WINDOW_SIZE
                # Smaller resolution = higher f, and reverse
                f = (b[0] / a[0], b[1] / a[1])
                print("Fix factor:", f)

                def get_mouse_pos_override(*, f=f):
                    p = self._get_mouse_pos()
                    return round(f[0] * p[0]), round(f[1] * p[1])

                pygame.mouse.get_pos = get_mouse_pos_override
                self.console.erase_all()
                self.enable_autoscale = True
            except:
                print("Error while trying to enable autoscale:")
                traceback.print_exc()
                autoscale(*WINDOW_SIZE)

        def set_max_fps(v):
            global act_max_fps
            act_max_fps = v

        def disable_autoscale():
            print("Disabling autoscale")
            old_screen = pygame.display.set_mode(WINDOW_SIZE, NORMAL_FLAGS)
            self.game.vars["screen"] = self._screen = self.screen = screen
            pygame.mouse.get_pos = self._get_mouse_pos
            self.enable_autoscale = False

        def create_savefile(filename="save.json"):
            save = self.game.create_save()
            json_str = json.dumps(save)
            with open(filename, "w") as file:
                file.write(json_str)

        def load_savefile(filename="save.json"):
            assert isinstance(current_state, states.MainMenuState)
            with open(filename, "r") as file:
                save_str = file.read()
            save = json.loads(save_str)
            level = self.game.load_save(save)
            self.game.push_state(
                states.DungeonState(self.game,
                                    player=self.game.player,
                                    level=level,
                                    repos_player=False))

        def bind_key(key, func):
            bindings[key] = func

        def autocall(func):
            autocalls.append(func)

        def autocall_a(func):
            autocalls_a.append(func)

        def infmana():
            autocall(set_max_mana)

        def godmode():
            autocall(set_max_hp)

        def noclip():
            get_player().noclip = not get_player().noclip

        # Main loop
        pause = False
        clock = pygame.time.Clock()
        self.running = True
        while self.running:
            # Event handling
            mouse_pos = pygame.mouse.get_pos()
            pressed_keys = controls.KeyboardState(pygame.key.get_pressed())
            events = pygame.event.get()
            for event in events:
                exit_status = exit_event(event, pressed_keys)
                if exit_status != 0:
                    if exit_status == 1:
                        print("Exit from app by generic exit event")
                    elif exit_status == 2:
                        print("Exit from app by shortcut")
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == controls.DebugKeys.ToggleConsole:
                        console_enabled = not console_enabled
                        if show_dbg:
                            force_show_dbg = True  # reposition the text
                    elif event.key == controls.DebugKeys.ToggleDebug:
                        show_dbg = not show_dbg
                        if show_dbg: force_show_dbg = True
                        minim_dbg = alt_pressed(pressed_keys)
                    elif event.key == controls.DebugKeys.ToggleMouse:
                        if not current_state.use_mouse:
                            self.game.vars[
                                "forced_mouse"] = not self.game.vars[
                                    "forced_mouse"]
                            pygame.mouse.set_visible(
                                self.game.vars["forced_mouse"])
                    elif event.key == controls.DebugKeys.ToggleFullscreen:
                        fullscreen = not fullscreen
                        if fullscreen:
                            flags = FULLSCREEN_FLAGS
                        else:
                            flags = NORMAL_FLAGS
                        if self.enable_autoscale:
                            disable_autoscale()
                            pygame.display.flip()
                        self._screen = self.screen = pygame.display.set_mode(
                            WINDOW_SIZE, flags)
                    elif event.key == controls.DebugKeys.TakeScreenshot:
                        self.capture_screenshot = True
                    elif shift_pressed(pressed_keys):
                        # These are valid characters, and the user may need them
                        # otherwise e.g. shift+p=P may be muted and a pause may be created
                        # instead
                        if not console_enabled:
                            if event.key == pygame.K_f:
                                act_max_fps = next(max_fps_vals)
                                print("Set max FPS to", act_max_fps)
                            elif event.key == pygame.K_p:
                                pause = not pause
            if pause:
                continue
            last_state = current_state
            current_state = self.game.top_state
            is_new_state = current_state is not last_state
            force_show_dbg = is_new_state or force_show_dbg

            self.game.handle_state_changes(current_state, last_state)

            # Some events (e.g. letter keypresses) are muted by the console,
            # so we need to process it first.
            if console_enabled:
                constatus = self.console.update(events, pressed_keys,
                                                mouse_pos)
            # Run after console to make sure a binding wasn't muted
            for event in events:
                if event.type == pygame.KEYDOWN and event.key in bindings:
                    try:
                        bindings[event.key]()
                    except Exception as e:
                        print("Error executing binding {}:".format(event.key))
                        print("{}: {}".format(type(e).__name__, e))

            with TimeKeeper(events_time):
                self.game.handle_events(current_state, events, pressed_keys,
                                        mouse_pos)
                pygame.event.pump()
            self.total_events_time += events_time.value

            for func in autocalls:
                try:
                    func()
                except Exception as e:
                    print(
                        "Unhandled {} while executing autocall {}: {}".format(
                            type(e).__name__, func.__name__, str(e)))

            # Logic

            if self.profile_update_tick:
                profile = AutoProfile("updateprofile")
                profile.start()
            with TimeKeeper(update_time):
                self.game.update(current_state)
            if self.profile_update_tick:
                profile.stop()
                del profile
                self.profile_update_tick = False
            self.total_update_time += update_time.value

            # Draw

            if self.profile_draw_tick:
                profile = AutoProfile("drawprofile")
                profile.start()
            with TimeKeeper(draw_time):
                self.game.draw(current_state, self.screen)
            if self.profile_draw_tick:
                profile.stop()
                del profile
                self.profile_draw_tick = False
            self.total_draw_time += draw_time.value

            for func in autocalls_a:
                try:
                    func()
                except Exception as e:
                    print("Unhandled {} while executing after autocall {}: {}".
                          format(type(e).__name__, func.__name__, str(e)))

            if console_enabled:
                if constatus is self.console.Status.Interpret:
                    namespace = locals()
                    namespace.update(self.console_namespace_additions)
                    self.console.interpret_current(namespace)
                self.console.draw(self.screen)

            if show_dbg:
                # Pathfinding tests
                if isinstance(current_state, states.DungeonState):
                    levelfix = current_state.config["level_surface_position"]
                    tile = current_state.config["tile_size"]
                    a1 = get_player().closest_tile_index
                    p2 = (mouse_pos[0] - levelfix[0],
                          mouse_pos[1] - levelfix[1])
                    a2 = (round(p2[0] / tile), round(p2[1] / tile))
                    layout = current_state.level.layout
                    if pressed_keys[pygame.K_1]:
                        t = pygame.Surface((tile, tile)).convert_alpha()
                        t.fill((255, 255, 255, 150))
                        for col, row in pathfinding.get_path_npoints(a1, a2):
                            r = layout[row][col].rect.move(levelfix)
                            self.screen.blit(t, r)
                    if pressed_keys[pygame.K_2]:
                        t = pygame.Surface((tile, tile)).convert_alpha()
                        t.fill((0, 255, 0, 150))
                        for col, row in pathfinding.a_star_in_level(
                                a1, a2, layout):
                            r = layout[row][col].rect.move(levelfix)
                            self.screen.blit(t, r)
                if not self.game.gticks % 15 or force_show_dbg:
                    dbg_text = ""
                    if not self.game.gticks % 30 or force_show_dbg:
                        current_fps = str(round(clock.get_fps()))
                    if not self.game.gticks % 15 or force_show_dbg:
                        _et = round(events_time.value * 10**6)
                        _ut = round(update_time.value * 10**6)
                        _dt = round(draw_time.value * 10**6)
                        _st = round(screenupdate_time.value * 10**6)
                    if not minim_dbg:
                        dbg_text += dbg_template.format(fps=current_fps,
                                                        et=_et,
                                                        ut=_ut,
                                                        dt=_dt,
                                                        st=_st)
                        if isinstance(current_state, states.DungeonState):
                            if not self.game.gticks % 15 or force_show_dbg:
                                _lvl = get_level()
                                _s = len(_lvl.sprites)
                                _f = len(_lvl.friendly_sprites)
                                _h = len(_lvl.hostile_sprites)
                                _p = len(_lvl.passive_sprites)
                                _pc = len(_lvl.particles)
                                _x, _y = get_player().rect.topleft
                                _xc, _yc = get_player().rect.center
                                _xi, _yi = get_player().closest_tile_index
                                dbg_text += dungeon_dbg_template.format(s=_s,
                                                                        f=_f,
                                                                        h=_h,
                                                                        p=_p,
                                                                        pc=_pc,
                                                                        x=_x,
                                                                        y=_y,
                                                                        xc=_xc,
                                                                        yc=_yc,
                                                                        xi=_xi,
                                                                        yi=_yi)
                        if self.recording:
                            dbg_text += "(REC)\n"
                    else:
                        dbg_text += "fps: {}".format(current_fps)
                        if self.recording:
                            dbg_text += " (REC)"
                    render = get_dbg_text_render(dbg_text)
                    dbg_text_rect = render.get_rect()
                    if console_enabled:
                        p = (0, 390)
                    elif isinstance(
                            current_state,
                        (states.MainMenuState, states.SettingsState)):
                        p = (0, 0)
                    else:
                        p = (0, 100)
                    dbg_text_rect.topleft = p
                    force_show_dbg = False
                self.screen.blit(render, dbg_text_rect)

            with TimeKeeper(screenupdate_time):
                if self.enable_autoscale:
                    self._screen.fill(Color.Black)
                    scaled = imglib.scale(self.screen,
                                          self._screen.get_size(),
                                          docache=False,
                                          dolog=False)
                    self._screen.blit(scaled, (0, 0))
                pygame.display.flip()
            self.total_screenupdate_time += screenupdate_time.value

            if self.recording:
                array = pygame.surfarray.pixels3d(self.screen)
                array = cv2.transpose(array)[:, :, ::-1]
                self.record_writer.write(array)
                del array
            if self.capture_screenshot:
                self.capture_screenshot = False
                try:
                    if not os.path.exists("screenshots"):
                        os.makedirs("screenshots")
                        screenshot_folder_exists = True
                    count = len([
                        f for f in os.listdir("screenshots")
                        if f.endswith(".png")
                    ])
                    name = "screenshots/Screenshot{}.png".format(count + 1)
                    if os.path.exists(name):
                        n = random.randint(1, 999)
                        name = "screenshots/Screenshot{}-{}.png".format(
                            count + 1, n)
                    pygame.image.save(self.screen, name)
                except Exception as e:
                    print("Error capturing screenshot ({}: {})".format(
                        type(e).__name__, str(e)))
            if current_state is not None and current_state.lazy_state:
                max_fps = 60
            else:
                max_fps = act_max_fps

            clock.tick(max_fps)
            self.game.gticks += 1

        self.game.cleanup()
        pygame.quit()
        if zipopen.archive is not None:
            zipopen.archive.close()
예제 #20
0
    def cb_image_in(self, msg):
        if self.is_shutdown: return
        if self.resetting == True: return

        if self.parametersChanged:
            self.log('Parameters changed.', 'info')
            self.refresh_parameters()
            self.parametersChanged = False

        if not self.active: return

        if self.verbose:
            self.log('Received image.', 'info')
        tk = TimeKeeper(msg)

        img_original = utils.read_image(msg)
        if img_original is None:
            return
        tk.completed('decode image')

        # helps to avoid mismatch if we had a frame without stopline detections
        if self.parameters['~integration_assisted_clustering']:
            if self.verbose:
                self.log('using integrated pose to predict cluster centers',
                         'info')
            self.pose = self.integrated_pose
        stopline_poses_predicted = self.model.get_stopline_poses_reference(
            self.pose.pose)
        if self.verbose:
            # To see them in rviz: (they are also in colour on the frame)
            self.publish_pose_array(self.pub_stoplines_predicted, 'axle',
                                    stopline_poses_predicted)
        tk.completed('predict poses')

        # Filtering red
        red_mask, dbg_image = self.stopline_detector.filter_red(
            img_original, verbose=self.verbose, tk=tk)
        if self.verbose and dbg_image is not None:
            utils.publish_image(self.bridge, self.pub_red_filter, dbg_image)

        # Clustering red points
        clusters = self.stopline_detector.cluster_mask(red_mask,
                                                       self.dbscan_eps,
                                                       self.dbscan_min_samples,
                                                       tk=tk)
        # Calculate quality indicators of clusters
        cluster_qualities = list(
            [self.stopline_detector.cluster_quality(c) for c in clusters])

        # Calculate stopline poses from clusters
        stopline_poses_measured = []
        for cluster_points in clusters:  # may be empty
            pose = self.stopline_detector.calculate_pose_from_points(
                cluster_points)
            stopline_poses_measured.append(pose)
        tk.completed('stopline pose calculations')

        # Classify measured poses based on the predicted poses
        labels = self.stopline_detector.classify_poses(
            stopline_poses_measured, stopline_poses_predicted)
        tk.completed('classifying poses')

        # Correct orientation of measured poses based on the predicted poses
        stopline_poses_corrected = []
        for pose_measured, label in zip(stopline_poses_measured, labels):
            corrected = self.stopline_detector.correct_orientation(
                pose_measured, stopline_poses_predicted[label])
            stopline_poses_corrected.append(corrected)
        tk.completed('corrected poses')

        # Calculate pose estimate of axle in intersection coordinate frame
        poses_estimated = []
        for stopline_pose, label in zip(stopline_poses_corrected, labels):
            pose_estimation = self.model.get_axle_pose_from_stopline_pose(
                stopline_pose, label)
            poses_estimated.append(pose_estimation.pose)
        tk.completed('pose estimations')

        # Filter pose estimates
        best_pose_estimate = None
        if len(poses_estimated) > 0:
            self.stopline_filter.update_stopline_poses(poses_estimated,
                                                       cluster_qualities)
            best_pose_estimate = self.stopline_filter.get_best_estimate()

        if best_pose_estimate is not None:  # at least one is good enough
            if self.verbose:
                self.log('got a good pose from stoplines', 'info')

            self.pose.header.stamp = msg.header.stamp
            self.pose.pose = best_pose_estimate
            self.pub_pose(self.pose)
            if self.verbose:
                qual = self.stopline_filter.get_estimate_quality()
                self.pub_quality(qual)
            if self.integration_enabled:
                self.update_integrator_offset()

        elif self.integration_enabled and self.integrated_pose is not None:
            if self.verbose:
                self.log('fall back to integrated pose', 'info')
            self.integrated_pose.header.stamp = msg.header.stamp
            self.pub_pose(self.integrated_pose)

        if self.verbose:
            if len(stopline_poses_corrected) > 0:
                self.publish_pose_array(self.pub_stoplines_measured, 'axle',
                                        stopline_poses_corrected)
                self.publish_pose_array(self.pub_pose_estimates,
                                        'intersection', poses_estimated)

            # Feature proposal: display quality on the cluster markers
            img_debug = self.stopline_detector.draw_debug_image(
                img_original, stopline_poses_predicted,
                stopline_poses_corrected, clusters, labels)
            utils.publish_image(self.bridge, self.pub_clustering, img_debug)
            tk.completed('debug image')

        if self.parameters['~show_time_keeping']:
            self.log(tk.getall())
예제 #21
0
class Reshaper(object):
    '''
    Base Class for the PyReshaper general operations.  Specific operations
    should be defined as derived types.
    '''
    def __init__(self, specifier, serial=False, verbosity=1, once=False):
        '''
        Constructor

        @param specifier  An instance of the Specifier class, defining the
                          input specification for this reshaper operation.

        @param serial     True or False, indicating whether the operation
                          should be performed in serial (True) or parallel
                          (False).  The default is to assume parallel operation
                          (but serial will be chosen if the mpi4py cannot be
                          found when trying to initialize decomposition.

        @param verbosity  Level of printed output (stdout).  A value of 0 means
                          no output, and a higher value means more output.  The
                          default value is 1.

        @param once       True or False, indicating whether the Reshaper should
                          write all metadata to a 'once' file (separately).
        '''

        # Check types
        if (not isinstance(specifier, Specifier)):
            err_msg = "Input must be given in the form of a Specifier object"
            raise TypeError(err_msg)
        if (type(serial) is not bool):
            err_msg = "Serial indicator must be True or False."
            raise TypeError(err_msg)
        if (type(verbosity) is not int):
            err_msg = "Verbosity level must be an integer."
            raise TypeError(err_msg)
        if (type(once) is not bool):
            err_msg = "Once-file indicator must be True or False."
            raise TypeError(err_msg)

        ## Whether to write a once file
        self._use_once_file = once

        ## Internal timer data
        self._timer = TimeKeeper()

        ## Dictionary storing read/write data amounts
        self.assumed_block_size = float(4 * 1024 * 1024)
        self._byte_counts = {}

        self._timer.start('Initializing Messenger')
        ## Pointer to the messenger
        self._messenger = create_messenger(serial=serial)
        self._timer.stop('Initializing Messenger')

        # Set the verbosity in the messenger
        self._messenger.verbosity = verbosity

        # Debug output starting
        self._messenger.print_once('Initializing Reshaper', vlevel=1)

        ## Input specification
        self._specifier = specifier

        # Validate the user input data
        self._timer.start('Specifier Validation')
        self._specifier.validate()
        self._timer.stop('Specifier Validation')
        self._messenger.print_once('Specifier validated', vlevel=1)

    def convert(self, output_limit=0):
        '''
        Method to perform the Reshaper's designated operation.

        @param output_limit Limit on the number of output files to write
                            during the convert() operation.  If set to 0,
                            no limit is placed.  This limits the number of
                            output files produced by each processor in a
                            parallel run.
        '''
        pass

    def print_diagnostics(self):
        '''
        Print out timing and I/O information collected up to this point
        in the Reshaper's operation.
        '''

        # Get all totals and maxima
        my_times = self._timer.get_all_times()
        #print str(self._messenger.get_rank()) + ': all_times =', my_times
        max_times = self._messenger.max(my_times)
        #print str(self._messenger.get_rank()) + ': max_times =', max_times
        my_bytes = self._byte_counts
        #print str(self._messenger.get_rank()) + ': byte_counts =', my_bytes
        total_bytes = self._messenger.sum(my_bytes)
        #print str(self._messenger.get_rank()) + ': total_bytes =', total_bytes

        # Synchronize
        self._messenger.sync()

        # Print timing maxima
        o = self._timer.get_order()
        time_table_str = _pprint_dictionary('TIMING DATA', max_times, order=o)
        self._messenger.print_once(time_table_str, vlevel=0)

        # Convert byte count to MB
        for name in total_bytes:
            total_bytes[name] = total_bytes[name] / float(1024 * 1024)

        # Print byte count totals
        byte_count_str = _pprint_dictionary('BYTE COUNTS (MB)', total_bytes)
        self._messenger.print_once(byte_count_str, vlevel=0)