Update to use pathlib.

这个提交包含在:
Craig Warren
2019-12-11 13:52:15 +00:00
父节点 2260187a21
当前提交 8df3aad4d4
共有 7 个文件被更改,包括 160 次插入100 次删除

查看文件

@@ -17,7 +17,7 @@
# along with gprMax. If not, see <http://www.gnu.org/licenses/>.
import argparse
import os
from pathlib import Path
import sys
import h5py
@@ -29,14 +29,17 @@ from gprMax.exceptions import CmdInputError
"""Plots a comparison of fields between given simulation output and experimental data files."""
# Parse command line arguments
parser = argparse.ArgumentParser(description='Plots a comparison of fields between given simulation output and experimental data files.', usage='cd gprMax; python -m tests.test_compare_experimental modelfile realfile output')
parser = argparse.ArgumentParser(description='Plots a comparison of fields between given simulation output and experimental data files.', usage='cd gprMax; python -m tests.test_experimental modelfile realfile output')
parser.add_argument('modelfile', help='name of model output file including path')
parser.add_argument('realfile', help='name of file containing experimental data including path')
parser.add_argument('output', help='output to be plotted, i.e. Ex Ey Ez', nargs='+')
args = parser.parse_args()
modelfile = Path(args.modelfile)
realfile = Path(args.realfile)
# Model results
f = h5py.File(args.modelfile, 'r')
f = h5py.File(Path(modelfile), 'r')
path = '/rxs/rx1/'
availablecomponents = list(f[path].keys())
@@ -48,7 +51,7 @@ else:
polarity = 1
if args.output[0] not in availablecomponents:
raise CmdInputError('{} output requested to plot, but the available output for receiver 1 is {}'.format(args.output[0], ', '.join(availablecomponents)))
raise CmdInputError(f"{args.output[0]} output requested to plot, but the available output for receiver 1 is {', '.join(availablecomponents)}")
floattype = f[path + args.output[0]].dtype
iterations = f.attrs['Iterations']
@@ -64,7 +67,7 @@ f.close()
modelmax = np.where(np.abs(model) == 1)[0][0]
# Real results
with open(args.realfile, 'r') as f:
with open(realfile, 'r') as f:
real = np.loadtxt(f)
real[:, 1] = real[:, 1] / np.amax(np.abs(real[:, 1]))
realmax = np.where(np.abs(real[:, 1]) == 1)[0][0]
@@ -72,19 +75,20 @@ realmax = np.where(np.abs(real[:, 1]) == 1)[0][0]
difftime = - (timemodel[modelmax] - real[realmax, 0])
# Plot modelled and real data
fig, ax = plt.subplots(num=args.modelfile + ' versus ' + args.realfile, figsize=(20, 10), facecolor='w', edgecolor='w')
fig, ax = plt.subplots(num=modelfile.stem + '_vs_' + realfile.stem, figsize=(20, 10), facecolor='w', edgecolor='w')
ax.plot(timemodel + difftime, model, 'r', lw=2, label='Model')
ax.plot(real[:, 0], real[:, 1], 'r', ls='--', lw=2, label='Experiment')
ax.set_xlabel('Time [s]')
ax.set_ylabel('Amplitude')
ax.set_xlim([0, timemodel[-1]])
ax.set_ylim([-1, 1])
# ax.set_ylim([-1, 1])
ax.legend()
ax.grid()
# Save a PDF/PNG of the figure
savename = os.path.abspath(os.path.dirname(args.modelfile)) + os.sep + os.path.splitext(os.path.split(args.modelfile)[1])[0] + '_vs_' + os.path.splitext(os.path.split(args.realfile)[1])[0]
# fig.savefig(savename + '.pdf', dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1)
# fig.savefig((savename + '.png', dpi=150, format='png', bbox_inches='tight', pad_inches=0.1)
savename = modelfile.stem + '_vs_' + realfile.stem
savename = modelfile.parent / savename
# fig.savefig(savename.with_suffix('.pdf'), dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1)
# fig.savefig(savename.with_suffix('.png'), dpi=150, format='png', bbox_inches='tight', pad_inches=0.1)
plt.show()

查看文件

@@ -44,12 +44,11 @@ modelset = 'models_basic'
# modelset += 'models_advanced'
# modelset += 'models_pmls'
basepath = Path(__file__).parents[0] / 'tests' / modelset
basepath = Path(__file__).parents[0] / 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 = ['2D_ExHyHz', '2D_EyHxHz', '2D_EzHxHy']
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']
# List of available advanced test models
# testmodels = ['antenna_GSSI_1500_fs', 'antenna_MALA_1200_fs']
@@ -72,12 +71,12 @@ for i, model in enumerate(testmodels):
# Run model
file = basepath / model / model
gprMax.run(inputfile=file.with_suffix('.in'), gpu=None)
gprMax.run(inputfile=file.with_suffix('.in'), gpu=[0])
# Special case for analytical comparison
if model == 'hertzian_dipole_fs_analytical':
# Get output for model file
filetest = h5py.File(file.with_suffix('.out'), 'r')
filetest = h5py.File(file.with_suffix('.h5'), 'r')
testresults[model]['Test version'] = filetest.attrs['gprMax']
# Get available field output component names
@@ -109,8 +108,8 @@ for i, model in enumerate(testmodels):
# Get output for model and reference files
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')
fileref = h5py.File(fileref.with_suffix('.h5'), 'r')
filetest = h5py.File(file.with_suffix('.h5'), 'r')
testresults[model]['Ref version'] = fileref.attrs['gprMax']
testresults[model]['Test version'] = filetest.attrs['gprMax']
@@ -201,8 +200,8 @@ for i, model in enumerate(testmodels):
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)
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()):