From 049ddb11609f924e4e7a85891e28786a29640f18 Mon Sep 17 00:00:00 2001 From: Yannick Calvino Alonso Date: Fri, 6 Dec 2024 12:34:28 +0100 Subject: [PATCH] spin/charge loader for .txt file only with `None` type converter --- qstack/spahm/rho/utils.py | 12 +++++++++--- tests/test_spahm_b.py | 6 ++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/qstack/spahm/rho/utils.py b/qstack/spahm/rho/utils.py index 87a2367..08ff35b 100644 --- a/qstack/spahm/rho/utils.py +++ b/qstack/spahm/rho/utils.py @@ -21,12 +21,18 @@ def get_chsp(fname, n): - if fname: - chsp = np.loadtxt(fname, dtype=int, ndmin=1) + def chsp_converter(chsp): + if chsp == 'None': + chsp = None + else: + chsp = int(chsp) + return chsp + if os.path.isfile(fname): + chsp = np.loadtxt(fname, dtype=object, converters=chsp_converter, encoding=None) if(len(chsp)!=n): raise RuntimeError(f'Wrong lengh of the file {fname}') else: - chsp = np.zeros(n, dtype=int) + raise RuntimeError(f"{fname} can not be found") return chsp diff --git a/tests/test_spahm_b.py b/tests/test_spahm_b.py index 778af30..09334b4 100755 --- a/tests/test_spahm_b.py +++ b/tests/test_spahm_b.py @@ -62,9 +62,11 @@ def test_ecp(): def test_from_list(): path = os.path.dirname(os.path.realpath(__file__)) path2list = path+'/data/list_water.txt' + path2spins = path+'/data/list_water_spins.txt' + path2charges = path+'/data/list_water_charges.txt' xyzlist = utils.get_xyzlist(path2list) - spins = utils.get_chsp(None, len(xyzlist)) - charges = utils.get_chsp(None, len(xyzlist)) + spins = utils.get_chsp(path2spins, len(xyzlist)) + charges = utils.get_chsp(path2charges, len(xyzlist)) mols = utils.load_mols(xyzlist, charges, spins, 'minao', srcdir=path+'/data/') spahm_b = bond.get_repr(mols, xyzlist, 'LB', spin=spins, same_basis=True) Xtrue = np.load(path+'/data/list_H2O_spahm-b_minao_LB_alpha-beta.npy')