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
|
2020-04-23 13:29:31 +05:30
|
|
|
from io import StringIO
|
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()
|
2020-04-23 13:29:31 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2020-04-23 13:29:31 +05:30
|
|
|
for name in filenames:
|
|
|
|
damask.util.report(scriptName,name)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2020-04-23 13:29:31 +05:30
|
|
|
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2020-04-23 13:29:31 +05:30
|
|
|
randomSeed = int(os.urandom(4).hex(), 16) if options.randomSeed is None else options.randomSeed # random seed per file
|
2020-04-29 01:49:09 +05:30
|
|
|
rng = np.random.default_rng(randomSeed)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2020-04-23 13:29:31 +05:30
|
|
|
for label in options.label:
|
|
|
|
data = table.get(label)
|
2020-04-29 01:49:09 +05:30
|
|
|
uniques,inverse = np.unique(data,return_inverse=True,axis=0) if options.unique else (data,np.arange(len(data)))
|
|
|
|
rng.shuffle(uniques)
|
|
|
|
table.set(label,uniques[inverse], scriptID+' '+' '.join(sys.argv[1:]))
|
2020-04-23 13:29:31 +05:30
|
|
|
|
2020-09-03 20:49:19 +05:30
|
|
|
table.to_file(sys.stdout if name is None else name)
|