Changeset 277257f in sasview for src/sas/sasgui/perspectives/fitting
- Timestamp:
- Jul 5, 2017 5:28:55 PM (7 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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 1386b2f
- Parents:
- 251ef684
- Location:
- src/sas/sasgui/perspectives/fitting
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/fitting/basepage.py
r65f3930 r277257f 504 504 self.state.manager = manager 505 505 506 def populate_box(self, model_ dict):506 def populate_box(self, model_list_box): 507 507 """ 508 508 Store list of model 509 509 510 :param model_dict: dictionary containing list of models 511 512 """ 513 self.model_list_box = model_dict 514 self.state.model_list_box = self.model_list_box 510 :param model_list_box: dictionary containing categorized models 511 """ 512 self.model_list_box = model_list_box 515 513 self.initialize_combox() 516 514 517 def set_model_dictionary(self, model_dict ):515 def set_model_dictionary(self, model_dictionary): 518 516 """ 519 517 Store a dictionary linking model name -> model object 520 518 521 :param model_dict : dictionary containing list ofmodels522 """ 523 self.model_dict = model_dict519 :param model_dictionary: dictionary containing all models 520 """ 521 self.model_dictionary = model_dictionary 524 522 525 523 def initialize_combox(self): … … 527 525 put default value in the combo box 528 526 """ 529 if self.model_list_box is not None and len(self.model_list_box) > 0:527 if self.model_list_box: 530 528 self._populate_box(self.structurebox, 531 529 self.model_list_box["Structure Factors"]) … … 1218 1216 self.categorybox.Select(category_pos) 1219 1217 self._show_combox(None) 1220 if self.categorybox.GetValue() == CUSTOM_MODEL \1221 and PLUGIN_NAME_BASE not in state.formfactorcombobox :1218 if (self.categorybox.GetValue() == CUSTOM_MODEL 1219 and PLUGIN_NAME_BASE not in state.formfactorcombobox): 1222 1220 state.formfactorcombobox = \ 1223 1221 PLUGIN_NAME_BASE + state.formfactorcombobox 1224 1222 formfactor_pos = 0 1225 1223 for ind_form in range(self.formfactorbox.GetCount()): 1226 if self.formfactorbox.GetString(ind_form) == \1227 (state.formfactorcombobox):1224 if (self.formfactorbox.GetString(ind_form) 1225 == state.formfactorcombobox): 1228 1226 formfactor_pos = int(ind_form) 1229 1227 break … … 1235 1233 state.structurecombobox = unicode(state.structurecombobox) 1236 1234 for ind_struct in range(self.structurebox.GetCount()): 1237 if self.structurebox.GetString(ind_struct) == \1238 (state.structurecombobox):1235 if (self.structurebox.GetString(ind_struct) 1236 == state.structurecombobox): 1239 1237 structfactor_pos = int(ind_struct) 1240 1238 break … … 1840 1838 if mod_cat == CUSTOM_MODEL: 1841 1839 for model in self.model_list_box[mod_cat]: 1842 m_list.append(self.model_dict [model.name])1840 m_list.append(self.model_dictionary[model.name]) 1843 1841 else: 1844 1842 cat_dic = self.master_category_dict[mod_cat] 1845 for (model, enabled)in cat_dic:1843 for model, enabled in cat_dic: 1846 1844 if enabled: 1847 m_list.append(self.model_dict [model])1845 m_list.append(self.model_dictionary[model]) 1848 1846 except Exception: 1849 1847 msg = traceback.format_exc() … … 3497 3495 self.model_box.Clear() 3498 3496 3499 if category == 'Plugin Models':3497 if category == CUSTOM_MODEL: 3500 3498 for model in self.model_list_box[category]: 3501 3499 str_m = str(model).split(".")[0] -
src/sas/sasgui/perspectives/fitting/fitpage.py
r50fcb09 r277257f 1143 1143 self.model.name = "M" + str(self.index_model) 1144 1144 1145 def _on_select_model(self, event=None ):1145 def _on_select_model(self, event=None, keep_pars=False): 1146 1146 """ 1147 1147 call back for model selection 1148 1148 """ 1149 1149 self.Show(False) 1150 copy_flag = False1151 is_poly_enabled = None1152 1150 if event is not None: 1153 if (event.GetEventObject() == self.formfactorbox 1154 and self.structurebox.GetLabel() != 'None')\ 1155 or event.GetEventObject() == self.structurebox\ 1156 or event.GetEventObject() == self.multifactorbox: 1157 copy_flag = self.get_copy_params() 1158 is_poly_enabled = self.enable_disp.GetValue() 1151 control = event.GetEventObject() 1152 if ((control == self.formfactorbox 1153 and self.structurebox.GetLabel() != 'None') 1154 or control == self.structurebox 1155 or control == self.multifactorbox): 1156 keep_pars = True 1157 1158 if keep_pars: 1159 saved_pars = self.get_copy_params() 1160 is_poly_enabled = self.enable_disp.GetValue() 1161 else: 1162 saved_pars = None 1163 is_poly_enabled = None 1159 1164 1160 1165 self._on_select_model_helper() … … 1171 1176 try: 1172 1177 self.set_dispers_sizer() 1173 except :1178 except Exception: 1174 1179 pass 1175 1180 self.state.enable_disp = self.enable_disp.GetValue() … … 1232 1237 self.state.model.name = self.model.name 1233 1238 1239 # when select a model only from guictr/button 1240 if is_poly_enabled is not None: 1241 self.enable_disp.SetValue(is_poly_enabled) 1242 self.disable_disp.SetValue(not is_poly_enabled) 1243 self._set_dipers_Param(event=None) 1244 self.state.enable_disp = self.enable_disp.GetValue() 1245 self.state.disable_disp = self.disable_disp.GetValue() 1246 1247 # Keep the previous param values 1248 if saved_pars: 1249 self.get_paste_params(saved_pars) 1250 1234 1251 if event is not None: 1252 # update list of plugins if new plugin is available 1253 # mod_cat = self.categorybox.GetStringSelection() 1254 # if mod_cat == CUSTOM_MODEL: 1255 # temp = self.parent.update_model_list() 1256 # if temp: 1257 # self.model_list_box = temp 1258 # current_val = self.formfactorbox.GetLabel() 1259 # pos = self.formfactorbox.GetSelection() 1260 # self._show_combox_helper() 1261 # self.formfactorbox.SetStringSelection(current_val) 1262 # self.formfactorbox.SetValue(current_val) 1235 1263 # post state to fit panel 1236 1264 new_event = PageInfoEvent(page=self) 1237 1265 wx.PostEvent(self.parent, new_event) 1238 # update list of plugins if new plugin is available1239 custom_model = CUSTOM_MODEL1240 mod_cat = self.categorybox.GetStringSelection()1241 if mod_cat == custom_model:1242 temp = self.parent.update_model_list()1243 if temp:1244 self.model_list_box = temp1245 current_val = self.formfactorbox.GetLabel()1246 pos = self.formfactorbox.GetSelection()1247 self._show_combox_helper()1248 self.formfactorbox.SetSelection(pos)1249 self.formfactorbox.SetValue(current_val)1250 # when select a model only from guictr/button1251 if is_poly_enabled is not None:1252 self.enable_disp.SetValue(is_poly_enabled)1253 self.disable_disp.SetValue(not is_poly_enabled)1254 self._set_dipers_Param(event=None)1255 self.state.enable_disp = self.enable_disp.GetValue()1256 self.state.disable_disp = self.disable_disp.GetValue()1257 1258 # Keep the previous param values1259 if copy_flag:1260 self.get_paste_params(copy_flag)1261 1266 wx.CallAfter(self._onDraw, None) 1262 1267 … … 1709 1714 and not self.temp_multi_functional: 1710 1715 return None 1716 print("_set_fun_box_list", self.model.name) 1711 1717 # Get the func name list 1712 1718 list = self.model.fun_list … … 1716 1722 ind = 0 1717 1723 while(ind < len(list)): 1718 for key, val in list.ite ritems():1719 if (val == ind):1724 for key, val in list.items(): 1725 if val == ind: 1720 1726 fun_box.Append(key, val) 1721 1727 break -
src/sas/sasgui/perspectives/fitting/fitpanel.py
r65f3930 r277257f 114 114 """ 115 115 temp = self.menu_mng.update() 116 if len(temp):116 if temp: 117 117 self.model_list_box = temp 118 118 return temp … … 121 121 """ 122 122 """ 123 temp = self.menu_mng.plugins_reset() 124 if len(temp): 125 self.model_list_box = temp 126 return temp 123 self.model_list_box = self.menu_mng.plugins_reset() 124 return self.model_list_box 127 125 128 126 def get_page_by_id(self, uid): … … 298 296 self.model_list_box = dict 299 297 300 def set_model_dict (self, m_dict):298 def set_model_dictionary(self, model_dictionary): 301 299 """ 302 300 copy a dictionary of model name -> model object 303 301 304 :param m _dict: dictionary linking model name -> model object302 :param model_dictionary: dictionary linking model name -> model object 305 303 """ 306 304 … … 356 354 panel._set_save_flag(not panel.batch_on) 357 355 panel.set_model_dictionary(self.model_dictionary) 358 panel.populate_box(model_ dict=self.model_list_box)356 panel.populate_box(model_list_box=self.model_list_box) 359 357 panel.formfactor_combo_init() 360 358 panel.set_manager(self._manager) -
src/sas/sasgui/perspectives/fitting/fitting.py
r00f7ff1 r277257f 34 34 from sas.sascalc.fit.BumpsFitting import BumpsFit as Fit 35 35 from sas.sascalc.fit.pagestate import Reader, PageState, SimFitPageState 36 from sas.sascalc.fit import models 36 37 37 38 from sas.sasgui.guiframe.dataFitting import Data2D … … 60 61 from .resultpanel import ResultPanel, PlotResultEvent 61 62 from .gpu_options import GpuOptions 62 from . import models63 63 64 64 logger = logging.getLogger(__name__) … … 297 297 event_id = event.GetId() 298 298 model_manager = models.ModelManager() 299 model_list = model_manager. get_model_name_list()299 model_list = model_manager.composable_models() 300 300 plug_dir = models.find_plugins_dir() 301 301 textdial = TextDialog(None, self, wx.ID_ANY, 'Easy Sum/Multi(p1, p2) Editor', … … 337 337 self.set_edit_menu_helper(self.parent, self.edit_custom_model) 338 338 self.set_edit_menu_helper(self.parent, self.delete_custom_model) 339 temp = self.fit_panel.reset_pmodel_list() 340 if temp: 341 # Set the new plugin model list for all fit pages 342 for uid, page in self.fit_panel.opened_pages.iteritems(): 343 if hasattr(page, "formfactorbox"): 344 page.model_list_box = temp 345 current_val = page.formfactorbox.GetLabel() 346 #if page.plugin_rbutton.GetValue(): 347 mod_cat = page.categorybox.GetStringSelection() 348 if mod_cat == custom_model: 349 #pos = page.formfactorbox.GetSelection() 350 page._show_combox_helper() 351 new_val = page.formfactorbox.GetLabel() 352 if current_val != new_val and new_val != '': 353 page.formfactorbox.SetLabel(new_val) 354 else: 355 page.formfactorbox.SetLabel(current_val) 356 except: 339 new_pmodel_list = self.fit_panel.reset_pmodel_list() 340 if not new_pmodel_list: 341 return 342 # Set the new plugin model list for all fit pages 343 for uid, page in self.fit_panel.opened_pages.iteritems(): 344 if hasattr(page, "formfactorbox"): 345 page.model_list_box = new_pmodel_list 346 mod_cat = page.categorybox.GetStringSelection() 347 if mod_cat == custom_model: 348 box = page.formfactorbox 349 model_name = box.GetValue() 350 model = (box.GetClientData(box.GetCurrentSelection()) 351 if model_name else None) 352 page._show_combox_helper() 353 new_index = box.FindString(model_name) 354 new_model = (box.GetClientData(new_index) 355 if new_index >= 0 else None) 356 if new_index >= 0: 357 box.SetStringSelection(model_name) 358 else: 359 box.SetStringSelection('') 360 if model and new_model != model: 361 page._on_select_model(keep_pars=True) 362 except Exception: 357 363 logger.error("update_custom_combo: %s", sys.exc_value) 358 364 … … 364 370 #new_model_menu = wx.Menu() 365 371 self.edit_model_menu.Append(wx_id, 'New Plugin Model', 366 'Add a new model function')372 'Add a new model function') 367 373 wx.EVT_MENU(owner, wx_id, self.make_new_model) 368 374 … … 646 652 self.parent.add_data(data_list={data.id: data}) 647 653 wx.PostEvent(self.parent, NewPlotEvent(plot=data, 648 654 title=data.title)) 649 655 #need to be fix later make sure we are sendind guiframe.data 650 656 #to panel … … 657 663 self.parent.add_data(data_list={data.id: data}) 658 664 wx.PostEvent(self.parent, NewPlotEvent(plot=data, 659 665 title=data.title)) 660 666 page = self.add_fit_page([data]) 661 667 caption = page.window_caption 662 668 self.store_data(uid=page.uid, data_list=page.get_data_list(), 663 caption=caption)669 caption=caption) 664 670 self.mypanels.append(page) 665 671 … … 778 784 """ 779 785 if item.find(".") >= 0: 780 param_names = re.split( "\.", item)786 param_names = re.split(r"\.", item) 781 787 model_name = param_names[0] 782 788 ##Assume max len is 3; eg., M0.radius.width … … 881 887 882 888 self.draw_model(model=model, data=data, page_id=uid, smearer=smear, 883 enable1D=enable1D, enable2D=enable2D,884 qmin=qmin, qmax=qmax, weight=weight)889 enable1D=enable1D, enable2D=enable2D, 890 qmin=qmin, qmax=qmax, weight=weight) 885 891 886 892 def draw_model(self, model, page_id, data=None, smearer=None, … … 925 931 ## draw model 2D with no initial data 926 932 self._draw_model2D(model=model, 927 928 929 930 931 932 933 934 935 936 937 938 933 page_id=page_id, 934 data=data, 935 enable2D=enable2D, 936 smearer=smearer, 937 qmin=qmin, 938 qmax=qmax, 939 fid=fid, 940 weight=weight, 941 state=state, 942 toggle_mode_on=toggle_mode_on, 943 update_chisqr=update_chisqr, 944 source=source) 939 945 940 946 def onFit(self, uid): … … 945 951 :param uid: id related to the panel currently calling this fit function. 946 952 """ 947 if uid is None: raise RuntimeError("no page to fit") # Should never happen 953 if uid is None: 954 raise RuntimeError("no page to fit") # Should never happen 948 955 949 956 sim_page_uid = getattr(self.sim_page, 'uid', None) … … 979 986 page = self.fit_panel.get_page_by_id(page_id) 980 987 self.set_fit_weight(uid=page.uid, 981 flag=page.get_weight_flag(),982 is2d=page._is_2D())988 flag=page.get_weight_flag(), 989 is2d=page._is_2D()) 983 990 if not page.param_toFit: 984 991 msg = "No fitting parameters for %s" % page.window_caption … … 1004 1011 fitter = sim_fitter 1005 1012 self._add_problem_to_fit(fitproblem=fitproblem, 1006 pars=pars,1007 fitter=fitter,1008 fit_id=fit_id)1013 pars=pars, 1014 fitter=fitter, 1015 fit_id=fit_id) 1009 1016 fit_id += 1 1010 1017 list_page_id.append(page_id) … … 1053 1060 ## Perform more than 1 fit at the time 1054 1061 calc_fit = FitThread(handler=handler, 1055 1056 1057 1058 1059 1060 1062 fn=fitter_list, 1063 batch_inputs=batch_inputs, 1064 batch_outputs=batch_outputs, 1065 page_id=list_page_id, 1066 updatefn=handler.update_fit, 1067 completefn=self._fit_completed) 1061 1068 #self.fit_thread_list[current_page_id] = calc_fit 1062 1069 self.fit_thread_list[uid] = calc_fit … … 1119 1126 evt = StatusEvent(status=msg, info="warning") 1120 1127 wx.PostEvent(self.parent, evt) 1121 except :1128 except Exception: 1122 1129 msg = "Creating Fit page: %s" % sys.exc_value 1123 1130 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) … … 1143 1150 group_id = str(page.uid) + " Model1D" 1144 1151 wx.PostEvent(self.parent, 1145 NewPlotEvent(group_id=group_id,1146 1152 NewPlotEvent(group_id=group_id, 1153 action="delete")) 1147 1154 self.parent.update_data(prev_data=theory_data, 1148 1155 new_data=data) 1149 1156 else: 1150 1157 if theory_data is not None: … … 1152 1159 data.group_id = theory_data.group_id 1153 1160 wx.PostEvent(self.parent, 1154 NewPlotEvent(group_id=group_id,1155 1161 NewPlotEvent(group_id=group_id, 1162 action="delete")) 1156 1163 self.parent.update_data(prev_data=theory_data, 1157 1164 new_data=data) 1158 1165 self.store_data(uid=page.uid, data_list=page.get_data_list(), 1159 1166 caption=page.window_caption) … … 1557 1564 except KeyboardInterrupt: 1558 1565 fit_msg += "\nSingular point: Fitting stopped." 1559 except :1566 except Exception: 1560 1567 fit_msg += "\nSingular point: Fitting error occurred." 1561 1568 if fit_msg: 1562 evt = StatusEvent(status=fit_msg, info="warning", type="stop")1563 wx.PostEvent(self.parent, evt)1564 1565 except :1569 evt = StatusEvent(status=fit_msg, info="warning", type="stop") 1570 wx.PostEvent(self.parent, evt) 1571 1572 except Exception: 1566 1573 msg = ("Fit completed but the following error occurred: %s" 1567 1574 % sys.exc_value) … … 1721 1728 fid=data.id) 1722 1729 self.parent.update_theory(data_id=data.id, theory=new_plot, 1723 1730 state=state) 1724 1731 return new_plot 1725 1732 … … 1775 1782 1776 1783 self.page_finder[page_id].set_theory_data(data=new_plot, 1777 1784 fid=data.id) 1778 1785 if toggle_mode_on: 1779 1786 wx.PostEvent(self.parent, 1780 1787 NewPlotEvent(group_id=str(page_id) + " Model2D", 1781 1788 action="Hide")) 1782 1789 else: 1783 1790 if update_chisqr: 1784 wx.PostEvent(current_pg, 1785 Chi2UpdateEvent(output=self._cal_chisqr( 1786 data=data, 1787 fid=fid, 1788 weight=weight, 1789 page_id=page_id, 1790 index=index))) 1791 output = self._cal_chisqr(data=data, 1792 fid=fid, 1793 weight=weight, 1794 page_id=page_id, 1795 index=index) 1796 wx.PostEvent(current_pg, Chi2UpdateEvent(output=output)) 1791 1797 else: 1792 1798 self._plot_residuals(page_id=page_id, data=data, fid=fid, … … 1796 1802 logger.error("Using the present parameters the model does not return any finite value. ") 1797 1803 msg = "Computing Error: Model did not return any finite value." 1798 wx.PostEvent(self.parent, StatusEvent(status =msg, info="error"))1804 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) 1799 1805 else: 1800 1806 msg = "Computation completed!" … … 1821 1827 1822 1828 def _complete2D(self, image, data, model, page_id, elapsed, index, qmin, 1823 qmax, fid=None, weight=None, toggle_mode_on=False, state=None,1824 1829 qmax, fid=None, weight=None, toggle_mode_on=False, state=None, 1830 update_chisqr=True, source='model', plot_result=True): 1825 1831 """ 1826 1832 Complete get the result of modelthread and create model 2D … … 1863 1869 fid=data.id) 1864 1870 self.parent.update_theory(data_id=data.id, 1865 1866 1871 theory=new_plot, 1872 state=state) 1867 1873 current_pg = self.fit_panel.get_page_by_id(page_id) 1868 1874 title = new_plot.title 1869 1875 if not source == 'fit' and plot_result: 1870 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 1871 title=title)) 1876 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) 1872 1877 if toggle_mode_on: 1873 1878 wx.PostEvent(self.parent, 1874 1875 1879 NewPlotEvent(group_id=str(page_id) + " Model1D", 1880 action="Hide")) 1876 1881 else: 1877 1882 # Chisqr in fitpage 1878 1883 if update_chisqr: 1879 wx.PostEvent(current_pg,1880 Chi2UpdateEvent(output=self._cal_chisqr(data=data,1881 weight=weight,1882 fid=fid,1883 page_id=page_id,1884 index=index)))1884 output = self._cal_chisqr(data=data, 1885 weight=weight, 1886 fid=fid, 1887 page_id=page_id, 1888 index=index) 1889 wx.PostEvent(current_pg, Chi2UpdateEvent(output=output)) 1885 1890 else: 1886 1891 self._plot_residuals(page_id=page_id, data=data, fid=fid, 1887 1892 index=index, weight=weight) 1888 1893 1889 1894 if not number_finite: 1890 1895 logger.error("Using the present parameters the model does not return any finite value. ") 1891 1896 msg = "Computing Error: Model did not return any finite value." 1892 wx.PostEvent(self.parent, StatusEvent(status =msg, info="error"))1897 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) 1893 1898 else: 1894 1899 msg = "Computation completed!" … … 1906 1911 weight=None, 1907 1912 toggle_mode_on=False, 1908 1913 update_chisqr=True, source='model'): 1909 1914 """ 1910 1915 draw model in 2D … … 1952 1957 def _draw_model1D(self, model, page_id, data, 1953 1958 qmin, qmax, smearer=None, 1954 state=None, 1955 weight=None, 1956 fid=None, 1957 toggle_mode_on=False, update_chisqr=True, source='model', 1958 enable1D=True): 1959 state=None, weight=None, fid=None, 1960 toggle_mode_on=False, update_chisqr=True, source='model', 1961 enable1D=True): 1959 1962 """ 1960 1963 Draw model 1D from loaded data1D
Note: See TracChangeset
for help on using the changeset viewer.