Revert all changes for python 2

+ Add makefile and pytype
This commit is contained in:
Antonin Raffin 2020-01-22 16:18:27 +01:00
parent 8152b34aaa
commit 37f9f13684
10 changed files with 31 additions and 26 deletions

1
.gitignore vendored
View file

@ -12,6 +12,7 @@ __pycache__/
_build/ _build/
*.npz *.npz
*.pth *.pth
.pytype/
# Setuptools distribution and build folders. # Setuptools distribution and build folders.
/dist/ /dist/

13
Makefile Normal file
View file

@ -0,0 +1,13 @@
SHELL=/bin/bash
pytest:
./scripts/run_tests.sh
pytype:
pytype
doc:
cd docs && make html
spelling:
cd docs && make spelling

View file

@ -15,3 +15,6 @@ filterwarnings =
# Gym warnings # Gym warnings
ignore:Parameters to load are deprecated.:DeprecationWarning ignore:Parameters to load are deprecated.:DeprecationWarning
ignore:the imp module is deprecated in favour of importlib:PendingDeprecationWarning ignore:the imp module is deprecated in favour of importlib:PendingDeprecationWarning
[pytype]
inputs = torchy_baselines

View file

@ -18,6 +18,7 @@ setup(name='torchy_baselines',
'pytest-cov', 'pytest-cov',
'pytest-env', 'pytest-env',
'pytest-xdist', 'pytest-xdist',
'pytype',
], ],
'docs': [ 'docs': [
'sphinx', 'sphinx',

View file

@ -1,9 +1,9 @@
import time import time
from abc import ABCMeta, abstractmethod
from collections import deque
import os import os
import io import io
import zipfile import zipfile
from abc import ABC, abstractmethod
from collections import deque
import gym import gym
import torch as th 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 from torchy_baselines.common.save_util import data_to_json, json_to_data
class BaseRLModel(object): class BaseRLModel(ABC):
""" """
The base RL model 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 :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) Default: -1 (only sample at the beginning of the rollout)
""" """
__metaclass__ = ABCMeta
def __init__(self, policy, env, policy_base, policy_kwargs=None, def __init__(self, policy, env, policy_base, policy_kwargs=None,
verbose=0, device='auto', support_multi_env=False, verbose=0, device='auto', support_multi_env=False,
create_eval_env=False, monitor_wrapper=True, seed=None, create_eval_env=False, monitor_wrapper=True, seed=None,

View file

@ -272,7 +272,7 @@ def getkvs():
return Logger.CURRENT.name2val return Logger.CURRENT.name2val
def log(*args, **kwargs): def log(*args, level=INFO):
""" """
Write the sequence of args, with no separators, Write the sequence of args, with no separators,
to the console and output files (if you've configured an output file). 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 args: (list) log the arguments
:param level: (int) the logging level (can be DEBUG=10, INFO=20, WARN=30, ERROR=40, DISABLED=50) :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) Logger.CURRENT.log(*args, level=level)
@ -424,7 +423,7 @@ class Logger(object):
self.name2val.clear() self.name2val.clear()
self.name2cnt.clear() self.name2cnt.clear()
def log(self, *args, **kwargs): def log(self, *args, level=INFO):
""" """
Write the sequence of args, with no separators, Write the sequence of args, with no separators,
to the console and output files (if you've configured an output file). 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 args: (list) log the arguments
:param level: (int) the logging level (can be DEBUG=10, INFO=20, WARN=30, ERROR=40, DISABLED=50) :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: if self.level <= level:
self._do_log(args) self._do_log(args)

View file

@ -1,6 +1,6 @@
from abc import ABCMeta, abstractmethod
import inspect import inspect
import pickle import pickle
from abc import ABC, abstractmethod
import cloudpickle import cloudpickle
@ -27,7 +27,7 @@ class NotSteppingError(Exception):
Exception.__init__(self, msg) Exception.__init__(self, msg)
class VecEnv(object): class VecEnv(ABC):
""" """
An abstract asynchronous, vectorized environment. An abstract asynchronous, vectorized environment.
@ -39,8 +39,6 @@ class VecEnv(object):
'render.modes': ['human', 'rgb_array'] 'render.modes': ['human', 'rgb_array']
} }
__metaclass__ = ABCMeta
def __init__(self, num_envs, observation_space, action_space): def __init__(self, num_envs, observation_space, action_space):
self.num_envs = num_envs self.num_envs = num_envs
self.observation_space = observation_space self.observation_space = observation_space
@ -112,7 +110,7 @@ class VecEnv(object):
raise NotImplementedError() raise NotImplementedError()
@abstractmethod @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. Call instance methods of vectorized environments.
@ -222,8 +220,8 @@ class VecEnvWrapper(VecEnv):
def set_attr(self, attr_name, value, indices=None): def set_attr(self, attr_name, value, indices=None):
return self.venv.set_attr(attr_name, value, indices) return self.venv.set_attr(attr_name, value, indices)
def env_method(self, 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, **method_kwargs) return self.venv.env_method(method_name, *method_args, indices=indices, **method_kwargs)
def __getattr__(self, name): def __getattr__(self, name):
"""Find attribute from wrapped venv(s) if this wrapper does not have it. """Find attribute from wrapped venv(s) if this wrapper does not have it.

View file

@ -99,11 +99,8 @@ class DummyVecEnv(VecEnv):
for env_i in target_envs: for env_i in target_envs:
setattr(env_i, attr_name, value) 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.""" """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) target_envs = self._get_target_envs(indices)
return [getattr(env_i, method_name)(*method_args, **method_kwargs) for env_i in target_envs] return [getattr(env_i, method_name)(*method_args, **method_kwargs) for env_i in target_envs]

View file

@ -153,8 +153,7 @@ class SubprocVecEnv(VecEnv):
for pipe in self.remotes: for pipe in self.remotes:
# gather images from subprocesses # gather images from subprocesses
# `mode` will be taken into account later # `mode` will be taken into account later
kwargs.update({'mode': 'rgb_array'}) pipe.send(('render', (args, {'mode': 'rgb_array', **kwargs})))
pipe.send(('render', (args, kwargs)))
imgs = [pipe.recv() for pipe in self.remotes] imgs = [pipe.recv() for pipe in self.remotes]
# Create a big image by tiling images from subprocesses # Create a big image by tiling images from subprocesses
bigimg = tile_images(imgs) bigimg = tile_images(imgs)
@ -199,11 +198,8 @@ class SubprocVecEnv(VecEnv):
assert len(seed) == len(indices) assert len(seed) == len(indices)
return [self.env_method('seed', seed[i], indices=i) for i in 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.""" """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) target_remotes = self._get_target_remotes(indices)
for remote in target_remotes: for remote in target_remotes:
remote.send(('env_method', (method_name, method_args, method_kwargs))) remote.send(('env_method', (method_name, method_args, method_kwargs)))

View file