diff --git a/Tests/Examples/CMakeLists.txt b/Tests/Examples/CMakeLists.txt index eb12ed4e56972ac820ff13ed9ee9b768c5687ea9..0a30d4aac210a6464a2818ee999aced59cf53fc0 100644 --- a/Tests/Examples/CMakeLists.txt +++ b/Tests/Examples/CMakeLists.txt @@ -33,20 +33,20 @@ macro(parse_example example) get_filename_component(EXAMPLE_SUBDIR ${example} DIRECTORY) endmacro() +set(launch_env COMMAND ${CMAKE_COMMAND} -E env) +set(launch_py + BA_EXAMPLE_DATA_DIR=${EXAMPLES_DATA_DIR} + PYTHONPATH=${CMAKE_BINARY_DIR}/lib + ${Python3_EXECUTABLE}) + # Run one Python example that does plot. function(run_example example) parse_example(${example}) string(REPLACE "/" "." TARGET_NAME Example.${example}.fig) add_test(NAME Example.run.${EXAMPLE_NAME} - COMMAND ${CMAKE_COMMAND} -E env - BA_EXAMPLE_DATA_DIR=${EXAMPLES_DATA_DIR} - PYTHONPATH=${CMAKE_BINARY_DIR}/lib - ${Python3_EXECUTABLE} ${EXAMPLE_SCRIPT} show=n) + ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} show=n) add_custom_target(${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} -E env - BA_EXAMPLE_DATA_DIR=${EXAMPLES_DATA_DIR} - PYTHONPATH=${CMAKE_BINARY_DIR}/lib - ${Python3_EXECUTABLE} ${EXAMPLE_SCRIPT} + ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} figfile=${FIG_DIR}/${EXAMPLE_SUBDIR}/${EXAMPLE_NAME}.png) add_dependencies(figures ${TARGET_NAME}) endfunction() @@ -56,9 +56,7 @@ function(run_plotless example) parse_example(${example}) string(REPLACE "/" "." TARGET_NAME Example.${example}.run) add_test(NAME ${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} -E env - PYTHONPATH=${CMAKE_BINARY_DIR}/lib - ${Python3_EXECUTABLE} ${EXAMPLE_SCRIPT} + ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) endfunction() @@ -68,9 +66,7 @@ function(run_manually example) string(REPLACE "/" "." TARGET_NAME Example.${example}.run) add_custom_target(${TARGET_NAME} COMMAND echo "\n### MANUAL TEST: EXAMPLE ${EXAMPLE_NAME}\n\n" - COMMAND ${CMAKE_COMMAND} -E env - PYTHONPATH=${CMAKE_BINARY_DIR}/lib - ${Python3_EXECUTABLE} ${EXAMPLE_SCRIPT} + ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) add_dependencies(manualtest ${TARGET_NAME}) endfunction() @@ -82,22 +78,11 @@ function(test_equality example reference tolerance) cmake_path(SET outfile NORMALIZE ${TEST_OUTPUT_DIR_PY_PERSIST}/${example}) cmake_path(SET reffile NORMALIZE ${REFERENCE_DIR_EXAMPLES_MINI}/${reference}) add_test(NAME ${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} - -DPython3_EXECUTABLE=${Python3_EXECUTABLE} - -Dxx_bindir=${CMAKE_BINARY_DIR} - -Dxx_srcdir=${CMAKE_CURRENT_SOURCE_DIR} - -Dxx_datdir=${EXAMPLES_DATA_DIR} - -Dxx_name=${EXAMPLE_NAME} - -Dxx_script=${EXAMPLE_SCRIPT} - -Dxx_outfile=${outfile} - -Dxx_reffile=${reffile} - -Dxx_tolerance=${tolerance} - -P ${CMAKE_CURRENT_SOURCE_DIR}/RunEqualityTest.cmake) + ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} show=n datfile=${outfile} + run_tolerance=${tolerance} run_reference=${reffile} + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) add_custom_target(${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} -E env - PYTHONPATH=${CMAKE_BINARY_DIR}/lib - BA_EXAMPLE_DATA_DIR=${EXAMPLES_DATA_DIR} - ${Python3_EXECUTABLE} ${EXAMPLE_SCRIPT} + ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} figfile=${FIG_DIR}/${EXAMPLE_SUBDIR}/${EXAMPLE_NAME}.png) add_dependencies(figures ${TARGET_NAME}) endfunction() diff --git a/Tests/Examples/CheckEquality.py b/Tests/Examples/CheckEquality.py deleted file mode 100755 index 0512470e3407b589f0faa523e0f487abcb6423a8..0000000000000000000000000000000000000000 --- a/Tests/Examples/CheckEquality.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/env python3 -""" -""" - -import glob, os, sys -import bornagain as ba - - -def main(datstem, refstem, tolerance): - os.makedirs(os.path.dirname(datstem), exist_ok=True) - - datfile = datstem + ".int.gz" - reffile = refstem + ".int.gz" - - datmatches = glob.glob(datstem + ".*.int.gz") - refmatches = glob.glob(refstem + ".*.int.gz") - n_subfiles = max(len(datmatches), len(refmatches)) - - if n_subfiles == 0: - - ok = ba.filesAgree(datfile, reffile, tolerance) - if not ok: - print( - f"=> no agreement between result and reference: {datfile} {reffile}", - flush=True) - return 1 - return 0 - - # we do have subfiles - if os.path.exists(datfile): - if len(datmatches) > 0: - raise Exception( - f"Test output contains unnumbered and numbered files: {datfile} " - + "vs {datmatches}") - raise Exception( - f"Test output contains unnumbered file {datfile} " - "while there are numbered reference files {refmatches}") - if os.path.exists(reffile): - if len(refmatches) > 0: - raise Exception( - f"Reference contains unnumbered and numbered files: {reffile} " - + "vs {refmatches}") - raise Exception( - f"Reference contains unnumbered file {reffile} " - "while there are numbered test output files {datmatches}") - - if len(datmatches) < len(refmatches): - raise Exception( - f"There are less test output files than reference files: " + - "{datmatches} vs {refmatches}") - if len(datmatches) > len(refmatches): - raise Exception( - f"There are more test output files than reference files: " + - "{datmatches} vs {refmatches}") - - ok = True - for i in range(n_subfiles): - datfile = datstem + "." + str(i) + ".int.gz" - reffile = refstem + "." + str(i) + ".int.gz" - ret = ba.filesAgree(datfile, reffile, tolerance) - if not ret: - print( - f"=> no agreement between result and reference: {datfile} {reffile}", - flush=True) - ok = False - if not ok: - return 1 - return 0 - - -if __name__ == '__main__': - if len(sys.argv) != 4: - raise Exception(f'{sys.argv[0]}: invalid arguments. Expected: ' + - 'datstem refstem tolerance') - datstem, refstem = sys.argv[1:3] - tolerance = float(sys.argv[3]) - ret = main(datstem, refstem, tolerance) - sys.exit(ret) diff --git a/Tests/Examples/RunEqualityTest.cmake b/Tests/Examples/RunEqualityTest.cmake deleted file mode 100644 index 87e22733eb3164a26860072bc8e4c053d10421ff..0000000000000000000000000000000000000000 --- a/Tests/Examples/RunEqualityTest.cmake +++ /dev/null @@ -1,23 +0,0 @@ -message(STATUS "RunEqualityTest step 1: Going to run ${xx_script}") -execute_process(COMMAND ${CMAKE_COMMAND} -E env - PYTHONPATH=${xx_bindir}/lib - BA_EXAMPLE_DATA_DIR=${xx_datdir} - ${Python3_EXECUTABLE} -B - ${xx_script} show=n datfile=${xx_outfile} - TIMEOUT 10 - RESULT_VARIABLE res) -if(NOT res EQUAL 0) - message(FATAL_ERROR "RunEqualityTest ${xx_name}: script ${xx_script} failed: ${res}") -endif() - -set(xx_pycmd "${xx_srcdir}/CheckEquality.py ${xx_outfile} ${xx_reffile} ${xx_tolerance}") -message(STATUS "RunEqualityTest step 2: Going to run ${xx_pycmd}") -execute_process(COMMAND ${CMAKE_COMMAND} -E env - PYTHONPATH=${xx_bindir}/lib - ${Python3_EXECUTABLE} -B - ${xx_srcdir}/CheckEquality.py ${xx_outfile} ${xx_reffile} ${xx_tolerance} - TIMEOUT 10 - RESULT_VARIABLE res) -if(NOT res EQUAL 0) - message(FATAL_ERROR "CheckEquality ${xx_name} failed: ${res}") -endif() diff --git a/Wrap/Python/ba_plot.py b/Wrap/Python/ba_plot.py index 7c7cd49e5226ad1246ce3ac9b98fcefef9aef881..ef7ddac62cd3c5427c20259badf95fd5a6454cbe 100644 --- a/Wrap/Python/ba_plot.py +++ b/Wrap/Python/ba_plot.py @@ -52,6 +52,7 @@ rc('text', usetex=usetex_default) plotargs = {} simargs = {} +runargs = {} # ************************************************************************** # # internal functions @@ -118,8 +119,17 @@ def get_axes_labels(result, units): return labels -def check_or_save(result, fname): - ba.writeDatafield(result, fname + ".int.gz") +def check_or_save(result, fname, subname=""): + outfile = fname + subname + ".int.gz" + tolerance = runargs.get('tolerance') + reference = runargs.get('reference') + ba.writeDatafield(result, outfile) + if not tolerance or not reference: + return + reffile = reference + subname + ".int.gz" + ok = ba.filesAgree(outfile, reffile, float(tolerance)) + if not ok: + raise Exception(f"=> no agreement between result and reference: {datfile} {reffile}") # ************************************************************************** # # versatile plot calls @@ -294,8 +304,7 @@ def save_results(results, name): nDigits = int(math.log10(len(results))) + 1 formatN = "%" + str(nDigits) + "i" for i, result in enumerate(results): - fname = name + "." + (formatN % i) - check_or_save(result, fname) + check_or_save(result, name, "." + (formatN % i)) class MultiPlot: @@ -405,6 +414,8 @@ Support for bp.simargs will be removed in future releases. Replace \"bp.simargs['{key[4:]}']\" by a hard-coded value or by variable under your own control. """) + elif key[0:4] == 'run_': + runargs[key[4:]] = tmp[key] else: plotargs[key] = tmp[key] diff --git a/auto/Examples/scatter2d/ObsoleteSimN.py b/auto/Examples/scatter2d/ObsoleteSimN.py index 26ae550e680e817fda375fad5506affcb7e5a730..8112c3f2eb8ce05af847d9f71c2c083ed3715f6c 100644 --- a/auto/Examples/scatter2d/ObsoleteSimN.py +++ b/auto/Examples/scatter2d/ObsoleteSimN.py @@ -22,7 +22,6 @@ def get_simulation(sample): # Define detector nPix = bp.simargs['n'] - print(f'DEBUG nPix={nPix} in simargs=f{bp.simargs}.') detector = ba.SphericalDetector(nPix, -2*deg, 2*deg, nPix, 0, 3*deg) return ba.ScatteringSimulation(beam, sample, detector) diff --git a/auto/MiniExamples/scatter2d/ObsoleteSimN.py b/auto/MiniExamples/scatter2d/ObsoleteSimN.py index 26ae550e680e817fda375fad5506affcb7e5a730..8112c3f2eb8ce05af847d9f71c2c083ed3715f6c 100644 --- a/auto/MiniExamples/scatter2d/ObsoleteSimN.py +++ b/auto/MiniExamples/scatter2d/ObsoleteSimN.py @@ -22,7 +22,6 @@ def get_simulation(sample): # Define detector nPix = bp.simargs['n'] - print(f'DEBUG nPix={nPix} in simargs=f{bp.simargs}.') detector = ba.SphericalDetector(nPix, -2*deg, 2*deg, nPix, 0, 3*deg) return ba.ScatteringSimulation(beam, sample, detector)