Filter Molecules

class FilterMolecules(filter, selector)[source]

Bases: stk.ea.selection.selectors.selector.Selector

Allows a Selector to select only some molecules.

Examples

Using a Selection Algorithm on a Subset of Batches

import stk

selector = stk.FilterMolecules(
    # Use only molecules in the top 5 batches of size 3.
    filter=stk.Best(num_batches=5, batch_size=3),
    # Select from those molecules using roulette selection.
    selector=stk.Roulette(num_batches=20, batch_size=3),
)

population = tuple(
    stk.MoleculeRecord(
        topology_graph=stk.polymer.Linear(
            building_blocks=(
                stk.BuildingBlock(
                    smiles='BrCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
            ),
            repeating_unit='A',
            num_repeating_units=2,
        ),
    ).with_fitness_value(i)
    for i in range(10)
)

for batch in selector.select(population):
    # Do stuff with batch. All the molecules in the batch
    # belong to the top 5 batches of size 3. The batch
    # was selected using roulette selection.
    pass

Methods

select(population[, included_batches, ...])

Yield batches of molecule records from population.

__init__(filter, selector)[source]

Initialize a FilterMolecules instance.

Parameters
  • filter (Selector) – Selects molecules which can be yielded by selector.

  • selector (Selector) – Selects batches of molecules. The batches can only contain molecules yielded by filter.

select(population, included_batches=None, excluded_batches=None)[source]

Yield batches of molecule records from population.

Parameters
  • population (tuple of MoleculeRecord) – A collection of molecules from which batches are selected.

  • included_batches (set, optional) – The identity keys of batches which are allowed to be yielded, if None all batches can be yielded. If not None only batches included_batches will be yielded.

  • excluded_batches (class:set, optional) – The identity keys of batches which are not allowed to be yielded. If None, no batch is forbidden from being yielded.

Yields

Batch of MoleculeRecord – A batch of selected molecule records.