[c9636f7] | 1 | |
---|
| 2 | from sans.models.BaseComponent import BaseComponent |
---|
| 3 | import numpy, math |
---|
| 4 | |
---|
| 5 | class MultiplicationModel(BaseComponent): |
---|
| 6 | """ |
---|
| 7 | Use for S(Q)*P(Q). |
---|
| 8 | Perform multplication of two models. |
---|
| 9 | Contains the models parameters combined. |
---|
| 10 | """ |
---|
| 11 | def __init__(self, model1, model2 ): |
---|
| 12 | BaseComponent.__init__(self) |
---|
| 13 | |
---|
[996fd35] | 14 | |
---|
[8cfdd5e] | 15 | ## Setting model name model description |
---|
[996fd35] | 16 | self.description="" |
---|
| 17 | if model1.name != "NoStructure"and model2.name != "NoStructure": |
---|
| 18 | self.name = model1.name +" * "+ model2.name |
---|
| 19 | self.description= self.name+"\n" |
---|
| 20 | self.description +="see %s description and %s description"%( model1.name, |
---|
| 21 | model2.name ) |
---|
| 22 | elif model2.name != "NoStructure": |
---|
| 23 | self.name = model2.name |
---|
| 24 | self.description= model2.description |
---|
| 25 | else : |
---|
| 26 | self.name = model1.name |
---|
| 27 | self.description= model1.description |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
[c9636f7] | 31 | self.model1= model1 |
---|
| 32 | self.model2= model2 |
---|
| 33 | ## dispersion |
---|
| 34 | self._set_dispersion() |
---|
| 35 | ## Define parameters |
---|
| 36 | self._set_params() |
---|
| 37 | ## Parameter details [units, min, max] |
---|
| 38 | self._set_details() |
---|
| 39 | #list of parameter that can be fitted |
---|
| 40 | self._set_fixed_params() |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | def _set_dispersion(self): |
---|
| 44 | """ |
---|
| 45 | combined the two models dispersions |
---|
| 46 | """ |
---|
| 47 | for name , value in self.model1.dispersion.iteritems(): |
---|
| 48 | self.dispersion[name]= value |
---|
| 49 | |
---|
| 50 | for name , value in self.model2.dispersion.iteritems(): |
---|
| 51 | if not name in self.dispersion.keys(): |
---|
| 52 | self.dispersion[name]= value |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | def _set_params(self): |
---|
| 56 | """ |
---|
| 57 | Concatenate the parameters of the two models to create |
---|
| 58 | this model parameters |
---|
| 59 | """ |
---|
| 60 | for name , value in self.model1.params.iteritems(): |
---|
| 61 | self.params[name]= value |
---|
| 62 | |
---|
| 63 | for name , value in self.model2.params.iteritems(): |
---|
| 64 | if not name in self.params.keys(): |
---|
| 65 | self.params[name]= value |
---|
| 66 | |
---|
| 67 | def _set_details(self): |
---|
| 68 | """ |
---|
| 69 | Concatenate details of the two models to create |
---|
| 70 | this model details |
---|
| 71 | """ |
---|
| 72 | for name ,detail in self.model1.details.iteritems(): |
---|
| 73 | self.details[name]= detail |
---|
| 74 | |
---|
| 75 | for name , detail in self.model2.details.iteritems(): |
---|
| 76 | if not name in self.details.keys(): |
---|
| 77 | self.details[name]= detail |
---|
| 78 | |
---|
[8cfdd5e] | 79 | def setParam(self, name, value): |
---|
| 80 | """ |
---|
| 81 | Set the value of a model parameter |
---|
| 82 | |
---|
| 83 | @param name: name of the parameter |
---|
| 84 | @param value: value of the parameter |
---|
| 85 | """ |
---|
| 86 | self._setParamHelper( name, value) |
---|
| 87 | if name in self.model1.getParamList(): |
---|
| 88 | self.model1.setParam( name, value) |
---|
| 89 | |
---|
| 90 | if name in self.model2.getParamList(): |
---|
| 91 | self.model2.setParam( name, value) |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | def _setParamHelper(self, name, value): |
---|
| 95 | """ |
---|
| 96 | Helper function to setparam |
---|
| 97 | """ |
---|
| 98 | # Look for dispersion parameters |
---|
| 99 | toks = name.split('.') |
---|
| 100 | if len(toks)==2: |
---|
| 101 | for item in self.dispersion.keys(): |
---|
| 102 | if item.lower()==toks[0].lower(): |
---|
| 103 | for par in self.dispersion[item]: |
---|
| 104 | if par.lower() == toks[1].lower(): |
---|
| 105 | self.dispersion[item][par] = value |
---|
| 106 | return |
---|
| 107 | else: |
---|
| 108 | # Look for standard parameter |
---|
| 109 | for item in self.params.keys(): |
---|
| 110 | if item.lower()==name.lower(): |
---|
| 111 | self.params[item] = value |
---|
| 112 | return |
---|
| 113 | |
---|
| 114 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
[c9636f7] | 120 | def _set_fixed_params(self): |
---|
| 121 | """ |
---|
| 122 | fill the self.fixed list with the two models fixed list |
---|
| 123 | """ |
---|
| 124 | for item in self.model1.fixed: |
---|
| 125 | self.fixed.append(item) |
---|
| 126 | |
---|
| 127 | for item in self.model2.fixed: |
---|
| 128 | if not item in self.fixed: |
---|
| 129 | self.fixed.append(item) |
---|
| 130 | self.fixed.sort() |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | def run(self, x = 0.0): |
---|
| 134 | """ Evaluate the model |
---|
| 135 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
| 136 | @return: (DAB value) |
---|
| 137 | """ |
---|
| 138 | return self.model1.run(x)* self.model2.run(x) |
---|
| 139 | |
---|
| 140 | def runXY(self, x = 0.0): |
---|
| 141 | """ Evaluate the model |
---|
| 142 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 143 | @return: DAB value |
---|
| 144 | """ |
---|
| 145 | return self.model1.runXY(x)* self.model2.runXY(x) |
---|
| 146 | |
---|
| 147 | def set_dispersion(self, parameter, dispersion): |
---|
| 148 | """ |
---|
| 149 | Set the dispersion object for a model parameter |
---|
| 150 | @param parameter: name of the parameter [string] |
---|
| 151 | @dispersion: dispersion object of type DispersionModel |
---|
| 152 | """ |
---|
| 153 | if parameter in self.model1.getParamList(): |
---|
| 154 | return self.model1.set_dispersion(parameter, dispersion) |
---|
| 155 | else: |
---|
| 156 | return self.model2.set_dispersion(parameter, dispersion) |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | |
---|