Add affinity_effect_calculator/src/affinity_effect_calculator/cli.py
This commit is contained in:
parent
5fd91018ab
commit
0fa1c18c8b
1 changed files with 76 additions and 0 deletions
|
|
@ -0,0 +1,76 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from affinity_effect_calculator import core
|
||||||
|
|
||||||
|
|
||||||
|
def _load_effects(input_path: Path) -> Dict[str, float]:
|
||||||
|
if not input_path.exists() or not input_path.is_file():
|
||||||
|
raise FileNotFoundError(f"Input file not found: {input_path}")
|
||||||
|
with input_path.open('r', encoding='utf-8') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise ValueError("Input JSON must be an object with effect_4x and effect_2x fields.")
|
||||||
|
if "effect_4x" not in data or "effect_2x" not in data:
|
||||||
|
raise ValueError("JSON missing required fields: 'effect_4x' and 'effect_2x'.")
|
||||||
|
try:
|
||||||
|
effect_4x = float(data["effect_4x"])
|
||||||
|
effect_2x = float(data["effect_2x"])
|
||||||
|
except (TypeError, ValueError) as e:
|
||||||
|
raise ValueError("Invalid numeric values for effect_4x or effect_2x.") from e
|
||||||
|
return {"effect_4x": effect_4x, "effect_2x": effect_2x}
|
||||||
|
|
||||||
|
|
||||||
|
def _save_result(output_path: Path, result_data: Dict[str, Any]) -> None:
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with output_path.open('w', encoding='utf-8') as f:
|
||||||
|
json.dump(result_data, f, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Berechnet den Affinitäts-Interaktionseffekt basierend auf 4× und 2× Lastwerten."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--input",
|
||||||
|
required=True,
|
||||||
|
help="Pfad zur JSON-Datei mit Eingabewerten."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--output",
|
||||||
|
required=False,
|
||||||
|
default="output/result.json",
|
||||||
|
help="Pfad zur Ergebnisdatei (Standard: output/result.json)."
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
input_path = Path(args.input)
|
||||||
|
output_path = Path(args.output)
|
||||||
|
|
||||||
|
try:
|
||||||
|
input_data = _load_effects(input_path)
|
||||||
|
calculated_effect = core.calculate_affinity_effect(
|
||||||
|
effect_4x=input_data["effect_4x"],
|
||||||
|
effect_2x=input_data["effect_2x"]
|
||||||
|
)
|
||||||
|
|
||||||
|
result = {
|
||||||
|
"effect_4x": input_data["effect_4x"],
|
||||||
|
"effect_2x": input_data["effect_2x"],
|
||||||
|
"calculated_effect": calculated_effect,
|
||||||
|
}
|
||||||
|
|
||||||
|
print(f"Calculated interaction effect: {calculated_effect}")
|
||||||
|
|
||||||
|
_save_result(output_path, result)
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"Error: {exc}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue