Example #1
0
def rotated_mean(N=100):
    images = lambda: util.images().pad(100, 100)
    angles = lambda: util.images().pad(100, 100).map(transform.PCA_angle)
    rotated = lambda: util.seq(transform.rotate(img, ang) for img, ang in zip(images(), angles()))

    logger.info("Calculating mean rotated image")
    mean_r = util.mean(rotated(), np.zeros((100, 100)))

    # distance
    d = np.linalg.norm
    dist = (d(img - mean_r) for img in util.progress(rotated()))

    logger.info(f"Finding {N} farthest away from the mean")
    oddities = nlargest(N, zip(dist, util.image_ids()))

    logger.info(f"Finished rotated mean outliers")
    return oddities
Example #2
0
def centered_mean(N=100):
    centered = lambda: util.images().pad(100, 100).map(transform.center)

    logger.info("Calculating mean centered image")
    mean_c = util.mean(centered(), np.zeros((100, 100)))

    # distance
    d = np.linalg.norm
    dist = (d(img - mean_c) for img in util.progress(centered()))

    logger.info(f"Finding {N} farthest away from the mean")
    oddities = nlargest(N, zip(dist, util.image_ids()))

    logger.info(f"Finished centered mean outliers")
    return oddities
Example #3
0
def amplitude_mean(N=100):
    amps = lambda: util.images().pad(100, 100).map(transform.amplitude)

    logger.info("Calculating mean amplitude")
    mean_a = util.mean(amps(), np.zeros((100, 100)))

    # distance
    d = np.linalg.norm
    dist = (d(amp - mean_a) for amp in util.progress(amps()))

    logger.info(f"Finding {N} farthest away from the mean")
    oddities = nlargest(N, zip(dist, util.image_ids()))

    logger.info(f"Finished amplitude mean outliers")
    return oddities
Example #4
0
def mean(N=100):
    images = lambda: util.images().pad(100, 100)

    logger.info("Calculating mean image")
    mean = util.mean(images())

    # max-correlation of mean
    d = util.max_correlation
    d = np.linalg.norm
    maxcorr = (d(img - mean) for img in util.progress(images()))

    logger.info(f"Finding {N} with least correlation with the mean")
    oddities = nlargest(N, zip(maxcorr, util.image_ids()))

    logger.info(f"Finished maxcorr outliers")
    return oddities
Example #5
0
def histogram_mean(N=100):
    hists = lambda: util.images().map(transform.smoothen).map(transform.grayscale)

    logger.info("Calculating mean histogram")
    mean_h = util.mean(hists(), np.zeros((256)))

    # distance
    d = np.linalg.norm
    d = util.max_correlation1d
    dist = (d(hist, mean_h) for hist in util.progress(hists()))

    logger.info(f"Finding {N} farthest away from the mean")
    oddities = nlargest(N, zip(dist, util.image_ids()))

    logger.info(f"Finished histogram mean outliers")
    return oddities
Example #6
0
def frequency_mean(N=100):
    phases = lambda: util.images().pad(100, 100).map(transform.phase)

    logger.info("Calculating mean phase")
    mean_p = util.mean(phases(), np.zeros((100, 100)))

    # mean_p = transform.shape(mean_p, shape="circle", mask_value=0, radius=25)

    # distance
    d = np.linalg.norm
    d = util.max_correlation
    dist = (-d(phase, mean_p) for phase in util.progress(phases()))

    logger.info(f"Finding {N} farthest away from the mean")
    oddities = nlargest(N, zip(dist, util.image_ids()))

    logger.info(f"Finished phase mean outliers")
    return oddities
Example #7
0
def PCA_angle(N=100):
    """
    Computes the mean angle, the finds N angles that deviates the most from
    the mean, modulo pi.

    There was an issue with the angle of the PCA to be inverted 180 degrees.
    This shouldn't affect the mean too much, but caused some images to be
    incorrectly classified as anomalies. Thus, we consider only angles
    between [-90, 90).
    """

    angles = lambda: util.images().pad(100, 100).map(transform.PCA_angle)

    logger.info("Calculating PCA angle mean")
    mean = util.angle_mean(angles())

    # distances from mean angle
    distances = (util.circle_dist(mean, angle) for angle in angles())

    logger.info(f"Finding {N} angles farthest away from mean")
    oddities = nlargest(N, zip(distances, util.image_ids()))

    logger.info(f"Finished PCA angle outliers")
    return oddities
Example #8
0
    for k, (i, j) in enumerate(block_loop(n)):
        axes[i, j].plot(circle_x, circle_y, color='k', lw=1.2)
        axes[i, j].plot(*line(angles[k]), color='k', lw=1.2)


with Figure(2, 5) as fig:
    axes = get_axes(2, 5, fig)

    arrow(axes[..., 2])

    for col_index in [0, 1, 3, 4]:
        clean(axes[..., col_index])
        square(axes[..., col_index])

    angles = lambda: (util.images().pad().map(transform.PCA_angle))

    # block 1
    img_mean = util.mean(util.images().pad(100, 100))
    img_3 = [
        util.pad(util.image(i), 100, 100) for i in ["1377", "1590", "9728"]
    ]

    samples = [img_mean] + img_3

    block_imshow(1, axes, samples)

    # block 2
    angle_mean = util.angle_mean(angles())
    angle_3 = list(map(transform.PCA_angle, img_3))
Example #9
0
            img = util.DUMMY_IMAGE()

            img = center(img)

            ax.imshow(img, cmap="gray")

            fig.update(1)

    #--- Fixed shift

    if False:
        with Figure(1, 1) as fig:
            ax = fig.ax[0, 0]

            key = ["3337", "1590", "1377"][0]
            for img in util.images():
                img = util.pad(img, 100, 100)

                # angle = PCA_angle(img) / tau * 360
                # img = rotate(img, angle=angle)

                img = shift(img, 20, 50)

                ax.clear()
                ax.imshow(img, cmap="gray")

                fig.update()

    #--- PCA angles

    if False: