comparison python/utils.py @ 332:a6ca86107f27

reorganized utils module
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 14 Jun 2013 09:53:32 -0400
parents 99ca91a46007
children c9201f6b143a
comparison
equal deleted inserted replaced
331:40790d93200e 332:a6ca86107f27
18 'Returns the dictionary that provides for each element in the input list its index in the input list' 18 'Returns the dictionary that provides for each element in the input list its index in the input list'
19 result = {} 19 result = {}
20 for i,x in enumerate(l): 20 for i,x in enumerate(l):
21 result[x] = i 21 result[x] = i
22 return result 22 return result
23
24 #########################
25 # CLI utils
26 #########################
27
28 def parseCLIOptions(helpMessage, options, cliArgs, optionalOptions=[]):
29 ''' Simple function to handle similar argument parsing
30 Returns the dictionary of options and their values
31
32 * cliArgs are most likely directly sys.argv
33 (only the elements after the first one are considered)
34
35 * options should be a list of strings for getopt options,
36 eg ['frame=','correspondences=','video=']
37 A value must be provided for each option, or the program quits'''
38 import sys, getopt
39 from numpy.core.fromnumeric import all
40 optionValues, args = getopt.getopt(cliArgs[1:], 'h', ['help']+options+optionalOptions)
41 optionValues = dict(optionValues)
42
43 if '--help' in optionValues.keys() or '-h' in optionValues.keys():
44 print(helpMessage+
45 '\n - Compulsory options: '+' '.join([opt.replace('=','') for opt in options])+
46 '\n - Non-compulsory options: '+' '.join([opt.replace('=','') for opt in optionalOptions]))
47 sys.exit()
48
49 missingArgument = [('--'+opt.replace('=','') in optionValues.keys()) for opt in options]
50 if not all(missingArgument):
51 print('Missing argument')
52 print(optionValues)
53 sys.exit()
54
55 return optionValues
56 23
57 ######################### 24 #########################
58 # simple statistics 25 # simple statistics
59 ######################### 26 #########################
60 27
285 252
286 ######################### 253 #########################
287 # plotting section 254 # plotting section
288 ######################### 255 #########################
289 256
257 def plotPolygon(poly, options = ''):
258 'Plots shapely polygon poly'
259 from numpy.core.multiarray import array
260 from matplotlib.pyplot import plot
261 from shapely.geometry import Polygon
262
263 tmp = array(poly.exterior)
264 plot(tmp[:,0], tmp[:,1], options)
265
290 def stepPlot(X, firstX, lastX, initialCount = 0, increment = 1): 266 def stepPlot(X, firstX, lastX, initialCount = 0, increment = 1):
291 '''for each value in X, increment by increment the initial count 267 '''for each value in X, increment by increment the initial count
292 returns the lists that can be plotted 268 returns the lists that can be plotted
293 to obtain a step plot increasing by one for each value in x, from first to last value 269 to obtain a step plot increasing by one for each value in x, from first to last value
294 firstX and lastX should be respectively smaller and larger than all elements in X''' 270 firstX and lastX should be respectively smaller and larger than all elements in X'''
418 if (os.path.exists(filename)): 394 if (os.path.exists(filename)):
419 os.remove(filename) 395 os.remove(filename)
420 else: 396 else:
421 print(filename+' does not exist') 397 print(filename+' does not exist')
422 398
423 def plotPolygon(poly, options = ''):
424 from numpy.core.multiarray import array
425 from matplotlib.pyplot import plot
426 from shapely.geometry import Polygon
427
428 tmp = array(poly.exterior)
429 plot(tmp[:,0], tmp[:,1], options)
430
431 def line2Floats(l, separator=' '): 399 def line2Floats(l, separator=' '):
432 '''Returns the list of floats corresponding to the string''' 400 '''Returns the list of floats corresponding to the string'''
433 return [float(x) for x in l.split(separator)] 401 return [float(x) for x in l.split(separator)]
434 402
435 def line2Ints(l, separator=' '): 403 def line2Ints(l, separator=' '):
436 '''Returns the list of ints corresponding to the string''' 404 '''Returns the list of ints corresponding to the string'''
437 return [int(x) for x in l.split(separator)] 405 return [int(x) for x in l.split(separator)]
406
407 #########################
408 # CLI utils
409 #########################
410
411 def parseCLIOptions(helpMessage, options, cliArgs, optionalOptions=[]):
412 ''' Simple function to handle similar argument parsing
413 Returns the dictionary of options and their values
414
415 * cliArgs are most likely directly sys.argv
416 (only the elements after the first one are considered)
417
418 * options should be a list of strings for getopt options,
419 eg ['frame=','correspondences=','video=']
420 A value must be provided for each option, or the program quits'''
421 import sys, getopt
422 from numpy.core.fromnumeric import all
423 optionValues, args = getopt.getopt(cliArgs[1:], 'h', ['help']+options+optionalOptions)
424 optionValues = dict(optionValues)
425
426 if '--help' in optionValues.keys() or '-h' in optionValues.keys():
427 print(helpMessage+
428 '\n - Compulsory options: '+' '.join([opt.replace('=','') for opt in options])+
429 '\n - Non-compulsory options: '+' '.join([opt.replace('=','') for opt in optionalOptions]))
430 sys.exit()
431
432 missingArgument = [('--'+opt.replace('=','') in optionValues.keys()) for opt in options]
433 if not all(missingArgument):
434 print('Missing argument')
435 print(optionValues)
436 sys.exit()
437
438 return optionValues
439
440
438 441
439 ######################### 442 #########################
440 # sqlite 443 # sqlite
441 ######################### 444 #########################
442 445