Path: blob/devel/elmerice/examples/Inverse_Methods/SCRIPTS/MakeReport.py
3206 views
#1# Make a report from output files of the inverse method2# make basic plots of cost function and norm of the gradient3# and report last lines of M1QN3_<RUN_NAME>.out4# Require files: M1QN3_<RUN_NAME>.out5# Cost_<RUN_NAME>.dat6# CostReg_<RUN_NAME>.dat7# GradientNormAdjoint_<RUN_NAME>.dat8from collections import deque9from matplotlib import pyplot as plt10from matplotlib.backends.backend_pdf import PdfPages11from pylab import genfromtxt;12import numpy as np13import sys1415try:16sys.argv[1]17except:18print('error: try ')19print(' python MakeReport.py RUN_NAME')20exit()2122# Output from M1QN323fname='M1QN3_%s.out'%(sys.argv[1])24try:25with open(fname) as file:26pass27except IOError as e:28print('Unable to open file %s'%(fname))29exit()3031with open(fname) as fin:32last = deque(fin, 7)33output=''.join(list(last))3435# Cost file36fname='Cost_%s.dat'%(sys.argv[1])37try:38with open(fname) as file:39pass40except IOError as e:41print('Unable to open file %s'%(fname))42exit()43cost = genfromtxt(fname,skip_header=3);4445# Gradient norm46fname='GradientNormAdjoint_%s.dat'%(sys.argv[1])47try:48with open(fname) as file:49pass50except IOError as e:51print('Unable to open file %s'%(fname))52exit()53grad = genfromtxt(fname,skip_header=1);5455# Regularisation56fname='CostReg_%s.dat'%(sys.argv[1])57try:58with open(fname) as file:59pass60except IOError as e:61print('Unable to open file %s'%(fname))62exit()63reg = genfromtxt(fname,skip_header=3);64with open(fname, 'r') as f:65first_line = f.readline().strip()66print(first_line.split((',')))6768# get regularisation patrameter69line = open(fname, "r").readlines()[1]70lreg=float(line.split((','))[1])71print(lreg)7273# Make the report74with PdfPages('Report_%s.pdf'%(sys.argv[1])) as pdf:75fig = plt.figure()76fig.suptitle("Convergence plots", fontsize=16)77plt.subplot(3,1,1)78plt.semilogx(cost[:,0],cost[:,2])79plt.ylabel('rms (m/a)')8081plt.subplot(3,1,2)82plt.loglog(cost[:,0],cost[:,1],label='$J_0$')83plt.loglog(reg[:,0],lreg*reg[:,1],label=('%1.2e$ * J_{reg}$'%(lreg)))84plt.legend(fontsize='10')85plt.ylim((0.01*np.amin(cost[:,1]),5*np.amax(cost[:,1])))86plt.ylabel('$J$')8788plt.subplot(3,1,3)89plt.loglog(grad[:,0],grad[:,1]/grad[0,1])90plt.xlabel('iter. number')91plt.ylabel(r'$|\!|g|\!|/|\!|g_0|\!|$')92plt.tight_layout()93fig.subplots_adjust(top=0.88)94pdf.savefig()95plt.close()9697fig = plt.figure()98fig.suptitle("M1QN3 last 7 lines", fontsize=16)99fig.text(.1,.5,output)100pdf.savefig() # saves the current figure into a pdf page101plt.close()102103104