Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-commit #415

Merged
merged 11 commits into from
Jun 29, 2023
  •  
  •  
  •  
22 changes: 22 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://pre-commit.com
# This GitHub Action assumes that the repo contains a valid .pre-commit-config.yaml file.
---
name: pre-commit
on:
pull_request:
push:
branches: [master]

permissions:
contents: read

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- run: pip install pre-commit
- run: pre-commit --version
- run: pre-commit install
- run: pre-commit run --all-files
52 changes: 52 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
repos:
- repo: https://github.com/python/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
hooks:
- id: codespell
args:
- --skip=*.css,*.js,*.map,*.scss,*.svg,*.ipynb
- --ignore-words-list=magent
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
args:
- '--per-file-ignores=*/__init__.py:F401'
- --extend-ignore=E203,W605,F841,E731,E741,F401 # TODO: fix some of these
- --max-complexity=205
- --max-line-length=300
- --show-source
- --statistics
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ["--py37-plus"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: mixed-line-ending
args: ["--fix=lf"]
- repo: https://github.com/pycqa/pydocstyle
rev: 6.3.0
hooks:
- id: pydocstyle
args:
- --source
- --explain
- --convention=google
- --count
# TODO: Remove ignoring rules D101, D102, D103, D105
- --add-ignore=D100,D107,D101,D102,D103,D105
exclude: "__init__.py$|^metaworld.tests|^docs"
additional_dependencies: ["tomli"]
18 changes: 11 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ To be submitted, a pull request must satisfy the following criteria:
These criteria may be satisfied in any order, but in practice your PR is unlikely to get attention from contributors until 1-3 are satisfied. Maintainer attention is a scarce resource, so generally maintainers wait for a review from a non-maintainer contributor before reviewing your PR.

## Preparing your repo to make contributions
After following the standard Meta-World setup steps, make sure to run to install the pre-commit hooks into your repository. pre-commit helps streamline the pull request process by catching basic problems locally before they are checked by the CI.
First, install the Metaworld locally in editable mode, with testing dependencies:

```
pip install -e .[dev]
```

Then, make sure to run to install the pre-commit hooks into your repository. pre-commit helps streamline the pull request process by catching basic problems locally before they are checked by the CI.

To setup pre-commit in your repo:
```sh
Expand All @@ -42,12 +48,10 @@ To setup pre-commit in your repo:
# pipenv shell
# poetry shell
# source venv/bin/activate
pre-commit install -t pre-commit
pre-commit install -t pre-push
pre-commit install -t commit-msg
pre-commit install
```

Once you've installed pre-commit, it will automatically run every time you type `git commit`.
Once you've installed pre-commit, it will automatically run every time you type `git commit`, or you can run it manually using `pre-commit run --all-files`.

## Code style
The Python code in metaworld conforms to the [PEP8](https://www.python.org/dev/peps/pep-0008/) standard. Please read and understand it in detail.
Expand Down Expand Up @@ -143,7 +147,7 @@ These are Meta-World specific rules which are not part of the aforementioned sty

* When using external dependencies, use the `import` statement only to import whole modules, not individual classes or functions.

This applies to both packages from the standard library and 3rd-party dependencies. If a package has a long or cumbersome full path, or is used very frequently (e.g. `numpy`, `tensorflow`), you may use the keyword `as` to create a file-specific name which makes sense. Additionally, you should always follow the community concensus short names for common dependencies (see below).
This applies to both packages from the standard library and 3rd-party dependencies. If a package has a long or cumbersome full path, or is used very frequently (e.g. `numpy`, `tensorflow`), you may use the keyword `as` to create a file-specific name which makes sense. Additionally, you should always follow the community consensus short names for common dependencies (see below).

*Do*
```python
Expand Down Expand Up @@ -172,7 +176,7 @@ These are Meta-World specific rules which are not part of the aforementioned sty
m = MLPModel(output_dim=2)
```

*Known community-concensus imports*
*Known community-consensus imports*
```python
import numpy as np
import matplotlib.pyplot as plt
Expand Down
2 changes: 1 addition & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ or specify the corresponding path through the MJKEY_PATH variable:
make run-headless RUN_CMD="..." MJKEY_PATH="/home/user/mjkey.txt"
```

If you require to pass addtional arguments to the the make commands, you can
If you require to pass additional arguments to the make commands, you can
use the variable ADD_ARGS, for example:
```
make build-headless ADD_ARGS="--build-arg MY_VAR=123"
Expand Down
96 changes: 50 additions & 46 deletions metaworld/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from collections import OrderedDict
from typing import List, NamedTuple, Type

import metaworld.envs.mujoco.env_dict as _env_dict
import numpy as np

import metaworld.envs.mujoco.env_dict as _env_dict

EnvName = str

__version__ = "2.0.0"


class Task(NamedTuple):
"""All data necessary to describe a single MDP.
Expand All @@ -26,6 +29,7 @@ class MetaWorldEnv:
Takes no arguments to its constructor, and raises an exception if used
before `set_task` is called.
"""

def set_task(self, task: Task) -> None:
"""Set the task.

Expand All @@ -40,17 +44,18 @@ class Benchmark(abc.ABC):

When used to evaluate an algorithm, only a single instance should be used.
"""

@abc.abstractmethod
def __init__(self):
pass

@property
def train_classes(self) -> 'OrderedDict[EnvName, Type]':
def train_classes(self) -> "OrderedDict[EnvName, Type]":
"""Get all of the environment classes used for training."""
return self._train_classes

@property
def test_classes(self) -> 'OrderedDict[EnvName, Type]':
def test_classes(self) -> "OrderedDict[EnvName, Type]":
"""Get all of the environment classes used for testing."""
return self._test_classes

Expand Down Expand Up @@ -80,15 +85,15 @@ def _make_tasks(classes, args_kwargs, kwargs_override, seed=None):
st0 = np.random.get_state()
np.random.seed(seed)
tasks = []
for (env_name, args) in args_kwargs.items():
assert len(args['args']) == 0
for env_name, args in args_kwargs.items():
assert len(args["args"]) == 0
env_cls = classes[env_name]
env = env_cls()
env._freeze_rand_vec = False
env._set_task_called = True
rand_vecs = []
kwargs = args['kwargs'].copy()
del kwargs['task_id']
kwargs = args["kwargs"].copy()
del kwargs["task_id"]
env._set_task_inner(**kwargs)
for _ in range(_N_GOALS):
env.reset()
Expand All @@ -98,8 +103,8 @@ def _make_tasks(classes, args_kwargs, kwargs_override, seed=None):

env.close()
for rand_vec in rand_vecs:
kwargs = args['kwargs'].copy()
del kwargs['task_id']
kwargs = args["kwargs"].copy()
del kwargs["task_id"]
kwargs.update(dict(rand_vec=rand_vec, env_cls=env_cls))
kwargs.update(kwargs_override)
tasks.append(_encode_task(env_name, kwargs))
Expand All @@ -109,84 +114,82 @@ def _make_tasks(classes, args_kwargs, kwargs_override, seed=None):


def _ml1_env_names():
tasks = list(_env_dict.ML1_V2['train'])
tasks = list(_env_dict.ML1_V2["train"])
assert len(tasks) == 50
return tasks


class ML1(Benchmark):

ENV_NAMES = _ml1_env_names()

def __init__(self, env_name, seed=None):
super().__init__()
if not env_name in _env_dict.ALL_V2_ENVIRONMENTS:
if env_name not in _env_dict.ALL_V2_ENVIRONMENTS:
raise ValueError(f"{env_name} is not a V2 environment")
cls = _env_dict.ALL_V2_ENVIRONMENTS[env_name]
self._train_classes = OrderedDict([(env_name, cls)])
self._test_classes = self._train_classes
self._train_ = OrderedDict([(env_name, cls)])
args_kwargs = _env_dict.ML1_args_kwargs[env_name]

self._train_tasks = _make_tasks(self._train_classes,
{env_name: args_kwargs},
_ML_OVERRIDE,
seed=seed)
self._train_tasks = _make_tasks(
self._train_classes, {env_name: args_kwargs}, _ML_OVERRIDE, seed=seed
)
self._test_tasks = _make_tasks(
self._test_classes, {env_name: args_kwargs},
self._test_classes,
{env_name: args_kwargs},
_ML_OVERRIDE,
seed=(seed + 1 if seed is not None else seed))
seed=(seed + 1 if seed is not None else seed),
)


class MT1(Benchmark):

ENV_NAMES = _ml1_env_names()

def __init__(self, env_name, seed=None):
super().__init__()
if not env_name in _env_dict.ALL_V2_ENVIRONMENTS:
if env_name not in _env_dict.ALL_V2_ENVIRONMENTS:
raise ValueError(f"{env_name} is not a V2 environment")
cls = _env_dict.ALL_V2_ENVIRONMENTS[env_name]
self._train_classes = OrderedDict([(env_name, cls)])
self._test_classes = self._train_classes
self._train_ = OrderedDict([(env_name, cls)])
args_kwargs = _env_dict.ML1_args_kwargs[env_name]

self._train_tasks = _make_tasks(self._train_classes,
{env_name: args_kwargs},
_MT_OVERRIDE,
seed=seed)
self._train_tasks = _make_tasks(
self._train_classes, {env_name: args_kwargs}, _MT_OVERRIDE, seed=seed
)
self._test_tasks = []


class ML10(Benchmark):
def __init__(self, seed=None):
super().__init__()
self._train_classes = _env_dict.ML10_V2['train']
self._test_classes = _env_dict.ML10_V2['test']
self._train_classes = _env_dict.ML10_V2["train"]
self._test_classes = _env_dict.ML10_V2["test"]
train_kwargs = _env_dict.ml10_train_args_kwargs
self._train_tasks = _make_tasks(self._train_classes, train_kwargs,
_ML_OVERRIDE,
seed=seed)
self._train_tasks = _make_tasks(
self._train_classes, train_kwargs, _ML_OVERRIDE, seed=seed
)
test_kwargs = _env_dict.ml10_test_args_kwargs
self._test_tasks = _make_tasks(self._test_classes, test_kwargs,
_ML_OVERRIDE,
seed=seed)
self._test_tasks = _make_tasks(
self._test_classes, test_kwargs, _ML_OVERRIDE, seed=seed
)


class ML45(Benchmark):
def __init__(self, seed=None):
super().__init__()
self._train_classes = _env_dict.ML45_V2['train']
self._test_classes = _env_dict.ML45_V2['test']
self._train_classes = _env_dict.ML45_V2["train"]
self._test_classes = _env_dict.ML45_V2["test"]
train_kwargs = _env_dict.ml45_train_args_kwargs
self._train_tasks = _make_tasks(self._train_classes, train_kwargs,
_ML_OVERRIDE,
seed=seed)
self._train_tasks = _make_tasks(
self._train_classes, train_kwargs, _ML_OVERRIDE, seed=seed
)
test_kwargs = _env_dict.ml45_test_args_kwargs
self._test_tasks = _make_tasks(self._test_classes, test_kwargs,
_ML_OVERRIDE,
seed=seed)
self._test_tasks = _make_tasks(
self._test_classes, test_kwargs, _ML_OVERRIDE, seed=seed
)


class MT10(Benchmark):
Expand All @@ -195,9 +198,9 @@ def __init__(self, seed=None):
self._train_classes = _env_dict.MT10_V2
self._test_classes = OrderedDict()
train_kwargs = _env_dict.MT10_V2_ARGS_KWARGS
self._train_tasks = _make_tasks(self._train_classes, train_kwargs,
_MT_OVERRIDE,
seed=seed)
self._train_tasks = _make_tasks(
self._train_classes, train_kwargs, _MT_OVERRIDE, seed=seed
)
self._test_tasks = []


Expand All @@ -207,9 +210,10 @@ def __init__(self, seed=None):
self._train_classes = _env_dict.MT50_V2
self._test_classes = OrderedDict()
train_kwargs = _env_dict.MT50_V2_ARGS_KWARGS
self._train_tasks = _make_tasks(self._train_classes, train_kwargs,
_MT_OVERRIDE,
seed=seed)
self._train_tasks = _make_tasks(
self._train_classes, train_kwargs, _MT_OVERRIDE, seed=seed
)
self._test_tasks = []


__all__ = ["ML1", "MT1", "ML10", "MT10", "ML45", "MT50"]
10 changes: 5 additions & 5 deletions metaworld/envs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from metaworld.envs.mujoco.env_dict import (ALL_V2_ENVIRONMENTS_GOAL_HIDDEN,
ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE
)
from metaworld.envs.mujoco.env_dict import (
ALL_V2_ENVIRONMENTS_GOAL_HIDDEN,
ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE,
)

__all__ = ['ALL_V2_ENVIRONMENTS_GOAL_HIDDEN',
'ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE']
__all__ = ["ALL_V2_ENVIRONMENTS_GOAL_HIDDEN", "ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE"]
4 changes: 2 additions & 2 deletions metaworld/envs/asset_path_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

ENV_ASSET_DIR_V1 = os.path.join(os.path.dirname(__file__), 'assets_v1')
ENV_ASSET_DIR_V2 = os.path.join(os.path.dirname(__file__), 'assets_v2')
ENV_ASSET_DIR_V1 = os.path.join(os.path.dirname(__file__), "assets_v1")
ENV_ASSET_DIR_V2 = os.path.join(os.path.dirname(__file__), "assets_v2")


def full_v1_path_for(file_name):
Expand Down
Loading