1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 04:43:57 +01:00

minor cleaning + exception handling for an unsafe call to the safe API

This commit is contained in:
q-posev 2021-08-18 15:42:10 +03:00
parent 902147b45b
commit 8d8658fabf
2 changed files with 10 additions and 25 deletions

View File

@ -2,7 +2,7 @@ import os
import shutil import shutil
import numpy as np import numpy as np
import trexio.trexio_api as tr import trexio as tr
#=========================================================# #=========================================================#
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========# #======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
@ -41,8 +41,6 @@ print(test_file)
nucleus_num = 12 nucleus_num = 12
tr.write_nucleus_num(test_file, nucleus_num) 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 # 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.]
@ -81,7 +79,7 @@ labels = [
tr.write_nucleus_label(test_file,labels) tr.write_nucleus_label(test_file,labels)
rc = tr.close(test_file) tr.close(test_file)
#==========================================================# #==========================================================#
#============ READ THE DATA FROM THE TEST FILE ============# #============ READ THE DATA FROM THE TEST FILE ============#
@ -97,10 +95,11 @@ rcharges_np = tr.read_safe_nucleus_charge(test_file2, 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 not only have return code = TREXIO_UNSAFE_ARRAY_DIM # unsafe call to read_safe should fail with error message corresponding to TREXIO_UNSAFE_ARRAY_DIM
# TODO: it should not return numpy array filled with garbage try:
# rc, rcharges_fail = tr.read_safe_nucleus_charge(test_file2, nucleus_num*5) rcharges_fail = tr.read_safe_nucleus_charge(test_file2, nucleus_num*5)
# assert rc==TREXIO_UNSAFE_ARRAY_DIM except Exception:
print("Unsafe call to safe API: successful")
# safe call to read_safe array of int values # safe call to read_safe array of int values
rindices_np = tr.read_safe_basis_nucleus_index(test_file2, nucleus_num) rindices_np = tr.read_safe_basis_nucleus_index(test_file2, nucleus_num)

View File

@ -2,15 +2,7 @@ import os
import shutil import shutil
import numpy as np import numpy as np
from trexio.pytrexio import * from pytrexio.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(...) == 0
# automatically download (hopefully the latest version) numpy.i using
# wget https://raw.githubusercontent.com/numpy/numpy/main/tools/swig/numpy.i
# but also keep the original copy in case if wget fails
#=========================================================# #=========================================================#
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========# #======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
@ -108,9 +100,6 @@ assert rc==0
test_file2 = trexio_open(output_filename, 'r', TEST_TREXIO_BACKEND) test_file2 = trexio_open(output_filename, 'r', TEST_TREXIO_BACKEND)
# 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) result = trexio_read_nucleus_num(test_file2)
assert result[0]==0 assert result[0]==0
assert result[1]==nucleus_num assert result[1]==nucleus_num
@ -124,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==TREXIO_UNSAFE_ARRAY_DIM
# 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)
@ -142,10 +131,7 @@ assert rindices_np.dtype is np.dtype(np.int32)
for i in range(nucleus_num): for i in range(nucleus_num):
assert rindices_np[i]==indices_np[i] assert rindices_np[i]==indices_np[i]
# currently only low-level routines (return one long string instead of an array of strings) work
# [WIP]: ideally, the list of strings should be returned as below
#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) rc, labels_1d = trexio_read_nucleus_label_low(test_file2, 10)
assert rc==0 assert rc==0