Skip to content

Commit

Permalink
Merge pull request #244 from emilazy/push-qonlzoyrmkms
Browse files Browse the repository at this point in the history
Migrate tests to pytest
  • Loading branch information
timodonnell authored Nov 18, 2024
2 parents e2b5d8d + 47b920c commit 8e9f353
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 92 deletions.
4 changes: 2 additions & 2 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ From a checkout you can run the unit tests with:

.. code-block:: shell
$ pip install nose
$ nosetests .
$ pip install pytest
$ pytest
Using conda
Expand Down
1 change: 0 additions & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
# Without ignoring this, we get errors like:
# E:249,20: Module 'numpy' has no 'nan' member (no-member)
ignored-modules = numpy
ignored-classes = nose.tools
2 changes: 1 addition & 1 deletion test/expensive_verify_pretrain_optimizable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Expensive test - not run by nose.
# Expensive test - not run by pytest.

from mhcflurry import train_pan_allele_models_command
from mhcflurry.downloads import get_path
Expand Down
3 changes: 1 addition & 2 deletions test/test_amino_acid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
initialize()

from mhcflurry import amino_acid
from nose.tools import eq_
from numpy.testing import assert_equal
import pandas

Expand Down Expand Up @@ -33,7 +32,7 @@ def test_index_and_one_hot_encoding():
one_hot = amino_acid.fixed_vectors_encoding(
index_encoding,
letter_to_vector_df)
eq_(one_hot.shape, (2, 4, 3))
assert one_hot.shape == (2, 4, 3)
assert_equal(
one_hot[0],
[
Expand Down
4 changes: 2 additions & 2 deletions test/test_calibrate_percentile_ranks_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def run_and_check(n_jobs=0, delete=True, additional_args=[]):
print("Saved predictor to", dest_models_dir)

new_predictor = Class1AffinityPredictor.load(dest_models_dir)
assert_equal(len(new_predictor.allele_to_percent_rank_transform), 0)
assert len(new_predictor.allele_to_percent_rank_transform) == 0

args = [
"mhcflurry-calibrate-percentile-ranks",
Expand All @@ -58,7 +58,7 @@ def run_and_check(n_jobs=0, delete=True, additional_args=[]):
subprocess.check_call(args)

new_predictor = Class1AffinityPredictor.load(dest_models_dir)
assert_equal(len(new_predictor.allele_to_percent_rank_transform), 2)
assert len(new_predictor.allele_to_percent_rank_transform) == 2

if delete:
print("Deleting: %s" % dest_models_dir)
Expand Down
13 changes: 6 additions & 7 deletions test/test_class1_affinity_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from mhcflurry import Class1AffinityPredictor

from nose.tools import eq_, assert_raises
from numpy import testing

from mhcflurry.downloads import get_path
Expand Down Expand Up @@ -167,7 +166,7 @@ def test_class1_affinity_predictor_a0205_memorize_training_data():
predictor.calibrate_percentile_ranks(num_peptides_per_length=1000)
ic50_pred = predictor.predict(df.peptide.values, allele=allele)
ic50_true = df.measurement_value.values
eq_(len(ic50_pred), len(ic50_true))
assert len(ic50_pred) == len(ic50_true)
testing.assert_allclose(
numpy.log(ic50_pred), numpy.log(ic50_true), rtol=0.2, atol=0.2
)
Expand All @@ -184,17 +183,17 @@ def test_class1_affinity_predictor_a0205_memorize_training_data():

# Test an unknown allele
print("Starting unknown allele check")
eq_(predictor.supported_alleles, [allele])
assert predictor.supported_alleles == [allele]
ic50_pred = predictor.predict(df.peptide.values, allele="HLA-A*02:01", throw=False)
assert numpy.isnan(ic50_pred).all()

assert_raises(
testing.assert_raises(
ValueError, predictor.predict, df.peptide.values, allele="HLA-A*02:01"
)

eq_(predictor.supported_alleles, [allele])
assert_raises(ValueError, predictor.predict, ["AAAAA"], allele=allele) # too short
assert_raises(
assert predictor.supported_alleles == [allele]
testing.assert_raises(ValueError, predictor.predict, ["AAAAA"], allele=allele) # too short
testing.assert_raises(
ValueError,
predictor.predict,
["AAAAAAAAAAAAAAAAAAAA"], # too long
Expand Down
10 changes: 4 additions & 6 deletions test/test_class1_neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import pytest

from nose.tools import eq_, assert_less, assert_greater, assert_almost_equal

import pandas

from mhcflurry.class1_neural_network import Class1NeuralNetwork
Expand Down Expand Up @@ -53,7 +51,7 @@ def test_class1_neural_network_a0205_training_accuracy(setup_module):
predictor.fit(df.peptide.values, df.measurement_value.values)
ic50_pred = predictor.predict(df.peptide.values)
ic50_true = df.measurement_value.values
eq_(len(ic50_pred), len(ic50_true))
assert len(ic50_pred) == len(ic50_true)
testing.assert_allclose(
numpy.log(ic50_pred), numpy.log(ic50_true), rtol=0.2, atol=0.2
)
Expand All @@ -75,7 +73,7 @@ def test_class1_neural_network_a0205_training_accuracy(setup_module):
)
predictor2 = Class1NeuralNetwork(**hyperparameters2)
predictor2.fit(df.peptide.values, df.measurement_value.values, verbose=0)
eq_(predictor.network().to_json(), predictor2.network().to_json())
assert predictor.network().to_json() == predictor2.network().to_json()


def test_inequalities(setup_module):
Expand Down Expand Up @@ -146,8 +144,8 @@ def test_inequalities(setup_module):

# Binders should be stronger
for pred in ["prediction1", "prediction2"]:
assert_less(df.loc[df.value < 1000, pred].mean(), 500)
assert_greater(df.loc[df.value >= 1000, pred].mean(), 500)
assert df.loc[df.value < 1000, pred].mean() < 500
assert df.loc[df.value >= 1000, pred].mean() > 500

# For the binders, the (=) on the weak-binding measurement (100) in
# inequality1 should make the prediction weaker, whereas for inequality2
Expand Down
25 changes: 12 additions & 13 deletions test/test_class1_presentation_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pickle

from numpy.testing import assert_, assert_equal, assert_allclose, assert_array_equal
from nose.tools import assert_greater, assert_less
import pytest
import numpy

Expand Down Expand Up @@ -108,17 +107,17 @@ def add_prediction_cols(test_df, predictor):

print("AUC", score1, score2)

assert_greater(score1, 0.8)
assert_greater(score2, 0.8)
assert score1 > 0.8
assert score2 > 0.8

score1 = roc_auc_score(
test_df.hit.values, -test_df.prediction1_percentile.values)
score2 = roc_auc_score(
test_df.hit.values, -test_df.prediction2_percentile.values)
print("AUC (using percentiles)", score1, score2)

assert_greater(score1, 0.8)
assert_greater(score2, 0.8)
assert score1 > 0.8
assert score2 > 0.8

# Test saving, loading, pickling
models_dir = tempfile.mkdtemp("_models")
Expand Down Expand Up @@ -161,7 +160,7 @@ def test_downloaded_predictor_small(predictors):
peptide_lengths=[9],
result="best")
print(scan_results)
assert_equal(len(scan_results), 6)
assert len(scan_results) == 6

scan_results = presentation_predictor.predict_sequences(
sequences=[
Expand All @@ -176,7 +175,7 @@ def test_downloaded_predictor_small(predictors):
peptide_lengths=[8, 9],
result="best")
print(scan_results)
assert_equal(len(scan_results), 6)
assert len(scan_results) == 6

scan_results = presentation_predictor.predict_sequences(
sequences=[
Expand All @@ -191,7 +190,7 @@ def test_downloaded_predictor_small(predictors):
peptide_lengths=[9],
result="all")
print(scan_results)
assert_equal(len(scan_results), 6)
assert len(scan_results) == 6

scan_results = presentation_predictor.predict_sequences(
sequences=[
Expand All @@ -206,7 +205,7 @@ def test_downloaded_predictor_small(predictors):
peptide_lengths=[8, 9],
result="all")
print(scan_results)
assert_equal(len(scan_results), 18)
assert len(scan_results) == 18

scan_results = presentation_predictor.predict_sequences(
sequences=[
Expand All @@ -221,7 +220,7 @@ def test_downloaded_predictor_small(predictors):
peptide_lengths=[10],
result="all")
print(scan_results)
assert_equal(len(scan_results), 0)
assert len(scan_results) == 0


def test_downloaded_predictor(predictors):
Expand All @@ -244,7 +243,7 @@ def test_downloaded_predictor(predictors):
])
print(scan_results1)

assert_equal(len(scan_results1), 3), str(scan_results1)
assert len(scan_results1) == 3, str(scan_results1)
assert (scan_results1.affinity < 200).all(), str(scan_results1)
assert (scan_results1.presentation_score > 0.7).all(), str(scan_results1)

Expand Down Expand Up @@ -311,7 +310,7 @@ def test_downloaded_predictor(predictors):
print(scan_results4)

assert len(scan_results4) > 200, len(scan_results4)
assert_less(scan_results4.iloc[0].affinity, 100)
assert scan_results4.iloc[0].affinity < 100

sequences = {
"seq1":
Expand Down Expand Up @@ -345,7 +344,7 @@ def test_downloaded_predictor(predictors):
],
})
print(scan_results5)
assert_equal(len(scan_results5), len(scan_results4) * 2)
assert len(scan_results5) == len(scan_results4) * 2

# Test case-insensitive.
scan_results6 = presentation_predictor.predict_sequences(
Expand Down
6 changes: 2 additions & 4 deletions test/test_class1_processing_neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from sklearn.metrics import roc_auc_score

from nose.tools import eq_, assert_less, assert_greater, assert_almost_equal

import pandas
import pytest

Expand Down Expand Up @@ -233,8 +231,8 @@ def is_hit(n_flank, c_flank, peptide):
print("Test auc", test_auc)

if do_assertions:
assert_greater(train_auc, 0.9)
assert_greater(test_auc, 0.85)
assert train_auc > 0.9
assert test_auc > 0.85

return network

28 changes: 13 additions & 15 deletions test/test_custom_loss.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from . import initialize
initialize()

from nose.tools import eq_, assert_less, assert_greater, assert_almost_equal

import numpy
import tensorflow as tf
import pytest
Expand Down Expand Up @@ -40,39 +38,39 @@ def test_mse_with_inequalities(loss_obj=CUSTOM_LOSSES['mse_with_inequalities']):
print(adjusted_y)
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, y_values)
print(loss0)
eq_(loss0, 0.0)
assert loss0 == 0.0

adjusted_y = loss_obj.encode_y(y_values, [">", ">", ">", ">"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, y_values)
eq_(loss0, 0.0)
assert loss0 == 0.0

adjusted_y = loss_obj.encode_y(y_values, ["<", "<", "<", "<"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, y_values)
eq_(loss0, 0.0)
assert loss0 == 0.0

adjusted_y = loss_obj.encode_y(y_values, ["=", "<", "=", ">"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, y_values)
eq_(loss0, 0.0)
assert loss0 == 0.0

adjusted_y = loss_obj.encode_y(y_values, ["=", "<", "=", ">"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, [0.0, 0.4, 0.8, 1.0])
eq_(loss0, 0.0)
assert loss0 == 0.0

adjusted_y = loss_obj.encode_y(y_values, [">", "<", ">", ">"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, [0.1, 0.4, 0.9, 1.0])
eq_(loss0, 0.0)
assert loss0 == 0.0

adjusted_y = loss_obj.encode_y(y_values, [">", "<", ">", ">"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, [0.1, 0.6, 0.9, 1.0])
assert_greater(loss0, 0.0)
assert loss0 > 0.0

adjusted_y = loss_obj.encode_y(y_values, ["=", "<", ">", ">"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, [0.1, 0.6, 0.9, 1.0])
assert_almost_equal(loss0, 0.02 / 4)
assert loss0 == pytest.approx(0.02 / 4, abs=1e-7)

adjusted_y = loss_obj.encode_y(y_values, ["=", "<", "=", ">"])
loss0 = evaluate_loss(loss_obj.loss, adjusted_y, [0.1, 0.6, 0.9, 1.0])
assert_almost_equal(loss0, 0.03 / 4)
assert loss0 == pytest.approx(0.03 / 4, abs=1e-7)


def test_mse_with_inequalities_and_multiple_outputs():
Expand All @@ -91,7 +89,7 @@ def test_mse_with_inequalities_and_multiple_outputs():
[3000, 0.8],
[4000, 1.0],
])
assert_almost_equal(loss0, 0.0)
assert loss0 == pytest.approx(0.0, abs=1e-7)

y_values = [0.0, 0.5, 0.8, 1.0]
adjusted_y = loss_obj.encode_y(
Expand All @@ -105,7 +103,7 @@ def test_mse_with_inequalities_and_multiple_outputs():
[3000, 0.8],
[1.0, 4000],
])
assert_almost_equal(loss0, 0.02 / 4)
assert loss0 == pytest.approx(0.02 / 4, abs=1e-7)

y_values = [0.0, 0.5, 0.8, 1.0]
adjusted_y = loss_obj.encode_y(
Expand All @@ -119,7 +117,7 @@ def test_mse_with_inequalities_and_multiple_outputs():
[3000, 0.8],
[1.0, 4000],
])
assert_almost_equal(loss0, 0.01 / 4)
assert loss0 == pytest.approx(0.01 / 4, abs=1e-7)

y_values = [0.0, 0.5, 0.8, 1.0]
adjusted_y = loss_obj.encode_y(
Expand All @@ -133,7 +131,7 @@ def test_mse_with_inequalities_and_multiple_outputs():
[3000, 0.8],
[1.0, 4000],
])
assert_almost_equal(loss0, 0.02 / 4)
assert loss0 == pytest.approx(0.02 / 4, abs=1e-7)


def test_multiallelic_mass_spec_loss():
Expand Down
2 changes: 1 addition & 1 deletion test/test_hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_all_combinations_of_hyperparameters():
Class1NeuralNetwork
.hyperparameter_defaults
.models_grid(**combinations_dict))
assert_equal(len(results), 4)
assert len(results) == 4

if __name__ == "__main__":
test_all_combinations_of_hyperparameters()
4 changes: 2 additions & 2 deletions test/test_predict_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_csv():
for delete in deletes:
os.unlink(delete)

assert_equal(result.shape, (3, 8))
assert result.shape == (3, 8)


def test_no_csv():
Expand All @@ -75,7 +75,7 @@ def test_no_csv():
os.unlink(delete)

print(result)
assert_equal(len(result), 6)
assert len(result) == 6
sub_result1 = result.loc[result.peptide == "SIINFEKL"].set_index("allele")
print(sub_result1)
assert (
Expand Down
Loading

0 comments on commit 8e9f353

Please sign in to comment.