/[MITgcm]/MITgcm_contrib/mlosch/optim_m1qn3/plotfc.py
ViewVC logotype

Contents of /MITgcm_contrib/mlosch/optim_m1qn3/plotfc.py

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed May 9 09:46:25 2018 UTC (5 years, 10 months ago) by mlosch
Branch: MAIN
File MIME type: text/x-python
- add some basic instructions to compile optim_m1qn3/optim.x and
  run an optimization based on tutorial_global_oce_optim
- add a python script that plots the cost function values as a function
  of m1qn3 iteration (not simulation!)

1 #!/usr/bin/env python
2 # -*- coding: iso-8859-15 -*-
3 ######################## -*- coding: utf-8 -*-
4 """Usage: plotfc.py INPUTFILE
5 """
6 import matplotlib.pyplot as plt
7 import numpy as np
8 import sys
9 from getopt import gnu_getopt as getopt
10 # parse command-line arguments
11 try:
12 optlist,args = getopt(sys.argv[1:], ':', ['verbose'])
13 print(len(args))
14 assert len(args) == 1
15 except (AssertionError):
16 sys.exit(__doc__)
17
18 fname=args[0]
19 print("reading from "+fname)
20
21 def get_output (fname, mystring):
22 """parse fname and get some numbers out"""
23 iters = []
24 simuls= []
25 fc = []
26 try:
27 f=open(fname)
28 except:
29 print(fname + " does not exist, continuing")
30 else:
31 for line in f:
32 if mystring in line:
33 ll = line.split()
34 iters.append( int(ll[2].replace(',','')))
35 simuls.append(int(ll[4].replace(',','')))
36 fc.append( float(ll[6].replace('D','e').replace(',','')))
37
38 return iters, simuls, fc
39
40 iters, simuls, fc = get_output(fname, "f=")
41 # sort out restarts
42 iters0 = np.asarray(iters)
43 for k,it in enumerate(iters[0:]):
44 if iters0[k]<iters0[k-1]:
45 iters0[k:] = iters0[k:]+(iters0[k-1]-iters0[k]+1)
46
47 fig, ax1 = plt.subplots()
48 ax1.semilogy(iters0,(np.asarray(fc)-fc[-1]),'bx-')
49 ax1.set_xlabel('iterations')
50 ax1.set_ylabel('fc-min(fc)', color='b')
51 ax1.tick_params('y', colors='b')
52 ax1.grid()
53
54 ax2 = ax1.twinx()
55 ax2.plot(iters0,simuls, 'r.')
56 ax2.set_ylabel('# simulations', color='r')
57 ax2.tick_params('y', colors='r')
58
59 plt.show()

  ViewVC Help
Powered by ViewVC 1.1.22