diff python/utils.py @ 746:e7ff0f60fef8

merged new developments (indicator and trajectory clustering)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 10 Sep 2015 15:52:45 -0400
parents fe71639f1ee7
children 10dbab1e871d
line wrap: on
line diff
--- a/python/utils.py	Mon Aug 10 01:06:59 2015 -0400
+++ b/python/utils.py	Thu Sep 10 15:52:45 2015 -0400
@@ -7,12 +7,21 @@
 from math import sqrt, ceil, floor
 from scipy.stats import kruskal, shapiro
 from scipy.spatial import distance
-from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve,  dtype, isnan, NaN, mean, ma
+from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve,  dtype, isnan, NaN, mean, ma, isinf
 
 
 datetimeFormat = "%Y-%m-%d %H:%M:%S"
 
 #########################
+# Strings
+#########################
+
+def upperCaseFirstLetter(s):
+    words = s.split(' ')
+    lowerWords = [w[0].upper()+w[1:].lower() for w in words]
+    return ' '.join(lowerWords)
+
+#########################
 # Enumerations
 #########################
 
@@ -233,6 +242,19 @@
     xsorted = sorted(D.keys())
     return xsorted, [D[x] for x in xsorted]
 
+def compareLengthForSort(i, j):
+    if len(i) < len(j):
+        return -1
+    elif len(i) == len(j):
+        return 0
+    else:
+        return 1
+
+def sortByLength(instances, reverse = False):
+    '''Returns a new list with the instances sorted by length (method __len__)
+    reverse is passed to sorted'''
+    return sorted(instances, cmp = compareLengthForSort, reverse = reverse)
+
 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'''
@@ -649,6 +671,8 @@
             import sys
             sys.exit()
         else:
+            if similarityFunc is None and metric is not None and not isinf(delta):
+                print('Warning: you are using a cdist metric and a finite delta, which will make probably computation slower than using the equivalent similarityFunc (since all pairwise distances will be computed by cdist).')
             self.similarityFunc = similarityFunc
             self.metric = metric
             self.epsilon = epsilon
@@ -865,7 +889,7 @@
 
 def cleanFilename(s):
     'cleans filenames obtained when contatenating figure characteristics'
-    return s.replace(' ','-').replace('.','').replace('/','-')
+    return s.replace(' ','-').replace('.','').replace('/','-').replace(',','')
 
 def listfiles(dirname, extension, remove = False):
     '''Returns the list of files with the extension in the directory dirname