-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
implement Gradient #2398
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
Merged
Merged
implement Gradient #2398
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
59ff688
Added xr.gradient, DataArray.gradient, Dataset.gradient
fujiisoup 0adfc68
Working with np.backend
fujiisoup d665b3c
test is not passing
fujiisoup e0fa5fd
Docs
fujiisoup 218e62d
flake8
fujiisoup 888b924
support environment without dask
fujiisoup a0ab4c2
Support numpy < 1.13
fujiisoup c581513
Support numpy 1.12
fujiisoup d6be041
simplify dask.gradient
fujiisoup a083460
lint
fujiisoup 267694d
Use npcompat.gradient in tests
fujiisoup bf2f35e
Merge branch 'master' into gradient
fujiisoup 2a71b62
move gradient to dask_array_compat
fujiisoup b504da8
gradient -> differentiate
fujiisoup fb356c5
lint
fujiisoup 1694d3c
Merge branch 'master' into gradient
fujiisoup 7a0b57f
Update dask_array_compat
fujiisoup e93b926
Added a link from diff
fujiisoup 4c656e0
Merge branch 'master' into gradient
fujiisoup 74acc81
remove xr.differentiate
fujiisoup 8817306
Added datetime support
fujiisoup 4112cd9
Update via comment. Use utils.to_numeric also in interp
fujiisoup 1c2c88c
time_unit -> datetime_unit
fujiisoup cbfecb4
Some more info in docs.
fujiisoup 2e8db19
update test
fujiisoup c31539e
Update via comments
fujiisoup 528bcab
Update docs.
fujiisoup 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -13,8 +13,8 @@ | |
import xarray as xr | ||
|
||
from . import ( | ||
alignment, duck_array_ops, formatting, groupby, indexing, ops, resample, | ||
rolling, utils) | ||
alignment, computation, duck_array_ops, formatting, groupby, indexing, ops, | ||
resample, rolling, utils) | ||
from .. import conventions | ||
from .alignment import align | ||
from .common import ( | ||
|
@@ -31,7 +31,7 @@ | |
OrderedDict, basestring, dask_array_type, integer_types, iteritems, range) | ||
from .utils import ( | ||
Frozen, SortedKeysDict, either_dict_or_kwargs, decode_numpy_dict_values, | ||
ensure_us_time_resolution, hashable, maybe_wrap_array) | ||
ensure_us_time_resolution, hashable, maybe_wrap_array, to_numeric) | ||
from .variable import IndexVariable, Variable, as_variable, broadcast_variables | ||
|
||
# list of attributes of pd.DatetimeIndex that are ndarrays of time info | ||
|
@@ -3313,6 +3313,9 @@ def diff(self, dim, n=1, label='upper'): | |
Data variables: | ||
foo (x) int64 1 -1 | ||
|
||
See Also | ||
-------- | ||
Dataset.differentiate | ||
""" | ||
if n == 0: | ||
return self | ||
|
@@ -3663,6 +3666,62 @@ def rank(self, dim, pct=False, keep_attrs=False): | |
attrs = self.attrs if keep_attrs else None | ||
return self._replace_vars_and_dims(variables, coord_names, attrs=attrs) | ||
|
||
def differentiate(self, coord, edge_order=1, datetime_unit=None): | ||
""" Differentiate with the second order accurate central | ||
differences. | ||
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. and here |
||
|
||
.. note:: | ||
This feature is limited to simple cartesian geometry, i.e. coord | ||
must be one dimensional. | ||
|
||
Parameters | ||
---------- | ||
coord: str | ||
The coordinate to be used to compute the gradient. | ||
edge_order: 1 or 2. Default 1 | ||
N-th order accurate differences at the boundaries. | ||
datetime_unit: None or any of {'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', | ||
'us', 'ns', 'ps', 'fs', 'as'} | ||
Unit to compute gradient. Only valid for datetime coordinate. | ||
|
||
Returns | ||
------- | ||
differentiated: Dataset | ||
|
||
See also | ||
-------- | ||
numpy.gradient: corresponding numpy function | ||
""" | ||
from .variable import Variable | ||
|
||
if coord not in self.variables and coord not in self.dims: | ||
raise ValueError('Coordinate {} does not exist.'.format(coord)) | ||
|
||
coord_var = self[coord].variable | ||
if coord_var.ndim != 1: | ||
raise ValueError('Coordinate {} must be 1 dimensional but is {}' | ||
' dimensional'.format(coord, coord_var.ndim)) | ||
|
||
dim = coord_var.dims[0] | ||
coord_data = coord_var.data | ||
if coord_data.dtype.kind in 'mM': | ||
if datetime_unit is None: | ||
datetime_unit, _ = np.datetime_data(coord_data.dtype) | ||
coord_data = to_numeric(coord_data, datetime_unit=datetime_unit) | ||
|
||
variables = OrderedDict() | ||
for k, v in self.variables.items(): | ||
if (k in self.data_vars and dim in v.dims and | ||
k not in self.coords): | ||
v = to_numeric(v, datetime_unit=datetime_unit) | ||
grad = duck_array_ops.gradient( | ||
v.data, coord_data, edge_order=edge_order, | ||
axis=v.get_axis_num(dim)) | ||
variables[k] = Variable(v.dims, grad) | ||
else: | ||
variables[k] = v | ||
return self._replace_vars_and_dims(variables) | ||
|
||
@property | ||
def real(self): | ||
return self._unary_op(lambda x: x.real, keep_attrs=True)(self) | ||
|
@@ -3676,7 +3735,7 @@ def filter_by_attrs(self, **kwargs): | |
|
||
Can pass in ``key=value`` or ``key=callable``. A Dataset is returned | ||
containing only the variables for which all the filter tests pass. | ||
These tests are either ``key=value`` for which the attribute ``key`` | ||
These tests are either ``key=value`` for which the attribute ``key`` | ||
has the exact value ``value`` or the callable passed into | ||
``key=callable`` returns True. The callable will be passed a single | ||
value, either the value of the attribute ``key`` or ``None`` if the | ||
|
This file contains hidden or 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
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.
and here