1 import wx
2 import traceback
3 import numpy
4
5 from park.parkAui.common.auiPanel import AuiPanel
6
7 ROW_LABEL_DEFAULT_SIZE = 30
8 SELECTED_COLOR = wx.Color(0, 255, 0)
9 WARNING_COLOR = wx.Color(255, 0, 0)
10
11
12 RoughFactor = numpy.sqrt( numpy.log(256.0) )
13 ReflFactor = 1.0e-6
14
15
16 DEFAULT_PANEL_SIZE = (400, 300)
17 DEFAULT_GRID_SIZE = DEFAULT_PANEL_SIZE
18
19 ANGSTROM_SYMBOL = u'\u212B'
20 DEGREE_SYMBOL = u'\u00B0'
21 SET_OPT_COLOR = True
22
23 COL_LABELS = ('Layer\n #',
24 'Layer Name',
25 'Thickness ' + ANGSTROM_SYMBOL +'\nDepth',
26 'Roughness 1/' + ANGSTROM_SYMBOL +'\nRO',
27 'Number density ' + '\nRho',
28 'Absorption ' + ANGSTROM_SYMBOL +'\nMu',
29 'Mag. density ' + ANGSTROM_SYMBOL +'\nPhi',
30 'Mag. theta ' + DEGREE_SYMBOL +'\nTheta'
31 )
32
33 COL_LABELS2 = ('Layer\n #',
34 'Layer Name',
35 'Thickness ' + ANGSTROM_SYMBOL +'\nDepth',
36 'Roughness 1/' + ANGSTROM_SYMBOL +'\nRO',
37 'Number density ' + '\nRho (10^6)',
38 'Absorption ' + ANGSTROM_SYMBOL +'\nMu (10^6)',
39 'Mag. density ' + ANGSTROM_SYMBOL +'\nPhi(10^6)',
40 'Mag. theta ' + DEGREE_SYMBOL +'\nTheta'
41 )
42
43
44
45
46
47
48
50 """ Panel to view the constrains and optimized variables only. """
57 AuiPanel.__init__(self, parent, id, pos=pos, size=size )
58 self.grid = ConstrainViewerGrid(self, id=id, pos=pos, size=size)
59 self.editMode = True
60
61 self.sizer = wx.BoxSizer(wx.VERTICAL)
62 self.sizer.Add(self.grid, 1, wx.LEFT|wx.TOP|wx.GROW|wx.EXPAND)
63 self.SetSizer(self.sizer)
64
65
67 """ Update all the views"""
68 self.xor = xor
69 xor.updateVariables()
70 self.grid.SetMultiplexor(xor, modelname)
71 self.UpdateViewer()
72
73
76
77
80
81
83 """ Update the multiplexor for the fitting. """
84 self.UpdateViewer()
85
86
87 - def Enable(self, enable=True):
88 """ Enable the panel."""
89 self.grid.Enable(enable)
90
91
93 """ Update the viewer due to the updating of the model. """
94 self.grid.UpdateViewer()
95
96
98 """ Update the viewer due to the updating of the model. """
99 self.grid.UpdateModelViewer()
100
101
105
106
108 """ Update the viewer when parameter value is updated. """
109 self.grid.RefreshViewer()
110
111
114
115
117 """ Set to editable mode or not. """
118 self.grid.SetEditMode(True)
119
120
123
124
127
128
131
132
133
134
135
136
138 """ The grid to show the constrains and variables"""
145 wx.grid.Grid.__init__(self,parent, id)
146 self.xor = None
147 self.editMode = True
148
149
150 self.table = CustomConstrainTable()
151 self._init_Table()
152
153
155 self.editMode = True
156 self.table.SetEditMode(editMode)
157
158 self.ForceRefresh()
159
160
163
164
167
168
170 """ Initialize the table. """
171
172 self.SetTable(self.table)
173
174 self.table.SetEditMode(self.IsEditMode())
175 self.SetRowLabelSize(0)
176 self.SetDefaultCellAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER)
177
178 self.AutoSize()
179
180
182 for model in self.xor.getXmlModels():
183 if modelName == model.name:
184 return model
185 return None
186
187
192
193
195 """ Update all the views"""
196 self.xor = xor
197
198 self.table.SetMultiplexor(xor, modelname)
199
200 self._init_Table()
201
202 bg = self.GetDefaultCellBackgroundColour()
203
204 self.setColor4OptVar()
205
206
208 bg = self.GetDefaultCellBackgroundColour()
209 _vars = self.table.GetXmlVariables().getVariables()
210
211 for var in _vars:
212 if not var.target.startswith(self.table.modelName):
213 continue
214
215 (row, col) = self.table.findRowCol(var)
216 self.SetCellBackgroundColour(row, col, bg)
217
218 if var.isOptimized():
219 if row==-1 or col == -1:
220 continue
221 self.SetCellBackgroundColour(row, col, SELECTED_COLOR)
222
223
225 """ Event handle to update viewer when underline data are updated. """
226 if SET_OPT_COLOR:
227 self.setColor4OptVar()
228
229 self.ForceRefresh()
230
231
233 """ Event handle to update the viewer when the model is updated. """
234 self.table.SetMultiplexor(self.xor)
235 self._init_Table()
236
237
239 """ Event handle to update viewer when the parameter is updated."""
240 modelName = self.table.getMoelName()
241 self.table.SetMultiplexor(self.xor, modelName)
242 self.UpdateViewer()
243
244
250
251
252
256
257
261
262
265
266
267
268
269
270
271
272
273
274
276 """
277 Table to show the parameter constrains only
278 """
280 """ Constructor. """
281 wx.grid.PyGridTableBase.__init__(self)
282 self.roughFmt = False
283 self.ReflFmt = True
284 self.xor = None
285 self.editMode = False
286 self.modelName = None
287 self.magnetic=True
288 self.setEmptyLists()
289 self.ncols = len(COL_LABELS)
290 self.nrows = 0
291
292
294 self._layerNames=[]
295 self._depths=[]
296 self._roughs=[]
297 self._rhos=[]
298 self._mus=[]
299 self._phis=[]
300 self._thetas=[]
301
302
304 vList = name.split('.')
305 layerName = vList[1].strip()
306 VarName = vList[2].strip()
307 return ( layerName, VarName )
308
309
311 if VarName == 'depth': col = 2
312 elif VarName == 'rough': col = 3
313 elif VarName == 'rho': col = 4
314 elif VarName == 'mu': col = 5
315 elif VarName == 'phi': col = 6
316 elif VarName == 'theta': col = 7
317 else:
318 col = -1
319
320 return col
321
322
324 row = -1
325 for i in xrange( len(self._layerNames) ):
326 if layerName==self._layerNames[i]:
327 row=i; break
328
329 return row
330
331
339
340
342 ( layerName, VarName ) = self.getNames4Names( name )
343
344 row = self.getColByLayerName( layerName )
345 col = self.getColByVarName( VarName )
346
347 return(row, col)
348
349
352
353
356
357
360
361
364
365
367 """ The table is not editable. """
368 self.editMode = False
369
370
372 """ Return True if it is in the edit mode. """
373 return False
374
375
377 return self.modelName
378
379
381 ret=[]
382 ret.append( self._layerNames )
383 ret.append( self._depths )
384 ret.append( self._roughs )
385 ret.append( self._rhos )
386 ret.append( self._mus )
387 if self.magnetic:
388 ret.append( self._phis )
389 ret.append( self._thetas )
390 return ret
391
392
393
395 """
396 In varaiable List, we have some hidden parameters.
397 """
398 try:
399 pm=self.xor.getLocalModels()[self.modelName].getXmlParameters()[0]
400 self._depths.append( pm.depth )
401 self._roughs.append( pm.rough )
402 self._phis.append( pm.phi )
403 self._thetas.append( pm.theta )
404 except:
405 pass
406
407
409 vList = var.target.split('.')
410 _ModelName = vList[0].strip()
411 layerName = vList[1].strip()
412 VarName = vList[2].strip()
413 return ( _ModelName, layerName, VarName )
414
415
417 if len(self._roughs) < 1 :
418 return
419 self._roughs.pop( len(self._roughs) -1 )
420 self._roughs.insert( 0, 0.0)
421
422
424 """ Set the multiplexor. The constrain and variable
425 are defined in multiplexor.
426 """
427 self.xor = xor
428 self.modelName = modelName
429
430
431 self.setEmptyLists()
432
433
434 self.setHideParameters()
435
436 try:
437 oldLayerName = ""
438 for var in self.xor.getXmlVariables().getVariables():
439
440 ( _ModelName, layerName, VarName ) = self.getNames4Var( var )
441 if _ModelName == self.modelName:
442 if layerName != oldLayerName:
443 self._layerNames.append( layerName )
444 oldLayerName = layerName
445
446 if VarName == 'depth': self._depths.append( var.v0 )
447 elif VarName == 'rough': self._roughs.append( var.v0 )
448 elif VarName == 'rho': self._rhos.append( var.v0 )
449 elif VarName == 'mu': self._mus.append( var.v0 )
450 elif VarName == 'phi': self._phis.append( var.v0 )
451 elif VarName == 'theta': self._thetas.append( var.v0 )
452 else:
453 pass
454
455 self._adjustRough()
456
457 if self._phis==[]:
458 self.magnetic = False
459 except:
460 pass
461
462
463 if self.magnetic: self.ncols = len(COL_LABELS)
464 else: self.ncols = len(COL_LABELS)-2
465
466 self.nrows = len(self._layerNames)
467
468
470 """ Return the multiplexor for the fitting. """
471 return self.xor
472
473
475 """ Return the XmlVariables for the fitting. """
476 return self.xor.getXmlVariables()
477
478
480 """ Return number of rows in the table."""
481 return self.nrows
482
483
485 """ Return number of columns in the table"""
486 return self.ncols
487
488
497
498
508
509
511 """ Return the value for the cell (row, col)"""
512
513 if col == 0:
514 if row==0: return 'V'
515 else: return row
516
517 elif col == 1:
518 try: return self._layerNames[row]
519 except: return ''
520
521 elif col == 2:
522 try: return "%.6f"%(self._depths[row])
523 except: return ''
524
525 elif col == 3: return self.toRoughFormat( self._roughs[row] )
526
527 elif col == 4: return self.toReflFormat( self._rhos[row] )
528
529 elif col == 5: return self.toReflFormat( self._mus[row] )
530
531 elif col == 6: return self.toReflFormat( self._phis[row] )
532
533 elif col == 7:
534 try: return "%.4f"%(self._thetas[row])
535 except: return ''
536
537 else:
538 return ''
539
540
542 """ Return the column label string. """
543 try:
544 if self.ReflFmt: return COL_LABELS2[col]
545 else: return COL_LABELS[col]
546 except:
547 return ''
548
549
551 """ Return the row label string. """
552 try:
553 return super(ConstrainViewerTable, self).GetRowLabelValue(row)
554 except:
555 return ''
556