2012-02-16 14:06:35 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2015-09-30 03:57:07 +05:30
|
|
|
import os,re,sys,fnmatch,math,random
|
2015-09-24 14:54:42 +05:30
|
|
|
import numpy as np
|
2014-08-07 00:36:33 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2013-09-09 19:42:00 +05:30
|
|
|
scriptID = '$Id$'
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2012-02-16 14:06:35 +05:30
|
|
|
|
fixed sorting logic to respect numerical values of vector data (i.e. 1,2,3,….,10,11,… instead of 10,11,12,1,2,…)
2015-09-09 05:25:44 +05:30
|
|
|
def sortingList(labels,whitelistitems):
|
|
|
|
|
|
|
|
indices = []
|
|
|
|
names = []
|
|
|
|
|
|
|
|
for label in labels:
|
|
|
|
if re.match('^\d+_',label):
|
|
|
|
indices.append(int(label.split('_',1)[0]))
|
|
|
|
names.append(label.split('_',1)[1])
|
|
|
|
else:
|
|
|
|
indices.append(0)
|
|
|
|
names.append(label)
|
|
|
|
|
|
|
|
return [indices,names,whitelistitems]
|
|
|
|
|
|
|
|
|
2012-02-16 14:06:35 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2012-02-16 14:06:35 +05:30
|
|
|
Filter rows according to condition and columns by either white or black listing.
|
|
|
|
|
2012-12-03 19:59:21 +05:30
|
|
|
Examples:
|
|
|
|
Every odd row if x coordinate is positive -- " #ip.x# >= 0.0 and #_row_#%2 == 1 ).
|
2013-12-12 08:05:01 +05:30
|
|
|
All rows where label 'foo' equals 'bar' -- " #foo# == \"bar\" "
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
""", version = scriptID)
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-w','--white',
|
|
|
|
dest = 'whitelist',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'whitelist of column labels (a,b,c,...)')
|
|
|
|
parser.add_option('-b','--black',
|
|
|
|
dest = 'blacklist',
|
|
|
|
action = 'extend', metavar='<string LIST>',
|
|
|
|
help = 'blacklist of column labels (a,b,c,...)')
|
|
|
|
parser.add_option('-c','--condition',
|
|
|
|
dest = 'condition', metavar='string',
|
|
|
|
help = 'condition to filter rows')
|
|
|
|
|
|
|
|
parser.set_defaults(condition = '',
|
|
|
|
)
|
2012-02-16 14:06:35 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-13 03:22:33 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-10-24 02:53:10 +05:30
|
|
|
for name in filenames:
|
2015-08-13 03:22:33 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble info ---------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
2014-08-07 00:36:33 +05:30
|
|
|
|
2012-02-16 14:06:35 +05:30
|
|
|
specials = { \
|
|
|
|
'_row_': 0,
|
|
|
|
}
|
|
|
|
labels = []
|
|
|
|
positions = []
|
2014-10-24 02:53:10 +05:30
|
|
|
|
2012-02-16 14:06:35 +05:30
|
|
|
for position,label in enumerate(table.labels):
|
2015-08-08 00:33:26 +05:30
|
|
|
if (options.whitelist == None or any([ position in table.label_indexrange(needle) \
|
|
|
|
or fnmatch.fnmatch(label,needle) for needle in options.whitelist])) \
|
|
|
|
and (options.blacklist == None or not any([ position in table.label_indexrange(needle) \
|
|
|
|
or fnmatch.fnmatch(label,needle) for needle in options.blacklist])): # a label to keep?
|
2014-08-07 00:36:33 +05:30
|
|
|
labels.append(label) # remember name...
|
|
|
|
positions.append(position) # ...and position
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if len(labels) > 0 and options.whitelist != None and options.blacklist == None: # check whether reordering is possible
|
fixed sorting logic to respect numerical values of vector data (i.e. 1,2,3,….,10,11,… instead of 10,11,12,1,2,…)
2015-09-09 05:25:44 +05:30
|
|
|
whitelistitem = np.zeros(len(labels),dtype=int)
|
2015-05-29 04:01:32 +05:30
|
|
|
for i,label in enumerate(labels): # check each selected label
|
2015-08-08 00:33:26 +05:30
|
|
|
match = [ positions[i] in table.label_indexrange(needle) \
|
fixed sorting logic to respect numerical values of vector data (i.e. 1,2,3,….,10,11,… instead of 10,11,12,1,2,…)
2015-09-09 05:25:44 +05:30
|
|
|
or fnmatch.fnmatch(label,needle) for needle in options.whitelist] # which whitelist items do match it
|
|
|
|
whitelistitem[i] = match.index(True) if np.sum(match) == 1 else -1 # unique match to a whitelist item --> store which
|
2015-05-29 04:01:32 +05:30
|
|
|
|
fixed sorting logic to respect numerical values of vector data (i.e. 1,2,3,….,10,11,… instead of 10,11,12,1,2,…)
2015-09-09 05:25:44 +05:30
|
|
|
sorted = np.lexsort(sortingList(labels,whitelistitem))
|
2015-05-29 04:01:32 +05:30
|
|
|
order = range(len(labels)) if sorted[0] < 0 else sorted # skip reordering if non-unique, i.e. first sorted is "-1"
|
|
|
|
else:
|
|
|
|
order = range(len(labels)) # maintain original order of labels
|
2015-05-28 04:32:11 +05:30
|
|
|
|
2012-02-16 14:06:35 +05:30
|
|
|
interpolator = []
|
2014-10-24 12:06:01 +05:30
|
|
|
condition = options.condition # copy per file, might be altered
|
|
|
|
for position,operand in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',condition))): # find three groups
|
|
|
|
condition = condition.replace('#'+operand[0]+'#',
|
2014-10-24 02:53:10 +05:30
|
|
|
{ '': '{%i}'%position,
|
|
|
|
's#':'"{%i}"'%position}[operand[1]])
|
2014-08-07 00:36:33 +05:30
|
|
|
if operand[2] in specials: # special label ?
|
2012-12-03 19:59:21 +05:30
|
|
|
interpolator += ['specials["%s"]'%operand[2]]
|
2012-02-16 14:06:35 +05:30
|
|
|
else:
|
|
|
|
try:
|
2012-12-03 19:59:21 +05:30
|
|
|
interpolator += ['%s(table.data[%i])'%({ '':'float',
|
|
|
|
's#':'str'}[operand[1]],
|
|
|
|
table.labels.index(operand[2]))]
|
2012-02-16 14:06:35 +05:30
|
|
|
except:
|
2013-04-17 01:40:17 +05:30
|
|
|
parser.error('column %s not found...\n'%operand[2])
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-10-24 02:53:10 +05:30
|
|
|
evaluator = "'" + condition + "'.format(" + ','.join(interpolator) + ")"
|
2012-12-03 19:59:21 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-13 03:22:33 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) # read ASCII header info
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_clear()
|
|
|
|
table.labels_append(np.array(labels)[order]) # update with new label set
|
2012-02-16 14:06:35 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ process and output data ------------------------------------------
|
|
|
|
|
2015-05-29 04:01:32 +05:30
|
|
|
positions = np.array(positions)[order]
|
2012-02-17 00:17:07 +05:30
|
|
|
outputAlive = True
|
2014-08-07 00:36:33 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
specials['_row_'] += 1 # count row
|
2014-10-24 02:53:10 +05:30
|
|
|
if condition == '' or eval(eval(evaluator)): # valid row ?
|
2014-08-07 00:36:33 +05:30
|
|
|
table.data = [table.data[position] for position in positions] # retain filtered columns
|
|
|
|
outputAlive = table.data_write() # output processed line
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ finalize output -----------------------------------------
|
|
|
|
|
|
|
|
table.close() # close input ASCII table (works for stdin)
|