/
test_similarity_metrics.py
52 lines (44 loc) · 1.99 KB
/
test_similarity_metrics.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
# Copyright 2019-2023 The kikuchipy developers
#
# This file is part of kikuchipy.
#
# kikuchipy 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.
#
# kikuchipy 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 kikuchipy. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
import pytest
from kikuchipy.indexing.similarity_metrics import NormalizedCrossCorrelationMetric
from kikuchipy.indexing.similarity_metrics._normalized_cross_correlation import (
_ncc_single_patterns_1d_float32_exp_centered,
)
class TestSimilarityMetric:
def test_invalid_metric(self):
with pytest.raises(ValueError, match="Data type float16 not among"):
NormalizedCrossCorrelationMetric(dtype=np.float16).raise_error_if_invalid()
def test_metric_repr(self):
ncc = NormalizedCrossCorrelationMetric(1, 1)
assert repr(ncc) == (
"NormalizedCrossCorrelationMetric: float32, greater is better, "
"rechunk: False, navigation mask: False, signal mask: False"
)
class TestNumbaAcceleratedMetrics:
def test_ncc_single_patterns_1d_float32(self):
exp = np.linspace(0, 0.5, 100, dtype=np.float32)
sim = np.linspace(0.5, 1, 100, dtype=np.float32)
exp -= np.mean(exp)
exp_squared_norm = np.square(exp).sum()
r1 = _ncc_single_patterns_1d_float32_exp_centered(exp, sim, exp_squared_norm)
r2 = _ncc_single_patterns_1d_float32_exp_centered.py_func(
exp, sim, exp_squared_norm
)
assert np.isclose(r1, r2)
assert np.isclose(r1, 0.99999994, atol=1e-8)