summaryrefslogtreecommitdiff
path: root/silx/io/test/test_octaveh5.py
blob: 2e658201afd1516d25720a6f43202509e7f30edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ############################################################################*/
"""
Tests for the octaveh5 module
"""

__authors__ = ["C. Nemoz", "H. Payno"]
__license__ = "MIT"
__date__ = "12/07/2016"

import unittest
import os
import tempfile

try:
    from ..octaveh5 import Octaveh5
except ImportError:
    Octaveh5 = None


@unittest.skipIf(Octaveh5 is None, "Could not import h5py")
class TestOctaveH5(unittest.TestCase):
    @staticmethod
    def _get_struct_FT():
        return { 
            'NO_CHECK': 0.0, 'SHOWSLICE': 1.0, 'DOTOMO': 1.0, 'DATABASE': 0.0, 'ANGLE_OFFSET': 0.0, 
            'VOLSELECTION_REMEMBER': 0.0, 'NUM_PART': 4.0, 'VOLOUTFILE': 0.0, 'RINGSCORRECTION': 0.0, 
            'DO_TEST_SLICE': 1.0, 'ZEROOFFMASK': 1.0, 'VERSION': 'fastomo3 version 2.0', 
            'CORRECT_SPIKES_THRESHOLD': 0.040000000000000001, 'SHOWPROJ': 0.0, 'HALF_ACQ': 0.0, 
            'ANGLE_OFFSET_VALUE': 0.0, 'FIXEDSLICE': 'middle', 'VOLSELECT': 'total' }
    @staticmethod
    def _get_struct_PYHSTEXE():
        return {
            'EXE': 'PyHST2_2015d', 'VERBOSE': 0.0, 'OFFV': 'PyHST2_2015d', 'TOMO': 0.0, 
            'VERBOSE_FILE': 'pyhst_out.txt', 'DIR': '/usr/bin/', 'OFFN': 'pyhst2'}

    @staticmethod
    def _get_struct_FTAXIS():
        return {
            'POSITION_VALUE': 12345.0, 'COR_ERROR': 0.0, 'FILESDURINGSCAN': 0.0, 'PLOTFIGURE': 1.0,
            'DIM1': 0.0, 'OVERSAMPLING': 5.0, 'TO_THE_CENTER': 1.0, 'POSITION': 'fixed', 
            'COR_POSITION': 0.0, 'HA': 0.0 }
            
    @staticmethod
    def _get_struct_PAGANIN():
        return {
            'MKEEP_MASK': 0.0, 'UNSHARP_SIGMA': 0.80000000000000004, 'DILATE': 2.0, 'UNSHARP_COEFF': 3.0, 
            'MEDIANR': 4.0, 'DB': 500.0, 'MKEEP_ABS': 0.0, 'MODE': 0.0, 'THRESHOLD': 0.5, 
            'MKEEP_BONE': 0.0, 'DB2': 100.0, 'MKEEP_CORR': 0.0, 'MKEEP_SOFT': 0.0 }

    @staticmethod
    def _get_struct_BEAMGEO():
        return {'DIST': 55.0, 'SY': 0.0, 'SX': 0.0, 'TYPE': 'p'}


    def setUp(self):
        self.tempdir = tempfile.mkdtemp()        
        self.test_3_6_fname = os.path.join(self.tempdir, "silx_tmp_t00_octaveTest_3_6.h5")
        self.test_3_8_fname = os.path.join(self.tempdir, "silx_tmp_t00_octaveTest_3_8.h5")

    def tearDown(self):
        if os.path.isfile(self.test_3_6_fname):
            os.unlink(self.test_3_6_fname)
        if os.path.isfile(self.test_3_8_fname):
            os.unlink(self.test_3_8_fname)

    def testWritedIsReaded(self):
        """
        Simple test to write and reaf the structure compatible with the octave h5 using structure.
        This test is for # test for octave version > 3.8
        """   
        writer = Octaveh5()

        writer.open(self.test_3_8_fname, 'a')
        # step 1 writing the file
        writer.write('FT', self._get_struct_FT())
        writer.write('PYHSTEXE', self._get_struct_PYHSTEXE())
        writer.write('FTAXIS', self._get_struct_FTAXIS())
        writer.write('PAGANIN', self._get_struct_PAGANIN())
        writer.write('BEAMGEO', self._get_struct_BEAMGEO())
        writer.close()

        # step 2 reading the file
        reader = Octaveh5().open(self.test_3_8_fname)
        #  2.1 check FT
        data_readed = reader.get('FT')
        self.assertEqual(data_readed, self._get_struct_FT() )
        #  2.2 check PYHSTEXE
        data_readed = reader.get('PYHSTEXE')
        self.assertEqual(data_readed, self._get_struct_PYHSTEXE() )
        #  2.3 check FTAXIS
        data_readed = reader.get('FTAXIS')
        self.assertEqual(data_readed, self._get_struct_FTAXIS() )
        #  2.4 check PAGANIN
        data_readed = reader.get('PAGANIN')
        self.assertEqual(data_readed, self._get_struct_PAGANIN() )
        #  2.5 check BEAMGEO
        data_readed = reader.get('BEAMGEO')
        self.assertEqual(data_readed, self._get_struct_BEAMGEO() )
        reader.close()

    def testWritedIsReadedOldOctaveVersion(self):
        """The same test as testWritedIsReaded but for octave version < 3.8
        """
        # test for octave version < 3.8
        writer = Octaveh5(3.6)

        writer.open(self.test_3_6_fname, 'a')

        # step 1 writing the file
        writer.write('FT', self._get_struct_FT())
        writer.write('PYHSTEXE', self._get_struct_PYHSTEXE())
        writer.write('FTAXIS', self._get_struct_FTAXIS())
        writer.write('PAGANIN', self._get_struct_PAGANIN())
        writer.write('BEAMGEO', self._get_struct_BEAMGEO())
        writer.close()

        # step 2 reading the file
        reader = Octaveh5(3.6).open(self.test_3_6_fname)
        #  2.1 check FT
        data_readed = reader.get('FT')
        self.assertEqual(data_readed, self._get_struct_FT() )
        #  2.2 check PYHSTEXE
        data_readed = reader.get('PYHSTEXE')
        self.assertEqual(data_readed, self._get_struct_PYHSTEXE() )
        #  2.3 check FTAXIS
        data_readed = reader.get('FTAXIS')
        self.assertEqual(data_readed, self._get_struct_FTAXIS() )
        #  2.4 check PAGANIN
        data_readed = reader.get('PAGANIN')
        self.assertEqual(data_readed, self._get_struct_PAGANIN() )
        #  2.5 check BEAMGEO
        data_readed = reader.get('BEAMGEO')
        self.assertEqual(data_readed, self._get_struct_BEAMGEO() )
        reader.close()

def suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(
        unittest.defaultTestLoader.loadTestsFromTestCase(TestOctaveH5))
    return test_suite

if __name__ == '__main__':
    unittest.main(defaultTest="suite")