-
Notifications
You must be signed in to change notification settings - Fork 84
/
displacement_gradient_tensor_generator.py
158 lines (135 loc) · 5.27 KB
/
displacement_gradient_tensor_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
# Copyright 2016-2024 The pyXem developers
#
# This file is part of pyXem.
#
# pyXem is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyXem is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyXem. If not, see <http://www.gnu.org/licenses/>.
"""Generating DisplacementGradientMaps from diffraction vectors."""
import numpy as np
from pyxem.signals.tensor_field import DisplacementGradientMap
def get_DisplacementGradientMap(
strained_vectors, unstrained_vectors, weights=None, return_residuals=False, **kwargs
):
r"""Calculates the displacement gradient tensor at each navigation position in a map.
Compares vectors to determine the 2 x 2 matrix,
:math:`\\mathbf(L)`, that maps unstrained vectors, Vu, to strained vectors,
Vs, using the np.lingalg.inv() function to find L that satisfies
:math:`Vs = \\mathbf(L) Vu`.
The transformation is returned as a 3 x 3 displacement gradient tensor.
Parameters
----------
strained_vectors : hyperspy.Signal2D
Signal2D with a 2 x n array at each navigation position containing the
Cartesian components of two strained basis vectors, V and U, defined as
row vectors.
unstrained_vectors : numpy.array
A 2 x n array containing the Cartesian components of two unstrained
basis vectors, V and U, defined as row vectors.
weights : list
of weights to be passed to the least squares optimiser, not used for n=2
return_residuals: Bool
If the residuals for the least squares optimiser should be returned.
kwargs: dict
Any additional keyword arguments passed to the `hyperspy.signals.BaseSignal.map`
function.
Returns
-------
D : DisplacementGradientMap
The 3 x 3 displacement gradient tensor (measured in reciprocal space) at
every navigation position.
Notes
-----
n=2 now behaves the same as the n>2 case; see Release Notes for 0.10.0 for details.
See Also
--------
get_single_DisplacementGradientTensor()
"""
# Calculate displacement gradient tensor across map.
D = strained_vectors.map(
get_single_DisplacementGradientTensor,
Vu=unstrained_vectors,
weights=weights,
inplace=False,
output_signal_size=(3, 3),
output_dtype=np.float64,
**kwargs
)
if return_residuals:
R = strained_vectors.map(
get_single_DisplacementGradientTensor,
Vu=unstrained_vectors,
weights=weights,
inplace=False,
output_dtype=np.float64,
return_residuals=True,
**kwargs
)
return DisplacementGradientMap(D), R
else:
return DisplacementGradientMap(D)
def get_single_DisplacementGradientTensor(
Vs, Vu=None, weights=None, return_residuals=False
):
r"""Calculates the displacement gradient tensor from a pairs of vectors.
Determines the 2 x 2 matrix, :math:`\\mathbf(L)`, that maps unstrained
vectors, Vu, onto strained vectors, Vs
The transformation is returned as a 3 x 3 displacement gradient tensor.
Parameters
----------
Vs : numpy.array
A 2 x n array containing the Cartesian components of two strained basis
vectors, V and U, defined as row vectors.
Vu : numpy.array
A 2 x n array containing the Cartesian components of two unstrained
basis vectors, V and U, defined as row vectors.
weights : list
of weights to be passed to the least squares optimiser
return_residuals: Bool
If the residuals for the least squares optimiser should be returned.
Returns
-------
D : numpy.array
A 3 x 3 displacement gradient tensor (measured in reciprocal space).
residuals : numpy.array
The residuals for the least squares fitting.
Notes
-----
n=2 now behaves the same as the n>2 case; see Release Notes for 0.10.0 for details.
See Also
--------
get_DisplacementGradientMap()
"""
is_row_nan = np.logical_not(np.any(np.isnan(Vs), axis=1))
Vs = Vs[is_row_nan]
Vu = Vu[is_row_nan]
if Vu is not None:
if Vu.dtype == object:
Vu = Vu[()]
if weights is not None:
# see https://stackoverflow.com/questions/27128688
weights = np.asarray(weights)
# Need vectors normalized to the unstrained region otherwise the weighting breaks down
Vs = (np.divide(Vs.T, np.linalg.norm(Vu, axis=1)) * np.sqrt(weights)).T
Vu = (np.divide(Vu.T, np.linalg.norm(Vu, axis=1)) * np.sqrt(weights)).T
else:
Vs, Vu = Vs, Vu
L, residuals, rank, s = np.linalg.lstsq(Vu, Vs, rcond=-1)
# only need the return array, see np,linalg.lstsq doc
# Put caculated matrix values into 3 x 3 matrix to be returned.
D = np.eye(3)
D[0:2, 0:2] = L
if return_residuals:
return residuals
else:
return D