source: sasview/test/fileconverter/test/utest_nxcansas_writer.py @ c476457

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.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c476457 was c476457, checked in by lewis, 4 years ago

Add basic NXcanSASWriter unit tests

  • Property mode set to 100644
File size: 2.8 KB
Line 
1from sas.sascalc.file_converter.nxcansas_writer import NXcanSASWriter
2from sas.sascalc.dataloader.readers.cansas_reader import Reader as XMLReader
3from sas.sascalc.dataloader.readers.red2d_reader import Reader as DATReader
4from sas.sascalc.dataloader.readers.cansas_reader_HDF5 import Reader as HDF5Reader
5
6import os
7import pylint
8import unittest
9import warnings
10
11warnings.simplefilter("ignore")
12
13class nxcansas_writer(unittest.TestCase):
14
15    def setUp(self):
16        self.writer = NXcanSASWriter()
17        self.read_file_1d = "cansas1d.xml"
18        self.write_file_1d = "export1d.h5"
19        self.read_file_2d = "exp18_14_igor_2dqxqy.dat"
20        self.write_file_2d = "export2d.h5"
21        self.hdf5_reader = HDF5Reader()
22
23        xml_reader = XMLReader()
24        self.data_1d = xml_reader.read(self.read_file_1d)[0]
25
26        dat_reader = DATReader()
27        self.data_2d = dat_reader.read(self.read_file_2d)
28        self.data_2d.detector[0].name = ''
29        self.data_2d.source.radiation = 'neutron'
30
31    def test_write_1d(self):
32        self.writer.write([self.data_1d], self.write_file_1d)
33        data = self.hdf5_reader.read(self.write_file_1d)
34        self.assertTrue(len(data) == 1)
35        data = data[0]
36        self.assertTrue(len(data.x) == len(self.data_1d.x))
37        self.assertTrue(len(data.y) == len(self.data_1d.y))
38        self.assertTrue(len(data.dy) == len(self.data_1d.dy))
39        self._check_metadata(data, self.data_1d)
40
41    def test_write_2d(self):
42        self.writer.write([self.data_2d], self.write_file_2d)
43        data = self.hdf5_reader.read(self.write_file_2d)
44        self.assertTrue(len(data) == 1)
45        data = data[0]
46        self.assertTrue(len(data.data) == len(self.data_2d.data))
47        self.assertTrue(len(data.qx_data) == len(self.data_2d.qx_data))
48        self.assertTrue(len(data.qy_data) == len(self.data_2d.qy_data))
49        self._check_metadata(data, self.data_2d)
50
51    def _check_metadata(self, written, correct):
52        self.assertTrue(written.title == correct.title)
53        self.assertTrue(written.sample.name == correct.sample.name)
54        self.assertAlmostEqual(written.sample.thickness, correct.sample.thickness)
55        self.assertAlmostEqual(written.sample.temperature, correct.sample.temperature)
56        self.assertTrue(written.instrument == correct.instrument)
57        self.assertTrue(len(written.detector) == len(correct.detector))
58        for i in range(len(written.detector)):
59            written_det = written.detector[i]
60            correct_det = correct.detector[i]
61            self.assertAlmostEqual(written_det.distance, correct_det.distance)
62            self.assertTrue(written_det.name == correct_det.name)
63        self.assertTrue(written.source.radiation == correct.source.radiation)
64
65    def tearDown(self):
66        if os.path.isfile(self.write_file_1d):
67            os.remove(self.write_file_1d)
Note: See TracBrowser for help on using the repository browser.