CSD_non_disordered / process_cif.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
from multiprocessing import Pool, cpu_count
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")]
def process_file(file_name):
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:
return f"Error processing file {file_name}: {e}"
return f"Successfully processed {file_name}"
if __name__ == "__main__":
# Use a Pool to process files in parallel
num_workers = cpu_count() # Use all available CPUs
with Pool(processes=num_workers) as pool:
# Use tqdm for a progress bar
results = list(tqdm(pool.imap(process_file, cif_files), total=len(cif_files), desc="Processing CIF files"))
# Print results
for result in results:
if "Error" in result:
print(result)