This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Avoid setting the .data
attribute
#156
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -33,6 +33,7 @@ def test_esmlab_accessor(): | |
attrs = {'calendar': 'noleap', 'units': 'days since 2000-01-01 00:00:00'} | ||
ds.time.attrs = attrs | ||
esm = ds.esmlab.set_time(time_coord_name='time') | ||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
# Time and Time bound Attributes | ||
expected = dict(esm.time_attrs) | ||
attrs['bounds'] = None | ||
|
@@ -96,6 +97,7 @@ def test_datetime_cftime_exception(): | |
|
||
def test_time_bound_var(dset, time_coord_name='time'): | ||
esm = dset.esmlab.set_time(time_coord_name=time_coord_name) | ||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
results = esm.tb_name, esm.tb_dim | ||
expected = ('time_bound', 'd2') | ||
assert results == expected | ||
|
@@ -114,6 +116,7 @@ def test_get_time_attrs(dset, time_coord_name='time'): | |
'bounds': 'time_bound', | ||
} | ||
esm = dset.esmlab.set_time(time_coord_name=time_coord_name) | ||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
results = esm.time_attrs | ||
assert results == expected | ||
|
||
|
@@ -122,12 +125,14 @@ def test_compute_time_var(dset, time_coord_name='time'): | |
idx = dset.indexes[time_coord_name] | ||
assert isinstance(idx, pd.core.indexes.numeric.Index) | ||
esm = dset.esmlab.set_time(time_coord_name=time_coord_name) | ||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
results = esm.get_time_decoded() | ||
assert isinstance(results, xr.DataArray) | ||
|
||
|
||
def test_uncompute_time_var(dset, time_coord_name='time'): | ||
esm = dset.esmlab.set_time(time_coord_name=time_coord_name) | ||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
ds = esm.compute_time_var() | ||
assert ds[time_coord_name].dtype == np.dtype('O') | ||
dset_with_uncomputed_time = esm.uncompute_time_var() | ||
|
@@ -137,6 +142,7 @@ def test_uncompute_time_var(dset, time_coord_name='time'): | |
# For some strange reason, this case fails when using pytest parametrization | ||
def test_sel_time_(dset): | ||
esm = dset.esmlab.set_time() | ||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
dset = esm.sel_time(indexer_val=slice('1850-01-01', '1850-12-31'), year_offset=1850) | ||
assert len(dset.time) == 12 | ||
|
||
|
@@ -155,10 +161,10 @@ def test_sel_time_(dset): | |
def test_mon_climatology(ds_name, decoded, variables, time_coord_name): | ||
ds = esmlab.datasets.open_dataset(ds_name, decode_times=decoded) | ||
esm = ds.esmlab.set_time(time_coord_name=time_coord_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Done. |
||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
computed_dset = esmlab.climatology(ds, freq='mon') | ||
esmlab_res = computed_dset.drop(esm.static_variables).to_dataframe() | ||
esmlab_res = computed_dset.to_dataframe() | ||
esmlab_res = esmlab_res.groupby('month').mean()[variables] | ||
|
||
df = ( | ||
esm._ds_time_computed.drop(esm.static_variables) | ||
.to_dataframe() | ||
|
@@ -171,11 +177,6 @@ def test_mon_climatology(ds_name, decoded, variables, time_coord_name): | |
|
||
assert_both_frames_equal(esmlab_res, pd_res) | ||
|
||
assert computed_dset[esm.time_coord_name].dtype == ds[esm.time_coord_name].dtype | ||
for key, value in ds[esm.time_coord_name].attrs.items(): | ||
assert key in computed_dset[esm.time_coord_name].attrs | ||
assert value == computed_dset[esm.time_coord_name].attrs[key] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'ds_name, decoded, variables, time_coord_name, time_bound_name', | ||
|
@@ -196,9 +197,10 @@ def test_mon_climatology_drop_time_bounds( | |
ds = ds.drop(ds_time_bound) | ||
del ds[time_coord_name].attrs[time_bound_name] | ||
esm = ds.esmlab.set_time(time_coord_name=time_coord_name) | ||
xr.testing._assert_internal_invariants(esm._ds_time_computed) | ||
computed_dset = esmlab.climatology(ds, freq='mon') | ||
|
||
esmlab_res = computed_dset.drop(esm.static_variables).to_dataframe() | ||
esmlab_res = computed_dset.to_dataframe() | ||
esmlab_res = esmlab_res.groupby('month').mean()[variables] | ||
|
||
df = ( | ||
|
@@ -213,11 +215,6 @@ def test_mon_climatology_drop_time_bounds( | |
|
||
assert_both_frames_equal(esmlab_res, pd_res) | ||
|
||
assert computed_dset[esm.time_coord_name].dtype == ds[esm.time_coord_name].dtype | ||
for key, value in ds[esm.time_coord_name].attrs.items(): | ||
assert key in computed_dset[esm.time_coord_name].attrs | ||
assert value == computed_dset[esm.time_coord_name].attrs[key] | ||
|
||
|
||
def test_anomaly_with_monthly_clim(dset): | ||
computed_dset = esmlab.anomaly(dset, clim_freq='mon') | ||
|
@@ -276,7 +273,6 @@ def test_resample_mon_mean(dset): | |
for method in {'left', 'right'}: | ||
ds = esmlab.resample(dset, freq='mon', method=method) | ||
assert len(ds.time) == 12 | ||
|
||
computed_dset = esmlab.resample(dset, freq='mon') | ||
res = computed_dset.variable_1.data | ||
expected = np.full(shape=(12, 2, 2), fill_value=0.5, dtype=np.float32) | ||
|
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.
keep
.data
on RHS? But it seems like you're rewriting attributes later, in which case this might not matter.