MCPcopy
hub / github.com/dask/dask / meta_from_array

Function meta_from_array

dask/array/utils.py:27–121  ·  view source on GitHub ↗

Normalize an array to appropriate meta object Parameters ---------- x: array-like, callable Either an object that looks sufficiently like a Numpy array, or a callable that accepts shape and dtype keywords ndim: int Number of dimensions of the array dtype:

(x, ndim=None, dtype=None)

Source from the content-addressed store, hash-verified

25
26
27def meta_from_array(x, ndim=None, dtype=None):
28 """Normalize an array to appropriate meta object
29
30 Parameters
31 ----------
32 x: array-like, callable
33 Either an object that looks sufficiently like a Numpy array,
34 or a callable that accepts shape and dtype keywords
35 ndim: int
36 Number of dimensions of the array
37 dtype: Numpy dtype
38 A valid input for ``np.dtype``
39
40 Returns
41 -------
42 array-like with zero elements of the correct dtype
43 """
44 # If using x._meta, x must be a Dask Array, some libraries (e.g. zarr)
45 # implement a _meta attribute that are incompatible with Dask Array._meta
46 if hasattr(x, "_meta") and is_dask_collection(x) and is_arraylike(x):
47 x = x._meta
48
49 if dtype is None and x is None:
50 raise ValueError("You must specify the meta or dtype of the array")
51
52 if np.isscalar(x):
53 x = np.array(x)
54
55 if x is None:
56 x = np.ndarray
57 elif dtype is None and hasattr(x, "dtype"):
58 dtype = x.dtype
59
60 if isinstance(x, type):
61 x = x(shape=(0,) * (ndim or 0), dtype=dtype)
62
63 if isinstance(x, (list, tuple)):
64 ndims = [
65 (
66 0
67 if isinstance(a, numbers.Number)
68 else a.ndim if hasattr(a, "ndim") else len(a)
69 )
70 for a in x
71 ]
72 a = [a if nd == 0 else meta_from_array(a, nd) for a, nd in zip(x, ndims)]
73 return a if isinstance(x, list) else tuple(x)
74
75 if (
76 not hasattr(x, "shape")
77 or not hasattr(x, "dtype")
78 or not isinstance(x.shape, tuple)
79 ):
80 return x
81
82 if ndim is None:
83 ndim = x.ndim
84

Callers 15

tsqrFunction · 0.90
sfqrFunction · 0.90
luFunction · 0.90
solve_triangularFunction · 0.90
_choleskyFunction · 0.90
lstsqFunction · 0.90
apply_gufuncFunction · 0.90
apply_infer_dtypeFunction · 0.90
__new__Method · 0.90
__setitem__Method · 0.90
__getitem__Method · 0.90
concatenateFunction · 0.90

Calls 7

is_dask_collectionFunction · 0.90
is_arraylikeFunction · 0.90
anyFunction · 0.85
reshapeMethod · 0.80
sumMethod · 0.45
emptyMethod · 0.45
astypeMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…