forked from torchmd/torchmd-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7743556
commit e9082c1
Showing
2 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from pytest import mark, raises | ||
import torch | ||
from torchmdnet.utils import make_splits | ||
|
||
|
||
def sum_lengths(*args): | ||
return sum(map(len, args)) | ||
|
||
|
||
def test_make_splits_outputs(): | ||
result = make_splits(100, 0.7, 0.2, 0.1, 1234) | ||
assert len(result) == 3 | ||
assert isinstance(result[0], torch.Tensor) | ||
assert isinstance(result[1], torch.Tensor) | ||
assert isinstance(result[2], torch.Tensor) | ||
assert sum_lengths(*result) == len(torch.unique(torch.cat(result))) | ||
assert max(map(max, result)) == 99 | ||
assert min(map(min, result)) == 0 | ||
|
||
|
||
@mark.parametrize("dset_len", [5, 1000]) | ||
@mark.parametrize("ratio1", [0.0, 0.3]) | ||
@mark.parametrize("ratio2", [0.0, 0.3]) | ||
@mark.parametrize("ratio3", [0.0, 0.3]) | ||
def test_make_splits_ratios(dset_len, ratio1, ratio2, ratio3): | ||
train, val, test = make_splits(dset_len, ratio1, ratio2, ratio3, 1234) | ||
assert sum_lengths(train, val, test) <= dset_len | ||
assert len(train) == round(ratio1 * dset_len) | ||
assert len(val) == round(ratio2 * dset_len) | ||
# simply multiplying and rounding ratios can lead to values larger than dset_len, | ||
# which make_splits should account for by removing one sample from the test set | ||
if ( | ||
round(ratio1 * dset_len) + round(ratio2 * dset_len) + round(ratio3 * dset_len) | ||
> dset_len | ||
): | ||
assert len(test) == round(ratio3 * dset_len) - 1 | ||
else: | ||
assert len(test) == round(ratio3 * dset_len) | ||
|
||
|
||
def test_make_splits_sizes(): | ||
assert sum_lengths(*make_splits(100, 70, 20, 10, 1234)) == 100 | ||
assert sum_lengths(*make_splits(100, 70, 20, None, 1234)) == 100 | ||
assert sum_lengths(*make_splits(100, 70, None, 10, 1234)) == 100 | ||
assert sum_lengths(*make_splits(100, None, 20, 10, 1234)) == 100 | ||
assert sum_lengths(*make_splits(100, 70, 20, 0.1, 1234)) == 100 | ||
assert sum_lengths(*make_splits(100, 70, 20, 0.05, 1234)) == 95 | ||
|
||
|
||
def test_make_splits_errors(): | ||
with raises(AssertionError): | ||
make_splits(100, 0.5, 0.5, 0.5, 1234) | ||
with raises(AssertionError): | ||
make_splits(100, 50, 50, 50, 1234) | ||
with raises(AssertionError): | ||
make_splits(100, None, None, 5, 1234) | ||
with raises(AssertionError): | ||
make_splits(100, 60, 60, None, 1234) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters