Esempio n. 1
0
def test_graph_conv_atom_features():
    tasks, dataset, transformers, metric = get_dataset('regression',
                                                       'Raw',
                                                       num_tasks=1)

    atom_feature_name = 'feature'
    y = []
    for mol in dataset.X:
        atom_features = []
        for atom in mol.GetAtoms():
            val = np.random.normal()
            mol.SetProp("atom %08d %s" % (atom.GetIdx(), atom_feature_name),
                        str(val))
            atom_features.append(np.random.normal())
        y.append([np.sum(atom_features)])

    featurizer = ConvMolFeaturizer(atom_properties=[atom_feature_name])
    X = featurizer.featurize(dataset.X)
    dataset = dc.data.NumpyDataset(X, np.array(y))
    batch_size = 50
    model = GraphConvModel(len(tasks),
                           number_atom_features=featurizer.feature_length(),
                           batch_size=batch_size,
                           mode='regression')

    model.fit(dataset, nb_epoch=1)
    y_pred1 = model.predict(dataset)
Esempio n. 2
0
    def test_graph_conv_atom_features(self):
        tasks, dataset, transformers, metric = self.get_dataset('regression',
                                                                'Raw',
                                                                num_tasks=1)

        atom_feature_name = 'feature'
        y = []
        for mol in dataset.X:
            atom_features = []
            for atom in mol.GetAtoms():
                val = np.random.normal()
                mol.SetProp(
                    "atom %08d %s" % (atom.GetIdx(), atom_feature_name),
                    str(val))
                atom_features.append(np.random.normal())
            y.append(np.sum(atom_features))

        featurizer = ConvMolFeaturizer(atom_properties=[atom_feature_name])
        X = featurizer.featurize(dataset.X)
        dataset = deepchem.data.NumpyDataset(X, np.array(y))
        batch_size = 50
        model = GraphConvTensorGraph(
            len(tasks),
            number_atom_features=featurizer.feature_length(),
            batch_size=batch_size,
            mode='regression')

        model.fit(dataset, nb_epoch=1)
        y_pred1 = model.predict(dataset)
        model.save()

        model2 = TensorGraph.load_from_dir(model.model_dir)
        y_pred2 = model2.predict(dataset)
        self.assertTrue(np.all(y_pred1 == y_pred2))
Esempio n. 3
0
def test_mol_ordering():
    mols = get_molecules()
    featurizer = ConvMolFeaturizer()
    featurized_mols = featurizer.featurize(mols)
    for i in range(len(featurized_mols)):
        atom_features = featurized_mols[i].atom_features
        degree_list = np.expand_dims(featurized_mols[i].degree_list, axis=1)
        atom_features = np.concatenate([degree_list, atom_features], axis=1)
        featurized_mols[i].atom_features = atom_features

    conv_mol = ConvMol.agglomerate_mols(featurized_mols)

    for start, end in conv_mol.deg_slice.tolist():
        members = conv_mol.membership[start:end]
        sorted_members = np.array(sorted(members))
        members = np.array(members)
        assert np.all(sorted_members == members)

    conv_mol_atom_features = conv_mol.get_atom_features()

    adj_number = 0
    for start, end in conv_mol.deg_slice.tolist():
        deg_features = conv_mol_atom_features[start:end]
        adj_number_array = deg_features[:, 0]
        assert np.all(adj_number_array == adj_number)
        adj_number += 1
Esempio n. 4
0
  def test_graph_conv_atom_features(self):
    tasks, dataset, transformers, metric = self.get_dataset(
        'regression', 'Raw', num_tasks=1)

    atom_feature_name = 'feature'
    y = []
    for mol in dataset.X:
      atom_features = []
      for atom in mol.GetAtoms():
        val = np.random.normal()
        mol.SetProp("atom %08d %s" % (atom.GetIdx(), atom_feature_name),
                    str(val))
        atom_features.append(np.random.normal())
      y.append([np.sum(atom_features)])

    featurizer = ConvMolFeaturizer(atom_properties=[atom_feature_name])
    X = featurizer.featurize(dataset.X)
    dataset = dc.data.NumpyDataset(X, np.array(y))
    batch_size = 50
    model = GraphConvModel(
        len(tasks),
        number_atom_features=featurizer.feature_length(),
        batch_size=batch_size,
        mode='regression')

    model.fit(dataset, nb_epoch=1)
    y_pred1 = model.predict(dataset)
    model.save()

    model2 = TensorGraph.load_from_dir(model.model_dir)
    y_pred2 = model2.predict(dataset)
    self.assertTrue(np.allclose(y_pred1, y_pred2))