Skip to content

Commit 5bbe1ee

Browse files
author
Vincent Blot
committed
ENH : remove image_input argument add modify tests accordingly
1 parent 244ba08 commit 5bbe1ee

File tree

3 files changed

+4
-95
lines changed

3 files changed

+4
-95
lines changed

mapie/classification.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
check_alpha_and_n_samples,
2828
check_n_jobs,
2929
check_verbose,
30-
check_input_is_image,
3130
fit_estimator
3231
)
3332

@@ -254,13 +253,8 @@ def _check_estimator(
254253
If the estimator is not fitted and ``cv`` attribute is "prefit".
255254
"""
256255
if estimator is None:
257-
if not self.image_input:
258-
return LogisticRegression(multi_class="multinomial").fit(X, y)
259-
else:
260-
raise ValueError(
261-
"Default LogisticRegression's input can't be an image."
262-
"Please provide a proper model."
263-
)
256+
return LogisticRegression(multi_class="multinomial").fit(X, y)
257+
264258
if isinstance(estimator, Pipeline):
265259
est = estimator[-1]
266260
else:
@@ -639,7 +633,6 @@ def fit(
639633
self,
640634
X: ArrayLike,
641635
y: ArrayLike,
642-
image_input: Optional[bool] = False,
643636
sample_weight: Optional[ArrayLike] = None,
644637
) -> MapieClassifier:
645638
"""
@@ -653,13 +646,6 @@ def fit(
653646
y : ArrayLike of shape (n_samples,)
654647
Training labels.
655648
656-
image_input: Optional[bool] = False
657-
Whether or not the X input is an image. If True, you must provide
658-
a model that accepts image as input (e.g., a Neural Network). All
659-
Scikit-learn classifiers only accept two-dimensional inputs.
660-
661-
By default False.
662-
663649
sample_weight : Optional[ArrayLike] of shape (n_samples,)
664650
Sample weights for fitting the out-of-fold models.
665651
If None, then samples are equally weighted.
@@ -675,12 +661,10 @@ def fit(
675661
The model itself.
676662
"""
677663
# Checks
678-
self.image_input = image_input
679664
self._check_parameters()
680665
cv = check_cv(self.cv)
681666
estimator = self._check_estimator(X, y, self.estimator)
682-
if self.image_input:
683-
check_input_is_image(X)
667+
684668
X, y = indexable(X, y)
685669
y = _check_y(y)
686670
assert type_of_target(y) == "multiclass"
@@ -844,8 +828,6 @@ def predict(
844828
include_last_label = self._check_include_last_label(include_last_label)
845829
alpha_ = check_alpha(alpha)
846830
check_is_fitted(self, self.fit_attributes)
847-
if self.image_input:
848-
check_input_is_image(X)
849831

850832
# Estimate prediction sets
851833
y_pred = self.single_estimator_.predict(X)

mapie/tests/test_classification.py

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,6 @@
415415
}
416416
]
417417

418-
X_WRONG_IMAGE = [
419-
np.zeros((3, 1024, 1024, 3, 1)),
420-
np.zeros((3, 512))
421-
]
422418
X_good_image = np.zeros((3, 1024, 1024, 3))
423419
y_toy_image = np.array([0, 0, 1])
424420

@@ -826,7 +822,7 @@ def test_image_cumulated_scores(X: Dict[str, ArrayLike]) -> None:
826822
cv="prefit",
827823
random_state=42
828824
)
829-
mapie.fit(cumclf.X_calib, cumclf.y_calib, image_input=True)
825+
mapie.fit(cumclf.X_calib, cumclf.y_calib)
830826
np.testing.assert_allclose(mapie.conformity_scores_, cumclf.y_calib_scores)
831827
# predict
832828
_, y_ps = mapie.predict(
@@ -893,51 +889,6 @@ def test_classifier_without_classes_attribute(
893889
mapie.fit(X_toy, y_toy)
894890

895891

896-
@pytest.mark.parametrize("X_wrong_image", X_WRONG_IMAGE)
897-
def test_wrong_image_shape_fit(X_wrong_image: ArrayLike) -> None:
898-
"""
899-
Test that ValueError is raised if image has not 3 or 4 dimensions in fit.
900-
"""
901-
cumclf = ImageClassifier(X_wrong_image, y_toy_image)
902-
cumclf.fit(cumclf.X_calib, cumclf.y_calib)
903-
mapie = MapieClassifier(
904-
cumclf,
905-
method="cumulated_score",
906-
cv="prefit",
907-
random_state=42
908-
)
909-
with pytest.raises(ValueError, match=r"Invalid X.*"):
910-
mapie.fit(cumclf.X_calib, cumclf.y_calib, image_input=True)
911-
912-
913-
@pytest.mark.parametrize("X_wrong_image", X_WRONG_IMAGE)
914-
def test_wrong_image_shape_predict(X_wrong_image: ArrayLike) -> None:
915-
"""
916-
Test that ValueError is raised if image has not
917-
3 or 4 dimensions in predict.
918-
"""
919-
cumclf = ImageClassifier(X_good_image, y_toy_image)
920-
cumclf.fit(cumclf.X_calib, cumclf.y_calib)
921-
mapie = MapieClassifier(
922-
cumclf,
923-
method="cumulated_score",
924-
cv="prefit",
925-
random_state=42
926-
)
927-
mapie.fit(cumclf.X_calib, cumclf.y_calib, image_input=True,)
928-
with pytest.raises(ValueError, match=r"Invalid X.*"):
929-
mapie.predict(X_wrong_image)
930-
931-
932-
def test_undefined_model() -> None:
933-
"""
934-
Test ValueError is raised if no model is specified with image input.
935-
"""
936-
mapie = MapieClassifier()
937-
with pytest.raises(ValueError, match=r"LogisticRegression's input.*"):
938-
mapie.fit(X_good_image, y_toy_image, image_input=True,)
939-
940-
941892
@pytest.mark.parametrize("method", WRONG_METHODS)
942893
def test_method_error_in_fit(monkeypatch: Any, method: str) -> None:
943894
"""Test else condition for the method in .fit"""

mapie/utils.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -397,27 +397,3 @@ def check_nan_in_aposteriori_prediction(X: ArrayLike) -> None:
397397
+ "belongs to every resamplings.\n"
398398
"Increase the number of resamplings"
399399
)
400-
401-
402-
def check_input_is_image(X: ArrayLike) -> None:
403-
"""
404-
Check if the image has 3 or 4 dimensions
405-
406-
Parameters
407-
----------
408-
X: Union[
409-
ArrayLike[n_samples, width, height],
410-
ArrayLike[n_samples, width, height, n_channels]
411-
]
412-
Image input
413-
414-
Raises
415-
------
416-
ValueError
417-
"""
418-
if len(X.shape) not in [3, 4]:
419-
raise ValueError(
420-
"Invalid X."
421-
"When X is an image, the number of dimensions"
422-
"must be equal to 3 or 4."
423-
)

0 commit comments

Comments
 (0)