Beispiel #1
0
    def setUp(self):
        self.img_model = ImgModel()
        self.mask_model = MaskModel()
        self.spectrum_model = PatternModel()
        self.calibration_model = CalibrationModel(self.img_model)
        self.calibration_model.integrate_1d = MagicMock(
            return_value=(self.calibration_model.tth,
                          self.calibration_model.int))

        self.phase_model = PhaseModel()

        self.integration_widget = IntegrationWidget()

        self.integration_controller = IntegrationController(
            {'spectrum': data_path},
            widget=self.integration_widget,
            img_model=self.img_model,
            mask_model=self.mask_model,
            calibration_model=self.calibration_model,
            spectrum_model=self.spectrum_model,
            phase_model=self.phase_model)
        self.calibration_model.load(
            os.path.join(data_path, 'CeO2_Pilatus1M.poni'))
        self.img_model.load(os.path.join(data_path, 'CeO2_Pilatus1M.tif'))

        self.integration_spectrum_controller = self.integration_controller.spectrum_controller
        self.integration_image_controller = self.integration_controller.image_controller
    def setUp(self):
        self.img_model = ImgModel()
        self.mask_model = MaskModel()
        self.spectrum_model = PatternModel()

        # setting up the calibration model but mocking the integration for speed
        self.calibration_model = CalibrationModel(self.img_model)
        self.calibration_model.num_points = 1000
        dummy_x = np.linspace(0, 25, 1000)
        dummy_y = np.sin(dummy_x)
        self.calibration_model.integrate_1d = mock.Mock(return_value=(dummy_x,
                                                                      dummy_y))

        self.phase_model = PhaseModel()
        self.widget = IntegrationWidget()
        self.integration_controller = IntegrationController(
            {'spectrum': data_path},
            widget=self.widget,
            img_model=self.img_model,
            mask_model=self.mask_model,
            calibration_model=self.calibration_model,
            spectrum_model=self.spectrum_model,
            phase_model=self.phase_model)
        self.image_controller = self.integration_controller.image_controller
        self.calibration_model.load(
            os.path.join(data_path, 'CeO2_Pilatus1M.poni'))
        self.img_model.load(os.path.join(data_path, 'CeO2_Pilatus1M.tif'))
Beispiel #3
0
 def setUp(self):
     self.integration_widget = IntegrationWidget()
     self.move_widget = self.integration_widget.move_widget
     self.setup_widget = self.integration_widget.move_widget.motors_setup_widget
     self.img_model = ImgModel()
     self.epics_controller = EpicsController(self.integration_widget,
                                             self.img_model)
    def setUp(self):
        self.image_model = ImgModel()
        self.calibration_model = CalibrationModel()
        self.calibration_model.is_calibrated = True
        self.calibration_model.spectrum_geometry.wavelength = 0.31E-10
        self.calibration_model.integrate_1d = MagicMock(return_value=(self.calibration_model.tth,
                                                                      self.calibration_model.int))
        self.spectrum_model = PatternModel()
        self.phase_model = PhaseModel()
        self.widget = IntegrationWidget()
        self.widget.pattern_widget._auto_range = True
        self.phase_tw = self.widget.phase_tw

        self.spectrum_controller = PatternController({}, self.widget, self.image_model, None,
                                                     self.calibration_model, self.spectrum_model)
        self.controller = PhaseController({}, self.widget, self.calibration_model, self.spectrum_model,
                                          self.phase_model)
        self.spectrum_controller.load(os.path.join(data_path, 'spectrum_001.xy'))
 def setUp(self):
     self.widget = IntegrationWidget()
     self.spectrum_model = PatternModel()
     self.img_model = ImgModel()
     self.spectrum_controller = PatternController({}, self.widget,
                                                  self.img_model, None,
                                                  None, self.spectrum_model)
     self.background_controller = BackgroundController({}, self.widget,
                                                       self.img_model,
                                                       self.spectrum_model)
     self.overlay_tw = self.widget.overlay_tw
    def setUp(self):
        self.working_dir = {'image': ''}

        self.widget = IntegrationWidget()
        self.image_model = ImgModel()
        self.mask_model = MaskModel()
        self.spectrum_model = PatternModel()
        self.calibration_model = CalibrationModel(self.image_model)

        self.controller = ImageController(
            working_dir=self.working_dir,
            widget=self.widget,
            img_model=self.image_model,
            mask_model=self.mask_model,
            spectrum_model=self.spectrum_model,
            calibration_model=self.calibration_model)
    def setUp(self):
        self.img_model = ImgModel()
        self.mask_model = MaskModel()
        self.spectrum_model = PatternModel()
        self.calibration_model = CalibrationModel(self.img_model)
        self.calibration_model.integrate_1d = MagicMock(return_value=(self.calibration_model.tth,
                                                                      self.calibration_model.int))

        self.phase_model = PhaseModel()

        self.integration_widget = IntegrationWidget()

        self.integration_controller = IntegrationController({'spectrum': data_path},
                                                            widget=self.integration_widget,
                                                            img_model=self.img_model,
                                                            mask_model=self.mask_model,
                                                            calibration_model=self.calibration_model,
                                                            spectrum_model=self.spectrum_model,
                                                            phase_model=self.phase_model)
        self.calibration_model.load(os.path.join(data_path, 'CeO2_Pilatus1M.poni'))
        self.img_model.load(os.path.join(data_path, 'CeO2_Pilatus1M.tif'))

        self.integration_spectrum_controller = self.integration_controller.spectrum_controller
        self.integration_image_controller = self.integration_controller.image_controller
class IntegrationFunctionalTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.app = QtGui.QApplication([])

    @classmethod
    def tearDownClass(cls):
        cls.app.quit()
        cls.app.deleteLater()

    def setUp(self):
        self.img_model = ImgModel()
        self.mask_model = MaskModel()
        self.spectrum_model = PatternModel()
        self.calibration_model = CalibrationModel(self.img_model)
        self.calibration_model.integrate_1d = MagicMock(return_value=(self.calibration_model.tth,
                                                                      self.calibration_model.int))

        self.phase_model = PhaseModel()

        self.integration_widget = IntegrationWidget()

        self.integration_controller = IntegrationController({'spectrum': data_path},
                                                            widget=self.integration_widget,
                                                            img_model=self.img_model,
                                                            mask_model=self.mask_model,
                                                            calibration_model=self.calibration_model,
                                                            spectrum_model=self.spectrum_model,
                                                            phase_model=self.phase_model)
        self.calibration_model.load(os.path.join(data_path, 'CeO2_Pilatus1M.poni'))
        self.img_model.load(os.path.join(data_path, 'CeO2_Pilatus1M.tif'))

        self.integration_spectrum_controller = self.integration_controller.spectrum_controller
        self.integration_image_controller = self.integration_controller.image_controller

    def tearDown(self):
        del self.integration_spectrum_controller
        del self.mask_model
        del self.img_model
        del self.calibration_model.cake_geometry
        del self.calibration_model.spectrum_geometry
        del self.calibration_model
        del self.integration_widget
        del self.integration_controller
        gc.collect()

    def enter_value_into_text_field(self, text_field, value):
        text_field.setText('')
        QTest.keyClicks(text_field, str(value))
        QTest.keyPress(text_field, QtCore.Qt.Key_Enter)
        QtGui.QApplication.processEvents()

    def test_changing_number_of_integration_bins(self):
        # Edith wants to change the number of integration bins in order to see the effect of binning onto her line
        # shape. She sees that there is an option in the X tab and deselects automatic and sees that the sbinbox
        # becomes editable.
        self.assertFalse(self.integration_widget.bin_count_txt.isEnabled())
        self.integration_widget.automatic_binning_cb.setChecked(False)
        self.assertTrue(self.integration_widget.bin_count_txt.isEnabled())

        # she sees that the current value and wants to double it and notices that the spectrum looks a little bit
        # smoother
        previous_number_of_points = len(self.spectrum_model.pattern.x)
        self.enter_value_into_text_field(self.integration_widget.bin_count_txt, 2 * previous_number_of_points)

        self.calibration_model.integrate_1d.assert_called_with(num_points=2 * previous_number_of_points,
                                                               mask=None, unit='2th_deg')

        # then she decides that having an automatic estimation may probably be better and changes back to automatic.
        # immediately the number is restored and the image looks like when she started
        self.integration_widget.automatic_binning_cb.setChecked(True)
        self.calibration_model.integrate_1d.assert_called_with(num_points=None,
                                                               mask=None, unit='2th_deg')

    def test_changing_supersampling_amount_integrating_to_cake_with_mask(self):
        # Edith opens the program, calibrates everything and looks in to the options menu. She sees that there is a
        # miraculous parameter called supersampling. It is currently set to 1 which seems to be normal
        self.assertEqual(self.integration_widget.supersampling_sb.value(), 1)

        # then she sets it to two and she sees that the number of spectrum bin changes and that the spectrum looks
        # smoother

        # values before:
        px1 = self.calibration_model.spectrum_geometry.pixel1
        px2 = self.calibration_model.spectrum_geometry.pixel2

        img_shape = self.img_model.img_data.shape

        self.integration_widget.supersampling_sb.setValue(2)
        self.assertEqual(self.calibration_model.spectrum_geometry.pixel1, 0.5 * px1)
        self.assertEqual(self.calibration_model.spectrum_geometry.pixel2, 0.5 * px2)
        self.assertEqual(self.calibration_model.cake_geometry.pixel1, px1)
        self.assertEqual(self.calibration_model.cake_geometry.pixel2, px2)

        self.assertEqual(self.img_model.img_data.shape[0], 2 * img_shape[0])
        self.assertEqual(self.img_model.img_data.shape[1], 2 * img_shape[1])

        self.mask_model.load_mask(os.path.join(data_path, 'test.mask'))
        QTest.mouseClick(self.integration_widget.img_mask_btn, QtCore.Qt.LeftButton)
        QTest.mouseClick(self.integration_widget.img_mode_btn, QtCore.Qt.LeftButton)

    def test_saving_image(self):
        # the widget has to be shown to be able to save the image:
        self.integration_widget.show()
        # Tests if the image save procedures are working for the different possible file endings
        self.integration_image_controller.save_img(os.path.join(data_path, 'Test_img.png'))
        self.integration_image_controller.save_img(os.path.join(data_path, 'Test_img.tiff'))

        self.assertTrue(os.path.exists(os.path.join(data_path, 'Test_img.png')))
        self.assertTrue(os.path.exists(os.path.join(data_path, 'Test_img.tiff')))

        os.remove(os.path.join(data_path, 'Test_img.png'))
        os.remove(os.path.join(data_path, 'Test_img.tiff'))

    def test_saving_spectrum(self):
        # the widget has to be shown to be able to save the image:
        self.integration_widget.show()

        # Tests if the spectrum save procedures is are working for all fileendings
        def save_spectra_test_for_size_and_delete(self):
            self.integration_spectrum_controller.save_pattern(os.path.join(data_path, 'Test_spec.xy'))
            self.integration_spectrum_controller.save_pattern(os.path.join(data_path, 'Test_spec.chi'))
            self.integration_spectrum_controller.save_pattern(os.path.join(data_path, 'Test_spec.dat'))
            self.integration_spectrum_controller.save_pattern(os.path.join(data_path, 'Test_spec.png'))
            self.integration_spectrum_controller.save_pattern(os.path.join(data_path, 'Test_spec.svg'))

            self.assertTrue(os.path.exists(os.path.join(data_path, 'Test_spec.xy')))
            self.assertTrue(os.path.exists(os.path.join(data_path, 'Test_spec.chi')))
            self.assertTrue(os.path.exists(os.path.join(data_path, 'Test_spec.dat')))
            self.assertTrue(os.path.exists(os.path.join(data_path, 'Test_spec.png')))
            self.assertTrue(os.path.exists(os.path.join(data_path, 'Test_spec.svg')))

            self.assertGreater(os.stat(os.path.join(data_path, 'Test_spec.xy')).st_size, 1)
            self.assertGreater(os.stat(os.path.join(data_path, 'Test_spec.chi')).st_size, 1)
            self.assertGreater(os.stat(os.path.join(data_path, 'Test_spec.dat')).st_size, 1)
            self.assertGreater(os.stat(os.path.join(data_path, 'Test_spec.png')).st_size, 1)
            self.assertGreater(os.stat(os.path.join(data_path, 'Test_spec.svg')).st_size, 1)

            os.remove(os.path.join(data_path, 'Test_spec.xy'))
            os.remove(os.path.join(data_path, 'Test_spec.chi'))
            os.remove(os.path.join(data_path, 'Test_spec.dat'))
            os.remove(os.path.join(data_path, 'Test_spec.png'))
            os.remove(os.path.join(data_path, 'Test_spec.svg'))

        save_spectra_test_for_size_and_delete(self)
        QTest.mouseClick(self.integration_spectrum_controller.widget.spec_q_btn, QtCore.Qt.LeftButton)
        save_spectra_test_for_size_and_delete(self)
        QTest.mouseClick(self.integration_spectrum_controller.widget.spec_d_btn, QtCore.Qt.LeftButton)
        save_spectra_test_for_size_and_delete(self)

    def test_undocking_and_docking_img_frame(self):
        QTest.mouseClick(self.integration_widget.img_dock_btn, QtCore.Qt.LeftButton)
        QTest.mouseClick(self.integration_widget.img_dock_btn, QtCore.Qt.LeftButton)

    def test_loading_multiple_images_and_batch_integrate_them(self):
        self.integration_widget.spec_autocreate_cb.setChecked(True)
        self.assertTrue(self.integration_widget.spec_autocreate_cb.isChecked())
        self.integration_image_controller.load_file([os.path.join(data_path, 'image_001.tif'),
                                                     os.path.join(data_path, 'image_002.tif')])
        self.assertTrue(os.path.exists(os.path.join(data_path, 'image_001.xy')))
        self.assertTrue(os.path.exists(os.path.join(data_path, 'image_002.xy')))
        os.remove(os.path.join(data_path, 'image_001.xy'))
        os.remove(os.path.join(data_path, 'image_002.xy'))
Beispiel #9
0
 def setUp(self):
     self.widget = IntegrationWidget()
     self.spectrum_model = PatternModel()
     self.overlay_controller = OverlayController({}, self.widget, self.spectrum_model)
     self.overlay_tw = self.widget.overlay_tw
Beispiel #10
0
class OverlayControllerTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.app = QtGui.QApplication([])

    @classmethod
    def tearDownClass(cls):
        cls.app.quit()
        cls.app.deleteLater()

    def setUp(self):
        self.widget = IntegrationWidget()
        self.spectrum_model = PatternModel()
        self.overlay_controller = OverlayController({}, self.widget, self.spectrum_model)
        self.overlay_tw = self.widget.overlay_tw

    def tearDown(self):
        del self.widget
        del self.spectrum_model
        del self.overlay_tw
        del self.overlay_controller
        gc.collect()

    def test_manual_deleting_overlays(self):
        self.load_overlays()

        self.assertEqual(self.overlay_tw.rowCount(), 6)
        self.assertEqual(len(self.spectrum_model.overlays), 6)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 6)
        self.assertEqual(self.overlay_tw.currentRow(), 5)

        self.overlay_controller.remove_overlay_btn_click_callback()
        self.assertEqual(self.overlay_tw.rowCount(), 5)
        self.assertEqual(len(self.spectrum_model.overlays), 5)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 5)
        self.assertEqual(self.overlay_tw.currentRow(), 4)

        self.widget.select_overlay(1)
        self.overlay_controller.remove_overlay_btn_click_callback()
        self.assertEqual(self.overlay_tw.rowCount(), 4)
        self.assertEqual(len(self.spectrum_model.overlays), 4)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 4)
        self.assertEqual(self.overlay_tw.currentRow(), 1)

        self.widget.select_overlay(0)
        self.overlay_controller.remove_overlay_btn_click_callback()
        self.assertEqual(self.overlay_tw.rowCount(), 3)
        self.assertEqual(len(self.spectrum_model.overlays), 3)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 3)
        self.assertEqual(self.overlay_tw.currentRow(), 0)

        self.overlay_controller.remove_overlay_btn_click_callback()
        self.overlay_controller.remove_overlay_btn_click_callback()
        self.overlay_controller.remove_overlay_btn_click_callback()
        self.assertEqual(self.overlay_tw.rowCount(), 0)
        self.assertEqual(len(self.spectrum_model.overlays), 0)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 0)
        self.assertEqual(self.overlay_tw.currentRow(), -1)

        self.overlay_controller.remove_overlay_btn_click_callback()
        self.assertEqual(self.overlay_tw.rowCount(), 0)
        self.assertEqual(self.overlay_tw.currentRow(), -1)

    def test_automatic_deleting_overlays(self):
        self.load_overlays()
        self.load_overlays()
        self.assertEqual(self.overlay_tw.rowCount(), 12)
        self.assertEqual(len(self.spectrum_model.overlays), 12)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 12)

        self.overlay_controller.clear_overlays_btn_click_callback()
        self.assertEqual(self.overlay_tw.rowCount(), 0)
        self.assertEqual(len(self.spectrum_model.overlays), 0)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 0)
        self.assertEqual(self.overlay_tw.currentRow(), -1)

        multiplier = 1
        for dummy_index in range(multiplier):
            self.load_overlays()

        self.assertEqual(self.overlay_tw.rowCount(), multiplier * 6)
        self.overlay_controller.clear_overlays_btn_click_callback()
        self.assertEqual(self.overlay_tw.rowCount(), 0)
        self.assertEqual(len(self.spectrum_model.overlays), 0)
        self.assertEqual(len(self.widget.pattern_widget.overlays), 0)
        self.assertEqual(self.overlay_tw.currentRow(), -1)

    def test_change_scaling_in_view(self):
        self.load_overlays()
        self.widget.select_overlay(2)

        self.widget.overlay_scale_sb.setValue(2.0)
        self.assertEqual(self.spectrum_model.get_overlay_scaling(2), 2)

        # tests if overlay is updated in spectrum
        x, y = self.spectrum_model.overlays[2].data
        x_spec, y_spec = self.widget.pattern_widget.overlays[2].getData()

        self.assertAlmostEqual(np.sum(y - y_spec), 0)

    def test_change_offset_in_view(self):
        self.load_overlays()
        self.widget.select_overlay(3)

        self.widget.overlay_offset_sb.setValue(100)
        self.assertEqual(self.spectrum_model.get_overlay_offset(3), 100)

        x, y = self.spectrum_model.overlays[3].data
        x_spec, y_spec = self.widget.pattern_widget.overlays[3].getData()

        self.assertAlmostEqual(np.sum(y - y_spec), 0)

    def test_setting_overlay_as_bkg(self):
        self.load_overlays()
        self.spectrum_model.load_pattern(os.path.join(data_path, 'spectrum_001.xy'))
        self.widget.select_overlay(0)
        QTest.mouseClick(self.widget.overlay_set_as_bkg_btn, QtCore.Qt.LeftButton)

        self.assertTrue(self.widget.overlay_set_as_bkg_btn.isChecked())

        self.assertEqual(self.spectrum_model.bkg_ind, 0)
        x, y = self.spectrum_model.pattern.data
        self.assertEqual(np.sum(y), 0)

    def test_setting_overlay_as_bkg_and_changing_scale(self):
        self.load_overlays()
        self.spectrum_model.load_pattern(os.path.join(data_path, 'spectrum_001.xy'))
        self.widget.select_overlay(0)
        QTest.mouseClick(self.widget.overlay_set_as_bkg_btn, QtCore.Qt.LeftButton)

        self.widget.overlay_scale_sb.setValue(2)
        _, y = self.spectrum_model.pattern.data
        _, y_original = self.spectrum_model.pattern.data
        self.assertEqual(np.sum(y - y_original), 0)

    def test_setting_overlay_as_bkg_and_changing_offset(self):
        self.load_overlays()
        self.spectrum_model.load_pattern(os.path.join(data_path, 'spectrum_001.xy'))
        self.widget.select_overlay(0)
        QTest.mouseClick(self.widget.overlay_set_as_bkg_btn, QtCore.Qt.LeftButton)

        self.widget.overlay_offset_sb.setValue(100)
        _, y = self.spectrum_model.pattern.data
        self.assertEqual(np.sum(y), -100 * y.size)

    def test_setting_overlay_as_bkg_and_then_change_to_new_overlay_as_bkg(self):
        self.load_overlays()
        self.spectrum_model.load_pattern(os.path.join(data_path, 'spectrum_001.xy'))
        self.widget.select_overlay(0)
        QTest.mouseClick(self.widget.overlay_set_as_bkg_btn, QtCore.Qt.LeftButton)

        _, y = self.spectrum_model.pattern.data
        self.assertEqual(np.sum(y), 0)

        self.widget.select_overlay(1)
        self.widget.overlay_scale_sb.setValue(2)
        QTest.mouseClick(self.widget.overlay_set_as_bkg_btn, QtCore.Qt.LeftButton)

        _, y = self.spectrum_model.pattern.data
        self.assertNotEqual(np.sum(y), 0)

    def test_setting_spectrum_as_bkg(self):
        self.spectrum_model.load_pattern(os.path.join(data_path, 'spectrum_001.xy'))
        QTest.mouseClick(self.widget.qa_set_as_background_btn, QtCore.Qt.LeftButton)

        self.assertTrue(self.widget.overlay_set_as_bkg_btn.isChecked())

        _, y = self.spectrum_model.pattern.data
        self.assertEqual(np.sum(y), 0)

    def test_having_overlay_as_bkg_and_deleting_it(self):
        self.spectrum_model.load_pattern(os.path.join(data_path, 'spectrum_001.xy'))
        QTest.mouseClick(self.widget.qa_set_as_background_btn, QtCore.Qt.LeftButton)

        QTest.mouseClick(self.widget.overlay_del_btn, QtCore.Qt.LeftButton)

        self.assertFalse(self.widget.overlay_set_as_bkg_btn.isChecked())
        self.assertEqual(self.widget.overlay_tw.rowCount(), 0)

        _, y = self.spectrum_model.pattern.data
        self.assertNotEqual(np.sum(y), 0)

    def test_overlay_waterfall(self):
        self.load_overlays()
        self.widget.waterfall_separation_txt.setText("10")
        QTest.mouseClick(self.widget.waterfall_btn, QtCore.Qt.LeftButton)

        self.assertEqual(self.spectrum_model.overlays[5].offset, -10)
        self.assertEqual(self.spectrum_model.overlays[4].offset, -20)

        QTest.mouseClick(self.widget.reset_waterfall_btn, QtCore.Qt.LeftButton)

        self.assertEqual(self.spectrum_model.overlays[5].offset, 0)
        self.assertEqual(self.spectrum_model.overlays[5].offset, 0)

    def load_overlays(self):
        self.load_overlay('spectrum_001.xy')
        self.load_overlay('spectrum_001.xy')
        self.load_overlay('spectrum_001.xy')
        self.load_overlay('spectrum_001.xy')
        self.load_overlay('spectrum_001.xy')
        self.load_overlay('spectrum_001.xy')

    def load_overlay(self, filename):
        self.overlay_controller.add_overlay_btn_click_callback(os.path.join(data_path, filename))
Beispiel #11
0
class IntegrationFunctionalTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.app = QtGui.QApplication([])

    @classmethod
    def tearDownClass(cls):
        cls.app.quit()
        cls.app.deleteLater()

    def setUp(self):
        self.img_model = ImgModel()
        self.mask_model = MaskModel()
        self.spectrum_model = PatternModel()
        self.calibration_model = CalibrationModel(self.img_model)
        self.calibration_model.integrate_1d = MagicMock(
            return_value=(self.calibration_model.tth,
                          self.calibration_model.int))

        self.phase_model = PhaseModel()

        self.integration_widget = IntegrationWidget()

        self.integration_controller = IntegrationController(
            {'spectrum': data_path},
            widget=self.integration_widget,
            img_model=self.img_model,
            mask_model=self.mask_model,
            calibration_model=self.calibration_model,
            spectrum_model=self.spectrum_model,
            phase_model=self.phase_model)
        self.calibration_model.load(
            os.path.join(data_path, 'CeO2_Pilatus1M.poni'))
        self.img_model.load(os.path.join(data_path, 'CeO2_Pilatus1M.tif'))

        self.integration_spectrum_controller = self.integration_controller.spectrum_controller
        self.integration_image_controller = self.integration_controller.image_controller

    def tearDown(self):
        del self.integration_spectrum_controller
        del self.mask_model
        del self.img_model
        del self.calibration_model.cake_geometry
        del self.calibration_model.spectrum_geometry
        del self.calibration_model
        del self.integration_widget
        del self.integration_controller
        gc.collect()

    def enter_value_into_text_field(self, text_field, value):
        text_field.setText('')
        QTest.keyClicks(text_field, str(value))
        QTest.keyPress(text_field, QtCore.Qt.Key_Enter)
        QtGui.QApplication.processEvents()

    def test_changing_number_of_integration_bins(self):
        # Edith wants to change the number of integration bins in order to see the effect of binning onto her line
        # shape. She sees that there is an option in the X tab and deselects automatic and sees that the sbinbox
        # becomes editable.
        self.assertFalse(self.integration_widget.bin_count_txt.isEnabled())
        self.integration_widget.automatic_binning_cb.setChecked(False)
        self.assertTrue(self.integration_widget.bin_count_txt.isEnabled())

        # she sees that the current value and wants to double it and notices that the spectrum looks a little bit
        # smoother
        previous_number_of_points = len(self.spectrum_model.pattern.x)
        self.enter_value_into_text_field(self.integration_widget.bin_count_txt,
                                         2 * previous_number_of_points)

        self.calibration_model.integrate_1d.assert_called_with(
            num_points=2 * previous_number_of_points,
            mask=None,
            unit='2th_deg')

        # then she decides that having an automatic estimation may probably be better and changes back to automatic.
        # immediately the number is restored and the image looks like when she started
        self.integration_widget.automatic_binning_cb.setChecked(True)
        self.calibration_model.integrate_1d.assert_called_with(num_points=None,
                                                               mask=None,
                                                               unit='2th_deg')

    def test_changing_supersampling_amount_integrating_to_cake_with_mask(self):
        # Edith opens the program, calibrates everything and looks in to the options menu. She sees that there is a
        # miraculous parameter called supersampling. It is currently set to 1 which seems to be normal
        self.assertEqual(self.integration_widget.supersampling_sb.value(), 1)

        # then she sets it to two and she sees that the number of spectrum bin changes and that the spectrum looks
        # smoother

        # values before:
        px1 = self.calibration_model.spectrum_geometry.pixel1
        px2 = self.calibration_model.spectrum_geometry.pixel2

        img_shape = self.img_model.img_data.shape

        self.integration_widget.supersampling_sb.setValue(2)
        self.assertEqual(self.calibration_model.spectrum_geometry.pixel1,
                         0.5 * px1)
        self.assertEqual(self.calibration_model.spectrum_geometry.pixel2,
                         0.5 * px2)
        self.assertEqual(self.calibration_model.cake_geometry.pixel1, px1)
        self.assertEqual(self.calibration_model.cake_geometry.pixel2, px2)

        self.assertEqual(self.img_model.img_data.shape[0], 2 * img_shape[0])
        self.assertEqual(self.img_model.img_data.shape[1], 2 * img_shape[1])

        self.mask_model.load_mask(os.path.join(data_path, 'test.mask'))
        QTest.mouseClick(self.integration_widget.img_mask_btn,
                         QtCore.Qt.LeftButton)
        QTest.mouseClick(self.integration_widget.img_mode_btn,
                         QtCore.Qt.LeftButton)

    def test_saving_image(self):
        # the widget has to be shown to be able to save the image:
        self.integration_widget.show()
        # Tests if the image save procedures are working for the different possible file endings
        self.integration_image_controller.save_img(
            os.path.join(data_path, 'Test_img.png'))
        self.integration_image_controller.save_img(
            os.path.join(data_path, 'Test_img.tiff'))

        self.assertTrue(os.path.exists(os.path.join(data_path,
                                                    'Test_img.png')))
        self.assertTrue(
            os.path.exists(os.path.join(data_path, 'Test_img.tiff')))

        os.remove(os.path.join(data_path, 'Test_img.png'))
        os.remove(os.path.join(data_path, 'Test_img.tiff'))

    def test_saving_spectrum(self):
        # the widget has to be shown to be able to save the image:
        self.integration_widget.show()

        # Tests if the spectrum save procedures is are working for all fileendings
        def save_spectra_test_for_size_and_delete(self):
            self.integration_spectrum_controller.save_pattern(
                os.path.join(data_path, 'Test_spec.xy'))
            self.integration_spectrum_controller.save_pattern(
                os.path.join(data_path, 'Test_spec.chi'))
            self.integration_spectrum_controller.save_pattern(
                os.path.join(data_path, 'Test_spec.dat'))
            self.integration_spectrum_controller.save_pattern(
                os.path.join(data_path, 'Test_spec.png'))
            self.integration_spectrum_controller.save_pattern(
                os.path.join(data_path, 'Test_spec.svg'))

            self.assertTrue(
                os.path.exists(os.path.join(data_path, 'Test_spec.xy')))
            self.assertTrue(
                os.path.exists(os.path.join(data_path, 'Test_spec.chi')))
            self.assertTrue(
                os.path.exists(os.path.join(data_path, 'Test_spec.dat')))
            self.assertTrue(
                os.path.exists(os.path.join(data_path, 'Test_spec.png')))
            self.assertTrue(
                os.path.exists(os.path.join(data_path, 'Test_spec.svg')))

            self.assertGreater(
                os.stat(os.path.join(data_path, 'Test_spec.xy')).st_size, 1)
            self.assertGreater(
                os.stat(os.path.join(data_path, 'Test_spec.chi')).st_size, 1)
            self.assertGreater(
                os.stat(os.path.join(data_path, 'Test_spec.dat')).st_size, 1)
            self.assertGreater(
                os.stat(os.path.join(data_path, 'Test_spec.png')).st_size, 1)
            self.assertGreater(
                os.stat(os.path.join(data_path, 'Test_spec.svg')).st_size, 1)

            os.remove(os.path.join(data_path, 'Test_spec.xy'))
            os.remove(os.path.join(data_path, 'Test_spec.chi'))
            os.remove(os.path.join(data_path, 'Test_spec.dat'))
            os.remove(os.path.join(data_path, 'Test_spec.png'))
            os.remove(os.path.join(data_path, 'Test_spec.svg'))

        save_spectra_test_for_size_and_delete(self)
        QTest.mouseClick(
            self.integration_spectrum_controller.widget.spec_q_btn,
            QtCore.Qt.LeftButton)
        save_spectra_test_for_size_and_delete(self)
        QTest.mouseClick(
            self.integration_spectrum_controller.widget.spec_d_btn,
            QtCore.Qt.LeftButton)
        save_spectra_test_for_size_and_delete(self)

    def test_undocking_and_docking_img_frame(self):
        QTest.mouseClick(self.integration_widget.img_dock_btn,
                         QtCore.Qt.LeftButton)
        QTest.mouseClick(self.integration_widget.img_dock_btn,
                         QtCore.Qt.LeftButton)

    def test_loading_multiple_images_and_batch_integrate_them(self):
        self.integration_widget.spec_autocreate_cb.setChecked(True)
        self.assertTrue(self.integration_widget.spec_autocreate_cb.isChecked())
        self.integration_image_controller.load_file([
            os.path.join(data_path, 'image_001.tif'),
            os.path.join(data_path, 'image_002.tif')
        ])
        self.assertTrue(os.path.exists(os.path.join(data_path,
                                                    'image_001.xy')))
        self.assertTrue(os.path.exists(os.path.join(data_path,
                                                    'image_002.xy')))
        os.remove(os.path.join(data_path, 'image_001.xy'))
        os.remove(os.path.join(data_path, 'image_002.xy'))
class PhaseControllerTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.app = QtGui.QApplication([])

    @classmethod
    def tearDownClass(cls):
        cls.app.quit()
        cls.app.deleteLater()

    def setUp(self):
        self.image_model = ImgModel()
        self.calibration_model = CalibrationModel()
        self.calibration_model.is_calibrated = True
        self.calibration_model.spectrum_geometry.wavelength = 0.31E-10
        self.calibration_model.integrate_1d = MagicMock(return_value=(self.calibration_model.tth,
                                                                      self.calibration_model.int))
        self.spectrum_model = PatternModel()
        self.phase_model = PhaseModel()
        self.widget = IntegrationWidget()
        self.widget.pattern_widget._auto_range = True
        self.phase_tw = self.widget.phase_tw

        self.spectrum_controller = PatternController({}, self.widget, self.image_model, None,
                                                     self.calibration_model, self.spectrum_model)
        self.controller = PhaseController({}, self.widget, self.calibration_model, self.spectrum_model,
                                          self.phase_model)
        self.spectrum_controller.load(os.path.join(data_path, 'spectrum_001.xy'))

    def tearDown(self):
        del self.calibration_model
        del self.spectrum_model
        del self.phase_model
        self.widget.close()
        del self.widget
        del self.controller
        del self.spectrum_controller
        gc.collect()

    def test_manual_deleting_phases(self):
        self.load_phases()
        QtGui.QApplication.processEvents()

        self.assertEqual(self.phase_tw.rowCount(), 6)
        self.assertEqual(len(self.phase_model.phases), 6)
        self.assertEqual(len(self.widget.pattern_widget.phases), 6)
        self.assertEqual(self.phase_tw.currentRow(), 5)

        self.controller.remove_btn_click_callback()
        self.assertEqual(self.phase_tw.rowCount(), 5)
        self.assertEqual(len(self.phase_model.phases), 5)
        self.assertEqual(len(self.widget.pattern_widget.phases), 5)
        self.assertEqual(self.phase_tw.currentRow(), 4)

        self.widget.select_phase(1)
        self.controller.remove_btn_click_callback()
        self.assertEqual(self.phase_tw.rowCount(), 4)
        self.assertEqual(len(self.phase_model.phases), 4)
        self.assertEqual(len(self.widget.pattern_widget.phases), 4)
        self.assertEqual(self.phase_tw.currentRow(), 1)

        self.widget.select_phase(0)
        self.controller.remove_btn_click_callback()
        self.assertEqual(self.phase_tw.rowCount(), 3)
        self.assertEqual(len(self.phase_model.phases), 3)
        self.assertEqual(len(self.widget.pattern_widget.phases), 3)
        self.assertEqual(self.phase_tw.currentRow(), 0)

        self.controller.remove_btn_click_callback()
        self.controller.remove_btn_click_callback()
        self.controller.remove_btn_click_callback()
        self.assertEqual(self.phase_tw.rowCount(), 0)
        self.assertEqual(len(self.phase_model.phases), 0)
        self.assertEqual(len(self.widget.pattern_widget.phases), 0)
        self.assertEqual(self.phase_tw.currentRow(), -1)

        self.controller.remove_btn_click_callback()
        self.assertEqual(self.phase_tw.rowCount(), 0)
        self.assertEqual(len(self.phase_model.phases), 0)
        self.assertEqual(len(self.widget.pattern_widget.phases), 0)
        self.assertEqual(self.phase_tw.currentRow(), -1)

    def test_automatic_deleting_phases(self):
        self.load_phases()
        self.load_phases()
        self.assertEqual(self.phase_tw.rowCount(), 12)
        self.assertEqual(len(self.phase_model.phases), 12)
        self.assertEqual(len(self.widget.pattern_widget.phases), 12)
        self.controller.clear_phases()
        self.assertEqual(self.phase_tw.rowCount(), 0)
        self.assertEqual(len(self.phase_model.phases), 0)
        self.assertEqual(len(self.widget.pattern_widget.phases), 0)
        self.assertEqual(self.phase_tw.currentRow(), -1)

        multiplier = 1
        for dummy_index in range(multiplier):
            self.load_phases()

        self.assertEqual(self.phase_tw.rowCount(), multiplier * 6)
        self.controller.clear_phases()
        self.assertEqual(self.phase_tw.rowCount(), 0)
        self.assertEqual(len(self.phase_model.phases), 0)
        self.assertEqual(len(self.widget.pattern_widget.phases), 0)
        self.assertEqual(self.phase_tw.currentRow(), -1)

    def test_pressure_change(self):
        self.load_phases()
        pressure = 200
        self.widget.phase_pressure_sb.setValue(200)
        for ind, phase in enumerate(self.phase_model.phases):
            self.assertEqual(phase.pressure, pressure)
            self.assertEqual(self.widget.get_phase_pressure(ind), pressure)

    def test_temperature_change(self):
        self.load_phases()
        temperature = 1500
        self.widget.phase_temperature_sb.setValue(temperature)
        for ind, phase in enumerate(self.phase_model.phases):
            if phase.has_thermal_expansion():
                self.assertEqual(phase.temperature, temperature)
                self.assertEqual(self.widget.get_phase_temperature(ind), temperature)
            else:
                self.assertEqual(phase.temperature, 298)
                self.assertEqual(self.widget.get_phase_temperature(ind), None)

    def test_apply_to_all_for_new_added_phase_in_table_widget(self):
        temperature = 1500
        pressure = 200
        self.widget.phase_temperature_sb.setValue(temperature)
        self.widget.phase_pressure_sb.setValue(pressure)
        self.load_phases()
        for ind, phase in enumerate(self.phase_model.phases):
            self.assertEqual(phase.pressure, pressure)
            self.assertEqual(self.widget.get_phase_pressure(ind), pressure)
            if phase.has_thermal_expansion():
                self.assertEqual(phase.temperature, temperature)
                self.assertEqual(self.widget.get_phase_temperature(ind), temperature)
            else:
                self.assertEqual(phase.temperature, 298)
                self.assertEqual(self.widget.get_phase_temperature(ind), None)

    def test_apply_to_all_for_new_added_phase_d_positions(self):
        pressure = 50
        self.load_phase('au_Anderson.jcpds')
        self.widget.phase_pressure_sb.setValue(pressure)
        self.load_phase('au_Anderson.jcpds')

        reflections1 = self.phase_model.get_lines_d(0)
        reflections2 = self.phase_model.get_lines_d(1)
        self.assertTrue(np.array_equal(reflections1, reflections2))

    def test_to_not_show_lines_in_legend(self):
        self.load_phases()
        self.phase_tw.selectRow(1)
        QTest.mouseClick(self.widget.phase_del_btn, QtCore.Qt.LeftButton)
        self.widget.pattern_widget.hide_phase(1)

    def test_auto_scaling_of_lines_in_spectrum_view(self):
        spectrum_view = self.widget.pattern_widget

        spectrum_view_range = spectrum_view.view_box.viewRange()
        spectrum_y = spectrum_view.plot_item.getData()[1]
        expected_maximum_height = np.max(spectrum_y) - spectrum_view_range[1][0]

        self.load_phase('au_Anderson.jcpds')
        phase_plot = spectrum_view.phases[0]
        line_heights = []
        for line in phase_plot.line_items:
            line_data = line.getData()
            height = line_data[1][1] - line_data[1][0]
            line_heights.append(height)

        self.assertAlmostEqual(expected_maximum_height, np.max(line_heights))

        spectrum_view_range = spectrum_view.view_box.viewRange()
        spectrum_y = spectrum_view.plot_item.getData()[1]
        expected_maximum_height = np.max(spectrum_y) - spectrum_view_range[1][0]

        self.assertAlmostEqual(expected_maximum_height, np.max(line_heights))

    def test_line_height_in_spectrum_view_after_zooming(self):
        spectrum_view = self.widget.pattern_widget
        self.load_phase('au_Anderson.jcpds')

        spectrum_view.view_box.setRange(xRange=[17, 30])
        spectrum_view.emit_sig_range_changed()

        phase_plot = spectrum_view.phases[0]
        line_heights = []
        for line in phase_plot.line_items:
            line_data = line.getData()
            if (line_data[0][0] > 17) and (line_data[0][1] < 30):
                height = line_data[1][1] - line_data[1][0]
                line_heights.append(height)

        spectrum_view_range = spectrum_view.view_box.viewRange()
        spectrum_x, spectrum_y = spectrum_view.plot_item.getData()
        spectrum_y_max_in_range = np.max(spectrum_y[(spectrum_x > spectrum_view_range[0][0]) & \
                                                    (spectrum_x < spectrum_view_range[0][1])])
        expected_maximum_height = spectrum_y_max_in_range - spectrum_view_range[1][0]

        self.assertAlmostEqual(expected_maximum_height, np.max(line_heights))

    def load_phases(self):
        self.load_phase('ar.jcpds')
        self.load_phase('ag.jcpds')
        self.load_phase('au_Anderson.jcpds')
        self.load_phase('mo.jcpds')
        self.load_phase('pt.jcpds')
        self.load_phase('re.jcpds')

    def load_phase(self, filename):
        self.controller.add_btn_click_callback(os.path.join(jcpds_path, filename))