From c4d191a63d32e0cc98ce4f5af7440677eda8e36d Mon Sep 17 00:00:00 2001 From: Isha Gokhale Date: Mon, 23 Oct 2023 23:12:57 -0700 Subject: [PATCH 1/7] added unit tests to raise exceptions when unrecognized/missing file entry --- casanovo/config.py | 16 ++++++++- tests/conftest.py | 60 ++++++++++++++++++++++++++++++++- tests/unit_tests/test_config.py | 26 ++++++++------ 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/casanovo/config.py b/casanovo/config.py index 0274a1b1..4992a882 100644 --- a/casanovo/config.py +++ b/casanovo/config.py @@ -83,7 +83,21 @@ def __init__(self, config_file: Optional[str] = None): else: with Path(config_file).open() as f_in: self._user_config = yaml.safe_load(f_in) - + # check for missing entries in config file + if len(self._user_config.keys()) < len(self._params.keys()): + keys_set = set(self._params.keys()) + users_set = set(self._user_config.keys()) + missing = list(keys_set - users_set) + raise KeyError( + f"Missing expected entry {missing}" + ) + # detect unrecognized config file entries + keys = list(self._params.keys()) + for key,val in self._user_config.items(): + if key not in keys: + raise KeyError( + f"Unrecognized config file entry {key}" + ) # Validate: for key, val in self._config_types.items(): self.validate_param(key, val) diff --git a/tests/conftest.py b/tests/conftest.py index 6dcda9c9..bbf51838 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -188,7 +188,7 @@ def tiny_config(tmp_path): """A config file for a tiny model.""" cfg = { "n_head": 2, - "dim_feedfoward": 10, + "dim_feedforward": 10, "n_layers": 1, "warmup_iters": 1, "max_iters": 1, @@ -196,6 +196,64 @@ def tiny_config(tmp_path): "val_check_interval": 1, "model_save_folder_path": str(tmp_path), "accelerator": "cpu", + "precursor_mass_tol": 5, + "isotope_error_range": [0,1], + "min_peptide_len": 6, + "predict_batch_size": 1024, + "n_beams": 1, + "top_match": 1, + "devices": None, + "random_seed": 454, + "n_log": 1, + "tb_summarywriter": None, + "save_top_k": 5, + "n_peaks": 150, + "min_mz": 50.0, + "max_mz": 2500.0, + "min_intensity": 0.01, + "remove_precursor_tol": 2.0, + "max_charge": 10, + "dim_model": 512, + "n_head": 8, + "dropout": 0.0, + "dim_intensity": None, + "max_length": 100, + "learning_rate": 5e-4, + "weight_decay": 1e-5, + "train_batch_size": 32, + "max_epochs": 30, + "num_sanity_val_steps": 0, + "train_from_scratch": True, + "calculate_precision": False, + "residues": { + "G": 57.021464, + "A": 71.037114, + "S": 87.032028, + "P": 97.052764, + "V": 99.068414, + "T": 101.047670, + "C+57.021": 160.030649, + "L": 113.084064, + "I": 113.084064, + "N": 114.042927, + "D": 115.026943, + "Q": 128.058578, + "K": 128.094963, + "E": 129.042593, + "M": 131.040485, + "H": 137.058912, + "F": 147.068414, + "R": 156.101111, + "Y": 163.063329, + "W": 186.079313, + "M+15.995": 147.035400, + "N+0.984": 115.026943, + "Q+0.984": 129.042594, + "+42.011": 42.010565, + "+43.006": 43.005814, + "-17.027": -17.026549, + "+43.006-17.027": 25.980265 + } } cfg_file = tmp_path / "config.yml" diff --git a/tests/unit_tests/test_config.py b/tests/unit_tests/test_config.py index 1e2ef338..ce695f93 100644 --- a/tests/unit_tests/test_config.py +++ b/tests/unit_tests/test_config.py @@ -1,5 +1,7 @@ """Test configuration loading""" from casanovo.config import Config +import pytest +import yaml def test_default(): @@ -11,7 +13,7 @@ def test_default(): assert config.file == "default" -def test_override(tmp_path): +def test_override(tmp_path, tiny_config): """Test overriding the default""" yml = tmp_path / "test.yml" with yml.open("w+") as f_out: @@ -26,12 +28,16 @@ def test_override(tmp_path): """ ) - config = Config(yml) - assert config.random_seed == 42 - assert config["random_seed"] == 42 - assert config.accelerator == "auto" - assert config.top_match == 3 - assert len(config.residues) == 4 - for i, residue in enumerate("WOUT", 1): - assert config["residues"][residue] == i - assert config.file == str(yml) + with open (tiny_config, 'r') as read_file: + contents = yaml.safe_load(read_file) + contents['random_seed_'] = 354 + print(contents) + + with open('output.yml', 'w') as write_file: + yaml.safe_dump(contents, write_file) + with pytest.raises(KeyError): + config = Config('output.yml') + + with pytest.raises(KeyError): + config = Config(yml) + \ No newline at end of file From 92b5fae73fe5333d09f5d9c7625987efe62536c1 Mon Sep 17 00:00:00 2001 From: Isha Gokhale Date: Tue, 24 Oct 2023 00:18:12 -0700 Subject: [PATCH 2/7] fixed lint issue --- casanovo/config.py | 10 +++------- tests/conftest.py | 6 +++--- tests/unit_tests/test_config.py | 12 +++++------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/casanovo/config.py b/casanovo/config.py index 4992a882..c07073d6 100644 --- a/casanovo/config.py +++ b/casanovo/config.py @@ -88,16 +88,12 @@ def __init__(self, config_file: Optional[str] = None): keys_set = set(self._params.keys()) users_set = set(self._user_config.keys()) missing = list(keys_set - users_set) - raise KeyError( - f"Missing expected entry {missing}" - ) + raise KeyError(f"Missing expected entry {missing}") # detect unrecognized config file entries keys = list(self._params.keys()) - for key,val in self._user_config.items(): + for key, val in self._user_config.items(): if key not in keys: - raise KeyError( - f"Unrecognized config file entry {key}" - ) + raise KeyError(f"Unrecognized config file entry {key}") # Validate: for key, val in self._config_types.items(): self.validate_param(key, val) diff --git a/tests/conftest.py b/tests/conftest.py index bbf51838..02d5015d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -197,7 +197,7 @@ def tiny_config(tmp_path): "model_save_folder_path": str(tmp_path), "accelerator": "cpu", "precursor_mass_tol": 5, - "isotope_error_range": [0,1], + "isotope_error_range": [0, 1], "min_peptide_len": 6, "predict_batch_size": 1024, "n_beams": 1, @@ -252,8 +252,8 @@ def tiny_config(tmp_path): "+42.011": 42.010565, "+43.006": 43.005814, "-17.027": -17.026549, - "+43.006-17.027": 25.980265 - } + "+43.006-17.027": 25.980265, + }, } cfg_file = tmp_path / "config.yml" diff --git a/tests/unit_tests/test_config.py b/tests/unit_tests/test_config.py index ce695f93..4d1bb17f 100644 --- a/tests/unit_tests/test_config.py +++ b/tests/unit_tests/test_config.py @@ -28,16 +28,14 @@ def test_override(tmp_path, tiny_config): """ ) - with open (tiny_config, 'r') as read_file: + with open(tiny_config, "r") as read_file: contents = yaml.safe_load(read_file) - contents['random_seed_'] = 354 - print(contents) + contents["random_seed_"] = 354 - with open('output.yml', 'w') as write_file: + with open("output.yml", "w") as write_file: yaml.safe_dump(contents, write_file) with pytest.raises(KeyError): - config = Config('output.yml') + config = Config("output.yml") with pytest.raises(KeyError): - config = Config(yml) - \ No newline at end of file + config = Config(yml) \ No newline at end of file From 1cdd29e19ab04403676137cf41895b538fdeca4c Mon Sep 17 00:00:00 2001 From: Isha Gokhale Date: Mon, 6 Nov 2023 18:48:38 -0800 Subject: [PATCH 3/7] fix lint issue --- casanovo/config.py | 2 ++ tests/unit_tests/test_config.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/casanovo/config.py b/casanovo/config.py index c07073d6..16d9ea1b 100644 --- a/casanovo/config.py +++ b/casanovo/config.py @@ -83,6 +83,7 @@ def __init__(self, config_file: Optional[str] = None): else: with Path(config_file).open() as f_in: self._user_config = yaml.safe_load(f_in) + """ # check for missing entries in config file if len(self._user_config.keys()) < len(self._params.keys()): keys_set = set(self._params.keys()) @@ -94,6 +95,7 @@ def __init__(self, config_file: Optional[str] = None): for key, val in self._user_config.items(): if key not in keys: raise KeyError(f"Unrecognized config file entry {key}") + """ # Validate: for key, val in self._config_types.items(): self.validate_param(key, val) diff --git a/tests/unit_tests/test_config.py b/tests/unit_tests/test_config.py index 4d1bb17f..7d374048 100644 --- a/tests/unit_tests/test_config.py +++ b/tests/unit_tests/test_config.py @@ -27,7 +27,7 @@ def test_override(tmp_path, tiny_config): T: 4 """ ) - + """ with open(tiny_config, "r") as read_file: contents = yaml.safe_load(read_file) contents["random_seed_"] = 354 @@ -38,4 +38,14 @@ def test_override(tmp_path, tiny_config): config = Config("output.yml") with pytest.raises(KeyError): - config = Config(yml) \ No newline at end of file + config = Config(yml) + """ + config = Config(yml) + assert config.random_seed == 42 + assert config["random_seed"] == 42 + assert config.accelerator == "auto" + assert config.top_match == 3 + assert len(config.residues) == 4 + for i, residue in enumerate("WOUT", 1): + assert config["residues"][residue] == i + assert config.file == str(yml) \ No newline at end of file From a12f5fc149bf878628e4c0f41d1ba89a9af699cd Mon Sep 17 00:00:00 2001 From: Isha Gokhale Date: Mon, 6 Nov 2023 20:41:48 -0800 Subject: [PATCH 4/7] fixed failing unit test --- casanovo/config.py | 4 ++-- tests/conftest.py | 2 -- tests/unit_tests/test_config.py | 14 ++------------ 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/casanovo/config.py b/casanovo/config.py index 16d9ea1b..9514cacd 100644 --- a/casanovo/config.py +++ b/casanovo/config.py @@ -83,7 +83,7 @@ def __init__(self, config_file: Optional[str] = None): else: with Path(config_file).open() as f_in: self._user_config = yaml.safe_load(f_in) - """ + # check for missing entries in config file if len(self._user_config.keys()) < len(self._params.keys()): keys_set = set(self._params.keys()) @@ -95,7 +95,7 @@ def __init__(self, config_file: Optional[str] = None): for key, val in self._user_config.items(): if key not in keys: raise KeyError(f"Unrecognized config file entry {key}") - """ + # Validate: for key, val in self._config_types.items(): self.validate_param(key, val) diff --git a/tests/conftest.py b/tests/conftest.py index 02d5015d..70ac2cbb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -214,14 +214,12 @@ def tiny_config(tmp_path): "remove_precursor_tol": 2.0, "max_charge": 10, "dim_model": 512, - "n_head": 8, "dropout": 0.0, "dim_intensity": None, "max_length": 100, "learning_rate": 5e-4, "weight_decay": 1e-5, "train_batch_size": 32, - "max_epochs": 30, "num_sanity_val_steps": 0, "train_from_scratch": True, "calculate_precision": False, diff --git a/tests/unit_tests/test_config.py b/tests/unit_tests/test_config.py index 7d374048..4d1bb17f 100644 --- a/tests/unit_tests/test_config.py +++ b/tests/unit_tests/test_config.py @@ -27,7 +27,7 @@ def test_override(tmp_path, tiny_config): T: 4 """ ) - """ + with open(tiny_config, "r") as read_file: contents = yaml.safe_load(read_file) contents["random_seed_"] = 354 @@ -38,14 +38,4 @@ def test_override(tmp_path, tiny_config): config = Config("output.yml") with pytest.raises(KeyError): - config = Config(yml) - """ - config = Config(yml) - assert config.random_seed == 42 - assert config["random_seed"] == 42 - assert config.accelerator == "auto" - assert config.top_match == 3 - assert len(config.residues) == 4 - for i, residue in enumerate("WOUT", 1): - assert config["residues"][residue] == i - assert config.file == str(yml) \ No newline at end of file + config = Config(yml) \ No newline at end of file From f6dc6be0947a65706bf8fc2519a373cf1bbe9af9 Mon Sep 17 00:00:00 2001 From: Isha Gokhale Date: Mon, 6 Nov 2023 20:56:01 -0800 Subject: [PATCH 5/7] lint issue --- casanovo/config.py | 2 -- tests/conftest.py | 1 + tests/unit_tests/test_config.py | 3 +-- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/casanovo/config.py b/casanovo/config.py index 9514cacd..c07073d6 100644 --- a/casanovo/config.py +++ b/casanovo/config.py @@ -83,7 +83,6 @@ def __init__(self, config_file: Optional[str] = None): else: with Path(config_file).open() as f_in: self._user_config = yaml.safe_load(f_in) - # check for missing entries in config file if len(self._user_config.keys()) < len(self._params.keys()): keys_set = set(self._params.keys()) @@ -95,7 +94,6 @@ def __init__(self, config_file: Optional[str] = None): for key, val in self._user_config.items(): if key not in keys: raise KeyError(f"Unrecognized config file entry {key}") - # Validate: for key, val in self._config_types.items(): self.validate_param(key, val) diff --git a/tests/conftest.py b/tests/conftest.py index 70ac2cbb..2855052e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -251,6 +251,7 @@ def tiny_config(tmp_path): "+43.006": 43.005814, "-17.027": -17.026549, "+43.006-17.027": 25.980265, + }, } diff --git a/tests/unit_tests/test_config.py b/tests/unit_tests/test_config.py index 4d1bb17f..fd8ed22e 100644 --- a/tests/unit_tests/test_config.py +++ b/tests/unit_tests/test_config.py @@ -36,6 +36,5 @@ def test_override(tmp_path, tiny_config): yaml.safe_dump(contents, write_file) with pytest.raises(KeyError): config = Config("output.yml") - with pytest.raises(KeyError): - config = Config(yml) \ No newline at end of file + config = Config(yml) From 533352e2f5d5d50a7918e0e8ef1869dc44785c48 Mon Sep 17 00:00:00 2001 From: Isha Gokhale Date: Mon, 6 Nov 2023 21:12:40 -0800 Subject: [PATCH 6/7] lint --- tests/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2855052e..70ac2cbb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -251,7 +251,6 @@ def tiny_config(tmp_path): "+43.006": 43.005814, "-17.027": -17.026549, "+43.006-17.027": 25.980265, - }, } From 374fb49db685c2aa1dbfd409bc921a430870c9a4 Mon Sep 17 00:00:00 2001 From: Isha Gokhale Date: Mon, 6 Nov 2023 23:26:49 -0800 Subject: [PATCH 7/7] lint issue --- tests/conftest.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 70ac2cbb..267dfa0f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -244,14 +244,14 @@ def tiny_config(tmp_path): "R": 156.101111, "Y": 163.063329, "W": 186.079313, - "M+15.995": 147.035400, - "N+0.984": 115.026943, - "Q+0.984": 129.042594, - "+42.011": 42.010565, - "+43.006": 43.005814, - "-17.027": -17.026549, + "M+15.995": 147.035400, + "N+0.984": 115.026943, + "Q+0.984": 129.042594, + "+42.011": 42.010565, + "+43.006": 43.005814, + "-17.027": -17.026549, "+43.006-17.027": 25.980265, - }, + }, } cfg_file = tmp_path / "config.yml"