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 SasView. |
---|
14 | ## ***************************************************************************** |
---|
15 | """ |
---|
16 | |
---|
17 | from sas.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 | import os |
---|
22 | import sys |
---|
23 | ##PLEASE READ COMMENTS CAREFULLY !!! COMMENT ARE IN CAPITAL LETTERS AND AFTER ## |
---|
24 | ## THESE COMMENTS ARE THERE TO GUIDE YOU. YOU CAN REMOVE THEM ONLY WHEN YOU ARE |
---|
25 | ## CONFORTABLE ENOUGH WITH OUR MODEL PLUGIN OPTION |
---|
26 | |
---|
27 | |
---|
28 | ## <----- SIGN DEFINES WHERE YOU CAN MODIFY THE CODE |
---|
29 | |
---|
30 | class Model(Model1DPlugin): ##DO NOT CHANGE THIS LINE!!! |
---|
31 | """ |
---|
32 | ##YOU CAN BE MODIFY ANYTHING BETWEEN """ """ |
---|
33 | ##DESCRIPTION OF MODEL PLUG-IN GOES HERE |
---|
34 | |
---|
35 | ##EXAMPLE: Class that evaluates a polynomial model. |
---|
36 | """ |
---|
37 | name = "" |
---|
38 | |
---|
39 | def __init__(self): ##DO NOT CHANGE THIS LINE!!! |
---|
40 | """ |
---|
41 | Initialization |
---|
42 | """ |
---|
43 | Model1DPlugin.__init__(self, name=self.name) ##DO NOT CHANGE THIS LINE!!! |
---|
44 | |
---|
45 | ## EDIT PARAMETERS' NAMES AND VALUE |
---|
46 | ## DELETE MODIFIABLE LINE HERE WILL REDUCE THE NUMBER OF PARAMETERS |
---|
47 | self.params = {} ##DO NOT CHANGE THIS LINE!!! |
---|
48 | # Set the name same as the file name |
---|
49 | self.name = self.get_fname() ##DO NOT CHANGE THIS LINE!!! |
---|
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 | ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
116 | def get_fname(self): |
---|
117 | """ |
---|
118 | Get the model name same as the file name |
---|
119 | """ |
---|
120 | path = sys._getframe().f_code.co_filename |
---|
121 | basename = os.path.basename(path) |
---|
122 | name, _ = os.path.splitext(basename) |
---|
123 | return name |
---|
124 | |
---|
125 | ############################################################################### |
---|
126 | ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
127 | if __name__ == "__main__": |
---|
128 | m= Model() |
---|
129 | out1 = m.runXY(0.0) |
---|
130 | out2 = m.runXY(0.01) |
---|
131 | isfine1 = numpy.isfinite(out1) |
---|
132 | isfine2 = numpy.isfinite(out2) |
---|
133 | print "Testing the value at Q = 0.0:" |
---|
134 | print out1, " : finite? ", isfine1 |
---|
135 | print "Testing the value at Q = 0.01:" |
---|
136 | print out2, " : finite? ", isfine2 |
---|
137 | if isfine1 and isfine2: |
---|
138 | print "===> Simple Test: Passed!" |
---|
139 | else: |
---|
140 | print "===> Simple Test: Failed!" |
---|