source: sasmodels/sasmodels/models/line.py @ c94ab04

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since c94ab04 was 0507e09, checked in by smk78, 5 years ago

Added link to source code to each model. Closes #883

  • Property mode set to 100644
File size: 3.1 KB
Line 
1r"""
2This model calculates intensity using simple linear function
3
4Definition
5----------
6
7The scattering intensity $I(q)$ is calculated as
8
9.. math::
10
11    I(q) = \text{scale} (A + B \cdot q) + \text{background}
12
13.. note::
14    For 2D plots intensity has different definition than other shape independent models
15
16.. math::
17
18    I(q) = \text{scale} (I(qx) \cdot I(qy)) + \text{background}
19
20References
21----------
22
23None.
24
25Source
26------
27
28`line.py <https://github.com/SasView/sasmodels/blob/master/sasmodels/models/line.py>`_
29
30Authorship and Verification
31----------------------------
32
33* **Author:**
34* **Last Modified by:**
35* **Last Reviewed by:**
36* **Source added by :** Steve King **Date:** March 25, 2019"""
37
38import numpy as np
39from numpy import inf
40
41name = "line"
42title = "Line model"
43description = """\
44      I(q) = A + B*q
45
46      List of default parameters:
47      A = intercept
48      B = slope
49      """
50category = "shape-independent"
51
52# pylint: disable=bad-whitespace, line-too-long
53#             ["name", "units", default, [lower, upper], "type", "description"],
54parameters = [["intercept", "1/cm",   1.0, [-inf, inf], "", "intercept in linear model"],
55              ["slope",     "Ang/cm", 1.0, [-inf, inf], "", "slope in linear model"],
56             ]
57# pylint: enable=bad-whitespace, line-too-long
58
59def Iq(q, intercept, slope):
60    """
61    :param q:           Input q-value
62    :param intercept:   Intrecept in linear model
63    :param slope:       Slope in linear model
64    :return:            Calculated Intensity
65    """
66    inten = intercept + slope*q
67    return inten
68
69Iq.vectorized = True # Iq accepts an array of q values
70
71
72def Iqxy(qx, qy, intercept, slope):
73    """
74    :param qx:   Input q_x-value
75    :param qy:   Input q_y-value
76    :param intercept:   Intrecept in linear model
77    :param slope:       Slope in linear model
78    :return:     2D-Intensity
79    """
80    # TODO: SasView documents 2D intensity as Iq(qx)*Iq(qy), but returns Iq(qy)
81    # Note: SasView.run([r, theta]) does return Iq(qx)*Iq(qy)
82    return Iq(qx, intercept, slope)*Iq(qy, intercept, slope)
83
84Iqxy.vectorized = True  # Iqxy accepts an array of qx qy values
85
86# uncomment the following to test Iqxy in C models
87#del Iq, Iqxy
88#c_code = """
89#static double Iq(double q, double b, double m) { return m*q+b; }
90#static double Iqxy(double qx, double qy, double b, double m)
91#{ return (m*qx+b)*(m*qy+b); }
92#"""
93
94def random():
95    """Return a random parameter set for the model."""
96    scale = 10**np.random.uniform(0, 3)
97    slope = np.random.uniform(-1, 1)*1e2
98    offset = 1e-5 + (0 if slope > 0 else -slope)
99    intercept = 10**np.random.uniform(0, 1) + offset
100    pars = dict(
101        #background=0,
102        scale=scale,
103        slope=slope,
104        intercept=intercept,
105    )
106    return pars
107
108tests = [
109    [{'intercept': 1.0, 'slope': 1.0, }, 1.0, 2.001],
110    [{'intercept': 1.0, 'slope': 1.0, }, 0.0, 1.001],
111    [{'intercept': 1.0, 'slope': 1.0, }, 0.4, 1.401],
112    [{'intercept': 1.0, 'slope': 1.0, }, 1.3, 2.301],
113    [{'intercept': 1.0, 'slope': 1.0, }, 0.5, 1.501],
114    [{'intercept': 1.0, 'slope': 1.0, }, [0.4, 0.5], [1.401, 1.501]],
115    [{'intercept': 1.0, 'slope': 1.0, 'background': 0.0, }, (1.3, 1.57), 5.911],
116]
Note: See TracBrowser for help on using the repository browser.