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 | from math import * ##DO NOT CHANGE THIS LINE!!! |
---|
19 | from numpy import * ##DO NOT CHANGE THIS LINE!!! |
---|
20 | |
---|
21 | ##PLEASE READ COMMENTS CAREFULLY !!! COMMENT ARE IN CAPITAL LETTERS AND AFTER ## |
---|
22 | ## THESE COMMENTS ARE THERE TO GUIDE YOU. YOU CAN REMOVE THEM ONLY WHEN YOU ARE |
---|
23 | ## CONFORTABLE ENOUGH WITH OUR MODEL PLUGIN OPTION |
---|
24 | |
---|
25 | |
---|
26 | ## <----- SIGN DEFINES WHERE YOU CAN MODIFY THE CODE |
---|
27 | |
---|
28 | class Model(Model1DPlugin): ##DO NOT CHANGE THIS LINE!!! |
---|
29 | """ |
---|
30 | ##YOU CAN BE MODIFY ANYTHING BETWEEN """ """ |
---|
31 | ##DESCRIPTION OF MODEL PLUG-IN GOES HERE |
---|
32 | |
---|
33 | ##EXAMPLE: Class that evaluates a polynomial model. |
---|
34 | """ |
---|
35 | ## YOU CAN MODIFY THE LINE BELLOW. CHANGE ONLY WORDS BETWEEN " " |
---|
36 | ## TO RENAME YOUR MODEL |
---|
37 | name = "sin(poly)/(poly)" ## <----- NAME OF THE MODEL |
---|
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 | |
---|
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 | if __name__ == "__main__": |
---|
153 | m= Model() |
---|
154 | out1 = m.runXY(0.0) |
---|
155 | out2 = m.runXY(0.01) |
---|
156 | isfine1 = numpy.isfinite(out1) |
---|
157 | isfine2 = numpy.isfinite(out2) |
---|
158 | print "Testing the value at Q = 0.0:" |
---|
159 | print out1, " : finite? ", isfine1 |
---|
160 | print "Testing the value at Q = 0.01:" |
---|
161 | print out2, " : finite? ", isfine2 |
---|
162 | if isfine1 and isfine2: |
---|
163 | print "===> Simple Test: Passed!" |
---|
164 | else: |
---|
165 | print "===> Simple Test: Failed!" |
---|