|
| 1 | +import pytest |
| 2 | +import inspect |
| 3 | +import torch |
| 4 | +import os |
| 5 | +import importlib |
| 6 | +from mambular.base_models.basemodel import BaseModel |
| 7 | + |
| 8 | +# Paths for models and configs |
| 9 | +MODEL_MODULE_PATH = "mambular.base_models" |
| 10 | +CONFIG_MODULE_PATH = "mambular.configs" |
| 11 | + |
| 12 | +# Discover all models |
| 13 | +model_classes = [] |
| 14 | +for filename in os.listdir(os.path.dirname(__file__) + "/../mambular/base_models"): |
| 15 | + if filename.endswith(".py") and filename not in [ |
| 16 | + "__init__.py", |
| 17 | + "basemodel.py", |
| 18 | + "lightning_wrapper.py", |
| 19 | + "bayesian_tabm.py", |
| 20 | + ]: |
| 21 | + module_name = f"{MODEL_MODULE_PATH}.{filename[:-3]}" |
| 22 | + module = importlib.import_module(module_name) |
| 23 | + |
| 24 | + for name, obj in inspect.getmembers(module, inspect.isclass): |
| 25 | + if issubclass(obj, BaseModel) and obj is not BaseModel: |
| 26 | + model_classes.append(obj) |
| 27 | + |
| 28 | + |
| 29 | +def get_model_config(model_class): |
| 30 | + """Dynamically load the correct config class for each model.""" |
| 31 | + model_name = model_class.__name__ # e.g., "Mambular" |
| 32 | + config_class_name = f"Default{model_name}Config" # e.g., "DefaultMambularConfig" |
| 33 | + |
| 34 | + try: |
| 35 | + config_module = importlib.import_module( |
| 36 | + f"{CONFIG_MODULE_PATH}.{model_name.lower()}_config" |
| 37 | + ) |
| 38 | + config_class = getattr(config_module, config_class_name) |
| 39 | + return config_class() # Instantiate config |
| 40 | + except (ModuleNotFoundError, AttributeError) as e: |
| 41 | + pytest.fail( |
| 42 | + f"Could not find or instantiate config {config_class_name} for {model_name}: {e}" |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("model_class", model_classes) |
| 47 | +def test_model_inherits_base_model(model_class): |
| 48 | + """Test that each model correctly inherits from BaseModel.""" |
| 49 | + assert issubclass( |
| 50 | + model_class, BaseModel |
| 51 | + ), f"{model_class.__name__} should inherit from BaseModel." |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("model_class", model_classes) |
| 55 | +def test_model_has_forward_method(model_class): |
| 56 | + """Test that each model has a forward method with *data.""" |
| 57 | + assert hasattr( |
| 58 | + model_class, "forward" |
| 59 | + ), f"{model_class.__name__} is missing a forward method." |
| 60 | + |
| 61 | + sig = inspect.signature(model_class.forward) |
| 62 | + assert any( |
| 63 | + p.kind == inspect.Parameter.VAR_POSITIONAL for p in sig.parameters.values() |
| 64 | + ), f"{model_class.__name__}.forward should have *data argument." |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize("model_class", model_classes) |
| 68 | +def test_model_takes_config(model_class): |
| 69 | + """Test that each model accepts a config argument.""" |
| 70 | + sig = inspect.signature(model_class.__init__) |
| 71 | + assert ( |
| 72 | + "config" in sig.parameters |
| 73 | + ), f"{model_class.__name__} should accept a 'config' parameter." |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize("model_class", model_classes) |
| 77 | +def test_model_has_num_classes(model_class): |
| 78 | + """Test that each model accepts a num_classes argument.""" |
| 79 | + sig = inspect.signature(model_class.__init__) |
| 80 | + assert ( |
| 81 | + "num_classes" in sig.parameters |
| 82 | + ), f"{model_class.__name__} should accept a 'num_classes' parameter." |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("model_class", model_classes) |
| 86 | +def test_model_calls_super_init(model_class): |
| 87 | + """Test that each model calls super().__init__(config=config, **kwargs).""" |
| 88 | + source = inspect.getsource(model_class.__init__) |
| 89 | + assert ( |
| 90 | + "super().__init__(config=config" in source |
| 91 | + ), f"{model_class.__name__} should call super().__init__(config=config, **kwargs)." |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize("model_class", model_classes) |
| 95 | +def test_model_initialization(model_class): |
| 96 | + """Test that each model can be initialized with its correct config.""" |
| 97 | + config = get_model_config(model_class) |
| 98 | + feature_info = ( |
| 99 | + { |
| 100 | + "A": { |
| 101 | + "preprocessing": "imputer -> check_positive -> box-cox", |
| 102 | + "dimension": 1, |
| 103 | + "categories": None, |
| 104 | + } |
| 105 | + }, |
| 106 | + { |
| 107 | + "sibsp": { |
| 108 | + "preprocessing": "imputer -> continuous_ordinal", |
| 109 | + "dimension": 1, |
| 110 | + "categories": 8, |
| 111 | + } |
| 112 | + }, |
| 113 | + {}, |
| 114 | + ) # Mock feature info |
| 115 | + |
| 116 | + try: |
| 117 | + model = model_class( |
| 118 | + feature_information=feature_info, num_classes=3, config=config |
| 119 | + ) |
| 120 | + except Exception as e: |
| 121 | + pytest.fail(f"Failed to initialize {model_class.__name__}: {e}") |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.parametrize("model_class", model_classes) |
| 125 | +def test_model_defines_key_attributes(model_class): |
| 126 | + """Test that each model defines expected attributes like returns_ensemble""" |
| 127 | + config = get_model_config(model_class) |
| 128 | + feature_info = ( |
| 129 | + { |
| 130 | + "A": { |
| 131 | + "preprocessing": "imputer -> check_positive -> box-cox", |
| 132 | + "dimension": 1, |
| 133 | + "categories": None, |
| 134 | + } |
| 135 | + }, |
| 136 | + { |
| 137 | + "sibsp": { |
| 138 | + "preprocessing": "imputer -> continuous_ordinal", |
| 139 | + "dimension": 1, |
| 140 | + "categories": 8, |
| 141 | + } |
| 142 | + }, |
| 143 | + {}, |
| 144 | + ) # Mock feature info |
| 145 | + |
| 146 | + try: |
| 147 | + model = model_class( |
| 148 | + feature_information=feature_info, num_classes=3, config=config |
| 149 | + ) |
| 150 | + except TypeError as e: |
| 151 | + pytest.fail(f"Failed to initialize {model_class.__name__}: {e}") |
| 152 | + |
| 153 | + expected_attrs = ["returns_ensemble"] |
| 154 | + for attr in expected_attrs: |
| 155 | + assert hasattr(model, attr), f"{model_class.__name__} should define '{attr}'." |
0 commit comments