Skip to content

Handle xarray DataArray in wrapped ufuncs #67

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 4 commits into from
Sep 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions gsw/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@ def wrapper(*args, **kw):
args = list(args)
args.append(p)

isarray = np.any([hasattr(a, '__iter__') for a in args])
ismasked = np.any([np.ma.isMaskedArray(a) for a in args])
isarray = [hasattr(a, '__iter__') for a in args]
ismasked = [np.ma.isMaskedArray(a) for a in args]
isduck = [hasattr(a, '__array_ufunc__')
Copy link
Member

@ocefpaf ocefpaf Sep 4, 2020

Choose a reason for hiding this comment

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

😄

Maybe isarrayinterface? No strong opinions, I actually like isduck ;-p

and not isinstance(a, np.ndarray) for a in args]

hasarray = np.any(isarray)
hasmasked = np.any(ismasked)
hasduck = np.any(isduck)

def fixup(ret):
if ismasked:
if hasduck:
return ret
if hasmasked:
ret = np.ma.masked_invalid(ret)
if not isarray and isinstance(ret, np.ndarray):
ret = ret[0]
if not hasarray and isinstance(ret, np.ndarray) and ret.size == 1:
try:
ret = ret[0]
except IndexError:
pass
return ret

if ismasked:
newargs = [masked_to_nan(a) for a in args]
else:
newargs = [np.asarray(a, dtype=float) for a in args]
newargs = []
for i, arg in enumerate(args):
if ismasked[i]:
newargs.append(masked_to_nan(arg))
elif isduck[i]:
newargs.append(arg)
else:
newargs.append(np.asarray(arg, dtype=float))

if p is not None:
kw['p'] = newargs.pop()
Expand Down