|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import random |
| 5 | +import sys |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +import pytest |
| 11 | +import pytorch_lightning as ptl |
| 12 | +import torch |
| 13 | +from hydra import compose, initialize |
| 14 | +from omegaconf import DictConfig, open_dict |
| 15 | + |
| 16 | +from instanovo.inference.knapsack_beam_search import KnapsackBeamSearchDecoder |
| 17 | +from instanovo.transformer.model import InstaNovo |
| 18 | +from instanovo.transformer.predict import _setup_knapsack |
| 19 | + |
| 20 | + |
| 21 | +# Add the root directory to the PYTHONPATH |
| 22 | +# This allows pytest to find the modules for testing |
| 23 | + |
| 24 | +root_dir = os.path.dirname(os.path.dirname(__file__)) |
| 25 | +sys.path.append(root_dir) |
| 26 | + |
| 27 | + |
| 28 | +def reset_seed(seed: int = 42) -> None: |
| 29 | + """Function to reset seeds.""" |
| 30 | + torch.manual_seed(seed) |
| 31 | + if torch.cuda.is_available(): |
| 32 | + torch.cuda.manual_seed_all(seed) |
| 33 | + np.random.seed(seed) |
| 34 | + random.seed(seed) |
| 35 | + ptl.seed_everything(seed) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture() |
| 39 | +def _reset_seed() -> None: |
| 40 | + """A pytest fixture to reset the seeds at the start of relevant tests.""" |
| 41 | + reset_seed() |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(scope="session") |
| 45 | +def checkpoints_dir() -> str: |
| 46 | + """A pytest fixture to create and provide the absolute path of a 'checkpoints' directory. |
| 47 | +
|
| 48 | + Ensures the directory exists for storing checkpoint files during the test session. |
| 49 | + """ |
| 50 | + checkpoints_dir = "checkpoints" |
| 51 | + os.makedirs(checkpoints_dir, exist_ok=True) |
| 52 | + return os.path.abspath(checkpoints_dir) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(scope="session") |
| 56 | +def instanovo_config() -> DictConfig: |
| 57 | + """A pytest fixture to read in a Hydra config for the Instanovo model unit and integration tests.""" |
| 58 | + with initialize(version_base=None, config_path="../instanovo/configs"): |
| 59 | + cfg = compose(config_name="instanovo_unit_test") |
| 60 | + |
| 61 | + sub_configs_list = ["model", "dataset", "residues"] |
| 62 | + for sub_name in sub_configs_list: |
| 63 | + if sub_name in cfg: |
| 64 | + with open_dict(cfg): |
| 65 | + temp = cfg[sub_name] |
| 66 | + del cfg[sub_name] |
| 67 | + cfg.update(temp) |
| 68 | + |
| 69 | + return cfg |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture(scope="session") |
| 73 | +def instanovo_inference_config() -> DictConfig: |
| 74 | + """A pytest fixture to read in a Hydra config for inference of the Instanovo model unit and integration tests.""" |
| 75 | + with initialize(version_base=None, config_path="../instanovo/configs/inference"): |
| 76 | + cfg = compose(config_name="unit_test") |
| 77 | + |
| 78 | + return cfg |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture(scope="session") |
| 82 | +def dir_paths() -> tuple[str, str]: |
| 83 | + """A pytest fixture that returns the root and data directories for the unit and integration tests.""" |
| 84 | + root_dir = "./tests/instanovo_test_resources" |
| 85 | + data_dir = os.path.join(root_dir, "example_data") |
| 86 | + return root_dir, data_dir |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture(scope="session") |
| 90 | +def instanovo_checkpoint(dir_paths: tuple[str, str]) -> str: |
| 91 | + """A pytest fixture that returns the InstaNovo model checkpoint used for unit and integration tests.""" |
| 92 | + root_dir, _ = dir_paths |
| 93 | + return os.path.join(root_dir, "model.ckpt") |
| 94 | + |
| 95 | + |
| 96 | +@pytest.fixture(scope="session") |
| 97 | +def instanovo_model( |
| 98 | + instanovo_checkpoint: str, |
| 99 | +) -> tuple[Any, Any]: |
| 100 | + """A pytest fixture that returns the InstaNovo model and config used for unit and integration tests.""" |
| 101 | + model, config = InstaNovo.load(path=instanovo_checkpoint) |
| 102 | + return model, config |
| 103 | + |
| 104 | + |
| 105 | +@pytest.fixture(scope="session") |
| 106 | +def residue_set(instanovo_model: tuple[Any, Any]) -> Any: |
| 107 | + """A pytest fixture to return the model's residue set used for unit and integration tests.""" |
| 108 | + model, _ = instanovo_model |
| 109 | + return model.residue_set |
| 110 | + |
| 111 | + |
| 112 | +@pytest.fixture(scope="session") |
| 113 | +def instanovo_output(dir_paths: tuple[str, str]) -> pd.DataFrame: |
| 114 | + """A pytest fixture to load the pre-computed InstaNovo model predictions for unit and integration tests.""" |
| 115 | + root_dir, _ = dir_paths |
| 116 | + return pd.read_csv(os.path.join(root_dir, "predictions.csv")) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.fixture(scope="session") |
| 120 | +def knapsack_dir(dir_paths: tuple[str, str]) -> str: |
| 121 | + """A pytest fixture to create and provide the absolute path of a 'knapsack' directory within the checkpoints directory for storing test artifacts.""" |
| 122 | + root_dir, _ = dir_paths |
| 123 | + knapsack_dir = os.path.join(root_dir, "example_knapsack") |
| 124 | + return os.path.abspath(knapsack_dir) |
| 125 | + |
| 126 | + |
| 127 | +@pytest.fixture(scope="session") |
| 128 | +def setup_knapsack_decoder( |
| 129 | + instanovo_model: tuple[Any, Any], knapsack_dir: str |
| 130 | +) -> KnapsackBeamSearchDecoder: |
| 131 | + """A pytest fixture to create a Knapsack object.""" |
| 132 | + model, _ = instanovo_model |
| 133 | + |
| 134 | + if os.path.exists(knapsack_dir): |
| 135 | + decoder = KnapsackBeamSearchDecoder.from_file(model=model, path=knapsack_dir) |
| 136 | + print("Loaded knapsack decoder.") |
| 137 | + |
| 138 | + else: |
| 139 | + knapsack = _setup_knapsack(model) |
| 140 | + |
| 141 | + knapsack.save(path=knapsack_dir) |
| 142 | + print("Created and saved knapsack.") |
| 143 | + |
| 144 | + decoder = KnapsackBeamSearchDecoder(model, knapsack) |
| 145 | + print("Loaded knapsack decoder.") |
| 146 | + |
| 147 | + return decoder |
0 commit comments