middle of refactor

This commit is contained in:
Stephen McQuay 2010-10-23 13:49:15 -06:00
parent 431f3a4e44
commit eadfa46e57
5 changed files with 51 additions and 51 deletions

View File

@ -1,10 +1,9 @@
from baker import *
from baker.tools import smblog
import numpy as np
import sys import sys
import numpy as np
import itertools import itertools
from tools import smberror from interp.tools import log, smberror
def get_phis(X, R): def get_phis(X, R):
""" """
@ -32,7 +31,7 @@ def get_phis(X, R):
phi = np.linalg.solve(A,b) phi = np.linalg.solve(A,b)
except np.linalg.LinAlgError as e: except np.linalg.LinAlgError as e:
msg = "calculation of phis yielded a linearly dependant system (%s)" % e msg = "calculation of phis yielded a linearly dependant system (%s)" % e
smblog.error(msg) log.error(msg)
raise smberror(msg) raise smberror(msg)
phi = np.dot(np.linalg.pinv(A), b) phi = np.dot(np.linalg.pinv(A), b)
@ -70,7 +69,7 @@ def get_phis_3D(X, R):
try: try:
phi = np.linalg.solve(A,b) phi = np.linalg.solve(A,b)
except np.linalg.LinAlgError as e: except np.linalg.LinAlgError as e:
smblog.error("calculation of phis yielded a linearly dependant system: %s" % e) log.error("calculation of phis yielded a linearly dependant system: %s" % e)
phi = np.dot(np.linalg.pinv(A), b) phi = np.dot(np.linalg.pinv(A), b)
return phi return phi
@ -131,7 +130,7 @@ def get_error_quadratic(phi, R, S):
try: try:
(a, b, c) = np.linalg.solve(A,b) (a, b, c) = np.linalg.solve(A,b)
except np.linalg.LinAlgError as e: except np.linalg.LinAlgError as e:
smblog.error("linear calculation went bad, resorting to np.linalg.pinv: %s" % e) log.error("linear calculation went bad, resorting to np.linalg.pinv: %s" % e)
(a, b, c) = np.dot(np.linalg.pinv(A), b) (a, b, c) = np.dot(np.linalg.pinv(A), b)
error_term = a * phi[0] * phi[1]\ error_term = a * phi[0] * phi[1]\
@ -172,7 +171,7 @@ def get_error_cubic(phi, R, S):
try: try:
(a, b, c, d, e, f, g) = np.linalg.solve(A,b) (a, b, c, d, e, f, g) = np.linalg.solve(A,b)
except np.linalg.LinAlgError as e: except np.linalg.LinAlgError as e:
smblog.error("linear calculation went bad, resorting to np.linalg.pinv: %s" % e) log.error("linear calculation went bad, resorting to np.linalg.pinv: %s" % e)
(a, b, c, d, e, f, g) = np.dot(np.linalg.pinv(A), b) (a, b, c, d, e, f, g) = np.dot(np.linalg.pinv(A), b)
error_term = a * phi[0] * phi[1] * phi[1]\ error_term = a * phi[0] * phi[1] * phi[1]\
@ -186,12 +185,12 @@ def get_error_cubic(phi, R, S):
return error_term return error_term
def get_error_sauron(phi, R, S, order = 2): def get_error_sauron(phi, R, S, order = 2):
smblog.debug("len(phi): %d"% len(phi)) log.debug("len(phi): %d"% len(phi))
B = [] # baker eq 9 B = [] # baker eq 9
w = [] # baker eq 11 w = [] # baker eq 11
p = pattern(order, len(phi), offset = -1) p = pattern(order, len(phi), offset = -1)
smblog.debug("pattern: %s" % p) log.debug("pattern: %s" % p)
for (s,q) in zip(S.points, S.q): for (s,q) in zip(S.points, S.q):
cur_phi, cur_qlin = qlinear(s, R) cur_phi, cur_qlin = qlinear(s, R)
@ -205,8 +204,8 @@ def get_error_sauron(phi, R, S, order = 2):
B.append(l) B.append(l)
w.append(q - cur_qlin) w.append(q - cur_qlin)
smblog.debug("B: %s" % B) log.debug("B: %s" % B)
smblog.debug("w: %s" % w) log.debug("w: %s" % w)
B = np.array(B) B = np.array(B)
@ -219,10 +218,10 @@ def get_error_sauron(phi, R, S, order = 2):
try: try:
abc = np.linalg.solve(A,b) abc = np.linalg.solve(A,b)
except np.linalg.LinAlgError as e: except np.linalg.LinAlgError as e:
smblog.error("linear calculation went bad, resorting to np.linalg.pinv: %s" % e) log.error("linear calculation went bad, resorting to np.linalg.pinv: %s" % e)
abc = np.dot(np.linalg.pinv(A), b) abc = np.dot(np.linalg.pinv(A), b)
smblog.debug(len(abc) == len(p)) log.debug(len(abc) == len(p))
error_term = 0.0 error_term = 0.0
for (a, i) in zip(abc, p): for (a, i) in zip(abc, p):
@ -231,7 +230,7 @@ def get_error_sauron(phi, R, S, order = 2):
cur_sum *= phi[j] cur_sum *= phi[j]
error_term += cur_sum error_term += cur_sum
smblog.debug("error_term smb: %s" % error_term) log.debug("error_term smb: %s" % error_term)
return error_term, abc return error_term, abc
def run_baker(X, R, S, order=2): def run_baker(X, R, S, order=2):
@ -244,7 +243,7 @@ def run_baker(X, R, S, order=2):
R = Simplex R = Simplex
S = extra points S = extra points
""" """
smblog.debug("order = %d" % order) log.debug("order = %d" % order)
answer = { answer = {
'qlin': None, 'qlin': None,
@ -328,7 +327,7 @@ def run_baker_3D(X, R, S):
try: try:
(a, b, c, d, e, f) = np.linalg.solve(A,b) (a, b, c, d, e, f) = np.linalg.solve(A,b)
except np.linalg.LinAlgError as e: except np.linalg.LinAlgError as e:
smblog.error("linear calculation went bad, resorting to np.linalg.pinv: %s", e) log.error("linear calculation went bad, resorting to np.linalg.pinv: %s", e)
(a, b, c, d, e, f) = np.dot(np.linalg.pinv(A), b) (a, b, c, d, e, f) = np.dot(np.linalg.pinv(A), b)
error_term = a * phi[0] * phi[1]\ error_term = a * phi[0] * phi[1]\
@ -374,7 +373,7 @@ def _samples_ur(items, k, offset = 0):
yield tuple([x + offset for sel in selections for x in sel]) yield tuple([x + offset for sel in selections for x in sel])
def pattern(power, phicount, offset = 0): def pattern(power, phicount, offset = 0):
smblog.debug("(power = %s, phicount = %s)" % (power, phicount)) log.debug("(power = %s, phicount = %s)" % (power, phicount))
r = [] r = []
for i in _samples_ur(range(1, phicount + 1), power, offset): for i in _samples_ur(range(1, phicount + 1), power, offset):
if not len(set(i)) == 1: if not len(set(i)) == 1:

View File

@ -6,8 +6,8 @@ import inspect
import numpy as np import numpy as np
import scipy.spatial import scipy.spatial
from baker import run_baker from interp.baker import run_baker
from baker.tools import exact_func, smberror, smblog from interp.tools import exact_func, smberror, log
from simplex import face, contains from simplex import face, contains
from smcqdelaunay import * from smcqdelaunay import *

View File

@ -1,5 +1,5 @@
from baker import get_phis, get_phis_3D from interp.baker import get_phis, get_phis_3D
from baker.tools import smblog from interp.tools import log
TOL = 1e-8 TOL = 1e-8

View File

@ -1,36 +1,36 @@
import os import os
import logging import logging
import logging.handlers
import inspect import inspect
import numpy as np import numpy as np
class smbLog(object): def get_logger(filename, level = logging.DEBUG, logger_name = 'pymoab', size = 2048, backupCount = 10):
interpolator = "%s ==> %s" """
def __init__(self, level = logging.DEBUG): This is a simple wrapper around a few sane
logging.basicConfig( defaults using Python's logging functionality.
level = level,
format = '%(asctime)s %(levelname)s %(message)s',
filename = os.path.join(os.sep, 'tmp', 'baker.lol'),
)
self.log = logging.getLogger()
def debug(self, message = None):
msg = smbLog.interpolator % (inspect.stack()[1][3], message)
self.log.debug(msg)
def info(self, message = None): An explaination of the optional parameters:
msg = smbLog.interpolator % (inspect.stack()[1][3], message) filename : the filename
self.log.info(msg) level : one of either debug, info, warn, or error
logger_name : if one needs multiple logfiles, one must name them.
size : the size in bytes of the logfile before roll (defaults to 2MB)
backupCount : number of rolled logs to keep around (defaults to 10)
"""
def warn(self, message = None): logger = logging.getLogger(logger_name)
msg = smbLog.interpolator % (inspect.stack()[1][3], message) logger.setLevel(level)
self.log.warn(msg) my_format = logging.Formatter('%(asctime)s %(levelname)s (%(process)d) %(filename)s %(funcName)s:%(lineno)d %(message)s')
handler = logging.handlers.RotatingFileHandler(
filename, maxBytes = size * 1024, backupCount = backupCount)
handler.setFormatter(my_format)
logger.addHandler(handler)
def error(self, message = None): return logger
msg = smbLog.interpolator % (inspect.stack()[1][3], message)
self.log.error(msg)
log = get_logger(filename = '/tmp/interp.log')
smblog = smbLog(logging.DEBUG)
class smberror(Exception): class smberror(Exception):
""" """

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
import unittest import unittest
from interplosion import baker
from interplosion import grid from interp import baker
from interp import grid
import numpy as np import numpy as np
import scipy.spatial import scipy.spatial
@ -28,8 +29,8 @@ class TestSequenceFunctions(unittest.TestCase):
def testImports(self): def testImports(self):
import numpy import numpy
import scipy import scipy
import grid import interp.grid
import baker import interp.baker
def testGetPhis(self): def testGetPhis(self):