/
_overwrite_hyperspy_methods.py
109 lines (90 loc) · 3.07 KB
/
_overwrite_hyperspy_methods.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
# 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/>.
"""Internal tools for overwriting HyperSpy class methods."""
import functools
import inspect
import re
from typing import Callable, List, Union
def get_parameters(
method: Callable, params_of_interest: List[str], args: tuple, kwargs: dict
) -> Union[dict, None]:
sig = inspect.signature(method)
params = {}
# Set keys with default values
for k, v in sig.parameters.items():
if k != "self":
default = v.default
if default is inspect.Signature.empty:
default = None
params[k] = default
# Update any positional arguments
if len(args) != 0:
keys = list(params.keys())
for i, a in enumerate(args):
k = keys[i]
params[k] = a
# Update any keyword arguments
for k, v in kwargs.items():
if k in params.keys():
params[k] = v
# Extract parameters of interest, returning None if the parameter
# could not be found
try:
out = {k: params[k] for k in params_of_interest}
except KeyError:
out = None
return out
class insert_doc_disclaimer:
"""Decorator to add a disclaimer to methods inhertied from HyperSpy.
Adopted from Dask's insertion of disclaimers into methods they
inherit from NumPy.
Parameters
----------
cls
Class, typically Signal2D.
meth
Method, e.g. rebin.
"""
def __init__(self, cls, meth):
self.cls_name = cls.__name__
self.doc = meth.__doc__
self.name = meth.__name__
def __call__(self, func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper.__doc__ = self._insert_doc_disclaimer()
return wrapper
def _insert_doc_disclaimer(self):
doc = self.doc
if doc is None:
return doc
i = doc.find("\n\n")
if i != -1:
# Disclaimer
l1 = (
"This docstring was copied from HyperSpy's "
f"{self.cls_name}.{self.name}.\n"
)
l2 = "Some inconsistencies with the kikuchipy version may exist."
# Parts
head = doc[: i + 2]
tail = doc[i + 2 :]
indent = re.match(r"\s*", tail).group(0)
parts = [head, indent, l1, indent, l2, "\n\n", tail]
doc = "".join(parts)
return doc