mimic python error message and ensure that error is raised

This commit is contained in:
Martin Diehl 2021-02-12 17:04:35 +01:00
parent 9e8a243d6f
commit fe63adcca4
2 changed files with 42 additions and 24 deletions

View File

@ -268,30 +268,33 @@ class Orientation(Rotation):
def _separate_arguments(parent_dict,function):
"""
Separate arguments required by Rotation and Orientation objects respectively.
Parameters
----------
parent_dict : Dictionary
Contains all **kwargs
function: Method
Function whose signature list is required
Returns
-------
ori_dict: dictionary
Dictionary consisting of valid keys accepted by Orientation class
rot_dict: dictionary
Dictionary consisting of valid keys accepted by 'function' in Rotation class
"""
set_ori = set(inspect.signature(Orientation.__init__).parameters.keys()) & set(parent_dict.keys())
set_rot = set(inspect.signature(function).parameters.keys()) & set(parent_dict.keys())
ori_dict = {key: parent_dict[key] for key in set_ori}
rot_dict = {key: parent_dict[key] for key in set_rot}
if(set(parent_dict.keys())-(set_ori|set_rot)):
raise KeyError(f'Unknown key {set(parent_dict.keys())-(set_ori|set_rot)} present')
invalid_keys = set(parent_dict.keys())-(set_ori|set_rot)
if invalid_keys:
raise TypeError(f"{inspect.stack()[1][3]}() got an unexpected keyword argument '{invalid_keys.pop()}'")
return rot_dict,ori_dict
@classmethod
@util.extended_docstring(Rotation.from_random,_parameter_doc)
@ -312,63 +315,63 @@ class Orientation(Rotation):
def from_Euler_angles(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_Euler_angles)
return cls(rotation=Rotation.from_Euler_angles(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_axis_angle,_parameter_doc)
def from_axis_angle(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_axis_angle)
return cls(rotation=Rotation.from_axis_angle(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_basis,_parameter_doc)
def from_basis(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_basis)
return cls(rotation=Rotation.from_basis(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_matrix,_parameter_doc)
def from_matrix(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_matrix)
return cls(rotation=Rotation.from_matrix(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_Rodrigues_vector,_parameter_doc)
def from_Rodrigues_vector(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_Rodrigues_vector)
return cls(rotation=Rotation.from_Rodrigues_vector(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_homochoric,_parameter_doc)
def from_homochoric(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_homochoric)
return cls(rotation=Rotation.from_homochoric(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_cubochoric,_parameter_doc)
def from_cubochoric(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_cubochoric)
return cls(rotation=Rotation.from_cubochoric(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_spherical_component,_parameter_doc)
def from_spherical_component(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_spherical_component)
return cls(rotation=Rotation.from_spherical_component(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extended_docstring(Rotation.from_fiber_component,_parameter_doc)
def from_fiber_component(cls,**kwargs):
kwargs_rot,kwargs_ori = Orientation._separate_arguments(kwargs,Rotation.from_fiber_component)
return cls(rotation=Rotation.from_fiber_component(**kwargs_rot),**kwargs_ori)
@classmethod
@util.extend_docstring(_parameter_doc)

View File

@ -141,7 +141,7 @@ class TestOrientation:
dict(lattice='hP',a=1.0 ),
dict(lattice='cI',a=1.0, ),
])
def test_from_direction(self,kwargs):
def test_from_directions(self,kwargs):
for a,b in np.random.random((10,2,3)):
c = np.cross(b,a)
if np.all(np.isclose(c,0)): continue
@ -151,6 +151,21 @@ class TestOrientation:
assert np.isclose(np.dot(x/np.linalg.norm(x),np.array([1,0,0])),1) \
and np.isclose(np.dot(z/np.linalg.norm(z),np.array([0,0,1])),1)
@pytest.mark.parametrize('function',[Orientation.from_random,
Orientation.from_quaternion,
Orientation.from_Euler_angles,
Orientation.from_axis_angle,
Orientation.from_basis,
Orientation.from_matrix,
Orientation.from_Rodrigues_vector,
Orientation.from_homochoric,
Orientation.from_cubochoric,
Orientation.from_spherical_component,
Orientation.from_fiber_component,
Orientation.from_directions])
def test_invalid_from(self,function):
with pytest.raises(TypeError):
function(c=.1,degrees=True,invalid=66)
def test_negative_angle(self):
with pytest.raises(ValueError):