labelmaps_navigation#


class ArmscanEnv(name2volume: dict[str, ~armscan_env.volumes.volumes.ImageVolume], observation: ~armscan_env.envs.base.Observation[~armscan_env.envs.state_action.LabelmapStateAction, ~typing.Any], reward_metric: ~armscan_env.envs.base.RewardMetric[~armscan_env.envs.state_action.LabelmapStateAction] = <armscan_env.envs.rewards.LabelmapClusteringBasedReward object>, termination_criterion: ~armscan_env.envs.base.TerminationCriterion | None = <armscan_env.envs.labelmaps_navigation.LabelmapEnvTerminationCriterion object>, slice_shape: tuple[int, int] | None = None, max_episode_len: int | None = None, rotation_bounds: tuple[float, float] = (180, 180), translation_bounds: tuple[float | None, float | None] = (None, None), render_mode: ~typing.Literal['plt', 'animation'] | None = None, seed: int | None = 0, project_actions_to: ~typing.Literal['x', 'y', 'xy', 'zy'] | None = None, apply_volume_transformation: bool = False)[source]#
Parameters:
  • name2volume -- mapping from labelmap names to volumes. One of these volumes will be selected at reset.

  • observation -- defines the observation space, e.g. LabelmapSliceObservation

  • reward_metric -- defines the reward metric that will be used, e.g. LabelmapClusteringBasedReward

  • termination_criterion -- if None, no termination criterion will be used

  • slice_shape -- determines the shape of the 2D slices that will be used as observations

  • max_episode_len -- maximum number of steps in an episode

  • rotation_bounds -- bounds for the rotation angles in degrees

  • translation_bounds -- bounds for the translation in mm. If None, the bound will be computed from the volume size.

  • render_mode -- determines how the environment will be rendered. Allowed values: "plt", "animation"

  • seed -- seed for the random number generator

property action_space: Space[ndarray]#
apply_volume_transformation(volume: ~armscan_env.volumes.volumes.ImageVolume, volume_transformation_action: ~armscan_env.envs.state_action.ManipulatorAction) -> (<class 'armscan_env.volumes.volumes.TransformedVolume'>, <class 'armscan_env.envs.state_action.ManipulatorAction'>)[source]#

Apply a random transformation to the volume and to the optimal action. The transformation is a random rotation and translation. The bounds of the rotation are updated if they have already been set. The translation bounds are computed from the volume size in the 'sample_initial_state' method.

Parameters:
  • volume -- the volume to transform

  • volume_transformation_action -- the transformation action to apply to the volume

  • optimal_action -- the optimal action for the volume to transform accordingly

Returns:

the transformed volume and the transformed optimal action

close() None[source]#

After the user has finished using the environment, close contains the code necessary to "clean up" the environment.

This is critical for closing rendering windows, database or HTTP connections. Calling close on an already closed environment has no effect and won't raise an error.

compute_next_state(normalized_action_arr: ndarray | ManipulatorAction) LabelmapStateAction[source]#
compute_slice_shape(volume: ImageVolume | None) None[source]#

Compute the shape of the 2D slices that will be used as observations.

property cur_labelmap_name: str | None#
property cur_labelmap_volume: ImageVolume | TransformedVolume | None#
property cur_optimal_action: ManipulatorAction | None#
get_camera() Camera[source]#
get_cur_animation() ArtistAnimation[source]#
get_cur_animation_as_html() HTML[source]#
get_cur_full_normalized_action_arr() ndarray[source]#
get_cur_manipulator_action() ManipulatorAction[source]#
get_cur_state_plot(create_new_figure: bool = True, title: str = 'Labelmap slice') Figure | None[source]#

Retrieve a figure visualizing the current state of the environment.

Parameters:

create_new_figure -- if True, a new figure will be created. Otherwise, a single figure will be used in subsequent calls to this method. False may be useful for animations.

get_figure_axes() tuple[Figure | None, tuple[Axes, Axes, Axes, Axes, Axes] | None][source]#
get_full_action_array_from_projected_action(normalized_action_arr: ndarray) ndarray[source]#

Converts a (potentially projected and) normalized action array to a full action array.

If project_actions_to is not None, the normalized_action_arr is assumed to be a projection of the correct dimension.

get_full_optimal_action_array() ndarray[source]#
get_manipulator_action_from_normalized_action(normalized_action_arr: ndarray) ManipulatorAction[source]#

Converts a (potentially projected and) normalized action array to a ManipulatorAction.

Passing a full action array is also supported, even if project_actions_to is not None. If normalized_action_arr is of a lower len and project_actions_to is not None, the normalized_action_arr is assumed to be a projection of the correct dimension.

get_optimal_action() ManipulatorAction[source]#
get_optimal_action_array() ndarray[source]#
metadata: ClassVar[dict] = {'render_fps': 10, 'render_modes': ['plt', 'animation', None]}#
property project_actions_to: Literal['x', 'y', 'xy', 'zy'] | None#
render(title: str = 'Labelmap slice') Figure | Camera | None[source]#

Compute the render frames as specified by render_mode during the initialization of the environment.

The environment's metadata render modes (env.metadata["render_modes"]) should contain the possible ways to implement the render modes. In addition, list versions for most render modes is achieved through gymnasium.make which automatically applies a wrapper to collect rendered frames.

Note:

As the render_mode is known during __init__, the objects used to render the environment state should be initialised in __init__.

By convention, if the render_mode is:

  • None (default): no render is computed.

  • "human": The environment is continuously rendered in the current display or terminal, usually for human consumption. This rendering should occur during step() and render() doesn't need to be called. Returns None.

  • "rgb_array": Return a single frame representing the current state of the environment. A frame is a np.ndarray with shape (x, y, 3) representing RGB values for an x-by-y pixel image.

  • "ansi": Return a strings (str) or StringIO.StringIO containing a terminal-style text representation for each time step. The text can include newlines and ANSI escape sequences (e.g. for colors).

  • "rgb_array_list" and "ansi_list": List based version of render modes are possible (except Human) through the wrapper, gymnasium.wrappers.RenderCollection that is automatically applied during gymnasium.make(..., render_mode="rgb_array_list"). The frames collected are popped after render() is called or reset().

Note:

Make sure that your class's metadata "render_modes" key includes the list of supported modes.

Changed in version 0.25.0: The render function was changed to no longer accept parameters, rather these parameters should be specified in the environment initialised, i.e., gymnasium.make("CartPole-v1", render_mode="human")

reset(reset_render: bool = True, reset_translation_bounds: bool = True, reset_slice_shape: bool = False, **kwargs: Any) tuple[ObsType, dict[str, Any]][source]#

Resets the environment to an initial internal state, returning an initial observation and info.

This method generates a new starting state often with some randomness to ensure that the agent explores the state space and learns a generalised policy about the environment. This randomness can be controlled with the seed parameter otherwise if the environment already has a random number generator and reset() is called with seed=None, the RNG is not reset.

Therefore, reset() should (in the typical use case) be called with a seed right after initialization and then never again.

For Custom environments, the first line of reset() should be super().reset(seed=seed) which implements the seeding correctly.

Changed in version v0.25: The return_info parameter was removed and now info is expected to be returned.

Args:
seed (optional int): The seed that is used to initialize the environment's PRNG (np_random) and

the read-only attribute np_random_seed. If the environment does not already have a PRNG and seed=None (the default option) is passed, a seed will be chosen from some source of entropy (e.g. timestamp or /dev/urandom). However, if the environment already has a PRNG and seed=None is passed, the PRNG will not be reset and the env's np_random_seed will not be altered. If you pass an integer, the PRNG will be reset even if it already exists. Usually, you want to pass an integer right after the environment has been initialized and then never again. Please refer to the minimal example above to see this paradigm in action.

options (optional dict): Additional information to specify how the environment is reset (optional,

depending on the specific environment)

Returns:
observation (ObsType): Observation of the initial state. This will be an element of observation_space

(typically a numpy array) and is analogous to the observation returned by step().

info (dictionary): This dictionary contains auxiliary information complementing observation. It should be analogous to

the info returned by step().

reset_render() None[source]#
sample_initial_state() LabelmapStateAction[source]#
step(action: ndarray | ManipulatorAction) tuple[Observation[LabelmapStateAction, Any], float, bool, bool, dict[str, Any]][source]#

Step through the environment to navigate to the next state.

step_to_optimal_state() tuple[Observation[LabelmapStateAction, Any], float, bool, bool, dict[str, Any]][source]#
class LabelmapEnvTerminationCriterion(min_reward_threshold: float = -0.05)[source]#
should_terminate(env: ArmscanEnv) bool[source]#