def create(): try: # get request params data = json.loads(request.params.data) file_id = int(data["file_id"]) file_name = _get_file_name_from_id(file_id) file_path = os.path.join(DATA_DIR, file_name) # create topology instance topology = Topology(verbose=0) loader = CSVLoader(file_path) topology.load(loader=loader, standardize=True) target_index = data["target_index"] mode = int(data["mode"]) clustering_algorithm = int(data["clustering_algorithm"]) train_size = float(data["train_size"]) k = int(data["k"]) eps = float(data["eps"]) min_samples = int(data["min_samples"]) resolution = int(data["resolution"]) overlap = float(data["overlap"]) topology.point_cloud = np.array(data["point_cloud"]) visualize_mode = int(data["visualize_mode"]) if mode == 0: # scatter plot colors = [] for i in range(topology.number_data.shape[1]): t = topology.number_data[:, i] scaler = preprocessing.MinMaxScaler() t = scaler.fit_transform(t.reshape(-1, 1)) topology.color_point_cloud(target=t, normalize=False) colors.append(topology.point_cloud_hex_colors) elif mode == 1: # unsupervised_clusterings # If target index isn't exists, use all data to calculate if target_index != '': topology.number_data, target = _split_target( topology.number_data, int(target_index)) clusters = [ cluster.KMeans(n_clusters=k), cluster.DBSCAN(eps=eps, min_samples=min_samples) ] topology.unsupervised_clustering_point_cloud( clusterer=clusters[clustering_algorithm]) if target_index != '': topology.number_data = _concat_target(topology.number_data, target, int(target_index)) colors = [] for i in range(topology.number_data.shape[1]): colors.append(topology.point_cloud_hex_colors) elif mode == 2: # supervised_clusterings # If target index isn't exists, use all data to calculate if target_index != '': topology.number_data, target = _split_target( topology.number_data, int(target_index)) clusters = [neighbors.KNeighborsClassifier(n_neighbors=k)] topology.supervised_clustering_point_cloud( clusterer=clusters[clustering_algorithm], target=target, train_size=train_size) if target_index != '': topology.number_data = _concat_target(topology.number_data, target, int(target_index)) colors = [] for i in range(topology.number_data.shape[1]): colors.append(topology.point_cloud_hex_colors) elif mode == 3: # tda topology.map(resolution=resolution, overlap=overlap, eps=eps, min_samples=min_samples) if visualize_mode == 2: presenter = SpectralPresenter(fig_size=(10, 10), node_size=5, edge_width=1) pos = presenter._get_position(topology.nodes, topology.edges) topology.nodes = np.array(list(pos.values())) print("spectral") colors = [] for i in range(topology.number_data.shape[1]): t = topology.number_data[:, i] scaler = preprocessing.MinMaxScaler() t = scaler.fit_transform(t.reshape(-1, 1)) topology.color(target=t) colors.append(topology.hex_colors) if mode < 3: hypercubes = _ndarray_to_list( np.arange(len(topology.point_cloud)).reshape(-1, 1)) nodes = _ndarray_to_list(topology.point_cloud) edges = [] node_sizes = [MIN_NODE_SIZE] * len(topology.point_cloud) colors = colors elif mode == 3: hypercubes = _convert_hypercubes_to_array(topology.hypercubes) nodes = _ndarray_to_list(topology.nodes) edges = _ndarray_to_list(topology.edges) scaler = preprocessing.MinMaxScaler(feature_range=(MIN_NODE_SIZE, MAX_NODE_SIZE)) node_sizes = _ndarray_to_list( scaler.fit_transform(topology.node_sizes)) colors = colors body = { "hypercubes": hypercubes, "nodes": nodes, "edges": edges, "node_sizes": node_sizes, "colors": colors, "train_index": _ndarray_to_list(topology.train_index) } r = create_response(body) return r except Exception as e: body = json.dumps({"error_msg": e.args[0]}) r = create_response(body) return r