Beispiel #1
0
def test_scorer():
    np.random.seed(42)
    # toy example with made up values
    mols = list(oddt.toolkit.readfile('sdf', actives_sdf))

    values = [0]*5 + [1]*5
    test_values = [0, 0, 1, 1, 0]

    if oddt.toolkit.backend == 'ob':
        fp = 'fp2'
    else:
        fp = 'rdkit'

    simple_scorer = scorer(neuralnetwork(), fingerprints(fp))
    simple_scorer.fit(mols[:10], values)
    predictions = simple_scorer.predict(mols[10:15])
    assert_array_almost_equal(predictions, [0, 1, 0, 1, 0])

    score = simple_scorer.score(mols[10:15], test_values)
    assert_almost_equal(score, 0.6)

    scored_mols = [simple_scorer.predict_ligand(mol) for mol in mols[10:15]]
    single_predictions = [float(mol.data['score']) for mol in scored_mols]
    assert_array_almost_equal(predictions, single_predictions)

    scored_mols_gen = simple_scorer.predict_ligands(mols[10:15])
    assert isinstance(scored_mols_gen, GeneratorType)
    gen_predictions = [float(mol.data['score']) for mol in scored_mols_gen]
    assert_array_almost_equal(predictions, gen_predictions)
Beispiel #2
0
def test_ensemble_model():
    X = np.vstack(
        (np.arange(30, 10,
                   -2, dtype='float64'), np.arange(100,
                                                   90,
                                                   -1,
                                                   dtype='float64'))).T

    Y = np.arange(10, dtype='float64')

    rf = regressors.randomforest(random_state=42)
    nn = regressors.neuralnetwork(solver='lbfgs', random_state=42)
    ensemble = ensemble_model((rf, nn))

    # we do not need to fit underlying models, they change when we fit enseble
    ensemble.fit(X, Y)

    pred = ensemble.predict(X)
    mean_pred = np.vstack((rf.predict(X), nn.predict(X))).mean(axis=0)
    assert_array_almost_equal(pred, mean_pred)
    assert_almost_equal(ensemble.score(X, Y), r2_score(Y, pred))

    # ensemble of a single model should behave exactly like this model
    nn = neuralnetwork(solver='lbfgs', random_state=42)
    ensemble = ensemble_model((nn, ))
    ensemble.fit(X, Y)
    assert_array_almost_equal(ensemble.predict(X), nn.predict(X))
    assert_almost_equal(ensemble.score(X, Y), nn.score(X, Y))
Beispiel #3
0
def test_classifiers():
    # toy data
    X = np.concatenate((np.zeros((5, 2)), np.ones((5, 2))))
    Y = np.concatenate((np.ones(5), np.zeros(5)))

    np.random.seed(42)

    for classifier in (classifiers.svm(probability=True),
                       classifiers.neuralnetwork(random_state=42)):
        classifier.fit(X, Y)

        assert_array_equal(classifier.predict(X), Y)
        assert_equal(classifier.score(X, Y), 1.0)

        prob = classifier.predict_proba(X)
        assert_array_almost_equal(prob, [[0, 1]] * 5 + [[1, 0]] * 5, decimal=1)
        log_prob = classifier.predict_log_proba(X)
        assert_array_almost_equal(np.log(prob), log_prob)

        pickled = pickle.dumps(classifier)
        reloaded = pickle.loads(pickled)
        prob_reloaded = reloaded.predict_proba(X)
        assert_array_almost_equal(prob, prob_reloaded)
Beispiel #4
0
import pickle
import numpy as np

from numpy.testing import assert_array_almost_equal, assert_array_equal
import pytest

from oddt.scoring.models import classifiers, regressors


@pytest.mark.filterwarnings('ignore:Stochastic Optimizer')
@pytest.mark.parametrize('cls', [
    classifiers.svm(probability=True),
    classifiers.neuralnetwork(random_state=42)
])
def test_classifiers(cls):
    # toy data
    X = np.concatenate((np.zeros((5, 2)), np.ones((5, 2))))
    Y = np.concatenate((np.ones(5), np.zeros(5)))

    np.random.seed(42)

    cls.fit(X, Y)

    assert_array_equal(cls.predict(X), Y)
    assert cls.score(X, Y) == 1.0

    prob = cls.predict_proba(X)
    assert_array_almost_equal(prob, [[0, 1]] * 5 + [[1, 0]] * 5, decimal=1)
    log_prob = cls.predict_log_proba(X)
    assert_array_almost_equal(np.log(prob), log_prob)