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

Function _choice_validate_params

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

Source from the content-addressed store, hash-verified

805
806
807def _choice_validate_params(state, a, size, replace, p, axis, chunks):
808 dependencies = []
809 # Normalize and validate `a`
810 if isinstance(a, Integral):
811 if isinstance(state, Generator):
812 if state._backend_name == "cupy":
813 raise NotImplementedError(
814 "`choice` not supported for cupy-backed `Generator`."
815 )
816 meta = state._backend.random.default_rng().choice(1, size=(), p=None)
817 elif isinstance(state, RandomState):
818 # On windows the output dtype differs if p is provided or
819 # # absent, see https://github.com/numpy/numpy/issues/9867
820 dummy_p = state._backend.array([1]) if p is not None else p
821 meta = state._backend.random.RandomState().choice(1, size=(), p=dummy_p)
822 else:
823 raise ValueError("Unknown generator class")
824 len_a = a
825 if a < 0:
826 raise ValueError("a must be greater than 0")
827 else:
828 a = asarray(a)
829 a = a.rechunk(a.shape)
830 meta = a._meta
831 if a.ndim != 1:
832 raise ValueError("a must be one dimensional")
833 len_a = len(a)
834 dependencies.append(a)
835 a = a.__dask_keys__()[0]
836
837 # Normalize and validate `p`
838 if p is not None:
839 if not isinstance(p, Array):
840 # If p is not a dask array, first check the sum is close
841 # to 1 before converting.
842 p = asarray_safe(p, like=p)
843 if not np.isclose(p.sum(), 1, rtol=1e-7, atol=0):
844 raise ValueError("probabilities do not sum to 1")
845 p = asarray(p)
846 else:
847 p = p.rechunk(p.shape)
848
849 if p.ndim != 1:
850 raise ValueError("p must be one dimensional")
851 if len(p) != len_a:
852 raise ValueError("a and p must have the same size")
853
854 dependencies.append(p)
855 p = p.__dask_keys__()[0]
856
857 if size is None:
858 size = ()
859
860 if axis != 0:
861 raise ValueError("axis must be 0 since a is one dimensional")
862
863 chunks = normalize_chunks(chunks, size, dtype=np.float64)
864 if not replace and len(chunks[0]) > 1:

Callers 2

choiceMethod · 0.70
choiceMethod · 0.70

Calls 8

asarrayFunction · 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