[4c718654] | 1 | |
---|
| 2 | class FitEngine: |
---|
| 3 | |
---|
| 4 | def _concatenateData(self, listdata=[]): |
---|
| 5 | """ |
---|
| 6 | _concatenateData method concatenates each fields of all data contains ins listdata. |
---|
| 7 | @param listdata: list of data |
---|
| 8 | |
---|
| 9 | @return xtemp, ytemp,dytemp: x,y,dy respectively of data all combined |
---|
| 10 | if xi,yi,dyi of two or more data are the same the second appearance of xi,yi, |
---|
| 11 | dyi is ignored in the concatenation. |
---|
| 12 | |
---|
| 13 | @raise: if listdata is empty will return None |
---|
| 14 | @raise: if data in listdata don't contain dy field ,will create an error |
---|
| 15 | during fitting |
---|
| 16 | """ |
---|
| 17 | if listdata==[]: |
---|
| 18 | raise ValueError, " data list missing" |
---|
| 19 | else: |
---|
| 20 | xtemp=[] |
---|
| 21 | ytemp=[] |
---|
| 22 | dytemp=[] |
---|
| 23 | |
---|
| 24 | for data in listdata: |
---|
| 25 | for i in range(len(data.x)): |
---|
| 26 | xtemp.append(data.x[i]) |
---|
| 27 | ytemp.append(data.y[i]) |
---|
| 28 | if data.dy is not None and len(data.dy)==len(data.y): |
---|
| 29 | dytemp.append(data.dy[i]) |
---|
| 30 | else: |
---|
| 31 | raise RuntimeError, "Fit._concatenateData: y-errors missinge" |
---|
| 32 | return xtemp, ytemp,dytemp |
---|
| 33 | |
---|
[d4b0687] | 34 | def set_model(self,model,name,Uid,pars={}): |
---|
| 35 | """ |
---|
| 36 | |
---|
| 37 | Receive a dictionary of parameter and save it Parameter list |
---|
| 38 | For scipy.fit use. |
---|
| 39 | Set model in a FitArrange object and add that object in a dictionary |
---|
| 40 | with key Uid. |
---|
| 41 | @param model: model on with parameter values are set |
---|
| 42 | @param name: model name |
---|
| 43 | @param Uid: unique key corresponding to a fitArrange object with model |
---|
| 44 | @param pars: dictionary of paramaters name and value |
---|
| 45 | pars={parameter's name: parameter's value} |
---|
| 46 | |
---|
| 47 | """ |
---|
[6831a99] | 48 | if pars !={}: |
---|
| 49 | self.parameters=[] |
---|
[202f93a] | 50 | self.paramList=[] |
---|
[6831a99] | 51 | if model==None: |
---|
| 52 | raise ValueError, "Cannot set parameters for empty model" |
---|
| 53 | else: |
---|
| 54 | model.name=name |
---|
| 55 | for key, value in pars.iteritems(): |
---|
| 56 | param = Parameter(model, key, value) |
---|
| 57 | self.parameters.append(param) |
---|
[202f93a] | 58 | self.paramList.append(key) |
---|
[6831a99] | 59 | |
---|
| 60 | #A fitArrange is already created but contains dList only at Uid |
---|
| 61 | if self.fitArrangeList.has_key(Uid): |
---|
| 62 | self.fitArrangeList[Uid].set_model(model) |
---|
| 63 | else: |
---|
| 64 | #no fitArrange object has been create with this Uid |
---|
| 65 | fitproblem= FitArrange() |
---|
| 66 | fitproblem.set_model(model) |
---|
| 67 | self.fitArrangeList[Uid]=fitproblem |
---|
[d4b0687] | 68 | else: |
---|
[6831a99] | 69 | raise ValueError, "park_integration:missing parameters" |
---|
[d4b0687] | 70 | |
---|
| 71 | |
---|
| 72 | def set_data(self,data,Uid): |
---|
| 73 | """ Receives plottable, creates a list of data to fit,set data |
---|
| 74 | in a FitArrange object and adds that object in a dictionary |
---|
| 75 | with key Uid. |
---|
| 76 | @param data: data added |
---|
| 77 | @param Uid: unique key corresponding to a fitArrange object with data |
---|
| 78 | """ |
---|
| 79 | #A fitArrange is already created but contains model only at Uid |
---|
| 80 | if self.fitArrangeList.has_key(Uid): |
---|
| 81 | self.fitArrangeList[Uid].add_data(data) |
---|
| 82 | else: |
---|
| 83 | #no fitArrange object has been create with this Uid |
---|
| 84 | fitproblem= FitArrange() |
---|
| 85 | fitproblem.add_data(data) |
---|
| 86 | self.fitArrangeList[Uid]=fitproblem |
---|
| 87 | |
---|
| 88 | def get_model(self,Uid): |
---|
| 89 | """ |
---|
| 90 | @param Uid: Uid is key in the dictionary containing the model to return |
---|
| 91 | @return a model at this uid or None if no FitArrange element was created |
---|
| 92 | with this Uid |
---|
| 93 | """ |
---|
| 94 | if self.fitArrangeList.has_key(Uid): |
---|
| 95 | return self.fitArrangeList[Uid].get_model() |
---|
| 96 | else: |
---|
| 97 | return None |
---|
| 98 | |
---|
| 99 | def remove_Fit_Problem(self,Uid): |
---|
| 100 | """remove fitarrange in Uid""" |
---|
| 101 | if self.fitArrangeList.has_key(Uid): |
---|
| 102 | del self.fitArrangeList[Uid] |
---|
| 103 | |
---|
[4c718654] | 104 | |
---|
[d4b0687] | 105 | |
---|
| 106 | |
---|
[4c718654] | 107 | class Parameter: |
---|
| 108 | """ |
---|
| 109 | Class to handle model parameters |
---|
| 110 | """ |
---|
| 111 | def __init__(self, model, name, value=None): |
---|
| 112 | self.model = model |
---|
| 113 | self.name = name |
---|
| 114 | if not value==None: |
---|
| 115 | self.model.setParam(self.name, value) |
---|
| 116 | |
---|
| 117 | def set(self, value): |
---|
| 118 | """ |
---|
| 119 | Set the value of the parameter |
---|
| 120 | """ |
---|
| 121 | self.model.setParam(self.name, value) |
---|
| 122 | |
---|
| 123 | def __call__(self): |
---|
| 124 | """ |
---|
| 125 | Return the current value of the parameter |
---|
| 126 | """ |
---|
| 127 | return self.model.getParam(self.name) |
---|
| 128 | |
---|
[d4b0687] | 129 | class FitArrange: |
---|
| 130 | def __init__(self): |
---|
| 131 | """ |
---|
| 132 | Class FitArrange contains a set of data for a given model |
---|
| 133 | to perform the Fit.FitArrange must contain exactly one model |
---|
| 134 | and at least one data for the fit to be performed. |
---|
| 135 | model: the model selected by the user |
---|
| 136 | Ldata: a list of data what the user wants to fit |
---|
| 137 | |
---|
| 138 | """ |
---|
| 139 | self.model = None |
---|
| 140 | self.dList =[] |
---|
| 141 | |
---|
| 142 | def set_model(self,model): |
---|
| 143 | """ |
---|
| 144 | set_model save a copy of the model |
---|
| 145 | @param model: the model being set |
---|
| 146 | """ |
---|
| 147 | self.model = model |
---|
| 148 | |
---|
| 149 | def add_data(self,data): |
---|
| 150 | """ |
---|
| 151 | add_data fill a self.dList with data to fit |
---|
| 152 | @param data: Data to add in the list |
---|
| 153 | """ |
---|
| 154 | if not data in self.dList: |
---|
| 155 | self.dList.append(data) |
---|
| 156 | |
---|
| 157 | def get_model(self): |
---|
| 158 | """ @return: saved model """ |
---|
| 159 | return self.model |
---|
| 160 | |
---|
| 161 | def get_data(self): |
---|
| 162 | """ @return: list of data dList""" |
---|
| 163 | return self.dList |
---|
| 164 | |
---|
| 165 | def remove_data(self,data): |
---|
| 166 | """ |
---|
| 167 | Remove one element from the list |
---|
| 168 | @param data: Data to remove from dList |
---|
| 169 | """ |
---|
| 170 | if data in self.dList: |
---|
| 171 | self.dList.remove(data) |
---|
[94b44293] | 172 | |
---|
[4c718654] | 173 | |
---|
| 174 | |
---|
| 175 | |
---|