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 | from sans.perspectives.invariant import InvStateUpdateEvent |
---|
11 | from perspectives.fitting import FitStateUpdateEvent |
---|
12 | |
---|
13 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
14 | |
---|
15 | def enable_add_data(existing_panel, new_plot): |
---|
16 | """ |
---|
17 | Enable append data on a plot panel |
---|
18 | """ |
---|
19 | is_theory = len(existing_panel.plots)<= 1 and \ |
---|
20 | existing_panel.plots.values()[0].__class__.__name__=="Theory1D" |
---|
21 | |
---|
22 | is_data2d = hasattr(new_plot, 'data') |
---|
23 | is_data1d = existing_panel.__class__.__name__ == "ModelPanel1D"\ |
---|
24 | and existing_panel.group_id is not None |
---|
25 | has_meta_data = hasattr(new_plot, 'meta_data') |
---|
26 | |
---|
27 | #disable_add_data if the data is being recovered from a saved state file. |
---|
28 | is_state_data = False |
---|
29 | if has_meta_data: |
---|
30 | if 'invstate' in new_plot.meta_data: is_state_data = True |
---|
31 | if 'prstate' in new_plot.meta_data: is_state_data = True |
---|
32 | if 'fitstate' in new_plot.meta_data: is_state_data = True |
---|
33 | |
---|
34 | return is_data1d and not is_data2d and not is_theory and not is_state_data |
---|
35 | |
---|
36 | def parse_name(name, expression): |
---|
37 | """ |
---|
38 | remove "_" in front of a name |
---|
39 | """ |
---|
40 | if re.match(expression, name) is not None: |
---|
41 | word = re.split(expression, name, 1) |
---|
42 | for item in word: |
---|
43 | if item.lstrip().rstrip() != '': |
---|
44 | return item |
---|
45 | else: |
---|
46 | return name |
---|
47 | |
---|
48 | def choose_data_file(parent, location=None): |
---|
49 | """ |
---|
50 | """ |
---|
51 | path = None |
---|
52 | if location == None: |
---|
53 | location = os.getcwd() |
---|
54 | |
---|
55 | l = Loader() |
---|
56 | cards = l.get_wildcards() |
---|
57 | wlist = '|'.join(cards) |
---|
58 | |
---|
59 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) |
---|
60 | if dlg.ShowModal() == wx.ID_OK: |
---|
61 | path = dlg.GetPath() |
---|
62 | mypath = os.path.basename(path) |
---|
63 | dlg.Destroy() |
---|
64 | |
---|
65 | return path |
---|
66 | |
---|
67 | def open_dialog_append_data(panel_name, data_name): |
---|
68 | """ |
---|
69 | Pop up an error message. |
---|
70 | |
---|
71 | :param panel_name: the name of the current panel |
---|
72 | :param data_name: the name of the current data |
---|
73 | |
---|
74 | """ |
---|
75 | message = " Do you want to append %s data\n in "%(str(data_name)) |
---|
76 | message += " %s panel?\n\n"%(str(panel_name)) |
---|
77 | dial = wx.MessageDialog(None, message, 'Question', |
---|
78 | wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION) |
---|
79 | if dial.ShowModal() == wx.ID_YES: |
---|
80 | return True |
---|
81 | else: |
---|
82 | return False |
---|
83 | |
---|
84 | |
---|
85 | def load_ascii_1D(path): |
---|
86 | """ |
---|
87 | Load a 1D ascii file, with errors |
---|
88 | """ |
---|
89 | if path and os.path.isfile(path): |
---|
90 | |
---|
91 | file_x = numpy.zeros(0) |
---|
92 | file_y = numpy.zeros(0) |
---|
93 | file_dy = numpy.zeros(0) |
---|
94 | file_dx = numpy.zeros(0) |
---|
95 | |
---|
96 | input_f = open(path,'r') |
---|
97 | buff = input_f.read() |
---|
98 | lines = buff.split('\n') |
---|
99 | |
---|
100 | has_dy = False |
---|
101 | has_dx = False |
---|
102 | |
---|
103 | for line in lines: |
---|
104 | try: |
---|
105 | toks = line.split() |
---|
106 | x = float(toks[0]) |
---|
107 | y = float(toks[1]) |
---|
108 | if len(toks)==3: |
---|
109 | has_dy = True |
---|
110 | errdy = float(toks[2]) |
---|
111 | else: |
---|
112 | errdy = 0.0 |
---|
113 | if len(toks) == 4: |
---|
114 | has_dx = True |
---|
115 | errdx = float(toks[3]) |
---|
116 | else: |
---|
117 | errdx = 0.0 |
---|
118 | file_x = numpy.append(file_x, x) |
---|
119 | file_y = numpy.append(file_y, y) |
---|
120 | file_dy = numpy.append(file_dy, dyerr) |
---|
121 | file_dx = numpy.append(file_dx, dxerr) |
---|
122 | except: |
---|
123 | print "READ ERROR", line |
---|
124 | |
---|
125 | if has_dy == False: |
---|
126 | file_dy = None |
---|
127 | if has_dx == False: |
---|
128 | file_dx = None |
---|
129 | |
---|
130 | return file_x, file_y, file_dy, file_dx |
---|
131 | return None, None, None, None |
---|
132 | |
---|
133 | def load_error(error=None): |
---|
134 | """ |
---|
135 | Pop up an error message. |
---|
136 | |
---|
137 | :param error: details error message to be displayed |
---|
138 | """ |
---|
139 | message = "The data file you selected could not be loaded.\n" |
---|
140 | message += "Make sure the content of your file is properly formatted.\n\n" |
---|
141 | |
---|
142 | if error is not None: |
---|
143 | message += "When contacting the DANSE team, mention the following:\n%s" % str(error) |
---|
144 | |
---|
145 | dial = wx.MessageDialog(None, message, 'Error Loading File', wx.OK | wx.ICON_EXCLAMATION) |
---|
146 | dial.ShowModal() |
---|
147 | |
---|
148 | def on_load_error(parent): |
---|
149 | """ |
---|
150 | """ |
---|
151 | wx.PostEvent(parent, StatusEvent(status="Load cancel..", info="warning", |
---|
152 | type="stop")) |
---|
153 | |
---|
154 | def plot_data(parent, path, format=None): |
---|
155 | """ |
---|
156 | Use the DataLoader loader to created data to plot. |
---|
157 | |
---|
158 | :param path: the path of the data to load |
---|
159 | : param format: file format (as file extension) |
---|
160 | """ |
---|
161 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
162 | from DataLoader.loader import Loader |
---|
163 | |
---|
164 | # Instantiate a loader |
---|
165 | L = Loader() |
---|
166 | |
---|
167 | # Load data |
---|
168 | try: |
---|
169 | output = L.load(path, format) |
---|
170 | except: |
---|
171 | load_error(sys.exc_value) |
---|
172 | return |
---|
173 | |
---|
174 | # Notify user if the loader completed the load but no data came out |
---|
175 | if output == None: |
---|
176 | basename = os.path.basename(path) |
---|
177 | if not basename.endswith('.svs'): |
---|
178 | load_error("The data file appears to be empty.") |
---|
179 | return |
---|
180 | |
---|
181 | |
---|
182 | filename = os.path.basename(path) |
---|
183 | if not output.__class__.__name__ == "list": |
---|
184 | ## Creating a Data2D with output |
---|
185 | if hasattr(output,'data'): |
---|
186 | msg = "Loading 2D data: %s"%output.filename |
---|
187 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
188 | new_plot = Data2D(image=None, err_image=None) |
---|
189 | |
---|
190 | else: |
---|
191 | msg = "Loading 1D data: %s"%output.filename |
---|
192 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
193 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
194 | |
---|
195 | new_plot.copy_from_datainfo(output) |
---|
196 | output.clone_without_data(clone=new_plot) |
---|
197 | |
---|
198 | ## data 's name |
---|
199 | if output.filename is None or output.filename == "": |
---|
200 | output.filename = str(filename) |
---|
201 | ## name of the data allow to differentiate data when plotted |
---|
202 | name = parse_name(name=output.filename, expression="_") |
---|
203 | |
---|
204 | new_plot.name = name |
---|
205 | ## allow to highlight data when plotted |
---|
206 | new_plot.interactive = True |
---|
207 | ## when 2 data have the same id override the 1 st plotted |
---|
208 | new_plot.id = name |
---|
209 | ##group_id specify on which panel to plot this data |
---|
210 | new_plot.group_id = name |
---|
211 | new_plot.is_data = True |
---|
212 | ##post data to plot |
---|
213 | title = output.filename |
---|
214 | if hasattr(new_plot,"title"): |
---|
215 | title = str(new_plot.title.lstrip().rstrip()) |
---|
216 | if title == "": |
---|
217 | title = str(name) |
---|
218 | else: |
---|
219 | title = str(name) |
---|
220 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
221 | existing_panel = parent.panel_on_focus |
---|
222 | panel_name = existing_panel.window_caption |
---|
223 | data_name = new_plot.name |
---|
224 | if enable_add_data(existing_panel, new_plot): |
---|
225 | if open_dialog_append_data(panel_name, data_name): |
---|
226 | #add this plot the an existing panel |
---|
227 | new_plot.group_id = existing_panel.group_id |
---|
228 | # plot data |
---|
229 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=title)) |
---|
230 | # set state and plot computation if exists |
---|
231 | wx.PostEvent(parent,InvStateUpdateEvent()) |
---|
232 | wx.PostEvent(parent,FitStateUpdateEvent()) |
---|
233 | ## the output of the loader is a list , some xml files contain more than one data |
---|
234 | else: |
---|
235 | i=0 |
---|
236 | for item in output: |
---|
237 | try: |
---|
238 | ## Creating a Data2D with output |
---|
239 | if hasattr(item,'data'): |
---|
240 | msg = "Loading 2D data: %s"%item.filename |
---|
241 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
242 | new_plot = Data2D(image=None, err_image=None) |
---|
243 | |
---|
244 | else: |
---|
245 | msg = "Loading 1D data: %s"%str(item.run[0]) |
---|
246 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
247 | try: |
---|
248 | dx = item.dx |
---|
249 | dxl = item.dxl |
---|
250 | dxw = item.dxw |
---|
251 | except: |
---|
252 | dx = None |
---|
253 | dxl = None |
---|
254 | dxw = None |
---|
255 | |
---|
256 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy) |
---|
257 | if dxl != None: |
---|
258 | new_plot.dxl = dxl |
---|
259 | if dxl != None: |
---|
260 | new_plot.dxw = dxw |
---|
261 | |
---|
262 | new_plot.copy_from_datainfo(item) |
---|
263 | item.clone_without_data(clone=new_plot) |
---|
264 | |
---|
265 | name = parse_name(name=str(item.run[0]), expression="_") |
---|
266 | |
---|
267 | #TODO: this is a very annoying feature. We should make this |
---|
268 | # an option. Excel doesn't do this. Why should we? |
---|
269 | # What is the requirement for this feature, and are the |
---|
270 | # counter arguments stronger? Is this feature developed |
---|
271 | # to please at least 80% of the users or a special few? |
---|
272 | new_plot.name = name |
---|
273 | new_plot.interactive = True |
---|
274 | new_plot.group_id = name |
---|
275 | new_plot.id = name |
---|
276 | new_plot.is_data = True |
---|
277 | title = item.filename |
---|
278 | |
---|
279 | if hasattr(item,"title"): |
---|
280 | title = item.title.lstrip().rstrip() |
---|
281 | if title == "": |
---|
282 | title = str(name) |
---|
283 | else: |
---|
284 | title = name |
---|
285 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
286 | existing_panel = parent.panel_on_focus |
---|
287 | panel_name = existing_panel.window_caption |
---|
288 | data_name = new_plot.name |
---|
289 | if enable_add_data(existing_panel, new_plot): |
---|
290 | if open_dialog_append_data(panel_name, data_name): |
---|
291 | #add this plot the an existing panel |
---|
292 | new_plot.group_id = existing_panel.group_id |
---|
293 | # plot data |
---|
294 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
295 | |
---|
296 | # set state and plot computation if exists |
---|
297 | wx.PostEvent(parent,InvStateUpdateEvent()) |
---|
298 | wx.PostEvent(parent,FitStateUpdateEvent()) |
---|
299 | except: |
---|
300 | raise |
---|