Changeset 410aad8 in sasview
- Timestamp:
- Jun 6, 2009 11:41:45 AM (16 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, 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
- Location:
- prview/perspectives/pr
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
prview/perspectives/pr/inversion_state.py
r91128648 r410aad8 80 80 self.iq_obs = None 81 81 self.iq_calc = None 82 83 # Coefficients 84 self.coefficients = None 85 self.covariance = None 82 86 83 87 def __str__(self): … … 152 156 153 157 # File name 158 element = newdoc.createElement("filename") 154 159 if self.file is not None: 155 element = newdoc.createElement("filename")156 160 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) 158 164 159 165 element = newdoc.createElement("timestamp") … … 181 187 exec "element.appendChild(newdoc.createTextNode(str(%s)))" % item[1] 182 188 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) 183 197 184 198 # Save the file … … 244 258 if node.hasChildNodes(): 245 259 for item in node.childNodes: 260 # Look for standard outputs 246 261 for out in out_list: 247 262 if item.nodeName == out[0]: … … 250 265 except: 251 266 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 252 325 else: 253 326 raise RuntimeError, "Unsupported P(r) file version" … … 349 422 basename = os.path.basename(path) 350 423 root, extension = os.path.splitext(basename) 424 #TODO: eventually remove the check for .xml once 425 # the P(r) writer/reader is truly complete. 351 426 if extension.lower() in self.ext or \ 352 427 extension.lower() == '.xml': … … 369 444 prstate = self._parse_prstate(entry) 370 445 sas_entry.meta_data['prstate'] = prstate 371 sas_entry.filename = basename446 sas_entry.filename = prstate.file 372 447 output.append(sas_entry) 373 448 else: … … 378 453 return None 379 454 elif len(output)==1: 455 # Call back to post the new state 456 self.call_back(output[0].meta_data['prstate'], datainfo = output[0]) 380 457 return output[0] 381 458 else: … … 396 473 if datainfo is None: 397 474 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__) 400 477 401 478 # 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.2 1 # Make sure the option of saving each curve is available 3 2 # Use the I(q) curve as input and compare the output to P(r) … … 89 88 from inversion_state import Reader 90 89 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) 92 92 l = Loader() 93 l.associate_file_reader('.prv', reader)93 l.associate_file_reader('.prv', self.state_reader) 94 94 95 95 # Log startup 96 96 logging.info("Pr(r) plug-in started") 97 97 98 def set_state(self, state ):98 def set_state(self, state, datainfo=None): 99 99 """ 100 100 Call-back method for the inversion state reader. … … 102 102 103 103 @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) 107 125 108 126 def populate_menu(self, id, owner): … … 828 846 self.control_panel.q_max = self.pr.x.max() 829 847 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 831 866 832 867 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.