From 72ea9a5cec90e9388e67eaf8bdd3229c201fc505 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 31 Jul 2020 17:04:14 +0200 Subject: [PATCH] non-vectorized formulas can be implemented in a user function --- python/tests/test_Geom.py | 10 +++++----- python/tests/test_Orientation.py | 2 +- python/tests/test_Result.py | 11 +++++++---- python/tests/test_Rotation.py | 2 +- python/tests/test_Table.py | 25 +++++++++++++++++-------- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/python/tests/test_Geom.py b/python/tests/test_Geom.py index 28c9d14b0..480c74788 100644 --- a/python/tests/test_Geom.py +++ b/python/tests/test_Geom.py @@ -26,7 +26,7 @@ def default(): @pytest.fixture def reference_dir(reference_dir_base): """Directory containing reference results.""" - return os.path.join(reference_dir_base,'Geom') + return reference_dir_base/'Geom' class TestGeom: @@ -87,7 +87,7 @@ class TestGeom: modified = copy.deepcopy(default) modified.mirror(directions,reflect) tag = f'directions={"-".join(directions)}_reflect={reflect}' - reference = os.path.join(reference_dir,f'mirror_{tag}.geom') + reference = reference_dir/f'mirror_{tag}.geom' if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) @@ -96,7 +96,7 @@ class TestGeom: modified = copy.deepcopy(default) modified.clean(stencil) tag = f'stencil={stencil}' - reference = os.path.join(reference_dir,f'clean_{tag}.geom') + reference = reference_dir/f'clean_{tag}.geom' if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) @@ -113,7 +113,7 @@ class TestGeom: modified = copy.deepcopy(default) modified.scale(grid) tag = f'grid={util.srepr(grid,"-")}' - reference = os.path.join(reference_dir,f'scale_{tag}.geom') + reference = reference_dir/f'scale_{tag}.geom' if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) @@ -152,7 +152,7 @@ class TestGeom: modified = copy.deepcopy(default) modified.rotate(Rotation.from_Eulers(Eulers,degrees=True)) tag = f'Eulers={util.srepr(Eulers,"-")}' - reference = os.path.join(reference_dir,f'rotate_{tag}.geom') + reference = reference_dir/f'rotate_{tag}.geom' if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference)) diff --git a/python/tests/test_Orientation.py b/python/tests/test_Orientation.py index 84414a343..4987f1f1f 100644 --- a/python/tests/test_Orientation.py +++ b/python/tests/test_Orientation.py @@ -33,7 +33,7 @@ def inverse_pole(orientation,axis,proper=False,SST=True): @pytest.fixture def reference_dir(reference_dir_base): """Directory containing reference results.""" - return os.path.join(reference_dir_base,'Rotation') + return reference_dir_base/'Rotation' class TestOrientation: diff --git a/python/tests/test_Result.py b/python/tests/test_Result.py index c6dbfb5bd..53bcdda9d 100644 --- a/python/tests/test_Result.py +++ b/python/tests/test_Result.py @@ -1,6 +1,7 @@ import time import shutil import os +import sys from datetime import datetime import pytest @@ -100,14 +101,16 @@ class TestResult: assert np.allclose(in_memory,in_file) @pytest.mark.parametrize('mode',['direct','function']) - def test_add_calculation(self,default,mode): - def my_func(field): - return 2.0*np.abs(field)-1.0 + def test_add_calculation(self,default,tmp_path,mode): if mode == 'direct': default.add_calculation('x','2.0*np.abs(#F#)-1.0','-','my notes') else: - default.enable_user_function(my_func) + with open(tmp_path/'f.py','w') as f: + f.write("import numpy as np\ndef my_func(field):\n return 2.0*np.abs(field)-1.0\n") + sys.path.insert(0,str(tmp_path)) + import f + default.enable_user_function(f.my_func) default.add_calculation('x','my_func(#F#)','-','my notes') loc = {'F': default.get_dataset_location('F'), diff --git a/python/tests/test_Rotation.py b/python/tests/test_Rotation.py index 49f57f67f..8b26a7472 100644 --- a/python/tests/test_Rotation.py +++ b/python/tests/test_Rotation.py @@ -6,7 +6,7 @@ import numpy as np from damask import Rotation from damask import _rotation -n = 1100 +n = 1000 atol=1.e-4 @pytest.fixture diff --git a/python/tests/test_Table.py b/python/tests/test_Table.py index ac7444808..2f7c14bae 100644 --- a/python/tests/test_Table.py +++ b/python/tests/test_Table.py @@ -15,7 +15,7 @@ def default(): @pytest.fixture def reference_dir(reference_dir_base): """Directory containing reference results.""" - return os.path.join(reference_dir_base,'Table') + return reference_dir_base/'Table' class TestTable: @@ -35,9 +35,13 @@ class TestTable: d = default.get('5_F') assert np.allclose(d,1.0) and d.shape[1:] == (1,) - def test_write_read_str(self,default,tmpdir): - default.to_ASCII(str(tmpdir.join('default.txt'))) - new = Table.from_ASCII(str(tmpdir.join('default.txt'))) + @pytest.mark.parametrize('mode',['str','path']) + def test_write_read(self,default,tmpdir,mode): + default.to_ASCII(tmpdir/'default.txt') + if mode == 'path': + new = Table.from_ASCII(tmpdir/'default.txt') + elif mode == 'str': + new = Table.from_ASCII(str(tmpdir/'default.txt')) assert all(default.data==new.data) and default.shapes == new.shapes def test_write_read_file(self,default,tmpdir): @@ -54,20 +58,25 @@ class TestTable: new = Table.from_ASCII(f) assert all(default.data==new.data) and default.shapes == new.shapes - def test_read_ang_str(self,reference_dir): - new = Table.from_ang(os.path.join(reference_dir,'simple.ang')) + + @pytest.mark.parametrize('mode',['str','path']) + def test_read_ang(self,reference_dir,mode): + if mode == 'path': + new = Table.from_ang(reference_dir/'simple.ang') + elif mode == 'str': + new = Table.from_ang(str(reference_dir/'simple.ang')) assert new.data.shape == (4,10) and \ new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit'] def test_read_ang_file(self,reference_dir): - f = open(os.path.join(reference_dir,'simple.ang')) + f = open(reference_dir/'simple.ang') new = Table.from_ang(f) assert new.data.shape == (4,10) and \ new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit'] @pytest.mark.parametrize('fname',['datatype-mix.txt','whitespace-mix.txt']) def test_read_strange(self,reference_dir,fname): - with open(os.path.join(reference_dir,fname)) as f: + with open(reference_dir/fname) as f: Table.from_ASCII(f) def test_set(self,default):