import copy
import math
import numpy as np
import torch
import torch.nn.functional as F
import gym
import highway_env

from random import randint

from lib.policy.batch import Batch
from lib.policy.continuous_mlp import ContinuousMLP
from lib.optimizers.actor_critic.actor_critic import TRPO

from lib.environments.parking import ParkingEmpty

from lib.vi.value_iteration_parking_orientation_new import ValueIteration33SyncNN

class Buffer:
    def __init__(self, max_size=int(16000)):
        self.max_size = max_size
        self.ptr = 0
        self.size = 0

        self.x = np.zeros((max_size, 3))
        self.y = np.zeros((max_size, 2))
        self.w = np.zeros((max_size, 5))

    def add(self, x, y):
        self.x[self.ptr] = x
        self.y[self.ptr] = y
        # self.w[self.ptr] = w

        self.ptr = (self.ptr + 1) % self.max_size
        self.size = min(self.size + 1, self.max_size)

    def sample(self, batch_size):
        ind = np.random.randint(0, self.size, size=batch_size)

        return (
            self.x[ind],
            self.y[ind]
        )

    def get_weights(self):
        class_sum = np.sum(self.w, axis=0)
        class_sum = class_sum / sum(class_sum)
        class_sum += 1e-3 * np.ones_like(class_sum)
        class_sum = np.array([1 / e for e in list(class_sum)])
        return class_sum / sum(class_sum)

class TRPOTrainer:

    def __init__(self,
                 op_mode,
                 state_dim,
                 action_dim,
                 hidden_sizes,
                 max_kl,
                 damping,
                 batch_size,
                 inner_episodes,
                 max_iter,
                 optimistic_model,
                 use_fim=False,
                 use_gpu=False
                 ):

        self.grid = ParkingEmpty()
        self.policy = ContinuousMLP(state_dim,
                                    action_dim,
                                    hidden_sizes=hidden_sizes,
                                    activation=F.relu,
                                    is_disc_action=False)
        self.optimizer = TRPO(policy=self.policy,
                              use_gpu=use_gpu,
                              max_kl=max_kl,
                              damping=damping,
                              use_fim=use_fim,
                              discount=0.99,
                              imp_weight=False)
        self.buffer = Buffer()
        gx = self.grid.x_size
        gy = self.grid.y_size
        self.vi = ValueIteration33SyncNN(grid_size_x=gx,
                                         grid_size_y=gy,
                                         gamma=self.optimizer.discount)

        self.op_mode = op_mode
        self.optimistic_model = optimistic_model
        self.batch_size = batch_size
        self.inner_episodes = inner_episodes
        self.max_iter = max_iter
        self.state_dim = state_dim
        self.dqn_steps = 0
        self.eps = 1.0
        self.time_scale = 2
        self.episode_steps = 0

    def roll_out_in_env(self, start, horizon):
        roll_out = Batch()
        s = start
        s_list = []
        a_list = []
        r_list = []

        d = False
        oob = False
        break_var = False
        s_u_goal = self.grid.lls2hls(s['observation'])
        target_vec = self.vi.get_target_vec(s['observation'])
        goal = self.grid.lls2hls(s['observation'] + np.array(list(target_vec[:2]) + [0, 0] + list(target_vec[2:4])))

        for step_i in range(horizon):
            self.episode_steps += 1

            target_vec = self.vi.get_target_vec(s['observation'])
            s_save = copy.deepcopy(np.array(list(target_vec[:2]) + list(s['observation'][2:4]) + list(target_vec[2:4])))

            s_list.append(copy.deepcopy(s['observation']))
            s_tensor = torch.tensor(s_save, dtype=torch.float).unsqueeze(0)
            a = self.policy.select_action(s_tensor)

            s_new, r, d, info = self.grid.env.step(a)
            s_new_bar = self.grid.lls2hls(s_new['observation'])
            success_var = info["is_success"]
            info = not info["is_success"]

            d = (s_new_bar[0] == goal[0]) and (s_new_bar[1] == goal[1]) and (s_new_bar[2] == goal[2])
            r = 0.0
            if d:
                r = 1.0
                info = False

            if success_var:
                info = False
                break_var = True

            break_var = break_var or (not info) or (step_i + 1 == horizon) or (self.episode_steps == self.max_iter)

            ib = self.grid.check_in_bounds(s_new['observation'])
            if not ib:
                oob = True

            a_list.append(a)
            r_list.append(r)
            roll_out.append(
                Batch([a.astype(np.float32)],
                      [s_save.astype(np.float32)],
                      [r],
                      [s_new['observation'].astype(np.float32)],
                      [0 if (break_var or oob) else 1],
                      [info],
                      [1.0]))

            s = s_new
            if break_var or oob:
                break

        s_list.append(copy.deepcopy(s['observation']))

        return roll_out, s_list, a_list, r_list, d, success_var, s_new, s_new_bar, oob

    def simulate_env(self, mode):
        batch = Batch()
        num_roll_outs = 0
        num_steps = 0
        total_success = 0
        total_wp_success = 0
        j = 0.
        jwp = 0.

        if mode == 'train':

            while num_steps < self.batch_size:

                """ INITIALIZE THE ENVIRONMENT """
                s_init = self.grid.reset()
                s_start = self.grid.lls2hls(s_init['observation'])
                s_goal = self.grid.lls2hls(s_init['desired_goal'])
                self.episode_steps = 0

                """ V MAP """
                if self.optimistic_model:
                    self.vi.update_p_table_optimistic(occupancy_map=self.grid.occupancy_map, walls=False)
                else:
                    self.vi.update_p_table(occupancy_map=self.grid.occupancy_map, walls=True)

                v, pi = self.vi.run_vi(grid=self.grid, goal=(s_goal[0], s_goal[1], s_goal[2]))

                """ START THE EPISODE """
                horizon_left = self.max_iter
                success = False

                hl_s_list = []
                hl_s_new_list = []
                hl_a_list = []
                hl_r_list = []
                hl_d_list = []

                while (horizon_left > 0) and not success:

                    # GET THE TARGET VECTOR
                    self.dqn_steps += 1
                    self.eps = 0.01 + 0.99 * math.exp(-1. * self.dqn_steps / 10000)

                    s_bar = self.grid.lls2hls(s_init['observation'])
                    hl_s_list.append(s_bar)

                    if torch.rand(1)[0] > self.eps:
                        a_bar = pi[s_bar[0], s_bar[1], s_bar[2]]
                    else:
                        a_bar = (randint(0, 7), randint(0, 7))

                    self.vi.set_target(s_bar, a_bar)

                    roll_out, _, _, _, wp_success, success, l_state, s_bar_p, oob = self.roll_out_in_env(
                        horizon=self.time_scale,
                        start=s_init)

                    hl_s_new_list.append(s_bar_p)
                    hl_a_list.append(a_bar)

                    s_init = l_state

                    num_roll_outs += 1
                    num_steps += roll_out.length()
                    horizon_left -= roll_out.length()

                    total_wp_success += wp_success
                    jwp += 1

                    if success:
                        hl_r_list.append(0)
                        hl_d_list.append(True)
                    else:
                        hl_r_list.append(-1)
                        hl_d_list.append(False)

                    batch.append(roll_out)

                    if oob:
                        break

                total_success += success
                j += 1

                if not self.optimistic_model:
                    x_temp, y_temp = self.vi.generate_dataset_flat(hl_s_list,
                                                                   hl_a_list,
                                                                   hl_s_new_list
                                                                   )

                    for bi in range(x_temp.shape[0]):
                        self.buffer.add(x_temp[bi], y_temp[bi])

                    self.vi.train_net(buffer=self.buffer, bs=128, opt_iterations=40)

            return batch, total_success / j, total_wp_success / jwp, num_steps / j, num_steps / num_roll_outs

        else:

            """ INITIALIZE THE ENVIRONMENT """
            s_init = self.grid.reset()
            s_start = self.grid.lls2hls(s_init['observation'])
            s_goal = self.grid.lls2hls(s_init['desired_goal'])
            self.episode_steps = 0

            """ V MAP """
            if self.optimistic_model:
                self.vi.update_p_table_optimistic(occupancy_map=self.grid.occupancy_map, walls=False)
            else:
                self.vi.update_p_table(occupancy_map=self.grid.occupancy_map, walls=True)

            v, pi = self.vi.run_vi(grid=self.grid, goal=(s_goal[0], s_goal[1], s_goal[2]))

            """ START THE EPISODE """
            horizon_left = self.max_iter
            success = False

            while (horizon_left > 0) and not success:

                # GET THE TARGET VECTOR
                s_bar = self.grid.lls2hls(s_init['observation'])
                a_bar = pi[s_bar[0], s_bar[1], s_bar[2]]

                self.vi.set_target(s_bar, a_bar)

                roll_out, states, actions, rewards, wp_success, success, l_state, s_bar_p, oob = self.roll_out_in_env(
                    horizon=self.time_scale,
                    start=s_init)

                s_init = l_state

                num_roll_outs += 1
                num_steps += roll_out.length()
                horizon_left -= roll_out.length()

                total_wp_success += wp_success
                jwp += 1

                if oob:
                    break

            return success

    def train(self):
        for iter_loop in range(self.inner_episodes):
            batch, g_success, wp_success, steps_trajectory, steps_wp = self.simulate_env(mode='train')
            self.optimizer.process_batch(self.policy, batch, [])
            print("G_S, WP_S, G_ST, WP_ST")
            print(g_success)
            print(wp_success)
            print(steps_trajectory)
            print(steps_wp)

    def test(self):
        g_success = self.simulate_env(mode='test')
        return g_success


import cv2
import copy

# mask no openCV

img = cv2.imread("original.jpeg")
img = cv2.resize(img, (300, 400))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.resize(gray, (300, 400))

final = copy.copy(img)

(row, col) = final.shape[0:2]

for i in range (row):
    for j in range (col):
        b = (final[i,j])[0]
        g = (final[i,j])[1]
        r = (final[i,j])[2]

        if(r > b > g):
              continue
        else:
            final[i,j] = sum(final[i,j]) * 0.33

cv2.imshow("BGR", img)
cv2.imshow("Gray", gray)
cv2.imshow("Final", final)

cv2.waitKey(0)
cv2.destroyAllWindows()


import sys, os, random
from tqdm import tqdm
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset
from config import DefaultConfig
from torch.utils.data import DataLoader, WeightedRandomSampler
from utils.utils import *

config = DefaultConfig()

# load the data
# token 标识resume 或者job
def load_sents(token):
    sents = {}
    sentnum = {}  # 用于标记已有句子数目
    tensor_size = [config.sent_num[token], config.word_num[token]]  # 每个resume or job 张量大小，维度
    filepath = os.path.join(config.train_path, '{}.sent.id'.format(token))
    time_print('\nloading from {}'.format(filepath))
    sys.stdout.flush()
    with open(filepath, 'r', encoding='utf-8') as file:
        for line in file:
            idx, sent = line.strip().split('\t')
            if idx not in sents:
                sents[idx] = torch.zeros(tensor_size).long()
                sentnum[idx] = 0
            if sentnum[idx] == config.sent_num[token]: continue  # 达到最大句子数，跳转继续读文件中的下一行
            sent = torch.LongTensor([int(x) for x in sent.split(' ')])
            sents[idx][sentnum[idx]] = F.pad(sent, (0, config.word_num[token] - len(sent)))
            sentnum[idx] += 1
    return sents

class PJFDataset(Dataset):
    def __init__(self, r_sents, j_sents, token):
        # token用于标注从train，valid，test文件读
        super(PJFDataset, self).__init__()
        self.r_sents = r_sents
        self.j_sents = j_sents
        self.pairs, self.labels = self.load_pairs(token)

    def load_pairs(self, token):
        pairs = []
        labels = []
        assert token in ['train', 'valid', 'test']
        filepath = os.path.join(config.splitdata_path, 'data.{}'.format(token))
        time_print('\nloading from {}'.format(filepath))
        sys.stdout.flush()
        with open(filepath, 'r', encoding='utf-8') as file:
            for line in file:
                resume_id, job_id, label = line.strip().split('\t')
                # 删除没有数据（sents）的pairs
                if resume_id not in self.r_sents or job_id not in self.j_sents: continue
                pairs.append([resume_id, job_id])
                labels.append(int(label))
        return pairs, torch.FloatTensor(labels)

    def getlabels(self):
        return self.labels

    def __getitem__(self, index):
        pair = self.pairs[index]
        resume_sent = self.r_sents[pair[0]]
        job_sent = self.j_sents[pair[1]]
        label = self.labels[index]
        return pair, resume_sent, job_sent, label

    def __len__(self):
        return len(self.pairs)

if __name__ == '__main__':
    resume_sents = load_sents('resume')
    job_sents = load_sents("job")
    train_dataset = PJFDataset(resume_sents, job_sents, 'train')

    train_loader = DataLoader(
        dataset=train_dataset,
        batch_size=config.batch_size['train'],
        shuffle=True,
        num_workers=config.num_workers['train']
    )
    for i, (geek_sent, job_sent, labels) in enumerate(train_loader):
        if i < 2:
            print(geek_sent.shape)
            #         print(emb(geek_sent).shape)
            print(job_sent.shape)
            print(len(labels))


from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse
from django.core.cache import cache

from posts.models import Group, Post

User = get_user_model()

class TestCache(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user = User.objects.create_user(username='test_author')
        cls.group = Group.objects.create(
            title='TestGroup1',
            slug='TestGroup1',
            description='TestGroup1'
        )
        cls.test_post = Post.objects.create(
            text='Тест №0',
            group=cls.group,
            author=cls.user
        )

    def setUp(self):
        self.user_1_client = Client()
        self.user_1_client.force_login(TestCache.user)

    def test_cache(self):
        """
        Проверим, что до очистки кэша контент сохраняется даже
        после принудительного удаления объекта из базы данных
        """
        response_1 = self.user_1_client.get(
            reverse(
                'posts:index'
            )
        )
        TestCache.test_post.delete()
        response_2 = self.user_1_client.get(
            reverse(
                'posts:index'
            )
        )
        self.assertEqual(response_1.content, response_2.content)
        cache.clear()
        response_3 = self.user_1_client.get(
            reverse(
                'posts:index'
            )
        )
        self.assertNotEqual(response_1.content, response_3.content)


# -*- coding: utf-8 -*-
import csv

fd = open('../JData_Product.csv')
info = csv.reader(fd)
headers = info.__next__()
# write = csv.writer(open('comment_feature.csv','w',newline=''))
fd2 = open('product_feature.csv','w',newline='')
write = csv.writer(fd2)
write.writerow(['sku_id','attr1','attr2','attr3','attr4','attr5','attr6','attr7','attr8','attr9','attr10'])

# 将特征值转换
for r in info:
    attr_first = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]][r[1] == '1' and 1 or r[1] == '2' and 2 or r[1] == '3' and 3]
    attr_second = [[1,0,0],[0,1,0],[0,0,1]][r[2] == '1' and 1 or r[2] == '2' and 2]
    attr_third = [[1,0,0],[0,1,0],[0,0,1]][r[3] == '1' and 1 or r[3] == '2' and 2]
    write.writerow([r[0]]+attr_first+attr_second+attr_third)


"""https://www.codewars.com/kata/56af1a20509ce5b9b000001e"""

def travel(r, zipcode):
    if zipcode == '':
        return ':/'
    places = [x.rstrip(zipcode).split() for x in r.split(',') if x.endswith(zipcode)]
    streets, numbers = [], []
    for pl in places:
        streets.append(' '.join(pl[1:]))
        numbers.append(pl[0])
    return zipcode + ':' + ','.join(streets) + '/' + ','.join(numbers)


from threading import Thread
from bs4 import BeautifulSoup as BS
import lxml
import time
import requests

def get_html(link):
    time.sleep(1)
    response = requests.get(link).text
    print(response)

links = ("https://github.com/dashboard", "https://pythontutor.ru/", "https://dzen.ru/?clid=2255080&from=dist_bookmark&win=480&yredirect=true", "https://relizua.com/", "https://travel.yandex.ru/avia/" )

threads = [Thread(target=get_html, args=(links[i],)) for i in range(5)]

start_1 = time.time()
for t in threads:
    t.start()
for t in threads:
    t.join()
end_1 = time.time()

start_2 = time.time()
for link in links:
    get_html(link)
end_2 = time.time()
print(f"Время выполнения параллельного запуска {end_1 - start_1} секунд")
print(f"Время выполнения последовательного запуска {end_2 - start_2} секунд")


'''
    File description: This file contains the implementation of an breadth first
    search algorithm, based on the Rush Hour puzzle. The goal of this algorithm
    is to find the shortest path to the solution possible.
'''

import grid
import time as clock
from collections import deque

def bfs(start):
    """This function starts an breadth first search algorithm finding the optimal
    solution for the rush hour board given to it.

    start: the initial rush hour board.

    Returns: a list representing the solution and the end time in seconds it
    took the algorithm to find the solution. If there is no solution found,
    it will alert the user this board has no solution.
    """
    came_from = dict()
    queue = deque()
    queue.appendleft(start)
    start_time = clock.clock()
    visited_nodes = 0
    cost = 0

    # Dequeue element from queue if there are elements left
    while len(queue) != 0:
        grid = queue.pop()
        visited_nodes += 1

        if grid.solved():
            # If sovled calculate time to solve the game
            end_time = clock.clock()
            time = end_time - start_time
            return came_from, grid, time, visited_nodes

        # If board is not solved, get every possible move from current board
        for move in grid.get_moves():
            came_from[move] = grid, cost
            queue.extendleft([move])

    time = 0
    return came_from, grid, time, visited_nodes


"""
ASCII Charts (Part 2: Bar Chart)
Given a dictionary containing quarterly sales values for a year, return a string representing a bar chart of the sales by quarter.

Quarter names (Q1, Q2, Q3, Q4) should appear on the left.
Quarters should be sorted in descending order by value.
Quarters with the same value should be shown in their yearly order (Q1 -> Q4).
Bars should begin with a "|".
Repeat the character "#" to fill the bar, with each character having a value of 50.
A single space comes after the bar, then the sales for that quarter.
If the value is 0, there should be no space after "|".
Use the newline character ("\n") to separate each bar in the chart.
Example #1
bar_chart({"Q4": 250, "Q1": 300, "Q2": 150, "Q3": 0})
➞ "Q1|###### 300\nQ4|##### 250\nQ2|### 150\nQ3|0"
When printed:

Q1|###### 300
Q4|##### 250
Q2|### 150
Q3|0
Example #2
bar_chart({"Q4": 500, "Q3": 100, "Q2": 100, "Q1": 150})
➞ "Q4|########## 500\nQ1|### 150\nQ2|## 100\nQ3|## 100"
When printed:

Q4|########## 500
Q1|### 150
Q2|## 100
Q3|## 100
"""
def bar_chart(results):

    a = list(sorted(sorted(results), key =results.get, reverse=True))
    b = list(sorted(results.values(), reverse=True))
    c= []
    for i in range(len(a)):
        if b[i] == 0:
            c.append(a[i]+"|"+"#"*(int(b[i]/50))+ str(b[i]))
        else:
            c.append(a[i]+"|"+"#"*(int(b[i]/50))+" "+ str(b[i]))
    return ("\n".join(c))

bar_chart({'Q4': 0, 'Q3': 100, 'Q2': 0, 'Q1': 600})

#bar_chart({"Q4": 250, "Q1": 300, "Q2": 150, "Q3": 0})


from time import sleep

class worker():

    def __init__(self,name,surname,department,salary):
        print("The init fuction of worker class")

        self.name = name
        self.surname = surname
        self.department = department
        self.salary = salary

    def show_info(self):
        print("""
        ===========================
        Information of Worker Class

        Name: {}
        Surname: {}
        Department: {}
        Salary: {}
        ============================
        """.format(self.name,self.surname,self.department,self.salary))

    def chg_department(self,new_department):
        print("Changing the department...")
        sleep(1)
        self.department = new_department

class manager(worker):

    def __init__(self,name,surname,department,salary,number_of_people):
        print("The init fuction of manager class")
        super().__init__(name,surname,department,salary)
        """
        ## Super KEY is used instaed of following methods.
        ## Super Key inherits the method from defined one.
        ## In this case, __init__ inherits from worker class
        self.name = name
        self.surname = surname
        self.department = department
        self.salary = salary
        """
        self.number_of_people = number_of_people

    def increase_salary(self,how_much_increase):
        print("The salary is being increased..")
        sleep(1)
        self.salary += how_much_increase

    def show_info(self):
        print("""
        ===========================
        Information of Manager Class

        Name: {}
        Surname: {}
        Department: {}
        Salary: {}
        Number of People: {}
        ============================
        """.format(self.name,self.surname,self.department,self.salary,self.number_of_people))

    def chg_people(self,how_many):
        print("The number of team is being changed..")
        sleep(1)
        self.number_of_people += how_many

worker1 = worker("Esin","KELES","Purchasing",3000)
manager1 = manager("Servet","INCE","DevOps",4800,24)
manager2 = manager("Arzu","BAL INCE","Finance",4000,12)

manager1.show_info()
worker1.show_info()
manager2.show_info()
manager1.chg_department("UNIX/LInux")
manager1.increase_salary(300)
manager2.increase_salary(450)
worker1.chg_department("HR")
manager2.chg_people(6)
manager1.show_info()
worker1.show_info()
manager2.show_info()

#print(dir(manager))


from sksurv.svm import FastSurvivalSVM
from sklearn_extensions.cached import CachedFitMixin

class CachedFastSurvivalSVM(CachedFitMixin, FastSurvivalSVM):
    def __init__(
        self,
        memory,
        alpha=1,
        rank_ratio=1.0,
        fit_intercept=False,
        max_iter=20,
        verbose=False,
        tol=None,
        optimizer=None,
        random_state=None,
        timeit=False,
    ):
        super().__init__(
            alpha=alpha,
            rank_ratio=rank_ratio,
            fit_intercept=fit_intercept,
            max_iter=max_iter,
            verbose=verbose,
            tol=tol,
            optimizer=optimizer,
            random_state=random_state,
            timeit=timeit,
        )
        self.memory = memory


from torchvision.models import vgg16
from .base_model import _BaseModel
from torch import nn

class VGG16(_BaseModel):
    def __init__(self, nClass, pretrain=True):
        super(VGG16, self).__init__(nClass, pretrain)
        original_mod = vgg16(pretrain)

        # change input_num of vgg16
        in_kernel2 = original_mod.features[2].in_channels
        original_mod.features[0] = nn.Conv2d(in_channels=1, out_channels=in_kernel2, kernel_size = 3)

        self.encoder = nn.Sequential(original_mod.features, original_mod.avgpool)

        self.classify = nn.Sequential(
            nn.Linear(25088, 4096),
            nn.ReLU(),
            nn.Dropout(0.5),
            nn.Linear(4096, nClass),
            nn.Sigmoid()
        )


import os
import pickle
from pathlib import Path

import numpy as np

import Main

database = {}
ids_names = {}

def evaluate_all():
    """
    Evaluates all query files and prints info
    :return: None
    """
    global database, ids_names
    # Load database from file
    db_file = open("fingerprints" + os.path.sep + "db_S.pkl", "rb")
    ids_names_file = open("fingerprints" + os.path.sep + "ids_names.pkl", "rb")
    database = pickle.load(db_file)
    ids_names = pickle.load(ids_names_file)

    # Path to wavs
    wav_path = "data" + os.path.sep + "query_recordings"

    wavs = Path(wav_path).rglob("*.wav")
    number_of_query_files = sum(1 for x in wavs)

    f_maxs = []
    all_f_measures = []
    all_precisions = []
    all_recalls = []
    map = 0
    counter = 0
    for wav in Path(wav_path).rglob("*.wav"):
        print("Evaluating " + str(counter) + " of " + str(number_of_query_files))
        precision, recall, f_measures, f_max, p_avg = evaluate(str(wav).split(os.path.sep)[-1])

        all_precisions.append(precision)
        all_recalls.append(recall)
        f_maxs.append(f_max)
        all_f_measures.append(f_measures)
        map += p_avg
        counter += 1

    avg_all_f = np.mean(all_f_measures, axis=0)
    f_max_avg = np.mean(f_maxs)
    map /= number_of_query_files

    print("Average precision for first three ranks : " + np.array2string(np.mean(all_precisions, axis=0), separator=", "))
    print("Average recall for first three ranks : " + np.array2string(np.mean(all_recalls, axis=0), separator=", "))
    print("Average F-measue values for the first three ranks: " + np.array2string(avg_all_f, separator=", "))
    print("Average maximum F-measure: " + str(f_max_avg))
    print("Mean Average Precision: " + str(map))

def evaluate(query_path):
    """
    Evaluate a given *.wav file
    :param query_path: The path to the *.wav file
    :return: Precision, recall and f_measures for the first three ranks, the maximum f-measure and the average precision
    """
    # Get matching results
    best_three, is_correct = Main.analyse(database, ids_names, str(query_path))

    # Get ground truth song ID
    song_name = query_path.split(os.path.sep)[-1].split('-')[0] + ".wav"

    # Get ground truth best ten
    best_ten_gt, is_correct = Main.analyse(database, ids_names, song_name)

    relevant = np.zeros(len(best_three))
    for i in range(len(best_three)):
        if best_three[i] in best_ten_gt:
            relevant[i] = 1

    precision = np.zeros(len(best_three))
    recall = np.zeros(len(best_three))

    for i in range(1, len(best_three) + 1):
        sum = np.sum(relevant[:i])
        precision[i - 1] = (1 / i) * sum
        recall[i - 1] = sum

    # Calculate F-measure
    f_measures = []
    for i in range(len(best_three)):
        if precision[i] == 0 and recall[i] == 0:
            f_measures.append(0)
        else:
            f_measures.append((2 * precision[i] * recall[i]) / (precision[i] + recall[i]))

    # Calculate maximum f measure
    f_max = np.max(f_measures)

    # Calculate average precision
    p_avg = np.sum(precision * relevant) / 1

    return precision, recall, f_measures, f_max, p_avg

# Evaluate
# relevance("pop.00050-snippet-10-0.wav")
# evaluate_all()


#!/usr/bin/python
from glob import glob
from os.path import basename, join
from pyrosetta import *
from pyrosetta.rosetta.protocols.relax import FastRelax

init()

sf = create_score_function('ref2015_cst')

fr = FastRelax()
fr.set_scorefxn(sf)
fr.constrain_coords(True)
fr.constrain_relax_to_start_coords(True)
fr.coord_constrain_sidechains(True)

structs = glob('pdz_nmr/*.pdb')
structs.sort()

for s in structs:
	pose = pose_from_pdb(s)

	jdname = join('pdz_relax/', basename(s).rstrip('.pdb'))

	jd = PyJobDistributor(jdname, 20, sf)
	while not jd.job_complete:
		pp = Pose()
		pp.assign(pose)
		fr.apply(pp)
		jd.output_decoy(pp)


# -*- encoding:utf-8 -*-
# __author__=='Gan'
import time

class Solution(object):
    def matrixChain(self, nums, n):
        for num in nums:
            if num <= 0:
                return None
        start = time.time()
        product_value_list, product_pos_list = [[[0 for _ in range(n)] for _ in range(n)] for _ in range(2)]
        for r in range(2, n):
            for i in range(1, n - r + 1):
                j = i + r - 1
                product_value_list[i][j] = product_value_list[i+1][j] + nums[i-1] * nums[i] * nums[j]
                product_pos_list[i][j] = i

                for k in range(i+1, j):
                    t = product_value_list[i][k] + product_value_list[k+1][j] + nums[i-1] * nums[k] * nums[j]
                    if t < product_value_list[i][j]:
                        product_value_list[i][j] = t
                        product_pos_list[i][j] = k

        def printf(list_):
            for r in range(1, n):
                print('r = {}  '.format(r),
                       ['%8d' % list_[i][i + r - 1] for i in range(1, n - r + 1)])
        print('Matrix Production cost: {}'.format(time.time() - start))

        def gen_path(product_pos_list, start, end, path):
            if start == end:
                path += "A" + str(start)
            elif start + 1 == end:
                path += "(A" + str(start) + '*' +"A" + str(end) + ")"
            else:
                path += '(' + gen_path(product_pos_list, start, product_pos_list[start][end], path) +\
                        gen_path(product_pos_list, product_pos_list[start][end] + 1, end, path) + ')'
            return path

        path = gen_path(product_pos_list, 1, len(nums)-1, '')
        print('The Value of the memo:')
        printf(product_value_list)
        print('The Position of the memo:')
        printf(product_pos_list)
        return path, product_value_list[1][n-1]

if __name__ == '__main__':
    import random
    rand_list = [random.randint(1, 100) for x in range(10)]
    path, res = Solution().matrixChain(rand_list, len(rand_list))
    if res:
        print('The least product times with {} is {}:'.format(path, res))
    else:
        print('Elements must be positive integer.\nPlease input again!')


import gym
from gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions
from gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL
import matplotlib.pyplot as plt
import pandas as pd
from stocks_env import StocksEnv

# env = gym.make('forex-v0', frame_bound=(50, 100), window_size=10)
# env = gym.make('stocks-v0', frame_bound=(50, 100), window_size=10)
env = StocksEnv(df=pd.read_csv('STOCKS_GOOGL.csv'),frame_bound=(50, 100), window_size=10)
print("==========env information:========")
print("> shape:", env.shape)
print("> df.shape:", env.df.shape)
print("> prices.shape:", env.prices.shape)
print("> signal_features.shape:", env.signal_features.shape)
print("> max_possible_profit:", env.max_possible_profit())
print(env.action_space.sample())

observation = env.reset()
while True:
    action = env.action_space.sample()   # Short=0 and Long=1

    # action =-1  # Short=0 and Long=1

    # print('action:', action)
    observation, reward, done, info = env.step(action)
    print('steps:',action, reward)
    # env.render()
    if done:
        print("info:", info)
        break

plt.cla()
env.render_all()
plt.show()


#importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import itertools
import math

#importing dataset
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, 1:4].values
Y = dataset.iloc[:, 4].values

#Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
#dummy encoding (making seprate columns for seprate categories )
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()

#Splitting the dataset into training set and Test set
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X, Y, test_size = 0.1, random_state = 0)

#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test) #here we transformed X_test by fitting sc_X to X_train so that both have same scaling

#Logistic regression using gradient descent

theta = [0,0,0,0] #values of coffiecient of independent variable

#Making X[i][0] = 1 so that we get genral form like h = theta0+ theta1*x + theta2*x ... after multiplying theta and X_train matrices
i = 0
j = 0
while(i<360):
    X_train[i][j] = 1
    if i<40:
        X_test[i][j] = 1
    i += 1

#function to return  derivative of cost function that will be subtracted form each theta respectevely.
def averageCost(temp):
    total = 0
    for (i,j) in zip(X_train,Y_train):
        y_pred = h_theta(i) #calculating value of predicted y by signoid or logistic function
        total = total + (y_pred - j)*i[temp]
    return total/len(X_train)

#calculating derivative of cost function for each theta
cost = [0,0,0,0]

def updateCostMatrix():
    s = 0 #it denotes index of respective theta
    while s<len(theta):
        cost[s] = averageCost(s)
        s += 1

def updateTheta():
    i = 0
    while(i<len(theta)):
        theta[i] = theta[i] - 0.1*cost[i]
        i += 1

def h_theta(i):
    mul = np.matmul(theta,i)
    if(mul<0):
            mul = 1 - 1/(1+math.exp(mul))
    else:
        mul = 1/(1+math.exp(-mul))
    return(mul)

#Here loop stops when cost becomes approximately to 0
#This loop is gradient descent algorithm
while(True):
    count = 0 #it is used to check that all cost values are zeroes
    updateCostMatrix()
    updateTheta()
    #print(cost)
    for i in cost:
        if(i < 0.5 and i>-0.5):
            count += 1

    if(count == 4):  #4 is no of features in X_train
        break

#Calculating error of predicted values and also storing predicted values in temp
error = 0 #initialising it.
temp = []
for i,j in zip(X_test,Y_test):
    y_pred = h_theta(i)
    if(y_pred>=0.5):
        y_pred  = 1
    else:
        y_pred = 0
    temp.append(y_pred)
    if(y_pred != j):
        error = error + 1
error = error*100/40
print("Error is:",error,"%")


import numpy as np

def calculate_moving_average(data, period, type='simple'):
    if type == 'simple':
        return data.rolling(window=period).mean()
    elif type == 'weighted':
        weights = np.arange(1, period + 1)
        return data.rolling(window=period).apply(lambda x: np.dot(x, weights) / weights.sum(), raw=True)
    elif type == 'ema':
        return data.ewm(span=period, adjust=False).mean()
    else:
        raise ValueError("Invalid moving average type. Supported types are 'simple', 'weighted', and 'ema'.")

def calculate_kama(price_series, n=10, pow1=2, pow2=30):
    '''Calculate the Kaufman Adaptive Moving Average(KAMA) for a given series
    Parameters:
        price_series : pandas series, timeseries of price
        n            : integer, lookback period
        pow1         : integer, fastest limit of exponential moving average window
        pow2         : integer, slowest limit of exponential moving average window
    Return:
        pandas series of KAMA indicator
    '''
    # Calculate the absolute price change (daily)
    delta = abs(price_series - price_series.shift())

    # Calculate the efficiency ratio = delta(n)/sum(n)
    efficiency_ratio = abs(price_series - price_series.shift(n)) / delta.rolling(n).sum()

    # Calculate the smoothing constant
    smoothing_constant = (efficiency_ratio * (2 / (pow1 + 1) - 2 / (pow2 + 1)) + 2 / (pow2 + 1)) ** 2

    # Calculate the KAMA
    kama = np.zeros_like(price_series)
    kama[:n] = price_series[:n]

    for i in range(n, len(price_series)):
        kama[i] = kama[i - 1] + smoothing_constant[i] * (price_series[i] - kama[i - 1])

    return pd.Series(kama, index=price_series.index)


from collections import defaultdict

# Bull and cows game, count match and unmatch char
# https://leetcode.com/problems/bulls-and-cows/
# Preprocess secret if guess match, increase A, else increase char
# loop through remain guess, if char in A, decrease char
# Optmize when encounter number in secrect, increase count, number in guess
# decrease count, if 2 number same => increase bull, if different, if count(secret)
# < 0, increase cows (as guess decrease it), if count(guess) > 0, increase cows
# (as secret increase it)
class BullCowSolution:
    def getHint(self, secret, guess):

        table = defaultdict(int)
        A = B = 0
        p = []
        for idx in range(len(guess)):
            if secret[idx] == guess[idx]:
                A += 1
            else:
                table[secret[idx]] += 1
                p.append(idx)

        for idx in p:
            if guess[idx] in table and table[guess[idx]] > 0:
                table[guess[idx]] -= 1
                B += 1

        return '{}A{}B'.format(A, B)

    def getHint2(self, secret, guess):
        count = [0 for i in range(10)]
        bull = cow = 0
        for idx in range(len(guess)):
            s = int(secret[idx])
            g = int(secret[idx])
            if s == g:
                bull += 1
            else:
                if count[s] < 0:
                    cow += 1
                if count[g] > 0:
                    cow += 1

                count[s] += 1
                count[g] -= 1
        return '{}A{}B'.format(bull, cow)


# TE-DPV calibration
import pandas as pd
import genetic_algorithm as ga
import os

while True:
    while True:
        file = input("Data set to import: ")
        try:
            data_set = pd.read_csv(file)
            break
        except Exception:
            try:
                data_set = pd.read_csv(file + ".csv")
                break
            except Exception:
                print("No file found.")
    print(data_set.Viscosity.nunique(dropna=True))
    if (len(data_set.index) < 3):
        print("At least three samples runs are required.")
    elif (data_set.Viscosity.nunique(dropna=True) < 2):
        print("Measurements of more than one viscosity are required.")
    else:
        break
os.system('cls')
data_set.insert(0, 'Calibrate', True)
data_set['Torque_binary'] = [int(j, 16) for j in list(data_set.Torque)]
data_set['Calculated Viscosity'] = 0
data_set['% Error'] = 0
data_set['Include'] = True
i = list(data_set.index.values)
x = data_set.Viscosity
y = data_set.Torque_binary
z = 0

def myfunc(x):
    return slope*x+intercept

while True:
    working_set = ga.build_ws(data_set)

    if (len(working_set.index) > 2):
        slope, intercept, r, p, std_err, calc_visc, reading_error, tss,\
            avg_err = ga.calculations(data_set, working_set)
        data_set['Calculated Viscosity'] = calc_visc
        data_set['% Error'] = reading_error
        print(data_set)
        print()
        print("y = {} x + {}".format(slope, intercept))
        print("r2 = {}".format(r*r))
        print("Standard Error (working data set) = {}".format(std_err))
        print("Total sum of the squares = {}".format(tss))
        print("Average error (complete data set) = {} ".format(avg_err))

    else:
        print("Unable to calculate. At least three samples are required.\n")
        data_set['Calculated Viscosity'] = abs(calc_visc * 0)
        data_set['% Error'] = abs(reading_error * 0)
        print(data_set)
        print()

    c = input("""
    Index number to toggle.
    A/a to run automatic selection (Calibrate & Include reset).
    B/b run automatic selection (no reset).
    C/c Select by viscosity constraints (no reset).
    I/i Select by index constraints (no reset).
    M/m print merged plot.
    P/p print new plot.
    R/r to reset.
    Q/q to quit.
    """)
    if c == "":
        os.system('cls')
    elif c in ('A', 'a'):
        data_set["Calibrate"] = True
        data_set['Include'] = True
        working_set = ga.build_ws(data_set)
        slope, intercept, r, p, std_err, calc_visc, reading_error, tss,\
            avg_err = ga.calculations(data_set, working_set)
        ga.genetic_algorithm(data_set, tss)
        os.system('cls')
    elif c in ('B', 'b'):
        working_set = ga.build_ws(data_set)
        slope, intercept, r, p, std_err, calc_visc, reading_error, tss,\
            avg_err = ga.calculations(data_set, working_set)
        ga.genetic_algorithm(data_set, tss)
        os.system('cls')
    elif c in ('C', 'c'):
        ll = input("Lower Viscosity Limit: ")
        if ll == '':
            ll = 0
        ul = input("Upper Viscosity limit: ")
        if ul == '':
            ul = 5000
        data_set = ga.constrain(data_set, int(ll), int(ul))
        os.system('cls')
    elif c in ('I', 'i'):
        ll = input("Lower Index Limit: ")
        if ll == '':
            ll = 0
        ul = input("Upper Index limit: ")
        if ul == '':
            ul = len(data_set.index) - 1
        data_set = ga.iconstrain(data_set, int(ll), int(ul))
        os.system('cls')
    elif c in ('M', 'm'):
        if z == 0:
            z = 1
        ga.display_plot(x, y, myfunc, z, slope, intercept, r)
        os.system('cls')
    elif c in ('P', 'p'):
        z = z + 1
        ga.display_plot(x, y, myfunc, z, slope, intercept, r)
#        os.system('cls')
    elif c in ('R', 'r'):
        while True:
            b = input("""
            (1) Reset Calibrate Flag
            (2) Reset Include Flag
            (3) Reset Both
            """)
            if b == "":
                b = 3
            if int(b) == 1:
                data_set['Calibrate'] = True
                break
            elif int(b) == 2:
                data_set['Include'] = True
                break
            else:
                data_set['Calibrate'] = True
                data_set['Include'] = True
                break
        os.system('cls')
    elif c in ('Q', 'q'):
        break
    elif c in str(i):
        while True:
            b = input("""
            (1) Toggle Calibrate Flag
            (2) Toggle Include Flag
            (3) Toggle Both
            """)
            if int(b) == 1:
                data_set.at[int(c), 'Calibrate'] ^= True
                break
            elif int(b) == 2:
                data_set.at[int(c), 'Include'] ^= True
                break
            elif int(b) == 3:
                data_set.at[int(c), 'Calibrate'] ^= True
                data_set.at[int(c), 'Include'] ^= True
                break
        os.system('cls')
    else:
        os.system('cls')


import cv2
import math

# Read video
cap = cv2.VideoCapture("parking_video.mp4")

# Location of entrance
ent = [(963, 13), (995, 31)]

# db to store cares
fake_db = {}

# lists for new detected objects
new_objects = []

# Number of the cars
car_num = 0

# Object detector for thrasholding
object_detector = cv2.createBackgroundSubtractorMOG2(history=150, varThreshold=20)

# Car Object, includes number plate, enter time, exit time, path
class Car:
    def __init__(self, id,cx,cy):
        self.id_count = id
        self.enter_time = None
        self.exit_time = None
        self.path = [(cx,cy)]

# Update the car position in the entrance
def update(objects):
    global car_num
    for obj in objects:
        x,y,w,h,cx,cy = obj
        same_object_detected = False
        for key in fake_db.keys():
            car = fake_db[key]
            # distance between cars and new detected object
            dist = math.hypot(cx - car.path[-1][0],cy - car.path[-1][1])
            # if distance < 10it is the same object
            if dist < 10:
                # append the new location to the path
                fake_db[key].path.append((cx,cy))
                same_object_detected = True
                break
        # else make a new object in fake_db
        if same_object_detected is False:
            fake_db[car_num] = Car(car_num,cx,cy)
            car_num += 1

# check if the object is in the fields
def is_in(cx,cy,fields):
    flag = False
    for field in fields:
        if field[0] <= cx <= field[1] and field[2] <= cy <= field[3]: flag = True
        if flag: break
    return flag

while True:
    # Read frame from video
    ret, frame = cap.read()

    # Thresholding
    mask = object_detector.apply(frame)
    _, frameThres = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY_INV)

    entrance = frameThres[13:31, 963:995]

    # Draw a rectangle and write number of pixels
    cv2.rectangle(frame, ent[0], ent[1], (255, 0, 0), 1)

    # Extract contours
    contours, _ = cv2.findContours(frameThres, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # Make fields, where already existing cars are located
    fields = []
    for key in fake_db.keys():
        car = fake_db[key]
        cx, cy = car.path[-1]
        fields.append([cx - 30, cx + 30, cy - 30, cy + 30])

    # Tracking the objects
    for cnt in contours:
        # if there are enough contours
        if cv2.contourArea(cnt) > 200:
            # make a rectangle, x,y,width,height
            x, y, w, h = cv2.boundingRect(cnt)
            # get the center coordinate of the rectangle
            cx = (x + x + w) // 2
            cy = (y + y + h) // 2
            # check if the center coordinate is in the fields
            # i.e. if the new detected rectangle is an already existing car
            if is_in(cx, cy, fields):
                # if the object was an already existing car, search which car is it.
                for key in fake_db.keys():
                    car = fake_db[key]
                    dist = math.hypot(cx - car.path[-1][0], cy - car.path[-1][1])
                    # if distance < 20, it is the same object
                    if dist < 20:
                        fake_db[key].path.append((cx, cy))
                        break
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
    fields = []

    # Check if there is a new object in entrance
    for cnt in contours:
        if cv2.contourArea(cnt) > 200:
            x, y, w, h = cv2.boundingRect(cnt)
            cx = (x + x + w) // 2
            cy = (y + y + h) // 2
            if 963 <= cx <= 995 and 13 <= cy <= 31:
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
                new_objects.append([x, y, w, h, cx, cy])

    # Filter the new objects
    update(new_objects)

    # Make the new_object empty
    new_object = []

    # Show the frames
    cv2.imshow("Frame", frame)
    cv2.imshow("imgThres", frameThres)
    cv2.imshow("entrance",entrance)

    key = cv2.waitKey(50)
    if key == ord('r'):
        pass


from utils import filter_predicted_growth_subset, filter_predicted_stable_subset, grid_search
from typing import List
from cfmodel import CfModel
from lightgbm import LGBMClassifier
import pandas as pd
class LGBM(CfModel):
    """Create a LGBM model object
    """

    def __init__(self, l90d: pd.DataFrame, n180d: pd.DataFrame, growth_data: pd.DataFrame, stable_data: pd.DataFrame,
                 features: List[str], subset_type: str):
        super().__init__(l90d, n180d, growth_data, stable_data, features, subset_type)
        self.subset_type = subset_type
        self.get_x_train_y_train()
        self.model, self.y_pred = self.train()
        self.pred, self.true = self.get_subset_pred_true()
        self.filtered_pred = filter_predicted_growth_subset(self.pred) if self.subset_type == 'growth' else \
            filter_predicted_stable_subset(self.pred)

        print('LGBM Model has finished training')
    def train(self) -> (LGBMClassifier, pd.DataFrame):
        parameters = {
            "max_depth": [10],
            "num_leaves": [20],
            "learning_rate": [0.05],
            "n_estimators": [1000],
        }

        model = LGBMClassifier(
            random_state=0,
        )
        model.set_params(verbose=0)
        model, y_pred = grid_search(parameters, model, self.X_train, self.y_train, self.X_test)
        return model, y_pred


import json
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import pandas as pd
#import seaborn as sns
import global_vars

COMPUTED_DIR = global_vars.COMPUTED_FOLLOWING + "/"
SPREADSHEET = global_vars.INPUT_GG_DATA_CSV

def heatmap(data, names = None):
    plt.pcolormesh(data)
    plt.colorbar()
    if names is not None:
        plt.xticks(np.arange(data.shape[0]) + 0.5,
                   names, rotation = "vertical")
        plt.yticks(np.arange(data.shape[0]) + 0.5,
                   names, rotation = "horizontal")
    plt.tight_layout()
    plt.show()

with open(COMPUTED_DIR + "names.json") as f:
    names = json.load(f)
    names = [n.split(".")[0] for n in names]

dot_products = np.genfromtxt(COMPUTED_DIR + "following_dot_products_normalized.csv", delimiter=",")

lower = 0.1
upper = 0.9
similar = np.nonzero(np.logical_and(dot_products > lower,
                                    dot_products < upper))
similar_values = dot_products[similar]
sorted_indices = np.argsort(similar_values)[::-1]
## need to filter out half of them due to
## the symmetry of the matrix
similar = [similar[0][sorted_indices], similar[1][sorted_indices]]
similar_values = similar_values[sorted_indices]

pairs = [(names[similar[0][i]], names[similar[1][i]], similar_values[i])
         for i in range(len(similar[0]))]

for pair in pairs:
    print(pair)

def plot_pairs(num = 10):
    n = len(similar[0])
    plot_names = [names[similar[0][i]] + ", " + names[similar[1][i]]
                  for i in range(n)][::2]
    plot_values = [similar_values[i] for i in range(n)][::2]
    plt.barh(range(len(plot_names[0:num])), plot_values[0:num][::-1],
             tick_label = plot_names[0:num][::-1], align = "center")
    plt.xlabel("Similarity")
    plt.tight_layout()
    plt.show()

nominees = pd.read_csv(SPREADSHEET)

def subset_indices(condition, nominees, names):
    """Find the indices of users based on condition.
    """

    screen_names = nominees[condition].TWITTER_SCREEN_NAME
    screen_names = screen_names[screen_names.isin(names)]
    indices = []
    for s in screen_names:
        indices.append(names.index(s))

    return screen_names, indices

cond = nominees.CATEGORY.str.contains("Best TV Series")
subset_names, indices = subset_indices(cond, nominees, names)
subset_dp = dot_products[indices][:, indices]

#user_by_following = np.genfromtxt(COMPUTED_DIR + "user_by_following.csv", delimiter = ",")

rowsums = np.sum(user_by_following, axis = 1)[indices]
sorted_indices = np.argsort(rowsums) # [::-1]
D = pd.DataFrame({"name": np.array(nominees.NOMINEE[cond])[sorted_indices],
                 "count": rowsums[sorted_indices].astype(int),
                  "winner": np.array(nominees.WINNER_FLAG[cond])[sorted_indices]})

plt.legend(handles=[mpatches.Patch(color="blue", label="Loser"),
                    mpatches.Patch(color="green", label="Winner")],
           loc = "center right")
plt.barh(range(len(rowsums)), D["count"],
            tick_label = D["name"],
            color = np.array(["blue", "green"])[D["winner"]],
            align = "center")
plt.show()

plt.hist(dot_products, bins = 30)
plt.xlabel("Similarity")
plt.ylabel("Frequency")
plt.show()

# ax = sns.barplot(y = np.array(nominees.NOMINEE[cond])[sorted_indices],
#                 x = rowsums[sorted_indices],
#                 hue = np.array(nominees.WINNER_FLAG[cond])[sorted_indices])

#ax = sns.barplot(x = D["count"], y = D["name"], hue = np.array()[D["winner"]])
#ax = sns.barplot(x = "count", y = "name", hue = "winner", data = D)
#sns.plt.show()

## TODO: add coloring by winner to above plot

#from ggplot import *

#ggplot(aes(x = "name",


#!/usr/bin/python

import matplotlib
matplotlib.use('cairo')
import matplotlib.pyplot as plt
import h5py as h5
import loadct as ct
import argparse
from matplotlib.patches import Rectangle, ConnectionPatch
from yt.mods import \
    load, parallel_objects
import numpy as np
from mpi4py import MPI

matplotlib.rc("axes", linewidth=1.5, labelsize="large")
matplotlib.rc("lines", markeredgewidth=1.5)
matplotlib.rc("font", size=20)

def read_data_hdf5(name,dset):
   temp = {}
   h5f = h5.File(name)
   temp[dset] = h5f[dset][:,0,:]
   for f in h5f.attrs.keys():
      temp[f] = h5f.attrs[f]
   h5f.close()
   return temp

def make_ticklabels_invisible(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
        for tl in ax.get_xticklabels() + ax.get_yticklabels():
            tl.set_visible(False)

parser = argparse.ArgumentParser()
parser.add_argument("-x", nargs=2, default=[4.2, 5.55])
parser.add_argument("-z", nargs=2, default=[0.0, 0.0])
parser.add_argument("files", nargs='*')

args = parser.parse_args()
xc = map(np.float64,args.x)
zc = map(np.float64,args.z)

my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', ct.p05)

h  = 0.146484375
phi = 0.5

c1 = np.array([xc[0], phi, zc[0]])
c2 = np.array([xc[1], phi, zc[1]])
patch1 = [c1[0] - h, c1[0] + h, c1[2]-h, c1[2]+h]
patch2 = [c2[0] - h, c2[0] + h, c2[2]-h, c2[2]+h]
vmin = 0.0
vmax = 0.1 * 0.2
first_pass = True
for fn in parallel_objects(args.files, njobs=-1):
   pf = load(fn)
   field = "dend"
   le = pf.domain_left_edge * pf['au']
   re = pf.domain_right_edge * pf['au']
   s = pf.h.slice(1, phi, fields=["dend"])
   # = pf.h.proj(1, 'dend')
   #fac = pf['au'] / (2.0 * pf['dend'] * np.pi)
   fac = 1./pf['dend']
   if first_pass:
      c1 /= pf.units['au']
      c2 /= pf.units['au']

   ext = [ le[0], re[0], le[2], re[2] ]
   fig = plt.figure(0, figsize=(14,10))
   fig.clf()
   ax1 = plt.subplot2grid((4,8), (0,0), colspan=8)
   ax1.axis([3.0, 6.0, le[2], re[2]])

   img = s.to_frb(3/pf['au'], (int(0.29296875*1024), 3*1024), center=np.array([4.5, phi, 0.0])/pf.units['au'],
                  height=2.*h/pf['au'])
   ax1.imshow(img['dend'] * fac, extent=[3.0, 6.0, le[2], re[2]], cmap=my_cmap,
         vmin=vmin, vmax=vmax)
   for f in [ patch1, patch2 ]:
      xy = (f[0], f[2])
      rect = Rectangle(xy, f[1]-f[0] , f[3]-f[2], facecolor="#aaaaaa", alpha=0.85)
      ax1.add_patch(rect)
   ax1.set_xlabel("R [AU]")
   ax1.set_ylabel("z [AU]")

   ax2 = plt.subplot2grid((4,8), (1,0), colspan=4, rowspan=3)
   ax2.axis(patch1)
   img = s.to_frb(2*h/pf['au'], (512,512), center=c1)
   ax2.imshow(img['dend'] * fac, cmap=my_cmap, extent=patch1,
         vmin=vmin, vmax=vmax, interpolation="nearest")
   ax2.set_aspect(1.0)
   ax2.set_xlabel("R [AU]")
   ax2.set_ylabel("z [AU]")

   ax3 = plt.subplot2grid((4,8), (1,4), colspan=4, rowspan=3)
   ax3.axis(patch2)
   ax3.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator(4))
   img = s.to_frb(2*h/pf['au'], (512,512),center=c2)
   ax3.imshow(img['dend'] * fac, extent=patch2, cmap=my_cmap,
         vmin=vmin, vmax=vmax, interpolation="nearest")
   ax3.set_aspect(1.0)
   ax3.set_xlabel("R [AU]")
   ax3.tick_params(axis="y",labelright=True, labelleft=False)

   xy = [ [(patch1[0],patch1[3]), (patch1[0],patch1[2])],
          [(patch1[1],patch1[3]), (patch1[1],patch1[2])] ]
   for f in xy:
      arrow = ConnectionPatch( xyA=f[0], xyB=f[1],
              axesA=ax2, axesB=ax1, coordsA="data", coordsB="data",
              arrowstyle="<-", shrinkA=3.0, shrinkB=3.0)
      ax2.add_artist(arrow)

   xy = [ [(patch2[0],patch2[3]), (patch2[0],patch2[2])],
          [(patch2[1],patch2[3]), (patch2[1],patch2[2])] ]
   for f in xy:
      arrow = ConnectionPatch( xyA=f[0], xyB=f[1],
              axesA=ax3, axesB=ax1, coordsA="data", coordsB="data",
              arrowstyle="<-", shrinkA=3.0, shrinkB=3.0)
      ax3.add_artist(arrow)

   fig.suptitle("Time = %4.0i [yr]" % (pf.current_time / (3600.*24.*365.25)))
   plt.draw()
   plt.savefig(fn.replace('.h5','_zoom.png'))
   print("%s written"%fn.replace('.h5','_zoom.png'))
   del ax1, ax2, ax3, fig, s, pf, img
   first_pass = False


#!/usr/bin/env python
# GIMP Python plug-in to make mirrors, hemitropes, and quadrupoles

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License at http://www.gnu.org/licenses for
# more details.

from gimpfu import *
import process

def mirror(img, layer, m_horiz, m_vert, ul_hflip, ul_vflip):
    with process.UndoContext(img):
        process.mirror(img, layer, m_horiz, m_vert, ul_hflip, ul_vflip)

process.pfreg(mirror,
              [
                  (PF_IMAGE, "image", "Input image", None),
                  (PF_DRAWABLE, "drawable", "Input drawable", None),
                  (PF_BOOL, "m_horiz", "Horizontal mirror", True),
                  (PF_BOOL, "m_vert", "Vertical mirror", True),
                  (PF_BOOL, "ul_hflip", "Horiz flip upper-left", False),
                  (PF_BOOL, "ul_vflip", "Vert flip upper left", False),
              ],
              name="Mirror",
              description="Reflect an image (possibly multiple times)",
              author="theilr",
              year=2022,
              menu="<Image>/Filters/theilr"
)

main()


#! /usr/local/bin/python

import heapq
import random

class Singleton(type):
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton,
                                        cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class LargestTracker(object):
    __metaclass__ = Singleton
    _entries = []

    @staticmethod
    def getInstance():
        """
        guarantees the creation of a single instance across the virtual
        machine. assumed to be called very frequently.

        @return an instance of largesttracker
        """
        return LargestTracker()

    def getNLargest(self, numberOfTopLargestElements):
        """
        Returns a list in O(n log m) time OR BETTER where n is the number of
        entries added to LargestTracker and m is numberOfTopLargestElements.
        Duplicates are allowed

        @param numberOfTopLargestElements
                    the number of top-most-elements to return
        @return the top-most-elements in the tracker sorted in ascending order
                    as a list of integers
        """
        # Docs say for 1 use max()
        if numberOfTopLargestElements == 1:
            return max(self._entries)
        # Docs say heapq.nlargest() only beats sort() for small number of N
        elif numberOfTopLargestElements < 1000:
            return heapq.nlargest(numberOfTopLargestElements,
                                  self._entries)[::-1]
        # Use sort() for large numbers of elements
        else:
            return sorted(self._entries)[-numberOfTopLargestElements:]

    def add(self, anEntry):
        """
        Adds an entry to the tracker. This method must operate in O(log n) time
        OR BETTER.

        @param anEntry
                   the entry to add to the tracker. Entries need not be unique.
        """
        heapq.heappush(self._entries, anEntry)

    def clear(self):
        """
        Removes all the entries from the tracker. This should return in
        constant time.
        """
        del self._entries[:]

def test():
    largeNumber = 100000

    # Test singleton
    tracker = LargestTracker()
    new_tracker = tracker.getInstance()
    assert tracker == new_tracker

    # Test adding elements
    for i in xrange(0, largeNumber):
        elem = random.randint(0, largeNumber)
        tracker.add(elem)
    assert len(tracker._entries) == largeNumber

    # Test getting largest elements
    largest = tracker.getNLargest(1)
    largest = tracker.getNLargest(50)
    assert len(largest) == 50
    assert largest[0] < largest[49]
    largest = tracker.getNLargest(1000)

    # Test clear
    tracker.clear()
    assert len(tracker._entries) == 0

if __name__ == "__main__":
    test()


import sys

class Node:
    def _init_(self):
        self.prev = None

        self.data = input("Enter data of the new node")
        self.next = None

class DoublyList:
    def _init_(self,first,last):
        self.first = None
        self.last = None
        print("An empty doubly LL is created")

    def insertFront(self):
        nn = Node() # create a new node object
        if self.first == None:  # if list is empty
            self.first = self.last = nn
            return
        self.first.prev = nn  # make the 1st node in list point to new node
        nn.next = self.first  # make the new node point to 1st node in list
        self.first = nn  # make the new node as 1st node

    def insertRear(self):
        nn = Node()
        if self. first == None:  # if list is empty
            self.first = self.last = nn
            return
        self.last.next = nn  # make the last node point to the new node
        nn.prev = self.last  # make the new node point to last node
        self.last = nn  # make the new node as last node

    def deleteNode(self):
        if self.first == None:
            print("List is empty")
            return
        userData = input("Enter data of the node to be deleted: ")
        if self.first == self.last:  # if list has only one node
            if self.first.data == userData:
                print("Node with data ", userData, " is deleted ")
                self.first = self.last = None  # make the list empty
                return
            print("Node with data ", userData, " was not found")
        else:  # list has 2 or more nodes
            temp = self.first
            while temp.data != userData and temp != self.last:
                temp = temp.next
            if temp.data == userData:  # we found the node to be deleted
                print("Node with data ", userData, " is deleted")
                if temp == self.first:  # node to be deleted is 1st node
                    self.first = first.next  # make the 2nd node as 1st node
                    self.first.prev = None
                elif temp == self.last:  # node to be deleted is last node
                    self.last = self.last.prev  # make the last but node as last node
                    self.last.next = None
                else:  # node to be deleted is inbetween node
                    temp.prev.next = temp.next  # make prev node of temp point to its next node
                    temp.next.prev = temp.prev  # make next node of temp point to its prev node
            else:
                print("Node with data ", userData, " was not found")

    def display_list(self):
        if self.first == None:
            print("List is empty")
            return
        temp = self.last  # begin from the rear end
        print("List elements from rear to front are")
        while temp != None:  # unitl temp has not reached prev link of 1st node
            print(temp.data)
            temp = temp.prev

def invalid_choice():
    print("Invalid operation")

obj = DoublyList()  # created an object of the class
choice = 0
list_operations = {
    1: obj.insertFront,
    2: obj.insertRear,
    3: obj.deleteNode,
    4: obj.display_list,
    5: sys.exit
}

while True:
    print("1:InsertFront 2: InsertRear 3:DeleteNode 4:Display List 5:Exit ")
    choice = int(input("Your choice ? "))
    list_operations.get(choice, invalid_choice)()


from selenium import webdriver
from bs4 import BeautifulSoup
import csv
import time
import random
import undetected_chromedriver as uc
from selenium.webdriver.chrome.service import Service

service = Service('.\chromedriver.exe')
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = uc.Chrome(service=service, options=options)
driver.maximize_window()

with open('articles.txt', 'r', encoding='utf-8') as f:
    art_links = f.read().split(' ')

w_file = open("#.csv", mode="w", encoding='utf-8')
file_writer = csv.writer(w_file, delimiter=",", lineterminator="\r")
file_writer.writerow(["doi", "author", "title", "year", "abstract", "journal"])

url1 = '#'
for i in art_links:
    url = url1 + i
    time.sleep(random.uniform(3, 5))
    driver.get(url)
    page = driver.page_source
    soup = BeautifulSoup(page, 'html.parser')

    doi = authors = abstract = data = title = ''
    if soup.find('meta', {"property": "og:title"}):
        title = str(soup.find('meta', {"property": "og:title"})["content"]).strip()
    authors1 = []
    for m in soup.find_all('meta', {"property": "article:author"}):
        authors1.append(str(m["content"]).strip())
        authors = '; '.join(authors1)
    if soup.find('meta', {"name": "citation_doi"}):
        doi = str(soup.find('meta', {"name": "citation_doi"})["content"]).strip()
    if soup.find('meta', {"property": "og:description"}):
        abstract = str(soup.find('meta', {"property": "og:description"})["content"]).strip()
    if soup.find('meta', {"property": "article:published_time"}):
        data = str(soup.find('meta', {"property": "article:published_time"})["content"]).strip()[:4]
    journal = '#'
    file_writer.writerow([doi, authors, title, data, abstract, journal])
    doi = authors = abstract = data = title = ''
    time.sleep(random.uniform(2, 6))


import os
import cowait
import asyncio
from ipykernel.kernelapp import IPKernelApp
from ipykernel.ipkernel import IPythonKernel
from cowait.tasks import Task, TaskDefinition
from cowait.tasks.notebook import NotebookRunner
from cowait.network import get_local_url
from cowait.worker import env_get_cluster_provider, env_get_task_definition
from .node import NotebookNode

import cowait.notebook.html_repr  # noqa: F401

ENV_KERNEL_TOKEN = 'COWAIT_KERNEL_TOKEN'

class KernelTask(Task):
    pass

class CowaitKernel(IPythonKernel):
    def start(self):
        super().start()
        asyncio.get_event_loop().create_task(self.init_node())

    async def init_node(self):
        cluster = env_get_cluster_provider()
        parent = env_get_task_definition()
        token = os.getenv(ENV_KERNEL_TOKEN)

        taskdef = TaskDefinition(
            name='kernel',
            image=parent.image,
            parent=parent.id,
            volumes=parent.volumes,
            env=parent.env,
            upstream=get_local_url(),
            meta={
                **parent.meta,
                'virtual': True,
            },
        )

        # set up notebook node
        self.node = NotebookNode(taskdef)
        await self.node.start(token)

        # instantiate kernel task
        self.task = KernelTask(node=self.node, cluster=cluster, taskdef=taskdef)

        # write globals
        self.shell.push({
            'cowait': cowait,
            'kernel': self.task,
            'tasks': self.task.subtasks,
            'NotebookRunner': NotebookRunner,
        })

    def do_shutdown(self, restart):
        asyncio.get_event_loop().create_task(self.shutdown_node())
        super().do_shutdown(restart)

    async def shutdown_node(self):
        await self.node.stop()
        self.node = None
        self.task = None

if __name__ == '__main__':
    app = IPKernelApp.instance(kernel_class=CowaitKernel)
    app.initialize()
    app.start()


import odl
import numpy as np
import torch
import matplotlib.pyplot as plt

# Function that creates needed ODL variables
def create_operator_and_space_FBP(shape, num_of_points, device='cpu'):

    device = 'astra_' + device

    # Defining discrete uniform domain
    domain = odl.uniform_discr([-1,-1], [1,1], shape, dtype='float32')

    # Defining some angle things for ray transform
    angle_interval = odl.set.domain.IntervalProd(0, 2*np.pi)
    angle_interval_points = odl.discr.grid.RectGrid(np.linspace(0, 2*np.pi, num_of_points)) #[:-1]
    angles = odl.discr.partition.RectPartition(angle_interval, angle_interval_points)

    # Defining some line things for ray transform
    line_interval = odl.set.domain.IntervalProd(-1*np.pi, 1*np.pi)
    line_interval_points = odl.discr.grid.RectGrid(np.linspace(-1*np.pi, 1*np.pi, num_of_points))
    lines = odl.discr.partition.RectPartition(line_interval, line_interval_points)

    # Defining fan beam geometry to be used in ray transform
    geometry = odl.tomo.geometry.conebeam.FanBeamGeometry(angles, lines, src_radius=2, det_radius=2)

    # Ray transform operator
    operator = odl.tomo.RayTransform(domain, geometry, impl=device)

    # Defining codomain just in case
    codomain = operator.range

    # And lastly the adjoint operator
    adjoint_operator = odl.tomo.analytic.filtered_back_projection.fbp_op(operator, padding=1)

    return domain, codomain, operator, adjoint_operator

def random_ellipsoids_and_sinogram(domain, codomain, operator, adjoint_operator, num_of_ellipsoids):

    # Create user-wanted-amount of random ellipsoids within the domain
    ellipsoids = [[1., np.random.rand(), np.random.rand(), 0.5*(np.random.rand()*2 - 1), 0.5*(np.random.rand()*2 - 1), 0] for i in range(num_of_ellipsoids)]

    # Ellipsoids as matrix, which can be plotted as an image
    image = odl.phantom.geometric.ellipsoid_phantom(domain, ellipsoids)
    image = torch.as_tensor(image)
    # Sinogram of the image of ellipsoids
    sinogram = operator(image)

    # Adding some Gaussian noise to the sinogram to make the reconstruction harder
    noisy_sinogram = sinogram + np.random.normal(0, 0.01, size=(np.shape(sinogram)[0], np.shape(sinogram)[1]))

    return image, sinogram, noisy_sinogram

# Functions in the use
domain, codomain, operator, adjoint_operator = create_operator_and_space_FBP((64,64), 64)
image, sinogram, noisy_sinogram = random_ellipsoids_and_sinogram(domain, codomain, operator, adjoint_operator, 4)

# plt.figure()
# plt.imshow(noisy_sinogram)
# plt.show()

# domain = torch.as_tensor(domain)
# noisy_sinogram = torch.as_tensor(noisy_sinogram)

grad = odl.Gradient(domain)
# print('grad', grad.shape)
l2_norm = odl.solvers.L2NormSquared(operator.range).translated(noisy_sinogram)
print(np.shape(noisy_sinogram))
# data_discrepancy = l2_norm.translated(noisy_sinogram)
data_discrepancy = l2_norm * operator

# regularizer = 0.05 * odl.solvers.GroupL1Norm(grad.range, 2) * grad
# regularizer = 0.05 * odl.solvers.L1Norm(grad.range) *grad
regularizer = 0.05 * odl.solvers.L2NormSquared(noisy_sinogram)

x = data_discrepancy.domain.zero()

print('x', np.shape(x))
print('regularizer', regularizer)
print('data', data_discrepancy)

print('##################')
print(data_discrepancy.domain)
print(regularizer.domain)
# print(x.domain)

reconstruction = odl.solvers.nonsmooth.proximal_gradient_solvers.proximal_gradient(x, f = regularizer, g = data_discrepancy, niter = 200, gamma=0.001)

### "no proximal operator implemented for funcitonal", Jevgenija tells in here -> https://github.com/odlgroup/odl/issues/1610 <- that
###                                                    'the proximal is not implemented for the composition of l1 and gradient'

### F JA G OVAT ERI DOMAINISSA!!!

# print(reconstruction)

reconstruction.show

# reconstruction.cpu().detach().numpy()
# print(np.dtype(reconstruction))

# plt.figure()
# plt.imshow(reconstruction)
# plt.show()

### https://github.com/odlgroup/odl/blob/master/examples/solvers/proximal_gradient_denoising.py


import numpy as np
from numpy.lib.function_base import iterable
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_digits

import random

def runMLPC(randomState = 1, maxIter = 50, nLayers = (8,), printProb = False, printNumber = 1):
    """
    Runs the MPLClassifier function and returns the score.
    Can also print the probability
    """
    digits = load_digits()

    X = digits.data
    y = digits.target

    # X = Data
    # Y = Classification
    # X, y = load_digits(return_X_y=True)

    trainX, testX, trainY, testY = train_test_split(X, y, test_size=0.30)

    clf = MLPClassifier(random_state=randomState, max_iter=maxIter, hidden_layer_sizes=nLayers).fit(trainX, trainY)

    if printProb:
        predict = clf.predict_proba(X[:8])

        for n in range(0,10):
            print(f'{n}: {round(predict[0][n], 3)}')

        plt.matshow(digits.images[0])
        plt.show()

    return clf.score(testX, testY)

def run(iterations = 5, randomState = 1, maxIter = 300, nLayers = (100,), printScore = False):
    """
    Runs multiple of runMLPC() then takes the mean for the resoult.
    Can then print to consol
    """
    scores = []

    for i in range(iterations):
        score = runMLPC(randomState=randomState,
                    maxIter=maxIter,
                    nLayers=nLayers,)

        scores.append(score)

    if printScore:
        for i in range(len(scores)):
            print(f'Run {i+1}| Score: {round(score * 100, 2)}%')

    return scores

def plotThing1(iter, iterations, layers, iterationIncreas=0, layersIncrease=0, plot = False):
    """
    Plots the score over diffrent iterations of values for the model.
    Can iterate over:
        - Numer of iteration the model should take
        - Numer of noed in the first layer

    """
    scores = []

    for i in range(iter):
        it = iterations + (iterationIncreas * i)
        layer = layers + (layersIncrease * i)

        if it < 1:
            it = 1

        if layer < 1:
            layer = 1

        score = run(maxIter=it, nLayers=(8,8,8,8))
        score = np.mean(score)
        scores.append(score)

    # print(scores)
    if plot:
        plt.plot(range(iter), scores)
        plt.xlabel(f'Num of iteration')
        plt.ylabel(f'Score (median of 5 runs)')
        plt.show()

    return scores

def plotThing2(iter, iterations, layers, plot = False):
    """
    This is a more limited version of the plotThis1() for when there is no need for iteration increases.
    """
    scores = []

    for _ in range(iter):
        score = runMLPC(maxIter=iterations, nLayers=layers)
        score = np.mean(score)
        scores.append(score)

    # print(scores)
    if plot:
        plt.plot(range(iter), scores)
        plt.show()

    return scores

def plotMulti():
    i = 15

    plt.plot(plotThing1(i,  0, 0, iterationIncreas=0, layersIncrease=0), label="1")
    plt.plot(plotThing1(i, 10, 0, iterationIncreas=0, layersIncrease=0), label="2")
    plt.plot(plotThing1(i, 20, 0, iterationIncreas=0, layersIncrease=0), label="3")
    plt.plot(plotThing1(i, 30, 0, iterationIncreas=0, layersIncrease=0), label="4")
    plt.plot(plotThing1(i, 40, 0, iterationIncreas=0, layersIncrease=0), label="5")
    plt.plot(plotThing1(i, 50, 0, iterationIncreas=0, layersIncrease=0), label="6")

    plt.legend()
    plt.xlabel('Num itterations')
    plt.ylabel('Score')
    plt.show()

def plotMulti2():
    """
    Prints a bar diagram of the score with difrent logic for the hidden layers.
    This uses the run() so there is only 1 output form the run.
    """
    it = 300

    startLayers = 0
    startNumLayers = 1
    layersSize = 1

    for i in range(15):
        i+1

        labelString = f'{startNumLayers + (layersSize*i)}'

        plt.bar(i, run(iterations=2,
                       randomState=1,
                       maxIter=it,
                       # Select one to use

                       ## Increments the amout of nodes in the layer and increas the amount of layers
                       # nLayers=tuple(startNumLayers + (LayerSize*l) for l in range(startLayers+i))),

                       ## Incements the amount of layers but keep tha same number of nodes
                       # nLayers=tuple(startNumLayers + (layersSize) for l in range(startLayers+i))),

                       ## Incremenst the amount of nodes in the first layer
                       nLayers=((startNumLayers + (layersSize*i)),)),
                label=str(labelString))

    plt.legend(title='Num layers')
    plt.xlabel("Num nodes")
    plt.ylabel("Score")
    plt.show()

def plotMulti3():
    """
    Print a bar diagram of how the score is over difrent number of hidden layers.
    This uses the plotThing2() to get a mean of multiple runs of the same model creation.
    """
    it = 3

    iterations = 100

    startLayers = 1
    startNumLayers = 8
    layersSize = 0

    start = 1
    end = 15

    for i in range(start, end):
        labelString = f'{startNumLayers + (layersSize*i)}'

        plt.bar(i, plotThing2(it, # Number of model iterations
                             iterations, # Number of iterations used in the model
                             # Adds more layers to the hidden layer, all with the same amount of nodes
                             tuple(startNumLayers + (layersSize) for l in range(startLayers+i))),
                label=str(labelString))

    #plt.legend(title='Num layers')
    plt.title(f'Score for {iterations} iterations of {start} to {end}')
    plt.xlabel(f'Num hidden layers of {startNumLayers} nodes')
    plt.ylabel(f'Score (mean of {it} runs per layer)')
    plt.show()

if __name__ == '__main__':
    # Run iterations of the MMPLClassifier
    # Start with itterations of the model
    # Start with nodes in the hiddel layer
    # Incrase the itteration
    # Incrase the hidden layesr

    plotThing1(30, 0, 0, iterationIncreas=10, plot=True)
    # plotThing1(50, 0, 100, iterationIncreas=1, layersIncrease=0, plot=True)

    # plotMulti()
    # plotMulti2()
    # plotMulti3()

    # run(iterations=5, nLayers=(8,8,8,8), maxIter=150, printScore=True)

    # print(runMLPC(printProb=True))


from os import system
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.

system('cls') # This clears the terminal to allow only the latest run to show

problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]

# This allows each problem total to be assessed
for problem in problems:
    # Split the string into components
    problem = problem.split()

    prob0 = int(problem[0])  # First number
    prob2 = int(problem[2]) # Second number

    # This gets the sum value
    if problem[1] == '+':
        x = prob0 + prob2
    else:
        x = prob0 - prob2

    # Sum Total
    print(prob0)
    print(problem[1], prob2)
    print("---------")
    print(x, '\n')

"""
Iterate over each item
Split each item into three components
    Convert [0] to an int
    Convert [1] to an operator
    Convert [2] to an int
    Perform a sum to get the answer
"""


import sqlite3
import struct

table_name = 'test'

def init_sqlite3(file):
    print('[DB:SQLite3] LIB Version :', sqlite3.version)
    print('[DB:SQLite3] DB Version :', sqlite3.sqlite_version)

    if not file:
        print('[DB:SQLite3] Not valid the file name.')
        return -1,

    #isolation_level=None:auto commit 설정 commit() 함수 실행 없이 자동으로 commit 됨.
    conn = sqlite3.connect(file, isolation_level=None)
    curr = conn.cursor()

    try:
        # First Read.
        curr.execute("SELECT * FROM {}".format(table_name))
        '''
        result = curr.fetchall()

        if result:
            for data in result:
                print(data)
        else:
            print("[DB:SQLite3] Faild to get data.")
        '''
    except sqlite3.OperationalError:
        # Create Table.
        print("[DB:SQLite3] Create Table.")
        curr.execute("CREATE TABLE IF NOT EXISTS '{}'(id INTEGER, name TEXT, age INTEGER)".format(table_name))

    return 0, conn, curr

def deinit_sqlite3(conn, curr):
    curr.close()
    conn.close()

def conv_data2sql(data : bytearray):
    try:
        """
        data_id  = struct.unpack('H', data[0:2]) # unsigned short (2 bytes)
        data_s01 = struct.unpack('f', data[2:6]) # float (4 bytes)
        data_s02 = struct.unpack('f', data[6:10]) # float (4 bytes)
        data_s03 = struct.unpack('f', data[10:14]) # float (4 bytes)
        data_s04 = struct.unpack('f', data[14:18]) # float (4 bytes)
        data_s05 = struct.unpack('f', data[18:22]) # float (4 bytes)
        data_s06 = struct.unpack('f', data[22:26]) # float (4 bytes)
        data_v01 = struct.unpack('f', data[26:30]) # float (4 bytes)
        data_v02 = struct.unpack('f', data[30:34]) # float (4 bytes)
        data_v03 = struct.unpack('f', data[34:38]) # float (4 bytes)
        data_v04 = struct.unpack('f', data[38:42]) # float (4 bytes)
        data_t01 = struct.unpack('f', data[42:46]) # float (4 bytes)
        data_h01 = struct.unpack('f', data[46:50]) # float (4 bytes)
        """

        #data_id, = struct.unpack('H', data[0:2])
        #data_s01, data_s02, data_s03, data_s04, data_s05, data_s06, data_v01, data_v02, data_v03, data_v04, data_t01, data_h01 = struct.unpack('ffffffffffff', data[2:50])

        data_id, data_s01, data_s02, data_s03, data_s04, data_s05, data_s06, data_v01, data_v02, data_v03, data_v04, data_t01, data_h01 = struct.unpack('<H12f', data)

        print('[STRUCT] Unpack. :', data_id, data_s01, data_s02, data_s03, data_s04, data_s05, data_s06, data_v01, data_v02, data_v03, data_v04, data_t01, data_h01)

        return data_id, 'insert into data_list (id, s01, s02, s03, s04, s05, s06, v01, v02, v03, v04, t01, h01) values ({:d},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f})'.format(data_id, data_s01, data_s02, data_s03, data_s04, data_s05, data_s06, data_v01, data_v02, data_v03, data_v04, data_t01, data_h01)
    except:
        print('[STRUCT] Error unpack.')
        return 0, ''

def get_option2sql(device_id):
    return 'select * from device_list where id={:d} limit 1'.format(device_id)

def set_option_with_SQLite3(curr, option_data):
    if option_data:
        for data in option_data:
            print(data)
    else:
        print("[DB:SQL] Faild to get data.")
        return -1
    return 0


def main():
    num = dict()
    for _ in range(10):
        n = int(input())
        if n in num:
            num[n] += 1
        else:
            num.update({n: 1})

    tot, m, temp = 0, 0, 0
    for i in num:
        tot += i * num[i]
        if num[i] > temp:
            m, temp = i, num[i]
    print(tot // 10)
    print(m)

if __name__ == "__main__":
    main()


from unittest import TestCase
import logging
import mock
from twitter_monitor import JsonStreamListener

logger = logging.getLogger("twitter_monitor")

class TestJsonStreamListener(TestCase):
    def setUp(self):
        logger.manager.disable = logging.CRITICAL

        self.listener = JsonStreamListener()

    def test_on_exception(self):
        self.assertTrue(self.listener.streaming_exception is None)

        try:
            raise Exception("testing")
        except Exception as exception:
            self.listener.on_exception(exception)
            self.assertEqual(self.listener.streaming_exception, exception, "Saves exception")

    def test_on_error(self):
        self.assertFalse(self.listener.on_error(404))

    def test_on_status(self):
        fake_status = dict(id=12345652)
        self.assertTrue(self.listener.on_status(fake_status))

class TestStreamListenerHandling(TestCase):
    def setUp(self):
        logger.manager.disable = logging.CRITICAL

        self.listener = JsonStreamListener()

        # Patch a bunch of methods on the listener so we can check
        # the handling of different message types
        self.patched_methods = ['on_status', 'on_delete', 'on_scrub_geo',
                                'on_limit', 'on_status_withheld', 'on_user_withheld',
                                'on_disconnect', 'on_stall_warning',
                                'on_error', 'on_unknown', 'on_exception']

        for patch in self.patched_methods:
            replacement = mock.Mock(wraps=getattr(self.listener, patch))
            mock.patch.object(self.listener, patch, replacement).start()

    def test_on_data_bad_json(self):
        """Nothing gets called when bad data is provided."""

        bad_json = "{asdfn35w3 asdkjnfw aksjnlka;;asd;f;a3lkjn"
        self.assertTrue(self.listener.on_data(bad_json), "Continues despite bad json")

        # Should have called NONE of the patched handler methods
        for patch in self.patched_methods:
            self.assertEqual(getattr(self.listener, patch).call_count, 0, "Does not call %s" % patch)

    def test_on_data_list_json(self):
        """Nothing gets called when the data represents a list."""

        list_json = '["foo", "bar"]'
        self.assertTrue(self.listener.on_data(list_json), "Continues despite list-like json")

        # Should have called NONE of the patched handler methods
        for patch in self.patched_methods:
            self.assertEqual(getattr(self.listener, patch).call_count, 0, "Does not call %s" % patch)

    def test_on_data_string_json(self):
        """Nothing gets called when the data represents a string."""

        string_json = '"foo"'
        self.assertTrue(self.listener.on_data(string_json), "Continues despite string-like json")

        # Should have called NONE of the patched handler methods
        for patch in self.patched_methods:
            self.assertEqual(getattr(self.listener, patch).call_count, 0, "Does not call %s" % patch)

    def test_on_data_delete(self):
        """Delete message"""
        delete_json = """{
          "delete":{
            "status":{
              "id":1234,
              "id_str":"1234",
              "user_id":3,
              "user_id_str":"3"
            }
          }
        }"""
        self.assertTrue(self.listener.on_data(delete_json))
        self.listener.on_delete.assert_called_once_with(1234, 3)

    def test_on_data_scrub_geo(self):
        """scrub_geo message"""
        scrub_geo = """{
          "scrub_geo":{
            "user_id":14090452,
            "user_id_str":"14090452",
            "up_to_status_id":23260136625,
            "up_to_status_id_str":"23260136625"
          }
        }"""
        self.assertTrue(self.listener.on_data(scrub_geo))
        self.listener.on_scrub_geo.assert_called_once_with(14090452, 23260136625)

    def test_on_data_limit(self):
        """limit message"""
        limit = """{
          "limit":{
            "track":1234
          }
        }"""

        self.assertTrue(self.listener.on_data(limit))
        self.listener.on_limit.assert_called_once_with(1234)

    def test_on_data_status_withheld(self):
        """status_withheld message"""
        status_withheld = """{
          "status_withheld":{
            "id":1234567890,
            "user_id":123456,
            "withheld_in_countries":["DE", "AR"]
          }
        }"""

        self.assertTrue(self.listener.on_data(status_withheld))
        self.listener.on_status_withheld.assert_called_once_with(1234567890, 123456, ["DE", "AR"])

    def test_on_data_user_withheld(self):
        """user_withheld message"""
        user_withheld = """{
          "user_withheld":{
            "id":123456,
            "withheld_in_countries":["DE","AR"]
          }
        }"""

        self.assertTrue(self.listener.on_data(user_withheld))
        self.listener.on_user_withheld.assert_called_once_with(123456, ["DE", "AR"])

    def test_on_data_disconnect(self):
        """disconnect message"""
        disconnect = """{
          "disconnect":{
            "code": 4,
            "stream_name":"test_stream",
            "reason":"Some reason"
          }
        }"""

        self.assertTrue(self.listener.on_data(disconnect))
        self.listener.on_disconnect.assert_called_once_with(4, "test_stream", "Some reason")

    def test_on_data_stall_warning(self):
        """stall_warning message"""
        stall_warning = """{
          "warning":{
            "code":"FALLING_BEHIND",
            "message":"Your connection is falling behind.",
            "percent_full": 60
          }
        }"""

        self.assertTrue(self.listener.on_data(stall_warning))
        self.listener.on_stall_warning.assert_called_once_with("FALLING_BEHIND",
                                                               "Your connection is falling behind.", 60)

    def test_on_data_status(self):
        """status message"""
        status = r"""{
          "coordinates": null,
          "created_at": "Sat Sep 10 22:23:38 +0000 2011",
          "truncated": false,
          "favorited": false,
          "id_str": "112652479837110273",
          "entities": {
            "urls": [
              {
                "expanded_url": "http://instagr.am/p/MuW67/",
                "url": "http://t.co/6J2EgYM",
                "indices": [
                  67,
                  86
                ],
                "display_url": "instagr.am/p/MuW67/"
              }
            ],
            "hashtags": [
              {
                "text": "tcdisrupt",
                "indices": [
                  32,
                  42
                ]
              }
            ],
            "user_mentions": [
              {
                "name": "Twitter",
                "id_str": "783214",
                "id": 783214,
                "indices": [
                  0,
                  8
                ],
                "screen_name": "twitter"
              },
              {
                "name": "Picture.ly",
                "id_str": "334715534",
                "id": 334715534,
                "indices": [
                  15,
                  28
                ],
                "screen_name": "SeePicturely"
              },
              {
                "name": "Bosco So",
                "id_str": "14792670",
                "id": 14792670,
                "indices": [
                  46,
                  58
                ],
                "screen_name": "boscomonkey"
              },
              {
                "name": "Taylor Singletary",
                "id_str": "819797",
                "id": 819797,
                "indices": [
                  59,
                  66
                ],
                "screen_name": "episod"
              }
            ]
          },
          "in_reply_to_user_id_str": "783214",
          "text": "@twitter meets @seepicturely at #tcdisrupt cc.@boscomonkey @episod http://t.co/6J2EgYM",
          "contributors": null,
          "id": 112652479837110273,
          "retweet_count": 0,
          "in_reply_to_status_id_str": null,
          "geo": null,
          "retweeted": false,
          "possibly_sensitive": false,
          "in_reply_to_user_id": 783214,
          "place": null,
          "source": "Instagram",
          "user": {
            "profile_sidebar_border_color": "eeeeee",
            "profile_background_tile": true,
            "profile_sidebar_fill_color": "efefef",
            "name": "Eoin McMillan ",
            "profile_image_url": "http://a1.twimg.com/profile_images/1380912173/Screen_shot_2011-06-03_at_7.35.36_PM_normal.png",
            "created_at": "Mon May 16 20:07:59 +0000 2011",
            "location": "Twitter",
            "profile_link_color": "009999",
            "follow_request_sent": null,
            "is_translator": false,
            "id_str": "299862462",
            "favourites_count": 0,
            "default_profile": false,
            "url": "http://www.eoin.me",
            "contributors_enabled": false,
            "id": 299862462,
            "utc_offset": null,
            "profile_image_url_https": "https://si0.twimg.com/profile_images/1380912173/Screen_shot_2011-06-03_at_7.35.36_PM_normal.png",
            "profile_use_background_image": true,
            "listed_count": 0,
            "followers_count": 9,
            "lang": "en",
            "profile_text_color": "333333",
            "protected": false,
            "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme14/bg.gif",
            "description": "Eoin's photography account. See @mceoin for tweets.",
            "geo_enabled": false,
            "verified": false,
            "profile_background_color": "131516",
            "time_zone": null,
            "notifications": null,
            "statuses_count": 255,
            "friends_count": 0,
            "default_profile_image": false,
            "profile_background_image_url": "http://a1.twimg.com/images/themes/theme14/bg.gif",
            "screen_name": "imeoin",
            "following": null,
            "show_all_inline_media": false
          },
          "in_reply_to_screen_name": "twitter",
          "in_reply_to_status_id": null
        }"""

        import json

        status_obj = json.loads(status)

        self.assertTrue(self.listener.on_data(status))
        self.listener.on_status.assert_called_once_with(status_obj)

    def test_on_data_unknown(self):
        """Unknown message"""
        unknown = """{
          "something":{
            "that":"i",
            "made":"up"
          }
        }"""

        import json

        unknown_obj = json.loads(unknown)

        self.assertTrue(self.listener.on_data(unknown))
        self.listener.on_unknown.assert_called_once_with(unknown_obj)


"""changed lastname length batch

Revision ID: 422e870731af
Revises: 33108cccddb9
Create Date: 2021-10-25 19:31:40.923928

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '422e870731af'
down_revision = '33108cccddb9'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.alter_column('lastname',
               existing_type=sa.VARCHAR(length=30),
               type_=sa.String(length=32),
               existing_nullable=False)

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.alter_column('lastname',
               existing_type=sa.String(length=32),
               type_=sa.VARCHAR(length=30),
               existing_nullable=False)

    # ### end Alembic commands ###


import sys
f = sys.stdin
# f = open("input.txt", "r")
n, t = map(int, f.readline().split())
a = [int(i) for i in f.readline().strip().split()]
s = 0
res = 0
left, right = 0, 0
while left < n:
    while right < n and s+a[right] <= t:
        s += a[right]
        right += 1
    res = max(res, right-left)
    s -= a[left]
    left += 1
print(res)


import random
boys = ['ali', 'reza', 'yasin','benyamin', 'mehrdad', 'sajjad', 'aidin', 'shahin']

girls = ['sara', 'zari', 'neda', 'homa', 'eli', 'goli', 'mary', 'mina']

marriage=[]

c=len(boys)

for i in range(c):

    if len(girls)==0:

        break

    a=random.choice(boys)

    b=random.choice(girls)
    boys.remove(a)

    girls.remove(b)

    couple=[a,b]

    marriage.append(couple)

print("results=",marriage)
print("goodbyeðŸ˜‡")


import configparser
import json
import sys, getopt
from varios import utilerias
from scrap import submanga_io, leermanga_net, manhwas_net, manhwas_18, tumanhwas_com, tmomanga_com
"""
Creado en python 3.11
"""

"""
Configuración Interna

Esta seccion es para la configuracion interna del programa
smg_Lib, es la ruta o librería en donde están almacenados los mangas.
Está definida en config.ini en el parámetro ruta_mangas
"""
smg_version = "2023.06.20"
config = configparser.ConfigParser()
config.read('config.ini')
home_mangas = config['DEFAULT']['ruta_mangas']
opciones = config.get('MANGAS','listado_mangas')
listado_mangas = json.loads(opciones)

"""
Fin de la definición de configuración
"""
def verifica_argumentos(argv):
        Varios = utilerias.utilerias(smg_version)
        submanga = submanga_io.submanga(smg_version, home_mangas, opciones, listado_mangas)
        Varios.presentacion()
        try:
                opts, args = getopt.getopt(argv,"hc")
                for opt, args in opts:
                        if opt == '-h':
                                Varios.modo_de_uso()
                                sys.exit()
                        if opt == '-c':
                                submanga.descargar_por_config()
                if (len(args) > 0):
                        if(args[0].startswith("https://www3.tumangaonline.site/manga/")):
                                submanga.descargar_solo(home_mangas, args[0])
                        elif(args[0].startswith("https://leermanga.net/manga/")):
                                leerManga = leermanga_net.leermanga_net(smg_version, home_mangas, opciones, listado_mangas)
                                leerManga.descargar_solo(home_mangas, args[0])
                        elif(args[0].startswith('https://www.manhwas.net/manga/')):
                                leer_manhwas = manhwas_net.manhwas_net(smg_version, home_mangas, opciones, listado_mangas)
                                leer_manhwas.descargar_solo(args[0])
                        elif(args[0].startswith('https://manhwa18.cc/webtoon/')):
                                leer_manhwas_18 = manhwas_18.manhwas_18(smg_version, home_mangas, opciones, listado_mangas)
                                leer_manhwas_18.descargar_solo(args[0])
                        elif(args[0].startswith('https://tumanhwas.com/manga/')):
                                tu_manhaws = tumanhwas_com.tumanhwas_com(smg_version, home_mangas, opciones, listado_mangas)
                                tu_manhaws.descargar_solo(args[0])
                        elif(args[0].startswith('https://tmomanga.com/manga/')):
                                tu_manhaws = tmomanga_com.tmomanga_com(smg_version, home_mangas, opciones, listado_mangas)
                                tu_manhaws.descargar_solo(args[0])
                        else:
                                print('Conector no encontrado para esa URL.')
        except getopt.GetoptError:
                print('smg.py -h para mayor información')
                sys.exit(2)

# Para ejecutar desde la linea de comandos
if __name__ == '__main__':
        verifica_argumentos(sys.argv[1:])