Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cube.exponentiate lazy #1846

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/iris/analysis/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ def exponentiate(cube, exponent, in_place=False):
"""
_assert_is_cube(cube)

def power(data, out=None):
return np.power(data, exponent, out)
def power(data):
return biggus.power(data, exponent)

return _math_op_common(cube, power, cube.units ** exponent,
in_place=in_place)
Expand Down Expand Up @@ -656,11 +656,7 @@ def _math_op_common(cube, operation_function, new_unit, in_place=False):
_assert_is_cube(cube)
if in_place:
new_cube = cube
try:
operation_function(new_cube._my_data, out=new_cube._my_data)
except TypeError:
# Non ufunc function
operation_function(new_cube.data)
new_cube._my_data = operation_function(new_cube._my_data)
else:
new_cube = cube.copy(data=operation_function(cube._my_data))
iris.analysis.clear_phenomenon_identity(new_cube)
Expand Down
6 changes: 3 additions & 3 deletions lib/iris/tests/test_basic_maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,10 @@ def vec_mag(u, v):
vec_mag_ufunc = np.frompyfunc(vec_mag, 2, 1)
b = iris.analysis.maths.apply_ufunc(vec_mag_ufunc, a, c)

ans = a.data**2 + c.data**2
b2 = b**2
ans = np.sqrt(a.data**2 + c.data**2)
# b2 = b**2

self.assertArrayAlmostEqual(b2.data, ans)
self.assertArrayAlmostEqual(b.data, ans)

class TestIFunc(tests.IrisTest):
def setUp(self):
Expand Down
94 changes: 94 additions & 0 deletions lib/iris/tests/unit/analysis/maths/test_exponentiate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# (C) British Crown Copyright 2015, Met Office
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the license header is the only problem, we might be ok after all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is far from being our only problem I suspect... Is it really complaining about the license header??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, but the tests didn't complete, so...

#
# This file is part of Iris.
#
# Iris is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Iris is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for the :func:`iris.analysis.maths.exponentiate` function."""

from __future__ import (absolute_import, division, print_function)
from six.moves import (filter, input, map, range, zip) # noqa

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests
from iris.tests.stock import simple_2d

from biggus import NumpyArrayAdapter
import iris
from iris.analysis.maths import exponentiate
import numpy as np


class Test_exponentiate(tests.IrisTest):
def setUp(self):
self.cube = simple_2d(with_bounds=False)
self.exponent = np.float32(2)

def test_basic(self):
expected = self.cube.data ** self.exponent
result = exponentiate(self.cube, self.exponent)
self.assertArrayEqual(result.data, expected)

def test_masked(self):
cube = self.cube.copy()
mask = cube.data % 3 == 0
masked_data = np.ma.masked_array(cube.data, mask)
cube.data = masked_data
expected = masked_data ** 2
result = exponentiate(cube, self.exponent)
self.assertMaskedArrayEqual(result.data, expected)

def test_lazy_data__inplace(self):
# Confirm that the cube's lazy data is preserved through an operation
# that is not in-place.
cube = self.cube.copy()
cube.lazy_data(array=NumpyArrayAdapter(cube.data))
expected = cube.copy().data ** self.exponent
exponentiate(cube, self.exponent, in_place=True)
self.assertTrue(cube.has_lazy_data())
self.assertArrayAlmostEqual(cube.data, expected)

def test_lazy_data__not_inplace(self):
# Confirm that the cube's lazy data is preserved through an
# in-place operation.
cube = self.cube.copy()
cube.lazy_data(array=NumpyArrayAdapter(cube.data))
expected = cube.copy().data ** self.exponent
result = exponentiate(cube, self.exponent, in_place=False)
self.assertTrue(result.has_lazy_data())
self.assertArrayEqual(result.data, expected)

def test_exponentiate__preloaded_data__inplace(self):
# Confirm that the cube's data is lazy after an in-place operation and
# after pre-loading the data.
cube = self.cube.copy()
cube.lazy_data(array=NumpyArrayAdapter(cube.data))
expected = cube.data ** self.exponent
exponentiate(cube, self.exponent, in_place=True)
self.assertTrue(cube.has_lazy_data())
self.assertArrayAlmostEqual(cube.data, expected)

def test_exponentiate__preloaded_data__not_inplace(self):
# Confirm that the cube's data is lazy after an operation that is not
# in-place and after pre-loading the data.
cube = self.cube.copy()
cube.lazy_data(array=NumpyArrayAdapter(cube.data))
expected = cube.data ** self.exponent
result = exponentiate(cube, self.exponent, in_place=False)
self.assertTrue(result.has_lazy_data())
self.assertArrayAlmostEqual(result.data, expected)


if __name__ == "__main__":
tests.main()