|
45 | 45 | _check_allowed_dtypes,
|
46 | 46 | get_chunks_idx,
|
47 | 47 | get_intersecting_chunks,
|
48 |
| - is_inside_new_expr, |
49 | 48 | local_ufunc_map,
|
50 | 49 | process_key,
|
51 | 50 | ufunc_map,
|
52 | 51 | ufunc_map_1param,
|
53 | 52 | )
|
54 | 53 |
|
55 |
| -from .shape_utils import constructors, infer_shape, lin_alg_funcs, reducers |
| 54 | +from .shape_utils import constructors, elementwise_funcs, infer_shape, lin_alg_attrs, lin_alg_funcs, reducers |
56 | 55 |
|
57 | 56 | if not blosc2.IS_WASM:
|
58 | 57 | import numexpr
|
@@ -158,119 +157,9 @@ def ne_evaluate(expression, local_dict=None, **kwargs):
|
158 | 157 | "S": np.str_,
|
159 | 158 | "V": np.bytes_,
|
160 | 159 | }
|
161 |
| - |
162 |
| -blosc2_funcs = [ |
163 |
| - "abs", |
164 |
| - "acos", |
165 |
| - "acosh", |
166 |
| - "add", |
167 |
| - "all", |
168 |
| - "any", |
169 |
| - "arange", |
170 |
| - "arccos", |
171 |
| - "arccosh", |
172 |
| - "arcsin", |
173 |
| - "arcsinh", |
174 |
| - "arctan", |
175 |
| - "arctan2", |
176 |
| - "arctanh", |
177 |
| - "asin", |
178 |
| - "asinh", |
179 |
| - "atan", |
180 |
| - "atan2", |
181 |
| - "atanh", |
182 |
| - "bitwise_and", |
183 |
| - "bitwise_invert", |
184 |
| - "bitwise_left_shift", |
185 |
| - "bitwise_or", |
186 |
| - "bitwise_right_shift", |
187 |
| - "bitwise_xor", |
188 |
| - "broadcast_to", |
189 |
| - "ceil", |
190 |
| - "clip", |
191 |
| - "concat", |
192 |
| - "concatenate", |
193 |
| - "copy", |
194 |
| - "copysign", |
195 |
| - "count_nonzero", |
196 |
| - "divide", |
197 |
| - "empty", |
198 |
| - "empty_like", |
199 |
| - "equal", |
200 |
| - "expand_dims", |
201 |
| - "expm1", |
202 |
| - "eye", |
203 |
| - "floor", |
204 |
| - "floor_divide", |
205 |
| - "frombuffer", |
206 |
| - "fromiter", |
207 |
| - "full", |
208 |
| - "full_like", |
209 |
| - "greater", |
210 |
| - "greater_equal", |
211 |
| - "hypot", |
212 |
| - "isfinite", |
213 |
| - "isinf", |
214 |
| - "isnan", |
215 |
| - "less_equal", |
216 |
| - "less_than", |
217 |
| - "linspace", |
218 |
| - "log", |
219 |
| - "log1p", |
220 |
| - "log2", |
221 |
| - "log10", |
222 |
| - "logaddexp", |
223 |
| - "logical_and", |
224 |
| - "logical_not", |
225 |
| - "logical_or", |
226 |
| - "logical_xor", |
227 |
| - "matmul", |
228 |
| - "matrix_transpose", |
229 |
| - "max", |
230 |
| - "maximum", |
231 |
| - "mean", |
232 |
| - "meshgrid", |
233 |
| - "min", |
234 |
| - "minimum", |
235 |
| - "multiply", |
236 |
| - "nans", |
237 |
| - "ndarray_from_cframe", |
238 |
| - "negative", |
239 |
| - "nextafter", |
240 |
| - "not_equal", |
241 |
| - "ones", |
242 |
| - "ones_like", |
243 |
| - "permute_dims", |
244 |
| - "positive", |
245 |
| - "pow", |
246 |
| - "prod", |
247 |
| - "real", |
248 |
| - "reciprocal", |
249 |
| - "remainder", |
250 |
| - "reshape", |
251 |
| - "round", |
252 |
| - "sign", |
253 |
| - "signbit", |
254 |
| - "sort", |
255 |
| - "square", |
256 |
| - "squeeze", |
257 |
| - "stack", |
258 |
| - "sum", |
259 |
| - "subtract", |
260 |
| - "take", |
261 |
| - "take_along_axis", |
262 |
| - "tan", |
263 |
| - "tanh", |
264 |
| - "tensordot", |
265 |
| - "transpose", |
266 |
| - "trunc", |
267 |
| - "var", |
268 |
| - "vecdot", |
269 |
| - "where", |
270 |
| - "zeros", |
271 |
| - "zeros_like", |
272 |
| -] |
273 |
| - |
| 160 | +blosc2_funcs = constructors + lin_alg_funcs + elementwise_funcs + reducers |
| 161 | +# functions that have to be evaluated before chunkwise lazyexpr machinery |
| 162 | +eager_funcs = lin_alg_funcs + reducers + ["slice"] + lin_alg_attrs |
274 | 163 | # Gather all callable functions in numpy
|
275 | 164 | numpy_funcs = {
|
276 | 165 | name
|
@@ -751,12 +640,7 @@ def validate_expr(expr: str) -> None:
|
751 | 640 |
|
752 | 641 | def extract_and_replace_slices(expr, operands):
|
753 | 642 | """
|
754 |
| - Replaces all var.slice(...).slice(...) chains in expr with oN temporary variables. |
755 |
| - Infers shapes using infer_shape and creates placeholder arrays in new_ops. |
756 |
| -
|
757 |
| - Returns: |
758 |
| - new_expr: expression string with oN replacements |
759 |
| - new_ops: dictionary mapping variable names (original and oN) to arrays |
| 643 | + Return new expression and operands with op.slice(...) replaced by temporary operands. |
760 | 644 | """
|
761 | 645 | # Copy shapes and operands
|
762 | 646 | shapes = {k: () if not hasattr(v, "shape") else v.shape for k, v in operands.items()}
|
@@ -1976,11 +1860,6 @@ def reduce_slices( # noqa: C901
|
1976 | 1860 | if out is not None and reduced_shape != out.shape:
|
1977 | 1861 | raise ValueError("Provided output shape does not match the reduced shape.")
|
1978 | 1862 |
|
1979 |
| - if is_inside_new_expr(): |
1980 |
| - # We already have the dtype and reduced_shape, so return immediately |
1981 |
| - # Use a blosc2 container, as it consumes less memory in general |
1982 |
| - return blosc2.zeros(reduced_shape, dtype=dtype) |
1983 |
| - |
1984 | 1863 | # Choose the array with the largest shape as the reference for chunks
|
1985 | 1864 | # Note: we could have expr = blosc2.lazyexpr('numpy_array + 1') (i.e. no choice for chunks)
|
1986 | 1865 | blosc2_arrs = tuple(o for o in operands.values() if hasattr(o, "chunks"))
|
@@ -3023,7 +2902,7 @@ def _compute_expr(self, item, kwargs): # noqa : C901
|
3023 | 2902 | }
|
3024 | 2903 | )
|
3025 | 2904 |
|
3026 |
| - if any(method in self.expression for method in reducers + lin_alg_funcs): |
| 2905 | + if any(method in self.expression for method in eager_funcs): |
3027 | 2906 | # We have reductions in the expression (probably coming from a string lazyexpr)
|
3028 | 2907 | # Also includes slice
|
3029 | 2908 | _globals = get_expr_globals(self.expression)
|
@@ -3073,7 +2952,7 @@ def _compute_expr(self, item, kwargs): # noqa : C901
|
3073 | 2952 | # Replace the constructor call by the new operand
|
3074 | 2953 | newexpr = newexpr.replace(constexpr, newop)
|
3075 | 2954 |
|
3076 |
| - _globals = {func: getattr(blosc2, func) for func in functions if func in newexpr} |
| 2955 | + _globals = get_expr_globals(newexpr) |
3077 | 2956 | lazy_expr = eval(newexpr, _globals, newops)
|
3078 | 2957 | if isinstance(lazy_expr, blosc2.NDArray):
|
3079 | 2958 | # Almost done (probably the expression is made of only constructors)
|
|
0 commit comments