Changeset 2f4b430 in sasview for src/sas/perspectives/fitting/pagestate.py
- Timestamp:
- Mar 4, 2015 3:39:45 PM (10 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 74f419d
- Parents:
- 2f732b0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/perspectives/fitting/pagestate.py
rd838715 r2f4b430 45 45 ["qmax", "qmax", "float"], 46 46 ["npts", "npts", "float"], 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 47 ["categorycombobox", "categorycombobox", "string"], 52 48 ["formfactorcombobox", "formfactorcombobox", "string"], 53 49 ["structurecombobox", "structurecombobox", "string"], 54 ["multi_factor", "multi_factor","float"],55 ["magnetic_on", "magnetic_on", "bool"],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"],62 ["dI_noweight", "dI_noweight","bool"],63 ["dI_didata", "dI_didata","bool"],64 ["dI_sqrdata", "dI_sqrdata","bool"],65 ["dI_idata", "dI_idata","bool"],66 ["enable2D", "enable2D","bool"],67 ["cb1", "cb1","bool"],68 ["tcChi", "tcChi","float"],50 ["multi_factor", "multi_factor", "float"], 51 ["magnetic_on", "magnetic_on", "bool"], 52 ["enable_smearer", "enable_smearer", "bool"], 53 ["disable_smearer", "disable_smearer", "bool"], 54 ["pinhole_smearer", "pinhole_smearer", "bool"], 55 ["slit_smearer", "slit_smearer", "bool"], 56 ["enable_disp", "enable_disp", "bool"], 57 ["disable_disp", "disable_disp", "bool"], 58 ["dI_noweight", "dI_noweight", "bool"], 59 ["dI_didata", "dI_didata", "bool"], 60 ["dI_sqrdata", "dI_sqrdata", "bool"], 61 ["dI_idata", "dI_idata", "bool"], 62 ["enable2D", "enable2D", "bool"], 63 ["cb1", "cb1", "bool"], 64 ["tcChi", "tcChi", "float"], 69 65 ["smearer", "smearer", "float"], 70 ["smear_type", "smear_type", "string"],66 ["smear_type", "smear_type", "string"], 71 67 ["dq_l", "dq_l", "string"], 72 ["dq_r", "dq_r", "string"],68 ["dq_r", "dq_r", "string"], 73 69 ["dx_max", "dx_max", "float"], 74 ["dx_min", "dx_min", "float"],70 ["dx_min", "dx_min", "float"], 75 71 ["dxl", "dxl", "float"], 76 ["dxw", "dxw", "float"]]72 ["dxw", "dxw", "float"]] 77 73 78 74 list_of_model_attributes = [["values", "values"], … … 110 106 """ 111 107 Create a numpy list from value extrated from the node 112 108 113 109 :param node: node from each the value is stored 114 110 :param item: list name of three strings.the two first are name of data 115 111 attribute and the third one is the type of the value of that 116 112 attribute. type can be string, float, bool, etc. 117 113 118 114 : return: numpy array 119 115 """ … … 124 120 try: 125 121 return node.get(item[0]).strip() == "True" 126 122 127 123 except: 128 124 return None … … 132 128 except: 133 129 return None 134 135 130 131 136 132 class PageState(object): 137 133 """ … … 141 137 """ 142 138 Initialize the current state 143 139 144 140 :param model: a selected model within a page 145 141 :param data: 146 142 147 143 """ 148 144 self.file = None … … 159 155 self.is_2D = False 160 156 self.images = None 161 157 162 158 #save additional information on data that dataloader.reader does not read 163 159 self.is_data = None … … 172 168 if self.data is not None and hasattr(self.data, "group_id"): 173 169 self.data_group_id = self.data.group_id 174 170 175 171 ## reset True change the state of exsiting button 176 172 self.reset = False 177 173 178 174 #engine type 179 175 self.engine_type = None … … 225 221 self.values = {} 226 222 self.weights = {} 227 223 228 224 #contains link between a model and selected parameters to fit 229 225 self.param_toFit = [] … … 251 247 self.qmax_x = None 252 248 self.qmin_x = None 253 249 254 250 self.npts = None 255 251 self.name = "" … … 269 265 self.enable_disp = False 270 266 self.disable_disp = True 271 267 272 268 ## state of selected all check button 273 269 self.cb1 = False 274 270 ## store value of chisqr 275 271 self.tcChi = None 276 272 277 273 def clone(self): 278 274 """ … … 292 288 obj.model_list_box = copy.deepcopy(self.model_list_box) 293 289 obj.engine_type = copy.deepcopy(self.engine_type) 294 290 295 291 obj.categorycombobox = self.categorycombobox 296 292 obj.formfactorcombobox = self.formfactorcombobox 297 293 obj.structurecombobox = self.structurecombobox 298 294 299 295 #obj.shape_rbutton = self.shape_rbutton 300 296 #obj.shape_indep_rbutton = self.shape_indep_rbutton 301 297 #obj.struct_rbutton = self.struct_rbutton 302 298 #obj.plugin_rbutton = self.plugin_rbutton 303 299 304 300 obj.manager = self.manager 305 301 obj.event_owner = self.event_owner 306 302 obj.disp_list = copy.deepcopy(self.disp_list) 307 303 308 304 obj.enable2D = copy.deepcopy(self.enable2D) 309 305 obj.parameters = copy.deepcopy(self.parameters) … … 316 312 obj.disable_disp = copy.deepcopy(self.disable_disp) 317 313 obj.tcChi = self.tcChi 318 314 319 315 if len(self._disp_obj_dict) > 0: 320 316 for k, v in self._disp_obj_dict.iteritems(): … … 343 339 obj.dx_min = copy.deepcopy(self.dx_min) 344 340 obj.dxl = copy.deepcopy(self.dxl) 345 obj.dxw = copy.deepcopy(self.dxw) 341 obj.dxw = copy.deepcopy(self.dxw) 346 342 obj.disp_box = copy.deepcopy(self.disp_box) 347 343 obj.qmin = copy.deepcopy(self.qmin) … … 352 348 obj.cb1 = copy.deepcopy(self.cb1) 353 349 obj.smearer = copy.deepcopy(self.smearer) 354 350 355 351 for name, state in self.saved_states.iteritems(): 356 352 copy_name = copy.deepcopy(name) … … 358 354 obj.saved_states[copy_name] = copy_state 359 355 return obj 360 356 361 357 def _repr_helper(self, list, rep): 362 358 """ … … 375 371 rep += "parameter unit: %s\n\n" % str(item[7]) 376 372 return rep 377 373 378 374 def __repr__(self): 379 375 """ … … 399 395 #rep += "model type (form factor) selected: %s\n" % self.shape_rbutton 400 396 rep += "multi_factor : %s\n" % str(self.multi_factor) 401 rep += "magnetic_on : %s\n" % str(self.magnetic_on)397 rep += "magnetic_on : %s\n" % str(self.magnetic_on) 402 398 rep += "model type (Category) selected: %s\n" % self.categorycombobox 403 399 #rep += "model type (shape independent) selected: %s\n" % self.shape_indep_rbutton … … 415 411 rep += "Dispersity disable : %s\n" % str(self.disable_disp) 416 412 rep += "Slit smearer enable: %s\n" % str(self.slit_smearer) 417 413 418 414 rep += "dI_noweight : %s\n" % str(self.dI_noweight) 419 415 rep += "dI_didata : %s\n" % str(self.dI_didata) 420 416 rep += "dI_sqrdata : %s\n" % str(self.dI_sqrdata) 421 417 rep += "dI_idata : %s\n" % str(self.dI_idata) 422 418 423 419 rep += "2D enable : %s\n" % str(self.enable2D) 424 420 rep += "All parameters checkbox selected: %s\n" % (self.cb1) … … 429 425 rep += "dq_r : %s\n" % self.dq_r 430 426 rep += "dx_max : %s\n" % str(self.dx_max) 431 rep += "dx_min : %s\n" % str(self.dx_min) 427 rep += "dx_min : %s\n" % str(self.dx_min) 432 428 rep += "dxl : %s\n" % str(self.dxl) 433 rep += "dxw : %s\n" % str(self.dxw) 429 rep += "dxw : %s\n" % str(self.dxw) 434 430 rep += "model : %s\n\n" % str(self.model) 435 431 temp_parameters = [] … … 450 446 temp_parameters = self.parameters 451 447 temp_fittable_param = self.fittable_param 452 448 453 449 rep += "number parameters(self.parameters): %s\n" % len(temp_parameters) 454 450 rep = self._repr_helper(list=temp_parameters, rep=rep) … … 550 546 modelname = "Model name:" + " NAN" 551 547 model_name = CENTRE % modelname 552 548 553 549 if name == "Plotting Range": 554 550 try: … … 572 568 paramval += param + "\n" 573 569 paramval_string += CENTRE % param + "\n" 574 570 575 571 text_string = "\n\n\n" + title + "\n\n" + file + \ 576 572 "\n" + q_name + \ 577 573 "\n" + chi2 + \ 578 574 "\n\n" + paramval 579 575 580 576 title_name = self._check_html_format(title_name) 581 577 file_name = self._check_html_format(file_name) 582 578 title = self._check_html_format(title) 583 579 584 580 html_string = title_name + "\n" + file_name + \ 585 581 "\n" + model_name + \ … … 591 587 "\n" + FEET_1 % title + \ 592 588 "\n" + FEET_2 593 589 594 590 return html_string, text_string, title 595 591 596 592 def _check_html_format(self, name): 597 593 """ … … 600 596 if name.count('%'): 601 597 name = name.replace('%', '%') 602 598 603 599 return name 604 600 605 601 def report(self, figs=None, canvases=None): 606 602 """ 607 603 Invoke report dialog panel 608 604 609 605 : param figs: list of pylab figures [list] 610 606 """ … … 638 634 dialog = ReportDialog(report_list, None, -1, "") 639 635 dialog.Show() 640 636 641 637 def _toXML_helper(self, thelist, element, newdoc): 642 638 """ … … 656 652 sub_element.setAttribute('unit', str(item[7])) 657 653 element.appendChild(sub_element) 658 654 659 655 def toXML(self, file="fitting_state.fitv", doc=None, entry_node=None): 660 656 """ 661 657 Writes the state of the InversionControl panel to file, as XML. 662 658 663 659 Compatible with standalone writing, or appending to an 664 660 already existing XML document. In that case, the XML document 665 661 is required. An optional entry node in the XML document may also be given. 666 662 667 663 :param file: file to write to 668 664 :param doc: XML document object [optional] 669 665 :param entry_node: XML node within the XML document at which we will append the data [optional] 670 666 671 667 """ 672 668 from xml.dom.minidom import getDOMImplementation … … 697 693 entry_node = node_list.item(0) 698 694 entry_node.appendChild(top_element) 699 695 700 696 attr = newdoc.createAttribute("version") 701 697 attr.nodeValue = '1.0' 702 698 top_element.setAttributeNode(attr) 703 699 704 700 # File name 705 701 element = newdoc.createElement("filename") … … 709 705 element.appendChild(newdoc.createTextNode(str(file))) 710 706 top_element.appendChild(element) 711 707 712 708 element = newdoc.createElement("timestamp") 713 709 element.appendChild(newdoc.createTextNode(time.ctime(self.timestamp))) … … 719 715 inputs = newdoc.createElement("Attributes") 720 716 top_element.appendChild(inputs) 721 717 722 718 if self.data is not None and hasattr(self.data, "group_id"): 723 719 self.data_group_id = self.data.group_id … … 728 724 if self.data is not None and hasattr(self.data, "id"): 729 725 self.data_id = self.data.id 730 726 731 727 for item in list_of_data_attributes: 732 728 element = newdoc.createElement(item[0]) 733 729 exec "element.setAttribute(item[0], str(self.%s))" % (item[1]) 734 730 inputs.appendChild(element) 735 731 736 732 for item in list_of_state_attributes: 737 733 element = newdoc.createElement(item[0]) 738 734 exec "element.setAttribute(item[0], str(self.%s))" % (item[1]) 739 735 inputs.appendChild(element) 740 736 741 737 # For self.values ={ disp_param_name: [vals,...],...} 742 738 # and for self.weights ={ disp_param_name: [weights,...],...} … … 752 748 com += "(newdoc.createTextNode(str(%s)))" 753 749 exec com % val 754 750 755 751 element.appendChild(sub_element) 756 752 inputs.appendChild(element) 757 753 758 754 # Create doc for the dictionary of self._disp_obj_dic 759 755 for item in list_of_obj_dic: … … 767 763 element.appendChild(sub_element) 768 764 inputs.appendChild(element) 769 765 770 766 for item in list_of_state_parameters: 771 767 element = newdoc.createElement(item[0]) … … 774 770 exec com % item[1] 775 771 inputs.appendChild(element) 776 772 777 773 # Save the file 778 774 if doc is None: … … 783 779 else: 784 780 return newdoc 785 781 786 782 def _fromXML_helper(self, node, list): 787 783 """ … … 833 829 [minimum_displayed, minimum_value], 834 830 [maximum_displayed, maximum_value], unit]) 835 831 836 832 def fromXML(self, file=None, node=None): 837 833 """ 838 834 Load fitting state from a file 839 835 840 836 :param file: .fitv file 841 837 :param node: node of a XML document to read from 842 838 843 839 """ 844 840 if file is not None: … … 846 842 msg += " format for fitting files" 847 843 raise RuntimeError, msg 848 844 849 845 if node.get('version')and node.get('version') == '1.0': 850 846 851 847 # Get file name 852 848 entry = get_content('ns:filename', node) 853 849 if entry is not None: 854 850 self.file = entry.text.strip() 855 851 856 852 # Get time stamp 857 853 entry = get_content('ns:timestamp', node) … … 863 859 msg += " read timestamp\n %s" % sys.exc_value 864 860 logging.error(msg) 865 861 866 862 # Parse fitting attributes 867 863 entry = get_content('ns:Attributes', node) … … 870 866 try: 871 867 exec "self.%s = parse_entry_helper(node, item)" % item[0] 872 868 873 869 except: 874 870 raise 875 871 876 872 if entry is not None: 877 873 878 874 for item in list_of_state_attributes: 879 875 node = get_content('ns:%s' % item[0], entry) … … 883 879 except: 884 880 raise 885 881 886 882 for item in list_of_state_parameters: 887 883 node = get_content("ns:%s" % item[0], entry) 888 884 exec "self._fromXML_helper(node=node, list=self.%s)" % \ 889 885 item[1] 890 886 891 887 # Recover _disp_obj_dict from xml file 892 888 self._disp_obj_dict = {} … … 910 906 except: 911 907 pass 912 908 913 909 # get self.values and self.weights dic. if exists 914 910 for item in list_of_model_attributes: … … 929 925 dic[name] = numpy.array(list) 930 926 exec "self.%s = dic" % item[1] 931 927 932 928 def set_plot_state(self, figs, canvases): 933 929 """ … … 943 939 self.imgRAM = None 944 940 wx.MemoryFSHandler() 945 941 946 942 # For no figures in the list, prepare empty plot 947 943 if figs == None or len(figs) == 0: 948 944 figs = [None] 949 945 950 946 # Loop over the list of figures 951 947 # use wx.MemoryFSHandler … … 955 951 ind = figs.index(fig) 956 952 canvas = canvases[ind] 957 953 958 954 #store the image in wx.FileSystem Object 959 955 wx.FileSystem.AddHandler(wx.MemoryFSHandler()) 960 956 961 957 # index of the fig 962 958 ind = figs.index(fig) 963 959 964 960 #AddFile, image can be retrieved with 'memory:filename' 965 961 self.imgRAM.AddFile('img_fit%s.png' % ind, 966 962 canvas.bitmap, wx.BITMAP_TYPE_PNG) 967 963 968 964 #append figs 969 965 images.append(fig) 970 966 971 967 return images 972 968 … … 978 974 ## File type 979 975 type_name = "Fitting" 980 976 981 977 ## Wildcards 982 978 type = ["Fitting files (*.fitv)|*.fitv" … … 984 980 ## List of allowed extensions 985 981 ext = ['.fitv', '.FITV', '.svs', 'SVS'] 986 982 987 983 def __init__(self, call_back=None, cansas=True): 988 984 CansasReader.__init__(self) … … 990 986 Initialize the call-back method to be called 991 987 after we load a file 992 988 993 989 :param call_back: call-back method 994 990 :param cansas: True = files will be written/read in CanSAS format 995 991 False = write CanSAS format 996 992 997 993 """ 998 994 ## Call back method to be executed after a file is read … … 1001 997 self.cansas = cansas 1002 998 self.state = None 1003 999 1004 1000 def get_state(self): 1005 1001 return self.state 1006 1002 1007 1003 def read(self, path): 1008 1004 """ 1009 1005 Load a new P(r) inversion state from file 1010 1006 1011 1007 :param path: file path 1012 1008 1013 1009 """ 1014 1010 if self.cansas == True: 1015 1011 return self._read_cansas(path) 1016 1012 1017 1013 def _data2d_to_xml_doc(self, datainfo): 1018 1014 """ 1019 1015 Create an XML document to contain the content of a Data2D 1020 1016 1021 1017 :param datainfo: Data2D object 1022 1018 1023 1019 """ 1024 1020 if not issubclass(datainfo.__class__, Data2D): 1025 1021 raise RuntimeError, "The cansas writer expects a Data2D instance" 1026 1022 1027 1023 doc = xml.dom.minidom.Document() 1028 1024 main_node = doc.createElement("SASroot") … … 1031 1027 main_node.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") 1032 1028 main_node.setAttribute("xsi:schemaLocation", "cansas1d/%s http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd" % self.version) 1033 1029 1034 1030 doc.appendChild(main_node) 1035 1031 1036 1032 entry_node = doc.createElement("SASentry") 1037 1033 main_node.appendChild(entry_node) 1038 1034 1039 1035 write_node(doc, entry_node, "Title", datainfo.title) 1040 1036 if datainfo is not None: … … 1042 1038 for item in datainfo.run: 1043 1039 runname = {} 1044 if datainfo.run_name.has_key(item) and len(str(datainfo.run_name[item])) >1:1040 if datainfo.run_name.has_key(item) and len(str(datainfo.run_name[item])) > 1: 1045 1041 runname = {'name': datainfo.run_name[item] } 1046 1042 write_node(doc, entry_node, "Run", item, runname) … … 1052 1048 exec "element.setAttribute(item[0], str(datainfo.%s))" % (item[1]) 1053 1049 new_node.appendChild(element) 1054 1050 1055 1051 for item in list_of_data2d_values: 1056 1052 root_node = doc.createElement(item[0]) … … 1059 1055 exec "temp_list = datainfo.%s" % item[1] 1060 1056 1061 if temp_list is None or len(temp_list) == 0:1057 if temp_list is None or len(temp_list) == 0: 1062 1058 element = doc.createElement(item[0]) 1063 1059 exec "element.appendChild(doc.createTextNode(str(%s)))" % temp_list … … 1068 1064 exec "element.setAttribute(item[0], str(%s))" % value 1069 1065 root_node.appendChild(element) 1070 1066 1071 1067 # Sample info 1072 1068 sample = doc.createElement("SASsample") … … 1080 1076 write_node(doc, sample, "temperature", datainfo.sample.temperature, 1081 1077 {"unit": datainfo.sample.temperature_unit}) 1082 1078 1083 1079 for item in datainfo.sample.details: 1084 1080 write_node(doc, sample, "details", item) 1085 1081 1086 1082 pos = doc.createElement("position") 1087 1083 written = write_node(doc, pos, "x", datainfo.sample.position.x, … … 1095 1091 if written == True: 1096 1092 sample.appendChild(pos) 1097 1093 1098 1094 ori = doc.createElement("orientation") 1099 1095 written = write_node(doc, ori, "roll", datainfo.sample.orientation.x, … … 1107 1103 if written == True: 1108 1104 sample.appendChild(ori) 1109 1105 1110 1106 # Instrument info 1111 1107 instr = doc.createElement("SASinstrument") 1112 1108 entry_node.appendChild(instr) 1113 1109 1114 1110 write_node(doc, instr, "name", datainfo.instrument) 1115 1111 1116 1112 # Source 1117 1113 source = doc.createElement("SASsource") … … 1119 1115 source.setAttribute("name", str(datainfo.source.name)) 1120 1116 instr.appendChild(source) 1121 1117 1122 1118 write_node(doc, source, "radiation", datainfo.source.radiation) 1123 1119 write_node(doc, source, "beam_shape", datainfo.source.beam_shape) … … 1135 1131 if written == True: 1136 1132 source.appendChild(size) 1137 1133 1138 1134 write_node(doc, source, "wavelength", datainfo.source.wavelength, 1139 1135 {"unit": datainfo.source.wavelength_unit}) … … 1147 1143 datainfo.source.wavelength_spread, 1148 1144 {"unit": datainfo.source.wavelength_spread_unit}) 1149 1145 1150 1146 # Collimation 1151 1147 for item in datainfo.collimation: … … 1154 1150 coll.setAttribute("name", str(item.name)) 1155 1151 instr.appendChild(coll) 1156 1152 1157 1153 write_node(doc, coll, "length", item.length, 1158 1154 {"unit": item.length_unit}) 1159 1155 1160 1156 for apert in item.aperture: 1161 1157 ap = doc.createElement("aperture") … … 1165 1161 ap.setAttribute("type", str(apert.type)) 1166 1162 coll.appendChild(ap) 1167 1163 1168 1164 write_node(doc, ap, "distance", apert.distance, 1169 1165 {"unit": apert.distance_unit}) 1170 1166 1171 1167 size = doc.createElement("size") 1172 1168 if apert.size_name is not None: … … 1192 1188 if written == True: 1193 1189 instr.appendChild(det) 1194 1190 1195 1191 off = doc.createElement("offset") 1196 1192 written = write_node(doc, off, "x", item.offset.x, … … 1202 1198 if written == True: 1203 1199 det.appendChild(off) 1204 1200 1205 1201 center = doc.createElement("beam_center") 1206 1202 written = write_node(doc, center, "x", item.beam_center.x, … … 1214 1210 if written == True: 1215 1211 det.appendChild(center) 1216 1212 1217 1213 pix = doc.createElement("pixel_size") 1218 1214 written = write_node(doc, pix, "x", item.pixel_size.x, … … 1224 1220 if written == True: 1225 1221 det.appendChild(pix) 1226 1222 1227 1223 ori = doc.createElement("orientation") 1228 1224 written = write_node(doc, ori, "roll", item.orientation.x, … … 1235 1231 if written == True: 1236 1232 det.appendChild(ori) 1237 1233 1238 1234 # Processes info 1239 1235 for item in datainfo.process: … … 1253 1249 # the data we just wrote 1254 1250 return doc, entry_node 1255 1251 1256 1252 def _parse_state(self, entry): 1257 1253 """ 1258 1254 Read a fit result from an XML node 1259 1255 1260 1256 :param entry: XML node to read from 1261 1257 1262 1258 :return: PageState object 1263 1259 """ 1264 1260 # Create an empty state 1265 state = None 1261 state = None 1266 1262 # Locate the P(r) node 1267 1263 try: … … 1272 1268 state = PageState() 1273 1269 state.fromXML(node=nodes[0]) 1274 1270 1275 1271 except: 1276 1272 logging.info("XML document does not contain fitting information.\n %s" % sys.exc_value) 1277 1273 1278 1274 return state 1279 1275 1280 1276 def _parse_save_state_entry(self, dom): 1281 1277 """ 1282 1278 Parse a SASentry 1283 1279 1284 1280 :param node: SASentry node 1285 1281 1286 1282 :return: Data1D/Data2D object 1287 1283 1288 1284 """ 1289 1285 node = dom.xpath('ns:data_class', namespaces={'ns': CANSAS_NS}) … … 1305 1301 numpy.trim_zeros(return_value.dxl) 1306 1302 numpy.trim_zeros(return_value.dxw) 1307 1303 1308 1304 return return_value, _ 1309 1305 1310 1306 #Parse 2D 1311 1307 data_info = Data2D() 1312 1308 1313 1309 # Look up title 1314 1310 self._store_content('ns:Title', dom, 'title', data_info) 1315 1311 1316 1312 # Look up run number 1317 1313 nodes = dom.xpath('ns:Run', namespaces={'ns': CANSAS_NS}) … … 1323 1319 if item.get('name') is not None: 1324 1320 data_info.run_name[value] = item.get('name') 1325 1321 1326 1322 # Look up instrument name 1327 1323 self._store_content('ns:SASinstrument/ns:name', dom, … … 1340 1336 self.errors.append(err_mess) 1341 1337 logging.error(err_mess) 1342 1338 1343 1339 # Sample info ################### 1344 1340 entry = get_content('ns:SASsample', dom) 1345 1341 if entry is not None: 1346 1342 data_info.sample.name = entry.get('name') 1347 1343 1348 1344 self._store_content('ns:SASsample/ns:ID', 1349 1345 dom, 'ID', data_info.sample) … … 1354 1350 self._store_float('ns:SASsample/ns:temperature', 1355 1351 dom, 'temperature', data_info.sample) 1356 1352 1357 1353 nodes = dom.xpath('ns:SASsample/ns:details', 1358 1354 namespaces={'ns': CANSAS_NS}) … … 1367 1363 self.errors.append(err_mess) 1368 1364 logging.error(err_mess) 1369 1365 1370 1366 # Position (as a vector) 1371 1367 self._store_float('ns:SASsample/ns:position/ns:x', … … 1375 1371 self._store_float('ns:SASsample/ns:position/ns:z', 1376 1372 dom, 'position.z', data_info.sample) 1377 1373 1378 1374 # Orientation (as a vector) 1379 1375 self._store_float('ns:SASsample/ns:orientation/ns:roll', … … 1383 1379 self._store_float('ns:SASsample/ns:orientation/ns:yaw', 1384 1380 dom, 'orientation.z', data_info.sample) 1385 1381 1386 1382 # Source info ################### 1387 1383 entry = get_content('ns:SASinstrument/ns:SASsource', dom) 1388 1384 if entry is not None: 1389 1385 data_info.source.name = entry.get('name') 1390 1386 1391 1387 self._store_content('ns:SASinstrument/ns:SASsource/ns:radiation', 1392 1388 dom, 'radiation', data_info.source) … … 1401 1397 self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_spread', 1402 1398 dom, 'wavelength_spread', data_info.source) 1403 1399 1404 1400 # Beam size (as a vector) 1405 1401 entry = get_content('ns:SASinstrument/ns:SASsource/ns:beam_size', dom) 1406 1402 if entry is not None: 1407 1403 data_info.source.beam_size_name = entry.get('name') 1408 1404 1409 1405 self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:x', 1410 1406 dom, 'beam_size.x', data_info.source) … … 1413 1409 self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:z', 1414 1410 dom, 'beam_size.z', data_info.source) 1415 1411 1416 1412 # Collimation info ################### 1417 1413 nodes = dom.xpath('ns:SASinstrument/ns:SAScollimation', … … 1422 1418 collim.name = item.get('name') 1423 1419 self._store_float('ns:length', item, 'length', collim) 1424 1420 1425 1421 # Look for apertures 1426 1422 apert_list = item.xpath('ns:aperture', … … 1428 1424 for apert in apert_list: 1429 1425 aperture = Aperture() 1430 1426 1431 1427 # Get the name and type of the aperture 1432 1428 aperture.name = apert.get('name') 1433 1429 aperture.type = apert.get('type') 1434 1430 1435 1431 self._store_float('ns:distance', apert, 'distance', aperture) 1436 1432 1437 1433 entry = get_content('ns:size', apert) 1438 1434 if entry is not None: 1439 1435 aperture.size_name = entry.get('name') 1440 1436 1441 1437 self._store_float('ns:size/ns:x', apert, 'size.x', aperture) 1442 1438 self._store_float('ns:size/ns:y', apert, 'size.y', aperture) 1443 1439 self._store_float('ns:size/ns:z', apert, 'size.z', aperture) 1444 1440 1445 1441 collim.aperture.append(aperture) 1446 1442 1447 1443 data_info.collimation.append(collim) 1448 1444 1449 1445 # Detector info ###################### 1450 1446 nodes = dom.xpath('ns:SASinstrument/ns:SASdetector', 1451 1447 namespaces={'ns': CANSAS_NS}) 1452 1448 for item in nodes: 1453 1449 1454 1450 detector = Detector() 1455 1451 1456 1452 self._store_content('ns:name', item, 'name', detector) 1457 1453 self._store_float('ns:SDD', item, 'distance', detector) 1458 1454 1459 1455 # Detector offset (as a vector) 1460 1456 self._store_float('ns:offset/ns:x', item, 'offset.x', detector) 1461 1457 self._store_float('ns:offset/ns:y', item, 'offset.y', detector) 1462 1458 self._store_float('ns:offset/ns:z', item, 'offset.z', detector) 1463 1459 1464 1460 # Detector orientation (as a vector) 1465 1461 self._store_float('ns:orientation/ns:roll', item, … … 1469 1465 self._store_float('ns:orientation/ns:yaw', item, 1470 1466 'orientation.z', detector) 1471 1467 1472 1468 # Beam center (as a vector) 1473 1469 self._store_float('ns:beam_center/ns:x', item, … … 1477 1473 self._store_float('ns:beam_center/ns:z', item, 1478 1474 'beam_center.z', detector) 1479 1475 1480 1476 # Pixel size (as a vector) 1481 1477 self._store_float('ns:pixel_size/ns:x', item, … … 1485 1481 self._store_float('ns:pixel_size/ns:z', item, 1486 1482 'pixel_size.z', detector) 1487 1483 1488 1484 self._store_float('ns:slit_length', item, 'slit_length', detector) 1489 1485 1490 1486 data_info.detector.append(detector) 1491 1487 … … 1497 1493 self._store_content('ns:date', item, 'date', process) 1498 1494 self._store_content('ns:description', item, 'description', process) 1499 1495 1500 1496 term_list = item.xpath('ns:term', namespaces={'ns': CANSAS_NS}) 1501 1497 for term in term_list: … … 1511 1507 self.errors.append(err_mess) 1512 1508 logging.error(err_mess) 1513 1509 1514 1510 note_list = item.xpath('ns:SASprocessnote', 1515 1511 namespaces={'ns': CANSAS_NS}) … … 1517 1513 if note.text is not None: 1518 1514 process.notes.append(note.text.strip()) 1519 1515 1520 1516 data_info.process.append(process) 1521 1517 1522 1518 # Data info ###################### 1523 1519 nodes = dom.xpath('ns:SASdata', namespaces={'ns': CANSAS_NS}) 1524 1520 if len(nodes) > 1: 1525 1521 raise RuntimeError, "CanSAS reader is not compatible with multiple SASdata entries" 1526 1522 1527 1523 for entry in nodes: 1528 1524 for item in list_of_data_2d_attr: … … 1530 1526 node = get_content('ns:%s' % item[0], entry) 1531 1527 exec "data_info.%s = parse_entry_helper(node, item)" % item[1] 1532 1528 1533 1529 for item in list_of_data2d_values: 1534 1530 field = get_content('ns:%s' % item[0], entry) … … 1540 1536 else: 1541 1537 exec "data_info.%s = numpy.array(list)" % item[0] 1542 1538 1543 1539 return data_info 1544 1540 … … 1546 1542 """ 1547 1543 Load data and P(r) information from a CanSAS XML file. 1548 1544 1549 1545 :param path: file path 1550 1546 1551 1547 :return: Data1D object if a single SASentry was found, 1552 1548 or a list of Data1D objects if multiple entries were found, 1553 1549 or None of nothing was found 1554 1550 1555 1551 :raise RuntimeError: when the file can't be opened 1556 1552 :raise ValueError: when the length of the data vectors are inconsistent 1557 1553 1558 1554 """ 1559 1555 output = [] … … 1563 1559 try: 1564 1560 if os.path.isfile(path): 1565 1561 1566 1562 #TODO: eventually remove the check for .xml once 1567 1563 # the P(r) writer/reader is truly complete. 1568 1564 if ext in self.ext or \ 1569 1565 ext == '.xml': 1570 1566 1571 1567 tree = etree.parse(path, parser=etree.ETCompatXMLParser()) 1572 1568 # Check the format version number … … 1581 1577 raise 1582 1578 fitstate = self._parse_state(entry) 1583 1579 1584 1580 #state could be None when .svs file is loaded 1585 1581 #in this case, skip appending to output … … 1608 1604 original_fname = state.file[0:max_char] 1609 1605 state.file = original_fname + ' [' + time_str + ']' 1610 1606 1611 1607 if state is not None and state.is_data is not None: 1612 1608 exec 'output[%d].is_data = state.is_data' % ind 1613 1609 1614 1610 output[ind].filename = state.file 1615 1611 state.data = output[ind] … … 1629 1625 self.state = state 1630 1626 return output 1631 1627 1632 1628 except: 1633 1629 self.call_back(format=ext) 1634 1630 #self.state= state 1635 1631 raise 1636 1632 1637 1633 def write(self, filename, datainfo=None, fitstate=None): 1638 1634 """ 1639 1635 Write the content of a Data1D as a CanSAS XML file only for standalone 1640 1636 1641 1637 :param filename: name of the file to write 1642 1638 :param datainfo: Data1D object 1643 1639 :param fitstate: PageState object 1644 1640 1645 1641 """ 1646 1642 # Sanity check … … 1651 1647 else: 1652 1648 doc = fitstate.toXML(file=filename) 1653 1649 1654 1650 # Save the document no matter the type 1655 1651 fd = open(filename, 'w') 1656 1652 fd.write(doc.toprettyxml()) 1657 1653 fd.close() 1658 1654 1659 1655 def write_toXML(self, datainfo=None, state=None): 1660 1656 """ 1661 1657 Write toXML, a helper for write(), 1662 1658 could be used by guimanager._on_save() 1663 1659 1664 1660 : return: xml doc 1665 1661 """ … … 1677 1673 state.data.run = [str(state.data.name)] 1678 1674 state.data.run_name[0] = state.data.name 1679 1675 1680 1676 if issubclass(state.data.__class__, 1681 1677 sas.dataloader.data_info.Data1D): … … 1685 1681 data = state.data 1686 1682 doc, sasentry = self._data2d_to_xml_doc(data) 1687 1683 1688 1684 if state is not None: 1689 1685 doc = state.toXML(doc=doc, file=data.filename, entry_node=sasentry) 1690 1686 1691 1687 return doc 1692 1688 1693 1689 # Simple html report templet 1694 1690 HEADER = "<html>\n" … … 1736 1732 #state.toXML() 1737 1733 """ 1738 1734 1739 1735 file = open("test_state", "w") 1740 1736 pickle.dump(state, file) … … 1769 1765 db.keys().sort() 1770 1766 print pickle.loads(db['state2']) 1771 1767 1772 1768 db.close()
Note: See TracChangeset
for help on using the changeset viewer.