(func, _dtype, *args, **kwargs)
| 122 | |
| 123 | |
| 124 | def compute_meta(func, _dtype, *args, **kwargs): |
| 125 | with np.errstate(all="ignore"), warnings.catch_warnings(): |
| 126 | warnings.simplefilter("ignore", category=RuntimeWarning) |
| 127 | |
| 128 | args_meta = [meta_from_array(x) if is_arraylike(x) else x for x in args] |
| 129 | kwargs_meta = { |
| 130 | k: meta_from_array(v) if is_arraylike(v) else v for k, v in kwargs.items() |
| 131 | } |
| 132 | |
| 133 | # todo: look for alternative to this, causes issues when using map_blocks() |
| 134 | # with np.vectorize, such as dask.array.routines._isnonzero_vec(). |
| 135 | if isinstance(func, np.vectorize): |
| 136 | meta = func(*args_meta) |
| 137 | else: |
| 138 | try: |
| 139 | # some reduction functions need to know they are computing meta |
| 140 | if has_keyword(func, "computing_meta"): |
| 141 | kwargs_meta["computing_meta"] = True |
| 142 | meta = func(*args_meta, **kwargs_meta) |
| 143 | except TypeError as e: |
| 144 | if any( |
| 145 | s in str(e) |
| 146 | for s in [ |
| 147 | "unexpected keyword argument", |
| 148 | "is an invalid keyword for", |
| 149 | "Did not understand the following kwargs", |
| 150 | ] |
| 151 | ): |
| 152 | raise |
| 153 | else: |
| 154 | return None |
| 155 | except ValueError as e: |
| 156 | # min/max functions have no identity, just use the same input type when there's only one |
| 157 | if len( |
| 158 | args_meta |
| 159 | ) == 1 and "zero-size array to reduction operation" in str(e): |
| 160 | meta = args_meta[0] |
| 161 | else: |
| 162 | return None |
| 163 | except Exception: |
| 164 | return None |
| 165 | |
| 166 | if _dtype and getattr(meta, "dtype", None) != _dtype: |
| 167 | with contextlib.suppress(AttributeError): |
| 168 | meta = meta.astype(_dtype) |
| 169 | |
| 170 | if np.isscalar(meta): |
| 171 | meta = np.array(meta) |
| 172 | |
| 173 | return meta |
| 174 | |
| 175 | |
| 176 | def allclose(a, b, equal_nan=False, **kwargs): |
no test coverage detected
searching dependent graphs…