Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

Add ability to create compounds from file-like objects #103

Merged
merged 2 commits into from
Feb 5, 2019
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
18 changes: 11 additions & 7 deletions qml/data/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from __future__ import print_function

from six import string_types
import numpy as np
import collections

Expand Down Expand Up @@ -341,20 +342,23 @@ def generate_acsf(self, elements = [1,6,7,8,16], nRs2 = 3, nRs3 = 3, nTs = 3, et
def read_xyz(self, filename):
"""(Re-)initializes the Compound-object with data from an xyz-file.

:param filename: Input xyz-filename.
:type filename: string
"""
:param filename: Input xyz-filename or file-like obejct
:type filename: string or file-like object
"""

f = open(filename, "r")
lines = f.readlines()
f.close()
if isinstance(filename, string_types):
with open(filename, "r") as f:
lines = f.readlines()
else:
lines = filename.readlines()

self.natoms = int(lines[0])
self.atomtypes = []
self.nuclear_charges = np.empty(self.natoms, dtype=int)
self.coordinates = np.empty((self.natoms, 3), dtype=float)

self.name = filename
# Give the Compound a name if it is a string
self.name = filename if isinstance(filename, string_types) else "Compound"

for i, line in enumerate(lines[2:self.natoms+2]):
tokens = line.split()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ numpy>=1.13
scipy
scikit-learn
ase
six
12 changes: 10 additions & 2 deletions test/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ def test_compound():
ref_atomtypes = ['C', 'Cl', 'Br', 'H', 'H']
ref_charges = [ 6, 17, 35, 1 , 1]

assert compare_lists(ref_atomtypes, c.atomtypes), "Failed parsing atomtypes"
assert compare_lists(ref_charges, c.nuclear_charges), "Failed parsing nuclear_charges"
assert compare_lists(ref_atomtypes, c2.atomtypes), "Failed parsing atomtypes"
assert compare_lists(ref_charges, c2.nuclear_charges), "Failed parsing nuclear_charges"

# Test extended xyz from a file pointer rather than a filename
with open(test_dir + "/data/compound_test.exyz") as fp:
c3 = Compound(xyz=fp)

assert compare_lists(ref_atomtypes, c3.atomtypes), "Failed parsing atomtypes"
assert compare_lists(ref_charges, c3.nuclear_charges), "Failed parsing nuclear_charges"


if __name__ == "__main__":

Expand Down