Unverified Commit ac241888 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[python-package] enforce keyword args in internal functions (#6858)

* [python-package] enforce keyword args in internal functions

* fix

* fixes

* empty commit
parent 18c11f86
...@@ -4371,7 +4371,7 @@ class Booster: ...@@ -4371,7 +4371,7 @@ class Booster:
self.add_valid(data, name) self.add_valid(data, name)
data_idx = self.__num_dataset - 1 data_idx = self.__num_dataset - 1
return self.__inner_eval(name, data_idx, feval) return self.__inner_eval(data_name=name, data_idx=data_idx, feval=feval)
def eval_train( def eval_train(
self, self,
...@@ -4405,7 +4405,7 @@ class Booster: ...@@ -4405,7 +4405,7 @@ class Booster:
result : list result : list
List with (train_dataset_name, eval_name, eval_result, is_higher_better) tuples. List with (train_dataset_name, eval_name, eval_result, is_higher_better) tuples.
""" """
return self.__inner_eval(self._train_data_name, 0, feval) return self.__inner_eval(data_name=self._train_data_name, data_idx=0, feval=feval)
def eval_valid( def eval_valid(
self, self,
...@@ -4442,7 +4442,7 @@ class Booster: ...@@ -4442,7 +4442,7 @@ class Booster:
return [ return [
item item
for i in range(1, self.__num_dataset) for i in range(1, self.__num_dataset)
for item in self.__inner_eval(self.name_valid_sets[i - 1], i, feval) for item in self.__inner_eval(data_name=self.name_valid_sets[i - 1], data_idx=i, feval=feval)
] ]
def save_model( def save_model(
...@@ -5169,6 +5169,7 @@ class Booster: ...@@ -5169,6 +5169,7 @@ class Booster:
def __inner_eval( def __inner_eval(
self, self,
*,
data_name: str, data_name: str,
data_idx: int, data_idx: int,
feval: Optional[Union[_LGBM_CustomEvalFunction, List[_LGBM_CustomEvalFunction]]], feval: Optional[Union[_LGBM_CustomEvalFunction, List[_LGBM_CustomEvalFunction]]],
......
...@@ -180,6 +180,7 @@ def _pad_eval_names(lgbm_model: LGBMModel, required_names: List[str]) -> LGBMMod ...@@ -180,6 +180,7 @@ def _pad_eval_names(lgbm_model: LGBMModel, required_names: List[str]) -> LGBMMod
def _train_part( def _train_part(
*,
params: Dict[str, Any], params: Dict[str, Any],
model_factory: Type[LGBMModel], model_factory: Type[LGBMModel],
list_of_parts: List[Dict[str, _DaskPart]], list_of_parts: List[Dict[str, _DaskPart]],
...@@ -412,6 +413,7 @@ def _machines_to_worker_map(machines: str, worker_addresses: Iterable[str]) -> D ...@@ -412,6 +413,7 @@ def _machines_to_worker_map(machines: str, worker_addresses: Iterable[str]) -> D
def _train( def _train(
*,
client: Client, client: Client,
data: _DaskMatrixLike, data: _DaskMatrixLike,
label: _DaskCollection, label: _DaskCollection,
...@@ -832,6 +834,7 @@ def _train( ...@@ -832,6 +834,7 @@ def _train(
def _predict_part( def _predict_part(
part: _DaskPart, part: _DaskPart,
*,
model: LGBMModel, model: LGBMModel,
raw_score: bool, raw_score: bool,
pred_proba: bool, pred_proba: bool,
...@@ -870,6 +873,7 @@ def _predict_part( ...@@ -870,6 +873,7 @@ def _predict_part(
def _predict( def _predict(
*,
model: LGBMModel, model: LGBMModel,
data: _DaskMatrixLike, data: _DaskMatrixLike,
client: Client, client: Client,
...@@ -1041,6 +1045,7 @@ class _DaskLGBMModel: ...@@ -1041,6 +1045,7 @@ class _DaskLGBMModel:
def _lgb_dask_fit( def _lgb_dask_fit(
self, self,
*,
model_factory: Type[LGBMModel], model_factory: Type[LGBMModel],
X: _DaskMatrixLike, X: _DaskMatrixLike,
y: _DaskCollection, y: _DaskCollection,
......
...@@ -50,7 +50,7 @@ _LGBM_PreprocFunction = Callable[ ...@@ -50,7 +50,7 @@ _LGBM_PreprocFunction = Callable[
] ]
def _choose_num_iterations(num_boost_round_kwarg: int, params: Dict[str, Any]) -> Dict[str, Any]: def _choose_num_iterations(*, num_boost_round_kwarg: int, params: Dict[str, Any]) -> Dict[str, Any]:
"""Choose number of boosting rounds. """Choose number of boosting rounds.
In ``train()`` and ``cv()``, there are multiple ways to provide configuration for In ``train()`` and ``cv()``, there are multiple ways to provide configuration for
...@@ -396,7 +396,13 @@ class CVBooster: ...@@ -396,7 +396,13 @@ class CVBooster:
for model_str in models["boosters"]: for model_str in models["boosters"]:
self.boosters.append(Booster(model_str=model_str)) self.boosters.append(Booster(model_str=model_str))
def _to_dict(self, num_iteration: Optional[int], start_iteration: int, importance_type: str) -> Dict[str, Any]: def _to_dict(
self,
*,
num_iteration: Optional[int],
start_iteration: int,
importance_type: str,
) -> Dict[str, Any]:
"""Serialize CVBooster to dict.""" """Serialize CVBooster to dict."""
models_str = [] models_str = []
for booster in self.boosters: for booster in self.boosters:
...@@ -467,7 +473,9 @@ class CVBooster: ...@@ -467,7 +473,9 @@ class CVBooster:
str_repr : str str_repr : str
JSON string representation of CVBooster. JSON string representation of CVBooster.
""" """
return json.dumps(self._to_dict(num_iteration, start_iteration, importance_type)) return json.dumps(
self._to_dict(num_iteration=num_iteration, start_iteration=start_iteration, importance_type=importance_type)
)
def save_model( def save_model(
self, self,
...@@ -499,12 +507,18 @@ class CVBooster: ...@@ -499,12 +507,18 @@ class CVBooster:
Returns self. Returns self.
""" """
with open(filename, "w") as file: with open(filename, "w") as file:
json.dump(self._to_dict(num_iteration, start_iteration, importance_type), file) json.dump(
self._to_dict(
num_iteration=num_iteration, start_iteration=start_iteration, importance_type=importance_type
),
file,
)
return self return self
def _make_n_folds( def _make_n_folds(
*,
full_data: Dataset, full_data: Dataset,
folds: Optional[Union[Iterable[Tuple[np.ndarray, np.ndarray]], _LGBMBaseCrossValidator]], folds: Optional[Union[Iterable[Tuple[np.ndarray, np.ndarray]], _LGBMBaseCrossValidator]],
nfold: int, nfold: int,
......
...@@ -425,6 +425,7 @@ def plot_metric( ...@@ -425,6 +425,7 @@ def plot_metric(
def _determine_direction_for_numeric_split( def _determine_direction_for_numeric_split(
*,
fval: float, fval: float,
threshold: float, threshold: float,
missing_type_str: str, missing_type_str: str,
...@@ -450,6 +451,7 @@ def _determine_direction_for_categorical_split(fval: float, thresholds: str) -> ...@@ -450,6 +451,7 @@ def _determine_direction_for_categorical_split(fval: float, thresholds: str) ->
def _to_graphviz( def _to_graphviz(
*,
tree_info: Dict[str, Any], tree_info: Dict[str, Any],
show_info: List[str], show_info: List[str],
feature_names: Union[List[str], None], feature_names: Union[List[str], None],
......
...@@ -329,7 +329,10 @@ def test_numeric_split_direction(use_missing, zero_as_missing): ...@@ -329,7 +329,10 @@ def test_numeric_split_direction(use_missing, zero_as_missing):
node = bst.dump_model()["tree_info"][0]["tree_structure"] node = bst.dump_model()["tree_info"][0]["tree_structure"]
while "decision_type" in node: while "decision_type" in node:
direction = lgb.plotting._determine_direction_for_numeric_split( direction = lgb.plotting._determine_direction_for_numeric_split(
case_with_zero[0][node["split_feature"]], node["threshold"], node["missing_type"], node["default_left"] fval=case_with_zero[0][node["split_feature"]],
threshold=node["threshold"],
missing_type_str=node["missing_type"],
default_left=node["default_left"],
) )
node = node["left_child"] if direction == "left" else node["right_child"] node = node["left_child"] if direction == "left" else node["right_child"]
assert node["leaf_index"] == expected_leaf_zero assert node["leaf_index"] == expected_leaf_zero
...@@ -340,7 +343,10 @@ def test_numeric_split_direction(use_missing, zero_as_missing): ...@@ -340,7 +343,10 @@ def test_numeric_split_direction(use_missing, zero_as_missing):
node = bst.dump_model()["tree_info"][0]["tree_structure"] node = bst.dump_model()["tree_info"][0]["tree_structure"]
while "decision_type" in node: while "decision_type" in node:
direction = lgb.plotting._determine_direction_for_numeric_split( direction = lgb.plotting._determine_direction_for_numeric_split(
case_with_nan[0][node["split_feature"]], node["threshold"], node["missing_type"], node["default_left"] fval=case_with_nan[0][node["split_feature"]],
threshold=node["threshold"],
missing_type_str=node["missing_type"],
default_left=node["default_left"],
) )
node = node["left_child"] if direction == "left" else node["right_child"] node = node["left_child"] if direction == "left" else node["right_child"]
assert node["leaf_index"] == expected_leaf_nan assert node["leaf_index"] == expected_leaf_nan
...@@ -377,10 +383,10 @@ def test_example_case_in_tree_digraph(): ...@@ -377,10 +383,10 @@ def test_example_case_in_tree_digraph():
edge_to_node = [e for e in gbody if f"-> split{split_index}" in e] edge_to_node = [e for e in gbody if f"-> split{split_index}" in e]
if node["decision_type"] == "<=": if node["decision_type"] == "<=":
direction = lgb.plotting._determine_direction_for_numeric_split( direction = lgb.plotting._determine_direction_for_numeric_split(
example_case[0][node["split_feature"]], fval=example_case[0][node["split_feature"]],
node["threshold"], threshold=node["threshold"],
node["missing_type"], missing_type_str=node["missing_type"],
node["default_left"], default_left=node["default_left"],
) )
else: else:
makes_categorical_splits = True makes_categorical_splits = True
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment