aizynthfinder.context.scoring package

Submodules

aizynthfinder.context.scoring.collection module

Module containing classes used to score the reaction routes.

class aizynthfinder.context.scoring.collection.ScorerCollection(config)

Bases: ContextCollection

Store scorer objects for the aizynthfinder interface.

The scorers can be obtained by name with simple indexing

scorers = ScorerCollection()
scorer = scorers['state score']

Scorers defined in this module and that does not require any other argument to initialize than the config are auto-loaded.

Parameters:

config (Configuration) – the configuration of the tree search

create_default_scorers()

Setup the scores that only need the config as their input.

Return type:

None

load(scorer, silent=False)

Add a pre-initialized scorer object to the collection

Parameters:
  • scorer (Scorer) – the item to add

  • silent (bool) – if True will not write out the name of the loaded scorer

Return type:

None

load_from_config(**scorers_config)

Load one or several scorers from a configuration dictionary

The keys are the name of scorer class. If a scorer is not defined in the aizynthfinder.context.scoring module, the module name can be appended, e.g. mypackage.scoring.AwesomeScore.

The values of the configuration is passed directly to the scorer class along with the config parameter.

Raises:

ScorerException – if module or class could not be found

Parameters:

scorers_config (Any)

Return type:

None

make_subset(subset_names)

Make a new scorer collection by taking a subset of this collection. The scorer instances will be shared between the collections

Parameters:

subset_names (List[str]) – the scorers to copy over

Returns:

the newly formed collection

Return type:

ScorerCollection

names()

Return a list of the names of all the loaded scorers

Return type:

List[str]

objects()

Return a list of all the loaded scorer objects

Return type:

List[Scorer]

score_vector(item)

For the given item, score it with all selected scorers and return a vector

Parameters:

item (_Scoreable) – the item to be scored

Returns:

the vector with the scores

Return type:

Sequence[float]

weighted_score(item, weights)

For the given item, score it with all selected scorers and return a weighted sum of all the scores.

If weights is not the same length as the number of scorers an exception is raised.

If no scorers are selected this will raise an exception

Parameters:
  • item (_Scoreable) – the item to be scored

  • weights (Sequence[float]) – the weights of the scorers

Returns:

the weighted sum

Return type:

float

aizynthfinder.context.scoring.scorers module

Module containing classes used to score the reaction routes.

class aizynthfinder.context.scoring.scorers.CombinedScorer(config, scorers, weights=None, combine_strategy='mean-arithmetic', short_name=None)

Bases: Scorer

Class for scoring nodes and reaction trees by combining weighted scores from a list of scorers

If no weights are provided as input, the scorer provides default weights that are equal for all input scorers.

The CombinedScorer cannot be instantiated from the config file as it requires the names of the scorers to combine as input.

Parameters:
  • config (Configuration)

  • scorers (Sequence[str])

  • weights (Optional[Sequence[float]])

  • combine_strategy (str)

  • short_name (Optional[str])

class aizynthfinder.context.scoring.scorers.StateScorer(config, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the state score

Parameters:
scorer_name = 'state score'
class aizynthfinder.context.scoring.scorers.RouteCostScorer(config, scaler_params=None, reaction_cost=1, average_yield=0.8, default_cost=1, not_in_stock_multiplier=10)

Bases: PriceSumScorer

Score based on the cost of molecules and reactions. From Badowski et al. Chem Sci. 2019, 10, 4640

Parameters:
  • config (Configuration)

  • scaler_params (Optional[StrDict])

  • reaction_cost (int)

  • average_yield (float)

  • default_cost (int)

  • not_in_stock_multiplier (int)

scorer_name = 'route cost'
class aizynthfinder.context.scoring.scorers.BrokenBondsScorer(config)

Bases: Scorer

Class for scoring nodes and reaction trees based on the breaking of atom bonds

The score is a summation of the depths in the tree where the focussed bonds are found to break in the reaction. If a focussed bond is found to be unbroken in the entire tree, the total length of the tree will be added to the score.

Parameters:

config (Configuration)

scorer_name = 'broken bonds'
class aizynthfinder.context.scoring.scorers.RouteSimilarityScorer(config, routes_path, model_path, scaler_params=None, agg_func='min', similarity=False)

Bases: Scorer

Class for scoring based on an LSTM model for computing Tree Edit Distance to a set of reference routes.

Parameters:
  • config (Configuration) – the configuration of the tree search

  • routes_path (str) – the filename of a JSON file with reference routes

  • model_path (str) – the filename of a checkpoint file with the LSTM model

  • scaler_params (Optional[StrDict]) – the parameter settings of the scaler

  • agg_func (str) – the name of numpy function used to aggregate the distances to the reference routes

  • similarity (bool) – if True, will compute similarity score else distance scores

scorer_name = 'route similarity'
class aizynthfinder.context.scoring.scorers.DeepSetScorer(config, deepset_model, sc_score_model, class_ranks_path, model_score_weight=0.97, length_weight=-0.43, scaler_params=None)

Bases: Scorer

Class for scoring nodes and reaction trees by a deep learning model, augmented by expert knowledge.

Wrapper for deepset_route_score from rxnutils with addition of the expert knowledge correction for route length.

The raw output of the score is capped between 0 and 20. 0 because sometimes the deepset model return negative values, even though it should be confined to >= 0 20 because anything large than 20 is useless and uninteresting, this cap could probably be removed in the future if we retrain it.

How to interpret the score: - ”Good” for scores between 0 and 5 - ”Plausible” for scores between 5 and 9 - ”Bad” for scores between 10 and 15

Parameters:
  • config (Configuration) – the configuration the tree search

  • deepset_model (str) – the path to the DeepSet model

  • sc_score_model (str) – the path to the SCScore model

  • class_ranks_path (str) – the path to a file with reaction class ranks

  • scaler_params (Optional[StrDict]) – the parameter settings of the scaler

  • model_score_weight (float)

  • length_weight (float)

scorer_name = 'expert-augmented score'

aizynthfinder.context.scoring.scorers_base module

Module containing base classes used to score routes.

class aizynthfinder.context.scoring.scorers_base.SquashScaler(slope, xoffset, yoffset)

Bases: object

Squash function loosely adapted from a sigmoid function with parameters to modify and offset the shape

Parameters:
  • slope (float) – the slope of the midpoint

  • xoffset (float) – the offset of the midpoint along the x-axis

  • yoffset (float) – the offset of the curve along the y-axis

slope: float
xoffset: float
yoffset: float
class aizynthfinder.context.scoring.scorers_base.MinMaxScaler(min_val, max_val, reverse, scale_factor=1)

Bases: object

Scaling function that normalises the value between 0 - 1, the reverse variable controls the direction of scaling, reverse should set to be true for rewards that need to be minimised the scale_factor could be used to adjust the scores when they are too small or too big

Parameters:
  • val – the value that is being scaled

  • min_val (float) – minimum val param val could take

  • max_val (float) – maximum val param val could take

  • scale_factor (float) – scaling factor applied to the minmax scaled output

  • reverse (bool)

min_val: float
max_val: float
reverse: bool
scale_factor: float = 1
class aizynthfinder.context.scoring.scorers_base.PowerScaler(base_coefficient=0.98)

Bases: object

Scaling function that returns the base_coefficient to the power of the value.

Parameters:
  • val – the value that is being scaled

  • base_coefficient (float) – coefficient for scalingm ‘base_coefficient < 1’ => reversing scale

base_coefficient: float = 0.98
class aizynthfinder.context.scoring.scorers_base.Scorer(config=None, scaler_params=None)

Bases: ABC

Abstract base class for classes that do scoring on MCTS-like nodes or reaction trees.

The actual scoring is done be calling an instance of a scorer class with a Node or ReactionTree object as only argument.

scorer = MyScorer()
score = scorer(node1)

You can also give a list of such objects to the scorer

scorer = MyScorer()
scores = scorer([node1, node2])
Parameters:
  • config (Optional[Configuration]) – the configuration the tree search

  • scaler_params (Optional[StrDict]) – the parameter settings of the scaler

scorer_name = 'base'
sort(items)

Sort nodes or reaction trees in descending order based on the score

Parameters:

items (_Scoreables) – the items to sort

Returns:

the sorted items and their scores

Return type:

Tuple[_Scoreables, Sequence[float], Sequence[int]]

aizynthfinder.context.scoring.scorers_base.make_rxnutils_route(tree)
Parameters:

tree (ReactionTree)

Return type:

SynthesisRoute

aizynthfinder.context.scoring.scorers_mols module

Module containing classes used to score the reaction routes. These scores are only based on molecules in the route.

class aizynthfinder.context.scoring.scorers_mols.FractionInStockScorer(config, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the fraction in stock

Parameters:
scorer_name = 'fraction in stock'
class aizynthfinder.context.scoring.scorers_mols.FractionInSourceStockScorer(config, source_stocks, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the fraction in stock

Parameters:
  • config (Configuration)

  • source_stocks (List[str])

  • scaler_params (Optional[StrDict])

scorer_name = 'fraction in source'
class aizynthfinder.context.scoring.scorers_mols.FractionOfIntermediatesInStockScorer(config, stock_name, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the fraction of intermediates in stock

Parameters:
  • config (Configuration)

  • stock_name (str)

  • scaler_params (Optional[StrDict])

class aizynthfinder.context.scoring.scorers_mols.NumberOfPrecursorsScorer(config=None, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the number of pre-cursors in a node or route

Parameters:
  • config (Optional[Configuration])

  • scaler_params (Optional[StrDict])

scorer_name = 'number of pre-cursors'
class aizynthfinder.context.scoring.scorers_mols.NumberOfPrecursorsInStockScorer(config, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the number of pre-cursors in stock a node or route

Parameters:
scorer_name = 'number of pre-cursors in stock'
class aizynthfinder.context.scoring.scorers_mols.PriceSumScorer(config, scaler_params=None, default_cost=1.0, not_in_stock_multiplier=10)

Bases: Scorer

Scorer that sums the prices of all pre-cursors

Parameters:
  • config (Configuration)

  • scaler_params (Optional[StrDict])

  • default_cost (float)

  • not_in_stock_multiplier (int)

scorer_name = 'sum of prices'
class aizynthfinder.context.scoring.scorers_mols.StockAvailabilityScorer(config, source_score, default_score=0.1, other_source_score=None)

Bases: Scorer

Scorer that computes score based on the stock availability of the starting material

The score is calculated as a product of a stock score per starting material. The stock score for each molecule is based on the source of the stock, or a default value if the molecule is not in stock. The other_source_score parameter can be used to distinguish between “not in stock” and “not in the specificed sources” cases.

Parameters:
  • config (Configuration)

  • source_score (StrDict)

  • default_score (float)

  • other_source_score (Optional[float])

class aizynthfinder.context.scoring.scorers_mols.DeltaSyntheticComplexityScorer(config, sc_score_model, scaler_params=None, horizon=3)

Bases: Scorer

Class for scoring nodes based on the delta-synthetic-complexity of the node and its parent ‘horizon’ steps up in the tree.

Parameters:
  • config (Configuration) – the configuration the tree search

  • sc_score_model (str) – the path to the SCScore model

  • scaler_params (Optional[StrDict]) – the parameter settings of the scaler, defaults to max-min between -1.5 and 4

  • horizon (int) – the number of steps backwards to look for parent molecule

scorer_name: str = 'delta-SC score'
sc_deltas(mols, parents)

Calculate delta SC-score among the list of mol-parent pairs.

Params mols:

the leaves of the tree

Parameters:
  • parents (_Molecules) – the parent of the leaves

  • mols (_Molecules)

Returns:

the pair-wise difference in SCScore

Return type:

Sequence[float]

aizynthfinder.context.scoring.scorers_reactions module

Module containing classes used to score the reaction routes. These scores are only based on reactions in the route.

class aizynthfinder.context.scoring.scorers_reactions.MaxTransformScorer(config=None, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the maximum transform

Parameters:
  • config (Optional[Configuration])

  • scaler_params (Optional[StrDict])

scorer_name = 'max transform'
class aizynthfinder.context.scoring.scorers_reactions.NumberOfReactionsScorer(config=None, scaler_params=None)

Bases: Scorer

Class for scoring nodes based on the number of reaction it took to get to a node

Parameters:
  • config (Optional[Configuration])

  • scaler_params (Optional[StrDict])

scorer_name = 'number of reactions'
class aizynthfinder.context.scoring.scorers_reactions.AverageTemplateOccurrenceScorer(config=None, scaler_params=None)

Bases: Scorer

Class for scoring the nodes based on the average occurrence of the templates used to get to a node

Parameters:
  • config (Optional[Configuration])

  • scaler_params (Optional[StrDict])

scorer_name = 'average template occurrence'
class aizynthfinder.context.scoring.scorers_reactions.ReactionClassMembershipScorer(config, reaction_class_set, in_set_score=1.0, not_in_set_score=0.1)

Bases: Scorer

Scorer that checks if the reaction classes are in a specified set

The score is calculated as product over each reaction. For each reaction the reaction classification is checked if it is in a given list of classes.

Parameters:
  • config (Configuration)

  • reaction_class_set (Union[str, List[str]])

  • in_set_score (float)

  • not_in_set_score (float)

class aizynthfinder.context.scoring.scorers_reactions.ReactionClassRankScorer(config, class_ranks_path, preferred_classes_path, non_preferred_factor=0.25, scaler_params=None)

Bases: Scorer

Class for scoring nodes and reaction trees by a weighted product of the ranks of the reaction classes in the route.

Wrapper for reaction_class_rank_score from rxnutils

Parameters:
  • config (Configuration) – the configuration the tree search

  • class_ranks_path (str) – the path to a file with reaction class ranks

  • preferred_classes_path (str) – the path to a file with preferred reaction classes

  • non_preferred_factor (float) – a penalty factor for non-preferred class

  • scaler_params (Optional[StrDict]) – the parameter settings of the scaler

scorer_name = 'reaction class-rank score'

Module contents

Sub-package containing scoring routines