diff python/utils.py @ 672:5473b7460375

moved and rationalized imports in modules
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 26 May 2015 13:53:40 +0200
parents 849f5f8bf4b9
children 01b89182891a
line wrap: on
line diff
--- a/python/utils.py	Tue May 26 11:40:32 2015 +0200
+++ b/python/utils.py	Tue May 26 13:53:40 2015 +0200
@@ -5,6 +5,7 @@
 from datetime import time, datetime
 from math import sqrt, ceil, floor
 from scipy.stats import kruskal, shapiro
+from numpy import zeros, array, exp, sum as npsum, arange, cumsum
 
 datetimeFormat = "%Y-%m-%d %H:%M:%S"
 
@@ -60,7 +61,6 @@
 
 def cumulativeDensityFunction(sample, normalized = False):
     '''Returns the cumulative density function of the sample of a random variable'''
-    from numpy import arange, cumsum
     xaxis = sorted(sample)
     counts = arange(1,len(sample)+1) # dtype = float
     if normalized:
@@ -70,15 +70,13 @@
 class EmpiricalDiscreteDistribution(EmpiricalDistribution):
     '''Class to represent a sample of a distribution for a discrete random variable
     '''
-    from numpy.core.fromnumeric import sum
-
     def __init__(self, categories, counts):
         self.categories = categories
         self.counts = counts
 
     def mean(self):
         result = [float(x*y) for x,y in zip(self.categories, self.counts)]
-        return sum(result)/self.nSamples()
+        return npsum(result)/self.nSamples()
 
     def var(self, mean = None):
         if not mean:
@@ -87,12 +85,12 @@
             m = mean
         result = 0.
         squares = [float((x-m)*(x-m)*y) for x,y in zip(self.categories, self.counts)]
-        return sum(squares)/(self.nSamples()-1)
+        return npsum(squares)/(self.nSamples()-1)
 
     def referenceCounts(self, probability):
         '''probability is a function that returns the probability of the random variable for the category values'''
         refProba = [probability(c) for c in self.categories]
-        refProba[-1] = 1-sum(refProba[:-1])
+        refProba[-1] = 1-npsum(refProba[:-1])
         refCounts = [r*self.nSamples() for r in refProba]
         return refCounts, refProba
 
@@ -168,7 +166,6 @@
 def kernelSmoothing(x, X, Y, weightFunc, halfwidth):
     '''Returns the smoothed estimate of (X,Y) at x
     Sum_x weight(sample_x,x) * y(x)'''
-    from numpy import zeros, array
     weights = array([weightFunc(x,observedx, halfwidth) for observedx in X])
     if sum(weights)>0:
         return sum(weights*Y)/sum(weights)
@@ -182,7 +179,6 @@
         return 0.
 
 def gaussian(center, x, halfwidth):
-    from numpy import exp
     return exp(-((center-x)/halfwidth)**2/2)
 
 def epanechnikov(center, x, halfwidth):