diff --git a/torchy_baselines/common/base_class.py b/torchy_baselines/common/base_class.py index a1f7722..5c780c9 100644 --- a/torchy_baselines/common/base_class.py +++ b/torchy_baselines/common/base_class.py @@ -57,7 +57,7 @@ class BaseRLModel(ABC): self.device = th.device(device) if verbose > 0: - print("Using {} device".format(self.device)) + print(f"Using {self.device} device") self.env = env # get VecNormalize object if needed @@ -317,9 +317,8 @@ class BaseRLModel(ABC): data, params, opt_params = cls._load_from_file(load_path) if 'policy_kwargs' in kwargs and kwargs['policy_kwargs'] != data['policy_kwargs']: - raise ValueError("The specified policy kwargs do not equal the stored policy kwargs." - "Stored kwargs: {}, specified kwargs: {}".format(data['policy_kwargs'], - kwargs['policy_kwargs'])) + raise ValueError(f"The specified policy kwargs do not equal the stored policy kwargs." + "Stored kwargs: {data['policy_kwargs']}, specified kwargs: {kwargs['policy_kwargs']}") # check if observation space and action space are part of the saved parameters if ("observation_space" not in data or "action_space" not in data) and "env" not in data: @@ -357,7 +356,7 @@ class BaseRLModel(ABC): if os.path.exists(load_path + ".zip"): load_path += ".zip" else: - raise ValueError("Error: the file {} could not be found".format(load_path)) + raise ValueError(f"Error: the file {load_path} could not be found") # Open the zip archive and load data try: @@ -404,7 +403,7 @@ class BaseRLModel(ABC): opt_params[os.path.splitext(file_path)[0]] = th.load(file_content) except zipfile.BadZipFile: # load_path wasn't a zip file - raise ValueError("Error: the file {} wasn't a zip-file".format(load_path)) + raise ValueError(f"Error: the file {load_path} wasn't a zip-file") return data, params, opt_params @@ -720,7 +719,7 @@ class BaseRLModel(ABC): sync_envs_normalization(self.env, eval_env) mean_reward, std_reward = evaluate_policy(self, eval_env, n_eval_episodes, deterministic=deterministic) if self.verbose > 0: - print("Eval num_timesteps={}, " - "episode_reward={:.2f} +/- {:.2f}".format(self.num_timesteps, mean_reward, std_reward)) - print("FPS: {:.2f}".format(self.num_timesteps / (time.time() - self.start_time))) + print(f"Eval num_timesteps={self.num_timesteps}, " + "episode_reward={mean_reward:.2f} +/- {std_reward:.2f}") + print(f"FPS: {self.num_timesteps / (time.time() - self.start_time):.2f}") return timesteps_since_eval diff --git a/torchy_baselines/common/distributions.py b/torchy_baselines/common/distributions.py index e4ca9bc..6ede43f 100644 --- a/torchy_baselines/common/distributions.py +++ b/torchy_baselines/common/distributions.py @@ -467,6 +467,5 @@ def make_proba_distribution(action_space, use_sde=False, dist_kwargs=None): # elif isinstance(action_space, spaces.MultiBinary): # return BernoulliDistribution(action_space.n, **dist_kwargs) else: - raise NotImplementedError("Error: probability distribution, not implemented for action space of type {}." - .format(type(action_space)) + + raise NotImplementedError(f"Error: probability distribution, not implemented for action space of type {type(action_space)}." " Must be of type Gym Spaces: Box, Discrete, MultiDiscrete or MultiBinary.") diff --git a/torchy_baselines/common/evaluation.py b/torchy_baselines/common/evaluation.py index 04ae296..0133621 100644 --- a/torchy_baselines/common/evaluation.py +++ b/torchy_baselines/common/evaluation.py @@ -47,8 +47,8 @@ def evaluate_policy(model, env, n_eval_episodes=10, deterministic=True, mean_reward = np.mean(episode_rewards) std_reward = np.std(episode_rewards) if reward_threshold is not None: - assert mean_reward > reward_threshold, 'Mean reward below threshold: '\ - '{:.2f} < {:.2f}'.format(mean_reward, reward_threshold) + assert mean_reward > reward_threshold, (f'Mean reward below threshold: ' + '{mean_reward:.2f} < {reward_threshold:.2f}') if return_episode_rewards: return episode_rewards, n_steps return mean_reward, std_reward diff --git a/torchy_baselines/common/logger.py b/torchy_baselines/common/logger.py index 78845ee..0da70e6 100644 --- a/torchy_baselines/common/logger.py +++ b/torchy_baselines/common/logger.py @@ -53,7 +53,7 @@ class HumanOutputFormat(KVWriter, SeqWriter): self.file = open(filename_or_file, 'wt') self.own_file = True else: - assert hasattr(filename_or_file, 'write'), 'Expected file or str, got {}'.format(filename_or_file) + assert hasattr(filename_or_file, 'write'), f'Expected file or str, got {filename_or_file}' self.file = filename_or_file self.own_file = False diff --git a/torchy_baselines/common/noise.py b/torchy_baselines/common/noise.py index 09e07fb..dd7d8a3 100644 --- a/torchy_baselines/common/noise.py +++ b/torchy_baselines/common/noise.py @@ -30,7 +30,7 @@ class NormalActionNoise(ActionNoise): return np.random.normal(self._mu, self._sigma) def __repr__(self): - return 'NormalActionNoise(mu={}, sigma={})'.format(self._mu, self._sigma) + return f'NormalActionNoise(mu={self._mu}, sigma={self._sigma})' class OrnsteinUhlenbeckActionNoise(ActionNoise): @@ -68,4 +68,4 @@ class OrnsteinUhlenbeckActionNoise(ActionNoise): self.noise_prev = self.initial_noise if self.initial_noise is not None else np.zeros_like(self._mu) def __repr__(self): - return 'OrnsteinUhlenbeckActionNoise(mu={}, sigma={})'.format(self._mu, self._sigma) + return f'OrnsteinUhlenbeckActionNoise(mu={self._mu}, sigma={self._sigma})' diff --git a/torchy_baselines/common/policies.py b/torchy_baselines/common/policies.py index f536624..ea7f755 100644 --- a/torchy_baselines/common/policies.py +++ b/torchy_baselines/common/policies.py @@ -150,10 +150,10 @@ def get_policy_from_name(base_policy_type, name): :return: (base_policy_type) the policy """ if base_policy_type not in _policy_registry: - raise ValueError("Error: the policy type {} is not registered!".format(base_policy_type)) + raise ValueError(f"Error: the policy type {base_policy_type} is not registered!") if name not in _policy_registry[base_policy_type]: - raise ValueError("Error: unknown policy type {}, the only registed policy type are: {}!" - .format(name, list(_policy_registry[base_policy_type].keys()))) + raise ValueError(f"Error: unknown policy type {name}," + "the only registed policy type are: {list(_policy_registry[base_policy_type].keys())}!") return _policy_registry[base_policy_type][name] @@ -174,13 +174,12 @@ def register_policy(name, policy): except AttributeError: sub_class = str(th.random.randint(100)) if sub_class is None: - raise ValueError("Error: the policy {} is not of any known subclasses of BasePolicy!".format(policy)) + raise ValueError(f"Error: the policy {policy} is not of any known subclasses of BasePolicy!") if sub_class not in _policy_registry: _policy_registry[sub_class] = {} if name in _policy_registry[sub_class]: - raise ValueError("Error: the name {} is alreay registered for a different policy, will not override." - .format(name)) + raise ValueError(f"Error: the name {name} is alreay registered for a different policy, will not override.") _policy_registry[sub_class][name] = policy diff --git a/torchy_baselines/common/save_util.py b/torchy_baselines/common/save_util.py index 7b6a2be..1b5d801 100644 --- a/torchy_baselines/common/save_util.py +++ b/torchy_baselines/common/save_util.py @@ -123,7 +123,7 @@ def json_to_data(json_string, custom_objects=None): ) except pickle.UnpicklingError: raise RuntimeError( - "Could not deserialize object {}. ".format(data_key) + + f"Could not deserialize object {data_key}. " + "Consider using `custom_objects` argument to replace " + "this object." ) diff --git a/torchy_baselines/common/vec_env/base_vec_env.py b/torchy_baselines/common/vec_env/base_vec_env.py index 47defa9..d239ddd 100644 --- a/torchy_baselines/common/vec_env/base_vec_env.py +++ b/torchy_baselines/common/vec_env/base_vec_env.py @@ -161,7 +161,7 @@ class VecEnv(ABC): :return: (str or None) name of module whose attribute is being shadowed, if any. """ if hasattr(self, name) and already_found: - return "{0}.{1}".format(type(self).__module__, type(self).__name__) + return f"{type(self).__module__}.{type(self).__name__}" else: return None @@ -230,10 +230,10 @@ class VecEnvWrapper(VecEnv): """ blocked_class = self.getattr_depth_check(name, already_found=False) if blocked_class is not None: - own_class = "{0}.{1}".format(type(self).__module__, type(self).__name__) - format_str = ("Error: Recursive attribute lookup for {0} from {1} is " - "ambiguous and hides attribute from {2}") - raise AttributeError(format_str.format(name, own_class, blocked_class)) + own_class = f"{type(self).__module__}.{type(self).__name__}" + error_str = (f"Error: Recursive attribute lookup for {name} from {own_class} is " + "ambiguous and hides attribute from {blocked_class}") + raise AttributeError(error_str) return self.getattr_recursive(name) @@ -272,7 +272,7 @@ class VecEnvWrapper(VecEnv): all_attributes = self._get_all_attributes() if name in all_attributes and already_found: # this venv's attribute is being hidden because of a higher venv. - shadowed_wrapper_class = "{0}.{1}".format(type(self).__module__, type(self).__name__) + shadowed_wrapper_class = f"{type(self).__module__}.{type(self).__name__}" elif name in all_attributes and not already_found: # we have found the first reference to the attribute. Now check for duplicates. shadowed_wrapper_class = self.venv.getattr_depth_check(name, True) diff --git a/torchy_baselines/common/vec_env/util.py b/torchy_baselines/common/vec_env/util.py index 03ce286..0e8629a 100644 --- a/torchy_baselines/common/vec_env/util.py +++ b/torchy_baselines/common/vec_env/util.py @@ -15,7 +15,7 @@ def copy_obs_dict(obs): :param obs: (OrderedDict): a dict of numpy arrays. :return (OrderedDict) a dict of copied numpy arrays. """ - assert isinstance(obs, OrderedDict), "unexpected type for observations '{}'".format(type(obs)) + assert isinstance(obs, OrderedDict), f"unexpected type for observations '{type(obs)}'" return OrderedDict([(k, np.copy(v)) for k, v in obs.items()]) @@ -61,7 +61,7 @@ def obs_space_info(obs_space): elif isinstance(obs_space, gym.spaces.Tuple): subspaces = {i: space for i, space in enumerate(obs_space.spaces)} else: - assert not hasattr(obs_space, 'spaces'), "Unsupported structured space '{}'".format(type(obs_space)) + assert not hasattr(obs_space, 'spaces'), f"Unsupported structured space '{type(obs_space)}'" subspaces = {None: obs_space} keys = [] shapes = {} diff --git a/torchy_baselines/common/vec_env/vec_normalize.py b/torchy_baselines/common/vec_env/vec_normalize.py index 0824f6a..4f5cb11 100644 --- a/torchy_baselines/common/vec_env/vec_normalize.py +++ b/torchy_baselines/common/vec_env/vec_normalize.py @@ -182,7 +182,7 @@ class VecNormalize(VecEnvWrapper): :param path: (str) path to log dir """ for rms, name in zip([self.obs_rms, self.ret_rms], ['obs_rms', 'ret_rms']): - with open("{}/{}.pkl".format(path, name), 'wb') as file_handler: + with open(f"{path}/{name}.pkl", 'wb') as file_handler: pickle.dump(rms, file_handler) def load_running_average(self, path): @@ -190,5 +190,5 @@ class VecNormalize(VecEnvWrapper): :param path: (str) path to log dir """ for name in ['obs_rms', 'ret_rms']: - with open("{}/{}.pkl".format(path, name), 'rb') as file_handler: + with open(f"{path}/{name}.pkl", 'rb') as file_handler: setattr(self, name, pickle.load(file_handler))