simpler interface for progress bar
This commit is contained in:
parent
2f3b518562
commit
7768c5874b
|
@ -1,5 +1,5 @@
|
||||||
# -*- coding: UTF-8 no BOM -*-
|
# -*- coding: UTF-8 no BOM -*-
|
||||||
import sys,time,random,threading,os,subprocess,shlex
|
import sys,time,random,threading,os,subprocess,shlex,time
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from optparse import Option
|
from optparse import Option
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ class extendableOption(Option):
|
||||||
|
|
||||||
# Print iterations progress
|
# Print iterations progress
|
||||||
# from https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
|
# from https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
|
||||||
def progressBar(iteration, total, start=None, prefix='', suffix='', decimals=1, bar_length=50):
|
def progressBar(iteration, total, prefix='', bar_length=50):
|
||||||
"""
|
"""
|
||||||
Call in a loop to create terminal progress bar
|
Call in a loop to create terminal progress bar
|
||||||
|
|
||||||
|
@ -142,23 +142,30 @@ def progressBar(iteration, total, start=None, prefix='', suffix='', decimals=1,
|
||||||
iteration - Required : current iteration (Int)
|
iteration - Required : current iteration (Int)
|
||||||
total - Required : total iterations (Int)
|
total - Required : total iterations (Int)
|
||||||
prefix - Optional : prefix string (Str)
|
prefix - Optional : prefix string (Str)
|
||||||
suffix - Optional : suffix string (Str)
|
|
||||||
decimals - Optional : positive number of decimals in percent complete (Int)
|
|
||||||
bar_length - Optional : character length of bar (Int)
|
bar_length - Optional : character length of bar (Int)
|
||||||
"""
|
"""
|
||||||
import time
|
|
||||||
|
|
||||||
if suffix == '' and start is not None and iteration > 0:
|
fraction = iteration / float(total)
|
||||||
remainder = (total - iteration) * (time.time()-start)/iteration
|
if not hasattr(progressBar, "last_fraction"): # first call to function
|
||||||
suffix = '{: 3d}:'.format(int( remainder//3600)) + \
|
progressBar.start_time = time.time()
|
||||||
'{:02d}:'.format(int((remainder//60)%60)) + \
|
progressBar.last_fraction = -1.0
|
||||||
'{:02d}' .format(int( remainder %60))
|
remaining_time = ' n/a'
|
||||||
str_format = "{{0:{}.{}f}}".format(decimals+4,decimals)
|
else:
|
||||||
percents = str_format.format(100 * (iteration / float(total)))
|
if fraction <= progressBar.last_fraction or iteration == 0: # reset: called within a new loop
|
||||||
filled_length = int(round(bar_length * iteration / float(total)))
|
progressBar.start_time = time.time()
|
||||||
bar = '█' * filled_length + '-' * (bar_length - filled_length)
|
progressBar.last_fraction = -1.0
|
||||||
|
remaining_time = ' n/a'
|
||||||
|
else:
|
||||||
|
progressBar.last_fraction = fraction
|
||||||
|
remainder = (total - iteration) * (time.time()-progressBar.start_time)/iteration
|
||||||
|
remaining_time = '{: 3d}:'.format(int( remainder//3600)) + \
|
||||||
|
'{:02d}:'.format(int((remainder//60)%60)) + \
|
||||||
|
'{:02d}' .format(int( remainder %60))
|
||||||
|
|
||||||
sys.stderr.write('\r%s |%s| %s %s' % (prefix, bar, percents+'%', suffix)),
|
filled_length = int(round(bar_length * fraction))
|
||||||
|
bar = '█' * filled_length + '░' * (bar_length - filled_length)
|
||||||
|
|
||||||
|
sys.stderr.write('\r{} {} {}'.format(prefix, bar, remaining_time)),
|
||||||
|
|
||||||
if iteration == total: sys.stderr.write('\n\n')
|
if iteration == total: sys.stderr.write('\n\n')
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
|
|
|
@ -830,10 +830,9 @@ if options.info:
|
||||||
|
|
||||||
elementsOfNode = {}
|
elementsOfNode = {}
|
||||||
Nelems = stat['NumberOfElements']
|
Nelems = stat['NumberOfElements']
|
||||||
starttime = time.time()
|
|
||||||
for e in range(Nelems):
|
for e in range(Nelems):
|
||||||
if options.verbose and Nelems > 100 and e%(Nelems//100) == 0: # report in 1% steps if possible and avoid modulo by zero
|
if options.verbose and Nelems >= 50 and e%(Nelems//50) == 0: # report in 2% steps if possible and avoid modulo by zero
|
||||||
damask.util.progressBar(iteration=e,total=Nelems,start=starttime,prefix='1/3: connecting elements')
|
damask.util.progressBar(iteration=e,total=Nelems,prefix='1/3: connecting elements')
|
||||||
for n in map(p.node_sequence,p.element(e).items):
|
for n in map(p.node_sequence,p.element(e).items):
|
||||||
if n not in elementsOfNode:
|
if n not in elementsOfNode:
|
||||||
elementsOfNode[n] = [p.element_id(e)]
|
elementsOfNode[n] = [p.element_id(e)]
|
||||||
|
@ -856,10 +855,9 @@ damask.util.progressBar(iteration=1,total=1,prefix='1/3: connecting elements')
|
||||||
|
|
||||||
if options.nodalScalar:
|
if options.nodalScalar:
|
||||||
Npoints = stat['NumberOfNodes']
|
Npoints = stat['NumberOfNodes']
|
||||||
starttime = time.time()
|
|
||||||
for n in range(Npoints):
|
for n in range(Npoints):
|
||||||
if options.verbose and Npoints > 100 and e%(Npoints//100) == 0: # report in 1% steps if possible and avoid modulo by zero
|
if options.verbose and Npoints >= 50 and e%(Npoints//50) == 0: # report in 2% steps if possible and avoid modulo by zero
|
||||||
damask.util.progressBar(iteration=n,total=Npoints,start=starttime,prefix='2/3: scanning nodes ')
|
damask.util.progressBar(iteration=n,total=Npoints,prefix='2/3: scanning nodes ')
|
||||||
myNodeID = p.node_id(n)
|
myNodeID = p.node_id(n)
|
||||||
myNodeCoordinates = [p.node(n).x, p.node(n).y, p.node(n).z]
|
myNodeCoordinates = [p.node(n).x, p.node(n).y, p.node(n).z]
|
||||||
myElemID = 0
|
myElemID = 0
|
||||||
|
@ -894,10 +892,9 @@ if options.nodalScalar:
|
||||||
|
|
||||||
else:
|
else:
|
||||||
Nelems = stat['NumberOfElements']
|
Nelems = stat['NumberOfElements']
|
||||||
starttime = time.time()
|
|
||||||
for e in range(Nelems):
|
for e in range(Nelems):
|
||||||
if options.verbose and Nelems > 100 and e%(Nelems//100) == 0: # report in 1% steps if possible and avoid modulo by zero
|
if options.verbose and Nelems >= 50 and e%(Nelems//50) == 0: # report in 2% steps if possible and avoid modulo by zero
|
||||||
damask.util.progressBar(iteration=e,total=Nelems,start=starttime,prefix='2/3: scanning elements ')
|
damask.util.progressBar(iteration=e,total=Nelems,prefix='2/3: scanning elements ')
|
||||||
myElemID = p.element_id(e)
|
myElemID = p.element_id(e)
|
||||||
myIpCoordinates = ipCoords(p.element(e).type, list(map(lambda node: [node.x, node.y, node.z],
|
myIpCoordinates = ipCoords(p.element(e).type, list(map(lambda node: [node.x, node.y, node.z],
|
||||||
list(map(p.node, map(p.node_sequence, p.element(e).items))))))
|
list(map(p.node, map(p.node_sequence, p.element(e).items))))))
|
||||||
|
@ -1035,11 +1032,10 @@ for incCount,position in enumerate(locations): # walk through locations
|
||||||
|
|
||||||
member = 0
|
member = 0
|
||||||
Ngroups = len(groups)
|
Ngroups = len(groups)
|
||||||
starttime = time.time()
|
|
||||||
for j,group in enumerate(groups):
|
for j,group in enumerate(groups):
|
||||||
f = incCount*Ngroups + j
|
f = incCount*Ngroups + j
|
||||||
if options.verbose and (Ngroups*Nincs) > 100 and f%((Ngroups*Nincs)//100) == 0: # report in 1% steps if possible and avoid modulo by zero
|
if options.verbose and (Ngroups*Nincs) >= 50 and f%((Ngroups*Nincs)//50) == 0: # report in 2% steps if possible and avoid modulo by zero
|
||||||
damask.util.progressBar(iteration=f,total=Ngroups*Nincs,start=starttime,prefix='3/3: processing points ')
|
damask.util.progressBar(iteration=f,total=Ngroups*Nincs,prefix='3/3: processing points ')
|
||||||
N = 0 # group member counter
|
N = 0 # group member counter
|
||||||
for (e,n,i,g,n_local) in group[1:]: # loop over group members
|
for (e,n,i,g,n_local) in group[1:]: # loop over group members
|
||||||
member += 1
|
member += 1
|
||||||
|
@ -1091,7 +1087,7 @@ for incCount,position in enumerate(locations): # walk through locations
|
||||||
['Crystallite']*len(options.crystalliteResult) +
|
['Crystallite']*len(options.crystalliteResult) +
|
||||||
['Constitutive']*len(options.constitutiveResult)
|
['Constitutive']*len(options.constitutiveResult)
|
||||||
):
|
):
|
||||||
outputIndex = (list(zip(*outputFormat[resultType]['outputs']))[0]).index(label) # find the position of this output in the outputFormat
|
outputIndex = (list(zip(*outputFormat[resultType]['outputs']))[0]).index(label) # find the position of this output in the outputFormat
|
||||||
length = int(outputFormat[resultType]['outputs'][outputIndex][1])
|
length = int(outputFormat[resultType]['outputs'][outputIndex][1])
|
||||||
thisHead = heading('_',[[component,''.join( label.split() )] for component in range(int(length>1),length+int(length>1))])
|
thisHead = heading('_',[[component,''.join( label.split() )] for component in range(int(length>1),length+int(length>1))])
|
||||||
if assembleHeader: header += thisHead
|
if assembleHeader: header += thisHead
|
||||||
|
|
Loading…
Reference in New Issue