[1b162dfa] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[79ac6f8] | 3 | ############################################################################ |
---|
| 4 | #This software was developed by the University of Tennessee as part of the |
---|
| 5 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 6 | #project funded by the US National Science Foundation. |
---|
| 7 | # |
---|
| 8 | #If you use DANSE applications to do scientific research that leads to |
---|
| 9 | #publication, we ask that you acknowledge the use of the software with the |
---|
| 10 | #following sentence: |
---|
| 11 | # |
---|
| 12 | #"This work benefited from DANSE software developed |
---|
| 13 | # under NSF award DMR-0520547." |
---|
| 14 | # |
---|
| 15 | #copyright 2008, University of Tennessee |
---|
| 16 | ############################################################################# |
---|
[1b162dfa] | 17 | |
---|
[79ac6f8] | 18 | """ |
---|
| 19 | Provide base functionality for all model components |
---|
[1b162dfa] | 20 | |
---|
[79ac6f8] | 21 | The following has changed since going from BaseComponent to BaseModel: |
---|
[1b162dfa] | 22 | |
---|
[79ac6f8] | 23 | - Arithmetic operation between models is no longer supported. |
---|
| 24 | It was found to be of little use and not very flexible. |
---|
[1b162dfa] | 25 | |
---|
[79ac6f8] | 26 | - Parameters are now stored as Parameter object to provide |
---|
| 27 | the necessary extra information like limits, units, etc... |
---|
[1b162dfa] | 28 | """ |
---|
| 29 | |
---|
| 30 | # imports |
---|
| 31 | import copy |
---|
| 32 | |
---|
| 33 | class Parameter(object): |
---|
| 34 | """ |
---|
[79ac6f8] | 35 | Parameter class |
---|
[1b162dfa] | 36 | """ |
---|
| 37 | name = '' |
---|
| 38 | value = 0.0 |
---|
| 39 | def __init__(self, name, value): |
---|
| 40 | self.name = name |
---|
| 41 | self.value = value |
---|
| 42 | |
---|
[8809e48] | 43 | def __str__(self): |
---|
| 44 | return "%s: %g" % (self.name, self.value) |
---|
[1b162dfa] | 45 | |
---|
| 46 | class ParameterProperty(object): |
---|
| 47 | """ |
---|
[79ac6f8] | 48 | Parameter property allow direct access to |
---|
| 49 | the parameter values |
---|
[1b162dfa] | 50 | """ |
---|
| 51 | def __init__(self,name,**kw): |
---|
| 52 | self.name = name |
---|
| 53 | |
---|
| 54 | def __get__(self,instance,cls): |
---|
| 55 | return instance.parameters[self.name].value |
---|
| 56 | |
---|
| 57 | def __set__(self,instance,value): |
---|
| 58 | instance.parameters[self.name].value = value |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | from ModelAdaptor import ModelAdaptor |
---|
| 63 | |
---|
| 64 | class BaseModel(ModelAdaptor): |
---|
| 65 | """ |
---|
[79ac6f8] | 66 | Basic model component |
---|
[1b162dfa] | 67 | """ |
---|
| 68 | ## Name of the model |
---|
| 69 | name = "BaseModel" |
---|
[ae60f86] | 70 | |
---|
| 71 | def __init__(self): |
---|
| 72 | ## Dictionary of Parameter objects |
---|
| 73 | self.parameters = {} |
---|
[1b162dfa] | 74 | |
---|
| 75 | # Evaluation methods to be implemented by the models |
---|
| 76 | def run(self, x=0): return NotImplemented |
---|
| 77 | def runXY(self, x=0): return NotImplemented |
---|
[f9bf661] | 78 | def calculate_ER(self): return NotImplemented |
---|
[1b162dfa] | 79 | def __call__(self, x=0): |
---|
| 80 | """ |
---|
[79ac6f8] | 81 | Evaluate the model. Equivalent to runXY(x) |
---|
| 82 | |
---|
| 83 | :param x: input value |
---|
| 84 | |
---|
| 85 | :return: value of the model |
---|
| 86 | |
---|
[1b162dfa] | 87 | """ |
---|
| 88 | return self.runXY(x) |
---|
| 89 | |
---|
| 90 | def clone(self): |
---|
| 91 | """ Returns a new object identical to the current object """ |
---|
| 92 | |
---|
| 93 | obj = copy.deepcopy(self) |
---|
| 94 | obj.params = copy.deepcopy(self.params) |
---|
| 95 | obj.details = copy.deepcopy(self.details) |
---|
| 96 | return obj |
---|
| 97 | |
---|
| 98 | def setParam(self, name, value): |
---|
| 99 | """ |
---|
[79ac6f8] | 100 | Set the value of a model parameter |
---|
| 101 | |
---|
| 102 | :param name: name of the parameter |
---|
| 103 | :param value: value of the parameter |
---|
[1b162dfa] | 104 | |
---|
| 105 | """ |
---|
| 106 | # Lowercase for case insensitivity |
---|
| 107 | name = name.lower() |
---|
[ae60f86] | 108 | if name in self.parameters: |
---|
| 109 | print "found" |
---|
[8809e48] | 110 | #self.parameters[name].value = value |
---|
[ae60f86] | 111 | return object.__setattr__(self, name, value) |
---|
[1b162dfa] | 112 | |
---|
| 113 | def getParam(self, name): |
---|
| 114 | """ |
---|
[79ac6f8] | 115 | Set the value of a model parameter |
---|
[1b162dfa] | 116 | |
---|
[79ac6f8] | 117 | :param name: name of the parameter |
---|
| 118 | :param value: value of the parameter |
---|
| 119 | |
---|
[1b162dfa] | 120 | """ |
---|
| 121 | # Lowercase for case insensitivity |
---|
| 122 | name = name.lower() |
---|
| 123 | return object.__getattribute__(self, name) |
---|
| 124 | |
---|
| 125 | def getParamList(self): |
---|
| 126 | """ |
---|
[79ac6f8] | 127 | Return a list of all available parameters for the model |
---|
[1b162dfa] | 128 | """ |
---|
| 129 | param_list = self.params.keys() |
---|
| 130 | return param_list |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | if __name__ == "__main__": |
---|
| 134 | b = BaseModel() |
---|
[c05234f] | 135 | #print b.operateOn |
---|
[1b162dfa] | 136 | |
---|