cambrian.utils

This module defines the Cambrian utilities.

Submodules

Classes

MjCambrianActuator

Helper class which stores information about a Mujoco actuator.

MjCambrianJoint

Helper class which stores information about a Mujoco joint.

MjCambrianGeometry

Helper class which stores information about a Mujoco geometry

Functions

evaluate_policy(env, model, num_runs, *[, ...])

Evaluate a policy.

save_data(data, outdir, pickle_file)

Save the parsed data to a pickle file.

try_load_pickle(folder, pickle_file)

Try to load the data from the pickle file.

generate_sequence_from_range(range, num[, endpoint])

Generate a sequence of numbers from a range. If num is 1, the average of the

setattrs_temporary(*args)

Temporarily set attributes of an object.

make_odd(num)

Make a number odd by adding 1 if it is even. If num is a float, it is cast to

round_half_up(n)

Round a number to the nearest integer, rounding half up.

safe_index(arr, value, *[, default])

Safely get the index of a value in a list. If the value is not in the list, None

pickle_unpickleable_object(source)

Return a reduce tuple: (unpickle_callable, (args,))

unpickle_unpickleable_object(cls, attributes)

This will be called automatically by Python when unpickling.

agent_selected(agent, agents)

Check if the agent is selected.

Package Contents

evaluate_policy(env, model, num_runs, *, record_kwargs=None, step_callback=lambda _: ..., done_callback=lambda _: ...)[source]

Evaluate a policy.

Parameters:
  • env (gym.Env) – The environment to evaluate the policy on. Assumed to be a VecEnv wrapper around a MjCambrianEnv.

  • model (MjCambrianModel) – The model to evaluate.

  • num_runs (int) – The number of runs to evaluate the policy on.

Keyword Arguments:
  • record_kwargs (Dict[str, Any]) – The keyword arguments to pass to the save method of the environment. If None, the environment will not be recorded.

  • step_callback (Callable[[], bool]) – The callback function to call at each step. If the function returns False, the evaluation will stop.

  • done_callback (Callable[[int], bool]) – The callback function to call when a run is done. If the function returns False, the evaluation will stop.

Returns:

float – The cumulative reward of the evaluation.

save_data(data, outdir, pickle_file)[source]

Save the parsed data to a pickle file.

try_load_pickle(folder, pickle_file)[source]

Try to load the data from the pickle file.

generate_sequence_from_range(range, num, endpoint=True)[source]

Generate a sequence of numbers from a range. If num is 1, the average of the range is returned. Otherwise, a sequence of numbers is generated using np.linspace.

Parameters:
  • range (Tuple[float, float]) – The range of the sequence.

  • num (int) – The number of elements in the sequence.

Keyword Arguments:

endpoint (bool) – Whether to include the endpoint in the sequence.

setattrs_temporary(*args)[source]

Temporarily set attributes of an object.

make_odd(num)[source]

Make a number odd by adding 1 if it is even. If num is a float, it is cast to an int.

round_half_up(n)[source]

Round a number to the nearest integer, rounding half up.

safe_index(arr, value, *, default=None)[source]

Safely get the index of a value in a list. If the value is not in the list, None is returned.

class MjCambrianActuator[source]

Helper class which stores information about a Mujoco actuator.

Variables:
  • adr (int) – The Mujoco actuator ID (index into model.actuator_* arrays).

  • trnadr (int) – The index of the actuator’s transmission in the model.

  • ctrlrange (Tuple[float, float]) – The control range of the actuator.

  • ctrllimited (bool) – Whether the actuator is control-limited.

class MjCambrianJoint[source]

Helper class which stores information about a Mujoco joint.

Variables:
  • type (int) – The Mujoco joint type (mj.mjtJoint).

  • adr (int) – The Mujoco joint ID (index into model.jnt_* arrays).

  • qposadr (int) – The index of the joint’s position in the qpos array.

  • numqpos (int) – The number of positions in the joint.

  • qveladr (int) – The index of the joint’s velocity in the qvel array.

  • numqvel (int) – The number of velocities in the joint.

static create(model, jntadr)[source]

Create a Joint object from a Mujoco model and joint body ID.

property qposadrs: List[int]

Get the indices of the joint’s positions in the qpos array.

property qveladrs: List[int]

Get the indices of the joint’s velocities in the qvel array.

class MjCambrianGeometry[source]

Helper class which stores information about a Mujoco geometry

Variables:
  • id (int) – The Mujoco geometry ID (index into model.geom_* arrays).

  • rbound (float) – The radius of the geometry’s bounding sphere.

  • pos (np.ndarray) – The position of the geometry relative to the body.

pickle_unpickleable_object(source)[source]

Return a reduce tuple: (unpickle_callable, (args,))

unpickle_unpickleable_object(cls, attributes)[source]

This will be called automatically by Python when unpickling.

agent_selected(agent, agents)[source]

Check if the agent is selected.