# -*- coding: utf-8 -*-
"""
Created on Fri Aug  4 17:12:46 2017

@author: alex
"""

import numpy as np
import sklearn.datasets
from sklearn.svm import SVC
from sklearn import metrics
import matplotlib.pyplot as plt
from time import time

def vis_data(data, labels):
    colors_d = {0: 'g', -1: 'g', 1: 'b'}
    colors = [colors_d[x] for x in labels]
    for label in set(labels):
        x1, x2 = data[labels == label, 0], data[labels == label, 1]
        plt.scatter(x1, x2,
                    marker='o',
                    s=100,
                    edgecolor=colors_d[label],
                    c='none',
                    alpha=0.7,
                    linewidths=2,
                    label="Class" + str(label))
    plt.legend()
    plt.xlabel('feature 1')
    plt.ylabel('feature 2')
    plt.show()

class SimpleSvm:
    x = None
    y = None
    n = None
    _kernel_type = ''
    alphas = None
    b = None
    C = None
    support_vectors = None
    tolerance = None
    max_passes = None
    _time_to_fit = None
    _random_seed = None

    def __init__(self, C=1.0, tolerance=0.05, max_passes=20, kernel='linear', random_seed=1, verbose=0):
        self.C = C
        self.tolerance = tolerance
        self.max_passes = max_passes
        self._kernel_type = kernel
        self._random_seed = random_seed
        self.verbose = verbose

    def fit(self, x, y):
        self._verify_data(x, y)

        st = time()
        self.x = x
        self.y = y
        self.n = len(y)
        if self._random_seed:
            np.random.seed(self._random_seed)

        self.alphas = np.zeros(self.n)
        self.b = 0
        passes_count = 0

        while passes_count < self.max_passes:
            num_changed_alphas = 0
            # for each instance
            for i in range(self.n):
                E_i = self._calc_f_x(self.x[i]) - self.y[i]
                if (self.y[i] * E_i < -self.tolerance and self.alphas[i] < self.C) or (
                            self.y[i] * E_i > self.tolerance and self.alphas[i] > 0):
                    rand_ind = np.random.randint(low=0, high=10, size=2)
                    j = rand_ind[0] if rand_ind[0] != i else rand_ind[1]

                    E_j = self._calc_f_x(self.x[j]) - self.y[j]
                    a_i, a_j = self.alphas[i], self.alphas[j]

                    # L and H
                    if self.y[i] != self.y[j]:
                        L = max(0.0, self.alphas[j] - self.alphas[i])
                        H = min(self.C, self.C + self.alphas[j] - self.alphas[i])
                    else:
                        L = max(0.0, self.alphas[j] + self.alphas[i] - self.C)
                        H = min(self.C, self.alphas[j] + self.alphas[i])

                    if L == H:
                        continue

                    # small eta
                    h = 2 * self._kernel(self.x[i], self.x[j]) - \
                        self._kernel(self.x[i], self.x[i]) - \
                        self._kernel(self.x[j], self.x[j])
                    if h >= 0:
                        continue

                    # new a_j
                    self.alphas[j] = self.alphas[j] - (self.y[j] * (E_i - E_j)) / h
                    if self.alphas[j] > H:
                        self.alphas[j] = H
                    elif L <= self.alphas[j] <= H:
                        self.alphas[j] = self.alphas[j]
                    elif self.alphas[j] < L:
                        self.alphas[j] = L

                    if abs(a_j - self.alphas[j]) < 10 ** -5:
                        continue

                    # new a_i
                    self.alphas[i] = self.alphas[i] + self.y[i] * self.y[j] * (a_j - self.alphas[j])

                    # new b
                    b1 = self.b - E_i - self.y[i] * (self.alphas[i] - a_i) * self._kernel(self.x[i], self.x[i]) - \
                         self.y[j] * (self.alphas[j] - a_j) * self._kernel(self.x[i], self.x[j])
                    b2 = self.b - E_j - self.y[i] * (self.alphas[i] - a_i) * self._kernel(self.x[i], self.x[j]) - \
                         self.y[j] * (self.alphas[j] - a_j) * self._kernel(self.x[j], self.x[j])

                    if 0 < self.alphas[i] < self.C:
                        self.b = b1
                    elif 0 < self.alphas[j] < self.C:
                        self.b = b2
                    else:
                        self.b = (b1 + b2) / 2.0

                    num_changed_alphas += 1
                    if self.verbose > 0:
                        print("num_changed_alphas = %d" % num_changed_alphas)
            if num_changed_alphas == 0:
                passes_count += 1
            else:
                passes_count = 0

        self._time_to_fit = time() - st
        # save support vectors ids
        self.support_vectors = np.where(self.alphas > 0)[0]

    def predict(self, x):
        fx = [self._calc_f_x(xi) for xi in x]
        predictions = [-1 if fxi < 0 else 1 for fxi in fx]
        return predictions

    def get_tr_accuracy(self):
        predictions = self.predict(self.x)
        return metrics.accuracy_score(self.y, predictions)

    def compare_to_sklearn(self):
        sklearn_model = sklearn.svm.SVC(C=self.C, kernel=self._kernel_type)
        st = time()
        sklearn_model.fit(X=self.x, y=self.y)
        time_sklearn = time() - st
        sklearn_prediction = sklearn_model.predict(X=self.x)
        sklearn_prediction[sklearn_prediction == 0] = -1  # although sklearn.svm also use -1/1 convention

        my_prediction = self.predict(x=self.x)

        # compare #
        mult_pred = my_prediction * sklearn_prediction
        n_right = sum(mult_pred > 0)
        print("\nSimpleSvm vs sklearn.svm.SVC:\n=============================\n")
        print("n={} | equal={} | unequal={}".format(self.n, n_right, self.n-n_right))
        print("Train accuracy:      SimpleSvm={} | sklearn={}".
              format(self.get_tr_accuracy(), sklearn_model.score(self.x, self.y)))
        print("Num support vectors: SimpleSvm={} | sklearn={}".
              format(len(self.support_vectors), len(sklearn_model.support_vectors_)))
        print("Training time:       SimpleSvm={0:.3f} sec | sklearn={1:.3f} sec".format(self._time_to_fit, time_sklearn))
        return sklearn_model

    def visualize_model(self):
        colors_d = {0: 'g', -1: 'g', 1: 'b'}
        colors = [colors_d[val] for val in self.y]
        plt.scatter(self.x[:, 0], self.x[:, 1],
                    marker='o',
                    s=80,
                    edgecolors=colors,
                    c='none',
                    alpha=0.7,
                    linewidths=2)
        a, b = self._get_sv_by_class()
        plt.plot(a[:, 0], a[:, 1], marker='d', linestyle='', color='g', label='support vectors -1')
        plt.plot(b[:, 0], b[:, 1], marker='d', linestyle='', color='b', label='support vectors +1')
        plt.legend()
        plt.show()

    def _calc_f_x(self, x_val):
        kernels_v = np.array([self._kernel(x_val, self.x[x]) if self.alphas[x] else 0 for x in range(self.n)])
        f_x = sum(self.alphas * self.y * kernels_v) + self.b
        return f_x

    def _kernel(self, a, b):
        if self._kernel_type == 'linear':
            return sum(a * b)
        else:
            raise Exception("Unsupported kernel")
            return None

    def _get_sv_by_class(self):
        sv_x = [self.x[xi] for xi in self.support_vectors]
        sv_classes = [self.y[xi] for xi in self.support_vectors]  # self.predict(sv_x)
        a = np.array([sv_x[i] for i, pred in enumerate(sv_classes) if pred == -1])
        b = np.array([sv_x[i] for i, pred in enumerate(sv_classes) if pred == 1])
        return a, b

    def _verify_data(self, x, y):
        if len(x) != len(y):
            print("X and Y contain different number of samples!")
            return False
        if len(np.unique(y)) > 2:
            print("Currently only 2 classes support!")
            return False
        if 0 in y:
            y[y == 0] = -1
            return True

def main(cluster_std=2.5):
    # Generate random dataset with 2 classes in 2D space
    data, labels = sklearn.datasets.make_blobs(centers=2, cluster_std=cluster_std)

    # Show the data
    vis_data(data, labels)

    # Fit simple SVM model
    my_svm = SimpleSvm()
    my_svm.fit(data, labels)

    # Compare to sklearn.svm.SVC
    sk_svm = my_svm.compare_to_sklearn()

if "__name__" == "__main__":
    main()


"""

This is the module for the nightmare ghost enemy sprite
This enemy patrols the perimeter of the level in very unpredictable ways.

"""

import pygame
from pygame.sprite import Sprite
import random

class NM_Ghost(Sprite):

    def __init__(self, screen):
        super(NM_Ghost, self).__init__()
        # Load main screen size
        self.screen = screen

        # Load initial ghost image and get rectangle collision hull
        self.images = []
        for i in range(1, 11):
            self.images.append(pygame.image.load('images/NMG_L_%d.png' % i).convert_alpha())

        self.frame = 0
        self.boolFrameUp = True
        self.image = self.images[self.frame]
        self.intRandFrameFlag = 0
        self.intRandFlag = 1
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        # Set randomly if the ghost will move along the x axis (1) or y axis (2)
        self.axis = random.randint(1, 4)
        if self.axis == 1:
            self.moving_left = True
            self.moving_down = False
        elif self.axis == 2:
            self.moving_left = False
            self.moving_down = False
        elif self.axis == 3:
            self.moving_left = False
            self.moving_down = True
        elif self.axis == 4:
            self.moving_left = False
            self.moving_down = False
        # Random groups to set ghost centers away from player
        self.starting_pos_group = random.randint(1, 4)

        # Position at random starting point on the screen
        if self.starting_pos_group == 1:
            self.rect.centerx = random.randrange(100, 700, 50)
            self.rect.centery = random.randrange(100, 200, 50)
        elif self.starting_pos_group == 2:
            self.rect.centerx = random.randrange(100, 700, 50)
            self.rect.centery = random.randrange(400, 500, 50)
        elif self.starting_pos_group == 3:
            self.rect.centerx = random.randrange(100, 200, 50)
            self.rect.centery = random.randrange(100, 500, 50)
        elif self.starting_pos_group == 4:
            self.rect.centerx = random.randrange(600, 700, 50)
            self.rect.centery = random.randrange(100, 500, 50)

    def move_and_blitself(self):
        if self.axis <= 2:
            if self.rect.centerx <= 100 and self.moving_left:
                self.intRandFlag = random.randint(1, 2)
                if self.intRandFlag == 1:
                    self.moving_left = False
                    self.axis = random.randint(1, 4)
                elif self.intRandFlag == 2:
                    self.rect.centerx = 700
            elif self.rect.centerx >= 700 and not self.moving_left:
                self.intRandFlag = random.randint(1, 2)
                if self.intRandFlag == 1:
                    self.moving_left = True
                    self.axis = random.randint(1, 4)
                elif self.intRandFlag == 2:
                    self.rect.centerx = 100

            if self.moving_left:
                self.rect.centerx -= 2
                self.image = self.images[self.frame]
            elif not self.moving_left:
                self.rect.centerx += 2
                self.image = pygame.transform.flip(self.images[self.frame], True, False)

            # Place on the main screen coordinates
            self.screen.blit(self.image, self.rect)

        elif self.axis > 2:
            if self.rect.centery <= 100 and not self.moving_down:
                self.intRandFlag = random.randint(1, 2)
                if self.intRandFlag == 1:
                    self.moving_down = True
                    self.axis = random.randint(1, 4)
                elif self.intRandFlag == 2:
                    self.rect.centery = 500

            elif self.rect.centery >= 500 and self.moving_down:
                self.intRandFlag = random.randint(1, 2)
                if self.intRandFlag == 1:
                    self.moving_down = False
                    self.axis = random.randint(1, 4)
                elif self.intRandFlag == 2:
                    self.rect.centery = 100

            if self.moving_down:
                self.rect.centery += 2
                self.image = pygame.transform.rotate(self.images[self.frame], 90)

            elif not self.moving_down:
                self.rect.centery -= 2
                self.image = pygame.transform.rotate(self.images[self.frame], 270)

            # Place on the main screen coordinates
            self.screen.blit(self.image, self.rect)

        '''
        Code to advance the frame at a random speed by using random and modulus division.
        The way it is coded there is a 1/20 chance that the frame will advance each loop.
        This is because the range of random is from 1 to 100 and there is a 1/20 chance
        that a number will be divisible by 5 with no remainder.
        This makes the ghosts animation move at a constantly varying frame rate to make it
        more interesting and keep the number of frame images low at 10 without the loop being
        too fast.
        '''
        self.intRandFrameFlag = random.randint(1, 100)

        if self.intRandFrameFlag % 5 == 0:
            if self.frame < 9 and self.boolFrameUp:
                self.frame += 1
            elif self.boolFrameUp:
                self.boolFrameUp = False
            elif self.frame > 0 and not self.boolFrameUp:
                self.frame -= 1
            elif not self.boolFrameUp:
                self.boolFrameUp = True


import pyttsx3
from selenium import webdriver

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)

def weather():
        driver = webdriver.Chrome()
        driver.get("https://www.weather-forecast.com/locations/Port-Elizabeth/forecasts/latest")
        weather = driver.find_element_by_class_name("b-forecast__table-description-content").text
        engine.say(weather)
        engine.runAndWait()

        driver.quit()


# -*- coding: utf-8 -*-
# @Time    : 2022/9/16 11:41
# @Author  : Carlos
import re
import requests

# 新建一个文件
file=open(
        "douban_Top250.txt",#必需，文件路径（相对或者绝对路径）
      mode="a", # "w"从开头开始编辑，即原有内容会被删除,"a"新的内容将会被写入到已有内容之后
      encoding="utf8",# 打开方式，一般使用utf8
      errors=None, # 报错级别
      newline=None, # 区分换行符
      closefd=True, # 传入的file参数类型
      opener=None #设置自定义开启器，开启器的返回值必须是一个打开的文件描述符。
          )

# 设置四个参数，依次添加。
def add_file(name,year,score,num):
    file.write(
       f"电影时间：{it.group('year').strip():<5},"
       f"电影评分：{it.group('score'):<4},"
       f"电影评论：{it.group('num'):<10},"
       f"电影名称：{it.group('name').strip():<10}\n"
        )

# 创建一个获取网页源代码的网址
def connet_resp(re_url):
    resp=requests.get(url=re_url,headers=header) # 获取网页源代码
    resp.close()  # 断开连接
    # print(resp.text) #输出网页源代码

    # 正则将将关键点做为一个迭代器。
    obj = re.compile(r'
.*?
.*?(?P<name>.*?)'
                     r'.*?
.*?
(?P<year>.*?)&nbsp.*?(?P<score>.*?).*?'
                     r'(?P<num>.*?人评价)', re.S)

    result = obj.finditer(resp.text)  # 将obj的迭代器返回成一个列表

    return result #返回正则后的信息

for pages in range(0,251,25):

    # 请求体，设备信息。
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.33"
    }

    if pages==0:
        re_url = "https://movie.douban.com/top250"
    else:
        re_url=f"https://movie.douban.com/top250?start={pages}&filter="
        print(re_url)

    for it in connet_resp(re_url): # 调用 connet_resp返回的正则表达式迭代器列表

        # print(it.group()) #输出迭代器内容

        print(
           f"电影时间：{it.group('year').strip():<5},"
           f"电影评分：{it.group('score'):<4},"
           f"电影评论：{it.group('num'):<10},"
           f"电影名称：{it.group('name').strip():<10}",
            end="\n"
            )

        # 调用add——file方法，添加相关信息
        add_file(it.group('name'),it.group('year').strip(),it.group('score'),it.group('num'))

# 关闭文件
file.close()


import logging
import os
from argparse import ArgumentParser
from pathlib import Path

from brbanks2ynab.config.initialize import init_config
from brbanks2ynab.sync.sync import sync

def _default_config_path():
    return os.path.join(os.getcwd(), 'brbanks2ynab.json')

def main():
    logging.basicConfig()
    logger = logging.getLogger('brbanks2ynab')

    parser = ArgumentParser(description='Importador de transações de bancos brasileiros para o YNAB')
    parser.add_argument('--debug', action='store_true')

    subparsers = parser.add_subparsers(dest='cmd')

    sync_parser = subparsers.add_parser('sync')
    sync_parser.add_argument('--config', default=_default_config_path())
    sync_parser.add_argument('--dry', action='store_true', default=False)
    configure_parser = subparsers.add_parser('configure')

    result = parser.parse_args()

    if result.debug:
        logger.setLevel(logging.DEBUG)

    if result.cmd == 'configure':
        init_config()
    elif result.cmd == 'sync':
        path = Path(result.config)
        sync(path, result.dry)

if __name__ == '__main__':
    main()


from __future__ import annotations

from typing import TYPE_CHECKING, Collection

from pipenv.patched.pip._vendor.cachecontrol.adapter import CacheControlAdapter
from pipenv.patched.pip._vendor.cachecontrol.cache import DictCache

if TYPE_CHECKING:
    from pipenv.patched.pip._vendor import requests

    from pipenv.patched.pip._vendor.cachecontrol.cache import BaseCache
    from pipenv.patched.pip._vendor.cachecontrol.controller import CacheController
    from pipenv.patched.pip._vendor.cachecontrol.heuristics import BaseHeuristic
    from pipenv.patched.pip._vendor.cachecontrol.serialize import Serializer

def CacheControl(
    sess: requests.Session,
    cache: BaseCache | None = None,
    cache_etags: bool = True,
    serializer: Serializer | None = None,
    heuristic: BaseHeuristic | None = None,
    controller_class: type[CacheController] | None = None,
    adapter_class: type[CacheControlAdapter] | None = None,
    cacheable_methods: Collection[str] | None = None,
) -> requests.Session:
    cache = DictCache() if cache is None else cache
    adapter_class = adapter_class or CacheControlAdapter
    adapter = adapter_class(
        cache,
        cache_etags=cache_etags,
        serializer=serializer,
        heuristic=heuristic,
        controller_class=controller_class,
        cacheable_methods=cacheable_methods,
    )
    sess.mount("http://", adapter)
    sess.mount("https://", adapter)

    return sess


import math
from typing import List

import torch
from torch import nn
from torch.nn import functional as F
from pydantic import StrictInt, StrictFloat, StrictBool
import torchvision

# def swish(input):
#     return input * torch.sigmoid(input)

swish = F.silu

@torch.no_grad()
def variance_scaling_init_(tensor, scale=1, mode="fan_avg", distribution="uniform"):
    fan_in, fan_out = nn.init._calculate_fan_in_and_fan_out(tensor)

    if mode == "fan_in":
        scale /= fan_in

    elif mode == "fan_out":
        scale /= fan_out

    else:
        scale /= (fan_in + fan_out) / 2

    if distribution == "normal":
        std = math.sqrt(scale)

        return tensor.normal_(0, std)

    else:
        bound = math.sqrt(3 * scale)

        return tensor.uniform_(-bound, bound)

def conv2d(
    in_channel,
    out_channel,
    kernel_size,
    stride=1,
    padding=0,
    bias=True,
    scale=1,
    mode="fan_avg",
):
    conv = nn.Conv2d(
        in_channel, out_channel, kernel_size, stride=stride, padding=padding, bias=bias
    )

    variance_scaling_init_(conv.weight, scale, mode=mode)

    if bias:
        nn.init.zeros_(conv.bias)

    return conv

def linear(in_channel, out_channel, scale=1, mode="fan_avg"):
    lin = nn.Linear(in_channel, out_channel)

    variance_scaling_init_(lin.weight, scale, mode=mode)
    nn.init.zeros_(lin.bias)

    return lin

class Swish(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input):
        return swish(input)

class Upsample(nn.Sequential):
    def __init__(self, channel):
        layers = [
            nn.Upsample(scale_factor=2, mode="nearest"),
            conv2d(channel, channel, 3, padding=1),
        ]

        super().__init__(*layers)

class Downsample(nn.Sequential):
    def __init__(self, channel):
        layers = [conv2d(channel, channel, 3, stride=2, padding=1)]

        super().__init__(*layers)

class InstanceNorm(nn.Module):
    def __init__(self, epsilon=1e-8):
        """
            @notice: avoid in-place ops.
            https://discuss.pytorch.org/t/encounter-the-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/836/3
        """
        super(InstanceNorm, self).__init__()
        self.epsilon = epsilon

    def forward(self, x):
        x   = x - torch.mean(x, (2, 3), True)
        tmp = torch.mul(x, x) # or x ** 2
        tmp = torch.rsqrt(torch.mean(tmp, (2, 3), True) + self.epsilon)
        return x * tmp

class ApplyStyle(nn.Module):
    """
        @ref: https://github.com/lernapparat/lernapparat/blob/master/style_gan/pytorch_style_gan.ipynb
    """
    def __init__(self, latent_size, channels):
        super(ApplyStyle, self).__init__()
        self.linear = nn.Linear(latent_size, channels * 2)

    def forward(self, x, latent):
        style = self.linear(latent)  # style => [batch_size, n_channels*2]
        shape = [-1, 2, x.size(1), 1, 1]
        style = style.view(shape)    # [batch_size, 2, n_channels, ...]
        #x = x * (style[:, 0] + 1.) + style[:, 1]
        x = x * (style[:, 0] * 1 + 1.) + style[:, 1] * 1
        return x

class ResnetBlock_Adain(nn.Module):
    def __init__(self, dim, latent_size, padding_type, activation=nn.ReLU(True)):
        super(ResnetBlock_Adain, self).__init__()

        p = 0
        conv1 = []
        if padding_type == 'reflect':
            conv1 += [nn.ReflectionPad2d(1)]
        elif padding_type == 'replicate':
            conv1 += [nn.ReplicationPad2d(1)]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' % padding_type)
        conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding = p), InstanceNorm()]
        self.conv1 = nn.Sequential(*conv1)
        self.style1 = ApplyStyle(latent_size, dim)
        self.act1 = activation

        p = 0
        conv2 = []
        if padding_type == 'reflect':
            conv2 += [nn.ReflectionPad2d(1)]
        elif padding_type == 'replicate':
            conv2 += [nn.ReplicationPad2d(1)]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' % padding_type)
        conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), InstanceNorm()]
        self.conv2 = nn.Sequential(*conv2)
        self.style2 = ApplyStyle(latent_size, dim)
        self.attention = SelfAttention(dim, n_head=1)

    def forward(self, x, dlatents_in_slice):
        y = self.conv1(x)
        y = self.style1(y, dlatents_in_slice)
        y = self.act1(y)
        y = self.conv2(y)
        y = self.style2(y, dlatents_in_slice)
        y = x + y
        out = self.attention(y)
        return out

class ResBlock(nn.Module):
    def __init__(
        self, in_channel, out_channel, time_dim, use_affine_time=False, dropout=0
    ):
        super().__init__()

        self.use_affine_time = use_affine_time
        time_out_dim = out_channel
        time_scale = 1
        norm_affine = True

        if self.use_affine_time:
            time_out_dim *= 2
            time_scale = 1e-10
            norm_affine = False

        self.norm1 = nn.GroupNorm(32, in_channel)
        self.activation1 = Swish()
        self.conv1 = conv2d(in_channel, out_channel, 3, padding=1)

        self.time = nn.Sequential(
            Swish(), linear(time_dim, time_out_dim, scale=time_scale)
        )

        self.norm2 = nn.GroupNorm(32, out_channel, affine=norm_affine)
        self.activation2 = Swish()
        self.dropout = nn.Dropout(dropout)
        self.conv2 = conv2d(out_channel, out_channel, 3, padding=1, scale=1e-10)

        if in_channel != out_channel:
            self.skip = conv2d(in_channel, out_channel, 1)

        else:
            self.skip = None

    def forward(self, input, time):
        batch = input.shape[0]

        out = self.conv1(self.activation1(self.norm1(input)))

        if self.use_affine_time:
            gamma, beta = self.time(time).view(batch, -1, 1, 1).chunk(2, dim=1)
            out = (1 + gamma) * self.norm2(out) + beta

        else:
            out = out + self.time(time).view(batch, -1, 1, 1)
            out = self.norm2(out)

        out = self.conv2(self.dropout(self.activation2(out)))

        if self.skip is not None:
            input = self.skip(input)

        return out + input

class SelfAttention(nn.Module):
    def __init__(self, in_channel, n_head=1):
        super().__init__()

        self.n_head = n_head

        self.norm = nn.GroupNorm(32, in_channel)
        self.qkv = conv2d(in_channel, in_channel * 3, 1)
        self.out = conv2d(in_channel, in_channel, 1, scale=1e-10)

    def forward(self, input):
        batch, channel, height, width = input.shape
        n_head = self.n_head
        head_dim = channel // n_head

        norm = self.norm(input)
        qkv = self.qkv(norm).view(batch, n_head, head_dim * 3, height, width)
        query, key, value = qkv.chunk(3, dim=2)  # bhdyx

        attn = torch.einsum(
            "bnchw, bncyx -> bnhwyx", query, key
        ).contiguous() / math.sqrt(channel)
        attn = attn.view(batch, n_head, height, width, -1)
        attn = torch.softmax(attn, -1)
        attn = attn.view(batch, n_head, height, width, height, width)

        out = torch.einsum("bnhwyx, bncyx -> bnchw", attn, value).contiguous()
        out = self.out(out.view(batch, channel, height, width))

        return out + input

class TimeEmbedding(nn.Module):
    def __init__(self, dim):
        super().__init__()

        self.dim = dim

        inv_freq = torch.exp(
            torch.arange(0, dim, 2, dtype=torch.float32) * (-math.log(10000) / dim)
        )

        self.register_buffer("inv_freq", inv_freq)

    def forward(self, input):
        shape = input.shape
        sinusoid_in = torch.ger(input.view(-1).float(), self.inv_freq)
        pos_emb = torch.cat([sinusoid_in.sin(), sinusoid_in.cos()], dim=-1)
        pos_emb = pos_emb.view(*shape, self.dim)

        return pos_emb

class ResBlockWithAttention(nn.Module):
    def __init__(
        self,
        in_channel,
        out_channel,
        time_dim,
        dropout,
        use_attention=False,
        attention_head=1,
        use_affine_time=False,
    ):
        super().__init__()

        self.resblocks = ResBlock(
            in_channel, out_channel, time_dim, use_affine_time, dropout
        )

        if use_attention:
            self.attention = SelfAttention(out_channel, n_head=attention_head)

        else:
            self.attention = None

    def forward(self, input, time):
        out = self.resblocks(input, time)

        if self.attention is not None:
            out = self.attention(out)

        return out

def spatial_fold(input, fold):
    if fold == 1:
        return input

    batch, channel, height, width = input.shape
    h_fold = height // fold
    w_fold = width // fold

    return (
        input.view(batch, channel, h_fold, fold, w_fold, fold)
        .permute(0, 1, 3, 5, 2, 4)
        .reshape(batch, -1, h_fold, w_fold)
    )

def spatial_unfold(input, unfold):
    if unfold == 1:
        return input

    batch, channel, height, width = input.shape
    h_unfold = height * unfold
    w_unfold = width * unfold

    return (
        input.view(batch, -1, unfold, unfold, height, width)
        .permute(0, 1, 4, 2, 5, 3)
        .reshape(batch, -1, h_unfold, w_unfold)
    )

class UNet(nn.Module):
    def __init__(
        self,
        in_channel: StrictInt,
        id_channel: StrictInt,
        channel: StrictInt,
        channel_multiplier: List[StrictInt],
        n_res_blocks: StrictInt,
        attn_strides: List[StrictInt],
        attn_heads: StrictInt = 1,
        use_affine_time: StrictBool = False,
        dropout: StrictFloat = 0,
        fold: StrictInt = 1,
    ):
        super().__init__()

        self.fold = fold

        time_dim = channel * 4

        n_block = len(channel_multiplier)

        self.time = nn.Sequential(
            TimeEmbedding(channel),
            linear(channel, time_dim),
            Swish(),
            linear(time_dim, time_dim),
        )

        down_layers = [conv2d(in_channel * (fold ** 2), channel, 3, padding=1)]
        feat_channels = [channel]
        in_channel = channel
        for i in range(n_block):
            for _ in range(n_res_blocks):
                channel_mult = channel * channel_multiplier[i]

                down_layers.append(
                    ResBlockWithAttention(
                        in_channel,
                        channel_mult,
                        time_dim,
                        dropout,
                        use_attention=2 ** i in attn_strides,
                        attention_head=attn_heads,
                        use_affine_time=use_affine_time,
                    )
                )

                feat_channels.append(channel_mult)
                in_channel = channel_mult

            if i != n_block - 1:
                down_layers.append(Downsample(in_channel))
                feat_channels.append(in_channel)

        self.down = nn.ModuleList(down_layers)

        ### resnet blocks
        BN = []
        for i in range(6):
            BN += [
                ResnetBlock_Adain(in_channel, latent_size=id_channel, padding_type='reflect', activation=nn.ReLU(True))]
        self.BottleNeck = nn.Sequential(*BN)

        up_layers = []
        for i in reversed(range(n_block)):
            for _ in range(n_res_blocks + 1):
                channel_mult = channel * channel_multiplier[i]

                up_layers.append(
                    ResBlockWithAttention(
                        in_channel + feat_channels.pop(),
                        channel_mult,
                        time_dim,
                        dropout=dropout,
                        use_attention=2 ** i in attn_strides,
                        attention_head=attn_heads,
                        use_affine_time=use_affine_time,
                    )
                )

                in_channel = channel_mult

            if i != 0:
                up_layers.append(Upsample(in_channel))

        self.up = nn.ModuleList(up_layers)

        self.out = nn.Sequential(
            nn.GroupNorm(32, in_channel),
            Swish(),
            conv2d(in_channel, 3 * (fold ** 2), 3, padding=1, scale=1e-10),
        )

    def forward(self, input, time, id):
        time_embed = self.time(time)

        feats = []

        out = spatial_fold(input, self.fold)
        for layer in self.down:
            if isinstance(layer, ResBlockWithAttention):
                out = layer(out, time_embed)

            else:
                out = layer(out)

            feats.append(out)

        for i in range(len(self.BottleNeck)):
            out = self.BottleNeck[i](out, id)

        for layer in self.up:
            if isinstance(layer, ResBlockWithAttention):
                out = layer(torch.cat((out, feats.pop()), 1), time_embed)

            else:
                out = layer(out)

        out = self.out(out)
        out = spatial_unfold(out, self.fold)

        return out

def get_id_from_image(img, embedder, centered, downsample=False):
  if centered:
    img = (img + 1) / 2
  img = torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=False)(img)
  if downsample:
    img = F.interpolate(img, scale_factor=56 / img.shape[-1])
  img = F.interpolate(img, scale_factor=112 / img.shape[-1])
  id = embedder(img)
  id = F.normalize(id, p=2, dim=1)
  return id

def cosin_metric(x1, x2):
    return torch.sum(x1 * x2, dim=1) / (torch.norm(x1, dim=1) * torch.norm(x2, dim=1))


import argparse

from raspibear.app import raspibearapp
from raspibear import default_config

def main():
    argparser = argparse.ArgumentParser(description="Run the RaspiBear web app")
    argparser.add_argument("-s", "--host", default=default_config.HOST, help="The host to listen on. Default: {}".format(default_config.HOST))
    argparser.add_argument("-d", "--debug", action="store_true", help="Enable debug log")
    argparser.add_argument("-p", "--port", default=default_config.PORT, help="Port to listen on. Default: {}".format(default_config.PORT))
    args = argparser.parse_args()

    raspibearapp.run(port=int(args.port), host=args.host, debug=args.debug)

if __name__ == "__main__":
    main()


"""add_4_url_for_artifacts

Revision ID: 43781f11dd5d
Revises: 2fc3fbeb7c6e
Create Date: 2022-08-05 10:21:52.351520

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = '43781f11dd5d'
down_revision = '2fc3fbeb7c6e'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('piece', sa.Column('artifact_url_1', sa.String(length=300), nullable=True))
    op.add_column('piece', sa.Column('artifact_url_2', sa.String(length=300), nullable=True))
    op.add_column('piece', sa.Column('artifact_url_3', sa.String(length=300), nullable=True))
    op.add_column('piece', sa.Column('artifact_url_4', sa.String(length=300), nullable=True))
    op.drop_column('piece', 'artifact_url')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('piece', sa.Column('artifact_url', mysql.VARCHAR(length=300), nullable=True))
    op.drop_column('piece', 'artifact_url_4')
    op.drop_column('piece', 'artifact_url_3')
    op.drop_column('piece', 'artifact_url_2')
    op.drop_column('piece', 'artifact_url_1')
    # ### end Alembic commands ###


import struct

from refinery.lib.crypto import rotl32

class xxhash:
    PRIME1 = 0x9E3779B1
    PRIME2 = 0x85EBCA77
    PRIME3 = 0xC2B2AE3D
    PRIME4 = 0x27D4EB2F
    PRIME5 = 0x165667B1

    @classmethod
    def xxhround(cls, a, b):
        return (rotl32(a + b * cls.PRIME2 & 0xFFFFFFFF, 13) * cls.PRIME1) & 0xFFFFFFFF

    @classmethod
    def xxhavalance(cls, h32):
        h32 &= 0xFFFFFFFF
        h32 ^= (h32 >> 15)
        h32 *= cls.PRIME2
        h32 &= 0xFFFFFFFF
        h32 ^= (h32 >> 13)
        h32 *= cls.PRIME3
        h32 &= 0xFFFFFFFF
        h32 ^= (h32 >> 16)
        return h32

    def __init__(self, data=B'', seed=0):
        self.seed = seed
        self.reset()
        self.update(data)

    def reset(self):
        seed = self.seed
        self.v1 = seed + self.PRIME1 + self.PRIME2
        self.v2 = seed + self.PRIME2
        self.v3 = seed + 0
        self.v4 = seed - self.PRIME1
        self.total_len = 0
        self.mem = bytearray(16)
        self.memsize = 0

    def update(self, data):
        if not data:
            return
        self.total_len += len(data)
        mv = memoryview(data)
        if self.memsize + len(mv) < 16:
            self.mem[self.memsize:self.memsize + len(mv)] = mv
            self.memsize += len(mv)
            return
        v1, v2, v3, v4 = self.v1, self.v2, self.v3, self.v4
        if self.memsize:
            self.mem[self.memsize:16] = mv[:16 - self.memsize]
            s1, s2, s3, s4 = struct.unpack('<IIII', self.mem)
            v1 = self.xxhround(v1, s1)
            v2 = self.xxhround(v2, s2)
            v3 = self.xxhround(v3, s3)
            v4 = self.xxhround(v4, s4)
            mv = mv[16 - self.memsize:]
            self.memsize = 0
        i = 0
        while i <= len(mv) - 16:
            bfr = mv[i:i + 16]
            if len(bfr) < 16: break
            s1, s2, s3, s4 = struct.unpack('<IIII', bfr)
            v1 = self.xxhround(v1, s1)
            v2 = self.xxhround(v2, s2)
            v3 = self.xxhround(v3, s3)
            v4 = self.xxhround(v4, s4)
            i += 16
        self.memsize = memsize = len(mv) - i
        assert memsize <= 16
        self.mem[:memsize] = mv[i:]
        self.v1, self.v2, self.v3, self.v4 = v1, v2, v3, v4

    def intdigest(self):
        v1, v2, v3, v4 = self.v1, self.v2, self.v3, self.v4
        if self.total_len >= 16:
            h32 = rotl32(v1, 1) + rotl32(v2, 7) + rotl32(v3, 12) + rotl32(v4, 18)
        else:
            h32 = v3 + self.PRIME5
        h32 += self.total_len
        i = 0
        while i <= self.memsize - 4:
            val, = struct.unpack('<I', self.mem[i:i + 4])
            h32 += val * self.PRIME3
            h32 &= 0xFFFFFFFF
            h32 = rotl32(h32, 17) * self.PRIME4
            i += 4
        for c in self.mem[i:self.memsize]:
            h32 += c * self.PRIME5
            h32 &= 0xFFFFFFFF
            h32 = rotl32(h32, 11) * self.PRIME1
        return self.xxhavalance(h32)

    def digest(self):
        return struct.pack('>I', self.intdigest())

    def hexdigest(self):
        return '{:08x}'.format(self.intdigest())


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('Data/veriler.csv')

print(df)

#Kategorik verileri sayısal veriye dönüştürme:
from sklearn import preprocessing

cinsiyet = df.iloc[:,-1:].values
print(cinsiyet)
le = preprocessing.LabelEncoder()

cinsiyet[:,-1] = le.fit_transform(df.iloc[:,-1])

print(cinsiyet)

ohe = preprocessing.OneHotEncoder()
cinsiyet = ohe.fit_transform(cinsiyet).toarray()

df["cinsiyet"] = cinsiyet

from sklearn import preprocessing

ulke = df.iloc[:,0:1].values
le = preprocessing.LabelEncoder()

ulke[:,0] = le.fit_transform(df.iloc[:,0])

print(ulke)

ohe = preprocessing.OneHotEncoder()
ulke = ohe.fit_transform(ulke).toarray()

#Verilerin birleştirilmesi

ulke = pd.DataFrame(data=ulke, columns = ["fr","tr","us"])

df = pd.concat([df,ulke],axis=1)

df = df.drop("ulke",axis=1)

print(df)

#Verileri train ve test olarak ayırma
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(df.drop(["boy","yas","cinsiyet"],axis=1),df["boy"],test_size=0.33,random_state=0)

"""from sklearn.preprocessing import StandardScaler

sc=StandardScaler()

x_train = sc.fit_transform(x_train)
x_test = sc.fit_transform(x_test)"""

#model inşası çoklu doğrusal regresyon
from sklearn.linear_model import LinearRegression

regressor = LinearRegression()
regressor.fit(x_train,y_train)

y_pred = regressor.predict(x_test)

#Model anlamlılık değerleri
import statsmodels.api as sm

X = np.append(arr = np.ones((22,1)).astype(int),values=df,axis=1)

X_l = df.drop(["boy","yas","cinsiyet"],axis=1).values
X_l = np.array(X_l,dtype=float)

model = sm.OLS(df["boy"],X_l).fit()

print(model.summary())


"""
Cleaning, transformation and feature engineering on the scraped data
"""

import pandas as pd
from modules import transformer_tools as tt
import glob

if __name__ == "__main__":
    # Read the scraped raw data from files
    berichte = pd.concat(
        [pd.read_csv(file, sep=";", header="infer", encoding="UTF-8") for file in glob.glob(r"../out/ffm_news*.csv")])

    # Drop rows where words are included in the filter/not wanted (remove news that are not crime related)
    topic_list = tt.get_topicfilter_list()
    berichte = tt.filter_topics(berichte, "Ueberschrift", topic_list)

    # Drop rows where the main article consists of less than 250 characters
    berichte = berichte[berichte["Hauptartikel"].str.len() > 250]

    # Parse timestamp strings as datetime
    berichte["Timestamp"] = tt.parse_timestamp(berichte["Timestamp"])

    # Clean headlines
    berichte["Ueberschrift"] = tt.clean_headline(berichte["Ueberschrift"])

    # Drop rows with NANs
    berichte = berichte.dropna()
    # Rearrange index
    berichte.index = range(len(berichte))

    # Extract locations
    berichte["Ort"], berichte["Sekundaerer Ort"] = tt.extract_location_from_headline(berichte["Ueberschrift"])

    # Get locations only from news that are younger because they have a more reliable format
    Loc_Tuple = tt.get_tuple_of_locations(berichte["Ort"][berichte["Year"] > 2008])
    print(Loc_Tuple)

    berichte["Loc1"], berichte["Loc2"], berichte["Loc3"], berichte["Loc4"] = tt.get_locations_by_tuple(
        berichte["Ueberschrift"], Loc_Tuple)

    # Remove location from headline
    berichte["Ueberschrift"] = berichte["Ueberschrift"].str.replace(' |'.join(list(Loc_Tuple)), '')

    # Extract author from description
    berichte["Author"] = tt.extract_author(berichte["Hauptartikel"])

    # Extract author from the end of the article
    berichte.index = range(len(berichte))
    berichte["Author2"] = tt.extract_author_from_end(berichte["Hauptartikel"])

    # Extract the authors from the end in a new column to remove them from the main article
    authors = berichte['Hauptartikel'].str[-40:-1].str.extract(r"\((.*?)\)", expand=False)
    authors = authors.fillna("")
    berichte['Hauptartikel'] = tt.remove_word_from_other_column(berichte['Hauptartikel'], authors)

    # Remove all dates from headline
    berichte["Ueberschrift"] = berichte["Ueberschrift"].str.replace(r'\d+', '')

    # Clean headline
    berichte["Ueberschrift_clean"] = tt.clean_column(berichte["Ueberschrift"])

    # Lemmatize words in headline text
    berichte["Ueberschrift_lem"] = tt.lemmatize_document_list(berichte["Ueberschrift_clean"])
    # Remove stopwords from headline
    berichte["Ueberschrift_lem_no_stop"] = berichte["Ueberschrift_lem"].apply(tt.remove_stopwords)

    # Split compound words
    berichte["Ueberschrift_split"] = tt.split_compound_words(berichte["Ueberschrift_lem_no_stop"])
    # Clean headline
    berichte["Ueberschrift_clean2"] = tt.clean_column(berichte["Ueberschrift_split"])
    # Lemmatize words in headline text
    berichte["Ueberschrift_lem2"] = tt.lemmatize_document_list(berichte["Ueberschrift_clean2"])
    # Clean headline again after lemmatization
    berichte["Ueberschrift_lem_clean2"] = tt.clean_column(berichte["Ueberschrift_lem2"])

    berichte["Ueberschrift_lem_clean_no_stop"] = berichte["Ueberschrift_lem_clean2"].apply(tt.remove_stopwords)
    # Combine with original Headline
    berichte["Ueberschrift_kombi"] = berichte["Ueberschrift_lem_clean_no_stop"] + " " + berichte[
        "Ueberschrift_lem_no_stop"]
    # Remove duplicates from Uberschirft_kombi
    berichte["Ueberschrift_kombi"] = berichte["Ueberschrift_kombi"].str.split(' ').apply(set).str.join(' ')

    # Clean main article
    berichte["Hauptartikel_clean"] = tt.clean_column(berichte["Hauptartikel"])
    # Lemmatize words in main article text
    berichte["Hauptartikel_lem"] = tt.lemmatize_document_list(berichte["Hauptartikel_clean"])
    # Clean main article again after lemmatization
    berichte["Hauptartikel_lemm_clean"] = tt.clean_column(berichte["Hauptartikel_lem"])
    # Remove stopwords from main article
    berichte["Hauptartikel_lem_clean_no_stop"] = berichte["Hauptartikel_lemm_clean"].apply(tt.remove_stopwords)
    # Remove author from main article
    berichte['Hauptartikel_lem_clean_no_stop'] = berichte['Hauptartikel_lem_clean_no_stop'].replace(
        to_replace=r'\b' + berichte['Author'] + r'\b', value='', regex=True)

    for i, (name, group) in enumerate(berichte.groupby('Year')):
        group.to_csv(r"../out/Polizeiberichte_transformed{}.csv".format(i),
                     sep=";",
                     encoding="UTF-8",
                     header=True,
                     quotechar='"',
                     index=False)


import datetime
import simplejson as json
from django.contrib.sites.shortcuts import get_current_site

from accounts.utils import send_notification
from vendor.models import Vendor

from .models import OrderedFood, Payment

def generate_order_number(pk):
    current_datetime = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    order_number = current_datetime + str(pk)
    return order_number

def save_payment_infos(user, transaction_id, payment_method, amount, status):
    payment = Payment.objects.create(
        user=user,
        transaction_id=transaction_id,
        payment_method=payment_method,
        amount=amount,
        status=status
    )
    payment.save()
    return payment

def add_payment_to_order(order, payment):
    order.payment = payment
    order.is_ordered = True
    order.save()
    return order

def move_cart_items_to_ordered_food(cart_items, order, payment, user):
    for item in cart_items:
            ordered_food = OrderedFood()
            ordered_food.order = order
            ordered_food.payment = payment
            ordered_food.user = user
            ordered_food.fooditem = item.fooditem
            ordered_food.quantity = item.quantity
            ordered_food.price = item.fooditem.price
            ordered_food.amount = item.fooditem.price * item.quantity # total amount
            ordered_food.save()

def send_order_confirmation_email_to_customer(request, order):
    mail_subject = 'Thank you for ordering with us'
    email_template = 'orders/order_confirmation_email.html'

    ordered_food = OrderedFood.objects.filter(order=order)
    customer_subtotal = 0
    for item in ordered_food:
        customer_subtotal += item.price * item.quantity
    tax_data = json.loads(order.tax_data)
    context = {
        'user': request.user,
        'order': order,
        'to_email': order.email,
        'ordered_food': ordered_food,
        'domain': get_current_site(request),
        'customer_subtotal': customer_subtotal,
        'tax_data': tax_data,
    }
    send_notification(mail_subject, email_template, context)

def send_order_received_email_to_vendor(request, cart_items, order):
    mail_subject = 'You have received a new order'
    email_template = 'orders/new_order_received.html'
    to_emails = []
    for i in cart_items:
        if i.fooditem.vendor.user.email not in to_emails:
            to_emails.append(i.fooditem.vendor.user.email)

            ordered_food_to_vendor = OrderedFood.objects.filter(order=order, fooditem__vendor=i.fooditem.vendor)
            # print(ordered_food_to_vendor)

            context = {
                'order': order,
                'vendor_name':i.fooditem.vendor.vendor_name,
                'to_email': i.fooditem.vendor.user.email,
                'ordered_food_to_vendor': ordered_food_to_vendor,
                'domain': get_current_site(request),
                'vendor_subtotal': get_order_total_by_vendor(order, i.fooditem.vendor.id)['subtotal'],
                'tax_data': get_order_total_by_vendor(order, i.fooditem.vendor.id)['tax_dict'],
                'vendor_grand_total': get_order_total_by_vendor(order, i.fooditem.vendor.id)['grand_total'],
            }
            send_notification(mail_subject, email_template, context)

def get_order_total_by_vendor(order, vendor_id):
    total_data = json.loads(order.total_data)
    data = total_data.get(str(vendor_id))
    subtotal = 0
    tax = 0
    tax_dict = {}

    for key, val in data.items():
        subtotal += float(key)
        val = val.replace("'",'"')
        val = json.loads(val)
        tax_dict.update(val)

        # calculate tax
        # {'TVA': {'8.00': '672'}}
        for i in val:
            for j in val[i]:
                tax += float(val[i][j])

    grand_total = float(subtotal) + float(tax)

    context = {
        'subtotal': subtotal,
        'tax_dict': tax_dict,
        'grand_total': grand_total,
    }
    return context


from collections import defaultdict
from heapq import heappop, heappush
import sys

def input():
    return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    prime_factorizations = defaultdict(list)

    for i in range(N):
        m = int(input())
        for _ in range(m):
            p, cnt = map(int, input().split())
            heappush(prime_factorizations[p], [-cnt, i])

    responsibles = set()
    for p, heap_p in prime_factorizations.items():

        maxcnt, idx = heappop(heap_p)
        if heap_p:
            secondcnt, _ = heappop(heap_p)
            if maxcnt == secondcnt:
                continue

        responsibles.add(idx)

    print(min(len(responsibles) + 1, N))

if __name__ == '__main__':
    main()


import pytest
import pandas as pd
import numpy as np

from prepnet.normalize.quantile_round import QuantileRound
from prepnet.normalize.standardize import Standardize

@pytest.mark.parametrize(('input', 'expected', 'quantile'), [
    (
        [10.0, 200.0, 40.0, 20.0, 15.0, 18.0],
        [15.0, 40.0, 40.0, 20.0, 15.0, 18.0],
        0.8
    )
])
def test_quantile_round(input, expected, quantile):
    input_series = pd.Series(input)
    expected_series = pd.Series(expected)

    converter = QuantileRound(quantile)
    output_series = converter.encode(input_series)
    pd.testing.assert_series_equal(expected_series, output_series)

    reconstruct_series = converter.decode(output_series)
    pd.testing.assert_series_equal(input_series, reconstruct_series)

@pytest.mark.parametrize(('mu', 'sigma'), [
    (20.0, 1.0),
    (1.0, 7.0),
    (4.0, 10.0),
])
def test_standardise(mu, sigma):
    input_series = pd.Series(np.random.normal(mu, sigma, size=(100000,)))

    converter = Standardize()
    output_series = converter.encode(input_series)
    out_mu = np.mean(output_series)
    out_sigma = np.std(output_series)
    assert out_mu == pytest.approx(0, rel=1e-1) and out_sigma == pytest.approx(1, rel=1e-1)

    reconstruct_series = converter.decode(output_series)
    reconstruct_mu = np.mean(reconstruct_series)
    reconstruct_sigma = np.std(reconstruct_series)
    assert reconstruct_mu == pytest.approx(mu, rel=1e-1) and reconstruct_sigma == pytest.approx(sigma, rel=1e-1)


from datetime import datetime

from dateutil.parser import parse
import pymongo

from operationsgateway_api.src.exceptions import QueryParameterError

class ParameterHandler:
    @staticmethod
    def extract_order_data(orders):
        """
        Given a string of the order portion of a MongoDB query, put it into a format
        that PyMongo can understand

        An example input string: `[channel_name.title asc, shotnum desc]`
        """

        sort_data = []

        for order in orders:
            field = order.split(" ")[0]
            direction = order.split(" ")[1]

            if direction.lower() == "asc":
                direction = pymongo.ASCENDING
            elif direction.lower() == "desc":
                direction = pymongo.DESCENDING
            else:
                raise ValueError(
                    "Invalid direction given in order parameter, please try again",
                )

            sort_data.append((field, direction))

        return sort_data

    @staticmethod
    def encode_date_for_conditions(value):
        new_date = None

        if isinstance(value, dict):
            for inner_key, inner_value in value.items():
                new_new_date = ParameterHandler.encode_date_for_conditions(inner_value)
                if new_new_date is not None:
                    value[inner_key] = new_new_date
        elif isinstance(value, list):
            for element in value:
                ParameterHandler.encode_date_for_conditions(element)
        elif isinstance(value, str):
            try:
                parse(value, fuzzy=False)
            except ValueError:
                # Not a date, nothing to do here
                return

            try:
                new_date = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
            except ValueError as exc:
                raise QueryParameterError(
                    "Incorrect date format used in query parameter. Use"
                    " %Y-%m-%dT%H:%M:%S to filter by datetimes",
                ) from exc

            return new_date


'''
https://adventofcode.com/2019/day/17
'''
DAY = 17

from utils import *
from icc import intCodeComputer
from collections import defaultdict, deque
# from time import sleep

SHOW_MAP = False
#        NORTH   SOUTH    WEST     EAST
STEPS = [(0, 1), (0, -1), (-1, 0), (1, 0) ]
OPPOSITE = [0, 2, 1, 4, 3]

def get_cameras(program):
    software = intCodeComputer(program, id=1, verbose=False)
    software.compute()
    ascii_out = software.outputArray

    # Convert the ascii codes to the visible image
    line = ''.join( [chr(a) for a in ascii_out] )
    # print(line)

    # Generate the mapp dict
    x = 0
    y = 0
    mapp = {}
    for a in ascii_out:
        if a == 10: # newline
            y += 1
            x = 0
            continue
        mapp[(x, y)] = chr(a)
        x += 1
    return mapp

def generate_graph(mapp):
    graph = defaultdict(list)

    for point, value in mapp.items():
        if value not in {'#', '^', 'v', '<', '>'}: # empty point
            continue

        if value in {'^', 'v', '<', '>'}:
            roomba = point

        for step in STEPS:
            adjacent = addc(point, step)
            adj_value = mapp.get(adjacent, '.')
            if adj_value in {'#', '^', 'v', '<', '>'}:
                graph[point].append(adjacent)

    return graph, roomba

def part1(program):
    mapp = get_cameras(program)

    # Get the intersection points
    intersections = []
    for point, value in mapp.items():
        if value not in {'#', '^', 'v', '<', '>'}: # empty point
            continue

        for step in STEPS:
            adjacent = addc(point, step)
            adj_value = mapp.get(adjacent, '.')
            if adj_value == '.':
                break
        else: #nobreak => its an interesection
            intersections.append( point )

    # print(f'Intersection points: {intersections} ({len(intersections)})')
    return sum( [a[0]*a[1] for a in intersections] )

def part2(program):

    # Start with the roomba movements
    software = intCodeComputer(program, id=2, verbose=False)
    # Wake the roomba
    software.memory[0] = 2

    # Program the roomba
    # Main movement
    main_mov = 'A,B,A,C,B,C,B,C,A,C'
    # Movement functions
    A = 'L,10,R,12,R,12'
    B = 'R,6,R,10,L,10'
    C = 'R,10,L,10,L,12,R,6'

    # Video feed
    video = 'n'

    roomba = '\n'.join([main_mov, A, B, C, video, ''])
    roomba_ascii = [ord(a) for a in roomba]

    # Upload the roomba code
    software.inputArray = roomba_ascii

    # Run the roomba
    software.compute()

    # Video
    ascii_out = software.outputArray

    # Convert the ascii codes to the visible image
    line = ''.join( [chr(a) for a in ascii_out] )
    dust = ascii_out[-1]
    # print(line)
    # print(f'Dust: {dust}')
    return dust

def main():
    input = Input(DAY, 2019, line_parser = integers)
    program = list(input[0])

    result_1 = part1(program)
    print(f'Part 1: {result_1}')

    print('')
    result_2 = part2(program)
    print(f'Part 2: {result_2}')
    # print(f'Part 2: {result_2}')

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
from stark.service.v1 import StarkHandler, get_m2m_text, get_choice_text, StarkModelForm
from web import models

class PrivateCustomerModelForm(StarkModelForm):
    class Meta:
        model = models.Customer
        exclude = ['consultant', ]

class PrivateCustomerHandler(StarkHandler):
    model_form_class = PrivateCustomerModelForm
    list_display = [StarkHandler.display_checkbox, 'name', 'qq', get_m2m_text('咨询课程', 'course'),
                    get_choice_text('状态', 'status')]

    def get_queryset(self, request, *args, **kwargs):
        current_user_id = request.session['user_info']['id']
        return self.model_class.objects.filter(consultant_id=current_user_id)

    def save(self, request, form, is_update, *args, **kwargs):
        if not is_update:
            current_user_id = request.session['user_info']['id']
            form.instance.consultant_id = current_user_id
        form.save()


"""Descent Gradient Methods for the Logistic regression
major update 15.11.2022"""

import numpy as np
import autograd.numpy as np
from autograd import grad

from func import*
from optimizers import *

#
# def GD(X_train, X_test, y_train, y_test, method, Niterations, init_LR, decay, momentum, seed):
#
#     """ Gradient Decent """
#     np.random.seed(seed)
#     theta = np.random.randn(np.shape(X_train)[1],1) # Initial thetas/betas
#     change = 0.0
#     mse = np.zeros(Niterations)
#     # Gradient decent:
#     for i in range(Niterations):
#
#         if method == 'auto':
#             gradients =  auto_gradient(theta, X_train, y_train) # Autograd
#         if method == 'anal':
#             gradients = gradient_CostOLS(theta, X_train, y_train) # Analytical
#
#         eta = learning_schedule(i, init_LR, decay) # LR
#         update = eta * gradients + momentum * change # Update to the thetas
#         theta -= update # Updating the thetas
#
#         y_predict_GD_test = X_test @ theta
#         mse[i] = MSE(y_test, y_predict_GD_test)
#         change = update # Update the amount the momentum gets added
#
#     y_predict_GD = X_test @ theta
#
#     return y_predict_GD, theta, mse

def SGD(X_train, X_test, y_train, y_test, Optimizer_method, Gradient_method, minibatch_size, n_minibatches, n_epochs, init_LR, decay, momentum, seed, lmb):

    """ Stochastic Gradient Decent """
    np.random.seed(seed)
    theta = np.random.randn(X_train.shape[1], 1) # Initial thetas/betas
    change = 0.0
    """List of mini batches:"""
    X_Batcharray = [] #empty list of batch array
    Y_Batcharray = [] #empty list of batch array
    for bat in range(n_minibatches):
        random_index = minibatch_size*np.random.randint(n_minibatches)
        #print(n_minibatches)
        X_Batcharray.append(X_train[random_index:random_index + minibatch_size])
        Y_Batcharray.append(y_train[random_index:random_index + minibatch_size])

    cost = np.zeros(n_epochs*n_minibatches) #Logistic regression cost.
    count = 0
    eta = init_LR

    """ Optimizer method """
    if Optimizer_method == 'Adagrad':
        optim = Adagrad(eta)
    if Optimizer_method == 'RMSprop':
        optim = RMSprop(eta)
    if Optimizer_method == 'Adam':
        optim = Adam(eta)

    for epoch in range(n_epochs):
        Giter = np.zeros(shape=(X_train.shape[1],X_train.shape[1]))

        for batch in range(n_minibatches):
            X_batch = X_Batcharray[batch]
            y_batch = Y_Batcharray[batch]

            """ Gradient method for logistic"""
            if Gradient_method == 'log':
                gradients = grad_cost_func_logregression(theta, X_batch, y_batch, lmb)

            """ Optimizer method """
            if Optimizer_method == 'Adagrad':
                update = optim(gradients, Giter)#uses class
                theta -= update

            if Optimizer_method == 'RMSprop':
                update = optim(gradients, Giter)#uses class
                theta -= update

            if Optimizer_method == 'Adam':
                update = optim(gradients)
                theta -= update

            if Optimizer_method == 'momentum':
                #gradients = grad_cost_func_logreg(theta, X_batch, y_batch)

                eta = learning_schedule(epoch, init_LR, decay) # LR
                update = eta * gradients + momentum * change # Update to the thetas
                # if count == 0:
                #     print('\nThetas for SGD:')
                # if count % 200 == 0:
                #     print(theta.flatten(), f'iter = {count}')
                theta -= update
                change = update

            y_predict_GD_test = 1/(1+np.exp(-X_test @ theta))

            cost[count] = cross_entropy_cost(y_test, y_predict_GD_test, lmb, theta)

            count +=1

    """logistic classification on test:"""
    Prob = 1/(1+np.exp(- X_test @ theta))
    y_pred = Prob.copy()
    tol = 0.50
    y_pred[y_pred >= tol] = 1
    y_pred[y_pred < tol] = 0
    acc_score = accuracy(y_pred,y_test)

    return y_pred, Prob, theta, cost, acc_score

Project 2 - Classification and Regression, from linear and logistic regression to neural networks/src/Logistic Regression and Neural Netword Logistic (task d & e)/descent_methods_logistic.py


import codecs
from typing import Container, Optional, Tuple

from ._qpdf import pdf_doc_to_utf8, utf8_to_pdf_doc

# pylint: disable=redefined-builtin

# See PDF Reference Manual 1.7, Table D.2. We record every Unicode code point
# that can be encoded to pdfdoc,
# except for: [0x2D8, 0x2C7, 0x2C6, 0x2D9, 0x2DD, 0x2DB, 0x2DA, 0x2DC]
# which qpdf does not seem to consider as encodable even though the PDF RM does.
# Follow qpdf is more important for consistency.
PDFDOC_ENCODABLE = frozenset(
    list(range(0x00, 0x17 + 1))
    + list(range(0x20, 0x7E + 1))
    + [
        0x2022,
        0x2020,
        0x2021,
        0x2026,
        0x2014,
        0x2013,
        0x0192,
        0x2044,
        0x2039,
        0x203A,
        0x2212,
        0x2030,
        0x201E,
        0x201C,
        0x201D,
        0x2018,
        0x2019,
        0x201A,
        0x2122,
        0xFB01,
        0xFB02,
        0x0141,
        0x0152,
        0x0160,
        0x0178,
        0x017D,
        0x0131,
        0x0142,
        0x0153,
        0x0161,
        0x017E,
        0xFFFD,
        0x20AC,
    ]
    + list(range(0xA1, 0xAC + 1))
    + list(range(0xAE, 0xFF + 1))
)

def _find_first_index(
    s: str, ordinals: Container[int], is_whitelist: bool = True
) -> int:
    for n, char in enumerate(s):
        if is_whitelist and (ord(char) not in ordinals):
            return n
        if not is_whitelist and (ord(char) in ordinals):
            return n  # pragma: no cover
    raise ValueError("couldn't find the unencodable character")  # pragma: no cover

def pdfdoc_encode(input: str, errors: str = 'strict') -> Tuple[bytes, int]:
    error_marker = b'?' if errors == 'replace' else b'\xad'
    try:
        success, pdfdoc = utf8_to_pdf_doc(input, error_marker)
    except RuntimeError as e:  # pragma: no cover
        # In pybind11 < 2.7.0, a RuntimeError occurs when attempting to handle
        # Unicode surrogates. Looks like:
        #   "Unable to extract string contents! (encoding issue)"
        # This appears to be fixed for pybind11 >= 2.7.0.
        offending_index = _find_first_index(
            input, range(0xD800, 0xDFFF + 1), is_whitelist=False
        )

        raise UnicodeEncodeError(
            'pdfdoc',
            input,
            offending_index,
            offending_index + 1,
            "can't process Unicode surrogates",
        ) from e
    if not success:
        if errors == 'strict':
            # libqpdf doesn't return what character caused the error, and Python
            # needs this, so make an educated guess and raise an exception based
            # on that.
            offending_index = _find_first_index(input, PDFDOC_ENCODABLE)
            raise UnicodeEncodeError(
                'pdfdoc',
                input,
                offending_index,
                offending_index + 1,
                "character cannot be represented in pdfdoc encoding",
            )

        if errors == 'ignore':
            pdfdoc = pdfdoc.replace(b'\xad', b'')
    return pdfdoc, len(input)

def pdfdoc_decode(input: bytes, errors: str = 'strict') -> Tuple[str, int]:
    del errors  # silence pylint warning; all bytes objects have a pdfdoc decoding
    if isinstance(input, memoryview):
        input = input.tobytes()
    utf8 = pdf_doc_to_utf8(input)
    return utf8, len(input)

class PdfDocCodec(codecs.Codec):
    """Implements PdfDocEncoding character map used inside PDFs."""

    def encode(self, input: str, errors: str = 'strict') -> Tuple[bytes, int]:
        return pdfdoc_encode(input, errors)

    def decode(self, input: bytes, errors: str = 'strict') -> Tuple[str, int]:
        return pdfdoc_decode(input, errors)

class PdfDocStreamWriter(PdfDocCodec, codecs.StreamWriter):
    pass

class PdfDocStreamReader(PdfDocCodec, codecs.StreamReader):
    def decode(self, input: bytes, errors: str = 'strict'):
        return PdfDocCodec.decode(self, input, errors)

class PdfDocIncrementalEncoder(codecs.IncrementalEncoder):
    def encode(self, input: str, final=False):
        return pdfdoc_encode(input, 'strict')[0]

class PdfDocIncrementalDecoder(codecs.IncrementalDecoder):
    def decode(self, input: bytes, final=False):
        return pdfdoc_decode(input, 'strict')[0]

def find_pdfdoc(encoding: str) -> Optional[codecs.CodecInfo]:
    if encoding in ('pdfdoc', 'pdfdoc_pikepdf'):
        codec = PdfDocCodec()
        return codecs.CodecInfo(
            name=encoding,
            encode=codec.encode,
            decode=codec.decode,
            streamwriter=PdfDocStreamWriter,
            streamreader=PdfDocStreamReader,
            incrementalencoder=PdfDocIncrementalEncoder,
            incrementaldecoder=PdfDocIncrementalDecoder,
        )
    return None  # pragma: no cover

codecs.register(find_pdfdoc)

__all__ = ['utf8_to_pdf_doc', 'pdf_doc_to_utf8']


import logging
from aiogram import Bot, Dispatcher, executor, types
import asyncio

import config as cfg
import markups as nav

logging.basicConfig(level=logging.INFO)
bot = Bot(token=cfg.TOKEN)
dp = Dispatcher(bot)

@dp.message_handler(content_types=['new_chat_members', 'left_chat_member'])
async def on_user_joined(message: types.Message):
    await message.delete()

def check_sub_channel(chat_member):
    return chat_member['status'] != 'left'

@dp.message_handler(content_types=['new_chat_members'])
async def user_joined(message: types.Message):
    await message.answer("Salom bizning chat guruhimizga xush kelibsiz!\nGuruhda yozish uchun bizning kanalimizga a\'zo bo\'lib qo\'ying",reply_markup=nav.channelMenu)

@dp.message_handler(content_types = ['new_chat_members', 'left_chat_member'])
async def delete(message):
    await message.delete(message.chat.id, message.message_id)

@dp.message_handler()
async def mess_handler(message:types.Message):
    if check_sub_channel(await bot.get_chat_member(chat_id=cfg.CHANNEL_ID, user_id=message.from_user.id)):
        text = message.text.lower()
        for word in cfg.WORDS:
            if word in text:
                await message.delete()
    else:
        await message.answer("Xabar yozish uchun kanalga a\'zo bo\'ling",reply_markup=nav.channelMenu)
        await message.delete()
asyncio.run(main())


import mimetypes

import magic
import mutagen

from funkwhale_api.common.search import normalize_query, get_query  # noqa

def guess_mimetype(f):
    b = min(1000000, f.size)
    t = magic.from_buffer(f.read(b), mime=True)
    if not t.startswith("audio/"):
        # failure, we try guessing by extension
        mt, _ = mimetypes.guess_type(f.path)
        if mt:
            t = mt
    return t

def compute_status(jobs):
    statuses = jobs.order_by().values_list("status", flat=True).distinct()
    errored = any([status == "errored" for status in statuses])
    if errored:
        return "errored"
    pending = any([status == "pending" for status in statuses])
    if pending:
        return "pending"
    return "finished"

AUDIO_EXTENSIONS_AND_MIMETYPE = [
    ("ogg", "audio/ogg"),
    ("mp3", "audio/mpeg"),
    ("flac", "audio/x-flac"),
]

EXTENSION_TO_MIMETYPE = {ext: mt for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}
MIMETYPE_TO_EXTENSION = {mt: ext for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}

def get_ext_from_type(mimetype):
    return MIMETYPE_TO_EXTENSION.get(mimetype)

def get_type_from_ext(extension):
    if extension.startswith("."):
        # we remove leading dot
        extension = extension[1:]
    return EXTENSION_TO_MIMETYPE.get(extension)

def get_audio_file_data(f):
    data = mutagen.File(f)
    if not data:
        return
    d = {}
    d["bitrate"] = getattr(data.info, "bitrate", 0)
    d["length"] = data.info.length

    return d

def get_actor_from_request(request):
    actor = None
    if hasattr(request, "actor"):
        actor = request.actor
    elif request.user.is_authenticated:
        actor = request.user.actor

    return actor


import numpy as np
import dynesty.utils as dyutil
import multiprocessing as mp
import scipy.special
import dynesty, dynesty.pool
import contextlib
import math

mv_sun = 4.81

class KirbyMassMetInfo:
    """
    Class representing the mass metallicity relation
    """

    def mass_met(log10l):
        """ avg feh at a given log10 (lum)"""
        return -1.68 + 0.29 * (log10l - 6)

    def log10_sig(log10l):
        """ log10 of MDF width at a given log10(lum) """
        return np.log10(0.45 - 0.06 * (log10l - 5))  # np.minimum(logl, -0.2)

    def mass_met_spread():
        """ Spread of the mass metallicity relation"""
        return 0.15

def get_weights(n_inbin, nmax_distinct):
    """
    Populate the 2d occupancy matrix, to pick up random fe/h in each
    logl bin according to the mass metallicity spread
    """
    nbins = len(n_inbin)
    weights = np.zeros((nbins, nmax_distinct), dtype=int)

    xind = n_inbin <= nmax_distinct
    weights[xind, :] = np.arange(nmax_distinct)[None, :] < n_inbin[xind][:,
                                                                         None]
    xind1 = ~xind

    weights[xind1, :] = n_inbin[xind1][:, None] // nmax_distinct + (
        ((n_inbin[xind1] % nmax_distinct)[:, None]) >
        np.arange(nmax_distinct)[None, :]).astype(int)
    assert (weights.sum(axis=1) == n_inbin).all()
    return weights

def evaler(xvals,
           logl_bins,
           n_inbin,
           rng=None,
           feh_means=None,
           nmax_distinct=200,
           mass_met_info=None):
    nbins = len(logl_bins)
    if rng is not None and feh_means is None:
        eps = rng.normal(size=(nbins, nmax_distinct))
        feh_means0 = mass_met_info.mass_met(logl_bins)
        feh_means = feh_means0[:, None] + eps * mass_met_info.mass_met_spread()
    elif feh_means is not None and rng is None:
        nmax_distinct = 1
        pass
    else:
        raise RuntimeError('feh_means and rng are both set or unset')
    feh_widths = 10**(mass_met_info.log10_sig(logl_bins)[:, None])
    nstars = 10**logl_bins
    weights = get_weights(n_inbin, nmax_distinct)
    mult = (weights) * nstars[:, None] / np.sqrt(2 * np.pi) / feh_widths
    # these are multipliers in front of exp(-((feh-mu)/sig)^2)
    # this assumes that feh_width is the same for every galaxy

    # may need to renormalize if considering restricted range of fehs
    # may need to sum to one

    # making all gaussian param 1ds
    mult = mult.reshape(-1)
    feh_means = feh_means.reshape(-1)
    feh_widths = (feh_widths[:, None] + np.zeros(
        (1, nmax_distinct))).reshape(-1)
    xind = mult > 0
    if not xind.any():
        return xvals * 0 - 1e100, -1e100
    logp = (np.log(mult[None, xind]) - 0.5 * ((
        (xvals[:, None] - feh_means[None, xind]) / feh_widths[None, xind])**2))
    ret = scipy.special.logsumexp(logp, axis=1)
    return ret, 0

class CubeTransf:
    """
    Class that is transform the parameter space into unit cube
    needed for nested sampling
    """

    def __init__(
            self,
            maxlogn=3,
            nseed=1000,
            minlogl=1.9,
            maxlogl=8  # logl range for the most massive system)
    ):
        self.minlogl = minlogl
        self.maxlogl = maxlogl
        self.nseed = nseed
        self.maxlogn = maxlogn

    def __call__(self, x):
        """
        Apply the actual cube transform
        """
        # first two dimensions are feh, logl then are the occupation numbers
        # followed by the seed
        nbins = len(x) - 3
        minfeh = -4
        maxfeh = 0.5
        feh = minfeh + (maxfeh - minfeh) * x[0]
        logl = self.minlogl + (self.maxlogl - self.minlogl) * x[1]
        ns = 10**(-1 + (self.maxlogn + 1) * x[2:nbins + 2])
        # seed = x[nbins]
        seed = (x[nbins + 2] * self.nseed)
        return np.concatenate(([feh, logl], ns, [seed]))

def logp(p, data, getModel=False):
    """
    Evaluate the logprobability of data given parameter vector
    """
    (fehs, logl_bins, logl0, loglerr, nmax_distinct, mass_met_info, prior_only,
     minfeh, maxfeh) = data
    nbins = len(logl_bins)
    sections = [2, nbins, 1]
    (feh_ref,
     logl_ref), xparam, seed0 = np.array_split(p,
                                               np.cumsum(sections)[:-1])
    nfehbins = 100
    xvals = np.linspace(minfeh, maxfeh, nfehbins)
    # interpolation grid of metallicities
    n_inbin = xparam.astype(int)
    if seed0 < 0:
        return -1e100
    seed0 = seed0[0]
    seed = seed0.astype(int)
    # seed = [int(_) for _ in (seed0.tobytes())]
    totlum = np.log10(np.sum(n_inbin * 10**logl_bins) + 10**logl_ref)

    # avoid plateau in logl
    penalty = (n_inbin - xparam).sum() * 0.001
    penalty += (seed - seed0) * .001

    penalty += -0.5 * ((totlum - logl0) / loglerr)**2
    penalty += -0.5 * ((feh_ref - mass_met_info.mass_met(logl_ref)) /
                       mass_met_info.mass_met_spread())**2
    if prior_only:
        return penalty
    rng = np.random.default_rng(seed)
    logp0, penalty1 = evaler(xvals,
                             logl_bins,
                             n_inbin,
                             rng=rng,
                             nmax_distinct=nmax_distinct,
                             mass_met_info=mass_met_info)
    penalty += penalty1
    logp_ref, _ = evaler(xvals,
                         np.r_[logl_ref],
                         np.r_[1],
                         feh_means=np.r_[feh_ref],
                         mass_met_info=mass_met_info)
    II = scipy.interpolate.InterpolatedUnivariateSpline(xvals, logp0, k=1)
    II_ref = scipy.interpolate.InterpolatedUnivariateSpline(xvals,
                                                            logp_ref,
                                                            k=1)
    norm = (np.exp(II(xvals)).sum() +
            np.exp(II_ref(xvals)).sum()) * (xvals[1] - xvals[0])
    logp1 = np.logaddexp(II(fehs), II_ref(fehs)) - np.log(norm)
    if getModel:
        return logp1
    return np.sum(logp1) + penalty

def doit(
    fehs,
    curmv,
    npar=None,
    nthreads=36,
    nlive=10000,
    neff=None,
    steps=100,
    rstate=1,
    maxlogn=3,  # max(log(occupation number in each logl bin))
    magpad=2.5,  # how far away to go in luminosity above
    mv_err=0.1,  # luminosity uncertainty
    minlogl=None,  # minimum log(L) in the grid
    maxlogl=None,  # maximum log(L) in the grid
    mass_met_info=None,
    # a class with 3 functions mass_met(log10l), mass_met_spread()
    # and log10_sig(log10l)
    # that return mean feh at log10l, spread in mass met relation and
    # log10 width of the mdf
    prior_only=False,
    minfeh=-4,  # minimum feh in the data/truncation
    maxfeh=0.5  # maximum feh in the data/truncation
):
    """
    Model  the MDF as combination of MDF of lower luminosity
    objects

    Parameters
    ----
    fehs: array
        Array of [Fe/H] of modelled stars
    curmv: absolute luminosity of the system
    npar: int or None
        The number of luminosity bins. If none, the number of bins
        is decided automatically
    nthreads: int
        The number of threads needed for sampling
    nlive: int
        The number of live-points for nested sampling
    neff: int
        The desired number of effective samples in the posterior
    steps: int
        The number of MCMC walk steps used at each iteration of
        nested sampling
    rstate: int or RandomGenerator
        The initialization of the random state generator
    maxlogn: int
        The maximum value of the log of the occupation number in each
        luminosity bin
    magpad: float
        How much brighter than the luminisity of the system the bins should go
    mv_err: float
        The uncertainty in the MV of the system
    minlogl: float or None
        minimum log(L) in the grid (if None, it's chosen automatically)
    maxlogl: float or None,
        maximum log(L) in the grid
    mass_met_info: MassMetInfo class or None
        a class instance that should have
        3 methods mass_met(log10l), mass_met_spread(), and log10_sig(log10l)
        that return mean feh at log10l, spread in mass met relation and
        log10 width of the mdf
    prior_only: boolean
        if True on the the prior is sampled, the data is ignored
    minfeh: float
        The minimum [Fe/H] in the data/truncation
    maxfeh: float
        The maximum [Fe/H] in the data/truncation

    Returns
    ------
    ret: dict
         Dictionary logl grid and posterior samples
         Keys are
         logl_grid: the logl grid
         samp: The raw posterior samples
         samp_n: The posterior samples for just the occupation numbers
               in logl bins
         n_16, cumul_n16, n84...: the percentiles of the occupation
               numbers  in bins (or for cumulativae number of dwarfs equal
               or brighter than a luminosity of the bin)
    """
    curlogl = -((curmv - mv_sun) / 2.5)
    loglerr = mv_err / 2.5

    if maxlogl is None:
        maxlogl = curlogl + magpad / 2.5
    if minlogl is None:
        minlogl = 1.9
    default_step_mag = 1
    if npar is None:
        npar = math.ceil((maxlogl - minlogl) / (default_step_mag / 2.5))
    # this is to ensure we have a point on curlogl
    #     step = (curlogl - minlogl) / int(npar * (curlogl - minlogl) /
    #                                     (maxlogl - minlogl))
    step = (maxlogl - minlogl) / (npar - 1)
    logl_grid = np.arange(npar) * step + minlogl
    nmax_distinct = 200
    logl_args = (fehs, logl_grid, curlogl, loglerr, nmax_distinct,
                 mass_met_info, prior_only, minfeh, maxfeh)
    # nseed = nlive
    nseed = 2000
    cube_transf = CubeTransf(nseed=nseed,
                             maxlogn=maxlogn,
                             minlogl=minlogl,
                             maxlogl=maxlogl)
    if isinstance(rstate, int):
        rstate = np.random.default_rng(rstate)
    with (dynesty.pool.Pool(
            nthreads, logp, cube_transf, logl_args=(logl_args, ))
          if nthreads > 1 else contextlib.nullcontext()) as pool:
        if nthreads > 1:
            curl, curp, curargs = pool.loglike, pool.prior_transform, None
        else:
            curl, curp, curargs = logp, cube_transf, (logl_args, )
        dns = dynesty.DynamicNestedSampler(
            curl,
            curp,
            npar + 3,
            nlive=nlive,
            rstate=rstate,
            logl_args=curargs,
            pool=pool,
            # sample='unif',
            # sample='rslice',
            walks=steps,
            slices=steps,
            sample='rslice',
        )
        dns.run_nested(n_effective=neff)
    # dlogz_init=0.5, maxbatch=0)
    samp = dyutil.resample_equal(
        dns.results['samples'],
        np.exp(dns.results['logwt'] - dns.results['logz'][-1]))
    xlogl = samp[:, 1] * 1
    xsamp = samp[:, 2:npar + 2].astype(int) * 1
    xind = np.argmin(np.abs(logl_grid[None, :] - xlogl[:, None]), axis=1)
    xsamp[np.arange(len(samp)), xind] += 1
    res = dict(logl_grid=logl_grid, samp=samp, samp_n=xsamp)
    for pp in [1, 5, 16, 50, 84, 95, 99]:
        res['n%d' % pp] = scipy.stats.scoreatpercentile(xsamp, pp, axis=0)
        res['cumul_n%d' % pp] = scipy.stats.scoreatpercentile(np.cumsum(
            xsamp[:, ::-1], axis=1)[:, ::-1],
                                                              pp,
                                                              axis=0)
    return res


import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import base64
#import mplcursors
import numpy as np
import os
import sys
from sklearn.manifold import TSNE
import mpld3
from skimage.transform import resize

from IPython import embed

def tsne_plot(X, images, color, num_clusters=30, perplexity=5, serve_port=8104, html_out_path='mpld3.html'):
    print('computing TSNE')
    Xtsne = TSNE(n_components=2, perplexity=perplexity).fit_transform(X)
    x = Xtsne[:,0]
    y = Xtsne[:,1]
    # get color from kmeans cluster
    #print('computing KMeans clustering')
    #Xclust = KMeans(n_clusters=num_clusters).fit_predict(Xtsne)
    #c = Xclust
    # Create list of image URIs
    html_imgs = []
    print('adding hover images')
    for nidx in range(images.shape[0]):
        f,ax = plt.subplots()
        ax.imshow(resize(images[nidx], (180,180)))
        dd = mpld3.fig_to_dict(f)
        img = dd['axes'][0]['images'][0]['data']
        html = '<img src="data:image/png;base64,{0}">'.format(img)
        html_imgs.append(html)
        plt.close()

    # Define figure and axes, hold on to object since they're needed by mpld3
    fig, ax = plt.subplots(figsize=(8,8))

    # Make scatterplot and label axes, title
    sc = ax.scatter(x,y,s=100,alpha=0.7, c=color, edgecolors='none')
    plt.title("TSNE")

    # Create the mpld3 HTML tooltip plugin
    tooltip = mpld3.plugins.PointHTMLTooltip(sc, html_imgs)
    # Connect the plugin to the matplotlib figure
    mpld3.plugins.connect(fig, tooltip)
    #plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(), plugins.Zoom())
    # Uncomment to save figure to html file
    out=mpld3.fig_to_html(fig)
    print('writing to %s'%html_out_path)
    fpath = open(html_out_path, 'w')
    fpath.write(out)
    # display is used in jupyter
    #mpld3.display()
    mpld3.show(port=serve_port, open_browser=False)


import math
import os

import numpy as np
from pendulum import parse, period
from sdf import X, Y, box, ease, measure_text, rectangle, text, union

import github_poster.skyline
from github_poster.skyline.config import (
    BASE_HEIGHT,
    BASE_LENGTH,
    BASE_TOP_LENGTH,
    BASE_TOP_WIDTH,
    BASE_WIDTH,
    BOX_DIMENSION,
)

class Skyline:
    def __init__(self, file_name, year, skyline_type, number_by_date_dict, user_name):
        self.file_name = file_name
        self.year = year
        self.skyline_type = skyline_type
        self.number_by_date_dict = number_by_date_dict
        self.type_info_dict = None
        assert len(user_name) < 16
        self.user_name = user_name

    def _make_box(self, box_height):
        b = box((BOX_DIMENSION, BOX_DIMENSION, box_height))
        # change to the base location
        b = b.translate(
            (BOX_DIMENSION / 2, BOX_DIMENSION / 2, box_height / 2 + BASE_HEIGHT)
        )
        return b

    def __make_one_year_dates(self):
        dates = list(period(parse(f"{self.year}-01-01"), parse(f"{self.year}-12-31")))
        return [d.to_date_string() for d in dates]

    def _make_skyline_boxes(self):
        box_list = []
        dates = self.__make_one_year_dates()
        # for test
        week_number = 0
        for i, j in enumerate(dates):
            day_number = i % 7
            if day_number == 0:
                week_number += 1
            value = self.number_by_date_dict.get(j)
            if value:
                box = self._make_box(value)
                box = box.translate(
                    (
                        3.5 + 2.5 + (week_number - 1) * BOX_DIMENSION,
                        3.5 + 2.5 + day_number * BOX_DIMENSION,
                        0,
                    )
                )
                box_list.append(box)
        return box_list

    def _make_skyline_card(self, text_info, offset=0):
        # TODO change the magic numbers
        # support change font
        FONT = os.path.join(github_poster.skyline.__path__[0], "font", "arial.ttf")
        TEXT = text_info
        w, h = measure_text(FONT, TEXT)
        t = (
            text(FONT, TEXT, 5 * (w / h), 5)
            .extrude(h)
            .translate((10 + w + offset, BASE_HEIGHT / 2, h / 2))
        )
        face_angle = math.tan(3.5 / 10)
        t = t.orient(Y).rotate(-face_angle, X)
        return t

    def _make_skyline_base(self):
        base = rectangle((BASE_LENGTH, BASE_WIDTH)).extrude_to(
            rectangle((BASE_TOP_LENGTH, BASE_TOP_WIDTH)), BASE_HEIGHT, ease.linear
        )
        # change to the base location
        base = base.translate((BASE_TOP_LENGTH / 2, BASE_WIDTH / 2, BASE_HEIGHT / 2))
        return base

    def _normalize_data(self):
        """
        normalize the values from 1 to 20
        """
        if self.number_by_date_dict:
            values_list = list(self.number_by_date_dict.values())
            values_list = (values_list - np.min(values_list)) / (
                np.max(values_list) - np.min(values_list)
            )
            values_list = [int(i * 20) + 1 for i in values_list]
            # only support for Python 3.6+, dict is ordered
            self.number_by_date_dict = dict(
                zip(self.number_by_date_dict.keys(), values_list)
            )

    def make_skyline(self):
        self._normalize_data()
        base = self._make_skyline_base()
        boxes = self._make_skyline_boxes()
        boxes = union(*boxes)
        text_info = self.type_info_dict.get(self.skyline_type, self.skyline_type)
        skyline_info_card = self._make_skyline_card(text_info)
        skyline_year_card = self._make_skyline_card(str(self.year), offset=120)
        skyline = base | boxes | skyline_info_card.k(0.25) | skyline_year_card.k(0.25)
        if self.user_name:
            skyline_name_card = self._make_skyline_card(self.user_name, offset=30)
            skyline = skyline | skyline_name_card
        skyline.save(self.file_name)


import os
import numpy
import pytest
import random as rand
import medleydb.sql as m

@pytest.fixture(params=(44100, 16000))
def fs(request):
    return request.param

@pytest.fixture(params=(1,))
def sig(request, fs, random):
    l = 1

    return numpy.squeeze(
        numpy.random.uniform(-1.0, 1.0, (l * fs, request.param))
    )

@pytest.fixture
def random():
    rand.seed(0)
    numpy.random.seed(0)

@pytest.fixture(scope="session")
def medleydb():
    os.environ['MEDLEYDB_PATH']

@pytest.fixture(scope="session", autouse=True)
def session(medleydb):
    _session = m.session(ephemeral=True)
    m.from_medleydb(_session, limit=2)
    return _session


# encoding: utf-8
import re
import collections
from Levenshtein import distance
import time
import pickle
import os
import codecs
import sys
import string

module_dir = os.path.dirname(__file__)
file_path = os.path.join(module_dir, 'data/')

f = open(file_path + 'spell.dat')
data = pickle.load(f)
f.close()

def is_eng_word(word):
    val = True
    include = set(string.letters + string.digits + string.punctuation + u" ")
    for ch in word:
        if ch not in include:
            val = False
            break
    return val

def edit1(word):
    if is_eng_word(word):
        dis = 2
    else:
        dis = 1
    return set(term for term in data if (distance(term, word) <= dis))

def known_edit2(word):
    return set(e2 for e1 in edit1(word) for e2 in edit1(e1) if e2 in data)

def known(words):
    return set(w for w in words if w in data)

def correctch(word):
    candidates = known([word]) or known(
        edit1(word)) or known_edit2(word) or [word]
    return max(candidates, key=data.get)


from sklearn.metrics import roc_auc_score, f1_score, accuracy_score, precision_score

import matplotlib.pyplot as plt

from models.baseline_binary_point_forecast_model import baseline_binary_point_forecast_model
from data_processing import csv_data_source
from data_processing.configurable import configurable_data_manipulator
from data_processing.configurable.stages.pre_point_conversion import drop_columns, normalize
from data_processing.configurable.stages.point_conversion import convert_to_1d_input
from data_processing.configurable.stages.post_point_conversion import convert_to_binary_output

from utils.splitter import splitter

window_size = 10

raw_data = csv_data_source().get_data('data/train_FD001.csv', ';')

cdm = configurable_data_manipulator('.local/cache/all_input_binary.pickle')

cdm.add_pre_point_conversion_stage(drop_columns(['s3', 's4', 's8', 's9', 's13', 's19', 's21', 's22']))
cdm.add_pre_point_conversion_stage(normalize())
cdm.set_point_conversion_stage(convert_to_1d_input(window_size))
cdm.add_post_point_conversion_stage(convert_to_binary_output(10))

data = cdm.get_processed_data(raw_data)

def split_func(training_set, verification_set):
    bbpfm = baseline_binary_point_forecast_model()
    bbpfm.fit(training_set)
    bbpfm.predict_points(verification_set)

splitter().run(split_func, data, 5, [])

forecasted = list(map(lambda x: x.forecasted_output, data))
expected = list(map(lambda x: x.training_output, data))

print('ROC/AUC: ' + str(roc_auc_score(forecasted, expected)))
print('F1: ' + str(f1_score(forecasted, expected)))
print('Accuracy: ' + str(accuracy_score(forecasted, expected)))
print('Precision: ' + str(precision_score(forecasted, expected)))

plt.plot(expected[:500], color = 'orange')
plt.scatter(range(0,500), forecasted[:500])
plt.show()


from django.shortcuts import render, redirect
from django.contrib.auth import logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from library import constants as const, decorator
from library import errormessage
from apps.candidate.models import Candidate
from apps.company.models import CandidateJobPost
from apps.resume.models import Resume
from apps.resume.controller import viewresume
from apps.candidate.forms import CandidateRegistrationForm, LoginForm
from apps.candidate.controller import candidateregistrationcontroller, candidatelogincontroller, \
	jobpostcontroller, jobapplication, candidatedashboard

def home(request):
	if request.method == 'GET':
		if request.user.groups.exists():
			group = request.user.groups.all()[0].name
			context = {"isLogged": True, "userRole": group}
		else:
			context = {"isLogged": False, "userRole": None}
		try:
			validation = jobpostcontroller.getJobPost(request)
			if validation.isValid:
				context.update(validation.response)
				return render(request, 'candidate/candidatehomepage.html', context)
			messages.error(request, 'Failed to fecth job post from DB!')
			return render(request, 'login.html', context)
		except:
			messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
			return render(request, 'login.html', context)

def candidateRegistration(request):
	if request.method == 'GET':
		form = CandidateRegistrationForm()
		context = {const.CANDIDATE_REGISTRATION_FORM: form}
		try:
			return render(request, 'candidate/candidateRegistration.html', context)
		except:
			messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
			return render(request, 'candidate/candidateRegistration.html', context)
	if request.method == 'POST':
		try:
			validation = candidateregistrationcontroller.candidateRegistrationFormValidation(request)
			if validation.isValid == True:
				return redirect('/candidate/login/')
			return render(request, 'candidate/candidateRegistration.html', validation.response)
		except:
			form = CandidateRegistrationForm(request.POST)
			context = {const.CANDIDATE_REGISTRATION_FORM: form}
			messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
			return render(request, 'candidate/candidateRegistration.html', context)

@decorator.unauthenticated_user
def candidateLogin(request):
	form = LoginForm()
	context = {
		const.LOGIN_FORM_PROPERTY: form,
		"userRole": "Candidate"
	}
	if request.method == 'GET':
		try:
			return render(request, 'login.html', context)
		except:
			messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
			return render(request, 'login.html', context)
	if request.method == 'POST':
		try:
			validation = candidatelogincontroller.loginFormValidation(request)
			if validation.isValid == True:
				return redirect('/home')
			messages.error(
				request, errormessage.INVALID_USERNAME_OR_PASSWORD_ERROR
			)
			return render(request, 'login.html', {const.LOGIN_FORM_PROPERTY : validation.response})
		except:
			messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
			return render(request, 'login.html', context)

@login_required(login_url="/candidate/login/")
@decorator.allowed_users(allowed_roles=['Candidate'])
def candidateDashboard(request):
	if request.method == 'GET':
		context = {
			"isLogged": True,
			"userRole": "Candidate"
		}
		return render(request, 'candidate/candidateDashboard.html', context)

@login_required(login_url="/candidate/login/")
@decorator.allowed_users(allowed_roles=['Candidate'])
def profile(request):
	if request.method == 'GET':
		context = {
			"isLogged": True,
			"userRole": "Candidate"
		}
		candidate = Candidate.objects.get(user=request.user)
		if Resume.objects.filter(candidate=candidate).first():
			try:
				validation = viewresume.getResume(request)
				if validation.isValid:
					context.update({'isResume': True})
					context.update(validation.response)
					return render(request, 'candidate/candidateprofile.html', context)
				messages.error(
					request, 'Failed to fetch DB!'
				)
				return redirect('/home')
			except:
				messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
				return redirect('/home')
		else:
			candidate = Candidate.objects.get(user=request.user)
			context.update({'isResume': False, 'candidate': candidate})
			return render(request, 'candidate/candidateprofile.html', context)

login_required(login_url="/candidate/login/")
@decorator.allowed_users(allowed_roles=['Candidate'])
def applyJob(request, jobId):
	if request.method == 'GET':
		context = {
			"isLogged": True,
			"userRole": "Candidate"
		}
		try:
			validation = jobapplication.apply(request, jobId)
			if validation.isValid:
				return redirect('/home')
			messages.error(
				request, 'Failed to apply!'
			)
			return redirect('/home')
		except:
			messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
			return redirect('/home')

def dashboard(request):
	if request.method == 'GET':
		candidate = Candidate.objects.get(user=request.user)
		appliedJob = CandidateJobPost.objects.filter(candidate=candidate)
		print("OKAYYYYYY)))))))))))")
		context = {
			"isLogged": True,
			"userRole": "Candidate",
			"appliedJob": appliedJob
		}
		try:
			print("&&&&&&&&&&&&&&&&&&")
			validation = candidatedashboard.getDashboardData(request)
			print("OKAYYYYYYYYYYYYYYYYYY")
			if validation.isValid:
				print(validation.response)
				context.update(validation.response)
				return render(request, 'candidate/candidatedashboard.html', context)
			messages.error(
				request, 'Failed to fetch!'
			)
			return redirect('/home')
		except:
			messages.error(request, errormessage.INTERNAL_SERVER_ERROR)
			return redirect('/home')

@login_required(login_url="/candidate/login/")
@decorator.allowed_users(allowed_roles=['Candidate'])
def candidateLogout(request):
	logout(request)
	return redirect('/home')