2013-11-27 01:49:27 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
import os,sys,string
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2013-11-27 01:49:27 +05:30
|
|
|
import damask
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[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')
|
|
|
|
parser.add_option('-r', '--rnd',
|
|
|
|
dest = 'randomSeed',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'seed of random number generator [%default]')
|
|
|
|
|
|
|
|
parser.set_defaults(label = [],
|
|
|
|
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
|
|
|
|
|
|
|
if filenames == []: filenames = ['STDIN']
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
for name in filenames:
|
2015-08-08 00:33:26 +05:30
|
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
|
|
table = damask.ASCIItable(name = name, outname = name+'_tmp',
|
|
|
|
buffered = False)
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
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):
|
|
|
|
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])
|
|
|
|
|
|
|
|
if remarks != []: table.croak(remarks)
|
|
|
|
if errors != []:
|
|
|
|
table.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
2014-08-07 22:21:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
randomSeed = int(os.urandom(4).encode('hex'), 16) if options.randomSeed == None else options.randomSeed # random seed per file
|
|
|
|
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):
|
|
|
|
table.data[:,col:col+dim] = np.random.permutation(table.data[:,col:col+dim])
|
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
|
|
|
|
if name != 'STDIN': os.rename(name+'_tmp',name) # overwrite old one with tmp new
|