Sampleset RestartΒΆ

This example demonstrates restarting a sampleset run using a log file from a run where not all samples ran. To imitate the case where not all samples ran in the first attempt, the log file is rewritten with 5 samples missing. Then the restart_logfile option is used in the run command using the modified log file. The result is that the second run attempt reads in the completed runs from the log file and then runs the incomplete runs. The end result is the sampleset object contains the results of all samples

%matplotlib inline
import sys,os
try:
    import matk
except:
    try:
        sys.path.append(os.path.join('..','src'))
        import matk
    except ImportError as err:
        print 'Unable to load MATK module: '+str(err)
import numpy
from scipy import arange, randn, exp
from multiprocessing import freeze_support

# Model function
def dbexpl(p):
    t=arange(0,100,20.)
    y =  (p['par1']*exp(-p['par2']*t) + p['par3']*exp(-p['par4']*t))
    #nm =  ['o1','o2','o3','o4','o5']
    #return dict(zip(nm,y))
    return y

# Setup MATK model with parameters
p = matk.matk(model=dbexpl)
p.add_par('par1',min=0,max=1)
p.add_par('par2',min=0,max=0.2)
p.add_par('par3',min=0,max=1)
p.add_par('par4',min=0,max=0.2)
# Create LHS sample
s = p.parstudy(nvals=[2,2,2,2])
print s.samples.values
[[ 0.   0.   0.   0. ]
 [ 0.   0.   0.   0.2]
 [ 0.   0.   1.   0. ]
 [ 0.   0.   1.   0.2]
 [ 0.   0.2  0.   0. ]
 [ 0.   0.2  0.   0.2]
 [ 0.   0.2  1.   0. ]
 [ 0.   0.2  1.   0.2]
 [ 1.   0.   0.   0. ]
 [ 1.   0.   0.   0.2]
 [ 1.   0.   1.   0. ]
 [ 1.   0.   1.   0.2]
 [ 1.   0.2  0.   0. ]
 [ 1.   0.2  0.   0.2]
 [ 1.   0.2  1.   0. ]
 [ 1.   0.2  1.   0.2]]
# Run model with parameter samples
out = s.run( cpus=2, outfile='results.dat', logfile='log.dat',verbose=False)
# To imitate an incomlete sample run, we will delete some lines in log.dat
with open('log.dat','r') as fh:
    lns = fh.readlines()

# Remove last 5 lines of file
with open('log.dat','w') as fh:
    for l in lns[0:-5]:
        fh.write(l)

# Remove results.dat so that it is generated from scratch
os.remove('results.dat')
# Rerun using restart_logfile option so that only incomplete runs are executed
# Change name of logfile so that results can be inspected
out = s.run( cpus=2, outfile='results.dat', logfile='log2.dat',verbose=False,restart_logfile='log.dat')
# Now take a look at log.dat, log2.dat and results.dat
# log.dat will be missing 5 samples
# log2.dat will contain the 5 missing samples
# results.dat will contain all samples in order
print s.samples.values
[[ 0.   0.   0.   0. ]
 [ 0.   0.   0.   0.2]
 [ 0.   0.   1.   0. ]
 [ 0.   0.   1.   0.2]
 [ 0.   0.2  0.   0. ]
 [ 0.   0.2  0.   0.2]
 [ 0.   0.2  1.   0. ]
 [ 0.   0.2  1.   0.2]
 [ 1.   0.   0.   0. ]
 [ 1.   0.   0.   0.2]
 [ 1.   0.   1.   0. ]
 [ 1.   0.   1.   0.2]
 [ 1.   0.2  0.   0. ]
 [ 1.   0.2  0.   0.2]
 [ 1.   0.2  1.   0. ]
 [ 1.   0.2  1.   0.2]]
print s.responses.values
[[  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00]
 [  1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00]
 [  1.00000000e+00   1.83156389e-02   3.35462628e-04   6.14421235e-06
    1.12535175e-07]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00]
 [  1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00]
 [  1.00000000e+00   1.83156389e-02   3.35462628e-04   6.14421235e-06
    1.12535175e-07]
 [  1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00]
 [  1.00000000e+00   1.00000000e+00   1.00000000e+00   1.00000000e+00
    1.00000000e+00]
 [  2.00000000e+00   2.00000000e+00   2.00000000e+00   2.00000000e+00
    2.00000000e+00]
 [  2.00000000e+00   1.01831564e+00   1.00033546e+00   1.00000614e+00
    1.00000011e+00]
 [  1.00000000e+00   1.83156389e-02   3.35462628e-04   6.14421235e-06
    1.12535175e-07]
 [  1.00000000e+00   1.83156389e-02   3.35462628e-04   6.14421235e-06
    1.12535175e-07]
 [  2.00000000e+00   1.01831564e+00   1.00033546e+00   1.00000614e+00
    1.00000011e+00]
 [  2.00000000e+00   3.66312778e-02   6.70925256e-04   1.22884247e-05
    2.25070349e-07]]