Access magnetic ordering using mp_api

I am trying to get magnetic ordering (e.g. NM, FM, AFM, ). I tried the code below to get material properties.

from pymatgen.analysis.magnetism.analyzer import Ordering
mpids = ['mp-13']
with MPRester(api_key) as mpr:
    fields = ["material_id", "energy_above_hull", "formation_energy_per_atom", 'band_gap', 'magnetic_ordering']
    materials_to_fetch = mat_input
for i, mpid in tqdm(enumerate(mpids), desc="Processing"):
            pstruct = mpr.get_structure_by_material_id(mpid)
            summary = mpr.summary.get_data_by_id(mpid, fields=fields)

Using the code above, I can get materials property, such as energy band gap summary.band_gap. However, I get an error with summary.magnetic_ordering, saying AttributeError: 'MPDataDoc' object has no attribute 'magnetic_ordering'. Does anybody know how I can get magnetic ordering for each material=id?

reference:
https://materialsproject.github.io/api/_autosummary/mp_api.client.routes.materials.summary.SummaryRester.html#mp_api.client.routes.materials.summary.SummaryRester.get_data_by_id

@Ryotaro_Okabe, two things. You can use mpr.materials.summary.search(material_ids=mpids) instead of iterating and calling get_data_by_id. This helps our servers and will be more efficient for you. Second, you can access the magnetic ordering on the summary doc with summary.ordering.

– Jason

Thanks for your reply. Now I run the code with mpr.materials.summary.search.
I have tried summary.ordering. That code did not give error, but the output is None. I have tried materials which I can find magnetic order from MP website (e.g. mp-13, mp-30), but I always see None is returned. Hope to know how I can get Ordering class (or just string objects like ‘NM’, ‘FM’ are fine too).

I just uesd that to get magnetic ordering, actually, it may work with just “ordering”

1 Like

@Ryotaro_Okabe @suk_chen

Yup, you can also query for a specific ordering in the search() method with ordering=...

– Jason

1 Like

Hi all,

I am facing a similar problem when I try to download ferromagnetic materials:

with MPRester(API_KEY) as mpr:
    docs = mpr.materials.summary.search(
        magnetic_ordering='FM'
    )

I have also tried with ‘Ferromagnetic’. What am I doing wrong?

@cwinkler Is this the error you’re getting?

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[9], line 2
----> 2 docs = mpr.materials.summary.search(magnetic_ordering="FM", fields=["material_id", "ordering"])
    337     query_params.update({"is_metal": is_metal})
    339 if magnetic_ordering:
--> 340     query_params.update({"ordering": magnetic_ordering.value})
    342 if has_reconstructed is not None:
    343     query_params.update({"has_reconstructed": has_reconstructed})

AttributeError: 'str' object has no attribute 'value'

If so, the magnetic_ordering argument to search() takes the Ordering enum from pymatgen as input, try:

from pymatgen.analysis.magnetism import Ordering
docs = mpr.materials.summary.search(
    magnetic_ordering=Ordering("FM"),
    fields=["material_id", "ordering"]
)
1 Like

Great thank you this worked!