<commit_before>from qtpy.QtWidgets import QTableWidgetItem, QCheckBox, QComboBox
import pyqtgraph as pg
import numpy as np

from ..step2 import gui_handler
from .. import RegionType
from .get import Get as Step2Get
from ..step1.roi import DEFAULT_ROI


class Step2RoiHandler(object):

    def __init__(self, parent=None):
        self.parent = parent

    def save_table(self):
        list_roi = self.parent.list_roi['normalization']

        o_get = Step2Get(parent=self.parent)
        for _row, roi in enumerate(list_roi):
            try:
                _row_infos = o_get.roi_table_row(_row)
            except ValueError:
                return

            list_roi[_row] = _row_infos

        self.parent.list_roi['normalization'] = list_roi

    def enable_selected_roi(self):
        list_roi = self.parent.list_roi['normalization']
        list_roi_id = self.parent.list_roi_id['normalization']
        list_label_roi_id = self.parent.list_label_roi_id['normalization']

        for index, roi in enumerate(list_roi):

            _roi_id = list_roi_id[index]
            _label_roi_id = list_label_roi_id[index]
            is_roi_visible = roi[0]
            _roi_id.setVisible(is_roi_visible)
            _label_roi_id.setVisible(is_roi_visible)

    # def get_row(self, row=-1):
    #     if row == -1:
    #         return []
    #
    #     # use flag
    #     _flag_widget = self.parent.ui.normalization_tableWidget.cellWidget(row, 0)
    #     if _flag_widget is None:
    #         raise ValueError
    #     flag = _flag_widget.isChecked()
    #
    #     # x0
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 1)
    #     if _item is None:
    #         raise ValueError
    #     x0 = str(_item.text())
    #
    #     # y0
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 2)
    #     if _item is None:
    #         raise ValueError
    #     y0 = str(_item.text())
    #
    #     # width
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 3)
    #     if _item is None:
    #         raise ValueError
    #     width = str(_item.text())
    #
    #     # height
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 4)
    #     if _item is None:
    #         raise ValueError
    #     height = str(_item.text())
    #
    #     # region type
    #     _text_widget = self.parent.ui.normalization_tableWidget.cellWidget(row, 5)
    #     region_type = _text_widget.currentText()
    #
    #     return [flag, x0, y0, width, height, region_type]

    def save_roi(self):
        list_roi_id = self.parent.list_roi_id['normalization']
        list_roi = self.parent.list_roi['normalization']

        sample = self.parent.data_metadata['normalization']['data']
        image_item = self.parent.step2_ui['image_view'].imageItem

        for _index, _roi_id in enumerate(list_roi_id):

            region = _roi_id.getArraySlice(sample, image_item)
            x0 = region[0][0].start
            x1 = region[0][0].stop - 1
            y0 = region[0][1].start
            y1 = region[0][1].stop - 1

            width = x1 - x0
            height = y1 - y0

            _roi = list_roi[_index]
            _roi[1] = x0
            _roi[2] = y0
            _roi[3] = width
            _roi[4] = height

            list_roi[_index] = _roi

        self.parent.list_roi['normalization'] = list_roi

    def remove_roi(self):
        selection = self.parent.ui.normalization_tableWidget.selectedRanges()
        if selection == []:
            return

        selection = selection[0]
        _row_selected = selection.bottomRow()

        self.parent.ui.normalization_tableWidget.removeRow(_row_selected)

        list_roi = self.parent.list_roi['normalization']
        list_roi_id = self.parent.list_roi_id['normalization']
        list_label_roi_id = self.parent.list_label_roi_id['normalization']

        new_list_roi = []
        new_list_roi_id = []
        new_list_label_roi_id = []
        for _index, _roi in enumerate(list_roi):
            if _index == _row_selected:
                self.parent.step2_ui['image_view'].removeItem(list_roi_id[_index])
                continue
            new_list_roi.append(_roi)
            new_list_roi_id.append(list_roi_id[_index])
            new_list_label_roi_id.append(list_label_roi_id[_index])

        self.parent.list_roi['normalization'] = new_list_roi
        self.parent.list_roi_id['normalization'] = new_list_roi_id
        self.parent.list_label_roi_id['normalization'] = new_list_label_roi_id

        o_gui = gui_handler.Step2GuiHandler(parent=self.parent)
        o_gui.check_add_remove_roi_buttons()

    def add_roi_in_image(self):
        x0 = int(DEFAULT_ROI[1])
        y0 = int(DEFAULT_ROI[2])<commit_msg>roi label is now removed when row is removed. this ref #38<commit_after>from qtpy.QtWidgets import QTableWidgetItem, QCheckBox, QComboBox
import pyqtgraph as pg
import numpy as np

from ..step2 import gui_handler
from .. import RegionType
from .get import Get as Step2Get
from ..step1.roi import DEFAULT_ROI


class Step2RoiHandler(object):

    def __init__(self, parent=None):
        self.parent = parent

    def save_table(self):
        list_roi = self.parent.list_roi['normalization']

        o_get = Step2Get(parent=self.parent)
        for _row, roi in enumerate(list_roi):
            try:
                _row_infos = o_get.roi_table_row(_row)
            except ValueError:
                return

            list_roi[_row] = _row_infos

        self.parent.list_roi['normalization'] = list_roi

    def enable_selected_roi(self):
        list_roi = self.parent.list_roi['normalization']
        list_roi_id = self.parent.list_roi_id['normalization']
        list_label_roi_id = self.parent.list_label_roi_id['normalization']

        for index, roi in enumerate(list_roi):

            _roi_id = list_roi_id[index]
            _label_roi_id = list_label_roi_id[index]
            is_roi_visible = roi[0]
            _roi_id.setVisible(is_roi_visible)
            _label_roi_id.setVisible(is_roi_visible)

    # def get_row(self, row=-1):
    #     if row == -1:
    #         return []
    #
    #     # use flag
    #     _flag_widget = self.parent.ui.normalization_tableWidget.cellWidget(row, 0)
    #     if _flag_widget is None:
    #         raise ValueError
    #     flag = _flag_widget.isChecked()
    #
    #     # x0
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 1)
    #     if _item is None:
    #         raise ValueError
    #     x0 = str(_item.text())
    #
    #     # y0
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 2)
    #     if _item is None:
    #         raise ValueError
    #     y0 = str(_item.text())
    #
    #     # width
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 3)
    #     if _item is None:
    #         raise ValueError
    #     width = str(_item.text())
    #
    #     # height
    #     _item = self.parent.ui.normalization_tableWidget.item(row, 4)
    #     if _item is None:
    #         raise ValueError
    #     height = str(_item.text())
    #
    #     # region type
    #     _text_widget = self.parent.ui.normalization_tableWidget.cellWidget(row, 5)
    #     region_type = _text_widget.currentText()
    #
    #     return [flag, x0, y0, width, height, region_type]

    def save_roi(self):
        list_roi_id = self.parent.list_roi_id['normalization']
        list_roi = self.parent.list_roi['normalization']

        sample = self.parent.data_metadata['normalization']['data']
        image_item = self.parent.step2_ui['image_view'].imageItem

        for _index, _roi_id in enumerate(list_roi_id):

            region = _roi_id.getArraySlice(sample, image_item)
            x0 = region[0][0].start
            x1 = region[0][0].stop - 1
            y0 = region[0][1].start
            y1 = region[0][1].stop - 1

            width = x1 - x0
            height = y1 - y0

            _roi = list_roi[_index]
            _roi[1] = x0
            _roi[2] = y0
            _roi[3] = width
            _roi[4] = height

            list_roi[_index] = _roi

        self.parent.list_roi['normalization'] = list_roi

    def remove_roi(self):
        selection = self.parent.ui.normalization_tableWidget.selectedRanges()
        if selection == []:
            return

        selection = selection[0]
        _row_selected = selection.bottomRow()

        self.parent.ui.normalization_tableWidget.removeRow(_row_selected)

        list_roi = self.parent.list_roi['normalization']
        list_roi_id = self.parent.list_roi_id['normalization']
        list_label_roi_id = self.parent.list_label_roi_id['normalization']

        new_list_roi = []
        new_list_roi_id = []
        new_list_label_roi_id = []
        for _index, _roi in enumerate(list_roi):
            if _index == _row_selected:
                self.parent.step2_ui['image_view'].removeItem(list_roi_id[_index])
                self.parent.step2_ui['image_view'].removeItem(list_label_roi_id[_index])
                continue
            new_list_roi.append(_roi)
            new_list_roi_id.append(list_roi_id[_index])
            new_list_label_roi_id.append(list_label_roi_id[_index])

        self.parent.list_roi['normalization'] = new_list_roi
        self.parent.list_roi_id['normalization'] = new_list_roi_id
        self.parent.list_label_roi_id['normalization'] = new_list_label_roi_id

        o_gui = gui_handler.Step2GuiHandler(parent=self.parent)
        o_gui.check_add_remove_roi_buttons()

    def add_roi_in_image(self):
        x0 = int(DEFAULT_ROI[1])
        y0 = int(DEFAULT_ROI[2])
{'fraction_non_alphanumeric': 0.10103199174406605, 'fraction_numerical': 0.011042311661506708, 'mean_word_length': 2.231410470156719, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6794859', 'n_tokens_mistral': 3334, 'n_tokens_neox': 3265, 'n_words': 529}