-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_field_mapping.py
More file actions
154 lines (128 loc) · 5.2 KB
/
debug_field_mapping.py
File metadata and controls
154 lines (128 loc) · 5.2 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
#!/usr/bin/env python3
"""
Debug script to test field mapping functionality
"""
import pandas as pd
import sys
import os
# Add the current directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_field_mapping():
"""Test the field mapping functionality"""
print("=== Testing Field Mapping ===")
# Create a test DataFrame with some fields
test_data = {
'element_1': ['Al', 'Ti', 'Fe'],
'element_2': ['Ni', 'Cu', 'Co'],
'composition_1': [0.5, 0.6, 0.7],
'composition_2': [0.5, 0.4, 0.3],
'formula': ['Al0.5Ni0.5', 'Ti0.6Cu0.4', 'Fe0.7Co0.3'],
'melting_point': [1000, 1200, 1400],
'density': [2.7, 4.5, 7.8],
'electronegativity': [1.5, 1.8, 1.9],
'atomic_radius': [1.43, 1.45, 1.25],
'is_generated': [True, True, True]
}
df = pd.DataFrame(test_data)
print(f"Original DataFrame columns: {list(df.columns)}")
print(f"Original DataFrame shape: {df.shape}")
print("Original DataFrame:")
print(df)
print()
# Test centralized field mapping
try:
from centralized_field_mapping import apply_field_mapping_to_generation, REQUIRED_FIELDS
print("Testing centralized field mapping...")
# Log initial state
from centralized_field_mapping import log_dataframe_state
log_dataframe_state(df, "Before field mapping")
# Apply field mapping
result_df = apply_field_mapping_to_generation(df)
print(f"After field mapping columns: {list(result_df.columns)}")
print(f"After field mapping shape: {result_df.shape}")
print("After field mapping:")
print(result_df)
print()
# Check required fields
print("Required fields status:")
for field in REQUIRED_FIELDS:
if field in result_df.columns:
print(f" ✓ {field}: {result_df[field].dtype}, min={result_df[field].min():.3f}, max={result_df[field].max():.3f}")
else:
print(f" ✗ {field}: MISSING")
return True
except Exception as e:
print(f"Error in centralized field mapping: {e}")
import traceback
traceback.print_exc()
# Test manual field mapping
print("\nTesting manual field mapping fallback...")
try:
print("Manual field mapping test completed")
return True
except Exception as e2:
print(f"Error in manual field mapping: {e2}")
import traceback
traceback.print_exc()
return False
def test_ml_prediction():
"""Test ML prediction with field mapping"""
print("\n=== Testing ML Prediction ===")
try:
# Create test data with required fields
test_data = {
'element_1': ['Al', 'Ti', 'Fe'],
'element_2': ['Ni', 'Cu', 'Co'],
'composition_1': [0.5, 0.6, 0.7],
'composition_2': [0.5, 0.4, 0.3],
'formula': ['Al0.5Ni0.5', 'Ti0.6Cu0.4', 'Fe0.7Co0.3'],
'melting_point': [1000, 1200, 1400],
'density': [2.7, 4.5, 7.8],
'electronegativity': [1.5, 1.8, 1.9],
'atomic_radius': [1.43, 1.45, 1.25],
'formation_energy_per_atom': [-1.2, -0.8, -1.5],
'energy_above_hull': [0.02, 0.05, 0.01],
'band_gap': [0.0, 0.1, 0.0],
'nsites': [2, 2, 2],
'is_generated': [True, True, True]
}
df = pd.DataFrame(test_data)
print(f"Test DataFrame columns: {list(df.columns)}")
print(f"Test DataFrame shape: {df.shape}")
# Test ML classifier
from synthesizability_predictor import SynthesizabilityClassifier
ml_classifier = SynthesizabilityClassifier()
# Create dummy training data
from synthesizability_predictor import create_vae_training_dataset_from_mp
try:
# Try to create real training data
dataset = create_vae_training_dataset_from_mp(api_key='test', n_materials=10)
except Exception:
# Use synthetic data
from gradio_app import create_synthetic_dataset
dataset = create_synthetic_dataset(10)
print(f"Training dataset prepared with {len(dataset)} rows")
# Train the classifier
ml_metrics = ml_classifier.train(api_key='test')
print(f"ML classifier trained: {ml_metrics}")
# Test prediction
results = ml_classifier.predict(df)
print(f"ML prediction results shape: {results.shape}")
print(f"ML prediction results columns: {list(results.columns)}")
print("ML prediction results:")
print(results)
return True
except Exception as e:
print(f"Error in ML prediction test: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
print("Starting field mapping debug tests...")
success1 = test_field_mapping()
success2 = test_ml_prediction()
if success1 and success2:
print("\n✅ All tests passed!")
else:
print("\n❌ Some tests failed!")
sys.exit(1)