Commit f17d79f6 authored by Shakthi Visagan's avatar Shakthi Visagan
Browse files

test lint error

parent 995cfb79
Loading
Loading
Loading
Loading
+559 −468
Original line number Diff line number Diff line
@@ -4,10 +4,7 @@ import tensorflow as tf
import deepchem.models.layers as layers
from tensorflow.python.framework import test_util


class TestLayers(test_util.TensorFlowTestCase):

  def test_cosine_dist(self):
def test_cosine_dist():
  """Test invoking _cosine_dist."""
  x = tf.ones((5, 4), dtype=tf.dtypes.float32, name=None)
  y_same = tf.ones((5, 4), dtype=tf.dtypes.float32, name=None)
@@ -29,7 +26,7 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert tf.reduce_sum(cos_sim_orth) == 0  # True
  assert all([cos_sim_orth.shape[dim] == 256 for dim in range(2)])  # True

  def test_highway(self):
def test_highway():
  """Test invoking Highway."""
  width = 5
  batch_size = 10
@@ -51,7 +48,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  result3 = layer(input)
  assert np.allclose(result, result3)

  def test_combine_mean_std(self):

def test_combine_mean_std():
  """Test invoking CombineMeanStd."""
  mean = np.random.rand(5, 3).astype(np.float32)
  std = np.random.rand(5, 3).astype(np.float32)
@@ -62,7 +60,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert not np.array_equal(result2, mean)
  assert np.allclose(result2, mean, atol=0.1)

  def test_stack(self):

def test_stack():
  """Test invoking Stack."""
  input1 = np.random.rand(5, 4).astype(np.float32)
  input2 = np.random.rand(5, 4).astype(np.float32)
@@ -71,7 +70,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert np.array_equal(input1, result[:, 0, :])
  assert np.array_equal(input2, result[:, 1, :])

  def test_variable(self):

def test_variable():
  """Test invoking Variable."""
  value = np.random.rand(5, 4).astype(np.float32)
  layer = layers.Variable(value)
@@ -80,7 +80,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert np.allclose(result, value)
  assert len(layer.trainable_variables) == 1

  def test_interatomic_l2_distances(self):

def test_interatomic_l2_distances():
  """Test invoking InteratomicL2Distances."""
  atoms = 5
  neighbors = 2
@@ -95,13 +96,14 @@ class TestLayers(test_util.TensorFlowTestCase):
      dist2 = np.dot(delta, delta)
      assert np.allclose(dist2, result[atom, neighbor])

  def test_weave_layer(self):

def test_weave_layer():
  """Test invoking WeaveLayer."""
  out_channels = 2
  n_atoms = 4  # In CCC and C, there are 4 atoms
  raw_smiles = ['CCC', 'C']
    import rdkit
    mols = [rdkit.Chem.MolFromSmiles(s) for s in raw_smiles]
  from rdkit import Chem
  mols = [Chem.MolFromSmiles(s) for s in raw_smiles]
  featurizer = dc.feat.WeaveFeaturizer()
  mols = featurizer.featurize(mols)
  weave = layers.WeaveLayer()
@@ -137,13 +139,89 @@ class TestLayers(test_util.TensorFlowTestCase):
  outputs = weave(inputs)
  assert len(outputs) == 2

  def test_graph_conv(self):

def test_weave_gather():
  """Test invoking WeaveGather."""
  out_channels = 2
  n_atoms = 4  # In CCC and C, there are 4 atoms
  raw_smiles = ['CCC', 'C']
  from rdkit import Chem
  mols = [Chem.MolFromSmiles(s) for s in raw_smiles]
  featurizer = dc.feat.WeaveFeaturizer()
  mols = featurizer.featurize(mols)
  atom_feat = []
  atom_split = []
  for im, mol in enumerate(mols):
    n_atoms = mol.get_num_atoms()
    atom_split.extend([im] * n_atoms)

    # atom features
    atom_feat.append(mol.get_atom_features())
  inputs = [
      np.array(np.concatenate(atom_feat, axis=0), dtype=np.float32),
      np.array(atom_split)
  ]
  # Try without compression
  gather = layers.WeaveGather(batch_size=2, n_input=75, gaussian_expand=True)
  # Outputs should be [mol1_vec, mol2_vec)
  outputs = gather(inputs)
  assert len(outputs) == 2
  assert np.array(outputs[0]).shape == (11 * 75,)
  assert np.array(outputs[1]).shape == (11 * 75,)

  # Try with compression
  gather = layers.WeaveGather(
      batch_size=2,
      n_input=75,
      gaussian_expand=True,
      compress_post_gaussian_expansion=True)
  # Outputs should be [mol1_vec, mol2_vec)
  outputs = gather(inputs)
  assert len(outputs) == 2
  assert np.array(outputs[0]).shape == (75,)
  assert np.array(outputs[1]).shape == (75,)


def test_weave_gather_gaussian_histogram():
  """Test Gaussian Histograms."""
  import tensorflow as tf
  from rdkit import Chem
  out_channels = 2
  n_atoms = 4  # In CCC and C, there are 4 atoms
  raw_smiles = ['CCC', 'C']
  mols = [Chem.MolFromSmiles(s) for s in raw_smiles]
  featurizer = dc.feat.WeaveFeaturizer()
  mols = featurizer.featurize(mols)
  gather = layers.WeaveGather(batch_size=2, n_input=75)
  atom_feat = []
  atom_split = []
  for im, mol in enumerate(mols):
    n_atoms = mol.get_num_atoms()
    atom_split.extend([im] * n_atoms)

    # atom features
    atom_feat.append(mol.get_atom_features())
  inputs = [
      np.array(np.concatenate(atom_feat, axis=0), dtype=np.float32),
      np.array(atom_split)
  ]
  #per_mol_features = tf.math.segment_sum(inputs[0], inputs[1])
  outputs = gather.gaussian_histogram(inputs[0])
  # Gaussian histograms expands into 11 Gaussian buckets.
  assert np.array(outputs).shape == (
      4,
      11 * 75,
  )
  #assert np.array(outputs[1]).shape == (11 * 75,)


def test_graph_conv():
  """Test invoking GraphConv."""
  out_channels = 2
  n_atoms = 4  # In CCC and C, there are 4 atoms
  raw_smiles = ['CCC', 'C']
    import rdkit
    mols = [rdkit.Chem.MolFromSmiles(s) for s in raw_smiles]
  from rdkit import Chem
  mols = [Chem.MolFromSmiles(s) for s in raw_smiles]
  featurizer = dc.feat.graph_features.ConvMolFeaturizer()
  mols = featurizer.featurize(mols)
  multi_mol = dc.feat.mol_graphs.ConvMol.agglomerate_mols(mols)
@@ -158,12 +236,13 @@ class TestLayers(test_util.TensorFlowTestCase):
  num_deg = 2 * layer.max_degree + (1 - layer.min_degree)
  assert len(layer.trainable_variables) == 2 * num_deg

  def test_graph_pool(self):

def test_graph_pool():
  """Test invoking GraphPool."""
  n_atoms = 4  # In CCC and C, there are 4 atoms
  raw_smiles = ['CCC', 'C']
    import rdkit
    mols = [rdkit.Chem.MolFromSmiles(s) for s in raw_smiles]
  from rdkit import Chem
  mols = [Chem.MolFromSmiles(s) for s in raw_smiles]
  featurizer = dc.feat.graph_features.ConvMolFeaturizer()
  mols = featurizer.featurize(mols)
  multi_mol = dc.feat.mol_graphs.ConvMol.agglomerate_mols(mols)
@@ -176,14 +255,15 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert result.shape[0] == n_atoms
  # TODO What should shape[1] be?  It's not documented.

  def test_graph_gather(self):

def test_graph_gather():
  """Test invoking GraphGather."""
  batch_size = 2
  n_features = 75
  n_atoms = 4  # In CCC and C, there are 4 atoms
  raw_smiles = ['CCC', 'C']
    import rdkit
    mols = [rdkit.Chem.MolFromSmiles(s) for s in raw_smiles]
  from rdkit import Chem
  mols = [Chem.MolFromSmiles(s) for s in raw_smiles]
  featurizer = dc.feat.graph_features.ConvMolFeaturizer()
  mols = featurizer.featurize(mols)
  multi_mol = dc.feat.mol_graphs.ConvMol.agglomerate_mols(mols)
@@ -196,7 +276,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  # TODO(rbharath): Why is it 2*n_features instead of n_features?
  assert result.shape == (batch_size, 2 * n_features)

  def test_lstm_step(self):

def test_lstm_step():
  """Test invoking LSTMStep."""
  max_depth = 5
  n_test = 5
@@ -212,7 +293,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert c_out.shape == (n_test, n_feat)
  assert len(layer.trainable_variables) == 1

  def test_attn_lstm_embedding(self):

def test_attn_lstm_embedding():
  """Test invoking AttnLSTMEmbedding."""
  max_depth = 5
  n_test = 5
@@ -226,7 +308,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert support_out.shape == (n_support, n_feat)
  assert len(layer.trainable_variables) == 4

  def test_iter_ref_lstm_embedding(self):

def test_iter_ref_lstm_embedding():
  """Test invoking IterRefLSTMEmbedding."""
  max_depth = 5
  n_test = 5
@@ -240,7 +323,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert support_out.shape == (n_support, n_feat)
  assert len(layer.trainable_variables) == 8

  def test_vina_free_energy(self):

def test_vina_free_energy():
  """Test invoking VinaFreeEnergy."""
  n_atoms = 5
  m_nbrs = 1
@@ -250,8 +334,7 @@ class TestLayers(test_util.TensorFlowTestCase):
  stop = 4
  X = np.random.rand(n_atoms, ndim).astype(np.float32)
  Z = np.random.randint(0, 2, (n_atoms)).astype(np.float32)
    layer = layers.VinaFreeEnergy(n_atoms, m_nbrs, ndim, nbr_cutoff, start,
                                  stop)
  layer = layers.VinaFreeEnergy(n_atoms, m_nbrs, ndim, nbr_cutoff, start, stop)
  result = layer([X, Z])
  assert len(layer.trainable_variables) == 6
  assert result.shape == tuple()
@@ -259,8 +342,7 @@ class TestLayers(test_util.TensorFlowTestCase):
  # Creating a second layer should produce different results, since it has
  # different random weights.

    layer2 = layers.VinaFreeEnergy(n_atoms, m_nbrs, ndim, nbr_cutoff, start,
                                   stop)
  layer2 = layers.VinaFreeEnergy(n_atoms, m_nbrs, ndim, nbr_cutoff, start, stop)
  result2 = layer2([X, Z])
  assert not np.allclose(result, result2)

@@ -269,7 +351,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  result3 = layer([X, Z])
  assert np.allclose(result, result3)

  def test_weighted_linear_combo(self):

def test_weighted_linear_combo():
  """Test invoking WeightedLinearCombo."""
  input1 = np.random.rand(5, 10).astype(np.float32)
  input2 = np.random.rand(5, 10).astype(np.float32)
@@ -279,7 +362,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  expected = input1 * layer.trainable_variables[0] + input2 * layer.trainable_variables[1]
  assert np.allclose(result, expected)

  def test_neighbor_list(self):

def test_neighbor_list():
  """Test invoking NeighborList."""
  N_atoms = 5
  start = 0
@@ -293,25 +377,25 @@ class TestLayers(test_util.TensorFlowTestCase):
  result = layer(coords)
  assert result.shape == (N_atoms, M_nbrs)

  def test_atomic_convolution(self):

def test_atomic_convolution():
  """Test invoking AtomicConvolution."""
  batch_size = 4
  max_atoms = 5
  max_neighbors = 2
  dimensions = 3
  params = [[5.0, 2.0, 0.5], [10.0, 2.0, 0.5]]
    input1 = np.random.rand(batch_size, max_atoms,
                            dimensions).astype(np.float32)
  input1 = np.random.rand(batch_size, max_atoms, dimensions).astype(np.float32)
  input2 = np.random.randint(
      max_atoms, size=(batch_size, max_atoms, max_neighbors))
    input3 = np.random.randint(
        1, 10, size=(batch_size, max_atoms, max_neighbors))
  input3 = np.random.randint(1, 10, size=(batch_size, max_atoms, max_neighbors))
  layer = layers.AtomicConvolution(radial_params=params)
  result = layer([input1, input2, input3])
  assert result.shape == (batch_size, max_atoms, len(params))
  assert len(layer.trainable_variables) == 3

  def test_alpha_share_layer(self):

def test_alpha_share_layer():
  """Test invoking AlphaShareLayer."""
  batch_size = 10
  length = 6
@@ -336,14 +420,16 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert np.allclose(result[0], result3[0])
  assert np.allclose(result[1], result3[1])

  def test_sluice_loss(self):

def test_sluice_loss():
  """Test invoking SluiceLoss."""
  input1 = np.ones((3, 4)).astype(np.float32)
  input2 = np.ones((2, 2)).astype(np.float32)
  result = layers.SluiceLoss()([input1, input2])
  assert np.allclose(result, 40.0)

  def test_beta_share(self):

def test_beta_share():
  """Test invoking BetaShare."""
  batch_size = 10
  length = 6
@@ -366,7 +452,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  result3 = layer([input1, input2])
  assert np.allclose(result, result3)

  def test_ani_feat(self):

def test_ani_feat():
  """Test invoking ANIFeat."""
  batch_size = 10
  max_atoms = 5
@@ -376,7 +463,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  # TODO What should the output shape be?  It's not documented, and there
  # are no other test cases for it.

  def test_graph_embed_pool_layer(self):

def test_graph_embed_pool_layer():
  """Test invoking GraphEmbedPoolLayer."""
  V = np.random.uniform(size=(10, 100, 50)).astype(np.float32)
  adjs = np.random.uniform(size=(10, 100, 5, 100)).astype(np.float32)
@@ -399,7 +487,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  assert np.allclose(result[0], result3[0])
  assert np.allclose(result[1], result3[1])

  def test_graph_cnn(self):

def test_graph_cnn():
  """Test invoking GraphCNN."""
  V = np.random.uniform(size=(10, 100, 50)).astype(np.float32)
  adjs = np.random.uniform(size=(10, 100, 5, 100)).astype(np.float32)
@@ -419,7 +508,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  result3 = layer([V, adjs])
  assert np.allclose(result, result3)

  def test_DAG_layer(self):

def test_DAG_layer():
  """Test invoking DAGLayer."""
  batch_size = 10
  n_graph_feat = 30
@@ -454,7 +544,8 @@ class TestLayers(test_util.TensorFlowTestCase):
  ## TODO(rbharath): What is the shape of outputs supposed to be?
  ## I'm getting (7, 30) here. Where does 7 come from??

  def test_DAG_gather(self):

def test_DAG_gather():
  """Test invoking DAGGather."""
  # TODO(rbharath): We need more documentation about why
  # these numbers work.