Error, 'MPDataDoc' object is not subscriptable',

Trying to make a ML model to predict Formation energy from structure
I am beginner in this area any help is really appreciated ,thank you
Following are the codes that i have used

results=mpr.summary.search(formula=[“ABC3”],fields=[“structure”,“formation_energy_per_atom”],band_gap=(1,4))

mean_atomic_numbers=[]
max_atomic_numbers=[]
min_atomic_numbers=[]
std_atomic_numbers=[]

a_parameters=[]
b_parameter=[]
c_parameters=[]

alpha_parameters=[]
beta_parameters=[]
gamma_parameters=[]

mean_distance_matrix=[]
max_distance_numbers=[]
min_distance_numbers=[]
std_distance_numbers=[]

formation_energy_per_atom=[]

for r in results:
poscar=r[“poscar”]
FE= r[“formation_energy_per_atom”]
parser =CifParser.from_string(cif)

structure=parser.get_structure()
structure=structure[0]

mean_atomic_numbers +=[np.mean(structure.atomic_numbers)]
max_atomic_numbers  +=[np.max(structure.atomic_numbers)]
min_atomic_numbers  +=[np.min(structure.atomic_numbers)]
std_atomic_numbers  +=[np.std(structure.atomic_numbers)]

a_parameters +=[structure.lattice.abc[0]]
b_parameters +=[structure.lattice.abc[1]]
c_parameters +=[structure.lattice.abc[2]]
alpha_parameters +=[structure.lattice.angles[0]]
beta_parameters +=[structure.lattice.angles[1]]
gamma_parameters +=[structure.lattice.angles[2]]

mean_distribution_matrix +=[np.mean(structure.distance_matrix)]
max_distribution_matrix +=[np.max(structure.distance_matrix)]
min_distribution_matrix +=[np.min(structure.distance_matrix)]
std_distribution_matrix +=[np.std(structure.distance_matrix)]

formation_energy_per_atom += [FE]

                TypeError                                 Traceback (most recent call last)

Cell In[13], line 2
1 for r in results:
----> 2 poscar=r[“poscar”]
3 FE= r[“formation_energy_per_atom”]
4 parser =CifParser.from_string(cif)

TypeError: ‘MPDataDoc’ object is not subscriptable

You can either access the data as an attribute (r.formation_energy_per_atom), or first convert to a dictionary if you want to access it as you are (r.dict()).

– Jason

Thank you for your message ,
can we get the the following request as cif format .
i have been trying for a few days to convert this search_result into a dictionary or cif,

search_results=mpr.summary.search(formula=[“ABC3”],fields=[“structure”,“formation_energy_per_atom”],band_gap=(1,4))

can you please give me example of code to convert it into dictionary/cif
Thankyou

See this post: How do you access the CIF data in the new API? - #3 by Luke

– Jason

yeah I got it , many thanks

Hi, Amal I’m trying to predict the bandgap of the material and faced the same problem. If you have solution can you help me?

initialize a empty list,
create a empty dictionary
iterate over each item from your “search_results” (obtained form your api search, for i in search_results)
add them to your dictionary
eg:- data_dict[‘structure’]=i.structure
similarly for composition,

finaly u can convert the list into a dataframe

hope this helps