def test_embed_dim(estimator, build_dataset): # Checks that the the dimension of the output space is as expected input_data, labels, _, X = build_dataset() model = clone(estimator) set_random_state(model) model.fit(*remove_y_quadruplets(estimator, input_data, labels)) assert model.transform(X).shape == X.shape # assert that ValueError is thrown if input shape is 1D context = make_context(estimator) err_msg = ("2D array of formed points expected{}. Found 1D array " "instead:\ninput={}. Reshape your data and/or use a " "preprocessor.\n".format(context, X[0])) with pytest.raises(ValueError) as raised_error: model.score_pairs(model.transform(X[0, :])) assert str(raised_error.value) == err_msg # we test that the shape is also OK when doing dimensionality reduction if hasattr(model, 'n_components'): model.set_params(n_components=2) model.fit(*remove_y_quadruplets(estimator, input_data, labels)) assert model.transform(X).shape == (X.shape[0], 2) # assert that ValueError is thrown if input shape is 1D with pytest.raises(ValueError) as raised_error: model.transform(model.transform(X[0, :])) assert str(raised_error.value) == err_msg
def test_embed_dim(estimator, build_dataset): # Checks that the the dimension of the output space is as expected input_data, labels, _, X = build_dataset() model = clone(estimator) set_random_state(model) model.fit(*remove_y_quadruplets(estimator, input_data, labels)) assert model.transform(X).shape == X.shape # assert that ValueError is thrown if input shape is 1D context = make_context(estimator) err_msg = ("2D array of formed points expected{}. Found 1D array " "instead:\ninput={}. Reshape your data and/or use a " "preprocessor.\n".format(context, X[0])) with pytest.raises(ValueError) as raised_error: model.score_pairs(model.transform(X[0, :])) assert str(raised_error.value) == err_msg # we test that the shape is also OK when doing dimensionality reduction if type(model).__name__ in {'LFDA', 'MLKR', 'NCA', 'RCA'}: # TODO: # avoid this enumeration and rather test if hasattr n_components # as soon as we have made the arguments names as such (issue #167) model.set_params(num_dims=2) model.fit(*remove_y_quadruplets(estimator, input_data, labels)) assert model.transform(X).shape == (X.shape[0], 2) # assert that ValueError is thrown if input shape is 1D with pytest.raises(ValueError) as raised_error: model.transform(model.transform(X[0, :])) assert str(raised_error.value) == err_msg
def test_error_message_tuple_size(estimator, _): """Tests that if a tuples learner is not given the good number of points per tuple, it throws an error message""" estimator = clone(estimator) set_random_state(estimator) invalid_pairs = np.ones((2, 5, 2)) y = [1, 1] with pytest.raises(ValueError) as raised_err: estimator.fit(*remove_y(estimator, invalid_pairs, y)) expected_msg = ( "Tuples of {} element(s) expected{}. Got tuples of 5 " "element(s) instead (shape=(2, 5, 2)):\ninput={}.\n".format( estimator._tuple_size, make_context(estimator), invalid_pairs)) assert str(raised_err.value) == expected_msg
def test_error_message_t_score_pairs(estimator, _): """tests that if you want to score_pairs on triplets for instance, it returns the right error message """ estimator = clone(estimator) set_random_state(estimator) estimator.check_preprocessor() triplets = np.array([[[1.3, 6.3], [3., 6.8], [6.5, 4.4]], [[1.9, 5.3], [1., 7.8], [3.2, 1.2]]]) with pytest.raises(ValueError) as raised_err: estimator.score_pairs(triplets) expected_msg = ("Tuples of 2 element(s) expected{}. Got tuples of 3 " "element(s) instead (shape=(2, 3, 2)):\ninput={}.\n" .format(make_context(estimator), triplets)) assert str(raised_err.value) == expected_msg
def test_error_message_tuple_size(estimator, _): """Tests that if a tuples learner is not given the good number of points per tuple, it throws an error message""" estimator = clone(estimator) set_random_state(estimator) invalid_pairs = np.array([[[1.3, 6.3], [3., 6.8], [6.5, 4.4]], [[1.9, 5.3], [1., 7.8], [3.2, 1.2]]]) y = [1, 1] with pytest.raises(ValueError) as raised_err: estimator.fit(*remove_y_quadruplets(estimator, invalid_pairs, y)) expected_msg = ("Tuples of {} element(s) expected{}. Got tuples of 3 " "element(s) instead (shape=(2, 3, 2)):\ninput={}.\n" .format(estimator._tuple_size, make_context(estimator), invalid_pairs)) assert str(raised_err.value) == expected_msg
def test_error_message_tuple_size(estimator): """Tests that if a tuples learner is not given the good number of points per tuple, it throws an error message""" estimator = clone(estimator) set_random_state(estimator) invalid_pairs = np.array([[[1.3, 6.3], [3., 6.8], [6.5, 4.4]], [[1.9, 5.3], [1., 7.8], [3.2, 1.2]]]) y = [1, 1] with pytest.raises(ValueError) as raised_err: estimator.fit(invalid_pairs, y) expected_msg = ( "Tuples of {} element(s) expected{}. Got tuples of 3 " "element(s) instead (shape=(2, 3, 2)):\ninput={}.\n".format( estimator._tuple_size, make_context(estimator), invalid_pairs)) assert str(raised_err.value) == expected_msg
def test_error_message_t_score_pairs(estimator, _): """tests that if you want to score_pairs on triplets for instance, it returns the right error message """ estimator = clone(estimator) set_random_state(estimator) estimator.check_preprocessor() triplets = np.array([[[1.3, 6.3], [3., 6.8], [6.5, 4.4]], [[1.9, 5.3], [1., 7.8], [3.2, 1.2]]]) with pytest.raises(ValueError) as raised_err: estimator.score_pairs(triplets) expected_msg = ("Tuples of 2 element(s) expected{}. Got tuples of 3 " "element(s) instead (shape=(2, 3, 2)):\ninput={}.\n" .format(make_context(estimator), triplets)) assert str(raised_err.value) == expected_msg
def test_score_pairs_dim(estimator, build_dataset): # scoring of 3D arrays should return 1D array (several tuples), # and scoring of 2D arrays (one tuple) should return an error (like # scikit-learn's error when scoring 1D arrays) input_data, labels, _, X = build_dataset() model = clone(estimator) set_random_state(model) model.fit(*remove_y_quadruplets(estimator, input_data, labels)) tuples = np.array(list(product(X, X))) assert model.score_pairs(tuples).shape == (tuples.shape[0], ) context = make_context(estimator) msg = ("3D array of formed tuples expected{}. Found 2D array " "instead:\ninput={}. Reshape your data and/or use a preprocessor.\n" .format(context, tuples[1])) with pytest.raises(ValueError) as raised_error: model.score_pairs(tuples[1]) assert str(raised_error.value) == msg
def test_preprocess_points_invalid_message(estimator): """Checks that if the preprocessor does some weird stuff, the preprocessed input is detected as weird.""" context = make_context(estimator) + (' after the preprocessor ' 'has been applied') def preprocessor(sequence): return np.ones((len(sequence), 2, 2)) # returns a 3D array instead of 2D with pytest.raises(ValueError) as raised_error: check_input(np.ones((3,)), type_of_inputs='classic', preprocessor=preprocessor, estimator=estimator) expected_msg = ("2D array of formed points expected{}. " "Found 3D array instead:\ninput={}. Reshape your data{}.\n" .format(context, np.ones((3, 2, 2)), ' and/or use a preprocessor' if preprocessor is not None else '')) assert str(raised_error.value) == expected_msg
def test_preprocess_points_invalid_message(estimator): """Checks that if the preprocessor does some weird stuff, the preprocessed input is detected as weird.""" context = make_context(estimator) + (' after the preprocessor ' 'has been applied') def preprocessor(sequence): return np.ones((len(sequence), 2, 2)) # returns a 3D array instead of 2D with pytest.raises(ValueError) as raised_error: check_input(np.ones((3,)), type_of_inputs='classic', preprocessor=preprocessor, estimator=estimator) expected_msg = ("2D array of formed points expected{}. " "Found 3D array instead:\ninput={}. Reshape your data{}.\n" .format(context, np.ones((3, 2, 2)), ' and/or use a preprocessor' if preprocessor is not None else '')) assert str(raised_error.value) == expected_msg
def test_make_context(estimator, expected): """test the make_name function""" assert make_context(estimator) == expected
def test_make_context(estimator, expected): """test the make_name function""" assert make_context(estimator) == expected