本文整理汇总了Python中expr.expr_eval函数的典型用法代码示例。如果您正苦于以下问题:Python expr_eval函数的具体用法?Python expr_eval怎么用?Python expr_eval使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了expr_eval函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: eval_assertion
def eval_assertion(self, context):
v1 = expr_eval(self.expr1, context)
v2 = expr_eval(self.expr2, context)
if not self.compare(v1, v2):
op = self.inv_op
return "%s %s %s (%s %s %s)" % (self.expr1, op, self.expr2,
v1, op, v2)
示例2: compute
def compute(self, context, set1filter, set2filter, orderby1, orderby2):
set1filterexpr = self._getfilter(context, set1filter)
set1filtervalue = expr_eval(set1filterexpr, context)
set2filterexpr = self._getfilter(context, set2filter)
set2filtervalue = expr_eval(set2filterexpr, context)
set1len = set1filtervalue.sum()
set2len = set2filtervalue.sum()
numtomatch = min(set1len, set2len)
print("matching with %d/%d individuals" % (set1len, set2len))
result = np.full(context_length(context), -1, dtype=int)
if not numtomatch:
return result
sorted_set1_indices = orderby1[set1filtervalue].argsort()[-numtomatch:]
sorted_set2_indices = orderby2[set2filtervalue].argsort()[-numtomatch:]
set1ids = context['id'][set1filtervalue]
set2ids = context['id'][set2filtervalue]
id_to_rownum = context.id_to_rownum
id1 = set1ids[sorted_set1_indices]
id2 = set2ids[sorted_set2_indices]
# cannot use sorted_setX_indices because those are "local" indices
result[id_to_rownum[id1]] = id2
result[id_to_rownum[id2]] = id1
return result
示例3: evaluate
def evaluate(self, context):
if config.debug:
print()
print("random sequence position before:", np.random.get_state()[2])
num = context_length(context)
choices = self.choices
if num:
bins = self.bins
if bins is None:
# all values have the same probability
choices_idx = np.random.randint(len(choices), size=num)
else:
if any(isinstance(b, Expr) for b in bins):
weights = [expr_eval(expr, context) for expr in bins]
bins = self._weights_to_bins(weights)
u = np.random.uniform(size=num)
#XXX: np.choice uses searchsorted(bins, u) instead of digitize
choices_idx = np.digitize(u, bins) - 1
else:
choices_idx = []
if config.debug:
print("random sequence position after:", np.random.get_state()[2])
if any(isinstance(c, Expr) for c in choices):
choices = np.array([expr_eval(expr, context) for expr in choices])
return choices[choices_idx]
示例4: evaluate
def evaluate(self, context):
# from Wikipedia:
# G = 1/n * (n + 1 - 2 * (sum((n + 1 - i) * a[i]) / sum(a[i])))
# i=1..n i=1..n
# but sum((n + 1 - i) * a[i])
# i=1..n
# = sum((n - i) * a[i] for i in range(n))
# = sum(cumsum(a))
values = expr_eval(self.expr, context)
if isinstance(values, (list, tuple)):
values = np.array(values)
filter_expr = self._getfilter(context)
if filter_expr is not None:
filter_values = expr_eval(filter_expr, context)
else:
filter_values = True
filter_values &= ispresent(values)
values = values[filter_values]
sorted_values = np.sort(values)
n = len(values)
# force float to avoid overflows with integer input expressions
cumsum = np.cumsum(sorted_values, dtype=float)
values_sum = cumsum[-1]
return (n + 1 - 2 * np.sum(cumsum) / values_sum) / n
示例5: run
def run(self, context):
plt.figure()
args = [expr_eval(arg, context) for arg in self.args]
kwargs = dict((k, expr_eval(v, context))
for k, v in self.kwargs.iteritems())
self._draw(*args, **kwargs)
plt.show()
示例6: evaluate
def evaluate(self, context):
values = expr_eval(self.expr, context)
values = np.asarray(values)
filter_expr = self._getfilter(context)
if filter_expr is not None:
filter_values = expr_eval(filter_expr, context)
else:
filter_values = True
if self.skip_na:
# we should *not* use an inplace operation because filter_values
# can be a simple variable
filter_values = filter_values & ispresent(values)
if filter_values is not True:
values = values[filter_values]
# from Wikipedia:
# G = 1/n * (n + 1 - 2 * (sum((n + 1 - i) * a[i]) / sum(a[i])))
# i=1..n i=1..n
# but sum((n + 1 - i) * a[i])
# i=1..n
# = sum((n - i) * a[i] for i in range(n))
# = sum(cumsum(a))
sorted_values = np.sort(values)
n = len(values)
# force float to avoid overflows with integer input expressions
cumsum = np.cumsum(sorted_values, dtype=float)
values_sum = cumsum[-1]
if values_sum == 0:
print("gini(%s, filter=%s): expression is all zeros (or nan) "
"for filter" % (self.expr, filter_expr))
return (n + 1 - 2 * np.sum(cumsum) / values_sum) / n
示例7: eval_assertion
def eval_assertion(self, context, exception, expr):
try:
expr_eval(expr)
return "did not raise"
except eval(exception):
return False
except Exception as e:
return "raised another exception (%s)" % e
示例8: evaluate
def evaluate(self, context):
if self.filter is not None:
filter_value = expr_eval(self.filter, context)
else:
filter_value = None
if self.expressions:
expressions = list(self.expressions)
else:
# extra=False because we don't want globals nor "system" variables
# (nan, period, __xxx__)
expressions = [Variable(name)
for name in context.keys(extra=False)]
str_expressions = [str(e) for e in expressions]
if 'id' not in str_expressions:
str_expressions.insert(0, 'id')
expressions.insert(0, Variable('id'))
id_pos = 0
else:
id_pos = str_expressions.index('id')
# if (self.periods is not None and len(self.periods) and
# 'period' not in str_expressions):
# str_expressions.insert(0, 'period')
# expressions.insert(0, Variable('period'))
# id_pos += 1
columns = []
for expr in expressions:
expr_value = expr_eval(expr, context)
if (filter_value is not None and isinstance(expr_value, np.ndarray)
and expr_value.shape):
expr_value = expr_value[filter_value]
columns.append(expr_value)
ids = columns[id_pos]
if isinstance(ids, np.ndarray) and ids.shape:
numrows = len(ids)
else:
numrows = 1
# expand scalar columns to full columns in memory
for idx, col in enumerate(columns):
dtype = None
if not isinstance(col, np.ndarray):
dtype = type(col)
elif not col.shape:
dtype = col.dtype.type
if dtype is not None:
newcol = np.empty(numrows, dtype=dtype)
newcol.fill(col)
columns[idx] = newcol
data = izip(*columns)
table = chain([str_expressions], data) if self.header else data
return PrettyTable(table, self.missing)
示例9: eval_assertion
def eval_assertion(self, context):
r1 = expr_eval(self.expr1, context)
r2 = expr_eval(self.expr2, context)
if isinstance(r1, np.ndarray) and isinstance(r2, np.ndarray):
passed = np.array_equal(r1, r2)
else:
passed = r1 == r2
if not passed:
return "%s != %s (%s != %s)" % (r1, r2, self.expr1, self.expr2)
示例10: value_for_period
def value_for_period(self, expr, period, context, fill='auto'):
sub_context = EntityContext(self, {'period': period})
result = expr_eval(expr, sub_context)
if isinstance(result, np.ndarray) and result.shape:
ids = expr_eval(Variable('id'), sub_context)
if fill is None:
return ids, result
else:
# expand values to the current "outer" context
return self.fill_missing_values(ids, result, context, fill)
else:
return result
示例11: eval_assertion
def eval_assertion(self, context):
v1 = expr_eval(self.expr1, context)
v2 = expr_eval(self.expr2, context)
result = self.compare(v1, v2)
if isinstance(result, tuple):
result, details = result
else:
details = ''
if not result:
op = self.inv_op
return "%s %s %s (%s %s %s)%s" % (self.expr1, op, self.expr2,
v1, op, v2, details)
示例12: _eval_need
def _eval_need(self, context, scores, filter_value):
expressions = self.expressions
possible_values = self.possible_values
if isinstance(self.need, (tuple, list)):
need = np.array([expr_eval(e, context) for e in self.need])
elif isinstance(self.need, Expr):
need = expr_eval(self.need, context)
# need was a *scalar* expr
if not (isinstance(need, np.ndarray) and need.shape):
need = np.array([need])
else:
need = self.need
if self.need[0] is None and self.method == "sidewalk":
#Note: need is calculated over score and we could think of
# calculate without leave_filter and without take_filter
if filter_value is not None:
scores = scores[filter_value]
need = int(sum(scores))
need = np.array([need])
if isinstance(need, LabeledArray):
if not expressions:
expressions = [Variable(expressions_context.entity, name)
for name in need.dim_names]
if not possible_values:
possible_values = need.pvalues
assert isinstance(need, np.ndarray)
if len(expressions) != len(possible_values):
raise Exception("align() expressions and possible_values "
"have different length: %d vs %d"
% (len(expressions), len(possible_values)))
if 'period' in [str(e) for e in expressions]:
period = context.period
expressions, possible_values, need = \
kill_axis('period', period, expressions, possible_values,
need, abs(self.periodicity_given))
# kill any axis where the value is constant for all individuals
# satisfying the filter
# tokill = [(expr, column[0])
# for expr, column in zip(expressions, columns)
# if isconstant(column, filter_value)]
# for expr, value in tokill:
# expressions, possible_values, need = \
# kill_axis(str(expr), value, expressions, possible_values,
# need)
return need, expressions, possible_values
示例13: align_no_link
def align_no_link(self, context, score, need, filter, take, leave,
expressions, possible_values, errors, frac_need, link,
secondary_axis, method):
ctx_length = context_length(context)
need, expressions, possible_values = \
self._eval_need(context, need, expressions, possible_values)
filter_value = expr_eval(self._getfilter(context, filter), context)
if filter_value is not None:
num_to_align = np.sum(filter_value)
else:
num_to_align = ctx_length
# retrieve the columns we need to work with
if expressions:
columns = [expr_eval(expr, context) for expr in expressions]
if filter_value is not None:
groups = partition_nd(columns, filter_value, possible_values)
else:
groups = partition_nd(columns, True, possible_values)
else:
columns = []
if filter_value is not None:
groups = [filter_to_indices(filter_value)]
else:
groups = [np.arange(num_to_align)]
# the sum is not necessarily equal to len(a), because some individuals
# might not fit in any group (eg if some alignment data is missing)
if sum(len(g) for g in groups) < num_to_align:
unaligned = np.ones(ctx_length, dtype=bool)
if filter_value is not None:
unaligned[~filter_value] = False
for member_indices in groups:
unaligned[member_indices] = False
self._display_unaligned(expressions, context['id'], columns,
unaligned)
# noinspection PyAugmentAssignment
need = need * self._get_need_correction(groups, possible_values)
need = self._handle_frac_need(need, frac_need)
need = self._add_past_error(context, need, errors)
need = np.asarray(need)
# FIXME: either handle past_error in no link (currently, the past
# error is added... but never computed, so always 0 !) or raise
# an error in case errors='carry" is used with no link.
return align_get_indices_nd(ctx_length, groups, need, filter_value,
score, take, leave, method)
示例14: align_no_link
def align_no_link(self, context):
ctx_length = context_length(context)
scores = expr_eval(self.expr, context)
need, expressions, possible_values = self._eval_need(context)
filter_value = expr_eval(self._getfilter(context), context)
take_filter = expr_eval(self.take_filter, context)
leave_filter = expr_eval(self.leave_filter, context)
if filter_value is not None:
num_to_align = np.sum(filter_value)
else:
num_to_align = ctx_length
if expressions:
# retrieve the columns we need to work with
columns = [expr_eval(expr, context) for expr in expressions]
if filter_value is not None:
groups = partition_nd(columns, filter_value, possible_values)
else:
groups = partition_nd(columns, True, possible_values)
else:
columns = []
if filter_value is not None:
groups = [filter_to_indices(filter_value)]
else:
groups = [np.arange(num_to_align)]
# the sum is not necessarily equal to len(a), because some individuals
# might not fit in any group (eg if some alignment data is missing)
if sum(len(g) for g in groups) < num_to_align:
unaligned = np.ones(ctx_length, dtype=bool)
if filter_value is not None:
unaligned[~filter_value] = False
for member_indices in groups:
unaligned[member_indices] = False
self._display_unaligned(expressions, context['id'], columns,
unaligned)
#noinspection PyAugmentAssignment
need = need * self._get_need_correction(groups, possible_values)
need = self._handle_frac_need(need)
need = self._add_past_error(need, context)
return align_get_indices_nd(ctx_length, groups, need, filter_value,
scores, take_filter, leave_filter)
示例15: run_guarded
def run_guarded(self, context):
while expr_eval(self.cond, context):
self.code.run_guarded(context)
# FIXME: this is a bit brutal :) This is necessary because
# otherwise test_while loops indefinitely (because "values" is
# never incremented)
expr_cache.clear()