1 | |
---|
2 | import os, sys,numpy |
---|
3 | import wx |
---|
4 | import re |
---|
5 | |
---|
6 | from dataFitting import Data1D |
---|
7 | from dataFitting import Data2D |
---|
8 | from DataLoader.loader import Loader |
---|
9 | from load_thread import DataReader |
---|
10 | |
---|
11 | from sans.guicomm.events import StatusEvent |
---|
12 | from sans.guicomm.events import NewStoreDataEvent |
---|
13 | from sans.guicomm.events import NewPlotEvent |
---|
14 | |
---|
15 | |
---|
16 | def enable_add_data(existing_panel, new_plot): |
---|
17 | """ |
---|
18 | Enable append data on a plot panel |
---|
19 | """ |
---|
20 | is_theory = len(existing_panel.plots)<= 1 and \ |
---|
21 | existing_panel.plots.values()[0].__class__.__name__=="Theory1D" |
---|
22 | |
---|
23 | is_data2d = hasattr(new_plot, 'data') |
---|
24 | is_data1d = existing_panel.__class__.__name__ == "ModelPanel1D"\ |
---|
25 | and existing_panel.group_id is not None |
---|
26 | |
---|
27 | return is_data1d and not is_data2d and not is_theory |
---|
28 | |
---|
29 | def parse_name(name, expression): |
---|
30 | """ |
---|
31 | remove "_" in front of a name |
---|
32 | """ |
---|
33 | if re.match(expression, name) is not None: |
---|
34 | word = re.split(expression, name, 1) |
---|
35 | for item in word: |
---|
36 | if item.lstrip().rstrip() != '': |
---|
37 | return item |
---|
38 | else: |
---|
39 | return name |
---|
40 | |
---|
41 | def choose_data_file(parent, location=None): |
---|
42 | """ |
---|
43 | return a list of file path to read |
---|
44 | """ |
---|
45 | path = None |
---|
46 | if location == None: |
---|
47 | location = os.getcwd() |
---|
48 | |
---|
49 | l = Loader() |
---|
50 | cards = l.get_wildcards() |
---|
51 | wlist = '|'.join(cards) |
---|
52 | |
---|
53 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, |
---|
54 | style=wx.OPEN|wx.MULTIPLE|wx.CHANGE_DIR) |
---|
55 | if dlg.ShowModal() == wx.ID_OK: |
---|
56 | path = dlg.GetPaths() |
---|
57 | if path: |
---|
58 | mypath = os.path.basename(path[0]) |
---|
59 | dlg.Destroy() |
---|
60 | |
---|
61 | return path |
---|
62 | |
---|
63 | def choose_data_folder(parent, location=None): |
---|
64 | """ |
---|
65 | return a list of folder to read |
---|
66 | """ |
---|
67 | path = None |
---|
68 | if location == None: |
---|
69 | location = os.getcwd() |
---|
70 | |
---|
71 | l = Loader() |
---|
72 | cards = l.get_wildcards() |
---|
73 | wlist = '|'.join(cards) |
---|
74 | |
---|
75 | dlg = wx.DirDialog(parent, "Choose a directory", location, |
---|
76 | style=wx.DD_DEFAULT_STYLE) |
---|
77 | if dlg.ShowModal() == wx.ID_OK: |
---|
78 | path = dlg.GetPath() |
---|
79 | mypath = os.path.basename(path) |
---|
80 | dlg.Destroy() |
---|
81 | |
---|
82 | return [path] |
---|
83 | |
---|
84 | def open_dialog_append_data(panel_name, data_name): |
---|
85 | """ |
---|
86 | Pop up an error message. |
---|
87 | |
---|
88 | :param panel_name: the name of the current panel |
---|
89 | :param data_name: the name of the current data |
---|
90 | |
---|
91 | """ |
---|
92 | message = " Do you want to append %s data\n in "%(str(data_name)) |
---|
93 | message += " %s panel?\n\n"%(str(panel_name)) |
---|
94 | dial = wx.MessageDialog(None, message, 'Question', |
---|
95 | wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION) |
---|
96 | if dial.ShowModal() == wx.ID_YES: |
---|
97 | return True |
---|
98 | else: |
---|
99 | return False |
---|
100 | |
---|
101 | |
---|
102 | def load_error(error=None): |
---|
103 | """ |
---|
104 | Pop up an error message. |
---|
105 | |
---|
106 | :param error: details error message to be displayed |
---|
107 | """ |
---|
108 | message = "You had to try this, didn't you?\n\n" |
---|
109 | message += "The data file you selected could not be loaded.\n" |
---|
110 | message += "Make sure the content of your file is properly formatted.\n\n" |
---|
111 | |
---|
112 | if error is not None: |
---|
113 | message += "When contacting the DANSE team, mention the following:\n%s" % str(error) |
---|
114 | |
---|
115 | dial = wx.MessageDialog(None, message, 'Error Loading File', wx.OK | wx.ICON_EXCLAMATION) |
---|
116 | dial.ShowModal() |
---|
117 | |
---|
118 | def on_load_error(parent): |
---|
119 | """ |
---|
120 | """ |
---|
121 | wx.PostEvent(parent, StatusEvent(status="Load cancel..", info="warning", |
---|
122 | type="stop")) |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | def read_data(parent, path): |
---|
127 | """ |
---|
128 | Create a list of data to read |
---|
129 | """ |
---|
130 | list = [] |
---|
131 | if path is not None and len(path) > 0: |
---|
132 | for p in path: |
---|
133 | if os.path.isdir(p): |
---|
134 | list = [os.path.join(os.path.abspath(p), file) for file in os.listdir(p) ] |
---|
135 | |
---|
136 | if os.path.isfile(p): |
---|
137 | list.append(p) |
---|
138 | |
---|
139 | return plot_data(parent, numpy.array(list)) |
---|
140 | |
---|
141 | def load_helper(parent , output, path): |
---|
142 | """ |
---|
143 | """ |
---|
144 | filename = os.path.basename(path) |
---|
145 | #print output.process |
---|
146 | if not output.__class__.__name__ == "list": |
---|
147 | ## Creating a Data2D with output |
---|
148 | if hasattr(output,'data'): |
---|
149 | msg = "Loading 2D data: %s "%output.filename |
---|
150 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
151 | new_plot = Data2D(image=None, err_image=None) |
---|
152 | |
---|
153 | else: |
---|
154 | msg = "Loading 1D data: %s "%output.filename |
---|
155 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
156 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
157 | |
---|
158 | new_plot.copy_from_datainfo(output) |
---|
159 | output.clone_without_data(clone=new_plot) |
---|
160 | |
---|
161 | ## data 's name |
---|
162 | if output.filename is None or output.filename == "": |
---|
163 | output.filename = str(filename) |
---|
164 | ## name of the data allow to differentiate data when plotted |
---|
165 | name = parse_name(name=output.filename, expression="_") |
---|
166 | if not name in parent.indice_load_data.keys(): |
---|
167 | parent.indice_load_data[name] = 0 |
---|
168 | else: |
---|
169 | ## create a copy of the loaded data |
---|
170 | parent.indice_load_data[name] += 1 |
---|
171 | name = name +"[%i]"%parent.indice_load_data[name] |
---|
172 | |
---|
173 | new_plot.name = name |
---|
174 | ## allow to highlight data when plotted |
---|
175 | new_plot.interactive = True |
---|
176 | ## when 2 data have the same id override the 1 st plotted |
---|
177 | new_plot.id = name |
---|
178 | ##group_id specify on which panel to plot this data |
---|
179 | new_plot.group_id = name |
---|
180 | new_plot.is_data = True |
---|
181 | ##post data to plot |
---|
182 | title = output.filename |
---|
183 | if hasattr(new_plot,"title"): |
---|
184 | title = str(new_plot.title.lstrip().rstrip()) |
---|
185 | if title == "": |
---|
186 | title = str(name) |
---|
187 | else: |
---|
188 | title = str(name) |
---|
189 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
190 | existing_panel = parent.panel_on_focus |
---|
191 | panel_name = existing_panel.window_caption |
---|
192 | data_name = new_plot.name |
---|
193 | if enable_add_data(existing_panel, new_plot): |
---|
194 | if open_dialog_append_data(panel_name, data_name): |
---|
195 | #add this plot the an existing panel |
---|
196 | new_plot.group_id = existing_panel.group_id |
---|
197 | return [(new_plot, path)] |
---|
198 | #wx.PostEvent(parent, NewStoreDataEvent(data=new_plot)) |
---|
199 | |
---|
200 | ## the output of the loader is a list , some xml files contain more than one data |
---|
201 | else: |
---|
202 | i=1 |
---|
203 | temp=[] |
---|
204 | for item in output: |
---|
205 | msg = "Loading 1D data: %s "%str(item.run[0]) |
---|
206 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
207 | try: |
---|
208 | dx = item.dx |
---|
209 | dxl = item.dxl |
---|
210 | dxw = item.dxw |
---|
211 | except: |
---|
212 | dx = None |
---|
213 | dxl = None |
---|
214 | dxw = None |
---|
215 | |
---|
216 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy) |
---|
217 | new_plot.copy_from_datainfo(item) |
---|
218 | item.clone_without_data(clone=new_plot) |
---|
219 | new_plot.dxl = dxl |
---|
220 | new_plot.dxw = dxw |
---|
221 | |
---|
222 | name = parse_name(name=str(item.run[0]), expression="_") |
---|
223 | if not name in parent.indice_load_data.keys(): |
---|
224 | parent.indice_load_data[name] = 0 |
---|
225 | else: |
---|
226 | ## create a copy of the loaded data |
---|
227 | |
---|
228 | #TODO: this is a very annoying feature. We should make this |
---|
229 | # an option. Excel doesn't do this. Why should we? |
---|
230 | # What is the requirement for this feature, and are the |
---|
231 | # counter arguments stronger? Is this feature developed |
---|
232 | # to please at least 80% of the users or a special few? |
---|
233 | parent.indice_load_data[name] += 1 |
---|
234 | name = name + "(copy %i)"%parent.indice_load_data[name] |
---|
235 | |
---|
236 | new_plot.name = name |
---|
237 | new_plot.interactive = True |
---|
238 | new_plot.group_id = name |
---|
239 | new_plot.id = name |
---|
240 | new_plot.is_data = True |
---|
241 | |
---|
242 | if hasattr(item,"title"): |
---|
243 | title = item.title.lstrip().rstrip() |
---|
244 | if title == "": |
---|
245 | title = str(name) |
---|
246 | else: |
---|
247 | title = name |
---|
248 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
249 | existing_panel = parent.panel_on_focus |
---|
250 | panel_name = existing_panel.window_caption |
---|
251 | data_name = new_plot.name |
---|
252 | if enable_add_data(existing_panel, new_plot): |
---|
253 | if open_dialog_append_data(panel_name, data_name): |
---|
254 | #add this plot the an existing panel |
---|
255 | new_plot.group_id = existing_panel.group_id |
---|
256 | temp.append((new_plot, path)) |
---|
257 | #wx.PostEvent(parent, NewStoreDataEvent(data=new_plot)) |
---|
258 | i+=1 |
---|
259 | return temp |
---|
260 | |
---|
261 | |
---|
262 | def plot_data(parent, path): |
---|
263 | """ |
---|
264 | Use the DataLoader loader to created data to plot. |
---|
265 | |
---|
266 | :param path: the path of the data to load |
---|
267 | |
---|
268 | """ |
---|
269 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
270 | from DataLoader.loader import Loader |
---|
271 | |
---|
272 | # Instantiate a loader |
---|
273 | L = Loader() |
---|
274 | |
---|
275 | # Load data |
---|
276 | msg = "" |
---|
277 | list_of_data = [] |
---|
278 | for p in path: |
---|
279 | try: |
---|
280 | list_of_data.append((L.load(p), p)) |
---|
281 | except: |
---|
282 | p_msg = "Loading... " + str(sys.exc_value) |
---|
283 | wx.PostEvent(parent, StatusEvent(status=p_msg, info="warning")) |
---|
284 | msg += (str(sys.exc_value)+"\n") |
---|
285 | if msg.lstrip().rstrip() != "": |
---|
286 | load_error(msg) |
---|
287 | |
---|
288 | #output = map(L.load, path) |
---|
289 | |
---|
290 | # Notify user if the loader completed the load but no data came out |
---|
291 | if len(list_of_data) == 0 or numpy.array(list_of_data).all() is None: |
---|
292 | load_error("The data file appears to be empty.") |
---|
293 | msg = "Loading complete: %s"%output.filename |
---|
294 | wx.PostEvent(parent, StatusEvent(status=msg, info="warning", type="stop")) |
---|
295 | return |
---|
296 | result =[] |
---|
297 | for output , path in list_of_data: |
---|
298 | result += load_helper(parent=parent, output=output, path=path) |
---|
299 | msg = "Loading complete: %s"%output.filename |
---|
300 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
301 | return result |
---|
302 | |
---|
303 | |
---|
304 | def old_plot_data(parent, path): |
---|
305 | """ |
---|
306 | Use the DataLoader loader to created data to plot. |
---|
307 | |
---|
308 | :param path: the path of the data to load |
---|
309 | |
---|
310 | """ |
---|
311 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
312 | from DataLoader.loader import Loader |
---|
313 | |
---|
314 | # Instantiate a loader |
---|
315 | L = Loader() |
---|
316 | |
---|
317 | # Load data |
---|
318 | try: |
---|
319 | output = L.load(path) |
---|
320 | except: |
---|
321 | raise |
---|
322 | #load_error(sys.exc_value) |
---|
323 | return |
---|
324 | |
---|
325 | # Notify user if the loader completed the load but no data came out |
---|
326 | if output == None: |
---|
327 | load_error("The data file appears to be empty.") |
---|
328 | return |
---|
329 | |
---|
330 | |
---|
331 | filename = os.path.basename(path) |
---|
332 | |
---|
333 | if not output.__class__.__name__ == "list": |
---|
334 | ## Creating a Data2D with output |
---|
335 | if hasattr(output,'data'): |
---|
336 | msg = "Loading 2D data: %s"%output.filename |
---|
337 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
338 | new_plot = Data2D(image=None, err_image=None) |
---|
339 | |
---|
340 | else: |
---|
341 | msg = "Loading 1D data: %s"%output.filename |
---|
342 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
343 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
344 | |
---|
345 | new_plot.copy_from_datainfo(output) |
---|
346 | output.clone_without_data(clone=new_plot) |
---|
347 | |
---|
348 | ## data 's name |
---|
349 | if output.filename is None or output.filename == "": |
---|
350 | output.filename = str(filename) |
---|
351 | ## name of the data allow to differentiate data when plotted |
---|
352 | name = parse_name(name=output.filename, expression="_") |
---|
353 | if not name in parent.indice_load_data.keys(): |
---|
354 | parent.indice_load_data[name] = 0 |
---|
355 | else: |
---|
356 | ## create a copy of the loaded data |
---|
357 | parent.indice_load_data[name] += 1 |
---|
358 | name = name +"[%i]"%parent.indice_load_data[name] |
---|
359 | |
---|
360 | new_plot.name = name |
---|
361 | ## allow to highlight data when plotted |
---|
362 | new_plot.interactive = True |
---|
363 | ## when 2 data have the same id override the 1 st plotted |
---|
364 | new_plot.id = name |
---|
365 | ##group_id specify on which panel to plot this data |
---|
366 | new_plot.group_id = name |
---|
367 | new_plot.is_data = True |
---|
368 | ##post data to plot |
---|
369 | title = output.filename |
---|
370 | if hasattr(new_plot,"title"): |
---|
371 | title = str(new_plot.title.lstrip().rstrip()) |
---|
372 | if title == "": |
---|
373 | title = str(name) |
---|
374 | else: |
---|
375 | title = str(name) |
---|
376 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
377 | existing_panel = parent.panel_on_focus |
---|
378 | panel_name = existing_panel.window_caption |
---|
379 | data_name = new_plot.name |
---|
380 | if enable_add_data(existing_panel, new_plot): |
---|
381 | if open_dialog_append_data(panel_name, data_name): |
---|
382 | #add this plot the an existing panel |
---|
383 | new_plot.group_id = existing_panel.group_id |
---|
384 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=title)) |
---|
385 | |
---|
386 | ## the output of the loader is a list , some xml files contain more than one data |
---|
387 | else: |
---|
388 | i=1 |
---|
389 | for item in output: |
---|
390 | msg = "Loading 1D data: %s"%str(item.run[0]) |
---|
391 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
392 | try: |
---|
393 | dx = item.dx |
---|
394 | dxl = item.dxl |
---|
395 | dxw = item.dxw |
---|
396 | except: |
---|
397 | dx = None |
---|
398 | dxl = None |
---|
399 | dxw = None |
---|
400 | |
---|
401 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy) |
---|
402 | new_plot.copy_from_datainfo(item) |
---|
403 | item.clone_without_data(clone=new_plot) |
---|
404 | new_plot.dxl = dxl |
---|
405 | new_plot.dxw = dxw |
---|
406 | |
---|
407 | name = parse_name(name=str(item.run[0]), expression="_") |
---|
408 | if not name in parent.indice_load_data.keys(): |
---|
409 | parent.indice_load_data[name] = 0 |
---|
410 | else: |
---|
411 | ## create a copy of the loaded data |
---|
412 | |
---|
413 | #TODO: this is a very annoying feature. We should make this |
---|
414 | # an option. Excel doesn't do this. Why should we? |
---|
415 | # What is the requirement for this feature, and are the |
---|
416 | # counter arguments stronger? Is this feature developed |
---|
417 | # to please at least 80% of the users or a special few? |
---|
418 | parent.indice_load_data[name] += 1 |
---|
419 | name = name + "(copy %i)"%parent.indice_load_data[name] |
---|
420 | |
---|
421 | new_plot.name = name |
---|
422 | new_plot.interactive = True |
---|
423 | new_plot.group_id = name |
---|
424 | new_plot.id = name |
---|
425 | new_plot.is_data = True |
---|
426 | |
---|
427 | if hasattr(item,"title"): |
---|
428 | title = item.title.lstrip().rstrip() |
---|
429 | if title == "": |
---|
430 | title = str(name) |
---|
431 | else: |
---|
432 | title = name |
---|
433 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
434 | existing_panel = parent.panel_on_focus |
---|
435 | panel_name = existing_panel.window_caption |
---|
436 | data_name = new_plot.name |
---|
437 | if enable_add_data(existing_panel, new_plot): |
---|
438 | if open_dialog_append_data(panel_name, data_name): |
---|
439 | #add this plot the an existing panel |
---|
440 | new_plot.group_id = existing_panel.group_id |
---|
441 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
442 | i+=1 |
---|
443 | |
---|