[1c66bc5] | 1 | """ |
---|
| 2 | Test plug-in model |
---|
| 3 | """ |
---|
[b30f001] | 4 | #try: |
---|
| 5 | # from DataPlugin import * |
---|
| 6 | #except: |
---|
| 7 | # import sys, os |
---|
| 8 | # print os.path.abspath('..') |
---|
| 9 | # sys.path.insert(1,os.path.abspath('..')) |
---|
| 10 | # from DataPlugin import * |
---|
| 11 | # print "Running independently" |
---|
| 12 | # |
---|
| 13 | import math |
---|
| 14 | from sans.models.BaseComponent import BaseComponent |
---|
[1c66bc5] | 15 | import math |
---|
[b30f001] | 16 | class Model1DPlugin(BaseComponent): |
---|
| 17 | ## Name of the model |
---|
| 18 | name = "Plugin Model" |
---|
| 19 | |
---|
| 20 | def __init__(self): |
---|
| 21 | """ Initialization """ |
---|
| 22 | self.details = {} |
---|
| 23 | self.params = {} |
---|
| 24 | |
---|
| 25 | def function(self, x): |
---|
| 26 | """ |
---|
| 27 | Function to be implemented by the plug-in writer |
---|
| 28 | """ |
---|
| 29 | return x |
---|
| 30 | |
---|
| 31 | def run(self, x = 0.0): |
---|
| 32 | """ Evaluate the model |
---|
| 33 | @param x: input x, or [x, phi] [radian] |
---|
| 34 | @return: function value |
---|
| 35 | """ |
---|
| 36 | if x.__class__.__name__ == 'list': |
---|
| 37 | x_val = x[0]*math.cos(x[1]) |
---|
| 38 | y_val = x[0]*math.sin(x[1]) |
---|
| 39 | return self.function(x_val)*self.function(y_val) |
---|
| 40 | elif x.__class__.__name__ == 'tuple': |
---|
| 41 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 42 | else: |
---|
| 43 | return self.function(x) |
---|
| 44 | |
---|
| 45 | def runXY(self, x = 0.0): |
---|
| 46 | """ Evaluate the model |
---|
| 47 | @param x: input x, or [x, y] |
---|
| 48 | @return: function value |
---|
| 49 | """ |
---|
| 50 | if x.__class__.__name__ == 'list': |
---|
| 51 | return self.function(x[0])*self.function(x[1]) |
---|
| 52 | elif x.__class__.__name__ == 'tuple': |
---|
| 53 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 54 | else: |
---|
| 55 | return self.function(x) |
---|
[1c66bc5] | 56 | |
---|
| 57 | # Your model HAS to be called Model |
---|
| 58 | class Model(Model1DPlugin): |
---|
| 59 | """ Class that evaluates a cos(x) model. |
---|
| 60 | """ |
---|
| 61 | |
---|
| 62 | ## Name of the model |
---|
| 63 | name = "A+Bcos(2x)+Csin(2x)" |
---|
| 64 | |
---|
| 65 | def __init__(self): |
---|
| 66 | """ Initialization """ |
---|
[b30f001] | 67 | |
---|
[1c66bc5] | 68 | |
---|
| 69 | ## Parameters definition and defaults |
---|
| 70 | self.params = {} |
---|
| 71 | self.params['A'] = 0.0 |
---|
| 72 | self.params['B'] = 1.0 |
---|
| 73 | self.params['C'] = 0.0 |
---|
| 74 | |
---|
| 75 | ## Parameter details [units, min, max] |
---|
| 76 | self.details = {} |
---|
| 77 | self.details['A'] = ['', -1e16, 1e16] |
---|
| 78 | self.details['B'] = ['', -1e16, 1e16] |
---|
| 79 | self.details['C'] = ['', -1e16, 1e16] |
---|
| 80 | |
---|
| 81 | def function(self, x = 0.0): |
---|
| 82 | """ Evaluate the model |
---|
| 83 | @param x: input x |
---|
| 84 | @return: function value |
---|
| 85 | """ |
---|
| 86 | return self.params['A']+self.params['B']*math.cos(2.0*x)+self.params['C']*math.sin(2.0*x) |
---|
| 87 | |
---|