transparently handle orientations/rotations

This commit is contained in:
Martin Diehl 2021-03-25 15:30:31 +01:00
parent 4f40e58ce6
commit fe0cd8e5e2
2 changed files with 15 additions and 2 deletions

View File

@ -6,6 +6,9 @@ import abc
import numpy as np import numpy as np
import yaml import yaml
from . import Rotation
from . import Orientation
class NiceDumper(yaml.SafeDumper): class NiceDumper(yaml.SafeDumper):
"""Make YAML readable for humans.""" """Make YAML readable for humans."""
@ -20,8 +23,12 @@ class NiceDumper(yaml.SafeDumper):
def represent_data(self, data): def represent_data(self, data):
"""Cast Config objects and its subclasses to dict.""" """Cast Config objects and its subclasses to dict."""
return self.represent_data(dict(data)) if isinstance(data, dict) and type(data) != dict else \ if isinstance(data, dict) and type(data) != dict:
super().represent_data(data) return self.represent_data(dict(data))
if isinstance(data, (Rotation, Orientation)):
return self.represent_data(data.as_quaternion())
else:
return super().represent_data(data)
def ignore_aliases(self, data): def ignore_aliases(self, data):
"""No references.""" """No references."""

View File

@ -2,6 +2,8 @@ import pytest
import numpy as np import numpy as np
from damask import Config from damask import Config
from damask import Rotation
from damask import Orientation
class TestConfig: class TestConfig:
@ -51,3 +53,7 @@ class TestConfig:
def test_abstract_is_complete(self): def test_abstract_is_complete(self):
assert Config().is_complete is None assert Config().is_complete is None
@pytest.mark.parametrize('data',[Rotation.from_random(),Orientation.from_random()])
def test_rotation_orientation(self,data):
assert str(Config(a=data)) == str(Config(a=data.as_quaternion()))