MCPcopy Index your code
hub / github.com/dask/dask / compute_meta

Function compute_meta

dask/array/_array_expr/_utils.py:13–74  ·  view source on GitHub ↗
(func, _dtype, *args, **kwargs)

Source from the content-addressed store, hash-verified

11
12
13def compute_meta(func, _dtype, *args, **kwargs):
14 with np.errstate(all="ignore"), warnings.catch_warnings():
15 warnings.simplefilter("ignore", category=RuntimeWarning)
16
17 args_meta = [
18 (
19 x._meta
20 if isinstance(x, ArrayExpr)
21 else meta_from_array(x) if is_arraylike(x) else x
22 )
23 for x in args
24 ]
25 kwargs_meta = {
26 k: (
27 v._meta
28 if isinstance(v, ArrayExpr)
29 else meta_from_array(v) if is_arraylike(v) else v
30 )
31 for k, v in kwargs.items()
32 }
33
34 # todo: look for alternative to this, causes issues when using map_blocks()
35 # with np.vectorize, such as dask.array.routines._isnonzero_vec().
36 if isinstance(func, np.vectorize):
37 meta = func(*args_meta)
38 else:
39 try:
40 # some reduction functions need to know they are computing meta
41 if has_keyword(func, "computing_meta"):
42 kwargs_meta["computing_meta"] = True
43 meta = func(*args_meta, **kwargs_meta)
44 except TypeError as e:
45 if any(
46 s in str(e)
47 for s in [
48 "unexpected keyword argument",
49 "is an invalid keyword for",
50 "Did not understand the following kwargs",
51 ]
52 ):
53 raise
54 else:
55 return None
56 except ValueError as e:
57 # min/max functions have no identity, just use the same input type when there's only one
58 if len(
59 args_meta
60 ) == 1 and "zero-size array to reduction operation" in str(e):
61 meta = args_meta[0]
62 else:
63 return None
64 except Exception:
65 return None
66
67 if _dtype and getattr(meta, "dtype", None) != _dtype:
68 with contextlib.suppress(AttributeError):
69 meta = meta.astype(_dtype)
70

Callers 4

reductionFunction · 0.90
map_blocksFunction · 0.90
_metaMethod · 0.90
_metaMethod · 0.90

Calls 7

meta_from_arrayFunction · 0.90
is_arraylikeFunction · 0.90
has_keywordFunction · 0.90
anyFunction · 0.85
funcFunction · 0.50
itemsMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…