Commit 8d6a19e2 authored by Marius Isken's avatar Marius Isken
Browse files

grond py3 clean

parent 60746551
......@@ -18,7 +18,7 @@ km = 1e3
def d2u(d):
if isinstance(d, dict):
return dict((k.replace('-', '_'), v) for (k, v) in d.iteritems())
return dict((k.replace('-', '_'), v) for (k, v) in d.items())
else:
return d.replace('-', '_')
......@@ -102,7 +102,7 @@ def cl_parse(command, args, setup=None, details=None):
usage = subcommand_usages[command]
descr = subcommand_descriptions[command]
if isinstance(usage, basestring):
if isinstance(usage, str):
usage = [usage]
susage = '%s %s' % (program_name, usage[0])
......@@ -284,7 +284,7 @@ region = Myanmar
for fn in empty_files:
open(p(fn), 'w').close()
else:
print config
print(config)
def command_events(args):
......@@ -298,9 +298,9 @@ def command_events(args):
config_path = args[0]
config = grond.read_config(config_path)
print 'Available Events:'
print('Available Events:')
for event_name in grond.get_event_names(config):
print '* %s' % event_name
print('* %s' % event_name)
def command_check(args):
......@@ -370,10 +370,10 @@ def command_go(args):
config_path = args[0]
config = grond.read_config(config_path)
print 'Available Events:'
print('Available Events:')
for event_name in grond.get_event_names(config):
print '* %s' % event_name
print '\n'
print('* %s' % event_name)
print('\n')
help_and_die(parser, 'missing arguments')
......@@ -516,7 +516,7 @@ selected by specifying a comma-separated list.''' % (
dirname, plotnames,
save=options.save, formats=formats, dpi=options.dpi)
except grond.GrondError, e:
except grond.GrondError as e:
die(str(e))
......@@ -595,7 +595,7 @@ def command_export(args):
type=options.type,
pnames=pnames)
except grond.GrondError, e:
except grond.GrondError as e:
die(str(e))
......
[metadata]
description-file=README.md
license-file=LICENSE
[bdist_wheel]
universal=1
[build_ext]
inplace=1
[nosetests]
verbosity=2
detailed-errors=1
with-coverage=1
cover-erase=1
cover-package=kite
[coverage:report]
exclude_lines =
def __repr__
def __str__
raise AssertionError
raise NotImplementedError
raise ValueError
raise
if __name__ == .__main__.:
logger.error
import copy
import numpy as num
from pyrocko.guts import Object, Int
......@@ -40,10 +39,10 @@ class Analyser(object):
notifier.emit('progress_start', 'analysing problem', self.niter)
isbad_mask = None
for iiter in xrange(self.niter):
for iiter in range(self.niter):
while True:
x = []
for ipar in xrange(npar):
for ipar in range(npar):
v = rstate.uniform(xbounds[ipar, 0], xbounds[ipar, 1])
x.append(v)
......@@ -70,7 +69,7 @@ class Analyser(object):
weights = 1. / mean_ms
groups, ngroups = wproblem.get_group_mask()
for igroup in xrange(ngroups):
for igroup in range(ngroups):
weights[groups == igroup] /= (
num.nansum(weights[groups == igroup]) /
num.nansum(num.isfinite(weights[groups == igroup])))
......
......@@ -7,7 +7,7 @@ class ColorCycler(list):
list.__init__(self, *args, **kwargs)
self.index = -1
def next(self):
def __next__(self):
self.index += 1
if self.index >= len(self):
self.index = 0
......@@ -20,4 +20,4 @@ def makeColorGradient(misfits, fr=1., fg=.5, fb=1.,
r = num.sin(fr * misfits + pr) * 127 + 128
g = num.sin(fg * misfits + pg) * 127 + 128
b = num.sin(fb * misfits + pb) * 127 + 128
return ['#%02x%02x%02x' % (r[i], g[i], b[i]) for i in xrange(misfits.size)]
return ['#%02x%02x%02x' % (r[i], g[i], b[i]) for i in range(misfits.size)]
......@@ -164,10 +164,10 @@ class SmoothColumnDataSource(ColumnDataSource):
def stream(self, data, **kwargs):
sigma = self._gaussian_kw['sigma']
if data.keys() == self.data.keys():
if list(data.keys()) == list(self.data.keys()):
raise AttributeError('streaming data must represent'
' existing column')
for key, arr in data.iteritems():
for key, arr in data.items():
if arr.ndim > 1 or not isinstance(arr, num.ndarray):
raise AttributeError('data is not numpy.ndarray of 1d')
......@@ -508,17 +508,17 @@ class Baraddur(BokehServer):
if handler_docs is None:
continue
for url, bokeh_handler in handler_docs.iteritems():
for url, bokeh_handler in handler_docs.items():
bokeh_apps['/plots/%s' % url] = Application(bokeh_handler(
self.config))
return bokeh_apps
def get_tornado_handlers(self):
return [(r'/', pages.values()[0],
return [(r'/', list(pages.values())[0],
{'config': self.config})] +\
[(r'/%s' % title, handler,
{'config': self.config})
for title, handler in pages.iteritems()] +\
for title, handler in pages.items()] +\
[(r'/res/(.*)', StaticFileHandler,
{'path': op.join(op.dirname(__file__), 'res')})]
......
......@@ -176,7 +176,7 @@ def load_problem_data(dirname, problem, skip_models=0):
count=nmodels * problem.nparameters)\
.astype(num.float)
nmodels = data1.size/problem.nparameters - skip_models
nmodels = data1.size//problem.nparameters - skip_models
xs = data1.reshape((nmodels, problem.nparameters))
fn = op.join(dirname, 'misfits')
......
......@@ -38,7 +38,7 @@ class CursesListener(object):
parameter_names = ['Parameters'] + state.parameters
col = 0
for icol in xrange(len(state.values) + 1):
for icol in range(len(state.values) + 1):
row = 0
if icol == 0:
......
......@@ -41,40 +41,39 @@ class TerminalListener(Listener):
lines = []
self.current_state = state
def l(t):
lines.append(t)
ladd = lines.append
def fmt(s):
return util.gform(s, significant_digits=(self.col_width-1-6)/2)
return util.gform(s, significant_digits=(self.col_width-1-6)//2)
out_ln = self.row_name +\
''.join([self.parameter_fmt] * len(state.parameter_sets))
col_param_width = max([len(p) for p in state.parameter_names]) + 2
l('Problem name: {s.problem_name}'
'\t({s.runtime:s} - remaining {s.runtime_remaining}'
' @ {s.iter_per_second:.1f} iter/s)'
.format(s=state))
l('Iteration {s.iiter} / {s.niter}'
.format(s=state))
ladd('Problem name: {s.problem_name}'
'\t({s.runtime.seconds} - remaining {s.runtime_remaining}'
' @ {s.iter_per_second:.1f} iter/s)'
.format(s=state))
ladd('Iteration {s.iiter} / {s.niter}'
.format(s=state))
l(out_ln.format(
*['Parameter'] + state.parameter_sets.keys(),
ladd(out_ln.format(
*['Parameter'] + list(state.parameter_sets.keys()),
col_param_width=col_param_width,
col_width=self.col_width,
type='s'))
for ip, parameter_name in enumerate(state.parameter_names):
l(out_ln.format(
parameter_name,
*[fmt(v[ip]) for v in state.parameter_sets.values()],
col_param_width=col_param_width,
col_width=self.col_width))
ladd(out_ln.format(
parameter_name,
*[fmt(v[ip]) for v in state.parameter_sets.values()],
col_param_width=col_param_width,
col_width=self.col_width))
l(state.extra_text.format(
ladd(state.extra_text.format(
col_param_width=col_param_width,
col_width=self.col_width,))
lines[0:0] = ['\033[2J']
l('')
print '\n'.join(lines)
ladd('')
print('\n'.join(lines))
......@@ -188,7 +188,7 @@ class Problem(Object):
ntargets = self.ntargets
ws = num.zeros((nbootstrap, ntargets))
rstate = num.random.RandomState(23)
for ibootstrap in xrange(nbootstrap):
for ibootstrap in range(nbootstrap):
if type == 'classic':
ii = rstate.randint(0, ntargets, size=self.ntargets)
ws[ibootstrap, :] = num.histogram(
......@@ -230,7 +230,7 @@ class Problem(Object):
group, ngroups = self.get_group_mask()
ws = num.zeros(self.ntargets)
for igroup in xrange(ngroups):
for igroup in range(ngroups):
mask = group == igroup
ws[mask] = 1.0 / root(num.nansum(exp(ns[mask])))
......@@ -241,7 +241,7 @@ class Problem(Object):
group, ngroups = self.get_group_mask()
ws = num.zeros(ns.shape)
for igroup in xrange(ngroups):
for igroup in range(ngroups):
mask = group == igroup
ws[:, mask] = (1.0 / root(
num.nansum(exp(ns[:, mask]), axis=1)))[:, num.newaxis]
......
......@@ -123,7 +123,7 @@ class DoubleDCProblem(Problem):
def random_uniform(self, xbounds):
x = num.zeros(self.nparameters)
for i in xrange(self.nparameters):
for i in range(self.nparameters):
x[i] = num.random.uniform(xbounds[i, 0], xbounds[i, 1])
return x.tolist()
......
......@@ -92,7 +92,7 @@ class RectangularProblem(Problem):
def random_uniform(self, xbounds):
x = num.zeros(self.nparameters)
for i in xrange(self.nparameters):
for i in range(self.nparameters):
x[i] = num.random.uniform(xbounds[i, 0], xbounds[i, 1])
return x.tolist()
......
......@@ -11,15 +11,13 @@ guts_prefix = 'grond'
logger = logging.getLogger('grond.solver')
class SimpleTimedelta(timedelta):
def __str__(self):
return timedelta.__str__(self).split('.')[0]
class RingBuffer(num.ndarray):
def __new__(cls, *args, **kwargs):
cls = num.ndarray.__new__(cls, *args, **kwargs)
cls.fill(0.)
return cls
def __init__(self, *args, **kwargs):
num.ndarray.__init__(self, *args, **kwargs)
self.fill(0.)
self.pos = 0
def put(self, value):
......@@ -58,14 +56,14 @@ class SolverState(object):
@property
def runtime(self):
return SimpleTimedelta(seconds=time.time() - self.starttime)
return timedelta(seconds=time.time() - self.starttime)
@property
def runtime_remaining(self):
if self.iter_per_second == 0.:
return SimpleTimedelta()
return SimpleTimedelta(seconds=(self.niter - self.iiter)
/ self.iter_per_second)
return timedelta()
return timedelta(seconds=(self.niter - self.iiter)
/ self.iter_per_second)
@property
def nparameters(self):
......
......@@ -217,8 +217,8 @@ def solve(problem,
if sampler_distribution == 'normal':
ncandidates = 1
xcandidates = num.zeros((ncandidates, npar))
for icandidate in xrange(ncandidates):
for ipar in xrange(npar):
for icandidate in range(ncandidates):
for ipar in range(npar):
ntry = 0
while True:
if local_sxs[ibootstrap_choice][ipar] > 0.:
......@@ -300,7 +300,7 @@ def solve(problem,
nlinks += 1
for ichain in xrange(chains_m.shape[0]):
for ichain in range(chains_m.shape[0]):
isort = num.argsort(chains_m[ichain, :nlinks])
chains_m[ichain, :nlinks] = chains_m[ichain, isort]
chains_i[ichain, :nlinks] = chains_i[ichain, isort]
......@@ -342,7 +342,7 @@ def solve(problem,
mxs = []
local_sxs = []
for i in xrange(1 + nbootstrap):
for i in range(1 + nbootstrap):
xs = xhist[chains_i[i, :nlinks], :]
mx = num.mean(xs, axis=0)
cov = num.cov(xs.T)
......@@ -386,7 +386,7 @@ def solve(problem,
matrix = (chains_i[:, :30] % 94 + 32).T
for row in matrix[::-1]:
lines.append(''.join(chr(xxx) for xxx in row))
print '\n'.join(lines)
print('\n'.join(lines))
if plot and plot.want_to_update(iiter):
plot.update(
......
......@@ -365,7 +365,7 @@ class WaveformMisfitTarget(gf.Target, MisfitTarget):
return mr
except NotFound, e:
except NotFound as e:
logger.debug(str(e))
raise gf.SeismosizerError('no waveform data, %s' % str(e))
......@@ -419,7 +419,7 @@ tautoshift**2 / tautoshift_max**2``
m, n = trace.Lx_norm(a, b, norm=exponent)
else:
mns = []
for ishift in xrange(-nshift_max, nshift_max+1):
for ishift in range(-nshift_max, nshift_max+1):
if ishift < 0:
a_cut = a[-ishift:]
b_cut = b[:ishift]
......
import unittest
from pyrocko import util
from grond import HasPaths, Path
class PathTestCase(unittest.TestCase):
def test_pathstuff(self):
class B(HasPaths):
p1 = Path.T()
p2 = Path.T()
class A(HasPaths):
p1 = Path.T()
p2 = Path.T()
b = B.T()
for path_prefix_a in (
None, 'relative_prefix_a', '/absolute_prefix_a'):
for path_prefix_b in (
None, 'relative_prefix_b', '/absolute_prefix_b'):
a = A(
path_prefix=path_prefix_a,
p1='abc/x.txt',
p2='/absolute/x.txt',
b=B(
path_prefix=path_prefix_b,
p1='abc/y.txt',
p2='/absolute/y.txt'))
a.set_basepath('rundir')
t1 = a.expand_path(a.p1)
t2 = a.expand_path(a.p2)
t3 = a.b.expand_path(a.b.p1)
t4 = a.b.expand_path(a.b.p2)
a.change_basepath('resultdir')
assert t1 == a.expand_path(a.p1)
assert t2 == a.expand_path(a.p2)
assert t3 == a.b.expand_path(a.b.p1)
assert t4 == a.b.expand_path(a.b.p2)
if __name__ == '__main__':
util.setup_logging('test_path', 'warning')
unittest.main()
Supports Markdown
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