Skip to content

Commit

Permalink
UPD: dosctrings + beautify
Browse files Browse the repository at this point in the history
  • Loading branch information
Candice Moyet committed Jul 10, 2023
1 parent 1dbf7c6 commit 5cc17fe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
54 changes: 27 additions & 27 deletions mapie/conformity_scores/conformity_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ def get_signed_conformity_scores(
Parameters
----------
X: ArrayLike of shape (n_samples_calib, n_features)
X: ArrayLike of shape (n_samples, n_features)
Observed feature values.
y: ArrayLike of shape (n_samples_calib,)
y: ArrayLike of shape (n_samples,)
Observed target values.
y_pred: ArrayLike of shape (n_samples_calib,)
y_pred: ArrayLike of shape (n_samples,)
Predicted target values.
Returns
-------
NDArray of shape (n_samples_calib,)
NDArray of shape (n_samples,)
Signed conformity scores.
"""

Expand All @@ -101,24 +101,24 @@ def get_estimation_distribution(
Parameters
----------
X: ArrayLike of shape (n_samples_calib, n_features)
X: ArrayLike of shape (n_samples, n_features)
Observed feature values.
y_pred: ArrayLike
The shape is either (n_samples_calib, n_samples_train): when the
The shape is either (n_samples, n_references): when the
method is called in ``get_bounds`` it needs a prediction per train
sample for each test sample to compute the bounds.
Or (n_samples_calib, 1): when it is called in ``check_consistency``
Or (n_samples,): when it is called in ``check_consistency``
conformity_scores: ArrayLike
The shape is either (n_samples_calib, n_alpha) when it is the
conformity scores themselves or (n_alpha, 1) when it is only the
The shape is either (n_samples, 1) when it is the
conformity scores themselves or (1, n_alpha) when it is only the
quantile of the conformity scores.
Returns
-------
NDArray of shape (n_samples_calib, n_alpha) or
(n_samples_calib, n_samples_train) according to the shape of ``y_pred``
NDArray of shape (n_samples, n_alpha) or
(n_samples, n_references) according to the shape of ``y_pred``
Observed values.
"""

Expand All @@ -142,16 +142,16 @@ def check_consistency(
Parameters
----------
X: ArrayLike of shape (n_samples_calib, n_features)
X: ArrayLike of shape (n_samples, n_features)
Observed feature values.
y: ArrayLike of shape (n_samples_calib,)
y: ArrayLike of shape (n_samples,)
Observed target values.
y_pred: ArrayLike of shape (n_samples_calib,)
y_pred: ArrayLike of shape (n_samples,)
Predicted target values.
conformity_scores: ArrayLike of shape (n_samples_calib,)
conformity_scores: ArrayLike of shape (n_samples,)
Conformity scores.
Raises
Expand Down Expand Up @@ -188,18 +188,18 @@ def get_conformity_scores(
Parameters
----------
X: NDArray of shape (n_samples_calib, n_features)
X: NDArray of shape (n_samples, n_features)
Observed feature values.
y: NDArray of shape (n_samples_calib,)
y: NDArray of shape (n_samples,)
Observed target values.
y_pred: NDArray of shape (n_samples_calib,)
y_pred: NDArray of shape (n_samples,)
Predicted target values.
Returns
-------
NDArray of shape (n_samples_calib, 1)
NDArray of shape (n_samples,)
Conformity scores.
"""
conformity_scores = self.get_signed_conformity_scores(X, y, y_pred)
Expand All @@ -222,8 +222,8 @@ def get_quantile(
Parameters
----------
values: NDArray of shape (n_samples_calib, n_alpha) or
(n_samples_calib, n_samples_train)
values: NDArray of shape (n_samples,) or
(n_samples, n_references)
Values from which the quantile is computed, it can be the
conformity scores or the conformity scores aggregated with
the predictions.
Expand All @@ -240,7 +240,7 @@ def get_quantile(
Returns
-------
NDArray of shape (n_alpha,)
NDArray of shape (1, n_alpha) or (n_samples, n_alpha)
The quantile of the conformity scores.
"""
quantile = np.column_stack([
Expand Down Expand Up @@ -269,13 +269,13 @@ def get_bounds(
Parameters
----------
X: ArrayLike of shape (n_samples_test, n_features)
X: ArrayLike of shape (n_samples, n_features)
Observed feature values.
estimator: EnsembleEstimator
Estimator that is fitted to predict y from X.
conformity_scores: ArrayLike of shape (n_samples_calib,)
conformity_scores: ArrayLike of shape (n_samples,)
Conformity scores.
alpha_np: NDArray of shape (n_alpha,)
Expand All @@ -295,11 +295,11 @@ def get_bounds(
Returns
-------
Tuple[NDArray, NDArray, NDArray]
- The predictions itself. (y_pred) of shape (n_samples_test,).
- The predictions itself. (y_pred) of shape (n_samples,).
- The lower bounds of the prediction intervals of shape
(n_samples_test,).
(n_samples, n_alpha).
- The upper bounds of the prediction intervals of shape
(n_samples_test,).
(n_samples, n_alpha).
"""
y_pred, y_pred_low, y_pred_up = estimator.predict(X, ensemble)
signed = -1 if self.sym else 1
Expand Down
8 changes: 4 additions & 4 deletions mapie/conformity_scores/residual_conformity_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def get_signed_conformity_scores(
y_pred: ArrayLike,
) -> NDArray:
"""
Compute the signed conformity scores from the observed values
and the predicted ones, from the following formula:
Compute the signed conformity scores from the predicted values
and the observed ones, from the following formula:
signed conformity score = y - y_pred
"""
return np.subtract(y, y_pred)
Expand All @@ -43,7 +43,7 @@ def get_estimation_distribution(
) -> NDArray:
"""
Compute samples of the estimation distribution from the predicted
targets and ``conformity_scores``, from the following formula:
values and the conformity scores, from the following formula:
signed conformity score = y - y_pred
<=> y = y_pred + signed conformity score
Expand Down Expand Up @@ -123,7 +123,7 @@ def get_estimation_distribution(
) -> NDArray:
"""
Compute samples of the estimation distribution from the predicted
targets and ``conformity_scores``, from the following formula:
values and the conformity scores, from the following formula:
signed conformity score = (y - y_pred) / y_pred
<=> y = y_pred * (1 + signed conformity score)
Expand Down
37 changes: 22 additions & 15 deletions mapie/tests/test_conformity_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ def get_signed_conformity_scores(
) -> NDArray:
return np.subtract(y, y_pred)

def get_estimation_distribution(self, X: ArrayLike, y_pred: ArrayLike,
conformity_scores: ArrayLike) -> NDArray:
def get_estimation_distribution(
self, X: ArrayLike, y_pred: ArrayLike, conformity_scores: ArrayLike
) -> NDArray:
"""
A positive constant is added to the sum between predictions and
conformity scores to make the estimated distribution inconsistent
Expand Down Expand Up @@ -65,8 +66,9 @@ def test_absolute_conformity_score_get_estimation_distribution(
) -> None:
"""Test conformity observed value computation for AbsoluteConformityScore.""" # noqa: E501
abs_conf_score = AbsoluteConformityScore()
y_obs = abs_conf_score.get_estimation_distribution(X_toy, y_pred,
conf_scores)
y_obs = abs_conf_score.get_estimation_distribution(
X_toy, y_pred, conf_scores
)
np.testing.assert_allclose(y_obs, y_toy)


Expand All @@ -77,8 +79,9 @@ def test_absolute_conformity_score_consistency(y_pred: NDArray) -> None:
signed_conf_scores = abs_conf_score.get_signed_conformity_scores(
X_toy, y_toy, y_pred
)
y_obs = abs_conf_score.get_estimation_distribution(X_toy, y_pred,
signed_conf_scores)
y_obs = abs_conf_score.get_estimation_distribution(
X_toy, y_pred, signed_conf_scores
)
np.testing.assert_allclose(y_obs, y_toy)


Expand All @@ -89,7 +92,8 @@ def test_gamma_conformity_score_get_conformity_scores(
"""Test conformity score computation for GammaConformityScore."""
gamma_conf_score = GammaConformityScore()
conf_scores = gamma_conf_score.get_conformity_scores(
X_toy, y_toy, y_pred)
X_toy, y_toy, y_pred
)
expected_signed_conf_scores = np.array(conf_scores_gamma_list)
np.testing.assert_allclose(conf_scores, expected_signed_conf_scores)

Expand All @@ -107,8 +111,9 @@ def test_gamma_conformity_score_get_estimation_distribution(
) -> None:
"""Test conformity observed value computation for GammaConformityScore.""" # noqa: E501
gamma_conf_score = GammaConformityScore()
y_obs = gamma_conf_score.get_estimation_distribution(X_toy, y_pred,
conf_scores)
y_obs = gamma_conf_score.get_estimation_distribution(
X_toy, y_pred, conf_scores
)
np.testing.assert_allclose(y_obs, y_toy)


Expand All @@ -119,8 +124,9 @@ def test_gamma_conformity_score_consistency(y_pred: NDArray) -> None:
signed_conf_scores = gamma_conf_score.get_signed_conformity_scores(
X_toy, y_toy, y_pred
)
y_obs = gamma_conf_score.get_estimation_distribution(X_toy, y_pred,
signed_conf_scores)
y_obs = gamma_conf_score.get_estimation_distribution(
X_toy, y_pred, signed_conf_scores
)
np.testing.assert_allclose(y_obs, y_toy)


Expand Down Expand Up @@ -183,14 +189,15 @@ def test_gamma_conformity_score_check_predicted_value(
ValueError,
match=r".*At least one of the predicted target is negative.*"
):
gamma_conf_score.get_estimation_distribution(X_toy, y_pred,
conf_scores)
gamma_conf_score.get_estimation_distribution(
X_toy, y_pred, conf_scores
)


def test_check_consistency() -> None:
"""
Test that a dummy ConformityScore class that gives inconsistent
conformity scores and distributions raises an error.
Test that a dummy ConformityScore class that gives inconsistent scores
and distributions raises an error.
"""
dummy_conf_score = DummyConformityScore()
conformity_scores = dummy_conf_score.get_signed_conformity_scores(
Expand Down

0 comments on commit 5cc17fe

Please sign in to comment.