2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2014-07-25 01:51:18 +05:30
|
|
|
from optparse import OptionParser
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2019-06-14 20:57:00 +05:30
|
|
|
import numpy as np
|
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
import damask
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-06-06 15:13:28 +05:30
|
|
|
|
2012-02-07 18:32:56 +05:30
|
|
|
# definition of element-wise p-norms for matrices
|
2019-02-16 19:23:56 +05:30
|
|
|
# ToDo: better use numpy.linalg.norm
|
2012-02-07 18:32:56 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
def norm(which,object):
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if which == 'Abs': # p = 1
|
|
|
|
return sum(map(abs, object))
|
|
|
|
elif which == 'Frobenius': # p = 2
|
2019-06-14 16:33:30 +05:30
|
|
|
return np.sqrt(sum([x*x for x in object]))
|
2015-08-08 00:33:26 +05:30
|
|
|
elif which == 'Max': # p = inf
|
|
|
|
return max(map(abs, object))
|
2019-02-16 19:23:56 +05:30
|
|
|
else:
|
|
|
|
return -1
|
2012-02-07 18:32:56 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2011-06-21 21:55:48 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-16 22:55:41 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2011-06-21 21:55:48 +05:30
|
|
|
Add column(s) containing norm of requested column(s) being either vectors or tensors.
|
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2012-02-08 17:30:31 +05:30
|
|
|
normChoices = ['abs','frobenius','max']
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-n','--norm',
|
|
|
|
dest = 'norm',
|
|
|
|
type = 'choice', choices = normChoices, metavar='string',
|
|
|
|
help = 'type of element-wise p-norm [frobenius] {%s}'%(','.join(map(str,normChoices))))
|
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'heading of column(s) to calculate norm of')
|
|
|
|
|
|
|
|
parser.set_defaults(norm = 'frobenius',
|
|
|
|
)
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2019-02-16 19:23:56 +05:30
|
|
|
if options.norm.lower() not in normChoices:
|
|
|
|
parser.error('invalid norm ({}) specified.'.format(options.norm))
|
2016-03-02 01:41:43 +05:30
|
|
|
if options.label is None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no data column specified.')
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-21 01:12:05 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
columns = []
|
|
|
|
dims = []
|
|
|
|
|
|
|
|
for what in options.label:
|
|
|
|
dim = table.label_dimension(what)
|
|
|
|
if dim < 0: remarks.append('column {} not found...'.format(what))
|
|
|
|
else:
|
|
|
|
dims.append(dim)
|
|
|
|
columns.append(table.label_index(what))
|
2019-02-16 19:23:56 +05:30
|
|
|
table.labels_append('norm{}({})'.format(options.norm.capitalize(),what)) # extend ASCII header with new labels
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header --------------------------------------
|
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2011-11-23 20:24:53 +05:30
|
|
|
table.head_write()
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-06-06 15:13:28 +05:30
|
|
|
outputAlive = True
|
2014-07-25 01:51:18 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2015-08-08 00:33:26 +05:30
|
|
|
for column,dim in zip(columns,dims):
|
|
|
|
table.data_append(norm(options.norm.capitalize(),
|
|
|
|
map(float,table.data[column:column+dim])))
|
2014-07-25 01:51:18 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close input ASCII table (works for stdin)
|