コード例 #1
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
コード例 #2
0
def predict_preserving_subtract_diff(prob, anlg, tran, d):
    best_diff_to_b2_x = d.get("diff_to_b2_x")
    best_diff_to_b2_y = d.get("diff_to_b2_y")
    best_diff_to_b3_x = d.get("diff_to_b3_x")
    best_diff_to_b3_y = d.get("diff_to_b3_y")
    best_diff = d.get("diff")
    b4 = prob.matrix[anlg.get("value")[3]]
    b5 = prob.matrix[anlg.get("value")[4]]
    b2_ref = prob.matrix_ref[anlg.get("value")[0]]

    prediction, best_diff_to_prediction_x, best_diff_to_prediction_y = transform.subtract_diff(
        b5,
        best_diff_to_b2_x,
        best_diff_to_b2_y,
        best_diff,
        b2_ref,
        coords=True)

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

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

        b5_score, diff_to_opt_x, diff_to_opt_y, diff_to_b5_x, diff_to_b5_y, diff = \
            asymmetric_jaccard.asymmetric_jaccard_coef(opt, b5)
        opt_to_b5_x = (-diff_to_opt_x) - (-diff_to_b5_x)
        opt_to_b5_y = (-diff_to_opt_y) - (-diff_to_b5_y)
        b3_to_b2_x = (-best_diff_to_b3_x) - (-best_diff_to_b2_x)
        b3_to_b2_y = (-best_diff_to_b3_y) - (-best_diff_to_b2_y)
        if abs(opt_to_b5_x - b3_to_b2_x) > 2 or abs(opt_to_b5_y -
                                                    b3_to_b2_y) > 2:
            b5_score, diff = asymmetric_jaccard.asymmetric_jaccard_coef_pos_fixed(
                opt, b5, b3_to_b2_x, b3_to_b2_y)
        diff_score, _, _ = jaccard.jaccard_coef(diff, best_diff)
        opt_score, _, _ = jaccard.jaccard_coef(prediction, opt)

        b4_b5_aj = asymmetric_jaccard.asymmetric_jaccard_coef(b4, b5)
        b4_opt_aj = asymmetric_jaccard.asymmetric_jaccard_coef(b4, opt)

        preserving_score = min(b4_b5_aj[0], b4_opt_aj[0])

        score = (diff_score + b5_score + diff_score + preserving_score) / 4
        pred_data.append({
            **d, "optn": ii + 1,
            "optn_score": score,
            "mato_score": (d.get("mat_score") + score) / 2,
            "pred": prediction
        })

    return pred_data
コード例 #3
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
コード例 #4
0
def predict_subtract_diff(prob, anlg, tran, d):
    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_ref = prob.matrix_ref[anlg.get("value")[0]]

    prediction, best_diff_to_prediction_x, best_diff_to_prediction_y = transform.subtract_diff(
        u3,
        best_diff_to_u1_x,
        best_diff_to_u1_y,
        best_diff,
        u1_ref,
        coords=True)

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

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

        u3_score, diff_to_opt_x, diff_to_opt_y, diff_to_u3_x, diff_to_u3_y, diff = \
            asymmetric_jaccard.asymmetric_jaccard_coef(opt, u3)
        opt_to_u3_x = (-diff_to_opt_x) - (-diff_to_u3_x)
        opt_to_u3_y = (-diff_to_opt_y) - (-diff_to_u3_y)
        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)
        if abs(opt_to_u3_x - u2_to_u1_x) > 2 or abs(opt_to_u3_y -
                                                    u2_to_u1_y) > 2:
            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(prediction, opt)

        # prediction_to_opt_x = -to_pred_x + to_opt_x
        # prediction_to_opt_y = -to_pred_y + to_opt_y
        #
        # best_diff_to_opt_x = best_diff_to_prediction_x + prediction_to_opt_x
        # best_diff_to_opt_y = best_diff_to_prediction_y + prediction_to_opt_y
        # best_diff_aligned, opt_aligned, _, _ = utils.align(best_diff, opt, best_diff_to_opt_x, best_diff_to_opt_y)
        # u3_restored = utils.trim_binary_image(np.logical_or(best_diff_aligned, opt_aligned))
        # u3_restored_score , _, _, _, _, _ = asymmetric_jaccard.asymmetric_jaccard_coef(u3, u3_restored)

        score = (diff_score + u3_score + opt_score) / 3
        pred_data.append({
            **d, "optn": ii + 1,
            "optn_score": score,
            "mato_score": (d.get("mat_score") + score) / 2,
            "pred": prediction
        })

    return pred_data
コード例 #5
0
def evaluate_duplicate(u1, u2):
    scores = []
    u1_to_u2_xs = []
    u1_to_u2_ys = []
    current = u2.copy()
    current_to_u2_x = 0
    current_to_u2_y = 0
    u1_tr = utils.trim_binary_image(u1)
    while current.sum():
        old_score_tmp, old_diff_tmp_to_u1_x, old_diff_tmp_to_u1_y, old_diff_tmp_to_current_x, old_diff_tmp_to_current_y, old_diff_tmp = \
            asymmetric_jaccard.asymmetric_jaccard_coef(u1, current)

        score_tmp, diff_tmp_to_u1_x, diff_tmp_to_u1_y, diff_tmp_to_current_x, diff_tmp_to_current_y, diff_tmp = \
            soft_jaccard.soft_jaccard(u1, current, asymmetric = True)

        if score_tmp < 0.9:
            break

        scores.append(score_tmp)
        u1_to_current_x = (-diff_tmp_to_u1_x) - (-diff_tmp_to_current_x)
        u1_to_current_y = (-diff_tmp_to_u1_y) - (-diff_tmp_to_current_y)
        u1_to_u2_x = u1_to_current_x + current_to_u2_x
        u1_to_u2_y = u1_to_current_y + current_to_u2_y
        u1_to_u2_xs.append(u1_to_u2_x)
        u1_to_u2_ys.append(u1_to_u2_y)

        current = diff_tmp
        current_to_u2_x = diff_tmp_to_current_x + current_to_u2_x
        current_to_u2_y = diff_tmp_to_current_y + current_to_u2_y

    if 1 >= len(scores):
        mat_score = 0
        u1_to_u2_locs = []
    else:
        mat_score = np.mean(scores)
        u1_to_u2_xs = ((np.array(u1_to_u2_xs) - min(u1_to_u2_xs)) /
                       u1_tr.shape[1]).tolist()
        u1_to_u2_ys = ((np.array(u1_to_u2_ys) - min(u1_to_u2_ys)) /
                       u1_tr.shape[0]).tolist()
        u1_to_u2_locs = np.array(list(zip(u1_to_u2_xs, u1_to_u2_ys)))

    stub = utils.make_stub(u1_to_u2_locs)

    return mat_score, stub
コード例 #6
0
def predict_add_diff(prob, anlg, tran, d):

    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_ref = prob.matrix_ref[anlg.get("value")[0]]
    prediction = transform.add_diff(u3, best_diff_to_u1_x, best_diff_to_u1_y,
                                    best_diff, u1_ref)

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

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

        u3_score, diff_to_u3_x, diff_to_u3_y, diff_to_opt_x, diff_to_opt_y, diff = \
            asymmetric_jaccard.asymmetric_jaccard_coef(u3, opt)
        u3_to_opt_x = (-diff_to_u3_x) - (-diff_to_opt_x)
        u3_to_opt_y = (-diff_to_u3_y) - (-diff_to_opt_y)
        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)
        if abs(u3_to_opt_x - u1_to_u2_x) > 2 or abs(u3_to_opt_y -
                                                    u1_to_u2_y) > 2:
            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

        pred_data.append({
            **d, "optn": ii + 1,
            "optn_score": score,
            "mato_score": (d.get("mat_score") + score) / 2,
            "pred": prediction
        })

    return pred_data
コード例 #7
0
def run_prob_anlg_tran_2x2(prob, anlg, tran):
    u1 = prob.matrix[anlg.get("value")[0]]
    u2 = prob.matrix[anlg.get("value")[1]]

    diff_to_u1_x = None
    diff_to_u1_y = None
    diff_to_u2_x = None
    diff_to_u2_y = None
    diff = None
    copies_to_u1_x = None
    copies_to_u1_y = None
    u1_coms_x = None
    u1_coms_y = None
    u2_coms_x = None
    u2_coms_y = None

    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)
    elif "xor_diff" == tran.get("name"):
        score, u1_to_u2_x, u1_to_u2_y = jaccard.jaccard_coef(u1, u2)
        score = 1 - score
        u1_aligned, u2_aligned, aligned_to_u2_x, aligned_to_u2_y = utils.align(
            u1, u2, u1_to_u2_x, u1_to_u2_y)
        diff = utils.erase_noise_point(np.logical_xor(u1_aligned, u2_aligned),
                                       4)
        diff_to_u2_x = int(aligned_to_u2_x)
        diff_to_u2_y = int(aligned_to_u2_y)
        diff_to_u1_x = int(diff_to_u2_x - u1_to_u2_x)
        diff_to_u1_y = int(diff_to_u2_y - u1_to_u2_y)
    elif "upscale_to" == tran.get("name"):
        u1_upscaled = transform.upscale_to(u1, u2)
        score, _, _ = jaccard.jaccard_coef(u2, u1_upscaled)
    elif "duplicate" == tran.get("name"):
        scores = []
        u1_to_u2_xs = []
        u1_to_u2_ys = []
        current = u2.copy()
        current_to_u2_x = 0
        current_to_u2_y = 0
        while current.sum():
            score_tmp, diff_tmp_to_u1_x, diff_tmp_to_u1_y, diff_tmp_to_current_x, diff_tmp_to_current_y, _ = \
                asymmetric_jaccard.asymmetric_jaccard_coef(u1, current)

            if score_tmp < 0.6:
                break

            scores.append(score_tmp)
            u1_to_current_x = (-diff_tmp_to_u1_x) - (-diff_tmp_to_current_x)
            u1_to_current_y = (-diff_tmp_to_u1_y) - (-diff_tmp_to_current_y)
            u1_to_u2_x = u1_to_current_x + current_to_u2_x
            u1_to_u2_y = u1_to_current_y + current_to_u2_y
            u1_to_u2_xs.append(u1_to_u2_x)
            u1_to_u2_ys.append(u1_to_u2_y)
            u1_aligned, current_aligned, aligned_to_current_x, aligned_to_current_y = utils.align(
                u1, current, u1_to_current_x, u1_to_current_y)
            current = utils.erase_noise_point(
                np.logical_and(current_aligned, np.logical_not(u1_aligned)), 8)
            current_to_u2_x = aligned_to_current_x + current_to_u2_x
            current_to_u2_y = aligned_to_current_y + current_to_u2_y

        if 1 >= len(scores):
            score = 0
            copies_to_u1_x = [0]
            copies_to_u1_y = [0]
        else:
            score = min(scores)
            copies_to_u1_x = (np.array(u1_to_u2_xs[1:]) -
                              u1_to_u2_xs[0]).tolist()
            copies_to_u1_y = (np.array(u1_to_u2_ys[1:]) -
                              u1_to_u2_ys[0]).tolist()
    elif "rearrange" == tran.get("name"):
        score, u1_coms_x, u1_coms_y, u2_coms_x, u2_coms_y = transform.evaluate_rearrange(
            u1, u2)
    else:
        u1_t = transform.apply_unary_transformation(u1, tran)
        score, _, _ = jaccard.jaccard_coef(u1_t, u2)

    if "mirror" == tran.get("name") or "mirror_rot_180" == tran.get("name"):
        # if u1 or u2 is already symmetric, then we shouldn't use mirror tran.
        u1_mirror_score, _, _ = jaccard.jaccard_coef(u1_t, u1)
        u2_mirror = transform.apply_unary_transformation(u2, tran)
        u2_mirror_score, _, _ = jaccard.jaccard_coef(u2_mirror, u2)
        if max(u1_mirror_score, u2_mirror_score) > 0.9:
            score = 0

    prob_anlg_tran_d = assemble_prob_anlg_tran_d(prob,
                                                 anlg,
                                                 tran,
                                                 score,
                                                 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,
                                                 copies_to_u1_x=copies_to_u1_x,
                                                 copies_to_u1_y=copies_to_u1_y,
                                                 u1_coms_x=u1_coms_x,
                                                 u1_coms_y=u1_coms_y,
                                                 u2_coms_x=u2_coms_x,
                                                 u2_coms_y=u2_coms_y)

    return prob_anlg_tran_d
コード例 #8
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
    }