Download aggregate.py from azimi2kht/JEFFNet: direct link, hf CLI and curl.
- Browser
- Download file 3.86 kB
-
https://huggingface.co/azimi2kht/JEFFNet/resolve/main/aggregate.py
- Command line
-
hf download hf://azimi2kht/JEFFNet/aggregate.py
-
curl -L -o aggregate.py https://huggingface.co/azimi2kht/JEFFNet/resolve/main/aggregate.py
3.86 kB
| #!/usr/bin/env python3 | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| from collections import defaultdict | |
| import numpy as np | |
| def find_metrics_files(root_dir: Path): | |
| """ | |
| Find all files matching: | |
| */test_metrics/metrics.json | |
| under root_dir. | |
| """ | |
| return sorted(root_dir.rglob("test_metrics/metrics.json")) | |
| def load_metrics(metrics_path: Path): | |
| """ | |
| Load the metrics dictionary from one metrics.json file. | |
| Expected structure: | |
| { | |
| "metrics": { | |
| "accuracy": ..., | |
| ... | |
| } | |
| } | |
| """ | |
| with open(metrics_path, "r") as f: | |
| data = json.load(f) | |
| if "metrics" not in data: | |
| raise ValueError(f"No 'metrics' key found in {metrics_path}") | |
| if not isinstance(data["metrics"], dict): | |
| raise ValueError(f"'metrics' is not a dictionary in {metrics_path}") | |
| return data["metrics"] | |
| def aggregate_metrics(metrics_list): | |
| """ | |
| metrics_list: | |
| list of dictionaries, one per run. | |
| Returns: | |
| dictionary containing mean/std aggregation. | |
| """ | |
| values_by_metric = defaultdict(list) | |
| for metrics in metrics_list: | |
| for metric_name, value in metrics.items(): | |
| if isinstance(value, (int, float)): | |
| values_by_metric[metric_name].append(float(value)) | |
| aggregated = {} | |
| for metric_name, values in values_by_metric.items(): | |
| values = np.array(values, dtype=np.float64) | |
| mean = float(np.mean(values)) | |
| # sample std if more than one run, otherwise 0.0 | |
| std = float(np.std(values, ddof=1)) if len(values) > 1 else 0.0 | |
| aggregated[metric_name] = { | |
| "mean": mean, | |
| "std": std, | |
| "mean_pm_std": f"{mean:.6f} ± {std:.6f}", | |
| "num_runs": int(len(values)), | |
| "values": values.tolist(), | |
| } | |
| return aggregated | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "--root-dir", | |
| type=str, | |
| required=True, | |
| help="Root folder containing experiment subfolders.", | |
| ) | |
| parser.add_argument( | |
| "--output-dir", | |
| type=str, | |
| required=True, | |
| help="Folder where the aggregated JSON will be saved.", | |
| ) | |
| parser.add_argument( | |
| "--output-name", | |
| type=str, | |
| default="aggregated_metrics.json", | |
| help="Name of the output JSON file.", | |
| ) | |
| args = parser.parse_args() | |
| root_dir = Path(args.root_dir) | |
| output_dir = Path(args.output_dir) | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| metrics_files = find_metrics_files(root_dir) | |
| if len(metrics_files) == 0: | |
| raise FileNotFoundError( | |
| f"No files found matching */test_metrics/metrics.json under {root_dir}" | |
| ) | |
| all_metrics = [] | |
| used_files = [] | |
| for metrics_path in metrics_files: | |
| try: | |
| metrics = load_metrics(metrics_path) | |
| all_metrics.append(metrics) | |
| used_files.append(str(metrics_path)) | |
| except Exception as e: | |
| print(f"Skipping {metrics_path}: {e}") | |
| if len(all_metrics) == 0: | |
| raise RuntimeError("No valid metrics files could be loaded.") | |
| aggregated = aggregate_metrics(all_metrics) | |
| output = { | |
| "num_valid_runs": len(all_metrics), | |
| "num_found_metrics_files": len(metrics_files), | |
| "source_root": str(root_dir), | |
| "used_files": used_files, | |
| "aggregated_metrics": aggregated, | |
| } | |
| output_path = output_dir / args.output_name | |
| with open(output_path, "w") as f: | |
| json.dump(output, f, indent=4) | |
| for i in metrics_files: | |
| print(i) | |
| print(f"Found metrics files: {len(metrics_files)}") | |
| print(f"Valid runs used: {len(all_metrics)}") | |
| print(f"Saved aggregation to: {output_path}") | |
| if __name__ == "__main__": | |
| main() |