def showWC(ax, thk, wc, wmin=0., wmax=0.45, maxdep=0., dw=0.05, **kwargs): """Show water content function nicely.""" drawModel1D(ax, thk, wc, xlabel=r'$\theta$') ax.set_xlim(0., 0.45) if maxdep > 0.: ax.set_ylim(maxdep, 0.) wt = np.arange(wmin, wmax, dw) ax.set_xticks(wt) ax.set_xticklabels([str(wi) for wi in wt])
def showWC(ax, thk, wc, wmin=0., wmax=0.45, maxdep=0., dw=0.05, **kwargs): """Show water content function nicely.""" drawModel1D(ax, thk, wc, xlabel=r'$\theta$') ax.set_xlim(0., 0.45) if maxdep > 0.: ax.set_ylim(maxdep, 0.) wt = np.arange(wmin, wmax, dw) ax.set_xticks(wt) ax.set_xticklabels([str(wi) for wi in wt])
def showT2(ax, thk, t2, maxdep=0., **kwargs): """Show T2 function nicely.""" drawModel1D(ax, thk, t2 * 1e3, xlabel=r'$T_2^*$ [ms]', plot='semilogx') tmin = min(20, min(t2) * 0.9e3) tmax = max(500, max(t2) * 1.1e3) ax.set_xlim(tmin, tmax) if maxdep > 0.: ax.set_ylim(maxdep, 0.) xt = [20, 50, 100, 200, 500] ax.set_xticks(xt) ax.set_xticklabels([str(ai) for ai in xt])
def showT2(ax, thk, t2, maxdep=0., **kwargs): """Show T2 function nicely.""" drawModel1D(ax, thk, t2*1e3, xlabel=r'$T_2^*$ [ms]', plot='semilogx') tmin = min(20, min(t2) * 0.9e3) tmax = max(500, max(t2) * 1.1e3) ax.set_xlim(tmin, tmax) if maxdep > 0.: ax.set_ylim(maxdep, 0.) xt = [20, 50, 100, 200, 500] ax.set_xticks(xt) ax.set_xticklabels([str(ai) for ai in xt])
def showModelAndData(self, model, xpos=0, response=None, figsize=(8, 6)): """Show both model and data with response in subfigures.""" fig, ax = plt.subplots(1, 3, figsize=figsize) model = np.asarray(model) nlay = int((len(model) + 1) / 2) thk = model[:nlay - 1] res = model[nlay - 1:2 * nlay - 1] drawModel1D(ax[0], thk, res, plotfunction='semilogx') self.plotData(xpos, response, ax=ax[1:3], clf=False) return fig, ax
def showModelAndData(self, model, xpos=0, response=None, figsize=(8, 6)): """Show both model and data with response in subfigures.""" fig, ax = plt.subplots(1, 3, figsize=figsize) model = np.asarray(model) nlay = int((len(model) + 1) / 2) thk = model[:nlay - 1] res = model[nlay - 1: 2 * nlay - 1] drawModel1D(ax[0], thk, res, plotfunction='semilogx') self.plotData(xpos, response, ax=ax[1:3], clf=False) return fig, ax
def showResults(self, ax=None, syn=None, color=None): """Shows the Results of the inversion.""" if ax is None: fig, ax = plt.subplots(ncols=1, figsize=(8, 6)) if syn is not None: drawModel1D(ax, syn[0], syn[1], color='b', label='synthetic', plotfunction='semilogx') if color is None: color = 'g' if self.type == 'smooth': drawModel1D(ax, self.Z, self.resDistribution, color=color, label=r'$\lambda$={:2.2f}'.format( self.INV.getLambda())) else: thk, rho = self.splitBlockModel() drawModel1D(ax, thk, rho, color=color, label=r'$\ chi^2$={:2.2f}'.format(self.INV.getChi2())) ax.grid(True, which='both') ax.legend(loc='best') return ax
def plotPopulation(self, maxfitness=None, fitratio=1.05, savefile=True): """Plot fittest individuals (fitness<maxfitness) as 1d models Parameters ---------- maxfitness : float maximum fitness value (absolute) OR fitratio : float [1.05] maximum ratio to minimum fitness """ if maxfitness is None: maxfitness = min(self.fits) * fitratio fig, ax = plt.subplots(1, 2, sharey=True) self.figs['population'] = fig maxz = 0 for ind in self.pop: if ind.fitness < maxfitness: model = np.asarray(self.genMod(ind.candidate)) thk = model[:self.nlay - 1] wc = model[self.nlay - 1:self.nlay * 2 - 1] t2 = model[self.nlay * 2 - 1:] drawModel1D(ax[0], thk, wc * 100, color='grey') drawModel1D(ax[1], thk, t2 * 1000, color='grey') maxz = max(maxz, sum(thk)) model = np.asarray(self.genMod(self.pop[0].candidate)) thk = model[:self.nlay - 1] wc = model[self.nlay - 1:self.nlay * 2 - 1] t2 = model[self.nlay * 2 - 1:] drawModel1D(ax[0], thk, wc * 100, color='black', linewidth=3) drawModel1D(ax[1], thk, t2 * 1000, color='black', linewidth=3, plotfunction='semilogx') ax[0].set_xlim(self.lowerBound[1] * 100, self.upperBound[1] * 100) ax[0].set_ylim((maxz * 1.2, 0)) ax[1].set_xlim(self.lowerBound[2] * 1000, self.upperBound[2] * 1000) ax[1].set_ylim((maxz * 1.2, 0)) xt = [10, 20, 50, 100, 200, 500, 1000] ax[1].set_xticks(xt) ax[1].set_xticklabels([str(xti) for xti in xt]) if savefile: fig.savefig(self.EAstatfile.replace('.csv', '.pdf'), bbox_inches='tight') plt.show()
def showResults(self, ax=None, syn=None, color=None): """Shows the Results of the inversion.""" if ax is None: fig, ax = plt.subplots(ncols=1, figsize=(8, 6)) if syn is not None: drawModel1D(ax, syn[0], syn[1], color='b', label='synthetic', plotfunction='semilogx') if color is None: color = 'g' if self.type == 'smooth': drawModel1D(ax, self.Z, self.resDistribution, color=color, label=r'$\lambda$={:2.2f}' .format(self.INV.getLambda())) else: thk, rho = self.splitBlockModel() drawModel1D(ax, thk, rho, color=color, label=r'$\ chi^2$={:2.2f}' .format(self.INV.getChi2())) ax.grid(True, which='both') ax.legend(loc='best') return ax
f.region(0).setTransModel(transThk) f.region(1).setTransModel(transRho) paraDepth = max(ab2) / 3 f.region(0).setStartValue(max(ab2) / 3. / nlay / 2.) f.region(1).setStartValue(pg.median(rhoa)) model = f.createStartVector() model[nlay] *= 1.5 inv = pg.RInversion(rhoa, f, True) inv.setModel(model) inv.setTransData(transRhoa) inv.setRelativeError(errPerc / 100.0) inv.setLambda(lam) inv.setMarquardtScheme(0.9) model = inv.run() fig, ax = plt.subplots(nrows=2) ax[0].loglog(rhoa, ab2, 'rx-', inv.response(), ab2, 'b-') ax[0].set_ylim(max(ab2), min(ab2)) ax[0].grid(which='both') ax[0].set_xlabel(r"\rho_a in \Omegam") ax[0].set_ylabel("AB/2 in m") ax[0].set_legend(("measured", "fitted"), loc="upper left") res, thk = model(nlay-1, nlay*2-1), model(0, nlay-1) drawModel1D(ax[1], thk, res, xlabel=r'\rho in \Omega m', plot='semilogx') drawModel1D(ax[1], [0.5, 3.5, 6.], [100., 500., 20., 1000.]) ax[1].grid(which='both') plt.show()
invDCEM.setMarquardtScheme(0.9) modelDCEM = invDCEM.run() respDCEM = invDCEM.response() ############################################################################### # The results of the inversion are plotted for comparison. for inv in [invEM, invDC, invDCEM]: inv.echoStatus() print([invEM.chi2(), invDC.chi2(), invDCEM.chi2()]) # chi-square values ############################################################################### # %% We finally plot the results fig = plt.figure(1, figsize=(10, 5)) ax1 = fig.add_subplot(131) drawModel1D(ax1, thk, res, plot='semilogx', color='blue') drawModel1D(ax1, modelEM(0, nlay - 1), modelEM(nlay - 1, nlay * 2 - 1), color='green') drawModel1D(ax1, modelDC(0, nlay - 1), modelDC(nlay - 1, nlay * 2 - 1), color='cyan') drawModel1D(ax1, modelDCEM(0, nlay - 1), modelDCEM(nlay - 1, nlay * 2 - 1), color='red') ax1.legend(('syn', 'EM', 'DC', 'JI')) ax1.set_xlim((10., 1000.)) ax1.set_ylim((40., 0.))
# Decrease the regularization (smoothness) and start (from old result) print("inversion with lam=20") inv.setLambda(10) res10 = inv.run() # result is a pg.RVector, but compatible to numpy array print('rrms={:.2f}%, chi^2={:.3f}'.format(inv.relrms(), inv.chi2())) # We now optimize lambda such that data are fitted within noise (chi^2=1) print("chi^2=1 optimized inversion") resChi = inv.runChi1() # ends up in a lambda of about 3 print("optimized lambda value:", inv.getLambda()) print('rrms={:.2f}%, chi^2={:.3f}'.format(inv.relrms(), inv.chi2())) ############################################################################### # Show model (inverted and synthetic) as well as model response and data fig, ax = plt.subplots(ncols=2, figsize=(8, 6)) # two-column figure drawModel1D(ax[0], synthk, synres, color='b', label='synthetic', plot='semilogx') drawModel1D(ax[0], thk, res100, color='g', label=r'$\lambda$=100') drawModel1D(ax[0], thk, res10, color='c', label=r'$\lambda$=10') drawModel1D(ax[0], thk, resChi, color='r', label=r'$\chi$={:.2f}'.format(inv.getLambda())) ax[0].grid(True, which='both') ax[0].legend(loc='best') ax[1].loglog(rhoa, ab2, 'rx-', label='measured') ax[1].loglog(inv.response(), ab2, 'b-', label='fitted') ax[1].set_ylim((max(ab2), min(ab2))) ax[1].grid(True, which='both')
# could also be set by inv.setTransData(transRhoa) ############################################################################### # set error model, regularization strength and Marquardt scheme inv.setRelativeError(errPerc / 100.0) # alternative: setAbsoluteError in Ohmm inv.setLambda(lam) # (initial) regularization parameter inv.setMarquardtScheme(0.9) # decrease lambda by factor 0.9 model = f.createStartVector() # creates from region start value model[nlay] *= 1.5 # change default model by changing 2nd layer resistivity inv.setModel(model) # ############################################################################### # run actual inversion and extract resistivity and thickness model = inv.run() # result is a pg.RVector, but compatible to numpy array res, thk = model[nlay-1:nlay*2-1], model[0:nlay-1] print('rrms={:.2f}%, chi^2={:.3f}'.format(inv.relrms(), inv.chi2())) ############################################################################### # show estimated&synthetic models and data with model response in 2 subplots fig, ax = plt.subplots(ncols=2, figsize=(8, 6)) # two-column figure drawModel1D(ax[0], synthk, synres, plot='semilogx', color='r') drawModel1D(ax[0], thk, res, color='b') ax[0].grid(True, which='both') ax[0].set_ylabel('z (m)') ax[0].set_xlabel(r'$\rho$ ($\Omega$m)') ax[1].loglog(rhoa, ab2, 'rx-', label='data') # sounding curve ax[1].loglog(inv.response(), ab2, 'b-', label='response') ax[1].set_ylim((max(ab2), min(ab2))) # downwards according to penetration ax[1].grid(True, which='both') ax[1].set_xlabel(r'$\rho_a$ ($\Omega$m)') ax[1].set_ylabel('AB/2 (m)') ax[1].legend(loc='best') plt.show()
print("inversion with lam=20") inv.setLambda(10) res10 = inv.run() # result is a pg.RVector, but compatible to numpy array inv.echoStatus() # We now optimize lambda such that data are fitted within noise (chi^2=1) print("chi^2=1 optimized inversion") resChi = inv.runChi1() # ends up in a lambda of about 3 inv.echoStatus() print("optimized lambda value:", inv.getLambda()) ############################################################################### # show everything fig, ax = plt.subplots(ncols=2, figsize=(8, 6)) # two-column figure # plot model (inverted and synthetic) drawModel1D(ax[0], synthk, synres, color='b', label='synthetic', plotfunction='semilogx') drawModel1D(ax[0], thk, res100, color='g', label=r'$\lambda$=100') drawModel1D(ax[0], thk, res10, color='c', label=r'$\lambda$=10') drawModel1D(ax[0], thk, resChi, color='r', label=r'$\chi$=1') ax[0].grid(True, which='both') ax[0].legend(loc='best') # plot sounding curve data and model response ax[1].loglog(rhoa, ab2, 'rx-', label='measured') ax[1].loglog(inv.response(), ab2, 'b-', label='fitted') ax[1].set_ylim((max(ab2), min(ab2))) ax[1].grid(True, which='both') ax[1].set_xlabel(r'$\rho_a$ [$\Omega$m]') ax[1].set_ylabel('AB/2 [m]') ax[1].legend(loc='best')
inv.setLambda(lam) # (initial) regularization parameter inv.setMarquardtScheme(0.9) # decrease lambda by factor 0.9 model = f.createStartVector() # creates from region start value ############################################################################### # optionally change default model by changing a layer resistivity model[nlay] *= 1.5 inv.setModel(model) # ############################################################################### # run actual inversion model = inv.run() # result is a pg.RVector, but compatible to numpy array res, thk = model[nlay-1:nlay*2-1], model[0:nlay-1] ############################################################################### # show everything fig, ax = plt.subplots(ncols=2, figsize=(8, 6)) # two-column figure # plot model (inverted and synthetic) drawModel1D(ax[0], thk, res, color='r') # r'\rho in \Omega m') drawModel1D(ax[0], synthk, synres, color='b') print(thk) ax[0].grid(True, which='both') # plot sounding curve data and model response ax[1].loglog(rhoa, ab2, 'rx-', label='measured') ax[1].loglog(inv.response(), ab2, 'b-', label='fitted') ax[1].set_ylim((max(ab2), min(ab2))) ax[1].grid(True, which='both') ax[1].set_xlabel(r'$\rho_a$ [$\Omega$m]') ax[1].set_ylabel('AB/2 [m]') ax[1].legend(loc='best')
def plotResult(self, filename=None, nrows=1, figsize=(10, 6)): """plot Result as three (time 1 or more) plots""" nl = self.nlay thk = self.model[:nl - 1] wc = self.model[nl - 1:2 * nl - 1] t2 = self.model[2 * nl - 1:3 * nl - 1] res = self.model[3 * nl - 1:4 * nl - 1] fig, ax = plt.subplots(nrows=nrows, ncols=3, sharey=(nrows == 1), figsize=figsize, squeeze=False) drawModel1D(ax[0, 0], thk, wc * 100., xlabel=r'$\theta$ [%]') drawModel1D(ax[0, 1], thk, t2 * 1e3, plotfunction='semilogx', xlabel=r'$T_2^*$ [ms]') drawModel1D(ax[0, 2], thk, res, plotfunction='semilogx') if self.modelL is not None and self.modelU is not None: thkL, thkU = self.modelL[:nl - 1], self.modelU[:nl - 1] wcL, wcU = self.modelL[nl - 1:2 * nl - 1], self.modelU[nl - 1:2 * nl - 1] t2L, t2U = self.modelL[2 * nl - 1:3 * nl - 1], self.modelU[2 * nl - 1:3 * nl - 1] resL, resU = self.modelL[3 * nl - 1:4 * nl - 1], self.modelU[3 * nl - 1:4 * nl - 1] zc = np.cumsum(thk) zm = np.hstack((zc - thk / 2, np.sum(thk) + 3.)) ax[0, 0].errorbar((wc[:-1] + wc[1:]) / 2 * 100., zc, fmt='.', yerr=np.vstack((thk - thkL, thkU - thk))) ax[0, 0].errorbar(wc * 100., zm, fmt='.', xerr=np.vstack((wc - wcL, wcU - wc)) * 100.) ax[0, 1].set_xlim(self.lowerBound[1] * 100., self.upperBound[1] * 100.) ax[0, 1].errorbar(np.sqrt(t2[:-1] * t2[1:]) * 1e3, zc, fmt='.', yerr=np.vstack((thk - thkL, thkU - thk))) ax[0, 1].errorbar(t2 * 1e3, zm, fmt='.', xerr=np.vstack((t2 - t2L, t2U - t2)) * 1e3) ax[0, 1].set_xlim(self.lowerBound[2] * 1e3, self.upperBound[2] * 1e3) ax[0, 2].errorbar(np.sqrt(res[:-1] * res[1:]), zc, fmt='.', yerr=np.vstack((thk - thkL, thkU - thk))) ax[0, 2].errorbar(res, zm, fmt='.', xerr=np.vstack((res - resL, resU - res))) ax[0, 2].set_xlim(self.lowerBound[3], self.upperBound[3]) ax[0, 2].set_xlabel(r'$\rho$ [$\Omega$m]') if filename is not None: fig.savefig(filename, bbox_inches='tight') return fig, ax
res100 = inv.run() # result is a pg.RVector, but compatible to numpy array print('rrms={:.2f}%, chi^2={:.3f}'.format(inv.relrms(), inv.chi2())) # Decrease the regularization (smoothness) and start (from old result) print("inversion with lam=20") inv.setLambda(10) res10 = inv.run() # result is a pg.RVector, but compatible to numpy array print('rrms={:.2f}%, chi^2={:.3f}'.format(inv.relrms(), inv.chi2())) # We now optimize lambda such that data are fitted within noise (chi^2=1) print("chi^2=1 optimized inversion") resChi = inv.runChi1() # ends up in a lambda of about 3 print("optimized lambda value:", inv.getLambda()) print('rrms={:.2f}%, chi^2={:.3f}'.format(inv.relrms(), inv.chi2())) ############################################################################### # Show model (inverted and synthetic) as well as model response and data fig, ax = plt.subplots(ncols=2, figsize=(8, 6)) # two-column figure drawModel1D(ax[0], synthk, synres, color='b', label='synthetic', plot='semilogx') drawModel1D(ax[0], thk, res100, color='g', label=r'$\lambda$=100') drawModel1D(ax[0], thk, res10, color='c', label=r'$\lambda$=10') drawModel1D(ax[0], thk, resChi, color='r', label=r'$\chi$={:.2f}'.format(inv.getLambda())) ax[0].grid(True, which='both') ax[0].legend(loc='best') ax[1].loglog(rhoa, ab2, 'rx-', label='measured') ax[1].loglog(inv.response(), ab2, 'b-', label='fitted') ax[1].set_ylim((max(ab2), min(ab2))) ax[1].grid(True, which='both') ax[1].set_xlabel(r'$\rho_a$ [$\Omega$m]') ax[1].set_ylabel('AB/2 [m]') ax[1].yaxis.set_label_position('right') ax[1].yaxis.set_ticks_position('right') ax[1].legend(loc='best')
# could also be set by inv.setTransData(transRhoa) ############################################################################### # set error model, regularization strength and Marquardt scheme inv.setRelativeError(errPerc / 100.0) # alternative: setAbsoluteError in Ohmm inv.setLambda(lam) # (initial) regularization parameter inv.setMarquardtScheme(0.9) # decrease lambda by factor 0.9 model = f.createStartVector() # creates from region start value model[nlay] *= 1.5 # change default model by changing 2nd layer resistivity inv.setModel(model) # ############################################################################### # run actual inversion and extract resistivity and thickness model = inv.run() # result is a pg.RVector, but compatible to numpy array res, thk = model[nlay - 1:nlay * 2 - 1], model[0:nlay - 1] print('rrms={:.2f}%, chi^2={:.3f}'.format(inv.relrms(), inv.chi2())) ############################################################################### # show estimated&synthetic models and data with model response in 2 subplots fig, ax = plt.subplots(ncols=2, figsize=(8, 6)) # two-column figure drawModel1D(ax[0], synthk, synres, plot='semilogx', color='r') drawModel1D(ax[0], thk, res, color='b') ax[0].grid(True, which='both') ax[0].set_ylabel('z (m)') ax[0].set_xlabel(r'$\rho$ ($\Omega$m)') ax[1].loglog(rhoa, ab2, 'rx-', label='data') # sounding curve ax[1].loglog(inv.response(), ab2, 'b-', label='response') ax[1].set_ylim((max(ab2), min(ab2))) # downwards according to penetration ax[1].grid(True, which='both') ax[1].set_xlabel(r'$\rho_a$ ($\Omega$m)') ax[1].set_ylabel('AB/2 (m)') ax[1].legend(loc='best') plt.show()
invDCEM.setMarquardtScheme(0.9) modelDCEM = invDCEM.run() respDCEM = invDCEM.response() ############################################################################### # The results of the inversion are plotted for comparison. for inv in [invEM, invDC, invDCEM]: inv.echoStatus() print([invEM.chi2(), invDC.chi2(), invDCEM.chi2()]) # chi-square values ############################################################################### # %% We finally plot the results fig = plt.figure(1, figsize=(10, 5)) ax1 = fig.add_subplot(131) drawModel1D(ax1, thk, res, plot='semilogx', color='blue') drawModel1D(ax1, modelEM(0, nlay-1), modelEM(nlay-1, nlay*2-1), color='green') drawModel1D(ax1, modelDC(0, nlay-1), modelDC(nlay-1, nlay*2-1), color='cyan') drawModel1D(ax1, modelDCEM(0, nlay-1), modelDCEM(nlay-1, nlay*2-1), color='red') ax1.legend(('syn', 'EM', 'DC', 'JI')) ax1.set_xlim((10., 1000.)) ax1.set_ylim((40., 0.)) ax1.grid(which='both') ax2 = fig.add_subplot(132) ax2.semilogy(dataEM(0, nf), freq, 'bx', label='syn IP') ax2.semilogy(dataEM(nf, nf*2), freq, 'bo', label='syn OP') ax2.semilogy(respEM(0, nf), freq, 'g--', label='EM') ax2.semilogy(respEM(nf, nf*2), freq, 'g--') ax2.semilogy(respDCEM(na, na+nf), freq, 'r:', label='DCEM') ax2.semilogy(respDCEM(na+nf, na+nf*2), freq, 'r:')