def GPGL2_seg(data, current_sample_seg): data = data + np.random.rand(len(data), len(data[0])) * 1e-6 dist_mat = kmeans_solver.fit_transform(data) node_top, labels = graph_cut(data, dist_mat, NUM_POINTS, NUM_CUTS) aij_mat = fun_graph_cosntruct(node_top) # aij_mat = squareform(pdist(node_top),checks=False) H = nx.from_numpy_matrix(aij_mat) pos_spring = nx.spring_layout(H) pos_spring = np.array([pos for idx, pos in sorted(pos_spring.items())]) # pos_spring = np.array(forceatlas2.forceatlas2(aij_mat, None, 10)) pos = fun_GPGL_layout_push(pos_spring, SIZE_SUB) pos_top = fun_GPGL_layout_push(pos_spring, SIZE_TOP) ##%% pos_cuts = [] for i_cut in range(NUM_CUTS): pos_cut_3D = data[labels == i_cut, :] if (len(pos_cut_3D) < 5): pos_raw = [[0, 0], [0, 1], [1, 1], [1, 0]] pos = pos_raw[:len(pos_cut_3D)] # H = nx.from_numpy_matrix(np.ones([len(pos_cut_3D),len(pos_cut_3D)])) pos_cuts.append(pos) continue aij_mat = fun_graph_cosntruct(pos_cut_3D) # aij_mat = squareform(pdist(pos_cut_3D),checks=False) H = nx.from_numpy_matrix(aij_mat) pos_spring = nx.spring_layout(H) pos_spring = np.array([pos for idx, pos in sorted(pos_spring.items())]) # pos_spring = np.array(forceatlas2.forceatlas2(aij_mat, None, 10)) pos = fun_GPGL_layout_push(pos_spring, SIZE_SUB) pos_cuts.append(pos) ##%% combine all layout positions cuts_count = np.zeros(NUM_CUTS).astype(np.int) pos_all = [] for idx in range(NUM_POINTS): label = labels[idx] pos_all.append(pos_cuts[label][cuts_count[label]] + pos_top[label] * SIZE_SUB) cuts_count[label] += 1 pos_all = np.array(pos_all) ##%% assign all features into the grid map mat = np.zeros([SIZE_SUB * SIZE_TOP, SIZE_SUB * SIZE_TOP, 3]) seg = np.zeros([SIZE_SUB * SIZE_TOP, SIZE_SUB * SIZE_TOP, 51]) seg[:, :, -1] = 1 for data1, seg1, pos in zip(data, current_sample_seg, pos_all): mat[pos[0], pos[1]] = data1 seg[pos[0], pos[1], int(seg1)] = 1 seg[pos[0], pos[1], -1] = 0 return mat, seg
def GPGL2_seg(data, current_sample_seg): data = data + np.random.rand(len(data), len(data[0])) * 1e-6 dist_mat = kmeans_solver.fit_transform(data) node_top, labels = graph_cut(data, dist_mat, NUM_POINTS, NUM_CUTS) aij_mat = squareform(pdist(node_top), checks=False) H = nx.from_numpy_matrix(aij_mat) pos_spring = nx.spring_layout(H) pos_spring = np.array([pos for idx, pos in sorted(pos_spring.items())]) pos = fun_GPGL_layout_push(pos_spring, SIZE_SUB) pos_top = fun_GPGL_layout_push(pos_spring, SIZE_TOP) ##%% pos_cuts = [] for i_cut in range(NUM_CUTS): pos_cut_3D = data[labels == i_cut, :] if (len(pos_cut_3D) < 5): pos_raw = [[0, 0], [0, 1], [1, 1], [1, 0]] pos = pos_raw[:len(pos_cut_3D)] pos_cuts.append(pos) continue aij_mat = squareform(pdist(pos_cut_3D), checks=False) H = nx.from_numpy_matrix(aij_mat) pos_spring = nx.spring_layout(H) pos_spring = np.array([pos for idx, pos in sorted(pos_spring.items())]) pos = fun_GPGL_layout_push(pos_spring, SIZE_SUB) pos_cuts.append(pos) ##%% combine all layout positions cuts_count = np.zeros(NUM_CUTS).astype(np.int) pos_all = [] for idx in range(NUM_POINTS): label = labels[idx] pos_all.append(pos_cuts[label][cuts_count[label]] + pos_top[label] * SIZE_SUB) cuts_count[label] += 1 pos_all = np.array(pos_all) num_nodes_m = len(np.unique(pos_all, axis=0)) node_loss_rate = (1 - num_nodes_m / NUM_POINTS) return pos_all, node_loss_rate