Split up reframe tests and pytest unit tests

这个提交包含在:
nmannall
2024-02-09 11:59:34 +00:00
父节点 8b603c165e
当前提交 474b7f52f7
共有 48 个文件被更改,包括 4 次插入6 次删除

查看文件

@@ -0,0 +1,53 @@
import logging
import h5py
import numpy as np
from gprMax.utilities.logging import logging_config
logger = logging.getLogger(__name__)
logging_config(name=__name__)
FIELD_COMPONENTS_BASE_PATH = "/rxs/rx1/"
def get_data_from_h5_file(h5_filepath):
with h5py.File(h5_filepath, "r") as h5_file:
# Get available field output component names and datatype
field_components = list(h5_file[FIELD_COMPONENTS_BASE_PATH].keys())
dtype = h5_file[FIELD_COMPONENTS_BASE_PATH + field_components[0]].dtype
shape = h5_file[FIELD_COMPONENTS_BASE_PATH + str(field_components[0])].shape
# Arrays for storing field data
if len(shape) == 1:
data = np.zeros((h5_file.attrs["Iterations"], len(field_components)), dtype=dtype)
else: # Merged B-scan data
data = np.zeros((h5_file.attrs["Iterations"], len(field_components), shape[1]), dtype=dtype)
for index, field_component in enumerate(field_components):
data[:, index] = h5_file[FIELD_COMPONENTS_BASE_PATH + str(field_component)]
if np.any(np.isnan(data[:, index])):
logger.exception("Data contains NaNs")
raise ValueError
max_time = (h5_file.attrs["Iterations"] - 1) * h5_file.attrs["dt"] / 1e-9
time = np.linspace(0, max_time, num=h5_file.attrs["Iterations"])
return time, data
def calculate_diffs(test_data, ref_data):
diffs = np.zeros(test_data.shape, dtype=np.float64)
for i in range(test_data.shape[1]):
maxi = np.amax(np.abs(ref_data[:, i]))
diffs[:, i] = np.divide(
np.abs(ref_data[:, i] - test_data[:, i]), maxi, out=np.zeros_like(ref_data[:, i]), where=maxi != 0
) # Replace any division by zero with zero
# Calculate power (ignore warning from taking a log of any zero values)
with np.errstate(divide="ignore"):
diffs[:, i] = 20 * np.log10(diffs[:, i])
# Replace any NaNs or Infs from zero division
diffs[:, i][np.invert(np.isfinite(diffs[:, i]))] = 0
return diffs

查看文件

@@ -0,0 +1,9 @@
import os
import reframe.utility.sanity as sn
@sn.deferrable
def path_join(*path):
"""Deferable version of os.path.join"""
return os.path.join(*path)

查看文件

@@ -0,0 +1,85 @@
import numpy as np
from matplotlib import pyplot as plt
def _plot_data(subplots, time, data, label=None, colour="r", line_style="-"):
for i in range(data.shape[1]):
subplots[i].plot(time, data[:, i], colour, lw=2, ls=line_style, label=label)
def plot_dataset_comparison(test_time, test_data, ref_time, ref_data, model_name):
fig, ((ex, hx), (ey, hy), (ez, hz)) = plt.subplots(
nrows=3,
ncols=2,
sharex=False,
sharey="col",
subplot_kw=dict(xlabel="Time [ns]"),
figsize=(20, 10),
facecolor="w",
edgecolor="w",
)
subplots = [ex, ey, ez, hx, hy, hz]
_plot_data(subplots, test_time, test_data, model_name)
_plot_data(subplots, ref_time, ref_data, f"{model_name} (Ref)", "g", "--")
ylabels = [
"$E_x$, field strength [V/m]",
"$H_x$, field strength [A/m]",
"$E_y$, field strength [V/m]",
"$H_y$, field strength [A/m]",
"$E_z$, field strength [V/m]",
"$H_z$, field strength [A/m]",
]
x_max = max(np.max(test_time), np.max(ref_time))
for i, ax in enumerate(fig.axes):
ax.set_ylabel(ylabels[i])
ax.set_xlim(0, x_max)
ax.grid()
ax.legend()
return fig
def plot_diffs(time, diffs, plot_min=-160):
"""Plots ...
Args:
time:
diffs:
plot_min: minimum value of difference to plot (dB). Default: -160
Returns:
plt: matplotlib plot object.
"""
fig, ((ex, hx), (ey, hy), (ez, hz)) = plt.subplots(
nrows=3,
ncols=2,
sharex=False,
sharey="col",
subplot_kw=dict(xlabel="Time [ns]"),
figsize=(20, 10),
facecolor="w",
edgecolor="w",
)
_plot_data([ex, ey, ez, hx, hy, hz], time, diffs)
ylabels = [
"$E_x$, difference [dB]",
"$H_x$, difference [dB]",
"$E_y$, difference [dB]",
"$H_y$, difference [dB]",
"$E_z$, difference [dB]",
"$H_z$, difference [dB]",
]
x_max = np.max(time)
y_max = np.max(diffs)
for i, ax in enumerate(fig.axes):
ax.set_ylabel(ylabels[i])
ax.set_xlim(0, x_max)
ax.set_ylim(plot_min, y_max)
ax.grid()
return fig

查看文件

@@ -0,0 +1,52 @@
import argparse
import re
from datetime import datetime
from pathlib import Path
import pandas as pd
def get_parameter(row, parameter):
value = re.search(f"\s%{parameter}=(?P<value>\S+)\s", row["info"])["value"]
return value
def get_parameter_names(item):
return re.findall(f"\s%(?P<name>\S+)=\S+", item)
columns_to_keep = ["num_tasks", "num_cpus_per_task", "num_tasks_per_node", "run_time_value", "simulation_time_value"]
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser(
usage="cd gprMax/reframe_tests; python -m utilities.process_perflog inputfile [-o OUTPUT]",
description="Extract perfvars from reframe perflog file.",
)
parser.add_argument("inputfile", help="name of input file including path")
parser.add_argument("--output", "-o", help="name of output file including path", required=False)
args = parser.parse_args()
perflog = pd.read_csv(args.inputfile, index_col=False)
# Extract recorded parameters and create a new column for them in the dataframe
parameters = perflog["info"].agg(get_parameter_names).explode().unique()
for parameter in parameters:
perflog[parameter] = perflog.apply(get_parameter, args=[parameter], axis=1)
# Organise dataframe
columns_to_keep += parameters.tolist()
columns_to_keep.sort()
perflog = perflog[columns_to_keep].sort_values(columns_to_keep)
perflog["simulation_time_value"] = perflog["simulation_time_value"].apply(round, args=[2])
perflog = perflog.rename(columns={"simulation_time_value": "simulation_time", "run_time_value": "run_time"})
# Save output to file
if args.output:
outputfile = args.output
else:
stem = f"{Path(args.inputfile).stem}_{datetime.today().strftime('%Y-%m-%d_%H-%M-%S')}"
outputfile = Path("benchmarks", stem).with_suffix(".csv")
perflog.to_csv(outputfile, index=False)
print(f"Saved benchmark: '{outputfile}'")