Skip to content

Commit 14784f8

Browse files
committed
some linting improvements
1 parent a09a5b9 commit 14784f8

148 files changed

Lines changed: 687 additions & 754 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Configuration file for the Sphinx documentation builder.
43
#

legacy/_check_1_dataset_unknown_folds_mos_acc_score.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import itertools
77

8-
from ...core import NUMERICAL_TOLERANCE
98
from ...aggregated import Evaluation
9+
from ...core import NUMERICAL_TOLERANCE
1010

1111
__all__ = ['check_1_dataset_unknown_folds_mos_acc_score']
1212

legacy/test_1_dataset_unknown_folds_mos_acc_score.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
with unknown folds (only k and the number of repetitions known)
44
"""
55

6-
import pytest
7-
86
import numpy as np
7+
import pytest
98

9+
from mlscorecheck.aggregated import Evaluation, Folding, generate_dataset
1010
from mlscorecheck.check.binary import check_1_dataset_unknown_folds_mos_acc_score
11-
from mlscorecheck.aggregated import (generate_dataset,
12-
Evaluation, Folding)
11+
1312

1413
@pytest.mark.parametrize('random_seed', range(20))
1514
def test_consistency(random_seed: int):

mlscorecheck/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
"""
44

55
from . import aggregated, check, core, experiments, individual, symbolic
6-
from ._version import *
6+
from ._version import __version__

mlscorecheck/aggregated/_fold_enumeration.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"_check_specification_and_determine_p_n",
2323
"multiclass_fold_partitioning_generator_22",
2424
"multiclass_fold_partitioning_generator_2n",
25-
"multiclass_fold_partitioning_generator_kn"
25+
"multiclass_fold_partitioning_generator_kn",
2626
]
2727

2828

@@ -75,9 +75,7 @@ def integer_partitioning_generator(n: int, m: int): # pylint: disable=invalid-n
7575
x[m] = n - s[m - 1]
7676

7777

78-
def all_integer_partitioning_generator(
79-
n, k, non_zero, max_count
80-
): # pylint: disable=invalid-name
78+
def all_integer_partitioning_generator(n, k, non_zero, max_count): # pylint: disable=invalid-name
8179
"""
8280
Generate all integer partitioning of n to k parts (including 0 parts)
8381
@@ -114,8 +112,7 @@ def not_enough_diverse_folds(p_values, n_values):
114112
"""
115113

116114
return len(p_values) > 1 and (
117-
sum(p_tmp > 0 for p_tmp in p_values) < 2
118-
or sum(n_tmp > 0 for n_tmp in n_values) < 2
115+
sum(p_tmp > 0 for p_tmp in p_values) < 2 or sum(n_tmp > 0 for n_tmp in n_values) < 2
119116
)
120117

121118

@@ -185,17 +182,13 @@ def fold_partitioning_generator(
185182
for p_a in range(min_p_a, max_p_a + 1):
186183
p_b = p - p_a
187184

188-
for ps_a in all_integer_partitioning_generator(
189-
p_a, k_a, p_non_zero, c_a - n_non_zero
190-
):
185+
for ps_a in all_integer_partitioning_generator(p_a, k_a, p_non_zero, c_a - n_non_zero):
191186
if any(p_tmp < p_min for p_tmp in ps_a):
192187
continue
193188

194189
ns_a = [c_a - tmp for tmp in ps_a]
195190

196-
for ps_b in all_integer_partitioning_generator(
197-
p_b, k_b, p_non_zero, c_b - n_non_zero
198-
):
191+
for ps_b in all_integer_partitioning_generator(p_b, k_b, p_non_zero, c_b - n_non_zero):
199192
if any(p_tmp < p_min for p_tmp in ps_b):
200193
continue
201194

@@ -210,7 +203,7 @@ def fold_partitioning_generator(
210203
yield ps_all, ns_all
211204

212205

213-
def _check_specification_and_determine_p_n(dataset: dict, folding: dict) -> (int, int):
206+
def _check_specification_and_determine_p_n(dataset: dict, folding: dict) -> tuple[int, int]:
214207
"""
215208
Checking if the dataset specification is correct and determining the p and n values
216209
@@ -271,14 +264,10 @@ def kfolds_generator(evaluation: dict, available_scores: list, repeat_idx=0):
271264

272265
if "sens" not in available_scores and "bacc" not in available_scores:
273266
p_zero = True
274-
logger.info(
275-
"sens and bacc not among the reported scores, p=0 folds are also considered"
276-
)
267+
logger.info("sens and bacc not among the reported scores, p=0 folds are also considered")
277268
if "spec" not in available_scores and "bacc" not in available_scores:
278269
n_zero = True
279-
logger.info(
280-
"spec and bacc not among the reported scores, n=0 folds are also considered"
281-
)
270+
logger.info("spec and bacc not among the reported scores, n=0 folds are also considered")
282271

283272
if evaluation["dataset"].get("dataset_name") is not None:
284273
evaluation["dataset"][
@@ -321,17 +310,13 @@ def repeated_kfolds_generator(evaluation: dict, available_scores: list):
321310
dict: one evaluation
322311
"""
323312
n_repeats = evaluation["folding"].get("n_repeats", 1)
324-
generators = [
325-
kfolds_generator(evaluation, available_scores, idx) for idx in range(n_repeats)
326-
]
313+
generators = [kfolds_generator(evaluation, available_scores, idx) for idx in range(n_repeats)]
327314

328315
if n_repeats > 1:
329316
for folds in itertools.product(*generators):
330317
yield {
331318
"dataset": copy.deepcopy(evaluation["dataset"]),
332-
"folding": {
333-
"folds": [fold for fold_list in folds for fold in fold_list]
334-
},
319+
"folding": {"folds": [fold for fold_list in folds for fold in fold_list]},
335320
"fold_score_bounds": copy.deepcopy(evaluation.get("fold_score_bounds")),
336321
"aggregation": evaluation.get("aggregation"),
337322
}
@@ -370,6 +355,7 @@ def experiment_kfolds_generator(experiment: dict, available_scores: list):
370355
"aggregation": experiment["aggregation"],
371356
}
372357

358+
373359
def multiclass_fold_partitioning_generator_22(n0: int, n1: int, c0: int):
374360
"""
375361
Generates the configurations for two folds of cardinalities n0 and n1 and two
@@ -387,10 +373,8 @@ def multiclass_fold_partitioning_generator_22(n0: int, n1: int, c0: int):
387373
lower_bound = max(c0 - n1, 0)
388374

389375
for c_00 in range(lower_bound, upper_bound + 1):
390-
yield {
391-
0: (c_00, n0 - c_00),
392-
1: (c0 - c_00, n1 - c0 + c_00)
393-
}
376+
yield {0: (c_00, n0 - c_00), 1: (c0 - c_00, n1 - c0 + c_00)}
377+
394378

395379
def multiclass_fold_partitioning_generator_2n(n0: int, n1: int, cs: list):
396380
"""
@@ -410,14 +394,10 @@ def multiclass_fold_partitioning_generator_2n(n0: int, n1: int, cs: list):
410394
yield part
411395
else:
412396
for part_deep in multiclass_fold_partitioning_generator_2n(
413-
part[0][1],
414-
part[1][1],
415-
cs[1:]
416-
):
417-
yield {
418-
0: (part[0][0], *(part_deep[0])),
419-
1: (part[1][0], *(part_deep[1]))
420-
}
397+
part[0][1], part[1][1], cs[1:]
398+
):
399+
yield {0: (part[0][0], *(part_deep[0])), 1: (part[1][0], *(part_deep[1]))}
400+
421401

422402
def multiclass_fold_partitioning_generator_kn(ns: list, cs: list):
423403
"""

mlscorecheck/check/binary/_check_1_dataset_unknown_folds_mos.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def check_1_dataset_unknown_folds_mos(
4848
solver_name: str = None,
4949
timeout: int = None,
5050
verbosity: int = 1,
51-
numerical_tolerance: float = NUMERICAL_TOLERANCE
51+
numerical_tolerance: float = NUMERICAL_TOLERANCE,
5252
) -> dict:
5353
"""
5454
Checking the consistency of scores calculated in a k-fold cross validation on a single
@@ -141,8 +141,7 @@ def check_1_dataset_unknown_folds_mos(
141141

142142
results = {"details": []}
143143

144-
idx = 0
145-
for evaluation_0 in repeated_kfolds_generator(evaluation, list(scores.keys())):
144+
for idx, evaluation_0 in enumerate(repeated_kfolds_generator(evaluation, list(scores.keys()))):
146145
tmp = {
147146
"folds": evaluation_0["folding"]["folds"],
148147
"details": check_1_dataset_known_folds_mos(
@@ -161,7 +160,6 @@ def check_1_dataset_unknown_folds_mos(
161160
results["details"].append(tmp)
162161
if not tmp["details"]["inconsistency"]:
163162
break
164-
idx += 1
165163

166164
results["inconsistency"] = all(tmp["details"]["inconsistency"] for tmp in results["details"])
167165

mlscorecheck/check/regression/_check_regression.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ def r_squared(y_true: np.array, y_pred: np.array) -> float:
8787
}
8888

8989

90-
def calculate_regression_scores(
91-
y_true: np.array, y_pred: np.array, subset=None
92-
) -> dict:
90+
def calculate_regression_scores(y_true: np.array, y_pred: np.array, subset=None) -> dict:
9391
"""
9492
Calculate the performance scores for a regression problem
9593
@@ -112,7 +110,7 @@ def calculate_regression_scores(
112110

113111
def generate_regression_problem_and_scores(
114112
random_state=None, max_n_samples=1000, subset=None, rounding_decimals=None
115-
) -> (float, int, dict):
113+
) -> tuple[float, int, dict]:
116114
"""
117115
Generate a regression problem and corresponding scores
118116
@@ -165,9 +163,7 @@ def expand_regression_scores(
165163
dict: the extended set of score intervals
166164
"""
167165
scores = {
168-
key: Interval(
169-
value - eps - numerical_tolerance, value + eps + numerical_tolerance
170-
)
166+
key: Interval(value - eps - numerical_tolerance, value + eps + numerical_tolerance)
171167
for key, value in scores.items()
172168
}
173169

@@ -176,9 +172,7 @@ def expand_regression_scores(
176172
if key not in scores:
177173
for sol, formula in value.items():
178174
if sol in scores:
179-
to_add[key] = safe_eval(
180-
formula, scores | {"var": var, "n_samples": n_samples}
181-
)
175+
to_add[key] = safe_eval(formula, scores | {"var": var, "n_samples": n_samples})
182176
break
183177

184178
return scores | to_add
@@ -256,8 +250,6 @@ def check_1_testset_no_kfold(
256250
>>> result['inconsistency']
257251
# True
258252
"""
259-
intervals = expand_regression_scores(
260-
var, n_samples, scores, eps, numerical_tolerance
261-
)
253+
intervals = expand_regression_scores(var, n_samples, scores, eps, numerical_tolerance)
262254

263255
return check_relations(intervals)

mlscorecheck/core/_rounding.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__all__ = ["round_scores"]
88

99

10-
def round_scores(to_round: dict, rounding_decimals: int = None):
10+
def round_scores(to_round: dict, rounding_decimals: int | None = None):
1111
"""
1212
Rounds the scores
1313
@@ -24,7 +24,4 @@ def round_scores(to_round: dict, rounding_decimals: int = None):
2424
if not isinstance(to_round, dict):
2525
return float(np.round(to_round, rounding_decimals))
2626

27-
return {
28-
key: float(np.round(value, rounding_decimals))
29-
for key, value in to_round.items()
30-
}
27+
return {key: float(np.round(value, rounding_decimals)) for key, value in to_round.items()}

mlscorecheck/core/_safe_eval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def safe_eval(expression: str, subs: dict):
2121
return eval(expression, subs) # pylint: disable=eval-used
2222

2323

24-
def check_applicability(params: dict, non_applicable: list = None) -> bool:
24+
def check_applicability(params: dict, non_applicable: list | None = None) -> bool:
2525
"""
2626
Checks if a parameter configuration is applicable according to the
2727
non-applicability configurations
@@ -50,7 +50,7 @@ def check_applicability(params: dict, non_applicable: list = None) -> bool:
5050
return True
5151

5252

53-
def safe_call(function, params: dict, non_applicable: list = None):
53+
def safe_call(function, params: dict, non_applicable: list | None = None):
5454
"""
5555
Safe call to a function
5656

mlscorecheck/experiments/_load_datasets.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ..core import load_json
1010

11-
dataset_statistics = {}
11+
dataset_statistics: dict = {}
1212

1313

1414
def resolve_pn(dataset_conf):
@@ -65,9 +65,7 @@ def load_ml_datasets():
6565
"n": entry["n"],
6666
}
6767

68-
data = load_json(
69-
os.path.join("experiments", "machine_learning"), "common_datasets.json"
70-
)
68+
data = load_json(os.path.join("experiments", "machine_learning"), "common_datasets.json")
7169

7270
for entry in data["datasets"]:
7371
dataset_statistics["common_datasets." + entry["name"]] = {

0 commit comments

Comments
 (0)