From e5e747ac357b852d92cf36de887fbb411b980e46 Mon Sep 17 00:00:00 2001 From: Craig Warren Date: Mon, 9 Dec 2019 17:11:11 +0000 Subject: [PATCH] Work on bug with test script - calling API in for loop. --- tests/test_input_cmd_funcs.py | 85 ----------------------------------- tests/test_models.py | 47 ++++++++++--------- 2 files changed, 27 insertions(+), 105 deletions(-) delete mode 100644 tests/test_input_cmd_funcs.py diff --git a/tests/test_input_cmd_funcs.py b/tests/test_input_cmd_funcs.py deleted file mode 100644 index d2bbdbdf..00000000 --- a/tests/test_input_cmd_funcs.py +++ /dev/null @@ -1,85 +0,0 @@ -import sys -import unittest - -# http://stackoverflow.com/a/17981937/1942837 -from contextlib import contextmanager -from io import StringIO - - -@contextmanager -def captured_output(): - new_out, new_err = StringIO(), StringIO() - old_out, old_err = sys.stdout, sys.stderr - try: - sys.stdout, sys.stderr = new_out, new_err - yield sys.stdout, sys.stderr - finally: - sys.stdout, sys.stderr = old_out, old_err - -# end stack copy - -from gprMax.input_cmd_funcs import * - - -class My_input_cmd_funcs_test(unittest.TestCase): - def assert_output(self, out, expected_out): - """helper function""" - output = out.getvalue().strip() - self.assertEqual(output, expected_out) - - def test_rx(self): - with captured_output() as (out, err): - rx(0, 0, 0) - self.assert_output(out, '#rx: 0 0 0') - - def test_rx2(self): - with captured_output() as (out, err): - rx(0, 1, 2, 'id') - self.assert_output(out, '#rx: 0 1 2 id') - - def test_rx3(self): - with captured_output() as (out, err): - rx(2, 1, 0, 'idd', ['Ex']) - self.assert_output(out, '#rx: 2 1 0 idd Ex') - - def test_rx4(self): - with captured_output() as (out, err): - rx(2, 1, 0, 'id', ['Ex', 'Ez']) - self.assert_output(out, '#rx: 2 1 0 id Ex Ez') - - def test_rx_rotate_exception(self): - with self.assertRaises(ValueError): - rx(2, 1, 0, 'id', ['Ex', 'Ez'], polarisation='x', rotate90origin=(1, 1)) # no dxdy given - - def test_rx_rotate_success(self): - with captured_output() as (out, err): - rx(2, 1, 0, 'id', ['Ex', 'Ez'], polarisation='x', rotate90origin=(1, 1), dxdy=(0, 0)) - self.assert_output(out, '#rx: 1 2 0 id Ex Ez') # note: x, y swapped - - def test_rx_rotate_success2(self): - with captured_output() as (out, err): - rx(2, 1, 0, 'id', ['Ex', 'Ez'], polarisation='y', rotate90origin=(1, 1), dxdy=(0, 0)) - self.assert_output(out, '#rx: 1 2 0 id Ex Ez') # note: x, y swapped - - def test_src_steps(self): - with captured_output() as (out, err): - src_steps() - self.assert_output(out, '#src_steps: 0 0 0') - - def test_src_steps2(self): - with captured_output() as (out, err): - src_steps(42, 43, 44.2) - self.assert_output(out, '#src_steps: 42 43 44.2') - - def test_rx_steps(self): - with captured_output() as (out, err): - rx_steps() - self.assert_output(out, '#rx_steps: 0 0 0') - - def test_rx_steps2(self): - with captured_output() as (out, err): - rx_steps(42, 43, 44.2) - self.assert_output(out, '#rx_steps: 42 43 44.2') - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_models.py b/tests/test_models.py index d06926fa..40023c0e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU General Public License # along with gprMax. If not, see . -import os +from pathlib import Path import sys from colorama import init, Fore, Style @@ -28,7 +28,7 @@ import matplotlib.pyplot as plt if sys.platform == 'linux': plt.switch_backend('agg') -from gprMax.gprMax import api +import gprMax from gprMax.exceptions import GeneralError from tests.analytical_solutions import hertzian_dipole_fs @@ -39,13 +39,17 @@ from tests.analytical_solutions import hertzian_dipole_fs python -m tests.test_models """ -basepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models_') -basepath += 'basic' -# basepath += 'advanced' -# basepath += 'pmls' +# Specify directoty with set of models to test +modelset = 'models_basic' +# modelset += 'models_advanced' +# modelset += 'models_pmls' + +basepath = Path(__file__).parents[0] / 'tests' / modelset + # List of available basic test models -testmodels = ['hertzian_dipole_fs_analytical', '2D_ExHyHz', '2D_EyHxHz', '2D_EzHxHy', 'cylinder_Ascan_2D', 'hertzian_dipole_fs', 'hertzian_dipole_hs', 'hertzian_dipole_dispersive', 'magnetic_dipole_fs'] +# testmodels = ['hertzian_dipole_fs_analytical', '2D_ExHyHz', '2D_EyHxHz', '2D_EzHxHy', 'cylinder_Ascan_2D', 'hertzian_dipole_fs', 'hertzian_dipole_hs', 'hertzian_dipole_dispersive', 'magnetic_dipole_fs'] +testmodels = ['2D_ExHyHz', '2D_EyHxHz', '2D_EzHxHy'] # List of available advanced test models # testmodels = ['antenna_GSSI_1500_fs', 'antenna_MALA_1200_fs'] @@ -67,13 +71,13 @@ for i, model in enumerate(testmodels): testresults[model] = {} # Run model - inputfile = os.path.join(basepath, model + os.path.sep + model + '.in') - api(inputfile, gpu=None) + file = basepath / model / model + gprMax.run(inputfile=file.with_suffix('.in'), gpu=None) # Special case for analytical comparison if model == 'hertzian_dipole_fs_analytical': # Get output for model file - filetest = h5py.File(os.path.join(basepath, model + os.path.sep + model + '.out'), 'r') + filetest = h5py.File(file.with_suffix('.out'), 'r') testresults[model]['Test version'] = filetest.attrs['gprMax'] # Get available field output component names @@ -103,8 +107,10 @@ for i, model in enumerate(testmodels): else: # Get output for model and reference files - fileref = h5py.File(os.path.join(basepath, model + os.path.sep + model + '_ref.out'), 'r') - filetest = h5py.File(os.path.join(basepath, model + os.path.sep + model + '.out'), 'r') + fileref = file.stem + '_ref' + fileref = file.parent / Path(fileref) + fileref = h5py.File(fileref.with_suffix('.out'), 'r') + filetest = h5py.File(file.with_suffix('.out'), 'r') testresults[model]['Ref version'] = fileref.attrs['gprMax'] testresults[model]['Test version'] = filetest.attrs['gprMax'] @@ -116,7 +122,7 @@ for i, model in enumerate(testmodels): # Check that type of float used to store fields matches if filetest[path + outputstest[0]].dtype != fileref[path + outputsref[0]].dtype: - print(Fore.RED + 'WARNING: Type of floating point number in test model ({}) does not match type in reference solution ({})\n'.format(filetest[path + outputstest[0]].dtype, fileref[path + outputsref[0]].dtype) + Style.RESET_ALL) + print(Fore.RED + f'WARNING: Type of floating point number in test model ({filetest[path + outputstest[0]].dtype}) does not match type in reference solution ({fileref[path + outputsref[0]].dtype})\n' + Style.RESET_ALL) float_or_doubleref = fileref[path + outputsref[0]].dtype float_or_doubletest = filetest[path + outputstest[0]].dtype @@ -191,15 +197,16 @@ for i, model in enumerate(testmodels): ax.grid() # Save a PDF/PNG of the figure - savename = os.path.join(basepath, model + os.path.sep + model) - # fig1.savefig(savename + '.pdf', dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1) - # fig2.savefig(savename + '_diffs.pdf', dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1) - fig1.savefig(savename + '.png', dpi=150, format='png', bbox_inches='tight', pad_inches=0.1) - fig2.savefig(savename + '_diffs.png', dpi=150, format='png', bbox_inches='tight', pad_inches=0.1) + filediffs = file.stem + '_diffs' + filediffs = file.parent / Path(filediffs) + # fig1.savefig(file.with_suffix('.pdf'), dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1) + # fig2.savefig(savediffs.with_suffix('.pdf'), dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1) + # fig1.savefig(file.with_suffix('.png'), dpi=150, format='png', bbox_inches='tight', pad_inches=0.1) + # fig2.savefig(filediffs.with_suffix('.png'), dpi=150, format='png', bbox_inches='tight', pad_inches=0.1) # Summary of results for name, data in sorted(testresults.items()): if 'analytical' in name: - print(Fore.CYAN + "Test '{}.in' using v.{} compared to analytical solution. Max difference {:.2f}dB.".format(name, data['Test version'], data['Max diff']) + Style.RESET_ALL) + print(Fore.CYAN + f"Test '{name}.in' using v.{data['Test version']} compared to analytical solution. Max difference {data['Max diff']:.2f}dB." + Style.RESET_ALL) else: - print(Fore.CYAN + "Test '{}.in' using v.{} compared to reference solution using v.{}. Max difference {:.2f}dB.".format(name, data['Test version'], data['Ref version'], data['Max diff']) + Style.RESET_ALL) + print(Fore.CYAN + f"Test '{name}.in' using v.{data['Test version']} compared to reference solution using v.{data['Ref version']}. Max difference {data['Max diff']:.2f}dB." + Style.RESET_ALL)