source: sasview/test/sansdataloader/test/utest_cansas.py @ c6ca23d

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c6ca23d was c6ca23d, checked in by Jeff Krzywon <jeffery.krzywon@…>, 7 years ago

Fixed the file header writing issue for cansas data files outlined in ticket #204.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1"""
2    Unit tests for the new recursive cansas reader
3"""
4import warnings
5warnings.simplefilter("ignore")
6
7import unittest
8import numpy
9import sans.dataloader.readers.cansas_reader as cansas
10from sans.dataloader.loader import Loader
11from sans.dataloader.data_info import Data1D
12from sans.dataloader.readers.xml_reader import XMLreader
13from sans.dataloader.readers.cansas_reader import Reader
14from sans.dataloader.readers.cansas_constants import cansasConstants
15
16import os
17import sys
18import urllib2
19import StringIO
20
21from lxml import etree
22import xml.dom.minidom
23 
24CANSAS_FORMAT = cansasConstants.CANSAS_FORMAT
25CANSAS_NS = cansasConstants.CANSAS_NS
26   
27class cansas_reader(unittest.TestCase):
28   
29    def setUp(self):
30        self.loader = Loader()
31        self.xml_valid = "cansas_test_modified.xml"
32        self.xml_invalid = "cansas_test.xml"
33        self.cansas1d_badunits = "cansas1d_badunits.xml"
34        self.cansas1d = "cansas1d.xml"
35        self.cansas1d_slit = "cansas1d_slit.xml"
36        self.cansas1d_units = "cansas1d_units.xml"
37        self.isis_1_0 = "ISIS_1_0.xml"
38        self.isis_1_1 = "ISIS_1_1.xml"
39        self.isis_1_1_notrans = "ISIS_1_1_notrans.xml"
40        self.isis_1_1_doubletrans = "ISIS_1_1_doubletrans.xml"
41        self.schema_1_0 = "cansas1d_v1_0.xsd"
42        self.schema_1_1 = "cansas1d_v1_1.xsd"
43       
44   
45    def get_number_of_entries(self, dictionary, name, i):
46        if dictionary.get(name) is not None:
47            i += 1
48            name = name.split("_")[0]
49            name += "_{0}".format(i)
50            name = self.get_number_of_entries(dictionary, name, i)
51        return name
52   
53
54    def test_xml_validate(self):
55        string = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n"
56        string += "\t<xsd:element name=\"a\" type=\"AType\"/>\n"
57        string += "\t<xsd:complexType name=\"AType\">\n"
58        string += "\t\t<xsd:sequence>\n"
59        string += "\t\t\t<xsd:element name=\"b\" type=\"xsd:string\" />\n"
60        string += "\t\t</xsd:sequence>\n"
61        string += "\t</xsd:complexType>\n"
62        string += "</xsd:schema>"
63        f = StringIO.StringIO(string)
64        xmlschema_doc = etree.parse(f)
65        xmlschema = etree.XMLSchema(xmlschema_doc)
66        valid = etree.parse(StringIO.StringIO('<a><b></b></a>'))
67        invalid = etree.parse(StringIO.StringIO('<a><c></c></a>'))
68        self.assertTrue(xmlschema.validate(valid))
69        self.assertFalse(xmlschema.validate(invalid))
70       
71       
72    def test_real_xml(self):
73        reader = XMLreader(self.xml_valid, self.schema_1_0)
74        valid = reader.validateXML()
75        if valid:
76            self.assertTrue(valid)
77        else:
78            self.assertFalse(valid)
79           
80           
81    def _check_data(self, data):
82        self.assertTrue(data.title == "TK49 c10_SANS")
83        self.assertTrue(data.x.size == 138)
84        self.assertTrue(len(data.meta_data) == 2)
85        self.assertTrue(data.detector[0].distance_unit == "mm")
86        self.assertTrue(data.detector[1].distance_unit == "mm")
87        self.assertTrue(data.detector[0].name == "HAB")
88        self.assertTrue(data.detector[1].name == "main-detector-bank")
89        self.assertTrue(data.detector[0].distance == 575.0)
90        self.assertTrue(data.detector[1].distance == 4145.02)
91        self.assertTrue(data.process[0].name == "Mantid generated CanSAS1D XML")
92       
93   
94    def _check_data_1_1(self, data):
95        spectrum = data.trans_spectrum[0]
96        self.assertTrue(len(spectrum.wavelength) == 138)
97       
98   
99    def test_cansas_xml(self):
100        filename = "isis_1_1_write_test.xml"
101        xmlreader = XMLreader(self.isis_1_1, self.schema_1_1)
102        valid = xmlreader.validateXML()
103        self.assertTrue(valid)
104        reader_generic = Loader()
105        dataloader = reader_generic.load(self.isis_1_1)
106        reader_cansas = Reader()
107        cansasreader = reader_cansas.read(self.isis_1_1)
108        for i in range(len(dataloader)):
109            self._check_data(dataloader[i])
110            self._check_data_1_1(dataloader[i])
111            self._check_data(cansasreader[i])
112            self._check_data_1_1(cansasreader[i])
113            reader_generic.save(filename, dataloader[i], None)
114            reader2 = Reader()
115            return_data = reader2.read(filename)
116            data_new = return_data
117            written_data = return_data[0]
118            self._check_data(written_data)
119           
120   
121    def test_double_trans_spectra(self):
122        xmlreader = XMLreader(self.isis_1_1_doubletrans, self.schema_1_1)
123        self.assertTrue(xmlreader.validateXML())
124        reader = Loader()
125        data = reader.load(self.isis_1_1_doubletrans)
126        for item in data:
127            self._check_data(item)
128   
129                   
130    def test_entry_name_recurse(self):
131        test_values = [1,2,3,4,5,6]
132        base_key = "key"
133        d = {}
134        for value in test_values:
135            new_key = self.get_number_of_entries(d, base_key, i = 0)
136            d[new_key] = value
137        self.assertTrue(len(d) == 6)
138       
139   
140    def test_load_cansas_file(self):
141        valid = []
142        reader1 = XMLreader(self.xml_valid, self.schema_1_0)
143        self.assertTrue(reader1.validateXML())
144        reader2 = XMLreader(self.xml_invalid, self.schema_1_0)
145        self.assertFalse(reader2.validateXML())
146        reader3 = XMLreader(self.xml_valid, self.schema_1_1)
147        self.assertFalse(reader3.validateXML())
148        reader4 = XMLreader(self.xml_invalid, self.schema_1_1)
149        self.assertFalse(reader4.validateXML())
150        reader5 = XMLreader(self.isis_1_0, self.schema_1_0)
151        self.assertTrue(reader5.validateXML())
152        reader6 = XMLreader(self.isis_1_1, self.schema_1_1)
153        self.assertTrue(reader6.validateXML())
154        reader7 = XMLreader(self.isis_1_1, self.schema_1_0)
155        self.assertFalse(reader7.validateXML())
156       
157   
158    def test_old_cansas_files(self):
159        reader1 = XMLreader(self.cansas1d, self.schema_1_0)
160        self.assertTrue(reader1.validateXML())
161        reader2 = XMLreader(self.cansas1d_units, self.schema_1_0)
162        self.assertTrue(reader2.validateXML())
163        reader3 = XMLreader(self.cansas1d_badunits, self.schema_1_0)
164        self.assertTrue(reader3.validateXML())
165        reader4 = XMLreader(self.cansas1d_slit, self.schema_1_0)
166        self.assertTrue(reader4.validateXML())
167       
168   
169    def test_save_cansas_v1_0(self):
170        filename = "isis_1_0_write_test.xml"
171        xmlreader = XMLreader(self.isis_1_0, self.schema_1_0)
172        valid = xmlreader.validateXML()
173        self.assertTrue(valid)
174        reader_generic = Loader()
175        dataloader = reader_generic.load(self.isis_1_0)
176        reader_cansas = Reader()
177        cansasreader = reader_cansas.read(self.isis_1_0)
178        for i in range(len(dataloader)):
179            self._check_data(dataloader[i])
180            self._check_data(cansasreader[i])
181            reader_generic.save(filename, dataloader[i], None)
182            reader2 = Reader()
183            return_data = reader2.read(filename)
184            data_new = return_data
185            written_data = return_data[0]
186            self._check_data(written_data)
187       
188
189if __name__ == '__main__':
190    unittest.main()   
Note: See TracBrowser for help on using the repository browser.