def fromXML(self, file=None, node=None): """ Load invariant states from a file : param file: .inv file : param node: node of a XML document to read from """ if file is not None: msg = "InvariantSate no longer supports non-CanSAS" msg += " format for invariant files" raise RuntimeError, msg if node.get('version')\ and node.get('version') == '1.0': # Get file name entry = get_content('ns:filename', node) if entry is not None: file_name = entry.text.strip() # Get time stamp entry = get_content('ns:timestamp', node) if entry is not None and entry.get('epoch'): try: timestamp = (entry.get('epoch')) except: msg = "InvariantSate.fromXML: Could not read" msg += " timestamp\n %s" % sys.exc_value logging.error(msg) # Parse bookmarks entry_bookmark = get_content('ns:bookmark', node) for ind in range(1, len(entry_bookmark) + 1): temp_state = {} temp_bookmark = {} entry = get_content('ns:mark_%s' % ind, entry_bookmark) if entry is not None: my_time = get_content('ns:time', entry) val_time = str(my_time.text.strip()) date = get_content('ns:date', entry) val_date = str(date.text.strip()) state_entry = get_content('ns:state', entry) for item in DEFAULT_STATE: input_field = get_content('ns:%s' % item, state_entry) val = str(input_field.text.strip()) if input_field is not None: temp_state[item] = val comp_entry = get_content('ns:comp_state', entry) for item in DEFAULT_STATE: input_field = get_content('ns:%s' % item, comp_entry) val = str(input_field.text.strip()) if input_field is not None: temp_bookmark[item] = val try: self.bookmark_list[ind] = [val_time, val_date, temp_state, temp_bookmark] except: raise "missing components of bookmarks..." # Parse histories entry_history = get_content('ns:history', node) for ind in range(0, len(entry_history)): temp_state = {} entry = get_content('ns:state_%s' % ind, entry_history) if entry is not None: for item in DEFAULT_STATE: input_field = get_content('ns:%s' % item, entry) val = str(input_field.text.strip()) if input_field is not None: temp_state[item] = val self.state_list[ind] = temp_state # Parse current state (ie, saved_state) entry = get_content('ns:state', node) if entry is not None: for item in DEFAULT_STATE: input_field = get_content('ns:%s' % item, entry) val = str(input_field.text.strip()) if input_field is not None: self.set_saved_state(name=item, value=val) self.file = file_name
def fromXML(self, file=None, node=None): """ Load a P(r) inversion state from a file :param file: .prv file :param node: node of a XML document to read from """ if file is not None: msg = "InversionState no longer supports non-CanSAS" msg += " format for P(r) files" raise RuntimeError, msg if node.get('version') and node.get('version') == '1.0': # Get file name entry = get_content('ns:filename', node) if entry is not None: self.file = entry.text.strip() # Get time stamp entry = get_content('ns:timestamp', node) if entry is not None and entry.get('epoch'): try: self.timestamp = float(entry.get('epoch')) except: msg = "InversionState.fromXML: Could not read " msg += "timestamp\n %s" % sys.exc_value logging.error(msg) # Parse inversion inputs entry = get_content('ns:inputs', node) if entry is not None: for item in in_list: input_field = get_content('ns:%s' % item[0], entry) if input_field is not None: try: setattr(self, item[1], float(input_field.text.strip())) except: setattr(self, item[1], None) input_field = get_content('ns:estimate_bck', entry) if input_field is not None: try: self.estimate_bck = input_field.text.strip() == 'True' except: self.estimate_bck = False # Parse inversion outputs entry = get_content('ns:outputs', node) if entry is not None: # Output parameters (scalars) for item in out_list: input_field = get_content('ns:%s' % item[0], entry) if input_field is not None: try: setattr(self, item[1], float(input_field.text.strip())) except: setattr(self, item[1], None) # Look for coefficients # Format is [value, value, value, value] coeff = get_content('ns:coefficients', entry) if coeff is not None: # Remove brackets c_values = coeff.text.strip().replace('[', '') c_values = c_values.replace(']', '') toks = c_values.split() self.coefficients = [] for c in toks: try: self.coefficients.append(float(c)) except: # Bad data, skip. We will count the number of # coefficients at the very end and deal with # inconsistencies then. pass # Sanity check if not len(self.coefficients) == self.nfunc: # Inconsistent number of coefficients. # Don't keep the data. err_msg = "InversionState.fromXML: inconsistant " err_msg += "number of coefficients: " err_msg += "%d %d" % (len(self.coefficients), self.nfunc) logging.error(err_msg) self.coefficients = None # Look for covariance matrix # Format is [ [value, value], [value, value] ] coeff = get_content('ns:covariance', entry) if coeff is not None: # Parse rows rows = coeff.text.strip().split('[') self.covariance = [] for row in rows: row = row.strip() if len(row) == 0: continue # Remove end bracket row = row.replace(']', '') c_values = row.split() cov_row = [] for c in c_values: try: cov_row.append(float(c)) except: # Bad data, skip. We will count the number of # coefficients at the very end and deal with # inconsistencies then. pass # Sanity check: check the number of entries in the row if len(cov_row) == self.nfunc: self.covariance.append(cov_row) # Sanity check: check the number of rows in the covariance # matrix if not len(self.covariance) == self.nfunc: # Inconsistent dimensions of the covariance matrix. # Don't keep the data. err_msg = "InversionState.fromXML: " err_msg += "inconsistant dimensions of the " err_msg += " covariance matrix: " err_msg += "%d %d" % (len(self.covariance), self.nfunc) logging.error(err_msg) self.covariance = None