Commit a99867ad authored by Sebastian Heimann's avatar Sebastian Heimann
Browse files

wip...

parent 579da771
...@@ -260,7 +260,7 @@ def analyse(problem, niter=1000, show_progress=False): ...@@ -260,7 +260,7 @@ def analyse(problem, niter=1000, show_progress=False):
wproblem = problem.copy() wproblem = problem.copy()
wproblem.targets = wtargets wproblem.targets = wtargets
xbounds = num.array(wproblem.bounds(), dtype=num.float) xbounds = num.array(wproblem.get_parameter_bounds(), dtype=num.float)
npar = xbounds.shape[0] npar = xbounds.shape[0]
mss = num.zeros((niter, wproblem.ntargets)) mss = num.zeros((niter, wproblem.ntargets))
...@@ -358,7 +358,7 @@ def solve(problem, ...@@ -358,7 +358,7 @@ def solve(problem,
compensate_excentricity=True, compensate_excentricity=True,
status=()): status=()):
xbounds = num.array(problem.bounds(), dtype=num.float) xbounds = num.array(problem.get_parameter_bounds(), dtype=num.float)
npar = xbounds.shape[0] npar = xbounds.shape[0]
nlinks_cap = int(round(chain_length_factor * npar + 1)) nlinks_cap = int(round(chain_length_factor * npar + 1))
...@@ -832,7 +832,7 @@ def check( ...@@ -832,7 +832,7 @@ def check(
check_problem(problem) check_problem(problem)
xbounds = num.array(problem.bounds(), dtype=num.float) xbounds = num.array(problem.get_parameter_bounds(), dtype=num.float)
results_list = [] results_list = []
......
...@@ -168,7 +168,7 @@ def draw_sequence_figures(model, plt, misfit_cutoff=None, sort_by='iteration'): ...@@ -168,7 +168,7 @@ def draw_sequence_figures(model, plt, misfit_cutoff=None, sort_by='iteration'):
problem = model.problem problem = model.problem
imodels = num.arange(model.nmodels) imodels = num.arange(model.nmodels)
bounds = problem.bounds() + problem.dependant_bounds() bounds = problem.get_parameter_bounds() + problem.get_dependant_bounds()
xref = problem.pack(problem.base_source) xref = problem.pack(problem.base_source)
...@@ -337,7 +337,7 @@ def draw_jointpar_figures( ...@@ -337,7 +337,7 @@ def draw_jointpar_figures(
xs = model.xs xs = model.xs
bounds = problem.bounds() + problem.dependant_bounds() bounds = problem.get_parameter_bounds() + problem.get_dependant_bounds()
for ipar in xrange(problem.ncombined): for ipar in xrange(problem.ncombined):
par = problem.combined[ipar] par = problem.combined[ipar]
lo, hi = bounds[ipar] lo, hi = bounds[ipar]
...@@ -347,7 +347,7 @@ def draw_jointpar_figures( ...@@ -347,7 +347,7 @@ def draw_jointpar_figures(
exclude.append(par.name) exclude.append(par.name)
xref = problem.pack(problem.base_source) xref = problem.get_xref()
if ibootstrap is not None: if ibootstrap is not None:
gms = problem.bootstrap_misfits(model.misfits, ibootstrap) gms = problem.bootstrap_misfits(model.misfits, ibootstrap)
......
...@@ -30,6 +30,7 @@ class Parameter(Object): ...@@ -30,6 +30,7 @@ class Parameter(Object):
kwargs['name'] = args[0] kwargs['name'] = args[0]
if len(args) >= 2: if len(args) >= 2:
kwargs['unit'] = args[1] kwargs['unit'] = args[1]
self.target = kwargs.pop('target', None) self.target = kwargs.pop('target', None)
Object.__init__(self, **kwargs) Object.__init__(self, **kwargs)
...@@ -85,9 +86,9 @@ class ProblemConfig(Object): ...@@ -85,9 +86,9 @@ class ProblemConfig(Object):
apply_balancing_weights = Bool.T(default=True) apply_balancing_weights = Bool.T(default=True)
class Problem(Object): class Problem(Object):
name = String.T() name = String.T()
parameters = List.T(Parameter.T())
ranges = Dict.T(String.T(), gf.Range.T()) ranges = Dict.T(String.T(), gf.Range.T())
dependants = List.T(Parameter.T()) dependants = List.T(Parameter.T())
apply_balancing_weights = Bool.T(default=True) apply_balancing_weights = Bool.T(default=True)
...@@ -115,14 +116,25 @@ class Problem(Object): ...@@ -115,14 +116,25 @@ class Problem(Object):
o._target_weights = None o._target_weights = None
return o return o
def parameter_dict(self, x, target=None): @property
params = [] def parameters(self):
for ip, p in enumerate(self.parameters): ps = self.problem_parameters
if p.target is target: for target in self.targets:
params.append((p.name, x[ip])) ps.extend(target.target_parameters)
return ADict(params)
return ps
def set_target_parameter_values(self, x):
i = len(self.problem_parameters)
for target in self.targets:
n = len(target.target_parameters)
target.set_parameter_values(x[i:i+n])
i += n
def get_parameter_dict(self, x):
return ADict((p.name, v) for for p, v in zip(self.parameters, x))
def parameter_array(self, d): def get_parameter_array(self, d):
return num.array([d[p.name] for p in self.parameters], dtype=num.float) return num.array([d[p.name] for p in self.parameters], dtype=num.float)
@property @property
...@@ -195,39 +207,6 @@ class Problem(Object): ...@@ -195,39 +207,6 @@ class Problem(Object):
def set_engine(self, engine): def set_engine(self, engine):
self._engine = engine self._engine = engine
def init_satellite_target_leveling(self):
for t in self.satellite_targets:
add = True
add_parameters = [
Parameter('%s:waterlevel' % t.scene_id.lower(), 'm',
label='Waterlevel (%s)' % t.scene_id,
target=t),
Parameter('%s:ramp_north' % t.scene_id.lower(), 'm/m',
label='Ramp North (%s)' % t.scene_id,
target=t),
Parameter('%s:ramp_east' % t.scene_id.lower(), 'm/m',
label='Ramp East (%s)' % t.scene_id,
target=t),
]
add_ranges = t.inner_misfit_config.leveling_ranges.copy()
for k in add_ranges.keys():
new_k = '%s:%s' % (t.scene_id.lower(), k)
if new_k in self.parameter_names:
add = False
add_ranges[new_k] = add_ranges.pop(k)
if add:
logger.info('Adding waterlevel parameters for %s' % t.scene_id)
self.parameters += add_parameters
self.ranges.update(add_ranges)
def set_satellite_scene_levels(self, x):
for target in self.satellite_targets:
levels = self.parameter_dict(x, target=target)
for k in levels.keys():
new_k = k.split(':')[-1]
levels[new_k] = levels.pop(k)
target.set_scene_levels(**levels)
def make_bootstrap_weights(self, nbootstrap): def make_bootstrap_weights(self, nbootstrap):
ntargets = len(self.targets) ntargets = len(self.targets)
ws = num.zeros((nbootstrap, ntargets)) ws = num.zeros((nbootstrap, ntargets))
...@@ -269,7 +248,6 @@ class Problem(Object): ...@@ -269,7 +248,6 @@ class Problem(Object):
return ws return ws
def inter_group_weights2(self, ns): def inter_group_weights2(self, ns):
''' Deprecated? '''
group, ngroups = self.get_group_mask() group, ngroups = self.get_group_mask()
ws = num.zeros(ns.shape) ws = num.zeros(ns.shape)
for igroup in xrange(ngroups): for igroup in xrange(ngroups):
...@@ -279,14 +257,25 @@ class Problem(Object): ...@@ -279,14 +257,25 @@ class Problem(Object):
return ws return ws
def bounds(self): def get_xref(self):
return self.pack(self.base_source)
def get_parameter_bounds(self):
out = [] out = []
for p in self.parameters: for p in self.problem_parameters:
r = self.ranges[p.name] r = self.ranges[p.name]
out.append((r.start, r.stop)) out.append((r.start, r.stop))
for target in self.targets:
for p in target.target_parameters:
r = target.ranges[p.name]
out.append((r.start, r.stop))
return out return out
def get_dependant_bounds(self):
return []
def bootstrap_misfit(self, ms, ns, ibootstrap=None): def bootstrap_misfit(self, ms, ns, ibootstrap=None):
w = self.get_bootstrap_weights(ibootstrap) * \ w = self.get_bootstrap_weights(ibootstrap) * \
self.get_target_weights() * self.inter_group_weights(ns) self.get_target_weights() * self.inter_group_weights(ns)
...@@ -376,7 +365,7 @@ class CMTProblem(Problem): ...@@ -376,7 +365,7 @@ class CMTProblem(Problem):
mt_type = StringChoice.T(default='full', choices=['full', 'deviatoric']) mt_type = StringChoice.T(default='full', choices=['full', 'deviatoric'])
def get_source(self, x): def get_source(self, x):
d = self.parameter_dict(x) d = self.get_parameter_dict(x)
rm6 = num.array([d.rmnn, d.rmee, d.rmdd, d.rmne, d.rmnd, d.rmed], rm6 = num.array([d.rmnn, d.rmee, d.rmdd, d.rmne, d.rmnd, d.rmed],
dtype=num.float) dtype=num.float)
...@@ -455,7 +444,7 @@ class CMTProblem(Problem): ...@@ -455,7 +444,7 @@ class CMTProblem(Problem):
def preconstrain(self, x): def preconstrain(self, x):
d = self.parameter_dict(x) d = self.get_parameter_dict(x)
m6 = num.array([d.rmnn, d.rmee, d.rmdd, d.rmne, d.rmnd, d.rmed], m6 = num.array([d.rmnn, d.rmee, d.rmdd, d.rmne, d.rmnd, d.rmed],
dtype=num.float) dtype=num.float)
...@@ -470,7 +459,7 @@ class CMTProblem(Problem): ...@@ -470,7 +459,7 @@ class CMTProblem(Problem):
m9 /= m0_unscaled m9 /= m0_unscaled
m6 = mtm.to6(m9) m6 = mtm.to6(m9)
d.rmnn, d.rmee, d.rmdd, d.rmne, d.rmnd, d.rmed = m6 d.rmnn, d.rmee, d.rmdd, d.rmne, d.rmnd, d.rmed = m6
x = self.parameter_array(d) x = self.get_parameter_array(d)
source = self.get_source(x) source = self.get_source(x)
if any(self.distance_min > source.distance_to(t) if any(self.distance_min > source.distance_to(t)
...@@ -479,7 +468,7 @@ class CMTProblem(Problem): ...@@ -479,7 +468,7 @@ class CMTProblem(Problem):
return x return x
def dependant_bounds(self): def get_dependant_bounds(self):
out = [ out = [
(-1., 1.), (-1., 1.),
(-1., 1.)] (-1., 1.)]
...@@ -584,7 +573,7 @@ class CMTProblemConfig(ProblemConfig): ...@@ -584,7 +573,7 @@ class CMTProblemConfig(ProblemConfig):
class DoubleDCProblem(Problem): class DoubleDCProblem(Problem):
parameters = [ problem_parameters = [
Parameter('time', 's', label='Time'), Parameter('time', 's', label='Time'),
Parameter('north_shift', 'm', label='Northing', **as_km), Parameter('north_shift', 'm', label='Northing', **as_km),
Parameter('east_shift', 'm', label='Easting', **as_km), Parameter('east_shift', 'm', label='Easting', **as_km),
...@@ -609,7 +598,7 @@ class DoubleDCProblem(Problem): ...@@ -609,7 +598,7 @@ class DoubleDCProblem(Problem):
nbootstrap = Int.T(default=100) nbootstrap = Int.T(default=100)
def get_source(self, x): def get_source(self, x):
d = self.parameter_dict(x) d = self.get_parameter_dict(x)
p = {} p = {}
for k in self.base_source.keys(): for k in self.base_source.keys():
if k in d: if k in d:
...@@ -676,10 +665,6 @@ class DoubleDCProblem(Problem): ...@@ -676,10 +665,6 @@ class DoubleDCProblem(Problem):
return num.array(x, dtype=num.float) return num.array(x, dtype=num.float)
def dependant_bounds(self):
out = []
return out
def evaluate(self, x, result_mode='sparse'): def evaluate(self, x, result_mode='sparse'):
source = self.get_source(x) source = self.get_source(x)
engine = self.get_engine() engine = self.get_engine()
...@@ -758,7 +743,7 @@ class DoubleDCProblemConfig(ProblemConfig): ...@@ -758,7 +743,7 @@ class DoubleDCProblemConfig(ProblemConfig):
class RectangularProblem(Problem): class RectangularProblem(Problem):
parameters = [ problem_parameters = [
Parameter('north_shift', 'm', label='Northing', **as_km), Parameter('north_shift', 'm', label='Northing', **as_km),
Parameter('east_shift', 'm', label='Easting', **as_km), Parameter('east_shift', 'm', label='Easting', **as_km),
Parameter('depth', 'm', label='Depth', **as_km), Parameter('depth', 'm', label='Depth', **as_km),
...@@ -775,10 +760,10 @@ class RectangularProblem(Problem): ...@@ -775,10 +760,10 @@ class RectangularProblem(Problem):
nbootstrap = 0 nbootstrap = 0
def pack(self, source): def pack(self, source):
return self.parameter_array(source) return self.get_parameter_array(source)
def get_source(self, x): def get_source(self, x):
source = self.base_source.clone(**self.parameter_dict(x)) source = self.base_source.clone(**self.get_parameter_dict(x))
return source return source
def extract(self, xs, i): def extract(self, xs, i):
...@@ -801,13 +786,6 @@ class RectangularProblem(Problem): ...@@ -801,13 +786,6 @@ class RectangularProblem(Problem):
# raise Forbidden() # raise Forbidden()
return x return x
def dependant_bounds(self):
print('dependent_bounds')
out = [
(-1., 1.),
(-1., 1.)]
return out
def evaluate(self, x, result_mode='sparse', mask=None, nprocs=0): def evaluate(self, x, result_mode='sparse', mask=None, nprocs=0):
source = self.get_source(x) source = self.get_source(x)
engine = self.get_engine() engine = self.get_engine()
......
...@@ -42,7 +42,7 @@ class SyntheticTest(Object): ...@@ -42,7 +42,7 @@ class SyntheticTest(Object):
problem = self.get_problem() problem = self.get_problem()
if self.x: if self.x:
x = problem.preconstrain( x = problem.preconstrain(
problem.parameter_array(self.x)) problem.get_parameter_array(self.x))
else: else:
x = problem.preconstrain( x = problem.preconstrain(
......
...@@ -468,12 +468,13 @@ def _process(tr, tmin, tmax, taper, domain): ...@@ -468,12 +468,13 @@ def _process(tr, tmin, tmax, taper, domain):
class InnerSatelliteMisfitConfig(Object): class InnerSatelliteMisfitConfig(Object):
use_weight_focal = Bool.T(default=False) use_weight_focal = Bool.T(default=False)
leveling_ranges = Dict.T(String.T(), gf.Range.T(), ranges = Dict.T(String.T(), gf.Range.T(),
default={'waterlevel': '-0.5 .. 0.5', default={'waterlevel': '-0.5 .. 0.5',
'ramp_north': '-1e-4 .. 1e-4', 'ramp_north': '-1e-4 .. 1e-4',
'ramp_east': '-1e-4 .. 1e-4'}) 'ramp_east': '-1e-4 .. 1e-4'})
class MisfitSatelliteTarget(gf.SatelliteTarget): class MisfitSatelliteTarget(gf.SatelliteTarget):
scene_id = String.T() scene_id = String.T()
super_group = gf.StringID.T() super_group = gf.StringID.T()
...@@ -481,6 +482,11 @@ class MisfitSatelliteTarget(gf.SatelliteTarget): ...@@ -481,6 +482,11 @@ class MisfitSatelliteTarget(gf.SatelliteTarget):
manual_weight = Float.T(default=1.0) manual_weight = Float.T(default=1.0)
group = gf.StringID.T() group = gf.StringID.T()
target_parameters = [
...]
ranges = Dict.T(String.T(), gf.Range.T())
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
gf.SatelliteTarget.__init__(self, *args, **kwargs) gf.SatelliteTarget.__init__(self, *args, **kwargs)
self.waterlevel = 0. self.waterlevel = 0.
......
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