|
|
|
|
|
import pandas as pd |
|
|
import os |
|
|
from tqdm import tqdm |
|
|
|
|
|
|
|
|
csv_path = 'formed.csv' |
|
|
xyz_folder = 'XYZ_FORMED' |
|
|
|
|
|
|
|
|
df = pd.read_csv(csv_path) |
|
|
|
|
|
|
|
|
df = df[['name', 'gap']] |
|
|
|
|
|
|
|
|
def load_xyz(mol_name): |
|
|
file_path = os.path.join(xyz_folder, mol_name + '.xyz') |
|
|
if not os.path.exists(file_path): |
|
|
print(f"File not found: {file_path}") |
|
|
return None |
|
|
try: |
|
|
with open(file_path, 'r') as f: |
|
|
xyz_data = f.read() |
|
|
return clean_xyz(xyz_data) |
|
|
except Exception as e: |
|
|
print(f"Error reading {file_path}: {e}") |
|
|
return None |
|
|
|
|
|
|
|
|
def clean_xyz(xyz_str): |
|
|
|
|
|
xyz_str = xyz_str.replace('"', '').strip() |
|
|
|
|
|
|
|
|
xyz_lines = xyz_str.splitlines() |
|
|
|
|
|
|
|
|
if len(xyz_lines) >= 2: |
|
|
xyz_lines = xyz_lines[2:] |
|
|
|
|
|
|
|
|
cleaned_data = [] |
|
|
for line in xyz_lines: |
|
|
parts = line.split() |
|
|
if len(parts) == 4: |
|
|
cleaned_data.append(" ".join(parts[0:4])) |
|
|
|
|
|
|
|
|
return "\n".join(cleaned_data) |
|
|
|
|
|
|
|
|
tqdm.pandas() |
|
|
df['xyz'] = df['name'].progress_apply(load_xyz) |
|
|
|
|
|
|
|
|
print(f"Number of missing XYZ values: {df['xyz'].isnull().sum()}") |
|
|
|
|
|
|
|
|
df.to_csv('formed_xyz_combined.csv', index=False) |
|
|
|