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