Example #1
0
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
Example #2
0
def test_state_scorer_tree(load_reaction_tree, default_config, mock_stock):
    mock_stock(default_config, "N#Cc1cccc(N)c1F", "O=C(Cl)c1ccc(F)cc1",
               "CN1CCC(Cl)CC1", "O")
    tree = ReactionTree.from_dict(load_reaction_tree("sample_reaction.json"))
    scorer = StateScorer(default_config)

    assert round(scorer(tree), 4) == 0.994
Example #3
0
def test_add_scorer_to_collection(default_config):
    collection = ScorerCollection(default_config)
    del collection["state score"]

    collection.load(StateScorer(default_config))

    assert "state score" in collection.names()
Example #4
0
def test_state_scorer_trees(default_config, setup_linear_reaction_tree):
    rt = setup_linear_reaction_tree()
    scorer = StateScorer(default_config)

    scores = scorer([rt, rt])

    assert round(scores[0], 4) == 0.994
    assert round(scores[1], 4) == 0.994
Example #5
0
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
Example #6
0
    def __init__(self, search_tree, scorer=None):
        self.search_tree = search_tree
        if scorer is None:
            # Do import here to avoid circular imports
            from aizynthfinder.context.scoring import StateScorer

            self.scorer = StateScorer()
        else:
            self.scorer = scorer
Example #7
0
def test_state_scorer_nodes(generate_root, default_config):
    root = generate_root("CCCCOc1ccc(CC(=O)N(C)O)cc1")
    scorer = StateScorer(default_config)

    scores = scorer([root, root])

    assert repr(scorer) == "state score"
    assert round(scores[0], 4) == 0.0491
    assert round(scores[1], 4) == 0.0491
Example #8
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
Example #9
0
def test_sort(default_config, setup_linear_mcts, setup_branched_mcts):
    _, node1 = setup_linear_mcts()
    _, node2 = setup_branched_mcts()
    scorer = StateScorer(default_config)

    sorted_nodes, scores, _ = scorer.sort([node2, node1])

    assert [np.round(score, 4) for score in scores] == [0.994, 0.9866]
    assert sorted_nodes == [node1, node2]
Example #10
0
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
Example #11
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
Example #12
0
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
Example #13
0
 def __init__(
     self,
     search_tree: Union[MctsSearchTree, AndOrSearchTreeBase],
     scorer: Scorer = None,
 ) -> None:
     self.search_tree = search_tree
     if scorer is None:
         self.scorer: Scorer = StateScorer(search_tree.config)
     else:
         self.scorer = scorer
Example #14
0
def test_state_scorer_nodes(setup_linear_mcts, setup_branched_mcts,
                            default_config):
    _, node1 = setup_linear_mcts()
    _, node2 = setup_branched_mcts()
    scorer = StateScorer(default_config)

    scores = scorer([node1, node2])

    assert repr(scorer) == "state score"
    assert round(scores[0], 4) == 0.994
    assert round(scores[1], 4) == 0.9866
Example #15
0
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
Example #16
0
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
Example #17
0
def test_sort(shared_datadir, default_config, mock_stock):
    mock_stock(default_config, "CCCO", "CC")
    search_tree = SearchTree.from_json(
        shared_datadir / "tree_without_repetition.json", default_config)
    nodes = list(search_tree.graph())
    scorer = StateScorer(default_config)

    sorted_nodes, scores, _ = scorer.sort(nodes)

    assert [np.round(score, 4) for score in scores] == [0.9976, 0.0491]
    assert sorted_nodes == [nodes[1], nodes[0]]
Example #18
0
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
Example #19
0
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
Example #20
0
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
Example #21
0
def test_state_scorer_node(default_config, setup_linear_mcts):
    _, node = setup_linear_mcts()
    scorer = StateScorer(default_config)

    assert repr(scorer) == "state score"
    assert round(scorer(node), 4) == 0.994
Example #22
0
def test_state_scorer_tree(default_config, setup_linear_reaction_tree):
    tree = setup_linear_reaction_tree()
    scorer = StateScorer(default_config)

    assert round(scorer(tree), 4) == 0.994
Example #23
0
def test_state_scorer_node(generate_root, default_config):
    root = generate_root("CCCCOc1ccc(CC(=O)N(C)O)cc1")
    scorer = StateScorer(default_config)

    assert repr(scorer) == "state score"
    assert round(scorer(root), 4) == 0.0491