Skip to content

Incorrect Recovery of Groups in Molecule Decoding/Encoding Process #7

Description

@blaschma

Hi,
I was playing with the nice tutorial provided in the repository. I have adapted the last cell of the notebook:

import numpy as np
import random
random.seed(42)

# to generate new molecules by combining random groups
# start with a grammar of groups, in this case `grammar_fragment` is a grammar of groups

# assign random overload_idxs
for group_name in grammar_fragment.vocab:
    group = grammar_fragment.vocab[group_name]
    group.overload_idx = random.randint(0, 100)
    pass

group_names = list(grammar_fragment.vocab.keys())

# pick n random groups
n = 2

n_pop = 0 # add pop tokens for extra branching
random_groups = [random.choice(group_names) for _ in range(n)] + ['[pop]' for _ in range(n_pop)]
random.shuffle(random_groups)


used_groups = []
# combine them into a new group selfies string
new_gselfies = ''
for i, g in enumerate(random_groups):
    if g == '[pop]':
        new_gselfies += '[pop]'
        continue
    #set high prio
    grammar_fragment.vocab[g].priority=50000
    used_groups.append(grammar_fragment.vocab[g])

    n_attachment_points = len(grammar_fragment.vocab[g].attachment_points)-1
    start = random.randint(0, n_attachment_points)
    
    random_range = np.arange(0, n_attachment_points)    
    random_range = [e for e in random_range if e!=start]
    if not random_range or i == len(random_groups)-1:
        new_block = f"[:{start}{g}]"
    else:        
        end = random.choice(random_range)    
        new_block = f"[:{start}{g}]{INDEX_ALPHABET[end]}"

    mol = grammar_fragment.vocab[g].mol
    for atom in mol.GetAtoms():
        atom.SetProp('atomLabel', str(atom.GetIdx() + 1))
    img = Draw.MolToImage(mol)
    print(g)
    display(img)
    new_gselfies += new_block

print(f"Generated group selfies {new_gselfies}")

#Create new GroupGrammer with used groups
grammar = GroupGrammar(used_groups)   
print(f"grammar vocab {grammar.vocab}")
out = grammar.decoder(new_gselfies)
display(out)

ex = grammar.extract_groups(out)
print(f"Extracted groups", ex)
valid = grammar.full_encoder(out, join=True)
print(f"{valid=}")
out = grammar.decoder(valid)
display(out)

This gives:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions