|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch, MagicMock |
| 5 | +from aobasis.utils import make_circular_actuator_grid, make_concentric_actuator_grid, plot_basis_modes |
| 6 | + |
| 7 | +def test_make_circular_actuator_grid(): |
| 8 | + diameter = 10.0 |
| 9 | + grid_size = 10 |
| 10 | + positions = make_circular_actuator_grid(diameter, grid_size) |
| 11 | + |
| 12 | + assert isinstance(positions, np.ndarray) |
| 13 | + assert positions.shape[1] == 2 |
| 14 | + |
| 15 | + # Check that all points are within the radius |
| 16 | + radius = diameter / 2 |
| 17 | + distances = np.linalg.norm(positions, axis=1) |
| 18 | + assert np.all(distances <= radius * 1.0000001) |
| 19 | + |
| 20 | +def test_make_concentric_actuator_grid(): |
| 21 | + diameter = 10.0 |
| 22 | + n_rings = 3 |
| 23 | + n_points_innermost = 6 |
| 24 | + positions = make_concentric_actuator_grid(diameter, n_rings, n_points_innermost) |
| 25 | + |
| 26 | + assert isinstance(positions, np.ndarray) |
| 27 | + assert positions.shape[1] == 2 |
| 28 | + |
| 29 | + # Expected number of points: 1 (center) + 6*1 + 6*2 + 6*3 = 1 + 6 + 12 + 18 = 37 |
| 30 | + expected_points = 1 + sum(n_points_innermost * i for i in range(1, n_rings + 1)) |
| 31 | + assert positions.shape[0] == expected_points |
| 32 | + |
| 33 | +@patch("aobasis.utils.plt") |
| 34 | +def test_plot_basis_modes(mock_plt): |
| 35 | + # Setup mock data |
| 36 | + n_actuators = 20 |
| 37 | + n_modes = 5 |
| 38 | + positions = np.random.rand(n_actuators, 2) |
| 39 | + modes = np.random.rand(n_actuators, n_modes) |
| 40 | + |
| 41 | + # Configure mock to return a tuple |
| 42 | + mock_fig = MagicMock() |
| 43 | + mock_axes = MagicMock() |
| 44 | + mock_plt.subplots.return_value = (mock_fig, mock_axes) |
| 45 | + |
| 46 | + # Test basic plotting |
| 47 | + plot_basis_modes(modes, positions, count=3) |
| 48 | + |
| 49 | + assert mock_plt.subplots.called |
| 50 | + assert mock_plt.show.called |
| 51 | + |
| 52 | +@patch("aobasis.utils.plt") |
| 53 | +def test_plot_basis_modes_save(mock_plt, tmp_path): |
| 54 | + # Setup mock data |
| 55 | + n_actuators = 20 |
| 56 | + n_modes = 5 |
| 57 | + positions = np.random.rand(n_actuators, 2) |
| 58 | + modes = np.random.rand(n_actuators, n_modes) |
| 59 | + outfile = tmp_path / "test_plot.png" |
| 60 | + |
| 61 | + # Configure mock to return a tuple |
| 62 | + mock_fig = MagicMock() |
| 63 | + mock_axes = MagicMock() |
| 64 | + mock_plt.subplots.return_value = (mock_fig, mock_axes) |
| 65 | + |
| 66 | + # Test saving to file |
| 67 | + plot_basis_modes(modes, positions, count=3, outfile=outfile) |
| 68 | + |
| 69 | + assert mock_plt.subplots.called |
| 70 | + mock_plt.savefig.assert_called_with(outfile, dpi=150) |
| 71 | + assert mock_plt.close.called |
| 72 | + |
| 73 | +@patch("aobasis.utils.plt") |
| 74 | +def test_plot_basis_modes_interpolate(mock_plt): |
| 75 | + # Setup mock data |
| 76 | + n_actuators = 20 |
| 77 | + n_modes = 5 |
| 78 | + positions = np.random.rand(n_actuators, 2) |
| 79 | + modes = np.random.rand(n_actuators, n_modes) |
| 80 | + |
| 81 | + # Configure mock to return a tuple |
| 82 | + mock_fig = MagicMock() |
| 83 | + mock_axes = MagicMock() |
| 84 | + mock_plt.subplots.return_value = (mock_fig, mock_axes) |
| 85 | + |
| 86 | + # Test interpolation |
| 87 | + plot_basis_modes(modes, positions, count=3, interpolate=True) |
| 88 | + |
| 89 | + assert mock_plt.subplots.called |
| 90 | + # We can't easily check if imshow was called on the axes objects without more complex mocking, |
| 91 | + # but we can check that no errors were raised. |
| 92 | + |
| 93 | +def test_plot_basis_modes_invalid_shape(): |
| 94 | + n_actuators = 20 |
| 95 | + n_modes = 5 |
| 96 | + positions = np.random.rand(n_actuators, 2) |
| 97 | + modes = np.random.rand(n_actuators + 1, n_modes) # Mismatch |
| 98 | + |
| 99 | + with pytest.raises(ValueError, match="Mode dimension 0"): |
| 100 | + plot_basis_modes(modes, positions) |
0 commit comments