def test_generate_network_pairs_min_cosine(random_matrix, pairs_min_cosine): """ All cosine scores in outputted array should be strictly higher than pairs_min_cosine. Values lower than pairs_min_cosine and negative cosine scores should be filtered out. """ mzs, matrix = random_matrix matrix = matrix.copy() matrix[0, 1] = matrix[1, 0] = pairs_min_cosine + 0.1 matrix[0, 2] = matrix[2, 0] = pairs_min_cosine - 0.1 interactions = generate_network(matrix, mzs, pairs_min_cosine, 0) seen1, seen2 = False, False for source, target, delta, cosine in interactions: assert cosine > pairs_min_cosine assert matrix[source, target] == cosine if (source == 0 and target == 1) or (source == 1 and target == 0): seen1 = True if (source == 0 and target == 2) or (source == 2 and target == 0): seen2 = True if matrix[0, 1] >= 0: assert seen1 assert not seen2
def test_generate_network_empty(): """An empty matrix should not throw an error but return an empty array. """ matrix = np.empty((0, 0), dtype=np.float32) interactions = generate_network(matrix, [], 0.65, 10) assert interactions.size == 0
def test_generate_network_high_pairs_min_cosine(random_matrix, pairs_min_cosine, top_k): """ If pairs_min_cosine is higher than 1, we should get an empty array. """ mzs, matrix = random_matrix interactions = generate_network(matrix, mzs, pairs_min_cosine, top_k) assert interactions.size == 0
def test_generate_network_all_zero(random_matrix): """ If all filtering parameters are set to zero, we should get all possibles interactions. """ mzs, matrix = random_matrix max_size = np.count_nonzero(np.triu(matrix)) interactions = generate_network(matrix, mzs, 0, 0) assert interactions.shape[0] == max_size
def test_generate_network_high_top_k(random_matrix): """ If top_k is high and pairs_min_cosine is zero, we should get all possibles interactions. """ mzs, matrix = random_matrix max_size = np.count_nonzero(np.triu(matrix)) top_k = matrix.shape[0] interactions = generate_network(matrix, mzs, 0, top_k) assert interactions.shape[0] == max_size
def test_generate_network_python_cython(random_matrix, pairs_min_cosine, top_k): """Cythonized `generate_network` and it's fallback Python version should give the same results. """ mzs, matrix = random_matrix interactions_p = generate_network.__wrapped__(matrix, mzs, pairs_min_cosine, 0) interactions_c = generate_network(matrix, mzs, pairs_min_cosine, 0) assert pytest.approx(interactions_p) == interactions_c
def test_generate_network_self_loop(random_matrix, pairs_min_cosine, top_k): """ Output array should include all self-loops if pairs_min_cosine is lower than one and self-loops should have cosine equal to one. """ mzs, matrix = random_matrix interactions = generate_network(matrix, mzs, pairs_min_cosine, top_k) count = 0 for source, target, delta, cosine in interactions: if source == target: count += 1 assert pytest.approx(cosine) == 1.0 assert count == matrix.shape[0]
def test_generate_network_list_larger_than_matrix(random_matrix): """ If list is larger than matrix, only part of the list will be used but no error should be thrown. """ mzs, matrix = random_matrix mzs = mzs + [1200.14225, 258.4475] max_index = len(mzs) interactions = generate_network(matrix, mzs, 0, 10) assert max_index - 1 not in interactions['Source'] assert max_index - 1 not in interactions['Target'] assert max_index - 2 not in interactions['Source'] assert max_index - 2 not in interactions['Target']
def test_generate_network_matrix_larger_than_list(random_matrix): """ If matrix is larger than list, only part of the matrix will be used but no error should be thrown. """ mzs, matrix = random_matrix mzs = mzs[:-2] max_index = matrix.shape[0] interactions = generate_network(matrix, mzs, 0, 10) assert max_index - 1 not in interactions['Source'] assert max_index - 1 not in interactions['Target'] assert max_index - 2 not in interactions['Source'] assert max_index - 2 not in interactions['Target']
def run(self): def callback(value): if value < 0: self.max += -value else: self.updated.emit(value) return not self.isStopped() # Create edges table (filter score below a threshold and apply TopK algorithm interactions = generate_network(self._scores, self._mzs, self.options.pairs_min_cosine, self.options.top_k, callback=callback) # Recreate graph deleting all previously created edges and eventually nodes graph = self._graph graph.delete_edges(self._graph.es) if not self._keep_vertices: graph.delete_vertices(graph.vs) nodes_idx = np.arange(self._scores.shape[0]) graph.add_vertices(nodes_idx.tolist()) # Add edges from edges table graph.add_edges(zip(interactions['Source'], interactions['Target'])) graph.es['__weight'] = interactions['Cosine'].tolist() # Set width for all edges based on their weight widths = np.array(interactions['Cosine']) if widths.size > 0: # noinspection PyUnboundLocalVariable min_ = max(0, widths.min() - 0.1) if min_ != widths.max(): widths = (RADIUS - 1) * (widths - min_) / (widths.max() - min_) + 1 else: widths = RADIUS else: widths = RADIUS graph.es['__width'] = widths if not self.isStopped(): return pd.DataFrame(interactions), graph else: self.canceled.emit()
def test_generate_network_python_cython(random_matrix, pairs_min_cosine, top_k): """Cythonized `generate_network` and it's fallback Python version should give the same results. """ mzs, matrix = random_matrix interactions_p = generate_network.__wrapped__(matrix, mzs, pairs_min_cosine, 0) interactions_c = generate_network(matrix, mzs, pairs_min_cosine, 0) assert interactions_p.shape == interactions_c.shape assert interactions_p.dtype == interactions_c.dtype for name, (dtype, _) in interactions_c.dtype.fields.items(): if np.issubdtype(dtype.type, np.floating): assert pytest.approx(interactions_p[name]) == interactions_c[name] else: assert np.array_equal(interactions_p[name], interactions_c[name])