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

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since f53d684 was f53d684, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

Make tests work from any directory and functional without special runner script (#124)

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