-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typing and warnings as much as we can
- Loading branch information
Showing
39 changed files
with
454 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,13 @@ | |
# Sami Hamdan <[email protected]> | ||
# License: AGPL | ||
|
||
from typing import Dict, Iterable, List, Optional, Union | ||
from typing import Dict, List, Optional, Union | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import sklearn | ||
from sklearn.base import BaseEstimator | ||
from sklearn.model_selection import ( | ||
BaseCrossValidator, | ||
check_cv, | ||
cross_validate, | ||
) | ||
|
@@ -23,6 +23,7 @@ | |
from .prepare import check_consistency, prepare_input_data | ||
from .scoring import check_scoring | ||
from .utils import _compute_cvmdsum, logger, raise_error | ||
from .utils.typing import CVLike | ||
|
||
|
||
def run_cross_validation( # noqa: C901 | ||
|
@@ -36,7 +37,7 @@ def run_cross_validation( # noqa: C901 | |
return_estimator: Optional[str] = None, | ||
return_inspector: bool = False, | ||
return_train_score: bool = False, | ||
cv: Optional[Union[int, BaseCrossValidator, Iterable]] = None, | ||
cv: Optional[CVLike] = None, | ||
groups: Optional[str] = None, | ||
scoring: Union[str, List[str], None] = None, | ||
pos_labels: Union[str, List[str], None] = None, | ||
|
@@ -357,20 +358,32 @@ def run_cross_validation( # noqa: C901 | |
|
||
# Prepare cross validation | ||
cv_outer = check_cv( | ||
cv, classifier=problem_type == "classification" # type: ignore | ||
cv, # type: ignore | ||
classifier=problem_type == "classification", | ||
) | ||
logger.info(f"Using outer CV scheme {cv_outer}") | ||
|
||
check_consistency(df_y, cv, groups, problem_type) # type: ignore | ||
|
||
cv_return_estimator = return_estimator in ["cv", "all"] | ||
scoring = check_scoring(pipeline, scoring, wrap_score=wrap_score) | ||
scoring = check_scoring( | ||
pipeline, # type: ignore | ||
scoring, | ||
wrap_score=wrap_score, | ||
) | ||
|
||
cv_mdsum = _compute_cvmdsum(cv_outer) | ||
fit_params = {} | ||
if df_groups is not None: | ||
if isinstance(pipeline, BaseSearchCV): | ||
fit_params["groups"] = df_groups.values | ||
|
||
_sklearn_deprec_fit_params = {} | ||
if sklearn.__version__ >= "1.4.0": | ||
_sklearn_deprec_fit_params["params"] = fit_params | ||
else: | ||
_sklearn_deprec_fit_params["fit_params"] = fit_params | ||
|
||
scores = cross_validate( | ||
pipeline, | ||
df_X, | ||
|
@@ -382,7 +395,7 @@ def run_cross_validation( # noqa: C901 | |
n_jobs=n_jobs, | ||
return_train_score=return_train_score, | ||
verbose=verbose, # type: ignore | ||
fit_params=fit_params, | ||
**_sklearn_deprec_fit_params, | ||
) | ||
|
||
n_repeats = getattr(cv_outer, "n_repeats", 1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,14 @@ | |
# Sami Hamdan <[email protected]> | ||
# License: AGPL | ||
|
||
from typing import List, Optional, Union | ||
from typing import Optional, Union | ||
|
||
import pandas as pd | ||
from sklearn.model_selection import BaseCrossValidator, check_cv | ||
from sklearn.utils.metaestimators import available_if | ||
|
||
from ..utils import _compute_cvmdsum, is_nonoverlapping_cv, raise_error | ||
from ..utils.typing import DataLike | ||
from ._pipeline import PipelineInspector | ||
|
||
|
||
|
@@ -60,14 +61,13 @@ class FoldsInspector: | |
def __init__( | ||
self, | ||
scores: pd.DataFrame, | ||
cv: BaseCrossValidator, | ||
X: Union[str, List[str]], # noqa: N803 | ||
y: str, | ||
cv: Union[BaseCrossValidator, int], | ||
X: DataLike, # noqa: N803 | ||
y: pd.Series, | ||
func: str = "predict", | ||
groups: Optional[str] = None, | ||
groups: Optional[pd.Series] = None, | ||
): | ||
self._scores = scores | ||
self._cv = cv | ||
self._X = X | ||
self._y = y | ||
self._func = func | ||
|
@@ -92,7 +92,7 @@ def __init__( | |
) | ||
|
||
cv = check_cv(cv) | ||
|
||
self._cv = cv | ||
t_cv_mdsum = _compute_cvmdsum(cv) | ||
if t_cv_mdsum != cv_mdsums[0]: | ||
raise_error( | ||
|
@@ -120,10 +120,16 @@ def _get_predictions(self, func): | |
|
||
predictions = [] | ||
for i_fold, (_, test) in enumerate( | ||
self._cv.split(self._X, self._y, groups=self._groups) | ||
self._cv.split( | ||
self._X, # type: ignore | ||
self._y, | ||
groups=self._groups, | ||
) | ||
): | ||
t_model = self._scores["estimator"][i_fold] | ||
t_values = getattr(t_model, func)(self._X.iloc[test]) | ||
t_values = getattr(t_model, func)( | ||
self._X.iloc[test] # type: ignore | ||
) | ||
if t_values.ndim == 1: | ||
t_values = t_values[:, None] | ||
column_names = [f"p{i}" for i in range(t_values.shape[1])] | ||
|
@@ -152,7 +158,7 @@ def _get_predictions(self, func): | |
t_df.columns = [f"fold{i_fold}_{x}" for x in t_df.columns] | ||
predictions = pd.concat(predictions, axis=1) | ||
predictions = predictions.sort_index() | ||
predictions["target"] = self._y.values | ||
predictions["target"] = self._y.values # type: ignore | ||
return predictions | ||
|
||
def __getitem__(self, key): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
# Authors: Federico Raimondo <[email protected]> | ||
# Sami Hamdan <[email protected]> | ||
# License: AGPL | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
@@ -70,7 +69,10 @@ def scores(df_typed_iris, n_iters=5, mock_model=None): | |
if mock_model is None: | ||
mock_model = MockModelReturnsIndex | ||
|
||
estimators = [WrapModel(mock_model()).fit(X, y) for _ in range(n_iters)] | ||
estimators = [ | ||
WrapModel(mock_model()).fit(X, y) # type: ignore | ||
for _ in range(n_iters) | ||
] | ||
|
||
return pd.DataFrame( | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.