source: sasview/test/fileconverter/test/utest_nxcansas_writer.py @ 3a473ef

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 3a473ef was 3a473ef, checked in by Piotr Rozyczko <rozyczko@…>, 14 months ago

Manual update of test/ directory with changes on master - SASVIEW-996

  • 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.