[1b07935d] | 1 | import sys |
---|
| 2 | import wx |
---|
| 3 | import wx.lib |
---|
| 4 | import numpy |
---|
| 5 | import copy |
---|
| 6 | |
---|
| 7 | from sans.guicomm.events import StatusEvent |
---|
| 8 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
| 9 | _BOX_WIDTH = 80 |
---|
| 10 | |
---|
| 11 | def format_number(value, high=False): |
---|
| 12 | """ |
---|
| 13 | Return a float in a standardized, human-readable formatted string |
---|
| 14 | """ |
---|
| 15 | try: |
---|
| 16 | value = float(value) |
---|
| 17 | except: |
---|
| 18 | print "returning 0" |
---|
| 19 | return "0" |
---|
| 20 | |
---|
| 21 | if high: |
---|
| 22 | return "%-6.4g" % value |
---|
| 23 | else: |
---|
| 24 | return "%-5.3g" % value |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | class ModelPage(wx.Panel): |
---|
| 28 | """ |
---|
| 29 | FitPanel class contains fields allowing to display results when |
---|
| 30 | fitting a model and one data |
---|
| 31 | @note: For Fit to be performed the user should check at least one parameter |
---|
| 32 | on fit Panel window. |
---|
| 33 | |
---|
| 34 | """ |
---|
| 35 | ## Internal name for the AUI manager |
---|
| 36 | window_name = "Fit page" |
---|
| 37 | ## Title to appear on top of the window |
---|
| 38 | window_caption = "Fit Page" |
---|
| 39 | |
---|
| 40 | |
---|
[2dbb681] | 41 | def __init__(self, parent,model, *args, **kwargs): |
---|
[1b07935d] | 42 | wx.Panel.__init__(self, parent, *args, **kwargs) |
---|
| 43 | """ |
---|
| 44 | Initialization of the Panel |
---|
| 45 | """ |
---|
| 46 | self.manager = None |
---|
| 47 | self.parent = parent |
---|
| 48 | self.event_owner=None |
---|
| 49 | #panel interface |
---|
| 50 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 51 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
[49b7efa] | 52 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
[1b07935d] | 53 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
[00561739] | 54 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
[1b07935d] | 55 | self.modelbox = wx.ComboBox(self, -1) |
---|
| 56 | id = wx.NewId() |
---|
| 57 | self.vbox.Add(self.sizer3) |
---|
[49b7efa] | 58 | self.vbox.Add(self.sizer1) |
---|
[1b07935d] | 59 | self.vbox.Add(self.sizer2) |
---|
[00561739] | 60 | self.vbox.Add(self.sizer4) |
---|
| 61 | |
---|
| 62 | id = wx.NewId() |
---|
| 63 | self.btClose =wx.Button(self,id,'Close') |
---|
| 64 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 65 | self.btClose.SetToolTipString("Close page.") |
---|
| 66 | |
---|
| 67 | ix = 12 |
---|
| 68 | iy = 1 |
---|
| 69 | self.sizer4.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[1b07935d] | 70 | ix = 0 |
---|
| 71 | iy = 1 |
---|
| 72 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
| 73 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 74 | ix += 1 |
---|
| 75 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 76 | # contains link between model ,all its parameters, and panel organization |
---|
| 77 | self.parameters=[] |
---|
| 78 | #contains link between a model and selected parameters to fit |
---|
| 79 | self.param_toFit=[] |
---|
| 80 | # model on which the fit would be performed |
---|
| 81 | self.model=model |
---|
[2dbb681] | 82 | try: |
---|
| 83 | print"init modelpage",model.name |
---|
| 84 | self.set_panel(model) |
---|
| 85 | except: |
---|
| 86 | raise |
---|
[1b07935d] | 87 | # preview selected model name |
---|
| 88 | self.prevmodel_name=model.__class__.__name__ |
---|
| 89 | self.modelbox.SetValue(self.prevmodel_name) |
---|
| 90 | # flag to check if the user has selected a new model in the combox box |
---|
| 91 | self.model_hasChanged=False |
---|
| 92 | #dictionary of model name and model class |
---|
| 93 | self.model_list_box={} |
---|
| 94 | # Data1D to make a deep comparison between 2 Data1D for checking data |
---|
| 95 | #change |
---|
| 96 | self.vbox.Layout() |
---|
| 97 | self.vbox.Fit(self) |
---|
| 98 | self.SetSizer(self.vbox) |
---|
| 99 | self.Centre() |
---|
[2dbb681] | 100 | |
---|
[1b07935d] | 101 | |
---|
[00561739] | 102 | def onClose(self,event): |
---|
| 103 | """ close the page associated with this panel""" |
---|
| 104 | self.GrandParent.onClose() |
---|
| 105 | |
---|
[1b07935d] | 106 | def set_owner(self,owner): |
---|
| 107 | """ |
---|
| 108 | set owner of fitpage |
---|
| 109 | @param owner: the class responsible of plotting |
---|
| 110 | """ |
---|
| 111 | self.event_owner=owner |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | def set_manager(self, manager): |
---|
| 115 | """ |
---|
| 116 | set panel manager |
---|
| 117 | @param manager: instance of plugin fitting |
---|
| 118 | """ |
---|
| 119 | self.manager = manager |
---|
| 120 | |
---|
| 121 | def populate_box(self, dict): |
---|
| 122 | """ |
---|
| 123 | Populate each combox box of each page |
---|
| 124 | @param page: the page to populate |
---|
| 125 | """ |
---|
| 126 | id=0 |
---|
| 127 | self.model_list_box=dict |
---|
[2dbb681] | 128 | list_name=[] |
---|
| 129 | for item in self.model_list_box.itervalues(): |
---|
| 130 | name = item.__name__ |
---|
| 131 | model=item() |
---|
| 132 | if hasattr(model, "name"): |
---|
| 133 | name = model.name |
---|
| 134 | list_name.append(name) |
---|
| 135 | list_name.sort() |
---|
| 136 | for name in list_name: |
---|
[1b07935d] | 137 | self.modelbox.Insert(name,int(id)) |
---|
| 138 | id+=1 |
---|
[2dbb681] | 139 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
[1b07935d] | 140 | return 0 |
---|
[060b857] | 141 | |
---|
[2dbb681] | 142 | def set_page(self, model,description=None): |
---|
[00561739] | 143 | #print " modelpage: set_page was called",model |
---|
[060b857] | 144 | self.model=model |
---|
[2dbb681] | 145 | name = self.model.__class__.__name__ |
---|
[060b857] | 146 | if hasattr(self.model, "name"): |
---|
| 147 | name = self.model.name |
---|
[2dbb681] | 148 | |
---|
| 149 | |
---|
[060b857] | 150 | self.modelbox.SetValue(name) |
---|
[2dbb681] | 151 | self.set_panel(self.model) |
---|
[060b857] | 152 | self.manager.draw_model(self.model) |
---|
[2dbb681] | 153 | |
---|
[1b07935d] | 154 | def _on_select_model(self,event): |
---|
| 155 | """ |
---|
| 156 | react when a model is selected from page's combo box |
---|
| 157 | post an event to its owner to draw an appropriate theory |
---|
| 158 | """ |
---|
[00561739] | 159 | #print "modelpage: self.model_list_box ",self.model_list_box |
---|
[1b07935d] | 160 | for item in self.model_list_box.itervalues(): |
---|
[2dbb681] | 161 | name = item.__name__ |
---|
| 162 | items=item() |
---|
| 163 | if hasattr(items, "name"): |
---|
| 164 | name = items.name |
---|
| 165 | print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
| 166 | if name ==event.GetString(): |
---|
| 167 | model=items |
---|
| 168 | print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
| 169 | self.manager.draw_model(model) |
---|
| 170 | self.set_panel(model) |
---|
[1b07935d] | 171 | def set_model_name(self,name): |
---|
| 172 | """ |
---|
| 173 | set model name. set also self.model_hasChanged to true is the model |
---|
| 174 | type has changed or false if it didn't |
---|
| 175 | @param name: model 's name |
---|
| 176 | """ |
---|
| 177 | self.model_hasChanged=False |
---|
| 178 | if (name != self.prevmodel_name): |
---|
| 179 | self.model_hasChanged=True |
---|
| 180 | |
---|
| 181 | self.prevmodel_name=self.modelbox.GetValue() |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | def get_model_box(self): |
---|
| 185 | """ return reference to combox box self.model""" |
---|
| 186 | return self.modelbox |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | def get_param_list(self): |
---|
| 190 | """ |
---|
| 191 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 192 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 193 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 194 | properly so raise ValueError,"missing parameter to fit" |
---|
| 195 | """ |
---|
| 196 | if self.param_toFit !=[]: |
---|
| 197 | return self.param_toFit |
---|
| 198 | else: |
---|
| 199 | raise ValueError,"missing parameter to fit" |
---|
| 200 | |
---|
| 201 | |
---|
[2dbb681] | 202 | def set_panel(self,model): |
---|
[1b07935d] | 203 | """ |
---|
| 204 | Build the panel from the model content |
---|
| 205 | @param model: the model selected in combo box for fitting purpose |
---|
| 206 | """ |
---|
[2dbb681] | 207 | |
---|
[1b07935d] | 208 | self.sizer2.Clear(True) |
---|
[49b7efa] | 209 | self.sizer1.Clear(True) |
---|
[1b07935d] | 210 | self.parameters = [] |
---|
| 211 | self.param_toFit=[] |
---|
| 212 | self.model = model |
---|
| 213 | keys = self.model.getParamList() |
---|
| 214 | keys.sort() |
---|
[5cab7d3] | 215 | description=None |
---|
| 216 | if hasattr(self.model,'description'): |
---|
| 217 | description =model.description |
---|
[d171299] | 218 | |
---|
| 219 | |
---|
[2dbb681] | 220 | print "went here",self.model.name,model.description |
---|
[1b07935d] | 221 | iy = 1 |
---|
| 222 | ix = 0 |
---|
[49b7efa] | 223 | self.cb0 = wx.StaticText(self, -1,'Model Description:') |
---|
| 224 | self.sizer1.Add(self.cb0,(iy, ix),(1,1),\ |
---|
| 225 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d171299] | 226 | iy += 1 |
---|
| 227 | |
---|
| 228 | self.cb01 = wx.StaticText(self, -1,str(description),style=wx.ALIGN_LEFT) |
---|
| 229 | self.cb01.Wrap(400) |
---|
| 230 | #self.cb01 = wx.StaticText(self, -1,str(description),(45, 25),style=wx.ALIGN_LEFT) |
---|
| 231 | |
---|
[49b7efa] | 232 | self.sizer1.Add(self.cb01,(iy, ix),(1,1),\ |
---|
| 233 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 234 | ix = 0 |
---|
| 235 | iy = 1 |
---|
[1b07935d] | 236 | self.cb1 = wx.StaticText(self, -1,'Parameters') |
---|
| 237 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
| 238 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 239 | ix +=1 |
---|
| 240 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
| 241 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
| 242 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 243 | ix +=1 |
---|
| 244 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
| 245 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
| 246 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 247 | self.text2_4.Hide() |
---|
| 248 | for item in keys: |
---|
| 249 | iy += 1 |
---|
| 250 | ix = 0 |
---|
| 251 | cb=wx.StaticText(self, -1, item) |
---|
| 252 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 253 | ix += 1 |
---|
| 254 | value= self.model.getParam(item) |
---|
| 255 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 256 | ctl1.SetValue(str (format_number(value))) |
---|
| 257 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
[55fd102] | 258 | ctl1.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
[1b07935d] | 259 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 260 | ix +=1 |
---|
| 261 | # Units |
---|
| 262 | try: |
---|
| 263 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 264 | except: |
---|
| 265 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
| 266 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 267 | #save data |
---|
| 268 | self.parameters.append([cb,ctl1]) |
---|
| 269 | #Display units text on panel |
---|
| 270 | for item in keys: |
---|
| 271 | if self.model.details[item][0]!='': |
---|
| 272 | self.text2_4.Show() |
---|
| 273 | break |
---|
| 274 | else: |
---|
| 275 | self.text2_4.Hide() |
---|
| 276 | self.vbox.Layout() |
---|
| 277 | self.GrandParent.GetSizer().Layout() |
---|
[2dbb681] | 278 | print "out" |
---|
[1b07935d] | 279 | |
---|
| 280 | def _onparamEnter(self,event): |
---|
| 281 | """ |
---|
| 282 | when enter value on panel redraw model according to changed |
---|
| 283 | """ |
---|
| 284 | self.set_model_parameter() |
---|
| 285 | |
---|
| 286 | def set_model_parameter(self): |
---|
| 287 | if len(self.parameters) !=0 and self.model !=None: |
---|
| 288 | for item in self.parameters: |
---|
| 289 | try: |
---|
[060b857] | 290 | name=str(item[0].GetLabelText()) |
---|
[1b07935d] | 291 | value= float(item[1].GetValue()) |
---|
| 292 | self.model.setParam(name,value) |
---|
| 293 | except: |
---|
| 294 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[442895f] | 295 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
[2dbb681] | 296 | self.manager.draw_model(self.model) |
---|
[1b07935d] | 297 | |
---|