1 import cPickle
2
3 import die
4 import mcmc
5
6 import mcmc_indexclass as IC
7 import mcmc_newlogger as LGI
8
9
10 HUGE = 1e38
11
13 """This and prepares to restart a mcmc.py optimization
14 from a log file.
15 @param arglist: the list of arguments to the optimization routine (i.e. the linux command line).
16 @param adjust_prms: a function to make arbitrary changes to the parameters before you restart.
17 @type adjust_prms: None or C{function(}L{mcmc_indexclass.index_base<IC.index_base>}, L{mcmc.problem_definition}, L{mcmc_indexclass.index<IC.index>}C{) ->} L{mcmc_indexclass.index_base<IC.index_base>}.
18 The parameters go into the first argument. The definition of the new problem is the second argument.
19 The third argument is a random indexer for the new problem.
20 (This is provided mostly so you can conveniently find out what parameters are expected.)
21 @type arglist: list(str)
22 @note: This assumes that an optimization does not require any fixed parameters.
23 @note: Don't mix -restart and -Restart.
24 @note: The -Restart flag will be treated as -restart if you attempt to adjust parameters.
25 """
26 argliste = list(arglist)
27 ll_ll = []
28 hdrs = {}
29 while arglist and arglist[0] in ['-restart', '-Restart']:
30 assert len(arglist)>=2
31
32 trust_logp = adjust_prms is None and arglist[0]=='-Restart'
33 hdr, lll = LGI.read_tail_uid(arglist[1], uid, Nsamp=-1)
34 ll_ll.extend(lll)
35 hdrs.update(hdr)
36 arglist = arglist[2:]
37 if 'Argv' in hdrs:
38 argliste = cPickle.loads(hdrs['Argv']) + arglist
39
40 ll_ll.reverse()
41
42 pd = pd_factory(list(argliste), hdrs)
43 guess = IC.guess(pd.PriorProbDist)
44
45 tries = 0
46 while True:
47 try:
48 pd.logp_guts(guess)
49 except mcmc.NotGoodPosition, ex:
50 die.warn("Initial position is not good: %s" % str(ex))
51 tries += 1
52 else:
53 break
54 if tries > 20:
55 die.die("Cannot find a good starting parameter in %d tries." % tries)
56 die.info("Adjustable parameters: %s" % ' '.join(guess.columns()))
57 n = 4 + guess.n()
58 pd.set_idxr( IC.guess_to_indexer(guess) )
59
60 def no_adjustment(x, j1, j2):
61 return x
62
63 if adjust_prms is None:
64 adjust_prms = no_adjustment
65
66 def lop_gen(lll, guess):
67 """Yield some initial values to seed the iteration."""
68 for ll in lll:
69 ic = IC.reindex(adjust_prms(ll.idxr(), pd, pd.idxr), pd.idxr)
70
71 if trust_logp:
72 pos = psn_factory(ic.get_prms(), pd, logp=ll.logp())
73 else:
74 pos = psn_factory(ic.get_prms(), pd)
75 yield pos
76 while True:
77 yield psn_factory(guess.get_prms(), pd)
78
79
80 while len(ll_ll) > n:
81 del ll_ll[-1]
82 if trust_logp:
83 smi = -1
84 smlp = HUGE
85 for (i, ll) in enumerate(ll_ll):
86 if ll.logp() < smlp:
87 smlp = ll.logp()
88 smi = i
89 if smi >= 0:
90 del ll_ll[smi]
91 return (argliste, pd, lop_gen(ll_ll, guess), len(ll_ll))
92