more systematic naming

YAML is the base class for ConfigMaterial and LoadcaseGrid
This commit is contained in:
Martin Diehl 2023-12-05 10:34:05 +01:00
parent ad60193502
commit 7bfb081106
7 changed files with 166718 additions and 38 deletions

@ -1 +1 @@
Subproject commit bc2c8e3b8f405fdda0d69a2900d9b94c7cc0936f Subproject commit 1f36292500ca01e7164ac6195eba0dc3e952bafb

View File

@ -23,7 +23,7 @@ from ._orientation import Orientation # noqa
from ._table import Table # noqa from ._table import Table # noqa
from ._colormap import Colormap # noqa from ._colormap import Colormap # noqa
from ._vtk import VTK # noqa from ._vtk import VTK # noqa
from ._config import Config # noqa from ._yaml import YAML # noqa
from ._configmaterial import ConfigMaterial # noqa from ._configmaterial import ConfigMaterial # noqa
from ._loadcasegrid import LoadcaseGrid # noqa from ._loadcasegrid import LoadcaseGrid # noqa
from ._geomgrid import GeomGrid # noqa from ._geomgrid import GeomGrid # noqa

View File

@ -4,7 +4,7 @@ import numpy as np
import h5py import h5py
from ._typehints import FileHandle, FloatSequence, StrSequence from ._typehints import FileHandle, FloatSequence, StrSequence
from . import Config from . import YAML
from . import Rotation from . import Rotation
from . import Orientation from . import Orientation
from . import util from . import util
@ -12,7 +12,7 @@ from . import tensor
from . import Table from . import Table
class ConfigMaterial(Config): class ConfigMaterial(YAML):
""" """
Material configuration. Material configuration.

View File

@ -4,9 +4,9 @@ from numpy import ma
import yaml import yaml
from ._typehints import FileHandle from ._typehints import FileHandle
from ._config import NiceDumper from ._yaml import NiceDumper
from . import util from . import util
from . import Config from . import YAML
class MaskedMatrixDumper(NiceDumper): class MaskedMatrixDumper(NiceDumper):
@ -16,7 +16,7 @@ class MaskedMatrixDumper(NiceDumper):
return super().represent_data(data.astype(object).filled('x') if isinstance(data, ma.core.MaskedArray) else data) # type: ignore[attr-defined] return super().represent_data(data.astype(object).filled('x') if isinstance(data, ma.core.MaskedArray) else data) # type: ignore[attr-defined]
class LoadcaseGrid(Config): class LoadcaseGrid(YAML):
"""Load case for grid solver.""" """Load case for grid solver."""
def __init__(self, def __init__(self,

View File

@ -18,7 +18,7 @@ from ._typehints import FileHandle
from . import Rotation from . import Rotation
from . import util from . import util
MyType = TypeVar('MyType', bound='Config') MyType = TypeVar('MyType', bound='YAML')
class NiceDumper(SafeDumper): class NiceDumper(SafeDumper):
"""Make YAML readable for humans.""" """Make YAML readable for humans."""
@ -37,7 +37,7 @@ class NiceDumper(SafeDumper):
def represent_data(self, def represent_data(self,
data: Any): data: Any):
"""Cast Config objects and its subclasses to dict.""" """Cast YAML objects and its subclasses to dict."""
if isinstance(data, dict) and type(data) != dict: if isinstance(data, dict) and type(data) != dict:
return self.represent_data(dict(data)) return self.represent_data(dict(data))
if isinstance(data, np.ndarray): if isinstance(data, np.ndarray):
@ -54,7 +54,7 @@ class NiceDumper(SafeDumper):
"""Do not use references to existing objects.""" """Do not use references to existing objects."""
return True return True
class Config(dict): class YAML(dict):
"""YAML-based configuration.""" """YAML-based configuration."""
def __init__(self, def __init__(self,
@ -66,7 +66,7 @@ class Config(dict):
Parameters Parameters
---------- ----------
config : dict or str, optional config : dict or str, optional
Configuration. String needs to be valid YAML. YAML. String needs to be valid YAML.
**kwargs: arbitray keyword-value pairs, optional **kwargs: arbitray keyword-value pairs, optional
Top level entries of the configuration. Top level entries of the configuration.
@ -129,12 +129,12 @@ class Config(dict):
Parameters Parameters
---------- ----------
other : damask.Config or dict other : damask.YAML or dict
Key-value pairs that update self. Key-value pairs that update self.
Returns Returns
------- -------
updated : damask.Config updated : damask.YAML
Updated configuration. Updated configuration.
Note Note
@ -170,7 +170,7 @@ class Config(dict):
Returns Returns
------- -------
updated : damask.Config updated : damask.YAML
Updated configuration. Updated configuration.
""" """
@ -193,8 +193,8 @@ class Config(dict):
Returns Returns
------- -------
loaded : damask.Config loaded : damask.YAML
Configuration from file. YAML from file.
""" """
return cls(yaml.load(util.open_text(fname), Loader=SafeLoader)) return cls(yaml.load(util.open_text(fname), Loader=SafeLoader))

166680
python/tests/resources/GeomGrid/d.vti vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,75 +1,75 @@
import pytest import pytest
import numpy as np import numpy as np
from damask import Config from damask import YAML
from damask import Rotation from damask import Rotation
from damask import Orientation from damask import Orientation
class TestConfig: class TestYAML:
def test_init_keyword(self): def test_init_keyword(self):
assert Config(p=4)['p'] == 4 assert YAML(p=4)['p'] == 4
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}']) @pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
def test_init_config(self,config): def test_init_config(self,config):
assert Config(config)['p'] == 1 assert YAML(config)['p'] == 1
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}']) @pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
def test_init_both(self,config): def test_init_both(self,config):
assert Config(config,p=2)['p'] == 2 assert YAML(config,p=2)['p'] == 2
@pytest.mark.parametrize('flow_style',[None,True,False]) @pytest.mark.parametrize('flow_style',[None,True,False])
def test_load_save_str(self,tmp_path,flow_style): def test_load_save_str(self,tmp_path,flow_style):
config = Config() config = YAML()
config['A'] = 1 config['A'] = 1
config['B'] = [2,3] config['B'] = [2,3]
config.save(tmp_path/'config.yaml',default_flow_style=flow_style) config.save(tmp_path/'config.yaml',default_flow_style=flow_style)
assert Config.load(tmp_path/'config.yaml') == config assert YAML.load(tmp_path/'config.yaml') == config
def test_load_save_file(self,tmp_path): def test_load_save_file(self,tmp_path):
config = Config() config = YAML()
config['A'] = 1 config['A'] = 1
config['B'] = [2,3] config['B'] = [2,3]
with open(tmp_path/'config.yaml','w') as f: with open(tmp_path/'config.yaml','w') as f:
config.save(f) config.save(f)
with open(tmp_path/'config.yaml') as f: with open(tmp_path/'config.yaml') as f:
assert Config.load(f) == config assert YAML.load(f) == config
def test_add_remove(self): def test_add_remove(self):
dummy = {'hello':'world','foo':'bar'} dummy = {'hello':'world','foo':'bar'}
config = Config() config = YAML()
config |= dummy config |= dummy
assert config == Config() | dummy assert config == YAML() | dummy
config = config.delete(dummy) config = config.delete(dummy)
assert config == Config() assert config == YAML()
assert (config | dummy ).delete( 'hello' ) == config | {'foo':'bar'} assert (config | dummy ).delete( 'hello' ) == config | {'foo':'bar'}
assert (config | dummy ).delete([ 'hello', 'foo' ]) == config assert (config | dummy ).delete([ 'hello', 'foo' ]) == config
assert (config | Config(dummy)).delete({ 'hello':1,'foo':2 }) == config assert (config | YAML(dummy)).delete({ 'hello':1,'foo':2 }) == config
assert (config | Config(dummy)).delete(Config({'hello':1 })) == config | {'foo':'bar'} assert (config | YAML(dummy)).delete(YAML({'hello':1 })) == config | {'foo':'bar'}
def test_repr(self,tmp_path): def test_repr(self,tmp_path):
config = Config() config = YAML()
config['A'] = 1 config['A'] = 1
config['B'] = [2,3] config['B'] = [2,3]
with open(tmp_path/'config.yaml','w') as f: with open(tmp_path/'config.yaml','w') as f:
f.write(config.__repr__()) f.write(config.__repr__())
assert Config.load(tmp_path/'config.yaml') == config assert YAML.load(tmp_path/'config.yaml') == config
def test_numpy(self,tmp_path): def test_numpy(self,tmp_path):
assert Config({'A':np.ones(3,'i'), 'B':np.ones(1)[0]}).__repr__() == \ assert YAML({'A':np.ones(3,'i'), 'B':np.ones(1)[0]}).__repr__() == \
Config({'A':[1,1,1], 'B':1.0}).__repr__() YAML({'A':[1,1,1], 'B':1.0}).__repr__()
def test_abstract_is_valid(self): def test_abstract_is_valid(self):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
Config().is_valid YAML().is_valid
def test_abstract_is_complete(self): def test_abstract_is_complete(self):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
Config().is_complete YAML().is_complete
@pytest.mark.parametrize('data',[Rotation.from_random(),Orientation.from_random(lattice='cI')]) @pytest.mark.parametrize('data',[Rotation.from_random(),Orientation.from_random(lattice='cI')])
def test_rotation_orientation(self,data): def test_rotation_orientation(self,data):
assert str(Config(a=data)) == str(Config(a=data.as_quaternion())) assert str(YAML(a=data)) == str(YAML(a=data.as_quaternion()))
def test_initialize(self): def test_initialize(self):
yml = """ yml = """
@ -77,4 +77,4 @@ class TestConfig:
- 1 - 1
- 2 - 2
""" """
assert Config(yml) == Config('{"a":[1,2]}') == Config(a=[1,2]) == Config(dict(a=[1,2])) assert YAML(yml) == YAML('{"a":[1,2]}') == YAML(a=[1,2]) == YAML(dict(a=[1,2]))