Skip to content

How to get apply_ufunc work with scipy's Mann-Whitney U test? #7845

Closed Answered by keewis
nicrie asked this question in Q&A
Discussion options

You must be logged in to vote

The issue is this (from the documentation of the input_core_dims parameter):

Core dimensions are automatically moved to the last axes of input variables before applying func, which facilitates using NumPy style generalized ufuncs [2].

which means that we can't just use the default axis=0 but have to set axis=-1. This should work:

def MWU(x, y, dim):
    """ xarray wrapper for scipy.stats.mannwhitneyu """
    return xr.apply_ufunc(
        scipy.stats.mannwhitneyu,
        x,
        y,
        input_core_dims=[[dim], [dim]],
        output_core_dims=[[], []],
        vectorize=False  # I don't want to vectorize because speed is important
        kwargs={"axis": -1},
    )

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@kmuehlbauer
Comment options

@nicrie
Comment options

Answer selected by nicrie
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants