import numpy import ConfigParser import os import sys import subprocess import time import sklearn.covariance def getSingleRMSEs(yTest, pred): rmses = numpy.asarray(numpy.empty([1,yTest.shape[1]]), dtype=numpy.float) rmses[:,:] = numpy.sqrt(numpy.mean(numpy.square(yTest - pred), axis=0)) return rmses def getMeanRMSE(yTest, pred): return numpy.mean(getSingleRMSEs(yTest, pred)) def normLP(x, norm, axis=1): if norm == 1: return numpy.sum(numpy.abs(x), axis=axis) elif norm == 2: return numpy.sqrt(numpy.sum(numpy.square(x), axis=axis)) else: raise Exception('Yet unsupported norm %d!'%norm) def normalizeLP(x, norm): return numpy.divide(x, normLP(x, norm)) def normalizeUCI(x): normX = x # TODO: vectorize for i in range(x.shape[0]): for j in range(x.shape[1]): normX[i,j] = (x[i,j] - numpy.min(x[:,j])) / (numpy.max(x[:,j]) - numpy.min(x[:,j])) return normX def estTimeLeft(t0, t1, curRun, totalRuns): timePast = float(t1 - t0) avgTimePerPass = timePast / float(curRun) estTimeOva = avgTimePerPass * totalRuns estTimeLeft = estTimeOva - timePast return [timePast , avgTimePerPass , estTimeLeft , estTimeOva] def getConfig(pathtoConfig, section, option, default=None, dtype='str', verbose=False): # set default value = default defaultUsed = True # check if file is available if pathtoConfig is not None and os.path.isfile(pathtoConfig): # init config = ConfigParser.ConfigParser() configFile = open(pathtoConfig) config.readfp(configFile) configFile.close() # check if section and option is available if config.has_section(section) and config.has_option(section, option): # get requested type if dtype == 'str': value = config.get(section, option) elif dtype == 'int': value = config.getint(section, option) elif dtype == 'float': value = config.getfloat(section, option) elif dtype == 'bool': value = config.getboolean(section, option) elif dtype == 'strList': value = config.get(section, option).split(',') elif dtype == 'intList': value = [int(entry) for entry in config.get(section, option).split(',')] elif dtype == 'floatList': value = [float(entry) for entry in config.get(section, option).split(',')] elif dtype == 'boolList': value = [bool(entry) for entry in config.get(section, option).split(',')] else: raise Exception('Unknown dtype!') defaultUsed = False # print config if verbose: aux = '' if 'List' in dtype and value is not None and len(value) > 0: aux = '| entryDtype:' + str(type(value[0])) print 'default:', defaultUsed, '| section:', section, '| option:', option, '| value:', value, '| dtype:', type(value), aux # return return value def getGitHash(gitPath=os.path.dirname(os.path.abspath(__file__))): curDir = os.getcwd() os.chdir(gitPath) gitHash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], stderr=subprocess.STDOUT).strip() os.chdir(curDir) return gitHash def showProgressBarTerminal(current, total, pre): sys.stdout.write('\r%s %0.2f %%'%(pre,(float(current)/float(total))*100.0)) sys.stdout.flush()