Mercurial > hg > nsaunier > traffic-intelligence
comparison python/utils.py @ 1021:16932cefabc1
work on paths in line with new configurations from tracker
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 05 Jun 2018 17:02:28 -0400 |
| parents | 4f3387a242a1 |
| children | b7689372c0ec |
comparison
equal
deleted
inserted
replaced
| 1020:9fb82fe0156f | 1021:16932cefabc1 |
|---|---|
| 3 ''' Generic utilities.''' | 3 ''' Generic utilities.''' |
| 4 | 4 |
| 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 argparse import ArgumentTypeError | 7 from argparse import ArgumentTypeError |
| 8 from pathlib import Path | |
| 8 from math import sqrt, ceil, floor | 9 from math import sqrt, ceil, floor |
| 9 from scipy.stats import kruskal, shapiro, lognorm | 10 from scipy.stats import kruskal, shapiro, lognorm |
| 10 from scipy.spatial import distance | 11 from scipy.spatial import distance |
| 11 from scipy.sparse import dok_matrix | 12 from scipy.sparse import dok_matrix |
| 12 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve, dtype, isnan, NaN, mean, ma, isinf, savez, load as npload, log | 13 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve, dtype, isnan, NaN, mean, ma, isinf, savez, load as npload, log |
| 969 | 970 |
| 970 def cleanFilename(s): | 971 def cleanFilename(s): |
| 971 'cleans filenames obtained when contatenating figure characteristics' | 972 'cleans filenames obtained when contatenating figure characteristics' |
| 972 return s.replace(' ','-').replace('.','').replace('/','-').replace(',','') | 973 return s.replace(' ','-').replace('.','').replace('/','-').replace(',','') |
| 973 | 974 |
| 975 def getRelativeFilename(parentPath, filename): | |
| 976 'Returns filename if absolute, otherwise parentPath/filename as string' | |
| 977 filePath = Path(filename) | |
| 978 if filePath.is_absolute(): | |
| 979 return filename | |
| 980 else: | |
| 981 return str(parentPath/filePath) | |
| 982 | |
| 974 def listfiles(dirname, extension, remove = False): | 983 def listfiles(dirname, extension, remove = False): |
| 975 '''Returns the list of files with the extension in the directory dirname | 984 '''Returns the list of files with the extension in the directory dirname |
| 976 If remove is True, the filenames are stripped from the extension''' | 985 If remove is True, the filenames are stripped from the extension''' |
| 977 from os import listdir | 986 d = Path(dirname) |
| 978 tmp = [f for f in listdir(dirname) if f.endswith(extension)] | 987 if d.is_dir(): |
| 979 tmp.sort() | 988 tmp = [str(f) for f in d.glob('*.extension')] |
| 980 if remove: | 989 if remove: |
| 981 return [removeExtension(f, extension) for f in tmp] | 990 return [removeExtension(f, extension) for f in tmp] |
| 982 else: | 991 else: |
| 983 return tmp | 992 return tmp |
| 993 else: | |
| 994 print(dirname+' is not a directory') | |
| 995 return [] | |
| 984 | 996 |
| 985 def mkdir(dirname): | 997 def mkdir(dirname): |
| 986 'Creates a directory if it does not exist' | 998 'Creates a directory if it does not exist' |
| 987 import os | 999 p = Path(dirname) |
| 988 if not os.path.exists(dirname): | 1000 if not p.exists(): |
| 989 os.mkdir(dirname) | 1001 p.mkdir() |
| 990 else: | 1002 else: |
| 991 print(dirname+' already exists') | 1003 print(dirname+' already exists') |
| 992 | 1004 |
| 993 def removeFile(filename): | 1005 def removeFile(filename): |
| 994 '''Deletes the file while avoiding raising an error | 1006 '''Deletes the file while avoiding raising an error |
| 995 if the file does not exist''' | 1007 if the file does not exist''' |
| 996 import os | 1008 f = Path(filename) |
| 997 if (os.path.exists(filename)): | 1009 if (f.exists()): |
| 998 os.remove(filename) | 1010 f.unlink() |
| 999 else: | 1011 else: |
| 1000 print(filename+' does not exist') | 1012 print(filename+' does not exist') |
| 1001 | 1013 |
| 1002 def line2Floats(l, separator=' '): | 1014 def line2Floats(l, separator=' '): |
| 1003 '''Returns the list of floats corresponding to the string''' | 1015 '''Returns the list of floats corresponding to the string''' |
