Beispiel #1
0
def segmentPlane(filePath):
    # print('Input file path = {}'.format(filePath))
    if not os.path.exists(filePath):
        filePath = '\\\\MPUFS7\\data_mrcv\\45_DATA_HUMANS\\CHEST\\STUDIES\\2017_Eldridge_NLP_MET\\002\\Normoxia\\CS_WAVELET_20_FRAMES_LOWRES\\dat\\CORRECTED\\Aorta_MAG.npy'
        print('File not found, using default instead.')
    # print('File path = {}'.format(filePath))

    mag = np.load(filePath)
    cd = np.load(filePath.replace('_MAG', '_CD'))
    velNormal = np.load(filePath.replace('_MAG', '_velNormal'))
    meanVel = np.mean(velNormal, axis=2)

    seg = KMeans(n_clusters=2).fit(cd.reshape(cd.size,
                                              1)).labels_.reshape(mag.shape)

    (nx, ny) = cd.shape
    (i, j) = np.meshgrid(np.linspace(-1.0, 1.0, nx),
                         np.linspace(-1.0, 1.0, ny))
    d2 = np.sqrt(np.sqrt(i * i + j * j))

    aveD1 = np.mean(d2 * (seg == 0))
    aveD2 = np.mean(d2 * (seg == 1))
    if aveD1 < aveD2:
        seg = 1 - seg
    #
    # color1 = colorConverter.to_rgba('white')
    # color2 = colorConverter.to_rgba('orange')
    # cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap2',[color1,color2],256)
    # cmap2._init()
    # alphas = np.linspace(0, 0.2, cmap2.N+3)
    # cmap2._lut[:,-1] = alphas
    #
    # plt.figure()
    # plt.imshow(cd,cmap='gray')
    # plt.imshow(seg, cmap=cmap2)
    # plt.show()

    (a, b) = cv2.connectedComponents(seg.astype('uint8'))
    aveD = [0 for _ in range(a)]
    for reg in range(a):
        aveD[reg] = np.sum(d2 * (b == reg)) / np.sum(b == reg)

    seg = b == np.argmin(aveD)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
    seg = cv2.dilate(seg.astype('uint8'), kernel=kernel)

    # color1 = colorConverter.to_rgba('white')
    # color2 = colorConverter.to_rgba('orange')
    # cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap2',[color1,color2],256)
    # cmap2._init()
    # alphas = np.linspace(0, 0.2, cmap2.N+3)
    # cmap2._lut[:,-1] = alphas
    #
    # plt.figure()
    # plt.imshow(cd,cmap='gray')
    # plt.imshow(seg, cmap=cmap2)
    # plt.show()

    np.save(filePath.replace('_MAG', '_SEG'), seg)
Beispiel #2
0
    def clu(self,
            KTIMES,
            ITER=100,
            anchor_num=0,
            anchor_way="k-means++",
            graph_knn=0,
            graph_way="t_free",
            km_init="k-means++",
            a1=0,
            a2=0):

        if anchor_num == 0:
            anchor_num = int(min(self.N / 2, 1024))
        Anchor = self.get_anchor(anchor_num=anchor_num, anchor_way=anchor_way)

        if graph_knn == 0:
            graph_knn = np.minimum(2 * self.c_true, anchor_num)
        B = self.get_graph(graph_knn=graph_knn,
                           graph_way=graph_way,
                           Anchor=Anchor)

        if a1 == 0:
            a1 = int(np.maximum(self.N / 10 / self.c_true, 1))
        if a2 == 0:
            a2 = int(np.maximum(self.N / 10 / self.c_true, 1))

        n, m = B.shape
        # initialize P
        B_sp = sparse.csr_matrix(B)
        U, S, VH = svds(B_sp, k=self.c_true, which="LM")

        ret = np.zeros((KTIMES, n))
        for ktimes in range(KTIMES):

            p = KMeans(self.c_true,
                       init=km_init).fit(U[:, :self.c_true]).labels_

            V = VH.T
            q = KMeans(self.c_true,
                       init=km_init).fit(V[:, :self.c_true]).labels_

            p = opt(p.astype(np.int32),
                    q.astype(np.int32),
                    B,
                    self.c_true,
                    a1=a1,
                    a2=a2,
                    ITER=ITER)

            ret[ktimes, :] = p

        return ret
Beispiel #3
0
def recursive_clustering(data, verbose=True):
    # Split into 3 groups instead of 2 if weight between 2000 and 2800
    if data.Weight.sum() < 2950 and data.Weight.sum() > 1970:
        nc = 3
    else:
        nc = 2
    # Split into Clusters
    X = KMeans(n_clusters=nc).fit_predict(data[lat_long])
    # Record New Cluster
    data.Cluster = np.core.defchararray.add(
        np.array(data.Cluster).astype(str), X.astype(str))
    # Get total weights of clusters
    w = data.groupby('Cluster')['Weight'].sum()

    # If Weight is under 2000 then we'll split up those presents later
    if w.sum() > 980 * nc:
        # Loop through clusters
        for new_cluster in w.index:
            # Ignore Clusters with Weight under 1000 - They are done
            if w[new_cluster] > 1000:
                # Data for this Cluster is recursively Clustered
                mask = data.Cluster == new_cluster
                data.loc[mask] = recursive_clustering(data[mask])
    else:
        if verbose:
            print w
    return data
    def find_centers(self, X_train, num_centr):
        self.N, self.D = X_train.shape

        self.batch_size = self.N
        try:
            centers = FCV(X_train, n_clusters=num_centr, r=4).optimize()
            c = centers.C
        except:
            c = KMeans(n_clusters=num_centr, random_state=0).fit(X_train)
            c = c.cluster_centers_

        centroids = c.astype(np.float32)
        return centroids
Beispiel #5
0
def df_kmeans_clustering(ds, cluster_by_columns, cluster_rate=0.1):
    n_clusters = int(len(ds) * cluster_rate)
    if n_clusters < 1:
        n_clusters = 1
    print('n_ds:', len(ds))
    print('n_clusters:', n_clusters)
    
    _units = []
    for unit in cluster_by_columns:
        _units.append(ds[unit].tolist())
    cust_array = np.array(_units).T
    pred = KMeans(n_clusters=n_clusters).fit_predict(cust_array)
    return pred.astype(str)
def cluster_nba(df, d, clusters):
    x_scaled = scaler.fit_transform(d)
    pca = PCA(n_components=8)
    pca.fit(x_scaled)

    x_pca = pca.transform(x_scaled)

    kmeans = KMeans(n_clusters=clusters, random_state=2).fit_predict(x_pca)

    df['Cluster'] = kmeans.astype('str')
    df['Cluster_x'] = x_pca[:, 0]
    df['Cluster_y'] = x_pca[:, 1]

    df = df.sort_values('Cluster', ascending=True)
    return df
                # starttime = time.time()
                # get points ##################################
                points = np.empty((3, img.size))
                points[0, :] = (pixel[0, :]+0.5-u0)*pixel[2, :]/f_x
                points[1, :] = (pixel[1, :]+0.5-v0)*pixel[2, :]/f_y
                points[2, :] = pixel[2, :]
                points = points.T
                # print(time.time() - starttime)
                # print(offset)
                # print(skel_camcoords)
                # randInidices = np.arange(len(points))
                # np.random.shuffle(randInidices)
                # points = points[randInidices[:400], :]
                label = KMeans(n_clusters=2, init=np.reshape([1000, np.mean(points[:, 2])], [-1, 1])).fit_predict(points[:, 2].reshape(-1, 1))
                temp = points[label.astype(bool)]
                offset[2] = np.mean(temp[:, 2])
                # fig = plt.figure()
                # ax = Axes3D(fig)
                # ax.set_xlabel('X', fontdict={'size': 15, 'color': 'red'})
                # ax.set_ylabel('Y', fontdict={'size': 15, 'color': 'red'})
                # ax.set_zlabel('Z', fontdict={'size': 15, 'color': 'red'})
                #
                # ax.view_init(-90, -90)
                # color = ('red', 'green')
                # colors = np.array(color)[label]
                #
                # ax.scatter(points[:, 0], points[:, 1], points[:, 2], c=colors)
                # plt.show()
                _, points, skel_camcoords = viewCorrection(np.mean(skel_camcoords, axis=0), points, skel_camcoords)
Beispiel #8
0
                tl_v = np.maximum(np.floor(np.min(skel_proj[:, 1])), 0)
                br_u = np.minimum(np.floor(np.max(skel_proj[:, 0])), 639)
                br_v = np.minimum(np.floor(np.max(skel_proj[:, 1])), 479)
                img_cropped = img[int(tl_v):int(br_v), int(tl_u):int(br_u)]

                # get 3D center (center of bounding box + centerDepth --> transformed to 3D camera cooordinates)
                img_cropped[np.where(
                    img_cropped == 0)] = np.max(img_cropped) + 1
                depths = img_cropped.reshape(-1, 1)
                label = KMeans(
                    n_clusters=2,
                    init=np.reshape(
                        [np.max(img_cropped) + 1,
                         np.min(img_cropped)], [-1, 1]),
                    n_init=1).fit_predict(depths)
                centerDepth = np.mean(depths[label.astype(bool)])
                if np.abs(centerDepth - skel_camcoords[3, 2]) > 100:
                    centerDepth = skel_camcoords[3, 2]

                center3D = np.zeros(3)
                center3D[0] = (0.5 *
                               (tl_u + br_u) + 0.5 - u0) * centerDepth / f_x
                center3D[1] = (0.5 *
                               (tl_v + br_v) + 0.5 - v0) * centerDepth / f_y
                center3D[2] = centerDepth

                cropStart = np.zeros(2)
                cropStart[0] = (center3D[0] -
                                cropSizePlus * 1.41) * f_x / center3D[2] + u0
                cropStart[1] = (center3D[1] -
                                cropSizePlus * 1.41) * f_y / center3D[2] + v0
Beispiel #9
0
# If projection is used project also the data to construct the LDA metric
if with_projection:
    X_data_lda = ((X_data_lda - b_prime.T) @ A_prime)  # N x D -> N x d'
D_lda = X_data_lda.shape[1]

N_centers = 1000  # Number of base points to learn LDA metrics

# This constructs a 'grid' for the latent codes
# Construct a uniform distribution to cover the latent codes and then use k-means on that.
Uniform_data = MU_Z_data.min(0) + (MU_Z_data.max(0) -
                                   MU_Z_data.min(0)) * np.random.rand(4000, 2)
X_centers = KMeans(n_clusters=N_centers,
                   max_iter=1000).fit(Uniform_data).cluster_centers_
with torch.no_grad():
    X_centers = VAE_model.decode(torch.from_numpy(X_centers.astype(
        np.float32)))[0].detach().numpy()
if with_projection:
    X_centers = ((X_centers - b_prime.T) @ A_prime)  # N x D -> N x d'

# The number of kNN for each center to use their labels for the LDA metric.
N_knn = 50

N_classes_lda = np.unique(labels).shape[0]
Class_labels_lda = np.unique(labels)
Metrics_lda = np.eye(D_lda)
Metrics_lda = Metrics_lda[None, :].repeat(N_centers, axis=0)  # N x D x D
sigmas_per_center = np.zeros((N_centers, 1))
epsilon_reg_lda = 0.001  # The epsilon to avoid degenerated metrics

# Construct for each center center the local LDA metric using the kNN's labels
for n in range(50):
Beispiel #10
0
    group_label = KMeans(n_clusters=cent_num, random_state=rs).fit_predict(total_data[:,:2])

    # random sample
    rand_group_label = KMeans(n_clusters=cent_num, random_state=rs).fit_predict(rand_total_data[:,:2])

    idx_1 = group_label >= 0
    idx_2 = group_label < cent_num
    idx_label = idx_1 & idx_2
    if idx_label.sum() != total_num:
        print("KMeans wrong!!! ",idx_label.sum(),total_num)

    t2 = time.time()

    h5f = h5py.File(stack_file_path, "w")
    h5f["/data"] = total_data
    h5f["/group_label"] = group_label.astype(dtype=numpy.intc)
    h5f.close()

    # random sample
    h5f = h5py.File(rand_stack_file_path, "w")
    h5f["/data"] = rand_total_data
    h5f["/group_label"] = rand_group_label.astype(dtype=numpy.intc)
    h5f.close()

    print("Time: %.2f sec. %d galaxies"%(t2-t1, total_num))
comm.Barrier()

if rank > 0:
    h5f = h5py.File(stack_file_path, "r")
    total_data = h5f["/data"][()]
    group_label = h5f["/group_label"][()]
Beispiel #11
0
                      0, ith_cluster_silhouette_values,
                      facecolor=color, edgecolor=color, alpha=0.7)

    # Compute the new y_lower for next plot
    y_lower = y_upper + 10  # 10 for the 0 samples

ax1.set_title("The silhouette plots")

# The vertical line for average silhoutte score of all the values
ax1.axvline(x=silhouette_avg, color="red", linestyle="--")

ax1.set_yticks([])  # Clear the yaxis labels / ticks
ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])

# 2nd Plot showing the actual clusters formed
colors = cm.spectral(y_pred.astype(float) / n_clusters)
ax2.scatter(X[:, 0], X[:, 1], marker='.', s=30, lw=0, alpha=0.7,
            c=colors)

ax2.set_title("n = 2 Clusters")
ax2.xaxis.set_visible(False)
ax2.yaxis.set_visible(False)

y_pred = KMeans(n_clusters=3).fit_predict(X)
ax4 = fig.add_subplot(224)
silhouette_avg = silhouette_score(X, y_pred)
print("For n_clusters = 3 The average silhouette_score is :", silhouette_avg)
sample_silhouette_values = silhouette_samples(X, y_pred)

y_lower = 10
n_clusters = 3