コード例 #1
0
          file=outfile)
    print(f"  \caption{{{caption}}}", file=outfile)
    print(f"  \label{{fig:{label}}}", file=outfile)
    print(f"\end{{figure*}}", file=outfile)


if len(sys.argv) < 5:
    print(f"USAGE: {sys.argv[0]} IMG SOM MAP TRANSFORM")

imgs = pu.ImageReader(sys.argv[1])
som = pu.SOM(sys.argv[2])
mapping = pu.Mapping(sys.argv[3])
trans = pu.Transform(sys.argv[4])
somset = pu.SOMSet(som, mapping, trans)

path = pu.PathHelper("Inspection")
outfile = open("latex_imgs.tex", "w")

### SOM 2D histogram ###
som_counts(somset, True)
plt.savefig(f"som_stats.png")
plt.clf()
caption = f"The total number of images within the {imgs.data.shape[0]} input images mapped to each neuron in the SOM."
latex_figure("som_stats.png", caption, "som_stats", outfile)
print("", file=outfile)
########################

### Euclidean Distance ###
dist_hist(somset, log=False)
plt.savefig(f"EucDist_hist_lin.png")
dist_hist(somset, log=True)
コード例 #2
0
def neuron_img_comp(somset, imgs, sampler, outpath=""):
    path = pu.PathHelper(outpath)
    logfile = open(f"{path.path}/info.txt", "w")

    bmu_ed = somset.mapping.bmu_ed()

    img_shape = imgs.data.shape[-1]
    neuron_shape = somset.som.neuron_shape[-1]
    b1 = (neuron_shape - img_shape) // 2
    b2 = b1 + img_shape

    for neuron, ind in enumerate(sampler.points):
        # Plot neuron
        somset.som.plot_neuron(ind)
        f1 = plt.gcf()
        plt.xticks([])
        plt.yticks([])

        radio_img = somset.som[ind][0]
        levels = np.linspace(0.25 * radio_img.max(), radio_img.max(), 4)
        f1.axes[1].contour(radio_img, levels=levels, colors="white", linewidths=0.5)

        f1.axes[0].set_xlim([b1, b2])
        f1.axes[0].set_ylim([b2, b1])
        for ax in f1.axes:
            ax.axvline(neuron_shape / 2, c="r", ls="--", lw=1)
            ax.axhline(neuron_shape / 2, c="r", ls="--", lw=1)

        f1.savefig(f"{path.path}/neuron_{neuron}.png")
        plt.close(f1)

        # Plot images
        matches = somset.mapping.images_with_bmu(ind)
        dist = bmu_ed[matches]
        idx1 = matches[np.argmin(dist)]
        idx2 = matches[np.argsort(dist)[len(matches) // 2]]
        for i, idx in enumerate([idx1, idx2]):
            plot_image(
                imgs,
                idx=idx,
                somset=somset,
                apply_transform=True,
                show_index=False,
                grid=True,
            )
            f2 = plt.gcf()
            plt.xticks([])
            plt.yticks([])
            for ax in f2.axes:
                ax.axvline(img_shape / 2, c="r", ls="--", lw=1)
                ax.axhline(img_shape / 2, c="r", ls="--", lw=1)

            bmu_idx = somset.mapping.bmu(idx)
            tkey = somset.transform.data[(idx, *bmu_idx)]
            radio_img = imgs.data[idx, 0]
            radio_img = pu.pink_spatial_transform(radio_img, tkey)
            levels = np.linspace(0.25 * radio_img.max(), radio_img.max(), 4)
            f2.axes[1].contour(radio_img, levels=levels, colors="white", linewidths=0.5)

            f2.savefig(f"{path.path}/neuron_{neuron}_img{i}.png")
            plt.close(f2)

        # Print to a log file
        print(f"Neuron {neuron}: {ind}", file=logfile)
        print(f"Number in neuron: {len(matches)}", file=logfile)
        print(f"img0 ind: {idx1}", file=logfile)
        print(f"img1 ind: {idx2}", file=logfile)
        print("------------------\n", file=logfile)
コード例 #3
0
ファイル: neuron_table.py プロジェクト: CIRADA-Tools/som
sns.scatterplot(data=neuron_cat[good_neurons],
                x="Area_ratio",
                y="Radio_area",
                hue="good")

candidates = neuron_cat.Neuron_ID[mask].values
choices = np.random.choice(candidates, 45, replace=False)
print(choices)
plt.imshow(img_positions(choices, som.som_shape[0]))

# from sklearn.cluster import KMeans
# km = KMeans(30).fit(list(candidates))
# choices = [tuple(ki) for ki in km.cluster_centers_.astype(int)]

path = pu.PathHelper("Zoo_revised")
for neuron in choices:
    plt.close("all")
    som.plot_neuron(neuron)
    plt.savefig(f"{path.path}/neuron_{neuron[0]}-{neuron[1]}.png")

doubles = [
    (0, 8),
    # (0, 28),
    (0, 29),
    # (1, 8),
    (4, 27),
    (6, 18),
    (28, 9),
    (30, 25),
    (31, 4),
コード例 #4
0
    )

    args = parser.parse_args()
    return args


if __name__ == "__main__":
    args = parse_args()

    # Initialize the sample DataFrame, ImageReader, and SOM
    # files = set_filenames(args.som_file, args.map_file, args.trans_file, args.outbase)

    # df = pd.read_csv(args.sample_file)
    imgs = pu.ImageReader(args.imbin_file)
    somset = init_somset(args.som_file, args.map_file, args.trans_file)
    path = pu.PathHelper(args.outpath)

    # Only keep the records that preprocessed successfully
    # df = df.loc[imgs.records].reset_index(drop=True)
    # df = update_component_catalogue(df, somset)

    som_counts(somset, show_counts=True)
    plt.savefig(f"{path}/bmu_frequency.png")
    # plot_image(imgs, df=df, somset=somset, show_bmu=True, apply_transform=True)

    # dist_hist(df, labels=True)
    # plt.savefig(f"{path}/euc_distance_hist.png")

    # dist_hist_2d(df, somset, bins=100, loglog=False)
    # plt.savefig(f"{path}/euc_distance_hist_2d.png", dpi=somset.som.som_shape[0] * 25)