1 | |
---|
2 | """ |
---|
3 | plugin DataLoader responsible of loading data |
---|
4 | """ |
---|
5 | import os |
---|
6 | import sys |
---|
7 | import wx |
---|
8 | import logging |
---|
9 | |
---|
10 | from DataLoader.loader import Loader |
---|
11 | import DataLoader.data_info as DataInfo |
---|
12 | from sans.guiframe.plugin_base import PluginBase |
---|
13 | from sans.guiframe.events import StatusEvent |
---|
14 | from sans.guiframe.events import NewPlotEvent |
---|
15 | from sans.guiframe.dataFitting import Data1D |
---|
16 | from sans.guiframe.dataFitting import Data2D |
---|
17 | from sans.guiframe.utils import parse_name |
---|
18 | |
---|
19 | STATE_FILE_EXT = ['SansView files (*.svs)|*.svs', |
---|
20 | 'P(r) files (*.prv)|*.prv', |
---|
21 | 'Fitting files (*.fitv)|*.fitv', |
---|
22 | 'Invariant files (*.inv)|*.inv'] |
---|
23 | EXTENSIONS = ['.svs', '.prv','.fitv', '.inv'] |
---|
24 | |
---|
25 | class Plugin(PluginBase): |
---|
26 | |
---|
27 | def __init__(self, standalone=False): |
---|
28 | PluginBase.__init__(self, name="DataLoader", standalone=standalone) |
---|
29 | #Default location |
---|
30 | self._default_save_location = None |
---|
31 | self.data_name_dict = {} |
---|
32 | self.loader = Loader() |
---|
33 | |
---|
34 | def populate_file_menu(self): |
---|
35 | """ |
---|
36 | get a menu item and append it under file menu of the application |
---|
37 | add load file menu item and load folder item |
---|
38 | """ |
---|
39 | |
---|
40 | hint_load_file = "Read state's files and load them into the application" |
---|
41 | return [["Open State from File", hint_load_file, self.load_file]] |
---|
42 | |
---|
43 | def load_data(self, event): |
---|
44 | """ |
---|
45 | Load data |
---|
46 | """ |
---|
47 | flag = True |
---|
48 | file_list = self.choose_data_file(flag) |
---|
49 | if not file_list or file_list[0] is None: |
---|
50 | return |
---|
51 | self.get_data(file_list, flag=flag) |
---|
52 | |
---|
53 | def can_load_data(self): |
---|
54 | """ |
---|
55 | if return True, then call handler to laod data |
---|
56 | """ |
---|
57 | return True |
---|
58 | |
---|
59 | def load_file(self, event): |
---|
60 | """ |
---|
61 | Load sansview defined files |
---|
62 | """ |
---|
63 | flag = False |
---|
64 | file_list = self.choose_data_file(flag) |
---|
65 | if not file_list or file_list[0] is None: |
---|
66 | return |
---|
67 | self.get_state(file_list) |
---|
68 | |
---|
69 | def load_folder(self, event): |
---|
70 | """ |
---|
71 | Load entire folder |
---|
72 | """ |
---|
73 | flag = True |
---|
74 | path = self.choose_data_folder(flag) |
---|
75 | if path is None: |
---|
76 | return |
---|
77 | file_list = self.get_file_path(path) |
---|
78 | self.get_data(file_list, flag=flag) |
---|
79 | |
---|
80 | def get_wild_card(self, flag=True): |
---|
81 | """ |
---|
82 | :param flag: is True load only data file, else load state file |
---|
83 | return wild cards |
---|
84 | """ |
---|
85 | if flag: |
---|
86 | cards = self.loader.get_wildcards() |
---|
87 | for item in STATE_FILE_EXT: |
---|
88 | if item in cards: |
---|
89 | cards.remove(item) |
---|
90 | else: |
---|
91 | cards = STATE_FILE_EXT |
---|
92 | return '|'.join(cards) |
---|
93 | |
---|
94 | |
---|
95 | def choose_data_file(self, flag=True): |
---|
96 | """ |
---|
97 | Open the file dialog to load file(s) |
---|
98 | """ |
---|
99 | path = None |
---|
100 | if self._default_save_location == None: |
---|
101 | self._default_save_location = os.getcwd() |
---|
102 | |
---|
103 | cards = self.loader.get_wildcards() |
---|
104 | wlist = self.get_wild_card(flag) |
---|
105 | if flag: |
---|
106 | style = wx.OPEN|wx.FD_MULTIPLE |
---|
107 | else: |
---|
108 | style = wx.OPEN|wx.FD_DEFAULT_STYLE |
---|
109 | |
---|
110 | dlg = wx.FileDialog(self.parent, |
---|
111 | "Choose a file", |
---|
112 | self._default_save_location, "", |
---|
113 | wlist, |
---|
114 | style=style) |
---|
115 | if dlg.ShowModal() == wx.ID_OK: |
---|
116 | path = dlg.GetPaths() |
---|
117 | if len(path) >= 0 and not(path[0]is None): |
---|
118 | self._default_save_location = os.path.dirname(path[0]) |
---|
119 | dlg.Destroy() |
---|
120 | return path |
---|
121 | |
---|
122 | def choose_data_folder(self, flag=True): |
---|
123 | """ |
---|
124 | :param flag: is True load only data file, else load state file |
---|
125 | return a list of folder to read |
---|
126 | """ |
---|
127 | path = None |
---|
128 | if self._default_save_location == None: |
---|
129 | self._default_save_location = os.getcwd() |
---|
130 | |
---|
131 | wlist = self.get_wild_card(flag) |
---|
132 | |
---|
133 | dlg = wx.DirDialog(self.parent, "Choose a directory", |
---|
134 | self._default_save_location, |
---|
135 | style=wx.DD_DEFAULT_STYLE) |
---|
136 | if dlg.ShowModal() == wx.ID_OK: |
---|
137 | path = dlg.GetPath() |
---|
138 | self._default_save_location = path |
---|
139 | dlg.Destroy() |
---|
140 | return path |
---|
141 | |
---|
142 | def load_error(self, error=None): |
---|
143 | """ |
---|
144 | Pop up an error message. |
---|
145 | |
---|
146 | :param error: details error message to be displayed |
---|
147 | """ |
---|
148 | message = "The data file you selected could not be loaded.\n" |
---|
149 | message += "Make sure the content of your file" |
---|
150 | message += " is properly formatted.\n\n" |
---|
151 | |
---|
152 | if error is not None: |
---|
153 | message += "When contacting the DANSE team, mention the" |
---|
154 | message += " following:\n%s" % str(error) |
---|
155 | dial = wx.MessageDialog(self.parent, message, 'Error Loading File', |
---|
156 | wx.OK | wx.ICON_EXCLAMATION) |
---|
157 | dial.ShowModal() |
---|
158 | |
---|
159 | def get_file_path(self, path): |
---|
160 | """ |
---|
161 | Receive a list containing folder then return a list of file |
---|
162 | """ |
---|
163 | if os.path.isdir(path): |
---|
164 | return [os.path.join(os.path.abspath(path), |
---|
165 | file) for file in os.listdir(path)] |
---|
166 | |
---|
167 | def get_state(self, path, format=None): |
---|
168 | """ |
---|
169 | read state |
---|
170 | """ |
---|
171 | output = [] |
---|
172 | for p_file in path: |
---|
173 | basename = os.path.basename(p_file) |
---|
174 | root, extension = os.path.splitext(basename) |
---|
175 | for extension in EXTENSIONS: |
---|
176 | temp = self.loader.load(p_file, extension) |
---|
177 | if temp.__class__.__name__ == "list": |
---|
178 | for item in temp: |
---|
179 | data = self.create_data(item, p_file) |
---|
180 | else: |
---|
181 | data = self.create_data(temp, p_file) |
---|
182 | output.append(data) |
---|
183 | self.load_complete(output=output, path=path) |
---|
184 | |
---|
185 | def get_data(self, path, format=None, flag=True): |
---|
186 | """ |
---|
187 | """ |
---|
188 | message = "" |
---|
189 | log_msg = '' |
---|
190 | output = [] |
---|
191 | error_message = "" |
---|
192 | for p_file in path: |
---|
193 | basename = os.path.basename(p_file) |
---|
194 | root, extension = os.path.splitext(basename) |
---|
195 | if flag: |
---|
196 | if extension.lower() in EXTENSIONS: |
---|
197 | log_msg = "Data Loader cannot " |
---|
198 | log_msg += "load: %s\n" % str(p_file) |
---|
199 | log_msg += "Try File -> open ...." |
---|
200 | logging.info(log_msg) |
---|
201 | continue |
---|
202 | else: |
---|
203 | if extension.lower() not in EXTENSIONS: |
---|
204 | log_msg = "File Loader cannot" |
---|
205 | log_msg += " load: %s\n" % str(p_file) |
---|
206 | log_msg += "Try Data -> Load ...." |
---|
207 | logging.info(log_msg) |
---|
208 | continue |
---|
209 | try: |
---|
210 | temp = self.loader.load(p_file, format) |
---|
211 | if temp.__class__.__name__ == "list": |
---|
212 | print "list", temp |
---|
213 | for item in temp: |
---|
214 | data = self.create_data(item, p_file) |
---|
215 | output.append(data) |
---|
216 | else: |
---|
217 | print "not list", [temp] |
---|
218 | data = self.create_data(temp, p_file) |
---|
219 | output.append(data) |
---|
220 | message = "Loading ..." + str(p_file) + "\n" |
---|
221 | self.load_update(output=output, message=message) |
---|
222 | except: |
---|
223 | raise |
---|
224 | error_message = "Error while loading: %s\n" % str(p_file) |
---|
225 | error_message += str(sys.exc_value) + "\n" |
---|
226 | self.load_update(output=output, message=error_message) |
---|
227 | |
---|
228 | message = "Loading Complete! " |
---|
229 | message += log_msg |
---|
230 | self.load_complete(output=output, error_message=error_message, |
---|
231 | message=message, path=path, flag=flag) |
---|
232 | |
---|
233 | |
---|
234 | def load_update(self, output=None, message=""): |
---|
235 | """ |
---|
236 | print update on the status bar |
---|
237 | """ |
---|
238 | if message != "": |
---|
239 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
240 | type="progress", |
---|
241 | info="warning")) |
---|
242 | |
---|
243 | def load_complete(self, output, message="", error_message="", |
---|
244 | path=None, flag=True): |
---|
245 | """ |
---|
246 | post message to status bar and return list of data |
---|
247 | """ |
---|
248 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
249 | info="warning", |
---|
250 | type="stop")) |
---|
251 | if error_message != "": |
---|
252 | self.load_error(error_message) |
---|
253 | self.parent.add_data(output, flag=flag) |
---|
254 | |
---|
255 | def create_data(self, data, path): |
---|
256 | """ |
---|
257 | Receive data from loader and create a data to use for guiframe |
---|
258 | """ |
---|
259 | |
---|
260 | if issubclass(DataInfo.Data2D, data.__class__): |
---|
261 | new_plot = Data2D(image=None, err_image=None) |
---|
262 | else: |
---|
263 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
264 | |
---|
265 | new_plot.copy_from_datainfo(data) |
---|
266 | data.clone_without_data(clone=new_plot) |
---|
267 | #creating a name for data |
---|
268 | name = "" |
---|
269 | title = "" |
---|
270 | file_name = "" |
---|
271 | if path is not None: |
---|
272 | file_name = os.path.basename(path) |
---|
273 | if data.run: |
---|
274 | name = data.run[0] |
---|
275 | if name == "": |
---|
276 | name = file_name |
---|
277 | ## name of the data allow to differentiate data when plotted |
---|
278 | name = parse_name(name=name, expression="_") |
---|
279 | |
---|
280 | max_char = name.find("[") |
---|
281 | if max_char < 0: |
---|
282 | max_char = len(name) |
---|
283 | name = name[0:max_char] |
---|
284 | |
---|
285 | if name not in self.data_name_dict: |
---|
286 | self.data_name_dict[name] = 0 |
---|
287 | else: |
---|
288 | self.data_name_dict[name] += 1 |
---|
289 | name = name + " [" + str(self.data_name_dict[name]) + "]" |
---|
290 | #find title |
---|
291 | if data.title.strip(): |
---|
292 | title = data.title |
---|
293 | if title.strip() == "": |
---|
294 | title = file_name |
---|
295 | |
---|
296 | if new_plot.filename.strip() == "": |
---|
297 | new_plot.filename = file_name |
---|
298 | |
---|
299 | new_plot.name = name |
---|
300 | new_plot.title = title |
---|
301 | ## allow to highlight data when plotted |
---|
302 | new_plot.interactive = True |
---|
303 | ## when 2 data have the same id override the 1 st plotted |
---|
304 | new_plot.id = name |
---|
305 | ##group_id specify on which panel to plot this data |
---|
306 | new_plot.group_id = name |
---|
307 | new_plot.is_data = True |
---|
308 | new_plot.path = path |
---|
309 | ##post data to plot |
---|
310 | # plot data |
---|
311 | return new_plot |
---|
312 | |
---|
313 | |
---|