changeset 670:f72ed51c6b65

corrected other missing imports
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 26 May 2015 11:39:36 +0200
parents df6be882f325
children 849f5f8bf4b9
files python/utils.py
diffstat 1 files changed, 11 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/python/utils.py	Tue May 26 11:35:24 2015 +0200
+++ b/python/utils.py	Tue May 26 11:39:36 2015 +0200
@@ -3,7 +3,7 @@
 
 import matplotlib.pyplot as plt
 from datetime import time, datetime
-from math import sqrt
+from math import sqrt, ceil, floor
 from scipy.stats import kruskal, shapiro
 
 datetimeFormat = "%Y-%m-%d %H:%M:%S"
@@ -37,7 +37,6 @@
     ex: norm.interval(0.95, loc = 0., scale = 2.3/sqrt(11))
     t.interval(0.95, 10, loc=1.2, scale = 2.3/sqrt(nSamples))
     loc is mean, scale is sigma/sqrt(n) (for Student, 10 is df)'''
-    from math import sqrt
     from scipy.stats.distributions import norm, t
     if trueStd:
         k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200.
@@ -211,7 +210,6 @@
 def framesToTime(nFrames, frameRate, initialTime = time()):
     '''returns a datetime.time for the time in hour, minutes and seconds
     initialTime is a datetime.time'''
-    from math import floor
     seconds = int(floor(float(nFrames)/float(frameRate))+initialTime.hour*3600+initialTime.minute*60+initialTime.second)
     h = int(floor(seconds/3600.))
     seconds = seconds - h*3600
@@ -233,8 +231,7 @@
 def ceilDecimals(v, nDecimals):
     '''Rounds the number at the nth decimal
     eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3'''
-    from math import ceil,pow
-    tens = pow(10,nDecimals)
+    tens = 10**nDecimals
     return ceil(v*tens)/tens
 
 def inBetween(bound1, bound2, x):
@@ -423,6 +420,8 @@
 
 def generateExperiments(independentVariables):
     '''Generates all possible models for including or not each independent variable'''
+    from numpy import nan
+    from pandas import DataFrame
     experiments = {}
     nIndependentVariables = len(independentVariables)
     if nIndependentVariables != len(set(independentVariables)):
@@ -433,9 +432,9 @@
     for i,var in enumerate(independentVariables):
         pattern = [False]*(2**i)+[True]*(2**i)
         experiments[var] = pattern*(2**(nIndependentVariables-i-1))
-    experiments = pd.DataFrame(experiments)
+    experiments = DataFrame(experiments)
     experiments['r2adj'] = 0.
-    experiments['condNum'] = np.nan
+    experiments['condNum'] = nan
     experiments['shapiroP'] = -1
     experiments['nobs'] = -1
     return experiments
@@ -444,6 +443,7 @@
     '''Generates all possible model with the independentVariables
     and runs them, saving the results in experiments
     with multiprocess option'''
+    from pandas import concat
     experiments = generateExperiments(independentVariables)
     nModels = len(experiments)
     print("Running {} models with {} processes".format(nModels, nProcesses))
@@ -451,9 +451,9 @@
         return runModels(experiments, data, dependentVariable, independentVariables, regressionType)
     else:
         pool = Pool(processes = nProcesses)
-        chunkSize = int(np.ceil(nModels/nProcesses))
+        chunkSize = int(ceil(nModels/nProcesses))
         jobs = [pool.apply_async(runModels, args = (experiments[i*chunkSize:(i+1)*chunkSize], data, dependentVariable, independentVariables, regressionType)) for i in range(nProcesses)]
-        return pd.concat([job.get() for job in jobs])
+        return concat([job.get() for job in jobs])
 
 def findBestModelFwd(data, dependentVariable, independentVariables, modelFunc, experiments = None):
     '''Forward search for best model (based on adjusted R2)
@@ -463,10 +463,11 @@
     The results are added to experiments if provided as argument
     Storing in experiment relies on the index being the number equal 
     to the binary code derived from the independent variables'''
+    from numpy.random import permutation as nppermutation
     if experiments is None:
         experiments = generateExperiments(independentVariables)
     nIndependentVariables = len(independentVariables)
-    permutation = np.random.permutation(range(nIndependentVariables)).tolist()
+    permutation = nppermutation(range(nIndependentVariables)).tolist()
     variableMapping = {j: independentVariables[i] for i,j in enumerate(permutation)}
     print('Tested variables '+', '.join([variableMapping[i] for i in xrange(nIndependentVariables)]))
     bestModel = [False]*nIndependentVariables