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

47 support reading of lists in custom xyz files #48

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions TCutility/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def parse_str(s: str):
if not isinstance(s, str):
return s

if ',' in s:
return [parse_str(part.strip()) for part in s.split(',')]

# to parse the string we use try/except method
try:
return int(s)
Expand Down Expand Up @@ -86,15 +89,15 @@ def load(path) -> plams.Molecule:
'''
def parse_flags(args):
ret = result.Result()
ret.tags = []
ret.tags = set()
for arg in args:
# flags are given as key=value pairs
# tags are given as loose keys
if '=' in arg:
key, value = arg.split('=')
ret[key.strip()] = parse_str(value.strip())
else:
ret.tags.append(parse_str(arg.strip()))
ret.tags.add(parse_str(arg.strip()))
return ret

with open(path) as f:
Expand Down
3 changes: 3 additions & 0 deletions TCutility/results/result.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of reduce? Do we use pickling or so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plams uses the copy module from Python to copy molecule objects, however weird classes like Result may give problems with the copy.deepcopy function. To fix that we have to overload __reduce__ or __reduce_ex__. You can try to copy a molecule read by TCutility.molecule.read and then try to call its copy method. If you do not define __reduce__ in the Result class you will get an error.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def __hash__(self):
'''
raise KeyError(f'Tried to hash {self.get_parent_tree()}, but it is empty')

def __reduce__(self):
return 'TCutility.results.result.Result'

def __bool__(self):
'''Make sure that keys starting and ending in "__" are skipped'''
return len([key for key in self.keys() if not (key.startswith('__') and key.endswith('__'))]) > 0
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/xyz/pyr.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
12

C 0.00000000 1.14206764 0.90887116
C 0.00000000 1.19134753 -0.47779047
C 0.00000000 0.00000000 -1.18863009
C 0.00000000 -1.19134753 -0.47779047
C 0.00000000 -1.14206764 0.90887116
N 0.00000000 0.00000000 1.58935329
H 0.00000000 2.05493274 1.49740370
H 0.00000000 2.14479192 -0.99199860
H 0.00000000 0.00000000 -2.27369478
H 0.00000000 -2.14479192 -0.99199860
H 0.00000000 -2.05493274 1.49740370
Xx 0.12739120 -0.00077609 2.50049043

conn = 6, 12
test_list = 3.14, pi
active_atoms = 8
36 changes: 30 additions & 6 deletions test/test_molecules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,63 @@


def test_atom_flags() -> None:
xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
mol = molecule.load(xyzfile)
atom = mol[2]
assert atom.flags.origin == 'substrate'


def test_atom_tags() -> None:
xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
mol = molecule.load(xyzfile)
atom = mol[3]
assert 'R1' in atom.flags.tags


def test_mol_reading() -> None:
xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
tcmol = molecule.load(xyzfile)
plamsmol = plams.Molecule(xyzfile)
assert all(tcatom.symbol == plamsatom.symbol and tcatom.coords == plamsatom.coords for tcatom, plamsatom in zip(tcmol.atoms, plamsmol.atoms))


def test_mol_flags() -> None:
xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
mol = molecule.load(xyzfile)
assert mol.flags.task == 'TransitionStateSearch'


def test_mol_tags() -> None:
xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
mol = molecule.load(xyzfile)
assert 'vibrations' in mol.flags.tags


def test_comment() -> None:
xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz')
mol = molecule.load(xyzfile)
assert mol.comment == 'molecule used for testing the molecule module'


def test_list_value() -> None:
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'pyr.xyz')
mol = molecule.load(xyzfile)
assert mol.flags.conn == [6, 12]


def test_list_value2() -> None:
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'pyr.xyz')
mol = molecule.load(xyzfile)
assert mol.flags.test_list == [3.14, 'pi']


def test_mol_copy() -> None:
xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'pyr.xyz')
mol = molecule.load(xyzfile)
mol.copy()


if __name__ == '__main__':
import pytest

pytest.main()