[f32d144] | 1 | """ |
---|
| 2 | Class that holds a fit page state |
---|
| 3 | """ |
---|
[5062bbf] | 4 | ################################################################################ |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[f32d144] | 7 | #project funded by the US National Science Foundation. |
---|
[5062bbf] | 8 | # |
---|
| 9 | #See the license text in license.txt |
---|
| 10 | # |
---|
| 11 | #copyright 2009, University of Tennessee |
---|
| 12 | ################################################################################ |
---|
[11a7e11] | 13 | import time |
---|
| 14 | import os |
---|
| 15 | import sys |
---|
[6f023e8] | 16 | import copy |
---|
[11a7e11] | 17 | import logging |
---|
[df7ed14] | 18 | import numpy |
---|
[eddb6ec] | 19 | import string |
---|
[c77d859] | 20 | |
---|
[35b556d] | 21 | import xml.dom.minidom |
---|
[ac5b69d] | 22 | from xml.dom.minidom import parseString |
---|
[11a7e11] | 23 | from lxml import etree |
---|
| 24 | |
---|
[79492222] | 25 | import sas.dataloader |
---|
| 26 | from sas.dataloader.readers.cansas_reader import Reader as CansasReader |
---|
| 27 | from sas.dataloader.readers.cansas_reader import get_content, write_node |
---|
| 28 | from sas.dataloader.data_info import Data2D |
---|
| 29 | from sas.dataloader.data_info import Collimation |
---|
| 30 | from sas.dataloader.data_info import Detector |
---|
| 31 | from sas.dataloader.data_info import Process |
---|
| 32 | from sas.dataloader.data_info import Aperture |
---|
[11a7e11] | 33 | #Information to read/write state as xml |
---|
[61cada5] | 34 | FITTING_NODE_NAME = 'fitting_plug_in' |
---|
| 35 | CANSAS_NS = "cansas1d/1.0" |
---|
| 36 | |
---|
[26f3dd5] | 37 | list_of_data_attributes = [["is_data", "is_data", "bool"], |
---|
[61cada5] | 38 | ["group_id", "data_group_id", "string"], |
---|
| 39 | ["data_name", "data_name", "string"], |
---|
| 40 | ["data_id", "data_id", "string"], |
---|
| 41 | ["name", "name", "string"], |
---|
[26f3dd5] | 42 | ["data_name", "data_name", "string"]] |
---|
[e3d1423] | 43 | list_of_state_attributes = [["engine_type", "engine_type", "string"], |
---|
| 44 | ["qmin", "qmin", "float"], |
---|
[61cada5] | 45 | ["qmax", "qmax", "float"], |
---|
| 46 | ["npts", "npts", "float"], |
---|
[ea5fa58] | 47 | #["shape_rbutton", "shape_rbutton", "bool"], |
---|
| 48 | #["shape_indep_rbutton", "shape_indep_rbutton", "bool"], |
---|
| 49 | #["plugin_rbutton", "plugin_rbutton", "bool"], |
---|
| 50 | #["struct_rbutton", "struct_rbutton", "bool"], |
---|
| 51 | ["categorycombobox", "categorycombobox", "string"], |
---|
[d40038e] | 52 | ["formfactorcombobox", "formfactorcombobox", "string"], |
---|
| 53 | ["structurecombobox", "structurecombobox", "string"], |
---|
[c0ff8cc] | 54 | ["multi_factor","multi_factor","float"], |
---|
[318b5bbb] | 55 | ["magnetic_on","magnetic_on", "bool"], |
---|
[26f3dd5] | 56 | ["enable_smearer","enable_smearer","bool"], |
---|
| 57 | ["disable_smearer","disable_smearer","bool"], |
---|
| 58 | ["pinhole_smearer","pinhole_smearer","bool"], |
---|
| 59 | ["slit_smearer","slit_smearer","bool"], |
---|
| 60 | ["enable_disp","enable_disp","bool"], |
---|
| 61 | ["disable_disp","disable_disp","bool"], |
---|
[55bb249c] | 62 | ["dI_noweight","dI_noweight","bool"], |
---|
| 63 | ["dI_didata","dI_didata","bool"], |
---|
| 64 | ["dI_sqrdata","dI_sqrdata","bool"], |
---|
| 65 | ["dI_idata","dI_idata","bool"], |
---|
[26f3dd5] | 66 | ["enable2D","enable2D","bool"], |
---|
| 67 | ["cb1","cb1","bool"], |
---|
[5cc7001] | 68 | ["tcChi","tcChi","float"], |
---|
[55bb249c] | 69 | ["smearer", "smearer", "float"], |
---|
| 70 | ["smear_type","smear_type", "string"], |
---|
| 71 | ["dq_l", "dq_l", "string"], |
---|
[db8fd5b] | 72 | ["dq_r","dq_r", "string"], |
---|
| 73 | ["dx_max", "dx_max", "float"], |
---|
| 74 | ["dx_min","dx_min", "float"], |
---|
| 75 | ["dxl", "dxl", "float"], |
---|
| 76 | ["dxw","dxw", "float"]] |
---|
[5cc7001] | 77 | |
---|
[11a7e11] | 78 | list_of_model_attributes = [["values", "values"], |
---|
| 79 | ["weights", "weights"]] |
---|
| 80 | |
---|
[f32d144] | 81 | list_of_obj_dic = [["disp_obj_dict", "_disp_obj_dict", "string"]] |
---|
[2296316] | 82 | |
---|
[f32d144] | 83 | list_of_state_parameters = [["parameters", "parameters"], |
---|
| 84 | ["str_parameters", "str_parameters"], |
---|
[61cada5] | 85 | ["orientation_parameters", "orientation_params"], |
---|
| 86 | ["dispersity_parameters", "orientation_params_disp"], |
---|
[f32d144] | 87 | ["fixed_param", "fixed_param"], |
---|
| 88 | ["fittable_param", "fittable_param"]] |
---|
| 89 | list_of_data_2d_attr = [["xmin", "xmin", "float"], |
---|
| 90 | ["xmax", "xmax", "float"], |
---|
| 91 | ["ymin", "ymin", "float"], |
---|
| 92 | ["ymax", "ymax", "float"], |
---|
| 93 | ["_xaxis", "_xaxis", "string"], |
---|
[df7ed14] | 94 | ["_xunit", "_xunit", "string"], |
---|
[f32d144] | 95 | ["_yaxis", "_yaxis", "string"], |
---|
| 96 | ["_yunit", "_yunit", "string"], |
---|
| 97 | ["_zaxis", "_zaxis", "string"], |
---|
| 98 | ["_zunit", "_zunit", "string"]] |
---|
| 99 | list_of_data2d_values = [["qx_data", "qx_data", "float"], |
---|
| 100 | ["qy_data", "qy_data", "float"], |
---|
| 101 | ["dqx_data", "dqx_data", "float"], |
---|
| 102 | ["dqy_data", "dqy_data", "float"], |
---|
| 103 | ["data", "data", "float"], |
---|
| 104 | ["q_data", "q_data", "float"], |
---|
| 105 | ["err_data", "err_data", "float"], |
---|
| 106 | ["mask", "mask", "bool"]] |
---|
[61cada5] | 107 | |
---|
[f32d144] | 108 | |
---|
| 109 | def parse_entry_helper(node, item): |
---|
[0b12abb5] | 110 | """ |
---|
| 111 | Create a numpy list from value extrated from the node |
---|
| 112 | |
---|
| 113 | :param node: node from each the value is stored |
---|
| 114 | :param item: list name of three strings.the two first are name of data |
---|
[f32d144] | 115 | attribute and the third one is the type of the value of that |
---|
[0b12abb5] | 116 | attribute. type can be string, float, bool, etc. |
---|
| 117 | |
---|
| 118 | : return: numpy array |
---|
| 119 | """ |
---|
| 120 | if node is not None: |
---|
| 121 | if item[2] == "string": |
---|
| 122 | return str(node.get(item[0]).strip()) |
---|
| 123 | elif item[2] == "bool": |
---|
| 124 | try: |
---|
[e3d1423] | 125 | return node.get(item[0]).strip() == "True" |
---|
| 126 | |
---|
[0b12abb5] | 127 | except: |
---|
| 128 | return None |
---|
| 129 | else: |
---|
| 130 | try: |
---|
| 131 | return float(node.get(item[0])) |
---|
| 132 | except: |
---|
| 133 | return None |
---|
| 134 | |
---|
| 135 | |
---|
[cfc0913] | 136 | class PageState(object): |
---|
[c77d859] | 137 | """ |
---|
[5062bbf] | 138 | Contains information to reconstruct a page of the fitpanel. |
---|
[c77d859] | 139 | """ |
---|
[61cada5] | 140 | def __init__(self, parent=None, model=None, data=None): |
---|
[f32d144] | 141 | """ |
---|
[5062bbf] | 142 | Initialize the current state |
---|
| 143 | |
---|
| 144 | :param model: a selected model within a page |
---|
[f32d144] | 145 | :param data: |
---|
[5062bbf] | 146 | |
---|
[c77d859] | 147 | """ |
---|
[61cada5] | 148 | self.file = None |
---|
[11a7e11] | 149 | #Time of state creation |
---|
| 150 | self.timestamp = time.time() |
---|
[c77d859] | 151 | ## Data member to store the dispersion object created |
---|
| 152 | self._disp_obj_dict = {} |
---|
[61cada5] | 153 | #------------------------ |
---|
[f32d144] | 154 | #Data used for fitting |
---|
[c77d859] | 155 | self.data = data |
---|
[2296316] | 156 | # model data |
---|
| 157 | self.theory_data = None |
---|
| 158 | #Is 2D |
---|
| 159 | self.is_2D = False |
---|
| 160 | self.images = None |
---|
| 161 | |
---|
[61cada5] | 162 | #save additional information on data that dataloader.reader does not read |
---|
| 163 | self.is_data = None |
---|
| 164 | self.data_name = "" |
---|
[c0ff8cc] | 165 | |
---|
[61cada5] | 166 | if self.data is not None: |
---|
| 167 | self.data_name = self.data.name |
---|
| 168 | self.data_id = None |
---|
| 169 | if self.data is not None and hasattr(self.data, "id"): |
---|
| 170 | self.data_id = self.data.id |
---|
| 171 | self.data_group_id = None |
---|
| 172 | if self.data is not None and hasattr(self.data, "group_id"): |
---|
| 173 | self.data_group_id = self.data.group_id |
---|
[0b12abb5] | 174 | |
---|
[61cada5] | 175 | ## reset True change the state of exsiting button |
---|
| 176 | self.reset = False |
---|
| 177 | |
---|
[c99a6c5] | 178 | #engine type |
---|
| 179 | self.engine_type = None |
---|
[c77d859] | 180 | # flag to allow data2D plot |
---|
[cfc0913] | 181 | self.enable2D = False |
---|
[c77d859] | 182 | # model on which the fit would be performed |
---|
| 183 | self.model = model |
---|
[93f0a862] | 184 | self.m_name = None |
---|
[dce84c0] | 185 | #list of process done to model |
---|
| 186 | self.process = [] |
---|
[f32d144] | 187 | #fit page manager |
---|
[c77d859] | 188 | self.manager = None |
---|
| 189 | #Store the parent of this panel parent |
---|
| 190 | # For this application fitpanel is the parent |
---|
[f32d144] | 191 | self.parent = parent |
---|
[c77d859] | 192 | # Event_owner is the owner of model event |
---|
| 193 | self.event_owner = None |
---|
[c9a4377] | 194 | ##page name |
---|
[cfc0913] | 195 | self.page_name = "" |
---|
[eddb6ec] | 196 | # Contains link between model, all its parameters, and panel organization |
---|
[61cada5] | 197 | self.parameters = [] |
---|
[fb59ed9] | 198 | # String parameter list that can not be fitted |
---|
| 199 | self.str_parameters = [] |
---|
[f32d144] | 200 | # Contains list of parameters that cannot be fitted and reference to |
---|
| 201 | #panel objects |
---|
[61cada5] | 202 | self.fixed_param = [] |
---|
[f32d144] | 203 | # Contains list of parameters with dispersity and reference to |
---|
| 204 | #panel objects |
---|
[61cada5] | 205 | self.fittable_param = [] |
---|
[60132ef] | 206 | ## orientation parameters |
---|
[61cada5] | 207 | self.orientation_params = [] |
---|
[eddb6ec] | 208 | ## orientation parameters for gaussian dispersity |
---|
[61cada5] | 209 | self.orientation_params_disp = [] |
---|
[3370922] | 210 | ## smearer info |
---|
[61cada5] | 211 | self.smearer = None |
---|
[7609f1a] | 212 | self.smear_type = None |
---|
| 213 | self.dq_l = None |
---|
| 214 | self.dq_r = None |
---|
[db8fd5b] | 215 | self.dx_max = None |
---|
| 216 | self.dx_min = None |
---|
| 217 | self.dxl = None |
---|
| 218 | self.dxw = None |
---|
[eddb6ec] | 219 | #list of dispersion parameters |
---|
[f32d144] | 220 | self.disp_list = [] |
---|
[61cada5] | 221 | if self.model is not None: |
---|
[71f0373] | 222 | self.disp_list = self.model.getDispParamList() |
---|
[2296316] | 223 | |
---|
[61cada5] | 224 | self.disp_cb_dict = {} |
---|
[2296316] | 225 | self.values = {} |
---|
| 226 | self.weights = {} |
---|
[dad49a0] | 227 | |
---|
[f32d144] | 228 | #contains link between a model and selected parameters to fit |
---|
[61cada5] | 229 | self.param_toFit = [] |
---|
[a074145] | 230 | ##dictionary of model type and model class |
---|
[cfc0913] | 231 | self.model_list_box = None |
---|
[a074145] | 232 | ## save the state of the context menu |
---|
[61cada5] | 233 | self.saved_states = {} |
---|
[240b9966] | 234 | ## save selection of combobox |
---|
[3b9e023] | 235 | self.formfactorcombobox = None |
---|
[ea5fa58] | 236 | self.categorycombobox = None |
---|
[f32d144] | 237 | self.structurecombobox = None |
---|
[c0ff8cc] | 238 | |
---|
[240b9966] | 239 | ## radio box to select type of model |
---|
[ea5fa58] | 240 | #self.shape_rbutton = False |
---|
| 241 | #self.shape_indep_rbutton = False |
---|
| 242 | #self.struct_rbutton = False |
---|
| 243 | #self.plugin_rbutton = False |
---|
[b787e68c] | 244 | ## the indice of the current selection |
---|
| 245 | self.disp_box = 0 |
---|
[c77d859] | 246 | ## Qrange |
---|
[cfc0913] | 247 | ## Q range |
---|
[61cada5] | 248 | self.qmin = 0.001 |
---|
| 249 | self.qmax = 0.1 |
---|
[0b12abb5] | 250 | #reset data range |
---|
| 251 | self.qmax_x = None |
---|
| 252 | self.qmin_x = None |
---|
| 253 | |
---|
[cfc0913] | 254 | self.npts = None |
---|
[61cada5] | 255 | self.name = "" |
---|
[4523b68] | 256 | self.multi_factor = None |
---|
[318b5bbb] | 257 | self.magnetic_on = False |
---|
[cfc0913] | 258 | ## enable smearering state |
---|
| 259 | self.enable_smearer = False |
---|
[fc6ea43] | 260 | self.disable_smearer = True |
---|
[7609f1a] | 261 | self.pinhole_smearer = False |
---|
[f32d144] | 262 | self.slit_smearer = False |
---|
[55bb249c] | 263 | # weighting options |
---|
| 264 | self.dI_noweight = False |
---|
| 265 | self.dI_didata = True |
---|
| 266 | self.dI_sqrdata = False |
---|
[f32d144] | 267 | self.dI_idata = False |
---|
[cfc0913] | 268 | ## disperity selection |
---|
[61cada5] | 269 | self.enable_disp = False |
---|
| 270 | self.disable_disp = True |
---|
[11a7e11] | 271 | |
---|
[cfc0913] | 272 | ## state of selected all check button |
---|
| 273 | self.cb1 = False |
---|
[0aeabc6] | 274 | ## store value of chisqr |
---|
[61cada5] | 275 | self.tcChi = None |
---|
[240b9966] | 276 | |
---|
[6f023e8] | 277 | def clone(self): |
---|
[61cada5] | 278 | """ |
---|
[5062bbf] | 279 | Create a new copy of the current object |
---|
[61cada5] | 280 | """ |
---|
| 281 | model = None |
---|
| 282 | if self.model is not None: |
---|
[6f023e8] | 283 | model = self.model.clone() |
---|
[bb70474] | 284 | model.name = self.model.name |
---|
[61cada5] | 285 | obj = PageState(self.parent, model=model) |
---|
| 286 | obj.file = copy.deepcopy(self.file) |
---|
[6f023e8] | 287 | obj.data = copy.deepcopy(self.data) |
---|
[61cada5] | 288 | if self.data is not None: |
---|
| 289 | self.data_name = self.data.name |
---|
| 290 | obj.data_name = self.data_name |
---|
| 291 | obj.is_data = self.is_data |
---|
[dcf29d7] | 292 | obj.model_list_box = copy.deepcopy(self.model_list_box) |
---|
[c99a6c5] | 293 | obj.engine_type = copy.deepcopy(self.engine_type) |
---|
[240b9966] | 294 | |
---|
[ea5fa58] | 295 | obj.categorycombobox = self.categorycombobox |
---|
[61cada5] | 296 | obj.formfactorcombobox = self.formfactorcombobox |
---|
[f32d144] | 297 | obj.structurecombobox = self.structurecombobox |
---|
[240b9966] | 298 | |
---|
[ea5fa58] | 299 | #obj.shape_rbutton = self.shape_rbutton |
---|
| 300 | #obj.shape_indep_rbutton = self.shape_indep_rbutton |
---|
| 301 | #obj.struct_rbutton = self.struct_rbutton |
---|
| 302 | #obj.plugin_rbutton = self.plugin_rbutton |
---|
[240b9966] | 303 | |
---|
[dcf29d7] | 304 | obj.manager = self.manager |
---|
| 305 | obj.event_owner = self.event_owner |
---|
[71f0373] | 306 | obj.disp_list = copy.deepcopy(self.disp_list) |
---|
[fc6ea43] | 307 | |
---|
[c477b31] | 308 | obj.enable2D = copy.deepcopy(self.enable2D) |
---|
[cfc0913] | 309 | obj.parameters = copy.deepcopy(self.parameters) |
---|
[fb59ed9] | 310 | obj.str_parameters = copy.deepcopy(self.str_parameters) |
---|
[cfc0913] | 311 | obj.fixed_param = copy.deepcopy(self.fixed_param) |
---|
| 312 | obj.fittable_param = copy.deepcopy(self.fittable_param) |
---|
[f32d144] | 313 | obj.orientation_params = copy.deepcopy(self.orientation_params) |
---|
| 314 | obj.orientation_params_disp = copy.deepcopy(self.orientation_params_disp) |
---|
[b787e68c] | 315 | obj.enable_disp = copy.deepcopy(self.enable_disp) |
---|
[fc6ea43] | 316 | obj.disable_disp = copy.deepcopy(self.disable_disp) |
---|
[0aeabc6] | 317 | obj.tcChi = self.tcChi |
---|
| 318 | |
---|
[f32d144] | 319 | if len(self._disp_obj_dict) > 0: |
---|
| 320 | for k, v in self._disp_obj_dict.iteritems(): |
---|
| 321 | obj._disp_obj_dict[k] = v |
---|
| 322 | if len(self.disp_cb_dict) > 0: |
---|
| 323 | for k, v in self.disp_cb_dict.iteritems(): |
---|
| 324 | obj.disp_cb_dict[k] = v |
---|
| 325 | if len(self.values) > 0: |
---|
| 326 | for k, v in self.values.iteritems(): |
---|
[2296316] | 327 | obj.values[k] = v |
---|
[f32d144] | 328 | if len(self.weights) > 0: |
---|
| 329 | for k, v in self.weights.iteritems(): |
---|
[2296316] | 330 | obj.weights[k] = v |
---|
[b787e68c] | 331 | obj.enable_smearer = copy.deepcopy(self.enable_smearer) |
---|
[fc6ea43] | 332 | obj.disable_smearer = copy.deepcopy(self.disable_smearer) |
---|
[7609f1a] | 333 | obj.pinhole_smearer = copy.deepcopy(self.pinhole_smearer) |
---|
| 334 | obj.slit_smearer = copy.deepcopy(self.slit_smearer) |
---|
| 335 | obj.smear_type = copy.deepcopy(self.smear_type) |
---|
[55bb249c] | 336 | obj.dI_noweight = copy.deepcopy(self.dI_noweight) |
---|
| 337 | obj.dI_didata = copy.deepcopy(self.dI_didata) |
---|
| 338 | obj.dI_sqrdata = copy.deepcopy(self.dI_sqrdata) |
---|
| 339 | obj.dI_idata = copy.deepcopy(self.dI_idata) |
---|
[7609f1a] | 340 | obj.dq_l = copy.deepcopy(self.dq_l) |
---|
| 341 | obj.dq_r = copy.deepcopy(self.dq_r) |
---|
[db8fd5b] | 342 | obj.dx_max = copy.deepcopy(self.dx_max) |
---|
| 343 | obj.dx_min = copy.deepcopy(self.dx_min) |
---|
| 344 | obj.dxl = copy.deepcopy(self.dxl) |
---|
| 345 | obj.dxw = copy.deepcopy(self.dxw) |
---|
[b787e68c] | 346 | obj.disp_box = copy.deepcopy(self.disp_box) |
---|
| 347 | obj.qmin = copy.deepcopy(self.qmin) |
---|
| 348 | obj.qmax = copy.deepcopy(self.qmax) |
---|
[c0ff8cc] | 349 | obj.multi_factor = self.multi_factor |
---|
[318b5bbb] | 350 | obj.magnetic_on = self.magnetic_on |
---|
[f32d144] | 351 | obj.npts = copy.deepcopy(self.npts) |
---|
[b787e68c] | 352 | obj.cb1 = copy.deepcopy(self.cb1) |
---|
[3370922] | 353 | obj.smearer = copy.deepcopy(self.smearer) |
---|
| 354 | |
---|
[a074145] | 355 | for name, state in self.saved_states.iteritems(): |
---|
| 356 | copy_name = copy.deepcopy(name) |
---|
| 357 | copy_state = state.clone() |
---|
[f32d144] | 358 | obj.saved_states[copy_name] = copy_state |
---|
[6f023e8] | 359 | return obj |
---|
[61cada5] | 360 | |
---|
| 361 | def _repr_helper(self, list, rep): |
---|
| 362 | """ |
---|
[5062bbf] | 363 | Helper method to print a state |
---|
[61cada5] | 364 | """ |
---|
| 365 | for item in list: |
---|
[f32d144] | 366 | rep += "parameter name: %s \n" % str(item[1]) |
---|
| 367 | rep += "value: %s\n" % str(item[2]) |
---|
| 368 | rep += "selected: %s\n" % str(item[0]) |
---|
| 369 | rep += "error displayed : %s \n" % str(item[4][0]) |
---|
| 370 | rep += "error value:%s \n" % str(item[4][1]) |
---|
| 371 | rep += "minimum displayed : %s \n" % str(item[5][0]) |
---|
| 372 | rep += "minimum value : %s \n" % str(item[5][1]) |
---|
| 373 | rep += "maximum displayed : %s \n" % str(item[6][0]) |
---|
| 374 | rep += "maximum value : %s \n" % str(item[6][1]) |
---|
| 375 | rep += "parameter unit: %s\n\n" % str(item[7]) |
---|
[240b9966] | 376 | return rep |
---|
[c0ff8cc] | 377 | |
---|
[61cada5] | 378 | def __repr__(self): |
---|
[f32d144] | 379 | """ |
---|
[5062bbf] | 380 | output string for printing |
---|
[11a7e11] | 381 | """ |
---|
[f32d144] | 382 | rep = "\nState name: %s\n" % self.file |
---|
[11a7e11] | 383 | t = time.localtime(self.timestamp) |
---|
[deff488] | 384 | time_str = time.strftime("%b %d %Y %H;%M;%S ", t) |
---|
[c0ff8cc] | 385 | |
---|
[f32d144] | 386 | rep += "State created: %s\n" % time_str |
---|
| 387 | rep += "State form factor combobox selection: %s\n" % self.formfactorcombobox |
---|
| 388 | rep += "State structure factor combobox selection: %s\n" % self.structurecombobox |
---|
| 389 | rep += "is data : %s\n" % self.is_data |
---|
| 390 | rep += "data's name : %s\n" % self.data_name |
---|
| 391 | rep += "data's id : %s\n" % self.data_id |
---|
[74dc0a4] | 392 | if self.model != None: |
---|
[93f0a862] | 393 | m_name = self.model.__class__.__name__ |
---|
| 394 | if m_name == 'Model': |
---|
| 395 | m_name = self.m_name |
---|
[f32d144] | 396 | rep += "model name : %s\n" % m_name |
---|
[74dc0a4] | 397 | else: |
---|
| 398 | rep += "model name : None\n" |
---|
[ea5fa58] | 399 | #rep += "model type (form factor) selected: %s\n" % self.shape_rbutton |
---|
[f32d144] | 400 | rep += "multi_factor : %s\n" % str(self.multi_factor) |
---|
[318b5bbb] | 401 | rep += "magnetic_on : %s\n"% str(self.magnetic_on) |
---|
[ea5fa58] | 402 | rep += "model type (Category) selected: %s\n" % self.categorycombobox |
---|
| 403 | #rep += "model type (shape independent) selected: %s\n" % self.shape_indep_rbutton |
---|
| 404 | #rep += "model type (structure factor) selected: %s\n" % self.struct_rbutton |
---|
| 405 | #rep += "model type (plug-in ) selected: %s\n" % self.plugin_rbutton |
---|
[f32d144] | 406 | rep += "data : %s\n" % str(self.data) |
---|
| 407 | rep += "Plotting Range: min: %s, max: %s, steps: %s\n" % (str(self.qmin), |
---|
| 408 | str(self.qmax), str(self.npts)) |
---|
| 409 | rep += "Dispersion selection : %s\n" % str(self.disp_box) |
---|
| 410 | rep += "Smearing enable : %s\n" % str(self.enable_smearer) |
---|
| 411 | rep += "Smearing disable : %s\n" % str(self.disable_smearer) |
---|
| 412 | rep += "Pinhole smearer enable : %s\n" % str(self.pinhole_smearer) |
---|
| 413 | rep += "Slit smearer enable : %s\n" % str(self.slit_smearer) |
---|
| 414 | rep += "Dispersity enable : %s\n" % str(self.enable_disp) |
---|
| 415 | rep += "Dispersity disable : %s\n" % str(self.disable_disp) |
---|
| 416 | rep += "Slit smearer enable: %s\n" % str(self.slit_smearer) |
---|
| 417 | |
---|
| 418 | rep += "dI_noweight : %s\n" % str(self.dI_noweight) |
---|
| 419 | rep += "dI_didata : %s\n" % str(self.dI_didata) |
---|
| 420 | rep += "dI_sqrdata : %s\n" % str(self.dI_sqrdata) |
---|
| 421 | rep += "dI_idata : %s\n" % str(self.dI_idata) |
---|
| 422 | |
---|
| 423 | rep += "2D enable : %s\n" % str(self.enable2D) |
---|
| 424 | rep += "All parameters checkbox selected: %s\n" % (self.cb1) |
---|
| 425 | rep += "Value of Chisqr : %s\n" % str(self.tcChi) |
---|
| 426 | rep += "Smear object : %s\n" % str(self.smearer) |
---|
| 427 | rep += "Smear type : %s\n" % (self.smear_type) |
---|
| 428 | rep += "dq_l : %s\n" % self.dq_l |
---|
| 429 | rep += "dq_r : %s\n" % self.dq_r |
---|
[db8fd5b] | 430 | rep += "dx_max : %s\n" % str(self.dx_max) |
---|
| 431 | rep += "dx_min : %s\n" % str(self.dx_min) |
---|
| 432 | rep += "dxl : %s\n" % str(self.dxl) |
---|
| 433 | rep += "dxw : %s\n" % str(self.dxw) |
---|
[f32d144] | 434 | rep += "model : %s\n\n" % str(self.model) |
---|
[2296316] | 435 | temp_parameters = [] |
---|
| 436 | temp_fittable_param = [] |
---|
| 437 | if self.data.__class__.__name__ == "Data2D": |
---|
| 438 | self.is_2D = True |
---|
| 439 | else: |
---|
| 440 | self.is_2D = False |
---|
| 441 | if self.data is not None: |
---|
| 442 | if not self.is_2D: |
---|
| 443 | for item in self.parameters: |
---|
| 444 | if not item in self.orientation_params: |
---|
| 445 | temp_parameters.append(item) |
---|
| 446 | for item in self.fittable_param: |
---|
| 447 | if not item in self.orientation_params_disp: |
---|
| 448 | temp_fittable_param.append(item) |
---|
| 449 | else: |
---|
| 450 | temp_parameters = self.parameters |
---|
| 451 | temp_fittable_param = self.fittable_param |
---|
| 452 | |
---|
[f32d144] | 453 | rep += "number parameters(self.parameters): %s\n" % len(temp_parameters) |
---|
| 454 | rep = self._repr_helper(list=temp_parameters, rep=rep) |
---|
| 455 | rep += "number str_parameters(self.str_parameters): %s\n" % len(self.str_parameters) |
---|
| 456 | rep = self._repr_helper(list=self.str_parameters, rep=rep) |
---|
| 457 | rep += "number fittable_param(self.fittable_param): %s\n" % len(temp_fittable_param) |
---|
| 458 | rep = self._repr_helper(list=temp_fittable_param, rep=rep) |
---|
| 459 | """ |
---|
[2296316] | 460 | if is_2D: |
---|
| 461 | rep += "number orientation parameters" |
---|
| 462 | rep += "(self.orientation_params): %s\n"%len(self.orientation_params) |
---|
| 463 | rep = self._repr_helper( list=self.orientation_params, rep=rep) |
---|
| 464 | rep += "number dispersity parameters" |
---|
| 465 | rep += "(self.orientation_params_disp): %s\n"%len(self.orientation_params_disp) |
---|
| 466 | rep = self._repr_helper( list=self.orientation_params_disp, rep=rep) |
---|
| 467 | """ |
---|
[61cada5] | 468 | return rep |
---|
[2296316] | 469 | |
---|
| 470 | def set_report_string(self): |
---|
| 471 | """ |
---|
[f32d144] | 472 | Get the values (strings) from __str__ for report |
---|
[2296316] | 473 | """ |
---|
| 474 | # Dictionary of teh report strings |
---|
| 475 | repo_time = "" |
---|
| 476 | model_name = "" |
---|
| 477 | title = "" |
---|
| 478 | title_name = "" |
---|
| 479 | file_name = "" |
---|
| 480 | param_string = "" |
---|
| 481 | paramval_string = "" |
---|
| 482 | chi2_string = "" |
---|
[c0ff8cc] | 483 | multi_factor_string = "" |
---|
[2296316] | 484 | q_range = "" |
---|
| 485 | strings = self.__repr__() |
---|
| 486 | lines = strings.split('\n') |
---|
| 487 | |
---|
| 488 | # get all string values from __str__() |
---|
| 489 | for line in lines: |
---|
| 490 | value = "" |
---|
| 491 | content = line.split(":") |
---|
| 492 | name = content[0] |
---|
| 493 | try: |
---|
| 494 | value = content[1] |
---|
| 495 | except: |
---|
| 496 | pass |
---|
[b7c6a4a] | 497 | if name.count("State created"): |
---|
| 498 | repo_time = "" + value |
---|
[2296316] | 499 | if name.count("parameter name"): |
---|
| 500 | val_name = value.split(".") |
---|
| 501 | if len(val_name) > 1: |
---|
| 502 | if val_name[1].count("width"): |
---|
| 503 | param_string += value + ',' |
---|
| 504 | else: |
---|
| 505 | continue |
---|
| 506 | else: |
---|
| 507 | param_string += value + ',' |
---|
| 508 | if name == "value": |
---|
| 509 | param_string += value + ',' |
---|
| 510 | if name == "error value": |
---|
| 511 | param_string += value + ',' |
---|
| 512 | if name == "parameter unit": |
---|
[f32d144] | 513 | param_string += value + ':' |
---|
[2296316] | 514 | if name == "Value of Chisqr ": |
---|
| 515 | chi2 = ("Chi2/Npts = " + value) |
---|
| 516 | chi2_string = CENTRE % chi2 |
---|
[c0ff8cc] | 517 | if name == "multi_factor ": |
---|
| 518 | muti_factor = ("muti_factor = " + value) |
---|
| 519 | muti_factor_string = CENTRE % muti_factor |
---|
[318b5bbb] | 520 | if name == "magentic_on ": |
---|
| 521 | magentic_on = ("magentic_on = " + value) |
---|
| 522 | if string(value) == 'True': |
---|
| 523 | magentic_on_string = CENTRE % magentic_on |
---|
[2296316] | 524 | if name == "Title": |
---|
| 525 | if len(value.strip()) == 0: |
---|
| 526 | continue |
---|
| 527 | title = value + " [" + repo_time + "]" |
---|
| 528 | title_name = HEADER % title |
---|
| 529 | if name == "data ": |
---|
| 530 | try: |
---|
| 531 | file = ("File name:" + content[2]) |
---|
| 532 | file_name = CENTRE % file |
---|
| 533 | if len(title) == 0: |
---|
| 534 | title = content[2] + " [" + repo_time + "]" |
---|
| 535 | title_name = HEADER % title |
---|
| 536 | except: |
---|
| 537 | pass |
---|
[74dc0a4] | 538 | if name == "model name ": |
---|
| 539 | try: |
---|
| 540 | modelname = "Model name:" + content[1] |
---|
| 541 | except: |
---|
| 542 | modelname = "Model name:" + " NAN" |
---|
[f32d144] | 543 | model_name = CENTRE % modelname |
---|
[74dc0a4] | 544 | |
---|
[2296316] | 545 | if name == "Plotting Range": |
---|
| 546 | try: |
---|
| 547 | q_range = content[1] + " = " + content[2] \ |
---|
| 548 | + " = " + content[3].split(",")[0] |
---|
| 549 | q_name = ("Q Range: " + q_range) |
---|
| 550 | q_range = CENTRE % q_name |
---|
| 551 | except: |
---|
| 552 | pass |
---|
| 553 | paramval = "" |
---|
[f32d144] | 554 | for lines in param_string.split(":"): |
---|
[2296316] | 555 | line = lines.split(",") |
---|
[f32d144] | 556 | if len(lines) > 0: |
---|
| 557 | param = line[0] |
---|
[2296316] | 558 | param += " = " + line[1] |
---|
| 559 | if len(line[2].split()) > 0 and not line[2].count("None"): |
---|
| 560 | param += " +- " + line[2] |
---|
| 561 | if len(line[3].split()) > 0 and not line[3].count("None"): |
---|
| 562 | param += " " + line[3] |
---|
| 563 | if not paramval.count(param): |
---|
[f32d144] | 564 | paramval += param + "\n" |
---|
[2296316] | 565 | paramval_string += CENTRE % param + "\n" |
---|
[3a3dd9c] | 566 | |
---|
[f5bdb4a] | 567 | text_string = "\n\n\n" + title + "\n\n" + file + \ |
---|
[f32d144] | 568 | "\n" + q_name + \ |
---|
| 569 | "\n" + chi2 + \ |
---|
| 570 | "\n\n" + paramval |
---|
[f5bdb4a] | 571 | |
---|
| 572 | title_name = self._check_html_format(title_name) |
---|
| 573 | file_name = self._check_html_format(file_name) |
---|
| 574 | title = self._check_html_format(title) |
---|
| 575 | |
---|
[2296316] | 576 | html_string = title_name + "\n" + file_name + \ |
---|
[f32d144] | 577 | "\n" + model_name + \ |
---|
[2296316] | 578 | "\n" + q_range + \ |
---|
| 579 | "\n" + chi2_string + \ |
---|
| 580 | "\n" + ELINE + \ |
---|
| 581 | "\n" + paramval_string + \ |
---|
| 582 | "\n" + ELINE + \ |
---|
| 583 | "\n" + FEET_1 % title + \ |
---|
[f32d144] | 584 | "\n" + FEET_2 |
---|
[f5bdb4a] | 585 | |
---|
[2296316] | 586 | return html_string, text_string, title |
---|
| 587 | |
---|
[f5bdb4a] | 588 | def _check_html_format(self, name): |
---|
| 589 | """ |
---|
| 590 | Check string '%' for html format |
---|
| 591 | """ |
---|
| 592 | if name.count('%'): |
---|
| 593 | name = name.replace('%', '%') |
---|
| 594 | |
---|
| 595 | return name |
---|
| 596 | |
---|
[2296316] | 597 | def report(self, figs=None, canvases=None): |
---|
| 598 | """ |
---|
| 599 | Invoke report dialog panel |
---|
| 600 | |
---|
| 601 | : param figs: list of pylab figures [list] |
---|
| 602 | """ |
---|
[79492222] | 603 | from sas.perspectives.fitting.report_dialog import ReportDialog |
---|
[2296316] | 604 | # get the strings for report |
---|
| 605 | html_str, text_str, title = self.set_report_string() |
---|
| 606 | # Allow 2 figures to append |
---|
| 607 | if len(figs) == 1: |
---|
[f32d144] | 608 | add_str = FEET_3 |
---|
[2296316] | 609 | elif len(figs) == 2: |
---|
[f32d144] | 610 | add_str = ELINE |
---|
| 611 | add_str += FEET_2 % ("%s") |
---|
| 612 | add_str += ELINE |
---|
| 613 | add_str += FEET_3 |
---|
[2296316] | 614 | elif len(figs) > 2: |
---|
[f32d144] | 615 | add_str = ELINE |
---|
| 616 | add_str += FEET_2 % ("%s") |
---|
| 617 | add_str += ELINE |
---|
| 618 | add_str += FEET_2 % ("%s") |
---|
| 619 | add_str += ELINE |
---|
| 620 | add_str += FEET_3 |
---|
[2296316] | 621 | else: |
---|
| 622 | add_str = "" |
---|
[c0ff8cc] | 623 | |
---|
[2296316] | 624 | # final report html strings |
---|
| 625 | report_str = html_str % ("%s") + add_str |
---|
| 626 | |
---|
| 627 | # make plot image |
---|
| 628 | images = self.set_plot_state(figs, canvases) |
---|
[f32d144] | 629 | report_list = [report_str, text_str, images] |
---|
[2296316] | 630 | dialog = ReportDialog(report_list, None, -1, "") |
---|
| 631 | dialog.ShowModal() |
---|
[c0ff8cc] | 632 | |
---|
[eddb6ec] | 633 | def _toXML_helper(self, thelist, element, newdoc): |
---|
[61cada5] | 634 | """ |
---|
[5062bbf] | 635 | Helper method to create xml file for saving state |
---|
[61cada5] | 636 | """ |
---|
[eddb6ec] | 637 | for item in thelist: |
---|
[61cada5] | 638 | sub_element = newdoc.createElement('parameter') |
---|
| 639 | sub_element.setAttribute('name', str(item[1])) |
---|
| 640 | sub_element.setAttribute('value', str(item[2])) |
---|
| 641 | sub_element.setAttribute('selected_to_fit', str(item[0])) |
---|
| 642 | sub_element.setAttribute('error_displayed', str(item[4][0])) |
---|
| 643 | sub_element.setAttribute('error_value', str(item[4][1])) |
---|
| 644 | sub_element.setAttribute('minimum_displayed', str(item[5][0])) |
---|
| 645 | sub_element.setAttribute('minimum_value', str(item[5][1])) |
---|
| 646 | sub_element.setAttribute('maximum_displayed', str(item[6][0])) |
---|
| 647 | sub_element.setAttribute('maximum_value', str(item[6][1])) |
---|
| 648 | sub_element.setAttribute('unit', str(item[7])) |
---|
| 649 | element.appendChild(sub_element) |
---|
| 650 | |
---|
| 651 | def toXML(self, file="fitting_state.fitv", doc=None, entry_node=None): |
---|
| 652 | """ |
---|
[5062bbf] | 653 | Writes the state of the InversionControl panel to file, as XML. |
---|
| 654 | |
---|
| 655 | Compatible with standalone writing, or appending to an |
---|
| 656 | already existing XML document. In that case, the XML document |
---|
| 657 | is required. An optional entry node in the XML document may also be given. |
---|
| 658 | |
---|
| 659 | :param file: file to write to |
---|
| 660 | :param doc: XML document object [optional] |
---|
| 661 | :param entry_node: XML node within the XML document at which we will append the data [optional] |
---|
| 662 | |
---|
[61cada5] | 663 | """ |
---|
| 664 | from xml.dom.minidom import getDOMImplementation |
---|
[71f0373] | 665 | |
---|
[61cada5] | 666 | # Check whether we have to write a standalone XML file |
---|
| 667 | if doc is None: |
---|
| 668 | impl = getDOMImplementation() |
---|
[f32d144] | 669 | doc_type = impl.createDocumentType(FITTING_NODE_NAME, "1.0", "1.0") |
---|
[61cada5] | 670 | newdoc = impl.createDocument(None, FITTING_NODE_NAME, doc_type) |
---|
| 671 | top_element = newdoc.documentElement |
---|
| 672 | else: |
---|
| 673 | # We are appending to an existing document |
---|
| 674 | newdoc = doc |
---|
[ac5b69d] | 675 | try: |
---|
| 676 | top_element = newdoc.createElement(FITTING_NODE_NAME) |
---|
| 677 | except: |
---|
| 678 | string = etree.tostring(doc, pretty_print=True) |
---|
| 679 | newdoc = parseString(string) |
---|
| 680 | top_element = newdoc.createElement(FITTING_NODE_NAME) |
---|
[61cada5] | 681 | if entry_node is None: |
---|
| 682 | newdoc.documentElement.appendChild(top_element) |
---|
| 683 | else: |
---|
[ac5b69d] | 684 | try: |
---|
| 685 | entry_node.appendChild(top_element) |
---|
| 686 | except: |
---|
| 687 | node_name = entry_node.tag |
---|
| 688 | node_list = newdoc.getElementsByTagName(node_name) |
---|
| 689 | entry_node = node_list.item(0) |
---|
| 690 | entry_node.appendChild(top_element) |
---|
| 691 | |
---|
[61cada5] | 692 | attr = newdoc.createAttribute("version") |
---|
| 693 | attr.nodeValue = '1.0' |
---|
| 694 | top_element.setAttributeNode(attr) |
---|
[cfc0913] | 695 | |
---|
[61cada5] | 696 | # File name |
---|
| 697 | element = newdoc.createElement("filename") |
---|
| 698 | if self.file is not None: |
---|
| 699 | element.appendChild(newdoc.createTextNode(str(self.file))) |
---|
| 700 | else: |
---|
| 701 | element.appendChild(newdoc.createTextNode(str(file))) |
---|
| 702 | top_element.appendChild(element) |
---|
[11a7e11] | 703 | |
---|
| 704 | element = newdoc.createElement("timestamp") |
---|
| 705 | element.appendChild(newdoc.createTextNode(time.ctime(self.timestamp))) |
---|
| 706 | attr = newdoc.createAttribute("epoch") |
---|
| 707 | attr.nodeValue = str(self.timestamp) |
---|
| 708 | element.setAttributeNode(attr) |
---|
| 709 | top_element.appendChild(element) |
---|
[61cada5] | 710 | # Inputs |
---|
| 711 | inputs = newdoc.createElement("Attributes") |
---|
| 712 | top_element.appendChild(inputs) |
---|
| 713 | |
---|
| 714 | if self.data is not None and hasattr(self.data, "group_id"): |
---|
| 715 | self.data_group_id = self.data.group_id |
---|
| 716 | if self.data is not None and hasattr(self.data, "is_data"): |
---|
| 717 | self.is_data = self.data.is_data |
---|
| 718 | if self.data is not None: |
---|
| 719 | self.data_name = self.data.name |
---|
| 720 | if self.data is not None and hasattr(self.data, "id"): |
---|
| 721 | self.data_id = self.data.id |
---|
| 722 | |
---|
[26f3dd5] | 723 | for item in list_of_data_attributes: |
---|
[0b12abb5] | 724 | element = newdoc.createElement(item[0]) |
---|
[2296316] | 725 | exec "element.setAttribute(item[0], str(self.%s))" % (item[1]) |
---|
[f32d144] | 726 | inputs.appendChild(element) |
---|
[26f3dd5] | 727 | |
---|
| 728 | for item in list_of_state_attributes: |
---|
| 729 | element = newdoc.createElement(item[0]) |
---|
[2296316] | 730 | exec "element.setAttribute(item[0], str(self.%s))" % (item[1]) |
---|
[26f3dd5] | 731 | inputs.appendChild(element) |
---|
[c0ff8cc] | 732 | |
---|
[f32d144] | 733 | # For self.values ={ disp_param_name: [vals,...],...} |
---|
| 734 | # and for self.weights ={ disp_param_name: [weights,...],...} |
---|
[38226d26] | 735 | list = {} |
---|
[11a7e11] | 736 | for item in list_of_model_attributes: |
---|
| 737 | element = newdoc.createElement(item[0]) |
---|
[2296316] | 738 | exec "list = self.%s" % item[1] |
---|
| 739 | for key, value in list.iteritems(): |
---|
| 740 | sub_element = newdoc.createElement(key) |
---|
| 741 | sub_element.setAttribute('name', str(key)) |
---|
| 742 | for val in value: |
---|
| 743 | com = "sub_element.appendChild" |
---|
| 744 | com += "(newdoc.createTextNode(str(%s)))" |
---|
| 745 | exec com % val |
---|
| 746 | |
---|
[f32d144] | 747 | element.appendChild(sub_element) |
---|
[11a7e11] | 748 | inputs.appendChild(element) |
---|
[2296316] | 749 | |
---|
| 750 | # Create doc for the dictionary of self._disp_obj_dic |
---|
| 751 | for item in list_of_obj_dic: |
---|
[f32d144] | 752 | element = newdoc.createElement(item[0]) |
---|
| 753 | exec "list = self.%s" % item[1] |
---|
| 754 | for key, val in list.iteritems(): |
---|
| 755 | value = repr(val) |
---|
| 756 | sub_element = newdoc.createElement(key) |
---|
| 757 | sub_element.setAttribute('name', str(key)) |
---|
| 758 | sub_element.setAttribute('value', str(value)) |
---|
| 759 | element.appendChild(sub_element) |
---|
| 760 | inputs.appendChild(element) |
---|
[2296316] | 761 | |
---|
[61cada5] | 762 | for item in list_of_state_parameters: |
---|
| 763 | element = newdoc.createElement(item[0]) |
---|
[ac5b69d] | 764 | com = "self._toXML_helper(thelist=self.%s," |
---|
[2296316] | 765 | com += " element=element, newdoc=newdoc)" |
---|
[f32d144] | 766 | exec com % item[1] |
---|
[61cada5] | 767 | inputs.appendChild(element) |
---|
[c0ff8cc] | 768 | |
---|
[61cada5] | 769 | # Save the file |
---|
| 770 | if doc is None: |
---|
| 771 | fd = open(file, 'w') |
---|
| 772 | fd.write(newdoc.toprettyxml()) |
---|
| 773 | fd.close() |
---|
| 774 | return None |
---|
| 775 | else: |
---|
[ac5b69d] | 776 | return newdoc |
---|
[61cada5] | 777 | |
---|
| 778 | def _fromXML_helper(self, node, list): |
---|
| 779 | """ |
---|
[5062bbf] | 780 | Helper function to write state to xml |
---|
[61cada5] | 781 | """ |
---|
| 782 | for item in node: |
---|
[0b12abb5] | 783 | try: |
---|
| 784 | name = item.get('name') |
---|
| 785 | except: |
---|
| 786 | name = None |
---|
| 787 | try: |
---|
| 788 | value = item.get('value') |
---|
| 789 | except: |
---|
| 790 | value = None |
---|
| 791 | try: |
---|
[3ad91de] | 792 | selected_to_fit = (item.get('selected_to_fit') == "True") |
---|
[0b12abb5] | 793 | except: |
---|
| 794 | selected_to_fit = None |
---|
| 795 | try: |
---|
[3ad91de] | 796 | error_displayed = (item.get('error_displayed') == "True") |
---|
[0b12abb5] | 797 | except: |
---|
| 798 | error_displayed = None |
---|
[f32d144] | 799 | try: |
---|
[0b12abb5] | 800 | error_value = item.get('error_value') |
---|
| 801 | except: |
---|
| 802 | error_value = None |
---|
| 803 | try: |
---|
[f32d144] | 804 | minimum_displayed = (item.get('minimum_displayed') == "True") |
---|
[0b12abb5] | 805 | except: |
---|
| 806 | minimum_displayed = None |
---|
| 807 | try: |
---|
| 808 | minimum_value = item.get('minimum_value') |
---|
| 809 | except: |
---|
| 810 | minimum_value = None |
---|
| 811 | try: |
---|
[3ad91de] | 812 | maximum_displayed = (item.get('maximum_displayed') == "True") |
---|
[0b12abb5] | 813 | except: |
---|
| 814 | maximum_displayed = None |
---|
| 815 | try: |
---|
| 816 | maximum_value = item.get('maximum_value') |
---|
| 817 | except: |
---|
| 818 | maximum_value = None |
---|
| 819 | try: |
---|
| 820 | unit = item.get('unit') |
---|
| 821 | except: |
---|
| 822 | unit = None |
---|
[2296316] | 823 | list.append([selected_to_fit, name, value, "+/-", |
---|
| 824 | [error_displayed, error_value], |
---|
[f32d144] | 825 | [minimum_displayed, minimum_value], |
---|
| 826 | [maximum_displayed, maximum_value], unit]) |
---|
[cfc0913] | 827 | |
---|
[61cada5] | 828 | def fromXML(self, file=None, node=None): |
---|
| 829 | """ |
---|
[5062bbf] | 830 | Load fitting state from a file |
---|
| 831 | |
---|
| 832 | :param file: .fitv file |
---|
| 833 | :param node: node of a XML document to read from |
---|
| 834 | |
---|
[61cada5] | 835 | """ |
---|
| 836 | if file is not None: |
---|
[11a7e11] | 837 | msg = "PageState no longer supports non-CanSAS" |
---|
| 838 | msg += " format for fitting files" |
---|
| 839 | raise RuntimeError, msg |
---|
[61cada5] | 840 | |
---|
[11a7e11] | 841 | if node.get('version')and node.get('version') == '1.0': |
---|
[61cada5] | 842 | |
---|
| 843 | # Get file name |
---|
| 844 | entry = get_content('ns:filename', node) |
---|
| 845 | if entry is not None: |
---|
| 846 | self.file = entry.text.strip() |
---|
[11a7e11] | 847 | |
---|
| 848 | # Get time stamp |
---|
| 849 | entry = get_content('ns:timestamp', node) |
---|
| 850 | if entry is not None and entry.get('epoch'): |
---|
| 851 | try: |
---|
| 852 | self.timestamp = float(entry.get('epoch')) |
---|
| 853 | except: |
---|
| 854 | msg = "PageState.fromXML: Could not" |
---|
| 855 | msg += " read timestamp\n %s" % sys.exc_value |
---|
| 856 | logging.error(msg) |
---|
| 857 | |
---|
[61cada5] | 858 | # Parse fitting attributes |
---|
| 859 | entry = get_content('ns:Attributes', node) |
---|
[0b12abb5] | 860 | for item in list_of_data_attributes: |
---|
[f32d144] | 861 | node = get_content('ns:%s' % item[0], entry) |
---|
[0b12abb5] | 862 | try: |
---|
[2296316] | 863 | exec "self.%s = parse_entry_helper(node, item)" % item[0] |
---|
[0b12abb5] | 864 | |
---|
| 865 | except: |
---|
| 866 | raise |
---|
[2296316] | 867 | |
---|
[61cada5] | 868 | if entry is not None: |
---|
[0b12abb5] | 869 | |
---|
[61cada5] | 870 | for item in list_of_state_attributes: |
---|
[2296316] | 871 | node = get_content('ns:%s' % item[0], entry) |
---|
[0b12abb5] | 872 | try: |
---|
[2296316] | 873 | exec "self.%s = parse_entry_helper(node, item)" % \ |
---|
| 874 | str(item[0]) |
---|
[0b12abb5] | 875 | except: |
---|
| 876 | raise |
---|
| 877 | |
---|
[2296316] | 878 | for item in list_of_state_parameters: |
---|
| 879 | node = get_content("ns:%s" % item[0], entry) |
---|
| 880 | exec "self._fromXML_helper(node=node, list=self.%s)" % \ |
---|
| 881 | item[1] |
---|
| 882 | |
---|
[f32d144] | 883 | # Recover _disp_obj_dict from xml file |
---|
| 884 | self._disp_obj_dict = {} |
---|
[2c44cf8] | 885 | disp_model = None |
---|
[2296316] | 886 | for item in list_of_obj_dic: |
---|
| 887 | # Get node |
---|
| 888 | node = get_content("ns:%s" % item[0], entry) |
---|
| 889 | for attr in node: |
---|
| 890 | name = attr.get('name') |
---|
[f32d144] | 891 | val = attr.get('value') |
---|
[2296316] | 892 | value = val.split(" instance")[0] |
---|
| 893 | disp_name = value.split("<")[1] |
---|
[11a7e11] | 894 | try: |
---|
[2296316] | 895 | # Try to recover disp_model object from strings |
---|
[79492222] | 896 | com = "from sas.models.dispersion_models " |
---|
[2c44cf8] | 897 | com += "import %s as disp" |
---|
[2296316] | 898 | com_name = disp_name.split(".")[3] |
---|
| 899 | exec com % com_name |
---|
[2c44cf8] | 900 | disp_model = disp() |
---|
[2296316] | 901 | exec "self.%s['%s'] = com_name" % (item[1], name) |
---|
[11a7e11] | 902 | except: |
---|
[2296316] | 903 | pass |
---|
| 904 | |
---|
[f32d144] | 905 | # get self.values and self.weights dic. if exists |
---|
[2296316] | 906 | for item in list_of_model_attributes: |
---|
| 907 | node = get_content("ns:%s" % item[0], entry) |
---|
| 908 | dic = {} |
---|
| 909 | list = [] |
---|
| 910 | for par in node: |
---|
| 911 | name = par.get('name') |
---|
| 912 | values = par.text.split('\n') |
---|
| 913 | # Get lines only with numbers |
---|
| 914 | for line in values: |
---|
| 915 | try: |
---|
[f32d144] | 916 | val = float(line) |
---|
| 917 | list.append(val) |
---|
[2296316] | 918 | except: |
---|
| 919 | # pass if line is empty (it happens) |
---|
| 920 | pass |
---|
| 921 | dic[name] = numpy.array(list) |
---|
| 922 | exec "self.%s = dic" % item[1] |
---|
| 923 | |
---|
| 924 | def set_plot_state(self, figs, canvases): |
---|
| 925 | """ |
---|
| 926 | Build image state that wx.html understand |
---|
| 927 | by plotting, putting it into wx.FileSystem image object |
---|
| 928 | |
---|
| 929 | """ |
---|
| 930 | images = [] |
---|
| 931 | # some imports |
---|
| 932 | import wx |
---|
| 933 | |
---|
| 934 | # Reset memory |
---|
| 935 | self.imgRAM = None |
---|
| 936 | wx.MemoryFSHandler() |
---|
| 937 | |
---|
| 938 | # For no figures in the list, prepare empty plot |
---|
| 939 | if figs == None or len(figs) == 0: |
---|
| 940 | figs = [None] |
---|
| 941 | |
---|
[f32d144] | 942 | # Loop over the list of figures |
---|
[2296316] | 943 | # use wx.MemoryFSHandler |
---|
[f32d144] | 944 | self.imgRAM = wx.MemoryFSHandler() |
---|
[2296316] | 945 | for fig in figs: |
---|
| 946 | if figs != None: |
---|
| 947 | ind = figs.index(fig) |
---|
| 948 | canvas = canvases[ind] |
---|
[0b12abb5] | 949 | |
---|
[2296316] | 950 | #store the image in wx.FileSystem Object |
---|
| 951 | wx.FileSystem.AddHandler(wx.MemoryFSHandler()) |
---|
| 952 | |
---|
| 953 | # index of the fig |
---|
| 954 | ind = figs.index(fig) |
---|
| 955 | |
---|
| 956 | #AddFile, image can be retrieved with 'memory:filename' |
---|
[f32d144] | 957 | self.imgRAM.AddFile('img_fit%s.png' % ind, |
---|
[2296316] | 958 | canvas.bitmap, wx.BITMAP_TYPE_PNG) |
---|
| 959 | |
---|
| 960 | #append figs |
---|
| 961 | images.append(fig) |
---|
| 962 | |
---|
[c0ff8cc] | 963 | return images |
---|
[61cada5] | 964 | |
---|
[f32d144] | 965 | |
---|
[61cada5] | 966 | class Reader(CansasReader): |
---|
| 967 | """ |
---|
[5062bbf] | 968 | Class to load a .fitv fitting file |
---|
[61cada5] | 969 | """ |
---|
| 970 | ## File type |
---|
| 971 | type_name = "Fitting" |
---|
| 972 | |
---|
| 973 | ## Wildcards |
---|
[b35d3d1] | 974 | type = ["Fitting files (*.fitv)|*.fitv" |
---|
| 975 | "SANSView file (*.svs)|*.svs"] |
---|
[61cada5] | 976 | ## List of allowed extensions |
---|
[f32d144] | 977 | ext = ['.fitv', '.FITV', '.svs', 'SVS'] |
---|
[61cada5] | 978 | |
---|
| 979 | def __init__(self, call_back=None, cansas=True): |
---|
[df7ed14] | 980 | CansasReader.__init__(self) |
---|
[61cada5] | 981 | """ |
---|
[5062bbf] | 982 | Initialize the call-back method to be called |
---|
| 983 | after we load a file |
---|
| 984 | |
---|
| 985 | :param call_back: call-back method |
---|
| 986 | :param cansas: True = files will be written/read in CanSAS format |
---|
| 987 | False = write CanSAS format |
---|
[61cada5] | 988 | |
---|
| 989 | """ |
---|
| 990 | ## Call back method to be executed after a file is read |
---|
| 991 | self.call_back = call_back |
---|
| 992 | ## CanSAS format flag |
---|
| 993 | self.cansas = cansas |
---|
[75fbd17] | 994 | self.state = None |
---|
[f32d144] | 995 | |
---|
[75fbd17] | 996 | def get_state(self): |
---|
| 997 | return self.state |
---|
[61cada5] | 998 | |
---|
| 999 | def read(self, path): |
---|
[f32d144] | 1000 | """ |
---|
[5062bbf] | 1001 | Load a new P(r) inversion state from file |
---|
| 1002 | |
---|
| 1003 | :param path: file path |
---|
| 1004 | |
---|
[61cada5] | 1005 | """ |
---|
[e9b12eaf] | 1006 | if self.cansas == True: |
---|
[61cada5] | 1007 | return self._read_cansas(path) |
---|
[e9b12eaf] | 1008 | |
---|
| 1009 | def _data2d_to_xml_doc(self, datainfo): |
---|
[61cada5] | 1010 | """ |
---|
[5062bbf] | 1011 | Create an XML document to contain the content of a Data2D |
---|
| 1012 | |
---|
| 1013 | :param datainfo: Data2D object |
---|
| 1014 | |
---|
[61cada5] | 1015 | """ |
---|
| 1016 | if not issubclass(datainfo.__class__, Data2D): |
---|
| 1017 | raise RuntimeError, "The cansas writer expects a Data2D instance" |
---|
| 1018 | |
---|
| 1019 | doc = xml.dom.minidom.Document() |
---|
| 1020 | main_node = doc.createElement("SASroot") |
---|
| 1021 | main_node.setAttribute("version", self.version) |
---|
[df7ed14] | 1022 | main_node.setAttribute("xmlns", "cansas1d/%s" % self.version) |
---|
| 1023 | main_node.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") |
---|
| 1024 | main_node.setAttribute("xsi:schemaLocation", "cansas1d/%s http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd" % self.version) |
---|
| 1025 | |
---|
[61cada5] | 1026 | doc.appendChild(main_node) |
---|
| 1027 | |
---|
| 1028 | entry_node = doc.createElement("SASentry") |
---|
| 1029 | main_node.appendChild(entry_node) |
---|
[df7ed14] | 1030 | |
---|
[61cada5] | 1031 | write_node(doc, entry_node, "Title", datainfo.title) |
---|
[df7ed14] | 1032 | if datainfo is not None: |
---|
| 1033 | write_node(doc, entry_node, "data_class", datainfo.__class__.__name__) |
---|
[61cada5] | 1034 | for item in datainfo.run: |
---|
| 1035 | runname = {} |
---|
| 1036 | if datainfo.run_name.has_key(item) and len(str(datainfo.run_name[item]))>1: |
---|
| 1037 | runname = {'name': datainfo.run_name[item] } |
---|
| 1038 | write_node(doc, entry_node, "Run", item, runname) |
---|
| 1039 | # Data info |
---|
[df7ed14] | 1040 | new_node = doc.createElement("SASdata") |
---|
| 1041 | entry_node.appendChild(new_node) |
---|
[e9b12eaf] | 1042 | for item in list_of_data_2d_attr: |
---|
| 1043 | element = doc.createElement(item[0]) |
---|
[f32d144] | 1044 | exec "element.setAttribute(item[0], str(datainfo.%s))" % (item[1]) |
---|
[df7ed14] | 1045 | new_node.appendChild(element) |
---|
| 1046 | |
---|
[e9b12eaf] | 1047 | for item in list_of_data2d_values: |
---|
[df7ed14] | 1048 | root_node = doc.createElement(item[0]) |
---|
| 1049 | new_node.appendChild(root_node) |
---|
[38226d26] | 1050 | temp_list = None |
---|
[f32d144] | 1051 | exec "temp_list = datainfo.%s" % item[1] |
---|
[df7ed14] | 1052 | |
---|
[35b556d] | 1053 | if temp_list is None or len(temp_list)== 0: |
---|
[df7ed14] | 1054 | element = doc.createElement(item[0]) |
---|
[f32d144] | 1055 | exec "element.appendChild(doc.createTextNode(str(%s)))" % temp_list |
---|
[df7ed14] | 1056 | root_node.appendChild(element) |
---|
[35b556d] | 1057 | else: |
---|
| 1058 | for value in temp_list: |
---|
[df7ed14] | 1059 | element = doc.createElement(item[0]) |
---|
[f32d144] | 1060 | exec "element.setAttribute(item[0], str(%s))" % value |
---|
[df7ed14] | 1061 | root_node.appendChild(element) |
---|
| 1062 | |
---|
[61cada5] | 1063 | # Sample info |
---|
| 1064 | sample = doc.createElement("SASsample") |
---|
| 1065 | if datainfo.sample.name is not None: |
---|
| 1066 | sample.setAttribute("name", str(datainfo.sample.name)) |
---|
| 1067 | entry_node.appendChild(sample) |
---|
| 1068 | write_node(doc, sample, "ID", str(datainfo.sample.ID)) |
---|
[f32d144] | 1069 | write_node(doc, sample, "thickness", datainfo.sample.thickness, |
---|
| 1070 | {"unit": datainfo.sample.thickness_unit}) |
---|
[61cada5] | 1071 | write_node(doc, sample, "transmission", datainfo.sample.transmission) |
---|
[f32d144] | 1072 | write_node(doc, sample, "temperature", datainfo.sample.temperature, |
---|
| 1073 | {"unit": datainfo.sample.temperature_unit}) |
---|
[61cada5] | 1074 | |
---|
| 1075 | for item in datainfo.sample.details: |
---|
| 1076 | write_node(doc, sample, "details", item) |
---|
| 1077 | |
---|
| 1078 | pos = doc.createElement("position") |
---|
[f32d144] | 1079 | written = write_node(doc, pos, "x", datainfo.sample.position.x, |
---|
| 1080 | {"unit": datainfo.sample.position_unit}) |
---|
| 1081 | written = written | write_node(doc, pos, "y", |
---|
| 1082 | datainfo.sample.position.y, |
---|
| 1083 | {"unit": datainfo.sample.position_unit}) |
---|
| 1084 | written = written | write_node(doc, pos, "z", |
---|
| 1085 | datainfo.sample.position.z, |
---|
| 1086 | {"unit": datainfo.sample.position_unit}) |
---|
[61cada5] | 1087 | if written == True: |
---|
| 1088 | sample.appendChild(pos) |
---|
| 1089 | |
---|
| 1090 | ori = doc.createElement("orientation") |
---|
[f32d144] | 1091 | written = write_node(doc, ori, "roll", datainfo.sample.orientation.x, |
---|
| 1092 | {"unit": datainfo.sample.orientation_unit}) |
---|
| 1093 | written = written | write_node(doc, ori, "pitch", |
---|
| 1094 | datainfo.sample.orientation.y, |
---|
| 1095 | {"unit": datainfo.sample.orientation_unit}) |
---|
| 1096 | written = written | write_node(doc, ori, "yaw", |
---|
| 1097 | datainfo.sample.orientation.z, |
---|
| 1098 | {"unit": datainfo.sample.orientation_unit}) |
---|
[61cada5] | 1099 | if written == True: |
---|
| 1100 | sample.appendChild(ori) |
---|
| 1101 | |
---|
| 1102 | # Instrument info |
---|
| 1103 | instr = doc.createElement("SASinstrument") |
---|
| 1104 | entry_node.appendChild(instr) |
---|
| 1105 | |
---|
| 1106 | write_node(doc, instr, "name", datainfo.instrument) |
---|
| 1107 | |
---|
| 1108 | # Source |
---|
| 1109 | source = doc.createElement("SASsource") |
---|
| 1110 | if datainfo.source.name is not None: |
---|
| 1111 | source.setAttribute("name", str(datainfo.source.name)) |
---|
| 1112 | instr.appendChild(source) |
---|
| 1113 | |
---|
| 1114 | write_node(doc, source, "radiation", datainfo.source.radiation) |
---|
| 1115 | write_node(doc, source, "beam_shape", datainfo.source.beam_shape) |
---|
| 1116 | size = doc.createElement("beam_size") |
---|
| 1117 | if datainfo.source.beam_size_name is not None: |
---|
| 1118 | size.setAttribute("name", str(datainfo.source.beam_size_name)) |
---|
[f32d144] | 1119 | written = write_node(doc, size, "x", datainfo.source.beam_size.x, |
---|
| 1120 | {"unit": datainfo.source.beam_size_unit}) |
---|
| 1121 | written = written | write_node(doc, size, "y", |
---|
| 1122 | datainfo.source.beam_size.y, |
---|
| 1123 | {"unit": datainfo.source.beam_size_unit}) |
---|
| 1124 | written = written | write_node(doc, size, "z", |
---|
| 1125 | datainfo.source.beam_size.z, |
---|
| 1126 | {"unit": datainfo.source.beam_size_unit}) |
---|
[61cada5] | 1127 | if written == True: |
---|
| 1128 | source.appendChild(size) |
---|
| 1129 | |
---|
[f32d144] | 1130 | write_node(doc, source, "wavelength", datainfo.source.wavelength, |
---|
| 1131 | {"unit": datainfo.source.wavelength_unit}) |
---|
| 1132 | write_node(doc, source, "wavelength_min", |
---|
| 1133 | datainfo.source.wavelength_min, |
---|
| 1134 | {"unit": datainfo.source.wavelength_min_unit}) |
---|
| 1135 | write_node(doc, source, "wavelength_max", |
---|
| 1136 | datainfo.source.wavelength_max, |
---|
| 1137 | {"unit": datainfo.source.wavelength_max_unit}) |
---|
| 1138 | write_node(doc, source, "wavelength_spread", |
---|
| 1139 | datainfo.source.wavelength_spread, |
---|
| 1140 | {"unit": datainfo.source.wavelength_spread_unit}) |
---|
[61cada5] | 1141 | |
---|
| 1142 | # Collimation |
---|
| 1143 | for item in datainfo.collimation: |
---|
| 1144 | coll = doc.createElement("SAScollimation") |
---|
| 1145 | if item.name is not None: |
---|
| 1146 | coll.setAttribute("name", str(item.name)) |
---|
| 1147 | instr.appendChild(coll) |
---|
| 1148 | |
---|
[f32d144] | 1149 | write_node(doc, coll, "length", item.length, |
---|
| 1150 | {"unit": item.length_unit}) |
---|
[61cada5] | 1151 | |
---|
| 1152 | for apert in item.aperture: |
---|
| 1153 | ap = doc.createElement("aperture") |
---|
| 1154 | if apert.name is not None: |
---|
| 1155 | ap.setAttribute("name", str(apert.name)) |
---|
| 1156 | if apert.type is not None: |
---|
| 1157 | ap.setAttribute("type", str(apert.type)) |
---|
| 1158 | coll.appendChild(ap) |
---|
| 1159 | |
---|
[f32d144] | 1160 | write_node(doc, ap, "distance", apert.distance, |
---|
| 1161 | {"unit": apert.distance_unit}) |
---|
[61cada5] | 1162 | |
---|
| 1163 | size = doc.createElement("size") |
---|
| 1164 | if apert.size_name is not None: |
---|
| 1165 | size.setAttribute("name", str(apert.size_name)) |
---|
[f32d144] | 1166 | written = write_node(doc, size, "x", apert.size.x, |
---|
| 1167 | {"unit": apert.size_unit}) |
---|
| 1168 | written = written | write_node(doc, size, "y", apert.size.y, |
---|
| 1169 | {"unit": apert.size_unit}) |
---|
| 1170 | written = written | write_node(doc, size, "z", apert.size.z, |
---|
| 1171 | {"unit": apert.size_unit}) |
---|
[61cada5] | 1172 | if written == True: |
---|
| 1173 | ap.appendChild(size) |
---|
| 1174 | |
---|
| 1175 | # Detectors |
---|
| 1176 | for item in datainfo.detector: |
---|
| 1177 | det = doc.createElement("SASdetector") |
---|
| 1178 | written = write_node(doc, det, "name", item.name) |
---|
[f32d144] | 1179 | written = written | write_node(doc, det, "SDD", item.distance, |
---|
| 1180 | {"unit": item.distance_unit}) |
---|
| 1181 | written = written | write_node(doc, det, "slit_length", |
---|
| 1182 | item.slit_length, |
---|
| 1183 | {"unit": item.slit_length_unit}) |
---|
[61cada5] | 1184 | if written == True: |
---|
| 1185 | instr.appendChild(det) |
---|
| 1186 | |
---|
| 1187 | off = doc.createElement("offset") |
---|
[f32d144] | 1188 | written = write_node(doc, off, "x", item.offset.x, |
---|
| 1189 | {"unit": item.offset_unit}) |
---|
| 1190 | written = written | write_node(doc, off, "y", item.offset.y, |
---|
| 1191 | {"unit": item.offset_unit}) |
---|
| 1192 | written = written | write_node(doc, off, "z", item.offset.z, |
---|
| 1193 | {"unit": item.offset_unit}) |
---|
[61cada5] | 1194 | if written == True: |
---|
| 1195 | det.appendChild(off) |
---|
| 1196 | |
---|
| 1197 | center = doc.createElement("beam_center") |
---|
[f32d144] | 1198 | written = write_node(doc, center, "x", item.beam_center.x, |
---|
| 1199 | {"unit": item.beam_center_unit}) |
---|
| 1200 | written = written | write_node(doc, center, "y", |
---|
| 1201 | item.beam_center.y, |
---|
| 1202 | {"unit": item.beam_center_unit}) |
---|
| 1203 | written = written | write_node(doc, center, "z", |
---|
| 1204 | item.beam_center.z, |
---|
| 1205 | {"unit": item.beam_center_unit}) |
---|
[61cada5] | 1206 | if written == True: |
---|
| 1207 | det.appendChild(center) |
---|
| 1208 | |
---|
| 1209 | pix = doc.createElement("pixel_size") |
---|
[f32d144] | 1210 | written = write_node(doc, pix, "x", item.pixel_size.x, |
---|
| 1211 | {"unit": item.pixel_size_unit}) |
---|
| 1212 | written = written | write_node(doc, pix, "y", item.pixel_size.y, |
---|
| 1213 | {"unit": item.pixel_size_unit}) |
---|
| 1214 | written = written | write_node(doc, pix, "z", item.pixel_size.z, |
---|
| 1215 | {"unit": item.pixel_size_unit}) |
---|
[61cada5] | 1216 | if written == True: |
---|
| 1217 | det.appendChild(pix) |
---|
| 1218 | |
---|
| 1219 | ori = doc.createElement("orientation") |
---|
[f32d144] | 1220 | written = write_node(doc, ori, "roll", item.orientation.x, |
---|
| 1221 | {"unit": item.orientation_unit}) |
---|
| 1222 | written = written | write_node(doc, ori, "pitch", |
---|
| 1223 | item.orientation.y, |
---|
| 1224 | {"unit": item.orientation_unit}) |
---|
| 1225 | written = written | write_node(doc, ori, "yaw", item.orientation.z, |
---|
| 1226 | {"unit": item.orientation_unit}) |
---|
[61cada5] | 1227 | if written == True: |
---|
| 1228 | det.appendChild(ori) |
---|
| 1229 | |
---|
| 1230 | # Processes info |
---|
| 1231 | for item in datainfo.process: |
---|
| 1232 | node = doc.createElement("SASprocess") |
---|
| 1233 | entry_node.appendChild(node) |
---|
| 1234 | |
---|
| 1235 | write_node(doc, node, "name", item.name) |
---|
| 1236 | write_node(doc, node, "date", item.date) |
---|
| 1237 | write_node(doc, node, "description", item.description) |
---|
| 1238 | for term in item.term: |
---|
| 1239 | value = term['value'] |
---|
| 1240 | del term['value'] |
---|
| 1241 | write_node(doc, node, "term", value, term) |
---|
| 1242 | for note in item.notes: |
---|
| 1243 | write_node(doc, node, "SASprocessnote", note) |
---|
| 1244 | # Return the document, and the SASentry node associated with |
---|
| 1245 | # the data we just wrote |
---|
| 1246 | return doc, entry_node |
---|
[e9b12eaf] | 1247 | |
---|
[61cada5] | 1248 | def _parse_state(self, entry): |
---|
| 1249 | """ |
---|
[5062bbf] | 1250 | Read a fit result from an XML node |
---|
| 1251 | |
---|
[f32d144] | 1252 | :param entry: XML node to read from |
---|
[5062bbf] | 1253 | |
---|
| 1254 | :return: PageState object |
---|
[61cada5] | 1255 | """ |
---|
| 1256 | # Create an empty state |
---|
[b35d3d1] | 1257 | state = None |
---|
[61cada5] | 1258 | # Locate the P(r) node |
---|
| 1259 | try: |
---|
[f32d144] | 1260 | nodes = entry.xpath('ns:%s' % FITTING_NODE_NAME, |
---|
| 1261 | namespaces={'ns': CANSAS_NS}) |
---|
| 1262 | if nodes != []: |
---|
[b35d3d1] | 1263 | # Create an empty state |
---|
[f32d144] | 1264 | state = PageState() |
---|
[b35d3d1] | 1265 | state.fromXML(node=nodes[0]) |
---|
[ac5b69d] | 1266 | |
---|
[61cada5] | 1267 | except: |
---|
| 1268 | logging.info("XML document does not contain fitting information.\n %s" % sys.exc_value) |
---|
| 1269 | |
---|
| 1270 | return state |
---|
[f32d144] | 1271 | |
---|
[ac5b69d] | 1272 | def _parse_save_state_entry(self, dom): |
---|
[e9b12eaf] | 1273 | """ |
---|
[5062bbf] | 1274 | Parse a SASentry |
---|
| 1275 | |
---|
| 1276 | :param node: SASentry node |
---|
| 1277 | |
---|
[df7ed14] | 1278 | :return: Data1D/Data2D object |
---|
[5062bbf] | 1279 | |
---|
[e9b12eaf] | 1280 | """ |
---|
[df7ed14] | 1281 | node = dom.xpath('ns:data_class', namespaces={'ns': CANSAS_NS}) |
---|
| 1282 | if not node or node[0].text.lstrip().rstrip() != "Data2D": |
---|
[ac5b69d] | 1283 | return_value, _ = self._parse_entry(dom) |
---|
| 1284 | numpy.trim_zeros(return_value.x) |
---|
| 1285 | numpy.trim_zeros(return_value.y) |
---|
| 1286 | numpy.trim_zeros(return_value.dy) |
---|
| 1287 | size_dx = return_value.dx.size |
---|
| 1288 | size_dxl = return_value.dxl.size |
---|
| 1289 | size_dxw = return_value.dxw.size |
---|
| 1290 | if size_dxl == 0 and size_dxw == 0: |
---|
| 1291 | return_value.dxl = None |
---|
| 1292 | return_value.dxw = None |
---|
| 1293 | numpy.trim_zeros(return_value.dx) |
---|
| 1294 | elif size_dx == 0: |
---|
| 1295 | return_value.dx = None |
---|
| 1296 | size_dx = size_dxl |
---|
| 1297 | numpy.trim_zeros(return_value.dxl) |
---|
| 1298 | numpy.trim_zeros(return_value.dxw) |
---|
| 1299 | |
---|
| 1300 | return return_value, _ |
---|
[df7ed14] | 1301 | |
---|
| 1302 | #Parse 2D |
---|
[e9b12eaf] | 1303 | data_info = Data2D() |
---|
| 1304 | |
---|
[f32d144] | 1305 | # Look up title |
---|
[e9b12eaf] | 1306 | self._store_content('ns:Title', dom, 'title', data_info) |
---|
| 1307 | |
---|
[f32d144] | 1308 | # Look up run number |
---|
[e9b12eaf] | 1309 | nodes = dom.xpath('ns:Run', namespaces={'ns': CANSAS_NS}) |
---|
[f32d144] | 1310 | for item in nodes: |
---|
[e9b12eaf] | 1311 | if item.text is not None: |
---|
| 1312 | value = item.text.strip() |
---|
| 1313 | if len(value) > 0: |
---|
| 1314 | data_info.run.append(value) |
---|
| 1315 | if item.get('name') is not None: |
---|
| 1316 | data_info.run_name[value] = item.get('name') |
---|
| 1317 | |
---|
[f32d144] | 1318 | # Look up instrument name |
---|
| 1319 | self._store_content('ns:SASinstrument/ns:name', dom, |
---|
| 1320 | 'instrument', data_info) |
---|
[e9b12eaf] | 1321 | |
---|
| 1322 | # Notes |
---|
| 1323 | note_list = dom.xpath('ns:SASnote', namespaces={'ns': CANSAS_NS}) |
---|
| 1324 | for note in note_list: |
---|
| 1325 | try: |
---|
| 1326 | if note.text is not None: |
---|
| 1327 | note_value = note.text.strip() |
---|
| 1328 | if len(note_value) > 0: |
---|
| 1329 | data_info.notes.append(note_value) |
---|
| 1330 | except: |
---|
| 1331 | err_mess = "cansas_reader.read: error processing entry notes\n %s" % sys.exc_value |
---|
| 1332 | self.errors.append(err_mess) |
---|
| 1333 | logging.error(err_mess) |
---|
| 1334 | |
---|
| 1335 | # Sample info ################### |
---|
| 1336 | entry = get_content('ns:SASsample', dom) |
---|
| 1337 | if entry is not None: |
---|
| 1338 | data_info.sample.name = entry.get('name') |
---|
| 1339 | |
---|
[f32d144] | 1340 | self._store_content('ns:SASsample/ns:ID', |
---|
| 1341 | dom, 'ID', data_info.sample) |
---|
| 1342 | self._store_float('ns:SASsample/ns:thickness', |
---|
[e9b12eaf] | 1343 | dom, 'thickness', data_info.sample) |
---|
[f32d144] | 1344 | self._store_float('ns:SASsample/ns:transmission', |
---|
[e9b12eaf] | 1345 | dom, 'transmission', data_info.sample) |
---|
[f32d144] | 1346 | self._store_float('ns:SASsample/ns:temperature', |
---|
[e9b12eaf] | 1347 | dom, 'temperature', data_info.sample) |
---|
| 1348 | |
---|
[f32d144] | 1349 | nodes = dom.xpath('ns:SASsample/ns:details', |
---|
| 1350 | namespaces={'ns': CANSAS_NS}) |
---|
[e9b12eaf] | 1351 | for item in nodes: |
---|
| 1352 | try: |
---|
| 1353 | if item.text is not None: |
---|
| 1354 | detail_value = item.text.strip() |
---|
| 1355 | if len(detail_value) > 0: |
---|
| 1356 | data_info.sample.details.append(detail_value) |
---|
| 1357 | except: |
---|
| 1358 | err_mess = "cansas_reader.read: error processing sample details\n %s" % sys.exc_value |
---|
| 1359 | self.errors.append(err_mess) |
---|
| 1360 | logging.error(err_mess) |
---|
| 1361 | |
---|
| 1362 | # Position (as a vector) |
---|
[f32d144] | 1363 | self._store_float('ns:SASsample/ns:position/ns:x', |
---|
| 1364 | dom, 'position.x', data_info.sample) |
---|
| 1365 | self._store_float('ns:SASsample/ns:position/ns:y', |
---|
| 1366 | dom, 'position.y', data_info.sample) |
---|
| 1367 | self._store_float('ns:SASsample/ns:position/ns:z', |
---|
| 1368 | dom, 'position.z', data_info.sample) |
---|
[e9b12eaf] | 1369 | |
---|
| 1370 | # Orientation (as a vector) |
---|
[f32d144] | 1371 | self._store_float('ns:SASsample/ns:orientation/ns:roll', |
---|
| 1372 | dom, 'orientation.x', data_info.sample) |
---|
| 1373 | self._store_float('ns:SASsample/ns:orientation/ns:pitch', |
---|
| 1374 | dom, 'orientation.y', data_info.sample) |
---|
| 1375 | self._store_float('ns:SASsample/ns:orientation/ns:yaw', |
---|
| 1376 | dom, 'orientation.z', data_info.sample) |
---|
[e9b12eaf] | 1377 | |
---|
| 1378 | # Source info ################### |
---|
| 1379 | entry = get_content('ns:SASinstrument/ns:SASsource', dom) |
---|
| 1380 | if entry is not None: |
---|
| 1381 | data_info.source.name = entry.get('name') |
---|
| 1382 | |
---|
[f32d144] | 1383 | self._store_content('ns:SASinstrument/ns:SASsource/ns:radiation', |
---|
| 1384 | dom, 'radiation', data_info.source) |
---|
| 1385 | self._store_content('ns:SASinstrument/ns:SASsource/ns:beam_shape', |
---|
| 1386 | dom, 'beam_shape', data_info.source) |
---|
| 1387 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength', |
---|
| 1388 | dom, 'wavelength', data_info.source) |
---|
| 1389 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_min', |
---|
| 1390 | dom, 'wavelength_min', data_info.source) |
---|
| 1391 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_max', |
---|
| 1392 | dom, 'wavelength_max', data_info.source) |
---|
| 1393 | self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_spread', |
---|
| 1394 | dom, 'wavelength_spread', data_info.source) |
---|
| 1395 | |
---|
| 1396 | # Beam size (as a vector) |
---|
[e9b12eaf] | 1397 | entry = get_content('ns:SASinstrument/ns:SASsource/ns:beam_size', dom) |
---|
| 1398 | if entry is not None: |
---|
| 1399 | data_info.source.beam_size_name = entry.get('name') |
---|
| 1400 | |
---|
[f32d144] | 1401 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:x', |
---|
| 1402 | dom, 'beam_size.x', data_info.source) |
---|
| 1403 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:y', |
---|
| 1404 | dom, 'beam_size.y', data_info.source) |
---|
| 1405 | self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:z', |
---|
| 1406 | dom, 'beam_size.z', data_info.source) |
---|
[e9b12eaf] | 1407 | |
---|
| 1408 | # Collimation info ################### |
---|
[f32d144] | 1409 | nodes = dom.xpath('ns:SASinstrument/ns:SAScollimation', |
---|
| 1410 | namespaces={'ns': CANSAS_NS}) |
---|
[e9b12eaf] | 1411 | for item in nodes: |
---|
| 1412 | collim = Collimation() |
---|
| 1413 | if item.get('name') is not None: |
---|
| 1414 | collim.name = item.get('name') |
---|
[f32d144] | 1415 | self._store_float('ns:length', item, 'length', collim) |
---|
[e9b12eaf] | 1416 | |
---|
| 1417 | # Look for apertures |
---|
[f32d144] | 1418 | apert_list = item.xpath('ns:aperture', |
---|
| 1419 | namespaces={'ns': CANSAS_NS}) |
---|
[e9b12eaf] | 1420 | for apert in apert_list: |
---|
[f32d144] | 1421 | aperture = Aperture() |
---|
[e9b12eaf] | 1422 | |
---|
| 1423 | # Get the name and type of the aperture |
---|
| 1424 | aperture.name = apert.get('name') |
---|
| 1425 | aperture.type = apert.get('type') |
---|
| 1426 | |
---|
[f32d144] | 1427 | self._store_float('ns:distance', apert, 'distance', aperture) |
---|
[e9b12eaf] | 1428 | |
---|
| 1429 | entry = get_content('ns:size', apert) |
---|
| 1430 | if entry is not None: |
---|
| 1431 | aperture.size_name = entry.get('name') |
---|
| 1432 | |
---|
[f32d144] | 1433 | self._store_float('ns:size/ns:x', apert, 'size.x', aperture) |
---|
| 1434 | self._store_float('ns:size/ns:y', apert, 'size.y', aperture) |
---|
[e9b12eaf] | 1435 | self._store_float('ns:size/ns:z', apert, 'size.z', aperture) |
---|
| 1436 | |
---|
| 1437 | collim.aperture.append(aperture) |
---|
| 1438 | |
---|
| 1439 | data_info.collimation.append(collim) |
---|
| 1440 | |
---|
| 1441 | # Detector info ###################### |
---|
[f32d144] | 1442 | nodes = dom.xpath('ns:SASinstrument/ns:SASdetector', |
---|
| 1443 | namespaces={'ns': CANSAS_NS}) |
---|
[e9b12eaf] | 1444 | for item in nodes: |
---|
| 1445 | |
---|
| 1446 | detector = Detector() |
---|
| 1447 | |
---|
| 1448 | self._store_content('ns:name', item, 'name', detector) |
---|
[f32d144] | 1449 | self._store_float('ns:SDD', item, 'distance', detector) |
---|
[e9b12eaf] | 1450 | |
---|
| 1451 | # Detector offset (as a vector) |
---|
[f32d144] | 1452 | self._store_float('ns:offset/ns:x', item, 'offset.x', detector) |
---|
| 1453 | self._store_float('ns:offset/ns:y', item, 'offset.y', detector) |
---|
| 1454 | self._store_float('ns:offset/ns:z', item, 'offset.z', detector) |
---|
[e9b12eaf] | 1455 | |
---|
| 1456 | # Detector orientation (as a vector) |
---|
[f32d144] | 1457 | self._store_float('ns:orientation/ns:roll', item, |
---|
| 1458 | 'orientation.x', detector) |
---|
| 1459 | self._store_float('ns:orientation/ns:pitch', item, |
---|
| 1460 | 'orientation.y', detector) |
---|
| 1461 | self._store_float('ns:orientation/ns:yaw', item, |
---|
| 1462 | 'orientation.z', detector) |
---|
[e9b12eaf] | 1463 | |
---|
| 1464 | # Beam center (as a vector) |
---|
[f32d144] | 1465 | self._store_float('ns:beam_center/ns:x', item, |
---|
| 1466 | 'beam_center.x', detector) |
---|
| 1467 | self._store_float('ns:beam_center/ns:y', item, |
---|
| 1468 | 'beam_center.y', detector) |
---|
| 1469 | self._store_float('ns:beam_center/ns:z', item, |
---|
| 1470 | 'beam_center.z', detector) |
---|
[e9b12eaf] | 1471 | |
---|
| 1472 | # Pixel size (as a vector) |
---|
[f32d144] | 1473 | self._store_float('ns:pixel_size/ns:x', item, |
---|
| 1474 | 'pixel_size.x', detector) |
---|
| 1475 | self._store_float('ns:pixel_size/ns:y', item, |
---|
| 1476 | 'pixel_size.y', detector) |
---|
| 1477 | self._store_float('ns:pixel_size/ns:z', item, |
---|
| 1478 | 'pixel_size.z', detector) |
---|
[e9b12eaf] | 1479 | |
---|
| 1480 | self._store_float('ns:slit_length', item, 'slit_length', detector) |
---|
| 1481 | |
---|
[f32d144] | 1482 | data_info.detector.append(detector) |
---|
[e9b12eaf] | 1483 | |
---|
| 1484 | # Processes info ###################### |
---|
| 1485 | nodes = dom.xpath('ns:SASprocess', namespaces={'ns': CANSAS_NS}) |
---|
| 1486 | for item in nodes: |
---|
| 1487 | process = Process() |
---|
| 1488 | self._store_content('ns:name', item, 'name', process) |
---|
| 1489 | self._store_content('ns:date', item, 'date', process) |
---|
| 1490 | self._store_content('ns:description', item, 'description', process) |
---|
| 1491 | |
---|
| 1492 | term_list = item.xpath('ns:term', namespaces={'ns': CANSAS_NS}) |
---|
| 1493 | for term in term_list: |
---|
| 1494 | try: |
---|
| 1495 | term_attr = {} |
---|
| 1496 | for attr in term.keys(): |
---|
| 1497 | term_attr[attr] = term.get(attr).strip() |
---|
| 1498 | if term.text is not None: |
---|
| 1499 | term_attr['value'] = term.text.strip() |
---|
| 1500 | process.term.append(term_attr) |
---|
| 1501 | except: |
---|
| 1502 | err_mess = "cansas_reader.read: error processing process term\n %s" % sys.exc_value |
---|
| 1503 | self.errors.append(err_mess) |
---|
| 1504 | logging.error(err_mess) |
---|
| 1505 | |
---|
[f32d144] | 1506 | note_list = item.xpath('ns:SASprocessnote', |
---|
| 1507 | namespaces={'ns': CANSAS_NS}) |
---|
[e9b12eaf] | 1508 | for note in note_list: |
---|
| 1509 | if note.text is not None: |
---|
| 1510 | process.notes.append(note.text.strip()) |
---|
| 1511 | |
---|
| 1512 | data_info.process.append(process) |
---|
| 1513 | |
---|
| 1514 | # Data info ###################### |
---|
| 1515 | nodes = dom.xpath('ns:SASdata', namespaces={'ns': CANSAS_NS}) |
---|
[f32d144] | 1516 | if len(nodes) > 1: |
---|
[e9b12eaf] | 1517 | raise RuntimeError, "CanSAS reader is not compatible with multiple SASdata entries" |
---|
[df7ed14] | 1518 | |
---|
| 1519 | for entry in nodes: |
---|
| 1520 | for item in list_of_data_2d_attr: |
---|
| 1521 | #get node |
---|
[f32d144] | 1522 | node = get_content('ns:%s' % item[0], entry) |
---|
| 1523 | exec "data_info.%s = parse_entry_helper(node, item)" % item[1] |
---|
[e9b12eaf] | 1524 | |
---|
[df7ed14] | 1525 | for item in list_of_data2d_values: |
---|
[f32d144] | 1526 | field = get_content('ns:%s' % item[0], entry) |
---|
[df7ed14] | 1527 | list = [] |
---|
| 1528 | if field is not None: |
---|
[0b12abb5] | 1529 | list = [parse_entry_helper(node, item) for node in field] |
---|
[44f7c1b] | 1530 | if len(list) < 2: |
---|
| 1531 | exec "data_info.%s = None" % item[0] |
---|
| 1532 | else: |
---|
| 1533 | exec "data_info.%s = numpy.array(list)" % item[0] |
---|
[e9b12eaf] | 1534 | |
---|
| 1535 | return data_info |
---|
| 1536 | |
---|
[61cada5] | 1537 | def _read_cansas(self, path): |
---|
[f32d144] | 1538 | """ |
---|
[5062bbf] | 1539 | Load data and P(r) information from a CanSAS XML file. |
---|
| 1540 | |
---|
| 1541 | :param path: file path |
---|
| 1542 | |
---|
[f32d144] | 1543 | :return: Data1D object if a single SASentry was found, |
---|
[5062bbf] | 1544 | or a list of Data1D objects if multiple entries were found, |
---|
| 1545 | or None of nothing was found |
---|
| 1546 | |
---|
| 1547 | :raise RuntimeError: when the file can't be opened |
---|
| 1548 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
| 1549 | |
---|
[61cada5] | 1550 | """ |
---|
| 1551 | output = [] |
---|
[f32d144] | 1552 | basename = os.path.basename(path) |
---|
[b63dc6e] | 1553 | root, extension = os.path.splitext(basename) |
---|
| 1554 | ext = extension.lower() |
---|
[61cada5] | 1555 | try: |
---|
| 1556 | if os.path.isfile(path): |
---|
[b63dc6e] | 1557 | |
---|
[61cada5] | 1558 | #TODO: eventually remove the check for .xml once |
---|
| 1559 | # the P(r) writer/reader is truly complete. |
---|
[b63dc6e] | 1560 | if ext in self.ext or \ |
---|
| 1561 | ext == '.xml': |
---|
[61cada5] | 1562 | |
---|
| 1563 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
| 1564 | # Check the format version number |
---|
[f32d144] | 1565 | # Specifying the namespace will take care of the file format version |
---|
[61cada5] | 1566 | root = tree.getroot() |
---|
[f32d144] | 1567 | entry_list = root.xpath('ns:SASentry', |
---|
| 1568 | namespaces={'ns': CANSAS_NS}) |
---|
| 1569 | for entry in entry_list: |
---|
[e9b12eaf] | 1570 | try: |
---|
[ac5b69d] | 1571 | sas_entry, _ = self._parse_save_state_entry(entry) |
---|
[e9b12eaf] | 1572 | except: |
---|
[df7ed14] | 1573 | raise |
---|
[61cada5] | 1574 | fitstate = self._parse_state(entry) |
---|
[b35d3d1] | 1575 | |
---|
| 1576 | #state could be None when .svs file is loaded |
---|
| 1577 | #in this case, skip appending to output |
---|
| 1578 | if fitstate != None: |
---|
| 1579 | sas_entry.meta_data['fitstate'] = fitstate |
---|
| 1580 | sas_entry.filename = fitstate.file |
---|
| 1581 | output.append(sas_entry) |
---|
[61cada5] | 1582 | else: |
---|
[b63dc6e] | 1583 | self.call_back(format=ext) |
---|
[61cada5] | 1584 | raise RuntimeError, "%s is not a file" % path |
---|
[b35d3d1] | 1585 | |
---|
[61cada5] | 1586 | # Return output consistent with the loader's api |
---|
[f32d144] | 1587 | if len(output) == 0: |
---|
| 1588 | self.call_back(state=None, datainfo=None, format=ext) |
---|
[61cada5] | 1589 | return None |
---|
| 1590 | else: |
---|
[b35d3d1] | 1591 | for ind in range(len(output)): |
---|
| 1592 | # Call back to post the new state |
---|
| 1593 | state = output[ind].meta_data['fitstate'] |
---|
| 1594 | t = time.localtime(state.timestamp) |
---|
| 1595 | time_str = time.strftime("%b %d %H:%M", t) |
---|
| 1596 | # Check that no time stamp is already appended |
---|
| 1597 | max_char = state.file.find("[") |
---|
| 1598 | if max_char < 0: |
---|
| 1599 | max_char = len(state.file) |
---|
[ef16f59] | 1600 | original_fname = state.file[0:max_char] |
---|
[f32d144] | 1601 | state.file = original_fname + ' [' + time_str + ']' |
---|
| 1602 | |
---|
[b35d3d1] | 1603 | if state is not None and state.is_data is not None: |
---|
[f32d144] | 1604 | exec 'output[%d].is_data = state.is_data' % ind |
---|
[b35d3d1] | 1605 | |
---|
| 1606 | output[ind].filename = state.file |
---|
| 1607 | state.data = output[ind] |
---|
[f32d144] | 1608 | state.data.name = output[ind].filename # state.data_name |
---|
[b35d3d1] | 1609 | state.data.id = state.data_id |
---|
| 1610 | if state.is_data is not None: |
---|
| 1611 | state.data.is_data = state.is_data |
---|
[f32d144] | 1612 | if output[ind].run_name is not None\ |
---|
| 1613 | and len(output[ind].run_name) != 0: |
---|
[ef16f59] | 1614 | name = output[ind].run_name |
---|
[f32d144] | 1615 | else: |
---|
| 1616 | name = original_fname |
---|
[ef16f59] | 1617 | state.data.group_id = name |
---|
| 1618 | #store state in fitting |
---|
[f32d144] | 1619 | self.call_back(state=state, |
---|
| 1620 | datainfo=output[ind], format=ext) |
---|
| 1621 | self.state = state |
---|
[ef16f59] | 1622 | return output |
---|
[b35d3d1] | 1623 | |
---|
[61cada5] | 1624 | except: |
---|
[4bee68d] | 1625 | self.call_back(format=ext) |
---|
| 1626 | #self.state= state |
---|
[61cada5] | 1627 | raise |
---|
| 1628 | |
---|
| 1629 | def write(self, filename, datainfo=None, fitstate=None): |
---|
| 1630 | """ |
---|
[b35d3d1] | 1631 | Write the content of a Data1D as a CanSAS XML file only for standalone |
---|
[5062bbf] | 1632 | |
---|
| 1633 | :param filename: name of the file to write |
---|
| 1634 | :param datainfo: Data1D object |
---|
| 1635 | :param fitstate: PageState object |
---|
| 1636 | |
---|
[61cada5] | 1637 | """ |
---|
| 1638 | # Sanity check |
---|
| 1639 | if self.cansas == True: |
---|
| 1640 | # Add fitting information to the XML document |
---|
[ef16f59] | 1641 | doc = self.write_toXML(datainfo, fitstate) |
---|
[61cada5] | 1642 | # Write the XML document |
---|
| 1643 | else: |
---|
[ac5b69d] | 1644 | doc = fitstate.toXML(file=filename) |
---|
| 1645 | |
---|
| 1646 | # Save the document no matter the type |
---|
| 1647 | fd = open(filename, 'w') |
---|
| 1648 | fd.write(doc.toprettyxml()) |
---|
| 1649 | fd.close() |
---|
[61cada5] | 1650 | |
---|
[b35d3d1] | 1651 | def write_toXML(self, datainfo=None, state=None): |
---|
| 1652 | """ |
---|
[f32d144] | 1653 | Write toXML, a helper for write(), |
---|
| 1654 | could be used by guimanager._on_save() |
---|
[b35d3d1] | 1655 | |
---|
| 1656 | : return: xml doc |
---|
| 1657 | """ |
---|
[3c44c66] | 1658 | |
---|
[b35d3d1] | 1659 | if state.data is None: |
---|
[79492222] | 1660 | data = sas.dataloader.data_info.Data1D(x=[], y=[]) |
---|
[a805118] | 1661 | return None |
---|
[3b148c3] | 1662 | elif not state.data.is_data: |
---|
| 1663 | return None |
---|
[f32d144] | 1664 | else: |
---|
[b35d3d1] | 1665 | #make sure title and data run is filled up. |
---|
[f32d144] | 1666 | if state.data.title == None or state.data.title == '': |
---|
| 1667 | state.data.title = state.data.name |
---|
| 1668 | if state.data.run_name == None or state.data.run_name == {}: |
---|
[028a0e8] | 1669 | state.data.run = [str(state.data.name)] |
---|
| 1670 | state.data.run_name[0] = state.data.name |
---|
[ac5b69d] | 1671 | |
---|
[f32d144] | 1672 | if issubclass(state.data.__class__, |
---|
[79492222] | 1673 | sas.dataloader.data_info.Data1D): |
---|
[b35d3d1] | 1674 | data = state.data |
---|
| 1675 | doc, sasentry = self._to_xml_doc(data) |
---|
| 1676 | else: |
---|
| 1677 | data = state.data |
---|
| 1678 | doc, sasentry = self._data2d_to_xml_doc(data) |
---|
| 1679 | |
---|
| 1680 | if state is not None: |
---|
[ac5b69d] | 1681 | doc = state.toXML(doc=doc, file=data.filename, entry_node=sasentry) |
---|
| 1682 | |
---|
[f32d144] | 1683 | return doc |
---|
[2296316] | 1684 | |
---|
[f32d144] | 1685 | # Simple html report templet |
---|
[2296316] | 1686 | HEADER = "<html>\n" |
---|
| 1687 | HEADER += "<head>\n" |
---|
| 1688 | HEADER += "<meta http-equiv=Content-Type content='text/html; " |
---|
| 1689 | HEADER += "charset=windows-1252'> \n" |
---|
| 1690 | HEADER += "<meta name=Generator >\n" |
---|
| 1691 | HEADER += "</head>\n" |
---|
| 1692 | HEADER += "<body lang=EN-US>\n" |
---|
| 1693 | HEADER += "<div class=WordSection1>\n" |
---|
[14d2daa] | 1694 | HEADER += "<p class=MsoNormal><b><span ><center><font size='4' >" |
---|
| 1695 | HEADER += "%s</font></center></span></center></b></p>" |
---|
[2296316] | 1696 | HEADER += "<p class=MsoNormal> </p>" |
---|
[14d2daa] | 1697 | PARA = "<p class=MsoNormal><font size='4' > %s \n" |
---|
| 1698 | PARA += "</font></p>" |
---|
| 1699 | CENTRE = "<p class=MsoNormal><center><font size='4' > %s \n" |
---|
| 1700 | CENTRE += "</font></center></p>" |
---|
[2296316] | 1701 | FEET_1 = \ |
---|
| 1702 | """ |
---|
[3a3dd9c] | 1703 | <p class=MsoNormal> </p> |
---|
[14d2daa] | 1704 | <br> |
---|
| 1705 | <p class=MsoNormal><b><span ><center> <font size='4' > Graph |
---|
[f32d144] | 1706 | </font></span></center></b></p> |
---|
| 1707 | <p class=MsoNormal> </p> |
---|
| 1708 | <center> |
---|
[14d2daa] | 1709 | <br><font size='4' >Model Computation</font> |
---|
[f32d144] | 1710 | <br><font size='4' >Data: "%s"</font><br> |
---|
[2296316] | 1711 | """ |
---|
| 1712 | FEET_2 = \ |
---|
| 1713 | """ |
---|
[f32d144] | 1714 | <img src="%s" > |
---|
[2296316] | 1715 | </img> |
---|
| 1716 | """ |
---|
| 1717 | FEET_3 = \ |
---|
| 1718 | """ |
---|
[f32d144] | 1719 | </center> |
---|
[2296316] | 1720 | </div> |
---|
| 1721 | </body> |
---|
| 1722 | </html> |
---|
| 1723 | """ |
---|
| 1724 | ELINE = "<p class=MsoNormal> </p>" |
---|
| 1725 | |
---|
[3c44c66] | 1726 | if __name__ == "__main__": |
---|
| 1727 | state = PageState(parent=None) |
---|
| 1728 | #state.toXML() |
---|
| 1729 | """ |
---|
[5062bbf] | 1730 | |
---|
[3c44c66] | 1731 | file = open("test_state", "w") |
---|
| 1732 | pickle.dump(state, file) |
---|
| 1733 | print pickle.dumps(state) |
---|
| 1734 | state.data_name = "hello---->" |
---|
| 1735 | pickle.dump(state, file) |
---|
| 1736 | file = open("test_state", "r") |
---|
| 1737 | new_state= pickle.load(file) |
---|
| 1738 | print "new state", new_state |
---|
| 1739 | new_state= pickle.load(file) |
---|
| 1740 | print "new state", new_state |
---|
| 1741 | #print "state", state |
---|
| 1742 | """ |
---|
| 1743 | import bsddb |
---|
| 1744 | import pickle |
---|
[f32d144] | 1745 | db = bsddb.btopen('file_state.db', 'c') |
---|
[3c44c66] | 1746 | val = (pickle.dumps(state), "hello", "hi") |
---|
[f32d144] | 1747 | db['state1'] = pickle.dumps(val) |
---|
[3c44c66] | 1748 | print pickle.loads(db['state1']) |
---|
| 1749 | state.data_name = "hello---->22" |
---|
[f32d144] | 1750 | db['state2'] = pickle.dumps(state) |
---|
[3c44c66] | 1751 | state.data_name = "hello---->2" |
---|
[f32d144] | 1752 | db['state3'] = pickle.dumps(state) |
---|
[3c44c66] | 1753 | del db['state3'] |
---|
| 1754 | state.data_name = "hello---->3" |
---|
[f32d144] | 1755 | db['state4'] = pickle.dumps(state) |
---|
[3c44c66] | 1756 | new_state = pickle.loads(db['state1']) |
---|
| 1757 | #print db.last() |
---|
| 1758 | db.set_location('state2') |
---|
| 1759 | state.data_name = "hello---->5" |
---|
[f32d144] | 1760 | db['aastate5'] = pickle.dumps(state) |
---|
[3c44c66] | 1761 | db.keys().sort() |
---|
| 1762 | print pickle.loads(db['state2']) |
---|
| 1763 | |
---|
[f32d144] | 1764 | db.close() |
---|