Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pearsonify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "1.0.1"

from .wrapper import Pearsonify
from .wrapper import Pearsonify
18 changes: 14 additions & 4 deletions pearsonify/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from sklearn.base import BaseEstimator
from sklearn.utils.validation import NotFittedError, check_is_fitted

from .utils import (
compute_pearson_residuals,
compute_confidence_intervals,
calculate_coverage,
compute_confidence_intervals,
compute_pearson_residuals,
)


Expand All @@ -24,7 +26,15 @@ def __init__(self, estimator: BaseEstimator, alpha=0.05):
def fit(self, X_train, y_train, X_cal, y_cal):
"""Fit the model and compute Pearson residual-based quantile from calibration data."""
# Train the model if it's not already fitted
self.estimator.fit(X_train, y_train)
try:
check_is_fitted(self.estimator)
if not callable(getattr(self.estimator, "predict_proba", None)):
raise TypeError("The estimator must have a callable 'predict_proba' method.")
except TypeError as e:
raise TypeError(f"Estimator validation failed: {e}") from e
Comment on lines +33 to +34
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Narrow or simplify the TypeError wrapping to avoid redundant exception handling.

This except TypeError will also catch the TypeError you raise for a missing predict_proba, only to re-wrap it with a slightly changed message, and it may also hide unrelated TypeErrors from inside check_is_fitted or the estimator. Consider either validating via explicit checks and not catching TypeError at all, or using/narrowing to a custom exception type you control for your own validation failure.

except NotFittedError:
# Attempt to fit the estimator if not already fitted
self.estimator.fit(X_train, y_train)

# Compute residuals on calibration set
y_cal_pred_proba = self.estimator.predict_proba(X_cal)[:, 1]
Expand Down