-
Notifications
You must be signed in to change notification settings - Fork 84
/
virtual_images_utils.py
93 lines (73 loc) · 2.72 KB
/
virtual_images_utils.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
# -*- 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/>.
import numpy as np
from pyxem.utils._deprecated import deprecated
@deprecated(since="0.18.0", removal="1.0.0")
def normalize_virtual_images(im):
"""Normalizes image intensity by dividing by maximum value.
Parameters
----------
im : np.array()
Array of image intensities
Returns
-------
imn : np.array()
Array of normalized image intensities
"""
imn = im / im.max()
return imn
@deprecated(since="0.18.0", removal="1.0.0")
def get_vectors_mesh(g1_norm, g2_norm, g_norm_max, angle=0.0, shear=0.0):
"""
Calculate vectors coordinates of a mesh defined by a norm, a rotation and
a shear component.
Parameters
----------
g1_norm, g2_norm : float
The norm of the two vectors of the mesh.
g_norm_max : float
The maximum value for the norm of each vector.
angle : float, optional
The rotation of the mesh in degree.
shear : float, optional
The shear of the mesh. It must be in the interval [0, 1].
The default is 0.0.
Returns
-------
np.ndarray
x and y coordinates of the vectors of the mesh
"""
def rotation_matrix(angle):
return np.array(
[[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]
)
def shear_matrix(shear):
return np.array([[1.0, shear], [0.0, 1.0]])
if shear < 0 or shear > 1:
raise ValueError("The `shear` value must be in the interval [0, 1].")
order1 = int(np.ceil(g_norm_max / g1_norm))
order2 = int(np.ceil(g_norm_max / g2_norm))
order = max(order1, order2)
x = np.arange(-g1_norm * order, g1_norm * (order + 1), g1_norm)
y = np.arange(-g2_norm * order, g2_norm * (order + 1), g2_norm)
xx, yy = np.meshgrid(x, y)
vectors = np.stack(np.meshgrid(x, y)).reshape((2, (2 * order + 1) ** 2))
transformation = rotation_matrix(np.radians(angle)) @ shear_matrix(shear)
vectors = transformation @ vectors
norm = np.linalg.norm(vectors, axis=0)
return vectors[:, norm <= g_norm_max].T