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 """ The grid to show the constrains and variables"""
57 wx.grid.Grid.__init__(self,parent, id)
58 self.xor = None
59 self.editMode = True
60
61
62 self.table = CustomConstrainTable()
63 self._init_Table()
64
65
67 self.editMode = True
68 self.table.SetEditMode(editMode)
69
70 self.ForceRefresh()
71
72
75
76
78 if 0:
79 self.table.ncols = 0
80 self.table.nrows = 0
81 self._init_Table_S()
82 self.Destroy()
83
84
86 """ Initialize the table. """
87
88 self.SetTable(self.table)
89
90 self.table.SetEditMode(self.IsEditMode())
91 self.SetRowLabelSize(0)
92 self.SetDefaultCellAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER)
93
94 self.AutoSize()
95
96
97
98
99
101 """ Initialize the table. """
102
103 self.SetTable(self.table)
104
105 self.table.SetEditMode(self.IsEditMode())
106 self.SetRowLabelSize(0)
107 self.SetDefaultCellAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER)
108
109
110 self.AutoSizeColumns(True)
111 self.AutoSizeRows(True)
112
113
115 for model in self.xor.getXmlModels():
116 if modelName == model.name:
117 return model
118 return None
119
120
125
126
128 """ Update all the views"""
129 self.xor = xor
130
131 self.table.SetMultiplexor(xor, modelname)
132
133 self._init_Table_S()
134
135 bg = self.GetDefaultCellBackgroundColour()
136
137 self.setColor4OptVar()
138
139
141 bg = self.GetDefaultCellBackgroundColour()
142 _vars = self.table.GetXmlVariables().getVariables()
143
144 for var in _vars:
145 if not var.target.startswith(self.table.modelName):
146 continue
147
148 (row, col) = self.table.findRowCol(var)
149 self.SetCellBackgroundColour(row, col, bg)
150
151 if var.isOptimized():
152 if row==-1 or col == -1:
153 continue
154 self.SetCellBackgroundColour(row, col, SELECTED_COLOR)
155
156
158 """ Event handle to update viewer when underline data are updated. """
159 if SET_OPT_COLOR:
160 self.setColor4OptVar()
161
162 self.ForceRefresh()
163
164
166 """ Event handle to update the viewer when the model is updated. """
167 self.table.SetMultiplexor(self.xor)
168 self._init_Table()
169
170
172 """ Event handle to update viewer when the parameter is updated."""
173 modelName = self.table.getMoelName()
174 self.table.SetMultiplexor(self.xor, modelName)
175 self.UpdateViewer()
176
177
183
184
185
189
190
194
195
198
199
200
201
202
203
204
205
206
207
209 """
210 Table to show the parameter constrains only
211 """
213 """ Constructor. """
214 wx.grid.PyGridTableBase.__init__(self)
215 self.roughFmt = False
216 self.ReflFmt = True
217 self.xor = None
218 self.editMode = False
219 self.modelName = None
220 self.magnetic=True
221 self.setEmptyLists()
222 self.ncols = len(COL_LABELS)
223 self.nrows = 0
224
225
227 self._layerNames=[]
228 self._depths=[]
229 self._roughs=[]
230 self._rhos=[]
231 self._mus=[]
232 self._phis=[]
233 self._thetas=[]
234
235
237 vList = name.split('.')
238 layerName = vList[1].strip()
239 VarName = vList[2].strip()
240 return ( layerName, VarName )
241
242
244 if VarName == 'depth': col = 2
245 elif VarName == 'rough': col = 3
246 elif VarName == 'rho': col = 4
247 elif VarName == 'mu': col = 5
248 elif VarName == 'phi': col = 6
249 elif VarName == 'theta': col = 7
250 else:
251 col = -1
252
253 return col
254
255
257 row = -1
258 for i in xrange( len(self._layerNames) ):
259 if layerName==self._layerNames[i]:
260 row=i; break
261
262 return row
263
264
272
273
275 ( layerName, VarName ) = self.getNames4Names( name )
276
277 row = self.getColByLayerName( layerName )
278 col = self.getColByVarName( VarName )
279
280 return(row, col)
281
282
285
286
289
290
293
294
297
298
300 """ The table is not editable. """
301 self.editMode = False
302
303
305 """ Return True if it is in the edit mode. """
306 return False
307
308
310 return self.modelName
311
312
314 ret=[]
315 ret.append( self._layerNames )
316 ret.append( self._depths )
317 ret.append( self._roughs )
318 ret.append( self._rhos )
319 ret.append( self._mus )
320 if self.magnetic:
321 ret.append( self._phis )
322 ret.append( self._thetas )
323 return ret
324
325
326
328 """
329 In varaiable List, we have some hidden parameters.
330 """
331 try:
332 pm=self.xor.getLocalModels()[self.modelName].getXmlParameters()[0]
333 self._depths.append( pm.depth )
334 self._roughs.append( pm.rough )
335 self._phis.append( pm.phi )
336 self._thetas.append( pm.theta )
337 except:
338 pass
339
340
342 vList = var.target.split('.')
343 _ModelName = vList[0].strip()
344 layerName = vList[1].strip()
345 VarName = vList[2].strip()
346 return ( _ModelName, layerName, VarName )
347
348
350 if len(self._roughs) < 1 :
351 return
352 self._roughs.pop( len(self._roughs) -1 )
353 self._roughs.insert( 0, 0.0)
354
355
357 """ Set the multiplexor. The constrain and variable
358 are defined in multiplexor.
359 """
360 self.xor = xor
361 self.modelName = modelName
362
363
364 self.setEmptyLists()
365
366
367 self.setHideParameters()
368
369 try:
370 oldLayerName = ""
371 for var in self.xor.getXmlVariables().getVariables():
372
373 ( _ModelName, layerName, VarName ) = self.getNames4Var( var )
374 if _ModelName == self.modelName:
375 if layerName != oldLayerName:
376 self._layerNames.append( layerName )
377 oldLayerName = layerName
378
379 if VarName == 'depth': self._depths.append( var.v0 )
380 elif VarName == 'rough': self._roughs.append( var.v0 )
381 elif VarName == 'rho': self._rhos.append( var.v0 )
382 elif VarName == 'mu': self._mus.append( var.v0 )
383 elif VarName == 'phi': self._phis.append( var.v0 )
384 elif VarName == 'theta': self._thetas.append( var.v0 )
385 else:
386 pass
387
388 self._adjustRough()
389
390 if self._phis==[]:
391 self.magnetic = False
392 except:
393 pass
394
395
396 if self.magnetic: self.ncols = len(COL_LABELS)
397 else: self.ncols = len(COL_LABELS)-2
398
399 self.nrows = len(self._layerNames)
400
401
403 """ Return the multiplexor for the fitting. """
404 return self.xor
405
406
408 """ Return the XmlVariables for the fitting. """
409 return self.xor.getXmlVariables()
410
411
413 """ Return number of rows in the table."""
414 return self.nrows
415
416
418 """ Return number of columns in the table"""
419 return self.ncols
420
421
430
431
441
442
444 """ Return the value for the cell (row, col)"""
445
446 if col == 0:
447 if row==0: return 'V'
448 else: return row
449
450 elif col == 1:
451 try: return self._layerNames[row]
452 except: return ''
453
454 elif col == 2:
455 try: return "%.6f"%(self._depths[row])
456 except: return ''
457
458 elif col == 3: return self.toRoughFormat( self._roughs[row] )
459
460 elif col == 4: return self.toReflFormat( self._rhos[row] )
461
462 elif col == 5: return self.toReflFormat( self._mus[row] )
463
464 elif col == 6: return self.toReflFormat( self._phis[row] )
465
466 elif col == 7:
467 try: return "%.4f"%(self._thetas[row])
468 except: return ''
469
470 else:
471 return ''
472
473
475 """ Return the column label string. """
476 try:
477 if self.ReflFmt: return COL_LABELS2[col]
478 else: return COL_LABELS[col]
479 except:
480 return ''
481
482
484 """ Return the row label string. """
485 try:
486 return super(ConstrainViewerTable, self).GetRowLabelValue(row)
487 except:
488 return ''
489