stk.FilterMolecules

class stk.FilterMolecules(filter, selector)[source]

Bases: Selector[T]

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 = {
    stk.MoleculeRecord(
        topology_graph=stk.polymer.Linear(
            building_blocks=[
                stk.BuildingBlock('BrCCBr', stk.BromoFactory()),
            ],
            repeating_unit='A',
            num_repeating_units=2,
        ),
    ): 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
Parameters:
  • filter (Selector[T]) – Selects molecules which can be yielded by selector.

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

Methods

select

Yield batches of molecule records from population.

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

Yield batches of molecule records from population.

Parameters:
  • population (dict[T, float]) – A collection of molecules from which batches are selected.

  • included_batches (set[BatchKey] | None) – 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 (set[BatchKey] | None) – The identity keys of batches which are not allowed to be yielded. If None, no batch is forbidden from being yielded.

Yields:

A batch of selected molecule records.

Return type:

Iterator[Batch[T]]