import datetime, json
from dateutil import parser

from django.http import HttpResponse
from django.template import loader
from django.views.generic import View
from django.core import serializers

from attendees.schemas import AttendeeSchema
from common.render import json_response
from scheduling.models import RecurringScheduledSession, OneTimeScheduledSession, ScheduleConfiguration
from scheduling.schemas import RecurringScheduledSessionSchema, OneTimeScheduledSessionSchema
from scheduling import constants

class ScheduleListView(View):

    def get(self, request, configuration_id):
        config = ScheduleConfiguration.objects.get(pk=configuration_id)
        recurring_sessions = RecurringScheduledSession.objects.filter(schedule_config_id=configuration_id).prefetch_related('attendees')
        one_time_scheduled_sessions = OneTimeScheduledSession.objects.filter(
            schedule_config_id=configuration_id,
            date__gte=datetime.date.today(),
            date__lte=datetime.date.today() + datetime.timedelta(days=7),
        ).prefetch_related('attendees')
        session_list = RecurringScheduledSessionSchema(many=True).dump(recurring_sessions) + OneTimeScheduledSessionSchema(many=True).dump(one_time_scheduled_sessions)
        session_list.sort(key=lambda x: parser.parse(x['date']))

        template = loader.get_template("userpages/schedules.html")

        return HttpResponse(template.render({'sessions': session_list, 'name': config.name}, request))

class ScheduleDetailView(View):

    def _get_session_data(self, session_id, session_type):
        session_cls = None
        session_serializer = None
        if session_type == 'recurring_scheduled_session':
            session_cls = RecurringScheduledSession
            session_serializer = RecurringScheduledSessionSchema
        elif session_type == 'one_time_scheduled_session':
            session_cls = OneTimeScheduledSession
            session_serializer = OneTimeScheduledSessionSchema
        else:
            raise Exception('everything is fucked')

        session = session_cls.objects.get(pk=session_id)
        return session_serializer().dump(session)

    def get(self, request, configuration_id, session_id, session_type):
        session_data = self._get_session_data(session_id, session_type)

        template = loader.get_template("userpages/session-detail.html")

        return HttpResponse(template.render({'session': session_data}, request))

    def post(self, request, configuration_id, session_id, session_type):
        data = json.loads(json.dumps(request.POST))
        data.pop('csrfmiddlewaretoken')
        attendee = AttendeeSchema().load(data)
        attendee.save()
        session_data = self._get_session_data(session_id, session_type)

        template = loader.get_template("userpages/session-detail.html")

        return HttpResponse(template.render({'session': session_data}, request))


import pygame
import random

# Initialisierung von Pygame
pygame.init()

# Fenstergröße und Fenster erstellen
window_size = (500, 500)
screen = pygame.display.set_mode(window_size)

# Titel des Fensters
pygame.display.set_caption("Snake")

# Farben als Dictionary definieren
colors = {
    "white": (255, 255, 255),
    "black": (0, 0, 0),
    "red": (255, 0, 0),
    "green": (0, 255, 0),
    "blue": (0, 0, 255)
}

# Font als statische Klasse definieren
class Font:
    font = pygame.font.Font(None, 25)
    @staticmethod
    def get_font():
        return Font.font

# Basisklasse für alle Spiel-Objekte
class GameObject:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def draw(self, screen, color):
        pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height))

# Snake Klasse erbt von GameObject
class Snake(GameObject):
    def __init__(self, x, y, width, height):
        super().__init__(x, y, width, height)
        self.vel = 5
        self.snake_list = [(x, y)]
        self.direction = "right"

    def draw(self, screen, color):
        for pos in self.snake_list:
            pygame.draw.rect(screen, color, (pos[0], pos[1], self.width, self.height))

    def move(self, keys):
        if keys[pygame.K_LEFT]:
            self.direction = "left"
        if keys[pygame.K_RIGHT]:
            self.direction = "right"
        if keys[pygame.K_UP]:
            self.direction = "up"
        if keys[pygame.K_DOWN]:
            self.direction = "down"

        if self.direction == "left":
            self.x -= self.vel
        if self.direction == "right":
            self.x += self.vel
        if self.direction == "up":
            self.y -= self.vel
        if self.direction == "down":
            self.y += self.vel

    def check_collision_with_borders(self, window_width, window_height):
        if self.x >= window_width or self.x < 0 or self.y >= window_height or self.y < 0:
            return True
        return False

# Food Klasse erbt von GameObject
class Food(GameObject):
    def __init__(self, x, y, width, height):
        super().__init__(x, y, width, height)

    def draw(self, screen, color):
        pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height))

# Die Klasse Game definiert das Spielverhalten
class Game:
    score_value = 0
    def __init__(self, snake, food, clock, score):
        self.snake = snake
        self.food = food
        self.clock = clock
        self.score = score

    def check_collision(self):
        snake_rect = pygame.Rect(self.snake.x, self.snake.y, self.snake.width, self.snake.height)
        food_rect = pygame.Rect(self.food.x, self.food.y, self.food.width, self.food.height)
        return snake_rect.colliderect(food_rect)

    def update_snake_position(self):
        self.snake.move(pygame.key.get_pressed())
        self.snake.snake_list.append((self.snake.x, self.snake.y))

    def update_food_position(self):
        if self.check_collision():
            self.food.x = round(random.randrange(0, window_size[0] - self.food.width) / 10.0) * 10.0
            self.food.y = round(random.randrange(0, window_size[1] - self.food.height) / 10.0) * 10.0
            self.score += 10
            self.snake.vel += 1
            self.snake.snake_list.append(self.snake.snake_list[-1])

    def update_score(self, screen, color):
        text = Font.get_font().render("Score: " + str(self.score), True, color)
        screen.blit(text, [0,0])

    def update_snake_length(self):
        if not self.check_collision():
            self.snake.snake_list.pop(0)

def game_loop(screen, clock):
    snake = Snake(250, 250, 10, 10)
    food = Food(round(random.randrange(0, window_size[0] - 10) / 10.0) * 10.0,
                round(random.randrange(0, window_size[1] - 10) / 10.0) * 10.0,
                10, 10)
    game = Game(snake, food, clock, 0)

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        screen.fill(colors["black"])
        game.update_snake_position()
        if snake.check_collision_with_borders(window_size[0], window_size[1]):
            run = False
        game.update_food_position()
        game.update_score(screen, colors["green"])
        snake.draw(screen, colors["green"])
        food.draw(screen, colors["red"])
        pygame.display.update()
        clock.tick(30)
        game.update_snake_length()

    pygame.quit()
    Game.score_value = game.score

game_loop(screen, pygame.time.Clock())


import pandas as pd
import numpy as np
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import neighbors
from sklearn import tree
from sklearn import neural_network
from sklearn.metrics import mean_absolute_error
import joblib
import tkinter as tk

# part 1
# read CSV file and convert to data ready for training
df = pd.read_csv("diabetes.csv")
df.dropna(axis=1, inplace=True)
df.head(9)

# normalize the numric data

def nom_x(item):
    X = np.array(df[item]).reshape(-1, 1)
    scaler = MinMaxScaler()
    scaler.fit(X)
    X_scaled = scaler.transform(X)
    df[item] = X_scaled.reshape(1, -1)[0]

def nom_input(item, item_min, item_max):
    return (float(item) - item_min) / (item_max - item_min)

# the max and min value
# to normalize user input data
preg_max = df["preg"].max()
preg_min = df["preg"].min()
plas_max = df["plas"].max()
plas_min = df["plas"].min()
pres_max = df["pres"].max()
pres_min = df["pres"].min()
skin_max = df["skin"].max()
skin_min = df["skin"].min()
insu_max = df["insu"].max()
insu_min = df["insu"].min()
mass_max = df["mass"].max()
mass_min = df["mass"].min()
pedi_max = df["pedi"].max()
pedi_min = df["pedi"].min()
age_max = df["age"].max()
age_min = df["age"].min()

# normnize the Xs
nom_x("preg")
nom_x("plas")
nom_x("pres")
nom_x("skin")
nom_x("insu")
nom_x("mass")
nom_x("pedi")
nom_x("age")

# one-hot-encoder to convert Y
encoder = OneHotEncoder(handle_unknown="ignore")
encoder_df = pd.DataFrame(encoder.fit_transform(df[["class"]]).toarray())

# put the processed data and new titles into a new CSV file
df = df.join(encoder_df)
df.drop("class", axis=1, inplace=True)
df.drop(0, axis=1, inplace=True)
df.columns = ["preg", "plas", "pres", "skin",
              "insu", "mass", "pedi", "age", "diabete"]
df.to_csv("processed_data.csv", index=False)

# part 2
# Training model with processed data
train_model = neighbors.KNeighborsRegressor()  # set default model
test_rate = 0.1  # default training set (90%) test set (10%)

def training():
    df = pd.read_csv("processed_data.csv")
    X = df[["preg", "plas", "pres", "skin",
            "insu", "mass", "pedi", "age"]]
    y = df["diabete"]
    X_train, X_test, y_train, y_test = train_test_split(
        X.values, y, test_size=test_rate)
    train_model.fit(X_train, y_train)
    joblib.dump(train_model, "diabete_diagnose_model.pkl")
    mse_train = mean_absolute_error(y_train, train_model.predict(X_train))
    mse_test = mean_absolute_error(y_test, train_model.predict(X_test))
    lbl_error1["text"] = f" Train with: {train_model} with test rate of: {test_rate}"
    lbl_error2["text"] = f" Training Set Error: {mse_train}- Test Set Error: {mse_test}"

# part 3
# use trained model to predict the user input

def prediction():
    preg = ent_preg.get()
    plas = ent_plas.get()
    pres = ent_pres.get()
    skin = ent_skin.get()
    insu = ent_insu.get()
    mass = ent_mass.get()
    pedi = ent_pedi.get()
    age = ent_age.get()

    preg_nom = nom_input(preg, preg_min, preg_max)
    plas_nom = nom_input(plas, plas_min, plas_max)
    pres_nom = nom_input(pres, pres_min, pres_max)
    skin_nom = nom_input(skin, skin_min, skin_max)
    insu_nom = nom_input(insu, insu_min, insu_max)
    mass_nom = nom_input(mass, mass_min, mass_max)
    pedi_nom = nom_input(pedi, pedi_min, pedi_max)
    age_nom = nom_input(age, age_min, age_max)

    use_model = joblib.load("diabete_diagnose_model.pkl")
    patient_input = [preg_nom, plas_nom, pres_nom, skin_nom,
                     insu_nom, mass_nom, pedi_nom, age_nom]
    patient_list = [
        patient_input
    ]
    patient_out = use_model.predict(patient_list)
    predicted_result = patient_out[0]
    if predicted_result > 0.5:
        lbl_predic["text"] = "Diabeties Positive"
    else:
        lbl_predic["text"] = "Diabeties Nagetive"

# part 4
# define the variable value when push the button
def LinearRegression_btn():
    global train_model
    train_model = LinearRegression()

def KNN_btn():
    global train_model
    train_model = neighbors.KNeighborsRegressor(5)

def DecisionTree_btn():
    global train_model
    train_model = tree.DecisionTreeClassifier()

def NNOneHiddenLayer_btn():
    global train_model
    train_model = neural_network.MLPClassifier(hidden_layer_sizes=(8, ), activation='relu', solver='adam', max_iter=1000,
                                               verbose=1, tol=1e-6, random_state=1, learning_rate_init=.1)

def NNFiveHiddenLayer_btn():
    global train_model
    train_model = neural_network.MLPClassifier(hidden_layer_sizes=(8, 8, 8, 8, 8,), activation='relu', solver='adam', max_iter=1000,
                                               verbose=1, tol=1e-6, random_state=1, learning_rate_init=.1)

def rate_66():
    global test_rate
    test_rate = 0.34

def rate_95():
    global test_rate
    test_rate = 0.05

def rate_75():
    global test_rate
    test_rate = 0.25

def rate_80():
    global test_rate
    test_rate = 0.2

# GUI for final user
window = tk.Tk()
window.title("Diabetes Diagnosis Prediction")
window.resizable(width=False, height=False)

frm_algorithm = tk.Frame()
frm_algorithm.pack(fill=tk.X, ipadx=5, ipady=5)
btn_LinearRegression = tk.Button(
    master=frm_algorithm,
    text="LinearRegression",
    command=LinearRegression_btn
)
btn_LinearRegression.pack(side=tk.LEFT, ipadx=10)
btn_KNN = tk.Button(
    master=frm_algorithm,
    text="KNN",
    command=KNN_btn
)
btn_KNN.pack(side=tk.LEFT, ipadx=10)
btn_DecisionTree = tk.Button(
    master=frm_algorithm,
    text="DecisionTree",
    command=DecisionTree_btn
)
btn_DecisionTree.pack(side=tk.LEFT, ipadx=10)
btn_NNOneHiddenLayer = tk.Button(
    master=frm_algorithm,
    text="NNOneHiddenLayer",
    command=NNOneHiddenLayer_btn
)
btn_NNOneHiddenLayer.pack(side=tk.LEFT, ipadx=10)
btn_NNFiveHiddenLayer = tk.Button(
    master=frm_algorithm,
    text="NNFiveHiddenLayer",
    command=NNFiveHiddenLayer_btn
)
btn_NNFiveHiddenLayer.pack(side=tk.LEFT, ipadx=10)

frm_split = tk.Frame()
frm_split.pack(fill=tk.X, ipadx=5, ipady=5)
btn_66 = tk.Button(
    master=frm_split,
    text="Training rate 66%",
    command=rate_66
)
btn_66.pack(side=tk.LEFT, ipadx=8)
btn_75 = tk.Button(
    master=frm_split,
    text="Training rate 75%",
    command=rate_75
)
btn_75.pack(side=tk.LEFT, ipadx=8)
btn_80 = tk.Button(
    master=frm_split,
    text="Training rate 80%",
    command=rate_80
)
btn_80.pack(side=tk.LEFT, ipadx=8)
btn_95 = tk.Button(
    master=frm_split,
    text="Training rate 95%",
    command=rate_95
)
btn_95.pack(side=tk.LEFT, ipadx=8)

frm_training = tk.Frame()
frm_training.pack(fill=tk.X, ipadx=5, ipady=5)
btn_training = tk.Button(
    master=frm_training,
    text="Train the model\N{RIGHTWARDS BLACK ARROW}",
    command=training
)
lbl_error1 = tk.Label(master=frm_training,
                      text="Please Choose Algrithum and training rate")
lbl_error2 = tk.Label(master=frm_training,
                      text="Then click \"Train the model\"")
btn_training.grid(row=0, column=0, pady=10)
lbl_error1.grid(row=1, column=1, padx=10)
lbl_error2.grid(row=2, column=1, padx=10)

frm_form = tk.Frame(relief=tk.SUNKEN, borderwidth=3)
frm_form.pack()

lbl_preg = tk.Label(master=frm_form, text="Preg")
ent_preg = tk.Entry(master=frm_form, width=50)
lbl_preg.grid(row=0, column=0, sticky="e")
ent_preg.grid(row=0, column=1)
lbl_plas = tk.Label(master=frm_form, text="Plas")
ent_plas = tk.Entry(master=frm_form, width=50)
lbl_plas.grid(row=1, column=0, sticky="e")
ent_plas.grid(row=1, column=1)
lbl_pres = tk.Label(master=frm_form, text="Pres")
ent_pres = tk.Entry(master=frm_form, width=50)
lbl_pres.grid(row=2, column=0, sticky="e")
ent_pres.grid(row=2, column=1)
lbl_skin = tk.Label(master=frm_form, text="Skin")
ent_skin = tk.Entry(master=frm_form, width=50)
lbl_skin.grid(row=3, column=0, sticky="e")
ent_skin.grid(row=3, column=1)
lbl_insu = tk.Label(master=frm_form, text="Insu")
ent_insu = tk.Entry(master=frm_form, width=50)
lbl_insu.grid(row=0, column=2, sticky="e")
ent_insu.grid(row=0, column=3)
lbl_mass = tk.Label(master=frm_form, text="Mass")
ent_mass = tk.Entry(master=frm_form, width=50)
lbl_mass.grid(row=1, column=2, sticky="e")
ent_mass.grid(row=1, column=3)
lbl_pedi = tk.Label(master=frm_form, text="Pedi")
ent_pedi = tk.Entry(master=frm_form, width=50)
lbl_pedi.grid(row=2, column=2, sticky="e")
ent_pedi.grid(row=2, column=3)
lbl_age = tk.Label(master=frm_form, text="Age")
ent_age = tk.Entry(master=frm_form, width=50)
lbl_age.grid(row=3, column=2, sticky="e")
ent_age.grid(row=3, column=3)

frm_buttons = tk.Frame()
frm_buttons.pack(fill=tk.X, ipadx=5, ipady=5)
btn_submit = tk.Button(
    master=frm_buttons,
    text="Run the prediction",
    command=prediction
)
btn_submit.pack(side=tk.LEFT, padx=10, ipadx=10)
lbl_predic = tk.Label(master=frm_buttons, text="prediction")
lbl_predic.pack(side=tk.LEFT, padx=10, ipadx=10)

window.mainloop()


from django.urls import path, include
from rest_framework import routers
from .views import EmployeeSerializer, EmployeeEmergencyContactSerializer

router = routers.DefaultRouter()
router.register(r'employees', EmployeeSerializer)
router.register(r'employee-emergency-contact',
                EmployeeEmergencyContactSerializer)

urlpatterns = [
    path('', include(router.urls))
]


import os
import time
from functools import partial
import random
from transformers import HfArgumentParser
from transformers.file_utils import ExplicitEnum

class BiasType(ExplicitEnum):
    LEXICAL = "lexical_bias"
    HYPOTHESIS = "hypothesis_only_bias"
    UNKNOWN = "unknown_bias"

def get_indices_arg(bias_type):
    evaluations_subsets = {
        BiasType.LEXICAL: [],
        BiasType.HYPOTHESIS: [
            # 'data/mnli_hypothesis_only_hard'
        ]
    }
    if bias_type == BiasType.UNKNOWN:
        evals = []
        # merge all evaluations sets and add unknown bias set
        for k in evaluations_subsets:
            evals += evaluations_subsets[k]
        # evals += ['data/tinybert_splits/5_folds_0.8_confidence/validation_matched']
        evaluations_subsets = evals
    else:
        evaluations_subsets = evaluations_subsets[bias_type]
    indices_arg = '--evaluate_on_hans' if bias_type in [BiasType.LEXICAL, BiasType.UNKNOWN] else ''
    if len(evaluations_subsets) > 0:
        indices_arg = indices_arg + f" --indices_dir  {' '.join(evaluations_subsets)}"

    if bias_type in [BiasType.UNKNOWN, BiasType.HYPOTHESIS]:
        indices_arg = f"{indices_arg} --mismatched_indices_dir data/hypothesis_bias_splits/validation_mismatched"
    return indices_arg

def sim_args(bias_type, biased_models, enforce_sim, unbiased_models):
    if enforce_sim:
        enforce_sim_arg = "--enforce_similarity"
        sim_name_arg = "_sim"
        weak_model_dir, fw_name = unbiased_models[bias_type]
    else:
        sim_name_arg = ""
        enforce_sim_arg = ""
        weak_model_dir, fw_name = biased_models[bias_type]
    return enforce_sim_arg, fw_name, sim_name_arg, weak_model_dir

def layer_to_arg(l, model_name='deberta'):
    layer_maper = {
        'P': 'pooler' if model_name == 'deberta' else 'bert.pooler',
        'e': f'{model_name}.embeddings'
    }
    return layer_maper[l] if l in layer_maper else f"{model_name}.encoder.layer.{l}"

def layers_args(regularized_layers_list, incraese_sim: bool):
    if type(regularized_layers_list) is tuple:
        main_model_layers = regularized_layers_list[0]
        weak_model_layers  = regularized_layers_list[1]
    else:
        main_model_layers = regularized_layers_list
        weak_model_layers = regularized_layers_list

    if type(main_model_layers) is not list:
        main_model_layers = [main_model_layers]
    if type(weak_model_layers) is not list:
        weak_model_layers = [weak_model_layers]

    weak_model_layers = list(map(str, weak_model_layers))
    main_model_layers = list(map(str, main_model_layers))

    layers_suffix = "-".join(weak_model_layers)
    layers_prefix = "-".join(map(lambda x: f"{x}M", main_model_layers))
    layers_arg = f"{layers_prefix}_{layers_suffix}"

    main_regularized_layers = " ".join(map(layer_to_arg, main_model_layers))
    if incraese_sim:
        weak_regularized_layers = " ".join(map(layer_to_arg, weak_model_layers))
    else:
        weak_regularized_layers = " ".join(map(partial(layer_to_arg, model_name='bert'), weak_model_layers))
    return layers_arg, main_regularized_layers, weak_regularized_layers

def only_bias_arg(reg_only_biased, bias_type: BiasType):
    bias_indices = {
        BiasType.LEXICAL: "data/lexical_bias_splits/train/confidence_based_0.65/train_biased_correct_indices.bin",
        BiasType.UNKNOWN: "data/tinybert_splits/5_folds_0.8_confidence/train/train_biased_correct_indices.bin",
        BiasType.HYPOTHESIS: "data/hypothesis_bias_splits/5_folds_0.6_confidence/train/train_biased_correct_indices.bin"
    }[bias_type]

    reg_only_biased_arg = ""
    reg_only_biased_identifier = ""
    if reg_only_biased:
        reg_only_biased_arg = f'--regularize_only_biased --bias_indices {bias_indices}'
        reg_only_biased_identifier = 'STOB_'
    return reg_only_biased_arg, reg_only_biased_identifier

def grads_arg(node_name, regularize_grads):
    grads_suffix = "_GReg" if regularize_grads else ""
    grads_arguments = "--regularize_grads" if regularize_grads else ""
    eval_bs = 64 if regularize_grads or node_name in ['plato1', 'plato2', 'nlp-2080-1', 'nlp-2080-2'] else 256
    return eval_bs, grads_arguments, grads_suffix

def main():
    parser = HfArgumentParser([])
    parser.add_argument('--regularize_only_grads', action='store_true', help='re-initialize weights of models randomly')
    parser.add_argument('--enforce_sim', action='store_true', help='re-initialize weights of models randomly')
    args = parser.parse_args()
    # main configs:
    regularize_only_grads = args.regularize_only_grads
    bias_type = BiasType.LEXICAL
    enforce_sim = args.enforce_sim

    # if regularize_only_grads:
    #     node_name = "nlp-a40-1"  # galileo2, newton2
    # else:
    #     node_name = "galileo2"  # plato1, plato2
    node_name = "nlp-a40-1"
    debertav3 = True
    if debertav3:
        unbiased_models = {
            BiasType.LEXICAL: ('runs/debertav3/extrensic_debiasing/lexical_bias/debertav3_64_lexical_extrensic_debiasing/checkpoint-11678', 'EDXE'),
            BiasType.UNKNOWN: ('runs/debertav3/extrensic_debiasing/unknown_bias/debertav3_64_unknown_extrensic_debiasing/checkpoint-9020', 'EDXE'),
            BiasType.HYPOTHESIS: ('runs/debertav3/extrensic_debiasing/hypohtesis_only_bias/debertav3_64_hypothesis_extrensic_debiasing/checkpoint-7044', 'EDXE')
        }
    else:
        unbiased_models = {
            BiasType.LEXICAL: ('runs/deberta/extrensic_debiasing/lexical_bias/deberta_64_lexical_extrensic_debiasing/checkpoint-11678', 'EDXE'),
            BiasType.UNKNOWN: ('runs/deberta/extrensic_debiasing/unknown_bias/deberta_64_unknown_extrensic_debiasing/checkpoint-9020', 'EDXE'),
            BiasType.HYPOTHESIS: ('runs/deberta/extrensic_debiasing/hypothesis_bias/deberta_64_hypothesis_extrensic_debiasing/checkpoint-7044', 'EDXE')
        }
    biased_models = {
        BiasType.LEXICAL: ('runs/bert_lexical_bias', 'LexicalModel'),
        BiasType.HYPOTHESIS: ([f'runs/5_fold_hypothesis_only/bert_{i}_hypothesis_only/checkpoint-14724' for i in range(4)], 'HOBert'),
        BiasType.UNKNOWN: ([f'runs/5_folds_unknown_bias/tinybert_{i}' for i in range(5)], 'TB')
    }
    # BiasType.LEXICAL: ('runs/deberta/extrensic_debiasing/lexical_bias/deberta_64_lexical_extrensic_debiasing', 'ED'),

    # BiasType.LEXICAL: ('runs/bert_lexical_bias', 'LexicalModel'),
    # BiasType.LEXICAL: ('runs/deberta/UBD/lexical_bias/UB_deberta', 'UBD'),

    batch_size = 64
    epochs = 10
    eval_bs, grads_arguments, grads_suffix = grads_arg(node_name, regularize_grads=regularize_only_grads)

    enforce_sim_arg, fw_name, sim_name_arg, weak_model_dir = sim_args(bias_type, biased_models, enforce_sim, unbiased_models)

    run_name_identifier = {
        BiasType.LEXICAL: 'lexicalDeBias',
        BiasType.HYPOTHESIS: 'HypoDeBias',
        BiasType.UNKNOWN: 'UNB'
    }[bias_type]

    main_task_lambda = 1
    indices_arg = get_indices_arg(bias_type)
    tags_arg = f'--tags {bias_type.value}'
    bias_dir = {
        BiasType.LEXICAL: 'lexical_bias_debiasing',
        BiasType.HYPOTHESIS: 'hypothesis_bias_debiasing',
        BiasType.UNKNOWN: 'unknown_bias/0.8_confidence'
    }[bias_type]
    bias_dir = bias_dir + ("/similarity" if enforce_sim else "/dissimilarity")

    reg_only_biased_arg, reg_only_biased_identifier = only_bias_arg(reg_only_biased=True, bias_type=bias_type)
    model_arg = 'microsoft/deberta-base' if not debertav3 else 'microsoft/deberta-v3-base'
    base_dir = 'deberta' if not debertav3 else 'debertav3'
    counter = 0
    for regularization_lambda in [100]:
        for regularization_method in ["linear_cka"]:
            # (['e', 10, 11], ['e', 1, 1]), ['e', 0, 6, 10, 11], ['e', 0, 6, 10, 11, 'P']
            for l in [[9, 10, 11]]:
                layers_arg, main_regularized_layers, weak_regularized_layers = layers_args(regularized_layers_list=l, incraese_sim=enforce_sim)
                reg_tokens_arg = "all" if 'P' not in l else " ".join(map(lambda x: "none" if x == 'P' else "all", l))
                model_name = f"{base_dir}_{fw_name}_{reg_only_biased_identifier}{run_name_identifier}_{regularization_method}_{layers_arg}_lambda_" \
                             f"{main_task_lambda}_{regularization_lambda}_{batch_size}{sim_name_arg}{grads_suffix}"
                seeds = [random.randint(0, 10000), random.randint(0, 1000)]
                for seed in seeds:
                    if type(weak_model_dir) is list:
                        current_seed_weak_model = weak_model_dir[random.randint(0, len(weak_model_dir) - 1)]
                    else:
                        current_seed_weak_model = weak_model_dir
                    output_dir = f"runs/{base_dir}/{bias_dir}/{regularization_method}/{model_name}_s{seed}"
                    tokenization_arg = '--separate_weak_tokenization' if bias_type in [BiasType.HYPOTHESIS, BiasType.UNKNOWN] and not enforce_sim else ''
                    padding_arg = '' #'--no_pad_to_max_length' if enforce_sim else ''
                    os.system(f"""
                        echo nlp_sbatch {node_name} run_glue.py --report_to wandb --model_name_or_path {model_arg} \
                            --output_dir {output_dir} --num_train_epochs {epochs} --learning_rate 2e-5 --per_device_eval_batch_size {eval_bs} \
                             --task_name mnli --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size {batch_size} \
                             --weak_model_layer {weak_regularized_layers} --weak_model_path {current_seed_weak_model} --regularized_layers {main_regularized_layers} \
                             --regularization_method {regularization_method} --evaluation_strategy half_epoch --save_strategy no \
                             --regularization_lambda {regularization_lambda} --run_name {model_name} --weight_decay 0.1 --warmup_ratio 0.1 \
                             --logging_first_step --regularized_tokens {reg_tokens_arg} --token_aggregation_strategy mean {enforce_sim_arg} \
                             {grads_arguments} {indices_arg} {tags_arg} {reg_only_biased_arg} --main_task_lambda {main_task_lambda} \
                             --bias_sampling_strategy stochastic --seed {seed} --wandb_group {tokenization_arg} {padding_arg}
                    """)
                    print()
                    counter += 1

    print(f"dispatched {counter} jobs")

if __name__ == '__main__':
    main()


from direct.gui.DirectGui import *
from direct.showbase.MessengerGlobal import messenger
from panda3d.core import *
from libotp import *
from direct.task.Task import Task
from direct.fsm import StateData
from direct.showbase import AppRunnerGlobal
from direct.directnotify import DirectNotifyGlobal
from toontown.toonbase import TTLocalizer
from toontown.toonbase.ToontownBattleGlobals import Tracks
from toontown.toontowngui import TTDialog
from toontown.toonbase import ToontownGlobals
from toontown.toonbase.DisplayOptions import DisplayOptions

class TrackSetDialog(DirectFrame, StateData.StateData):
    notify = DirectNotifyGlobal.directNotify.newCategory('TrackSetDialog')

    def __init__(self):
        DirectFrame.__init__(self, pos=(0, 0, 0.3), relief=None, image=DGG.getDefaultDialogGeom(),
                             image_scale=(1.6, 1, 1.2), image_pos=(0, 0, -0.05),
                             image_color=ToontownGlobals.GlobalDialogColor, text=TTLocalizer.TrackSetTitle,
                             text_scale=0.12, text_pos=(0, 0.4), borderWidth=(0.01, 0.01))
        StateData.StateData.__init__(self, 'close-track-selection')

        self.infoText = DirectLabel(parent=self, relief=None, scale=TTLocalizer.DSDintroText, pos=(-0.725, 0, 0.3),
                                    text=TTLocalizer.TrackSetInfo,
                                    text_wordwrap=TTLocalizer.DSDintroTextWordwrap,
                                    text_align=TextNode.ALeft)

        guiButton = loader.loadModel('phase_3/models/gui/quit_button')
        self.trackButtons = []
        for i in xrange(len(Tracks)):
            posx = -0.6 + 0.4 * (i % 4)
            posz = -0.05 - 0.2 * (i / 4)
            self.trackButtons.append(DirectButton(parent=self, relief=None, pos=(posx, 0, posz),
                                                  image=(guiButton.find('**/QuitBtn_UP'),
                                                         guiButton.find('**/QuitBtn_DN'),
                                                         guiButton.find('**/QuitBtn_RLVR')),
                                                  image_scale=(0.6, 1, 1),
                                                  text=Tracks[i].upper(),
                                                  text_scale=0.06,
                                                  text_pos=(0, -0.02),
                                                  command=self.__chooseTrack,
                                                  extraArgs=[i]))
        self.cancel = DirectButton(parent=self, relief=None, pos=(0, 0, -0.53),
                                   image=(guiButton.find('**/QuitBtn_UP'),
                                          guiButton.find('**/QuitBtn_DN'),
                                          guiButton.find('**/QuitBtn_RLVR')),
                                   image_scale=(0.6, 1, 1),
                                   text=TTLocalizer.DisplaySettingsCancel,
                                   text_scale=0.06,
                                   text_pos=(0, -0.02),
                                   command=self.__cancel)

        guiButton.removeNode()
        self.initialiseoptions(TrackSetDialog)
        self.chosenTrack = None
        self.chosenTrackName = None
        self.frameNum = None
        return

    def unload(self):
        if self.isLoaded == 0:
            return None
        self.isLoaded = 0
        self.exit()
        self.hide()
        return None

    def load(self):
        if self.isLoaded == 1:
            return None
        self.isLoaded = 1
        self.setBin('gui-popup', 0)
        self.hide()
        return

    def enter(self, frameNum=0):
        if self.isEntered == 1:
            return None
        self.isEntered = 1
        if self.isLoaded == 0:
            self.load()
        self.applyDialog = None
        base.transitions.fadeScreen(0.5)
        self.frameNum = frameNum
        trainingFrames = base.localAvatar.getTrainingFrames()
        for i in xrange(len(Tracks)):
            if trainingFrames.count(i) >= 3:
                self.trackButtons[i]['state'] = DGG.DISABLED
            else:
                self.trackButtons[i]['state'] = DGG.NORMAL
        self.show()
        return

    def exit(self):
        if self.isEntered == 0:
            return None
        if self.isLoaded:
            self.unload()
        self.isEntered = 0
        self.cleanupDialogs()
        base.transitions.noTransitions()
        self.ignoreAll()
        return None

    def cleanupDialogs(self):
        if self.applyDialog:
            self.applyDialog.cleanup()
            self.applyDialog = None
        return

    def __chooseTrack(self, trackNum):
        self.chosenTrack = trackNum
        self.chosenTrackName = Tracks[trackNum].upper()
        self.cleanupDialogs()
        self.clearBin()
        self.applyDialog = TTDialog.TTDialog(dialogName='TrackSetApply', style=TTDialog.TwoChoice,
                                             text=TTLocalizer.TrackSetConfirmation % self.chosenTrackName,
                                             text_wordwrap=15, command=self.__applyDone)
        self.applyDialog.setBin('gui-popup', 0)

    def __applyDone(self, command):
        self.applyDialog.clearBin()
        self.applyDialog.cleanup()
        self.applyDialog = None
        if command != DGG.DIALOG_OK:
            return
        messenger.send('chosen-track', [self.chosenTrack, self.frameNum])
        self.exit()

    def __cancel(self):
        messenger.send('close-track-selection')
        self.exit()


#### 赋值 (=)就是创建了对象的一个新的引用，修改其中任意一个变量都会影响到另一个
import copy

def fuzhi():
    a = [1,2]
    b = a
    print("没改变b值时,a和b的值:",a,b) ### [1, 2] [1, 2]
    ### 修改b, a也随之改变 (注意是修改,而不是重新给 b 赋值)
    b.append(3)
    print("改变b值时,a和b的值:",a,b) ### [1, 2, 3] [1, 2, 3]

    ## 打印 a , b的地址
    print("a,b的地址: ",id(a),id(b)) ### 1810258324616 1810258324616

"""浅拷贝:浅拷贝会新建一个对象，拷贝对象与原对象的变化间有无关联.要看原对象中的元素类型
如果是可变参数,则会影响; 如果是不可变参数则不会影响."""
def qian_copy_1():

    ############# a 中的元素(int型)是不可变对象 情况 ############
    a = [1,2]
    b = a.copy()
    print("没改变b值时,a和b的值:",a,b)  ### [1, 2] [1, 2]
    ### 浅拷贝只是拷贝了一层,所以 b 与 a 不是同一个引用
    b.append(30)
    print("改变b值时,a和b的值:",a,b)  ### [1, 2] [1,2,3]

    ## 打印 a , b的地址
    print("a,b的地址: ",id(a),id(b))  #### 2461021105480 2461021105416

def qian_copy_2():
   ############# a 中的元素(int型)是 可变对象 情况 ############
    a = [[1],[2]]
    b = a.copy()

    # print(id(a),id(b)) ### 第一层引用地址是不一样:3028232891528 3028232891464
    # print(id(a[0]),id(b[0])) ### 第二层应用地址就一样了:3028232891720 3028232891720

    print("没改变b值时,a和b的值:",a,b)  ### [[1], [2]] [[1], [2]]
    ### 浅拷贝只是拷贝了一层,所以 b 与 a 不是同一个引用,但是: b[0] 与 a[0] 都是同一个引用
    b[0].append(10)
    print("改变b值时,a和b的值:",a,b)  ### [[1, 10], [2]] [[1, 10], [2]]

    ## 打印 a , b的地址
    print("a,b的地址: ",id(a),id(b))  #### 1581996698760 1581996698696

#####深拷贝：创建一个新的对象，并且递归的复制它所包含的对象（修改其中一个，另外一个不会改变）
def deep_copy():
    a = [[1],2,3]
    b = copy.deepcopy(a)
    #### 深拷贝指拷贝数据集合的所有层,所以 b 与 a 不是同一个引用,而且 b[0] 与 a[0] 也不是同一个引用
    b[0].append(100)

    print("改变b值时,a和b的值:",a,b) #### [[1], 2, 3] [[1, 100], 2, 3]

    ## 打印 a , b的地址
    print("a,b的地址: ",id(a),id(b)) #### 2476710030600 2476710030472

# fuzhi()
# qian_copy_1()
qian_copy_2()
# deep_copy()

赋值_浅拷贝_深拷贝的区别.py


'''
In IDLE code a Python program.
Import csv.
Open potions.csv for reading.
Define an empty list.
Read the file.
Loop through the content, adding each line to the list.
Display the list. (This line isn't indented.)
'''

import csv
with open("potions.csv") as J:
    potionsData = csv.reader(J)
    listForPotionData = []
    for every_line in potionsData:
        listForPotionData += every_line

print(listForPotionData)


from __future__ import unicode_literals
import frappe
from frappe import throw, _

from awesome_cart.compat.erpnext.shopping_cart import get_party, \
	apply_cart_settings, set_taxes, get_address_docs, \
	get_applicable_shipping_rules, decorate_quotation_doc, get_shopping_cart_settings

def convert_quotation_to_sales_order(quotation):
	quotation.company = frappe.db.get_value("Shopping Cart Settings", None, "company")
	for fieldname in ["customer_address", "shipping_address_name"]:
		if not quotation.get(fieldname):
			throw(_("{0} is required").format(quotation.meta.get_label(fieldname)))

	quotation.flags.ignore_permissions = True
	quotation.submit()

	if quotation.lead:
		# company used to create customer accounts
		frappe.defaults.set_user_default("company", quotation.company)

	from erpnext.selling.doctype.quotation.quotation import _make_sales_order
	sales_order = frappe.get_doc(_make_sales_order(quotation.name, ignore_permissions=True))
	for item in sales_order.get("items"):
		item.reserved_warehouse = frappe.db.get_value("Item", item.item_code, "website_warehouse") or None

	sales_order.flags.ignore_permissions = True
	sales_order.insert()
	sales_order.submit()
	return sales_order

def get_customer_primary_contact(customer_name):

	# find olddest is_primary or oldest contact
	contacts = frappe.db.sql("""
		SELECT
			c.name as name,
			c.email_id as email_id,
			c.first_name as first_name,
			c.last_name as last_name
		FROM
			`tabContact` c
		LEFT JOIN
			`tabDynamic Link` dl ON dl.parent=c.name
		WHERE
			dl.link_doctype='Customer' AND
			dl.link_name=%(customer_name)s
		ORDER BY
			c.is_primary_contact DESC,
			c.creation asc
	""",
	{ "customer_name": customer_name},
	as_dict=True)

	# Check if current session user is in the contact list and return that contact info
	for contact in contacts:
		if contact.get("email_id") == frappe.session.user:
			return contact.get("name"), \
				contact.get("email_id"), \
				"%s %s" % (contact.get("first_name", ""), contact.get("last_name", ""))

	# Else, return primary customer contact if one is set
	primary_contact = frappe.db.get_value("Customer", customer_name, "customer_primary_contact")
	if primary_contact:
		return primary_contact, \
			frappe.db.get_value("Contact", primary_contact, "email_id"), \
			"%s %s" % (
				frappe.db.get_value("Contact", primary_contact, "first_name") or "",
				frappe.db.get_value("Contact", primary_contact, "last_name") or ""
			)

	# else return first contact on query result
	if len(contacts) > 0:
		return contacts[0].get("name"), \
			contacts[0].get("email_id"), \
			"%s %s" % (contacts[0].get("first_name", ""), contacts[0].get("last_name", ""))

	frappe.throw("This customer has no primary contact!. Please add one before you continue.")

def _override_get_cart_quotation(party=None):
	if not party:
		party = get_party()

	quotation = frappe.get_all("Quotation", fields=["name"], filters=
		{party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0},
		order_by="modified desc", limit=1, limit_page_length=1)

	contact_person, contact_email, contact_display = get_customer_primary_contact(party.name)

	if quotation:
		qdoc = frappe.get_doc("Quotation", quotation[0].name)
	else:
		qdoc = frappe.get_doc({
			"doctype": "Quotation",
			"naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-",
			"quotation_to": party.doctype,
			"company": frappe.db.get_value("Shopping Cart Settings", None, "company"),
			"order_type": "Shopping Cart",
			"status": "Draft",
			"contact_display": contact_display,
			"contact_person": contact_person,
			"contact_email": contact_email,
			"customer_name": party.customer_name if party else "",
			"docstatus": 0,
			"__islocal": 1,
			(party.doctype.lower()): party.name
		})

		qdoc.flags.ignore_permissions = True
		qdoc.run_method("set_missing_values")

		apply_cart_settings(party, qdoc)

	return qdoc

def get_cart_quotation(doc=None, party=None):
	if not party:
		party = get_party()

	if not doc:
		quotation = _override_get_cart_quotation(party)
		doc = quotation

	return {
		"doc": decorate_quotation_doc(doc),
		"addresses": [{"name": address.name, "display": address.display}
			for address in get_address_docs(party=party)],
		"shipping_rules": get_applicable_shipping_rules(party)
	}


from keras.layers import Activation
from keras.models import load_model, Model

from app.classifier import Classifier

from fastapi import FastAPI, File, UploadFile

app = FastAPI()
model = load_model('app/src/model/best_model.h5')
output = model.output
activation = Activation('softmax', name='activation_add')(output)
keras_model = Model(model.input, activation)
keras_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

@app.post("/predict")
def predict(file: UploadFile):
    file_data = file.file.read()
    classifier = Classifier(keras_model)
    predictions = classifier.predictions(file_data)
    classification = classifier.get_predict_classification(predictions)
    description = classifier.get_predicted_description(classification)
    return {"classification": classification, "description": description, "predictions": predictions.tolist()}


#this code is used to train a network which can simulate +-1 embedding

import tensorflow as tf
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
os.environ["CUDA_VISIBLE_DEVICES"]= '-1'

def fc(input, output_size, name, stddev = 1.0,reuse=False):
	with tf.variable_scope(name) as scope:
		w_size = [int(input.shape[-1]), output_size]
		b_size = [1, output_size]
		weights = tf.get_variable(name='t_weights', shape=w_size, initializer=tf.random_normal_initializer(mean=0, stddev=stddev))
		bias = tf.get_variable(name='t_bias', shape=b_size, initializer=tf.random_normal_initializer(mean=0, stddev=stddev))
		return tf.matmul(input, weights) + bias

def nn(x, num_nerous):
	num_nerous = 10

	#第一个子网络，共四层，激活函数均使用sigmoid
	n1_out1 = tf.nn.sigmoid(fc(x, num_nerous, 'n1_layer1'), name = 'n1_out1')
	n1_out1 -= 0.5

	n1_out2 = tf.nn.sigmoid(fc(n1_out1, num_nerous, 'n1_layer2'), name = 'n1_out2')
	n1_out2 -= 0.5

	n1_out3 = tf.nn.sigmoid(fc(n1_out2, 1, 'n1_layer3'), name = 'n1_out3')

	#第二个子网络，共四层，激活函数均使用sigmoid
	n2_out1 = tf.nn.sigmoid(fc(x, num_nerous, 'n2_layer1'), name = 'n2_out1')
	n2_out1 -= 0.5

	n2_out2 = tf.nn.sigmoid(fc(n2_out1, num_nerous, 'n2_layer2'), name = 'n2_out2')
	n2_out2 -= 0.5

	n2_out3 = tf.nn.sigmoid(fc(n2_out2, 1, 'n2_layer3'), name = 'n2_out3')
	n2_out3 -= 1

	return n1_out3 + n2_out3

def get_y(x):
	y=np.zeros([x.shape[0],1])
	len=x.shape[0]
	for i in range(len):
		if(x[i,1]<x[i,0]/2):
			y[i]=-1.0
		elif(x[i,1]>1-x[i,0]/2):
			y[i]=1.0
	return y

def tes_train(input=None, mode='train'):
	x=tf.placeholder(tf.float32, shape=[None, 2], name = 'input')#输入
	y=tf.placeholder(tf.float32, shape=[None, 1], name = 'ground_truth')#输入

	#最终预测将两个子网络的输出加起来
	pred_y = nn(x, 10)
	if(mode == 'test'):
		saver = tf.train.Saver()
		init = tf.global_variables_initializer()

		with tf.Session() as sess:
			sess.run(init)
			saver.restore(sess, os.getcwd() + '\\tes_ckpt\\tes.ckpt')

			x_trn = input#np.random.uniform(size=[1000,2])
			y_trn =get_y(x_trn)

			pred=sess.run(pred_y, feed_dict={x:x_trn, y:y_trn})
			print(pred)
		return

	#构建损失
	loss = tf.reduce_mean(tf.square(pred_y - y))

	#优化器
	optimizer = tf.train.GradientDescentOptimizer(0.01)
	train = optimizer.minimize(loss)

	saver = tf.train.Saver()

	init = tf.global_variables_initializer()
	if(mode == 'train'):
		with tf.Session() as sess:
			sess.run(init)
			saver.restore(sess, os.getcwd() + '\\tes_ckpt\\tes.ckpt')
			Loss = 0
			for step in range(6000000):
				x_trn = np.random.uniform(size=[1000,2])
				#计算真实值，:,0 is p :,1 is rand
				#0.1 0.9
				y_trn=get_y(x_trn)
				"""
				y_neg1 = np.sign(x_trn[:,1]-x_trn[:,0]/2)  -1
				y_pos1 = np.sign(x_trn[:,1]-(1-x_trn[:,0]/2))
				y_trn = y_pos1 + y_neg1
				y_trn/=2
				y_trn = y_trn[:,None]
				"""
				LOSS,_,pred = sess.run([loss, train, pred_y], feed_dict = {x:x_trn, y:y_trn})#每次迭代的数据都是全部随机的[1000,2]
				Loss += LOSS
				if(step % 1000 == 0):
					print('step%d'%step, Loss/1000)
					Loss = 0
			save_path = saver.save(sess, os.getcwd() + '\\tes_ckpt\\tes.ckpt')
	else :
		with tf.Session() as sess:
			sess.run(init)
			saver.restore(sess, os.getcwd() + '\\tes_ckpt\\tes.ckpt')

			dict={}
			for i in range(1,3):
				for j in range(1,4):
					with tf.variable_scope('n%d_layer%d'%(i,j),reuse=True):
						weights = tf.get_variable(name='t_weights')
						bias = tf.get_variable(name='t_bias')
						dict['n%d_layer%d'%(i,j)] = {}
						dict['n%d_layer%d'%(i,j)]['w'] = (sess.run(weights)).tolist()
						dict['n%d_layer%d'%(i,j)]['b'] = (sess.run(bias)).tolist()

			f=open('tes_ckpt/weights.txt', 'w')
			f.write(str(dict))
			f.close()
			print('weight saved')
			#with tf.variable_scope('n1_layer1',reuse=True) as scope:
			#	weights = tf.get_variable(name='t_weights')
			#	print(sess.run(weights))
			#print(sess.run(pred_y, feed_dict={x:input}))

#tes_train(None, '1')

test = np.random.uniform(size=[100,2])
tes_train(test,'s')
"""

y=get_y(test)
print(y, np.sum(y))
"""


''' adaline_logistic.py
    Azalea Yunus and Benji Andrews
    September 2, 2020
    CS343: Neural Networks
    Project 1: Single Layer Networks
    ADALINE (ADaptive LInear NEuron) neural network for classification and regression
'''
import numpy as np
import adaline

class AdalineLogistic(adaline.Adaline):
    ''' Single-layer neural network
    Network weights are organized [bias, wt1, wt2, wt3, ..., wtM] for a net with M input neurons.
    '''
    def activation(self, net_in):
        '''
        Applies the activation function to the net input and returns the output neuron's activation.
        It is simply the identify function for vanilla ADALINE: f(x) = x
        Parameters:
        ----------
        net_in: ndarray. Shape = [Num samples N,]
        Returns:
        ----------
        net_act. ndarray. Shape = [Num samples N,]
        '''
        # sigmoid activation function
        net_act = 1 / (1 + np.exp(-net_in))
        return net_act

    def compute_loss(self, y, net_act):
        ''' Computes the cross-entropy loss (over a single training epoch)
        Parameters:
        ----------
        y: ndarray. Shape = [Num samples N,]
            True classes corresponding to each input sample in a training epoch (coded as -1 or +1).
        net_act: ndarray. Shape = [Num samples N,]
            Output neuron's activation value (after activation function is applied)
        Returns:
        ----------
        float. The cross-entropy loss (across a single training epoch).
        '''
        cross_entropy = np.sum((-y@np.log(net_act)-(1-y)) @ np.log(1-net_act))

        return -cross_entropy

    def predict(self, features):
        '''Predicts the class of each test input sample
        Parameters:
        ----------
        features: ndarray. Shape = [Num samples N, Num features M]
            Collection of input vectors.
        Returns:
        ----------
        The predicted classes (-1 or +1) for each input feature vector. Shape = [Num samples N,]
        NOTE: Remember to apply the activation function!
        '''
        pre_pred = self.net_input(features)
        post_pred = self.activation(pre_pred)
        pred = post_pred.copy()
        pred[pred < 0.5] = 0
        pred[pred >= 0.5] = 1
        return pred

    def predict_probability(self, features):
        '''Predicts the class of each test input sample
        Parameters:
        ----------
        features: ndarray. Shape = [Num samples N, Num features M]
            Collection of input vectors.
        Returns:
        ----------
        pred: ndarray. predicted classes (-1 or +1) for each input feature vector. Shape = [Num samples N,]
        NOTE: Remember to apply the activation function!
        cert: ndarray. The precent certaint the prediction is in each class. Shape = [Num samples N,]
        '''
        pre_pred = self.net_input(features)
        post_pred = self.activation(pre_pred)
        pred = post_pred.copy()
        cert = post_pred.copy()

        pred[pred < 0.5] = 0
        pred[pred >= 0.5] = 1
        cert = np.where(pred==0,np.abs(cert-1),cert)

        return pred, cert


import json
from datetime import datetime
from apiAccess import *

def getCurTimestamp():
    timestamp = datetime.timestamp(datetime.now())
    return int(timestamp)

def formatToLocalFile(newDataFromApi):
    if not newDataFromApi["success"]:
        print("Error retrieving data from API: \nError code:"+str(newDataFromApi["error"]["code"])+"\nInfo: "+newDataFromApi["error"]["info"] )
        return None
    now = getCurTimestamp()
    listData = { "timestamp": now, "currencies" : newDataFromApi["quotes"] }
    with open('localData.json', 'w') as outfile:
        json.dump(listData, outfile, indent=4)
    return listData

def loadLocalFile():
    try:
        with open('localData.json') as f:
            data = json.load(f)
    except FileNotFoundError:
        return formatToLocalFile(getJsonResponseList("USD"))
    if(getCurTimestamp() - data["timestamp"] > 172800):
        return formatToLocalFile(getJsonResponseList("USD"))
    else:
        return data

def printJsonConvert(fromC, toC, amount):
    localData = loadLocalFile()
    if localData is None:
        exit()
    fromValue = localData["currencies"].get("USD"+fromC)
    toValue = localData["currencies"].get("USD"+toC)
    toReturn = (toValue / fromValue) * float(amount)
    print(str(amount)+" "+fromC+" = "+str(toReturn)+" "+toC)

def printJsonListAll(fromC, amount):
    localData = loadLocalFile()
    if localData is None:
        exit()
    fromValue = localData["currencies"].get("USD"+fromC)
    print(str(format(float(amount), '.2f'))+"\t\t"+fromC+":")
    for i in localData["currencies"]:
        endAmount = (localData["currencies"][i] / fromValue) * float(amount)
        if(fromC != i[3:]):
            if(len(str(format(endAmount,'.2f'))) > 6):
                print(str(format(endAmount,'.2f')), "\t"+i[3:])
            else:
                print(str(format(endAmount,'.2f')), "\t\t"+i[3:])

def printJsonListShort(fromC, amount):
    localData = loadLocalFile()
    if localData is None:
        exit()
    localShort = getJsonCurrenciesShort()
    fromValue = localData["currencies"].get("USD"+fromC)
    print(str(format(float(amount), '.2f'))+"\t\t"+fromC+":")
    for i in localData["currencies"]:
        endAmount = (localData["currencies"][i] / fromValue) * float(amount)
        if((i[3:] in localShort) and not (fromC == i[3:])):
            if(len(str(format(endAmount,'.2f'))) > 6):
                print(str(format(endAmount,'.2f')), "\t"+i[3:])
            else:
                print(str(format(endAmount,'.2f')), "\t\t"+i[3:])


import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path

from oscillation import TFNetworkModel

def main(
    code: str = "*ABC::ABi_BCi_CAi",
    n_threads: int = 4,
    t: np.ndarray = np.linspace(0, 80000, 4001),
    prows: int = 2,
    pcols: int = 2,
    save_dir=None,
    save=True,
    fmt="png",
    dpi=300,
):
    dt = t[1] - t[0]
    t_mins = t / 60
    t_hrs = t_mins / 60
    half_nt = len(t) // 2
    model = TFNetworkModel(code, seed=0)
    model.initialize_ssa(seed=0, dt=dt, nt=len(t))

    pop0s = []
    param_sets = []
    for _ in range(100000):
        pop0, params = model.ssa.draw_random_initial_and_params()
        if params[0] > 3.0:
            pop0s.append(pop0)
            param_sets.append(params)
            if len(param_sets) == n_threads:
                break

    y_t = model.run_batch_with_params(pop0s, param_sets, n_threads)
    y_t = y_t[..., 3:6]
    acorr = model.get_autocorrelation(y_t)
    nt_acorr = acorr.shape[1]
    indices, freqs, peaks = model.get_acf_minima_and_results(
        t, y_t, freqs=True, indices=True
    )

    fig1 = plt.figure(figsize=(4, 3))
    for i in range(n_threads):
        ax = fig1.add_subplot(prows, pcols, i + 1)
        ax.plot(t_hrs, y_t[i, :, 0], label="TF A", lw=0.5)
        ax.plot(t_hrs, y_t[i, :, 1], label="TF B", lw=0.5)
        ax.plot(t_hrs, y_t[i, :, 2], label="TF C", lw=0.5)
        ax.set_xlabel("Time (hours)")
        ax.set_ylabel("Copy number")
    if save:
        fpath = Path(save_dir).joinpath(f"repressilator_traces").with_suffix(f".{fmt}")
        print(f"Writing to: {fpath.resolve().absolute()}")
        fig1.savefig(str(fpath), dpi=dpi, bbox_inches="tight")

    fig2 = plt.figure(figsize=(4, 3))
    for i in range(n_threads):
        ax = fig2.add_subplot(prows, pcols, i + 1)
        ax.plot(t_hrs[:nt_acorr], acorr[i, :, 0], label="TF A", lw=0.5)
        ax.plot(t_hrs[:nt_acorr], acorr[i, :, 1], label="TF B", lw=0.5)
        ax.plot(t_hrs[:nt_acorr], acorr[i, :, 2], label="TF C", lw=0.5)

        # Point out second peak of the autocorrelation function
        ax.scatter(t_hrs[indices[i]], peaks[i], marker="x", s=50, c="gray", zorder=100)

        ax.set_xlabel("Time (hours)")
        ax.set_ylabel("Autocorrelation")

    if save:
        fpath = (
            Path(save_dir)
            .joinpath(f"repressilator_autocorr_newranges")
            .with_suffix(f".{fmt}")
        )
        print(f"Writing to: {fpath.resolve().absolute()}")
        fig2.savefig(str(fpath), dpi=dpi, bbox_inches="tight")

if __name__ == "__main__":
    # save_dir = Path("./figures/oscillation")
    save_dir = Path("./figures/oscillation/newranges")
    save_dir.mkdir(exist_ok=True)

    main(
        save_dir=save_dir,
    )


"""
Module utils
==============

Module utils for import or migrate module
"""

__all__ = [
    "ModuleUtils"
]

import importlib
import os
import shutil
import sys
from pathlib import PurePath
from typing import Union

class ModuleUtils(object):

    @classmethod
    def migrate_module(cls, src: str, dst: str, dst_name: str = None) -> None:
        """
        migrate a module from ``src`` to ``dst``, and rename it to ``dst_name``.

        :param src: the module source dir.
        :param dst: target dir.
        :param dst_name: new module name, the module name will keep if this param is None.
        :return:
        """
        src_name = os.path.basename(src)
        os.makedirs(dst, exist_ok=True)
        if dst_name is None:
            if src_name.endswith(".py"):
                dst_name = src_name[:-3]
            else:
                dst_name = src_name
        if os.path.isdir(src):
            shutil.copytree(src, PurePath(dst, dst_name).as_posix())
        else:
            shutil.copy(src, PurePath(dst, dst_name + ".py").as_posix())

    @classmethod
    def import_module(cls, name: str, path: str = None):
        """
        Import the module dynamically

        :param path: module path
        :param name: module name
        :return: module
        """
        if path is not None:
            sys.path.insert(0, os.path.abspath(path))
        try:
            module = importlib.import_module(name)
        except Exception as e:
            module = None
            print(e)
        if path is not None:
            sys.path.remove(sys.path[0])
        return module

    @classmethod
    def exists_module(cls, module: str) -> bool:
        """
        check if module is exists.

        :param module: the module to be checked.
        :return: a bool value.
        """
        try:
            exec("import " + module)
            return True
        except:
            return False

    @classmethod
    def get_name(cls, module, obj) -> Union[str, None]:
        """
        Get the name of ``obj`` in ``module``.

        :param module: the module to found ``obj``.
        :param obj: the obj need to get name.
        :return: a string value represent the name of ``obj`` or None if not found.
        """
        if module is None or obj is None:
            raise ValueError("")
        for k, v in module.__dict__.items():
            if v == obj:
                return k
        return None


'''
-*- coding: utf-8 -*-
@Time    : 2021/4/23 16:32
@Author  : Zhang Zhiyuan
@File    : Sr_system.py
@Software: PyCharm
'''
import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Setting parameters
path_root = os.path.join(os.path.dirname(__file__), '..')
path_excel_reader = os.path.join(path_root, 'results', 'crystal_system_results.xlsx')
list_bl = ['Orbl', 'Nrbl', 'Srbl', 'Prbl']
df = pd.read_excel(path_excel_reader, sheet_name='Sr', engine='openpyxl')
df['bl'] = 0
for i, row in df.iterrows():
    list_uzero = []
    for n_bl in list_bl:
        if not not row[n_bl]:
            list_uzero.append(row[n_bl])
    if not not list_uzero:
        df.loc[i, 'bl'] = np.mean(list_uzero)
df.drop(df[df['bl'] == 0].index, inplace=True)
print(len(df))
sns.set()
sns.scatterplot(x='labels_', y='rcm', hue='bl', data=df)
plt.show()


#from CRABClient.UserUtilities import config, getUsernameFromSiteDB
from CRABClient.UserUtilities import config
config = config()

config.General.requestName = 'miniAOD_BcToBsPi_v1'
config.General.workArea = 'crab_privateMC'
config.General.transferOutputs = True
config.General.transferLogs = False

config.JobType.pluginName = 'PrivateMC'
#config.JobType.disableAutomaticOutputCollection = True
config.JobType.maxMemoryMB = 2500

config.JobType.psetName = 'miniAOD_crab_fake_cfg.py'

#config.JobType.inputFiles = ['run_crab.sh', 'BPH-RunIIFall18GS-BcToBsPi_cfg.py', 'DR_step1_crab_cfg.py', 'DR_step2_crab_cfg.py', 'pu.py' , 'miniAOD_crab_fake_cfg.py', 'miniAOD_crab_cfg.py']
config.JobType.inputFiles = ['run_crab_TOT_prod_fromLHE.sh', 'LHE_crab_cfg.py', 'BPH-RunIIFall18GS-BcToBsPi_cfg.py', 'DR_step1_crab_cfg_ac.py', 'DR_step2_crab_cfg.py', 'pu.py' , 'miniAOD_crab_fake_cfg.py', 'miniAOD_crab_cfg.py']

config.JobType.scriptExe= 'run_crab_TOT_prod_fromLHE.sh'
config.JobType.numCores=1

config.Data.splitting = 'EventBased'
config.Data.unitsPerJob = 1000 #500
#config.JobType.eventsPerLumi = 50000
config.Data.totalUnits = 3500000 #200000
#config.Data.outLFNDirBase = '/store/user/%s/' % (getUsernameFromSiteDB())
config.Data.outLFNDirBase = '/store/user/moameen/'
config.Data.publication = True
config.Data.outputPrimaryDataset = 'BcToBsPi_JPsiPhi_MuMu_KK_13TeV_pythia8'
config.Data.outputDatasetTag ='BcToBsPi-MINIAODSIM'

#config.Site.storageSite = 'T2_CH_CSCS'
config.Site.storageSite = 'T3_CH_CERNBOX'


import sys
import socket

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 30001)
print("connecting to {}".format(server_address))
sock.connect(server_address)

file_name=sys.argv[1]
buf=1024

try:
    #mengirim nama file
    sock.sendto(file_name.encode(),server_address)
    #menerima isi file, sesuai request
    data = sock.recv(buf)
    print('Receive file...')
    while(data):
        #menulis isi file ke file baru
        file = open('received_' + file_name, 'wb')
        file.write(data)
        sock.settimeout(2)
        data =sock.recv(buf)
    print("File received.")
except:
    file.close()
    sock.close()


import numpy as np
import os
import cv2
import picamera
import time
import xapi_spi
import random
import datetime
import struct
import argparse
import sys
from gpiozero import LED

from collections import namedtuple

#VOC Class name
classname = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]

def image_resize(image, width = None, height = None, inter = cv2.INTER_NEAREST):
    # initialize the dimensions of the image to be resized and
    # grab the image size
    dim = None
    (h, w) = image.shape[:2]

    # if both the width and height are None, then return the
    # original image
    if width is None and height is None:
        return image

    new_ratio = float(width / height)
    old_ratio = float(w / h)

    if old_ratio > new_ratio:
        r = width / float(w)
        dim = (width, int(h * r))
    elif old_ratio < new_ratio:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        dim = (width, height)

    # resize the image
    resized = cv2.resize(image, dim, interpolation = inter)

    old_size = resized.shape[:2] # old_size is in (height, width) format

    delta_w = width - old_size[1]
    delta_h = height - old_size[0]
    top, bottom = delta_h//2, delta_h-(delta_h//2)
    left, right = delta_w//2, delta_w-(delta_w//2)

    #color = [0, 0, 0]
    color = [255, 255, 255]
    resized = cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)

    # return the resized image
    return resized

def main(imgsrc):
    k210_reset = LED(27)

    k210_reset.off()
    time.sleep(0.5)
    k210_reset.on()
    time.sleep(0.5)

    #xapispi = xapi_spi.Xapi_spi(0,0,4000000)
    xapispi = xapi_spi.Xapi_spi(0,0,60000000)
    xapispi.init()

    boxstruct = namedtuple('boxstruct',['x1','y1','x2','y2','boxclass','prob'])

    font                   = cv2.FONT_HERSHEY_PLAIN
    TopLeftCornerOfText = (10,11)
    bottomLeftCornerOfText = (10,238)
    fontScale              = 1
    fontColor              = (165,26,26)
    lineType               = 1

    boxes = []

    boardver = xapispi.spi_rd_boardver()
    print("Board Version : "+ str(boardver))
    fpgaver = xapispi.spi_rd_fpgaver()
    print("FPGA Version : "+ str(fpgaver) )

    #img = cv2.imread('data/dog.jpg')
    img = cv2.imread(imgsrc)
    print("Original image size : "+str(np.shape(img)))
    img_small = image_resize(img, width=320, height = 224)
    img_large = image_resize(img, width=1280, height = 896)
    print("Small image size : "+str(np.shape(img_small)))
    print("Large image size : "+str(np.shape(img_large)))

    #Send the small image to K210
    xapispi.spi_send_img(img_small)

    boxes = xapispi.spi_getbox()

    if len(boxes) > 0 and boxes[0] != "na":
        for box in boxes:
            x1 = box.x1
            x2 = box.x2
            y1 = box.y1
            y2 = box.y2
            boxclass = box.boxclass
            prob = box.prob
            #cv2.rectangle(img_small, (x1*4, y1*4), (x2*4, y2*4), (255,0,0), 2)
            text = "{} : {:.2f}".format(classname[boxclass[0]],prob[0])

            if prob[0] > 0.6:
                cv2.putText(img_small, text, (x1, y1-5), cv2.FONT_HERSHEY_SIMPLEX,0.5,fontColor, 1)
                cv2.rectangle(img_small, (x1, y1), (x2, y2), (255,0,0), 1)

                cv2.putText(img_large, text, (x1*4, y1*4-5), cv2.FONT_HERSHEY_SIMPLEX,0.5,fontColor, 1)
                cv2.rectangle(img_large, (x1*4, y1*4), (x2*4, y2*4), (255,0,0), 1)

    cv2.imshow('Detection : large',img_large)
    cv2.imshow('Detection : small',img_small)
    cv2.waitKey(0)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--imgsrc', type=str, help='Model definition.', default='data/dog.jpg')

    args = parser.parse_args(sys.argv[1:])

    main(args.imgsrc)


from odoo import models, fields, api, _
from odoo.exceptions import ValidationError

class ResPartner(models.Model):
    """inherit base.res_partner and add columns to allow central invoicing"""
    _inherit = 'res.partner'

    invoicing_partner_id = fields.Many2one(
        comodel_name='res.partner',
        string='Invoicing Customer',
    )
    store_ref = fields.Char(
        string='Store Code',
        help='If the customer requires specific store '
             'references on documentation',
        copy=False
    )
    billing_partner_id = fields.Many2one(
        comodel_name='res.partner',
        string='Billing Supplier',
        oldname='hq_partner_id'
    )

    store_ids = fields.One2many(
        comodel_name='res.partner',
        inverse_name='invoicing_partner_id',
        string='Stores',
        copy=False
    )

    @api.constrains('store_ref', 'invoicing_partner_id')
    def _check_store_code(self):
        """This function checks that the store code is unique within
        the account hierarchy it belongs"""
        if not self.invoicing_partner_id:
            return True
        for p in self:
            store_refs = p.invoicing_partner_id.store_ids.filtered(
                lambda r: bool(r.store_ref)).mapped('store_ref')
            if len(store_refs) != len(set(store_refs)):
                raise ValidationError('Cannot have duplicate store codes')

    @api.multi
    def get_billing_partner(self, vals, invoice=None):
        self.ensure_one()
        invoice_type = vals.get('type', invoice.type if invoice else '')
        if invoice_type.startswith('out_'):
            return self.invoicing_partner_id
        elif invoice_type.startswith('in_'):
            invoice_company = (
                self.env['res.company'].browse(vals['company_id'])
                if 'company_id' in vals else invoice.company_id)
            company_partner = invoice_company.partner_id
            return (self.billing_partner_id if
                    self.billing_partner_id != company_partner else self.id)
        return False


# -*- coding: utf-8 -*-
from setuptools import setup, find_packages

with open("requirements.txt") as f:
	install_requires = f.read().strip().split("\n")

# get version from __version__ variable in apollo/__init__.py
from apollo import __version__ as version

setup(
	name="apollo",
	version=version,
	description="Custom App for Apollo Tyres",
	author="Neil Lasrado",
	author_email="neil@switchon.io",
	packages=find_packages(),
	zip_safe=False,
	include_package_data=True,
	install_requires=install_requires
)


from collections import namedtuple
from pytest import approx, fixture, raises
from symplyphysics import (
    errors,
    units,
    Quantity,
    SI,
    convert_to,
)
from symplyphysics.core.symbols.celsius import Celsius, to_kelvin_quantity
from symplyphysics.laws.thermodynamics import inner_energy_from_temperature as inner_energy_law

# Description
## With help of calculator on https://www.calculatoratoz.com/ru/molar-internal-energy-of-ideal-gas-calculator/Calc-1705 I calculated inner energy of 1 mole (4 gram) of Helium with it's molar mass 4 gramm/mole at 20 Celsius degrees.
## It should be 3656 Joules of energy.

@fixture(name="test_args")
def test_args_fixture():
    m = Quantity(4 * units.gram)
    normal_temperature = Celsius(20)
    T = to_kelvin_quantity(normal_temperature)
    M = Quantity(4 * units.gram / units.mole)
    Args = namedtuple("Args", ["m", "T", "M"])
    return Args(m=m, T=T, M=M)

def test_basic_energy(test_args):
    result = inner_energy_law.calculate_inner_energy(test_args.m, test_args.T, test_args.M)
    assert SI.get_dimension_system().equivalent_dims(result.dimension, units.energy)
    result_pressure = convert_to(result, units.joule).evalf(2)
    assert result_pressure == approx(3656, 0.01)

def test_bad_mass(test_args):
    mb = Quantity(1 * units.coulomb)
    with raises(errors.UnitsError):
        inner_energy_law.calculate_inner_energy(mb, test_args.T, test_args.M)
    with raises(TypeError):
        inner_energy_law.calculate_inner_energy(100, test_args.T, test_args.M)

def test_bad_temperature(test_args):
    tb = Quantity(1 * units.coulomb)
    with raises(errors.UnitsError):
        inner_energy_law.calculate_inner_energy(test_args.m, tb, test_args.M)
    with raises(TypeError):
        inner_energy_law.calculate_inner_energy(test_args.m, 100, test_args.M)

def test_bad_mole_mass(test_args):
    Mb = Quantity(1 * units.coulomb)
    with raises(errors.UnitsError):
        inner_energy_law.calculate_inner_energy(test_args.m, test_args.T, Mb)
    with raises(TypeError):
        inner_energy_law.calculate_inner_energy(test_args.m, test_args.T, 100)


from gearbox.migrations import Migration

class AddTableTPService(Migration):

    database = "ordercanal"

    def up(self):
        t = self.alter_table('TPService')
        t.column('SkyTvVoucher', 'character', format="X(12)", initial="", max_width=24, label="Voucher", column_label="Voucher", position=18, order=180, help="Sky Tv Voucher")
        t.column('VoucherStatus', 'character', format="X(12)", initial="", max_width=24, label="Voucher Status", column_label="Voucher Status", position=19, order=190, help="Voucher Status Code")
        t.column('RedemFile', 'character', format="X(35)", initial="", max_width=70, label="Redemption File", column_label="Redemption File", position=20, order=200, help="Redemption File Name")
        t.column('VoucherActiveDt', 'date', format="99-99-99", initial=self.unknown, max_width=4, label="Voucher Active", column_label="Voucher Active", position=21, order=210, help="Voucher Active Date")
        t.column('VoucherExpiryDt', 'date', format="99-99-99", initial=self.unknown, max_width=4, label="Voucher Expiry", column_label="Voucher Expiry", position=22, order=220, help="Voucher Expiry Date")
        t.column('VoucherRedemDt', 'date', format="99-99-99", initial=self.unknown, max_width=4, label="Voucher Redem", column_label="Voucher Redem", position=23, order=230, help="Voucher Redem Date")
        t.column('VoucherCancelDt', 'date', format="99-99-99", initial=self.unknown, max_width=4, label="Voucher Cancel", column_label="Voucher Cancel", position=24, order=240, help="Voucher Cancel Date")
        t.index('Voucher', [['SkyTvVoucher']], area="Dyn_Index_1")
        t.index('VoucherStatus', [['VoucherStatus'], ['Operation'], ['ServType'], ['ServStatus']], area="Dyn_Index_1")
        t.alter_index('MsSeqTypeStatus', [['MsSeq'], ['Operation'], ['ServType'], ['ServStatus'], ['UpdateTS']], area="Dyn_Index_1")

    def down(self):
        t = self.alter_table('TPService')
        t.alter_index('MsSeqTypeStatus', [['MsSeq'], ['Operation'], ['ServType'], ['ServStatus']], area="Dyn_Index_1")
        t.drop_index('Voucher')
        t.drop_index('VoucherStatus')
        t.drop_column('SkyTvVoucher')
        t.drop_column('VoucherStatus')
        t.drop_column('RedemFile')
        t.drop_column('VoucherActiveDt')
        t.drop_column('VoucherExpiryDt')
        t.drop_column('VoucherRedemDt')
        t.drop_column('VoucherCancelDt')


import os

HOME_DIR = os.environ['HOME']
ALLENNLP_PATH = os.path.join(HOME_DIR, 'allennlp')
DATA_PATH = os.path.join(HOME_DIR, 'data', 'elmo4irony')
CACHE_PATH = os.path.join(DATA_PATH, 'cache')
PREPARED_DATA_PATH = os.path.join(DATA_PATH, 'prepared')
PREPROCESSED_DATA_PATH = os.path.join(DATA_PATH, 'preprocessed')
RESULTS_PATH = os.path.join(DATA_PATH, 'results')
LOG_PATH = os.path.join(RESULTS_PATH, 'log')

# EMBEDDINGS
EMBEDDINGS_DIR = os.path.join(DATA_PATH, 'word_embeddings')
ELMO_OPTIONS = os.path.join(EMBEDDINGS_DIR, 'elmo_2x4096_512_2048cnn_2xhighway_options.json')
ELMO_WEIGHTS = os.path.join(EMBEDDINGS_DIR, 'elmo_2x4096_512_2048cnn_2xhighway_weights.hdf5')

corpora_dict = {}
label_dict = {}

for corpus_name in os.listdir(PREPROCESSED_DATA_PATH):
    corpus_path = os.path.join(PREPROCESSED_DATA_PATH, corpus_name)
    if os.path.isdir(corpus_path):

        corpus_examples_dict = {
            'train': os.path.join(corpus_path, 'train.txt'),
            'dev': os.path.join(corpus_path, 'dev.txt'),
            'test': os.path.join(corpus_path, 'test.txt'),
        }

        corpus_labels_dict = {
            'train': os.path.join(corpus_path, 'train_labels.txt'),
            'dev': os.path.join(corpus_path, 'dev_labels.txt'),
            'test': os.path.join(corpus_path, 'test_labels.txt')
        }

        corpora_dict[corpus_name] = corpus_examples_dict
        label_dict[corpus_name] = corpus_labels_dict

WRITE_MODES = {'none': None,
               'file': 'FILE',
               'db': 'DATABASE',
               'both': 'BOTH'}

PAD_ID = 0
UNK_ID = 1
NUM_ID = 2
URL_ID = 3

# Specific to IEST dataset
USR_ID = 4
HASHTAG_ID = 5

PAD_TOKEN = '__PAD__'
UNK_TOKEN = '__UNK__'
NUM_TOKEN = '__NUM__'
URL_TOKEN = '__URL__'

# These should be just like the ones appearing in the input dataset (these are
# different to the originals because of preprocessing)
USR_TOKEN = '__USERNAME__'
HASHTAG_TOKEN = '__HASHTAG__'

SPECIAL_TOKENS = {
        PAD_TOKEN: PAD_ID,
        UNK_TOKEN: UNK_ID,
        NUM_TOKEN: NUM_ID,
        URL_TOKEN: URL_ID,
        USR_TOKEN: USR_ID,
}

UNK_CHAR_ID = 0
UNK_CHAR_TOKEN = 'あ'

SPECIAL_CHARS = {
    UNK_CHAR_TOKEN: UNK_CHAR_ID,
}

LABEL2ID = {'0': 0, '1': 1}

ID2LABEL = {value: key for key, value in LABEL2ID.items()}

LABELS = ['0', '1']

# DATABASE PARAMETERS
_DB_NAME = 'runs.db'

DATABASE_CONNECTION_STRING = 'sqlite:///' + os.path.join(RESULTS_PATH,
                                                         _DB_NAME)


#
# @lc app=leetcode id=852 lang=python3
#
# [852] Peak Index in a Mountain Array
#

# @lc code=start
class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        n = len(arr)
        left = 1
        right = n - 2
        while left <= right:
            mid = (left + right) // 2
            if arr[mid - 1] < arr[mid] > arr[mid + 1]:
                return mid
            if arr[mid - 1] < arr[mid]:
                left = mid + 1
            else:
                right = mid - 1

# @lc code=end


#!/usr/bin/env python3
# python3.7で動作確認済み

import ambient
import time
import json
import datetime as dt
import sys
import random
import os
import subprocess
import requests

def conv(data):
    if data[0] in {"temp", "hum"}:
        return int(data[1]) / 100
    else:
        return int(data[1])

def send_message(token,message):
    headers = {
        "Authorization": "Bearer " + token,
    }
    files = {
        "message": (None, message),
    }
    res = requests.post("https://notify-api.line.me/api/notify", headers=headers, files=files)
    print(res)

# 設定値読み込み
f = open("/home/pi/work/usb_notifier/config.json", "r")
conf = json.loads(f.read())
f.close()

data_dic = {}
data_l = []
for device in conf["devices"]:
    data_arr = []
    if "cpu_temp" == device["sensor_name"]:
        cmd = 'cat /sys/class/thermal/thermal_zone0/temp'
        data = int((subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                        shell=True).communicate()[0]).decode('utf-8').split("\n")[0])/1000.0
        data_arr.append(str(data))
        data_dic[device["sensors"][0]] = data
    else:
        filename = conf["logdir"] + "/" + device["sensor_name"] + "/" + device["sensor_name"] + "_" + dt.datetime.now().strftime("%Y-%m-%d") + ".csv"
        try:
            f = open(filename,"r")
            # ログの末尾1行をとってくる
            lines = f.readlines()[-1:][0][:-1]
            f.close

            # ログに含まれるセンサの数がconfig.jsonと同じか確認
            data_list = lines.split(",")[1].split(";")
            data_num = (len(data_list) - 1)
            if len(device["sensors"]) == data_num:
                for i in range(data_num):
                    # センサ名と数字のペアができる ex) ["temp","2657"]
                    data_pair = data_list[i].split("=")

                    if data_pair[0] in device["sensors"]:
                        # 送信すべきambientのデータ番号が存在することを確認 ex) d1~d8
                        data_l.append(device["sensors"][data_pair[0]] + ":" + str(conv(data_pair)))
                        data_dic[device["sensors"][data_pair[0]]] = conv(data_pair)
                    else:
                        break
            else:
                break
        except Exception as e:
            pass

# 送信メッセージ作成
messages = conf["title"] + "\n"
for i in range(len(data_l)):
    messages += data_l[i] + "\n"
print(messages)

# line送信処理
send_message(conf["line_token"],messages)


# https://leetcode.com/problems/find-smallest-letter-greater-than-target

from typing import List
import unittest
from dataclasses import dataclass

class Solution:
    def arraysIntersection(self, arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]:
        result = []

        def binary_search(arr: List[int], target: int) -> bool:
            low, high = 0, len(arr) - 1

            while low <= high:
                mid = (low + high) // 2
                if arr[mid] == target:
                    return True
                if arr[mid] > target:
                    high = mid - 1
                else:
                    low = mid + 1
            return False

        for num in arr1:
            if binary_search(arr2, num) and binary_search(arr3, num):
                result.append(num)
        return result

class Solution2:
    def arraysIntersection(self, arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]:
        hashmap = {}

        for num in arr1:
            if num not in hashmap:
                hashmap[num] = 1

        for num in arr2:
            if num in hashmap and hashmap[num] == 1:
                hashmap[num] = 2

        for num in arr3:
            if num in hashmap and hashmap[num] == 2:
                hashmap[num] = 3

        result = []
        for k, v in hashmap.items():
            if v == 3:
                result.append(k)
        return result

@dataclass
class TestCase:
    arr1: List[int]
    arr2: List[int]
    arr3: List[int]
    expectation: List[int]

class TestSolution(unittest.TestCase):
    def test_solution(self):
        solution = Solution()

        tests = [
            TestCase([1, 2, 3, 4, 5], [1, 2, 5, 7, 9], [1, 3, 4, 5, 8], [1, 5]),
            TestCase([197, 418, 523, 876, 1356], [501, 880, 1593, 1710, 1870], [521, 682, 1337, 1395, 1764], [])
        ]

        for t in tests:
            with self.subTest(t):
                self.assertEqual(solution.arraysIntersection(t.arr1, t.arr2, t.arr3), t.expectation)


#!/usr/bin/env python3
# # -*- coding: utf-8 -*-

"""
Python Package File
________________________________________________________________________________

Created by brightSPARK Labs
www.brightsparklabs.com
"""

# to use a consistent encoding
from codecs import open
from os import path
from subprocess import PIPE, run

# always prefer setuptools over distutils
from setuptools import find_packages, setup

# ------------------------------------------------------------------------------
# UTILITY FUNCTIONS
# ------------------------------------------------------------------------------

def get_version():
    try:
        process = run(["git", "describe", "--dirty", "--always"], stdout=PIPE)
        line = process.stdout.strip().decode("utf-8")
        return line
    except Exception as ex:
        return "UNKNOWN"

# ------------------------------------------------------------------------------
# SETUP DEFINITION
# ------------------------------------------------------------------------------

here = path.dirname(path.realpath(__file__))
# get the long description from the README file
with open(path.join(here, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

# get the license
with open(path.join(here, "LICENSE"), encoding="utf-8") as f:
    license = f.read()

setup(
    name="appcli",
    version=get_version(),
    description=long_description,
    license=license,
    author="brightSPARK Labs",
    author_email="enquire@brightsparklabs.com",
    url="www.brightsparklabs.com",
    packages=find_packages(exclude=["contrib", "docs", "tests"]),
    include_package_data=True,
    install_requires=[
        "click==7.1.2",
        "coloredlogs==14.0",
        "GitPython==3.1.12",
        "jinja2==2.11.2",
        "python-dotenv==0.14.0",
        "python-keycloak==0.22.0",
        "pycryptodome==3.10.1",
        "ruamel-yaml==0.16.10",
        "tabulate==0.8.7",
        "boto3==1.16.62",
        "cronex==0.1.3.1",
        "dataclasses-json==0.5.2",
    ],
    extras_require={"dev": ["black", "flake8", "isort", "pytest"]},
)


import abc
from typing import Optional
import warnings

import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModel, AutoConfig, AutoModelForCausalLM, AutoModelForSeq2SeqLM
from fastchat.serve.modeling_gpt_neox_hpu import GPTNeoXForCausalLM

from fastchat.conversation import conv_templates, get_default_conv_template, SeparatorStyle
from fastchat.serve.compression import compress_module
from fastchat.serve.monkey_patch_non_inplace import replace_llama_attn_with_non_inplace_operations
from fastchat.serve.serve_chatglm import chatglm_generate_stream
import time
import numpy as np
import os

tps_message = ''
token_count = 0

def raise_warning_for_old_weights(model_path, model):
    if "vicuna" in model_path.lower():
        try:
            is_vicuna = isinstance(model, LlamaForCausalLM)
        except Exception:
            is_vicuna = isinstance(model, LLamaForCausalLM)
        if is_vicuna and model.model.vocab_size > 32000:
            warnings.warn(
                "\nYou are probably using the old Vicuna-v0 model, "
                "which will generate unexpected results with the "
                "current fschat.\nYou can try one of the following methods:\n"
                "1. Upgrade your weights to the new Vicuna-v1.1: https://github.com/lm-sys/FastChat#vicuna-weights.\n"
                "2. Use the old conversation template by `python3 -m fastchat.serve.cli --model-path /path/to/vicuna-v0 --conv-template conv_one_shot`\n"
                "3. Downgrade fschat to fschat==0.1.10 (Not recommonded).\n")

def get_gpu_memory(max_gpus=None):
    gpu_memory = []
    num_gpus = torch.cuda.device_count() if max_gpus is None else min(max_gpus, torch.cuda.device_count())

    for gpu_id in range(num_gpus):
        with torch.cuda.device(gpu_id):
            device = torch.cuda.current_device()
            gpu_properties = torch.cuda.get_device_properties(device)
            total_memory = gpu_properties.total_memory / (1024**3)
            allocated_memory = torch.cuda.memory_allocated() / (1024**3)
            available_memory = total_memory - allocated_memory
            gpu_memory.append(available_memory)
    return gpu_memory

def compute_skip_echo_len(model_name, conv, prompt):
    model_name = model_name.lower()
    if "chatglm" in model_name:
        skip_echo_len = len(conv.messages[-2][1]) + 1
    elif "dolly" in model_name:
        special_toks = ["### Instruction:", "### Response:", "### End"]
        prompt_tmp = prompt
        for tok in special_toks:
            prompt_tmp = prompt_tmp.replace(tok, "")
        skip_echo_len = len(prompt_tmp)
    else:
        skip_echo_len = len(prompt) + 1 - prompt.count("</s>") * 3
    return skip_echo_len

def load_model(model_path, device, num_gpus, max_gpu_memory=None,
               load_8bit=False, debug=False, use_graphs=False):
    if device == "cpu":
        kwargs = {}
    elif device == "cuda":
        kwargs = {"torch_dtype": torch.bfloat16}
        if num_gpus == "auto":
            kwargs["device_map"] = "auto"
        else:
            num_gpus = int(num_gpus)
            if num_gpus != 1:
                kwargs["device_map"] = "auto"
                if max_gpu_memory is None:
                    available_gpu_memory = get_gpu_memory(num_gpus)
                    kwargs["max_memory"] = {i: str(int(available_gpu_memory[i] * 0.85)) +
                        "GiB" for i in range(num_gpus)}
                else:
                    kwargs["max_memory"] = {i: max_gpu_memory for i in range(num_gpus)}
        print("init_kwargs", kwargs)
    elif device == "mps":
        kwargs = {"torch_dtype": torch.float16}
        # Avoid bugs in mps backend by not using in-place operations.
        replace_llama_attn_with_non_inplace_operations()
    elif device == "hpu":
        import habana_frameworks.torch.core as htcore
        import habana_frameworks.torch.hpu.graphs as htgraphs
        import habana_frameworks.torch as ht
        kwargs = {"torch_dtype": torch.bfloat16}
    else:
        raise ValueError(f"Invalid device: {device}")

    config = AutoConfig.from_pretrained(model_path)
    if "chatglm" in model_path:
        tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
        model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().to(device)
    elif "dolly" in model_path:
        kwargs.update({"torch_dtype": torch.bfloat16})
        tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=True)
        # 50277 means "### End"
        tokenizer.eos_token_id = 50277
        model = GPTNeoXForCausalLM.from_pretrained(model_path, low_cpu_mem_usage=True, **kwargs)
        model.config.pad_token_id = model.config.eos_token_id
    elif config.model_type == "llama":
        tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
        model = LlamaForCausalLM.from_pretrained(model_path, low_cpu_mem_usage=True, **kwargs)
    else:
        tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
        model = AutoModelForCausalLM.from_pretrained(model_path,
            low_cpu_mem_usage=True, **kwargs)
        raise_warning_for_old_weights(model_path, model)

    if load_8bit:
        compress_module(model, device)

    if (device == "cuda" and num_gpus == 1) or device == "mps":
        model.to(device)
    elif device == "hpu":
        if config.model_type not in ['llama', 'gpt_neox']:
            raise NotImplementedError
        model.to(device)
        model = model.eval()
        if use_graphs:
            model = ht.hpu.wrap_in_hpu_graph(model)

    if debug:
        print(model)

    return model, tokenizer

def get_device(model):
    if hasattr(model, 'device'):
        return model.device
    if hasattr(model, 'module'):
        return model.module.device
    assert False, 'Cannot extract device!'
    return None

def prepare_input(model, max_length, static_shapes=False, **model_args):
    device = get_device(model)
    if static_shapes:
        input_ids = model_args['input_ids']
        attention_mask = model_args['attention_mask']
        cur_length = input_ids.shape[-1]
        padding_length = max_length - cur_length
        model_args['token_idx'] = torch.tensor(cur_length)
        model_args['input_ids'] = F.pad(input_ids, (0, padding_length), value=model.config.pad_token_id)
        model_args['attention_mask'] = F.pad(attention_mask, (0, padding_length), value=0)
    if 'token_idx' in model_args:
        model_args['token_idx'] = model_args['token_idx'].to(device)
    model_args['input_ids'] = model_args['input_ids'].to(device)
    model_args['attention_mask'] = model_args['attention_mask'].to(device)
    model_args['use_cache'] = model_args.get('use_cache')
    return model_args

@torch.inference_mode()
def generate_stream(model, tokenizer, params, device,
                    context_len=2048, stream_interval=2,
                    use_cache=False, static_shapes=False,
                    output_tps=False, seed=1):
    prompt = params["prompt"]
    l_prompt = len(prompt)
    temperature = float(params.get("temperature", 1.0))
    max_new_tokens = int(params.get("max_new_tokens", 256))
    stop_str = params.get("stop", None)
    if stop_str == tokenizer.eos_token:
        stop_str = None
    max_length=context_len
    model_args = tokenizer(prompt, return_tensors="pt", max_length=max_length, truncation=True)
    model_args = model_args.to(device)
    input_ids = model_args.input_ids
    output_ids = input_ids.tolist()[0]

    global token_count
    token_count = len(output_ids)

    eos_generated = torch.zeros((input_ids.shape[-2],), dtype=torch.bool, device=input_ids.device)

    model_args = prepare_input(model, max_length=max_length, static_shapes=static_shapes, **model_args)
    model_args['use_cache'] = use_cache

    if device == 'hpu':
        import habana_frameworks.torch.core as htcore
        import habana_frameworks.torch.hpu.random as htrandom
        htrandom.manual_seed(seed)
        htcore.mark_step()
        g = None
    else:
        g = torch.Generator(device=device).manual_seed(seed)

    t1 = time.time()
    n = 0

    for i in range(max_new_tokens):
        n+=1
        out = model(**model_args)
        logits = out.logits
        past_key_values = out.past_key_values
        if static_shapes and (i == 0 or not use_cache):
            last_token_logits = logits[:,model_args['token_idx'] - 1,:]
        else:
            last_token_logits = logits[:,-1,:]

        if device == "mps":
            # Switch to CPU by avoiding some bugs in mps backend.
            last_token_logits = last_token_logits.float().to("cpu")

        if temperature < 1e-4:
            token = int(torch.argmax(last_token_logits))
        else:
            probs = torch.softmax(last_token_logits / temperature, dim=-1)
            token = int(torch.multinomial(probs, num_samples=1, generator=g))

        output_ids.append(token)

        if token == tokenizer.eos_token_id:
            stopped = True
        else:
            stopped = False

        if i % stream_interval == 0 or i == max_new_tokens - 1 or stopped:
            output = tokenizer.decode(output_ids, skip_special_tokens=True)
            if stop_str:
                pos = output.rfind(stop_str, l_prompt)
                if pos != -1:
                    output = output[:pos]
                    stopped = True
            yield output

        if stopped:
            break

        next_tokens = torch.logical_not(eos_generated) * token + eos_generated * model.config.pad_token_id
        eos_generated.logical_or_(next_tokens.eq(model.config.eos_token_id))
        next_tokens = next_tokens.unsqueeze(-1)

        # set input_ids for next forward pass
        if use_cache:
            model_args['input_ids'] = next_tokens
            model_args['past_key_values'] = past_key_values
        elif static_shapes:
            model_args['input_ids'].index_copy_(1, model_args['token_idx'], next_tokens)
        else:
            model_args['input_ids'] = torch.cat([model_args['input_ids'], next_tokens], dim=-1)

        # set attention_mask and token_idx
        if static_shapes:
            model_args['attention_mask'].index_fill_(1, model_args['token_idx'], 1)
            model_args['token_idx'].add_(1)
        else:
            model_args['attention_mask'] = F.pad(model_args['attention_mask'], (0, 1), value=1)

        if device == 'hpu':
            htcore.mark_step()

    t2 = time.time()
    if output_tps:
        total_time = round(t2-t1, 3)
        global tps_message
        tps_message = '-------------------------------------------\nTime: ' + str(total_time) + "\tTokens: " + str(n) + '\tTPS: ' + str(round(n/total_time, 2)) + '\n-------------------------------------------'

    token_count += n

    del past_key_values, model_args, out, logits, last_token_logits

class ChatIO(abc.ABC):
    @abc.abstractmethod
    def prompt_for_input(self, role: str) -> str:
        """Prompt for input from a role."""

    @abc.abstractmethod
    def prompt_for_output(self, role: str):
        """Prompt for output from a role."""

    @abc.abstractmethod
    def stream_output(self, output_stream, skip_echo_len: int):
        """Stream output."""

def chat_loop(model_path: str, device: str, num_gpus: str,
              max_gpu_memory: str, load_8bit: bool,
              conv_template: Optional[str], temperature: float,
              max_new_tokens: int, chatio: ChatIO,
              debug: bool, use_graphs: bool, use_cache: bool,
              static_shapes: bool, context_len: int, output_tps: bool,
              seed: int):
    # Model
    model, tokenizer = load_model(model_path, device,
        num_gpus, max_gpu_memory, load_8bit, debug, use_graphs)
    is_chatglm = "chatglm" in str(type(model)).lower()

    # Chat
    if conv_template:
        conv = conv_templates[conv_template].copy()
    else:
        conv = get_default_conv_template(model_path).copy()

    while True:
        try:
            inp = chatio.prompt_for_input(conv.roles[0])
        except EOFError:
            inp = ""
        if not inp:
            print("exit...")
            break

        if inp == 'clear':
            conv = conv_templates[conv_template].copy() if conv_template else get_default_conv_template(model_path).copy()
            os.system('cls' if os.name == 'nt' else 'clear')
            continue

        conv.append_message(conv.roles[0], inp)
        conv.append_message(conv.roles[1], None)

        if is_chatglm:
            prompt = conv.messages[conv.offset:]
            generate_stream_func = chatglm_generate_stream
        else:
            generate_stream_func = generate_stream
            prompt = conv.get_prompt()

        skip_echo_len = compute_skip_echo_len(model_path, conv, prompt)

        params = {
            "model": model_path,
            "prompt": prompt,
            "temperature": temperature,
            "max_new_tokens": max_new_tokens,
            "stop": conv.sep if conv.sep_style == SeparatorStyle.SINGLE else conv.sep2,
        }

        chatio.prompt_for_output(conv.roles[1])
        output_stream = generate_stream_func(model, tokenizer, params, device,
                                             use_cache=use_cache, static_shapes=static_shapes,
                                             context_len=context_len, output_tps=output_tps,
                                             seed=seed)
        outputs = chatio.stream_output(output_stream, skip_echo_len)
        if output_tps:
            print(tps_message)
        # NOTE: strip is important to align with the training data.
        conv.messages[-1][-1] = outputs.strip()

        if debug:
            print("\n", {"prompt": prompt, "outputs": outputs}, "\n")
            print('Token count: ' + str(token_count))

        if token_count + max_new_tokens > context_len:
            print('\n\nThe session history has been cleared because the maximum context length was reached\n\n')
            if conv_template:
                conv = conv_templates[conv_template].copy()
            else:
                conv = get_default_conv_template(model_path).copy()