This repository was archived by the owner on Apr 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
S3 maps generation and loading #108
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
86473e0
tests.mapgen can store AsciiMap files to s3
berekuk b30704a
mettagrid.map.from_uri, mettagrid.map.from_s3_dir
berekuk b27cbca
move mapgen script to tools/
berekuk 5f1ac6d
nop and remove_agents scenes
berekuk ce7a530
rename builders, add documentation
berekuk 446eb2d
doc update
berekuk 86a1fc4
simplify hydra; load_random doesn't need index; refactorings
berekuk 70307e9
index_s3_maps argparse
berekuk c793fd3
rewrite mapgen without hydra; generate maps without env
berekuk 758e427
revert constants.pyx
berekuk e664f74
Merge branch 'main' into s3-mapgen
berekuk 95aec62
simplify type checking for "where"
berekuk ceb5a01
comment about boto3
berekuk 448a0b3
extract mettagrid.map.utils.storage code; handle file:// uris
berekuk 98440fe
remove old script configs
berekuk 3b3c6d0
better --help
berekuk 484765f
update docs
berekuk b486454
change mapgen options according to https://github.com/Metta-AI/mettag…
berekuk 3cb907d
tools.map.view script; tests; update docs
berekuk 8b8baa4
normalize uri
berekuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| _target_: mettagrid.map.load.Load | ||
|
|
||
| uri: ??? | ||
|
|
||
| # Optional scene to render on top of the loaded map. | ||
| extra_root: | ||
| _target_: mettagrid.map.scenes.make_connected.MakeConnected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| _target_: mettagrid.map.load_random.LoadRandom | ||
|
|
||
| dir: ??? | ||
|
|
||
| # Optional scene to render on top of the loaded map. | ||
| # The following example shows how to patch the map if its agent count doesn't match `game.num_agents`. | ||
| extra_root: | ||
| _target_: mettagrid.map.scenes.nop.Nop | ||
|
|
||
| children: | ||
| - where: full | ||
| scene: | ||
| _target_: mettagrid.map.scenes.remove_agents.RemoveAgents | ||
|
|
||
| - where: full | ||
| scene: | ||
| _target_: mettagrid.map.scenes.random.Random | ||
| agents: 40 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Map generation | ||
|
|
||
| ## S3 maps | ||
|
|
||
| To produce maps in bulk and store them in S3, use the following commands: | ||
|
|
||
| ### Creating maps | ||
|
|
||
| ```bash | ||
| python -m tools.map.gen --output-uri=s3://BUCKET/DIR ./configs/game/map_builder/mapgen_auto.yaml | ||
| ``` | ||
|
|
||
| `mapgen_auto` builder is an example. You can use any YAML config that can be parsed by OmegaConf. | ||
|
|
||
| If `--output-uri` looks like a file (ends with `.yaml` or other extension), the map will be saved to that file. | ||
|
|
||
| Otherwise, the map will be saved to a file with a random suffix in that directory. | ||
|
|
||
| If `--output-uri` is not specified, the map won't be saved, only shown on screen. | ||
|
|
||
| To create maps in bulk, use `--count=N` option. | ||
|
|
||
| See `python -m tools.map.gen --help` for more options. | ||
|
|
||
| ### Viewing maps | ||
|
|
||
| You can view a single map by running: | ||
|
|
||
| ```bash | ||
| python -m tools.map.view s3://BUCKET/PATH/TO/MAP.yaml | ||
| ``` | ||
|
|
||
| The following command will show a random map from an S3 directory: | ||
|
|
||
| ```bash | ||
| python -m tools.map.view s3://BUCKET/DIR | ||
| ``` | ||
|
|
||
| Same heuristics about detecting if the URI is a file apply here. | ||
|
|
||
| ### Loading maps in map_builder configs | ||
|
|
||
| You can load a random map from an S3 directory in your YAML configs by using `mettagrid.map.load_random.LoadRandom` as a map builder. | ||
|
|
||
| `LoadRandom` allows you to modify the map by applying additional scenes to it. Check out `configs/game/map_builder/load_random.yaml` for an example config that modifies the number of agents in the map. | ||
|
|
||
| ### Indexing maps | ||
|
|
||
| Optionally, you can index your maps to make loading them faster. | ||
|
|
||
| This is intended to speed up reading from S3. It shouldn't change any functionality, and you should skip playing with this unless you find map loading from S3 is slow. | ||
|
|
||
| Index is a plain text file that lists URIs of all the maps. You can assemble it manually, or use the following script: | ||
|
|
||
| ```bash | ||
| python -m tools.index_s3_maps --dir=s3://BUCKET/DIR --target=s3://BUCKET/DIR/index.txt | ||
| ``` | ||
|
|
||
| `--target` is optional. If not provided, the index will be saved to `{--dir}/index.txt`. | ||
|
|
||
| You can then use `mettagrid.map.load_random_from_index.LoadRandomFromIndex` to load a random map from the index. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from mettagrid.config.room.room import Room | ||
| from mettagrid.map.utils.storable_map import StorableMap | ||
|
|
||
| from .scene import SceneCfg, make_scene | ||
|
|
||
|
|
||
| # Note that this class can't be a scene, because the width and height come from the stored data. | ||
| class Load(Room): | ||
| """ | ||
| Load a pregenerated map from a URI (file or S3 object). | ||
|
|
||
| See also: `FromS3Dir` for picking a random map from a directory of pregenerated maps. | ||
| """ | ||
|
|
||
| def __init__(self, uri: str, extra_root: SceneCfg | None = None): | ||
| super().__init__() | ||
| self._uri = uri | ||
| self._storable_map = StorableMap.from_uri(uri) | ||
|
|
||
| if extra_root is not None: | ||
| self._root = make_scene(extra_root) | ||
| else: | ||
| self._root = None | ||
|
|
||
| def build(self): | ||
| grid = self._storable_map.grid | ||
|
|
||
| if self._root is not None: | ||
| root_node = self._root.make_node(grid) | ||
| root_node.render() | ||
|
|
||
| return grid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import os | ||
| import random | ||
| from pathlib import Path | ||
|
|
||
| from mettagrid.map.load import Load | ||
| from mettagrid.map.utils import s3utils | ||
| from mettagrid.map.utils.storage import parse_file_uri | ||
|
|
||
| from .scene import SceneCfg | ||
|
|
||
|
|
||
| def get_random_map_uri(dir_uri: str) -> str: | ||
| if dir_uri.startswith("s3://"): | ||
| filenames = s3utils.list_objects(dir_uri) | ||
| filenames = [uri for uri in filenames if uri.endswith(".yaml")] | ||
| return random.choice(filenames) | ||
| else: | ||
| dirname = parse_file_uri(dir_uri) | ||
| if not os.path.isdir(dirname): | ||
| raise ValueError(f"Directory {dirname} does not exist") | ||
|
|
||
| filenames = os.listdir(dirname) | ||
| filenames = [Path(dirname) / Path(filename) for filename in filenames if filename.endswith(".yaml")] | ||
| return str(random.choice(filenames)) | ||
|
|
||
|
|
||
| class LoadRandom(Load): | ||
| """ | ||
| Load a random map from a directory, local or S3. | ||
|
|
||
| See also: `LoadRandomFromIndex` for a version that loads a random map from a pre-generated index. | ||
| """ | ||
|
|
||
| def __init__(self, dir: str, extra_root: SceneCfg | None = None): | ||
| self._dir_uri = dir | ||
|
|
||
| random_map_uri = get_random_map_uri(self._dir_uri) | ||
|
|
||
| super().__init__(random_map_uri, extra_root) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import random | ||
|
|
||
| from mettagrid.map.load import Load | ||
| from mettagrid.map.utils import storage | ||
|
|
||
| from .scene import SceneCfg | ||
|
|
||
|
|
||
| class LoadRandomFromIndex(Load): | ||
| """ | ||
| Load a random map from a list of pregenerated maps. | ||
|
|
||
| The index file can be produced with the following command: | ||
| python -m tools.index_s3_maps --dir=s3://... | ||
|
|
||
| See also: `LoadRandom` for a version that loads a random map from an S3 directory. | ||
| """ | ||
|
|
||
| def __init__(self, index_uri: str, extra_root: SceneCfg | None = None): | ||
| self._index_uri = index_uri | ||
|
|
||
| # For 10k maps in a directory we'd have to fetch 100Kb of index data. | ||
| # (Can we optimize this further by caching?) | ||
| index = storage.load_from_uri(self._index_uri) | ||
| index = index.split("\n") | ||
| random_map_uri = random.choice(index) | ||
|
|
||
| super().__init__(random_map_uri, extra_root) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.