Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3. [WIP] OASIS algorithm implementation #330

Open
wants to merge 146 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
8fc975c
Add venv to gitignore
Sep 13, 2021
a3b4d6e
Create yml draft
mvargas33 Sep 15, 2021
bc8c7a8
Update yml
mvargas33 Sep 15, 2021
1ec62a2
Update 2
mvargas33 Sep 15, 2021
dac8414
Update 3
mvargas33 Sep 15, 2021
faaf5d7
YML: Only Python3 versions
mvargas33 Sep 15, 2021
0456111
Add Codecov to CI
mvargas33 Sep 15, 2021
e6edd75
Mirroring actual yml from metric_learning repo
mvargas33 Sep 15, 2021
c3bdde2
Fix codecov
mvargas33 Sep 15, 2021
0a0c9e8
Fix old scikit learn
mvargas33 Sep 15, 2021
db1bde0
Update yml
mvargas33 Sep 15, 2021
8046281
Remove 3.9 from compatibility
mvargas33 Sep 15, 2021
09fdd03
Fixed issue with sklearn 0.20
mvargas33 Sep 15, 2021
ab3dc5f
Delete comments, and unnecesary workflow_dispatch
mvargas33 Sep 16, 2021
d0a7a24
Merge branch 'master' of https://github.com/mvargas33/metric-learn
Sep 16, 2021
cdbefa7
Merge branch 'scikit-learn-contrib:master' into feat-bilinear
mvargas33 Sep 17, 2021
a353e70
FIrst draft of bilinear mixin
mvargas33 Sep 17, 2021
889f12a
Fix score_pairs
mvargas33 Sep 17, 2021
0495338
Two implementations for score_pairs
mvargas33 Sep 17, 2021
4285b56
Generalized toy tests
mvargas33 Sep 17, 2021
9ff9617
Handmade tests incorporated
mvargas33 Sep 17, 2021
c271d06
Oasis draft v0.1
Sep 20, 2021
eec5970
Oasis draft v0.2
Sep 20, 2021
ce08910
OASIS draft v0.3
Sep 20, 2021
e610384
Fix identation
Sep 21, 2021
67fe603
Fix identation for bilinear
Sep 21, 2021
b5007c9
Merge branch 'feat-bilinear' into feat-oasis
Sep 21, 2021
d34cfcd
Add performance test to choose between two methods for bilinear calc
Sep 21, 2021
d2179d6
Found an efficient way to compute Bilinear Sim for n pairs
Sep 22, 2021
16bd667
Update method's descriptions
Sep 22, 2021
adbdd85
Following the correct testing structure
Sep 22, 2021
4240738
Fix identation
Sep 22, 2021
25526e9
Add more tests. Fix 4 to 2 identation
Sep 23, 2021
39a1c76
Minor flake8 fix
Sep 23, 2021
edf64c5
Commented each test
Sep 23, 2021
7304ce5
All tests have been generalized
Sep 23, 2021
97c1fcc
Fix flake8 identation
Sep 23, 2021
3e6adb7
Minor details
Sep 23, 2021
ea74de7
Merge branch 'feat-bilinear' into feat-oasis
Sep 24, 2021
a139760
Fit OASIS into current package structure
Sep 24, 2021
9d9023b
Convention: score_pairs returns -1*bilinear to avoid breaking classif…
Sep 24, 2021
42e82c2
Add sanity check for OASIS. The more the triplets, the best the score…
Sep 24, 2021
0b80c5c
Classification with sim=0 will be -1
Sep 27, 2021
529b7d3
Indices management. Tests to be developed yet
Sep 27, 2021
7843c0b
Expected warning silenced
Sep 27, 2021
5c345eb
Tests needs a -1, as score_pairs is -1*bilinearSim
Sep 27, 2021
997abd0
max_iter is now n_iter
Sep 27, 2021
4bdd786
Add custom M initialization
Sep 27, 2021
1b67f8f
Base draft test for _get_random_indices. To be improved
Sep 28, 2021
ab77a05
Generalized test_indices_funct test
Sep 28, 2021
2c31cae
Fix identation
Sep 28, 2021
42bf565
Add OASIS Supervised.
Sep 28, 2021
d59ca9d
Patch an SCML test
Sep 28, 2021
7c92493
Test Iris with OASIS_Supervised
Sep 28, 2021
11a383e
Add oasis to library export
Sep 29, 2021
caa7ade
Add Gridserach example for OASIS
Sep 29, 2021
6896240
Moved params to constructor
Sep 29, 2021
0e8938b
Add experiment of random_State
Sep 29, 2021
efb99b1
Tests consistency in random_state
Sep 30, 2021
09f901e
Add an output consistency test. Use parametrize
Sep 30, 2021
09630a0
Add another test regarding random_state
Sep 30, 2021
4b7cdec
Remove 3.9 from compatibility
mvargas33 Sep 15, 2021
0147c0c
FIrst draft of bilinear mixin
mvargas33 Sep 17, 2021
ec09f59
Fix score_pairs
mvargas33 Sep 17, 2021
ec49397
Two implementations for score_pairs
mvargas33 Sep 17, 2021
2f3c3e1
Generalized toy tests
mvargas33 Sep 17, 2021
c21d283
Handmade tests incorporated
mvargas33 Sep 17, 2021
dbe2a7a
Fix identation for bilinear
Sep 21, 2021
ee5c5ee
Add performance test to choose between two methods for bilinear calc
Sep 21, 2021
9a10e06
Found an efficient way to compute Bilinear Sim for n pairs
Sep 22, 2021
b1edc46
Update method's descriptions
Sep 22, 2021
ae562e6
Following the correct testing structure
Sep 22, 2021
7ebc026
Fix identation
Sep 22, 2021
1d752f7
Add more tests. Fix 4 to 2 identation
Sep 23, 2021
45c9b97
Minor flake8 fix
Sep 23, 2021
407f910
Commented each test
Sep 23, 2021
80c9085
All tests have been generalized
Sep 23, 2021
90ac550
Fix flake8 identation
Sep 23, 2021
68eeda9
Minor details
Sep 23, 2021
c47797c
Remove 3.9 from compatibility
mvargas33 Sep 15, 2021
e07b11a
First draft of refactoring BaseMetricLearner and Mahalanobis Learner
Oct 1, 2021
f06c49d
Modify _get_random_indices as discussed
Oct 5, 2021
a029549
Minor code style fix
Oct 5, 2021
8aaa37c
Move _to_index_points to _utils. Used by OASIS and SCML
Oct 5, 2021
e9d9d40
move _get_random_indices tp _util. Update OASIS and tests
Oct 5, 2021
dc6710f
move _initialize_sim_bilinear to _utils. Fix Oasis and tests.
Oct 5, 2021
8210acd
Avoid warning related to score_pairs deprecation in tests of pair_cal…
Oct 6, 2021
11b5df6
Minor fix
Oct 6, 2021
06b7131
Replaced score_pairs with pair_distance in tests
Oct 6, 2021
d5cb8b4
Replace score_pairs with pair_distance inb docs.
Oct 6, 2021
b88215f
Refactor sim_bilinear init
Oct 7, 2021
59e44ae
Merge custom_order with random_indices
Oct 7, 2021
cb4bbf5
Deleted unnecesary getter. All params are public
Oct 7, 2021
1709e2c
Changed Frobenius norm for np.linalg.norm
Oct 8, 2021
2f61e7b
Fix weird commit
Oct 8, 2021
9dd38aa
Fix weird commit
Oct 8, 2021
5f68ed2
Update classifiers to use pair_similarity
Oct 8, 2021
3d6450b
Updated rst docs
Oct 8, 2021
7bce493
Fix identation
Oct 8, 2021
7e6584a
Update docs of score_pairs, get_metric
Oct 11, 2021
0b58f45
Add deprecation Test. Fix identation
Oct 11, 2021
d4d3a9c
Merge branch 'master' into score-deprecation
Oct 11, 2021
d27bdf5
Merge branch 'score-deprecation' into feat-bilinear
Oct 11, 2021
78a205c
Refactor to use pair_similarity instead of score_pairs
Oct 11, 2021
fd9d7c3
Draft of OASIS docs
Oct 12, 2021
2bb9171
Add more docs to OASIS
Oct 13, 2021
d4cc32c
Add OASIS source code docs
Oct 13, 2021
dde3576
Add more testing. Test refactor TBD
Oct 13, 2021
3020110
Tests are now parametrized
Oct 13, 2021
a125b09
Merge branch 'feat-bilinear' into feat-oasis. Fix score_pairs in oasis
Oct 13, 2021
ca6f839
Merge branch 'master' into feat-oasis
mvargas33 Oct 13, 2021
2746668
Add bilinear in introduction
Oct 15, 2021
920e504
Minor comment on use case
Oct 15, 2021
7a24319
More changes to sueprvised
Oct 15, 2021
2f8ee76
Changes in weakly Supervised
Oct 15, 2021
aa04d79
Merge branch 'feat-bilinear' into feat-oasis
Oct 15, 2021
60c88a6
Merge remote-tracking branch 'upstream/master' into score-deprecation
Oct 19, 2021
8c55970
Fixed changes requested 1
Oct 19, 2021
787a8d1
Fixed changes requested 2
Oct 19, 2021
e14f956
Add equivalence test, p_dist == p_score
Oct 19, 2021
0941a32
Fix tests and identation.
Oct 19, 2021
b019d85
Fixed changes requested 3
Oct 20, 2021
74df897
Fix identation
Oct 20, 2021
c62a4e7
Last requested changes
Oct 21, 2021
526e4ba
Merge branch 'score-deprecation' into feat-bilinear
Oct 21, 2021
2199724
Replaced pair_similarity for paiir_score
Oct 21, 2021
249e0fe
Last small detail
Oct 21, 2021
80f31ba
Merge branch 'score-deprecation' into feat-bilinear
Oct 21, 2021
8df44a4
Merge remote-tracking branch 'upstream/master' into feat-bilinear
Oct 26, 2021
eef13bb
Classifiers only test classifiers methods now. + Standard doctrings now.
Oct 26, 2021
b952af0
Work in tests. More comments. Some refactors
Oct 26, 2021
7cc0d5e
Learner lists for M and B learners. Separated test by kind. Mock clas…
Oct 27, 2021
5f6bdc2
Moved mocks to test_utils.py, then refactor test_bilinear_mixin.py
Oct 27, 2021
100a05d
Merge branch 'master' into feat-bilinear
Nov 3, 2021
f69391b
Merge branch 'feat-bilinear' into feat-oasis
Nov 3, 2021
d9d4584
Removed custom order. Optimize OASIS loop
Nov 3, 2021
190b9c0
test bilinear init parametrized. Fix partial_fit. Sanity check test w…
Nov 3, 2021
5823b39
Moved OASIS tests to metric_learn_test.py
Nov 8, 2021
447935e
Add OASIS to all bilinear, general tests. Change class inheritance to…
Nov 8, 2021
d86cfa0
Add covariance equivalence test at bilinear init. Fix indentation.
Nov 8, 2021
3bf5eae
Resolved observations in interoduction.rst
Nov 8, 2021
acfd54b
Resolved all observations for supervised.rst and weakly_s.rst
Nov 8, 2021
69bd9fe
Spellcheck
Nov 9, 2021
ade34cc
Moved common test to test_base_metric.py . Refactor preprocessor test…
Nov 9, 2021
16f1bd7
Merge branch 'feat-bilinear' into feat-oasis
Nov 9, 2021
cd30564
Adding Oasis example: WIP
Nov 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed changes requested 2
mvargas33 committed Oct 19, 2021
commit 787a8d16df04e05f176da61d1ad6d668872d7300
10 changes: 5 additions & 5 deletions doc/supervised.rst
Original file line number Diff line number Diff line change
@@ -82,18 +82,18 @@ array([0.49627072, 3.65287282, 6.06079877])
>>> metric_fun([3.5, 3.6], [5.6, 2.4])
0.4962707194621285

- Alternatively, you can use `pair_similarity` to return the **score** between
- Alternatively, you can use `pair_score` to return the **score** between
pairs of points, the larger the **score**, the more similar the pair
and vice-versa. For Mahalanobis learners, it is equal to the opposite
of the distance.

>>> score = nca.pair_similarity([[[3.5, 3.6], [5.6, 2.4]], [[1.2, 4.2], [2.1, 6.4]], [[3.3, 7.8], [10.9, 0.1]]])
>>> score = nca.pair_score([[[3.5, 3.6], [5.6, 2.4]], [[1.2, 4.2], [2.1, 6.4]], [[3.3, 7.8], [10.9, 0.1]]])
>>> score
array([-0.49627072, -3.65287282, -6.06079877])

This is useful because `pair_similarity` matches the **score** semantic of
scikit-learn's `Classification metrics
<https://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics>`_.
This is useful because `pair_score` matches the **score** semantic of
scikit-learn's `Classification metrics
<https://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics>`_.

.. note::

12 changes: 6 additions & 6 deletions doc/weakly_supervised.rst
Original file line number Diff line number Diff line change
@@ -175,16 +175,16 @@ array([7.27607365, 0.88853014])
>>> metric_fun([3.5, 3.6, 5.2], [5.6, 2.4, 6.7])
7.276073646278203

- Alternatively, you can use `pair_similarity` to return the **score** between
- Alternatively, you can use `pair_score` to return the **score** between
pairs of points, the larger the **score**, the more similar the pair
and vice-versa. For Mahalanobis learners, it is equal to the opposite
of the distance.

>>> score = mmc.pair_similarity([[[3.5, 3.6], [5.6, 2.4]], [[1.2, 4.2], [2.1, 6.4]], [[3.3, 7.8], [10.9, 0.1]]])
>>> score = mmc.pair_score([[[3.5, 3.6], [5.6, 2.4]], [[1.2, 4.2], [2.1, 6.4]], [[3.3, 7.8], [10.9, 0.1]]])
>>> score
array([-0.49627072, -3.65287282, -6.06079877])

This is useful because `pair_similarity` matches the **score** semantic of
This is useful because `pair_score` matches the **score** semantic of
scikit-learn's `Classification metrics
<https://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics>`_.

@@ -356,7 +356,7 @@ returns the `sklearn.metrics.roc_auc_score` (which is threshold-independent).
.. note::
See :ref:`fit_ws` for more details on metric learners functions that are
not specific to learning on pairs, like `transform`, `pair_distance`,
`pair_similarity`, `get_metric` and `get_mahalanobis_matrix`.
`pair_score`, `get_metric` and `get_mahalanobis_matrix`.

Algorithms
----------
@@ -703,7 +703,7 @@ of triplets that have the right predicted ordering.
.. note::
See :ref:`fit_ws` for more details on metric learners functions that are
not specific to learning on pairs, like `transform`, `pair_distance`,
`pair_similarity`, `get_metric` and `get_mahalanobis_matrix`.
`pair_score`, `get_metric` and `get_mahalanobis_matrix`.



@@ -871,7 +871,7 @@ of quadruplets have the right predicted ordering.
.. note::
See :ref:`fit_ws` for more details on metric learners functions that are
not specific to learning on pairs, like `transform`, `pair_distance`,
`pair_similarity`, `get_metric` and `get_mahalanobis_matrix`.
`pair_score`, `get_metric` and `get_mahalanobis_matrix`.



81 changes: 43 additions & 38 deletions metric_learn/base_metric.py
Original file line number Diff line number Diff line change
@@ -41,8 +41,10 @@ def score_pairs(self, pairs):
Parameters
----------
pairs : `numpy.ndarray`, shape=(n_samples, 2, n_features)
3D array of pairs.
pairs : array-like, shape=(n_pairs, 2, n_features) or (n_pairs, 2)
3D Array of pairs to score, with each row corresponding to two points,
for 2D array of indices of pairs if the metric learner uses a
preprocessor.
Returns
-------
@@ -52,27 +54,29 @@ def score_pairs(self, pairs):
See Also
--------
get_metric : a method that returns a function to compute the metric between
two points. The difference with `score_pairs` is that it works on two
1D arrays and cannot use a preprocessor. Besides, the returned function
is independent of the metric learner and hence is not modified if the
metric learner is.
two points. The difference between `pair_score` and `pair_distance` is
that it works on two 1D arrays and cannot use a preprocessor. Besides,
the returned function is independent of the metric learner and hence is
not modified if the metric learner is.
"""

@abstractmethod
def pair_similarity(self, pairs):
def pair_score(self, pairs):
"""
.. versionadded:: 0.7.0 Compute the similarity score between pairs
Returns the similarity score between pairs. Depending on the algorithm,
this method can return the learned similarity score between pairs,
or the inverse of the distance learned between two pairs. The more the
score, the more similar the pairs. All learners have access to this
Returns the similarity score between pairs of points. Depending on the
algorithm, this method can return the learned similarity score between
pairs, or the opposite of the distance learned between pairs. The larger
the score, the more similar the pair. All learners have access to this
method.
Parameters
----------
pairs : `numpy.ndarray`, shape=(n_samples, 2, n_features)
3D array of pairs.
pairs : array-like, shape=(n_pairs, 2, n_features) or (n_pairs, 2)
3D Array of pairs to score, with each row corresponding to two points,
for 2D array of indices of pairs if the metric learner uses a
preprocessor.
Returns
-------
@@ -82,7 +86,7 @@ def pair_similarity(self, pairs):
See Also
--------
get_metric : a method that returns a function to compute the metric between
two points. The difference with `pair_similarity` is that it works on two
two points. The difference with `pair_score` is that it works on two
1D arrays and cannot use a preprocessor. Besides, the returned function
is independent of the metric learner and hence is not modified if the
metric learner is.
@@ -91,17 +95,18 @@ def pair_similarity(self, pairs):
@abstractmethod
def pair_distance(self, pairs):
"""
.. versionadded:: 0.7.0 Compute the distance score between pairs
.. versionadded:: 0.7.0 Compute the distance between pairs
Returns the distance score between pairs. For Mahalanobis learners, it
returns the pseudo-distance between pairs. It is not available for
learners that does not learn a distance or pseudo-distance, an error
will be shown instead.
Returns the (pseudo) distance between pairs, when available. For metric
learners that do not learn a (pseudo) distance, an error is thrown
instead.
Parameters
----------
pairs : `numpy.ndarray`, shape=(n_samples, 2, n_features)
3D array of pairs.
pairs : array-like, shape=(n_pairs, 2, n_features) or (n_pairs, 2)
3D Array of pairs to score, with each row corresponding to two points,
for 2D array of indices of pairs if the metric learner uses a
preprocessor.
Returns
-------
@@ -170,10 +175,10 @@ def _prepare_inputs(self, X, y=None, type_of_inputs='classic',

@abstractmethod
def get_metric(self):
"""Returns a function that takes as input two 1D arrays and outputs the
learned metric score on these two points. Depending on the algorithm, it
can return the distance or similarity function between pairs. It always
returns what the specific algorithm learns.
"""Returns a function that takes as input two 1D arrays and outputs
the value of the learned metric on these two points. Depending on the
algorithm, it can return a distance or a score function between pairs.
It always returns what the specific algorithm learns.
This function will be independent from the metric learner that learned it
(it will not be modified if the initial metric learner is modified),
@@ -206,13 +211,13 @@ def get_metric(self):
See Also
--------
pair_distance : a method that returns the distance score between several
pair_distance : a method that returns the distance between several
pairs of points. Unlike `get_metric`, this is a method of the metric
learner and therefore can change if the metric learner changes. Besides,
it can use the metric learner's preprocessor, and works on concatenated
arrays.
pair_similarity : a method that returns the similarity score between
pair_score : a method that returns the similarity score between
several pairs of points. Unlike `get_metric`, this is a method of the
metric learner and therefore can change if the metric learner changes.
Besides, it can use the metric learner's preprocessor, and works on
@@ -265,7 +270,7 @@ def score_pairs(self, pairs):
.. warning::
This method will be removed in 0.8.0. Please refer to `pair_distance`
or `pair_similarity`. This change will occur in order to add learners
or `pair_score`. This change will occur in order to add learners
that don't necessarily learn a Mahalanobis distance.
Returns the learned Mahalanobis distance between pairs.
@@ -302,14 +307,14 @@ def score_pairs(self, pairs):
that describes Mahalanobis Distances.
"""
dpr_msg = ("score_pairs will be deprecated in release 0.7.0. "
"Use pair_similarity to compute similarities, or "
"Use pair_score to compute similarity scores, or "
"pair_distances to compute distances.")
warnings.warn(dpr_msg, category=FutureWarning)
return self.pair_distance(pairs)

def pair_similarity(self, pairs):
def pair_score(self, pairs):
"""
Returns the inverse of the learned Mahalanobis distance between pairs.
Returns the opposite of the learned Mahalanobis distance between pairs.
Parameters
----------
@@ -321,12 +326,12 @@ def pair_similarity(self, pairs):
Returns
-------
scores : `numpy.ndarray` of shape=(n_pairs,)
The inverse of the learned Mahalanobis distance for every pair.
The opposite of the learned Mahalanobis distance for every pair.
See Also
--------
get_metric : a method that returns a function to compute the metric between
two points. The difference with `pair_similarity` is that it works on two
two points. The difference with `pair_score` is that it works on two
1D arrays and cannot use a preprocessor. Besides, the returned function
is independent of the metric learner and hence is not modified if the
metric learner is.
@@ -517,7 +522,7 @@ def decision_function(self, pairs):
pairs = check_input(pairs, type_of_inputs='tuples',
preprocessor=self.preprocessor_,
estimator=self, tuple_size=self._tuple_size)
return self.pair_similarity(pairs)
return self.pair_score(pairs)

def score(self, pairs, y):
"""Computes score of pairs similarity prediction.
@@ -787,8 +792,8 @@ def decision_function(self, triplets):
triplets = check_input(triplets, type_of_inputs='tuples',
preprocessor=self.preprocessor_,
estimator=self, tuple_size=self._tuple_size)
return (self.pair_similarity(triplets[:, :2]) -
self.pair_similarity(triplets[:, [0, 2]]))
return (self.pair_score(triplets[:, :2]) -
self.pair_score(triplets[:, [0, 2]]))

def score(self, triplets):
"""Computes score on input triplets.
@@ -872,8 +877,8 @@ def decision_function(self, quadruplets):
quadruplets = check_input(quadruplets, type_of_inputs='tuples',
preprocessor=self.preprocessor_,
estimator=self, tuple_size=self._tuple_size)
return (self.pair_similarity(quadruplets[:, :2]) -
self.pair_similarity(quadruplets[:, 2:]))
return (self.pair_score(quadruplets[:, :2]) -
self.pair_score(quadruplets[:, 2:]))

def score(self, quadruplets):
"""Computes score on input quadruplets
4 changes: 2 additions & 2 deletions test/test_base_metric.py
Original file line number Diff line number Diff line change
@@ -284,12 +284,12 @@ def test_score_pairs_warning(estimator, build_dataset):
model = clone(estimator)
set_random_state(model)

# we fit the metric learner on it and then we call score_apirs on some
# We fit the metric learner on it and then we call score_pairs on some
# points
model.fit(*remove_y(model, input_data, labels))

msg = ("score_pairs will be deprecated in release 0.7.0. "
"Use pair_similarity to compute similarities, or "
"Use pair_score to compute similarity scores, or "
"pair_distances to compute distances.")
with pytest.warns(FutureWarning) as raised_warning:
score = model.score_pairs([[X[0], X[1]], ])
20 changes: 11 additions & 9 deletions test/test_pairs_classifiers.py
Original file line number Diff line number Diff line change
@@ -49,14 +49,14 @@ def test_predict_monotonous(estimator, build_dataset,
pairs_train, pairs_test, y_train, y_test = train_test_split(input_data,
labels)
estimator.fit(pairs_train, y_train)
distances = estimator.pair_distance(pairs_test)
scores = estimator.pair_score(pairs_test)
predictions = estimator.predict(pairs_test)
min_dissimilar = np.min(distances[predictions == -1])
max_similar = np.max(distances[predictions == 1])
assert max_similar <= min_dissimilar
separator = np.mean([min_dissimilar, max_similar])
assert (predictions[distances > separator] == -1).all()
assert (predictions[distances < separator] == 1).all()
max_dissimilar = np.max(scores[predictions == -1])
min_similar = np.min(scores[predictions == 1])
assert max_dissimilar <= min_similar
separator = np.mean([max_dissimilar, min_similar])
assert (predictions[scores < separator] == -1).all()
assert (predictions[scores > separator] == 1).all()


@pytest.mark.parametrize('with_preprocessor', [True, False])
@@ -65,15 +65,17 @@ def test_predict_monotonous(estimator, build_dataset,
def test_raise_not_fitted_error_if_not_fitted(estimator, build_dataset,
with_preprocessor):
"""Test that a NotFittedError is raised if someone tries to use
pair_distance, decision_function, get_metric, transform or
pair_score, score_pairs, decision_function, get_metric, transform or
get_mahalanobis_matrix on input data and the metric learner
has not been fitted."""
input_data, labels, preprocessor, _ = build_dataset(with_preprocessor)
estimator = clone(estimator)
estimator.set_params(preprocessor=preprocessor)
set_random_state(estimator)
with pytest.raises(NotFittedError): # Remove in 0.8.0
estimator.score_pairs(input_data)
with pytest.raises(NotFittedError):
estimator.pair_distance(input_data)
estimator.pair_score(input_data)
with pytest.raises(NotFittedError):
estimator.decision_function(input_data)
with pytest.raises(NotFittedError):
11 changes: 10 additions & 1 deletion test/test_sklearn_compat.py
Original file line number Diff line number Diff line change
@@ -147,8 +147,17 @@ def test_array_like_inputs(estimator, build_dataset, with_preprocessor):

pairs = np.array([[X[0], X[1]], [X[0], X[2]]])
pairs_variants, _ = generate_array_like(pairs)
msg = ""
# Todo in 0.7.0: Change 'msg' for the message that says "This learner does
# not have pair_distance"
for pairs_variant in pairs_variants:
estimator.pair_distance(pairs_variant)
estimator.pair_score(pairs_variant) # All learners have pair_score
# But all of them will have pair_distance
with pytest.raises(Exception) as raised_exception:
estimator.pair_distance(pairs_variant)
if raised_exception is not None:
assert msg == raised_exception.value.args[0]



@pytest.mark.parametrize('with_preprocessor', [True, False])