Skip to content

Commit

Permalink
[Test] Better tests and multi OS (#89)
Browse files Browse the repository at this point in the history
* amend

* amend

* [CI] Rerun tests

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* amend

* faster tests

* faster tests
  • Loading branch information
matteobettini authored Mar 5, 2024
1 parent 685b7f1 commit abf7b67
Show file tree
Hide file tree
Showing 26 changed files with 732 additions and 523 deletions.
12 changes: 12 additions & 0 deletions .github/unittest/install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# Copyright (c) 2024.
# ProrokLab (https://www.proroklab.org/)
# All rights reserved.
#


python -m pip install --upgrade pip
python -m pip install flake8 pytest pytest-cov tqdm matplotlib
python -m pip install cvxpylayers # Navigation heuristic

pip install -e .
27 changes: 27 additions & 0 deletions .github/workflows/pre_commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

name: pre-commit


on:
push:
branches: [ $default-branch , "main" , "dev" ]
pull_request:
branches: [ $default-branch , "main" ]

permissions:
contents: read


jobs:
pre-commit:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.10" ]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- uses: pre-commit/[email protected]
57 changes: 0 additions & 57 deletions .github/workflows/python-app.yml

This file was deleted.

43 changes: 43 additions & 0 deletions .github/workflows/tests-linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

name: pytest-linux

on:
push:
branches: [ $default-branch , "main" , "dev" ]
pull_request:
branches: [ $default-branch , "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
python-version: ["3.9", "3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install python3-opengl xvfb
bash .github/unittest/install_dependencies.sh
- name: Test with pytest
run: |
xvfb-run -s "-screen 0 1024x768x24" pytest tests/ --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml --cov-report=html
- if: matrix.python-version == '3.10'
name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
fail_ci_if_error: false
35 changes: 35 additions & 0 deletions .github/workflows/tests-mac.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


name: pytest-mac

on:
push:
branches: [ $default-branch , "main" , "dev" ]
pull_request:
branches: [ $default-branch , "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ macos-latest ]
python-version: ["3.9", "3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
bash .github/unittest/install_dependencies.sh
- name: Test with pytest
run: |
pytest tests/ --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml --cov-report=html
35 changes: 35 additions & 0 deletions .github/workflows/tests-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


name: pytest-windows

on:
push:
branches: [ $default-branch , "main" , "dev" ]
pull_request:
branches: [ $default-branch , "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ windows-latest ]
python-version: ["3.9", "3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
bash .github/unittest/install_dependencies.sh
- name: Test with pytest
run: |
pytest tests/ --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml --cov-report=html
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ pip install torchrl
# Logging
pip installl wandb

# Save renders
pip install opencv-python moviepy
# Rendering
pip install opencv-python moviepy matplotlib

# Tests
pip install pytest pyyaml pytest-instafail tqdm
Expand Down Expand Up @@ -410,9 +410,9 @@ To create a fake screen you need to have `Xvfb` installed.
## TODOS

- [ ] Reset any number of dimensions
- [ ] Improve test efficiency and add new tests
- [ ] Implement 1D camera sensor
- [ ] Implement 2D birds eye view camera sensor
- [X] Improve test efficiency and add new tests
- [X] Implement 2D drone dynamics
- [X] Allow any number of actions
- [X] Improve VMAS performance
Expand Down
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
status:
patch: off
2 changes: 1 addition & 1 deletion docs/source/usage/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ You may want to install the following rendering and logging tools
.. code-block:: console
pip install wandb
pip install opencv-python moviepy
pip install opencv-python moviepy matplotlib
55 changes: 30 additions & 25 deletions tests/test_scenarios/test_balance.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
# Copyright (c) 2022-2023.
# Copyright (c) 2022-2024.
# ProrokLab (https://www.proroklab.org/)
# All rights reserved.
import unittest

import pytest
import torch

from vmas import make_env
from vmas.scenarios import balance


class TestBalance(unittest.TestCase):
class TestBalance:
def setup_env(
self,
n_envs,
**kwargs,
) -> None:
super().setUp()
self.n_agents = kwargs.get("n_agents", 4)

self.continuous_actions = True
self.n_envs = 15
self.env = make_env(
scenario="balance",
num_envs=self.n_envs,
num_envs=n_envs,
device="cpu",
continuous_actions=self.continuous_actions,
# Environment specific variables
**kwargs,
)
self.env.seed(0)

def test_heuristic(self):
@pytest.mark.parametrize("n_agents", [2, 5])
def test_heuristic(self, n_agents, n_steps=50, n_envs=4):
self.setup_env(
n_agents=n_agents, random_package_pos_on_line=False, n_envs=n_envs
)
policy = balance.HeuristicPolicy(self.continuous_actions)

for n_agents in [2, 5, 6, 10]:
self.setup_env(n_agents=n_agents, random_package_pos_on_line=False)
policy = balance.HeuristicPolicy(self.continuous_actions)
obs = self.env.reset()

obs = self.env.reset()
rews = None
prev_package_dist_to_goal = obs[0][:, 8:10]

for _ in range(100):
actions = []
for i in range(n_agents):
obs_agent = obs[i]
for _ in range(n_steps):
actions = []
for i in range(n_agents):
obs_agent = obs[i]
package_dist_to_goal = obs_agent[:, 8:10]

action_agent = policy.compute_action(
obs_agent, self.env.agents[i].u_range
)
action_agent = policy.compute_action(
obs_agent, self.env.agents[i].u_range
)

actions.append(action_agent)
actions.append(action_agent)

obs, new_rews, dones, _ = self.env.step(actions)
obs, new_rews, dones, _ = self.env.step(actions)

if rews is not None:
for i in range(self.n_agents):
self.assertTrue((new_rews[i] >= rews[i]).all())
rews = new_rews
assert (
torch.linalg.vector_norm(package_dist_to_goal, dim=-1)
<= torch.linalg.vector_norm(prev_package_dist_to_goal, dim=-1)
).all()
prev_package_dist_to_goal = package_dist_to_goal
Loading

0 comments on commit abf7b67

Please sign in to comment.