2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2014-08-07 22:21:26 +05:30
|
|
|
from optparse import OptionParser
|
2019-06-14 16:33:30 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2013-11-27 01:49:27 +05:30
|
|
|
import damask
|
|
|
|
|
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])
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2013-11-27 01:49:27 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-16 22:55:41 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2013-11-27 01:49:27 +05:30
|
|
|
Permute all values in given column(s).
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
""", version = scriptID)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help ='column(s) to permute')
|
2015-12-04 06:47:45 +05:30
|
|
|
parser.add_option('-u', '--unique',
|
|
|
|
dest = 'unique',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'shuffle unique values as group')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-r', '--rnd',
|
|
|
|
dest = 'randomSeed',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'seed of random number generator [%default]')
|
|
|
|
|
|
|
|
parser.set_defaults(label = [],
|
2020-01-26 19:44:16 +05:30
|
|
|
unique = False,
|
2015-08-08 00:33:26 +05:30
|
|
|
randomSeed = None,
|
|
|
|
)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if len(options.label) == 0:
|
|
|
|
parser.error('no labels specified.')
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-02-11 22:52:47 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-18 20:07:32 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
for name in filenames:
|
2015-08-18 20:07:32 +05:30
|
|
|
try:
|
2020-02-20 19:35:38 +05:30
|
|
|
table = damask.ASCIItable(name = name)
|
2020-01-29 04:09:46 +05:30
|
|
|
except IOError:
|
|
|
|
continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ process labels ---------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
columns = []
|
|
|
|
dims = []
|
|
|
|
|
|
|
|
indices = table.label_index (options.label)
|
|
|
|
dimensions = table.label_dimension(options.label)
|
|
|
|
for i,index in enumerate(indices):
|
2015-12-04 06:47:45 +05:30
|
|
|
if index == -1: remarks.append('label "{}" not present...'.format(options.label[i]))
|
2014-08-07 22:21:26 +05:30
|
|
|
else:
|
2015-08-08 00:33:26 +05:30
|
|
|
columns.append(index)
|
|
|
|
dims.append(dimensions[i])
|
|
|
|
|
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
|
2014-08-07 22:21:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2020-02-21 15:30:53 +05:30
|
|
|
randomSeed = int(os.urandom(4).hex(), 16) if options.randomSeed is None else options.randomSeed # random seed per file
|
2015-08-08 00:33:26 +05:30
|
|
|
np.random.seed(randomSeed)
|
|
|
|
|
|
|
|
table.info_append([scriptID + '\t' + ' '.join(sys.argv[1:]),
|
|
|
|
'random seed {}'.format(randomSeed),
|
|
|
|
])
|
2014-08-07 22:21:26 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.data_readArray() # read all data at once
|
|
|
|
for col,dim in zip(columns,dims):
|
2015-12-04 06:47:45 +05:30
|
|
|
if options.unique:
|
|
|
|
s = set(map(tuple,table.data[:,col:col+dim])) # generate set of (unique) values
|
|
|
|
uniques = np.array(map(np.array,s)) # translate set to np.array
|
|
|
|
shuffler = dict(zip(s,np.random.permutation(len(s)))) # random permutation
|
|
|
|
table.data[:,col:col+dim] = uniques[np.array(map(lambda x: shuffler[tuple(x)],
|
|
|
|
table.data[:,col:col+dim]))] # fill table with mapped uniques
|
|
|
|
else:
|
|
|
|
np.random.shuffle(table.data[:,col:col+dim]) # independently shuffle every row
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.data_writeArray()
|
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII tables
|