def test_sort_nodes_scorer(setup_analysis): analysis, _ = setup_analysis(scorer=NumberOfReactionsScorer()) best_nodes, best_scores = analysis.sort_nodes() assert len(best_nodes) == 10 assert best_scores[0] == 2
def test_scoring_branch_mcts_tree_in_stock(shared_datadir, 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", ) search_tree = SearchTree.from_json( shared_datadir / "tree_with_branching.json", default_config) nodes = list(search_tree.graph()) assert pytest.approx(StateScorer(default_config)(nodes[-1]), abs=1e-3) == 0.950 assert NumberOfReactionsScorer()(nodes[-1]) == 14 assert NumberOfPrecursorsScorer(default_config)(nodes[-1]) == 8 assert NumberOfPrecursorsInStockScorer(default_config)(nodes[-1]) == 8 assert PriceSumScorer(default_config)(nodes[-1]) == 8 cost_score = RouteCostScorer(default_config)(nodes[-1]) assert pytest.approx(cost_score, abs=1e-3) == 77.4797
def test_number_of_reaction_scorer_node(shared_datadir, default_config): search_tree = SearchTree.from_json( shared_datadir / "tree_without_repetition.json", default_config) nodes = list(search_tree.graph()) scorer = NumberOfReactionsScorer() assert scorer(nodes[1]) == 1
def test_sort_routes(setup_analysis_andor_tree): analysis = setup_analysis_andor_tree(scorer=NumberOfReactionsScorer()) best_routes, best_scores = analysis.sort() assert len(best_routes) == 3 assert best_scores[0] == 1
def test_scoring_branched_route(load_reaction_tree, default_config): tree = ReactionTree.from_dict(load_reaction_tree("branched_route.json")) assert pytest.approx(StateScorer(default_config)(tree), abs=1e-6) == 0.00012363 assert NumberOfReactionsScorer(default_config)(tree) == 14 assert NumberOfPrecursorsScorer(default_config)(tree) == 8 assert NumberOfPrecursorsInStockScorer(default_config)(tree) == 0
def test_scoring_branched_mcts_tree(shared_datadir, default_config): search_tree = SearchTree.from_json( shared_datadir / "tree_with_branching.json", default_config) nodes = list(search_tree.graph()) assert pytest.approx(StateScorer()(nodes[-1]), abs=1e-6) == 0.00012363 assert NumberOfReactionsScorer()(nodes[-1]) == 14 assert NumberOfPrecursorsScorer(default_config)(nodes[-1]) == 8 assert NumberOfPrecursorsInStockScorer(default_config)(nodes[-1]) == 0
def test_rescore_collection_for_trees(default_config, setup_linear_reaction_tree): rt = setup_linear_reaction_tree() routes = RouteCollection(reaction_trees=[rt]) routes.compute_scores(StateScorer(default_config)) routes.rescore(NumberOfReactionsScorer()) assert routes.scores[0] == 2 assert np.round(routes.all_scores[0]["state score"], 3) == 0.994 assert routes.all_scores[0]["number of reactions"] == 2
def test_scoring_branched_route(default_config, setup_branched_reaction_tree): tree = setup_branched_reaction_tree() assert pytest.approx(StateScorer(default_config)(tree), abs=1e-4) == 0.9866 assert NumberOfReactionsScorer()(tree) == 4 assert NumberOfPrecursorsScorer(default_config)(tree) == 5 assert NumberOfPrecursorsInStockScorer(default_config)(tree) == 5 assert PriceSumScorer(default_config)(tree) == 5 cost_score = RouteCostScorer(default_config)(tree) assert pytest.approx(cost_score, abs=1e-4) == 13.6563
def test_scorers_one_mcts_node(default_config): tree = SearchTree(default_config, root_smiles="CCCCOc1ccc(CC(=O)N(C)O)cc1") node = tree.root assert pytest.approx(StateScorer(default_config)(node), abs=1e-3) == 0.0497 assert NumberOfReactionsScorer(default_config)(node) == 0 assert NumberOfPrecursorsScorer(default_config)(node) == 1 assert NumberOfPrecursorsInStockScorer(default_config)(node) == 0 assert PriceSumScorer(default_config)(node) == 10 assert RouteCostScorer(default_config)(node) == 10
def test_scoring_branched_route_not_in_stock(default_config, setup_branched_reaction_tree): tree = setup_branched_reaction_tree("O") assert pytest.approx(StateScorer(default_config)(tree), abs=1e-4) == 0.7966 assert NumberOfReactionsScorer()(tree) == 4 assert NumberOfPrecursorsScorer(default_config)(tree) == 5 assert NumberOfPrecursorsInStockScorer(default_config)(tree) == 4 assert PriceSumScorer(default_config)(tree) == 14 cost_score = RouteCostScorer(default_config)(tree) assert pytest.approx(cost_score, abs=1e-4) == 31.2344
def test_scorers_tree_one_node_route(default_config): tree = ReactionTree() tree.root = UniqueMolecule(smiles="CCCCOc1ccc(CC(=O)N(C)O)cc1") tree.graph.add_node(tree.root) assert pytest.approx(StateScorer(default_config)(tree), abs=1e-3) == 0.0497 assert NumberOfReactionsScorer(default_config)(tree) == 0 assert NumberOfPrecursorsScorer(default_config)(tree) == 1 assert NumberOfPrecursorsInStockScorer(default_config)(tree) == 0 assert PriceSumScorer(default_config)(tree) == 10 assert RouteCostScorer(default_config)(tree) == 10
def test_rescore_collection_for_trees(default_config, mock_stock, load_reaction_tree): mock_stock(default_config, "N#Cc1cccc(N)c1F", "O=C(Cl)c1ccc(F)cc1", "CN1CCC(Cl)CC1", "O") rt = ReactionTree.from_dict(load_reaction_tree("sample_reaction.json")) routes = RouteCollection(reaction_trees=[rt]) routes.compute_scores(StateScorer(default_config)) routes.rescore(NumberOfReactionsScorer()) assert routes.scores[0] == 2 assert np.round(routes.all_scores[0]["state score"], 3) == 0.994 assert routes.all_scores[0]["number of reactions"] == 2
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 test_compute_new_score_for_trees(default_config, setup_linear_reaction_tree): rt = setup_linear_reaction_tree() routes = RouteCollection(reaction_trees=[rt]) assert routes.nodes[0] is None assert routes.scores[0] is np.nan assert routes.all_scores[0] == {} routes.compute_scores(StateScorer(default_config), NumberOfReactionsScorer()) assert routes.scores[0] is np.nan assert np.round(routes.all_scores[0]["state score"], 3) == 0.994 assert routes.all_scores[0]["number of reactions"] == 2
def test_tree_statistics_andor_tree(setup_analysis_andor_tree): analysis = setup_analysis_andor_tree(scorer=NumberOfReactionsScorer()) stats = analysis.tree_statistics() assert stats["number_of_nodes"] == 9 assert stats["max_transforms"] == 2 assert stats["max_children"] == 2 assert stats["number_of_routes"] == 3 assert stats["number_of_solved_routes"] == 3 assert stats["top_score"] == 1 assert stats["is_solved"] assert stats["number_of_steps"] == 1 assert stats["number_of_precursors"] == 2 assert stats["number_of_precursors_in_stock"] == 2 mol_str = "Cc1ccc2nc3ccccc3c(Cl)c2c1, Nc1ccc(NC(=S)Nc2ccccc2)cc1" assert stats["precursors_in_stock"] == mol_str assert stats["precursors_not_in_stock"] == "" assert stats["policy_used_counts"] == {}
def test_tree_statistics(setup_analysis): analysis, _ = setup_analysis(scorer=NumberOfReactionsScorer()) stats = analysis.tree_statistics() assert stats["number_of_nodes"] == 61 assert stats["max_transforms"] == 7 assert stats["max_children"] == 10 assert stats["number_of_leafs"] == 10 assert stats["number_of_solved_leafs"] == 1 assert stats["top_score"] == 2 assert stats["is_solved"] assert stats["number_of_steps"] == 2 assert stats["number_of_precursors"] == 4 assert stats["number_of_precursors_in_stock"] == 4 mol_str = "CN1CCC(Cl)CC1, O, N#Cc1cccc(N)c1F, O=C(Cl)c1ccc(F)cc1" assert stats["precursors_in_stock"] == mol_str assert stats["precursors_not_in_stock"] == "" assert stats["policy_used_counts"] == {}
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_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_best_route(setup_analysis_andor_tree): analysis = setup_analysis_andor_tree(scorer=NumberOfReactionsScorer()) assert len(list(analysis.best().reactions())) == 1
def test_best_node(setup_analysis): analysis, nodes = setup_analysis(scorer=NumberOfReactionsScorer()) assert analysis.best_node() is nodes[51]