Skip to content

Commit a583acc

Browse files
committed
some linting updates
1 parent 9577a16 commit a583acc

15 files changed

Lines changed: 89 additions & 42 deletions

mlscorecheck/aggregated/_check_aggregated_scores.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ def check_aggregated_scores(
7171
"message": "no scores suitable for aggregated consistency checks",
7272
}
7373

74-
experiment = Experiment(**experiment) if isinstance(experiment, dict) else experiment
74+
experiment_obj = Experiment(**experiment) if isinstance(experiment, dict) else experiment
7575

76-
if experiment.aggregation == "som" and any(
77-
evaluation.aggregation == "mos" for evaluation in experiment.evaluations
76+
if experiment_obj.aggregation == "som" and any(
77+
evaluation.aggregation == "mos" for evaluation in experiment_obj.evaluations
7878
):
7979
raise ValueError(
8080
"experiment level MoS aggregation with dataset level SoM "
@@ -95,7 +95,7 @@ def check_aggregated_scores(
9595

9696
result = solve(experiment, scores, eps, solver)
9797

98-
populated = experiment.populate(result)
98+
populated = experiment_obj.populate(result)
9999
populated.calculate_scores(score_subset=list(scores.keys()))
100100
configuration_details = populated.check_bounds()
101101

mlscorecheck/aggregated/_evaluation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(
5454
"n": sum(fold.n for fold in self.folds),
5555
}
5656

57-
self.scores = None
57+
self.scores: dict | None = None
5858

5959
def to_dict(self) -> dict:
6060
"""
@@ -120,6 +120,9 @@ def calculate_scores(
120120
elif self.aggregation == "mos":
121121
self.scores = dict_mean([fold.scores for fold in self.folds])
122122

123+
if self.scores is None:
124+
return {}
125+
123126
return (
124127
self.scores
125128
if rounding_decimals is None
@@ -148,7 +151,8 @@ def init_lp(self, lp_problem: pl.LpProblem, scores: dict | None = None) -> pl.Lp
148151
self.calculate_scores(score_subset=score_subset)
149152

150153
for fold in self.folds:
151-
add_bounds(lp_problem, fold.scores, self.fold_score_bounds, fold.identifier)
154+
if self.fold_score_bounds is not None:
155+
add_bounds(lp_problem, fold.scores, self.fold_score_bounds, fold.identifier)
152156

153157
return lp_problem
154158

mlscorecheck/aggregated/_experiment.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(
4646
"n": sum(evaluation.figures["n"] for evaluation in self.evaluations),
4747
}
4848

49-
self.scores = None
49+
self.scores: dict | None = None
5050

5151
def to_dict(self) -> dict:
5252
"""
@@ -115,6 +115,9 @@ def calculate_scores(
115115
elif self.aggregation == "mos":
116116
self.scores = dict_mean([evaluation.scores for evaluation in self.evaluations])
117117

118+
if self.scores is None:
119+
return {}
120+
118121
return (
119122
self.scores
120123
if rounding_decimals is None
@@ -143,12 +146,13 @@ def init_lp(self, lp_problem: pl.LpProblem, scores: dict | None = None) -> pl.Lp
143146
self.calculate_scores(score_subset=score_subset)
144147

145148
for evaluation in self.evaluations:
146-
add_bounds(
147-
lp_problem,
148-
evaluation.scores,
149-
self.dataset_score_bounds,
150-
evaluation.dataset.identifier,
151-
)
149+
if evaluation.scores is not None and self.dataset_score_bounds is not None:
150+
add_bounds(
151+
lp_problem,
152+
evaluation.scores,
153+
self.dataset_score_bounds,
154+
evaluation.dataset.identifier,
155+
)
152156

153157
return lp_problem
154158

@@ -186,11 +190,14 @@ def check_bounds(self, numerical_tolerance: float = NUMERICAL_TOLERANCE) -> dict
186190
"scores": evaluation.scores,
187191
"score_bounds": self.dataset_score_bounds,
188192
}
189-
if self.dataset_score_bounds is not None:
193+
if self.dataset_score_bounds is not None and evaluation.scores is not None:
190194
tmp["bounds_flag"] = check_bounds(
191195
evaluation.scores, self.dataset_score_bounds, numerical_tolerance
192196
)
193-
tmp["bounds_flag"] = tmp["bounds_flag"] and tmp["folds"]["bounds_flag"]
197+
if tmp["folds"] is not None and isinstance(tmp["folds"], dict):
198+
tmp["bounds_flag"] = tmp["bounds_flag"] and tmp["folds"].get(
199+
"bounds_flag", True
200+
)
194201
else:
195202
tmp["bounds_flag"] = tmp["folds"]
196203
results["evaluations"].append(tmp)

mlscorecheck/aggregated/_fold_enumeration.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ def not_enough_diverse_folds(p_values, n_values):
116116
)
117117

118118

119-
def determine_min_max_p(*, p, n, k_a, k_b, c_a, p_non_zero, n_non_zero): # pylint: disable=too-many-locals
119+
def determine_min_max_p(
120+
*, p, n, k_a, k_b, c_a, p_non_zero, n_non_zero
121+
): # pylint: disable=too-many-locals
120122
"""
121123
Determines the minimum and maximum number of positives that can appear in folds
122124
of type A
@@ -143,7 +145,9 @@ def determine_min_max_p(*, p, n, k_a, k_b, c_a, p_non_zero, n_non_zero): # pyli
143145
return min_p_a, max_p_a
144146

145147

146-
def fold_partitioning_generator(*, p, n, k, p_non_zero=True, n_non_zero=True, p_min=-1): # pylint: disable=invalid-name,too-many-locals
148+
def fold_partitioning_generator(
149+
*, p, n, k, p_non_zero=True, n_non_zero=True, p_min=-1
150+
): # pylint: disable=invalid-name,too-many-locals
147151
"""
148152
Generates the fold partitioning
149153
@@ -251,9 +255,13 @@ def kfolds_generator(evaluation: dict, available_scores: list, repeat_idx=0):
251255
Yields:
252256
list(dict): the list of fold specifications
253257
"""
254-
p, n = _check_specification_and_determine_p_n(
255-
evaluation.get("dataset"), evaluation.get("folding")
256-
)
258+
dataset = evaluation.get("dataset")
259+
folding = evaluation.get("folding")
260+
261+
if dataset is None or folding is None:
262+
raise ValueError("Evaluation must have dataset and folding specifications")
263+
264+
p, n = _check_specification_and_determine_p_n(dataset, folding)
257265

258266
p_zero = False
259267
n_zero = False
@@ -266,9 +274,9 @@ def kfolds_generator(evaluation: dict, available_scores: list, repeat_idx=0):
266274
logger.info("spec and bacc not among the reported scores, n=0 folds are also considered")
267275

268276
if evaluation["dataset"].get("dataset_name") is not None:
269-
evaluation["dataset"]["identifier"] = (
270-
f"{evaluation['dataset']['dataset_name']}_{random_identifier(3)}"
271-
)
277+
evaluation["dataset"][
278+
"identifier"
279+
] = f"{evaluation['dataset']['dataset_name']}_{random_identifier(3)}"
272280
else:
273281
evaluation["dataset"]["identifier"] = random_identifier(6)
274282

mlscorecheck/aggregated/_folding.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
raise ValueError("specify strategy if folds are not set explicitly")
3939

4040
self.n_folds = n_folds
41-
self.n_repeats = n_repeats if n_repeats is not None else 1
41+
self.n_repeats: int = n_repeats if n_repeats is not None else 1
4242
self.folds = folds
4343
self.strategy = strategy
4444

@@ -77,9 +77,21 @@ def generate_folds(self, dataset: Dataset, aggregation: str) -> list:
7777
p += fold["p"]
7878
n += fold["n"]
7979

80-
term_a = (dataset.p != p) and (p % dataset.p != 0)
81-
term_b = (dataset.n != n) and (n % dataset.n != 0)
82-
term_c = dataset.p > 0 and dataset.n > 0 and (p // dataset.p != n // dataset.n)
80+
term_a = (dataset.p is not None and dataset.p != p) and (
81+
dataset.p != 0 and p % dataset.p != 0
82+
)
83+
term_b = (dataset.n is not None and dataset.n != n) and (
84+
dataset.n != 0 and n % dataset.n != 0
85+
)
86+
term_c = (
87+
dataset.p is not None
88+
and dataset.n is not None
89+
and dataset.p > 0
90+
and dataset.n > 0
91+
and dataset.p != 0
92+
and dataset.n != 0
93+
and (p // dataset.p != n // dataset.n)
94+
)
8395

8496
if term_a or term_b or term_c:
8597
raise ValueError(

mlscorecheck/aggregated/_generate_problems.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ def generate_folding(
8080

8181
strategies = ["stratified_sklearn"] if strategies is None else strategies
8282

83-
dataset = Dataset(**dataset)
84-
p, n = dataset.p, dataset.n
83+
dataset_obj = Dataset(**dataset)
84+
p, n = dataset_obj.p, dataset_obj.n
85+
86+
# Ensure p and n are valid integers
87+
if p is None or n is None:
88+
raise ValueError("Dataset must have valid p and n values")
89+
8590
max_folds = min(p, n, max_folds)
8691

8792
n_folds = random_state.randint(1, max_folds + 1)
@@ -93,7 +98,7 @@ def generate_folding(
9398

9499
folding = Folding(n_folds=n_folds, n_repeats=n_repeats, strategy=strategy)
95100

96-
return {"folds": [fold.to_dict() for fold in folding.generate_folds(dataset, "mos")]}
101+
return {"folds": [fold.to_dict() for fold in folding.generate_folds(dataset_obj, "mos")]}
97102

98103

99104
def generate_evaluation( # pylint: disable=too-many-locals
@@ -111,7 +116,7 @@ def generate_evaluation( # pylint: disable=too-many-locals
111116
no_name: bool = False,
112117
no_folds: bool = False,
113118
score_subset: list | None = None,
114-
) -> dict:
119+
) -> dict | tuple[dict, dict]:
115120
"""
116121
Generate a random evaluation specification
117122
@@ -212,7 +217,7 @@ def generate_experiment(
212217
*,
213218
max_evaluations: int = 5,
214219
evaluation_params: dict | None = None,
215-
feasible_dataset_score_bounds: bool = None,
220+
feasible_dataset_score_bounds: bool | None = None,
216221
aggregation: str | None = None,
217222
random_state=None,
218223
return_scores: bool = False,

mlscorecheck/aggregated/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def random_identifier(length: int):
2626
return "".join(random.choice(letters) for _ in range(length))
2727

2828

29-
def check_bounds(scores: dict, bounds: dict | None = None, tolerance: float = 1e-5) -> bool:
29+
def check_bounds(scores: dict, bounds: dict | None = None, tolerance: float = 1e-5) -> bool | None:
3030
"""
3131
Checks the bounds for the scores
3232

mlscorecheck/check/binary/_check_1_dataset_unknown_folds_mos.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def estimate_n_evaluations(
2525
Returns:
2626
int: the estimated number of different fold configurations.
2727
"""
28-
dataset = Dataset(**dataset)
28+
dataset_obj = Dataset(**dataset)
2929
n_repeats = folding.get("n_repeats", 1)
3030

3131
available_scores = [] if available_scores is None else available_scores
3232

3333
count = sum(
3434
1
3535
for _ in kfolds_generator(
36-
{"dataset": dataset.to_dict(), "folding": folding}, available_scores
36+
{"dataset": dataset_obj.to_dict(), "folding": folding}, available_scores
3737
)
3838
)
3939

mlscorecheck/check/binary/_check_1_testset_no_kfold.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def check_1_testset_no_kfold(
118118
p = dataset_statistics[testset["name"]]["p"]
119119
n = dataset_statistics[testset["name"]]["n"]
120120

121+
if p is None or n is None:
122+
raise ValueError("Testset must have valid p and n values")
123+
121124
logger.info(
122125
"calling the score check with scores %s, uncertainty %s, p %d and n %d",
123126
str(scores),

mlscorecheck/check/binary/_check_n_datasets_mos_unknown_folds_mos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def estimate_n_experiments(evaluations: list, available_scores: list | None = No
4141
)
4242
for evaluation in evaluations
4343
]
44-
return np.prod(counts)
44+
return int(np.prod(counts))
4545

4646

4747
def check_n_datasets_mos_unknown_folds_mos(

0 commit comments

Comments
 (0)