Skip to content

Commit

Permalink
FIX: remove pandas in package import
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibault Cordier committed Jan 5, 2024
1 parent 7efe25e commit 1883099
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 87 deletions.
8 changes: 4 additions & 4 deletions examples/regression/4-tutorials/plot_ts-tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ class that block bootstraps the training set.
)
for step in range(gap, len(X_test), gap):
mapie_aci.adapt_conformal_inference(
X_test.iloc[(step - gap):step, :],
y_test.iloc[(step - gap):step],
X_test.iloc[(step - gap):step, :].to_numpy(),
y_test.iloc[(step - gap):step].to_numpy(),
gamma=0.05
)
(
Expand Down Expand Up @@ -357,8 +357,8 @@ class that block bootstraps the training set.
y_test.iloc[(step - gap):step],
)
mapie_aci.adapt_conformal_inference(
X_test.iloc[(step - gap):step, :],
y_test.iloc[(step - gap):step],
X_test.iloc[(step - gap):step, :].to_numpy(),
y_test.iloc[(step - gap):step].to_numpy(),
gamma=0.05
)
(
Expand Down
3 changes: 1 addition & 2 deletions mapie/regression/time_series_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from mapie._typing import ArrayLike, NDArray
from mapie.conformity_scores import ConformityScore
from mapie.regression import MapieRegressor
from mapie.utils import (check_alpha, check_gamma, convert_to_numpy)
from mapie.utils import (check_alpha, check_gamma)


class MapieTimeSeriesRegressor(MapieRegressor):
Expand Down Expand Up @@ -296,7 +296,6 @@ def adapt_conformal_inference(
check_is_fitted(self, self.fit_attributes)
check_gamma(gamma)
X, y = cast(NDArray, X), cast(NDArray, y)
X, y = convert_to_numpy(X, y)

self._get_alpha()
alpha = cast(Optional[NDArray], check_alpha(alpha))
Expand Down
40 changes: 2 additions & 38 deletions mapie/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Any, Optional, Tuple

import numpy as np
import pandas as pd
import pytest
from numpy.random import RandomState
from sklearn.datasets import make_regression
Expand All @@ -20,8 +19,8 @@
check_lower_upper_bounds, check_n_features_in,
check_n_jobs, check_no_agg_cv, check_null_weight,
check_number_bins, check_split_strategy,
check_verbose, compute_quantiles, convert_to_numpy,
fit_estimator, get_binning_groups)
check_verbose, compute_quantiles, fit_estimator,
get_binning_groups)


X_toy = np.array([0, 1, 2, 3, 4, 5]).reshape(-1, 1)
Expand Down Expand Up @@ -521,38 +520,3 @@ def test_check_no_agg_cv_value_error(cv: Any) -> None:
match=r"Allowed values must have the `get_n_splits` method"
):
check_no_agg_cv(X_toy, cv, array)


def test_convert_to_numpy_dataframe_and_series():
X_df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
y_series = pd.Series([7, 8, 9])
X_array, y_array = convert_to_numpy(X_df, y_series)
assert isinstance(X_array, np.ndarray)
assert isinstance(y_array, np.ndarray)
assert np.array_equal(X_array, X_df.values)
assert np.array_equal(y_array, y_series.values)


def test_convert_to_numpy_numpy_arrays():
X_array = np.array([[1, 2], [3, 4]])
y_array = np.array([5, 6])
X_result, y_result = convert_to_numpy(X_array, y_array)
assert isinstance(X_result, np.ndarray)
assert isinstance(y_result, np.ndarray)
assert np.array_equal(X_result, X_array)
assert np.array_equal(y_result, y_array)


def test_convert_to_numpy_invalid_input_df():
with pytest.raises(ValueError):
invalid_input = "Invalid Input"
convert_to_numpy(invalid_input, [1, 2, 3])


def test_convert_to_numpy_invalid_input_series():
with pytest.raises(ValueError):
invalid_input = "Invalid Input"
convert_to_numpy(
pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}),
invalid_input
)
38 changes: 0 additions & 38 deletions mapie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Any, Iterable, Optional, Tuple, Union, cast

import numpy as np
from pandas import DataFrame, Series
from sklearn.base import ClassifierMixin, RegressorMixin
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import (BaseCrossValidator, KFold, LeaveOneOut,
Expand Down Expand Up @@ -1275,43 +1274,6 @@ def check_nb_sets_sizes(sizes: NDArray, num_bins: int) -> None:
)


def convert_to_numpy(
X: DataFrame, y_true: Series
) -> Tuple[NDArray, NDArray]:
"""
Converts pandas DataFrame and Series to NumPy arrays.
Parameters
----------
X: panda.DataFrame
The input DataFrame to be converted.
y_true: panda.Series)
The input Series to be converted.
Returns
-------
Tuple[NDArray, NDArray]
A tuple containing two NumPy arrays.
The first element is the NumPy array corresponding to X,
and the second element is the NumPy array corresponding to y_true.
"""
if isinstance(X, DataFrame):
X_values = X.values
elif isinstance(X, np.ndarray):
X_values = X
else:
raise ValueError("X must be a pandas DataFrame or a NumPy array")

if isinstance(y_true, Series):
y_true_values = y_true.values
elif isinstance(y_true, np.ndarray):
y_true_values = y_true
else:
raise ValueError("y_true must be a pandas Series or a NumPy array")

return X_values, y_true_values


def check_array_nan(array: NDArray) -> None:
"""
Checks if the array have only NaN values. If it has we throw an error.
Expand Down
10 changes: 5 additions & 5 deletions notebooks/regression/ts-changepoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@
" y_test.iloc[(step - gap):step],\n",
" )\n",
" mapie_aci.adapt_conformal_inference(\n",
" X_test.iloc[(step - gap):step, :],\n",
" y_test.iloc[(step - gap):step],\n",
" X_test.iloc[(step - gap):step, :].to_numpy(),\n",
" y_test.iloc[(step - gap):step].to_numpy(),\n",
" gamma = 0.05\n",
" )\n",
" (\n",
Expand Down Expand Up @@ -775,8 +775,8 @@
" y_test.iloc[(step - gap):step],\n",
" )\n",
" mapie_aci.adapt_conformal_inference(\n",
" X_test.iloc[(step - gap):step, :],\n",
" y_test.iloc[(step - gap):step],\n",
" X_test.iloc[(step - gap):step, :].to_numpy(),\n",
" y_test.iloc[(step - gap):step].to_numpy(),\n",
" gamma = 0.05\n",
" )\n",
" (\n",
Expand Down Expand Up @@ -997,7 +997,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 1883099

Please sign in to comment.