Exemple #1
0
def tsne(df_anim, ndim):
    """ Takes a df of animation, reduces the 17d to ndim dimensions """

    joints_values = df_anim.loc[:, joints_names].values
    X = np.array(joints_values)

    X_embedded = TSNE(n_components=ndim,
                      perplexity=100.0,
                      learning_rate=30,
                      n_iter=50000,
                      init='random',
                      random_state=4).fit_transform(X)

    return pd.Series(X_embedded.reshape([
        100,
    ]))
Exemple #2
0
def cluster(features, num_objs, n_clusters, save_path):
    name = 'features'
    centers = {}
    for label in range(num_objs):
        feat = features[label]
        if feat.shape[0]:
            n_feat_clusters = min(feat.shape[0], n_clusters)
            if n_feat_clusters < n_clusters:
                print(label)
            kmeans = KMeans(n_clusters=n_feat_clusters, random_state=0).fit(feat)
            if n_feat_clusters == 1:
                centers[label] = kmeans.cluster_centers_
            else:
                one_dimension_centers = TSNE(n_components=1).fit_transform(kmeans.cluster_centers_)
                args = np.argsort(one_dimension_centers.reshape(-1))
                centers[label] = kmeans.cluster_centers_[args]
    save_name = os.path.join(save_path, name + '_clustered_%03d.npy' % n_clusters)
    np.save(save_name, centers)
    print('saving to %s' % save_name)
Exemple #3
0
    S = np.array([None]*df.shape[0])
    for i in range(0, len(df)):
        S[i] = df.samples[i][int(0.5 + df.cfd_trig_rise[i]/1000)-20: int(0.5 + df.cfd_trig_rise[i]/1000)+waveFormLegth-20].astype(np.float64)
    return S
St = get_samples(df_test)
x_test=np.stack(St)#df_test.samples)
x_test=x_test.reshape(len(x_test), window_width, 1)

model_path='weights-improvement-01-0.79.hdf5'#model_zero.hdf5'
model=keras.models.load_model('../CNN_models/%s'%model_path)
predictions = model.predict(x_test)
df_test['pred'] = predictions

int_layer_model = Model(inputs=model.input, outputs=model.get_layer('flat').output)
params = int_layer_model.predict(x_test)
params = StandardScaler().fit_transform(params)

X_embedded = TSNE(n_components=2, init='pca', n_iter=1000, verbose=2, perplexity=20, method='barnes_hut', learning_rate=10, early_exaggeration=2).fit_transform(params)

# pca = PCA(n_components=30)
# pca.fit(params)
# print(pca.explained_variance_ratio_)
# print(pca.explained_variance_ratio_.sum())
# params_new = pca.transform(params)
# params_new = params_new.reshape(params_new.shape[1], params_new.shape[0])
X1=X_embedded.reshape(X_embedded.shape[1], X_embedded.shape[0])[0]
X2=X_embedded.reshape(X_embedded.shape[1], X_embedded.shape[0])[1]
plt.scatter(X1, X2, c=df_test.pred, cmap='viridis')
plt.colorbar()
plt.show()
Exemple #4
0
        columns = batch_size // rows if batch_size // rows < 8 else 8
    else:
        rows = 2
        columns = 2

    span = np.linspace(-4, 4, rows)
    grid = np.dstack(np.meshgrid(span, span)).reshape(-1, 2)

    # If you want to use a dimensionality reduction method you can use
    # for example PCA by projecting on two principal dimensions
    """ PCA """
    # z = PCA(n_components=2).fit_transform(z.reshape(-1,latent_features))
    # z = z.reshape(batch_size,num_samples,2)
    """ TSNE """
    print(z.shape)
    z = TSNE(n_components=2).fit_transform(z.reshape(-1, latent_features))
    z = z.reshape(batch_size, num_samples**2, 2)
    # z = z[:,1,] # We do only want to plot one z instead of |num_samples|

    colors = iter(plt.get_cmap('Set1')(np.linspace(0, 1.0, len(classes))))
    for c in classes:
        ax.scatter(*z[c == y.numpy()].reshape(-1, 2).T,
                   c=next(colors),
                   marker='o',
                   label=c)

    if show_sampling_points:
        ax.scatter(*grid.T,
                   color="k",
                   marker="x",
                   alpha=0.5,
    imzml = imzmlio.open_imzml(inputname)
    spectra = imzmlio.get_full_spectra(imzml)
    max_x = max(imzml.coordinates, key=lambda item: item[0])[0]
    max_y = max(imzml.coordinates, key=lambda item: item[1])[1]
    max_z = max(imzml.coordinates, key=lambda item: item[2])[2]
    image = imzmlio.get_images_from_spectra(spectra, (max_x, max_y, max_z))
    mzs, intensities = imzml.getspectrum(0)
else:
    image = sitk.GetArrayFromImage(sitk.ReadImage(inputname)).T
    mzs = [i for i in range(image.shape[2])]
    mzs = np.asarray(mzs)

image = image[..., mzs >= threshold]
print(image.shape)
mzs = mzs[mzs >= threshold]
mzs = np.around(mzs, decimals=2)
mzs = mzs.astype(str)

shape = image.shape[:-1]

image_norm = imzmlio.normalize(image)
image_norm = fusion.flatten(image_norm, is_spectral=True).T

n = 3
X_tsne = TSNE(n_components=n, random_state=0).fit_transform(image_norm)
X_tsne = imzmlio.normalize(X_tsne)
X_tsne = X_tsne.reshape(shape + (3, ))

tsne_itk = sitk.GetImageFromArray(X_tsne, isVector=True)
sitk.WriteImage(tsne_itk, outputname)