Commit 372c5d01 authored by leswing's avatar leswing
Browse files

Mark Tests

parent 287ada79
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -5,13 +5,14 @@ export python_version=2.7
bash scripts/install_deepchem_conda.sh $envname
source activate $envname
python setup.py install
conda install jupyter
conda install nbconvert
conda install jupyter_client
conda install ipykernel
conda install matplotlib
pip install nglview
conda install ipywidgets
conda install -y jupyter
conda install -y nbconvert
conda install -y jupyter_client
conda install -y ipykernel
conda install -y matplotlib
yes | pip install nglview
conda install -y ipywidgets
yes | pip uninstall tensorflow

cd examples/notebooks
nosetests --with-timer tests.py --with-xunit --xunit-file=notebook_tests.xml|| true
+253 −273

File changed.

Preview size limit exceeded, changes collapsed.

+208 −29
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

This notebook walks through the creation of multitask models on MUV. The goal is to demonstrate that multitask methods outperform singletask methods on MUV.

%% Cell type:code id: tags:

``` python
%reload_ext autoreload
%autoreload 2
%pdb off
reload = True
```

%% Output

    Automatic pdb calling has been turned OFF

%% Cell type:code id: tags:

``` python
import deepchem as dc

dataset_file= "../../datasets/muv.csv.gz"
dataset = dc.utils.save.load_from_disk(dataset_file)
print("Columns of dataset: %s" % str(dataset.columns.values))
print("Number of examples in dataset: %s" % str(dataset.shape[0]))
```

%% Output

    /home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
      "This module will be removed in 0.20.", DeprecationWarning)

    Columns of dataset: ['MUV-466' 'MUV-548' 'MUV-600' 'MUV-644' 'MUV-652' 'MUV-689' 'MUV-692'
     'MUV-712' 'MUV-713' 'MUV-733' 'MUV-737' 'MUV-810' 'MUV-832' 'MUV-846'
     'MUV-852' 'MUV-858' 'MUV-859' 'mol_id' 'smiles']
    Number of examples in dataset: 93127

%% Cell type:markdown id: tags:

Now, let's visualize some compounds from our dataset

%% Cell type:code id: tags:

``` python
from rdkit import Chem
from rdkit.Chem import Draw
from itertools import islice
from IPython.display import Image, display, HTML

def display_images(filenames):
    """Helper to pretty-print images."""
    imagesList=''.join(
        ["<img style='width: 140px; margin: 0px; float: left; border: 1px solid black;' src='%s' />"
         % str(s) for s in sorted(filenames)])
    display(HTML(imagesList))

def mols_to_pngs(mols, basename="test"):
    """Helper to write RDKit mols to png files."""
    filenames = []
    for i, mol in enumerate(mols):
        filename = "%s%d.png" % (basename, i)
        Draw.MolToFile(mol, filename)
        filenames.append(filename)
    return filenames

num_to_display = 12
molecules = []
for _, data in islice(dataset.iterrows(), num_to_display):
    molecules.append(Chem.MolFromSmiles(data["smiles"]))
display_images(mols_to_pngs(molecules))
```

%% Output


%% Cell type:code id: tags:

``` python
MUV_tasks = ['MUV-692', 'MUV-689', 'MUV-846', 'MUV-859', 'MUV-644',
             'MUV-548', 'MUV-852', 'MUV-600', 'MUV-810', 'MUV-712',
             'MUV-737', 'MUV-858', 'MUV-713', 'MUV-733', 'MUV-652',
             'MUV-466', 'MUV-832']

featurizer = dc.feat.CircularFingerprint(size=1024)
loader = dc.data.CSVLoader(
      tasks=MUV_tasks, smiles_field="smiles",
      featurizer=featurizer)
dataset = loader.featurize(dataset_file)
```

%% Output

    Loading raw samples now.
    shard_size: 8192
    About to start loading CSV from ../../datasets/muv.csv.gz
    Loading shard 1 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 0 took 15.158 s
    Loading shard 2 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 1 took 15.454 s
    Loading shard 3 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 2 took 15.119 s
    Loading shard 4 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 3 took 15.337 s
    Loading shard 5 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 4 took 15.104 s
    Loading shard 6 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 5 took 15.349 s
    Loading shard 7 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 6 took 15.181 s
    Loading shard 8 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 7 took 15.062 s
    Loading shard 9 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 8 took 14.960 s
    Loading shard 10 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 9 took 15.355 s
    Loading shard 11 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    Featurizing sample 4000
    Featurizing sample 5000
    Featurizing sample 6000
    Featurizing sample 7000
    Featurizing sample 8000
    TIMING: featurizing shard 10 took 15.209 s
    Loading shard 12 of size 8192.
    Featurizing sample 0
    Featurizing sample 1000
    Featurizing sample 2000
    Featurizing sample 3000
    TIMING: featurizing shard 11 took 5.511 s
    TIMING: dataset construction took 176.355 s
    Loading dataset from disk.

%% Cell type:code id: tags:

``` python
splitter = dc.splits.RandomSplitter(dataset_file)
train_dataset, valid_dataset, test_dataset = splitter.train_valid_test_split(
    dataset)
#NOTE THE RENAMING:
valid_dataset, test_dataset = test_dataset, valid_dataset
```

%% Output

    Computing train/valid/test indices
    TIMING: dataset construction took 3.757 s
    Loading dataset from disk.
    TIMING: dataset construction took 1.717 s
    Loading dataset from disk.
    TIMING: dataset construction took 1.707 s
    Loading dataset from disk.

%% Cell type:code id: tags:

``` python
import numpy as np
import numpy.random

params_dict = {"activation": ["relu"],
               "momentum": [.9],
               "batch_size": [50],
               "init": ["glorot_uniform"],
               "data_shape": [train_dataset.get_data_shape()],
               "learning_rate": [1e-3],
               "decay": [1e-6],
               "nb_epoch": [1],
               "nesterov": [False],
               "dropouts": [(.5,)],
               "nb_layers": [1],
               "batchnorm": [False],
               "layer_sizes": [(1000,)],
               "weight_init_stddevs": [(.1,)],
               "bias_init_consts": [(1.,)],
               "penalty": [0.],
              }


n_features = train_dataset.get_data_shape()[0]
def model_builder(model_params, model_dir):
  model = dc.models.TensorflowMultiTaskClassifier(
    len(MUV_tasks), n_features, **model_params)
  return model

metric = dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean)
optimizer = dc.hyper.HyperparamOpt(model_builder)
best_dnn, best_hyperparams, all_results = optimizer.hyperparam_search(
    params_dict, train_dataset, valid_dataset, [], metric)
```

%% Output

    Fitting model 1/1
    hyperparameters: {'learning_rate': 0.001, 'layer_sizes': (1000,), 'data_shape': (1024,), 'dropouts': (0.5,), 'activation': 'relu', 'decay': 1e-06, 'batch_size': 50, 'penalty': 0.0, 'nesterov': False, 'init': 'glorot_uniform', 'bias_init_consts': (1.0,), 'weight_init_stddevs': (0.1,), 'batchnorm': False, 'nb_layers': 1, 'nb_epoch': 1, 'momentum': 0.9}
    Training for 1 epochs
    On batch 0
    On batch 50
    On batch 100
    On batch 150
    On batch 200
    On batch 250
    On batch 300
    On batch 350
    On batch 400
    On batch 450
    On batch 500
    On batch 550
    On batch 600
    On batch 650
    On batch 700
    On batch 750
    On batch 800
    On batch 850
    On batch 900
    On batch 950
    On batch 1000
    On batch 1050
    On batch 1100
    On batch 1150
    On batch 1200
    On batch 1250
    On batch 1300
    On batch 1350
    On batch 1400
    On batch 1450
    Ending epoch 0: Average loss 0.0512837
    On batch 0
    TIMING: model fitting took 21.154 s True
    computed_metrics: [0.82459677419354838, 0.57987551867219911, 0.87625923438549358, 0.40115904700579524, 0.85100593667546176, 0.84176720285969608, 0.81982616866337787, 0.92716408268733852, 0.90555555555555556, 0.93358568479008941, 0.84312370421561855, 0.5993421052631579, 0.40930420711974114, 0.53205995041694831, 0.99836708033964727, 0.6931528662420382, 0.89704918032786884]
    Model 1/1, Metric mean-roc_auc_score, Validation set 0: 0.760776
    	best_validation_score so far: 0.760776
    computed_metrics: [0.88035724476402444, 0.92499356167911406, 0.98634926369263543, 0.86291516863550766, 0.95042120575284494, 0.97632672092234651, 0.9834740792000799, 0.91723021876461464, 0.9288788241326269, 0.9427229610525234, 0.9281144552776599, 0.94111351509505148, 0.90709998845843109, 0.93500087572450941, 0.95015000422547113, 0.92490461101880572, 0.97988502677604972]
    Best hyperparameters: (1e-06, (1024,), 1, 'relu', (1000,), 50, 0.0, False, 'glorot_uniform', (1.0,), (0.1,), 1, False, (0.5,), 0.001, 0.9)
    train_score: 0.936467
    validation_score: 0.760776
+312 −0

File added.

Preview size limit exceeded, changes collapsed.

+16 −37
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

Setting up imports

%% Cell type:code id: tags:

``` python
%load_ext autoreload
%autoreload 2
%pdb off
"""
Not Currently Working
"""
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

__author__ = "Joseph Gomes"
__copyright__ = "Copyright 2016, Stanford University"
__license__ = "LGPL"

import os
import unittest
import tempfile
import shutil

import numpy as np
import numpy.random

from deepchem import metrics
from deepchem.datasets import Dataset
from deepchem.data.datasets import Dataset
from deepchem.featurizers.featurize import DataFeaturizer
from deepchem.featurizers.featurize import FeaturizedSamples
from deepchem.hyperparameters import HyperparamOpt
from deepchem.metrics import Metric
from deepchem.models import Model
from deepchem.models.sklearn_models import SklearnModel
from deepchem.transformers import NormalizationTransformer
from deepchem.utils.evaluate import Evaluator
from sklearn.ensemble import RandomForestRegressor
from sklearn.kernel_ridge import KernelRidge
```

%% Output

    The autoreload extension is already loaded. To reload it, use:
      %reload_ext autoreload
    Automatic pdb calling has been turned OFF

    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    <ipython-input-2-a6ac7c9c202f> in <module>()
         20 from deepchem import metrics
         21 from deepchem.data.datasets import Dataset
    ---> 22 from deepchem.featurizers.featurize import DataFeaturizer
         23 from deepchem.featurizers.featurize import FeaturizedSamples
         24 from deepchem.hyperparameters import HyperparamOpt
    ImportError: No module named featurizers.featurize

%% Cell type:markdown id: tags:

Creating temporary directories

%% Cell type:code id: tags:

``` python
feature_dir = tempfile.mkdtemp()
samples_dir = tempfile.mkdtemp()
train_dir = tempfile.mkdtemp()
valid_dir = tempfile.mkdtemp()
test_dir = tempfile.mkdtemp()
model_dir = tempfile.mkdtemp()
```

%% Cell type:markdown id: tags:

Setting up model variables

%% Cell type:code id: tags:

``` python
from deepchem.featurizers.coulomb_matrices import CoulombMatrixEig
compound_featurizers = [CoulombMatrixEig(23, remove_hydrogens=False)]
complex_featurizers = []
tasks = ["atomization_energy"]
task_type = "regression"
task_types = {task: task_type for task in tasks}
input_file = "../datasets/gdb1k.sdf"
smiles_field = "smiles"
mol_field = "mol"
```

%% Cell type:markdown id: tags:

Load featurized data

%% Cell type:code id: tags:

``` python
featurizers = compound_featurizers + complex_featurizers
featurizer = DataFeaturizer(tasks=tasks,
                            smiles_field=smiles_field,
                            mol_field=mol_field,
                            compound_featurizers=compound_featurizers,
                            complex_featurizers=complex_featurizers, verbosity="high")
```

%% Cell type:code id: tags:

``` python
featurized_samples = featurizer.featurize(input_file, feature_dir, samples_dir)
```

%% Output

    Loading raw samples now.
    Reading structures from ../datasets/gdb1k.sdf.
    Loaded raw data frame from file.
    About to preprocess samples.
    Sharding and standardizing into shard-1 / 1 shards
    Currently featurizing feature_type: CoulombMatrixEig
    Featurizing sample 0
    Saving compounds to disk

%% Cell type:markdown id: tags:

Perform Train, Validation, and Testing Split

%% Cell type:code id: tags:

``` python
from deepchem.splits import RandomSplitter
random_splitter = RandomSplitter()
train_samples, valid_samples, test_samples = random_splitter.train_valid_test_split(featurized_samples,
    train_dir, valid_dir, test_dir)
```

%% Cell type:markdown id: tags:

Creating datasets

%% Cell type:code id: tags:

``` python
train_dataset = Dataset(data_dir=train_dir, samples=train_samples,
                        featurizers=featurizers, tasks=tasks)
valid_dataset = Dataset(data_dir=valid_dir, samples=valid_samples,
                        featurizers=featurizers, tasks=tasks)
test_dataset = Dataset(data_dir=test_dir, samples=test_samples,
                       featurizers=featurizers, tasks=tasks)
```

%% Output

    /home/joegomes/deepchem/deepchem/datasets/__init__.py:402: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      if features[feature_ind] == "":
    /home/joegomes/deepchem/deepchem/datasets/__init__.py:411: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      if y[ind, task] == "":

%% Cell type:markdown id: tags:

Transforming datasets

%% Cell type:code id: tags:

``` python
input_transformers = [NormalizationTransformer(transform_X=True, dataset=train_dataset)]
output_transformers = [NormalizationTransformer(transform_y=True, dataset=train_dataset)]
transformers = input_transformers + output_transformers
for transformer in transformers:
    transformer.transform(train_dataset)
for transformer in transformers:
    transformer.transform(valid_dataset)
for transformer in transformers:
    transformer.transform(test_dataset)
```

%% Cell type:markdown id: tags:

Fit Random Forest with hyperparameter search

%% Cell type:code id: tags:

``` python
def rf_model_builder(tasks, task_types, params_dict, model_dir, verbosity=None):
    """Builds random forests given hyperparameters.

    """
    n_estimators = params_dict["n_estimators"]
    max_features = params_dict["max_features"]
    return SklearnModel(
        tasks, task_types, params_dict, model_dir,
        mode="regression",
        model_instance=RandomForestRegressor(n_estimators=n_estimators,
                                             max_features=max_features))

params_dict = {
    "n_estimators": [10, 100],
    "data_shape": [train_dataset.get_data_shape()],
    "max_features": ["auto"],
    }

metric = Metric(metrics.mean_absolute_error)
optimizer = HyperparamOpt(rf_model_builder, tasks, task_types, verbosity="low")
best_model, best_hyperparams, all_results = optimizer.hyperparam_search(
    params_dict, train_dataset, valid_dataset, output_transformers,
    metric, use_max="False", logdir=None)
```

%% Output

    Model 1/2, Metric mean_absolute_error, Validation set 0: 27.577397
    	best_validation_score so far: 27.577397
    Model 2/2, Metric mean_absolute_error, Validation set 1: 24.751563
    	best_validation_score so far: 27.577397
    Best hyperparameters: (10, (23,), u'auto')
    train_score: 10.962967
    validation_score: 27.577397

%% Cell type:code id: tags:

``` python
def kr_model_builder(tasks, task_types, params_dict, model_dir, verbosity=None):
    """Builds random forests given hyperparameters.

    """
    kernel = params_dict["kernel"]
    alpha = params_dict["alpha"]
    gamma = params_dict["gamma"]
    return SklearnModel(
        tasks, task_types, params_dict, model_dir,
        mode="regression",
        model_instance=KernelRidge(alpha=alpha,kernel=kernel,gamma=gamma))

params_dict = {
    "kernel": ["laplacian"],
    "alpha": [0.0001],
    "gamma": [0.0001]
    }

metric = Metric(metrics.mean_absolute_error)
optimizer = HyperparamOpt(kr_model_builder, tasks, task_types, verbosity="low")
best_model, best_hyperparams, all_results = optimizer.hyperparam_search(
    params_dict, train_dataset, valid_dataset, output_transformers,
    metric, use_max="False", logdir=None)
```

%% Output

    Model 1/1, Metric mean_absolute_error, Validation set 0: 11.888263
    	best_validation_score so far: 11.888263
    Best hyperparameters: (u'laplacian', 0.0001, 0.0001)
    train_score: 8.300859
    validation_score: 11.888263

%% Cell type:code id: tags:

``` python
```
Loading