DAMASK_EICMD/processing/post/permuteData.py

62 lines
2.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
import sys
2020-04-23 13:29:31 +05:30
from io import StringIO
from optparse import OptionParser
import numpy as np
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
2019-02-16 22:55:41 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
Permute all values in given column(s).
""", version = scriptID)
parser.add_option('-l','--label',
dest = 'label',
action = 'extend', metavar = '<string LIST>',
help ='column(s) to permute')
parser.add_option('-u', '--unique',
dest = 'unique',
action = 'store_true',
help = 'shuffle unique values as group')
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,
randomSeed = None,
)
(options,filenames) = parser.parse_args()
2020-04-23 13:29:31 +05:30
if filenames == []: filenames = [None]
2020-04-23 13:29:31 +05:30
for name in filenames:
damask.util.report(scriptName,name)
2020-04-23 13:29:31 +05:30
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
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)
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
table.to_ASCII(sys.stdout if name is None else name)