DAMASK_EICMD/python/tests/conftest.py

157 lines
5.5 KiB
Python
Raw Normal View History

2020-06-28 03:07:46 +05:30
from pathlib import Path
import datetime
2020-11-15 16:30:26 +05:30
import os
2019-11-23 17:29:41 +05:30
import numpy as np
2019-11-23 17:29:41 +05:30
import pytest
2020-11-15 16:30:26 +05:30
import matplotlib as mpl
if os.name == 'posix' and 'DISPLAY' not in os.environ:
mpl.use('Agg')
import matplotlib.pyplot as plt
2019-11-23 17:29:41 +05:30
import damask
patched_version = '99.99.99-9999-pytest'
@pytest.fixture
2020-08-24 04:12:58 +05:30
def patch_damask_version(monkeypatch):
"""Set damask.version for reproducible tests results."""
2020-08-24 04:12:58 +05:30
monkeypatch.setattr(damask, 'version', patched_version)
patched_date = datetime.datetime(2019, 11, 2, 11, 58, 0)
@pytest.fixture
2020-08-24 04:12:58 +05:30
def patch_datetime_now(monkeypatch):
"""Set datetime.datetime.now for reproducible tests results."""
class mydatetime:
@classmethod
def now(cls):
return patched_date
2019-11-23 17:29:41 +05:30
2020-08-24 04:12:58 +05:30
monkeypatch.setattr(datetime, 'datetime', mydatetime)
@pytest.fixture
def patch_execution_stamp(monkeypatch):
"""Set damask.util.execution_stamp for reproducible tests results."""
def execution_stamp(class_name,function_name=None):
_function_name = '' if function_name is None else f'.{function_name}'
return f'damask.{class_name}{_function_name} v{patched_version} ({patched_date})'
monkeypatch.setattr(damask.util, 'execution_stamp', execution_stamp)
2020-11-15 16:30:26 +05:30
@pytest.fixture
def patch_plt_show(monkeypatch):
def _None(block=None):
pass
monkeypatch.setattr(plt, 'show', _None, raising=True)
2019-11-23 17:29:41 +05:30
def pytest_addoption(parser):
2021-11-16 03:35:44 +05:30
parser.addoption('--update', action='store_true', default=False,
help='Update reference results.')
2019-11-23 17:29:41 +05:30
2019-11-23 17:29:41 +05:30
@pytest.fixture
def update(request):
2019-11-27 17:49:58 +05:30
"""Store current results as new reference results."""
2019-11-23 17:29:41 +05:30
return request.config.getoption("--update")
2019-11-23 17:29:41 +05:30
@pytest.fixture
def res_path_base():
"""Directory containing testing resources."""
return Path(__file__).parent/'resources'
2020-06-30 21:05:52 +05:30
2020-06-30 21:05:52 +05:30
@pytest.fixture
2020-06-30 21:42:19 +05:30
def set_of_quaternions():
2020-06-30 21:05:52 +05:30
"""A set of n random rotations."""
2020-06-30 21:42:19 +05:30
def random_quaternions(N):
r = np.random.rand(N,3)
A = np.sqrt(r[:,2])
B = np.sqrt(1.0-r[:,2])
qu = np.column_stack([np.cos(2.0*np.pi*r[:,0])*A,
np.sin(2.0*np.pi*r[:,1])*B,
np.cos(2.0*np.pi*r[:,1])*B,
np.sin(2.0*np.pi*r[:,0])*A])
qu[:,0]*=np.sign(qu[:,0])
return qu
2020-11-24 00:36:34 +05:30
n = 600
2020-06-30 21:05:52 +05:30
scatter=1.e-2
specials = np.array([
[1.0, 0.0, 0.0, 0.0],
#----------------------
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
[0.0,-1.0, 0.0, 0.0],
[0.0, 0.0,-1.0, 0.0],
[0.0, 0.0, 0.0,-1.0],
#----------------------
[1.0, 1.0, 0.0, 0.0],
[1.0, 0.0, 1.0, 0.0],
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 1.0, 0.0],
[0.0, 1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
#----------------------
[1.0,-1.0, 0.0, 0.0],
[1.0, 0.0,-1.0, 0.0],
[1.0, 0.0, 0.0,-1.0],
[0.0, 1.0,-1.0, 0.0],
[0.0, 1.0, 0.0,-1.0],
[0.0, 0.0, 1.0,-1.0],
#----------------------
[0.0, 1.0,-1.0, 0.0],
[0.0, 1.0, 0.0,-1.0],
[0.0, 0.0, 1.0,-1.0],
#----------------------
[0.0,-1.0,-1.0, 0.0],
[0.0,-1.0, 0.0,-1.0],
[0.0, 0.0,-1.0,-1.0],
#----------------------
[1.0, 1.0, 1.0, 0.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0,-1.0, 1.0, 0.0],
[1.0,-1.0, 0.0, 1.0],
[1.0, 0.0,-1.0, 1.0],
[1.0, 1.0,-1.0, 0.0],
[1.0, 1.0, 0.0,-1.0],
[1.0, 0.0, 1.0,-1.0],
[1.0,-1.0,-1.0, 0.0],
[1.0,-1.0, 0.0,-1.0],
[1.0, 0.0,-1.0,-1.0],
#----------------------
[0.0, 1.0, 1.0, 1.0],
[0.0, 1.0,-1.0, 1.0],
[0.0, 1.0, 1.0,-1.0],
[0.0,-1.0, 1.0, 1.0],
[0.0,-1.0,-1.0, 1.0],
[0.0,-1.0, 1.0,-1.0],
[0.0,-1.0,-1.0,-1.0],
#----------------------
[1.0, 1.0, 1.0, 1.0],
[1.0,-1.0, 1.0, 1.0],
[1.0, 1.0,-1.0, 1.0],
[1.0, 1.0, 1.0,-1.0],
[1.0,-1.0,-1.0, 1.0],
[1.0,-1.0, 1.0,-1.0],
[1.0, 1.0,-1.0,-1.0],
[1.0,-1.0,-1.0,-1.0],
])
specials /= np.linalg.norm(specials,axis=1).reshape(-1,1)
2020-07-25 02:11:41 +05:30
specials_scatter = specials + np.broadcast_to((np.random.rand(4)*2.-1.)*scatter,specials.shape)
2020-06-30 21:05:52 +05:30
specials_scatter /= np.linalg.norm(specials_scatter,axis=1).reshape(-1,1)
specials_scatter[specials_scatter[:,0]<0]*=-1
2023-12-09 22:33:24 +05:30
return np.vstack((specials,
specials_scatter,
random_quaternions(n-2*len(specials)),
))