aizynthfinder.utils package

Submodules

aizynthfinder.utils.bonds module

Module containing a class to identify broken focussed bonds

class aizynthfinder.utils.bonds.BrokenBonds(focussed_bonds)

Bases: object

A class to keep track of focussed bonds breaking in a target molecule.

Parameters:

focussed_bonds (Sequence[Sequence[int]]) – A list of focussed bond pairs. The bond pairs are represented as tuples of size 2. These bond pairs exist in the target molecule’s atom bonds.

aizynthfinder.utils.bonds.sort_bonds(bonds)
Parameters:

bonds (Sequence[Sequence[int]])

Return type:

List[Tuple[int, int]]

aizynthfinder.utils.exceptions module

Module containing custom exception classes

exception aizynthfinder.utils.exceptions.CostException

Bases: Exception

Exception raised by molecule cost classes

exception aizynthfinder.utils.exceptions.ExternalModelAPIError

Bases: Exception

Custom error type to signal failure in External model

exception aizynthfinder.utils.exceptions.MoleculeException

Bases: Exception

An exception that is raised by molecule class

exception aizynthfinder.utils.exceptions.NodeUnexpectedBehaviourException

Bases: Exception

Exception that is raised if the tree search is behaving unexpectedly.

exception aizynthfinder.utils.exceptions.PolicyException

Bases: Exception

An exception raised by policy classes

exception aizynthfinder.utils.exceptions.RejectionException

Bases: Exception

An exception raised if a retro action should be rejected

exception aizynthfinder.utils.exceptions.ScorerException

Bases: Exception

Exception raised by scoring classes

exception aizynthfinder.utils.exceptions.StockException

Bases: Exception

An exception raised by stock classes

exception aizynthfinder.utils.exceptions.TreeAnalysisException

Bases: Exception

Exception raised when analysing trees

aizynthfinder.utils.files module

Module containing routines to work with files and processes.

aizynthfinder.utils.files.read_datafile(filename)

Read aizynth output from disc in either .hdf5 or .json format

Parameters:

filename (Union[str, Path]) – the path to the data

Returns:

the loaded data

Return type:

pd.DataFrame

aizynthfinder.utils.files.save_datafile(data, filename)

Save the given data to disc in either .hdf5 or .json format

Parameters:
  • data (pd.DataFrame) – the data to save

  • filename (Union[str, Path]) – the path to the data

Return type:

None

aizynthfinder.utils.files.cat_hdf_files(input_files, output_name, trees_name=None)

Concatenate hdf5 or json datafiles

Parameters:
  • input_files (List[str])

  • output_name (str)

  • trees_name (Optional[str])

Return type:

None

aizynthfinder.utils.files.cat_datafiles(input_files, output_name, trees_name=None)

Concatenate hdf5 or json datafiles

if tree_name is given, will take out the trees column from the tables and save it to a gzipped-json file.

Parameters:
  • input_files (List[str]) – the paths to the files to concatenate

  • output_name (str) – the name of the concatenated file

  • trees_name (Optional[str]) – the name of the concatenated trees

Return type:

None

aizynthfinder.utils.files.split_file(filename, nparts)

Split the content of a text file into a given number of temporary files

Parameters:
  • filename (str) – the path to the file to split

  • nparts (int) – the number of parts to create

Returns:

list of filenames of the parts

Return type:

List[str]

aizynthfinder.utils.files.start_processes(inputs, log_prefix, cmd_callback, poll_freq=5)

Start a number of background processes and wait for them to complete.

The standard output and standard error is saved to a log file.

The command to start for each process is given by the cmd_callback function that takes the index of the process and an item of the input as arguments.

Parameters:
  • inputs (Sequence[Any]) – a sequence of input to the processes

  • log_prefix (str) – the prefix to the log file of each processes

  • cmd_callback (Callable) – function that creates the process commands

  • poll_freq (int) – the polling frequency for checking if processes are completed

Return type:

None

aizynthfinder.utils.image module

This module contains a collection of routines to produce pretty images

aizynthfinder.utils.image.molecule_to_image(mol, frame_color, size=300)

Create a pretty image of a molecule, with a colored frame around it

Parameters:
  • mol (Molecule) – the molecule

  • frame_color (PilColor) – the color of the frame

  • size (int) – the size of the image

Returns:

the produced image

Return type:

PilImage

aizynthfinder.utils.image.molecules_to_images(mols, frame_colors, size=300)

Create pretty images of molecules with a colored frame around each one of them.

The molecules will be resized to be of similar sizes.

Parameters:
  • mols (Sequence[Molecule]) – the molecules

  • frame_colors (Sequence[PilColor]) – the color of the frame for each molecule

  • size (int) – the sub-image size

Returns:

the produced images

Return type:

List[PilImage]

aizynthfinder.utils.image.crop_image(img, margin=20)

Crop an image by removing white space around it

Parameters:
  • img (PilImage) – the image to crop

  • margin (int) – padding, defaults to 20

Returns:

the cropped image

Return type:

PilImage

aizynthfinder.utils.image.draw_rounded_rectangle(img, color, arc_size=20)

Draw a rounded rectangle around an image

Parameters:
  • img (PilImage) – the image to draw upon

  • color (PilColor) – the color of the rectangle

  • arc_size (int) – the size of the corner, defaults to 20

Returns:

the new image

Return type:

PilImage

aizynthfinder.utils.image.save_molecule_images(molecules, frame_colors, size=300)

Create images of a list of molecules and save them to disc a globally managed folder.

Parameters:
  • molecules (Sequence[Molecule]) – the molecules to save as images

  • frame_colors (Sequence[PilColor]) – the color of the frame around each image

  • size (int) – the sub-image size for each molecule

Returns:

the filename of the created images

Return type:

Dict[Molecule, str]

aizynthfinder.utils.image.make_visjs_page(filename, molecules, reactions, edges, frame_colors, hierarchical=False)

Create HTML code of a bipartite graph of molecules and reactions using the vis.js network library.

Package the created HTML page and all images as tar-ball.

Parameters:
  • filename (str) – the basename of the archive

  • molecules (Sequence[Molecule]) – the molecules nodes

  • reactions (Sequence[FixedRetroReaction]) – the reaction nodes

  • edges (Union[Sequence[Tuple[Any, Any]], nx.digraph.OutEdgeView]) – the edges of the graph

  • frame_colors (Sequence[PilColor]) – the color of the frame around each image

  • hierarchical (bool) – if True, will produce a hierarchical layout

Return type:

None

class aizynthfinder.utils.image.RouteImageFactory(route, in_stock_colors=None, show_all=True, margin=100)

Bases: object

Factory class for drawing a route

Parameters:
  • route (StrDict) – the dictionary representation of the route

  • in_stock_colors (FrameColors) – the colors around molecules, defaults to {True: “green”, False: “orange”}

  • show_all (bool) – if True, also show nodes that are marked as hidden

  • margin (int) – the margin between images

aizynthfinder.utils.loading module

Module containing routine to dynamically load a class from a specification

aizynthfinder.utils.loading.load_dynamic_class(name_spec, default_module=None, exception_cls=<class 'ValueError'>)

Load an object from a dynamic specification.

The specification can be either:

ClassName, in-case the module name is taken from the default_module argument

or

package_name.module_name.ClassName, in-case the module is taken as package_name.module_name

Parameters:
  • name_spec (str) – the class specification

  • default_module (Optional[str]) – the default module

  • exception_cls (Any) – the exception class to raise on exception

Returns:

the loaded class

Return type:

Any

aizynthfinder.utils.logging module

Module containing routines to setup proper logging

aizynthfinder.utils.logging.logger()

Returns the logger that should be used by all classes

Returns:

the logger object

Return type:

Logger

aizynthfinder.utils.logging.setup_logger(console_level, file_level=None)

Setup the logger that should be used by all classes

The logger configuration is read from the logging.yml file.

Parameters:
  • console_level (int) – the level of logging to the console

  • file_level (int | None) – the level of logging to file, if not set logging to file is disabled, default to None

Returns:

the logger object

Return type:

Logger

aizynthfinder.utils.math module

Module containing diverse math functions, including neural network-related functions.

aizynthfinder.utils.math.dense_layer_forward_pass(x, weights, bias, activation)

Forward pass through a dense neural network layer. :param x: layer input :param weights: layer weights :param bias: layer bias :param activation: layer activation function :return: the layer output

Parameters:
  • x (ndarray)

  • weights (ndarray)

  • bias (ndarray)

  • activation (Callable)

Return type:

ndarray

aizynthfinder.utils.math.rectified_linear_unit(x)

ReLU activation function

Parameters:

x (ndarray)

Return type:

ndarray

aizynthfinder.utils.math.sigmoid(x)

Sigmoid activation function

Parameters:

x (ndarray)

Return type:

ndarray

aizynthfinder.utils.math.softmax(x)

Compute softmax values for each sets of scores in x.

Parameters:

x (ndarray)

Return type:

ndarray

aizynthfinder.utils.models module

Module containing helper routines for using Keras, Tensorflow and Onnx models

aizynthfinder.utils.models.load_model(source, key, use_remote_models)

Load model from a configuration specification.

If use_remote_models is True, tries to load:
  1. A Tensorflow server through gRPC

  2. A Tensorflow server through REST API

  3. A local Keras model

otherwise it just loads the local model.

Parameters:
  • source (str) – if fallbacks to a local model, this is the filename

  • key (str) – when connecting to Tensorflow server this is the model name

  • use_remote_models (bool) – if True will try to connect to remote model server

Returns:

a model object with a predict object

Return type:

Union[‘LocalKerasModel’, ‘LocalOnnxModel’, ‘ExternalModelViaGRPC’, ‘ExternalModelViaREST’]

class aizynthfinder.utils.models.LocalKerasModel(filename)

Bases: object

A keras policy model that is executed locally.

The size of the input vector can be determined with the len() method.

Variables:
  • model – the compiled model

  • output_size – the length of the output vector

Parameters:

filename (str) – the path to a Keras checkpoint file

predict(*args, **_)

Perform a forward pass of the neural network.

Parameters:
  • args (ndarray) – the input vectors

  • _ (ndarray)

Returns:

the vector of the output layer

Return type:

ndarray

class aizynthfinder.utils.models.LocalOnnxModel(filename)

Bases: object

An Onnx model that is executed locally.

The size of the input vector can be determined with the len() method.

Variables:
  • model – the compiled Onnx model

  • output_size – the length of the output vector

Parameters:

filename (str) – the path to a Onnx model checkpoint file

predict(*args, **_)

Perform a prediction run on the onnx model.

Parameters:
  • args (ndarray) – the input vectors

  • _ (ndarray)

Returns:

the vector of the output layer

Return type:

ndarray

class aizynthfinder.utils.models.ExternalModelViaREST(name)

Bases: object

A neural network model implementation using TF Serving via REST API.

Parameters:

name (str) – the name of model

predict(*args, **kwargs)

Get prediction from model.

If the keys in kwargs agree with the model input names, they will be used in stead of arg

Parameters:
  • args (ndarray) – the input vectors

  • kwargs (ndarray) – the named input vectors

Returns:

the vector of the output layer

Return type:

ndarray

class aizynthfinder.utils.models.ExternalModelViaGRPC(name)

Bases: object

A neural network model implementation using TF Serving via gRPC.

Parameters:

name (str) – the name of model

predict(*args, **kwargs)

Get prediction from model.

If the keys in kwargs agree with the model input names, they will be used in stead of arg

Parameters:
  • args (ndarray) – the input vectors

  • kwargs (ndarray) – the named input vectors

Returns:

the vector of the output layer

Return type:

ndarray

aizynthfinder.utils.mongo module

Module containing routines to obtain a MongoClient instance

aizynthfinder.utils.mongo.get_mongo_client(host='localhost', port=27017, user=None, password=None, tls_certs_path='')

A helper function to create and reuse MongoClient

The client is only setup once. Therefore if this function is called a second time with different parameters, it would still return the first client.

Parameters:
  • host (str) – the host

  • port (int) – the host port

  • user (str | None) – username, defaults to None

  • password (str | None) – password, defaults to None

  • tls_certs_path (str) – the path to TLS certificates if to be used, defaults to “”

Raises:

ValueError – if host and port is not given first time

Returns:

the MongoDB client

Return type:

MongoClient | None

aizynthfinder.utils.paths module

Module containing routines for returning package paths

aizynthfinder.utils.paths.package_path()

Return the path to the package

Return type:

str

aizynthfinder.utils.paths.data_path()

Return the path to the data directory of the package

Return type:

str

aizynthfinder.utils.sc_score module

Module containing the implementation of the SC-score model for synthetic complexity scoring.

class aizynthfinder.utils.sc_score.SCScore(model_path, fingerprint_length=1024, fingerprint_radius=2)

Bases: object

Encapsulation of the SCScore model

Re-write of the SCScorer from the scscorer package

The predictions of the score is made with a sanitized instance of an RDKit molecule

mol = Molecule(smiles="CCC", sanitize=True)
scscorer = SCScorer("path_to_model")
score = scscorer(mol.rd_mol)

The model provided when creating the scorer object should be pickled tuple. The first item of the tuple should be a list of the model weights for each layer. The second item of the tuple should be a list of the model biases for each layer.

Parameters:
  • model_path (str) – the filename of the model weights and biases

  • fingerprint_length (int) – the number of bits in the fingerprint

  • fingerprint_radius (int) – the radius of the fingerprint

forward(x)

Forward pass with dense neural network

Parameters:

x (ndarray)

Return type:

ndarray

aizynthfinder.utils.type_utils module

Module containing all types and type imports

Module contents