import os

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import PyPDF2

class DataHandler(object):
    """
    Given data from different sources, DataConverter converts and reformats the data so
    that it can be used in training a neural network. The data can be saved as csv file or
    returned as a pandas DataFrame.

    File naming convention: Element/Compound_lower temperature_upper temperature_temperature increment
    The temperatures in the filenames are in K for all elements.

    Table 1:
    -----------------------------
    Data Source     | File format
    -----------------------------
    FactSage        | .txt
    """

    def __init__(self, source, input_file):
        """

        :param source: name string of the data source as listed in Table 1.
        :param input_file: absolute path of input file
        """
        super(DataHandler, self).__init__()

        # Specify error prompts
        self.errors = {'ERR_VIS_1': '*** ERR_VIS_1: Entered column name is not a valid column name *** ',
                       'ERR_SRC_1': '*** ERR_SRC_1: Entered source is not a valid source ***',
                       'ERR_SRC_2': '*** ERR_SRC_2: Entered file could not be opened ***',
                       'ERR_SVE_1': '*** ERR_SVE_1: Table could not be saved because input_file is None',
                       'ERR_SVE_2': '*** ERR_SVE_2: Table could not be saved because source is None'}

        self.input_file = input_file
        self.table = None
        self.source = None
        self.possible_sources = [None, 'FactSage', 'Barin', 'Dorog']
        self.set_source(source)
        self.id_figure = 1  # Used in specifying a new plot
        self.max_suffix = 1

        if self.source is not None and self.input_file is not None:
            self.load_table()

    def convert(self, set_table=True):
        """
        Loads and converts the table

        :param set_table: If set table is True, the converted table will be saved to self.table.
        If set_table is False, the converted table is returned as pandas DataFrame. This is because
        converting table might be necessary without wanting to change self.table
        :return:
        """

        def text_from_pdf(pdf_file, page_number):
            # creating a pdf file object
            pdf_file_obj = open(pdf_file, 'rb')

            # creating a pdf reader object
            pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)

            # creating a page object
            page_obj = pdf_reader.getPage(page_number)

            return page_obj.extractText()

        # Treat different sources differently
        if self.source == 'FactSage':
            # Extract the data and temporarily save every column in the txt file into a list
            first = True
            end_of_block = False
            header = 5
            table_dict = dict()
            save_data = []
            column_names = []

            phase_translation = {'Fe': {'S1': 'bcc', 'S2': 'fcc', 'L1': 'liquid', 'G1': 'gas'}}

            element = self.get_element_from_file()

            def table_to_dict():
                """
                Takes the column names and the extracted data and makes a dict of it (easy to convert
                into pd.DataFrame)
                :return:
                """
                for i, c in enumerate(column_names):
                    table_dict[c] = save_data[i]

            with open(self.input_file, 'r') as f:
                for i, line in enumerate(f):
                    #print(i, 'O', line)
                    if i < header:
                        continue

                    # FactSage tables are separated by a row of '_'. If such row occurs, save
                    # the data obtained priorly and reinitialize save_data.
                    if '_' in line:
                        # If the '_' row occurs the first time, there is nothing yet to save
                        if not first and not end_of_block:
                            table_to_dict()
                        elif first:
                            first = False

                        # The row of '_' marks the end and the beginning of a block. In between
                        # are the column names. To extract those, end_of_block marks which row
                        # of '_' was found (first or second).
                        end_of_block = not end_of_block
                    # If end_of_block is True, the line is in between two lines of '_'. Then,
                    # the column names can be extracted.
                    elif end_of_block:
                        # Get the column names
                        column_names = list(filter(lambda v: v != '', line.strip().split(' ')))
                        # Get the phase (first entry in column_names) and remove it
                        phase = column_names[0]
                        column_names.remove(phase)
                        # If there is a different name for the phase, translate it
                        if element in phase_translation:
                            phase = phase_translation[element][phase]
                        # Append _phase to every column name to make it unique (there might be other phases)
                        column_names = [c + '_' + phase for c in column_names]

                        # Reset save_data
                        save_data = []
                        for _ in range(len(column_names)):
                            save_data.append([])
                    # If an empty line is found, EOF is reached
                    elif line.isspace():
                        # If EOF is reached, table_dict is converted to a pandas DataFrame
                        table_to_dict()
                        table = pd.DataFrame(table_dict)
                        if set_table:
                            self.table = table
                        else:
                            return table
                    # Extract the numeric values
                    else:
                        # Get the values
                        values = list(filter(lambda v: v != '' and not v.isalpha(), line.strip().split(' ')))
                        for i_data in range(len(save_data)):
                            save_data[i_data].append(float(values[i_data]))

    def get_element_from_file(self):
        """
        Extracts the element from the filename. The element is always specified as the string after the first '_'
        in the filename
        :return: String of element abbreviation
        """

        return self.input_file.split('_')[1]

    def save_table(self, output_filename=None):
        """

        :param output_filename: Specifies where to save the table at. Is None by default, which indicates
        that the file should be saved in the standard location defined in code
        :return:
        """

        if output_filename is None:
            # Check if there is an input file specified and ask for one if not
            if self.input_file is None:
                print(self.errors['ERR_SVE_1'])
                self.input_file = input('Please enter absolute input file path: ')
            # Check if there is a source specified and ask for one if not
            if self.source is None:
                print(self.errors['ERR_SVE_2'])
                self.set_source(-1)

            directory = r"C:\Users\danie\Documents\Montanuni\Masterarbeit\4 Daten\Preprocessed" + "\\" \
                        + self.get_element_from_file()
            filename = os.path.basename(self.input_file).split('.')[0] + '.xlsx'
            self.table.to_excel(os.path.join(directory, filename), index=False)
        else:
            self.table.to_excel(output_filename, index=False)

    def get_x_y_data(self, x_col, y_col):
        """

        :param x_col: column name of x values
        :param y_col: column name of y values
        :return: x, y data as numpy arrays
        """

        self.load_table()

        # Extract the x and y data
        x = np.array(self.table[x_col])
        y = np.array(self.table[y_col])

        return x, y

    def get_column_names(self):
        self.load_table()

        cols = self.table.columns
        print(list(cols))

        return cols

    def set_input_file(self, new_input_file):
        # Check if file exists
        if os.path.isfile(new_input_file):
            self.input_file = new_input_file
        else:
            print(self.errors['ERR_SRC_2'])
            while not os.path.isfile(new_input_file):
                new_input_file = input('Enter file path: ')
            self.input_file = new_input_file

    def set_source(self, new_source):
        # Check if source is valid
        while new_source not in self.possible_sources:
            print(self.errors['ERR_SRC_1'])
            print('Source entered: ', new_source, ' Please select one of the following sources:')
            print(self.possible_sources)
            new_source = input('Enter source: ')
        self.source = new_source

    def show_table(self, n_cols=5):
        self.load_table()

        print(self.table.head(n_cols))

    def load_table(self):
        # If no table was opened yet, open the one specified
        if self.table is None:
            # If the input file is a .txt file from FactSage, it needs to be converted first
            filename, file_extension = os.path.splitext(self.input_file)
            if self.source == 'FactSage' and file_extension.lower() == '.txt':
                self.convert()
            elif file_extension.lower() == '.csv':
                self.table = pd.read_csv(self.input_file)
            elif file_extension.lower() == '.xlsx':
                self.table = pd.read_excel(self.input_file)

    def change_table(self, source, input_file):
        self.set_source(source)
        self.set_input_file(input_file)
        self.load_table()

    def plot_data(self, x_col, y_col, title=None, axis_labels=None):
        """
        Plots the data from the table
        :param x_col: Column name for the x-values
        :param y_col: Column name for the y-values
        :param title: Plot title
        :param axis_labels: Axis labels in the format (x_label, y_label)
        :return:
        """

        self.load_table()

        plt.figure(self.id_figure)
        self.id_figure += 1

        def check_column_name(col_name):
            new_col = col_name
            while col_name not in self.table.columns:
                print(self.errors['ERR_VIS_1'])
                print('Please enter valid column name! Valid columns:')
                self.get_column_names()
                new_col = input('Enter column name: ')

            return new_col

        # Check x_col
        x_col = check_column_name(x_col)

        # Check y_col(s) and plot the data
        if type(y_col) == list:
            for i, col in enumerate(y_col):
                y_col[i] = check_column_name(col)
                x, y = self.get_x_y_data(x_col, y_col[i])
                plt.scatter(x, y, s=1)
                plt.legend(y_col)
        else:
            y_col = check_column_name(y_col)
            x, y = self.get_x_y_data(x_col, y_col)
            plt.scatter(x, y, s=1)

        # Beautify the plot
        plt.grid(b=True)
        if title is not None:
            plt.title(title)
        else:
            plt.title(None)

        if axis_labels is not None:
            plt.xlabel(axis_labels[0])
            plt.ylabel(axis_labels[1])
        else:
            plt.xlabel(None)
            plt.ylabel(None)

    def plot_gibbs(self, temp_col_name=None):
        """
        Makes a full plot of all the Gibbs energies for all phases over the temperature
        :param temp_col_name: Specifies a column which includes temperature values
        :return:
        """

        self.plot_variable_over_temp('G(J)', plot_title='Gibbs energy vs temperature', temp_col_name=None)

    def plot_enthalpy(self, temp_col_name=None):
        """
        Makes a full plot of all the Enthalpies for all phases over the temperature
        :param temp_col_name: Specifies a column which includes temperature values
        :return:
        """

        self.plot_variable_over_temp('H(J)', plot_title='Enthalpy vs temperature', temp_col_name=None)

    def plot_heat_capacity(self, temp_col_name=None):
        """
        Makes a full plot of all the heat capacities for all phases over the temperature
        :param temp_col_name: Specifies a column which includes temperature values
        :return:
        """

        self.plot_variable_over_temp('Cp(J/K)', plot_title='Heat capacity vs temperature', temp_col_name=None)

    def plot_entropy(self, temp_col_name=None):
        """
        Makes a full plot of the Entropy for all phases over the temperature
        :param temp_col_name: Specifies a column which includes temperature values
        :return:
        """

        self.plot_variable_over_temp('S(J/K)', plot_title='Entropy vs temperature', temp_col_name=None)

    def plot_variable_over_temp(self, y_col_name: str, plot_title=None, temp_col_name=None):
        """
        Plots all columns where the name contains y_col_name (a thermodynamic variable like the
        Gibbs energy) over the temperature

        :param y_col_name: Specifies which columns shall be plotted. For example for plotting
        all Gibbs energy columns, col_name could be 'G(J)'
        :param plot_title: Specifies the title of the plot
        :param temp_col_name: Specifies a column which includes temperature values
        :return:
        """

        self.load_table()

        columns = self.table.columns

        # Find temperature column and all columns with Gibbs energies of different phases
        var_cols = []
        for col in columns:
            if 'T(K)' in col and temp_col_name is None:
                temp_col_name = col
            elif y_col_name in col:
                var_cols.append(col)

        # Don't plot, if no data is available
        if len(var_cols) == 0:
            print('No data available for ', y_col_name)
        else:
            self.plot_data(temp_col_name, var_cols, title=plot_title, axis_labels=('T(K)', y_col_name))

    def merge(self, other, keep_data_handler=True, keep_tables_unique=False):
        """

        :param keep_data_handler: If keep is True, the DataHandler from which merge was called will now
        be the merged DataHandler (it contains the merged DataFrame)
        :param other: DataHandler to merge with
        :param keep_tables_unique: If keep_tables_unique is False, columns with the same name but from different sources
        will be mixed into one column. If keep_tables_unique is True, the columns will be kept unique by adding suffixes
        to their column names (ascending index saved in self.max_suffix)
        :return: Merged DataHandler
        """

        if not keep_tables_unique:
            merged_table = self.table.append(other.table, ignore_index=True)
            merged_table = merged_table.sort_values(by=['T(K)_bcc'])
        else:
            left_table = self.table.add_suffix('_' + str(self.max_suffix))
            right_table = self.table.add_suffix('_' + str(self.max_suffix + 1))
            self.max_suffix += 2
            merged_table = left_table.append(right_table, ignore_index=True)

        if keep_data_handler:
            self.table = merged_table
            self.set_source(None)
            self.set_input_file(None)
        else:
            new_data_handler = DataHandler(None, None)
            new_data_handler.table = merged_table
            new_data_handler.max_suffix = self.max_suffix
            return new_data_handler


import numpy as np
from matplotlib import pyplot as plt
import pylab

file = open("predictions.txt")
numarray = []
while 1:
	line = file.readline()
	if not line:
		break
	numarray.append(float(line))

x = range(len(numarray))
plt.scatter(x,numarray)

plt.savefig("figure.pdf")


"""
Module for functions that analyse text
"""

import pandas as pd
from utils.text_preprocessing_utils import normalise, tokenise

def get_cluster(row, kmeans):
    return kmeans.predict(row.values.reshape(1, -1))[0]

def get_common_words(string):
    text_df = pd.DataFrame(normalise(tokenise(string)))
    text_df["count"] = 1
    grouped_df = text_df.groupby([0]).count()
    grouped_df = grouped_df.sort_values(["count"], ascending=False)
    grouped_df = grouped_df[:20]
    grouped_df.plot.bar()


# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QPushButton
from PyQt5.QtCore import pyqtSlot
import backend
import time
import threading
import os
import re
import subprocess
import SpotipyManager
import Excel

indexread = False
index = 0
startTime = 0
endTime = 0
song = ""
artist = ""

if os.name == "nt":
    import ctypes
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("spotifylyrics.version1")

class Communicate(QtCore.QObject):
    signal = QtCore.pyqtSignal(str, str)

class Ui_Form(object):
    sync = False
    ontop = False
    open_spotify = False

    if os.name == "nt":
        settingsdir = os.getenv("APPDATA") + "\\SpotifyLyrics\\"
    else:
        settingsdir = os.path.expanduser("~") + "/.SpotifyLyrics/"
    def __init__(self):
        super().__init__()

        self.comm = Communicate()
        self.comm.signal.connect(self.change_lyrics)
        self.setupUi(Form)
        self.set_style()
        Excel.setupExcelFormatting()
        self.load_save_settings()
        if self.open_spotify:
            self.spotify()
        self.start_thread()

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(550, 610)
        Form.setMinimumSize(QtCore.QSize(350, 310))
        self.gridLayout_2 = QtWidgets.QGridLayout(Form)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_songname = QtWidgets.QLabel(Form)
        self.label_songname.setObjectName("label_songname")
        self.label_songname.setOpenExternalLinks(True)
        self.horizontalLayout_2.addWidget(self.label_songname, 0, QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem)
        self.comboBox = QtWidgets.QComboBox(Form)
        self.comboBox.setGeometry(QtCore.QRect(160, 120, 69, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.horizontalLayout_2.addWidget(self.comboBox, 0, QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        self.fontBox = QtWidgets.QSpinBox(Form)
        self.fontBox.setMinimum(1)
        self.fontBox.setProperty("value", 10)
        self.fontBox.setObjectName("fontBox")
        self.horizontalLayout_2.addWidget(self.fontBox, 0, QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter)
        self.verticalLayout_2.addLayout(self.horizontalLayout_2)
        self.textBrowser = QtWidgets.QTextBrowser(Form)
        self.textBrowser.setObjectName("textBrowser")
        self.textBrowser.setAcceptRichText(True)
        self.textBrowser.setStyleSheet("font-size: %spt;" % self.fontBox.value() * 2)
        self.textBrowser.setFontPointSize(self.fontBox.value())
        self.verticalLayout_2.addWidget(self.textBrowser)
        self.gridLayout_2.addLayout(self.verticalLayout_2, 2, 0, 1, 1)
        #self.button = QtGui.QPushButton('Test', self)
        #self.button.clicked.connect(self.handleButton)
        self.retranslateUi(Form)
        self.fontBox.valueChanged.connect(self.update_fontsize)
        self.comboBox.currentIndexChanged.connect(self.optionschanged)

        self.button = QPushButton("&Erase all",Form)
        self.button.setToolTip('This is an example button')
        self.button.move(50,550)
        self.button.clicked.connect(Excel.eraseExcelData)

        QtCore.QMetaObject.connectSlotsByName(Form)
        Form.setTabOrder(self.textBrowser, self.comboBox)
        Form.setTabOrder(self.comboBox, self.fontBox)

    def load_save_settings(self, save=False):
        settingsfile = self.settingsdir + "settings.ini"
        if save is False:
            if os.path.exists(settingsfile):
                with open(settingsfile, 'r') as settings:
                    for line in settings.readlines():
                        lcline = line.lower()
                        if "syncedlyrics" in lcline:
                            if "true" in lcline:
                                self.sync = True
                            else:
                                self.sync = False
                        if "alwaysontop" in lcline:
                            if "true" in lcline:
                                self.ontop = True
                            else:
                                self.ontop = False
                        if "fontsize" in lcline:
                            set = line.split("=",1)[1].strip()
                            try:
                                self.fontBox.setValue(int(set))
                            except ValueError:
                                pass
                        if "openspotify" in lcline:
                            if "true" in lcline:
                                self.open_spotify = True
                            else:
                                self.open_spotify = False

            else:
                directory = os.path.dirname(settingsfile)
                if not os.path.exists(directory):
                    os.makedirs(directory)
                with open(settingsfile, 'w+') as settings:
                    settings.write("[settings]\nAlwaysOnTop=False\nFontSize=10\nOpenSpotify=False")
            if self.ontop is True:
                Form.setWindowFlags(Form.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
                self.comboBox.setItemText(2, ("Always on Top (on)"))
                Form.show()
            if self.open_spotify is True:
                self.comboBox.setItemText(4, ("Start spotify (on)"))
        else:
            with open(settingsfile, 'w+') as settings:
                settings.write("[settings]\n")
                if self.ontop is True:
                    settings.write("AlwaysOnTop=True\n")
                else:
                    settings.write("AlwaysOnTop=False\n")
                if self.open_spotify is True:
                    settings.write("OpenSpotify=True\n")
                else:
                    settings.write("AlwaysOnTop=False\n")
                settings.write("FontSize=%s" % str(self.fontBox.value()))

    def optionschanged(self):
        current_index = self.comboBox.currentIndex()
        if current_index == 1:
            if self.ontop is False:
                self.ontop = True
                Form.setWindowFlags(Form.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
                self.comboBox.setItemText(1, ("Always on Top (on)"))
                Form.show()
            else:
                self.ontop = False
                Form.setWindowFlags(Form.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
                self.comboBox.setItemText(1, ("Always on Top"))
                Form.show()
        elif current_index == 2:
            self.load_save_settings(save=True)
        elif current_index == 3:
            if self.open_spotify is True:
                self.open_spotify = False
                self.comboBox.setItemText(3, ("Open Spotify"))
            else:
                self.open_spotify = True
                self.comboBox.setItemText(3, ("Open Spotify (on)"))
        else:
            pass
        self.comboBox.setCurrentIndex(0)

    def set_style(self):
        if os.path.exists(self.settingsdir + "theme.ini"):
            themefile = self.settingsdir + "theme.ini"
        else:
            themefile = "theme.ini"
        if os.path.exists(themefile):
            with open(themefile, 'r') as theme:
                try:
                    for setting in theme.readlines():
                        lcsetting = setting.lower()
                        try:
                            set = setting.split("=",1)[1].strip()
                        except IndexError:
                            set = ""
                        if "windowopacity" in lcsetting:
                            Form.setWindowOpacity(float(set))
                        if "backgroundcolor" in lcsetting:
                            Form.setStyleSheet("background-color: %s" % set)
                        if "lyricsbackgroundcolor" in lcsetting:
                            style = self.textBrowser.styleSheet()
                            style = style + "background-color: %s;" % set
                            self.textBrowser.setStyleSheet(style)
                        if "lyricstextcolor" in lcsetting:
                            style = self.textBrowser.styleSheet()
                            style = style + "color: %s;" % set
                            self.textBrowser.setStyleSheet(style)
                        if "songnamecolor" in lcsetting:
                            style = self.label_songname.styleSheet()
                            style = style + "color: %s;" % set
                            self.label_songname.setStyleSheet(style)
                        if "fontboxbackgroundcolor" in lcsetting:
                            style = self.fontBox.styleSheet()
                            style = style + "background-color: %s;" % set
                            self.comboBox.setStyleSheet(style)
                            self.fontBox.setStyleSheet(style)
                        if "fontboxtextcolor" in lcsetting:
                            style = self.fontBox.styleSheet()
                            style = style + "color: %s;" % set
                            self.comboBox.setStyleSheet(style)
                            self.fontBox.setStyleSheet(style)
                        if "songnameunderline" in lcsetting:
                            if "true" in set.lower():
                                style = self.label_songname.styleSheet()
                                style = style + "text-decoration: underline;"
                                self.label_songname.setStyleSheet(style)
                except Exception:
                    pass
        else:
            self.label_songname.setStyleSheet("color: black; text-decoration: underline;")
            pass

    def resource_path(self, relative_path):
        try:
            base_path = sys._MEIPASS
        except Exception:
            base_path = os.path.abspath(".")
        return os.path.join(base_path, relative_path)

    def update_fontsize(self):
        self.textBrowser.setFontPointSize(self.fontBox.value())
        style = self.textBrowser.styleSheet()
        style = style.replace('%s' % style[style.find("font"):style.find("pt;") + 3], '')
        style = style.replace('p ', '')
        self.textBrowser.setStyleSheet(style + "p font-size: %spt;" % self.fontBox.value() * 2)
        lyrics = self.textBrowser.toPlainText()
        self.textBrowser.setText(lyrics)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Spotify Lyrics - {}".format(backend.version())))
        Form.setWindowIcon(QtGui.QIcon(self.resource_path('icon.png')))
        self.textBrowser.setText(_translate("Form", "I don't think Spotify is open")) ##THIS Writes stuff
        self.fontBox.setToolTip(_translate("Form", "Font Size"))
        self.comboBox.setItemText(0, _translate("Form", "Options"))
        self.comboBox.setItemText(1, _translate("Form", "Always on Top"))
        self.comboBox.setItemText(2, _translate("Form", "Save Settings"))
        self.comboBox.setItemText(3, _translate("Form", "Open Spotify"))

    def newSong(self, songname):
        Excel.manageIndex(songname)

        Excel.writeNewSongToFile(songname)

    def lyrics_thread(self, comm):
        global song
        global artist
        oldsongname = ""
        style = self.label_songname.styleSheet()
        if style == "":
            color = "color: black"
        else:
            color = style
        while True:
            songname = backend.getwindowtitle()
            if oldsongname != songname and songname != "":
                print("Changed!")
                self.newSong(songname)
            oldsongname = songname

            print("Playing: " + song)

            song, artist = backend.getSongData(songname)

            comm.signal.emit(songname, "Playing song " + song + " by " + artist)
            if songname != "Spotify" and songname != "": #when you switch songs

                lyrics, url, timed = backend.getlyrics(songname)
                if url == "":
                    header = songname
                else:
                    header = '''%s''' % (color, url, songname)
            time.sleep(1)

    def start_thread(self):
        lyricsthread = threading.Thread(target=self.lyrics_thread, args=(self.comm,))
        lyricsthread.daemon = True
        lyricsthread.start()

    def change_lyrics(self, songname, lyrics):
        _translate = QtCore.QCoreApplication.translate
        self.label_songname.setText(_translate("Form", songname))
        self.textBrowser.setText(_translate("Form", lyrics))
        self.textBrowser.scrollToAnchor("#scrollHere")

    def spotify(self):
        if os.name == "nt":
            path = os.getenv("APPDATA") + '\Spotify\Spotify.exe'
            subprocess.Popen(path)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    Form.show()
    sys.exit(app.exec_())


from docx import Document
from log import logger

def read_sheets_from_template(template_file):
    wordDoc = Document(template_file)
    tables = wordDoc.tables
    sheets = []

    for t_idx, table in enumerate(tables):
        sheet = []

        for r_idx, row in enumerate(table.rows):
            row_data = []
            for c_idx, cell in enumerate(row.cells):
                if r_idx == 0:
                    if len(cell.text) >= 0:
                        row_data.append(cell.text)
                else:
                    row_data.append(cell.text)
            if len(row_data) > 0:
                sheet.append(row_data)
        if len(sheet) > 0:
            sheets.append(sheet)
    print(sheets)
    return sheets

def write_to_docx(sheets,docx_template):
    wordDoc = Document(docx_template)
    # para = wordDoc.add_paragraph()
    tables = wordDoc.tables
    sStr2 = "Tr"
    for t_idx, table in enumerate(tables):
        if table is not None:
            for r_idx, row in enumerate(table.rows):
                if row is not None:
                    for c_idx, cell in enumerate(row.cells):
                        if r_idx >= 0 and c_idx >= 0 and len(cell.text) >= 0:
                            try:
                                if cell is not None:
                                    cell.text = str(sheets[t_idx][r_idx][c_idx])
                                    # <----remove flag string --->
                                    sStr1 =cell.text
                                    # print(sStr1)
                                    if sStr2 in sStr1:
                                        nPos = sStr1.index(sStr2)
                                        cell.text=sStr1[0:nPos]
                                        # print (nPos)
                                    # <--------------------------->
                            except Exception as err:
                                logger.error(
                                    "t_idx  " + str(t_idx) + "   r_idx  " + str(r_idx) + "    c_idx  " + str(c_idx))
                                logger.error(err)

    wordDoc.save(docx_template)

if __name__ == '__main__':

    sheets1 = read_sheets_from_template("../docx/test1.docx")
    write_to_docx(sheets1,"../docx/test1.docx")


import torch
import lightly
import torchvision
from typing import Tuple
from pl_bolts.models.autoencoders.components import DecoderBlock, ResNetDecoder, DecoderBottleneck

def make_resnet_backbone(
        backbone_in_ch: int,
        backbone_type: str) -> torch.nn.Module:
    """ Simple wrapper """
    return make_resnet_backbone_large_resolution(
        backbone_in_ch=backbone_in_ch,
        backbone_type=backbone_type,
        pretrained=True)

def make_resnet_backbone_large_resolution(
        backbone_in_ch: int,
        backbone_type: str,
        pretrained: bool = True) -> torch.nn.Module:
    """
    This is based on torchvision standard implementation of resnet for large resolutions.
    The spatial resolution is decreased by a factor of 16.
    For example a 64x64 input is reduced to a 4x4 outputs (before torch.nn.AdaptiveAvgPool2d)
    The input resolution must be a multiple of 16. The minimum resolution of the input is 32x32.
    For example: 32, 48, 64, 80, 96, ...
    """
    if backbone_type == 'resnet18':
        net = torchvision.models.resnet18(pretrained=pretrained)
    elif backbone_type == 'resnet34':
        net = torchvision.models.resnet34(pretrained=pretrained)
    elif backbone_type == 'resnet50':
        net = torchvision.models.resnet50(pretrained=pretrained)
    else:
        raise Exception("Invalid enc_dec_type. Received {0}".format(backbone_type))

    first_conv_out_channels = list(net.children())[0].out_channels
    new_net = torch.nn.Sequential(
        torch.nn.Conv2d(
            backbone_in_ch,
            first_conv_out_channels,
            kernel_size=(3, 3),
            stride=(1, 1),
            padding=(1, 1),
            bias=False,
        ),
        *list(net.children())[1:-2],  # I am excluding the last (fc) layer and AveragePool2D
        torch.nn.AdaptiveAvgPool2d(1),  # adding adaptive pooling
        torch.nn.Flatten(start_dim=1)  # adding flattening
    )
    return new_net

def make_resnet_backbone_tiny_resolution(
        backbone_in_ch: int,
        backbone_type: str) -> torch.nn.Module:
    """
    This is based on lightly reimplementation of resnet for small resolutions.
    The spatial resolution is decreased by a factor of 8.
    For example a 16x16 input is reduced to a 2x2 outputs (before torch.nn.AdaptiveAvgPool2d)
    The input resolution must be a multiple of 8. The minimum resolution of the input is 16x16.
    For example: 16, 24, 32, 40, 48, ...
    """

    if backbone_type == 'resnet18':
        net = lightly.models.resnet.ResNetGenerator(name='resnet-18', num_classes=10)
    elif backbone_type == 'resnet34':
        net = lightly.models.resnet.ResNetGenerator(name='resnet-34', num_classes=10)
    elif backbone_type == 'resnet50':
        net = lightly.models.resnet.ResNetGenerator(name='resnet-50', num_classes=10)
    else:
        raise Exception("backbone_type not recognized. Received ->", backbone_type)

    first_conv_out_channels = list(net.children())[0].out_channels

    new_net = torch.nn.Sequential(
        torch.nn.Conv2d(
            backbone_in_ch,
            first_conv_out_channels,
            kernel_size=(3, 3),
            stride=(1, 1),
            padding=(1, 1),
            bias=False,
        ),
        *list(net.children())[1:-1],  # note that I am excluding the last_fc_layer.
        torch.nn.AdaptiveAvgPool2d(1),  # adding adaptive pooling
        torch.nn.Flatten(start_dim=1)  # adding flattening
    )
    return new_net

def make_vae_encoder_backbone_from_resnet(in_channels: int, resnet_type: str):
    if resnet_type == 'resnet18':
        net = torchvision.models.resnet18(pretrained=True)
    elif resnet_type == 'resnet34':
        net = torchvision.models.resnet34(pretrained=True)
    elif resnet_type == 'resnet50':
        net = torchvision.models.resnet50(pretrained=True)
    else:
        raise Exception("Invalid enc_dec_type. Received {0}".format(resnet_type))

    first_conv_out_channels = list(net.children())[0].out_channels
    encoder_backbone = torch.nn.Sequential(
        torch.nn.Conv2d(
            in_channels,
            first_conv_out_channels,
            kernel_size=(7, 7),
            stride=(2, 2),
            padding=(3, 3),
            bias=False,
        ),
        *list(net.children())[1:-2],  # note that I am excluding the last (fc) layer and the average_pool2D
    )
    return encoder_backbone

def make_vae_decoder_backbone_from_resnet(resnet_type: str):
    if resnet_type == 'resnet18':
        net = ResNetDecoder(DecoderBlock, [2, 2, 2, 2], latent_dim=1,
                            input_height=1, first_conv=True, maxpool1=True)
        backbone_dec = torch.nn.Sequential(*list(net.children())[1:-3])  # remove the first and last layer
    elif resnet_type == 'resnet34':
        net = ResNetDecoder(DecoderBlock, [3, 4, 6, 3], latent_dim=1,
                            input_height=1, first_conv=True, maxpool1=True)
        backbone_dec = torch.nn.Sequential(*list(net.children())[1:-3])  # remove the first and last layer
    elif resnet_type == 'resnet50':
        net = ResNetDecoder(DecoderBottleneck, [3, 4, 6, 3], latent_dim=1,
                            input_height=1, first_conv=True, maxpool1=True)
        backbone_dec = torch.nn.Sequential(*list(net.children())[1:-3])  # remove the first and last layer
    else:
        raise NotImplementedError

    return backbone_dec

def make_vae_encoder_backbone_from_scratch(in_channels: int, hidden_dims: Tuple[int]):
    modules = []
    ch_in = in_channels
    for h_dim in hidden_dims:
        modules.append(
            torch.nn.Sequential(
                torch.nn.Conv2d(
                    in_channels=ch_in,
                    out_channels=h_dim,
                    kernel_size=(3, 3),
                    stride=(2, 2),
                    padding=1),
                torch.nn.BatchNorm2d(h_dim),
                torch.nn.LeakyReLU())
        )
        ch_in = h_dim
    encoder_backbone = torch.nn.Sequential(*modules)
    return encoder_backbone

def make_vae_decoder_backbone_from_scratch(hidden_dims: Tuple[int]):
    modules = []

    for i in range(len(hidden_dims) - 1):
        modules.append(
            torch.nn.Sequential(
                torch.nn.ConvTranspose2d(
                    in_channels=hidden_dims[i],
                    out_channels=hidden_dims[i + 1],
                    kernel_size=(3, 3),
                    stride=(2, 2),
                    padding=1,
                    output_padding=1),
                torch.nn.BatchNorm2d(hidden_dims[i + 1]),
                torch.nn.LeakyReLU())
        )
    decoder_backbone = torch.nn.Sequential(*modules)
    return decoder_backbone


from urllib import request, parse, error
import hmac
import hashlib
import json
from collections import namedtuple

API_KEY = ''

def _json_hook(d):
    return namedtuple('X', list(d.keys()))(*list(d.values()))

def p_object(data):
    return json.loads(data, object_hook=_json_hook).result

class CryptoPayments:
    url = 'https://www.coinpayments.net/api.php'

    def __init__(self, public_key, private_key, ipn_url):
        self.public_key = public_key
        self.private_key = private_key
        self.ipn_url = ipn_url
        self.format = 'json'
        self.version = 1

    def create_hmac(self, **params):
        """ Generate an HMAC based upon the url arguments/parameters

            We generate the encoded url here and return it to Request because
            the hmac on both sides depends upon the order of the parameters, any
            change in the order and the hmacs wouldn't match
        """
        encoded = parse.urlencode(params).encode('utf-8')
        return encoded, hmac.new(bytearray(self.private_key, 'utf-8'), encoded, hashlib.sha512).hexdigest()

    def request(self, request_method, **params):
        """The basic request that all API calls use

            the parameters are joined in the actual api methods so the parameter
            strings can be passed and merged inside those methods instead of the
            request method. The final encoded URL and HMAC are generated here
        """
        encoded, sig = self.create_hmac(**params)

        headers = {'hmac': sig}

        req = None
        if request_method == 'get':
            req = request.Request(self.url, headers=headers)
        elif request_method == 'post':
            headers['Content-Type'] = 'application/x-www-form-urlencoded'
            req = request.Request(self.url, data=encoded, headers=headers)

        try:
            response = request.urlopen(req)
            # status_code = response.getcode()
            response_body = response.read()
        except error.HTTPError as e:
            # status_code = e.getcode()
            response_body = e.read()

        return p_object(response_body)

    def create_transaction(self, params=None):
        """ Creates a transaction to give to the purchaser
            https://www.coinpayments.net/apidoc-create-transaction
        """
        if params is None:
            params = {}
        params.update({'cmd': 'create_transaction',
                       'ipn_url': self.ipn_url,
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_basic_info(self, params=None):
        """Gets merchant info based on API key (callee)
           https://www.coinpayments.net/apidoc-get-basic-info
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_basic_info',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def rates(self, params=None):
        """Gets current rates for currencies
           https://www.coinpayments.net/apidoc-rates
        """
        if params is None:
            params = {}
        params.update({'cmd': 'rates',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def balances(self, params=None):
        """Get current wallet balances
            https://www.coinpayments.net/apidoc-balances
        """
        if params is None:
            params = {}
        params.update({'cmd': 'balances',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_deposit_address(self, params=None):
        """Get address for personal deposit use
           https://www.coinpayments.net/apidoc-get-deposit-address
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_deposit_address',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_callback_address(self, params=None):
        """Get a callback address to receive info about address status
           https://www.coinpayments.net/apidoc-get-callback-address
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_callback_address',
                       'ipn_url': self.ipn_url,
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def create_transfer(self, params=None):
        """Not really sure why this function exists to be honest, it transfers
            coins from your addresses to another account on coinpayments using
            merchant ID
           https://www.coinpayments.net/apidoc-create-transfer
        """
        if params is None:
            params = {}
        params.update({'cmd': 'create_transfer',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def create_withdrawal(self, params=None):
        """Withdraw or masswithdraw(NOT RECOMMENDED) coins to a specified address,
        optionally set a IPN when complete.
            https://www.coinpayments.net/apidoc-create-withdrawal
        """
        if params is None:
            params = {}
        params.update({'cmd': 'create_withdrawal',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def convert_coins(self, params=None):
        """Convert your balances from one currency to another
            https://www.coinpayments.net/apidoc-convert
        """
        if params is None:
            params = {}
        params.update({'cmd': 'convert',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_withdrawal_history(self, params=None):
        """Get list of recent withdrawals (1-100max)
            https://www.coinpayments.net/apidoc-get-withdrawal-history
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_withdrawal_history',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_withdrawal_info(self, params=None):
        """Get information about a specific withdrawal based on withdrawal ID
            https://www.coinpayments.net/apidoc-get-withdrawal-info
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_withdrawal_info',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_conversion_info(self, params=None):
        """Get information about a specific withdrawal based on withdrawal ID
            https://www.coinpayments.net/apidoc-get-conversion-info
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_conversion_info',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def validate_mac(self, uuid, price, currency, test_hash):
        to_check = API_KEY + '_' + uuid + '_' + str(int(price * 100)) + currency
        computed_hash = hashlib.sha256(to_check).hexdigest()
        return computed_hash == test_hash


import time
import sys
import os
import math
import numpy as np
from scipy.optimize import NonlinearConstraint, minimize
from sklearn.preprocessing import PolynomialFeatures
import pickle
from collections import deque

N = 4
LAMBDA = 0.5
ALPHA = 0.1
BETA = 0.1
THETA =  2.1 * 10**-3
deltaT = 1

# Hyperparameters
GAMMA = 1

def f(b):
    return math.exp(-LAMBDA*b)

def VQ(theta, r):
    return 1 - np.exp(-theta*r)

class QOEOptimization:
    def __init__(self, abrList, Tp, windowLength=3):
        self.w = [500 for _ in range(4*Tp)]
        self.abrList = abrList
        self.initMLModels()
        self.Tp = Tp
        self.windowLength = windowLength

    def initMLModels(self):
        self.abrModels = {}
        for abr in self.abrList:
            if abr not in self.abrModels:
                with open("./Data/Models/{}.pickle".format(abr), "rb") as mf:
                    self.abrModels[abr] = pickle.load(mf)

    def playerABR(self, x, prev_bw, b, prev_r, abr):
        poly = PolynomialFeatures(degree=3)
        X = [x] + [pbw*1000 for pbw in prev_bw] + b + [prev_r]
        X = np.array(X).reshape(1, -1)
        x_poly = poly.fit_transform(X)

        y_pred = self.abrModels[abr].predict(x_poly)

        return max(y_pred, 0)

    def ABR(self, bw_list, buf_list, r, abr):
        poly = PolynomialFeatures(degree=3)
        X = list(bw_list) + list(buf_list) + [r]
        X = np.array(X).reshape(1, -1)
        x_poly = poly.fit_transform(X)

        y_pred = self.abrModels[abr].predict(x_poly)

        return min(max(y_pred[0], 0), 1000000)

    def QoE(self, bw_list, buf_list, prev_r, abr):
        return VQ(THETA, self.ABR(bw_list, buf_list, prev_r, abr)) - BETA * f(buf_list[0]) - abs(ALPHA*(VQ(THETA, self.ABR(bw_list, buf_list, prev_r, abr)) - VQ(THETA, prev_r)))

    def pairwiseSum(self, QoEs):
        pairwiseError = 0
        for i in QoEs:
            for j in QoEs:
                pairwiseError += abs(i-j)
        return pairwiseError

    def tpSumGen(self, t):
        def tpSum(x):
            x_vals = [x[self.Tp*n+t] for n in range(N)]
            return sum(x_vals)
        return tpSum

    # Should return pairwise sum
    def costFunction(self, x, inputs):
        # Build bandwidths deque
        bandwidths = {k: deque(maxlen=self.windowLength) for k in range(1, 5)}
        for k in range(1, 5):
            for bw in inputs[k]["bandwidth"]:
                bandwidths[k].append(bw*1000)

        # Build bitrates dict
        bitrates = {k: inputs[k]["bitrate"] for k in range(1, 5)}

        # Build buffer deque
        buffers = {k: deque(maxlen=self.windowLength) for k in range(1, 5)}
        for k in range(1, 5):
            for buf in inputs[k]["buffer"]:
                buffers[k].append(buf)

        # Calculate pairwise sum for prediction window
        totalPairwiseSum = 0
        totalSocialWelfare = 0
        for t in range(self.Tp):
            QoEs = []
            for n in range(N):
                QoEs.append(self.QoE(
                    bandwidths[n+1],
                    buffers[n+1],
                    bitrates[n+1],
                    self.abrList[n]))

                # Update inputs for next timestamp
                next_abr_for_buf = self.ABR(bandwidths[n+1], buffers[n+1], bitrates[n+1], self.abrList[n])
                if next_abr_for_buf != 0:
                    new_buf = buffers[n+1][0] + (bandwidths[n+1][0] / next_abr_for_buf) * deltaT
                else:
                    new_buf = max(0, buffers[n+1][0] - deltaT)
                # print("next aBR: ", next_abr_for_buf)
                # print("New buf: ", new_buf, buffers[n+1][0])
                # print("New bw: ", x[N*n+t])
                buffers[n+1].appendleft(new_buf)
                bandwidths[n+1].appendleft(x[self.Tp*n + t])
                bitrates[n+1] = self.ABR(bandwidths[n+1], buffers[n+1], bitrates[n+1], self.abrList[n])

            totalPairwiseSum += self.pairwiseSum(QoEs)
            totalSocialWelfare += sum(QoEs)

        totalSocialWelfare = -totalSocialWelfare / (N * self.Tp)
        return GAMMA*totalSocialWelfare + (1 - GAMMA)*totalPairwiseSum

    def runOneIteration(self, inputs):
        for k in ["W", 1, 2, 3, 4]:
            if k not in inputs:
                print("{} not in inputs".format(k))
                return []

        for i in range(1, 5):
            for k in ["bandwidth", "bitrate", "buffer"]:
                if k not in inputs[i]:
                    print("{} not in inputs[{}]".format(k, i))
                    return []
            if inputs[i]["bitrate"] < 0:
                inputs[i]["bitrate"] = 0
            inputs[i]["buffer"] = [max(buf, 0) for buf in inputs[i]["buffer"]]

        # Constraints
        nlc1 = [NonlinearConstraint(self.tpSumGen(t), int(inputs["W"][t]*1000), int(inputs["W"][t]*1000)) for t in range(self.Tp)]
        bounds = [(0, None) for _ in range(N*self.Tp)]

        # Objective Function
        sol = minimize(self.costFunction, self.w, args=(inputs), method='SLSQP', bounds=bounds, constraints=nlc1)

        self.w = [int(solx) for solx in sol.x]
        print("Optimizedzz: ", [self.w[self.Tp*n] for n in range(N)])
        print()

        return [self.w[self.Tp*n]/1000 for n in range(N)][::-1]

if __name__ == "__main__":
    qoe = QOEOptimization(["YouTube", "YouTube", "mpc", "mpc"], 3)
    inputs = {
        "W": [0.94457142, 1.11073432, 0.72179543],
        1: {
            "bandwidth": [0.1, 0.1, 0.1],
            "bitrate": 3000,
            "buffer": [4, 5, 6]
        },
        2: {
            "bandwidth": [0.1, 0.1, 0.1],
            "bitrate": 3000,
            "buffer": [4, 5, 6]
        },
        3: {
            "bandwidth": [0.5, 0.5, 0.5],
            "bitrate": 300,
            "buffer": [4, 5, 6]
        },
        4: {
            "bandwidth": [0.5, 0.5, 0.5],
            "bitrate": 300,
            "buffer": [4, 5, 6]
        }
    }
    print(qoe.runOneIteration(inputs))


from django.http import JsonResponse

def json_from_object(obj):
    data = {
        'id': obj.id
    }

    if hasattr(obj, 'name'):
        data['name'] = obj.name
    elif hasattr(obj, 'address'):
        data['address'] = obj.address
    else:
        data['name'] = str(obj)

    if hasattr(obj, 'phone'):
        data['phone'] = obj.percent
    return JsonResponse(data)


import cv2
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt

# 转为u8类型
def restore1(u, sigma, v, k):
    m = len(u)
    n = len(v)
    a = np.zeros((m, n))
    a = np.dot(u[:, :k], np.diag(sigma[:k])).dot(v[:k, :])
    # s1 =  np.size(u[:, :k])
    # s1+= np.size(np.diag(sigma[:k]))
    # s1+= np.size(np.diag(v[:k, :]))
    # s2 = np.size(a)
    # print("压缩率：",s1/s2)
    a[a < 0] = 0
    a[a > 255] = 255
    return np.rint(a).astype('uint8')

def SVD(frame, K=10):
    a = np.array(frame)
    # 由于是彩色图像，所以3通道。a的最内层数组为三个数，分别表示RGB，用来表示一个像素
    u_r, sigma_r, v_r = np.linalg.svd(a[:, :, 0])
    u_g, sigma_g, v_g = np.linalg.svd(a[:, :, 1])
    u_b, sigma_b, v_b = np.linalg.svd(a[:, :, 2])

    R = restore1(u_r, sigma_r, v_r, K)
    G = restore1(u_g, sigma_g, v_g, K)
    B = restore1(u_b, sigma_b, v_b, K)
    I = np.stack((R, G, B), axis=2)
    return I

if __name__ == "__main__":
    mpl.rcParams['font.sans-serif'] = [u'simHei']
    mpl.rcParams['axes.unicode_minus'] = False
    frame = cv2.imread("test.png", -1)
    I = SVD(frame, 40)
    plt.imshow(I)
    cv2.imwrite("out.png", I)


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

# In[3]:

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
import seaborn as sns

import scipy.stats as stats

# In[10]:

data=sns.load_dataset('iris')

# In[11]:

data.head()

# In[35]:

data.size

# In[6]:

data['sepal_length'].mean()

# In[10]:

data['sepal_length'].median()

# In[11]:

data['sepal_length'].mode()

# In[7]:

data['sepal_width'].mean()

# In[8]:

data['petal_length'].mean()

# In[9]:

data['petal_width'].mean()

# In[13]:

range_=max(data['sepal_length'])-min(data['sepal_length'])
range_

# In[14]:

q2=data['sepal_length'].quantile(0.50)
q2

# In[18]:

q1=data['sepal_length'].quantile(0.25)
q1

# In[19]:

q3=data['sepal_length'].quantile(0.75)
q3

# In[27]:

st=data['sepal_length'].std()
st

# In[22]:

si=data['sepal_length'].var()
si

# In[23]:

mn=data['sepal_length'].mean()

# In[24]:

mn

# In[30]:

coefficient_of_variation=st/mn*100

# In[31]:

coefficient_of_variation

# In[41]:

co=data.corr()
co

# In[32]:

from scipy.stats import zscore

# In[33]:

zscore(data['sepal_length'])

# In[34]:

sns.pairplot(data)
plt.show()

# In[36]:

sns.countplot(data['sepal_length'])
plt.show()

# In[38]:

sns.boxplot(data['sepal_length'])
plt.plot()

# In[39]:

plt.scatter(data['sepal_length'],data['sepal_width'])
plt.show()

# In[42]:

sns.heatmap(co)
plt.show()

# In[44]:

data.isnull().sum()

# In[49]:

sns.violinplot(data['sepal_length'])
plt.show()

# In[6]:

plt.hist(data['sepal_length'],bins=4)
plt.show()

# In[8]:

data.hist()

# In[52]:

sns.violinplot(x='petal_length',y='petal_length',hue='species',data=data)

# In[4]:

dt=pd.read_csv('titanictrain.csv')
dt

# In[6]:

dt.size

# In[8]:

a=pd.get_dummies(dt['Sex'])
a.head()#we have to add the columns

# In[13]:

pd.get_dummies(dt,columns=['Sex']).head()# it direct add the columns to the dataframe skipping the original one

# In[74]:

pd.merge(dt,a,on=dt.index)# adding the columns by merge method

# In[14]:

import sklearn
from sklearn.preprocessing import OneHotEncoder
hotencoder=OneHotEncoder()
encoded=hotencoder.fit_transform(dt.Sex.values.reshape(-1,1)).toarray()
encoded

# In[69]:

aa=pd.get_dummies(data,prefix='park',columns=['species'])
aa.head()

# In[75]:

from sklearn.preprocessing import StandardScaler

# In[76]:

std_scale=StandardScaler()
std_scale

# In[81]:

dt['age_stdscl']=std_scale.fit_transform(dt[['Age']])# it takes

# In[82]:

dt['age_stdscl']

# In[86]:

from sklearn.preprocessing import MinMaxScaler

# In[88]:

minmax_scale=MinMaxScaler()
minmax_scale

# In[89]:

dt['t']=minmax_scale.fit_transform(dt[['Age']])# it takes only numerical value

# In[90]:

dt['t']

# In[ ]:


from kinematics import Twist
from files import getEvents, getTeamsForEvent, getFilesForTeamForEvent
import sys
import matplotlib.pyplot as plt

# handle args
printout = False
if len(sys.argv) == 2:
    if sys.argv[1] == "p":
        printout = True

# Read file from STDIN
file = open("/dev/stdin").read()

# Split file to lines
lines = file.split("\n")[2:-1]

# Tracker for last datapoint
last = [0, 0, 0]  # L, R, t

# Plot trackers
data = []

# Print CSV header
print("left, right, dt")

# Parse each datapoint
for line in lines:

    # Convert line from CSV to datapoints
    x, y, t = line.split(",")

    # Convert datatype
    x, y, t = float(x), float(y), float(t)

    # Build a twist from current point, referencing the last
    d: Twist = Twist.fromTranslation(last[0], last[1], x, y)

    # Get wheelspeeds
    speeds = d.toWheelSpeeds()

    # Get dt
    dt = t - last[2]

    # Print the new datapoint
    if printout:
        print(
            f"{round(speeds[0] / 100, 2)}, {round(speeds[1] / 100, 2)}, {round(dt, 4)}")

    else:
        # Add to plot
        # data.append((speeds[0], speeds[1], dt))
        plt.plot(t, speeds[0], t, speeds[1])

    # Set last to current
    last[0] = speeds[0]
    last[1] = speeds[1]
    last[2] = t

plt.show()
# if not printout:


#!/usr/bin/env python
__author__ = 'uwe'

from sys import argv
from os import listdir, makedirs, system
from os.path import exists

from bc.eodata.beam_processing.conf.paths import gptProcessor, ECOHAM_graph_file
from utils.utilities import getDOY, ensureTrailingSlash, exit_on_empty_list
from nasa.modis.seadas_processing.conf.paths import modisL3_TSMBasePath, modisL3_ECOHAMBasePath
from nasa.modis.seadas_processing.conf.params import _site

def printUsage():
    print("Usage: ", argv[0], "<date>")
    print("where date is a string representing the date to process,")
    print("e.g. 20120607 for June 7, 2012.")

if len(argv) != 2:
    printUsage()
    exit(1)

back_date = argv[1]
if len(back_date)!=8:
    print("****************************")
    print("* date parameter malformed *")
    print("****************************")
    printUsage()
    exit(1)

_year  = back_date[:4]
_month = back_date[4:6]
_day   = back_date[6:]
_doy   = getDOY(_year, _month, _day)
print("Processing date " + back_date + " (DOY = " + str(_doy)+ ").")

modisL3_TSMPath  = ensureTrailingSlash(ensureTrailingSlash(ensureTrailingSlash(modisL3_TSMBasePath  + _year) + _month) + _day)
modisL3_ECOHAMPath  = ensureTrailingSlash(modisL3_ECOHAMBasePath  + _year)

for _path in [modisL3_TSMPath, modisL3_ECOHAMPath]:
    if not exists(_path):
        print("Making directory: ", _path, " ...")
        makedirs(_path)

try:
    srcList = listdir(modisL3_TSMPath)
except OSError:
    print("Cannot open ", modisL3_TSMPath+ "! Now exiting...")
    exit(1)
else:
    listSize = exit_on_empty_list(srcList)
    print(listSize)

# Liste bereinigen:
for a in range(listSize):
    for item in srcList:
        if not item.startswith('cb_' + _site + '_' + back_date) or not item.endswith('.dim'):
            srcList.remove(item)

listSize = exit_on_empty_list(srcList)
srcList.sort()

# reprojected L3 products,        Name e.g. cb_ns_20130603_eo_ecoham.dim
outputProductPath = modisL3_ECOHAMPath + 'cb_' + _site + '_' + back_date + '_eo_bc_lat_lon_ecoham.dim'

print(srcList, outputProductPath)

for item in srcList:
    reproj_processingCall = gptProcessor + ' ' + ECOHAM_graph_file + ' -Ssource=' + modisL3_TSMPath + item + ' -Pfile=' + outputProductPath
    system(reproj_processingCall)


import API
import requests
from users_manager import get_user_info
import json

class DataManager:
    """This class is responsible for talking to the Google Sheet."""
    def __init__(self):
        self.SHEETY_HEADERS = {
            'Authorization': API.sheety_auth
        }
        self.sheety_username = API.sheety_username
        self.prices_api_endpoint = f'https://api.sheety.co/{self.sheety_username}/flightDeals/prices'
        self.users_api_endpoint = f'https://api.sheety.co/{self.sheety_username}/flightDeals/users'

    def get_rows_prices_sheet(self) -> list:
        """Get all the data from the table. Return a list containing dict objects.
        [
            {
                "city": "Paris",
                "iataCode": "",
                "lowestPrice": 54,
                "id": 2
            },
            {
                "city": "Berlin",
                "iataCode": "",
                "lowestPrice": 42,
                "id": 3
            },
            ...
        ]"""
        response = requests.get(url=self.prices_api_endpoint, headers=self.SHEETY_HEADERS)
        return response.json()['prices']

    def update_iata(self, city_id: int, city_code: str):
        """Update the IATA codes of target city. Input the city as in ['id']

        Name of the record that needs changing must match the name of the key in the returned data"""
        print('.update_iata()')
        update_info = {
            'price': {
                'iataCode': city_code
            }
        }
        response = requests.put(url=f'{self.prices_api_endpoint}/{city_id}',
                                headers=self.SHEETY_HEADERS, json=update_info)
        print(f'Sheety response: {response.text}')

    def get_rows_users_sheet(self) -> list:
        """Get all the data from the table. Return a list containing dict objects.
        [{'firstName': 'Philip', 'lastName': 'Li', 'email': 'philip.test.python@gmail.com', 'id': 2}, {...}]"""
        response = requests.get(url=self.users_api_endpoint, headers=self.SHEETY_HEADERS)
        return response.json()['users']

    def add_new_user(self):
        """Add a new user to the Google sheet 'users'"""
        print('Adding a new user...')
        name_email: dict = get_user_info()
        body = {
            'user': {
                'firstName': name_email['first name'],
                'lastName': name_email['last name'],
                'email': name_email['email'],
            }
        }
        response = requests.post(url=f'https://api.sheety.co/{self.sheety_username}/flightDeals/users',
                                 headers=self.SHEETY_HEADERS, json=body)
        print(f'Sheety response: {response.text}')

# t1 = DataManager()
# t1.add_new_user()


import os
import shutil

import boto3

from data_refinery_common.enums import PipelineEnum
from data_refinery_common.logging import get_and_configure_logger
from data_refinery_common.models import Pipeline, ProcessorJob, Sample
from data_refinery_common.utils import get_env_variable
from data_refinery_workers.processors import utils

LOCAL_ROOT_DIR = get_env_variable("LOCAL_ROOT_DIR", "/home/user/data_store")
logger = get_and_configure_logger(__name__)

# Default to us-east-1 if the region variable can't be found
AWS_REGION = get_env_variable("AWS_REGION", "us-east-1")

# 100 is the maximum number of jobIds you can pass to a AWS Batch's
# describe_jobs.
DESCRIBE_JOBS_PAGE_SIZE = 100

batch = boto3.client("batch", region_name=AWS_REGION)

def _find_and_remove_expired_jobs(job_context):
    """Finds expired jobs and removes their working directories"""

    job_context["deleted_items"] = []

    items_to_delete = []
    jobs_to_check = []
    for item in os.listdir(LOCAL_ROOT_DIR):

        # There may be successful processors
        if "SRP" in item or "ERP" in item or "DRP" in item:
            sub_path = os.path.join(LOCAL_ROOT_DIR, item)
            for sub_item in os.listdir(sub_path):
                try:
                    sample = Sample.objects.get(accession_code=sub_item)
                    if sample.computed_files.count() == 0:
                        # This doesn't have any associated computed files - leave it be.
                        continue
                except Sample.DoesNotExist:
                    # Interesting. This shouldn't happen at all.
                    continue
                except Exception:
                    # We can't contact the DB right now, skip deletion.
                    continue

                try:
                    sub_item_path = os.path.join(sub_path, sub_item)
                    logger.debug(
                        "Janitor deleting " + sub_item_path, contents=str(os.listdir(sub_item_path))
                    )
                    shutil.rmtree(sub_item_path)
                    job_context["deleted_items"].append(sub_item_path)
                except Exception:
                    # This job is likely vanished. No need for this directory.
                    pass

        # Processor job working directories
        if "processor_job_" in item:

            # TX Index jobs are the only ones who are allowed to hang around
            # after their jobs are finished. They're marked with an _index in their path.
            if "_index" in item:
                continue

            # Okay, does this job exist?
            try:
                job = ProcessorJob.objects.get(id=item.split("processor_job_")[1])

                jobs_to_check.append(job)
            except ProcessorJob.DoesNotExist:
                # This job has vanished from the DB - clean it up!
                logger.error("Janitor found no record of " + item + " - why?")
                items_to_delete.append(item)

    for page_start in range(0, len(jobs_to_check), DESCRIBE_JOBS_PAGE_SIZE):
        try:
            page_end = page_start + DESCRIBE_JOBS_PAGE_SIZE
            page = jobs_to_check[page_start:page_end]

            job_ids = [job.batch_job_id for job in page if job.batch_job_id]
            batch_jobs = batch.describe_jobs(jobs=job_ids)["jobs"]

            running_ids = {job["jobId"] for job in batch_jobs if job["status"] == "RUNNING"}

            for job in page:
                if not job.batch_job_id or job.batch_job_id not in running_ids:
                    # Reconstitute the path we broke apart previously.
                    items_to_delete.append("processor_job_" + str(job.id))

        except Exception:
            # We're unable to connect to the DB or Batch right
            # now, so hold onto it for right now.
            logger.exception("Problem finding job record for " + item + " - why?")
            continue

    for item in items_to_delete:
        to_delete = LOCAL_ROOT_DIR + "/" + item
        logger.debug("Janitor deleting " + to_delete)
        # This job is likely vanished. Not a problem, it's go
        shutil.rmtree(to_delete, ignore_errors=True)
        job_context["deleted_items"].append(to_delete)

    job_context["success"] = True
    return job_context

def run_janitor(job_id: int) -> None:
    pipeline = Pipeline(name=PipelineEnum.JANITOR.value)
    job_context = utils.run_pipeline(
        {"job_id": job_id, "pipeline": pipeline},
        [utils.start_job, _find_and_remove_expired_jobs, utils.end_job],
    )
    return job_context


import numpy as np
import pandas as pd
import cv2
import os

from random import shuffle

""" WHAT'S THE FORMAT I WANT? METADATA (accident/no_accident)  SEPARATELY, PATHS TO IMAGES ELSEWHERE """

# Get the frames, videos, ... for the videos with accidents
acc_paths = []
acc_times = []

duos = []
i = 0 # Current output video
frame_opath = "/home/dd/arcelormittalvideos/accident_frames/final/"
with open("accident_times.txt", "r") as f:
    for line in f.readlines():
        vid, duration = line.strip().split(" ")

        # ENH: Also detect 1 class and 0 class types
        if duration in ["-1","many", "none", "0:00", "None"]: print("Skipping{}".format(vid));continue
        duos.append((vid, duration))

    # OK, we got the duos. Now shuffle and iterate again
    shuffle(duos)
    print("We have {} videos".format(len(duos)))
    for vid_idx, (vid, duration) in enumerate(duos):

        ipath = "/home/dd/arcelormittalvideos/accident{}".format(vid)
        if not os.path.exists(ipath): print("{} not existing".format(ipath))
        cap = cv2.VideoCapture(ipath)
        video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
        def getFrame(sec, i, vid, cls):
            cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
            hasFrames,image = cap.read()
            if hasFrames:
                print("Writing")
                this_o_path = frame_opath
                if vid_idx < len(duos) * 0.8:
                    subdir = "train"
                else:
                    subdir = "val"
                this_o_path = this_o_path + os.sep + subdir
                if not os.path.exists(this_o_path): os.mkdir(this_o_path)
                # Also add class
                this_o_path = this_o_path + os.sep + str(cls)
                if not os.path.exists(this_o_path): os.mkdir(this_o_path)

                cv2.imwrite(this_o_path + os.sep + vid +"_"+str(i)+"_"+str(cls)+".jpg", image)     # save frame as JPG file
            return hasFrames

        sec = 0
        frameRate = 1

        minutes, seconds = duration.split(":")
        total_seconds = int(minutes) * 60 + int(seconds)
        print(total_seconds)

        # Get before the accident
        success = True
        while success and sec < total_seconds:
            sec = sec + frameRate
            sec = round(sec, 2)
            i = i + 1
            success = getFrame(sec, i, vid, 0)

        # Get after the accident
        while success and sec < 2 * total_seconds:
            sec = sec + frameRate
            sec = round(sec, 2)
            i = i + 1
            success = getFrame(sec, i, vid, 1)

        cap.release()


import requests
import datetime
from environs import Env
from work_with_img import download_imgs

def fetch_epic(nasa_api_token):
    url = "https://api.nasa.gov/EPIC/api/natural"
    payload = {"api_key": nasa_api_token}
    response = requests.get(url, params=payload)
    response.raise_for_status()
    image_urls = []
    image_paths = []
    response_earth = response.json()
    for img_number, earth in enumerate(response_earth):
        image_name = earth["image"]
        image_date = datetime.datetime.fromisoformat(earth["date"])
        date_image_for_link = image_date.strftime("%Y/%m/%d")
        link_on_EPIC = f"https://api.nasa.gov/EPIC/archive/natural/{date_image_for_link}/png/{image_name}.png"
        image_urls.append(link_on_EPIC)
        filename = f"epic_{img_number}.png"
        image_paths.append(filename)
    download_imgs(image_urls, image_paths, payload)

def main():
    env = Env()
    env.read_env()
    nasa_api_token = env("NASA_API_TOKEN")
    fetch_epic(nasa_api_token)

if __name__ == "__main__":
    main()


from rich.panel import Panel
from rich.console import RenderableType, StyleType

from textual.app import App
from textual.reactive import Reactive
from textual.widgets import Button
from textual.events import Click

class ButtonBox(Button):

    def __init__(
        self,
        label: RenderableType,
        name: str | None = None,
        style: StyleType = "bold white",
        hover_style: StyleType = "bold white on dark_green",
        press_style: StyleType = "bold white on green",
        disabled_style: StyleType = "bold grey50",
        enabled: bool = True,
    ):
        super().__init__(label=label, name=name, style=style)
        self.hover_style = hover_style
        self.press_style = press_style
        self.disabled_style = disabled_style
        self.enabled = enabled

    mouse_over = Reactive(False)
    mouse_press = Reactive(False)

    def render(self) -> Panel:
        border_style = None
        if self.enabled:
            if self.mouse_over:
                if self.mouse_press:
                    style = self.press_style
                else:
                    style = self.hover_style
            else:
                style = self.button_style
        else:
            style = self.disabled_style
            border_style = "gray70"

        return Panel(self.label, style=style)

    def on_enter(self) -> None:
        self.mouse_over = True

    def on_leave(self) -> None:
        self.mouse_over = False
        self.mouse_press = False

    def on_mouse_down(self) -> None:
        self.mouse_press = True

    def on_mouse_up(self) -> None:
        self.mouse_press = False

    def disable(self) -> None:
        self.enabled = False

    def enable(self) -> None:
        self.enabled = True

    async def on_click(self, event: Click) -> None:
        if self.enabled:
            return await super().on_click(event)


import sys
import gzip
import json

infile = sys.argv[1]
vocabfile = sys.argv[2]
n = int(sys.argv[3])

with open(infile) as f:
    events = json.load(f)

with gzip.open(vocabfile) as f:
    vocab = json.load(f)
reverse_vocab = {j:i for i,j in vocab.items()}

queries = '''dog barked
glass fell
glass broke
kid fell
obama spoke
congress passed law
he gave book her
hogwarts closing
astronauts launched space_shuttle
john bought milk'''.split('\n')

for key in sorted(events):
    goodEvents = 0
    print(key, queries[int(key)])
    for event,PMI in sorted(events[key], key=lambda x:x[-1])[::-1]:
        if len(event[1]) != 1 or len(event[2]) > 1 or len(event[3]) > 1:
            continue

        predicate = reverse_vocab[event[0]]
        subjects = reverse_vocab[event[1][0]]
        objects = None
        if len(event[2]):
            objects = reverse_vocab[event[2][0]]
        iobjects = None
        if len(event[3]):
            iobjects = reverse_vocab[event[3][0]]

        print(subjects, predicate, objects, iobjects, PMI)
        goodEvents += 1

        if goodEvents > n:
            break


import gspread
from google.oauth2.service_account import Credentials

SCOPE = [
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/drive.file",
    "https://www.googleapis.com/auth/drive"
    ]

CREDS = Credentials.from_service_account_file('creds.json')
SCOPED_CREDS = CREDS.with_scopes(SCOPE)
GSPREAD_CLIENT = gspread.authorize(SCOPED_CREDS)
SHEET = GSPREAD_CLIENT.open('sweet_cake_for_all1')

def get_sales_data():
    """
     Get user input figures from the daily sales data
     a while loop will run until it get the valid data figure from the user
     which is a string separated by comma,
    """
    while True:
        print("please enter sales data from the last market day.")
        print("Data should be six figures, and separated by commas.")
        print("Example:50,55,65,40,34,40\n")

        data_str = input("Please enter your sales data here:\n")

        sales_data = data_str.split(",")

        if validate_data(sales_data):
            print("Data is valid!")
            break

    return sales_data

def validate_data(values):
    """
    Inside the try except statement, converts all string values into intgers.
    if strings cannot be converted into integer,raises value error,or values
    are not exactly 6 positive numbers.
    """
    try:
        [int(value)for value in values]
        if len(values) != 6:
            raise ValueError(
                f"6 values are required, you provided {len(values)}"
            )
        for value in values:
            if int(value) < 0:
                raise ValueError(
                    f"6 values should be greater than 0")
    except ValueError as e:
        print(f"Invalid data: {e}, please try again.\n")
        return False
    return True

def update_worksheet(data, worksheet):
    """
    receives a lsit of integers to be inserted to a worksheet
    update the specified worksheet with the data provided.
    """
    print(f"Updating {worksheet} worksheet...\n")
    worksheet_to_update = SHEET.worksheet(worksheet)
    worksheet_to_update.append_row(data)
    print(f"{worksheet} Work sheet updated successfully!\n")
    print(f"{worksheet} please click the below link to view the update\n")
    print(f"{worksheet} https://tinyurl.com/24wwjfce\n")

def calculate_surplus_data(sales_row):
    """
    Compare sales with stock and calculate for each value
    surplus is the difference of stock and sales
    positive surplus shows wastage
    negative surplus showes additional sales
    """
    print("Calculating surplus data...\n")
    stock = SHEET.worksheet("stock").get_all_values()
    stock_row = stock[-1]
    surplus_data = []
    for stock, sales in zip(stock_row, sales_row):
        surplus = int(stock) - sales
        surplus_data.append(surplus)
    return surplus_data

def get_last_5_entries_sales():
    """
    Collects columens of data from sales worksheet, collecting
    the last five entries for each cake and returns the data as
    list of lists
    """
    sales = SHEET.worksheet("sales")
    columns = []
    for ind in range(1, 7):
        column = sales.col_values(ind)
        columns.append(column[-5:])
    return columns

def calculate_stock_data(data):
    """
    calculate teh average stock for each item type adding 25%
    """
    print("Calculating stock data...\n")
    new_stock_data = []

    for column in data:
        int_column = [int(num) for num in column]
        average = sum(int_column)/5
        stock_num = average * 2.5
        new_stock_data.append(round(stock_num))
    return new_stock_data

def main():
    """
    Run all program function
    """
    data = get_sales_data()
    sales_data = [int(num) for num in data]
    update_worksheet(sales_data, "sales")
    new_surplus_data = calculate_surplus_data(sales_data)
    update_worksheet(new_surplus_data, "surplus")
    sales_columns = get_last_5_entries_sales()
    stock_data = calculate_stock_data(sales_columns)
    update_worksheet(stock_data, "stock")

if __name__ == "__main__":
    print("Welcome to Sweet Cake for all1 Data Automation!")
    main()


from . import utils
import json
import os.path

def set_single_val(val, unit=None,
                   special={'true': True, 'false': False, 'none': None, 'null': None}):
    """Set a single value from config input."""
    if isinstance(val, str):
        if val.lower() in special.keys():
            val = special[str(val).lower()]
        elif ',' in val:
            val = val.split(',')
        elif '.' in val or 'e' in val or 'E' in val:
            try:
                val = float(val)
            except ValueError:
                pass
        else:
            try:
                val = int(val)
            except ValueError:
                pass
    if isinstance(val, float):
        val = utils.convert_unit(val, unit)

    return val

default_config_files = [os.path.join(os.path.dirname(__file__), 'default_config.json'),
                        os.path.join(os.path.dirname(__file__), 'default_state.json')]

class planetConfig:
    """Initializes, reads in, updates and displays the configuration."""

    def __init__(self, planet, configFile):
        """Read in config file."""
        planet = planet.capitalize()
        self.planet = planet
        self.filename = configFile
        self.path = planet

        # Set defaults
        self.toks = {}
        for json_file in default_config_files:
            with open(json_file, 'r') as f:
                config_data = json.load(f)
            for tok, fval in config_data['toks'].items():
                self.toks[tok] = fval['name']
                pval = fval['default'][self.planet]
                if isinstance(pval, (str, float)):
                    pval = set_single_val(pval, fval['unit'])
                setattr(self, fval['name'], pval)
        self.setConfig(configFile)

    def __str__(self):
        """Return string."""
        return self.show()

    def setConfig(self, configFile):
        """
        Read in config files and update after defaults set in __init__.

        These are all shown in print(<instance>)
        """
        if configFile is None:
            print('Config file not provided.  Using defaults.')
            return 0
        try:
            fp = open(configFile, 'r')
        except IOError:
            print(configFile, ' not found.  Using defaults.')
            return 0

        for line in fp:
            if line[0] in utils.commentChars or len(line) < 4:
                continue
            if '#' in line:
                line = line[:line.index('#')]
            data = line.split()
            tok = data[0].lower()
            del(data[0])
            try:
                preval = getattr(self, self.toks[tok])
            except ValueError:
                print('token {} ({}) not found'.format(tok, self.toks[tok]))
                continue
            if isinstance(preval, (str, float, int)):
                unit = 'none'
                if len(data) == 2:
                    unit = data[1]
                if len(data):
                    val = set_single_val(data[0], unit)
                else:
                    print("{} didn't have an associated argument in config file.".format(tok))
                    print("Retaining  {}".format(preval))
                    continue
            elif isinstance(preval, list):
                if len(data) == 1 and ',' in data[0]:
                    data = data[0].split(',')
                val = [set_single_val(x) for x in data]
            elif isinstance(preval, dict):
                val = preval
                for i, v in enumerate(data):
                    if ':' in v:
                        frml = v.split(':')
                        val[frml[0].lower().strip()] = set_single_val(frml[1].lower().strip())
                    else:
                        val[v.strip()] = i
            else:
                print("Incorrect type:  {} <{}>".
                      format(tok, type(preval)))
                continue
            setattr(self, self.toks[tok], val)
        fp.close()

        if 'DZ' not in self.C.keys():
            self.C['DZ'] = len(self.C.keys())
        if 'DZ' not in self.Cl.keys():
            self.Cl['DZ'] = len(self.Cl.keys())

        try:
            fp = open(self.zonal, 'r')
            self.vwlat = []
            self.vwdat = []
            for line in fp:
                data = line.split()
                self.vwlat.append(float(data[0]))
                self.vwdat.append(float(data[1]))
            fp.close()
        except IOError:
            self.vwlat = [0.0, 90.0]
            self.vwdat = [0.0, 0.0]

    def update_config(self, key=None, value=None, **kwargs):
        """
        Update the config file in a variety of ways.

        Parameters
        ----------
        key : any
            If dict: updates based on key/value
            If list: updates assuming value is same length list
            If str: updates based on key, value
        value : any
            See above for key
        override : bool
            Allow for new config entry to be made.
        kwargs : key = val
        """
        proc_key = []
        proc_val = []
        if key is None:
            pass
        elif isinstance(key, dict):
            for k, v in key.items():
                proc_key.append(k)
                proc_val.append(v)
        elif not isinstance(key, list):
            proc_key.append(key)
            proc_val.append(value)
        else:
            if len(key) != len(value):
                print("key/value pairs not matched.")
            else:
                for k, v in zip(key, value):
                    proc_key.append(k)
                    proc_val.append(v)
        for k, v in kwargs.items():
            proc_key.append(k)
            proc_val.append(v)

        for k, v in zip(proc_key, proc_val):
            if k in self.toks.keys():
                tok = self.toks[k]
            else:
                tok = k
            try:
                preval = getattr(self, tok)  # noqa
            except ValueError:
                print("{} not in config keys and override is False".format(tok))
                continue
            setattr(self, tok, set_single_val(v))

    def show(self, print_it=True):
        """Return string containing configuration."""
        s = 'Run parameters:\n'
        keys = list(self.toks.keys())
        keys.sort()
        for key in keys:
            s += '\t{:20s}:  {}\n'.format(key, str(getattr(self, self.toks[key])))
        if print_it:
            print(s)
        return s


from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from .models import Order
from .api.serializers import OrderSerializer

@api_view(['GET', 'POST', 'DELETE'])
def order(request):
  if request.method == 'GET':
    ordersSerializer = OrderSerializer
    orders = Order.objects.all()
    return Response({"results": ordersSerializer(orders, many=True).data })

  if request.method == 'POST':
    serializer = OrderSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response(serializer.data, status=status.HTTP_201_CREATED)

@api_view(['DELETE', 'GET'])
def orderDetails(request, id):
  try:
    order = Order.objects.get(id=id)
  except Order.DoesNotExist:
    return Response(status=status.HTTP_404_NOT_FOUND)

  if request.method == 'GET':
    ordersSerializer = OrderSerializer
    return Response(ordersSerializer(order).data)

  if request.method == 'DELETE':
    order.delete()
    return Response(status=status.HTTP_200_OK)


from collections import deque
import sys

M, N = map(int ,sys.stdin.readline().split())

grid = []
for i in range(N):
    grid.append(list(map(int, sys.stdin.readline().split())))

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

q = deque([])

for i in range(N):
    for j in range(M):
        if grid[i][j] == 1:
            q.append([i, j])

def bfs():
    while q:
        x, y = q.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if nx >= 0 and nx < N and ny >= 0 and ny < M:
                if grid[nx][ny] == 0:
                    q.append([nx, ny])
                    grid[nx][ny] = grid[x][y] + 1

bfs()

result = 0

for i in range(N):
    if grid[i].count(0):
        print(-1)
        exit()
    else:
        result = max(result, max(grid[i]))

print(result - 1)


import sys
import math
import json
import multiprocessing as mp
import numpy as np

def genNsets_once(l_pars):
    #Read the parameters off the argument list l_pars
    i2i = l_pars[0]
    init_cond = l_pars[1]

    numEs = 1

    NUMES = str(numEs)

    INITCOND = str(init_cond)

    I2I = str(i2i)

    outfile = open("configure_files/nsets_Neuron_%si2i.isf"%I2I,"w")
    print("configure_files/nsets_Neuron_%si2i.isf"%I2I)

    np.random.seed(init_cond)
    #g_vs = n qp.random.random([numEs,8])
    #g_vs[:,0] = np.random.choice(np.arange(-80,-10,0.5))

    asympt = json.load(open("configure_files/asymptode_v_stellate_i2e-3.6.txt"))
    strToFile = ""

    for i in range(numEs):
        PMIN = -3.6
        TAURISE = str(np.random.choice(np.arange(100.0,300.0,1.0),1)[0])
        strToFile += "\"neuron %s\"dxdt:7,v:%s,m:%s,n:%s,h:%s,ms:%s,mhs:%s,mhf:%s,gh:1.5,I_syn:0.0,ampSin:0.0,ampNoise:0.008,freq:0.0,phase:0.0,V_th_Osc:-80,tau_fact:1.0,PulseDuration:10000,PulseStart:500,PulseEnd:9500,tau_rise:%s,tau_fall:20.0,PulseMax:%s,PulseMin:%s,I_PeriodicPulse:0.0,I_OscillatoryDrive:0.0, I_Na_Stellate_HR2005:0.0, I_K_Stellate_HR2005:0.0, I_Leak_Stellate_HR2005:0.0, I_NaP_Stellate_HR2005:0.0, I_Hs_Stellate_HR2005:0.0, I_Hf_Stellate_HR2005:0.0, ;\n\n"%(str(i),str(asympt["v"]),str(asympt["m"]),str(asympt["n"]),str(asympt["h"]),str(asympt["ms"]),str(asympt["mhs"]),str(asympt["mhf"]),TAURISE,I2I,PMIN)
    outfile.write(strToFile)
    outfile.close()

l_i2is = [-2.8,-2.75,-2.7,-2.65,-2.6]

cnt = 0

l_phases = [0]

for init_cond in [1]:
    for i2i in l_i2is:
        genNsets_once([i2i,init_cond])
        cnt += 1

print(cnt)


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import caption
import argparse
import torch
import json

class filedialogdemo(QWidget):

    def __init__(self, parent=None):
        super(filedialogdemo, self).__init__(parent)
        layout = QVBoxLayout()

        self.btn = QPushButton()
        self.btn.clicked.connect(self.loadFile)
        self.btn.setText("从文件中获取照片")
        layout.addWidget(self.btn)

        self.label = QLabel()
        layout.addWidget(self.label)

        # self.btn_2 = QPushButton()
        # self.btn_2.clicked.connect(self.load_text)
        # self.btn_2.setText("显示文本")
        # layout.addWidget(self.btn_2)

        self.content = QTextEdit()
        layout.addWidget(self.content)
        self.setWindowTitle("测试")

        self.setLayout(layout)

    def loadFile(self):
        print("load--file")
        fname, _ = QFileDialog.getOpenFileName(self, '选择图片', 'c:\\', 'Image files(*.jpg *.gif *.png)')
        print(fname)
        self.label.setPixmap(QPixmap(fname))

        defult_path = './BEST_checkpoint_flickr8k_5_cap_per_img_5_min_word_freq.pth'
        word_map_path = './WORDMAP_flickr8k_5_cap_per_img_5_min_word_freq.json'
        img_path = fname
        parser = argparse.ArgumentParser(description='Show, Attend, and Tell - Tutorial - Generate Caption')
        parser.add_argument('--img', '-i', default=img_path, help='path to image')
        parser.add_argument('--model', '-m', default=defult_path, help='path to model')
        parser.add_argument('--word_map', '-wm', default=word_map_path, help='path to word map JSON')
        parser.add_argument('--beam_size', '-b', default=5, type=int, help='beam size for beam search')
        parser.add_argument('--dont_smooth', dest='smooth', action='store_false', help='do not smooth alpha overlay')

        args = parser.parse_args()

        # Load model
        checkpoint = torch.load(args.model, map_location=str(caption.device))
        decoder = checkpoint['decoder']
        decoder = decoder.to(caption.device)
        decoder.eval()
        encoder = checkpoint['encoder']
        encoder = encoder.to(caption.device)
        encoder.eval()

        # Load word map (word2ix)
        with open(args.word_map, 'r') as j:
            word_map = json.load(j)

        # Encode, decode with attention and beam search
        seq, alphas = caption.caption_image_beam_search(encoder, decoder, args.img, word_map, args.beam_size)
        alphas = torch.FloatTensor(alphas)
        print(seq)
        result_str = ''
        for i in range(len(seq)):
            # if i == 0 or i == len(seq):
            #     continue
            result_str = result_str + ' ' + (list(word_map.keys())[seq[i] - 1])
        print(result_str)
        self.content.setText(result_str)

    def load_text(self):
        print("load--text")
        self.content.setText()
        dlg = QFileDialog()
        dlg.setFileMode(QFileDialog.AnyFile)
        dlg.setFilter(QDir.Files)
        if dlg.exec_():
            filenames = dlg.selectedFiles()
            f = open(filenames[0], 'r')
            with f:
                data = f.read()
                self.content.setText(data)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    fileload =  filedialogdemo()
    fileload.show()
    sys.exit(app.exec_())


import os
import argparse

from cbp_translate.pipeline import Config, run

if __name__ == "__main__":

    if os.getenv("MODAL_RUN_LOCALLY", None) != "1":
        raise RuntimeError(
            "This script can only be used locally, please set the environment variable MODAL_RUN_LOCALLY=1"
        )

    parser = argparse.ArgumentParser()
    parser.add_argument("--path-in", type=str, required=True)
    parser.add_argument("--path-out", type=str, required=True)
    parser.add_argument("--language", type=str, required=True)
    parser.add_argument("--path-tmp", type=str, default="/tmp")

    args = parser.parse_args()
    config = Config(target_lang=args.language, speaker_markers=True)

    run(
        path_in=args.path_in,
        path_out=args.path_out,
        path_tmp=args.path_tmp,
        config=config,
    )