score_widget.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2018-2019, Locus Robotics
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of the copyright holder nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import rospy
34 
35 from .table_widget import TableWidget
36 
37 
39  """This widget displays the critic scores for an arbitrary number of trajectories in tabular format.
40 
41  The main data is the dwb_msgs/CriticScore[] scores part for each selected trajectory in a LocalPlanEvaluation.
42  Each critic is a different row, and each trajectory is a column.
43 
44  Each critic has two columns for row-headers that display the critic's name and scale.
45 
46  Each trajectory also has column-headers that display the trajectory's velocity as well as
47  column-footer(s) that show the trajectory's total score. This is left extensible for others to modify.
48  """
49 
50  def __init__(self, parent):
51  TableWidget.__init__(self, self.getRowHeaders())
52  self.evaluation = None
53  self.selected = []
54  self.row_mapping = {} # Save the index of each critic
55 
56  # Allow for potential overwrites of headers/footers
57  self.row_headers = self.getRowHeaders()
58  self.col_headers = self.getColHeaders()
59  self.col_footers = self.getColFooters()
60 
61  def getRowHeaders(self):
62  # Name has to be one of the headers
63  return ['Name', 'Scale']
64 
65  def getColHeaders(self):
66  return ['x velocity', 'theta velocity']
67 
68  def getColFooters(self):
69  return ['Total']
70 
71  def getRowAttribute(self, score, attribute_name):
72  if attribute_name == 'Scale':
73  return '%.5f' % score.scale
74 
75  def getColAttribute(self, twist, attribute_name):
76  if attribute_name == 'x velocity':
77  return '%.2f' % twist.traj.velocity.x
78  elif attribute_name == 'theta velocity':
79  return '%.2f' % twist.traj.velocity.theta
80  elif attribute_name == 'Total':
81  return '%.2f' % twist.total
82 
83  def setEvaluation(self, evaluation):
84  self.selected = []
85  if evaluation is None:
86  self.table.setRowCount(0)
87  return
88  self.evaluation = evaluation
89  best = evaluation.twists[evaluation.best_index]
90 
91  # One row for each critic, column header and column footer
92  self.table.setRowCount(len(best.scores) + len(self.col_headers) + len(self.col_footers))
93  self.table.resize(self.size())
94 
95  # Populate the Row Headers
96  for index, row_header in enumerate(self.row_headers):
97  if row_header == 'Name':
98  for i, name in enumerate(self.col_headers):
99  self.setValue(i, index, name)
100  row_offset = len(self.col_headers)
101  for i, score in enumerate(best.scores):
102  self.setValue(i + row_offset, index, score.name)
103  self.row_mapping[score.name] = i + row_offset
104 
105  row_offset = len(self.col_headers) + len(best.scores)
106  for i, name in enumerate(self.col_footers):
107  self.setValue(i + row_offset, index, name)
108  else:
109  # Populate row headers for each critic
110  row_offset = len(self.col_headers)
111  for i, score in enumerate(best.scores):
112  self.setValue(i + row_offset, index, self.getRowAttribute(score, row_header))
113 
114  self.table.resizeColumnToContents(index)
115 
116  self.update()
117 
118  def updateColumn(self, twist, column, name, color):
119  self.setColumnHeader(column, name)
120 
121  # Headers
122  for i, name in enumerate(self.col_headers):
123  self.setValue(i, column, self.getColAttribute(twist, name))
124 
125  # Main Data
126  row_offset = len(self.col_headers)
127  for score in twist.scores:
128  if score.name in self.row_mapping:
129  self.setValue(self.row_mapping[score.name], column, '%.2f' % score.raw_score)
130  else:
131  rospy.logwarn('Unknown row name: {}'.format(score.name))
132 
133  # Footers
134  row_offset = len(self.col_headers) + len(self.row_mapping)
135  for i, name in enumerate(self.col_footers):
136  self.setValue(i + row_offset, column, self.getColAttribute(twist, name))
137 
138  self.setColumnColor(column, color)
139  self.table.resizeColumnToContents(column)
140 
141  def getColumnName(self, index):
142  if index == self.evaluation.best_index:
143  return 'Best'
144 
145  def setSelected(self, selected):
146  n_row_headers = len(self.row_headers)
147  # One column for each row header and selected trajectory
148  self.table.setColumnCount(n_row_headers + len(selected))
149  for col_offset, index in enumerate(selected):
150  col_name = self.getColumnName(index) or ''
151  self.updateColumn(self.evaluation.twists[index], n_row_headers + col_offset, col_name, selected[index])
def getRowAttribute(self, score, attribute_name)
Definition: score_widget.py:71
def setColumnColor(self, column, color)
Definition: table_widget.py:80
def updateColumn(self, twist, column, name, color)
def getColAttribute(self, twist, attribute_name)
Definition: score_widget.py:75
def setColumnHeader(self, column, value)
Definition: table_widget.py:65


rqt_dwb_plugin
Author(s):
autogenerated on Mon Feb 28 2022 23:33:43