def compute_scores( data: Table, genes: Table, p_threshold: float, p_value_fun: str, scoring: str, start: float, end: float, result: Result, state: TaskState, ): if not data or not genes: result.scores.z_vals = None result.scores.annotations = None result.scores.p_vals = None result.scores.table = None else: state.set_status("Computing scores...") weights = np.array([15, 75, 10]) * (end - start) / 100 if not result.scores.z_vals: result.scores.z_vals = AnnotateSamplesMeta.mann_whitney_test( data) state.set_partial_result(("scores", result)) state.set_progress_value(weights[0]) if state.is_interruption_requested(): return if not result.scores.annotations or not result.scores.p_vals: annot, p_vals = AnnotateSamplesMeta.assign_annotations( result.scores.z_vals, genes, data, p_value_fun=p_value_fun, scoring=scoring) result.scores.annotations = annot result.scores.p_vals = p_vals state.set_partial_result(("scores", result)) state.set_progress_value(weights[1]) if state.is_interruption_requested(): return result.scores.table = AnnotateSamplesMeta.filter_annotations( result.scores.annotations, result.scores.p_vals, p_threshold=p_threshold) state.set_partial_result(("scores", result))
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 _prepare_dir_and_save_images(paths_queue, dir_name, target_size, previously_saved, state: TaskState): """ This function prepares a directory structure and calls function that saves images. Parameters ---------- previously_saved : int Number of saved images in the previous process. If the process is resumed it is non-zero. """ res = Result(paths=paths_queue) if previously_saved == 0: _clean_dir(dir_name) steps = len(paths_queue) + previously_saved loader = ImageLoader() while res.paths: from_path, to_path = res.paths.popleft() _save_an_image(loader, from_path, to_path, target_size) state.set_progress_value((1 - len(res.paths) / steps) * 100) state.set_partial_result(res) if state.is_interruption_requested(): return res return res
def run_vizrank(compute_score: Callable, states: Iterator, scores: List, task: TaskState): res = Result(queue=Queue(), scores=None) scores = scores.copy() def do_work(st, next_st): try: score = compute_score(st) if score is not None: pos = bisect_left(scores, score) res.queue.put_nowait( QueuedScore(position=pos, score=score, state=st, next_state=next_st)) scores.insert(pos, score) except Exception: # ignore current state in case of any problem pass res.scores = scores.copy() task.set_partial_result(res) state = None next_state = next(states) try: while True: if task.is_interruption_requested(): return res state = copy.copy(next_state) next_state = copy.copy(next(states)) do_work(state, next_state) except StopIteration: do_work(state, None) return res
def run_vizrank(compute_score: Callable, states: Iterator, scores: List, task: TaskState): res = Result(queue=Queue(), scores=None) scores = scores.copy() def do_work(st, next_st): try: score = compute_score(st) if score is not None: pos = bisect_left(scores, score) res.queue.put_nowait(QueuedScore(position=pos, score=score, state=st, next_state=next_st)) scores.insert(pos, score) except Exception: # ignore current state in case of any problem pass res.scores = scores.copy() task.set_partial_result(res) state = None next_state = next(states) try: while True: if task.is_interruption_requested(): return res state = copy.copy(next_state) next_state = copy.copy(next(states)) do_work(state, next_state) except StopIteration: do_work(state, None) return res
def run_vizrank(compute_score: Callable, iterate_states: Callable, saved_state: Optional[Iterable], scores: List, progress: int, state_count: int, task: TaskState): task.set_status("Getting combinations...") task.set_progress_value(0.1) states = iterate_states(saved_state) task.set_status("Getting scores...") res = Result(queue=Queue(), scores=None) scores = scores.copy() can_set_partial_result = True def do_work(st, next_st): try: score = compute_score(st) if score is not None: pos = bisect_left(scores, score) res.queue.put_nowait( QueuedScore(position=pos, score=score, state=st, next_state=next_st)) scores.insert(pos, score) except Exception: # ignore current state in case of any problem pass res.scores = scores.copy() def reset_flag(): nonlocal can_set_partial_result can_set_partial_result = True state = None next_state = next(states) try: while True: if task.is_interruption_requested(): return res task.set_progress_value(int(progress * 100 / max(1, state_count))) progress += 1 state = copy.copy(next_state) next_state = copy.copy(next(states)) do_work(state, next_state) # for simple scores (e.g. correlations widget) and many feature # combinations, the 'partial_result_ready' signal (emitted by # invoking 'task.set_partial_result') was emitted too frequently # for a longer period of time and therefore causing the widget # being unresponsive if can_set_partial_result: task.set_partial_result(res) can_set_partial_result = False Timer(0.01, reset_flag).start() except StopIteration: do_work(state, None) task.set_partial_result(res) return res
def run(gene_sets: GeneSets, selected_gene_sets: List[Tuple[str, ...]], genes, state: TaskState) -> Results: results = Results() items = [] step, steps = 0, len(gene_sets) if not genes: return results state.set_status('Calculating...') for gene_set in sorted(gene_sets): step += 1 if step % (steps / 10) == 0: state.set_progress_value(100 * step / steps) if gene_set.hierarchy not in selected_gene_sets: continue if state.is_interruption_requested(): return results matched_set = gene_set.genes & genes if len(matched_set) > 0: category_column = QStandardItem() term_column = QStandardItem() count_column = QStandardItem() genes_column = QStandardItem() category_column.setData(", ".join(gene_set.hierarchy), Qt.DisplayRole) term_column.setData(gene_set.name, Qt.DisplayRole) term_column.setData(gene_set.name, Qt.ToolTipRole) # there was some cases when link string was not empty string but not valid (e.g. "_") if gene_set.link and urlparse(gene_set.link).scheme: term_column.setData(gene_set.link, LinkRole) term_column.setForeground(QColor(Qt.blue)) count_column.setData(matched_set, Qt.UserRole) count_column.setData(len(matched_set), Qt.DisplayRole) genes_column.setData(len(gene_set.genes), Qt.DisplayRole) genes_column.setData( set(gene_set.genes), Qt.UserRole) # store genes to get then on output on selection items.append( [count_column, genes_column, category_column, term_column]) results.items = items return results
def run(data: Table, desc, use_values, task: TaskState) -> Result: if task.is_interruption_requested(): raise CancelledError # pragma: no cover new_variables = construct_variables(desc, data, use_values) # Explicit cancellation point after `construct_variables` which can # already run `compute_value`. if task.is_interruption_requested(): raise CancelledError # pragma: no cover attrs = [var for var in new_variables if var.is_primitive()] metas = [var for var in new_variables if not var.is_primitive()] new_domain = Orange.data.Domain(data.domain.attributes + tuple(attrs), data.domain.class_vars, metas=data.domain.metas + tuple(metas)) try: for variable in new_variables: variable.compute_value.mask_exceptions = False data = data.transform(new_domain) finally: for variable in new_variables: variable.compute_value.mask_exceptions = True return Result(data, attrs, metas)
def run(data: Table, embedding: Optional[np.ndarray], state: TaskState): res = Result(embedding=embedding) # simulate wasteful calculation (increase 'steps') step, steps = 0, 10 state.set_status("Calculating...") while step < steps: for _ in range(steps): x_data = np.array(np.mean(data.X, axis=1)) if x_data.ndim == 2: x_data = x_data.ravel() y_data = np.random.rand(len(x_data)) embedding = np.vstack((x_data, y_data)).T step += 1 if step % (steps / 10) == 0: state.set_progress_value(100 * step / steps) if state.is_interruption_requested(): return res res.embedding = embedding state.set_partial_result(res) return res
def run_freeviz(data: Table, projector: FreeViz, state: TaskState): res = Result(projector=projector, projection=None) step, steps = 0, MAX_ITERATIONS initial = res.projector.components_.T state.set_status("Calculating...") while True: # Needs a copy because projection should not be modified inplace. # If it is modified inplace, the widget and the thread hold a # reference to the same object. When the thread is interrupted it # is still modifying the object, but the widget receives it # (the modified object) with a delay. res.projection = res.projector(data).copy() anchors = res.projector.components_.T res.projector.initial = anchors state.set_partial_result(res) if np.allclose(initial, anchors, rtol=1e-5, atol=1e-4): return res initial = anchors step += 1 state.set_progress_value(100 * step / steps) if state.is_interruption_requested(): return res
def run( gene_sets: GeneSets, selected_gene_sets: List[Tuple[str, ...]], genes, state: TaskState, reference_genes=None ) -> Results: results = Results() items = [] step, steps = 0, len(gene_sets) def set_progress(): nonlocal step step += 1 state.set_progress_value(100 * (step / steps)) if not genes: return results state.set_status('Calculating...') for gene_set in sorted(gene_sets): set_progress() if gene_set.hierarchy not in selected_gene_sets: continue if state.is_interruption_requested(): return results reference_genes = [] if reference_genes is None else reference_genes enrichemnt_result = gene_set.set_enrichment(reference_genes, genes.intersection(reference_genes)) if len(enrichemnt_result.query) > 0: category_column = QStandardItem() term_column = QStandardItem() count_column = QStandardItem() genes_column = QStandardItem() ref_column = QStandardItem() pval_column = QStandardItem() fdr_column = QStandardItem() enrichment_column = QStandardItem() category_column.setData(", ".join(gene_set.hierarchy), Qt.DisplayRole) term_column.setData(gene_set.name, Qt.DisplayRole) term_column.setData(gene_set.name, Qt.ToolTipRole) # there was some cases when link string was not empty string but not valid (e.g. "_") if gene_set.link and urlparse(gene_set.link).scheme: term_column.setData(gene_set.link, LinkRole) term_column.setForeground(QColor(Qt.blue)) count_column.setData(len(enrichemnt_result.query), Qt.DisplayRole) count_column.setData(set(enrichemnt_result.query), Qt.UserRole) genes_column.setData(len(gene_set.genes), Qt.DisplayRole) genes_column.setData(set(gene_set.genes), Qt.UserRole) # store genes to get then on output on selection ref_column.setData(len(enrichemnt_result.reference), Qt.DisplayRole) pval_column.setData(enrichemnt_result.p_value, Qt.DisplayRole) pval_column.setData(enrichemnt_result.p_value, Qt.ToolTipRole) enrichment_column.setData(enrichemnt_result.enrichment_score, Qt.DisplayRole) enrichment_column.setData(enrichemnt_result.enrichment_score, Qt.ToolTipRole) items.append( [ count_column, ref_column, pval_column, fdr_column, enrichment_column, genes_column, category_column, term_column, ] ) results.items = items return results