def test_create_route_collection_andor_tree(setup_analysis_andor_tree): analysis = setup_analysis_andor_tree() routes = RouteCollection.from_analysis(analysis) assert len(routes) == 3 assert routes.nodes == [None, None, None]
def test_create_combine_tree_dict_from_tree(mock_stock, default_config, load_reaction_tree, shared_datadir): mock_stock( default_config, "Nc1ccc(NC(=S)Nc2ccccc2)cc1", "Cc1ccc2nc3ccccc3c(Cl)c2c1", "Nc1ccc(N)cc1", "S=C=Nc1ccccc1", "Cc1ccc2nc3ccccc3c(N)c2c1", "Nc1ccc(Br)cc1", ) search_tree = SearchTree.from_json( shared_datadir / "tree_for_clustering.json", default_config) analysis = TreeAnalysis(search_tree) collection = RouteCollection.from_analysis(analysis, 3) expected = load_reaction_tree("combined_example_tree.json") combined_dict = collection.combined_reaction_trees().to_dict() assert len(combined_dict["children"]) == 2 assert combined_dict["children"][0]["is_reaction"] assert len(combined_dict["children"][0]["children"]) == 2 assert len(combined_dict["children"][1]["children"]) == 2 assert len(combined_dict["children"][1]["children"][0]["children"]) == 2 assert combined_dict["children"][1]["children"][0]["children"][0][ "is_reaction"] assert combined_dict == expected
def test_create_route_collection_full(setup_analysis, mocker): analysis, _ = setup_analysis() routes = RouteCollection.from_analysis(analysis, 5) assert len(routes) == 7 # Check a few of the routes assert np.round(routes.scores[0], 3) == 0.994 assert len(routes.reaction_trees[0].graph) == 8 assert np.round(routes.scores[1], 3) == 0.681 assert len(routes.reaction_trees[1].graph) == 5 assert "dict" not in routes[0] assert "json" not in routes[0] assert "image" not in routes[0] mocker.patch("aizynthfinder.analysis.ReactionTree.to_dict") mocker.patch("aizynthfinder.analysis.json.dumps") mocker.patch("aizynthfinder.utils.image.GraphvizReactionGraph.to_image") mocker.patch( "aizynthfinder.utils.image.GraphvizReactionGraph.add_molecule") # Just see that the code does not crash, does not verify content assert len(routes.images) == 7 assert len(routes.dicts) == 7 assert len(routes.jsons) == 7
def test_dict_with_scores(setup_analysis): analysis, _ = setup_analysis() routes = RouteCollection.from_analysis(analysis, 5) dicts = routes.dict_with_scores() assert "scores" not in routes.dicts[0] assert "scores" in dicts[0] assert np.round(dicts[0]["scores"]["state score"], 3) == 0.994
def build_routes(self, min_nodes=5): """ Build reaction routes This is necessary to call after the tree search has completed in order to extract results from the tree search. :param min_nodes: the minimum number of top-ranked nodes to consider, defaults to 5 :type min_nodes: int, optional """ self.analysis = TreeAnalysis(self.tree) self.routes = RouteCollection.from_analysis(self.analysis, min_nodes)
def test_rescore_collection(setup_analysis): analysis, _ = setup_analysis() routes = RouteCollection.from_analysis(analysis, 5) routes.rescore(NumberOfReactionsScorer()) assert routes.scores[0] == 1 assert np.round(routes.all_scores[0]["state score"], 3) == 0.681 assert routes.all_scores[0]["number of reactions"] == 1 assert np.round(routes.all_scores[1]["state score"], 3) == 0.523 assert routes.scores[1] == 1 assert routes.all_scores[1]["number of reactions"] == 1
def test_compute_new_score(setup_analysis): analysis, _ = setup_analysis() routes = RouteCollection.from_analysis(analysis, 5) routes.compute_scores(NumberOfReactionsScorer()) assert np.round(routes.scores[0], 3) == 0.994 assert np.round(routes.all_scores[0]["state score"], 3) == 0.994 assert routes.all_scores[0]["number of reactions"] == 2 assert np.round(routes.scores[1], 3) == 0.681 assert np.round(routes.all_scores[1]["state score"], 3) == 0.681 assert routes.all_scores[1]["number of reactions"] == 1
def build_routes(self, min_nodes=5, scorer="state score"): """ Build reaction routes This is necessary to call after the tree search has completed in order to extract results from the tree search. :param min_nodes: the minimum number of top-ranked nodes to consider, defaults to 5 :type min_nodes: int, optional :param scorer: the object used to score the nodes :type scorer: str, optional """ self.analysis = TreeAnalysis(self.tree, scorer=self.scorers[scorer]) self.routes = RouteCollection.from_analysis(self.analysis, min_nodes)
def build_routes(self, min_nodes: int = 5, scorer: str = "state score") -> None: """ Build reaction routes This is necessary to call after the tree search has completed in order to extract results from the tree search. :param min_nodes: the minimum number of top-ranked nodes to consider, defaults to 5 :param scorer: a reference to the object used to score the nodes :raises ValueError: if the search tree not initialized """ if not self.tree: raise ValueError("Search tree not initialized") self.analysis = TreeAnalysis(self.tree, scorer=self.scorers[scorer]) self.routes = RouteCollection.from_analysis(self.analysis, min_nodes)
def build_routes(self, selection: RouteSelectionArguments = None, scorer: str = "state score") -> None: """ Build reaction routes This is necessary to call after the tree search has completed in order to extract results from the tree search. :param selection: the selection criteria for the routes :param scorer: a reference to the object used to score the nodes :raises ValueError: if the search tree not initialized """ if not self.tree: raise ValueError("Search tree not initialized") self.analysis = TreeAnalysis(self.tree, scorer=self.scorers[scorer]) self.routes = RouteCollection.from_analysis(self.analysis, selection)
def test_create_route_collection(setup_complete_tree, mocker): tree, nodes = setup_complete_tree analysis = TreeAnalysis(tree) mocker.patch("aizynthfinder.analysis.ReactionTree.to_dict") mocker.patch("aizynthfinder.analysis.json.dumps") routes = RouteCollection.from_analysis(analysis, 5) assert len(routes) == 3 assert routes[0]["score"] == 0.99 assert routes[0]["node"] is nodes[2] reaction_nodes = [ node for node in routes[0]["reaction_tree"].graph if isinstance(node, Reaction) ] assert len(reaction_nodes) == 2 # Just see that the code does not crash, does not verify content assert len(routes.images) == 3 assert len(routes.dicts) == 3 assert len(routes.jsons) == 3