2020-03-19 04:13:56 +05:30
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
import logging
|
|
|
|
import logging.config
|
2019-11-22 00:17:27 +05:30
|
|
|
from collections.abc import Iterable
|
2012-02-16 02:24:14 +05:30
|
|
|
from optparse import OptionParser
|
2011-12-15 20:23:10 +05:30
|
|
|
|
2020-03-19 04:13:56 +05:30
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import damask
|
|
|
|
|
|
|
|
class Test:
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
|
|
|
General class for testing.
|
|
|
|
|
|
|
|
Is sub-classed by the individual tests.
|
|
|
|
"""
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
variants = []
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2016-09-05 04:16:53 +05:30
|
|
|
def __init__(self, **kwargs):
|
2019-11-22 17:46:08 +05:30
|
|
|
"""New test."""
|
2016-09-05 04:16:53 +05:30
|
|
|
defaults = {'description': '',
|
2016-09-11 00:14:46 +05:30
|
|
|
'keep': False,
|
|
|
|
'accept': False,
|
|
|
|
'updateRequest': False,
|
2016-09-20 10:40:07 +05:30
|
|
|
'show': False,
|
|
|
|
'select': None,
|
2016-09-05 04:16:53 +05:30
|
|
|
}
|
|
|
|
for arg in defaults.keys():
|
|
|
|
setattr(self,arg,kwargs.get(arg) if kwargs.get(arg) else defaults[arg])
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2016-09-11 00:14:46 +05:30
|
|
|
fh = logging.FileHandler('test.log') # create file handler which logs even debug messages
|
2014-06-04 21:04:35 +05:30
|
|
|
fh.setLevel(logging.DEBUG)
|
2016-09-02 01:07:49 +05:30
|
|
|
fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s: \n%(message)s'))
|
|
|
|
|
2016-09-11 00:14:46 +05:30
|
|
|
ch = logging.StreamHandler(stream=sys.stdout) # create console handler with a higher log level
|
2014-06-04 21:04:35 +05:30
|
|
|
ch.setLevel(logging.INFO)
|
2016-09-02 01:07:49 +05:30
|
|
|
ch.setFormatter(logging.Formatter('%(message)s'))
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
2014-06-04 21:04:35 +05:30
|
|
|
logger.addHandler(fh)
|
|
|
|
logger.addHandler(ch)
|
2016-09-02 01:07:49 +05:30
|
|
|
logger.setLevel(0)
|
2014-06-04 21:04:35 +05:30
|
|
|
|
2016-09-02 01:07:49 +05:30
|
|
|
logging.info('\n'.join(['+'*40,
|
|
|
|
'-'*40,
|
2016-09-05 04:16:53 +05:30
|
|
|
'| '+self.description,
|
2016-09-02 01:07:49 +05:30
|
|
|
'-'*40,
|
|
|
|
]))
|
2014-06-04 21:04:35 +05:30
|
|
|
|
2014-06-03 16:00:51 +05:30
|
|
|
self.dirBase = os.path.dirname(os.path.realpath(sys.modules[self.__class__.__module__].__file__))
|
2014-06-11 23:16:26 +05:30
|
|
|
|
2018-01-29 07:24:25 +05:30
|
|
|
self.parser = OptionParser(option_class=damask.extendableOption,
|
2020-06-25 01:04:51 +05:30
|
|
|
description = f'{self.description} (Test class version: {damask.version})',
|
2016-09-02 01:07:49 +05:30
|
|
|
usage = './test.py [options]')
|
2016-09-05 04:16:53 +05:30
|
|
|
self.parser.add_option("-k", "--keep",
|
2016-09-02 01:07:49 +05:30
|
|
|
action = "store_true",
|
2016-09-05 04:16:53 +05:30
|
|
|
dest = "keep",
|
|
|
|
help = "keep current results, just run postprocessing")
|
|
|
|
self.parser.add_option("--ok", "--accept",
|
2016-09-02 01:07:49 +05:30
|
|
|
action = "store_true",
|
|
|
|
dest = "accept",
|
2016-12-01 04:48:03 +05:30
|
|
|
help = "calculate results but always consider test as successful")
|
2016-09-20 10:40:07 +05:30
|
|
|
self.parser.add_option("-l", "--list",
|
|
|
|
action = "store_true",
|
|
|
|
dest = "show",
|
2016-12-01 04:48:03 +05:30
|
|
|
help = "show all test variants without actual calculation")
|
2016-09-20 10:40:07 +05:30
|
|
|
self.parser.add_option("-s", "--select",
|
|
|
|
dest = "select",
|
2018-01-29 07:24:25 +05:30
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = "run test(s) of given name only")
|
2016-09-05 04:16:53 +05:30
|
|
|
self.parser.set_defaults(keep = self.keep,
|
|
|
|
accept = self.accept,
|
2016-09-11 00:14:46 +05:30
|
|
|
update = self.updateRequest,
|
2016-09-20 10:40:07 +05:30
|
|
|
show = self.show,
|
|
|
|
select = self.select,
|
2016-09-02 01:07:49 +05:30
|
|
|
)
|
2014-06-11 23:16:26 +05:30
|
|
|
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2017-04-30 22:45:53 +05:30
|
|
|
def variantName(self,variant):
|
|
|
|
"""Generate name of (numerical) variant."""
|
|
|
|
return str(variant)
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
def execute(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Run all variants and report first failure."""
|
2016-09-05 04:16:53 +05:30
|
|
|
if not self.options.keep:
|
2016-09-02 01:07:49 +05:30
|
|
|
if not self.feasible(): return -1
|
2015-12-15 20:00:17 +05:30
|
|
|
self.clean()
|
|
|
|
self.prepareAll()
|
2016-09-02 01:07:49 +05:30
|
|
|
|
2017-05-01 04:37:49 +05:30
|
|
|
for variant,object in enumerate(self.variants):
|
|
|
|
name = self.variantName(variant)
|
2016-09-20 10:40:07 +05:30
|
|
|
if self.options.show:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'{variant+1}: {name}')
|
2016-12-01 04:48:03 +05:30
|
|
|
elif self.options.select is not None \
|
2018-01-29 07:24:25 +05:30
|
|
|
and not (name in self.options.select or str(variant+1) in self.options.select):
|
2016-09-20 10:40:07 +05:30
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if not self.options.keep:
|
|
|
|
self.prepare(variant)
|
|
|
|
self.run(variant)
|
|
|
|
|
|
|
|
self.postprocess(variant)
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2016-09-20 10:40:07 +05:30
|
|
|
if self.options.update:
|
2020-06-25 01:04:51 +05:30
|
|
|
if self.update(variant) != 0: logging.critical(f'update for "{name}" failed.')
|
2016-09-20 10:40:07 +05:30
|
|
|
elif not (self.options.accept or self.compare(variant)): # no update, do comparison
|
|
|
|
return variant+1 # return culprit
|
|
|
|
|
2016-12-01 04:48:03 +05:30
|
|
|
except Exception as e:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'exception during variant execution: "{e}"')
|
2018-01-30 07:57:05 +05:30
|
|
|
return variant+1 # return culprit
|
2016-09-05 04:16:53 +05:30
|
|
|
return 0
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2016-09-02 01:07:49 +05:30
|
|
|
def feasible(self):
|
|
|
|
"""Check whether test is possible or not (e.g. no license available)."""
|
2013-03-05 01:09:13 +05:30
|
|
|
return True
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def clean(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Delete directory tree containing current results."""
|
2012-01-18 15:04:49 +05:30
|
|
|
try:
|
|
|
|
shutil.rmtree(self.dirCurrent())
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.warning(f'removal of directory "{self.dirCurrent()}" not possible...')
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(self.dirCurrent())
|
2016-09-05 04:16:53 +05:30
|
|
|
return True
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileExistsError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'creation of directory "{self.dirCurrent()}" failed.')
|
2016-09-05 04:16:53 +05:30
|
|
|
return False
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2012-12-12 22:40:04 +05:30
|
|
|
def prepareAll(self):
|
2019-11-22 17:46:08 +05:30
|
|
|
"""Do all necessary preparations for the whole test."""
|
2012-12-12 22:40:04 +05:30
|
|
|
return True
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
def prepare(self,variant):
|
2019-11-22 17:46:08 +05:30
|
|
|
"""Do all necessary preparations for the run of each test variant."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
def run(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Execute the requested test variant."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def postprocess(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Perform post-processing of generated results for this test variant."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def compare(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Compare reference to current results."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def update(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Update reference with current results."""
|
2016-09-05 04:16:53 +05:30
|
|
|
logging.critical('update not supported.')
|
2016-09-11 00:14:46 +05:30
|
|
|
return 1
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
|
|
|
|
def dirReference(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Directory containing reference results of the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.normpath(os.path.join(self.dirBase,'reference/'))
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def dirCurrent(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Directory containing current results of the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.normpath(os.path.join(self.dirBase,'current/'))
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-12-12 22:40:04 +05:30
|
|
|
def dirProof(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Directory containing human readable proof of correctness for the test."""
|
2012-12-12 22:40:04 +05:30
|
|
|
return os.path.normpath(os.path.join(self.dirBase,'proof/'))
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
def fileInRoot(self,dir,file):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Path to a file in the root directory of DAMASK."""
|
2021-01-13 23:13:20 +05:30
|
|
|
return str(damask.environment.options['DAMASK_ROOT']/dir/file)
|
2015-12-04 03:22:03 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def fileInReference(self,file):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Path to a file in the refrence directory for the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.join(self.dirReference(),file)
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def fileInCurrent(self,file):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Path to a file in the current results directory for the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.join(self.dirCurrent(),file)
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-12-12 22:40:04 +05:30
|
|
|
def fileInProof(self,file):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Path to a file in the proof directory for the test."""
|
2012-12-12 22:40:04 +05:30
|
|
|
return os.path.join(self.dirProof(),file)
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
def copy(self, mapA, mapB,
|
|
|
|
A = [], B = []):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2016-10-25 00:46:29 +05:30
|
|
|
Copy list of files from (mapped) source to target.
|
2016-03-04 22:23:55 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
mapA/B is one of self.fileInX.
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2015-12-04 03:22:03 +05:30
|
|
|
if not B or len(B) == 0: B = A
|
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
for source,target in zip(list(map(mapA,A)),list(map(mapB,B))):
|
2015-12-04 03:22:03 +05:30
|
|
|
try:
|
2016-09-03 00:00:08 +05:30
|
|
|
shutil.copy2(source,target)
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'error copying {source} to {target}')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2015-12-04 03:22:03 +05:30
|
|
|
|
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def copy_Reference2Current(self,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
if len(targetfiles) == 0: targetfiles = sourcefiles
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2012-02-23 23:14:09 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Reference2Current: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2013-03-26 17:36:19 +05:30
|
|
|
def copy_Base2Current(self,sourceDir,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
source = os.path.normpath(os.path.join(self.dirBase,'../../..',sourceDir))
|
2013-03-26 17:36:19 +05:30
|
|
|
if len(targetfiles) == 0: targetfiles = sourcefiles
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2013-03-26 17:36:19 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(os.path.join(source,f),self.fileInCurrent(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2017-03-05 23:34:06 +05:30
|
|
|
logging.error(os.path.join(source,f))
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Base2Current: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2013-03-26 17:36:19 +05:30
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def copy_Current2Reference(self,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
if len(targetfiles) == 0: targetfiles = sourcefiles
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2012-07-17 18:34:57 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(self.fileInCurrent(f),self.fileInReference(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Current2Reference: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-12-12 22:40:04 +05:30
|
|
|
def copy_Proof2Current(self,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-12-12 22:40:04 +05:30
|
|
|
if len(targetfiles) == 0: targetfiles = sourcefiles
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2012-12-12 22:40:04 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(self.fileInProof(f),self.fileInCurrent(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Proof2Current: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def copy_Current2Current(self,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2012-02-23 23:14:09 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Current2Current: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2019-06-07 21:21:27 +05:30
|
|
|
def execute_inCurrentDir(self,cmd,streamIn=None,env=None):
|
2014-10-03 02:57:03 +05:30
|
|
|
|
2014-06-04 21:04:35 +05:30
|
|
|
logging.info(cmd)
|
2015-10-09 11:21:58 +05:30
|
|
|
out,error = damask.util.execute(cmd,streamIn,self.dirCurrent())
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2014-10-03 02:57:03 +05:30
|
|
|
logging.info(error)
|
|
|
|
logging.debug(out)
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2014-10-03 02:57:03 +05:30
|
|
|
return out,error
|
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def compare_Array(self,File1,File2):
|
2015-08-13 00:02:28 +05:30
|
|
|
|
|
|
|
import numpy as np
|
2016-03-21 19:48:58 +05:30
|
|
|
logging.info('\n '.join(['comparing',File1,File2]))
|
2020-09-21 00:15:06 +05:30
|
|
|
table = damask.Table.load(File1)
|
2020-09-15 11:17:26 +05:30
|
|
|
len1 = len(table.comments)+2
|
2020-09-21 00:15:06 +05:30
|
|
|
table = damask.Table.load(File2)
|
2020-09-15 11:17:26 +05:30
|
|
|
len2 = len(table.comments)+2
|
2015-08-12 19:13:05 +05:30
|
|
|
|
2015-08-13 00:02:28 +05:30
|
|
|
refArray = np.nan_to_num(np.genfromtxt(File1,missing_values='n/a',skip_header = len1,autostrip=True))
|
|
|
|
curArray = np.nan_to_num(np.genfromtxt(File2,missing_values='n/a',skip_header = len2,autostrip=True))
|
2014-10-09 21:07:35 +05:30
|
|
|
|
|
|
|
if len(curArray) == len(refArray):
|
2012-11-21 21:07:59 +05:30
|
|
|
refArrayNonZero = refArray[refArray.nonzero()]
|
2020-09-15 10:28:06 +05:30
|
|
|
curArray = curArray[refArray.nonzero()]
|
|
|
|
max_err = np. max(abs(refArrayNonZero[curArray.nonzero()]/curArray[curArray.nonzero()]-1.))
|
|
|
|
max_loc = np.argmax(abs(refArrayNonZero[curArray.nonzero()]/curArray[curArray.nonzero()]-1.))
|
2012-11-21 21:07:59 +05:30
|
|
|
refArrayNonZero = refArrayNonZero[curArray.nonzero()]
|
2020-09-15 10:28:06 +05:30
|
|
|
curArray = curArray[curArray.nonzero()]
|
2020-06-25 01:04:51 +05:30
|
|
|
print(f' ********\n * maximum relative error {max_err} between {refArrayNonZero[max_loc]} and {curArray[max_loc]}\n ********')
|
2012-10-25 13:25:27 +05:30
|
|
|
return max_err
|
|
|
|
else:
|
2020-09-15 10:28:06 +05:30
|
|
|
raise Exception(f'mismatch in array sizes ({len(refArray)} and {len(curArray)}) to compare')
|
2012-11-23 20:46:51 +05:30
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def compare_ArrayRefCur(self,ref,cur=''):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
if cur == '': cur = ref
|
2012-07-17 18:34:57 +05:30
|
|
|
refName = self.fileInReference(ref)
|
|
|
|
curName = self.fileInCurrent(cur)
|
|
|
|
return self.compare_Array(refName,curName)
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2018-01-31 22:39:14 +05:30
|
|
|
def compare_Table(self,headings0,file0,
|
|
|
|
headings1,file1,
|
|
|
|
normHeadings='',normType=None,
|
|
|
|
absoluteTolerance=False,perLine=False,skipLines=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2015-08-13 00:02:28 +05:30
|
|
|
import numpy as np
|
2016-03-21 19:48:58 +05:30
|
|
|
logging.info('\n '.join(['comparing ASCII Tables',file0,file1]))
|
2012-12-12 22:40:04 +05:30
|
|
|
if normHeadings == '': normHeadings = headings0
|
2012-11-22 23:31:51 +05:30
|
|
|
|
2020-09-15 11:17:26 +05:30
|
|
|
# check if comparison is possible and determine length of columns
|
2016-09-03 00:00:08 +05:30
|
|
|
if len(headings0) == len(headings1) == len(normHeadings):
|
2012-11-22 23:31:51 +05:30
|
|
|
dataLength = len(headings0)
|
2016-09-11 22:33:32 +05:30
|
|
|
length = [1 for i in range(dataLength)]
|
|
|
|
shape = [[] for i in range(dataLength)]
|
|
|
|
data = [[] for i in range(dataLength)]
|
|
|
|
maxError = [0.0 for i in range(dataLength)]
|
|
|
|
absTol = [absoluteTolerance for i in range(dataLength)]
|
2018-01-31 22:39:14 +05:30
|
|
|
column = [[1 for i in range(dataLength)] for j in range(2)]
|
2016-09-11 22:44:52 +05:30
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
norm = [[] for i in range(dataLength)]
|
|
|
|
normLength = [1 for i in range(dataLength)]
|
|
|
|
normShape = [[] for i in range(dataLength)]
|
|
|
|
normColumn = [1 for i in range(dataLength)]
|
2012-12-12 22:40:04 +05:30
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(dataLength):
|
2016-09-03 00:00:08 +05:30
|
|
|
if headings0[i]['shape'] != headings1[i]['shape']:
|
2020-06-25 01:04:51 +05:30
|
|
|
raise Exception(f"shape mismatch between {headings0[i]['label']} and {headings1[i]['label']}")
|
2012-11-23 20:46:51 +05:30
|
|
|
shape[i] = headings0[i]['shape']
|
2016-09-11 22:33:32 +05:30
|
|
|
for j in range(np.shape(shape[i])[0]):
|
2012-11-23 20:46:51 +05:30
|
|
|
length[i] *= shape[i][j]
|
2012-12-12 22:40:04 +05:30
|
|
|
normShape[i] = normHeadings[i]['shape']
|
2016-09-11 22:33:32 +05:30
|
|
|
for j in range(np.shape(normShape[i])[0]):
|
2012-12-12 22:40:04 +05:30
|
|
|
normLength[i] *= normShape[i][j]
|
2012-11-22 23:31:51 +05:30
|
|
|
else:
|
2020-06-25 01:04:51 +05:30
|
|
|
raise Exception(f'trying to compare {len(headings0)} with {len(headings1)} normed by {len(normHeadings)} data sets')
|
2012-01-27 15:25:19 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
table0 = damask.ASCIItable(name=file0,readonly=True)
|
2012-11-22 23:31:51 +05:30
|
|
|
table0.head_read()
|
2015-12-04 03:22:03 +05:30
|
|
|
table1 = damask.ASCIItable(name=file1,readonly=True)
|
2016-09-03 00:00:08 +05:30
|
|
|
table1.head_read()
|
2012-11-21 21:07:59 +05:30
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(dataLength):
|
2016-03-21 19:48:58 +05:30
|
|
|
key0 = ('1_' if length[i]>1 else '') + headings0[i]['label']
|
|
|
|
key1 = ('1_' if length[i]>1 else '') + headings1[i]['label']
|
|
|
|
normKey = ('1_' if normLength[i]>1 else '') + normHeadings[i]['label']
|
2016-05-17 20:22:21 +05:30
|
|
|
if key0 not in table0.labels(raw = True):
|
2020-06-25 01:04:51 +05:30
|
|
|
raise Exception(f'column "{key0}" not found in first table...')
|
2016-05-17 20:22:21 +05:30
|
|
|
elif key1 not in table1.labels(raw = True):
|
2020-06-25 01:04:51 +05:30
|
|
|
raise Exception(f'column "{key1}" not found in second table...')
|
2016-05-17 20:22:21 +05:30
|
|
|
elif normKey not in table0.labels(raw = True):
|
2020-06-25 01:04:51 +05:30
|
|
|
raise Exception(f'column "{normKey}" not found in first table...')
|
2012-11-22 23:31:51 +05:30
|
|
|
else:
|
2016-05-17 20:22:21 +05:30
|
|
|
column[0][i] = table0.label_index(key0)
|
|
|
|
column[1][i] = table1.label_index(key1)
|
|
|
|
normColumn[i] = table0.label_index(normKey)
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-11-22 23:31:51 +05:30
|
|
|
line0 = 0
|
2016-03-21 19:48:58 +05:30
|
|
|
while table0.data_read(): # read next data line of ASCII table
|
2012-12-12 22:40:04 +05:30
|
|
|
if line0 not in skipLines:
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(dataLength):
|
2018-11-15 18:12:19 +05:30
|
|
|
myData = np.array(list(map(float,table0.data[column[0][i]:\
|
|
|
|
column[0][i]+length[i]])),'d')
|
|
|
|
normData = np.array(list(map(float,table0.data[normColumn[i]:\
|
|
|
|
normColumn[i]+normLength[i]])),'d')
|
2015-08-13 00:02:28 +05:30
|
|
|
data[i] = np.append(data[i],np.reshape(myData,shape[i]))
|
2012-12-12 22:40:04 +05:30
|
|
|
if normType == 'pInf':
|
2015-08-13 00:02:28 +05:30
|
|
|
norm[i] = np.append(norm[i],np.max(np.abs(normData)))
|
2012-12-12 22:40:04 +05:30
|
|
|
else:
|
2015-08-13 00:02:28 +05:30
|
|
|
norm[i] = np.append(norm[i],np.linalg.norm(np.reshape(normData,normShape[i]),normType))
|
2016-03-21 19:48:58 +05:30
|
|
|
line0 += 1
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(dataLength):
|
2016-10-25 00:46:29 +05:30
|
|
|
if not perLine: norm[i] = [np.max(norm[i]) for j in range(line0-len(skipLines))]
|
2015-08-13 00:02:28 +05:30
|
|
|
data[i] = np.reshape(data[i],[line0-len(skipLines),length[i]])
|
2012-12-12 22:40:04 +05:30
|
|
|
if any(norm[i]) == 0.0 or absTol[i]:
|
2016-09-11 22:33:32 +05:30
|
|
|
norm[i] = [1.0 for j in range(line0-len(skipLines))]
|
2012-12-12 22:40:04 +05:30
|
|
|
absTol[i] = True
|
2020-09-15 10:28:06 +05:30
|
|
|
logging.warning(f'''{"At least one" if perLine else "Maximum"} norm of
|
|
|
|
"{headings0[i]['label']}" in first table is 0.0, using absolute tolerance''')
|
2012-12-12 22:40:04 +05:30
|
|
|
|
2012-11-22 23:31:51 +05:30
|
|
|
line1 = 0
|
2016-03-21 19:48:58 +05:30
|
|
|
while table1.data_read(): # read next data line of ASCII table
|
2012-12-12 22:40:04 +05:30
|
|
|
if line1 not in skipLines:
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(dataLength):
|
2018-11-15 18:12:19 +05:30
|
|
|
myData = np.array(list(map(float,table1.data[column[1][i]:\
|
|
|
|
column[1][i]+length[i]])),'d')
|
2015-08-13 00:02:28 +05:30
|
|
|
maxError[i] = max(maxError[i],np.linalg.norm(np.reshape(myData-data[i][line1-len(skipLines),:],shape[i]))/
|
2013-09-20 19:52:37 +05:30
|
|
|
norm[i][line1-len(skipLines)])
|
2012-11-23 22:51:18 +05:30
|
|
|
line1 +=1
|
2012-11-23 20:46:51 +05:30
|
|
|
|
2020-06-25 01:04:51 +05:30
|
|
|
if (line0 != line1): raise Exception(f'found {line0} lines in first table but {line1} in second table')
|
2012-11-23 22:51:18 +05:30
|
|
|
|
2014-06-04 21:04:35 +05:30
|
|
|
logging.info(' ********')
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(dataLength):
|
2020-09-15 10:28:06 +05:30
|
|
|
logging.info(f''' * maximum {'absolute' if absTol[i] else 'relative'} error {maxError[i]}
|
|
|
|
between {headings0[i]['label']} and {headings1[i]['label']}''')
|
2014-06-04 21:04:35 +05:30
|
|
|
logging.info(' ********')
|
2012-11-22 23:31:51 +05:30
|
|
|
return maxError
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2015-10-22 11:29:11 +05:30
|
|
|
|
2015-12-15 20:00:17 +05:30
|
|
|
def compare_TablesStatistically(self,
|
2020-09-15 10:28:06 +05:30
|
|
|
files = [None,None], # list of file names
|
|
|
|
columns = [None], # list of list of column labels (per file)
|
|
|
|
meanTol = 1.0e-4,
|
|
|
|
stdTol = 1.0e-6,
|
|
|
|
preFilter = 1.0e-9):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2019-11-22 17:46:08 +05:30
|
|
|
Calculate statistics of tables.
|
2015-12-15 20:00:17 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
threshold can be used to ignore small values (a negative number disables this feature)
|
|
|
|
"""
|
2016-03-21 19:48:58 +05:30
|
|
|
if not (isinstance(files, Iterable) and not isinstance(files, str)): # check whether list of files is requested
|
2015-12-15 20:00:17 +05:30
|
|
|
files = [str(files)]
|
|
|
|
|
2020-09-21 00:15:06 +05:30
|
|
|
tables = [damask.Table.load(filename) for filename in files]
|
2015-12-15 20:00:17 +05:30
|
|
|
for table in tables:
|
2020-09-15 10:28:06 +05:30
|
|
|
table._label_discrete()
|
2015-12-15 20:00:17 +05:30
|
|
|
|
2016-03-21 19:48:58 +05:30
|
|
|
columns += [columns[0]]*(len(files)-len(columns)) # extend to same length as files
|
|
|
|
columns = columns[:len(files)] # truncate to same length as files
|
2015-12-15 20:00:17 +05:30
|
|
|
|
|
|
|
for i,column in enumerate(columns):
|
2020-03-18 22:21:47 +05:30
|
|
|
if column is None: columns[i] = list(tables[i].data.columns) # if no column is given, read all
|
2015-12-15 20:00:17 +05:30
|
|
|
|
|
|
|
logging.info('comparing ASCIItables statistically')
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(len(columns)):
|
2015-12-15 20:00:17 +05:30
|
|
|
columns[i] = columns[0] if not columns[i] else \
|
|
|
|
([columns[i]] if not (isinstance(columns[i], Iterable) and not isinstance(columns[i], str)) else \
|
|
|
|
columns[i]
|
|
|
|
)
|
|
|
|
logging.info(files[i]+':'+','.join(columns[i]))
|
|
|
|
|
2016-03-21 19:48:58 +05:30
|
|
|
if len(files) < 2: return True # single table is always close to itself...
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2015-12-15 20:00:17 +05:30
|
|
|
data = []
|
|
|
|
for table,labels in zip(tables,columns):
|
2020-09-15 10:28:06 +05:30
|
|
|
table._label_uniform()
|
2020-03-18 22:21:47 +05:30
|
|
|
data.append(np.hstack(list(table.get(label) for label in labels)))
|
2016-09-03 00:00:08 +05:30
|
|
|
|
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(1,len(data)):
|
2015-12-15 20:00:17 +05:30
|
|
|
delta = data[i]-data[i-1]
|
|
|
|
normBy = (np.abs(data[i]) + np.abs(data[i-1]))*0.5
|
|
|
|
normedDelta = np.where(normBy>preFilter,delta/normBy,0.0)
|
|
|
|
mean = np.amax(np.abs(np.mean(normedDelta,0)))
|
2020-09-15 10:28:06 +05:30
|
|
|
std = np.amax(np.std(normedDelta,0))
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.info(f'mean: {mean:f}')
|
|
|
|
logging.info(f'std: {std:f}')
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
return (mean < meanTol) & (std < stdTol)
|
2015-12-15 20:00:17 +05:30
|
|
|
|
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
def compare_Tables(self,
|
2016-09-05 04:16:53 +05:30
|
|
|
files = [None,None], # list of file names
|
2016-03-21 19:48:58 +05:30
|
|
|
columns = [None], # list of list of column labels (per file)
|
2016-09-05 04:16:53 +05:30
|
|
|
rtol = 1e-5,
|
|
|
|
atol = 1e-8,
|
|
|
|
debug = False):
|
2019-11-22 17:46:08 +05:30
|
|
|
"""Compare multiple tables with np.allclose."""
|
2016-03-21 19:48:58 +05:30
|
|
|
if not (isinstance(files, Iterable) and not isinstance(files, str)): # check whether list of files is requested
|
2015-12-04 03:22:03 +05:30
|
|
|
files = [str(files)]
|
2015-10-22 11:29:11 +05:30
|
|
|
|
2016-09-05 04:16:53 +05:30
|
|
|
if len(files) < 2: return True # single table is always close to itself...
|
|
|
|
|
2020-09-21 00:15:06 +05:30
|
|
|
tables = [damask.Table.load(filename) for filename in files]
|
2015-10-30 03:21:57 +05:30
|
|
|
|
2016-03-21 19:48:58 +05:30
|
|
|
columns += [columns[0]]*(len(files)-len(columns)) # extend to same length as files
|
|
|
|
columns = columns[:len(files)] # truncate to same length as files
|
2015-12-04 03:22:03 +05:30
|
|
|
|
2015-12-05 04:10:39 +05:30
|
|
|
for i,column in enumerate(columns):
|
2020-03-19 04:13:56 +05:30
|
|
|
if column is None: columns[i] = list(tables[i].shapes.keys()) # if no column is given, use all
|
2015-12-05 04:10:39 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
logging.info('comparing ASCIItables')
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(len(columns)):
|
2015-12-04 03:22:03 +05:30
|
|
|
columns[i] = columns[0] if not columns[i] else \
|
|
|
|
([columns[i]] if not (isinstance(columns[i], Iterable) and not isinstance(columns[i], str)) else \
|
|
|
|
columns[i]
|
|
|
|
)
|
2016-09-05 04:16:53 +05:30
|
|
|
logging.info(files[i]+': '+','.join(columns[i]))
|
|
|
|
|
2020-03-19 04:13:56 +05:30
|
|
|
dimensions = [np.prod(tables[0].shapes[c]) for c in columns[0]] # width of each requested column
|
2016-09-05 04:16:53 +05:30
|
|
|
maximum = np.zeros_like(columns[0],dtype=float) # one magnitude per column entry
|
|
|
|
data = [] # list of feature table extracted from each file (ASCII table)
|
|
|
|
|
|
|
|
for i,(table,labels) in enumerate(zip(tables,columns)):
|
2020-03-19 04:13:56 +05:30
|
|
|
if np.any(dimensions != [np.prod(table.shapes[c]) for c in labels]): # check data object consistency
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Table {files[i]} differs in data layout.')
|
2016-09-05 04:16:53 +05:30
|
|
|
return False
|
2020-03-19 17:57:23 +05:30
|
|
|
data.append(np.hstack(list(table.get(label) for label in labels)).astype(np.float)) # store
|
2016-09-05 04:16:53 +05:30
|
|
|
|
|
|
|
for j,label in enumerate(labels): # iterate over object labels
|
2016-12-01 04:48:03 +05:30
|
|
|
maximum[j] = np.maximum(
|
2016-09-05 04:16:53 +05:30
|
|
|
maximum[j],
|
2020-03-19 04:13:56 +05:30
|
|
|
np.amax(np.linalg.norm(table.get(label),
|
2016-09-05 04:16:53 +05:30
|
|
|
axis=1))
|
|
|
|
) # find maximum Euclidean norm across rows
|
|
|
|
|
|
|
|
maximum = np.where(maximum > 0.0, maximum, 1.0) # avoid div by zero for zero columns
|
|
|
|
maximum = np.repeat(maximum,dimensions) # spread maximum over columns of each object
|
2015-12-04 03:22:03 +05:30
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(len(data)):
|
2016-09-05 04:16:53 +05:30
|
|
|
data[i] /= maximum # normalize each table
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.info(f'shape of data {i}: {data[i].shape}')
|
2016-09-05 04:16:53 +05:30
|
|
|
|
|
|
|
if debug:
|
2016-12-01 04:48:03 +05:30
|
|
|
violators = np.absolute(data[0]-data[1]) > atol + rtol*np.absolute(data[1])
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.info(f'shape of violators: {violators.shape}')
|
2016-12-01 04:48:03 +05:30
|
|
|
for j,culprits in enumerate(violators):
|
|
|
|
goodguys = np.logical_not(culprits)
|
|
|
|
if culprits.any():
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.info(f'{j} has {np.sum(culprits)}')
|
|
|
|
logging.info(f'deviation: {np.absolute(data[0][j]-data[1][j])[culprits]}')
|
|
|
|
logging.info(f'data : {np.absolute(data[1][j])[culprits]}')
|
|
|
|
logging.info(f'deviation: {np.absolute(data[0][j]-data[1][j])[goodguys]}')
|
|
|
|
logging.info(f'data : {np.absolute(data[1][j])[goodguys]}')
|
2015-12-05 04:10:39 +05:30
|
|
|
|
2016-03-21 19:48:58 +05:30
|
|
|
allclose = True # start optimistic
|
2016-09-11 22:33:32 +05:30
|
|
|
for i in range(1,len(data)):
|
2016-09-05 04:16:53 +05:30
|
|
|
allclose &= np.allclose(data[i-1],data[i],rtol,atol) # accumulate "pessimism"
|
2015-10-22 11:29:11 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
return allclose
|
2015-10-22 11:29:11 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2018-01-31 22:39:14 +05:30
|
|
|
def compare_TableRefCur(self,headingsRef,ref,headingsCur='',cur='',
|
|
|
|
normHeadings='',normType=None,
|
|
|
|
absoluteTolerance=False,perLine=False,skipLines=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2018-01-31 22:39:14 +05:30
|
|
|
return self.compare_Table(headingsRef,
|
|
|
|
self.fileInReference(ref),
|
|
|
|
headingsRef if headingsCur == '' else headingsCur,
|
2018-01-31 23:05:50 +05:30
|
|
|
self.fileInCurrent(ref if cur == '' else cur),
|
2018-01-31 22:39:14 +05:30
|
|
|
normHeadings,normType,
|
|
|
|
absoluteTolerance,perLine,skipLines)
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2018-01-31 22:39:14 +05:30
|
|
|
def compare_TableCurCur(self,headingsCur0,Cur0,Cur1,
|
|
|
|
headingsCur1='',
|
|
|
|
normHeadings='',normType=None,
|
|
|
|
absoluteTolerance=False,perLine=False,skipLines=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2018-01-31 22:39:14 +05:30
|
|
|
return self.compare_Table(headingsCur0,
|
|
|
|
self.fileInCurrent(Cur0),
|
|
|
|
headingsCur0 if headingsCur1 == '' else headingsCur1,
|
|
|
|
self.fileInCurrent(Cur1),
|
|
|
|
normHeadings,normType,absoluteTolerance,perLine,skipLines)
|
2013-03-19 21:16:07 +05:30
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-01-31 18:40:14 +05:30
|
|
|
def report_Success(self,culprit):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
|
|
|
ret = culprit
|
|
|
|
|
2013-03-05 01:09:13 +05:30
|
|
|
if culprit == 0:
|
2018-01-29 07:24:25 +05:30
|
|
|
count = len(self.variants) if self.options.select is None else len(self.options.select)
|
2020-09-15 10:28:06 +05:30
|
|
|
msg = ('Test passed.' if count == 1 else f'All {count} tests passed.') + '\a\a\a'
|
2016-09-03 00:00:08 +05:30
|
|
|
elif culprit == -1:
|
2018-01-29 07:24:25 +05:30
|
|
|
msg = 'Warning: could not start test...'
|
2016-09-03 00:00:08 +05:30
|
|
|
ret = 0
|
2012-01-27 15:25:19 +05:30
|
|
|
else:
|
2020-06-25 01:04:51 +05:30
|
|
|
msg = f'Test "{self.variantName(culprit-1)}" failed.'
|
2016-09-03 00:00:08 +05:30
|
|
|
|
|
|
|
logging.critical('\n'.join(['*'*40,msg,'*'*40]) + '\n')
|
|
|
|
return ret
|