Ejemplo n.º 1
0
def run_prob_anlg_tran_3x2_and_3x2(prob, anlg, tran):
    b1_to_b2_x = None
    b1_to_b2_y = None
    diff_to_b3_x = None
    diff_to_b3_y = None
    diff_to_b2_x = None
    diff_to_b2_y = None
    diff = None

    b1 = prob.matrix[anlg.get("value")[0]]
    b2 = prob.matrix[anlg.get("value")[1]]
    b3 = prob.matrix[anlg.get("value")[2]]

    if "inv_unite" == tran.get("name"):
        b1_new, _, _, _, _ = transform.apply_binary_transformation(
            b2, b3, transform.get_tran("unite"), imgC=b1)
        score, _, _ = jaccard.jaccard_coef(b1_new, b1)
    elif "preserving_subtract_diff" == tran.get("name"):
        b2_aj = asymmetric_jaccard.asymmetric_jaccard_coef(b1, b2)
        b3_aj = asymmetric_jaccard.asymmetric_jaccard_coef(b1, b3)
        preserving_score = min(b2_aj[0], b3_aj[0])
        score, diff_to_b3_x, diff_to_b3_y, diff_to_b2_x, diff_to_b2_y, diff = \
            asymmetric_jaccard.asymmetric_jaccard_coef(b3, b2)
        if preserving_score < 0.85:
            score = 0
    else:
        b1_b2_t, b1_to_b2_x, b1_to_b2_y, _, _ = transform.apply_binary_transformation(
            b1, b2, tran, imgC=b3)
        score, _, _ = jaccard.jaccard_coef(b1_b2_t, b3)

    if "inv_unite" == tran.get("name"):
        b2_score, _, _, _, _, _ = asymmetric_jaccard.asymmetric_jaccard_coef(
            b2, b3)
        b3_score, _, _, _, _, _ = asymmetric_jaccard.asymmetric_jaccard_coef(
            b3, b2)
        if max(b2_score, b3_score) > 0.9:
            score = 0

    if "unite" == tran.get("name") or "shadow_mask_unite" == tran.get("name"):
        b1_score, _, _, _, _, _ = asymmetric_jaccard.asymmetric_jaccard_coef(
            b1, b2)
        b2_score, _, _, _, _, _ = asymmetric_jaccard.asymmetric_jaccard_coef(
            b2, b1)
        if max(b1_score,
               b2_score) > 0.9:  # if b1 is almost a subset of b2 or vice versa
            score = 0

    prob_anlg_tran_d = assemble_prob_anlg_tran_d(prob,
                                                 anlg,
                                                 tran,
                                                 score,
                                                 b1_to_b2_x=b1_to_b2_x,
                                                 b1_to_b2_y=b1_to_b2_y,
                                                 diff_to_b3_x=diff_to_b3_x,
                                                 diff_to_b3_y=diff_to_b3_y,
                                                 diff_to_b2_x=diff_to_b2_x,
                                                 diff_to_b2_y=diff_to_b2_y,
                                                 diff=diff)

    return prob_anlg_tran_d
Ejemplo n.º 2
0
def predict_inv_unite(prob, anlg, tran, d):
    b4 = prob.matrix[anlg.get("value")[3]]
    b5 = prob.matrix[anlg.get("value")[4]]

    pred_data = []
    for ii, opt in enumerate(prob.options):

        print(prob.name, anlg.get("name"), tran.get("name"), ii)

        # prediction = transform.inv_unite(b4, b5, opt)
        # score, _, _ = jaccard.jaccard_coef(opt, prediction)
        b4_new, _, _, _, _ = transform.apply_binary_transformation(
            b5, opt, transform.get_tran("unite"), imgC=b4)
        score, _, _ = jaccard.jaccard_coef(b4_new, b4)
        b5_score, _, _, _, _, _ = asymmetric_jaccard.asymmetric_jaccard_coef(
            b5, opt)
        opt_score, _, _, _, _, _ = asymmetric_jaccard.asymmetric_jaccard_coef(
            opt, b5)
        if max(b5_score, opt_score) > 0.85:
            score = 0
        pred_data.append({
            **d, "optn": ii + 1,
            "optn_score": score,
            "mato_score": (d.get("mat_score") + score) / 2,
            "pred": opt
        })

    return pred_data
Ejemplo n.º 3
0
def predict_binary_default(prob, anlg, tran, d):
    b4 = prob.matrix[anlg.get("value")[3]]
    b5 = prob.matrix[anlg.get("value")[4]]

    prediction, _, _, _, _ = transform.apply_binary_transformation(
        b4, b5, tran)

    pred_data = []
    for ii, opt in enumerate(prob.options):
        score, _, _ = jaccard.jaccard_coef(opt, prediction)
        pred_data.append({
            **d, "optn": ii + 1,
            "optn_score": score,
            "mato_score": (d.get("mat_score") + score) / 2,
            "pred": prediction
        })

    return pred_data
Ejemplo n.º 4
0
def predict(prob, d):

    anlg = analogy.get_anlg(d.get("anlg_name"))
    tran = transform.get_tran(d.get("tran_name"))

    if 3 == len(anlg.get("value")):
        best_diff_to_u1_x = d.get("diff_to_u1_x")
        best_diff_to_u1_y = d.get("diff_to_u1_y")
        best_diff_to_u2_x = d.get("diff_to_u2_x")
        best_diff_to_u2_y = d.get("diff_to_u2_y")
        best_diff = d.get("diff")
        u3 = prob.matrix[anlg.get("value")[2]]
        u1 = prob.matrix[anlg.get("value")[0]]

        if tran.get("name") == "add_diff":
            prediction = transform.add_diff(u3, best_diff_to_u1_x,
                                            best_diff_to_u1_y, best_diff, u1)
        elif tran.get("name") == "subtract_diff":
            prediction = transform.subtract_diff(u3, best_diff_to_u1_x,
                                                 best_diff_to_u1_y, best_diff,
                                                 u1)
        else:
            prediction = transform.apply_unary_transformation(u3, tran)

    elif 5 == len(anlg.get("value")):
        best_b1_to_b2_x = d.get("b1_to_b2_x")
        best_b1_to_b2_y = d.get("b1_to_b2_y")
        b1 = prob.matrix[anlg.get("value")[0]]
        b2 = prob.matrix[anlg.get("value")[1]]
        b4 = prob.matrix[anlg.get("value")[3]]
        b5 = prob.matrix[anlg.get("value")[4]]
        _, b4_to_b1_x, b4_to_b1_y = jaccard.jaccard_coef(b4, b1)
        _, b5_to_b2_x, b5_to_b2_y = jaccard.jaccard_coef(b5, b2)
        b4_to_b5_x = b4_to_b1_x - (b5_to_b2_x - best_b1_to_b2_x)
        b4_to_b5_y = b4_to_b1_y - (b5_to_b2_y - best_b1_to_b2_y)
        prediction, _, _, _, _ = transform.apply_binary_transformation(
            b4, b5, tran, b4_to_b5_x, b4_to_b5_y)

    else:
        raise Exception("Ryan!")

    pred_data = []
    for ii, opt in enumerate(prob.options):

        print(prob.name, anlg.get("name"), tran.get("name"), ii)

        if tran.get("name") == "add_diff":
            u1_to_u2_x = (-best_diff_to_u1_x) - (-best_diff_to_u2_x)
            u1_to_u2_y = (-best_diff_to_u1_y) - (-best_diff_to_u2_y)
            u3_score, diff = asymmetric_jaccard.asymmetric_jaccard_coef_pos_fixed(
                u3, opt, u1_to_u2_x, u1_to_u2_y)
            diff_score, _, _ = jaccard.jaccard_coef(diff, best_diff)
            opt_score, _, _ = jaccard.jaccard_coef(opt, prediction)
            score = (diff_score + opt_score + u3_score) / 3
        elif tran.get("name") == "subtract_diff":
            u2_to_u1_x = (-best_diff_to_u2_x) - (-best_diff_to_u1_x)
            u2_to_u1_y = (-best_diff_to_u2_y) - (-best_diff_to_u1_y)
            u3_score, diff = asymmetric_jaccard.asymmetric_jaccard_coef_pos_fixed(
                opt, u3, u2_to_u1_x, u2_to_u1_y)
            diff_score, _, _ = jaccard.jaccard_coef(diff, best_diff)
            opt_score, _, _ = jaccard.jaccard_coef(opt, prediction)
            score = (diff_score + opt_score + u3_score) / 3
        else:
            score, _, _ = jaccard.jaccard_coef(opt, prediction)

        pred_data.append({
            **d, "optn": ii + 1,
            "pato_score": score,
            "pred": prediction
        })

    return pred_data
Ejemplo n.º 5
0
def run_prob_anlg_tran(prob, anlg, tran):
    """
    compute the result for a combination of a problem, an analogy and a transformation.
    :param prob:
    :param anlg:
    :param tran:
    :return:
    """

    diff_to_u1_x = None
    diff_to_u1_y = None
    diff_to_u2_x = None
    diff_to_u2_y = None
    diff = None
    b1_to_b2_x = None
    b1_to_b2_y = None

    if 3 == len(anlg.get("value")):  # unary anlg and tran

        u1 = prob.matrix[anlg.get("value")[0]]
        u2 = prob.matrix[anlg.get("value")[1]]

        print(prob.name, anlg.get("name"), tran.get("name"))

        if "add_diff" == tran.get("name"):
            score, diff_to_u1_x, diff_to_u1_y, diff_to_u2_x, diff_to_u2_y, diff = \
                asymmetric_jaccard.asymmetric_jaccard_coef(u1, u2)
        elif "subtract_diff" == tran.get("name"):
            score, diff_to_u2_x, diff_to_u2_y, diff_to_u1_x, diff_to_u1_y, diff = \
                asymmetric_jaccard.asymmetric_jaccard_coef(u2, u1)
        else:
            u1_t = transform.apply_unary_transformation(u1, tran)
            score, _, _ = jaccard.jaccard_coef(u1_t, u2)

    elif 5 == len(anlg.get("value")):  # binary anlg and tran

        b1 = prob.matrix[anlg.get("value")[0]]
        b2 = prob.matrix[anlg.get("value")[1]]
        b3 = prob.matrix[anlg.get("value")[2]]

        b1_b2_t, b1_to_b2_x, b1_to_b2_y, _, _ = transform.apply_binary_transformation(
            b1, b2, tran)
        score, _, _ = jaccard.jaccard_coef(b1_b2_t, b3)

    else:
        raise Exception("Ryan!")

    return {
        "prob_name": prob.name,
        "anlg_name": anlg.get("name"),
        "tran_name": tran.get("name"),
        "pat_score": score,  # pat = prob + anlg + tran
        "prob_ansr": prob.answer,
        "prob_type": prob.type,
        "anlg_type": anlg.get("type"),
        "tran_type": tran.get("type"),
        "diff_to_u1_x": diff_to_u1_x,
        "diff_to_u1_y": diff_to_u1_y,
        "diff_to_u2_x": diff_to_u2_x,
        "diff_to_u2_y": diff_to_u2_y,
        "diff": diff,
        "b1_to_b2_x": b1_to_b2_x,
        "b1_to_b2_y": b1_to_b2_y
    }