Skip to content
Closed
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
30 changes: 15 additions & 15 deletions src/multicalibration/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _set_lightgbm_params(self, lightgbm_params: dict[str, Any] | None) -> None:
logger.warning(
"Model has already been fit. To avoid inconsistent state all training state will be reset after setting lightgbm_params."
)
self.reset_training_state()
self._reset_training_state()
except AttributeError:
pass

Expand Down Expand Up @@ -385,7 +385,7 @@ def feature_importance(self) -> pd.DataFrame:
}
).sort_values("importance", ascending=False)

def reset_training_state(self) -> None:
def _reset_training_state(self) -> None:
self.mr = []
self.unshrink_factors = []
self.mce_below_initial = None
Expand All @@ -396,7 +396,7 @@ def reset_training_state(self) -> None:
self.numerical_feature_names = None

@property
def mce_is_satisfactory(self) -> bool | None:
def _mce_is_satisfactory(self) -> bool | None:
return self.mce_below_initial and self.mce_below_strong_evidence_threshold

@property
Expand Down Expand Up @@ -470,7 +470,7 @@ def _preprocess_input_data(
logger.info(
f"Preprocessing input data with {len(df)} rows; in_fit_phase = {is_fit_phase}"
)
x = self.extract_features(
x = self._extract_features(
df=df,
categorical_feature_column_names=categorical_feature_column_names,
numerical_feature_column_names=numerical_feature_column_names,
Expand Down Expand Up @@ -525,7 +525,7 @@ def fit(
numerical_feature_column_names,
)

self.reset_training_state()
self._reset_training_state()

# Store feature names to be used in feature importance later
self.categorical_feature_names = categorical_feature_column_names or []
Expand Down Expand Up @@ -617,7 +617,7 @@ def _fit_single_round(

self.mr.append(
lgb.train(
params=self.get_lgbm_params(x),
params=self._get_lgbm_params(x),
train_set=lgb.Dataset(
x,
label=y,
Expand Down Expand Up @@ -749,7 +749,7 @@ def _predict(

return predictions_per_round if return_all_rounds else predictions_per_round[-1]

def get_lgbm_params(self, x: npt.NDArray) -> dict[str, Any]:
def _get_lgbm_params(self, x: npt.NDArray) -> dict[str, Any]:
lgb_params = self.lightgbm_params.copy()
if self.MONOTONE_T:
score_constraint = [1]
Expand All @@ -759,7 +759,7 @@ def get_lgbm_params(self, x: npt.NDArray) -> dict[str, Any]:
)
return lgb_params

def extract_features(
def _extract_features(
self,
df: pd.DataFrame,
categorical_feature_column_names: list[str] | None,
Expand Down Expand Up @@ -1642,7 +1642,7 @@ def __init__(self) -> None:
self.kbd_columns: list[str] | None = None
self.features: list[str] | None = None

def fit_feature_encoders(
def _fit_feature_encoders(
self,
df: pd.DataFrame,
categorical_feature_column_names: list[str] | None,
Expand All @@ -1660,7 +1660,7 @@ def fit_feature_encoders(
else:
self.kbd = None

def convert_df(
def _convert_df(
self,
df: pd.DataFrame,
prediction_column_name: str,
Expand Down Expand Up @@ -1702,7 +1702,7 @@ def convert_df(

return df

def train_model(
def _train_model(
self,
df: pd.DataFrame,
prediction_column_name: str,
Expand Down Expand Up @@ -1744,18 +1744,18 @@ def fit(
**kwargs: Any,
) -> Self:
df_train = df_train.copy().reset_index().fillna(0)
self.fit_feature_encoders(
self._fit_feature_encoders(
df_train, categorical_feature_column_names, numerical_feature_column_names
)

df_train = self.convert_df(
df_train = self._convert_df(
df_train,
prediction_column_name,
categorical_feature_column_names,
numerical_feature_column_names,
)

log_reg = self.train_model(
log_reg = self._train_model(
df_train,
prediction_column_name,
label_column_name,
Expand All @@ -1776,7 +1776,7 @@ def predict(
) -> npt.NDArray:
df = df.copy().reset_index().fillna(0)

df = self.convert_df(
df = self._convert_df(
df=df,
prediction_column_name=prediction_column_name,
categorical_feature_column_names=categorical_feature_column_names,
Expand Down
12 changes: 6 additions & 6 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ def __call__(
and mcboost.mce_below_strong_evidence_threshold is not None
), f"MCE is greater than {mcboost.MCE_STRONG_EVIDENCE_THRESHOLD}. Thus, mce_below_strong_evidence_threshold must be False."
assert (
not mcboost.mce_is_satisfactory and mcboost.mce_is_satisfactory is not None
not mcboost._mce_is_satisfactory and mcboost._mce_is_satisfactory is not None
), "MCE is neither below the initial value nor below strong evidence threshold. Thus, mce_is_satisfactory must be False."


Expand All @@ -1706,7 +1706,7 @@ def test_extract_features_categorical_features_overflow(calibrator_class):
mcboost = calibrator_class(encode_categorical_variables=False)
x_cat = np.array([np.iinfo(np.int32).max + 1, np.nan, 0])
with pytest.raises(ValueError) as exc_info:
mcboost.extract_features(
mcboost._extract_features(
df=pd.DataFrame({"cat_feature": x_cat}),
categorical_feature_column_names=["cat_feature"],
numerical_feature_column_names=None,
Expand All @@ -1726,7 +1726,7 @@ def test_extract_features_categorical_features_negative(calibrator_class):
mcboost = calibrator_class(encode_categorical_variables=False)
x_cat = np.array([-1, np.nan, 0])
with pytest.raises(ValueError) as exc_info:
mcboost.extract_features(
mcboost._extract_features(
df=pd.DataFrame({"cat_feature": x_cat}),
categorical_feature_column_names=["cat_feature"],
numerical_feature_column_names=None,
Expand All @@ -1745,7 +1745,7 @@ def test_extract_features_categorical_features_negative(calibrator_class):
def test_extract_features_categorical_features_valid(calibrator_class):
mcboost = calibrator_class(encode_categorical_variables=False)
x_cat = np.array([1, np.nan, 0])
mcboost.extract_features(
mcboost._extract_features(
df=pd.DataFrame({"cat_feature": x_cat}),
categorical_feature_column_names=["cat_feature"],
numerical_feature_column_names=None,
Expand All @@ -1762,7 +1762,7 @@ def test_extract_features_categorical_features_valid(calibrator_class):
def test_extract_features_numerical_features_valid(calibrator_class):
mcboost = calibrator_class(encode_categorical_variables=False)
x_num = np.array([1.0, np.nan, 0])
mcboost.extract_features(
mcboost._extract_features(
df=pd.DataFrame({"num_feature": x_num}),
categorical_feature_column_names=None,
numerical_feature_column_names=["num_feature"],
Expand Down Expand Up @@ -2271,7 +2271,7 @@ def test_prepare_mcboost_processed_data_matches_individual_operations(calibrator
is_fit_phase=True,
)

x_direct = model.extract_features(
x_direct = model._extract_features(
df=df,
categorical_feature_column_names=cat_features,
numerical_feature_column_names=num_features,
Expand Down