migrating to new table class

This commit is contained in:
Martin Diehl 2020-03-18 23:43:56 +01:00
parent cbe5944390
commit f7a6ac4a0e
1 changed files with 22 additions and 23 deletions

View File

@ -1,11 +1,16 @@
import os,sys,shutil import os
import logging,logging.config import sys
import damask import shutil
import numpy as np import logging
import logging.config
from collections.abc import Iterable from collections.abc import Iterable
from optparse import OptionParser from optparse import OptionParser
class Test(): import numpy as np
import damask
class Test:
""" """
General class for testing. General class for testing.
@ -13,7 +18,7 @@ class Test():
""" """
variants = [] variants = []
def __init__(self, **kwargs): def __init__(self, **kwargs):
"""New test.""" """New test."""
defaults = {'description': '', defaults = {'description': '',
@ -25,7 +30,7 @@ class Test():
} }
for arg in defaults.keys(): for arg in defaults.keys():
setattr(self,arg,kwargs.get(arg) if kwargs.get(arg) else defaults[arg]) setattr(self,arg,kwargs.get(arg) if kwargs.get(arg) else defaults[arg])
fh = logging.FileHandler('test.log') # create file handler which logs even debug messages fh = logging.FileHandler('test.log') # create file handler which logs even debug messages
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s: \n%(message)s')) fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s: \n%(message)s'))
@ -73,7 +78,7 @@ class Test():
select = self.select, select = self.select,
) )
def variantName(self,variant): def variantName(self,variant):
"""Generate name of (numerical) variant.""" """Generate name of (numerical) variant."""
return str(variant) return str(variant)
@ -99,7 +104,7 @@ class Test():
self.run(variant) self.run(variant)
self.postprocess(variant) self.postprocess(variant)
if self.options.update: if self.options.update:
if self.update(variant) != 0: logging.critical('update for "{}" failed.'.format(name)) if self.update(variant) != 0: logging.critical('update for "{}" failed.'.format(name))
elif not (self.options.accept or self.compare(variant)): # no update, do comparison elif not (self.options.accept or self.compare(variant)): # no update, do comparison
@ -109,7 +114,7 @@ class Test():
logging.critical('exception during variant execution: "{}"'.format(str(e))) logging.critical('exception during variant execution: "{}"'.format(str(e)))
return variant+1 # return culprit return variant+1 # return culprit
return 0 return 0
def feasible(self): def feasible(self):
"""Check whether test is possible or not (e.g. no license available).""" """Check whether test is possible or not (e.g. no license available)."""
return True return True
@ -127,7 +132,7 @@ class Test():
except FileExistsError: except FileExistsError:
logging.critical('creation of directory "{}" failed.'.format(self.dirCurrent())) logging.critical('creation of directory "{}" failed.'.format(self.dirCurrent()))
return False return False
def prepareAll(self): def prepareAll(self):
"""Do all necessary preparations for the whole test.""" """Do all necessary preparations for the whole test."""
return True return True
@ -494,15 +499,13 @@ class Test():
if len(files) < 2: return True # single table is always close to itself... if len(files) < 2: return True # single table is always close to itself...
tables = [damask.ASCIItable(name = filename,readonly = True) for filename in files] tables = [damask.Table.from_ASCII(filename) for filename in files]
for table in tables:
table.head_read()
columns += [columns[0]]*(len(files)-len(columns)) # extend to same length as files columns += [columns[0]]*(len(files)-len(columns)) # extend to same length as files
columns = columns[:len(files)] # truncate to same length as files columns = columns[:len(files)] # truncate to same length as files
for i,column in enumerate(columns): for i,column in enumerate(columns):
if column is None: columns[i] = tables[i].labels(raw = False) # if no column is given, use all if column is None: columns[i] = list(tables[i].shapes.keys()) # if no column is given, use all
logging.info('comparing ASCIItables') logging.info('comparing ASCIItables')
for i in range(len(columns)): for i in range(len(columns)):
@ -512,22 +515,20 @@ class Test():
) )
logging.info(files[i]+': '+','.join(columns[i])) logging.info(files[i]+': '+','.join(columns[i]))
dimensions = tables[0].label_dimension(columns[0]) # width of each requested column dimensions = [np.prod(tables[0].shapes[c]) for c in columns[0]] # width of each requested column
maximum = np.zeros_like(columns[0],dtype=float) # one magnitude per column entry maximum = np.zeros_like(columns[0],dtype=float) # one magnitude per column entry
data = [] # list of feature table extracted from each file (ASCII table) data = [] # list of feature table extracted from each file (ASCII table)
for i,(table,labels) in enumerate(zip(tables,columns)): for i,(table,labels) in enumerate(zip(tables,columns)):
if np.any(dimensions != table.label_dimension(labels)): # check data object consistency if np.any(dimensions != [np.prod(table.shapes[c]) for c in labels]): # check data object consistency
logging.critical('Table {} differs in data layout.'.format(files[i])) logging.critical('Table {} differs in data layout.'.format(files[i]))
return False return False
table.data_readArray(labels) # read data, ... data.append(np.hstack(list(table.get(label) for label in labels))) # store
data.append(table.data) # ... store, ...
table.close() # ... close
for j,label in enumerate(labels): # iterate over object labels for j,label in enumerate(labels): # iterate over object labels
maximum[j] = np.maximum( maximum[j] = np.maximum(
maximum[j], maximum[j],
np.amax(np.linalg.norm(table.data[:,table.label_indexrange(label)], np.amax(np.linalg.norm(table.get(label),
axis=1)) axis=1))
) # find maximum Euclidean norm across rows ) # find maximum Euclidean norm across rows
@ -549,8 +550,6 @@ class Test():
logging.info('data : {}'.format(np.absolute(data[1][j])[culprits])) logging.info('data : {}'.format(np.absolute(data[1][j])[culprits]))
logging.info('deviation: {}'.format(np.absolute(data[0][j]-data[1][j])[goodguys])) logging.info('deviation: {}'.format(np.absolute(data[0][j]-data[1][j])[goodguys]))
logging.info('data : {}'.format(np.absolute(data[1][j])[goodguys])) logging.info('data : {}'.format(np.absolute(data[1][j])[goodguys]))
# for ok,valA,valB in zip(allclose,data[0],data[1]):
# logging.debug('{}:\n{}\n{}'.format(ok,valA,valB))
allclose = True # start optimistic allclose = True # start optimistic
for i in range(1,len(data)): for i in range(1,len(data)):