Wrappers¶
Model adapters normalizing predict / predict_proba across frameworks.
modeltest.wrappers
¶
Model adapters.
TestContext talks to models through a small common interface:
predict(X) -> class labels
predict_proba(X) -> probability estimates (optional)
wrap(model) returns a normalized object implementing this interface for the
given model, dispatching on the framework:
- scikit-learn / numpy (BaseEstimator)
- PyTorch (torch.nn.Module) — lazily imported
- Keras / TensorFlow — lazily imported
If the model can't be recognised, we assume it already follows the common
interface (e.g. a user-provided wrapper) and let predict/predict_proba
resolve dynamically.
ModelWrapper(model)
¶
Normalized interface all scenarios use to query a model.
Wrap model and expose the normalized interface.
Source code in modeltest/wrappers.py
32 33 34 35 36 | |
feature_names_in_
property
¶
Expose the model's feature names when available (for filtering).
predict(X)
¶
Return class labels for X. Concrete adapters implement this.
Source code in modeltest/wrappers.py
38 39 40 | |
predict_proba(X)
¶
Return probability estimates for X, or None when the
underlying model does not support them.
Source code in modeltest/wrappers.py
42 43 44 45 | |
SklearnModel(model)
¶
Bases: ModelWrapper
Adapter for scikit-learn style estimators exposing predict.
Source code in modeltest/wrappers.py
32 33 34 35 36 | |
predict(X)
¶
Delegate the prediction to the wrapped estimator.
Source code in modeltest/wrappers.py
56 57 58 | |
predict_proba(X)
¶
Return the estimator's probabilities when available, else
None.
Source code in modeltest/wrappers.py
60 61 62 63 64 65 | |
SklearnClassifier(model)
¶
Bases: SklearnModel
Adapter for classifiers: labels + probabilities.
Source code in modeltest/wrappers.py
32 33 34 35 36 | |
predict_proba(X)
¶
Return classifier probabilities (assumed available).
Source code in modeltest/wrappers.py
71 72 73 | |
TorchModel(model, *, input_key='images', device=None)
¶
Bases: ModelWrapper
Adapter for a PyTorch nn.Module trained for classification.
predict returns argmax class indices; predict_proba returns
softmax probabilities. Expects X as a numpy array or torch tensor.
Configure the torch adapter.
Args:
model: A torch.nn.Module classifier.
input_key: Reserved for models expecting dict inputs.
device: Optional device to move inputs to before forward.
Source code in modeltest/wrappers.py
83 84 85 86 87 88 89 90 91 92 93 | |
predict(X)
¶
Forward X (in inference mode) and return argmax class
indices.
Source code in modeltest/wrappers.py
125 126 127 128 129 130 131 132 133 | |
predict_proba(X)
¶
Forward X and return softmax-normalized probabilities.
Source code in modeltest/wrappers.py
135 136 137 138 139 140 141 142 143 | |
KerasModel(model, *, multiclass=False)
¶
Bases: ModelWrapper
Adapter for a compiled Keras/TensorFlow model.
Configure the Keras adapter.
Args:
model: A compiled tf.keras.Model.
multiclass: Force argmax decoding even when the model has two
outputs (default: threshold at 0.5 for single-output).
Source code in modeltest/wrappers.py
149 150 151 152 153 154 155 156 157 158 | |
predict(X)
¶
Return class labels: argmax for multiclass outputs, 0.5 threshold for single-output models.
Source code in modeltest/wrappers.py
160 161 162 163 164 165 166 | |
predict_proba(X)
¶
Return the model's raw output as probabilities.
Source code in modeltest/wrappers.py
168 169 170 | |
wrap(model, **kwargs)
¶
Return a normalized :class:ModelWrapper for model.
kwargs are forwarded to the adapter (e.g. input_key for Torch,
multiclass for Keras). If the model is already a ModelWrapper it is
returned unchanged.
Source code in modeltest/wrappers.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |