#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 24 15:28:06 2021

@author: dhruv
"""
import numpy as np
import pandas as pd
import scipy.io
import neuroseries as nts
from pylab import *
import os, sys
from wrappers import loadSpikeData
from wrappers import loadXML
from wrappers import loadPosition
from wrappers import loadEpoch
from functions import *
import sys
import matplotlib.pyplot as plt

data_directory = '/media/DataDhruv/Recordings/B0800/B0801/B0801-211118'

session = ['B0801-211118', 'B0801-211119', 'B0801-211119A']
xparam = [0.05, 0.035, 0.035]
yparam = [0.06, 0.07, 0.07]
rth = 0.2

sessdata = pd.DataFrame(index = ['x', 'y'], columns = session, data = [xparam, yparam])
sessdata = sessdata.T

episodes = ['sleep', 'wake', 'sleep','wake', 'sleep']
events = ['1','3']

# episodes = ['sleep', 'wake', 'sleep']
# events = ['1']

spikes, shank = loadSpikeData(data_directory)
n_channels, fs, shank_to_channel = loadXML(data_directory)
position = loadPosition(data_directory, events, episodes)

wake_ep = loadEpoch(data_directory, 'wake', episodes)
sleep_ep = loadEpoch(data_directory, 'sleep')

xth = 0.05
yth = 0.06

print(xth, yth)

circle1 = plt.Circle((xth, yth), rth, color='k', fill = False)

freepos = position.restrict(wake_ep.iloc[[1]])
d = np.sqrt((freepos['x'].values - xth)**2 + (freepos['z'].values - yth)**2)
dy = freepos['z'].values - yth
dint = pd.DataFrame(index = freepos.index.values, columns = ['dist'], data = d)
dint['dy'] = dy
r1 = dint[dint['dist'] < rth]

start = [r1.index[0]]
stvals = [r1['dy'].values[0]]
end = []
evals = []

for i in range(len(r1)):
    if (r1.index.values[i] - r1.index.values[i-1]) > 1e6:
        end.append(r1.index.values[i-1])
        start.append(r1.index.values[i])
        stvals.append(r1['dy'].values[i])
        evals.append(r1['dy'].values[i-1])

start = np.array(start[0:-1])
stvals = np.array(stvals[0:-1])
end = np.array(end)
evals = np.array(evals)

tokeep = []

for i in range(len(stvals)):
    if (stvals[i] > 0 and evals[i] < 0):  #forward trials only
      # if (stvals[i] > 0 and evals[i] < 0) :
        tokeep.append(i)

# plt.figure()
# plt.plot(r1['dy'],'o-')
# plt.plot(start[tokeep], stvals[tokeep], "x")
# plt.plot(end[tokeep], evals[tokeep], "*")
# plt.show()

# fwd = nts.IntervalSet(start = start, end = end)

fwd = nts.IntervalSet(start = start[tokeep], end = end[tokeep])
# rev = nts.IntervalSet(start = start[tokeep], end = end[tokeep])

plt.figure()
plt.plot(freepos['x'],freepos['z'],'silver', zorder = 1)
plt.scatter(freepos['x'].restrict(fwd), freepos['z'].restrict(fwd), zorder = 2)
# plt.scatter(position['x'].restrict(rev), position['z'].restrict(rev), zorder = 2)
plt.gca().add_patch(circle1)

fwd.to_csv(data_directory + '/' + 'B0801-211118_vte.csv', index=False)

fwd = (fwd - wake_ep.iloc[1]['start']) / 1e6
# fwd = (fwd - wake_ep.iloc[1]['start']) / 1e6
# rev = (rev - wake_ep.iloc[1]['start']) / 1e6

fwd.to_csv(data_directory + '/' + 'B0801-211118_fwd.csv', index=False)
# rev.to_csv(path + '/' + name + '_rev.csv', index=False)


from packages import *

class ExperimentWizard(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Experiment set up Wizard")

        self.UiComponents()

        self.show()

        self.conditions_dic = {} # this is the output of the dialog window

    def UiComponents(self):
        self.central_widget = QWidget(self)
        self.setCentralWidget(self.central_widget)

        # first layout - conditions
        self.conditions_layout = QGridLayout()
        self.conditions_label = QLabel("Conditions:")

        ######################
        # CONDITIONS TYPES #
        ######################

        self.conditions_distractor_layout = QHBoxLayout()
        self.n_distractor_label = QLabel("Number of Distractors (use comma to separate values):")
        self.conditions_distractor_layout.addWidget(self.n_distractor_label)

        self.n_distractors_textbox = QLineEdit()
        self.n_distractors_textbox.setText("4, 8, 12, 32")
        self.n_distractors_textbox.setFixedSize(280, 40)
        self.conditions_distractor_layout.addWidget(self.n_distractors_textbox)

        self.conditions_movement_layout = QHBoxLayout()
        self.stimuli_fixed_radio = QRadioButton("Fixed stimuli")
        self.stimuli_scatter_radio = QRadioButton("Moving stimuli")
        self.conditions_movement_layout.addWidget(self.stimuli_fixed_radio)
        self.conditions_movement_layout.addWidget(self.stimuli_scatter_radio)

        self.conditions_layout.addWidget(self.conditions_label, 0, 0)
        self.conditions_layout.addLayout(self.conditions_distractor_layout, 1, 0)
        self.conditions_layout.addLayout(self.conditions_movement_layout, 2, 0)

        # second layout: stimuli characteristics
        self.stimuli_char_layout = QGridLayout()
        self.stimuli_char_label = QLabel("Stimuli Characteristics:")
        self.stimuli_char_layout.addWidget(self.stimuli_char_label, 0, 0)

        self.target_form = QHBoxLayout()
        self.distractor_form = QHBoxLayout()

        self.target_shape_combo = QComboBox(self)
        self.target_shape_combo.addItems(["", "triangle", "rectangle", "circle"])
        self.target_shape_combo.setFixedSize(280, 40)

        self.distractor_shape_combo = QComboBox(self)
        self.distractor_shape_combo.addItems(["", "triangle", "rectangle", "circle", "random"])
        self.distractor_shape_combo.setFixedSize(280, 40)

        self.target_color_combo = QComboBox(self)
        self.target_color_combo.addItems(["", "red", "blue"])
        self.target_color_combo.setFixedSize(280, 40)

        self.distractor_color_combo = QComboBox(self)
        self.distractor_color_combo.addItems(["", "red", "blue", "random"])
        self.distractor_color_combo.setFixedSize(280, 40)

        self.target_label = QLabel("Target")
        self.target_color_label = QLabel("Color")

        self.distractor_label = QLabel("Distractor(s)")
        self.distractor_color_label = QLabel("Color")

        target_form_list = [self.target_label, self.target_shape_combo, self.target_color_label, self.target_color_combo]

        for target_form_element in target_form_list:
            self.target_form.addWidget(target_form_element)

        distractor_form_list = [self.distractor_label, self.distractor_shape_combo, self.distractor_color_label
                                , self.distractor_color_combo]

        for distractor_form_element in distractor_form_list:
            self.distractor_form.addWidget(distractor_form_element)

        self.stimuli_char_layout.addLayout(self.target_form, 1, 0)
        self.stimuli_char_layout.addLayout(self.distractor_form, 2, 0)

        # third layout - trials specs
        self.loops_layout = QHBoxLayout(self)

        self.loops_label = QLabel("Trials' specifications:")
        self.trials_label = QLabel("Number of trials")
        self.target_presence = QLabel("Target's presence")

        self.trials_spinbox = QSpinBox(self)
        self.trials_spinbox.setFixedSize(80, 40)

        self.target_presence_spinbox = QSpinBox(self)
        self.target_presence_spinbox.setFixedSize(80, 40)

        # ADDING WIDGETS
        loops_layout_widgets = [self.loops_label, self.trials_label, self.trials_spinbox, self.target_presence,
                                self.target_presence_spinbox]

        for loops_layout_element in loops_layout_widgets:
            self.loops_layout.addWidget(loops_layout_element)

        # forth layout: input type
        self.input_layout = QVBoxLayout(self)
        self.input_label = QLabel("Keyboard keys (use comma to separate values)")
        self.input_layout.addWidget(self.input_label)

        self.keyboard_keys = QLineEdit()
        self.keyboard_keys.setText("y,n")
        self.keyboard_keys.setFixedSize(280, 40)
        self.input_layout.addWidget(self.keyboard_keys)

        self.submit_push = QPushButton("Submit")

        # main layout
        self.main_layout = QVBoxLayout(self.central_widget)

        self.layouts_list = [self.conditions_layout, self.stimuli_char_layout, self.loops_layout,
                        self.input_layout]
        for layout in self.layouts_list:
            self.main_layout.addLayout(layout)

        self.main_layout.addWidget(self.submit_push)
        self.central_widget.setLayout(self.main_layout)

        # Set some lists to be retrieved as output of the dialog window
        self.count = 0
        self.conditions_list = []
        self.children_list = []

        # Actions
        self.trials_spinbox.valueChanged.connect(self.update_max_value) # bound the value of 'target presence' to trials
        self.submit_push.clicked.connect(self.conditions_check)

    def update_max_value(self):
        value = self.trials_spinbox.value()
        self.target_presence_spinbox.setMaximum(value)

    def conditions_check(self):

        self.conditions_dic = {}

        if self.n_distractors_textbox.text() == "":
            self.displayError("Please insert the number of distractors")

        elif self.stimuli_fixed_radio.isChecked() == False and self.stimuli_scatter_radio.isChecked() == False:
            self.displayError("Please choose the movement of distractors")

        elif self.target_shape_combo.currentText() == "":
            self.displayError("Please choose a target's shape")

        elif self.target_color_combo.currentText() == "":
            self.displayError("Please choose the target's color")

        elif self.distractor_shape_combo.currentText() == "":
            self.displayError("Please choose the shape of distractors")

        elif self.distractor_color_combo.currentText() == "":
            self.displayError("Please choose the color of distractors")

        elif self.target_presence_spinbox.value() == 0:
            self.displayError("Please allow for the presentation of the target at least 1 time")

        elif self.trials_spinbox.value() == 0:
            self.displayError("Please choose the number of trials")

        elif self.keyboard_keys.text() == "":
            self.displayError("Please type some keyboard keys (e.g. 'y,n' "
                              "for 'yes' and 'no' respectively.")

        else:

            if self.stimuli_scatter_radio.isChecked():
                movement = "scatter"
            elif self.stimuli_fixed_radio.isChecked():
                movement = "fixed"

            # Display a message box summarising the conditions
            message = "Distractors number: {}\nStimuli's movement: {}\nTarget's shape: {}" \
                      "\nTarget's color: {}\nDistractors shape: {}\nDistractor's color: {}" \
                      "\nNumber of trials: {}\nof which the target is present: {}\n" \
                      "Keyboard's input keys: {}".format(self.n_distractors_textbox.text(),
                                                         movement, self.target_shape_combo.currentText(),
                                                         self.target_shape_combo.currentText(),
                                                         self.target_color_combo.currentText(),
                                                         self.distractor_shape_combo.currentText(),
                                                         self.distractor_color_combo.currentText(),
                                                         self.target_presence_spinbox.value(),
                                                         self.keyboard_keys.text())

            QMessageBox.information(self, "Experiment's Information", message)

            # POPULATE THE DICTIONARY -> OUTPUT OF THE WINDOW
            conditions_list = ["n_distractors", "movement", "target_shape", "target_color", "distractor_shape",
                               "distractor_color", "n_trials", "target_presence", "keyboard_keys"]

            values_list = [self.n_distractors_textbox.text(), movement, self.target_shape_combo.currentText(),
                          self.target_color_combo.currentText(), self.distractor_shape_combo.currentText(),
                          self.distractor_color_combo.currentText(), self.trials_spinbox.value(),
                          str(self.target_presence_spinbox.value()), self.keyboard_keys.text()]

            # the dict() method creates a dictionary, the lambda map function crosses the values within the two lists
            self.conditions_dic = dict(map(lambda i,j : (i,j) , conditions_list, values_list))

            self.close()

    # Small error window to be displayed if one of the conditions is not specified
    def displayError(self, message):
        error_dialog = QMessageBox()
        error_dialog.setIcon(QMessageBox.Warning)
        error_dialog.setText(message)
        error_dialog.setWindowTitle("Error")
        error_dialog.exec_()


from django.contrib.auth import get_user_model

from word.models import Word

gm = get_user_model()

Word.objects.create(
    user=gm.objects.first(),
    original_word='apple2',
    transcription='dsa',
    translation='dsadsa'
)


import numpy as np
import functools

def LinearRegression(data, validate = None, rk = 0):

    data = np.array(data)
    X = data[:,:-1]
    Y = data[:, -1]

    # add 1 to data to get ready for matrix operation
    X = np.hstack([X, np.ones((X.shape[0], 1))])

    # analytical solotion
    pX = X.T @ X + rk * np.eye(X.shape[1])
    pX = np.linalg.inv(pX)
    weight = pX @ X.T @ Y

    # predict on training set and compute error
    train_err = np.linalg.norm(X @ weight.T - Y) / X.shape[0]

    # predict on test set
    test_err = None
    if validate is not None:
        validate = np.array(validate)
        testX = validate[:,:-1]
        testX = np.hstack([testX, np.ones((testX.shape[0], 1))])
        testY = validate[:, -1]

        test_err = np.linalg.norm(testX @ weight.T - testY) / testX.shape[0]

    # return result
    result = {'w': weight,
              'train error': train_err,
              'test error': test_err}

    return result

def LogisticRegression(data, validate = None, lr = 0.05, rk = 0, max_iter = 1000):

    data = np.array(data)
    X = data[:,:-1]
    Y = data[:,-1:]
    n_components, n_features = X.shape

    # binarization
    threshold = 0.6 * np.max(Y)
    Y = np.where(Y >= threshold, 1, 0)

    # normalization
    Xmin = np.min(X, axis = 0)
    Xmax = np.max(X, axis = 0)
    X = (X - Xmin) / (Xmax - Xmin)

    # initialize para for gradient desent
    weight = np.ones((n_features, 1))
    bia = 1
    wx_b = X @ weight + bia

    # define useful func
    Sigmoid = lambda x: 1 / (1 + np.exp(-x))
    Ln_part = lambda x: np.log(1 + np.exp(x))

    # compute initial likelihood
    LLhood = np.mean(Y * wx_b - Ln_part(wx_b))
    LLhood += rk * np.sum(weight **2) / n_components  # regularization

    # apply gradient desent
    total_iter = max_iter
    for i in range(max_iter):

        dw = X.T @ (Sigmoid(wx_b) - Y) + rk * weight
        dw = dw / n_components
        db = np.sum(Sigmoid(wx_b) - Y) / n_components

        weight = weight - lr * dw
        bia = bia - lr * db

        wx_b = X @ weight + bia
        LLhood = np.mean(Y * wx_b - Ln_part(wx_b))
        LLhood += rk * np.sum(weight **2) / n_components

        # stop iter when gradient close to 0
        if (np.linalg.norm(dw) + db * db < 1e-4):
            total_iter = i
            break

    # predict on training set
    prob = Sigmoid(X @ weight + bia)
    Y_pre = np.where(prob >= 0.5, 1, 0)
    train_err = np.linalg.norm(Y_pre - Y, ord=1)

    # predict on test set
    test_err = None
    if validate is not None:
        validate = np.array(validate)
        testX = validate[:,:-1]
        testY = validate[:, -1]

        testY = np.where(testY >= threshold, 1, 0)
        testX = (testX - Xmin) / (Xmax - Xmin)

        prob = Sigmoid(testX @ weight + bia)
        Y_pre = np.where(prob >= 0.5, 1, 0)
        test_err = np.linalg.norm(Y_pre - testY, ord=1)

    # return result
    result = {'w': weight,
              'b': bia,
              'train error': train_err,
              'test error': test_err,
              'likelihood': LLhood,
              'iter num': total_iter}

    return result


import sys
sys.path.append("../../../lib")

from random import randint, random
from vector import Vector
from sketchy import Sketchy
from physics import limit
import math

class Square(object):
    def __init__(self, width, height):

        # sets how long i want the vector to be
        self.vectorLength = 75
        self.theta = math.pi / 4

        self.location = Vector(randint(0, width), randint(0, height))
        # self.velocity = Vector(0, 0)
        # self.acceleration = Vector(0, 0)

    def update(self):
        """updates the objects"""

        # translates the position the object should be on the circle into cartesian coordinates
        self.location.x = self.vectorLength * math.cos(self.theta)
        self.location.y = self.vectorLength * math.sin(self.theta)
        self.theta += .1
        # self.velocity += self.acceleration
        # self.location += self.velocity

    def draw(self, g, width, height):
        """ draws objects to the screen """

        # sets the color and opacity of the objects outline
        g.stroke(0, 0, 0, 1)

        # sets the thickness of the outline
        g.strokeWeight(1)

        # sets the color fill of the object
        g.fill(0, 1, 1, 0.75)

        # draws a cirlce
        g.rect(self.location.x + (width / 2), self.location.y + (height / 2), 20, 20)


n, m = map(int, input().split())
board = []
for i in range(n):
    line = list(map(int, list(input())))
    board.append(line)

ans = 0
for r in range(n):
    if board[r][0]:
        ans = 1
for c in range(m):
    if board[0][c]:
        ans = 1

for r in range(1, n):
    for c in range(1, m):
        if board[r][c]:
            if ans == 0:
                ans = 1
            if board[r-1][c-1]:
                length = 0
                for k in range(1, board[r-1][c-1]+1):
                    if board[r-k][c] * board[r][c-k] == 0:
                        break
                    length += 1
                board[r][c] = length + 1
                if ans < board[r][c]:
                    ans = board[r][c]

print(ans*ans)


#!/usr/bin/env python
"""
@file    sensor.py
@author  Simon Box
@date    31/01/2013

Classes for extracting sensor data from SUMO and simulating real world data

"""
import traci

class sensor(object):
    pass

class censusSensor(sensor):

    def __init__(self,sensorID,aggregationPeriod=0):
        self.sensorID = sensorID
        self.aggregationPeriod = aggregationPeriod
        self.cumulativeCount = {}
        self.cumulativeOccupancy = {}
        self.occupiedLastStep = False

    def process(self):
        ##collect the latest data.
        occupied = traci.inductionloop.getLastStepOccupancy(self.sensorID)
        if occupied > 0:
            self.occupiedLastStep = True
            self.cumulativeOccupancy[traci.simulation.getCurrentTime()] = occupied
        elif occupied <= 0:
            self.cumulativeOccupancy[traci.simulation.getCurrentTime()] = 0
            if self.occupiedLastStep:
                self.cumulativeCount[traci.simulation.getCurrentTime()] = 1
            self.occupiedLastStep = False

        self.loopEliminateDictionary(self.cumulativeOccupancy)
        self.loopEliminateDictionary(self.cumulativeCount)

    def loopEliminateDictionary(self,dictionary):
        deleteList = []
        for time, value in dictionary.iteritems():
            if (traci.simulation.getCurrentTime() - time) > self.aggregationPeriod:
                deleteList.append(time)
        for deleteItem in deleteList:
            del dictionary[deleteItem]

    def getAggregatedCount(self):
        addValues = self.sumOverDictionary(self.cumulativeCount)
        return addValues

    def getAggregatedOccupancy(self):
        addValues = self.sumOverDictionary(self.cumulativeOccupancy)
        return addValues/len(self.cumulativeOccupancy)

    def sumOverDictionary(self,dictionary):
        addValues = 0
        for time, value in dictionary.iteritems():
            addValues += value
        return addValues

class probeSensor(sensor):
    pass


#!/usr/bin/env python
# coding=utf-8
"""
Author: Deean
Date: 2021-09-27 23:02:12
Description:
FilePath: 387.字符串中的第一个唯一字符.py
"""
#
# @lc app=leetcode.cn id=387 lang=python3
#
# [387] 字符串中的第一个唯一字符
#

# @lc code=start

class Solution:
    def firstUniqChar(self, s: str) -> int:
        for v in sorted(list(set(s)), key=lambda el: s.index(el)):
            if s.count(v) == 1:
                return s.index(v)
        else:
            return -1
# @lc code=end

if __name__ == '__main__':
    s = Solution()
    print(s.firstUniqChar("dddccdbba"))

P0387. 字符串中的第一个唯一字符.py


#!/usr/bin/env python3
import sys
import random
import text_stats as txtST

temp_previously_used_words = {}

def print_random_word_text(start_word,max_count):
    global temp_previously_used_words

    temp_previously_used_words = {}  # Clear Global Variable
    iteration = 0
    generated_text = start_word
    next_word = start_word

    while int(max_count) > iteration:
        new_word = get_random_word_from_followed_word_list(next_word)
        if new_word == None:
            break
        generated_text = generated_text + ' ' + new_word
        next_word = new_word
        iteration = iteration + 1
    print(generated_text)

def check_cache_data(word):
    global temp_previously_used_words
    if temp_previously_used_words.get(word, None) == None:
        None
    else:
        return temp_previously_used_words[word]

def get_random_word_from_followed_word_list(word):
    global temp_previously_used_words

    if check_cache_data(word) == None:
        follow_words = txtST.follow_word_dic[word]
        if len(follow_words) > 0:
            follow_words_seq = []
            for fword, ftimes in sorted(follow_words.items(), key=lambda item: item[1],reverse = True):
                follow_word_list = [fword] * ftimes
                follow_words_seq.extend(follow_word_list)
            random_index = random.randint(0,len(follow_words_seq) - 1)
            temp_previously_used_words[word] = follow_words_seq # update global variable
            return follow_words_seq[random_index]
        else:
            return None
    else:
        follow_words_seq = check_cache_data(word)
        random_index = random.randint(0,len(follow_words_seq) - 1)
        temp_previously_used_words[word] = follow_words_seq # update global variable
        return follow_words_seq[random_index]

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def main():
    if len(sys.argv) == 4:

        contents = txtST.get_file_text(sys.argv[1])
        contents = txtST.get_text_without_special_char(contents)

        print(f'{bcolors.OKBLUE}Please Wait! Generating text results .... .... ... .. .{bcolors.ENDC}')
        print('\n')

        txtST.generate_text_stat(contents)

        print_random_word_text(sys.argv[2],sys.argv[3])
        print('\n')
        print(f'{bcolors.OKGREEN}***Please Note that word with s implie for is word.{bcolors.ENDC}')
        print('\n')
        #open_file(sys.argv[1],sys.argv[2],sys.argv[3])
    else:
        print("Please Provide the (1)File name, (2)Starting Word and (3)Max number of words !")

if __name__ == '__main__':
    if len(sys.argv) == 4:
        main()
    elif len(sys.argv) >= 5:
        print("Please Provide the (1)File name, (2)Starting Word and (3)Max number of words !. No need for extra arguments!")
    else:
        print("Please Provide the File!")

#### References

# https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
# https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python
# https://stackoverflow.com/questions/5843518/remove-all-special-characters-punctuation-and-spaces-from-string


# subprocessというものを用いて、いつもターミナルで行なっているコマンドをpython上で行う

import subprocess

subprocess.run(['ls', '-al'])
# subprocess.run('ls -al', shell=True)   # こっちはあんまり使わないようにする

# r = subprocess.run('ls', shell=True, check=True)
# print('###')

print('###############')

# パイプをやるには
p1 = subprocess.Popen(['ls', '-al'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', 'section8'], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output = p2.communicate()[0]
print(output)


# Original author: Andrew Beatty
# Code used and comments by: Enda Lynch

# This program reads in a string and strips
# any leading or trailing spaces.
# It also converts all the letters to lower case
# this program also outputs the lenght of the original string
# and the normalised one

#Requests input, strips leading space and makes all letters lower case
rawString = input("please enter a string:")
normalisedString = rawString.strip().lower()

#gets lenght of Raw string and of normalised string(including space in between words)
lenghtOfRawString = len(rawString)
lenghtOfNormalised = len(normalisedString)

#prints result and tells you the lenght at which you have recuced your orig string from and to.
print("That String normalised is :{}".format(normalisedString))
print("we reduced the input string from {} to {} characters".format(
lenghtOfRawString, lenghtOfNormalised ))


import pandas as pd
import altair as alt
import numpy as np
from help_func import*
from sklearn.decomposition import PCA

##
# data preprocess
##

def data_preprocess(df):

    df = df.reset_index(drop=True)
    df["MR"] = df.apply(MR_df, axis=1)
    # df["corr"] = df.apply(lambda row: Correlation(row["MR"]), axis=1)
    df["Mag"] = df.apply(lambda row: Mag_dis(row["MR"]), axis=1)

    return df

##
# Join Prob
##

def gen_plot_df(r1):

    rtemp = r1["MR"].ravel()
    k, p = np.meshgrid(range(1,6), range(1,6))
    plot_df = pd.DataFrame({"k":k.ravel(), "p":p.ravel(), "MR":rtemp, "cohort":r1["cohort"]})
    return plot_df

##
# Parent income prepocess for people
##

# Preprocess help funcs
def check_100(df):
    sum_check = 100 - df[["par_q1","par_q2","par_q3","par_q4","par_q5_mod","par_top10pc_mod","par_top1pc_mod","par_toppt1pc"]].sum(axis=1)

    df["par_q5_mod"] = df["par_q5_mod"] + sum_check

    return df

def func(row):
    q = list()

    q.append(np.repeat('par_q1', row['par_q1']).tolist())
    q.append(np.repeat('par_q2',row['par_q2']).tolist())
    q.append(np.repeat('par_q3',row['par_q3']).tolist())
    q.append(np.repeat('par_q4',row['par_q4']).tolist())
    q.append(np.repeat('par_q5',row['par_q5_mod']).tolist())
    q.append(np.repeat('par_top10pc',row['par_top10pc_mod']).tolist())
    q.append(np.repeat('par_top1pc',row['par_top1pc_mod']).tolist())
    q.append(np.repeat('par_toppt1pc',row['par_toppt1pc']).tolist())

    return q

#  https://stackoverflow.com/questions/716477/join-list-of-lists-in-python
def func1(col):
    result = list()
    for el in col:
        result.extend(list(el))
    return [result]

def gen_people_df(row):

    rtemp = np.array(row["data"]).ravel()
    plot_df = pd.DataFrame({"data":rtemp, "cohort":row["cohort"]})
    return plot_df

def Par_income_pre(df):

    parent_df = round(df[["par_q1","par_q2","par_q3","par_q4","par_q5","par_top10pc","par_top1pc","par_toppt1pc"]]*100)
    parent_df = pd.concat([df["cohort"],parent_df], axis=1)
    parent_df["par_q5_mod"] = parent_df["par_q5"] - parent_df["par_top10pc"]
    parent_df["par_top10pc_mod"] = parent_df["par_top10pc"] - parent_df["par_top1pc"]
    parent_df["par_top1pc_mod"] = parent_df["par_top1pc"] - parent_df["par_toppt1pc"]
    parent_df = check_100(parent_df)

    df_mod = parent_df.apply(lambda row: row.to_frame().apply(lambda col: func(col), axis =0), axis = 1)
    df_mod = pd.concat(list(df_mod), axis =1)
    df_mod = df_mod.apply(func1).T

    df_mod = pd.concat([df_mod,parent_df["cohort"]], axis =1)
    df_mod.columns = ["data","cohort"]
    df_mod = df_mod.apply(gen_people_df, axis=1)
    df_mod = pd.concat(list(df_mod)).reset_index()
    df_mod["index"] = df_mod["index"]+1

    return df_mod

def people_plot(df, select):
    # people
    df_new1 = Par_income_pre(df)

    person_img = 'M1.7 -1.7h-0.8c0.3 -0.2 0.6 -0.5 0.6 -0.9c0 -0.6 -0.4 -1 -1 -1c-0.6 0 -1 0.4 -1 1c0 0.4 0.2 0.7 0.6 0.9h-0.8c-0.4 0 -0.7 0.3 -0.7 0.6v1.9c0 0.3 0.3 0.6 0.6 0.6h0.2c0 0 0 0.1 0 0.1v1.9c0 0.3 0.2 0.6 0.3 0.6h1.3c0.2 0 0.3 -0.3 0.3 -0.6v-1.8c0 0 0 -0.1 0 -0.1h0.2c0.3 0 0.6 -0.3 0.6 -0.6v-2c0.2 -0.3 -0.1 -0.6 -0.4 -0.6z'

    income_class8 = ["Lower class", "Working class", "Lower middle class", "Upper middle class", "Upper class", "Upper class (10%)","Upper class (1%)", "Upper class (0.1%)"]
    domains = ["par_q1","par_q2","par_q3","par_q4","par_q5", "par_top10pc", "par_top1pc", 'par_toppt1pc']
    for i in range(0,8):
        df_new1.loc[df_new1["data"] == domains[i], "data"] = income_class8[i]

    shape_scale = alt.Scale(
        domain=income_class8,
        range = [person_img, person_img, person_img, person_img, person_img, person_img, person_img, person_img]
    )

    color_scale = alt.Scale(
        domain=income_class8,
        range=['#4c78a8', '#f58518', '#e45756', '#72b7b2', '#54a24b', '#454B1B', '#00a235', '#45ff00']
    )

    base1 = alt.Chart(df_new1)

    people_chart = base1.transform_calculate(
        row ="ceil(datum.index/10)"
    ).transform_calculate(
        col="datum.index-datum.row*10"
    ).mark_point(filled=True, opacity = 1, size = 50).encode(
        alt.X('col:O', axis=None),
        alt.Y("row:O", axis=None),
        alt.Shape("data:N", legend = None, scale=shape_scale),
        alt.Color("data:N", legend = alt.Legend(title = "Income classes"), scale=color_scale)
    ).add_selection(select).transform_filter(select).properties(title = "Parent\'s Income")

    return df_new1, people_chart

##
# https://stackoverflow.com/questions/55700724/controlling-stack-order-of-an-altair-area
# https://nextjournal.com/sdanisch/multi-view-composition
# https://stackoverflow.com/questions/31511997/pandas-dataframe-replace-all-values-in-a-column-based-on-condition/31512025
# http://bebi103.caltech.edu.s3-website-us-east-1.amazonaws.com/2018/tutorials/t1b_plotting.html
# https://github.com/altair-viz/altair/issues/1826
##

def Joint_Prob_plot(df):

    year_min = df["cohort"].min()
    year_max = df["cohort"].max()
    slider = alt.binding_range(min=year_min, max=year_max, step = 1)
    select = alt.selection_single(name="year",fields=['cohort'],bind=slider, init ={'cohort':year_min} )

    df_new = df.apply(lambda row: gen_plot_df(row), axis = 1)
    df_new = pd.concat(list(df_new))

    income_class = ["Lower class", "Working class", "Lower middle class", "Upper middle class", "Upper class"]
    income_classy = ["Upper class","Upper middle class", "Lower middle class", "Working class", "Lower class"]

    for i in range(0,5):
        df_new.loc[df_new["k"] == i+1, "k"] = income_class[i]
        df_new.loc[df_new["p"] == i+1, "p"] = income_class[i]

    base = alt.Chart(df_new)

    # chart on Joint Prob

    plot_scale = alt.Scale(type="pow", exponent=0.5, scheme = "greens", nice = True)
    color = alt.Color('MR:Q', scale = plot_scale, legend=alt.Legend(title="Mobility Rate"))

    joint_chart = base.mark_rect().encode(
        x=alt.X("p:N",
            sort = income_class,
            axis=alt.Axis(title='Parent\'s Income Class')),
        y=alt.Y('k:N',
            sort = income_classy,
            axis=alt.Axis(title='Children\'s Income Class')),
        color=color
        ).add_selection(select).transform_filter(select).properties(
            height=300, width=300, title='Children Vs Parents Income Distribution Heatmap')

    color_scale = alt.Scale(
        domain=income_class,
        range=['#4c78a8', '#f58518', '#e45756', '#72b7b2', '#54a24b']
    )
    p_mag_1 = base.mark_bar().encode(
        y = alt.Y("p:N",
            sort = income_class,
            axis=alt.Axis(title='Income Quintiles')),
        x = alt.X('sum(MR):Q',
            scale=alt.Scale(domain=(0,1)),
            axis=alt.Axis(title='Probabilities (p)')),
        color = alt.Color('k:N',
            scale=color_scale,
            legend = alt.Legend(title=None)),
        order=alt.Order(aggregate='sum', type="quantitative", sort='descending')
        ).add_selection(select).transform_filter(select).properties(
            height=300, width=300, title='Parent\'s Income Probability Distribution')

    k_mag_1 = base.mark_bar().encode(
        y = alt.Y("k:N",
            sort = income_classy,
            axis=alt.Axis(title='Income Quintiles')),
        x = alt.X('sum(MR):Q',
            axis=alt.Axis(title='Probabilities (k)'),
            scale=alt.Scale(domain=(0,1))),
        color = alt.Color('p:N',
            scale=color_scale,
            legend = alt.Legend(title=None),
            sort=alt.EncodingSortField('p', order='ascending')),
            order=alt.Order(aggregate='sum', type="quantitative", sort='descending')
        ).add_selection(select).transform_filter(select).properties(
            height=300, width=300, title='Children\'s Income Probability Distribution')

    _, people_c = people_plot(df,select)
    people_c = people_c.properties(width=300, height = 300)

    JPP_explanation = """The probability distribution charts below show actual and forecasted household as well as children's income classes.
                        The horizontal axes for both charts indicate the probabilities (forecast) of the parents/children belonging to each of the five income classes on the vertical axes.
                        The stacks on each vertical bar show the actual income distributions within the forecasted classes i.e how many households/children actually fall in the forecasted class. The income classes are color-coded.
                    """
    st.write(JPP_explanation)

    return (p_mag_1 & k_mag_1 & joint_chart & people_c).resolve_scale(color='independent').configure_axis(titleFontWeight = "normal")

##
# cluster helper funcs
##

def data_pre(source):

    s1 = source[source["k"]>source["p"]]
    s1 = s1.groupby(['p']).sum()
    s1['name'] = source['name']
    s1['tier_name'] = source['tier_name']
    s1['k'] = source['k']

    s2 = source[source["k"]==source["p"]]
    s2 = s2.groupby(['p']).sum()
    s2['name'] = source['name']
    s2['tier_name'] = source['tier_name']
    s2['k'] = source['k']

    s3 = source[source["k"]<source["p"]]
    s3 = -1*s3.groupby(['p']).sum()
    s3['name'] = source['name']
    s3['tier_name'] = source['tier_name']
    s3['k'] = source['k']

    s1["type"] = "pos"
    s1["type_num"] = 1
    s2["type"] = "same"
    s2["type_num"] = 0
    s3["type"] = "neg"
    s3["type_num"] = -1

    s = pd.concat([s1,s2,s3])
    s = s.reset_index()

    s['MR_abs']= round(abs(s['MR'])*100,2)

    return s

##
##
def gen_plot_df_name(r1):

    rtemp = r1["MR"].ravel()
    k, p = np.meshgrid(range(1,6), range(1,6))
    plot_df = pd.DataFrame({"k":k.ravel(), "p":p.ravel(), "MR":rtemp, "name":r1["name"], "tier_name":r1["tier_name"]})

    return data_pre(plot_df)

def distance(df1, df2):
    dneg = df1["neg"] - df2["neg"]
    dpos = df1["pos"] - df2["pos"]
    dsame = df1["same"] - df2["same"]
    return np.sqrt(dneg*dneg+dpos*dpos+dsame*dsame)

def filter(df, t_name,U_name):
    df_new = df.loc[df['tier_name']==t_name]
    df_new = df_new.loc[df_new["name"]!=U_name]
    df_new = df_new.sort_values(by=["distance","name"], ascending=True)[:10]
    return df_new

def filter_name(df, name):
    return df.loc[df["name"]==name]

##
# https://stackoverflow.com/questions/59381202/python-altair-condition-color-opacity
##

def cluster_plot(data, U_df, U_Name):

    cp_note = """
                The two plots below - the scatter plot and the horizontal stacked bar chart show how the children that attend colleges/universities turn out in the income
                mobility ladder based on the tier of higher learning that they attend(ed).

                The scatter plot clusters the schools based on the college tier that they belong and consequently displays the student income mobility direction in the haorizontal stacked bar chart below it.

                What is interesting here is this bar chart also indicates the proportion of this income class movement, down to each income class, while also color coding the movements as follows:
                * Green: The student/child moved upwards from their household's income class
                * Yellow: The student/child maintains the same income level as their household
                * Red: The student/child drops from their houshold's income class
            """

    st.write(cp_note)

    U_tier = data.groupby(["name","tier_name"]).mean()
    U_tier = U_tier.reset_index()
    U_tier = data_preprocess(U_tier)

    source = U_tier.apply(gen_plot_df_name, axis =1)
    source = pd.concat(list(source), axis = 0)
    source = source.reset_index()
    source_new = source[["name","MR","type","tier_name"]].groupby(["name","type","tier_name"]).sum()

    s = source_new.stack().to_frame()
    s = s.unstack(1)
    s.columns = s.columns.droplevel()
    source_new = s.reset_index()

    features = ["neg","pos","same"]
    x = source_new.loc[:,features].values
    y = source_new.loc[:,["name"]].values
    pca = PCA(n_components=2)
    prinComp = pca.fit_transform(x)
    prinDf = pd.DataFrame(data=prinComp, columns=['F1', 'F2'])
    df_name = source_new[['name','tier_name']]
    x_df = pd.DataFrame(data=np.round(100*(x),2), columns=features)
    finaldf = pd.concat([prinDf, df_name,x_df], axis=1)

    int_val = U_df['tier_name'].unique()[0]
    sel = alt.selection_single(fields=["tier_name"], bind='legend', init={'tier_name':int_val}, nearest=True, empty="none")
    color = alt.condition(sel, alt.Color('tier_name:N', scale=alt.Scale(scheme='set1')), alt.value('lightgray') )
    opacity = alt.condition(sel, alt.value(1.0), alt.value(0.25))

    d = university_df(finaldf, U_Name)
    finaldf["distance"] = finaldf.apply(lambda x: distance(x,d), axis = 1)
    t_name = finaldf["tier_name"].unique()

    df_new = pd.DataFrame()
    for name in t_name:
        df_new = pd.concat([df_new,filter(finaldf,name, U_Name)])

    df_new = df_new.reset_index()
    name1 = list(df_new["name"].unique())

    s_new = pd.DataFrame()
    d_new = pd.DataFrame()
    for n in name1:
        s_new = pd.concat([s_new,filter_name(source,n)], axis = 0)
        d_new = pd.concat([d_new,filter_name(finaldf,n)], axis = 0)

##
## Cluster Plot
##

    cluster_plot = alt.Chart(finaldf).mark_point(size=50, filled=True, fillOpacity=1).encode(
        alt.X("F1:Q", sort = alt.Sort('descending'), axis=None),
        alt.Y("F2:Q", axis = None),
        color = color,
        opacity=opacity,
        tooltip=['name','pos','same','neg', "tier_name"]
    ).add_selection(sel)

##
## Cross Plot
##

    cross_u = alt.Chart(d).mark_point(size = 250, shape = 'diamond', filled = True).encode(
    alt.X("F1:Q", sort = alt.Sort('descending'), axis = None),
    alt.Y("F2:Q", axis = None),
    color = alt.Color('tier_name:N', scale=alt.Scale(scheme='set1'))
    )

    cross_y = alt.Chart(d_new).mark_point(size = 250, shape = 'cross', filled = True).encode(
    alt.X("F1:Q", sort = alt.Sort('descending'), axis =None ),
    alt.Y("F2:Q", axis = None),
    color = alt.Color('tier_name:N',
        scale=alt.Scale(scheme='set1'),
        legend=alt.Legend(title=None)
        )
    ).transform_filter(sel)

##
## Bar Plot
##

    U_data = university_df(source, U_Name)
    new_U_data = pd.DataFrame()

    for n in t_name:
        temp = U_data.replace({"tier_name":list(U_data["tier_name"])}, {"tier_name":n})
        new_U_data = pd.concat([new_U_data,temp])

    s_new = pd.concat([new_U_data,s_new])

    color_scale1 = alt.Scale(
        domain=["pos","same","neg"],
        range=["green","orange","red"]
    )

    # https://stackoverflow.com/questions/52223358/rename-tooltip-in-altair
    s_new = s_new.reset_index()
    income_class = ["Lower class", "Working class", "Lower middle class", "Upper middle class", "Upper class"]

    for i in range(0,5):
        s_new.loc[s_new["p"] == i+1, "p"] = income_class[i]

    bar_chart = alt.Chart(s_new).mark_bar().encode(
        y = alt.Y("name:N",
            axis=alt.Axis(title=None)
            ),
        x = alt.X("sum(MR):Q",
            axis=alt.Axis(title="Mobility Rate")
            ),
        color = alt.Color('type:N',
            scale = color_scale1,
            legend=alt.Legend(title=None)
            ),
        tooltip=[alt.Tooltip("sum(MR_abs)",title="Mobility Rate"), alt.Tooltip('p', title="Parent's Income Quintiles")]
    ).transform_filter(sel)

    cluster = cluster_plot+cross_u+cross_y

    return (cluster & bar_chart).resolve_scale(color='independent').resolve_legend(color='independent')


# Упражнение по программированию 9.9

# Имитация игры в блек-джек

# Глобальная константа для выигрышного количества карт.
MAX = 21

# Главная функция.
def main():
    # Локальные переменные.
    hand1 = 0
    hand2 = 0
    deck = create_deck()

    while hand1 <= MAX and hand2 <= MAX:

        # Раздать карты каждому игроку и вычислить стоимость
		# комбинации карт на руках.
        card1, value1 = deck.popitem()
        hand1 = update_hand_value(hand1, value1, card1)

        card2, value2 = deck.popitem()
        hand2 = update_hand_value(hand2, value2, card2)

        print('Игроку 1 сдана карта', card1)
        print('Игроку 2 сдана карта', card2)
        print()

    # Determine the winner.
    if hand1 > MAX and hand2 > MAX:
        print("Победителя нет.")
    elif hand1 > 21:
        print("Игрок 2 выиграл.")
    else:
        print("Игрок 1 выиграл.")

# Функция create_deck создает колоду карт и возвращает колоду.
def create_deck():
    # Задать локальные переменные.
    suits = ['пик','червей','крестей','бубей']
    special_values = {'туз':1, 'король':10, 'дама':10, 'валет':10}

    # Создать список всех достоинств карт.
    numbers = ['туз', 'король', 'дама', 'валет']
    for i in range(2,11):
        numbers.append(str(i))

    # Инициализировать колоду.
    deck = {}
    for suit in suits:
        for num in numbers:
            # Значения 2-10.
            if num.isnumeric():
                deck[num + ' ' + suit] = int(num)
            # Туз, король, дама или валет.
            else:
                deck[num + ' ' + suit] = special_values[num]
    return deck

def update_hand_value(hand, value, card):
    if not card.startswith('Туз'):
        return hand+value
    # Добавление 11 вызовет превышение максимума.
    elif hand > 10:
        # По умолчанию значение 1.
        return hand + value
    else:
        return hand + 11

# Вызвать главную функцию.
main()

source-code/Упражнения по программированию/Глава 09/Упражнение 9.9.py

Упражнение 9.9.py


# 가장 많이 재생된 장르에서 많이 재생된 노래 2곡 수록(재생횟수가 같다면 고유번호가 낮은노래 먼저)
# 고유번호도 필요하기에, dict에 함께 저장해준다!
def solution(genres, plays):  # 장르, 재생 횟수
    answer = []
    play_times = {}  # 장르 : (고유번호, 재생횟수)
    total = {}  # 장르별 재생횟수 저장
    for i in range(len(genres)):
        if genres[i] in play_times:
            play_times[genres[i]] += [(i, plays[i])]
            total[genres[i]] += plays[i]
        else:
            play_times[genres[i]] = [(i, plays[i])]
            total[genres[i]] = plays[i]
    # print(play_times)  # {'classic': [(0, 500), (2, 150), (3, 800)], 'pop': [(1, 600), (4, 2500)]}
    # print(total)  # {'classic': 1450, 'pop': 3100}
    total = sorted(total.items(), key=lambda x: -x[1])  # value를 기준으로 내림차순 정렬

    for key in total:
        playlist = play_times[key[0]]  # play_times[pop]  pop장르 가져와서
        playlist = sorted(playlist, key=lambda x: -x[1])

        for i in range(len(playlist)):
            if i == 2:
                break
            else:
                answer.append(playlist[i][0])  # idx 넣어주기
    return answer

print(solution(["classic", "pop", "classic", "classic", "pop"], [500, 600, 150, 800, 2500]))


# coding: utf-8

from __future__ import unicode_literals, absolute_import, print_function, division

from aiohttp import web
from scilib.gender.benchmark.benchmark import CONFIGS_MAP
import redis

KEY_PREFIX = 'scilib-gender'
POOL = redis.ConnectionPool()

async def gender(request):
    tool = request.query.get('tool', 'gender_guesser')
    names = request.query.get('name', None)
    if tool not in CONFIGS_MAP:
        return web.json_response(dict(status='error', message_type='invalid_tool'))
    if not names:
        return web.json_response(dict(status='error', message_type='invalid_name'))

    func = CONFIGS_MAP[tool][1]
    names = [i.strip() for i in names.split(';') if i.strip()]

    conn = redis.Redis(connection_pool=POOL)
    results = conn.hmget(f'{KEY_PREFIX}-{tool}', keys=names)
    results = [i.decode('utf-8') if i else None for i in results]

    results_items = [[i, r] for i, r in enumerate(results)]
    blank_items = [[i, r] for i, r in results_items if not r]
    if blank_items:
        call_results = func([names[i] for i, r in blank_items])
        for i, r in enumerate(call_results):
            results_items[blank_items[i][0]][1] = r

    results = [r for i, r in results_items]
    if not all(results):
        return web.json_response(dict(status='error', message_type='invalid_results', results_items=results_items))

    if blank_items:
        conn.hset(f'{KEY_PREFIX}-{tool}', mapping={names[i]: r for i, r in results_items})

    return web.json_response(dict(
        status='success',
        results=[dict(name=names[i], result=r) for i, r in results_items],
    ))

def main():
    app = web.Application()
    app.add_routes([
        web.get('/gender', gender)
    ])
    web.run_app(app, port=9901)

if __name__ == '__main__':
    main()


import numpy as np
import torch.nn as nn

def constant_init(module, val, bias=0):
    nn.init.constant_(module.weight, val)
    if hasattr(module, 'bias') and module.bias is not None:
        nn.init.constant_(module.bias, bias)

def xavier_init(module, gain=1, bias=0, distribution='normal'):
    assert distribution in ['uniform', 'normal']
    if distribution == 'uniform':
        nn.init.xavier_uniform_(module.weight, gain=gain)
    else:
        nn.init.xavier_normal_(module.weight, gain=gain)
    if hasattr(module, 'bias') and module.bias is not None:
        nn.init.constant_(module.bias, bias)

def normal_init(module, mean=0, std=1, bias=0):
    nn.init.normal_(module.weight, mean, std)
    if hasattr(module, 'bias') and module.bias is not None:
        nn.init.constant_(module.bias, bias)

def uniform_init(module, a=0, b=1, bias=0):
    nn.init.uniform_(module.weight, a, b)
    if hasattr(module, 'bias') and module.bias is not None:
        nn.init.constant_(module.bias, bias)

def kaiming_init(module,
                 a=0,
                 mode='fan_out',
                 nonlinearity='relu',
                 bias=0,
                 distribution='normal'):
    assert distribution in ['uniform', 'normal']
    if distribution == 'uniform':
        nn.init.kaiming_uniform_(
            module.weight, a=a, mode=mode, nonlinearity=nonlinearity)
    else:
        nn.init.kaiming_normal_(
            module.weight, a=a, mode=mode, nonlinearity=nonlinearity)
    if hasattr(module, 'bias') and module.bias is not None:
        nn.init.constant_(module.bias, bias)

def caffe2_xavier_init(module, bias=0):
    # `XavierFill` in Caffe2 corresponds to `kaiming_uniform_` in PyTorch
    # Acknowledgment to FAIR's internal code
    kaiming_init(
        module,
        a=1,
        mode='fan_in',
        nonlinearity='leaky_relu',
        distribution='uniform')


import numpy as np
from gensim import corpora, models, similarities, downloader
from keybert import KeyBERT
import os
# from theme_wc import WordCloudGenerator

def load_embeddings(embeddings_file):
    # Detect the model format by its extension:
        emb_model = models.KeyedVectors.load_word2vec_format(embeddings_file, binary=True,                                                             unicode_errors='replace')
        key_list = emb_model.index_to_key  # list
        key_index_dict = emb_model.key_to_index  # dict
        embeddings = emb_model.vectors        # print(embeddings.shape)  #(199430, 300)
        return emb_model, embeddings, key_list, key_index_dict

def get_similar_words(theme, emb_model, embeddings, key_list, key_index_dict):
    word_dict = [(key, value) for key, value in key_index_dict.items() if key.startswith(theme)]    # print(word_dict)
    # [('covid-19_PROPN', 3894), ('covid-19_NOUN', 9583), ('covid-19_ADJ', 12940), ('covid-19_NUM', 16412)]    # get embedding of theme word
    emb1 = embeddings[word_dict[0][1]]    # compute the similarities
    similarities = emb_model.cosine_similarities(emb1, embeddings)
    sorted_similarities_index = np.argsort(similarities)[::-1]  # large->small
    # get the similar words
    similar_text = [key_list[idx].split('_')[0] for idx in sorted_similarities_index[1:10]]
    similar_text = list(dict.fromkeys(similar_text))  # remove duplicates    #
    # print(similar_text)
    # compute the frequency of these words
    similar_text_dict = {}
    for text in similar_text:
        text_frequency = sum([emb_model.get_vecattr(key, "count") for key, value in key_index_dict.items() if key.startswith(text)])
        similar_text_dict[text] = text_frequency
    similar_text_dict = sorted(similar_text_dict.items(), key=lambda x: x[1], reverse=True)
    if len(similar_text_dict) >= 5:
        similar_text_dict = similar_text_dict[:4]
    similar_text_list = list(list(zip(*similar_text_dict))[0])
    return similar_text_list

def extract_kw_similar(title):
    kw_model = KeyBERT(model='all-mpnet-base-v2')
    # title = 'Number of police officers in crimeville'
    keywords = kw_model.extract_keywords(title,
                                        keyphrase_ngram_range=(1,1),
                                        stop_words='english',
                                        highlight=False,
                                        top_n=3)
    keywords_list = list(dict(keywords).keys())
    # print(keywords_list)

    # load model
    emb_model, embeddings, key_list, key_index_dict = load_embeddings('D:/speak/theme_extract/model.bin')
    # find all words containing "xx"
    theme = keywords_list[0]
    theme_and_text = {}
    for theme in keywords_list:
        try:
            similar_text_list = get_similar_words(theme, emb_model, embeddings, key_list, key_index_dict)
            theme_and_text[theme] = similar_text_list
        except:
            pass
            # theme_and_text[theme] = []
    result_list = []
    for key, value in theme_and_text.items():
        result_list.append(key)
        result_list.extend(value)
    my_dict = {key: 20 - 1.5*i + 10*(i//5) for i,key in enumerate(result_list)}
    return my_dict

# title = "Date of cherry blossom in High Park"
# my_dict = extract_kw_similar(title)
# # print(my_dict)
# wordcloud_gen = WordCloudGenerator(font_path='D:/speak/frontend/src/assets/fonts/TiltNeon-Regular.ttf',
#                                     background_color='#F5F5F5', colormap="binary",
#                                     prefer_horizontal=1,
#                                     max_font_size=45, min_font_size=12,
#                                     width=420, height=200,
#                                     margin=50, d=my_dict)
# img_path2 = wordcloud_gen.generate_wordcloud()


# -*- coding: utf-8 -*-
import json
import re

import scrapy

from Jovi.items import JoviLonglasttimeItem

class XinaRollSpiderSpider(scrapy.Spider):
    name = 'sina_roll_spider'
    pid = '153'
    col = [
        {
            'name': "全部",
            'lid': "2509",
            'page': 9669
        },
        {
            'name': "国内",
            'lid': "2510",
            'page': 14550

        },
        {
            'name': "国际",
            'lid': "2511",
            'page': 488

        },
        {
            'name': "社会",
            'lid': "2669",
            'page': 597
        },
        {
            'name': "体育",
            'lid': "2512",
            'page': 4215
        },
        {
            'name': "娱乐",
            'lid': "2513",
            'page': 1212
        },
        {
            'name': "军事",
            'lid': "2514",
            'page': 1156

        },
        {
            'name': "科技",
            'lid': "2515",
            'page': 2148
        },
        {
            'name': "财经",
            'lid': "2516",
            'page': 6529
        },
        {
            'name': "股市",
            'lid': "2517",
            'page': 3507
        },
        {
            'name': "美股",
            'lid': "2518",
            'page': 12854
        }
    ]
    meta = dict()

    def start_requests(self):
        meta = self.meta
        for i in self.col:
            meta['third_tag'] = i['name']
            for j in range(1, i['page'] + 1):
                url = 'https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid={}&k=&num=50&page={}'.format(i['lid'],
                                                                                j)
                yield scrapy.Request(url, meta=meta, callback=self.get_urls)

    def get_urls(self, response):
        meta = response.meta
        data = json.loads(response.text)
        for i in data['result']['data']:
            url = i['url']
            meta['title'] = i['title']
            yield scrapy.Request(url, callback=self.get_content, meta=meta)

    def get_content(self, response):
        item = JoviLonglasttimeItem()
        meta = response.meta
        article = response.xpath('//div[@class="article"]/p[not(@class)]//text()').extract()
        content = ''
        pattern = r'原标题：|特别声明：|{}'.format(meta['title'])
        for i in article:
            if re.search(pattern, i):
                continue
            else:
                content += i.strip()
        item['article_content'] = content
        item['article_title'] = meta['title']
        item['article_url'] = response.url
        item['first_tag'] = '新浪滚动'
        item['second_tag'] = '新浪滚动'
        item['third_tag'] = meta['third_tag']
        yield item


import cantera as ct
import numpy as np
import matplotlib.pyplot as plt

CCl4_X_0 = float(1000)
pressure_0 = 12.4
reaction_mech="~/EDC/FPC/KM/2009_Schirmeister_EDC/chem_annotated_irreversible.cti"
mass_flow_rate = float(36)
length = 18
n_steps = 1000
area = 0.03225097679
n_pfr=18
# T_list = [322, 350, 375, 399, 424, 451, 461, 466, 471, 477, 479, 479, 480, 481, 482, 483, 484, 485, 486]

temp_T = 750
if CCl4_X_0 > 1 :  #ppm
        CCl4_X_0 = CCl4_X_0 /1000000
# T_0 = 273.15 + T_list[0]  # inlet temperature [K]
pressure_0 *= ct.one_atm
spcs = ct.Species.listFromFile(reaction_mech)
for spc in spcs[::-1]:
    if spc.composition == {'C': 2.0, 'Cl': 2.0, 'H': 4.0} and spc.charge == 0:
        EDC_label = spc.name
    if spc.composition == {'C': 1.0, 'Cl': 4.0} and spc.charge == 0:
        CCl4_label = spc.name
EDC_X_0 = 1 - CCl4_X_0
composition_0 = '{}:{}, {}:{}'.format(EDC_label, EDC_X_0, CCl4_label, CCl4_X_0)
mass_flow_rate *= 1000 / 3600  # T/H to kg/s

# import the gas model and set the initial conditions
gas = ct.Solution(reaction_mech)
gas.TPX = temp_T, pressure_0, composition_0
dz = length / n_steps
r_vol = area * dz

# create a new reactor
r = ct.IdealGasConstPressureReactor(gas)
r.volume = r_vol

gas.set_multiplier(10)

# # create a reservoir to represent the reactor immediately upstream. Note
# # that the gas object is set already to the state of the upstream reactor
# upstream = ct.Reservoir(gas, name='upstream')

# # create a reservoir for the reactor to exhaust into. The composition of
# # this reservoir is irrelevant.
# downstream = ct.Reservoir(gas, name='downstream')

# # The mass flow rate into the reactor will be fixed by using a
# # MassFlowController object.
# m = ct.MassFlowController(upstream, r, mdot=mass_flow_rate)

# # We need an outlet to the downstream reservoir. This will determine the
# # pressure in the reactor. The value of K will only affect the transient
# # pressure difference.
# v = ct.PressureController(r, downstream, master=m, K=1e-5)

sim = ct.ReactorNet([r])

rxn = gas.reaction_equations()
n_rxn = len(rxn)

for i in range(n_rxn):
    r.add_sensitivity_reaction(i)

max_s = [0] * n_rxn
max_s_abs = [0]* n_rxn
sim.rtol = 1e-6 # Relative tolerance
sim.atol = 1e-15 # Absolute tolerance

sim.rtol_sensitivity = 1e-6 # Relative tolerance for sensitivity
sim.atol_sensitivity - 1e-6 # absolute tolerance for sensitivity

# define time, space, and other information vectors
z = (np.arange(n_steps) + 1) * dz
t = np.zeros(n_pfr)  # residence time in each PFR reactor
compositions = [None] * n_pfr  # compositions of output stream in each PFR reactor
states = ct.SolutionArray(gas,extra = ['t'])

tot_time = 2e-3
dt = 5e-6

print(r.component_index('VCM(10)'))
for time in np.arange(0,tot_time,dt):
    sim.advance(time)
    states.append(r.thermo.state,t = time*1000)

    for j in range(n_rxn):
        s = sim.sensitivity('EDC(1)',j)

        if np.abs(s) > np.abs(max_s[j]):
            max_s[j] = s
            max_s_abs[j] = abs(s)
sen_zip = zip(max_s_abs,max_s,range(n_rxn))
sen_tuple = tuple(sen_zip)
sen_tuple_sorted = sorted(sen_tuple,reverse=True)

top_n = 10
top_rxn_index = [0]*top_n
top_rxn = [0]*top_n
top_sen = [0]*top_n

for i in range(top_n):
    top_rxn_index[i] = int(sen_tuple_sorted[i][2])
    top_rxn[i] = rxn[top_rxn_index[i]]
    top_sen[i] = float(sen_tuple_sorted[i][1])

print("The top %d EDC sensitive reactions are:" %(top_n))
print("-----------------------------------------------------------")
print("%10s  %s %18s %12s %12s" % ('Reaction Index','|','Reaction','|','Sensitivity'))
print("-----------------------------------------------------------")
for i in range(top_n):
    print("%7d %8s %26s %4s %10.8f" % (top_rxn_index[i],'|',top_rxn[i],'|',top_sen[i]))
print("-----------------------------------------------------------")

top_rxn_index.reverse()
top_rxn.reverse()
top_sen.reverse()

plt.figure(1)
plt.barh(range(top_n),top_sen,0.75,align= 'center')
plt.yticks(range(top_n),top_rxn)
plt.xlabel('')
plt.title('{0} Most EDC Sensitivity Reactions' .format(top_n), fontweight= 'bold')
plt.grid(which='major')
plt.minorticks_on()
plt.grid(which='minor',alpha=0.2)
plt.tight_layout()
plt.savefig('sensitivity.png')

# for i, T in enumerate(T_list[1:]):
#     Ti = T_list[i] + 273.15
#     Te = T + 273.15
#     dT = (Te - Ti) / n_steps
#     T = Ti
#     t_r = np.zeros_like(z)  # residence time in each CSTR reactor
#     # iterate through the PFR cells
#     for n in range(n_steps):
#         # simulate the linear T-profile in each reactor
#         T = Ti + (n + 1) * dT
#         gas.TP = T, None
#         r.syncState()
#         # Set the state of the reservoir to match that of the previous reactor
#         gas.TPX = r.thermo.TPX
#         upstream.syncState()
#         # integrate the reactor forward in time until steady state is reached
#         sim.reinitialize()
#         sim.set_initial_time(0)
#         sim.advance_to_steady_state()
#         # compute velocity and transform into time
#         t_r[n] = r.mass / mass_flow_rate  # residence time in this reactor
#         # write output data
#         states.append(r.thermo.state)


"""消息链处理器用到的工具函数, 类"""
import argparse
import inspect
import re
from contextvars import ContextVar
from typing import (
    TYPE_CHECKING,
    Callable,
    ClassVar,
    Dict,
    Final,
    List,
    Literal,
    NoReturn,
    Optional,
    Type,
    TypeVar,
    Union,
    overload,
)

from graia.amnesia.message.chain import MessageChain

from avilla.core.elements import Element, Notice, NoticeAll, Text
from avilla.core.utilles import identity

from .typing import T

if TYPE_CHECKING:
    from .twilight import Twilight

Element_T = TypeVar("Element_T", bound=Element)
elem_mapping_ctx: ContextVar[Dict[str, Element]] = ContextVar("elem_mapping_ctx")

def split(string: str, keep_quote: bool = False) -> List[str]:
    """尊重引号与转义的字符串切分

    Args:
        string (str): 要切割的字符串
        keep_quote (bool): 是否保留引号, 默认 False.

    Returns:
        List[str]: 切割后的字符串, 可能含有空格
    """
    result: List[str] = []
    quote = ""
    cache: List[str] = []
    for index, char in enumerate(string):
        if char in {"'", '"'}:
            if not quote:
                quote = char
            elif char == quote and index and string[index - 1] != "\\":  # is current quote, not transfigured
                quote = ""
            else:
                cache.append(char)
                continue
            if keep_quote:
                cache.append(char)
        elif not quote and char == " ":
            result.append("".join(cache))
            cache = []
        elif char != "\\":
            cache.append(char)
    if cache:
        result.append("".join(cache))
    return result

def gen_flags_repr(flags: re.RegexFlag) -> str:
    """通过 RegexFlag 生成对应的字符串

    Args:
        flags (re.RegexFlag): 正则表达式的标记

    Returns:
        str: 对应的标记字符串
    """
    flags_list: List[str] = []

    if re.ASCII in flags:
        flags_list.append("a")
    if re.IGNORECASE in flags:
        flags_list.append("i")
    if re.LOCALE in flags:
        flags_list.append("L")
    if re.MULTILINE in flags:
        flags_list.append("m")
    if re.DOTALL in flags:
        flags_list.append("s")
    if re.UNICODE in flags:
        flags_list.append("u")
    if re.VERBOSE in flags:
        flags_list.append("x")

    return "".join(flags_list)

def transform_regex(flag: re.RegexFlag, regex_pattern: str) -> str:
    """生成嵌套正则表达式字符串来达到至少最外层含有一个捕获组的效果

    Args:
        flag (re.RegexFlag): 正则表达式标记
        regex_pattern (str): 正则表达式字符串

    Returns:
        str: 转换后的正则表达式字符串
    """
    return f"(?{gen_flags_repr(flag)}:({regex_pattern}))" if flag else f"({regex_pattern})"

class MessageChainType:
    """用于标记类型为消息链, 在 ArgumentMatch 上使用"""

    @staticmethod
    def __call__(string: str) -> MessageChain:
        return _from_mapping_string(string, elem_mapping_ctx.get())

class ElementType:
    """用于标记类型为消息链元素, 在 ArgumentMatch 上使用"""

    def __init__(self, pattern: Type[Element_T]):
        self.regex = re.compile(f"\x02(\\d+)_{identity(pattern)}\x03")

    def __call__(self, string: str) -> Element:
        if not self.regex.fullmatch(string):
            raise ValueError(f"{string} not matching {self.regex.pattern}")
        return _from_mapping_string(string, elem_mapping_ctx.get())[0]

class UnmatchedClass:
    __slots__ = ()

    def __repr__(self) -> str:
        return "<Unmatched>"

    def __bool__(self) -> bool:
        return False

Unmatched = UnmatchedClass()

class TwilightParser(argparse.ArgumentParser):
    """适于 Twilight 使用的 argparse.ArgumentParser 子类
    移除了报错时自动退出解释器的行为
    """

    def error(self, message) -> NoReturn:
        raise ValueError(message)

    def accept_type(self, action: Union[str, type]) -> bool:
        """检查一个 action 是否接受 type 参数

        Args:
            action (Union[str, type]): 检查的 action

        Returns:
            bool: 是否接受 type 参数
        """
        if action is ...:
            action = "store"
        if isinstance(action, str):
            action_cls: Type[argparse.Action] = self._registry_get("action", action, action)
        elif isinstance(action, type) and issubclass(action, argparse.Action):
            action_cls = action
        else:
            return False
        action_init_sig = inspect.signature(action_cls.__init__)
        if "type" not in action_init_sig.parameters:
            return False
        return True

class TwilightHelpManager:
    AUTO_ID: Final[str] = "&auto_id" + hex(id("&auto_id"))
    _manager_ref: ClassVar[Dict[str, "TwilightHelpManager"]] = {}

    name: str
    display_name: Optional[str]
    help_map: Dict[str, "Twilight"]

    def __init__(self, name: str, display_name: Optional[str] = AUTO_ID):
        self.name: str = name
        self.help_map: Dict[str, Twilight] = {}
        if display_name == self.AUTO_ID:
            self.display_name = None if name.startswith(("global", "local", "_")) else name
        else:
            self.display_name = display_name
        if name in TwilightHelpManager._manager_ref:
            global_ref = TwilightHelpManager._manager_ref[name]
            global_ref.display_name = global_ref.display_name or display_name
            self.help_map = global_ref.help_map
        else:
            TwilightHelpManager._manager_ref[name] = self

    def register(self, twilight: "Twilight") -> None:
        if twilight.help_id == self.AUTO_ID:
            from .twilight import ElementMatch, ParamMatch, RegexMatch, UnionMatch

            extracted_ids: List[str] = []
            for match in twilight.matcher.origin_match_list:
                if isinstance(match, UnionMatch):
                    extracted_ids.extend(match.pattern)
                elif isinstance(match, ElementMatch):
                    extracted_ids.append(match.type.__name__)
                elif isinstance(match, RegexMatch) and not isinstance(match, ParamMatch):
                    extracted_ids.append(match.pattern)
            if not extracted_ids:
                raise ValueError(f"Unable to extract help_id from {twilight}")
            help_id = extracted_ids[0]
        else:
            help_id = twilight.help_id
        if help_id in self.help_map and self.help_map[help_id] is not twilight:
            raise ValueError(
                f"Help Manager {self.name}'s help id {help_id} has been registered", self.help_map[help_id]
            )
        self.help_map[help_id] = twilight

    @classmethod
    def get_help_mgr(cls, mgr: Union["TwilightHelpManager", str]) -> "TwilightHelpManager":
        return TwilightHelpManager(mgr) if isinstance(mgr, str) else mgr

    @overload
    def get_help(
        self,
        description: str = "",
        epilog: str = "",
        *,
        prefix_src: Literal["brief", "usage", "description"] = "brief",
        fmt_cls: Type[argparse.HelpFormatter] = argparse.HelpFormatter,
    ) -> str:
        ...

    @overload
    def get_help(
        self,
        description: str = "",
        epilog: str = "",
        *,
        prefix_src: Literal["brief", "usage", "description"] = "brief",
        fmt_func: Callable[[str], T],
        fmt_cls: Type[argparse.HelpFormatter] = argparse.HelpFormatter,
    ) -> T:
        ...

    def get_help(
        self,
        description: str = "",
        epilog: str = "",
        *,
        prefix_src: Literal["brief", "usage", "description"] = "brief",
        fmt_func: Optional[Callable[[str], T]] = None,
        fmt_cls: Type[argparse.HelpFormatter] = argparse.HelpFormatter,
    ) -> Union[T, str]:
        """获取本管理器总的帮助信息

        Args:
            fmt_func (Optional[Callable[[str], T]]): 如果指定, 则使用该函数来转换帮助信息格式
            fmt_cls (Type[argparse.HelpFormatter], optional): 如果指定, 则使用该类来格式化帮助信息. \
                默认为 argparse.HelpFormatter
        """
        formatter = fmt_cls("")
        formatter.add_usage(self.display_name or "", [], [], "")

        formatter.add_text(description)

        if self.help_map:
            formatter.start_section(f"共有 {len(self.help_map)} 个子匹配")
            for help_id, twilight in self.help_map.items():
                if twilight.help_data is not None:
                    prefixes: Dict[str, str] = {
                        "brief": twilight.help_brief,
                        "usage": twilight.help_data["usage"],
                        "description": twilight.help_data["description"],
                    }
                    formatter.add_text(f"{help_id} - {prefixes[prefix_src]}")
                else:
                    formatter.add_text(f"{help_id}")
            formatter.end_section()

        formatter.add_text(epilog)

        help_string: str = formatter.format_help()
        return fmt_func(help_string) if fmt_func else help_string

def _to_mapping_str(
    chain: MessageChain,
    *,
    remove_source: bool = True,
    remove_quote: bool = True,
    remove_extra_space: bool = False,
) -> tuple[str, Dict[str, Element]]:
    """转换消息链为映射字符串与映射字典的元组.
    Args:
        remove_source (bool, optional): 是否移除消息链中的 Source 元素. 默认为 True.
        remove_quote (bool, optional): 是否移除消息链中的 Quote 元素. 默认为 True.
        remove_extra_space (bool, optional): 是否移除 Quote At AtAll 的多余空格. 默认为 False.
    Returns:
        Tuple[str, Dict[str, Element]]: 生成的映射字符串与映射字典的元组
    """
    elem_mapping: Dict[str, Element] = {}
    elem_str_list: List[str] = []
    for i, elem in enumerate(chain.content):
        if not isinstance(elem, Text):
            elem_mapping[str(i)] = elem
            elem_str_list.append(f"\x02{i}_{identity(elem)}\x03")
        elif (
            remove_extra_space
            and i  # not first element
            and isinstance(
                chain.content[i - 1], (Notice, NoticeAll)
            )  # following elements which have an dumb trailing space
            and elem.text.startswith("  ")  # extra space (count >= 2)
        ):
            elem_str_list.append(elem.text[1:])
        else:
            elem_str_list.append(elem.text)
    return "".join(elem_str_list), elem_mapping

__element_pattern = re.compile("(\x02\\w+\x03)")

def gen_subclass(cls: type[T]):  # pyright: ignore
    """生成某个类的所有子类 (包括其自身)

    Args:
        cls (Type[T]): 类

    Yields:
        Type[T]: 子类
    """
    yield cls
    for sub_cls in cls.__subclasses__():
        if TYPE_CHECKING:
            assert issubclass(sub_cls, cls)
        yield from gen_subclass(sub_cls)

ELEMENT_MAPPING: Dict[str, Type[Element]] = {identity(i): i for i in gen_subclass(Element)}

def _from_mapping_string(string: str, mapping: Dict[str, Element]) -> MessageChain:
    """从映射字符串与映射字典的元组还原消息链.
    Args:
        string (str): 映射字符串
        mapping (Dict[int, Element]): 映射字典.
    Returns:
        MessageChain: 构建的消息链
    """
    elements: List[Element] = []
    for x in __element_pattern.split(string):
        if x:
            if x[0] == "\x02" and x[-1] == "\x03":
                index, class_name = x[1:-1].split("_")
                if not isinstance(mapping[index], ELEMENT_MAPPING[class_name]):
                    raise ValueError("Validation failed: not matching element type!")
                elements.append(mapping[index])
            else:
                elements.append(Text(x))
    return MessageChain(elements)


from tkinter import *
from tkinter import messagebox

def button_click():
    spl_str = (entry_str.get()).split()
    if len(spl_str) == 0:
        messagebox.showwarning("Ошибка", "Ввод пустой строки")
        return
    try:
        first = int(entry_first_str.get()) - 1
        end = int(entry_end_str.get()) - 1
        if(first < 0 or first > end or end > len(spl_str)-1):
            messagebox.showerror("Ошибка", "Ошибка диапазона")
            return
    except:
        messagebox.showerror("Ошибка", "Ошибка ввода")
        return
    new_str = " ".join(spl_str[first:end+1])
    if len(spl_str[:first]) > 0:
        new_str += " " + " ".join(spl_str[:first])
    if len(spl_str[end+1:]) > 0:
        new_str += " " + " ".join(spl_str[end+1:])

    label3.config(text=new_str)

root = Tk()
root.title("Перестановка слов")
root.geometry("600x200")
root.config(bg="#dce0ce")

entry_str = StringVar()
textbox1 = Entry(textvariable=entry_str,borderwidth=1, relief="solid", width="50", font = "5")
textbox1.place(relx=.5, rely=.1, anchor="c")

entry_first_str = StringVar()
label1 = Label(text="C", bg="#dce0ce", width = "10", font = "5")
label1.place(relx=.1, rely=.3, anchor="c")
textbox2 = Entry(textvariable=entry_first_str, width="5", font = "5")
textbox2.place(relx=.2, rely=.3, anchor="c")

entry_end_str = StringVar()
label2 = Label(text="ПО", bg="#dce0ce", width = "10", font = "5")
label2.place(relx=.5, rely=.3, anchor="c")
textbox3 = Entry(textvariable=entry_end_str, width="5", font = "5")
textbox3.place(relx=.6, rely=.3, anchor="c")

btn = Button(text="OK", background ="#dcdcdc", width="5", font="5", command=button_click)
btn.place(relx=.8, rely=.3, anchor="c")

label3 = Label(borderwidth=1, relief="solid", bg="#dce9ce", width = "50", font = "5")
label3.place(relx=.5, rely=.6, anchor="c")

root.mainloop()


#-*-coding:utf-8-*-

'''
Description:
    Ce fichier est le script du test fonctionnel. Il dessiner les courbes de la qualité
    des meilleures solution et la qualité moyenne des solutions dans la figure

Version: 1.0

Auteur: Peng BI
'''

import matplotlib.pyplot as plt

class TestParameters:
    '''
    Description:
        Cette classe est pour déterminer le nombre d'itérations

    Attribut: rien
    '''

    def drawFigure(self):
        '''
        Description:
            Cette méthode est pour dessiner la courbe de qualité des meilleures solutions de chaque
            itérations et dessiner la courbe de qualité moyenne de solutions de chaque itérations

        :return: rien
        '''

        bestQualityOfSolutionForEachIterationList = []
        averageQualityOfSolutionForEachIterationList = []

        # lire le fichire de qualité, et extraire la colonne de meilleure qualité et la colonne de qualité moyenne
        counter = 0
        with open('../../files/quality.txt', 'rt') as qualities:
            for quality in qualities:
                if counter != 0:
                    bestQualityOfSolutionForEachIterationList.append(float(quality.split('\t')[1]))
                    averageQualityOfSolutionForEachIterationList.append(float(quality.split('\t')[2]))
                counter += 1

        # chercher l'id d'itération où la meilleure qualité est maximum
        maxQualityIteration = bestQualityOfSolutionForEachIterationList.index(max(bestQualityOfSolutionForEachIterationList))
        print("La meilleure solution est dans itération %d" % int(maxQualityIteration))

        # dessiner la figure, l'axe x est l'itération, l'axe y est la qualité
        iteration = len(bestQualityOfSolutionForEachIterationList)
        x = [x for x in range(0,iteration)]
        y1 = bestQualityOfSolutionForEachIterationList
        y2 = averageQualityOfSolutionForEachIterationList

        plt.figure()
        plt.plot(x, y1, 'b', linewidth=0.5, marker='*')
        plt.plot(x, y2, 'r', linewidth=2, marker='+')

        plt.fill_between(x, y1, y2, color='g', alpha=0.5)
        plt.legend(['bestSolution', 'averageSolution'])
        plt.xlabel('Iteration')
        plt.ylabel('Quality')

        plt.show()

if __name__ == '__main__':
    testParameter = TestParameters()
    testParameter.drawFigure()


import asyncio
import io
import sys

import pytest
from httpx import AsyncClient
from PIL import Image
from sqlalchemy.ext.asyncio import AsyncSession

from backend.config import settings
from backend.main import app
from backend.models import ImageModel, TweetModel, UserModel
from backend.models.base import Base
from backend.models.db_helper import db_helper
from backend.services.medias_services import add_image_path_to_db, get_image, save_image
from backend.tests.factories import TweetFactory, UserFactory

@pytest.fixture(scope="session")
async def db() -> AsyncSession:
    async for session in db_helper.session_dependency():
        yield session

@pytest.fixture(scope="session", autouse=True)
async def prepare_db():
    assert settings.mode == "TEST"

    async with db_helper.engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    yield
    async with db_helper.engine.begin() as conn:
        await conn.run_sync(Base.metadata.drop_all)

@pytest.fixture(scope="session")
def event_loop(request):
    if sys.platform == "win32":
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()

@pytest.fixture
async def client(user: UserModel) -> AsyncClient:
    async with AsyncClient(
        app=app,
        base_url="http://127.0.0.1:5000/api",
        headers={"api-key": str(user.api_key)},
    ) as ac:
        yield ac
        await ac.aclose()

@pytest.fixture
async def user() -> UserModel:
    user: UserModel = await UserFactory()
    return user

@pytest.fixture
async def tweet(user) -> TweetModel:
    tweet = await TweetFactory(author=user)
    return tweet

@pytest.fixture
async def image_bytes() -> bytes:
    image = Image.new("RGB", (100, 100))
    io_obj = io.BytesIO()
    image.save(io_obj, format="JPEG")
    io_obj.seek(0)

    return io_obj.read()

@pytest.fixture
async def image(image_bytes: bytes) -> ImageModel:
    async for session in db_helper.session_dependency():
        path = await save_image(image_bytes)
        image_id = await add_image_path_to_db(session=session, photo_path=path)

        return await get_image(session=session, image_id=image_id)


#!/usr/bin/env python
from faws.util import assert_pyversion
assert_pyversion()

def get_args():
    import argparse
    parser = argparse.ArgumentParser(
        description='List launch AMI for each instance ID',
        epilog='Demo tool that demonstrates concurrent API access',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        )
    parser.add_argument('-r', '--region',
                        help='EC2 region')
    return parser.parse_args()

if '__main__' == __name__:
    from faws import Agent
    agent = Agent()
    instances_f = agent.callf('DescribeInstances')
    images_f = agent.callf('DescribeImages')
    # First make map of the AMIs of each instance
    instances = instances_f.result()
    instance_amis = dict()
    for instance_item in instances.tree().xpath('//instancesSet/item'):
        instance_id = instance_item.find('instanceId').text
        image_id = instance_item.find('imageId').text
        instance_amis[instance_id] = image_id
    # Now get the names of each
    ami_names = dict()
    amis_to_check = set(instance_amis.values())
    images = images_f.result()
    for image_item in images.tree().xpath('//imagesSet/item'):
        if len(amis_to_check) == 0:
            break
        image_id = image_item.find('imageId').text
        if image_id in amis_to_check:
            ami_names[image_id] = image_item.find('name').text
            amis_to_check.remove(image_id)
    # Now cross-reference and report
    print('\t'.join(['instanceId', 'amiName']))
    for instance_id in sorted(instance_amis.keys()):
        image_id = instance_amis[instance_id]
        image_name = ami_names[image_id]
        print('\t'.join([instance_id, image_name]))


def int_input():
	N = input()
	N = N.split(' ')
	if len(N)!=1:
		return [int(i) for i in N]
	else :
		return int(N[0])

N = int_input()
if N==2 or N==3:
	print("NO SOLUTION")
elif N==4 :
	print("2 4 1 3")
else:
	if N&1==1:
		n=int(N/2)+1
	else :
		n=int(N/2)
	Temp = [0]*N
	j=0
	for i in range(1,N+1,2):
		Temp[j]=i
		if j+n<N:
			Temp[j+n]=i+1
		j+=1
	for i in Temp:
		print(i,end=" ")


# 내리막길
m, n = map(int, input().split())
hillMap = []

for _ in range(m):
    hillMap.append(list(map(int, input().split())))

visited = [[-1 for _ in range(n)] for _ in range(m)]

def dfs(x, y, last):
    # 나가면 리턴
    if x < 0 or y < 0 or x >= n or y >= m:
        return 0

    # 높은 곳으로 가면 리턴
    if hillMap[y][x] >= last:
        return 0

    # 이미 방문했었다면 그 숫자를 리턴
    if visited[y][x] > -1:
        return visited[y][x]

    # 도착시 1 리턴
    if x == (n - 1) and y == (m - 1):
        return 1

    sumResult = 0
    sumResult += dfs(x, y - 1, hillMap[y][x]) # 상
    sumResult += dfs(x, y + 1, hillMap[y][x]) # 하
    sumResult += dfs(x - 1, y, hillMap[y][x]) # 좌
    sumResult += dfs(x + 1, y, hillMap[y][x]) # 우
    visited[y][x] = sumResult
    return sumResult

result = dfs(0, 0, 10001)
print(result)


# Biblioteca para obtener los signos de puntuacion
import string
# Se importa SnowballStemmer para realizar el stemming
from nltk.stem import SnowballStemmer
# Se establece el stemmer y se configura para el español.
stemmer = SnowballStemmer('spanish')
#EOS_punct_add = {'.',';',':','?', '!', '...'}
EOS_punct = {'.'}
BOS_punct = set()
#TEMP_BOS = '-'
#BOS_punct = {'¿', '¡',TEMP_BOS}
ADDITIONAL = {';',':','?', '!', '...','¿', '¡','-'}
# Se construye el conjunto de signos de puntuación adicionalmente se añaden estos caracteres ya que en español si se usan.
exclude = set(string.punctuation + "<>" + string.digits ).union(ADDITIONAL) - EOS_punct - BOS_punct

def clean_punctuation_line(line):
  """
  Limpia de signos de puntuación de un string.
  Args:
    line (str): Texto que se quiere limpiar.
  Returns:
    str: Texto limpio.
  """
  return ''.join(ch for ch in line if ch not in exclude)

def clean_line(line):
  """
  Limpia de signos de puntuación de un string.
  Args:
    line (str): Texto que se quiere limpiar.
  Returns:
    str: Texto limpio.
  """
  sentence = ''.join(ch for ch in line if ch not in exclude)
  sentence = sentence.replace("\xa0","")
  sentence = sentence.replace("\t"," ")
  sentence = sentence.replace("—fol. v→","")
  sentence = sentence.strip().lower()
  if len(sentence) < 2  or sentence == "":
    return ""
  return sentence + " "

def stemming_words(line):
  """
  Realiza el stemming de las palabras de un texto.
  Args:
      line (str): Texto que se quiere stemmizar.
  Returns:
      str: Texto limpio.
  """
  words = line.split()
  return " ".join([stemmer.stem(word) for word in words]) + " "

def remove_consecutive_eos_punct(text):
  punct = zip(EOS_punct,EOS_punct)
  for p in punct:
    text = text.replace("".join(p), ".")
  return text

def clean_raw_corpus(raw_corpus_list, stem_words=True):
  """
  Limpia el corpus de lineas vacias, signos de puntuación y hace el stemming de cada palabra.
  Args:
      raw_corpus_list (list): Lista de strings que se quieren limpiar.
  Returns:
      str: Texto limpio.
  """
  clean_str = ""
  for line in raw_corpus_list:
    new_line = line.strip()
    new_line = clean_line(new_line)
    if stem_words:
      new_line = stemming_words(new_line)
    clean_str += new_line
  #clean_str = remove_consecutive_eos_punct(clean_str)
  return clean_str.strip()


"""
pyape.uwsgiproxy
~~~~~~~~~~~~~~~~~~~
代理 uwsgi 模块的方法
"""
import warnings
import pickle
import importlib

in_uwsgi = False
uwsgi = None

try:
    uwsgi = importlib.import_module('uwsgi')
    in_uwsgi = True
except:
    in_uwsgi = False

def cache_get(key):
    if in_uwsgi:
        return uwsgi.cache_get(key)
    return None

def cache_set(key, value):
    if in_uwsgi:
        return uwsgi.cache_set(key, value)
    return None

def cache_del(key):
    if in_uwsgi:
        return uwsgi.cache_del(key)
    return None

def cache_exists(key):
    if in_uwsgi:
        return uwsgi.cache_exists(key)
    return None

def cache_update(key, value):
    if in_uwsgi:
        return uwsgi.cache_update(key, value)
    return None

def mule_msg(msg, mule_id=1):
    if in_uwsgi:
        try:
            uwsgi.mule_msg(pickle.dumps(msg, protocol=pickle.HIGHEST_PROTOCOL), mule_id)
        except Exception as e:
            warnings.warn(str(e))
    return None

def worker_id():
    if in_uwsgi:
        return uwsgi.worker_id()
    return -1

def register_signal(signum, target, func):
    """ 注册一个 uwsgi 信号
    :param signum: 信号量
    :param target: 字符串
        worker/worker0 将发送信号给第一个可用worker。如果你指定过一个空字符串，那么这是默认值。
        workers 会发送信号给每个worker。
        workerN (N > 0) 会发送信号给worker N。
        mule/mule0 会发送信号给第一个可用mule。 (见 uWSGI Mule)
        mules 会发送信号给所有mule。
        muleN (N > 0) 会发送信号给mule N。
    """
    if in_uwsgi:
        return uwsgi.register_signal(signum, target, func)
    return -1

def signal_registered(signum):
    """ 一个信号是否被注册
    """
    if not in_uwsgi:
        return None
    return uwsgi.signal_registered(signum)

def get_free_signal():
    """ 获取没有被注册的信号量，否则抛出一个错误
    """
    if not in_uwsgi:
        return None
    for signum in range(0, 256):
        if not uwsgi.signal_registered(signum):
            return signum
    raise IndexError("No free uwsgi signal available!")

def add_timer(signum, seconds):
    """ 注册一个定时器
    """
    if not in_uwsgi:
        return None
    return uwsgi.add_timer(signum, seconds)

class signal(object):
    """ register_signal 的装饰器模式
    """
    def __init__(self, signum, **kwargs):
        self.signum = signum
        self.target = kwargs.get('target', '')

    def __call__(self, f):
        uwsgi.register_signal(self.signum, self.target, f)
        return f

class timer(object):
    """ add_timer 的装饰器模式
    """
    def __init__(self, secs, **kwargs):
        self.signum = kwargs.get('signum', get_free_signal())
        self.secs = secs
        self.target = kwargs.get('target', '')

    def __call__(self, f):
        uwsgi.register_signal(self.signum, self.target, f)
        uwsgi.add_timer(self.signum, self.secs)
        return f


import datetime

from django.test import TestCase

from autofixture import AutoFixture

from teamlogic.models import Cup
from teamlogic.models import Match
from teamlogic.models import MatchInLeague
from teamlogic.models import MatchPair
from teamlogic.models import Tournament

class TestUtils(object):
    @staticmethod
    def create_match(home_goal, away_goal,
                     home_goal_first=None, away_goal_first=None):
        home_goal_first = home_goal_first or 0
        away_goal_first = away_goal_first or 0
        fixture_match = AutoFixture(
            Match, generate_fk=True,
            field_values={'home_goal': home_goal,
                          'away_goal': away_goal,
                          'home_goal_first': home_goal_first,
                          'away_goal_first': home_goal_first})
        return fixture_match.create(1)[0]

    @staticmethod
    def create_tournament(beginDate=None, endDate=None):
        beginDate = beginDate or datetime.date(2007, 4, 24)
        endDate = endDate or datetime.date(2010, 4, 22)
        fixture_tournament = AutoFixture(
            Tournament, generate_fk=True,
            field_values={'begin_date': beginDate,
                          'end_date': endDate})
        return fixture_tournament.create(1)[0]

    @staticmethod
    def create_cup(beginDate=None, endDate=None):
        beginDate = beginDate or datetime.date(2007, 4, 25)
        endDate = endDate or datetime.date(2008, 1, 1)
        fixture_cup = AutoFixture(
            Cup, generate_fk=True,
            field_values={'begin_date': beginDate,
                          'end_date': endDate})
        return fixture_cup.create(1)[0]

class TestMatches(TestCase):
    def test_drawn_result(self):
        match = TestUtils.create_match(1, 1, 0, 0)
        self.assertTrue(match.is_drawn())

    def test_home_winner_result_positive(self):
        match = TestUtils.create_match(1, 0, 0, 0)
        self.assertTrue(match.is_home_winner())

    def test_home_winner_result_negative(self):
        match = TestUtils.create_match(0, 1, 0, 0)
        self.assertFalse(match.is_home_winner())

    def test_away_winner_result_positive(self):
        match = TestUtils.create_match(0, 1, 0, 0)
        self.assertTrue(match.is_away_winner())

    def test_away_winner_result_negative(self):
        match = TestUtils.create_match(1, 0, 0, 0)
        self.assertFalse(match.is_away_winner())

    def test_getWinner(self):
        match = TestUtils.create_match(1, 0, 0, 0)
        self.assertEqual(match.getWinner(), match.home)

        match = TestUtils.create_match(0, 1, 0, 0)
        self.assertEqual(match.getWinner(), match.away)

        match = TestUtils.create_match(0, 0, 0, 0)
        self.assertEqual(match.getWinner(), None)

    def test_this_team(self):
        match = TestUtils.create_match(1, 0, 0, 0)
        self.assertTrue(match.this_team(match.home))
        self.assertTrue(match.this_team(match.away))
        other_match = TestUtils.create_match(1, 0, 0, 0)
        self.assertFalse(match.this_team(other_match.home))

class TestTournament(TestCase):
    def test_get_season(self):
        for result, end_year in [('2007/2008', 2008), ('2007', 2007)]:
            tournament = TestUtils.create_tournament(
                beginDate=datetime.date(2007, 4, 22),
                endDate=datetime.date(end_year, 11, 23)
            )
            self.assertEqual(result, tournament.get_season())

class TestMatchPair(TestCase):
    def test_getWinner(self):
        # For two match in pair
        first_match = TestUtils.create_match(1, 0, 0, 0)
        second_match = TestUtils.create_match(2, 0, 0, 0)
        second_match.home = first_match.away
        second_match.away = first_match.home

        pair = MatchPair.objects.create(
            first_match=first_match,
            second_match=second_match,
            cup=TestUtils.create_cup(),
            only_one_match=False,
            is_penalty=False)

        self.assertEqual(pair.getWinner(), second_match.home)
        self.assertEqual(pair.getWinner(), first_match.away)

        # For two match in pair with penalty
        second_match = TestUtils.create_match(1, 0, 0, 0)
        second_match.home = first_match.away
        second_match.away = first_match.home

        pair.second_match = second_match
        pair.first_penalty = 5
        pair.second_penalty = 6

        self.assertEqual(pair.getWinner(), first_match.away)
        self.assertEqual(pair.getWinner(), second_match.home)

        pair.only_one_match = True
        self.assertEqual(pair.getWinner(), first_match.home)

        pair.first_match.away_goal = 1
        self.assertEqual(pair.getWinner(), first_match.away)
        # For one match in pair with penalty

    def test_unicode(self):
        first_match = TestUtils.create_match(1, 0, 0, 0)
        second_match = TestUtils.create_match(2, 0, 0, 0)
        second_match.home = first_match.away
        second_match.away = first_match.home

        pair = MatchPair.objects.create(
            first_match=first_match,
            second_match=second_match,
            cup=TestUtils.create_cup(),
            only_one_match=False,
            is_penalty=False)

        self.assertEqual(unicode('%s - %s' % (first_match.home.name,
                                               second_match.home.name)),
                         pair.__unicode__())


import io
import requests

from flask import jsonify, Response
from flask_restful import Resource, reqparse
from PyPDF2 import PdfFileMerger

parser = reqparse.RequestParser()
parser.add_argument('pdf_urls', type=str, required=True, location='form', action='append')

class MergeResource(Resource):
    def post(self):
        args = parser.parse_args()

        merger = PdfFileMerger()

        failed_urls = []
        for pdf_url in args['pdf_urls']:
            if pdf_url == "":
                continue
            try:
                pdf_response = requests.get(pdf_url)
                pdf_bytes = pdf_response.content
                merger.append(fileobj=io.BytesIO(pdf_bytes))
            except Exception:
                failed_urls.append(pdf_url)
                continue

        if len(failed_urls) > 0:
            response = jsonify({'failed_urls': failed_urls})
            response.status_code = 400

            return response

        combined_pdf = io.BytesIO()
        merger.write(combined_pdf)
        combined_pdf.seek(0)

        response = Response(combined_pdf, mimetype='application/pdf')
        response.headers['Content-Disposition'] = "attachment; filename=%s.pdf" % 'pdf_name'

        return response


import os
import csv
import shutil
from PIL import Image
from src.datasets import labels, compute_split_weights
from src.datasets.datagen.loader import load_from_split

def copy_img_to_ds(src, dst, size=(600, 600)):
    img = Image.open(src).convert('L')
    img = img.resize(size)
    img.save(dst)

def create_split_dir(split, split_name, labels, data_dir):
    split_dir = os.path.sep.join([data_dir, split, split_name])

    labels = ['unknown'] if split_name == 'test' else labels

    # create dirs for labels into {split_dir}
    for label in labels:
        label_dir = os.path.sep.join([split_dir, label])
        if not os.path.exists(label_dir):
            os.makedirs(label_dir)

    return split_dir

def generate_split(split, data_dir, sets=[]):
    dataset_paths = {}

    for (x_split, y_split), split_name in sets:
        split_dir = create_split_dir(split, split_name, labels, data_dir)

        # labels counter to determine file name based on split and label
        labels_counter = {'normal': 0, 'covid': 0, 'pneumonia': 0}

        X, y = [], []

        for img, label in zip(x_split, y_split):
            y.append(label)

            # str representation of the label
            label = labels[label]

            i = labels_counter[label]
            dest = os.path.sep.join(
                [split_dir, label, "{0:04d}.jpg".format(i)])
            copy_img_to_ds(img, dest)
            X.append(dest)
            labels_counter[label] += 1

        dataset_paths[split_name] = (X, y)

    return dataset_paths

def create_dataset(dataset, version, data_dir, split, splits_dir, **kwargs):
    data_dir = data_dir if data_dir else os.path.sep.join(
        ["./data", dataset, "data"])

    # Load splits and save those into a new dataset called {dataset}
    train_split_path = os.path.sep.join([splits_dir, split, "train.txt"])
    test_split_path = os.path.sep.join([splits_dir, split, "test.txt"])
    val_split_path = os.path.sep.join([splits_dir, split, "val.txt"])
    train_split = (load_from_split(
        version, data_dir, train_split_path), 'train')
    val_split = (load_from_split(version, data_dir, val_split_path), 'val')
    test_split = (load_from_split(version, data_dir, test_split_path), 'test')

    # splits to iterate
    train_val_test = [train_split, val_split, test_split]
    train_val = [train_split, val_split]

    # generate labeled train and val sets
    generate_split(split, data_dir, sets=train_val)

    # generate all datasets in one
    sets = ([], [])
    for (X, y), _ in train_val_test:
        sets = (X + sets[0], y + sets[1])
    paths = generate_split(split, data_dir, sets=[(sets, 'dataset')])
    (X, y) = paths['dataset']

    # weights = compute_split_weights(X, y)

    # generate test with unknown label
    (x_split, y_split), split_name = test_split
    split_dir = create_split_dir(split, split_name, labels, data_dir)

    # labels counter to determine file name based on split and label
    i = 0
    filenames, y_test = [], []

    for img, label in zip(x_split, y_split):
        dest = os.path.sep.join(
            [split_dir, 'unknown', "{0:04d}.jpg".format(i)])
        copy_img_to_ds(img, dest)
        filenames.append("{0:04d}.jpg".format(i))
        y_test.append(label)
        i += 1

    # save test labels for kaggle
    with open(os.path.sep.join([data_dir, split, 'derived.csv']), mode='w') as f:
        writer = csv.writer(f, delimiter=',', quotechar='"',
                            quoting=csv.QUOTE_MINIMAL)
        # writer.writerow(['Id', 'Expected', 'Weight'])
        writer.writerow(['Id', 'Expected'])
        for f, y in zip(filenames, y_test):
            # weight = weights[y]
            # writer.writerow([f, str(y), str(weight)])
            writer.writerow([f, str(y)])


import matplotlib.pyplot
from sklearn.cluster import KMeans
import numpy
image = matplotlib.pyplot.imread("kmeans.jpg")
img_shape = image.shape
print (img_shape)
w = img_shape[0]
h = img_shape[1]
image = image.reshape(w*h,3)
print(image)
K = int(input("Enter K: "))
run = KMeans(n_clusters = K).fit(image)
labels = run.predict(image)

clusters = run.cluster_centers_

print(clusters)
print(labels)
# zero: 0, like: dimension like image
# new_image = numpy.zeros_like(image)
new_image = numpy.zeros((w,h,3), dtype=numpy.uint8)
x = 0
# reshape by self
for i in range(w):
    for j in range(h):
        new_image[i][j] = clusters[labels[x]]
        x += 1
# for i in range(len(new_image)):
#     new_image[i] = clusters[labels[i]]
# new_image = new_image.reshape(w, h, 3)
matplotlib.pyplot.imshow(new_image)
matplotlib.pyplot.show()