[df30106] | 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 | |
---|
[1b5e020] | 75 | def 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 |
---|
[df30106] | 87 | def Iq(q, scale=1.0): |
---|
| 88 | """ |
---|
| 89 | :param q: sequence of momentum transfer values |
---|
| 90 | :param scale: scaling factor |
---|
| 91 | :return: calculated intensities |
---|
| 92 | """ |
---|
[1b5e020] | 93 | return scale * _attr['itp'](q) |
---|
[df30106] | 94 | |
---|
| 95 | Iq.vectorized = True # Iq accepts an array of q values |
---|
| 96 | |
---|
| 97 | |
---|
[1b5e020] | 98 | @numpyze |
---|
[df30106] | 99 | def 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 | """ |
---|
[1b5e020] | 106 | return scale * _attr['itp'](qx, qy) |
---|
[df30106] | 107 | pass |
---|
| 108 | |
---|
| 109 | Iqxy.vectorized = True # Iqxy accepts arrays of qx, qy values |
---|
| 110 | |
---|
| 111 | |
---|