Nearest Neighbour

#1

I am not able to fetch the structure of all “Mn” based elements via their mp-id’s
‘’’
#Code- Python3.6 script
import pandas as pd
import matplotlib.pyplot as plt
import csv
import matplotlib.ticker as ticker
import numpy as np
from pymatgen.analysis.local_env import CrystalNN
from pymatgen import MPRester
m = MPRester(api-key)
df = pd.read_csv(‘f.csv’)
data_id = m.query( criteria={“elements”: {"$all": [“Mn”]}}, properties=[“task_ids”])
c = 0
for i in range(len(data_id)):
id = data_id[i][“task_ids”][0]

print(id)
struct = m.get_structure_by_material_id(id)
c = c+1
print(c)
nn=CrystalNN()
for k, v in nn.get_cn_dict(struct,i).items():
	if k == 'Mn':
		with open('last.csv',a) as f:
			f.write(mpid)
			f.write(i)
			f.write(v)

‘’’
Code is able to run a few of the mp-id’s and for others I am getting the following error:
‘’’
Traceback (most recent call last):
File “script.py”, line 24, in
struct = m.get_structure_by_material_id(id)
File “/home/username/.local/lib/python3.7/site-packages/pymatgen/ext/matproj.py”, line 542, in get_structure_by_material_id
return data[0][prop]
IndexError: list index out of range
‘’’
Anyone having any idea about the error or have any alternate to fetch structure (or first nearest neighbor) of elements, Plz help me !!!

#2

You’re using the wrong field to search for materials. You want to reference task_id not task_ids. Also please don’t hammer the REST API this way. Use the query to grab all the structures you want and then process them.

data = m.query( criteria={“elements”: {"$all": [“Mn”]}}, properties=[“task_id”,"structure"])
for d in data:
	struct = d["structure"]
    ....
1 Like
#3

Thanks for the help and suggestions.