From 2e2f758f9e2bd097bb33cc869026274b4786ede8 Mon Sep 17 00:00:00 2001 From: Logan Ward <ward.logan.t@gmail.com> Date: Tue, 29 Jan 2019 16:42:36 -0600 Subject: [PATCH 1/2] Made Compound able to read file-like objects I added a six dependency to make life easier --- qml/data/compound.py | 15 +++++++++------ requirements.txt | 1 + test/test_compound.py | 12 ++++++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/qml/data/compound.py b/qml/data/compound.py index 558ab6d84..263ebfa21 100644 --- a/qml/data/compound.py +++ b/qml/data/compound.py @@ -22,6 +22,7 @@ from __future__ import print_function +from six import string_types import numpy as np import collections @@ -341,13 +342,15 @@ 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 = [] diff --git a/requirements.txt b/requirements.txt index afdaacacd..bfcdf5a3d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ numpy>=1.13 scipy scikit-learn ase +six diff --git a/test/test_compound.py b/test/test_compound.py index 0a902d699..0d7727994 100644 --- a/test/test_compound.py +++ b/test/test_compound.py @@ -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__": From db848a02d1fb3595d69672243d3a0b6bbed67a26 Mon Sep 17 00:00:00 2001 From: Logan Ward <ward.logan.t@gmail.com> Date: Tue, 29 Jan 2019 16:54:38 -0600 Subject: [PATCH 2/2] Give Compound a name only if there is a filename --- qml/data/compound.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qml/data/compound.py b/qml/data/compound.py index 263ebfa21..b2b7d9f5c 100644 --- a/qml/data/compound.py +++ b/qml/data/compound.py @@ -357,7 +357,8 @@ def read_xyz(self, filename): 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()