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

Implement functions from np.random #46

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions unumpy/dask_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@
get_state,
set_state,
)
from unumpy import ufunc, ufunc_list, ndarray
from unumpy import ufunc, ufunc_list, ndarray, random
import unumpy
import functools
import sys
import collections
import itertools
import random
from random import randrange

from typing import Dict

_ufunc_mapping: Dict[ufunc, np.ufunc] = {}
__ua_domain__ = "numpy"

EXCEPTIONS = [
unumpy.random.rand,
unumpy.random.randn,
unumpy.random.ranf,
unumpy.random.sample,
unumpy.random.bytes,
unumpy.random.shuffle,
unumpy.random.dirichlet,
unumpy.random.multivariate_normal,
unumpy.random.get_state,
]


def wrap_current_state(func):
state = get_state()
Expand Down Expand Up @@ -60,7 +72,7 @@ def wrapped(shape, *args, **kwargs):
chunks[-1].append(s)
l -= s

name = func.__name__ + "-" + hex(random.randrange(2 ** 64))
name = func.__name__ + "-" + hex(randrange(2 ** 64))
dsk = {}
with skip_backend(sys.modules[__name__]):
for chunk_id in itertools.product(*map(lambda x: range(len(x)), chunks)):
Expand All @@ -87,11 +99,15 @@ def __ua_function__(method, args, kwargs):
if method in _implementations:
return _implementations[method](*args, **kwargs)

if not hasattr(da, method.__name__):
if method.__module__ == "unumpy._multimethods" and hasattr(da, method.__name__):
return getattr(da, method.__name__)(*args, **kwargs)
elif (
method.__module__ == "unumpy.random._multimethods" and method not in EXCEPTIONS
):
return getattr(da.random, method.__name__)(*args, **kwargs)
else:
return NotImplemented

return getattr(da, method.__name__)(*args, **kwargs)


@wrap_single_convertor
def __ua_convert__(value, dispatch_type, coerce):
Expand Down
10 changes: 6 additions & 4 deletions unumpy/numpy_backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from uarray import Dispatchable, wrap_single_convertor
from unumpy import ufunc, ufunc_list, ndarray, dtype
from unumpy import ufunc, ufunc_list, ndarray, dtype, random
import unumpy
import functools

Expand All @@ -24,11 +24,13 @@ def __ua_function__(method, args, kwargs):
if method in _implementations:
return _implementations[method](*args, **kwargs)

if not hasattr(np, method.__name__):
if method.__module__ == "unumpy._multimethods":
return getattr(np, method.__name__)(*args, **kwargs)
elif method.__module__ == "unumpy.random._multimethods":
return getattr(np.random, method.__name__)(*args, **kwargs)
else:
return NotImplemented

return getattr(np, method.__name__)(*args, **kwargs)


@wrap_single_convertor
def __ua_convert__(value, dispatch_type, coerce):
Expand Down
1 change: 1 addition & 0 deletions unumpy/random/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._multimethods import *
prasunanand marked this conversation as resolved.
Show resolved Hide resolved