-
Notifications
You must be signed in to change notification settings - Fork 207
/
signal_tools.py
206 lines (177 loc) · 6.54 KB
/
signal_tools.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
# Copyright 2007-2024 The HyperSpy developers
#
# This file is part of HyperSpy.
#
# HyperSpy 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.
#
# HyperSpy 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 HyperSpy. If not, see <https://www.gnu.org/licenses/#GPL>.
import logging
from itertools import zip_longest
import numpy as np
from hyperspy.misc.array_tools import are_aligned
from hyperspy.misc.axis_tools import check_axes_calibration
_logger = logging.getLogger(__name__)
def _get_shapes(am, ignore_axis):
if ignore_axis is not None:
try:
ignore_axis = am[ignore_axis]
except ValueError:
pass
sigsh = (
tuple(
axis.size if (ignore_axis is None or axis is not ignore_axis) else 1
for axis in am.signal_axes
)
if am.signal_dimension != 0
else ()
)
navsh = (
tuple(
axis.size if (ignore_axis is None or axis is not ignore_axis) else 1
for axis in am.navigation_axes
)
if am.navigation_dimension != 0
else ()
)
return sigsh, navsh
def are_signals_aligned(*args, ignore_axis=None):
if len(args) < 2:
raise ValueError("This function requires at least two signal instances")
args = list(args)
am = args.pop().axes_manager
sigsh, navsh = _get_shapes(am, ignore_axis)
while args:
amo = args.pop().axes_manager
sigsho, navsho = _get_shapes(amo, ignore_axis)
if not (
are_aligned(sigsh[::-1], sigsho[::-1])
and are_aligned(navsh[::-1], navsho[::-1])
):
return False
return True
def _check_and_get_longest_axis(axes):
"""Return the longest axis from a list of axes.
In the case of ties, the first element in the list
will be returned. Logs a warning if the axes have
different calibrations.
"""
only_left = filter(lambda x: x is not None, axes)
longest_idx, longest = sorted(
enumerate(only_left),
key=lambda x: x[1].size,
reverse=True,
)[0]
# Exit early if not DataAxis objects - None is used
# in broadcast_signals() below as a filler so we
# skip it here.
def _check_wrapper(ax1, ax2):
if ax1 is None or ax2 is None:
return True
return check_axes_calibration(ax1, ax2)
# Returns a list of bools, where False
# indicates an axis with a different
# calibration to the longest axis
axes_check = [
_check_wrapper(axes[longest_idx], axes[i])
for i in range(len(axes))
if i != longest_idx
]
if not all(axes_check):
_logger.warning(
"Axis calibration mismatch detected along axis "
f"{longest.index_in_axes_manager}. The "
f"calibration of signal {longest_idx} along "
"this axis will be applied to all signals "
"after stacking."
)
return longest
def broadcast_signals(*args, ignore_axis=None):
"""Broadcasts signals according to the HyperSpy broadcasting rules.
signal and navigation spaces are each separately broadcasted according to
the numpy broadcasting rules. One axis can be ignored and left untouched
(or set to be size 1) across all signals.
Parameters
----------
*args : BaseSignal
Signals to broadcast together
ignore_axis : {None, str, int, Axis}
The axis to be ignored when broadcasting
Returns
-------
list of signals
"""
from hyperspy.signal import BaseSignal
if len(args) < 2:
raise ValueError("This function requires at least two signal instances")
if any([not isinstance(a, BaseSignal) for a in args]):
raise ValueError("Arguments must be of signal type.")
args = list(args)
if not are_signals_aligned(*args, ignore_axis=ignore_axis):
raise ValueError("The signals cannot be broadcasted")
else:
if ignore_axis is not None:
for s in args:
try:
ignore_axis = s.axes_manager[ignore_axis]
break
except ValueError:
pass
new_nav_axes = []
new_nav_shapes = []
for axes in zip_longest(
*[s.axes_manager.navigation_axes for s in args], fillvalue=None
):
longest = _check_and_get_longest_axis(axes)
new_nav_axes.append(longest)
new_nav_shapes.append(
longest.size
if (ignore_axis is None or ignore_axis not in axes)
else None
)
new_sig_axes = []
new_sig_shapes = []
for axes in zip_longest(
*[s.axes_manager.signal_axes for s in args], fillvalue=None
):
longest = _check_and_get_longest_axis(axes)
new_sig_axes.append(longest)
new_sig_shapes.append(
longest.size
if (ignore_axis is None or ignore_axis not in axes)
else None
)
results = []
new_axes = new_nav_axes[::-1] + new_sig_axes[::-1]
new_data_shape = new_nav_shapes[::-1] + new_sig_shapes[::-1]
for s in args:
data = s._data_aligned_with_axes
sam = s.axes_manager
sdim_diff = len(new_sig_axes) - len(sam.signal_axes)
while sdim_diff > 0:
slices = (slice(None),) * sam.navigation_dimension
slices += (None, Ellipsis)
data = data[slices]
sdim_diff -= 1
thisshape = new_data_shape.copy()
if ignore_axis is not None:
_id = new_data_shape.index(None)
newlen = data.shape[_id] if len(data.shape) > _id else 1
thisshape[_id] = newlen
thisshape = tuple(thisshape)
if data.shape != thisshape:
data = np.broadcast_to(data, thisshape)
ns = s._deepcopy_with_new_data(data)
ns.axes_manager._axes = [ax.copy() for ax in new_axes]
ns.get_dimensions_from_data()
results.append(ns.transpose(signal_axes=len(new_sig_axes)))
return results