Commit c25df056 authored by marius's avatar marius
Browse files

wip

parent 70ee2234
...@@ -24,7 +24,7 @@ def d2u(d): ...@@ -24,7 +24,7 @@ def d2u(d):
subcommand_descriptions = { subcommand_descriptions = {
'init': 'print example configuration', 'init': 'create project structure or print example configuration',
'events': 'print available event names for given configuration', 'events': 'print available event names for given configuration',
'check': 'check data and configuration', 'check': 'check data and configuration',
'go': 'run Grond optimization', 'go': 'run Grond optimization',
...@@ -35,7 +35,7 @@ subcommand_descriptions = { ...@@ -35,7 +35,7 @@ subcommand_descriptions = {
} }
subcommand_usages = { subcommand_usages = {
'init': 'init [options]', 'init': 'init [options] <project_dir>',
'events': 'events <configfile>', 'events': 'events <configfile>',
'check': 'check <configfile> <eventnames> ... [options]', 'check': 'check <configfile> <eventnames> ... [options]',
'go': 'go <configfile> <eventnames> ... [options]', 'go': 'go <configfile> <eventnames> ... [options]',
...@@ -145,10 +145,20 @@ def command_init(args): ...@@ -145,10 +145,20 @@ def command_init(args):
parser, options, args = cl_parse('init', args, setup) parser, options, args = cl_parse('init', args, setup)
dir_struct = ['gf_store'] project_dir = None
if len(args) == 1:
project_dir = op.join(op.curdir, args[0])
if op.exists(project_dir):
raise EnvironmentError('Directory %s already exists' % args[0])
sub_dirs = ['gf_store']
empty_files = []
if options.waveform and not options.static: if options.waveform and not options.static:
dir_struct += ['data'] config_type = 'waveform'
sub_dirs += ['data']
empty_files += ['stations.xml']
dataset_config = grond.DatasetConfig( dataset_config = grond.DatasetConfig(
stations_path='stations.txt', stations_path='stations.txt',
events_path='events.txt', events_path='events.txt',
...@@ -188,7 +198,9 @@ def command_init(args): ...@@ -188,7 +198,9 @@ def command_init(args):
) )
elif options.static: elif options.static:
dir_struct += ['scenes'] config_type = 'static'
sub_dirs += ['scenes']
dataset_config = grond.DatasetConfig( dataset_config = grond.DatasetConfig(
events_path='events.txt', events_path='events.txt',
kite_scene_paths=['scenes'], kite_scene_paths=['scenes'],
...@@ -236,22 +248,26 @@ depth = 8000 ...@@ -236,22 +248,26 @@ depth = 8000
region = Myanmar region = Myanmar
--------------------------------------------''' --------------------------------------------'''
def create_struct(dirname): if project_dir is not None:
logger.info('Creating empty %s project in folder %s'
% (config_type, args[0]))
def p(fn): def p(fn):
op.abspath(op.join(dirname, fn)) return op.join(project_dir, fn)
os.mkdir(op.abspath(dirname)) os.mkdir(op.abspath(project_dir))
for d in sub_dirs:
os.mkdir(p(d))
with open(p('config.yml'), 'w') as cf: with open(p('config.yml'), 'w') as cf:
cf.write(str(config)) cf.write(str(config))
with open(p('events.txt'), 'w') as ef: with open(p('events.txt'), 'w') as ef:
ef.write(events) ef.write(events)
open('stations.txt', 'a').close()
for d in dir_struct:
os.mkdir(p(d))
print config for fn in empty_files:
open(p(fn), 'w').close()
else:
print config
def command_events(args): def command_events(args):
......
from .server import # noqa
\ No newline at end of file
import tornado.ioloop import tornado.ioloop
import grond import grond
import os
import os.path as op import os.path as op
import logging import logging
import numpy as num import numpy as num
...@@ -45,6 +46,67 @@ class BaraddurBokehHandler(BokehHandler): ...@@ -45,6 +46,67 @@ class BaraddurBokehHandler(BokehHandler):
self.config = config self.config = config
class GrondBokehModel(object):
def __init__(self, config):
self.config = config
self.set_rundir(self.config.rundir)
def set_rundir(self, rundir):
logger.debug('Loading problem from %s' % rundir)
self.rundir = rundir
self.problem = grond.core.load_problem_info(self.rundir)
self.parameters = self.problem.parameters
self.nparameters = self.problem.nparameters
self.ntargets = self.problem.ntargets
def get_models(self, skip_nmodels=0):
fn = op.join(self.rundir, 'models')
with open(fn, 'r') as f:
nmodels = os.fstat(f.fileno()).st_size / (self.nparameters * 8)
nmodels -= skip_nmodels
f.seek(skip_nmodels * self.nparameters * 8)
data = num.fromfile(
f, dtype='<f8', count=nmodels * self.nparameters)\
.astype(num.float)
nmodels = data.size/self.nparameters - skip_nmodels
models = data.reshape((nmodels, self.nparameters))
mods_dict = {}
for ip, par in enumerate(self.parameters):
mods_dict[par.name] = models[:, ip]
mods_dict['niter'] = num.arange(nmodels, dtype=num.int) + (nmodels+1)
return nmodels, mods_dict
def get_misfits(self, skip_nmodels=0):
fn = op.join(self.rundir, 'misfits')
with open(fn, 'r') as f:
nmodels = os.fstat(f.fileno()).st_size / (self.nparameters * 8)
nmodels -= skip_nmodels
f.seek(skip_nmodels * self.ntargets * 2 * 8)
data = num.fromfile(
f, dtype='<f8', count=nmodels*self.ntargets*2)\
.astype(num.float)
data = data.reshape((nmodels, self.ntargets*2))
combi = num.empty_like(data)
combi[:, 0::2] = data[:, :self.ntargets]
combi[:, 1::2] = data[:, self.ntargets:]
assert(data.size/self.nparameters - skip_nmodels == nmodels)
misfits = combi.reshape((nmodels, self.ntargets, 2))
mf_dict = {}
for it in xrange(self.ntargets):
mf_dict['target_%03d' % it] = misfits[:, it, 0]
mf_dict['target_mean'] = num.mean(misfits[:, :, 0])
mf_dict['niter'] = num.arange(nmodels, dtype=num.int) + (nmodels+1)
return nmodels, misfits
class Status(BaraddurRequestHandler): class Status(BaraddurRequestHandler):
class MisfitsPlot(BaraddurBokehHandler): class MisfitsPlot(BaraddurBokehHandler):
...@@ -52,8 +114,8 @@ class Status(BaraddurRequestHandler): ...@@ -52,8 +114,8 @@ class Status(BaraddurRequestHandler):
def modify_document(self, doc): def modify_document(self, doc):
self.nmodels = 0 self.nmodels = 0
self.source = ColumnDataSource( self.source = ColumnDataSource(
data={'n': [], data={'n': num.ndarray(0),
'gm': []}) 'gm': num.ndarray(0)})
self.update_misfits() self.update_misfits()
plot = figure(webgl=True, plot = figure(webgl=True,
...@@ -101,7 +163,7 @@ class Parameters(BaraddurRequestHandler): ...@@ -101,7 +163,7 @@ class Parameters(BaraddurRequestHandler):
self.source = ColumnDataSource() self.source = ColumnDataSource()
for p in ['n'] + [p.name for p in problem.parameters]: for p in ['n'] + [p.name for p in problem.parameters]:
self.source.add([], p) self.source.add(num.ndarray(0), p)
self.update_parameters() self.update_parameters()
plots = [] plots = []
...@@ -231,7 +293,7 @@ class BaraddurConfig(Object): ...@@ -231,7 +293,7 @@ class BaraddurConfig(Object):
String.T(), String.T(),
default=['*'], default=['*'],
optional=True, optional=True,
help='List of allowed hosts.') help='List of allowed hosts, default is all \'*\'.')
port = Int.T( port = Int.T(
default=8080, default=8080,
optional=True, optional=True,
......
...@@ -126,7 +126,7 @@ class Problem(Object): ...@@ -126,7 +126,7 @@ class Problem(Object):
guts.dump(self, filename=fn) guts.dump(self, filename=fn)
def dump_problem_data(self, dirname, x, ms, ns): def dump_problem_data(self, dirname, x, ms, ns):
fn = op.join(dirname, 'x') fn = op.join(dirname, 'models')
if not isinstance(x, num.ndarray): if not isinstance(x, num.ndarray):
x = num.array(x) x = num.array(x)
with open(fn, 'ab') as f: with open(fn, 'ab') as f:
......
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