source: sasmodels/sasmodels/models/tabulated.py @ 1b5e020

Last change on this file since 1b5e020 was 1b5e020, checked in by Jose Borreguero <borreguero@…>, 5 years ago

decorator to numpyze positional arguments

  • Property mode set to 100644
File size: 3.3 KB
Line 
1r"""
2Definition
3----------
4
5References
6----------
7
8None.
9
10Authorship 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
17import inspect
18from scipy.interpolate import interp1d, interp2d
19import numpy as np
20
21name = "tabulated"
22title = "An interpolator of a numeric structure factor"
23description = """\
24    I(q) = scale * interpolator(q|q', I')
25    List of default parameters:
26    scale = scaling factor"""
27category = "shape-independent"
28
29
30# pylint: disable=bad-whitespace, line-too-long
31#             ["name", "units", default, [lower, upper], "type", "description"],
32parameters = [["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
44def 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
58def 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
75def numpyze(calc_I):
76    """
77    Transform positional arguments into numpy arrays
78    :param calc_I: Intensities calculator
79    :return: function acting on the transformed positional arguments
80    """
81    def wrapper(*args, **kwargs):
82        numpy_args = [np.array(arg, dtype=np.float) for arg in args]
83        return calc_I(*numpy_args, **kwargs)
84
85
86@numpyze
87def Iq(q, scale=1.0):
88    """
89    :param q:     sequence of momentum transfer values
90    :param scale: scaling factor
91    :return:      calculated intensities
92    """
93    return scale * _attr['itp'](q)
94
95Iq.vectorized = True  # Iq accepts an array of q values
96
97
98@numpyze
99def Iqxy(qx, qy, scale=1.0):
100    """
101    :param qx:    sequence of momentum transfer values along X-axis
102    :param qy:    sequence of momentum transfer values along X-axis
103    :param scale: scaling factor
104    :return:      calculated intensities
105    """
106    return scale * _attr['itp'](qx, qy)
107    pass
108
109Iqxy.vectorized = True  # Iqxy accepts arrays of qx, qy values
110
111
Note: See TracBrowser for help on using the repository browser.