# -*- coding:utf-8 -*-
# Author : MMagicLoren
# @Email : 993983320@qq.com
# @Time : 2019/10/13 15:59
# @File : 图像梯度计算.py
# @Project : Workspace
import cv2 as cv
import os
import sys
path = os.path.abspath(os.path.dirname(sys.argv[0]))

def sobel_demo(image):
    grad_x = cv.Sobel(src, cv.CV_64F, 1, 0, ksize=3)
    gradx = cv.convertScaleAbs(grad_x)
    grad_y = cv.Sobel(src, cv.CV_64F, 0, 1, ksize=3)
    grady = cv.convertScaleAbs(grad_y)
    gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0)
    cv.imshow("grad_x", grad_x)  # 将src图片放入该创建的窗口中
    cv.imshow("grad_y", grad_y)  # 将src图片放入该创建的窗口中
    cv.imshow("gradxy", gradxy)  # 将src图片放入该创建的窗口中

if __name__ == '__main__':
    src = cv.imread(path + '\\1.jpg')  # 读入图片放进src中
    cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)  # 创建窗口, 窗口尺寸自动调整
    cv.imshow("input image", src)
    sobel_demo(src)

    cv.waitKey(0)  # 等有键输入或者1000ms后自动将窗口消除，0表示只用键输入结束窗口
    cv.destroyAllWindows()


import numpy as np
import pandas as pd
import re
import matplotlib.pyplot as plt
import seaborn as sns

from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

import nltk
nltk.download('stopwords')

# printing stopwords in English
print(stopwords.words('english'))

# Data-Preprocessing
# loading the dataset
news_dataset = pd.read_csv('fake_or_real_news.csv')

# print the first 5 rows of the dataset
news_dataset.head()

# counting the number of missing valuesin the dataset
news_dataset.isnull().sum()

# replacing the null values with empty strings
news_dataset = news_dataset.fillna('')

#merging the author name and news title
news_dataset['content'] = news_dataset['text']

print(news_dataset['content'])

# separating the data and the label
X = news_dataset.drop(columns=['label'],axis=1)
Y = news_dataset['label']

# Stemming: It is the process of reducing a word to its root word.(remove␣suffix and prefix)
port_stem = PorterStemmer()

def stemming(content):
    stemmed_content = re.sub('[^a-zA-Z]',' ',content)
    stemmed_content = stemmed_content.lower()
    stemmed_content = stemmed_content.split()
    stemmed_content = [port_stem.stem(word) for word in stemmed_content if not word in stopwords.words('english')]
    stemmed_content = ' '.join(stemmed_content)
    return stemmed_content

news_dataset['content'] = news_dataset['content'].apply(stemming)

print(news_dataset['content'])

#separating the data and label
X = news_dataset['content'].values
Y = news_dataset['label'].values

# converting the textual data to numerical data
vectorizer = TfidfVectorizer()
vectorizer.fit(X)
X = vectorizer.transform(X)

# Splitting the dataset to training & test data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2,stratify=Y, random_state=42)

clf_dt=DecisionTreeClassifier()
clf_dt=clf_dt.fit(X_train, Y_train)

predictions_dt = clf_dt.predict(X_test)
test_data_accuracy_dt = accuracy_score(predictions_dt, Y_test)
test_data_accuracy_dt = round(test_data_accuracy_dt, 3)
print('Accuracy score of the test data : ', test_data_accuracy_dt)

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

print(classification_report(Y_test, predictions_dt))
mat=confusion_matrix(Y_test, predictions_dt)
print(confusion_matrix(Y_test, predictions_dt))

# Making a Predictive System
X_new = X_test[3]
prediction = clf_dt.predict(X_new)
print(prediction)
if (prediction[0]=='REAL'):
    print('The news is Real')
else:
    print('The news is Fake')

classes = ['Real','Fake']

cm_df = pd.DataFrame(mat, index = classes,columns = classes)
plt.figure(figsize = (5,5))
sns.set(font_scale=1.2)
sns.heatmap(cm_df, annot = True,cbar=False,linewidth=2,fmt='d',cmap="GnBu")
plt.title('Confusion Matrix for Decision Tree Classifier')
plt.ylabel('Original Values')
plt.xlabel('Predicted Values')
plt.savefig('Cm_dt.png')
plt.show()

import joblib
joblib.dump(clf_dt, 'model.pkl')
joblib.dump(vectorizer, 'vectorizer.pkl')


#! /usr/bin/env python

import unittest
from unit_timeside import TestRunner
#from timeside.plugins.decoder.file import FileDecoder
from timeside.plugins.decoder.aubio import AubioDecoder as FileDecoder
from timeside.plugins.analyzer.level import Level
from timeside.core.tools.test_samples import samples

class TestAnalyzerLevel(unittest.TestCase):

    def setUp(self):
        self.analyzer = Level()

    def testOnSweep(self):
        "runs on sweep"
        self.source = samples["sweep.wav"]

        max_level_value = 0
        rms_level_value = -2.995

        self.expected = {'level.max': max_level_value,
                         'level.rms': rms_level_value}

    def testOnC4_Scale(self):
        "runs on C4 scale"
        self.source = samples["C4_scale.wav"]

        max_level_value = 0
        rms_level_value = -3.705

        self.expected = {'level.max': max_level_value,
                         'level.rms': rms_level_value}

    def tearDown(self):
        decoder = FileDecoder(self.source)
        (decoder | self.analyzer).run()
        results = self.analyzer.results
        for result_id in self.expected.keys():
            result = results[result_id]
            self.assertEqual(result.data_object.value,
                              self.expected[result_id])
        #print results
        #print results.to_yaml()
        #print results.to_json()
        #print results.to_xml()

if __name__ == '__main__':
    unittest.main(testRunner=TestRunner())


import numpy as np

from Utils.timer_decorator import Timer

class Kor3D:
    def __init__(self):
        self.syllable = 0xac00
        self.consonant = ord('ㄱ')
        self.vowel = ord('ㅏ')
        self.upper_consonants = ['ㄱ','ㄲ','ㄴ','ㄷ','ㄸ','ㄹ','ㅁ','ㅂ','ㅃ','ㅅ','ㅆ','ㅇ','ㅈ','ㅉ','ㅊ','ㅋ','ㅌ','ㅍ','ㅎ']
        self.vowels = ['ㅏ','ㅐ','ㅑ','ㅒ','ㅓ','ㅔ','ㅕ','ㅖ','ㅗ','ㅘ','ㅙ','ㅚ','ㅛ','ㅜ','ㅝ','ㅞ','ㅟ','ㅠ','ㅡ','ㅢ','ㅣ']
        self.lower_consonants = ['ᆧ','ㄱ','ㄲ','ㄳ','ㄴ','ㄵ','ㄶ','ㄷ','ㄹ','ㄺ','ㄻ','ㄼ','ㄽ','ㄾ','ㄿ','ㅀ','ㅁ','ㅂ','ㅄ','ㅅ','ㅆ','ㅇ','ㅈ','ㅊ','ㅋ','ㅌ','ㅍ','ㅎ']

    def phoneme_decomposition(self, letter):
        #     음절코드: 0xac00, 초성코드: 0x1100, 중성코드: 0x1161, 종성코드: 0x11a8
        # return [chr((ord(letter) - self.syllable) // (28 * 21) + 0x1100),
        #        chr(((ord(letter) - self.syllable) % (28 * 21)) // 28 + 0x1161),
        #        chr((ord(letter) - self.syllable) % 28 - 1 + 0x11a8).strip()]
        return [self.upper_consonants[(ord(letter) - self.syllable) // (28 * 21)],
               self.vowels[((ord(letter) - self.syllable) % (28 * 21)) // 28],
               self.lower_consonants[(ord(letter) - self.syllable) % 28]]

    def trans_syllable_3d(self, letter):
        if self.syllable - 1 < ord(letter) < self.syllable + 11172:
            letter_3d = self.phoneme_decomposition(letter)
        elif self.consonant - 1 < ord(letter) < self.consonant + 30:
            letter_3d = [letter, '', '']
        elif self.vowel - 1 < ord(letter) < self.vowel + 21:
            letter_3d = ['', letter, '']
        else:
            letter_3d = [letter] * 3

        return letter_3d

    def trans_sent_3n(self, sentence):
        trans_sent = np.array([self.trans_syllable_3d(letter) for letter in sentence])
        print(f'tranfrom string to 3*n dimension array')

        return trans_sent

    def phoneme_composition(self, vector):
        upper_consonant = self.upper_consonants.index(vector[0])
        vowel = self.vowels.index(vector[1])
        lower_consonant = self.lower_consonants.index(vector[2])

        return chr(((upper_consonant * 21) + vowel) * 28 + lower_consonant + 0xac00)

    def trans_syllable_1d(self, vector):
        if vector[0] == vector[1] == vector[2]:
            letter = vector[0]
        elif not vector[2]:
            letter = [x for x in vector if x][0]
        else:
            letter = self.phoneme_composition(vector)

        return letter

    def trans_sent_1n(self, vectors):
        trans_vec = [self.trans_syllable_1d(vector) for vector in vectors]
        print('tranfrom 3*n dimension array to string')

        return ''.join(trans_vec)


# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
import re

sys.path.insert(0, os.path.abspath('exts'))

import sphinx_rtd_theme

# -- Project information -----------------------------------------------------

project = 'GYRE'
author = 'Rich Townsend & The GYRE Team'
version = "master"
release = "master"
branch = "master"
copyright = '2020, Rich Townsend & The GYRE Team'

# -- General configuration ---------------------------------------------------

# Numbered figures
numfig = True

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx_rtd_theme',
    'sphinx.ext.mathjax',
    'sphinx.ext.extlinks',
    'sphinx-prompt',
    'sphinx_substitution_extensions',
    'ads_cite',
    'nml_roles',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# -- Additional configuration ------------------------------------------------

# sphinx_rtd options
html_theme_options = {
    'collapse_navigation': True,
    'sticky_navigation': True,
    'navigation_depth': 4,
    'includehidden': True,
    'titles_only': False,
    'logo_only': True
}

# CSS
html_context = {
    'css_files': [
        '_static/theme_overrides.css',  # override wide tables in RTD theme
    ],
}

# Set master doc
master_doc = 'index'

# Set logo
html_logo = 'gyre-logo.png'

# Set up Extlinks
extlinks = {
    'wiki': ('https://en.wikipedia.org/wiki/%s', ''),
    'ads': ('https://ui.adsabs.harvard.edu/abs/%s/abstract', ''),
    'netlib': ('https://www.netlib.org/%s', ''),
    'git': ('https://github.com/%s', ''),
    'repo': ('https://github.com/rhdtownsend/gyre/blob/{:s}/%s'.format(branch), '')
}

# Set site-wide targets

if release == 'master':
    tarball = 'master'
else:
    tarball = 'v{0:s}'.format(release)

targets = {
    'github-tarball': 'https://codeload.github.com/rhdtownsend/gyre/tar.gz/{0:s}'.format(tarball),
    'gyre-forums': 'http://www.astro.wisc.edu/~townsend/gyre-forums/',
    'mesa-sdk': 'http://www.astro.wisc.edu/~townsend/static.php?ref=mesasdk',
    'mesa': 'http://mesa.sourceforge.net/',
}

rst_prolog = '\n'.join(['.. _{:s}: {:s}'.format(x, targets[x]) for x in targets])

# Add substitutions for sphinx_substitution_extensions

rep_exts = {"release": release,
            "author": author}

for rep_ext_key, rep_ext_val in rep_exts.items():
    rst_prolog += "\n.. |{:s}| replace:: {:s}".format(rep_ext_key, rep_ext_val)

# Latex macros

macros = {}

with open('macros.def') as f:
    line = f.readline()
    while line:
        key, value = line.rstrip().split('\t')
        macros[key] = value
        line = f.readline()

mathjax_macros = {}

for key, value in macros.items():
    argnums = re.findall('#(\d)', value)
    if argnums:
        mathjax_macros[key] = [value, int(max(argnums))]
    else:
        mathjax_macros[key] = value

mathjax_config = {
    'TeX': {
        'Macros': mathjax_macros
    }
}

# Enable email obfuscation
email_automode = True


"""English text processing"""

_character_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\'(),-.:;? "
_pad = "_PAD_"
_eos = "_EOS_"
_unk = "_UNK_"

_vocab = [_pad, _eos, _unk] + list(_character_set)

char2idx = {char: idx for idx, char in enumerate(_vocab)}
idx2char = {idx: char for idx, char in enumerate(_vocab)}

def load_vocab():
    """Returns the char2index mapping
    """
    return char2idx

def normalize_text(textpath):
    """Read text from file; normalize it and return it as a sequence of charaters
    """
    with open(textpath, "r") as fp:
        text = fp.readlines()
    text = " ".join(text)
    text = text.strip("\n")

    return text

def text_to_sequence(text):
    """Convert text to sequence of indices corresponding to characters in the text
    """
    sequence = [char2idx[char] if char in char2idx else char2idx["_UNK_"] for char in text]

    return sequence


from abc import ABC, abstractmethod
from flask import request
import logging
import mlog

logger = logging.getLogger('endpoint')

class Endpoint(ABC):
    def __init__(self, dbClient, mqClient, logger, env):
        self.dbClient = dbClient
        self.mqClient = mqClient
        self.logger = logger
        self.env = env
        super().__init__()

    def publish(self, topic, data):
        self.mqClient.publish(topic, data)

    @abstractmethod
    def handle(path_data, request_data):
        pass

registered_endpoints = []

def register(url, methods):
    def decorator(cls):
        logger.debug(f'Registering class {cls.__name__} with url {url} and methods {methods}')
        cls.url = url
        cls.methods = methods

        registered_endpoints.append(cls)
    return decorator

def _make(ep, dbClient, mqClient, env):
    def _(*args, **kwargs):
        inst = ep(dbClient, mqClient, logging.getLogger(ep.__name__), env)
        logger.debug(f'Endpoint instance is of type {type(inst)}')
        path_data = kwargs
        logger.debug(f'invoked endpoint {ep.__name__}')
        logger.debug(f'path data: {path_data}')
        logger.debug(f'request data: {request}')
        return inst.handle(path_data, request)
    return _

def load(flaskApp, dbClient, mqClient, env):
    for ep in registered_endpoints:
        mlog.configLoggers([ep.__name__], env.logs_folder, env.debug_mode)
        logger.debug(f'Adding flask url rule {ep.url} with {ep.__name__}')
        flaskApp.add_url_rule(ep.url, ep.__name__, _make(ep, dbClient, mqClient, env), methods=ep.methods)


from collections import defaultdict
from math import sqrt
import sys

#AC
input = sys.stdin.readline

def check(c, i, m): #O(sqrt(n))
    for j in range(1, int(sqrt(i))+1):
        if i%j == 0 and j <= m:
            c[i].add(j)
            if i//j <= m:
                c[i].add(i//j)

for _ in range(int(input())):
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    c = defaultdict(set) # This dictionary contains divisors (up to 'm') of [Ai] number. #Complexity sqrt(n)
    for i in arr:
        check(c, i, m)
    d = defaultdict(int)
    j = 0
    ans = float('inf')

    # Here, if there no valid solution, we extend right pointer (j) until a valid solution.
    # While a valid solution is found we move the left pointer (i) to right while keeping the left right pointer (j) stationary and repeat.
    for i in range(n): #O(n)
        while j < n:
            if len(d) == m:
                ans = min(ans, abs(arr[i]-arr[j-1]))
                break
            else:
                for k in c[arr[j]]:
                    d[k] += 1
                j += 1
        if len(d) == m:
                ans = min(ans, abs(arr[i]-arr[j-1]))

        for k in c[arr[i]]:
            d[k] -= 1
            if d[k] == 0:
                del d[k]

    if ans == float('inf'):
        print("-1")
    else:
        print(ans)


if __name__ == "__main__":

    # task one
    # name = input("введіть своє ім'я")
    # print(введіть свій вік")
    # while True:
    #     try:
    #         age = int(input())
    #         if age > 130:
    #             print("вік занадто великий, спробуйте ще раз")
    #         elif age < 0:
    #             print("вік не може бути меншим за нуль, спробуйте ще раз")
    #
    #         else:
    #             break
    #     except ValueError:
    #         print("вам слід ввести вік у цілих цифрах, спробуйте ще раз")
    # print(f"Привіт, {name}! Твій вік — {age}")

    # task two ver one
    # def greet_user(name, age):
    #     return f"Привіт, {name}! Твій вік — {age}"
    #
    #
    # if __name__ == "__main__":
    #     name = input("Введіть ваше ім'я: ")
    #     print("Введіть ваш вік: ")
    #
    #     while True:
    #         try:
    #             age = int(input())
    #             if age > 130:
    #                 print("Це занадто великий вік, спробуйте ще раз")
    #             elif age < 0:
    #                 print("Вік не може бути менше нуля, спробуйте ще раз")
    #             else:
    #                 break
    #         except ValueError:
    #             print("Ви повинні ввести вік у форматі числа, спробуйте ще раз")
    #
    #     result = greet_user(name, age)
    #     print(result)

    # task two ver two
    # def greet_user(name, age):
    #     try:
    #         age = int(age)
    #         if age > 130:
    #             return "Це занадто великий вік, спробуйте ще раз"
    #         elif age < 0:
    #             return "Вік не може бути менше нуля, спробуйте ще раз"
    #         else:
    #             return f"Привіт, {name}! Твій вік — {age}"
    #     except ValueError:
    #         return "Ви повинні ввести вік у форматі числа, спробуйте ще раз"
    #
    #
    # if __name__ == "__main__":
    #     name = input("Введіть ваше ім'я: ")
    #     age = input("Введіть ваш вік: ")
    #
    #     result = greet_user(name, age)
    #     print(result)

    # task three
    # numbers = []
    #
    # while True:
    #     try:
    #         num = float(input("Введіть додатне число (або введіть '0' для завершення): "))
    #         if num == 0:
    #             break
    #         elif num < 0:
    #             raise ValueError("Введено від'ємне число")
    #         numbers.append(num)
    #     except ValueError as e:
    #         print(f"Помилка: {e}")
    #
    # if not numbers:
    #     print("Список чисел порожній.")
    # else:
    #     total = sum(numbers)
    #     print(f"Сума всіх додатних чисел: {total}")

    # task four ver one
    def calculate_sum(numbers):
        return sum(numbers)

    if __name__ == "__main__":
        numbers = []
        while True:
            try:
                num = float(input("Введіть число (або '0' для завершення вводу): "))
                if num == 0:
                    break
                numbers.append(num)
            except ValueError as e:
                print(f"Помилка: {e}")

        total = calculate_sum(numbers)
        print(f"Сума елементів списку: {total}")

    # task four ver two
    # def calculate_sum(numbers):
    #     try:
    #         total = sum(numbers)
    #         return total
    #     except ValueError as e:
    #         raise ValueError("Помилка при обчисленні суми: " + str(e))
    #
    #
    # if __name__ == "__main__":
    #     numbers = []
    #     while True:
    #         try:
    #             num = float(input("Введіть число (або '0' для завершення вводу): "))
    #             if num == 0:
    #                 break
    #             numbers.append(num)
    #         except ValueError as e:
    #             print(f"Помилка: {e}")
    #
    #     try:
    #         total = calculate_sum(numbers)
    #         print(f"Сума елементів списку: {total}")
    #     except ValueError as e:
    #         print(f"Помилка: {e}")

    # to commit


def display_board(board):
    print(board[7] + '|' + board[8] + '|' + board[9])
    print('-----')
    print(board[4] + '|' + board[5] + '|' + board[6])
    print('-----')
    print(board[1] + '|' + board[2] + '|' + board[3])

def player_input():
    player1choice = ''
    while player1choice != 'X' and player1choice != 'O':
        player1choice = input('Does Player 1 want to be X or O?\n')
        player1choice = player1choice.upper()
    print(f'Player 1 has chosen to be {player1choice}')
    if player1choice == 'X':
        player2choice = 'O'
        print(f'Player 2 will be {player2choice}')
    else:
        player2choice = 'X'
        print(f'Player 2 will be {player2choice}')
    return player1choice, player2choice

def place_marker(board, marker, position):
    board[position] = marker

def win_check(board, mark):
    if board[7] == board[8] == board[9] and board[7] == mark:
        return True
    elif board[4] == board[5] == board[6] and board[4] == mark:
        return True
    elif board[1] == board[2] == board[3] and board[1] == mark:
        return True
    elif board[7] == board[4] == board[1] and board[1] == mark:
        return True
    elif board[8] == board[5] == board[2] and board[2] == mark:
        return True
    elif board[9] == board[6] == board[3] and board[3] == mark:
        return True
    elif board[7] == board[5] == board[3] and board[3] == mark:
        return True
    elif board[1] == board[5] == board[9] and board[1] == mark:
        return True
    else:
        return False

def full_board_check(board):
    return board[1] != ' ' and board[2] != ' ' and board[3] != ' ' and board[4] != ' ' and board[5] != ' ' \
           and board[6] != ' ' and board[7] != ' ' and board[8] != ' ' and board[9] != ' '

def check_tie(board, marker1, marker2):
    if full_board_check(board) and not win_check(board, marker1) or \
            full_board_check(board) and not win_check(board, marker2):
        return True
    else:
        return False

def space_check(board, position):
    return board[position] == ' '

def player_choice(board):
    pos = input('What space would you like to place your marker in? ')
    while pos != '1' and pos != '2' and pos != '3' and pos != '4' and pos != '5' and pos != '6' and \
            pos != '7' and pos != '8' and pos != '9':
        print('Invalid input, please pick a number from 1 to 9')
        print('')
        pos = input('What space would you like to place your marker in? ')
    if space_check(board, int(pos)):
        return int(pos)
    else:
        return 'Error. Space already filled'

def replay():
    again = input('Do you want to play again? (Yes/No) ').lower()
    while again != 'yes' and again != 'no':
        print('Invalid input, please enter either "yes" or "no"')
        again = input('Do you want to play again? (Yes/No) ').lower()
    if again == 'yes':
        return True
    else:
        return False

print('Welcome to Tic Tac Toe!')
print('The board follows the same layout as the number pad. '
      'Enter the space you want to put your marker in with the number pad.')
print('The player who is X will go first')
print('')
game = True
win = False
switcher = 'X'
# Game Setup
player1, player2 = player_input()
board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
print('\n')
while game:
    if switcher == 'X':
        if player1 == 'X':
            print("Player 1's turn")
        else:
            print("Player 2's turn")
    else:
        if player1 == 'O':
            print("Player 1's turn")
        else:
            print("Player 2's turn")
    display_board(board)
    move = player_choice(board)
    while move == 'Error. Space already filled':
        print('Space already filled, please pick another space')
        move = player_choice(board)
    if switcher == 'X':
        if player1 == 'X':
            place_marker(board, player1, move)  # Player 1 Move if X
        else:
            place_marker(board, player2, move)  # Player 2 Move if X
    else:
        if player1 == 'O':
            place_marker(board, player1, move)  # Player 1 Move if O
        else:
            place_marker(board, player2, move)  # Player 2 Move if O
    if switcher == 'X':
        switcher = 'O'
    else:
        switcher = 'X'
    print('\n')
    if not check_tie(board, player1, player2):  # If there is no tie
        if win_check(board, player1):
            print('Player 1 wins!')
            win = True
            display_board(board)
        elif win_check(board, player2):
            print('Player 2 wins!')
            win = True
            display_board(board)
    else:
        print('Tie! No one wins.')
        display_board(board)
        if not replay():
            break
        else:  # Remakes the board and asks for mark assignment again for rematch
            board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
            win = False
            print('\n'*50)
            player1, player2, = player_input()
    if win:  # Win check and check if players want a rematch
        if not replay():
            break
        else:  # Remakes the board and asks for mark assignment again for rematch
            board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
            win = False
            print('\n'*50)
            switcher = 'X'
            player1, player2, = player_input()


##좀 복잡해 보이기는 하지만 다음과 같이 하였다.
##[[7], [3, 8], [8, 1, 0]] 이 주어졌다면.
##top=[7]로 우선 초기화하고 이중for문으로
##[10,15]를 만들고 top에 넣는다.
##[18,max(11,16),15] 즉,[18,16,15]를 만들고 top에 넣고.. 를 반복하고
##return에서 가장 큰 값만을 반환한다.

def solution(triangle):
    top = triangle[0]
    for depth in range(1,len(triangle)):
        bottom = [0 for _ in range(depth+1)]
        for i in range(depth+1):
            if i == 0: bottom[0] = top[0]+triangle[depth][0]
            elif i == depth: bottom[depth] = top[depth-1]+triangle[depth][depth]
            else:
                bottom[i] = max(top[i-1]+triangle[depth][i],top[i]+triangle[depth][i])
        #print(bottom)
        top = bottom

    return max(top)

##정확성: 64.3
##효율성: 35.7
##합계: 100.0 / 100.0


from pathlib import Path
import sys

parent_dir = Path(__file__).parent
file_name = Path(__file__).stem

sys.stdin = open(f"{parent_dir}\{file_name} input.txt")
input = sys.stdin.readline

from collections import deque

T = int(input())
for test_case in range(1, T + 1):
    N, M = map(int, input().split())
    Q = deque([int(n) for n in input().split()])

    for _ in range(M):
        # Q.append(Q.popleft())
        Q.rotate(-1)

    print("#{} {}".format(test_case, Q[0]))


from celery import shared_task
from celery.task import task
from django.contrib.auth.models import User
from celery import Celery
from django.core.mail import send_mail
from django_celery_beat.models import PeriodicTask
from django.core.exceptions import ValidationError
from datetime import timedelta
import datetime
import json

from .models import Todo, CrontabSchedule, Settings

app = Celery()

app.conf.timezone = 'Europe/Vilnius'

@shared_task
def send_welcome_word(user, email, password):
    subject = (f"Welcome onboard {user}")
    body = f"""Thank You for registering on Todo App, {user}! \n
        No more missed or forgotten tasks for job, leisure, travel or anything!
        You can now track your daily activities, organize in groups and get daily reminders.\n
        Your login date is:
        Username: {user}
        Password: {password}\n
        You will receive tasks reminders to this email address as well \n\n
        Todo App Team
    """
    send_mail(subject,
    body,
    'my.todo.apl@gmail.com',
    [email])
    return None

@shared_task
def one_off_task_reminder(subject, body, email):
    send_mail(subject,
    body,
    'my.todo.apl@gmail.com',
    [email])
    return None

@shared_task
def delete_reminder(task_id):
    crontabs = CrontabSchedule.objects.all()
    for cron in crontabs:
        if cron.name.startswith("id:"+str(task_id)):
            cron.delete()

@app.task
def delete_old_tasks():
    users = User.objects.all()
    for user in users:
        interval = Settings.objects.get(user=user)
        todos = Todo.objects.filter(author=user, completed=True, created__lt=(datetime.date.today() - datetime.timedelta(days=interval.interval)))
        if todos.exists():
            for todo in todos:
                todo.delete()

@app.task
def daily_evening_reminder():
    users = User.objects.all()
    for user in users:
        list_of_task = []
        todos = Todo.objects.filter(author=user, completed=False, created=datetime.datetime.today())
        for todo in todos:
            list_of_task.append(todo.title)
        nl = '\n'
        subject = (f"Hey {user}. You have uncompleted tasks left for today!")
        msg = (f"Don't forget about your uncompleted today's tasks:\n{nl.join(map(str, list_of_task))}")
        if len(list_of_task)>0:
            send_mail(subject,
            msg,
            'my.todo.apl@gmail.com',
            [user.email])

@app.task
def daily_morning_reminder():
    users = User.objects.all()
    for user in users:
        list_of_task = []
        todos = Todo.objects.filter(author=user, completed=False, created=datetime.datetime.today())
        for todo in todos:
            list_of_task.append(todo.title)
        nl = '\n'
        subject = (f"Hey {user}. You have tasks waiting for you today!")
        msg = (f"For today your tasks are:\n{nl.join(map(str, list_of_task))}")
        if len(list_of_task)>0:
            send_mail(subject,
            msg,
            'my.todo.apl@gmail.com',
            [user.email])

def custom_reminder(reminder_time, reminder_date, user, task_id, email, title, on_off):
    reminder_hour = reminder_time[0:2]
    reminder_minute = reminder_time[3:5]
    reminder_month = reminder_date[5:7]
    reminder_day = reminder_date[8:10]
    if on_off == True:
        schedule, _ = CrontabSchedule.objects.get_or_create(
            minute=int(reminder_minute),
            hour=int(reminder_hour),
            day_of_week='*',
            day_of_month=int(reminder_day),
            month_of_year=int(reminder_month),
            timezone='Europe/Vilnius',
            name='id'+':'+str(task_id)+'_'+'reminder'+'_'+str(user)+'_'+str(reminder_month)+'_'+str(reminder_day)+'_'+str(reminder_hour)+':'+str(reminder_minute)
            )
        try:
            PeriodicTask.objects.create(
                crontab=schedule,
                name='id'+':'+str(task_id)+'_'+'reminder'+'_'+str(user)+'_'+str(reminder_month)+'_'+str(reminder_day)+'_'+str(reminder_hour)+':'+str(reminder_minute),
                task='todo_items.tasks.one_off_task_reminder',
                args=json.dumps([title, 'This is a reminder for your task!', email]),
                one_off=True,
                )
        except ValidationError:
            PeriodicTask.objects.get(
                crontab=schedule,
                name='id'+':'+str(task_id)+'_'+'reminder'+'_'+str(user)+'_'+str(reminder_month)+'_'+str(reminder_day)+'_'+str(reminder_hour)+':'+str(reminder_minute),
                task='todo_items.tasks.one_off_task_reminder',
                args=json.dumps([title, 'This is a reminder for your task!', email]),
                one_off=True,
                )
    if on_off == False:
        try:
            periodic_task = PeriodicTask.objects.get(name='id'+':'+str(task_id)+'_'+'Reminder'+'_'+str(user)+'_'+str(reminder_month)+'_'+str(reminder_day)+'_'+str(reminder_hour)+':'+str(reminder_minute))
            periodic_task.delete()
        except:
            pass


from matplotlib import pyplot as plt
from collections import deque
from threading import Lock, Thread
import myo
import time
import numpy as np
import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from socket import socket, AF_INET, SOCK_DGRAM
from statistics import mode
from scipy.spatial import distance

emg_train=np.array([[] for i in range(8)])

def queue_init(dim,q_length):   #dim x lengthのキューを作成
    tmp=deque(maxlen=q_length)
    a=0
    if dim==2:
        a=[0,0]
    for i in range(q_length):
        tmp.append(a)
    return tmp

finger_2d = queue_init(2,10)    #直近10回のデータサンプルの2次元座標を保存
finger_predict = queue_init(1,10)   #上の座標に対応する直近10回のクラス判別結果を保存

testdata_2d_rec=[]  #評価タスク時のデータを格納

finger_label=[]
emg_features=[]
lda_finger=LinearDiscriminantAnalysis(n_components=2)   #線形判別分析の学習器作成

count=0
ovr_l = 20  #クラス判別に使用するデータサンプルのオーバーラップ分は20sample
win_l = 40  #解析窓の長さは40sample分

fps=0.001   #fpsというより、視覚フィードバックの図の更新頻度(とりあえず0.001s感覚でフィードバックに関するwhile部分が動作します)

queue_len=6
do_record=True

class_f = 9  # 学習データの動作クラス数(使用データに合わせて変更してください)

ax1=0    #pltのリアルタイムプロット用のグローバル変数(0は仮で代入)(変更不要)
scat_finger=0

def update_plot(scat1,ax1): #
    tmp = np.array([x for x in list(finger_2d)])
    scat1.set_offsets(tmp)
    label_ = ["グー", "人差し指","中薬指","小指","パー","ピース","内屈","外屈","無動作"]
    ax1.set_title("finger motion : " + label_[finger_predict[-1]],fontname="MS Gothic")
    plt.pause(fps)

class EmgCollector(myo.DeviceListener): #Myo armbandを用いた近電位記録に関数するクラス

  def __init__(self, n):
    self.idle=False  #Trueの間計測を行う
    self.lock = Lock()
    self.time_pre=0
    self.emg_data_queue = deque(maxlen=win_l)  #クラス判別に使用するデータが格納されるキュー
    self.start_time=time.time() #計測時間の記録に使用
    #以下2つはなぜ入れたか覚えてないです
    self.sampleamount = 0
    self.n = n

  def get_emg_queue(self):  #呼び出し時に各センサの筋電位データを要素数8のリストで返す
    with self.lock:
      return list(self.emg_data_queue)  #リスト化が必要

  def on_connected(self, event):
    event.device.stream_emg(True)

  def start(self):  #筋電位のリアルタイム判別開始に呼び出す
      for i in range(ovr_l):
        self.emg_data_queue.append((0, [0,0,0,0,0,0,0,0]))  #一番最初のデータサンプルだけ解析窓に含まれるデータ半分が0になる
      self.idle=True

  def predict(self):    #クラスの予測
      tmp=self.get_emg_queue()  #その段階での解析窓を切り出し
      tmp=np.array([x[1] for x in tmp]).T
      test_features=[feature_calc(tmp, win_l)]  #特徴量を計算してリストを得る
      df = pd.DataFrame(test_features)
      global finger_predict,wrist_predict,testdata_2d_rec   #いらない変数があるかもしれない
      lda_fingerresult=int(lda_finger.predict(df.values))   #特徴量のデータからクラス判別を実施
      finger_predict.append(lda_fingerresult)   #判別結果のラベルを保存
      a= lda_finger.transform(df.values)    #先ほどの特徴量を2次元座標に変換
      global finger_2d
      finger_2d.append(a[0])    #座標データの保存
      testdata_2d_rec.append([lda_fingerresult, a[0][0], a[0][1]])  #記録データ用に判別クラス、座標を保存

  def end(self):
      self.idle=False

  def on_emg(self, event):
    with self.lock:
        if self.idle:
            tmp=event.emg
            self.sampleamount+=1
            self.emg_data_queue.append((event.timestamp, event.emg))

            if self.sampleamount==ovr_l:
                self.sampleamount=0
                thread = Thread(target=self.predict)
                thread.start()
                thread.join(0.0001)

def Record():   #Unityとの同期確認
    print("wait msg")
    ADDR = ''
    PORT = 50004 # 受信ポート
    M_SIZE = 1024
    msg=0
    rcv = socket(AF_INET, SOCK_DGRAM)
    VR_test_start = 0
    rcv.bind((ADDR, PORT))
    while True:
        msg, address = rcv.recvfrom(8192)
        msg_=int(msg[0] - 48)

        if VR_test_start==0 and msg_==1:
            VR_test_start=1
            global testdata_2d_rec
            testdata_2d_rec=[]
            print("msg received record start")

        if VR_test_start==1 and msg_==9:
            VR_test_start=0
            print("msg received record finished")
            df = pd.DataFrame(testdata_2d_rec)
            df.to_csv(msg.decode()[1:]+".csv")  # Unity側で設定した名前で記録したcsvデータを保存

class Train(object):    #視覚フィードバックに関するクラス
    def __init__(self,listener):
        self.n = listener.n
        self.listener = listener

    def Show_emg_fb(self):
        ADDR = '127.0.0.1'
        PORT_TO = 50007  # 送信ポート
        M_SIZE = 1024
        global ax1,finger_2d,scat_finger,finger_predict
        snd = socket(AF_INET, SOCK_DGRAM)
        self.listener.start()
        while True:
            update_plot(scat_finger,ax1)
            msg = str(finger_predict[-1]) + "0"
            snd.sendto(msg.encode(),(ADDR,PORT_TO))

    def Show_emg_nfb(self,df,ff,fm):
        ADDR = '127.0.0.1'
        PORT_TO = 50007  # 送信ポート
        M_SIZE = 1024
        global finger_2d,finger_predict
        fig = plt.figure(figsize=(4, 12))
        ax1 = plt.subplot(111)
        snd = socket(AF_INET, SOCK_DGRAM)
        base_df = df
        features_finger = ff
        base2d_finger = fm

        basedata_finger = []  # 0番目から各クラスの重心

        for k in range(class_f):
            tmp = []
            tmp.append([base2d_finger[j][0] for j in range(len(base2d_finger)) if features_finger[j] == (k)])
            tmp.append([base2d_finger[j][1] for j in range(len(base2d_finger)) if features_finger[j] == (k)])
            basedata_finger.append(tmp)

        basedata_f_centers = []

        for i in range(class_f):
            tmp = []
            tmp.append(np.mean(basedata_finger[i][0]))
            tmp.append(np.mean(basedata_finger[i][1]))
            basedata_f_centers.append(tmp)

        # --ここまでが初回計測データからの重心計算と、使用ファイル読み込み
        # ここからが計測データの処理
        basedata_f_cov = []

        for i in range(class_f):
            tmp = np.cov(basedata_finger[i][0], basedata_finger[i][1])
            basedata_f_cov.append(np.linalg.pinv(tmp))

        label_ =["グー", "人差し指","中薬指","小指","パー","ピース","内屈","外屈","無動作"]
        self.listener.start()
        while True:
            msg=str(finger_predict[-1])+"0"
            snd.sendto(msg.encode(),(ADDR,PORT_TO))
            ax1.cla()
            ax1.set_title("finger motion : " + label_[finger_predict[-1]], fontname="MS Gothic")
            f_maharanobis=distance.mahalanobis(list(finger_2d[-1]),basedata_f_centers[finger_predict[-1]],basedata_f_cov[finger_predict[-1]])
            ax1.bar([1], f_maharanobis)
            ax1.set_ylim(0,8)
            plt.pause(0.00001)

def feature_calc(emg,win_l):    #特徴量計算を行う関数
    FEATURES = []
    for i in range(8):
        tmp = emg[i]
        FEATURES.append(np.mean(np.abs(tmp)))  # MAV

        FEATURES.append(np.var(tmp))  # VAR
        zero = 0
        for j in range(0, win_l - 1):
            if tmp[j] * tmp[j + 1] < 0:
                zero += 1

        FEATURES.append(zero)   #ZC

        diff = np.diff(tmp, n=1)    # WL
        FEATURES.append(np.sum(np.abs(diff)))
        # freq = np.abs(np.fft.fft(tmp))  # 周波数領域
        # FEATURES.append(np.max(freq))  # PKF
        # FEATURES.append(np.mean(freq))  # MKF

    return FEATURES

def main():
    finger_2d = queue_init(2,queue_len)
    finger_predict = queue_init(1,queue_len)
#-----------------------------------------
    myo.init()
    hub = myo.Hub()
    listener = EmgCollector(512)

    features_finger=[]
    features_wrist=[]
    with hub.run_in_background(listener.on_event):
        global lda_finger, lda_wrist
        print("training")
        df = pd.read_csv('nkn_r_base0_.csv', header=0, index_col=0)
        features_finger = df.values[:,-1]
        df=df.values[:,0:-1]
        finger_motion = lda_finger.fit(df, features_finger).transform(df)

        fb = input("start Train with FeedBack? y/n:")
        thread = Thread(target=Record)
        thread.start()
        if fb == "y":
            fig = plt.figure(figsize=(18, 12))
            global ax1, ax2, scat_finger, scat_wrist
            ax1 = plt.subplot(111)
            label_ = ["グー", "人差し指","中薬指","小指","パー","ピース","内屈","外屈","無動作"]
            for k in range(class_f):  # 手描画
                tmp = []
                tmp.append([finger_motion[j][0] for j in range(len(finger_motion)) if features_finger[j] == (k)])
                tmp.append([finger_motion[j][1] for j in range(len(finger_motion)) if features_finger[j] == (k)])
                tmp = np.array(tmp)
                scat_finger = ax1.scatter(tmp[0], tmp[1], label=label_[k], cmap='viridis', edgecolor='blacK')
            ax1.set_title("finger_pattern")
            ax1.legend(labels=label_, fontsize=12,prop={"family":"MS Gothic"})
            scat_finger = ax1.scatter(0, 0, label="current", c="crimson", s=100,
                                      marker="X")  # 空撃ちすることでリアルタイム分類時のset_datasに備える

            plt.pause(0.05)
            Train(listener).Show_emg_fb()
        if fb =="n":
            Train(listener).Show_emg_nfb(df,features_finger,finger_motion)

if __name__ == '__main__':  #一応ここから開始
  main()


import openpyxl
import datetime
from openpyxl.utils.cell import get_column_letter

workbook = openpyxl.load_workbook(filename="testMKTCAPCashBalanceCPJAN18.xlsx")
sheet = workbook["CashBalanceJAN18"]
number_rows = sheet.max_row
number_columns = sheet.max_column
CURRENTDATE = "03/09/2021"
#######################

def findClosePrice():

    for i in range(1,number_rows):
        print("---------------finding ClosePrice----------------")
        try:
            tickerName = str(sheet[("A"+str(i+1))].value)
            endDateValue = str(sheet[("D"+str(i+1))].value)[:10]
            endDate = datetime.datetime.strptime(endDateValue, '%Y-%m-%d')
            # endDateM5 = endDate + datetime.timedelta(days=-5)

            strEndDate = endDate.strftime('%d/%m/%Y')
            # strEndDateM5 = endDateM5.strftime('%d/%m/%Y')
            currentDateOBJ = datetime.datetime.strptime(CURRENTDATE, "%d/%m/%Y")
            endDateOBJ = datetime.datetime.strptime(strEndDate, "%d/%m/%Y")
            print(tickerName + " cashBalance endDate " + strEndDate)

            tickerFileOpen = "cleanDate" + tickerName + ".xlsx"
            print(".....Opening '" + tickerFileOpen + "'....")

            tickerWorkBook = openpyxl.load_workbook(filename=tickerFileOpen)
            tickerSheet = tickerWorkBook.active
            tickerNumber_rows = tickerSheet.max_row
            # tickerNumber_columns = tickerSheet.max_column
            loopIndex = 0
            foundDate = False
            while foundDate == False:
                #if endDate is in the future, break the loop || loop more than 6 times
                if foundDate == True or currentDateOBJ < endDateOBJ or loopIndex > 6:
                    break

                k = 0
                while k < tickerNumber_rows:
                    tickerDate = str(tickerSheet["A"+str(k+1)].value)
                    strEndDateValue = strEndDate
                    if loopIndex > 0:
                        tempDate = datetime.datetime.strptime(strEndDate, '%d/%m/%Y') + datetime.timedelta(days=-loopIndex)
                        strEndDateValue = tempDate.strftime('%d/%m/%Y')

                        # strEndDate = str(tickerSheet["A"+str(k+1)].value)[:10]
                        # tickerDateDateTime = datetime.datetime.strptime(tickerDate, '%Y-%m-%d')
                        # tickerDateMinus1 = tickerDateDateTime + datetime.timedelta(days=-1)
                        # tickerDate = tickerDateMinus1.strftime('%d/%m/%Y')

                    # tickerCell = "A" + str(k+1)
                    # print('this is tickerdate', tickerDate, ' strEndDate', strEndDate)
                    if tickerDate == strEndDateValue:
                        foundDate = True

                        # print('Found EndDate at Cell Number'+tickerCell)
                        tickerDateM7 = str(tickerSheet["A"+str(k-6)].value)
                        tickerDateM6 = str(tickerSheet["A"+str(k-5)].value)
                        tickerDateM5 = str(tickerSheet["A"+str(k-4)].value)
                        tickerDateM4 = str(tickerSheet["A"+str(k-3)].value)
                        tickerDateM3 = str(tickerSheet["A"+str(k-2)].value)
                        tickerDateM2 = str(tickerSheet["A"+str(k-1)].value)
                        tickerDateM1 = str(tickerSheet["A"+str(k)].value)
                        tickerDate0 = str(tickerSheet["A"+str(k+1)].value)
                        tickerDateP1 = str(tickerSheet["A"+str(k+2)].value)
                        tickerDateP2 = str(tickerSheet["A"+str(k+3)].value)
                        tickerDateP3 = str(tickerSheet["A"+str(k+4)].value)
                        tickerDateP4 = str(tickerSheet["A"+str(k+5)].value)
                        tickerDateP5 = str(tickerSheet["A"+str(k+6)].value)

                        tickerCPEndDateM7 = str(tickerSheet["E"+str(k-6)].value)
                        tickerCPEndDateM6 = str(tickerSheet["E"+str(k-5)].value)
                        tickerCPEndDateM5 = str(tickerSheet["E"+str(k-4)].value)
                        tickerCPEndDateM4 = str(tickerSheet["E"+str(k-3)].value)
                        tickerCPEndDateM3 = str(tickerSheet["E"+str(k-2)].value)
                        tickerCPEndDateM2 = str(tickerSheet["E"+str(k-1)].value)
                        tickerCPEndDateM1 = str(tickerSheet["E"+str(k)].value)
                        tickerCPEndDate = str(tickerSheet["E"+str(k+1)].value)
                        tickerCPEndDateP1 = str(tickerSheet["E"+str(k+2)].value)
                        tickerCPEndDateP2 = str(tickerSheet["E"+str(k+3)].value)
                        tickerCPEndDateP3 = str(tickerSheet["E"+str(k+4)].value)
                        tickerCPEndDateP4 = str(tickerSheet["E"+str(k+5)].value)
                        tickerCPEndDateP5 = str(tickerSheet["E"+str(k+6)].value)

                        sheet["F"+str(i+1)] = float(tickerCPEndDateM7) if tickerCPEndDateM7 != "None" else "DNE"
                        sheet["G"+str(i+1)] = float(tickerCPEndDateM6) if tickerCPEndDateM6 != "None" else "DNE"
                        sheet["H"+str(i+1)] = float(tickerCPEndDateM5) if tickerCPEndDateM5 != "None" else "DNE"
                        sheet["I"+str(i+1)] = float(tickerCPEndDateM4) if tickerCPEndDateM4 != "None" else "DNE"
                        sheet["J"+str(i+1)] = float(tickerCPEndDateM3) if tickerCPEndDateM3 != "None" else "DNE"
                        sheet["K"+str(i+1)] = float(tickerCPEndDateM2) if tickerCPEndDateM2 != "None" else "DNE"
                        sheet["L"+str(i+1)] = float(tickerCPEndDateM1) if tickerCPEndDateM1 != "None" else "DNE"
                        sheet["M"+str(i+1)] = float(tickerCPEndDate) if tickerCPEndDate != "None" else "DNE"
                        sheet["N"+str(i+1)] = float(tickerCPEndDateP1) if tickerCPEndDateP1 != "None" else "DNE"
                        sheet["O"+str(i+1)] = float(tickerCPEndDateP2) if tickerCPEndDateP2 != "None" else "DNE"
                        sheet["P"+str(i+1)] = float(tickerCPEndDateP3) if tickerCPEndDateP3 != "None" else "DNE"
                        sheet["Q"+str(i+1)] = float(tickerCPEndDateP4) if tickerCPEndDateP4 != "None" else "DNE"
                        sheet["R"+str(i+1)] = float(tickerCPEndDateP5) if tickerCPEndDateP5 != "None" else "DNE"

                        print(tickerName + " " + tickerDateM7 + " " + tickerCPEndDateM7)
                        print(tickerName + " " + tickerDateM6 + " " + tickerCPEndDateM6)
                        print(tickerName + " " + tickerDateM5 + " " + tickerCPEndDateM5)
                        print(tickerName + " " + tickerDateM4 + " " + tickerCPEndDateM4)
                        print(tickerName + " " + tickerDateM3 + " " + tickerCPEndDateM3)
                        print(tickerName + " " + tickerDateM2 + " " + tickerCPEndDateM2)
                        print(tickerName + " " + tickerDateM1 + " " + tickerCPEndDateM1)
                        print("###")
                        print("END CashBalance " + tickerName + " " + tickerDate0 + " " + tickerCPEndDate)
                        print("###")
                        print(tickerName + " " + tickerDateP1 + " " + tickerCPEndDateP1)
                        print(tickerName + " " + tickerDateP2 + " " + tickerCPEndDateP2)
                        print(tickerName + " " + tickerDateP3 + " " + tickerCPEndDateP3)
                        print(tickerName + " " + tickerDateP4 + " " + tickerCPEndDateP4)
                        print(tickerName + " " + tickerDateP5 + " " + tickerCPEndDateP5)

                        workbook.save("testMKTCAPCashBalanceCPJAN18.xlsx")

                        break
                    k += 1

                loopIndex += 1
                print("-------------------------------")

        except Exception as e:
            print(e)
            print("Not a DATE")
            break

    workbook.save("testMKTCAPCashBalanceCPJAN18.xlsx")

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

def findOpenPrice():

    for i in range(1, number_rows):
        print("---------------finding OpenPrice----------------")
        try:
            tickerName = str(sheet[("A"+str(i+1))].value)
            endDateValue = str(sheet[("D"+str(i+1))].value)[:10]
            endDate = datetime.datetime.strptime(endDateValue, '%Y-%m-%d')

            strEndDate = endDate.strftime('%d/%m/%Y')

            currentDateOBJ = datetime.datetime.strptime(CURRENTDATE, "%d/%m/%Y")
            endDateOBJ = datetime.datetime.strptime(strEndDate, "%d/%m/%Y")
            print(tickerName + " cashBalance endDate " + strEndDate)

            tickerFileOpen = "cleanDate" + tickerName + ".xlsx"
            print(".....Opening '" + tickerFileOpen + "'....")

            tickerWorkBook = openpyxl.load_workbook(filename=tickerFileOpen)
            tickerSheet = tickerWorkBook.active
            tickerNumber_rows = tickerSheet.max_row

            loopIndex = 0
            foundDate = False
            while foundDate == False:
                #if endDate is in the future, break the loop || loop more than 6 times

                if foundDate == True or currentDateOBJ < endDateOBJ or loopIndex > 6:
                    break

                k = 0
                while k < tickerNumber_rows:
                    tickerDate = str(tickerSheet["A"+str(k+1)].value)
                    strEndDateValue = strEndDate
                    if loopIndex > 0:
                        tempDate = datetime.datetime.strptime(strEndDate, "%d/%m/%Y") + datetime.timedelta(days=-loopIndex)
                        strEndDateValue = tempDate.strftime('%d/%m/%Y')

                    if tickerDate == strEndDateValue:
                        foundDate = True
                        tickerDateM7 = str(tickerSheet["A"+str(k-6)].value)
                        tickerDateM6 = str(tickerSheet["A"+str(k-5)].value)
                        tickerDateM5 = str(tickerSheet["A"+str(k-4)].value)
                        tickerDateM4 = str(tickerSheet["A"+str(k-3)].value)
                        tickerDateM3 = str(tickerSheet["A"+str(k-2)].value)
                        tickerDateM2 = str(tickerSheet["A"+str(k-1)].value)
                        tickerDateM1 = str(tickerSheet["A"+str(k)].value)
                        tickerDate0 = str(tickerSheet["A"+str(k+1)].value)
                        tickerDateP1 = str(tickerSheet["A"+str(k+2)].value)
                        tickerDateP2 = str(tickerSheet["A"+str(k+3)].value)
                        tickerDateP3 = str(tickerSheet["A"+str(k+4)].value)
                        tickerDateP4 = str(tickerSheet["A"+str(k+5)].value)
                        tickerDateP5 = str(tickerSheet["A"+str(k+6)].value)

                        tickerOPEndDateM7 = str(tickerSheet["B"+str(k-6)].value)
                        tickerOPEndDateM6 = str(tickerSheet["B"+str(k-5)].value)
                        tickerOPEndDateM5 = str(tickerSheet["B"+str(k-4)].value)
                        tickerOPEndDateM4 = str(tickerSheet["B"+str(k-3)].value)
                        tickerOPEndDateM3 = str(tickerSheet["B"+str(k-2)].value)
                        tickerOPEndDateM2 = str(tickerSheet["B"+str(k-1)].value)
                        tickerOPEndDateM1 = str(tickerSheet["B"+str(k)].value)
                        tickerOPEndDate = str(tickerSheet["B"+str(k+1)].value)
                        tickerOPEndDateP1 = str(tickerSheet["B"+str(k+2)].value)
                        tickerOPEndDateP2 = str(tickerSheet["B"+str(k+3)].value)
                        tickerOPEndDateP3 = str(tickerSheet["B"+str(k+4)].value)
                        tickerOPEndDateP4 = str(tickerSheet["B"+str(k+5)].value)
                        tickerOPEndDateP5 = str(tickerSheet["B"+str(k+6)].value)

                        sheet["F"+str(i+1)] = float(tickerOPEndDateM7) if tickerOPEndDateM7 != "None" else "DNE"
                        sheet["G"+str(i+1)] = float(tickerOPEndDateM6) if tickerOPEndDateM6 != "None" else "DNE"
                        sheet["H"+str(i+1)] = float(tickerOPEndDateM5) if tickerOPEndDateM5 != "None" else "DNE"
                        sheet["I"+str(i+1)] = float(tickerOPEndDateM4) if tickerOPEndDateM4 != "None" else "DNE"
                        sheet["J"+str(i+1)] = float(tickerOPEndDateM3) if tickerOPEndDateM3 != "None" else "DNE"
                        sheet["K"+str(i+1)] = float(tickerOPEndDateM2) if tickerOPEndDateM2 != "None" else "DNE"
                        sheet["L"+str(i+1)] = float(tickerOPEndDateM1) if tickerOPEndDateM1 != "None" else "DNE"
                        sheet["M"+str(i+1)] = float(tickerOPEndDate) if tickerOPEndDate != "None" else "DNE"
                        sheet["N"+str(i+1)] = float(tickerOPEndDateP1) if tickerOPEndDateP1 != "None" else "DNE"
                        sheet["O"+str(i+1)] = float(tickerOPEndDateP2) if tickerOPEndDateP2 != "None" else "DNE"
                        sheet["P"+str(i+1)] = float(tickerOPEndDateP3) if tickerOPEndDateP3 != "None" else "DNE"
                        sheet["Q"+str(i+1)] = float(tickerOPEndDateP4) if tickerOPEndDateP4 != "None" else "DNE"
                        sheet["R"+str(i+1)] = float(tickerOPEndDateP5) if tickerOPEndDateP5 != "None" else "DNE"

                        print(tickerName + " " + tickerDateM7 + " " + tickerOPEndDateM7)
                        print(tickerName + " " + tickerDateM6 + " " + tickerOPEndDateM6)
                        print(tickerName + " " + tickerDateM5 + " " + tickerOPEndDateM5)
                        print(tickerName + " " + tickerDateM4 + " " + tickerOPEndDateM4)
                        print(tickerName + " " + tickerDateM3 + " " + tickerOPEndDateM3)
                        print(tickerName + " " + tickerDateM2 + " " + tickerOPEndDateM2)
                        print(tickerName + " " + tickerDateM1 + " " + tickerOPEndDateM1)
                        print("###")
                        print("END CashBalance " + tickerName + " " + tickerDate0 + " " + tickerOPEndDate)
                        print("###")
                        print(tickerName + " " + tickerDateP1 + " " + tickerOPEndDateP1)
                        print(tickerName + " " + tickerDateP2 + " " + tickerOPEndDateP2)
                        print(tickerName + " " + tickerDateP3 + " " + tickerOPEndDateP3)
                        print(tickerName + " " + tickerDateP4 + " " + tickerOPEndDateP4)
                        print(tickerName + " " + tickerDateP5 + " " + tickerOPEndDateP5)
                        workbook.save("CashBalanceOPJAN18.xlsx")
                        break
                    k += 1

                loopIndex += 1
                print("-------------------------------")
        except Exception as e:
            print(e)
            print("Not a DATE")
            break
    workbook.save("CashBalanceOPJAN18.xlsx")

def addMktCap():

    for i in range(1, number_rows):
        print("--------------adding Market Capitalization--------------")
        try:
            tickerName = str(sheet[("A"+str(i+1))].value)
            endDateValue = str(sheet[("D"+str(i+1))].value)[:10]
            endDate = datetime.datetime.strptime(endDateValue, '%Y-%m-%d')
            # endDateM5 = endDate + datetime.timedelta(days=-5)

            strEndDate = endDate.strftime('%d/%m/%Y')
            # strEndDateM5 = endDateM5.strftime('%d/%m/%Y')
            currentDateOBJ = datetime.datetime.strptime(CURRENTDATE, "%d/%m/%Y")
            endDateOBJ = datetime.datetime.strptime(strEndDate, "%d/%m/%Y")
            print(tickerName + " cashBalance endDate " + strEndDate)

            tickerFileOpen = "cleanDate" + tickerName + ".xlsx"
            print(".....Opening '" + tickerFileOpen + "'....")

            tickerWorkBook = openpyxl.load_workbook(filename=tickerFileOpen)
            tickerSheet = tickerWorkBook.active
            tickerNumber_rows = tickerSheet.max_row
            # tickerNumber_columns = tickerSheet.max_column
            loopIndex = 0
            foundDate = False
            while foundDate == False:
                #if endDate is in the future, break the loop || loop more than 6 times
                if foundDate == True or currentDateOBJ < endDateOBJ or loopIndex > 6:
                    break

                k = 0
                while k < tickerNumber_rows:
                    tickerDate = str(tickerSheet["A"+str(k+1)].value)
                    strEndDateValue = strEndDate
                    if loopIndex > 0:
                        tempDate = datetime.datetime.strptime(strEndDate, '%d/%m/%Y') + datetime.timedelta(days=-loopIndex)
                        strEndDateValue = tempDate.strftime('%d/%m/%Y')

                        # strEndDate = str(tickerSheet["A"+str(k+1)].value)[:10]
                        # tickerDateDateTime = datetime.datetime.strptime(tickerDate, '%Y-%m-%d')
                        # tickerDateMinus1 = tickerDateDateTime + datetime.timedelta(days=-1)
                        # tickerDate = tickerDateMinus1.strftime('%d/%m/%Y')

                    # tickerCell = "A" + str(k+1)
                    # print('this is tickerdate', tickerDate, ' strEndDate', strEndDate)
                    if tickerDate == strEndDateValue:
                        foundDate = True

                        # print('Found EndDate at Cell Number'+tickerCell)

                        tickerDate0 = str(tickerSheet["A"+str(k+1)].value)

                        tickerMktCap = str(tickerSheet["H"+str(k+1)].value)

                        sheet["AQ"+str(i+1)] = float(tickerMktCap) if tickerMktCap != "NaN" else "DNE"

                        print("###")
                        print("Market Cap of " + tickerName + " on " + tickerDate0 + " is " + tickerMktCap)
                        print("###")

                        workbook.save("testMKTCAPCashBalanceCPJAN18.xlsx")
                        break
                    k += 1

                loopIndex += 1
                print("-------------------------------")

        except Exception as e:
            print(e)
            print("An error occured")
            break

    workbook.save("testMKTCAPCashBalanceCPJAN18.xlsx")

def addFreeFloat():

    for i in range(1, number_rows):
        print("--------------adding Free Float Percentage--------------")
        try:
            tickerName = str(sheet[("A"+str(i+1))].value)
            endDateValue = str(sheet[("D"+str(i+1))].value)[:10]
            endDate = datetime.datetime.strptime(endDateValue, '%Y-%m-%d')
            # endDateM5 = endDate + datetime.timedelta(days=-5)

            strEndDate = endDate.strftime('%d/%m/%Y')
            # strEndDateM5 = endDateM5.strftime('%d/%m/%Y')
            currentDateOBJ = datetime.datetime.strptime(CURRENTDATE, "%d/%m/%Y")
            endDateOBJ = datetime.datetime.strptime(strEndDate, "%d/%m/%Y")
            print(tickerName + " cashBalance endDate " + strEndDate)

            tickerFileOpen = "cleanDate" + tickerName + ".xlsx"
            print(".....Opening '" + tickerFileOpen + "'....")

            tickerWorkBook = openpyxl.load_workbook(filename=tickerFileOpen)
            tickerSheet = tickerWorkBook.active
            tickerNumber_rows = tickerSheet.max_row
            # tickerNumber_columns = tickerSheet.max_column
            loopIndex = 0
            foundDate = False
            while foundDate == False:
                #if endDate is in the future, break the loop || loop more than 6 times
                if foundDate == True or currentDateOBJ < endDateOBJ or loopIndex > 6:
                    break

                k = 0
                while k < tickerNumber_rows:
                    tickerDate = str(tickerSheet["A"+str(k+1)].value)
                    strEndDateValue = strEndDate
                    if loopIndex > 0:
                        tempDate = datetime.datetime.strptime(strEndDate, '%d/%m/%Y') + datetime.timedelta(days=-loopIndex)
                        strEndDateValue = tempDate.strftime('%d/%m/%Y')

                        # strEndDate = str(tickerSheet["A"+str(k+1)].value)[:10]
                        # tickerDateDateTime = datetime.datetime.strptime(tickerDate, '%Y-%m-%d')
                        # tickerDateMinus1 = tickerDateDateTime + datetime.timedelta(days=-1)
                        # tickerDate = tickerDateMinus1.strftime('%d/%m/%Y')

                    # tickerCell = "A" + str(k+1)
                    # print('this is tickerdate', tickerDate, ' strEndDate', strEndDate)
                    if tickerDate == strEndDateValue:
                        foundDate = True

                        # print('Found EndDate at Cell Number'+tickerCell)

                        tickerDate0 = str(tickerSheet["A"+str(k+1)].value)
                        tickerFloatSharesOutstanding = float(tickerSheet["J" + str(k+1)].value)
                        tickerCommonSharesOutstanding = float(tickerSheet["K"+str(k+1)].value)
                        tickerFreeFloatPercentage = "NaN"
                        if tickerFloatSharesOutstanding != "NaN" and tickerCommonSharesOutstanding != "NaN":
                            tickerFreeFloatPercentage = tickerFloatSharesOutstanding/tickerCommonSharesOutstanding

                        sheet["AR"+str(i+1)] = float(tickerFreeFloatPercentage) if tickerFreeFloatPercentage != "NaN" else "DNE"

                        print("###")
                        print("Free Float Percentage of " + tickerName + " on " + tickerDate0 + " is " + str(tickerFreeFloatPercentage))
                        print("###")

                        workbook.save("testMKTCAPCashBalanceCPJAN18.xlsx")
                        break
                    k += 1

                loopIndex += 1
                print("-------------------------------")

        except Exception as e:
            print(e)
            print("An error occured")
            break

    workbook.save("testMKTCAPCashBalanceCPJAN18.xlsx")

#########

getUserInput = False
while not getUserInput:
    userInput = input("cp / op / mktcap / ff / ccmktcapff ? ")
    if userInput == "cp":
        getUserInput = True
        findClosePrice()
    elif userInput == "op":
        getUserInput = True
        findOpenPrice()
    elif userInput =="mktcap":
        getUserInput = True
        addMktCap()
    elif userInput == "ff":
        getUserInput = True
        addFreeFloat()
    elif userInput == "exit":
        getUserInput = True
    elif userInput == "ccmktcapff":
        getUserInput = True
        findClosePrice()
        addMktCap()
        addFreeFloat()


import json
import datetime
import RPi.GPIO as GPIO
from time import sleep
from flask import Flask

BIND_ADDRESS = '0.0.0.0'
PORT = 5001

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

# Date to string converter
def date_to_str(o):
    if isinstance(o, datetime.datetime):
        return o.__str__()

def get_data():
    result = GPIO.input(4)
    if result == 1:
        result = False;
    elif result == 0:
        result = True;
    else:
        result = None

    datetime_now = datetime.datetime.now()
    data = {
            "time_stamp": datetime_now,
            "light": result
        }

    sleep(1)
    return data

app = Flask(__name__)

@app.route("/api/light")
def execute_route():
    try:
        json_data = json.dumps(get_data(), default = date_to_str)
        return json_data
    except Exception as err:
        print(f'[ Error ]: err')
        return { 'error': err}

if __name__ == '__main__':
    app.run(host=BIND_ADDRESS, port=PORT, debug=True)


import re
from datetime import datetime
from urllib.parse import quote

import requests

from server.extensions import config

def get_twitter_user_details(url: str):
    username = re.match(r"https://(www\.)?twitter\.com/([a-zA-Z0-9_]+)/?", url).group(2)
    try:
        response = requests.get(
            f"{config['FRONTEND_URL']}/api/twitter/users/{username}"
        )
        if response.status_code != 200:
            raise Exception("")
    except Exception as e:
        print(e)
        raise Exception("The Twitter user does not exist")

    return response.json()["user"]

def get_tweets(username: str, last_scanned: "datetime"):
    try:
        response = requests.get(
            f"{config['FRONTEND_URL']}/api/twitter/tweets/{username}?last_scanned={last_scanned.isoformat()}"
        )
        if response.status_code != 200:
            raise Exception("")
    except Exception as e:
        print(e)
        raise Exception("Please try again later")

    return list(
        map(
            lambda x: {
                "text": x.get("fullText"),
                "date": x.get("createdAt"),
                "url": f"https://twitter.com/user/status/{x.get('id')}",
            },
            response.json().get("tweets"),
        )
    )


# !/usr/bin/env python3
# -*- coding: utf-8 -*-

from Predictor import Predictor
from sklearn.neighbors import KNeighborsRegressor
#Used to get command line arguments
import sys
import os
#Used to check validity of date
from datetime import datetime, timedelta

from TFMLP import MLPR

import Utils
from Utils import fetch_data_from_api, plot_data

#Display usage information
def print_usage():
    print('Usage:\n')
    print('\tpython main.py <source type> <market> <currency pair> <period> <start time> <end time>')

    print('\t')
    print('\tsource type: K_NEIGHBORS_V1')
    print('\tsource type: MLPR_V1')
    print('\t')
    print('\tmarket: BITSTAMP')
    print('\t')
    print('\tcurrency pair: BTC_USD')
    print('\t')
    print('\tperiod: ONE_HOUR')
    print('\tperiod: ONE_DAY')
    print('\tperiod: WEEK')
    print('\t')
    print('\tstart time: NOW | 2017-02-09T08:00:00')
    print('\t')
    print('\tend time (optional): 2017-03-14T16:00:00')

#Main program
def main(args):
    if(len(args) != 5 and len(args) != 6):
        print("Invalid parameters count")
        print_usage()
        return

    print("-------------------------------------------------------------------")
    print ("Start time = %s" % datetime.now())
    print("\n");

    #API Client ID
    try:
        api_client_id = os.environ["API_CLIENT_ID"]
    except KeyError:
        print("Env. variable API_CLIENT_ID is not defined or is invalid!")
        print_usage()
        return

    #API Client secret
    try:
        api_client_secret = os.environ["API_CLIENT_SECRET"]
    except KeyError:
        print("Env. variable API_CLIENT_SECRET is not defined or is invalid!")
        print_usage()
        return

    #Source type
    pred_source = args[0].upper()

    #Market type
    market = args[1].upper()

    #Currency pair
    currency_pair = args[2].upper()

    #Period
    period = args[3].upper()

    # Start and end time
    if(args[4] == "NOW"):
        if(period == "ONE_HOUR"):
            startTime = datetime.today()
            # startTime = startTime - timedelta(hours=24 + 3)
            startTime = startTime - timedelta(hours=0)
            startTime = startTime.replace(minute=0, second=0)
            endTime = startTime + timedelta(hours=24*3) # 3 dni
            endTime = endTime.replace(minute=0, second=0)
        elif(period == "ONE_DAY"):
            startTime = datetime.today()
            # startTime = startTime - timedelta(days=14) # 16 trochu rozdielne
            startTime = startTime - timedelta(days=0)
            startTime = startTime.replace(hour=0, minute=0, second=0)
            endTime = startTime + timedelta(days=30*5) # 5mesiace
            endTime = endTime.replace(hour=0, minute=0, second=0)
        elif(period == "WEEK"):
            startTime = datetime.today()
            # startTime = startTime - timedelta(weeks=14)
            startTime = startTime - timedelta(weeks=0)
            while startTime.weekday() != 0: #0 for monday
                startTime -= timedelta(days=1)

            startTime = startTime.replace(hour=0, minute=0, second=0)
            endTime = startTime + timedelta(weeks=150)
            while endTime.weekday() != 0: #0 for monday
                endTime -= timedelta(days=1)
            endTime = endTime.replace(hour=0, minute=0, second=0)

            startTime += timedelta(days=1)
            endTime += timedelta(days=1)

        start = startTime.strftime("%Y-%m-%dT%H:%M:%S")
        end = endTime.strftime("%Y-%m-%dT%H:%M:%S")
    else:
        #Test validity of start date string
        try:
            datetime.strptime(args[4], '%Y-%m-%dT%H:%M:%S').timestamp()
        except Exception as e:
            print('Error parsing date: ' + args[1])
            print_usage()
            return
        #Test validity of end date string
        try:
            datetime.strptime(args[5], '%Y-%m-%dT%H:%M:%S').timestamp()
        except Exception as e:
            print('Error parsing date: ' + args[2])
            PrintUsage()
            return
        start = args[4]
        end = args[5]

    print("api_client_id: {}".format(api_client_id))
    print("api_client_secret: {}".format(api_client_secret))
    print("start: {}".format(start))
    print("start: {}".format(start))
    print("end: {}".format(end))
    print("pred_source: {}".format(pred_source))
    print("market: {}".format(market))
    print("currency_pair: {}".format(currency_pair))
    print("period: {}".format(period))
    print("\n")

    #Everything looks okay; proceed with program
    #Grab the data frame
    D = fetch_data_from_api(api_client_id, api_client_secret, market, currency_pair, period)

    # print("D: {}".format(D))

    #The number of previous days of data used
    #when making a prediction
    num_past_days = 16

    plot_data(D)

    #Number of neurons in the input layer
    i = num_past_days * 7 + 1
    #Number of neurons in the output layer
    o = D.shape[1] - 1
    #Number of neurons in the hidden layers
    h = int((i + o) / 2)
    #The list of layer sizes
    layers = [i, h, h, h, h, h, h, o]

    if(pred_source.startswith('K_NEIGHBORS')):
        R = KNeighborsRegressor(n_neighbors = 5)
    elif(predSource.startswith('MLPR')):
        R = MLPR(layers, maxItr = 1000, tol = 0.60, reg = 0.001, verbose = False)
    else:
        print("Source not implemented yet!")
        print_usage()
        return

    sp = Predictor(R, nPastDays = num_past_days)
    #Learn the dataset and then display performance statistics
    sp.Learn(D)
    sp.TestPerformance()
    #Perform prediction for a specified date range

    P = sp.PredictDate(start, end, period)
    print("P.shape[0]: {}".format(P.shape[0]))
    # print("P: {}".format(P))

    #Keep track of number of predicted results for plot
    n = P.shape[0]
    #Append the predicted results to the actual results
    D = P.append(D)

    #Predicted results are the first n rows
    plot_data(D, range(n + 1))

    print("\n");
    print("End time = %s" % datetime.now())
    print("-------------------------------------------------------------------")

    return (P, n)

#Main entry point for the program
if __name__ == "__main__":
    p, n = main(sys.argv[1:])


"""
    📌 Прочитайте созданный в прошлом задании csv файл без
    использования csv.DictReader.
    📌 Распечатайте его как pickle строку.
"""

__all__ = ['csv_to_pickle']

import csv
import pickle

def csv_to_pickle(path_csv: str)->None:
    with open(path_csv,'r',encoding='utf-8') as csv_file:
        csv_reader = csv.reader(csv_file, dialect='excel')
        data_list = []
        headers = []
        for i, row in enumerate(csv_reader):
            if not i:
                headers = row
            else:
                row_data = {key: value for key, value in zip(headers,row)}
                data_list.append(row_data)
    print(pickle.dumps(data_list))
    print(data_list)

if __name__ == '__main__':
    csv_to_pickle('task_6_file.csv')


# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the relevant file
with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='carrierx',
    version='0.0.1',
    description='Client library for CarrierX API.',
    long_description=long_description,
    url='https://github.com/mkching/carrierx-python',
    author='Michael Ching',
    author_email='mching@carrierx.com',
    license='MIT',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Topic :: Communications :: Telephony',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.2',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
    ],
    keywords='carrierx',
    packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
    install_requires=['requests'],
    extras_require={
        'dev': ['check-manifest'],
        'test': ['coverage'],
    },
)


# -*- coding: utf-8 -*-
class Solution:
    def numberOfTree(self , n ):
        # write code here
        dp = [0]*(n+1)
        dp[0] = dp[1] = 1
        for i in range(2, n+1):
            for k in range(1, i+1):
                dp[i] += dp[k-1]*dp[i-k]
                dp[i] = dp[i]%1000000007
        return dp[n]

print(Solution().numberOfTree(10000)) # 512319131


import pandas as pd
from nltk.tokenize import NLTKWordTokenizer
from nltk.stem import WordNetLemmatizer
import nltk
from nltk.corpus import wordnet
from nltk.corpus import stopwords
import json
import string
from math import floor

# naming convention: preprocessing_<W|C>[L][S][P][H<cutoff>]
#   W: with words
#   C: with chunks
#   L: with lemmatization
#   S: with stopwords
#   P: with punctuation
#   H: with highpass

def preprocessing_W(path):
    """Return preprocessed dataframe

    Parameters
    ----------
    path : str
        path of data

    Returns
    -------
    pd.DataFrame
        Preprocessed Dataframe. Data columns are as follows:

        ======  ==============================================================
        id      id of document, e.g. "id18154" (as `str`)
        token   a token in the dataset, e.g. "the" (as `list(str)`)
        author  author of the document that this token belongs to (as `str`)
        ======  ==============================================================
    """
    df = _load(path)
    df.token = _lower(df.token)
    df.token = _tokenize(df.token)
    df.token = _remove_punctuation(df.token)
    df.token = _remove_stopwords(df.token)
    return df

def preprocessing_L(path):
    df = _load(path)
    df.token = _lower(df.token)
    df.token = _tokenize(df.token)
    df.token = _lemmatize(df.token)
    df.token = _remove_punctuation(df.token)
    df.token = _remove_stopwords(df.token)
    return df

def preprocessing_SP(path):
    df = _load(path)
    df.token = _lower(df.token)
    df.token = _tokenize(df.token)
    return df

def preprocessing_SPC(path):
    df = _load(path)
    df.token = _lower(df.token)
    df.token = _tokenize(df.token)
    df.token = _chunks(df.token)
    return df

def preprocessing_C(path):
    df = _load(path)
    df.token = _lower(df.token)
    df.token = _tokenize(df.token)
    df.token = _remove_punctuation(df.token)
    df.token = _remove_stopwords(df.token)
    df.token = _chunks(df.token)
    return df

def preprocessing_SPH(path, cutoff):
    df = _load(path)
    df.token = _lower(df.token)
    df.token = _tokenize(df.token)
    df.token = _high_pass(df.token, cutoff)
    return df

def preprocessing_SPHC(path, cutoff):
    df = _load(path)
    df.token = _lower(df.token)
    df.token = _tokenize(df.token)
    df.token = _chunks(df.token)
    df.token = _high_pass(df.token, cutoff)
    return df

# utilitiy functions to facilitate preprocessing

def _load(path):
    file =  open(path, "r")
    data = json.load(file)
    return pd.DataFrame(data, columns=['id', 'token', 'author'])

def _lower(docs: pd.Series):
    return docs.apply(lambda s: s.lower())

def _tokenize(docs: pd.Series):
    return docs.apply(lambda s: [s[start:end] for start, end in NLTKWordTokenizer().span_tokenize(s)])

def _lemmatize(docs: pd.Series):
    def pos_tagger(tag):
        if tag.startswith("J"):
            return wordnet.ADJ
        elif tag.startswith("V"):
            return wordnet.VERB
        elif tag.startswith("N"):
            return wordnet.NOUN
        elif tag.startswith("R"):
            return wordnet.ADV
        else:
            return None

    lemm = WordNetLemmatizer()
    return docs.apply(lambda doc: nltk.pos_tag(doc)) \
        .apply(lambda doc: list(map(lambda x: (x[0], pos_tagger(x[1])), doc))) \
        .apply(lambda doc: list(map(lambda x: x[0] if x[1] == None else lemm.lemmatize(x[0], x[1]), doc)))

def _remove_stopwords(docs: pd.Series):
    stop_words = set(stopwords.words('english'))
    return docs.apply(lambda doc: [w for w in doc if not w in stop_words])

def _remove_punctuation(docs: pd.Series):
    return docs.apply(lambda doc: [w for w in doc if not w in string.punctuation])

def _high_pass(docs: pd.Series, cutoff: float):
    tokens = docs.explode()
    dist = nltk.FreqDist(tokens)
    n = len(tokens)
    return docs.apply(lambda doc: [w for w in doc if dist[w] > n * cutoff])

def _chunks(doc: pd.Series):
    joined = doc.apply(lambda doc: "".join(doc))
    return joined.apply(lambda doc: ["".join([doc[3*i], doc[3*i+1], doc[3*i+2]]) for i in range(floor(len(doc)/3))])

if __name__ == "__main__":
    for path in ["new_train.json", "new_test.json"]:
        preprocessing_W(path).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_W.json', orient='records')
        preprocessing_L(path).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_WL.json', orient='records')
        preprocessing_SP(path).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_WSP.json', orient='records')
        preprocessing_SPC(path).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_CSP.json', orient='records')
        preprocessing_C(path).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_C.json', orient='records')
        preprocessing_SPH(path, 5e-4).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_WSPH5e-4.json', orient='records')
        preprocessing_SPHC(path, 5e-4).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_CSPH5e-4.json', orient='records')
        preprocessing_SPH(path, 1e-4).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_WSPH1e-4.json', orient='records')
        preprocessing_SPHC(path, 1e-4).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_CSPH1e-4.json', orient='records')
        preprocessing_SPH(path, 5e-5).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_WSPH5e-5.json', orient='records')
        preprocessing_SPHC(path, 5e-5).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_CSPH5e-5.json', orient='records')
        preprocessing_SPH(path, 1e-5).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_WSPH1e-5.json', orient='records')
        preprocessing_SPHC(path, 1e-5).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_CSPH1e-5.json', orient='records')
        preprocessing_SPH(path, 1e-6).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_WSPH1e-6.json', orient='records')
        preprocessing_SPHC(path, 1e-6).to_json(f'preprocessing_output/preprocessed_{"train" if "train" in path else "test"}_CSPH1e-6.json', orient='records')


import pygame
from Block import Block
from settings import *
from typing import Tuple

class Sudoku:

    board = [
        [7, 8, 0, 4, 0, 0, 1, 2, 0],
        [6, 0, 0, 0, 7, 5, 0, 0, 9],
        [0, 0, 0, 6, 0, 1, 0, 7, 8],
        [0, 0, 7, 0, 4, 0, 2, 6, 0],
        [0, 0, 1, 0, 5, 0, 9, 3, 0],
        [9, 0, 4, 0, 6, 0, 0, 0, 5],
        [0, 7, 0, 3, 0, 0, 0, 1, 2],
        [1, 2, 0, 0, 0, 7, 4, 0, 0],
        [0, 4, 9, 2, 0, 6, 0, 0, 7]
    ]

    def __init__(self):
        self.width = WIDTH
        self.height = HEIGHT
        self.sudoku_width = SUDOKU_WIDTH
        self.sudoku_height = SUDOKU_HEIGHT
        self.x_delta = X_DELTA
        self.y_delta = Y_DELTA
        self.size = SIZE
        self.grid = None
        self.win = None
        self.sudokuWin = None
        self.clock = None
        self.fps = FPS
        self.selected = None
        self.font = None

    def display_init(self):

        pygame.init()
        pygame.font.init()

        self.win = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption("Sudoku Solver")
        self.win.fill(BACKGROUND_COLOR)

        self.sudokuWin = self.win.subsurface((self.x_delta, self.y_delta, self.sudoku_width, self.sudoku_width))

        self.clock = pygame.time.Clock()

        self.font = pygame.font.SysFont("comicsansms", 40)
        self.title = self.font.render('Sudoku solver', 1, MID_BLACK)
        w, h = self.title.get_size()

        self.win.blit(self.title, ((self.width - w) // 2, (self.y_delta - h) // 2))
        pygame.display.update()

    def grid_init(self):
        self.grid = list()
        for row in range(9):
            self.grid.append(list())
            for col in range(9):
                self.grid[row].append(Block(row, col))

        for i, row in enumerate(self.board):
            for j, element in enumerate(row):
                self.grid[i][j].set_number(element)
                if element != 0:
                    self.grid[i][j].make_readonly()

    def get_row_col(self, pos: Tuple) -> Tuple:
        x, y = pos
        row = (y - self.y_delta) // self.size
        col = (x - self.x_delta) // self.size

        return row, col

    def is_valid_dims(self, row: int, col: int) -> bool:
        return row in range(9) and col in range(9)

    def draw_grid(self, win: pygame.Surface) -> None:
        width, height = win.get_size()
        color = MID_BLACK

        # pygame.draw.line(win, color, (0, 0), (width, 0), 2)
        # pygame.draw.line(win, color, (0, 0), (0, height), 2)

        for i in range(9):
            linewidth = 2 if i % 3 == 0 else 1

            pygame.draw.line(win, color, (0, i * self.size), (width, i * self.size), linewidth)
            pygame.draw.line(win, color, (i * self.size, 0), (i * self.size, height), linewidth)

        pygame.draw.line(win, color, (width - 2, 0), (width - 2, height), 2)
        pygame.draw.line(win, color, (0, height - 2), (width, height - 2), 2)

    def draw_board(self, win: pygame.Surface) -> None:
        if self.grid is None:
            return

        for row in self.grid:
            for block in row:
                block.draw(win)

    def verify_temp(self, row: int, col: int, num: int) -> bool:
        if self.selected:
            # row, col = self.selected.get_dims()
            # num = self.selected.get_number()

            for i in range(9):
                if self.grid[row][i] == num and i != col:
                    return False

                if self.grid[i][col] == num and i != row:
                    return False

            boxRow = row // 3
            boxCol = col // 3

            for i in range(3 * boxRow, 3 * boxRow + 3):
                for j in range(3 * boxCol, 3 * boxCol + 3):
                    if self.grid[i][j] == num and i != row and j != col:
                        return False

            return True

        return False

    def solve_gui(self):
        pos = self.find_empty()

        if pos is None:
            return True

        row, col = pos

        for i in range(1, 10):
            if self.verify_temp(row, col, i):
                self.grid[row][col].set_number(i)
                self.grid[row][col].make_readonly()
                self.draw()
                pygame.time.delay(100)

                if self.solve_gui():
                    return True

                self.grid[row][col].set_number(0)
                self.grid[row][col].remove_readonly()
                self.grid[row][col].remove_temp()
                self.draw()
                pygame.time.delay(100)

        return False

    def find_empty(self) -> Tuple:
        for row in self.grid:
            for block in row:
                if block.is_empty():
                    row, col = block.get_dims()
                    return row, col

        return None

    def draw(self):
        self.sudokuWin.fill(CREAM)

        self.draw_grid(self.sudokuWin)
        self.draw_board(self.sudokuWin)

        pygame.display.update()

    def quit(self):
        pygame.font.quit()
        pygame.quit()

    def run(self):

        self.display_init()
        self.grid_init()

        run = True
        while run:
            self.clock.tick(self.fps)
            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    run = False

                if event.type == pygame.KEYDOWN:

                    if event.key in (pygame.K_1, pygame.K_KP1) and self.selected:
                        self.selected.set_temp(1)

                    if event.key in (pygame.K_2, pygame.K_KP2) and self.selected:
                        self.selected.set_temp(2)

                    if event.key in (pygame.K_3, pygame.K_KP3) and self.selected:
                        self.selected.set_temp(3)

                    if event.key in (pygame.K_4, pygame.K_KP4) and self.selected:
                        self.selected.set_temp(4)

                    if event.key in (pygame.K_5, pygame.K_KP5) and self.selected:
                        self.selected.set_temp(5)

                    if event.key in (pygame.K_6, pygame.K_KP6) and self.selected:
                        self.selected.set_temp(6)

                    if event.key in (pygame.K_7, pygame.K_KP7) and self.selected:
                        self.selected.set_temp(7)

                    if event.key in (pygame.K_8, pygame.K_KP8) and self.selected:
                        self.selected.set_temp(8)

                    if event.key in (pygame.K_9, pygame.K_KP9) and self.selected:
                        self.selected.set_temp(9)

                    if event.key == pygame.K_ESCAPE:
                        if self.selected:
                            self.selected.remove_temp()

                    if event.key in (pygame.K_RETURN, pygame.K_KP_ENTER):
                        if self.selected:
                            row, col = self.selected.get_dims()
                            num = self.selected.get_number()
                            if self.verify_temp(row, col, num):
                                self.selected.set_valid()

                    if event.key == pygame.K_SPACE:
                        self.solve_gui()

                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    row, col = self.get_row_col(pos)

                    if self.is_valid_dims(row, col):
                        if self.selected:
                            self.selected.deselect()
                            self.selected = None

                        if not self.grid[row][col].is_readonly():
                            self.selected = self.grid[row][col]
                            self.selected.select()

                    print(row, col, sep='\t')

            self.draw()

        self.quit()

if __name__ == "__main__":
    X = Sudoku()
    X.run()


""""配置文件"""
import pickle

import torch

device=torch.device("cuda:0"if torch.cuda.is_available() else "cpu")
#############语料相关############
user_dict_path="corpus/user_dict/keyword.txt"
stopwords_path=r"corpus/user_dict/stopwords.txt"#"\corpus\user_dict\stopwords.txt"
classify_corpus_train_path="corpus/classify/classify_train.txt"
classify_corpus_test_path="corpus/classify/classify_test.txt"

classify_corpus_by_word_train_path="corpus/classify/classify_train_by_word.txt"
classify_corpus_bu_word_test_path="corpus/classify/classify_test_by_word.txt"

###################################分类相关##############################
classify_model_path="model/classify.model"#词语作为特征的模型的保存地址
classify_model_path_by_word="model/classify_by_word.model"#单个字作为特征的模型的保存地址

classify_model_final_path="model/classify.model"#一个词语作为特征的模型
classify_model_final_path_by_word="model/classify_by_word.model"#单个字作为特征的模型

##################################chatbot相关################################
#
# chatbot_input_path1=r'E:\Chat_robot_project\chart_service\corpus\chatbot\input.txt'#将小黄鸡未分词的问 放到一个路径下
# chatbot_target_path1=r'E:\Chat_robot_project\chart_service\corpus\chatbot\target.txt'#将小黄鸡未分词的回答 放到一个路径下

chatbot_by_word=True
if chatbot_by_word:

    chatbot_input_path='corpus/chatbot/input_by_word.txt'#将小黄鸡未分词的问 放到一个路径下
    chatbot_target_path='corpus/chatbot/target_by_word.txt'#将小黄鸡未分词的回答 放到一个路径下

else:
    chatbot_input_path='corpus/chatbot/input.txt'#将小黄鸡未分词的问 放到一个路径下
    chatbot_target_path='corpus/chatbot/target.txt'#将小黄鸡未分词的回答 放到一个路径下

#ws
if chatbot_by_word:
    chatbot_ws_input_path = "model/chatbot/ws_by_word_input_path.pkl"
    chatbot_ws_target_path="model/chatbot/ws_by_word_target_path.pkl"
else:
    chatbot_ws_input_path="model/chatbot/ws_input_path.pkl"
    chatbot_ws_target_path="model/chatbot/ws_target_path.pkl"

chatbot_ws_input=pickle.load(open(chatbot_ws_input_path,'rb'))
chatbot_ws_target=pickle.load(open(chatbot_ws_target_path,'rb'))

chatbot_batch_size=128
if chatbot_by_word:
    chatbot_input_max_len=30
    chatbot_target_max_len = 30
else:
    chatbot_input_max_len = 12
    chatbot_target_max_len = 12

chatbot_embedding_dim=256
chatbot_encoder_num_layers=1
chatbot_encoder_hidden_size=128

chatbot_decoder_num_layer=1
chatbot_decoder_hidden_size=128

chatbot_teacher_forcing_ratio=0.7

chatbot_model_save_path="model/chatbot/seq2seq.model" if chatbot_by_word else "model/chatbot/seq2seq_by_word.model"
chatbot_optimizer_save_path="model/chatbot/optimizer.model" if chatbot_by_word else "model/chatbot/optimizer_by_word.model"

clip=0.01


# FUNCTION ARGUMENT AND RETURN STATEMENT

# def average(a,b =1):
#     print("The average is: ",(a+b)/2)

# average(a =2) # a is required argument

# Variable length arumment
# Example 1
# def average(*numbers): #numbers -> here this is tuple
#     sum = 0;
#     for i in numbers:
#         sum = sum + i
#     print("Average is: ",sum/len(numbers))

# average(1,5,7)

# Example 2
# def name(**name):
#     print(type(name))
#     print("Hello,", name["fname"],name["mname"],name["lname"])
# name(mname = "Glitch",lname = "prathu", fname = "oneshot")

# Return statement -> used to return the value of the expession back to the calling function

def average(*numbers):  # numbers -> here this is tuple
    sum = 0
    for i in numbers:
        sum = sum + i
    return sum/len(numbers)
c = average( 5, 6 ,7, 1)
print(c)


"""
We start with mnist, then do something else
"""
import numpy as np
import torch.optim

import torch
import torchvision.datasets as datasets
import pandas as pd
import sklearn.decomposition
import sklearn.metrics

from pathlib import Path
import torch
import torchvision

import torchvision.datasets as datasets

class MNISTRandomLabels(datasets.MNIST):
  def __init__(self, corrupt_prob=0.0, num_classes=10, shfld_pxls=False , rnd_pxls=False, gaussian=False, **kwargs):
    super(MNISTRandomLabels, self).__init__(**kwargs)
    self.n_classes = num_classes

    if corrupt_prob > 0:
      print("Introducing Noise: Corrupting labels ...")
      self.corrupt_labels(corrupt_prob)
    elif shfld_pxls:
      print("Introducing Noise: Shuffling pixels")
      self.shuffled_pixels()
    elif rnd_pxls:
      print("Introducing Noise: Randomizing pixels")
      self.random_pixels()
    elif gaussian:
      print("Introducing Noise: Generating pixels from a Gaussian distribution")
      self.gaussian()

  def corrupt_labels(self, corrupt_prob):
    """
    Labels are corrupted with a corrput_probability
    :param corrupt_prob:
    :return:
    """
    np.random.seed(12345)
    labels = np.array(self.targets)
    mask = np.random.rand(len(labels)) <= corrupt_prob/100.
    rnd_labels = np.random.choice(self.n_classes, mask.sum())
    labels[mask] = rnd_labels
    # we need to explicitly cast the labels from npy.int64 to
    # builtin int type, otherwise pytorch will fail...
    labels = [int(x) for x in labels]
    self.targets = labels

  def shuffled_pixels(self):
    """
    All images are permuted using the same permutation
    :return:
    """
    np.random.seed(12345)
    images = np.array(self.data)
    data = np.swapaxes(np.reshape(images, (images.shape[0], -1, images.shape[-1])), 0, 1)
    np.random.shuffle(data)
    self.data = np.reshape(np.swapaxes(data, 0, 1), images.shape)

  def random_pixels(self):
    """
    Each image is permuted using a different permutation matrix
    :return:
    """
    np.random.seed(12345) # sets the seed for the ensemble of permutations
    images = np.array(self.data)
    for i, img in enumerate(images):
      rndpxls = np.reshape(img, (-1, img.shape[2])).copy()
      np.random.shuffle(rndpxls)
      images[i] = np.reshape(rndpxls, img.shape)
    self.data = images

  def gaussian(self):
    """
    Each pixel in the data is ind. sampled from a Gaussian dist. with mean and variance matching the original dataset's
    :return:
    """
    np.random.seed(12345) # sets the seed for the ensemble of generated pixels
    images = self.data
    print(images.shape)
    mean = np.mean(images, axis=(0,1,2))
    std = np.std(images, axis=(0,1,2))
    data = np.clip(np.random.multivariate_normal(mean=mean, cov=np.diag(std), size=images.shape[:-1]), 0, 255).astype(np.uint8)     # we need to explicitly cast the data as int otherwise pytorch will fail
    self.data = data

def transform_truncated(pca, X, n_components):
    X = pca._validate_data(X, dtype=[np.float64, np.float32], reset=False)
    if pca.mean_ is not None:
        X = X - pca.mean_
    X_transformed = np.dot(X, pca.components_[:n_components, :].T)
    #start = np.random.choice(range(0,len(pca.components_)-n_components))
    #X_transformed = np.dot(X, pca.components_[start:start+n_components, :].T)
    if pca.whiten:
        X_transformed /= np.sqrt(pca.explained_variance_)
    return X_transformed

def inv_transform(pca, X, n_components):
    return np.dot(X, pca.components_[:n_components, :]) + pca.mean_

def inv_forward_transform(pca, X, n_components):
    return inv_transform(
        pca, transform_truncated(pca, X, n_components), n_components
    )

def get_pca_transformed_fmnist(n_components, train=True):

  transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
  data = torchvision.datasets.FashionMNIST(Path() / "data", train=train, download=True, transform=transform)
  dataloader = torch.utils.data.DataLoader(dataset=data, batch_size=len(data))
  images_all, _ = next(iter(dataloader))
  # convert to 1D
  images_flat = images_all.reshape(images_all.shape[0], -1)

  pca = sklearn.decomposition.PCA(n_components=784)
  images_flat_hat = pca.inverse_transform(pca.fit_transform(images_flat))
  images_hat = inv_forward_transform(pca, X=images_flat, n_components=n_components).reshape(-1,28,28)

  return images_hat

def normalize_img(x):
    """
    Over all three channels; there should be numpy way
    :param x:
    :return:
    """
    if len(x.shape) == 2:
        try:
            x=x.numpy()
        except:
            pass
        x = (x - np.amin(x)) / (np.amax(x) - np.amin(x))
    elif len(x.shape) ==3:
        for i in range(3):
            x[...,i] = (x[...,i] - np.amin(x[...,i])) / (np.amax(x[...,i]) - np.amin(x[...,i]))
    return (255*x).astype(np.uint8)

def normalize_dataset(data):
    data_norm = np.array([normalize_img(data[i]) for i in range(len(data))])
    return data_norm

class MNISTDatasetNoise(datasets.MNIST):
    """CIFAR10 dataset, with support for random labels and pixels

    Params
    ------
    corrupt_prob: float
      Default 0.0. The probability of a label being replaced with
      random label.
    num_classes: int
      Default 10. The number of classes in the dataset.
    """

    def __init__(self, n_components=0, num_classes=10, **kwargs):
        super(MNISTDatasetNoise, self).__init__(**kwargs)
        self.n_classes = num_classes
        self.n_components = n_components
        self.train = kwargs['train']
        if n_components > 0:
            print("Adding dataset noise ...")
            self.add_fmnist(n_components)

    def add_fmnist(self, n_components):
        fmnist = get_pca_transformed_fmnist(self.n_components, self.train)
        norm_fmnist = normalize_dataset(fmnist)
        d = np.mean([norm_fmnist, normalize_dataset(self.data.numpy())], axis=0).astype(np.uint8)
        self.data = torch.from_numpy(d)