-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement integrate #2653
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
Implement integrate #2653
Changes from all commits
b0e843f
968b6d0
721ff5a
3decc22
88a8385
9ed470d
42bab02
ecf9318
0561113
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3867,6 +3867,78 @@ def differentiate(self, coord, edge_order=1, datetime_unit=None): | |
variables[k] = v | ||
return self._replace_vars_and_dims(variables) | ||
|
||
def integrate(self, coord, datetime_unit=None): | ||
fujiisoup marked this conversation as resolved.
Show resolved
Hide resolved
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. Should 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. I personally think it would be a little confusing because the result may change depending on which coordinate is used for It would be a little convenient for 1d arrays, but aswe disallow default argument for |
||
""" integrate the array with the trapezoidal rule. | ||
|
||
.. note:: | ||
This feature is limited to simple cartesian geometry, i.e. coord | ||
must be one dimensional. | ||
|
||
Parameters | ||
---------- | ||
dim: str, or a sequence of str | ||
Coordinate(s) used for the integration. | ||
datetime_unit | ||
Can be specify the unit if datetime coordinate is used. One of | ||
{'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', 'ps', 'fs', | ||
'as'} | ||
|
||
Returns | ||
------- | ||
integrated: Dataset | ||
|
||
See also | ||
-------- | ||
DataArray.integrate | ||
numpy.trapz: corresponding numpy function | ||
""" | ||
if not isinstance(coord, (list, tuple)): | ||
coord = (coord, ) | ||
result = self | ||
for c in coord: | ||
result = result._integrate_one(c, datetime_unit=datetime_unit) | ||
return result | ||
|
||
def _integrate_one(self, coord, datetime_unit=None): | ||
from .variable import Variable | ||
|
||
if coord not in self.variables and coord not in self.dims: | ||
raise ValueError('Coordinate {} does not exist.'.format(dim)) | ||
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. I think splitting these checks into two would be a little clearer:
|
||
|
||
coord_var = self[coord].variable | ||
if coord_var.ndim != 1: | ||
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. I think this is currently not possible due to xarray's data model, but it's a good idea to add this anyways given that we want to change this soon (e.g., see #2405). I would recommend adjusting this to 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. I first thought that it would be nice if we could integrate even along non-dimensional (1d) coordinate (as 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. Yes, that seems reasonable to support 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. Then, 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. I don't have a strong opinion here. 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. Well, 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. OK, +1 for consistency with |
||
raise ValueError('Coordinate {} must be 1 dimensional but is {}' | ||
' dimensional'.format(coord, coord_var.ndim)) | ||
|
||
dim = coord_var.dims[0] | ||
if _contains_datetime_like_objects(coord_var): | ||
if coord_var.dtype.kind in 'mM' and datetime_unit is None: | ||
datetime_unit, _ = np.datetime_data(coord_var.dtype) | ||
elif datetime_unit is None: | ||
datetime_unit = 's' # Default to seconds for cftime objects | ||
coord_var = datetime_to_numeric( | ||
coord_var, datetime_unit=datetime_unit) | ||
|
||
variables = OrderedDict() | ||
coord_names = set() | ||
for k, v in self.variables.items(): | ||
if k in self.coords: | ||
if dim not in v.dims: | ||
variables[k] = v | ||
coord_names.add(k) | ||
else: | ||
if k in self.data_vars and dim in v.dims: | ||
if _contains_datetime_like_objects(v): | ||
v = datetime_to_numeric(v, datetime_unit=datetime_unit) | ||
integ = duck_array_ops.trapz( | ||
v.data, coord_var.data, axis=v.get_axis_num(dim)) | ||
v_dims = list(v.dims) | ||
v_dims.remove(dim) | ||
variables[k] = Variable(v_dims, integ) | ||
else: | ||
variables[k] = v | ||
return self._replace_vars_and_dims(variables, coord_names=coord_names) | ||
|
||
@property | ||
def real(self): | ||
return self._unary_op(lambda x: x.real, | ||
|
Uh oh!
There was an error while loading. Please reload this page.