Skip to content
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

Document that Coarsen accepts coord func as callable #7981

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

TomNicholas
Copy link
Member

@TomNicholas TomNicholas commented Jul 12, 2023

Documents a hidden feature I noticed yesterday, corrects incorrect docstrings, and tidies up some of the typing internally.

  • Closes #xxxx
  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice improvement, thanks!
I have run into problems regarding this in the past :)

boundary: CoarsenBoundaryOptions = "exact",
side: SideOptions | Mapping[Any, SideOptions] = "left",
keep_attrs=None,
**kwargs,
):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While at it you could add a return type.

A mapping from the name of the dimension to create the coarsened block along (e.g. `time`) to the size of
the coarsened block.
func : function or str name of function
Function to use to reduce the values of one block down along one or more axes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention the signature of the function.
I myself have already run into the problem if not knowing what exactly you have to supply.

Also, I never know from which "pool" you can choose the str names, maybe this can be documented better? (Same for the other docstrings)

Copy link
Member Author

@TomNicholas TomNicholas Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I never know from which "pool" you can choose the str names, maybe this can be documented better? (Same for the other docstrings)

Yeah this seems like an issue. It's currently literally just any function in the internal duck_array_ops module, which (a) is hacky AF, (b) is not documented or even very easy to document, and (c) could fail (there are fn's in duck_array_ops that aren't reductions, for example). I'm not aware of anywhere else in xarray's codebase where we just implicitly allow attempting to get any function from duck_array_ops like this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add a public variable in the duck_arr_ops module that collects valid reduction names and add a reference to it?

If 'exact', a ValueError will be raised if dimension size is not a
multiple of window size. If 'trim', the excess indexes are trimmed.
If 'pad', NA will be padded.
side : 'left' or 'right' or mapping from dimension to 'left' or 'right'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add **kwargs as being passed to func.

windows : mapping of hashable to int
A mapping from the name of the dimension to create the coarsened block along (e.g. `time`) to the size of
the coarsened block.
func : function or str name of function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func : function or str name of function
func : Callable or str name of function

boundary : {"exact", "trim", "pad"}
If 'exact', a ValueError will be raised if dimension size is not a
multiple of window size. If 'trim', the excess indexes are trimmed.
If 'pad', NA will be padded.
side : 'left' or 'right' or mapping from dimension to 'left' or 'right'
coord_func : function (name) or mapping from coordinate name to function (name).
coord_func : function, str name of function, or mapping from coordinate name to function or str name of func.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
coord_func : function, str name of function, or mapping from coordinate name to function or str name of func.
coord_func : Callable, str name of function, or mapping from coordinate name to Callable or str name of func.

Also probably more readable that way because you don't have 4 times "function" in one sentence.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add an explicit list of function names

self, windows, func, boundary="exact", side="left", keep_attrs=None, **kwargs
self,
windows: Mapping[Any, int],
func: str | Callable,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func: str | Callable,
func: Literal["max", "min", ...] | Callable,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants