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._data = [] 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)
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:2, 0:8] self._gs_action_output = self._gs[2:3, 0:4] self._gs_status_output = self._gs[3:4, 0:4] self._gs_cost_plotter = self._gs[2:4, 4:8] self._gs_algthm_output = self._gs[4:8, 0:8] if config['image_on']: self._gs_traj_visualizer = self._gs[8:16, 0:4] self._gs_image_visualizer = self._gs[8:16, 4:8] else: self._gs_traj_visualizer = self._gs[8: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', min_itr=self._hyperparams.get( 'iterations', 10)) 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()