This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
ENH: Implemented max, min and sum operators #72
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# (C) British Crown Copyright 2014, Met Office | ||
# | ||
# This file is part of Biggus. | ||
# | ||
# Biggus 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. | ||
# | ||
# Biggus 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 Biggus. If not, see <http://www.gnu.org/licenses/>. | ||
"""Unit tests for `biggus` aggregation operators.""" | ||
from abc import ABCMeta, abstractproperty | ||
|
||
import numpy as np | ||
import numpy.ma as ma | ||
|
||
import biggus | ||
|
||
|
||
class Operator(object): | ||
__metaclass__ = ABCMeta | ||
|
||
@abstractproperty | ||
def biggus_operator(self): | ||
pass | ||
|
||
@abstractproperty | ||
def numpy_operator(self): | ||
pass | ||
|
||
@abstractproperty | ||
def numpy_masked_operator(self): | ||
pass | ||
|
||
|
||
class InvalidAxis(Operator): | ||
def setUp(self): | ||
self.array = biggus.NumpyArrayAdapter(np.arange(12)) | ||
|
||
def test_none(self): | ||
with self.assertRaises(AssertionError): | ||
self.biggus_operator(self.array) | ||
|
||
def test_too_large(self): | ||
with self.assertRaises(ValueError): | ||
self.biggus_operator(self.array, axis=1) | ||
|
||
def test_too_small(self): | ||
with self.assertRaises(ValueError): | ||
self.biggus_operator(self.array, axis=-2) | ||
|
||
def test_multiple(self): | ||
array = biggus.NumpyArrayAdapter(np.arange(12).reshape(3, 4)) | ||
with self.assertRaises(AssertionError): | ||
self.biggus_operator(array, axis=(0, 1)) | ||
|
||
|
||
class AggregationDtype(Operator): | ||
def _check(self, source): | ||
# Default behaviour is for operators which inherrit their dtype from | ||
# the objects they perform the aggregation. | ||
array = biggus.NumpyArrayAdapter(np.arange(2, dtype=source)) | ||
agg = self.biggus_operator(array, axis=0) | ||
self.assertEqual(agg.dtype, source) | ||
|
||
def test_dtype_equal_source_dtype(self): | ||
dtypes = [np.int8, np.int16, np.int32, np.int] | ||
for dtype in dtypes: | ||
self._check(dtype) | ||
|
||
|
||
class NumpyArrayAdapter(Operator): | ||
def setUp(self): | ||
self.data = np.arange(12) | ||
|
||
def _check(self, data, dtype=None, shape=None): | ||
data = np.asarray(data, dtype=dtype) | ||
if shape is not None: | ||
data = data.reshape(shape) | ||
array = biggus.NumpyArrayAdapter(data) | ||
result = self.biggus_operator(array, axis=0).ndarray() | ||
expected = self.numpy_operator(data, axis=0) | ||
if expected.ndim == 0: | ||
expected = np.asarray(expected) | ||
np.testing.assert_array_equal(result, expected) | ||
|
||
def test_flat_int(self): | ||
self._check(self.data) | ||
|
||
def test_multi_int(self): | ||
self._check(self.data, shape=(3, 4)) | ||
|
||
def test_flat_float(self): | ||
self._check(self.data, dtype=np.float) | ||
|
||
def test_multi_float(self): | ||
self._check(self.data, dtype=np.float, shape=(3, 4)) | ||
|
||
|
||
class NumpyArrayAdapterMasked(): | ||
def _check(self, data): | ||
array = biggus.NumpyArrayAdapter(data) | ||
result = self.biggus_operator(array, axis=0).masked_array() | ||
expected = self.numpy_masked_operator(data, axis=0) | ||
if expected.ndim == 0: | ||
if expected is np.ma.masked: | ||
expected = ma.asarray(expected, dtype=array.dtype) | ||
else: | ||
expected = ma.asarray(expected) | ||
np.testing.assert_array_equal(result.filled(), expected.filled()) | ||
np.testing.assert_array_equal(result.mask, expected.mask) | ||
|
||
def test_no_mask_flat(self): | ||
for dtype in [np.int, np.float]: | ||
data = ma.arange(12, dtype=dtype) | ||
self._check(data) | ||
|
||
def test_no_mask_multi(self): | ||
for dtype in [np.int, np.float]: | ||
data = ma.arange(12, dtype=dtype).reshape(3, 4) | ||
self._check(data) | ||
|
||
def test_flat(self): | ||
for dtype in [np.int, np.float]: | ||
data = ma.arange(12, dtype=dtype) | ||
data[::2] = ma.masked | ||
self._check(data) | ||
|
||
data.mask = ma.nomask | ||
data[1::2] = ma.masked | ||
self._check(data) | ||
|
||
def test_all_masked(self): | ||
data = ma.arange(12, dtype=np.int) | ||
data[:] = ma.masked | ||
self._check(data) | ||
|
||
def test_multi(self): | ||
for dtype in [np.int, np.float]: | ||
data = ma.arange(12, dtype=dtype) | ||
data[::2] = ma.masked | ||
self._check(data.reshape(3, 4)) | ||
|
||
data = ma.arange(12, dtype=dtype) | ||
data[1::2] = ma.masked | ||
self._check(data.reshape(3, 4)) | ||
|
||
data = ma.arange(12, dtype=dtype).reshape(3, 4) | ||
data[::2] = ma.masked | ||
self._check(data) | ||
|
||
data = ma.arange(12, dtype=dtype).reshape(3, 4) | ||
data[1::2] = ma.masked | ||
self._check(data) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whats the best way of pulling out commonality between these different aggregators without having mixed inheritance or multiple inheritance? Better to leave as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd quite like to refactor the aggregation stream handler stuff anyway, so rather than try to do two things in one PR I'm happy to let leave these as is. They can serve as an even more obvious reminder to refactor! 😉