1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2008, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | |
---|
12 | |
---|
13 | import wx |
---|
14 | import sys |
---|
15 | import os |
---|
16 | import pylab |
---|
17 | import math |
---|
18 | import numpy |
---|
19 | import time |
---|
20 | |
---|
21 | from danse.common.plottools.PlotPanel import PlotPanel |
---|
22 | #from danse.common.plottools.plottables import Graph |
---|
23 | from sans.guiframe import dataFitting |
---|
24 | from sans.guiframe.events import EVT_NEW_PLOT |
---|
25 | from sans.guiframe.events import StatusEvent |
---|
26 | from sans.guiframe.events import NewPlotEvent |
---|
27 | from sans.guiframe.events import SlicerEvent |
---|
28 | from sans.guiframe.events import PanelOnFocusEvent |
---|
29 | from sans.guiframe.events import EVT_NEW_LOADED_DATA |
---|
30 | from sans.guiframe.utils import PanelMenu |
---|
31 | from sans.guiframe.dataFitting import Data1D |
---|
32 | from sans.guiframe.panel_base import PanelBase |
---|
33 | from binder import BindArtist |
---|
34 | |
---|
35 | DEFAULT_QMAX = 0.05 |
---|
36 | DEFAULT_QSTEP = 0.001 |
---|
37 | DEFAULT_BEAM = 0.005 |
---|
38 | BIN_WIDTH = 1 |
---|
39 | |
---|
40 | |
---|
41 | class ModelPanel1D(PlotPanel, PanelBase): |
---|
42 | """ |
---|
43 | Plot panel for use with the GUI manager |
---|
44 | """ |
---|
45 | |
---|
46 | ## Internal name for the AUI manager |
---|
47 | window_name = "plotpanel" |
---|
48 | ## Title to appear on top of the window |
---|
49 | window_caption = "Plot Panel" |
---|
50 | ## Flag to tell the GUI manager that this panel is not |
---|
51 | # tied to any perspective |
---|
52 | ALWAYS_ON = True |
---|
53 | ## Group ID |
---|
54 | group_id = None |
---|
55 | |
---|
56 | def __init__(self, parent, id=-1, color = None, |
---|
57 | dpi=None, style=wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs): |
---|
58 | PlotPanel.__init__(self, parent, id=id, style=style, **kwargs) |
---|
59 | PanelBase.__init__(self, parent) |
---|
60 | ## Reference to the parent window |
---|
61 | self.parent = parent |
---|
62 | ## Plottables |
---|
63 | self.plots = {} |
---|
64 | #context menu |
---|
65 | self._slicerpop = None |
---|
66 | |
---|
67 | self._available_data = [] |
---|
68 | self._menu_add_ids = [] |
---|
69 | self._symbol_labels = self.get_symbol_label() |
---|
70 | |
---|
71 | self.hide_menu = None |
---|
72 | ## Unique ID (from gui_manager) |
---|
73 | self.uid = None |
---|
74 | self.x_size = None |
---|
75 | ## Default locations |
---|
76 | self._default_save_location = os.getcwd() |
---|
77 | self.size = None |
---|
78 | ## Graph |
---|
79 | #self.graph = Graph() |
---|
80 | self.graph.xaxis("\\rm{Q}", 'A^{-1}') |
---|
81 | self.graph.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
82 | self.graph.render(self) |
---|
83 | |
---|
84 | # In resizing event |
---|
85 | self.resizing = False |
---|
86 | self.canvas.set_resizing(self.resizing) |
---|
87 | self.Bind(wx.EVT_SIZE, self._OnReSize) |
---|
88 | self._add_more_tool() |
---|
89 | |
---|
90 | def get_symbol_label(self): |
---|
91 | """ |
---|
92 | Associates label to symbol |
---|
93 | """ |
---|
94 | _labels = {} |
---|
95 | i = 0 |
---|
96 | _labels['Circle'] = i |
---|
97 | i += 1 |
---|
98 | _labels['Cross X '] = i |
---|
99 | i += 1 |
---|
100 | _labels['Triangle Down'] = i |
---|
101 | i += 1 |
---|
102 | _labels['Triangle Up'] = i |
---|
103 | i += 1 |
---|
104 | _labels['Triangle Left'] = i |
---|
105 | i += 1 |
---|
106 | _labels['Triangle Right'] = i |
---|
107 | i += 1 |
---|
108 | _labels['Cross +'] = i |
---|
109 | i += 1 |
---|
110 | _labels['Square'] = i |
---|
111 | i += 1 |
---|
112 | _labels['Diamond'] = i |
---|
113 | i += 1 |
---|
114 | _labels['Diamond'] = i |
---|
115 | i += 1 |
---|
116 | _labels['Hexagon1'] = i |
---|
117 | i += 1 |
---|
118 | _labels['Hexagon2'] = i |
---|
119 | i += 1 |
---|
120 | _labels['Pentagon'] = i |
---|
121 | i += 1 |
---|
122 | _labels['Line'] = i |
---|
123 | return _labels |
---|
124 | |
---|
125 | |
---|
126 | def set_data(self, list=None): |
---|
127 | """ |
---|
128 | """ |
---|
129 | pass |
---|
130 | |
---|
131 | def _reset(self): |
---|
132 | """ |
---|
133 | Resets internal data and graph |
---|
134 | """ |
---|
135 | self.graph.reset() |
---|
136 | self.plots = {} |
---|
137 | |
---|
138 | def _OnReSize(self, event): |
---|
139 | """ |
---|
140 | On response of the resize of a panel, set axes_visiable False |
---|
141 | """ |
---|
142 | # It was found that wx >= 2.9.3 sends an event even if no size changed. |
---|
143 | # So manually recode the size (=x_size) and compare here. |
---|
144 | if self.x_size != None: |
---|
145 | if self.x_size == self.GetSize(): |
---|
146 | self.resizing = False |
---|
147 | self.canvas.set_resizing(self.resizing) |
---|
148 | return |
---|
149 | self.x_size = self.GetSize() |
---|
150 | |
---|
151 | # Ready for another event |
---|
152 | # Do not remove this Skip. Otherwise it will get runtime error on wx>=2.9. |
---|
153 | event.Skip() |
---|
154 | # set the resizing flag |
---|
155 | self.resizing = True |
---|
156 | self.canvas.set_resizing(self.resizing) |
---|
157 | self.parent.set_schedule(True) |
---|
158 | pos_x, pos_y = self.GetPositionTuple() |
---|
159 | if pos_x != 0 and pos_y != 0: |
---|
160 | self.size, _ = self.GetClientSizeTuple() |
---|
161 | |
---|
162 | def set_resizing(self, resizing=False): |
---|
163 | """ |
---|
164 | Set the resizing (True/False) |
---|
165 | """ |
---|
166 | self.resizing = resizing |
---|
167 | #self.canvas.set_resizing(resizing) |
---|
168 | |
---|
169 | def schedule_full_draw(self, func='append'): |
---|
170 | """ |
---|
171 | Put self in schedule to full redraw list |
---|
172 | """ |
---|
173 | # append/del this panel in the schedule list |
---|
174 | self.parent.set_schedule_full_draw(self, func) |
---|
175 | |
---|
176 | |
---|
177 | def remove_data_by_id(self, id): |
---|
178 | """' |
---|
179 | remove data from plot |
---|
180 | """ |
---|
181 | if id in self.plots.keys(): |
---|
182 | data = self.plots[id] |
---|
183 | self.graph.delete(data) |
---|
184 | data_manager = self._manager.parent.get_data_manager() |
---|
185 | data_list, theory_list = data_manager.get_by_id(id_list=[id]) |
---|
186 | |
---|
187 | if id in data_list.keys(): |
---|
188 | data = data_list[id] |
---|
189 | if id in theory_list.keys(): |
---|
190 | data = theory_list[id] |
---|
191 | |
---|
192 | del self.plots[id] |
---|
193 | self.graph.render(self) |
---|
194 | self.subplot.figure.canvas.draw_idle() |
---|
195 | if len(self.graph.plottables) == 0: |
---|
196 | #onRemove: graph is empty must be the panel must be destroyed |
---|
197 | self.parent.delete_panel(self.uid) |
---|
198 | |
---|
199 | def plot_data(self, data): |
---|
200 | """ |
---|
201 | Data is ready to be displayed |
---|
202 | |
---|
203 | :param event: data event |
---|
204 | """ |
---|
205 | if data.id in self.plots.keys(): |
---|
206 | #replace |
---|
207 | self.graph.replace(data) |
---|
208 | self.plots[data.id] = data |
---|
209 | else: |
---|
210 | self.plots[data.id] = data |
---|
211 | self.graph.add(self.plots[data.id]) |
---|
212 | |
---|
213 | ## Set the view scale for all plots |
---|
214 | self._onEVT_FUNC_PROPERTY() |
---|
215 | ## render the graph<=No need this done in canvas |
---|
216 | #self.graph.render(self) |
---|
217 | #self.subplot.figure.canvas.draw_idle() |
---|
218 | |
---|
219 | def draw_plot(self): |
---|
220 | """ |
---|
221 | Draw plot |
---|
222 | """ |
---|
223 | self.draw() |
---|
224 | |
---|
225 | |
---|
226 | |
---|
227 | def onLeftDown(self,event): |
---|
228 | """ |
---|
229 | left button down and ready to drag |
---|
230 | Display the position of the mouse on the statusbar |
---|
231 | """ |
---|
232 | PlotPanel.onLeftDown(self, event) |
---|
233 | ax = event.inaxes |
---|
234 | if ax != None: |
---|
235 | position = "x: %8.3g y: %8.3g" % (event.xdata, event.ydata) |
---|
236 | wx.PostEvent(self.parent, StatusEvent(status=position)) |
---|
237 | # unfocus all |
---|
238 | self.parent.set_plot_unfocus() |
---|
239 | #post nd event to notify guiframe that this panel is on focus |
---|
240 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
241 | |
---|
242 | |
---|
243 | def _ontoggle_hide_error(self, event): |
---|
244 | """ |
---|
245 | Toggle error display to hide or show |
---|
246 | """ |
---|
247 | |
---|
248 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
249 | if self.hide_menu.GetText() == "Hide Error": |
---|
250 | selected_plot.hide_error = True |
---|
251 | else: |
---|
252 | selected_plot.hide_error = False |
---|
253 | ## increment graph color |
---|
254 | self.graph.render(self) |
---|
255 | self.subplot.figure.canvas.draw_idle() |
---|
256 | |
---|
257 | def _onRemove(self, event): |
---|
258 | """ |
---|
259 | Remove a plottable from the graph and render the graph |
---|
260 | |
---|
261 | :param event: Menu event |
---|
262 | |
---|
263 | """ |
---|
264 | ## Check if there is a selected graph to remove |
---|
265 | if self.graph.selected_plottable in self.plots.keys(): |
---|
266 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
267 | id = self.graph.selected_plottable |
---|
268 | self.remove_data_by_id(id) |
---|
269 | |
---|
270 | def onContextMenu(self, event): |
---|
271 | """ |
---|
272 | 1D plot context menu |
---|
273 | |
---|
274 | :param event: wx context event |
---|
275 | |
---|
276 | """ |
---|
277 | self._slicerpop = PanelMenu() |
---|
278 | self._slicerpop.set_plots(self.plots) |
---|
279 | self._slicerpop.set_graph(self.graph) |
---|
280 | # Various plot options |
---|
281 | id = wx.NewId() |
---|
282 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
283 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
284 | id = wx.NewId() |
---|
285 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
286 | wx.EVT_MENU(self, id, self.onPrint) |
---|
287 | id = wx.NewId() |
---|
288 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
289 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
290 | |
---|
291 | id = wx.NewId() |
---|
292 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
293 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
294 | |
---|
295 | self._slicerpop.AppendSeparator() |
---|
296 | |
---|
297 | #add menu of other plugins |
---|
298 | item_list = self.parent.get_context_menu(self) |
---|
299 | |
---|
300 | if (not item_list == None) and (not len(item_list) == 0): |
---|
301 | for item in item_list: |
---|
302 | try: |
---|
303 | id = wx.NewId() |
---|
304 | self._slicerpop.Append(id, item[0], item[1]) |
---|
305 | wx.EVT_MENU(self, id, item[2]) |
---|
306 | except: |
---|
307 | msg = "ModelPanel1D.onContextMenu: " |
---|
308 | msg += "bad menu item %s" % sys.exc_value |
---|
309 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
310 | pass |
---|
311 | self._slicerpop.AppendSeparator() |
---|
312 | #id = wx.NewId() |
---|
313 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
314 | if self.graph.selected_plottable in self.plots: |
---|
315 | plot = self.plots[self.graph.selected_plottable] |
---|
316 | |
---|
317 | id = wx.NewId() |
---|
318 | name = plot.name |
---|
319 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
320 | self._slicerpop.AppendSeparator() |
---|
321 | if plot.name != 'SLD': |
---|
322 | wx.EVT_MENU(self, id, self._onSave) |
---|
323 | id = wx.NewId() |
---|
324 | self._slicerpop.Append(id, '&Linear Fit') |
---|
325 | wx.EVT_MENU(self, id, self.onFitting) |
---|
326 | self._slicerpop.AppendSeparator() |
---|
327 | |
---|
328 | id = wx.NewId() |
---|
329 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
330 | wx.EVT_MENU(self, id, self._onRemove) |
---|
331 | if not plot.is_data: |
---|
332 | id = wx.NewId() |
---|
333 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
334 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
335 | symbol_menu = wx.Menu() |
---|
336 | for label in self._symbol_labels: |
---|
337 | id = wx.NewId() |
---|
338 | symbol_menu.Append(id, str(label), str(label)) |
---|
339 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
340 | id = wx.NewId() |
---|
341 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
342 | self._slicerpop.AppendSeparator() |
---|
343 | |
---|
344 | id = wx.NewId() |
---|
345 | self.hide_menu = self._slicerpop.Append(id, "Hide Error") |
---|
346 | |
---|
347 | if plot.dy is not None and plot.dy != []: |
---|
348 | if plot.hide_error : |
---|
349 | self.hide_menu.SetText('Show Error') |
---|
350 | else: |
---|
351 | self.hide_menu.SetText('Hide Error') |
---|
352 | else: |
---|
353 | self.hide_menu.Enable(False) |
---|
354 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
355 | |
---|
356 | self._slicerpop.AppendSeparator() |
---|
357 | # Option to hide |
---|
358 | #TODO: implement functionality to hide a plottable (legend click) |
---|
359 | |
---|
360 | |
---|
361 | id = wx.NewId() |
---|
362 | self._slicerpop.Append(id, '&Change scale') |
---|
363 | wx.EVT_MENU(self, id, self._onProperties) |
---|
364 | id = wx.NewId() |
---|
365 | self._slicerpop.Append(id, '&Reset Graph') |
---|
366 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
367 | pos = event.GetPosition() |
---|
368 | pos = self.ScreenToClient(pos) |
---|
369 | self.PopupMenu(self._slicerpop, pos) |
---|
370 | |
---|
371 | def onFreeze(self, event): |
---|
372 | """ |
---|
373 | """ |
---|
374 | plot = self.plots[self.graph.selected_plottable] |
---|
375 | self.parent.onfreeze([plot.id]) |
---|
376 | |
---|
377 | def onChangeSymbol(self, event): |
---|
378 | """ |
---|
379 | """ |
---|
380 | menu = event.GetEventObject() |
---|
381 | id = event.GetId() |
---|
382 | label = menu.GetLabel(id) |
---|
383 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
384 | selected_plot.symbol = self._symbol_labels[label] |
---|
385 | ## Set the view scale for all plots |
---|
386 | self._onEVT_FUNC_PROPERTY() |
---|
387 | ## render the graph |
---|
388 | #self.graph.render(self) |
---|
389 | #self.subplot.figure.canvas.draw_idle() |
---|
390 | |
---|
391 | def _onsaveTXT(self, path): |
---|
392 | """ |
---|
393 | Save file as txt |
---|
394 | |
---|
395 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
396 | |
---|
397 | """ |
---|
398 | data = self.plots[self.graph.selected_plottable] |
---|
399 | |
---|
400 | if not path == None: |
---|
401 | out = open(path, 'w') |
---|
402 | has_errors = True |
---|
403 | if data.dy == None or data.dy == []: |
---|
404 | has_errors = False |
---|
405 | # Sanity check |
---|
406 | if has_errors: |
---|
407 | try: |
---|
408 | if len(data.y) != len(data.dy): |
---|
409 | has_errors = False |
---|
410 | except: |
---|
411 | has_errors = False |
---|
412 | if has_errors: |
---|
413 | if data.dx != None: |
---|
414 | out.write("<X> <Y> <dY> <dX>\n") |
---|
415 | else: |
---|
416 | out.write("<X> <Y> <dY>\n") |
---|
417 | else: |
---|
418 | out.write("<X> <Y>\n") |
---|
419 | |
---|
420 | for i in range(len(data.x)): |
---|
421 | if has_errors: |
---|
422 | if data.dx != None: |
---|
423 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
424 | data.y[i], |
---|
425 | data.dy[i], |
---|
426 | data.dx[i])) |
---|
427 | else: |
---|
428 | out.write("%g %g %g\n" % (data.x[i], |
---|
429 | data.y[i], |
---|
430 | data.dy[i])) |
---|
431 | else: |
---|
432 | out.write("%g %g\n" % (data.x[i], |
---|
433 | data.y[i])) |
---|
434 | out.close() |
---|
435 | try: |
---|
436 | self._default_save_location = os.path.dirname(path) |
---|
437 | except: |
---|
438 | pass |
---|
439 | |
---|
440 | def _onSave(self, evt): |
---|
441 | """ |
---|
442 | Save a data set to a text file |
---|
443 | |
---|
444 | :param evt: Menu event |
---|
445 | |
---|
446 | """ |
---|
447 | |
---|
448 | path = None |
---|
449 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
450 | "CanSAS 1D files(*.xml)|*.xml" |
---|
451 | dlg = wx.FileDialog(self, "Choose a file", |
---|
452 | self._default_save_location, |
---|
453 | "", wildcard , wx.SAVE) |
---|
454 | |
---|
455 | if dlg.ShowModal() == wx.ID_OK: |
---|
456 | path = dlg.GetPath() |
---|
457 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
458 | # This is MAC Fix |
---|
459 | ext_num = dlg.GetFilterIndex() |
---|
460 | if ext_num == 0: |
---|
461 | format = '.txt' |
---|
462 | else: |
---|
463 | format = '.xml' |
---|
464 | path = os.path.splitext(path)[0] + format |
---|
465 | mypath = os.path.basename(path) |
---|
466 | |
---|
467 | #TODO: This is bad design. The DataLoader is designed |
---|
468 | #to recognize extensions. |
---|
469 | # It should be a simple matter of calling the . |
---|
470 | #save(file, data, '.xml') method |
---|
471 | # of the DataLoader.loader.Loader class. |
---|
472 | from DataLoader.loader import Loader |
---|
473 | #Instantiate a loader |
---|
474 | loader = Loader() |
---|
475 | data = self.plots[self.graph.selected_plottable] |
---|
476 | format = ".txt" |
---|
477 | if os.path.splitext(mypath)[1].lower() == format: |
---|
478 | # Make sure the ext included in the file name |
---|
479 | # especially on MAC |
---|
480 | fName = os.path.splitext(path)[0] + format |
---|
481 | self._onsaveTXT(fName) |
---|
482 | format = ".xml" |
---|
483 | if os.path.splitext(mypath)[1].lower() == format: |
---|
484 | # Make sure the ext included in the file name |
---|
485 | # especially on MAC |
---|
486 | fName = os.path.splitext(path)[0] + format |
---|
487 | loader.save(fName, data, format) |
---|
488 | try: |
---|
489 | self._default_save_location = os.path.dirname(path) |
---|
490 | except: |
---|
491 | pass |
---|
492 | dlg.Destroy() |
---|
493 | |
---|
494 | def _add_more_tool(self): |
---|
495 | """ |
---|
496 | Add refresh button in the tool bar |
---|
497 | """ |
---|
498 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
499 | return |
---|
500 | self.toolbar.AddSeparator() |
---|
501 | id_delete = wx.NewId() |
---|
502 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
503 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
504 | 'Delete', 'permanently Delete') |
---|
505 | |
---|
506 | self.toolbar.Realize() |
---|
507 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
508 | |
---|
509 | def _on_delete(self, event): |
---|
510 | """ |
---|
511 | Refreshes the plotpanel on refresh tollbar button |
---|
512 | """ |
---|
513 | |
---|
514 | if self.parent is not None: |
---|
515 | wx.PostEvent(self.parent, |
---|
516 | NewPlotEvent(group_id=self.group_id, |
---|
517 | action="delete")) |
---|
518 | |
---|