import argparse
import os
import pathlib
import sys
import time
import traceback
from logging import INFO, WARNING, FileHandler, StreamHandler, getLogger
from typing import Callable

import numpy as np
import pandas as pd
import torch
from cfd_model.cfd.periodic_channel_domain import TorchSpectralModel2D
from cfd_model.filter.low_pass_periodic_channel_domain import LowPassFilter
from cfd_model.initialization.periodic_channel_jet_initializer import (
    calc_init_omega,
    calc_init_perturbation_hr_omegas,
    calc_jet_forcing,
)
from scipy.ndimage import sobel
from src.utils import set_seeds

if "ipykernel" in sys.modules:
    from tqdm.notebook import tqdm
else:
    from tqdm import tqdm

os.environ["CUBLAS_WORKSPACE_CONFIG"] = r":4096:8"  # to make calculations deterministic

logger = getLogger()
logger.addHandler(StreamHandler(sys.stdout))
logger.setLevel(INFO)

LR_NX = 32
LR_NY = 17
LR_DT = 5e-4
LR_NT = 500

HR_NX = 128
HR_NY = 65
HR_DT = LR_DT / 4.0
HR_NT = LR_NT * 4

assert HR_DT * HR_NT == LR_DT * LR_NT

N_CYCLES = 140
ASSIM_PERIOD = 4
N_ENSEMBLES = 20

assert N_CYCLES % ASSIM_PERIOD == 0

Y0 = np.pi / 2.0
SIGMA = 0.4
U0 = 3.0
TAU0 = 0.3
PERTUB_NOISE = 0.0025

BETA = 0.1
COEFF_LINEAR_DRAG = 1e-2
ORDER_DIFFUSION = 2
HR_COEFF_DIFFUSION = 1e-5
LR_COEFF_DIFFUSION = 1e-5

ROOT_DIR = str((pathlib.Path(os.environ["PYTHONPATH"]) / "..").resolve())
DEVICE = "cuda"

DF_SEEDS = pd.read_csv(f"{ROOT_DIR}/pytorch/config/cfd_seeds/seeds01.csv").set_index(
    "SimulationNumber"
)

EXPERIMENT_DIR = f"{ROOT_DIR}/data/pytorch/CFD/jet03"
os.makedirs(EXPERIMENT_DIR, exist_ok=True)

low_pass_filter = LowPassFilter(
    nx_lr=LR_NX, ny_lr=LR_NY, nx_hr=HR_NX, ny_hr=HR_NY, device=DEVICE
)

def make_and_initialize_hr_model(n_ensembles: int, seed: int, t0: float = 0.0):
    logger.setLevel(WARNING)

    hr_jet, hr_forcing = calc_jet_forcing(
        nx=HR_NX,
        ny=HR_NY,
        ne=n_ensembles,
        y0=Y0,
        sigma=SIGMA,
        tau0=TAU0,
    )

    hr_perturb = calc_init_perturbation_hr_omegas(
        nx=HR_NX, ny=HR_NY, ne=n_ensembles, noise_amp=PERTUB_NOISE, seed=seed
    )

    hr_omega0 = calc_init_omega(
        perturb_omega=hr_perturb,
        jet=hr_jet,
        u0=U0,
    )

    hr_model = TorchSpectralModel2D(
        nx=HR_NX,
        ny=HR_NY,
        coeff_linear_drag=COEFF_LINEAR_DRAG,
        coeff_diffusion=HR_COEFF_DIFFUSION,
        order_diffusion=ORDER_DIFFUSION,
        beta=BETA,
        device=DEVICE,
    )
    hr_model.initialize(t0=t0, omega0=hr_omega0, forcing=hr_forcing)
    hr_model.calc_grid_data()

    logger.setLevel(INFO)

    return hr_model

def make_and_initialize_lr_model(
    t0: float,
    hr_omega: torch.Tensor,
    noise_generator: Callable[[torch.Tensor], torch.Tensor] = None,
):
    logger.setLevel(WARNING)

    _, lr_forcing = calc_jet_forcing(
        nx=LR_NX,
        ny=LR_NY,
        ne=hr_omega.shape[0],  # n_ensembles
        y0=Y0,
        sigma=SIGMA,
        tau0=TAU0,
    )

    lr_omega0 = None
    if noise_generator is None:
        lr_omega0 = low_pass_filter.apply(hr_omega)
    else:
        noise = noise_generator(hr_omega)
        lr_omega0 = low_pass_filter.apply(hr_omega + noise)

    lr_model = TorchSpectralModel2D(
        nx=LR_NX,
        ny=LR_NY,
        coeff_linear_drag=COEFF_LINEAR_DRAG,
        coeff_diffusion=LR_COEFF_DIFFUSION,
        order_diffusion=ORDER_DIFFUSION,
        beta=BETA,
        device=DEVICE,
    )
    lr_model.initialize(t0=t0, omega0=lr_omega0, forcing=lr_forcing)
    lr_model.calc_grid_data()

    logger.setLevel(INFO)

    return lr_model

def write_out(*, hr_omegas, dict_lr_omegas, i_seed, i_start, i_end, result_dir):
    hr_omegas = torch.stack(hr_omegas, dim=1)  # stack along time dim.
    assert hr_omegas.shape == (N_ENSEMBLES, 2 * ASSIM_PERIOD + 1, HR_NX, HR_NY)

    file_path = (
        f"{result_dir}/seed{i_seed:05}_start{i_start:02}_end{i_end:02}_hr_omega.npy"
    )
    np.save(file_path, hr_omegas.cpu().numpy())

    for name in dict_lr_omegas.keys():
        lr_omegas = torch.stack(dict_lr_omegas[name], dim=1)  # stack along time dim.
        assert lr_omegas.shape == (N_ENSEMBLES, 2 * ASSIM_PERIOD + 1, LR_NX, LR_NY)

        file_path = f"{result_dir}/seed{i_seed:05}_start{i_start:02}_end{i_end:02}_lr_omega_{name}.npy"
        np.save(file_path, lr_omegas.cpu().numpy())

def store_data(*, hr_omegas, hr_model, dict_lr_omegas, dict_lr_models):
    hr_omegas.append(hr_model.omega.cpu().clone().to(torch.float32))
    for name in dict_lr_omegas.keys():
        dict_lr_omegas[name].append(
            dict_lr_models[name].omega.cpu().clone().to(torch.float32)
        )

class GaussianNoiseGenerator:
    def __init__(self, noise_amplitude: float):
        self.noise_amplitude = noise_amplitude
        logger.info(f"noise_amplitude = {self.noise_amplitude}")

    def __call__(self, hr_omega: torch.Tensor):
        noise = torch.randn_like(hr_omega)
        noise *= self.noise_amplitude
        return noise

class SobelNoiseGenerator:
    def __init__(self, axis: int, noise_amplitude: float):
        self.axis = axis
        self.noise_amplitude = noise_amplitude
        logger.info(f"axis = {self.axis}, noise amp = {self.noise_amplitude}")

    def __call__(self, hr_omega: torch.Tensor):
        noise = sobel(hr_omega.cpu().numpy(), axis=self.axis)
        noise = noise / np.mean(np.abs(noise), axis=(-2, -1), keepdims=True)
        noise *= self.noise_amplitude
        return torch.from_numpy(noise)

LR_NOISE_GENERATORS = {
    "no-noise": None,
}

parser = argparse.ArgumentParser()
parser.add_argument("--i_seed_start", type=int, required=True)
parser.add_argument("--i_seed_end", type=int, required=True)

if __name__ == "__main__":

    start_time = time.time()
    i_seed_start = parser.parse_args().i_seed_start
    i_seed_end = parser.parse_args().i_seed_end
    logger.addHandler(
        FileHandler(
            f"{EXPERIMENT_DIR}/log_start{i_seed_start:05}_end{i_seed_end:05}.txt"
        )
    )

    for i_seed in range(i_seed_start, i_seed_end + 1):
        try:
            seed = DF_SEEDS.loc[i_seed, "Seed0"]
            set_seeds(seed, use_deterministic=True)
            logger.info("\n*********************************************************")
            logger.info(f"i_seed = {i_seed}, seed = {seed}")
            logger.info("*********************************************************\n")

            start_time_per_loop = time.time()

            result_dir = f"{EXPERIMENT_DIR}/seed{i_seed:05}"
            os.makedirs(result_dir, exist_ok=True)

            hr_model = make_and_initialize_hr_model(seed=seed, n_ensembles=N_ENSEMBLES)

            all_hr_omegas = []
            dict_lr_init_infos = {}

            for i_cycle in tqdm(range(N_CYCLES + 1), total=N_CYCLES + 1):
                if i_cycle % ASSIM_PERIOD == 0:
                    i_start = i_cycle
                    i_end = i_start + 2 * ASSIM_PERIOD
                    logger.info(
                        f"LR model register: i_start = {i_start}, i_end = {i_end} (i_cycle = {i_cycle})"
                    )

                    _dict = {}
                    _t0 = hr_model.t
                    _hr_omega = hr_model.omega.cpu().clone()
                    for name, gen in LR_NOISE_GENERATORS.items():
                        _dict[name] = make_and_initialize_lr_model(_t0, _hr_omega, gen)

                    dict_lr_init_infos[i_end] = {
                        "i_start": i_start,
                        "i_end": i_end,
                        "t0": _t0,
                        "lr_models": _dict,
                    }

                all_hr_omegas.append(hr_model.omega.cpu().clone().to(torch.float32))

                hr_model.time_integrate(dt=HR_DT, nt=HR_NT, hide_progress_bar=True)
                hr_model.calc_grid_data()

                if i_cycle in dict_lr_init_infos.keys():

                    i_start = dict_lr_init_infos[i_cycle]["i_start"]
                    i_end = dict_lr_init_infos[i_cycle]["i_end"]

                    logger.info(
                        f"\nLR model integration: i_start = {i_start}, i_end = {i_end} (i_cycle = {i_cycle})"
                    )

                    hr_omegas = all_hr_omegas[i_start : i_end + 1]
                    logger.info(
                        f"hr omega len = {len(hr_omegas)}, all hr omega len = {len(all_hr_omegas)}"
                    )

                    lr_models = dict_lr_init_infos[i_cycle]["lr_models"]

                    dict_lr_omegas = {
                        key: [model.omega.cpu().clone().to(torch.float32)]
                        for key, model in lr_models.items()
                    }

                    for key, model in lr_models.items():
                        for _ in range(i_end - i_start):
                            model.time_integrate(
                                dt=LR_DT, nt=LR_NT, hide_progress_bar=True
                            )
                            model.calc_grid_data()
                            dict_lr_omegas[key].append(
                                model.omega.cpu().clone().to(torch.float32)
                            )
                        logger.info(
                            f"lr model [{key}] len = {len(dict_lr_omegas[key])}"
                        )

                    write_out(
                        hr_omegas=hr_omegas,
                        dict_lr_omegas=dict_lr_omegas,
                        i_seed=i_seed,
                        i_start=i_start,
                        i_end=i_end,
                        result_dir=result_dir,
                    )

                    logger.info("all data were written out.\n")
                    del dict_lr_init_infos[i_cycle], lr_models, dict_lr_omegas

            end_time_per_loop = time.time()
            logger.info(
                f"Elapsed time per loop = {end_time_per_loop - start_time_per_loop} sec"
            )

        except Exception as e:
            logger.info("\n*********************************************************")
            logger.info("Error")
            logger.info("*********************************************************\n")
            logger.error(e)
            logger.error(traceback.format_exc())

    end_time = time.time()
    logger.info(f"Total elapsed time = {end_time - start_time} sec")


import os
import random
import shutil
import string
import unittest
import uuid

import pandas as pd

import constants
from grade_helper_ops.compile_grade_report import compile_grade_report

class TestCompileGradeReport(unittest.TestCase):

    def test_compile_grade_report_all_students_graded(self):
        path = os.path.join("test_lab")
        students_amt = 10
        if not os.path.exists(path):
            os.makedirs(path, exist_ok=True)
        column_names = ["First name", "Surname", "ID number", "Grade", "Feedback"]
        for i in range(students_amt):
            row_values = [''.join(random.choices(string.ascii_uppercase, k=i)),
                          ''.join(random.choices(string.ascii_uppercase, k=i)), uuid.uuid4(), f"{i}", "Try harder!"]
            student_path = os.path.join(path, f"student_{i}")
            os.makedirs(student_path, exist_ok=True)
            csv_path = os.path.join(student_path, constants.CSV_FILE_NAME)

            csv_data = {}
            for row, col in zip(row_values, column_names):
                csv_data[col] = [row]
            data_frame = pd.DataFrame(csv_data)
            data_frame.to_csv(csv_path, columns=column_names)

        response = compile_grade_report(os.listdir(path), path, os.path.join(path, constants.FINAL_GRADE_REPORT_PATH),
                                        column_names)
        self.assertFalse(response["error"])
        shutil.rmtree(path)

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


import re
from tlds import tlds

EYES = r'[\#:;Xx8]'
HAT = r'[<>~]?'
NOSE = r'[\'-o\*]?'
MOUTH_F = r'[\[\]\|\)\(\/\\DSspPQ><xoO\*]'
MOUTH_B = r'[\[\]\|\)\(\/\\DSsqQ><xoO\*]'
TEAR = r'\'?'

class Tokenizer(object):

    SPLITTER = re.compile(
        '((?:' + ')|(?:'.join([

            # Emoticons
            HAT + EYES + TEAR + NOSE + MOUTH_F,   # <:'-(
            MOUTH_B + NOSE + TEAR + EYES + HAT,   # )-':>
            r'[oO\^\*][_.oOv][oO\^\*]',  # o_O

            # Whitespace
            r'\s+',

            # Elipsis
            r'\.\.+'

            # Contractions
            #r"\w+n't",
            #r"\w+'ll",
            #r"\w+'ve",
            #r"\w+'re",
            #r"\w+'d",
            #r"\w+'s",
            #r"I'm",
            #r"y'all",
            r"\w+'\w+",
            r"\w+n'",
            r"o'\w*",
            r"ol'",
            r"'tis",
            r"'twas",

            # Punctuation
            r'[?!,.;:"\'\(\)\]\[]',

            # Hyperlinks
            r'https?\://\w+(?:\.\w+)+(?:/\S+)*/?',
            r'(?:\w+\.)+(?:' + '|'.join(tlds) + ')(?:/\S+)*/?(?:\s|$)',

        ]) + '))')

    ALL_WHITE = re.compile(r'\s*$')

    def split(self, text):
        return [
            s.lower() for s in self.SPLITTER.split(text)
            if not self.ALL_WHITE.match(s)
        ]


from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import json
from collections import OrderedDict
import string
from prettytable import PrettyTable

# Define words we do not want to show in frequent list
def unwanted():
    unwanted = list(stopwords.words('english'))
    unwanted += list(string.punctuation)
    unwanted += ['https', 'http', '...', '…',"''",'``','iphone', 'Iphone', 'iphone7', 'Iphone7',"'s"]
    return unwanted

# Find the most frequent n words in tweets json data
def frequent_words(data, n):
    unwanted_words = unwanted()
    counts = dict() # {"word1": 253, "word2": 13}
    for tweet in data:
        if "text" in tweet.keys() and tweet["lang"]=="en":
            sentence = tweet["text"]
            words = word_tokenize(sentence)
            for word in words:
                if word.lower() not in unwanted_words:
                    counts[word] = counts.get(word, 0)+1
    sorted_counts = sorted(counts.items(), key=lambda t: t[1], reverse = True)
    with open("frequency.txt", 'w', encoding='utf-8') as f:
        f.write(str(sorted_counts[:n]))
    return sorted_counts[:n]

# Find tweets retweeted more than n times
def popular_tweet(statuses, n):
    popular = []
    for tweet in statuses:
        if "retweet_count" in tweet.keys() and tweet["retweet_count"] > n:
            popular.append(tweet)
    return popular

# Find tweets with an overall retweeted numbers more than n times
def popular_retweeted_count(statuses, n):
    popular = {}
    for tweet in statuses:
        if "retweeted_status" in tweet.keys() and len(tweet["retweeted_status"])!=0 and tweet["lang"] == "en" \
            and "retweet_count" in tweet["retweeted_status"].keys():
                if tweet["retweeted_status"]["retweet_count"] > n and "text" in tweet.keys() and len(tweet["text"])!=0:
                    if tweet["text"] not in popular.values():
                        popular[tweet["retweeted_status"]["retweet_count"]] = tweet["text"]
    sorted_popular = sorted(popular.items(), key=lambda t: t[0], reverse = True)
    return sorted_popular

tweets_all = load_data("combined_all.json")
frequent_words_tweets_all = frequent_words(tweets_all, 30)

# print out results as table
pt_word_frequency = PrettyTable(field_names = ['Words', 'Frequency'])
[pt_word_frequency.add_row(row) for row in frequent_words_tweets_all]
print(pt_word_frequency)

# find tweets with an overall retweeted number greater than 5000
popular_retweeted_count = popular_retweeted_count(content, 5000)
for tweet in sorted(popular_retweeted_count, reverse=True):
    print("retweeted_number: ", tweet[0], "\t","tweet_text: ", tweet[1])


#!/usr/bin/env python3

from lib import common, githubapi

ORGANIZATION_CONFIG_KEY = "ORGANIZATION"

def organization_repository_webhooks_list(
    auth_token: str, organization_name: str, repository_type: str
) -> list[tuple[str, list[str]]]:
    repository_list: list[tuple[str, list[str]]] = []

    try:
        for repository_item in githubapi.organization_repository_list(
            auth_token, organization_name, repository_type
        ):
            # split repository into owner/repository parts
            owner, repository = repository_item["full_name"].split("/")
            webhook_list: list[str] = []

            try:
                for item in githubapi.repository_webhook_list(
                    auth_token, owner, repository
                ):
                    webhook_list.append(item["config"]["url"])
            except githubapi.APIRequestError as err:
                common.github_api_exit_error(
                    f"Unable to fetch webhook list for repository {owner}/{repository}.",
                    err,
                )

            if webhook_list:
                repository_list.append((repository_item["git_url"], webhook_list))

    except githubapi.APIRequestError as err:
        common.github_api_exit_error(
            f"Unable to fetch repository list for organization {organization_name}, type {repository_type}.",
            err,
        )

    return repository_list

def main():
    # load config from file
    config_data = common.load_config(config_key_addition_set={ORGANIZATION_CONFIG_KEY})
    config_auth_token = config_data["AUTH_TOKEN"]

    # fetch repositories of the specified type and their defined webhooks
    print("Building repository list including webhooks:")
    repository_list = organization_repository_webhooks_list(
        config_auth_token,
        config_data[ORGANIZATION_CONFIG_KEY],
        config_data["REPOSITORY_TYPE"],
    )

    # output list, repository URL with webhooks defined
    for repository_url, webhook_list in repository_list:
        print(f"{repository_url}:")
        for hook_item in webhook_list:
            print(f"\t{hook_item}")

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 18 08:35:20 2019

@author: pickering
"""

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

plt.rc('text', usetex=True)
plt.rc('font', family='sans-serif')

h = 6.626E-34
c = 3E8
temp = 5777
k = 1.38E-23

plt.close('all')

sun_spectra = np.loadtxt('../data_files_for_figures/sun_black_body.txt')
wavelength_sun = np.loadtxt('../data_files_for_figures/sun_wavelengths.txt')
wavelength_nm = np.linspace(0,5000,500)
wavelength_m = wavelength_nm/1E9
frequency = c/wavelength_m
planck = (2*h*(c**2)/(wavelength_m**5))*(1/(np.exp((h*c)/(wavelength_m*k*temp))-1))

rayleigh = 2*c*k*temp/(wavelength_m**4)

plt.figure()
plt.plot(wavelength_nm, planck/1E13, label=r"Planck's Law")
plt.plot(wavelength_nm, rayleigh/1E13, label=r'Rayleigh-Jeans Law')
plt.plot(wavelength_sun, sun_spectra*1.25, linestyle="-", alpha=0.5, label=r'Solar Spectral Radiance')
plt.ylim(0,3)
plt.xlim(0,3000)
plt.xlabel(r'Wavelength [nm]')
plt.ylabel(r'Spectral Radiance [a.u.]')
plt.legend()

plt.savefig('../final_figures/black_body.png',dpi=400,bbox_inches='tight')


import logging

_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)

def logged_query(cr, query, args=None):
    if args is None:
        args = ()
    args = tuple(args) if type(args) == list else args
    cr.execute(query, args)
    _logger.debug('Running %s', query % args)
    _logger.debug('%s rows affected', cr.rowcount)
    return cr.rowcount

def rename_module(cr):
    old_names = ['acsone_hr_contract']
    new_name = 'hr_contract_signature'
    query = ("UPDATE ir_model_data SET module = %s "
             "WHERE module in %s ")
    logged_query(cr, query, (new_name, tuple(old_names)))
    query = ("UPDATE ir_module_module_dependency SET name = %s "
             "WHERE name in %s")
    logged_query(cr, query, (new_name, tuple(old_names)))


import pyautogui
import pyscreeze
import time
import configparser
import datetime
import PyWinMouse

pyautogui.PAUSE = 0.2
pyautogui.FAILSAFE = False

#on set up la config
config = configparser.ConfigParser()
config.read("config.ini")

playButtonPixelPos = pyautogui.Point(x=eval(config["DEFAULT"]["playButtonPixelPos"])[0], y=eval(config["DEFAULT"]["playButtonPixelPos"])[1])
playButtonPixelRGB = eval(config["DEFAULT"]["playButtonPixelRGB"])
inBetweenSettingsButtonPixelPos1 = pyautogui.Point(x=eval(config["DEFAULT"]["inBetweenSettingsButtonPixelPos1"])[0], y=eval(config["DEFAULT"]["inBetweenSettingsButtonPixelPos1"])[1])
inBetweenSettingsButtonPixelRGB1 = eval(config["DEFAULT"]["inBetweenSettingsButtonPixelRGB1"])
inBetweenSettingsButtonPixelPos2 = pyautogui.Point(x=eval(config["DEFAULT"]["inBetweenSettingsButtonPixelPos2"])[0], y=eval(config["DEFAULT"]["inBetweenSettingsButtonPixelPos2"])[1])
inBetweenSettingsButtonPixelRGB2 = eval(config["DEFAULT"]["inBetweenSettingsButtonPixelRGB2"])
concedeButtonPos = pyautogui.Point(x=eval(config["DEFAULT"]["concedeButtonPos"])[0], y=eval(config["DEFAULT"]["concedeButtonPos"])[1])
concedeButtonRGB = eval(config["DEFAULT"]["concedeButtonRGB"])
inMatchSettingsButtonPixelPos1 = pyautogui.Point(x=eval(config["DEFAULT"]["inMatchSettingsButtonPixelPos1"])[0], y=eval(config["DEFAULT"]["inMatchSettingsButtonPixelPos1"])[1])
inMatchSettingsButtonPixelRGB1 = eval(config["DEFAULT"]["inMatchSettingsButtonPixelRGB1"])
inMatchSettingsButtonPixelPos2 = pyautogui.Point(x=eval(config["DEFAULT"]["inMatchSettingsButtonPixelPos2"])[0], y=eval(config["DEFAULT"]["inMatchSettingsButtonPixelPos2"])[1])
inMatchSettingsButtonPixelRGB2 = eval(config["DEFAULT"]["inMatchSettingsButtonPixelRGB2"])

class quickgrab:
    def __init__(self):
        self.isRunning = False
        self.timeBeforeConcede = 0
        self.mouse = PyWinMouse.Mouse()

    def startTheScript(self):
        count = 0
        while True:
            time.sleep(0.5)
            count += 1

            if not self.isRunning:
                print("pause")
                while not self.isRunning:
                    one = 1

            if(self.areWeInMainScreen()):
                self.goToPixAndClick(playButtonPixelPos)
                print("We are in MainScreen")

            if(self.areWeInBetweenMatchScreen()):
                time.sleep(self.timeBeforeConcede)
                self.goToPixAndClick(inBetweenSettingsButtonPixelPos1)
                print("We are in Between Matches")

            if(self.areWeInConcedeMenu()):
                self.goToPixAndClick(concedeButtonPos)
                print("We are in Concede Menu")

            if (self.areWeInMatchScreen()):
                time.sleep(self.timeBeforeConcede)
                self.goToPixAndClick(inBetweenSettingsButtonPixelPos1)
                print("We are in Match Screen")

            if count > 15:
                self.goToPixAndClick(pyautogui.Point(x=10, y=10))
                count = 0

    def getPosIn3Secs(self):
        time.sleep(3)
        return pyautogui.position()

    def getColorIn3Secs(selfself):
        time.sleep(3)
        return pyautogui.pixel(pyautogui.position().x, pyautogui.position().y)

    def getPosNow(self):
        return pyautogui.position()

    def getColorNow(self):
        return pyautogui.pixel(pyautogui.position().x, pyautogui.position().y)

    def click(self):
        self.mouse.left_down()
        time.sleep(0.2)
        self.mouse.left_up()

    def goToImgAndClick(self, path):
        try:
            Pix = pyautogui.locateCenterOnScreen(path)
            pyautogui.moveTo(Pix.x, Pix.y, 0)
            self.click()
        except pyscreeze.ImageNotFoundException:
            print("image non trouvee")

    def goToPixAndClick(self, Pix):
        pyautogui.moveTo(Pix.x, Pix.y, 0)
        self.click()

    def printMyPosInXSecs(self):
        time.sleep(3)
        print(pyautogui.position())

    def printMyRGB(self):
        print(pyautogui.pixel(inBetweenSettingsButtonPixelPos1.x, inBetweenSettingsButtonPixelPos1.y))

    def areWeInMainScreen(self):
        if pyautogui.pixel(playButtonPixelPos.x, playButtonPixelPos.y) == playButtonPixelRGB:
            return True
        else:
            return False

    def areWeInBetweenMatchScreen(self):
        if pyautogui.pixelMatchesColor(inBetweenSettingsButtonPixelPos1.x, inBetweenSettingsButtonPixelPos1.y, inBetweenSettingsButtonPixelRGB1)\
                or pyautogui.pixelMatchesColor(inBetweenSettingsButtonPixelPos2.x, inBetweenSettingsButtonPixelPos2.y, inBetweenSettingsButtonPixelRGB2):
                return True
        else:
            return False

    def areWeInConcedeMenu(self):
        if pyautogui.pixelMatchesColor(concedeButtonPos.x, concedeButtonPos.y, concedeButtonRGB):
            return True
        else:
            return False

    def areWeInMatchScreen(self):
        if pyautogui.pixelMatchesColor(inMatchSettingsButtonPixelPos1.x, inMatchSettingsButtonPixelPos1.y, inMatchSettingsButtonPixelRGB1)\
                or pyautogui.pixelMatchesColor(inMatchSettingsButtonPixelPos2.x, inMatchSettingsButtonPixelPos2.y, inMatchSettingsButtonPixelRGB2):
            return True
        else:
            return False


import cv2 as cv
import numpy as np
import threading
import os
import time
import datetime
from upload import s3
import firebase_admin
from firebase_admin import credentials, db
import multiprocessing as mp
from multiprocessing import shared_memory
import sys

cred = credentials.Certificate("your firebase credit path")
firebase_admin.initialize_app(cred,
                              {'databaseURL' : 'your firebase url'})

datapath='video'
ref=db.reference(datapath)
fps= 30
dst='./test.mp4'    #저장 위치

event_flag=np.array([0])

save_second=3   #몇초 저장할건지

queue_size = save_second*30
queue = []
class Sdata:
    def __init__(self, cam_no):
        self.cam_no=cam_no
        self.event_type=0
        self.link_storage=''
        self.date=''
        self.time=''

def Vsave(sdata, filename):
    global event_flag
    ref=db.reference('flag')
    ref.set(str(event_flag[0]))
    s3.upload_file(filename,"sjmama1",filename)
    print('s3 upload!')
    sdata.link_storage='https://your_s3_storage_url/'+sdata.date+'%'+sdata.date[2:4]+sdata.time+'.mp4'
    sdata_dict=sdata.__dict__
    print(sdata_dict)
    ref=db.reference('video')#저장한 파일은 삭제
    ref.push(sdata_dict)
    print('firebase push!')
    os.remove(filename)

def showNsave(cam_no):
    global cap
    global event_flag
    ret, frame = cap.read()
    ref=db.reference('flag')
    ref.set(str('0'))
    print(str(frame.shape).replace(' ', ''))
    os.system('echo '+str(frame.shape).replace(' ', '').replace('(', '').replace(')','')+' > info.txt')
    sevent_flag=np.array([0])
    print(sevent_flag.nbytes)
    flagshm=shared_memory.SharedMemory(create=True, size=sevent_flag.nbytes)
    print('flag= '+flagshm.name)
    os.system('echo '+flagshm.name+' >> info.txt')
    event_flag = np.ndarray(sevent_flag.shape, dtype=sevent_flag.dtype, buffer=flagshm.buf)
    event_flag[:]=sevent_flag[:]
    sdata=Sdata(cam_no)
    innerflag=0
    print(frame.shape, frame.dtype)
    fourcc = cv.VideoWriter_fourcc(*'mp4v')
    frameshm=shared_memory.SharedMemory(create=True, size=frame.nbytes)
    os.system('echo '+frameshm.name+' >> info.txt')
    print('frame= '+frameshm.name)
    out = cv.VideoWriter(dst, fourcc, fps, (frame.shape[1], frame.shape[0]))
    while cap.isOpened():
        ret, frame = cap.read()
        sharedframe=np.ndarray(frame.shape, dtype=frame.dtype, buffer=frameshm.buf)
        sharedframe[:]=frame[:]
        # if frame is read correctly ret is True
        if not ret:
            print("Can't receive frame (stream end?). Exiting ...")
            break
        #cv.imshow('frame', frame)
        queue.append(frame)
        if len(queue) > queue_size:
            queue.pop(0)
        if innerflag==0 and event_flag[0]!=0:#여기는 위험 신호 감지해서 저장하게 해야하고
            print(event_flag[0])
            ref=db.reference('flag')
            if event_flag[0]==12:
                ref.set(str(2))
            else:
                ref.set(str(event_flag[0]))
            sdata.event_type=str(event_flag[0])
            innerflag=event_flag[0]
            now = datetime.datetime.now()
            sdata.date=now.strftime("%Y-%m-%d")
            sdata.time=now.strftime("%Hh%Mm%Ss")
            new_filename = f"{sdata.date}#{sdata.time}.mp4"
            for f in queue:
                out.write(f)
        elif innerflag!=0 and event_flag[0]!=0:
            out.write(frame)
            if (innerflag != event_flag[0]) :
                innerflag=event_flag[0]
                ref=db.reference('flag')
                if event_flag[0]==12:
                    ref.set(str(2))
                else:
                    ref.set(str(event_flag[0]))
        elif innerflag!=0 and event_flag[0]==0:
            print(event_flag[0])
            out.release()
            os.rename("test.mp4", new_filename)
            Vsaveth=threading.Thread(target=Vsave, args=(sdata, new_filename))
            Vsaveth.start()
            innerflag=event_flag[0]
            out = cv.VideoWriter(dst, fourcc, fps, (frame.shape[1], frame.shape[0]))

    out.release()
    cap.release()

# fctl=threading.Thread(target=flagcontrol, args=())
# fctl.daemon=True
def main():
    global cap
    cam_no='0000'
    cap = cv.VideoCapture('rtmp://stramserverip/stream app name/'+cam_no)
    S_S=threading.Thread(target=showNsave, args=(cam_no,))#show and save
    # fctl.start()
    if os.path.exists(dst):
        os.remove(dst)
    S_S.start()
    S_S.join()
    cv.destroyAllWindows()

if __name__ == "__main__":
    main()


import cv2
import numpy as np
import argparse
import tensorflow as tf

class JudgeFashion:
    def __init__(
        self,
        img_size=640,
        model_path="./trained_model_v1.tflite",
        use_gpu=False,
        w="./yolov3.weights",
        cfg="./yolo.cfg",
    ):
        self.w = w
        self.cfg = cfg

        self.self.IMG_SIZE = self.IMG_SIZE
        self.tflite_interpreter = tf.lite.Interpreter(model_path=args.model_path)

        self.tflite_interpreter.allocate_tensors()
        self.input_details = self.tflite_interpreter.get_input_details()
        self.output_details = self.tflite_interpreter.get_output_details()
        self.input_shape = self.input_details[0]["shape"]
        self.net = cv2.dnn.readNet(self.w, self.cfg)

        if use_gpu:
            net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
            net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

    def get_output_layers(self, net):
        layer_names = net.getLayerNames()
        output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
        return output_layers

    def fix_img_size(self, im, desired_size=640):
        old_size = im.shape[:2]  # old_size is in (height, width) format

        ratio = float(desired_size) / max(old_size)
        new_size = tuple([int(x * ratio) for x in old_size])

        # new_size should be in (width, height) format

        im = cv2.resize(im, (new_size[1], new_size[0]))

        delta_w = desired_size - new_size[1]
        delta_h = desired_size - new_size[0]
        top, bottom = delta_h // 2, delta_h - (delta_h // 2)
        left, right = delta_w // 2, delta_w - (delta_w // 2)

        color = [0, 0, 0]
        new_im = cv2.copyMakeBorder(
            im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color
        )

        return new_im

    def getYoloPredictions(self, frame, YOLO_NET):
        class_ids = []
        confidences = []
        boxes = []
        blob = cv2.dnn.blobFromImage(
            frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False
        )
        YOLO_NET.setInput(blob)
        outs = YOLO_NET.forward(get_output_layers(YOLO_NET))
        width = frame.shape[1]
        height = frame.shape[0]

        for out in outs:
            for detection in out:
                scores = detection[5:]
                class_id = np.argmax(scores)
                confidence = scores[class_id]
                if confidence > 0.5:
                    center_x = int(detection[0] * width)
                    center_y = int(detection[1] * height)
                    w = int(detection[2] * width)
                    h = int(detection[3] * height)
                    x = center_x - w / 2
                    y = center_y - h / 2
                    class_ids.append(class_id)
                    confidences.append(float(confidence))
                    boxes.append([x, y, w, h])

            indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
            return indices, boxes, class_ids

    def crop_out_person(self, img, YOLO_NET=net):
        img = fix_img_size(img, self.IMG_SIZE)
        predictions, boxes, labels = getYoloPredictions(img, YOLO_NET)
        person_bboxes = []
        for currentPrediction in predictions:
            currentPrediction = currentPrediction[0]
            if labels[currentPrediction] != 0:
                continue

            box = boxes[currentPrediction]
            x = box[0]
            y = box[1]
            w = box[2]
            h = box[3]
            label = str(labels[currentPrediction])
            person_bboxes.append([x, y, w, h])

        return person_bboxes

    def get_fashion_results(self, image):
        bboxes = crop_out_person(image)

        for bbox in bboxes:
            x, y, w, h = bbox

            person_croped = image[y : y + h, x : x + w, :]
            fix_sized_img = fix_img_size(person_croped, self.IMG_SIZE)
            self.tflite_interpreter.set_tensor(
                self.input_details[0]["index"], fix_sized_img
            )
            self.tflite_interpreter.invoke()

            results = np.squeeze(
                self.tflite_interpreter.get_tensor(self.output_details[0]["index"])
            )
            return results


import os
import random
import urllib.request
import hjson
import pandas as pd
import matplotlib.colors as colors
import beartype
import warnings
from .m2l_enums import Datatype, VerboseLevel

class Config(object):
    """Object that represents a sub-project. It is defined by some source data,
    a region of interest (bounding box or polygon) and some execution flags.
    """

    def __init__(self, map_data, verbose_level=VerboseLevel.ALL):
        self.project_path = None
        self.bbox_3d = {
            "minx": 0,
            "maxx": 1000,
            "miny": 0,
            "maxy": 1000,
            "base": -10000,
            "top": 1200,
        }
        self.polygon = None
        self.step_out = 0.1
        self.dtm_crs = "EPSG:4326"
        self.project_crs = None

        # TODO: Need to add check for map_data and verbose_level types
        self.map_data = map_data
        self.verbose_level = verbose_level

        # Set default run flags
        self.run_flags = {
            "aus": True,
            "deposits": "Fe,Cu,Au,NONE",
            "dtb": "",
            "dtb_null": -2147483648,
            "cover_dip": 10,
            "cover_spacing": 5000,
            "orientation_decimate": 1,
            "contact_decimate": 5,
            "intrusion_mode": 0,
            "interpolation_spacing": 500,
            "misorientation": 30,
            "interpolation_scheme": "scipy_rbf",
            "fault_decimate": 5,
            "min_fault_length": 5000,
            "fault_dip": 90,
            "fault_orientation_clusters": 2,
            "fault_length_clusters": 2,
            "pluton_dip": 45,
            "pluton_form": "domes",
            "min_pluton_area": 10000000,
            "dist_buffer": 10,
            "contact_dip": -999,
            "contact_orientation_decimate": 5,
            "null_scheme": "null",
            "thickness_buffer": 5000,
            "max_thickness_allowed": 10000,
            "fold_decimate": 5,
            "fat_step": 750,
            "close_dip": -999,
            "use_interpolations": True,
            "use_fat": True,
            "use_roi_clip": False,
            "roi_clip_path": "",
            "drift_prefix": [],
            "ignore_codes": [],
            "fault_fault_weight": 3,
            "fault_weight": 1,
            "formation_weight": 7,
            "formation_formation_weight": 9,
            "fault_formation_weight": 5,
            "map2graph": False,
            "granular_map2graph": False,
        }

    @beartype.beartype
    def update(
        self,
        project_path: str,
        bbox_3d,
        polygon,
        step_out,
        dtm_crs,
        project_crs,
        metadata_filename: str = "",
        clut_path: str = "",
        run_flags=dict(),
    ):
        self.project_path = project_path
        self.graph_path = os.path.join(project_path, "graph")
        self.tmp_path = os.path.join(project_path, "tmp")
        self.output_path = os.path.join(project_path, "output")

        # filenames for WKT format (map2graph requirement)
        self.fault_filename_wkt = os.path.join(project_path, "tmp", "faults.csv")
        # self.fault_output_file_csv = os.path.join(project_path,'output',"faults.csv")
        self.structure_filename_wkt = os.path.join(project_path, "tmp", "structure.csv")
        self.geology_filename_wkt = os.path.join(project_path, "tmp", "geology.csv")
        self.mindep_filename_wkt = os.path.join(project_path, "tmp", "mindep.csv")

        self.strat_graph_filename = os.path.join(
            project_path, "graph", "graph_strat_NONE.gml"
        )
        # self.dtm_filename = os.path.join(project_path,"dtm", 'dtm.tif')
        # self.dtm_reproj_filename = os.path.join(project_path,"dtm", 'dtm_rp.tif')
        self.clut_path = clut_path

        if isinstance(run_flags, dict):
            for key in run_flags:
                if key not in self.run_flags:
                    warnings.warn(f"config run_flags key {key} is not a valid option")
            self.run_flags.update(run_flags)
            # TODO: Add sanity checks for run_flags updated from user
            if self.run_flags["interpolation_spacing"] < 0:
                self.run_flags["interpolation_spacing"] = -self.run_flags[
                    "interpolation_spacing"
                ]
            self.run_flags["orientation_decimate"] = max(
                self.run_flags["orientation_decimate"], 1
            )
            self.run_flags["contact_decimate"] = max(
                self.run_flags["contact_decimate"], 1
            )
            self.run_flags["fault_decimate"] = max(self.run_flags["fault_decimate"], 1)
            self.run_flags["contact_orientation_decimate"] = max(
                self.run_flags["contact_orientation_decimate"], 1
            )
            self.run_flags["fold_decimate"] = max(self.run_flags["fold_decimate"], 1)
        else:
            print(
                "run_flags must be a dictionary, setting config run flags to the defaults."
            )

        self.bbox_3d = bbox_3d
        self.bbox = tuple(
            [bbox_3d["minx"], bbox_3d["miny"], bbox_3d["maxx"], bbox_3d["maxy"]]
        )

        # Recalculate height and width from bbox and interpolation_spacing
        self.width = (
            int((self.bbox[2] - self.bbox[0]) / self.run_flags["interpolation_spacing"])
            + 1
        )
        self.height = (
            int((self.bbox[3] - self.bbox[1]) / self.run_flags["interpolation_spacing"])
            + 1
        )
        self.polygon = polygon
        self.step_out = step_out

        self.dtm_crs = dtm_crs
        self.project_crs = project_crs

        self.read_metadata(metadata_filename)
        self.create_cmap()

    @beartype.beartype
    def read_metadata(self, filename: str):
        """
        Helper function that turns json and hjson files into usable configuration dictionaries.

        Args:
            filename (string) : Path or url to metadata file
        """
        if filename == "" and self.map_data.get_map_data(Datatype.GEOLOGY) is not None:
            metadata_error = "When using your own local files or remote data, pass the path or url to a valid metadata file (.json or .hjson) that describes your input column names.\n"
            metadata_error += "You can find an example config here https://gist.github.com/yohanderose/a127c29cb88529f049a5bafc881bb1a0"
            raise ValueError(metadata_error)

        # Load in dictionary that describes the column names
        try:
            if filename.startswith("http"):
                with urllib.request.urlopen(filename) as raw_data:
                    self.c_l = hjson.load(raw_data)
                    if self.state is not None:
                        # Check for if remote sources are given as local files, state won't exist
                        if self.state == "SA":
                            for key in self.c_l.keys():
                                try:
                                    self.c_l[key] = self.c_l[key].lower()
                                except Exception:
                                    pass
            else:
                with open(filename) as raw_data:
                    self.c_l = hjson.load(raw_data)
        except Exception as e:
            self.errorState = str(e)
        self.check_for_reserved_codes_and_labels()

    def check_for_reserved_codes_and_labels(self):
        reserved_labels_structure = {
            "d": "DIP",
            "dd": "DIPDIR",
            "sf": "STRUCTURE_TYPE",
            "ds": "DESCRIPTION",
            "bo": "POLARITY",
            "gi": "STRUCTURE_POINT_ID",
        }
        reserved_labels_geology = {
            "g": "GROUP",
            "u": "CODE",
            "g2": "GROUP2",
            "c": "UNIT_NAME",
            "r1": "ROCKTYPE1",
            "r2": "ROCKTYPE2",
            "min": "MIN_AGE",
            "max": "MAX_AGE",
            "o": "GEOMETRY_OBJECT_ID",
        }
        reserved_labels_fault = {
            "n": "NAME",
            "f": "FEATURE",
            "fdip": "DIP",
            "fdipdir": "DIPDIR",
            "fdipest": "DIP_ESTIMATE",
            "o": "GEOMETRY_OBJECT_ID",
        }

        for label in reserved_labels_structure.keys():
            if (
                self.c_l[label] in reserved_labels_structure.values()
                and not self.c_l[label] == reserved_labels_structure[label]
            ):
                raise ValueError(
                    f"Metadata label '{label}' uses reserved keyword {self.c_l[label]} from structure dataframes, please rename this column to something else."
                )
        for label in reserved_labels_geology.keys():
            if (
                self.c_l[label] in reserved_labels_geology.values()
                and not self.c_l[label] == reserved_labels_geology[label]
            ):
                raise ValueError(
                    f"Metadata label '{label}' uses reserved keyword {self.c_l[label]} from geology dataframes, please rename this column to something else."
                )
        for label in reserved_labels_fault.keys():
            if (
                self.c_l[label] in reserved_labels_fault.values()
                and not self.c_l[label] == reserved_labels_fault[label]
            ):
                raise ValueError(
                    f"Metadata label '{label}' uses reserved keyword {self.c_l[label]} from fault dataframes, please rename this column to something else."
                )

    # TODO: Make it more obvious when formations are not found and random colours are assigned
    def create_cmap(self):
        # Make colours consistent from map to model
        formations = sorted(
            [
                formation.replace(" ", "_").replace("-", "_")
                for formation in list(
                    set(
                        self.map_data.get_map_data(Datatype.GEOLOGY)[
                            "UNIT_NAME"
                        ].to_numpy()
                    )
                )
            ]
        )
        temp_colours = [""] * len(formations)
        self.colour_dict = dict(zip(formations, temp_colours))
        try:
            # Try to retrieve the clut reference
            colour_ref = pd.read_csv(self.clut_path)
            for formation in formations:
                key = formation.replace("-", "_")
                colour = None
                try:
                    colour = colour_ref[colour_ref["code"] == key]["colour"].to_numpy()[
                        0
                    ]
                except Exception:
                    try:
                        colour = colour_ref[colour_ref["UNITNAME"] == key][
                            "colour"
                        ].to_numpy()[0]
                    except Exception:
                        print(
                            "formation "
                            + formation
                            + " does not have a colour reference"
                        )
                        colour = "#%02X%02X%02X" % (
                            random.randint(0, 255),
                            random.randint(0, 255),
                            random.randint(0, 255),
                        )

                self.colour_dict[formation] = colour
                # print(key, colour)

        except Exception:
            # Otherwise, just append a random set
            self.clut_path = ""
            random_colours = [
                "#%02X%02X%02X"
                % (
                    random.randint(0, 255),
                    random.randint(0, 255),
                    random.randint(0, 255),
                )
                for i in range(len(formations))
            ]
            i = 0
            for key in self.colour_dict.keys():
                self.colour_dict[key] = random_colours[i]
                i = i + 1

        self.cmap = colors.ListedColormap(self.colour_dict.values(), name="geol_key")

    @beartype.beartype
    def save_cmap(self, workflow: dict):
        """Create a colourmap for the model using the colour code"""
        all_sorts = pd.read_csv(os.path.join(self.tmp_path, "all_sorts_clean.csv"))

        if workflow["cover_map"]:
            self.colour_dict["cover"] = "#FFFFC0"
            self.colour_dict["cover_up"] = "#FFFFC0"
        colours = []
        newKeys = [key.replace("-", "_") for key in self.colour_dict.keys()]
        newColourDict = dict(zip(newKeys, list(self.colour_dict.values())))
        for code in all_sorts["code"]:
            # colours.append([self.colour_dict[code]])
            code = code.replace("-", "_")
            colours.append([newColourDict[code]])
        data = colours
        expected_extra_cols = pd.DataFrame(columns=["colour"], data=data)
        all_sorts = pd.concat([all_sorts, expected_extra_cols], axis=1)
        all_sorts.to_csv(
            os.path.join(self.tmp_path, "all_sorts_clean.csv"), ",", index=None
        )


from dateutil import tz
from dateutil.parser import parse

ET = tz.gettz('US/Eastern')
CT = tz.gettz('US/Central')
MT = tz.gettz('US/Mountain')
PT = tz.gettz('US/Pacific')

us_tzinfos = {  'CST': CT, 'CDT' : CT,
                'EST': ET, 'EDT' : ET,
                'MST': MT, 'MDT' : MT,
                'PST': PT, 'PDT' : PT}

dt_est = parse('2014-01-02 04:00:00 EST', tzinfos=us_tzinfos)
dt_pst = parse('2016-03-11 16:00:00 PST', tzinfos=us_tzinfos)

print(dt_est)
print(dt_pst)


import numpy as np
import scipy as sp
import scipy.fftpack
import pandas as pd
from datetime import datetime
import random
import matplotlib.pyplot as plt
from common import process_merchant_avg_cents_hour_of_day, make_df

first_day = datetime(2033, 1, 1)
last_day = datetime(2034, 12, 31)

df = make_df()
by_merchant = df.groupby("merchant")

df_churned_merchants = pd.read_pickle("churned_merchants.pkl")
print(df_churned_merchants[0:20])

def get_threshold(transactions):
    from skimage.filters import threshold_otsu, threshold_local
    transactions = np.row_stack(( transactions, np.ones(transactions.shape)))
    thresh = threshold_otsu(transactions)
    binary = transactions > thresh
    return thresh

def threshold_transactions(transactions, threshold):
    transactions[transactions <= threshold] = 0
    return transactions

def get_boolean_transactions(transactions, threshold):
    bool_array = transactions > threshold
    bool_array[-1] = True
    return bool_array

def plot_merchant(t_days, transactions, id_merchant):
    fig, ax = plt.subplots()
    ax.plot(t_days , transactions, '.-')
    plt.xlabel("Days")
    plt.ylabel("Transaction Amount (cents)")
    plt.title("Transactions of Merchant {}".format(hex(id_merchant)[2:]))

def plot_merchant_bool(t_days, transactions, bool_transactions, id_merchant):
    f, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
    ax1.plot(t_days , transactions, '.-')
    ax2.plot(t_days, bool_transactions)
    ax2.set_xlabel("Days")
    ax1.set_ylabel("Transaction Amount (cents)")
    ax1.set_title("Transactions of Merchant {}".format(hex(id_merchant)[2:]))
    ax2.set_title("Are There Sales Today?")
    ax2.set_ylabel("Yes (1) / No (0)")

def process_merchant_to_time_series(single_merchant):
    id_merchant = single_merchant['merchant'].iloc[0]
    df_end = pd.DataFrame([[last_day, id_merchant, 0]], columns=['time', 'merchant', 'cents'])
    df_beg = pd.DataFrame([[first_day, id_merchant, 0]], columns=['time', 'merchant', 'cents'])
    single_merchant = single_merchant.append([df_beg, df_end], ignore_index=True)

    # Resample the dataframe
    single_merchant = single_merchant.set_index(['time'])

    df_hourly = pd.DataFrame()
    df_hourly['cents'] = single_merchant['cents'].resample('1D').count()
    df_hourly.insert(1, "merchant", id_merchant)
    df_hourly = df_hourly.reset_index('time')

    transactions = df_hourly['cents'].to_numpy()
    first_of_year = datetime( df.agg({"time": "min"})[0].year, 1, 1 )
    t = df_hourly['time'] - first_of_year
    t_secs = t.dt.total_seconds().to_numpy()
    t_days = t_secs / (3600 * 24)
    return t_days, transactions

id_merchant = int(653416108)
single_merchant = by_merchant.get_group(id_merchant)
churned_merchant = df_churned_merchants.set_index("merchant").loc[id_merchant]
print(churned_merchant)
t_days, transactions = process_merchant_to_time_series(single_merchant)

threshold = get_threshold(transactions)
thresholded_transactions = threshold_transactions(transactions, threshold)
bool_transactions = get_boolean_transactions(transactions, threshold)
i_non_zero = np.nonzero(bool_transactions)
i_diff_non_zero = np.diff(i_non_zero[0])

plot_merchant_bool(t_days[0:-1], transactions[0:-1], bool_transactions[0:-1], id_merchant)
plt.show()


###############
# Authored by Weisheng Jiang
# Book 4  |  From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############

import plotly.graph_objects as go
import numpy as np
import streamlit as st
from scipy.stats import multivariate_normal

st.latex(r'''{\displaystyle f_{\mathbf {X} }(x_{1},\ldots ,x_{k})={\frac {\exp \left(-{\frac {1}{2}}({\mathbf {x} }-{\boldsymbol {\mu }})^{\mathrm {T} }{\boldsymbol {\Sigma }}^{-1}({\mathbf {x} }-{\boldsymbol {\mu }})\right)}{\sqrt {(2\pi )^{k}|{\boldsymbol {\Sigma }}|}}}}''')

def bmatrix(a):
    """Returns a LaTeX bmatrix

    :a: numpy array
    :returns: LaTeX bmatrix as a string
    """
    if len(a.shape) > 2:
        raise ValueError('bmatrix can at most display two dimensions')
    lines = str(a).replace('[', '').replace(']', '').splitlines()
    rv = [r'\begin{bmatrix}']
    rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
    rv +=  [r'\end{bmatrix}']
    return '\n'.join(rv)

# x1_array = np.linspace(-3,3,301)
# x2_array = np.linspace(-3,3,301)
# x3_array = np.linspace(-3,3,301)

xxx1,xxx2,xxx3 = np.mgrid[-3:3:0.2,-3:3:0.2,-3:3:0.2]

with st.sidebar:

    sigma_1 = st.slider('sigma_1', min_value=0.5, max_value=3.0, value=1.0, step=0.1)
    sigma_2 = st.slider('sigma_2', min_value=0.5, max_value=3.0, value=1.0, step=0.1)
    sigma_3 = st.slider('sigma_3', min_value=0.5, max_value=3.0, value=1.0, step=0.1)

    rho_1_2 = st.slider('rho_1_2', min_value=-0.95, max_value=0.95, value=0.0, step=0.05)
    rho_1_3 = st.slider('rho_1_3', min_value=-0.95, max_value=0.95, value=0.0, step=0.05)
    rho_2_3 = st.slider('rho_2_3', min_value=-0.95, max_value=0.95, value=0.0, step=0.05)

SIGMA = np.array([[sigma_1**2, rho_1_2*sigma_1*sigma_2, rho_1_3*sigma_1*sigma_3],
                  [rho_1_2*sigma_1*sigma_2, sigma_2**2, rho_2_3*sigma_2*sigma_3],
                  [rho_1_3*sigma_1*sigma_3, rho_2_3*sigma_2*sigma_3, sigma_3**2]])

st.latex(r'\Sigma = ' + bmatrix(SIGMA))

MU = np.array([0, 0, 0])

# st.write(xxx1.shape)
pos = np.dstack((xxx1.ravel(),xxx2.ravel(),xxx3.ravel()))

# st.write(pos.shape)
rv  = multivariate_normal(MU, SIGMA)
PDF = rv.pdf(pos)

fig = go.Figure(data=go.Volume(
    x=xxx1.flatten(),
    y=xxx2.flatten(),
    z=xxx3.flatten(),
    value=PDF.flatten(),
    isomin=0,
    isomax=PDF.max(),
    colorscale = 'RdYlBu_r',
    opacity=0.1,
    surface_count=11,
    ))

fig.update_layout(scene = dict(
                    xaxis_title=r'x_1',
                    yaxis_title=r'x_2',
                    zaxis_title=r'x_3'),
                    width=1000,
                    margin=dict(r=20, b=10, l=10, t=10))

st.plotly_chart(fig, theme=None, use_container_width=True)


import dash_bootstrap_components as dbc
from dash import dcc
from dash import html
from dash import dash_table

def Forecast(pdb):

  pm = pdb.current_portfolio
  tickers = [ticker.ticker for ticker in pm.tickers]
  cg = pdb.current_content

  fig_portfolio_prediction = cg.forecast_portfolio
  portfolio_perfomance_df = cg.forecast_perfomance_df

  body = dbc.Container(
    [
      dbc.Row(
        [
          dbc.Col(
            [
              dcc.Graph( id='graph-portfolio_forecast',figure=fig_portfolio_prediction ),
              html.P( "Montecarlo simulations predict the following scenario for the portfolio:"),
              html.Center([dash_table.DataTable(id='table-portfolio_perfomance', data=portfolio_perfomance_df.to_dict('records'),
                                  columns=[{"name": i, "id": i} for i in portfolio_perfomance_df.columns],
                                  fill_width=False
                          )])
            ], width=6, style={'marginLeft': 'auto', 'marginRight': 'auto'},
          ),
          dbc.Col(
            [
              dcc.Dropdown(
                id='dropdown-forecast_asset',
                options=[
                  {'label':i, 'value':i} for i in tickers
                ],
                value=tickers[0],
                placeholder="Select an asset"
              ),
              dcc.Graph( id='graph-asset_forecast' ),
              html.Center([html.Div(id='table-asset_perfomance')]),
            ], width=6,
          ),
        ], justify="center", style={'marginLeft': 'auto', 'marginRight': 'auto'}
      )
    ],
  className="mt-4",
  )

  layout = html.Div([
    body
  ])

  return layout


from django.core.management.base import BaseCommand

from foodgram.models import Ingredient
import csv
from pathlib import Path
import logging

logging.basicConfig(filename='default.log')

class Command(BaseCommand):
    help = 'Load data from csv'

    def handle(self, *args, **options):
        path_to_dir = f'{Path(__file__).resolve().parent.parent.parent}'
        final_path = path_to_dir + '/data/ingredients.csv'
        with open(final_path) as csv_file:
            file_reader = csv.reader(csv_file)
            for row in file_reader:
                title, unit = row
                Ingredient.objects.get_or_create(title=title, unit=unit)
        logging.info(f'Добавлено {Ingredient.objects.count()} игредиентов')


'''
This is the main code of the ECAPATDNN project, to define the parameters and build the construction
'''

import argparse, glob, os, torch, warnings, time
from utils.tools import *
from utils.dataLoader import train_loader, eval_loader,my_collate_fn
from utils.ECAPAModel import ECAPAModel

parser = argparse.ArgumentParser(description = "ECAPA_trainer")
parser.add_argument('--device',      type=str,   default='cuda:0',       help='Device training on ')
## Training Settings
parser.add_argument('--num_frames', type=int,   default=300,     help='Duration of the input segments, eg: 200 for 2 second')
parser.add_argument('--max_epoch',  type=int,   default=80,      help='Maximum number of epochs')
parser.add_argument('--batch_size', type=int,   default=128,     help='Batch size')
parser.add_argument('--n_cpu',      type=int,   default=4,       help='Number of loader threads')
parser.add_argument('--test_step',  type=int,   default=1,       help='Test and save every [test_step] epochs')
parser.add_argument('--lr',         type=float, default=0.001,   help='Learning rate')
parser.add_argument("--lr_decay",   type=float, default=0.97,    help='Learning rate decay every [test_step] epochs')

## Training and evaluation path/lists, save path
parser.add_argument('--train_list', type=str,   default="./data/train_df",     help='The path of the training list, eg:"/data08/VoxCeleb2/train_list.txt" in my case')
parser.add_argument('--eval_list',  type=str,   default="./data/valid_df",              help='The path of the evaluation list, eg:"/data08/VoxCeleb1/veri_test2.txt" in my case')
parser.add_argument('--eval_max_length', type=int,  default=120000,  help='the max length of evaluate audio')
parser.add_argument('--save_path',  type=str,   default="./exps",                                     help='Path to save the score.txt and models')
parser.add_argument('--initial_model',  type=str,   default="",                                          help='Path of the initial_model')

## Model and Loss settings
parser.add_argument('--C',       type=int,   default=1024,   help='Channel size for the dialect encoder')
parser.add_argument('--m',       type=float, default=0.2,    help='Loss margin in AAM softmax')
parser.add_argument('--s',       type=float, default=30,     help='Loss scale in AAM softmax')
parser.add_argument('--n_class', type=int,   default=9,   help='Number of dialects')
parser.add_argument('--loss' ,   type=str, default="PairDistanceLoss", help='Target and loss function')
## Command
parser.add_argument('--eval',    dest='eval', action='store_true', help='Only do evaluation')

## Initialization
warnings.simplefilter("ignore")
torch.multiprocessing.set_sharing_strategy('file_system')
args = parser.parse_args()
args = init_args(args)

## Define the data loader
train_data = train_loader(**vars(args))
trainLoader = torch.utils.data.DataLoader(train_data, batch_size = args.batch_size, shuffle = True, num_workers = args.n_cpu, drop_last = True)

def my_collate_fn(data, max_length=args.eval_max_length):

    lens = [x[0].shape[0] for x in data]
    max_len = max(lens)
    max_length = min(max_len, max_length)

    features = torch.zeros(len(data), max_length)
    for i, length in enumerate(lens):
        features[i,:length] = data[i][0][:max_length]
    labels = torch.tensor([x[1] for x in data])

    return features, labels

eval_data = eval_loader(**vars(args))
evalLoader = torch.utils.data.DataLoader(eval_data, batch_size = args.batch_size*2, shuffle = True, num_workers = args.n_cpu, drop_last = True, collate_fn=my_collate_fn)

## Search for the exist models
#modelfiles = glob.glob('%s/model_0*.model'%args.model_save_path)
#modelfiles.sort()
modelfiles = []

## Only do evaluation, the initial_model is necessary
if args.eval == True:
    s = ECAPAModel(**vars(args))
    print("Model %s loaded from previous state!"%args.initial_model)
    s.load_parameters(args.initial_model)
    loss = s.eval_network(loader=evalLoader)
    print("EvalLoss %2.2f"%(loss))
    quit()

## If initial_model is exist, system will train from the initial_model
if args.initial_model != "":
    print("Model %s loaded from previous state!"%args.initial_model)
    s = ECAPAModel(**vars(args))
    s.load_parameters(args.initial_model)
    epoch = 1

## Otherwise, system will try to start from the saved model&epoch
elif len(modelfiles) >= 1:
    print("Model %s loaded from previous state!"%modelfiles[-1])
    epoch = 1
    s = ECAPAModel(**vars(args))
    s.load_parameters(modelfiles[-1])
    ## Otherwise, system will train from scratch
else:
    epoch = 1
    s = ECAPAModel(**vars(args))

eval_losses = []
score_file = open(args.score_save_path, "a+")

while(1):
    ## Training for one epoch
    loss, lr = s.train_network(epoch = epoch, loader = trainLoader)

    ## Evaluation every [test_step] epochs
    if epoch % args.test_step == 0:
        s.save_parameters(args.model_save_path + "/model_%04d.model"%epoch)
        eval_losses.append(s.eval_network(loader=evalLoader))
        print(time.strftime("%Y-%m-%d %H:%M:%S"), "%d epoch, EvalLoss %2.2f, minEvalLoss %2.2f"%(epoch,eval_losses[-1], min(eval_losses)))
        score_file.write("%d epoch, LR %f, LOSS %f, EvalLoss %2.2f, minEvalLoss %2.2f\n"%(epoch, lr, loss, eval_losses[-1], min(eval_losses)))
        score_file.flush()

    if epoch >= args.max_epoch:
        quit()

    epoch += 1


import spritesheet_handler, pygame
pygame.init()

#Base class for all beings in the game.
class Olio():
    def __init__(self,posX,posY,Sprite, SpriteWidth, SpriteHeight):
        #Each character has a position, and a sprite sheet.
        self.__x = posX
        self.__y = posY
        self.__sprites = spritesheet_handler.spritesheet(Sprite)
        #self.__n = 0
        self.__spriteWidth = SpriteWidth
        self.__spriteHeight = SpriteHeight
        #self.__runner = SpriteStripAnim(Sprite,pygame.Rect(216,2,41,351),5)
        self.__leftruns = [(216,0,41,65),
                           (216,72,41,65),
                           (216,144,41,67),
                           (216,215,37,68),
                            (216,287,41,64)]

    def __get_anims(SpritePosList):
        for column in SpritePosList:
            self.__anims.append

    #The function for drawing the character.
    def __draw__(self, gameDisplay,moveX,moveY):
        image = self.__sprites.image_at((0,0,41,65))
        if moveY > 0:
            try:
                image = self.__sprites.image_at(self.__leftruns[self.__n])
                self.__n += 1
            except IndexError:
                self.__n = 0
            #image = self.__runner.
        #elif moveY != 0:
        gameDisplay.blit(image,(self.__x,self.__y))
        #gameDisplay.blit(self.__sprites.image_at(pygame.Rect(1,2,40,64)),(self.__x,self.__y))

    #Move character on the X-axis.
    def __move_x_axis__(self, moveAmount):
        self.__x += moveAmount

    #Move character on the Y-axis.
    def __move_y_axis__(self, moveAmount):
        self.__y += moveAmount

    #Function for handling the animation. Work in progress.
    def __animation(self):
        self.__runner

#Class of the Player character. Derived from the base character class Olio.
class main_dude(Olio):
    def __nothing(self):
        pass
    '''
    def __init__(self,posX,posY,Sprite):
        self.__x = posX
        self.__y = posY
        self.__sprites = spritesheet_handler.spritesheet(Sprite)'''

#class enemy(olio):


import numpy as np
import progressbar
import gym
from tensorflow.python.keras.optimizer_v2.adam import Adam

from rl.agent import Agent

# note: for 08.10.2022 requires a stable_baselines3 package to be installed
if __name__ == '__main__':
    # prepare an environment
    environment = gym.make("Taxi-v3")
    environment.reset()
    environment.render()

    print('Number of states: {}'.format(environment.observation_space.n))
    action_space = environment.action_space
    print('Number of actions: {}'.format(action_space.n))

    optimizer = Adam(learning_rate=0.01)
    agent = Agent(environment, optimizer)

    batch_size = 2000  # 2000
    num_of_episodes = 20  # 20
    time_steps_per_episode = 100  # 100
    agent.q_network.summary()

    # training (could take 30m+, increase batch_size/num_of_episodes, decrease time_steps_per_episode to speed up)
    for e in range(0, num_of_episodes):
        # Reset the environment
        state = environment.reset()
        state = np.reshape(state, [1, 1])

        # Initialize variables
        reward = 0
        terminated = False

        bar = progressbar.ProgressBar(maxval=time_steps_per_episode / 10,
                                      widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
        bar.start()

        for timestep in range(time_steps_per_episode):
            # Run Action
            action = agent.act(environment, state)

            # Take action
            next_state, reward, terminated, info = environment.step(action)
            next_state = np.reshape(next_state, [1, 1])
            agent.store(state, action, reward, next_state, terminated)

            state = next_state

            # average termination is on the 20th step
            if terminated:
                agent.align_target_model()
                break

            if len(agent.experience_replay) > batch_size:
                agent.retrain(batch_size)

            if timestep % 10 == 0:
                bar.update(timestep / 10 + 1)

        bar.finish()
        if (e + 1) % 10 == 0:
            print("**********************************")
            print("Episode: {}".format(e + 1))
            environment.render()
            print("**********************************")
    print("Model trained!")

    # model evaluation (could take 30m+, decrease test_num_of_episodes to speed up)
    test_total_epochs = 0
    test_total_penalties = 0
    test_num_of_episodes = 15  # 100

    for _ in range(test_num_of_episodes):
        print(f'Episode {_}')
        state = environment.reset()
        state = np.reshape(state, [1, 1])
        epochs = 0
        penalties = 0
        reward = 0

        terminated = False

        while not terminated:
            if epochs % 10 == 0:
                print(f'Timestep {epochs}')
            action = np.argmax(agent.q_network.predict(state)[0])
            state, reward, terminated, info = environment.step(action)
            state = np.reshape(state, [1, 1])

            if reward == -10:
                penalties += 1

            epochs += 1

        test_total_penalties += penalties
        test_total_epochs += epochs

    print("**********************************")
    print("Results")
    print("**********************************")
    print("Epochs per episode: {}".format(test_total_epochs / test_num_of_episodes))
    print("Penalties per episode: {}".format(test_total_penalties / test_num_of_episodes))
    # fixme model is walking on the field for too long and makes a lot of wrong pickups/drops


'''
Author: your name
Date: 2021-10-05 22:17:39
LastEditTime: 2021-10-07 01:47:21
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \show_trajectory\video2seq.py
'''
import multiprocessing as mp
import time
import cv2
import os

def Video2Image(videoFile, outputFile):
    vc = cv2.VideoCapture(videoFile)
    if vc.isOpened():
        rval, frame = vc.read()
    else:
        print('openerror!')
        rval = False

    frame_id = 1
    while rval:
        rval, frame = vc.read()
        frame_name = outputFile + str(frame_id).zfill(6) + '.jpg'
        if frame is None:
            print(frame_id)
            continue
        cv2.imwrite(frame_name, frame)
        frame_id += 1

    vc.release()

if __name__ == "__main__":
    video_dir = "figures/video"
    output_dir = "figures/trajectory"

    for seq_name in os.listdir(video_dir):
        st = time.time()
        name = os.path.splitext(seq_name)[0]
        videoFile = os.path.join(video_dir, seq_name)
        outputFile = os.path.join(output_dir, name+"/")
        if not os.path.exists(outputFile):
            os.makedirs(outputFile, exist_ok=True)

        Video2Image(videoFile, outputFile)

        print("[%.2f sec] Processed %s." % (time.time()-st, name))


import pygame, random, sys
pygame.init()
screen = pygame.display.set_mode((800,600))

draw_on = False
last_pos = (0, 0)
color = (255, 128, 0)
radius = 10

def roundline(srf, color, start, end, radius=1):
    dx = end[0]-start[0]
    dy = end[1]-start[1]
    distance = max(abs(dx), abs(dy))
    for i in range(distance):
        x = int( start[0]+float(i)/distance*dx)
        y = int( start[1]+float(i)/distance*dy)
        pygame.draw.circle(srf, color, (x, y), radius)

try:
    while True:
        e = pygame.event.wait()
        print(e)

        if e.type == pygame.QUIT:
            raise StopIteration
        if e.type == pygame.MOUSEBUTTONDOWN:
            if e.button == 3:
                print('\nSaved my bit!')
                fname = "hml.png"
                pg.image.save(win, fname)
                pygame.quit()
            color = (random.randrange(256), random.randrange(256), random.randrange(256))
            pygame.draw.circle(screen, color, e.pos, radius)
            draw_on = True
        if e.type == pygame.MOUSEBUTTONUP:
            draw_on = False
        if e.type == pygame.MOUSEMOTION:
            if draw_on:
                pygame.draw.circle(screen, color, e.pos, radius)
                roundline(screen, color, e.pos, last_pos,  radius)
            last_pos = e.pos
        pygame.display.flip()
        pygame.event.pump()
except StopIteration:
    pass

pygame.quit()


import os
import platform
from pathlib import Path
import subprocess

IS_WINDOWS = (platform.system() == 'Windows')

if IS_WINDOWS:
    UNRAR_EXE = str(Path(__file__).parent / 'bin' / 'UnRAR.exe')
else:
    UNRAR_EXE = 'unrar'

def unrar(filepath, folder, unrar_executable=UNRAR_EXE):
    # UnRAR wants its folder argument with '/'
    folder = "{}{}".format(folder, os.sep)
    tokens = [unrar_executable, 'e', str(filepath), folder, '-y']
    try:
        return subprocess.check_call(tokens)
    except subprocess.CalledProcessError as e:
        raise e

class DocFiles:
    def __init__(self, folder):
        self.folder = Path(folder)

    @property
    def paths(self):
        return [str(x) for x in self.folder.glob('*.doc*')]

    def exist(self):
        return len(self.paths) > 0

    def __str__(self):
        return ' ' * 4 + ('\n' + ' ' * 4).join(self.paths)

class Unpacker:
    def __init__(self, archive_filepath,
                 destination_folder):
        self.path = archive_filepath
        if not Path(self.path).exists():
            raise FileNotFoundError(self.path)
        self.folder = destination_folder
        self.status = (f'Already unpacked:\n{self.docs}'
                       if self.docs.exist()
                       else None)

    @property
    def docs(self):
        return DocFiles(self.folder)

    def run(self):
        res = unrar(self.path, self.folder)
        if res == 0:
            self.status = f'UnRARed {self.path}'
            return True
        return False


import os
from flask import Flask, Blueprint,request, render_template, redirect, url_for
from werkzeug.utils import secure_filename
from blueprints import *
import cv2
import base64
import sql
import tensorflow as tf
import pathlib as p

model = tf.keras.models.load_model('models/3_96_25k.h5')

upload_api = Blueprint('upload_api', __name__)

@upload_api.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
   if request.method == 'POST':
      try:
        f = request.files['file']
        f.save(os.path.join(str(p.Path.cwd() /'static'/'images'),secure_filename(f.filename)))
        preprocess_img = sql.load_and_preprocess_image(os.path.join(str(p.Path.cwd() /'static'/'images' / str(f.filename))))
        preprocess_img = tf.reshape(preprocess_img,[1,192,192,3])
        predict = model.predict(preprocess_img)
        d = {0: 'cat',
                1: 'dog'}
        predict = d[predict.argmax()]
        return render_template('predict.html', img=f.filename, predict = predict)
      except:
        data = request.json
        print('1' not in data)
        img_name = data.split(" ")[0]
        path = os.path.join(str(p.Path.cwd() /'static'/'images' / img_name))
        if '1' not in data:
          label = {"cat":1,"dog":0}.get(data.split(" ")[1])
          sql.save_into_db(path,label)
        os.remove(path)
        return render_template('predict.html', img="", predict = "")
if __name__ == '__main__':
   app.run(debug = True)


import math
from PIL import Image

suites = ['Heart', 'Club', 'Diamond', 'Spade']
values = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']

class Card:
    suit = None
    value = None

    image_dimensions = 71, 96
    image_pad = 2

    def __init__ (self, suit, value):
        self.suit = int(suit)
        self.value = int(value)

    def __str__ (self):
        return "%s of %ss (%s)" % (values[self.value], suites[self.suit], self.get_color())

    def get_color (self):
        return 'Black' if (self.suit % 2) else 'Red'

    def get_card_image (self):
        suit_translation = [2,0,3,1][self.suit]

        im = Image.open('cards.png')
        x = self.image_pad * (self.value + 1) + self.value * self.image_dimensions[0]
        y = 1 + self.image_pad * suit_translation + suit_translation * self.image_dimensions[1]
        return im.crop((
            x,
            y,
            x + self.image_dimensions[0],
            y + self.image_dimensions[1]
        ))

def build_new_deck ():
    deck = []
    for i in range(0, 52):
        suit = math.floor(i/13)
        value = i % 13
        if i >= 26:
            value = 12 - value

        deck.append(Card(
            suit,
            value
        ))
    return deck

def faro_shuffle (deck):
    half_count = len(deck)/2
    left_cut = deck[:half_count]
    right_cut = deck[half_count:]
    new_deck = []
    for i in range(0, half_count):
        new_deck.append(right_cut[i])
        new_deck.append(left_cut[i])
    return new_deck

def longest_sequences (deck):
    max_suite_sequence = 0
    max_value_sequence = 0
    max_color_sequence = 0

    previous_suite = None
    previous_value = None
    previous_color = None
    current_value_direction = None # -1 descending, 0 duplicates, 1 ascending

    current_suite_sequence = 0
    current_value_sequence = 0
    current_color_sequence = 0

    for card in deck:
        if card.suit != previous_suite:
            max_suite_sequence = max(max_suite_sequence, current_suite_sequence)
            current_suite_sequence = 0

        if card.value == previous_value:
            if current_value_direction != 0:
                max_value_sequence = max(max_value_sequence, current_value_sequence)
                current_value_direction = 0
                current_value_sequence = 1
        elif card.value - 1 == previous_value:
            if current_value_direction != 1:
                max_value_sequence = max(max_value_sequence, current_value_sequence)
                current_value_direction = 1
                current_value_sequence = 1
        elif card.value + 1 == previous_value:
            if current_value_direction != -1:
                max_value_sequence = max(max_value_sequence, current_value_sequence)
                current_value_direction = -1
                current_value_sequence = 1
        else:
            max_value_sequence = max(max_value_sequence, current_value_sequence)
            current_value_direction = None
            current_value_sequence = 0

        if card.get_color() != previous_color:
            max_color_sequence = max(max_color_sequence, current_color_sequence)
            current_color_sequence = 0

        current_suite_sequence += 1
        current_value_sequence += 1
        current_color_sequence += 1

        previous_suite = card.suit
        previous_value = card.value
        previous_color = card.get_color()

    max_suite_sequence = max(max_suite_sequence, current_suite_sequence)
    max_value_sequence = max(max_value_sequence, current_value_sequence)
    max_color_sequence = max(max_color_sequence, current_color_sequence)

    return max_suite_sequence, max_value_sequence, max_color_sequence

def print_deck (deck):
    for card in deck:
        print(card)

def save_deck_image (deck, name):
    til = Image.new("RGB",(834,95))
    x = 0
    for card in deck:
        til.paste(card.get_card_image(),(x,0))
        x += 15
    til.save("images/%s.png" % name)

deck = build_new_deck()
for i in range(0, 100):
    max_suite_sequence, max_value_sequence, max_color_sequence = longest_sequences(deck)
    save_deck_image(deck, '%s - %s, %s, %s' % (i, max_suite_sequence, max_value_sequence, max_color_sequence))
    deck = faro_shuffle(deck)


from typing import Dict, List, Optional, Tuple

import numpy as np
import tensorflow as tf
from tensorflow.python.framework import tensor_util

from simple_graph import SimpleGraph, SimpleNode

# [Basic graph utils]
def get_node_name_parts_from_input(input_name: str) -> Tuple[str, str, str]:
    input_parts = input_name.split(":")
    if len(input_parts) < 2:
        suffix = ""
    else:
        suffix = ":" + input_parts[1]
    node_name = input_parts[0]
    if node_name[0] == "^":
        prefix = "^"
        node_name = node_name[1:]
    else:
        prefix = ""
    return prefix, node_name, suffix

def get_node_name_from_input(input_name: Optional[str]) -> str:
    if input_name is None:
        return ""
    _, node_name, _ = get_node_name_parts_from_input(input_name)
    return node_name

def get_canonical_input_name(input_name: str) -> str:
    prefix, node_name, suffix = get_node_name_parts_from_input(input_name)
    suffix = ":0" if suffix == "" else suffix
    return prefix + node_name + suffix

def get_const_value(node: tf.NodeDef) -> np.ndarray:
    # Alternatively
    # tf.contrib.util.constant_value(tensor) will get a tensor"s constant value
    return tensor_util.MakeNdarray(node.attr["value"].tensor)

def get_const_value_by_name(
    graph_def: tf.GraphDef, name: str, simple_graph: Optional[SimpleGraph] = None
) -> np.ndarray:
    if simple_graph:
        node = get_node_by_name(graph_def, simple_graph, name)
        return get_const_value(node)
    else:
        node_name = get_node_name_from_input(name)
        founds = [nd for nd in graph_def.node if nd.name == node_name]
        if len(founds) == 0:
            error_msg = "Unknown node name: {}".format(node_name)
            raise Exception(error_msg)
        return get_const_value(founds[0])

# deprecated: use SimpleGraph.get_node_by_name instead
def get_node_by_name(
    graph_def: Optional[tf.GraphDef], simple_graph: SimpleGraph, name: str
) -> tf.NodeDef:
    return simple_graph.get_node_by_name(name)

# deprecated: use simple_graph.get_simple_node_by_name instead
def get_simple_node_by_name(simple_graph: SimpleGraph, name: str) -> SimpleNode:
    return simple_graph.get_simple_node_by_name(name)

# [Pattern matching]
def check_inputs(  # noqa: C901
    simple_graph: SimpleGraph,
    current_node: SimpleNode,
    pattern_nodes: Dict[str, SimpleNode],
    first_node: SimpleNode,
    matched_name_map: Dict[str, str],
) -> bool:
    # check op type
    if first_node.op != "*":
        matched_ops = [op.strip() for op in first_node.op.split("|")]
        if current_node.op not in matched_ops:
            return False
    # check node name
    if first_node.name in matched_name_map:
        if matched_name_map[first_node.name] != current_node.name:
            return False
    # check inputs
    if (len(first_node.inputs) == 1) and (first_node.inputs[0] == "*"):
        matched_name_map[first_node.name] = current_node.name
        return True
    # if inputs contains both unknown inputs and known inputs
    if (len(first_node.inputs) > 1) and ("*" in first_node.inputs):
        known_inputs = [name for name in first_node.inputs if name != "*"]
        start_idx = 0
        for key_name in known_inputs:
            matched = False
            if key_name.isdigit():
                matched = True
                continue
            for i in range(start_idx, len(current_node.inputs)):
                input_name = current_node.inputs[i]
                cur_input_node = simple_graph.get_simple_node_by_name(input_name)
                expected_input_op_str = (pattern_nodes[key_name].op).strip()
                if "|" in expected_input_op_str:
                    expected_input_ops = expected_input_op_str.split("|")
                else:
                    expected_input_ops = list([expected_input_op_str])
                if (cur_input_node.op in expected_input_ops) and (
                    check_inputs(
                        simple_graph,
                        cur_input_node,
                        pattern_nodes,
                        pattern_nodes[key_name],
                        matched_name_map,
                    )
                ):
                    matched = True
                    start_idx = i
            if not matched:
                return False
    # if all listed inputs are known inputs
    else:
        if len(current_node.inputs) != len(first_node.inputs):
            return False
        for i, input_name in enumerate(current_node.inputs):
            cur_input_node = simple_graph.get_simple_node_by_name(input_name)
            if first_node.inputs[i].isdigit():
                continue
            tmp_input_node = pattern_nodes[first_node.inputs[i]]
            if not check_inputs(
                simple_graph,
                cur_input_node,
                pattern_nodes,
                tmp_input_node,
                matched_name_map,
            ):
                return False
    matched_name_map[first_node.name] = current_node.name
    return True

def get_matched_pattern(
    simple_graph: SimpleGraph,
    pattern_nodes: Dict[str, SimpleNode],
    first_node_key: str,
    init_name_map: Optional[Dict[str, str]] = None,
) -> List[Dict[str, str]]:
    matched_name_maps = list()
    for i, node in enumerate(simple_graph.nodes):
        simple_node = get_simple_node_by_name(simple_graph, node.name)
        tmp_name_map = init_name_map.copy() if init_name_map else dict()
        if check_inputs(
            simple_graph,
            simple_node,
            pattern_nodes,
            pattern_nodes[first_node_key],
            tmp_name_map,
        ):
            matched_name_maps.append(tmp_name_map)
    return matched_name_maps

def get_input_target_op_name(
    simple_graph: SimpleGraph,
    node_name: str,
    input_index: int,
    target_op: str,
    op_map: Dict[str, List[int]],
) -> Optional[str]:
    node = get_simple_node_by_name(simple_graph, node_name)
    input_node_name = get_node_name_from_input(node.inputs[input_index])
    input_node = get_simple_node_by_name(simple_graph, input_node_name)
    if input_node.op == target_op:
        return input_node_name
    if input_node.op in op_map:
        for tmp_index in op_map[input_node.op]:
            target_name = get_input_target_op_name(
                simple_graph, input_node_name, tmp_index, target_op, op_map
            )
            if target_name is not None:
                break
        return target_name
    else:
        return None

def remove_underscore_class_attr(graph_def: tf.GraphDef):
    for i, node in enumerate(graph_def.node):
        if '_class' in node.attr.keys():
            node.attr.pop('_class')


import os
import joblib
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis

def countLabels(set):
    count = {}
    for label in set:
        if label in count:
            count[label] += 1
        else:
            count[label] = 1

    return count

file = open('version', 'r')
version = file.read()
print(version)

file = open('version', 'w')
numbers = version.split('.')
file.write(f'{numbers[0]}.{numbers[1]}.{int(numbers[2]) + 1}')

dataPath = '../balanced/classification_dataset.csv'
classifierPath = f'../Classifiers/{version}/'

if not os.path.exists(classifierPath):
    os.mkdir(classifierPath)

# read training file
file = open(dataPath, 'r')
data = file.read().split('\n')
dataset = []
labels = []
for row in data:
    xy, label = row.split(',')
    xy = xy[:-1]
    xy = xy.split(' ')

    coordinates = []
    for pair in xy:
        x, y = pair.split(':')
        coordinates.append(float(x))
        coordinates.append(float(y))
    dataset.append(coordinates)
    labels.append(label)

dataset = np.array(dataset).astype(np.float32)

X_train, X_test, y_train, y_test = train_test_split(dataset, labels, test_size=0.2, stratify=labels)

names = [
    "kNN",
    "SVM",
    "NN",
    "NB",
    "QDA",
]

classifiers = [
    KNeighborsClassifier(3),
    SVC(gamma=2, C=1),
    MLPClassifier(alpha=1, max_iter=1000),
    GaussianNB(),
    QuadraticDiscriminantAnalysis(),
]

for i in range(len(classifiers)):
    classifier = classifiers[i]
    classifier.fit(X_train, y_train)
    score = classifier.score(X_test, y_test)
    print(f'{names[i]} : {score}')
    joblib.dump(classifier, f'{classifierPath}{names[i]}' + version + '.pkl')


from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from .models import Tour, Category, Types, Comment, Entry, Like

class CommentSerializer(serializers.ModelSerializer):
    user = serializers.CharField(source="user.username")
    tour = serializers.CharField(source="tour.title")

    class Meta:
        model = Comment
        fields = ('id', 'text', 'image', 'user', 'tour')

class TourSerializer(serializers.ModelSerializer):
    category = serializers.CharField(source="category.title")
    types = serializers.CharField(source="types.title")
    comments = CommentSerializer(many=True)

    class Meta:
        model = Tour
        fields = ('id', 'category', 'types', 'title', 'image', 'price', 'start_day', 'end_day', 'comments',
                  'likes_count')

        def get(self, request):
            serializer = TourSerializer(Tour.objects.all(), many=True)

class CategorySerializer(serializers.ModelSerializer):
    tours = serializers.SerializerMethodField()

    def get_tours(self, obj):
        return TourSerializer(obj.tours.all(), many=True).data

    class Meta:
        model = Category
        fields = ('id', 'title', 'tours')

class TypesSerializer(serializers.ModelSerializer):
    tours = serializers.SerializerMethodField()

    def get_tours(self, obj):
        return TourSerializer(obj.tours.all(), many=True).data

    class Meta:
        model = Types
        fields = ('id', 'title', 'tours')

class TourCrudSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tour
        fields = "__all__"

class EntrySerializer(serializers.ModelSerializer):
    """Запись на тур,Если мест нету выдаст ошибку"""

    def validate(self, attrs):
        print(attrs)
        quantity = attrs['tour'].quantity_of_seats
        quantity2 = Entry.objects.filter(tour=attrs['tour']).count()
        if quantity2 >= quantity:
            raise ValidationError(
                {
                    'quantity_of_seats': 'Извините, Все места забронированы!'
                                         'Вы можете записаться на другую дату!'
                }
            )
        return attrs

    class Meta:
        model = Entry
        fields = "__all__"

class DeleteCommentSerializer(serializers.ModelSerializer):
    '''Удаление коментариев Под Турами.
    Коментарий может удалить только тот человек кто его написал'''

    class Meta:
        model = Comment
        fields = ('id', 'user')

class LikeSerializer(serializers.ModelSerializer):
    likes_count = serializers.IntegerField(read_only=True)

    def to_representation(self, instance):
        data = super().to_representation(instance)
        if isinstance(instance, Like):
            data['likes_count'] = instance.like
        elif isinstance(instance, Tour):
            likes = Like.objects.filter(tour=instance)
            data['likes_count'] = likes.count()
        return data

    class Meta:
        model = Like
        fields = ('id', 'like', 'user', 'tour', 'likes_count')

class LikeDeleteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Like
        fields = ('id', 'user')


from django.urls import path
from .views import PostsList, PostDetail, PostCreate, PostUpdate, PostDelete, PostSearch, NewsCreate, ArticleCreate, ArticlesPost, NewsPost 	# Импортируем созданные нами представления

urlpatterns = [
    path('', PostsList.as_view(), name='post_list'),   # ???либо name='news_list' либо name='post_list'??? В данном случае путь ко всем постам у нас останется пустым(urlpatterns = [path('cheburashka/', ListPost.as_view())] # так ничего не выводится на страницу)(???)
    path('<int:pk>', PostDetail.as_view(), name='post_detail'),
    path('create/', PostCreate.as_view(), name='post_create'),
    path('news/create/', NewsCreate.as_view(), name='news_create'),
    path('articles/create/', ArticleCreate.as_view(), name='articles_create'),
    path('<int:pk>/update/', PostUpdate.as_view(), name='post_update'),          #??????????????????????
    path('<int:pk>/edit/', PostUpdate.as_view(), name='post_edit'),              #??????????????????????
    path('<int:pk>/delete/', PostDelete.as_view(), name='post_delete'),
    path('search/', PostSearch.as_view(), name='post_search'),
]
	# path — означает путь.
	# Т.к. наше объявленное представление является классом, а Django ожидает функцию, нам надо представить этот класс в виде view.
	# Для этого вызываем метод as_view.
    # pk — это первичный ключ публикации, который будет выводиться у нас в шаблон
    # int — указывает на то, что принимаются только целочисленные значения


"""
A custom OAuth2 application that allows wildcard for redirect_uris

https://github.com/jazzband/django-oauth-toolkit/issues/443#issuecomment-420255286
"""
import re
from urllib.parse import parse_qsl
from urllib.parse import urlparse

from django.core.exceptions import ValidationError

from oauth2_provider.models import AbstractApplication
from oauth2_provider.settings import oauth2_settings

def validate_uris(value):
    """ Ensure that `value` contains valid blank-separated URIs. """
    urls = value.split()
    for url in urls:
        obj = urlparse(url)
        if obj.fragment:
            raise ValidationError('Redirect URIs must not contain fragments')
        if obj.scheme.lower() not in oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES:
            raise ValidationError('Redirect URI scheme is not allowed.')
        if not obj.netloc:
            raise ValidationError('Redirect URI must contain a domain.')

class Application(AbstractApplication):
    """ Subclass of application to allow for regular expressions for the redirect uri. """

    @staticmethod
    def _uri_is_allowed(allowed_uri, uri):
        """ Check that the URI conforms to these rules. """
        schemes_match = allowed_uri.scheme == uri.scheme
        netloc_matches_pattern = re.fullmatch(allowed_uri.netloc, uri.netloc)
        paths_match = allowed_uri.path == uri.path

        return all([schemes_match, netloc_matches_pattern, paths_match])

    def __init__(self, *args, **kwargs):
        """ Relax the validator to allow for uris with regular expressions. """
        self._meta.get_field('redirect_uris').validators = [validate_uris, ]
        super(). __init__(*args, **kwargs)

    def redirect_uri_allowed(self, uri):
        """
        Check if given url is one of the items in :attr:`redirect_uris` string.
        A Redirect uri domain may be a regular expression e.g. `^(.*).example.com$` will
        match all subdomains of example.com.
        A Redirect uri may be `https://(.*).example.com/some/path/?q=x`
        :param uri: Url to check
        """
        for allowed_uri in self.redirect_uris.split():
            parsed_allowed_uri = urlparse(allowed_uri)
            parsed_uri = urlparse(uri)

            if self._uri_is_allowed(parsed_allowed_uri, parsed_uri):
                aqs_set = set(parse_qsl(parsed_allowed_uri.query))
                uqs_set = set(parse_qsl(parsed_uri.query))

                if aqs_set.issubset(uqs_set):
                    return True

        return False

    class Meta:
        db_table = 'oauth2_provider_application'


import numpy as np
import scipy as sp
from scipy import optimize
from scipy import stats
import matplotlib.pyplot as plt

def dilser(low=0.001, limit=100., dilfactor=2.):
    '''returns a numpy array dilution series from low to limit by dilfactor'''
    #replace this with a generator/iterator someday
    a = [low]
    while a[-1] <= limit:
        a.append(a[len(a)-1]*dilfactor)
    return np.array(a)

def k1mod(params,ligs,noise=None):
    '''
    returns tuple of ligand concentrations, saturation, params

    params is array of binding constants
    ligs is array of ligand concentrations
    noise is relative percent of gaussian noise (e.g., 0.0-1.) applied to data
    '''

    k1 = params[0]
    sat = (k1*ligs)/(1 + k1*ligs)

    if noise is None:
        return (ligs,sat)
    else:
        noised = np.random.normal(sat,noise*sat)
        return (ligs,noised,params)

def k2mod(params,ligs,noise=None):
    '''
    returns tuple of ligand concentrations, saturation, params

    params is array of binding constants
    ligs is array of ligand concentrations
    noise is relative percent of gaussian noise (e.g., 0.0-1.) applied to data
    '''

    k1 = params[0]
    k2 = params[1]
    sat = (1/2)*((k1*ligs + 2*k1*k2*(ligs**2))/(1 + k1*ligs + k1*k2*(ligs**2)))

    if noise is None:
        return (ligs,sat)
    else:
        noised = np.random.normal(sat,noise*sat)
        return (ligs,noised,params)

def k2mod_fix(params,ligs,noise=None):
    '''
    returns tuple of ligand concentrations, saturation, params

    params is array of binding constants
    ligs is array of ligand concentrations
    noise is relative percent of gaussian noise (e.g., 0.0-1.) applied to data
    '''

    k1 = params[0]
    k2 = params[0]/4
    sat = (1/2)*((k1*ligs + 2*k1*k2*(ligs**2))/(1 + k1*ligs + k1*k2*(ligs**2)))

    if noise is None:
        return (ligs,sat)
    else:
        noised = np.random.normal(sat,noise*sat)
        return (ligs,noised,params)

def obj(guess,model,data):
    return model(guess,data[0])[1] - data[1]

def fit(data,model,guess):
        return optimize.least_squares(obj,guess,args=(model,data),ftol=1e-14,gtol=1e-14,xtol=1e-14)

def ftest(fit1,fit2):
    '''
    fit1 is fit from simpler model
    '''

    SS1 = np.sum(np.square(fit1.fun))
    SS2 = np.sum(np.square(fit2.fun))

    DF1 = fit1.fun.shape[0] - fit1.x.shape[0]
    DF2 = fit2.fun.shape[0] - fit2.x.shape[0]

    F = ((SS1-SS2)/(DF1-DF2))/(SS2/DF2)

    p = stats.f.sf(F,(DF1-DF2),DF2)

    return (SS1,SS2,DF1,DF2,F,p)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
import sys
from Bio import SeqIO

"""

Code here adapted from:

https://2-bitbio.com/2018/06/one-hot-encode-dna-sequence-using.html

Code to help read in and parse sequence formats specific to this assignment.

"""

#Fully display all rows in dataframe: optional
#pd.set_option('display.max_rows', None)

#Parse and return a specified sampling of sequences of length 17 from the negative
#fasta file.

def return_negs (path = './data/yeast-upstream-1k-negative.fa', number_to_return = 137, length = 17):
	#set a seed
	random.seed(1)
	#parse fasta file
	seqs = SeqIO.parse(path, "fasta")
	full_negs = []
	#For each sequence, append the raw DNA sequence to a list
	for seq in seqs:
		full_negs.append(seq.seq)
	sampled_negs = []
	#Sample one of these DNA sequences from the full list
	for i in range(number_to_return):
		sequence = str(random.sample(full_negs, 1)[0])
		end = len(sequence)
		#In the range of the sequence length, choose to start somewhere anywhere
		#in the sequence (assumption: it's ALL negative )
		begin = random.randrange(1, end - length)
		to = begin + length
		#Subsequence is begin, to begin + length
		subseq = sequence[begin:to]
		#append sampled subsequence and then return the full list of sampled negs
		sampled_negs.append(subseq)
	return sampled_negs

#return seqs from a text file of DNA sequences
def return_seqs (path = './data/rap1-lieb-positives.txt'):
	with open(path) as f:
		#Split and strip off new lines
		split_file = f.readlines()
		seqs = [i.strip() for i in split_file]
	return seqs

#Return a balanced sample of data. Always will have 137 positive sequences, and
#however many specified negative sequences.
def return_training_data (length = 137):
	test_negs = return_negs()
	test_pos = return_seqs()
	#Label vectors
	neg_df = pd.DataFrame({'seq': test_negs, 'class': ['0'] * length})
	pos_df = pd.DataFrame({'seq': test_pos, 'class': ['1'] * length})
	#concat data and return as dataframe
	full_data = pd.concat ([neg_df, pos_df])
	return full_data

#one-hot encoding logic: given a dictionary, return array of values (vectors of 0/1)
def one_hot_encode (string, dictionary):
	new_list = []
	#Each nucleotide gets a different encoding
	for nucl in string:
		value = dictionary[nucl]
		new_list.append(value)
	#Return an array with shape 17 * 4
	return np.array(new_list)

#unpack an input string by calling above 'one_hot_encode', and flattens list
def unpack (input, dict):
	input = one_hot_encode(input, dict)
	#List flattening
	input = [item for sublist in input for item in sublist]
	return input

#Helper function to plot mostly error vs epochs plots. Weird '1.02' limit is
#so to help see CV accuracies of 1
def plot_utility (keys, values, name, title, ylab, xticks, ylim = (0, 1.02)):
	plt.plot(keys, values, linewidth = 3)
	plt.xticks(rotation=90)
	#Setting xticks so that I don't get a mess on the x-axis of each epoch
	plt.xticks(xticks)
	plt.title(title)
	plt.ylim(ylim)
	plt.ylabel(ylab)
	plt.savefig(name)
	plt.clf()


from django.urls import path
from . import views

urlpatterns = [
    path('user_profile/',views.SetUserProfile,name='UserProfile'),
    path('get_services/',views.get_services,name='get_services'),
    path('me/',views.me,name='me'),
    path('add_handler/',views.addHandler,name='AddHandler'),
    path('remove_handler/',views.removeHandler,name='removeHandler'),
    path('events/<str:id>',views.UserEvents,name='UserEvents'),
    path('add_event/',views.addEvent,name='addEvent'),
    path('remove_event/',views.removeEvent,name='removeEvent'),
]


"""total_number_of_projects_added

Revision ID: c1e908241401
Revises: 879b99e7f212
Create Date: 2023-05-29 11:27:10.843043

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

# revision identifiers, used by Alembic.
revision = "c1e908241401"
down_revision = "879b99e7f212"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("reporting", sa.Column("total_project_count", sa.Integer(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("reporting", "total_project_count")
    # ### end Alembic commands ###