Skip to content

Picking up #1118: Do not convert subclasses of ndarray unless required #2956

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

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
dcc4bfe
import the unittest test suite for quantities
keewis May 12, 2019
f6925be
make sure no divide by zero occurs
keewis May 12, 2019
ef13531
use asanyarray instead of asarray in as_compatible_data
keewis May 12, 2019
b373ecf
preserve ndarray subclasses with the data accessor
keewis May 12, 2019
a2aced1
now the sel test passes, too, so don't xfail it
keewis May 12, 2019
97683a4
remove the last divide-by-zero possibility
keewis May 12, 2019
2ece12b
add quantities to some of the requirements files
keewis May 12, 2019
7a25fb6
rename the test file to match the name of the original test file
keewis May 12, 2019
4348e0b
remove trailing whitespace
keewis May 12, 2019
dbeaed8
fix a typo
keewis May 13, 2019
f792478
replace the single data fixture with multiple smaller ones
keewis May 13, 2019
b2e3ae2
add a test for combining data arrays
keewis May 13, 2019
453c693
replace the requires_quantities decorator with skipif on module level
keewis May 13, 2019
0d4b543
convert the test methods from the namespace class to functions
keewis May 13, 2019
8beaf76
also check that units on the data itself survive
keewis May 13, 2019
b4d4288
fix the order of imports
keewis May 19, 2019
1ad1d6d
assert in the comparison function instead of asserting the result
keewis May 20, 2019
2b654a5
use data creation helpers instead of data fixtures
keewis May 20, 2019
c52bdf4
add an option to switch on the support for subclasses
keewis May 20, 2019
92e62b3
modify duck_array_ops.asarray to work like asanyarray if enabled
keewis May 20, 2019
280abf3
add a function that uses asanyarray instead of asarray if the option …
keewis May 20, 2019
24d2771
use the new asarray function instead of using options directly
keewis May 20, 2019
2ea846e
explicitly convert matrix objects to ndarrays
keewis May 20, 2019
5a4db0c
wrap the option name and validator lines
keewis May 20, 2019
9809596
add tests to ensure the matrix and MaskedArray classes get converted
keewis May 20, 2019
b4cab61
fix the indentation of a parenthesis
keewis May 20, 2019
6f398e5
fix the line length of a decorator call
keewis May 20, 2019
54522e3
Merge commit 'f172c673' into member-arrays-with-units
keewis Aug 19, 2019
ee15176
black
keewis Aug 19, 2019
3bc5c5c
black2
keewis Aug 19, 2019
c1e513a
Merge commit 'd089df38' into member-arrays-with-units
keewis Aug 19, 2019
5477bca
Merge branch 'master' into member-arrays-with-units
keewis Aug 19, 2019
c787809
move the function deciding between asarray and asanyarray to npcompat
keewis Aug 19, 2019
c653eaa
make sure the original arrays are used as comparison
keewis Aug 19, 2019
5aee870
isort
keewis Aug 19, 2019
c2944c5
allow passing custom arrays to the helper functions
keewis Aug 20, 2019
8e7d7ce
create the test data in the tests to increase the readability
keewis Aug 20, 2019
25f5800
Merge branch 'master' into member-arrays-with-units
keewis Aug 20, 2019
e13e273
black
keewis Aug 21, 2019
a89a1e5
reuse the coordinate dict
keewis Aug 21, 2019
fe6a799
ignore the missing type annotations for quantities
keewis Aug 22, 2019
c4d8512
Merge branch 'master' into member-arrays-with-units
keewis Aug 26, 2019
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
Prev Previous commit
Next Next commit
add an option to switch on the support for subclasses
  • Loading branch information
keewis committed May 20, 2019
commit c52bdf4d14bb7856ec32c8c41bfc860b51db3ccc
10 changes: 8 additions & 2 deletions xarray/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
CMAP_SEQUENTIAL = 'cmap_sequential'
CMAP_DIVERGENT = 'cmap_divergent'
KEEP_ATTRS = 'keep_attrs'
ENABLE_EXPERIMENTAL_NDARRAY_SUBCLASS_SUPPORT = 'enable_experimental_ndarray_subclass_support'


OPTIONS = {
Expand All @@ -18,7 +19,8 @@
WARN_FOR_UNCLOSED_FILES: False,
CMAP_SEQUENTIAL: 'viridis',
CMAP_DIVERGENT: 'RdBu_r',
KEEP_ATTRS: 'default'
KEEP_ATTRS: 'default',
ENABLE_EXPERIMENTAL_NDARRAY_SUBCLASS_SUPPORT: False,
}

_JOIN_OPTIONS = frozenset(['inner', 'outer', 'left', 'right', 'exact'])
Expand All @@ -34,7 +36,8 @@ def _positive_integer(value):
ENABLE_CFTIMEINDEX: lambda value: isinstance(value, bool),
FILE_CACHE_MAXSIZE: _positive_integer,
WARN_FOR_UNCLOSED_FILES: lambda value: isinstance(value, bool),
KEEP_ATTRS: lambda choice: choice in [True, False, 'default']
KEEP_ATTRS: lambda choice: choice in [True, False, 'default'],
ENABLE_EXPERIMENTAL_NDARRAY_SUBCLASS_SUPPORT: lambda value: isinstance(value, bool),
}


Expand Down Expand Up @@ -96,6 +99,9 @@ class set_options:
attrs, ``False`` to always discard them, or ``'default'`` to use original
logic that attrs should only be kept in unambiguous circumstances.
Default: ``'default'``.
- ``enable_experimental_ndarray_subclass_support``: whether or not
to enable the support for subclasses of numpy's ndarray.
Default: ``False``.

You can use ``set_options`` either as a context manager:

Expand Down
11 changes: 8 additions & 3 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .indexing import (
BasicIndexer, OuterIndexer, PandasIndexAdapter, VectorizedIndexer,
as_indexable)
from .options import _get_keep_attrs
from .options import _get_keep_attrs, OPTIONS
from .pycompat import TYPE_CHECKING, dask_array_type, integer_types
from .utils import (
OrderedSet, decode_numpy_dict_values, either_dict_or_kwargs,
Expand Down Expand Up @@ -183,7 +183,10 @@ def as_compatible_data(data, fastpath=False):
data = np.asarray(data)

# validate whether the data is valid data types
data = np.asanyarray(data)
if OPTIONS["enable_experimental_ndarray_subclass_support"]:
data = np.asanyarray(data)
else:
data = np.asarray(data)

if isinstance(data, np.ndarray):
if data.dtype.kind == 'O':
Expand Down Expand Up @@ -308,8 +311,10 @@ def _in_memory(self):
def data(self):
if isinstance(self._data, dask_array_type):
return self._data
else:
elif OPTIONS["enable_experimental_ndarray_subclass_support"]:
return _as_any_array_or_item(self._data)
else:
return self.values

@data.setter
def data(self, data):
Expand Down
11 changes: 10 additions & 1 deletion xarray/tests/test_quantities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from xarray import DataArray
from xarray import DataArray, set_options

try:
import quantities as pq
Expand All @@ -15,6 +15,9 @@
)


set_options(enable_experimental_ndarray_subclass_support=True)


def assert_equal_with_units(a, b):
a = a if not isinstance(a, DataArray) else a.data
b = b if not isinstance(b, DataArray) else b.data
Expand Down Expand Up @@ -69,6 +72,12 @@ def with_keys(mapping, keys):
}


def test_without_subclass_support():
with set_options(enable_experimental_ndarray_subclass_support=False):
data_array = create_data_array()
assert not hasattr(data_array.data, "units")


def test_units_in_data_and_coords():
data_array = create_data_array()

Expand Down