test_edfscan.py 17.2 KB
Newer Older
payno's avatar
payno committed
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 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.
#
# ###########################################################################*/

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "24/01/2017"

import unittest
import logging
import tempfile
import numpy
import fabio.edfimage
from tomoscan.test.utils import UtilsTest
36
37
38
39
from tomoscan.esrf.mock import MockEDF
from tomoscan.esrf.edfscan import EDFTomoScan
from tomoscan.scanbase import TomoScanBase
from tomoscan.scanfactory import ScanFactory
40
from silx.io.url import DataUrl
payno's avatar
payno committed
41
42
43
44
import collections
import json
import shutil
import os
payno's avatar
payno committed
45
import silx.io.utils
payno's avatar
payno committed
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

logging.disable(logging.INFO)


class TestTomoBaseHashable(unittest.TestCase):
    """Make sure EDFTomoScan is hashable"""
    def setUp(self):
        self._folder = tempfile.mkdtemp()
        MockEDF.fastMockAcquisition(self._folder, n_radio=100)

    def tearDown(self):
        shutil.rmtree(self._folder)

    def test_is_hashable(self):
        tomo_base = TomoScanBase(scan=self._folder, type_='toto')
        self.assertTrue(isinstance(tomo_base, collections.Hashable))
        tomo_scan = EDFTomoScan(self._folder)
        self.assertTrue(isinstance(tomo_scan, collections.Hashable))


class TestScanFactory(unittest.TestCase):
    """Make sure the Scan factory is correctly working. Able to detect the valid
    scan type for a given file / directory
    """
    def test_no_scan(self):
        scan_dir = tempfile.mkdtemp()
        with self.assertRaises(ValueError):
            ScanFactory.create_scan_object(scan_dir)

    def test_scan_edf(self):
        scan_dir = UtilsTest.getDataset('test10')
        scan = ScanFactory.create_scan_object(scan_dir)
        self.assertTrue(isinstance(scan, EDFTomoScan))


payno's avatar
payno committed
81
class TestDarksFlats(unittest.TestCase):
payno's avatar
payno committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    """unit test for the FTSerieReconstruction functions"""
    def setUp(self):
        def saveData(data, _file, folder):
            file_desc = fabio.edfimage.EdfImage(data=data)
            file_desc.write(os.path.join(folder, _file))

        unittest.TestCase.setUp(self)
        self.folderTwoFlats = tempfile.mkdtemp()
        self.folderOneFlat = tempfile.mkdtemp()

        self.darkData = numpy.arange(100).reshape(10, 10)
        self.flatField_0 = numpy.arange(start=10, stop=110).reshape(10, 10)
        self.flatField_600 = numpy.arange(start=-10, stop=90).reshape(10, 10)
        self.flatField = numpy.arange(start=0, stop=100).reshape(10, 10)

        # save configuration one
        saveData(self.darkData, 'darkHST.edf', self.folderTwoFlats)
        saveData(self.flatField_0, 'refHST_0000.edf', self.folderTwoFlats)
        saveData(self.flatField_600, 'refHST_0600.edf', self.folderTwoFlats)

        # save configuration two
payno's avatar
payno committed
103
        saveData(self.darkData, 'dark.edf', self.folderOneFlat)
payno's avatar
payno committed
104
105
106
107
108
109
110
111
112
113
        saveData(self.flatField, 'refHST.edf', self.folderOneFlat)

        self.acquiOneFlat = EDFTomoScan(self.folderOneFlat)
        self.acquiTwoFlats = EDFTomoScan(self.folderTwoFlats)

    def tearDown(self):
        for f in (self.folderOneFlat, self.folderTwoFlats):
            shutil.rmtree(f)
        unittest.TestCase.tearDown(self)

payno's avatar
payno committed
114
115
116
117
118
    def testDarks(self):
        self.assertTrue(isinstance(self.acquiOneFlat.darks, dict))
        self.assertEqual(len(self.acquiOneFlat.darks), 1)
        self.assertTrue(isinstance(self.acquiTwoFlats.darks, dict))
        self.assertEqual(len(self.acquiTwoFlats.darks), 1)
payno's avatar
payno committed
119
120

        self.assertTrue(
payno's avatar
payno committed
121
122
123
124
125
126
127
128
129
            numpy.array_equal(silx.io.utils.get_data(self.acquiOneFlat.darks[0]),
                              self.darkData)
        )
        self.assertTrue(
            numpy.array_equal(silx.io.utils.get_data(self.acquiTwoFlats.darks[0]),
                                                     self.darkData)
        )

    def testFlats(self):
130
        """test that refHST flats are correctly found"""
payno's avatar
payno committed
131
132
133
134
135
136
        # check one flat file with two ref
        self.assertEqual(len(self.acquiOneFlat.flats), 1)
        self.assertTrue(isinstance(self.acquiOneFlat.flats[0], silx.io.url.DataUrl))

        data = silx.io.utils.get_data(self.acquiOneFlat.flats[0])
        numpy.array_equal(data, self.flatField)
payno's avatar
payno committed
137
138

        # check two flat files
payno's avatar
payno committed
139
140
141
142
143
144
145
146
147
        self.assertTrue(
            numpy.array_equal(silx.io.utils.get_data(self.acquiTwoFlats.flats[600]),
                              self.flatField_600)
        )
        self.assertTrue(
            numpy.array_equal(silx.io.utils.get_data(self.acquiTwoFlats.flats[0]),
                              self.flatField_0)
        )
        self.assertTrue(12 not in self.acquiTwoFlats.flats)
payno's avatar
payno committed
148
149


150
151
152
153
154
155
156
157
158
159
160
161
class TestOriDarksFlats(unittest.TestCase):
    """
    Test that original ref* and dark* before creation of refHST  and darkHST
    are correctly found
    """
    def setUp(self) -> None:
        unittest.TestCase.setUp(self)
        self.pixel_size = 0.05
        self.det_width = 10
        self.det_height = 10

        self.folder = tempfile.mkdtemp()
162
        # create 2 series of ref with 20 frames each and 2 refHST file
163
        for ref_index in ('0000', '1500'):
164
            # ref files
165
166
167
168
169
            for ref_sub_index in range(20):
                file_desc = fabio.edfimage.EdfImage(data=numpy.random.random((self.det_height,self.det_width)))
                file_name = '_'.join(('ref', str(ref_sub_index).zfill(4), str(ref_index).zfill(4) + '.edf'))
                file_name = os.path.join(self.folder, file_name)
                file_desc.write(file_name)
170
171
172
173
174
175
176
            # simulate two refHST generated by tomwer
            file_desc = fabio.edfimage.EdfImage(
                data=numpy.random.random((self.det_height, self.det_width)))
            file_name = 'refHST' + str(ref_index).zfill(4) + '.edf'
            file_name = os.path.join(self.folder, file_name)
            file_desc.write(file_name)

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
        # create one dark with 10 frames
        file_desc = fabio.edfimage.EdfImage(
            data=numpy.random.random((self.det_height, self.det_width)))
        for frame in range(9):
            file_desc.append_frame(data=numpy.random.random((self.det_height, self.det_width)))
        self._dark_file_name = os.path.join(self.folder, 'darkend0000.edf')
        file_desc.write(self._dark_file_name)

        self.scan = EDFTomoScan(self.folder)
        # create a .info for mocking the acquisition
        self.write_metadata(n_radio=0, scan_range=360, ref_n=20, dark_n=1)

    def get_info_file(self):
        return os.path.join(self.scan.path,
                            os.path.basename(self.scan.path) + '.info')

    def write_metadata(self, n_radio, scan_range, ref_n, dark_n):
        info_file = self.get_info_file()
        if not os.path.exists(info_file):
            # write the info file
            with open(self.get_info_file(), 'w') as info_file:
                info_file.write('TOMO_N=    ' + str(n_radio) + '\n')
                info_file.write('ScanRange= ' + str(scan_range) + '\n')
                info_file.write('REF_N=     ' + str(ref_n) + '\n')
                info_file.write('REF_ON=    ' + str(n_radio) + '\n')
                info_file.write('DARK_N=    ' + str(dark_n) + '\n')
                info_file.write('Dim_1=     ' + str(self.det_width) + '\n')
                info_file.write('Dim_2=     ' + str(self.det_height) + '\n')
                info_file.write('Col_beg=    0' + '\n')
                info_file.write('Col_end=   ' + str(self.det_width) + '\n')
                info_file.write('Row_beg=    0' + '\n')
                info_file.write('Row_end=    ' + str(self.det_height) + '\n')
                info_file.write('PixelSize=  ' + str(self.pixel_size) + '\n')

    def tearDown(self) -> None:
        shutil.rmtree(self.folder)
        unittest.TestCase.tearDown(self)

    def testFlatsOri(self):
        ref_urls = (self.scan.get_refs_url(prefix='ref', scan_path=self.scan.path))
217
        self.assertEqual(len(ref_urls), 42)
218
219
220
221
222
        # checks some random files
        file_paths = [url.file_path() for url in ref_urls.values()]
        for ref_file in ('ref_0000_1500.edf', 'ref_0004_0000.edf', 'ref_0019_1500.edf'):
            ref_full_path = os.path.join(self.scan.path, ref_file)
            self.assertTrue(ref_full_path in file_paths)
223
224
        ref_urls = (self.scan.get_refs_url(prefix='refHST', scan_path=self.scan.path))
        self.assertEqual(len(ref_urls), 2)
225

226
227
228
229
230
        ref_urls = (self.scan.get_refs_url(prefix='ref',
                                           scan_path=self.scan.path,
                                           ignore=('HST',)))
        self.assertEqual(len(ref_urls), 40)

231
232
233
234
235
236
237
238
239
    def testDarksOri(self):
        darks = self.scan.get_darks_url(prefix='darkend',
                                        scan_path=self.scan.path)
        self.assertEqual(len(darks), 10)
        # check one random url
        url = DataUrl(file_path=self._dark_file_name,  data_slice=[4,], scheme='fabio')
        self.assertTrue(url in darks.values())


payno's avatar
payno committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
class TestProjections(unittest.TestCase):
    """Test that the """
    def setUp(self) -> None:
        self.folder = tempfile.mkdtemp()

    def tearDown(self) -> None:
        shutil.rmtree(self.folder)

    def testProjectionNoExtra(self):
        mock = MockEDF(scan_path=self.folder, n_radio=10, n_extra_radio=0)
        mock.end_acquisition()
        scan = EDFTomoScan(scan=self.folder)
        self.assertEqual(len(scan.projections), 10)

    def testProjectionUpdate(self):
        mock = MockEDF(scan_path=self.folder, n_radio=10, n_ini_radio=3)
        scan = EDFTomoScan(scan=self.folder)
        self.assertEqual(len(scan.projections), 3)
        mock.add_radio()
        self.assertEqual(len(scan.projections), 3)
        scan.update()
        self.assertEqual(len(scan.projections), 4)
262
263
264

        index_0 = list(scan.projections.keys())[0]
        self.assertTrue(isinstance(scan.projections[index_0], silx.io.url.DataUrl))
payno's avatar
payno committed
265
266

    def testProjectionWithExtraRadio(self):
267
268
        mock = MockEDF(scan_path=self.folder, n_radio=11, n_extra_radio=2,
                       scan_range=180)
payno's avatar
payno committed
269
270
        mock.end_acquisition()
        scan = EDFTomoScan(scan=self.folder)
271
272
273
274
275
276
277
278
        self.assertEqual(len(scan.projections), 11+2)
        proj_angle_dict = scan.get_proj_angle_url()
        self.assertEqual(len(proj_angle_dict), 11+2)
        self.assertTrue(90 in proj_angle_dict)
        self.assertTrue(180 in proj_angle_dict)
        self.assertTrue('90(1)' in proj_angle_dict)
        self.assertTrue('0(1)' in proj_angle_dict)
        self.assertTrue(360 not in proj_angle_dict)
payno's avatar
payno committed
279
280


payno's avatar
payno committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
class TestScanValidatorFindFiles(unittest.TestCase):
    """Function testing the getReconstructionsPaths function is correctly
    functioning"""

    DIM_MOCK_SCAN = 10

    N_RADIO = 20
    N_RECONS = 10
    N_PAG_RECONS = 5

    def setUp(self):
        # create scan folder
        self.path = tempfile.mkdtemp()
        MockEDF.mockScan(scanID=self.path,
                         nRadio=self.N_RADIO,
                         nRecons=self.N_RECONS,
                         nPagRecons=self.N_PAG_RECONS,
                         dim=self.DIM_MOCK_SCAN)
        basename = os.path.basename(self.path)

        # add some random files
        for _file in ('45gfdgfg1.edf', '465slicetest1.edf', 'slice_ab.edf'):
            with open(os.path.join(self.path, basename + _file), "w+") as ofile:
                ofile.write('test')

    def tearDown(self):
        if os.path.isdir(self.path):
            shutil.rmtree(self.path)


class TestRadioPath(unittest.TestCase):
    """Test static method getRadioPaths for EDFTomoScan"""
    def test(self):
        files = [
          'essai1_0008.edf',
          'essai1_0019.edf',
          'essai1_0030.edf',
          'essai1_0041.edf',
          'essai1_0052.edf',
          'essai1_0063.edf',
          'essai1_0074.edf',
          'essai1_0085.edf',
          'essai1_0096.edf',
          'essai1_.par',
          'refHST0100.edf',
          'darkend0000.edf',
          'essai1_0009.edf',
          'essai1_0020.edf',
          'essai1_0031.edf',
          'essai1_0042.edf',
          'essai1_0053.edf',
          'essai1_0064.edf',
          'essai1_0075.edf',
          'essai1_0086.edf',
          'essai1_0097.edf',
          'essai1_.rec',
          'essai1_0000.edf',
          'essai1_0010.edf',
          'essai1_0021.edf',
          'essai1_0032.edf',
          'essai1_0043.edf',
          'essai1_0054.edf',
          'essai1_0065.edf',
          'essai1_0076.edf',
          'essai1_0087.edf',
          'essai1_0098.edf',
          'essai1_slice_1023.edf',
          'essai1_0001.edf',
          'essai1_0011.edf',
          'essai1_0022.edf',
          'essai1_0033.edf',
          'essai1_0044.edf',
          'essai1_0055.edf',
          'essai1_0066.edf',
          'essai1_0077.edf',
          'essai1_0088.edf',
          'essai1_0099.edf',
          'essai1_slice.info',
          'essai1_0001.par',
          'essai1_0012.edf',
          'essai1_0023.edf',
          'essai1_0034.edf',
          'essai1_0045.edf',
          'essai1_0056.edf',
          'essai1_0067.edf',
          'essai1_0078.edf',
          'essai1_0089.edf',
          'essai1_0100.edf',
          'essai1_slice.par',
          'essai1_0002.edf',
          'essai1_0013.edf',
          'essai1_0024.edf',
          'essai1_0035.edf',
          'essai1_0046.edf',
          'essai1_0057.edf',
          'essai1_0068.edf',
          'essai1_0079.edf',
          'essai1_0090.edf',
          'essai1_0101.edf',
          'essai1_slice.xml',
          'essai1_0003.edf',
          'essai1_0014.edf',
          'essai1_0025.edf',
          'essai1_0036.edf',
          'essai1_0047.edf',
          'essai1_0058.edf',
          'essai1_0069.edf',
          'essai1_0080.edf',
          'essai1_0091.edf',
          'essai1_0102.edf',
          'essai1_.xml',
          'essai1_0004.edf',
          'essai1_0015.edf',
          'essai1_0026.edf',
          'essai1_0037.edf',
          'essai1_0048.edf',
          'essai1_0059.edf',
          'essai1_0070.edf',
          'essai1_0081.edf',
          'essai1_0092.edf',
          'essai1_0103.edf',
          'histogram_essai1_slice',
          'essai1_0005.edf',
          'essai1_0016.edf',
          'essai1_0027.edf',
          'essai1_0038.edf',
          'essai1_0049.edf',
          'essai1_0060.edf',
          'essai1_0071.edf',
          'essai1_0082.edf',
          'essai1_0093.edf',
          'essai1_0104.edf',
          'machinefile',
          'essai1_0006.edf',
          'essai1_0017.edf',
          'essai1_0028.edf',
          'essai1_0039.edf',
          'essai1_0050.edf',
          'essai1_0061.edf',
          'essai1_0072.edf',
          'essai1_0083.edf',
          'essai1_0094.edf',
          'essai1_.cfg',
          'pyhst_out.txt',
          'essai1_0007.edf',
          'essai1_0018.edf',
          'essai1_0029.edf',
          'essai1_0040.edf',
          'essai1_0051.edf',
          'essai1_0062.edf',
          'essai1_0073.edf',
          'essai1_0084.edf',
          'essai1_0095.edf'
          ]

        nbRadio = 0
        for f in files:
            nbRadio += EDFTomoScan.is_a_proj_path(f, 'essai1_')
        self.assertTrue(nbRadio == 105)


class TestScanBaseJSON(unittest.TestCase):
    """Test save / load to / for, json files"""
    def setUp(self):
        self.scan_folder = tempfile.mkdtemp()
        self.json_folder = tempfile.mkdtemp()

    def tearDown(self):
payno's avatar
payno committed
449
450
        for folder in (self.scan_folder, self.json_folder):
            shutil.rmtree(folder)
payno's avatar
payno committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469

    def testEmptyJson(self):
        """Test behavior if we try to load from empty json stream"""
        _file = os.path.join(self.json_folder, 'json_file.json')
        with open(_file, 'w') as outfile:
            json.dump({}, outfile)

        with open(_file, 'r') as outfile:
            with self.assertRaises(ValueError):
                ScanFactory.create_from_json(outfile)

        scan_to_update = EDFTomoScan(scan=None)
        with open(_file, 'r') as outfile:
            with self.assertRaises(ValueError):
                scan_to_update.load_from_dict(outfile)


def suite():
    test_suite = unittest.TestSuite()
payno's avatar
payno committed
470
471
    for ui in (TestScanFactory, TestDarksFlats,
               TestScanValidatorFindFiles, TestRadioPath, TestProjections,
472
               TestTomoBaseHashable, TestOriDarksFlats):
payno's avatar
payno committed
473
474
475
476
477
478
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
    return test_suite


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