コード例 #1
0
ファイル: CalibrationController.py プロジェクト: kif/Py2DeX
    def __init__(self,
                 working_dir,
                 view=None,
                 img_data=None,
                 calibration_data=None):
        self.working_dir = working_dir
        if view == None:
            self.view = CalibrationView()
        else:
            self.view = view

        if img_data == None:
            self.data = ImgData()
        else:
            self.data = img_data

        if calibration_data == None:
            self.calibration_data = CalibrationData(self.data)
        else:
            self.calibration_data = calibration_data

        self.data.subscribe(self.plot_image)
        self.view.set_start_values(self.calibration_data.start_values)
        self._first_plot = True
        self.create_signals()
        self.load_calibrants_list()
        self.raise_window()
コード例 #2
0
    def setUp(self):
        self.app = QtGui.QApplication(sys.argv)
        self.img_data = ImgData()
        self.mask_data = MaskData()
        self.calibration_data = CalibrationData(self.img_data)
        self.calibration_data._calibrants_working_dir = 'Data/Calibrants'
        self.calibration_view = CalibrationView()

        self.working_dir = {}

        self.calibration_controller = CalibrationController(working_dir=self.working_dir,
                                                            img_data=self.img_data,
                                                            mask_data=self.mask_data,
                                                            view=self.calibration_view,
                                                            calibration_data=self.calibration_data)
コード例 #3
0
ファイル: MainView.py プロジェクト: kif/Py2DeX
    def __init__(self):
        super(MainView, self).__init__(None)
        self.setupUi(self)

        self.calibration_widget = CalibrationView()
        self.mask_widget = MaskView()
        self.integration_widget = IntegrationView()

        self.calibration_layout = QtGui.QHBoxLayout()
        self.calibration_layout.setContentsMargins(0, 0, 0, 0)
        self.calibration_tab.setLayout(self.calibration_layout)
        self.calibration_layout.addWidget(self.calibration_widget)

        self.mask_layout = QtGui.QHBoxLayout()
        self.mask_layout.setContentsMargins(0, 0, 0, 0)
        self.mask_tab.setLayout(self.mask_layout)
        self.mask_layout.addWidget(self.mask_widget)

        self.integration_layout = QtGui.QHBoxLayout()
        self.integration_layout.setContentsMargins(0, 0, 0, 0)
        self.integration_tab.setLayout(self.integration_layout)
        self.integration_layout.addWidget(self.integration_widget)
コード例 #4
0
ファイル: CalibrationController.py プロジェクト: kif/Py2DeX
    def __init__(self, working_dir, view=None, img_data=None, calibration_data=None):
        self.working_dir = working_dir
        if view == None:
            self.view = CalibrationView()
        else:
            self.view = view

        if img_data == None:
            self.data = ImgData()
        else:
            self.data = img_data

        if calibration_data == None:
            self.calibration_data = CalibrationData(self.data)
        else:
            self.calibration_data = calibration_data

        self.data.subscribe(self.plot_image)
        self.view.set_start_values(self.calibration_data.start_values)
        self._first_plot = True
        self.create_signals()
        self.load_calibrants_list()
        self.raise_window()
コード例 #5
0
ファイル: CalibrationController.py プロジェクト: kif/Py2DeX
class CalibrationController(object):
    def __init__(self, working_dir, view=None, img_data=None, calibration_data=None):
        self.working_dir = working_dir
        if view == None:
            self.view = CalibrationView()
        else:
            self.view = view

        if img_data == None:
            self.data = ImgData()
        else:
            self.data = img_data

        if calibration_data == None:
            self.calibration_data = CalibrationData(self.data)
        else:
            self.calibration_data = calibration_data

        self.data.subscribe(self.plot_image)
        self.view.set_start_values(self.calibration_data.start_values)
        self._first_plot = True
        self.create_signals()
        self.load_calibrants_list()
        self.raise_window()

    def raise_window(self):
        self.view.show()
        self.view.setWindowState(self.view.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
        self.view.activateWindow()
        self.view.raise_()

    def create_signals(self):
        self.create_transformation_signals()
        self.create_txt_box_signals()

        self.view.calibrant_cb.currentIndexChanged.connect(self.load_calibrant)

        self.connect_click_function(self.view.load_file_btn, self.load_file)
        self.connect_click_function(self.view.save_calibration_btn, self.save_calibration)
        self.connect_click_function(self.view.load_calibration_btn, self.load_calibration)

        self.connect_click_function(self.view.integrate_btn, self.calibrate)
        self.connect_click_function(self.view.refine_btn, self.refine)

        self.view.img_view.add_left_click_observer(self.search_peaks)
        self.connect_click_function(self.view.clear_peaks_btn, self.clear_peaks_btn_click)


    def create_transformation_signals(self):
        self.connect_click_function(self.view.rotate_m90_btn, self.data.rotate_img_m90)
        self.connect_click_function(self.view.rotate_m90_btn, self.clear_peaks_btn_click)
        self.connect_click_function(self.view.rotate_p90_btn, self.data.rotate_img_p90)
        self.connect_click_function(self.view.rotate_p90_btn, self.clear_peaks_btn_click)
        self.connect_click_function(self.view.invert_horizontal_btn, self.data.flip_img_horizontally)
        self.connect_click_function(self.view.invert_horizontal_btn, self.clear_peaks_btn_click)
        self.connect_click_function(self.view.invert_vertical_btn, self.data.flip_img_vertically)
        self.connect_click_function(self.view.invert_vertical_btn, self.clear_peaks_btn_click)
        self.connect_click_function(self.view.reset_transformations_btn, self.data.reset_img_transformations)
        self.connect_click_function(self.view.reset_transformations_btn, self.clear_peaks_btn_click)
        self.view.connect(self.view.f2_wavelength_cb, QtCore.SIGNAL('clicked()'), self.wavelength_cb_changed)
        self.view.connect(self.view.pf_wavelength_cb, QtCore.SIGNAL('clicked()'), self.wavelength_cb_changed)


    def create_txt_box_signals(self):
        self.connect_click_function(self.view.f2_update_btn, self.update_f2_btn_click)
        self.connect_click_function(self.view.pf_update_btn, self.update_pyFAI_btn_click)

    def update_f2_btn_click(self):
        fit2d_parameter = self.view.get_fit2d_parameter()
        self.calibration_data.geometry.setFit2D(directDist=fit2d_parameter['directDist'],
                                                centerX=fit2d_parameter['centerX'],
                                                centerY=fit2d_parameter['centerY'],
                                                tilt=fit2d_parameter['tilt'],
                                                tiltPlanRotation=fit2d_parameter['tiltPlanRotation'],
                                                pixelX=fit2d_parameter['pixelX'],
                                                pixelY=fit2d_parameter['pixelY'])
        self.calibration_data.geometry.wavelength = fit2d_parameter['wavelength']
        self.calibration_data.polarization_factor = fit2d_parameter['polarization_factor']
        self.calibration_data.is_calibrated = True
        self.update_all()

    def update_pyFAI_btn_click(self):
        pyFAI_parameter = self.view.get_pyFAI_parameter()
        self.calibration_data.geometry.setPyFAI(dist=pyFAI_parameter['dist'],
                                                poni1=pyFAI_parameter['poni1'],
                                                poni2=pyFAI_parameter['poni2'],
                                                rot1=pyFAI_parameter['rot1'],
                                                rot2=pyFAI_parameter['rot2'],
                                                rot3=pyFAI_parameter['rot3'],
                                                pixel1=pyFAI_parameter['pixel1'],
                                                pixel2=pyFAI_parameter['pixel2'])
        self.calibration_data.geometry.wavelength = pyFAI_parameter['wavelength']
        self.calibration_data.polarization_factor = pyFAI_parameter['polarization_factor']
        self.calibration_data.is_calibrated = True
        self.update_all()


    def load_file(self, filename=None):
        if filename is None:
            filename = str(QtGui.QFileDialog.getOpenFileName(self.view, caption="Load Calibration Image",
                                                             directory=self.working_dir['image']))

        if filename is not '':
            self.working_dir['image'] = os.path.dirname(filename)
            self.data.load(filename)

    def load_calibrants_list(self):
        self._calibrants_file_list = []
        self._calibrants_file_names_list = []
        for file in os.listdir(self.calibration_data._calibrants_working_dir):
            if file.endswith('.D'):
                self._calibrants_file_list.append(file)
                self._calibrants_file_names_list.append(file.split('.')[:-1][0])
        self.view.calibrant_cb.blockSignals(True)
        self.view.calibrant_cb.clear()
        self.view.calibrant_cb.addItems(self._calibrants_file_names_list)
        self.view.calibrant_cb.blockSignals(False)
        self.view.calibrant_cb.setCurrentIndex(7)  # to LaB6
        self.load_calibrant()

    def load_calibrant(self, wavelength_from='start_values'):
        current_index = self.view.calibrant_cb.currentIndex()
        filename = os.path.join(self.calibration_data._calibrants_working_dir,
                                self._calibrants_file_list[current_index])
        self.calibration_data.set_calibrant(filename)

        wavelength = 0
        if wavelength_from == 'start_values':
            start_values = self.view.get_start_values()
            wavelength = start_values['wavelength']
        elif wavelength_from == 'pyFAI':
            pyFAI_parameter, _ = self.calibration_data.get_calibration_parameter()
            if pyFAI_parameter['wavelength'] is not 0:
                wavelength = pyFAI_parameter['wavelength']
            else:
                start_values = self.view.get_start_values()
                wavelength = start_values['wavelength']
        else:
            start_values = self.view.get_start_values()
            wavelength = start_values['wavelength']

        self.calibration_data.calibrant.setWavelength_change2th(wavelength)
        self.view.spectrum_view.plot_vertical_lines(np.array(self.calibration_data.calibrant.get_2th()) / np.pi * 180,
                                                    name=self._calibrants_file_names_list[current_index])

    def set_calibrant(self, index):
        self.view.calibrant_cb.setCurrentIndex(index)
        self.load_calibrant()


    def plot_image(self):
        if self._first_plot:
            self.view.img_view.plot_image(self.data.get_img_data(), True)
            self.view.img_view.auto_range()
            self._first_plot = False
        else:
            self.view.img_view.plot_image(self.data.get_img_data(), False)
        self.view.set_img_filename(self.data.filename)


    def connect_click_function(self, emitter, function):
        self.view.connect(emitter, QtCore.SIGNAL('clicked()'), function)

    def search_peaks(self, x, y):
        peak_ind = self.view.peak_num_sb.value()
        if self.view.automatic_peak_search_rb.isChecked():
            points = self.calibration_data.find_peaks_automatic(x, y, peak_ind - 1)
        else:
            search_size = np.int(self.view.search_size_sb.value())
            points = self.calibration_data.find_peak(x, y, search_size, peak_ind - 1)
        if len(points):
            self.plot_points(points)
            if self.view.automatic_peak_num_inc_cb.checkState():
                self.view.peak_num_sb.setValue(peak_ind + 1)

    def plot_points(self, points=None):
        if points == None:
            try:
                points = self.calibration_data.get_point_array()
            except IndexError:
                points = []
        if len(points):
            self.view.img_view.add_scatter_data(points[:, 0] + 0.5, points[:, 1] + 0.5)

    def clear_peaks_btn_click(self):
        self.calibration_data.clear_peaks()
        self.view.img_view.clear_scatter_plot()
        self.view.peak_num_sb.setValue(1)

    def wavelength_cb_changed(self):
        self.calibration_data.fit_wavelength = self.view.f2_wavelength_cb.isChecked()

    def calibrate(self):
        self.load_calibrant()  #load the right calibration file...
        self.calibration_data.set_start_values(self.view.get_start_values())
        self.calibration_data.calibrate()
        self.update_calibration_parameter()

        if self.view.options_automatic_refinement_cb.isChecked():
            self.refine()
        self.update_all()

    def refine(self):
        self.clear_peaks_btn_click()
        self.load_calibrant(wavelength_from='pyFAI')  #load right calibration file

        # get options
        algorithm = str(self.view.options_peaksearch_algorithm_cb.currentText())
        delta_tth = np.float(self.view.options_delta_tth_txt.text())
        intensity_min_factor = np.float(self.view.options_intensity_mean_factor_sb.value())
        intensity_max = np.float(self.view.options_intensity_limit_txt.text())
        num_rings = self.view.options_num_rings_sb.value()

        self.calibration_data.search_peaks_on_ring(0, delta_tth, algorithm, intensity_min_factor, intensity_max)
        self.calibration_data.search_peaks_on_ring(1, delta_tth, algorithm, intensity_min_factor, intensity_max)
        try:
            self.calibration_data.refine()
        except IndexError:
            print 'Did not find any Points with the specified parameters for the first two rings!'
        self.plot_points()

        for i in xrange(num_rings - 2):
            points = self.calibration_data.search_peaks_on_ring(i + 2, delta_tth, algorithm, intensity_min_factor,
                                                                intensity_max)
            self.plot_points(points)
            QtGui.QApplication.processEvents()
            QtGui.QApplication.processEvents()
            try:
                self.calibration_data.refine()
            except IndexError:
                print 'Did not find enough points with the specified parameters!'
        self.calibration_data.integrate()
        self.update_all()

    def load_calibration(self, filename=None):
        if filename is None:
            filename = str(QtGui.QFileDialog.getOpenFileName(self.view, caption="Load calibration...",
                                                             directory=self.working_dir['calibration'],
                                                             filter='*.poni'))
        if filename is not '':
            self.working_dir['calibration'] = os.path.dirname(filename)
            self.calibration_data.load(filename)
            self.update_all()


    def update_all(self):
        if not self._first_plot:
            self.calibration_data.integrate_1d()
            self.calibration_data.integrate_2d()
            self.view.cake_view.plot_image(self.calibration_data.cake_img, True)

            self.view.spectrum_view.plot_data(self.calibration_data.tth, self.calibration_data.int)
            self.view.spectrum_view.plot_vertical_lines(np.array(self.calibration_data.calibrant.get_2th()) /
                                                        np.pi * 180)
            self.view.spectrum_view.view_box.autoRange()
            if self.view.tab_widget.currentIndex() == 0:
                self.view.tab_widget.setCurrentIndex(1)

        if self.view.ToolBox.currentIndex() is not 2 or \
                        self.view.ToolBox.currentIndex() is not 3:
            self.view.ToolBox.setCurrentIndex(2)
        self.update_calibration_parameter()

    def update_calibration_parameter(self):
        pyFAI_parameter, fit2d_parameter = self.calibration_data.get_calibration_parameter()
        self.view.set_calibration_parameters(pyFAI_parameter, fit2d_parameter)

    def save_calibration(self, filename=None):
        if filename is None:
            filename = str(QtGui.QFileDialog.getSaveFileName(self.view, "Save calibration...",
                                                             self.working_dir['calibration'], '*.poni'))
        if filename is not '':
            self.working_dir['calibration'] = os.path.dirname(filename)
            self.calibration_data.geometry.save(filename)
コード例 #6
0
class CalibrationControllerTest(unittest.TestCase):
    def setUp(self):
        self.app = QtGui.QApplication(sys.argv)
        self.img_data = ImgData()
        self.mask_data = MaskData()
        self.calibration_data = CalibrationData(self.img_data)
        self.calibration_data._calibrants_working_dir = 'Data/Calibrants'
        self.calibration_view = CalibrationView()

        self.working_dir = {}

        self.calibration_controller = CalibrationController(working_dir=self.working_dir,
                                                            img_data=self.img_data,
                                                            mask_data=self.mask_data,
                                                            view=self.calibration_view,
                                                            calibration_data=self.calibration_data)

    def tearDown(self):
        del self.app

    def test_automatic_calibration1(self):
        self.calibration_controller.load_img('Data/LaB6_p49_40keV_006.tif')
        self.calibration_controller.search_peaks(1179.6, 1129.4)
        self.calibration_controller.search_peaks(1268.5, 1119.8)
        self.calibration_controller.view.sv_wavelength_txt.setText('0.31')
        self.calibration_controller.view.sv_distance_txt.setText('200')
        self.calibration_controller.view.sv_pixel_width_txt.setText('79')
        self.calibration_controller.view.sv_pixel_height_txt.setText('79')
        self.calibration_controller.view.calibrant_cb.setCurrentIndex(7)
        self.calibration_controller.calibrate()
        self.calibration_controller.view.cake_view.set_vertical_line_pos(1419.8, 653.4)

    def test_automatic_calibration2(self):
        self.calibration_controller.load_img('Data/LaB6_WOS_30keV_005.tif')
        self.calibration_controller.search_peaks(1245.2, 1919.3)
        self.calibration_controller.search_peaks(1334.0, 1823.7)
        self.calibration_controller.view.sv_wavelength_txt.setText('0.3344')
        self.calibration_controller.view.sv_distance_txt.setText('500')
        self.calibration_controller.view.sv_pixel_width_txt.setText('200')
        self.calibration_controller.view.sv_pixel_height_txt.setText('200')
        self.calibration_controller.view.calibrant_cb.setCurrentIndex(7)
        self.calibration_controller.calibrate()
        self.calibration_controller.view.cake_view.set_vertical_line_pos(206.5, 171.6)

    def test_automatic_calibration3(self):
        self.calibration_controller.load_img('Data/CeO2_Oct24_2014_001_0000.tif')
        QTest.mouseClick(self.calibration_view.automatic_peak_num_inc_cb, QtCore.Qt.LeftButton)

        self.assertFalse(self.calibration_view.automatic_peak_num_inc_cb.isChecked())
        self.calibration_controller.search_peaks(517.664434674, 647.529865592)
        self.calibration_controller.search_peaks(667.380513299, 525.252854758)
        self.calibration_controller.search_peaks(671.110095329, 473.571503774)
        self.calibration_controller.search_peaks(592.788872703, 350.495296791)
        self.calibration_controller.search_peaks(387.395462348, 390.987901686)
        self.calibration_controller.search_peaks(367.94835605, 554.290314848)

        self.calibration_controller.view.sv_wavelength_txt.setText('0.406626')
        self.calibration_controller.view.sv_distance_txt.setText('200')
        self.calibration_controller.view.sv_pixel_width_txt.setText('172')
        self.calibration_controller.view.sv_pixel_height_txt.setText('172')
        self.calibration_controller.view.calibrant_cb.setCurrentIndex(4)

        start_values = self.calibration_view.get_start_values()
        self.assertAlmostEqual(start_values['wavelength'], 0.406626e-10)
        self.assertAlmostEqual(start_values['pixel_height'], 172e-6)
        self.assertAlmostEqual(start_values['pixel_width'], 172e-6)
        self.calibration_controller.load_calibrant()
        self.assertAlmostEqual(self.calibration_data.calibrant.wavelength, 0.406626e-10)

        QTest.mouseClick(self.calibration_view.integrate_btn, QtCore.Qt.LeftButton)
        calibration_parameter = self.calibration_data.get_calibration_parameter()[0]

        self.assertAlmostEqual(calibration_parameter['dist'], .2086, places=4)

    def test_automatic_calibration_with_supersampling(self):
        self.calibration_controller.load_img('Data/LaB6_p49_40keV_006.tif')
        self.calibration_controller.search_peaks(1179.6, 1129.4)
        self.calibration_controller.search_peaks(1268.5, 1119.8)
        self.calibration_controller.view.sv_wavelength_txt.setText('0.31')
        self.calibration_controller.view.sv_distance_txt.setText('200')
        self.calibration_controller.view.sv_pixel_width_txt.setText('79')
        self.calibration_controller.view.sv_pixel_height_txt.setText('79')
        self.calibration_controller.view.calibrant_cb.setCurrentIndex(7)
        self.img_data.set_supersampling(2)
        self.calibration_data.set_supersampling(2)
        self.calibration_controller.calibrate()

    def test_automatic_calibration_with_supersampling_and_mask(self):
        self.calibration_controller.load_img('Data/LaB6_p49_40keV_006.tif')
        self.calibration_controller.search_peaks(1179.6, 1129.4)
        self.calibration_controller.search_peaks(1268.5, 1119.8)
        self.calibration_controller.view.sv_wavelength_txt.setText('0.31')
        self.calibration_controller.view.sv_distance_txt.setText('200')
        self.calibration_controller.view.sv_pixel_width_txt.setText('79')
        self.calibration_controller.view.sv_pixel_height_txt.setText('79')
        self.calibration_controller.view.calibrant_cb.setCurrentIndex(7)
        self.img_data.set_supersampling(2)
        self.mask_data.mask_below_threshold(self.img_data._img_data, 1)
        self.mask_data.set_supersampling(2)
        self.calibration_data.set_supersampling(2)
        self.calibration_controller.view.use_mask_cb.setChecked(True)
        self.calibration_controller.calibrate()

    def test_calibrating_one_image_size_and_loading_different_image_size(self):
        self.calibration_controller.load_img('Data/LaB6_p49_40keV_006.tif')
        self.calibration_controller.search_peaks(1179.6, 1129.4)
        self.calibration_controller.search_peaks(1268.5, 1119.8)
        self.calibration_controller.view.sv_wavelength_txt.setText('0.31')
        self.calibration_controller.view.sv_distance_txt.setText('200')
        self.calibration_controller.view.sv_pixel_width_txt.setText('79')
        self.calibration_controller.view.sv_pixel_height_txt.setText('79')
        self.calibration_controller.view.calibrant_cb.setCurrentIndex(7)
        self.calibration_controller.view.options_automatic_refinement_cb.setChecked(False)
        self.calibration_controller.view.use_mask_cb.setChecked(True)
        self.calibration_controller.calibrate()
        self.calibration_data.integrate_1d()
        self.calibration_data.integrate_2d()
        self.calibration_controller.load_img('Data/CeO2_Pilatus1M.tif')
        self.calibration_data.integrate_1d()
        self.calibration_data.integrate_2d()

    def test_loading_and_saving_of_calibration_files(self):
        self.calibration_controller.load_calibration('Data/calibration.poni')
        self.calibration_controller.save_calibration('Data/calibration.poni')
コード例 #7
0
ファイル: CalibrationController.py プロジェクト: kif/Py2DeX
class CalibrationController(object):
    def __init__(self,
                 working_dir,
                 view=None,
                 img_data=None,
                 calibration_data=None):
        self.working_dir = working_dir
        if view == None:
            self.view = CalibrationView()
        else:
            self.view = view

        if img_data == None:
            self.data = ImgData()
        else:
            self.data = img_data

        if calibration_data == None:
            self.calibration_data = CalibrationData(self.data)
        else:
            self.calibration_data = calibration_data

        self.data.subscribe(self.plot_image)
        self.view.set_start_values(self.calibration_data.start_values)
        self._first_plot = True
        self.create_signals()
        self.load_calibrants_list()
        self.raise_window()

    def raise_window(self):
        self.view.show()
        self.view.setWindowState(self.view.windowState()
                                 & ~QtCore.Qt.WindowMinimized
                                 | QtCore.Qt.WindowActive)
        self.view.activateWindow()
        self.view.raise_()

    def create_signals(self):
        self.create_transformation_signals()
        self.create_txt_box_signals()

        self.view.calibrant_cb.currentIndexChanged.connect(self.load_calibrant)

        self.connect_click_function(self.view.load_file_btn, self.load_file)
        self.connect_click_function(self.view.save_calibration_btn,
                                    self.save_calibration)
        self.connect_click_function(self.view.load_calibration_btn,
                                    self.load_calibration)

        self.connect_click_function(self.view.integrate_btn, self.calibrate)
        self.connect_click_function(self.view.refine_btn, self.refine)

        self.view.img_view.add_left_click_observer(self.search_peaks)
        self.connect_click_function(self.view.clear_peaks_btn,
                                    self.clear_peaks_btn_click)

    def create_transformation_signals(self):
        self.connect_click_function(self.view.rotate_m90_btn,
                                    self.data.rotate_img_m90)
        self.connect_click_function(self.view.rotate_m90_btn,
                                    self.clear_peaks_btn_click)
        self.connect_click_function(self.view.rotate_p90_btn,
                                    self.data.rotate_img_p90)
        self.connect_click_function(self.view.rotate_p90_btn,
                                    self.clear_peaks_btn_click)
        self.connect_click_function(self.view.invert_horizontal_btn,
                                    self.data.flip_img_horizontally)
        self.connect_click_function(self.view.invert_horizontal_btn,
                                    self.clear_peaks_btn_click)
        self.connect_click_function(self.view.invert_vertical_btn,
                                    self.data.flip_img_vertically)
        self.connect_click_function(self.view.invert_vertical_btn,
                                    self.clear_peaks_btn_click)
        self.connect_click_function(self.view.reset_transformations_btn,
                                    self.data.reset_img_transformations)
        self.connect_click_function(self.view.reset_transformations_btn,
                                    self.clear_peaks_btn_click)
        self.view.connect(self.view.f2_wavelength_cb,
                          QtCore.SIGNAL('clicked()'),
                          self.wavelength_cb_changed)
        self.view.connect(self.view.pf_wavelength_cb,
                          QtCore.SIGNAL('clicked()'),
                          self.wavelength_cb_changed)

    def create_txt_box_signals(self):
        self.connect_click_function(self.view.f2_update_btn,
                                    self.update_f2_btn_click)
        self.connect_click_function(self.view.pf_update_btn,
                                    self.update_pyFAI_btn_click)

    def update_f2_btn_click(self):
        fit2d_parameter = self.view.get_fit2d_parameter()
        self.calibration_data.geometry.setFit2D(
            directDist=fit2d_parameter['directDist'],
            centerX=fit2d_parameter['centerX'],
            centerY=fit2d_parameter['centerY'],
            tilt=fit2d_parameter['tilt'],
            tiltPlanRotation=fit2d_parameter['tiltPlanRotation'],
            pixelX=fit2d_parameter['pixelX'],
            pixelY=fit2d_parameter['pixelY'])
        self.calibration_data.geometry.wavelength = fit2d_parameter[
            'wavelength']
        self.calibration_data.polarization_factor = fit2d_parameter[
            'polarization_factor']
        self.calibration_data.is_calibrated = True
        self.update_all()

    def update_pyFAI_btn_click(self):
        pyFAI_parameter = self.view.get_pyFAI_parameter()
        self.calibration_data.geometry.setPyFAI(
            dist=pyFAI_parameter['dist'],
            poni1=pyFAI_parameter['poni1'],
            poni2=pyFAI_parameter['poni2'],
            rot1=pyFAI_parameter['rot1'],
            rot2=pyFAI_parameter['rot2'],
            rot3=pyFAI_parameter['rot3'],
            pixel1=pyFAI_parameter['pixel1'],
            pixel2=pyFAI_parameter['pixel2'])
        self.calibration_data.geometry.wavelength = pyFAI_parameter[
            'wavelength']
        self.calibration_data.polarization_factor = pyFAI_parameter[
            'polarization_factor']
        self.calibration_data.is_calibrated = True
        self.update_all()

    def load_file(self, filename=None):
        if filename is None:
            filename = str(
                QtGui.QFileDialog.getOpenFileName(
                    self.view,
                    caption="Load Calibration Image",
                    directory=self.working_dir['image']))

        if filename is not '':
            self.working_dir['image'] = os.path.dirname(filename)
            self.data.load(filename)

    def load_calibrants_list(self):
        self._calibrants_file_list = []
        self._calibrants_file_names_list = []
        for file in os.listdir(self.calibration_data._calibrants_working_dir):
            if file.endswith('.D'):
                self._calibrants_file_list.append(file)
                self._calibrants_file_names_list.append(
                    file.split('.')[:-1][0])
        self.view.calibrant_cb.blockSignals(True)
        self.view.calibrant_cb.clear()
        self.view.calibrant_cb.addItems(self._calibrants_file_names_list)
        self.view.calibrant_cb.blockSignals(False)
        self.view.calibrant_cb.setCurrentIndex(7)  # to LaB6
        self.load_calibrant()

    def load_calibrant(self, wavelength_from='start_values'):
        current_index = self.view.calibrant_cb.currentIndex()
        filename = os.path.join(self.calibration_data._calibrants_working_dir,
                                self._calibrants_file_list[current_index])
        self.calibration_data.set_calibrant(filename)

        wavelength = 0
        if wavelength_from == 'start_values':
            start_values = self.view.get_start_values()
            wavelength = start_values['wavelength']
        elif wavelength_from == 'pyFAI':
            pyFAI_parameter, _ = self.calibration_data.get_calibration_parameter(
            )
            if pyFAI_parameter['wavelength'] is not 0:
                wavelength = pyFAI_parameter['wavelength']
            else:
                start_values = self.view.get_start_values()
                wavelength = start_values['wavelength']
        else:
            start_values = self.view.get_start_values()
            wavelength = start_values['wavelength']

        self.calibration_data.calibrant.setWavelength_change2th(wavelength)
        self.view.spectrum_view.plot_vertical_lines(
            np.array(self.calibration_data.calibrant.get_2th()) / np.pi * 180,
            name=self._calibrants_file_names_list[current_index])

    def set_calibrant(self, index):
        self.view.calibrant_cb.setCurrentIndex(index)
        self.load_calibrant()

    def plot_image(self):
        if self._first_plot:
            self.view.img_view.plot_image(self.data.get_img_data(), True)
            self.view.img_view.auto_range()
            self._first_plot = False
        else:
            self.view.img_view.plot_image(self.data.get_img_data(), False)
        self.view.set_img_filename(self.data.filename)

    def connect_click_function(self, emitter, function):
        self.view.connect(emitter, QtCore.SIGNAL('clicked()'), function)

    def search_peaks(self, x, y):
        peak_ind = self.view.peak_num_sb.value()
        if self.view.automatic_peak_search_rb.isChecked():
            points = self.calibration_data.find_peaks_automatic(
                x, y, peak_ind - 1)
        else:
            search_size = np.int(self.view.search_size_sb.value())
            points = self.calibration_data.find_peak(x, y, search_size,
                                                     peak_ind - 1)
        if len(points):
            self.plot_points(points)
            if self.view.automatic_peak_num_inc_cb.checkState():
                self.view.peak_num_sb.setValue(peak_ind + 1)

    def plot_points(self, points=None):
        if points == None:
            try:
                points = self.calibration_data.get_point_array()
            except IndexError:
                points = []
        if len(points):
            self.view.img_view.add_scatter_data(points[:, 0] + 0.5,
                                                points[:, 1] + 0.5)

    def clear_peaks_btn_click(self):
        self.calibration_data.clear_peaks()
        self.view.img_view.clear_scatter_plot()
        self.view.peak_num_sb.setValue(1)

    def wavelength_cb_changed(self):
        self.calibration_data.fit_wavelength = self.view.f2_wavelength_cb.isChecked(
        )

    def calibrate(self):
        self.load_calibrant()  #load the right calibration file...
        self.calibration_data.set_start_values(self.view.get_start_values())
        self.calibration_data.calibrate()
        self.update_calibration_parameter()

        if self.view.options_automatic_refinement_cb.isChecked():
            self.refine()
        self.update_all()

    def refine(self):
        self.clear_peaks_btn_click()
        self.load_calibrant(
            wavelength_from='pyFAI')  #load right calibration file

        # get options
        algorithm = str(
            self.view.options_peaksearch_algorithm_cb.currentText())
        delta_tth = np.float(self.view.options_delta_tth_txt.text())
        intensity_min_factor = np.float(
            self.view.options_intensity_mean_factor_sb.value())
        intensity_max = np.float(self.view.options_intensity_limit_txt.text())
        num_rings = self.view.options_num_rings_sb.value()

        self.calibration_data.search_peaks_on_ring(0, delta_tth, algorithm,
                                                   intensity_min_factor,
                                                   intensity_max)
        self.calibration_data.search_peaks_on_ring(1, delta_tth, algorithm,
                                                   intensity_min_factor,
                                                   intensity_max)
        try:
            self.calibration_data.refine()
        except IndexError:
            print 'Did not find any Points with the specified parameters for the first two rings!'
        self.plot_points()

        for i in xrange(num_rings - 2):
            points = self.calibration_data.search_peaks_on_ring(
                i + 2, delta_tth, algorithm, intensity_min_factor,
                intensity_max)
            self.plot_points(points)
            QtGui.QApplication.processEvents()
            QtGui.QApplication.processEvents()
            try:
                self.calibration_data.refine()
            except IndexError:
                print 'Did not find enough points with the specified parameters!'
        self.calibration_data.integrate()
        self.update_all()

    def load_calibration(self, filename=None):
        if filename is None:
            filename = str(
                QtGui.QFileDialog.getOpenFileName(
                    self.view,
                    caption="Load calibration...",
                    directory=self.working_dir['calibration'],
                    filter='*.poni'))
        if filename is not '':
            self.working_dir['calibration'] = os.path.dirname(filename)
            self.calibration_data.load(filename)
            self.update_all()

    def update_all(self):
        if not self._first_plot:
            self.calibration_data.integrate_1d()
            self.calibration_data.integrate_2d()
            self.view.cake_view.plot_image(self.calibration_data.cake_img,
                                           True)

            self.view.spectrum_view.plot_data(self.calibration_data.tth,
                                              self.calibration_data.int)
            self.view.spectrum_view.plot_vertical_lines(
                np.array(self.calibration_data.calibrant.get_2th()) / np.pi *
                180)
            self.view.spectrum_view.view_box.autoRange()
            if self.view.tab_widget.currentIndex() == 0:
                self.view.tab_widget.setCurrentIndex(1)

        if self.view.ToolBox.currentIndex() is not 2 or \
                        self.view.ToolBox.currentIndex() is not 3:
            self.view.ToolBox.setCurrentIndex(2)
        self.update_calibration_parameter()

    def update_calibration_parameter(self):
        pyFAI_parameter, fit2d_parameter = self.calibration_data.get_calibration_parameter(
        )
        self.view.set_calibration_parameters(pyFAI_parameter, fit2d_parameter)

    def save_calibration(self, filename=None):
        if filename is None:
            filename = str(
                QtGui.QFileDialog.getSaveFileName(
                    self.view, "Save calibration...",
                    self.working_dir['calibration'], '*.poni'))
        if filename is not '':
            self.working_dir['calibration'] = os.path.dirname(filename)
            self.calibration_data.geometry.save(filename)