Changeset 78312f7 in sasview for src/sas/sasgui/perspectives/fitting


Ignore:
Timestamp:
Jun 19, 2017 6:10:07 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:
2a0f33f
Parents:
bc04647
Message:

remove wx references from fitting pagestate

Location:
src/sas/sasgui/perspectives/fitting
Files:
3 edited

Legend:

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

    r914c49d5 r78312f7  
    66import sys 
    77import os 
    8 import wx 
    9 import numpy as np 
    108import time 
    119import copy 
     
    1412import logging 
    1513import traceback 
    16  
    1714from collections import defaultdict 
     15 
     16import numpy as np 
     17 
     18import wx 
    1819from wx.lib.scrolledpanel import ScrolledPanel 
    1920 
    2021from sasmodels.weights import MODELS as POLYDISPERSITY_MODELS 
     22 
     23from sas.sascalc.dataloader.data_info import Detector 
     24from sas.sascalc.dataloader.data_info import Source 
    2125 
    2226from sas.sasgui.guiframe.panel_base import PanelBase 
     
    3034from sas.sasgui.guiframe.dataFitting import check_data_validity 
    3135from sas.sasgui.guiframe.gui_style import GUIFRAME_ID 
    32 from sas.sascalc.dataloader.data_info import Detector 
    33 from sas.sascalc.dataloader.data_info import Source 
    34 from sas.sasgui.perspectives.fitting.pagestate import PageState 
    3536from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller 
    3637from sas.sasgui.guiframe.documentation_window import DocumentationWindow 
     38 
     39from sas.sasgui.perspectives.fitting.pagestate import PageState 
     40from sas.sasgui.perspectives.fitting.report_dialog import ReportDialog 
    3741 
    3842logger = logging.getLogger(__name__) 
     
    619623        # Get plot image from plotpanel 
    620624        images, canvases = self.get_images() 
    621         # get the report dialog 
    622         self.state.report(images, canvases) 
     625        imgRAM, images, refs = self._build_plots_for_report(images, canvases) 
     626 
     627        # get the strings for report 
     628        report_str, text_str = self.state.report(fig_urls=refs) 
     629 
     630        # Show the dialog 
     631        report_list = [report_str, text_str, images] 
     632        dialog = ReportDialog(report_list, None, wx.ID_ANY, "") 
     633        dialog.Show() 
     634 
     635    def _build_plots_for_report(self, figs, canvases): 
     636        """ 
     637        Build image state that wx.html understand 
     638        by plotting, putting it into wx.FileSystem image object 
     639        """ 
     640        images = [] 
     641        refs = [] 
     642 
     643        # For no figures in the list, prepare empty plot 
     644        if figs is None or len(figs) == 0: 
     645            figs = [None] 
     646 
     647        # Loop over the list of figures 
     648        # use wx.MemoryFSHandler 
     649        imgRAM = wx.MemoryFSHandler() 
     650        for fig in figs: 
     651            if fig is not None: 
     652                ind = figs.index(fig) 
     653                canvas = canvases[ind] 
     654 
     655            # store the image in wx.FileSystem Object 
     656            wx.FileSystem.AddHandler(wx.MemoryFSHandler()) 
     657 
     658            # index of the fig 
     659            ind = figs.index(fig) 
     660 
     661            # AddFile, image can be retrieved with 'memory:filename' 
     662            name = 'img_fit%s.png' % ind 
     663            refs.append('memory:' + name) 
     664            imgRAM.AddFile(name, canvas.bitmap, wx.BITMAP_TYPE_PNG) 
     665 
     666            # append figs 
     667            images.append(fig) 
     668 
     669        return imgRAM, images, refs 
     670 
    623671 
    624672    def on_save(self, event): 
  • src/sas/sasgui/perspectives/fitting/pagestate.py

    r959eb01 r78312f7  
    1515import os 
    1616import sys 
    17 import wx 
    1817import copy 
    1918import logging 
     
    519518        rep = "\nState name: %s\n" % self.file 
    520519        t = time.localtime(self.timestamp) 
    521         time_str = time.strftime("%b %d %Y %H;%M;%S ", t) 
     520        time_str = time.strftime("%b %d %Y %H:%M:%S ", t) 
    522521 
    523522        rep += "State created: %s\n" % time_str 
     
    596595        return rep 
    597596 
    598     def set_report_string(self): 
     597    def _get_report_string(self): 
    599598        """ 
    600599        Get the values (strings) from __str__ for report 
     
    611610        q_range = "" 
    612611        strings = self.__repr__() 
     612        fixed_parameter = False 
    613613        lines = strings.split('\n') 
    614  
    615614        # get all string values from __str__() 
    616615        for line in lines: 
    617             value = "" 
    618             content = line.split(":") 
    619             name = content[0] 
    620             try: 
    621                 value = content[1] 
    622             except Exception: 
    623                 msg = "Report string expected 'name: value' but got %r" % line 
    624                 logger.error(msg) 
    625             if name.count("State created"): 
    626                 repo_time = "" + value 
    627             if name.count("parameter name"): 
     616            # Skip lines which are not key: value pairs, which includes 
     617            # blank lines and freeform notes in SASNotes fields. 
     618            if not ':' in line: 
     619                #msg = "Report string expected 'name: value' but got %r" % line 
     620                #logger.error(msg) 
     621                continue 
     622 
     623            name, value = [s.strip() for s in line.split(":", 1)] 
     624            if name == "State created": 
     625                repo_time = value 
     626            elif name == "parameter name": 
    628627                val_name = value.split(".") 
    629628                if len(val_name) > 1: 
     
    634633                else: 
    635634                    param_string += value + ',' 
    636             if name == "value": 
     635            elif name == "value": 
    637636                param_string += value + ',' 
    638             fixed_parameter = False 
    639             if name == "selected": 
    640                 if value == u' False': 
    641                     fixed_parameter = True 
    642             if name == "error value": 
     637            elif name == "selected": 
     638                # remember if it is fixed when reporting error value 
     639                fixed_parameter = (value == u'False') 
     640            elif name == "error value": 
    643641                if fixed_parameter: 
    644642                    param_string += '(fixed),' 
    645643                else: 
    646644                    param_string += value + ',' 
    647             if name == "parameter unit": 
     645            elif name == "parameter unit": 
    648646                param_string += value + ':' 
    649             if name == "Value of Chisqr ": 
     647            elif name == "Value of Chisqr": 
    650648                chi2 = ("Chi2/Npts = " + value) 
    651649                chi2_string = CENTRE % chi2 
    652             if name == "Title": 
     650            elif name == "Title": 
    653651                if len(value.strip()) == 0: 
    654652                    continue 
    655653                title = value + " [" + repo_time + "]" 
    656654                title_name = HEADER % title 
    657             if name == "data ": 
     655            elif name == "data": 
    658656                try: 
    659                     file_value = ("File name:" + content[2]) 
     657                    # parsing "data : File:     filename [mmm dd hh:mm]" 
     658                    name = value.split(':', 1)[1].strip() 
     659                    file_value = "File name:" + name 
    660660                    file_name = CENTRE % file_value 
    661661                    if len(title) == 0: 
    662                         title = content[2] + " [" + repo_time + "]" 
     662                        title = name + " [" + repo_time + "]" 
    663663                        title_name = HEADER % title 
    664664                except Exception: 
    665665                    msg = "While parsing 'data: ...'\n" 
    666666                    logger.error(msg + traceback.format_exc()) 
    667             if name == "model name ": 
     667            elif name == "model name": 
    668668                try: 
    669                     modelname = "Model name:" + content[1] 
    670                 except: 
     669                    modelname = "Model name:" + value 
     670                except Exception: 
    671671                    modelname = "Model name:" + " NAN" 
    672672                model_name = CENTRE % modelname 
    673673 
    674             if name == "Plotting Range": 
     674            elif name == "Plotting Range": 
    675675                try: 
    676                     q_range = content[1] + " = " + content[2] \ 
    677                             + " = " + content[3].split(",")[0] 
     676                    parts = value.split(':') 
     677                    q_range = parts[0] + " = " + parts[1] \ 
     678                            + " = " + parts[2].split(",")[0] 
    678679                    q_name = ("Q Range:    " + q_range) 
    679680                    q_range = CENTRE % q_name 
     
    681682                    msg = "While parsing 'Plotting Range: ...'\n" 
    682683                    logger.error(msg + traceback.format_exc()) 
     684 
    683685        paramval = "" 
    684686        for lines in param_string.split(":"): 
     
    709711                                   "\n" + paramval_string + \ 
    710712                                   "\n" + ELINE + \ 
    711                                    "\n" + FEET_1 % title + \ 
    712                                    "\n" + FEET_2 
     713                                   "\n" + FEET_1 % title 
    713714 
    714715        return html_string, text_string, title 
     
    723724        return name 
    724725 
    725     def report(self, figs=None, canvases=None): 
     726    def report(self, fig_urls): 
    726727        """ 
    727728        Invoke report dialog panel 
     
    729730        : param figs: list of pylab figures [list] 
    730731        """ 
    731         from sas.sasgui.perspectives.fitting.report_dialog import ReportDialog 
    732732        # get the strings for report 
    733         html_str, text_str, title = self.set_report_string() 
     733        html_str, text_str, title = self._get_report_string() 
    734734        # Allow 2 figures to append 
    735         if len(figs) == 1: 
    736             add_str = FEET_3 
    737         elif len(figs) == 2: 
    738             add_str = ELINE 
    739             add_str += FEET_2 % ("%s") 
    740             add_str += ELINE 
    741             add_str += FEET_3 
    742         elif len(figs) > 2: 
    743             add_str = ELINE 
    744             add_str += FEET_2 % ("%s") 
    745             add_str += ELINE 
    746             add_str += FEET_2 % ("%s") 
    747             add_str += ELINE 
    748             add_str += FEET_3 
    749         else: 
    750             add_str = "" 
     735        image_links = [FEET_2%fig for fig in fig_urls] 
    751736 
    752737        # final report html strings 
    753         report_str = html_str % ("%s") + add_str 
    754  
    755         # make plot image 
    756         images = self.set_plot_state(figs, canvases) 
    757         report_list = [report_str, text_str, images] 
    758         dialog = ReportDialog(report_list, None, wx.ID_ANY, "") 
    759         dialog.Show() 
     738        report_str = html_str + ELINE.join(image_links) 
     739 
     740        return report_str, text_str 
    760741 
    761742    def _to_xml_helper(self, thelist, element, newdoc): 
     
    805786            try: 
    806787                top_element = newdoc.createElement(FITTING_NODE_NAME) 
    807             except: 
     788            except Exception: 
    808789                string = etree.tostring(doc, pretty_print=True) 
    809790                newdoc = parseString(string) 
     
    814795                try: 
    815796                    entry_node.appendChild(top_element) 
    816                 except: 
     797                except Exception: 
    817798                    node_name = entry_node.tag 
    818799                    node_list = newdoc.getElementsByTagName(node_name) 
     
    10361017                try: 
    10371018                    self.timestamp = float(entry.get('epoch')) 
    1038                 except: 
     1019                except Exception: 
    10391020                    msg = "PageState.fromXML: Could not" 
    10401021                    msg += " read timestamp\n %s" % sys.exc_value 
     
    11041085                        dic[name] = np.array(value_list) 
    11051086                    setattr(self, varname, dic) 
    1106  
    1107     def set_plot_state(self, figs, canvases): 
    1108         """ 
    1109         Build image state that wx.html understand 
    1110         by plotting, putting it into wx.FileSystem image object 
    1111  
    1112         """ 
    1113         images = [] 
    1114  
    1115         # Reset memory 
    1116         self.imgRAM = None 
    1117         wx.MemoryFSHandler() 
    1118  
    1119         # For no figures in the list, prepare empty plot 
    1120         if figs is None or len(figs) == 0: 
    1121             figs = [None] 
    1122  
    1123         # Loop over the list of figures 
    1124         # use wx.MemoryFSHandler 
    1125         self.imgRAM = wx.MemoryFSHandler() 
    1126         for fig in figs: 
    1127             if fig is not None: 
    1128                 ind = figs.index(fig) 
    1129                 canvas = canvases[ind] 
    1130  
    1131             # store the image in wx.FileSystem Object 
    1132             wx.FileSystem.AddHandler(wx.MemoryFSHandler()) 
    1133  
    1134             # index of the fig 
    1135             ind = figs.index(fig) 
    1136  
    1137             # AddFile, image can be retrieved with 'memory:filename' 
    1138             self.imgRAM.AddFile('img_fit%s.png' % ind, 
    1139                                 canvas.bitmap, wx.BITMAP_TYPE_PNG) 
    1140  
    1141             # append figs 
    1142             images.append(fig) 
    1143  
    1144         return images 
    11451087 
    11461088 
     
    14101352        return doc 
    14111353 
    1412 # Simple html report templet 
     1354# Simple html report template 
    14131355HEADER = "<html>\n" 
    14141356HEADER += "<head>\n" 
     
    14381380""" 
    14391381FEET_2 = \ 
    1440 """ 
    1441 <img src="%s" > 
    1442 </img> 
     1382"""<img src="%s" ></img> 
    14431383""" 
    14441384FEET_3 = \ 
    1445 """ 
    1446 </center> 
     1385"""</center> 
    14471386</div> 
    14481387</body> 
    14491388</html> 
    14501389""" 
    1451 ELINE = "<p class=MsoNormal>&nbsp;</p>" 
     1390ELINE = """<p class=MsoNormal>&nbsp;</p> 
     1391""" 
  • src/sas/sasgui/perspectives/fitting/report_dialog.py

    r7432acb r78312f7  
    3838        # number of images of plot 
    3939        self.nimages = len(self.report_list[2]) 
    40  
    41         if self.report_list[2] is not None: 
    42             # put image path in the report string 
    43             if len(self.report_list[2]) == 1: 
    44                 self.report_html = self.report_list[0] % \ 
    45                                     "memory:img_fit0.png" 
    46             elif len(self.report_list[2]) == 2: 
    47                 self.report_html = self.report_list[0] % \ 
    48                                     ("memory:img_fit0.png", 
    49                                      "memory:img_fit1.png") 
    50             # allows up to three images 
    51             else: 
    52                 self.report_html = self.report_list[0] % \ 
    53                                     ("memory:img_fit0.png", 
    54                                      "memory:img_fit1.png", 
    55                                      "memory:img_fit2.png") 
    56         else: 
    57             self.report_html = self.report_list[0] 
     40        self.report_html = self.report_list[0] 
    5841        # layout 
    5942        self._setup_layout() 
     
    10588            elif self.nimages == 3: 
    10689                html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), 
    107                                           str(pic_fname[2])) 
     90                                       str(pic_fname[2])) 
    10891 
    10992            # make/open file in case of absence 
     
    118101                    #Windows 
    119102                    os.startfile(str(fName)) 
    120                 except: 
     103                except Exception: 
    121104                    try: 
    122105                        #Mac 
    123106                        os.system("open %s" % fName) 
    124                     except: 
     107                    except Exception: 
    125108                        #DO not open 
    126109                        pass 
Note: See TracChangeset for help on using the changeset viewer.