from pyface.qt import QtCore, QtGui, is_qt4
from pyface.image_resource import ImageResource
from pyface.timer.api import do_later
from pyface.ui_traits import Image
from traits.api import (
    Any,
    Bool,
    Button,
    Dict,
    Event,
    List,
    HasTraits,
    Instance,
    Int,
    Property,
    Str,
    cached_property,
    observe,
)

from traitsui.api import (
    EnumEditor,
    InstanceEditor,
    Group,
    Item,
    Label,
    ObjectColumn,
    TableColumn,
    TableFilter,
    UI,
    View,
    default_handler,
    spring,
)
from traitsui.editors.table_editor import (
    BaseTableEditor,
    ReversedList,
    customize_filter,
)
from traitsui.ui_traits import SequenceTypes

from .editor import Editor
from .table_model import TableModel, SortFilterTableModel

if is_qt4:

    def set_qheader_section_resize_mode(header):
        return header.setResizeMode

else:

    def set_qheader_section_resize_mode(header):
        return header.setSectionResizeMode

class TableEditor(Editor, BaseTableEditor):
    """Editor that presents data in a table. Optionally, tables can have
    a set of filters that reduce the set of data displayed, according to
    their criteria.
    """

    # -------------------------------------------------------------------------
    #  Trait definitions:
    # -------------------------------------------------------------------------

    #: The table view control associated with the editor:
    table_view = Any()

    def _table_view_default(self):
        return TableView(editor=self)

    #: A wrapper around the source model which provides filtering and sorting:
    model = Instance(SortFilterTableModel)

    def _model_default(self):
        return SortFilterTableModel(editor=self)

    #: The table model associated with the editor:
    source_model = Instance(TableModel)

    def _source_model_default(self):
        return TableModel(editor=self)

    #: The set of columns currently defined on the editor:
    columns = List(TableColumn)

    #: The currently selected row(s), column(s), or cell(s).
    selected = Any()

    #: The current selected row
    selected_row = Property(Any, observe="selected")

    selected_indices = Property(Any, observe="selected")

    #: Current filter object (should be a TableFilter or callable or None):
    filter = Any()

    #: The indices of the table items currently passing the table filter:
    filtered_indices = List(Int)

    #: Current filter summary message
    filter_summary = Str("All items")

    #: Update the filtered contents.
    update_filter = Event()

    #: The event fired when a cell is clicked on:
    click = Event()

    #: The event fired when a cell is double-clicked on:
    dclick = Event()

    #: The Traits UI associated with the table editor toolbar:
    toolbar_ui = Instance(UI)

    #: The index of the row that was last right clicked on its vertical header
    header_row = Int()

    #: Whether to auto-size the columns or not.
    auto_size = Bool(False)

    #: Dictionary mapping image names to QIcons
    images = Dict()

    #: Dictionary mapping ImageResource objects to QIcons
    image_resources = Dict()

    #: An image being converted:
    image = Image

    def init(self, parent):
        """Finishes initializing the editor by creating the underlying toolkit
        widget."""

        factory = self.factory
        self.filter = factory.filter

        columns = factory.columns[:]
        if (len(columns) == 0) and (len(self.value) > 0):
            columns = [
                ObjectColumn(name=name)
                for name in self.value[0].editable_traits()
            ]
        self.columns = columns

        if factory.table_view_factory is not None:
            self.table_view = factory.table_view_factory(editor=self)
        if factory.source_model_factory is not None:
            self.source_model = factory.source_model_factory(editor=self)
        if factory.model_factory is not None:
            self.model = factory.model_factory(editor=self)

        # Create the table view and model
        self.model.setDynamicSortFilter(True)
        self.model.setSourceModel(self.source_model)
        self.table_view.setModel(self.model)

        # When sorting is enabled, the first column is initially displayed with
        # the triangle indicating it is the sort index, even though no sorting
        # has actually been done. Sort here for UI/model consistency.
        if self.factory.sortable and not self.factory.reorderable:
            self.model.sort(0, QtCore.Qt.SortOrder.AscendingOrder)

        # Connect to the mode specific selection handler and select the first
        # row/column/cell. Do this before creating the edit_view to make sure
        # that it has a valid item to use when constructing its view.
        smodel = self.table_view.selectionModel()
        mode_slot = getattr(self, "_on_%s_selection" % factory.selection_mode)
        smodel.selectionChanged.connect(mode_slot)
        self.table_view.setCurrentIndex(self.model.index(0, 0))

        # Create the toolbar if necessary
        if factory.show_toolbar and len(factory.filters) > 0:
            main_view = QtGui.QWidget()
            layout = QtGui.QVBoxLayout(main_view)
            layout.setContentsMargins(0, 0, 0, 0)
            self.toolbar_ui = self.edit_traits(
                parent=parent,
                kind="subpanel",
                view=View(
                    Group(
                        Item("filter{View}", editor=factory._filter_editor),
                        Item("filter_summary{Results}", style="readonly"),
                        spring,
                        orientation="horizontal",
                    ),
                    resizable=True,
                ),
            )
            self.toolbar_ui.parent = self.ui
            layout.addWidget(self.toolbar_ui.control)
            layout.addWidget(self.table_view)
        else:
            main_view = self.table_view

        # Create auxiliary editor and encompassing splitter if necessary
        mode = factory.selection_mode
        if (factory.edit_view == " ") or mode not in {"row", "rows"}:
            self.control = main_view
        else:
            if factory.orientation == "horizontal":
                self.control = QtGui.QSplitter(QtCore.Qt.Orientation.Horizontal)
            else:
                self.control = QtGui.QSplitter(QtCore.Qt.Orientation.Vertical)
            self.control.setSizePolicy(
                QtGui.QSizePolicy.Policy.Expanding, QtGui.QSizePolicy.Policy.Expanding
            )
            self.control.addWidget(main_view)
            self.control.setStretchFactor(0, 2)

            # Create the row editor below the table view
            editor = InstanceEditor(view=factory.edit_view, kind="subpanel")
            self._ui = self.edit_traits(
                parent=self.control,
                kind="subpanel",
                view=View(
                    Item(
                        "selected_row",
                        style="custom",
                        editor=editor,
                        show_label=False,
                        resizable=True,
                        width=factory.edit_view_width,
                        height=factory.edit_view_height,
                    ),
                    resizable=True,
                    handler=factory.edit_view_handler,
                ),
            )
            self._ui.parent = self.ui
            self.control.addWidget(self._ui.control)
            self.control.setStretchFactor(1, 1)

        # Connect to the click and double click handlers
        self.table_view.clicked.connect(self._on_click)
        self.table_view.doubleClicked.connect(self._on_dclick)

        # Make sure we listen for 'items' changes as well as complete list
        # replacements
        self.context_object.on_trait_change(
            self.update_editor, self.extended_name + "_items", dispatch="ui"
        )

        # Listen for changes to traits on the objects in the list
        self.context_object.on_trait_change(
            self.refresh_editor, self.extended_name + ".-", dispatch="ui"
        )

        # Listen for changes on column definitions
        self.on_trait_change(self._update_columns, "columns", dispatch="ui")
        self.on_trait_change(
            self._update_columns, "columns_items", dispatch="ui"
        )

        # Set up the required externally synchronized traits
        is_list = mode in ("rows", "columns", "cells")
        self.sync_value(factory.click, "click", "to")
        self.sync_value(factory.dclick, "dclick", "to")
        self.sync_value(factory.columns_name, "columns", is_list=True)
        self.sync_value(factory.selected, "selected", is_list=is_list)
        self.sync_value(
            factory.selected_indices, "selected_indices", is_list=is_list
        )
        self.sync_value(factory.filter_name, "filter", "from")
        self.sync_value(factory.filtered_indices, "filtered_indices", "to")
        self.sync_value(factory.update_filter_name, "update_filter", "from")

        self.auto_size = self.factory.auto_size

        # Initialize the ItemDelegates for each column
        self._update_columns()

    def dispose(self):
        """Disposes of the contents of an editor."""
        self.model.beginResetModel()
        self.model.endResetModel()

        # Make sure that the auxiliary UIs are properly disposed
        if self.toolbar_ui is not None:
            self.toolbar_ui.dispose()
        if self._ui is not None:
            self._ui.dispose()

        # Remove listener for 'items' changes on object trait
        self.context_object.on_trait_change(
            self.update_editor, self.extended_name + "_items", remove=True
        )

        # Remove listener for changes to traits on the objects in the list
        self.context_object.on_trait_change(
            self.refresh_editor, self.extended_name + ".-", remove=True
        )

        # Remove listeners for column definition changes
        self.on_trait_change(self._update_columns, "columns", remove=True)
        self.on_trait_change(
            self._update_columns, "columns_items", remove=True
        )

        super().dispose()

    def update_editor(self):
        """Updates the editor when the object trait changes externally to the
        editor."""

        if self._no_notify:
            return

        self.table_view.setUpdatesEnabled(False)
        try:
            filtering = (
                len(self.factory.filters) > 0 or self.filter is not None
            )
            if filtering:
                self._update_filtering()

            # invalidate the model, but do not reset it. Resetting the model
            # may cause problems if the selection sync'ed traits are being used
            # externally to manage the selections
            self.model.invalidate()

            self.table_view.resizeColumnsToContents()
            if self.auto_size:
                self.table_view.resizeRowsToContents()

        finally:
            self.table_view.setUpdatesEnabled(True)

    def restore_prefs(self, prefs):
        """Restores any saved user preference information associated with the
        editor.
        """
        header = self.table_view.horizontalHeader()
        if header is not None and "column_state" in prefs:
            header.restoreState(prefs["column_state"])

    def save_prefs(self):
        """Returns any user preference information associated with the editor."""
        prefs = {}
        header = self.table_view.horizontalHeader()
        if header is not None:
            prefs["column_state"] = header.saveState().data()
        return prefs

    def refresh_editor(self):
        """Requests that the underlying table widget to redraw itself."""

        self.table_view.viewport().update()

    def create_new_row(self):
        """Creates a new row object using the provided factory."""

        factory = self.factory
        kw = factory.row_factory_kw.copy()
        if "__table_editor__" in kw:
            kw["__table_editor__"] = self

        return self.ui.evaluate(
            factory.row_factory, *factory.row_factory_args, **kw
        )

    def items(self):
        """Returns the raw list of model objects."""

        items = self.value
        if not isinstance(items, SequenceTypes):
            items = [items]

        if self.factory and self.factory.reverse:
            items = ReversedList(items)

        return items

    def callx(self, func, *args, **kw):
        """Call a function without notifying the underlying table view or
        model."""

        old = self._no_notify
        self._no_notify = True
        try:
            func(*args, **kw)
        finally:
            self._no_notify = old

    def setx(self, **keywords):
        """Set one or more attributes without notifying the underlying table
        view or model."""

        old = self._no_notify
        self._no_notify = True
        try:
            for name, value in keywords.items():
                setattr(self, name, value)
        finally:
            self._no_notify = old

    def set_selection(self, objects=[], notify=True):
        """Sets the current selection to a set of specified objects."""

        if not isinstance(objects, list):
            objects = [objects]

        mode = self.factory.selection_mode
        indexes = []
        flags = QtGui.QItemSelectionModel.SelectionFlag.ClearAndSelect

        # In the case of row or column selection, we need a dummy value for the
        # other dimension that has not been filtered.
        source_index = self.model.mapToSource(self.model.index(0, 0))
        source_row, source_column = source_index.row(), source_index.column()

        # Selection mode is 'row' or 'rows'
        if mode.startswith("row"):
            flags |= QtGui.QItemSelectionModel.SelectionFlag.Rows
            items = self.items()
            for obj in objects:
                try:
                    row = items.index(obj)
                except ValueError:
                    continue
                indexes.append(self.source_model.index(row, source_column))

        # Selection mode is 'column' or 'columns'
        elif mode.startswith("column"):
            flags |= QtGui.QItemSelectionModel.SelectionFlag.Columns
            for name in objects:
                column = self._column_index_from_name(name)
                if column != -1:
                    indexes.append(self.source_model.index(source_row, column))

        # Selection mode is 'cell' or 'cells'
        else:
            items = self.items()
            for obj, name in objects:
                try:
                    row = items.index(obj)
                except ValueError:
                    continue
                column = self._column_index_from_name(name)
                if column != -1:
                    indexes.append(self.source_model.index(row, column))

        # Perform the selection so that only one signal is emitted
        selection = QtGui.QItemSelection()
        smodel = self.table_view.selectionModel()
        if smodel is None:
            # guard against selection during tear-down
            return
        for index in indexes:
            index = self.model.mapFromSource(index)
            if index.isValid():
                smodel.setCurrentIndex(
                    index, QtGui.QItemSelectionModel.SelectionFlag.NoUpdate
                )
                selection.select(index, index)

        smodel.blockSignals(not notify)
        try:
            if len(selection.indexes()):
                smodel.clear()
                smodel.select(selection, flags)
            else:
                smodel.clear()
        finally:
            smodel.blockSignals(False)

        self.refresh_editor()

    # -------------------------------------------------------------------------
    #  Private methods:
    # -------------------------------------------------------------------------

    def _column_index_from_name(self, name):
        """Returns the index of the column with the given name or -1 if no
        column exists with that name."""

        for i, column in enumerate(self.columns):
            if name == column.name:
                return i
        return -1

    def _customize_filters(self, filter):
        """Allows the user to customize the current set of table filters."""

        filter_editor = TableFilterEditor(editor=self)
        ui = filter_editor.edit_traits(parent=self.control)
        if ui.result:
            self.factory.filters = filter_editor.templates
            self.filter = filter_editor.selected_filter
        else:
            self.setx(filter=filter)

    def _update_filtering(self):
        """Update the filter summary and the filtered indices."""

        items = self.items()
        num_items = len(items)

        f = self.filter
        if f is None:
            self._filtered_cache = None
            self.filtered_indices = list(range(num_items))
            self.filter_summary = "All %i items" % num_items
        else:
            if not callable(f):
                f = f.filter
            self._filtered_cache = fc = [f(item) for item in items]
            self.filtered_indices = fi = [i for i, ok in enumerate(fc) if ok]
            self.filter_summary = "%i of %i items" % (len(fi), num_items)

    def _add_image(self, image_resource):
        """Adds a new image to the image map."""
        image = image_resource.create_icon()

        self.image_resources[image_resource] = image
        self.images[image_resource.name] = image

        return image

    def _get_image(self, image):
        """Converts a user specified image to a QIcon."""
        if isinstance(image, str):
            self.image = image
            image = self.image

        if isinstance(image, ImageResource):
            result = self.image_resources.get(image)
            if result is not None:
                return result
            return self._add_image(image)

        return self.images.get(image)

    def _create_empty_menu(self):
        """Create a QMenu to display in empty space below the rows.

        Returns a QMenu or None if no menu to display.
        """
        if not self.factory.editable or self.factory.row_factory is None:
            return None

        empty_menu = QtGui.QMenu(self.table_view)
        action = empty_menu.addAction("Add new item")
        action.triggered.connect(self._on_context_append)
        return empty_menu

    def _create_header_menu(self):
        """Create a QMenu to display in the vertical header.

        Returns a QMenu or None if no menu to display.
        """
        header_menu = QtGui.QMenu(self.table_view)
        if self.factory.editable:
            if self.factory.row_factory is not None:
                action = header_menu.addAction("Insert new item")
                action.triggered.connect(self._on_context_insert)
            if self.factory.deletable:
                action = header_menu.addAction("Delete item")
                action.triggered.connect(self._on_context_remove)
        if self.factory.reorderable:
            show_up = (self.header_row > 0)
            show_down = (self.header_row < self.model.rowCount() - 1)
            if not header_menu.isEmpty() and (show_up or show_down):
                header_menu.addSeparator()
            if show_up:
                header_menu_up = header_menu.addAction("Move item up")
                header_menu_up.triggered.connect(self._on_context_move_up)
            if show_down:
                header_menu_down = header_menu.addAction("Move item down")
                header_menu_down.triggered.connect(self._on_context_move_down)

        if header_menu.isEmpty():
            return None
        else:
            return header_menu

    # -- Trait Property getters/setters ---------------------------------------

    @cached_property
    def _get_selected_row(self):
        """Gets the selected row, or the first row if multiple rows are
        selected."""

        mode = self.factory.selection_mode

        if mode.startswith("column"):
            return None
        elif mode == "row":
            return self.selected

        try:
            if mode == "rows":
                return self.selected[0]
            elif mode == "cell":
                return self.selected[0]
            elif mode == "cells":
                return self.selected[0][0]
        except IndexError:
            return None

    @cached_property
    def _get_selected_indices(self):
        """Gets the row,column indices which match the selected trait"""
        selection_items = self.table_view.selectionModel().selection()
        indices = self.model.mapSelectionToSource(selection_items).indexes()
        if self.factory.selection_mode.startswith("row"):
            indices = sorted(set(index.row() for index in indices))
        elif self.factory.selection_mode.startswith("column"):
            indices = sorted(set(index.column() for index in indices))
        else:
            indices = [(index.row(), index.column()) for index in indices]

        if self.factory.selection_mode in {"rows", "columns", "cells"}:
            return indices
        elif len(indices) > 0:
            return indices[0]
        else:
            return -1

    def _set_selected_indices(self, indices):
        if not isinstance(indices, list):
            indices = [indices]
        selected = []
        if self.factory.selection_mode.startswith("row"):
            for row in indices:
                selected.append(self.value[row])
        elif self.factory.selection_mode.startswith("column"):
            for col in indices:
                selected.append(self.columns[col].name)
        else:
            for row, col in indices:
                selected.append((self.value[row], self.columns[col].name))

        self.selected = selected
        self.set_selection(self.selected, False)

    # -- Trait Change Handlers ------------------------------------------------

    def _filter_changed(self, old_filter, new_filter):
        """Handles the current filter being changed."""

        if not self._no_notify:
            if new_filter is customize_filter:
                do_later(self._customize_filters, old_filter)
            else:
                self._update_filtering()
                self.model.invalidate()
                self.set_selection(self.selected)

    def _update_columns(self):
        """Handle the column list being changed."""

        self.table_view.setItemDelegate(TableDelegate(self.table_view))
        for i, column in enumerate(self.columns):
            if column.renderer:
                self.table_view.setItemDelegateForColumn(i, column.renderer)

        self.model.invalidate()
        self.table_view.resizeColumnsToContents()
        if self.auto_size:
            self.table_view.resizeRowsToContents()

    def _selected_changed(self, new):
        """Handle the selected row/column/cell being changed externally."""
        if not self._no_notify:
            self.set_selection(self.selected, notify=False)

    def _update_filter_changed(self):
        """The filter has changed internally."""
        self._filter_changed(self.filter, self.filter)

    # -- Event Handlers -------------------------------------------------------

    def _on_row_selection(self, added, removed):
        """Handle the row selection being changed."""

        items = self.items()
        indexes = self.table_view.selectionModel().selectedRows()
        if len(indexes):
            index = self.model.mapToSource(indexes[0])
            selected = items[index.row()]
        else:
            selected = None

        self.setx(selected=selected)
        self.ui.evaluate(self.factory.on_select, self.selected)

    def _on_rows_selection(self, added, removed):
        """Handle the rows selection being changed."""
        items = self.items()
        indexes = self.table_view.selectionModel().selectedRows()
        selected = [
            items[self.model.mapToSource(index).row()] for index in indexes
        ]

        self.setx(selected=selected)
        self.ui.evaluate(self.factory.on_select, self.selected)

    def _on_column_selection(self, added, removed):
        """Handle the column selection being changed."""

        indexes = self.table_view.selectionModel().selectedColumns()
        if len(indexes):
            index = self.model.mapToSource(indexes[0])
            selected = self.columns[index.column()].name
        else:
            selected = ""

        self.setx(selected=selected)
        self.ui.evaluate(self.factory.on_select, self.selected)

    def _on_columns_selection(self, added, removed):
        """Handle the columns selection being changed."""

        indexes = self.table_view.selectionModel().selectedColumns()
        selected = [
            self.columns[self.model.mapToSource(index).column()].name
            for index in indexes
        ]

        self.setx(selected=selected)
        self.ui.evaluate(self.factory.on_select, self.selected)

    def _on_cell_selection(self, added, removed):
        """Handle the cell selection being changed."""

        items = self.items()
        indexes = self.table_view.selectionModel().selectedIndexes()
        if len(indexes):
            index = self.model.mapToSource(indexes[0])
            obj = items[index.row()]
            column_name = self.columns[index.column()].name
        else:
            obj = None
            column_name = ""
        selected = (obj, column_name)

        self.setx(selected=selected)
        self.ui.evaluate(self.factory.on_select, self.selected)

    def _on_cells_selection(self, added, removed):
        """Handle the cells selection being changed."""

        items = self.items()
        indexes = self.table_view.selectionModel().selectedIndexes()
        selected = []
        for index in indexes:
            index = self.model.mapToSource(index)
            obj = items[index.row()]
            column_name = self.columns[index.column()].name
            selected.append((obj, column_name))

        self.setx(selected=selected)
        self.ui.evaluate(self.factory.on_select, self.selected)

    def _on_click(self, index):
        """Handle a cell being clicked."""

        index = self.model.mapToSource(index)
        column = self.columns[index.column()]
        obj = self.items()[index.row()]

        # Fire the same event on the editor after mapping it to a model object
        # and column name:
        self.click = (obj, column)

        # Invoke the column's click handler:
        column.on_click(obj)

    def _on_dclick(self, index):
        """Handle a cell being double clicked."""

        index = self.model.mapToSource(index)
        column = self.columns[index.column()]
        obj = self.items()[index.row()]

        # Fire the same event on the editor after mapping it to a model object
        # and column name:
        self.dclick = (obj, column)

        # Invoke the column's double-click handler:
        column.on_dclick(obj)

    def _on_context_insert(self):
        """Handle 'insert item' being selected from the header context menu."""

        self.model.insertRow(self.header_row)

    def _on_context_append(self):
        """Handle 'add item' being selected from the empty space context
        menu."""

        self.model.insertRow(self.model.rowCount())

    def _on_context_remove(self):
        """Handle 'remove item' being selected from the header context menu."""

        self.model.removeRow(self.header_row)

    def _on_context_move_up(self):
        """Handle 'move up' being selected from the header context menu."""

        self.model.moveRow(self.header_row, self.header_row - 1)

    def _on_context_move_down(self):
        """Handle 'move down' being selected from the header context menu."""

        self.model.moveRow(self.header_row, self.header_row + 1)

# Define the SimpleEditor class.
SimpleEditor = TableEditor

# Define the ReadonlyEditor class.
ReadonlyEditor = TableEditor

# -------------------------------------------------------------------------
#  Qt widgets that have been configured to behave as expected by Traits UI:
# -------------------------------------------------------------------------

class TableDelegate(QtGui.QStyledItemDelegate):
    """A QStyledItemDelegate which fetches Traits UI editors."""

    def createEditor(self, parent, option, index):
        """Reimplemented to return the editor for a given index."""

        model = index.model()
        index = model.mapToSource(index)
        table_editor = model._editor
        column = table_editor.columns[index.column()]
        obj = table_editor.items()[index.row()]

        factory = column.get_editor(obj)
        style = column.get_style(obj)
        if factory is None:
            return None

        target, name = column.target_name(obj)
        handler = default_handler()
        if table_editor.ui.context is None:
            ui = UI(handler=handler)
        else:
            context = table_editor.ui.context.copy()
            context["table_editor_object"] = context["object"]
            context["object"] = target
            ui = UI(handler=handler, context=context)

        # Create and initialize the editor
        factory_method = getattr(factory, style + "_editor")
        editor = factory_method(ui, target, name, "", parent)
        editor.prepare(parent)
        control = editor.control
        control.setParent(parent)

        # Required for QMouseEvents to propagate to the widget
        control.setFocusPolicy(QtCore.Qt.FocusPolicy.StrongFocus)

        # The table view's background will shine through unless the editor
        # paints its own background
        control.setAutoFillBackground(True)

        # Make sure that editors are disposed of correctly
        # will be disposed in closeEditor of the TableView
        control._editor = editor
        return control

    def updateEditorGeometry(self, editor, option, index):
        """Update the editor's geometry."""
        editor.setGeometry(option.rect)

    def paint(self, painter, option, index):
        self.initStyleOption(option, index)

        if (option.state & QtGui.QStyle.StateFlag.State_Selected) and (
            option.state & QtGui.QStyle.StateFlag.State_Active
        ):
            factory = self.parent()._editor.factory
            if factory.selection_bg_color is not None:
                option.palette.setColor(
                    QtGui.QPalette.ColorRole.Highlight, factory.selection_bg_color_
                )
            if factory.selection_color is not None:
                option.palette.setColor(
                    QtGui.QPalette.ColorRole.HighlightedText, factory.selection_color_
                )

        QtGui.QApplication.style().drawControl(
            QtGui.QStyle.ControlElement.CE_ItemViewItem, option, painter, None
        )

class TableView(QtGui.QTableView):
    """A QTableView configured to behave as expected by TraitsUI."""

    _SELECTION_MAP = {
        "row": (
            QtGui.QAbstractItemView.SelectionBehavior.SelectRows,
            QtGui.QAbstractItemView.SelectionMode.SingleSelection,
        ),
        "rows": (
            QtGui.QAbstractItemView.SelectionBehavior.SelectRows,
            QtGui.QAbstractItemView.SelectionMode.ExtendedSelection,
        ),
        "column": (
            QtGui.QAbstractItemView.SelectionBehavior.SelectColumns,
            QtGui.QAbstractItemView.SelectionMode.SingleSelection,
        ),
        "columns": (
            QtGui.QAbstractItemView.SelectionBehavior.SelectColumns,
            QtGui.QAbstractItemView.SelectionMode.ExtendedSelection,
        ),
        "cell": (
            QtGui.QAbstractItemView.SelectionBehavior.SelectItems,
            QtGui.QAbstractItemView.SelectionMode.SingleSelection,
        ),
        "cells": (
            QtGui.QAbstractItemView.SelectionBehavior.SelectItems,
            QtGui.QAbstractItemView.SelectionMode.ExtendedSelection,
        ),
    }

    def __init__(self, editor):
        """Initialise the object."""

        QtGui.QTableView.__init__(self)

        self._initial_size = False
        self._editor = editor
        factory = editor.factory

        # Configure the grid lines.
        self.setShowGrid(factory.show_lines)

        # Configure the selection behaviour.
        self.setCornerButtonEnabled(False)
        behav, mode = self._SELECTION_MAP[factory.selection_mode]
        self.setSelectionBehavior(behav)
        self.setSelectionMode(mode)

        # Configure the editing behavior.
        triggers = (
            QtGui.QAbstractItemView.EditTrigger.DoubleClicked
            | QtGui.QAbstractItemView.EditTrigger.SelectedClicked
        )
        if factory.edit_on_first_click and not factory.reorderable:
            triggers |= QtGui.QAbstractItemView.EditTrigger.CurrentChanged
        self.setEditTriggers(triggers)

        # Configure the reordering and sorting behavior.
        self.setDragEnabled(True)
        self.viewport().setAcceptDrops(True)
        self.setDropIndicatorShown(True)

        if factory.reorderable:
            self.setDragDropMode(QtGui.QAbstractItemView.DragDropMode.InternalMove)
        if factory.sortable:
            self.setSortingEnabled(True)

        if factory._qt_stylesheet is not None:
            self.setStyleSheet(factory._qt_stylesheet)

        self.resizeColumnsToContents()

    def setModel(self, model):
        super().setModel(model)
        self._update_header_sizing()

    def contextMenuEvent(self, event):
        """Reimplemented to create context menus for cells and empty space."""

        # Determine the logical indices of the cell where click occured
        hheader, vheader = self.horizontalHeader(), self.verticalHeader()
        position = event.globalPos()
        row = vheader.logicalIndexAt(vheader.mapFromGlobal(position))
        column = hheader.logicalIndexAt(hheader.mapFromGlobal(position))

        # Map the logical row index to a real index for the source model
        model = self.model()
        row = model.mapToSource(model.index(row, 0)).row()

        # Show a context menu for empty space at bottom of table...
        editor = self._editor
        if row == -1:
            empty_menu = editor._create_empty_menu()
            if empty_menu is not None:
                event.accept()
                empty_menu.exec_(position)

        # ...or show a context menu for a cell.
        elif column != -1:
            obj = editor.items()[row]
            column = editor.columns[column]
            menu_manager = column.get_menu(obj)
            if menu_manager is None:
                menu_manager = editor.factory.menu
            if menu_manager is not None:
                event.accept()
                selected = editor.selected
                if not isinstance(selected, SequenceTypes):
                    selected = [selected]
                if obj not in selected:
                    selected = [obj]
                editor.set_menu_context(selected, obj, column)
                menu = menu_manager.create_menu(self, controller=editor)
                menu.exec_(position)

    def eventFilter(self, obj, event):
        """Reimplemented to create context menu for the vertical header."""

        vheader = self.verticalHeader()
        if obj is vheader and event.type() == QtCore.QEvent.Type.ContextMenu:
            position = event.globalPos()
            editor = self._editor
            row = vheader.logicalIndexAt(event.pos().y())
            if row == -1:
                empty_menu = editor._create_empty_menu()
                if empty_menu is not None:
                    event.accept()
                    empty_menu.exec_(position)
            else:
                editor.header_row = row
                header_menu = editor._create_header_menu()
                if header_menu is not None:
                    event.accept()
                    header_menu.exec_(position)
            return True

        else:
            return QtGui.QTableView.eventFilter(self, obj, event)

    def resizeEvent(self, event):
        """Reimplemented to size the table columns when the size of the table
        changes. Because the layout algorithm requires that the available
        space be known, we have to wait until the UI that contains this table
        gives it its initial size."""

        QtGui.QTableView.resizeEvent(self, event)

        if self._editor.auto_size:
            self.resizeColumnsToContents()
            self.resizeRowsToContents()

        else:
            parent = self.parent()
            if (
                not self._initial_size
                and parent
                and (self.isVisible() or isinstance(parent, QtGui.QMainWindow))
            ):
                self._initial_size = True
                if self._editor.auto_size:
                    self.resizeColumnsToContents()
                    self.resizeRowsToContents()

    def sizeHint(self):
        """Reimplemented to define a better size hint for the width of the
        TableEditor."""

        size_hint = QtGui.QTableView.sizeHint(self)

        # This method is sometimes called by Qt after the editor has been
        # disposed but before this control has been deleted:
        if self._editor.factory is None:
            return size_hint

        width = self.style().pixelMetric(
            QtGui.QStyle.PixelMetric.PM_ScrollBarExtent, QtGui.QStyleOptionHeader(), self
        )
        for column in range(len(self._editor.columns)):
            width += self.sizeHintForColumn(column)
        size_hint.setWidth(width)
        return size_hint

    def sizeHintForColumn(self, column_index):
        """Reimplemented to support absolute width specification via
        TableColumns and to improve the metric for autosizing columns."""

        editor = self._editor
        column = editor.columns[column_index]
        requested_width = column.get_width()

        # Autosize based on column contents and label width. Qt's default
        # implementation of this function does content, we handle the label.
        if requested_width < 1:
            base_width = QtGui.QTableView.sizeHintForColumn(self, column_index)

            # Determine what font to use in the calculation
            font = column.get_text_font(None)
            if font is None:
                font = self.font()
                font.setBold(True)
            else:
                font = QtGui.QFont(font)

            # Determine the width of the column label
            text = column.get_label()
            # QFontMetrics.width() is deprecated and Qt docs suggest using
            # horizontalAdvance() instead, but is only available since Qt 5.11
            if QtCore.__version_info__ >= (5, 11):
                width = QtGui.QFontMetrics(font).horizontalAdvance(text)
            else:
                width = QtGui.QFontMetrics(font).width(text)

            # Add margin to the calculated width as appropriate
            style = self.style()
            option = QtGui.QStyleOptionHeader()
            width += (
                style.pixelMetric(
                    QtGui.QStyle.PixelMetric.PM_HeaderGripMargin, option, self
                )
                * 2
            )
            if editor.factory.sortable and not editor.factory.reorderable:
                # Add size of sort indicator
                width += style.pixelMetric(
                    QtGui.QStyle.PixelMetric.PM_HeaderMarkSize, option, self
                )
                # Add distance between sort indicator and text
                width += style.pixelMetric(
                    QtGui.QStyle.PixelMetric.PM_HeaderMargin, option, self
                )
            return max(base_width, width)

        # Or else set width absolutely
        else:
            return requested_width

    def resizeColumnsToContents(self):
        """Support proportional column width specifications."""

        # TODO: The proportional size specification approach found in the
        # TableColumns is not entirely compatible with the ability to
        # specify the resize_mode.  Namely, there are combinations of
        # specifications that are redundant, and others which are
        # contradictory.  Rework this method so that the various values
        # for **width** have a well-defined, sensible meaning for each
        # of the possible values of resize_mode.

        editor = self._editor
        available_space = self.viewport().width()
        hheader = self.horizontalHeader()

        # Compute sizes for columns with absolute or no size requests
        proportional = []
        for column_index in range(len(editor.columns)):
            column = editor.columns[column_index]
            requested_width = column.get_width()
            if (
                column.resize_mode in ("interactive", "stretch")
                and 0 < requested_width <= 1.0
            ):
                proportional.append((column_index, requested_width))
            elif (
                column.resize_mode == "interactive"
                and requested_width < 0
                and self._initial_size
            ):
                # Keep previous size if initial sizing has been done
                available_space -= hheader.sectionSize(column_index)
            else:
                base_width = hheader.sectionSizeHint(column_index)
                width = max(base_width, self.sizeHintForColumn(column_index))
                hheader.resizeSection(column_index, width)
                available_space -= width

        # Now use the remaining space for columns with proportional width
        # requests
        for column_index, percent in proportional:
            base_width = hheader.sectionSizeHint(column_index)
            width = max(base_width, int(percent * available_space))
            hheader.resizeSection(column_index, width)

    def closeEditor(self, control, hint):
        # dispose traits editor associated with control if any
        editor = getattr(control, "_editor", None)
        if editor is not None:
            editor.dispose()
            delattr(control, "_editor")

        return super().closeEditor(control, hint)

    def _update_header_sizing(self):
        """Header section sizing can be done only after a valid model is set.
        Otherwise results in segfault with Qt5.
        """
        editor = self._editor
        factory = editor.factory
        # Configure the row headings.
        vheader = self.verticalHeader()
        set_resize_mode = set_qheader_section_resize_mode(vheader)
        insertable = factory.row_factory is not None
        if (
            factory.editable and (insertable or factory.deletable)
        ) or factory.reorderable:
            vheader.installEventFilter(self)
            set_resize_mode(QtGui.QHeaderView.ResizeMode.ResizeToContents)
        elif not factory.show_row_labels:
            vheader.hide()
        if factory.row_height > 0:
            vheader.setDefaultSectionSize(factory.row_height)
        self.setAlternatingRowColors(factory.alternate_bg_color)
        self.setHorizontalScrollMode(QtGui.QAbstractItemView.ScrollMode.ScrollPerPixel)
        # Configure the column headings.
        # We detect if there are any stretchy sections at all; if not, then
        # we make the last non-fixed-size column stretchy.
        hheader = self.horizontalHeader()
        set_resize_mode = set_qheader_section_resize_mode(hheader)
        resize_mode_map = dict(
            interactive=QtGui.QHeaderView.ResizeMode.Interactive,
            fixed=QtGui.QHeaderView.ResizeMode.Fixed,
            stretch=QtGui.QHeaderView.ResizeMode.Stretch,
            resize_to_contents=QtGui.QHeaderView.ResizeMode.ResizeToContents,
        )
        stretchable_columns = []
        for i, column in enumerate(editor.columns):
            set_resize_mode(i, resize_mode_map[column.resize_mode])
            if column.resize_mode in ("stretch", "interactive"):
                stretchable_columns.append(i)
        if not stretchable_columns:
            # Use the behavior from before the "resize_mode" trait was added
            # to TableColumn
            hheader.setStretchLastSection(True)
        else:
            # hheader.setSectionResizeMode(
            #     stretchable_columns[-1], QtGui.QHeaderView.ResizeMode.Stretch)
            hheader.setStretchLastSection(False)
        if factory.show_column_labels:
            hheader.setHighlightSections(False)
        else:
            hheader.hide()

# -------------------------------------------------------------------------
#  Editor for configuring the filters available to a TableEditor:
# -------------------------------------------------------------------------

class TableFilterEditor(HasTraits):
    """An editor that manages table filters."""

    # -------------------------------------------------------------------------
    #  Trait definitions:
    # -------------------------------------------------------------------------

    #: TableEditor this editor is associated with
    editor = Instance(TableEditor)

    #: The list of filters
    filters = List(TableFilter)

    #: The list of available templates from which filters can be created
    templates = Property(List(TableFilter), observe="filters")

    #: The currently selected filter template
    selected_template = Instance(TableFilter)

    #: The currently selected filter
    selected_filter = Instance(TableFilter, allow_none=True)

    #: The view to use for the current filter
    selected_filter_view = Property(observe="selected_filter")

    #: Buttons for add/removing filters
    add_button = Button("New")
    remove_button = Button("Delete")

    # The default view for this editor
    view = View(
        Group(
            Group(
                Group(
                    Item("add_button", enabled_when="selected_template"),
                    Item(
                        "remove_button",
                        enabled_when="len(templates) > 1 and "
                        "selected_filter is not None",
                    ),
                    orientation="horizontal",
                    show_labels=False,
                ),
                Label("Base filter for new filters:"),
                Item("selected_template", editor=EnumEditor(name="templates")),
                Item(
                    "selected_filter",
                    style="custom",
                    editor=EnumEditor(name="filters", mode="list"),
                ),
                show_labels=False,
            ),
            Item(
                "selected_filter",
                width=0.75,
                style="custom",
                editor=InstanceEditor(view_name="selected_filter_view"),
            ),
            id="TableFilterEditorSplit",
            show_labels=False,
            layout="split",
            orientation="horizontal",
        ),
        id="traitsui.qt.table_editor.TableFilterEditor",
        buttons=["OK", "Cancel"],
        kind="livemodal",
        resizable=True,
        width=800,
        height=400,
        title="Customize filters",
    )

    # -------------------------------------------------------------------------
    #  Private methods:
    # -------------------------------------------------------------------------

    # -- Trait Property getter/setters ----------------------------------------

    @cached_property
    def _get_selected_filter_view(self):
        view = None
        if self.selected_filter:
            model = self.editor.model
            index = model.mapToSource(model.index(0, 0))
            if index.isValid():
                obj = self.editor.items()[index.row()]
            else:
                obj = None
            view = self.selected_filter.edit_view(obj)
        return view

    @cached_property
    def _get_templates(self):
        templates = [f for f in self.editor.factory.filters if f.template]
        templates.extend(self.filters)
        return templates

    # -- Trait Change Handlers ------------------------------------------------

    def _editor_changed(self):
        self.filters = [
            f.clone_traits()
            for f in self.editor.factory.filters
            if not f.template
        ]
        self.selected_template = self.templates[0]

    @observe('add_button')
    def _create_and_select_new_filter(self, event):
        """Create a new filter based on the selected template and select it."""
        new_filter = self.selected_template.clone_traits()
        new_filter.template = False
        new_filter.name = new_filter._name = "New filter"
        self.filters.append(new_filter)
        self.selected_filter = new_filter

    @observe("remove_button")
    def _delete_selected_filter(self, event):
        """Delete the currently selected filter."""
        if self.selected_template == self.selected_filter:
            self.selected_template = self.templates[0]

        index = self.filters.index(self.selected_filter)
        del self.filters[index]
        if index < len(self.filters):
            self.selected_filter = self.filters[index]
        else:
            self.selected_filter = None

    @observe("selected_filter:name")
    def _update_filter_list(self, event):
        """A hack to make the EnumEditor watching the list of filters refresh
        their text when the name of the selected filter changes.
        """
        filters = self.filters
        self.filters = []
        self.filters = filters


from django.urls import path, include

from . import views

app_name = 'api'

employment = [
    path('', views.EmploymentListEmployee.as_view(), name='list'),
]

employee = [
    path('<int:pk>/employment/', include((employment, 'employment'))),
]

urlpatterns = [
    path('employee/', include((employee, 'employee'))),
]


import os

_, filename = os.path.split('/a/b/c/t.txt')

print(filename)

metro_areas = [
    ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
    ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
    ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
    ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
    ('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
]

print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
fmt = '{:15} | {:9.4f} | {:9.4f}'
for name, cc, pop, (latitude, latitude) in metro_areas:
    if latitude <= 0:
        print(fmt.format(name, latitude, latitude))

from collections import namedtuple

City = namedtuple('City', 'name country population coordinates')
tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
print(tokyo)

for key, value in tokyo._asdict().items():
    print(key + ":", value)


import transformers
import torch

def shape(structure):
    try:
        return structure.shape
    except AttributeError:
        return (f"list[{len(structure)}]", *shape(structure[0]))

short_prompt = """To be or not to"""

long_prompt = """It was the best of times, it was the worst"""

if __name__ == "__main__":
    print("Started")

    model_uri = "gpt2"

    tokenizer = transformers.AutoTokenizer.from_pretrained(model_uri)
    tokenizer.pad_token = tokenizer.eos_token
    model = transformers.AutoModelForCausalLM.from_pretrained(model_uri)
    model.to("cuda:0")

    inputs = tokenizer([short_prompt, long_prompt], padding=True, return_tensors="pt").to("cuda:0")

    generated = []
    for _ in range(10):
        print(inputs.keys())
        print(shape(inputs["input_ids"]))
        print(shape(inputs["attention_mask"]))
        print(inputs["attention_mask"].sum(axis=1, dtype=torch.int64))
        with torch.no_grad():
            outputs = model(**inputs)

        print(shape(outputs.past_key_values))
        print()

        next_tokens = outputs.logits[:, -1, :].max(axis=-1).indices

        generated.append(tokenizer.decode(next_tokens))

        insertion_points = inputs["attention_mask"].sum(axis=1, dtype=torch.int64)
        new_column = torch.tensor(tokenizer.pad_token_id).repeat(2).to("cuda:0")
        new_inputs = torch.cat((inputs["input_ids"], new_column[:, None]), dim=1)
        new_inputs.scatter_(1, insertion_points[:, None], next_tokens[:, None])

        mask = inputs["attention_mask"]
        new_mask_column = torch.zeros((len(inputs["input_ids"]), 1)).to("cuda:0")
        new_mask = torch.cat((mask, new_mask_column), dim=1)
        new_mask.scatter_(1, insertion_points[:, None], torch.ones(2, 1).to("cuda:0"))

        # inputs["input_ids"] = new_inputs
        # inputs["attention_mask"] = new_mask
        # inputs.past_key_values = outputs.past_key_values

        inputs = {
            "input_ids": new_inputs,
            "attention_mask": new_mask,
            # "past_key_values": outputs.past_key_values
        }

    print(tokenizer.batch_decode(new_inputs, ignore_special_tokens=True))


import string

# Initializing Variables
num_sentences = 0
num_words = 0
the_num_sentences = 0
frequency_the = 0

# Task 0

with open('war_and_peace.txt', 'r') as f:  # opening and reading file
    for line in f:
        line = line.rstrip()  # removing the space on right side
        num_sentences += line.count('.') + line.count('!') + line.count('?')  # counting the sentences

with open('war_and_peace.txt', 'r') as f:
    for line in f:
        words = line.split(None)  # splitting into words and storing in list
        num_words += len(words)  # finding length of list

print("Number of sentences:", num_sentences)
print("Number of words:", num_words)

# Task 1

text = open('war_and_peace.txt', "r")  # Open the file in read mode

d = dict()  # Create an empty dictionary

for line in text:  # Loop through each line of the file

    line = line.strip()  # Remove the leading spaces and newline character

    # Convert the characters in line to

    line = line.lower()  # lowercase to avoid case mismatch

    line = line.translate(line.maketrans("", "", string.punctuation))  # Remove the punctuation marks from the line

    words = line.split(" ")  # Split the line into words

    for word in words:     # Iterate over each word in line

        if word in d:         # Check if the word is already in dictionary
            d[word] = d[word] + 1    # Increment count of word by 1
        else:
            d[word] = 1

sorted_by_value = sorted(d.items(), key=lambda kv: kv[1], reverse=True)  # sorting  dictionary as higher value first

with open('result.csv', 'w', newline='') as f:  # creating csv file
    for w in sorted_by_value:
        f.write(w[0] + "," + str(d[w[0]]) + "," + str(d[w[0]] / num_words) + '\n')  # writing in to csv file

# Task 2
with open('war_and_peace.txt', 'r') as f:
    for line in f:
        line = line.rstrip()
        the_num_sentences += (line.count(". the") + line.count("! the") + line.count("? the") +
                              line.count('. The') + line.count('! The') + line.count('? The'))
    print("Number of sentences starts with 'the' :", the_num_sentences)

frequency_the = the_num_sentences / num_sentences
print("Frequency of THE sentences:", frequency_the)

# Task 3

import re
from itertools import islice
from collections import Counter

s = open("war_and_peace.txt")  #opening file
g = s.read()
words = re.findall("\w+", g) #finding the reg-ex
letter = (Counter(zip(words, islice(words, 1, None))))    #counting The frequent two word combunations
print(letter.most_common()[0])


import sys
import numpy as np

def compute_class_precision_recall(L,K):
    _,L = np.unique(np.array(L),return_inverse=True)
    _,K = np.unique(np.array(K),return_inverse=True)
    if(len(L) != len(K)):
        sys.stderr.write("Labels and clusters are not of the same length.")
        sys.exit(1)
    num_elements = len(L)
    num_labels   = L.max() + 1
    num_clusters = K.max() + 1
    X_L = np.tile(L, (num_labels,1) ).T
    X_K = np.tile(K, (num_clusters,1) ).T
    L_j = np.equal( np.tile(np.arange(num_labels),(num_elements,1))   , X_L ).astype(float)
    K_j = np.equal( np.tile(np.arange(num_clusters),(num_elements,1)) , X_K ).astype(float)
    P_ij = np.dot(L_j.T,K_j)
    S_i  = P_ij.sum(axis=1)
    T_i  = P_ij.sum(axis=0)
    R_i  = ( P_ij * P_ij ).sum(axis=1) / ( S_i * S_i )
    P_i  = ( P_ij.T * P_ij.T ).sum(axis=1) / ( T_i * T_i )
    return [(P_i , R_i) , (S_i , T_i)]

def calc_b3(L , K , class_norm=False, beta=1.0):
    precision_recall , class_sizes = compute_class_precision_recall(L,K)
    if(class_norm == True):
        precision = precision_recall[0].sum() / class_sizes[1].size
        recall    = precision_recall[1].sum() / class_sizes[0].size
    else:
        precision = ( precision_recall[0] * class_sizes[1] ).sum() / class_sizes[1].sum()
        recall    = ( precision_recall[1] * class_sizes[0] ).sum() / class_sizes[0].sum()
    f_measure = (1 + beta**2) * (precision * recall) /( (beta**2) * precision + recall )
    return [f_measure,precision,recall]

# L = np.array([1,3,3,3,3,4,2,2,0,3,3])
# K = np.array([1,2,3,4,5,5,5,6,2,1,1])
# # Standard BCUBED
# [fmeasure, precision, recall] = calc_b3(L,K)
#
# print( calc_b3(L,K) )


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
@author: Pramod Bharadwaj Chandrashekar, Li Liu
@email: pchandrashe3@wisc.edu, liliu@asu.edu
"""

import numpy as np
from sklearn.cluster import KMeans
import scipy.stats as stats

def get_cdf_pval(data):
    """ Function for guassian mixture of dat and computing pvalues """
    cdf_pvals = []
    for i in range(0, len(data)):
        mn_samp = np.mean(data[i, :])
        sd_samp = np.std(data[i, :])

        kcl = KMeans(n_clusters=2, random_state=0).fit(np.reshape(data[i], [-1, 1]))
        cluster_1_id = np.where(kcl.labels_ == 0)[0]
        c1_mn, c1_sd = np.mean(data[i, cluster_1_id]), np.std(data[i, cluster_1_id])
        cdf_pval_1 = np.reshape(1.0 - stats.norm.cdf(data[i, :], c1_mn, c1_sd), [-1, 1])

        cluster_2_id = np.where(kcl.labels_ == 1)[0]
        c2_mn, c2_sd = np.mean(data[i, cluster_2_id]), np.std(data[i, cluster_2_id])
        cdf_pval_2 = np.reshape(1.0 - stats.norm.cdf(data[i, :], c2_mn, c2_sd), [-1, 1])

        cdf_pval_3 = np.reshape(1.0 - stats.norm.cdf(data[i, :], mn_samp, sd_samp), [-1, 1])

        cdf_pvals.append(np.concatenate((cdf_pval_1, cdf_pval_2, cdf_pval_3), axis=1))
    return cdf_pvals

def get_important_bins(pval_data):
    """ Fetch important bins based on pvalues"""
    imp_bins = []
    # Bonferroni Corrected pvals check
    if len(np.where(pval_data*200 < 0.05)[0]) > 0:
        imp_bins = np.where(pval_data*200 < 0.05)[0]
    # Normal pval check
    elif len(np.where(pval_data < 0.05)[0]):
        imp_bins = np.where(pval_data < 0.05)[0]
    # Top 10 bins
    else:
        sorted_bins = np.argsort(pval_data)
        imp_bins = sorted_bins[0:20]
        #imp_bins = np.argpartition(pval_data, 10)
    return imp_bins


'''
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

'''

# Python program to find LCA of n1 and n2 using one
# traversal of Binary tree

# A binary tree node
class Node:

    # Constructor to create a new tree node
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

# This function returns pointer to LCA of two given
# values n1 and n2
# This function assumes that n1 and n2 are present in
# Binary Tree
def findLCA(root, n1, n2):
    # Base Case
    if root is None: return root

    # If either n1 or n2 matches with root's key, report
    #  the presence by returning root (Note that if a key is
    #  ancestor of other, then the ancestor key becomes LCA
    if root.key == n1 or root.key == n2:
        return root

        # Look for keys in left and right subtrees
    left_lca = findLCA(root.left, n1, n2)
    right_lca = findLCA(root.right, n1, n2)

    # If both of the above calls return Non-NULL, then one key
    # is present in once subtree and other is present in other,
    # So this node is the LCA
    if left_lca and right_lca:
        return root

        # Otherwise check if left subtree or right subtree is LCA
    return left_lca if left_lca is not None else right_lca

# Driver program to test above function

# Let us create a binary tree given in the above example
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
print("LCA(4,5) = ", findLCA(root, 4, 5).key)
print("LCA(4,6) = ", findLCA(root, 4, 6).key)
print("LCA(3,4) = ", findLCA(root, 3, 4).key)
print("LCA(2,4) = ", findLCA(root, 2, 4).key)


import copy
from typing import Dict, Optional, TypeVar

from pymilvus.exceptions import CollectionNotExistException, ExceptionsMessage
from pymilvus.settings import Config

Index = TypeVar("Index")
Collection = TypeVar("Collection")

class Index:
    def __init__(
        self,
        collection: Collection,
        field_name: str,
        index_params: Dict,
        **kwargs,
    ) -> Index:
        """Creates index on a specified field according to the index parameters.

        Args:
            collection(Collection): The collection in which the index is created
            field_name(str): The name of the field to create an index for.
            index_params(dict): Indexing parameters.
            kwargs:
                * *index_name* (``str``) --
                  The name of index which will be created. If no index name is specified,
                  default index name will be used.

        Raises:
            MilvusException: If anything goes wrong.

        Examples:
            >>> from pymilvus import *
            >>> from pymilvus.schema import *
            >>> from pymilvus.types import DataType
            >>> connections.connect()
            <pymilvus.client.stub.Milvus object at 0x7fac15e53470>
            >>> field1 = FieldSchema("int64", DataType.INT64, is_primary=True)
            >>> field2 = FieldSchema("fvec", DataType.FLOAT_VECTOR, is_primary=False, dim=128)
            >>> schema = CollectionSchema(fields=[field1, field2])
            >>> collection = Collection(name='test_collection', schema=schema)
            >>> # insert some data
            >>> index_params = {
            ...     "index_type": "IVF_FLAT",
            ...     "metric_type": "L2",
            ...     "params": {"nlist": 128}}
            >>> index = Index(collection, "fvec", index_params)
            >>> index.params
            {'index_type': 'IVF_FLAT', 'metric_type': 'L2', 'params': {'nlist': 128}}
            >>> index.collection_name
            test_collection
            >>> index.field_name
            fvec
            >>> index.drop()
        """
        from .collection import Collection

        if not isinstance(collection, Collection):
            raise CollectionNotExistException(message=ExceptionsMessage.CollectionType)
        self._collection = collection
        self._field_name = field_name
        self._index_params = index_params
        index_name = kwargs.get("index_name", Config.IndexName)
        self._index_name = index_name
        self._kwargs = kwargs
        if self._kwargs.pop("construct_only", False):
            return

        conn = self._get_connection()
        conn.create_index(self._collection.name, self._field_name, self._index_params, **kwargs)
        indexes = conn.list_indexes(self._collection.name)
        for index in indexes:
            if index.field_name == self._field_name:
                self._index_name = index.index_name
                break

    def _get_connection(self):
        return self._collection._get_connection()

    @property
    def params(self) -> dict:
        """dict: The index parameters"""
        return copy.deepcopy(self._index_params)

    @property
    def collection_name(self) -> str:
        """str: The corresponding collection name"""
        return self._collection.name

    @property
    def field_name(self) -> str:
        """str: The corresponding field name."""
        return self._field_name

    @property
    def index_name(self) -> str:
        """str: The corresponding index name."""
        return self._index_name

    def __eq__(self, other: Index) -> bool:
        """The order of the fields of index must be consistent."""
        return self.to_dict() == other.to_dict()

    def to_dict(self):
        """Put collection name, field name and index params into dict."""
        return {
            "collection": self._collection._name,
            "field": self._field_name,
            "index_name": self._index_name,
            "index_param": self.params,
        }

    def drop(self, timeout: Optional[float] = None, **kwargs):
        """Drop an index and its corresponding index files.

        Args:
            timeout(float, optional): An optional duration of time in seconds to allow
                for the RPC. When timeout is set to None, client waits until server response
                or error occur
            kwargs:
                * *index_name* (``str``) --
                  The name of index. If no index is specified, the default index name is used.
        """
        copy_kwargs = copy.deepcopy(kwargs)
        index_name = copy_kwargs.pop("index_name", Config.IndexName)
        conn = self._get_connection()
        conn.drop_index(
            collection_name=self._collection.name,
            field_name=self.field_name,
            index_name=index_name,
            timeout=timeout,
            **copy_kwargs,
        )


"""fitnesspro URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import re_path, include

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import re_path
from fitnessproapp import views

urlpatterns = [
    re_path('admin/', admin.site.urls),

    re_path(r'^$', views.login, name='login'),
    re_path(r'^Forgot_password$',views.Forgot_password, name='Forgot_password'),
    re_path(r'^index/$', views.index, name='index'),
    re_path(r'^User_profile/$',views.User_profile,name='User_profile'),
    re_path(r'^User_edit_profile/$',views.User_edit_profile,name='User_edit_profile'),
    re_path(r'^about/$', views.about, name='about'),
    re_path(r'^classes/$', views.classes, name='classes'),
    re_path(r'^train/$', views.train, name='train'),
    re_path(r'^selecttrainer/$', views.selecttrainer, name='selecttrainer'),
    re_path(r'^shedule/$', views.shedule, name='shedule'),
    re_path(r'^contact/$', views.contact, name='contact'),
    re_path(r'^signup/$', views.signup, name='signup'),
    re_path(r'^userpaymentpage/$', views.userpaymentpage, name='userpaymentpage'),
    re_path(r'^online_training/$', views.online_training, name='online_training'),
    re_path(r'^offline_training/$', views.offline_training, name='offline_training'),
    re_path(r'^onlin/$', views.onlin, name='onlin'),
    re_path(r'^onedit/(?P<i_id>[0-9]+)/$', views.onedit, name='onedit'),
    re_path(r'^onlineedit/(?P<oned_id>[0-9]+)/$', views.onlineedit, name='onlineedit'),
    re_path(r'^offlin/$', views.offlin, name='offlin'),
    re_path(r'^Usert_profile/$',views.Usert_profile,name='Usert_profile'),
    re_path(r'^Usert_edit_profile/$',views.Usert_edit_profile,name='Usert_edit_profile'),
    re_path(r'^offedit/(?P<i_id>[0-9]+)/$', views.offedit, name='offedit'),
    re_path(r'^offlineedit/(?P<offd_id>[0-9]+)/$', views.offlineedit, name='offlineedit'),
    re_path(r'^staffd/$', views.staffd, name='staffd'),
    re_path(r'^maint/$', views.maint, name='maint'),
    re_path(r'^admhome/$', views.admhome, name='admhome'),
    re_path(r'^admreg/$', views.admreg, name='admreg'),
    re_path(r'^admregedit/(?P<i_id>[0-9]+)/$', views.admregedit, name='admregedit'),
    re_path(r'^admregistration/(?P<reg_id>[0-9]+)/$', views.admregistration, name='admregistration'),
    re_path(r'^admintimetable/$', views.admintimetable, name='admintimetable'),
    re_path(r'^admin_view_timetable/$', views.admin_view_timetable, name='admin_view_timetable'),
    re_path(r'^admin_edit_timetable/(?P<i_id>[0-9]+)/$', views.admin_edit_timetable, name='admin_edit_timetable'),
    re_path(r'^admin_editpage/(?P<timet_id>[0-9]+)/$', views.admin_editpage, name='admin_editpage'),
    re_path(r'^delete_batch/(?P<p_id>[0-9]+)/$', views.delete_batch, name='delete_batch'),
    re_path(r'^admin_userpayment/$', views.admin_userpayment, name='admin_userpayment'),
    re_path(r'^admin_payment/$', views.admin_payment, name='admin_payment'),
    re_path(r'^admin_pay_page/$', views.admin_pay_page, name='admin_pay_page'),
    re_path(r'^Trainee_logout/$', views.Trainee_logout, name='Trainee_logout'),
    re_path(r'^Trainer_logout/$', views.Trainer_logout, name='Trainer_logout'),
    re_path(r'^SuperAdmin_logout/$', views.SuperAdmin_logout, name='SuperAdmin_logout'),

]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)