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:
self.add_valid(data, name)
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(
self,
......@@ -4405,7 +4405,7 @@ class Booster:
result : list
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(
self,
......@@ -4442,7 +4442,7 @@ class Booster:
return [
item
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(
......@@ -5169,6 +5169,7 @@ class Booster:
def __inner_eval(
self,
*,
data_name: str,
data_idx: int,
feval: Optional[Union[_LGBM_CustomEvalFunction, List[_LGBM_CustomEvalFunction]]],
......
......@@ -180,6 +180,7 @@ def _pad_eval_names(lgbm_model: LGBMModel, required_names: List[str]) -> LGBMMod
def _train_part(
*,
params: Dict[str, Any],
model_factory: Type[LGBMModel],
list_of_parts: List[Dict[str, _DaskPart]],
......@@ -412,6 +413,7 @@ def _machines_to_worker_map(machines: str, worker_addresses: Iterable[str]) -> D
def _train(
*,
client: Client,
data: _DaskMatrixLike,
label: _DaskCollection,
......@@ -832,6 +834,7 @@ def _train(
def _predict_part(
part: _DaskPart,
*,
model: LGBMModel,
raw_score: bool,
pred_proba: bool,
......@@ -870,6 +873,7 @@ def _predict_part(
def _predict(
*,
model: LGBMModel,
data: _DaskMatrixLike,
client: Client,
......@@ -1041,6 +1045,7 @@ class _DaskLGBMModel:
def _lgb_dask_fit(
self,
*,
model_factory: Type[LGBMModel],
X: _DaskMatrixLike,
y: _DaskCollection,
......
......@@ -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.
In ``train()`` and ``cv()``, there are multiple ways to provide configuration for
......@@ -396,7 +396,13 @@ class CVBooster:
for model_str in models["boosters"]:
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."""
models_str = []
for booster in self.boosters:
......@@ -467,7 +473,9 @@ class CVBooster:
str_repr : str
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(
self,
......@@ -499,12 +507,18 @@ class CVBooster:
Returns self.
"""
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
def _make_n_folds(
*,
full_data: Dataset,
folds: Optional[Union[Iterable[Tuple[np.ndarray, np.ndarray]], _LGBMBaseCrossValidator]],
nfold: int,
......
......@@ -425,6 +425,7 @@ def plot_metric(
def _determine_direction_for_numeric_split(
*,
fval: float,
threshold: float,
missing_type_str: str,
......@@ -450,6 +451,7 @@ def _determine_direction_for_categorical_split(fval: float, thresholds: str) ->
def _to_graphviz(
*,
tree_info: Dict[str, Any],
show_info: List[str],
feature_names: Union[List[str], None],
......
......@@ -329,7 +329,10 @@ def test_numeric_split_direction(use_missing, zero_as_missing):
node = bst.dump_model()["tree_info"][0]["tree_structure"]
while "decision_type" in node:
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"]
assert node["leaf_index"] == expected_leaf_zero
......@@ -340,7 +343,10 @@ def test_numeric_split_direction(use_missing, zero_as_missing):
node = bst.dump_model()["tree_info"][0]["tree_structure"]
while "decision_type" in node:
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"]
assert node["leaf_index"] == expected_leaf_nan
......@@ -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]
if node["decision_type"] == "<=":
direction = lgb.plotting._determine_direction_for_numeric_split(
example_case[0][node["split_feature"]],
node["threshold"],
node["missing_type"],
node["default_left"],
fval=example_case[0][node["split_feature"]],
threshold=node["threshold"],
missing_type_str=node["missing_type"],
default_left=node["default_left"],
)
else:
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