-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
269 lines (228 loc) · 11.7 KB
/
__init__.py
File metadata and controls
269 lines (228 loc) · 11.7 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Validation Module
This module provides comprehensive validation capabilities for GNN models,
including semantic validation, performance profiling, and consistency checking.
"""
__version__ = "1.6.0"
FEATURES = {
"semantic_validation": True,
"performance_profiling": True,
"consistency_checking": True,
"multi_model_validation": True,
"mcp_integration": True
}
from pathlib import Path
from typing import Any, Dict, List, Optional
from .consistency_checker import ConsistencyChecker, check_consistency
from .performance_profiler import PerformanceProfiler, profile_performance
# Import core validation functionality
from .semantic_validator import SemanticValidator, process_semantic_validation
def process_validation(target_dir: Path, output_dir: Path, verbose: bool = False, **kwargs) -> bool:
"""
Main validation processing function for GNN models.
This function orchestrates the complete validation workflow including:
- Semantic validation
- Performance profiling
- Consistency checking
Args:
target_dir: Directory containing GNN files to validate
output_dir: Output directory for validation results
verbose: Whether to enable verbose logging
**kwargs: Additional processing options
Returns:
True if validation succeeded, False otherwise
"""
import datetime
import json
import logging
from pathlib import Path
# Setup logging
logger = logging.getLogger(__name__)
if verbose:
logger.setLevel(logging.DEBUG)
# Ensure output directory exists
output_dir.mkdir(parents=True, exist_ok=True)
try:
# Load parsed GNN data from previous step (step 3)
from pipeline.config import get_output_dir_for_script
# Look in the base output directory, not the step-specific directory
base_output_dir = Path(output_dir).parent if Path(output_dir).name.startswith(('6_validation', '7_export', '8_visualization')) else output_dir
gnn_output_dir = get_output_dir_for_script("3_gnn.py", base_output_dir)
gnn_results_file = gnn_output_dir / "gnn_processing_results.json"
if not gnn_results_file.exists():
logger.error(f"GNN processing results not found at {gnn_results_file}. Run step 3 first.")
logger.error(f"Expected file location: {gnn_results_file}")
logger.error(f"GNN output directory: {gnn_output_dir}")
logger.error(f"GNN output directory exists: {gnn_output_dir.exists()}")
if gnn_output_dir.exists():
logger.error(f"Contents: {list(gnn_output_dir.iterdir())}")
return False
with open(gnn_results_file, 'r') as f:
gnn_results = json.load(f)
logger.info(f"Loaded {len(gnn_results['processed_files'])} parsed GNN files")
# Load existing validation results if present (accumulate across subdirectory passes)
validation_results_file = output_dir / "validation_results.json"
if validation_results_file.exists():
try:
with open(validation_results_file, 'r') as f:
validation_results = json.load(f)
# Ensure all expected keys exist for safe accumulation
validation_results.setdefault("files_validated", [])
validation_results.setdefault("summary", {})
validation_results["summary"].setdefault("total_files", 0)
validation_results["summary"].setdefault("successful_validations", 0)
validation_results["summary"].setdefault("failed_validations", 0)
validation_results["summary"].setdefault("validation_scores", {})
for k in ("semantic", "performance", "consistency"):
validation_results["summary"]["validation_scores"].setdefault(k, [])
# Update source directory to show accumulated sources
prev_sources = validation_results.get("source_directories", [])
if not prev_sources:
prev_src = validation_results.get("source_directory", "")
prev_sources = [prev_src] if prev_src else []
if str(target_dir) not in prev_sources:
prev_sources.append(str(target_dir))
validation_results["source_directories"] = prev_sources
validation_results["source_directory"] = str(target_dir)
validation_results["timestamp"] = datetime.datetime.now().isoformat()
logger.info(f"Accumulating validation results from {len(validation_results['files_validated'])} previously validated files")
except (json.JSONDecodeError, OSError) as e:
logger.warning(f"Could not load existing validation results, starting fresh: {e}")
validation_results = None
if not validation_results_file.exists():
validation_results = None
if validation_results is None:
validation_results = {
"timestamp": datetime.datetime.now().isoformat(),
"source_directory": str(target_dir),
"source_directories": [str(target_dir)],
"output_directory": str(output_dir),
"files_validated": [],
"summary": {
"total_files": 0,
"successful_validations": 0,
"failed_validations": 0,
"validation_scores": {
"semantic": [],
"performance": [],
"consistency": []
}
}
}
# Process each file
for file_result in gnn_results["processed_files"]:
if not file_result["parse_success"]:
continue
file_name = file_result["file_name"]
logger.info(f"Validating: {file_name}")
# Load the actual parsed GNN specification
parsed_model_file = file_result.get("parsed_model_file")
if parsed_model_file and Path(parsed_model_file).exists():
try:
with open(parsed_model_file, 'r') as f:
actual_gnn_spec = json.load(f)
logger.info(f"Loaded parsed GNN specification from {parsed_model_file}")
model_data = actual_gnn_spec
except Exception as e:
logger.error(f"Failed to load parsed GNN spec from {parsed_model_file}: {e}")
model_data = file_result
else:
logger.warning(f"Parsed model file not found for {file_name}, using summary data")
model_data = file_result
file_validation_result = {
"file_name": file_name,
"file_path": file_result["file_path"],
"validations": {},
"success": True
}
# Perform semantic validation
try:
semantic_result = process_semantic_validation(model_data)
file_validation_result["validations"]["semantic"] = semantic_result
validation_results["summary"]["validation_scores"]["semantic"].append(
semantic_result.get("semantic_score", 0.0)
)
logger.info(f"Semantic validation completed for {file_name}")
except Exception as e:
logger.error(f"Semantic validation failed for {file_name}: {e}")
file_validation_result["success"] = False
# Perform performance profiling
try:
performance_result = profile_performance(model_data)
file_validation_result["validations"]["performance"] = performance_result
validation_results["summary"]["validation_scores"]["performance"].append(
performance_result.get("performance_score", 0.0)
)
logger.info(f"Performance profiling completed for {file_name}")
except Exception as e:
logger.error(f"Performance profiling failed for {file_name}: {e}")
file_validation_result["success"] = False
# Perform consistency checking
try:
consistency_result = check_consistency(model_data)
file_validation_result["validations"]["consistency"] = consistency_result
validation_results["summary"]["validation_scores"]["consistency"].append(
consistency_result.get("consistency_score", 0.0)
)
logger.info(f"Consistency checking completed for {file_name}")
except Exception as e:
logger.error(f"Consistency checking failed for {file_name}: {e}")
file_validation_result["success"] = False
validation_results["files_validated"].append(file_validation_result)
validation_results["summary"]["total_files"] += 1
if file_validation_result["success"]:
validation_results["summary"]["successful_validations"] += 1
else:
validation_results["summary"]["failed_validations"] += 1
# Calculate average scores
for score_type in ["semantic", "performance", "consistency"]:
scores = validation_results["summary"]["validation_scores"][score_type]
if scores:
avg_score = sum(scores) / len(scores)
validation_results["summary"]["validation_scores"][f"avg_{score_type}_score"] = avg_score
# Save validation results
validation_results_file = output_dir / "validation_results.json"
with open(validation_results_file, 'w') as f:
json.dump(validation_results, f, indent=2)
# Save validation summary
validation_summary_file = output_dir / "validation_summary.json"
with open(validation_summary_file, 'w') as f:
json.dump(validation_results["summary"], f, indent=2)
logger.info("Validation processing completed:")
logger.info(f" Total files: {validation_results['summary']['total_files']}")
logger.info(f" Successful validations: {validation_results['summary']['successful_validations']}")
logger.info(f" Failed validations: {validation_results['summary']['failed_validations']}")
if validation_results["summary"]["validation_scores"]["semantic"]:
avg_semantic = validation_results["summary"]["validation_scores"]["avg_semantic_score"]
logger.info(f" Average semantic score: {avg_semantic:.2f}")
if validation_results["summary"]["validation_scores"]["performance"]:
avg_performance = validation_results["summary"]["validation_scores"]["avg_performance_score"]
logger.info(f" Average performance score: {avg_performance:.2f}")
if validation_results["summary"]["validation_scores"]["consistency"]:
avg_consistency = validation_results["summary"]["validation_scores"]["avg_consistency_score"]
logger.info(f" Average consistency score: {avg_consistency:.2f}")
success = validation_results["summary"]["successful_validations"] > 0
return success
except Exception as e:
logger.error(f"Validation processing failed: {e}")
return False
# Re-export main classes and functions
__all__ = [
'__version__',
'FEATURES',
'SemanticValidator',
'PerformanceProfiler',
'ConsistencyChecker',
'process_semantic_validation',
'profile_performance',
'check_consistency',
'process_validation'
]
def get_module_info() -> dict:
"""Return module metadata for composability and MCP discovery."""
return {
"name": "validation",
"version": __version__,
"description": "Advanced validation and consistency checking",
"features": FEATURES,
}