|
| 1 | +# Copyright 2025 DeepMind Technologies Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for the geometry_loader module.""" |
| 16 | + |
| 17 | +from unittest import mock |
| 18 | +from absl.testing import absltest |
| 19 | +import numpy as np |
| 20 | +from torax._src.geometry import geometry_loader |
| 21 | + |
| 22 | +# pylint: disable=invalid-name |
| 23 | + |
| 24 | + |
| 25 | +class GeometryLoaderTest(absltest.TestCase): |
| 26 | + |
| 27 | + @mock.patch("scipy.io.loadmat") |
| 28 | + def test_load_data_flat(self, mock_loadmat): |
| 29 | + """Tests loading MEQ data that is already flat in the mat file.""" |
| 30 | + mock_data = { |
| 31 | + "rBt": np.array([5.0]), |
| 32 | + "aminor": np.linspace(0.1, 1.0, 10), |
| 33 | + } |
| 34 | + mock_loadmat.return_value = mock_data |
| 35 | + |
| 36 | + result = geometry_loader._load_fbt_data("dummy_path.mat") |
| 37 | + self.assertEqual(result, mock_data) |
| 38 | + |
| 39 | + @mock.patch("scipy.io.loadmat") |
| 40 | + def test_load_fbt_data_nested_LY(self, mock_loadmat): |
| 41 | + """Tests loading MEQ data nested inside an 'LY' structured array.""" |
| 42 | + # Build a structured array to mimic scipy.io.loadmat's behavior for |
| 43 | + # meqlpack outputs. |
| 44 | + aminor_data = np.linspace(0.1, 1.0, 10) |
| 45 | + dtype = [("rBt", "O"), ("aminor", "O")] |
| 46 | + # Structured array with 1 element, fields contain arrays. |
| 47 | + LY_array = np.array([(np.array([5.0]), aminor_data)], dtype=dtype) |
| 48 | + |
| 49 | + mock_loadmat.return_value = {"LY": LY_array} |
| 50 | + |
| 51 | + result = geometry_loader._load_fbt_data("dummy_path.mat") |
| 52 | + |
| 53 | + self.assertIn("rBt", result) |
| 54 | + self.assertIn("aminor", result) |
| 55 | + np.testing.assert_array_equal(result["rBt"], np.array([5.0])) |
| 56 | + np.testing.assert_array_equal(result["aminor"], aminor_data) |
| 57 | + |
| 58 | + @mock.patch("scipy.io.loadmat") |
| 59 | + def test_load_fbt_data_nested_L(self, mock_loadmat): |
| 60 | + """Tests loading MEQ data nested inside an 'L' structured array.""" |
| 61 | + pQ_data = np.linspace(0.0, 1.0, 20) |
| 62 | + dtype = [("pQ", "O")] |
| 63 | + L_array = np.array([(pQ_data,)], dtype=dtype) |
| 64 | + |
| 65 | + mock_loadmat.return_value = {"L": L_array} |
| 66 | + |
| 67 | + result = geometry_loader._load_fbt_data("dummy_path.mat") |
| 68 | + |
| 69 | + self.assertIn("pQ", result) |
| 70 | + np.testing.assert_array_equal(result["pQ"], pQ_data) |
| 71 | + |
| 72 | + @mock.patch("scipy.io.loadmat") |
| 73 | + def test_load_fbt_data_invalid_item_type(self, mock_loadmat): |
| 74 | + """Tests that a ValueError is raised if a nested item is not an array.""" |
| 75 | + dtype = [("bad_field", "O")] |
| 76 | + # Create a structured array where the item is NOT a numpy array (e.g., int) |
| 77 | + LY_array = np.array([(123,)], dtype=dtype) |
| 78 | + |
| 79 | + mock_loadmat.return_value = {"LY": LY_array} |
| 80 | + |
| 81 | + with self.assertRaisesRegex( |
| 82 | + ValueError, "MEQ data field 'bad_field' is not a numpy array" |
| 83 | + ): |
| 84 | + geometry_loader._load_fbt_data("dummy_path.mat") |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + absltest.main() |
0 commit comments