from abc import abstractmethod
from dataclasses import dataclass
from typing import List, Union

import dp_transformers
import numpy as np
import torch
from tqdm import tqdm
from transformers import DataCollatorForLanguageModeling, Trainer, AutoTokenizer, AutoModelForCausalLM, \
    TrainerCallback

from ..arguments.env_args import EnvArgs
from ..arguments.model_args import ModelArgs
from ..arguments.privacy_args import PrivacyArgs
from ..arguments.sampling_args import SamplingArgs
from ..arguments.trainer_args import TrainerArgs
from ..dataset.real_dataset import RealDataset
from ..utils.callbacks import EvaluatePerplexityCallback, PrintSampleCallback
from ..utils.output import print_highlighted
from ..utils.web import is_valid_url, download_and_unzip

@dataclass
class GeneratedText:
    text: str  # the generated text
    score: torch.Tensor  # the score for the text

    def __str__(self):
        return self.text

@dataclass
class GeneratedTextList:
    data: List[GeneratedText]

    def __getitem__(self, item):
        return self.data[item]

    def __str__(self):
        return "\n".join([str(x) for x in self.data])

class LanguageModel:

    def __init__(self, model_args: ModelArgs, env_args: EnvArgs = None):
        """ A wrapper class around a huggingface LM.
        """
        self.model_args = model_args
        self.env_args = env_args if env_args is not None else EnvArgs()

        self._lm = None  # the language model in huggingface
        self._tokenizer = None  # the tokenizer in huggingface
        self._data = {}  # additional data to be saved for the model

    @property
    def ckpt(self):
        return self.model_args.model_ckpt

    @property
    def n_positions(self):
        """ Gets the maximum size of the context """
        return self._lm.config.n_positions

    @abstractmethod
    def tokenizer(self):
        """ Returns this model's tokenizer. """
        raise NotImplementedError

    @abstractmethod
    def get_config(self):
        raise NotImplementedError

    def load(self, verbose: bool = False) -> 'LanguageModel':
        """ Loads the model and tokenizer from the checkpoint.
        """
        model_cls, tokenizer = AutoModelForCausalLM, AutoTokenizer
        self._tokenizer = tokenizer.from_pretrained(self.model_args.architecture,
                                                    use_fast=self.model_args.tokenizer_use_fast)

        if self.model_args.model_ckpt:  # always load the checkpoint if provided.
            if verbose:
                print(
                    f"> Loading the provided {self.model_args.architecture} checkpoint from '{self.model_args.model_ckpt}'.")

            if is_valid_url(self.model_args.model_ckpt):
                self.model_args.model_ckpt = download_and_unzip(self.model_args.model_ckpt)
            self._lm = model_cls.from_pretrained(self.model_args.model_ckpt, return_dict=True).eval()
        elif self.model_args.pre_trained:  # if no checkpoint is provided, load a public, pre-trained model.
            if verbose:
                print(f"> Loading a public, pre-trained {self.model_args.architecture} model.")
            self._lm = model_cls.from_pretrained(self.model_args.architecture, return_dict=True).eval()
        else:  # no checkpoint and no pre-trained model, hence randomly initialize model's parameters.
            if verbose:
                print(f"> Loading an uninitialized {self.model_args.architecture} model.")
            self._lm = model_cls(config=self.get_config())

        self._tokenizer.padding_side = "right"
        self._tokenizer.pad_token = self._tokenizer.eos_token
        self._lm.config.pad_token_id = self._lm.config.eos_token_id
        self._lm.to(self.env_args.device)
        return self

    def substring_perplexity(self, seq: str, substring: str) -> float:
        """ Computes the perplexity of a substring in a string.
        For example: seq="My name is Ronald and I like hamburgers.", substring="Ronald",
        then this function computes the perplexity of generating "Ronald" given prefix "My name is".
        """
        original_mode = self._lm.training
        self._lm.eval()

        txt = seq[:seq.index(substring) + len(substring)]
        input_ids = torch.tensor(self._tokenizer.encode(txt, truncation=True)).unsqueeze(0).to(self.env_args.device)
        substring_len = len(self._tokenizer.encode(substring, truncation=True))
        target_ids = input_ids.clone()
        target_ids[:, :input_ids.size(1) - substring_len] = -100
        with torch.no_grad():
            outputs = self._lm(input_ids, labels=target_ids)
        loss, _, num_tokens = outputs[:3]

        perplexity = torch.exp(loss / num_tokens)

        self._lm.training = original_mode
        return perplexity.cpu().item()

    def autocomplete(self, sampling_args: SamplingArgs):
        """ Predicts the top-1 most probable next tokens. """
        return self.generate(sampling_args)[0]

    def print_sample(self, prompt=None):
        self._lm.eval()
        data = self.generate(SamplingArgs(N=1, prompt=prompt, generate_verbose=False, seq_len=64))
        print_highlighted(data[0].text)
        return data[0].text

    @torch.no_grad()
    def generate(self, sampling_args: SamplingArgs) -> GeneratedTextList:
        """ Generates text using the sampling args.
        """
        self._lm.eval()

        r = min(self.env_args.eval_batch_size, sampling_args.N)

        # Encode the input prompt
        prompts: List[str] = (
            [" "] * r if sampling_args.prompt is None or sampling_args.prompt.strip() == ""
            else [sampling_args.prompt] * r
        )

        inputs = self._tokenizer(prompts, return_tensors="pt", padding=True, truncation=True)
        input_ids = inputs['input_ids'].repeat(r, 1)
        attention_mask = inputs['attention_mask'].repeat(r, 1)

        def generate_batch(input_ids, attention_mask) -> List[GeneratedText]:
            """ Helper function to generate a single batch of text.
            """
            input_len = input_ids.size(1)
            out = self._lm.generate(
                input_ids=input_ids.to(self.env_args.device),
                attention_mask=attention_mask.to(self.env_args.device),
                max_length=min(self.n_positions, input_len + sampling_args.seq_len),
                do_sample=sampling_args.do_sample,
                top_k=sampling_args.top_k,
                top_p=sampling_args.top_p,
                output_scores=True,
                return_dict_in_generate=True
            )

            generated_texts: List[GeneratedText] = []
            for text, score in zip(
                    self._tokenizer.batch_decode(out.sequences, skip_special_tokens=False),
                    [torch.softmax(x, 1) if sampling_args.as_probabilities else x for x in out.scores]
            ):
                generated_texts.append(GeneratedText(text=text, score=score.detach().cpu()))
            return generated_texts

        generated_data: List[GeneratedText] = []
        num_batches = int(np.ceil(sampling_args.N / self.env_args.eval_batch_size))
        for _ in tqdm(
                range(num_batches),
                disable=not sampling_args.generate_verbose,
                desc="Generating with LM"
        ):
            generated_data.extend(generate_batch(input_ids, attention_mask))

        return GeneratedTextList(data=generated_data)

    def tokenize_datasets(self, datasets: List[RealDataset], column_name="text") -> List:
        """ Tokenizes the 'text' column of a list of dataset using this model's tokenizer """
        tokenize_function = lambda x: self._tokenizer(x[column_name], truncation=True)
        return [dataset.get_hf_dataset().map(tokenize_function, batched=True) for dataset in datasets]

    def perplexity(self, data: Union[list, str], offset=0, max_length=0, apply_exp=True, verbose=True,
                   return_as_list: bool = False) -> float:
        """ Compute the perplexity of the model on a string.
        """
        original_mode = self._lm.training
        self._lm.eval()

        if isinstance(data, str):  # always consider lists as input
            data = [data]

        nlls = []  # negative log likelihoods
        ctr = 0  # Number of tokens viewed
        for txt in tqdm(data, desc="Compute PPL", disable=not verbose):
            input_ids = torch.tensor(self._tokenizer.encode(txt, truncation=True)).unsqueeze(0).to(self.env_args.device)
            target_ids = input_ids.clone()

            if offset > 0:  # ignore everything up to the offset
                target_ids[:, :offset] = -100

            tgt_len = (target_ids.size(1) - offset)
            if max_length > 0:  # ignore everything except offset:offset+max_length
                target_ids[:, offset + max_length:] = -100
                tgt_len = max_length

            with torch.no_grad():
                outputs = self._lm(input_ids, labels=target_ids)
            loss, logits = outputs[:2]
            if return_as_list:
                nlls.append(loss.cpu().detach())
            else:
                nlls.append(loss.cpu().detach())
                ctr += tgt_len

        self._lm.training = original_mode
        if return_as_list:
            if apply_exp:
                return torch.exp(torch.stack(nlls))
            return torch.stack(nlls, 0)

        if apply_exp:
            return float(torch.exp(torch.stack(nlls).mean()).item())
        return float(torch.stack(nlls).mean().item())

    def _fine_tune_dp(self,
                      train_dataset: RealDataset,
                      eval_dataset: RealDataset,
                      train_args: TrainerArgs,
                      privacy_args: PrivacyArgs):

        with train_args.main_process_first(desc="Tokenizing datasets"):
            hf_train_dataset, hf_eval_dataset = self.tokenize_datasets([train_dataset, eval_dataset])

        self._lm = self._lm.to(self.env_args.device)
        self._lm.train()

        data_collator = dp_transformers.DataCollatorForPrivateCausalLanguageModeling(self._tokenizer)

        # transfer privacy args
        dpt_privacy_args = dp_transformers.PrivacyArguments(noise_multiplier=privacy_args.noise_multiplier,
                                                            target_epsilon=privacy_args.target_epsilon,
                                                            target_delta=privacy_args.target_delta,
                                                            per_sample_max_grad_norm=privacy_args.max_grad_norm_dp)

        trainer = dp_transformers.dp_utils.OpacusDPTrainer(
            args=train_args,
            model=self._lm,
            train_dataset=hf_train_dataset,
            eval_dataset=hf_eval_dataset,
            data_collator=data_collator,
            privacy_args=dpt_privacy_args,
        )

        try:
            trainer.train()
        finally:
            eps_prv = trainer.get_prv_epsilon()
            eps_rdp = trainer.get_rdp_epsilon()
            trainer.log({
                "final_epsilon_prv": eps_prv,
                "final_epsilon_rdp": eps_rdp
            })

    def fine_tune(self,
                  train_dataset,
                  eval_dataset,
                  train_args: TrainerArgs,
                  privacy_args: PrivacyArgs):
        """ Fine-Tune the LM with/without DP
        """
        if privacy_args.target_epsilon > 0:
            return self._fine_tune_dp(train_dataset, eval_dataset, train_args, privacy_args)
        return self._fine_tune(train_dataset, eval_dataset, train_args)

    def _fine_tune(self,
                   train_dataset,
                   eval_dataset,
                   train_args: TrainerArgs,
                   extra_callbacks: List[TrainerCallback] = None):
        """ Fine-Tune the model and save checkpoints to output directory
        """
        if extra_callbacks is None:
            extra_callbacks = []

        extra_callbacks += [PrintSampleCallback(model=self, sampling_args=SamplingArgs(),
                                                num_steps=train_args.callback_after_n_steps)]
        extra_callbacks += [EvaluatePerplexityCallback(dataset=eval_dataset, model=self, prefix="Eval PPL",
                                                       num_steps=train_args.callback_after_n_steps)]

        data_collator = DataCollatorForLanguageModeling(tokenizer=self._tokenizer, mlm=False)

        print("Tokenizing Train and Eval Datasets ..")
        eval_dataset = eval_dataset.shuffle().select(list(range(train_args.limit_eval_dataset)))
        train_dataset, eval_dataset = self.tokenize_datasets([train_dataset, eval_dataset])
        print("Done Tokenizing!")

        train_args.evaluation_strategy = "no"
        trainer = Trainer(model=self._lm,
                          args=train_args,
                          train_dataset=train_dataset,
                          eval_dataset=eval_dataset,
                          data_collator=data_collator,
                          callbacks=extra_callbacks)

        trainer.train(resume_from_checkpoint=train_args.resume_from_checkpoint)
        trainer.save_model()
        self._lm.eval()


import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
# from model.model import HousePriceModel, TransformerRegressor
from model.model_v3 import HousePriceModel, TransformerRegressor
from model.model_v2 import HousePriceModel_CNN
from dataloader import  HousePriceTestDataset, min_max_denormalize, z_score_denormalize
import platform

def inference():
    # Load Data
    data_path = 'data/final_updated_public_dataset.csv'  # Update with the path of your data file
    data = pd.read_csv(data_path)
    # normalize_columns = {'橫坐標': 'min-max', '縱坐標': 'min-max'}
    normalize_columns = {
    '行政區編號': 'one-hot-encoding', #area_code
    '使用分區':'one-hot-encoding', #building_class
    '主要用途':'one-hot-encoding', #building_usage
    '主要建材':'one-hot-encoding', #building_material
    '建物型態':'one-hot-encoding', #building_style
    '土地面積': 'min-max',
    '移轉層次': 'min-max',
    '總樓層數': 'min-max',
    '屋齡': 'min-max',
    '建物面積': 'min-max',
    '車位面積': 'min-max',
    '車位個數': 'min-max',
    '橫坐標': 'min-max', #z-score
    '縱坐標': 'min-max',
    '主建物面積': 'min-max',
    '陽台面積': 'min-max',
    '附屬建物面積': 'min-max',
    }

    if platform.system() == "Linux":
        gpu = True
    elif platform.system() == "Darwin":
        gpu = False

    dataset = HousePriceTestDataset(data, normalize_columns)
    data_loader = DataLoader(dataset, batch_size=len(dataset), shuffle=False)

    # Load Model
    model_path = 'model_cnn.pth'  # Update with the path of your trained model file
    input_dim = 12 #len(normalize_columns.keys())-1
    model = HousePriceModel(input_dim)
    # model = TransformerRegressor(input_dim, 4, 6)
    # model = HousePriceModel_CNN(input_dim)
    if gpu:
        model = model.cuda()
    else:
        model = model
    model.load_state_dict(torch.load(model_path))
    model.eval()

    # Inference
    with torch.no_grad():
        for batch in data_loader:
            if gpu:
                data = batch['features']
                for i in range(len(data)):
                    data[i] = data[i].cuda()
            else:
                data = batch['features']

            predictions = model(data).cpu().numpy().flatten()

    # Create a DataFrame to hold the IDs and predicted prices
    ids = [f"PU-{i}" for i in range(1, len(predictions) + 1)]  # Adjust ID format as needed
    predicted_prices_df = pd.DataFrame({"ID": ids, "predicted_price": predictions})
    predicted_prices_df = min_max_denormalize(predicted_prices_df, ["predicted_price"],scaler_path="pkl/單價_min_max_normalize_data.pkl")
    # predicted_prices_df = z_score_denormalize(predicted_prices_df, ["predicted_price"],scaler_path="pkl/單價_z_score_normalize_data.pkl")

    # Save to CSV
    output_csv_path = 'predicted_prices.csv'  # Update with the desired output path
    predicted_prices_df.to_csv(output_csv_path, index=False)
    print(f"Predictions saved to {output_csv_path}")

if __name__ == "__main__":
    inference()


from fastapi import FastAPI

app = FastAPI()

fake_items_db = [{
    "item_name": "Foo"
  }, {
    "item_name": "Bar"
  }, {
    "item_name": "Baz"
  }
]

# Query Parameters with default values
@app.get('/items/')
async def read_item(skip: int = 0, limit: int = 10):
  return fake_items_db[skip : limit + skip]

# Optional Query Parameters
from typing import Optional

@app.get('/items/{item_id}')
async def read_item2(item_id: str, q: Optional[str] = None):
  value = {
    "item_id": item_id
  }
  if q:
    value["q"] = q
  return value


from kge.util.io import load_checkpoint
import argparse
import os

import torch
import numpy as np
from scipy.stats import pearsonr

from model.KGEInfluence import KGEInfluence

def dump(obj):
    for attr in dir(obj):
        if hasattr(obj, attr):
            print("obj.%s = %s" % (attr, getattr(obj, attr)))

parser = argparse.ArgumentParser()
parser.add_argument("--dataset",
                    type=str,
                    default="FB15k-237",
                    nargs="?",
                    help="Which dataset to use: FB15k-237 or WN18RR.")
parser.add_argument("--num_iterations",
                    type=int,
                    default=500,
                    nargs="?",
                    help="Number of iterations.")
parser.add_argument("--batch_size",
                    type=int,
                    default=128,
                    nargs="?",
                    help="Batch size.")
parser.add_argument("--nneg",
                    type=int,
                    default=50,
                    nargs="?",
                    help="Number of negative samples.")
parser.add_argument('--avextol',
                    type=float,
                    default=1e-03,
                    help='threshold for optimization in influence function')
parser.add_argument('--damping',
                    type=float,
                    default=1e-01,
                    help='damping term in influence function')
parser.add_argument("--lr",
                    type=float,
                    default=50,
                    nargs="?",
                    help="Learning rate.")
parser.add_argument("--dim",
                    type=int,
                    default=40,
                    nargs="?",
                    help="Embedding dimensionality.")
parser.add_argument("--no-wandb",
                    action='store_true',
                    default=False,
                    help="Log wandb.")
parser.add_argument("--compare", action='store_true', help=".")
parser.add_argument('--disable-cuda', action='store_true', help='Disable CUDA')

args = parser.parse_args()

if not args.disable_cuda and torch.cuda.is_available():
    device = torch.device('cuda')
else:
    device = torch.device('cpu')

# fix seeds
torch.backends.cudnn.deterministic = True
seed = 40
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available:
    torch.cuda.manual_seed_all(seed)

# track experiments with wandb
if args.no_wandb:
    os.environ['WANDB_MODE'] = 'dryrun'

# load pretrained model
checkpoint = load_checkpoint('cp/toy.pt')
# checkpoint = load_checkpoint('cp/toy_distmult.pt')
model = KGEInfluence.create_from(checkpoint, device).to(device)
model.train()
print(model)

# load data and create dataloader
train_triples = model.dataset.load_triples('train')
test_triples = model.dataset.load_triples('test')
# we need indices to get corresponding embeddings gradients
model.test_indices = test_triples

# TODO: args Parameters for dataloader
params = {'batch_size': 32, 'shuffle': False, 'num_workers': 6}
params_test = {'batch_size': 1, 'shuffle': False, 'num_workers': 6}

train_loader = torch.utils.data.DataLoader(train_triples, **params)
single_train_loader = torch.utils.data.DataLoader(train_triples, **params_test)
test_loader = torch.utils.data.DataLoader(test_triples[0:6], **params_test)

# print(model.dataset)

# attrs = vars(model.dataset)
# print(', '.join("%s: %s" % item for item in attrs.items()))

for i in range(model.dataset._triples['train'].shape[0]):
    if i == 0:
        print(model.dataset._triples['train'][1:, :].shape)
    elif i+1 == model.dataset._triples['train'].shape[0]:
        print(model.dataset._triples['train'][:i, :].shape)
        print("LAST")
    else:
        print(
            torch.cat([
                model.dataset._triples['train'][0:i],
                model.dataset._triples['train'][i + 1:]
            ]).shape)

print(model.dataset._triples['train'].shape)

# kge start kge_configs/train_config.yaml -f retrains/test2 --random_seed.default 1 --random_seed.python 2 --random_seed.torch 3 --random_seed.numpy 4 --random_seed.numba 5


from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardButton, InlineKeyboardMarkup
from database import getUserLang

# ************************************* 1 *************************************

russianButton = InlineKeyboardButton(callback_data="Русский", text="🇷🇺 Русский")
kazakhButton = InlineKeyboardButton(callback_data="Қазақша", text="🇰🇿 Қазақша")

choice_lang = InlineKeyboardMarkup(row_width=2, resize_keyboard=True)

choice_lang.add(kazakhButton)
choice_lang.add(russianButton)

# ************************************* 2 *************************************

async def mainKeyboard(userID) -> ReplyKeyboardMarkup:
    userLang = await getUserLang(userID)

    kb = ReplyKeyboardMarkup(
        keyboard=[
            [KeyboardButton(text="📧 Обращение")],
            [KeyboardButton(text="💬 Консультация")],
            [KeyboardButton(text="⚙ Настройки" if userLang == "Русский" else "⚙ Параметрлер")]
        ], resize_keyboard=True
    )

    return kb

# ************************************* 3 *************************************

async def changeKeyboardFunc(userID) -> InlineKeyboardMarkup:
    userLang = await getUserLang(userID)

    kb = InlineKeyboardMarkup(
        inline_keyboard=[
            [InlineKeyboardButton("⌨ Изменить язык" if userLang == "Русский" else "⌨ Тілді өзгерту",
                                  callback_data="changeLang")]
        ]
    )

    return kb

# ************************************* 4 *************************************

async def setAppealKeyboard(userID) -> ReplyKeyboardMarkup:
    userLang = await getUserLang(userID)

    kb = ReplyKeyboardMarkup(
        keyboard=[
            [KeyboardButton(text="👩⚕ Психолог")],
            [KeyboardButton(text="👨💼 ЗДВР" if userLang == "Русский" else "👨💼 ДТЖЖО")],
            [KeyboardButton(text="◀ Назад" if userLang == "Русский" else "◀ Артқа")]
        ], resize_keyboard=True
    )

    return kb

# ************************************* 5 *************************************
async def causeKeyboard(userID) -> ReplyKeyboardMarkup:
    userLang = await getUserLang(userID)

    causeRuArray = ["Поведенческое расстройство", "Буллинг", "Нет желания учиться", "Нет друзей",
                    "Конфликт с одноклассниками", "Снижение мотивации"]

    causeKzArray = ["Мінез-құлықтың бұзылуы", "Қорқыту", "Оқуға деген құштарлықтың болмауы", "Достары жоқ",
                    "Сыныптастармен жанжал", "Мотивацияның төмендеуі"]

    kb = ReplyKeyboardMarkup(resize_keyboard=True)

    causeArray = causeRuArray if userLang == "Русский" else causeKzArray

    # Добавление кнопок в клавиатуру
    for i in causeArray:
        kb.add(KeyboardButton(text=i))

    kb.add("◀ Haзaд" if userLang == "Русский" else "◀ Apтқa")

    return kb

# ************************************* 6 *************************************

async def skipKeyboard(userID):
    userLang = await getUserLang(userID)
    kb = ReplyKeyboardMarkup(resize_keyboard=True)

    kb.add("▶ Пропустить" if userLang == "Русский" else "▶ Өткізіп жіберу")

    return kb

# ************************************* 7 *************************************

async def dayOfTheWeekKeyboard(userID) -> ReplyKeyboardMarkup:
    userLang = await getUserLang(userID)

    dayOfTheWeekArrayRu = ["Понедельник",
                           "Вторник",
                           "Среда",
                           "Четверг",
                           "Пятница",
                           ]

    dayOfTheWeekArrayKz = [
                            "Дүйсенбі",
                            "Сейсенбі",
                            "Сәрсенбі",
                            "Бейсенбі",
                            "Жұма"
                        ]

    kb = ReplyKeyboardMarkup(resize_keyboard=True)

    causeArray = dayOfTheWeekArrayRu if userLang == "Русский" else dayOfTheWeekArrayKz

    for i in causeArray:
        kb.add(KeyboardButton(text=i))

    kb.add(KeyboardButton(text="◀ Назад" if userLang == "Русский" else "◀ Артқа"))

    return kb

async def timeConsultationKeyboard(userID):

    userLang = await getUserLang(userID)

    time = [
        "9:00",
        "9:30",
        "10:00",
        "10:30",
        "11:00",
        "11:30",
        "12:00",
        "12:30",
        "14:30",
        "15:00",
        "15:30",
        "16:00",
        "16:30",
        "17:00",
        "17:30"
    ]

    kb = ReplyKeyboardMarkup(resize_keyboard=True)

    for i in time:
        kb.add(KeyboardButton(text=i))

    kb.add(KeyboardButton(text="◀ Назад" if userLang == "Русский" else "◀ Артқа"))

    return kb


import os
import sys
import optparse,argparse
import subprocess
import json
import ROOT
from collections import OrderedDict

def prepare_shell(shell_file, command, condor, FarmDir):

  cwd       = os.getcwd()
  with open('%s/%s'%(FarmDir,shell_file), 'w') as shell:
    shell.write('#!/bin/bash\n')
    shell.write('WORKDIR=%s\n'%cwd)
    shell.write('cd %s\n'%cmsswBase)
    shell.write('eval `scram r -sh`\n')
    shell.write('cd ${WORKDIR}\n')
    shell.write(command)
  condor.write('cfgFile=%s\n'%shell_file)
  condor.write('queue 1\n')

if __name__=='__main__':

  usage = 'usage: %prog [options]'
  parser = argparse.ArgumentParser(description=usage)
  parser.add_argument('-m', '--method', dest='method', help='[data/slim_mc/slim_data/...]',default='all', type=str)
  parser.add_argument('-e', '--era', dest='era', help='[all/2016apv/2016postapv/2017/2018]',default='all',type=str, choices=["all","2016apv","2016postapv","2017","2018"])
  parser.add_argument('-s', '--sampletype', dest='sampletype', help='[all/normal/interference/highmass]',default='normal',type=str, choices=["all","normal","interference","highmass"])
  parser.add_argument("--test", action="store_true")
  args = parser.parse_args()

  Eras_List = ['2016postapv','2016apv','2017','2018']
  Eras = []
  for Era in Eras_List:
    if args.era == 'all' or args.era == Era:
      Eras.append(Era)

  cwd       = os.getcwd()
  os.system('cp %s/../../python/common.py .'%cwd)

  from common import inputFile_path, cmsswBase
  from common import GetTrainingFile, GetDataFile

  FarmDir   = '%s/Farm_BDT'%cmsswBase
  os.system('mkdir -p %s'%FarmDir)

  condor = open('%s/condor.sub'%FarmDir,'w')
  condor.write('output = %s/job_common.out\n'%FarmDir)
  condor.write('error  = %s/job_common.err\n'%FarmDir)
  condor.write('log    = %s/job_common.log\n'%FarmDir)
  condor.write('executable = %s/$(cfgFile)\n'%FarmDir)
  condor.write('requirements = (OpSysAndVer =?= "CentOS7")\n')
  condor.write('+JobFlavour = "tomorrow"\n')
  condor.write('+MaxRuntime = 7200\n')

  cwd = os.getcwd()

  for Era in Eras:

    template = "%s/../../script/slim.h"%cwd
    era_header = "script/slim_%s.h"%Era
    template_fake = "%s/../../script/slim_fake.h"%cwd
    era_header_fake = "script/slim_fake_%s.h"%Era

    os.system('mkdir -p script')
    os.system('cat %s | sed "s/EraToBeReplaced/%s/g" > %s'%(template,Era,era_header))
    os.system('cat %s | sed "s/EraToBeReplaced/%s/g" > %s'%(template_fake,Era,era_header_fake))
    os.system('mkdir -p sample/%s'%Era)

    path = inputFile_path[Era]
    if (args.method == 'slim_mc' or args.method == 'all') and args.sampletype == 'normal':
      print ("Creating configuration for slim_mc")
      python_file = "%s/slim_mc.py"%cwd

      # Training sample
      Training_list = GetTrainingFile(Era, True)
      print ("=="*50)
      print ("Training_list: ", Training_list)

      for iin in Training_list:
        # if not iin == 'TTTo2L.root': continue #Meng confirmed on 7Sep2022
        command = "python %s --era %s --train %d --iin %s"%(python_file, Era, 1, iin)
        shell_file = 'slim_mc_%s_%s.sh'%(iin, Era)
        prepare_shell(shell_file, command, condor, FarmDir)

      # nonTraining sample
      nonTraining_list = GetTrainingFile(Era, False)
      print ("=="*50)
      print ("nonTraining_list: ", nonTraining_list)

      for iin in nonTraining_list:
        command = "python %s --era %s --train %d --iin %s"%(python_file, Era, 0, iin)
        shell_file = 'slim_mc_%s_%s.sh'%(iin,Era)
        prepare_shell(shell_file, command, condor, FarmDir)

      # Signal sample
      # coups=['rtc01','rtc04','rtc08','rtc10','rtu01','rtu04','rtu08','rtu10']
      coups=['rtc04','rtu04']
      cps=['A']
      masses=['200','300','350','400','500','600','700']

      for cp in cps:
        for coup in coups:
          for mass in masses:
            iin = 'ttc_%s_%s.root'%(cp.lower(),coup)
            flag='GenModel_T%sToTTQ_M%s_%s_TuneCP5_13TeV_G2HDM_%s_madgraphMLM_pythia8'%(cp,cp,mass,coup)
            command = "python %s --era %s --train %d --iin %s --flag %s"%(python_file, Era, 1, iin, flag)
            shell_file = 'slim_mc_%s_%s.sh'%(flag,Era)
            prepare_shell(shell_file, command, condor, FarmDir)

    #====================
    #Highmass samples
    #====================

    if (args.method == 'slim_mc' or args.method == 'all') and args.sampletype == 'highmass':
      print ("Creating configuration for highmass samples using slim_mc")
      python_file = "%s/slim_mc.py"%cwd

      print ("=="*50)

      coups=['rtc04','rtu04']
      cps=['A']
      masses=['800','900','1000']

      for cp in cps:
        for coup in coups:
          for mass in masses:
            iin = 'ttc_%s_%s_%s_highmass.root'%(cp.lower(), mass, coup)
            command = "python %s --era %s --train %d --iin %s "%(python_file, Era, 1, iin)
            shell_file = 'slim_mc_%s_%s.sh'%(iin,Era)
            print ("shell_file: ", shell_file)
            prepare_shell(shell_file, command, condor, FarmDir)

    #====================
    #Interference samples
    #====================
    if (args.method == 'slim_mc' or args.method == 'all') and args.sampletype == 'interference' :
      print ("=="*50)
      print ("Creating configuration for interference samples using slim_mc")
      python_file = "%s/slim_mc.py"%cwd

      print ("=="*50)

      coups=['rtc04','rtu04']

      A_masses = ['250','300','350','400','550','700','800','900','1000']
      S_masses = ['200','250','300','350','500','650','750','850','950']

      for coup in coups:
          for index, mass in enumerate(A_masses):
            iin = 'ttc_a_%s_s_%s_%s.root'%(A_masses[index], S_masses[index], coup)
            command = "python %s --era %s --train %d --iin %s "%(python_file, Era, 1, iin)
            shell_file = 'slim_%s_%s.sh'%(iin,Era)
            prepare_shell(shell_file, command, condor, FarmDir)

    #====================
    # Data run
    #====================
    if (args.method == 'slim_data' or args.method == 'all') and args.sampletype == 'normal':
      print ("Creating configuration for slim_data")

      python_file = "%s/slim_data.py"%cwd

      for channel in ['DoubleElectron','DoubleMuon','ElectronMuon']:
        FileList = GetDataFile(Era,channel)
        print ("=="*50)
        print ("FileList: ", FileList)

        for iin in FileList:
          shell_file = 'slim_data_%s_%s_%s.sh'%(iin,Era,channel)
          command = "python %s --era %s --channel %s --iin %s"%(python_file, Era, channel, iin)
          prepare_shell(shell_file, command, condor, FarmDir)

    if (args.method == 'slim_fakelep_mc' or args.method == 'all') and args.sampletype == 'normal':
      print ("Creating configuration for slim_falelep_mc")

      python_file = "%s/slim_fakelep_mc.py"%cwd

      for channel in ['DoubleElectron','DoubleMuon','ElectronMuon']:

        Training_list = GetTrainingFile(Era, True)
        print ("=="*50)
        print ("Training_list: ", Training_list)

        for iin in Training_list:
          if "TTTo1L" in  iin: continue
          command = "python %s --era %s --train %d --iin %s --channel %s"%(python_file, Era, 1, iin, channel)
          shell_file = 'slim_fakelep_mc_%s_%s_%s.sh'%(iin, Era, channel)
          prepare_shell(shell_file, command, condor, FarmDir)

        nonTraining_list = GetTrainingFile(Era, False)
        print ("=="*50)
        print ("nonTraining_list: ", nonTraining_list)

        for iin in nonTraining_list:
          command = "python %s --era %s --train %d --iin %s --channel %s"%(python_file, Era, 0, iin, channel)
          shell_file = 'slim_fakelep_mc_%s_%s_%s.sh'%(iin,Era,channel)
          prepare_shell(shell_file, command, condor, FarmDir)

    if (args.method == 'slim_fakelep_data' or args.method=='all') and args.sampletype == 'normal':
      print ("Creating configuration for slim_fakelep_data")

      python_file = "%s/slim_fakelep_data.py"%cwd
      for channel in ['DoubleElectron','DoubleMuon','ElectronMuon']:
        DataList = GetDataFile(Era, channel)
        print ("=="*50)
        print ("DataList: ", DataList)

        for iin in DataList:
          command  = "python %s --era %s --train %d --iin %s --channel %s"%(python_file, Era, 0, iin, channel)
          shell_file = 'slim_fakelep_data_%s_%s_%s.sh'%(iin, Era, channel)
          prepare_shell(shell_file, command, condor, FarmDir)

  condor.close()
  if not args.test:
    print ("Submitting Jobs on Condor")
    os.system('condor_submit %s/condor.sub'%FarmDir)


import numpy as np
import sympy
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from pylab import figure, axes, pie, title, show

print('Graphik1')
print('Steigung','Y-Achsenabschnitt')
x, y = np.loadtxt('tab2.txt', unpack=True,delimiter=',')
T=np.sqrt(y)

plt.plot(x, y, "kx", label="Messwerte")
plt.errorbar(x, y, yerr=T, fmt="none", capsize=5, capthick=2, ms=9, markerfacecolor="red")

c, v = np.loadtxt('tab2.txt', unpack=True,delimiter=',')
def f(c,a,b):
    return a*c+b
popt, pcov = curve_fit(f, c, v)
print(popt)
print(np.diag(pcov))
print(np.sqrt(np.diag(pcov)))
c_new = np.linspace(x[1], x[-1], 500)

plt.figure(1)
#plt.plot(x,y,'x')
plt.plot(c_new,f(c_new,*popt),'-', label='Lineare Regression')
plt.xlabel('Spannung U / V')
plt.ylabel('N/t $/$ 1/min')
plt.grid()
plt.legend()

plt.savefig('Graphik1.pdf')
print ('Fertig')


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

import pygame
import os
from Entity import Entity

class Player(Entity):
    def __init__(self):
        super(Entity, self).__init__()
        self.xvel = 0
        self.yvel = 0
        self.onGround = False
        self.image = pygame.image.load(os.path.join('', 'player.png'))
        self.rect = pygame.Rect(128, 128, 32, 32)

    def update(self, up, down, left, right, running, platform):
        if up:
            self.yvel -= 7
        if down:
            self.yvel = 7
        if running:
            self.xvel = 12
        if left:
            self.xvel = -8
        if right:
            self.xvel = 8

        if not (up or down):
            self.yvel = 0
        if not (left or right):
            self.xvel = 0

        self.rect.left += self.xvel

        self.collide(self.xvel, 0, platform)

        self.rect.top += self.yvel

        self.onGround = False

        self.collide(0, self.yvel, platform)

    def collide(self, xvel, yvel, platform):
        for p in platform:
            if pygame.sprite.collide_rect(self, p):
                if xvel > 0:
                    self.rect.right = p.rect.left
                    print("collide right")
                if xvel < 0:
                    self.rect.left = p.rect.right
                    print("collide left")
                if yvel > 0:
                    self.rect.bottom = p.rect.top
                    # self.onGround = True
                    self.yvel = 0
                if yvel < 0:
                    self.rect.top = p.rect.bottom
                    self.yvel = 0


# psutil wird zum Auslesen der CPU-Last unter Windows verwendet (Muss jedoch installiert werden).
# Unter pycharm (File > Settings > Project: pythonProject >Python interpreter > +psutil).
# Unter Python direkt oder anderen IDEs (CMD> "py -m pip install psutil").
import psutil
import os

# Die Zeit gibt, in Sekunden, an wie lange das Interval zur Messung der CPU-Last ist (Minimum=0.1)
zeit = 1

# Gibt die Anzahl der Durchläufe des Programms an (Minimum=1 für einen Durchlauf)
loop = 60

# Variablen zur bestimmung der CPU Belastungsgrenzen (Minimum und Maximum der moderaten/gelben Grenze) maxgelb>mingelb!
mingelb = 50
maxgelb = 85

def cpulast():
    # Kommando aus der PSutil Bibliothek um die Auslastung der CPU in einem Intervall anzugeben.
    cpulast = psutil.cpu_percent(interval=zeit)
    # Kommando aus os um die Konsole zu leeren (funktioniert nicht in der IDE-Konsole)
    os.system('cls')
    print(cpulast, "% CPU-Auslastung")
    return cpulast

def belastung(cpulast):
    # Wenn die CPU-Last unter der gelben Mindestgrenze somit unter gelb/moderat
    if cpulast < mingelb:
        print("Die CPU-Auslastung liegt unter", mingelb, "%, alles im grünen Bereich!")
    # Wenn die CPU-Last unter der gelben Maximum-Grenze somit innerhalb von gelb/moderat
    elif cpulast < maxgelb:
        print("Die CPU-Auslastung liegt zwischen ", mingelb, "%  und ", maxgelb, "%. moderate Auslastung!")
    else:
        print("Über ", maxgelb, "% CPU-Auslastung, starke Auslastung!")

# Visualisierung der CPU-Last mit 100 Strichen welche Parallel zur oberen Auswertung ablaufen
def visualize(cpulast):
    # Neue Variable um diese von float auf int zu setzen und +0,5 um mathematisch korrekt zu runden
    cpulastint = cpulast + 0.5
    cpulastint = int(cpulastint)
    # visuello ist zur Visualisierung der nicht genutzte CPU-Ressourcen
    # visuell1,2,3 für die Stärken der Auslastungen um diese farblich zu markieren 1=grün 2=gelb 3=rot
    # '\033[32m' stehen für Farbcodes um die Visualisierung farblich darzustellen '\x1b[0m' zum reset der farben.
    visuello = 100 - cpulastint
    visuello = visuello * "-"
    if cpulastint < mingelb:
        visuell1 = cpulastint * "|"
        print("[" + '\033[92m' + visuell1 + '\x1b[0m' + visuello + "]")
    elif cpulastint < maxgelb:
        visuell1 = mingelb * "|"
        calc = cpulastint - mingelb
        visuell2 = calc * "|"
        print("[" + '\033[92m' + visuell1 + '\033[33m' + visuell2 + '\x1b[0m' + visuello + "]")
    else:
        visuell1 = mingelb * "|"
        gelb = maxgelb-mingelb
        visuell2 = gelb * "|"
        calc = cpulastint - maxgelb
        visuell3 = calc * "|"
        print("[" + '\033[92m' + visuell1 + '\033[33m' + visuell2 + '\033[91m' + visuell3 + '\x1b[0m' + visuello + "]")

# Loop welche alle funktionen chronologisch durchgeht und diese anspricht
while loop > 0:
    loop -= 1
    # Zieht, die Variable last aus der Funktion cpulast heraus
    last = cpulast()
    # Beide funktionen nehmen die Variable Last hinein
    belastung(last)
    visualize(last)


"""Memory replay utilities."""

import random
from dataclasses import dataclass, astuple

import tensorflow as tf

from typing import Any, Iterable, Optional

@dataclass
class Transition:
    state: tf.Tensor
    action: int
    reward: float
    is_terminal: bool
    next_state: Optional[tf.Tensor]

@dataclass
class Batch:
    states: tf.Tensor
    actions: tf.Tensor
    rewards: tf.Tensor
    is_terminal: tf.Tensor
    next_states: tf.Tensor

class ReplayMemory:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self._items = []
        self._idx = 0

    def store(self, transition: Transition) -> None:

        item = astuple(transition)

        if len(self) < self.capacity:
            self._items.append(item)
        else:
            self._items[self._idx] = item

        self._idx = (self._idx + 1) % self.capacity

    def sample(self, batch_size: int) -> Batch:
        """Sample a batch of transitions, uniformly at random"""

        assert len(self._items) >= batch_size, (
            f'Cannot sample batch of size {batch_size} from buffer with'
            f' {len(self)} elements.'
        )

        # Sample a new batch without replacement
        sampled = random.sample(self._items, batch_size)

        # Transpose List[Transition] -> List[State], List[Action], etc.
        states, actions, rewards, is_terminal, next_states = zip(*sampled)

        batch = Batch(states, actions, rewards, is_terminal,
            [s for s in next_states if s is not None]
        )

        batch = Batch(
            states = tf.stack(states),
            actions = tf.convert_to_tensor(actions),
            rewards = tf.convert_to_tensor(rewards),
            is_terminal = tf.convert_to_tensor(is_terminal),
            next_states = tf.stack([s for s in next_states if s is not None])
        )

        return batch

    def __len__(self) -> int:
        return len(self._items)

    def __iter__(self) -> Iterable[Any]:
        return iter(self._items)

    def __str__(self) -> str:
        return str(self._items)

class Sampler:

    def __init__(self, policy, env) -> None:
        self._policy = policy
        self._env = env

        self._state = self._env.reset()

    def step(self) -> Transition:
        """Samples the next transition"""

        action = self._policy(self._state)
        next_state, reward, done, _ = self._env.step(action)

        transition = Transition(
            state=self._state,
            action=action,
            reward=reward,
            is_terminal=done,
            next_state=next_state if not done else None
        )

        self._state = self._env.reset() if done else next_state
        return transition


# This script takes the following GET params:
#   * pid: A pcap ID
#   * host: The hostname of the device to get the pcap from
#   * stime: The search_time window, formatted as YYYY/MM/DD HH:MM:SS
#   * serial: Optional.  Serial number of the firewall if host is panorama

import os
import sys

libpath = os.path.dirname(os.path.abspath(__file__))
sys.path[:0] = [os.path.join(libpath, 'lib')]
sys.path[:0] = [os.path.join(libpath, 'lib', 'pan-python', 'lib')]
sys.path[:0] = [os.path.join(libpath, 'lib', 'pandevice')]

import time
import common
import json
import splunk
import splunk.mining.dcutils as dcu

import pandevice.base
import pandevice.panorama
import pandevice.firewall

logger = dcu.getLogger()

class Cfg(object):
    """Config is a separate object for testing purposes."""
    def __init__(self):
        self.skey = None
        self.hostname = None
        self.search_time = ''
        self.serial = None
        self.pcap_id = 0
        self.api_key = None

    def parse(self, skey, req):
        # Initialize all config.
        self.skey = skey

        #sc = common.SplunkConnector(self.skey, logger)
        self.hostname = req['query']['host']
        self.search_time = req['query']['stime']
        self.serial = req['query'].get('serial', None)
        self.pcap_id = req['query']['pid']
        if self.pcap_id <= 0:
            raise ValueError('{0} is not a valid pcap ID'.format(self.pcap_id))

        self.api_key = common.apikey(self.skey, self.hostname)

class MasterP(splunk.rest.BaseRestHandler):
    def handle_GET(self):
        # Get and process query params.
        logger.info('Initiated pcap retrieval, parsing request config')
        cfg = Cfg()
        cfg.parse(self.sessionKey, self.request)

        # Connect to the firewall / panorama.
        logger.info('Creating connection object for given hostname')
        o = pandevice.base.PanDevice.create_from_device(
            cfg.hostname, api_key=cfg.api_key)

        # Set the serial target (if present and this is a panorama).
        if isinstance(o, pandevice.panorama.Panorama):
            if not cfg.serial:
                raise ValueError('Host is panorama, serial number is required')
            logger.info('This is a panorama device, creating sub-object')
            dev = pandevice.firewall.Firewall(serial=cfg.serial)
            o.add(dev)
        else:
            dev = o

        # Retrieve the pcap file.
        logger.info('Retrieving pcap file')
        dev.xapi.export(
            'threat-pcap', search_time=cfg.search_time, pcapid=cfg.pcap_id)
        if not dev.xapi.export_result or not dev.xapi.export_result['content']:
            raise ValueError('File not present or file is empty')

        # Send pcap to the user.
        self.response.setHeader('Content-Type', 'application/vnd.tcpdump.pcap')
        self.response.write(dev.xapi.export_result['content'])
        logger.info('Done')


from fastapi import FastAPI
# from starlette.requests import Request
# from starlette.responses import Response

from app.auth.router import router as auth_router
from app.forum.router import router as forum_router
from app.profile.router import router as profile_router
from app.subjects.router import router as subject_router
# from app.database import SessionLocal

tags_metadata = [
    {
        "name": "forum",
        "description": "MetaBSUIR forum",
    },
    {
        "name": "authentication",
        "description": "MetaBSUIR authentication",
    },
    {
        "name": "profile",
        "description": "Members profile",
    },
    {
        "name": "subjects",
        "description": "Subjects profile",
    },
]

def get_application() -> FastAPI:
    application = FastAPI(
        title="MetaBSUIR",
        description="BSUIR meta-universe",
        version="0.1.0",
        openapi_tags=tags_metadata,
    )
    application.include_router(auth_router)
    application.include_router(forum_router)
    application.include_router(profile_router)
    application.include_router(subject_router)
    return application

app = get_application()


#!/usr/bin/env python

import json

if __name__== "__main__":
   import argparse

   parser = argparse.ArgumentParser(description='Arguments.')

   parser.add_argument('-f','-file', dest='json_file',action='store',default='whitelist.json',
                       help='JSON file')

   args = parser.parse_args()

   with open(args.json_file, "r") as rf:
      val = json.load(rf)

   with open(args.json_file, "w") as wf:
      json.dump(val,wf,indent=4)


import jieba.posseg as pseg
import codecs
from gensim import corpora
from gensim.summarization import bm25
import os
import re
from util import jsonutil, configutil, jiebautil
from util.linereader import LineReader
import json

DICTIONARY_PATH = configutil.config["sim"]["dic_path"]

def buildModel(jsonFile, fieldNames, query_str):
    # iterable 不能循环两次，所以创建两个变量
    t1 = jsonutil.iterCutFieldList(jsonFile, fieldNames)
    t2 = jsonutil.iterCutFieldList(jsonFile, fieldNames)

    # 建立单词索引字典
    dictionary = corpora.Dictionary(t1)
    dictionary.save(DICTIONARY_PATH)

    # 建立词袋模型.将词汇表示的文本，转换成用id表示
    corpus = [dictionary.doc2bow(text) for text in t2]
    print("词袋: %i " % len(corpus))

    bm25Model = bm25.BM25(corpus)

    # print("bm25 idf lens: %i " %len(bm25Model.f))

    average_idf = sum(map(lambda k: float(bm25Model.idf[k]), bm25Model.idf.keys())) / len(bm25Model.idf.keys())

    query = jiebautil.cutWords(query_str).split()
    query_bow = dictionary.doc2bow(query)

    scores = bm25Model.get_scores(query_bow, average_idf)
    # i = scores.index(max(scores))

    lineRead = LineReader(jsonFile)
    for i in range(5):
        score = max(scores)
        lineNum = scores.index(score) + 1
        s = lineRead.load(lineNum)
        j = json.loads(s)
        print(jsonutil.recursive_get(j, fieldNames[0]))

        del scores[lineNum-1]


import streamlit as st
import pandas as pd
import numpy as np
from xgboost import XGBRegressor

st.set_page_config(page_title="AirBnB Price Predictor in New York City", page_icon=":house:", layout="wide")

st.markdown("
Predicting Airbnb Prices in New York City
", unsafe_allow_html=True)

st.sidebar.header("Enter your Airbnb Details")

def user_input():
    txt1 = st.sidebar.subheader("Enter location")
    latitude = st.sidebar.number_input('Latitude', 40.499790, 40.913060, 40.755942, step=1e-7, format="%.6f")
    longitude = st.sidebar.number_input('Longitude', -74.244420	, -73.712990, -73.977619, step=1e-7, format="%.6f")
    availability_365 = st.sidebar.number_input('Number of days available per year', 1, 365, 24)
    minimum_nights = st.sidebar.number_input('What is the minimum number of days a visitor must reserve for your listing ?', 1, 12, 2)
    host_activity = st.sidebar.number_input('How many active listings do you have ?', 0, 629, 4)
    reviews_per_month = st.sidebar.number_input('How many reviews per month do your listing receive ?', 0, 58, 5)
    number_of_reviews = st.sidebar.number_input('In total, how many reviews does your listing have? ', 0, 629, 4)
    time_since_last_review = st.sidebar.number_input('When was your last reviews (in days) ?', 0, 4000, 50)
    #neighbourhood_popularity = st.sidebar.selectbox('Neighbourhood popularity', 0.00, 10.00, 1.43)
    txt2 = st.sidebar.subheader("Choose 1 if your listing is located in one of these boroughs (leave 0 for others)")
    dummy_Bronx = st.sidebar.selectbox('Bronx', (0, 1))
    dummy_Brooklyn = st.sidebar.selectbox('Brooklyn', (0, 1))
    dummy_Manhattan = st.sidebar.selectbox('Manhattan', (0, 1))
    dummy_Queens = st.sidebar.selectbox('Queens', (0, 1))
    dummy_Staten_Island = st.sidebar.selectbox('Staten Island', (0, 1))
    txt3 = st.sidebar.subheader("Choose 1 if your listing is one of the following (leave 0 for others) : ")
    dummy_Entire_home_apt = st.sidebar.selectbox('Entire Home / Appartment', (0, 1))
    dummy_Private_room = st.sidebar.selectbox('Private Room', (0, 1))
    dummy_Shared_room = st.sidebar.selectbox('Share Room', (0, 1))
    txt4 = st.sidebar.subheader("How far (in kilometers) is your listing from : ?")
    distance_to_wall_street = st.sidebar.number_input('Wall Street', 0, 31, 23)
    distance_to_closest_station = st.sidebar.number_input('Closest Metro Station', 0, 31, 23)

    data = {'latitude':latitude,
            'longitude':longitude,
            'availability_365': availability_365,
            'minimum_nights': minimum_nights,
            'host_activity':  host_activity,
            'reviews_per_month': reviews_per_month,
            'number_of_reviews': number_of_reviews,
            'time_since_last_review': time_since_last_review,
            #'neighbourhood_popularity': neighbourhood_popularity,
            'dummy_Bronx': dummy_Bronx,
            'dummy_Brooklyn': dummy_Brooklyn,
            'dummy_Manhattan': dummy_Manhattan,
            'dummy_Queens': dummy_Queens,
            'dummy_Staten_Island': dummy_Staten_Island,
            'dummy_Entire_home_apt':  dummy_Entire_home_apt,
            'dummy_Private_room': dummy_Private_room,
            'dummy_Shared_room': dummy_Shared_room,
            'distance_to_wall_street': distance_to_wall_street,
            'distance_to_closest_station': distance_to_closest_station
    }
    appt_parametre = pd.DataFrame(data,index=[0])
    return appt_parametre

df = user_input()

model = XGBRegressor()
model.load_model("/Users/Koko/Desktop/xg_boost6.json")
y_pred = model.predict(df)

price = round(np.exp(y_pred[0]))

st.caption("
Price Per Night
", unsafe_allow_html=True)

m2 = st.metric("", "$"+f"{price:,}")
css='''
[data-testid="metric-container"] {
    width: fit-content;
    margin: auto;
}

[data-testid=m2] > div {
    width: fit-content;
    margin: auto;
}

[data-testid="metric-container"] label {
    width: fit-content;
    margin: auto;
}
'''

st.markdown(f'',unsafe_allow_html=True)

st.subheader("Property Location")
st.map(df)


import argparse
import pandas as pd
import os
from happytransformer import HappyTextToText
import progressbar
from multiprocessing import freeze_support
from params import testing_args

def parseArguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('process', help='evaluate a csv file or generate corrected sentences, eval/gen', default='generate')
    parser.add_argument('model_path', help='path to the model', type=str)
    parser.add_argument('-s', '--sentence', help='if you want to predict only one sentence, default=None', type=str, default=None)
    parser.add_argument('-f', '--file', help='if you want to multiple sentences in a file, default=None', type=str, default=None)
    args = parser.parse_args()
    return args

def prepare_data(df):
    df.columns = ['input', 'target']
    df['input'] = 'grammar: ' + df['input']
    return df

if __name__ == '__main__':
    freeze_support()
    args = parseArguments()

    model = HappyTextToText('t5', args.model_path)

    if args.process == 'eval':
        if args.file:
                testing_data = pd.read_csv(args.file)
                processed_data = prepare_data(testing_data)
                test_file = os.path.join(os.path.dirname(args.file),'prepared_'+os.path.basename(args.file))
                processed_data.to_csv(test_file, index=False)

                result = model.eval(test_file)
                print(result)
        else:
            print('CSV File is neccessary for the evaluation...')
            exit()

    if args.process == 'gen':
        if args.sentence:
            generated_text = model.generate_text('grammar: '+args.sentence, args=testing_args)
            print('\nCorrected sentence: '+generated_text.text)

        if args.file:
            read_file = open(args.file, 'r', encoding='utf8')
            write_filename = os.path.join(os.path.dirname(args.file), 'corrected_'+os.path.basename(args.file))
            write_file = open(write_filename, 'w', encoding='utf8')

            lines = read_file.readlines()
            text_list = []

            print('\nGenerating correction file...')
            bar = progressbar.ProgressBar(maxval=len(lines), widgets=[progressbar.Bar(), ' ', progressbar.SimpleProgress()])
            bar.start()
            sentence_count = 0
            for line in lines:
                line = 'grammar: ' + line.strip()
                output = model.generate_text(line, args=testing_args)
                text_list.append(line[9:] + ' --> ' +output.text.strip()+'\n')
                sentence_count += 1
                bar.update(sentence_count)
            write_file.writelines(text_list)
            bar.finish()

            read_file.close()
            write_file.close()
            print('File stored at '+ write_filename)

        if not args.sentence and not args.file:
            print('Either a sentence or a file is necessary...')
            exit()


from abc import ABC, abstractmethod
from typing import Iterable, List

import numpy as np
import pandas as pd
import scipy.stats as sps

import ambrosia.tools._lib._bin_ci_aide as helper_dir
import ambrosia.tools.pvalue_tools as pvalue_pkg
from ambrosia import types

from . import EFFECT_COL_NAME, FIRST_TYPE_ERROR_COL_NAME, GROUP_SIZE_COL_NAME, STAT_ERRORS_COL_NAME

RELATIVE_ABSOLUTE_DELTA_ERROR = ValueError("Choose relative or absolute delta, not both")
ROUND_DIGITS: int = 3
ROUND_DIGITS_TABLE: int = 3
ROUND_DIGITS_PERCENT: int = 1

class BinomTwoSampleCI(ABC):
    """
    Implementation of two-sample confidence interval.
    Supported the following types  interval_type:

    X ~ Bin(m, p1)
    Y ~ Bin(n, p2)

    Estimated Delta = p1 - p2
    For more information explore:
    http://stat.wharton.upenn.edu/~lbrown/Papers/2005c%20Confidence%20intervals%20for%
    20the%20two%20sample%20binomial%20distribution%20problem.pdf

    Methods
    -------

        Frequency:
        ----------

        Wald's CI - Using MLE estimation for proportions and
                asymptotic normality

        Yule's CI - Using estimation for Var(p1 - p2), in assumption
                (p1 = p2) and (m ~= n). Could be use for criterion
                H0: p1 = p2   vs   H1: p1 != p2
        Yule's modified CI - analogue for previous one for m != n
        Newcombe's CI - combination of CI for p1 and p2, building CI for difference
        Recentred CI - Using t quantiles

        Bayes:
        ------

        Jeffrey's CI - Using for prior distribution Beta(1/2, 1/2)
        Agresti's CI - Using for prior distribution Beta(1, 1)
        Conjugate Beta - Using for prior Beta(n_success, n_failure)

    """

    __PRECISION: float = 0.0001

    # This class just a container with all confidence intervals.
    # Thats why there is no instantiation.
    @abstractmethod
    def __init__(self):
        pass

    @staticmethod
    def __wald_ci(
        sample_a: int, sample_b: int, a_trials: int, b_trials: int, significance_level: float
    ) -> types.IntervalType:
        """
        Implementation of Wald confidence interval.
        """
        p_a_est = sample_a / a_trials
        p_b_est = sample_b / b_trials
        q_a_est = 1 - p_a_est
        q_b_est = 1 - p_b_est
        delta_estimation = p_b_est - p_a_est

        quantile = sps.norm.ppf(significance_level / 2)
        variation = p_a_est * q_a_est / a_trials + p_b_est * q_b_est / b_trials
        shift = -quantile * np.sqrt(variation)

        return delta_estimation - shift, delta_estimation + shift

    @staticmethod
    def __yule_ci(
        sample_a: int, sample_b: int, a_trials: int, b_trials: int, significance_level: float, modified: bool = False
    ) -> types.IntervalType:
        """
        Implementation of Yule's confidence intervals.
        """

        p_a_est = sample_a / a_trials
        p_b_est = sample_b / b_trials
        delta_estimation = p_b_est - p_a_est

        quantile = sps.norm.ppf(significance_level / 2)

        if modified:
            coef_a = b_trials / a_trials
            coef_b = a_trials / b_trials
            p_hat = (sample_a * coef_a + sample_b * coef_b) / (a_trials + b_trials)
            q_hat = 1 - p_hat
            variation = (1 / a_trials + 1 / b_trials) * p_hat * q_hat
        else:
            p_overline = (sample_a + sample_b) / (a_trials + b_trials)
            q_overline = 1 - p_overline
            variation = (1 / a_trials + 1 / b_trials) * p_overline * q_overline

        shift = -quantile * np.sqrt(variation)
        return delta_estimation - shift, delta_estimation + shift

    @staticmethod
    def __bayes_conjugate_beta(
        sample_a: int,
        sample_b: int,
        a_trials: int,
        b_trials: int,
        n_success: int,
        n_failure: int,
        significance_level: float,
    ) -> types.IntervalType:
        """
        Implementation of bayes confidence intervals with cojugate distribution
        Beta(n_success, n_falure).
        """

        p_tilde_a = (sample_a + n_success) / (n_success + n_failure + a_trials)
        p_tilde_b = (sample_b + n_success) / (n_success + n_failure + b_trials)

        q_tilde_a = 1 - p_tilde_a
        q_tilde_b = 1 - p_tilde_b
        delta_tilde = p_tilde_b - p_tilde_a

        quantile = sps.norm.ppf(significance_level / 2)
        variation = p_tilde_a * q_tilde_a / a_trials + p_tilde_b * q_tilde_b / b_trials
        shift = -quantile * np.sqrt(variation)
        return delta_tilde - shift, delta_tilde + shift

    @staticmethod
    def __square_eq_newcombe(p_est: float, m: int, quantile: float) -> types.IntervalType:  # pylint: disable=C0103
        """
        Helper function for newcombe_ci.
        """

        coef_a = 1 + quantile**2 / m
        coef_b = -(2 * p_est + quantile**2 / m)
        coef_c = p_est**2
        d = coef_b**2 - 4 * coef_a * coef_c  # pylint: disable=C0103
        left = (-coef_b - np.sqrt(d)) / (2 * coef_a)
        right = (-coef_b + np.sqrt(d)) / (2 * coef_a)

        return left, right

    @staticmethod
    def __newcombe_ci(
        sample_a: int,
        sample_b: int,
        a_trials: int,
        b_trials: int,
        significance_level: float = 0.05,
    ) -> types.IntervalType:
        """
        Implementation of Newcombe's confidence intervals.
        """
        p_a_est = sample_a / a_trials
        p_b_est = sample_b / b_trials
        delta_est = p_b_est - p_a_est

        quantile = sps.norm.ppf(significance_level / 2)

        # Calculate intevalls for A and B group
        left_bound_group_a, right_bound_group_a = BinomTwoSampleCI.__square_eq_newcombe(p_a_est, a_trials, quantile)
        left_bound_group_b, right_bound_group_b = BinomTwoSampleCI.__square_eq_newcombe(p_b_est, b_trials, quantile)

        variance_a_left = left_bound_group_a * (1 - left_bound_group_a) / a_trials
        variance_a_right = right_bound_group_a * (1 - right_bound_group_a) / a_trials
        variance_b_left = left_bound_group_b * (1 - left_bound_group_b) / b_trials
        variance_b_right = right_bound_group_b * (1 - right_bound_group_b) / b_trials
        variance_left = variance_a_left + variance_b_right
        variance_right = variance_b_left + variance_a_right
        left_bound = delta_est + quantile * np.sqrt(variance_left)
        right_bound = delta_est - quantile * np.sqrt(variance_right)

        return left_bound, right_bound

    @staticmethod
    def __recentered_ci(
        sample_a: int, sample_b: int, a_trials: int, b_trials: int, significance_level: float
    ) -> types.IntervalType:
        """
        Implementation of recentered confidence intervals.
        """
        p_a_est = sample_a / a_trials
        p_b_est = sample_b / b_trials
        delta_estimation = p_b_est - p_a_est
        p_est = (b_trials * p_a_est + a_trials * p_b_est) / (a_trials + b_trials)
        q_est = 1 - p_est
        quantile = -sps.t.ppf(significance_level / 2, df=a_trials + b_trials - 2)
        coef = 1 + quantile**2 / (a_trials + b_trials)
        center = delta_estimation / coef
        var_est = (1 / a_trials + 1 / b_trials) * p_est * q_est
        shift = quantile * np.sqrt(coef * var_est - delta_estimation**2 / (a_trials + b_trials)) / coef
        return center - shift, center + shift

    @staticmethod
    def calculate_pvalue(
        a_success: int,
        b_success: int,
        a_trials: int,
        b_trials: int,
        interval_type: str = "wald",
        alternative: str = "two-sided",
        n_success_conjugate: int = None,
        n_failure_conjugate: int = None,
    ) -> float:
        """
        Calculate pvalue for confidence interval.
        pvalue(x) = inf_a {a | x \\in S_a }

        Parameters
        ----------
        a_success : int
            Samples from Bin(a_trials, p_a)
        b_success : int
            Sample from Bin(b_trials, p_b)
        a_trials : int
            Parameter for trials amount for group A
        b_trials : int
            Parameter for trials amount for group B
        confidence_level : float
            Pr ( [p1 - p2] in CI ) -> confidence_level, n -> infty
        interval_type : str, default : ``"wald"``
            One from [wald, yule, yule_modif, newcombe, jeffrey, agresti, bayes_beta, recenter]
        alternative : str, default : ``"two-sided"``
            Alternative for static criteria - two-sided, less, greater
            Less means, that mean in first group less, than mean in second group
        n_success : int
            Arguments for conjugate distribution Beta(n_success, n_failure) if interval_type = "bayes_beta"
        n_failure : int
            Arguments for conjugate distribution Beta(n_success, n_failure) if interval_type = "bayes_beta"

        Comment
        -------
        You can pass numpy arrays with same shapes for a_success, b_success and e.t.c

        Returns
        -------
        pvalue : float
            P-value of the interval-induced criterion
        """

        return pvalue_pkg.calculate_pvalue_by_interval(
            BinomTwoSampleCI.confidence_interval,
            0,
            a_success=a_success,
            a_trials=a_trials,
            b_success=b_success,
            b_trials=b_trials,
            n_success_conjugate=n_success_conjugate,
            n_failure_conjugate=n_failure_conjugate,
            interval_type=interval_type,
            alternative=alternative,
        )

    @staticmethod
    def confidence_interval(
        a_success: int,
        b_success: int,
        a_trials: int,
        b_trials: int,
        confidence_level: float,
        interval_type: str = "wald",
        alternative: str = "two-sided",
        n_success_conjugate: int = None,
        n_failure_conjugate: int = None,
    ) -> types.IntervalType:
        """
        Main function building confidence interval.

        Parameters
        ----------
        a_success : int
            Samples from Bin(a_trials, p_a)
        b_success : int
            Sample from Bin(b_trials, p_b)
        a_trials : int
            Parameter for trials amount for group A
        b_trials : int
            Parameter for trials amount for group B
        confidence_level : float
            Pr ( [p1 - p2] in CI ) -> confidence_level, n -> infty
        interval_type : str, default : ``"wald"``
            One from [wald, yule, yule_modif, newcombe, jeffrey, agresti, bayes_beta, recenter]
        alternative : str, default : ``"two-sided"``
            Alternative for static criteria - two-sided, less, greater
            Less means, that mean in first group less, than mean in second group
        n_success : int
            Arguments for conjugate distribution Beta(n_success, n_failure) if interval_type = "bayes_beta"
        n_failure : int
            Arguments for conjugate distribution Beta(n_success, n_failure) if interval_type = "bayes_beta"

        Note
        ----
        You can pass numpy arrays with same shapes for a_success, b_success and e.t.c

        Returns
        -------
        interval : Tuple[float, float]
        """

        valid_types: List[str] = [
            "wald",
            "yule",
            "yule_modif",
            "newcombe",
            "jeffrey",
            "agresti",
            "bayes_beta",
            "recenter",
        ]
        pvalue_pkg.check_alternative(alternative)
        significance_level = pvalue_pkg.corrected_alpha(1 - confidence_level, alternative)
        if interval_type == "wald":
            left_ci, right_ci = BinomTwoSampleCI.__wald_ci(a_success, b_success, a_trials, b_trials, significance_level)
        elif interval_type == "yule":
            left_ci, right_ci = BinomTwoSampleCI.__yule_ci(
                a_success, b_success, a_trials, b_trials, significance_level, modified=False
            )
        elif interval_type == "yule_modif":
            left_ci, right_ci = BinomTwoSampleCI.__yule_ci(
                a_success, b_success, a_trials, b_trials, significance_level, modified=True
            )
        elif interval_type == "newcombe":
            left_ci, right_ci = BinomTwoSampleCI.__newcombe_ci(
                a_success, b_success, a_trials, b_trials, significance_level
            )
        elif interval_type == "recenter":
            left_ci, right_ci = BinomTwoSampleCI.__recentered_ci(
                a_success, b_success, a_trials, b_trials, significance_level
            )
        elif interval_type == "jeffrey":
            left_ci, right_ci = BinomTwoSampleCI.__bayes_conjugate_beta(
                a_success, b_success, a_trials, b_trials, 0.5, 0.5, significance_level
            )
        elif interval_type == "agresti":
            left_ci, right_ci = BinomTwoSampleCI.__bayes_conjugate_beta(
                a_success, b_success, a_trials, b_trials, 1, 1, significance_level
            )
        elif interval_type == "bayes_beta":
            error_beta_params: str = "Pass correct params n_success_conjugate, n_faliure_conjugate"
            args_correctness = (
                (n_success_conjugate is not None)
                and (n_failure_conjugate is not None)
                and (n_success_conjugate > 0)
                and (n_failure_conjugate > 0)
            )
            if not args_correctness:
                raise ValueError(error_beta_params)
            left_ci, right_ci = BinomTwoSampleCI.__bayes_conjugate_beta(
                a_success, b_success, a_trials, b_trials, n_success_conjugate, n_failure_conjugate, significance_level
            )
        else:
            algo_type_error: str = f'Choose one from accepted methods, from - {", ".join(valid_types)}'
            raise ValueError(algo_type_error)
        left_ci, right_ci = pvalue_pkg.choose_from_bounds(
            left_ci, right_ci, alternative, left_bound=np.array([-1]), right_bound=np.array([1])
        )
        return left_ci, right_ci

def get_table_power_on_size_and_conversions(
    interval_type: str = "wald",
    p_a_values: Iterable[float] = (0.5,),
    p_b_values: Iterable[float] = (0.4,),
    sample_sizes: Iterable[int] = (100,),
    amount: int = 10000,
    confidence_level: float = 0.95,
) -> pd.DataFrame:
    """
    Table with power / empirical 1 type error = 1 - coverage, for fixed size and conversions.

    Parameters
    ----------
    interval_type : str, default : ``"wald"``
        interval_type for confidence interval
    p_a_values : Iterable[float], default : ``(0.5,)``
        Conversions for A group
    p_b_values : Itrable[float], default : ``(0.5,)``
        Conversions for B group
    sample_sizes : Iterable[float], default : ``(100,)``
        Sizes for samples
    amount : int, default : ``10000``
        Amount of generated samples for one n(trials amount), to estimate power
    confidence_level : float, default : ``0.95``
        Such value x, that: Pr ( delta in I ) >= x

    Returns
    -------
    table : pd.DataFrame
        Required table with power
    """
    trials = np.array(sample_sizes)
    conversions_cond = np.all((np.array(p_a_values) >= 0) & (np.array(p_a_values) <= 1)) and np.all(
        (np.array(p_b_values) >= 0) & (np.array(p_b_values) <= 1)
    )
    if not conversions_cond:
        raise ValueError("Conversions must be from 0 to 1")
    powers_array: List[np.ndarray] = []
    for p_a in p_a_values:
        for p_b in p_b_values:
            sample_a = sps.binom.rvs(n=trials, p=p_a, size=(amount, len(sample_sizes)))
            sample_b = sps.binom.rvs(n=trials, p=p_b, size=(amount, len(sample_sizes)))
            binom_kwargs = {
                "interval_type": interval_type,
                "a_success": sample_a,
                "b_success": sample_b,
                "a_trials": trials,
                "b_trials": trials,
                "confidence_level": confidence_level,
            }
            conf_interval: types.ManyIntervalType = BinomTwoSampleCI.confidence_interval(**binom_kwargs)
            power: np.ndarray = helper_dir.__helper_calc_empirical_power(conf_interval)
            powers_array.append(power)
    power_matrix = np.vstack(powers_array)
    table = pd.DataFrame(
        power_matrix,
        index=pd.MultiIndex.from_tuples(
            [(round(p_a, 3), round(p_b, 3)) for p_a in p_a_values for p_b in p_b_values], names=[r"$p_a$", r"$p_b$"]
        ),
        columns=sample_sizes,
    )
    table.index.name = "conversions"
    table.columns.name = "sample sizes"
    return table

def get_table_power_on_size_and_delta(
    p_a: float,
    sample_sizes: Iterable[int],
    first_errors: Iterable[float] = (0.05,),
    delta_values: Iterable[float] = None,
    delta_relative_values: Iterable[float] = None,
    interval_type: str = "wald",
    # alternative: str = "two-sided",
    amount: int = 10000,
    as_numeric: bool = False,
) -> pd.DataFrame:
    """
    Table with power / empirical 1 type error = 1 - coverage for fixed size and effect.

    Parameters
    ----------
    p_a : Iterable[float]
        Conversion in A group
    sample_sizes : Iterable[float]
        Sizes for samples
    first_errors : Iterable[float], default : ``(0.05,)``
        First type error values
    delta_values : Iterable[float]
        Absolute delta values: p_b - p_a = delta
    delta_relative_values : Iterable[float]
        Relative delta values: p_b = delta_relative * p_a
    interval_type : str
        interval_type for confidence interval
    alternative : str, default : ``"two-sided"``
        Alternative for static criteria - two-sided, less, greater
        Less means, that mean in first group less, than mean in second group
    amount : int, default : ``10000``
        Amount of generated samples for one n(trials amount), to estimate power
    as_numeric : bool, default: ``False``
        The result of calculations can be obtained as a percentage string
        either as a number, this parameter could used to toggle.
        Works only for relative values of power.

    Returns
    -------
    table : pd.DataFrame
        Required table with power
    """
    trials = np.array(sample_sizes)
    if not (delta_values is None) ^ (delta_relative_values is None):
        raise RELATIVE_ABSOLUTE_DELTA_ERROR
    if delta_values is not None:
        p_b_values: np.ndarray = p_a + np.array(delta_values)
        grid_delta: np.ndarray = delta_values
    else:
        p_b_values: np.ndarray = p_a * np.array(delta_relative_values)
        grid_delta: np.ndarray = [f"{np.round((x - 1) * 100, ROUND_DIGITS_PERCENT)}%" for x in delta_relative_values]
    if not np.all((p_b_values >= 0) & (p_b_values <= 1)):
        raise ValueError(f"Probability of success in group B must be positive, not {p_b_values}")

    values = [(round(a, ROUND_DIGITS), b) for a in first_errors for b in grid_delta]
    table: pd.DataFrame = pd.DataFrame(
        index=pd.MultiIndex.from_tuples(values, names=[FIRST_TYPE_ERROR_COL_NAME, EFFECT_COL_NAME]),
        columns=trials,
    )
    table.columns.name = GROUP_SIZE_COL_NAME
    sample_a = sps.binom.rvs(n=trials, p=p_a, size=(amount, trials.shape[0]))
    for alpha in first_errors:
        for p_b, delta in zip(p_b_values, grid_delta):
            sample_b = sps.binom.rvs(n=trials, p=p_b, size=(amount, trials.shape[0]))
            binom_kwargs = {
                "interval_type": interval_type,
                "a_success": sample_a,
                "b_success": sample_b,
                "a_trials": trials,
                "b_trials": trials,
                "confidence_level": 1 - alpha,
                #    "alternative": alternative,
            }
            conf_interval: types.ManyIntervalType = BinomTwoSampleCI.confidence_interval(**binom_kwargs)
            power: np.ndarray = helper_dir.__helper_calc_empirical_power(conf_interval)
            if as_numeric:
                power = [round(power_val, ROUND_DIGITS_TABLE) for power_val in power]
            else:
                power = [str(round(power_val * 100, ROUND_DIGITS_PERCENT)) + "%" for power_val in power]
            table.loc[(alpha, delta), trials] = power
    return table

def iterate_for_sample_size(
    interval_type: str,
    first_errors: Iterable[float],
    second_errors: Iterable[float],
    p_a: float,
    p_b_values: Iterable[float],
    grid_delta: Iterable[float],
    amount: int,
) -> pd.DataFrame:
    """
    Iterate over params for different sample size
    """
    # values = [(round(a, ROUND_DIGITS), round(b, ROUND_DIGITS)) for a in first_errors for b in second_errors]
    # table: pd.DataFrame = pd.DataFrame(
    #     index=pd.MultiIndex.from_tuples(values, names=[r"$\alpha$", r"$\beta$"]),
    #     columns=grid_delta,
    # )
    multiindex = pd.MultiIndex.from_tuples([(eff,) for eff in grid_delta], names=[EFFECT_COL_NAME])
    multicols = pd.MultiIndex.from_tuples(
        [(f"({alpha}; {beta})",) for alpha in first_errors for beta in second_errors],
        names=[STAT_ERRORS_COL_NAME],
    )
    table: pd.DataFrame = pd.DataFrame(index=multiindex, columns=multicols)
    for alpha in first_errors:
        for beta in second_errors:
            power = 1 - beta
            for p_b, delta in zip(p_b_values, grid_delta):
                trials = helper_dir.__helper_bin_search_for_size(
                    interval_type=interval_type,
                    confidence_level=1 - alpha,
                    p_a=p_a,
                    p_b=p_b,
                    amount=amount,
                    power=power,
                )
                table.loc[delta, f"({alpha}; {beta})"] = trials
    return table

def get_table_sample_size_on_effect(
    interval_type: str = "wald",
    first_errors: Iterable[float] = (0.05,),
    second_errors: Iterable[float] = (0.2,),
    p_a: float = 0.5,
    delta_values: Iterable[float] = None,
    delta_relative_values: Iterable[float] = None,
    amount: int = 10000,
) -> pd.DataFrame:
    """
    Table for sample sizes with given effect and errors.

    Parameters
    ----------
    interval_type : str, default : ``"wald"``
        interval_type for confidence interval
    first_errors : Iterable[float], default : ``(0.05,)``
        First type error values
    second_errors : Iterable[float], default : ``(0.2,)``
        Second type error values
    p_a : Iterable[float]
        Conversion in A group, default : ``0.5``
    delta_values : Iterable[float]
        Absolute delta values: p_b - p_a = delta
    delta_relative_values : Iterable[float]
        Relative delta values: p_b = delta_relative * p_a
    amount : int, default : ``1000``
        Amount of generated samples for one n(trials amount), to estimate power

    Returns
    -------
    table : pd.DataFrame
        Required table with sample sizes
    """

    errors_condition = np.all((np.array(first_errors) >= 0) & (np.array(first_errors) <= 1)) and np.all(
        (np.array(second_errors) >= 0) & (np.array(second_errors) <= 1)
    )
    if not errors_condition:
        raise ValueError("Errors must be from 0 to 1")

    if (delta_values is not None) and (delta_relative_values is not None):
        raise RELATIVE_ABSOLUTE_DELTA_ERROR

    # If delta type not set => set to absolute
    if delta_values is None and delta_relative_values is None:
        delta_values = [p_a / 2]

    if delta_values is not None:
        p_b_values: np.ndarray = p_a + np.array(delta_values)
        grid_delta: np.ndarray = delta_values
    else:
        p_b_values: np.ndarray = p_a * np.array(delta_relative_values)
        grid_delta: np.ndarray = [f"{np.round((x - 1) * 100, ROUND_DIGITS_PERCENT)}%" for x in delta_relative_values]
    if not np.all((p_b_values >= 0) & (p_b_values <= 1)):
        raise ValueError(f"Probability of success in group B must be positive, not {p_b_values}")
    table = iterate_for_sample_size(interval_type, first_errors, second_errors, p_a, p_b_values, grid_delta, amount)
    return table

def iterate_for_delta(
    interval_type: str,
    first_errors: Iterable[float],
    second_errors: Iterable[float],
    sample_sizes: Iterable[int],
    p_a: float,
    amount: int,
    delta_type: str,
    as_numeric: bool = False,
) -> pd.DataFrame:
    """
    Helps to find effect for different params.
    """
    multiindex = pd.MultiIndex.from_tuples([(trials,) for trials in sample_sizes], names=[GROUP_SIZE_COL_NAME])
    multicols = pd.MultiIndex.from_tuples(
        [(f"({alpha}; {beta})",) for alpha in first_errors for beta in second_errors],
        names=[STAT_ERRORS_COL_NAME],
    )
    table: pd.DataFrame = pd.DataFrame(index=multiindex, columns=multicols)
    for alpha in first_errors:
        for beta in second_errors:
            power = 1 - beta
            for trials in sample_sizes:
                delta = helper_dir.__helper_bin_search_for_delta(
                    interval_type=interval_type,
                    confidence_level=1 - alpha,
                    p_a=p_a,
                    trials=trials,
                    amount=amount,
                    power=power,
                )
                if delta is not None and delta_type == "relative":
                    if as_numeric:
                        delta = round(abs(delta) / p_a, ROUND_DIGITS_TABLE) + 1
                    else:
                        delta = str(round(abs(delta) / p_a * 100, ROUND_DIGITS_PERCENT)) + "%"
                table.loc[trials, f"({alpha}; {beta})"] = delta
    return table

def get_table_effect_on_sample_size(
    interval_type: str = "wald",
    first_errors: Iterable[float] = (0.05,),
    second_errors: Iterable[float] = (0.2,),
    sample_sizes: Iterable[int] = (100,),
    p_a: float = 0.5,
    amount: int = 10000,
    delta_type: str = "relative",
    as_numeric: bool = False,
) -> pd.DataFrame:
    """
    Table for effects with given sample sizes and erros.
    If there are no effects satisfy first and second errors value will be set to None

    Parameters
    ----------
    interval_type : str, default : ``"wald"``
        interval_type for confidence interval
    first_errors : Iterable[float], default : ``(0.05,)``
        First type error values
    second_errors : Iterable[float], default : ``(0.2,)``
        Second type error values
    p_a : Iterable[float], default : ``0.5``
        Conversion in A group
    amount : int, default : ``10000``
        Amount of generated samples for one n(trials amount), to estimate power
    sample_sizes : Iterable[int], default : ``(100,)``
        Sample sizes for A/B group
    delta_type : str, default : ``"absolute``
        absolute or relative, if relative gives effect in percents: |delta| / p_a
    as_numeric : bool, default: ``False``
        The result of calculations can be obtained as a percentage string
        either as a number, this parameter could used to toggle.
        Works only for relative values of effect.

    Returns
    -------
    table : pd.DataFrame
        Required table with effects
    """
    errors_condition = np.all((np.array(first_errors) >= 0) & (np.array(first_errors) <= 1)) and np.all(
        (np.array(second_errors) >= 0) & (np.array(second_errors) <= 1)
    )
    if not errors_condition:
        error_mesage_errors: str = "Errors must be from 0 to 1"
        raise ValueError(error_mesage_errors)

    delta_types: List[str] = ["absolute", "relative"]
    if delta_type not in delta_types:
        raise ValueError(f"Delta type must be absolute relative, not {delta_type}")
    table: pd.DataFrame = iterate_for_delta(
        interval_type, first_errors, second_errors, sample_sizes, p_a, amount, delta_type, as_numeric
    )
    return table


"""
Display a bar's height and name on top of it upon hovering
==========================================================

Using an event handler to change the annotation text and position.
"""

import string
import matplotlib.pyplot as plt
import mplcursors

fig, ax = plt.subplots()
ax.bar(range(9), range(1, 10), align="center")
labels = string.ascii_uppercase[:9]
ax.set(xticks=range(9), xticklabels=labels, title="Hover over a bar")

# With HoverMode.Transient, the annotation is removed as soon as the mouse
# leaves the artist.  Alternatively, one can use HoverMode.Persistent (or True)
# which keeps the annotation until another artist gets selected.
cursor = mplcursors.cursor(hover=mplcursors.HoverMode.Transient)
@cursor.connect("add")
def on_add(sel):
    x, y, width, height = sel.artist[sel.index].get_bbox().bounds
    sel.annotation.set(text=f"{x+width/2}: {height}",
                       position=(0, 20), anncoords="offset points")
    sel.annotation.xy = (x + width / 2, y + height)

plt.show()


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

import argparse
import numpy as np
import h5py
import os
import cv2

def prepare(args):
    h5_file = h5py.File(args.output_path, 'w')
    noise_patchs = list()
    source_patchs = list()

    for count, image_file in enumerate(os.listdir(args.images_dir)):
        if count < 6000:
            continue
        print(image_file)
        source_img = cv2.imread(os.path.join(args.images_dir, image_file))
        # source_img = cv2.resize(source_img, (256, 256))
        ret, noise_buf = cv2.imencode(".JPG", source_img, [int(cv2.IMWRITE_JPEG_QUALITY), 10])
        noise_img = cv2.imdecode(noise_buf, 1)
        source_img = source_img.transpose(2, 0, 1)
        noise_img = noise_img.transpose(2, 0, 1)
        # print(source_img.shape)

        # source_img = np.array(source_img)
        # noise_img = np.array(noise_img)
        # noise_ds.append(noise_img)
        # source_ds.append(source_img)
        for i in range(0, source_img.shape[1] - args.patch_size + 1, args.stride):
            for j in range(0, source_img.shape[2] - args.patch_size + 1, args.stride):
                noise_patchs.append(noise_img[:, i:i + args.patch_size, j:j + args.patch_size])
                source_patchs.append(source_img[:, i:i + args.patch_size, j:j + args.patch_size])
        if count > 6010 :
            break

    noise_ds = np.array(noise_patchs, dtype=np.uint8)
    source_ds = np.array(source_patchs, dtype=np.uint8)
    print(noise_ds.shape)
    h5_file.create_dataset('noise', data=noise_ds)
    h5_file.create_dataset('source', data=source_ds)
    h5_file.close()
    pass

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--images-dir', type=str, required=True)
    parser.add_argument('--output-path', type=str, required=True)
    parser.add_argument('--patch-size', type=int, default=32)
    parser.add_argument('--stride', type=int, default=16)
    args = parser.parse_args()

    prepare(args)


from django.shortcuts import render,redirect,get_object_or_404
from .models import Recent,Song,Favourite,Playlist
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.db.models import Q

# Create your views here.

def indexPaige(request):
    # display recent music

    if not request.user.is_anonymous:
        recent = list(Recent.objects.filter(user=request.user).values('song_id').order_by('-id'))
        recent_id = [each['song_id'] for each in recent][:5]
        recent_songs_unsorted = Song.objects.filter(id__in=recent_id,recent__user=request.user)
        recent_songs = list()

        for id in recent_id:
            recent_songs.append(recent_songs_unsorted.get(id=id))
    else:
        recent = None
        recent_songs = None
    first_time = False

    #last played song

    if not request.user.is_anonymous:
            last_played_list = list(Recent.objects.filter(user=request.user).values('song_id').order_by('-id'))
            if last_played_list:
                last_played_id = last_played_list[0]['song_id']
                last_played_song =Song.objects.get(id=last_played_id)
            else:
                first_time =True
                last_played_song = Song.objects.get(id=2)
    else:
        first_time = True
        last_played_song = Song.objects.get(id=2)

#DISPLAY ALL THE SONGS
    songs = Song.objects.all()

# display songs on the home page
    songs_all = list(Song.objects.all().values('id').order_by('?'))
    slided_ids = [each['id'] for each in songs_all ][:5]
    indexpage_Songs = Song.objects.filter(id__in=slided_ids)

    # display Rap Songs
    songs_Rap = list(Song.objects.filter(genre='Rap').values('id'))
    slided_ids = [each['id'] for each in songs_Rap][:5]
    indexpaige_Rap_songs = Song.objects.filter(id__in=slided_ids)

    # display Gospel Songs
    songs_Gospel = list(Song.objects.filter(genre='Gospel').values('id'))
    slided_ids = [each['id'] for each in songs_Gospel][:5]
    indexpaige_Gospel_songs = Song.objects.filter(id__in=slided_ids)

    # display Rock Songs

    songs_Rock = list(Song.objects.filter(genre='Rock').values('id'))
    slided_ids = [each['id'] for each in songs_Rap][:5]
    indexpaige_Rock_songs = Song.objects.filter(id__in=slided_ids)

    if len(request.GET)>0:
        search_query = request.GET.get('q')
        filtered_songs = songs.filter(Q(name__icontains=search_query)).distinct()
        context ={
            'allsongs':filtered_songs,'last_played':last_played_song,'query_search':True
        }
        return render(request,'music/index.html',context=context)

    context ={
          'all_songs':indexpage_Songs,
          'recent_songs':recent_songs,
          'rap_songs':indexpaige_Rap_songs,
          'gospel_songs':indexpaige_Gospel_songs,
          'rock_songs':indexpaige_Rock_songs,
          'last_palyed':last_played_song,
          'first_time':first_time,
          'query_search':False
    }

    return render(request,'music/index.html',context=context)

def gospel_song(request):
    gospel_songs  = Song.objects.filter(genre='Gospel')

    last_played_list = list(Recent.objects.values('song_id').order_by('-id'))
    if last_played_list:
        last_played_id = last_played_list[0]['song_id']
        last_played_song = Song.objects.get(id=last_played_id)
    else:
        last_played_id_song = Song.objects.get(id=2)

    query = request.GET.get('q')

    if query:
        gospel_songs = Song.objects.filter(Q(name__icontains=query)).distinct()
        context = {
            'gospel_songs':gospel_songs

        }
        return render(request,music/rap_songs.html,context=context)

    context ={ 'gospel_songs':gospel_songs,'last_played':last_played_id_song}

    return render(request,music/rap_songs.html,context=context)

def rock_song(request):
    rock_songs  = Song.objects.filter(genre='Rock')

    last_played_list = list(Recent.objects.values('song_id').order_by('-id'))
    if last_played_list:
        last_played_id = last_played_list[0]['song_id']
        last_played_song = Song.objects.get(id=last_played_id)
    else:
        last_played_id_song = Song.objects.get(id=2)

    query = request.GET.get('q')

    if query:
        rap_songs = Song.objects.filter(Q(name__icontains=query)).distinct()
        context = {
            'rock_songs':rock_songs

        }
        return render(request,music/rap_songs.html,context=context)

    context ={ 'rock_songs':rock_songs,'last_played':last_played_id_song}

    return render(request,music/rap_songs.html,context=context)

def rap_song(request):
    rap_songs  = Song.objects.filter(genre='Rap')

    last_played_list = list(Recent.objects.values('song_id').order_by('-id'))
    if last_played_list:
        last_played_id = last_played_list[0]['song_id']
        last_played_song = Song.objects.get(id=last_played_id)
    else:
        last_played_id_song = Song.objects.get(id=2)

    query = request.GET.get('q')

    if query:
        rap_songs = Song.objects.filter(Q(name__icontains=query)).distinct()
        context = {
            'rap_songs':rap_songs

        }
        return render(request,music/rap_songs.html,context=context)

    context ={ 'rap_songs':rap_songs,'last_played':last_played_id_song}

    return render(request,music/rap_songs.html,context=context)

@login_required(login_url='login')
def play_song(request,song_id):
    songs = Song.objects.filter(id=song_id).first()

    #add this data to the recent dataabase
    if list(Recent.objects.filter(song=songs,user=request.user).values()):
        data = Recent.objects.filter(song=songs,user=request.user)
        data.delete()
    data = Recent(song=songs,user=request.user)
    data.save()
    return redirect('all_songs')

@login_required(login_url='login')
def play_song_index(request,song_id):
    songs = Song.objects.filter(id=song_id).first()

    if list(Recent.objects.filter(song=songs,user=request.user).values()):
        data = Recent.objects.filter(song=songs,user=request.user)
        data.delete()
    data = Recent(song=songs,user=request.user)
    data.save()
    return redirect('index')

@login_required(login_url='login')
def play_recent_song(request,song_id):
    songs = Song.objects.filter(id=song_id).first()

    #add this data to the recent dataabase
    if list(Recent.objects.filter(song=songs,user=request.user).values()):
        data = Recent.objects.filter(song=songs,user=request.user)
        data.delete()
    data = Recent(song=songs,user=request.user)
    data.save()
    return redirect('recent')

def all_songs(request):
    songs = Song.objects.all()

    first_time = False

    if not request.user.is_anonymous:
        last_played_list = list(Recent.objects.filter(user=request.user).values('song_id').order_by(['-id']))
        if last_played_list:
            last_played_id =  last_played_list[0]['song_id']
            last_played_song = Song.objects.get(id=last_played_id)
    else:
        first_time= True
        last_played_song = Song.objects.get(id='2')

        ######## APPLYING THE SEARCH FILTERS
        qs_singers = Song.objects.values_list('singer').all()
        s_list =[s.split(',') for singer in qs_singers for s in singers]
        all_singers = sorted(list(set([s.strip() for singer in  s_list for s in singer])))

        qs_genres = Song.objects.values_list('genre').all()
        all_genres = sorted(list(set([l.strip() for genr in qs_genres for g in genr])))

        if len(request.GET)>0:
            search_query = request.GET.get('q')
            search_singer = request.GET.get('singers') or ""
            search_genre = request.GET.get('genres') or ''
            filtered_Songs =songs.filter(Q(name__icontains=search_query)).filter(Q(genre__icontains=search_genre)).filter(Q(singer__icontains=search_singer)).distinct()

            context = {
                'songs':filtered_Songs,
                'last_played':last_played_song,
                'all_singers':all_singers,
                'all_genres': all_genres,
                'query_search':True,
            }

            return render(request, 'music/all_songs.html',context)

        context = {
            'songs':filtered_Songs,
            'last_played':last_played_song,
            'first_time':first_time,
            'all_singers':all_singers,
            'all_genres': all_genres,
            'query_search':False,
        }
        return render(request, 'music/all_songs.html',context)

def recent(request):

    ### last played songs
    last_played_list =  list(Recent.objects.values('song_id').order_by('-id'))

    if last_played_list:
        last_played_id = last_played_list[0]['song_id']
        last_played_song = Song.objects.get(id=last_played_id)
    else:
        last_played_song = Song.objects.get(id=2)

        ### Display the recent songs
    recent = list(Recent.objects.filter(user=request.user).values('song_id').order_by(-id))
    if recent and not request.user.is_anonymous:
        recent_id = [each['song_id'] for each in recent]
        recent_songs_unsorted = Song.objects.filter(id__in=recent_id,recent__user=request.user)
        recent_songs = list()

        for id in recent_id:
            recent_songs.append(recent_songs_unsorted.get(id=id))
    else:
        recent_songs= None

    if len(request.GET)>0:
        search_query = request.GET.get('q')
        filtered_songs = recent_songs_unsorted.filter(Q(name__icontains=search_query)).distinct()
        context = {
            'recent_songs':filtered_songs,'lastplayed':last_played_song, 'query_search':True

        }
        return render(request, 'music/recent.html',context)

    context = {
            'recent_songs':filtered_songs,'lastplayed':last_played_song, 'query_search':False

    }

    return render(request, 'music/recent.html',context)

@login_required(login_url='login')
def detail(request,song_id):
    song = Song.objects.filter(id=song_id).first()

    ## add data to the recent database

    if list(Recent.objects.filter(song=songs,user=request.user).values()):
        data = Recent.objects.filter(song=songs,user=request.user)
        data.delete()
    data = Recent(song=songs,user=request.user)
    data.save()

    # last played song
    last_played_list = list(Recent.objects.values('song_id').order_by('-id'))
    if last_play_list:
        last_played_id = last_played_list[0]['song_id']
        last_played_song = Song.objects.get(id=last_played_id)
    else:
        last_played_song = Song.objects.get(id=2)

    playlists = Playlist.objects.filter(user=request.user).values('playlist_name').distinct()
    is_favourite = Favourite.objects.filter(user=request.user).filter(song=song_id).values('is_fav')

    if request.method =='POST':
        if 'playlist' in request.POST:
            playlist_name =  request.POST['playlist']
            q = Playlist(user=request.user,song=songs,playlist_name=playlist_name)
            q.save()
            messages.success(request,'Song added to playlist')
        elif 'add-fav' in request.POST:
            is_fav = True
            query = Favourite(user=request.user,song=songs,is_fav=is_fav)
            query.save()
            messages.success(request,'Added to favourite')
            return rediect('detail',song_id=song_id)
        elif 'rm-fav' in request.POST:
            is_fav = True
            query = Favourite.objects.filter(user=request.user,song=songs,is_fav=is_fav)
            query.delete()
            messages.success(request,'Removed from favourites')
            return redirec('detail',song_id=song_id)
    context ={ 'songs':songs,'playlists':playlists,'is_favourite':is_favourite, 'last_played_song':last_played_song}

    return render(request,'music/detail.html',context=context)

def mymusic(request):
    return render(request,'music/mymusic.html')

def playlist(request):
    playlists =Playlists.objects.filter(user=request.user).values('playlist_name').distinct
    context = {
        'playlists':playlists
    }
    return render(request,'music/playlist.html',context=context)

def playlist_songs(request,playlist_name):
    songs = Song.objects.filter(playlist__playlist_name=playlist_name,song__id = song_id,user=request.user).distinct()

    if request.method == "POST":
        song_id = list(request.POST.keys())[1]
        favourite_song = Favourite.objects.filter(user=request.user,song__id = song_id,is_fav=True)
        playlist_song.delete()
        messages.success(request,"Song Removed from playlist")

        context ={
            'playlist_name':playlist_name,
            'songs':songs
        }
    return render(request,'music/playlist_song.html',context=content)

def favourite(request):
    songs = Song.objects.filter(favourite_user=request.user,favourite__is_fav=True).distinct()

    if request.method == "POST":
        song_id = list(request.POST.keys())[1]
        favourite_song = Favourite.objects.filter(user=request.user,song__id = song_id,is_fav=True)
        favourite_song.delete()
        messages.success(request,"Removed from favourite")
    context ={
        'songs':songs

    }
    return render(request,'music/favourite.html',context=context)