Browse Source

simple depth overlap check; pep8

multisource
asteinbe 2 years ago
parent
commit
f38ea955ce
  1. 71
      src/optimisers/highscore/optimiser.py
  2. 39
      src/problems/base.py

71
src/optimisers/highscore/optimiser.py

@ -8,7 +8,7 @@ import numpy as num
from collections import OrderedDict
from pyrocko.guts import StringChoice, Int, Float, Object, List
from pyrocko.guts_array import Array
from shapely.geometry import Polygon
from shapely.geometry import Polygon, LineString
from grond.meta import GrondError, Forbidden, has_get_plot_classes
from grond.problems.base import ModelHistory
@ -161,15 +161,33 @@ class UniformSamplerPhase(SamplerPhase):
for i in range(nsources):
source = problem.get_source(pars, i)
sources.append(source)
src1 = sources[0].outline('xy')
src2 = sources[1].outline('xy')
p1 = Polygon(src1)
p2 = Polygon(src2)
if not p1.intersects(p2):
intersect = False
else:
depths_max = []
depths_min = []
polygons = []
for src in sources:
depths_max.append(num.max(src.outline()[:, 2]))
depths_min.append(num.min(src.outline()[:, 2]))
src_outline = sources[0].outline('xy')
polygons.append(Polygon(src_outline))
for k in range(len(polygons)):
for j in range(len(polygons)):
p1 = polygons[k]
p2 = polygons[j]
if not p1.intersects(p2) or p1 == p2:
intersect = False
else:
line_1 = [(1.0, depths_min[k]), (1.0, depths_max[k])]
line_2 = [(1.0, depths_min[j]), (1.0, depths_max[j])]
line1 = LineString(line_1)
line2 = LineString(line_2)
intersection_depth = line1.intersection(line2)
if intersection_depth is False:
intersect = False
else:
intersect = True
print('intersection, uniform phase redraw')
if any(sources.count(x) > 1 for x in sources):
intersect = True
print('intersection, uniform phase redraw')
return Sample(model=problem.random_uniform(xbounds, self.get_rstate()))
@ -331,18 +349,39 @@ class DirectedSamplerPhase(SamplerPhase):
pars = x
nsources = 2
sources = []
if nsources is not None:
for i in range(nsources):
source = problem.get_source(pars, i)
sources.append(source)
src1 = sources[0].outline('xy')
src2 = sources[1].outline('xy')
p1 = Polygon(src1)
p2 = Polygon(src2)
if not p1.intersects(p2):
intersect = False
else:
depths_max = []
depths_min = []
polygons = []
for src in sources:
depths_max.append(num.max(src.outline()[:,2]))
depths_min.append(num.min(src.outline()[:,2]))
src_outline = sources[0].outline('xy')
polygons.append(Polygon(src_outline))
for k in range(len(polygons)):
for j in range(len(polygons)):
p1 = polygons[k]
p2 = polygons[j]
if not p1.intersects(p2) or p1 == p2:
intersect = False
else:
line_1 = [(1.0, depths_min[k]), (1.0, depths_max[k])]
line_2 = [(1.0, depths_min[j]), (1.0, depths_max[j])]
line1 = LineString(line_1)
line2 = LineString(line_2)
intersection_depth = line1.intersection(line2)
if intersection_depth is False:
intersect = False
else:
intersect = True
print('intersection, uniform phase redraw')
if any(sources.count(x) > 1 for x in sources):
intersect = True
return Sample(
model=x,
ichain_base=ichain_choice,

39
src/problems/base.py

@ -29,6 +29,7 @@ guts_prefix = 'grond'
logger = logging.getLogger('grond.problems.base')
km = 1e3
as_km = dict(scale_factor=km, scale_unit='km')
g_rstate = num.random.RandomState()
def range_overlap(a_min, a_max, b_min, b_max):
@ -66,8 +67,9 @@ class CombiSource(gf.Source):
assert num.all(lats == lats[0]) and num.all(lons == lons[0])
lat, lon = lats[0], lons[0]
t = float(num.min([p.time for p in subsources]))
kwargs.update(time=t, lat=float(lat), lon=float(lon))
depth = float(num.mean([p.depth for p in subsources]))
t = float(list([p.time for p in subsources])[0])
kwargs.update(time=t, lat=float(lat), lon=float(lon), depth=depth)
gf.Source.__init__(self, subsources=subsources, **kwargs)
@ -78,14 +80,13 @@ class CombiSource(gf.Source):
dsources = []
t0 = self.subsources[0].time
t1 = self.subsources[1].time
tdiff = t0-t1
for sf in self.subsources:
ds = sf.discretize_basesource(store, target)
ds.m6s *= sf.get_factor()
t1 = sf.time
if t0 < t1:
tdiff = t0-t1
ds.times = ds.times - tdiff
dsources.append(ds)
dsources[1].times = dsources[1].times - tdiff
return gf.DiscretizedMTSource.combine(dsources)
@ -159,9 +160,6 @@ class Problem(Object):
paths.add(grp.path)
logger.debug('TargetGroup check OK.')
def get_engine(self):
return self._engine
def copy(self):
o = copy.copy(self)
o._target_weights = None
@ -312,6 +310,14 @@ class Problem(Object):
def set_engine(self, engine):
self._engine = engine
def get_engine(self):
return self._engine
def get_gf_store(self, target):
if self.get_engine() is None:
raise GrondError('Cannot get GF Store, modelling is not set up!')
return self.get_engine().get_store(target.store_id)
def random_uniform(self, xbounds, rstate):
x = rstate.uniform(0., 1., self.nparameters)
x *= (xbounds[:, 1] - xbounds[:, 0])
@ -531,9 +537,9 @@ class Problem(Object):
def evaluate(self, x, mask=None, result_mode='full',
targets=None, nsources=None):
patches = []
nsources = 2 # for testing
nsources = 2
outlines = []
if nsources == 2: #if self.nsources:
if nsources == 2:
for i in range(nsources):
source = self.get_source(x, i)
patches.append(source)
@ -570,8 +576,9 @@ class Problem(Object):
modelling_targets_unique = list(u2m_map.keys())
resp = engine.process(source, modelling_targets_unique,
resp = engine.process(sources, modelling_targets_unique,
nthreads=self.nthreads)
modelling_results_unique = list(resp.results_list[0])
modelling_results = [None] * len(modelling_targets)
@ -634,10 +641,10 @@ class Problem(Object):
return results
def get_random_model(self):
def get_random_model(self, ntries_limit=100):
xbounds = self.get_parameter_bounds()
while True:
for _ in range(ntries_limit):
x = self.random_uniform(xbounds, rstate=g_rstate)
try:
return self.preconstrain(x)
@ -645,6 +652,10 @@ class Problem(Object):
except Forbidden:
pass
raise GrondError(
'Could not find any suitable candidate sample within %i tries' % (
ntries_limit))
class ProblemInfoNotAvailable(GrondError):
pass

Loading…
Cancel
Save