Commit 361fab94 authored by Sebastian Heimann's avatar Sebastian Heimann

cleanup logging and outputs

parent e65dc80a
import copy
import time
import logging
import numpy as num
from pyrocko import util
from pyrocko.guts import Object, Int
from ..targets import TargetAnalysisResult
from ..meta import Forbidden
logger = logging.getLogger('grond.analysers.base')
guts_prefix = 'grond'
......@@ -14,7 +17,19 @@ class Analyser(object):
def __init__(self, niter):
self.niter = niter
self.pbar = util.progressbar('analysing problem', niter)
def log_progress(self, problem, iiter, niter):
t = time.time()
if self._tlog_last < t - 10. \
or iiter == 0 \
or iiter == niter - 1:
logger.info(
'%s at %i/%i (%s, %i/%i)' % (
problem.name,
iiter, niter))
self._tlog_last = t
def analyse(self, problem):
if self.niter == 0:
......@@ -41,9 +56,9 @@ class Analyser(object):
isbad_mask = None
self.pbar.start()
self._tlog_last = 0
for iiter in range(self.niter):
self.pbar.update(iiter)
self.log_progress(iiter, self.niter)
while True:
x = []
for ipar in range(npar):
......@@ -65,7 +80,6 @@ class Analyser(object):
mss[iiter, :] = ms
isbad_mask = num.isnan(ms)
self.pbar.finish()
mean_ms = num.mean(mss, axis=0)
weights = 1. / mean_ms
......
......@@ -12,7 +12,7 @@ from pyrocko.gf import Range
import grond
logger = logging.getLogger('main')
logger = logging.getLogger('grond.main')
km = 1e3
......
......@@ -341,7 +341,7 @@ def check(
if show_plot:
from matplotlib import pyplot as plt
from grond.plot import colors
from pyrocko import plot
markers = []
for ievent, event_name in enumerate(event_names):
......@@ -523,7 +523,7 @@ def check(
ydata = result.filtered_obs.get_ydata() / yabsmax
axes.plot(xdata, ydata*0.5 + 3.5, color='black')
color = colors[ii % len(colors)]
color = plot.mpl_graph_color(ii)
xdata = result.filtered_syn.get_xdata()
ydata = result.filtered_syn.get_ydata()
......@@ -660,22 +660,6 @@ def process_event(ievent, g_data_id):
if synt and synt.inject_solution:
xs_inject = synt.get_x()[num.newaxis, :]
# from matplotlib import pyplot as plt
# from grond import plot
# splot = plot.SolverPlot(
# plt, 'time', 'magnitude',
# show=False,
# update_every=10,
# movie_filename='grond_opt_time_magnitude.mp4')
def startThreads():
from .listeners import terminal
term = terminal.TerminalListener(rundir)
term.start()
return term
term = startThreads()
try:
optimizer = config.optimizer_config.get_optimizer()
if xs_inject is not None:
......@@ -695,8 +679,6 @@ def process_event(ievent, g_data_id):
except BadProblem as e:
logger.error(str(e))
finally:
term.join()
tstop = time.time()
logger.info(
......
......@@ -9,7 +9,7 @@ from grond.problems import ModelHistory
from .base import Listener
logger = logging.getLogger('TerminalListener')
logger = logging.getLogger('grond.listeners.terminal')
class RingBuffer(num.ndarray):
......@@ -77,7 +77,7 @@ class TerminalListener(Listener):
@property
def runtime(self):
return timedelta(seconds=time.time() - self.starttime)
return timedelta(seconds=round(time.time() - self.starttime))
@property
def iiter(self):
......@@ -95,8 +95,8 @@ class TerminalListener(Listener):
def runtime_remaining(self):
if self.iter_per_second == 0.:
return timedelta()
return timedelta(seconds=(self.niter - self.iiter)
/ self.iter_per_second)
return timedelta(seconds=round((self.niter - self.iiter)
/ self.iter_per_second))
def extend(self, *args):
self.iiter = self.history.nmodels
......@@ -115,26 +115,31 @@ class TerminalListener(Listener):
ladd('Iteration {s.iiter} / {s.niter}'
.format(s=self))
out_ln = self.row_name +\
''.join([self.parameter_fmt] * len(problem.parameter_names))
col_param_width = max([len(p) for p in problem.parameter_names]) + 2
ladd(out_ln.format(
*['Parameter'] + list(problem.parameter_names),
col_param_width=col_param_width,
col_width=self.col_width,
type='s'))
# for ip, parameter_name in enumerate(problem.parameter_names):
# ladd(out_ln.format(
# parameter_name,
# *[fmt(v[ip]) for v in problem.parameter_sets.values()],
# col_param_width=col_param_width,
# col_width=self.col_width))
# ladd(problem.extra_text.format(
# col_param_width=col_param_width,
# col_width=self.col_width,))
for parameter in problem.parameters:
ladd(' {p.name} {values}'.format(p=parameter, values=values))
if False:
out_ln = self.row_name +\
''.join([self.parameter_fmt] * len(problem.parameter_names))
ladd(out_ln.format(
*['Parameter'] + list(problem.parameter_names),
col_param_width=col_param_width,
col_width=self.col_width,
type='s'))
for ip, parameter_name in enumerate(problem.parameter_names):
ladd(out_ln.format(
parameter_name,
*[fmt(v[ip]) for v in problem.parameter_sets.values()],
col_param_width=col_param_width,
col_width=self.col_width))
ladd(problem.extra_text.format(
col_param_width=col_param_width,
col_width=self.col_width,))
lines[0:0] = ['\033[2J']
ladd('')
......
......@@ -5,7 +5,7 @@ from grond.meta import GrondError
guts_prefix = 'grond'
logger = logging.getLogger('grond.solver')
logger = logging.getLogger('grond.optimizers.base')
class BadProblem(GrondError):
......
......@@ -3,6 +3,7 @@ import math
import os.path as op
import os
import logging
import time
import numpy as num
......@@ -17,7 +18,7 @@ from grond.problems.base import ModelHistory
guts_prefix = 'grond'
logger = logging.getLogger('grond.optimizers.highscore')
logger = logging.getLogger('grond.optimizers.highscore.optimizer')
def excentricity_compensated_probabilities(xs, sbx, factor):
......@@ -398,6 +399,20 @@ class HighScoreOptimizer(Optimizer):
assert False, 'out of bounds'
def log_progress(self, problem, iiter, niter, phase, iiter_phase):
t = time.time()
if self._tlog_last < t - 10. \
or iiter_phase == 0 \
or iiter_phase == phase.niterations - 1:
logger.info(
'%s at %i/%i (%s, %i/%i)' % (
problem.name,
iiter, niter,
phase.__class__.__name__, iiter_phase, phase.niterations))
self._tlog_last = t
def optimize(self, problem, rundir=None):
if rundir is not None:
......@@ -408,8 +423,11 @@ class HighScoreOptimizer(Optimizer):
niter = self.niterations
isbad_mask = None
self._tlog_last = 0
for iiter in range(niter):
phase, iiter_phase = self.get_sampler_phase(iiter)
self.log_progress(problem, iiter, niter, phase, iiter_phase)
x = phase.get_sample(problem, iiter_phase, chains)
if isbad_mask is not None and num.any(isbad_mask):
......
......@@ -15,7 +15,7 @@ from ..targets import WaveformMisfitTarget, SatelliteMisfitTarget
guts_prefix = 'grond'
logger = logging.getLogger('grond')
logger = logging.getLogger('grond.problems.base')
km = 1e3
as_km = dict(scale_factor=km, scale_unit='km')
......
......@@ -10,7 +10,7 @@ from grond.meta import Forbidden, expand_template, Parameter
from ..base import Problem, ProblemConfig
guts_prefix = 'grond'
logger = logging.getLogger('grond.problems').getChild('cmt')
logger = logging.getLogger('grond.problems.cmt.problem')
km = 1e3
as_km = dict(scale_factor=km, scale_unit='km')
......
......@@ -10,7 +10,7 @@ from ..base import Problem, ProblemConfig
guts_prefix = 'grond'
logger = logging.getLogger('grond.problems').getChild('double_dc')
logger = logging.getLogger('grond.problems.double_dc.problem')
km = 1e3
as_km = dict(scale_factor=km, scale_unit='km')
......
......@@ -9,7 +9,7 @@ from grond.meta import expand_template, Parameter
from ..base import Problem, ProblemConfig
guts_prefix = 'grond'
logger = logging.getLogger('grond.problems').getChild('rectangular')
logger = logging.getLogger('grond.problems.rectangular.problem')
km = 1e3
as_km = dict(scale_factor=km, scale_unit='km')
......
......@@ -5,7 +5,7 @@ from pyrocko import gf
from ..base import MisfitTarget, MisfitConfig, MisfitResult, TargetGroup
guts_prefix = 'grond'
logger = logging.getLogger('grond.target').getChild('gnss')
logger = logging.getLogger('grond.targets.gnss.target')
class GNSSTargetGroup(TargetGroup):
......
......@@ -9,7 +9,7 @@ from grond.meta import Parameter
from ..base import MisfitTarget, MisfitConfig, MisfitResult, TargetGroup
guts_prefix = 'grond'
logger = logging.getLogger('grond.target').getChild('satellite')
logger = logging.getLogger('grond.targets.satellite.target')
class SatelliteTargetGroup(TargetGroup):
......
......@@ -13,7 +13,7 @@ from ..base import (MisfitConfig, MisfitTarget, TargetGroup,
MisfitResult, TargetAnalysisResult)
guts_prefix = 'grond'
logger = logging.getLogger('grond.target').getChild('waveform')
logger = logging.getLogger('grond.targets.waveform.target')
class WaveformTargetGroup(TargetGroup):
......
Markdown is supported
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