Changeset ba8d326 in sasview for src/sas/sascalc/fit/pagestate.py


Ignore:
Timestamp:
Jun 23, 2017 4:16:29 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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:
98e3f24
Parents:
81b35396
git-author:
Paul Kienzle <pkienzle@…> (06/21/17 12:38:48)
git-committer:
Paul Kienzle <pkienzle@…> (06/23/17 16:16:29)
Message:

code cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/fit/pagestate.py

    r81b35396 rba8d326  
    11""" 
    2     Class that holds a fit page state 
     2Class that holds a fit page state 
    33""" 
    44# TODO: Refactor code so we don't need to use getattr/setattr 
     
    4040# Information to read/write state as xml 
    4141FITTING_NODE_NAME = 'fitting_plug_in' 
    42 CANSAS_NS = "cansas1d/1.0" 
     42CANSAS_NS = {"ns": "cansas1d/1.0"} 
    4343 
    4444CUSTOM_MODEL = 'Plugin Models' 
     
    8383                            ["weights", "weights"]] 
    8484 
    85 DISPERSION_LIST = [["disp_obj_dict", "_disp_obj_dict", "string"]] 
     85DISPERSION_LIST = [["disp_obj_dict", "disp_obj_dict", "string"]] 
    8686 
    8787LIST_OF_STATE_PARAMETERS = [["parameters", "parameters"], 
     
    154154        self.timestamp = time.time() 
    155155        # Data member to store the dispersion object created 
    156         self._disp_obj_dict = {} 
     156        self.disp_obj_dict = {} 
    157157        # ------------------------ 
    158158        # Data used for fitting 
     
    271271        # store value of chisqr 
    272272        self.tcChi = None 
    273         self.version = (1,0,0) 
     273        self.version = (1, 0, 0) 
    274274 
    275275    def clone(self): 
     
    315315        obj.tcChi = self.tcChi 
    316316 
    317         if len(self._disp_obj_dict) > 0: 
    318             for k, v in self._disp_obj_dict.iteritems(): 
    319                 obj._disp_obj_dict[k] = v 
     317        if len(self.disp_obj_dict) > 0: 
     318            for k, v in self.disp_obj_dict.iteritems(): 
     319                obj.disp_obj_dict[k] = v 
    320320        if len(self.disp_cb_dict) > 0: 
    321321            for k, v in self.disp_cb_dict.iteritems(): 
     
    847847            inputs.appendChild(element) 
    848848 
    849         # Create doc for the dictionary of self._disp_obj_dic 
     849        # Create doc for the dictionary of self.disp_obj_dic 
    850850        for tagname, varname, tagtype in DISPERSION_LIST: 
    851851            element = newdoc.createElement(tagname) 
     
    928928        """ 
    929929        for item in node: 
    930             try: 
    931                 name = item.get('name') 
    932             except: 
    933                 name = None 
    934             try: 
    935                 value = item.get('value') 
    936             except: 
    937                 value = None 
    938             try: 
    939                 selected_to_fit = (item.get('selected_to_fit') == "True") 
    940             except: 
    941                 selected_to_fit = None 
    942             try: 
    943                 error_displayed = (item.get('error_displayed') == "True") 
    944             except: 
    945                 error_displayed = None 
    946             try: 
    947                 error_value = item.get('error_value') 
    948             except: 
    949                 error_value = None 
    950             try: 
    951                 minimum_displayed = (item.get('minimum_displayed') == "True") 
    952             except: 
    953                 minimum_displayed = None 
    954             try: 
    955                 minimum_value = item.get('minimum_value') 
    956             except: 
    957                 minimum_value = None 
    958             try: 
    959                 maximum_displayed = (item.get('maximum_displayed') == "True") 
    960             except: 
    961                 maximum_displayed = None 
    962             try: 
    963                 maximum_value = item.get('maximum_value') 
    964             except: 
    965                 maximum_value = None 
    966             try: 
    967                 unit = item.get('unit') 
    968             except: 
    969                 unit = None 
     930            name = item.get('name') 
     931            value = item.get('value') 
     932            selected_to_fit = (item.get('selected_to_fit') == "True") 
     933            error_displayed = (item.get('error_displayed') == "True") 
     934            error_value = item.get('error_value') 
     935            minimum_displayed = (item.get('minimum_displayed') == "True") 
     936            minimum_value = item.get('minimum_value') 
     937            maximum_displayed = (item.get('maximum_displayed') == "True") 
     938            maximum_value = item.get('maximum_value') 
     939            unit = item.get('unit') 
    970940            list.append([selected_to_fit, name, value, "+/-", 
    971941                         [error_displayed, error_value], 
     
    10331003                                          list=getattr(self, item[1])) 
    10341004 
    1035                 # Recover _disp_obj_dict from xml file 
    1036                 self._disp_obj_dict = {} 
     1005                # Recover disp_obj_dict from xml file 
     1006                self.disp_obj_dict = {} 
    10371007                for tagname, varname, tagtype in DISPERSION_LIST: 
    10381008                    node = get_content("ns:%s" % tagname, entry) 
     
    10481018                            except Exception: 
    10491019                                base = "unable to load distribution %r for %s" 
    1050                                 logger.error(base % (value, parameter)) 
     1020                                logger.error(base, value, parameter) 
    10511021                                continue 
    1052                         _disp_obj_dict = getattr(self, varname) 
    1053                         _disp_obj_dict[parameter] = value 
     1022                        disp_obj_dict = getattr(self, varname) 
     1023                        disp_obj_dict[parameter] = value 
    10541024 
    10551025                # get self.values and self.weights dic. if exists 
     
    10741044                    setattr(self, varname, dic) 
    10751045 
    1076 class SimFitPageState: 
     1046class SimFitPageState(object): 
    10771047    """ 
    10781048    State of the simultaneous fit page for saving purposes 
     
    10951065        self.constraints_list = [] 
    10961066 
     1067    def __repr__(self): 
     1068        # TODO: should use __str__, not __repr__ (similarly for PageState) 
     1069        # TODO: could use a nicer representation 
     1070        repr = """\ 
     1071fit page number : %(fit_page_no)s 
     1072select all : %(select_all)s 
     1073model_list : %(model_list)s 
     1074model to fit : %(model_to_fit)s 
     1075number of construsts : %(no_constraint)s 
     1076constraint dict : %(constraint_dict)s 
     1077constraints list : %(constraints_list)s 
     1078"""%self.__dict__ 
     1079        return repr 
     1080 
    10971081class Reader(CansasReader): 
    10981082    """ 
     
    11521136        try: 
    11531137            nodes = entry.xpath('ns:%s' % FITTING_NODE_NAME, 
    1154                                 namespaces={'ns': CANSAS_NS}) 
     1138                                namespaces=CANSAS_NS) 
    11551139            if nodes: 
    11561140                # Create an empty state 
     
    11581142                state.from_xml(node=nodes[0]) 
    11591143 
    1160         except: 
     1144        except Exception: 
    11611145            logger.info("XML document does not contain fitting information.\n" 
    1162                          + traceback.format_exc()) 
     1146                        + traceback.format_exc()) 
    11631147 
    11641148        return state 
     
    11711155        """ 
    11721156        nodes = entry.xpath('ns:%s' % FITTING_NODE_NAME, 
    1173                             namespaces={'ns': CANSAS_NS}) 
     1157                            namespaces=CANSAS_NS) 
    11741158        if nodes: 
    11751159            simfitstate = nodes[0].xpath('ns:simultaneous_fit', 
    1176                                          namespaces={'ns': CANSAS_NS}) 
     1160                                         namespaces=CANSAS_NS) 
    11771161            if simfitstate: 
    11781162                sim_fit_state = SimFitPageState() 
    11791163                simfitstate_0 = simfitstate[0] 
    11801164                all = simfitstate_0.xpath('ns:select_all', 
    1181                                           namespaces={'ns': CANSAS_NS}) 
     1165                                          namespaces=CANSAS_NS) 
    11821166                atts = all[0].attrib 
    11831167                checked = atts.get('checked') 
    11841168                sim_fit_state.select_all = bool(checked) 
    11851169                model_list = simfitstate_0.xpath('ns:model_list', 
    1186                                                  namespaces={'ns': CANSAS_NS}) 
     1170                                                 namespaces=CANSAS_NS) 
    11871171                model_list_items = model_list[0].xpath('ns:model_list_item', 
    1188                                                        namespaces={'ns': 
    1189                                                                     CANSAS_NS}) 
     1172                                                       namespaces=CANSAS_NS) 
    11901173                for model in model_list_items: 
    11911174                    attrs = model.attrib 
     
    11931176 
    11941177                constraints = simfitstate_0.xpath('ns:constraints', 
    1195                                                 namespaces={'ns': CANSAS_NS}) 
     1178                                                  namespaces=CANSAS_NS) 
    11961179                constraint_list = constraints[0].xpath('ns:constraint', 
    1197                                                namespaces={'ns': CANSAS_NS}) 
     1180                                                       namespaces=CANSAS_NS) 
    11981181                for constraint in constraint_list: 
    11991182                    attrs = constraint.attrib 
     
    12131196 
    12141197        """ 
    1215         node = dom.xpath('ns:data_class', namespaces={'ns': CANSAS_NS}) 
     1198        node = dom.xpath('ns:data_class', namespaces=CANSAS_NS) 
    12161199        return_value, _ = self._parse_entry(dom) 
    12171200        return return_value, _ 
     
    12421225                    root = tree.getroot() 
    12431226                    entry_list = root.xpath('ns:SASentry', 
    1244                                             namespaces={'ns': CANSAS_NS}) 
     1227                                            namespaces=CANSAS_NS) 
    12451228                    for entry in entry_list: 
    12461229                        try: 
     
    12661249                return None 
    12671250            else: 
    1268                 for ind in range(len(output)): 
     1251                for data in output: 
    12691252                    # Call back to post the new state 
    1270                     state = output[ind].meta_data['fitstate'] 
     1253                    state = data.meta_data['fitstate'] 
    12711254                    t = time.localtime(state.timestamp) 
    12721255                    time_str = time.strftime("%b %d %H:%M", t) 
     
    12791262 
    12801263                    if state is not None and state.is_data is not None: 
    1281                         output[ind].is_data = state.is_data 
    1282  
    1283                     output[ind].filename = state.file 
    1284                     state.data = output[ind] 
    1285                     state.data.name = output[ind].filename  # state.data_name 
     1264                        data.is_data = state.is_data 
     1265 
     1266                    data.filename = state.file 
     1267                    state.data = data 
     1268                    state.data.name = data.filename  # state.data_name 
    12861269                    state.data.id = state.data_id 
    12871270                    if state.is_data is not None: 
    12881271                        state.data.is_data = state.is_data 
    1289                     if output[ind].run_name is not None\ 
    1290                          and len(output[ind].run_name) != 0: 
    1291                         if isinstance(output[ind].run_name, dict): 
    1292                             name = output[ind].run_name.keys()[0] 
     1272                    if data.run_name is not None and len(data.run_name) != 0: 
     1273                        if isinstance(data.run_name, dict): 
     1274                            # Note: key order in dict is not guaranteed, so sort 
     1275                            name = data.run_name.keys()[0] 
    12931276                        else: 
    1294                             name = output[ind].run_name 
     1277                            name = data.run_name 
    12951278                    else: 
    12961279                        name = original_fname 
     
    12981281                    state.version = fitstate.version 
    12991282                    # store state in fitting 
    1300                     self.call_back(state=state, 
    1301                                    datainfo=output[ind], format=ext) 
     1283                    self.call_back(state=state, datainfo=data, format=ext) 
    13021284                    self.state = state 
    13031285                simfitstate = self._parse_simfit_state(entry) 
Note: See TracChangeset for help on using the changeset viewer.