forked from pydata/xarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_options.py
170 lines (133 loc) · 5.59 KB
/
test_options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from __future__ import absolute_import, division, print_function
import pytest
import xarray
from xarray.core.options import OPTIONS, _get_keep_attrs
from xarray.backends.file_manager import FILE_CACHE
from xarray.tests.test_dataset import create_test_data
from xarray import concat, merge
def test_invalid_option_raises():
with pytest.raises(ValueError):
xarray.set_options(not_a_valid_options=True)
def test_display_width():
with pytest.raises(ValueError):
xarray.set_options(display_width=0)
with pytest.raises(ValueError):
xarray.set_options(display_width=-10)
with pytest.raises(ValueError):
xarray.set_options(display_width=3.5)
def test_arithmetic_join():
with pytest.raises(ValueError):
xarray.set_options(arithmetic_join='invalid')
with xarray.set_options(arithmetic_join='exact'):
assert OPTIONS['arithmetic_join'] == 'exact'
def test_enable_cftimeindex():
with pytest.raises(ValueError):
xarray.set_options(enable_cftimeindex=None)
with xarray.set_options(enable_cftimeindex=True):
assert OPTIONS['enable_cftimeindex']
def test_file_cache_maxsize():
with pytest.raises(ValueError):
xarray.set_options(file_cache_maxsize=0)
original_size = FILE_CACHE.maxsize
with xarray.set_options(file_cache_maxsize=123):
assert FILE_CACHE.maxsize == 123
assert FILE_CACHE.maxsize == original_size
def test_keep_attrs():
with pytest.raises(ValueError):
xarray.set_options(keep_attrs='invalid_str')
with xarray.set_options(keep_attrs=True):
assert OPTIONS['keep_attrs']
with xarray.set_options(keep_attrs=False):
assert not OPTIONS['keep_attrs']
with xarray.set_options(keep_attrs='default'):
assert _get_keep_attrs(default=True)
assert not _get_keep_attrs(default=False)
def test_nested_options():
original = OPTIONS['display_width']
with xarray.set_options(display_width=1):
assert OPTIONS['display_width'] == 1
with xarray.set_options(display_width=2):
assert OPTIONS['display_width'] == 2
assert OPTIONS['display_width'] == 1
assert OPTIONS['display_width'] == original
def create_test_dataset_attrs(seed=0):
ds = create_test_data(seed)
ds.attrs = {'attr1': 5, 'attr2': 'history',
'attr3': {'nested': 'more_info'}}
return ds
def create_test_dataarray_attrs(seed=0, var='var1'):
da = create_test_data(seed)[var]
da.attrs = {'attr1': 5, 'attr2': 'history',
'attr3': {'nested': 'more_info'}}
return da
class TestAttrRetention(object):
def test_dataset_attr_retention(self):
# Use .mean() for all tests: a typical reduction operation
ds = create_test_dataset_attrs()
original_attrs = ds.attrs
# Test default behaviour
result = ds.mean()
assert result.attrs == {}
with xarray.set_options(keep_attrs='default'):
result = ds.mean()
assert result.attrs == {}
with xarray.set_options(keep_attrs=True):
result = ds.mean()
assert result.attrs == original_attrs
with xarray.set_options(keep_attrs=False):
result = ds.mean()
assert result.attrs == {}
def test_dataarray_attr_retention(self):
# Use .mean() for all tests: a typical reduction operation
da = create_test_dataarray_attrs()
original_attrs = da.attrs
# Test default behaviour
result = da.mean()
assert result.attrs == {}
with xarray.set_options(keep_attrs='default'):
result = da.mean()
assert result.attrs == {}
with xarray.set_options(keep_attrs=True):
result = da.mean()
assert result.attrs == original_attrs
with xarray.set_options(keep_attrs=False):
result = da.mean()
assert result.attrs == {}
def test_groupby_attr_retention(self):
da = xarray.DataArray([1, 2, 3], [('x', [1, 1, 2])])
da.attrs = {'attr1': 5, 'attr2': 'history',
'attr3': {'nested': 'more_info'}}
original_attrs = da.attrs
# Test default behaviour
result = da.groupby('x').sum(keep_attrs=True)
assert result.attrs == original_attrs
with xarray.set_options(keep_attrs='default'):
result = da.groupby('x').sum(keep_attrs=True)
assert result.attrs == original_attrs
with xarray.set_options(keep_attrs=True):
result1 = da.groupby('x')
result = result1.sum()
assert result.attrs == original_attrs
with xarray.set_options(keep_attrs=False):
result = da.groupby('x').sum()
assert result.attrs == {}
def test_concat_attr_retention(self):
ds1 = create_test_dataset_attrs()
ds2 = create_test_dataset_attrs()
ds2.attrs = {'wrong': 'attributes'}
original_attrs = ds1.attrs
# Test default behaviour of keeping the attrs of the first
# dataset in the supplied list
# global keep_attrs option current doesn't affect concat
result = concat([ds1, ds2], dim='dim1')
assert result.attrs == original_attrs
@pytest.mark.xfail
def test_merge_attr_retention(self):
da1 = create_test_dataarray_attrs(var='var1')
da2 = create_test_dataarray_attrs(var='var2')
da2.attrs = {'wrong': 'attributes'}
original_attrs = da1.attrs
# merge currently discards attrs, and the global keep_attrs
# option doesn't affect this
result = merge([da1, da2])
assert result.attrs == original_attrs