Update f1_edges_main.py
Browse files- f1_edges_main.py +22 -9
f1_edges_main.py
CHANGED
|
@@ -7,9 +7,29 @@ from pathlib import Path
|
|
| 7 |
from typing import List, Dict, Set, Tuple
|
| 8 |
import numpy as np
|
| 9 |
from collections import Counter
|
| 10 |
-
from
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def main():
|
| 14 |
parser = argparse.ArgumentParser(description="Calculate edge F1 metrics (TP/FP/FN/P/R/F1) for topology JSON.")
|
| 15 |
parser.add_argument("--gen", type=Path, nargs="+", required=True,
|
|
@@ -29,7 +49,6 @@ def main():
|
|
| 29 |
args.output = results_dir / args.output
|
| 30 |
|
| 31 |
global LOG_FILE
|
| 32 |
-
# Try to open the log file
|
| 33 |
try:
|
| 34 |
LOG_FILE = open(args.output, "w", encoding="utf-8")
|
| 35 |
custom_print(f"{C.OKGREEN}✅ Detailed output saved to: {args.output}{C.ENDC}")
|
|
@@ -42,12 +61,10 @@ def main():
|
|
| 42 |
current_ref_path = None
|
| 43 |
ref_nodes = []
|
| 44 |
|
| 45 |
-
# Collect all generated file paths
|
| 46 |
gen_files = []
|
| 47 |
for p in args.gen:
|
| 48 |
path = Path(p)
|
| 49 |
if path.is_dir():
|
| 50 |
-
# Ne traiter que les fichiers du dossier principal, pas des sous-dossiers
|
| 51 |
for json_file in sorted(path.glob("*.json")):
|
| 52 |
gen_files.append(json_file)
|
| 53 |
elif path.is_file() and path.suffix.lower() == ".json":
|
|
@@ -97,7 +114,7 @@ def main():
|
|
| 97 |
# Check if devices list is empty
|
| 98 |
gen_devices = gen_data.get("result", {}).get("network_topology", {}).get("devices", [])
|
| 99 |
if len(gen_devices) == 0:
|
| 100 |
-
custom_print(f"{C.WARNING}
|
| 101 |
custom_print(f"{C.FAIL}Generated topology is empty, no mapping processing will be performed{C.ENDC}")
|
| 102 |
|
| 103 |
# Calculate FN = total connections in the reference
|
|
@@ -496,7 +513,3 @@ def main():
|
|
| 496 |
|
| 497 |
if __name__ == "__main__":
|
| 498 |
main()
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
# sudo systemctl stop ollama
|
| 502 |
-
# OLLAMA_FLASH_ATTENTION=1 ollama serve
|
|
|
|
| 7 |
from typing import List, Dict, Set, Tuple
|
| 8 |
import numpy as np
|
| 9 |
from collections import Counter
|
| 10 |
+
from f1_edges_functions import*
|
| 11 |
|
| 12 |
|
| 13 |
+
|
| 14 |
+
def _normalize_name(name: str) -> str:
|
| 15 |
+
return re.sub(r'[^a-z0-9]', '', str(name).lower())
|
| 16 |
+
|
| 17 |
+
def _normalize_if_type(if_name: str) -> str:
|
| 18 |
+
n = if_name.lower()
|
| 19 |
+
if 'se' in n: return "Serial"
|
| 20 |
+
if 'eth' in n: return "Ethernet"
|
| 21 |
+
return "Ethernet"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def custom_print(message: str = "", end: str = "\n"):
|
| 25 |
+
global LOG_FILE
|
| 26 |
+
full_message = message + end
|
| 27 |
+
if LOG_FILE:
|
| 28 |
+
LOG_FILE.write(full_message)
|
| 29 |
+
LOG_FILE.flush()
|
| 30 |
+
sys.stdout.write(full_message)
|
| 31 |
+
sys.stdout.flush()
|
| 32 |
+
|
| 33 |
def main():
|
| 34 |
parser = argparse.ArgumentParser(description="Calculate edge F1 metrics (TP/FP/FN/P/R/F1) for topology JSON.")
|
| 35 |
parser.add_argument("--gen", type=Path, nargs="+", required=True,
|
|
|
|
| 49 |
args.output = results_dir / args.output
|
| 50 |
|
| 51 |
global LOG_FILE
|
|
|
|
| 52 |
try:
|
| 53 |
LOG_FILE = open(args.output, "w", encoding="utf-8")
|
| 54 |
custom_print(f"{C.OKGREEN}✅ Detailed output saved to: {args.output}{C.ENDC}")
|
|
|
|
| 61 |
current_ref_path = None
|
| 62 |
ref_nodes = []
|
| 63 |
|
|
|
|
| 64 |
gen_files = []
|
| 65 |
for p in args.gen:
|
| 66 |
path = Path(p)
|
| 67 |
if path.is_dir():
|
|
|
|
| 68 |
for json_file in sorted(path.glob("*.json")):
|
| 69 |
gen_files.append(json_file)
|
| 70 |
elif path.is_file() and path.suffix.lower() == ".json":
|
|
|
|
| 114 |
# Check if devices list is empty
|
| 115 |
gen_devices = gen_data.get("result", {}).get("network_topology", {}).get("devices", [])
|
| 116 |
if len(gen_devices) == 0:
|
| 117 |
+
custom_print(f"{C.WARNING}Empty devices list detected - format with devices: []{C.ENDC}")
|
| 118 |
custom_print(f"{C.FAIL}Generated topology is empty, no mapping processing will be performed{C.ENDC}")
|
| 119 |
|
| 120 |
# Calculate FN = total connections in the reference
|
|
|
|
| 513 |
|
| 514 |
if __name__ == "__main__":
|
| 515 |
main()
|
|
|
|
|
|
|
|
|
|
|
|