Commit 485efaee authored by joegomes's avatar joegomes
Browse files

Add .sdf reader test

parent 4710045c
Loading
Loading
Loading
Loading
+160 −0
Original line number Diff line number Diff line
Generated by ForceBalance from calcs/cluster-02/VLE/250K/00/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
    0.3522   -0.0789   -1.1805 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.1361   -1.0054   -1.2859 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.2308    0.3743   -1.7895 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.2501    0.1228    1.2735 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.1609   -0.0681    0.3398 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.1927    0.0889    1.4364 H   0  0  0  0  0  0  0  0  0  0  0  0
  2  1  1  0  0  0  0
  3  1  1  0  0  0  0
  4  6  1  0  0  0  0
  5  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/01/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
   -0.6833    0.9705    0.3745 O   0  0  0  0  0  0  0  0  0  0  0  0
   -1.4703    0.7389   -0.1187 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.0013    1.1666    1.2558 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.8119   -0.9589   -0.4393 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.4470   -1.7720   -0.0901 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.2255   -0.2747   -0.1166 H   0  0  0  0  0  0  0  0  0  0  0  0
  1  3  1  0  0  0  0
  2  1  1  0  0  0  0
  4  6  1  0  0  0  0
  4  5  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/02/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
    0.2250   -1.2400    0.1519 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.0488   -1.9014    0.8211 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0675   -1.6423   -0.6659 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.2524    1.3362   -0.1080 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0379    0.4038   -0.0769 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.4224    1.7191   -0.6687 H   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  3  1  1  0  0  0  0
  4  5  1  0  0  0  0
  6  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/03/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
    0.5442   -1.2553    0.0884 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.2717   -0.3382    0.0601 H   0  0  0  0  0  0  0  0  0  0  0  0
    1.2009   -1.2892    0.7840 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.5724    1.1365   -0.1524 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.9053    1.4770    0.6779 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.1396    1.8852   -0.5627 H   0  0  0  0  0  0  0  0  0  0  0  0
  1  3  1  0  0  0  0
  2  1  1  0  0  0  0
  4  5  1  0  0  0  0
  6  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/04/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
    0.5716   -0.9660    0.8167 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.0990   -0.2491    0.3937 H   0  0  0  0  0  0  0  0  0  0  0  0
    1.4503   -0.9302    0.4386 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.5869    0.8748   -0.7201 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4764    0.7662   -1.6647 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.8321    1.7936   -0.6112 H   0  0  0  0  0  0  0  0  0  0  0  0
  2  1  1  0  0  0  0
  3  1  1  0  0  0  0
  4  6  1  0  0  0  0
  5  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/05/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
   -1.2519    0.2981    0.1241 O   0  0  0  0  0  0  0  0  0  0  0  0
   -1.7775   -0.3172    0.6354 H   0  0  0  0  0  0  0  0  0  0  0  0
   -1.6386    1.1542    0.3076 H   0  0  0  0  0  0  0  0  0  0  0  0
    1.3413   -0.2661   -0.1294 O   0  0  0  0  0  0  0  0  0  0  0  0
    1.5930   -0.9980   -0.6925 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.3851   -0.3004   -0.1042 H   0  0  0  0  0  0  0  0  0  0  0  0
  1  3  1  0  0  0  0
  1  2  1  0  0  0  0
  4  6  1  0  0  0  0
  5  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/06/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
    0.9408   -0.0129    1.0150 O   0  0  0  0  0  0  0  0  0  0  0  0
    1.8624    0.0338    0.7608 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.4636   -0.0294    0.1854 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.9270   -0.0427   -0.9440 O   0  0  0  0  0  0  0  0  0  0  0  0
   -1.7394    0.2078   -0.5042 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.7702    0.6586   -1.5762 H   0  0  0  0  0  0  0  0  0  0  0  0
  2  1  1  0  0  0  0
  3  1  1  0  0  0  0
  4  5  1  0  0  0  0
  6  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/07/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
   -0.2170    0.6223   -1.1153 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.9903    0.6099   -1.6793 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.2030    1.4618   -1.3022 H   0  0  0  0  0  0  0  0  0  0  0  0
    0.2096   -0.6802    1.2222 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.1250   -0.2173    0.4540 H   0  0  0  0  0  0  0  0  0  0  0  0
    1.0515   -1.0371    0.9394 H   0  0  0  0  0  0  0  0  0  0  0  0
  2  1  1  0  0  0  0
  3  1  1  0  0  0  0
  5  4  1  0  0  0  0
  6  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/08/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
    0.4738    0.9229   -0.7887 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.2878    1.1716   -1.3126 H   0  0  0  0  0  0  0  0  0  0  0  0
    1.2208    1.2882   -1.2627 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4689   -1.0248    0.8355 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.8800   -0.5469    1.5558 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.1112   -0.3421    0.2678 H   0  0  0  0  0  0  0  0  0  0  0  0
  2  1  1  0  0  0  0
  3  1  1  0  0  0  0
  4  5  1  0  0  0  0
  6  4  1  0  0  0  0
M  END
$$$$
Generated by ForceBalance from calcs/cluster-02/VLE/250K/09/qchem.out: Frame 1 of 1
 OpenBabel03241615583D

  6  4  0  0  0  0  0  0  0  0999 V2000
    0.3431    0.5546    1.2527 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.9254   -0.1303    1.5813 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0195    0.1937    0.4437 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.3857   -0.4769   -1.1651 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0194   -1.2992   -1.4903 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.2338    0.1499   -1.8724 H   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  3  1  1  0  0  0  0
  5  4  1  0  0  0  0
  6  4  1  0  0  0  0
M  END
$$$$
+11 −0
Original line number Diff line number Diff line
atomization_energy
447.082359
448.859851
450.466600
450.851977
450.894234
450.743387
451.436905
451.559751
451.326782
451.400550
+86 −0
Original line number Diff line number Diff line
"""
Tests for importing .sdf files
"""
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
from deepchem.splits import RandomSplitter
from deepchem.featurizers.featurize import DataFeaturizer
from deepchem.featurizers.coulomb_matrices import CoulombMatrixEig

class TestFeaturizedSamples(unittest.TestCase):
  """
  Test Featurized Samples class.
  """
  def setUp(self):
    self.current_dir = os.path.dirname(os.path.abspath(__file__))
    self.smiles_field = "smiles"
    self.mol_field = "mol"
    self.feature_dir = tempfile.mkdtemp()
    self.samples_dir = tempfile.mkdtemp()
    self.train_dir = tempfile.mkdtemp()
    self.valid_dir = tempfile.mkdtemp()
    self.test_dir = tempfile.mkdtemp()

  def _featurize_train_valid_test_split(self, splittype, input_file, tasks,
                                        frac_train, frac_valid, frac_test):
    # Featurize input
    compound_featurizers = [CoulombMatrixEig(6, remove_hydrogens=False)]
    complex_featurizers = []
    featurizers = compound_featurizers + complex_featurizers

    input_file = os.path.join(self.current_dir, input_file)
    featurizer = DataFeaturizer(tasks=tasks,
                                smiles_field=self.smiles_field,
                                mol_field=self.mol_field,
                                compound_featurizers=compound_featurizers,
                                complex_featurizers=complex_featurizers,
                                verbosity="low")

    #Featurizes samples and transforms them into NumPy arrays suitable for ML.
    #returns an instance of class FeaturizedSamples()

    samples = featurizer.featurize(input_file, self.feature_dir, self.samples_dir)

    # Splits featurized samples into train/test
    splitter = RandomSplitter()
    if frac_valid > 0:
      train_samples, valid_samples, test_samples = splitter.train_valid_test_split(
          samples, train_dir=self.train_dir, valid_dir=self.valid_dir,
          test_dir=self.test_dir, frac_train=frac_train,
          frac_valid=frac_valid, frac_test=frac_test)

      return train_samples, valid_samples, test_samples
    else:
      train_samples, test_samples = splitter.train_test_split(
          samples, train_dir=self.train_dir, test_dir=self.test_dir,
          frac_train=frac_train)
      return train_samples, test_samples

  def random_test_train_valid_test_split_from_sdf(self):
    """Test of singletask RF ECFP regression API when reading from .sdf file."""
    splittype = "random"
    input_transforms = []
    output_transforms = ["normalize"]
    model_params = {}
    tasks = ["atomization_energy"]
    task_type = "regression"
    task_types = {task: task_type for task in tasks}
    input_file = "data/water.sdf"
    train_samples, valid_samples, test_samples = (
        self._featurize_train_valid_test_split(
            splittype, input_file, tasks, frac_train=.8,
            frac_valid=.1, frac_test=.1))
    assert len(train_samples) == 8
    assert len(valid_samples) == 1
    assert len(test_samples) == 1