1 | |
---|
2 | |
---|
3 | import os |
---|
4 | import wx |
---|
5 | import wx.lib.newevent |
---|
6 | from sas.sascalc.dataloader.readers.cansas_reader import Reader |
---|
7 | from sas.sasgui.guiframe.utils import format_number |
---|
8 | from sas.sasgui.guiframe.events import EVT_SLICER_PARS, EVT_SLICER |
---|
9 | from sas.sasgui.guiframe.events import SlicerParameterEvent, StatusEvent |
---|
10 | from Plotter2D import ModelPanel2D |
---|
11 | apply_params, EVT_APPLY_PARAMS = wx.lib.newevent.NewEvent() |
---|
12 | save_files, EVT_AUTO_SAVE = wx.lib.newevent.NewEvent() |
---|
13 | |
---|
14 | FIT_OPTIONS = ["No fitting", "Fitting", "Batch Fitting"] |
---|
15 | CONVERT_KEYS = ["SectorInteractor", "AnnulusInteractor", "BoxInteractorX", |
---|
16 | "BoxInteractorY"] |
---|
17 | CONVERT_DICT = {"SectorInteractor": "SectorQ", |
---|
18 | "AnnulusInteractor": "AnnulusPhi", |
---|
19 | "BoxInteractorX": "SlabX", |
---|
20 | "BoxInteractorY": "SlabY"} |
---|
21 | BINNING_OPTIONS = {"Linear" : 0, |
---|
22 | "Logarithmic" : 10,} |
---|
23 | |
---|
24 | |
---|
25 | class SlicerParameterPanel(wx.Dialog): |
---|
26 | """ |
---|
27 | Panel for dynamically changing slicer parameters and apply the same slicer |
---|
28 | to multiple 2D plot panels |
---|
29 | """ |
---|
30 | |
---|
31 | def __init__(self, parent, *args, **kwargs): |
---|
32 | """ |
---|
33 | Dialog window that allow to edit parameters slicer |
---|
34 | by entering new values |
---|
35 | """ |
---|
36 | wx.Dialog.__init__(self, parent, *args, **kwargs) |
---|
37 | self.params = {} |
---|
38 | self.iter = 0 |
---|
39 | self.parent = parent |
---|
40 | self.main_window = parent.parent |
---|
41 | self.data_panel = self.main_window._data_panel |
---|
42 | self.type = None |
---|
43 | self.listeners = [] |
---|
44 | self.parameters = [] |
---|
45 | self.bck = wx.GridBagSizer(5, 5) |
---|
46 | self.SetSizer(self.bck) |
---|
47 | self.auto_save = None |
---|
48 | self.path = None |
---|
49 | self.fitting_options = None |
---|
50 | self.bin_ctl = None |
---|
51 | self.type_list = [] |
---|
52 | self.loaded_data = [] |
---|
53 | self.always_on = None |
---|
54 | self.type_select = None |
---|
55 | self.append_name = None |
---|
56 | self.data_list = None |
---|
57 | self.default_value = "" |
---|
58 | self.batch_slicer_button = None |
---|
59 | label = "Right-click on 2D plot for slicer options" |
---|
60 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
61 | self.bck.Add(title, (0, 0), (1, 2), |
---|
62 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
63 | # Bindings |
---|
64 | self.parent.Bind(EVT_SLICER, self.on_evt_slicer) |
---|
65 | self.Bind(EVT_SLICER_PARS, self.on_param_change) |
---|
66 | self.Bind(EVT_APPLY_PARAMS, self.apply_params_list_and_process) |
---|
67 | self.Bind(EVT_AUTO_SAVE, self.save_files) |
---|
68 | |
---|
69 | def on_evt_slicer(self, event): |
---|
70 | """ |
---|
71 | Process EVT_SLICER events |
---|
72 | When the slicer changes, update the panel |
---|
73 | |
---|
74 | :param event: EVT_SLICER event |
---|
75 | """ |
---|
76 | event.Skip() |
---|
77 | if event.obj_class is None: |
---|
78 | self.set_slicer(None, None) |
---|
79 | else: |
---|
80 | self.set_slicer(event.type, event.params) |
---|
81 | |
---|
82 | def set_slicer(self, type, params): |
---|
83 | """ |
---|
84 | Rebuild the panel |
---|
85 | """ |
---|
86 | self.bck.Clear(True) |
---|
87 | self.bck.Add((5, 5), (0, 0), (1, 1), |
---|
88 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
89 | self.type = type |
---|
90 | if type is None: |
---|
91 | label = "Right-click on 2D plot for slicer options" |
---|
92 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
93 | self.bck.Add(title, (1, 0), (1, 2), |
---|
94 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
95 | else: |
---|
96 | title = wx.StaticText(self, -1, |
---|
97 | "Slicer Parameters:", style=wx.ALIGN_LEFT) |
---|
98 | self.bck.Add(title, (1, 0), (1, 2), |
---|
99 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
100 | iy = 1 |
---|
101 | self.parameters = [] |
---|
102 | keys = params.keys() |
---|
103 | keys.sort() |
---|
104 | for item in keys: |
---|
105 | ix = 0 |
---|
106 | iy += 1 |
---|
107 | if item not in ["count", "errors", "binning base"]: |
---|
108 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
109 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
110 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
111 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
112 | style=wx.TE_PROCESS_ENTER) |
---|
113 | hint_msg = "Modify the value of %s to change" % item |
---|
114 | hint_msg += " the 2D slicer" |
---|
115 | ctl.SetToolTipString(hint_msg) |
---|
116 | ix = 1 |
---|
117 | ctl.SetValue(format_number(str(params[item]))) |
---|
118 | self.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter) |
---|
119 | self.parameters.append([item, ctl]) |
---|
120 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
121 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
122 | ix = 3 |
---|
123 | self.bck.Add((20, 20), (iy, ix), (1, 1), |
---|
124 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
125 | elif item == 'binning base': |
---|
126 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
127 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
128 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
129 | options = BINNING_OPTIONS.keys() |
---|
130 | self.bin_ctl = wx.ComboBox(parent=self, choices=options) |
---|
131 | hint_msg = "Modify the value of %s to change" % item |
---|
132 | hint_msg += " the 2D slicer" |
---|
133 | self.bin_ctl.SetToolTipString(hint_msg) |
---|
134 | ix = 1 |
---|
135 | result = "" |
---|
136 | value = 0 |
---|
137 | for name, value in BINNING_OPTIONS.items(): |
---|
138 | if value == params[item]: |
---|
139 | result = name |
---|
140 | break |
---|
141 | index = self.bin_ctl.FindString(result) |
---|
142 | self.bin_ctl.SetSelection(index) |
---|
143 | self.parameters.append([item, self.bin_ctl]) |
---|
144 | self.Bind(wx.EVT_COMBOBOX, self.on_text_enter) |
---|
145 | self.bck.Add(self.bin_ctl, (iy, ix), (1, 1), |
---|
146 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
147 | ix = 3 |
---|
148 | self.bck.Add((20, 20), (iy, ix), (1, 1), |
---|
149 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
150 | else: |
---|
151 | text = wx.StaticText(self, -1, item + " : ", |
---|
152 | style=wx.ALIGN_LEFT) |
---|
153 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
154 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
155 | ctl = wx.StaticText(self, -1, |
---|
156 | format_number(str(params[item])), |
---|
157 | style=wx.ALIGN_LEFT) |
---|
158 | ix = 1 |
---|
159 | self.bck.Add(ctl, (iy, ix), (1, 1), |
---|
160 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
161 | |
---|
162 | # Change slicer within the window |
---|
163 | ix = 0 |
---|
164 | iy += 1 |
---|
165 | txt = "Slicer type" |
---|
166 | text = wx.StaticText(self, -1, txt, style=wx.ALIGN_LEFT) |
---|
167 | self.bck.Add(text, (iy, ix), (1, 1), |
---|
168 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
169 | self.type_list = CONVERT_KEYS |
---|
170 | self.type_select = wx.ComboBox(parent=self, choices=self.type_list) |
---|
171 | self.type_select.Bind(wx.EVT_COMBOBOX, self.on_change_slicer) |
---|
172 | index = self.type_select.FindString(type) |
---|
173 | self.type_select.SetSelection(index) |
---|
174 | self.bck.Add(self.type_select, (iy, 1), (1, 1), |
---|
175 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
176 | |
---|
177 | # batch slicing parameters |
---|
178 | title_text = "Batch Slicing Options:" |
---|
179 | title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT) |
---|
180 | iy += 1 |
---|
181 | line = wx.StaticLine(self, -1, style=wx.LI_VERTICAL) |
---|
182 | line.SetSize((60, 60)) |
---|
183 | self.bck.Add(line, (iy, ix), (1, 2), |
---|
184 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
185 | iy += 1 |
---|
186 | self.bck.Add(title, (iy, ix), (1, 1), |
---|
187 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
188 | |
---|
189 | # Create a list box with all of the 2D plots |
---|
190 | iy += 1 |
---|
191 | self.process_list() |
---|
192 | self.bck.Add(self.data_list, (iy, ix), (1, 1), |
---|
193 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
194 | |
---|
195 | # Checkbox to enable saving and fitting options |
---|
196 | iy += 1 |
---|
197 | self.auto_save = wx.CheckBox(parent=self, id=wx.NewId(), |
---|
198 | label="Auto save generated 1D:") |
---|
199 | self.Bind(wx.EVT_CHECKBOX, self.on_auto_save_checked) |
---|
200 | self.bck.Add(self.auto_save, (iy, ix), (1, 1), |
---|
201 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
202 | iy += 1 |
---|
203 | # File browser |
---|
204 | save_to = "Save files to:" |
---|
205 | save = wx.StaticText(self, -1, save_to, style=wx.ALIGN_LEFT) |
---|
206 | path = os.getcwd() |
---|
207 | self.path = wx.DirPickerCtrl(self, id=wx.NewId(), path=path, |
---|
208 | message=save_to) |
---|
209 | self.path.Enable(False) |
---|
210 | self.bck.Add(save, (iy, ix), (1, 1), |
---|
211 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
212 | self.bck.Add(self.path, (iy, 1), (1, 1), |
---|
213 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
214 | # Append to file |
---|
215 | iy += 1 |
---|
216 | self.update_file_append(params) |
---|
217 | append_text = "Append to file name:" |
---|
218 | append = wx.StaticText(self, -1, append_text, style=wx.ALIGN_LEFT) |
---|
219 | self.append_name = wx.TextCtrl(parent=self, id=wx.NewId(), |
---|
220 | name="Append to file name:") |
---|
221 | append_tool_tip = "Files will be saved as <SlicerType><FileName>" |
---|
222 | append_tool_tip += "<AppendToText>.txt" |
---|
223 | self.append_name.SetToolTipString(append_tool_tip) |
---|
224 | self.append_name.SetValue(self.default_value) |
---|
225 | self.append_name.Enable(False) |
---|
226 | self.bck.Add(append, (iy, ix), (1, 1), |
---|
227 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
228 | self.bck.Add(self.append_name, (iy, 1), (1, 1), |
---|
229 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
230 | |
---|
231 | # Combobox for selecting fitting options |
---|
232 | iy += 1 |
---|
233 | fit_text = "Fitting Options:" |
---|
234 | fit_text_item = wx.StaticText(self, -1, fit_text, |
---|
235 | style=wx.ALIGN_LEFT) |
---|
236 | self.bck.Add(fit_text_item, (iy, ix), (1, 1), |
---|
237 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
238 | self.fitting_options = wx.ComboBox(parent=self, choices=FIT_OPTIONS) |
---|
239 | self.fitting_options.SetSelection(0) |
---|
240 | self.bck.Add(self.fitting_options, (iy, 1), (1, 1), |
---|
241 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
242 | self.fitting_options.Enable(False) |
---|
243 | self.fitting_options.Bind(wx.EVT_COMBOBOX, None) |
---|
244 | |
---|
245 | # Button to start batch slicing |
---|
246 | iy += 1 |
---|
247 | button_label = "Apply Slicer to Selected Plots" |
---|
248 | self.batch_slicer_button = wx.Button(parent=self, |
---|
249 | label=button_label) |
---|
250 | self.Bind(wx.EVT_BUTTON, self.on_batch_slicer) |
---|
251 | self.bck.Add(self.batch_slicer_button, (iy, ix), (1, 1), |
---|
252 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
253 | iy += 1 |
---|
254 | self.bck.Add((5, 5), (iy, ix), (1, 1), |
---|
255 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5) |
---|
256 | self.bck.Layout() |
---|
257 | self.bck.Fit(self) |
---|
258 | self.parent.GetSizer().Layout() |
---|
259 | |
---|
260 | def on_param_change(self, evt): |
---|
261 | """ |
---|
262 | receive an event end reset value text fields |
---|
263 | inside self.parameters |
---|
264 | """ |
---|
265 | evt.Skip() |
---|
266 | if evt.type == "UPDATE": |
---|
267 | for item in self.parameters: |
---|
268 | if item[0] in evt.params: |
---|
269 | item[1].SetValue("%-5.3g" % evt.params[item[0]]) |
---|
270 | item[1].Refresh() |
---|
271 | |
---|
272 | def on_text_enter(self, evt): |
---|
273 | """ |
---|
274 | Parameters have changed |
---|
275 | """ |
---|
276 | params = {} |
---|
277 | has_error = False |
---|
278 | for item in self.parameters: |
---|
279 | try: |
---|
280 | if item[0] == "binning base": |
---|
281 | title = self.bin_ctl.GetValue() |
---|
282 | params["binning base"] = BINNING_OPTIONS.get(title) |
---|
283 | continue |
---|
284 | params[item[0]] = float(item[1].GetValue()) |
---|
285 | item[1].SetBackgroundColour( |
---|
286 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
287 | item[1].Refresh() |
---|
288 | except: |
---|
289 | has_error = True |
---|
290 | item[1].SetBackgroundColour("pink") |
---|
291 | item[1].Refresh() |
---|
292 | |
---|
293 | if not has_error: |
---|
294 | # Post parameter event |
---|
295 | # parent here is plotter2D |
---|
296 | self.update_file_append(params) |
---|
297 | self.append_name.SetValue(self.default_value) |
---|
298 | self.append_name.Refresh() |
---|
299 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
300 | wx.PostEvent(self.parent, event) |
---|
301 | |
---|
302 | def on_batch_slicer(self, evt=None): |
---|
303 | """ |
---|
304 | Event triggered when batch slicing button is pressed |
---|
305 | :param evt: Event triggering the batch slicing |
---|
306 | """ |
---|
307 | apply_to_list = [] |
---|
308 | spp = self.parent.parent |
---|
309 | params = self.parent.slicer.get_params() |
---|
310 | slicer_type = self.type_select.GetStringSelection() |
---|
311 | save = self.auto_save.IsChecked() |
---|
312 | append = self.append_name.GetValue() |
---|
313 | path = self.path.GetPath() |
---|
314 | fit = self.fitting_options.GetValue() |
---|
315 | |
---|
316 | # Find desired 2D data panels |
---|
317 | for key, mgr in spp.plot_panels.iteritems(): |
---|
318 | if mgr.graph.prop['title'] in self.data_list.CheckedStrings: |
---|
319 | apply_to_list.append(mgr) |
---|
320 | |
---|
321 | # Apply slicer type to selected panels |
---|
322 | for item in apply_to_list: |
---|
323 | self._apply_slicer_to_plot(item, slicer_type) |
---|
324 | |
---|
325 | # Post an event to apply appropriate slicer params to each slicer |
---|
326 | # Pass all variables, including class variables |
---|
327 | event_params = apply_params(params=params, apply_to_list=apply_to_list, |
---|
328 | auto_save=save, append=append, fit=fit, |
---|
329 | path=path, type=slicer_type) |
---|
330 | wx.PostEvent(self, event_params) |
---|
331 | |
---|
332 | def on_change_slicer(self, evt): |
---|
333 | """ |
---|
334 | Event driven slicer change when self.type_select changes |
---|
335 | :param evt: Event triggering this change |
---|
336 | """ |
---|
337 | self._apply_slicer_to_plot(self.parent) |
---|
338 | |
---|
339 | def _apply_slicer_to_plot(self, plot, slicer_type=None): |
---|
340 | """ |
---|
341 | Apply a slicer to *any* plot window, not just parent window |
---|
342 | :param plot: 2D plot panel to apply a slicer to |
---|
343 | :param slicer_type: The type of slicer to apply to the panel |
---|
344 | """ |
---|
345 | # Skip redrawing the current plot if no change in slicer type |
---|
346 | if self.parent == plot and self.type == slicer_type: |
---|
347 | return |
---|
348 | # Do not draw a slicer on a 1D plot |
---|
349 | if not isinstance(plot, ModelPanel2D): |
---|
350 | return |
---|
351 | if slicer_type is None: |
---|
352 | slicer_type = self.type_select.GetStringSelection() |
---|
353 | if slicer_type == self.type_list[0]: |
---|
354 | plot.onSectorQ(None) |
---|
355 | elif slicer_type == self.type_list[1]: |
---|
356 | plot.onSectorPhi(None) |
---|
357 | elif slicer_type == self.type_list[2]: |
---|
358 | plot.onBoxavgX(None) |
---|
359 | elif slicer_type == self.type_list[3]: |
---|
360 | plot.onBoxavgY(None) |
---|
361 | |
---|
362 | def process_list(self): |
---|
363 | """ |
---|
364 | Populate the check list from the currently plotted 2D data |
---|
365 | """ |
---|
366 | # Reinitialize loaded data list on redraw |
---|
367 | self.loaded_data = [] |
---|
368 | # Iterate over the loaded plots and find all 2D panels |
---|
369 | for key, value in self.main_window.plot_panels.iteritems(): |
---|
370 | if isinstance(value, ModelPanel2D): |
---|
371 | self.loaded_data.append(value.data2D.name) |
---|
372 | if value.data2D.id == self.parent.data2D.id: |
---|
373 | # Set current plot panel as uncheckable |
---|
374 | self.always_on = self.loaded_data.index(value.data2D.name) |
---|
375 | self.data_list = wx.CheckListBox(parent=self, id=wx.NewId(), |
---|
376 | choices=self.loaded_data, |
---|
377 | name="Apply Slicer to 2D Plots:") |
---|
378 | # Check all items by default |
---|
379 | for item in range(len(self.data_list.Items)): |
---|
380 | self.data_list.Check(item) |
---|
381 | self.data_list.Bind(wx.EVT_CHECKLISTBOX, self.on_check_box_list) |
---|
382 | |
---|
383 | def on_check_box_list(self, evt=None): |
---|
384 | """ |
---|
385 | Prevent a checkbox item from being unchecked |
---|
386 | :param evt: Event triggered when a checkbox list item is checked |
---|
387 | """ |
---|
388 | if evt is None: |
---|
389 | return |
---|
390 | index = evt.GetSelection() |
---|
391 | if index == self.always_on: |
---|
392 | self.data_list.Check(index) |
---|
393 | |
---|
394 | def apply_params_list_and_process(self, evt=None): |
---|
395 | """ |
---|
396 | Event based parameter setting. |
---|
397 | |
---|
398 | :param evt: Event triggered to apply parameters to a list of plots |
---|
399 | evt should have attrs plot_list and params |
---|
400 | |
---|
401 | """ |
---|
402 | if evt is None: |
---|
403 | return |
---|
404 | # Apply parameter list to each plot as desired |
---|
405 | for item in evt.apply_to_list: |
---|
406 | event = SlicerParameterEvent(type=evt.type, params=evt.params) |
---|
407 | wx.PostEvent(item, event) |
---|
408 | # Post an event to save each data set to file |
---|
409 | if evt.auto_save: |
---|
410 | event = save_files(append_to_name=evt.append, path=evt.path, |
---|
411 | type=evt.type, file_list=evt.apply_to_list, |
---|
412 | fit=evt.fit) |
---|
413 | wx.PostEvent(self, event) |
---|
414 | |
---|
415 | def save_files(self, evt=None): |
---|
416 | """ |
---|
417 | Automatically save the sliced data to file. |
---|
418 | :param evt: Event that triggered the call to the method |
---|
419 | """ |
---|
420 | |
---|
421 | # Events triggered after this event pass other events to wx that are |
---|
422 | # necessary before this event is called. If this is the first time |
---|
423 | # reaching this event, send it to the end of the wx event queue |
---|
424 | if self.iter < 2: |
---|
425 | clone = evt.Clone() |
---|
426 | wx.PostEvent(self, clone) |
---|
427 | self.iter += 1 |
---|
428 | return |
---|
429 | if evt is None: |
---|
430 | return |
---|
431 | |
---|
432 | # Start definitions |
---|
433 | writer = Reader() |
---|
434 | data_dic = {} |
---|
435 | append = evt.append_to_name |
---|
436 | names = [] |
---|
437 | f_name_list = [] |
---|
438 | f_path_list = [] |
---|
439 | |
---|
440 | # Get list of 2D data names for saving |
---|
441 | for f_name in evt.file_list: |
---|
442 | names.append(f_name.data2D.label) |
---|
443 | |
---|
444 | # Find the correct plots to save |
---|
445 | for key, plot in self.main_window.plot_panels.iteritems(): |
---|
446 | if not hasattr(plot, "data2D"): |
---|
447 | for item in plot.plots: |
---|
448 | base = item.replace(CONVERT_DICT[evt.type], "") |
---|
449 | if base in names: |
---|
450 | data_dic[item] = plot.plots[item] |
---|
451 | |
---|
452 | # Save files as Text |
---|
453 | for item, data1d in data_dic.iteritems(): |
---|
454 | base = '.'.join(item.split('.')[:-1]) |
---|
455 | file_name = base + append + ".txt" |
---|
456 | save_to = evt.path + "\\" + file_name |
---|
457 | writer.write(save_to, data1d) |
---|
458 | f_path_list.append(save_to) |
---|
459 | f_name_list.append(file_name) |
---|
460 | |
---|
461 | # Load files into GUI |
---|
462 | for item in f_path_list: |
---|
463 | self.main_window.load_data(item) |
---|
464 | |
---|
465 | # Send to fitting |
---|
466 | self.send_to_fitting(evt.fit, f_name_list) |
---|
467 | |
---|
468 | def send_to_fitting(self, fit=FIT_OPTIONS[0], file_list=None): |
---|
469 | """ |
---|
470 | Send a list of data to the fitting perspective |
---|
471 | :param fit: fit type desired |
---|
472 | :param file_list: list of loaded file names to send to fit |
---|
473 | """ |
---|
474 | if fit in FIT_OPTIONS and fit != FIT_OPTIONS[0] and \ |
---|
475 | file_list is not None: |
---|
476 | # Set perspective to fitting |
---|
477 | int = self.data_panel.perspective_cbox.FindString("Fitting") |
---|
478 | self.data_panel.perspective_cbox.SetSelection(int) |
---|
479 | self.data_panel._on_perspective_selection(None) |
---|
480 | # Unselect all loaded data |
---|
481 | self.data_panel.selection_cbox.SetValue('Unselect all Data') |
---|
482 | self.data_panel._on_selection_type(None) |
---|
483 | # Click each sliced data file |
---|
484 | for f_name in file_list: |
---|
485 | num = len(f_name) |
---|
486 | data_list = self.data_panel.list_cb_data |
---|
487 | for key in data_list: |
---|
488 | loaded_key = (key[:num]) if len(key) > num else key |
---|
489 | if loaded_key == f_name: |
---|
490 | selection = key |
---|
491 | data_ctrl = data_list[selection][0] |
---|
492 | self.check_item_and_children(data_ctrl=data_ctrl, |
---|
493 | check_value=True) |
---|
494 | # Switch to batch mode if selected |
---|
495 | if fit == FIT_OPTIONS[2]: |
---|
496 | self.data_panel.rb_single_mode.SetValue(False) |
---|
497 | self.data_panel.rb_batch_mode.SetValue(True) |
---|
498 | self.data_panel.on_batch_mode(None) |
---|
499 | else: |
---|
500 | self.data_panel.rb_single_mode.SetValue(True) |
---|
501 | self.data_panel.rb_batch_mode.SetValue(False) |
---|
502 | self.data_panel.on_single_mode(None) |
---|
503 | |
---|
504 | # Post button click event to send data to fitting |
---|
505 | evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, |
---|
506 | self.data_panel.bt_import.GetId()) |
---|
507 | wx.PostEvent(self.data_panel, evt) |
---|
508 | |
---|
509 | def on_auto_save_checked(self, evt=None): |
---|
510 | """ |
---|
511 | Enable/Disable auto append when checkbox is checked |
---|
512 | :param evt: Event |
---|
513 | """ |
---|
514 | self.append_name.Enable(self.auto_save.IsChecked()) |
---|
515 | self.path.Enable(self.auto_save.IsChecked()) |
---|
516 | self.fitting_options.Enable(self.auto_save.IsChecked()) |
---|
517 | |
---|
518 | def check_item_and_children(self, data_ctrl, check_value=True): |
---|
519 | self.data_panel.tree_ctrl.CheckItem(data_ctrl, check_value) |
---|
520 | if data_ctrl.HasChildren(): |
---|
521 | if check_value and not data_ctrl.IsExpanded(): |
---|
522 | return |
---|
523 | for child_ctrl in data_ctrl.GetChildren(): |
---|
524 | self.data_panel.CheckItem(child_ctrl, check_value) |
---|
525 | |
---|
526 | def update_file_append(self, params=None): |
---|
527 | """ |
---|
528 | Update default_value when any parameters are changed |
---|
529 | :param params: dictionary of parameters |
---|
530 | """ |
---|
531 | self.default_value = "" |
---|
532 | if params is None: |
---|
533 | params = self.params |
---|
534 | for key in params: |
---|
535 | self.default_value += "_{0}".format(key).split(" [")[0] |
---|
536 | self.default_value += "-{:.2f}".format(params[key]) |
---|