Skip to content

Commit

Permalink
added Encode_SymmOp
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Jul 2, 2018
1 parent 25f8c2f commit a6370b8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ejplugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def load_all_encoders():
return plugins.load_plugin_classes([Encode_Pymatgen, Encode_ASE])


__version__ = "0.9.3"
__version__ = "0.9.4"
47 changes: 47 additions & 0 deletions ejplugins/pymatgen_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Encode_Pymatgen(object):
['@class', '@module', 'charge', 'lattice', 'sites']
>>> isinstance(Encode_Pymatgen().from_json(struct_dict), pym.Structure)
True
>>> Encode_Pymatgen().to_str(struct)
'Structure(Fe2, Comp=Fe, SpGrp=229)'
"""
plugin_name = 'pymatgen.Structure'
Expand Down Expand Up @@ -48,3 +50,48 @@ def to_str(self, obj):
return "{0}({1}, Comp={2}, SpGrp={3})".format(name,
obj.formula.replace(" ", ""), obj.composition.reduced_formula, obj.get_space_group_info()[1])


class Encode_SymmOp(object):
"""
Examples
--------
>>> symop = pym.SymmOp.from_rotation_and_translation()
>>> symop_dict = Encode_SymmOp().to_json(symop)
>>> print(sorted([str(s) for s in symop_dict.keys()]))
['@class', '@module', 'matrix', 'tolerance']
>>> isinstance(Encode_SymmOp().from_json(symop_dict), pym.SymmOp)
True
>>> Encode_SymmOp().to_str(symop)
'SymOp(R=[[1. 0. 0.],[0. 1. 0.],[0. 0. 1.]], T=[0. 0. 0.])'
"""
plugin_name = 'pymatgen.SymmOp'
plugin_descript = 'encode/decode pymatgen.SymmOp'
objclass = pym.Structure
dict_signature = ['@class', '@module', 'matrix']
allow_other_keys = True

def to_json(self, obj):
return obj.as_dict()

def from_json(self, obj):

modname = obj["@module"]
classname = obj["@class"]
mod = __import__(modname, globals(), locals(), [classname], 0)
if hasattr(mod, classname):
cls_ = getattr(mod, classname)
data = {k: v for k, v in obj.items()
if k not in ["@module", "@class"]}
if hasattr(cls_, "from_dict"):
return cls_.from_dict(data)
else:
raise ValueError("the class {0}.{1} does not have a from_dict method".format(modname, classname))
else:
raise ValueError("the module {0} does not have the required class {1}".format(modname, classname))

def to_str(self, obj):
return "SymOp({})".format(
str(obj).replace("\n", " ").replace("Rot: ", "R=").replace(" tau ", ", T=").replace("] [", "],["))

0 comments on commit a6370b8

Please sign in to comment.