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