def __init__(self, data_dir, dataset_name, batch_size, seq_length): sensor_locations_fname = os.path.join(data_dir, dataset_name, 'amd_master.tsv') train_fname = os.path.join(data_dir, dataset_name, "train.csv")# the train output file path test_fname = os.path.join(data_dir, dataset_name, "test.csv") # the test output file path val_fname = os.path.join(data_dir, dataset_name, "val.csv") # the validation output file path # load the data into DataFrames train_df = pd.read_csv(train_fname, index_col="datetime", parse_dates=["datetime"]) test_df = pd.read_csv(test_fname, index_col="datetime", parse_dates=["datetime"]) val_df = pd.read_csv(val_fname, index_col="datetime", parse_dates=["datetime"]) # get the sensor locations sensor_locations = pd.read_csv(sensor_locations_fname, delimiter="\t", usecols=["aid", "lat1", "lng1", "alt"]) sensor_locations = sensor_locations.loc[sensor_locations.aid.isin([int(x) for x in list(train_df)]),:].drop("aid", axis=1) num_sensors = sensor_locations.shape[0] # construct an adjacency matrix out of the sensors' locations dist, idx = distance_scipy_spatial(sensor_locations, k=8) adj = adjacency(dist, idx) # convert the dataframes into numpy arrays data = [train_df.values, val_df.values, test_df.values] for d in data: print(d.shape) self.sizes = [] self.all_batches = [] self.all_data = data self.adj = adj print("Reshaping tensors...") for split, data in enumerate(data): # split = 0:train, 1:valid, 2:test #Cutting training sample for check profile fast..(Temporal) #if split==0: # #Only for training set length = data.shape[0] # data = data[:int(length/4)] # temperature_part = data[:,:-4] # sensor's temperature # seasonal_part = data[:,-4:] # seasonal data scaler = StandardScaler(copy=False) scaler.fit_transform(data) # scale the sensor's temperature part num_features = data.shape[1] # seasonal_part = np.array([seasonal_part]).repeat(num_features, axis=1).reshape([length, 4 * num_features]) # repeat the seasonal part # data = np.concatenate([temperature_part, seasonal_part], axis=1) ydata = np.zeros_like(temperature_part) ydata[:-1] = temperature_part[1:].copy() ydata[-1] = temperature_part[0].copy() x_batches = list(data.reshape([-1, batch_size, data.shape[1] * seq_length])) y_batches = list(ydata.reshape([-1, batch_size, ydata.shape[1] * seq_length])) self.sizes.append(len(x_batches)) self.all_batches.append([x_batches, y_batches]) self.batch_idx = [0, 0, 0] print("data load done. Number of batches in train: %d, val: %d, test: %d" \ % (self.sizes[0], self.sizes[1], self.sizes[2]))
def grid_graph(m): z = graph.grid(m) # normalized nodes coordinates dist, idx = graph.distance_sklearn_metrics(z, k=number_edges, metric=metric) # dist contains the distance of the 8 nearest neighbors for each node indicated in z sorted in ascending order # idx contains the indexes of the 8 nearest for each node sorted in ascending order by distance A = graph.adjacency(dist, idx) # graph.adjacency() builds a sparse matrix out of the identified edges computing similarities as: A_{ij} = e^(-dist_{ij}^2/sigma^2) return A, z
def __init__(self, env, params): # Communication graph self.graph = params["Graph"] self.P = self.graph["P"] self.root = self.graph["root"] self.M = self.graph["M"] self.comms = adjacency(self.P, self.graph["comms"]) self.capacity = torch.tensor(self.graph["capacity"], dtype=torch.long).detach() # print("[Agent] Comms:") # print(self.comms) # print("[Agent] Capacity:") # print(self.capacity) # Configuration parameters self.config = params["Config"] self.rw_type = self.config["reward_type"] self.baseline_type = self.config["Baseline"] self.verbose = self.config["verbose"] # Verbosity self.verbosity_int = self.config["verbosity_interval"] # Hyperparameters self.hyperparams = params["Hyperparameters"] self.gamma = self.hyperparams["gamma"] # Discounted factor self.alpha = self.hyperparams["alpha"] # Learning rate self.n_episodes = self.hyperparams["n_episodes"] self.K = self.hyperparams["K"] # Num. samples # Store loss and episode length evolution self.episode = 0 self.t = 0 self.env = env # Optimization on GPUs if torch.cuda.is_available(): self.device = torch.device('cuda') else: self.device = torch.device('cpu') # Parametrized Policy network self.policy = PolicyNetwork(params).to(self.device) print("Printing the policy", self.policy) self.optimizer = torch.optim.Adam(self.policy.parameters(), lr=self.alpha) # Information in each episode self.info = {}
def grid_graph(m, corners=False): z = graph.grid(m) dist, idx = graph.distance_sklearn_metrics(z, k=FLAGS.number_edges, metric=FLAGS.metric) A = graph.adjacency(dist, idx) # Connections are only vertical or horizontal on the grid. # Corner vertices are connected to 2 neightbors only. if corners: import scipy.sparse A = A.toarray() A[A < A.max()/1.5] = 0 A = scipy.sparse.csr_matrix(A) print('{} edges'.format(A.nnz)) print("{} > {} edges".format(A.nnz//2, FLAGS.number_edges*m**2//2)) return A
'_split')[0] + '.pt' if not os.path.exists(SavePath): os.mkdir(SavePath) if 'adj' not in vars(): # Get the graph structure X = 0 for f in files: X += cross_val.Eloadmat(f, ['static_R'])[0] X = X / len(files) mean_connec = np.squeeze(cross_val.triu2mat(X)) dist, idx = graph.distance_lshforest(mean_connec, k=10, metric='cosine') adj = graph.adjacency(dist, idx).astype(np.float32) adj = graph.rescale_adj(adj) labels = [] for f in files: tmp = cross_val.Eloadmat(f, ['label']) labels.extend(tmp[0]) labels = np.stack(labels, 0) # for batch nodes max_num_nodes = adj.shape[0] foldnum = 10 all_vals = [] all_val_pred = [] all_val_subid = [] all_val_labels = []
def prepare_graphs(): p = os.path.join(os.getcwd(), '../survivalnet/data/Brain_Integ.mat') D = sio.loadmat(p) T = np.asarray([t[0] for t in D['Survival']]) O = 1 - np.asarray([c[0] for c in D['Censored']]) X = D['Integ_X'] #[:,1855:] X = (X - np.mean(X, axis=0)) / np.std(X, axis=0) fold_size = int(10 * len(X) / 100) X_train, T_train, O_train = X[2 * fold_size:], T[2 * fold_size:], O[2 * fold_size:] X_test, T_test, O_test = X[:fold_size], T[:fold_size], O[:fold_size] X_val, T_val, O_val = X[fold_size:2 * fold_size], T[fold_size:2 * fold_size], O[fold_size:2 * fold_size] print_log('train and test shapes:' + str(X_train.shape) + str(X_test.shape)) if not LOAD_A: start = time.time() dist, idx = graph.distance_scipy_spatial(X_train.T, k=4, metric='euclidean') print_log('graph constructed:' + str(dist.shape) + str(idx.shape) + ' in ' + str(time.time() - start)) A = graph.adjacency(dist, idx).astype(np.float32) d = X.shape[1] assert A.shape == (d, d) np.savez('A_sml', data=A.data, indices=A.indices, indptr=A.indptr, shape=A.shape) print('d = |V| = {}, k|V| < |E| = {}'.format(d, A.nnz)) #plt.spy(A, markersize=2, color='black'); #plt.savefig('tmp.png') else: start = time.time() loader = np.load('A.npz') A = csr_matrix((loader['data'], loader['indices'], loader['indptr']), shape=loader['shape']) print_log('graph loaded:' + ' in ' + str(time.time() - start)) print('adjacency matrix type and shape: ', A.__class__, A.shape) start = time.time() graphs, perm = coarsening.coarsen(A, levels=CL, self_connections=False) print_log('graph coarsened:' + ' in ' + str(time.time() - start)) X_train = coarsening.perm_data(X_train, perm) X_val = coarsening.perm_data(X_val, perm) X_test = coarsening.perm_data(X_test, perm) print_log('train and test shapes:' + str(X_train.shape) + str(X_test.shape)) L = [graph.laplacian(A, normalized=True) for A in graphs] #graph.plot_spectrum(L) n_train = len(X_train) params = dict() params['dir_name'] = 'demo' params['num_epochs'] = 2000 params['batch_size'] = int(len(X_train) / 1.0) params['eval_frequency'] = 10 # Building blocks. params['filter'] = 'chebyshev5' params['brelu'] = 'b1relu' params['pool'] = 'apool1' # Architecture. params['F'] = [8, 8, 8] # Number of graph convolutional filters. params['K'] = [9, 9, 9] # Polynomial orders. params['p'] = [2, 2, 2] # Pooling sizes. params['M'] = [128, 1] # Output dimensionality of fully connected layers. # Optimization. params['regularization'] = 0 params['dropout'] = 1 params['learning_rate'] = 1e-4 params['decay_rate'] = 0.999 params['momentum'] = 0 params['decay_steps'] = n_train / params['batch_size'] model = models.cgcnn(L, **params) accuracy, loss, t_step = model.cox_fit(X_train, T_train, O_train, X_val, T_val, O_val)
def train(method, view_com, n_views, k, m, n_epoch, batch_size, pairs, labels, coords, subj, data, data_type, i_fold): str_params = view_com + '_k' + str(k) + '_m' + str(m) + '_' obj_params = 'softmax' print(str_params) print('Construct ROI graphs...') t_start = time.process_time() # A = grid_graph(86, corners=False) # A = graph.replace_random_edges(A, 0) coo1, coo2, coo3 = coords.shape # coo2 is the roi dimension features = np.zeros([coo1 * coo3, coo2]) for i in range(coo3): features[coo1 * i:coo1 * (i + 1), :] = coords[:, :, i] dist, idx = graph.distance_scipy_spatial(np.transpose(features), k=10, metric='euclidean') A = graph.adjacency(dist, idx).astype(np.float32) if method == '2gcn': graphs, perm = coarsening.coarsen(A, levels=FLAGS.coarsening_levels, self_connections=False) L = [graph.laplacian(A, normalized=True) for A in graphs] data = coarsening.perm_data1(data, perm, True) else: graphs = list() graphs.append(A) L = [graph.laplacian(A, normalized=True)] print('Execution time: {:.2f}s'.format(time.process_time() - t_start)) # graph.plot_spectrum(L) del A print('Set parameters...') mp = model_perf() # Architecture. common = {} common['dir_name'] = 'ppmi/' common['num_epochs'] = n_epoch common['batch_size'] = batch_size common['eval_frequency'] = 5 * common['num_epochs'] common['patience'] = 5 common['regularization'] = 5e-3 common['dropout'] = 1 common['learning_rate'] = 1e-2 common['decay_rate'] = 0.95 common['momentum'] = 0.9 common['n_views'] = n_views common['view_com'] = view_com # common['brelu'] = 'b1relu' # common['pool'] = 'mpool1' print('Get feed pairs and labels...') train_pairs, train_y, val_x, val_y, test_pairs, test_y = get_feed_data( data, subj, pairs, labels, method) C = max(train_y) + 1 common['decay_steps'] = train_pairs.shape[0] / (common['batch_size'] * 5) if method == 'fnn': str_params += 'siamese_' name = 'mvfnn' params = common.copy() params['method'] = 'fnn' params['fin'] = 1 params['F'] = [m] params['K'] = [1] params['p'] = [1] params['M'] = [C] params['dir_name'] += name mp.test(models.siamese_fnn(L, **params), name, params, data, train_pairs, train_y, val_x, val_y, test_pairs, test_y) if method == '2fnn': str_params += 'siamese_layer2_' name = 'mvfnn2' params = common.copy() params['method'] = 'fnn' params['fin'] = 1 params['F'] = [m] params['K'] = [1] params['p'] = [1] params['M'] = [64, C] params['dir_name'] += name mp.test(models.siamese_fnn(L, **params), name, params, data, train_pairs, train_y, val_x, val_y, test_pairs, test_y) if method == 'gcn': # str_params += 'b_max_eu_' name = 'mvgcn' params = common.copy() params['method'] = 'gcn' params['F'] = [m] # filters params['K'] = [k] # supports params['p'] = [1] params['M'] = [C] params['fin'] = val_x.shape[3] params['dir_name'] += name params['filter'] = 'chebyshev5' params['brelu'] = 'b2relu' params['pool'] = 'apool1' mp.test(models.siamese_m_cgcnn(L, **params), name, params, data, train_pairs, train_y, val_x, val_y, test_pairs, test_y) # Common hyper-parameters for LeNet5-like networks. if method == '2gcn': str_params += 'p4_fc64_' name = 'mvgcn2' params = common.copy() params['method'] = '2gcn' params['F'] = [m, 64] # filters params['K'] = [k, k] # supports params['p'] = [4, 4] params['M'] = [512, C] params['fin'] = val_x.shape[3] params['dir_name'] += name params['filter'] = 'chebyshev5' params['brelu'] = 'b2relu' params['pool'] = 'apool1' mp.test(models.siamese_m_cgcnn(L, **params), name, params, data, train_pairs, train_y, val_x, val_y, test_pairs, test_y) # mp.save(data_type) method_type = method + '_' mp.fin_result(method_type + data_type + str_params + obj_params, i_fold)
def __init__(self, params): super(MPIMapEnv, self).__init__() self.params = params # Communication graph self.graph = params["Graph"] self.P = self.graph["P"] self.root = self.graph["root"] self.M = self.graph["M"] self.comms = adjacency(self.P, self.graph["comms"]) # Configuration parameters self.config = params["Config"] self.rw_type = self.config["reward_type"] # # self.t = 0 # Communication matrix: It represents the communications of the # application and it is an input to this algorithm. # TBD: read it from a file # comms = np.zeros((self.P, self.P), dtype=np.int) # comms[0,0] = 1 # comms[0,1] = 2 # comms[1,3] = 3 # comms[0,2] = 3 # comms[0,4] = 4 # comms[1,5] = 4 # comms[2,6] = 4 # comms[3,7] = 4 """ comms[0,8] = 5 comms[1,9] = 5 comms[2,10] = 5 comms[3,11] = 5 comms[4,12] = 5 comms[5,13] = 5 comms[6,14] = 5 comms[7,15] = 5 comms[0,16] = 6 comms[1,17] = 6 comms[2,18] = 6 comms[3,19] = 6 comms[4,20] = 6 comms[5,21] = 6 comms[6,22] = 6 comms[7,23] = 6 comms[8,24] = 6 comms[9,25] = 6 comms[10,26] = 6 comms[11,27] = 6 comms[12,28] = 6 comms[13,29] = 6 comms[14,30] = 6 comms[15,31] = 6 """ # self.comms = comms # Optimization on GPUs if torch.cuda.is_available(): self.device = torch.device('cuda') else: self.device = torch.device('cpu') # self.A = torch.FloatTensor(comms).detach().to(self.device) # self.A[self.A > 0] = self.m # print(self.A) """
def __init__(self, agent, env, params): # Communication graph self.graph = params["Graph"] self.P = self.graph["P"] self.root = self.graph["root"] self.M = self.graph["M"] self.node_names = self.graph["node_names"] self.comms = adjacency(self.P, self.graph["comms"]) # Configuration parameters self.config = params["Config"] self.rw_type = self.config["reward_type"] self.baseline_type = self.config["Baseline"] self.verbose = self.config["verbose"] # Verbosity self.verbosity_int = self.config["verbosity_interval"] # Hyperparameters self.hyperparams = params["Hyperparameters"] self.gamma = self.hyperparams["gamma"] # Discounted factor self.alpha = self.hyperparams["alpha"] # Learning rate self.n_episodes = self.hyperparams["n_episodes"] self.K = self.hyperparams["K"] # Num. samples # Output config. self.output = params["Output"] output_file = self.output["output_file"] # Other variables self.J_history = [] self.R_history = [] self.T_history = [] self.agent = agent self.env = env # Write header to file self.f = open(output_file, "w") self.f.write("#P: " + str(self.P) + "\n") self.f.write("#M: " + str(self.M) + "\n") self.f.write("#alpha: " + str(self.alpha) + "\n") self.f.write("#gamma: " + str(self.gamma) + "\n") self.f.write("#n_episodes: " + str(self.n_episodes) + "\n") self.f.write("#K: " + str(self.K) + "\n") self.f.write("#Baseline: " + str(self.baseline_type) + "\n") self.f.write("#Node names: " + str(self.node_names) + "\n") # f.write("#Processors/Node: " + str(penv["nodes_procs"]) + "\n") self.f.write("#Reward_type: " + str(self.rw_type) + "\n") # f.write("#StateRep: " + str(penv["state_rep"]) + "\n") self.f.write("#StartTime: " + str(time.time()) + "\n") try: slurm_job_id = os.environ['SLURM_JOB_ID'] slurm_job_id = "slurm-" + slurm_job_id + ".txt" slurm_nodelist = os.environ['SLURM_NODELIST'] except: slurm_job_id = '0' slurm_nodelist = 'local' self.f.write("#slurm file: " + str(slurm_job_id) + "\n") self.f.write("#node list: " + str(slurm_nodelist) + "\n") self.f.write("# \n") self.f.write( "# e \t J \t time \t T \t reward \t baseline \t Actions \n" ) self.f.write( "#----- \t ----- \t ------ \t ----- \t ------ \t -------- \t ------- \n" )
def train(modality, method, data_type, distance, k, fdim, nhops, mem_size, code_size, n_words, edim, n_epoch, batch_size, pairs, labels, coords, data, records, i_fold): str_params = '_' + modality + '_' + distance + '_k' + str(k) + '_fdim' + str(fdim) + '_nhops' + str(nhops) + '_memsize' + str(mem_size) + '_codesize' + str(code_size) + '_nwords' + str(n_words) + '_edim' + str(edim) print ('Construct ROI graphs...') t_start = time.process_time() coo1, coo2, coo3 = coords.shape # coo2 is the roi dimension features = np.zeros([coo1*coo3, coo2]) for i in range(coo3): features[coo1*i:coo1*(i+1), :] = coords[:, :, i] dist, idx = graph.distance_scipy_spatial(np.transpose(features), k=10, metric='euclidean') A = graph.adjacency(dist, idx).astype(np.float32) if method == '2gcn': graphs, perm = coarsening.coarsen(A, levels=FLAGS.coarsening_levels, self_connections=False) L = [graph.laplacian(A, normalized=True) for A in graphs] data = coarsening.perm_data1(data, perm) else: graphs = list() graphs.append(A) L = [graph.laplacian(A, normalized=True)] print ('The number of GCN layers: ', len(L)) print('Execution time: {:.2f}s'.format(time.process_time() - t_start)) # graph.plot_spectrum(L) del A print ('Set parameters...') mp = model_perf(i_fold, get_pair_label(pairs, labels)) # Architecture. common = {} common['dir_name'] = 'ppmi/' common['num_epochs'] = n_epoch common['batch_size'] = batch_size common['eval_frequency'] = 5 * common['num_epochs'] common['patience'] = 5 common['regularization'] = 1e-2 common['dropout'] = 1 common['learning_rate'] = 5e-3 common['decay_rate'] = 0.95 common['momentum'] = 0.9 common['init_std'] = 5e-2 print ('Get feed pairs and labels...') train_pairs, val_pairs, test_pairs = pairs train_x, train_y, val_x, val_y, test_x, test_y = get_feed_data(data, pairs, labels, method) train_r, val_r, test_r = get_feed_records(records, pairs, mem_size, code_size, method) C = max(train_y)+1 common['decay_steps'] = train_x.shape[0] / common['batch_size'] if method == 'MemGCN': # str_params += '' name = 'cgconv_softmax' params = common.copy() params['method'] = method params['p'] = [1] # pooling size params['M'] = [C] params['K'] = k # support number params['nhops'] = nhops # hop number params['fdim'] = fdim # filters dimension params['edim'] = edim # embeddings dimension params['mem_size'] = mem_size # the length of sequential records params['code_size'] = code_size # the size of one record params['n_words'] = n_words # feature dimension params['distance'] = distance params['fin'] = train_x.shape[2] params['dir_name'] += name params['filter'] = 'chebyshev5' params['brelu'] = 'b2relu' params['pool'] = 'apool1' mp.test(models.siamese_cgcnn_mem(L, **params), name, params, data, records, train_x, train_r, train_y, val_x, val_r, val_y, test_x, test_r, test_y, train_pairs, test_pairs) # mp.save(data_type) method_type = method + '_' mp.fin_result(method_type + data_type + str_params, i_fold)
def fGraph(inFIDDic, series_size=3, is_distance=True): # # 1 get the label of this sample. k = list(inFIDDic.keys())[0] #print(inFIDDic) inFIDDic[k][0] label = 1 if inFIDDic[k][0] == 3 else 0 # # 2 get the feature vector of vertices. # representing the building object (one vertice) by a feature vector, # Fourer expansion or Geometry description. # the number of buildings (vertices) in one building group (a sample) subObject_size = len(inFIDDic[k][1]) XY_coords, vertice, coords = [], [], [] for i in range(0, subObject_size): # one building in the sample. subObject = inFIDDic[k][1][i] [density] = inFIDDic[k][2][i] # Calculate the basic indicators of polygon. # Geometry descriptors: area, peri, SMBR_area [[CX, CY], area, peri] = geoutils.get_basic_parametries_of_Poly(subObject) XY_coords.append([CX, CY, i]) compactness = area / math.pow(0.282 * peri, 2) OBB, SMBR_area = geoutils.mininumAreaRectangle(subObject) orientation = OBB.Orientation() length_width = OBB.e1 / OBB.e0 if OBB.e0 > OBB.e1 else OBB.e0 / OBB.e1 area_radio = area / (OBB.e1 * OBB.e0 * 4) # print("area={}, peri={}, SMBR_area={}".format(area, peri, SMBR_area)) # Five basic indices. Faster geometry_vector = [ orientation, area, length_width, area_radio, compactness ] # More indices and more slowly. if True: # preparatory work uniform_coords = np.array([[(j[0] - CX), (j[1] - CY)] for j in subObject]) uniform_size = len(uniform_coords) # Closing the polygon. if uniform_coords[0][0] - uniform_coords[uniform_size - 1][ 0] != 0 or uniform_coords[0][1] - uniform_coords[ uniform_size - 1][1] != 0: print('Closing!') uniform_coords.append(uniform_coords[0]) # Part One. Size indicators: CONVEX_area, MEAN_radius, LONG_chord convexHull = ConvexHull(uniform_coords) CONVEX_area = convexHull.area sum_radius, size_radius, MEAN_radius, LONG_chord = 0, 0, 0, 0 for j in range(0, uniform_size - 1): sum_radius += math.sqrt( uniform_coords[j][0] * uniform_coords[j][0] + uniform_coords[j][1] * uniform_coords[j][1]) size_radius += 1 if size_radius != 0: MEAN_radius = sum_radius / size_radius pairwise_distances, index_j, index_h = sklearn.metrics.pairwise.pairwise_distances( uniform_coords[convexHull.vertices], metric="euclidean", n_jobs=1), 0, 0 for j in range(0, len(pairwise_distances)): for h in range(j, len(pairwise_distances)): if (pairwise_distances[j, h] > LONG_chord): LONG_chord, index_j, index_h = pairwise_distances[ j, h], j, h SECOND_chord, index_p, index_q = 0, 0, 0 for j in range(0, len(pairwise_distances)): for h in range(j, len(pairwise_distances)): if pairwise_distances[j, h] > SECOND_chord: if j != index_j and h != index_h: SECOND_chord, index_p, index_q = pairwise_distances[ j, h], j, h # Part two. Orientation indicators: LONGEDGE_orien, SMBR_orien, WEIGHT_orien from_longedge, to_longedge = uniform_coords[convexHull.vertices[ index_j]], uniform_coords[convexHull.vertices[index_h]] LONGEDGE_orien = abs( math.atan2(from_longedge[0] - to_longedge[0], from_longedge[1] - to_longedge[1])) from_secondedge, to_secondedge = uniform_coords[ convexHull.vertices[index_p]], uniform_coords[ convexHull.vertices[index_q]] SENCONDEDGE_orien = abs( math.atan2(from_secondedge[0] - to_secondedge[0], from_secondedge[1] - to_secondedge[1])) #LONGEDGE_dis = math.sqrt((from_longedge[0]-to_longedge[0])*(from_longedge[0]-to_longedge[0]) + (from_longedge[1]-to_longedge[1])*(from_longedge[1]-to_longedge[1])) SECONDEDGE_dis = math.sqrt( (from_secondedge[0] - to_secondedge[0]) * (from_secondedge[0] - to_secondedge[0]) + (from_secondedge[1] - to_secondedge[1]) * (from_secondedge[1] - to_secondedge[1])) BISSECTOR_orien = (LONGEDGE_orien * LONG_chord + SENCONDEDGE_orien * SECONDEDGE_dis) / ( LONG_chord + SECONDEDGE_dis) # print("LONG_width={}, LONGEDGE_dis={}".format(LONG_width, LONGEDGE_dis)) SMBR_orien, WALL_orien, WEIGHT_orien = orientation, 0, 0 # Calculate vertical width agaist long cord. # line equation: longedge_a, longedge_b, longedge_c = geoutils2.get_equation( from_longedge, to_longedge) LONG_width, up_offset, down_offset = 0, longedge_c, longedge_c for j in range(0, uniform_size - 1): crossing_product = longedge_a * uniform_coords[j][ 0] + longedge_b * uniform_coords[j][1] if crossing_product + up_offset < 0: up_offset = -crossing_product if crossing_product + down_offset > 0: down_offset = -crossing_product longedge_square = math.sqrt(longedge_a * longedge_a + longedge_b * longedge_b) if longedge_square == 0: LONG_width = abs(up_offset - down_offset) else: LONG_width = abs(up_offset - down_offset) / longedge_square edge_orien_weight, edge_length_sun, edge_tuple, candidate_max = 0, 0, [], 0 for j in range(0, uniform_size - 1): dx, dy = uniform_coords[j + 1][0] - uniform_coords[j][ 0], uniform_coords[j + 1][1] - uniform_coords[j][1] edge_orien = (math.atan2(dx, dy) + math.pi) % (math.pi / 2.0) # edge_orien = (math.atan2(dx, dy) + 2*math.pi) % math.pi # edge_orien = math.atan2(dx, dy) edge_length = math.sqrt(dx * dx + dy * dy) edge_orien_weight += edge_length * edge_orien edge_length_sun += edge_length edge_tuple.append([edge_orien, edge_length]) # add test code. # print("edge_length={}, edge_orien={}".format(edge_length, edge_orien*180/math.pi)) WALL_orien = edge_orien_weight / edge_length_sun for j in range(0, 90): candidate_orien, candidate_weight = j * math.pi / 180, 0 for j in range(0, len(edge_tuple)): if abs(edge_tuple[j][0] - candidate_orien) < math.pi / 24: candidate_weight += ( math.pi / 24 - abs(edge_tuple[j][0] - candidate_orien) ) * edge_tuple[j][1] / (math.pi / 24) if candidate_weight > candidate_max: candidate_max, WEIGHT_orien = candidate_weight, candidate_orien # Part three. shape indices: Diameter-Perimeter-Area- measurements RIC_compa, IPQ_compa, FRA_compa = area / peri, 4 * math.pi * area / ( peri * peri), 1 - math.log(area) * .5 / math.log(peri) GIB_compa, Div_compa = 2 * math.sqrt( math.pi * area) / LONG_chord, 4 * area / (LONG_chord * peri) # Part four. shape indices: Related shape # fit_Ellipse = geoutils2.fitEllipse(np.array(uniform_coords)[:,0], np.array(uniform_coords)[:,1]) # ellipse_axi = geoutils2.ellipse_axis_length(fit_Ellipse) # elongation, ellipticity, concavity = length_width, ellipse_axi[0]/ellipse_axi[1] if ellipse_axi[1] != 0 else 1, area/convexHull.area elongation, ellipticity, concavity = length_width, LONG_width / LONG_chord, area / convexHull.area radius, standard_circle, enclosing_circle = math.sqrt( area / math.pi), [], geoutils2.make_circle(uniform_coords) for j in range(0, 60): standard_circle.append([ math.cos(2 * math.pi * j / 100) * radius, math.sin(2 * math.pi * j / 100) * radius ]) standard_intersection = Polygon(uniform_coords).intersection( Polygon(standard_circle)) standard_union = Polygon(uniform_coords).union( Polygon(standard_circle)) DCM_index = area / (math.pi * enclosing_circle[2] * enclosing_circle[2]) BOT_index = 1 - standard_intersection.area / area closest_length, closest_sun, closest_size, BOY_measure = [], 0, 0, 0 for j in range(0, 40): x, y = math.cos(2 * math.pi * j / 40) * peri, math.sin( 2 * math.pi * j / 40) * peri closest_point, is_test = geoutils2.find_intersection( uniform_coords, [x, y]) if is_test: print("k={}, i={}, j={}".format(k, i, j)) # plt.plot([0, closest_point[0]], [0, closest_point[1]]) # debug if closest_point is not None: # plt.plot([0, closest_point[0]], [0, closest_point[1]]) # debug closest_length.append( math.sqrt(closest_point[0] * closest_point[0] + closest_point[1] * closest_point[1])) closest_sun += math.sqrt( closest_point[0] * closest_point[0] + closest_point[1] * closest_point[1]) closest_size += 1 #else: # print("Maybe the centerpoint is not in the polygon.") for j in closest_length: BOY_measure += abs(100 * j / closest_sun - 100 / closest_size) BOY_index = 1 - BOY_measure / 200 #print("BOY_index={}".format(BOY_index)) # Part six. shape indices: Dispersion of elements / components of area M02, M20, M11 = 0, 0, 0 for j in range(0, uniform_size - 1): M02 += (uniform_coords[j][1]) * (uniform_coords[j][1]) M20 += (uniform_coords[j][0]) * (uniform_coords[j][0]) M11 += (uniform_coords[j][0]) * (uniform_coords[j][1]) Eccentricity = ((M02 + M20) * (M02 + M20) + 4 * M11) / area geometry_vector = [CX, CY, area, peri, LONG_chord, MEAN_radius, \ SMBR_orien, LONGEDGE_orien, BISSECTOR_orien, WEIGHT_orien, \ RIC_compa, IPQ_compa, FRA_compa, GIB_compa, Div_compa, \ elongation, ellipticity, concavity, DCM_index, BOT_index, BOY_index, \ M11, Eccentricity, \ density] geometry_vector = [CX, CY, area, peri, MEAN_radius, \ SMBR_orien, WEIGHT_orien, \ IPQ_compa, FRA_compa, elongation, concavity, BOT_index, \ density] vertice.append(geometry_vector) coords.append(subObject) # # 3 get the adjacency graph of the building group (one sample). # KNN, MST, Delaunay = 1, 2, 3. vertice = np.array(vertice) points = np.array(XY_coords) adjacency = np.zeros((subObject_size, subObject_size)) # print(points[:,0:2]) tri = Delaunay(points[:, 0:2]) for i in range(0, tri.nsimplex): if i > tri.neighbors[i, 2]: adjacency[tri.vertices[i, 0], tri.vertices[i, 1]] = 1 adjacency[tri.vertices[i, 1], tri.vertices[i, 0]] = 1 if i > tri.neighbors[i, 0]: adjacency[tri.vertices[i, 1], tri.vertices[i, 2]] = 1 adjacency[tri.vertices[i, 2], tri.vertices[i, 1]] = 1 if i > tri.neighbors[i, 1]: adjacency[tri.vertices[i, 2], tri.vertices[i, 0]] = 1 adjacency[tri.vertices[i, 0], tri.vertices[i, 2]] = 1 adjacency = scipy.sparse.coo_matrix(adjacency, shape=(subObject_size, subObject_size)) distances = sklearn.metrics.pairwise.pairwise_distances(points[:, 0:2], metric='euclidean') if False: # Distance matrix. is it necessary to be normalized? idx = np.argsort(distances)[:, 1:1 + 1] distances.sort() distances = graph.adjacency(distances[:, 1:1 + 1], idx) adjacency = scipy.sparse.coo_matrix( np.ones((subObject_size, subObject_size)), shape=(subObject_size, subObject_size)).multiply(distances) print(distances.toarray()) # adjacency = adjacency.multiply(distances) else: adjacency = adjacency.multiply(distances) if False: adjacency = scipy.sparse.csgraph.minimum_spanning_tree(adjacency) adjacency = scipy.sparse.csr_matrix(adjacency).toarray() adjacency += adjacency.T - np.diag(adjacency.diagonal()) # print(adjacency) else: # distances = sklearn.metrics.pairwise.pairwise_distances(points[:,0:2], metric="euclidean", n_jobs=1) adjacency = scipy.sparse.csr_matrix(adjacency).toarray() if False: file = r'C:\Users\Administrator\Desktop\ahtor\thesis\gcnn\data\_used_bk.txt' file = "./data/_config_22.txt" conc = np.loadtxt(file) #print((vertice[0,3] - conc[0,1]) / conc[1,1]) vertice_shape = vertice[:, 2:].shape vertice[:, 2:] -= np.tile(conc[0, :], vertice_shape[0]).reshape(vertice_shape) vertice[:, 2:] /= np.tile(conc[1, :], vertice_shape[0]).reshape(vertice_shape) if len(vertice) < 128 and False: vertice = np.pad(vertice, ((0, 128 - len(vertice)), (0, 0)), 'constant', constant_values=(0)) adjacency = np.pad(adjacency, ((0, 128 - adjacency.shape[0]), (0, 128 - adjacency.shape[0])), 'constant', constant_values=(0)) laplacian = graph.laplacian(scipy.sparse.csr_matrix(adjacency), normalized=True, rescaled=True) print(vertice.shape) print(adjacency.shape) print(laplacian.shape) return coords, np.array(vertice), np.array(adjacency), laplacian, np.array( label)