你已经派生过 gprMax
镜像自地址
https://gitee.com/sunhf/gprMax.git
已同步 2025-08-07 04:56:51 +08:00
Consolidated min/max functions into a single function. Added more robust checking of output names when reading output files.
这个提交包含在:
107
user_libs/optimisation_taguchi/fitness_functions.py
普通文件 -> 可执行文件
107
user_libs/optimisation_taguchi/fitness_functions.py
普通文件 -> 可执行文件
@@ -24,73 +24,41 @@ from gprMax.exceptions import GeneralError
|
||||
The second argument is a dictionary which can contain any number of additional arguments, e.g. names (IDs) of outputs (rxs) from input file
|
||||
"""
|
||||
|
||||
def minvalue(filename, args):
|
||||
def min_max_value(filename, args):
|
||||
"""Minimum value from a response.
|
||||
|
||||
Args:
|
||||
filename (str): Name of output file
|
||||
args (dict): 'outputs' key with a list of names (IDs) of outputs (rxs) from input file
|
||||
args (dict): 'type' key with string 'min', 'max' or 'absmax'; 'outputs' key with a list of names (IDs) of outputs (rxs) from input file
|
||||
|
||||
Returns:
|
||||
minvalue (float): Magnitude of minimum value from specific outputs
|
||||
value (float): Minimum, maximum, or absolute maximum value from specific outputs
|
||||
"""
|
||||
|
||||
f = h5py.File(filename, 'r')
|
||||
nrx = f.attrs['nrx']
|
||||
|
||||
value = 0
|
||||
outputsused = False
|
||||
for rx in range(1, nrx + 1):
|
||||
output = f['/rxs/rx' + str(rx) + '/']
|
||||
if output.attrs['Name'] in args['outputs']:
|
||||
outputname = list(output.keys())[0]
|
||||
minvalue = np.amin(output[outputname])
|
||||
if args['type'] == 'min':
|
||||
value += np.abs(np.amin(output[outputname]))
|
||||
elif args['type'] == 'max':
|
||||
value += np.amax(output[outputname])
|
||||
elif args['type'] == 'absmax':
|
||||
value += np.amax(np.abs(output[outputname]))
|
||||
else:
|
||||
raise GeneralError('type must be either min, max, or absmax')
|
||||
outputsused = True
|
||||
|
||||
return np.abs(minvalue)
|
||||
# Check in case no outputs where found
|
||||
if not outputsused:
|
||||
raise GeneralError('No outputs matching {} were found'.format(args['outputs']))
|
||||
|
||||
|
||||
def maxvalue(filename, args):
|
||||
"""Maximum value from a response.
|
||||
|
||||
Args:
|
||||
filename (str): Name of output file
|
||||
args (dict): 'outputs' key with a list of names (IDs) of outputs (rxs) from input file
|
||||
|
||||
Returns:
|
||||
maxvalue (float): Maximum value from specific outputs
|
||||
"""
|
||||
|
||||
f = h5py.File(filename, 'r')
|
||||
nrx = f.attrs['nrx']
|
||||
|
||||
for rx in range(1, nrx + 1):
|
||||
output = f['/rxs/rx' + str(rx) + '/']
|
||||
if output.attrs['Name'] in args['outputs']:
|
||||
outputname = list(output.keys())[0]
|
||||
maxvalue = np.amax(output[outputname])
|
||||
|
||||
return maxvalue
|
||||
|
||||
|
||||
def maxabsvalue(filename, args):
|
||||
"""Maximum absolute value from a response.
|
||||
|
||||
Args:
|
||||
filename (str): Name of output file
|
||||
args (dict): 'outputs' key with a list of names (IDs) of outputs (rxs) from input file
|
||||
|
||||
Returns:
|
||||
maxabsvalue (float): Maximum absolute value from specific outputs
|
||||
"""
|
||||
|
||||
f = h5py.File(filename, 'r')
|
||||
nrx = f.attrs['nrx']
|
||||
|
||||
for rx in range(1, nrx + 1):
|
||||
output = f['/rxs/rx' + str(rx) + '/']
|
||||
if output.attrs['Name'] in args['outputs']:
|
||||
outputname = list(output.keys())[0]
|
||||
maxabsvalue = np.amax(np.abs(output[outputname]))
|
||||
|
||||
return maxabsvalue
|
||||
return value
|
||||
|
||||
|
||||
def xcorr(filename, args):
|
||||
@@ -107,8 +75,8 @@ def xcorr(filename, args):
|
||||
# Load (from text file) the reference response. See if file exists at specified path and if not try input file directory
|
||||
refrespfile = os.path.abspath(args['refresp'])
|
||||
if not os.path.isfile(refrespfile):
|
||||
raise GeneralError('Cannot load reference response at {}'.format(refrespfile))
|
||||
with open(refresp, 'r') as f:
|
||||
raise GeneralError('Cannot load reference response from {}'.format(refrespfile))
|
||||
with open(refrespfile, 'r') as f:
|
||||
refdata = np.loadtxt(f)
|
||||
reftime = refdata[:,0] * 1e-9
|
||||
refresp = refdata[:,1]
|
||||
@@ -118,18 +86,24 @@ def xcorr(filename, args):
|
||||
nrx = f.attrs['nrx']
|
||||
modeltime = np.arange(0, f.attrs['dt'] * f.attrs['Iterations'], f.attrs['dt'])
|
||||
|
||||
outputsused = False
|
||||
for rx in range(1, nrx + 1):
|
||||
output = f['/rxs/rx' + str(rx) + '/']
|
||||
if output.attrs['Name'] in args['outputs']:
|
||||
outputname = list(output.keys())[0]
|
||||
modelresp = output[outputname]
|
||||
# Convert field value (V/m) to voltage
|
||||
# Convert electric field value (V/m) to voltage (V)
|
||||
if outputname == 'Ex':
|
||||
modelresp *= -f.attrs['dx, dy, dz'][0]
|
||||
elif outputname == 'Ey':
|
||||
modelresp *= -f.attrs['dx, dy, dz'][1]
|
||||
elif outputname == 'Ez':
|
||||
modelresp *= -f.attrs['dx, dy, dz'][2]
|
||||
outputsused = True
|
||||
|
||||
# Check in case no outputs where found
|
||||
if not outputsused:
|
||||
raise GeneralError('No outputs matching {} were found'.format(args['outputs']))
|
||||
|
||||
# Normalise reference respose and response from output file
|
||||
# refresp /= np.amax(np.abs(refresp))
|
||||
@@ -214,6 +188,10 @@ def min_sum_diffs(filename, args):
|
||||
diffdB += tmp
|
||||
outputs += 1
|
||||
|
||||
# Check in case no outputs where found
|
||||
if outputs = 0:
|
||||
raise GeneralError('No outputs matching {} were found'.format(args['outputs']))
|
||||
|
||||
return diffdB / outputs
|
||||
|
||||
|
||||
@@ -235,6 +213,7 @@ def compactness(filename, args):
|
||||
time = np.linspace(0, 1, iterations)
|
||||
time *= (iterations * dt)
|
||||
|
||||
outputsused = False
|
||||
for rx in range(1, nrx + 1):
|
||||
output = f['/rxs/rx' + str(rx) + '/']
|
||||
if output.attrs['Name'] in args['outputs']:
|
||||
@@ -267,8 +246,11 @@ def compactness(filename, args):
|
||||
# time2threshold = time[peaks[durationthresholdexist[0]]]
|
||||
# compactness = time2threshold - time[min(peaks)]
|
||||
|
||||
return compactness
|
||||
# Check in case no outputs where found
|
||||
if not outputsused:
|
||||
raise GeneralError('No outputs matching {} were found'.format(args['outputs']))
|
||||
|
||||
return compactness
|
||||
|
||||
|
||||
######################################
|
||||
@@ -288,11 +270,10 @@ def spectral_centroid(x, samplerate):
|
||||
|
||||
magnitudes = np.abs(np.fft.rfft(x))
|
||||
length = len(x)
|
||||
|
||||
# Positive frequencies
|
||||
freqs = np.abs(np.fft.fftfreq(length, 1.0/samplerate)[:length//2+1])
|
||||
|
||||
centroid = np.sum(magnitudes*freqs) / np.sum(magnitudes)
|
||||
|
||||
return centroid
|
||||
|
||||
|
||||
@@ -309,6 +290,7 @@ def zero_crossings(x):
|
||||
pos = x > 0
|
||||
npos = ~pos
|
||||
indexzeros = ((pos[:-1] & npos[1:]) | (npos[:-1] & pos[1:])).nonzero()[0]
|
||||
|
||||
return indexzeros
|
||||
|
||||
|
||||
@@ -374,16 +356,3 @@ def peakdet(v, delta, x = None):
|
||||
lookformax = True
|
||||
|
||||
return maxtab, mintab
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
在新工单中引用
屏蔽一个用户