Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TobyRoseman committed Aug 3, 2022
1 parent 85d9e7d commit bb8dd74
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 130 deletions.
12 changes: 5 additions & 7 deletions coremltools/test/sklearn_tests/test_NuSVC.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# Use of this source code is governed by a BSD-3-clause license that can be
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause

import unittest
import tempfile
import os
import pandas as pd
import tempfile
import random
import unittest

import pandas as pd
import pytest

from coremltools.models.utils import (
Expand Down Expand Up @@ -90,7 +91,6 @@ def _evaluation_test_helper(
cur_params.update(param2)
cur_params["probability"] = use_probability_estimates
cur_params["max_iter"] = 10 # Don't want test to take too long
# print("cur_params=" + str(cur_params))

cur_model = NuSVC(**cur_params)
cur_model.fit(x, y)
Expand All @@ -112,7 +112,7 @@ def _evaluation_test_helper(
metrics["max_probability_error"], allowed_prob_delta
)
else:
df["prediction"] = cur_model.predict(x)
df["target"] = cur_model.predict(x)
metrics = evaluate_classifier(spec, df, verbose=False)
self.assertEqual(metrics["num_errors"], 0)

Expand Down Expand Up @@ -159,8 +159,6 @@ def test_multi_class_string_label_with_probability(self):
self._evaluation_test_helper(["X", "Y", "z"], True, allow_slow=False)

def test_conversion_bad_inputs(self):
from sklearn.preprocessing import OneHotEncoder

# Error on converting an untrained model
with self.assertRaises(TypeError):
model = NuSVC()
Expand Down
19 changes: 11 additions & 8 deletions coremltools/test/sklearn_tests/test_NuSVR.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
# Use of this source code is governed by a BSD-3-clause license that can be
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause

import pandas as pd
import tempfile
import random
import unittest

import pandas as pd
import pytest

from coremltools._deps import (
Expand All @@ -18,8 +19,13 @@
from coremltools.models.utils import evaluate_regressor, _macos_version, _is_macos

if _HAS_LIBSVM:
from libsvm import svmutil
from libsvm import svm
from libsvm import (
svm,
svm_parameter,
svm_problem,
svmutil,
)
from svmutil import svm_train, svm_predict
from coremltools.converters import libsvm

if _HAS_SKLEARN:
Expand Down Expand Up @@ -109,7 +115,7 @@ def _test_evaluation(self, allow_slow):

cur_model = NuSVR(**cur_params)
cur_model.fit(x, y)
df["prediction"] = cur_model.predict(x)
df["target"] = cur_model.predict(x)

spec = scikit_converter.convert(cur_model, input_names, "target")

Expand Down Expand Up @@ -175,9 +181,6 @@ def _test_evaluation(self, allow_slow):
"""
Test that the same predictions are made
"""
from svm import svm_parameter, svm_problem
from svmutil import svm_train, svm_predict

# Generate some smallish (poly kernels take too long on anything else) random data
x, y = [], []
for _ in range(50):
Expand Down Expand Up @@ -212,7 +215,7 @@ def _test_evaluation(self, allow_slow):
param = svm_parameter(param_str)

model = svm_train(prob, param)
(df["prediction"], _, _) = svm_predict(y, x, model)
(df["target"], _, _) = svm_predict(y, x, model)

spec = libsvm.convert(model, input_names, "target")

Expand Down
28 changes: 12 additions & 16 deletions coremltools/test/sklearn_tests/test_SVC.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
# Use of this source code is governed by a BSD-3-clause license that can be
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause

import unittest
import tempfile
import copy
import os
import pandas as pd
import random
import pytest
import tempfile
import unittest

import numpy as np
import pandas as pd
import pytest

from coremltools._deps import _HAS_LIBSVM, _HAS_SKLEARN
from coremltools.models.utils import (
evaluate_classifier,
evaluate_classifier_with_probabilities,
_macos_version,
_is_macos,
)
from coremltools._deps import _HAS_LIBSVM, _HAS_SKLEARN

if _HAS_SKLEARN:
from sklearn.preprocessing import OneHotEncoder
from sklearn.svm import SVC
from coremltools.converters import sklearn as scikit_converter

Expand Down Expand Up @@ -85,7 +88,6 @@ def _evaluation_test_helper(
cur_params.update(param2)
cur_params["probability"] = use_probability_estimates
cur_params["max_iter"] = 10 # Don't want test to take too long
print("cur_params=" + str(cur_params))

cur_model = SVC(**cur_params)
cur_model.fit(x, y)
Expand All @@ -107,7 +109,7 @@ def _evaluation_test_helper(
metrics["max_probability_error"], allowed_prob_delta
)
else:
df["prediction"] = cur_model.predict(x)
df["target"] = cur_model.predict(x)
metrics = evaluate_classifier(spec, df, verbose=False)
self.assertEqual(metrics["num_errors"], 0)

Expand Down Expand Up @@ -154,8 +156,6 @@ def test_multi_class_int_label_with_probability(self):
self._evaluation_test_helper([1, 2, 3], True, allow_slow=False)

def test_conversion_bad_inputs(self):
from sklearn.preprocessing import OneHotEncoder

# Error on converting an untrained model
with self.assertRaises(TypeError):
model = SVC()
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_default_names(self):
self.assertEqual(len(spec.description.output), 1)
self.assertEqual(spec.description.output[0].name, u"target")
if _is_macos() and _macos_version() >= (10, 13):
(df["prediction"], _, _) = svm_predict(
(df["target"], _, _) = svm_predict(
self.y, self.x, no_probability_model, " -q"
)
metrics = evaluate_classifier(spec, df, verbose=False)
Expand Down Expand Up @@ -282,8 +282,6 @@ def test_multi_class_with_probability(self):
self._evaluation_test_helper_with_probability([1, 2, 3], allow_slow=False)

def _evaluation_test_helper_with_probability(self, labels, allow_slow):
import copy

df = pd.DataFrame(self.x, columns=self.column_names)
y = copy.copy(self.y)
for i, val in enumerate(labels):
Expand All @@ -295,13 +293,12 @@ def _evaluation_test_helper_with_probability(self, labels, allow_slow):
param_str = " ".join(
[self.base_param, param1, param2, probability_param]
)
# print("PARAMS: ", param_str)
param = svm_parameter(param_str)

model = svm_train(self.prob, param)

# Get predictions with probabilities as dictionaries
(df["prediction"], _, probability_lists) = svm_predict(
(df["target"], _, probability_lists) = svm_predict(
y, self.x, model, probability_param + " -q"
)
probability_dicts = [
Expand Down Expand Up @@ -347,13 +344,12 @@ def _evaluation_test_helper_no_probability(self, labels, allow_slow):
for param1 in self.non_kernel_parameters:
for param2 in self.kernel_parameters:
param_str = " ".join([self.base_param, param1, param2])
print("PARAMS: ", param_str)
param = svm_parameter(param_str)

model = svm_train(prob, param)

# Get predictions with probabilities as dictionaries
(df["prediction"], _, _) = svm_predict(y, x, model, " -q")
(df["target"], _, _) = svm_predict(y, x, model, " -q")

spec = libsvm.convert(model, column_names, "target")

Expand Down
11 changes: 6 additions & 5 deletions coremltools/test/sklearn_tests/test_SVR.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# Use of this source code is governed by a BSD-3-clause license that can be
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause

import pandas as pd
import numpy as np
import random
import tempfile
import unittest

import numpy as np
import pandas as pd
import pytest

from coremltools._deps import (
Expand Down Expand Up @@ -115,7 +116,7 @@ def _test_evaluation(self, allow_slow):

cur_model = SVR(**cur_params)
cur_model.fit(x, y)
df["prediction"] = cur_model.predict(x)
df["target"] = cur_model.predict(x)

spec = sklearn_converter.convert(cur_model, input_names, "target")

Expand Down Expand Up @@ -164,7 +165,7 @@ def test_input_names(self):
# Default values
spec = libsvm.convert(self.libsvm_model)
if _is_macos() and _macos_version() >= (10, 13):
(df["prediction"], _, _) = svmutil.svm_predict(
(df["target"], _, _) = svmutil.svm_predict(
data["target"], data["data"].tolist(), self.libsvm_model
)
metrics = evaluate_regressor(spec, df)
Expand Down Expand Up @@ -243,7 +244,7 @@ def _test_evaluation(self, allow_slow):
param = svm_parameter(param_str)

model = svm_train(prob, param)
(df["prediction"], _, _) = svm_predict(y, x, model)
(df["target"], _, _) = svm_predict(y, x, model)

spec = libsvm.convert(
model, input_names=input_names, target_name="target"
Expand Down
9 changes: 4 additions & 5 deletions coremltools/test/sklearn_tests/test_composite_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# Use of this source code is governed by a BSD-3-clause license that can be
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause

import unittest
import itertools
import os
import unittest

import pandas as pd
import itertools
import numpy as np
import pandas as pd

from coremltools._deps import _HAS_SKLEARN
from coremltools.models.utils import evaluate_transformer
Expand All @@ -27,7 +27,6 @@
@unittest.skipIf(not _HAS_SKLEARN, "Missing sklearn. Skipping tests.")
class GradientBoostingRegressorBostonHousingScikitNumericTest(unittest.TestCase):
def test_boston_OHE_plus_normalizer(self):

data = load_boston()

pl = Pipeline(
Expand Down Expand Up @@ -68,7 +67,7 @@ def _test_boston_OHE_plus_trees(self, loss='ls'):
if _is_macos() and _macos_version() >= (10, 13):
# Get predictions
df = pd.DataFrame(data.data, columns=data.feature_names)
df["prediction"] = pl.predict(data.data)
df["target"] = pl.predict(data.data)

# Evaluate it
result = evaluate_regressor(spec, df, "target", verbose=False)
Expand Down
33 changes: 16 additions & 17 deletions coremltools/test/sklearn_tests/test_dict_vectorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
# Use of this source code is governed by a BSD-3-clause license that can be
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause

import unittest
from coremltools._deps import _HAS_SKLEARN
from copy import copy
import unittest

import numpy as np
from coremltools.models.utils import evaluate_transformer
from coremltools.models.utils import evaluate_classifier
from coremltools.models.utils import _macos_version, _is_macos
import numpy.random as rn
import pandas as pd

import coremltools
from coremltools._deps import _HAS_SKLEARN
from coremltools.models.utils import (
_macos_version, _is_macos, evaluate_classifier, evaluate_transformer,
)

if _HAS_SKLEARN:
from coremltools.converters import sklearn
from sklearn.feature_extraction import DictVectorizer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression


@unittest.skipIf(not _HAS_SKLEARN, "Missing sklearn. Skipping tests.")
Expand Down Expand Up @@ -41,8 +48,8 @@ def _test_conversion(self, data, trained_dict_vectorizer):
)
assert ret["num_errors"] == 0

def test_dictvectorizer(self):

def test_dictvectorizer(self):
D = [
{"foo": 1, "bar": 3},
{"bar": 4, "baz": 2},
Expand All @@ -56,6 +63,7 @@ def test_dictvectorizer(self):
v = v.fit(D)
self._test_conversion(D, v)


def test_unseen_or_no_features(self):
D1 = [{"camelot": 0, "spamalot": 1}]
D2 = [{}, {"nothing": 21}]
Expand All @@ -67,34 +75,25 @@ def test_unseen_or_no_features(self):
v = v.fit(D1)
self._test_conversion(D2, v)

def test_int_features_in_pipeline(self):

import numpy.random as rn
import pandas as pd

def test_int_features_in_pipeline(self):
rn.seed(0)

x_train_dict = [
dict((rn.randint(100), 1) for i in range(20)) for j in range(100)
]
y_train = [0, 1] * 50

from sklearn.pipeline import Pipeline
from sklearn.feature_extraction import DictVectorizer
from sklearn.linear_model import LogisticRegression

pl = Pipeline([("dv", DictVectorizer()), ("lm", LogisticRegression())])
pl.fit(x_train_dict, y_train)

import coremltools

model = coremltools.converters.sklearn.convert(
pl, input_features="features", output_feature_names="target"
)

if _is_macos() and _macos_version() >= (10, 13):
x = pd.DataFrame(
{"features": x_train_dict, "prediction": pl.predict(x_train_dict)}
{"features": x_train_dict, "target": pl.predict(x_train_dict)}
)

cur_eval_metics = evaluate_classifier(model, x)
Expand Down
Loading

0 comments on commit bb8dd74

Please sign in to comment.