Add reference data to output plot and plot diffs

这个提交包含在:
nmannall
2024-07-24 10:13:22 +01:00
父节点 7d97ebcd81
当前提交 8f336d9de0
共有 3 个文件被更改,包括 68 次插入22 次删除

查看文件

@@ -1,7 +1,9 @@
import logging
from typing import Tuple
import h5py
import numpy as np
import numpy.typing as npt
from gprMax.utilities.logging import logging_config
@@ -12,7 +14,7 @@ logging_config(name=__name__)
FIELD_COMPONENTS_BASE_PATH = "/rxs/rx1/"
def get_data_from_h5_file(h5_filepath):
def get_data_from_h5_file(h5_filepath: str) -> Tuple[npt.NDArray, npt.NDArray]:
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())
@@ -23,7 +25,9 @@ def get_data_from_h5_file(h5_filepath):
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)
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])):
@@ -36,18 +40,21 @@ def get_data_from_h5_file(h5_filepath):
return time, data
def calculate_diffs(test_data, ref_data):
def calculate_diffs(test_data: npt.NDArray, ref_data: npt.NDArray) -> npt.NDArray:
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
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
# diffs[:, i][np.invert(np.isfinite(diffs[:, i]))] = 0
return diffs