From c10945f53a3274748510aa9e7c120f22e9b5f01a Mon Sep 17 00:00:00 2001 From: Francois Chollet Date: Wed, 11 Jan 2017 11:40:57 -0800 Subject: [PATCH] PEP8 fixes in tests. --- tests/integration_tests/test_temporal_data_tasks.py | 6 +++--- tests/keras/backend/test_backends.py | 8 ++++---- tests/keras/engine/test_topology.py | 3 ++- tests/keras/preprocessing/test_image.py | 2 +- tests/keras/test_callbacks.py | 1 + tests/keras/test_initializations.py | 4 ++-- tests/keras/test_metrics.py | 6 +++--- tests/keras/wrappers/test_scikit_learn.py | 4 ++++ 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/tests/integration_tests/test_temporal_data_tasks.py b/tests/integration_tests/test_temporal_data_tasks.py index b2b56ed33..f932bd2e9 100644 --- a/tests/integration_tests/test_temporal_data_tasks.py +++ b/tests/integration_tests/test_temporal_data_tasks.py @@ -108,8 +108,8 @@ def test_stacked_lstm_char_prediction(): y = np.zeros((len(sentences), number_of_chars), dtype=np.bool) for i, sentence in enumerate(sentences): for t, char in enumerate(sentence): - X[i, t, ord(char)-ord('a')] = 1 - y[i, ord(next_chars[i])-ord('a')] = 1 + X[i, t, ord(char) - ord('a')] = 1 + y[i, ord(next_chars[i]) - ord('a')] = 1 # learn the alphabet with stacked LSTM model = Sequential([ @@ -123,7 +123,7 @@ def test_stacked_lstm_char_prediction(): # prime the model with 'ab' sequence and let it generate the learned alphabet sentence = alphabet[:sequence_length] generated = sentence - for iteration in range(number_of_chars-sequence_length): + for iteration in range(number_of_chars - sequence_length): x = np.zeros((1, sequence_length, number_of_chars)) for t, char in enumerate(sentence): x[0, t, ord(char) - ord('a')] = 1. diff --git a/tests/keras/backend/test_backends.py b/tests/keras/backend/test_backends.py index b0dccd671..cee21ae73 100644 --- a/tests/keras/backend/test_backends.py +++ b/tests/keras/backend/test_backends.py @@ -790,7 +790,7 @@ class TestBackend(object): # len max_time_steps array of batch_size x depth matrices inputs = ([input_prob_matrix_0[t, :][np.newaxis, :] - for t in range(seq_len_0)] + # Pad to max_time_steps = 8 + for t in range(seq_len_0)] + # Pad to max_time_steps = 8 2 * [np.zeros((1, depth), dtype=np.float32)]) inputs = KTF.variable(np.asarray(inputs).transpose((1, 0, 2))) @@ -899,7 +899,7 @@ class TestBackend(object): def test_foldl(self): x = np.random.rand(10, 3).astype(np.float32) for K in [KTF, KTH]: - kx = K.eval(K.foldl(lambda a, b: a+b, x)) + kx = K.eval(K.foldl(lambda a, b: a + b, x)) assert (3,) == kx.shape assert_allclose(x.sum(axis=0), kx, atol=1e-05) @@ -911,8 +911,8 @@ class TestBackend(object): # right to left we have no such problem and the result is larger x = np.array([1e-20, 1e-20, 10, 10, 10], dtype=np.float32) for K in [KTF, KTH]: - p1 = K.eval(K.foldl(lambda a, b: a*b, x)) - p2 = K.eval(K.foldr(lambda a, b: a*b, x)) + p1 = K.eval(K.foldl(lambda a, b: a * b, x)) + p2 = K.eval(K.foldr(lambda a, b: a * b, x)) assert p1 < p2 assert 9e-38 < p2 <= 1e-37 diff --git a/tests/keras/engine/test_topology.py b/tests/keras/engine/test_topology.py index c64d6e897..ea5077ab5 100644 --- a/tests/keras/engine/test_topology.py +++ b/tests/keras/engine/test_topology.py @@ -9,6 +9,7 @@ from keras import backend as K from keras.models import model_from_json, model_from_yaml from keras.utils.test_utils import keras_test + @keras_test def test_get_updates_for(): a = Input(shape=(2,)) @@ -26,7 +27,7 @@ def test_get_losses_for(): dense_layer = Dense(1) dense_layer.add_loss(0, inputs=a) dense_layer.add_loss(1, inputs=None) - + assert dense_layer.get_losses_for(a) == [0] assert dense_layer.get_losses_for(None) == [1] diff --git a/tests/keras/preprocessing/test_image.py b/tests/keras/preprocessing/test_image.py index 2792ac4cd..712ce1b5f 100644 --- a/tests/keras/preprocessing/test_image.py +++ b/tests/keras/preprocessing/test_image.py @@ -15,7 +15,7 @@ class TestImage: gray_images = [] for n in range(8): bias = np.random.rand(img_w, img_h, 1) * 64 - variance = np.random.rand(img_w, img_h, 1) * (255-64) + variance = np.random.rand(img_w, img_h, 1) * (255 - 64) imarray = np.random.rand(img_w, img_h, 3) * variance + bias im = Image.fromarray(imarray.astype('uint8')).convert('RGB') rgb_images.append(im) diff --git a/tests/keras/test_callbacks.py b/tests/keras/test_callbacks.py index 2f5e95a7a..d2ffe3c75 100644 --- a/tests/keras/test_callbacks.py +++ b/tests/keras/test_callbacks.py @@ -96,6 +96,7 @@ def test_ModelCheckpoint(): os.remove(filepath.format(epoch=1)) os.remove(filepath.format(epoch=3)) + def test_EarlyStopping(): (X_train, y_train), (X_test, y_test) = get_test_data(nb_train=train_samples, nb_test=test_samples, diff --git a/tests/keras/test_initializations.py b/tests/keras/test_initializations.py index 50208ac78..8bdb04418 100644 --- a/tests/keras/test_initializations.py +++ b/tests/keras/test_initializations.py @@ -86,10 +86,10 @@ def test_identity(tensor_shape): if len(tensor_shape) > 2: with pytest.raises(Exception): _runner(initializations.identity, tensor_shape, - target_mean=1./SHAPE[0], target_max=1.) + target_mean=1. / SHAPE[0], target_max=1.) else: _runner(initializations.identity, tensor_shape, - target_mean=1./SHAPE[0], target_max=1.) + target_mean=1. / SHAPE[0], target_max=1.) @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV']) diff --git a/tests/keras/test_metrics.py b/tests/keras/test_metrics.py index 0eca9f9e2..0e396357e 100644 --- a/tests/keras/test_metrics.py +++ b/tests/keras/test_metrics.py @@ -105,13 +105,13 @@ def test_top_k_categorical_accuracy(): y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]])) success_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, - k=3)) + k=3)) assert success_result == 1 partial_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, - k=2)) + k=2)) assert partial_result == 0.5 failure_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, - k=1)) + k=1)) assert failure_result == 0 diff --git a/tests/keras/wrappers/test_scikit_learn.py b/tests/keras/wrappers/test_scikit_learn.py index 7fe66e266..ca5196418 100644 --- a/tests/keras/wrappers/test_scikit_learn.py +++ b/tests/keras/wrappers/test_scikit_learn.py @@ -49,6 +49,7 @@ def test_clasify_build_fn(): def test_clasify_class_build_fn(): class ClassBuildFnClf(object): + def __call__(self, hidden_dims): return build_fn_clf(hidden_dims) @@ -61,6 +62,7 @@ def test_clasify_class_build_fn(): def test_clasify_inherit_class_build_fn(): class InheritClassBuildFnClf(KerasClassifier): + def __call__(self, hidden_dims): return build_fn_clf(hidden_dims) @@ -110,6 +112,7 @@ def test_regression_build_fn(): def test_regression_class_build_fn(): class ClassBuildFnReg(object): + def __call__(self, hidden_dims): return build_fn_reg(hidden_dims) @@ -122,6 +125,7 @@ def test_regression_class_build_fn(): def test_regression_inherit_class_build_fn(): class InheritClassBuildFnReg(KerasRegressor): + def __call__(self, hidden_dims): return build_fn_reg(hidden_dims)