### Install MiniGrid Library
Source: https://minari.farama.org/_downloads/8392853af10481c4703c346afc6e6605/custom_space_serialization
Instructions to install the `minigrid` library, which is a prerequisite for running the MiniGrid environment examples in this tutorial.
```Bash
pip install minigrid
```
--------------------------------
### Install Minari Library
Source: https://minari.farama.org/content/basic_usage
Provides commands for installing the Minari library, covering basic installation, installation with all dependencies, and setting up for development from source.
```Shell
pip install minari
```
```Shell
pip install "minari[all]"
```
```Shell
git clone https://github.com/Farama-Foundation/Minari.git --single-branch
cd Minari
pip install -e ".[all]"
```
--------------------------------
### Minari CLI Installation and Basic Usage
Source: https://minari.farama.org/content/minari_cli
This snippet demonstrates how to install the Minari library using pip, check the installed Minari version, and view the comprehensive help output for the Minari CLI. The help output serves as API documentation for the available commands and options, detailing how to interact with Minari datasets.
```bash
$ pip install minari
---> 100%
$ minari --version
Minari version: 0.5.0
```
```APIDOC
Usage: minari [OPTIONS] COMMANDS [ARGS]...
Minari is a tool for collecting and hosting Offline datasets for Reinforcement Learning environments based on the Gymnaisum API.
Options:
--version -v Show installed Minari version.
--help Show this message and exit.
Commands:
combine Combine multiple datasets into a single Minari dataset.
delete Delete datasets from local database.
download Download Minari datasets from Farama server.
list List Minari datasets.
upload Upload Minari datasets to the remote Farama server.
```
--------------------------------
### Install Prerequisites for Headless Environment Display
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Installs system-level packages (apt-get) and Python libraries (pip) required for setting up a virtual display in headless environments like Google Colab. These packages enable graphical output and video recording without a physical screen.
```bash
! sudo apt-get update
! sudo apt-get install -y python3-opengl
! apt install ffmpeg
! apt install xvfb
! pip install pyvirtualdisplay
```
--------------------------------
### Install TorchRL and Dependencies
Source: https://minari.farama.org/_downloads/221b3a2007d099dba1e44346a4856376/IQL_torchrl
Installs necessary libraries for the tutorial, including TorchRL, Matplotlib, Minari, and Gymnasium-Robotics. Users might need to install PyTorch nightly separately.
```bash
! pip install torchrl
! pip install matplotlib minari gymnasium-robotics
```
--------------------------------
### Install Minigrid Library
Source: https://minari.farama.org/tutorials/dataset_creation/custom_space_serialization
Installs the `minigrid` Python package using pip, which is required to run the MiniGrid environments used in this tutorial.
```bash
pip install minigrid
```
--------------------------------
### Install Core Dependencies for TorchRL and Minari
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Installs the necessary Python packages, including TorchRL, Matplotlib, Minari, and Gymnasium-robotics, required for running the tutorial. Users might need to install PyTorch nightly separately if conflicts occur, and remember to use the '-U' flag for upgrades.
```bash
! pip install torchrl
! pip install matplotlib minari gymnasium-robotics
```
--------------------------------
### Install TorchRL and Core Dependencies
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
This snippet provides shell commands to install the primary Python packages required for the tutorial. It includes TorchRL, Matplotlib for plotting, Minari for dataset handling, and Gymnasium-robotics for environment interactions. A note is included regarding potential PyTorch conflicts and the recommendation to use PyTorch nightly builds.
```shell
! pip install torchrl
! pip install matplotlib minari gymnasium-robotics
```
--------------------------------
### Install Virtual Display Prerequisites
Source: https://minari.farama.org/_downloads/221b3a2007d099dba1e44346a4856376/IQL_torchrl
Installs system-level packages and Python libraries required for setting up a virtual display in headless environments like Google Colab. This is necessary for rendering graphical outputs.
```bash
! sudo apt-get update
! sudo apt-get install -y python3-opengl
! apt install ffmpeg
! apt install xvfb
! pip install pyvirtualdisplay
```
--------------------------------
### Install Virtual Display Prerequisites for Headless Environments
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
These shell commands are necessary for setting up a virtual display in headless environments, such as Google Colab notebooks, where a graphical interface is not directly available. They update the package lists and install essential system-level dependencies like python3-opengl, ffmpeg, and xvfb, followed by the pyvirtualdisplay Python package.
```shell
! sudo apt-get update
! sudo apt-get install -y python3-opengl
! apt install ffmpeg
! apt install xvfb
! pip install pyvirtualdisplay
```
--------------------------------
### Example Cumulative Reward Output
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
Provides an example of the console output when displaying the cumulative reward calculated from an environment rollout. This demonstrates the typical format for presenting the total reward accumulated over an episode.
```Console Output
Cumulative reward: 884.10
```
--------------------------------
### Verify Evaluation Environment Specifications
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OpenDoorColor/optimal-fullobs-v0
This example shows how to load a Minari dataset and recover both the training and evaluation environments. It then asserts that their specifications are identical, indicating that the evaluation environment is the same as the one used for dataset creation.
```Python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-OpenDoorColor/optimal-fullobs-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Initialize and Start PyVirtualDisplay
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
This Python code snippet initializes and starts a virtual display using the `pyvirtualdisplay` library. It configures the display to be invisible (`visible=0`) and sets its resolution to 1400x900 pixels. This step is crucial for rendering graphical environments and videos in environments without a physical display, allowing the simulation to run without errors.
```python
from pyvirtualdisplay import Display
virtual_display = Display(visible=0, size=(1400, 900))
virtual_display.start()
```
--------------------------------
### Install and Manage Minari Datasets via CLI
Source: https://minari.farama.org/index
This snippet demonstrates the command-line interface (CLI) usage for Minari. It covers installing the Minari library with all its dependencies, listing available remote datasets from the Farama server, and downloading a specific dataset to the local machine. This workflow is essential for users to set up Minari and access its hosted Offline Reinforcement Learning datasets.
```shell
// Install Minari
$ pip install "minari[all]"
---> 100%
// Show remote datasets.
$ minari list remote
Minari datasets in Farama server
┌─────────────────────┬───────────┬────────────┬───────────┬─────────┐
│ │ Total │ Total │ Dataset │ │
│ Name │ Episodes │ Steps │ Size │ Author │
┡─────────────────────╇───────────╇────────────╇───────────╇─────────┩
│ D4RL/door/cloned-v2 │ 4356 │ 1000000 │ 1077.7 MB │ Farama │
│ D4RL/door/expert-v2 │ 5000 │ 1000000 │ 1096.4 MB │ Farama │
│ D4RL/door/human-v2 │ 25 │ 6729 │ 7.1 MB │ Farama │
└─────────────────────┴───────────┴────────────┴───────────┴─────────┘
// Download dataset D4RL/door/cloned-v2
$ minari download D4RL/door/cloned-v2
Downloading D4RL/door/cloned-v2 from Farama servers...
* Downloading data file 'D4RL/door/cloned-v2/data/main_data.hdf5' ...
---> 100%
Dataset D4RL/door/cloned-v2 downloaded to ~/.minari/datasets/D4RL/door/cloned-v2
```
--------------------------------
### Example Cumulative Reward Output
Source: https://minari.farama.org/_downloads/221b3a2007d099dba1e44346a4856376/IQL_torchrl
Shows an example output of the cumulative reward calculation, demonstrating the format and typical value obtained from an environment rollout.
```APIDOC
Cumulative reward: 884.10
```
--------------------------------
### Initialize AdroitHandPen Environment with GymEnv
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
Initializes the `AdroitHandPen-v1` environment using TorchRL's `GymEnv` wrapper. This setup enables interaction with the Gymnasium environment, configured to return pixel observations, and sets a reproducible random seed.
```Python
env_id = "AdroitHandPen-v1"
example_env = GymEnv(env_id, from_pixels=True, pixels_only=False)
example_env.set_seed(seed)
```
--------------------------------
### Verify Evaluation Environment Specifications in Minari
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OpenDoor/optimal-fullobs-v0
This Python example shows how to load a Minari dataset and recover both the primary and evaluation environments. It then asserts that their specifications are identical, indicating that no separate evaluation environment was defined for this dataset.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-OpenDoor/optimal-fullobs-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Verify Evaluation Environment Equivalence in Minari
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OpenRedBlueDoors/optimal-fullobs-v0
This Python example shows how to load a Minari dataset and recover both the training and evaluation environments. It then asserts that their specifications are identical, indicating that the evaluation environment is the same as the one used for dataset creation.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-OpenRedBlueDoors/optimal-fullobs-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Example Usage of minari.DataCollector
Source: https://minari.farama.org/api/data_collector
This snippet demonstrates how to initialize `minari.DataCollector` with a Gymnasium environment, simulate steps to collect data, and then create a Minari dataset from the collected information. It illustrates the basic workflow for data collection and dataset generation using the wrapper.
```python
import minari
import gymnasium as gym
env = minari.DataCollector(gym.make('EnvID'))
env.reset()
for _ in range(num_steps):
action = env.action_space.sample()
obs, rew, terminated, truncated, info = env.step()
if terminated or truncated:
env.reset()
dataset = env.create_dataset(dataset_id="env_name/dataset_name-v(version)", **kwargs)
```
--------------------------------
### Validate Minari Dataset Version Against Installed Library
Source: https://minari.farama.org/_modules/minari/dataset/minari_dataset
This code block ensures that the Minari dataset's `minari_version` is compatible with the currently installed Minari library. It imports necessary version information and raises a `ValueError` if the dataset's version is not among the supported versions, guiding the user on compatibility.
```python
dataset_id = metadata["dataset_id"]
assert isinstance(dataset_id, str)
self._dataset_id = dataset_id
minari_version = metadata["minari_version"]
assert isinstance(minari_version, str)
from minari import __version__, supported_dataset_versions
if minari_version not in supported_dataset_versions:
raise ValueError(
f"The installed Minari version {__version__} does not support the dataset generated by Minari {minari_version}.\n"
f"Supported versions: {supported_dataset_versions}"
)
self._minari_version = minari_version
```
--------------------------------
### Verify Evaluation Environment Specs in Minari
Source: https://minari.farama.org/datasets/mujoco/walker2d/medium-v0
This Python example illustrates how to load a Minari dataset and recover both the primary training environment and the evaluation environment. It then includes an assertion to confirm that the specifications of the recovered training environment are identical to those of the evaluation environment, indicating consistent setup.
```python
import minari
dataset = minari.load_dataset('mujoco/walker2d/medium-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Initialize AdroitHandPen Environment with GymEnv
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Demonstrates how to initialize the 'AdroitHandPen-v1' environment using TorchRL's `GymEnv` wrapper, enabling pixel observations and setting a random seed for reproducibility.
```python
env_id = "AdroitHandPen-v1"
example_env = GymEnv(env_id, from_pixels=True, pixels_only=False)
example_env.set_seed(seed)
```
--------------------------------
### Verify Minari Evaluation Environment Specifications
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OpenDoorsOrderN4Debug/optimal-v0
This Python example illustrates how to confirm that the evaluation environment for a Minari dataset, when not explicitly defined, matches the primary data collection environment. It loads the dataset, recovers both environments, and uses an assertion to verify their identical specifications. This snippet is useful for ensuring consistency in experimental setups where the evaluation context is expected to mirror the training context.
```Python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-OpenDoorsOrderN4Debug/optimal-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Initialize Minari Experience Replay Buffer
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Demonstrates how to create a `MinariExperienceReplay` buffer for the 'D4RL/pen/human-v2' dataset, configuring batch size, sampler, and applying the `DoubleToFloat` transform for data consistency during sampling.
```python
dataset_id = "D4RL/pen/human-v2"
batch_size = 256
replay_buffer = MinariExperienceReplay(
dataset_id,
split_trajs=False,
batch_size=batch_size,
sampler=SamplerWithoutReplacement(),
transform=DoubleToFloat(),
)
```
--------------------------------
### Perform Environment Rollout with TorchRL
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Illustrates how to use `env.rollout()` to simulate a full episode up to a maximum number of steps, automatically casting data to the appropriate device for efficient processing.
```python
max_episode_steps = 1000
tensordict = example_env.rollout(max_steps=max_episode_steps, auto_cast_to_device=True)
```
--------------------------------
### Import TorchRL Environment Wrappers
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Imports necessary classes from `torchrl.envs.libs.gym` and `torchrl.envs` to wrap Gymnasium environments and apply data transformations like `DoubleToFloat`.
```python
from torchrl.envs.libs.gym import GymEnv
from torchrl.envs import DoubleToFloat, TransformedEnv
```
--------------------------------
### Minari Dataset Specifications for BabyAI-GoTo/optimal-fullobs-v0
Source: https://minari.farama.org/datasets/minigrid/BabyAI-GoTo/optimal-fullobs-v0
Detailed specifications of the `minigrid/BabyAI-GoTo/optimal-fullobs-v0` Minari dataset, including its total steps, episodes, observation/action spaces, and metadata like algorithm, author, and download command.
```APIDOC
Dataset Specs:
Total Steps: 52531
Total Episodes: 1000
Dataset Observation Space: Dict('direction': Discrete(4), 'image': Box(0, 255, (22, 22, 3), uint8), 'mission': Text(1, 256, charset=''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''(),,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeeeffghijklmnnoopqrrssttuvwxyzz{}'))
Dataset Action Space: Discrete(7)
Algorithm: BabyAI expert bot
Author: Omar G. Younis
Email: omar@farama.org
Code Permalink: https://github.com/Farama-Foundation/minari-dataset-generation-scripts
Minari Version: 0.5.1 (supported)
Download: minari download minigrid/BabyAI-GoTo/optimal-fullobs-v0
```
--------------------------------
### Example Console Output: Final Policy Score
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Displays an example of the console output after evaluating the trained policy. It shows the calculated average cumulative reward, providing a concrete performance metric.
```text
Cumulative reward (averaged over 100 episodes): 1872.69
```
--------------------------------
### Install MiniGrid and Import Required Modules
Source: https://minari.farama.org/_downloads/e848fd7cb68dc8286565925cb7c015d1/custom_space_serialization
This snippet installs the `minigrid` library, a prerequisite for the tutorial, and imports all necessary Python modules from `json`, `typing`, `gymnasium`, `minigrid.core.mission`, and `minari` for custom space serialization and data collection.
```shell
pip install minigrid
```
```python
import json
from typing import Dict, Union
import gymnasium as gym
from minigrid.core.mission import MissionSpace
import minari
from minari import DataCollector
from minari.serialization import deserialize_space, serialize_space
```
--------------------------------
### Download Minari BabyAI-PutNextS6N3 Optimal Fullobs Dataset
Source: https://minari.farama.org/datasets/minigrid/BabyAI-PutNextS6N3/optimal-fullobs-v0
This shell command initiates the download of the specified Minari dataset, 'minigrid/BabyAI-PutNextS6N3/optimal-fullobs-v0', to your local system, making it available for use.
```shell
minari download minigrid/BabyAI-PutNextS6N3/optimal-fullobs-v0
```
--------------------------------
### Download Minari Dataset via CLI
Source: https://minari.farama.org/datasets/minigrid/BabyAI-PutNextS5N1/optimal-fullobs-v0
Command-line instruction to download the 'minigrid/BabyAI-PutNextS5N1/optimal-fullobs-v0' dataset using the Minari CLI tool. This allows users to obtain the dataset for local use.
```Shell
minari download minigrid/BabyAI-PutNextS5N1/optimal-fullobs-v0
```
--------------------------------
### Example Output of QIteration Path Generation
Source: https://minari.farama.org/_downloads/d242f1ed6ffcff2255fb7296630d75ca/point_maze_dataset
This code example demonstrates the dictionary format of waypoints returned by the `QIteration.generate_path` method. Each key-value pair represents a step in the optimal path, where the key is the current cell coordinate and the value is the next cell coordinate to move towards the goal within the discretized maze.
```python
{(5, 1): (4, 1), (4, 1): (4, 2), (4, 2): (3, 2), (3, 2): (2, 2), (2, 2): (2, 1), (2, 1): (1, 1)}
```
--------------------------------
### Initialize Virtual Display for Headless Environments
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Initializes and starts a virtual display using the `pyvirtualdisplay` library with specified dimensions (1400x900). This allows graphical applications and environments to run and render in environments without a physical display, such as remote servers or cloud notebooks.
```python
from pyvirtualdisplay import Display
virtual_display = Display(visible=0, size=(1400, 900))
virtual_display.start()
```
--------------------------------
### Verify Evaluation Environment Specifications Match Training Environment
Source: https://minari.farama.org/datasets/minigrid/BabyAI-ActionObjDoor/optimal-v0
This Python code illustrates how to load a Minari dataset and recover both the primary training environment and the evaluation environment. It then asserts that the specifications of these two environments are identical, confirming that the evaluation setup mirrors the training setup when no separate `eval_env_spec` is provided.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-ActionObjDoor/optimal-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Demonstrate and Visualize Agent Rollout in Gym Environment
Source: https://minari.farama.org/_downloads/221b3a2007d099dba1e44346a4856376/IQL_torchrl
This snippet initializes a transformed Gym environment, sets a seed, performs a rollout using a given policy, calculates and prints the cumulative reward, saves the episode frames as an MP4 video, and then embeds the video into an HTML element for display, suitable for environments like Google Colab.
```python
viewer_env = TransformedEnv(
GymEnv(env_id, from_pixels=True, pixels_only=False),
DoubleToFloat()
)
viewer_env.set_seed(seed)
tensordict = viewer_env.rollout(max_steps=max_episode_steps, policy=model[0], auto_cast_to_device=True)
print(f"Cumulative reward: {tensordict['next', 'reward'].sum():.2f}")
frames = list(tensordict["pixels"].numpy())
save_video(frames, video_folder="results_video", fps=30)
# Display the video. Embedding is necessary for Google Colab etc
mp4 = open("results_video/rl-video-episode-0.mp4", "rb").read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML("""
""" % data_url)
```
--------------------------------
### Minari Observation Space Dictionary Example
Source: https://minari.farama.org/_downloads/e848fd7cb68dc8286565925cb7c015d1/custom_space_serialization
An example of a dictionary representing an observation space, likely used within the Minari library. It defines components such as 'direction' (a discrete space), 'image' (a Box space for image data), and 'mission' (a custom MissionSpace). This structure is crucial for defining the environment's state representation.
```python
Dict('direction': Discrete(4), 'image': Box(0, 255, (7, 7, 3), uint8), 'mission': MissionSpace(. at 0x11f2608b0>, None))
```
--------------------------------
### Execute Environment Rollout for Episode Data Collection
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
Demonstrates using `env.rollout()` to simulate a full episode, collecting all transition data into a single `TensorDict`. The `max_steps` parameter limits the episode length, and `auto_cast_to_device` ensures data is on the correct compute device.
```Python
max_episode_steps = 1000
tensordict = example_env.rollout(max_steps=max_episode_steps, auto_cast_to_device=True)
```
--------------------------------
### Minari Data Collector Module Imports and Setup
Source: https://minari.farama.org/_modules/minari/data_collector/data_collector
This Python code snippet displays the initial imports and setup for the `minari.data_collector.data_collector` module. It includes standard library imports (e.g., `os`, `shutil`, `tempfile`), third-party libraries crucial for reinforcement learning environments like `gymnasium` and `numpy`, and internal Minari components such as `EpisodeBuffer`, `MinariDataset`, and utilities for namespace and metadata generation. This section lays the groundwork for data collection functionalities within the Minari library.
```Python
from __future__ import annotations
import copy
import os
import secrets
import shutil
import tempfile
import warnings
from typing import Any, Callable, Dict, Optional, SupportsFloat, Type
import gymnasium as gym
import numpy as np
from gymnasium.core import ActType, ObsType
from gymnasium.envs.registration import EnvSpec
from minari.data_collector.callbacks import EpisodeMetadataCallback, StepDataCallback
from minari.data_collector.episode_buffer import EpisodeBuffer
from minari.dataset.minari_dataset import MinariDataset, parse_dataset_id
from minari.dataset.minari_storage import MinariStorage
from minari.namespace import create_namespace, list_local_namespaces
from minari.utils import _generate_dataset_metadata, _generate_dataset_path
```
--------------------------------
### Download Minari Dataset
Source: https://minari.farama.org/datasets/minigrid/BabyAI-GoToObjS6/optimal-fullobs-v0
Command-line instruction to download the specific Minari dataset 'minigrid/BabyAI-GoToObjS6/optimal-fullobs-v0' using the Minari CLI.
```shell
minari download minigrid/BabyAI-GoToObjS6/optimal-fullobs-v0
```
--------------------------------
### Minari Dataset Specifications
Source: https://minari.farama.org/datasets/minigrid/BabyAI-PutNextS6N3/optimal-v0
Detailed specifications for the `minigrid/BabyAI-PutNextS6N3/optimal-v0` dataset, including total steps, episodes, observation/action spaces, and metadata like algorithm, author, and download information.
```APIDOC
Dataset Specifications:
Total Steps: 14194
Total Episodes: 1000
Dataset Observation Space: Dict('direction': Discrete(4), 'image': Box(0, 255, (7, 7, 3), uint8), 'mission': Text(1, 256, charset=...))
Dataset Action Space: Discrete(7)
Algorithm: BabyAI expert bot
Author: Omar G. Younis
Email: omar@farama.org
Code Permalink: https://github.com/Farama-Foundation/minari-dataset-generation-scripts
Minari Version: 0.5.1 (supported)
Download: minari download minigrid/BabyAI-PutNextS6N3/optimal-v0
```
--------------------------------
### Recover Environment from Minari Dataset
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OpenDoorDebug/optimal-fullobs-v0
This Python code demonstrates how to load a Minari dataset and recover the corresponding Gymnasium environment. It uses the 'minigrid/BabyAI-OpenDoorDebug/optimal-fullobs-v0' dataset as an example.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-OpenDoorDebug/optimal-fullobs-v0')
env = dataset.recover_environment()
```
--------------------------------
### Get Total Number of Episodes
Source: https://minari.farama.org/_modules/minari/dataset/minari_dataset
Implements the `__len__` method, returning the total count of episodes in the dataset, making the dataset object compatible with `len()` function.
```APIDOC
__len__() -> int
```
--------------------------------
### Download Minari BabyAI-GoToObjDoor/optimal-v0 Dataset
Source: https://minari.farama.org/datasets/minigrid/BabyAI-GoToObjDoor/optimal-v0
This command demonstrates how to download the specific 'minigrid/BabyAI-GoToObjDoor/optimal-v0' dataset using the Minari command-line interface.
```bash
minari download minigrid/BabyAI-GoToObjDoor/optimal-v0
```
--------------------------------
### Verify Evaluation Environment Specification
Source: https://minari.farama.org/datasets/minigrid/BabyAI-GoToObjMazeS5/optimal-v0
This Python example illustrates that when an `eval_env_spec` is not explicitly defined for a dataset, the evaluation environment recovered will have the same specifications as the environment used for dataset creation.
```Python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-GoToObjMazeS5/optimal-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Import Libraries for Policy Performance Visualization
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
These import statements bring in necessary modules from `IPython.display`, `gymnasium.utils.save_video`, and `base64` to facilitate the display and saving of video visualizations of the trained policy's performance.
```Python
from IPython.display import HTML
from gymnasium.utils.save_video import save_video
from base64 import b64encode
```
--------------------------------
### Load Minari Dataset and Recover Environment
Source: https://minari.farama.org/datasets/minigrid/BabyAI-GoToOpen/optimal-fullobs-v0
Demonstrates how to load the `minigrid/BabyAI-GoToOpen/optimal-fullobs-v0` dataset using the `minari` library and recover the corresponding Gymnasium environment.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-GoToOpen/optimal-fullobs-v0')
env = dataset.recover_environment()
```
--------------------------------
### Load and Sample Minari Dataset Episodes
Source: https://minari.farama.org/content/dataset_standards
Demonstrates how to load a Minari dataset and sample a specified number of `EpisodeData` elements from it. This example uses the 'D4RL/door/human-v2' dataset, downloading it if not already present.
```python
import minari
dataset = minari.load_dataset("D4RL/door/human-v2", download=True)
sampled_episodes = dataset.sample_episodes(10)
```
--------------------------------
### Import Libraries for Policy Visualization and Display
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Imports essential Python libraries for displaying rich content in notebooks, saving video files from episode frames, and encoding binary data for web embedding. These are crucial for visualizing trained agent performance.
```python
from IPython.display import HTML
from gymnasium.utils.save_video import save_video
from base64 import b64encode
```
--------------------------------
### Get Local Minari Namespace Metadata API
Source: https://minari.farama.org/api/namespace/namespace
Loads the metadata associated with a specified local namespace. This API is part of the experimental namespace feature and may evolve in future releases.
```APIDOC
minari.namespace.get_namespace_metadata(namespace: str) -> Dict[str, Any]
namespace: Identifier for the local namespace.
Returns: A dictionary containing the metadata for the specified namespace.
```
--------------------------------
### Import Minari Replay Buffer Components
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Imports necessary classes from `torchrl.data.datasets.minari_data` and `torchrl.data.replay_buffers` for creating and managing replay buffers with Minari datasets, specifically `MinariExperienceReplay` and `SamplerWithoutReplacement`.
```python
from torchrl.data.datasets.minari_data import MinariExperienceReplay
from torchrl.data.replay_buffers import SamplerWithoutReplacement
```
--------------------------------
### Verifying Evaluation Environment Equivalence
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OpenDoorLoc/optimal-fullobs-v0
This example shows how to recover both the training and evaluation environments from a Minari dataset. It then asserts that their specifications are identical, indicating that the evaluation environment is the same as the one used for dataset creation.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-OpenDoorLoc/optimal-fullobs-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Example of Customizing StepDataCallback
Source: https://minari.farama.org/api/data_collector/step_data_callback
This Python code snippet demonstrates how to extend the `StepDataCallback` class to include custom environment state information, such as position, orientation, and velocity, within the collected step data dictionary.
```python
class CustomStepDataCallback(StepDataCallback):
def __call__(self, env, **kwargs):
step_data = super().__call__(env, **kwargs)
step_data['environment_states'] = {}
step_data['environment_states']['pose'] = {}
step_data['environment_states']['pose']['position'] = env.position
step_data['environment_states']['pose']['orientation'] = env.orientation
step_data['environment_states']['velocity'] = env.velocity
return step_data
```
--------------------------------
### Generate Video of Trained Policy Rollout
Source: https://minari.farama.org/tutorials/using_datasets/IQL_torchrl
This code block demonstrates how to visualize the trained policy's performance by rolling out a single episode and saving it as a video. It sets up a viewer environment, performs a rollout, extracts pixel frames, and uses `save_video` to create an MP4 file.
```Python
viewer_env = TransformedEnv(
GymEnv(env_id, from_pixels=True, pixels_only=False),
DoubleToFloat()
)
viewer_env.set_seed(seed)
tensordict = viewer_env.rollout(max_steps=max_episode_steps, policy=model[0], auto_cast_to_device=True)
print(f"Cumulative reward: {tensordict['next', 'reward'].sum():.2f}")
frames = list(tensordict["pixels"].numpy())
save_video(frames, video_folder="results_video", fps=30)
```
--------------------------------
### Recover Environment from Minari Dataset
Source: https://minari.farama.org/datasets/atari/crazyclimber/expert-v0
This snippet demonstrates how to load a Minari dataset by its ID and then recover the Gymnasium environment that was used to generate the dataset. This allows users to recreate the exact environment setup.
```python
import minari
dataset = minari.load_dataset('atari/crazyclimber/expert-v0')
env = dataset.recover_environment()
```
--------------------------------
### Minari Dataset Specifications
Source: https://minari.farama.org/datasets/minigrid/BabyAI-Open/optimal-fullobs-v0
Detailed specifications for the 'minigrid/BabyAI-Open/optimal-fullobs-v0' dataset, including total steps, episodes, observation/action spaces, algorithm, author, and version information. It also provides the command to download the dataset.
```APIDOC
Dataset Specs:
Total Steps: 30147
Total Episodes: 1000
Dataset Observation Space: Dict('direction': Discrete(4), 'image': Box(0, 255, (22, 22, 3), uint8), 'mission': Text(1, 256, charset='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeeeffghijklmnnoopqrrssttuvwxyzz{}'))
Dataset Action Space: Discrete(7)
Algorithm: BabyAI expert bot
Author: Omar G. Younis
Email: omar@farama.org
Code Permalink: https://github.com/Farama-Foundation/minari-dataset-generation-scripts
Minari Version: 0.5.1 (supported)
Download: minari download minigrid/BabyAI-Open/optimal-fullobs-v0
```
--------------------------------
### Download Large-Diverse AntMaze Dataset
Source: https://minari.farama.org/datasets/D4RL/antmaze/large-diverse-v1
Use this command to download the Large-Diverse AntMaze dataset, which is part of the D4RL collection, using the Minari library. Ensure Minari is installed and configured correctly to access this dataset.
```shell
minari download D4RL/antmaze/large-diverse-v1
```
--------------------------------
### Minari Dataset Specifications
Source: https://minari.farama.org/datasets/minigrid/BabyAI-KeyCorridorS6R3/optimal-fullobs-v0
Detailed specifications of the Minari dataset, including total steps, episodes, observation/action spaces, and metadata such as algorithm, author, and download instructions.
```APIDOC
Dataset Specs:
Total Steps: 111764
Total Episodes: 1000
Dataset Observation Space:
Type: Dict
Components:
direction: Discrete(4)
image: Box(0, 255, (16, 16, 3), uint8)
mission: Text(1, 256, charset=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeeeffghijklmnnoopqrrssttuvwxyzz{})
Dataset Action Space: Discrete(7)
Algorithm: BabyAI expert bot
Author: Omar G. Younis
Email: omar@farama.org
Code Permalink: https://github.com/Farama-Foundation/minari-dataset-generation-scripts
Minari Version: 0.5.1 (supported)
Download: minari download minigrid/BabyAI-KeyCorridorS6R3/optimal-fullobs-v0
```
--------------------------------
### List Local Minari Datasets
Source: https://minari.farama.org/api/minari_functions
Retrieves the IDs and associated metadata for all Minari datasets stored in the local database. It provides filtering options to return only the latest versions or datasets compatible with the current Minari installation.
```APIDOC
minari.list_local_datasets(latest_version: bool = False, compatible_minari_version: bool = False, prefix: str | None = None) -> Dict[str, Dict[str, str | int | bool]]
latest_version: If True, only the metadata for the most recent version of each dataset is returned. Defaults to False.
compatible_minari_version: If True, only datasets that are compatible with the currently installed Minari version are included in the results. Defaults to False.
prefix: An optional string prefix to filter the dataset IDs. Only datasets whose IDs start with this prefix will be returned. Defaults to None.
Returns:
Dict[str, Dict[str, str | int | bool]]: A dictionary where keys are the names of the Minari datasets and values are their corresponding metadata (e.g., version, size, creation date).
```
--------------------------------
### Minari Dataset Specifications
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OpenDoorDebug/optimal-fullobs-v0
Detailed specifications for the minigrid/BabyAI-OpenDoorDebug/optimal-fullobs-v0 dataset, including total steps, episodes, observation and action spaces, the algorithm used, author information, Minari version, and download instructions.
```APIDOC
Dataset Specs:
Total Steps: 7399
Total Episodes: 1000
Dataset Observation Space: Dict('direction': Discrete(4), 'image': Box(0, 255, (22, 22, 3), uint8), 'mission': Text(1, 256, charset=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeeeffghijklmnnoopqrrssttuvwxyzz{}))
Dataset Action Space: Discrete(7)
Algorithm: BabyAI expert bot
Author: Omar G. Younis
Email: omar@farama.org
Code Permalink: https://github.com/Farama-Foundation/minari-dataset-generation-scripts
Minari Version: 0.5.1 (supported)
Download: minari download minigrid/BabyAI-OpenDoorDebug/optimal-fullobs-v0
```
--------------------------------
### Recover and Compare Evaluation Environment
Source: https://minari.farama.org/datasets/minigrid/BabyAI-MoveTwoAcrossS5N2/optimal-fullobs-v0
This Python example illustrates how to recover both the training and evaluation environments from a Minari dataset. It then asserts that their specifications are identical, indicating that the evaluation environment is the same as the one used for dataset creation.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-MoveTwoAcrossS5N2/optimal-fullobs-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Compare Recovered and Evaluation Environments from Minari Dataset
Source: https://minari.farama.org/datasets/minigrid/BabyAI-BossLevel/optimal-fullobs-v0
Illustrates how to recover both the primary and evaluation environments from a Minari dataset. The example then asserts that their specifications are identical, indicating that no separate evaluation environment was defined for this dataset.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-BossLevel/optimal-fullobs-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Verify Minari Dataset Environment and Evaluation Environment Specs
Source: https://minari.farama.org/datasets/mujoco/halfcheetah/simple-v0
This Python example illustrates loading a Minari dataset and recovering both the primary and evaluation environments. It includes an assertion to confirm that the specifications of these two environments are identical, indicating consistent environment parameters.
```python
import minari
dataset = minari.load_dataset('mujoco/halfcheetah/simple-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Recover Environment from Minari Dataset
Source: https://minari.farama.org/datasets/D4RL/door/cloned-v2
This Python example demonstrates how to load a specific Minari dataset, 'D4RL/door/cloned-v2', and then recover the Gymnasium environment that was used to generate this dataset. This allows users to interact with the environment directly.
```Python
import minari
dataset = minari.load_dataset('D4RL/door/cloned-v2')
env = dataset.recover_environment()
```
--------------------------------
### Recover Gymnasium Environment from Minari Dataset
Source: https://minari.farama.org/datasets/D4RL/antmaze/large-diverse-v1
This Python code demonstrates how to load a Minari dataset and recover the associated Gymnasium environment. It shows examples for both the standard environment and a specific evaluation environment instance.
```python
import minari
dataset = minari.load_dataset('D4RL/antmaze/large-diverse-v1')
env = dataset.recover_environment()
```
```python
import minari
dataset = minari.load_dataset('D4RL/antmaze/large-diverse-v1')
eval_env = dataset.recover_environment(eval_env=True)
```
--------------------------------
### Minari Dataset Specifications for BabyAI-GoToOpen Optimal-Fullobs
Source: https://minari.farama.org/datasets/minigrid/BabyAI-GoToOpen/optimal-fullobs-v0
Details the key characteristics of the `minigrid/BabyAI-GoToOpen/optimal-fullobs-v0` dataset, including total steps, episodes, observation/action spaces, and metadata about its creation.
```APIDOC
Dataset Specs:
Total Steps: 39844
Total Episodes: 1000
Dataset Observation Space: Dict('direction': Discrete(4), 'image': Box(0, 255, (22, 22, 3), uint8), 'mission': Text(1, 256, charset=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeeeffghijklmnnoopqrrssttuvwxyzz{}))
Dataset Action Space: Discrete(7)
Algorithm: BabyAI expert bot
Author: Omar G. Younis
Email: omar@farama.org
Code Permalink: https://github.com/Farama-Foundation/minari-dataset-generation-scripts
Minari Version: 0.5.1 (supported)
Download: minari download minigrid/BabyAI-GoToOpen/optimal-fullobs-v0
```
--------------------------------
### Visualize First Frame of Episode Pixels
Source: https://minari.farama.org/_downloads/33c142a2af8efe60072d187060d148e2/IQL_torchrl
Shows how to extract and display the first frame's pixel data from the `TensorDict` using `matplotlib.pyplot`, demonstrating the visual observation capabilities of the environment.
```python
plt.imshow(tensordict["pixels"][0].numpy());
```
--------------------------------
### Enable Matplotlib Inline Display
Source: https://minari.farama.org/_downloads/4309a2a5710b4a8d568d698c8b7a60be/observation_space_subseting
A Jupyter Notebook magic command to ensure Matplotlib plots are displayed directly within the notebook output, though no plots are generated in this specific example.
```python
%matplotlib inline
```
--------------------------------
### Load Minari Dataset and Recover Environment
Source: https://minari.farama.org/datasets/minigrid/BabyAI-UnlockPickupDist/optimal-v0
This Python snippet demonstrates how to load a specific Minari dataset and then recover the Gymnasium environment associated with it. This is useful for reproducing the environment setup used during dataset generation.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-UnlockPickupDist/optimal-v0')
env = dataset.recover_environment()
```
--------------------------------
### Minari Dataset Specifications
Source: https://minari.farama.org/datasets/minigrid/BabyAI-PutNextS5N1/optimal-fullobs-v0
Detailed specifications for the 'minigrid/BabyAI-PutNextS5N1/optimal-fullobs-v0' dataset, including total steps, episodes, observation/action spaces, generation algorithm, author, and Minari version compatibility.
```APIDOC
Dataset:
Total Steps: 10295
Total Episodes: 1000
Dataset Observation Space: Dict('direction': Discrete(4), 'image': Box(0, 255, (9, 5, 3), uint8), 'mission': Text(1, 256, charset=
```
--------------------------------
### Download Minari Dataset via CLI
Source: https://minari.farama.org/datasets/minigrid/BabyAI-GoToObjMazeS4/optimal-fullobs-v0
Command-line instruction to download the specified Minari dataset using the 'minari download' utility.
```Bash
minari download minigrid/BabyAI-GoToObjMazeS4/optimal-fullobs-v0
```
--------------------------------
### Minari Evaluation Environment Recovery and Comparison
Source: https://minari.farama.org/datasets/minigrid/BabyAI-PutNextLocal/optimal-v0
Illustrates how to recover both the primary and evaluation environments from a Minari dataset and verifies that their specifications are identical when no separate evaluation environment is specified. This confirms consistency in environment setup.
```Python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-PutNextLocal/optimal-v0')
env = dataset.recover_environment()
eval_env = dataset.recover_environment(eval_env=True)
assert env.spec == eval_env.spec
```
--------------------------------
### Recovering Gymnasium Environment from Minari Dataset
Source: https://minari.farama.org/datasets/minigrid/BabyAI-OneRoomS20/optimal-fullobs-v0
This Python snippet demonstrates how to load a Minari dataset and recover the associated Gymnasium environment. It uses `minari.load_dataset` to get the dataset object and `dataset.recover_environment()` to instantiate the environment.
```python
import minari
dataset = minari.load_dataset('minigrid/BabyAI-OneRoomS20/optimal-fullobs-v0')
env = dataset.recover_environment()
```
--------------------------------
### Minari Dataset Specifications for BabyAI-PutNextS4N1
Source: https://minari.farama.org/datasets/minigrid/BabyAI-PutNextS4N1/optimal-v0
Detailed specifications of the minigrid/BabyAI-PutNextS4N1/optimal-v0 dataset, including total steps, episodes, observation/action spaces, and metadata such as algorithm, author, and download instructions.
```APIDOC
Dataset Specs:
Total Steps: 7824
Total Episodes: 1000
Dataset Observation Space: Dict('direction': Discrete(4), 'image': Box(0, 255, (7, 7, 3), uint8), 'mission': Text(1, 256, charset='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeeeffghijklmnnoopqrrssttuvwxyzz{}'))
Dataset Action Space: Discrete(7)
Algorithm: BabyAI expert bot
Author: Omar G. Younis
Email: omar@farama.org
Code Permalink: https://github.com/Farama-Foundation/minari-dataset-generation-scripts
Minari Version: 0.5.1 (supported)
Download: minari download minigrid/BabyAI-PutNextS4N1/optimal-v0
```
--------------------------------
### Initialize Adroit Pen Environment with GymEnv
Source: https://minari.farama.org/_downloads/221b3a2007d099dba1e44346a4856376/IQL_torchrl
Initializes the `AdroitHandPen-v1` environment from Gymnasium-Robotics using TorchRL's `GymEnv` wrapper. Configures it to include pixel observations and sets a random seed for reproducibility.
```python
env_id = "AdroitHandPen-v1"
example_env = GymEnv(env_id, from_pixels=True, pixels_only=False)
example_env.set_seed(seed)
```
--------------------------------
### Example Waypoint Trajectory Dictionary
Source: https://minari.farama.org/tutorials/dataset_creation/point_maze_dataset
This dictionary illustrates the format of the generated waypoint trajectory. Keys represent the current cell coordinates, and values represent the next cell coordinates in the path towards the goal.
```Python
{(5, 1): (4, 1), (4, 1): (4, 2), (4, 2): (3, 2), (3, 2): (2, 2), (2, 2): (2, 1), (2, 1): (1, 1)}
```