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
)
| 896 | |
| 897 | |
| 898 | def _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)): |
no test coverage detected
searching dependent graphs…