[51f14603] | 1 | """ |
---|
[78a205a] | 2 | State class for the invariant UI |
---|
[51f14603] | 3 | """ |
---|
| 4 | |
---|
[78a205a] | 5 | # import time |
---|
[51f14603] | 6 | import os |
---|
| 7 | import sys |
---|
| 8 | import logging |
---|
| 9 | import copy |
---|
[b699768] | 10 | import sas.sascalc.dataloader |
---|
[78a205a] | 11 | # from xml.dom.minidom import parse |
---|
[51f14603] | 12 | from lxml import etree |
---|
[b699768] | 13 | from sas.sascalc.dataloader.readers.cansas_reader import Reader as CansasReader |
---|
| 14 | from sas.sascalc.dataloader.readers.cansas_reader import get_content |
---|
[d85c194] | 15 | from sas.sasgui.guiframe.utils import format_number |
---|
| 16 | from sas.sasgui.guiframe.gui_style import GUIFRAME_ID |
---|
| 17 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
[51f14603] | 18 | |
---|
| 19 | INVNODE_NAME = 'invariant' |
---|
| 20 | CANSAS_NS = "cansas1d/1.0" |
---|
| 21 | |
---|
| 22 | # default state |
---|
[78a205a] | 23 | DEFAULT_STATE = {'file': 'None', |
---|
| 24 | 'compute_num':0, |
---|
| 25 | 'state_num':0, |
---|
| 26 | 'is_time_machine':False, |
---|
| 27 | 'background_tcl':0.0, |
---|
| 28 | 'scale_tcl':1.0, |
---|
| 29 | 'contrast_tcl':1.0, |
---|
| 30 | 'porod_constant_tcl':'', |
---|
| 31 | 'npts_low_tcl':10, |
---|
| 32 | 'npts_high_tcl':10, |
---|
| 33 | 'power_high_tcl':4.0, |
---|
| 34 | 'power_low_tcl': 4.0, |
---|
| 35 | 'enable_high_cbox':False, |
---|
| 36 | 'enable_low_cbox':False, |
---|
| 37 | 'guinier': True, |
---|
| 38 | 'power_law_high': False, |
---|
| 39 | 'power_law_low': False, |
---|
| 40 | 'fit_enable_high': False, |
---|
| 41 | 'fit_enable_low': False, |
---|
| 42 | 'fix_enable_high':True, |
---|
| 43 | 'fix_enable_low':True, |
---|
| 44 | 'volume_tcl':'', |
---|
| 45 | 'volume_err_tcl':'', |
---|
| 46 | 'surface_tcl':'', |
---|
| 47 | 'surface_err_tcl':''} |
---|
| 48 | # list of states: This list will be filled as panel |
---|
[51f14603] | 49 | # init and the number of states increases |
---|
| 50 | state_list = {} |
---|
| 51 | bookmark_list = {} |
---|
[78a205a] | 52 | # list of input parameters (will be filled up on panel init) used by __str__ |
---|
[51f14603] | 53 | input_list = {'background_tcl':0, |
---|
[78a205a] | 54 | 'scale_tcl':0, |
---|
| 55 | 'contrast_tcl':0, |
---|
| 56 | 'porod_constant_tcl':'', |
---|
| 57 | 'npts_low_tcl':0, |
---|
| 58 | 'npts_high_tcl':0, |
---|
| 59 | 'power_high_tcl':0, |
---|
| 60 | 'power_low_tcl': 0} |
---|
| 61 | # list of output parameters (order sensitive) used by __str__ |
---|
| 62 | output_list = [["qstar_low", "Q* from low Q extrapolation [1/(cm*A)]"], |
---|
| 63 | ["qstar_low_err", "dQ* from low Q extrapolation"], |
---|
| 64 | ["qstar_low_percent", "Q* percent from low Q extrapolation"], |
---|
| 65 | ["qstar", "Q* from data [1/(cm*A)]"], |
---|
| 66 | ["qstar_err", "dQ* from data"], |
---|
| 67 | ["qstar_percent", "Q* percent from data"], |
---|
| 68 | ["qstar_high", "Q* from high Q extrapolation [1/(cm*A)]"], |
---|
| 69 | ["qstar_high_err", "dQ* from high Q extrapolation"], |
---|
[51f14603] | 70 | ["qstar_high_percent", "Q* percent from low Q extrapolation"], |
---|
[78a205a] | 71 | ["qstar_total", "total Q* [1/(cm*A)]"], |
---|
| 72 | ["qstar_total_err", "total dQ*"], |
---|
| 73 | ["volume", "volume fraction"], |
---|
| 74 | ["volume_err", "volume fraction error"], |
---|
| 75 | ["surface", "specific surface"], |
---|
| 76 | ["surface_err", "specific surface error"]] |
---|
| 77 | |
---|
[51f14603] | 78 | |
---|
| 79 | |
---|
| 80 | class InvariantState(object): |
---|
| 81 | """ |
---|
| 82 | Class to hold the state information of the InversionControl panel. |
---|
| 83 | """ |
---|
| 84 | def __init__(self): |
---|
| 85 | """ |
---|
| 86 | Default values |
---|
| 87 | """ |
---|
[78a205a] | 88 | # Input |
---|
| 89 | self.file = None |
---|
[51f14603] | 90 | self.data = Data1D(x=[], y=[], dx=None, dy=None) |
---|
[78a205a] | 91 | self.theory_lowQ = Data1D(x=[], y=[], dy=None) |
---|
[51f14603] | 92 | self.theory_lowQ.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
| 93 | self.theory_highQ = Data1D(x=[], y=[], dy=None) |
---|
| 94 | self.theory_highQ.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[78a205a] | 95 | # self.is_time_machine = False |
---|
| 96 | self.saved_state = DEFAULT_STATE |
---|
[51f14603] | 97 | self.state_list = state_list |
---|
| 98 | self.bookmark_list = bookmark_list |
---|
| 99 | self.input_list = input_list |
---|
| 100 | self.output_list = output_list |
---|
[78a205a] | 101 | |
---|
[51f14603] | 102 | self.compute_num = 0 |
---|
| 103 | self.state_num = 0 |
---|
| 104 | self.timestamp = ('00:00:00', '00/00/0000') |
---|
| 105 | self.container = None |
---|
[78a205a] | 106 | # plot image |
---|
[51f14603] | 107 | self.wximbmp = None |
---|
| 108 | # report_html strings |
---|
[d85c194] | 109 | import sas.sasgui.perspectives.invariant as invariant |
---|
[51f14603] | 110 | path = invariant.get_data_path(media='media') |
---|
[78a205a] | 111 | path_report_html = os.path.join(path, "report_template.html") |
---|
| 112 | html_template = open(path_report_html, "r") |
---|
[51f14603] | 113 | self.template_str = html_template.read() |
---|
| 114 | self.report_str = self.template_str |
---|
[78a205a] | 115 | # self.report_str_save = None |
---|
[51f14603] | 116 | html_template.close() |
---|
[78a205a] | 117 | |
---|
[51f14603] | 118 | def __str__(self): |
---|
| 119 | """ |
---|
| 120 | Pretty print |
---|
[78a205a] | 121 | |
---|
[51f14603] | 122 | : return: string representing the state |
---|
| 123 | """ |
---|
| 124 | # Input string |
---|
| 125 | compute_num = self.saved_state['compute_num'] |
---|
| 126 | compute_state = self.state_list[str(compute_num)] |
---|
| 127 | my_time, date = self.timestamp |
---|
| 128 | file_name = self.file |
---|
| 129 | |
---|
| 130 | state_num = int(self.saved_state['state_num']) |
---|
| 131 | state = "\n[Invariant computation for %s: " % file_name |
---|
| 132 | state += "performed at %s on %s] \n" % (my_time, date) |
---|
| 133 | state += "State No.: %d \n" % state_num |
---|
| 134 | state += "\n=== Inputs ===\n" |
---|
[78a205a] | 135 | |
---|
[51f14603] | 136 | # text ctl general inputs ( excluding extrapolation text ctl) |
---|
[78a205a] | 137 | for key, value in self.input_list.iteritems(): |
---|
[51f14603] | 138 | if value == '': |
---|
| 139 | continue |
---|
[78a205a] | 140 | key_split = key.split('_') |
---|
| 141 | max_ind = len(key_split) - 1 |
---|
| 142 | if key_split[max_ind] == 'tcl': |
---|
[51f14603] | 143 | name = "" |
---|
| 144 | if key_split[1] == 'low' or key_split[1] == 'high': |
---|
| 145 | continue |
---|
| 146 | for ind in range(0, max_ind): |
---|
| 147 | name += " %s" % key_split[ind] |
---|
| 148 | state += "%s: %s\n" % (name.lstrip(" "), value) |
---|
[78a205a] | 149 | |
---|
| 150 | # other input parameters |
---|
[51f14603] | 151 | extra_lo = compute_state['enable_low_cbox'] |
---|
| 152 | if compute_state['enable_low_cbox']: |
---|
| 153 | if compute_state['guinier']: |
---|
| 154 | extra_lo = 'Guinier' |
---|
| 155 | else: |
---|
| 156 | extra_lo = 'Power law' |
---|
| 157 | extra_hi = compute_state['enable_high_cbox'] |
---|
| 158 | if compute_state['enable_high_cbox']: |
---|
| 159 | extra_hi = 'Power law' |
---|
[78a205a] | 160 | state += "\nExtrapolation: High=%s; Low=%s\n" % (extra_hi, extra_lo) |
---|
[51f14603] | 161 | low_off = False |
---|
| 162 | high_off = False |
---|
[78a205a] | 163 | for key, value in self.input_list.iteritems(): |
---|
| 164 | key_split = key.split('_') |
---|
| 165 | max_ind = len(key_split) - 1 |
---|
| 166 | if key_split[max_ind] == 'tcl': |
---|
| 167 | name = "" |
---|
[51f14603] | 168 | # check each buttons whether or not ON or OFF |
---|
| 169 | if key_split[1] == 'low' or key_split[1] == 'high': |
---|
| 170 | if not compute_state['enable_low_cbox'] and \ |
---|
[78a205a] | 171 | key_split[max_ind - 1] == 'low': |
---|
[51f14603] | 172 | low_off = True |
---|
[78a205a] | 173 | continue |
---|
[51f14603] | 174 | elif not compute_state['enable_high_cbox'] and \ |
---|
[78a205a] | 175 | key_split[max_ind - 1] == 'high': |
---|
[51f14603] | 176 | high_off = True |
---|
| 177 | continue |
---|
| 178 | elif extra_lo == 'Guinier' and key_split[0] == 'power' and \ |
---|
[78a205a] | 179 | key_split[max_ind - 1] == 'low': |
---|
[51f14603] | 180 | continue |
---|
| 181 | for ind in range(0, max_ind): |
---|
| 182 | name += " %s" % key_split[ind] |
---|
| 183 | name = name.lstrip(" ") |
---|
[78a205a] | 184 | if name == "power low": |
---|
[51f14603] | 185 | if compute_state['fix_enable_low']: |
---|
| 186 | name += ' (Fixed)' |
---|
| 187 | else: |
---|
| 188 | name += ' (Fitted)' |
---|
[78a205a] | 189 | if name == "power high": |
---|
[51f14603] | 190 | if compute_state['fix_enable_high']: |
---|
| 191 | name += ' (Fixed)' |
---|
| 192 | else: |
---|
| 193 | name += ' (Fitted)' |
---|
| 194 | state += "%s: %s\n" % (name, value) |
---|
| 195 | # Outputs |
---|
| 196 | state += "\n=== Outputs ===" |
---|
| 197 | for item in output_list: |
---|
[78a205a] | 198 | item_split = item[0].split('_') |
---|
[51f14603] | 199 | # Exclude the extrapolation that turned off |
---|
| 200 | if len(item_split) > 1: |
---|
| 201 | if low_off and item_split[1] == 'low': |
---|
| 202 | continue |
---|
| 203 | if high_off and item_split[1] == 'high': |
---|
| 204 | continue |
---|
| 205 | max_ind = len(item_split) - 1 |
---|
| 206 | value = None |
---|
[78a205a] | 207 | if hasattr(self.container, item[0]): |
---|
[51f14603] | 208 | # Q* outputs |
---|
[78a205a] | 209 | value = getattr(self.container, item[0]) |
---|
| 210 | else: |
---|
[51f14603] | 211 | # other outputs than Q* |
---|
| 212 | name = item[0] + "_tcl" |
---|
| 213 | if name in self.saved_state.keys(): |
---|
[78a205a] | 214 | value = self.saved_state[name] |
---|
| 215 | |
---|
| 216 | # Exclude the outputs w/'' |
---|
[51f14603] | 217 | if value == '': |
---|
[78a205a] | 218 | continue |
---|
[51f14603] | 219 | # Error outputs |
---|
| 220 | if item_split[max_ind] == 'err': |
---|
| 221 | state += "+- %s " % format_number(value) |
---|
| 222 | # Percentage outputs |
---|
| 223 | elif item_split[max_ind] == 'percent': |
---|
[78a205a] | 224 | value = float(value) * 100 |
---|
[51f14603] | 225 | state += "(%s %s)" % (format_number(value), '%') |
---|
| 226 | # Outputs |
---|
| 227 | else: |
---|
[78a205a] | 228 | state += "\n%s: %s " % (item[1], |
---|
| 229 | format_number(value, high=True)) |
---|
[51f14603] | 230 | # Include warning msg |
---|
| 231 | if self.container is not None: |
---|
| 232 | state += "\n\nNote:\n" + self.container.warning_msg |
---|
| 233 | return state |
---|
| 234 | |
---|
| 235 | def clone_state(self): |
---|
| 236 | """ |
---|
| 237 | deepcopy the state |
---|
| 238 | """ |
---|
| 239 | return copy.deepcopy(self.saved_state) |
---|
| 240 | |
---|
| 241 | def toXML(self, file="inv_state.inv", doc=None, entry_node=None): |
---|
| 242 | """ |
---|
| 243 | Writes the state of the InversionControl panel to file, as XML. |
---|
[78a205a] | 244 | |
---|
[51f14603] | 245 | Compatible with standalone writing, or appending to an |
---|
| 246 | already existing XML document. In that case, the XML document |
---|
[78a205a] | 247 | is required. An optional entry node in the XML document |
---|
[51f14603] | 248 | may also be given. |
---|
[78a205a] | 249 | |
---|
[51f14603] | 250 | : param file: file to write to |
---|
| 251 | : param doc: XML document object [optional] |
---|
[78a205a] | 252 | : param entry_node: XML node within the document at which we will append the data [optional] |
---|
[51f14603] | 253 | """ |
---|
[92a2ecd] | 254 | # TODO: Get this to work |
---|
[51f14603] | 255 | from xml.dom.minidom import getDOMImplementation |
---|
| 256 | import time |
---|
| 257 | timestamp = time.time() |
---|
| 258 | # Check whether we have to write a standalone XML file |
---|
| 259 | if doc is None: |
---|
| 260 | impl = getDOMImplementation() |
---|
[78a205a] | 261 | |
---|
| 262 | doc_type = impl.createDocumentType(INVNODE_NAME, "1.0", "1.0") |
---|
| 263 | |
---|
[51f14603] | 264 | newdoc = impl.createDocument(None, INVNODE_NAME, doc_type) |
---|
| 265 | top_element = newdoc.documentElement |
---|
| 266 | else: |
---|
| 267 | # We are appending to an existing document |
---|
| 268 | newdoc = doc |
---|
| 269 | top_element = newdoc.createElement(INVNODE_NAME) |
---|
| 270 | if entry_node is None: |
---|
| 271 | newdoc.documentElement.appendChild(top_element) |
---|
| 272 | else: |
---|
| 273 | entry_node.appendChild(top_element) |
---|
[78a205a] | 274 | |
---|
[51f14603] | 275 | attr = newdoc.createAttribute("version") |
---|
| 276 | attr.nodeValue = '1.0' |
---|
| 277 | top_element.setAttributeNode(attr) |
---|
[78a205a] | 278 | |
---|
[51f14603] | 279 | # File name |
---|
| 280 | element = newdoc.createElement("filename") |
---|
| 281 | if self.file != None and self.file != '': |
---|
| 282 | element.appendChild(newdoc.createTextNode(str(self.file))) |
---|
| 283 | else: |
---|
| 284 | element.appendChild(newdoc.createTextNode(str(file))) |
---|
| 285 | top_element.appendChild(element) |
---|
[78a205a] | 286 | |
---|
[51f14603] | 287 | element = newdoc.createElement("timestamp") |
---|
| 288 | element.appendChild(newdoc.createTextNode(time.ctime(timestamp))) |
---|
| 289 | attr = newdoc.createAttribute("epoch") |
---|
| 290 | attr.nodeValue = str(timestamp) |
---|
| 291 | element.setAttributeNode(attr) |
---|
| 292 | top_element.appendChild(element) |
---|
[78a205a] | 293 | |
---|
[51f14603] | 294 | # Current state |
---|
| 295 | state = newdoc.createElement("state") |
---|
| 296 | top_element.appendChild(state) |
---|
[78a205a] | 297 | |
---|
| 298 | for name, value in self.saved_state.iteritems(): |
---|
[51f14603] | 299 | element = newdoc.createElement(str(name)) |
---|
| 300 | element.appendChild(newdoc.createTextNode(str(value))) |
---|
| 301 | state.appendChild(element) |
---|
[78a205a] | 302 | |
---|
[51f14603] | 303 | # State history list |
---|
| 304 | history = newdoc.createElement("history") |
---|
| 305 | top_element.appendChild(history) |
---|
[78a205a] | 306 | |
---|
[51f14603] | 307 | for name, value in self.state_list.iteritems(): |
---|
| 308 | history_element = newdoc.createElement('state_' + str(name)) |
---|
[78a205a] | 309 | for state_name, state_value in value.iteritems(): |
---|
[51f14603] | 310 | state_element = newdoc.createElement(str(state_name)) |
---|
| 311 | child = newdoc.createTextNode(str(state_value)) |
---|
| 312 | state_element.appendChild(child) |
---|
| 313 | history_element.appendChild(state_element) |
---|
[78a205a] | 314 | # history_element.appendChild(state_list_element) |
---|
[51f14603] | 315 | history.appendChild(history_element) |
---|
| 316 | |
---|
| 317 | # Bookmarks bookmark_list[self.bookmark_num] = [\ |
---|
[78a205a] | 318 | # my_time,date,state,comp_state] |
---|
[51f14603] | 319 | bookmark = newdoc.createElement("bookmark") |
---|
| 320 | top_element.appendChild(bookmark) |
---|
[78a205a] | 321 | item_list = ['time', 'date', 'state', 'comp_state'] |
---|
[51f14603] | 322 | for name, value_list in self.bookmark_list.iteritems(): |
---|
[78a205a] | 323 | element = newdoc.createElement('mark_' + str(name)) |
---|
| 324 | _, date, state, comp_state = value_list |
---|
[51f14603] | 325 | time_element = newdoc.createElement('time') |
---|
| 326 | time_element.appendChild(newdoc.createTextNode(str(value_list[0]))) |
---|
| 327 | date_element = newdoc.createElement('date') |
---|
| 328 | date_element.appendChild(newdoc.createTextNode(str(value_list[1]))) |
---|
| 329 | state_list_element = newdoc.createElement('state') |
---|
| 330 | comp_state_list_element = newdoc.createElement('comp_state') |
---|
| 331 | for state_name, state_value in value_list[2].iteritems(): |
---|
| 332 | state_element = newdoc.createElement(str(state_name)) |
---|
| 333 | child = newdoc.createTextNode(str(state_value)) |
---|
| 334 | state_element.appendChild(child) |
---|
| 335 | state_list_element.appendChild(state_element) |
---|
| 336 | for comp_name, comp_value in value_list[3].iteritems(): |
---|
| 337 | comp_element = newdoc.createElement(str(comp_name)) |
---|
| 338 | comp_element.appendChild(newdoc.createTextNode(str(comp_value))) |
---|
| 339 | comp_state_list_element.appendChild(comp_element) |
---|
| 340 | element.appendChild(time_element) |
---|
| 341 | element.appendChild(date_element) |
---|
| 342 | element.appendChild(state_list_element) |
---|
| 343 | element.appendChild(comp_state_list_element) |
---|
| 344 | bookmark.appendChild(element) |
---|
| 345 | |
---|
| 346 | # Save the file |
---|
| 347 | if doc is None: |
---|
| 348 | fd = open('test000', 'w') |
---|
| 349 | fd.write(newdoc.toprettyxml()) |
---|
| 350 | fd.close() |
---|
| 351 | return None |
---|
| 352 | else: |
---|
[92a2ecd] | 353 | return newdoc |
---|
[78a205a] | 354 | |
---|
[51f14603] | 355 | def fromXML(self, file=None, node=None): |
---|
| 356 | """ |
---|
| 357 | Load invariant states from a file |
---|
[78a205a] | 358 | |
---|
[51f14603] | 359 | : param file: .inv file |
---|
[78a205a] | 360 | : param node: node of a XML document to read from |
---|
[51f14603] | 361 | """ |
---|
| 362 | if file is not None: |
---|
| 363 | msg = "InvariantSate no longer supports non-CanSAS" |
---|
| 364 | msg += " format for invariant files" |
---|
| 365 | raise RuntimeError, msg |
---|
[78a205a] | 366 | |
---|
[51f14603] | 367 | if node.get('version')\ |
---|
| 368 | and node.get('version') == '1.0': |
---|
| 369 | |
---|
| 370 | # Get file name |
---|
| 371 | entry = get_content('ns:filename', node) |
---|
| 372 | if entry is not None: |
---|
| 373 | file_name = entry.text.strip() |
---|
| 374 | |
---|
| 375 | # Get time stamp |
---|
| 376 | entry = get_content('ns:timestamp', node) |
---|
| 377 | if entry is not None and entry.get('epoch'): |
---|
| 378 | try: |
---|
| 379 | timestamp = (entry.get('epoch')) |
---|
| 380 | except: |
---|
| 381 | msg = "InvariantSate.fromXML: Could not read" |
---|
| 382 | msg += " timestamp\n %s" % sys.exc_value |
---|
| 383 | logging.error(msg) |
---|
[78a205a] | 384 | |
---|
[51f14603] | 385 | # Parse bookmarks |
---|
| 386 | entry_bookmark = get_content('ns:bookmark', node) |
---|
| 387 | |
---|
| 388 | for ind in range(1, len(entry_bookmark) + 1): |
---|
| 389 | temp_state = {} |
---|
| 390 | temp_bookmark = {} |
---|
[78a205a] | 391 | entry = get_content('ns:mark_%s' % ind, entry_bookmark) |
---|
| 392 | |
---|
[51f14603] | 393 | if entry is not None: |
---|
| 394 | my_time = get_content('ns:time', entry) |
---|
| 395 | val_time = str(my_time.text.strip()) |
---|
| 396 | date = get_content('ns:date', entry) |
---|
| 397 | val_date = str(date.text.strip()) |
---|
| 398 | state_entry = get_content('ns:state', entry) |
---|
[78a205a] | 399 | for item in DEFAULT_STATE: |
---|
[51f14603] | 400 | input_field = get_content('ns:%s' % item, state_entry) |
---|
| 401 | val = str(input_field.text.strip()) |
---|
| 402 | if input_field is not None: |
---|
[78a205a] | 403 | temp_state[item] = val |
---|
[51f14603] | 404 | comp_entry = get_content('ns:comp_state', entry) |
---|
[78a205a] | 405 | |
---|
| 406 | for item in DEFAULT_STATE: |
---|
[51f14603] | 407 | input_field = get_content('ns:%s' % item, comp_entry) |
---|
| 408 | val = str(input_field.text.strip()) |
---|
| 409 | if input_field is not None: |
---|
[78a205a] | 410 | temp_bookmark[item] = val |
---|
[51f14603] | 411 | try: |
---|
[78a205a] | 412 | self.bookmark_list[ind] = [val_time, val_date, temp_state, temp_bookmark] |
---|
[51f14603] | 413 | except: |
---|
| 414 | raise "missing components of bookmarks..." |
---|
| 415 | # Parse histories |
---|
| 416 | entry_history = get_content('ns:history', node) |
---|
| 417 | |
---|
| 418 | for ind in range(0, len(entry_history)): |
---|
| 419 | temp_state = {} |
---|
[78a205a] | 420 | entry = get_content('ns:state_%s' % ind, entry_history) |
---|
[51f14603] | 421 | |
---|
| 422 | if entry is not None: |
---|
[78a205a] | 423 | for item in DEFAULT_STATE: |
---|
| 424 | input_field = get_content('ns:%s' % item, entry) |
---|
[db5294e] | 425 | if input_field.text is not None: |
---|
| 426 | val = str(input_field.text.strip()) |
---|
| 427 | else: |
---|
| 428 | val = '' |
---|
[51f14603] | 429 | if input_field is not None: |
---|
[78a205a] | 430 | temp_state[item] = val |
---|
[cb93b40] | 431 | self.state_list[str(ind)] = temp_state |
---|
[78a205a] | 432 | |
---|
[51f14603] | 433 | # Parse current state (ie, saved_state) |
---|
[78a205a] | 434 | entry = get_content('ns:state', node) |
---|
[51f14603] | 435 | if entry is not None: |
---|
[78a205a] | 436 | for item in DEFAULT_STATE: |
---|
[51f14603] | 437 | input_field = get_content('ns:%s' % item, entry) |
---|
[db5294e] | 438 | if input_field.text is not None: |
---|
| 439 | val = str(input_field.text.strip()) |
---|
| 440 | else: |
---|
| 441 | val = '' |
---|
[51f14603] | 442 | if input_field is not None: |
---|
| 443 | self.set_saved_state(name=item, value=val) |
---|
| 444 | self.file = file_name |
---|
[78a205a] | 445 | |
---|
[51f14603] | 446 | def set_report_string(self): |
---|
| 447 | """ |
---|
[78a205a] | 448 | Get the values (strings) from __str__ for report |
---|
[51f14603] | 449 | """ |
---|
| 450 | strings = self.__str__() |
---|
[78a205a] | 451 | |
---|
[51f14603] | 452 | # default string values |
---|
[78a205a] | 453 | for num in range(1, 19): |
---|
[51f14603] | 454 | exec "s_%s = 'NA'" % str(num) |
---|
| 455 | lines = strings.split('\n') |
---|
| 456 | # get all string values from __str__() |
---|
| 457 | for line in range(0, len(lines)): |
---|
| 458 | if line == 1: |
---|
| 459 | s_1 = lines[1] |
---|
| 460 | elif line == 2: |
---|
| 461 | s_2 = lines[2] |
---|
| 462 | else: |
---|
| 463 | item = lines[line].split(':') |
---|
| 464 | item[0] = item[0].strip() |
---|
| 465 | if item[0] == "scale": |
---|
| 466 | s_3 = item[1] |
---|
| 467 | elif item[0] == "porod constant": |
---|
| 468 | s_4 = item[1] |
---|
| 469 | elif item[0] == "background": |
---|
| 470 | s_5 = item[1] |
---|
| 471 | elif item[0] == "contrast": |
---|
| 472 | s_6 = item[1] |
---|
| 473 | elif item[0] == "Extrapolation": |
---|
| 474 | extra = item[1].split(";") |
---|
| 475 | bool_0 = extra[0].split("=") |
---|
| 476 | bool_1 = extra[1].split("=") |
---|
| 477 | s_8 = " " + bool_0[0] + "Q region = " + bool_0[1] |
---|
| 478 | s_7 = " " + bool_1[0] + "Q region = " + bool_1[1] |
---|
| 479 | elif item[0] == "npts low": |
---|
| 480 | s_9 = item[1] |
---|
| 481 | elif item[0] == "npts high": |
---|
| 482 | s_10 = item[1] |
---|
| 483 | elif item[0] == "volume fraction": |
---|
[78a205a] | 484 | val = item[1].split("+-")[0].strip() |
---|
[51f14603] | 485 | error = item[1].split("+-")[1].strip() |
---|
| 486 | s_17 = val + " ± " + error |
---|
| 487 | elif item[0] == "specific surface": |
---|
| 488 | val = item[1].split("+-")[0].strip() |
---|
| 489 | error = item[1].split("+-")[1].strip() |
---|
| 490 | s_18 = val + " ± " + error |
---|
| 491 | elif item[0].split("(")[0].strip() == "power low": |
---|
[78a205a] | 492 | s_11 = item[0] + " =" + item[1] |
---|
[51f14603] | 493 | elif item[0].split("(")[0].strip() == "power high": |
---|
[78a205a] | 494 | s_12 = item[0] + " =" + item[1] |
---|
[51f14603] | 495 | elif item[0].split("[")[0].strip() == "Q* from low Q extrapolation": |
---|
[78a205a] | 496 | # looks messy but this way the symbols +_ and % work on html |
---|
[51f14603] | 497 | val = item[1].split("+-")[0].strip() |
---|
| 498 | error = item[1].split("+-")[1].strip() |
---|
| 499 | err = error.split("%")[0].strip() |
---|
| 500 | percent = error.split("%")[1].strip() |
---|
| 501 | s_13 = val + " ± " + err + "%" + percent |
---|
| 502 | elif item[0].split("[")[0].strip() == "Q* from data": |
---|
| 503 | val = item[1].split("+-")[0].strip() |
---|
| 504 | error = item[1].split("+-")[1].strip() |
---|
| 505 | err = error.split("%")[0].strip() |
---|
| 506 | percent = error.split("%")[1].strip() |
---|
| 507 | s_14 = val + " ± " + err + "%" + percent |
---|
| 508 | elif item[0].split("[")[0].strip() == "Q* from high Q extrapolation": |
---|
| 509 | val = item[1].split("+-")[0].strip() |
---|
| 510 | error = item[1].split("+-")[1].strip() |
---|
| 511 | err = error.split("%")[0].strip() |
---|
| 512 | percent = error.split("%")[1].strip() |
---|
| 513 | s_15 = val + " ± " + err + "%" + percent |
---|
| 514 | elif item[0].split("[")[0].strip() == "total Q*": |
---|
| 515 | val = item[1].split("+-")[0].strip() |
---|
| 516 | error = item[1].split("+-")[1].strip() |
---|
| 517 | s_16 = val + " ± " + error |
---|
| 518 | else: |
---|
| 519 | continue |
---|
| 520 | |
---|
| 521 | s_1 = self._check_html_format(s_1) |
---|
| 522 | file_name = self._check_html_format(self.file) |
---|
| 523 | |
---|
| 524 | # make plot image |
---|
[78a205a] | 525 | self.set_plot_state(extra_high=bool_0[1], extra_low=bool_1[1]) |
---|
[51f14603] | 526 | # get ready for report with setting all the html strings |
---|
| 527 | self.report_str = str(self.template_str) % (s_1, s_2, |
---|
[78a205a] | 528 | s_3, s_4, s_5, s_6, s_7, s_8, |
---|
| 529 | s_9, s_10, s_11, s_12, s_13, s_14, s_15, |
---|
| 530 | s_16, s_17, s_18, file_name, "%s") |
---|
[51f14603] | 531 | |
---|
| 532 | def _check_html_format(self, name): |
---|
| 533 | """ |
---|
| 534 | Check string '%' for html format |
---|
| 535 | """ |
---|
| 536 | if name.count('%'): |
---|
| 537 | name = name.replace('%', '%') |
---|
[78a205a] | 538 | |
---|
[51f14603] | 539 | return name |
---|
[78a205a] | 540 | |
---|
[51f14603] | 541 | def set_saved_state(self, name, value): |
---|
| 542 | """ |
---|
[78a205a] | 543 | Set the state list |
---|
| 544 | |
---|
[51f14603] | 545 | : param name: name of the state component |
---|
| 546 | : param value: value of the state component |
---|
| 547 | """ |
---|
[78a205a] | 548 | rb_list = [['power_law_low', 'guinier'], |
---|
| 549 | ['fit_enable_low', 'fix_enable_low'], |
---|
| 550 | ['fit_enable_high', 'fix_enable_high']] |
---|
[51f14603] | 551 | |
---|
[78a205a] | 552 | self.name = value |
---|
| 553 | self.saved_state[name] = value |
---|
| 554 | # set the count part of radio button clicked |
---|
| 555 | # False for the saved_state |
---|
| 556 | for title, content in rb_list: |
---|
| 557 | if name == title: |
---|
| 558 | name = content |
---|
| 559 | value = False |
---|
| 560 | elif name == content: |
---|
| 561 | name = title |
---|
| 562 | value = False |
---|
| 563 | self.saved_state[name] = value |
---|
| 564 | self.state_num = self.saved_state['state_num'] |
---|
[51f14603] | 565 | |
---|
| 566 | def set_plot_state(self, extra_high=False, extra_low=False): |
---|
| 567 | """ |
---|
| 568 | Build image state that wx.html understand |
---|
| 569 | by plotting, putting it into wx.FileSystem image object |
---|
[78a205a] | 570 | |
---|
[51f14603] | 571 | : extrap_high,extra_low: low/high extrapolations |
---|
| 572 | are possible extra-plots |
---|
| 573 | """ |
---|
| 574 | # some imports |
---|
| 575 | import wx |
---|
| 576 | import matplotlib.pyplot as plt |
---|
| 577 | from matplotlib.backends.backend_agg import FigureCanvasAgg |
---|
| 578 | |
---|
[78a205a] | 579 | # we use simple plot, not plotpanel |
---|
| 580 | # make matlab figure |
---|
[51f14603] | 581 | fig = plt.figure() |
---|
| 582 | fig.set_facecolor('w') |
---|
| 583 | graph = fig.add_subplot(111) |
---|
| 584 | |
---|
[78a205a] | 585 | # data plot |
---|
[51f14603] | 586 | graph.errorbar(self.data.x, self.data.y, yerr=self.data.dy, fmt='o') |
---|
[78a205a] | 587 | # low Q extrapolation fit plot |
---|
[51f14603] | 588 | if not extra_low == 'False': |
---|
[78a205a] | 589 | graph.plot(self.theory_lowQ.x, self.theory_lowQ.y) |
---|
| 590 | # high Q extrapolation fit plot |
---|
[51f14603] | 591 | if not extra_high == 'False': |
---|
[78a205a] | 592 | graph.plot(self.theory_highQ.x, self.theory_highQ.y) |
---|
[51f14603] | 593 | graph.set_xscale("log", nonposx='clip') |
---|
| 594 | graph.set_yscale("log", nonposy='clip') |
---|
[78a205a] | 595 | graph.set_xlabel('$\\rm{Q}(\\AA^{-1})$', fontsize=12) |
---|
| 596 | graph.set_ylabel('$\\rm{Intensity}(cm^{-1})$', fontsize=12) |
---|
[51f14603] | 597 | canvas = FigureCanvasAgg(fig) |
---|
[78a205a] | 598 | # actually make image |
---|
[51f14603] | 599 | canvas.draw() |
---|
[78a205a] | 600 | |
---|
| 601 | # make python.Image object |
---|
| 602 | # size |
---|
[51f14603] | 603 | w, h = canvas.get_width_height() |
---|
[78a205a] | 604 | # convert to wx.Image |
---|
| 605 | wximg = wx.EmptyImage(w, h) |
---|
| 606 | # wxim.SetData(img.convert('RGB').tostring() ) |
---|
| 607 | wximg.SetData(canvas.tostring_rgb()) |
---|
| 608 | # get the dynamic image for the htmlwindow |
---|
[51f14603] | 609 | wximgbmp = wx.BitmapFromImage(wximg) |
---|
[78a205a] | 610 | # store the image in wx.FileSystem Object |
---|
[51f14603] | 611 | wx.FileSystem.AddHandler(wx.MemoryFSHandler()) |
---|
| 612 | # use wx.MemoryFSHandler |
---|
| 613 | self.imgRAM = wx.MemoryFSHandler() |
---|
[78a205a] | 614 | # AddFile, image can be retrieved with 'memory:filename' |
---|
[51f14603] | 615 | self.imgRAM.AddFile('img_inv.png', wximgbmp, wx.BITMAP_TYPE_PNG) |
---|
[78a205a] | 616 | |
---|
[51f14603] | 617 | self.wximgbmp = 'memory:img_inv.png' |
---|
| 618 | self.image = fig |
---|
| 619 | |
---|
| 620 | class Reader(CansasReader): |
---|
| 621 | """ |
---|
| 622 | Class to load a .inv invariant file |
---|
| 623 | """ |
---|
[78a205a] | 624 | # # File type |
---|
[51f14603] | 625 | type_name = "Invariant" |
---|
[78a205a] | 626 | |
---|
| 627 | # # Wildcards |
---|
[51f14603] | 628 | type = ["Invariant file (*.inv)|*.inv", |
---|
[b9a5f0e] | 629 | "SASView file (*.svs)|*.svs"] |
---|
[78a205a] | 630 | # # List of allowed extensions |
---|
| 631 | ext = ['.inv', '.INV', '.svs', 'SVS'] |
---|
| 632 | |
---|
[51f14603] | 633 | def __init__(self, call_back, cansas=True): |
---|
| 634 | """ |
---|
| 635 | Initialize the call-back method to be called |
---|
| 636 | after we load a file |
---|
[78a205a] | 637 | |
---|
[51f14603] | 638 | : param call_back: call-back method |
---|
| 639 | : param cansas: True = files will be written/read in CanSAS format |
---|
[78a205a] | 640 | False = write CanSAS format |
---|
[51f14603] | 641 | """ |
---|
[78a205a] | 642 | # # Call back method to be executed after a file is read |
---|
[51f14603] | 643 | self.call_back = call_back |
---|
[78a205a] | 644 | # # CanSAS format flag |
---|
[51f14603] | 645 | self.cansas = cansas |
---|
| 646 | self.state = None |
---|
| 647 | |
---|
| 648 | def read(self, path): |
---|
[78a205a] | 649 | """ |
---|
[51f14603] | 650 | Load a new invariant state from file |
---|
[78a205a] | 651 | |
---|
[51f14603] | 652 | : param path: file path |
---|
| 653 | : return: None |
---|
| 654 | """ |
---|
| 655 | if self.cansas == True: |
---|
| 656 | return self._read_cansas(path) |
---|
| 657 | else: |
---|
| 658 | return self._read_standalone(path) |
---|
[78a205a] | 659 | |
---|
[51f14603] | 660 | def _read_standalone(self, path): |
---|
[78a205a] | 661 | """ |
---|
[51f14603] | 662 | Load a new invariant state from file. |
---|
| 663 | The invariant node is assumed to be the top element. |
---|
[78a205a] | 664 | |
---|
[51f14603] | 665 | : param path: file path |
---|
| 666 | : return: None |
---|
| 667 | """ |
---|
| 668 | # Read the new state from file |
---|
| 669 | state = InvariantState() |
---|
| 670 | |
---|
| 671 | state.fromXML(file=path) |
---|
[78a205a] | 672 | |
---|
[51f14603] | 673 | # Call back to post the new state |
---|
| 674 | self.call_back(state) |
---|
| 675 | return None |
---|
[78a205a] | 676 | |
---|
[51f14603] | 677 | def _parse_state(self, entry): |
---|
| 678 | """ |
---|
| 679 | Read an invariant result from an XML node |
---|
[78a205a] | 680 | |
---|
| 681 | : param entry: XML node to read from |
---|
[51f14603] | 682 | : return: InvariantState object |
---|
| 683 | """ |
---|
| 684 | state = None |
---|
| 685 | # Locate the invariant node |
---|
| 686 | try: |
---|
| 687 | nodes = entry.xpath('ns:%s' % INVNODE_NAME, |
---|
| 688 | namespaces={'ns': CANSAS_NS}) |
---|
| 689 | # Create an empty state |
---|
| 690 | if nodes != []: |
---|
| 691 | state = InvariantState() |
---|
| 692 | state.fromXML(node=nodes[0]) |
---|
| 693 | except: |
---|
| 694 | msg = "XML document does not contain invariant" |
---|
| 695 | msg += " information.\n %s" % sys.exc_value |
---|
[78a205a] | 696 | logging.info(msg) |
---|
[51f14603] | 697 | return state |
---|
[78a205a] | 698 | |
---|
[51f14603] | 699 | def _read_cansas(self, path): |
---|
[78a205a] | 700 | """ |
---|
[51f14603] | 701 | Load data and invariant information from a CanSAS XML file. |
---|
[78a205a] | 702 | |
---|
[51f14603] | 703 | : param path: file path |
---|
[78a205a] | 704 | : return: Data1D object if a single SASentry was found, |
---|
[51f14603] | 705 | or a list of Data1D objects if multiple entries were found, |
---|
| 706 | or None of nothing was found |
---|
| 707 | : raise RuntimeError: when the file can't be opened |
---|
| 708 | : raise ValueError: when the length of the data vectors are inconsistent |
---|
| 709 | """ |
---|
| 710 | output = [] |
---|
| 711 | if os.path.isfile(path): |
---|
[78a205a] | 712 | basename = os.path.basename(path) |
---|
[51f14603] | 713 | root, extension = os.path.splitext(basename) |
---|
[78a205a] | 714 | |
---|
[51f14603] | 715 | if extension.lower() in self.ext or \ |
---|
| 716 | extension.lower() == '.xml': |
---|
| 717 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
[78a205a] | 718 | |
---|
[51f14603] | 719 | # Check the format version number |
---|
[78a205a] | 720 | # Specifying the namespace will take care of |
---|
| 721 | # the file format version |
---|
[51f14603] | 722 | root = tree.getroot() |
---|
[78a205a] | 723 | |
---|
[51f14603] | 724 | entry_list = root.xpath('/ns:SASroot/ns:SASentry', |
---|
| 725 | namespaces={'ns': CANSAS_NS}) |
---|
[78a205a] | 726 | |
---|
[51f14603] | 727 | for entry in entry_list: |
---|
[78a205a] | 728 | |
---|
[92a2ecd] | 729 | sas_entry, _ = self._parse_entry(entry) |
---|
[51f14603] | 730 | invstate = self._parse_state(entry) |
---|
[78a205a] | 731 | |
---|
| 732 | # invstate could be None when .svs file is loaded |
---|
| 733 | # in this case, skip appending to output |
---|
[51f14603] | 734 | if invstate != None: |
---|
| 735 | sas_entry.meta_data['invstate'] = invstate |
---|
| 736 | sas_entry.filename = invstate.file |
---|
| 737 | output.append(sas_entry) |
---|
| 738 | else: |
---|
| 739 | raise RuntimeError, "%s is not a file" % path |
---|
| 740 | |
---|
| 741 | # Return output consistent with the loader's api |
---|
| 742 | if len(output) == 0: |
---|
| 743 | return None |
---|
| 744 | elif len(output) == 1: |
---|
| 745 | # Call back to post the new state |
---|
| 746 | self.state = output[0].meta_data['invstate'] |
---|
| 747 | self.call_back(state=output[0].meta_data['invstate'], |
---|
[78a205a] | 748 | datainfo=output[0]) |
---|
[51f14603] | 749 | return output[0] |
---|
| 750 | else: |
---|
[78a205a] | 751 | return output |
---|
| 752 | |
---|
[51f14603] | 753 | def get_state(self): |
---|
| 754 | return self.state |
---|
[78a205a] | 755 | |
---|
[51f14603] | 756 | def write(self, filename, datainfo=None, invstate=None): |
---|
| 757 | """ |
---|
| 758 | Write the content of a Data1D as a CanSAS XML file |
---|
[78a205a] | 759 | |
---|
[51f14603] | 760 | : param filename: name of the file to write |
---|
| 761 | : param datainfo: Data1D object |
---|
| 762 | : param invstate: InvariantState object |
---|
| 763 | """ |
---|
| 764 | # Sanity check |
---|
| 765 | if self.cansas == True: |
---|
[78a205a] | 766 | doc = self.write_toXML(datainfo, invstate) |
---|
[51f14603] | 767 | # Write the XML document |
---|
| 768 | fd = open(filename, 'w') |
---|
| 769 | fd.write(doc.toprettyxml()) |
---|
| 770 | fd.close() |
---|
| 771 | else: |
---|
| 772 | invstate.toXML(file=filename) |
---|
[78a205a] | 773 | |
---|
[51f14603] | 774 | def write_toXML(self, datainfo=None, state=None): |
---|
| 775 | """ |
---|
| 776 | Write toXML, a helper for write() |
---|
[78a205a] | 777 | |
---|
[51f14603] | 778 | : return: xml doc |
---|
| 779 | """ |
---|
| 780 | if datainfo is None: |
---|
[c10d9d6c] | 781 | datainfo = sas.sascalc.dataloader.data_info.Data1D(x=[], y=[]) |
---|
| 782 | elif not issubclass(datainfo.__class__, sas.sascalc.dataloader.data_info.Data1D): |
---|
[51f14603] | 783 | msg = "The cansas writer expects a Data1D" |
---|
| 784 | msg += " instance: %s" % str(datainfo.__class__.__name__) |
---|
| 785 | raise RuntimeError, msg |
---|
[78a205a] | 786 | # make sure title and data run is filled up. |
---|
[51f14603] | 787 | if datainfo.title == None or datainfo.title == '': |
---|
| 788 | datainfo.title = datainfo.name |
---|
[78a205a] | 789 | if datainfo.run_name == None or datainfo.run_name == {}: |
---|
[51f14603] | 790 | datainfo.run = [str(datainfo.name)] |
---|
| 791 | datainfo.run_name[0] = datainfo.name |
---|
| 792 | # Create basic XML document |
---|
| 793 | doc, sasentry = self._to_xml_doc(datainfo) |
---|
| 794 | # Add the invariant information to the XML document |
---|
| 795 | if state is not None: |
---|
[78a205a] | 796 | doc = state.toXML(datainfo.name, doc=doc, entry_node=sasentry) |
---|
[51f14603] | 797 | return doc |
---|