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

Pass kwargs to IStructure.to method in JSON format #4295

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2925,17 +2925,17 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
fmt is not specified, the format is determined from the
filename. Defaults is None, i.e. string output.
fmt (str): Format to output to. Defaults to JSON unless filename
is provided. If fmt is specifies, it overrides whatever the
is provided. If specified, it overrides whatever the
filename is. Options include "cif", "poscar", "cssr", "json",
"xsf", "mcsqs", "prismatic", "yaml", "yml", "fleur-inpgen", "pwmat",
"aims".
Non-case sensitive.
**kwargs: Kwargs passthru to relevant methods. e.g. This allows
the passing of parameters like symprec to the
CifWriter.__init__ method for generation of symmetric CIFs.
Case insensitive.
**kwargs: Kwargs pass thru to relevant methods. This allows
the passing of parameters like `symprec` to the
`CifWriter.__init__ method` for generation of symmetric CIFs.

Returns:
str: String representation of molecule in given format. If a filename
str: String representation of structure in given format. If a filename
is provided, the same string is written to the file.
"""
filename, fmt = str(filename), cast(FileFormats, fmt.lower())
Expand All @@ -2944,24 +2944,29 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
from pymatgen.io.cif import CifWriter

writer: Any = CifWriter(self, **kwargs)

elif fmt == "mcif" or fnmatch(filename.lower(), "*.mcif*"):
from pymatgen.io.cif import CifWriter

writer = CifWriter(self, write_magmoms=True, **kwargs)

elif fmt == "poscar" or fnmatch(filename, "*POSCAR*"):
from pymatgen.io.vasp import Poscar

writer = Poscar(self, **kwargs)

elif fmt == "cssr" or fnmatch(filename.lower(), "*.cssr*"):
from pymatgen.io.cssr import Cssr

writer = Cssr(self)

elif fmt == "json" or fnmatch(filename.lower(), "*.json*"):
json_str = json.dumps(self.as_dict())
json_str = json.dumps(self.as_dict(), **kwargs)
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(json_str)
return json_str

elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
from pymatgen.io.xcrysden import XSF

Expand All @@ -2970,6 +2975,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(res_str)
return res_str

elif (
fmt == "mcsqs"
or fnmatch(filename, "*rndstr.in*")
Expand All @@ -2983,10 +2989,12 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="ascii") as file:
file.write(res_str)
return res_str

elif fmt == "prismatic" or fnmatch(filename, "*prismatic*"):
from pymatgen.io.prismatic import Prismatic

return Prismatic(self).to_str()

elif fmt in ("yaml", "yml") or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
yaml = YAML()
str_io = io.StringIO()
Expand All @@ -2996,6 +3004,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(yaml_str)
return yaml_str

elif fmt == "aims" or fnmatch(filename, "geometry.in"):
from pymatgen.io.aims.inputs import AimsGeometryIn

Expand All @@ -3006,11 +3015,13 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
file.write(geom_in.content)
file.write("\n")
return geom_in.content

# fleur support implemented in external namespace pkg https://github.com/JuDFTteam/pymatgen-io-fleur
elif fmt == "fleur-inpgen" or fnmatch(filename, "*.in*"):
from pymatgen.io.fleur import FleurInput

writer = FleurInput(self, **kwargs)

elif fmt == "res" or fnmatch(filename, "*.res"):
from pymatgen.io.res import ResIO

Expand All @@ -3019,10 +3030,12 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(res_str)
return res_str

elif fmt == "pwmat" or fnmatch(filename.lower(), "*.pwmat") or fnmatch(filename.lower(), "*.config"):
from pymatgen.io.pwmat import AtomConfig

writer = AtomConfig(self, **kwargs)

else:
if fmt == "":
raise ValueError(f"Format not specified and could not infer from {filename=}")
Expand Down