smbinterp/interp/tools.py

85 lines
2.3 KiB
Python

import os
import logging
import logging.handlers
import inspect
import numpy as np
def get_logger(filename, level = logging.DEBUG, logger_name = 'pymoab', size = 2048, backupCount = 10):
"""
This is a simple wrapper around a few sane
defaults using Python's logging functionality.
An explaination of the optional parameters:
filename : the filename
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)
"""
logger = logging.getLogger(logger_name)
logger.setLevel(level)
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)
return logger
log = get_logger(filename = '/tmp/interp.log')
class smberror(Exception):
"""
this is a silly little exception subclass
"""
def __init__(self, val):
self.value = val
def __str__(self):
return repr(self.value)
def rms(errors):
"""
root mean square calculation
"""
r = 0.0
for i in errors:
r += np.power(i, 2)
r = np.sqrt(r / len(errors))
return r
def exact_func(X):
"""
the exact function used from baker's article (for testing)
"""
x = X[0]
y = X[0]
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
def exact_func_3D(X):
"""
the exact function (3D) used from baker's article (for testing)
"""
x = X[0]
y = X[1]
z = X[2]
return np.power((np.sin(x * np.pi / 2.0) * np.sin(y * np.pi / 2.0) * np.sin(z * np.pi / 2.0)), 2)
def improved_answer(answer, exact, verbose=False):
if not answer['error']:
return True
smblog.debug('error: %s' % answer['error'])
smblog.debug('qlin: %s' % answer['qlin'])
smblog.debug('final: %s' % answer['final'])
smblog.debug('exact: %s' % exact)
if np.abs(answer['final'] - exact) <= np.abs(answer['qlin'] - exact):
smblog.debug(":) improved result")
return True
else:
smblog.debug(":( damaged result")
return False