test_experiment.py 6.06 KB
Newer Older
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
# 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__ = "09/10/2018"


import unittest
32
from id06workflow.core.experiment import Experiment, Dataset, AcquisitionDims, _Dim, DEFAULT_METADATA
33
from id06workflow.core.geometry.TwoThetaGeometry import TwoThetaGeometry
34 35 36
from id06workflow.test import utils
import numpy
import tempfile
37
import os
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


class TestDataset(unittest.TestCase):
    """TODO"""
    pass


class TestExperiement(unittest.TestCase):
    """Test behavior of :class:Experiment"""

    def testApplyThreshod(self):
        """
        Make sure the :func:`apply_threshold` is correctly applied
        """
        dims = (100, 100)
        pattern = numpy.arange(dims[0]*dims[1]).reshape(dims)
        background = numpy.zeros(dims)
        dataset = utils.createDataset(_dir=tempfile.mkdtemp(),
                                      pattern=pattern,
                                      background=background,
                                      nb_data_files=10)
        experiment = Experiment(dataset=dataset)
        thRes = pattern.copy()
        thRes[thRes < 20.0] = 0.0
        back_sub_data = experiment.apply_threshold(threshold=20, lower=True,
                                                   replacement_value=0.0)
        self.assertTrue(numpy.array_equal(back_sub_data[0], thRes))
        thRes[thRes > 70.0] = 0.0
        back_sub_data = experiment.apply_threshold(threshold=70, lower=False,
67
                                                   replacement_value=0.0)
68 69 70 71
        self.assertTrue(numpy.array_equal(back_sub_data[0], thRes))

    def testApplyBackgroundSubstraction(self):
        """
72
        Make sure the :func:`apply_background_subtraction` is correctly applied
73 74 75 76 77 78 79
        """
        dims = (100, 100)
        pattern = numpy.zeros(dims)
        background = numpy.random.random(dims)
        dataset = utils.createDataset(_dir=tempfile.mkdtemp(),
                                      pattern=pattern,
                                      background=background,
80 81
                                      nb_data_files=10,
                                      nb_flat_field_files=1)
82
        experiment = Experiment(dataset=dataset)
83
        back_sub_data = experiment.apply_background_subtraction()
84
        self.assertTrue(numpy.array_equal(back_sub_data[2], numpy.zeros(dims)))
85
        back_sub_data = experiment.apply_background_subtraction()
86 87
        self.assertTrue(numpy.array_equal(back_sub_data[3], numpy.zeros(dims)))

88
    @unittest.skipUnless(os.path.exists('/nobackup/linazimov/payno/dev/esrf/ID06/dataset/for_mapping'))
89 90 91 92 93
    def testMapping(self):
        # TODO: store datasets on the web and retrieve them using utils.getDataset()
        _dir = '/nobackup/linazimov/payno/dev/esrf/ID06/dataset/for_mapping'
        files = []
        [files.append(os.path.join(_dir, _file)) for _file in os.listdir(_dir)]
94
        dataset = Dataset(data_files_pattern=files)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        geometry = TwoThetaGeometry(
                twotheta=0.03, # for now those are defined in degree but should
                # be defined in radians
                xmag=1.0, # what is the unit of this ?
                xpixelsize=1.0,
                ypixelsize=1.0,
                orientation=TwoThetaGeometry.VERTICAL)
        experiment = Experiment(dataset=dataset, geometry=geometry)
        experiment.roi = (750, 750), (600, 600)
        dim = experiment.compute_dx2d_map()
        self.assertTrue(dim is not None)
        self.assertTrue(len(dim) is 1)
        self.assertTrue(dim[0].shape == (274, 274, 4))

        from id06workflow.gui.mapping import MappingPlot
        from silx.gui import qt
        app = qt.QApplication([])
        widget = MappingPlot()
        widget.setExperiment(experiment)
        widget.show()
        app.exec_()

117

118 119 120 121 122 123 124
class TestAcquisitionDims(unittest.TestCase):
    """
    Test the AcquisitionDims API
    """
    def test(self):
        """global test of the AcquisitionDims APi"""
        acquiDim = AcquisitionDims()
125 126 127
        self.assertTrue(acquiDim.shape == (1, ))
        acquiDim.add_dim(axis=0, dim=_Dim(kind=DEFAULT_METADATA, name='0', size=10))
        acquiDim.add_dim(axis=1, dim=_Dim(kind=DEFAULT_METADATA, name='1'))
128 129 130 131
        self.assertTrue(acquiDim.shape == (10, -1))
        acquiDim.set_size(axis=1, size=2)
        self.assertTrue(acquiDim.shape == (10, 2))
        acquiDim.clear()
132 133 134
        self.assertTrue(acquiDim.shape == (1, ))
        acquiDim.add_dim(axis=1, dim=_Dim(kind=DEFAULT_METADATA, name='0', size=10))
        acquiDim.add_dim(axis=4, dim=_Dim(kind=DEFAULT_METADATA, name='1'))
135 136 137
        self.assertTrue(acquiDim.shape == (1, 10, 1, 1, -1))


138 139
def suite():
    test_suite = unittest.TestSuite()
140
    for ui in (TestDataset, TestAcquisitionDims):
141 142 143 144 145 146
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
    return test_suite


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