def test_adding_workflow_components(factory_type): """ Test building workflows from a verity of workflow components. """ factory = factory_type() # element filter efilter = workflow_components.ElementFilter() factory.add_workflow_component(efilter) assert len(factory.workflow) == 1 # conformer generator conformer_gen = workflow_components.StandardConformerGenerator() conformer_gen.max_conformers = 200 factory.add_workflow_component(conformer_gen) assert len(factory.workflow) == 2 # add element filter again and make sure the component name has been incremented factory.add_workflow_component(efilter) assert len(factory.workflow) == 3 assert efilter.component_name in factory.workflow # try to add a non component with pytest.raises(InvalidWorkflowComponentError): factory.add_workflow_component(3) with pytest.raises(ValidationError): factory.workflow = {"first component": 3} factory.workflow = {"test_conformer": conformer_gen} assert len(factory.workflow) == 1
def test_conformer_apply(toolkit): """ Test applying the standard conformer generator to a workflow. """ toolkit_name, toolkit_class = toolkit if toolkit_class.is_available(): conf_gen = workflow_components.StandardConformerGenerator() conf_gen.toolkit = toolkit_name conf_gen.max_conformers = 1 conf_gen.clear_existing = True mols = get_stereoisomers() # remove duplicates from the set molecule_container = get_container(mols) result = conf_gen.apply(molecule_container.molecules, processors=1) assert result.component_name == conf_gen.component_name assert result.component_description == conf_gen.dict() # make sure each molecule has a conformer that passed for molecule in result.molecules: assert molecule.n_conformers == 1, print(molecule.conformers) for molecule in result.filtered: assert molecule.n_conformers == 0 else: pytest.skip(f"Toolkit {toolkit_name} not available.")
def test_exporting_settings_workflow(file_type, factory_type): """ Test exporting the settings and a workflow to the different file types. """ with temp_directory(): factory = factory_type() changed_attrs = { "maxiter": 400, "priority": "super_high", "compute_tag": "test tag" } for attr, value in changed_attrs.items(): setattr(factory, attr, value) conformer_gen = workflow_components.StandardConformerGenerator() conformer_gen.max_conformers = 100 factory.add_workflow_component(conformer_gen) file_name = "test." + file_type factory.export_settings(file_name=file_name) with open(file_name) as f: data = f.read() assert conformer_gen.component_name in data assert str(conformer_gen.max_conformers) in data
def test_standardconformer_generator_validators(toolkit): """ Test the standard conformer generator which calls the OFFTK. """ toolkit_name, toolkit_class = toolkit if toolkit_class.is_available(): conf_gen = workflow_components.StandardConformerGenerator() # make sure we are casting to ints conf_gen.max_conformers = 1.02 assert conf_gen.max_conformers == 1 # test the toolkit validator with pytest.raises(ValueError): conf_gen.toolkit = "ambertools" conf_gen.toolkit = toolkit_name assert toolkit_name in conf_gen.provenance() conf_gen.clear_existing = "no" assert conf_gen.clear_existing is False assert "OpenforcefieldToolkit" in conf_gen.provenance() else: pytest.skip(f"Toolkit {toolkit} not available.")
def test_get_component(): """ Make sure the correct component is returned when requested. """ gen = get_component("standardconformergenerator") assert gen == workflow_components.StandardConformerGenerator()
def test_register_component_replace(): """ Test registering a component that is already registered with and without using replace. """ # get the standard conformer generator gen = workflow_components.StandardConformerGenerator(max_conformers=1) with pytest.raises(ComponentRegisterError): register_component(component=gen, replace=False) # now register using replace with a new default register_component(component=gen, replace=True) gen2 = get_component(gen.component_name) assert gen2.max_conformers == gen.max_conformers # now return th list back to normal register_component(component=workflow_components.StandardConformerGenerator(), replace=True)
def test_adding_multipule_workflow_components(factory_type): """ Test adding a list of workflow components. """ factory = factory_type() efilter = workflow_components.ElementFilter() weight = workflow_components.MolecularWeightFilter() conformer = workflow_components.StandardConformerGenerator() components = [efilter, weight, conformer] factory.add_workflow_component(components) assert len(factory.workflow) == 3 for component in components: assert component.component_name in factory.workflow
def test_get_wrokflow_component(factory_type): """ Test retrieving a workflow component. """ factory = factory_type() efilter = workflow_components.ElementFilter() weight = workflow_components.MolecularWeightFilter() conformer = workflow_components.StandardConformerGenerator() components = [efilter, weight, conformer] factory.add_workflow_component(components) for component in components: assert factory.get_workflow_component( component.component_name) == component
def test_create_dataset(factory_dataset_type): """ Test making a the correct corresponding dataset type from a given factory type. """ factory = factory_dataset_type[0]() element_filter = workflow_components.ElementFilter() element_filter.allowed_elements = [1, 6, 8, 7] factory.add_workflow_component(element_filter) conformer_generator = workflow_components.StandardConformerGenerator( max_conformers=1) factory.add_workflow_component(conformer_generator) mols = Molecule.from_file(get_data("tautomers_small.smi"), "smi", allow_undefined_stereo=True) # set some settings changed_attrs = { "compute_tag": "test tag", "dataset_tags": ["openff", "test"], "maxiter": 400 } for attr, value in changed_attrs.items(): setattr(factory, attr, value) dataset = factory.create_dataset(dataset_name="test name", molecules=mols, description="Force field test", tagline="A test dataset") # check the attributes were changed for attr, value in changed_attrs.items(): assert getattr(dataset, attr) == value assert dataset.dataset_name == "test name" assert isinstance(dataset, factory_dataset_type[1]) is True # make sure molecules we filtered and passed assert dataset.dataset != {} assert dataset.filtered != {} assert element_filter.component_name in dataset.filtered_molecules
def test_remove_workflow_componet(factory_type): """ Test removing a workflow component through the API. """ factory = factory_type() efilter = workflow_components.ElementFilter() weight = workflow_components.MolecularWeightFilter() conformer = workflow_components.StandardConformerGenerator() components = [efilter, weight, conformer] factory.add_workflow_component(components) assert len(factory.workflow) == 3 for component in components: factory.remove_workflow_component(component.component_name) assert factory.workflow == {}
def test_export_workflow_only(file_type, factory_type): """ Test exporting the workflow only from the factory. """ with temp_directory(): factory = factory_type() conformer_gen = workflow_components.StandardConformerGenerator() conformer_gen.max_conformers = 100 factory.add_workflow_component(conformer_gen) file_name = "workflow." + file_type factory.export_workflow(file_name) with open(file_name) as workflow: data = workflow.read() assert "method" not in data assert "basis" not in data assert "tag" not in data
def test_clear_workflow(factory_type): """ Test clearing out the workflow. """ factory = factory_type() efilter = workflow_components.ElementFilter() weight = workflow_components.MolecularWeightFilter() conformer = workflow_components.StandardConformerGenerator() components = [efilter, weight, conformer] factory.add_workflow_component(components) factory.clear_workflow() assert factory.workflow == {} factory.add_workflow_component(components) factory.workflow = {} assert factory.workflow == {}
#!/usr/bin/env python3 import sys from openforcefield.topology import Molecule from qcsubmit import \ workflow_components # load in a list of workflow_components from qcsubmit.factories import OptimizationDatasetFactory if __name__ == "__main__": breakpoint() qcsds = OptimizationDatasetFactory() component = workflow_components.StandardConformerGenerator() component.max_conformers = 100 component.toolkit = "rdkit" qcsds.add_workflow_component(component) filter = workflow_components.RMSDCutoffConformerFilter() filter.rmsd_cutoff = 4.0 qcsds.add_workflow_component(filter) smi_file = sys.argv[1] mols = [Molecule.from_smiles(smi.split()[0], allow_undefined_stereochemistry=True) for smi in open(smi_file).readlines()] dataset = qcsds.create_dataset(dataset_name='my_dataset', molecules=mols, description="my test dataset.") dataset.export_dataset("dataset.json") # now lets save the workflow to json qcsds.export_settings('output.yaml')