Skip to content

Use return annotation to wrap tensors into ndarrays/sequences of ndarrays #83

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

Closed
wants to merge 11 commits into from

Conversation

ev-br
Copy link
Collaborator

@ev-br ev-br commented Mar 14, 2023

Annotate return values of wrapper functions to trigger generic postprocessing. No annotation mean no postprocessing of scalar returns; -> NDArray triggers ndarray(tensor) etc. There's again a need for a special annotation for functions which return either an array or a sequence of arrays (atleast_1d, where, unique). Other than that, all looks straightforward.

on top of gh-82, so it's the top of the stack based off gh-70.

NB: postprocessing currently invokes functions from _helpers. Those are straighforward to clean up/inline, once the rest of the "stack" is in.

ev-br added 10 commits March 11, 2023 11:44
Base is tracked via `self._tensor._base`. Use `a.get()._base is b.get()` instead
of numpy's `a.base is b`.
…eturn a value

On failure,  @ normalizer without arguments (default) raises, while
@Normalizer(return_or_failure=value) return a specified `value`.

This is useful, e.g. for

@-normalizer
def is_scalar(a: ArrayLike):
   ....

Consider is_scalar(int): the argument cannot be converted to tensor, so
the function returns False. But the actual failure is in the decorator,
so cannot do try-except in the body of the function!
Two cases
- just {tuple, list}[NDArrays]  (list is e.g. meshgrid)
- a variadic return of a single NDArray or a list/tuple (where, atleast_{1,2,3}d, unique)

Note that in the latter case the choice of the return type depends purely on
the number of input array_likes.
`_helpers.tuple_arrays_from` is now localized to the normalizer only
Optional arguments to the normalizer decorator turn out to be
useful for quantile/percentile, which does non-standard things
allowing zero-dim out arrays for size-out outputs etc.
@ev-br ev-br force-pushed the return_annotation branch from 35e647b to bc9bd0c Compare March 19, 2023 12:14
Copy link
Collaborator

@lezcano lezcano left a comment

Choose a reason for hiding this comment

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

Numpy returns either a Tensor, or a list / tuple or Arrays. Now, why do we need the annotations if this information is readily available in the type of result after executing the function? I think we should be able to post process everything simply based on that.


postprocessors = {
NDArray: postprocess_ndarray,
OutArray: postprocess_out,
Copy link
Collaborator

Choose a reason for hiding this comment

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

As discussed, I think we should be able to implement the out= keyword generically. Let's catch-up on this today.

@ev-br ev-br force-pushed the move_to_impl_take_2 branch 2 times, most recently from 78b64c6 to 5fea60f Compare March 23, 2023 14:19
Base automatically changed from move_to_impl_take_2 to main March 23, 2023 14:27
@ev-br ev-br closed this Mar 27, 2023
@ev-br ev-br deleted the return_annotation branch April 17, 2023 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants