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 os |
---|
21 | import sys |
---|
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 | name = "" |
---|
37 | |
---|
38 | def __init__(self): ##DO NOT CHANGE THIS LINE!!! |
---|
39 | """ |
---|
40 | Initialization |
---|
41 | """ |
---|
42 | Model1DPlugin.__init__(self, name=self.name) ##DO NOT CHANGE THIS LINE!!! |
---|
43 | |
---|
44 | ## EDIT PARAMETERS' NAMES AND VALUE |
---|
45 | ## DELETE MODIFIABLE LINE HERE WILL REDUCE THE NUMBER OF PARAMETERS |
---|
46 | self.params = {} ##DO NOT CHANGE THIS LINE!!! |
---|
47 | # Set the name same as the file name |
---|
48 | self.name = self.get_fname() ##DO NOT CHANGE THIS LINE!!! |
---|
49 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
50 | ## YOU CAN ALSO DELETE THIS LINE |
---|
51 | self.params['scale'] = 1.0 ## <----- |
---|
52 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
53 | ## YOU CAN ALSO DELETE THIS LINE |
---|
54 | self.params['A'] = 0.0 ## <----- |
---|
55 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
56 | ## YOU CAN ALSO DELETE THIS LINE |
---|
57 | self.params['B'] = 10.0 ## <----- |
---|
58 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
59 | ## YOU CAN ALSO DELETE THIS LINE |
---|
60 | self.params['C'] = 0.0 ## <----- |
---|
61 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
62 | ## YOU CAN ALSO DELETE THIS LINE |
---|
63 | self.params['D'] = 0.0 ## <----- |
---|
64 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
65 | ## YOU CAN ALSO DELETE THIS LINE |
---|
66 | self.params['E'] = 0.0 ## <----- |
---|
67 | ## YOU CAN MODIFY THELINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
68 | ## YOU CAN ALSO DELETE THIS LINE |
---|
69 | self.params['F'] = 0.0 ## <----- |
---|
70 | |
---|
71 | ## STORING PARAMETERS [UNIT, MINIMUM VALUE, MAXIMUM VALUE] |
---|
72 | self.details = {} ##DO NOT CHANGE THIS LINE!!! |
---|
73 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN |
---|
74 | ## ' ', TWO OTHER NUMBESR TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE |
---|
75 | self.details['scale'] = ['',None, None] ## <----- |
---|
76 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN |
---|
77 | ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE |
---|
78 | self.details['A'] = ['', None, None] ## <----- |
---|
79 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN |
---|
80 | ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE |
---|
81 | self.details['B'] = ['', None, None] ## <----- |
---|
82 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN |
---|
83 | ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE |
---|
84 | self.details['C'] = ['', None, None] ## <----- |
---|
85 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN |
---|
86 | ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE |
---|
87 | self.details['D'] = ['', None, None] ## <----- |
---|
88 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN |
---|
89 | ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE |
---|
90 | self.details['E'] = ['', None, None] ## <----- |
---|
91 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ',WORD BETWEEN |
---|
92 | ## ' ', TWO OTHER NUMBERS TO NEW VALUE OR YOU CAN ALSO DELETE TH LINE |
---|
93 | self.details['F'] = ['', 0, 1e16] ## <----- |
---|
94 | ## YOU CAN MODIFY THE LINE BELLOW.MODIFY WORDS BETWEEN """ """ ONLY!!!! |
---|
95 | self.description = """ |
---|
96 | scale * sin(F(x)/F(x)) \n where F(x)=A+B*x+C*x^2+D*x^3+E*x^4+F*x^5 |
---|
97 | """ ## <----- |
---|
98 | |
---|
99 | def function(self, x = 0.0): ##DO NOT CHANGE THIS LINE!!! |
---|
100 | """ |
---|
101 | Evaluate the model |
---|
102 | |
---|
103 | :param x: input x |
---|
104 | |
---|
105 | :return: function value |
---|
106 | |
---|
107 | """ |
---|
108 | ## ADD YOUR FUNCTION HERE. |
---|
109 | ## REUSE THE PARAMETERS DEFINED PREVIOUSLY TO WRITE YOUR FUNCTION. |
---|
110 | |
---|
111 | ## IN THIS EXAMPLE THE FUNTION IS: |
---|
112 | ## scale * sin(F(x)/F(x)) \n where F(x)=A+B*x+C*x^2+D*x^3+E*x^4+F*x^5 |
---|
113 | |
---|
114 | ## YOU CAN REWRITE OUR EXAMPLE WITH EVERYTING INSIDE " " : |
---|
115 | ## "RETURN self.params['scale']* (self.params['A'] + self.params['B']*x + \ |
---|
116 | ## self.params['C']* pow(x,2) + self.params['D']*pow(x,3)+\ |
---|
117 | ## self.params['E']*pow(x,4) +self.params['F']*pow(x,5) ) " |
---|
118 | |
---|
119 | ## NOTE: pow IS A FUNCTION IMPORTED FROM PYTHON MATH LIBRARY |
---|
120 | ## FOR MORE INFORMATION CHECK http://docs.python.org/library/math.html |
---|
121 | ## OTHER FUNCTIONS ARE ALSO |
---|
122 | ### AVAILABLE http://www.scipy.org/Numpy_Functions_by_Category |
---|
123 | ## numpy FUNCTIONS ARE FOR EXPERT USER |
---|
124 | |
---|
125 | |
---|
126 | ## YOU CAN ERASE EVERYTHING BELLOW FOR YOUR OWN FUNCTION |
---|
127 | #Redefine parameters as local parameters |
---|
128 | a = self.params['A'] ## <----- |
---|
129 | b = self.params['B'] ## <----- |
---|
130 | c = self.params['C'] ## <----- |
---|
131 | d = self.params['D'] ## <----- |
---|
132 | e = self.params['E'] ## <----- |
---|
133 | f = self.params['F'] ## <----- |
---|
134 | scl = self.params['scale'] ## <----- |
---|
135 | ##THIS OUR FUNCTION TEMPLATE |
---|
136 | poly = a + b*x + c*math.pow(x,2) + d*math.pow(x,3) \ |
---|
137 | + e*math.pow(x,4) +f*math.pow(x,5) ## <----- |
---|
138 | |
---|
139 | #Remove a singular point (lim poly --> 0) for sin(poly)/poly |
---|
140 | #(Just note: In Python, indentation defines the belongings of 'if', 'for', and so on..) |
---|
141 | if poly == 0: ## <----- |
---|
142 | result = 1 ## <----- |
---|
143 | else: ## <----- |
---|
144 | result = math.sin(poly)/poly ## <----- |
---|
145 | |
---|
146 | #Re-scale ## <----- |
---|
147 | result *=scl ## <----- |
---|
148 | |
---|
149 | return result ## MODIFY ONLY RESULT. DON'T DELETE RETURN!!!! |
---|
150 | |
---|
151 | ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
152 | def get_fname(self): |
---|
153 | """ |
---|
154 | Get the model name same as the file name |
---|
155 | """ |
---|
156 | path = sys._getframe().f_code.co_filename |
---|
157 | basename = os.path.basename(path) |
---|
158 | name, _ = os.path.splitext(basename) |
---|
159 | return name |
---|
160 | |
---|
161 | ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
162 | if __name__ == "__main__": |
---|
163 | m= Model() |
---|
164 | out1 = m.runXY(0.0) |
---|
165 | out2 = m.runXY(0.01) |
---|
166 | isfine1 = numpy.isfinite(out1) |
---|
167 | isfine2 = numpy.isfinite(out2) |
---|
168 | print "Testing the value at Q = 0.0:" |
---|
169 | print out1, " : finite? ", isfine1 |
---|
170 | print "Testing the value at Q = 0.01:" |
---|
171 | print out2, " : finite? ", isfine2 |
---|
172 | if isfine1 and isfine2: |
---|
173 | print "===> Simple Test: Passed!" |
---|
174 | else: |
---|
175 | print "===> Simple Test: Failed!" |
---|