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