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.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
27 | self.vbox.Add(self.sizer3) |
---|
28 | self.vbox.Add(self.sizer1) |
---|
29 | self.vbox.Add(self.sizer2) |
---|
30 | id = wx.NewId() |
---|
31 | self.btFit =wx.Button(self,id,'Constraint Fit') |
---|
32 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
33 | self.btFit.SetToolTipString("Perform fit.") |
---|
34 | ix = 0 |
---|
35 | iy = 1 |
---|
36 | self.cb1 = wx.CheckBox(self, -1,'Models', (10, 10)) |
---|
37 | self.sizer3.Add(self.cb1,(iy, ix),(1,1),\ |
---|
38 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
39 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) |
---|
40 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) |
---|
41 | ix = 0 |
---|
42 | iy = 1 |
---|
43 | text=wx.StaticText(self, -1, 'Constraint') |
---|
44 | self.sizer2.Add(text,(iy, ix),(1,1),\ |
---|
45 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
46 | ix = 0 |
---|
47 | iy += 1 |
---|
48 | self.ctl2 = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE) |
---|
49 | self.ctl2.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
50 | self.sizer2.Add(self.ctl2, (iy, ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
51 | ix +=2 |
---|
52 | self.sizer2.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
53 | self.params=[] |
---|
54 | self.model_list=[] |
---|
55 | self.model_toFit=[] |
---|
56 | self.page_finder={} |
---|
57 | iy +=1 |
---|
58 | self.sizer2.Add((20,20),(iy, ix)) |
---|
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 | self.model_toFit.append(item) |
---|
94 | else: |
---|
95 | for item in self.model_list: |
---|
96 | item[0].SetValue(False) |
---|
97 | self.model_toFit=[] |
---|
98 | |
---|
99 | |
---|
100 | def add_model(self,page_finder): |
---|
101 | """ |
---|
102 | Receive a dictionary containing information to display model name |
---|
103 | @param page_finder: the dictionary containing models information |
---|
104 | """ |
---|
105 | import copy |
---|
106 | self.model_list=[] |
---|
107 | self.model_toFit=[] |
---|
108 | self.sizer1.Clear(True) |
---|
109 | self.page_finder=page_finder |
---|
110 | ix = 0 |
---|
111 | iy = 1 |
---|
112 | list=[] |
---|
113 | for page, value in page_finder.iteritems(): |
---|
114 | try: |
---|
115 | list=value.get_model() |
---|
116 | model=list[0] |
---|
117 | modelname=list[1] |
---|
118 | cb = wx.CheckBox(self, -1, modelname, (10, 10)) |
---|
119 | cb.SetValue(False) |
---|
120 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
121 | ix = 0 |
---|
122 | iy += 1 |
---|
123 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
124 | self.model_list.append([cb,value,page,modelname]) |
---|
125 | except: |
---|
126 | #wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Simultaneous fit: %s doesn't have a model selected yet %s" % \ |
---|
127 | #(value.get_data().group_id,sys.exc_value))) |
---|
128 | pass |
---|
129 | self.sizer1.Layout() |
---|
130 | self.vbox.Layout() |
---|
131 | |
---|
132 | def remove_model(self,delpage): |
---|
133 | """ |
---|
134 | Remove a checkbox and the name realted to a model selected on page delpage |
---|
135 | @param delpage: the page removed |
---|
136 | """ |
---|
137 | self.sizer1.Clear(True) |
---|
138 | ix = 0 |
---|
139 | iy = 1 |
---|
140 | for item in self.model_list: |
---|
141 | try: |
---|
142 | if not delpage in item: |
---|
143 | #print "simfitpage: redraw modelname",item[3] |
---|
144 | cb = wx.CheckBox(self, -1, item[3], (10, 10)) |
---|
145 | cb.SetValue(False) |
---|
146 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
147 | ix = 0 |
---|
148 | iy += 1 |
---|
149 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
150 | else: |
---|
151 | self.model_list.remove(item) |
---|
152 | except: |
---|
153 | raise |
---|
154 | self.sizer1.Layout() |
---|
155 | self.vbox.Layout() |
---|
156 | |
---|
157 | |
---|
158 | def select_model_name(self,event): |
---|
159 | """ |
---|
160 | Save interformation related to checkbox and their states |
---|
161 | """ |
---|
162 | self.model_toFit=[] |
---|
163 | for item in self.model_list: |
---|
164 | if item[0].GetValue()==True: |
---|
165 | self.model_toFit.append(item) |
---|
166 | else: |
---|
167 | if item in self.model_toFit: |
---|
168 | self.model_toFit.remove(item) |
---|
169 | self.cb1.SetValue(False) |
---|
170 | if len(self.model_list)==len(self.model_toFit): |
---|
171 | self.cb1.SetValue(True) |
---|
172 | else: |
---|
173 | self.cb1.SetValue(False) |
---|
174 | |
---|
175 | |
---|
176 | def set_model(self): |
---|
177 | """ |
---|
178 | set_model take values in self.params which are the values enters by the user |
---|
179 | and try to assign them into the model concerned in self.manager. page.finder |
---|
180 | """ |
---|
181 | if len(self.params) >0: |
---|
182 | for item in self.model_toFit: |
---|
183 | #print"simfitpage: self.model_toFit",item[1] |
---|
184 | list=item[1].get_model() |
---|
185 | #print "simfitpage: list fitpanel2",list,list[0] |
---|
186 | model=list[0] |
---|
187 | param_list=model.getParamList() |
---|
188 | #print "simfitpage: on set_model ",self.params |
---|
189 | if self.params !=[]: |
---|
190 | for element in self.params: |
---|
191 | if model.name == str(element[0]): |
---|
192 | for item in param_list: |
---|
193 | if item==str(element[1]): |
---|
194 | #print "simfitpage: on set_model page 1",param_list |
---|
195 | #print "simfitpage: model name",element[0], model.name |
---|
196 | #print "simfitpage: param name ,param value",element[1],element[2] |
---|
197 | self.manager.set_page_finder(model.name,element[1],\ |
---|
198 | str(element[2])) |
---|
199 | #print "simfitpage:on set_model page 2",model.params['A'],self.params[2] |
---|
200 | |
---|
201 | |
---|
202 | def _onTextEnter(self,event): |
---|
203 | """ |
---|
204 | get values from the constrainst textcrtl ,parses them into model name |
---|
205 | parameter name and parameters values. |
---|
206 | store them in a list self.params .when when params is not empty set_model uses it |
---|
207 | to reset the appropriate model and its appropriates parameters |
---|
208 | """ |
---|
209 | value= self.ctl2.GetValue() |
---|
210 | self.params=[] |
---|
211 | #print "simfitpage: value",value |
---|
212 | try: |
---|
213 | expression='[\s,;]' |
---|
214 | if re.search(expression,value) !=None: |
---|
215 | word=re.split(expression,value) |
---|
216 | #print "simfitpage: when herre",word |
---|
217 | for item in word: |
---|
218 | try: |
---|
219 | self.params.append(self.parser_helper(item)) |
---|
220 | except: |
---|
221 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Loading Error: %s" % sys.exc_value)) |
---|
222 | else: |
---|
223 | try: |
---|
224 | self.params.append(self.parser_helper(value)) |
---|
225 | except: |
---|
226 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status="Loading Error: %s" % sys.exc_value)) |
---|
227 | except: |
---|
228 | raise |
---|
229 | #print "simfitpage: self.params",self.params |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | def parser_helper(self,value): |
---|
234 | """ |
---|
235 | @return param:a list containing the name of a model ,its parameters |
---|
236 | value and name extracted from the constrainst controlbox |
---|
237 | """ |
---|
238 | #print "simfitpage: value",value |
---|
239 | if string.find(value, "=") !=-1: |
---|
240 | model_param= re.split("=",value) |
---|
241 | param_name=model_param[0] |
---|
242 | param_value=model_param[1] |
---|
243 | #print"simfitpage: ", param_name |
---|
244 | #print "simfitpage: ",param_value |
---|
245 | if string.find(param_name,".")!=-1: |
---|
246 | param_names= re.split("\.",param_name) |
---|
247 | model_name=param_names[0] |
---|
248 | param_name=param_names[1] |
---|
249 | param=[str(model_name),str(param_name),str(param_value)] |
---|
250 | #print "simfitpage: param",param |
---|
251 | return param |
---|
252 | else: |
---|
253 | raise ValueError,"cannot evaluate this expression" |
---|
254 | return |
---|
255 | else: |
---|
256 | raise ValueError,"Missing '=' in expression" |
---|
257 | |
---|
258 | |
---|
259 | |
---|