comparison 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
comparison
equal deleted inserted replaced
727:c6d4ea05a2d0 746:e7ff0f60fef8
5 import matplotlib.pyplot as plt 5 import matplotlib.pyplot as plt
6 from datetime import time, datetime 6 from datetime import time, datetime
7 from math import sqrt, ceil, floor 7 from math import sqrt, ceil, floor
8 from scipy.stats import kruskal, shapiro 8 from scipy.stats import kruskal, shapiro
9 from scipy.spatial import distance 9 from scipy.spatial import distance
10 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve, dtype, isnan, NaN, mean, ma 10 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve, dtype, isnan, NaN, mean, ma, isinf
11 11
12 12
13 datetimeFormat = "%Y-%m-%d %H:%M:%S" 13 datetimeFormat = "%Y-%m-%d %H:%M:%S"
14
15 #########################
16 # Strings
17 #########################
18
19 def upperCaseFirstLetter(s):
20 words = s.split(' ')
21 lowerWords = [w[0].upper()+w[1:].lower() for w in words]
22 return ' '.join(lowerWords)
14 23
15 ######################### 24 #########################
16 # Enumerations 25 # Enumerations
17 ######################### 26 #########################
18 27
230 D = {} 239 D = {}
231 for x, y in zip(X,Y): 240 for x, y in zip(X,Y):
232 D[x]=y 241 D[x]=y
233 xsorted = sorted(D.keys()) 242 xsorted = sorted(D.keys())
234 return xsorted, [D[x] for x in xsorted] 243 return xsorted, [D[x] for x in xsorted]
244
245 def compareLengthForSort(i, j):
246 if len(i) < len(j):
247 return -1
248 elif len(i) == len(j):
249 return 0
250 else:
251 return 1
252
253 def sortByLength(instances, reverse = False):
254 '''Returns a new list with the instances sorted by length (method __len__)
255 reverse is passed to sorted'''
256 return sorted(instances, cmp = compareLengthForSort, reverse = reverse)
235 257
236 def ceilDecimals(v, nDecimals): 258 def ceilDecimals(v, nDecimals):
237 '''Rounds the number at the nth decimal 259 '''Rounds the number at the nth decimal
238 eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3''' 260 eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3'''
239 tens = 10**nDecimals 261 tens = 10**nDecimals
647 elif metric is not None and epsilon is None: 669 elif metric is not None and epsilon is None:
648 print("Please provide a value for epsilon if using a cdist metric. Exiting") 670 print("Please provide a value for epsilon if using a cdist metric. Exiting")
649 import sys 671 import sys
650 sys.exit() 672 sys.exit()
651 else: 673 else:
674 if similarityFunc is None and metric is not None and not isinf(delta):
675 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).')
652 self.similarityFunc = similarityFunc 676 self.similarityFunc = similarityFunc
653 self.metric = metric 677 self.metric = metric
654 self.epsilon = epsilon 678 self.epsilon = epsilon
655 self.aligned = aligned 679 self.aligned = aligned
656 self.delta = delta 680 self.delta = delta
863 else: 887 else:
864 return filename 888 return filename
865 889
866 def cleanFilename(s): 890 def cleanFilename(s):
867 'cleans filenames obtained when contatenating figure characteristics' 891 'cleans filenames obtained when contatenating figure characteristics'
868 return s.replace(' ','-').replace('.','').replace('/','-') 892 return s.replace(' ','-').replace('.','').replace('/','-').replace(',','')
869 893
870 def listfiles(dirname, extension, remove = False): 894 def listfiles(dirname, extension, remove = False):
871 '''Returns the list of files with the extension in the directory dirname 895 '''Returns the list of files with the extension in the directory dirname
872 If remove is True, the filenames are stripped from the extension''' 896 If remove is True, the filenames are stripped from the extension'''
873 from os import listdir 897 from os import listdir