Changeset b76e65a in sasview


Ignore:
Timestamp:
Sep 4, 2017 7:28:41 AM (7 years ago)
Author:
lewis
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:
5c1c486
Parents:
6f9abd3
Message:

Refactor 'Copy to Excel/LaTeX' logic

The error on fitted polydispersity parameters will now be exported
correctly, and the code that writes the values to the clipboard is
a lot easier to understand.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/fitting/basepage.py

    r1c699de rb76e65a  
    29292929            return False 
    29302930 
     2931 
     2932    def _get_copy_params_details(self): 
     2933        """ 
     2934        Combines polydisperse parameters with self.parameters so that they can 
     2935        be written to the clipboard (for Excel or LaTeX). Also returns a list of 
     2936        the names of parameters that have been fitted 
     2937 
     2938        :returns: all_params - A list of all parameters, in the format of  
     2939        self.parameters 
     2940        :returns: fitted_par_names - A list of the names of parameters that have 
     2941        been fitted 
     2942        """ 
     2943        # Names of params that are being fitted 
     2944        fitted_par_names = [param[1] for param in self.param_toFit] 
     2945        # Names of params with associated polydispersity 
     2946        disp_params = [param[1].split('.')[0] for param in self.fittable_param] 
     2947 
     2948        # Create array of all parameters 
     2949        all_params = copy.copy(self.parameters) 
     2950        for param in self.parameters: 
     2951            if param[1] in disp_params: 
     2952                # Polydisperse params aren't in self.parameters, so need adding 
     2953                # to all_params 
     2954                name = param[1] + ".width" 
     2955                index = all_params.index(param) + 1 
     2956                to_insert = [] 
     2957                if name in fitted_par_names: 
     2958                    # Param is fitted, so already has a param list in self.param_toFit 
     2959                    to_insert = self.param_toFit[fitted_par_names.index(name)] 
     2960                else: 
     2961                    # Param isn't fitted, so mockup a param list 
     2962                    to_insert = [None, name, self.model.getParam(name), None, None] 
     2963                all_params.insert(index, to_insert) 
     2964        return all_params, fitted_par_names 
     2965 
    29312966    def get_copy_excel(self): 
    29322967        """ 
     
    29422977        Get the string copies of the param names and values in the tap 
    29432978        """ 
     2979        if not self.parameters: 
     2980            # Do nothing if parameters doesn't exist 
     2981            return False 
     2982 
    29442983        content = '' 
    2945  
    29462984        crlf = chr(13) + chr(10) 
    29472985        tab = chr(9) 
    29482986 
    2949         # Do it if params exist 
    2950         if self.parameters: 
    2951  
    2952             disp_params = [] 
    2953             for param in self.disp_list: 
    2954                 p1, p2 = param.split('.') 
    2955                 if p1 not in disp_params: 
    2956                     disp_params.append(p1) 
    2957  
    2958             for param in self.parameters: 
    2959                 content += param[1]  # parameter name 
     2987        all_params, fitted_param_names = self._get_copy_params_details() 
     2988 
     2989        # Construct row of parameter names 
     2990        for param in all_params: 
     2991            name = param[1] # Parameter name 
     2992            content += name 
     2993            content += tab 
     2994            if name in fitted_param_names: 
     2995                # Only print errors for fitted parameters 
     2996                content += name + "_err" 
    29602997                content += tab 
    2961                 if param[1] in disp_params: 
    2962                     content += param[1] + ".width" 
    2963                     content += tab 
    2964                 content += param[1] + "_err" 
    2965                 content += tab 
    2966  
    2967             content += crlf 
    2968  
    2969             # row of values and errors... 
    2970             for param in self.parameters: 
    2971                 content += param[2].GetValue()  # value 
    2972                 content += tab 
    2973                 if param[1] in disp_params: 
    2974                     content += str(self.model.getParam(param[1] + '.width')) 
    2975                     content += tab 
    2976                 content += param[4].GetValue()  # error 
    2977                 content += tab 
    2978  
    2979             return content 
    2980         else: 
    2981             return False 
     2998 
     2999        content += crlf 
     3000 
     3001        # Construct row of parameter values and errors 
     3002        for param in all_params: 
     3003            value = param[2] 
     3004            if hasattr(value, 'GetValue'): 
     3005                # param[2] is a text box 
     3006                value = value.GetValue() 
     3007            else: 
     3008                # param[2] is a float (from our self._get_copy_params_details) 
     3009                value = str(value) 
     3010            content += value 
     3011            content += tab 
     3012            if param[1] in fitted_param_names: 
     3013                # Only print errors for fitted parameters 
     3014                content += param[4].GetValue() 
     3015                content += tab  
     3016 
     3017        return content 
    29823018 
    29833019    def get_copy_latex(self): 
     
    29943030        Get the string copies of the param names and values in the tap 
    29953031        """ 
     3032        if not self.parameters: 
     3033            # Do nothing if self.parameters doesn't exist 
     3034            return False 
     3035         
    29963036        content = '\\begin{table}' 
    29973037        content += '\\begin{tabular}[h]' 
     
    30003040        tab = chr(9) 
    30013041 
    3002         # Do it if params exist 
    3003         if self.parameters: 
    3004             disp_params = [] 
    3005             for param in self.disp_list: 
    3006                 p1, p2 = param.split('.') 
    3007                 if p1 not in disp_params: 
    3008                     disp_params.append(p1) 
    3009  
    3010             content += '{|' 
    3011             for param in (self.parameters + disp_params): 
    3012                 content += 'l|l|' 
    3013             content += '}\hline' 
    3014             content += crlf 
    3015  
    3016             for index, param in enumerate(self.parameters): 
    3017                 content += param[1].replace('_', '\_')  # parameter name 
     3042        all_params, fitted_param_names = self._get_copy_params_details() 
     3043 
     3044        content += '{|' 
     3045        for param in all_params: 
     3046            content += 'l|l|' 
     3047        content += '}\hline' 
     3048        content += crlf 
     3049 
     3050        # Construct row of parameter names 
     3051        for index, param in enumerate(all_params): 
     3052            name = param[1] # Parameter name 
     3053            content += name.replace('_', '\_')  # Escape underscores 
     3054            if name in fitted_param_names: 
     3055                # Only print errors for fitted parameters 
    30183056                content += ' & ' 
    3019                 if param[1] in disp_params: 
    3020                     content += param[1].replace('_', '\_') + ".width" 
    3021                     content += ' & ' 
    3022                 content += param[1].replace('_', '\_') + "\_err" 
    3023                 if index < len(self.parameters) - 1: 
    3024                     content += ' & ' 
    3025             content += '\\\\ \\hline' 
    3026             content += crlf 
    3027  
    3028             # row of values and errors... 
    3029             for index, param in enumerate(self.parameters): 
    3030                 content += param[2].GetValue()  # parameter value 
     3057                content += name.replace('_', '\_') + "\_err" 
     3058            if index < len(all_params) - 1: 
    30313059                content += ' & ' 
    3032                 if param[1] in disp_params: 
    3033                     content += str(self.model.getParam(param[1] + '.width')) 
    3034                     content += ' & ' 
    3035                 content += param[4].GetValue()  # parameter error 
    3036                 if index < len(self.parameters) - 1: 
    3037                     content += ' & ' 
    3038             content += '\\\\ \\hline' 
    3039             content += crlf 
    3040  
    3041             content += '\\end{tabular}' 
    3042             content += '\\end{table}' 
    3043             return content 
    3044         else: 
    3045             return False 
     3060 
     3061        content += '\\\\ \\hline' 
     3062        content += crlf 
     3063 
     3064        # Construct row of values and errors 
     3065        for index, param in enumerate(all_params): 
     3066            value = param[2] 
     3067            if hasattr(value, "GetValue"): 
     3068                # value is a text box 
     3069                value = value.GetValue() 
     3070            else: 
     3071                # value is a float (from self._get_copy_params_details) 
     3072                value = str(value) 
     3073            content += value 
     3074            if param[1] in fitted_param_names: 
     3075                # Only print errors for fitted params 
     3076                content += ' & ' 
     3077                content += param[4].GetValue() 
     3078            if index < len(all_params) - 1: 
     3079                content += ' & ' 
     3080         
     3081        content += '\\\\ \\hline' 
     3082        content += crlf 
     3083        content += '\\end{tabular}' 
     3084        content += '\\end{table}' 
     3085 
     3086        return content 
    30463087 
    30473088    def set_clipboard(self, content=None): 
Note: See TracChangeset for help on using the changeset viewer.