Skip to content

Commit 09628f3

Browse files
FIX: clean code to lint and type-check errors
1 parent c332d27 commit 09628f3

File tree

13 files changed

+104
-231
lines changed

13 files changed

+104
-231
lines changed

mapie/classification.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
from sklearn.utils.validation import (_check_y, _num_samples, check_is_fitted,
1414
indexable)
1515

16-
from ._machine_precision import EPSILON
17-
from ._typing import ArrayLike, NDArray
18-
from .estimator.classification.estimator import EnsembleClassifier
19-
from .metrics import classification_mean_width_score
20-
from .utils import (check_alpha, check_alpha_and_n_samples, check_cv,
21-
check_estimator_classification, check_n_features_in,
22-
check_n_jobs, check_null_weight, check_verbose,
23-
compute_quantiles)
24-
from .conformity_scores.utils_classification_conformity_scores import (
16+
from mapie._machine_precision import EPSILON
17+
from mapie._typing import ArrayLike, NDArray
18+
from mapie.estimator import EnsembleClassifier
19+
from mapie.metrics import classification_mean_width_score
20+
from mapie.utils import (check_alpha, check_alpha_and_n_samples, check_cv,
21+
check_estimator_classification, check_n_features_in,
22+
check_n_jobs, check_null_weight, check_verbose,
23+
compute_quantiles)
24+
from mapie.conformity_scores.utils import (
2525
get_true_label_position
2626
)
2727

@@ -988,9 +988,9 @@ def _split_data(
988988
sample_weight,
989989
groups,
990990
size_raps
991-
) -> Tuple[ArrayLike, ArrayLike, ArrayLike, ArrayLike, NDArray, ArrayLike]:
992-
991+
):
993992
"""Split data for raps method
993+
994994
Parameters
995995
----------
996996
X: ArrayLike
@@ -1013,15 +1013,15 @@ def _split_data(
10131013
10141014
Returns
10151015
-------
1016-
Tuple[ArrayLike, ArrayLike, ArrayLike, NDArray, Optional[NDArray],
1017-
Optional[ArrayLike]]
1016+
Tuple[NDArray, NDArray, NDArray, NDArray, Optional[NDArray],
1017+
Optional[NDArray]]
10181018
1019-
- ArrayLike of shape (n_samples, n_features)
1020-
- ArrayLike of shape (n_samples,)
1021-
- ArrayLike of shape (n_samples,)
1022-
- ArrayLike of shape (n_samples,)
1019+
- NDArray of shape (n_samples, n_features)
1020+
- NDArray of shape (n_samples,)
1021+
- NDArray of shape (n_samples,)
1022+
- NDArray of shape (n_samples,)
1023+
- NDArray of shape (n_samples,)
10231024
- NDArray of shape (n_samples,)
1024-
- ArrayLike of shape (n_samples,)
10251025
"""
10261026
raps_split = ShuffleSplit(
10271027
1, test_size=size_raps, random_state=self.random_state
@@ -1096,15 +1096,25 @@ def fit(
10961096
The model itself.
10971097
"""
10981098
# Checks
1099-
(estimator, cv, X, y, y_enc, sample_weight, groups, n_samples) = (
1100-
self._check_fit_parameter(X, y, sample_weight, groups)
1101-
)
1099+
(estimator,
1100+
cv,
1101+
X,
1102+
y,
1103+
y_enc,
1104+
sample_weight,
1105+
groups,
1106+
n_samples) = self._check_fit_parameter(X, y, sample_weight, groups)
11021107

11031108
if self.method == "raps":
11041109
(X, y_enc, y, n_samples, sample_weight, groups) = self._split_data(
11051110
X, y_enc, sample_weight, groups, size_raps
11061111
)
11071112

1113+
# Cast
1114+
X, y_enc, y = cast(NDArray, X), cast(NDArray, y_enc), cast(NDArray, y)
1115+
sample_weight = cast(NDArray, sample_weight)
1116+
groups = cast(NDArray, groups)
1117+
11081118
# Work
11091119
self.estimator_ = EnsembleClassifier(
11101120
estimator,
@@ -1117,7 +1127,7 @@ def fit(
11171127
)
11181128

11191129
self.estimator_ = self.estimator_.fit(
1120-
X, y, y_enc, sample_weight, groups,
1130+
X, y, y_enc=y_enc, sample_weight=sample_weight, groups=groups,
11211131
**fit_params
11221132
)
11231133

mapie/conformity_scores/conformity_scores.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from mapie._compatibility import np_nanquantile
77
from mapie._typing import ArrayLike, NDArray
8-
from mapie.estimator.regression.interface import EnsembleEstimator
8+
from mapie.estimator import EnsembleRegressor
99

1010

1111
class ConformityScore(metaclass=ABCMeta):
@@ -317,7 +317,7 @@ def _beta_optimize(
317317
def get_bounds(
318318
self,
319319
X: ArrayLike,
320-
estimator: EnsembleEstimator,
320+
estimator: EnsembleRegressor,
321321
conformity_scores: NDArray,
322322
alpha_np: NDArray,
323323
ensemble: bool = False,

mapie/estimator/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .interface import EnsembleEstimator
2+
from .regressor import EnsembleRegressor
3+
from .classifier import EnsembleClassifier
4+
5+
__all__ = [
6+
"EnsembleEstimator",
7+
"EnsembleRegressor",
8+
"EnsembleClassifier",
9+
]

mapie/estimator/classification/__init__.py

Whitespace-only changes.

mapie/estimator/classification/interface.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

mapie/estimator/classification/estimator.py renamed to mapie/estimator/classifier.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sklearn.utils.validation import _num_samples, check_is_fitted
1111

1212
from mapie._typing import ArrayLike, NDArray
13-
from mapie.estimator.classification.interface import EnsembleEstimator
13+
from mapie.estimator.interface import EnsembleEstimator
1414
from mapie.utils import (
1515
check_no_agg_cv,
1616
fit_estimator,
@@ -294,7 +294,7 @@ def fit(
294294
self,
295295
X: ArrayLike,
296296
y: ArrayLike,
297-
y_enc: ArrayLike,
297+
y_enc: Optional[ArrayLike] = None,
298298
sample_weight: Optional[ArrayLike] = None,
299299
groups: Optional[ArrayLike] = None,
300300
**fit_params,
@@ -341,6 +341,9 @@ def fit(
341341
self.use_split_method_ = check_no_agg_cv(X, self.cv, self.no_agg_cv_)
342342
estimator = self.estimator
343343
n_samples = _num_samples(y)
344+
if y_enc is None:
345+
raise ValueError
346+
y_enc = cast(NDArray, y_enc)
344347

345348
# Computation
346349
if cv == "prefit":
@@ -378,25 +381,26 @@ def fit(
378381

379382
def predict_proba_calib(
380383
self,
381-
X: ArrayLike,
382-
y: ArrayLike,
383-
y_enc: ArrayLike,
384-
groups: Optional[ArrayLike] = None,
385-
) -> Tuple[NDArray, ArrayLike, ArrayLike]:
384+
X: NDArray,
385+
y: NDArray,
386+
y_enc: NDArray,
387+
groups: Optional[NDArray] = None,
388+
**predict_params
389+
) -> Tuple[NDArray, NDArray, NDArray]:
386390
"""
387391
Perform predictions on X : the calibration set.
388392
389393
Parameters
390394
----------
391-
X: ArrayLike of shape (n_samples_test, n_features)
395+
X: NDArray of shape (n_samples_test, n_features)
392396
Input data
393397
394-
y: Optional[ArrayLike] of shape (n_samples_test,)
398+
y: Optional[NDArray] of shape (n_samples_test,)
395399
Input labels.
396400
397401
By default ``None``.
398402
399-
groups: Optional[ArrayLike] of shape (n_samples_test,)
403+
groups: Optional[NDArray] of shape (n_samples_test,)
400404
Group labels for the samples used while splitting the dataset into
401405
train/test set.
402406
@@ -439,7 +443,7 @@ def predict_proba_calib(
439443
# are not used during calibration
440444
self.k_ = self.k_[val_indices]
441445
y_pred_proba = y_pred_proba[val_indices]
442-
# y_enc = y_enc[val_indices]
446+
y_enc = y_enc[val_indices]
443447
y = cast(NDArray, y)[val_indices]
444448

445449
return y_pred_proba, y, y_enc
@@ -448,7 +452,8 @@ def predict(
448452
self,
449453
X: ArrayLike,
450454
alpha_np: ArrayLike = [],
451-
agg_scores: Any = None
455+
agg_scores: Any = None,
456+
**predict_params
452457
) -> NDArray:
453458
"""
454459
Predict target from X. It also computes the prediction per train sample

mapie/estimator/interface.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from __future__ import annotations
2+
3+
from abc import ABCMeta, abstractmethod
4+
5+
from mapie._typing import ArrayLike
6+
7+
8+
class EnsembleEstimator(metaclass=ABCMeta):
9+
"""
10+
This class implements methods to handle the training and usage of the
11+
estimator. This estimator can be unique or composed by cross validated
12+
estimators.
13+
"""
14+
15+
@abstractmethod
16+
def fit(
17+
self,
18+
X: ArrayLike,
19+
y: ArrayLike,
20+
**kwargs
21+
) -> EnsembleEstimator:
22+
"""
23+
Fit the base estimator under the ``single_estimator_`` attribute.
24+
Fit all cross-validated estimator clones
25+
and rearrange them into a list, the ``estimators_`` attribute.
26+
Out-of-fold conformity scores are stored under
27+
the ``conformity_scores_`` attribute.
28+
"""
29+
30+
@abstractmethod
31+
def predict(
32+
self,
33+
X: ArrayLike,
34+
**kwargs
35+
):
36+
"""
37+
Predict target from X. It also computes the prediction per train sample
38+
for each test sample according to ``self.method``.
39+
"""

mapie/estimator/regression/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)