[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 | """ |
---|
[37d9521] | 48 | print "fitting parmater",pars |
---|
| 49 | temp=[] |
---|
[6831a99] | 50 | if pars !={}: |
---|
| 51 | self.parameters=[] |
---|
[202f93a] | 52 | self.paramList=[] |
---|
[6831a99] | 53 | if model==None: |
---|
| 54 | raise ValueError, "Cannot set parameters for empty model" |
---|
| 55 | else: |
---|
| 56 | model.name=name |
---|
| 57 | for key, value in pars.iteritems(): |
---|
| 58 | param = Parameter(model, key, value) |
---|
| 59 | self.parameters.append(param) |
---|
[37d9521] | 60 | temp.append(key) |
---|
| 61 | self.paramList.append(temp) |
---|
| 62 | print "self.paramList2", self.paramList |
---|
[6831a99] | 63 | #A fitArrange is already created but contains dList only at Uid |
---|
| 64 | if self.fitArrangeList.has_key(Uid): |
---|
| 65 | self.fitArrangeList[Uid].set_model(model) |
---|
| 66 | else: |
---|
| 67 | #no fitArrange object has been create with this Uid |
---|
| 68 | fitproblem= FitArrange() |
---|
| 69 | fitproblem.set_model(model) |
---|
| 70 | self.fitArrangeList[Uid]=fitproblem |
---|
[d4b0687] | 71 | else: |
---|
[6831a99] | 72 | raise ValueError, "park_integration:missing parameters" |
---|
[d4b0687] | 73 | |
---|
| 74 | |
---|
| 75 | def set_data(self,data,Uid): |
---|
| 76 | """ Receives plottable, creates a list of data to fit,set data |
---|
| 77 | in a FitArrange object and adds that object in a dictionary |
---|
| 78 | with key Uid. |
---|
| 79 | @param data: data added |
---|
| 80 | @param Uid: unique key corresponding to a fitArrange object with data |
---|
| 81 | """ |
---|
| 82 | #A fitArrange is already created but contains model only at Uid |
---|
| 83 | if self.fitArrangeList.has_key(Uid): |
---|
| 84 | self.fitArrangeList[Uid].add_data(data) |
---|
| 85 | else: |
---|
| 86 | #no fitArrange object has been create with this Uid |
---|
| 87 | fitproblem= FitArrange() |
---|
| 88 | fitproblem.add_data(data) |
---|
| 89 | self.fitArrangeList[Uid]=fitproblem |
---|
| 90 | |
---|
| 91 | def get_model(self,Uid): |
---|
| 92 | """ |
---|
| 93 | @param Uid: Uid is key in the dictionary containing the model to return |
---|
| 94 | @return a model at this uid or None if no FitArrange element was created |
---|
| 95 | with this Uid |
---|
| 96 | """ |
---|
| 97 | if self.fitArrangeList.has_key(Uid): |
---|
| 98 | return self.fitArrangeList[Uid].get_model() |
---|
| 99 | else: |
---|
| 100 | return None |
---|
| 101 | |
---|
| 102 | def remove_Fit_Problem(self,Uid): |
---|
| 103 | """remove fitarrange in Uid""" |
---|
| 104 | if self.fitArrangeList.has_key(Uid): |
---|
| 105 | del self.fitArrangeList[Uid] |
---|
| 106 | |
---|
[4c718654] | 107 | |
---|
[d4b0687] | 108 | |
---|
| 109 | |
---|
[4c718654] | 110 | class Parameter: |
---|
| 111 | """ |
---|
| 112 | Class to handle model parameters |
---|
| 113 | """ |
---|
| 114 | def __init__(self, model, name, value=None): |
---|
| 115 | self.model = model |
---|
| 116 | self.name = name |
---|
| 117 | if not value==None: |
---|
| 118 | self.model.setParam(self.name, value) |
---|
| 119 | |
---|
| 120 | def set(self, value): |
---|
| 121 | """ |
---|
| 122 | Set the value of the parameter |
---|
| 123 | """ |
---|
| 124 | self.model.setParam(self.name, value) |
---|
| 125 | |
---|
| 126 | def __call__(self): |
---|
| 127 | """ |
---|
| 128 | Return the current value of the parameter |
---|
| 129 | """ |
---|
| 130 | return self.model.getParam(self.name) |
---|
| 131 | |
---|
[d4b0687] | 132 | class FitArrange: |
---|
| 133 | def __init__(self): |
---|
| 134 | """ |
---|
| 135 | Class FitArrange contains a set of data for a given model |
---|
| 136 | to perform the Fit.FitArrange must contain exactly one model |
---|
| 137 | and at least one data for the fit to be performed. |
---|
| 138 | model: the model selected by the user |
---|
| 139 | Ldata: a list of data what the user wants to fit |
---|
| 140 | |
---|
| 141 | """ |
---|
| 142 | self.model = None |
---|
| 143 | self.dList =[] |
---|
| 144 | |
---|
| 145 | def set_model(self,model): |
---|
| 146 | """ |
---|
| 147 | set_model save a copy of the model |
---|
| 148 | @param model: the model being set |
---|
| 149 | """ |
---|
| 150 | self.model = model |
---|
| 151 | |
---|
| 152 | def add_data(self,data): |
---|
| 153 | """ |
---|
| 154 | add_data fill a self.dList with data to fit |
---|
| 155 | @param data: Data to add in the list |
---|
| 156 | """ |
---|
| 157 | if not data in self.dList: |
---|
| 158 | self.dList.append(data) |
---|
| 159 | |
---|
| 160 | def get_model(self): |
---|
| 161 | """ @return: saved model """ |
---|
| 162 | return self.model |
---|
| 163 | |
---|
| 164 | def get_data(self): |
---|
| 165 | """ @return: list of data dList""" |
---|
| 166 | return self.dList |
---|
| 167 | |
---|
| 168 | def remove_data(self,data): |
---|
| 169 | """ |
---|
| 170 | Remove one element from the list |
---|
| 171 | @param data: Data to remove from dList |
---|
| 172 | """ |
---|
| 173 | if data in self.dList: |
---|
| 174 | self.dList.remove(data) |
---|
[94b44293] | 175 | |
---|
[4c718654] | 176 | |
---|
| 177 | |
---|
| 178 | |
---|