Example #1
0
 def test_plot_comparison(self, example_shp, tmpdir):
     matplotlib.pyplot.clf()
     x, y, _ = spatial_efd.ProcessGeometry(example_shp[0])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     ax = spatial_efd.InitPlot()
     spatial_efd.plotComparison(ax, coeffs, 10, x, y)
     spatial_efd.SavePlot(ax, 10, tmpdir, 'png')
     assert os.path.isfile('{0}_10.png'.format(tmpdir))
Example #2
0
 def test_plot_comparison_norm(self, example_shp, tmpdir):
     x, y, _ = spatial_efd.ProcessGeometry(example_shp[1])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     coeffs, rotation = spatial_efd.normalize_efd(coeffs,
                                                  size_invariant=False)
     ax = spatial_efd.InitPlot()
     spatial_efd.plotComparison(ax, coeffs, 7, x, y, rotation=rotation)
     spatial_efd.SavePlot(ax, 7, tmpdir, 'png')
     assert os.path.isfile('{0}_7.png'.format(tmpdir))
def reduce_cnts_based_on_coeffs(coeffs: list,
                                cnts: list,
                                percentile=75,
                                plot=True) -> list:
    avgcoeffs = spatial_efd.AverageCoefficients(coeffs)
    SDcoeffs = spatial_efd.AverageSD(coeffs, avgcoeffs)

    if plot:
        median = np.median(np.array(coeffs), axis=0)

        x_med, y_med = spatial_efd.inverse_transform(median, harmonic=10)
        x_avg, y_avg = spatial_efd.inverse_transform(avgcoeffs, harmonic=10)
        x_sd, y_sd = spatial_efd.inverse_transform(SDcoeffs, harmonic=10)

        ax = spatial_efd.InitPlot()
        spatial_efd.PlotEllipse(ax, x_avg, y_avg, color="w", width=2.0)
        spatial_efd.PlotEllipse(ax, x_med, y_med, color="b", width=2.0)

        # Plot avg +/- 1 SD error ellipses
        spatial_efd.PlotEllipse(ax,
                                x_avg + x_sd,
                                y_avg + y_sd,
                                color="r",
                                width=1.0)
        spatial_efd.PlotEllipse(ax,
                                x_avg - x_sd,
                                y_avg - y_sd,
                                color="r",
                                width=1.0)

        plt.close("all")

    arr = np.array(coeffs)
    reshaped = np.reshape(arr, [arr.shape[0], -1])
    MCD = MinCovDet()
    MCD.fit(reshaped)

    a = MCD.mahalanobis(reshaped)

    if plot:
        plt.boxplot(a)
        plt.show()
        plt.close("all")

    percentile = np.percentile(a, percentile)

    reduced = list(np.array(coeffs)[a < percentile])

    avgcoeffs = spatial_efd.AverageCoefficients(reduced)
    SDcoeffs = spatial_efd.AverageSD(reduced, avgcoeffs)

    median = np.median(np.array(reduced), axis=0)

    x_med, y_med = spatial_efd.inverse_transform(median, harmonic=10)
    x_avg, y_avg = spatial_efd.inverse_transform(avgcoeffs, harmonic=10)
    x_sd, y_sd = spatial_efd.inverse_transform(0.1 * SDcoeffs, harmonic=10)

    if plot:
        ax = spatial_efd.InitPlot()
        spatial_efd.PlotEllipse(ax, x_avg, y_avg, color="w", width=2.0)
        spatial_efd.PlotEllipse(ax, x_med, y_med, color="b", width=2.0)

        # Plot avg +/- 1 SD error ellipses
        spatial_efd.PlotEllipse(ax,
                                x_avg + x_sd,
                                y_avg + y_sd,
                                color="r",
                                width=1.0)
        spatial_efd.PlotEllipse(ax,
                                x_avg - x_sd,
                                y_avg - y_sd,
                                color="r",
                                width=1.0)

        i = 10
        plt.figure()
        ax = plt.gca()
        spatial_efd.plotComparison(
            ax,
            coeffs[i],
            10,
            cnts[i][:, 0],
            cnts[i][:, 1],
            color1="w",
            rotation=rots[i],
        )
        plt.show()
        plt.close("all")

    reduced_cnts = np.array(cnts)[a < percentile]

    return reduced_cnts, a < percentile