Changeset e9b12eaf in sasview for sansview/perspectives
- Timestamp:
- May 25, 2010 2:40:34 PM (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:
- 35b556d
- Parents:
- bb70474
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/pagestate.py
rbb70474 re9b12eaf 19 19 import DataLoader 20 20 from DataLoader.readers.cansas_reader import Reader as CansasReader 21 from DataLoader.readers.cansas_reader import get_content 21 from DataLoader.readers.cansas_reader import get_content, write_node 22 22 23 23 #Information to read/write state as xml … … 64 64 ["fixed_param", "fixed_param"], 65 65 ["fittable_param","fittable_param"]] 66 list_of_data_2d_attr = [["xmin", "xmin"], 67 ["xmax","xmax"], 68 ["ymin","ymin"], 69 ["ymax","ymax"], 70 ["_xaxis","_xaxis"], 71 ["_xunit", "_xunit"], 72 ["_yaxis","_yaxis"], 73 ["_yunit","_yunit"], 74 ["_zaxis","_zaxis"], 75 ["_zunit","_zunit"]] 76 list_of_data2d_values = [["qx_data","qx_data"], 77 ["qy_data","qy_data"], 78 ["qdx_data","qdx_data"], 79 ["qdy_data","qdy_data"], 80 ["data","data"], 81 ["q_data","q_data"], 82 ["err_data","err_data"], 83 ["mask","mask"],] 66 84 67 85 class PageState(object): … … 390 408 element = newdoc.createElement(item[0]) 391 409 exec "list = self.%s"%item[1] 392 for itemin list:393 exec "element.appendChild(newdoc.createTextNode(str(%s)))" % item410 for value in list: 411 exec "element.appendChild(newdoc.createTextNode(str(%s)))" % value 394 412 inputs.appendChild(element) 395 413 … … 523 541 @return: None 524 542 """ 525 if self.cansas ==True:543 if self.cansas == True: 526 544 return self._read_cansas(path) 527 else: 528 return self._read_standalone(path) 529 530 def _data2d_to_xml_doc(self): 545 546 def _data2d_to_xml_doc(self, datainfo): 531 547 """ 532 548 Create an XML document to contain the content of a Data2D … … 553 569 runname = {'name': datainfo.run_name[item] } 554 570 write_node(doc, entry_node, "Run", item, runname) 555 556 571 # Data info 557 572 node = doc.createElement("SASdata") 558 573 entry_node.appendChild(node) 559 560 for i in range(len(datainfo.x)): 561 pt = doc.createElement("Idata") 562 node.appendChild(pt) 563 write_node(doc, pt, "Q", datainfo.x[i], {'unit':datainfo.x_unit}) 564 if len(datainfo.y)>=i: 565 write_node(doc, pt, "I", datainfo.y[i], {'unit':datainfo.y_unit}) 566 if datainfo.dx !=None and len(datainfo.dx)>=i: 567 write_node(doc, pt, "Qdev", datainfo.dx[i], {'unit':datainfo.x_unit}) 568 if datainfo.dy !=None and len(datainfo.dy)>=i: 569 write_node(doc, pt, "Idev", datainfo.dy[i], {'unit':datainfo.y_unit}) 570 571 574 for item in list_of_data_2d_attr: 575 element = doc.createElement(item[0]) 576 exec "element.setAttribute(item[0], str(datainfo.%s))"%(item[1]) 577 entry_node.appendChild(element) 578 for item in list_of_data2d_values: 579 element = doc.createElement(item[0]) 580 exec "temp_list = self.%s"%item[1] 581 for value in temp_list: 582 exec "element.appendChild(doc.createTextNode(str(%s)))" % value 583 entry_node.appendChild(element) 584 572 585 # Sample info 573 586 sample = doc.createElement("SASsample") … … 690 703 det.appendChild(ori) 691 704 692 693 705 # Processes info 694 706 for item in datainfo.process: … … 709 721 # the data we just wrote 710 722 return doc, entry_node 711 712 def _read_standalone(self, path): 713 """ 714 Load a new P(r) inversion state from file. 715 The P(r) node is assumed to be the top element. 716 717 @param path: file path 718 @return: None 719 """ 720 # Read the new state from file 721 state = PageState() 722 state.fromXML(file=path) 723 # Call back to post the new state 724 self.call_back(state) 725 return None 723 726 724 727 725 def _parse_state(self, entry): … … 742 740 return state 743 741 742 def _parse_entry_2d(self, dom): 743 """ 744 Parse a SASentry 745 746 @param node: SASentry node 747 @return: Data2D object 748 """ 749 750 data_info = Data2D() 751 752 # Look up title 753 self._store_content('ns:Title', dom, 'title', data_info) 754 755 # Look up run number 756 nodes = dom.xpath('ns:Run', namespaces={'ns': CANSAS_NS}) 757 for item in nodes: 758 if item.text is not None: 759 value = item.text.strip() 760 if len(value) > 0: 761 data_info.run.append(value) 762 if item.get('name') is not None: 763 data_info.run_name[value] = item.get('name') 764 765 # Look up instrument name 766 self._store_content('ns:SASinstrument/ns:name', dom, 'instrument', data_info) 767 768 # Notes 769 note_list = dom.xpath('ns:SASnote', namespaces={'ns': CANSAS_NS}) 770 for note in note_list: 771 try: 772 if note.text is not None: 773 note_value = note.text.strip() 774 if len(note_value) > 0: 775 data_info.notes.append(note_value) 776 except: 777 err_mess = "cansas_reader.read: error processing entry notes\n %s" % sys.exc_value 778 self.errors.append(err_mess) 779 logging.error(err_mess) 780 781 # Sample info ################### 782 entry = get_content('ns:SASsample', dom) 783 if entry is not None: 784 data_info.sample.name = entry.get('name') 785 786 self._store_content('ns:SASsample/ns:ID', 787 dom, 'ID', data_info.sample) 788 self._store_float('ns:SASsample/ns:thickness', 789 dom, 'thickness', data_info.sample) 790 self._store_float('ns:SASsample/ns:transmission', 791 dom, 'transmission', data_info.sample) 792 self._store_float('ns:SASsample/ns:temperature', 793 dom, 'temperature', data_info.sample) 794 795 nodes = dom.xpath('ns:SASsample/ns:details', namespaces={'ns': CANSAS_NS}) 796 for item in nodes: 797 try: 798 if item.text is not None: 799 detail_value = item.text.strip() 800 if len(detail_value) > 0: 801 data_info.sample.details.append(detail_value) 802 except: 803 err_mess = "cansas_reader.read: error processing sample details\n %s" % sys.exc_value 804 self.errors.append(err_mess) 805 logging.error(err_mess) 806 807 # Position (as a vector) 808 self._store_float('ns:SASsample/ns:position/ns:x', 809 dom, 'position.x', data_info.sample) 810 self._store_float('ns:SASsample/ns:position/ns:y', 811 dom, 'position.y', data_info.sample) 812 self._store_float('ns:SASsample/ns:position/ns:z', 813 dom, 'position.z', data_info.sample) 814 815 # Orientation (as a vector) 816 self._store_float('ns:SASsample/ns:orientation/ns:roll', 817 dom, 'orientation.x', data_info.sample) 818 self._store_float('ns:SASsample/ns:orientation/ns:pitch', 819 dom, 'orientation.y', data_info.sample) 820 self._store_float('ns:SASsample/ns:orientation/ns:yaw', 821 dom, 'orientation.z', data_info.sample) 822 823 # Source info ################### 824 entry = get_content('ns:SASinstrument/ns:SASsource', dom) 825 if entry is not None: 826 data_info.source.name = entry.get('name') 827 828 self._store_content('ns:SASinstrument/ns:SASsource/ns:radiation', 829 dom, 'radiation', data_info.source) 830 self._store_content('ns:SASinstrument/ns:SASsource/ns:beam_shape', 831 dom, 'beam_shape', data_info.source) 832 self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength', 833 dom, 'wavelength', data_info.source) 834 self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_min', 835 dom, 'wavelength_min', data_info.source) 836 self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_max', 837 dom, 'wavelength_max', data_info.source) 838 self._store_float('ns:SASinstrument/ns:SASsource/ns:wavelength_spread', 839 dom, 'wavelength_spread', data_info.source) 840 841 # Beam size (as a vector) 842 entry = get_content('ns:SASinstrument/ns:SASsource/ns:beam_size', dom) 843 if entry is not None: 844 data_info.source.beam_size_name = entry.get('name') 845 846 self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:x', 847 dom, 'beam_size.x', data_info.source) 848 self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:y', 849 dom, 'beam_size.y', data_info.source) 850 self._store_float('ns:SASinstrument/ns:SASsource/ns:beam_size/ns:z', 851 dom, 'beam_size.z', data_info.source) 852 853 # Collimation info ################### 854 nodes = dom.xpath('ns:SASinstrument/ns:SAScollimation', namespaces={'ns': CANSAS_NS}) 855 for item in nodes: 856 collim = Collimation() 857 if item.get('name') is not None: 858 collim.name = item.get('name') 859 self._store_float('ns:length', item, 'length', collim) 860 861 # Look for apertures 862 apert_list = item.xpath('ns:aperture', namespaces={'ns': CANSAS_NS}) 863 for apert in apert_list: 864 aperture = Aperture() 865 866 # Get the name and type of the aperture 867 aperture.name = apert.get('name') 868 aperture.type = apert.get('type') 869 870 self._store_float('ns:distance', apert, 'distance', aperture) 871 872 entry = get_content('ns:size', apert) 873 if entry is not None: 874 aperture.size_name = entry.get('name') 875 876 self._store_float('ns:size/ns:x', apert, 'size.x', aperture) 877 self._store_float('ns:size/ns:y', apert, 'size.y', aperture) 878 self._store_float('ns:size/ns:z', apert, 'size.z', aperture) 879 880 collim.aperture.append(aperture) 881 882 data_info.collimation.append(collim) 883 884 # Detector info ###################### 885 nodes = dom.xpath('ns:SASinstrument/ns:SASdetector', namespaces={'ns': CANSAS_NS}) 886 for item in nodes: 887 888 detector = Detector() 889 890 self._store_content('ns:name', item, 'name', detector) 891 self._store_float('ns:SDD', item, 'distance', detector) 892 893 # Detector offset (as a vector) 894 self._store_float('ns:offset/ns:x', item, 'offset.x', detector) 895 self._store_float('ns:offset/ns:y', item, 'offset.y', detector) 896 self._store_float('ns:offset/ns:z', item, 'offset.z', detector) 897 898 # Detector orientation (as a vector) 899 self._store_float('ns:orientation/ns:roll', item, 'orientation.x', detector) 900 self._store_float('ns:orientation/ns:pitch', item, 'orientation.y', detector) 901 self._store_float('ns:orientation/ns:yaw', item, 'orientation.z', detector) 902 903 # Beam center (as a vector) 904 self._store_float('ns:beam_center/ns:x', item, 'beam_center.x', detector) 905 self._store_float('ns:beam_center/ns:y', item, 'beam_center.y', detector) 906 self._store_float('ns:beam_center/ns:z', item, 'beam_center.z', detector) 907 908 # Pixel size (as a vector) 909 self._store_float('ns:pixel_size/ns:x', item, 'pixel_size.x', detector) 910 self._store_float('ns:pixel_size/ns:y', item, 'pixel_size.y', detector) 911 self._store_float('ns:pixel_size/ns:z', item, 'pixel_size.z', detector) 912 913 self._store_float('ns:slit_length', item, 'slit_length', detector) 914 915 data_info.detector.append(detector) 916 917 # Processes info ###################### 918 nodes = dom.xpath('ns:SASprocess', namespaces={'ns': CANSAS_NS}) 919 for item in nodes: 920 process = Process() 921 self._store_content('ns:name', item, 'name', process) 922 self._store_content('ns:date', item, 'date', process) 923 self._store_content('ns:description', item, 'description', process) 924 925 term_list = item.xpath('ns:term', namespaces={'ns': CANSAS_NS}) 926 for term in term_list: 927 try: 928 term_attr = {} 929 for attr in term.keys(): 930 term_attr[attr] = term.get(attr).strip() 931 if term.text is not None: 932 term_attr['value'] = term.text.strip() 933 process.term.append(term_attr) 934 except: 935 err_mess = "cansas_reader.read: error processing process term\n %s" % sys.exc_value 936 self.errors.append(err_mess) 937 logging.error(err_mess) 938 939 note_list = item.xpath('ns:SASprocessnote', namespaces={'ns': CANSAS_NS}) 940 for note in note_list: 941 if note.text is not None: 942 process.notes.append(note.text.strip()) 943 944 data_info.process.append(process) 945 946 947 # Data info ###################### 948 nodes = dom.xpath('ns:SASdata', namespaces={'ns': CANSAS_NS}) 949 if len(nodes)>1: 950 raise RuntimeError, "CanSAS reader is not compatible with multiple SASdata entries" 951 952 nodes = dom.xpath('ns:SASdata/ns:Idata', namespaces={'ns': CANSAS_NS}) 953 954 for item in nodes: 955 _x, attr = get_float('ns:Q', item) 956 _dx, attr_d = get_float('ns:Qdev', item) 957 _dxl, attr_l = get_float('ns:dQl', item) 958 _dxw, attr_w = get_float('ns:dQw', item) 959 if _dx == None: 960 _dx = 0.0 961 if _dxl == None: 962 _dxl = 0.0 963 if _dxw == None: 964 _dxw = 0.0 965 966 if attr.has_key('unit') and attr['unit'].lower() != data_info.x_unit.lower(): 967 if has_converter==True: 968 try: 969 data_conv_q = Converter(attr['unit']) 970 _x = data_conv_q(_x, units=data_info.x_unit) 971 except: 972 raise ValueError, "CanSAS reader: could not convert Q unit [%s]; expecting [%s]\n %s" \ 973 % (attr['unit'], data_info.x_unit, sys.exc_value) 974 else: 975 raise ValueError, "CanSAS reader: unrecognized Q unit [%s]; expecting [%s]" \ 976 % (attr['unit'], data_info.x_unit) 977 # Error in Q 978 if attr_d.has_key('unit') and attr_d['unit'].lower() != data_info.x_unit.lower(): 979 if has_converter==True: 980 try: 981 data_conv_q = Converter(attr_d['unit']) 982 _dx = data_conv_q(_dx, units=data_info.x_unit) 983 except: 984 raise ValueError, "CanSAS reader: could not convert dQ unit [%s]; expecting [%s]\n %s" \ 985 % (attr['unit'], data_info.x_unit, sys.exc_value) 986 else: 987 raise ValueError, "CanSAS reader: unrecognized dQ unit [%s]; expecting [%s]" \ 988 % (attr['unit'], data_info.x_unit) 989 # Slit length 990 if attr_l.has_key('unit') and attr_l['unit'].lower() != data_info.x_unit.lower(): 991 if has_converter==True: 992 try: 993 data_conv_q = Converter(attr_l['unit']) 994 _dxl = data_conv_q(_dxl, units=data_info.x_unit) 995 except: 996 raise ValueError, "CanSAS reader: could not convert dQl unit [%s]; expecting [%s]\n %s" \ 997 % (attr['unit'], data_info.x_unit, sys.exc_value) 998 else: 999 raise ValueError, "CanSAS reader: unrecognized dQl unit [%s]; expecting [%s]" \ 1000 % (attr['unit'], data_info.x_unit) 1001 # Slit width 1002 if attr_w.has_key('unit') and attr_w['unit'].lower() != data_info.x_unit.lower(): 1003 if has_converter==True: 1004 try: 1005 data_conv_q = Converter(attr_w['unit']) 1006 _dxw = data_conv_q(_dxw, units=data_info.x_unit) 1007 except: 1008 raise ValueError, "CanSAS reader: could not convert dQw unit [%s]; expecting [%s]\n %s" \ 1009 % (attr['unit'], data_info.x_unit, sys.exc_value) 1010 else: 1011 raise ValueError, "CanSAS reader: unrecognized dQw unit [%s]; expecting [%s]" \ 1012 % (attr['unit'], data_info.x_unit) 1013 1014 _y, attr = get_float('ns:I', item) 1015 _dy, attr_d = get_float('ns:Idev', item) 1016 if _dy == None: 1017 _dy = 0.0 1018 if attr.has_key('unit') and attr['unit'].lower() != data_info.y_unit.lower(): 1019 if has_converter==True: 1020 try: 1021 data_conv_i = Converter(attr['unit']) 1022 _y = data_conv_i(_y, units=data_info.y_unit) 1023 except: 1024 raise ValueError, "CanSAS reader: could not convert I(q) unit [%s]; expecting [%s]\n %s" \ 1025 % (attr['unit'], data_info.y_unit, sys.exc_value) 1026 else: 1027 raise ValueError, "CanSAS reader: unrecognized I(q) unit [%s]; expecting [%s]" \ 1028 % (attr['unit'], data_info.y_unit) 1029 if attr_d.has_key('unit') and attr_d['unit'].lower() != data_info.y_unit.lower(): 1030 if has_converter==True: 1031 try: 1032 data_conv_i = Converter(attr_d['unit']) 1033 _dy = data_conv_i(_dy, units=data_info.y_unit) 1034 except: 1035 raise ValueError, "CanSAS reader: could not convert dI(q) unit [%s]; expecting [%s]\n %s" \ 1036 % (attr_d['unit'], data_info.y_unit, sys.exc_value) 1037 else: 1038 raise ValueError, "CanSAS reader: unrecognized dI(q) unit [%s]; expecting [%s]" \ 1039 % (attr_d['unit'], data_info.y_unit) 1040 1041 if _x is not None and _y is not None: 1042 exec "item = numpy.append(x, _x)" 1043 1044 for item in list_of_model_attributes: 1045 node = get_content("ns:%s"%item[0], entry) 1046 list = [] 1047 for value in node: 1048 try: 1049 list.append(float(value)) 1050 except: 1051 list.append(None) 1052 1053 exec "self.%s = list"%item[1] 1054 data_conv_q = None 1055 data_conv_i = None 1056 1057 if has_converter == True and data_info.x_unit != '1/A': 1058 data_conv_q = Converter('1/A') 1059 # Test it 1060 data_conv_q(1.0, output.Q_unit) 1061 1062 if has_converter == True and data_info.y_unit != '1/cm': 1063 data_conv_i = Converter('1/cm') 1064 # Test it 1065 data_conv_i(1.0, output.I_unit) 1066 1067 if data_conv_q is not None: 1068 data_info.xaxis("\\rm{%s}"%str(_xaxis), data_info.x_unit) 1069 else: 1070 data_info.xaxis("\\rm{%s}"%str(_xaxis), 'A^{-1}') 1071 if data_conv_i is not None: 1072 data_info.yaxis("\\rm{%s}"%str(_yaxis), data_info.y_unit) 1073 else: 1074 data_info.yaxis("\\rm{%s}"%str(_yaxis),"cm^{-1}") 1075 1076 return data_info 1077 744 1078 def _read_cansas(self, path): 745 1079 """ … … 769 1103 entry_list = root.xpath('/ns:SASroot/ns:SASentry', namespaces={'ns': CANSAS_NS}) 770 1104 for entry in entry_list: 771 sas_entry = self._parse_entry(entry) 1105 try: 1106 sas_entry = self._parse_entry(entry) 1107 except: 1108 sas_entry = self._parse_entry_2d(entry) 772 1109 fitstate = self._parse_state(entry) 773 1110 sas_entry.meta_data['fitstate'] = fitstate … … 832 1169 data = fitstate.data 833 1170 doc, sasentry = self._data2d_to_xml_doc(data) 834 fitstate.toXML(doc=doc, entry_node=sasentry)1171 fitstate.toXML(doc=doc, file=data.name, entry_node=sasentry) 835 1172 # Write the XML document 836 1173 fd = open(filename, 'w')
Note: See TracChangeset
for help on using the changeset viewer.