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