import asyncio
import functools
import json
import os
import subprocess

from git import Repo, Actor
from importlib import resources
from mimetypes import guess_type
from shutil import rmtree
from tornado import web, websocket

async def render(basepath: str):
    """Render the project at the given basepath."""
    install = await asyncio.create_subprocess_exec('poetry', 'install', cwd=basepath)
    await install.wait()
    build = await asyncio.create_subprocess_exec('poetry', 'run', 'make', 'html', cwd=basepath, stdout=subprocess.PIPE)
    await build.wait()
    return (await build.stdout.read()).decode('utf-8')

class FrontendHandler(web.RequestHandler):
    """Handles serving the UI."""

    def initialize(self, basepath: str):
        self.basepath = basepath

    def get(self, path):
        """Return the UI resource at ``path``."""
        if path.startswith('/'):
            path = path[1:]
        base = resources.files('ou_content_author') / 'frontend' / 'public'
        try:
            self.send_resource(base, path.split('/'))
        except FileNotFoundError:
            self.send_resource(base, ('index.html',))

    def send_resource(self, resource, path):
        """Send the resource identified by the ``path``.

        Performs content modification when sending the index.html file to set the correct base path for all resources.
        """
        for part in path:
            resource = resource / part
        try:
            mimetype = guess_type(path[-1])
            if mimetype and mimetype[0]:
                self.set_header('Content-Type', mimetype[0])
            if path == ('index.html',):
                content = resource.read_bytes().decode('utf-8')
                content = content.replace('{app_path}', f'{self.basepath}app/')
                self.write(content.encode('utf-8'))
            else:
                self.write(resource.read_bytes())
        except IsADirectoryError:
            raise FileNotFoundError
        except IndexError:
            raise FileNotFoundError

class ApiHandler(websocket.WebSocketHandler):

    def initialize(self, repository_location: str):
        self._repository_location = repository_location
        self._task = None

    async def on_message(self, message: str):
        data = json.loads(message)
        if 'type' in data:
            if data['type'] == 'clone-repository':
                self.clone_repository(data)
            elif data['type'] == 'delete-repository':
                self.delete_repository(data)
            elif data['type'] == 'checkout-branch':
                self.checkout_branch(data)
            elif data['type'] == 'select-block':
                self.select_block(data)
            elif data['type'] == 'load-file-content':
                await self.load_file_content(data)
            elif data['type'] == 'save-file-content':
                await self.save_file_content(data)
            elif data['type'] == 'commit-changes':
                self.commit_changes(data)
            elif data['type'] == 'discard-changes':
                self.discard_changes(data)
            elif data['type'] == 'add-file':
                self.add_file(data)
            elif data['type'] == 'delete-file':
                self.delete_file(data)

    def send_message(self, message: dict):
        self.write_message(json.dumps(message))

    def clone_repository(self, data: dict):
        """Clone the repository at the URL."""
        if 'url' in data:
            if os.path.exists(self._repository_location):
                rmtree(self._repository_location)
            repo = Repo.clone_from(data['url'], self._repository_location)
            branches = [ref.path.split('/')[-1] for ref in repo.remote().refs if not ref.path.endswith('/HEAD')]
            self.send_message({
                'type': 'repository',
                'branches': branches
            })

    def delete_repository(self, data: dict):
        if os.path.exists(self._repository_location):
            rmtree(self._repository_location)
        self.send_message({
            'type': 'repository-deleted',
        })

    def checkout_branch(self, data: dict):
        """Checkout the branch either locally or from remote"""
        if 'branch' in data:
            repo = Repo(self._repository_location)
            found = False
            for ref in repo.branches:
                if ref.path.endswith(f'/{data["branch"]}'):
                    ref.checkout()
                    found = True
                    break
            if not found:
                for ref in repo.remote().refs:
                    if ref.path.endswith(f'/{data["branch"]}'):
                        repo.git.checkout('-b', data['branch'], ref.path)
                        found = True
            if found:
                blocks = []
                for basepath, dirnames, filenames in os.walk(self._repository_location):
                    for filename in filenames:
                        if filename == 'conf.py':
                            blocks.append(os.path.join(basepath, filename)[len(self._repository_location):])
                self.send_message({
                    'type': 'branch',
                    'blocks': blocks
                })

    def select_block(self, data: dict):
        """Select a given block."""
        if 'block' in data:
            self.scan_block(data['block'])

    async def load_file_content(self, data: dict):
        """Load the content of a file."""
        if 'file' in data and 'block' in data and 'directory' in data['file'] and 'filename' in data['file']:
            filepath = os.path.join(self._repository_location,
                                    os.path.dirname(data['block'])[1:],
                                    data['file']['directory'],
                                    data['file']['filename'])
            if os.path.exists(filepath) and os.path.abspath(filepath).startswith(os.path.abspath(self._repository_location)):
                with open(filepath) as in_f:
                    self.send_message({
                        'type': 'file-content',
                        'content': in_f.read()
                    })
                await self.run_render(os.path.join(self._repository_location, os.path.dirname(data['block'])[1:]),
                                      os.path.dirname(data['block'])[1:],
                                      os.path.join(data['file']['directory'], data['file']['filename']))

    async def save_file_content(self, data: dict):
        """Save the updated file content."""
        if 'file' in data and 'block' in data and 'directory' in data['file'] and 'filename' in data['file'] and 'content' in data:
            filepath = os.path.join(self._repository_location,
                                    os.path.dirname(data['block'])[1:],
                                    data['file']['directory'],
                                    data['file']['filename'])
            if os.path.exists(filepath) and os.path.abspath(filepath).startswith(os.path.abspath(self._repository_location)):
                with open(filepath, 'w') as out_f:
                    out_f.write(data['content'])
                await self.run_render(os.path.join(self._repository_location, os.path.dirname(data['block'])[1:]),
                                      os.path.dirname(data['block'])[1:],
                                      os.path.join(data['file']['directory'], data['file']['filename']))
            self.check_repo_changes()

    def commit_changes(self, data: dict):
        """Commit and push changes."""
        if 'name' in data and data['name'] and 'email' in data and data['email'] and 'message' in data and data['message']:
            repo = Repo(self._repository_location)
            if len(repo.index.diff(None) + repo.index.diff('HEAD')) > 0 or len(repo.untracked_files) > 0:
                repo.git.add('.')
                actor = Actor(data['name'], data['email'])
                repo.index.commit(data['message'], author=actor, committer=actor)
                repo.git.push('--force')
            self.send_message({
                'type': 'changes-committed',
            })
            self.check_repo_changes()

    def discard_changes(self, data: dict):
        """Discard any changes."""
        repo = Repo(self._repository_location)
        repo.git.checkout('--', '.')
        self.send_message({
            'type': 'changes-discarded',
        })
        self.check_repo_changes()

    def add_file(self, data: dict):
        if 'block' in data and 'file' in data and 'directory' in data['file'] and 'filename' in data['file']:
            filepath = os.path.join(self._repository_location,
                                    os.path.dirname(data['block'])[1:],
                                    data['file']['directory'],
                                    data['file']['filename'])
            if not os.path.exists(filepath) and os.path.abspath(filepath).startswith(os.path.abspath(self._repository_location)):
                if not os.path.exists(os.path.dirname(filepath)):
                    os.makedirs(os.path.dirname(filepath), exist_ok=True)
                with open(filepath, 'w') as out_f:
                    if filepath.endswith('.md'):
                        out_f.write('# Page Title')
            self.scan_block(data['block'])
            self.check_repo_changes()

    def delete_file(self, data: dict):
        if 'block' in data and 'file' in data and 'directory' in data['file'] and 'filename' in data['file']:
            filepath = os.path.join(self._repository_location,
                                    os.path.dirname(data['block'])[1:],
                                    data['file']['directory'],
                                    data['file']['filename'])
            if os.path.exists(filepath) and os.path.abspath(filepath).startswith(os.path.abspath(self._repository_location)):
                os.unlink(filepath)
            self.scan_block(data['block'])
            self.check_repo_changes()

    async def run_render(self, basepath: str, block: str, filepath: str):
        """Run the render process and send the required messages."""
        if basepath.endswith('/source'):
            basepath = basepath[:-7]
        self.send_message({
            'type': 'file-rendering',
        })
        if self._task is not None and not self._task.done():
            self._task.cancel()
        self._task = asyncio.create_task(render(basepath))
        self._task.add_done_callback(functools.partial(self.render_complete, block, filepath))

    def render_complete(self, block: str, filepath: str, task: asyncio.Task):
        """Report rendering completed to the client."""
        if not task.cancelled():
            result = task.result()
            if result:
                if '.' in filepath:
                    filepath = f'{filepath[:filepath.rfind(".")]}.html'
                else:
                    filepath = f'{filepath}.html'
                if block.endswith('/source'):
                    block = block[:-7]
                self.send_message({
                    'type': 'file-rendered',
                    'url': f'/{block}/{filepath}',
                    'output': result,
                })

    def check_repo_changes(self):
        """Check if there are changes to commit in the repo."""
        repo = Repo(self._repository_location)
        if len(repo.index.diff(None) + repo.index.diff('HEAD')) > 0 or len(repo.untracked_files) > 0:
            self.send_message({
                'type': 'changes-found'
            })
        else:
            self.send_message({
                'type': 'no-changes-found'
            })

    def scan_block(self, block):
        """Scan the block and send back the """
        blockpath = os.path.dirname(f'{self._repository_location}{block}')
        files = []
        for basepath, dirnames, filenames in os.walk(blockpath):
            for filename in filenames:
                if filename.endswith('.md'):
                    files.append({
                        'directory': basepath[len(blockpath) + 1:],
                        'filename': filename
                    })
        self.send_message({
            'type': 'block',
            'path': block,
            'files': files
        })

class RenderedHandler(web.RequestHandler):
    """Handler for the rendered HTML pages."""

    def initialize(self, repository_location: str):
        """Initialise the handler."""
        self._repository_location = repository_location

    def get(self, path: str):
        """Get the rendered HTML page."""
        if path.startswith('/'):
            path = path[1:]
        path_elements = path.split('/')
        filepath = os.path.abspath(os.path.join(self._repository_location,
                                                path_elements[0],
                                                'build',
                                                'html',
                                                *path_elements[1:]))
        if filepath.startswith(self._repository_location) and os.path.exists(filepath):
            mimetype = guess_type(filepath)
            if mimetype and mimetype[0]:
                self.set_header('Content-Type', mimetype[0])
            with open(filepath, 'rb') as in_f:
                self.write(in_f.read())


__author__ = 'lenord'
import pandas as pd
import numpy as np
import math
import random
import chartanalysis as chanalysis
import chartdata as chdata

#BASIC MATHEMATICAL FUNCTIONS
def moving_median(base_list,window_range):
    list_range = base_list.shape[0]
    mmedian = np.zeros(list_range)
    for window in range(0,list_range):
        window_list = base_list[window-window_range:window]
        if (np.shape(window_list)[0] == 0):
            mmedian[window] = 0
        else:
            mmedian[window] = np.median(window_list)
    return mmedian
def moving_min(base_list,window_range):
    list_range = base_list.shape[0]
    mminimum = np.zeros(list_range)
    for window in range(0,list_range):
        window_list = base_list[window-window_range:window]
        if(np.shape(window_list)[0] == 0):
            mminimum[window] = 0
        else:
            mminimum[window] = np.min(window_list)
    return mminimum
def diffSeries(data_list1,data_list2):
    return data_list1 - data_list2
def removeBelowValue(data_list,value):
    return np.array([element if element>value else 0 for element in data_list])
def timeShift(data_list,roll_size):
    shift_list = np.roll(data_list,roll_size)
    # shift_list[:roll_size] = 0
    return shift_list
def keepLastValue(data_list):
    # for index in range(2,data_list.shape[0]):
    #     if data_list[index]==0:
    #         data_list[index] = data_list[index-1]
    return data_list

#CAIDA DETECT ALGORITHM
def algoCaidaDetect(base_list):
    '''
    DETECTION ALGORITHM :
removeBelowValue(
  diffSeries(
    timeShift(
      transformNull(
        removeBelowValue(
          diffSeries(
            movingMinimum(
              darknet.ucsd-nt.non-erratic.routing.asn.3346.uniq_src_ip,
              120
            ),
            diffSeries(
              movingMedian(
                darknet.ucsd-nt.non-erratic.routing.asn.3346.uniq_src_ip,
                20
              ),
              movingMinimum(
                darknet.ucsd-nt.non-erratic.routing.asn.3346.uniq_src_ip,
                20
              )
            )
          ),
          0
        ),
        0
      ),
      "20min"
    ),
    keepLastValue(
      darknet.ucsd-nt.non-erratic.routing.asn.3346.uniq_src_ip
    )
  ),
  0
)
    '''
    mmed_20 = abs(moving_median(base_list,20))
    mmin_20 = abs(moving_min(base_list,20))
    mmin_120 = abs(moving_min(base_list,120))
    return removeBelowValue(diffSeries(timeShift(removeBelowValue(diffSeries(mmin_120,diffSeries(mmed_20,mmin_20)),
                                                                      0),20),keepLastValue(base_list)), 0)

def bayesChangePoint(base_list):
    window_start = 1050
    window_end = 1120
    offset = 1050

    data = base_list[window_start:window_end]
    windowSize = len(data)
    meanData = np.mean(data)
    meanSquareData = np.mean(np.multiply(data,data))
    squareDiff = meanSquareData - np.square(meanData)

    sumData = 0
    cumulativeSum = []

    for timestamp in range(windowSize):
        timestamp += offset
        sumData += data[timestamp]
        cumulativeSum.append(sumData)

    changePointDetect = []
    for m in range(windowSize-1):
        pos=m+1
        mscale = 4*(pos)*((windowSize)-pos)
        Q = cumulativeSum[m]-(sumData - cumulativeSum[m])
        U = -np.square(meanData*((windowSize)-2*pos) + Q)/float(mscale) + squareDiff
        changePointDetect.append(-(windowSize/float(2)-1)*math.log(windowSize*U/2) - 0.5*math.log((pos*((windowSize)-pos))))

    maxChange, changePoint, minChange = np.max(changePointDetect), np.argmax(changePointDetect), np.min(changePointDetect)
    variation = np.abs(minChange) - np.abs(maxChange)
    return changePointDetect, variation, changePoint


import numpy as np
# Features(Hrs Slept,Hrs Studied)
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)
# Labels(Marks Obtained)
y = np.array(([92], [86], [89]), dtype=float)

X = X/np.amax(X, axis=0)  # Normalize
y = y/100

def sigmoid(x):  # activation function
    return 1/(1+np.exp(-x))

def sigmoid_grad(x):
    return x*(1-x)

# variable initialization
epoch = 1000  # Setting training iterations
eta = 0.2     # Setting learning rate(eta)
input_neurons = 2   # number of features in data set
hidden_neurons = 3  # number of hidden layers neurons
output_neurons = 1  # number of neurons at output layer

# weight and bias random initialization
# wh,wout = weights, bias of hidden layers
# wout,bout = weights, bias of output layers
wh = np.random.uniform(size=(input_neurons, hidden_neurons))
bh = np.random.uniform(size=(1, hidden_neurons))
wout = np.random.uniform(size=(hidden_neurons, output_neurons))
bout = np.random.uniform(size=(1, output_neurons))

for i in range(epoch):
    # * forward propogation
    h_ip = np.dot(X, wh)+bh
    h_act = sigmoid(h_ip)
    o_ip = np.dot(h_act, wout)+bout
    output = sigmoid(o_ip)

    # * Back propogation
    # Error at output layer
    Eo = y-output
    outgrad = sigmoid_grad(output)
    d_output = Eo*outgrad

    # Error at hidden layer
    Eh = d_output.dot(wout.T)
    hiddengrad = sigmoid_grad(h_act)
    d_hidden = Eh*hiddengrad

    wout += h_act.T.dot(d_output)*eta
    wh += X.T.dot(d_hidden)*eta

print("Normalized Input:\n"+str(X))
print("Actual Output:\n"+str(y))
print("Predicted output:\n", output)


import time
import file
from adb import By

class Groupchat:
    def __init__(self, adb, wechat_list, group_names, wechat_index, groups_txt_list, image_num, main):
        self._adb = adb

        # 微信名称
        self._wechat_list = wechat_list
        # 群名
        self._group_list = group_names
        # 从哪一个微信开始运行
        self.wechats_index = wechat_index

        self._groups_txt_list = groups_txt_list

        self._image_count = image_num

        self._group_index = 0

        self._main = main

    def choice_images(self):
        self._adb.refresh_nodes()
        time.sleep(1)
        print(self._image_count)
        for num in range(self._image_count):
            print(num)
            if num < 4:
                self._adb.adb_click(250 * (num + 1), 300)
            elif num >= 4 and num < 8:
                print('4-8')
                self._adb.adb_click(250 * (num - 3), 300 + 250)
            else:
                self._adb.adb_click(250 * (num - 7), 300 + 500)
            time.sleep(1)
        time.sleep(1)
        self._adb.refresh_nodes()
        if self._adb.find_nodes_by_text('发送(' + str(self._image_count) + '/9)'):
            self._adb.click(0)
        else:
            if self._adb.find_nodes_by_text('发送(' + str(self._image_count - 1) + '/9)'):
                self._adb.click(0)
            elif self._adb.find_nodes_by_text('发送(' + str(self._image_count - 2) + '/9)'):
                self._adb.click(0)
            elif self._adb.find_nodes_by_text('发送(' + str(self._image_count - 3) + '/9)'):
                self._adb.click(0)
            elif self._adb.find_nodes_by_text('发送(' + str(self._image_count - 4) + '/9)'):
                self._adb.click(0)
            elif self._adb.find_nodes_by_text('发送(' + str(self._image_count - 5) + '/9)'):
                self._adb.click(0)
            elif self._adb.find_nodes_by_text('发送(' + str(self._image_count - 6) + '/9)'):
                self._adb.click(0)
            elif self._adb.find_nodes_by_text('发送(' + str(self._image_count - 7) + '/9)'):
                self._adb.click(0)
            elif self._adb.find_nodes_by_text('发送(' + str(self._image_count - 8) + '/9)'):
                self._adb.click(0)

    def clean_wechat(self):
        time.sleep(5)
        self._adb.adb_put_back()
        time.sleep(1)
        # 点击进程按钮，显示所有后台进程
        self._adb.adb_keyboard(82)
        time.sleep(1)
        # 点击清理按钮
        self._adb.click_by_text_do_not_refresh0('清理')
        time.sleep(2)

    def send_image(self):
        print('发送图片')
        if self._image_count > 0:
            self._adb.refresh_nodes()
            if self._adb.find_nodes_by_content('更多功能按钮，已展开'):
                self._adb.click(0)
                self._adb.refresh_nodes()
                if self._adb.find_nodes_by_text('相册'):
                    self._adb.click(0)
                    self.choice_images()
        else:
            self._group_index += 1
            self.search_group()

    def input_txt(self):
        print('输入文字')
        time.sleep(1)
        for _str in self._groups_txt_list:
            print(_str)
            self._adb.adb_input_chinese(_str)
            self._adb.adb_keyboard(66)
            time.sleep(1)

    def search_group(self):
        print('找群')
        if self.wechats_index < len(self._group_list) or self._group_index < len(self._group_list[self.wechats_index]):
            self._adb.refresh_nodes()
            if self._adb.find_nodes_by_text(self._group_list[self.wechats_index][self._group_index]):
                print('找到' + self._group_list[self.wechats_index][self._group_index])
                self._adb.click(0)
                time.sleep(1)
                self._adb.refresh_nodes()
                if self._adb.find_nodes_by_content('切换到按住说话'):
                    self._adb.adb_click(500, 1900)
                elif self._adb.find_nodes_by_content('切换到键盘'):
                    self._adb.click(0)
                self.input_txt()
                self._adb.refresh_nodes()
                if self._adb.find_nodes_by_text('发送'):
                    self._adb.click(0)
                    self.send_image()
        else:
            self._adb.adb_put_back()
            self._adb.adb_put_back()
            self._adb.adb_put_back()
            self._adb.adb_put_back()
            self._adb.adb_put_back()
            self.clean_wechat()
            self.wechats_index += 1
            self.find_wechat()

    def search_book(self):
        self._adb.click_by_text_after_refresh('通讯录')
        self._adb.click_by_text_after_refresh('群聊')
        time.sleep(2)
        self.search_group()

    # 找到需要打开的微信
    def find_wechat(self):
        self._group_index = 0
        self._adb.refresh_nodes()
        time.sleep(2)
        if self.wechats_index < len(self._wechat_list):
            if self._adb.find_nodes_by_text(self._wechat_list[self.wechats_index]):
                print('找到' + self._wechat_list[self.wechats_index])
                self._adb.click(0)
                time.sleep(15)
                self.search_book()

    def test(self):
        self._adb.refresh_nodes()

    def main(self):
        # self.test()
        try:
            self.find_wechat()
        except KeyboardInterrupt as e:
            print('e', e)


from flask import abort, make_response

from config import db
from models import Person, person_schema, people_schema

def create(person):
    """ Creates a new person in the people structure """

    lname = person.get("lname")
    existing_person = Person.query.filter(Person.lname == lname).one_or_none()

    if existing_person is None:
        new_person = person_schema.load(person, session=db.session)
        db.session.add(new_person)
        db.session.commit()

        return person_schema.dump(new_person), 201

    abort(
        406,
        f"Person with last name {lname} already exists"
    )

def read_all():
    """Returns all people from the people structure"""

    people = Person.query.all()

    return people_schema.dump(people)

def read_one(lname):
    """Returns one person for the given last name"""

    person = Person.query.filter(Person.lname == lname).one_or_none()

    if person:
        return person_schema.dump(person)

    abort(
        404,
        f"Person with last name {lname} not found"
    )

def update(lname, person):
    """ Updates an existing person in the people structure """

    existing_person = Person.query.filter(Person.lname == lname).one_or_none()

    if existing_person:
        update_person = person_schema.load(person, session=db.session)
        existing_person.fname = update_person.fname
        existing_person.lname = update_person.lname

        db.session.merge(existing_person)
        db.session.commit()

        return person_schema.dump(existing_person), 200

    abort(
        404,
        f"Person with last name {lname} not found"
    )

def delete(lname):
    """ Deletes a person from the people structure """

    existing_person = Person.query.filter(Person.lname == lname).one_or_none()

    if existing_person:
        db.session.delete(existing_person)
        db.session.commit()

        return make_response(
            f"Person with last name {lname} successfully deleted", 204
        )

    abort(
        404,
        f"Person with last name {lname} not found"
    )


from os import listdir
from os.path import isfile, join
import argparse

def get_file_names(folderpath,out='output.txt', write_or_abend='w'):
    """ takes a path to a folder and writes all filenames in the folder to a specified output file"""
    # Make a list of files & dirs and then remove everything that is not a file
    files_and_dir = listdir(folderpath)
    lst = [file for file in files_and_dir if isfile(join(folderpath, file))]
    print(f'Files in {folderpath}: ', lst)

    # Convert list to string and write to new file.
    list_as_string = f'/{folderpath}/: ' + ' '.join(lst)
    with open(f'data/{out}', write_or_abend) as file_object:
        # Make line break if its a sub folder being abended
        if write_or_abend == 'a':
            file_object.write('\n')
        file_object.write(list_as_string)

    # Makes sure it doesn't print this msg out if its a sub folder
    if write_or_abend == 'w':
        print(f'{out} has been created with the file names in the data folder.')

def get_all_file_names(folderpath,out='output.txt'):
    """takes a path to a folder and write all filenames recursively (files of all sub folders to)"""
    # First run the other function
    get_file_names(folderpath, out)

    # Now find the sub folders
    files_and_dir = listdir(folderpath)
    sub_folders = [file for file in files_and_dir if not isfile(join(folderpath, file))]
    for sub in sub_folders:
        fp = f'{folderpath}/{sub}'
        get_file_names(fp, out, 'a')

def print_line_one(file_names):
    """takes a list of filenames and print the first line of each"""
    # Using idx and enumerate just for the readability and for practicing.
    for idx, file in enumerate(file_names):
        with open(file) as data:
            for line in data:
                print(idx, line)
                # Breaking after 1 line
                break

def print_emails(file_names):
    """takes a list of filenames and print each line that contains an email (just look for @)"""
    for file in file_names:
        print(f'Lines with email adresses in {file}: ')
        print('-----------------------------------------------------------')
        with open(file) as data:
            for idx, line in enumerate(data):
                if '@' in line:
                    print(f'{idx}:', line)

def write_headlines(md_files, out='output.txt'):
    """takes a list of md files and writes all headlines (lines starting with #) to a file"""
    for file in md_files:
        with open(file) as data:
            for line in data:
                if '#' in line[0]:
                    print(line)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='A program that creates a txt files with all the files in the path folder, including the subfolders')
    parser.add_argument('path', help='File path for the csv file')
    parser.add_argument('--out', help='The name of the output file. Default output.txt')

    args = parser.parse_args()

    get_all_file_names(args.path, f'{args.out}.txt')


from src.Display.Input import Input
from src.DataSource.WriteToFile import WriteToFile
from src.DataSource.DataSourceConstants import *

class InputConsole(Input):

    write_to_file = True
    user_input_write_to_file = WriteToFile(USERACTION_FOLDER,INPUT_LOG)

    def get_input_string(self, request):
        user_input = input(request)
        if self.write_to_file:
            self.user_input_write_to_file.write_to_file(user_input)
        return user_input


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar  6 10:26:16 2020

@author: bencohen
"""
import numpy as np
import skimage
from skimage.color import rgb2gray
from skimage.io import imread
import matplotlib.pyplot as plt
import os

# allow for device input, comment out to use shortcut
#directory = input("Enter directory path: ")
#os.chdir(directory)
#name = input("Enter image name: ")

#shortcut - change "directory", change file "name"
directory = r"C:\Users\joekh\Documents\GitHub\ML-Breat_Cancer_Classfier\images\contrast_images"
os.chdir(directory)
name = 'clear'

if(os.path.exists(name + '.jpeg')):
    image = skimage.color.rgb2gray(skimage.io.imread(directory + "\\" + name + '.jpeg'))
    y = image[400,:]
    x = np.arange(len(y))

    plt.plot(x, y)
    plt.xlabel('Pixel')
    plt.ylabel('Greyscale Value')
    plt.title('Clear')
    plt.ylim([0, 1])
    plt.show()
else:
    print("File does not exist.")


from pabiana import Area, repo
from pabiana.utils import multiple

from . import utils

area = Area(repo['area-name'], repo['interfaces'])
premise = utils.setup
config = {
	'clock-name': 'clock',
	'clock-slots': multiple(4, 6),
	'context-values': {
		'temperature': 10,
		'humidity': 40
	}
}

@area.pulse
def publish():
	area.publish({
		'temperature': area.context['temperature'],
		'humidity': area.context['humidity']
	})


import django.views.generic.base as base
import django.http as http
import text_speech.forms as forms
import text_speech.amazon_polly as amazon_polly
import os

class text_speech(base.TemplateView):
    template_name = "text_speech/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["form"] = forms.input_form()
        return context

    @staticmethod
    def post(request, *args, **kwargs):
        # create a form instance and populate it with data from the request:
        form = forms.input_form(request.POST)

        # check whether it's valid:
        if form.is_valid():
            # process the data
            text = form.cleaned_data["text"]
            voice = form.cleaned_data["voice"]
            speed = request.POST["speed"]

            return_text = amazon_polly.amazon_polly(text=text, voice=voice, speed=speed)

            return http.JsonResponse({"return_text": return_text}, status=200)
        else:
            return http.JsonResponse({"error": form.errors}, status=400)


import json
import random

def progress(str, i, end):
    barLen = 30
    print(
        f'{str} {i}/{end}\t[{"="*(i*barLen//end)}>{" "*(barLen-i*barLen//end)}]', end='\r')

print('Getting data...')
print('Loading json...')

with open('moves.json', 'r') as f:
    json_moves = json.load(f)

print('Creating conv_tables...')

board_size = 8

moves_conv_tbl = {(fromx, fromy, tox, toy): (fromx * board_size**3 + fromy *
                  board_size**2 + tox * board_size + toy) for fromx in range(
    board_size) for fromy in range(board_size) for tox in range(board_size) for toy in range(board_size)}

pieces_types_num = 6

def get_pieces_conv_tbl_list(i):
    conv_tbl_list = [0] * (pieces_types_num * 2 + 1)
    conv_tbl_list[i] = 1
    return conv_tbl_list

pieces_conv_tbl = [get_pieces_conv_tbl_list(
    i) for i in range(pieces_types_num * 2 + 1)]

def get_boards_moves(winner_index, moves):
    _boards = []
    _moves = []
    board = [
        [8, 9, 10, 11, 12, 10, 9, 8],
        [7, 7, 7, 7, 7, 7, 7, 7],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1, 1, 1, 1],
        [2, 3, 4, 5, 6, 4, 3, 2]
    ]
    for row in board:
        for i, cell in enumerate(row):
            row[i] = pieces_conv_tbl[cell]
    for i, m in enumerate(moves):
        if i % 2 == winner_index:
            _boards.append(board)
            _moves.append(moves_conv_tbl[tuple(m)])
        board[m[2]][m[3]] = board[m[0]][m[1]]
        board[m[0]][m[1]] = pieces_conv_tbl[0]
    return _boards, _moves

def get_train_data(train_len=None, test_len=None):
    temp = []
    for i, round in enumerate(json_moves):
        progress('Creating data', i, len(json_moves))
        winner = round[0]
        moves = round[1]
        if winner == '1-0' or winner == '1/2-1/2':
            temp.append(get_boards_moves(0, moves))
        if winner == '0-1' or winner == '1/2-1/2':
            temp.append(get_boards_moves(1, moves))

    print('\nSampling data...')
    if (train_len is not None) and (test_len is not None):
        train = random.sample(temp, train_len)
        test = random.sample(temp, test_len)
    else:
        train = temp
        test = random.sample(temp, len(temp) / 10)
    print('Converting data...')
    train_boards = []
    train_moves = []
    for b, m in train:
        train_boards.extend(b)
        train_moves.extend(m)
    test_boards = []
    test_moves = []
    for b, m in test:
        test_boards.extend(b)
        test_moves.extend(m)

    return train_boards, train_moves, test_boards, test_moves


import numpy as np
from soundquilter import quilter as qtr
import pytest

# TODO: how to test houskeeping methods in the app
SHOW = False
PLAY = False

# PATH_TO_REF_ARRAYS = "soundquilter/tests/ref/"
PATH_TO_REF_ARRAYS = "ref/"

def test_split_array():
    # should work only when signal size evenly divisible by segment length
    signal = np.ones([30, 50])
    len_segment_samples = 5
    segments = qtr.split_array(signal, len_segment_samples)
    assert segments.shape[0] == signal.shape[-1] // len_segment_samples
    assert segments[0].shape == (30, 5)

def test_make_window():
    window_reference = np.load(PATH_TO_REF_ARRAYS + "window.npy")
    len_overlap_samples = 600
    len_segment_samples = 1200
    len_sides = len_overlap_samples
    len_middle = len_segment_samples - len_overlap_samples
    window = qtr.make_window(len_sides, len_middle)
    assert (window == window_reference).all()

def test_compute_distances():
    # TODO: test with borders where the 2 arrays compared are not the same
    arrays = np.load(PATH_TO_REF_ARRAYS + "arrays_for_compute_distance.npy")
    distance_matrix = qtr.compute_distances(arrays)
    assert np.trace(distance_matrix) == 0  # sum along diagonal should be zero
    assert np.all(distance_matrix.shape == (arrays.shape[0], arrays.shape[0]))
    # TODO: this part is failing but not clear the functionality is needed
    # # test case where we pass 2 sets of different number of arrays
    # arrays_2 = arrays.copy()[0:3, ...]
    # distance_matrix = qtr.compute_distances(arrays, arrays_2)
    # assert np.trace(distance_matrix) == 0  # sum along diagonal should be zero
    # assert np.all(distance_matrix.shape == (arrays.shape[0], arrays_2.shape[0]))

def test_find_sequence_similar_diagonal():
    # TODO: test with sensible distances
    distance_matrix = np.arange(1, 101).reshape([10, 10]).astype(np.float)
    len_sequence = 6
    index_sequence = qtr.find_sequence_similar_diagonal(distance_matrix, len_sequence)
    assert len(index_sequence) == len_sequence

def test_find_optimal_location():
    vector = np.sin(2*np.pi * np.linspace(0, 1, 10))
    candidates = np.random.randn(100) * 0.3  # small amplitude noise
    actual_best_location = 20
    candidates[actual_best_location:30] = vector  # simulate a region where it should yield highest correlation
    obtained_best_location = qtr.find_optimal_location(vector, candidates)
    assert obtained_best_location == actual_best_location

def test_join_segments_psola():
    signal_size = int(2e4) * 7
    signal = np.random.randn(signal_size)
    len_segment = 1200
    ordered_initial_locations = np.arange(
        len_segment, signal_size - len_segment, len_segment
        )
    window = np.load(PATH_TO_REF_ARRAYS + "window.npy")
    max_shift = 300
    len_overlap = 600
    joined_segments = qtr.join_segments_psola(
        signal, ordered_initial_locations,
        window, max_shift,
        len_segment, len_overlap
        )

@pytest.mark.smoke
def test_sound_quilter():
    from scipy.signal import spectrogram, resample_poly
    import soundfile as sf
    np.random.seed(seed=12345678)
    srate = int(2e4)
    # num_secs = 7
    # num_samples = num_secs * srate
    path2signal = PATH_TO_REF_ARRAYS + "Laughter.wav"

    signal, srate_loaded = sf.read(path2signal, dtype="float32")
    if signal.ndim > 1:  # if stereo, to mono and normalize
        signal = signal.sum(axis=1) / np.abs(signal.sum(axis=1)).max()
    if srate_loaded != srate:  # if different sampling rate, resample
        signal = resample_poly(signal, srate, srate_loaded, axis=0)

    # TODO: delte the following line
    # signal = signal[0:4800]

    quilter = qtr.SoundQuilter()
    config = {
        # Attributes set by the user
        "srate": srate,
        "len_segment_samples": 2400,
        "len_overlap_samples": 600,
        "len_quilt_samples": 2400 * 6,  # int(srate * 4.0),
        "len_feature_border_samples": 300,  # defines the extent to use for distance calculation
        # "_distance_metric": None,  # should work with broadcasting
        # for selecting via maximizing cross-correlation (PSOLA),
        "max_shift_samples": 300,
        "signal": signal,
        "distance_metric": "sqerror"
        }
    quilter.configure(config)
    quilter.register_custom_transform(lambda x: spectrogram(x, srate)[2])
    quilt = quilter.make_quilt()

    assert quilt.shape[0] == config["len_quilt_samples"]

    if SHOW:
        from matplotlib import pyplot as plt
        plt.plot(quilt)
    if PLAY:
        from pycochleagram.utils import play_array
        play_array(quilt, srate, rescale="normalize", ignore_warning=True)
        # play_array(signal, srate, rescale="normalize", ignore_warning=True)


import random

def selection_sort(arr):
    for i in range(len(arr)):
        min_val = arr[i]
        min_idx = i
        for j in range(i, len(arr)):
            if arr[j] < min_val:
                min_val = arr[j]
                min_idx = j
        tmp = arr[min_idx]
        arr[min_idx] = arr[i]
        arr[i] = min_val
    return arr

for _ in range(5):
    arr = [random.randint(0, 100) for _ in range(10)]
    print(arr)
    print(selection_sort(arr))
    print("----------")


import random
import numpy as np
import matplotlib.pyplot as plt
import sys

#參數
learn_rate = 0.2
epoch = 100
accuracy = 0.9

#input data
data = []
sol = []
sol_class = []

#store final solution
best_ac = 0.0
best_w = []
final_ac = 0.0

def data_input(path):
     #read file
     file_c = False
     while(file_c == False):
          try:
               f = open(path)
               file_c = True
          except:
               print("can't find file")
               return False

     for line in f:
          #repace '\n' to ''
          line = line.replace('\n', '')
          #split and string to int
          sp = line.split(" ")
          data_x = [-1]
          data_y = 0
          count = 0
          for item in sp:
               if(count == (len(sp)-1)):
                    data_y = int(item)
               else:
                    data_x.append(float(item))
               count = count + 1

          data.append(data_x)
          sol.append(data_y)

          #紀錄class label
          if len(sol_class) == 0:
               sol_class.append(data_y)
          else:
               ch = True
               for item in sol_class:
                    if(item == data_y):
                         ch = False
                         break
               if(ch == True):
                    sol_class.append(data_y)
     f.close()

def paint(data, sol, status ):
     x = []
     y = []
     x2 = []
     y2 = []
     for i  in range(len(data)):
          for j in range(len(data[i])):

               if(sol[i] == 1):
                    x.append(data[i][1])
                    y.append(data[i][2])
               else:
                    x2.append(data[i][1])
                    y2.append(data[i][2])
     max_x = 0
     min_x = 0
     max_y = 0
     min_y = 0
     # 座標最大 最小
     if(len(x) != 0 and len(x2)!= 0):
          max_x = max([max(x), max(x2)])
          min_x = min([min(x), min(x2)])
     elif(len(x) != 0):
          max_x = max(x)
          min_x = min(x)
     elif(len(x2)!= 0):
          max_x = max(x2)
          min_x = min(x2)

     if(len(y) != 0 and len(y2)!= 0):
          max_y = max([max(y), max(y2)])
          min_y = min([min(y), min(y2)])
     elif(len(y) != 0):
          max_y = max(y)
          min_y = min(y)
     elif(len(y2)!= 0):
          max_y = max(y2)
          min_y = min(y2)

     plt.plot(x, y ,'r^')
     plt.plot(x2, y2 ,'gs')
     if(best_w[2] != 0):
          plt.plot([max_x, min_x], [(best_w[0] - best_w[1] * max_x)/best_w[2], (best_w[0] - best_w[1] * min_x)/best_w[2]] ,'y--')
     else:
          plt.plot([max_x, min_x], [max_y, min_y] ,'y--')

     #存檔的path
     path_spilt = path.split('\\')
     path_spilt = path.split('/')
     name = (path_spilt[len(path_spilt)-1].split('.'))[0]
     try:
          if(status == 0):
               plt.savefig('dataset/image/' + name + '_train_data.jpg')
          else:
               plt.savefig('dataset/image/' + name + '_all_data.jpg')
     except:
          print("--------*******************************************  -----")
          print("--------   can't find path, path is dataset/image/   -----")
          print("--------*******************************************  -----")
     plt.show()

def data_split():
     data_train = []
     sol_train = []
     data_test = []
     sol_test = []
     #use random()
     for j in range(len(data)):
          if(random.random() >= 0.33):
               data_train.append(data[j])
               sol_train.append(sol[j])
          else:
               data_test.append(data[j])
               sol_test.append(sol[j])

          #防止data_test沒有資料
          if(len(data_test) == 0):
               ran = random.randint(0, len(data_train)-1)
               data_test.append(data_train[ran])
               sol_test.append(sol[j])
               del data_train[ran]
               del sol_train[ran]

     return data_train, sol_train, data_test, sol_test

def train():
     global best_ac
     global best_w
     global accuracy
     global final_ac

     #初始weight

     w = [-1]
     #初始weight
     if(len(sol_class) > 2):
          for i in range(len(sol_class)):
               w.append(random.random())
     else:
          w = [-1,1, 0]

     data_train = []
     data_test = []
     sol_train = []
     sol_test = []
     predict = 0

     pre_ac = 0
     pre_w = []
     data_train, sol_train, data_test, sol_test = data_split()

     #start train
     for i in range(epoch):
          predict = 0
          for j in range(len(data_train)):
               sum = 0
               for k in range(len(data_train[j])):
                    sum = sum + data_train[j][k] * w[k]

               if(sum >= 0):
                    predict = sol_class[0]
                    if(predict != sol_train[j]):
                         for k in range(len(w)):
                              w[k] = w[k] - (learn_rate)/(1+i/10) * data_train[j][k]
               if(sum < 0):
                    predict = sol_class[1]
                    if(predict != sol_train[j]):
                         for k in range(len(w)):
                              w[k] = w[k] + (learn_rate)/(1+i/10) * data_train[j][k]

          #evalute
          count = 0
          for j in range(len(data_train)):
               sum = 0
               for k in range(len(data_train[j])):
                    sum = sum + data_train[j][k] * w[k]

               if(sum >= 0):
                    predict = sol_class[0]
               if(sum < 0):
                    predict = sol_class[1]
               if(predict == sol[j]):
                    count = count + 1
          print("epoch: " + str(i+1) + "\ntrain accuracy:", count/len(data_train))

          count = 0
          for j in range(len(data_test)):
               sum = 0
               for k in range(len(data_test[j])):
                    sum = sum + data_test[j][k] * w[k]

               if(sum >= 0):
                    predict = sol_class[0]
               if(sum < 0):
                    predict = sol_class[1]
               if(predict == sol_test[j]):
                    count = count + 1
          ac = count/len(data_test)

          if(pre_ac > ac):
               w = pre_w
               ac = pre_ac
          else:
               pre_ac = ac
               pre_w = w

          #record best accuracy
          if(best_ac < ac):
               best_ac = ac
               best_w = w

          print("test accuracy:", str(ac))

          count = 0
          for j in range(len(data)):
               sum = 0
               for k in range(len(data[j])):
                    sum = sum + data[j][k] * w[k]

               if(sum >= 0):
                    predict = sol_class[0]
               if(sum < 0):
                    predict = sol_class[1]
               if(predict == sol[j]):
                    count = count + 1

          final_ac = count/len(data)
          print("total_accuracy", final_ac)
          if(best_ac >= accuracy):
               paint(data_train, sol_train, 0)
               return

     if(len(sol_class) <= 2):
          paint(data_train, sol_train, 0)

if __name__ == '__main__':
     while(True):
          data = []
          sol = []
          sol_class = []
          best_ac = 0.0
          best_w = []
          final_ac = 0.0
          path = str(input("檔案路徑: "))
          c1 = False
          c2 = False
          c3 = False
          while(True):
               if(c1 == False):
                    try:
                         learn_rate = float(input("type:float learning_rate: "))
                         break
                    except:
                         print("learning_rate error")
          while(True):
               if(c2 == False):
                    try:
                         epoch = int(input("type:int epoch: "))
                         break
                    except:
                         print("epoch error")
          while(True):
               if(c3 == False):
                    try:
                         accuracy = float(input("type:float accuracy: "))
                         break
                    except:
                         print("accuracy error")

          if(data_input(path) != False):
               train()
               print("test data  accuracy: " + str(best_ac),)
               print("wieght ", end = "")
               print(best_w)

               print("all data accuracy: ", final_ac)
               if(len(sol_class) <= 2):
                    paint(data, sol, 1)
          print("")

          status = int(input("input->1 continue, input->2 stop: "))
          if(status == 2):
               sys.exit()


import torch
import torch.nn as nn

import torchvision
from torchvision import transforms
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor

import string
import cv2
import json
from PIL import Image
import os
import numpy as np
import random
from tqdm import tqdm
import shutil

EPOCHS = 1000
IMAGE_PATH = 'data'
BEST_MODEL_DIR = 'model'
IMAGE_SIZE = (600, 600)
BATCH_SIZE = 2
LR = 1e-3

if torch.cuda.is_available():
    device = 'cuda'
else:
    device = 'cuda'

def construct_dict(x): return {k: [] for k in range(1, x + 1)}

def images_with_annotations(image_path, annotations_path):
    with open(annotations_path, 'r') as fp:
        content = json.load(fp)

    class_names = {category['id']: category['name']
                   for category in content['categories']}
    class_names[0] = 'background'

    images = {image['id']: image['file_name'] for image in content['images']}

    for key, val in images.items():
        image = Image.open(os.path.join(image_path, val))
        images[key] = image
    img_annots = {k: [] for k in range(1, len(images) + 1)}

    for annot in content['annotations']:
        img_annots[annot['image_id']].append(annot)

    segmentations = {k: [] for k in range(1, len(images) + 1)}
    categories = {k: [] for k in range(1, len(images) + 1)}
    for val in img_annots.values():
        for annot in val:
            categories[annot['image_id']].append(annot['category_id'])
            segmentation = annot['segmentation'][0]
            segmentations[annot['image_id']].append(
                [[segmentation[i], segmentation[i + 1]] for i in range(0, len(segmentation) - 1, 2)])

    return images, segmentations, categories, class_names

def construct_masks(images, segmentations):
    masks = {}
    for key, val in images.items():
        tmp = []
        for segmentation in segmentations[key]:
            mask = np.zeros((val.size[1], val.size[0]), dtype=np.uint8)
            cv2.fillPoly(mask, np.array(
                [segmentation], dtype=np.int32), 255)  # type: ignore
            tmp.append(mask)
        masks[key] = tmp
    return masks

def display_masks_images(images, targets):
    for i in range(len(images)):
        image = np.array(images[i].permute(1, 2, 0).cpu().numpy() * 255, dtype=np.uint8)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        masks = targets[i]['masks'].cpu().numpy()
        boxes = targets[i]['boxes'].cpu().numpy()
        for mask in masks:
            alpha = 0.5
            image[mask == 1] = (1 - alpha) * image[mask == 1] + alpha * np.array([0, 255, 0])
        for box in boxes:
            x1, y1, x2, y2 = np.array(box, dtype=np.int32)
            cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.imshow('image', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

def resize_images(images, masks, size):
    for key, val in images.items():
        images[key] = val.resize(size)

        tmp = []
        for mask in masks[key]:
            mask = cv2.resize(mask, size)
            tmp.append(mask)
        masks[key] = tmp

        images[key] = cv2.cvtColor(np.array(images[key]), cv2.COLOR_BGR2RGB)
    return images, masks

def augment_data(images, masks, categories, class_names):
    targets = {}

    for k, v in masks.items():
        data = {}
        tmp = []
        li = []
        for mask in v:
            tmp_mask = (mask > 0).astype(np.uint8)
            x, y, w, h = cv2.boundingRect(tmp_mask)
            if h == 0 or w == 0:
                continue

            li.append([x, y, x + w, y + h])
            tmp.append(tmp_mask)
        data['masks'] = torch.tensor(tmp, dtype=torch.uint8)
        data['boxes'] = torch.tensor(li, dtype=torch.float32)
        data['labels'] = torch.tensor(
            [c for c in categories[k]], dtype=torch.int64)
        targets[k] = data

    return images, targets

def _process_image(images, size):
    # resize images if not correct size
    imgs = [cv2.resize(img, size) for img in images]

    # process images
    imgs = torch.stack([torch.tensor(img, dtype=torch.float32)
                       for img in images], 0)
    imgs = imgs.permute(0, 3, 1, 2)
    imgs = imgs / 255.0
    # imgs = imgs - torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1)
    # imgs = imgs / torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
    return imgs

def _augment_training_data(images, targets):
    res_images = []
    res_targets = []
    hflip_transform = transforms.RandomHorizontalFlip(p=0.5)

    for i in range(len(images)):
        image = images[i].permute(1, 2, 0).cpu().numpy()
        masks = targets[i]['masks'].cpu().numpy()

        # random color jitter
        image_pil = Image.fromarray(image.astype(np.uint8))
        image_pil = transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)(image_pil)

        # random horizontal flip
        if random.random() > 0.5:
            image_pil = hflip_transform(image_pil)
            image = np.array(image_pil)
            masks = [cv2.flip(mask, 1) for mask in masks]
            boxes = []
            for mask in masks:
                x, y, w, h = cv2.boundingRect(mask)
                if h == 0 or w == 0:
                    continue
                boxes.append([x, y, x + w, y + h])
        else:
            image = np.array(image_pil)
            boxes = targets[i]['boxes'].cpu().numpy()

        res_images.append(torch.tensor(image, dtype=torch.float32).permute(2, 0, 1))
        res_targets.append({'masks': torch.tensor(masks, dtype=torch.uint8), 'boxes': torch.tensor(boxes, dtype=torch.float32), 'labels': targets[i]['labels']})

    return res_images, res_targets

class Data:

    def __init__(self, image_path, size=(600, 600), batch_size=2):
        self.train_images, self.train_targets = self._data_pipeline(
            image_path, os.path.join(image_path, 'train_annotations.json'), size)
        self.test_images, self.test_targets = self._data_pipeline(
            image_path, os.path.join(image_path, 'test_annotations.json'), size, train=False)
        self.batch_size = batch_size

    def _data_pipeline(self, image_path, annotations_path, size, train=True):
        images, segmentations, categories, class_names = images_with_annotations(
            image_path, annotations_path)
        masks = construct_masks(images, segmentations)
        images, masks = resize_images(images, masks, size)
        images, targets = augment_data(images, masks, categories, class_names)
        images, targets = list(images.values()), list(targets.values())

        images = _process_image(images, size)
        # images = list(image.to(device) for image in images)
        # targets = [{k: v.to(device) for k,v in t.items()} for t in targets]
        if train:
            images, targets = _augment_training_data(images, targets)
        self.num_classes = len(class_names)
        return images, targets

    def train_generator(self):
        return Generator(self.train_images, self.train_targets, self.batch_size)

    def test_generator(self):
        return Generator(self.test_images, self.test_targets, self.batch_size)

class Generator:

    def __init__(self, data, targets, batch_size):
        self.data = data
        self.targets = targets

        array = list(range(len(self.data)))
        random.shuffle(array)
        self.data = [self.data[i] for i in array]
        self.targets = [self.targets[i] for i in array]

        self.batch_size = batch_size

    def __len__(self):
        return len(self.data) // self.batch_size

    def __getitem__(self, idx):
        if len(self.data) % self.batch_size != 0 and idx == len(self) - 1:
            return self.data[idx * self.batch_size:], self.targets[idx * self.batch_size:]
        return self.data[idx * self.batch_size: (idx + 1) * self.batch_size], self.targets[idx * self.batch_size: (idx + 1) * self.batch_size]

    def __iter__(self):
        for i in range(len(self)):
            yield self[i]

class Model(nn.Module):

    def __init__(self, num_classes, pretrained=True):
        super(Model, self).__init__()
        self.model = torchvision.models.detection.maskrcnn_resnet50_fpn(
            pretrained=pretrained)
        in_features = self.model.roi_heads.box_predictor.cls_score.in_features  # type: ignore
        self.model.roi_heads.box_predictor = FastRCNNPredictor(
            in_features, num_classes)

    def forward(self, x, y):
        return self.model(x, y)

    def predict(self, image):
        # resize the image
        image = cv2.resize(image, (600, 600))

        # convert image to tensor
        image = torch.tensor(image, dtype=torch.float32)

        # permute image
        image = image.permute(2, 0, 1)

        # normalize image
        image = image / 255.0
        image = image - torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1)
        image = image / torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)

        # add batch dimension
        image = image.unsqueeze(0)

        # predict
        self.model.eval()
        with torch.no_grad():
            return self.model(image)

class Trainer:

    def __init__(self, model, optimizer, epochs=EPOCHS):
        self.model = model
        self.optimizer = optimizer
        self.epochs = epochs

    def train(self, epoch, train_loader, verbose=True):
        pbar = tqdm(train_loader, total=len(train_loader))
        for x, y in pbar:
            self.model.train()
            self.optimizer.zero_grad()
            loss_dict = self.model(x, y)
            losses = sum(loss for loss in loss_dict.values())

            losses.backward()  # type: ignore
            self.optimizer.step()
        if verbose:
            print(f'Train: Epoch={epoch} | Loss={losses}')  # type: ignore
        return losses  # type: ignore

    def test(self, epoch, test_loader, verbose=True):
        pbar = tqdm(test_loader, total=len(test_loader))
        total_loss = 0

        for x, y in pbar:
            with torch.no_grad():
                loss_dict = self.model(x, y)
                losses = sum(loss for loss in loss_dict.values())
                total_loss += losses

        if verbose:
            print(f'Test: Epoch={epoch} | Loss={total_loss/len(test_loader)}')
        return total_loss/len(test_loader)

class Helper:

    @staticmethod
    def save_model(model, path):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        torch.save(model.state_dict(), path)

    @staticmethod
    def delete_files_in_path(path):
        if os.path.exists(path):
            for file in os.listdir(path):
                os.remove(os.path.join(path, file))

    @staticmethod
    def get_best_loss():
        if os.path.exists(BEST_MODEL_DIR) and len(os.listdir(BEST_MODEL_DIR)) > 0:
            return float(os.listdir(BEST_MODEL_DIR)[0].split('--loss-')[1].split('.pth')[0])
        else:
            return float('inf')

    @staticmethod
    def early_stop(test_losses, patience=30):
        if len(test_losses) < patience:
            return False
        else:
            return test_losses.index(min(test_losses)) < len(test_losses) - patience - 1

    @staticmethod
    def load_model(model, path):
        model.load_state_dict(torch.load(path))
        return model

    @staticmethod
    def infer(model, image):
        original_size = image.shape[-2:]
        image_copy = image.copy()
        image = cv2.resize(image, IMAGE_SIZE)
        model.eval()
        with torch.no_grad():
            out = model.predict(image)
        # display image
        print(out)
        boxes = out[0]['boxes'].detach().cpu().numpy()
        scores = out[0]['scores'].detach().cpu().numpy()
        labels = out[0]['labels'].detach().cpu().numpy()
        masks = out[0]['masks'].detach().cpu().numpy()
        for box, score, label, mask in zip(boxes, scores, labels, masks):
            if score > 0.5:
                box = box.astype(np.int32)
                mask = cv2.resize(mask, original_size)
                mask = (mask > 0.5).astype(np.uint8)
                color = np.random.randint(0, 255, (1, 3), dtype=np.uint8)
                color = tuple([int(c) for c in color[0]])

                image_copy = Helper.apply_mask(
                    image_copy, mask, color, alpha=0.5)
                image_copy = cv2.rectangle(
                    image_copy, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 2)
                image_copy = cv2.putText(image_copy, str(
                    label), (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.imshow('image', image_copy)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    @staticmethod
    def apply_mask(image, mask, color, alpha):
        """Apply the given mask to the image.
        """
        for c in range(3):
            image[:, :, c] = np.where(mask == 1,
                                      image[:, :, c] *
                                      (1 - alpha) + alpha * color[c],
                                      image[:, :, c])
        return image

class DataHelper:
    @staticmethod
    def copy_missing_annotation_images(image_dir, annotation_path):
        file1 = json.load(open(annotation_path, 'r'))
        images = file1['images']
        images = [d['file_name'] for d in images]
        local_images = os.listdir(image_dir)
        for image in local_images:
            if image not in images:
                os.makedirs('missing_images', exist_ok=True)
                shutil.copy(os.path.join(image_dir, image),
                            os.path.join('missing_images', image))

    @staticmethod
    def handle_missing_category_ids(path1, id=1):
        file1 = json.load(open(path1, 'r'))

        annotations = file1['annotations']
        for annotation in annotations:
            if 'category_id' not in annotation:
                annotation['category_id'] = id

        return file1

    @staticmethod
    def handle_file_names(dir):
        for file in os.listdir(dir):
            if not file.endswith('.json'):
                random_name = ''.join(random.choices(
                    string.ascii_uppercase + string.digits, k=10))
                file_extension = file.split('.')[-1]
                os.rename(os.path.join(dir, file), os.path.join(
                    dir, random_name + '.' + file_extension))

    @staticmethod
    def combine_annotations(path1, path2):
        file1 = json.load(open(path1, 'r'))
        file2 = json.load(open(path2, 'r'))

        res_info = file1['info']

        images1 = file1['images']
        images2 = file2['images']
        images2_copy = [d.copy() for d in images2]
        initial_file_names = [d['file_name'] for d in images1]
        last_image_id = images1[-1]['id']
        images2 = [d for d in images2 if d['file_name']
                   not in initial_file_names]
        for image in images2:
            image['id'] += last_image_id
        res_images = images1 + images2
        image_conversions = {}
        for image in images2_copy:
            res_image_id = [
                img for img in res_images if img['file_name'] == image['file_name']][0]['id']
            image_conversions[image['id']] = res_image_id

        category1 = file1['categories']
        category2 = file2['categories']
        category2_copy = [d.copy() for d in category2]
        last_category_id = category1[-1]['id']
        initial_category_names = [d['name'] for d in category1]
        category2 = [d for d in category2 if d['name']
                     not in initial_category_names]
        for category in category2:
            category['id'] += last_category_id
        res_categories = category1 + category2
        category_conversions = {}
        for category in category2_copy:
            res_category_id = [
                ctg for ctg in res_categories if ctg['name'] == category['name']][0]['id']
            category_conversions[category['id']] = res_category_id

        annotation1 = file1['annotations']
        annotation2 = file2['annotations']
        last_annotation_id = annotation1[-1]['id']
        for annotation in annotation2:
            annotation['id'] += last_annotation_id
            annotation['image_id'] = image_conversions[annotation['image_id']]
            annotation['category_id'] = category_conversions[annotation['category_id']]
        res_annotations = annotation1 + annotation2

        return {'info': res_info, 'images': res_images, 'categories': res_categories, 'annotations': res_annotations}

if __name__ == '__main__':
    data = Data(image_path=IMAGE_PATH, size=IMAGE_SIZE, batch_size=BATCH_SIZE)
    train_loader = data.train_generator()
    test_loader = data.test_generator()

    model = Model(num_classes=data.num_classes)
    # model = model.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=LR)
    trainer = Trainer(model, optimizer)
    test_losses = []
    best_loss = Helper.get_best_loss()

    for epoch in range(EPOCHS):
        trainer.train(epoch, train_loader)
        loss = trainer.test(epoch, test_loader)

        test_losses.append(loss)
        if loss < best_loss:
            Helper.delete_files_in_path(BEST_MODEL_DIR)
            Helper.save_model(
                model, rf'{BEST_MODEL_DIR}/epoch-{epoch+1}--loss-{loss:.3f}.pth')
            best_loss = loss

        if Helper.early_stop(test_losses):
            print(f'Early Stopping at epoch {epoch + 1}')
            break

    # best_model_path = os.path.join(
    #     BEST_MODEL_DIR, os.listdir(BEST_MODEL_DIR)[0])
    # model = Helper.load_model(model, best_model_path)
    # model = torchvision.models.detection.maskrcnn_resnet50_fpn(2)
    # model.eval()
    # image = cv2.imread('test.jpeg')
    # image = cv2.resize(image, (224, 224))
    # image = (torch.tensor(image) / 255.0).permute(2, 0, 1).float()
    # with torch.no_grad():
    #     output = model([image])
    # print(output)
    # Helper.infer(model, cv2.imread('test.jpeg'))


from functools import reduce

from django.db.models import Q
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import JsonResponse
from django.contrib.auth.models import User
from django.db import connection

from core.exceptions import NoResultsError, PrivateProfileError, UnknownError
from core.generate_view_helper import get_team, get_teams

from players.models import (
    DataUsersTeams,
    DataUsersLeagueteamlinks,
    DataUsersLeagues,
)

def ajax_team_by_name(request):
    if request.user.is_authenticated:
        current_user = request.user
    else:
        current_user = "guest"

    teams_found = list()

    teamname = request.GET.get('teamname', None)
    if teamname and len(teamname) > 1:
        teamname = teamname.split()  # split space
        query = reduce(lambda x, y: x | y, [
                       Q(teamname__unaccent__icontains=name) for name in teamname])

        teams = list(DataUsersTeams.objects.for_user(current_user).filter(
            query).all().order_by('-overallrating', 'teamid')[:12].iterator())

        for team in teams:
            t = dict()
            t['teamid'] = team.teamid
            t['overallrating'] = team.overallrating
            t['teamname'] = team.teamname
            teams_found.append(t)

    data = {
        'teams': teams_found,
    }

    return JsonResponse(data)

def ajax_leagues(request):
    if request.user.is_authenticated:
        current_user = request.user
    else:
        current_user = "guest"

    selected = request.GET.get('selected', None)

    if selected:
        selected = list(selected.split(","))
        leagues = list(DataUsersLeagues.objects.for_user(
            current_user).all().filter(Q(leagueid__in=selected)).values())
    else:
        leagues = list(DataUsersLeagues.objects.for_user(
            current_user).all().values())

    data = {
        'leagues': leagues,
    }

    return JsonResponse(data)

def teams(request):
    try:
        context = get_teams(request, paginate=True)
        return render(request, 'teams/teams.html', context=context)
    except (NoResultsError, PrivateProfileError, UnknownError) as e:
        messages.error(request, e)
        return redirect('home')

def team(request, teamid):
    try:
        context = get_team(request, teamid=teamid)
        return render(request, 'teams/team.html', context=context)
    except (NoResultsError, PrivateProfileError, UnknownError) as e:
        messages.error(request, e)
        return redirect('teams')


import datetime
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.management import BaseCommand
from django.db import IntegrityError
from education.models import Role, UserProfile
from rapidsms.contrib.locations.models import Location, LocationType
from poll.models import Poll, Category

class Command(BaseCommand):

    def handle(self, *args, **options):
        Role.objects.get_or_create(name='Admins')
        admin = User.objects.get_or_create(username='admin')[0]
        admin.set_password('admin')
        admin.is_staff = True
        admin.is_superuser = True
        admin.save()
        UserProfile.objects.get_or_create(
            name='Admins',
            location=Location.objects.get(
                name='Kampala',
                type=LocationType.objects.get(slug='district')),
            role=Role.objects.get(name='Admins'),
            user=User.objects.get(username='admin'))
        try:
            Site.objects.get_or_create(
                id=2,
                domain="edtrac.unicefuganda.org",
                name="edtrac")
        except IntegrityError:
            pass

        poll_names = [
            'edtrac_violence_boys',
            'edtrac_violence_girls',
            'edtrac_violence_reported',
            'edtrac_upe_grant',
            'edtrac_smc_meetings',
            'edtrac_smc_meals',
            'edtrac_head_teachers_attendance',
            'edtrac_f_teachers_deployment',
            'edtrac_m_teachers_deployment',
            'edtrac_f_teachers_attendance',
            'edtrac_m_teachers_attendance',
            'edtrac_boysp3_attendance',
            'edtrac_boysp6_attendance',
            'edtrac_girlsp3_attendance',
            'edtrac_girlsp6_attendance',
            'edtrac_boysp3_enrollment',
            'edtrac_boysp6_enrollment',
            'edtrac_girlsp3_enrollment',
            'edtrac_girlsp6_enrollment',
            'edtrac_headteachers_meals',
            'edtrac_gem_abuse',
            'edtrac_girls_violence',
            'edtrac_p3curriculum_progress',
        ]

        Poll.objects.all().delete()
        for poll_name in poll_names:
            Poll.objects.get_or_create(
                name=poll_name,
                start_date=datetime.datetime.now(),
                type='n',
                default_response='',
                user=User.objects.get(username='admin'))
            Category.objects.get_or_create(
                name='yes',
                poll=Poll.objects.get(name=poll_name))


class Solution:
    def complexNumberMultiply(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        real_a, img_a = a.split('+')
        real_a = int(real_a)
        img_a = int(img_a.rstrip('i'))
        real_b, img_b = b.split('+')
        real_b = int(real_b)
        img_b = int(img_b.rstrip('i'))
        return str(real_a*real_b - img_a*img_b)+'+'+str(real_a*img_b + real_b*img_a)+'i'

a = '1+1i'
b = '1+1i'
a = '1+-1i'
b = '1+-1i'
res = Solution().complexNumberMultiply(a,b)
print(res)


from urllib import request

ck_url = "http://banggia2.ssi.com.vn/"

def download_stock_data(csv_url):
    response = request.urlopen(csv_url)
    csv = response.read()
    csv_str = str(csv)
    lines = csv_str.split("\\n")
    dest_url = r"ck.csv"
    fx = open(dest_url, "w")
    for line in lines:
        fx.write(line + "\n")
    fx.close()

download_stock_data(ck_url)


import numpy as np
import pandas as pd
from scipy import sparse as ssp
import os
import glob
import math
import pickle
import datetime
from sklearn.preprocessing import LabelEncoder,OneHotEncoder,LabelBinarizer
from sklearn.ensemble import RandomForestRegressor
from sklearn.utils import resample
from sklearn.decomposition import TruncatedSVD
# from keras.callbacks import ModelCheckpoint
# from keras import backend as K
# from keras.layers import Input, Embedding, LSTM, Dense,Flatten, Dropout, merge,Convolution1D,MaxPooling1D,Lambda
# from keras.layers.normalization import BatchNormalization
# from keras.optimizers import SGD
# from keras.layers.advanced_activations import PReLU,LeakyReLU,ELU,SReLU
# from keras.models import Model
# from keras.utils.visualize_util import plot
from gensim import corpora, models, similarities
from collections import defaultdict
from ml_metrics import rmse
import xgboost as xgb
seed = 1024
np.random.seed(seed)
dims = 32

def user_matrix(X,y,num_u,num_i):
    return ssp.csr_matrix((y,(X[:,0],X[:,1])),shape=(num_u,num_i), dtype=np.int8)

def item_matrix(X,y,num_u,num_i):
    return ssp.csr_matrix((y,(X[:,1],X[:,0])),shape=(num_i,num_u), dtype=np.int8)

def get_user_vec(u,sentences,model):
    vector = np.zeros(dims)
    for i in sentences[u]:
        vector+=model[i]
    return vector

if __name__ == '__main__':

    use_all=False

    path = "E:\\RSdata\\"
    features = ['uid','iid']
    score = 'score'

    X_train = pd.read_csv(path+"X_train.csv")
    X_test = pd.read_csv(path+"X_test.csv")
    test = pd.read_csv(path+"test.csv")

    y_train = X_train[score].values
    X_train = X_train[features].values

    y_test = X_test[score].values
    X_test = X_test[features].values

    test = test[features].values

    # X = np.concatenate([X_train,X_test,test])

    sentences = pd.read_pickle(path+'sentences.pkl')
    model = models.Word2Vec.load(path+'w2v_item_based.mdl')

    X_train,y_train = resample(X_train,y_train,n_samples = X_train.shape[0]/10, random_state =seed)
    X_test,y_test = resample(X_test,y_test,n_samples = X_test.shape[0]/10, random_state =seed)

    s = []

    for xx in X_train:
        u = xx[0]
        i = xx[1]
        u = 'u%s'%u
        i = 'i%s'%i
        u_vect = get_user_vec(u,sentences,model)
        i_vect = np.zeros(dims)
        if i in model.vocab:
            i_vect += model[i]
        tmp = np.concatenate([u_vect,i_vect]).ravel()
        s.append(tmp)

    X_train = np.array(s)
    del s

    s = []
    for xx in X_test:
        u = xx[0]
        i = xx[1]
        u = 'u%s'%u
        i = 'i%s'%i
        u_vect = get_user_vec(u,sentences,model)
        i_vect = np.zeros(dims)
        if i in model.vocab:
            i_vect += model[i]
        tmp = np.concatenate([u_vect,i_vect]).ravel()
        s.append(tmp)

    X_test = np.array(s)
    del s

    print(X_train.shape,X_test.shape)

    clf = xgb.XGBRegressor(
            learning_rate=0.3,
            n_estimators=500,
            min_child_weight=1,
            max_depth=6,
            subsample=0.7,
            colsample_bytree= 0.7,
            reg_alpha=0,
            reg_lambda=0.1,
            gamma=0,
            max_delta_step=0,
            seed=seed,
        )

    clf.fit(
        X_train,
        y_train,
        early_stopping_rounds=150,
        eval_set=[(X_train, y_train),(X_test, y_test)],
        eval_metric='rmse',
    )

    y_preds = clf.predict(X_test).ravel()
    score = rmse(y_test,y_preds)
    print('xgb rmse score',score)


import cv2
import math
import os

from src.const_spec import *
from ext import cut_detector as cd

def video2img(file, output_directory, resize=None, get_frame=0.0, unit='select', distinguish=False,
              min_frames=0, verbose=True):

    # Creating a new directory if not exists
    if not os.path.exists(output_directory):
        os.mkdir(output_directory)

    # Warning that in the output directory may be some images
    if os.listdir(output_directory) != list():
        print('[W] Output directory is not empty! %s' % os.path.abspath(output_directory))

    # Loading the video and getting frame rate
    cap = cv2.VideoCapture(file)

    # Setting the framerate to get video image
    if get_frame == 0:
        get_frame = 1
    elif unit == 'time':
        get_frame = cap.get(cv2.CAP_PROP_FPS) * get_frame
    # endif get_frame == 0 // Setting the framerate to get video image

    # it should be an integer
    get_frame = math.floor(get_frame)

    # time increment
    dt = 1000.0 / cap.get(cv2.CAP_PROP_FPS)

    # Get total frames to save
    total_frames = math.ceil(cap.get(cv2.CAP_PROP_FRAME_COUNT) / get_frame)

    # A video cutter is used to distinguish each scene
    cuts = list()
    if distinguish:
        if verbose:
            print('Using the Video Cutter')

        # Configuration for TV News
        config = cd.get_config('udalosti')

        # Score calculation to detect cuts
        scorings = cd.calculate_scorings(file, sizes=[config['size']])

        # Get cuts using TV News configuration and scorings
        cuts, means, maxs = cd.calculate_cuts(
            scorings['SAD_%d' % config['size']], config['neighbourhood_size'], config['neighbourhood_distance'],
            config['T1'], config['T2'], config['T3']
        )

        get_frame_list = list()
        cuts = [0] + cuts + [cap.get(cv2.CAP_PROP_FRAME_COUNT)]
        for i in range(0, len(cuts) - 1):
            if (cuts[i + 1] - cuts[i]) / get_frame < min_frames:
                save = int((cuts[i + 1] - cuts[i]) / min_frames)

                if save == 0:
                    save = 1
                get_frame_list.append(save)
            else:
                get_frame_list.append(get_frame)
        get_frame = get_frame_list
        cuts = cuts[1:-1]
    else:
        # The video cutter is not required
        cuts.append(math.inf)
        get_frame = [get_frame]
    # endif distinguish // A video cutter is used to distinguish each scene

    # Get number of cuts
    num_cuts = len(cuts)

    # Print information about image saving
    if verbose and min_frames in [0, 1]:
        if str(get_frame)[-1] == '1':
            termination = 'st'
        elif str(get_frame)[-1] == '2':
            termination = 'nd'
        elif str(get_frame)[-1] == '3':
            termination = 'rd'
        else:
            termination = 'th'

        print('Saving every ' + str(get_frame) + termination + ' frame.')
    # endif verbose // Print information about image saving

    # Saving images from the video
    saved = 0
    time = 0
    scene = 0
    scene_directory = ''
    while cap.isOpened():
        frame_id = int(cap.get(1))

        # Check that the video is not at the end
        ret, frame = cap.read()
        if ret is not True:
            break

        # Time to save frame
        if frame_id % get_frame[scene] == 0:
            # Use the video cutter information to divide images by scene type
            if distinguish and scene < num_cuts and frame_id >= cuts[scene]:
                # A new scene is detected - create a directory to store images
                scene += 1
                scene_directory = f'scene%04d' % scene
                if scene_directory not in os.listdir(output_directory):
                    os.mkdir(output_directory + '\\' + scene_directory)
            elif distinguish and time == 0:
                # Init directory for storing images when the video cutter is required
                scene_directory = f'scene%04d' % 0
                if scene_directory not in os.listdir(output_directory):
                    os.mkdir(output_directory + '\\' + scene_directory)

            # Get image name
            img_name = output_directory + '\\' + scene_directory + f'\\%08dms.jpg' % time

            # Resize image if required
            if resize is not None:
                frame = cv2.resize(frame, resize)

            # Save image to the output directory
            cv2.imwrite(img_name, frame)
            saved += 1

            if verbose and (saved % 50 == 0):
                if min_frames in [0, 1]:
                    print(f'[%5d/%5d] images saved' % (saved, total_frames))
                else:
                    print(f'[%5d/more than %5d] images saved' % (saved, total_frames))
        # endif frame_id % get_frame == 0 // Time to save frame

        time += dt
    # endwhile cap.isOpened() // Saving images from the video

    if verbose and (saved % 50 != 0):
        if min_frames in [0, 1]:
            print(f'[%5d/%5d] images saved' % (saved, total_frames))
        else:
            print('[the last] image saved')

    cap.release()

if __name__ == "__main__":
    VIDEO_FILE = "../data/zpravy_TV_prima.ts"
    OUTPUT_DIR = '../data/dataset_test_TV_prima'

    video2img(VIDEO_FILE, OUTPUT_DIR, resize=FRAME_SIZE, get_frame=0.2, unit='time', distinguish=True, min_frames=NUM_FRAMES)
    #video2img(VIDEO_FILE, '../data/dataset_test', resize=FRAME_SIZE, get_frame=1, unit='time', distinguish=False)


# 파이썬 실습 파일: 2-3.ECC(Group).py
import math
import numpy as np
import matplotlib.pyplot as plt

# Additive Operation
def addOperation(a, b, p, q, m):
    if q == (math.inf, math.inf):
        return p

    x1 = p[0]
    y1 = p[1]
    x2 = q[0]
    y2 = q[1]

    if p == q:
        # Doubling
        # slope (s) = (3 * x1 ^ 2 + a) / (2 * y1) mod m
        # 분모의 역원부터 계산한다 (by Fermat's Little Theorem)
        r = 2 * y1
        rInv = pow(r, m-2, m)   # Fermat's Little Theorem
        s = (rInv * (3 * (x1 ** 2) + a)) % m
    else:
        r = x2 - x1
        rInv = pow(r, m-2, m)   # Fermat's Little Theorem
        s = (rInv * (y2 - y1)) % m
    x3 = (s ** 2 - x1 - x2) % m
    y3 = (s * (x1 - x3) - y1) % m
    return x3, y3

# y^2 = x^3 + 2 * x + 2 mod 127
a = 2
b = 2
m = 127   # Prime number 이어야 함.
P = (5,1)
Q = P

allPoints = [P]
while(1):
    # R이 P의 inverse인지 확인한다. inverse이면 infinity 지점임
    # A = (x1, y1), B = (x2, y2) 일 때 x1 = x2 이고 y1 과 y2가
    # (mod m)에 대해 additive inverse 이면 infinity
    if (Q[0] == P[0]) & (abs(Q[1] - m) == P[1]):
        # 다음부터 cyclic 되므로 여기서 멈춤.
        break
    else:
        R = addOperation(a, b, P, Q, m)
        allPoints.append(R)
        Q = R

x, y = np.array(allPoints).T
plt.figure(figsize=(8,6))
plt.scatter(x, y, marker='o', color='green', alpha=0.5, s=150)
plt.show()
print(allPoints)


import os ,django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()
from faker import Faker
from product.models import Brand ,Product
import random
from random import randint

fake = Faker()

def seed_brand(n):
    for _ in range(n):
        image=['1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png', '8.png', '9.png', '10.png']

        Brand.objects.create(
            name=fake.name(),
            image=f"brand_images/{image[randint(0,9)]}"
        )
    print(f"added {n} brands successfuly ")

# seed_brand(45)

def seed_products(n):
    images=['11.jpeg', '12.jpeg', '13.jpeg', '14.jpeg', '15.jpeg', '16.jpeg', '17.jpeg', '18.jpeg', '19.jpeg']
    flag_choices =['Sale','New','Feature']
    for _ in range(n):
        Product.objects.create(
            name = fake.name(),
            subtitle =fake.text(),
            image =f'product_images/{images[randint(0,8)]}',
            price =round(random.uniform(9.99,99.99),2),
            sku =randint(10000,100000000),
            description = fake.text() ,
            flag = flag_choices[randint(0,2)],
            brand= Brand.objects.get(id=randint(134,180)),
            quantity = randint(0,10),
            tags="dummy",
        )
    print(f"added {n} Products successfuly ")

seed_products(100)


import argparse
import codecs
import json
import struct
import xml.etree.ElementTree as ElementTree
import os

import zlib

import dicttoxml
import yaml

class NodeType:
    Node = 0x00
    Boolean = 0x00
    Float = 0x01
    Int = 0x02
    Vector2 = 0x03
    Vector3 = 0x04
    Vector4 = 0x06
    String = 0x07
    Actor = 0x08
    UnknownString = 0x0f
    UnknownUnsignedInt = 0x11
    String2 = 0x14

    Values = [
        0x00,
        0x01,
        0x02,
        0x07,
        0x08,
        0x0f,
        0x11,
        0x14
    ]

    Reference = [
    ]

class AAMP:
    data_object = {}
    hash_table = {}

    def __init__(self, path):
        print("Parsing AAMP file...")

        filename = os.path.basename(path)
        print("Reading {0}...".format(filename))

        file = open(path, 'rb')
        self.data = file.read()

        signature = self.data[0x00:0x04]

        if signature != b'AAMP':
            print('\033[31mQuitting: {0} is not a AAMP file\033[0m'.format(filename))
            print('\033[31mExpected b\'AAMP\' but saw {0}\033[0m'.format(signature))
            exit(0)

        version = struct.unpack('<I', self.data[0x04:0x08])[0]
        if version != 2:
            print('\033[31mQuitting: {0} is not the correct AAMP version\033[0m'.format(filename))
            print('\033[31mExpected 2 but saw {0}\033[0m'.format(version))
            exit(0)

        # Get hashed names
        self.get_hash_table()

        root_nodes_length = struct.unpack('<I', self.data[0x18:0x1c])[0]
        pos = 0x34

        for index in range(0, root_nodes_length):
            children = {}

            node_id, unknown, offset, child_count = \
                struct.unpack('<IIHH', self.data[pos:pos + 0x0c])

            if node_id in self.hash_table:
                node_id = self.hash_table[node_id]

            node_id = str(node_id)

            self.data_object[node_id] = {}

            child_pos = offset * 4 + pos
            for child_index in range(0, child_count):
                child_node_id = struct.unpack('<I', self.data[child_pos:child_pos + 0x04])[0]
                if child_node_id in self.hash_table:
                    child_node_id = self.hash_table[child_node_id]

                child_node_id = str(child_node_id)

                children[child_node_id] = self.get_node(child_pos)
                child_pos += 0x08

            self.data_object[node_id] = children
            pos += 0x0c

    def get_hash_table(self):
        file = open('C:\\botw-data\\src\\extractors\\hashed_names.txt', 'r')
        data = file.read()
        data = data.split('\n')

        for index in range(0, len(data)):
            self.hash_table[zlib.crc32(bytearray(data[index], 'utf-8'))] = data[index]

        file = open('C:\\botw-data\\src\\extractors\\hash-number-appendix.txt', 'r')
        data = file.read()
        data = data.split('\n')

        for index in range(0, len(data)):
            self.hash_table[zlib.crc32(bytearray(data[index], 'utf-8'))] = data[index]

        file.close()

    def get_node(self, pos):
        node = {}

        node_id, offset, child_count, child_node_type \
            = struct.unpack('<IHBB', self.data[pos:pos + 0x08])

        if node_id in self.hash_table:
            node_id = self.hash_table[node_id]

        node_id = str(node_id)

        offset = offset * 4 + pos

        # print("Node id: {0}, Offset: {1}, Child Count: {2}, Child Node Type: {3}"
        #       .format(node_id, hex(offset), child_count, hex(child_node_type)))

        if child_node_type == NodeType.Node and child_count > 0:
            children = []
            for index in range(0, child_count):
                child = self.get_node(offset)
                node[child[0]] = child[1]
                offset += 0x08
            return node

        # Node = 0x00
        # Boolean = 0x00
        # Float = 0x01
        # Int = 0x02
        # Vector2 = 0x03
        # Vector3 = 0x04
        # Vector4 = 0x06
        # String = 0x07
        # Actor = 0x08
        # UnknownString = 0x0f
        # UnknownUnsignedInt = 0x11
        # String2 = 0x14

        elif child_node_type == NodeType.Boolean:
            value = struct.unpack('<I', self.data[offset:offset + 0x04])[0]
            value = True if value == 1 else False
            node[node_id] = value

        elif child_node_type == NodeType.Float:
            value = struct.unpack('<f', self.data[offset:offset + 0x04])[0]
            node[node_id] = value

        elif child_node_type == NodeType.Int:
            value = struct.unpack('<I', self.data[offset:offset + 0x04])[0]
            node[node_id] = value

        elif child_node_type == NodeType.String:
            value = self.data[offset:].decode('utf-8')
            value = value.split('\x00')
            value = value[0]
            node[node_id] = value

        elif child_node_type == NodeType.Actor:
            value = self.data[offset:].decode('utf-8')
            value = value.split('\x00')
            value = value[0]
            node[node_id] = value

        elif child_node_type == NodeType.String2:
            value = self.data[offset:].decode('utf-8')
            value = value.split('\x00')
            value = value[0]
            node[node_id] = value

        else:
            value = self.data[offset:offset + 0x04]

        return node_id, value

def main():
    parser = argparse.ArgumentParser(description="Parse the Legend of Zelda: Breath of the Wild aamp files to xml")
    parser.add_argument("filename", type=str, help="File to be parsed.")
    parser.add_argument("-x", "--xml",
                        help="Exports data as a xml file (default)",
                        action="store_true")
    parser.add_argument("-y", "--yaml",
                        help="Exports data as a yaml file",
                        action="store_true")
    parser.add_argument("-j", "--json",
                        help="Exports data as a json file",
                        action="store_true")
    parser.add_argument("-a", "--all",
                        help="Exports data as a xml, yaml and json file",
                        action="store_true")

    args = parser.parse_args()

    aamp = AAMP(args.filename)

    if args.all:
        args.yaml = True
        args.json = True
        args.xml = True

    if args.yaml:
        save_as_yaml(args, aamp)

    if args.json:
        save_as_json(args, aamp)

    if args.xml:
        save_as_xml(args, aamp)

    if not args.yaml and not args.json and not args.xml:
        save_as_xml(args, aamp)

def save_as_yaml(args, byml):
    filename = os.path.basename(args.filename)
    print('Saving {0}.yaml...'.format(filename))
    file = codecs.open(args.filename + '.yaml', 'w', 'utf-8')
    yaml.dump(byml.data_object, file, allow_unicode=True)
    file.close()

def save_as_json(args, byml):
    filename = os.path.basename(args.filename)
    print('Saving {0}.json...'.format(filename))
    file = codecs.open(args.filename + '.json', 'w', 'utf-8')
    json.dump(byml.data_object, file, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))
    file.close()

def save_as_xml(args, byml):
    from xml.dom.minidom import parseString
    filename = os.path.basename(args.filename)
    path = os.path.dirname(os.path.abspath(args.filename))
    base_filename = os.path.splitext(filename)[0]
    print('Saving {0}...'.format(path + '\\' + base_filename + '.xml'))
    file = codecs.open(path + '\\' + base_filename + '.xml', 'w', 'utf-8')
    dom = dicttoxml.dicttoxml(byml.data_object).decode('utf-8')
    file.write(parseString(dom).toprettyxml())
    file.close()

if __name__ == "__main__":
    main()


'''
Problem: Collections.deque().
Description: Perform the given commands
on a deque, e.g. append(), pop(),
popleft(), appendleft().
Points: 20.

'''
from collections import deque

def perform_cmd(d, command):
    command = command.split()
    if command[0] == 'append':
        d.append(int(command[1]))
    elif command[0] == 'pop':
        d.pop()
    elif command[0] == 'popleft':
        d.popleft()
    elif command[0] == 'appendleft':
        d.appendleft(int(command[1]))

if __name__ == '__main__':
    deq = deque()
    num_choices = int(input())

    for i in range(num_choices):
        operation = input()
        perform_cmd(deq, operation)

    print(deq)

#    for i in deq:
#        print(i, end=" ")


class animal():
    def __init__(self,type,nutrition,redroductive):
        print(">> Hayvan sınıfı init fonksiyonu <<")
        self.type = type
        self.nutrition = nutrition
        self.redroductive = redroductive
    def information(self):
        print("*** Hayvan Sınıfı Bilgileri ***")
        print(f"TÜRÜ:{self.type}\nBESLENME:{self.nutrition}\nÜREME:{self.redroductive}\n")

class dog(animal):
    def __init__(self,name,type,nutrition,redroductive,size):
        super(dog, self).__init__(type,nutrition,redroductive)
        print(">> Köpek sınıfı init fonksiyonu <<")
        self.name = name
        self.size = size
    def information(self):
        print("*** Köpek Sınıfı Bilgileri ***")
        print(f"AD:{self.name}\nTÜR:{self.type}\nBESLENME:{self.nutrition}\nBOYUT:{self.size}\nÜREME:{self.redroductive}\n")

class bird(animal):
    def __init__(self,name,type,nutrition,redroductive,size):
        super(bird, self).__init__(type,nutrition,redroductive)
        print(">> Kuş sınıfı init fonksiyonu <<")
        self.name = name
        self.size = size
    def information(self):
        print("*** Kuş Sınıfı Bilgileri ***")
        print(f"AD:{self.name}\nTÜR:{self.type}\nBESLENME:{self.nutrition}\nBOYUT:{self.size}\nÜREME:{self.redroductive}\n")

class horse(animal):
    def __init__(self,name,type,nutrition,redroductive,size):
        super(horse, self).__init__(type,nutrition,redroductive)
        print(">> At sınıfı init fonksiyonu <<")
        self.name = name
        self.size = size
    def information(self):
        print("*** At Sınıfı Bilgileri ***")
        print(f"AD:{self.name}\nTÜR:{self.type}\nBESLENME:{self.nutrition}\nBOYUT:{self.size}\nÜREME:{self.redroductive}")

animal1 = animal("Köpek","Etcil","Eşeyli")
animal2 = animal("Kuş","Etcil&Otcul","Eşeyli")
print(animal1.information(),animal2.information())

dog1 = dog("Dost","Golden","Etcil","Eşeyli","Büyük")
dog2 = dog("Bulut","Kangal","Etcil","Eşeyli","Büyük")
print(dog1.information(),dog2.information())

bird1 = bird("Kartal","Kaya Kartalı","Etcil","Eşeyli","Büyük")
bird2 = bird("Kanarya","Sarı Kanarya","Otcul","Eşeyli","Küçük")
print(bird1.information(),bird2.information())

horse1 = horse("Yağız","Arap Atı","Otcul","Eşeyli","Büyük")
horse2 = horse("Serin","İngiliz Atı","Otcul","Eşeyli","Büyük")
print(horse1.information(),horse2.information())


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findNearestRightNode(self, root: TreeNode, u: TreeNode) -> Optional[TreeNode]:
        r = None
        hei = None
        def res(node = root, h = 0):
            nonlocal hei, r
            if node is not None:
                res(node.left, h + 1)
                if node is u:
                    hei = h
                elif h == hei and r is None:
                    r = node
                res(node.right, h + 1)
        res()
        return r