Skip to content

Commit

Permalink
Add test for backward compatibility of v0.2.0 regression models
Browse files Browse the repository at this point in the history
  • Loading branch information
opcode81 committed Aug 6, 2023
1 parent f10b84b commit be2c860
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
46 changes: 14 additions & 32 deletions tests/backwardscompat/test_models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
from glob import glob

import pytest

import sensai
from sensai import VectorModel
from sensai.data_transformation import DFTNormalisation, SkLearnTransformerFactoryFactory, DFTOneHotEncoder
from sensai.featuregen import FeatureGeneratorTakeColumns, FeatureCollector
from sensai.sklearn.sklearn_regression import SkLearnLinearRegressionVectorRegressionModel, SkLearnRandomForestVectorRegressionModel, \
SkLearnMultiLayerPerceptronVectorRegressionModel
from tests.conftest import RegressionTestCase
from sensai.util.pickle import loadPickle
from tests.conftest import RESOURCE_DIR


def test_modelCanBeLoaded(testResources, irisClassificationTestCase):
def test_classification_model_backward_compatibility_v0_0_4(testResources, irisClassificationTestCase):
# The model file was generated with tests/frameworks/torch/test_torch.test_MLPClassifier at commit f93c6b11d
# NOTE: This test fails with scikit-learn 0.23 because of a change in StandardScaler
modelPath = os.path.join(testResources, "torch_mlp.pickle")
Expand All @@ -18,29 +18,11 @@ def test_modelCanBeLoaded(testResources, irisClassificationTestCase):
irisClassificationTestCase.testMinAccuracy(model, 0.8, fit=False)


def createRegressionModelsForBackwardsCompatibilityTest(testCase: RegressionTestCase):
fc = FeatureCollector(FeatureGeneratorTakeColumns(categoricalFeatureNames=["SEX"],
normalisationRuleTemplate=DFTNormalisation.RuleTemplate(independentColumns=False)))

modelLinear = SkLearnLinearRegressionVectorRegressionModel() \
.withFeatureCollector(fc) \
.withFeatureTransformers(
DFTOneHotEncoder(fc.getCategoricalFeatureNameRegex()))
#DFTNormalisation(fc.getNormalisationRules(), defaultTransformerFactory=SkLearnTransformerFactoryFactory.RobustScaler()))

modelRF = SkLearnRandomForestVectorRegressionModel() \
.withFeatureCollector(fc) \
.withFeatureTransformers(DFTOneHotEncoder(fc.getCategoricalFeatureNameRegex()))

modelMLP = SkLearnMultiLayerPerceptronVectorRegressionModel(hidden_layer_sizes=(10, 10), solver="lbfgs") \
.withFeatureCollector(fc) \
.withFeatureTransformers(
DFTOneHotEncoder(fc.getCategoricalFeatureNameRegex()),
DFTNormalisation(fc.getNormalisationRules(), defaultTransformerFactory=SkLearnTransformerFactoryFactory.RobustScaler()))

return modelMLP


def test_backward_compatibility_v020(diabetesRegressionTestCase):
model = createRegressionModelsForBackwardsCompatibility(diabetesRegressionTestCase)
diabetesRegressionTestCase.testMinR2(model, 0.5, fit=True)
@pytest.mark.parametrize("pickle_file", glob(f"{RESOURCE_DIR}/backward_compatibility/regression_model_*.v0.2.0.pickle"))
def test_regression_model_backward_compatibility_v0_2_0(pickle_file, diabetesRegressionTestCase):
"""
Tests for compatibility with models created with v0.2.0 using create_test_models.py
"""
d = loadPickle(pickle_file)
r2, model = d["R2"], d["model"]
diabetesRegressionTestCase.testMinR2(model, r2-0.02, fit=False)
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit be2c860

Please sign in to comment.