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 | from sans.models.pluginmodel import Model1DPlugin ##DO NOT CHANGE THIS LINE!!! |
---|
9 | |
---|
10 | ## FOR MORE INFORMATION CHECK http://docs.python.org/library/math.html |
---|
11 | ## AND http://www.scipy.org/Numpy_Functions_by_Category |
---|
12 | import math ##DO NOT CHANGE THIS LINE!!! |
---|
13 | import numpy ##DO NOT CHANGE THIS LINE!!! |
---|
14 | |
---|
15 | ## <----- SIGN DEFINES WHERE YOU CAN MODIFY THE CODE |
---|
16 | |
---|
17 | class Model(Model1DPlugin): ##DO NOT CHANGE THIS LINE!!! |
---|
18 | """ |
---|
19 | ##YOU CAN BE MODIFY ANYTHING BETWEEN """ """ |
---|
20 | ##DESCRIPTION OF MODEL PLUG-IN GOES HERE |
---|
21 | |
---|
22 | ##EXAMPLE: Class that evaluates a polynomial model. |
---|
23 | """ |
---|
24 | ## YOU CAN MODIFY THE LINE BELLOW. CHANGE ONLY WORDS BETWEEN " " |
---|
25 | ## TO RENAME YOUR MODEL: THIS NAME IS WHAT YOU SEE ON GUI. |
---|
26 | name = "polynomial5" ## <----- NAME OF THE MODEL |
---|
27 | |
---|
28 | def __init__(self): ##DO NOT CHANGE THIS LINE!!! |
---|
29 | """ |
---|
30 | Initialization |
---|
31 | """ |
---|
32 | Model1DPlugin.__init__(self, name=self.name) ##DO NOT CHANGE THIS LINE!!! |
---|
33 | |
---|
34 | ## HERE WE DEFINE THE PARAM NAME AND ITS INITIAL VALUE |
---|
35 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
36 | self.params['A'] = 0.1 ## <----- |
---|
37 | self.params['B'] = 10.0 ## <----- |
---|
38 | self.params['C'] = 0.0 ## <----- |
---|
39 | self.params['D'] = 0.0 ## <----- |
---|
40 | self.params['E'] = 0.0 ## <----- |
---|
41 | self.params['F'] = 0.0 ## <----- |
---|
42 | |
---|
43 | ## DEFINE DEFAULT DETAILS |
---|
44 | self.set_details() ##DO NOT DELETE OR CHANGE THIS LINE!!! |
---|
45 | |
---|
46 | ## IN THIS EXAMPLE THE FUNTION IS: |
---|
47 | ## F(x)=A+B*x+C*x^2+D*x^3+E*x^4+F*x^5 |
---|
48 | |
---|
49 | def function(self, x = 0.0): ##DO NOT CHANGE THIS LINE!!! |
---|
50 | """ |
---|
51 | Evaluate the model |
---|
52 | :param x: input x |
---|
53 | :return: function value |
---|
54 | """ |
---|
55 | ## DEFINE YOUR FUNCTION HERE. |
---|
56 | ## YOU CAN ERASE EVERYTHING BELLOW FOR YOUR OWN FUNCTION |
---|
57 | #Redefine parameters as local parameters, or skip and use long name |
---|
58 | a = self.params['A'] ## <----- |
---|
59 | b = self.params['B'] ## <----- |
---|
60 | c = self.params['C'] ## <----- |
---|
61 | d = self.params['D'] ## <----- |
---|
62 | e = self.params['E'] ## <----- |
---|
63 | f = self.params['F'] ## <----- |
---|
64 | |
---|
65 | ##THIS OUR FUNCTION TEMPLATE |
---|
66 | poly = a + b * x + c * math.pow(x,2) + d * math.pow(x,3) \ |
---|
67 | + e * math.pow(x,4) + f * math.pow(x,5) ## <----- |
---|
68 | |
---|
69 | #(Just note: In Python, indentation defines the belongings |
---|
70 | # of 'if', 'for', and so on..) |
---|
71 | #(lim x --> 0) |
---|
72 | if x == 0: ## <----- |
---|
73 | result = a ## <----- |
---|
74 | else: ## <----- |
---|
75 | result = poly ## <----- |
---|
76 | |
---|
77 | return result ## MODIFY ONLY RESULT. DON'T DELETE RETURN!!!! |
---|
78 | |
---|
79 | ############################################################################### |
---|
80 | ## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
81 | if __name__ == "__main__": |
---|
82 | m= Model() |
---|
83 | out1 = m.runXY(0.0) |
---|
84 | out2 = m.runXY(0.01) |
---|
85 | isfine1 = numpy.isfinite(out1) |
---|
86 | isfine2 = numpy.isfinite(out2) |
---|
87 | print "Testing the value at Q = 0.0:" |
---|
88 | print out1, " : finite? ", isfine1 |
---|
89 | print "Testing the value at Q = 0.01:" |
---|
90 | print out2, " : finite? ", isfine2 |
---|
91 | if isfine1 and isfine2: |
---|
92 | print "===> Simple Test: Passed!" |
---|
93 | else: |
---|
94 | print "===> Simple Test: Failed!" |
---|