replaced answer with a namedtuple
This commit is contained in:
+19
-24
@@ -1,8 +1,9 @@
|
||||
import numpy as np
|
||||
|
||||
from collections import namedtuple
|
||||
from functools import wraps
|
||||
import itertools
|
||||
|
||||
import numpy as np
|
||||
|
||||
import interp
|
||||
|
||||
AGGRESSIVE_ERROR_SOLVE = True
|
||||
@@ -10,6 +11,8 @@ RAISE_PATHOLOGICAL_EXCEPTION = False
|
||||
|
||||
__version__ = interp.__version__
|
||||
|
||||
Answer = namedtuple("Answer", ['qlin', 'final', 'error', 'abc'])
|
||||
|
||||
|
||||
def get_phis(X, R):
|
||||
"""
|
||||
@@ -124,7 +127,7 @@ def get_error(phi, R, R_q, S, S_q, order=2):
|
||||
return error_term, abc
|
||||
|
||||
|
||||
def run_baker(X, R, R_q, S, S_q, order=2):
|
||||
def interpolate(X, R, R_q, S=None, S_q=None, order=2):
|
||||
"""
|
||||
This is the main function to call to get an interpolation to X from the
|
||||
input meshes
|
||||
@@ -132,23 +135,22 @@ def run_baker(X, R, R_q, S, S_q, order=2):
|
||||
X -- the destination point
|
||||
|
||||
R = Simplex
|
||||
R_q = q values at R
|
||||
S = extra points
|
||||
S_q = q values at S
|
||||
|
||||
order - order of interpolation - 1
|
||||
"""
|
||||
|
||||
answer = {
|
||||
'qlin': None,
|
||||
'error': None,
|
||||
'final': None,
|
||||
}
|
||||
qlin=None
|
||||
error_term=None
|
||||
final=None
|
||||
abc={}
|
||||
|
||||
# calculate values only for the simplex triangle
|
||||
phi, qlin = qlinear(X, R, R_q)
|
||||
|
||||
if order == 1:
|
||||
answer['qlin'] = qlin
|
||||
answer['final'] = qlin
|
||||
return answer
|
||||
elif order in xrange(2, 11):
|
||||
if order in xrange(2, 11) and S:
|
||||
error_term, abc = get_error(phi, R, R_q, S, S_q, order)
|
||||
|
||||
# if a pathological vertex configuration was encountered and
|
||||
@@ -157,20 +159,13 @@ def run_baker(X, R, R_q, S, S_q, order=2):
|
||||
if (error_term is None) and (abc is None):
|
||||
if RAISE_PATHOLOGICAL_EXCEPTION:
|
||||
raise np.linalg.LinAlgError("Pathological Vertex Config")
|
||||
answer['qlin'] = qlin
|
||||
answer['final'] = qlin
|
||||
return answer
|
||||
else:
|
||||
else:
|
||||
final = qlin + error_term
|
||||
elif order not in xrange(2,11):
|
||||
raise Exception('unsupported order "%d" for baker method' % order)
|
||||
|
||||
q_final = qlin + error_term
|
||||
|
||||
answer['qlin'] = qlin
|
||||
answer['error'] = error_term
|
||||
answer['final'] = q_final
|
||||
answer['abc'] = abc
|
||||
|
||||
return answer
|
||||
return Answer(qlin=qlin, error=error_term, final=final, abc=abc)
|
||||
|
||||
|
||||
def memoize(f):
|
||||
|
||||
@@ -6,7 +6,7 @@ from xml.dom.minidom import Document
|
||||
import numpy as np
|
||||
from scipy.spatial import KDTree
|
||||
|
||||
from interp.baker import run_baker
|
||||
from interp.baker import interpolate
|
||||
from interp.baker import get_phis
|
||||
import interp
|
||||
|
||||
@@ -136,9 +136,9 @@ class grid(object):
|
||||
|
||||
return (r_mesh, s_mesh)
|
||||
|
||||
def run_baker(self, X, order=2, extra_points=3):
|
||||
def interpolate(self, X, order=2, extra_points=3):
|
||||
(R, S) = self.get_simplex_and_nearest_points(X, extra_points)
|
||||
answer = run_baker(X, R, S, order)
|
||||
answer = interpolate(X, R, S, order)
|
||||
return answer
|
||||
|
||||
def for_qhull_generator(self):
|
||||
|
||||
@@ -45,6 +45,10 @@ def baker_exact_3D(X):
|
||||
np.sin(z * np.pi / 2.0)), 2)
|
||||
return answer
|
||||
|
||||
def exact_me(X, f):
|
||||
a = np.array([f(i) for i in X])
|
||||
return a
|
||||
|
||||
|
||||
def friendly_exact_3D(X):
|
||||
x, y, z = X
|
||||
@@ -67,6 +71,9 @@ def improved_answer(answer, exact):
|
||||
else:
|
||||
return False
|
||||
|
||||
def identical_points(a,b):
|
||||
return all(set(j[i] for j in a) \
|
||||
== set(j[i] for j in b) for i in xrange(len(a[0])))
|
||||
|
||||
def improved(qlin, err, final, exact):
|
||||
if np.abs(final - exact) <= np.abs(qlin - exact):
|
||||
|
||||
Reference in New Issue
Block a user