source: sasview/test/fileconverter/test/utest_nxcansas_writer.py @ 248ff73

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

Ensure unit tests pass

utest_averaging still fails as it relies on loading in an Igor 2D file,
which is no longer possible

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