Mercurial > repos > goeckslab > image_learner
annotate utils.py @ 13:1a9c42974a5a draft default tip
planemo upload for repository https://github.com/goeckslab/gleam.git commit 9f96da4ea7ab3b572af86698ff51b870125cd674
| author | goeckslab |
|---|---|
| date | Fri, 21 Nov 2025 17:35:00 +0000 |
| parents | bcfa2e234a80 |
| children |
| rev | line source |
|---|---|
|
12
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
1 import logging |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
2 from pathlib import Path |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
3 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
4 import pandas as pd |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
5 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
6 logger = logging.getLogger("ImageLearner") |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
7 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
8 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
9 def load_metadata_table(file_path: Path) -> pd.DataFrame: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
10 """Load image metadata allowing either CSV or TSV delimiters.""" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
11 logger.info("Loading metadata table from %s", file_path) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
12 return pd.read_csv(file_path, sep=None, engine="python") |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
13 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
14 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
15 def detect_output_type(test_stats): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
16 """Detects if the output type is 'binary' or 'category' based on test statistics.""" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
17 label_stats = test_stats.get("label", {}) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
18 if "mean_squared_error" in label_stats: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
19 return "regression" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
20 per_class = label_stats.get("per_class_stats", {}) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
21 if len(per_class) == 2: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
22 return "binary" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
23 return "category" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
24 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
25 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
26 def aug_parse(aug_string: str): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
27 """ |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
28 Parse comma-separated augmentation keys into Ludwig augmentation dicts. |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
29 Raises ValueError on unknown key. |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
30 """ |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
31 mapping = { |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
32 "random_horizontal_flip": {"type": "random_horizontal_flip"}, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
33 "random_vertical_flip": {"type": "random_vertical_flip"}, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
34 "random_rotate": {"type": "random_rotate", "degree": 10}, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
35 "random_blur": {"type": "random_blur", "kernel_size": 3}, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
36 "random_brightness": {"type": "random_brightness", "min": 0.5, "max": 2.0}, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
37 "random_contrast": {"type": "random_contrast", "min": 0.5, "max": 2.0}, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
38 } |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
39 aug_list = [] |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
40 for tok in aug_string.split(","): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
41 key = tok.strip() |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
42 if not key: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
43 continue |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
44 if key not in mapping: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
45 valid = ", ".join(mapping.keys()) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
46 raise ValueError(f"Unknown augmentation '{key}'. Valid choices: {valid}") |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
47 aug_list.append(mapping[key]) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
48 return aug_list |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
49 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
50 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
51 def argument_checker(args, parser): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
52 if not 0.0 <= args.validation_size <= 1.0: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
53 parser.error("validation-size must be between 0.0 and 1.0") |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
54 if not args.csv_file.is_file(): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
55 parser.error(f"Metada file not found: {args.csv_file}") |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
56 if not (args.image_zip.is_file() or args.image_zip.is_dir()): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
57 parser.error(f"ZIP or directory not found: {args.image_zip}") |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
58 if args.augmentation is not None: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
59 try: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
60 augmentation_setup = aug_parse(args.augmentation) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
61 setattr(args, "augmentation", augmentation_setup) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
62 except ValueError as e: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
63 parser.error(str(e)) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
64 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
65 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
66 def parse_learning_rate(s): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
67 try: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
68 return float(s) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
69 except (TypeError, ValueError): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
70 return None |
|
0
54b871dfc51e
planemo upload for repository https://github.com/goeckslab/gleam.git commit b7411ff35b6228ccdfd36cd4ebd946c03ac7f7e9
goeckslab
parents:
diff
changeset
|
71 |
|
54b871dfc51e
planemo upload for repository https://github.com/goeckslab/gleam.git commit b7411ff35b6228ccdfd36cd4ebd946c03ac7f7e9
goeckslab
parents:
diff
changeset
|
72 |
|
12
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
73 def extract_metrics_from_json( |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
74 train_stats: dict, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
75 test_stats: dict, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
76 output_type: str, |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
77 ) -> dict: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
78 """Extracts relevant metrics from training and test statistics based on the output type.""" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
79 metrics = {"training": {}, "validation": {}, "test": {}} |
|
8
85e6f4b2ad18
planemo upload for repository https://github.com/goeckslab/gleam.git commit 8a42eb9b33df7e1df5ad5153b380e20b910a05b6
goeckslab
parents:
2
diff
changeset
|
80 |
|
12
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
81 def get_last_value(stats, key): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
82 val = stats.get(key) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
83 if isinstance(val, list) and val: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
84 return val[-1] |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
85 elif isinstance(val, (int, float)): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
86 return val |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
87 return None |
|
8
85e6f4b2ad18
planemo upload for repository https://github.com/goeckslab/gleam.git commit 8a42eb9b33df7e1df5ad5153b380e20b910a05b6
goeckslab
parents:
2
diff
changeset
|
88 |
|
12
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
89 for split in ["training", "validation"]: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
90 split_stats = train_stats.get(split, {}) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
91 if not split_stats: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
92 logger.warning("No statistics found for %s split", split) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
93 continue |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
94 label_stats = split_stats.get("label", {}) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
95 if not label_stats: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
96 logger.warning("No label statistics found for %s split", split) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
97 continue |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
98 if output_type == "binary": |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
99 metrics[split] = { |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
100 "accuracy": get_last_value(label_stats, "accuracy"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
101 "loss": get_last_value(label_stats, "loss"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
102 "precision": get_last_value(label_stats, "precision"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
103 "recall": get_last_value(label_stats, "recall"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
104 "specificity": get_last_value(label_stats, "specificity"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
105 "roc_auc": get_last_value(label_stats, "roc_auc"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
106 } |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
107 elif output_type == "regression": |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
108 metrics[split] = { |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
109 "loss": get_last_value(label_stats, "loss"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
110 "mean_absolute_error": get_last_value( |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
111 label_stats, "mean_absolute_error" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
112 ), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
113 "mean_absolute_percentage_error": get_last_value( |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
114 label_stats, "mean_absolute_percentage_error" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
115 ), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
116 "mean_squared_error": get_last_value(label_stats, "mean_squared_error"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
117 "root_mean_squared_error": get_last_value( |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
118 label_stats, "root_mean_squared_error" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
119 ), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
120 "root_mean_squared_percentage_error": get_last_value( |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
121 label_stats, "root_mean_squared_percentage_error" |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
122 ), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
123 "r2": get_last_value(label_stats, "r2"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
124 } |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
125 else: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
126 metrics[split] = { |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
127 "accuracy": get_last_value(label_stats, "accuracy"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
128 "accuracy_micro": get_last_value(label_stats, "accuracy_micro"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
129 "loss": get_last_value(label_stats, "loss"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
130 "roc_auc": get_last_value(label_stats, "roc_auc"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
131 "hits_at_k": get_last_value(label_stats, "hits_at_k"), |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
132 } |
|
2
186424a7eca7
planemo upload for repository https://github.com/goeckslab/gleam.git commit 91fa4aba245520fc0680088a07cead66bcfd4ed2
goeckslab
parents:
1
diff
changeset
|
133 |
|
12
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
134 # Test metrics: dynamic extraction according to exclusions |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
135 test_label_stats = test_stats.get("label", {}) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
136 if not test_label_stats: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
137 logger.warning("No label statistics found for test split") |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
138 else: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
139 combined_stats = test_stats.get("combined", {}) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
140 overall_stats = test_label_stats.get("overall_stats", {}) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
141 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
142 # Define exclusions |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
143 if output_type == "binary": |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
144 exclude = {"per_class_stats", "precision_recall_curve", "roc_curve"} |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
145 else: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
146 exclude = {"per_class_stats", "confusion_matrix"} |
|
10
b0d893d04d4c
planemo upload for repository https://github.com/goeckslab/gleam.git commit 1594d503179f28987720594eb49b48a15486f073
goeckslab
parents:
9
diff
changeset
|
147 |
|
12
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
148 # 1. Get all scalar test_label_stats not excluded |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
149 test_metrics = {} |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
150 for k, v in test_label_stats.items(): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
151 if k in exclude: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
152 continue |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
153 if k == "overall_stats": |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
154 continue |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
155 if isinstance(v, (int, float, str, bool)): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
156 test_metrics[k] = v |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
157 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
158 # 2. Add overall_stats (flattened) |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
159 for k, v in overall_stats.items(): |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
160 test_metrics[k] = v |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
161 |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
162 # 3. Optionally include combined/loss if present and not already |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
163 if "loss" in combined_stats and "loss" not in test_metrics: |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
164 test_metrics["loss"] = combined_stats["loss"] |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
165 metrics["test"] = test_metrics |
|
bcfa2e234a80
planemo upload for repository https://github.com/goeckslab/gleam.git commit 96bab8325992d16fcaad8e0a4dc4c62b00e2abc2
goeckslab
parents:
11
diff
changeset
|
166 return metrics |
