1
0
mirror of https://github.com/gryf/coach.git synced 2026-01-08 23:04:15 +01:00

Cleanup imports.

Till now, most of the modules were importing all of the module objects
(variables, classes, functions, other imports) into module namespace,
which potentially could (and was) cause of unintentional use of class or
methods, which was indirect imported.

With this patch, all the star imports were substituted with top-level
module, which provides desired class or function.

Besides, all imports where sorted (where possible) in a way pep8[1]
suggests - first are imports from standard library, than goes third
party imports (like numpy, tensorflow etc) and finally coach modules.
All of those sections are separated by one empty line.

[1] https://www.python.org/dev/peps/pep-0008/#imports
This commit is contained in:
Roman Dobosz
2018-04-12 19:46:32 +02:00
parent cafa152382
commit 1b095aeeca
75 changed files with 1169 additions and 1139 deletions

View File

@@ -1,5 +1,5 @@
#
# Copyright (c) 2017 Intel Corporation
# Copyright (c) 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,15 +13,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from logger import *
from utils import Enum, get_open_port
from environments.gym_environment_wrapper import *
from environments.doom_environment_wrapper import *
from environments.carla_environment_wrapper import *
from environments.gym_environment_wrapper import GymEnvironmentWrapper
from environments.doom_environment_wrapper import DoomEnvironmentWrapper
from environments.carla_environment_wrapper import CarlaEnvironmentWrapper
import utils
class EnvTypes(Enum):
class EnvTypes(utils.Enum):
Doom = "DoomEnvironmentWrapper"
Gym = "GymEnvironmentWrapper"
Carla = "CarlaEnvironmentWrapper"
@@ -31,6 +29,3 @@ def create_environment(tuning_parameters):
env_type_name, env_type = EnvTypes().verify(tuning_parameters.env.type)
env = eval(env_type)(tuning_parameters)
return env

View File

@@ -1,34 +1,31 @@
import logging
import os
import signal
import subprocess
import sys
from os import path, environ
try:
if 'CARLA_ROOT' in environ:
sys.path.append(path.join(environ.get('CARLA_ROOT'), 'PythonClient'))
from carla.client import CarlaClient
from carla.settings import CarlaSettings
from carla.tcp import TCPConnectionError
from carla.sensor import Camera
from carla.client import VehicleControl
except ImportError:
from logger import failed_imports
failed_imports.append("CARLA")
import numpy as np
import time
import logging
import subprocess
import signal
from environments.environment_wrapper import EnvironmentWrapper
from utils import *
from logger import screen, logger
from PIL import Image
import logger
try:
if 'CARLA_ROOT' in os.environ:
sys.path.append(os.path.join(os.environ.get('CARLA_ROOT'),
'PythonClient'))
from carla import client as carla_client
from carla import settings as carla_settings
from carla import sensor as carla_sensor
except ImportError:
logger.failed_imports.append("CARLA")
from environments import environment_wrapper as ew
import utils
# enum of the available levels and their path
class CarlaLevel(Enum):
class CarlaLevel(utils.Enum):
TOWN1 = "/Game/Maps/Town01"
TOWN2 = "/Game/Maps/Town02"
key_map = {
'BRAKE': (274,), # down arrow
'GAS': (273,), # up arrow
@@ -41,16 +38,16 @@ key_map = {
}
class CarlaEnvironmentWrapper(EnvironmentWrapper):
class CarlaEnvironmentWrapper(ew.EnvironmentWrapper):
def __init__(self, tuning_parameters):
EnvironmentWrapper.__init__(self, tuning_parameters)
ew.EnvironmentWrapper.__init__(self, tuning_parameters)
self.tp = tuning_parameters
# server configuration
self.server_height = self.tp.env.server_height
self.server_width = self.tp.env.server_width
self.port = get_open_port()
self.port = utils.get_open_port()
self.host = 'localhost'
self.map = CarlaLevel().get(self.tp.env.level)
@@ -70,7 +67,7 @@ class CarlaEnvironmentWrapper(EnvironmentWrapper):
self.settings = fp.read()
else:
# hard coded settings
self.settings = CarlaSettings()
self.settings = carla_settings.CarlaSettings()
self.settings.set(
SynchronousMode=True,
SendNonPlayerAgentsInfo=False,
@@ -80,7 +77,7 @@ class CarlaEnvironmentWrapper(EnvironmentWrapper):
self.settings.randomize_seeds()
# add cameras
camera = Camera('CameraRGB')
camera = carla_sensor.Camera('CameraRGB')
camera.set_image_size(self.width, self.height)
camera.set_position(200, 0, 140)
camera.set_rotation(0, 0, 0)
@@ -92,7 +89,7 @@ class CarlaEnvironmentWrapper(EnvironmentWrapper):
logging.disable(40)
# open the client
self.game = CarlaClient(self.host, self.port, timeout=99999999)
self.game = carla_client.CarlaClient(self.host, self.port, timeout=99999999)
self.game.connect()
scene = self.game.load_settings(self.settings)
@@ -141,12 +138,12 @@ class CarlaEnvironmentWrapper(EnvironmentWrapper):
self.renderer.create_screen(image.shape[1], image.shape[0])
def _open_server(self):
log_path = path.join(logger.experiments_path, "CARLA_LOG_{}.txt".format(self.port))
log_path = os.path.join(logger.logger.experiments_path, "CARLA_LOG_{}.txt".format(self.port))
with open(log_path, "wb") as out:
cmd = [path.join(environ.get('CARLA_ROOT'), 'CarlaUE4.sh'), self.map,
"-benchmark", "-carla-server", "-fps=10", "-world-port={}".format(self.port),
"-windowed -ResX={} -ResY={}".format(self.server_width, self.server_height),
"-carla-no-hud"]
cmd = [os.path.join(os.environ.get('CARLA_ROOT'), 'CarlaUE4.sh'), self.map,
"-benchmark", "-carla-server", "-fps=10", "-world-port={}".format(self.port),
"-windowed -ResX={} -ResY={}".format(self.server_width, self.server_height),
"-carla-no-hud"]
if self.config:
cmd.append("-carla-settings={}".format(self.config))
p = subprocess.Popen(cmd, stdout=out, stderr=out)
@@ -201,7 +198,7 @@ class CarlaEnvironmentWrapper(EnvironmentWrapper):
action = action_idx
self.last_action_idx = action
self.control = VehicleControl()
self.control = carla_client.VehicleControl()
self.control.throttle = np.clip(action[0], 0, 1)
self.control.steer = np.clip(action[1], -1, 1)
self.control.brake = np.abs(np.clip(action[0], -1, 0))

View File

@@ -13,23 +13,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import enum
import os
import numpy as np
import logger
try:
import vizdoom
except ImportError:
from logger import failed_imports
failed_imports.append("ViZDoom")
logger.failed_imports.append("ViZDoom")
import numpy as np
from environments.environment_wrapper import EnvironmentWrapper
from os import path, environ
from utils import *
from logger import *
from environments import environment_wrapper as ew
import utils
# enum of the available levels and their path
class DoomLevel(Enum):
class DoomLevel(utils.Enum):
BASIC = "basic.cfg"
DEFEND = "defend_the_center.cfg"
DEATHMATCH = "deathmatch.cfg"
@@ -40,6 +40,7 @@ class DoomLevel(Enum):
DEFEND_THE_LINE = "defend_the_line.cfg"
DEADLY_CORRIDOR = "deadly_corridor.cfg"
key_map = {
'NO-OP': 96, # `
'ATTACK': 13, # enter
@@ -78,15 +79,16 @@ key_map = {
}
class DoomEnvironmentWrapper(EnvironmentWrapper):
class DoomEnvironmentWrapper(ew.EnvironmentWrapper):
def __init__(self, tuning_parameters):
EnvironmentWrapper.__init__(self, tuning_parameters)
ew.EnvironmentWrapper.__init__(self, tuning_parameters)
# load the emulator with the required level
self.level = DoomLevel().get(self.tp.env.level)
self.scenarios_dir = path.join(environ.get('VIZDOOM_ROOT'), 'scenarios')
self.scenarios_dir = os.path.join(os.environ.get('VIZDOOM_ROOT'),
'scenarios')
self.game = vizdoom.DoomGame()
self.game.load_config(path.join(self.scenarios_dir, self.level))
self.game.load_config(os.path.join(self.scenarios_dir, self.level))
self.game.set_window_visible(False)
self.game.add_game_args("+vid_forcesurface 1")

View File

@@ -13,14 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import numpy as np
from utils import *
from configurations import Preset
from renderer import Renderer
import operator
import time
import numpy as np
import renderer
import utils
class EnvironmentWrapper(object):
def __init__(self, tuning_parameters):
@@ -50,7 +50,7 @@ class EnvironmentWrapper(object):
self.height = 1
self.is_state_type_image = True
self.measurements_size = 0
self.phase = RunPhase.TRAIN
self.phase = utils.RunPhase.TRAIN
self.tp = tuning_parameters
self.record_video_every = self.tp.visualization.record_video_every
self.env_id = self.tp.env.level
@@ -62,7 +62,7 @@ class EnvironmentWrapper(object):
self.wait_for_explicit_human_action = False
self.is_rendered = self.is_rendered or self.human_control
self.game_is_open = True
self.renderer = Renderer()
self.renderer = renderer.Renderer()
@property
def measurements(self):

View File

@@ -13,40 +13,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
import sys
from logger import *
import gym
import numpy as np
import time
import random
try:
import roboschool
from OpenGL import GL
except ImportError:
from logger import failed_imports
failed_imports.append("RoboSchool")
try:
from gym_extensions.continuous import mujoco
except:
from logger import failed_imports
failed_imports.append("GymExtensions")
try:
import pybullet_envs
except ImportError:
from logger import failed_imports
failed_imports.append("PyBullet")
from gym import wrappers
from utils import force_list, RunPhase
from environments.environment_wrapper import EnvironmentWrapper
from environments import environment_wrapper as ew
import utils
class GymEnvironmentWrapper(EnvironmentWrapper):
class GymEnvironmentWrapper(ew.EnvironmentWrapper):
def __init__(self, tuning_parameters):
EnvironmentWrapper.__init__(self, tuning_parameters)
ew.EnvironmentWrapper.__init__(self, tuning_parameters)
# env parameters
if ':' in self.env_id:
@@ -124,7 +102,7 @@ class GymEnvironmentWrapper(EnvironmentWrapper):
def _update_state(self):
if hasattr(self.env, 'env') and hasattr(self.env.env, 'ale'):
if self.phase == RunPhase.TRAIN and hasattr(self, 'current_ale_lives'):
if self.phase == utils.RunPhase.TRAIN and hasattr(self, 'current_ale_lives'):
# signal termination for life loss
if self.current_ale_lives != self.env.env.ale.lives():
self.done = True
@@ -146,7 +124,7 @@ class GymEnvironmentWrapper(EnvironmentWrapper):
if type(action_idx) == int and action_idx == 0:
# deal with the "reset" action 0
action = [0] * self.env.action_space.shape[0]
action = np.array(force_list(action))
action = np.array(utils.force_list(action))
# removing redundant dimensions such that the action size will match the expected action size from gym
if action.shape != self.env.action_space.shape:
action = np.squeeze(action)