MCPcopy
hub / github.com/dask/dask / _wrap_func

Function _wrap_func

dask/array/random.py:898–1038  ·  view source on GitHub ↗

Wrap numpy random function to produce dask.array random function extra_chunks should be a chunks tuple to append to the end of chunks

(
    rng, funcname, *args, size=None, chunks="auto", extra_chunks=(), **kwargs
)

Source from the content-addressed store, hash-verified

896
897
898def _wrap_func(
899 rng, funcname, *args, size=None, chunks="auto", extra_chunks=(), **kwargs
900):
901 """Wrap numpy random function to produce dask.array random function
902 extra_chunks should be a chunks tuple to append to the end of chunks
903 """
904 if size is not None and not isinstance(size, (tuple, list)):
905 size = (size,)
906
907 shapes = list(
908 {
909 ar.shape
910 for ar in chain(args, kwargs.values())
911 if isinstance(ar, (Array, np.ndarray))
912 }
913 )
914 if size is not None:
915 shapes.append(size)
916 # broadcast to the final size(shape)
917 size = broadcast_shapes(*shapes)
918 chunks = normalize_chunks(
919 chunks,
920 size, # ideally would use dtype here
921 dtype=kwargs.get("dtype", np.float64),
922 )
923 slices = slices_from_chunks(chunks)
924
925 def _broadcast_any(ar, shape, chunks):
926 if isinstance(ar, Array):
927 return broadcast_to(ar, shape).rechunk(chunks)
928 elif isinstance(ar, np.ndarray):
929 return np.ascontiguousarray(np.broadcast_to(ar, shape))
930 else:
931 raise TypeError("Unknown object type for broadcast")
932
933 # Broadcast all arguments, get tiny versions as well
934 # Start adding the relevant bits to the graph
935 dsk = {}
936 lookup = {}
937 small_args = []
938 dependencies = []
939 for i, ar in enumerate(args):
940 if isinstance(ar, (np.ndarray, Array)):
941 res = _broadcast_any(ar, size, chunks)
942 if isinstance(res, Array):
943 dependencies.append(res)
944 lookup[i] = res.name
945 elif isinstance(res, np.ndarray):
946 name = f"array-{tokenize(res)}"
947 lookup[i] = name
948 dsk[name] = res
949 small_args.append(ar[tuple(0 for _ in ar.shape)])
950 else:
951 small_args.append(ar)
952
953 small_kwargs = {}
954 for key, ar in kwargs.items():
955 if isinstance(ar, (np.ndarray, Array)):

Callers 15

betaMethod · 0.70
binomialMethod · 0.70
chisquareMethod · 0.70
exponentialMethod · 0.70
fMethod · 0.70
gammaMethod · 0.70
geometricMethod · 0.70
gumbelMethod · 0.70
hypergeometricMethod · 0.70
integersMethod · 0.70
laplaceMethod · 0.70
logisticMethod · 0.70

Calls 15

broadcast_shapesFunction · 0.90
normalize_chunksFunction · 0.90
slices_from_chunksFunction · 0.90
random_state_dataFunction · 0.90
TaskRefClass · 0.90
TaskClass · 0.90
parse_inputFunction · 0.90
ArrayClass · 0.90
_broadcast_anyFunction · 0.85
from_collectionsMethod · 0.80
_spawn_bitgensFunction · 0.70
tokenizeFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…