Duplicate Cif files for material project

Hi, I am trying to get cif files of the structure based on the condition of my query search. However, when I get the results I see different cif files with different space groups for the same structure. I am wondering why that happens? why it doesn’t grab the first structure(the one that shows up in material Project explorer website)?
def query_materials_project(elements, chemsys, api_key=environ[“PMG_API_KEY”], output_directory=“testing_dupliacte_cif”):

with MPRester(api_key=api_key) as mpr:
    docs = mpr.materials.search(elements=elements,
    chemsys=chemsys,
        fields=["material_id", "initial_structures"])

#the output array which saves all the structures
results = []

for d in docs:
    initial_structures = d.initial_structures # Access "initial_structures" property
    if initial_structures:
        for i, structure in enumerate(initial_structures):

            result = Structure2(
            pmg_structure = structure,
            material_id = d.material_id
        )
            results.append(result)
            output_file = f"{output_directory}/{d.material_id}_structure{i + 1}.cif"
            save_structure_to_cif(structure, output_file)


return results




    
#Nm: this function saves the cif files

def save_structure_to_cif(structure, output_file):
output_directory = os.path.dirname(output_file)
os.makedirs(output_directory, exist_ok=True)
with open(output_file, “w”) as f:
cif_writer = CifWriter(structure,symprec=0.1)
cif_writer.write_file(f.name)

My Output looks like this:

By getting initial_structures you are asking for the input structures for all calculations associated with that material ID. Instead, just ask for the structure field and pass that data to CIF writer.

– Jason

I tried running with “structure” field but I am having issues. This is my updated code;

def query_materials_project(elements, chemsys, api_key=environ["PMG_API_KEY"], output_directory="testing_dupliacte_cif"):


    with MPRester(api_key=api_key) as mpr:
        docs = mpr.materials.search(elements=elements,
        chemsys=chemsys,
            fields=["material_id", "structure"])
        

    results = []

    for d in docs:

        structures = d.structure
        if structures:
            for i, structure in enumerate(structures):

                result = Structure2(
                pmg_structure = structure,
                material_id = d.material_id
            )
                results.append(result)
                output_file = f"{output_directory}/{d.material_id}_structure{i + 1}.cif"
                save_structure_to_cif(structure, output_file)


        return results
def save_structure_to_cif(structure, output_file):
    output_directory = os.path.dirname(output_file)
    os.makedirs(output_directory, exist_ok=True)
    with open(output_file, "w") as f:
        cif_writer = CifWriter(structure,symprec=0.1)
        cif_writer.write_file(f.name)       

The error I get:

AttributeError: attr='site_properties' not found on PeriodicSite

@Nadia_Mehjabin please include an error message or describe the actual issue. Thanks!

I cleaned my code a little bit and have the still issue. I am wondering if it’s beacuse writing CIF for the structure.

import os
from mp_api.client import MPRester
from emmet.core.utils import jsanitize
from pathlib import Path
from pymatgen.io.cif import CifWriter
from pymatgen.core.structure import Structure

output_directory = "demo"

class Structure2:
    def __init__(
        self,
        pmg_structure,
        
        material_id=None,
      
    ):
        self._pmg_structure = pmg_structure
        
        self._material_id = material_id
        
        
        
def save_structure_to_cif(structure, output_file):
    output_directory = os.path.dirname(output_file)
    os.makedirs(output_directory, exist_ok=True)
    
    with open(output_file, "w") as f:
        cif_writer = CifWriter(structure,symprec=0.1)
        cif_writer.write_file(f.name)

        
        
with MPRester(api_key="My_API_KEY") as mpr:
        docs = mpr.materials.search(
            elements=["Pb", "S"],
            chemsys="Pb-S",
            fields=["material_id", "structure"])

results = []

for d in docs:

    structures = d.structure
    if structures:
        for i, structure in enumerate(structures):

            result = Structure2(
            pmg_structure = structure,
            material_id = d.material_id
        )
            results.append(result)
            output_file = Path(output_directory) / f"{d.material_id}_structure{i+1}.cif"
            save_structure_to_cif(structure, output_file)
            

return results
     

The error:

AttributeError                            Traceback (most recent call last)
<ipython-input-33-b71dfc4e170c> in <module>
     53             results.append(result)
     54             output_file = Path(output_directory) / f"{d.material_id}_structure{i+1}.cif"
---> 55             save_structure_to_cif(structure, output_file)
     56 
     57 

<ipython-input-33-b71dfc4e170c> in save_structure_to_cif(structure, output_file)
     28 
     29     with open(output_file, "w") as f:
---> 30         cif_writer = CifWriter(structure,symprec=0.1)
     31         cif_writer.write_file(f.name)
     32 

~/.local/lib/python3.8/site-packages/pymatgen/io/cif.py in __init__(self, struct, symprec, write_magmoms, significant_figures, angle_tolerance, refine_struct)
   1243         spacegroup = ("P 1", 1)
   1244         if symprec is not None:
-> 1245             sf = SpacegroupAnalyzer(struct, symprec, angle_tolerance=angle_tolerance)
   1246             spacegroup = (sf.get_space_group_symbol(), sf.get_space_group_number())
   1247 

~/.local/lib/python3.8/site-packages/pymatgen/symmetry/analyzer.py in __init__(self, structure, symprec, angle_tolerance)
     74         self._angle_tol = angle_tolerance
     75         self._structure = structure
---> 76         self._site_props = structure.site_properties
     77         unique_species: list[Element | Species] = []
     78         zs = []

~/.local/lib/python3.8/site-packages/pymatgen/core/sites.py in __getattr__(self, attr)
     76         if attr in props:
     77             return props[attr]
---> 78         raise AttributeError(f"{attr=} not found on {type(self).__name__}")
     79 
     80     @property

AttributeError: attr='site_properties' not found on PeriodicSite

This looks like a simple coding error. You’re looping over the sites of the structure and then are trying to write a CIF for each site. Remove the loop over structures (and most of the rest of the code), and save d.structure directly:

output_file = Path(output_directory) / f"{d.material_id}_structure.cif"
save_structure_to_cif(d.structure, output_file)

Thank you! I appreciate it. It worked.