Repository navigation
Expand file tree
/
Copy pathparser_core.py
More file actions
276 lines (238 loc) · 11.6 KB
/
Copy pathparser_core.py
File metadata and controls
276 lines (238 loc) · 11.6 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
270
271
272
273
274
275
276
"""
parser_core.py
Core chemistry logic and parsing engine using cclib, numpy, pandas, scipy, and scikit-learn.
Handles Gaussian .log/.out extraction, thermochemistry, frequency checks,
conformer Boltzmann weighting, automated error diagnostics, TD-DFT photophysics, and advanced batch cheminformatics analytics.
"""
import os
import numpy as np
import pandas as pd
import cclib
import scipy.stats as stats
from tddft import parse_uv_vis_data, analyze_orbital_transitions
from boltzmann_core import process_conformer_ensemble
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
# Physical constants for unit conversions
HARTREE_TO_KCAL_MOL = 627.509474
HARTREE_TO_EV = 27.211386
KB = 0.0019872041 # Boltzmann constant in kcal/(mol·K)
DEFAULT_TEMP = 298.15 # Room temperature in Kelvin
# Common Gaussian error signatures and recommended fixes
ERROR_RULES = [
{
"pattern": "Convergence failure -- run terminated",
"cause": "SCF electron density failed to converge within default maximum cycles.",
"fix": "Add `SCF=(Xqc,MaxCycle=256)` to your route section to switch to quadratic convergence."
},
{
"pattern": "Number of steps exceeded",
"cause": "Geometry optimization reached the maximum step limit on a flat potential energy surface.",
"fix": "Restart from the final geometry using `Opt=(CalcFC,MaxCycles=150)` or `Geom=AllCheck`."
},
{
"pattern": "FormBX had a problem",
"cause": "Near-linear bond angles (~180°) broke redundant internal coordinate calculations.",
"fix": "Switch coordinate system by adding `Opt=Cartesian` or `Opt=GDIIS` to the route."
},
{
"pattern": "Error termination via Lnk1e",
"cause": "General unexpected runtime termination (check scratch disk space or memory limits %chk/%mem).",
"fix": "Increase memory allocation (`%mem=8GB`) and check available disk space on the node."
}
]
def diagnose_gaussian_error(file_path: str) -> dict:
"""Reads the tail of a failed Gaussian log file to diagnose root causes and suggest fixes."""
try:
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
tail_text = "".join(lines[-40:])
# Check if normal termination occurred
if "Normal termination of Gaussian" in tail_text or any("Normal termination" in line for line in lines[-10:]):
return {
"status": "Success",
"cause": None,
"fix": None
}
# Scan for known error rules
for rule in ERROR_RULES:
if rule["pattern"] in tail_text:
return {
"status": "Failed",
"cause": rule["cause"],
"fix": rule["fix"]
}
return {
"status": "Failed",
"cause": "Unknown error termination detected in log file footer.",
"fix": "Inspect log file tail manually or check resource/memory limitations."
}
except Exception as e:
return {
"status": "Error",
"cause": f"Could not read file: {str(e)}",
"fix": "Ensure file path is valid and accessible."
}
def parse_gaussian_log(file_path: str) -> dict:
"""
Parses a single Gaussian output file using cclib and extracts
energies, HOMO/LUMO, dipole moments, vibrational frequencies, and TD-DFT properties.
"""
# 1. Check if the job crashed before trying to parse properties
diag = diagnose_gaussian_error(file_path)
if diag["status"] == "Failed":
return {
"file_name": os.path.basename(file_path),
"status": "FAILED",
"error_cause": diag["cause"],
"suggested_fix": diag["fix"]
}
try:
data = cclib.io.ccread(file_path)
if data is None:
return {"file_name": os.path.basename(file_path), "status": "ERROR", "message": "cclib failed to parse output."}
# 2. Electronic Energy (Hartree)
scf_energy_hartree = None
if hasattr(data, "scfenergies") and len(data.scfenergies) > 0:
scf_energy_hartree = float(data.scfenergies[-1]) / HARTREE_TO_EV
# 3. Thermochemistry (ZPE, Enthalpy, Gibbs Free Energy)
zpe = float(getattr(data, "zpve", 0.0))
enthalpy = float(getattr(data, "enthalpy", 0.0))
free_energy = float(getattr(data, "freeenergy", 0.0))
# 4. HOMO / LUMO and Gap (eV)
homo_energy = None
lumo_energy = None
gap_ev = None
if hasattr(data, "homos") and hasattr(data, "moenergies"):
homo_idx = data.homos[0]
mo_energies = data.moenergies[0]
if homo_idx < len(mo_energies) - 1:
homo_energy = float(mo_energies[homo_idx])
lumo_energy = float(mo_energies[homo_idx + 1])
gap_ev = float(lumo_energy - homo_energy)
# 5. Dipole Moment (Debye)
total_dipole = None
if hasattr(data, "moments") and len(data.moments) > 1:
dipole_vector = data.moments[1] # [x, y, z] dipole vector
total_dipole = float(np.linalg.norm(dipole_vector))
# 6. Vibrational Frequency Stability Check
freq_status = "NOT CHECKED"
imaginary_count = 0
if hasattr(data, "vibfreqs") and data.vibfreqs is not None:
freqs = data.vibfreqs
imaginary_freqs = [f for f in freqs if f < 0]
imaginary_count = len(imaginary_freqs)
if imaginary_count == 0:
freq_status = "PASS: True Local Minimum (0 imaginary frequencies)"
elif imaginary_count == 1:
freq_status = f"WARNING: Transition State / Saddle Point (1 imaginary freq: {imaginary_freqs[0]:.1f} cm^-1)"
else:
freq_status = f"FAIL: Higher-Order Saddle Point ({imaginary_count} imaginary freqs)"
# 7. TD-DFT Photophysics Extraction
tddft_summary = parse_uv_vis_data(data)
transitions_breakdown = analyze_orbital_transitions(data) if tddft_summary["has_tddft"] else []
return {
"file_name": os.path.basename(file_path),
"status": "SUCCESS",
"scf_energy_hartree": scf_energy_hartree,
"zpe_hartree": zpe if zpe != 0.0 else None,
"enthalpy_hartree": enthalpy if enthalpy != 0.0 else None,
"free_energy_hartree": free_energy if free_energy != 0.0 else None,
"homo_ev": homo_energy,
"lumo_ev": lumo_energy,
"homo_lumo_gap_ev": gap_ev,
"dipole_moment_debye": total_dipole,
"frequency_status": freq_status,
"imaginary_freq_count": imaginary_count,
"has_tddft": tddft_summary["has_tddft"],
"lambda_max_nm": tddft_summary.get("lambda_max_nm"),
"max_oscillation_strength": tddft_summary.get("max_oscillator_strength"),
"transitions": tddft_summary.get("transitions", []),
"transitions_breakdown": transitions_breakdown
}
except Exception as e:
return {
"file_name": os.path.basename(file_path),
"status": "PARSER_ERROR",
"message": str(e)
}
def rank_conformers(file_paths: list, temperature: float = DEFAULT_TEMP, energy_window_kcal: float = 3.0) -> tuple:
"""
Parses a batch of conformer log files and delegates ensemble statistical mechanics
to boltzmann_core.py.
"""
results = [parse_gaussian_log(path) for path in file_paths]
return process_conformer_ensemble(results, temperature=temperature, energy_window_kcal=energy_window_kcal)
def run_batch_cheminformatics_report(file_paths: list, n_clusters: int = 2) -> dict:
"""
Executes advanced batch cheminformatics analytics across a list of Gaussian log files:
1. Outlier Detection (Z-Score threshold > 2.5)
2. Feature Correlation Matrix
3. Principal Component Analysis (PCA) for structural variance reduction
4. K-Means Unsupervised Clustering (Grouping electronic families)
"""
results = [parse_gaussian_log(path) for path in file_paths]
df = pd.DataFrame(results)
successful_df = df[df["status"] == "SUCCESS"].copy()
failed_df = df[df["status"] != "SUCCESS"].copy()
# Dynamic minimum cluster check based on available successful logs
min_required = max(2, n_clusters)
if len(successful_df) < min_required:
return {
"error": f"Insufficient successful log files ({len(successful_df)}) for advanced statistical analysis. Minimum required: {min_required}.",
"failed_runs_summary": failed_df[["file_name", "status"]].to_dict(orient="records") if not failed_df.empty else []
}
# Descriptors used for machine learning and statistical modeling
feature_cols = ["homo_lumo_gap_ev", "dipole_moment_debye", "free_energy_hartree", "scf_energy_hartree"]
# Filter out columns that might be entirely NaN or missing
active_features = [col for col in feature_cols if col in successful_df.columns and successful_df[col].notna().sum() > 0]
if len(active_features) < 2:
return {"error": "Not enough valid numeric features extracted across logs to run multi-variable analytics."}
X = successful_df[active_features].copy()
for col in active_features:
X[col] = X[col].fillna(X[col].mean())
# Standardize data (mean=0, variance=1)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 1. Outlier Detection (Z-Score > 2.5 with safety for zero-variance)
z_scores = np.abs(stats.zscore(X, nan_policy='omit'))
z_scores = np.nan_to_num(z_scores, nan=0.0)
outlier_mask = (z_scores > 2.5).any(axis=1) if len(X) > 2 else np.zeros(len(X), dtype=bool)
successful_df["is_outlier"] = outlier_mask
outliers = successful_df[outlier_mask]["file_name"].tolist()
# 2. Correlation Matrix
corr_matrix = successful_df[active_features].corr().to_dict()
# 3. Principal Component Analysis (PCA - max 2 components or number of features)
n_components = min(2, len(active_features))
pca = PCA(n_components=n_components)
pca_coords = pca.fit_transform(X_scaled)
successful_df["pca_pc1"] = pca_coords[:, 0]
successful_df["pca_pc2"] = pca_coords[:, 1] if n_components > 1 else 0.0
explained_variance = pca.explained_variance_ratio_.tolist()
# 4. K-Means Clustering (adjust clusters if batch size is small)
actual_clusters = min(n_clusters, len(successful_df))
kmeans = KMeans(n_clusters=actual_clusters, random_state=42, n_init=10)
successful_df["cluster_id"] = kmeans.fit_predict(X_scaled)
return {
"batch_summary": {
"total_files_evaluated": len(file_paths),
"successful_jobs": len(successful_df),
"failed_jobs": len(failed_df)
},
"outlier_analysis": {
"flagged_outliers": outliers,
"detection_criteria": "Z-score magnitude > 2.5 across quantum descriptors."
},
"correlation_matrix": corr_matrix,
"pca_analysis": {
"explained_variance_ratio": {
"PC1": round(explained_variance[0], 4),
"PC2": round(explained_variance[1], 4) if len(explained_variance) > 1 else 0.0
},
"coordinates": successful_df[["file_name", "pca_pc1", "pca_pc2", "cluster_id"]].to_dict(orient="records")
},
"clustering_analysis": {
"clusters": successful_df[["file_name", "cluster_id", "homo_lumo_gap_ev", "dipole_moment_debye"]].to_dict(orient="records")
}
}