diff --git a/.gitignore b/.gitignore index 4e58c84..1c61a6c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ __pycache__/ _build/ *.npz *.pth +.pytype/ # Setuptools distribution and build folders. /dist/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c3636cf --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +SHELL=/bin/bash + +pytest: + ./scripts/run_tests.sh + +pytype: + pytype + +doc: + cd docs && make html + +spelling: + cd docs && make spelling diff --git a/setup.cfg b/setup.cfg index aca686b..708de33 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,3 +15,6 @@ filterwarnings = # Gym warnings ignore:Parameters to load are deprecated.:DeprecationWarning ignore:the imp module is deprecated in favour of importlib:PendingDeprecationWarning + +[pytype] +inputs = torchy_baselines diff --git a/setup.py b/setup.py index 393174f..343967a 100644 --- a/setup.py +++ b/setup.py @@ -18,6 +18,7 @@ setup(name='torchy_baselines', 'pytest-cov', 'pytest-env', 'pytest-xdist', + 'pytype', ], 'docs': [ 'sphinx', diff --git a/torchy_baselines/common/base_class.py b/torchy_baselines/common/base_class.py index bdbef53..a1f7722 100644 --- a/torchy_baselines/common/base_class.py +++ b/torchy_baselines/common/base_class.py @@ -1,9 +1,9 @@ import time -from abc import ABCMeta, abstractmethod -from collections import deque import os import io import zipfile +from abc import ABC, abstractmethod +from collections import deque import gym import torch as th @@ -18,7 +18,7 @@ from torchy_baselines.common.evaluation import evaluate_policy from torchy_baselines.common.save_util import data_to_json, json_to_data -class BaseRLModel(object): +class BaseRLModel(ABC): """ The base RL model @@ -43,8 +43,6 @@ class BaseRLModel(object): :param sde_sample_freq: (int) Sample a new noise matrix every n steps when using SDE Default: -1 (only sample at the beginning of the rollout) """ - __metaclass__ = ABCMeta - def __init__(self, policy, env, policy_base, policy_kwargs=None, verbose=0, device='auto', support_multi_env=False, create_eval_env=False, monitor_wrapper=True, seed=None, diff --git a/torchy_baselines/common/logger.py b/torchy_baselines/common/logger.py index a909285..78845ee 100644 --- a/torchy_baselines/common/logger.py +++ b/torchy_baselines/common/logger.py @@ -272,7 +272,7 @@ def getkvs(): return Logger.CURRENT.name2val -def log(*args, **kwargs): +def log(*args, level=INFO): """ Write the sequence of args, with no separators, to the console and output files (if you've configured an output file). @@ -283,7 +283,6 @@ def log(*args, **kwargs): :param args: (list) log the arguments :param level: (int) the logging level (can be DEBUG=10, INFO=20, WARN=30, ERROR=40, DISABLED=50) """ - level = kwargs.get('level', INFO) Logger.CURRENT.log(*args, level=level) @@ -424,7 +423,7 @@ class Logger(object): self.name2val.clear() self.name2cnt.clear() - def log(self, *args, **kwargs): + def log(self, *args, level=INFO): """ Write the sequence of args, with no separators, to the console and output files (if you've configured an output file). @@ -435,7 +434,6 @@ class Logger(object): :param args: (list) log the arguments :param level: (int) the logging level (can be DEBUG=10, INFO=20, WARN=30, ERROR=40, DISABLED=50) """ - level = kwargs.get('level', INFO) if self.level <= level: self._do_log(args) diff --git a/torchy_baselines/common/vec_env/base_vec_env.py b/torchy_baselines/common/vec_env/base_vec_env.py index abc36a8..47defa9 100644 --- a/torchy_baselines/common/vec_env/base_vec_env.py +++ b/torchy_baselines/common/vec_env/base_vec_env.py @@ -1,6 +1,6 @@ -from abc import ABCMeta, abstractmethod import inspect import pickle +from abc import ABC, abstractmethod import cloudpickle @@ -27,7 +27,7 @@ class NotSteppingError(Exception): Exception.__init__(self, msg) -class VecEnv(object): +class VecEnv(ABC): """ An abstract asynchronous, vectorized environment. @@ -39,8 +39,6 @@ class VecEnv(object): 'render.modes': ['human', 'rgb_array'] } - __metaclass__ = ABCMeta - def __init__(self, num_envs, observation_space, action_space): self.num_envs = num_envs self.observation_space = observation_space @@ -112,7 +110,7 @@ class VecEnv(object): raise NotImplementedError() @abstractmethod - def env_method(self, method_name, *method_args, **method_kwargs): + def env_method(self, method_name, *method_args, indices=None, **method_kwargs): """ Call instance methods of vectorized environments. @@ -222,8 +220,8 @@ class VecEnvWrapper(VecEnv): def set_attr(self, attr_name, value, indices=None): return self.venv.set_attr(attr_name, value, indices) - def env_method(self, method_name, *method_args, **method_kwargs): - return self.venv.env_method(method_name, *method_args, **method_kwargs) + def env_method(self, method_name, *method_args, indices=None, **method_kwargs): + return self.venv.env_method(method_name, *method_args, indices=indices, **method_kwargs) def __getattr__(self, name): """Find attribute from wrapped venv(s) if this wrapper does not have it. diff --git a/torchy_baselines/common/vec_env/dummy_vec_env.py b/torchy_baselines/common/vec_env/dummy_vec_env.py index 7bfe1a5..c55ca6a 100644 --- a/torchy_baselines/common/vec_env/dummy_vec_env.py +++ b/torchy_baselines/common/vec_env/dummy_vec_env.py @@ -99,11 +99,8 @@ class DummyVecEnv(VecEnv): for env_i in target_envs: setattr(env_i, attr_name, value) - def env_method(self, method_name, *method_args, **method_kwargs): + def env_method(self, method_name, *method_args, indices=None, **method_kwargs): """Call instance methods of vectorized environments.""" - indices = method_kwargs.get('indices') - if 'indices' in method_kwargs: - del method_kwargs['indices'] target_envs = self._get_target_envs(indices) return [getattr(env_i, method_name)(*method_args, **method_kwargs) for env_i in target_envs] diff --git a/torchy_baselines/common/vec_env/subproc_vec_env.py b/torchy_baselines/common/vec_env/subproc_vec_env.py index f89cbc6..0d1ecde 100644 --- a/torchy_baselines/common/vec_env/subproc_vec_env.py +++ b/torchy_baselines/common/vec_env/subproc_vec_env.py @@ -153,8 +153,7 @@ class SubprocVecEnv(VecEnv): for pipe in self.remotes: # gather images from subprocesses # `mode` will be taken into account later - kwargs.update({'mode': 'rgb_array'}) - pipe.send(('render', (args, kwargs))) + pipe.send(('render', (args, {'mode': 'rgb_array', **kwargs}))) imgs = [pipe.recv() for pipe in self.remotes] # Create a big image by tiling images from subprocesses bigimg = tile_images(imgs) @@ -199,11 +198,8 @@ class SubprocVecEnv(VecEnv): assert len(seed) == len(indices) return [self.env_method('seed', seed[i], indices=i) for i in indices] - def env_method(self, method_name, *method_args, **method_kwargs): + def env_method(self, method_name, *method_args, indices=None, **method_kwargs): """Call instance methods of vectorized environments.""" - indices = method_kwargs.get('indices') - if 'indices' in method_kwargs: - del method_kwargs['indices'] target_remotes = self._get_target_remotes(indices) for remote in target_remotes: remote.send(('env_method', (method_name, method_args, method_kwargs))) diff --git a/torchy_baselines/py.typed b/torchy_baselines/py.typed new file mode 100644 index 0000000..e69de29