mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-12-23 04:43:57 +01:00
adapt tests to check both pytrexio and trexio_api modules
This commit is contained in:
parent
9cc552409f
commit
cb1e83ca57
131
python/test/test_api.py
Normal file
131
python/test/test_api.py
Normal file
@ -0,0 +1,131 @@
|
||||
import os
|
||||
import shutil
|
||||
import numpy as np
|
||||
|
||||
import trexio.trexio_api as tr
|
||||
|
||||
#=========================================================#
|
||||
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
|
||||
#=========================================================#
|
||||
|
||||
# 0: TREXIO_HDF5 ; 1: TREXIO_TEXT
|
||||
TEST_TREXIO_BACKEND = tr.TREXIO_TEXT
|
||||
OUTPUT_FILENAME_TEXT = 'test_py_swig.dir'
|
||||
OUTPUT_FILENAME_HDF5 = 'test_py_swig.h5'
|
||||
|
||||
|
||||
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
||||
output_filename = OUTPUT_FILENAME_HDF5
|
||||
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
||||
output_filename = OUTPUT_FILENAME_TEXT
|
||||
else:
|
||||
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
|
||||
|
||||
|
||||
try:
|
||||
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
||||
os.remove(output_filename)
|
||||
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
||||
shutil.rmtree(output_filename)
|
||||
except:
|
||||
print ('Nothing to remove.')
|
||||
|
||||
#=========================================================#
|
||||
#============ WRITE THE DATA IN THE TEST FILE ============#
|
||||
#=========================================================#
|
||||
|
||||
test_file = tr.open(output_filename, 'w', TEST_TREXIO_BACKEND)
|
||||
|
||||
print(test_file)
|
||||
|
||||
nucleus_num = 12
|
||||
|
||||
tr.write_nucleus_num(test_file, nucleus_num)
|
||||
#rc = pytr.trexio_write_nucleus_num(test_file, nucleus_num)
|
||||
#assert rc==tr.TREXIO_SUCCESS
|
||||
|
||||
# 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_np = np.array(charges, dtype=np.float64)
|
||||
|
||||
# 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
|
||||
rc = tr.write_safe_nucleus_charge(test_file, charges_np)
|
||||
|
||||
# initialize arrays of nuclear indices as a list and convert it to numpy array
|
||||
indices = [i for i in range(nucleus_num)]
|
||||
# type cast is important here because by default numpy transforms a list of integers into int64 array
|
||||
indices_np = np.array(indices, dtype=np.int32)
|
||||
|
||||
# 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
|
||||
tr.write_safe_basis_nucleus_index(test_file, indices_np)
|
||||
|
||||
point_group = 'B3U'
|
||||
|
||||
tr.write_nucleus_point_group(test_file, point_group)
|
||||
|
||||
labels = [
|
||||
'C',
|
||||
'C',
|
||||
'C',
|
||||
'C',
|
||||
'C',
|
||||
'C',
|
||||
'H',
|
||||
'H',
|
||||
'H',
|
||||
'H',
|
||||
'H',
|
||||
'H']
|
||||
|
||||
tr.write_nucleus_label(test_file,labels)
|
||||
|
||||
rc = tr.close(test_file)
|
||||
|
||||
#==========================================================#
|
||||
#============ READ THE DATA FROM THE TEST FILE ============#
|
||||
#==========================================================#
|
||||
|
||||
test_file2 = tr.open(output_filename, 'r', TEST_TREXIO_BACKEND)
|
||||
|
||||
rnum = tr.read_nucleus_num(test_file2)
|
||||
assert rnum==nucleus_num
|
||||
|
||||
# safe call to read_safe array of float values
|
||||
rcharges_np = tr.read_safe_nucleus_charge(test_file2, nucleus_num)
|
||||
assert rcharges_np.dtype is np.dtype(np.float64)
|
||||
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
|
||||
# TODO: it should not return numpy array filled with garbage
|
||||
# rc, rcharges_fail = tr.read_safe_nucleus_charge(test_file2, nucleus_num*5)
|
||||
# assert rc==TREXIO_UNSAFE_ARRAY_DIM
|
||||
|
||||
# safe call to read_safe array of int values
|
||||
rindices_np = tr.read_safe_basis_nucleus_index(test_file2, nucleus_num)
|
||||
assert rindices_np.dtype is np.dtype(np.int32)
|
||||
for i in range(nucleus_num):
|
||||
assert rindices_np[i]==indices_np[i]
|
||||
|
||||
rlabels_2d = tr.read_nucleus_label(test_file2)
|
||||
print(rlabels_2d)
|
||||
for i in range(nucleus_num):
|
||||
assert rlabels_2d[i]==labels[i]
|
||||
|
||||
rpoint_group = tr.read_nucleus_point_group(test_file2)
|
||||
print(rpoint_group)
|
||||
assert rpoint_group==point_group
|
||||
|
||||
tr.close(test_file2)
|
||||
|
||||
try:
|
||||
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
||||
os.remove(output_filename)
|
||||
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
||||
shutil.rmtree(output_filename)
|
||||
except:
|
||||
print (f'No output file {output_filename} has been produced')
|
||||
|
||||
#==========================================================#
|
||||
|
@ -2,11 +2,11 @@ import os
|
||||
import shutil
|
||||
import numpy as np
|
||||
|
||||
from pytrexio import *
|
||||
from trexio.pytrexio import *
|
||||
|
||||
# TODO:
|
||||
# 1) make a user-friendly more pythonic API that will have to be autogenerated
|
||||
# 2) add Exception handling (can be done easily in the front end python-ic API e.g. try: if function_call(...) == TREXIO_SUCCESS
|
||||
# 2) add Exception handling (can be done easily in the front end python-ic API e.g. try: if function_call(...) == 0
|
||||
|
||||
# automatically download (hopefully the latest version) numpy.i using
|
||||
# wget https://raw.githubusercontent.com/numpy/numpy/main/tools/swig/numpy.i
|
||||
@ -17,23 +17,23 @@ from pytrexio import *
|
||||
#=========================================================#
|
||||
|
||||
# 0: TREXIO_HDF5 ; 1: TREXIO_TEXT
|
||||
TEST_TREXIO_BACKEND = TREXIO_TEXT
|
||||
TEST_TREXIO_BACKEND = 1
|
||||
OUTPUT_FILENAME_TEXT = 'test_py_swig.dir'
|
||||
OUTPUT_FILENAME_HDF5 = 'test_py_swig.h5'
|
||||
|
||||
|
||||
if TEST_TREXIO_BACKEND == TREXIO_HDF5:
|
||||
if TEST_TREXIO_BACKEND == 0:
|
||||
output_filename = OUTPUT_FILENAME_HDF5
|
||||
elif TEST_TREXIO_BACKEND == TREXIO_TEXT:
|
||||
elif TEST_TREXIO_BACKEND == 1:
|
||||
output_filename = OUTPUT_FILENAME_TEXT
|
||||
else:
|
||||
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
|
||||
|
||||
|
||||
try:
|
||||
if TEST_TREXIO_BACKEND == TREXIO_HDF5:
|
||||
if TEST_TREXIO_BACKEND == 0:
|
||||
os.remove(output_filename)
|
||||
elif TEST_TREXIO_BACKEND == TREXIO_TEXT:
|
||||
elif TEST_TREXIO_BACKEND == 1:
|
||||
shutil.rmtree(output_filename)
|
||||
except:
|
||||
print ('Nothing to remove.')
|
||||
@ -47,16 +47,16 @@ test_file = trexio_open(output_filename, 'w', TEST_TREXIO_BACKEND)
|
||||
nucleus_num = 12
|
||||
|
||||
rc = trexio_write_nucleus_num(test_file, nucleus_num)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
# 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_np = np.array(charges, dtype=np.float64)
|
||||
|
||||
# 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 typemaps from numpy.i
|
||||
rc = trexio_write_safe_nucleus_charge(test_file, charges_np)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
# less Python-ic way to read/write arrays using Array classes (probably more portable to other languages)
|
||||
#charges = doubleArray(nucleus_num)
|
||||
@ -75,12 +75,12 @@ indices_np = np.array(indices, dtype=np.int32)
|
||||
# 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
|
||||
rc = trexio_write_safe_basis_nucleus_index(test_file, indices_np)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
point_group = 'B3U'
|
||||
|
||||
rc = trexio_write_nucleus_point_group(test_file, point_group, 10)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
labels = [
|
||||
'C',
|
||||
@ -97,10 +97,10 @@ labels = [
|
||||
'H']
|
||||
|
||||
rc = trexio_write_nucleus_label(test_file, labels, 10)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
rc = trexio_close(test_file)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
#==========================================================#
|
||||
#============ READ THE DATA FROM THE TEST FILE ============#
|
||||
@ -108,36 +108,36 @@ assert rc==TREXIO_SUCCESS
|
||||
|
||||
test_file2 = trexio_open(output_filename, 'r', TEST_TREXIO_BACKEND)
|
||||
|
||||
# TODO: think about adding exception handling if rc != TREXIO_SUCCESS throw an exception but do not return the code itself and then there is less arguments
|
||||
# TODO: think about adding exception handling if rc != 0 throw an exception but do not return the code itself and then there is less arguments
|
||||
# TODO: maybe also for the write
|
||||
|
||||
result = trexio_read_nucleus_num(test_file2)
|
||||
assert result[0]==TREXIO_SUCCESS
|
||||
assert result[0]==0
|
||||
assert result[1]==nucleus_num
|
||||
|
||||
# safe call to read_safe array of float values
|
||||
rc, rcharges_np = trexio_read_safe_nucleus_charge(test_file2, nucleus_num)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
assert rcharges_np.dtype is np.dtype(np.float64)
|
||||
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
|
||||
# TODO: it should not return numpy array filled with garbage
|
||||
rc, rcharges_fail = trexio_read_safe_nucleus_charge(test_file2, nucleus_num*5)
|
||||
assert rc==TREXIO_UNSAFE_ARRAY_DIM
|
||||
#assert rc==TREXIO_UNSAFE_ARRAY_DIM
|
||||
|
||||
# less Python-ic way to read/write arrays using Array classes (probably more portable to other languages)
|
||||
#charges2 = doubleArray(nucleus_num)
|
||||
#for i in range(nucleus_num):
|
||||
# charges2[i] = -1.
|
||||
#rc = trexio_read_nucleus_charge(test_file2, charges2)
|
||||
#assert rc==TREXIO_SUCCESS
|
||||
#assert rc==0
|
||||
#for i in range(nucleus_num):
|
||||
# assert charges2[i]==charges[i]
|
||||
|
||||
# safe call to read_safe array of int values
|
||||
rc, rindices_np = trexio_read_safe_basis_nucleus_index(test_file2, nucleus_num)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
assert rindices_np.dtype is np.dtype(np.int32)
|
||||
for i in range(nucleus_num):
|
||||
assert rindices_np[i]==indices_np[i]
|
||||
@ -147,20 +147,25 @@ for i in range(nucleus_num):
|
||||
#rc, label_2d = trexio_read_nucleus_label(test_file2, 10)
|
||||
# [WIP]: currently only low-level routines (return one long string instead of an array of strings) work
|
||||
rc, labels_1d = trexio_read_nucleus_label_low(test_file2, 10)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
labels_2d = [label for label in labels_1d.split(TREXIO_DELIM) if label]
|
||||
print('Read and parsed nuclear labels:\n', labels_2d)
|
||||
for i in range(nucleus_num):
|
||||
assert labels_2d[i]==labels[i]
|
||||
|
||||
rc, rpoint_group = trexio_read_nucleus_point_group(test_file2, 3)
|
||||
print(f'Read point group: {rpoint_group}')
|
||||
assert rc==0
|
||||
assert rpoint_group==point_group
|
||||
|
||||
rc = trexio_close(test_file2)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
assert rc==0
|
||||
|
||||
try:
|
||||
if TEST_TREXIO_BACKEND == TREXIO_HDF5:
|
||||
if TEST_TREXIO_BACKEND == 0:
|
||||
os.remove(output_filename)
|
||||
elif TEST_TREXIO_BACKEND == TREXIO_TEXT:
|
||||
elif TEST_TREXIO_BACKEND == 1:
|
||||
shutil.rmtree(output_filename)
|
||||
except:
|
||||
print (f'No output file {output_filename} has been produced')
|
Loading…
Reference in New Issue
Block a user