Added functionality to better handle divide by zero error when taking log10.

这个提交包含在:
Craig Warren
2017-10-11 10:24:51 +01:00
父节点 e346b17cec
当前提交 e351a5fde8
共有 7 个文件被更改,包括 75 次插入29 次删除

查看文件

@@ -254,10 +254,13 @@ def dispersion_analysis(G):
# Calculate magnitude of frequency spectra of waveform
mag = np.abs(np.fft.fft(waveformvalues))**2
# Calculate power (avoiding taking a log of any zero values)
np.seterr(divide='ignore')
power = 10 * np.log10(mag)
np.seterr(divide='warn')
# Calculate power (ignore warning from taking a log of any zero values)
with np.errstate(divide='ignore'):
power = 10 * np.log10(mag)
# Replace any NaNs or Infs from zero division
power[np.invert(np.isfinite(power))] = 0
# Frequency bins
freqs = np.fft.fftfreq(power.size, d=G.dt)
# Shift powers so that frequency with maximum power is at zero decibels

查看文件

@@ -116,7 +116,7 @@ for i, model in enumerate(testmodels):
floattyperef = fileref[path + outputsref[0]].dtype
floattypetest = filetest[path + outputstest[0]].dtype
# Array for storing time
# Arrays for storing time
timeref = np.zeros((fileref.attrs['Iterations']), dtype=floattyperef)
timeref = np.arange(0, fileref.attrs['dt'] * fileref.attrs['Iterations'], fileref.attrs['dt']) / 1e-9
timetest = np.zeros((filetest.attrs['Iterations']), dtype=floattypetest)
@@ -139,8 +139,12 @@ for i, model in enumerate(testmodels):
for i in range(len(outputstest)):
max = np.amax(np.abs(dataref[:, i]))
datadiffs[:, i] = np.divide(np.abs(dataref[:, i] - datatest[:, i]), max, out=np.zeros_like(dataref[:, i]), where=max != 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'):
datadiffs[:, i] = 20 * np.log10(datadiffs[:, i]) # Ignore any zero division in log10
datadiffs[:, i] = 20 * np.log10(datadiffs[:, i])
# Replace any NaNs or Infs from zero division
datadiffs[:, i][np.invert(np.isfinite(datadiffs[:, i]))] = 0
# Store max difference
maxdiff = np.amax(np.amax(datadiffs))

查看文件

@@ -83,8 +83,13 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
# Plotting if FFT required
if fft:
# Calculate magnitude of frequency spectra of waveform
power = 10 * np.log10(np.abs(np.fft.fft(outputdata))**2)
# Calculate magnitude of frequency spectra of waveform (ignore warning from taking a log of any zero values)
with np.errstate(divide='ignore'):
power = 10 * np.log10(np.abs(np.fft.fft(outputdata))**2)
# Replace any NaNs or Infs from zero division
power[np.invert(np.isfinite(power))] = 0
# Frequency bins
freqs = np.fft.fftfreq(power.size, d=dt)
# Shift powers so that frequency with maximum power is at zero decibels

查看文件

@@ -112,14 +112,24 @@ def calculate_antenna_params(filename, tltxnumber=1, tlrxnumber=None, rxnumber=N
# Calculate input admittance
yin = np.fft.fft(Itotal) / (np.fft.fft(Vtotal) * delaycorrection)
# Convert to decibels
Vincp = 20 * np.log10(np.abs((np.fft.fft(Vinc) * delaycorrection)))
Iincp = 20 * np.log10(np.abs(np.fft.fft(Iinc)))
Vrefp = 20 * np.log10(np.abs((np.fft.fft(Vref) * delaycorrection)))
Irefp = 20 * np.log10(np.abs(np.fft.fft(Iref)))
Vtotalp = 20 * np.log10(np.abs((np.fft.fft(Vtotal) * delaycorrection)))
Itotalp = 20 * np.log10(np.abs(np.fft.fft(Itotal)))
s11 = 20 * np.log10(s11)
# Convert to decibels (ignore warning from taking a log of any zero values)
with np.errstate(divide='ignore'):
Vincp = 20 * np.log10(np.abs((np.fft.fft(Vinc) * delaycorrection)))
Iincp = 20 * np.log10(np.abs(np.fft.fft(Iinc)))
Vrefp = 20 * np.log10(np.abs((np.fft.fft(Vref) * delaycorrection)))
Irefp = 20 * np.log10(np.abs(np.fft.fft(Iref)))
Vtotalp = 20 * np.log10(np.abs((np.fft.fft(Vtotal) * delaycorrection)))
Itotalp = 20 * np.log10(np.abs(np.fft.fft(Itotal)))
s11 = 20 * np.log10(s11)
# Replace any NaNs or Infs from zero division
Vincp[np.invert(np.isfinite(Vincp))] = 0
Iincp[np.invert(np.isfinite(Iincp))] = 0
Vrefp[np.invert(np.isfinite(Vrefp))] = 0
Irefp[np.invert(np.isfinite(Irefp))] = 0
Vtotalp[np.invert(np.isfinite(Vtotalp))] = 0
Itotalp[np.invert(np.isfinite(Itotalp))] = 0
s11[np.invert(np.isfinite(s11))] = 0
# Create dictionary of antenna parameters
antennaparams = {'time': time, 'freqs': freqs, 'Vinc': Vinc, 'Vincp': Vincp, 'Iinc': Iinc, 'Iincp': Iincp,
@@ -127,7 +137,9 @@ def calculate_antenna_params(filename, tltxnumber=1, tlrxnumber=None, rxnumber=N
'Vtotal': Vtotal, 'Vtotalp': Vtotalp, 'Itotal': Itotal, 'Itotalp': Itotalp,
's11': s11, 'zin': zin, 'yin': yin}
if tlrxnumber or rxnumber:
s21 = 20 * np.log10(s21)
with np.errstate(divide='ignore'): # Ignore warning from taking a log of any zero values
s21 = 20 * np.log10(s21)
s21[np.invert(np.isfinite(s21))] = 0
antennaparams['s21'] = s21
return antennaparams

查看文件

@@ -27,8 +27,6 @@ from gprMax.exceptions import CmdInputError
from gprMax.utilities import round_value
from gprMax.waveforms import Waveform
np.seterr(divide='ignore')
def check_timewindow(timewindow, dt):
"""Checks and sets time window and number of iterations.
@@ -105,27 +103,41 @@ def mpl_plot(w, timewindow, dt, iterations, fft=False):
# Calculate pulse width for gaussian
if w.type == 'gaussian':
powerdrop = -3 # dB
start = np.where((10 * np.log10(waveform / np.amax(waveform))) > powerdrop)[0][0]
stop = np.where((10 * np.log10(waveform[start:] / np.amax(waveform))) < powerdrop)[0][0] + start
with np.errstate(divide='ignore'): # Ignore warning from taking a log of any zero values
startpower = 10 * np.log10(waveform / np.amax(waveform))
stopower = 10 * np.log10(waveform[start:] / np.amax(waveform))
# Replace any NaNs or Infs from zero division
startpower[np.invert(np.isfinite(startpower))] = 0
stopower[np.invert(np.isfinite(stopower))] = 0
start = np.where(startpower > powerdrop)[0][0]
stop = np.where(stopower < powerdrop)[0][0] + start
print('Pulse width at {:d}dB, i.e. full width at half maximum (FWHM): {:g} s'.format(powerdrop, time[stop] - time[start]))
print('Time window: {:g} s ({} iterations)'.format(timewindow, iterations))
print('Time step: {:g} s'.format(dt))
if fft:
# Calculate magnitude of frequency spectra of waveform
power = 10 * np.log10(np.abs(np.fft.fft(waveform))**2)
# Calculate magnitude of frequency spectra of waveform (ignore warning from taking a log of any zero values)
with np.errstate(divide='ignore'): #
power = 10 * np.log10(np.abs(np.fft.fft(waveform))**2)
# Replace any NaNs or Infs from zero division
power[np.invert(np.isfinite(power))] = 0
# Frequency bins
freqs = np.fft.fftfreq(power.size, d=dt)
# Shift powers so that frequency with maximum power is at zero decibels
power -= np.amax(power)
# Set plotting range to 4 times centre frequency of waveform
if w.type == 'user':
fmaxpower = np.where(power == 0)[0][0]
w.freq = freqs[fmaxpower]
print('Centre frequency: {:g} Hz'.format(w.freq))
# Set plotting range to 4 times centre frequency of waveform
pltrange = np.where(freqs > 4 * w.freq)[0][0]
pltrange = np.s_[0:pltrange]

查看文件

@@ -83,7 +83,14 @@ ax.annotate('Ground', xy=(np.deg2rad(270), 0), xytext=(8, -15), textcoords='offs
for patt in range(0, len(radii)):
pattplot = np.append(patterns[patt, :], patterns[patt, 0]) # Append start value to close circle
pattplot = pattplot / np.max(np.max(patterns)) # Normalise, based on set of patterns
ax.plot(theta, 10 * np.log10(pattplot), label='{:.2f}m'.format(radii[patt]), marker='.', ms=6, lw=1.5)
# Calculate power (ignore warning from taking a log of any zero values)
with np.errstate(divide='ignore'):
power = 10 * np.log10(pattplot)
# Replace any NaNs or Infs from zero division
power[np.invert(np.isfinite(power))] = 0
ax.plot(theta, power, label='{:.2f}m'.format(radii[patt]), marker='.', ms=6, lw=1.5)
# Add Hertzian dipole plot
# hertzplot1 = np.append(hertzian[0, :], hertzian[0, 0]) # Append start value to close circle

查看文件

@@ -13,8 +13,6 @@ import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
np.seterr(divide='ignore')
from gprMax.exceptions import GeneralError
"""This module contains fitness metric functions that can be used with the Taguchi optimisation method.
@@ -183,9 +181,14 @@ def min_sum_diffs(filename, args):
if output.attrs['Name'] in args['outputs']:
outputname = list(output.keys())[0]
modelresp = np.array(output[outputname])
# Calculate sum of differences
tmp = 20 * np.log10(np.abs(modelresp - refresp) / np.amax(np.abs(refresp)))
tmp = np.abs(np.sum(tmp[-np.isneginf(tmp)])) / len(tmp[-np.isneginf(tmp)])
with np.errstate(divide='ignore'): # Ignore warning from taking a log of any zero values
tmp = 20 * np.log10(np.abs(modelresp - refresp) / np.amax(np.abs(refresp)))
# Replace any NaNs or Infs from zero division
tmp[np.invert(np.isfinite(tmp))] = 0
tmp = np.abs(np.sum(tmp)) / len(tmp)
diffdB += tmp
outputs += 1