From f2a097b423d8b37b9472900d3d4696d111fa390f Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Wed, 14 Feb 2024 12:23:01 +0100 Subject: [PATCH] refactor: fix flake8 and documentation formatting errors This commit fixes several flake8 and doc formatting errors. --- setup.cfg | 7 +- stable_gym/common/utils.py | 4 +- .../envs/biological/oscillator/oscillator.py | 21 +++--- .../oscillator_complicated.py | 25 +++---- .../envs/classic_control/ex3_ekf/__init__.py | 70 +++++++++++++++---- .../envs/classic_control/ex3_ekf/ex3_ekf.py | 4 +- .../fetch_reach_cost/fetch_reach_cost.py | 4 +- .../quadx_waypoints_cost.py | 1 - 8 files changed, 92 insertions(+), 44 deletions(-) diff --git a/setup.cfg b/setup.cfg index dc90b0c6..c914493c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,8 +1,11 @@ [flake8] max-line-length = 89 -extend-ignore = E266 exclude = + docs/source/conf.py, build, - node_modules + node_modules, + tests, + stable_gym/version.py per-file-ignores = __init__.py: F401, E501 +extend-ignore = E266, D400, D401, D205 diff --git a/stable_gym/common/utils.py b/stable_gym/common/utils.py index b130bdf4..df1ac81a 100644 --- a/stable_gym/common/utils.py +++ b/stable_gym/common/utils.py @@ -228,7 +228,7 @@ def inject_value(input_item, value, round_accuracy=2, order=False, axis=0): Returns: union[list,dict]: The list or dictionary that contains the value. """ - order_op = lambda *args, **kwargs: ( + order_op = lambda *args, **kwargs: ( # noqa: E731 sorted(*args, **kwargs) if order else list(*args, **kwargs) ) if isinstance(input_item, dict): @@ -263,7 +263,7 @@ def verify_number_and_cast(x): def maybe_parse_reset_bounds(options, default_low, default_high): - """sThis function can be called during a reset() to customize the sampling + """This function can be called during a reset() to customize the sampling ranges for setting the initial state distributions. Args: diff --git a/stable_gym/envs/biological/oscillator/oscillator.py b/stable_gym/envs/biological/oscillator/oscillator.py index 7999a488..52e17379 100644 --- a/stable_gym/envs/biological/oscillator/oscillator.py +++ b/stable_gym/envs/biological/oscillator/oscillator.py @@ -37,7 +37,7 @@ class Oscillator(gym.Env): presented in `Han et al. 2020`_, which was not possible with the original code's observation space. - Added an adjustable ``max_cost`` threshold for episode termination, - defaulting to $100$ to match the original environment. + defaulting to 100 to match the original environment. .. _`Han et al. 2020`: https://arxiv.org/abs/2004.14288 @@ -47,24 +47,24 @@ class Oscillator(gym.Env): +-----+-----------------------------------------------+-------------------+-------------------+ | Num | Observation | Min | Max | +=====+===============================================+===================+===================+ - | 0 | Lacl mRNA transcripts concentration | 0 | $\infty$ | + | 0 | Lacl mRNA transcripts concentration | 0 | :math:`\infty` | +-----+-----------------------------------------------+-------------------+-------------------+ - | 1 | tetR mRNA transcripts concentration | 0 | $\infty$ | + | 1 | tetR mRNA transcripts concentration | 0 | :math:`\infty` | +-----+-----------------------------------------------+-------------------+-------------------+ - | 2 | CI mRNA transcripts concentration | 0 | $\infty$ | + | 2 | CI mRNA transcripts concentration | 0 | :math:`\infty` | +-----+-----------------------------------------------+-------------------+-------------------+ - | 3 || lacI (repressor) protein concentration | 0 | $\infty$ | + | 3 || lacI (repressor) protein concentration | 0 | :math:`\infty` | | || (Inhibits transcription of the tetR gene) | | | +-----+-----------------------------------------------+-------------------+-------------------+ - | 4 || tetR (repressor) protein concentration | 0 | $\infty$ | + | 4 || tetR (repressor) protein concentration | 0 | :math:`\infty` | | || (Inhibits transcription of CI gene) | | | +-----+-----------------------------------------------+-------------------+-------------------+ - | 5 || CI (repressor) protein concentration | 0 | $\infty$ | + | 5 || CI (repressor) protein concentration | 0 | :math:`\infty` | | || (Inhibits transcription of lacI gene) | | | +-----+-----------------------------------------------+-------------------+-------------------+ - | 6 | The reference we want to follow | 0 | $\infty$ | + | 6 | The reference we want to follow | 0 | :math:`\infty` | +-----+-----------------------------------------------+-------------------+-------------------+ - | (7) || **Optional** - The error between the current | $-\infty$ | $\infty$ | + | (7) || **Optional** - The error between the current | :math:`-\infty` | :math:`\infty` | | || value of protein 1 and the reference | | | +-----+-----------------------------------------------+-------------------+-------------------+ @@ -96,7 +96,8 @@ class Oscillator(gym.Env): Episode Termination: - An episode is terminated when the maximum step limit is reached. - - The step exceeds a threshold (default is $100$). This threshold can be adjusted using the `max_cost` environment argument. + - The step exceeds a threshold (default is 100). This threshold can be + adjusted using the `max_cost` environment argument. Solved Requirements: Considered solved when the average cost is lower than 300. diff --git a/stable_gym/envs/biological/oscillator_complicated/oscillator_complicated.py b/stable_gym/envs/biological/oscillator_complicated/oscillator_complicated.py index bb197b5a..3c6d0cd2 100644 --- a/stable_gym/envs/biological/oscillator_complicated/oscillator_complicated.py +++ b/stable_gym/envs/biological/oscillator_complicated/oscillator_complicated.py @@ -43,7 +43,7 @@ class is based on the :class:`~stable_gym.envs.biological.oscillator.oscillator. presented in `Han et al. 2020`_, which was not possible with the original code's observation space. - Added an adjustable ``max_cost`` threshold for episode termination, - defaulting to $\infty$ to match the original environment. + defaulting to :math:`\infty` to match the original environment. .. _`Han et al. 2020`: https://arxiv.org/abs/2004.14288 @@ -53,29 +53,29 @@ class is based on the :class:`~stable_gym.envs.biological.oscillator.oscillator. +-----+-------------------------------------------------+-------------------+-------------------+ | Num | Observation | Min | Max | +=====+=================================================+===================+===================+ - | 0 | Lacl mRNA transcripts concentration | 0 | $\infty$ | + | 0 | Lacl mRNA transcripts concentration | 0 | :math:`\infty` | +-----+-------------------------------------------------+-------------------+-------------------+ - | 1 | tetR mRNA transcripts concentration | 0 | $\infty$ | + | 1 | tetR mRNA transcripts concentration | 0 | :math:`\infty` | +-----+-------------------------------------------------+-------------------+-------------------+ - | 2 | CI mRNA transcripts concentration | 0 | $\infty$ | + | 2 | CI mRNA transcripts concentration | 0 | :math:`\infty` | +-----+-------------------------------------------------+-------------------+-------------------+ - | 3 | Extra protein mRNA transcripts concentration | 0 | $\infty$ | + | 3 | Extra protein mRNA transcripts concentration | 0 | :math:`\infty` | +-----+-------------------------------------------------+-------------------+-------------------+ - | 4 || lacI (repressor) protein concentration | 0 | $\infty$ | + | 4 || lacI (repressor) protein concentration | 0 | :math:`\infty` | | || (Inhibits transcription of the tetR gene) | | | +-----+-------------------------------------------------+-------------------+-------------------+ - | 5 || tetR (repressor) protein concentration | 0 | $\infty$ | + | 5 || tetR (repressor) protein concentration | 0 | :math:`\infty` | | || (Inhibits transcription of CI gene) | | | +-----+-------------------------------------------------+-------------------+-------------------+ - | 6 || CI (repressor) protein concentration | 0 | $\infty$ | + | 6 || CI (repressor) protein concentration | 0 | :math:`\infty` | | || (Inhibits transcription of extra protein gene) | | | +-----+-------------------------------------------------+-------------------+-------------------+ - | 7 || Extra (repressor) protein concentration | 0 | $\infty$ | + | 7 || Extra (repressor) protein concentration | 0 | :math:`\infty` | | || (Inhibits transcription of lacI gene) | | | +-----+-------------------------------------------------+-------------------+-------------------+ - | 8 | The reference we want to follow | 0 | $\infty$ | + | 8 | The reference we want to follow | 0 | :math:`\infty` | +-----+-------------------------------------------------+-------------------+-------------------+ - | (9) || **Optional** - The error between the current | -$\infty$ | $\infty$ | + | (9) || **Optional** - The error between the current | -:math:`\infty` | :math:`\infty` | | || value of protein 1 and the reference | | | +-----+-------------------------------------------------+-------------------+-------------------+ @@ -110,7 +110,8 @@ class is based on the :class:`~stable_gym.envs.biological.oscillator.oscillator. Episode Termination: - An episode is terminated when the maximum step limit is reached. - - The step exceeds a threshold (default is $\infty$). This threshold can be adjusted using the `max_cost` environment argument. + - The step exceeds a threshold (default is :math:`\infty`). This threshold can + be adjusted using the `max_cost` environment argument. Solved Requirements: Considered solved when the average cost is lower than 300. diff --git a/stable_gym/envs/classic_control/ex3_ekf/__init__.py b/stable_gym/envs/classic_control/ex3_ekf/__init__.py index 46344dc8..4cfad264 100644 --- a/stable_gym/envs/classic_control/ex3_ekf/__init__.py +++ b/stable_gym/envs/classic_control/ex3_ekf/__init__.py @@ -1,26 +1,70 @@ -"""Noisy master slave system (Ex3EKF) gymnasium environment. +r"""Noisy master slave system (Ex3EKF) gymnasium environment. -The dynamic system whose state is to be estimated: +Dynamics +======== + +The dynamics of the system whose state is to be estimated are given by: + +.. math:: + :nowrap: + + \[ + \begin{split} + x(k+1) &= A x(k) + w(k) \\ + \end{split} + \] + +In which the state vector :math:`(x(k)` is given by: .. math:: + :nowrap: + + \[ + \begin{align*} + x_1 &: \text{angle} \\ + x_2 &: \text{frequency} \\ + x_3 &: \text{amplitude} + \end{align*} + \] - x(k+1)=Ax(k)+w(k) - x_1: angle - x_2: frequency - x_3: amplitude +and the measurement vector :math:`(y(k))` is given by: + +.. math:: + :nowrap: - y(k)=x_3(k)*sin(x_1(k))+v(k) - A=[1,dt,0;0,1,0;0,0,1] - x(0)~N([0;10;1],[3,0,0;0,3,0;0,0,3]) - w(k)~N([0;0;0],[1/3*(dt)^3*q_1,1/2*(dt)^2*q_1,0;1/2*(dt)^2*q_1,dt*q_1,0;0,0,dt*q_2]) - v(k)~N(0,1) + \[ + \begin{split} + y(k) &= x_3(k) \cdot \sin(x_1(k)) + v(k) \\ + A &= \begin{bmatrix} + 1 & dt & 0 \\ + 0 & 1 & 0 \\ + 0 & 0 & 1 + \end{bmatrix} \\ + x(0) &\sim \mathcal{N}\left(\begin{bmatrix}0 \\ 10 \\ 1\end{bmatrix}, \begin{bmatrix} + 3 & 0 & 0 \\ + 0 & 3 & 0 \\ + 0 & 0 & 3 + \end{bmatrix}\right) \\ + w(k) &\sim \mathcal{N}\left(\begin{bmatrix}0 \\ 0 \\ 0\end{bmatrix}, \begin{bmatrix} + \frac{1}{3}dt^3 q_1 & \frac{1}{2}dt^2 q_1 & 0 \\ + \frac{1}{2}dt^2 q_1 & dt q_1 & 0 \\ + 0 & 0 & dt q_2 + \end{bmatrix}\right) \\ + v(k) &\sim \mathcal{N}(0, 1) + \end{split} + \] Estimator design: .. math:: + :nowrap: - \\hat(x)(k+1)=A\\hat(x)(k)+u - where u=[u1,u2,u3]', u=l(\\hat(x)(k),y(k)) come from the policy network l(.,.) + \[ + \begin{split} + \hat{x}(k+1) &= A \hat{x}(k) + u \\ + \text{where } u &= [u1, u2, u3]', \ u = l(\hat{x}(k), y(k)) \text{ come from the policy network } l(.,.). + \end{split} + \] """ from stable_gym.envs.classic_control.ex3_ekf.ex3_ekf import Ex3EKF diff --git a/stable_gym/envs/classic_control/ex3_ekf/ex3_ekf.py b/stable_gym/envs/classic_control/ex3_ekf/ex3_ekf.py index 6d9dba88..6786233f 100644 --- a/stable_gym/envs/classic_control/ex3_ekf/ex3_ekf.py +++ b/stable_gym/envs/classic_control/ex3_ekf/ex3_ekf.py @@ -10,7 +10,7 @@ class Ex3EKF(gym.Env): - """Noisy master slave system + r"""Noisy master slave system Description: The goal of the agent in the Ex3EKF environment is to act in such a way that @@ -48,7 +48,7 @@ class Ex3EKF(gym.Env): .. math:: - C = {(\\hat{x}_1 - x_1)}^2 + {(\\hat{x}_2 - x_2)}^2 + C = {(\hat{x}_1 - x_1)}^2 + {(\hat{x}_2 - x_2)}^2 Starting State: All observations are assigned a uniform random value in ``[-0.05..0.05]`` diff --git a/stable_gym/envs/robotics/fetch/fetch_reach_cost/fetch_reach_cost.py b/stable_gym/envs/robotics/fetch/fetch_reach_cost/fetch_reach_cost.py index e211af64..1fde80d0 100644 --- a/stable_gym/envs/robotics/fetch/fetch_reach_cost/fetch_reach_cost.py +++ b/stable_gym/envs/robotics/fetch/fetch_reach_cost/fetch_reach_cost.py @@ -12,7 +12,7 @@ # TODO: Update solving criteria after training. class FetchReachCost(MujocoFetchReachEnv, utils.EzPickle): - """Custom FetchReach gymnasium robotics environment. + r"""Custom FetchReach gymnasium robotics environment. .. note:: Can also be used in a vectorized manner. See the @@ -41,7 +41,7 @@ class FetchReachCost(MujocoFetchReachEnv, utils.EzPickle): .. math:: - cost = \\left | reward \\right | + cost = \left | reward \right | Solved Requirements: Considered solved when the average cost is less than or equal to 50 over diff --git a/stable_gym/envs/robotics/quadrotor/quadx_waypoints_cost/quadx_waypoints_cost.py b/stable_gym/envs/robotics/quadrotor/quadx_waypoints_cost/quadx_waypoints_cost.py index 631afc5a..ba09237f 100644 --- a/stable_gym/envs/robotics/quadrotor/quadx_waypoints_cost/quadx_waypoints_cost.py +++ b/stable_gym/envs/robotics/quadrotor/quadx_waypoints_cost/quadx_waypoints_cost.py @@ -331,7 +331,6 @@ def compute_target_deltas(self, ang_pos, lin_pos, quarternion): Returns: (np.ndarray): The waypoints target deltas. """ # noqa: E501 - # Store waypoints targets, distances and yaw error scalar. waypoints_targets = copy.copy(self.waypoints.targets) old_waypoints_distance = copy.copy(self.waypoints.old_distance)