<jupyter_start><jupyter_text>Training performance<jupyter_code>with open('logp_IReLeaSE-REINFORCE_no_irl.json', 'r') as f:
    reinf_train_hist = json.load(f)
with open('logp_IReLeaSE-ppo_with_irl_no_attn.json', 'r') as f:
    ppo_train_hist = json.load(f)
rl_baseline_mean_vals = reinf_train_hist['logp_IReLeaSE-REINFORCE_no_irl'][0]['seed_1'][0]['baseline_mean_vals']
rl_biased_mean_vals = reinf_train_hist['logp_IReLeaSE-REINFORCE_no_irl'][0]['seed_1'][1]['biased_mean_vals']
rl_gen_mean_vals = reinf_train_hist['logp_IReLeaSE-REINFORCE_no_irl'][0]['seed_1'][2]['gen_mean_vals']
len(rl_baseline_mean_vals), len(rl_biased_mean_vals), len(rl_gen_mean_vals)
ppo_baseline_mean_vals = ppo_train_hist['logp_IReLeaSE-ppo_with_irl_no_attn'][0]['seed_1'][0]['baseline_mean_vals']
ppo_biased_mean_vals = ppo_train_hist['logp_IReLeaSE-ppo_with_irl_no_attn'][0]['seed_1'][1]['biased_mean_vals']
ppo_gen_mean_vals = ppo_train_hist['logp_IReLeaSE-ppo_with_irl_no_attn'][0]['seed_1'][2]['gen_mean_vals']
len(ppo_baseline_mean_vals), len(ppo_biased_mean_vals), len(ppo_gen_mean_vals)
plt.plot(rl_baseline_mean_vals)
plt.plot(rl_biased_mean_vals)
plt.plot(nbu.smoothing_values(ppo_gen_mean_vals[:200])[20:])
plt.plot(nbu.smoothing_values(rl_gen_mean_vals)[20:])
plt.legend(['baseline','demonstration','irelease','reinforce'])
plt.savefig('logp_irl_vs_rl_convergence.png')<jupyter_output><empty_output><jupyter_text>Model analysis<jupyter_code>generators = nbu.data_provider('../../data/logp_smiles_biased.smi', '../../data/unbiased_smiles.smi')
init_dict = nbu.initialize(nbu.logp_ppo_hparams(), generators['demo_data'], generators['unbiased_data'], True)
demo_data_gen = init_dict['demo_data_gen']
unbiased_data_gen = init_dict['unbiased_data_gen']
expert_model = init_dict['expert_model']
def sample_smiles(generator, num_samples):
    with torch.set_grad_enabled(False):
        gen_samples = []
        gen_logits = []
        hidden_neurons = []
        per_round = 200
        rounds = ceil(num_samples/per_round)
        if num_samples < 200:
            per_round = num_samples
        for i in range(rounds):
            smiles, logits, hidden = generate_smiles(generator, demo_data_gen, init_dict['gen_args'], 
                                                             num_samples=per_round, is_train=False, verbose=True, 
                                                             return_logits=True)
            gen_samples.extend(smiles)
            gen_logits.extend(logits)
            hidden_neurons.append(hidden)
    return gen_samples, np.array(gen_logits), np.concatenate(hidden_neurons, axis=1)
def plot_hist(prediction, num_generated):
    if isinstance(prediction, (list, tuple, set)):
        prediction = np.array(prediction)
    print("Mean value of predictions:", prediction.mean())
    print("Proportion of valid SMILES:", len(prediction)/num_generated)
    ax = sns.kdeplot(prediction.ravel(), shade=True)
    ax.set(xlabel='Predicted pIC50', 
           title='Distribution of predicted pIC50 for generated molecules')
    plt.show()
n_to_generate = 10
unbiased_smiles = unbiased_data_gen.random_training_set_smiles(n_to_generate)
unbiased_smiles_predictions = expert_model(unbiased_smiles)[1]
len(unbiased_smiles), unbiased_smiles[0]
biased_smiles = demo_data_gen.random_training_set_smiles(n_to_generate)
biased_smiles_predictions = expert_model(biased_smiles)[1]
len(biased_smiles), biased_smiles[0]<jupyter_output><empty_output><jupyter_text>PPO models<jupyter_code>ppo_agent_net = init_dict['agent_net']
ppo_agent_net.load_state_dict(nbu.load_model_weights('../model_dir/logp_irelease_stack-rnn_gru_ppo_agent_2020_06_25__01_31_53_1.978_500.mod'))
ppo_critic_net = init_dict['critic_net']
ppo_critic_net.load_state_dict(nbu.load_model_weights('../model_dir/logp_irelease_stack-rnn_gru_ppo_critic_2020_06_25__01_31_53_1.978_500.mod'))
ppo_reward_net = init_dict['reward_net']
ppo_reward_net.load_state_dict(nbu.load_model_weights('../model_dir/logp_irelease_stack-rnn_gru_ppo_reward_net_2020_06_25__01_31_53_1.978_500.mod'))
ppo_samples, ppo_samples_logits, ppo_samples_neurons = sample_smiles(ppo_agent_net, n_to_generate)
print(len(ppo_samples))
ppo_samples_neurons.shape
ppo_mols, ppo_samples_predictions, ppo_invalid = expert_model(ppo_samples)
len(ppo_samples_predictions), len(ppo_invalid)
plot_hist(ppo_samples_predictions, len(ppo_samples))<jupyter_output>Mean value of predictions: 0.7369131
Proportion of valid SMILES: 0.9<jupyter_text>REINFORCE models<jupyter_code>init_dict = nbu.initialize(nbu.logp_reinforce_hparams(), generators['demo_data'], generators['unbiased_data'], False)
rl_agent_net = init_dict['agent_net']
rl_agent_net.load_state_dict(nbu.load_model_weights('../model_dir/logp_irelease_stack-rnn_gru_reinforce_agent_2020_06_25__01_56_50_1.873_600.mod'))
rl_reward_net = init_dict['reward_net']
rl_reward_net.load_state_dict(nbu.load_model_weights('../model_dir/logp_irelease_stack-rnn_gru_reinforce_reward_net_2020_06_25__01_56_50_1.873_600.mod'))
rl_samples, rl_samples_logits, rl_samples_neurons = sample_smiles(rl_agent_net, n_to_generate)
print(len(rl_samples))
rl_mols, rl_samples_predictions, rl_invalid = expert_model(rl_samples)
len(rl_samples_predictions), len(rl_invalid)
plot_hist(rl_samples_predictions, len(rl_samples))<jupyter_output>Mean value of predictions: 0.49940777
Proportion of valid SMILES: 0.937<jupyter_text>Comparisons  KDE plots<jupyter_code>sns.kdeplot(ppo_samples_predictions.ravel(),label='Optimized_irl', shade=True, color='red')
sns.kdeplot(rl_samples_predictions.ravel(),label='Optimized_rl', shade=True, color='blue')
sns.kdeplot(unbiased_smiles_predictions.ravel(), label='Unbiased', shade=True, color='grey')
ax = sns.kdeplot(biased_smiles_predictions.ravel(), label='Demo', shade=True, color='orange')
plt.xlabel('pIC50 values')
# ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# ax.set_xticks([])
sns.despine(offset=5, left=True, bottom=False)
plt.savefig('logp_kde_plots.png')
plt.show()<jupyter_output><empty_output><jupyter_text>RNN hidden neurons analysis<jupyter_code>def plot_heatmap(smiles_list, logits, neuron=None, save=False):
    if neuron is None:
        neuron = np.random.randint(logits.shape[-1])
    print(f'Plotting for neuron {neuron}')
    for i, smiles in enumerate(smiles_list):
#         print(smiles)
        arr = logits[:len(smiles), i, neuron].reshape(1, len(smiles))
        chars = np.array([c for c in smiles]).reshape(1,-1)
        fig = plt.figure(figsize=(200,4))
        sns.heatmap(arr, annot=chars, fmt='', cbar=False, 
                    cmap=sns.color_palette("bwr", 10), annot_kws={'size':100, 'fontweight':'normal'},
                   xticklabels=False, yticklabels=False, square=True)
        if save:
            os.makedirs(f'neuron_{neuron}', exist_ok=True)
            plt.savefig(f'neuron_{neuron}/{i}.png')
        plt.show()
ppo_samples_neurons.shape
plot_heatmap(ppo_samples[:10], ppo_samples_neurons[:,:10,:], neuron=1005, save=True)
ppo_eval_rpt = nbu.evaluate(ppo_samples, biased_smiles)
ppo_eval_rpt
rl_eval_rpt = nbu.evaluate(rl_samples, biased_smiles)
rl_eval_rpt<jupyter_output><empty_output><jupyter_text>Draw random compounds<jupyter_code>import os
import rdkit.Chem as Chem
from rdkit.Chem.Draw import DrawingOptions
from rdkit.Chem import Draw
DrawingOptions.atomLabelFontSize = 50
DrawingOptions.dotsPerAngstrom = 100
DrawingOptions.bondLineWidth = 3
generated_mols = [Chem.MolFromSmiles(sm, sanitize=True) for sm in set(ppo_samples)]
sanitized_gen_mols = [generated_mols[i] for i in np.where(np.array(generated_mols) != None)[0]]
n_to_draw = 20
ind = np.random.randint(0, len(sanitized_gen_mols), n_to_draw)
mols_to_draw = [sanitized_gen_mols[i] for i in ind]
legends = ['pIC50 = ' + str(round(float(ppo_samples_predictions[i]), 3)) for i in ind]
Draw.MolsToGridImage(mols_to_draw, molsPerRow=5, subImgSize=(300,300), legends=legends)
# Save selected compounds to file
os.makedirs('./logp_samples', exist_ok=True)
for i, mol in enumerate(mols_to_draw):
    Draw.MolToImageFile(mol, f'./logp_samples/pic50_{round(float(ppo_samples_predictions[i]), 3)}.png')<jupyter_output>Warning: unable to load font metrics from dir C:\Users\bbrighttaer\Anaconda3\envs\denovo\lib\site-packages\rdkit\sping\PIL\pilfonts<jupyter_text>Reward net analysis<jupyter_code>ppo_samples = [s.strip() for s in ppo_samples]
ppo_invalid_smiles = [s.strip() for s in ppo_invalid]
len(ppo_samples)
encoder = init_dict['encoder']
ppo_reward_net_rnn = init_dict['reward_net_rnn']
with torch.set_grad_enabled(False):
    inp, valid_vec = nbu.smiles_to_tensor(ppo_samples)
    enc_out = encoder([inp, valid_vec])
    reward, logits = ppo_reward_net_rnn(enc_out, return_logits=True)
logits.shape
from sklearn.manifold import TSNE
import pandas as pd 
tsne_rep = TSNE(n_components=2).fit_transform(logits.detach().cpu().numpy())
data = pd.DataFrame({'dim_1':tsne_rep[:,0], 'dim_2':tsne_rep[:,1]})
cm = plt.cm.get_cmap('RdYlBu')
sc = plt.scatter(tsne_rep[:, 0], tsne_rep[:, 1], c=tsne_rep[:, 1], vmin=0, vmax=20, s=35, cmap=cm)
plt.colorbar(sc)
plt.axis('off')
plt.savefig('logp_rewardnet_tsne.png')
plt.show()
sns.set_style("white")
plt.rcParams['lines.markeredgewidth'] = 1
points = plt.scatter(data['dim_1'], data['dim_2'], c=data['dim_2'], s=20, cmap="Spectral")
plt.colorbar(points)
sns.regplot("dim_1", "dim_2", data=data, scatter=False, color=".1", fit_reg=False)
plt.axis('off')
plt.show()
cm = plt.cm.get_cmap('RdYlBu')
sc = plt.scatter(tsne_rep[:, 0], tsne_rep[:, 1], c=tsne_rep[:, 1], vmin=0, vmax=20, s=35, cmap=cm)
plt.colorbar(sc)
plt.axis('off')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1327176507211788, 'fraction_numerical': 0.0257341496316281, 'mean_word_length': 6.8677551020408165, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '648215', 'license': 'MIT', 'n_tokens_mistral': 3929, 'n_tokens_neox': 3689, 'n_words': 475, 'path': 'proj/notebooks/old/logp.ipynb', 'repo_name': 'bbrighttaer/irelease', 'size': '9637'}