def update_loop(X, max_iter, step, init): """ return an iterator over successive improved MDS point embeddings. """ # NOTE: this code MUST NOT call into QApplication.processEvents done = False iterations_done = 0 oldstress = np.finfo(np.float).max init_type = "PCA" if self.initialization == OWMDS.PCA else "random" while not done: step_iter = min(max_iter - iterations_done, step) mds = MDS(dissimilarity="precomputed", n_components=2, n_init=1, max_iter=step_iter, init_type=init_type, init_data=init) mdsfit = mds(X) iterations_done += step_iter embedding, stress = mdsfit.embedding_, mdsfit.stress_ stress /= np.sqrt(np.sum(embedding**2, axis=1)).sum() if iterations_done >= max_iter: done = True elif (oldstress - stress) < mds.params["eps"]: done = True init = embedding oldstress = stress yield embedding, mdsfit.stress_, iterations_done / max_iter
def run_mds(matrix: DistMatrix, max_iter: int, step_size: int, init_type: int, embedding: np.ndarray, state: TaskState): res = Result(embedding=embedding) iterations_done = 0 init = embedding state.set_status("Running...") oldstress = np.finfo(np.float).max while True: step_iter = min(max_iter - iterations_done, step_size) mds = MDS( dissimilarity="precomputed", n_components=2, n_init=1, max_iter=step_iter, init_type=init_type, init_data=init ) mdsfit = mds(matrix) iterations_done += step_iter embedding, stress = mdsfit.embedding_, mdsfit.stress_ emb_norm = np.sqrt(np.sum(embedding ** 2, axis=1)).sum() if emb_norm > 0: stress /= emb_norm res.embedding = embedding state.set_partial_result(res) state.set_progress_value(100 * iterations_done / max_iter) if iterations_done >= max_iter or stress == 0 or \ (oldstress - stress) < mds.params["eps"]: return res init = embedding oldstress = stress if state.is_interruption_requested(): return res
def layout_mds(self, steps, distances, progress_callback=None, opt_from_curr=False): """Position the network components according to similarities among them. """ nodes = self.nodes() if distances == None or distances.dim != len(nodes): self.information('invalid or no distance matrix') return 1 p = self.plot() minStressDelta = 0 mdsRefresh = int(steps / 20) self.mdsStep = 1 self.stopMDS = False #~ distances.matrixType = core.SymMatrix.Symmetric mds = MDS()(distances) mds.optimize(10, projection.mds.SgnRelStress, 0) rect = self.data_rect() w_fr = rect.width() h_fr = rect.height() d_fr = math.sqrt(w_fr ** 2 + h_fr ** 2) x_mds, y_mds = zip(*mds.points) w_mds = max(x_mds) - min(x_mds) h_mds = max(y_mds) - min(y_mds) d_mds = math.sqrt(w_mds ** 2 + h_mds ** 2) self.set_node_coordinates(dict( (n, (nodes[n].x() * d_mds / d_fr, nodes[n].y() * d_mds / d_fr)) for n in nodes)) p.update_graph_layout() qApp.processEvents() if opt_from_curr: for i, u in enumerate(sorted(nodes.keys())): mds.points[i][0] = nodes[u].x() mds.points[i][1] = nodes[u].y() else: mds.Torgerson() mds.optimize(steps, projection.mds.SgnRelStress, minStressDelta, progressCallback= lambda a, b=None, mds=mds, mdsRefresh=mdsRefresh, progress_callback=progress_callback: self.mds_callback(a, b, mds, mdsRefresh, progress_callback)) self.mds_callback(mds.avgStress, 0, mds, mdsRefresh, progress_callback) if progress_callback is not None: progress_callback(mds.avgStress, self.mdsStep) return 0
def _initialize(self): matrix_existed = self.effective_matrix is not None effective_matrix = self.effective_matrix self._invalidated = True self.data = None self.effective_matrix = None self.closeContext() self.clear_messages() # if no data nor matrix is present reset plot if self.signal_data is None and self.matrix is None: self.clear() self.init_attr_values() return if self.signal_data is not None and self.matrix is not None and \ len(self.signal_data) != len(self.matrix): self.Error.mismatching_dimensions() self.clear() self.init_attr_values() return if self.signal_data is not None: self.data = self.signal_data elif self.matrix_data is not None: self.data = self.matrix_data if self.matrix is not None: self.effective_matrix = self.matrix if self.matrix.axis == 0 and self.data is not None \ and self.data is self.matrix_data: names = [[attr.name] for attr in self.data.domain.attributes] domain = Domain([], metas=[StringVariable("labels")]) self.data = Table.from_list(domain, names) elif self.data.domain.attributes: preprocessed_data = MDS().preprocess(self.data) self.effective_matrix = Euclidean(preprocessed_data) else: self.Error.no_attributes() self.clear() self.init_attr_values() return self.init_attr_values() self.openContext(self.data) self._invalidated = not ( matrix_existed and self.effective_matrix is not None and array_equal(effective_matrix, self.effective_matrix)) if self._invalidated: self.clear() self.graph.set_effective_matrix(self.effective_matrix)
def _initialize(self): matrix_existed = self.effective_matrix is not None effective_matrix = self.effective_matrix self.__invalidated = True self.data = None self.effective_matrix = None self.closeContext() self.clear_messages() # if no data nor matrix is present reset plot if self.signal_data is None and self.matrix is None: self.clear() self.init_attr_values() return if self.signal_data is not None and self.matrix is not None and \ len(self.signal_data) != len(self.matrix): self.Error.mismatching_dimensions() self.clear() self.init_attr_values() return if self.signal_data is not None: self.data = self.signal_data elif self.matrix_data is not None: self.data = self.matrix_data if self.matrix is not None: self.effective_matrix = self.matrix if self.matrix.axis == 0 and self.data is self.matrix_data: self.data = None elif self.data.domain.attributes: preprocessed_data = MDS().preprocess(self.data) self.effective_matrix = Euclidean(preprocessed_data) else: self.Error.no_attributes() self.clear() self.init_attr_values() return self.init_attr_values() self.openContext(self.data) self.__invalidated = not (matrix_existed and self.effective_matrix is not None and np.array_equal(effective_matrix, self.effective_matrix)) if self.__invalidated: self.clear() self.graph.set_effective_matrix(self.effective_matrix)
def _initialize(self): # clear everything self.closeContext() self._clear() self.Error.clear() self.effective_matrix = None self.embedding = None # if no data nor matrix is present reset plot if self.signal_data is None and self.matrix_data is None: self.data = None self.init_attr_values() return if self.signal_data is not None and self.matrix is not None and \ len(self.signal_data) != len(self.matrix): self.Error.mismatching_dimensions() self._update_plot() return if self.signal_data is not None: self.data = self.signal_data elif self.matrix_data is not None: self.data = self.matrix_data if self.matrix is not None: self.effective_matrix = self.matrix if self.matrix.axis == 0 and self.data is self.matrix_data: self.data = None elif self.data.domain.attributes: preprocessed_data = MDS().preprocess(self.data) self.effective_matrix = Euclidean(preprocessed_data) else: self.Error.no_attributes() return self.init_attr_values() self.openContext(self.data) self.graph.set_effective_matrix(self.effective_matrix)
def update_loop(X, max_iter, step, init): """ return an iterator over successive improved MDS point embeddings. """ # NOTE: this code MUST NOT call into QApplication.processEvents done = False iterations_done = 0 oldstress = np.finfo(np.float).max init_type = "PCA" if self.initialization == OWMDS.PCA else "random" while not done: step_iter = min(max_iter - iterations_done, step) mds = MDS(dissimilarity="precomputed", n_components=2, n_init=1, max_iter=step_iter, init_type=init_type, init_data=init) with warnings.catch_warnings(): warnings.filterwarnings("ignore", ".*double_scalars.*", RuntimeWarning) mdsfit = mds(X) iterations_done += step_iter embedding, stress = mdsfit.embedding_, mdsfit.stress_ emb_norm = np.sqrt(np.sum(embedding**2, axis=1)).sum() if emb_norm > 0: stress /= emb_norm if iterations_done >= max_iter \ or (oldstress - stress) < mds.params["eps"] \ or stress == 0: done = True init = embedding oldstress = stress yield embedding, mdsfit.stress_, iterations_done / max_iter
def layout_fragviz(self, steps, distances, graph, progress_callback=None, opt_from_curr=False): """Position the network components according to similarities among them. """ if distances == None or graph == None or distances.dim != graph.number_of_nodes(): self.information('invalid or no distance matrix') return 1 p = self.plot() edges = self.edges() nodes = self.nodes() avgLinkage = True rotationOnly = False minStressDelta = 0 mdsRefresh = 10#int(steps / 20) self.mdsStep = 1 self.stopMDS = False nodes_inds = dict((n, i) for i, n in enumerate(sorted(graph.nodes_iter()))) inds_nodes = dict((i, n) for i, n in enumerate(sorted(graph.nodes_iter()))) graph_components = nx.algorithms.connected_components(graph) matrix_components = [[nodes_inds[n] for n in c] for c in graph_components] #~ distances.matrixType = core.SymMatrix.Symmetric # scale net coordinates if avgLinkage: distances = distances.avgLinkage(matrix_components) # if only one component if distances.dim == 1: return 0 mds = MDS()(distances) rect = self.data_rect() w_fr = rect.width() h_fr = rect.height() d_fr = math.sqrt(w_fr ** 2 + h_fr ** 2) x_mds, y_mds = zip(*mds.points) w_mds = max(x_mds) - min(x_mds) h_mds = max(y_mds) - min(y_mds) d_mds = math.sqrt(w_mds ** 2 + h_mds ** 2) # if only one component if d_mds == 0 or d_fr == 0: d_mds = 1 d_fr = 1 self.set_node_coordinates(dict((key, (node.x() * d_mds / d_fr, node.y() * d_mds / d_fr)) \ for key, node in nodes.items())) p.update_graph_layout() qApp.processEvents() if opt_from_curr: if avgLinkage: for u, c in enumerate(graph_components): x = sum([nodes[n].x() for n in c]) / len(c) y = sum([nodes[n].y() for n in c]) / len(c) mds.points[u][0] = x mds.points[u][1] = y else: for i, u in enumerate(sorted(nodes.keys())): mds.points[i][0] = nodes[u].x() mds.points[i][1] = nodes[u].y() else: mds.Torgerson() mds.optimize(steps, projection.mds.SgnRelStress, minStressDelta, progressCallback= lambda a, b=None, mds=mds, mdsRefresh=mdsRefresh, graph_comp=graph_components, matrix_comp=matrix_components, progress_callback=progress_callback: self.fragviz_callback(a, b, mds, mdsRefresh, graph_comp, matrix_comp, progress_callback)) self.fragviz_callback(mds.avgStress, 0, mds, mdsRefresh, graph_components, matrix_components, progress_callback) if progress_callback is not None: progress_callback(mds.avgStress, self.mdsStep) return 0