Changeset df7ed14 in sasview
- Timestamp:
- Jun 15, 2010 11:16:14 AM (15 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:
- 0b12abb5
- Parents:
- 7cc0899
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/pagestate.py
r5062bbf rdf7ed14 16 16 import copy 17 17 import logging 18 import numpy 18 19 19 20 import xml.dom.minidom … … 24 25 from DataLoader.readers.cansas_reader import Reader as CansasReader 25 26 from DataLoader.readers.cansas_reader import get_content, write_node 26 from DataLoader.data_info import Data2D 27 from DataLoader.data_info import Data2D, Detector 27 28 28 29 #Information to read/write state as xml … … 69 70 ["fixed_param", "fixed_param"], 70 71 ["fittable_param","fittable_param"]] 71 list_of_data_2d_attr = [["xmin", "xmin" ],72 ["xmax","xmax" ],73 ["ymin","ymin" ],74 ["ymax","ymax" ],75 ["_xaxis","_xaxis" ],76 ["_xunit", "_xunit" ],77 ["_yaxis","_yaxis" ],78 ["_yunit","_yunit" ],79 ["_zaxis","_zaxis" ],80 ["_zunit","_zunit" ]]81 list_of_data2d_values = [["qx_data","qx_data" ],82 ["qy_data","qy_data" ],83 ["dqx_data","dqx_data" ],84 ["dqy_data","dqy_data" ],85 ["data","data" ],86 ["q_data","q_data" ],87 ["err_data","err_data" ],88 ["mask","mask" ],]72 list_of_data_2d_attr = [["xmin", "xmin","float"], 73 ["xmax","xmax","float"], 74 ["ymin","ymin","float"], 75 ["ymax","ymax","float"], 76 ["_xaxis","_xaxis", "string"], 77 ["_xunit", "_xunit", "string"], 78 ["_yaxis","_yaxis","string"], 79 ["_yunit","_yunit","string"], 80 ["_zaxis","_zaxis","string"], 81 ["_zunit","_zunit","string"]] 82 list_of_data2d_values = [["qx_data","qx_data","float"], 83 ["qy_data","qy_data","float"], 84 ["dqx_data","dqx_data","float"], 85 ["dqy_data","dqy_data","float"], 86 ["data","data","float"], 87 ["q_data","q_data","float"], 88 ["err_data","err_data","float"], 89 ["mask","mask","bool"]] 89 90 90 91 class PageState(object): … … 493 494 elif item[2] == "bool": 494 495 try: 495 exec "self.%s= field.get( str(%s))"%(item[1], item[0])496 exec "self.%s= field.get(%s)"%(item[1], item[0]) 496 497 except: 497 498 exec "self.%s = None"%item[1] … … 530 531 531 532 def __init__(self, call_back=None, cansas=True): 533 CansasReader.__init__(self) 532 534 """ 533 535 Initialize the call-back method to be called … … 567 569 main_node = doc.createElement("SASroot") 568 570 main_node.setAttribute("version", self.version) 569 571 main_node.setAttribute("xmlns", "cansas1d/%s" % self.version) 572 main_node.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") 573 main_node.setAttribute("xsi:schemaLocation", "cansas1d/%s http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd" % self.version) 574 570 575 doc.appendChild(main_node) 571 576 572 577 entry_node = doc.createElement("SASentry") 573 578 main_node.appendChild(entry_node) 574 579 575 580 write_node(doc, entry_node, "Title", datainfo.title) 576 581 if datainfo is not None: 582 write_node(doc, entry_node, "data_class", datainfo.__class__.__name__) 577 583 for item in datainfo.run: 578 584 runname = {} … … 581 587 write_node(doc, entry_node, "Run", item, runname) 582 588 # Data info 583 n ode = doc.createElement("SASdata")584 entry_node.appendChild(n ode)589 new_node = doc.createElement("SASdata") 590 entry_node.appendChild(new_node) 585 591 for item in list_of_data_2d_attr: 586 592 element = doc.createElement(item[0]) 587 593 exec "element.setAttribute(item[0], str(datainfo.%s))"%(item[1]) 588 entry_node.appendChild(element) 594 new_node.appendChild(element) 595 589 596 for item in list_of_data2d_values: 590 element = doc.createElement(item[0]) 597 root_node = doc.createElement(item[0]) 598 new_node.appendChild(root_node) 599 591 600 exec "temp_list = datainfo.%s"%item[1] 601 592 602 if temp_list is None or len(temp_list)== 0: 603 element = doc.createElement(item[0]) 593 604 exec "element.appendChild(doc.createTextNode(str(%s)))"%temp_list 605 root_node.appendChild(element) 594 606 else: 595 607 for value in temp_list: 596 exec "element.appendChild(doc.createTextNode(str(%s)))"%value 597 entry_node.appendChild(element) 598 608 element = doc.createElement(item[0]) 609 exec "element.setAttribute(item[0], str(%s))"%value 610 root_node.appendChild(element) 611 599 612 # Sample info 600 613 sample = doc.createElement("SASsample") … … 748 761 try: 749 762 nodes = entry.xpath('ns:%s' % FITTING_NODE_NAME, namespaces={'ns': CANSAS_NS}) 763 #if entry is not None: 764 # self.file = entry.text.strip() 750 765 state.fromXML(node=nodes[0]) 751 766 except: … … 754 769 return state 755 770 756 def _parse_entry_2d(self, dom): 771 def _parse_entry_2d_helper(self, node, item): 772 """ 773 Create a numpy list from value extrated from the node 774 775 :param node: node from each the value is stored 776 :param item: list name of three strings.the two first are name of data 777 attribute and the third one is the type of the value of that 778 attribute. type can be string, float, bool, etc. 779 780 : return: numpy array 781 """ 782 if node is not None: 783 if item[2] == "string": 784 return node.get(item[0]).strip() 785 elif item[2] == "bool": 786 try: 787 return node.get(item[0]).strip() 788 except: 789 return None 790 else: 791 try: 792 return float(node.get(item[0])) 793 except: 794 return None 795 796 def _parse_entry(self, dom): 757 797 """ 758 798 Parse a SASentry … … 760 800 :param node: SASentry node 761 801 762 :return: Data2D object 763 764 """ 802 :return: Data1D/Data2D object 803 804 """ 805 node = dom.xpath('ns:data_class', namespaces={'ns': CANSAS_NS}) 806 if not node or node[0].text.lstrip().rstrip() != "Data2D": 807 return CansasReader._parse_entry(self, dom) 808 809 #Parse 2D 765 810 data_info = Data2D() 766 811 … … 964 1009 if len(nodes)>1: 965 1010 raise RuntimeError, "CanSAS reader is not compatible with multiple SASdata entries" 966 967 nodes = dom.xpath('ns:SASdata/ns:Idata', namespaces={'ns': CANSAS_NS}) 968 969 for item in nodes: 970 _x, attr = get_float('ns:Q', item) 971 _dx, attr_d = get_float('ns:Qdev', item) 972 _dxl, attr_l = get_float('ns:dQl', item) 973 _dxw, attr_w = get_float('ns:dQw', item) 974 if _dx == None: 975 _dx = 0.0 976 if _dxl == None: 977 _dxl = 0.0 978 if _dxw == None: 979 _dxw = 0.0 980 981 if attr.has_key('unit') and attr['unit'].lower() != data_info.x_unit.lower(): 982 if has_converter==True: 983 try: 984 data_conv_q = Converter(attr['unit']) 985 _x = data_conv_q(_x, units=data_info.x_unit) 986 except: 987 raise ValueError, "CanSAS reader: could not convert Q unit [%s]; expecting [%s]\n %s" \ 988 % (attr['unit'], data_info.x_unit, sys.exc_value) 989 else: 990 raise ValueError, "CanSAS reader: unrecognized Q unit [%s]; expecting [%s]" \ 991 % (attr['unit'], data_info.x_unit) 992 # Error in Q 993 if attr_d.has_key('unit') and attr_d['unit'].lower() != data_info.x_unit.lower(): 994 if has_converter==True: 995 try: 996 data_conv_q = Converter(attr_d['unit']) 997 _dx = data_conv_q(_dx, units=data_info.x_unit) 998 except: 999 raise ValueError, "CanSAS reader: could not convert dQ unit [%s]; expecting [%s]\n %s" \ 1000 % (attr['unit'], data_info.x_unit, sys.exc_value) 1001 else: 1002 raise ValueError, "CanSAS reader: unrecognized dQ unit [%s]; expecting [%s]" \ 1003 % (attr['unit'], data_info.x_unit) 1004 # Slit length 1005 if attr_l.has_key('unit') and attr_l['unit'].lower() != data_info.x_unit.lower(): 1006 if has_converter==True: 1007 try: 1008 data_conv_q = Converter(attr_l['unit']) 1009 _dxl = data_conv_q(_dxl, units=data_info.x_unit) 1010 except: 1011 raise ValueError, "CanSAS reader: could not convert dQl unit [%s]; expecting [%s]\n %s" \ 1012 % (attr['unit'], data_info.x_unit, sys.exc_value) 1013 else: 1014 raise ValueError, "CanSAS reader: unrecognized dQl unit [%s]; expecting [%s]" \ 1015 % (attr['unit'], data_info.x_unit) 1016 # Slit width 1017 if attr_w.has_key('unit') and attr_w['unit'].lower() != data_info.x_unit.lower(): 1018 if has_converter==True: 1019 try: 1020 data_conv_q = Converter(attr_w['unit']) 1021 _dxw = data_conv_q(_dxw, units=data_info.x_unit) 1022 except: 1023 raise ValueError, "CanSAS reader: could not convert dQw unit [%s]; expecting [%s]\n %s" \ 1024 % (attr['unit'], data_info.x_unit, sys.exc_value) 1025 else: 1026 raise ValueError, "CanSAS reader: unrecognized dQw unit [%s]; expecting [%s]" \ 1027 % (attr['unit'], data_info.x_unit) 1011 1012 for entry in nodes: 1013 for item in list_of_data_2d_attr: 1014 #get node 1015 node = get_content('ns:%s'%item[0], entry) 1016 exec "data_info.%s = self._parse_entry_2d_helper(node, item)"%(item[1]) 1028 1017 1029 _y, attr = get_float('ns:I', item) 1030 _dy, attr_d = get_float('ns:Idev', item) 1031 if _dy == None: 1032 _dy = 0.0 1033 if attr.has_key('unit') and attr['unit'].lower() != data_info.y_unit.lower(): 1034 if has_converter==True: 1035 try: 1036 data_conv_i = Converter(attr['unit']) 1037 _y = data_conv_i(_y, units=data_info.y_unit) 1038 except: 1039 raise ValueError, "CanSAS reader: could not convert I(q) unit [%s]; expecting [%s]\n %s" \ 1040 % (attr['unit'], data_info.y_unit, sys.exc_value) 1041 else: 1042 raise ValueError, "CanSAS reader: unrecognized I(q) unit [%s]; expecting [%s]" \ 1043 % (attr['unit'], data_info.y_unit) 1044 if attr_d.has_key('unit') and attr_d['unit'].lower() != data_info.y_unit.lower(): 1045 if has_converter==True: 1046 try: 1047 data_conv_i = Converter(attr_d['unit']) 1048 _dy = data_conv_i(_dy, units=data_info.y_unit) 1049 except: 1050 raise ValueError, "CanSAS reader: could not convert dI(q) unit [%s]; expecting [%s]\n %s" \ 1051 % (attr_d['unit'], data_info.y_unit, sys.exc_value) 1052 else: 1053 raise ValueError, "CanSAS reader: unrecognized dI(q) unit [%s]; expecting [%s]" \ 1054 % (attr_d['unit'], data_info.y_unit) 1055 1056 if _x is not None and _y is not None: 1057 exec "item = numpy.append(x, _x)" 1058 1059 for item in list_of_model_attributes: 1060 node = get_content("ns:%s"%item[0], entry) 1061 list = [] 1062 for value in node: 1063 try: 1064 list.append(float(value)) 1065 except: 1066 list.append(None) 1067 1068 exec "self.%s = list"%item[1] 1069 data_conv_q = None 1070 data_conv_i = None 1071 1072 if has_converter == True and data_info.x_unit != '1/A': 1073 data_conv_q = Converter('1/A') 1074 # Test it 1075 data_conv_q(1.0, output.Q_unit) 1076 1077 if has_converter == True and data_info.y_unit != '1/cm': 1078 data_conv_i = Converter('1/cm') 1079 # Test it 1080 data_conv_i(1.0, output.I_unit) 1081 1082 if data_conv_q is not None: 1083 data_info.xaxis("\\rm{%s}"%str(_xaxis), data_info.x_unit) 1084 else: 1085 data_info.xaxis("\\rm{%s}"%str(_xaxis), 'A^{-1}') 1086 if data_conv_i is not None: 1087 data_info.yaxis("\\rm{%s}"%str(_yaxis), data_info.y_unit) 1088 else: 1089 data_info.yaxis("\\rm{%s}"%str(_yaxis),"cm^{-1}") 1018 for item in list_of_data2d_values: 1019 field = get_content('ns:%s'%item[0], entry) 1020 list = [] 1021 if field is not None: 1022 list = [self._parse_entry_2d_helper(node, item) for node in field] 1023 exec "data_info.%s = numpy.array(list)"%item[0] 1090 1024 1091 1025 return data_info … … 1119 1053 # Specifying the namespace will take care of the file format version 1120 1054 root = tree.getroot() 1121 entry_list = root.xpath(' /ns:SASroot/ns:SASentry', namespaces={'ns': CANSAS_NS})1055 entry_list = root.xpath('ns:SASentry', namespaces={'ns': CANSAS_NS}) 1122 1056 for entry in entry_list: 1123 1057 try: 1124 1058 sas_entry = self._parse_entry(entry) 1125 1059 except: 1126 sas_entry = self._parse_entry_2d(entry)1060 raise 1127 1061 fitstate = self._parse_state(entry) 1128 1062 sas_entry.meta_data['fitstate'] = fitstate 1129 1063 sas_entry.filename = fitstate.file 1130 1064 output.append(sas_entry) 1131 1132 1065 else: 1133 1066 raise RuntimeError, "%s is not a file" % path … … 1156 1089 state.data.name = state.data_name 1157 1090 state.data.id = state.data_id 1091 state.data.id = state.data_id 1092 if state.is_data is not None: 1093 state.data.is_data = state.is_data 1158 1094 #state.data.group_id = state.data_group_id 1159 1095 state.data.group_id = output[0].filename
Note: See TracChangeset
for help on using the changeset viewer.