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