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

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since b297ba9 was b297ba9, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

lint

  • Property mode set to 100644
File size: 2.9 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"""
25
26import numpy as np
27from numpy import inf
28
29name = "line"
30title = "Line model"
31description = """\
32      I(q) = A + B*q
33
34      List of default parameters:
35      A = intercept
36      B = slope
37      """
38category = "shape-independent"
39
40# pylint: disable=bad-whitespace, line-too-long
41#             ["name", "units", default, [lower, upper], "type", "description"],
42parameters = [["intercept", "1/cm",   1.0, [-inf, inf], "", "intercept in linear model"],
43              ["slope",     "Ang/cm", 1.0, [-inf, inf], "", "slope in linear model"],
44             ]
45# pylint: enable=bad-whitespace, line-too-long
46
47def Iq(q, intercept, slope):
48    """
49    :param q:           Input q-value
50    :param intercept:   Intrecept in linear model
51    :param slope:       Slope in linear model
52    :return:            Calculated Intensity
53    """
54    inten = intercept + slope*q
55    return inten
56
57Iq.vectorized = True # Iq accepts an array of q values
58
59
60def Iqxy(qx, qy, intercept, slope):
61    """
62    :param qx:   Input q_x-value
63    :param qy:   Input q_y-value
64    :param intercept:   Intrecept in linear model
65    :param slope:       Slope in linear model
66    :return:     2D-Intensity
67    """
68    # TODO: SasView documents 2D intensity as Iq(qx)*Iq(qy), but returns Iq(qy)
69    # Note: SasView.run([r, theta]) does return Iq(qx)*Iq(qy)
70    return Iq(qx, intercept, slope)*Iq(qy, intercept, slope)
71
72Iqxy.vectorized = True  # Iqxy accepts an array of qx qy values
73
74# uncomment the following to test Iqxy in C models
75#del Iq, Iqxy
76#c_code = """
77#static double Iq(double q, double b, double m) { return m*q+b; }
78#static double Iqxy(double qx, double qy, double b, double m)
79#{ return (m*qx+b)*(m*qy+b); }
80#"""
81
82def random():
83    """Return a random parameter set for the model."""
84    scale = 10**np.random.uniform(0, 3)
85    slope = np.random.uniform(-1, 1)*1e2
86    offset = 1e-5 + (0 if slope > 0 else -slope)
87    intercept = 10**np.random.uniform(0, 1) + offset
88    pars = dict(
89        #background=0,
90        scale=scale,
91        slope=slope,
92        intercept=intercept,
93    )
94    return pars
95
96tests = [
97    [{'intercept': 1.0, 'slope': 1.0, }, 1.0, 2.001],
98    [{'intercept': 1.0, 'slope': 1.0, }, 0.0, 1.001],
99    [{'intercept': 1.0, 'slope': 1.0, }, 0.4, 1.401],
100    [{'intercept': 1.0, 'slope': 1.0, }, 1.3, 2.301],
101    [{'intercept': 1.0, 'slope': 1.0, }, 0.5, 1.501],
102    [{'intercept': 1.0, 'slope': 1.0, }, [0.4, 0.5], [1.401, 1.501]],
103    [{'intercept': 1.0, 'slope': 1.0, 'background': 0.0, }, (1.3, 1.57), 5.911],
104]
Note: See TracBrowser for help on using the repository browser.