1 | |
---|
2 | import sys,re,string, wx |
---|
3 | from sans.guicomm.events import StatusEvent |
---|
4 | |
---|
5 | class SimultaneousFitPage(wx.Panel): |
---|
6 | """ |
---|
7 | Simultaneous fitting panel |
---|
8 | All that needs to be defined are the |
---|
9 | two data members window_name and window_caption |
---|
10 | """ |
---|
11 | ## Internal name for the AUI manager |
---|
12 | window_name = "simultaneous Fit page" |
---|
13 | ## Title to appear on top of the window |
---|
14 | window_caption = "Simultaneous Fit Page" |
---|
15 | |
---|
16 | |
---|
17 | def __init__(self, parent, *args, **kwargs): |
---|
18 | wx.Panel.__init__(self, parent, *args, **kwargs) |
---|
19 | """ |
---|
20 | Simultaneous page display |
---|
21 | """ |
---|
22 | self.parent = parent |
---|
23 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
24 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
25 | #self.sizer2 = wx.GridBagSizer(5,5) |
---|
26 | self.sizer2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
27 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
28 | self.vbox.Add(self.sizer3) |
---|
29 | self.vbox.Add(self.sizer1) |
---|
30 | self.vbox.Add(self.sizer2) |
---|
31 | id = wx.NewId() |
---|
32 | self.btFit =wx.Button(self,id,'Constraint Fit') |
---|
33 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
34 | self.btFit.SetToolTipString("Perform fit.") |
---|
35 | ix = 0 |
---|
36 | iy = 1 |
---|
37 | self.cb1 = wx.CheckBox(self, -1,'Models', (10, 10)) |
---|
38 | self.sizer3.Add(self.cb1,(iy, ix),(1,1),\ |
---|
39 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
40 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) |
---|
41 | |
---|
42 | text=wx.StaticText(self, -1, 'Constraint') |
---|
43 | self.sizer2.Add(text,0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
44 | |
---|
45 | self.ctl2 = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE) |
---|
46 | self.ctl2.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
47 | self.ctl2.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
48 | self.sizer2.Add(self.ctl2, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
49 | |
---|
50 | self.sizer2.Add(self.btFit, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
51 | self.params=[] |
---|
52 | self.model_list=[] |
---|
53 | self.model_toFit=[] |
---|
54 | self.page_finder={} |
---|
55 | |
---|
56 | |
---|
57 | #self.sizer2.Add((20,20),(iy, ix)) |
---|
58 | #self.sizer2.Add((20,20), 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
59 | self.vbox.Layout() |
---|
60 | self.vbox.Fit(self) |
---|
61 | self.SetSizer(self.vbox) |
---|
62 | self.Centre() |
---|
63 | |
---|
64 | |
---|
65 | def onFit(self,event): |
---|
66 | """ signal for fitting""" |
---|
67 | if len(self.model_toFit) >0 : |
---|
68 | if len(self.params)>0: |
---|
69 | self.set_model() |
---|
70 | else: |
---|
71 | for page in self.page_finder.iterkeys(): |
---|
72 | page.set_model_parameter() |
---|
73 | self.manager._on_simul_fit() |
---|
74 | else: |
---|
75 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
76 | "Select at least on model to fit ")) |
---|
77 | def set_manager(self, manager): |
---|
78 | """ |
---|
79 | set panel manager |
---|
80 | @param manager: instance of plugin fitting |
---|
81 | """ |
---|
82 | self.manager = manager |
---|
83 | |
---|
84 | |
---|
85 | def select_all_model_name(self,event): |
---|
86 | """ |
---|
87 | check all models names |
---|
88 | """ |
---|
89 | self.model_toFit=[] |
---|
90 | if self.cb1.GetValue()==True: |
---|
91 | for item in self.model_list: |
---|
92 | item[0].SetValue(True) |
---|
93 | item[1].schedule_tofit('True') |
---|
94 | self.model_toFit.append(item) |
---|
95 | else: |
---|
96 | for item in self.model_list: |
---|
97 | item[0].SetValue(False) |
---|
98 | item[1].schedule_tofit('False') |
---|
99 | self.model_toFit=[] |
---|
100 | |
---|
101 | |
---|
102 | def add_model(self,page_finder): |
---|
103 | """ |
---|
104 | Receive a dictionary containing information to display model name |
---|
105 | @param page_finder: the dictionary containing models information |
---|
106 | """ |
---|
107 | import copy |
---|
108 | self.model_list=[] |
---|
109 | self.model_toFit=[] |
---|
110 | self.sizer1.Clear(True) |
---|
111 | self.page_finder=page_finder |
---|
112 | ix = 0 |
---|
113 | iy = 1 |
---|
114 | list=[] |
---|
115 | for page, value in page_finder.iteritems(): |
---|
116 | try: |
---|
117 | list = value.get_model() |
---|
118 | model = list[0] |
---|
119 | modelname = list[1] |
---|
120 | cb = wx.CheckBox(self, -1, modelname, (10, 10)) |
---|
121 | cb.SetValue(False) |
---|
122 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
123 | ix = 0 |
---|
124 | iy += 1 |
---|
125 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
126 | self.model_list.append([cb,value,page,modelname]) |
---|
127 | except: |
---|
128 | #wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Simultaneous fit: %s doesn't have a model selected yet %s" % \ |
---|
129 | #(value.get_data().group_id,sys.exc_value))) |
---|
130 | pass |
---|
131 | iy +=1 |
---|
132 | self.sizer1.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
133 | self.sizer1.Layout() |
---|
134 | self.vbox.Layout() |
---|
135 | |
---|
136 | def remove_model(self,delpage): |
---|
137 | """ |
---|
138 | Remove a checkbox and the name realted to a model selected on page delpage |
---|
139 | @param delpage: the page removed |
---|
140 | """ |
---|
141 | self.sizer1.Clear(True) |
---|
142 | ix = 0 |
---|
143 | iy = 1 |
---|
144 | for item in self.model_list: |
---|
145 | try: |
---|
146 | if not delpage in item: |
---|
147 | #print "simfitpage: redraw modelname",item[3] |
---|
148 | cb = wx.CheckBox(self, -1, item[3], (10, 10)) |
---|
149 | cb.SetValue(False) |
---|
150 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
151 | ix = 0 |
---|
152 | iy += 1 |
---|
153 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
154 | else: |
---|
155 | self.model_list.remove(item) |
---|
156 | except: |
---|
157 | raise |
---|
158 | self.sizer1.Layout() |
---|
159 | self.vbox.Layout() |
---|
160 | |
---|
161 | |
---|
162 | def select_model_name(self,event): |
---|
163 | """ |
---|
164 | Save interformation related to checkbox and their states |
---|
165 | """ |
---|
166 | self.model_toFit=[] |
---|
167 | for item in self.model_list: |
---|
168 | if item[0].GetValue()==True: |
---|
169 | item[1].schedule_tofit('True') |
---|
170 | self.model_toFit.append(item) |
---|
171 | else: |
---|
172 | if item in self.model_toFit: |
---|
173 | self.model_toFit.remove(item) |
---|
174 | item[1].schedule_tofit('False') |
---|
175 | self.cb1.SetValue(False) |
---|
176 | if len(self.model_list)==len(self.model_toFit): |
---|
177 | self.cb1.SetValue(True) |
---|
178 | else: |
---|
179 | self.cb1.SetValue(False) |
---|
180 | |
---|
181 | |
---|
182 | def set_model(self): |
---|
183 | """ |
---|
184 | set_model take values in self.params which are the values enters by the user |
---|
185 | and try to assign them into the model concerned in self.manager. page.finder |
---|
186 | """ |
---|
187 | if len(self.params) >0: |
---|
188 | for item in self.model_toFit: |
---|
189 | #print"simfitpage: self.model_toFit",item[1] |
---|
190 | list=item[1].get_model() |
---|
191 | #print "simfitpage: list fitpanel2",list,list[0] |
---|
192 | model=list[0] |
---|
193 | param_list=model.getParamList() |
---|
194 | #print "simfitpage: on set_model ",self.params |
---|
195 | if self.params !=[]: |
---|
196 | for element in self.params: |
---|
197 | if model.name == str(element[0]): |
---|
198 | for item in param_list: |
---|
199 | if item==str(element[1]): |
---|
200 | #print "simfitpage: on set_model page 1",param_list |
---|
201 | #print "simfitpage: model name",element[0], model.name |
---|
202 | #print "simfitpage: param name ,param value",element[1],element[2] |
---|
203 | self.manager.set_page_finder(model.name,element[1],\ |
---|
204 | str(element[2])) |
---|
205 | #print "simfitpage:on set_model page 2",model.params['A'],self.params[2] |
---|
206 | |
---|
207 | |
---|
208 | def _onTextEnter(self,event): |
---|
209 | """ |
---|
210 | get values from the constrainst textcrtl ,parses them into model name |
---|
211 | parameter name and parameters values. |
---|
212 | store them in a list self.params .when when params is not empty set_model uses it |
---|
213 | to reset the appropriate model and its appropriates parameters |
---|
214 | """ |
---|
215 | value= self.ctl2.GetValue() |
---|
216 | self.params=[] |
---|
217 | #print "simfitpage: value",value |
---|
218 | try: |
---|
219 | expression='[\s,;]' |
---|
220 | if re.search(expression,value) !=None: |
---|
221 | word=re.split(expression,value) |
---|
222 | #print "simfitpage: when herre",word |
---|
223 | for item in word: |
---|
224 | try: |
---|
225 | self.params.append(self.parser_helper(item)) |
---|
226 | except: |
---|
227 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Loading Error: %s" % sys.exc_value)) |
---|
228 | else: |
---|
229 | try: |
---|
230 | self.params.append(self.parser_helper(value)) |
---|
231 | except: |
---|
232 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Loading Error: %s" % sys.exc_value)) |
---|
233 | except: |
---|
234 | raise |
---|
235 | #print "simfitpage: self.params",self.params |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | def parser_helper(self,value): |
---|
240 | """ |
---|
241 | @return param:a list containing the name of a model ,its parameters |
---|
242 | value and name extracted from the constrainst controlbox |
---|
243 | """ |
---|
244 | #print "simfitpage: value",value |
---|
245 | if string.find(value, "=") !=-1: |
---|
246 | model_param= re.split("=",value) |
---|
247 | param_name=model_param[0] |
---|
248 | param_value=model_param[1] |
---|
249 | #print"simfitpage: ", param_name |
---|
250 | #print "simfitpage: ",param_value |
---|
251 | if string.find(param_name,".")!=-1: |
---|
252 | param_names= re.split("\.",param_name) |
---|
253 | model_name=param_names[0] |
---|
254 | param_name=param_names[1] |
---|
255 | param=[str(model_name),str(param_name),str(param_value)] |
---|
256 | #print "simfitpage: param",param |
---|
257 | return param |
---|
258 | else: |
---|
259 | raise ValueError,"cannot evaluate this expression" |
---|
260 | return |
---|
261 | else: |
---|
262 | raise ValueError,"Missing '=' in expression" |
---|
263 | |
---|
264 | |
---|
265 | |
---|