Add run_data_analysis/src/run_data_analysis/cli.py
This commit is contained in:
parent
d77271c3d1
commit
cd5f4aa37a
1 changed files with 54 additions and 0 deletions
54
run_data_analysis/src/run_data_analysis/cli.py
Normal file
54
run_data_analysis/src/run_data_analysis/cli.py
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from run_data_analysis.core import analyse_run_data
|
||||||
|
|
||||||
|
|
||||||
|
def _load_run_data(path: Path) -> dict:
|
||||||
|
if not path.exists():
|
||||||
|
raise FileNotFoundError(f'Eingabedatei nicht gefunden: {path}')
|
||||||
|
try:
|
||||||
|
with path.open('r', encoding='utf-8') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
raise ValueError(f'Ungültiges JSON-Format: {path}') from e
|
||||||
|
|
||||||
|
required_fields = {"band_center", "band_width", "cluster_score", "retry_tail"}
|
||||||
|
missing = required_fields - data.keys()
|
||||||
|
if missing:
|
||||||
|
raise ValueError(f'Fehlende Felder im Eingabeobjekt: {missing}')
|
||||||
|
for field in required_fields:
|
||||||
|
if not isinstance(data[field], (int, float)):
|
||||||
|
raise TypeError(f'Feld {field} muss numerisch sein, ist {type(data[field]).__name__}')
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def _write_output(path: Path, result: dict) -> None:
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with path.open('w', encoding='utf-8') as f:
|
||||||
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Analyse der Run-Daten zur Berechnung des Resonanzbands und zugehöriger Metriken.'
|
||||||
|
)
|
||||||
|
parser.add_argument('--input', required=True, help='Pfad zur Run-Datendatei im JSON-Format')
|
||||||
|
parser.add_argument('--output', required=True, help='Pfad zur Ausgabe der Analyseergebnisse im JSON-Format')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
input_path = Path(args.input)
|
||||||
|
output_path = Path(args.output)
|
||||||
|
|
||||||
|
run_data = _load_run_data(input_path)
|
||||||
|
analysis_results = analyse_run_data(run_data)
|
||||||
|
|
||||||
|
if not isinstance(analysis_results, dict):
|
||||||
|
raise TypeError('analyse_run_data() muss ein dict zurückgeben.')
|
||||||
|
|
||||||
|
_write_output(output_path, analysis_results)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue