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

[Bug]: The jsanitize function does not behave as expected with recursive_msonable=True #726

Closed
3 of 4 tasks
Andrew-S-Rosen opened this issue Nov 30, 2024 · 0 comments · Fixed by #728
Closed
3 of 4 tasks
Labels

Comments

@Andrew-S-Rosen
Copy link
Contributor

Andrew-S-Rosen commented Nov 30, 2024

Email (Optional)

No response

Version

2024.10.21

Which OS(es) are you using?

  • MacOS
  • Windows
  • Linux

What happened?

Currently, jsanitize(recursive_msonable=True) does not (as the name might suggest) recursively crawl an MSONable object and make it fully sanitized. If an MSONable object is within an attribute of the parent MSONable object, the former will not be sanitized.

For instance, try the following:

from monty.json import jsanitize
from pymatgen.core import Structure
from copy import deepcopy
structure = Structure(
   lattice=[[0, 2.13, 2.13], [2.13, 0, 2.13], [2.13, 2.13, 0]],
   species=["Mg", "O"],
   coords=[[0, 0, 0], [0.5, 0.5, 0.5]],
   )
structure.properties = {"test": deepcopy(structure)}
sanitized = jsanitize(structure, recursive_msonable=True)

You will see the output is as follows:

{'@module': 'pymatgen.core.structure',
 '@class': 'Structure',
 'charge': 0,
 'lattice': {'matrix': [[0.0, 2.13, 2.13],
   [2.13, 0.0, 2.13],
   [2.13, 2.13, 0.0]],
  'pbc': (True, True, True),
  'a': 3.012274887854692,
  'b': 3.012274887854692,
  'c': 3.012274887854692,
  'alpha': 60.00000000000001,
  'beta': 60.00000000000001,
  'gamma': 60.00000000000001,
  'volume': 19.327193999999995},
 'properties': {'test': Structure Summary
  Lattice
      abc : 3.012274887854692 3.012274887854692 3.012274887854692
   angles : 60.00000000000001 60.00000000000001 60.00000000000001
   volume : 19.327193999999995
        A : 0.0 2.13 2.13
        B : 2.13 0.0 2.13
        C : 2.13 2.13 0.0
      pbc : True True True
  PeriodicSite: Mg (0.0, 0.0, 0.0) [0.0, 0.0, 0.0]
  PeriodicSite: O (2.13, 2.13, 2.13) [0.5, 0.5, 0.5]},
 'sites': [{'species': [{'element': 'Mg', 'occu': 1}],
   'abc': [0.0, 0.0, 0.0],
   'properties': {},
   'label': 'Mg',
   'xyz': [0.0, 0.0, 0.0]},
  {'species': [{'element': 'O', 'occu': 1}],
   'abc': [0.5, 0.5, 0.5],
   'properties': {},
   'label': 'O',
   'xyz': [2.13, 2.13, 2.13]}]}

Note how the properties key has an actual Structure object in it.

Code snippet

No response

Log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant