def test_clustering_collection_timeout(load_reaction_tree): collection = RouteCollection( reaction_trees=[ ReactionTree.from_dict( load_reaction_tree("routes_for_clustering.json", idx) ) for idx in range(3) ] ) cluster_labels = collection.cluster(n_clusters=1, timeout=0) assert len(cluster_labels) == 0 assert collection.clusters is None
def test_clustering_collection(load_reaction_tree): collection = RouteCollection( reaction_trees=[ ReactionTree.from_dict( load_reaction_tree("routes_for_clustering.json", idx) ) for idx in range(3) ] ) collection.cluster(n_clusters=1) assert len(collection.clusters) == 2 assert collection.clusters[0].reaction_trees == collection.reaction_trees[1:3] assert collection.clusters[1].reaction_trees == [collection.reaction_trees[0]]
def _score_reaction_tree(self, tree: ReactionTree) -> float: def _recursive_score(node): # This list should contains 0 or 1 elements reaction_nodes = list(tree.graph[node]) if not reaction_nodes: return leaf_costs[node] child_sum = sum( 1 / self.average_yield * _recursive_score(child) for child in tree.graph[reaction_nodes[0]] ) return self.reaction_cost + child_sum leaf_costs = self._calculate_leaf_costs(tree.leafs()) return _recursive_score(tree.root)
def test_scoring_branched_route_in_stock(load_reaction_tree, default_config, mock_stock): mock_stock( default_config, "CC(C)(C)CO", "CC(C)(C)OC(=O)N(CCCl)CCCl", "N#CCc1cccc(O)c1F", "O=[N+]([O-])c1ccccc1F", "O=C1CCC(=O)N1Br", "O=C=Nc1csc(C(F)(F)F)n1", "CCC[Sn](Cl)(CCC)CCC", "COc1ccc2ncsc2c1", ) tree = ReactionTree.from_dict(load_reaction_tree("branched_route.json")) assert pytest.approx(StateScorer(default_config)(tree), abs=1e-3) == 0.950 assert NumberOfReactionsScorer(default_config)(tree) == 14 assert NumberOfPrecursorsScorer(default_config)(tree) == 8 assert NumberOfPrecursorsInStockScorer(default_config)(tree) == 8 assert PriceSumScorer(default_config)(tree) == 8 cost_score = RouteCostScorer(default_config)(tree) assert pytest.approx(cost_score, abs=1e-3) == 77.4797
def test_distance_collection(load_reaction_tree): collection = RouteCollection( reaction_trees=[ ReactionTree.from_dict( load_reaction_tree("routes_for_clustering.json", idx) ) for idx in range(3) ] ) dist_mat1 = collection.distance_matrix() dist_mat2 = collection.distance_matrix(recreate=True) assert (dist_mat1 - dist_mat2).sum() == 0 dist_mat3 = collection.distance_matrix(content="molecules") assert (dist_mat1 - dist_mat3).sum() != 0 assert len(dist_mat3) == 3 assert pytest.approx(dist_mat3[0, 1], abs=1e-2) == 2.6522 assert pytest.approx(dist_mat3[0, 2], abs=1e-2) == 3.0779 assert pytest.approx(dist_mat3[2, 1], abs=1e-2) == 0.7483
def test_number_of_reaction_scorer_tree(load_reaction_tree): tree = ReactionTree.from_dict(load_reaction_tree("sample_reaction.json")) scorer = NumberOfReactionsScorer() assert scorer(tree) == 2
def test_template_occurence_scorer_tree(load_reaction_tree): tree = ReactionTree.from_dict(load_reaction_tree("sample_reaction.json")) scorer = AverageTemplateOccurenceScorer() assert scorer(tree) == 0
def _score_reaction_tree(self, tree: ReactionTree) -> float: leaf_costs = self._calculate_leaf_costs(tree.leafs()) return sum(leaf_costs[leaf] for leaf in tree.leafs())
def _score_reaction_tree(self, tree: ReactionTree) -> float: return self._calc_average(list(tree.reactions()))
def _score_reaction_tree(self, tree: ReactionTree) -> float: return len([mol for mol in tree.leafs() if mol in self._stock])
def _score_reaction_tree(self, tree: ReactionTree) -> float: return len(list(tree.leafs()))
def test_route_is_branched(load_reaction_tree, filename, expected): rt = ReactionTree.from_dict(load_reaction_tree(filename)) assert rt.is_branched() == expected
def test_route_distance_self(load_reaction_tree): dict_ = load_reaction_tree("branched_route.json", 0) rt = ReactionTree.from_dict(dict_) assert rt.distance_to(rt) == 0.0