import os import logging import logging.handlers import inspect import numpy as np def get_logger(filename, level = logging.DEBUG, logger_name = 'interp', 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', level = logging.INFO, size = 10240) 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 ,y = X answer = 1.0 + x*x + y*y # np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2) log.debug(answer) return answer 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] answer = np.power((np.sin(x * np.pi / 2.0) * np.sin(y * np.pi / 2.0) * np.sin(z * np.pi / 2.0)), 2) log.debug(answer) return answer def exact(X): x,y,z = X return 1 + x*x + y*y + z*z def improved_answer(answer, exact, verbose=False): if not answer['error']: return True log.debug('qlin: %s' % answer['qlin']) log.debug('error: %s' % answer['error']) log.debug('final: %s' % answer['final']) log.debug('exact: %s' % exact) if np.abs(answer['final'] - exact) <= np.abs(answer['qlin'] - exact): log.debug(":) improved result") return True else: log.debug(":( damaged result") return False