Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

label in Structure.as_dict() is None in the latest version #3166

Closed
njzjz opened this issue Jul 19, 2023 · 3 comments · Fixed by #3169
Closed

label in Structure.as_dict() is None in the latest version #3166

njzjz opened this issue Jul 19, 2023 · 3 comments · Fixed by #3169
Labels
bug core Pymatgen core

Comments

@njzjz
Copy link
Contributor

njzjz commented Jul 19, 2023

Description

The latest version gives the 'label': None in Structure.as_dict(). I want to confirm if it is expected, as it breaks the tests in our downstream package.

Repro

from pymatgen.core import Structure
import numpy as np
s = Structure(np.eye(3), ["H"], np.zeros((1, 3)), coords_are_cartesian=True)
print(s.as_dict())
{'@module': 'pymatgen.core.structure', '@class': 'Structure', 'charge': 0, 'lattice': {'matrix': [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], 'pbc': (True, True, True), 'a': 1.0, 'b': 1.0, 'c': 1.0, 'alpha': 90.0, 'beta': 90.0, 'gamma': 90.0, 'volume': 1.0}, 'sites': [{'species': [{'element': 'H', 'occu': 1}], 'abc': [0.0, 0.0, 0.0], 'xyz': [0.0, 0.0, 0.0], 'label': None, 'properties': {}}]}

Expected behavior

In previous versions, label is H.

Environment

Relevant versions and platform info:

  • OS: Linux
  • Version 2023.7.17

Additional context

@janosh
Copy link
Member

janosh commented Jul 19, 2023

@njzjz Thanks for reporting. This is not intended.

This was caused by #3135. @stefsmeets Could you take a look? label should still be H here.

@stefsmeets
Copy link
Contributor

stefsmeets commented Jul 20, 2023

Thanks for pinging me!

I added Site.label is a new attribute (default None), so it was never 'H' to begin with unless explicitly set by the user or any code that uses it. So this behaviour is as expected (or at least the way intended).

I can submit a PR to set the default to the species name if that is more sensible.

@stefsmeets
Copy link
Contributor

stefsmeets commented Jul 20, 2023

Ah, I found the culprit:

if verbosity > 0:
dct["xyz"] = [float(c) for c in self.coords]
dct["label"] = self.species_string
dct["properties"] = self.properties
dct["label"] = self.label

Structure.as_dict() writes with verbosity=1, which adds "label" to the dict. This causes a name collision with the .label property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug core Pymatgen core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants