making table class feature complete
This commit is contained in:
parent
ee8e3386f4
commit
469d638afb
|
@ -24,35 +24,16 @@ parser.add_option('-i',
|
|||
dest = 'info', action = 'extend', metavar = '<string LIST>',
|
||||
help = 'items to add')
|
||||
|
||||
|
||||
(options,filenames) = parser.parse_args()
|
||||
if filenames == []: filenames = [None]
|
||||
|
||||
if options.info is None:
|
||||
parser.error('no info specified.')
|
||||
|
||||
# --- loop over input files ------------------------------------------------------------------------
|
||||
|
||||
if filenames == []: filenames = [None]
|
||||
|
||||
for name in filenames:
|
||||
try: table = damask.ASCIItable(name = name,
|
||||
buffered = False)
|
||||
except: continue
|
||||
damask.util.report(scriptName,name)
|
||||
damask.util.report(scriptName,name)
|
||||
|
||||
# ------------------------------------------ assemble header ---------------------------------------
|
||||
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
||||
table.comments += options.info
|
||||
|
||||
table.head_read()
|
||||
table.info_append(options.info)
|
||||
table.head_write()
|
||||
|
||||
# ------------------------------------------ pass through data -------------------------------------
|
||||
|
||||
outputAlive = True
|
||||
|
||||
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||
outputAlive = table.data_write() # output processed line
|
||||
|
||||
# ------------------------------------------ output finalization -----------------------------------
|
||||
|
||||
table.close() # close ASCII tables
|
||||
table.to_ASCII(sys.stdout if name is None else name)
|
||||
|
|
|
@ -35,62 +35,18 @@ parser.set_defaults(label = [],
|
|||
)
|
||||
|
||||
(options,filenames) = parser.parse_args()
|
||||
|
||||
pattern = [re.compile('^()(.+)$'), # label pattern for scalar
|
||||
re.compile('^(\d+_)?(.+)$'), # label pattern for multidimension
|
||||
]
|
||||
|
||||
# --- loop over input files -------------------------------------------------------------------------
|
||||
|
||||
if filenames == []: filenames = [None]
|
||||
|
||||
if len(options.label) != len(options.substitute):
|
||||
parser.error('number of column labels and substitutes do not match.')
|
||||
|
||||
for name in filenames:
|
||||
try: table = damask.ASCIItable(name = name,
|
||||
buffered = False)
|
||||
except: continue
|
||||
damask.util.report(scriptName,name)
|
||||
damask.util.report(scriptName,name)
|
||||
|
||||
# ------------------------------------------ read header ------------------------------------------
|
||||
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
||||
for i,label in enumerate(options.label):
|
||||
table.rename(label,
|
||||
options.substitute[i],
|
||||
scriptID+' '+' '.join(sys.argv[1:]))
|
||||
|
||||
table.head_read()
|
||||
|
||||
# ------------------------------------------ process labels ---------------------------------------
|
||||
|
||||
errors = []
|
||||
remarks = []
|
||||
|
||||
if len(options.label) == 0:
|
||||
errors.append('no labels specified.')
|
||||
elif len(options.label) != len(options.substitute):
|
||||
errors.append('mismatch between number of labels ({}) and substitutes ({}).'.format(len(options.label),
|
||||
len(options.substitute)))
|
||||
else:
|
||||
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]))
|
||||
else:
|
||||
m = pattern[int(dimensions[i]>1)].match(table.tags[index]) # isolate label name
|
||||
for j in range(dimensions[i]):
|
||||
table.tags[index+j] = table.tags[index+j].replace(m.group(2),options.substitute[i]) # replace name with substitute
|
||||
|
||||
if remarks != []: damask.util.croak(remarks)
|
||||
if errors != []:
|
||||
damask.util.croak(errors)
|
||||
table.close(dismiss = True)
|
||||
continue
|
||||
|
||||
# ------------------------------------------ assemble header ---------------------------------------
|
||||
|
||||
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
||||
table.head_write()
|
||||
|
||||
# ------------------------------------------ process data ------------------------------------------
|
||||
|
||||
outputAlive = True
|
||||
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||
outputAlive = table.data_write() # output processed line
|
||||
|
||||
# ------------------------------------------ output finalization -----------------------------------
|
||||
|
||||
table.close() # close ASCII tables
|
||||
table.to_ASCII(sys.stdout if name is None else name)
|
||||
|
|
|
@ -161,6 +161,40 @@ class Table():
|
|||
columns=[label for l in range(size)])
|
||||
self.data = pd.concat([self.data,new_data],axis=1)
|
||||
|
||||
def delete(self,label):
|
||||
"""
|
||||
Delete column data.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
label : str
|
||||
Column label.
|
||||
|
||||
"""
|
||||
self.data.drop(columns=label,inplace=True)
|
||||
|
||||
del self.shapes[label]
|
||||
|
||||
def rename(self,label_old,label_new,info=None):
|
||||
"""
|
||||
Rename column data.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
label_old : str
|
||||
Old column label.
|
||||
label_new : str
|
||||
New column label.
|
||||
|
||||
"""
|
||||
self.data.rename(columns={label_old:label_new},inplace=True)
|
||||
|
||||
comments = '{} => {}'.format(label_old,label_new)
|
||||
comments += ': {}'.format(info) if info is not None else ''
|
||||
self.comments.append(comments)
|
||||
|
||||
self.shapes[label_new] = self.shapes.pop(label_old)
|
||||
|
||||
|
||||
def to_ASCII(self,fname):
|
||||
"""
|
||||
|
|
|
@ -57,6 +57,22 @@ class TestTable:
|
|||
default.add('nine',d,'random data')
|
||||
assert np.allclose(d,default.get('nine'))
|
||||
|
||||
def test_rename_equivalent(self,default):
|
||||
v = default.get('v')
|
||||
default.rename('v','u')
|
||||
u = default.get('u')
|
||||
assert np.all(v == u)
|
||||
|
||||
def test_rename_gone(self,default):
|
||||
default.rename('v','V')
|
||||
with pytest.raises(KeyError):
|
||||
default.get('v')
|
||||
|
||||
def test_delete(self,default):
|
||||
default.delete('v')
|
||||
with pytest.raises(KeyError):
|
||||
default.get('v')
|
||||
|
||||
|
||||
def test_invalid_initialization(self,default):
|
||||
x = default.get('v')
|
||||
|
|
Loading…
Reference in New Issue