CSD_non_disordered / clean.py
vickie02736's picture
Add files using upload-large-folder tool
678206c verified
import os
from tqdm import tqdm
from ase.io import read, write
from pymatgen.io.cif import CifParser
import warnings
warnings.filterwarnings("ignore")
# Define your input and output folders
input_folder = "/users/PAS2490/marcusshen/cmame/Data/CSD_non_disordered/CSD_non_disordered/"
output_folder_ase = "/users/PAS2490/marcusshen/cmame/Data/CSD_non_disordered/CSD_non_disordered/cleaned_ase/"
output_folder_pymatgen = "/users/PAS2490/marcusshen/cmame/Data/CSD_non_disordered/CSD_non_disordered/cleaned_pymatgen/"
# Create output directories if they do not exist
os.makedirs(output_folder_ase, exist_ok=True)
os.makedirs(output_folder_pymatgen, exist_ok=True)
# Get the list of CIF files in the input folder
cif_files = [f for f in os.listdir(input_folder) if f.endswith(".cif")]
# Iterate through all files in the input folder
for file_name in tqdm(cif_files, desc="Processing CIF files"):
if file_name.endswith(".cif"): # Process only CIF files
input_path = os.path.join(input_folder, file_name)
ase_output_path = os.path.join(output_folder_ase, file_name)
pymatgen_output_path = os.path.join(output_folder_pymatgen, file_name)
try:
# Step 1: Use ASE to clean the structure and save to output folder
structure = read(input_path)
write(ase_output_path, structure)
# Step 2: Use Pymatgen to further clean and save the structure
parser = CifParser(ase_output_path)
structure_pymatgen = parser.get_structures()[0] # Extract the first structure
structure_pymatgen.to(filename=pymatgen_output_path)
except Exception as e:
print(f"Error processing file {file_name}: {e}")