Commit e65dc80a authored by Sebastian Heimann's avatar Sebastian Heimann

wip refactor

parent e4ce7284
......@@ -11,7 +11,6 @@ from pyrocko import util, marker
from pyrocko.gf import Range
import grond
import grond.toy
logger = logging.getLogger('main')
km = 1e3
......@@ -48,7 +47,7 @@ subcommand_usages = {
'forward <configfile> <eventnames> ... [options]'),
'harvest': 'harvest <rundir> [options]',
'plot': 'plot <plotnames> <rundir> [options]',
'movie': 'movie <rundir> [options]',
'movie': 'movie <rundir> <xpar> <ypar> <filetemplate> [options]',
'baraddur': 'plot-server',
'export': 'export (best|mean|ensemble|stats) <rundirs> ... [options]',
'qc-polarization': 'qc-polarization <configfile> <eventname> '
......@@ -560,7 +559,7 @@ def command_movie(args):
parser, options, args = cl_parse('movie', args, setup)
if len(args) != 4:
help_and_die(parser, 'one argument required')
help_and_die(parser, 'four arguments required')
run_path, xpar_name, ypar_name, movie_filename_template = args
......
from .base import * # noqa
from .highscore.optimizer import * # noqa
from .highscore.plot import * # noqa
from __future__ import print_function
import math
import re
import random
......@@ -653,7 +654,9 @@ def draw_contributions_figure(history, optimizer, plt):
gms_softclip = num.where(gms > 1.0, 0.1 * num.log10(gms) + 1.0, gms)
gcms = problem.global_contributions(history.misfits)
gcms = problem.combine_misfits(
history.misfits, get_contributions=True)
gcms = gcms[isort, :]
jsort = num.argsort(gcms[-1, :])[::-1]
......@@ -1405,7 +1408,9 @@ def draw_fits_figures(ds, history, optimizer, plt):
xbest = models[0, :]
ws = problem.get_target_weights()
gcms = problem.global_contributions(misfits[:1])[0]
gcms = problem.combine_misfits(
misfits[:1, :, :], get_contributions=True)[0, :]
w_max = num.nanmax(ws)
gcm_max = num.nanmax(gcms)
......@@ -1810,6 +1815,7 @@ def draw_hudson_figure(history, optimizer, plt):
beachballsize = markersize
beachballsize_small = beachballsize * 0.5
width = 7.
beachball_type = 'dc'
figsize = (width, width / (4. / 3.))
problem = history.problem
......@@ -1830,7 +1836,7 @@ def draw_hudson_figure(history, optimizer, plt):
try:
beachball.plot_beachball_mpl(
mt, axes,
beachball_type='dc',
beachball_type=beachball_type,
position=(u, v),
size=beachballsize_small,
color_t=color,
......@@ -1862,7 +1868,7 @@ def draw_hudson_figure(history, optimizer, plt):
try:
beachball.plot_beachball_mpl(
mt, axes,
beachball_type='dc',
beachball_type=beachball_type,
position=(u, v),
size=beachballsize,
color_t=color,
......@@ -1888,7 +1894,7 @@ def draw_hudson_figure(history, optimizer, plt):
try:
beachball.plot_beachball_mpl(
mt, axes,
beachball_type='dc',
beachball_type=beachball_type,
position=(u, v),
size=beachballsize,
color_t='red',
......@@ -1909,6 +1915,7 @@ def draw_location_figure(history, optimizer, plt):
# markersize_small = markersize * 0.2
beachballsize = markersize
beachballsize_small = beachballsize * 0.5
beachball_type = 'dc'
width = 7.
figsize = (width, width / (4. / 3.))
......@@ -1976,7 +1983,7 @@ def draw_location_figure(history, optimizer, plt):
try:
beachball.plot_beachball_mpl(
mt, axes,
beachball_type='dc',
beachball_type=beachball_type,
position=(sx, sy),
size=beachballsize_small,
color_t=color,
......
......@@ -296,7 +296,9 @@ class Problem(Object):
if misfits.ndim == 2:
misfits = misfits[num.newaxis, :, :]
return self.combine_misfits(misfits, extra_weights)[0, ...]
return self.combine_misfits(
misfits, extra_weights,
get_contributions=get_contributions)[0, ...]
assert misfits.ndim == 3
assert extra_weights is None or extra_weights.ndim == 2
......@@ -309,7 +311,9 @@ class Problem(Object):
if get_contributions:
return exp(w*misfits[:, num.newaxis, :, 0]) \
/ num.nansum(exp(w*misfits[:, num.newaxis, :, 1]), axis=2)
/ num.nansum(
exp(w*misfits[:, num.newaxis, :, 1]),
axis=2)[:, :, num.newaxis]
return root(
num.nansum(exp(w*misfits[:, num.newaxis, :, 0]), axis=2) /
......@@ -320,7 +324,9 @@ class Problem(Object):
if get_contributions:
return exp(w*misfits[:, :, 0]) \
/ num.nansum(exp(w*misfits[:, :, 1]), axis=1)
/ num.nansum(
exp(w*misfits[:, :, 1]),
axis=1)[:, num.newaxis]
return root(
num.nansum(exp(w*misfits[:, :, 0]), axis=1) /
......
from __future__ import print_function
import nose.tools as t
import numpy as num
from numpy.testing import assert_almost_equal as assert_ae
from pyrocko import gf
from grond.toy import scenario, ToyProblem
......@@ -32,11 +35,20 @@ def test_combine_misfits():
misfitss = p.evaluate_many(xg)
# misfitss[imodel, itarget, 0], misfitss[imodel, itarget, 1]
gms = p.combine_misfits(misfitss)
gms_contrib = p.combine_misfits(misfitss, get_contributions=True)
# gms[imodel]
# gms_contrib[imodel, itarget]
bweights = num.ones((2, p.ntargets))
gms_2 = p.combine_misfits(misfitss, extra_weights=bweights)
gms_2_contrib = p.combine_misfits(
misfitss,
extra_weights=bweights,
get_contributions=True)
# gms_2[imodel, ibootstrap]
# gms_2_contrib[imodel, ibootstrap, itarget]
for ix, x in enumerate(xg):
misfits = p.evaluate(x)
......@@ -44,8 +56,25 @@ def test_combine_misfits():
gm = p.combine_misfits(misfits)
# gm is scalar
t.assert_equal(gm, gms[ix])
gm_contrib = p.combine_misfits(
misfits,
get_contributions=True)
assert_ae(gms_contrib[ix, :], gm_contrib)
gm_2 = p.combine_misfits(misfits, extra_weights=bweights)
assert gm_2[0] == gm
assert gm_2[1] == gm
assert gms_2[ix, 0] == gm
assert gms_2[ix, 1] == gm
gm_2_contrib = p.combine_misfits(
misfits, extra_weights=bweights,
get_contributions=True)
assert_ae(gm_2_contrib[0, :], gm_contrib)
assert_ae(gm_2_contrib[1, :], gm_contrib)
assert_ae(gms_2_contrib[ix, 0, :], gm_contrib)
assert_ae(gms_2_contrib[ix, 1, :], gm_contrib)
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