mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-12-23 04:43:57 +01:00
BUG: trexio_open fails when test script is executed from outside the root test directory
This commit is contained in:
parent
74c69bb293
commit
ced8210ff0
@ -191,7 +191,7 @@ setup_py = $(srcdir)/python/setup.py
|
|||||||
setup_cfg = $(srcdir)/python/setup.cfg
|
setup_cfg = $(srcdir)/python/setup.cfg
|
||||||
pytrexio_py = $(srcdir)/python/pytrexio/pytrexio.py
|
pytrexio_py = $(srcdir)/python/pytrexio/pytrexio.py
|
||||||
trexio_py = $(srcdir)/python/trexio.py
|
trexio_py = $(srcdir)/python/trexio.py
|
||||||
TEST_PY = $(srcdir)/python/test/test_api.py
|
TEST_PY = python/test/test_api.py
|
||||||
pytrexio_c = $(srcdir)/src/pytrexio_wrap.c
|
pytrexio_c = $(srcdir)/src/pytrexio_wrap.c
|
||||||
pytrexio_i = $(srcdir)/src/pytrexio.i
|
pytrexio_i = $(srcdir)/src/pytrexio.i
|
||||||
numpy_i = $(srcdir)/src/numpy.i
|
numpy_i = $(srcdir)/src/numpy.i
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
import trexio as tr
|
import trexio
|
||||||
|
|
||||||
#=========================================================#
|
#=========================================================#
|
||||||
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
|
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
|
||||||
@ -15,9 +17,9 @@ OUTPUT_FILENAME_HDF5 = 'test_py_swig.h5'
|
|||||||
|
|
||||||
|
|
||||||
# define TREXIO file name
|
# define TREXIO file name
|
||||||
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
||||||
output_filename = OUTPUT_FILENAME_HDF5
|
output_filename = OUTPUT_FILENAME_HDF5
|
||||||
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
||||||
output_filename = OUTPUT_FILENAME_TEXT
|
output_filename = OUTPUT_FILENAME_TEXT
|
||||||
else:
|
else:
|
||||||
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
|
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
|
||||||
@ -25,9 +27,9 @@ else:
|
|||||||
|
|
||||||
# remove TREXIO file if exists in the current directory
|
# remove TREXIO file if exists in the current directory
|
||||||
try:
|
try:
|
||||||
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
||||||
os.remove(output_filename)
|
os.remove(output_filename)
|
||||||
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
||||||
shutil.rmtree(output_filename)
|
shutil.rmtree(output_filename)
|
||||||
except:
|
except:
|
||||||
print ('Nothing to remove.')
|
print ('Nothing to remove.')
|
||||||
@ -39,16 +41,29 @@ except:
|
|||||||
|
|
||||||
|
|
||||||
# create TREXIO file and open it for writing
|
# create TREXIO file and open it for writing
|
||||||
#test_file = tr.open(output_filename, 'w', TEST_TREXIO_BACKEND)
|
#test_file = trexio.open(output_filename, 'w', TEST_TREXIO_BACKEND)
|
||||||
test_file = tr.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND)
|
test_file = trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND)
|
||||||
|
|
||||||
# Print docstring of the tr.open function
|
# Print docstring of the trexio.open function
|
||||||
#print(tr.open.__doc__)
|
#print(trexio.open.__doc__)
|
||||||
|
|
||||||
nucleus_num = 12
|
nucleus_num = 12
|
||||||
|
|
||||||
|
try:
|
||||||
|
trexio.write_nucleus_num(test_file, -100)
|
||||||
|
except trexio.Error:
|
||||||
|
print("Writing negative nucleus_num: checked.")
|
||||||
|
|
||||||
# write nucleus_num in the file
|
# write nucleus_num in the file
|
||||||
tr.write_nucleus_num(test_file, nucleus_num)
|
try:
|
||||||
|
trexio.write_nucleus_num(test_file, nucleus_num)
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
|
||||||
|
try:
|
||||||
|
trexio.write_nucleus_num(test_file, nucleus_num*2)
|
||||||
|
except trexio.Error:
|
||||||
|
print("Attempt to overwrite nucleus_num: checked.")
|
||||||
|
|
||||||
# initialize charge arrays as a list and convert it to numpy array
|
# initialize charge arrays as a list and convert it to numpy array
|
||||||
charges = [6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.]
|
charges = [6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.]
|
||||||
@ -57,7 +72,7 @@ charges_np = np.array(charges, dtype=np.int32)
|
|||||||
|
|
||||||
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
||||||
# from the size of the list/array by SWIG using typemaps from numpy.i
|
# from the size of the list/array by SWIG using typemaps from numpy.i
|
||||||
tr.write_nucleus_charge(test_file, charges_np)
|
trexio.write_nucleus_charge(test_file, charges_np)
|
||||||
|
|
||||||
# initialize arrays of nuclear indices as a list and convert it to numpy array
|
# initialize arrays of nuclear indices as a list and convert it to numpy array
|
||||||
indices = [i for i in range(nucleus_num)]
|
indices = [i for i in range(nucleus_num)]
|
||||||
@ -66,7 +81,7 @@ indices_np = np.array(indices, dtype=np.int64)
|
|||||||
|
|
||||||
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
||||||
# from the size of the list/array by SWIG using typemacs from numpy.i
|
# from the size of the list/array by SWIG using typemacs from numpy.i
|
||||||
tr.write_basis_nucleus_index(test_file, indices_np)
|
trexio.write_basis_nucleus_index(test_file, indices_np)
|
||||||
|
|
||||||
# initialize a list of nuclear coordinates
|
# initialize a list of nuclear coordinates
|
||||||
coords = [
|
coords = [
|
||||||
@ -85,12 +100,12 @@ coords = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
# write coordinates in the file
|
# write coordinates in the file
|
||||||
tr.write_nucleus_coord(test_file, coords)
|
trexio.write_nucleus_coord(test_file, coords)
|
||||||
|
|
||||||
point_group = 'B3U'
|
point_group = 'B3U'
|
||||||
|
|
||||||
# write nucleus_point_group in the file
|
# write nucleus_point_group in the file
|
||||||
tr.write_nucleus_point_group(test_file, point_group)
|
trexio.write_nucleus_point_group(test_file, point_group)
|
||||||
|
|
||||||
labels = [
|
labels = [
|
||||||
'C',
|
'C',
|
||||||
@ -107,13 +122,13 @@ labels = [
|
|||||||
'H']
|
'H']
|
||||||
|
|
||||||
# write nucleus_label in the file
|
# write nucleus_label in the file
|
||||||
tr.write_nucleus_label(test_file,labels)
|
trexio.write_nucleus_label(test_file,labels)
|
||||||
|
|
||||||
# close TREXIO file
|
# close TREXIO file
|
||||||
# [TODO:] this functional call is no longer needed as we introduced TREXIO_File class which has a desctructor that closes the file
|
# [TODO:] this functional call is no longer needed as we introduced TREXIO_File class which has a desctructor that closes the file
|
||||||
#tr.close(test_file)
|
#trexio.close(test_file)
|
||||||
# [TODO:] without calling destructor on test_file the TREXIO_FILE is not getting created and the data is not written when using TEXT back end. This, the user still has to explicitly call destructor on test_file object instead
|
# [TODO:] without calling destructor on test_file the TREXIO_FILE is not getting created and the data is not written when using TEXT back end. This, the user still has to explicitly call destructor on test_file object instead
|
||||||
# tr.close function. This is only an issue when the data is getting written and read in the same session (e.g. in Jupyter notebook)
|
# trexio.close function. This is only an issue when the data is getting written and read in the same session (e.g. in Jupyter notebook)
|
||||||
del test_file
|
del test_file
|
||||||
|
|
||||||
|
|
||||||
@ -123,68 +138,68 @@ del test_file
|
|||||||
#==========================================================#
|
#==========================================================#
|
||||||
|
|
||||||
# open previously created TREXIO file, now in 'read' mode
|
# open previously created TREXIO file, now in 'read' mode
|
||||||
#test_file2 = tr.open(output_filename, 'r', TEST_TREXIO_BACKEND)
|
#test_file2 = trexio.open(output_filename, 'r', TEST_TREXIO_BACKEND)
|
||||||
test_file2 = tr.File(output_filename, 'r', TEST_TREXIO_BACKEND)
|
test_file2 = trexio.File(output_filename, 'r', TEST_TREXIO_BACKEND)
|
||||||
|
|
||||||
# read nucleus_num from file
|
# read nucleus_num from file
|
||||||
rnum = tr.read_nucleus_num(test_file2)
|
rnum = trexio.read_nucleus_num(test_file2)
|
||||||
assert rnum==nucleus_num
|
assert rnum==nucleus_num
|
||||||
|
|
||||||
# safe call to read_nucleus_charge array of float values
|
# safe call to read_nucleus_charge array of float values
|
||||||
rcharges_np = tr.read_nucleus_charge(test_file2, dim=nucleus_num)
|
rcharges_np = trexio.read_nucleus_charge(test_file2, dim=nucleus_num)
|
||||||
assert rcharges_np.dtype is np.dtype(np.float64)
|
assert rcharges_np.dtype is np.dtype(np.float64)
|
||||||
np.testing.assert_array_almost_equal(rcharges_np, charges_np, decimal=8)
|
np.testing.assert_array_almost_equal(rcharges_np, charges_np, decimal=8)
|
||||||
|
|
||||||
# unsafe call to read_safe should fail with error message corresponding to TREXIO_UNSAFE_ARRAY_DIM
|
# unsafe call to read_safe should fail with error message corresponding to TREXIO_UNSAFE_ARRAY_DIM
|
||||||
try:
|
try:
|
||||||
rcharges_fail = tr.read_nucleus_charge(test_file2, dim=nucleus_num*5)
|
rcharges_fail = trexio.read_nucleus_charge(test_file2, dim=nucleus_num*5)
|
||||||
except Exception:
|
except Exception:
|
||||||
print("Unsafe call to safe API: checked")
|
print("Unsafe call to safe API: checked")
|
||||||
|
|
||||||
# safe call to read array of int values (nuclear indices)
|
# safe call to read array of int values (nuclear indices)
|
||||||
rindices_np_16 = tr.read_basis_nucleus_index(test_file2, dim=nucleus_num, dtype=np.int16)
|
rindices_np_16 = trexio.read_basis_nucleus_index(test_file2, dim=nucleus_num, dtype=np.int16)
|
||||||
assert rindices_np_16.dtype is np.dtype(np.int16)
|
assert rindices_np_16.dtype is np.dtype(np.int16)
|
||||||
for i in range(nucleus_num):
|
for i in range(nucleus_num):
|
||||||
assert rindices_np_16[i]==indices_np[i]
|
assert rindices_np_16[i]==indices_np[i]
|
||||||
|
|
||||||
rindices_np_32 = tr.read_basis_nucleus_index(test_file2, dim=nucleus_num, dtype=np.int32)
|
rindices_np_32 = trexio.read_basis_nucleus_index(test_file2, dim=nucleus_num, dtype=np.int32)
|
||||||
assert rindices_np_32.dtype is np.dtype(np.int32)
|
assert rindices_np_32.dtype is np.dtype(np.int32)
|
||||||
for i in range(nucleus_num):
|
for i in range(nucleus_num):
|
||||||
assert rindices_np_32[i]==indices_np[i]
|
assert rindices_np_32[i]==indices_np[i]
|
||||||
|
|
||||||
rindices_np_64 = tr.read_basis_nucleus_index(test_file2)
|
rindices_np_64 = trexio.read_basis_nucleus_index(test_file2)
|
||||||
assert rindices_np_64.dtype is np.dtype(np.int64)
|
assert rindices_np_64.dtype is np.dtype(np.int64)
|
||||||
assert rindices_np_64.size==nucleus_num
|
assert rindices_np_64.size==nucleus_num
|
||||||
for i in range(nucleus_num):
|
for i in range(nucleus_num):
|
||||||
assert rindices_np_64[i]==indices_np[i]
|
assert rindices_np_64[i]==indices_np[i]
|
||||||
|
|
||||||
# read nuclear coordinates without providing optional argument dim
|
# read nuclear coordinates without providing optional argument dim
|
||||||
rcoords_np = tr.read_nucleus_coord(test_file2)
|
rcoords_np = trexio.read_nucleus_coord(test_file2)
|
||||||
|
|
||||||
assert rcoords_np.size==nucleus_num*3
|
assert rcoords_np.size==nucleus_num*3
|
||||||
np.testing.assert_array_almost_equal(rcoords_np, np.array(coords).reshape(nucleus_num,3), decimal=8)
|
np.testing.assert_array_almost_equal(rcoords_np, np.array(coords).reshape(nucleus_num,3), decimal=8)
|
||||||
|
|
||||||
# set doReshape to False to get a flat 1D array (e.g. when reading matrices like nuclear coordinates)
|
# set doReshape to False to get a flat 1D array (e.g. when reading matrices like nuclear coordinates)
|
||||||
#rcoords_reshaped_2 = tr.read_nucleus_coord(test_file2, doReshape=False)
|
#rcoords_reshaped_2 = trexio.read_nucleus_coord(test_file2, doReshape=False)
|
||||||
|
|
||||||
# read array of nuclear labels
|
# read array of nuclear labels
|
||||||
rlabels_2d = tr.read_nucleus_label(test_file2, dim=nucleus_num)
|
rlabels_2d = trexio.read_nucleus_label(test_file2, dim=nucleus_num)
|
||||||
print(rlabels_2d)
|
print(rlabels_2d)
|
||||||
for i in range(nucleus_num):
|
for i in range(nucleus_num):
|
||||||
assert rlabels_2d[i]==labels[i]
|
assert rlabels_2d[i]==labels[i]
|
||||||
|
|
||||||
# read a string corresponding to nuclear point group
|
# read a string corresponding to nuclear point group
|
||||||
rpoint_group = tr.read_nucleus_point_group(test_file2)
|
rpoint_group = trexio.read_nucleus_point_group(test_file2)
|
||||||
assert rpoint_group==point_group
|
assert rpoint_group==point_group
|
||||||
|
|
||||||
# close TREXIO file
|
# close TREXIO file
|
||||||
#tr.close(test_file2)
|
#trexio.close(test_file2)
|
||||||
|
|
||||||
# cleaning (remove the TREXIO file)
|
# cleaning (remove the TREXIO file)
|
||||||
try:
|
try:
|
||||||
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
||||||
os.remove(output_filename)
|
os.remove(output_filename)
|
||||||
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
||||||
shutil.rmtree(output_filename)
|
shutil.rmtree(output_filename)
|
||||||
except:
|
except:
|
||||||
print (f'No output file {output_filename} has been produced')
|
print (f'No output file {output_filename} has been produced')
|
||||||
|
@ -113,7 +113,7 @@ np.testing.assert_array_almost_equal(rcharges_np, charges_np, decimal=8)
|
|||||||
# unsafe call to read_safe should not only have return code = TREXIO_UNSAFE_ARRAY_DIM
|
# unsafe call to read_safe should not only have return code = TREXIO_UNSAFE_ARRAY_DIM
|
||||||
# TODO: it should not return numpy array filled with garbage
|
# TODO: it should not return numpy array filled with garbage
|
||||||
rc, rcharges_fail = trexio_read_safe_nucleus_charge(test_file2, nucleus_num*5)
|
rc, rcharges_fail = trexio_read_safe_nucleus_charge(test_file2, nucleus_num*5)
|
||||||
assert rc==TREXIO_UNSAFE_ARRAY_DIM
|
assert rc==23
|
||||||
|
|
||||||
# less Python-ic way to read/write arrays using Array classes (probably more portable to other languages)
|
# less Python-ic way to read/write arrays using Array classes (probably more portable to other languages)
|
||||||
#charges2 = doubleArray(nucleus_num)
|
#charges2 = doubleArray(nucleus_num)
|
||||||
|
Loading…
Reference in New Issue
Block a user