|
| 1 | +# (C) British Crown Copyright 2017, Met Office |
| 2 | +# |
| 3 | +# This file is part of Iris. |
| 4 | +# |
| 5 | +# Iris is free software: you can redistribute it and/or modify it under |
| 6 | +# the terms of the GNU Lesser General Public License as published by the |
| 7 | +# Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# Iris is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with Iris. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +"""Unit tests for :class:`iris.coords.DimCoord`.""" |
| 18 | + |
| 19 | +from __future__ import (absolute_import, division, print_function) |
| 20 | +from six.moves import (filter, input, map, range, zip) # noqa |
| 21 | + |
| 22 | +# Import iris.tests first so that some things can be initialised before |
| 23 | +# importing anything else. |
| 24 | +import iris.tests as tests |
| 25 | + |
| 26 | +import copy |
| 27 | + |
| 28 | +import dask |
| 29 | +import numpy as np |
| 30 | + |
| 31 | +from iris._lazy_data import as_lazy_data |
| 32 | +from iris.coords import DimCoord |
| 33 | + |
| 34 | + |
| 35 | +class Test___init__(tests.IrisTest): |
| 36 | + def test_writeable(self): |
| 37 | + coord = DimCoord([1, 2], bounds=[[1, 2], [2, 3]]) |
| 38 | + self.assertFalse(coord.points.flags.writeable) |
| 39 | + self.assertFalse(coord.bounds.flags.writeable) |
| 40 | + |
| 41 | + |
| 42 | +def fetch_base(ndarray): |
| 43 | + if ndarray.base is not None: |
| 44 | + return fetch_base(ndarray.base) |
| 45 | + return ndarray |
| 46 | + |
| 47 | + |
| 48 | +class Test___getitem__(tests.IrisTest): |
| 49 | + def test_share_data(self): |
| 50 | + # Ensure that slicing a coordinate behaves like slicing a numpy array |
| 51 | + # i.e. that the points and bounds are views of the original. |
| 52 | + original = DimCoord([1, 2], bounds=[[1, 2], [2, 3]], |
| 53 | + attributes={'dummy1': None}, |
| 54 | + coord_system=tests.mock.sentinel.coord_system) |
| 55 | + sliced_coord = original[:] |
| 56 | + self.assertIs(fetch_base(sliced_coord._points), |
| 57 | + fetch_base(original._points)) |
| 58 | + self.assertIs(fetch_base(sliced_coord._bounds), |
| 59 | + fetch_base(original._bounds)) |
| 60 | + self.assertIsNot(sliced_coord.coord_system, original.coord_system) |
| 61 | + self.assertIsNot(sliced_coord.attributes, original.attributes) |
| 62 | + |
| 63 | + def test_lazy_data_realisation(self): |
| 64 | + # Capture the fact that we realise the data when slicing. |
| 65 | + points = np.array([1, 2]) |
| 66 | + points = as_lazy_data(points) |
| 67 | + |
| 68 | + bounds = np.array([[1, 2], [2, 3]]) |
| 69 | + bounds = as_lazy_data(bounds) |
| 70 | + |
| 71 | + original = DimCoord(points, bounds=bounds, |
| 72 | + attributes={'dummy1': None}, |
| 73 | + coord_system=tests.mock.sentinel.coord_system) |
| 74 | + sliced_coord = original[:] |
| 75 | + # Returned coord is realised. |
| 76 | + self.assertIsInstance(sliced_coord._points, np.ndarray) |
| 77 | + self.assertIsInstance(sliced_coord._bounds, np.ndarray) |
| 78 | + |
| 79 | + # Original coord remains unrealised. |
| 80 | + self.assertIsInstance(points, dask.array.core.Array) |
| 81 | + self.assertIsInstance(bounds, dask.array.core.Array) |
| 82 | + |
| 83 | + |
| 84 | +class Test_copy(tests.IrisTest): |
| 85 | + def setUp(self): |
| 86 | + self.original = DimCoord([1, 2], bounds=[[1, 2], [2, 3]], |
| 87 | + attributes={'dummy1': None}, |
| 88 | + coord_system=tests.mock.sentinel.coord_system) |
| 89 | + |
| 90 | + def assert_data_no_share(self, coord_copy): |
| 91 | + self.assertIsNot(fetch_base(coord_copy._points), |
| 92 | + fetch_base(self.original._points)) |
| 93 | + self.assertIsNot(fetch_base(coord_copy._bounds), |
| 94 | + fetch_base(self.original._bounds)) |
| 95 | + self.assertIsNot(coord_copy.coord_system, self.original.coord_system) |
| 96 | + self.assertIsNot(coord_copy.attributes, self.original.attributes) |
| 97 | + |
| 98 | + def test_existing_points(self): |
| 99 | + # Ensure that copying a coordinate does not return a view of its |
| 100 | + # points or bounds. |
| 101 | + coord_copy = self.original.copy() |
| 102 | + self.assert_data_no_share(coord_copy) |
| 103 | + |
| 104 | + def test_existing_points_deepcopy_call(self): |
| 105 | + # Ensure that the coordinate object itself is deepcopied called. |
| 106 | + cp_orig = copy.deepcopy(self.original) |
| 107 | + with tests.mock.patch('copy.deepcopy', return_value=cp_orig) as \ |
| 108 | + mock_copy: |
| 109 | + self.original.copy() |
| 110 | + mock_copy.assert_called_once_with(self.original) |
| 111 | + |
| 112 | + def test_new_points(self): |
| 113 | + coord_copy = self.original.copy([1, 2], bounds=[[1, 2], [2, 3]]) |
| 114 | + self.assert_data_no_share(coord_copy) |
| 115 | + |
| 116 | + def test_new_points_shallowcopy_call(self): |
| 117 | + # Ensure that the coordinate object itself is shallow copied so that |
| 118 | + # the points and bounds are not unnecessarily copied. |
| 119 | + cp_orig = copy.copy(self.original) |
| 120 | + with tests.mock.patch('copy.copy', return_value=cp_orig) as mock_copy: |
| 121 | + self.original.copy([1, 2], bounds=[[1, 2], [2, 3]]) |
| 122 | + mock_copy.assert_called_once_with(self.original) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + tests.main() |
0 commit comments