Changeset 410aad8 in sasview


Ignore:
Timestamp:
Jun 6, 2009 11:41:45 AM (15 years ago)
Author:
Mathieu Doucet <doucetm@…>
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:
fde249c
Parents:
1abcb04
Message:

prview: completed first cut of functionality to save a P(r) inversion. Need to iron out the file format.

Location:
prview/perspectives/pr
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • prview/perspectives/pr/inversion_state.py

    r91128648 r410aad8  
    8080        self.iq_obs  = None 
    8181        self.iq_calc = None 
     82         
     83        # Coefficients 
     84        self.coefficients = None 
     85        self.covariance = None 
    8286     
    8387    def __str__(self): 
     
    152156         
    153157        # File name 
     158        element = newdoc.createElement("filename") 
    154159        if self.file is not None: 
    155             element = newdoc.createElement("filename") 
    156160            element.appendChild(newdoc.createTextNode(str(self.file))) 
    157             top_element.appendChild(element) 
     161        else: 
     162            element.appendChild(newdoc.createTextNode(str(file))) 
     163        top_element.appendChild(element) 
    158164         
    159165        element = newdoc.createElement("timestamp") 
     
    181187            exec "element.appendChild(newdoc.createTextNode(str(%s)))" % item[1] 
    182188            outputs.appendChild(element) 
     189                     
     190        # Save output coefficients and its covariance matrix 
     191        element = newdoc.createElement("coefficients") 
     192        element.appendChild(newdoc.createTextNode(str(self.coefficients))) 
     193        outputs.appendChild(element) 
     194        element = newdoc.createElement("covariance") 
     195        element.appendChild(newdoc.createTextNode(str(self.covariance))) 
     196        outputs.appendChild(element) 
    183197                     
    184198        # Save the file 
     
    244258                            if node.hasChildNodes(): 
    245259                                for item in node.childNodes: 
     260                                    # Look for standard outputs 
    246261                                    for out in out_list: 
    247262                                        if item.nodeName == out[0]: 
     
    250265                                            except: 
    251266                                                exec '%s = None' % out[1] 
     267                                                 
     268                                    # Look for coefficients 
     269                                    # Format is [value, value, value, value] 
     270                                    if item.nodeName == 'coefficients': 
     271                                        # Remove brackets 
     272                                        c_values = item.childNodes[0].nodeValue.strip().replace('[','') 
     273                                        c_values = c_values.replace(']','') 
     274                                        toks = c_values.split() 
     275                                        self.coefficients = [] 
     276                                        for c in toks: 
     277                                            try: 
     278                                                self.coefficients.append(float(c)) 
     279                                            except: 
     280                                                # Bad data, skip. We will count the number of  
     281                                                # coefficients at the very end and deal with  
     282                                                # inconsistencies then. 
     283                                                pass 
     284                                        # Sanity check 
     285                                        if not len(self.coefficients) == self.nfunc: 
     286                                            # Inconsistent number of coefficients. Don't keep the data. 
     287                                            err_msg = "InversionState.fromXML: inconsistant number of coefficients: " 
     288                                            err_msg += "%d %d" % (len(self.coefficients), self.nfunc) 
     289                                            logging.error(err_msg) 
     290                                            self.coefficients = None 
     291                                             
     292                                    # Look for covariance matrix 
     293                                    # Format is [ [value, value], [value, value] ] 
     294                                    elif item.nodeName == "covariance": 
     295                                        # Parse rows 
     296                                        rows = item.childNodes[0].nodeValue.strip().split('[') 
     297                                        self.covariance = [] 
     298                                        for row in rows: 
     299                                            row = row.strip() 
     300                                            if len(row) == 0: continue 
     301                                            # Remove end bracket 
     302                                            row = row.replace(']','') 
     303                                            c_values = row.split() 
     304                                            cov_row = [] 
     305                                            for c in c_values: 
     306                                                try: 
     307                                                    cov_row.append(float(c)) 
     308                                                except: 
     309                                                    # Bad data, skip. We will count the number of  
     310                                                    # coefficients at the very end and deal with  
     311                                                    # inconsistencies then. 
     312                                                    pass 
     313                                            # Sanity check: check the number of entries in the row 
     314                                            if len(cov_row) == self.nfunc: 
     315                                                self.covariance.append(cov_row) 
     316                                        # Sanity check: check the number of rows in the covariance 
     317                                        # matrix 
     318                                        if not len(self.covariance) == self.nfunc: 
     319                                            # Inconsistent dimensions of the covariance matrix. 
     320                                            # Don't keep the data. 
     321                                            err_msg = "InversionState.fromXML: inconsistant dimensions of the covariance matrix: " 
     322                                            err_msg += "%d %d" % (len(self.covariance), self.nfunc) 
     323                                            logging.error(err_msg) 
     324                                            self.covariance = None 
    252325            else: 
    253326                raise RuntimeError, "Unsupported P(r) file version" 
     
    349422            basename  = os.path.basename(path) 
    350423            root, extension = os.path.splitext(basename) 
     424            #TODO: eventually remove the check for .xml once 
     425            # the P(r) writer/reader is truly complete. 
    351426            if  extension.lower() in self.ext or \ 
    352427                extension.lower() == '.xml': 
     
    369444                    prstate = self._parse_prstate(entry) 
    370445                    sas_entry.meta_data['prstate'] = prstate 
    371                     sas_entry.filename = basename 
     446                    sas_entry.filename = prstate.file 
    372447                    output.append(sas_entry) 
    373448        else: 
     
    378453            return None 
    379454        elif len(output)==1: 
     455            # Call back to post the new state 
     456            self.call_back(output[0].meta_data['prstate'], datainfo = output[0]) 
    380457            return output[0] 
    381458        else: 
     
    396473            if datainfo is None: 
    397474                datainfo = DataLoader.data_info.Data1D(x=[], y=[])     
    398             elif not datainfo.__class__ == DataLoader.data_info.Data1D:  
    399                 raise RuntimeError, "The cansas writer expects a Data1D instance" 
     475            elif not issubclass(datainfo.__class__, DataLoader.data_info.Data1D): 
     476                raise RuntimeError, "The cansas writer expects a Data1D instance: %s" % str(datainfo.__class__.__name__) 
    400477         
    401478            # Create basic XML document 
  • prview/perspectives/pr/pr.py

    rd483799 r410aad8  
    1 #TODO: Use simview to generate P(r) and I(q) pairs in sansview. 
    21# Make sure the option of saving each curve is available  
    32# Use the I(q) curve as input and compare the output to P(r) 
     
    8988        from inversion_state import Reader 
    9089          
    91         reader = Reader(self.set_state) 
     90        #TODO: get rid of the cansas flag  
     91        self.state_reader = Reader(self.set_state, cansas = not self.standalone) 
    9292        l = Loader() 
    93         l.associate_file_reader('.prv', reader) 
     93        l.associate_file_reader('.prv', self.state_reader) 
    9494                 
    9595        # Log startup 
    9696        logging.info("Pr(r) plug-in started") 
    9797         
    98     def set_state(self, state): 
     98    def set_state(self, state, datainfo=None): 
    9999        """ 
    100100            Call-back method for the inversion state reader. 
     
    102102             
    103103            @param state: InversionState object 
    104         """ 
    105         self.control_panel.set_state(state) 
    106         #print state 
     104            @param datainfo: Data1D object [optional] 
     105        """ 
     106        try: 
     107            # If we are not in standalone mode, the panel will not 
     108            # load any data file and we need to keep track of the 
     109            # data here. 
     110            if self.standalone == False: 
     111                if datainfo is None: 
     112                    raise RuntimeError, "Pr.set_state: datainfo parameter cannot be None in standalone mode" 
     113                     
     114                self.current_plottable = datainfo 
     115                self.current_plottable.group_id = datainfo.meta_data['prstate'].file 
     116                 
     117            # Load the P(r) results 
     118            self.control_panel.set_state(state) 
     119                         
     120            # Make sure the user sees the P(r) panel after loading 
     121            self.parent.set_perspective(self.perspective)             
     122 
     123        except: 
     124            logging.error("prview.set_state: %s" % sys.exc_value) 
    107125 
    108126    def populate_menu(self, id, owner): 
     
    828846        self.control_panel.q_max = self.pr.x.max() 
    829847             
    830  
     848    def save_data(self, filepath, prstate=None): 
     849        """ 
     850            Save data in provided state object. 
     851            TODO: move the state code away from inversion_panel and move it here.  
     852                    Then remove the "prstate" input and make this method private. 
     853                     
     854            @param filepath: path of file to write to 
     855            @param prstate: P(r) inversion state  
     856        """ 
     857        #TODO: do we need this or can we use DataLoader.loader.save directly? 
     858         
     859        # Add output data and coefficients to state 
     860        prstate.coefficients = self._last_out 
     861        prstate.covariance = self._last_cov 
     862         
     863        # Write the output to file 
     864        self.state_reader.write(filepath, self.current_plottable, prstate) 
     865         
    831866         
    832867    def setup_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None,  
Note: See TracChangeset for help on using the changeset viewer.