|
| 1 | +""" |
| 2 | +This file is part of CLIMADA. |
| 3 | +
|
| 4 | +Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS. |
| 5 | +
|
| 6 | +CLIMADA is free software: you can redistribute it and/or modify it under the |
| 7 | +terms of the GNU General Public License as published by the Free |
| 8 | +Software Foundation, version 3. |
| 9 | +
|
| 10 | +CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | +PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License along |
| 15 | +with CLIMADA. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | +--- |
| 18 | +Tests for cross calibration module |
| 19 | +""" |
| 20 | +import unittest |
| 21 | +from tempfile import TemporaryDirectory |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import pandas as pd |
| 25 | +import pandas.testing as pdt |
| 26 | +import numpy as np |
| 27 | +import numpy.testing as npt |
| 28 | + |
| 29 | +from climada.util.calibrate.cross_calibrate import ( |
| 30 | + EnsembleOptimizerOutput, |
| 31 | + SingleEnsembleOptimizerOutput, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +class TestEnsembleOptimizerOutput(unittest.TestCase): |
| 36 | + """Test the EnsembleOptimizerOutput""" |
| 37 | + |
| 38 | + def setUp(self): |
| 39 | + """Initialize single outputs""" |
| 40 | + self.output1 = SingleEnsembleOptimizerOutput( |
| 41 | + params={"param1": 1.0, "param2": 2.0}, |
| 42 | + target=1, |
| 43 | + event_info={ |
| 44 | + "event_id": np.array([1, 2]), |
| 45 | + "region_id": np.array([1, 2]), |
| 46 | + "event_name": ["a", "b"], |
| 47 | + }, |
| 48 | + ) |
| 49 | + self.output2 = SingleEnsembleOptimizerOutput( |
| 50 | + params={"param1": 1.1, "param2": 2.1}, |
| 51 | + target=2, |
| 52 | + event_info={ |
| 53 | + "event_name": [1], |
| 54 | + "event_id": np.array([3]), |
| 55 | + "region_id": np.array([4]), |
| 56 | + }, |
| 57 | + ) |
| 58 | + |
| 59 | + def test_from_outputs(self): |
| 60 | + """Test 'from_outputs' initialization""" |
| 61 | + out = EnsembleOptimizerOutput.from_outputs([self.output1, self.output2]) |
| 62 | + data = out.data |
| 63 | + |
| 64 | + # Test MultiIndex columns |
| 65 | + npt.assert_array_equal(data["Parameters"].columns, ["param1", "param2"]) |
| 66 | + npt.assert_array_equal( |
| 67 | + data["Event"].columns, ["event_id", "region_id", "event_name"] |
| 68 | + ) |
| 69 | + |
| 70 | + # Test parameters |
| 71 | + npt.assert_array_equal(data[("Parameters", "param1")], [1.0, 1.1]) |
| 72 | + npt.assert_array_equal(data[("Parameters", "param2")], [2.0, 2.1]) |
| 73 | + |
| 74 | + # Test event info |
| 75 | + pdt.assert_series_equal( |
| 76 | + data[("Event", "event_id")], |
| 77 | + pd.Series([np.array([1, 2]), np.array([3])], name=("Event", "event_id")), |
| 78 | + ) |
| 79 | + pdt.assert_series_equal( |
| 80 | + data[("Event", "region_id")], |
| 81 | + pd.Series([np.array([1, 2]), np.array([4])], name=("Event", "region_id")), |
| 82 | + ) |
| 83 | + pdt.assert_series_equal( |
| 84 | + data[("Event", "event_name")], |
| 85 | + pd.Series([["a", "b"], [1]], name=("Event", "event_name")), |
| 86 | + ) |
| 87 | + |
| 88 | + def test_cycling(self): |
| 89 | + """Test correct cycling to files""" |
| 90 | + with TemporaryDirectory() as tmp: |
| 91 | + filepath = Path(tmp, "file.h5") |
| 92 | + |
| 93 | + out = EnsembleOptimizerOutput.from_outputs([self.output1, self.output2]) |
| 94 | + out.to_hdf(filepath) |
| 95 | + |
| 96 | + out_new = EnsembleOptimizerOutput.from_hdf(filepath) |
| 97 | + pdt.assert_frame_equal(out.data, out_new.data) |
| 98 | + |
| 99 | + @unittest.skip("Cycling with CSV does not preserve data types") |
| 100 | + def test_cycling_csv(self): |
| 101 | + """Test correct cycling with CSV""" |
| 102 | + with TemporaryDirectory() as tmp: |
| 103 | + filepath = Path(tmp, "file.csv") |
| 104 | + |
| 105 | + out = EnsembleOptimizerOutput.from_outputs([self.output1, self.output2]) |
| 106 | + out.to_csv(filepath) |
| 107 | + |
| 108 | + out_new = EnsembleOptimizerOutput.from_csv(filepath) |
| 109 | + pdt.assert_frame_equal(out.data, out_new.data) |
0 commit comments