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