test_real_dataset.py 6.76 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 32
# 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 logging
import unittest
from silx.gui import qt
from orangecontrib.id06workflow.test.OrangeWorkflowTest import OrangeWorflowTest
33
from id06workflow.core.experiment import Dataset
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
from id06workflow.unitsystem import metricsystem
from id06workflow.core.geometry.TwoThetaGeometry import TwoThetaGeometry
import os

logging.disable(logging.INFO)

app = qt.QApplication.instance() or qt.QApplication([])


class TestTrueData(OrangeWorflowTest):
    """
    Create dummy workflow just to make sure all the widget can interact together
    then are able to process the treatment
    """
    def setUp(self):
        OrangeWorflowTest.setUp(self)

        dataSelectionNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.dataselection.DataSelectionOW')
        geometryNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.geometry.GeometryOW')
        dataReductionNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.datareduction.DataReductionOW')
        roiSelectionNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.roiselection.RoiSelectionOW')
        noiseReductionNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.noisereduction.NoiseReductionOW')
61 62
        shiftCorrectionNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.shiftcorrection.ShiftCorrectionOW')
63 64 65 66 67 68 69 70 71 72 73
        mappingNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.mapping.MappingOW')
        saveNode = self.addWidget(
                'orangecontrib.id06workflow.widgets.saveexperiment.SaveExperimentOW')
        # TODO: add processing in here

        self.processOrangeEvents()

        self.link(dataSelectionNode, "data", geometryNode, "data")
        self.link(geometryNode, "data", dataReductionNode, "data")
        self.link(dataReductionNode, "data", roiSelectionNode, "data")
74 75 76
        self.link(roiSelectionNode, "data", noiseReductionNode, "data")
        self.link(noiseReductionNode, "data", shiftCorrectionNode, "data")
        self.link(shiftCorrectionNode, "data", mappingNode, "data")
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
        self.link(mappingNode, "data", saveNode, "data")
        self.processOrangeEvents()

        self._selectionWidget = self.getWidgetForNode(dataSelectionNode)
        self._geometryWidget = self.getWidgetForNode(geometryNode)
        self._dataReductionWidget = self.getWidgetForNode(dataSelectionNode)
        self._roiSelectionWidget = self.getWidgetForNode(roiSelectionNode)
        self._shiftCorrectionWidget = self.getWidgetForNode(shiftCorrectionNode)
        self._noiseReductionWidget = self.getWidgetForNode(noiseReductionNode)
        self._mappingWidget = self.getWidgetForNode(mappingNode)
        self._saveWidget = self.getWidgetForNode(saveNode)

        self._dataset = self.createDataset()

        self._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*metricsystem.micrometer, # TODO: unit
                ypixelsize=1.0*metricsystem.micrometer,
                orientation=TwoThetaGeometry.VERTICAL)

    def tearDown(self):
        OrangeWorflowTest.tearDown(self)

    def createDataset(self):
103 104
        data_file_pattern = '/users/payno/datasets/id06/strain_scan/reduced_strain/strain_0000.edf'
        assert os.path.exists(data_file_pattern)
105 106 107

        ff_files = []
        dir_ff = "/users/payno/datasets/id06/strain_scan/bg_ff_5s_1x1/"
108
        [ff_files.append(os.path.join(dir_ff, _file)) for _file in os.listdir(dir_ff)]
109

110
        return Dataset(data_files_pattern=data_file_pattern, ff_files=ff_files)
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

    def _moveToNextStep(self):
        app.processEvents()
        import time
        time.sleep(0.5)
        app.processEvents()

    def test(self):
        # define the dataset
        self._selectionWidget.setDataset(self._dataset)
        self._moveToNextStep()

        # define geometry
        self._geometryWidget.setSetupGeometry(self._geometry)
        self._moveToNextStep()
        self.assertTrue(self._geometryWidget.getSetupGeometry() == self._geometry)
        self._selectionWidget._process()

        # define noise reduction
        self._noiseReductionWidget.setBackgroundSubtraction(True)
        self._noiseReductionWidget.setLowerThreshold(5.0)
        self._noiseReductionWidget.setUpperThreshold(5000)

        # manage roi selection
        self._moveToNextStep()
        self._moveToNextStep()
        self._moveToNextStep()
        self._roiSelectionWidget.setROI(origin=(750, 750), size=(600, 600))
        self._roiSelectionWidget.validate()
        self._moveToNextStep()
        self._roiSelectionWidget.hide()
        self._moveToNextStep()

144 145 146 147 148
        # manage noise reduction
        self._noiseReductionWidget.validate()
        self._shiftCorrectionWidget.hide()
        self._moveToNextStep()

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        # manage shift correction
        self.assertTrue(self._shiftCorrectionWidget._editedExperiment is not None)
        self._shiftCorrectionWidget.setShift(dx=0.0, dy=0.0, dz=0.0)
        self._shiftCorrectionWidget.validate()
        self._shiftCorrectionWidget.hide()
        self._moveToNextStep()


def suite():
    test_suite = unittest.TestSuite()
    for ui in (TestTrueData,):
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
    return test_suite


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