Skip to content

Commit f2c646f

Browse files
committed
further clean-up
1 parent 648c9fa commit f2c646f

29 files changed

+204
-140
lines changed

mlscorecheck/aggregated/_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def resolve_pn(self):
6060
"""
6161
Resolves the ``p`` and ``n`` values from the name of the dataset
6262
"""
63-
if self.p is None:
63+
if self.p is None and self.dataset_name is not None:
6464
dataset = dataset_statistics[self.dataset_name]
6565
self.p = dataset["p"]
6666
self.n = dataset["n"]

mlscorecheck/aggregated/_experiment.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ def calculate_scores(
113113
if self.aggregation == "som":
114114
self.scores = calculate_scores_for_lp(self.figures, score_subset=score_subset)
115115
elif self.aggregation == "mos":
116-
self.scores = dict_mean([evaluation.scores for evaluation in self.evaluations])
116+
self.scores = dict_mean(
117+
[
118+
evaluation.scores
119+
for evaluation in self.evaluations
120+
if evaluation.scores is not None
121+
]
122+
)
117123

118124
if self.scores is None:
119125
return {}

mlscorecheck/aggregated/_fold.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
the linear programming problem
88
"""
99

10+
from typing import Any
11+
1012
import pulp as pl
1113

1214
from ..core import init_random_state, round_scores
@@ -34,8 +36,8 @@ def __init__(self, p: int, n: int, identifier: str | None = None):
3436
self.n = n
3537
self.identifier = random_identifier(5) if identifier is None else identifier
3638

37-
self.tp: int | None = None
38-
self.tn: int | None = None
39+
self.tp: Any = None # Can be int | None or pl.LpVariable
40+
self.tn: Any = None # Can be int | None or pl.LpVariable
3941
self.scores: dict | None = None
4042

4143
self.variable_names = {

mlscorecheck/aggregated/_folding_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import copy
6+
from typing import Any, cast
67

78
import numpy as np
89
from sklearn.model_selection import StratifiedKFold
@@ -194,7 +195,7 @@ def create_folds_multiclass(dataset: dict, folding: dict) -> list:
194195
raise ValueError("either specify the folds or the folding strategy")
195196

196197
if "folds" in folding:
197-
return folding["folds"]
198+
return cast(list[Any], folding["folds"])
198199
if folding.get("strategy") == "stratified_sklearn":
199200
folds = multiclass_stratified_folds(dataset, folding.get("n_folds", 1))
200201
else:

mlscorecheck/aggregated/_generate_problems.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,13 @@ def get_dataset_score_bounds(
307307
Returns:
308308
dict(str,tuple(float,float)): the score bounds
309309
"""
310-
score_bounds = dict_minmax([evaluation.scores for evaluation in experiment.evaluations])
310+
score_bounds = dict_minmax(
311+
[
312+
evaluation.scores
313+
for evaluation in experiment.evaluations
314+
if evaluation.scores is not None
315+
]
316+
)
311317
for key, value in score_bounds.items():
312318
score_bounds[key] = (
313319
max(0.0, value[0] - numerical_tolerance),
@@ -349,8 +355,8 @@ def generate_scores_for_testsets(
349355
testset["tn"] = random_state.randint(testset["n"] + 1)
350356

351357
if aggregation == "mos":
352-
scores = [calculate_scores_for_lp(testset) for testset in testsets]
353-
scores = round_scores(dict_mean(scores), rounding_decimals=rounding_decimals)
358+
scores_list = [calculate_scores_for_lp(testset) for testset in testsets]
359+
scores = round_scores(dict_mean(scores_list), rounding_decimals=rounding_decimals)
354360
return {key: value for key, value in scores.items() if key in subset}
355361

356362
mean_figures = dict_mean(testsets)
@@ -431,7 +437,7 @@ def generate_dataset_folding_multiclass(
431437
]
432438

433439
if aggregation == "mos":
434-
scores = [
440+
scores_list = [
435441
calculate_multiclass_scores(
436442
sample,
437443
average=average,
@@ -440,7 +446,7 @@ def generate_dataset_folding_multiclass(
440446
)
441447
for sample in samples
442448
]
443-
scores = round_scores(dict_mean(scores), rounding_decimals=rounding_decimals)
449+
scores = round_scores(dict_mean(scores_list), rounding_decimals=rounding_decimals)
444450
return dataset, folding, scores
445451

446452
# if aggregation == 'som':

mlscorecheck/check/binary/_check_1_dataset_unknown_folds_mos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def estimate_n_evaluations(
3737
)
3838
)
3939

40-
return count**n_repeats
40+
return int(count**n_repeats)
4141

4242

4343
def check_1_dataset_unknown_folds_mos(

mlscorecheck/check/bundles/retina/_diaretdb0.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Tests for the DIARETDB0 dataset
33
"""
44

5+
from typing import Any, cast
6+
57
from ....core import NUMERICAL_TOLERANCE
68
from ....experiments import get_experiment
79
from ...binary import check_n_testsets_mos_no_kfold, check_n_testsets_som_no_kfold
@@ -114,12 +116,15 @@ def check_diaretdb0_class_som(
114116
"""
115117
testsets = _prepare_configuration_diaretdb0(subset, batch, class_name)
116118

117-
return check_n_testsets_som_no_kfold(
118-
testsets=testsets,
119-
scores=scores,
120-
eps=eps,
121-
numerical_tolerance=numerical_tolerance,
122-
prefilter_by_pairs=True,
119+
return cast(
120+
dict[Any, Any],
121+
check_n_testsets_som_no_kfold(
122+
testsets=testsets,
123+
scores=scores,
124+
eps=eps,
125+
numerical_tolerance=numerical_tolerance,
126+
prefilter_by_pairs=True,
127+
),
123128
)
124129

125130

mlscorecheck/check/bundles/retina/_diaretdb1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
This module implements tests for the DIARETDB1 dataset
33
"""
44

5+
from typing import Any, cast
6+
57
from ....core import NUMERICAL_TOLERANCE, logger
68
from ....experiments import get_experiment
79
from ...binary import (
@@ -103,7 +105,7 @@ def _prepare_configuration_diaretdb1(
103105
if only_valid:
104106
testsets = [tset for tset in testsets if tset["p"] > 0 and tset["n"] > 0]
105107

106-
return testsets if pixel_level else testset
108+
return cast(list[Any], testsets if pixel_level else testset)
107109

108110

109111
def check_diaretdb1_class(

mlscorecheck/check/bundles/retina/_drive.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
segmentation drive dataset
44
"""
55

6+
from typing import Any
7+
68
from ....core import NUMERICAL_TOLERANCE
79
from ....experiments import get_experiment
810
from ...binary import (
@@ -239,7 +241,7 @@ def check_drive_vessel_image_assumption(
239241
The evidence for satisfying the consistency constraints.
240242
241243
"""
242-
images = get_experiment("retina.drive")
244+
images: Any = get_experiment("retina.drive")
243245
testset = [
244246
image
245247
for image in images[(annotator, assumption)]["train"]["images"]

mlscorecheck/check/regression/_check_regression.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def mean_average_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
3434
Returns:
3535
float: the MAE score
3636
"""
37-
return np.mean(np.abs(y_true - y_pred))
37+
return float(np.mean(np.abs(y_true - y_pred)))
3838

3939

4040
def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
@@ -48,7 +48,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
4848
Returns:
4949
float: the MSE score
5050
"""
51-
return np.mean((y_true - y_pred) ** 2)
51+
return float(np.mean((y_true - y_pred) ** 2))
5252

5353

5454
def root_mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
@@ -62,7 +62,7 @@ def root_mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
6262
Returns:
6363
float: the RMSE score
6464
"""
65-
return np.sqrt(mean_squared_error(y_true, y_pred))
65+
return float(np.sqrt(mean_squared_error(y_true, y_pred)))
6666

6767

6868
def r_squared(y_true: np.ndarray, y_pred: np.ndarray) -> float:
@@ -76,7 +76,7 @@ def r_squared(y_true: np.ndarray, y_pred: np.ndarray) -> float:
7676
Returns:
7777
float: the R2 score
7878
"""
79-
return 1.0 - np.sum((y_true - y_pred) ** 2) / (np.var(y_true) * y_true.shape[0])
79+
return float(1.0 - np.sum((y_true - y_pred) ** 2) / (np.var(y_true) * y_true.shape[0]))
8080

8181

8282
regression_scores = {

0 commit comments

Comments
 (0)