import unittest
import os
import sys
import pandas
path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(path.split("tests")[0])

import covidPlotter
import webscraper

class covidPlotterTest(unittest.TestCase):
    def setUp(self):
        self.testFrame = pandas.read_csv('datatest.csv')
        self.testPopFrame = webscraper.webscrape_population_2020()

    def test_plot_multi_countries_fake_countries(self):
        '''Test Plotting will return false if no avaliable countries'''
        unique_countries = ["Some","Fake","Country"]
        val = covidPlotter.plot_multi_countries(unique_countries, self.testPopFrame, self.testFrame, yaxis = 'FAKE')
        self.assertFalse(val)

    def test_plot_multi_countries(self):
        '''Test Plotting will return error if incorrect option of yaxis is given'''

        unique_countries = ["US","China","Spain"]

        try:
            covidPlotter.plot_multi_countries(unique_countries, self.testPopFrame, self.testFrame, yaxis = 'FAKE')
        except ValueError as e:
            self.assertEqual(type(e), ValueError)

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


import time

import requests
from tqdm import trange
from loguru import logger

class YaD:
    url = 'https://cloud-api.yandex.net/'
    def __init__(self, token):
        self.token = token

    def get_headers(self):
        return {
            'Content-Type': 'application/json',
            'Authorization': 'OAuth {}'.format(self.token)
        }

    def loader(self, data_list):
        name= input('Название папки: ')
        url_new = self.url + 'v1/disk/resources'
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'OAuth {}'.format(self.token)
        }
        params = {'path': name}
        response = requests.put(url=url_new, headers=headers, params=params)
        if response.status_code == 201:
            logger.info(f'Создана папка {name}')
        else:
            logger.info('Папка с таким названием уже существут')

        upload_url = self.url + 'v1/disk/resources/upload'
        headers = self.get_headers()
        logger.info('Загрузка данных...')
        for i in trange(len(data_list)):
            file_name = data_list[i]['file_name']
            size = data_list[i]['size']
            url = data_list[i]['url']
            params = {'path': f'{name}/{file_name}', 'url': self.url, 'disable_redirects': False}
            response = requests.post(url=upload_url, headers=headers, params=params)
            time.sleep(1)
            response.raise_for_status()
            if response.status_code == 201:
                print('Загруженно')


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

   File Name：     bubble_sort
   Author :       jing
   Date：          2020/3/31

"""

def bubble_sort(nums):
    n = len(nums)
    # 进行多次循环
    for c in range(n):
        for i in range(1, n - c):
            if nums[i - 1] > nums[i]:
                nums[i - 1], nums[i] = nums[i], nums[i - 1]
    return nums

if __name__ == '__main__':
    print(bubble_sort([10, 44, 56, 1, 9, 4, 12, 6, 7]))


import os

folder = r'E:\IMPORTANT _DATA\DATA_2P\M6_MPFC'
pathiter = (os.path.join(root, filename)
            for root, _, filenames in os.walk(folder)
            for filename in filenames
            )

for path in pathiter:
    newname =  path.replace('nanaive', 'naive')
    if newname != path:
        print(path)
        os.rename(path,newname)


import argparse
import glob
from natsort import natsorted
import os
import matplotlib.pyplot as plt
from functions import *

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("--input", required=True, help="Path folder with img and .reg")
    ap.add_argument("--vis", action="store_true", help="Visualize results")
    args = ap.parse_args()

    path_in = args.input
    # Check if it's a image or directory
    if  ".png" in path_in.split('/')[-1]:
        lst_images = [path_in]
    else:
        lst_images = natsorted(glob.glob(args.input+"/*.png"))
    MyfilteGabor = GaborFilter()
    TotalFalsePositive,TotalTruePositive,TotalBBGT,TotalFalseNegative = 0,0,0,0
    print("PATH TO FILE\t\t\t\tTruePositive\tFalsePositive\tFalseNegative")

    for path_image in lst_images:

        # Load image and convert to gray
        img = cv2.imread(path_image)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # Apply Filter Gabors
        proccess_image = process(gray, MyfilteGabor)
        (thresh, im_bw) = cv2.threshold(proccess_image, 127, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

        # Apply Morphology functions
        kernel = np.ones((5, 5), np.uint8)
        erosion = cv2.erode(im_bw, kernel, iterations=2)
        dilation = cv2.dilate(erosion, kernel, iterations=2)
        kernel = np.ones((3, 3), np.uint8)
        ero2 = cv2.erode(dilation, kernel, iterations=1)

        # Get the bounding boxes
        Detected_BB = getBoxes(mask=ero2)

        # Load the ground truth
        reg_path = path_image.split('.')[0] + ".reg"
        if os.path.isfile(reg_path):
            GroundTruth_BB = load_reg(reg_path)
            TotalBBGT+=GroundTruth_BB.shape[0]
        else:
            GroundTruth_BB = np.array([])

        # Calculate the iou
        TruePositive, FalsePositive, FalseNegative = verifDetection(GT_BBs=GroundTruth_BB,DT_BBs=Detected_BB,threshold=0.6)
        TotalFalsePositive+=FalsePositive
        TotalTruePositive+=TruePositive
        TotalFalseNegative+=FalseNegative
        print(path_image,TruePositive,FalsePositive,FalseNegative)

        # If you type "--vis"
        if args.vis:
            # Drawing the bounding boxes
            out = drawBoundingBoxes(img,GroundTruth_BB,(0,0,255))
            out = drawBoundingBoxes(out,Detected_BB,(0,255,0),thickness=1)
            cv2.imshow("result",out)
            cv2.waitKey()

    print("\n\n-----------------TOTAL--------------------")
    print("Total of BB GT: ",TotalBBGT)
    print("Falses positives: ",TotalFalsePositive)
    print("Trues positives: ",TotalTruePositive)
    print("False negative: ", TotalFalseNegative)
    print("Recall:",TotalTruePositive/(TotalTruePositive+TotalFalseNegative))
    print("Accuracy: ",TotalTruePositive/TotalBBGT)


"""This  script (executed in vagrant) extracts the specified files from the GPCRmd database into a pickle file in the folder MEDIA_ROOT /Precomputed used to create the volmaps.
The pickle file contains the list of pdb and trajectory filepaths (vagrant pahts) of which the volmaps will later be created.
To create the volmaps, one should execute the script: 'create_volmaps.py' on their LOCAL computer (not in vagrant, VMD can not be run there).
The argument --filename is necessary to fill in. So to use the script one would do: python manage.py volmap_files --filename [name of picklefile]
Volmaps.py uses the just created pickle file to create the actual occupancy or density maps. """

import re
import os
from os import path
import gc
import pickle
from django.core.management.base import BaseCommand, CommandError
from django.db.models import F
import numpy as np

from modules.dynadb.models import *
from django.conf import settings

class Command(BaseCommand):
    help = "Creates the list of files used to create the water density maps and occupancy maps. By default, al ready for publication dynamics are considered"
    def add_arguments(self, parser):
        parser.add_argument(
           '--sub',
            dest='submission_id',
            nargs='*',
            action='store',
            default=False,
            help='Specify submission id(s) for which the density and occupancy map will be precomputed.'
        )
        parser.add_argument(
           '--dyn',
            dest='dynamics_id',
            nargs='*',
            action='store',
            default=False,
            help='Specify dynamics id(s) for which the density and/or occupancy map will be precomputed. '
        )
        parser.add_argument(
           '--traj',
            dest='traj_id',
            nargs='*',
            action='store',
            default=False,
            help='Specify file id(s) of trajectories for which an occupancy file will be precomputed. '
        )
        parser.add_argument(
            '--ignore_publication',
            action='store_true',
            dest='ignore_publication',
            default=False,
            help='Consider both published and unpublished dynamics.'
        )

        parser.add_argument(
           '--filename',
            dest='filename',
            nargs='*',
            type=str,
            action='store',
            #default=False,
            required = True,
            help='Specify the filename in which the list with dynamics will be saved. '
        )

        parser.add_argument(
            '--overwrite',
            action='store_true',
            dest='overwrite',
            default=False,
            help='Overwrites already generated pickle files.'
        )

    def handle(self, *args, **options):

        #create folder where files should be accessed from and where information will be stored
        dynamics_path = settings.MEDIA_ROOT + "Dynamics"
        pickle_path = settings.MEDIA_ROOT + "Precomputed/WaterMaps"

        #give the options of the different parserarguments.

        if not os.path.isdir(pickle_path):
            os.makedirs(pickle_path)

        if options['ignore_publication']:
            dynobj=DyndbDynamics.objects.all()
        else:
            dynobj=DyndbDynamics.objects.filter(is_published=True)
        if options['submission_id']:
            dynobj=dynobj.filter(submission_id__in=options['submission_id'])
        if options['dynamics_id']:
            dynobj=dynobj.filter(id__in=options['dynamics_id'])

        #dynobj=dynobj.annotate(traj_id=F('dyndbfilesdynamics__id_files__id'))
        if options['traj_id']:
            dynobj=dynobj.annotate(traj_id=F('dyndbfilesdynamics__id_files__id'))
            dynobj=dynobj.filter(traj_id__in=options['traj_id'])

        pdbfiles = dynobj.annotate(dyn_id=F('id'))                                      #file id from DyndbFilesDynamics table and on website id/12/
        pdbfiles = pdbfiles.filter(dyndbfilesdynamics__id_files__id_file_types_id=2)
        pdbfiles = pdbfiles.annotate(file_id=F('dyndbfilesdynamics__id_files_id'))      #last id not neccesary, gives same result without it.
        pdbfiles = pdbfiles.annotate(pdb_path=F('dyndbfilesdynamics__id_files__filepath'))  #the order matters when to annotate
        pdbfiles = pdbfiles.values('dyn_id', 'file_id', 'pdb_path')
        #print(pdbfiles)

        #{'pdb_path': settings.MEDIA_ROOT + 'Dynamics/10177_dyn_9.pdb', 'dyn_id': 9, 'file_id': 10177} which is a dictionary

        #create the actual dictionary
        dyn_dict = {}
        for i in pdbfiles:
            if i['dyn_id'] not in dyn_dict:
                dyn_dict[i['dyn_id']] = dict()                    #4: {}
                dyn_dict[i['dyn_id']]['dyn_id'] = i['dyn_id']
                dyn_dict[i['dyn_id']]['file_id'] = i['file_id']   #4: {'file_id': 10142}
                dyn_dict[i['dyn_id']]['pdb_path'] = i['pdb_path'] #4: {'file_id: 10142', 'pdb_path:/path/file.pdb'}
                dyn_dict[i['dyn_id']]['traj_files'] = dict()      #4: {'file_id: 10142', 'pdb_path:/path/file.pdb', 'traj_path': {}}
            if i['dyn_id'] is None:
                continue

        del pdbfiles
        del dynobj

        gc.collect()

        #get trajectory information and filepaths for dyn_ids present in dictionary
        trajfiles = DyndbFiles.objects.annotate(dyn_id=F('dyndbfilesdynamics__id_dynamics'))
        trajfiles = trajfiles.filter(dyn_id__in=list(dyn_dict), id_file_types__is_trajectory=True)
        trajfiles = trajfiles.annotate(file_id=F('dyndbfilesdynamics__id_files_id'))
        trajfiles = trajfiles.values('dyn_id', 'file_id', 'filepath')

        #add trajfiles to dictionary dyn_dict.
        for traj in trajfiles:
            dyn_dict[traj['dyn_id']]['traj_files'][traj['file_id']] = dict()
            if traj['filepath'] not in dyn_dict[traj['dyn_id']]['traj_files'][traj['file_id']]:
                dyn_dict[traj['dyn_id']]['traj_files'][traj['file_id']]['file_id'] = traj['file_id']
                dyn_dict[traj['dyn_id']]['traj_files'][traj['file_id']]['traj_path'] = traj['filepath']

        del trajfiles
        gc.collect()

        if len(dyn_dict.keys()) == 0:
            self.stdout.write(self.style.NOTICE("No dynamics found with specified conditions."))

        #transform dyn_dict into sorted list
        dyn_list = [dyn_dict[dyn_id] for dyn_id in sorted(list(dyn_dict))]
        del dyn_dict
        gc.collect()

        for dyn in dyn_list:
            dyn['traj_files'] = [dyn['traj_files'][traj_id] for traj_id in sorted(list(dyn['traj_files']))]
        gc.collect()

        for i in dyn_list:
            print(i)
        print(len(dyn_list))

        #create pickle file
        file_name = options['filename'][0]  #options gives a list with the saved input argument. To get the content of this list, specify [0]
        full_filename = os.path.join(pickle_path, file_name)
        exists = os.path.isfile(full_filename)
        create_file = False
        if exists:
            if options['overwrite']:
                create_file = True
                self.stdout.write(self.style.NOTICE("Picklefile "+file_name+" already exists, but will be overwritten."))
            else:
                self.stdout.write(self.style.NOTICE("Pickle file %s already exists and will not be overwritten."%file_name))
        else:
            create_file = True

        if create_file == True:
            file_Object = open(full_filename, 'wb')
            pickle.dump(dyn_list, file_Object)
            file_Object.close()


from collections import deque
from math import ceil, floor, log, sqrt
import numpy as np
import bisect

class ConfigurationTester():
    """

    """

    def __init__(self, cid, k0, theta_multiplier, update_lcb_every=1000):
        """
        Parameters:
            cid: config id
            k0 : kappa 0
            theta_multiplier : how much to increase theta by after timing out
            update_lcb_every : only re-compute the lcb for this config if at least this many iterations have elapsed (for efficiency)
        """

        self.cid = cid
        self.theta = k0  # current runtime cap, initially the initial value
        self.r = 0  # number of active instances
        self.q = 1.
        self.Q = deque()  # double ended queue
        self.theta_multiplier = theta_multiplier

        self.instance_runtimes_capped = {}  # mapping from instances to most recent capped runtime
        self.unique_values = []  # sorted list of unique runtime values seen
        self.unique_value_counts = {}

        self.total_time = 0  # time spent runing this configuration

        self.lcb = 0
        self.update_lcb_every = update_lcb_every
        self.t_last_update_lcb = -1

    def execute_step(self, env, t):
        """
        Execute one step of the algorithm for this configuration.
        """

        if len(self.Q) < self.q:
            self.r += 1
            l = self.r
        else:
            l, theta = self.Q.pop()
            self.theta = theta

        did_timeout, elapsed, resumed_elapsed = env.run(config_id=self.cid, timeout=self.theta, instance_id=l)  # get the runtime of config <cid> in instance <l>

        if did_timeout:
            rt = self.theta
            self.Q.appendleft((l, self.theta_multiplier * self.theta))
        else:
            rt = elapsed

        rt = np.round(rt, decimals=3)  # round to milliseconds

        self.total_time += rt

        self._update_runtime_values(l, rt)

        self.lcb = self._compute_confidence_bound(t)
        self.t_last_update_lcb = t

        if self.r <= 1 or t <= 1:
            self.q = 25.
        else:
            self.q = ceil(25. * log(t * log(self.r, 2), 2))

        return did_timeout, rt, self.lcb, len(self.instance_runtimes_capped)

    def _compute_confidence_bound(self, t):
        """
        Compute the lcb from the paper.
        """
        n = len(self.instance_runtimes_capped)
        if n == 0:  # if no runtime values, prioritize this config
            return -1e-6
        ecdf = []  # empirical cumulative distribution of runtime values
        lcb = 0.
        for i in range(len(self.unique_values)):
            rt = self.unique_values[i]
            if i == 0:
                rt_low = 0.
                ecdf.append(self.unique_value_counts[rt] / n)
                g = 0.
            else:
                rt_low = self.unique_values[i - 1]
                ecdf.append(ecdf[i - 1] + self.unique_value_counts[rt] / n)
                g = ecdf[i - 1]

            lcb += (rt - rt_low) * self.beta(1. - g, t)

        return lcb

    def get_confidence_bound(self, t):
        """
        Returns the lcb for this config, re-computing it as necessary.
        """
        if t - self.t_last_update_lcb > self.update_lcb_every:  # re-compute lcb if enough iterations have elapsed
            self.lcb = self._compute_confidence_bound(t)
            self.t_last_update_lcb = t
        return self.lcb

    def beta(self, p, t):
        """
        Implementation of Beta function from paper.
        """
        _t = max(t, 1)  # setting initial values so that we don't take log of 0
        _r = max(self.r, 1)
        k = floor(log(1 / p, 2))
        if k == 0:
            eps = sqrt(9. * log(_t) / _r)
        else:
            eps = sqrt(9 * 2 ** k * log(k * _t) / _r)
        if eps <= 0.5:
            return p / (1 + eps)
        else:
            return 0.

    def get_num_active(self):
        """
        Returns the number of active instances for this config.
        """
        return self.r

    def _update_runtime_values(self, instance_id, new_rt, eps=1e-6):
        """
        Maintains the sorted list of unique runtime values and their counts.
        """

        if instance_id in self.instance_runtimes_capped:
            old_rt = self.instance_runtimes_capped[instance_id]
            rt_count = self.unique_value_counts[old_rt]
            if rt_count > 1:
                self.unique_value_counts[old_rt] -= 1
            else:  # remove rt from unique values
                delete_ind = bisect.bisect_left(self.unique_values, old_rt)
                del self.unique_values[delete_ind]
                del self.unique_value_counts[old_rt]

        insert_ind = bisect.bisect_left(self.unique_values, new_rt)  # get new insert index
        n = len(self.unique_values)

        if insert_ind < n:  # insert a new value
            if abs(self.unique_values[insert_ind] - new_rt) > eps:  # sufficiently different from other unique values
                self.instance_runtimes_capped[instance_id] = new_rt  # save/update runtime value for this instance with new theta
                bisect.insort(self.unique_values, new_rt)
                self.unique_value_counts[new_rt] = 1
            else:
                self.instance_runtimes_capped[instance_id] = self.unique_values[insert_ind]
                self.unique_value_counts[self.unique_values[insert_ind]] += 1
        else:  # new_value is greater than all existing unique_values
            if n == 0 or abs(self.unique_values[n - 1] - new_rt) > eps:  # first value or sufficiently different from largest unique value
                self.instance_runtimes_capped[instance_id] = new_rt
                self.unique_values.append(new_rt)
                self.unique_value_counts[new_rt] = 1
            else:
                self.instance_runtimes_capped[instance_id] = self.unique_values[n - 1]
                self.unique_value_counts[self.unique_values[n - 1]] += 1


# 594. strStr II
# 中文English
# Implement strStr function in O(n + m) time.
#
# strStr return the first index of the target string in a source string. The length of the target string
# is m and the length of the source string is n.
# If target does not exist in source, just return -1.
#
# Example
# Example 1:
#
# Input：source = "abcdef"， target = "bcd"
# Output：1
# Explanation：
# The position of the first occurrence of a string is 1.
# Example 2:
#
# Input：source = "abcde"， target = "e"
# Output：4
# Explanation：
# The position of the first occurrence of a string is 4.

#
# 算法：HASH
# 字符串Hash可以通俗的理解为，把一个字符串转换为一个整数。
# 如果我们通过某种方法，将字符串转换为一个整数，就可以快速的判断两个字符串是否相同。
# 当然如果有不同的两个字符串同时Hash到一个整数，这样就比较麻烦了，所以我们希望构造这个Hash函数使得他们成为一个单射。
#
# 算法思路
# 给定一个字符串S,对于一个字符c我们规定id(c)=c-'a'+1
# hash[i]=(hash[i-1]*p+id(s[i]))%MOD
# p和MOD均为质数，并且要尽量大
#
# 代码思路
# 计算target的hash值
# 计算source的hash值的过程中，依次计算每targetLen位的hash值。
#     -假设target长度为2，source为“abcd”
#     -hash("cd") = (hash("bc + d") - hash("b")*2 ) % BASE
#
# 复杂度分析
# N表示字符串source长度，M表示字符串target长度
# 空间复杂度：O(1)
# 时间复杂度：O(N+M)

class Solution:
    # @param {string} source a source string
    # @param {string} target a target string
    # @return {int} an integer as index
    def strStr2(self, source, target):
        # Write your code here
        if source is None or target is None:
            return -1
        m = len(target)
        n = len(source)
        if m == 0:
            return 0
        BASE = 100007
        targetCode = 0
        power = 1
        # 先计算一下target的hash值
        for i in range(m):
            targetCode = (targetCode * 31 + ord(target[i]) - ord('a')) % BASE
            if targetCode < 0:
                targetCode += BASE

        for i in range(m - 1):
            power = power * 31 % BASE

        sourceCode = 0

        # 当source code 加上右边一个character，就要减掉左边的一个character
        for i in range(n):
            if i >= m:
                sourceCode = (sourceCode - power * (ord(source[i - m]) - ord('a'))) % BASE

            sourceCode = (sourceCode * 31 + ord(source[i]) - ord('a')) % BASE

            if sourceCode < 0:
                sourceCode += BASE

            # 若hash值相同，返回答案
            if i >= m - 1 and sourceCode == targetCode:
                return i - m + 1

        return -1


from argparse import ArgumentParser

import eventlet
from socketio import Server, WSGIApp

from indoman.utils import docker, logging, populate_namespace

logging.init()
docker.init()

static_files = {
    "/": "./frontend/index.html",
    "": "./frontend"
}
if __name__ == "__main__":
    parser = ArgumentParser(
        "indoman",
        description="Backend of Interactive Docker Manager"
        )
    parser.add_argument("--docker-url",
                        help="URL to the Docker server",
                        type=str)
    parser.add_argument("--docker-tls",
                        help="Use TLS to connect to Docker server",
                        action="store_true")
    parser.add_argument("--disable-cors",
                        help="Disable CORS check (ONLY FOR DEBUGGING!)",
                        action="store_true")
    parser.add_argument("--debug",
                        help="Show debug log",
                        action="store_true")
    parser.add_argument("--socketio-debug",
                        help="Show socket.io debug log",
                        action="store_true")
    parser.add_argument("--host",
                        help="Host to listen on",
                        type=str,
                        default="0.0.0.0")
    parser.add_argument("--port",
                        help="Port to listen on",
                        type=int,
                        default="4636")
    args = parser.parse_args()
    server_init_args = {}
    if args.disable_cors:
        server_init_args.update({"cors_allowed_origins": "*"})
    if args.debug:
        logging.logger.setLevel("DEBUG")
    if args.socketio_debug:
        server_init_args.update({"logger": True})
        server_init_args.update({"engineio_logger": True})
    sio = Server(**server_init_args)
    logging.logger.debug("Populating namespaces...")
    populate_namespace(sio)
    app = WSGIApp(sio, static_files=static_files)
    logging.logger.info("Starting up indoman...")
    eventlet.wsgi.server(eventlet.listen((args.host, args.port)), app)
else:
    sio = Server()
    populate_namespace(sio)
    app = WSGIApp(sio, static_files=static_files)


#!/usr/bin/env python3
import os, sys

sys.path.append(os.path.abspath("../../../share/trick/trickops/"))

from TrickWorkflow import *
class ExampleWorkflow(TrickWorkflow):
    def __init__( self, quiet, trick_top_level=os.path.abspath("../../../")):
      # Base Class initialize, this creates internal management structures
      if not(os.path.isdir(os.path.abspath("./MCGTrickOpsLog"))):
        logDirectory = "MCGTrickOpsLog"
        logDirectoryParent = os.path.abspath(".")
        logPath = os.path.join(logDirectoryParent, logDirectory)
        os.mkdir(logPath)
      TrickWorkflow.__init__(self, project_top_level=trick_top_level,
        log_dir=os.path.join(trick_top_level,'test/SIM_mc_generation/MCGTrickOps/MCGTrickOpsLog'),
        trick_dir=trick_top_level,
        config_file="test/SIM_mc_generation/MCGTrickOps/MCGenerationTest.yml",
        cpus=3, quiet=quiet)
    def run( self):
      build_jobs      = self.get_jobs(kind='build')
      run_jobs        = self.get_jobs(kind='run')
      builds_status = self.execute_jobs(build_jobs, max_concurrent=3, header='Executing all sim builds.')
      runs_status   = self.execute_jobs(run_jobs,   max_concurrent=1, header='Executing all sim runs.')
      c = self.compare()
      self.report()           # Print Verbose report
      self.status_summary()   # Print a Succinct summary
      return (builds_status or runs_status or self.config_errors or c)
if __name__ == "__main__":
      ExampleWorkflow(quiet=True).run()


import csv
import requests
from bs4 import BeautifulSoup

#1 page
page=requests.get("https://www.amazon.com/best-sellers-books-Amazon/zgbs/books/ref=zg_bs_pg_1?_encoding=UTF8&pg=1&ajax=1")
soup=BeautifulSoup(page.content,"html.parser")
allinfo=soup.find_all("div",{"class":"zg_itemWrapper"})

#2 page
page=requests.get("https://www.amazon.com/best-sellers-books-Amazon/zgbs/books/ref=zg_bs_pg_2?_encoding=UTF8&pg=2&ajax=1")
soup=BeautifulSoup(page.content,"html.parser")
extra=soup.find_all("div",{"class":"zg_itemWrapper"})
for i in extra:
	allinfo.append(i)

#3 page
page=requests.get("https://www.amazon.com/best-sellers-books-Amazon/zgbs/books/ref=zg_bs_pg_3?_encoding=UTF8&pg=3&ajax=1")
soup=BeautifulSoup(page.content,"html.parser")
extra=soup.find_all("div",{"class":"zg_itemWrapper"})
for i in extra:
	allinfo.append(i)

#4 page
page=requests.get("https://www.amazon.com/best-sellers-books-Amazon/zgbs/books/ref=zg_bs_pg_4?_encoding=UTF8&pg=4&ajax=1")
soup=BeautifulSoup(page.content,"html.parser")
extra=soup.find_all("div",{"class":"zg_itemWrapper"})
for i in extra:
	allinfo.append(i)

#5 page
page=requests.get("https://www.amazon.com/best-sellers-books-Amazon/zgbs/books/ref=zg_bs_pg_5?_encoding=UTF8&pg=5&ajax=1")
soup=BeautifulSoup(page.content,"html.parser")
extra=soup.find_all("div",{"class":"zg_itemWrapper"})
for i in extra:
	allinfo.append(i)

#a=1
#for i in range(100):
#	print("")
#	print("")
#	print("")
#	print(a)
#	print(allinfo[i])
#	a=a+1

#contains <a> tag
temp1=[]
for i in allinfo:
	temp1.append(i.find("a"))

#store url
url=[]
for i in temp1:
	url.append(i["href"])

#for i in url:
#	print (i)

#contains <div> inside <a> tag
temp2=[]
for i in temp1:
	temp2.append(i.find_all("div"))

#book contains book
book=[]
for i in temp2:
	book.append(i[1].get_text())

#for i in book:
#	print(i)

#temp3 contains <div> tags
temp3=[]
for i in allinfo:
	temp3.append(i.find_all("div"))

#author
author=[]
for i in temp3:
	author.append(i[3].get_text())

#for i in author:
#	print(i)

#temp4 contains 7th div in allinfo
temp4=[]
for i in temp3:
	try:
		x=(len(i[5]))
		if x==1:
			temp4.append(i[6])
		else:
			temp4.append(i[5])
	except IndexError:
		x=i[0].find_all("a")[4]
		y=x.find_all("span")[2]
		temp4.append(y.get_text())

#price
price=[]
for i in temp4:
	try:
		price.append(i.find_all("span")[1].get_text())
	except:
		price.append(i)
#a=1
#for i in price:
#	print(a,i)
#	a=a+1

#temp5 contains 6th div
temp5=[]
for i in temp3:
	temp5.append(i[4])

#avg rating
avgrat=[]
for i in temp5:
	try:
		avgrat.append(i.find_all("a")[0].get_text())
	except:
		avgrat.append("Not available")

#for i in avgrat:
#	print(i)

#rating
rat=[]
for i in temp5:
	try:
		rat.append(i.find_all("a")[1].get_text())
	except:
		rat.append("Not available")

#for i in rat:
#	print(i)

finallist=[]
finallist.append(["Name","URL","Author","Price","Number of Ratings","Average Rating"])
for i in range(100):
	finallist.append([book[i].strip(),"https://www.amazon.com"+url[i].strip(),author[i].strip(),price[i].strip(),rat[i].strip(),avgrat[i].strip()])

#for i in finallist:
#	print(i[0])
#	print(i[1])
#	print(i[2])
#	print(i[3])
#	print(i[4])
#	print(i[5])

myfile=open("com_book.csv","w")
with myfile:
	writer=csv.writer(myfile,delimiter=";")
	writer.writerows(finallist)


# WSGI Handler sample configuration file.
#
# Change the appropriate settings below, in order to provide the parameters
# that would normally be passed in the command-line.
# (at least conf['addons_path'])
#
# For generic wsgi handlers a global application is defined.
# For uwsgi this should work:
#   $ uwsgi_python --http :9090 --pythonpath . --wsgi-file openerp-wsgi.py
#
# For gunicorn additional globals need to be defined in the Gunicorn section.
# Then the following command should run:
#   $ gunicorn odoo:service.wsgi_server.application -c openerp-wsgi.py

import odoo

#----------------------------------------------------------
# Common
#----------------------------------------------------------
odoo.multi_process = True # Nah!

# Equivalent of --load command-line option
odoo.conf.server_wide_modules = ['base', 'web']
conf = odoo.tools.config

# Path to the OpenERP Addons repository (comma-separated for
# multiple locations)
#conf['addons_path'] = './odoo/addons,./addons'

# Optional database config if not using local socket
#conf['db_name'] = 'mycompany'
#conf['db_host'] = 'localhost'
#conf['db_user'] = 'foo'
#conf['db_port'] = 5432
#conf['db_password'] = 'secret'

#----------------------------------------------------------
# Generic WSGI handlers application
#----------------------------------------------------------
application = odoo.http.root

odoo.service.server.load_server_wide_modules()

#----------------------------------------------------------
# Gunicorn
#----------------------------------------------------------
# Standard OpenERP XML-RPC port is 8069
bind = '127.0.0.1:8069'
pidfile = '.gunicorn.pid'
workers = 4
timeout = 240
max_requests = 2000


import os
import sys
import torch
from context import needlemaster as nm
from pdb import set_trace as woah

def playback(env_path, demo_path, im_path):
    """
            Molly 11/30/2018

            read in an environment and demonstration and "hallucinate" the
            screen images

            Args:
                env_path: path/to/corresponding/environment/file
                demo_path: path/to/demo/file
    """
    environment = nm.Environment(env_path, device=torch.device('cuda'))
    demo        = nm.Demo(environment.width, environment.height, filename=demo_path)
    actions     = demo.u;
    state       = demo.s;

    """ ..................................... """
    done = False
    demo_name = demo_path.split('/')[-1].split('.')[0]
    out_path = im_path + demo_name + '/'
    if(not os.path.exists(out_path)):
       os.mkdir(out_path)

    environment.render(save_image=True)

    if(len(actions) > 0):
        while(not done and environment.t < len(actions)):
            _, _, done = environment.step(actions[environment.t,0:2], save_image=True, save_path=out_path)

        #print("________________________")
        #print(" Level " + str(demo.env))
        #environment.score(True)
        #print("________________________")
        """ ..................................... """

    else:
        print("Warning: empty demonstration")

#-------------------------------------------------------
# main()
args = sys.argv
print(len(args))
if len(args) == 4:
   playback(args[1], args[2], args[3])
else:
   print("ERROR: 3 command line arguments required")
   print("[Usage] python play.py <path to environment file> <path to demonstration> <image output path>")


#
# @lc app=leetcode.cn id=480 lang=python3
#
# [480] 滑动窗口中位数
#
# https://leetcode.cn/problems/sliding-window-median/description/
#
# algorithms
# Hard (43.23%)
# Likes:    432
# Dislikes: 0
# Total Accepted:    40.7K
# Total Submissions: 94.3K
# Testcase Example:  '[1,3,-1,-3,5,3,6,7]\n3'
#
# 中位数是有序序列最中间的那个数。如果序列的长度是偶数，则没有最中间的数；此时中位数是最中间的两个数的平均数。
#
# 例如：
#
#
# [2,3,4]，中位数是 3
# [2,3]，中位数是 (2 + 3) / 2 = 2.5
#
#
# 给你一个数组 nums，有一个长度为 k 的窗口从最左端滑动到最右端。窗口中有 k 个数，每次窗口向右移动 1
# 位。你的任务是找出每次窗口移动后得到的新窗口中元素的中位数，并输出由它们组成的数组。
#
#
#
# 示例：
#
# 给出 nums = [1,3,-1,-3,5,3,6,7]，以及 k = 3。
#
#
# 窗口位置                      中位数
# ---------------               -----
# [1  3  -1] -3  5  3  6  7       1
# 1 [3  -1  -3] 5  3  6  7      -1
# 1  3 [-1  -3  5] 3  6  7      -1
# 1  3  -1 [-3  5  3] 6  7       3
# 1  3  -1  -3 [5  3  6] 7       5
# 1  3  -1  -3  5 [3  6  7]      6
#
#
# 因此，返回该滑动窗口的中位数数组 [1,-1,-1,3,5,6]。
#
#
#
# 提示：
#
#
# 你可以假设 k 始终有效，即：k 始终小于等于输入的非空数组的元素个数。
# 与真实值误差在 10 ^ -5 以内的答案将被视作正确答案。
#
#
#

# @lc code=start

'''
关键字
有序数组 滑动窗口
'''

from sortedcontainers import SortedList

class Solution:
    def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:
        sl = SortedList(key=lambda m: nums[m])
        for i in range(k):
            sl.add(i)

        def get_mid():
            if k % 2 == 0:
                return (nums[sl[k // 2]] + nums[sl[k//2 - 1]]) / 2
            else:
                return nums[sl[k//2]]
        res = [get_mid()]

        for i in range(len(nums) - k):
            sl.remove(i)
            sl.add(i+k)
            res.append(get_mid())

        return res
# @lc code=end

480.滑动窗口中位数.py


import os, wandb, json, torch
from cached_dataset import *
import numpy as np
from torch.utils.data import DataLoader, RandomSampler, SubsetRandomSampler
from base_model import *
from loss import *
from core import *
from communication_func import *
from copy import deepcopy
from SAnD.model import SAnD

def nan_checker( args, patients ):

    new_patients = []

    for patient in patients :
        temp = CachedDataset("total", [patient], args.data_path)

        if args.task == "mort_24h" or args.task == "mort_48h" or args.task == "LOS" :
            index = ["mort_24h", "mort_48h", "LOS"].index(args.task)
            label = temp[0]['tasks_binary_multilabel'][index]
        else :
            label = temp[0][args.task]

        check = torch.isnan(label).item()

        if check == False :
            new_patients.append(patient)

    return new_patients

def get_dataset(args):
    new_dir = f"{args.data_path}/eicu-2.0/federated_preprocessed_data/data_split_fixed"
    client_id = args.hospital_id

    train_dataset, valid_dataset, test_dataset = [], [], []

    for c_id in client_id :
        each_client = str(c_id)

        ##########################################################
        if args.task in ['mort_24h', 'mort_48h', 'LOS' ] :
            with open( os.path.join(new_dir, f"{c_id}.json"), "r") as json_file:
                json_data = json.load(json_file)
            train_patients = json_data['train']
            valid_patients = json_data['valid']
            test_patients = json_data['test']
        else :
            with open( os.path.join(new_dir, f"{c_id}_ver2.json"), "r") as json_file:
                json_data = json.load(json_file)
            train_patients = json_data['train']
            valid_patients = json_data['valid']
            test_patients = json_data['test']

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

        print(f"{c_id} client's patient number : ", len(train_patients + valid_patients + test_patients))

        train_patients = nan_checker( args, train_patients )
        valid_patients = nan_checker( args, valid_patients )
        test_patients = nan_checker( args, test_patients )

        train_dataset.append( CachedDataset( each_client, train_patients, args.data_path) )
        valid_dataset.append( CachedDataset( each_client, valid_patients, args.data_path) )
        test_dataset.append( CachedDataset( each_client, test_patients, args.data_path) )

    return client_id, train_dataset, valid_dataset, test_dataset

def get_dataloader(args, train_dataset, valid_dataset, test_dataset):

    train_loaders, valid_loaders, test_loaders = [], [], []
    client_weights = []

    for i in range(len(args.hospital_id)) :
        client_weights.append( len(train_dataset[i]) )

        train_sampler = RandomSampler(train_dataset[i])
        if len(train_dataset[i]) % args.batch_size == 1:
            train_loaders.append( DataLoader(train_dataset[i], sampler=train_sampler, batch_size=args.batch_size, num_workers=8, drop_last=True) )
        else :
            train_loaders.append( DataLoader(train_dataset[i], sampler=train_sampler, batch_size=args.batch_size, num_workers=8, drop_last=False) )

        valid_sampler = RandomSampler(valid_dataset[i])
        if len(valid_dataset[i]) % args.batch_size == 1 :
            valid_loaders.append( DataLoader(valid_dataset[i], sampler=valid_sampler, batch_size=args.batch_size, num_workers=8, drop_last=True) )
        else :
            valid_loaders.append( DataLoader(valid_dataset[i], sampler=valid_sampler, batch_size=args.batch_size, num_workers=8, drop_last=False) )

        if len(test_dataset[i]) % args.batch_size == 1 :
            test_loaders.append( DataLoader(test_dataset[i], batch_size=args.batch_size, num_workers=8, drop_last=True) )
        else :
            test_loaders.append( DataLoader(test_dataset[i], batch_size=args.batch_size, num_workers=8, drop_last=False) )

    total = sum(client_weights)
    client_weights = [ weight / total for weight in client_weights ]

    return client_weights, train_loaders, valid_loaders, test_loaders

def update_best_models(args, best_models, models) :
    for idx in range(len(args.hospital_id)) :
        best_models[idx] = deepcopy(models[idx])
    return best_models

def save_models(args, models, task, save_path, best=False):
    model_list = {}

    if args.task == 'Final Acuity Outcome' :
        task_name = 'Final_Acuity_Outcome'
    else :
        task_name = args.task

    if best == True :
        for idx in range(len(args.hospital_id)) :
            model_name = 'model_' + str(idx)
            model_list[model_name] = models[idx].state_dict()

        if args.algorithm == "fedadam" or args.algorithm == "fedadagrad" or args.algorithm == "fedyogi" :
            torch.save( model_list , os.path.join( save_path, f"checkpoint_best_{task_name}_{args.model_type}_{args.learning_rate}_{args.server_learning_rate}_{args.tau}_{args.seed}.pt" ) )
        else :
            torch.save( model_list , os.path.join( save_path, f"checkpoint_best_{task_name}_{args.model_type}_{args.seed}.pt" ) )

    else :
        for idx in range(len(args.hospital_id)) :
            model_name = 'model_' + str(idx)
            model_list[model_name] = models[idx].state_dict()

        if args.algorithm == "fedadam" or args.algorithm == "fedadagrad" or args.algorithm == "fedyogi" :
            torch.save( model_list , os.path.join( save_path, f"checkpoint_{task_name}_{args.model_type}_{args.learning_rate}_{args.server_learning_rate}_{args.tau}_{args.seed}.pt" ) )
        else :
            torch.save( model_list , os.path.join( save_path, f"checkpoint_{task_name}_{args.model_type}_{args.seed}.pt" ) )

def initialization(args, model):
    factor = deepcopy(model)

    for key in factor.state_dict().keys():
        if 'num_batches_tracked' in key:
            pass;
        else:
            temp = torch.zeros_like(factor.state_dict()[key])
            # temp.new_full(temp.shape, args.tau ** 2)
            factor.state_dict()[key].data.copy_(temp)

    return factor

def initialization_prev_grad(args, model):

    factor = deepcopy(model)

    for key in factor.state_dict().keys():
        if 'num_batches_tracked' in key:
            pass;
        else:
            temp = torch.zeros_like(factor.state_dict()[key])
            factor.state_dict()[key].data.copy_(temp)

    prev_grads = [deepcopy(factor) for idx in range(len(args.hospital_id))]

    return prev_grads

def get_param_tensor(target):
    temp_tensor = None
    for name, param in target.named_parameters():
        if not isinstance(temp_tensor, torch.Tensor):
            temp_tensor = param.view(-1).clone()
        else:
            temp_tensor = torch.cat((temp_tensor, param.view(-1).clone()), dim=0)
    return temp_tensor

def get_grad_diff( prev_grads, current_model, cld_model):
    with torch.no_grad():
        # aggregate params
        for key in cld_model.state_dict().keys():
            if 'num_batches_tracked' in key:
                pass
            else:
                temp = current_model.state_dict()[key] - cld_model.state_dict()[key]
                temp += prev_grads.state_dict()[key]
                prev_grads.state_dict()[key].data.copy_(temp)

    return prev_grads

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

def ehr_federated_main(args, wandb_run=None) :

    naming = "max_" + str(len(args.hospital_id))

    SAVE_PATH = f'{args.save_path}/federated/{args.algorithm}_{args.communications}_{args.local_epochs}/{naming}'

    if not os.path.exists( SAVE_PATH ):
        os.makedirs( SAVE_PATH )

    patience = 0
    best_communication_rounds = 0

    client_id, train_dataset, valid_dataset, test_dataset = get_dataset(args)

    client_weights, train_loaders, valid_loaders, test_loaders = get_dataloader(args, train_dataset, valid_dataset, test_dataset)

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    if args.model_type == "transformer_gn" :
        server_model = TransformerModel(task=args.task, norm_layer='gn').to(device)
    elif args.model_type == "transformer_ln" :
        server_model = TransformerModel(task=args.task, norm_layer='ln').to(device)
    else :
        server_model = TransformerModel(task=args.task, norm_layer='x').to(device)

    param_name = []
    for name, param in server_model.named_parameters() :
        param_name.append(name)

    models = [deepcopy(server_model).to(device) for idx in range(len(args.hospital_id))]

    if args.algorithm == "fedadam" or args.algorithm == "fedadagrad" or args.algorithm == "fedyogi" :
        m_t = initialization(args, server_model)
        v_t = initialization(args, server_model)

    if args.algorithm == "feddyn" :
        prev_grads = initialization_prev_grad(args, server_model)
        cld_model = deepcopy(server_model)

    criterion = task_losses(args.task)
    best_scores = 0.0
    best_models = []
    for idx in range(len(args.hospital_id)):
        best_models.append( deepcopy(server_model) )

    if args.test :
        if args.task == 'Final Acuity Outcome' :
            task_name = 'Final_Acuity_Outcome'
        else :
            task_name = args.task

        if args.algorithm == "fedadam" or args.algorithm == "fedadagrad" or args.algorithm == "fedyogi" :
            checkpoint = torch.load( os.path.join(f'{SAVE_PATH}', f'checkpoint_best_{task_name}_{args.model_type}_{args.learning_rate}_{args.server_learning_rate}_{args.tau}_{args.seed}.pt'))
        else :
            checkpoint = torch.load( os.path.join(f'{SAVE_PATH}', f'checkpoint_best_{task_name}_{args.model_type}_{args.seed}.pt'))

        for client_idx in range(len(args.hospital_id)):
            models[client_idx].load_state_dict(checkpoint['model_{}'.format(client_idx)])

        print(f"communications : {args.communications}, local epochs : {args.local_epochs}, {args.algorithm} {args.task} results")
        for c in range(len(args.hospital_id)) :
            model, test_loader = models[c], test_loaders[c]
            print(f"########## {c} client's test result ##########")
            test_loss, test_accuracy, auroc_macro, auprc_macro = test_naive(args, model, test_loader, criterion, device=device)

            print(f"{c} client test loss : ", test_loss)
            print(f"{c} client test accuracy : ", test_accuracy)
            print(f"{c} client test auroc(macro) : ", auroc_macro)
            print(f"{c} client test auprc(macro) : ", auprc_macro)
            print()

        exit()

    for comms in range(args.communications):

        if args.algorithm != "feddyn" :
            optimizers = [ torch.optim.Adam( models[idx].parameters(), lr=args.learning_rate, weight_decay=args.weight_decay) for idx in range(len(args.hospital_id)) ]
        else :
            optimizers = [ torch.optim.Adam( models[idx].parameters(), lr=args.learning_rate, weight_decay=args.feddyn_alpha + args.weight_decay) for idx in range(len(args.hospital_id)) ]

        schedulers = [ torch.optim.lr_scheduler.StepLR(optimizers[idx], args.learning_rate_step, args.learning_rate_decay) for idx in range(len(args.hospital_id)) ]

        for c in range(len(args.hospital_id)) :
            model, train_loader, optimizer, scheduler = models[c], train_loaders[c], optimizers[c], schedulers[c]

            if args.algorithm == "feddyn" :
                prev_grad_tensor = get_param_tensor(prev_grads[c])
                server_model_tensor = get_param_tensor(server_model)

            for epoch in range(args.local_epochs) :
                total_steps = epoch + comms * args.local_epochs

                if (args.algorithm == "fedprox" or args.algorithm == "fedpxn" ) and comms > 0 :
                    train_loss = train_fedprox(args, server_model, model, train_loader, criterion, optimizer, param_name, device=device, scheduler=scheduler)
                    print(f"[{c} client {total_steps} steps] train loss : ", train_loss)
                    if wandb_run :
                        wandb_run.log( {f'{c}_train_loss ': train_loss, 'total_step' : total_steps} )
                elif args.algorithm == "feddyn" :
                    train_loss = train_feddyn(args, model, server_model_tensor, train_loader, criterion, optimizer, prev_grad_tensor, device=device, scheduler=scheduler)
                    print(f"[{c} client {total_steps} steps] train loss : ", train_loss)
                    if wandb_run :
                        wandb_run.log( {f'{c}_train_loss ': train_loss, 'total_step' : total_steps} )
                else :
                    train_loss = train_naive(args, model, train_loader, criterion, optimizer, device=device, scheduler=scheduler)
                    print(f"[{c} client {total_steps} steps] train loss : ", train_loss)
                    if wandb_run :
                        wandb_run.log( {f'{c}_train_loss ': train_loss, 'total_step' : total_steps} )

            if args.algorithm == "feddyn" :
                prev_grads[c] = get_grad_diff( prev_grads[c], model, cld_model)

        ###################################################################################################
        if args.algorithm == "fedadam" or args.algorithm == "fedadagrad" or args.algorithm == "fedyogi" :
            server_model, models, m_t, v_t = communication_fedopt(args, server_model, models, client_weights, m_t, v_t)
        elif args.algorithm == "feddyn" :
            server_model, models, cld_model = communication_FedDyn(args, server_model, models, client_weights, cld_model, prev_grads)
        else :
            server_model, models = communication(args, server_model, models, client_weights)
        ###################################################################################################

        total_steps = (comms + 1) * args.local_epochs
        step_scores = 0.0

        for c in range(len(args.hospital_id)) :
            if args.algorithm != "feddyn" :
                model, valid_loader = models[c], valid_loaders[c]
            else :
                model, valid_loader = server_model, valid_loaders[c]

            valid_loss, valid_accuracy, auroc_macro, auprc_macro = test_naive(args, model, valid_loader, criterion, device=device)
            step_scores += auroc_macro
            print(f"{c} client valid loss : ", valid_loss)
            print(f"{c} client valid accuracy : ", valid_accuracy)
            print(f"{c} client valid AUROC(macro) : ", auroc_macro)
            print(f"{c} client valid AUPRC(macro) : ", auprc_macro)

            if wandb_run :
                wandb_run.log( {f'{c}_valid_loss ': valid_loss, 'total_step' : total_steps} )
                wandb_run.log( {f'{c}_valid_accuracy ': valid_accuracy, 'total_step' : total_steps} )
                wandb_run.log( {f'{c}_valid_AUROC(macro) ': auroc_macro, 'total_step' : total_steps} )
                wandb_run.log( {f'{c}_valid_AUPRC(macro) ': auprc_macro, 'total_step' : total_steps} )

        # save best model
        step_scores = step_scores / len(args.hospital_id)
        if step_scores > best_scores :
            patience = 0
            best_scores = step_scores
            best_models = update_best_models(args, best_models, models)
            save_models(args, best_models, args.task, SAVE_PATH, best=True)
            best_communication_rounds = comms + 1
        else :
            patience += 1

        save_models(args, models, args.task, SAVE_PATH, best=False)

    print("patience : ", patience)
    print("communication round of best model : ", best_communication_rounds)
    print("relative global epochs of best model : ", best_communication_rounds * args.local_epochs)

    if wandb_run :
        wandb_run.log( {'patience': patience} )
        wandb_run.log( {'communication_round_of_best_model': best_communication_rounds} )
        wandb_run.log( {'relative_global_epochs_of_best_model': best_communication_rounds * args.local_epochs} )


import random

class BMM:

  def __init__(self, data, K):
    self.data =data

    self.N = len(data)
    self.D = len(data[0])
    self.K = K

    # model parameters
    # random initialization
    self.z = [[0 for k in range(self.K)] for n in range(self.N)]
    self.pi = [1/self.K for k in range(self.K)]
    self.mu = [[random.random() for d in range(self.D)] for k in range(self.K)]

    # update parameters
    self.learn()

  # probability of x for the k component
  def pk(self, x, k):
    resp=1
    for i in range(len(x)):
      if x[i]==1: resp*=self.mu[k][i]
      else: resp*=(1-self.mu[k][i])
    return resp*self.pi[k]

  # e-m algorithm to learn the parameters
  def learn(self, data=None):
    change = True
    niter=0
    while change:
      change = False
      # E step
      for n in range(self.N):
        sumz=0
        for k in range(self.K):
          self.z[n][k] = self.pk(self.data[n], k)
          sumz+=self.z[n][k]
        for k in range(self.K):
          self.z[n][k]/=sumz

      print("\nZ:")
      print(self.z)

      # M step
      N_m = [0 for k in range(self.K)]
      z_x = [[0 for d in range(self.D)] for k in range(self.K)]
      newpi = [1/self.K for k in range(self.K)]
      newmu = [[1/self.D for d in range(self.D)] for k in range(self.K)]
      for k in range(self.K):
        for n in range(self.N):
          N_m[k]+=self.z[n][k]
          for d in range(self.D):
            z_x[k][d] += self.z[n][k]*self.data[n][d]
        for d in range(self.D):
          newmu[k][d] = z_x[k][d]/N_m[k]
        newpi[k] = N_m[k]/self.N

      print("\nN_m:")
      print(N_m)

      print("\nz_x:")
      print(z_x)

      for k in range(self.K):
        if self.pi[k] != newpi[k]:
          change=True
          self.pi[k] = newpi[k]
        for d in range(self.D):
          if self.mu[k][d] != newmu[k][d]:
            change=True
            self.mu[k][d] = newmu[k][d]
      niter+=1
    print("Finished in "+str(niter)+" iterations")

  def printModel(self):
     print("Mu: "+str(self.mu))
     print("Pi: "+str(self.pi))

  def predict(self, x):
    resp = []
    for k in range(self.K):
      resp.append(self.pk(x, k))
    return resp

  def predictAll(self, data):
     resp = []
     for dat in data:
       resp.append(self.predict(dat))
     return resp

data = [[1,1,1,1,0,0,0,0,0,0],
        [1,1,1,1,0,0,0,0,0,0],
        [0,0,0,0,1,1,1,1,1,1],
        [0,0,0,0,1,1,1,1,1,1]]

bmm = BMM(data, 2)
bmm.printModel()
for r in bmm.predictAll(data):
  print(r)

print("\n\n\n")


import os
import sys
import json
from shutil import copy

def get_config():
    with open("config.json") as f:
        config = json.load(f)
        config["alias"] = {str(als): judge for judge, li in config["alias"].items() for als in li}
    return config

config = get_config()

def get_path(folder):
    if folder in config["alias"]:
        folder = config["alias"][folder]
    path = f"{folder}/"
    if not os.path.exists(path):
        os.makedirs(path)
    return path

def get_src_name(name):
    contest = str(config["contest"])
    if contest == "-1": contest = ""
    contest += name
    return contest.upper()

if __name__ == "__main__":
    args = sys.argv[1:]
    if len(args) == 0:
        src, des = "template.cpp", "main.cpp"
        print("{} -> {}".format(src, copy(src, des)))
    else:
        if len(args) == 1: args.insert(0, str(config["default"]))
        if len(args) == 2: args.append("main")
        path = get_path(args[0])
        src = f"{path}{get_src_name(args[1])}.cpp"
        des = args[2] + ".cpp"
        print("{} -> {}".format(src, copy(src, des)))


from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from guardian.shortcuts import assign_perm, get_perms_for_model
from jsonfield import JSONField
from taggit.managers import TaggableManager

from onadata.apps.logger.models import XForm
from onadata.libs.models.base_model import BaseModel

class Project(BaseModel):
    class Meta:
        app_label = 'api'
        unique_together = (('name', 'organization'),)
        permissions = (
            ('view_project', "Can view project"),
            ('add_xform', "Can add xform to project"),
            ('transfer_project', "Can transfer project to different owner"),
        )

    name = models.CharField(max_length=255)
    metadata = JSONField(blank=True)
    organization = models.ForeignKey(User, related_name='project_organization')
    created_by = models.ForeignKey(User, related_name='project_creator')
    user_stars = models.ManyToManyField(User)
    shared = models.BooleanField(default=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

    tags = TaggableManager()

    def __unicode__(self):
        return u'%s|%s' % (self.organization, self.name)

    def clean(self):
        query_set = Project.objects.exclude(pk=self.pk)\
            .filter(name__iexact=self.name, organization=self.organization)
        if query_set.exists():
            raise ValidationError(u'Project name "%s" is already in'
                                  u' use in this account.'
                                  % self.name.lower())

def set_object_permissions(sender, instance=None, created=False, **kwargs):
    if created:
        for perm in get_perms_for_model(Project):
            assign_perm(perm.codename, instance.organization, instance)

            if instance.created_by:
                assign_perm(perm.codename, instance.created_by, instance)

def update_xform_share_settings(sender,
                                instance=None,
                                created=False,
                                **kwargs):
    if not created:
        project_xforms = instance.projectxform_set.exclude(
            xform__shared=instance.shared).values_list('xform', flat=True)
        XForm.objects.filter(pk__in=project_xforms).update(
            shared=instance.shared)

post_save.connect(set_object_permissions, sender=Project,
                  dispatch_uid='set_project_object_permissions')
post_save.connect(update_xform_share_settings, sender=Project,
                  dispatch_uid='update_xform_share_settings')


class DataBank(object):
    """
    Bid-Ask Prices of Pairs Stored by timestamp
    """
    timestamps = []
    prices = {} #Array of Price Quotes keyed by timestamp
    latestTimestamp = -1
    def pushNewData (self,timestamp,priceQuotes):
        """
        Expects array of PriceQuotes
        """
        if timestamp in self.timestamps:
            Exception
        else:
            self.timestamps.append(timestamp)
            self.prices[timestamp]=priceQuotes
            if self.latestTimestamp < timestamp :
                self.latestTimestamp = timestamp

    def getLatestPrices(self):
        if self.latestTimestamp == -1:
            Exception
        else:
            return self.prices[self.latestTimestamp]

    def getPrices(self,timestamp):
        if timestamp in self.timestamps:
            return self.prices[timestamp]
        else:
            Exception
class PriceQuote(object):
    """
    Stores point in time bid-ask spread for a currency pair
    """
    def __init__(self,pair,bid,ask):
        self.pair = pair
        self.bid = bid
        self.ask = ask


__fill__="\n\n####################\n\n"
__author__="CYbo"
__version__="1.0"
__desc__="Text crypter-decrypter \n"
__help__="<LETTER> <FUNCTION> \n E      used for encoding \n D     used for decoding \n e      used for exit \n h     generates this page \n q     same function as e \n s      if added to D or E it uses the local variable for storing the text \n"
def reorderer(text,psw):
        if len(psw) < 4:
                return "ERROR"
                exit
        textfin=""
        ist=0
        textr=""
        for i in range(1, len(text)+1):
            if ist==0:
                textr= textr + chr(ord(text[len(text)-i])-int(psw[1]))
                ist=1
            else:
                textr= textr + chr(ord(text[len(text)-i])-int(psw[2]))
                ist=0
        text=""
        for a in range(0, len(textr)):
            text=text+ chr(ord(textr[a])+a-int(psw[3]))
            textfin=textfin + chr(ord(text[a])+int(psw[0]))
        return textfin
def dereorderer(text,psw):
        if len(psw) < 4:
                return "ERROR"
                exit
        textfin=""
        if len(text)%2==0:
            ist=1
        else:
            ist=0
        textd=""
        for i in range(0, len(text)):
            textfin=textfin + chr(ord(text[i])-int(psw[0]))
            textd=textd+chr(ord(textfin[i])-i+int(psw[3]))
        text=""
        for a in range(1, len(textd)+1):
            if ist==0:
                text=text+ chr(ord(textd[len(textd)-a])+int(psw[1]))
                ist=1
            else:
                text=text+ chr(ord(textd[len(textd)-a])+int(psw[2]))
                ist=0
        return text
tmp=""
while True:
        func=input("Insert command function:")
        if func[0] =='E':
                text=input("Insert the text:")
                psw=input("Insert the 4 pin digit:")
                print("Your initial text:" + text)
                enc=reorderer(text,psw)
                print("FINAL ENCODED:"+enc)
                if len(func)==2:
                        if func[1]=="s":
                                tmp=enc
                                print("The encoded text is stored in the local variable")
        elif func[0] == 'D':
                if len(func)==2:
                        if func[1]=='s':
                                text=tmp
                else:
                        text=input("Insert the text:")
                psw=input("Insert the 4 pin digit:")
                print("Your initial encoded text:" + text)
                print("FINAL DECODE:"+dereorderer(text,psw))
        elif func[0] == 'e' or func == 'q':
                print("Quitting...")
                break
        elif func[0]=='h':
                print(__fill__)
                print("Author: "+__author__)
                print("Version: "+__version__)
                print("Description:"+__desc__)
                print("Generated help page:\n\n"+__help__)
                print(__fill__)
        else:
                print("#Command not recognized try h for more info#")
exit()


import sys

T = 3
while (T):
    N = int(input())
    s_ = 0
    for i in range(N):
        s_ += int(sys.stdin.readline())

    if s_ > 0:
        print("+")
    elif s_ == 0:
        print("0")
    else:
        print("-")

    T -= 1


from fastapi import FastAPI, status
from pydantic import BaseModel
from typing import Optional, List
from database import SessionLocal
import models

app=FastAPI()

#creating the columns of the database
class Inventory(BaseModel):
    id:int
    product_name:str
    variant:str
    sku:str
    price:float
    qty:int
    description:str

    class Config:
        orm_mode=True

db=SessionLocal()

# get everything from the database
@app.get('/inventory',response_model=List[Inventory],status_code=200)
def get_all_products():
    pens=db.query(models.Inventory).all()

    return pens

# get a single product from the database based on the given id
@app.get('/inventory/{product_id}',response_model=Inventory,status_code=status.HTTP_200_OK)
def get_a_product(product_id:int):
    product=db.query(models.Inventory).filter(models.Inventory.id==product_id).first()

    return product

# global search for a product based on the given keyword
@app.get('/inventory/search/{keyword}')
def search_products(keyword:str):
    result = []
    # allow there to be random string before and after the keyword
    tag = "%{}%".format(keyword)
    # search the keyword in all product names
    target=db.query(models.Inventory).filter(models.Inventory.product_name.like(tag)).all()
    result.append(target)
    # search the keyword in all variant
    target1=db.query(models.Inventory).filter(models.Inventory.variant.like(tag)).all()
    result.append(target1)
    # search the keyword in all sku
    target2 = db.query(models.Inventory).filter(models.Inventory.sku.like(tag)).all()
    result.append(target2)
    # search the keyword in all descriptions
    target3 = db.query(models.Inventory).filter(models.Inventory.description.like(tag)).all()
    result.append(target3)

    return result

# allow the user to buy a product
@app.get('/inventory/buy/{product_id}/{quantity}')
def buy_products(product_id:int,quantity:int):
    # get the product based on given id
    target = db.query(models.Inventory).filter(models.Inventory.id==product_id).first()

    # calculate the cost based on the price of the target product and the quantity
    total_cost=(target.price)*quantity

    if quantity > target.qty: # if the input quantity is larger than the supply
        return {"message":"Insufficient Supply!"}

    target.qty-=quantity

    return {"message":"Your total is: "+str(total_cost)}

# create a new product
@app.post('/inventory',response_model=Inventory,status_code=status.HTTP_201_CREATED)
def new_product(product:Inventory):
    new_product=models.Inventory(
        id=product.id,
        product_name=product.product_name,
        variant=product.variant,
        sku=product.sku,
        price=product.price,
        qty=product.qty,
        description=product.description
    )

    db.add(new_product)
    db.commit()

    return new_product

# decrement the quantity of a product
@app.post('/inventory/remove/{product_id}/{quantity}')
def remove_products(product_id:int,quantity:int):
    target = db.query(models.Inventory).filter(models.Inventory.id==product_id).first()

    # if the given quantity is larger than the supply, clear all the supplies
    if quantity > target.qty:
        target.qty=0
        return target

    target.qty-= quantity

    return target

# increment the quantity of a product
@app.post('/inventory/add/{product_id}/{quantity}')
def add_products(product_id:int,quantity:int):
    target = db.query(models.Inventory).filter(models.Inventory.id==product_id).first()

    target.qty += quantity

    return target

# update the info of a product
@app.put('/inventory/{product_id}',response_model=Inventory,status_code=status.HTTP_200_OK)
def update_product(product_id:int,product:Inventory):
    target=db.query(models.Inventory).filter(models.Inventory.id==product_id).first()
    target.product_name=product.product_name
    target.variant=product.variant
    target.sku=product.sku
    target.price=product.price
    target.qty=product.qty
    target.description=product.description

    db.commit()

    return target

# delete a product from the inventory
@app.delete('/inventory/{product_id}')
def delete_product(product_id:int):
    target=db.query(models.Inventory).filter(models.Inventory.id==product_id).first()

    # if the given id does not exist
    if target is None:
        return {"message":"PRODUCT NOT FOUND"}

    db.delete(target)
    db.commit()
    return target


# project/groups/utils.py
import qrcode
import io
import base64
import numpy as np
import os
from PIL import Image

from flask import current_app

def make_qr_code(text, color):

    qr = qrcode.QRCode(
        version=8,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
    )

    qr.add_data(text)
    qr.make(fit=True)

    # qrcode as background
    bg = qr.make_image(fill_color=color, back_color='white')
    bg_w, bg_h = bg.size

    # logo as foreground
    logo_path = os.path.join(current_app.root_path, 'static/images/wu-logo.png')
    logo = Image.open(logo_path, 'r')
    data = np.array(logo)

    # changing color of logo to desired color
    r1, g1, b1 = 255, 255, 255
    r2, g2, b2 = tuple(int(color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4)) # convert hex to rgb

    # reading colors
    red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]

    mask = (red < r1) & (green < g1) & (blue < b1) # mask for fill
    data[:,:,:3][mask] = [r2, g2, b2] # converting colors

    logo = Image.fromarray(data)
    logo_w, logo_h = logo.size

    # putting logo over qr code
    offset = ((bg_w - logo_w) // 2, (bg_h - logo_h) // 2)
    bg.paste(logo, offset, logo)
    bg.resize((100, 100))
    # returning qr code bytes written in ascii string so it can be parsed by browser
    bytes_io = io.BytesIO()
    bg.save(bytes_io, format='PNG')
    output = base64.b64encode(bytes_io.getvalue()).decode('ascii')

    return output


n = int(input())

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

for i in range(n):
    a, b = map(int, input().split())
    print(a * b // gcd(a, b))


'''
F SCORE
'''
# %% set system path
import sys,os

sys.path.append(os.path.abspath(".."))

# %% import data
import pandas as pd

month_return = pd.read_hdf('.\\data\\month_return.h5', key='month_return')
company_data = pd.read_hdf('.\\data\\last_filter_pe.h5', key='data')
trade_data = pd.read_hdf('.\\data\\mean_filter_trade.h5', key='data')
beta = pd.read_hdf('.\\data\\beta.h5', key='data')
risk_premium = pd.read_hdf('.\\data\\risk_premium.h5', key='data')
debt = pd.read_hdf('.\\data\\debt.h5', key='data')
profit = pd.read_hdf('.\\data\\profit.h5', key='data')
cash = pd.read_hdf('.\\data\\cash.h5', key='data')
operation = pd.read_hdf('.\\data\\operation.h5', key='data')
eq_offer = pd.read_hdf('.\\data\\eq_offer.h5', key='data')

# %% construct variable
# ROA
profit['ROA(TTM)'] = profit['F050204C']
# diff ROA
profit['delta_ROA'] = profit.groupby(['Stkcd'])['ROA(TTM)'].diff(4)
# CFOA = operation cash flow / total asset
profit['CFOA'] = profit['ROA(TTM)'] * profit['F052901C'] / profit['F050901B']
# Accrued profit
profit['accrued_profit'] = profit['F051101B'] - profit['CFOA']

# long term debt
debt['delta_LEVER'] = debt.groupby(['Stkcd'])['F011901A'].diff(4)
# diff liquidity
debt['delta_LIQUID'] = debt.groupby(['Stkcd'])['F010101A'].diff(4)
# EQ_OFFER already have

# margin profit
profit['delta_MARGIN'] = profit['F050201B'] / profit['F050901B']
# capital asset
operation['delta_TURN'] = operation.groupby(['Stkcd'])['F041701B'].diff(4)

def compare(x):
    if x > 0 :
        return 1
    else:
        return 0
def compare_re(x):
    if x < 0 :
        return 1
    else:
        return 0

def compare_eq(x):
    if x <= 0:
        return 1
    else:
        return 0

# count
profit['count_ROA(TTM)'] = profit['ROA(TTM)'].apply(compare)
profit['count_delta_ROA'] = profit['delta_ROA'].apply(compare)
profit['count_CFOA'] = profit['CFOA'].apply(compare)
profit['count_accrued_profit'] = profit['accrued_profit'].apply(compare_re)

debt['count_delta_LEVER'] = debt['delta_LEVER'].apply(compare_re)
debt['count_delta_LIQUID'] = debt['delta_LIQUID'].apply(compare_re)
eq_offer['count_EQ_OFFER'] = eq_offer['EQ_OFFER'].apply(compare_eq)

profit['count_delta_MARGIN'] = profit['delta_MARGIN'].apply(compare)
operation['count_delta_TURN'] = operation['delta_TURN'].apply(compare)

# %% data preprocessing
# forward the monthly return for each stock
# emrwd is the return including dividend
month_return['emrwd'] = month_return.groupby(['Stkcd'])['Mretwd'].shift(-1)
# emrnd is the return including no dividend
month_return['emrnd'] = month_return.groupby(['Stkcd'])['Mretnd'].shift(-1)
# select the A share stock
month_return = month_return[month_return['Markettype'].isin([1, 4, 16])]

# % distinguish the stocks whose size is among the up 30% stocks in each month
def percentile(stocks) :
    return stocks >= stocks.quantile(q=.3)

month_return['cap'] = month_return.groupby(['Trdmnt'])['Msmvttl'].apply(percentile)

profit = profit[profit['Typrep']=='A']
debt = debt[debt['Typrep']=='A']
eq_offer = eq_offer[eq_offer['Markettype'].isin([1,4,16])]
operation = operation[operation['Typrep']=='A']

# %%construct variable
import numpy as np
# BM
company_data['BM'] = 1 / company_data['PBV1A']

# Size
month_return['Size'] = np.log(month_return['Msmvttl'])

# %% merge data
from pandas.tseries.offsets import *

month_return['Stkcd_merge'] = month_return['Stkcd'].astype(dtype='string')
month_return['Date_merge'] = pd.to_datetime(month_return['Trdmnt'])
#month_return['Date_merge'] += MonthEnd()

profit['Stkcd_merge'] = profit['Stkcd'].astype(dtype='string')
profit['Date_merge'] = pd.to_datetime(profit['Accper'])
profit['Date_merge'] += MonthBegin()

debt['Stkcd_merge'] = debt['Stkcd'].astype(dtype='string')
debt['Date_merge'] = pd.to_datetime(debt['Accper'])
debt['Date_merge'] += MonthBegin()

eq_offer['Stkcd_merge'] = eq_offer['Stkcd'].astype(dtype='string')
eq_offer['Date_merge'] = pd.to_datetime(eq_offer['Trdmnt'])

operation['Stkcd_merge'] = operation['Stkcd'].astype(dtype='string')
operation['Date_merge'] = pd.to_datetime(operation['Accper'])
operation['Date_merge'] += MonthBegin()

company_data['Stkcd_merge'] = company_data['Symbol'].dropna().astype(dtype='int').astype(dtype='string')
company_data['Date_merge'] = pd.to_datetime(company_data['TradingDate'])
company_data['Date_merge'] += MonthBegin()

# merge
return_company = pd.merge(month_return, profit, on=['Stkcd_merge', 'Date_merge'], how='left')
return_company = pd.merge(return_company, debt, on=['Stkcd_merge', 'Date_merge'], how='left')
return_company = pd.merge(return_company, eq_offer, on=['Stkcd_merge', 'Date_merge'], how='left')
return_company = pd.merge(return_company, operation, on=['Stkcd_merge', 'Date_merge'], how='left')
return_company = pd.merge(return_company, company_data, on=['Stkcd_merge', 'Date_merge'], how='left')

return_company['Stkcd_fillna'] = return_company['Stkcd_merge']
return_company = return_company.groupby(['Stkcd_fillna']).fillna(method='pad')
return_company = return_company.fillna(0)

# %% construct F score
return_company['F_SCORE'] = return_company[['count_ROA(TTM)', 'count_delta_ROA', 'count_CFOA', 'count_accrued_profit', 'count_delta_LEVER', 'count_delta_LIQUID', 'count_EQ_OFFER', 'count_delta_MARGIN', 'count_delta_TURN']].sum(axis=1)

# %% construct test_data for bivariate analysis
# dataset 1
from portfolio_analysis import Bivariate, Univariate
import numpy as np

# select stocks whose size is among the up 30% stocks in each month and whose trading
# days are more than or equal to 10 days
test_data_1 = return_company[(return_company['cap']==True) & (return_company['Ndaytrd']>=10)]
test_data_1 = test_data_1[['emrwd', 'F_SCORE', 'BM', 'Date_merge']].dropna()
test_data_1 = test_data_1[(test_data_1['Date_merge'] >= '2000-01-01') & (test_data_1['Date_merge'] <= '2019-12-01')]

# Univariate analysis
uni_1 = Univariate(test_data_1[['emrwd', 'F_SCORE', 'Date_merge','BM']], number=2, weight=True)
uni_1.fit()
uni_1.print_summary()

# Bivariate analysis
bi_1 = Bivariate(test_data_1, number=2)
bi_1.fit()
bi_1.print_summary()

# %% risk adjustment
risk_model = risk_premium[['MKT', 'SMB', 'HML']]
risk_model = risk_model['2000':'2019']
bi_1.factor_adjustment(risk_model)
bi_1.print_summary()

# %% construct test_data for bivariate analysis
# dataset 2: Size weighted
from portfolio_analysis import Bivariate, Univariate
import numpy as np

# select stocks whose size is among the up 30% stocks in each month and whose trading
# days are more than or equal to 10 days
test_data_2 = return_company[(return_company['cap']==True) & (return_company['Ndaytrd']>=10)]
test_data_2 = test_data_2[['emrwd', 'F_SCORE', 'BM', 'Date_merge', 'Size']].dropna()
test_data_2 = test_data_2[(test_data_2['Date_merge'] >= '2000-01-01') & (test_data_2['Date_merge'] <= '2019-12-01')]

# Univariate analysis
uni_2 = Univariate(test_data_2[['emrwd', 'F_SCORE', 'Date_merge']], number=2)
uni_2.fit()
uni_2.print_summary()

# Bivariate analysis
bi_2 = Bivariate(test_data_2, number=2, weight=True)
bi_2.fit()
bi_2.print_summary()

# %% risk adjustment
risk_model = risk_premium[['MKT', 'SMB', 'HML']]
risk_model = risk_model['2000':'2019']
bi_2.factor_adjustment(risk_model)
bi_2.print_summary()


import cv2
import tkinter
import logging

# load logger
logger = logging.getLogger("AnnotationObject")

def hex_to_rgb(hex_color):
    return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))

class BoxAnnotation:

    def __init__(self, points, class_name, class_id, obj_id, color='#ffffff'):
        """
        point1 and point2 are the upper left and the lower right corner, respectively.
        tuples of (x,y)
        """

        self.class_name = class_name
        self.class_id = class_id
        self.obj_id = obj_id

        # hold a reference to the drawing
        self.draw_ref = None

        # store the box coordinates
        self.coords = points

        self.tag = f"obj_{obj_id}_tag"

        # annotation color
        self.color = color

        # is the annotation visible
        self.visible = True

        logger.debug(f"creating annotation box with tag {self.tag}")

    def draw_annotation_to_array(self, frame, color, active=False):
        """
            Given a numpy array representing an image draw this object.
        """

        cv2.rectangle(frame, (self.coords[0],self.coords[1]), (self.coords[2],self.coords[3]), hex_to_rgb(self.color[1:]), thickness=2)

        if active:
            spacing = 4
            cv2.rectangle(frame, (self.coords[0] + spacing,self.coords[1] + spacing), (self.coords[2] - spacing,self.coords[3] - spacing), hex_to_rgb(self.color[1:]), thickness=2)

    def draw_annotation(self, canvas,color):
        """
            Given a tkinter canvas object, draw this object.

            For finding the same object but drawn in different frames,
            the object tag is used to find the correct object.
        """

        # update color
        self.color = color

        # if the draw reference is not None, the annotation has been drawn
        if self.draw_ref is not None:

            state = tkinter.NORMAL if self.visible else tkinter.HIDDEN

            # update color
            canvas.itemconfig(self.draw_ref, outline=color)

            # update visibility
            canvas.itemconfig(self.draw_ref, state=state)

            # update location
            canvas.coords(self.draw_ref, *self.coords)

        # if the annotation has not been draw yet, create a new box
        # First check if the object with this id (and tag) has been drawn already
        else:

             # find boxes that correspond to this object and are already drawn on canvas
            drawn_tags = canvas.find_withtag(self.tag)

            # if the annotation is drawn already, update the current annotation
            if len(drawn_tags) > 0:
                logger.debug(f"Not creating new box for annotation because found one drawn with tags {drawn_tags}")
                self.draw_ref = drawn_tags[0]

            # if the draw ref is still None, this object has not been drawn
            if self.draw_ref is None:

                self.draw_ref = canvas.create_rectangle(self.coords,
                                                        tags=self.tag,
                                                        fill="",
                                                        width=2,
                                                        outline=color)

                # move the recently created item to the top
                canvas.tag_raise(self.draw_ref)

    def update_annotation(self, coords=None, visible=True, color=None, class_name=None, class_id=None):
        """
            Update the visibility, position, and/or color of the drawn annotation
        """

        logger.debug(f"Updating annotation with coords: {coords} visible {visible} color {color} and class_name {class_name}")
        # control the visibility
        self.visible = visible

        # update the coordinates is they are given
        self.coords = self.coords if coords is None else coords

        # update the class
        self.class_name = self.class_name if class_name is None else class_name
        self.class_id = self.class_id if class_id is None else class_id

        # update color
        self.color = self.color if color is None else color

    def detection_to_json(self):
        """
        example:
            {
              "class_name" : "normal",
              "class_id" : 0,
              "object_id" : 0,
              "object_coords" : [
            	{
            	  "x" : 100,
            	  "y" : 100
            	},
            	{
            	  "x" : 200,
            	  "y" : 200
            	}
              ]
            }

        """

        det_dict = {
                    'class_name' : self.class_name,
                    'class_id' : self.class_id,
                    'object_id' : self.obj_id,
                    'object_coords' : [
                        {
                            'x' : self.coords[0],
                            'y' : self.coords[1]
                        },
                        {
                            'x' : self.coords[2],
                            'y' : self.coords[3]
                        }
                    ]
        }

        return det_dict

    @classmethod
    def from_detection_json(cls, detection_json):

        """
        given a json formatted detection, create a box object from it
        """

        points = (detection_json['object_coords'][0]['x'], detection_json['object_coords'][0]['y'],
                detection_json['object_coords'][1]['x'], detection_json['object_coords'][1]['y'])

        return cls(points, detection_json['class_name'], detection_json['class_id'], detection_json['object_id'])

    def __repr__(self):
        return f"{self.__class__.__name__} of class {self.class_name} and object id {self.obj_id} at {self.coords}"

class TextBoxAnnotation(BoxAnnotation):

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

        self.text = ""

    def update_annotation(self, text=None, **kwargs):

        super().update_annotation(**kwargs)

        self.text = self.text if text is None else text

    @classmethod
    def from_detection_json(cls, detection_json):
        """
            Do the same as parent class but just add the extra text
        """

        # this calls the parent method but returns TextBoxAnnotation :)
        basic_annotation = super(TextBoxAnnotation, cls).from_detection_json(detection_json)

        if 'text' not in detection_json:
            print(f"No text found for anntoation {basic_annotation}, cannot add it")
            return basic_annotation

        basic_annotation.update_annotation(text=detection_json['text'])

        print(f"Loaded annotation {basic_annotation}")

        return basic_annotation

    def detection_to_json(self):
        """
            The same as parent but with added text
        """
        parent_json = super().detection_to_json()

        parent_json['text'] = self.text

        return parent_json

    def __repr__(self):
        return f"{self.__class__.__name__} of class {self.class_name} and object id {self.obj_id} at {self.coords} with text {self.text}"


def bin_to_dec(num: str):
    number = 0
    for elem in range(1, len(num) + 1):
        number += int(num[-elem]) * 2 ** (elem - 1)
    return number

def gamma_epsilon_rate(data):
    with open(data) as f:
        file = f.readlines()

    num_rate = [0 for digit in file[0].strip()]
    counter = 0
    for line in file:
        counter += 1
        for i in range(len(line.strip())):
            num_rate[i] += int(line.strip()[i])

    gamma_rate = ''
    epsilon_rate = ''
    for i in range(len(num_rate)):
        if num_rate[i] > counter // 2:
            gamma_rate += '1'
            epsilon_rate += '0'
        else:
            gamma_rate += '0'
            epsilon_rate += '1'

    gamma_rate = bin_to_dec(gamma_rate)
    epsilon_rate = bin_to_dec(epsilon_rate)
    result = gamma_rate * epsilon_rate

    return gamma_rate, epsilon_rate, result

def oxygen_co2_rating(data, value:str):
    with open(data) as f:
        file = f.readlines()

    num_list = file

    if value == 'oxygen':
        for digit in range(len(file[0].strip())):
            list_zero = []
            list_one = []
            for num in num_list:
                if num[digit] == "1":
                    list_one.append(num)
                elif num[digit] == "0":
                    list_zero.append(num)
            if len(list_one) > len(list_zero):
                num_list = list_one
            elif len(list_one) < len(list_zero):
                num_list = list_zero
            else:
                num_list = list_one
            if len(num_list) == 1:
                break
    else:
        for digit in range(len(file[0].strip())):
            list_zero = []
            list_one = []
            for num in num_list:
                if num[digit] == "1":
                    list_one.append(num)
                elif num[digit] == "0":
                    list_zero.append(num)
            if len(list_one) < len(list_zero):
                num_list = list_one
            elif len(list_one) > len(list_zero):
                num_list = list_zero
            else:
                num_list = list_zero
            if len(num_list) == 1:
                break

    return bin_to_dec(num_list[0].strip())

if __name__ == '__main__':
    input = 'input.txt'

    print(gamma_epsilon_rate(input))

    oxygen_generator_rating = oxygen_co2_rating(input, 'oxygen')
    co2_scrubber_rating = oxygen_co2_rating(input, 'co2')
    print(f'oxygen generator rating = {oxygen_generator_rating}')
    print(f'co2 scrubber rating = {co2_scrubber_rating}')
    print(f'life support rating = {oxygen_generator_rating * co2_scrubber_rating}')


class Solution:
  def findPythagoreanTriplets(self, nums):
    for i in range(len(nums)):
      nums[i] *= nums[i]
    nums.sort()
    for k in range(2, len(nums)):
      i = 0
      j = k - 1
      while (i < j):
        s = nums[i] + nums[j]
        if s < nums[k]:
          i += 1
        elif s > nums[k]:
          j -= 1
        else:
          return True

    return False

def main():
  s = Solution()
  print(s.findPythagoreanTriplets([3, 12, 5, 13]))

if __name__ == '__main__':
  main()


'''
sum_pairs([4,2,10,5,1], 6) # [4,2]
sum_pairs([11,20,4,2,1,5], 100) # []
'''

def sum_pairs(ints, s):
    already_visited = set()
    for i in ints:
        difference = s - i
        if difference in already_visited:
            return [difference, i]
        already_visited.add(i)
    return []

print(sum_pairs([4, 2, 10, 5, 1], 6))  # [4,2])


import numpy as np
import torch
from torch.nn import functional as F

ANGLE_EPS = 0.001

def normalize(v):
    return v / np.linalg.norm(v)

def get_r_matrix(ax_, angle):
    ax = normalize(ax_)
    if np.abs(angle) > ANGLE_EPS:
        S_hat = np.array(
            [[0.0, -ax[2], ax[1]], [ax[2], 0.0, -ax[0]], [-ax[1], ax[0], 0.0]],
            dtype=np.float32)
        R = np.eye(3) + np.sin(angle) * S_hat + \
            (1 - np.cos(angle)) * (np.linalg.matrix_power(S_hat, 2))
    else:
        R = np.eye(3)
    return R

def r_between(v_from_, v_to_):
    v_from = normalize(v_from_)
    v_to = normalize(v_to_)
    ax = normalize(np.cross(v_from, v_to))
    angle = np.arccos(np.dot(v_from, v_to))
    return get_r_matrix(ax, angle)

def rotate_camera_to_point_at(up_from, lookat_from, up_to, lookat_to):
    inputs = [up_from, lookat_from, up_to, lookat_to]
    for i in range(4):
        inputs[i] = normalize(np.array(inputs[i]).reshape((-1,)))
    up_from, lookat_from, up_to, lookat_to = inputs
    r1 = r_between(lookat_from, lookat_to)

    new_x = np.dot(r1, np.array([1, 0, 0]).reshape((-1, 1))).reshape((-1))
    to_x = normalize(np.cross(lookat_to, up_to))
    angle = np.arccos(np.dot(new_x, to_x))
    if angle > ANGLE_EPS:
        if angle < np.pi - ANGLE_EPS:
            ax = normalize(np.cross(new_x, to_x))
            flip = np.dot(lookat_to, ax)
            if flip > 0:
                r2 = get_r_matrix(lookat_to, angle)
            elif flip < 0:
                r2 = get_r_matrix(lookat_to, -1. * angle)
        else:
            # Angle of rotation is too close to 180 degrees, direction of
            # rotation does not matter.
            r2 = get_r_matrix(lookat_to, angle)
    else:
        r2 = np.eye(3)
    return np.dot(r2, r1)

def get_grid(pose, grid_size, precision):
    """
    Input:
        `pose` FloatTensor(bs, 3)
        `grid_size` 4-tuple (bs, _, grid_h, grid_w)
        `device` torch.device (cpu or gpu)

    Output:
        `rot_grid` FloatTensor(bs, grid_h, grid_w, 2)
        `trans_grid` FloatTensor(bs, grid_h, grid_w, 2)
    """
    x = pose[:, 0]
    y = pose[:, 1]
    t = pose[:, 2]

    t = t * np.pi / 180.
    cos_t = t.cos()
    sin_t = t.sin()

    theta11 = torch.stack([cos_t, -sin_t, torch.zeros_like(cos_t)], 1)
    theta12 = torch.stack([sin_t, cos_t, torch.zeros_like(cos_t)], 1)
    theta1 = torch.stack([theta11, theta12], 1)

    theta21 = torch.stack([torch.ones_like(x), torch.zeros_like(x), x], 1)
    theta22 = torch.stack([torch.zeros_like(x), torch.ones_like(x), y], 1)
    theta2 = torch.stack([theta21, theta22], 1)

    rot_grid = F.affine_grid(theta1, torch.Size(grid_size)).to(precision)
    trans_grid = F.affine_grid(theta2, torch.Size(grid_size)).to(precision)

    return rot_grid, trans_grid


"""
    This document contains all under the name "IMAGES" and it
    contains 2 classes. The first class, `Page`, contains the
    initialization of a page on a renderer(assumed as root).

    Furthermore it has code that builds the physical structure
    of the first page, which contains buttons that will lead
    to the simulations.
    """
import tkinter as tk
from tkinter import ttk
import numpy as np
from PIL import ImageTk, Image
import matplotlib.pyplot as plt
import matplotlib as mpl

#The next two imports are from self-made files.
from images import IMAGES
from messages import m1#Message for tk.messagebox about creation
                       #of duplicate first page

IMAGES = IMAGES #This contains all images needed in the GUI.
#{'planet_icon': , 'question_icon': , 'box_icon': , 'time_icon':,
#'arrow_right':, 'arrow_left':, ...}

class Page(tk.Frame):
    """
        This class contains the general layout for
        a page.
        Parameters
        --------------
        name: str -> Name of the page. This allows
        for identification of pages.

        """
    instances = {} #Will contain ALL the pages of the GUI

    conversions = {
            "nm":1e-9, "μm":1e-6, "mm":1e-3, "cm":1e-2,
            "dm":0.1, "m":1, "km":1e3, "Mm":1e6, "Gm":1e9,
            "μm":1e-6, "ms":1e-3, "s":1, "min":60, "hour":3600,
            "day":3600*24, "month":3600*24*30, "year":3600*24*30*12,
            "km/s":1e3, "kg/m3":1, "g/m3":1e-3, "C/kg":1, "C/g":1e3,
            "C/mg":1e6, "m/s":1, "kg":1}

    def __init__(self, master=False, name = "start", *args, **kwargs):
        #First we make sure the parent can resize
        master.geometry("510x400")
        master.update()
        master.columnconfigure([0], weight=1)
        master.rowconfigure([0], weight=1)

        #There is a bug that causes tkinter to not close sometimes
        #It is solved using the following code line
        master.protocol("WM_DELETE_WINDOW", lambda: [master.quit(),
                                                     master.destroy(),
                                                     plt.close("all")])

        #Then we create the frame object and place it in the parent
        tk.Frame.__init__(self, master,
                          *args, **kwargs)

        self.grid(column = 0, row = 0, sticky = "NSEW")

        #This part ensures that there is only one starting page
        #It also works as reminder in case I forget to pass an id
        #to a page.
        if name == "start" and "start" in self.__class__.instances:
            tk.messagebox.showwarning("GIVE NAME", m1)
        elif "start" not in self.__class__.instances:
            img = ImageTk.PhotoImage(IMAGES["time_icon"].resize((30, 30)))
            master.iconphoto(False, img)
            master.title("Predru's simulator")

        self.__class__.instances[name] = self
        #Now you can access the newly created instance
        #by calling Page.pages[f"{pagename}"]
        # Current pages
        # -------------
        # "main_page": Entrance of the app
        # "page1" : Solar system page
        # "page1_1": Settings for planet simualtion.
        # "page1_2": Simulation planets
        # "page4":Clock

        #This is just a list of colors for whenever colors are needed

        TABLEAU_COLORS2 = [c.strip("tab:") for c in list(mpl._color_data.TABLEAU_COLORS)]
        TABLEAU_COLORS2[0] = "blue"
        self.colors = TABLEAU_COLORS2
        self.all_colors = list(mpl._color_data.CSS4_COLORS)
        self.markers = list(mpl.markers.MarkerStyle.markers)
        self.colors+=["yellow", "white"]

    def make_navigation_buttons(self, forward = False, backward = False):
        """
            This creates the buttons that allow to go to other pages
            self.change_page() takes in the page you want to go to
            and return a function that raises that page.
            It just makes the buttons! It does not place them.
            """
        if forward:
            self.r = ImageTk.PhotoImage(IMAGES['arrow_right'].resize((20,20)))
            self.forward_button = ttk.Button(self,
                                     image = self.r,
                                     takefocus = 0,
                                     command = self.change_page(forward))
        if backward:
            self.l = ImageTk.PhotoImage(IMAGES['arrow_left'].resize((20,20)))
            self.backward_button = ttk.Button(self,
                                     image = self.l,
                                     takefocus = 0,
                                     command = self.change_page(backward))

    def change_page(self, where):
        """
            Changes current page that is being displayed
            Parameters
            ----------
            where: str -> page name
            """
        def switch():
            try:
                self.__class__.instances[where].grid(
                    row = 0, column = 0,
                    sticky = "NSEW") #Makes it easier to handle resize
                self.__class__.instances[where].tkraise()
                self.grid_forget()
            except : print("NOT IMPLEMENTED")
        return switch

    def delete_canvas(self):
        if hasattr(self, "running"):
            self.running = False
        if hasattr(self, "universe"):
            self.universe.running = False
        if hasattr(self, "canvas"):
            setattr(self, "running", False)
            for x in range(20):#Makes nice fading pattern.
                for a in self.ax.get_children():
                    a.set_alpha((20-x)/20)
                self.canvas.draw()
            self.ax.figure.clear()
            plt.close('all')
            self.canvas.get_tk_widget().delete("all")
            self.canvas.get_tk_widget().grid_forget()
            self.canvas.get_tk_widget().destroy()
            self.main_frame.update()
            delattr(self, "canvas")

class Main_page(Page):
    """
        This is the first page of the application.
        It makes use of the class defined above to
        configure the widgets and other parameters
        """
    def __init__(self, *args, **kwargs):
        Page.__init__(self, name = "main_page", *args, **kwargs)
        self.master.geometry("510x400")
        self.master.update()

        #The next part makes the use of grid easier
        #The idea is to use many squares like large pixels
        grid_len = [x for x in range(30)]
        minsize = (self.master.winfo_width()/30,
                   self.master.winfo_height()/30)

        self.columnconfigure(grid_len,
                             minsize = minsize[0],
                             weight = 1,
                             )

        self.rowconfigure(grid_len,
                          minsize = minsize[1],
                          weight=1,
                          )

        self.main_frame = tk.Frame(self,
                                   relief = "sunken",
                                   border = 1
                                   )

        self.main_frame.grid(column=1, row=1, sticky="NSEW",
                             columnspan=28, rowspan = 28,
                             )
        self.main_frame.update()
        grid_len = [x for x in range(30)]
        minsize = (self.main_frame.winfo_width()/30,
                   self.main_frame.winfo_height()/30)
        self.main_frame.rowconfigure(grid_len,
                                     minsize = minsize[1],
                                     weight =1
                                     )

        self.main_frame.columnconfigure(grid_len,
                                        minsize = minsize[0],
                                        weight = 1,
                                        )

        self.make_navigation_buttons(forward = "page2",backward = "None")
        self.forward_button.grid(row = 27, column = 28,
                                 rowspan = 2, columnspan = 2,
                                 sticky = "NSEW",
                                 )
        self.forward_button.config(cursor ="pirate")

        self.backward_button.grid(row = 27, column = 26,
                                 rowspan = 2, columnspan = 2,
                                 sticky = "NSEW",
                                  )
        self.backward_button.config(cursor = "pirate")

        #Since we can't edit the font, a dummy variable with a style will be made
        s = ttk.Style()
        s.configure("new.TButton", font = ("Consolas","15","bold"))
        self.planet_icon = ImageTk.PhotoImage(IMAGES["planet_icon"].resize((80,80)))
        self.large_sim_button = ttk.Button(self.main_frame,
                                           text="Large scale",
                                           image = self.planet_icon,
                                           takefocus = 0,
                                           compound = tk.BOTTOM,
                                           style = "new.TButton",
                                           command = self.change_page("page1"),
                                           )

        self.large_sim_button.grid(row=2, column=3,
                                   rowspan=13, columnspan = 11,
                                   sticky="NSEW",
                                   )

        self.question_icon = ImageTk.PhotoImage(IMAGES["question_icon"].resize((80,80)))
        self.help_button = ttk.Button(self.main_frame,
                                      text = "Help",
                                      image = self.question_icon,
                                      takefocus = 0,
                                      compound =tk.BOTTOM,
                                      style = "new.TButton",
                                      command = self.change_page("helppage"),
                                      cursor = "pirate"
                                      )

        self.help_button.grid(row=2, column=14, rowspan=13,
                              columnspan = 11, sticky = "NSEW",
                              )

        self.time_icon = ImageTk.PhotoImage(IMAGES["time_icon"].resize((80, 80)))
        self.spring_icon = ImageTk.PhotoImage(IMAGES["spring_icon"].resize((80,80)))
        self.coils_button = ttk.Button(self.main_frame,
                                      text = "Hook's law",
                                      image = self.spring_icon,
                                      takefocus = 0,
                                      compound =tk.BOTTOM,
                                      style = "new.TButton",
                                      command = lambda: [
                                          self.change_page("page3")(),
                                          self.master.geometry("550x500"),
                                          ],
                                      cursor = "pirate",
                                      )
        self.coils_button.grid(row=15, column=3,
                              rowspan=13, columnspan = 11,
                              sticky="NSEW",
                              )

        self.sub_icon = ImageTk.PhotoImage(IMAGES["time_icon"].resize((80, 80)))
        self.subatomic_b = ttk.Button(self.main_frame,
                                      text = "Subatomic",
                                      image = self.time_icon,
                                      takefocus = 0,
                                      compound =tk.BOTTOM,
                                      style = "new.TButton",
                                      command = self.change_page("page4"),
                                      )

        self.subatomic_b.grid(row=15, column=14,
                              rowspan=13, columnspan = 11,
                              sticky="NSEW",
                              )

if __name__ == "__main__":
    """This part is just for testing"""
    window = tk.Tk()
    main_page = Main_page(window)
    window.mainloop()