def __init__(self, hyperparams): self._hyperparams = hyperparams self._log_filename = self._hyperparams['log_filename'] if 'target_filename' in self._hyperparams: self._target_filename = self._hyperparams['target_filename'] else: self._target_filename = None # GPS Training Status. self.mode = config[ 'initial_mode'] # Modes: run, wait, end, request, process. self.request = None # Requests: stop, reset, go, fail, None. self.err_msg = None self._colors = { 'run': 'cyan', 'wait': 'orange', 'end': 'red', 'stop': 'red', 'reset': 'yellow', 'go': 'green', 'fail': 'magenta', } self._first_update = True # Actions. actions_arr = [ Action('stop', 'stop', self.request_stop, axis_pos=0), Action('reset', 'reset', self.request_reset, axis_pos=1), Action('go', 'go', self.request_go, axis_pos=2), Action('fail', 'fail', self.request_fail, axis_pos=3), ] # Setup figure. plt.ion() plt.rcParams['toolbar'] = 'None' for key in plt.rcParams: if key.startswith('keymap.'): plt.rcParams[key] = '' self._fig = plt.figure(figsize=config['figsize']) self._fig.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0, hspace=0) # Assign GUI component locations. self._gs = gridspec.GridSpec(16, 8) self._gs_action_panel = self._gs[0:1, 0:8] self._gs_action_output = self._gs[1:2, 0:4] self._gs_status_output = self._gs[2:3, 0:4] self._gs_cost_plotter = self._gs[1:3, 4:8] self._gs_gt_cost_plotter = self._gs[4:6, 4:8] self._gs_algthm_output = self._gs[3:9, 0:4] if config['image_on']: self._gs_traj_visualizer = self._gs[9:16, 0:4] self._gs_image_visualizer = self._gs[9:16, 4:8] else: self._gs_traj_visualizer = self._gs[9:16, 0:8] # Create GUI components. self._action_panel = ActionPanel(self._fig, self._gs_action_panel, 1, 4, actions_arr) self._action_output = Textbox(self._fig, self._gs_action_output, border_on=True) self._status_output = Textbox(self._fig, self._gs_status_output, border_on=False) self._algthm_output = Textbox( self._fig, self._gs_algthm_output, max_display_size=config['algthm_output_max_display_size'], log_filename=self._log_filename, fontsize=config['algthm_output_fontsize'], font_family='monospace') self._cost_plotter = MeanPlotter(self._fig, self._gs_cost_plotter, color='blue', label='mean cost') self._gt_cost_plotter = MeanPlotter(self._fig, self._gs_gt_cost_plotter, color='red', label='ground truth cost') self._traj_visualizer = Plotter3D( self._fig, self._gs_traj_visualizer, num_plots=self._hyperparams['conditions']) if config['image_on']: self._image_visualizer = ImageVisualizer( self._fig, self._gs_image_visualizer, cropsize=config['image_size'], rostopic=config['image_topic'], show_overlay_buttons=True) # Setup GUI components. self._algthm_output.log_text('\n') self.set_output_text(self._hyperparams['info']) if config['initial_mode'] == 'run': self.run_mode() else: self.wait_mode() # Setup 3D Trajectory Visualizer plot titles and legends for m in range(self._hyperparams['conditions']): self._traj_visualizer.set_title(m, 'Condition %d' % (m)) self._traj_visualizer.add_legend(linestyle='-', marker='None', color='green', label='Trajectory Samples') self._traj_visualizer.add_legend(linestyle='-', marker='None', color='blue', label='Policy Samples') self._traj_visualizer.add_legend(linestyle='None', marker='x', color=(0.5, 0, 0), label='LG Controller Means') self._traj_visualizer.add_legend(linestyle='-', marker='None', color='red', label='LG Controller Distributions') self._fig.canvas.draw() # Display calculating thread def display_calculating(delay, run_event): while True: if not run_event.is_set(): run_event.wait() if run_event.is_set(): self.set_status_text('Calculating.') time.sleep(delay) if run_event.is_set(): self.set_status_text('Calculating..') time.sleep(delay) if run_event.is_set(): self.set_status_text('Calculating...') time.sleep(delay) self._calculating_run = threading.Event() self._calculating_thread = threading.Thread( target=display_calculating, args=(1, self._calculating_run)) self._calculating_thread.daemon = True self._calculating_thread.start()
def __init__(self, hyperparams, agent): self._hyperparams = hyperparams self._agent = agent self._log_filename = self._hyperparams['log_filename'] self._target_filename = self._hyperparams['target_filename'] self._num_targets = config['num_targets'] self._actuator_types = config['actuator_types'] self._actuator_names = config['actuator_names'] self._num_actuators = len(self._actuator_types) # Target Setup Status. self._target_number = 0 self._actuator_number = 0 self._actuator_type = self._actuator_types[self._actuator_number] self._actuator_name = self._actuator_names[self._actuator_number] self._initial_position = ('unknown', 'unknown', 'unknown') self._target_position = ('unknown', 'unknown', 'unknown') self._initial_image = None self._target_image = None self._mannequin_mode = False self._mm_process = None # Actions. actions_arr = [ Action('ptn', 'prev_target_number', self.prev_target_number, axis_pos=0), Action('ntn', 'next_target_number', self.next_target_number, axis_pos=1), Action('pat', 'prev_actuator_type', self.prev_actuator_type, axis_pos=2), Action('nat', 'next_actuator_type', self.next_actuator_type, axis_pos=3), Action('sip', 'set_initial_position', self.set_initial_position, axis_pos=4), Action('stp', 'set_target_position', self.set_target_position, axis_pos=5), Action('sii', 'set_initial_image', self.set_initial_image, axis_pos=6), Action('sti', 'set_target_image', self.set_target_image, axis_pos=7), Action('mti', 'move_to_initial', self.move_to_initial, axis_pos=8), Action('mtt', 'move_to_target', self.move_to_target, axis_pos=9), Action('rc', 'relax_controller', self.relax_controller, axis_pos=10), Action('mm', 'mannequin_mode', self.mannequin_mode, axis_pos=11), ] # Setup figure. plt.ion() plt.rcParams['toolbar'] = 'None' for key in plt.rcParams: if key.startswith('keymap.'): plt.rcParams[key] = '' self._fig = plt.figure(figsize=config['figsize']) self._fig.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0, hspace=0) # Assign GUI component locations. self._gs = gridspec.GridSpec(4, 4) self._gs_action_panel = self._gs[0:1, 0:4] if config['image_on']: self._gs_target_output = self._gs[1:3, 0:2] self._gs_initial_image_visualizer = self._gs[3:4, 0:1] self._gs_target_image_visualizer = self._gs[3:4, 1:2] self._gs_action_output = self._gs[1:2, 2:4] self._gs_image_visualizer = self._gs[2:4, 2:4] else: self._gs_target_output = self._gs[1:4, 0:2] self._gs_action_output = self._gs[1:4, 2:4] # Create GUI components. self._action_panel = ActionPanel(self._fig, self._gs_action_panel, 3, 4, actions_arr) self._target_output = Textbox( self._fig, self._gs_target_output, log_filename=self._log_filename, fontsize=config['target_output_fontsize']) self._action_output = Textbox(self._fig, self._gs_action_output) if config['image_on']: self._initial_image_visualizer = ImageVisualizer( self._fig, self._gs_initial_image_visualizer) self._target_image_visualizer = ImageVisualizer( self._fig, self._gs_target_image_visualizer) self._image_visualizer = ImageVisualizer( self._fig, self._gs_image_visualizer, cropsize=config['image_size'], rostopic=config['image_topic'], show_overlay_buttons=True) # Setup GUI components. self.reload_positions() self.update_target_text() self.set_action_text('Press an action to begin.') self.set_action_bgcolor('white') self._fig.canvas.draw()
def __init__(self, hyperparams, agent): self._agent = agent self._hyperparams = hyperparams self._log_filename = self._hyperparams['log_filename'] if 'target_filename' in self._hyperparams: self._target_filename = self._hyperparams['target_filename'] else: self._target_filename = None # GPS Training Status. self.mode = config['initial_mode'] # Modes: run, wait, end, request, process. self.request = None # Requests: stop, reset, go, fail, None. self.err_msg = None self._colors = { 'run': 'cyan', 'wait': 'orange', 'end': 'red', } self._actuator_types = config['actuator_types'] self._actuator_names = config['actuator_names'] self._first_update = True self._actuator_number = 0 self._actuator_type = self._actuator_types[self._actuator_number] self._initial_position = ('unknown', 'unknown', 'unknown') self._target_position = ('unknown', 'unknown', 'unknown') # Actions. actions_arr = [ Action('stop', 'stop', self.request_stop, axis_pos=0), Action('reset', 'reset', self.request_reset, axis_pos=1), Action('GCM go', 'go', self.request_go, axis_pos=2), Action('transfer learning', 'transfer_learning', self.request_tl, axis_pos=3), Action('set initial state', 'initstate', self.request_init_state, axis_pos=4), Action('set goal state', 'goalstate', self.request_goal_state, axis_pos=5), Action('test transfer learning', 'test_tl', self.request_test_tl, axis_pos=6), Action('generalize', 'generalize', self.request_generalize, axis_pos=7), Action('mti', 'move_to_initial', self.move_to_initial, axis_pos=8), Action('mtt', 'move_to_target', self.move_to_target, axis_pos=9), Action('rc', 'relax_controller', self.relax_controller, axis_pos=10), ] # Setup figure. plt.ion() plt.rcParams['toolbar'] = 'None' for key in plt.rcParams: if key.startswith('keymap.'): plt.rcParams[key] = '' self._fig = plt.figure(figsize=config['figsize']) self._fig.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0, hspace=0) # Assign GUI component locations. self._gs = gridspec.GridSpec(18, 8) self._gs_action_panel = self._gs[0:4, 0:8] self._gs_action_output = self._gs[4:5, 0:4] self._gs_status_output = self._gs[5:6, 0:4] self._gs_cost_plotter = self._gs[4:10, 4:8] self._gs_algthm_output = self._gs[6:10, 0:4] self._gs_traj_visualizer = self._gs[10:18, 0:8] # Create GUI components. self._action_panel = ActionPanel(self._fig, self._gs_action_panel, 3, 4, actions_arr) self._action_output = Textbox(self._fig, self._gs_action_output, border_on=True) self._status_output = Textbox(self._fig, self._gs_status_output, border_on=False) self._algthm_output = Textbox(self._fig, self._gs_algthm_output, max_display_size=config['algthm_output_max_display_size'], log_filename=self._log_filename, fontsize=config['algthm_output_fontsize'], font_family='monospace') self._cost_plotter = MeanPlotter(self._fig, self._gs_cost_plotter, color='blue', label='mean cost') self._traj_visualizer = Plotter3D(self._fig, self._gs_traj_visualizer, num_plots=self._hyperparams['conditions']) # Setup GUI components. self._algthm_output.log_text('\n') self.set_output_text(self._hyperparams['info']) if config['initial_mode'] == 'run': self.run_mode() else: self.wait_mode() # Setup 3D Trajectory Visualizer plot titles and legends for m in range(self._hyperparams['conditions']): self._traj_visualizer.set_title(m, 'Condition %d' % (m)) self._traj_visualizer.add_legend(linestyle='-', marker='None', color='green', label='Trajectory Samples') self._traj_visualizer.add_legend(linestyle='-', marker='None', color='blue', label='Policy Samples') self._traj_visualizer.add_legend(linestyle='None', marker='x', color=(0.5, 0, 0), label='LG Controller Means') self._traj_visualizer.add_legend(linestyle='-', marker='None', color='red', label='LG Controller Distributions') self._fig.canvas.draw() # Display calculating thread def display_calculating(delay, run_event): while True: if not run_event.is_set(): run_event.wait() if run_event.is_set(): self.set_status_text('Calculating.') time.sleep(delay) if run_event.is_set(): self.set_status_text('Calculating..') time.sleep(delay) if run_event.is_set(): self.set_status_text('Calculating...') time.sleep(delay) self._calculating_run = threading.Event() self._calculating_thread = threading.Thread(target=display_calculating, args=(1, self._calculating_run)) self._calculating_thread.daemon = True self._calculating_thread.start()
def __init__(self, fig, gs, cropsize=None, rostopic=None, show_overlay_buttons=False): """ If rostopic is given to this constructor, then the image visualizer will automatically update with rostopic image. Else, the update method must be manually called to supply images. """ # Real-time image self._t = 0 self._current_image = None self._crop_size = cropsize # Image overlay self._initial_image_overlay_on = False self._target_image_overlay_on = False self._initial_image = None self._initial_alpha = None self._target_image = None self._target_alpha = None self._default_image = np.zeros((1, 1, 3)) self._default_alpha = 0.0 # Actions actions_arr = [ Action('oii', 'overlay_initial_image', self.toggle_initial_image_overlay, axis_pos=0), Action('oti', 'overlay_target_image', self.toggle_target_image_overlay, axis_pos=1), ] # GUI Components self._fig = fig self._gs = gridspec.GridSpecFromSubplotSpec(8, 1, subplot_spec=gs) self._gs_action_panel = self._gs[0:1, 0] self._gs_image_axis = self._gs[1:8, 0] if show_overlay_buttons: self._action_panel = ActionPanel(self._fig, self._gs_action_panel, 1, 2, actions_arr) self._ax_image = plt.subplot(self._gs_image_axis) self._ax_image.set_axis_off() self._plot = self._ax_image.imshow(self._default_image) self._overlay_plot_initial = self._ax_image.imshow( self._default_image, alpha=self._default_alpha) self._overlay_plot_target = self._ax_image.imshow( self._default_image, alpha=self._default_alpha) self._fig.canvas.draw() self._fig.canvas.flush_events() # Fixes bug with Qt4Agg backend # ROS subscriber for PS3 controller if rostopic and ROS_ENABLED: rospy.Subscriber(rostopic, Image, self.update_ros, queue_size=1, buff_size=2**24)
demo_thread = threading.Thread(target=demo_func) demo_thread.daemon = True demo_thread.start() # Initialize Figure fig = plt.figure() gs = gridspec.GridSpec(2, 3) # Action Panel number = 0 def plus_1(event=None): global number; number += 1; print(number) def plus_2(event=None): global number; number += 2; print(number) def mult_4(event=None): global number; number *= 4; print(number) actions_arr = [ Action('plus1', 'plus1', plus_1, axis_pos=0, keyboard_binding='1', ps3_binding=None), Action('plus2', 'plus2', plus_2, axis_pos=1, keyboard_binding='2', ps3_binding=None), Action('print', 'print', mult_4, axis_pos=2, keyboard_binding='4', ps3_binding=None), ] action_panel = ActionPanel(fig, gs[0], 3, 1, actions_arr) # Textbox def demo_textbox(): max_i = 20 for i in range(max_i): textbox.append_text(str(i)) c = 0.5 + 0.5*i/max_i textbox.set_bgcolor((c, c, c)) time.sleep(1) textbox = Textbox(fig, gs[1], max_display_size=10, log_filename=None)