python3 compatible

hope that the new bar causes less trouble than the background message
This commit is contained in:
Martin Diehl 2018-12-09 09:08:33 +01:00
parent 6b73840853
commit 7eaea64d09
2 changed files with 30 additions and 22 deletions

View File

@ -132,6 +132,30 @@ class extendableOption(Option):
else: else:
Option.take_action(self, action, dest, opt, value, values, parser) Option.take_action(self, action, dest, opt, value, values, parser)
# Print iterations progress
# from https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
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)
"""
str_format = "{0:." + str(decimals) + "f}"
percents = str_format.format(100 * (iteration / float(total)))
filled_length = int(round(bar_length * iteration / float(total)))
bar = '' * filled_length + '-' * (bar_length - filled_length)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
# ----------------------------- # -----------------------------
class backgroundMessage(threading.Thread): class backgroundMessage(threading.Thread):
"""Reporting with animation to indicate progress""" """Reporting with animation to indicate progress"""

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2.7 #!/usr/bin/env python3
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
import os,sys,time,copy import os,sys,time,copy
@ -78,7 +78,7 @@ for name in filenames:
errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos)) errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos))
if not np.all(table.label_dimension(options.quaternion) == 4): if not np.all(table.label_dimension(options.quaternion) == 4):
errors.append('input "{}" does not have dimension 4.'.format(options.quaternion)) errors.append('input "{}" does not have dimension 4.'.format(options.quaternion))
else: column = table.label_index(label) else: column = table.label_index(options.quaternion)
if remarks != []: damask.util.croak(remarks) if remarks != []: damask.util.croak(remarks)
if errors != []: if errors != []:
@ -93,26 +93,15 @@ for name in filenames:
options.disorientation)) # report orientation source and disorientation options.disorientation)) # report orientation source and disorientation
table.head_write() table.head_write()
# ------------------------------------------ process data ------------------------------------------
# ------------------------------------------ build KD tree ----------------------------------------- # ------------------------------------------ build KD tree -----------------------------------------
table.data_readArray(options.pos) # read position vectors table.data_readArray(options.pos) # read position vectors
grainID = -np.ones(len(table.data),dtype=int) grainID = -np.ones(len(table.data),dtype=int)
Npoints = table.data.shape[0]
# --- start background messaging
bg = damask.util.backgroundMessage()
bg.start()
start = tick = time.clock()
bg.set_message('building KD tree...')
kdtree = spatial.KDTree(copy.deepcopy(table.data)) kdtree = spatial.KDTree(copy.deepcopy(table.data))
# ------------------------------------------ assign grain IDs -------------------------------------- # ------------------------------------------ assign grain IDs --------------------------------------
tick = time.clock()
orientations = [] # quaternions found for grain orientations = [] # quaternions found for grain
memberCounts = [] # number of voxels in grain memberCounts = [] # number of voxels in grain
p = 0 # point counter p = 0 # point counter
@ -123,13 +112,10 @@ for name in filenames:
table.data_rewind() table.data_rewind()
while table.data_read(): # read next data line of ASCII table while table.data_read(): # read next data line of ASCII table
if p > 0 and p % 1000 == 0: if Npoints > 100 and p%(Npoints//100) == 0: # report in 1% steps if possible and avoid modulo by zero
damask.util.print_progress(iteration=p,total=Npoints)
time_delta = (time.clock()-tick) * (len(grainID) - p) / p o = damask.Orientation(quaternion = np.array(list(map(float,table.data[column:column+4]))),
bg.set_message('(%02i:%02i:%02i) processing point %i of %i (grain count %i)...'\
%(time_delta//3600,time_delta%3600//60,time_delta%60,p,len(grainID),np.count_nonzero(memberCounts)))
o = damask.Orientation(quaternion = np.array(map(float,table.data[column:column+4])),
symmetry = options.symmetry).reduced() symmetry = options.symmetry).reduced()
matched = False matched = False
@ -180,8 +166,6 @@ for name in filenames:
outputAlive = table.data_write() # output processed line outputAlive = table.data_write() # output processed line
p += 1 p += 1
bg.set_message('done after {} seconds'.format(time.clock()-start))
# ------------------------------------------ output finalization ----------------------------------- # ------------------------------------------ output finalization -----------------------------------
table.close() # close ASCII tables table.close() # close ASCII tables