From 1ff6a0974627a69139c56f628003d7a4e8bceaeb Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 23 Mar 2021 15:00:59 +0100 Subject: [PATCH] tests/specifications --- python/damask/util.py | 8 ++++---- python/tests/test_util.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/python/damask/util.py b/python/damask/util.py index b5b86c9b1..56143cc5e 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -395,7 +395,7 @@ def DREAM3D_base_group(fname): base_group = f.visit(lambda path: path.rsplit('/',2)[0] if '_SIMPL_GEOMETRY/SPACING' in path else None) if base_group is None: - raise ValueError('Could not determine base group in file {fname}.') + raise ValueError(f'Could not determine base group in file {fname}.') return base_group @@ -415,13 +415,13 @@ def DREAM3D_cell_data_group(fname): """ base_group = DREAM3D_base_group(fname) with h5py.File(fname,'r') as f: - N_points = np.prod(f[os.path.join(base_group,'_SIMPL_GEOMETRY','DIMENSIONS')]) + cells = tuple(f[os.path.join(base_group,'_SIMPL_GEOMETRY','DIMENSIONS')][()][::-1]) cell_data_group = f[base_group].visititems(lambda path,obj: path.split('/')[0] \ - if isinstance(obj,h5py._hl.dataset.Dataset) and np.prod(np.shape(obj)) == N_points \ + if isinstance(obj,h5py._hl.dataset.Dataset) and np.shape(obj)[:-1] == cells \ else None) if cell_data_group is None: - raise ValueError('Could not determine cell data group in file {fname}.') + raise ValueError(f'Could not determine cell data group in file {fname}/{base_group}.') return cell_data_group diff --git a/python/tests/test_util.py b/python/tests/test_util.py index 397926682..93044b31d 100644 --- a/python/tests/test_util.py +++ b/python/tests/test_util.py @@ -1,6 +1,10 @@ +import random +import os + import pytest import numpy as np from scipy import stats +import h5py from damask import util @@ -102,3 +106,36 @@ class TestUtil: @pytest.mark.parametrize('style',[util.emph,util.deemph,util.warn,util.strikeout]) def test_decorate(self,style): assert 'DAMASK' in style('DAMASK') + + @pytest.mark.parametrize('complete',[True,False]) + def test_D3D_base_group(self,tmp_path,complete): + base_group = ''.join(random.choices('DAMASK', k=10)) + with h5py.File(tmp_path/'base_group.dream3d','w') as f: + f.create_group(os.path.join(base_group,'_SIMPL_GEOMETRY')) + if complete: + f[os.path.join(base_group,'_SIMPL_GEOMETRY')].create_dataset('SPACING',data=np.ones(3)) + + if complete: + assert base_group == util.DREAM3D_base_group(tmp_path/'base_group.dream3d') + else: + with pytest.raises(ValueError): + util.DREAM3D_base_group(tmp_path/'base_group.dream3d') + + @pytest.mark.parametrize('complete',[True,False]) + def test_D3D_cell_data_group(self,tmp_path,complete): + base_group = ''.join(random.choices('DAMASK', k=10)) + cell_data_group = ''.join(random.choices('KULeuven', k=10)) + cells = np.random.randint(1,50,3) + with h5py.File(tmp_path/'cell_data_group.dream3d','w') as f: + f.create_group(os.path.join(base_group,'_SIMPL_GEOMETRY')) + f[os.path.join(base_group,'_SIMPL_GEOMETRY')].create_dataset('SPACING',data=np.ones(3)) + f[os.path.join(base_group,'_SIMPL_GEOMETRY')].create_dataset('DIMENSIONS',data=cells[::-1]) + f[base_group].create_group(cell_data_group) + if complete: + f[os.path.join(base_group,cell_data_group)].create_dataset('data',shape=np.append(cells,1)) + + if complete: + assert cell_data_group == util.DREAM3D_cell_data_group(tmp_path/'cell_data_group.dream3d') + else: + with pytest.raises(ValueError): + util.DREAM3D_cell_data_group(tmp_path/'cell_data_group.dream3d')