예제 #1
0
def get_tab_selected(parent=None):
    '''
    return: 'sample', 'ob', 'normalization' or 'normalized'
    
    '''
    o_gui = GuiHandler(parent=parent)
    return o_gui.get_active_tab()
    def __init__(self, parent=None):
        self.parent = parent

        o_gui = GuiHandler(parent=self.parent)
        element_name = str(
            o_gui.get_text_selected(ui=self.parent.ui.list_of_elements))
        lattice_value = np.float(
            o_gui.get_text(ui=self.parent.ui.lattice_parameter))
        crystal_structure = str(
            o_gui.get_text_selected(ui=self.parent.ui.crystal_structure))

        _element_dictionary = {
            'name': element_name,
            'lattice': lattice_value,
            'crystal_structure': crystal_structure
        }

        _handler = BraggEdge(new_material=[_element_dictionary])
        self.parent.selected_element_bragg_edges_array = _handler.bragg_edges[
            element_name]
        self.parent.selected_element_hkl_array = _handler.hkl[element_name]
        self.parent.selected_element_name = element_name

        # modified the fitting window list of h,k,l if window is alive
        if self.parent.fitting_ui:
            hkl_list = _handler.hkl[element_name]
            str_hkl_list = [
                "{},{},{}".format(_hkl[0], _hkl[1], _hkl[2])
                for _hkl in hkl_list
            ]
            self.parent.fitting_ui.ui.hkl_list_ui.clear()
            self.parent.fitting_ui.ui.hkl_list_ui.addItems(str_hkl_list)
            self.parent.fitting_ui.ui.material_groupBox.setTitle(element_name)
예제 #3
0
    def retrieve_metadata(self):
        o_gui = GuiHandler(parent=self)

        element_name = o_gui.get_text(ui=self.ui.element_name)
        lattice = o_gui.get_text(ui=self.ui.lattice)
        crystal_structure = o_gui.get_text_selected(
            ui=self.ui.crystal_structure)

        self.new_element = {
            'element_name': element_name,
            'lattice': lattice,
            'crystal_structure': crystal_structure
        }
예제 #4
0
    def add_element_to_local_bragg_edge_list(self, material=None):
        '''
        Add a new material into the local bragg edge list
        new entry will be
        'material': {'crystal_structure': '', 'lattice': -1}
        '''
        if material is None:
            return None

        o_gui = GuiHandler(parent = self.parent)
        _crystal_structure = o_gui.get_text_selected(ui = self.parent.ui.crystal_structure)
        _lattice = o_gui.get_text(ui = self.parent.ui.lattice_parameter)
        
        self.parent.local_bragg_edge_list[material] = {'crystal_structure': _crystal_structure,
                                                       'lattice': _lattice}
예제 #5
0
    def sync_instrument_widgets(self, source='load_data'):
        
        target = 'normalized'
        if source == 'normalized':
            target = 'load_data'
        
        list_ui = {'load_data' : {'distance': self.parent.ui.distance_source_detector,
                                  'beam': self.parent.ui.beam_rate,
                                  'detector': self.parent.ui.detector_offset},
                   'normalized': {'distance': self.parent.ui.distance_source_detector_2,
                                  'beam': self.parent.ui.beam_rate_2,
                                  'detector': self.parent.ui.detector_offset_2}}
        
        o_gui = GuiHandler(parent = self.parent)    
        distance_value = o_gui.get_text(ui = list_ui[source]['distance'])
        detector_value = o_gui.get_text(ui = list_ui[source]['detector'])
        beam_index = o_gui.get_index_selected(ui = list_ui[source]['beam'])

        o_gui.set_text(value = distance_value, ui = list_ui[target]['distance'])
        o_gui.set_text(value = detector_value, ui = list_ui[target]['detector'])
        o_gui.set_index_selected(index = beam_index, ui = list_ui[target]['beam'])
예제 #6
0
    def display_images_and_bragg_edge(self,
                                      tof_array=[],
                                      lambda_array=[],
                                      bragg_edges=[]):

        data_type = self.data_type
        plot_ui = self.plot_ui[data_type]
        plot_ui.clear()

        list_files_selected = self.parent.list_file_selected[self.data_type]
        linear_region_left = list_files_selected[0]
        linear_region_right = list_files_selected[-1]

        x_axis = []
        plot_ui.setLabel("left", "Total Counts")

        _symbol = 't'

        if tof_array == []:

            plot_ui.setLabel('bottom', 'File Index')

            for _key in bragg_edges.keys():
                _bragg_edge = bragg_edges[_key]
                if _bragg_edge == []:
                    continue
                curve = plot_ui.plot(_bragg_edge,
                                     symbolPen=None,
                                     pen=pen_color[_key],
                                     symbol=_symbol,
                                     symbolSize=5)
                x_axis = np.arange(len(_bragg_edge))

                curvePoint = pg.CurvePoint(curve)
                plot_ui.addItem(curvePoint)
                _text = pg.TextItem("Group {}".format(_key), anchor=(0.5, 0))
                _text.setParentItem(curvePoint)
                arrow = pg.ArrowItem(angle=0)
                arrow.setParentItem(curvePoint)
                curvePoint.setPos(x_axis[-1])

        else:

            tof_array = tof_array * 1e6

            o_gui = GuiHandler(parent=self.parent)
            xaxis_choice = o_gui.get_xaxis_checked(data_type=self.data_type)

            first_index = True

            for _key in bragg_edges.keys():
                _bragg_edge = bragg_edges[_key]
                if _bragg_edge == []:
                    continue

                if xaxis_choice == 'file_index':
                    curve = plot_ui.plot(_bragg_edge,
                                         pen=pen_color[_key],
                                         symbolPen=None,
                                         symbolSize=5,
                                         symbol=_symbol)
                    x_axis = np.arange(len(_bragg_edge))

                elif xaxis_choice == 'tof':
                    curve = plot_ui.plot(tof_array,
                                         _bragg_edge,
                                         pen=pen_color[_key],
                                         symbolPen=None,
                                         symbolSize=5,
                                         symbol=_symbol)
                    x_axis = tof_array
                    linear_region_left = tof_array[linear_region_left]
                    linear_region_right = tof_array[linear_region_right]

                else:  #lambda

                    if first_index:
                        lambda_array = lambda_array * 1e10

                    curve = plot_ui.plot(
                        lambda_array,
                        _bragg_edge,
                        pen=pen_color[_key],
                        symbolPen=None,
                        symbolSize=5,
                    )
                    x_axis = lambda_array
                    linear_region_left = lambda_array[linear_region_left]
                    linear_region_right = lambda_array[linear_region_right]

                    if first_index:
                        self.display_selected_element_bragg_edges(
                            plot_ui=plot_ui,
                            lambda_range=[lambda_array[0], lambda_array[-1]],
                            ymax=np.max(_bragg_edge))
                        first_index = False

                curvePoint = pg.CurvePoint(curve)
                plot_ui.addItem(curvePoint)
                _text = pg.TextItem("Group {}".format(_key), anchor=(0.5, 0))
                _text.setParentItem(curvePoint)
                arrow = pg.ArrowItem(angle=0)
                arrow.setParentItem(curvePoint)

                if xaxis_choice == 'lambda':
                    last_position = x_axis[-1]
                else:
                    last_position = x_axis[-1]

                curvePoint.setPos(last_position)

        return {
            'x_axis': x_axis,
            'linear_region': [linear_region_left, linear_region_right]
        }
예제 #7
0
    def display_bragg_edge(self, mouse_selection=True):

        _data = self.data
        if _data == []:  #clear data if no data
            self.clear_bragg_edge_plot()

        else:  #retrieve dictionaries of roi_id and roi data (label, x, y, w, h, group)
            list_roi_id = self.parent.list_roi_id[self.data_type]
            list_roi = self.parent.list_roi[self.data_type]

            roi_editor_ui = self.parent.roi_editor_ui[self.data_type]
            if self.data_type == 'sample':
                _image_view = self.parent.ui.image_view
                _image_view_item = self.parent.ui.image_view.imageItem
            elif self.data_type == 'ob':
                _image_view = self.parent.ui.ob_image_view
                _image_view_item = self.parent.ui.ob_image_view.imageItem
            elif self.data_type == 'normalized':
                _image_view = self.parent.ui.normalized_image_view
                _image_view_item = self.parent.ui.normalized_image_view.imageItem

            # used here to group rois into their group for Bragg Edge plot
            list_data_group = {'0': [], '1': [], '2': [], '3': []}

            for _index, roi in enumerate(list_roi_id):

                if mouse_selection:
                    region = roi.getArraySlice(self.parent.live_data,
                                               _image_view_item)

                    label = list_roi[_index][0]
                    x0 = region[0][0].start
                    x1 = region[0][0].stop - 1
                    y0 = region[0][1].start
                    y1 = region[0][1].stop - 1
                    group = list_roi[_index][-1]

                    if x1 == x0:
                        x1 += 1
                    if y1 == y0:
                        y1 += 1

                else:
                    if roi_editor_ui is None:
                        [label, x0, y0, w, h, group] = list_roi[_index]
                        x0 = int(x0)
                        y0 = int(y0)
                        w = int(w)
                        h = int(h)
#                        return
                    else:
                        try:
                            [label, x0, y0, w, h,
                             group] = self.get_row_parameters(
                                 roi_editor_ui.ui, _index)
                        except ValueError:
                            return
                    x1 = x0 + w
                    y1 = y0 + h
                    roi.setPos([x0, y0], update=False, finish=False)
                    roi.setSize([w, h], update=False, finish=False)

                # display ROI boxes
                roi.setPen(pen_color[group])

                _text_array = self.parent.list_label_roi_id[self.data_type]
                if _text_array == []:
                    text_id = pg.TextItem(
                        html=
                        '<div style="text-align: center"><span style="color: #FFF;">'
                        + label + '</span></div>',
                        anchor=(-0.3, 1.3),
                        border='w',
                        fill=(0, 0, 255, 50))
                    _image_view.addItem(text_id)
                    text_id.setPos(x0, y0)
                    self.parent.list_label_roi_id[self.data_type].append(
                        text_id)
                else:
                    text_id = self.parent.list_label_roi_id[
                        self.data_type][_index]
                    text_id.setText(label)
                    text_id.setPos(x0, y0)

                list_data_group[group].append([x0, x1, y0, y1])

                self.save_roi(label, x0, y0, x1, y1, group, self.data_type,
                              _index)

                if mouse_selection:
                    if not (roi_editor_ui is None):
                        roi_editor_ui.ui.tableWidget.blockSignals(True)
                        self.update_roi_editor(_index)
                        roi_editor_ui.ui.tableWidget.blockSignals(False)

            # work over groups
            data = self.parent.data_metadata[self.data_type]['data']
            bragg_edges = self.extract_data(list_data_group, data)

            #check if xaxis can be in lambda, or tof
            #o_time_handler = TimeSpectraHandler(parent = self.parent)
            #o_time_handler.load()
            #tof_array = o_time_handler.tof_array
            if self.data_type == 'normalized':
                tof_array = self.parent.data_metadata['time_spectra'][
                    'normalized_data']
                lambda_array = self.parent.data_metadata['time_spectra'][
                    'normalized_lambda']
            else:
                tof_array = self.parent.data_metadata['time_spectra']['data']
                lambda_array = self.parent.data_metadata['time_spectra'][
                    'lambda']

            # enable the right xaxis buttons
            o_gui = GuiHandler(parent=self.parent)
            if tof_array == []:
                tof_flag = False
            else:
                tof_flag = True
            o_gui.enable_xaxis_button(tof_flag=tof_flag)

            list_files_selected = self.parent.list_file_selected[
                self.data_type]
            linear_region_left = list_files_selected[0]
            linear_region_right = list_files_selected[-1]

            xaxis_choice = o_gui.get_xaxis_checked(data_type=self.data_type)

            # display of bottom bragg edge plot
            dictionary = self.display_images_and_bragg_edge(
                tof_array=tof_array,
                lambda_array=lambda_array,
                bragg_edges=bragg_edges)
            x_axis = dictionary['x_axis']
            [linear_region_left,
             linear_region_right] = dictionary['linear_region']
            self.parent.normalized_lambda_bragg_edge_x_axis = lambda_array * 1e10
            o_gui.xaxis_label()

            lr = pg.LinearRegionItem([linear_region_left, linear_region_right])
            lr.setZValue(-10)

            if self.data_type == 'sample':
                self.parent.ui.bragg_edge_plot.addItem(lr)
            elif self.data_type == 'ob':
                self.parent.ui.ob_bragg_edge_plot.addItem(lr)
            else:
                self.parent.ui.normalized_bragg_edge_plot.addItem(lr)
                self.parent.fitting_bragg_edge_x_axis = x_axis

            lr.sigRegionChangeFinished.connect(
                self.parent.bragg_edge_selection_changed)
            self.parent.list_bragg_edge_selection_id[self.data_type] = lr

            #FIXME (seems to work this way)

            #if tof_flag:
            #self.parent.current_bragg_edge_x_axis[self.data_type] = x_axis

            self.parent.current_bragg_edge_x_axis[self.data_type] = x_axis
예제 #8
0
    def display_counts_vs_file(self, data=[], list_roi=[]):
        if data == []:
            _data = self.normalized
            if _data == []:
                self.clear_counts_vs_file()
                return

            if list_roi == []:
                self.clear_counts_vs_file()
                return

            _array_sample_vs_file_index = self.calculate_mean_counts(
                _data, list_roi=list_roi)

        else:

            _array_sample_vs_file_index = data

        _plot_ui = self.parent.step2_ui['bragg_edge_plot']
        _plot_ui.clear()

        o_gui = GuiHandler(parent=self.parent)
        xaxis_choice = o_gui.get_step2_xaxis_checked()

        if xaxis_choice == 'file_index':
            x_axis = np.arange(len(_array_sample_vs_file_index))
            curve = _plot_ui.plot(_array_sample_vs_file_index)
            _plot_ui.setLabel("bottom", "File Index")

        elif xaxis_choice == 'tof':
            tof_array = self.parent.data_metadata['time_spectra']['data']
            tof_array = tof_array * 1e6
            curve = _plot_ui.plot(tof_array, _array_sample_vs_file_index)
            _plot_ui.setLabel("bottom", u"TOF (\u00B5s)")
            x_axis = tof_array

        else:
            lambda_array = self.parent.data_metadata['time_spectra']['lambda']
            lambda_array = lambda_array * 1e10
            curve = _plot_ui.plot(lambda_array, _array_sample_vs_file_index)
            _plot_ui.setLabel("bottom", u'\u03BB (\u212B)')
            x_axis = lambda_array

        if self.parent.range_files_to_normalized_step2['file_index'] == []:
            _range_files_to_normalized_step2 = [0, x_axis[-1]]
            self.parent.range_files_to_normalized_step2[
                'file_index'] = _range_files_to_normalized_step2
        else:
            _range_files_to_normalized_step2 = self.parent.range_files_to_normalized_step2[
                'file_index']

        # labels
        _plot_ui.setLabel("left", "OB/Sample of ROI")

        # display range of file to keep

        linear_region_range = [
            x_axis[_range_files_to_normalized_step2[0]],
            x_axis[_range_files_to_normalized_step2[1]]
        ]

        lr = pg.LinearRegionItem(values=linear_region_range,
                                 orientation=None,
                                 brush=None,
                                 movable=True,
                                 bounds=None)

        lr.sigRegionChangeFinished.connect(
            self.parent.step2_bragg_edge_selection_changed)
        lr.setZValue(-10)
        self.parent.step2_ui['bragg_edge_plot'].addItem(lr)
        self.parent.bragg_edge_selection = lr
        self.parent.current_bragg_edge_x_axis['normalization'] = x_axis