Reference for DiscreteFieldGrapher
This utility uses matplotlib to graph vehicles in descrete positions as defined by an instance of FieldDiscretizer
WARNING: This class is EXTREMELY ineffecient and should not be used in a loop which timing is important. Its primary use is for debuging.
You can see a example of how to use this visualizer with the FieldDiscretizer here.
Source code in mivp_agent/aquaticus/field.py
class DiscreteFieldGrapher:
'''
This utility uses matplotlib to graph vehicles in descrete positions as defined by an instance of [`FieldDiscretizer`][mivp_agent.aquaticus.field.FieldDiscretizer]
**WARNING:** This class is *EXTREMELY* ineffecient and should not be used in a loop which timing is important. Its primary use is for debuging.
You can see a example of how to use this visualizer with the FieldDiscretizer [here](https://github.com/mivp-agent/moos-ivp-agent/blob/main/examples/DiscreteField/run.py).
'''
def __init__(self, discretizer):
'''
Args:
discretizer (FieldDiscretizer): Used to plot discrete points and during vehicle initalization.
'''
assert isinstance(discretizer, FieldDiscretizer), 'discretizer is of wrong type'
# Store discretizer
self._discretizer = discretizer
# Construct field plot
plt.ion()
self._field_fig, self._field_ax = construct_field_figure()
# Add grid points to field graph
for p in self._discretizer._point_idx_map:
# Ignore the entry used for out of field points
if p is None:
continue
self._field_ax.plot([p[0]], [p[1]], marker='.', markersize=3, color='dimgray')
# Draw for first time
self._field_fig.canvas.draw()
# Create data structure for storing vehicle's locations
self._vehicles = {}
def init_vehicle(self, name, color, start_pos=FIELD_OUT_BOUNDS_POINT, plot=True):
'''
Initalizes a vehicle specified by `name` in the DiscreteFieldGrapher class and it's matplotlib plot. To update the vehicles initalized by this method see
[`update_vehicle()`][mivp_agent.aquaticus.field.DiscreteFieldGrapher.update_vehicle].
Args:
name (str): The name (or key) to list the vehicle under.
color (str): A matplotlib [named color](https://matplotlib.org/stable/gallery/color/named_colors.html).
start_pos (tuple): A tuple where containing the x / y position to initally render the vehicle.
plot (bool): A boolean indicating if the the plot should be redrawn after vehicle initalization.
Raises:
RuntimeError: If the specified `name` has already been initalized.
'''
if name in self._vehicles:
raise RuntimeError(f'FieldGrapher alread has vehicle named "{name}"')
if start_pos != FIELD_OUT_BOUNDS_POINT:
start_pos = self._discretizer.to_discrete_point(
start_pos[0],
start_pos[1]
)
self._vehicles[name] = {
'circle': plt.Circle(start_pos, 2, color=color),
'label': self._field_ax.text(start_pos[0] + 3, start_pos[1]+3, name),
'position': start_pos
}
self._field_ax.add_artist(self._vehicles[name]['circle'])
if plot:
self._plot()
def update_vehicle(self, name, discrete_position, plot=True):
'''
Used to update the position of a vehicle which has been previously initalized via [`init_vehicle()`][mivp_agent.aquaticus.field.DiscreteFieldGrapher.init_vehicle]
Args:
name (str): The name (or key) to list the vehicle under.
discrete_position (tuple): A tuple where containing the **DISCRETE** x / y position to render the vehicle at. This method **will not** pass the point through the discretizer.
plot (bool): A boolean indicating if the the plot should be redrawn after vehicle update.
Raises:
RuntimeError: If the specified `name` has not been initalized previously.
'''
if name not in self._vehicles:
raise RuntimeError(f'FieldGrapher cannot find vehicle with name "{name}"')
if discrete_position is None:
discrete_position = FIELD_OUT_BOUNDS_POINT
# Remove old label position
self._vehicles[name]['label'].set_visible(False)
# Update graphic
self._vehicles[name]['circle'].center = discrete_position
self._vehicles[name]['label'] = self._field_ax.text(discrete_position[0]+3, discrete_position[1]+3, name)
self._vehicles[name]['position'] = discrete_position
if plot:
self._plot()
def _plot(self):
# Draw the vehicles
for v in self._vehicles:
self._field_ax.draw_artist(self._vehicles[v]['circle'])
self._field_fig.canvas.flush_events()
__init__(self, discretizer)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
discretizer |
FieldDiscretizer |
Used to plot discrete points and during vehicle initalization. |
required |
Source code in mivp_agent/aquaticus/field.py
def __init__(self, discretizer):
'''
Args:
discretizer (FieldDiscretizer): Used to plot discrete points and during vehicle initalization.
'''
assert isinstance(discretizer, FieldDiscretizer), 'discretizer is of wrong type'
# Store discretizer
self._discretizer = discretizer
# Construct field plot
plt.ion()
self._field_fig, self._field_ax = construct_field_figure()
# Add grid points to field graph
for p in self._discretizer._point_idx_map:
# Ignore the entry used for out of field points
if p is None:
continue
self._field_ax.plot([p[0]], [p[1]], marker='.', markersize=3, color='dimgray')
# Draw for first time
self._field_fig.canvas.draw()
# Create data structure for storing vehicle's locations
self._vehicles = {}
init_vehicle(self, name, color, start_pos=(30, 10), plot=True)
Initalizes a vehicle specified by name
in the DiscreteFieldGrapher class and it's matplotlib plot. To update the vehicles initalized by this method see
update_vehicle()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str |
The name (or key) to list the vehicle under. |
required |
color |
str |
A matplotlib named color. |
required |
start_pos |
tuple |
A tuple where containing the x / y position to initally render the vehicle. |
(30, 10) |
plot |
bool |
A boolean indicating if the the plot should be redrawn after vehicle initalization. |
True |
Exceptions:
Type | Description |
---|---|
RuntimeError |
If the specified |
Source code in mivp_agent/aquaticus/field.py
def init_vehicle(self, name, color, start_pos=FIELD_OUT_BOUNDS_POINT, plot=True):
'''
Initalizes a vehicle specified by `name` in the DiscreteFieldGrapher class and it's matplotlib plot. To update the vehicles initalized by this method see
[`update_vehicle()`][mivp_agent.aquaticus.field.DiscreteFieldGrapher.update_vehicle].
Args:
name (str): The name (or key) to list the vehicle under.
color (str): A matplotlib [named color](https://matplotlib.org/stable/gallery/color/named_colors.html).
start_pos (tuple): A tuple where containing the x / y position to initally render the vehicle.
plot (bool): A boolean indicating if the the plot should be redrawn after vehicle initalization.
Raises:
RuntimeError: If the specified `name` has already been initalized.
'''
if name in self._vehicles:
raise RuntimeError(f'FieldGrapher alread has vehicle named "{name}"')
if start_pos != FIELD_OUT_BOUNDS_POINT:
start_pos = self._discretizer.to_discrete_point(
start_pos[0],
start_pos[1]
)
self._vehicles[name] = {
'circle': plt.Circle(start_pos, 2, color=color),
'label': self._field_ax.text(start_pos[0] + 3, start_pos[1]+3, name),
'position': start_pos
}
self._field_ax.add_artist(self._vehicles[name]['circle'])
if plot:
self._plot()
update_vehicle(self, name, discrete_position, plot=True)
Used to update the position of a vehicle which has been previously initalized via init_vehicle()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str |
The name (or key) to list the vehicle under. |
required |
discrete_position |
tuple |
A tuple where containing the DISCRETE x / y position to render the vehicle at. This method will not pass the point through the discretizer. |
required |
plot |
bool |
A boolean indicating if the the plot should be redrawn after vehicle update. |
True |
Exceptions:
Type | Description |
---|---|
RuntimeError |
If the specified |
Source code in mivp_agent/aquaticus/field.py
def update_vehicle(self, name, discrete_position, plot=True):
'''
Used to update the position of a vehicle which has been previously initalized via [`init_vehicle()`][mivp_agent.aquaticus.field.DiscreteFieldGrapher.init_vehicle]
Args:
name (str): The name (or key) to list the vehicle under.
discrete_position (tuple): A tuple where containing the **DISCRETE** x / y position to render the vehicle at. This method **will not** pass the point through the discretizer.
plot (bool): A boolean indicating if the the plot should be redrawn after vehicle update.
Raises:
RuntimeError: If the specified `name` has not been initalized previously.
'''
if name not in self._vehicles:
raise RuntimeError(f'FieldGrapher cannot find vehicle with name "{name}"')
if discrete_position is None:
discrete_position = FIELD_OUT_BOUNDS_POINT
# Remove old label position
self._vehicles[name]['label'].set_visible(False)
# Update graphic
self._vehicles[name]['circle'].center = discrete_position
self._vehicles[name]['label'] = self._field_ax.text(discrete_position[0]+3, discrete_position[1]+3, name)
self._vehicles[name]['position'] = discrete_position
if plot:
self._plot()