helperFunctions.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import numpy
  2. import ConfigParser
  3. import os
  4. import sys
  5. import subprocess
  6. import time
  7. import sklearn.covariance
  8. def getSingleRMSEs(yTest, pred):
  9. rmses = numpy.asarray(numpy.empty([1,yTest.shape[1]]), dtype=numpy.float)
  10. rmses[:,:] = numpy.sqrt(numpy.mean(numpy.square(yTest - pred), axis=0))
  11. return rmses
  12. def getMeanRMSE(yTest, pred):
  13. return numpy.mean(getSingleRMSEs(yTest, pred))
  14. def normLP(x, norm, axis=1):
  15. if norm == 1:
  16. return numpy.sum(numpy.abs(x), axis=axis)
  17. elif norm == 2:
  18. return numpy.sqrt(numpy.sum(numpy.square(x), axis=axis))
  19. else:
  20. raise Exception('Yet unsupported norm %d!'%norm)
  21. def normalizeLP(x, norm):
  22. return numpy.divide(x, normLP(x, norm))
  23. def normalizeUCI(x):
  24. normX = x
  25. # TODO: vectorize
  26. for i in range(x.shape[0]):
  27. for j in range(x.shape[1]):
  28. normX[i,j] = (x[i,j] - numpy.min(x[:,j])) / (numpy.max(x[:,j]) - numpy.min(x[:,j]))
  29. return normX
  30. def estTimeLeft(t0, t1, curRun, totalRuns):
  31. timePast = float(t1 - t0)
  32. avgTimePerPass = timePast / float(curRun)
  33. estTimeOva = avgTimePerPass * totalRuns
  34. estTimeLeft = estTimeOva - timePast
  35. return [timePast , avgTimePerPass , estTimeLeft , estTimeOva]
  36. def getConfig(pathtoConfig, section, option, default=None, dtype='str', verbose=False):
  37. # set default
  38. value = default
  39. defaultUsed = True
  40. # check if file is available
  41. if pathtoConfig is not None and os.path.isfile(pathtoConfig):
  42. # init
  43. config = ConfigParser.ConfigParser()
  44. configFile = open(pathtoConfig)
  45. config.readfp(configFile)
  46. configFile.close()
  47. # check if section and option is available
  48. if config.has_section(section) and config.has_option(section, option):
  49. # get requested type
  50. if dtype == 'str':
  51. value = config.get(section, option)
  52. elif dtype == 'int':
  53. value = config.getint(section, option)
  54. elif dtype == 'float':
  55. value = config.getfloat(section, option)
  56. elif dtype == 'bool':
  57. value = config.getboolean(section, option)
  58. elif dtype == 'strList':
  59. value = config.get(section, option).split(',')
  60. elif dtype == 'intList':
  61. value = [int(entry) for entry in config.get(section, option).split(',')]
  62. elif dtype == 'floatList':
  63. value = [float(entry) for entry in config.get(section, option).split(',')]
  64. elif dtype == 'boolList':
  65. value = [bool(entry) for entry in config.get(section, option).split(',')]
  66. else:
  67. raise Exception('Unknown dtype!')
  68. defaultUsed = False
  69. # print config
  70. if verbose:
  71. aux = ''
  72. if 'List' in dtype and value is not None and len(value) > 0:
  73. aux = '| entryDtype:' + str(type(value[0]))
  74. print 'default:', defaultUsed, '| section:', section, '| option:', option, '| value:', value, '| dtype:', type(value), aux
  75. # return
  76. return value
  77. def getGitHash(gitPath=os.path.dirname(os.path.abspath(__file__))):
  78. curDir = os.getcwd()
  79. os.chdir(gitPath)
  80. gitHash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], stderr=subprocess.STDOUT).strip()
  81. os.chdir(curDir)
  82. return gitHash
  83. def showProgressBarTerminal(current, total, pre):
  84. sys.stdout.write('\r%s %0.2f %%'%(pre,(float(current)/float(total))*100.0))
  85. sys.stdout.flush()