Spaces:
Build error
Build error
File size: 6,193 Bytes
3ea26d1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
from astartes import train_val_test_split
from astartes.utils.warnings import NormalizationWarning
import numpy as np
import pytest
from rdkit import Chem
from chemprop.data.splitting import _unpack_astartes_result, make_split_indices
@pytest.fixture(params=[["C", "CC", "CCC", "CN", "CCN", "CCCN", "CCCCN", "CO", "CCO", "CCCO"]])
def mol_data(request):
"""A dataset with single molecules"""
return [Chem.MolFromSmiles(smi) for smi in request.param]
@pytest.fixture(params=[["C", "CC", "CN", "CN", "CO", "C"]])
def mol_data_with_repeated_mols(request):
"""A dataset with repeated single molecules"""
return [Chem.MolFromSmiles(smi) for smi in request.param]
@pytest.fixture(params=[["C", "CC", "CCC", "C1CC1", "C1CCC1"]])
def molecule_dataset_with_rings(request):
"""A dataset with rings (for scaffold splitting)"""
return [Chem.MolFromSmiles(smi) for smi in request.param]
def test_splits_sum1_warning(mol_data):
"""Testing that the splits are normalized to 1, for overspecified case."""
with pytest.warns(NormalizationWarning):
make_split_indices(mols=mol_data, sizes=(0.4, 0.6, 0.2))
def test_splits_sum2_warning(mol_data):
"""Testing that the splits are normalized to 1, for underspecified case."""
with pytest.warns(NormalizationWarning):
make_split_indices(mols=mol_data, sizes=(0.1, 0.1, 0.1))
def test_three_splits_provided(mol_data):
"""Testing that three splits are provided"""
with pytest.raises(ValueError):
make_split_indices(mols=mol_data, sizes=(0.8, 0.2))
def test_seed0(mol_data):
"""
Testing that make_split_indices can get expected output using astartes as backend for random split with seed 0.
Note: the behaviour of randomness for data splitting is not controlled by chemprop but by the chosen backend.
"""
train, val, test = make_split_indices(mols=mol_data, seed=0)
train_astartes, val_astartes, test_astartes = _unpack_astartes_result(
train_val_test_split(np.arange(len(mol_data)), sampler="random", random_state=0), True
)
assert set(train[0]) == set(train_astartes)
assert set(val[0]) == set(val_astartes)
assert set(test[0]) == set(test_astartes)
def test_seed100(mol_data):
"""
Testing that make_split_indices can get expected output using astartes as backend for random split with seed 100.
Note: the behaviour of randomness for data splitting is not controlled by chemprop but by the chosen backend.
"""
train, val, test = make_split_indices(mols=mol_data, seed=100)
train_astartes, val_astartes, test_astartes = _unpack_astartes_result(
train_val_test_split(np.arange(len(mol_data)), sampler="random", random_state=100), True
)
assert set(train[0]) == set(train_astartes)
assert set(val[0]) == set(val_astartes)
assert set(test[0]) == set(test_astartes)
def test_split_4_4_2(mol_data):
"""Testing the random split with changed sizes"""
train, val, test = make_split_indices(mols=mol_data, sizes=(0.4, 0.4, 0.2))
train_astartes, val_astartes, test_astartes = _unpack_astartes_result(
train_val_test_split(
np.arange(len(mol_data)),
sampler="random",
train_size=0.4,
val_size=0.4,
test_size=0.2,
random_state=0,
),
True,
)
assert set(train[0]) == set(train_astartes)
assert set(val[0]) == set(val_astartes)
assert set(test[0]) == set(test_astartes)
def test_split_empty_validation_set(mol_data):
"""Testing the random split with an empty validation set"""
train, val, test = make_split_indices(mols=mol_data, sizes=(0.4, 0, 0.6))
assert set(val[0]) == set([])
def test_random_split(mol_data_with_repeated_mols):
"""
Testing if random split yield expected results.
Note: This test mainly serves as a red flag. Test failure strongly indicates unexpected change of data splitting backend that needs attention.
"""
split_type = "random"
train, val, test = make_split_indices(
mols=mol_data_with_repeated_mols, sizes=(0.4, 0.4, 0.2), split=split_type
)
assert train[0] == [2, 1]
def test_repeated_smiles(mol_data_with_repeated_mols):
"""
Testing if random split with repeated smiles yield expected results.
Note: This test mainly serves as a red flag. Test failure strongly indicates unexpected change of data splitting backend that needs attention.
"""
split_type = "random_with_repeated_smiles"
train, val, test = make_split_indices(
mols=mol_data_with_repeated_mols, sizes=(0.8, 0.0, 0.2), split=split_type
)
assert train[0] == [4, 1, 0, 5]
assert test[0] == [2, 3]
def test_kennard_stone(mol_data):
"""
Testing if Kennard-Stone split yield expected results.
Note: This test mainly serves as a red flag. Test failure strongly indicates unexpected change of data splitting backend that needs attention.
"""
split_type = "kennard_stone"
train, val, test = make_split_indices(mols=mol_data, sizes=(0.4, 0.4, 0.2), split=split_type)
assert set(test[0]) == set([9, 5])
def test_kmeans(mol_data):
"""
Testing if Kmeans split yield expected results.
Note: This test mainly serves as a red flag. Test failure strongly indicates unexpected change of data splitting backend that needs attention.
"""
split_type = "kmeans"
train, val, test = make_split_indices(mols=mol_data, sizes=(0.5, 0.0, 0.5), split=split_type)
assert train[0] == [0, 1, 2, 3, 7, 8, 9]
def test_scaffold(molecule_dataset_with_rings):
"""
Testing if Bemis-Murcko Scaffolds split yield expected results.
Note: This test mainly serves as a red flag. Test failure strongly indicates unexpected change of data splitting backend that needs attention.
"""
split_type = "scaffold_balanced"
train, val, test = make_split_indices(
mols=molecule_dataset_with_rings, sizes=(0.3, 0.3, 0.3), split=split_type
)
assert train[0] == [0, 1, 2]
|