MCPcopy Create free account
hub / github.com/dask/dask / _choice_validate_params

Function _choice_validate_params

dask/array/random.py:832–899  ·  view source on GitHub ↗
(state, a, size, replace, p, axis, chunks)

Source from the content-addressed store, hash-verified

830
831
832def _choice_validate_params(state, a, size, replace, p, axis, chunks):
833 dependencies = []
834 # Normalize and validate `a`
835 if isinstance(a, Integral):
836 if isinstance(state, Generator):
837 if state._backend_name == "cupy":
838 raise NotImplementedError(
839 "`choice` not supported for cupy-backed `Generator`."
840 )
841 meta = state._backend.random.default_rng().choice(1, size=(), p=None)
842 elif isinstance(state, RandomState):
843 # On windows the output dtype differs if p is provided or
844 # # absent, see https://github.com/numpy/numpy/issues/9867
845 dummy_p = state._backend.array([1]) if p is not None else p
846 meta = state._backend.random.RandomState().choice(1, size=(), p=dummy_p)
847 else:
848 raise ValueError("Unknown generator class")
849 len_a = a
850 if a < 0:
851 raise ValueError("a must be greater than 0")
852 else:
853 a = asarray(a)
854 a = a.rechunk(a.shape)
855 meta = a._meta
856 if a.ndim != 1:
857 raise ValueError("a must be one dimensional")
858 len_a = len(a)
859 dependencies.append(a)
860 a = TaskRef(a.__dask_keys__()[0])
861
862 # Normalize and validate `p`
863 if p is not None:
864 if not isinstance(p, Array):
865 # If p is not a dask array, first check the sum is close
866 # to 1 before converting.
867 p = asarray_safe(p, like=p)
868 if not np.isclose(p.sum(), 1, rtol=1e-7, atol=0):
869 raise ValueError("probabilities do not sum to 1")
870 p = asarray(p)
871 else:
872 p = p.rechunk(p.shape)
873
874 if p.ndim != 1:
875 raise ValueError("p must be one dimensional")
876 if len(p) != len_a:
877 raise ValueError("a and p must have the same size")
878
879 dependencies.append(p)
880 p = TaskRef(p.__dask_keys__()[0])
881
882 if size is None:
883 size = ()
884 elif not isinstance(size, (tuple, list)):
885 size = (size,)
886
887 if axis != 0:
888 raise ValueError("axis must be 0 since a is one dimensional")
889

Callers 2

choiceMethod · 0.70
choiceMethod · 0.70

Calls 9

asarrayFunction · 0.90
TaskRefClass · 0.90
asarray_safeFunction · 0.90
normalize_chunksFunction · 0.90
choiceMethod · 0.45
RandomStateMethod · 0.45
rechunkMethod · 0.45
__dask_keys__Method · 0.45
sumMethod · 0.45

Tested by

no test coverage detected