Python numexpr.evaluate() Examples
The following are 30
code examples of numexpr.evaluate().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numexpr
, or try the search function
.
Example #1
Source File: expression.py From zipline-chinese with Apache License 2.0 | 6 votes |
def _compute(self, arrays, dates, assets, mask): """ Compute our stored expression string with numexpr. """ out = full(mask.shape, self.missing_value, dtype=self.dtype) # This writes directly into our output buffer. numexpr.evaluate( self._expr, local_dict={ "x_%d" % idx: array for idx, array in enumerate(arrays) }, global_dict={'inf': inf}, out=out, ) return out
Example #2
Source File: ne.py From mars with Apache License 2.0 | 6 votes |
def execute(cls, ctx, op): chunk = op.outputs[0] inputs = as_same_device([ctx[c.key] for c in op.inputs], device=op.device) for c, i in zip(op.inputs, inputs): exec('V_' + c.key + ' = i') expr = _evaluate(chunk) if cls._lock is not None: cls._lock.acquire() try: res = ne.evaluate(expr) finally: if cls._lock is not None: cls._lock.release() res = _maybe_keepdims(chunk, res) if chunk.ndim == 0 and res.ndim == 1 and res.size == 0: res = res.dtype.type(0) ctx[chunk.key] = res
Example #3
Source File: metrics.py From PSSR with BSD 3-Clause "New" or "Revised" License | 6 votes |
def normalize_mi_ma(x, mi, ma, clip=False, eps=1e-20, dtype=np.float32): if dtype is not None: x = x.astype(dtype,copy=False) mi = dtype(mi) if np.isscalar(mi) else mi.astype(dtype,copy=False) ma = dtype(ma) if np.isscalar(ma) else ma.astype(dtype,copy=False) eps = dtype(eps) try: import numexpr x = numexpr.evaluate("(x - mi) / ( ma - mi + eps )") except ImportError: x = (x - mi) / ( ma - mi + eps ) if clip: x = np.clip(x,0,1) return x
Example #4
Source File: engines.py From recruit with Apache License 2.0 | 6 votes |
def evaluate(self): """Run the engine on the expression This method performs alignment which is necessary no matter what engine is being used, thus its implementation is in the base class. Returns ------- obj : object The result of the passed expression. """ if not self._is_aligned: self.result_type, self.aligned_axes = _align(self.expr.terms) # make sure no names in resolvers and locals/globals clash res = self._evaluate() return _reconstruct_object(self.result_type, res, self.aligned_axes, self.expr.terms.return_type)
Example #5
Source File: sc.py From ocelot with GNU General Public License v3.0 | 6 votes |
def sym_kernel(self, ijk2, hxyz): i2 = ijk2[0] j2 = ijk2[1] k2 = ijk2[2] hx = hxyz[0] hy = hxyz[1] hz = hxyz[2] x = hx*np.r_[0:i2+1] - hx/2 y = hy*np.r_[0:j2+1] - hy/2 z = hz*np.r_[0:k2+1] - hz/2 x, y, z = np.ix_(x, y, z) r = np.sqrt(x*x + y*y + z*z) if ne_flag: IG = ne.evaluate("(-x*x*0.5*arctan(y*z/(x*r)) + y*z*log(x+r) - y*y*0.5*arctan(z*x/(y*r)) + z*x*log(y+r) - z*z*0.5*arctan(x*y/(z*r)) + x*y*log(z+r))") else: IG = (-x*x*0.5*np.arctan(y*z/(x*r)) + y*z*np.log(x+r) -y*y*0.5*np.arctan(z*x/(y*r)) + z*x*np.log(y+r) -z*z*0.5*np.arctan(x*y/(z*r)) + x*y*np.log(z+r)) kern = (IG[1:i2+1, 1:j2+1, 1:k2+1] - IG[0:i2, 1:j2+1, 1:k2+1] -IG[1:i2+1, 0:j2, 1:k2+1] + IG[0:i2, 0:j2, 1:k2+1] -IG[1:i2+1, 1:j2+1, 0:k2] + IG[0:i2, 1:j2+1, 0:k2] +IG[1:i2+1, 0:j2, 0:k2] - IG[0:i2, 0:j2, 0:k2]) return kern
Example #6
Source File: rk_py.py From ocelot with GNU General Public License v3.0 | 6 votes |
def fields(x,y,z, kx, ky, kz, B0): k1 = -B0*kx/ky k2 = -B0*kz/ky kx_x = kx*x ky_y = ky*y kz_z = kz*z cosx = np.cos(kx_x) sinhy = np.sinh(ky_y) cosz = np.cos(kz_z) Bx = k1*np.sin(kx_x)*sinhy*cosz #// here kx is only real By = B0*cosx*np.cosh(ky_y)*cosz Bz = k2*cosx*sinhy*np.sin(kz_z) #Bx = ne.evaluate("k1*sin(kx*x)*sinhy*cosz") #By = ne.evaluate("B0*cosx*cosh(ky*y)*cosz") #Bz = ne.evaluate("k2*cosx*sinhy*sin(kz*z)") return Bx, By, Bz
Example #7
Source File: color_thread.py From LIFX-Control-Panel with MIT License | 6 votes |
def dominant_screen_color(initial_color, func_bounds=lambda: None): """ https://stackoverflow.com/questions/50899692/most-dominant-color-in-rgb-image-opencv-numpy-python """ monitor = get_monitor_bounds(func_bounds) if "full" in monitor: screenshot = getScreenAsImage() else: screenshot = getRectAsImage(str2list(monitor, int)) downscale_width, downscale_height = screenshot.width // 4, screenshot.height // 4 screenshot = screenshot.resize((downscale_width, downscale_height), Image.HAMMING) a = np.array(screenshot) a2D = a.reshape(-1, a.shape[-1]) col_range = (256, 256, 256) # generically : a2D.max(0)+1 eval_params = {'a0': a2D[:, 0], 'a1': a2D[:, 1], 'a2': a2D[:, 2], 's0': col_range[0], 's1': col_range[1]} a1D = ne.evaluate('a0*s0*s1+a1*s0+a2', eval_params) color = np.unravel_index(np.bincount(a1D).argmax(), col_range) color_hsbk = list(utils.RGBtoHSBK(color, temperature=initial_color[3])) # color_hsbk[2] = initial_color[2] # TODO Decide this return color_hsbk
Example #8
Source File: optics.py From ocelot with GNU General Public License v3.0 | 6 votes |
def numexpr_apply(self, X, R, T): x, px, y, py, tau, dp = np.copy((X[0], X[1], X[2], X[3], X[4], X[5])) R00, R01, R02, R03, R04, R05 = R[0, 0], R[0, 1], R[0, 2], R[0, 3], R[0, 4], R[0, 5] R10, R11, R12, R13, R14, R15 = R[1, 0], R[1, 1], R[1, 2], R[1, 3], R[1, 4], R[1, 5] R20, R21, R22, R23, R24, R25 = R[2, 0], R[2, 1], R[2, 2], R[2, 3], R[2, 4], R[2, 5] R30, R31, R32, R33, R34, R35 = R[3, 0], R[3, 1], R[3, 2], R[3, 3], R[3, 4], R[3, 5] R40, R41, R42, R43, R44, R45 = R[4, 0], R[4, 1], R[4, 2], R[4, 3], R[4, 4], R[4, 5] R50, R51, R52, R53, R54, R55 = R[5, 0], R[5, 1], R[5, 2], R[5, 3], R[5, 4], R[5, 5] T000, T001, T005, T011, T015, T055, T022, T023, T033 = T[0, 0, 0], T[0, 0, 1], T[0, 0, 5], T[0, 1, 1], T[0, 1, 5], T[0, 5, 5], T[0, 2, 2],T[0, 2, 3], T[0, 3, 3] T100, T101, T105, T111, T115, T155, T122, T123, T133 = T[1, 0, 0], T[1, 0, 1], T[1, 0, 5], T[1, 1, 1], T[1, 1, 5], T[1, 5, 5], T[1, 2, 2],T[1, 2, 3], T[1, 3, 3] T202, T203, T212, T213, T225, T235 = T[2, 0, 2], T[2, 0, 3], T[2, 1, 2], T[2, 1, 3], T[2, 2, 5], T[2, 3, 5] T302, T303, T312, T313, T325, T335 = T[3, 0, 2], T[3, 0, 3], T[3, 1, 2], T[3, 1, 3], T[3, 2, 5], T[3, 3, 5] T400, T401, T405, T411, T415, T455, T422, T423, T433 = T[4, 0, 0], T[4, 0, 1], T[4, 0, 5], T[4, 1, 1], T[4, 1, 5], T[4, 5, 5], T[4, 2, 2], T[4, 2, 3], T[4, 3, 3] X[0] = ne.evaluate('R00 * x + R01 * px + R02 * y + R03 * py + R04 * tau + R05 * dp + T000 * x*x + T001 * x*px + T005 * x*dp + T011 * px*px + T015 * px*dp + T055 * dp*dp + T022 * y*y + T023 * y*py + T033 * py*py') X[1] = ne.evaluate('R10 * x + R11 * px + R12 * y + R13 * py + R14 * tau + R15 * dp + T100 * x*x + T101 * x*px + T105 * x*dp + T111 * px*px + T115 * px*dp + T155 * dp*dp + T122 * y*y + T123 * y*py + T133 * py*py') X[2] = ne.evaluate('R20 * x + R21 * px + R22 * y + R23 * py + R24 * tau + R25 * dp + T202 * x*y + T203 * x*py + T212 * y*px + T213 * px*py + T225 * y*dp + T235 * py*dp') X[3] = ne.evaluate('R30 * x + R31 * px + R32 * y + R33 * py + R34 * tau + R35 * dp + T302 * x*y + T303 * x*py + T312 * y*px + T313 * px*py + T325 * y*dp + T335 * py*dp') X[4] = ne.evaluate('R40 * x + R41 * px + R42 * y + R43 * py + R44 * tau + R45 * dp + T400 * x*x + T401 * x*px + T405 * x*dp + T411 * px*px + T415 * px*dp + T455 * dp*dp + T422 * y*y + T423 * y*py + T433 * py*py') # + U5666*dp2*dp # third order
Example #9
Source File: expressions.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def evaluate(op, op_str, a, b, use_numexpr=True, **eval_kwargs): """ evaluate and return the expression of the op on a and b Parameters ---------- op : the actual operand op_str: the string version of the op a : left operand b : right operand use_numexpr : whether to try to use numexpr (default True) """ use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b) if use_numexpr: return _evaluate(op, op_str, a, b, **eval_kwargs) return _evaluate_standard(op, op_str, a, b)
Example #10
Source File: expressions.py From recruit with Apache License 2.0 | 6 votes |
def evaluate(op, op_str, a, b, use_numexpr=True, **eval_kwargs): """ evaluate and return the expression of the op on a and b Parameters ---------- op : the actual operand op_str: the string version of the op a : left operand b : right operand use_numexpr : whether to try to use numexpr (default True) """ use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b) if use_numexpr: return _evaluate(op, op_str, a, b, **eval_kwargs) return _evaluate_standard(op, op_str, a, b)
Example #11
Source File: engines.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def evaluate(self): """Run the engine on the expression This method performs alignment which is necessary no matter what engine is being used, thus its implementation is in the base class. Returns ------- obj : object The result of the passed expression. """ if not self._is_aligned: self.result_type, self.aligned_axes = _align(self.expr.terms) # make sure no names in resolvers and locals/globals clash res = self._evaluate() return _reconstruct_object(self.result_type, res, self.aligned_axes, self.expr.terms.return_type)
Example #12
Source File: engines.py From recruit with Apache License 2.0 | 6 votes |
def _evaluate(self): import numexpr as ne # convert the expression to a valid numexpr expression s = self.convert() try: env = self.expr.env scope = env.full_scope truediv = scope['truediv'] _check_ne_builtin_clash(self.expr) return ne.evaluate(s, local_dict=scope, truediv=truediv) except KeyError as e: # python 3 compat kludge try: msg = e.message except AttributeError: msg = compat.text_type(e) raise UndefinedVariableError(msg)
Example #13
Source File: engines.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def _evaluate(self): import numexpr as ne # convert the expression to a valid numexpr expression s = self.convert() try: env = self.expr.env scope = env.full_scope truediv = scope['truediv'] _check_ne_builtin_clash(self.expr) return ne.evaluate(s, local_dict=scope, truediv=truediv) except KeyError as e: # python 3 compat kludge try: msg = e.message except AttributeError: msg = compat.text_type(e) raise UndefinedVariableError(msg)
Example #14
Source File: em.py From typhon with MIT License | 6 votes |
def planck_f(f, T): """Planck law expressed in frequency. If more than 10⁵ resulting radiances, uses numexpr. :param f: Frequency. Quantity in [Hz] :param T: Temperature. Quantity in [K] """ # try: # f = f.astype(numpy.float64) # except AttributeError: # pass if (f.size * T.size) > 1e5: return numexpr.evaluate("(2 * h * f**3) / (c**2) * " "1 / (exp((h*f)/(k*T)) - 1)") * ( radiance_units["si"]) return ((2 * ureg.h * f**3) / (ureg.c ** 2) * 1 / (numpy.exp(((ureg.h * f) / (ureg.k * T)).to("1")) - 1)).to( ureg.W / (ureg.m**2 * ureg.sr * ureg.Hz))
Example #15
Source File: utils.py From CSBDeep with BSD 3-Clause "New" or "Revised" License | 6 votes |
def normalize_mi_ma(x, mi, ma, clip=False, eps=1e-20, dtype=np.float32): if dtype is not None: x = x.astype(dtype,copy=False) mi = dtype(mi) if np.isscalar(mi) else mi.astype(dtype,copy=False) ma = dtype(ma) if np.isscalar(ma) else ma.astype(dtype,copy=False) eps = dtype(eps) try: import numexpr x = numexpr.evaluate("(x - mi) / ( ma - mi + eps )") except ImportError: x = (x - mi) / ( ma - mi + eps ) if clip: x = np.clip(x,0,1) return x
Example #16
Source File: engines.py From vnpy_crypto with MIT License | 6 votes |
def evaluate(self): """Run the engine on the expression This method performs alignment which is necessary no matter what engine is being used, thus its implementation is in the base class. Returns ------- obj : object The result of the passed expression. """ if not self._is_aligned: self.result_type, self.aligned_axes = _align(self.expr.terms) # make sure no names in resolvers and locals/globals clash res = self._evaluate() return _reconstruct_object(self.result_type, res, self.aligned_axes, self.expr.terms.return_type)
Example #17
Source File: technical.py From catalyst with Apache License 2.0 | 6 votes |
def compute(self, today, assets, out, closes, lows, highs): highest_highs = nanmax(highs, axis=0) lowest_lows = nanmin(lows, axis=0) today_closes = closes[-1] evaluate( '((tc - ll) / (hh - ll)) * 100', local_dict={ 'tc': today_closes, 'll': lowest_lows, 'hh': highest_highs, }, global_dict={}, out=out, )
Example #18
Source File: engines.py From vnpy_crypto with MIT License | 6 votes |
def _evaluate(self): import numexpr as ne # convert the expression to a valid numexpr expression s = self.convert() try: env = self.expr.env scope = env.full_scope truediv = scope['truediv'] _check_ne_builtin_clash(self.expr) return ne.evaluate(s, local_dict=scope, truediv=truediv) except KeyError as e: # python 3 compat kludge try: msg = e.message except AttributeError: msg = compat.text_type(e) raise UndefinedVariableError(msg)
Example #19
Source File: technical.py From catalyst with Apache License 2.0 | 6 votes |
def compute(self, today, assets, out, lows, highs): wl = self.window_length high_date_index = nanargmax(highs, axis=0) low_date_index = nanargmin(lows, axis=0) evaluate( '(100 * high_date_index) / (wl - 1)', local_dict={ 'high_date_index': high_date_index, 'wl': wl, }, out=out.up, ) evaluate( '(100 * low_date_index) / (wl - 1)', local_dict={ 'low_date_index': low_date_index, 'wl': wl, }, out=out.down, )
Example #20
Source File: expressions.py From vnpy_crypto with MIT License | 6 votes |
def evaluate(op, op_str, a, b, use_numexpr=True, **eval_kwargs): """ evaluate and return the expression of the op on a and b Parameters ---------- op : the actual operand op_str: the string version of the op a : left operand b : right operand use_numexpr : whether to try to use numexpr (default True) """ use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b) if use_numexpr: return _evaluate(op, op_str, a, b, **eval_kwargs) return _evaluate_standard(op, op_str, a, b)
Example #21
Source File: technical.py From catalyst with Apache License 2.0 | 6 votes |
def compute(self, today, assets, out, closes, lows, highs): highest_highs = nanmax(highs, axis=0) lowest_lows = nanmin(lows, axis=0) today_closes = closes[-1] evaluate( '((tc - ll) / (hh - ll)) * 100', local_dict={ 'tc': today_closes, 'll': lowest_lows, 'hh': highest_highs, }, global_dict={}, out=out, )
Example #22
Source File: technical.py From catalyst with Apache License 2.0 | 6 votes |
def compute(self, today, assets, out, lows, highs): wl = self.window_length high_date_index = nanargmax(highs, axis=0) low_date_index = nanargmin(lows, axis=0) evaluate( '(100 * high_date_index) / (wl - 1)', local_dict={ 'high_date_index': high_date_index, 'wl': wl, }, out=out.up, ) evaluate( '(100 * low_date_index) / (wl - 1)', local_dict={ 'low_date_index': low_date_index, 'wl': wl, }, out=out.down, )
Example #23
Source File: engines.py From Computable with MIT License | 6 votes |
def evaluate(self): """Run the engine on the expression This method performs alignment which is necessary no matter what engine is being used, thus its implementation is in the base class. Returns ------- obj : object The result of the passed expression. """ if not self._is_aligned: self.result_type, self.aligned_axes = _align(self.expr.terms) # make sure no names in resolvers and locals/globals clash self.pre_evaluate() res = self._evaluate() return _reconstruct_object(self.result_type, res, self.aligned_axes, self.expr.terms.return_type)
Example #24
Source File: engines.py From Computable with MIT License | 6 votes |
def _evaluate(self): import numexpr as ne # add the resolvers to locals self.expr.add_resolvers_to_locals() # convert the expression to a valid numexpr expression s = self.convert() try: return ne.evaluate(s, local_dict=self.expr.env.locals, global_dict=self.expr.env.globals, truediv=self.expr.truediv) except KeyError as e: # python 3 compat kludge try: msg = e.message except AttributeError: msg = compat.text_type(e) raise UndefinedVariableError(msg)
Example #25
Source File: expression.py From catalyst with Apache License 2.0 | 6 votes |
def _compute(self, arrays, dates, assets, mask): """ Compute our stored expression string with numexpr. """ out = full(mask.shape, self.missing_value, dtype=self.dtype) # This writes directly into our output buffer. numexpr.evaluate( self._expr, local_dict={ "x_%d" % idx: array for idx, array in enumerate(arrays) }, global_dict={'inf': inf}, out=out, ) return out
Example #26
Source File: utils.py From cape-webservices with Apache License 2.0 | 6 votes |
def try_numerical_answer(question: str) -> Optional[Tuple[str, str]]: if question.endswith('?') or question.endswith('.') or question.endswith('!'): question = question[:-1] words = NON_WORD_CHARS.split(question) starting_idx = None for idx, word in enumerate(words): if NUMERICAL_EXPRESSION_STARTER.match(word): starting_idx = idx break if starting_idx is None: return words = words[starting_idx:] length = len(words) ending_idx = None for idx, word in enumerate(reversed(words)): if NUMERICAL_EXPRESSION_ENDER.match("".join(reversed(word))): ending_idx = length - idx break expression = " ".join(words[:ending_idx]) try: result = numexpr.evaluate(expression, local_dict={'pi': math.pi, 'tau': math.tau, 'e': math.e}, global_dict={}) except Exception: return return expression, str(result)
Example #27
Source File: expressions.py From Computable with MIT License | 6 votes |
def evaluate(op, op_str, a, b, raise_on_error=False, use_numexpr=True, **eval_kwargs): """ evaluate and return the expression of the op on a and b Parameters ---------- op : the actual operand op_str: the string version of the op a : left operand b : right operand raise_on_error : pass the error to the higher level if indicated (default is False), otherwise evaluate the op with and return the results use_numexpr : whether to try to use numexpr (default True) """ if use_numexpr: return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error, **eval_kwargs) return _evaluate_standard(op, op_str, a, b, raise_on_error=raise_on_error)
Example #28
Source File: expressions.py From Computable with MIT License | 6 votes |
def where(cond, a, b, raise_on_error=False, use_numexpr=True): """ evaluate the where condition cond on a and b Parameters ---------- cond : a boolean array a : return if cond is True b : return if cond is False raise_on_error : pass the error to the higher level if indicated (default is False), otherwise evaluate the op with and return the results use_numexpr : whether to try to use numexpr (default True) """ if use_numexpr: return _where(cond, a, b, raise_on_error=raise_on_error) return _where_standard(cond, a, b, raise_on_error=raise_on_error)
Example #29
Source File: volshow.py From spimagine with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _rescale(x, upper, lower=0.): if upper<lower: raise ValueError("upper<lower! (%s < %s)"%(upper, lower)) ma, mi = np.amax(x), np.amin(x) if ma == mi: x = lower * np.ones(x.shape) else: try: import numexpr lower_32, upper_32 = np.float32(lower), np.float32(upper) mi_32, ma_32 = np.float32(mi), np.float32(ma) x = x.astype(np.float32, copy = False) x = numexpr.evaluate("lower_32+(upper_32 -lower_32)* (x - mi_32) / (ma_32 - mi_32)") except ImportError: logger.debug("could not find numexpr") x = lower+(upper-lower) * (x.astype(np.float32) - mi) / (ma - mi) return x
Example #30
Source File: technical.py From catalyst with Apache License 2.0 | 5 votes |
def compute(self, today, assets, out, close): today_close = close[-1] prev_close = close[0] evaluate('((tc - pc) / pc) * 100', local_dict={ 'tc': today_close, 'pc': prev_close }, global_dict={}, out=out, )