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')
|
|
|
|
scriptName = scriptID.split()[1][:-3]
|
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
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
parser.add_option('-l','--label', dest='label', action='extend', metavar='<strig LIST>',
|
|
|
|
help='heading(s) of column to permute')
|
|
|
|
parser.add_option('-r', '--rnd', dest='randomSeed', type='int', metavar='int',
|
|
|
|
help='seed of random number generator [%default]')
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
parser.set_defaults(label = [])
|
2014-08-07 22:21:26 +05:30
|
|
|
parser.set_defaults(randomSeed = None)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(options.label)== 0:
|
|
|
|
parser.error('no data column specified...')
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
datainfo = { # list of requested labels per datatype
|
2013-11-27 01:49:27 +05:30
|
|
|
'scalar': {'len':1,
|
|
|
|
'label':[]},
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.label != None: datainfo['scalar']['label'] += options.label
|
2014-08-07 22:21:26 +05:30
|
|
|
np.random.seed(options.randomSeed)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
# ------------------------------------------ setup file handles ------------------------------------
|
2013-11-27 01:49:27 +05:30
|
|
|
files = []
|
2014-08-07 22:21:26 +05:30
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
|
|
|
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
|
|
|
|
|
|
|
#--- loop over input files -------------------------------------------------------------------------
|
2013-11-27 01:49:27 +05:30
|
|
|
for file in files:
|
2014-08-07 22:21:26 +05:30
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],True) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
# --------------- figure out columns to process ---------------------------------------------------
|
|
|
|
active = []
|
|
|
|
column = {}
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
for label in datainfo['scalar']['label']:
|
|
|
|
if label in table.labels:
|
|
|
|
active.append(label)
|
|
|
|
column[label] = table.labels.index(label) # remember columns of requested data
|
|
|
|
else:
|
|
|
|
file['croak'].write('column %s not found...\n'%label)
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2013-11-27 01:49:27 +05:30
|
|
|
permutation = {}
|
2014-08-07 22:21:26 +05:30
|
|
|
table.data_readArray([column[label] for label in active])
|
|
|
|
for i,label in enumerate(active):
|
|
|
|
mySeed = np.random.randint(9999999)
|
|
|
|
np.random.seed(mySeed)
|
2013-12-14 09:21:22 +05:30
|
|
|
unique = list(set(table.data[:,i]))
|
2014-08-07 22:21:26 +05:30
|
|
|
permutated = np.random.permutation(unique)
|
2013-11-27 01:49:27 +05:30
|
|
|
permutation[label] = dict(zip(unique,permutated))
|
|
|
|
|
|
|
|
table.data_rewind()
|
2014-08-07 22:21:26 +05:30
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
for label in active: # loop over all requested stiffnesses
|
|
|
|
for c in xrange(column[label],
|
|
|
|
column[label]+datainfo['scalar']['len']):
|
|
|
|
table.data[c] = permutation[label][float(table.data[c])] # apply permutation
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
|
|
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-08-22 22:28:53 +05:30
|
|
|
table.input_close() # close input ASCII table
|
|
|
|
table.output_close() # close output ASCII table
|
2014-08-07 22:21:26 +05:30
|
|
|
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|