File: power_law.py

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (72 lines) | stat: -rw-r--r-- 1,772 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#power_law model
#conversion of PowerLawAbsModel.py
#converted by Steve King, Dec 2015

r"""
This model calculates a simple power law with a flat background.

Definition
----------

.. math::

    I(q) = \text{scale} \cdot q^{-\text{power}} + \text{background}

Note the minus sign in front of the exponent. The exponent *power*
should therefore be entered as a **positive** number for fitting.

Also note that unlike many other models, *scale* in this model
is NOT explicitly related to a volume fraction. Be careful if
combining this model with other models.


References
----------

None.

Authorship and Verification
----------------------------

* **Author:**
* **Last Modified by:**
* **Last Reviewed by:**
"""

import numpy as np
from numpy import inf, errstate

name = "power_law"
title = "Simple power law with a flat background"

description = """
    Evaluates the function
    I(q) = scale * q^(-power) + background
    NB: enter power as a positive number!
    """
category = "shape-independent"

#             ["name", "units", default, [lower, upper], "type", "description"],
parameters = [["power", "", 4.0, [-inf, inf], "", "Power law exponent"]]

# NB: Scale and Background are implicit parameters on every model
def Iq(q, power):
    # pylint: disable=missing-docstring
    with errstate(divide='ignore'):
        result = q**-power
    return result
Iq.vectorized = True  # Iq accepts an array of q values

def random():
    """Return a random parameter set for the model."""
    power = np.random.uniform(1, 6)
    pars = dict(
        scale=0.1**power*10**np.random.uniform(-4, 2),
        power=power,
    )
    return pars

tests = [
    [{'scale': 1.0, 'power': 4.0, 'background' : 0.0},
     [0.0106939, 0.469418], [7.64644e+07, 20.5949]],
    ]