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