-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processing.py
More file actions
81 lines (69 loc) · 2.5 KB
/
data_processing.py
File metadata and controls
81 lines (69 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import pandas as pd
from typing import List, Tuple, Dict, Any
REQUIRED_BASE_COLUMNS = [
"NodeID","NodeName","NeighborNodeID"
]
OPTIONAL_NUMERIC_GUESSES = ["TransitionCost","WaitingTime"]
def load_dataset(path: str) -> pd.DataFrame:
df = pd.read_csv(path)
# Normalize column names (strip spaces)
df.columns = [c.strip() for c in df.columns]
# Type coercion attempts
for col in ["NodeID","NeighborNodeID"]:
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors='coerce')
for col in df.columns:
if col not in ("NodeName",):
# Try numeric
try:
df[col] = pd.to_numeric(df[col])
except Exception:
pass
return df
def validate_dataset(df: pd.DataFrame) -> Tuple[bool, List[str]]:
errors = []
for col in REQUIRED_BASE_COLUMNS:
if col not in df.columns:
errors.append(f"Missing required column: {col}")
if df['NodeID'].isna().any():
errors.append("Some NodeID values are NaN after coercion.")
# Allow NeighborNodeID NaN for terminal nodes
return len(errors) == 0, errors
def list_numeric_columns(df: pd.DataFrame) -> List[str]:
numeric_cols = []
for col in df.columns:
if col in ("NodeID","NeighborNodeID"):
continue
if pd.api.types.is_numeric_dtype(df[col]):
numeric_cols.append(col)
return numeric_cols
def build_edge_records(df: pd.DataFrame) -> List[Dict[str, Any]]:
records = []
for _, row in df.iterrows():
src = int(row['NodeID']) if not pd.isna(row['NodeID']) else None
dst = row.get('NeighborNodeID', None)
if pd.isna(dst) or dst == '' or dst is None:
continue
dst = int(dst)
rec = {k: row[k] for k in df.columns if k not in ('NodeID','NeighborNodeID')}
rec['source'] = src
rec['target'] = dst
records.append(rec)
return records
def list_nodes(df: pd.DataFrame):
node_info = {}
for _, row in df.iterrows():
nid = int(row['NodeID']) if not pd.isna(row['NodeID']) else None
if nid is None:
continue
node_info.setdefault(nid, {
'name': row.get('NodeName', str(nid))
})
# Also include neighbor-only nodes
for _, row in df.iterrows():
dst = row.get('NeighborNodeID', None)
if pd.isna(dst) or dst == '' or dst is None:
continue
dst = int(dst)
node_info.setdefault(dst, {'name': str(dst)})
return node_info