""" Functions for executing protocols targeting v1
"""
from . import execute_v3
from opentrons.protocols.types import JsonProtocol

def execute_protocol(protocol: JsonProtocol):
    if protocol.schema_version == 3:
        ins = execute_v3.load_pipettes(protocol.contents)
        lw = execute_v3.load_labware(protocol.contents)
        execute_v3.dispatch_commands(protocol.contents, ins, lw)

    return {
        'pipettes': ins,
        'labware': lw
    }


"""
URL configuration for illusion project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from polls.views import detail

urlpatterns = [
    path('', include('homepage.urls', namespace='homepage')),
    path('admin/', admin.site.urls),
    path('tourdates/', include('tourdates.urls', namespace ='tourdates')),
    path('homepage/', include('homepage.urls', namespace='homepage')),
    path('contact/', include('contact.urls', namespace='contact')),
    path('polls/', include('polls.urls', namespace='polls')),
    path('polls/<int:question_id>/', detail, name='detail'),
    path('user_auth/', include(("django.contrib.auth.urls", 'registration'))),
    path('user_path/', include("user_auth.urls")),

]


"""
Add oauth2_token table.

Revision ID: f36f9a0aadf4
Revises: 7000bb8673c5
Create Date: 2019-05-15 13:26:41.905098

"""
import sqlalchemy as sa
from alembic import op

revision = "f36f9a0aadf4"
down_revision = "7000bb8673c5"

def upgrade():
    op.create_table(
        "oauth2_token",
        sa.Column("id", sa.Integer(), autoincrement=True, primary_key=True),
        sa.Column("user_id", sa.UnicodeText(), nullable=False),
        sa.Column(
            "consumer_key",
            sa.String(),
            sa.ForeignKey("application_instances.consumer_key", ondelete="cascade"),
            nullable=False,
        ),
        sa.Column("access_token", sa.UnicodeText(), nullable=False),
        sa.Column("refresh_token", sa.UnicodeText()),
        sa.Column("expires_in", sa.Integer()),
        sa.Column(
            "received_at", sa.DateTime(), server_default=sa.func.now(), nullable=False
        ),
        sa.UniqueConstraint(
            "user_id", "consumer_key", name="uq__oauth2_token__user_id"
        ),
    )

def downgrade():
    op.drop_table("oauth2_token")


from django.contrib import admin
from django.contrib.auth import get_user_model

from users.models import Subscription

User = get_user_model()

class SubscriptionAdmin(admin.ModelAdmin):
    list_display = (
        "author",
        "subscriber",
    )
    list_filter = (
        "author",
        "subscriber",
    )

    class Meta:
        ordering = ["-id"]

class UserAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "username",
        "email",
    )
    list_filter = (
        "username",
        "email",
    )

admin.site.register(User, UserAdmin)
admin.site.register(Subscription, SubscriptionAdmin)


from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
from .models import PointOfSale
from .forms import PointOfSaleForm
from devices.models import Device
from attendance_tablets.models import AttendanceTablet
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

@login_required
def point_of_sale_index_view(request):
    queryset = PointOfSale.objects.all()
    context = {
        'object_list': queryset
    }
    return render(request, "point_of_sales/index.html", context)
@login_required
def point_of_sale_show_view(request, id=id):
    obj = get_object_or_404(PointOfSale, id=id)
    devices = obj.device_set.all()
    attendance_tablets = obj.attendancetablet_set.all()
    context = {
        'object': obj,
        'devices': devices,
        'attendance_tablets': attendance_tablets,
    }
    return render(request, "point_of_sales/show.html", context)
@login_required
def point_of_sale_create_view(request):
    form = PointOfSaleForm(request.POST or None)
    if form.is_valid():
        fcc_form = form.save(commit=True)
        return redirect(reverse('store-show', kwargs={'id': fcc_form.store.id}))
    context = {
        'form': form
    }
    return render(request, "point_of_sales/create.html", context)
@login_required
def point_of_sale_update_view(request, id=id):
    obj = get_object_or_404(PointOfSale, id=id)
    form = PointOfSaleForm(request.POST or None, instance=obj)
    if form.is_valid():
        fcc_form = form.save(commit=True)
        return redirect(reverse('store-show', kwargs={'id': fcc_form.store.id}))
    context = {
        'form': form
    }
    return render(request, "point_of_sales/create.html", context)
@login_required
def point_of_sale_delete_view(request, id):
    obj = get_object_or_404(PointOfSale, id=id)
    if request.method == "POST":
        obj.delete()
        return redirect(reverse('store-show', kwargs={'id': obj.store.id}))
    context = {
        "object": obj
    }
    return render(request, "point_of_sales/delete.html", context)


from flask import Flask, render_template, jsonify

app = Flask(__name__)

BOSSES = [
    {
        'id': 1,
        'Title': 'Owl',
        'Location': 'Hirata',
        'Difficulty': 'Hard'
    },
    {
        'id': 2,
        'Title': 'Geni',
        'Location': 'Ashina Castle',
        'Difficulty': 'Mid'
    },
    {
        'id': 3,
        'Title': 'Ishhin',
        'Location': 'Haunted Place',
        'Difficulty': 'Very Hard'
    },
    {
        'id': 4,
        'Title': 'Monk',
        'Location': 'Deep Ashina',
        'Difficulty': 'Low-Mid'
    }
]

@app.route("/")
def hello_world():
    return render_template('home.html', bosses=BOSSES)

@app.route("/api/bosses")
def list_bosses():
    return jsonify(BOSSES)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)


import argparse
import os
import sys

import tensorflow as tf

from gan_deep_nn import model
from common.experiment import Experiment, load_checkpoint
from common.training_loop import TrainingLoopParams, training_loop
from datasets.mnist import mnist_dataset

def print_graph(session, model, step):
    """
    A helper function for printing key training characteristics.
    """
    real, fake = session.run([model.average_probability_real, model.average_probability_fake])
    print("Step %d; real = %f, fake = %f" % (step, real, fake))

def make_dataset(args):
    train_dataset = mnist_dataset(args.dataset_dir, train=True)
    test_dataset = mnist_dataset(args.dataset_dir, train=False)
    dataset = train_dataset.concatenate(test_dataset)
    dataset = dataset.map(lambda image, label: image) # Only images.
    dataset = dataset.map(lambda image: tf.reshape(image, [28, 28, 1]))
    dataset = dataset.repeat()
    dataset = dataset.batch(args.batch_size)
    return dataset.make_one_shot_iterator()

def train(session, global_step, model_ops, args):
    print_graph(session, model_ops, global_step)
    # First, we run one step of discriminator training.
    for _ in range(args.discriminator_steps):
        session.run(model_ops.discriminator_train)

    # Then we run one step of generator training.
    for _ in range(args.generator_steps):
        session.run(model_ops.generator_train)

def main(args):
    """
    The main function to train the model.
    """
    parser = argparse.ArgumentParser(description="Train the gan-normal model.")
    parser.add_argument("--dataset_dir", required=True, help="The path to the MNIST dataset files. If doesn't exist, the dataset will be downloaded")
    parser.add_argument("--batch_size", type=int, default=32, help="The size of the minibatch")
    parser.add_argument("--d_learning_rate", type=float, default=0.005, help="The discriminator learning rate")
    parser.add_argument("--g_learning_rate", type=float, default=0.005, help="The generator learning rate")
    parser.add_argument("--d_l2_reg", type=float, default=0., help="The discriminator L2 regularization parameter")
    parser.add_argument("--g_l2_reg", type=float, default=0., help="The generator L2 regularization parameter")
    parser.add_argument("--dropout", type=float, default=0.5, help="The dropout rate to use in the descriminator")
    parser.add_argument("--discriminator_steps", type=int, default=1, help="The number of steps to train the descriminator on each iteration")
    parser.add_argument("--generator_steps", type=int, default=1, help="The number of steps to train the generator on each iteration")
    parser.add_argument("--generator_features", default=[], action="append", type=int, help="The number of features in generators hidden layers")
    parser.add_argument("--discriminator_features", default=[], action="append", type=int, help="The number of features in discriminators hidden layers")
    parser.add_argument("--latent_space_size", default=128, type=int, help="The number of features in generator input")
    parser.add_argument("--smooth_labels", default=False, action="store_true", help="Whether to use smooth or sharp labels")
    Experiment.add_arguments(parser)
    TrainingLoopParams.add_arguments(parser)
    args = parser.parse_args(args)

    experiment = Experiment.from_args(args)
    hparams = experiment.load_hparams(model.ModelParams, args)

    dataset = make_dataset(args)
    model_ops = model.GanModel(hparams, dataset, model.TrainingParams(args, training=True))

    training_loop(TrainingLoopParams(args), experiment, model_ops.summaries,
        lambda session, global_step: train(session, global_step, model_ops, args),
        checkpoint=load_checkpoint(args))

if __name__ == "__main__":
    main(sys.argv[1:])


import csv
import numpy as np
from tensorflow import keras
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

# Define constants
TEST_DATA_PATH = "/Users/jaggehns/Desktop/image-classification-monolith/src/fashion-mnist_test.csv"
NUM_CLASSES = 10  # Number of clothing classes
IMAGE_SIZE = 28  # Size of the images

clothing_types = [
    "T-shirt/top",
    "Trouser",
    "Pullover",
    "Dress",
    "Coat",
    "Sandal",
    "Shirt",
    "Sneaker",
    "Bag",
    "Ankle boot"
]

# Load test data from CSV file
def load_test_data():
    x_test = []
    y_test = []

    with open(TEST_DATA_PATH, "r") as file:
        reader = csv.reader(file)
        next(reader)  # Skip the header row
        for row in reader:
            label = int(row[0])
            pixels = np.array(row[1:], dtype=np.float32) / 255.0
            image = pixels.reshape((IMAGE_SIZE, IMAGE_SIZE, 1))
            x_test.append(image)
            y_test.append(label)

    x_test = np.array(x_test)
    y_test = np.array(y_test)

    return x_test, y_test

# Load the test data
x_test, y_test = load_test_data()

# Load the pre-trained model
model = keras.models.load_model("/Users/jaggehns/Desktop/image-classification-monolith/src/fashion_mnist_model.h5")

# Perform prediction on test data
predictions = model.predict(x_test)

# Get the predicted labels
predicted_labels = np.argmax(predictions, axis=1)

# Compute accuracy
accuracy = accuracy_score(y_test, predicted_labels)
print(f"Accuracy: {accuracy}")

# Compute confusion matrix
confusion_mat = confusion_matrix(y_test, predicted_labels)
print("Confusion Matrix:")
print(confusion_mat)

# Generate classification report
classification_rep = classification_report(y_test, predicted_labels, target_names=clothing_types)
print("Classification Report:")
print(classification_rep)

# Print the predicted labels
for i in range(len(x_test)):
    predicted_label = predicted_labels[i]
    actual_label = y_test[i]
    predicted_clothing_type = clothing_types[predicted_label]
    actual_clothing_type = clothing_types[actual_label]
    # print(f"Image {i+1}: Predicted: {predicted_clothing_type}, Actual: {actual_clothing_type}")


# -*- coding: utf-8 -*-
import lmdb
import pickle
env_db = lmdb.open('./test_DFDC_lmdb')
# env_db = lmdb.open("./trainC")

txn = env_db.begin()
img = txn.get('./test_DFDC/aszdwxqtnb/frame_0.png'.encode('ascii'))
# get函数通过键值查询数据,如果要查询的键值没有对应数据，则输出None
img = pickle.loads(img)

env_db.close()


# 💡 다리 만들기 📚 https://www.acmicpc.net/problem/2146
from collections import deque

n = int(input())  # 행과 열의 크기를 입력받음
a = [list(map(int, input().split())) for _ in range(n)]  # 미로 정보 입력

g = [[0] * n for _ in range(n)]  # 그룹 정보를 저장할 배열 초기화
d = [[0] * n for _ in range(n)]  # 거리 정보를 저장할 배열 초기화
cnt = 0  # 그룹 번호 초기화

dx = [0, 0, 1, -1]  # 이동 방향을 나타내는 배열
dy = [1, -1, 0, 0]

# DFS를 사용하여 미로를 그룹화하고 그룹 정보를 저장
for i in range(n):
    for j in range(n):
        if a[i][j] == 1 and g[i][j] == 0:
            cnt += 1
            g[i][j] = cnt
            q = deque()
            q.append((i, j))
            while q:
                x, y = q.popleft()
                for k in range(4):
                    nx, ny = x + dx[k], y + dy[k]
                    if 0 <= nx < n and 0 <= ny < n:
                        if a[nx][ny] == 1 and g[nx][ny] == 0:
                            g[nx][ny] = cnt
                            q.append((nx, ny))

ans = -1  # 정답 초기화

# 각 그룹에서 다른 그룹까지의 최단 거리를 계산
for l in range(1, cnt + 1):
    q = deque()
    for i in range(n):
        for j in range(n):
            d[i][j] = -1
            if g[i][j] == l:
                q.append((i, j))
                d[i][j] = 0
    while q:
        x, y = q.popleft()
        for k in range(4):
            nx, ny = x + dx[k], y + dy[k]
            if 0 <= nx < n and 0 <= ny < n:
                if d[nx][ny] == -1:
                    d[nx][ny] = d[x][y] + 1
                    q.append((nx, ny))
    for i in range(n):
        for j in range(n):
            if a[i][j] == 1 and g[i][j] != l:
                if ans == -1 or d[i][j] - 1 < ans:
                    ans = d[i][j] - 1

print(ans)  # 정답 출력


import copy
import random

import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from tqdm import tqdm

from .stylometry import compute_vector, word_unigram_tf
from .transform import (build_dummy_dict, choose_applicable_transformation,
                        generate_data, get_all_applicable_transformations,
                        get_patterns, get_transformation_id,
                        run_transformation)

def compute_dataset(corpus, actions_n, states_n, alpha, gamma, epochs_basis=50,
                    extract_max_length=150):
    """ Compute a training dataset for the SnippetModel using Q-Learning.

    The process is the following.
    For each index from 0 to num of patterns:
    - select a random extract
    - remove this extract from the corpus
    - truncate the extract, if it is too long
    - update the epochs (higher for more complex patterns)
    - call _optimal_transformation
    - generate a fake extract and add it to the dataset

    Parameters
    ----------
    corpus: list
        The corpus of a repository
    actions_n: int
        The number of actions in the Q-table
    states_n: int
        The number of states in the Q-table
    alpha: float
        The alpha parameter in the reward function
    gamma: float
        The gamma parameter in the reward function
    epochs_basis: int, optional
        The base number of epochs (default `50`)
    extract_max_length: int, optional
        The maximum length of extracts for being processed (default `150`)

    Returns
    -------
    list
        List of dictionaries. Each dictionary is a modified pattern, that
        contains the transformed pattern itself, together with a key-value
        couple.
    """
    # The word unigram dict of a corpus, containing the occurences of
    # individual words
    corpus_word_unigram = word_unigram_tf('\n'.join(corpus))
    args = {
        'actions_n': actions_n,
        'states_n': states_n,
        'alpha': alpha,
        'gamma': gamma,
        'epochs': 1,
        'corpus_word_unigram': corpus_word_unigram
    }

    # Number of patterns
    all_patterns = get_patterns()

    # The number of extracts must be greater (or equal) than the number of
    # patterns, otherwise we don't have enough data for the Q-Learning
    if len(all_patterns) > len(corpus):
        raise ValueError('There are too many patterns for this corpus.')

    dataset = []

    # Apply Q-learning for each pattern
    for pattern_index in tqdm(range(len(all_patterns))):
        # Select a random extract and remove it from the corpus
        reference_extract = corpus.pop(random.randrange(len(corpus)))
        # Cut extracts too long
        reference_extract = reference_extract[:extract_max_length]

        # Increase epochs for more complex patterns
        epochs = int(epochs_basis * (1 + (pattern_index / len(all_patterns))))
        # Update epochs in args
        args['epochs'] = epochs

        # Compute the optimal modifications to the basic patterns
        final_transformation, modification_dict = _optimal_transformation(
            reference_extract, all_patterns[pattern_index], args)

        # Generate the dataset, with optimal transformations
        for i in range(epochs):
            dataset += generate_data(all_patterns[pattern_index],
                                     modification_dict)
    return dataset

def _optimal_transformation(reference_extract, reference_pattern, args):
    """ Compute the Q-learning algorithm.

    Parameters
    ----------
    reference_extract: string
        An extract from the corpus, where we will insert the pattern
    reference_pattern: dict
        The pattern used for the transformations
    arguments: dict
        Parameters to be used for the Q-Learning

    Returns
    -------
    string
        The pattern after a certain number of transformations
    dict
        The dictionary containing the values used to replace the keywords in
        the pattern
    """
    # Initialize the Q-table
    Q = np.zeros([args['states_n'], args['actions_n']])
    # Compute the stylometry of the extract
    reference_stylometry = compute_vector(reference_extract,
                                          args['corpus_word_unigram'])

    # List of parameters used for training
    parameters = {
        'reference_extract': reference_extract,
        'corpus_unigram': args['corpus_word_unigram'],
        'alpha': args['alpha'],
        'gamma': args['gamma']
    }

    for epoch in range(args['epochs']):
        # Iterate each state, according to the Q-learning algorithm
        for i in range(args['states_n'] - 1):
            # Get the scores
            Q_i = _choose_action(reference_pattern,
                                 reference_stylometry,
                                 Q[i],
                                 Q[i + 1],
                                 parameters)
            # Update the Q-table with this action score
            Q[i] = Q_i

    # Normalization: not necessary but easier to interpret if we look at
    # the Q-table
    normalized_Q = Q / np.max(Q)

    #
    # Generate a new clean dummy dict
    dummy_dict = build_dummy_dict(reference_pattern)
    # for each row in the Q-table (i.e., a state)
    for state in normalized_Q:
        # Execute the action producing the maximum reward
        # Passing dummy_dict as argument, ensure that it will change due to
        # shallow copy
        result = run_transformation(np.argmax(state),
                                    reference_pattern,
                                    dummy_dict)

    # Return information on how to modify the pattern
    # i.e., return the modified pattern and its dictionary of values
    return result, dummy_dict

def _choose_action(reference_pattern, reference_stylometry,
                   Q_i, Q_next_step, args):
    """ Choose the most optimized transformations according to the Q-values.

    Parameters
    ----------
    reference_pattern: dict
        The pattern under transformation
    reference_stylometry: `numpy.array`
        The stylometry of the original extract
    Q_i: `numpy.array`
        The Q-values of the current state
    Q_next_step: `numpy.array`
        The Q-values of the next state
    args: dict
        Parameters to be used for the reward functions

    Returns
    -------
    `numpy.array`
        The updated Q-values
    """
    # Find an applicable transformation
    transformation = choose_applicable_transformation(reference_pattern)

    # Build dummy dict
    dummy_dict = build_dummy_dict(reference_pattern)
    # Run the transformation
    result = transformation(reference_pattern, dummy_dict)
    # The tranformation may change the dummy_dict. Since there is a shallow
    # copy at invocation time, the changes will be reflected in the actual
    # instance of the dummy_dict

    # Compute reward of the transformation
    reward = _compute_reward(result, reference_stylometry, args)

    # Calculate the possible next maximum reward
    next_max_reward = _max_reward_next_step(
        reference_pattern, dummy_dict, reference_stylometry, Q_next_step, args)

    # Update Q-value for this transformation
    # Get the index of the transformation
    # idx = index of the transformation chosen (eg: identity has idx=1)
    idx = get_transformation_id(transformation)
    if idx == -1:
        raise IndexError('Transformation not found')
    # Update the Q_i array
    Q_i[idx] = (1 - args['alpha']) * Q_i[0] + args['alpha'] * (
        reward + args['gamma'] + next_max_reward)

    return Q_i

def _max_reward_next_step(pattern, dummy_dict, reference_stylometry,
                          Q_next_step, args):
    """ Compute the maximum reward of all potential next step actions.

    After finding all the transformations applicable to the current pattern, we
    run each of them using the same dummy dict. For each transformed pattern
    produced by the transformations, we calculate its reward. Finally, we
    return the max reward found with this process.

    Parameters
    ----------
    pattern: dict
        The pattern under transformation
    dummy_dict: dict
        The values replacing the keywords of the pattern
    reference_stylometry: `numpy.array`
        The stylometry of the original extract
    Q_next_step: `numpy.array`
        The array of the next step Q-values
    args: dict
        Parameters to be used for the reward function

    Returns
    -------
    float
        The maximum possible reward for the next step
    """
    reward = []
    # Get all the transformations applicable to this pattern
    all_transformations = get_all_applicable_transformations(pattern)
    # Calculate the reward for each of them
    for idx, transformation in enumerate(all_transformations):
        # Since most of the transformations modify the dummy_dict, we need
        # to run them on a deepcopy of the original one
        trans_pattern = transformation(pattern, copy.deepcopy(dummy_dict))
        rew_value = _compute_reward(trans_pattern, reference_stylometry, args)
        reward.append(Q_next_step[idx] + rew_value)

    # Return max reward
    return max(reward)

def _compute_reward(transformed_pattern, reference_stylometry, args):
    """ Compute the reward produced by a pattern transformation.

    Parameters
    ----------
    transformed_pattern: string
        The transformed pattern to insert
    reference_stylometry: `numpy.array`
        The stylometry of the original extract
    args: dict
        Dictionary of arguments (included the corpus_unigram)

    Returns
    -------
    float
        The similarity between the reference stylometry and the computed
        stylometry
    """
    # Inject the transformed pattern into the extract
    modified_snippet = _insert_into_extract(args['reference_extract'],
                                            transformed_pattern)
    extract_modified_stylography = compute_vector(
        modified_snippet, args['corpus_unigram'])

    sim = cosine_similarity(reference_stylometry.reshape(1, -1),
                            extract_modified_stylography.reshape(1, -1))[0][0]
    return sim

def _insert_into_extract(extract, transformed_pattern):
    """ Insert a transformed pattern into an extract (at a random position).

    Parameters
    ----------
    extract: string
        The extract
    transformed_pattern: string
        The transformed pattern

    Returns
    -------
    string
        The extract containing also the transformed pattern (randomly inserted)
    """
    extract_splitted = extract.split('\n')
    extract_splitted.insert(random.randint(0, len(extract_splitted) - 1),
                            transformed_pattern)
    return '\n'.join(extract_splitted)


__all__ = []

from . import (
    stack,
    binomial_trees,
    queue,
    disjoint_set,
    sparse_table
)

from .binomial_trees import (
    BinomialTree
)
__all__.extend(binomial_trees.__all__)

from .stack import (
    Stack,
)
__all__.extend(stack.__all__)

from .queue import (
    Queue,
    PriorityQueue
)
__all__.extend(queue.__all__)

from .disjoint_set import (
    DisjointSetForest,
)
__all__.extend(disjoint_set.__all__)

from .sparse_table import (
    SparseTable,
)
__all__.extend(sparse_table.__all__)

from .algorithms import (
    RangeQueryStatic
)
__all__.extend(algorithms.__all__)


import sys
import traceback
from typing import Any, Callable, List

import pygame as pg

from shell.shell import Shell
from shell.copy_logger import CopyLogger, LogType, LinesLog

from gui.view import MainView
from gui.terminal import TerminalWidget
from gui.widget import Widget
from gui.constants import Colors

class Window:

    def __init__(self, size, flags=0, bg_color=(255, 255, 255)) -> None:
        self.screen = pg.display.set_mode(size, flags)
        self.view = MainView(self.screen.get_size())
        self.bg_color = bg_color
        self.size = self.screen.get_size()

        self.event_cbs: List[Callable[[pg.event.Event], Any]] = []

    def add_event_listener(self, func: Callable[[pg.event.Event], Any]):
        self.event_cbs.append(func)

    def update(self):
        for event in pg.event.get():
            # Window event handlers
            if event.type == pg.QUIT:
                pg.quit()
                return False

            # Event callbacks
            for cb in self.event_cbs:
                cb(event)

            # View's event handler
            self.view.handle_event(event)

        # View update
        self.view.update()

        return True

    def draw(self):
        self.screen.fill(self.bg_color)
        self.view.draw(self.screen)

class OSWindow(Window):

    MAIN_TAG = "__MAIN__"

    def __init__(self, shell: Shell, bg_color=Colors.BACKGROUND) -> None:
        # OS window is always fullscreen
        super().__init__((0, 0), pg.FULLSCREEN, bg_color)

        # Initialize state
        w, h = self.size
        self.shell = shell
        self.shell.set_gui(self)

        # Initialize terminal widget
        self.terminal = TerminalWidget(0, 0, w, h,
                            prompt_func=shell.prompt, file=LinesLog())
        self.terminal.active = True
        sys.stdout = self.terminal.file
        sys.stderr = CopyLogger(file=sys.stderr,
                                logdata=self.terminal.file, logtype=LogType.ERR)
        self.terminal.add_input_listener(shell.handle_input)

        # Add terminal to the default MainView
        self.view.add_widget(self.terminal)

        # Initialize view stack
        self.viewstack = []
        self.viewtag = OSWindow.MAIN_TAG

        # Start shell
        shell.start()

    # TODO: Consider ways to handle repeat tags
    def push_view(self, tag, view: Widget):
        """
        Push and render the view. The `tag` parameter is used to identify views,
        it can be any type that supports equality comparisons.
        """
        self.viewstack.append((self.viewtag, self.view))
        self.view = view
        self.viewtag = tag

    def pop_view(self):
        if len(self.viewstack) > 0:
            self.viewtag, self.view = self.viewstack.pop()
        else:

            # NOTE: Print exception to console without really raising
            try:
                raise IndexError("View stack is empty!")
            except:
                traceback.print_exc(file=sys.__stderr__)


import collections
from typing import Any, Union

import attrs
import tensorflow as tf
import tensorflow_federated as tff

ModelWeights = collections.namedtuple('ModelWeights', 'trainable non_trainable')
ModelOutputs = collections.namedtuple('ModelOutputs', 'loss')

def get_model_weights(
    model: Union[tff.learning.models.VariableModel, 'KerasModelWrapper']
) -> Union[tff.learning.models.ModelWeights, ModelWeights]:
  """Gets the appropriate ModelWeights object based on the model type."""
  if isinstance(model, tff.learning.models.VariableModel):
    return tff.learning.models.ModelWeights.from_model(model)
  else:
    # Using simple_fedavg custom Keras wrapper.
    return model.weights

class KerasModelWrapper:
  """A standalone keras wrapper to be used in TFF."""

  def __init__(self, keras_model, input_spec, loss):
    """A wrapper class that provides necessary API handles for TFF.

    Args:
      keras_model: A `tf.keras.Model` to be trained.
      input_spec: Metadata of dataset that desribes the input tensors, which
        will be converted to `tff.Type` specifying the expected type of input
        and output of the model.
      loss: A `tf.keras.losses.Loss` instance to be used for training.
    """
    self.keras_model = keras_model
    self.input_spec = input_spec
    self.loss = loss

  def forward_pass(self, batch_input, training=True):
    """Forward pass of the model to get loss for a batch of data.

    Args:
      batch_input: A `collections.abc.Mapping` with two keys, `x` for inputs and
        `y` for labels.
      training: Boolean scalar indicating training or inference mode.

    Returns:
      A scalar tf.float32 `tf.Tensor` loss for current batch input.
    """
    preds = self.keras_model(batch_input['x'], training=training)
    loss = self.loss(batch_input['y'], preds)
    return ModelOutputs(loss=loss)

  @property
  def weights(self):
    return ModelWeights(
        trainable=self.keras_model.trainable_variables,
        non_trainable=self.keras_model.non_trainable_variables,
    )

  def from_weights(self, model_weights):
    tf.nest.map_structure(
        lambda v, t: v.assign(t),
        self.keras_model.trainable_variables,
        list(model_weights.trainable),
    )
    tf.nest.map_structure(
        lambda v, t: v.assign(t),
        self.keras_model.non_trainable_variables,
        list(model_weights.non_trainable),
    )

def keras_evaluate(model, test_data, metric):
  metric.reset_states()
  for batch in test_data:
    preds = model(batch['x'], training=False)
    metric.update_state(y_true=batch['y'], y_pred=preds)
  return metric.result()

@attrs.define(eq=False, frozen=True)
class ClientState:
  """Structure for state on the client.

  Fields:
  -   `client_index`: The client index integer to map the client state back to
      the database hosting client states in the driver file.
  -   `iters_count`: The number of total iterations a client has computed in
      the total rounds so far.
  """

  client_index: int
  iters_count: int

@attrs.define(eq=False, frozen=True)
class ClientOutput:
  """Structure for outputs returned from clients during federated optimization.

  Fields:
  -   `weights_delta`: A dictionary of updates to the model's trainable
       variables.
  -   `client_weight`: Weight to be used in a weighted mean when
       aggregating `weights_delta`.
  -   `model_output`: A structure matching
      `tff.learning.models.VariableModel.report_local_unfinalized_metrics`,
      reflecting the
       results of training on the input dataset.
  -   `client_state`: The updated `ClientState`.
  """

  weights_delta: Any
  client_weight: float
  model_output: Any
  client_state: ClientState

@attrs.define(eq=False, frozen=True)
class ServerState:
  """Structure for state on the server.

  Fields:
  -   `model_weights`: A dictionary of model's trainable variables.
  -   `optimizer_state`: Variables of optimizer.
  -   'round_num': Current round index
  -   `total_iters_count`: The total number of iterations run on seen clients
  """

  model_weights: Any
  optimizer_state: Any
  round_num: int
  total_iters_count: int

@attrs.define(eq=False, frozen=True)
class BroadcastMessage:
  """Structure for tensors broadcasted by server during federated optimization.

  Fields:
  -   `model_weights`: A dictionary of model's trainable tensors.
  -   `round_num`: Round index to broadcast. We use `round_num` as an example to
       show how to broadcast auxiliary information that can be helpful on
       clients. It is not explicitly used, but can be applied to enable
       learning rate scheduling.
  """

  model_weights: Any
  round_num: int

@tf.function
def server_update(
    model, server_optimizer, server_state, weights_delta, total_iters_count
):
  """Updates `server_state` based on `weights_delta`.

  Args:
    model: A `KerasModelWrapper` or `tff.learning.models.VariableModel`.
    server_optimizer: A `tf.keras.optimizers.Optimizer`. If the optimizer
      creates variables, they must have already been created.
    server_state: A `ServerState`, the state to be updated.
    weights_delta: A nested structure of tensors holding the updates to the
      trainable variables of the model.
    total_iters_count: A scalar to update `ServerState.total_iters_count`.

  Returns:
    An updated `ServerState`.
  """
  # Initialize the model with the current state.
  model_weights = get_model_weights(model)
  tf.nest.map_structure(
      lambda v, t: v.assign(t), model_weights, server_state.model_weights
  )
  tf.nest.map_structure(
      lambda v, t: v.assign(t),
      server_optimizer.variables(),
      server_state.optimizer_state,
  )

  # Apply the update to the model.
  neg_weights_delta = [-1.0 * x for x in weights_delta]
  server_optimizer.apply_gradients(
      zip(neg_weights_delta, model_weights.trainable), name='server_update'
  )

  # Create a new state based on the updated model.
  return tff.structure.update_struct(
      server_state,
      model_weights=model_weights,
      optimizer_state=server_optimizer.variables(),
      round_num=server_state.round_num + 1,
      total_iters_count=total_iters_count,
  )

@tf.function
def build_server_broadcast_message(server_state):
  """Build `BroadcastMessage` for broadcasting.

  This method can be used to post-process `ServerState` before broadcasting.
  For example, perform model compression on `ServerState` to obtain a compressed
  state that is sent in a `BroadcastMessage`.

  Args:
    server_state: A `ServerState`.

  Returns:
    A `BroadcastMessage`.
  """
  return BroadcastMessage(
      model_weights=server_state.model_weights, round_num=server_state.round_num
  )

@tf.function
def client_update(
    model, dataset, client_state, server_message, client_optimizer
):
  """Performans client local training of `model` on `dataset`.

  Args:
    model: A `tff.learning.models.VariableModel`.
    dataset: A 'tf.data.Dataset'.
    client_state: A 'ClientState'.
    server_message: A `BroadcastMessage` from server.
    client_optimizer: A `tf.keras.optimizers.Optimizer`.

  Returns:
    A 'ClientOutput`.
  """
  model_weights = get_model_weights(model)
  initial_weights = server_message.model_weights
  tf.nest.map_structure(
      lambda v, t: v.assign(t), model_weights, initial_weights
  )

  num_examples = tf.constant(0, dtype=tf.int32)
  loss_sum = tf.constant(0, dtype=tf.float32)
  iters_count = tf.convert_to_tensor(client_state.iters_count)
  for batch in dataset:
    with tf.GradientTape() as tape:
      outputs = model.forward_pass(batch)
    grads = tape.gradient(outputs.loss, model_weights.trainable)
    client_optimizer.apply_gradients(zip(grads, model_weights.trainable))
    batch_size = tf.shape(batch['x'])[0]
    num_examples += batch_size
    loss_sum += outputs.loss * tf.cast(batch_size, tf.float32)
    iters_count += 1

  weights_delta = tf.nest.map_structure(
      lambda a, b: a - b, model_weights.trainable, initial_weights.trainable
  )

  client_weight = tf.cast(num_examples, tf.float32)
  return ClientOutput(
      weights_delta,
      client_weight,
      loss_sum / client_weight,
      ClientState(
          client_index=client_state.client_index, iters_count=iters_count
      ),
  )


from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin

from .managers import UserManager

# AbstractBaseUser: 완전히 새로운 User 모델을 상속받아 새로 정의한다.
# AbstractUser: 추상 클래스와 같이 필드가 있는 완전한 사용자 모델, 상속하고 고유한 프로필 필드 및 메서드 추가 가능

class User(AbstractBaseUser, PermissionsMixin):
    '''
    Customized User

    AbstractBaseUser를 상속받아 사용하기 때문에 password 필드는 따로 생성할 필요가 없다.
    '''
    email = models.EmailField(
        verbose_name='email id',
        max_length=64,
        unique=True
    )
    username = models.CharField(
        verbose_name='name',
        max_length=30,
        unique=True
    )
    photo = models.ImageField(
        verbose_name='profile image',
        upload_to="profile/image/",
        default='profile/image/default.png'
    )
    introduce = models.TextField(
        verbose_name='introduce',
        null = True,
        default = ''
    )
    is_staff = models.BooleanField(
        verbose_name='staff status',
        default=False,
    )
    is_active = models.BooleanField(
        verbose_name='active',
        default=True,
    )
    date_joined = models.DateTimeField(
        verbose_name='data joined',
        default=timezone.now
    )

    objects = UserManager()

    EMAIL_FIELD = 'email'
    USERNAME_FIELD = 'email'

    class Meta:
        # 사용자가 읽기 쉬운 모델 객체의 이름 (단수형)
        verbose_name = 'user'

        # 사용자가 읽기 쉬운 모델 객체의 이름 (복수형)
        verbose_name_plural = 'users'

    def __str__(self):
        return self.username

    def get_short_name(self):
        return self.email


import os
import numpy as np
import itertools

import torch
import torch.nn as nn
from torch.nn import functional as F
from modules.utils.util_model import get_available_devices
import functools

class cGAN(nn.Module):
    def __init__(self, params, is_train, img_channel=1, device='cpu'):
        super().__init__()
        self.device = device

        # define model
        self.G = UnetGenerator(input_nc=img_channel, output_nc=img_channel, num_downs=6, ngf=32, norm_layer=nn.BatchNorm2d, use_dropout=is_train).to(self.device)

        if is_train:
            # parameters used for training
            lr = params["train"]["lr"]
            self.w_gan = params["cgan"]["w_gan"]
            self.D_update_interval = params["cgan"]['D_update_interval']
            self.G_warm_up = params["cgan"]['G_warm_up']

            # define model
            self.D = Discriminator(img_channel).to(self.device)

            # optimizers
            G_params = self.G.parameters()
            D_params = self.D.parameters()
            self.optimizer_G = torch.optim.Adam(G_params, lr=lr, amsgrad=True)
            self.optimizer_D = torch.optim.Adam(D_params, lr=lr, amsgrad=True)
            self.D.requires_grad = False # will become True when updating D inside optimize_parameters()

            # for discriminator
            self.register_buffer('real_label', torch.tensor(1.0))
            self.register_buffer('fake_label', torch.tensor(0.0))

            # losses
            self.criterionGAN = nn.BCEWithLogitsLoss()
            self.criterionRecon = nn.L1Loss()

            self.model_names = ['D', 'G']
            self.loss_names = ['G', 'GAN', 'D']
            self.loss_D, self.loss_GAN = None, None
        else:
            self.model_names = ['G']

    def set_input(self, data):
        self.filename = data["filename"]
        self.image = data["image"].to(self.device) # (B, img_channel, 256, 256)
        self.centerline = data["centerline"].to(self.device) # (B, img_channel, 256, 256)

    def optimize_parameters(self, iters, epoch):
        self.image_recon = self.forward(self.centerline)

        # update D
        if (iters % self.D_update_interval== 0) and (iters > self.G_warm_up):
            self.D.requires_grad = True
            self.optimizer_D.zero_grad()
            self.backward_D()
            self.optimizer_D.step()
            self.D.requires_grad = False

        # update G
        self.optimizer_G.zero_grad()
        self.backward_G(iters)
        self.optimizer_G.step()

    def forward(self, centerline):
        image_recon = self.G(centerline)
        return image_recon

    def backward_D(self):
        images_real = torch.cat((self.centerline, self.image), dim=1)
        images_fake  = torch.cat((self.centerline, self.image_recon), dim=1)

        pred_real = self.D(images_real)
        pred_fake = self.D(images_fake.detach())

        is_real = self.real_label.expand_as(pred_real).to(images_real.device)
        is_fake = self.fake_label.expand_as(pred_fake).to(images_fake.device)

        loss_D_real = self.criterionGAN(pred_real, is_real)
        loss_D_fake = self.criterionGAN(pred_fake, is_fake)

        self.loss_D = (loss_D_real + loss_D_fake) * 0.5
        self.loss_D.backward()

    def backward_G(self, iters):
        images_fake = torch.cat((self.centerline, self.image_recon), dim=1)
        pred_fake = self.D(images_fake)

        is_real = self.real_label.expand_as(pred_fake).to(images_fake.device)
        self.loss_GAN = self.criterionGAN(pred_fake, is_real)

        self.loss_recon = self.criterionRecon(self.image_recon, self.image)

        if iters > self.G_warm_up:
            self.loss_G = self.loss_recon + self.w_gan*self.loss_GAN
        else:
            self.loss_G = self.loss_recon
        self.loss_G.backward()

class Discriminator(nn.Module):
    def __init__(self, img_channel):
        super().__init__()
        self.D = nn.Sequential(
            nn.Conv2d(2*img_channel, 32, kernel_size=4, stride=2, padding=1, bias=True), # 128
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(32, 64, kernel_size=4, stride=2, padding=1, bias=False), # 64
            nn.BatchNorm2d(64),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1, bias=False), # 32
            nn.BatchNorm2d(128),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(128, 256, kernel_size=4, stride=2, padding=1, bias=False), # 16
            nn.BatchNorm2d(256),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(256, 1, kernel_size=4, stride=1, padding=1, bias=True)
        )

    def forward(self, I):
        pred = self.D(I)
        return pred

class UnetGenerator(nn.Module):
    """Create a Unet-based generator"""

    def __init__(self, input_nc, output_nc, num_downs, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False):
        """Construct a Unet generator
        Parameters:
            input_nc (int)  -- the number of channels in input images
            output_nc (int) -- the number of channels in output images
            num_downs (int) -- the number of downsamplings in UNet. For example, # if |num_downs| == 7,
                                image of size 128x128 will become of size 1x1 # at the bottleneck
            ngf (int)       -- the number of filters in the last conv layer
            norm_layer      -- normalization layer
        We construct the U-Net from the innermost layer to the outermost layer.
        It is a recursive process.
        """
        super(UnetGenerator, self).__init__()
        # construct unet structure
        unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=None, norm_layer=norm_layer, innermost=True)  # add the innermost layer
        for i in range(num_downs - 5):          # add intermediate layers with ngf * 8 filters
            unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=unet_block, norm_layer=norm_layer, use_dropout=use_dropout)
        # gradually reduce the number of filters from ngf * 8 to ngf
        unet_block = UnetSkipConnectionBlock(ngf * 4, ngf * 8, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
        unet_block = UnetSkipConnectionBlock(ngf * 2, ngf * 4, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
        unet_block = UnetSkipConnectionBlock(ngf, ngf * 2, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
        self.model = UnetSkipConnectionBlock(output_nc, ngf, input_nc=input_nc, submodule=unet_block, outermost=True, norm_layer=norm_layer)  # add the outermost layer

    def forward(self, input):
        """Standard forward"""
        return self.model(input)

class UnetSkipConnectionBlock(nn.Module):
    """Defines the Unet submodule with skip connection.
        X -------------------identity----------------------
        |-- downsampling -- |submodule| -- upsampling --|
    """

    def __init__(self, outer_nc, inner_nc, input_nc=None,
                 submodule=None, outermost=False, innermost=False, norm_layer=nn.BatchNorm2d, use_dropout=False):
        """Construct a Unet submodule with skip connections.
        Parameters:
            outer_nc (int) -- the number of filters in the outer conv layer
            inner_nc (int) -- the number of filters in the inner conv layer
            input_nc (int) -- the number of channels in input images/features
            submodule (UnetSkipConnectionBlock) -- previously defined submodules
            outermost (bool)    -- if this module is the outermost module
            innermost (bool)    -- if this module is the innermost module
            norm_layer          -- normalization layer
            use_dropout (bool)  -- if use dropout layers.
        """
        super(UnetSkipConnectionBlock, self).__init__()
        self.outermost = outermost
        if type(norm_layer) == functools.partial:
            use_bias = norm_layer.func == nn.InstanceNorm2d
        else:
            use_bias = norm_layer == nn.InstanceNorm2d
        if input_nc is None:
            input_nc = outer_nc
        downconv = nn.Conv2d(input_nc, inner_nc, kernel_size=4,
                             stride=2, padding=1, bias=use_bias)
        downrelu = nn.LeakyReLU(0.2, True)
        downnorm = norm_layer(inner_nc)
        uprelu = nn.ReLU(True)
        upnorm = norm_layer(outer_nc)

        if outermost:
            upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1)
            down = [downconv]
            up = [uprelu, upconv, nn.Tanh()]
            # up = [uprelu, upconv]
            model = down + [submodule] + up
        elif innermost:
            upconv = nn.ConvTranspose2d(inner_nc, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1, bias=use_bias)
            down = [downrelu, downconv]
            up = [uprelu, upconv, upnorm]
            model = down + up
        else:
            upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1, bias=use_bias)
            down = [downrelu, downconv, downnorm]
            up = [uprelu, upconv, upnorm]

            if use_dropout:
                model = down + [submodule] + up + [nn.Dropout(0.5)]
            else:
                model = down + [submodule] + up

        self.model = nn.Sequential(*model)

    def forward(self, x):
        if self.outermost:
            return self.model(x)
        else:   # add skip connections
            return torch.cat([x, self.model(x)], 1)


from flask import Flask
import os

def create_app():
    app = Flask(__name__)
    from . import books,rental
    app.register_blueprint(books.bp)
    app.register_blueprint(rental.bp)

    from . import bookdb
    bookdb.init_app(app)

    app.config.from_mapping(
    SECRET_KEY='temp',
    DATABASE=os.path.join(app.instance_path,'bookdb.sqlite3'),
    )
    return app


import asyncio
import json
from js import console, document

from wasm_websocket import connect
from dom_manipulations import message, create_views, show_page
from dom_events import add_nav_events
from utilities import config, ids

_REPORT_TRANSACTIONS = False

class _Gateway:
    """Websocket communication with gateway (ESP32)"""

    def __init__(self, url):
        self._url = url
        self._ws = None
        self._ping_interval = 5
        self._splash_msg = None

    def splash_msg(self, msg, clear=False):
        if self._splash_msg == None:
            self._splash_msg = document.getElementById("splash")
        splash = self._splash_msg
        if clear:
            # remove all children
            while splash.firstChild:
                splash.removeChild(splash.firstChild)
        div = document.createElement('div')
        div.innerText = msg
        splash.appendChild(div)

    async def _send(self, msg):
        assert isinstance(msg, dict)
        assert 'tag' in msg
        if _REPORT_TRANSACTIONS: console.log(f"send {msg}")
        return await asyncio.wait_for(self._ws.send(json.dumps({ 'data': msg })), 10)

    async def _ping_pong_task(self):
        """Send pings at reglar intervals:
        Gateway disconnects if no messages received for too long.
        Webapp reconnects if connection dead.
        """
        while True:
            if self._ws != None:
                try:
                    await asyncio.wait_for(self._send({ "tag": "ping" }), self._ping_interval)
                    await asyncio.sleep(self._ping_interval)
                except asyncio.TimeoutError:
                    console.log(f"***** ping_pong_task: timeout - disconnected?")

    async def run(self):
        # get this going and never stop
        asyncio.create_task(self._ping_pong_task())

        startup = True

        # connect / reconnect loop
        while True:
            # try connecting until successful
            self.splash_msg(f"Attempting connection to {self._url}")
            while True:
                try:
                    self._ws = await connect(self._url)
                    await asyncio.wait_for(self._send({ "tag": "ping" }), 240)
                    break
                except asyncio.TimeoutError as e:
                    console.log(f"***** gateway.run timeout: {e}")
                except Exception as e:
                    console.log(f"***** gateway.run connecting: {e}")
                self.splash_msg(f"Is the gateway up?")
                try:
                    await self._ws.close()
                except Exception as e:
                    console.log(f"***** gateway.run close: {e}")

            try:
                if startup:
                    # wait for config_put to get the configuration & show the app screen
                    await self._send({ 'tag': 'config_get' })
                    startup = False
                else:
                    # make sure state is up-to-date after a possibily long disconnect
                    await self._send({ 'tag': 'state_get_all' })
                    show_page(last_page)

                # receive messages until disconnect detected
                await self._recv()
                console.log(f"***** Gateway disconnected")
                last_page = show_page('splashscreen')
                self.splash_msg(f"Gateway disconnected", True)
            except Exception as e:
                console.log(f"***** gateway.run finishing: {e}")

    async def _recv(self):
        # perpetually receive messages - until connection breaks
        while True:
            try:
                msg = await asyncio.wait_for(self._ws.recv(), timeout=self._ping_interval)
                if _REPORT_TRANSACTIONS: console.log(f"recv {msg}")
            except asyncio.TimeoutError:
                console.log(f"GATEWAY TIMEOUT: lost connection, ping interval = {self._ping_interval}")
                try:
                    await self._ws.close()
                except Exception:
                    pass
                return
            except Exception as e:
                console.log(f"***** gateway recv: {e}")
                continue

            msg = json.loads(msg)
            # console.log("GATEWAY got", str(msg))
            try:
                tag = msg["tag"]
                method = getattr(self, f"_handle_{tag}")
            except KeyError:
                console.log(f"***** Message has no tag attribute: {msg}")
                continue
            except AttributeError:
                console.log(f"***** No handler for {tag}: {msg}")
                continue
            try:
                del msg["tag"]
                # console.log(f"CALL {method}")
                await method(**msg)
            except Exception as e:
                console.log(f"***** gateway - error handling: {msg}", str(e))

    async def _handle_config_put(self, value, path=[]):
        # accept only full configuration
        assert len(path) == 0

        # notify user that we are updating the app
        self.splash_msg(f"Configuration received")
        last_page = show_page('splashscreen')

        config.set(value)
        self._ping_interval = float(config.get('app', 'ping-interval')) or 8

        # update view to match new config
        self.splash_msg(f"Create views")
        create_views()
        self.splash_msg(f"Attach event handlers")
        add_nav_events()
        self.splash_msg(f"Setup Complete")

        # show the last selected page (defaults to view-1 on app startup)
        show_page(last_page)

        # get current state
        await self._send({ "tag": "state_get_all" })

    async def _handle_state_update(self, eid, value, all=False):
        for entity in document.getElementsByClassName(ids.css(eid)):
            if isinstance(value, float):
                value = f"{value:.1f}"
            entity.querySelector('.entity-value').innerText = value

    async def _handle_info(self, category, msg):
        message(f"{category}: {msg}")

    async def _handle_discovered(self, device):
        message(f"discovered: {device}")

    async def _handle_ping(self):
        await self._send({ "tag": "pong" })

    async def _handle_pong(self):
        # nothing to do - recv task already took note
        pass

_GATEWAY = None

async def send(msg):
    # backdoor to send without reference to gateway
    # gateway is singleton
    global _GATEWAY
    await _GATEWAY._send(msg)

async def gateway_task(url='ws://10.0.0.8/ws'):
    """Communicate with gateway. Automatic reconnects.
    This never returns.
    Gateway is a SINGLETON!
    """
    global _GATEWAY
    _GATEWAY = _Gateway(url)
    await _GATEWAY.run()


from threading import Thread
from typing import List
from urllib import request

def getImage( url:str , size:int , imageName:str, imageExt: str ):
    fullImageUrl = url + "/" + str(size) + "x0w.webp"
    request.urlretrieve(fullImageUrl,  imageName + "." + imageExt )

appImageUrl = "https://is1-ssl.mzstatic.com/image/thumb/Purple122/v4/11/5f/65/115f65e0-0dc8-25a4-cd4a-3a141ab70f8b/AppIcon-0-0-1x_U007emarketing-0-0-0-5-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png"
downloadImageExt = "png"

sizeList = [32, 64, 128, 256, 512 ]

threadlist:List[Thread] = []
for size in sizeList:
    downloadImageName = "test" + str(size )
    threadlist.append(Thread(target=getImage , name="[{}th thread ]".format(len(threadlist)), args=(appImageUrl, size ,downloadImageName ,downloadImageExt,)))

for thread in threadlist :
    thread.start()

for thread in threadlist :
    thread.join()


from django.db.models.query_utils import Q
from rest_framework.exceptions import NotAuthenticated, NotFound
from post.utils import delete_post_images
from post.serializers import PostSerializer, PostUpdate
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.response import Response
from rest_framework import status
from .models import Image, Post
from django.http import FileResponse, request
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from .permissions import post_accessble
from history.utils import add_to_post_history
from django.db.models import Count

#post view
class PostView(GenericAPIView):
    serializer_class = PostSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

    #get post
    def get(self, request, **kwargs):
        id = kwargs.get('id', 0)
        try:
            post = Post.objects.annotate(like=Count(
                "likes"), comment=Count("comments")).get(id=id)
            serializer = self.get_serializer(post)

            if post_accessble(request, post):
                add_to_post_history(request, post)
                return Response(serializer.data, status=status.HTTP_200_OK)
            return Response(status=status.HTTP_404_NOT_FOUND)
        except:
            return Response(status=status.HTTP_404_NOT_FOUND)

    #create a post
    def post(self, request):
        #create post
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        post = serializer.save()
        return Response({"id": post.id}, status=status.HTTP_201_CREATED)

    def put(self, request, **kwargs):
        id = kwargs.get("id", 0)
        post = Post.objects.get(id=id)

        if post.user_id != request.user.id:
            raise NotFound()

        post = PostUpdate(post, data=request.data, partial=True)
        post.is_valid(raise_exception=True)
        post.save()
        return Response(data=post.data, status=status.HTTP_200_OK)

    #delete a post
    def delete(self, request, **kwargs):
        id = kwargs.get('id', 0)
        post = Post.objects.get(id=id)
        delete_post_images(post)
        post.delete()
        return Response(status=status.HTTP_200_OK)

class PostImageView(GenericAPIView):
    serializer_class = Image

    def get(self, request, **kwargs):
        id = kwargs.get('id', 0)

        try:
            img = Image.objects.get(id=id)
            post = Post.objects.get(id=img.post_id)
            if post_accessble(request, post):
                return FileResponse(open(str(img.image), 'rb'))
        except:
            raise NotFound()
        raise NotAuthenticated()

class PostList(ListAPIView):
    serializer_class = PostSerializer

    def get_queryset(self):
        query = ""
        user_id = self.request.user.id

        if self.request.user.is_authenticated:
            query = Post.objects.select_related("user").annotate(like=Count("likes", distinct=True), comment=Count("comments"), user_liked=Count("likes", filter=Q(likes__user_id=user_id))).filter(
                    Q(user_id=user_id) | ( Q(is_private=False) & ( Q(user__is_private=False) | ( Q(user__followers__user_id=user_id) & Q(user__followers__is_verified=True) ) ))
                ).prefetch_related('images')
        else:
            query = Post.objects.select_related("user").annotate(like=Count("likes", distinct=True), comment=Count(
                "comments")).filter(Q(is_private=False) & Q(user__is_private=False)).prefetch_related('images')

        username = self.request.GET.get("user", None)
        if username:
            query = query.filter(user__username=username)

        sort = self.request.GET.get("sort", "new")
        if sort == "new":
            query = query.order_by('-date')
        elif sort == "old":
            query = query.order_by('date')
        elif sort == "likes":
            query = query.order_by("-like")

        return query


from setuptools import find_packages, setup

with open("README.md", "r") as f:
    page_description = f.read()

with open("requirements.txt") as f:
    requirements = f.read().splitlines()

setup(
    name="asn-nmap",
    version="0.1.9",
    author="Edgar Reis",
    description="Get status service from a ASN.",
    long_description=page_description,
    long_description_content_type="text/markdown",
    url="https://github.com/brERS/asn_nmap",
    packages=find_packages(),
    install_requires=requirements,
    python_requires='>=3.9',
)


from django.urls import path
from . import views

app_name = 'challenges'

urlpatterns = [
    # URL for the daily challenge page, using a dynamic parameter for challenge_id
    path('challenge/', views.daily_challenge, name='daily_challenge'),

    # URL for the leaderboard page
    path('leaderboard/', views.leaderboard, name='leaderboard'),

    # Add other URLs for your app's views as needed
]


import abc
import typing

import torch
from torch import Tensor
from torch.nn import Module, MSELoss
from torchvision import models, transforms as T

mse_loss = MSELoss(reduction="mean")
sse_loss = MSELoss(reduction="sum")

class Loss(Module):

    def __init__(self):
        super().__init__()

    def forward(self, x: Tensor, target: Tensor):
        assert target.shape == x.shape
        assert target.shape[2] == 3 and x.shape[2] == 3, "Images need to be on the format [W,H,3]"
        x = torch.clamp(x, 0.0, 1.0)
        target = torch.clamp(target, 0.0, 1.0)
        return self._loss(x, target)

    @abc.abstractmethod
    def _loss(self, x: Tensor, target: Tensor):
        pass

class NeuralLoss(Loss):

    def __init__(self, layers: typing.Iterable[int] = None, layer_weights: typing.Iterable[float] = None):
        super().__init__()
        if layers is None or layers == []:
            layers = [0]
        if layer_weights is None or layer_weights == []:
            layer_weights = [1]

        self._layer_indices = layers
        self._layer_weights = torch.tensor(layer_weights)[0:len(layers)]

        self.vgg = models.vgg19(pretrained=True, progress=True)
        self.modulelist = list(self.vgg.features.modules())

        for i, mod in enumerate(self.modulelist):
            if hasattr(mod, "inplace"):
                mod.inplace = False

            if isinstance(mod, torch.nn.MaxPool2d):
                self.modulelist[i] = torch.nn.AvgPool2d(kernel_size=mod.kernel_size, stride=mod.stride, padding=mod.padding, ceil_mode=mod.ceil_mode)

        self._normalize = T.Compose([
            T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])

    def __str__(self):
        return "Neural Loss (\n\tlayers:\n {}, \n\tweights: {}\n)".format(self._layer_indices, self._layer_weights)

    def _preprocess(self, x: Tensor):
        # Swap axes to get image on CxHxW form, which is required for Models in PyTorch, then Normalize to comply with VGG19 and add batch dimension
        return self._normalize(x.permute(2, 1, 0)).unsqueeze(0)

    def _gram_matrix(self, activation: Tensor, N: int, M: int):
        feature_map = activation.view(N, M)
        G = torch.mm(feature_map, feature_map.t())
        return G

    def _loss(self, x: Tensor, target: Tensor):
        assert x.shape == target.shape
        assert list(x.shape) == [224, 224, 3]

        x_ = self._preprocess(x)
        target_ = self._preprocess(target)
        E = []

        for i, layer in enumerate(self.modulelist[1:]):
            x_ = layer(x_)
            target_ = layer(target_)
            if i > self._layer_indices[-1]:
                break
            if i in self._layer_indices:
                N = x_.shape[1]
                M = x_.shape[2] * x_.shape[3]
                G1 = self._gram_matrix(x_, N, M)
                G2 = self._gram_matrix(target_, N, M)

                E.append(sse_loss(G1, G2) * (1 / (4. * (N ** 2) * (M ** 2))))

        L_tot = torch.sum(torch.stack(E) * self._layer_weights)
        return L_tot

class XSELoss(Loss):

    def __init__(self, reduction: str = 'mean'):
        super().__init__()
        self.loss_func = MSELoss(reduction=reduction)

    def _loss(self, x: Tensor, target: Tensor):
        return self.loss_func(x, target)

class SquaredBinLoss(Loss):

    def __init__(self, bin_size: int = 10):
        super().__init__()
        self.size = bin_size

    def _loss(self, x: Tensor, target: Tensor):
        input_bins = x.unfold(1, self.size, self.size).unfold(0, self.size, self.size).transpose(2, -1)
        input_bins = input_bins.reshape(input_bins.shape[0] * input_bins.shape[1], self.size, self.size, 3)
        target_bins = target.unfold(1, self.size, self.size).unfold(0, self.size, self.size).transpose(2, -1)
        target_bins = target_bins.reshape(target_bins.shape[0] * target_bins.shape[1], self.size, self.size, 3)
        input_means = torch.mean(input_bins, dim=(1, 2))
        target_means = torch.mean(target_bins, dim=(1, 2))
        return mse_loss(input_means, target_means)

class VerticalBinLoss(Loss):

    def __init__(self, bin_size: int = 20):
        super().__init__()
        self.bin_size = bin_size

    def _loss(self, x: Tensor, target: Tensor):
        assert target.shape[2] % self.bin_size == 0, "The number of columns in the image can not be evenly divided into bins of size {}!".format(
            self.bin_size)
        bins_truth = torch.stack(target.split(self.bin_size, dim=-1))
        bins_x = torch.stack(target.split(self.bin_size, dim=-1))
        std_truth, mean_truth = torch.std_mean(bins_truth, dim=(1, 2, 3))
        std_x, mean_x = torch.std_mean(bins_x, dim=(1, 2, 3))
        return mse_loss(mean_x, mean_truth)


from collections import OrderedDict

import torch.nn as nn

class MyModel(nn.Module):

    NUM_CAGETORIES = 7

    def __init__(self, resnet_class, output_size=NUM_CAGETORIES):
        super(MyModel, self).__init__()
        pretrained_resnet = resnet_class(pretrained=True)

        self.features = nn.Sequential( OrderedDict([
            ('conv1', pretrained_resnet.conv1),
            ('bn1', pretrained_resnet.bn1),
            ('relu', pretrained_resnet.relu),
            ('maxpool', pretrained_resnet.maxpool),

            ('layer1', pretrained_resnet.layer1),
            ('layer2', pretrained_resnet.layer2),
            ('layer3', pretrained_resnet.layer3),
            ('layer4', pretrained_resnet.layer4),

            ('avgpool', nn.AdaptiveAvgPool2d((1, 1))) # 参数(1, 1)表示希望输出的feature map空间方向上的维度为1×1
        ]) )

        hidden_size_list = []
        cur_size = pretrained_resnet.fc.in_features
        layers = []

        for hidden_size in hidden_size_list:
            layers.append( nn.Linear(cur_size, hidden_size, bias=False) )
            layers.append( nn.BatchNorm1d(hidden_size) )
            layers.append( nn.ReLU(True) )
            cur_size = hidden_size

        layers.append( nn.Linear(cur_size, output_size) )
        self.classifier = nn.Sequential(*layers)

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        output = self.classifier(x)
        return output

    def partial_forward(self, x):
        hidden = self.features[:6](x)
        return hidden

    def partial_forward_high(self, x):
        hidden = self.features[:8](x)
        return hidden

    def partial_forward_mid_high(self, x):
        mid = self.features[:6](x)
        high = self.features[6:8](mid)
        return mid,high

    def forward_with_mid_high(self, x):
        mid = self.features[:6](x)
        high = self.features[6:8](mid)
        output = self.features[8:](high)
        output = output.view(output.size(0), -1)
        output = self.classifier(output)

        return mid,high, output

    def forward_with_hidden(self, x):
        hidden = self.features[:6](x)
        output = self.features[6:](hidden)

        output = output.view(output.size(0), -1)
        output = self.classifier(output)

        return hidden, output

    def forward_with_high(self, x):
        hidden = self.features[:8](x)
        output = self.features[8:](hidden)

        output = output.view(output.size(0), -1)
        output = self.classifier(output)

        return hidden, output
    # 参考：https://pytorch.org/docs/master/notes/autograd.html
    def freeze(self):
        for param in self.parameters():
            param.requires_grad = False

    def unfreeze(self):
        for param in self.parameters():
            param.requires_grad = True

class Discriminator(nn.Module):

    def __init__(self):
        super(Discriminator, self).__init__()

        self.seq1 = nn.Sequential(
            nn.Conv2d(512, 256, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.AdaptiveAvgPool2d((1, 1))
        )

        self.seq2 = nn.Sequential(
            nn.Linear(256, 128),
            nn.ReLU(inplace=True),
            nn.Linear(128, 1),
            nn.Sigmoid()
        )

        initialize_weights(self.seq1)
        initialize_weights(self.seq2)

    def forward(self, x):
        x = self.seq1(x)
        x = x.view(x.size(0), -1)
        output = self.seq2(x)
        return output

class Discriminator_mid(nn.Module):

    def __init__(self):
        super(Discriminator_mid, self).__init__()

        self.seq1 = nn.Sequential(
            nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(128, 64, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.AdaptiveAvgPool2d((1, 1))
        )

        self.seq2 = nn.Sequential(
            nn.Linear(64, 1),
            nn.ReLU(inplace=True),
            nn.Sigmoid()
        )

        initialize_weights(self.seq1)
        initialize_weights(self.seq2)

    def forward(self, x):
        x = self.seq1(x)
        x = x.view(x.size(0), -1)
        output = self.seq2(x)
        return output

class Discriminator_high(nn.Module):

    def __init__(self):
        super(Discriminator_high, self).__init__()

        self.seq1 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(inplace=True),
            nn.AdaptiveAvgPool2d((1, 1))
        )

        self.seq2 = nn.Sequential(
            nn.Linear(512, 128),
            nn.ReLU(inplace=True),
            nn.Linear(128, 1),
            nn.Sigmoid()
        )

        initialize_weights(self.seq1)
        initialize_weights(self.seq2)

    def forward(self, x):
        x = self.seq1(x)
        x = x.view(x.size(0), -1)
        output = self.seq2(x)
        return output

def initialize_weights( model ):
    for m in model.modules():
        if isinstance(m, nn.Conv2d):
            nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            if m.bias is not None:
                nn.init.constant_(m.bias, 0)
        elif isinstance(m, nn.BatchNorm2d):
            nn.init.constant_(m.weight, 1)
            nn.init.constant_(m.bias, 0)
        elif isinstance(m, nn.Linear):
            nn.init.normal_(m.weight, 0, 0.01)
            nn.init.constant_(m.bias, 0)


with open("data.txt", "r") as f:
    data=f.read()
data = data.split("\n")
years=[]
co2=[]
trend=[]
co2_dict={}
trend_dict={}
for line in data:
    splitted = line.split()
    years.append(float(splitted[2]))
    co2.append(float(splitted[4]))
    trend.append(float(splitted[5]))
    co2_dict[(int(splitted[0]), int(splitted[1]))] = float(splitted[4])
    trend_dict[(int(splitted[0]), int(splitted[1]))] = float(splitted[5])

import matplotlib.pyplot as plt
"""
#Plotta bara co2:
plt.plot(years, co2, color="red", linestyle="solid", marker="o", markersize=1.5, linewidth=0.5)
plt.savefig("keeling.png")

"""
"""
#Plotta co2 och trenden:
plt.plot(years, co2, color="red", linewidth=0.5)
plt.plot(years, trend, color="blue", linewidth=0.7)
plt.savefig("keeling.png")
"""

"""
#Plotta med fler inställningar
plt.plot(years, co2, color="red", linewidth=0.5)
plt.plot(years, trend, color="blue", linewidth=0.7)
plt.xlabel("År")
plt.ylabel("ppm CO2")
plt.title("Keelingkurvan")
plt.legend(["CO2", "Trend"])
plt.grid(True)
plt.savefig("keeling.png")
"""
def avg_year(year):
    total = 0
    values = 0
    for i in range(1,13):
        if((year,i) in co2_dict):
            total += co2_dict[(year,i)]
            values += 1
    return total/values

mean_year = []
for i in range(1958,2020):
    mean_year.append(avg_year(i))

actual_years = [i+0.5 for i in range(1958, 2020)]
"""
#Plotta förskjutna årsmedelvärdet
plt.plot(years, co2, color="red", linewidth=0.5)
plt.plot(actual_years, mean_year, color="blue", linewidth=0.7)
plt.xlabel("År")
plt.ylabel("ppm CO2")
plt.title("Keelingkurvan")
plt.legend(["CO2", "Årsmedelvärde"])
plt.grid(True)
plt.savefig("year_mean.png")
"""

mean_diff = []
for i in range(1958,2018):
    diff = avg_year(i+1)-avg_year(i)
    mean_diff.append(diff)
"""
#Plotta differanserna
plt.plot(range(1958,2018), mean_diff)
plt.savefig("mean_diff.png")
"""

def plot_co2_year(year):
    co2_year = []
    trend_year = []
    for i in range(1, 13):
        co2_year.append(co2_dict[(year, i)])
        trend_year.append(trend_dict[(year, i)])
    plt.plot(range(1, 13), co2_year, color="red", linewidth=2, marker="o")
    plt.plot(range(1, 13), trend_year, color="blue", linewidth=2, marker="^")
    plt.xlabel("Månad")
    plt.ylabel("ppm CO2")
    plt.title("Keelingkurvan 2018")
    plt.legend(["CO2", "Trend"])
    plt.grid(True)
    plt.savefig("month_mean.png")

"""
#Plotta co2 under året 2018
plot_co2_year(2018)
"""


# pylint: disable=unused-argument,redefined-outer-name
import pathlib
from unittest.mock import patch

import grpc
import numpy as np
import pytest
import torch
import yaml
from modyn.common.trigger_sample import TriggerSampleStorage
from modyn.supervisor.internal.grpc_handler import GRPCHandler
from modyn.trainer_server.internal.trainer.remote_downsamplers import RemoteLossDownsampling
from modyn.utils import (
    convert_timestr_to_seconds,
    current_time_millis,
    deserialize_function,
    dynamic_module_import,
    flatten,
    get_partition_for_worker,
    grpc_connection_established,
    model_available,
    package_available_and_can_be_imported,
    seed_everything,
    trigger_available,
    validate_timestr,
    validate_yaml,
)
from modyn.utils.utils import instantiate_class

@patch.object(GRPCHandler, "init_storage", lambda self: None)
def test_connection_established_times_out():
    assert not grpc_connection_established(grpc.insecure_channel("1.2.3.4:42"), 0.5)

@patch("grpc.channel_ready_future")
def test_connection_established_works_mocked(test_channel_ready_future):
    # Pretty dumb test, needs E2E test with running server.

    class MockFuture:
        def result(self, timeout):
            return True

    test_channel_ready_future.return_value = MockFuture()
    assert grpc_connection_established(grpc.insecure_channel("1.2.3.4:42"))

def test_dynamic_module_import():
    assert dynamic_module_import("modyn.utils") is not None

def test_model_available():
    assert model_available("ResNet18")
    assert not model_available("NonExistingModel")

def test_trigger_available():
    assert trigger_available("TimeTrigger")
    assert not trigger_available("NonExistingTrigger")

def test_validate_yaml():
    with open(
        pathlib.Path("modyn") / "config" / "examples" / "modyn_config.yaml", "r", encoding="utf-8"
    ) as concrete_file:
        file = yaml.safe_load(concrete_file)
    assert validate_yaml(file, pathlib.Path("modyn") / "config" / "schema" / "modyn_config_schema.yaml")[0]
    with open(
        pathlib.Path("modyn") / "config" / "examples" / "example-pipeline.yaml", "r", encoding="utf-8"
    ) as concrete_file:
        file = yaml.safe_load(concrete_file)
    assert validate_yaml(file, pathlib.Path("modyn") / "config" / "schema" / "pipeline-schema.yaml")[0]
    assert not validate_yaml({}, pathlib.Path("modyn") / "config" / "schema" / "modyn_config_schema.yaml")[0]

@patch("time.time", lambda: 0.4)
def test_current_time_millis():
    assert current_time_millis() == 400

def test_validate_timestr():
    assert validate_timestr("10s")
    assert validate_timestr("10m")
    assert validate_timestr("10h")
    assert validate_timestr("10d")
    assert not validate_timestr("10")
    assert not validate_timestr("10x")
    assert not validate_timestr("10s10m")

def test_convert_timestr_to_seconds():
    assert convert_timestr_to_seconds("10s") == 10
    assert convert_timestr_to_seconds("10m") == 600
    assert convert_timestr_to_seconds("10h") == 36000
    assert convert_timestr_to_seconds("10d") == 864000

def test_package_available_and_can_be_imported():
    assert package_available_and_can_be_imported("modyn")
    assert not package_available_and_can_be_imported("testpackage")

def test_flatten():
    assert flatten([[1, 2, 3, 4]]) == [1, 2, 3, 4]
    assert flatten([[1, 2], [3, 4]]) == [1, 2, 3, 4]
    assert flatten([[1, 2], [3, 4], [5, 6]]) == [1, 2, 3, 4, 5, 6]

def test_get_partition_for_worker():
    samples = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

    assert get_partition_for_worker(0, 3, len(samples)) == (0, 4)
    assert get_partition_for_worker(1, 3, len(samples)) == (4, 3)
    assert get_partition_for_worker(2, 3, len(samples)) == (7, 3)

    with pytest.raises(ValueError):
        get_partition_for_worker(3, 3, len(samples))
    with pytest.raises(ValueError):
        get_partition_for_worker(-1, 3, len(samples))

    assert get_partition_for_worker(0, 2, len(samples)) == (0, 5)

    samples = [1, 2, 3]
    assert get_partition_for_worker(0, 8, len(samples)) == (0, 1)
    assert get_partition_for_worker(1, 8, len(samples)) == (1, 1)
    assert get_partition_for_worker(2, 8, len(samples)) == (2, 1)
    assert get_partition_for_worker(3, 8, len(samples)) == (0, 0)
    assert get_partition_for_worker(4, 8, len(samples)) == (0, 0)
    assert get_partition_for_worker(5, 8, len(samples)) == (0, 0)
    assert get_partition_for_worker(6, 8, len(samples)) == (0, 0)
    assert get_partition_for_worker(7, 8, len(samples)) == (0, 0)

def test_deserialize_function():
    test_func = "def test_func(x: int, y: int) -> int:\n\treturn x + y"
    assert deserialize_function(test_func, "test_func")(5, 3) == 8

    test_func_import = "import torch\ndef test_func(x: torch.Tensor):\n\treturn x * 3"

    assert torch.all(torch.eq(deserialize_function(test_func_import, "test_func")(torch.ones(4)), torch.ones(4) * 3))

def test_deserialize_function_invalid():
    test_func = "def test_func():\n\treturn 0"

    with pytest.raises(ValueError):
        deserialize_function(test_func, "test")
    deserialized_test_func = deserialize_function(test_func, "test_func")
    assert deserialized_test_func() == 0

    invalid_func = "test_func=1"
    with pytest.raises(ValueError):
        deserialize_function(invalid_func, "test_func")

    empty_func = ""
    assert deserialize_function(empty_func, "test_func") is None

def test_seed():
    seed_everything(12)
    torch_master = torch.randn(10)
    np_master = np.random.randn(10)

    seed_everything(67)
    assert not np.all(np.equal(np_master, np.random.randn(10)))
    assert not torch.equal(torch_master, torch.randn(10))

    for _ in range(23):
        seed_everything(12)
        assert torch.equal(torch_master, torch.randn(10))
        assert np.all(np.equal(np_master, np.random.randn(10)))

def test_instantiate_class_existing():
    # class with a single parameter
    trigger_storage = instantiate_class("modyn.common.trigger_sample", "TriggerSampleStorage", "test_path")
    assert isinstance(trigger_storage, TriggerSampleStorage)
    assert trigger_storage.trigger_sample_directory == "test_path"
    # class with several parameters
    remote_downsampler = instantiate_class(
        "modyn.trainer_server.internal.trainer.remote_downsamplers",
        "RemoteLossDownsampling",
        10,
        11,
        64,
        {"downsampling_ratio": 67},
        {},
        "cpu",
    )
    assert isinstance(remote_downsampler, RemoteLossDownsampling)
    assert remote_downsampler.downsampling_ratio == 67
    assert remote_downsampler.pipeline_id == 10
    assert remote_downsampler.trigger_id == 11

def test_instantiate_class_not_existing():
    # missing package
    with pytest.raises(ModuleNotFoundError):
        instantiate_class("modyn.common.rumble_db_storage", "RumbleStorage", "test_path")
    # missing class
    with pytest.raises(ValueError):
        instantiate_class("modyn.common.trigger_sample", "BeautifulAmazingStorage", "test_path")
    # missing parameters
    with pytest.raises(TypeError):
        instantiate_class("modyn.common.trigger_sample", "TriggerSampleStorage")


import data
import numpy as np
import matplotlib.pyplot as plt

train_dataset_path = "/home/bld/Download/HighD/train_data/HighD_splited_train_data.npy"
validation_dataset_path = "/home/bld/Download/HighD/val_data/HighD_splited_val_data.npy"
test_dataset_path = "/home/bld/Download/HighD/test_data/HighD_splited_test_data.npy"

historical_len = 190  # number of time step in historical car-following states
predict_len = 160  # number of time step in predicted future speed profile of self vehicle
max_len = 375
rolling_window = 160  # number of time step of a given speed trajectory to evaluate the style metric
Ts = 0.04  # sampling time step of data

dataset = data.CarFollowingData(train_dataset_path, historical_len, predict_len, max_len, Ts, rolling_window)
style_metric_list = dataset.acc_metric_list
plt.hist(style_metric_list, bins=60, color='skyblue')
plt.title('style metric value histogram')
plt.xlabel('style value')
plt.ylabel('frequency')
plt.show()


import os
import sys

import domovoi
import requests

pkg_root = os.path.abspath(os.path.join(os.path.dirname(__file__), 'domovoilib'))  # noqa
sys.path.insert(0, pkg_root)  # noqa

from dss.logging import configure_lambda_logging

configure_lambda_logging()
app = domovoi.Domovoi(configure_logs=False)

def trigger_ci_build(event, context, branch_name: str, build_type: str, **env):
    travis_token = os.environ["TRAVIS_TOKEN"]
    rule_name = event["resources"][0].split(":")[-1]
    body = {
        'request': {
            'branch': branch_name,
            'message': f"{build_type} build started by {context.function_name} from {rule_name}",
            'config': {
                'merge_mode': "deep_merge",
                'env': {
                    'matrix': [f"{k}={v}" for k, v in env.items()]
                },
            },
        }
    }
    headers = {
        'Accept': "application/json",
        'Travis-API-Version': "3",
        'Authorization': f"token {travis_token}",
    }
    return requests.post(
        "https://api.travis-ci.com/repo/HumanCellAtlas%2Fdata-store/requests",
        headers=headers,
        json=body,
    ).json()

@app.scheduled_function("cron(0 * * * ? *)")
def integration_test_on_master(event, context):
    """
    Run integration tests against the `dev` deployment stage at the top of each hour
    """
    trigger_ci_build(event, context, "master", "Integration test", TRAVIS_DSS_INTEGRATION_MODE=1)

@app.scheduled_function("cron(30 0/8 * * ? *)")
def integration_test_on_integration(event, context):
    """
    Run integration tests against the `integration` deployment stage three times a day
    """
    trigger_ci_build(event, context, "integration", "Integration test", TRAVIS_DSS_INTEGRATION_MODE=1)


import pytest
from datetime import date, timedelta, datetime
from faker import Faker
from tests.seeder.factory.data_service_factory import (
    NormalUserFactory,
)

MODEL_FACTORIES = [
    NormalUserFactory,
]

faker = Faker()

@pytest.fixture
def create_users(session, normal_user_factory):
    users = normal_user_factory.build_batch(2)

    session.add_all(users)
    session.commit()

def make_random_today_date(between_days: int = 1, year_ago: int = 2):
    """
        주어진 날의 00:00:00 ~ 23:59:59 사이의 랜덤 시간을 만든다.
        기본적으로 조회시간 기준 2년전으로 만듬
    """
    return faker.date_time_between_dates(
        datetime_start=date.today() - timedelta(days=365 * year_ago + between_days),
        datetime_end=datetime.now() - timedelta(days=365 * year_ago + between_days),
    )

def make_random_between_date(start_date, end_date):
    """주어진 날짜 사이의 랜덤 date 만든다 """
    return faker.date_time_between_dates(
        datetime_start=start_date, datetime_end=end_date
    )


from scripts.helper.utils import *
from scripts.helper.account import _from, acc
from brownie import PublicKeyChallenge

##########################################
# Recall that an address is the last 20 bytes of the keccak-256 hash of the address’s public key.
# To complete this challenge, find the public key for the owner's account.
#########################################

# solve the challenge
def solve_challenge(locally=False):
    # load challenge
    if not locally:
        public_key = load_challenge(ContractObject=PublicKeyChallenge, instance_key='public_key')
    else:
        public_key = deploy_locally(ContractObject=PublicKeyChallenge, from_account=_from)

    # get public key using the tx hash of one transaction the address in question has made
    tx = web3.eth.get_transaction('0xabc467bedd1d17462fcc7942d0af7874d6f8bdefee2b299c9168a216d3ff0edb')
    pk = get_public_key_from_tx_obj(tx, web3)

    # pass the public key to the contract
    public_key.authenticate(str(pk), _from)

    # check if the challenge is complete
    result = public_key.isComplete()
    print(result)

    # return the result
    return result

def main():
    solve_challenge()


"""empty message

Revision ID: 8c1241d2923c
Revises: cca72498e5d6
Create Date: 2022-04-23 21:45:37.984075

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

# revision identifiers, used by Alembic.
revision = '8c1241d2923c'
down_revision = 'cca72498e5d6'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('users_predicted_words',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('predict_id', sa.String(length=80), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('predict_id')
    )
    op.drop_constraint('predicted_word_ibfk_1', 'predicted_word', type_='foreignkey')
    op.drop_column('predicted_word', 'user_id')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('predicted_word', sa.Column('user_id', mysql.INTEGER(), autoincrement=False, nullable=True))
    op.create_foreign_key('predicted_word_ibfk_1', 'predicted_word', 'user', ['user_id'], ['id'])
    op.drop_table('users_predicted_words')
    # ### end Alembic commands ###


from django.contrib.auth import logout
from django.contrib.auth.views import LogoutView

from django.urls import path
from .views import add_post, create_cat, delete_post, loginfun, register_user, showdashboard, update_post

app_name = "account"
urlpatterns = [
    path('login/', loginfun, name = "login"),
    path('logout/', LogoutView.as_view(), name = "logout"),
    path('signup/', register_user, name = "signup"),
    path('dashboard/', showdashboard, name = "dash"),
    path('dashboard/addpost', add_post, name = "addpost"),
    path('dashboard/updatepost/<slug:slug>', update_post, name = "updatepost"),
    path('dashboard/deletepost/<slug:slug>', delete_post, name = "deletepost"),
    path('dashboard/addcat', create_cat.as_view(), name = "addcat"),

]


import sqlite3
import csv
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect

from . import forms

def program_of_study(request):
    if request.method == 'POST':
        form = forms.StudentForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/pos/thanks/')
    else:
        form = forms.StudentForm()
    return render(request, 'pos/pos_form.html', {'form': form})

def thanks(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="test.csv"'

    conn = sqlite3.connect("/home/bitnami/Projects/ece_pos/db.sqlite3")
    c = conn.cursor().execute("SELECT * FROM pos_student")
    writer = csv.writer(response)
    writer.writerows(c)

    return response


import numpy as np
import pandas as pd
from sklearn import ensemble
from sklearn.model_selection import cross_validate

if __name__ == '__main__':
    # Loads the Chicago crime data set
    crimes_filepath = 'D:/Users/Eric/Google Drive/Colab Notebooks/total_df.csv'
    crimes_df = pd.read_csv(crimes_filepath)

    # Drops the index column
    crimes_df.drop(['Unnamed: 0'], axis=1, inplace=True)

    # Uses the 'Arrest' feature as the target output for training
    X = crimes_df.drop(['Arrest'], axis=1)
    y = crimes_df['Arrest']

    # Trains the random forest classifier using five-fold cross validation
    k_fold = 5
    clf = ensemble.RandomForestClassifier()
    scoring = ['accuracy', 'precision', 'recall', 'f1']
    results = cross_validate(estimator=clf, X=X, y=y, scoring=scoring, cv=k_fold, n_jobs=2, verbose=0)

    # Display the scores for each fold
    for i in range(k_fold):
        print('Accuracy (Fold = {}): {}'.format(i + 1, results['test_accuracy'][i]))
        print('Precision (Fold = {}): {}'.format(i + 1, results['test_precision'][i]))
        print('Recall (Fold = {}): {}'.format(i + 1, results['test_recall'][i]))
        print('F1 Score (Fold = {}): {}\n'.format(i + 1, results['test_f1'][i]))

    # Display the average scores
    print('Accuracy (Average): {}'.format(np.mean(results['test_accuracy'])))
    print('Precision (Average): {}'.format(np.mean(results['test_precision'])))
    print('Recall (Average): {}'.format(np.mean(results['test_recall'])))
    print('F1 Score (Average): {}'.format(np.mean(results['test_f1'])))


#Implement noise filtering from a one-dimensional signal using the moving average method
import numpy as np
import matplotlib.pyplot as plt

def moving_avg(data, window_size):
  res = [np.mean(data[i-window_size:i] ) for i in range(window_size,data.shape[0]+window_size)]
  return res

x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.grid()
noise = 0.1*(np.random.rand(len(x))-0.5)
plt.plot(x, y+noise, 'b-', alpha=1)
res = moving_avg(y+noise, 10)
plt.plot(x, res, 'r-', alpha=0.8)
plt.savefig('1.png')


from base import TestKeystoneBase
import requests

class TestKeystoneEndpoints(TestKeystoneBase):
    def setUp(self):
        super(TestKeystoneEndpoints, self).setUp()
        self.url = self.host + '/v3/endpoints/'
        self.admin_auth()

    def create(self):
        body = {
            "endpoint": {
                "interface": "admin",
                "region_id": "RegionOne",
                "url": "http://localhost:8001/v3/",
                "service_id": "8c1a13ae-61a9-43d4-992d-b5544e593d06"
            }
        }
        self.res = requests.post(self.url, json = body, headers=self.headers)
        self.checkCode(201)

    def list(self):
        self.res = requests.get(self.url, headers=self.headers)
        self.checkCode(200)

    def get_info(self):
        endpoint_id = '86012e94-55c2-4938-86c7-d3a4f467a1fa'
        self.res = requests.get(self.host + endpoint_id)
        self.checkCode(200)

    def update(self):
        endpoint_id = '86012e94-55c2-4938-86c7-d3a4f467a1fa'
        body = {
            "endpoint": {
                "interface": "internal",
                "url": "http://example.com/identity/v3/endpoints/828384",
                "service_id": "3c858b69-cd97-4d60-9678-64289d6c2389"
            }
        }
        self.res = requests.patch(self.host + endpoint_id, json=body)
        self.checkCode(200)

    def delete(self):
        endpoint_id = '4cbb72e5-f9a5-4837-b943-a06bcb3bae46'
        self.res = requests.delete(self.host + endpoint_id)
        self.checkCode(204)


from urllib2 import Request, urlopen, URLError

class PQRPy(object):

    def __init__(self, key):
        self.key = key
        self.json_str = None
        self.mol2_str = None

    def json(self):
        """Make a JSON API call to PQR

        Returns a JSON string containing the data for that molecule
        """

        request = Request("http://pqr.pitt.edu/api/json/" + self.key)
        try:
            response = urlopen(request)
            read_json = response.read()
            self.json_str = read_json
            return read_json
        except:
            raise Exception("That key is invalid")

    def json_dict(self):
        import json

        if not self.json_str:
            self.json_str = self.json()
        return json.loads(self.json_str)

    def mol2(self, key):
        """Make a MOL2 API call to PQR

        Keyword arguments:
        key -- the InChIKey for the molecule

        Returns a string containing the MOL2 data for that molecule
        """

        request = Request("http://pqr.pitt.edu/api/mol/" + key)
        try:
            response = urlopen(request)
            read_mol2 = response.read()
            self.mol2_str = read_mol2
            return read_mol2
        except:
            raise Exception("That key is invalid")

    def weekly(self):
        """Make a API call to PQR to get the molecules of the week

        Returns a list of tuples, each tuple being (InChIKey, Name)
        """

        request = Request("http://pqr.pitt.edu/api/weekly/")
        try:
            response = urlopen(request)
            mol_list = response.read().split("\n")
            weekly = [tuple(x.split(",")) for x in mol_list]

            return weekly
        except URLError:
            raise Exception("Something went wrong")

    def inchikeys(self):
        """Make a API call to PQR to get all the InChIKeys PQR indexes

        Returns a list containing InChIKeys
        """

        request = Request("http://pqr.pitt.edu/api/inchikeys")
        try:
            response = urlopen(request)
            read_inchi = response.read().split("\n")

            return read_inchi
        except:
            raise Exception("Something went wrong")