1 | r""" |
---|
2 | Definition |
---|
3 | ---------- |
---|
4 | |
---|
5 | References |
---|
6 | ---------- |
---|
7 | |
---|
8 | None. |
---|
9 | |
---|
10 | Authorship and Verification |
---|
11 | --------------------------- |
---|
12 | * **Author:** Jose Borreguero **Date:** August 26 2017 |
---|
13 | * **Last Modified by:** Jose Borreguero **Date:** July 27 2016 |
---|
14 | * **Last Reviewed by:** _REVIEWER_NAME_HERE **Date:** _DATE_HERE_ |
---|
15 | """ |
---|
16 | |
---|
17 | import inspect |
---|
18 | from scipy.interpolate import interp1d, interp2d |
---|
19 | import numpy as np |
---|
20 | |
---|
21 | name = "tabulated" |
---|
22 | title = "An interpolator of a numeric structure factor" |
---|
23 | description = """\ |
---|
24 | I(q) = scale * interpolator(q|q', I') |
---|
25 | List of default parameters: |
---|
26 | scale = scaling factor""" |
---|
27 | category = "shape-independent" |
---|
28 | |
---|
29 | |
---|
30 | # pylint: disable=bad-whitespace, line-too-long |
---|
31 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
32 | parameters = [["scale", "", 1.0, [0, inf], "", "Scaling factor"],] |
---|
33 | # pylint: enable=bad-whitespace, line-too-long |
---|
34 | |
---|
35 | |
---|
36 | # Attributes of the model |
---|
37 | _attr = {'itp': None, # interpolator |
---|
38 | 'q': None, # table of momentum transfer values |
---|
39 | 'qx': None, # table of momentum transfer values along X-axis |
---|
40 | 'qy': None, # table of momentum transfer values along Y-axis |
---|
41 | 'I': None} # table of intensities |
---|
42 | |
---|
43 | |
---|
44 | def update_interpolator(itp=None): |
---|
45 | """ |
---|
46 | Update the interpolator with custom. 'None' for linear interpolation. |
---|
47 | :param itp: interpolator generator. See interp1D and interp2D of |
---|
48 | scipy.interpolate for examples. |
---|
49 | """ |
---|
50 | if None not in (_attr['q'], _attr['I']): |
---|
51 | interpolator = itp if itp else interp1d |
---|
52 | _attr['itp'] = interpolator(_attr['q'], _attr['I']) |
---|
53 | elif None not in (_attr['qx'], _attr['qy'], _attr['I']): |
---|
54 | interpolator = itp if itp else interp2d |
---|
55 | _attr['itp'] = interpolator(_attr['qx'], _attr['qx'], _attr['I']) |
---|
56 | |
---|
57 | |
---|
58 | def initialize(q=None, qx=None, qy=None, I=None): |
---|
59 | """ |
---|
60 | Initialize any of the function attributes |
---|
61 | :param q: sequence of momentum transfer values |
---|
62 | :param qx: sequence of momentum transfer values along X-axis |
---|
63 | :param qy: sequence of momentum transfer values along X-axis |
---|
64 | :param I: sequence of intensities |
---|
65 | """ |
---|
66 | frame = inspect.currentframe() |
---|
67 | attrs, _, _, values = inspect.getargvalues(frame) |
---|
68 | for attr in attrs: |
---|
69 | value = values[attr] |
---|
70 | if value is not None: |
---|
71 | _attr[attr] = np.array(value, dtype=np.float) |
---|
72 | update_interpolator() |
---|
73 | |
---|
74 | |
---|
75 | def Iq(q, scale=1.0): |
---|
76 | """ |
---|
77 | :param q: sequence of momentum transfer values |
---|
78 | :param scale: scaling factor |
---|
79 | :return: calculated intensities |
---|
80 | """ |
---|
81 | q_a = np.array(q, dtype='float') |
---|
82 | return scale * _attr['itp'](q_a) |
---|
83 | |
---|
84 | Iq.vectorized = True # Iq accepts an array of q values |
---|
85 | |
---|
86 | |
---|
87 | def Iqxy(qx, qy, scale=1.0): |
---|
88 | """ |
---|
89 | :param qx: sequence of momentum transfer values along X-axis |
---|
90 | :param qy: sequence of momentum transfer values along X-axis |
---|
91 | :param scale: scaling factor |
---|
92 | :return: calculated intensities |
---|
93 | """ |
---|
94 | qx_a = np.array(qx, dtype='float') |
---|
95 | qy_a = np.array(qy, dtype='float') |
---|
96 | return scale * _attr['itp'](qx_a, qy_a) |
---|
97 | pass |
---|
98 | |
---|
99 | Iqxy.vectorized = True # Iqxy accepts arrays of qx, qy values |
---|
100 | |
---|
101 | |
---|