Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type hinting and doc updating in tests #523

Closed
wants to merge 9 commits into from

Conversation

andrewtarzia
Copy link
Collaborator

@andrewtarzia andrewtarzia commented Feb 13, 2024

Requested Reviewers: @lukasturcani

This PR will be slow going, but I will occasionally do folders in tests and update their doc strings to Google and add TypeHints.

  • Once this process is done, we can remove the exclusion of tests from ruff and mypy

@andrewtarzia
Copy link
Collaborator Author

Testing functions for this PR:

def test1():
    initial = stk.BuildingBlock(
        smiles="C1=C(C(=C(C(=C1Br)Br)Br)Br)Br",
        functional_groups=(stk.BromoFactory(),),
    )
    fgs = list(initial.get_functional_groups())
    print(len(fgs))

    rng = np.random.default_rng(seed=1000)

    chosen_fgs = rng.choice(
        fgs,
        size=3,
        replace=False,
    )
    print(len(chosen_fgs))

    temp_molecule = initial.with_functional_groups(chosen_fgs)
    # The fg_repr was not updating prior to this!
    print(temp_molecule)
    print(temp_molecule.get_num_functional_groups())


def get_disconnected_components(molecule):
    # Produce a graph from the molecule that does not include edges
    # where the bonds to be optimized are.
    mol_graph = nx.Graph()
    for atom in molecule.get_atoms():
        mol_graph.add_node(atom.get_id())

    # Add edges.
    for bond in molecule.get_bonds():
        pair_ids = (bond.get_atom1().get_id(), bond.get_atom2().get_id())
        mol_graph.add_edge(*pair_ids)

    # Get atom ids in disconnected subgraphs.
    components = {}
    for c in nx.connected_components(mol_graph):
        c_ids = sorted(c)
        molecule.write("temp_mol.mol", atom_ids=c_ids)
        num_atoms = len(c_ids)
        newbb = stk.BuildingBlock.init_from_file("temp_mol.mol")
        os.system("rm temp_mol.mol")

        components[num_atoms] = newbb

    return components


def extract_host(molecule):
    components = get_disconnected_components(molecule)
    return components[max(components.keys())]


def extract_guest(molecule):
    components = get_disconnected_components(molecule)
    return components[min(components.keys())]


def test2():
    host = stk.ConstructedMolecule(
        topology_graph=stk.cage.FourPlusSix(
            building_blocks=(
                stk.BuildingBlock(
                    smiles="NC1CCCCC1N",
                    functional_groups=[
                        stk.PrimaryAminoFactory(),
                    ],
                ),
                stk.BuildingBlock(
                    smiles="O=Cc1cc(C=O)cc(C=O)c1",
                    functional_groups=[stk.AldehydeFactory()],
                ),
            ),
            optimizer=stk.MCHammer(),
        ),
    ).with_centroid((0, 0, 0))
    print("host:", host.get_centroid())
    guest_bb = stk.BuildingBlock("C1=C(C(=C(C(=C1Br)Br)Br)Br)Br")
    guest = stk.host_guest.Guest(
        building_block=guest_bb,
        start_vector=guest_bb.get_direction(),
        end_vector=host.get_plane_normal(),
        displacement=(0, 0, 0),
    )
    hgcomplex = stk.ConstructedMolecule(
        topology_graph=stk.host_guest.Complex(
            host=stk.BuildingBlock.init_from_molecule(host),
            guests=guest,
        ),
    )
    hgcomplex.write("hg1.mol")
    print("host 1", extract_host(hgcomplex).get_centroid())
    extract_host(hgcomplex).write("eh_1.mol")
    print("guest 1", extract_guest(hgcomplex).get_centroid())
    extract_guest(hgcomplex).write("eg_1.mol")
    print("----")
    host = stk.ConstructedMolecule(
        topology_graph=stk.cage.FourPlusSix(
            building_blocks=(
                stk.BuildingBlock(
                    smiles="NC1CCCCC1N",
                    functional_groups=[
                        stk.PrimaryAminoFactory(),
                    ],
                ),
                stk.BuildingBlock(
                    smiles="O=Cc1cc(C=O)cc(C=O)c1",
                    functional_groups=[stk.AldehydeFactory()],
                ),
            ),
            optimizer=stk.MCHammer(),
        ),
    ).with_centroid((2, 3, 5))
    print("host:", host.get_centroid())
    guest = stk.host_guest.Guest(
        building_block=guest_bb,
        start_vector=guest_bb.get_direction(),
        end_vector=host.get_plane_normal(),
        displacement=host.get_centroid(),
    )
    hgcomplex = stk.ConstructedMolecule(
        topology_graph=stk.host_guest.Complex(
            host=stk.BuildingBlock.init_from_molecule(host),
            guests=guest,
        ),
    )
    hgcomplex.write("hg2.mol")
    print("host 2", extract_host(hgcomplex).get_centroid())
    print("guest 2", extract_guest(hgcomplex).get_centroid())
    extract_host(hgcomplex).write("eh_2.mol")
    extract_guest(hgcomplex).write("eg_2.mol")

    guest = stk.host_guest.Guest(
        building_block=guest_bb,
        start_vector=guest_bb.get_direction(),
        end_vector=host.get_plane_normal(),
    )
    hgcomplex = stk.ConstructedMolecule(
        topology_graph=stk.host_guest.Complex(
            host=stk.BuildingBlock.init_from_molecule(host),
            guests=guest,
        ),
    )
    hgcomplex.write("hg3.mol")
    print("host 3", extract_host(hgcomplex).get_centroid())
    print("guest 3", extract_guest(hgcomplex).get_centroid())
    extract_host(hgcomplex).write("eh_3.mol")
    extract_guest(hgcomplex).write("eg_3.mol")
    raise SystemExit

andrewtarzia and others added 6 commits March 6, 2024 15:03
…cani#525)

Some position matrices changed because I updated to the new numpy RNG
instead of mixed old-numpy and `random.random` RNG in both packages.
Related Issues: lukasturcani#524

The existing implementation was overwriting the existing host
vertex if the guest was the same building block as the vertex.
Fix this by appending to the list of vertices that a given building
block is attached to.
@andrewtarzia andrewtarzia marked this pull request as ready for review March 6, 2024 14:04
@andrewtarzia
Copy link
Collaborator Author

Some issue with rebasing! Tried again

@andrewtarzia andrewtarzia deleted the update_docs branch March 7, 2024 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants