Commit 040cce1f authored by marius's avatar marius
Browse files

Improvements SolverState // Listener

parent d04383de
......@@ -42,17 +42,17 @@ class TerminalListener(Listener):
lines.append(t)
out_ln = self.row_name +\
''.join([self.parameter_fmt] * len(state.parameter_values))
''.join([self.parameter_fmt] * len(state.parameter_sets))
col_param_width = max([len(p) for p in state.parameter_names]) + 2
l('Problem name: {s.problem_name}'
'\t({s.runtime:s} - remaining {s.runtime_remaining})'
.format(s=state))
l('Iteration {s.iiter} / {s.niter} ({s.iter_per_second:.1f} iter/s)'
l('Iteration {s.iiter} / {s.niter}\t\t({s.iter_per_second:.1f} iter/s)'
.format(s=state))
l(out_ln.format(
*['Parameter'] + state.column_names,
*['Parameter'] + state.parameter_sets.keys(),
col_param_width=col_param_width,
col_width=self.col_width,
type='s'))
......@@ -60,7 +60,7 @@ class TerminalListener(Listener):
for ip, parameter_name in enumerate(state.parameter_names):
l(out_ln.format(
parameter_name,
*[v[ip] for v in state.parameter_values],
*[v[ip] for v in state.parameter_sets.values()],
col_param_width=col_param_width,
col_width=self.col_width,
type='.4g'))
......
......@@ -2,6 +2,7 @@ import logging
import time
import numpy as num
from datetime import timedelta
from collections import OrderedDict
from pyrocko.guts import Object
......@@ -10,6 +11,11 @@ guts_prefix = 'grond'
logger = logging.getLogger('grond.solver')
class SimpleTimedelta(timedelta):
def __str__(self):
return timedelta.__str__(self).split('.')[0]
class RingBuffer(num.ndarray):
def __init__(self, *args, **kwargs):
num.ndarray.__init__(self, *args, **kwargs)
......@@ -24,16 +30,18 @@ class RingBuffer(num.ndarray):
class SolverState(object):
problem_name = ''
parameter_sets = OrderedDict()
parameter_names = []
parameter_values = []
column_names = []
extra_text = ''
starttime = time.time()
niter = 0
_iiter = 0
iter_per_second = 0.
_iter_buffer = RingBuffer(20)
starttime = time.time()
extra_text = ''
_iiter = 0
_iter_buffer = RingBuffer(25)
_last_update = time.time()
@property
......@@ -50,14 +58,14 @@ class SolverState(object):
@property
def runtime(self):
return timedelta(seconds=time.time() - self.starttime)
return SimpleTimedelta(seconds=time.time() - self.starttime)
@property
def runtime_remaining(self):
if self.iter_per_second == 0.:
return timedelta()
return timedelta(seconds=(self.niter - self.iiter)
/ self.iter_per_second)
return SimpleTimedelta()
return SimpleTimedelta(seconds=(self.niter - self.iiter)
/ self.iter_per_second)
class Solver(object):
......
......@@ -3,6 +3,8 @@ import logging
import numpy as num
from collections import OrderedDict
from pyrocko.guts import StringChoice, Int, Float, Bool
from ..meta import GrondError, Forbidden
......@@ -101,9 +103,19 @@ def solve(problem,
pnames = problem.parameter_names
state.problem_name = problem.name
state.column_names = ['B mean', 'B std',
'G mean', 'G std', 'G best']
state.parameter_names = problem.parameter_names + ['Misfit']
state.parameter_sets = OrderedDict()
state.parameter_sets['BS mean'] = num.zeros(problem.nparameters + 1)
state.parameter_sets['BS std'] = num.zeros(problem.nparameters + 1)
state.parameter_sets['Global mean'] = num.zeros(problem.nparameters + 1)
state.parameter_sets['Global std'] = num.zeros(problem.nparameters + 1)
state.parameter_sets['Global best'] = num.zeros(problem.nparameters + 1)
for par in state.parameter_sets.values():
par.fill(num.nan)
state.niter = niter
if plot:
......@@ -350,12 +362,16 @@ def solve(problem,
else:
assert False, 'invalid standard_deviation_estimator choice'
state.parameter_values = [
num.append(mbx, num.nan),
num.append(sbx, num.nan),
num.append(mgx, num.mean(gms)),
num.append(sgx, num.std(gms)),
num.append(bgx, num.min(gms))]
state.parameter_sets['BS mean'][:-1] = mbx
state.parameter_sets['BS std'][:-1] = sbx
state.parameter_sets['Global mean'][:-1] = mgx
state.parameter_sets['Global std'][:-1] = sgx
state.parameter_sets['Global best'][:-1] = bgx
state.parameter_sets['Global mean'][-1] = num.mean(gms)
state.parameter_sets['Global std'][-1] = num.std(gms)
state.parameter_sets['Global best'][-1] = num.min(gms)
state.iiter = iiter + 1
state.extra_text =\
'Phase: %s (factor %d); ntries %d, ntries_preconstrain %d'\
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment