浏览代码

Enable pylint and fix a few things (#2767)

* enable pylint, disable some messages and fix a few

* SAC memories in init
/develop-newnormalization
GitHub 5 年前
当前提交
c6c01a03
共有 15 个文件被更改,包括 106 次插入63 次删除
  1. 14
      .pre-commit-config.yaml
  2. 2
      gym-unity/gym_unity/envs/__init__.py
  3. 1
      ml-agents-envs/mlagents/envs/brain.py
  4. 37
      ml-agents-envs/mlagents/envs/environment.py
  5. 1
      ml-agents-envs/mlagents/envs/mock_communicator.py
  6. 1
      ml-agents-envs/mlagents/envs/rpc_communicator.py
  7. 13
      ml-agents-envs/mlagents/envs/timers.py
  8. 1
      ml-agents/mlagents/trainers/barracuda.py
  9. 18
      ml-agents/mlagents/trainers/components/reward_signals/extrinsic/signal.py
  10. 12
      ml-agents/mlagents/trainers/learn.py
  11. 11
      ml-agents/mlagents/trainers/ppo/multi_gpu_policy.py
  12. 6
      ml-agents/mlagents/trainers/sac/models.py
  13. 1
      ml-agents/mlagents/trainers/tensorflow_to_barracuda.py
  14. 2
      ml-agents/mlagents/trainers/tf_policy.py
  15. 49
      .pylintrc

14
.pre-commit-config.yaml


hooks:
- id: python-check-mock-methods
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.4.3
hooks:
- id: pylint
exclude: >
(?x)^(
.*_pb2.py|
.*_pb2_grpc.py|
.*/tests/.*
)$
require_serial: true
# "Local" hooks, see https://pre-commit.com/#repository-local-hooks
- repo: local
hooks:

2
gym-unity/gym_unity/envs/__init__.py


"""Sets the seed for this env's random number generator(s).
Currently not implemented.
"""
logger.warn("Could not seed environment %s", self.name)
logger.warning("Could not seed environment %s", self.name)
return
def _check_agents(self, n_agents):

1
ml-agents-envs/mlagents/envs/brain.py


for x in agent_info_list
]
vis_obs += [obs]
total_num_actions = sum(brain_params.vector_action_space_size)
mask_actions = np.ones((len(agent_info_list), total_num_actions))
for agent_index, agent_info in enumerate(agent_info_list):

37
ml-agents-envs/mlagents/envs/environment.py


) from perm
else:
"""
Comments for future maintenance:
xvfb-run is a wrapper around Xvfb, a virtual xserver where all
rendering is done to virtual memory. It automatically creates a
new virtual server automatically picking a server number `auto-servernum`.
The server is passed the arguments using `server-args`, we are telling
Xvfb to create Screen number 0 with width 640, height 480 and depth 24 bits.
Note that 640 X 480 are the default width and height. The main reason for
us to add this is because we'd like to change the depth from the default
of 8 bits to 24.
Unfortunately, this means that we will need to pass the arguments through
a shell which is why we set `shell=True`. Now, this adds its own
complications. E.g SIGINT can bounce off the shell and not get propagated
to the child processes. This is why we add `exec`, so that the shell gets
launched, the arguments are passed to `xvfb-run`. `exec` replaces the shell
we created with `xvfb`.
"""
# Comments for future maintenance:
# xvfb-run is a wrapper around Xvfb, a virtual xserver where all
# rendering is done to virtual memory. It automatically creates a
# new virtual server automatically picking a server number `auto-servernum`.
# The server is passed the arguments using `server-args`, we are telling
# Xvfb to create Screen number 0 with width 640, height 480 and depth 24 bits.
# Note that 640 X 480 are the default width and height. The main reason for
# us to add this is because we'd like to change the depth from the default
# of 8 bits to 24.
# Unfortunately, this means that we will need to pass the arguments through
# a shell which is why we set `shell=True`. Now, this adds its own
# complications. E.g SIGINT can bounce off the shell and not get propagated
# to the child processes. This is why we add `exec`, so that the shell gets
# launched, the arguments are passed to `xvfb-run`. `exec` replaces the shell
# we created with `xvfb`.
#
docker_ls = (
"exec xvfb-run --auto-servernum"
" --server-args='-screen 0 640x480x24'"

if len(arr) == 0:
return arr
if isinstance(arr[0], np.ndarray):
# pylint: disable=no-member
# pylint: disable=not-an-iterable
arr = [item for sublist in arr for item in sublist]
arr = [float(x) for x in arr]
return arr

"""
try:
# A negative value -N indicates that the child was terminated by signal N (POSIX only).
s = signal.Signals(-returncode)
s = signal.Signals(-returncode) # pylint: disable=no-member
return s.name
except Exception:
# Should generally be a ValueError, but catch everything just in case.

1
ml-agents-envs/mlagents/envs/mock_communicator.py


:int base_port: Baseline port number to connect to Unity environment over. worker_id increments over this.
:int worker_id: Number to add to communication port (5005) [0]. Used for asynchronous agent scenarios.
"""
super().__init__()
self.is_discrete = discrete_action
self.steps = 0
self.visual_inputs = visual_inputs

1
ml-agents-envs/mlagents/envs/rpc_communicator.py


:int base_port: Baseline port number to connect to Unity environment over. worker_id increments over this.
:int worker_id: Number to add to communication port (5005) [0]. Used for asynchronous agent scenarios.
"""
super().__init__(worker_id, base_port)
self.port = base_port + worker_id
self.worker_id = worker_id
self.timeout_wait = timeout_wait

13
ml-agents-envs/mlagents/envs/timers.py


# # Unity ML-Agents Toolkit
import math
from time import perf_counter
from contextlib import contextmanager
from typing import Any, Callable, Dict, Generator, List, TypeVar
"""
Lightweight, hierarchical timers for profiling sections of code.

The decorator and contextmanager are equivalent; the context manager may be more useful if you want more control
over the timer name, or are splitting up multiple sections of a large function.
"""
import math
from time import perf_counter
from contextlib import contextmanager
from typing import Any, Callable, Dict, Generator, List, TypeVar
class TimerNode:

1
ml-agents/mlagents/trainers/barracuda.py


# pylint: skip-file
from __future__ import print_function
from collections import defaultdict
import numpy as np

18
ml-agents/mlagents/trainers/components/reward_signals/extrinsic/signal.py


from mlagents.envs.brain import BrainInfo
from mlagents.trainers.components.reward_signals import RewardSignal, RewardSignalResult
from mlagents.trainers.tf_policy import TFPolicy
from mlagents.trainers.models import LearningModel
def __init__(
self,
policy: TFPolicy,
policy_model: LearningModel,
strength: float,
gamma: float,
):
"""
The extrinsic reward generator. Returns the reward received by the environment
:param policy: The Policy object (e.g. PPOPolicy) that this Reward Signal will apply to.
:param strength: The strength of the reward. The reward's raw value will be multiplied by this value.
:param gamma: The time discounting factor used for this reward.
:return: An ExtrinsicRewardSignal object.
"""
super().__init__(policy, policy_model, strength, gamma)
@classmethod
def check_config(
cls, config_dict: Dict[str, Any], param_keys: List[str] = None

12
ml-agents/mlagents/trainers/learn.py


)
docker_training = docker_target_name is not None
if docker_training and env_path is not None:
"""
Comments for future maintenance:
Some OS/VM instances (e.g. COS GCP Image) mount filesystems
with COS flag which prevents execution of the Unity scene,
to get around this, we will copy the executable into the
container.
"""
# Comments for future maintenance:
# Some OS/VM instances (e.g. COS GCP Image) mount filesystems
# with COS flag which prevents execution of the Unity scene,
# to get around this, we will copy the executable into the
# container.
# Navigate in docker path and find env_path and copy it.
env_path = prepare_for_docker_run(docker_target_name, env_path)
seed_count = 10000

11
ml-agents/mlagents/trainers/ppo/multi_gpu_policy.py


class MultiGpuPPOPolicy(PPOPolicy):
def __init__(self, seed, brain, trainer_params, is_training, load):
"""
Policy for Proximal Policy Optimization Networks with multi-GPU training
:param seed: Random seed.
:param brain: Assigned Brain object.
:param trainer_params: Defined training parameters.
:param is_training: Whether the model should be trained.
:param load: Whether a pre-trained model will be loaded or a new one created.
"""
super().__init__(seed, brain, trainer_params, is_training, load)
def create_model(
self, brain, trainer_params, reward_signal_configs, is_training, load, seed
):

6
ml-agents/mlagents/trainers/sac/models.py


import logging
import numpy as np
from typing import Optional
import tensorflow as tf
from mlagents.trainers.models import LearningModel, LearningRateSchedule, EncoderType

self.stream_names = stream_names
self.h_size = h_size
self.activ_fn = self.swish
self.policy_memory_in: Optional[tf.Tensor] = None
self.value_memory_in: Optional[tf.Tensor] = None
self.q1_memory_in: Optional[tf.Tensor] = None
self.q2_memory_in: Optional[tf.Tensor] = None
def get_vars(self, scope):
return tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)

1
ml-agents/mlagents/trainers/tensorflow_to_barracuda.py


# pylint: skip-file
from __future__ import print_function
import numpy as np
import struct # convert from Python values and C structs

2
ml-agents/mlagents/trainers/tf_policy.py


if done
]
)
run_out = self.evaluate(brain_info)
run_out = self.evaluate(brain_info) # pylint: disable=assignment-from-no-return
self.save_memories(brain_info.agents, run_out.get("memory_out"))
return ActionInfo(
action=run_out.get("action"),

49
.pylintrc


[MASTER]
# Add files or directories to the blacklist. They should be base names, not
# paths.
ignore=CVS
[MESSAGES CONTROL]
#enable=
disable =
# C0301: Line too long
# C0330: Wrong hanging indentation before block
# disabled because black handles this
C0301,C0330,
# C0115: Missing class docstring
# C0114: Missing module docstring
C0115,C0114,
# All convention and refactor for now
C,R,
# W1201: Specify string format arguments as logging function parameters
# W1202: Use % formatting in logging functions and pass the % parameters as arguments
W1201,W1202,
# W0612: Unused variable
# W0613: Unused argument
W0612, W0613,
# W0107: Unnecessary pass statement
W0107,
# W0511 TODO
W0511,
# W0703: Catching too general exception Exception
W0703,
# W0201: Attribute '...' defined outside __init__
W0201,
# We should fix these up ASAP
# W0221: Parameters differ from overridden
W0221,
# E0401: Unable to import...
# E0611: No name '...' in module '...'
# need to look into these, probably namespace packages
E0401, E0611
正在加载...
取消
保存