Skip to content

Commit

Permalink
revised task api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haifeng-jin committed Jun 22, 2020
1 parent a51a830 commit 683c1b1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 76 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ filterwarnings =
ignore::RuntimeWarning
ignore::PendingDeprecationWarning
ignore::FutureWarning
ignore::numpy.VisibleDeprecationWarning

addopts=-v
--durations=10
Expand Down
42 changes: 29 additions & 13 deletions tests/autokeras/tasks/image_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
from unittest import mock

from autokeras.tasks import image
import autokeras as ak
from tests import utils


@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_image_classifier(auto_model, tmp_path):
image.ImageClassifier(directory=tmp_path, max_trials=2, seed=utils.SEED)
assert auto_model.called
@mock.patch('autokeras.AutoModel.fit')
def test_img_clf_fit_call_auto_model_fit(fit, tmp_path):
auto_model = ak.ImageClassifier(directory=tmp_path, seed=utils.SEED)

auto_model.fit(
x=utils.generate_data(num_instances=100, shape=(32, 32, 3)),
y=utils.generate_one_hot_labels(num_instances=100, num_classes=10))

@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_image_regressor(auto_model, tmp_path):
image.ImageRegressor(directory=tmp_path, max_trials=2, seed=utils.SEED)
assert auto_model.called
assert fit.is_called


@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_image_segmenter(auto_model, tmp_path):
image.ImageSegmenter(directory=tmp_path, max_trials=2, seed=utils.SEED)
assert auto_model.called
@mock.patch('autokeras.AutoModel.fit')
def test_img_reg_fit_call_auto_model_fit(fit, tmp_path):
auto_model = ak.ImageRegressor(directory=tmp_path, seed=utils.SEED)

auto_model.fit(
x=utils.generate_data(num_instances=100, shape=(32, 32, 3)),
y=utils.generate_data(num_instances=100, shape=(1,)))

assert fit.is_called


@mock.patch('autokeras.AutoModel.fit')
def test_img_seg_fit_call_auto_model_fit(fit, tmp_path):
auto_model = ak.tasks.image.ImageSegmenter(
directory=tmp_path, seed=utils.SEED)

auto_model.fit(
x=utils.generate_data(num_instances=100, shape=(32, 32, 3)),
y=utils.generate_data(num_instances=100, shape=(32, 32)))

assert fit.is_called
80 changes: 26 additions & 54 deletions tests/autokeras/tasks/structure_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,51 @@
import pandas
import pytest

from autokeras.tasks import structured_data
import autokeras as ak
from tests import utils


def test_structured_data_unknown_str_in_col_type(tmp_path):
def test_raise_error_unknown_str_in_col_type(tmp_path):
with pytest.raises(ValueError) as info:
structured_data.StructuredDataClassifier(
column_types=utils.FALSE_COLUMN_TYPES_FROM_CSV,
ak.StructuredDataClassifier(
column_types={'age': 'num', 'parch': 'categorical'},
directory=tmp_path,
max_trials=1,
seed=utils.SEED)
assert 'Column_types should be either "categorical"' in str(info.value)


def test_structured_data_col_name_type_mismatch(tmp_path):
with pytest.raises(ValueError) as info:
structured_data.StructuredDataClassifier(
column_names=utils.COLUMN_NAMES_FROM_NUMPY,
column_types=utils.COLUMN_TYPES_FROM_CSV,
directory=tmp_path,
max_trials=1,
seed=utils.SEED)
assert 'Column_names and column_types are mismatched.' in str(info.value)
assert 'Column_types should be either "categorical"' in str(info.value)


@mock.patch('autokeras.auto_model.AutoModel.fit')
@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_structured_classifier(init, fit, tmp_path):
num_data = 500
train_x = utils.generate_structured_data(num_data)
train_y = utils.generate_one_hot_labels(num_instances=num_data, num_classes=3)
@mock.patch('autokeras.AutoModel.fit')
def test_structured_clf_fit_call_auto_model_fit(fit, tmp_path):
auto_model = ak.StructuredDataClassifier(directory=tmp_path, seed=utils.SEED)

clf = structured_data.StructuredDataClassifier(
column_names=utils.COLUMN_NAMES_FROM_NUMPY,
directory=tmp_path,
max_trials=1,
seed=utils.SEED)
clf.fit(train_x, train_y, epochs=2, validation_data=(train_x, train_y))
auto_model.fit(
x=utils.generate_structured_data(num_instances=100),
y=utils.generate_one_hot_labels(num_instances=100, num_classes=3))

assert init.called
assert fit.called
assert fit.is_called


@mock.patch('autokeras.auto_model.AutoModel.fit')
@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_structured_regressor(init, fit, tmp_path):
num_data = 500
train_x = utils.generate_structured_data(num_data)
train_y = utils.generate_data(num_instances=100, shape=(1,))
@mock.patch('autokeras.AutoModel.fit')
def test_structured_reg_fit_call_auto_model_fit(fit, tmp_path):
auto_model = ak.StructuredDataRegressor(directory=tmp_path, seed=utils.SEED)

clf = structured_data.StructuredDataRegressor(
column_names=utils.COLUMN_NAMES_FROM_NUMPY,
directory=tmp_path,
max_trials=1,
seed=utils.SEED)
clf.fit(train_x, train_y, epochs=2, validation_data=(train_x, train_y))
auto_model.fit(
x=utils.generate_structured_data(num_instances=100),
y=utils.generate_data(num_instances=100, shape=(1,)))

assert init.called
assert fit.called
assert fit.is_called


@mock.patch('autokeras.auto_model.AutoModel.fit')
@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_structured_data_classifier_from_csv(init, fit, tmp_path):
clf = structured_data.StructuredDataClassifier(
directory=tmp_path,
max_trials=1,
seed=utils.SEED)
@mock.patch('autokeras.AutoModel.fit')
def test_structured_data_clf_convert_csv_to_df_and_np(fit, tmp_path):
auto_model = ak.StructuredDataClassifier(directory=tmp_path, seed=utils.SEED)

clf.fit(x=utils.TRAIN_FILE_PATH, y='survived', epochs=2,
validation_data=(utils.TEST_FILE_PATH, 'survived'))
auto_model.fit(x=utils.TRAIN_FILE_PATH,
y='survived',
epochs=2,
validation_data=(utils.TEST_FILE_PATH, 'survived'))

assert init.called
_, kwargs = fit.call_args_list[0]
assert isinstance(kwargs['x'], pandas.DataFrame)
assert isinstance(kwargs['y'], np.ndarray)
30 changes: 21 additions & 9 deletions tests/autokeras/tasks/text_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
from unittest import mock

from autokeras.tasks import text
import numpy as np

import autokeras as ak
from tests import utils


@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_text_classifier(auto_model, tmp_path):
text.TextClassifier(directory=tmp_path, max_trials=2, seed=utils.SEED)
assert auto_model.called
@mock.patch('autokeras.AutoModel.fit')
def test_txt_clf_fit_call_auto_model_fit(fit, tmp_path):
auto_model = ak.TextClassifier(directory=tmp_path, seed=utils.SEED)

auto_model.fit(
x=np.array(['a b c', 'b b c']),
y=np.array([1, 2]))

assert fit.is_called


@mock.patch('autokeras.AutoModel.fit')
def test_txt_reg_fit_call_auto_model_fit(fit, tmp_path):
auto_model = ak.TextRegressor(directory=tmp_path, seed=utils.SEED)

auto_model.fit(
x=np.array(['a b c', 'b b c']),
y=np.array([1.0, 2.0]))

@mock.patch('autokeras.auto_model.AutoModel.__init__')
def test_text_regressor(auto_model, tmp_path):
text.TextRegressor(directory=tmp_path, max_trials=2, seed=utils.SEED)
assert auto_model.called
assert fit.is_called

0 comments on commit 683c1b1

Please sign in to comment.