[b7c7a1c] | 1 | |
---|
| 2 | """ |
---|
| 3 | plugin DataLoader responsible of loading data |
---|
| 4 | """ |
---|
[f444b20] | 5 | import os |
---|
| 6 | import sys |
---|
| 7 | import wx |
---|
[df7046f] | 8 | import logging |
---|
[b7c7a1c] | 9 | |
---|
[f444b20] | 10 | from DataLoader.loader import Loader |
---|
| 11 | import DataLoader.data_info as DataInfo |
---|
[b7c7a1c] | 12 | from sans.guiframe.plugin_base import PluginBase |
---|
[f444b20] | 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 | |
---|
[52725d6] | 19 | STATE_FILE_EXT = ['SansView files (*.svs)|*.svs', |
---|
| 20 | 'P(r) files (*.prv)|*.prv', |
---|
[f444b20] | 21 | 'Fitting files (*.fitv)|*.fitv', |
---|
[52725d6] | 22 | 'Invariant files (*.inv)|*.inv'] |
---|
| 23 | EXTENSIONS = ['.svs', '.prv','.fitv', '.inv'] |
---|
[b7c7a1c] | 24 | |
---|
| 25 | class Plugin(PluginBase): |
---|
| 26 | |
---|
| 27 | def __init__(self, standalone=False): |
---|
[f444b20] | 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 | |
---|
[b7c7a1c] | 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 | """ |
---|
[f444b20] | 39 | |
---|
| 40 | hint_load_file = "Read state's files and load them into the application" |
---|
[52b8b74] | 41 | return [["Open State from File", hint_load_file, self.load_file]] |
---|
[b7c7a1c] | 42 | |
---|
[52b8b74] | 43 | def load_data(self, event): |
---|
[f444b20] | 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 | |
---|
[52b8b74] | 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): |
---|
[b7c7a1c] | 60 | """ |
---|
[f444b20] | 61 | Load sansview defined files |
---|
[b7c7a1c] | 62 | """ |
---|
[f444b20] | 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_data(file_list, flag=flag) |
---|
| 68 | |
---|
[52b8b74] | 69 | def load_folder(self, event): |
---|
[b7c7a1c] | 70 | """ |
---|
| 71 | Load entire folder |
---|
[5878f190] | 72 | """ |
---|
[f444b20] | 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) |
---|
[52b8b74] | 79 | |
---|
[f444b20] | 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_data(self, path, format=None, flag=True): |
---|
| 168 | """ |
---|
| 169 | """ |
---|
| 170 | message = "" |
---|
[df7046f] | 171 | log_msg = '' |
---|
[f444b20] | 172 | output = [] |
---|
| 173 | error_message = "" |
---|
| 174 | for p_file in path: |
---|
| 175 | basename = os.path.basename(p_file) |
---|
| 176 | root, extension = os.path.splitext(basename) |
---|
| 177 | if flag: |
---|
| 178 | if extension.lower() in EXTENSIONS: |
---|
[df7046f] | 179 | log_msg = "Data Loader cannot " |
---|
| 180 | log_msg += "load: %s\n" % str(p_file) |
---|
| 181 | log_msg += "Try File -> open ...." |
---|
| 182 | logging.info(log_msg) |
---|
[f444b20] | 183 | continue |
---|
| 184 | else: |
---|
| 185 | if extension.lower() not in EXTENSIONS: |
---|
[df7046f] | 186 | log_msg = "File Loader cannot" |
---|
| 187 | log_msg += " load: %s\n" % str(p_file) |
---|
| 188 | log_msg += "Try Data -> Load ...." |
---|
| 189 | logging.info(log_msg) |
---|
[f444b20] | 190 | continue |
---|
| 191 | try: |
---|
| 192 | temp = self.loader.load(p_file) |
---|
| 193 | if temp.__class__.__name__ == "list": |
---|
[52725d6] | 194 | print "list", temp |
---|
[f444b20] | 195 | for item in temp: |
---|
| 196 | data = self.create_data(item, p_file) |
---|
| 197 | output.append(data) |
---|
| 198 | else: |
---|
[52725d6] | 199 | print "list", temp |
---|
[f444b20] | 200 | data = self.create_data(temp, p_file) |
---|
| 201 | output.append(data) |
---|
| 202 | message = "Loading ..." + str(p_file) + "\n" |
---|
| 203 | self.load_update(output=output, message=message) |
---|
| 204 | except: |
---|
[52725d6] | 205 | raise |
---|
[f444b20] | 206 | error_message = "Error while loading: %s\n" % str(p_file) |
---|
| 207 | error_message += str(sys.exc_value) + "\n" |
---|
| 208 | self.load_update(output=output, message=error_message) |
---|
| 209 | |
---|
[df7046f] | 210 | message = "Loading Complete! " |
---|
| 211 | message += log_msg |
---|
[f444b20] | 212 | self.load_complete(output=output, error_message=error_message, |
---|
[52725d6] | 213 | message=message, path=path, flag=flag) |
---|
[f444b20] | 214 | |
---|
| 215 | |
---|
| 216 | def old_get_data(self, path, format=None, flag=True): |
---|
| 217 | """ |
---|
| 218 | :param flag: is True load only data file, else load state file |
---|
| 219 | Receive a list of file paths and return a list of Data objects |
---|
| 220 | """ |
---|
| 221 | from .load_thread import DataReader |
---|
| 222 | message = "Start Loading \n" |
---|
| 223 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 224 | info="info", type="progress")) |
---|
| 225 | calc_load = DataReader(loader=self.loader, |
---|
| 226 | path=path, |
---|
| 227 | flag=flag, |
---|
| 228 | transform_data=self.create_data, |
---|
| 229 | updatefn=self.load_update, |
---|
| 230 | completefn=self.load_complete) |
---|
| 231 | calc_load.queue() |
---|
| 232 | |
---|
| 233 | def load_update(self, output=None, message=""): |
---|
| 234 | """ |
---|
| 235 | print update on the status bar |
---|
| 236 | """ |
---|
| 237 | if message != "": |
---|
| 238 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 239 | type="progress", |
---|
| 240 | info="warning")) |
---|
| 241 | |
---|
[52725d6] | 242 | def load_complete(self, output, message="", error_message="", |
---|
| 243 | path=None, flag=True): |
---|
[f444b20] | 244 | """ |
---|
| 245 | post message to status bar and return list of data |
---|
| 246 | """ |
---|
| 247 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 248 | info="warning", |
---|
| 249 | type="stop")) |
---|
| 250 | if error_message != "": |
---|
| 251 | self.load_error(error_message) |
---|
[52725d6] | 252 | self.parent.add_data(output, flag=flag) |
---|
[de48a1a] | 253 | |
---|
[f444b20] | 254 | def create_data(self, data, path): |
---|
| 255 | """ |
---|
| 256 | Receive data from loader and create a data to use for guiframe |
---|
| 257 | """ |
---|
| 258 | |
---|
| 259 | if issubclass(DataInfo.Data2D, data.__class__): |
---|
| 260 | new_plot = Data2D(image=None, err_image=None) |
---|
| 261 | else: |
---|
| 262 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
| 263 | |
---|
| 264 | new_plot.copy_from_datainfo(data) |
---|
| 265 | data.clone_without_data(clone=new_plot) |
---|
| 266 | #creating a name for data |
---|
| 267 | name = "" |
---|
| 268 | title = "" |
---|
| 269 | file_name = "" |
---|
| 270 | if path is not None: |
---|
| 271 | file_name = os.path.basename(path) |
---|
| 272 | if data.run: |
---|
| 273 | name = data.run[0] |
---|
| 274 | if name == "": |
---|
| 275 | name = file_name |
---|
| 276 | ## name of the data allow to differentiate data when plotted |
---|
| 277 | name = parse_name(name=name, expression="_") |
---|
| 278 | |
---|
| 279 | max_char = name.find("[") |
---|
| 280 | if max_char < 0: |
---|
| 281 | max_char = len(name) |
---|
| 282 | name = name[0:max_char] |
---|
| 283 | |
---|
| 284 | if name not in self.data_name_dict: |
---|
| 285 | self.data_name_dict[name] = 0 |
---|
| 286 | else: |
---|
| 287 | self.data_name_dict[name] += 1 |
---|
| 288 | name = name + " [" + str(self.data_name_dict[name]) + "]" |
---|
| 289 | #find title |
---|
| 290 | if data.title.strip(): |
---|
| 291 | title = data.title |
---|
| 292 | if title.strip() == "": |
---|
| 293 | title = file_name |
---|
| 294 | |
---|
| 295 | if new_plot.filename.strip() == "": |
---|
| 296 | new_plot.filename = file_name |
---|
| 297 | |
---|
| 298 | new_plot.name = name |
---|
| 299 | new_plot.title = title |
---|
| 300 | ## allow to highlight data when plotted |
---|
| 301 | new_plot.interactive = True |
---|
| 302 | ## when 2 data have the same id override the 1 st plotted |
---|
| 303 | new_plot.id = name |
---|
| 304 | ##group_id specify on which panel to plot this data |
---|
| 305 | new_plot.group_id = name |
---|
| 306 | new_plot.is_data = True |
---|
| 307 | new_plot.path = path |
---|
| 308 | ##post data to plot |
---|
| 309 | # plot data |
---|
| 310 | return new_plot |
---|
| 311 | |
---|
| 312 | |
---|