1 | """ |
---|
2 | Test plug-in model |
---|
3 | These are links of available functions: |
---|
4 | |
---|
5 | http://docs.python.org/library/math.html |
---|
6 | http://www.scipy.org/Numpy_Functions_by_Category |
---|
7 | """ |
---|
8 | ## ***************************************************************************** |
---|
9 | ## For safety of your model, saving as a different file name is recommended. |
---|
10 | ## Otherwise, it could be removed in the future re-installation of the SansView. |
---|
11 | ## ***************************************************************************** |
---|
12 | |
---|
13 | from sans.models.pluginmodel import Model1DPlugin ##DO NOT CHANGE THIS LINE!!! |
---|
14 | import math ##DO NOT CHANGE THIS LINE!!! |
---|
15 | import numpy ##DO NOT CHANGE THIS LINE!!! |
---|
16 | import scipy.special ##CHANGE THIS LINE WITH CAUTION!!! |
---|
17 | |
---|
18 | ##PLEASE READ COMMENTS CAREFULLY !!! COMMENT ARE IN CAPITAL LETTERS AND AFTER ## |
---|
19 | ## THESE COMMENTS ARE THERE TO GUIDE YOU. YOU CAN REMOVE THEM ONLY WHEN YOU ARE |
---|
20 | ## CONFORTABLE ENOUGH WITH OUR MODEL PLUGIN OPTION |
---|
21 | |
---|
22 | |
---|
23 | ## <----- SIGN DEFINES WHERE YOU CAN MODIFY THE CODE |
---|
24 | |
---|
25 | class Model(Model1DPlugin): ##DO NOT CHANGE THIS LINE!!! |
---|
26 | """ |
---|
27 | ##YOU CAN BE MODIFY ANYTHING BETWEEN """ """ |
---|
28 | ##DESCRIPTION OF MODEL PLUG-IN GOES HERE |
---|
29 | |
---|
30 | ##EXAMPLE: Class that evaluates a polynomial model. |
---|
31 | """ |
---|
32 | ## YOU CAN MODIFY THE LINE BELLOW. CHANGE ONLY WORDS BETWEEN " " |
---|
33 | ## TO RENAME YOUR MODEL |
---|
34 | name = "C*sph_jn(Ax+B)+D" ## <----- NAME OF THE MODEL |
---|
35 | |
---|
36 | def __init__(self): ##DO NOT CHANGE THIS LINE!!! |
---|
37 | """ |
---|
38 | Initialization |
---|
39 | """ |
---|
40 | Model1DPlugin.__init__(self, name=self.name) ##DO NOT CHANGE THIS LINE!!! |
---|
41 | |
---|
42 | ## EDIT PARAMETERS' NAMES AND VALUE |
---|
43 | ## DELETE MODIFIABLE LINE HERE WILL REDUCE THE NUMBER OF PARAMETERS |
---|
44 | self.params = {} ##DO NOT CHANGE THIS LINE!!! |
---|
45 | |
---|
46 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
47 | ## YOU CAN ALSO DELETE THIS LINE |
---|
48 | self.params['C'] = 1.0 ## <----- |
---|
49 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
50 | ## YOU CAN ALSO DELETE THIS LINE |
---|
51 | self.params['A'] = 1.0 ## <----- |
---|
52 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
53 | ## YOU CAN ALSO DELETE THIS LINE |
---|
54 | self.params['B'] = 0.0 ## <----- |
---|
55 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
56 | ## YOU CAN ALSO DELETE THIS LINE |
---|
57 | self.params['D'] = 0.0 ## <----- |
---|
58 | ## YOU CAN ALSO DELETE THIS LINE |
---|
59 | self.params['n'] = 1 ## <----- |
---|
60 | |
---|
61 | ## DEFINE DEFAULT DETAILS |
---|
62 | self.set_details() ##DO NOT DELETE OR CHANGE THIS LINE!!! |
---|
63 | |
---|
64 | ## YOU CAN MODIFY THE LINE BELLOW.MODIFY WORDS BETWEEN """ """ ONLY!!!! |
---|
65 | self.description = """ |
---|
66 | Spherical Bessel Function: C*sph_jn(n, Ax+B)+D |
---|
67 | """ ## <----- |
---|
68 | |
---|
69 | def function(self, x = 0.0): ##DO NOT CHANGE THIS LINE!!! |
---|
70 | """ |
---|
71 | Evaluate the model |
---|
72 | |
---|
73 | :param x: input x |
---|
74 | |
---|
75 | :return: function value |
---|
76 | |
---|
77 | """ |
---|
78 | ## ADD YOUR FUNCTION HERE. |
---|
79 | ## REUSE THE PARAMETERS DEFINED PREVIOUSLY TO WRITE YOUR FUNCTION. |
---|
80 | |
---|
81 | ## IN THIS EXAMPLE THE FUNTION IS: |
---|
82 | ## C*sph_jn(Ax+B)+D |
---|
83 | |
---|
84 | ## NOTE: pow IS A FUNCTION IMPORTED FROM PYTHON MATH LIBRARY |
---|
85 | ## FOR MORE INFORMATION CHECK http://docs.python.org/library/math.html |
---|
86 | ## OTHER FUNCTIONS ARE ALSO |
---|
87 | ### AVAILABLE http://www.scipy.org/Numpy_Functions_by_Category |
---|
88 | ## numpy FUNCTIONS ARE FOR EXPERT USER |
---|
89 | |
---|
90 | ## YOU CAN ERASE EVERYTHING BELLOW FOR YOUR OWN FUNCTION |
---|
91 | #Redefine parameters as local parameters |
---|
92 | a = self.params['A'] ## <----- |
---|
93 | b = self.params['B'] ## <----- |
---|
94 | c = self.params['C'] ## <----- |
---|
95 | d = self.params['D'] ## <----- |
---|
96 | # Take integer value only |
---|
97 | n = int(self.params['n']) ## <----- |
---|
98 | |
---|
99 | ##THIS OUR FUNCTION TEMPLATE |
---|
100 | #Remove a singular point (lim poly --> 0) for sin(poly)/poly |
---|
101 | #(Just note: In Python, indentation defines the belongings of 'if', 'for', and so on..) |
---|
102 | input = a * x + b |
---|
103 | # sph_out and _ are in array types from scipy upto n'th order |
---|
104 | # where _ is not used for this function. |
---|
105 | sph_out, _ = scipy.special.sph_jn(n, input) |
---|
106 | # Take only n'th value |
---|
107 | result = c * sph_out[n] + d ## <----- |
---|
108 | |
---|
109 | return result ## MODIFY ONLY RESULT. DON'T DELETE RETURN!!!! |
---|
110 | |
---|
111 | ############################################################################### |
---|
112 | ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
113 | if __name__ == "__main__": |
---|
114 | m= Model() |
---|
115 | out1 = m.runXY(0.0) |
---|
116 | out2 = m.runXY(0.01) |
---|
117 | isfine1 = numpy.isfinite(out1) |
---|
118 | isfine2 = numpy.isfinite(out2) |
---|
119 | print "Testing the value at Q = 0.0:" |
---|
120 | print out1, " : finite? ", isfine1 |
---|
121 | print "Testing the value at Q = 0.01:" |
---|
122 | print out2, " : finite? ", isfine2 |
---|
123 | if isfine1 and isfine2: |
---|
124 | print "===> Simple Test: Passed!" |
---|
125 | else: |
---|
126 | print "===> Simple Test: Failed!" |
---|