Browse Source

refactored optimizer -> optimiser

whitelist
miili 2 years ago
parent
commit
106c14bc98
22 changed files with 98 additions and 98 deletions
  1. +2
    -2
      setup.py
  2. +1
    -1
      src/__init__.py
  3. +6
    -6
      src/apps/grond.py
  4. +3
    -3
      src/config.py
  5. +11
    -11
      src/core.py
  6. +9
    -9
      src/environment.py
  7. +8
    -8
      src/monitor.py
  8. +2
    -0
      src/optimisers/__init__.py
  9. +6
    -6
      src/optimisers/base.py
  10. +1
    -0
      src/optimisers/highscore/__init__.py
  11. +16
    -16
      src/optimisers/highscore/optimiser.py
  12. +8
    -8
      src/optimisers/highscore/plot.py
  13. +5
    -5
      src/optimisers/plot.py
  14. +0
    -2
      src/optimizers/__init__.py
  15. +0
    -1
      src/optimizers/highscore/__init__.py
  16. +2
    -2
      src/plot/main.py
  17. +2
    -2
      src/problems/base.py
  18. +4
    -4
      src/problems/plot.py
  19. +2
    -2
      src/scenario.py
  20. +3
    -3
      src/targets/gnss_campaign/plot.py
  21. +3
    -3
      src/targets/satellite/plot.py
  22. +4
    -4
      src/toy.py

+ 2
- 2
setup.py View File

@ -26,8 +26,8 @@ setup(
'grond.problems.cmt',
'grond.problems.double_dc',
'grond.problems.rectangular',
'grond.optimizers',
'grond.optimizers.highscore',
'grond.optimisers',
'grond.optimisers.highscore',
'grond.analysers',
'grond.analysers.target_balancing',
'grond.report',


+ 1
- 1
src/__init__.py View File

@ -5,6 +5,6 @@ from .problems import * # noqa
from .targets import * # noqa
from .meta import * # noqa
from .synthetic_tests import * # noqa
from .optimizers import * # noqa
from .optimisers import * # noqa
__version__ = '0.2'

+ 6
- 6
src/apps/grond.py View File

@ -29,11 +29,11 @@ subcommand_descriptions = {
'init': 'create project structure or print example configuration',
'events': 'print available event names for given configuration',
'check': 'check data and configuration',
'go': 'run Grond optimization',
'go': 'run Grond optimisation',
'forward': 'run forward modelling',
'harvest': 'manually run harvesting',
'plot': 'plot optimization result',
'movie': 'visualize optimizer evolution',
'plot': 'plot optimisation result',
'movie': 'visualize optimiser evolution',
'export': 'export results',
'report': 'create result report',
'report-index': 'create report index',
@ -374,7 +374,7 @@ def command_init(args):
kite_scenes=['*all'],
misfit_config=grond.SatelliteMisfitConfig(
use_weight_focal=False,
optimize_orbital_ramp=True,
optimise_orbital_ramp=True,
ranges={
'offset': '-0.5 .. 0.5',
'ramp_north': '-1e-4 .. 1e-4',
@ -399,14 +399,14 @@ def command_init(args):
engine_config = grond.EngineConfig(
gf_store_superdirs=['.'])
optimizer_config = grond.HighScoreOptimizerConfig()
optimiser_config = grond.HighScoreOptimiserConfig()
config = grond.Config(
rundir_template=op.join('rundir', '${problem_name}.grun'),
dataset_config=dataset_config,
target_groups=target_groups,
problem_config=problem_config,
optimizer_config=optimizer_config,
optimiser_config=optimiser_config,
engine_config=engine_config)
events = '''name = 2011-myanmar


+ 3
- 3
src/config.py View File

@ -7,7 +7,7 @@ from .dataset import DatasetConfig
from .analysers.base import AnalyserConfig
from .analysers.target_balancing import TargetBalancingAnalyserConfig
from .problems.base import ProblemConfig
from .optimizers.base import OptimizerConfig
from .optimisers.base import OptimiserConfig
from .targets.base import TargetGroup
guts_prefix = 'grond'
@ -54,8 +54,8 @@ class Config(HasPaths):
AnalyserConfig.T(),
default=[TargetBalancingAnalyserConfig.D()],
help='List of problem analysers')
optimizer_config = OptimizerConfig.T(
help='The optimizers configuration')
optimiser_config = OptimiserConfig.T(
help='The optimisers configuration')
engine_config = EngineConfig.T(
default=EngineConfig.D(),
help=':class:`pyrocko.gf.LocalEngine` configuration')


+ 11
- 11
src/core.py View File

@ -16,9 +16,9 @@ from pyrocko import parimap, model, marker as pmarker
from .dataset import NotFound
from .problems.base import Problem, load_problem_info_and_data, \
load_problem_data, load_optimizer_info
load_problem_data, load_optimiser_info
from .optimizers.base import BadProblem
from .optimisers.base import BadProblem
from .targets.waveform.target import WaveformMisfitResult
from .meta import expand_template, GrondError
from .config import read_config
@ -195,7 +195,7 @@ def harvest(rundir, problem=None, nbest=10, force=False, weed=0):
else:
xs, misfits = load_problem_data(rundir, problem)
optimizer = load_optimizer_info(rundir)
optimiser = load_optimiser_info(rundir)
dumpdir = op.join(rundir, 'harvest')
if op.exists(dumpdir):
if force:
@ -213,8 +213,8 @@ def harvest(rundir, problem=None, nbest=10, force=False, weed=0):
ibests_list.append(isort[:nbest])
if weed != 3:
for ibootstrap in range(optimizer.nbootstrap):
bms = optimizer.bootstrap_misfits(problem, misfits, ibootstrap)
for ibootstrap in range(optimiser.nbootstrap):
bms = optimiser.bootstrap_misfits(problem, misfits, ibootstrap)
isort = num.argsort(bms)
ibests_list.append(isort[:nbest])
ibests.append(isort[0])
@ -511,17 +511,17 @@ def process_event(ievent, g_data_id):
xs_inject = synt.get_x()[num.newaxis, :]
try:
optimizer = config.optimizer_config.get_optimizer()
optimiser = config.optimiser_config.get_optimiser()
if xs_inject is not None:
from .optimizers import highscore
if not isinstance(optimizer, highscore.HighScoreOptimizer):
from .optimisers import highscore
if not isinstance(optimiser, highscore.HighScoreOptimiser):
raise GrondError(
'optimizer does not support injections')
'optimiser does not support injections')
optimizer.sampler_phases[0:0] = [
optimiser.sampler_phases[0:0] = [
highscore.InjectionSamplerPhase(xs_inject=xs_inject)]
optimizer.optimize(
optimiser.optimize(
problem,
rundir=rundir)


+ 9
- 9
src/environment.py View File

@ -2,7 +2,7 @@ import os.path as op
from grond.config import read_config
from grond import meta
from grond.problems.base import load_optimizer_info, load_problem_info, \
from grond.problems.base import load_optimiser_info, load_problem_info, \
ModelHistory
@ -42,7 +42,7 @@ class Environment(object):
def reset(self):
self._histories = {}
self._dataset = None
self._optimizer = None
self._optimiser = None
self._problem = None
def get_config(self):
@ -81,15 +81,15 @@ class Environment(object):
return self._rundir_path
def get_optimizer(self):
if self._optimizer is None:
def get_optimiser(self):
if self._optimiser is None:
try:
self._optimizer = load_optimizer_info(self.get_rundir_path())
self._optimiser = load_optimiser_info(self.get_rundir_path())
except NoRundirAvailable:
self._optimizer = \
self.get_config().optimizer_config.get_optimizer()
self._optimiser = \
self.get_config().optimiser_config.get_optimiser()
return self._optimizer
return self._optimiser
def get_problem(self):
if self._problem is None:
@ -137,7 +137,7 @@ class Environment(object):
pass
try:
plots.update(self.get_optimizer().get_plot_classes())
plots.update(self.get_optimiser().get_plot_classes())
except GrondEnvironmentError:
pass


+ 8
- 8
src/monitor.py View File

@ -57,11 +57,11 @@ class GrondMonitor(object):
self.history = ModelHistory.follow(self.rundir)
optimizer_fn = op.join(self.rundir, 'optimizer.yaml')
self.optimizer = guts.load(filename=optimizer_fn)
optimiser_fn = op.join(self.rundir, 'optimiser.yaml')
self.optimiser = guts.load(filename=optimiser_fn)
self.problem = self.history.problem
self.niter = self.optimizer.niterations
self.niter = self.optimiser.niterations
self.starttime = time.time()
self.last_update = self.starttime
@ -101,7 +101,7 @@ class GrondMonitor(object):
''' Connected and called through the self.history.add_listener '''
self.iiter = self.history.nmodels
problem = self.history.problem
optimizer_status = self.optimizer.get_status(self.history)
optimiser_status = self.optimiser.get_status(self.history)
lines = []
lnadd = lines.append
@ -117,15 +117,15 @@ class GrondMonitor(object):
.format(s=self, p=problem))
lnadd('Iteration {s.iiter} / {s.niter}'
.format(s=self))
lnadd(optimizer_status.extra_text)
lnadd(optimiser_status.extra_text)
col_param_width = max([len(p) for p in parameter_names]) + 2
out_ln = self.row_name +\
''.join([self.parameter_fmt] * optimizer_status.ncolumns)
''.join([self.parameter_fmt] * optimiser_status.ncolumns)
lnadd(out_ln.format(
*['Parameter'] + list(optimizer_status.column_names),
*['Parameter'] + list(optimiser_status.column_names),
col_param_width=col_param_width,
col_width=self.col_width,
type='s'))
@ -133,7 +133,7 @@ class GrondMonitor(object):
for ip, parameter_name in enumerate(parameter_names):
lnadd(out_ln.format(
parameter_name,
*[fmt(v[ip]) for v in optimizer_status.values],
*[fmt(v[ip]) for v in optimiser_status.values],
col_param_width=col_param_width,
col_width=self.col_width))


+ 2
- 0
src/optimisers/__init__.py View File

@ -0,0 +1,2 @@
from .base import * # noqa
from .highscore.optimiser import * # noqa

src/optimizers/base.py → src/optimisers/base.py View File

@ -5,14 +5,14 @@ from grond.meta import GrondError
guts_prefix = 'grond'
logger = logging.getLogger('grond.optimizers.base')
logger = logging.getLogger('grond.optimisers.base')
class BadProblem(GrondError):
pass
class Optimizer(Object):
class Optimiser(Object):
@classmethod
def get_plot_classes(cls):
@ -30,11 +30,11 @@ class Optimizer(Object):
pass
class OptimizerConfig(Object):
class OptimiserConfig(Object):
pass
class OptimizerStatus(object):
class OptimiserStatus(object):
__slots__ = ['columns', 'extra_text']
def __init__(self, columns, extra_text):
@ -56,6 +56,6 @@ class OptimizerStatus(object):
__all__ = '''
BadProblem
Optimizer
OptimizerConfig
Optimiser
OptimiserConfig
'''.split()

+ 1
- 0
src/optimisers/highscore/__init__.py View File

@ -0,0 +1 @@
from .optimiser import * # noqa

src/optimizers/highscore/optimizer.py → src/optimisers/highscore/optimiser.py View File

@ -12,12 +12,12 @@ from pyrocko.guts_array import Array
from grond.meta import GrondError, Forbidden
from grond.problems.base import ModelHistory
from grond.optimizers.base import Optimizer, OptimizerConfig, BadProblem, \
OptimizerStatus
from grond.optimisers.base import Optimiser, OptimiserConfig, BadProblem, \
OptimiserStatus
guts_prefix = 'grond'
logger = logging.getLogger('grond.optimizers.highscore.optimizer')
logger = logging.getLogger('grond.optimisers.highscore.optimiser')
def excentricity_compensated_probabilities(xs, sbx, factor):
@ -347,7 +347,7 @@ class Chains(object):
return num.cov(xs.T)
class HighScoreOptimizer(Optimizer):
class HighScoreOptimiser(Optimiser):
sampler_phases = List.T(SamplerPhase.T())
chain_length_factor = Float.T(default=8.)
@ -356,7 +356,7 @@ class HighScoreOptimizer(Optimizer):
bootstrap_seed = Int.T(default=23)
def __init__(self, **kwargs):
Optimizer.__init__(self, **kwargs)
Optimiser.__init__(self, **kwargs)
self._bootstrap_weights = {}
self._status_chains = None
@ -416,10 +416,10 @@ class HighScoreOptimizer(Optimizer):
self._tlog_last = t
def optimize(self, problem, rundir=None):
def optimise(self, problem, rundir=None):
if rundir is not None:
self.dump(filename=op.join(rundir, 'optimizer.yaml'))
self.dump(filename=op.join(rundir, 'optimiser.yaml'))
history = ModelHistory(problem, path=rundir, mode='w')
chains = self.chains(problem, history)
@ -489,22 +489,22 @@ class HighScoreOptimizer(Optimizer):
ichain=0, estimator='standard_deviation_single_chain')
glob_best = chains.best_model(ichain=0)
return OptimizerStatus(
return OptimiserStatus(
columns=OrderedDict(
zip(['BS mean', 'BS std',
'Glob mean', 'Glob std', 'Glob best'],
[bs_mean, bs_std, glob_mean, glob_std, glob_best])),
extra_text='Optimizer phase: %s' % phase.__class__.__name__)
extra_text='Optimiser phase: %s' % phase.__class__.__name__)
def get_movie_maker(
self, problem, history, xpar_name, ypar_name, movie_filename):
from . import plot
return plot.HighScoreOptimizerPlot(
return plot.HighScoreOptimiserPlot(
self, problem, history, xpar_name, ypar_name, movie_filename)
class HighScoreOptimizerConfig(OptimizerConfig):
class HighScoreOptimiserConfig(OptimiserConfig):
sampler_phases = List.T(
SamplerPhase.T(),
@ -513,14 +513,14 @@ class HighScoreOptimizerConfig(OptimizerConfig):
chain_length_factor = Float.T(default=8.)
nbootstrap = Int.T(default=10)
def get_optimizer(self):
return HighScoreOptimizer(
def get_optimiser(self):
return HighScoreOptimiser(
sampler_phases=list(self.sampler_phases),
chain_length_factor=self.chain_length_factor,
nbootstrap=self.nbootstrap)
def load_optimizer_history(dirname, problem):
def load_optimiser_history(dirname, problem):
fn = op.join(dirname, 'accepted')
with open(fn, 'r') as f:
nmodels = os.fstat(f.fileno()).st_size // (problem.nbootstrap+1)
@ -550,6 +550,6 @@ __all__ = '''
UniformSamplerPhase
DirectedSamplerPhase
Chains
HighScoreOptimizerConfig
HighScoreOptimizer
HighScoreOptimiserConfig
HighScoreOptimiser
'''.split()

src/optimizers/highscore/plot.py → src/optimisers/highscore/plot.py View File

@ -6,18 +6,18 @@ from matplotlib import pyplot as plt
from pyrocko.plot import mpl_init, mpl_margins
logger = logging.getLogger('grond.optimizer.highscore.plot')
logger = logging.getLogger('grond.optimiser.highscore.plot')
class HighScoreOptimizerPlot(object):
class HighScoreOptimiserPlot(object):
def __init__(
self, optimizer, problem, history, xpar_name, ypar_name,
self, optimiser, problem, history, xpar_name, ypar_name,
movie_filename):
self.optimizer = optimizer
self.optimiser = optimiser
self.problem = problem
self.chains = optimizer.chains(problem, history)
self.chains = optimiser.chains(problem, history)
self.history = history
self.xpar_name = xpar_name
self.ypar_name = ypar_name
@ -71,7 +71,7 @@ class HighScoreOptimizerPlot(object):
self.ixpar = ixpar
self.iypar = iypar
from matplotlib import colors
n = self.optimizer.nbootstrap + 1
n = self.optimiser.nbootstrap + 1
hsv = num.vstack((
num.random.uniform(0., 1., n),
num.random.uniform(0.5, 0.9, n),
@ -176,7 +176,7 @@ class HighScoreOptimizerPlot(object):
self._volatile.append(paths)
phase, iiter_phase = self.optimizer.get_sampler_phase(self.iiter)
phase, iiter_phase = self.optimiser.get_sampler_phase(self.iiter)
np = 1000
models_prob = num.zeros((np, self.problem.nparameters))
@ -259,4 +259,4 @@ class HighScoreOptimizerPlot(object):
__all__ = [
'HighScoreOptimizerPlot']
'HighScoreOptimiserPlot']

src/optimizers/plot.py → src/optimisers/plot.py View File

@ -350,11 +350,11 @@ class BootstrapPlot(PlotConfig):
def make(self, environ):
cm = environ.get_plot_collection_manager()
history = environ.get_history()
optimizer = environ.get_optimizer()
optimiser = environ.get_optimiser()
mpl_init(fontsize=self.font_size)
cm.create_group_mpl(self, self.draw_figures(history, optimizer))
cm.create_group_mpl(self, self.draw_figures(history, optimiser))
def draw_figures(self, history, optimizer):
def draw_figures(self, history, optimiser):
fig = plt.figure()
@ -368,8 +368,8 @@ class BootstrapPlot(PlotConfig):
gms_softclip = num.where(gms > 1.0, 0.1 * num.log10(gms) + 1.0, gms)
ibests = []
for ibootstrap in range(optimizer.nbootstrap):
bms = optimizer.bootstrap_misfits(
for ibootstrap in range(optimiser.nbootstrap):
bms = optimiser.bootstrap_misfits(
problem, history.misfits, ibootstrap)
isort_bms = num.argsort(bms)[::-1]

+ 0
- 2
src/optimizers/__init__.py View File

@ -1,2 +0,0 @@
from .base import * # noqa
from .highscore.optimizer import * # noqa

+ 0
- 1
src/optimizers/highscore/__init__.py View File

@ -1 +0,0 @@
from .optimizer import * # noqa

+ 2
- 2
src/plot/main.py View File

@ -53,10 +53,10 @@ def make_plots(plot_list, args, plots_path=None):
def make_movie(dirname, xpar_name, ypar_name, movie_filename):
env = Environment(dirname)
optimizer = env.get_optimizer()
optimiser = env.get_optimiser()
problem = env.get_problem()
history = env.get_history()
movie_maker = optimizer.get_movie_maker(
movie_maker = optimiser.get_movie_maker(
problem, history, xpar_name, ypar_name, movie_filename)
movie_maker.render()


+ 2
- 2
src/problems/base.py View File

@ -635,8 +635,8 @@ def load_problem_info_and_data(dirname, subset=None):
return problem, models, misfits
def load_optimizer_info(dirname):
fn = op.join(dirname, 'optimizer.yaml')
def load_optimiser_info(dirname):
fn = op.join(dirname, 'optimiser.yaml')
return guts.load(filename=fn)


+ 4
- 4
src/problems/plot.py View File

@ -47,12 +47,12 @@ class JointparPlot(PlotConfig):
def make(self, environ):
cm = environ.get_plot_collection_manager()
history = environ.get_history(subset='harvest')
optimizer = environ.get_optimizer()
optimiser = environ.get_optimiser()
mpl_init(fontsize=self.font_size)
cm.create_group_mpl(self, self.draw_figures(history, optimizer))
cm.create_group_mpl(self, self.draw_figures(history, optimiser))
def draw_figures(self, history, optimizer):
def draw_figures(self, history, optimiser):
color_parameter = self.color_parameter
exclude = self.exclude
@ -81,7 +81,7 @@ class JointparPlot(PlotConfig):
xref = problem.get_reference_model()
if ibootstrap is not None:
gms = optimizer.bootstrap_misfits(
gms = optimiser.bootstrap_misfits(
problem, history.misfits, ibootstrap)
else:
gms = problem.combine_misfits(history.misfits)


+ 2
- 2
src/scenario.py View File

@ -162,7 +162,7 @@ class GrondScenario(object):
gf_stores_from_pyrocko_config=False,
gf_store_superdirs=['gf_stores'])
optimizer_config = grond.HighScoreOptimizerConfig()
optimiser_config = grond.HighScoreOptimiserConfig()
config = grond.Config(
rundir_template=op.join('rundir', '${problem_name}.grun'),
@ -170,7 +170,7 @@ class GrondScenario(object):
target_groups=[obs.get_grond_target_group()
for obs in self.observations],
problem_config=self.problem.get_grond_problem_config(),
optimizer_config=optimizer_config,
optimiser_config=optimiser_config,
engine_config=engine_config)
return config


+ 3
- 3
src/targets/gnss_campaign/plot.py View File

@ -13,14 +13,14 @@ class GNSSTargetMisfitPlot(PlotConfig):
def make(self, environ):
cm = environ.get_plot_collection_manager()
history = environ.get_history()
optimizer = environ.get_optimizer()
optimiser = environ.get_optimiser()
ds = environ.get_dataset()
cm.create_group_pygmt(
self,
self.draw_gnss_fits(ds, history, optimizer))
self.draw_gnss_fits(ds, history, optimiser))
def plot_gnss_fits(self, ds, history, optimizer):
def plot_gnss_fits(self, ds, history, optimiser):
lat, lon = self.get_center_latlon()
radius = self.get_radius()


+ 3
- 3
src/targets/satellite/plot.py View File

@ -32,16 +32,16 @@ class SatelliteTargetPlot(PlotConfig):
def make(self, environ):
cm = environ.get_plot_collection_manager()
history = environ.get_history()
optimizer = environ.get_optimizer()
optimiser = environ.get_optimiser()
ds = environ.get_dataset()
environ.setup_modelling()
cm.create_group_mpl(
self,
self.draw_static_fits(ds, history, optimizer))
self.draw_static_fits(ds, history, optimiser))
def draw_static_fits(self, ds, history, optimizer):
def draw_static_fits(self, ds, history, optimiser):
from pyrocko.orthodrome import latlon_to_ne_numpy
problem = history.problem


+ 4
- 4
src/toy.py View File

@ -4,12 +4,12 @@ import numpy as num
from pyrocko.guts import Object, Float, Dict, List, String
from pyrocko import gf
from grond import Problem, Parameter, MisfitTarget
from grond.optimizers.highscore.plot import HighScoreOptimizerPlot
from grond.optimisers.highscore.plot import HighScoreOptimiserPlot
guts_prefix = 'grond.toy'
class ToyOptimizerPlot(HighScoreOptimizerPlot):
class ToyOptimiserPlot(HighScoreOptimiserPlot):
def set_source(self, source):
self._source = source
@ -25,7 +25,7 @@ class ToyOptimizerPlot(HighScoreOptimizerPlot):
self.axes.set_ylim(-10., 10.)
def start(self):
HighScoreOptimizerPlot.start(self)
HighScoreOptimiserPlot.start(self)
x = [getattr(t, self.xpar_name) for t in self._targets]
y = [getattr(t, self.ypar_name) for t in self._targets]
self.axes.plot(x, y, '^', color='black')
@ -35,7 +35,7 @@ class ToyOptimizerPlot(HighScoreOptimizerPlot):
zmin = num.min(zc)
if self.optimizer.nbootstrap < 5:
if self.optimiser.nbootstrap < 5:
alpha = 1.0
else:
alpha = 0.5


Loading…
Cancel
Save