Skip to content

Commit

Permalink
Merge pull request #133 from Federico-PizarroBejarano/improved_linting
Browse files Browse the repository at this point in the history
Improved linting
  • Loading branch information
adamhall authored Oct 12, 2023
2 parents d286cea + c5f27c8 commit 0b118b4
Show file tree
Hide file tree
Showing 84 changed files with 18,784 additions and 18,734 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Test package quality
name: Test Package Quality

on: push

Expand All @@ -26,6 +21,11 @@ jobs:
- name: Install package
run: |
pip install -e .
- name: Run Linting
run: |
pre-commit install
pre-commit autoupdate
pre-commit run
- name: Unit tests
run: |
python -m pytest tests/
Expand Down
57 changes: 57 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Install the pre-commit hooks below with
# 'pre-commit install'

# Auto-update the version of the hooks with
# 'pre-commit autoupdate'

# Run the hooks on all files with
# 'pre-commit run --all'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-ast
- id: check-yaml
- id: check-toml
- id: check-added-large-files
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: check-merge-conflict
- id: debug-statements
- id: trailing-whitespace
- id: end-of-file-fixer
- id: double-quote-string-fixer

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort
args: ['--line-length=110']

- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.4
hooks:
- id: autopep8
name: autopep8-default
args: ['-i', '--ignore=C0301', '--max-line-length=1000']
exclude: (^tests/|^safe_control_gym/math_and_models/transformations.py)
- id: autopep8
name: autopep8-tests
args: ['-i', '--ignore=C0301,E501,E201,E241,E127', '--max-line-length=1000']
files: (^tests/|^safe_control_gym/math_and_models/transformations.py)

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
name: flake8_default
args: ['--ignore=E501']
exclude: (^safe_control_gym/__init__.py|^tests/|^safe_control_gym/math_and_models/transformations.py)
- id: flake8
name: flake8_tests
args: ['--ignore=E501,E201,E241,E127']
files: (^tests/|^safe_control_gym/math_and_models/transformations.py)
exclude: ^tests/test_build.py
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ Note that the Bullet engine frequency reported for `safe-control-gym` is typical
[004]: https://github.com/utiasDSL/safe-control-gym/blob/main/safe_control_gym/envs/gym_control/cartpole.py
[005]: https://github.com/utiasDSL/safe-control-gym/blob/main/safe_control_gym/envs/gym_pybullet_drones/quadrotor.py

## Run Tests and Linting
Tests can be run locally by executing:
```bash
python3 -m pytest ./tests/ # Run all tests
```

Linting can be run locally with:
```bash
pre-commit install # Install the pre-commit hooks
pre-commit autoupdate # Auto-update the version of the hooks
pre-commit run --all # Run the hooks on all files
```

## References

- Brunke, L., Greeff, M., Hall, A. W., Yuan, Z., Zhou, S., Panerati, J., & Schoellig, A. P. (2022). [Safe learning in robotics: From learning-based control to safe reinforcement learning](https://www.annualreviews.org/doi/abs/10.1146/annurev-control-042920-020211). Annual Review of Control, Robotics, and Autonomous Systems, 5, 411-444.
Expand Down
4 changes: 2 additions & 2 deletions examples/cbf/cbf_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import shutil
from functools import partial

import numpy as np
import matplotlib.pyplot as plt
import numpy as np

from safe_control_gym.experiments.base_experiment import BaseExperiment
from safe_control_gym.utils.registration import make
from safe_control_gym.utils.configuration import ConfigFactory
from safe_control_gym.utils.registration import make


def run(plot=True, training=True, n_episodes=1, n_steps=None, curr_path='.', save_data=False):
Expand Down
6 changes: 3 additions & 3 deletions examples/lqr/lqr_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import os
import pickle
from functools import partial
from collections import defaultdict
from functools import partial

import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter

from safe_control_gym.experiments.base_experiment import BaseExperiment
from safe_control_gym.envs.benchmark_env import Task
from safe_control_gym.experiments.base_experiment import BaseExperiment
from safe_control_gym.utils.configuration import ConfigFactory
from safe_control_gym.utils.registration import make

Expand Down
4 changes: 2 additions & 2 deletions examples/mpsc/config_overrides/cartpole/cartpole_stab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ task_config:
ctrl_freq: 15
pyb_freq: 750
physics: pyb

# state initialization
init_state:
init_x: 0.1
init_x_dot: -1.5
init_theta: -0.175
init_theta_dot: 0.5
init_theta_dot: 0.5
randomized_init: True
randomized_inertial_prop: False
normalized_rl_action_space: True
Expand Down
4 changes: 2 additions & 2 deletions examples/mpsc/config_overrides/cartpole/cartpole_track.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ task_config:
ctrl_freq: 15
pyb_freq: 750
physics: pyb

# state initialization
init_state:
init_x: 0
init_x_dot: 0
init_theta: 0
init_theta_dot: 0
init_theta_dot: 0
randomized_init: True
randomized_inertial_prop: False
normalized_rl_action_space: True
Expand Down
2 changes: 1 addition & 1 deletion examples/mpsc/config_overrides/cartpole/sac_cartpole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ algo_config:
save_interval: 1000
num_checkpoints: 100
eval_interval: 1000
eval_save_best: True
eval_save_best: True
tensorboard: True
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ task_config:
rew_state_weight: [1, 1, 1, 1, 1, 1]
rew_act_weight: 0.1
rew_exponential: True

constraints:
- constraint_form: default_constraint
constrained_variable: state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ task_config:
trajectory_plane: 'xz'
trajectory_position_offset: [0, 1]
trajectory_scale: 1

inertial_prop:
M: 0.027
Iyy: 1.4e-05
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ algo_config:
save_interval: 1000
num_checkpoints: 100
eval_interval: 1000
eval_save_best: True
eval_save_best: True
tensorboard: True
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ task_config:
rew_state_weight: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
rew_act_weight: 0.1
rew_exponential: True

constraints:
- constraint_form: default_constraint
constrained_variable: state
Expand Down
6 changes: 3 additions & 3 deletions examples/mpsc/mpsc_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import shutil
from functools import partial

import numpy as np
import matplotlib.pyplot as plt
import numpy as np

from safe_control_gym.envs.benchmark_env import Cost, Environment, Task
from safe_control_gym.experiments.base_experiment import BaseExperiment
from safe_control_gym.utils.registration import make
from safe_control_gym.utils.configuration import ConfigFactory
from safe_control_gym.envs.benchmark_env import Task, Cost, Environment
from safe_control_gym.utils.registration import make


def run(plot=True, training=False, n_episodes=1, n_steps=None, curr_path='.'):
Expand Down
3 changes: 2 additions & 1 deletion examples/no_controller/verbose_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
$ python3 verbose_api.py --task quadrotor --overrides verbose_api.yaml
'''

import time
import inspect
import time

import pybullet as p

from safe_control_gym.utils.configuration import ConfigFactory
Expand Down
2 changes: 1 addition & 1 deletion examples/pid/pid_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import pickle
from functools import partial

import matplotlib.pyplot as plt
import numpy as np
import pybullet as p
import matplotlib.pyplot as plt

from safe_control_gym.experiments.base_experiment import BaseExperiment
from safe_control_gym.utils.configuration import ConfigFactory
Expand Down
6 changes: 3 additions & 3 deletions examples/rl/rl_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import shutil
from functools import partial

import numpy as np
import matplotlib.pyplot as plt
import numpy as np

from safe_control_gym.envs.benchmark_env import Environment, Task
from safe_control_gym.experiments.base_experiment import BaseExperiment
from safe_control_gym.utils.registration import make
from safe_control_gym.utils.configuration import ConfigFactory
from safe_control_gym.envs.benchmark_env import Task, Environment
from safe_control_gym.utils.registration import make


def run(gui=True, n_episodes=1, n_steps=None, curr_path='.'):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pytope = "^0.0.4"
Mosek = "^10.0.18"
termcolor = "^1.1.0"
pytest = "^7.2.2"
pre-commit = "^3.3.2"

[tool.poetry.dev-dependencies]

Expand Down
2 changes: 1 addition & 1 deletion safe_control_gym/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from safe_control_gym.controllers import *
from safe_control_gym.safety_filters import *
from safe_control_gym.envs import *
from safe_control_gym.envs.env_wrappers import *
from safe_control_gym.experiments import *
from safe_control_gym.safety_filters import *
15 changes: 8 additions & 7 deletions safe_control_gym/controllers/ddpg/ddpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
import numpy as np
import torch

from safe_control_gym.utils.logging import ExperimentLogger
from safe_control_gym.utils.utils import get_random_state, set_random_state, is_wrapped
from safe_control_gym.envs.env_wrappers.vectorized_env import make_vec_envs
from safe_control_gym.envs.env_wrappers.vectorized_env.vec_env_utils import _flatten_obs, _unflatten_obs
from safe_control_gym.envs.env_wrappers.record_episode_statistics import RecordEpisodeStatistics, VecRecordEpisodeStatistics
from safe_control_gym.math_and_models.normalization import BaseNormalizer, MeanStdNormalizer, RewardStdNormalizer

from safe_control_gym.controllers.base_controller import BaseController
from safe_control_gym.controllers.ddpg.ddpg_utils import DDPGAgent, DDPGBuffer, make_action_noise_process
from safe_control_gym.envs.env_wrappers.record_episode_statistics import (RecordEpisodeStatistics,
VecRecordEpisodeStatistics)
from safe_control_gym.envs.env_wrappers.vectorized_env import make_vec_envs
from safe_control_gym.envs.env_wrappers.vectorized_env.vec_env_utils import _flatten_obs, _unflatten_obs
from safe_control_gym.math_and_models.normalization import (BaseNormalizer, MeanStdNormalizer,
RewardStdNormalizer)
from safe_control_gym.utils.logging import ExperimentLogger
from safe_control_gym.utils.utils import get_random_state, is_wrapped, set_random_state


class DDPG(BaseController):
Expand Down
12 changes: 7 additions & 5 deletions safe_control_gym/controllers/ddpg/ddpg_utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from copy import deepcopy
from collections import defaultdict
from copy import deepcopy

import numpy as np
import torch
import torch.nn as nn

from safe_control_gym.math_and_models.neural_networks import MLP
from safe_control_gym.controllers.sac.sac_utils import SACBuffer, soft_update

from safe_control_gym.math_and_models.neural_networks import MLP

# -----------------------------------------------------------------------------------
# Agent
# -----------------------------------------------------------------------------------


class DDPGAgent:
'''A DDPG class that encapsulates model, optimizer and update functions.'''

Expand Down Expand Up @@ -160,8 +160,10 @@ def __init__(self, obs_space, act_space, hidden_dims=(64, 64), activation='relu'
low, high = act_space.low, act_space.high
low = torch.FloatTensor(low)
high = torch.FloatTensor(high)
# Rescale action from [-1, 1] to [low, high]
def unscale_fn(x): return low.to(x.device) + (0.5 * (x + 1.0) * (high.to(x.device) - low.to(x.device)))

def unscale_fn(x): # Rescale action from [-1, 1] to [low, high]
return low.to(x.device) + (0.5 * (x + 1.0) * (high.to(x.device) - low.to(x.device)))

self.actor = MLPActor(obs_dim, act_dim, hidden_dims, activation, postprocess_fn=unscale_fn)

# Q functions
Expand Down
16 changes: 7 additions & 9 deletions safe_control_gym/controllers/lqr/ilqr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from termcolor import colored

from safe_control_gym.controllers.base_controller import BaseController
from safe_control_gym.controllers.lqr.lqr_utils import get_cost_weight_matrix, compute_lqr_gain, discretize_linear_system
from safe_control_gym.controllers.lqr.lqr_utils import (compute_lqr_gain, discretize_linear_system,
get_cost_weight_matrix)
from safe_control_gym.envs.benchmark_env import Task


Expand Down Expand Up @@ -108,8 +109,7 @@ def learn(self, env=None, **kwargs):
print(colored('--------------------------', 'green'))

if self.ite_counter == 0 and env.done_on_out_of_bound and self.final_info['out_of_bounds']:
print(colored('[ERROR] The initial policy might be unstable. '
+ 'Break from iLQR updates.', 'red'))
print(colored('[ERROR] The initial policy might be unstable. Break from iLQR updates.', 'red'))
break

# Maximum episode length.
Expand All @@ -133,10 +133,9 @@ def learn(self, env=None, **kwargs):
# If cost is increased, increase lambda
self.lamb *= self.lamb_factor

print(f'Cost increased by {-delta_cost}. '
+ 'Set feedforward term and controller gain to that '
'from the previous iteration. '
f'Increased lambda to {self.lamb}.')
print(f'Cost increased by {-delta_cost}.')
print('Set feedforward term and controller gain to that from the previous iteration.')
print(f'Increased lambda to {self.lamb}.')
print(f'Current policy is from iteration {self.best_iteration}.')

# Reset feedforward term and controller gain to that from
Expand Down Expand Up @@ -164,8 +163,7 @@ def learn(self, env=None, **kwargs):
# Check consecutive cost increment (cost decrement).
if abs(delta_cost) < self.epsilon and self.prev_ite_improved:
# Cost converged.
print(colored('iLQR cost converged with a tolerance '
+ f'of {self.epsilon}.', 'yellow'))
print(colored(f'iLQR cost converged with a tolerance of {self.epsilon}.', 'yellow'))
break

# Set improved flag to True.
Expand Down
2 changes: 1 addition & 1 deletion safe_control_gym/controllers/lqr/lqr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''Linear Quadratic Regulator (LQR).'''

from safe_control_gym.controllers.base_controller import BaseController
from safe_control_gym.controllers.lqr.lqr_utils import get_cost_weight_matrix, compute_lqr_gain
from safe_control_gym.controllers.lqr.lqr_utils import compute_lqr_gain, get_cost_weight_matrix
from safe_control_gym.envs.benchmark_env import Task


Expand Down
Loading

0 comments on commit 0b118b4

Please sign in to comment.