<jupyter_start><jupyter_text>Polymer Machine Learning via Polymer DatabaseDatasource: http://polymerdatabase.com/polymer%20physics/Polymer%20Tg.htmlPolymer names were cleaned to have better conformance with `pubchempy` API.  Setup  Installation<jupyter_code>!pip install pubchempy
!pip install deepchem
!pip install selfies<jupyter_output>Requirement already satisfied: pubchempy in /usr/local/lib/python3.7/dist-packages (1.0.4)
Requirement already satisfied: deepchem in /usr/local/lib/python3.7/dist-packages (2.6.1)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from deepchem) (1.0.2)
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from deepchem) (1.4.1)
Requirement already satisfied: joblib in /usr/local/lib/python3.7/dist-packages (from deepchem) (1.1.0)
Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from deepchem) (1.3.5)
Requirement already satisfied: rdkit-pypi in /usr/local/lib/python3.7/dist-packages (from deepchem) (2021.9.4)
Requirement already satisfied: numpy>=1.21 in /usr/local/lib/python3.7/dist-packages (from deepchem) (1.21.5)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas->deepchem) (2.8.2)
Requirement already satisfied: pytz>=2017.3 in /u[...]<jupyter_text>Imports<jupyter_code>from tqdm import tqdm
from time import sleep
import numpy as np
import pandas as pd
import pubchempy as pcp
import deepchem as dc
import selfies as sf<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>data = pd.read_csv("/content/drive/MyDrive/School/2021-2022 School/mat-informatics/hw2/polymer-database-glass-transition.csv")
df = data.rename(columns = {"pretty_polymer": "name", "Tg (deg C)": "Tg"})
df
df.describe()<jupyter_output><empty_output><jupyter_text>PubChemPy  Basic UsageSee https://pubchempy.readthedocs.io/en/latest/guide/gettingstarted.htmlgetting-started<jupyter_code>pcp.get_properties('IsomericSMILES', 'CC', 'smiles')
pcp.get_properties('IsomericSMILES', "Nylon-12", "name")<jupyter_output><empty_output><jupyter_text>Retrieve SMILES StringsNote that the `sleep` command helps prevent "Server Busy" errors. Consider increasing this value if the error appears again.<jupyter_code>ps = []
for name in tqdm(df.name):
  # enter your code to retrieve the SMILES strings
  # consider how you want to deal with cases of multiple or no entries
  sleep(0.1) # to prevent "Server Busy" error<jupyter_output>100%|██████████| 222/222 [00:55<00:00,  3.98it/s]<jupyter_text>DeepChem SMILES Featurization<jupyter_code># https://deepchem.readthedocs.io/en/latest/api_reference/featurizers.html#onehotfeaturizer
featurizer = dc.feat.OneHotFeaturizer()
smiles = df2["smiles"]
encodings = featurizer.featurize(smiles)

print("type: ", type(encodings[0]))
print("shape: ", encodings[0].shape)
print("untransformed: ", featurizer.untransform(encodings[0]))
print(encodings[0])
print(encodings[0].sum())<jupyter_output>[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 1. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]]
100.0<jupyter_text>SELFIES Featurization  Basic Usage<jupyter_code># https://github.com/aspuru-guzik-group/selfies#translation-between-selfies-and-smiles-representations
# SMILES -> SELFIES -> SMILES translation
benzene = "c1ccccc1"
try:
    benzene_sf = sf.encoder(benzene)  # [C][=C][C][=C][C][=C][Ring1][=Branch1]
    benzene_smi = sf.decoder(benzene_sf)  # C1=CC=CC=C1
except sf.EncoderError:
    pass  # sf.encoder error!
except sf.DecoderError:
    pass  # sf.decoder error!

len_benzene = sf.len_selfies(benzene_sf)  # 8<jupyter_output><empty_output><jupyter_text>One-hot Encoding Example<jupyter_code># https://github.com/aspuru-guzik-group/selfies#integer-and-one-hot-encoding-selfies
dataset = ["[C][O][C]", "[F][C][F]", "[O][=O]", "[C][C][O][C][C]"]
alphabet = sf.get_alphabet_from_selfies(dataset)
alphabet.add("[nop]")  # [nop] is a special padding symbol
alphabet = list(sorted(alphabet))  # ['[=O]', '[C]', '[F]', '[O]', '[nop]']

pad_to_len = max(sf.len_selfies(s) for s in dataset)  # 5
symbol_to_idx = {s: i for i, s in enumerate(alphabet)}

dimethyl_ether = dataset[0]  # [C][O][C]

label, one_hot = sf.selfies_to_encoding(
   selfies=dimethyl_ether,
   vocab_stoi=symbol_to_idx,
   pad_to_len=pad_to_len,
   enc_type="both"
)
print(label, one_hot)<jupyter_output>[1, 3, 1, 4, 4] [[0, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]]
{'fraction_non_alphanumeric': 0.16182937554969218, 'fraction_numerical': 0.04001759014951627, 'mean_word_length': 5.27448275862069, 'pattern_counts': {'":': 2, '<': 30, '<?xml version=': 0, '>': 35, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '522623', 'license': 'MIT', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1752, 'n_words': 373, 'path': 'worked_examples/smiles_selfies/polymer_ml_tutorial.ipynb', 'repo_name': 'sp8rks/MaterialsInformatics', 'size': '4548'}