[7c8d3093] | 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 | """ |
---|
[e9bd127] | 8 | |
---|
| 9 | """ |
---|
[22b1d38] | 10 | ## ***************************************************************************** |
---|
[e9bd127] | 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. |
---|
[b9a5f0e] | 13 | Otherwise, it could be removed in the future on re-installation of the SasView. |
---|
[22b1d38] | 14 | ## ***************************************************************************** |
---|
[e9bd127] | 15 | """ |
---|
[79492222] | 16 | from sas.models.pluginmodel import Model1DPlugin ##DO NOT CHANGE THIS LINE!!! |
---|
[7c8d3093] | 17 | |
---|
[ed8fa6a3] | 18 | ## FOR MORE INFORMATION CHECK http://docs.python.org/library/math.html |
---|
| 19 | ## AND http://www.scipy.org/Numpy_Functions_by_Category |
---|
| 20 | import math ##DO NOT CHANGE THIS LINE!!! |
---|
| 21 | import numpy ##DO NOT CHANGE THIS LINE!!! |
---|
[316e231] | 22 | import os |
---|
| 23 | import sys |
---|
[7c8d3093] | 24 | ## <----- SIGN DEFINES WHERE YOU CAN MODIFY THE CODE |
---|
| 25 | |
---|
| 26 | class Model(Model1DPlugin): ##DO NOT CHANGE THIS LINE!!! |
---|
| 27 | """ |
---|
| 28 | ##YOU CAN BE MODIFY ANYTHING BETWEEN """ """ |
---|
| 29 | ##DESCRIPTION OF MODEL PLUG-IN GOES HERE |
---|
| 30 | |
---|
| 31 | ##EXAMPLE: Class that evaluates a polynomial model. |
---|
| 32 | """ |
---|
[316e231] | 33 | name = "" |
---|
[7c8d3093] | 34 | |
---|
| 35 | def __init__(self): ##DO NOT CHANGE THIS LINE!!! |
---|
| 36 | """ |
---|
| 37 | Initialization |
---|
| 38 | """ |
---|
| 39 | Model1DPlugin.__init__(self, name=self.name) ##DO NOT CHANGE THIS LINE!!! |
---|
| 40 | |
---|
[ed8fa6a3] | 41 | ## HERE WE DEFINE THE PARAM NAME AND ITS INITIAL VALUE |
---|
| 42 | ## YOU CAN MODIFY THE LINE BELLOW.CHANGE WORD BETWEEN ' ' AND NUMBER |
---|
| 43 | self.params['A'] = 0.1 ## <----- |
---|
| 44 | self.params['B'] = 10.0 ## <----- |
---|
| 45 | self.params['C'] = 0.0 ## <----- |
---|
| 46 | self.params['D'] = 0.0 ## <----- |
---|
| 47 | self.params['E'] = 0.0 ## <----- |
---|
| 48 | self.params['F'] = 0.0 ## <----- |
---|
[316e231] | 49 | # Set the name same as the file name |
---|
| 50 | self.name = self.get_fname() ##DO NOT CHANGE THIS LINE!!! |
---|
[19e614a] | 51 | ## YOU CAN MODIFY THE LINE BELLOW.MODIFY WORDS BETWEEN """ """ ONLY!!!! |
---|
| 52 | self.description = """ |
---|
| 53 | a + b * x + c * math.pow(x,2) + d * math.pow(x,3) \n |
---|
| 54 | + e * math.pow(x,4) + f * math.pow(x,5) |
---|
| 55 | """ ## <----- |
---|
[ed8fa6a3] | 56 | ## DEFINE DEFAULT DETAILS |
---|
| 57 | self.set_details() ##DO NOT DELETE OR CHANGE THIS LINE!!! |
---|
| 58 | |
---|
| 59 | ## IN THIS EXAMPLE THE FUNTION IS: |
---|
| 60 | ## F(x)=A+B*x+C*x^2+D*x^3+E*x^4+F*x^5 |
---|
[7c8d3093] | 61 | |
---|
| 62 | def function(self, x = 0.0): ##DO NOT CHANGE THIS LINE!!! |
---|
| 63 | """ |
---|
| 64 | Evaluate the model |
---|
| 65 | :param x: input x |
---|
| 66 | :return: function value |
---|
| 67 | """ |
---|
[ed8fa6a3] | 68 | ## DEFINE YOUR FUNCTION HERE. |
---|
[7c8d3093] | 69 | ## YOU CAN ERASE EVERYTHING BELLOW FOR YOUR OWN FUNCTION |
---|
[ed8fa6a3] | 70 | #Redefine parameters as local parameters, or skip and use long name |
---|
[7c8d3093] | 71 | a = self.params['A'] ## <----- |
---|
| 72 | b = self.params['B'] ## <----- |
---|
| 73 | c = self.params['C'] ## <----- |
---|
| 74 | d = self.params['D'] ## <----- |
---|
| 75 | e = self.params['E'] ## <----- |
---|
[ed8fa6a3] | 76 | f = self.params['F'] ## <----- |
---|
[7c8d3093] | 77 | |
---|
| 78 | ##THIS OUR FUNCTION TEMPLATE |
---|
[ed8fa6a3] | 79 | poly = a + b * x + c * math.pow(x,2) + d * math.pow(x,3) \ |
---|
| 80 | + e * math.pow(x,4) + f * math.pow(x,5) ## <----- |
---|
[7c8d3093] | 81 | |
---|
[ed8fa6a3] | 82 | #(Just note: In Python, indentation defines the belongings |
---|
| 83 | # of 'if', 'for', and so on..) |
---|
[7c8d3093] | 84 | #(lim x --> 0) |
---|
[ed8fa6a3] | 85 | if x == 0: ## <----- |
---|
[7c8d3093] | 86 | result = a ## <----- |
---|
| 87 | else: ## <----- |
---|
[ed8fa6a3] | 88 | result = poly ## <----- |
---|
[7c8d3093] | 89 | |
---|
[ed8fa6a3] | 90 | return result ## MODIFY ONLY RESULT. DON'T DELETE RETURN!!!! |
---|
[316e231] | 91 | |
---|
| 92 | ## DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
| 93 | def get_fname(self): |
---|
| 94 | """ |
---|
| 95 | Get the model name same as the file name |
---|
| 96 | """ |
---|
| 97 | path = sys._getframe().f_code.co_filename |
---|
| 98 | basename = os.path.basename(path) |
---|
| 99 | name, _ = os.path.splitext(basename) |
---|
| 100 | return name |
---|
| 101 | |
---|
[ed8fa6a3] | 102 | ############################################################################### |
---|
| 103 | ## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
[7c8d3093] | 104 | if __name__ == "__main__": |
---|
| 105 | m= Model() |
---|
| 106 | out1 = m.runXY(0.0) |
---|
| 107 | out2 = m.runXY(0.01) |
---|
| 108 | isfine1 = numpy.isfinite(out1) |
---|
| 109 | isfine2 = numpy.isfinite(out2) |
---|
| 110 | print "Testing the value at Q = 0.0:" |
---|
| 111 | print out1, " : finite? ", isfine1 |
---|
| 112 | print "Testing the value at Q = 0.01:" |
---|
| 113 | print out2, " : finite? ", isfine2 |
---|
| 114 | if isfine1 and isfine2: |
---|
| 115 | print "===> Simple Test: Passed!" |
---|
| 116 | else: |
---|
[b9a5f0e] | 117 | print "===> Simple Test: Failed!" |
---|