<jupyter_start><jupyter_text>Prototype script for parallelizing making graphs and doing deep learning on them<jupyter_code>import pandas as pd

protease_data = pd.read_csv('../data/hiv_data/hiv-protease-data-expanded.csv', index_col=0)
protease_data.describe()<jupyter_output><empty_output><jupyter_text>Based on the number of samples available for each drug, let us start with NFV, which has the most number of values available.<jupyter_code>protease_data['NFV'].describe()<jupyter_output><empty_output><jupyter_text>I will log-transform these values, and then Z-standardize them.<jupyter_code>import sklearn.preprocessing as pp
# from sklearn.preprocessing import scale
import numpy as np

# Drop NaN values on only the NFV column
fpv_data = protease_data.dropna(subset=['FPV'])[['FPV', 'seqid']]
fpv_data['FPV'] = fpv_data['FPV'].apply(np.log10)
# fpv_data['FPV_scaled'] = pp.scale(fpv_data['FPV'])
fpv_data.hist()
fpv_data.head()
# Grab out a sample of projects, based on the seqid.
import random as rnd
# seqids_interest = rnd.sample(list(fpv_data['seqid']), 25)

proj_titles = {c['title']:c['code'] for c in model_data['projects']}


projs_interest = dict()

n_graphs = 4

while len(projs_interest) < n_graphs:
    seqid = rnd.choice(list(fpv_data['seqid']))
    if seqid in proj_titles.keys():
        projs_interest[seqid] = proj_titles[seqid]
        
        
        
# for seqid in :
#     if seqid not in proj_titles:
#         print(seqid)
# projects_interest = {c['title']:c['code'] for c in model_data['projects'] if c['title'] in seqids_interest}
projs_interest
fpv_data[fpv_data['seqid'].isin(projs_interest.keys())]
projects = [i for i in projs_interest.values()]

p = pin.ProteinInteractionNetwork('../data/batch_models/{0}/model_01.pdb'.format(projects[0]))
p.graph['project'] = project
p.graph['input_shape'] = p.nodes(data=True)[0][1]['features'].shape
p.graph['seqid'] = seqid


# Takes about 2 hours for the task to finish over 3401 graphs. This is it being done on a single core.
from tqdm import tqdm
from joblib import Parallel, delayed
graphs = []

def make_protein_graphs(project, seqid):
    """
    Custom function for this notebook to parallelize the making of protein graphs over individual cores.
    """
    from pin import pin
    p = pin.ProteinInteractionNetwork('../data/batch_models/{0}/model_01.pdb'.format(project))
    p.graph['project'] = project
    p.graph['input_shape'] = p.nodes(data=True)[0][1]['features'].shape
    p.graph['seqid'] = seqid
    return p

graphs = Parallel(n_jobs=-1)(delayed(make_protein_graphs)(proj, seqid) for seqid, proj in projs_interest.items())
len(graphs)
# Check a sample of the graphs to make sure that the input shapes are correct.
graphs[0].graph['input_shape']
from graphfp.layers import FingerprintLayer, GraphInputLayer, LinearRegressionLayer, GraphConvLayer,\
    FullyConnectedLayer, DropoutLayer
from graphfp.utils import initialize_network, batch_sample
graphs[0].nodes(data=True)[0][1]['features'].shape
def train_loss(wb_vect, unflattener, batch=True, batch_size=2, debug=False):
    """
    Training loss is MSE.

    We pass in a flattened parameter vector and its unflattener.
    """
    wb_struct = unflattener(wb_vect)

    if batch:
        batch_size = batch_size
    else:
        batch_size = len(graphs)

    samp_graphs, samp_inputs = batch_sample(graphs, input_shape, batch_size)

    preds = predict(wb_struct, samp_inputs, samp_graphs)
    graph_ids = [g.graph['seqid'] for g in samp_graphs]
    # graph_scores = fpv_data[fpv_data['seqid'].isin(graph_ids)]['FPV'].values.reshape(preds.shape)
    graph_scores = fpv_data.set_index('seqid').ix[graph_ids]['FPV'].values.reshape(preds.shape)
    assert preds.shape == graph_scores.shape
    
    if debug:
        print(graph_ids)
        print('Predictions:')
        print(preds)
        print('Mean: {0}'.format(np.mean(preds)))
        print('')
        print('Actual')
        print(graph_scores)
        print('Mean: {0}'.format(np.mean(graph_scores)))
        print('')
        print('Difference')
        print(preds - graph_scores)
        print('Mean Squared Error: {0}'.format(np.mean(np.power(preds - graph_scores, 2))))
        print('')
    # print(preds.shape, graph_scores.shape)
    # assert preds.shape[1] == graph_scores.shape[1]
    # print(preds - graph_scores)
    mse = np.mean(np.power(preds - graph_scores, 2))
    return mse
# fpv_data[fpv_data['seqid'].isin(['2996-1', '2996-0'])]
graph_ids = ['4387-1', '2996-1', '2996-0', '4482-1']
fpv_data.set_index('seqid').ix[graph_ids]['FPV'].values.reshape((4,1))
def predict(wb_struct, inputs, graphs):
    """
    Makes predictions by running the forward pass over all of the layers.

    Parameters:
    ===========
    - wb_struct: a dictionary of weights and biases stored for each layer.
    - inputs: the input data matrix. should be one row per graph.
    - graphs: a list of all graphs.
    """
    curr_inputs = inputs

    for i, layer in enumerate(layers):
        # print(type(wb_struct))
        wb = wb_struct['layer{0}_{1}'.format(i, layer)]
        curr_inputs = layer.forward_pass(wb, curr_inputs, graphs)
    return curr_inputs
def callback(wb, i):
    """
    Any function you want to run at each iteration of the optimization.
    """
    # from time import time
    from graphfp.flatten import flatten

    # new_time = time()
    wb_vect, wb_unflattener = flatten(wb)
    print('Iteration: {0}'.format(i))
    # print('Computing gradient w.r.t. weights...')

    print('Training Loss: ')
    
    tl = train_loss(wb_vect, wb_unflattener, batch=False)
    print(tl)
    print('')

    training_losses.append(tl)
from autograd import grad
grad_tl = grad(train_loss)
len(graphs[0])
import networkx as nx
from collections import Counter
Counter([len(graphs[0].neighbors(n)) for n in graphs[0].nodes()])
from graphfp.optimizers import sgd, adam

input_shape = graphs[0].graph['input_shape']
layers = [# GraphConvLayer((input_shape[1], input_shape[1] * 2)),
          # GraphConvLayer((input_shape[1], input_shape[1])),
          # GraphConvLayer((input_shape[1], input_shape[1])),
          GraphConvLayer((input_shape[1], input_shape[1])),
          FingerprintLayer(input_shape[1]),
          # FullyConnectedLayer((input_shape[1], input_shape[1])),
          # DropoutLayer(p=0),
          # FullyConnectedLayer((input_shape[1], input_shape[1])),
          # DropoutLayer(p=0.3),
          # FullyConnectedLayer((input_shape[1], input_shape[1])),
          LinearRegressionLayer((input_shape[1], 1)),
]

wb_all = initialize_network(input_shape, graphs, layers)

training_losses = list()
from graphfp.flatten import flatten
wb_vect, wb_unflattener = flatten(wb_all)
train_loss(wb_vect, wb_unflattener, debug=True)
wb_vect, wb_unflattener = adam(grad_tl, wb_all, callback=clbk, num_iters=1000)
wb_all = wb_unflattener(wb_vect)
plt.plot(training_losses)
plt.yscale('log')
from graphfp.flatten import flatten
wb_vect, wb_unflattener = flatten(wb_all)
train_loss(wb_vect, wb_unflattener, debug=True)
wb_all['layer0_GraphConvLayer']['weights'].max()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13482356249121327, 'fraction_numerical': 0.014058765640376775, 'mean_word_length': 4.352896914973664, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '138770', 'license': 'MIT', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2431, 'n_words': 538, 'path': 'notebooks/script_prototypes.ipynb', 'repo_name': 'ericmjl/protein-convolutional-nets', 'size': '7113'}