Changeset 7673ecd in sasview
- Timestamp:
- Jun 22, 2016 12:23:20 PM (8 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:
- d7573c7
- Parents:
- ad52d31
- Location:
- src/sas/sasgui/perspectives
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/calculator/model_editor.py
r70c5d490 r7673ecd 578 578 else: 579 579 out_f.write(line + "\n") 580 elif line.count("P1 = make_class"):580 elif line.count("P1 = find_model"): 581 581 out_f.write(line % (name1) + "\n") 582 elif line.count("P2 = make_class"):582 elif line.count("P2 = find_model"): 583 583 out_f.write(line % (name2) + "\n") 584 584 … … 1238 1238 ##import scipy? 1239 1239 #class Model(Model1DPlugin): 1240 # name = ""1240 # name = basename without extension of __file__ 1241 1241 # def __init__(self): 1242 1242 # Model1DPlugin.__init__(self, name=self.name) 1243 1243 # #set name same as file name 1244 # self.name = self.get_fname()1245 1244 # #self.params here 1246 1245 # self.description = "%s" … … 1304 1303 """ 1305 1304 TEST_TEMPLATE = """ 1306 def get_fname(self):1307 path = sys._getframe().f_code.co_filename1308 basename = os.path.basename(path)1309 name, _ = os.path.splitext(basename)1310 return name1311 1305 ###################################################################### 1312 1306 ## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! … … 1335 1329 1336 1330 from sas.sascalc.fit.pluginmodel import Model1DPlugin 1337 from sasmodels.sasview_model import make_class 1338 from sasmodels.core import load_model_info 1339 # User can change the name of the model (only with single functional model) 1340 #P1_model: 1341 #from %s import Model as P1 1342 1343 #P2_model: 1344 #from %s import Model as P2 1331 from sasmodels.sasview_model import find_model 1345 1332 1346 1333 class Model(Model1DPlugin): 1347 name = "" 1348 def __init__(self): 1334 name = os.path.splitext(os.path.basename(__file__))[0] 1335 is_multiplicity_model = False 1336 def __init__(self, multiplicity=1): 1349 1337 Model1DPlugin.__init__(self, name='') 1350 P1 = make_class('%s')1351 P2 = make_class('%s')1338 P1 = find_model('%s') 1339 P2 = find_model('%s') 1352 1340 p_model1 = P1() 1353 1341 p_model2 = P2() 1354 1342 ## Setting model name model description 1355 1343 self.description = '%s' 1356 self.name = self.get_fname()1357 1344 if self.name.rstrip().lstrip() == '': 1358 1345 self.name = self._get_name(p_model1.name, p_model2.name) … … 1392 1379 #list of parameter that can be fitted 1393 1380 self._set_fixed_params() 1381 1394 1382 ## parameters with orientation 1383 self.orientation_params = [] 1395 1384 for item in self.p_model1.orientation_params: 1396 1385 new_item = "p1_" + item … … 1403 1392 self.orientation_params.append(new_item) 1404 1393 ## magnetic params 1394 self.magnetic_params = [] 1405 1395 for item in self.p_model1.magnetic_params: 1406 1396 new_item = "p1_" + item … … 1460 1450 1461 1451 def _set_dispersion(self): 1452 self.dispersion = {} 1462 1453 ##set dispersion only from p_model 1463 1454 for name , value in self.p_model1.dispersion.iteritems(): … … 1575 1566 1576 1567 def _set_fixed_params(self): 1568 self.fixed = [] 1577 1569 for item in self.p_model1.fixed: 1578 1570 new_item = "p1" + item … … 1625 1617 self.description += description 1626 1618 1627 def get_fname(self):1628 path = sys._getframe().f_code.co_filename1629 basename = os.path.basename(path)1630 name, _ = os.path.splitext(basename)1631 return name1632 1633 1619 if __name__ == "__main__": 1634 1620 m1= Model() -
src/sas/sasgui/perspectives/calculator/pyconsole.py
r26d6e045 r7673ecd 27 27 """ 28 28 # try running the model 29 from sasmodels. core import load_model, call_kernel30 model = load_model(path)31 29 from sasmodels.sasview_model import load_custom_model 30 Model = load_custom_model(path) 31 model = Model() 32 32 q = np.array([0.01, 0.1]) 33 kernel = model.make_kernel([q]) 34 Iq = call_kernel(kernel, {}) 35 33 Iq = model.evalDistribution(q) 36 34 qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) 37 kernel = model.make_kernel([qx, qy]) 38 Iqxy = call_kernel(kernel, {}) 35 Iqxy = model.evalDistribution([qx, qy]) 39 36 40 37 result = """ … … 56 53 except Exception: 57 54 import traceback 58 result, errmsg = None, traceback.format_exc( limit=2)55 result, errmsg = None, traceback.format_exc() 59 56 60 57 parts = ["Running model '%s'..." % os.path.basename(fname)] -
src/sas/sasgui/perspectives/fitting/basepage.py
rcb4ef58 r7673ecd 12 12 import json 13 13 import logging 14 import traceback 15 14 16 from collections import defaultdict 15 17 from wx.lib.scrolledpanel import ScrolledPanel 18 19 import sasmodels.sasview_model 16 20 from sas.sasgui.guiframe.panel_base import PanelBase 17 21 from sas.sasgui.guiframe.utils import format_number, check_float, IdList … … 852 856 angles.append(angle) 853 857 weights.append(weight) 854 except :858 except Exception: 855 859 # Skip non-data lines 856 logging.error( sys.exc_info()[1])860 logging.error(traceback.format_exc()) 857 861 return numpy.array(angles), numpy.array(weights) 858 862 except: … … 1393 1397 self.model._persistency_dict[key] = \ 1394 1398 [state.values, state.weights] 1395 except :1396 logging.error( sys.exc_info()[1])1399 except Exception: 1400 logging.error(traceback.format_exc()) 1397 1401 selection = self._find_polyfunc_selection(disp_model) 1398 1402 for list in self.fittable_param: … … 1410 1414 list[5].Disable() 1411 1415 list[6].Disable() 1412 except :1413 logging.error( sys.exc_info()[1])1416 except Exception: 1417 logging.error(traceback.format_exc()) 1414 1418 # For array, disable all fixed params 1415 1419 if selection == 1: … … 1419 1423 try: 1420 1424 item[2].Disable() 1421 except :1422 logging.error( sys.exc_info()[1])1425 except Exception: 1426 logging.error(traceback.format_exc()) 1423 1427 1424 1428 # Make sure the check box updated when all checked … … 1507 1511 is_modified = self._check_value_enter(self.parameters, 1508 1512 is_modified) 1509 except :1510 logging.error( sys.exc_info()[1])1513 except Exception: 1514 logging.error(traceback.format_exc()) 1511 1515 1512 1516 # Here we should check whether the boundaries have been modified. … … 1651 1655 try: 1652 1656 self.save_current_state() 1653 except :1654 logging.error( sys.exc_info()[1])1657 except Exception: 1658 logging.error(traceback.format_exc()) 1655 1659 1656 1660 return flag … … 1895 1899 if mod_cat == custom_model: 1896 1900 for model in self.model_list_box[mod_cat]: 1897 if 'sasmodels.sasview_model.' in str(model): 1898 str_m = model.id 1899 else: 1900 str_m = str(model).split(".")[0] 1901 #self.model_box.Append(str_m) 1901 str_m = model.id if hasattr(model, 'id') else model.name 1902 1902 m_list.append(self.model_dict[str_m]) 1903 1903 else: … … 1910 1910 # wx.PostEvent(self.parent.parent, 1911 1911 # StatusEvent(status=msg, info="error")) 1912 except :1913 msg = "%s\n" % (sys.exc_info()[1])1912 except Exception: 1913 msg = traceback.format_exc() 1914 1914 wx.PostEvent(self._manager.parent, 1915 1915 StatusEvent(status=msg, info="error")) … … 2474 2474 try: 2475 2475 self.model.set_dispersion(p, disp_model) 2476 except :2477 logging.error( sys.exc_info()[1])2476 except Exception: 2477 logging.error(traceback.format_exc()) 2478 2478 2479 2479 ## save state into … … 2687 2687 del self.model._persistency_dict[name.split('.')[0]] 2688 2688 del self.state.model._persistency_dict[name.split('.')[0]] 2689 except :2690 logging.error( sys.exc_info()[1])2689 except Exception: 2690 logging.error(traceback.format_exc()) 2691 2691 2692 2692 def _lay_out(self): … … 2832 2832 graphs.append(item2.figure) 2833 2833 canvases.append(item2.canvas) 2834 except :2834 except Exception: 2835 2835 # Not for control panels 2836 logging.error( sys.exc_info()[1])2836 logging.error(traceback.format_exc()) 2837 2837 # Make sure the resduals plot goes to the last 2838 2838 if res_item != None: … … 3167 3167 if item[7].__class__.__name__ == 'ComboBox': 3168 3168 disfunc = str(item[7].GetValue()) 3169 except :3170 logging.error( sys.exc_info()[1])3169 except Exception: 3170 logging.error(traceback.format_exc()) 3171 3171 3172 3172 # 2D … … 3174 3174 try: 3175 3175 check = item[0].GetValue() 3176 except :3176 except Exception: 3177 3177 check = None 3178 3178 name = item[1] … … 3202 3202 for weight in self.weights[name]: 3203 3203 disfunc += ' ' + str(weight) 3204 except :3205 logging.error( sys.exc_info()[1])3204 except Exception: 3205 logging.error(traceback.format_exc()) 3206 3206 content += name + ',' + str(check) + ',' + value + disfunc + ':' 3207 3207 … … 3403 3403 weights=pd_weights) 3404 3404 is_array = True 3405 except :3406 logging.error( sys.exc_info()[1])3405 except Exception: 3406 logging.error(traceback.format_exc()) 3407 3407 if not is_array: 3408 3408 self._disp_obj_dict[name] = disp_model … … 3418 3418 self.state.weights] 3419 3419 3420 except :3421 logging.error( sys.exc_info()[1])3420 except Exception: 3421 logging.error(traceback.format_exc()) 3422 3422 print "Error in BasePage._paste_poly_help: %s" % \ 3423 3423 sys.exc_info()[1] -
src/sas/sasgui/perspectives/fitting/fitting.py
r934ce649 r7673ecd 273 273 wx.PostEvent(self.parent, evt) 274 274 break 275 except: 275 except Exception: 276 import traceback; traceback.print_exc() 276 277 msg = 'Delete Error: \nCould not delete the file; Check if in use.' 277 278 wx.MessageBox(msg, 'Error') … … 579 580 _, theory_state = item 580 581 self.fit_panel.set_model_state(theory_state) 581 except :582 except Exception: 582 583 msg = "Fitting: cannot deal with the theory received" 583 584 evt = StatusEvent(status=msg, info="error") -
src/sas/sasgui/perspectives/fitting/models.py
rcb4ef58 r7673ecd 2 2 Utilities to manage models 3 3 """ 4 import imp4 import traceback 5 5 import os 6 6 import sys … … 8 8 # Time is needed by the log method 9 9 import time 10 import datetime 10 11 import logging 11 12 import py_compile … … 20 21 21 22 PLUGIN_DIR = 'plugin_models' 23 PLUGIN_LOG = os.path.join(os.path.expanduser("~"), '.sasview', PLUGIN_DIR, 24 "plugins.log") 22 25 23 26 def get_model_python_path(): … … 28 31 29 32 30 def log(message):33 def plugin_log(message): 31 34 """ 32 35 Log a message in a file located in the user's home directory 33 36 """ 34 dir = os.path.join(os.path.expanduser("~"), '.sasview', PLUGIN_DIR) 35 out = open(os.path.join(dir, "plugins.log"), 'a') 36 out.write("%10g: %s\n" % (time.clock(), message)) 37 out = open(PLUGIN_LOG, 'a') 38 now = time.time() 39 stamp = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S') 40 out.write("%s: %s\n" % (stamp, message)) 37 41 out.close() 38 42 … … 51 55 if not issubclass(model, Model1DPlugin): 52 56 msg = "Plugin %s must be of type Model1DPlugin \n" % str(name) 53 log(msg)57 plugin_log(msg) 54 58 return None 55 59 if model.__name__ != "Model": 56 60 msg = "Plugin %s class name must be Model \n" % str(name) 57 log(msg)61 plugin_log(msg) 58 62 return None 59 63 try: … … 63 67 str(sys.exc_type), 64 68 sys.exc_info()[1]) 65 log(msg)69 plugin_log(msg) 66 70 return None 67 71 … … 72 76 msg = "Plugin %s: error writing function \n\t :%s %s\n " % \ 73 77 (str(name), str(sys.exc_type), sys.exc_info()[1]) 74 log(msg)78 plugin_log(msg) 75 79 return None 76 80 else: 77 81 msg = "Plugin %s needs a method called function \n" % str(name) 78 log(msg)82 plugin_log(msg) 79 83 return None 80 84 return model … … 132 136 """ 133 137 def __nonzero__(self): 134 type, value, t raceback= sys.exc_info()138 type, value, tb = sys.exc_info() 135 139 if type is not None and issubclass(type, py_compile.PyCompileError): 136 140 print "Problem with", repr(value) 137 raise type, value, t raceback141 raise type, value, tb 138 142 return 1 139 143 … … 155 159 156 160 def _findModels(dir): 161 """ 162 Find custom models 163 """ 157 164 # List of plugin objects 158 plugins = {}159 165 dir = find_plugins_dir() 160 166 # Go through files in plug-in directory 161 #always recompile the folder plugin162 167 if not os.path.isdir(dir): 163 msg = "SasView couldn't locate Model plugin folder." 164 msg += """ "%s" does not exist""" % dir 168 msg = "SasView couldn't locate Model plugin folder %r." % dir 165 169 logging.warning(msg) 166 return plugins 167 else: 168 log("looking for models in: %s" % str(dir)) 169 compile_file(dir) 170 logging.info("plugin model dir: %s" % str(dir)) 171 try: 172 list = os.listdir(dir) 173 for item in list: 174 toks = os.path.splitext(os.path.basename(item)) 175 if toks[1] == '.py' and not toks[0] == '__init__': 176 name = toks[0] 177 path = [os.path.abspath(dir)] 178 file = None 179 try: 180 (file, path, info) = imp.find_module(name, path) 181 module = imp.load_module(name, file, item, info) 182 if hasattr(module, "Model"): 183 try: 184 if _check_plugin(module.Model, name) != None: 185 plugins[name] = module.Model 186 except: 187 msg = "Error accessing Model" 188 msg += "in %s\n %s %s\n" % (name, 189 str(sys.exc_type), 190 sys.exc_info()[1]) 191 log(msg) 192 else: 193 filename = os.path.join(dir, item) 194 plugins[name] = load_custom_model(filename) 195 196 except: 197 msg = "Error accessing Model" 198 msg += " in %s\n %s %s \n" % (name, 199 str(sys.exc_type), 200 sys.exc_info()[1]) 201 log(msg) 202 finally: 203 204 if not file == None: 205 file.close() 206 except: 207 # Don't deal with bad plug-in imports. Just skip. 208 msg = "Could not import model plugin: %s" % sys.exc_info()[1] 209 log(msg) 170 return {} 171 172 plugin_log("looking for models in: %s" % str(dir)) 173 #compile_file(dir) #always recompile the folder plugin 174 logging.info("plugin model dir: %s" % str(dir)) 175 176 plugins = {} 177 for filename in os.listdir(dir): 178 name, ext = os.path.splitext(filename) 179 if ext == '.py' and not name == '__init__': 180 path = os.path.abspath(os.path.join(dir, filename)) 181 try: 182 model = load_custom_model(path) 183 except Exception: 184 msg = traceback.format_exc() 185 msg += "\nwhile accessing model in %r" % path 186 plugin_log(msg) 187 logging.warning("Failed to load plugin %r. See %s for details" 188 % (path, PLUGIN_LOG)) 189 plugins[model.name] = model 210 190 211 191 return plugins -
src/sas/sasgui/perspectives/fitting/pagestate.py
rc10d9d6c r7673ecd 19 19 import logging 20 20 import numpy 21 import string21 import traceback 22 22 23 23 import xml.dom.minidom … … 473 473 try: 474 474 value = content[1] 475 except :476 logging.error( sys.exc_value)475 except Exception: 476 logging.error(traceback.format_exc()) 477 477 if name.count("State created"): 478 478 repo_time = "" + value … … 515 515 title = content[2] + " [" + repo_time + "]" 516 516 title_name = HEADER % title 517 except :518 logging.error( sys.exc_value)517 except Exception: 518 logging.error(traceback.format_exc()) 519 519 if name == "model name ": 520 520 try: … … 530 530 q_name = ("Q Range: " + q_range) 531 531 q_range = CENTRE % q_name 532 except :533 logging.error( sys.exc_value)532 except Exception: 533 logging.error(traceback.format_exc()) 534 534 paramval = "" 535 535 for lines in param_string.split(":"): … … 864 864 attribute = getattr(self, item[1]) 865 865 attribute[name] = com_name 866 except :867 logging.error( sys.exc_value)866 except Exception: 867 logging.error(traceback.format_exc()) 868 868 869 869 # get self.values and self.weights dic. if exists … … 880 880 val = float(line) 881 881 value_list.append(val) 882 except :882 except Exception: 883 883 # pass if line is empty (it happens) 884 logging.error( sys.exc_value)884 logging.error(traceback.format_exc()) 885 885 dic[name] = numpy.array(value_list) 886 886 setattr(self, item[1], dic) … … 1293 1293 if len(note_value) > 0: 1294 1294 data_info.notes.append(note_value) 1295 except :1295 except Exception: 1296 1296 err_mess = "cansas_reader.read: error processing entry notes\n %s" % sys.exc_value 1297 1297 self.errors.append(err_mess) … … 1315 1315 if len(detail_value) > 0: 1316 1316 data_info.sample.details.append(detail_value) 1317 except :1317 except Exception: 1318 1318 err_mess = "cansas_reader.read: error processing sample details\n %s" % sys.exc_value 1319 1319 self.errors.append(err_mess)
Note: See TracChangeset
for help on using the changeset viewer.