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-02-11 22:52:47 +05:30
|
|
|
parser.add_option('-l','--label', dest='label', action='extend', metavar='<string LIST>',
|
2015-05-22 10:27:05 +05:30
|
|
|
help='heading(s) of column to permute')
|
2014-08-07 22:21:26 +05:30
|
|
|
parser.add_option('-r', '--rnd', dest='randomSeed', type='int', metavar='int',
|
2015-05-22 10:27:05 +05:30
|
|
|
help='seed of random number generator [%default]')
|
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()
|
|
|
|
|
2015-05-22 10:27:05 +05:30
|
|
|
if options.label == None:
|
2013-11-27 01:49:27 +05:30
|
|
|
parser.error('no data column specified...')
|
|
|
|
|
2015-02-11 22:52:47 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2014-08-07 22:21:26 +05:30
|
|
|
for name in filenames:
|
2015-02-11 22:52:47 +05:30
|
|
|
if not os.path.exists(name): continue
|
|
|
|
file = {'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr}
|
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
|
|
|
|
2015-05-22 11:26:49 +05:30
|
|
|
randomSeed = int(os.urandom(4).encode('hex'), 16) if options.randomSeed == None else options.randomSeed # radom seed per file for second phase
|
|
|
|
np.random.seed(randomSeed)
|
2015-02-11 22:52:47 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],buffered=False) # make unbuffered ASCII_table
|
2014-08-07 22:21:26 +05:30
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2015-05-22 11:26:49 +05:30
|
|
|
table.info_append('random seed %i'%randomSeed)
|
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
|
|
|
|
2015-05-25 16:19:20 +05:30
|
|
|
for label in options.label:
|
2014-08-07 22:21:26 +05:30
|
|
|
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 = {}
|
2015-05-22 10:27:05 +05:30
|
|
|
table.data_readArray(active)
|
2015-05-22 11:26:49 +05:30
|
|
|
for i,label in enumerate(active):
|
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()
|
2015-05-22 10:27:05 +05:30
|
|
|
table.head_read() # read ASCII header info again to get the completed data
|
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
|
2015-05-22 11:26:49 +05:30
|
|
|
table.data[column[label]] = permutation[label][float(table.data[column[label]])] # 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
|