def test_mean(): template_h = Aggregate(Orientation.HORIZONTAL, Operation.AVERAGE) template_v = Aggregate(Orientation.VERTICAL, Operation.AVERAGE) x = np.array([ [20.3, 14, 7], [8.9, 1.6, 5.2], [2.3, 43.8, 140], ]) result = evaluate_template(template_h, {template_h.x: x}) assert all(result == np.mean(x, axis=1)) result = evaluate_template(template_v, {template_v.x: x}) assert all(result == np.mean(x, axis=0))
def test_rank(): template = Rank() x = np.array([4, 17.3, 17.3, 50.2]) target = np.array([4, 2, 2, 1]) result = evaluate_template(template, {template.x: x}) assert all(target == result)
def test_sum(): template = Aggregate(Orientation.HORIZONTAL, Operation.SUM) x = np.array([ [5, 6, 7], [10, 20, 30], ]) result = evaluate_template(template, {template.x: x}) assert all(result == np.array([5 + 6 + 7, 10 + 20 + 30]))
def predict(self, input_matrix): from tacle.engine import evaluate variables = [ v for i, v in enumerate(self.template.variables) if v != self.template.target ] if len(variables) == 1 and input_matrix.shape[1] > 1: assignment = {variables[0]: input_matrix} else: assignment = {v.name: input_matrix[:, i] for i, v in variables} return evaluate.evaluate_template(self.template, assignment)
def test_conditional_sum(): template = ConditionalAggregate(Operation.SUM) assignment = { template.o_key: np.array(["a", "b", "c"]), template.f_key: np.array(["b", "b", "c", "b", "c"]), template.values: np.array([10.2, 3.7, 5.12, 20, 1]), } result = evaluate_template(template, assignment) assert 0 == result[0] assert sum(assignment[template.values][np.array( [True, True, False, True, False])]) == result[1] assert sum(assignment[template.values][np.array( [False, False, True, False, True])]) == result[2]
def test_lookup(): template = Lookup() ok = np.array([1, 2, 3]) ov = np.array(["a", "b", "c"]) d = {1: "a", 2: "b", 3: "c"} fk = np.array([2, 2, 3, 3, 2]) result = evaluate_template(template, { template.o_key: ok, template.f_key: fk, template.o_value: ov }) assert all(result == [d[k] for k in fk])
def is_aggregate(ok, fk, v, r=None): if not overlap[frozenset({ok, fk})]: return False foreign_key = ForeignKey() if solutions.has(foreign_key, [foreign_key.fk, foreign_key.pk], [ok, fk]): return False if r is None: vectors = {g: g.get_vector(1) for g in [ok, fk, v]} r = "?" vectors[r] = evaluate.evaluate_template( c, {k: assignment[k.name].get_vector(1) for k in keys}).flatten() else: if solutions.has(foreign_key, [foreign_key.fk, foreign_key.pk], [r, v]): return False vectors = {g: g.get_vector(1) for g in [ok, r, fk, v]} for g in [ok, r, v]: # FIXME avoid all call if g not in partial_cache: partial_cache[g] = all( numpy.vectorize(blank_filter(vectors[g])[1])( vectors[g])) if not partial_cache[g]: return False if fk not in fk_dict: filtered = vectors[fk][blank_filter(vectors[fk], True)[1](vectors[fk])] unique = set(filtered) masks = {u: vectors[fk] == u for u in unique} fk_dict[fk] = masks any_match = False for i in range(len(vectors[ok])): if vectors[ok][i] not in fk_dict[fk]: res = c.default else: k = vectors[ok][i] data = vectors[v][fk_dict[fk][k]] res = c.operation.aggregate( data) if len(data) > 0 else c.default any_match = True n_digits = precision_and_scale(vectors[r][i])[1] res = numpy.round(res, n_digits) if not equal(res, vectors[r][i]): return False lookup = Lookup() if solutions.has(lookup, [ lookup.f_value, lookup.f_key, lookup.o_key, lookup.o_value ], [fk, v, r, ok]): return False return any_match