Changeset 75fbd17 in sasview for guiframe/local_perspectives
- Timestamp:
- Feb 9, 2011 12:04:24 AM (14 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- c83a5af
- Parents:
- 8240eab
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
guiframe/local_perspectives/data_loader/data_loader.py
rec489f5 r75fbd17 16 16 from sans.guiframe.dataFitting import Data2D 17 17 from sans.guiframe.utils import parse_name 18 from sans.guiframe.gui_style import GUIFRAME 18 19 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 20 EXTENSIONS = ['.svs', '.prv','.fitv', '.inv'] 24 21 … … 30 27 self._default_save_location = None 31 28 self.data_name_dict = {} 32 self.loader = Loader() 29 self.loader = Loader() 30 self._data_menu = None 33 31 34 def populate_ file_menu(self):32 def populate_menu(self, parent): 35 33 """ 36 get a menu item and append it under file menu of the application37 add load file menu item and load folder item38 34 """ 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): 35 # Add menu data 36 self._data_menu = wx.Menu() 37 #menu for data files 38 data_file_id = wx.NewId() 39 data_file_hint = "load one or more data in the application" 40 self._data_menu.Append(data_file_id, 41 '&Load Data File(s)', data_file_hint) 42 wx.EVT_MENU(self.parent, data_file_id, self._load_data) 43 gui_style = self.parent.get_style() 44 style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS 45 style1 = gui_style & GUIFRAME.DATALOADER_ON 46 if style == GUIFRAME.MULTIPLE_APPLICATIONS: 47 #menu for data from folder 48 data_folder_id = wx.NewId() 49 data_folder_hint = "load multiple data in the application" 50 self._data_menu.Append(data_folder_id, 51 '&Load Data Folder', data_folder_hint) 52 wx.EVT_MENU(self.parent, data_folder_id, self._load_folder) 53 54 return [(self._data_menu, 'Data')] 55 56 def _load_data(self, event): 44 57 """ 45 58 Load data 46 59 """ 47 flag = True 48 file_list = self.choose_data_file(flag) 60 if self._default_save_location == None: 61 self._default_save_location = os.getcwd() 62 63 cards = self.loader.get_wildcards() 64 wlist = '|'.join(cards) 65 style = wx.OPEN|wx.FD_MULTIPLE 66 dlg = wx.FileDialog(self.parent, 67 "Choose a file", 68 self._default_save_location, "", 69 wlist, 70 style=style) 71 if dlg.ShowModal() == wx.ID_OK: 72 file_list = dlg.GetPaths() 73 if len(file_list) >= 0 and not(file_list[0]is None): 74 self._default_save_location = os.path.dirname(file_list[0]) 75 dlg.Destroy() 76 49 77 if not file_list or file_list[0] is None: 50 78 return 51 self.get_data(file_list, flag=flag) 79 self.get_data(file_list) 80 self.parent.show_data_panel(event=None) 52 81 53 82 def can_load_data(self): … … 56 85 """ 57 86 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) 87 68 88 69 def load_folder(self, event):89 def _load_folder(self, event): 70 90 """ 71 91 Load entire folder 72 92 """ 73 flag = True74 path = self.choose_data_folder(flag)75 if path is None:76 return77 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 file83 return wild cards84 """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_EXT92 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 = None100 93 if self._default_save_location == None: 101 94 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_MULTIPLE107 else:108 style = wx.OPEN|wx.FD_DEFAULT_STYLE109 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 path121 122 def choose_data_folder(self, flag=True):123 """124 :param flag: is True load only data file, else load state file125 return a list of folder to read126 """127 path = None128 if self._default_save_location == None:129 self._default_save_location = os.getcwd()130 131 wlist = self.get_wild_card(flag)132 133 95 dlg = wx.DirDialog(self.parent, "Choose a directory", 134 96 self._default_save_location, … … 138 100 self._default_save_location = path 139 101 dlg.Destroy() 140 return path 141 102 if path is not None: 103 self._default_save_location = os.path.dirname(path) 104 else: 105 return 106 file_list = self.get_file_path(path) 107 self.get_data(file_list) 108 self.parent.show_data_panel(event=None) 109 110 142 111 def load_error(self, error=None): 143 112 """ … … 164 133 return [os.path.join(os.path.abspath(path), 165 134 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): 135 136 def get_data(self, path, format=None): 186 137 """ 187 138 """ … … 193 144 basename = os.path.basename(p_file) 194 145 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 146 if extension.lower() in EXTENSIONS: 147 log_msg = "Data Loader cannot " 148 log_msg += "load: %s\n" % str(p_file) 149 log_msg += "Try File opening ...." 150 logging.info(log_msg) 151 continue 152 209 153 try: 210 154 temp = self.loader.load(p_file, format) 211 155 if temp.__class__.__name__ == "list": 212 print "list", temp213 156 for item in temp: 214 data = self. create_data(item, p_file)157 data = self.parent.create_gui_data(item, p_file) 215 158 output.append(data) 216 159 else: 217 print "not list", [temp] 218 data = self.create_data(temp, p_file) 160 data = self.parent.create_gui_data(temp, p_file) 219 161 output.append(data) 220 message = "Loading ..." + str(p_file) + "\n"162 message = "Loading Data..." + str(p_file) + "\n" 221 163 self.load_update(output=output, message=message) 222 164 except: 223 raise 224 error_message = "Error while loading: %s\n" % str(p_file) 165 error_message = "Error while loading Data: %s\n" % str(p_file) 225 166 error_message += str(sys.exc_value) + "\n" 226 167 self.load_update(output=output, message=error_message) 227 168 228 message = "Loading Complete! "169 message = "Loading Data Complete! " 229 170 message += log_msg 230 171 self.load_complete(output=output, error_message=error_message, 231 message=message, path=path , flag=flag)172 message=message, path=path) 232 173 233 234 174 def load_update(self, output=None, message=""): 235 175 """ … … 240 180 type="progress", 241 181 info="warning")) 242 243 def load_complete(self, output, message="", error_message="", 244 path=None, flag=True): 182 def load_complete(self, output, message="", error_message="", path=None): 245 183 """ 246 184 post message to status bar and return list of data … … 251 189 if error_message != "": 252 190 self.load_error(error_message) 253 self.parent.add_data(output, flag=flag) 191 self.parent.add_data(output) 192 193 254 194 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 195
Note: See TracChangeset
for help on using the changeset viewer.