from gensim.utils import simple_preprocess
import gensim.downloader as api
from gensim.corpora import Dictionary
from gensim.models import TfidfModel, WordEmbeddingSimilarityIndex
from gensim.similarities import SparseTermSimilarityMatrix, SoftCosineSimilarity

import re
from nltk.corpus import stopwords
stops = list(stopwords.words('english'))

glove = api.load("glove-wiki-gigaword-50")
similarity_index = WordEmbeddingSimilarityIndex(glove)

class GloVe:
    def __init__(self, corpus: list):
        self.corpus = corpus

    @staticmethod
    def preprocess(doc):
        '''Tokenize, clean up input document string'''
        doc = re.sub(r'<img[^<>]+(>|$)', " image_token ", doc)
        doc = re.sub(r'<[^<>]+(>|$)', " ", doc)
        doc = re.sub(r'\[img_assist[^]]*?\]', " ", doc)
        doc = re.sub(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', " url_token ", doc)
        tokens = simple_preprocess(doc, min_len=0, max_len=float("inf"))
        return [token for token in tokens if token not in stopwords]

    def process(self, query: str):
        '''Conducts preprocessing, creates BoW corpus and computes text similarity scores'''
        corpus = [self.preprocess(text) for text in self.corpus]
        dictionary = Dictionary(corpus)
        tfidf = TfidfModel(dictionary=dictionary)

        # Create the term similarity matrix.
        similarity_matrix = SparseTermSimilarityMatrix(similarity_index, dictionary, tfidf)
        query_tf = tfidf[dictionary.doc2bow(self.preprocess(query))]
        index = SoftCosineSimilarity(tfidf[[dictionary.doc2bow(document) for document in corpus]], similarity_matrix)
        scores = list(index[query_tf])
        return {i: scores[i] for i in range(len(scores))}


# !/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@Author         :  Liu Yue
@Version        :
------------------------------------
@File           :  project.py
@Description    :  项目路由
@CreateTime     :  2021/5/29, 11:44
------------------------------------
@ModifyTime     :
"""
import core
from db import models
from fastapi import APIRouter

project_router = APIRouter(tags=['项目相关'])

@project_router.post("/project/add", name="项目新增编辑")
async def update(project: models.ProjectIn_Pydantic):
    """
    - 新增和编辑项目的接口，项目名称name为必须传参数\n
    - name: 项目名称
    - 没有项目名称会新增项目信息，如果有项目名称会编辑项目信息
    """
    try:
        # 判断项目是否被删除
        is_deleted = await models.Project.filter(name=project.name, deleted=1)
        # 判断项目是否被激活
        is_status = await models.Project.filter(name=project.name, status=0)
        if is_deleted:
            return core.Fail(message="项目已被删除.")
        elif is_status:
            return core.Fail(message="项目未被激活.")
        # 创建或更新项目
        else:
            # 判断项目是否存在，存在就编辑，不存在就新增
            if await models.Project.filter(name=project.name):
                await models.Project.filter(name=project.name).update(**project.dict(exclude_unset=True))
                return core.Success(data=await models.Project_Pydantic.from_queryset_single(models.Project.get(name=project.name)))
            else:
                try:
                    project_obj = await models.Project.create(**project.dict(exclude_unset=True))
                    return core.Success(data=await models.Project_Pydantic.from_tortoise_orm(project_obj))
                except Exception as e:
                    return core.Fail(message=f"创建失败.{e}")

    except Exception as e:
        return core.Fail(message="项目已存在.")

@project_router.delete("/project/del", name="删除项目")
async def delete(project: core.NameBase):
    """
    - 删除项目的接口\n
    - name: 项目名称【必填】
    - 只需要传递需要删除的项目名称 name 字段
    """
    try:
        # 判断项目是否存在
        await models.Project.get(name=project.name)
        # 判断项目是否被删除
        is_deleted = await models.Project.filter(name=project.name, deleted=1)
        if is_deleted:
            return core.Fail(message="项目已被删除.")
        # 更新项目是否删除为1
        await models.Project.filter(name=project.name).update(deleted=1)
        return core.Success()
    except Exception as e:
        return core.Fail(message="项目不存在.")

@project_router.post("/project/res", name="恢复项目")
async def restore(project: core.NameBase):
    """
    - 恢复项目的接口\n
    - name: 项目名称【必填】
    - 只需要传递需要恢复的项目名称 name字段
    """
    try:
        # 判断项目是否存在
        await models.Project.get(name=project.name)
        # 判断项目是否被删除
        is_deleted = await models.Project.filter(name=project.name, deleted=0)
        if is_deleted:
            return core.Fail(message="项目未被删除.")
        # 更新项目是否删除为1
        await models.Project.filter(name=project.name).update(deleted=0)
        return core.Success()
    except Exception as e:
        return core.Fail(message="项目不存在.")

# https://tortoise-orm.readthedocs.io/en/latest/CHANGELOG.html?highlight=from_queryset_single#id27
@project_router.get("/project/getAll", name="获取所有项目")
async def select_all(limit: int = 10, page: int = 1):
    """
    - 获得所有项目接口\n
    - limit： 每页条数\n
    - int： 当前页面
    """
    skip = (page - 1) * limit
    # from_queryset 针对queryset 对象序列化
    data = await models.Project_Pydantic.from_queryset(models.Project.filter(deleted=0, status=1).all().order_by('-created_at').offset(skip).limit(limit))
    return core.Success(data={"total": await models.Project.all().count(), "items": data})

@project_router.post("/project/detail", name="获取项目详细")
async def select(project: models.ProjectIn_Pydantic):
    """
    - 获取指定项目名称详情接口\n
    - name: 项目名称【必填】
    """
    try:
        # 获取指定项目
        # 判断项目是否存在
        await models.Project.get(name=project.name)
        # 判断项目是否被删除
        is_deleted = await models.Project.filter(name=project.name, deleted=1)
        # 判断项目是否被激活
        is_status = await models.Project.filter(name=project.name, status=0)
        if is_deleted:
            return core.Fail(message="项目已被删除.")
        elif is_status:
            return core.Fail(message="项目未被激活.")
        else:
            try:
                data = await models.Project_Pydantic.from_queryset_single(models.Project.get(name=project.name))
                return core.Success(data=data)
            except Exception as e:
                return core.Fail(message=f"打开项目详情失败.{e}")
    except Exception as e:
        return core.Fail(message=f"项目不存在.")

@project_router.get("/project/getAllNoPage", name="获取所有项目不分页")
async def get_projects():
    """
    - 获取全部分页内容，不支持分页
    """
    # 按照不分页结构显示
    data = await models.Project_Pydantic.from_queryset(models.Project.filter(deleted=0, status=1).all())
    return core.Success(data={"total": len(data), "items": data})


from wsgiref.simple_server import demo_app
from fastapi import FastAPI
from src.routes.clustering import router_clustering
from src.routes.vizualization import router_viz
from src.models import *

app = FastAPI(
    title="Clustering App API",
    description="""FastAPI documentation for Clustering App""",
    contact ={
        'name': 'Demo App',
        'url': 'http://localhost:8501'
    },
    version="0.1.0",
)
app.include_router(router_clustering)
app.include_router(router_viz)

tags_metadata = [
    {
        'name': 'Clustering',
        'descriptions': 'Preprocessing, Dimension Reduction, Clustering'
    },
    {
        'name': 'Vizualization',
        'descriptions': 'Return vizualization images'
    }
]


"""Setup script for aaypyutil"""

import os.path

from setuptools import setup

# The directory containing this file
HERE = os.path.abspath(os.path.dirname(__file__))

# The text of the README file
with open(os.path.join(HERE, "README.md")) as fid:
    README = fid.read()

# This call to setup() does all the work
setup(
    name="aaypyutil",
    version="1.0.3",
    description="Common python util functions",
    long_description=README,
    long_description_content_type="text/markdown",
    url="https://github.com/aayushuppal/aaypyutil",
    author="Aayush Uppal",
    author_email="aayuppal@gmail.com",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 3",
    ],
    packages=["aaypyutil"],
    include_package_data=True,
    install_requires=[],
    entry_points={},
)


from keras.layers import Lambda, Conv2D, Dropout, Dense, Flatten, Concatenate, Input, MaxPooling2D
from keras.models import Model

INPUT_SHAPE, RADAR_SHAPE = (66, 200, 3), (20, 20, 1)

def main_model():
    # image model
    img_input = Input(shape=INPUT_SHAPE)
    img_model = (Lambda(lambda x: x / 127.5 - 1.0, input_shape=INPUT_SHAPE))(img_input)
    img_model = (Conv2D(24, (5, 5), activation='elu', strides=(2, 2)))(img_model)
    img_model = (Conv2D(36, (5, 5), activation='elu', strides=(2, 2)))(img_model)
    img_model = (Conv2D(48, (5, 5), activation='elu', strides=(2, 2)))(img_model)
    img_model = (Conv2D(64, (3, 3), activation='elu'))(img_model)
    img_model = (Conv2D(64, (3, 3), activation='elu'))(img_model)
    img_model = (Flatten())(img_model)
    img_model = (Dense(100, activation='elu'))(img_model)

    # radar model
    radar_input = Input(shape=RADAR_SHAPE)
    radar_model = (Conv2D(32, (5, 5), activation='elu'))(radar_input)
    radar_model = (MaxPooling2D((2, 2), strides=(2, 2)))(radar_model)
    radar_model = (Conv2D(64, (5, 5), activation='elu'))(radar_model)
    radar_model = (MaxPooling2D((2, 2), strides=(2, 2)))(radar_model)
    radar_model = (Flatten())(radar_model)
    radar_model = (Dense(10, activation='elu'))(radar_model)

    # speed
    speed_input = Input(shape=(1,))

    # combined model
    out = Concatenate()([img_model, radar_model])
    out = (Dense(50, activation='elu'))(out)
    out = Concatenate()([out, speed_input])
    out = (Dense(10, activation='elu'))(out)
    out = (Dense(1))(out)

    model = Model(inputs=[img_input, radar_input, speed_input], outputs=out)

    return model


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 15 11:30:30 2016

@author: Juan
"""

import numpy as np
import matplotlib.pyplot as plt
import scipy.io.wavfile as wav
from scipy import signal
import pandas as pd
import seaborn as sns

melcoeff = 20
melbands = 40

prediction_file_pablo="../prediction/pablo_mfcc_" + str(melcoeff) + str(melbands) + "_prediction.npy"
test_pablo=np.load(prediction_file_pablo)

prediction_file_ulla="../prediction/ulla_mfcc_" + str(melcoeff) + str(melbands) + "_prediction.npy"
test_ulla=np.load(prediction_file_ulla)

prediction_file_emma="../prediction/emma_mfcc_" + str(melcoeff) + str(melbands) + "_prediction.npy"
test_emma=np.load(prediction_file_emma)

prediction_file_juan="../prediction/juan_mfcc_" + str(melcoeff) + str(melbands) + "_prediction.npy"
test_juan=np.load(prediction_file_juan)

prediction_file_claire="../prediction/claire_mfcc_" + str(melcoeff) + str(melbands) + "_prediction.npy"
test_claire=np.load(prediction_file_claire)

gt_pablo = test_pablo[:,1]
gt_ulla = test_ulla[:,1]
gt_emma = test_emma[:,1]
gt_juan = test_juan[:,1]
gt_claire = test_claire[:,1]

#%%
N = 5
bhc = (len(np.argwhere(gt_ulla==1)), len(np.argwhere(gt_pablo==1)), \
len(np.argwhere(gt_emma==1)), len(np.argwhere(gt_juan==1)), len(np.argwhere(gt_claire==1)))
breathy = (len(np.argwhere(gt_ulla==2)), len(np.argwhere(gt_pablo==2)), \
len(np.argwhere(gt_emma==2)), len(np.argwhere(gt_juan==2)), len(np.argwhere(gt_claire==2)))
normal = (len(np.argwhere(gt_ulla==3)), len(np.argwhere(gt_pablo==3)), \
len(np.argwhere(gt_emma==3)), len(np.argwhere(gt_juan==3)), len(np.argwhere(gt_claire==3)))
ind = np.arange(N)    # the x locations for the groups
width = 0.50           # the width of the bars: can also be len(x) sequence

p1 = plt.barh(ind, bhc, width, color='r')
p2 = plt.barh(ind, breathy, width, color='y',
             left=bhc)
bottomp3=np.array(breathy)+np.array(bhc)
p3 = plt.barh(ind, normal, width, color='g', left=bottomp3)

plt.xlabel('Cantidad de frames')
plt.title('Bag of Frames de Aliento/Arrugas')
plt.yticks(ind + width/2., ('ulla', 'pablo', 'emma', 'juan', 'claire'))
plt.xticks(np.arange(0, 25000, 1000))
plt.legend((p1[0], p2[0], p3[0]), ('Blow Hole Covert', 'Breathy', 'Normal'))

plt.show()


import os
import random

import cv2
import numpy as np
from PIL import Image
import tensorflow as tf
import xml.etree.ElementTree as ET
import skimage.io as skio
import skimage.transform as skit

import hyperparameters as hp

class Datasets():
    """ Class for containing the training and test sets as well as
    other useful data-related information. Contains the functions
    for preprocessing.
    """

    def __init__(self, data_path):

        self.data_path = data_path

        # Dictionaries for (label index) <--> (class name)
        self.idx_to_class = {}
        self.class_to_idx = {}

        # For storing list of classes
        self.classes = [""] * hp.num_classes

        # Mean and std for standardization
        self.mean = np.zeros((3,))
        self.std = np.zeros((3,))
        self.calc_mean_and_std()

        # Setup data generators
        self.train_data = self.get_data(
            os.path.join(self.data_path, "Data/CLS-LOC/train/"), True, True,
            os.path.join(self.data_path, "Annotations/CLS-LOC/train/"))
        self.test_data = self.get_data(
            os.path.join(self.data_path, "Data/CLS-LOC/val/"), False, False,
            os.path.join(self.data_path, "Annotations/CLS-LOC/val/"))

    def calc_mean_and_std(self):
        """ Calculate mean and standard deviation of a sample of the
        training dataset for standardization.

        Arguments: none

        Returns: none
        """

        # Get list of all images in training directory
        file_list = []
        for root, _, files in os.walk(os.path.join(self.data_path, "train/")):
            for name in files:
                if name.endswith(".JPEG"):
                    file_list.append(os.path.join(root, name))

        # Shuffle filepaths
        random.shuffle(file_list)

        # Take sample of file paths
        file_list = file_list[:hp.preprocess_sample_size]

        # Allocate space in memory for images
        data_sample = np.zeros(
            (hp.preprocess_sample_size, hp.img_size, hp.img_size, 3))

        # Import images
        for i, file_path in enumerate(file_list):
            img = Image.open(file_path)
            img = img.resize((hp.img_size, hp.img_size))
            img = np.array(img, dtype=np.float32)
            img /= 255.

            # Grayscale -> RGB
            if len(img.shape) == 2:
                img = np.stack([img, img, img], axis=-1)

            data_sample[i] = img

        self.mean = np.mean(data_sample, axis=(0, 1, 2))
        self.std = np.std(data_sample, axis=(0, 1, 2))

        print("Dataset mean: [{0:.4f}, {1:.4f}, {2:.4f}]".format(
            self.mean[0], self.mean[1], self.mean[2]))

        print("Dataset std: [{0:.4f}, {1:.4f}, {2:.4f}]".format(
            self.std[0], self.std[1], self.std[2]))

    def standardize(self, img):
        """ Function for applying standardization to an input image.

        Arguments:
            img - numpy array of shape (image size, image size, 3)

        Returns:
            img - numpy array of shape (image size, image size, 3)
        """

        img = (img - self.mean) / self.std

        return img

    def preprocess_fn(self, img):
        """ Preprocess function for ImageDataGenerator. """

        img = img / 255.
        img = self.standardize(img)

        return img

    def get_data(self, path, shuffle, augment, annotation_path):
        """ Returns an image data generator which can be iterated
        through for images and corresponding class labels.

        Arguments:
            path - the path to the image data
            shuffle - Boolean value indicating whether the data should
                      be randomly shuffled.
            augment - Boolean value indicating whether the data should
                      be augmented or not.
            annotation_path - the path to the annotations of the image data

        Returns:
            An iterable image-batch generator
        """

        if augment:
            data_gen = tf.keras.preprocessing.image.ImageDataGenerator(
                preprocessing_function=self.preprocess_fn,
                rotation_range=35,
                horizontal_flip=True,
                shear_range=10,
                zoom_range=0.5
            )

        else:
            # Don't modify this
            data_gen = tf.keras.preprocessing.image.ImageDataGenerator(
                preprocessing_function=self.preprocess_fn)

        img_size = hp.img_size

        classes_for_flow = None

        # Make sure all data generators are aligned in label indices
        if bool(self.idx_to_class):
            classes_for_flow = self.classes  # list of empty strings of length num_classes -> this is probably fine

        # n02085620 -> folder name of chihuahuas
        folder_id = 'n02085620'
        titles_to_annotations = {}

        for direct in os.listdir(annotation_path):
            for annotation in os.listdir(os.path.join(annotation_path, direct)):
                xml_title = annotation[:-4]
                root = ET.parse(os.path.join(annotation_path, os.path.join(direct, annotation)))
                the_annotation = np.zeros(7)
                object_id = root.find('object')
                # name = object_id.find('name')
                bounding_box = object_id.find('bndbox')
                xmin = int(bounding_box.find('xmin').text)
                ymin = int(bounding_box.find('ymin').text)
                xmax = int(bounding_box.find('xmax').text)
                ymax = int(bounding_box.find('ymax').text)
                the_annotation[0] = 1
                the_annotation[1] = xmin
                the_annotation[2] = ymin
                the_annotation[3] = xmax
                the_annotation[4] = ymax
                if annotation == folder_id:
                    the_annotation[5] = 1
                    the_annotation[6] = 0
                else:
                    the_annotation[5] = 0
                    the_annotation[6] = 1
                titles_to_annotations[xml_title] = the_annotation

        annotations = []

        images = []
        for img in os.listdir(path):
            img_title = img[:-5]
            image = skio.imread(os.path.join(path, img))
            image = skit.resize(image, (hp.img_size, hp.img_size))
            images.append(image)
            annotations.append(titles_to_annotations[img_title])
        images = np.asarray(images)
        data_gen = data_gen.flow(images, annotations, batch_size=hp.batch_size)
        # Setup the dictionaries if not already done
        # if not bool(self.idx_to_class):
        #     # this whole thing is built on the names of directories being the labels
        #     #           -> we want it to be built on the object name
        #     # this gets a single xml file's object
        #     #   -> iterate through the whole directory and put these in unordered classes
        #     #       (idk what the file structure looks like)
        #     # root = ET.parse(path) -> this is the path of the label
        #     # object_id = root.find('object').find('name').text
        #     unordered_classes = []
        #     for dir_name in os.listdir(path):
        #         if os.path.isdir(os.path.join(path, dir_name)):
        #             unordered_classes.append(dir_name)  # gets labeled directory names
        #
        #     for img_class in unordered_classes:  # for each image class
        #         self.idx_to_class[data_gen.class_indices[img_class]] = img_class
        #         self.class_to_idx[img_class] = int(data_gen.class_indices[img_class])
        #         self.classes[int(data_gen.class_indices[img_class])] = img_class

        return data_gen


"""
A simple FIFO queue for jobs.
"""

import bson
import copy
import pymongo
import datetime

from .. import config
from .jobs import Job, Logs, JobTicket
from .gears import get_gear, validate_gear_config, fill_gear_default_values
from ..dao.containerutil import create_filereference_from_dictionary, create_containerreference_from_dictionary, create_containerreference_from_filereference
from ..web.errors import InputValidationException

log = config.log

JOB_STATES = [
    'pending',  # Job is queued
    'running',  # Job has been handed to an engine and is being processed
    'failed',   # Job has an expired heartbeat (orphaned) or has suffered an error
    'complete', # Job has successfully completed
    'cancelled' # Job has been cancelled (via a bulk job cancellation)
]

JOB_STATES_ALLOWED_MUTATE = [
    'pending',
    'running',
]

JOB_TRANSITIONS = [
    'pending --> running',
    'pending --> cancelled',
    'running --> cancelled',
    'running --> failed',
    'running --> complete',
]

# How many times a job should be retried
def max_attempts():
    return config.get_item('queue', 'max_retries')

# Should a job be retried when explicitly failed.
# Does not affect orphaned jobs.
def retry_on_explicit_fail():
    return config.get_item('queue', 'retry_on_fail')

def valid_transition(from_state, to_state):
    return (from_state + ' --> ' + to_state) in JOB_TRANSITIONS or from_state == to_state

class Queue(object):

    @staticmethod
    def mutate(job, mutation):
        """
        Validate and save a job mutation
        """

        if job.state not in JOB_STATES_ALLOWED_MUTATE:
            raise InputValidationException('Cannot mutate a job that is ' + job.state + '.')

        # TODO: This should use InputValidationException or similar
        if 'state' in mutation and not valid_transition(job.state, mutation['state']):
            raise Exception('Mutating job from ' + job.state + ' to ' + mutation['state'] + ' not allowed.')

        # Any modification must be a timestamp update
        mutation['modified'] = datetime.datetime.utcnow()

        # Create an object with all the fields that must not have changed concurrently.
        job_query =  {
            '_id': bson.ObjectId(job.id_),
            'state': job.state,
        }

        result = config.db.jobs.update_one(job_query, {'$set': mutation})
        if result.modified_count != 1:
            raise Exception('Job modification not saved')

        # If the job did not succeed, check to see if job should be retried.
        if 'state' in mutation and mutation['state'] == 'failed' and retry_on_explicit_fail():
            job.state = 'failed'
            Queue.retry(job)

    @staticmethod
    def retry(job, force=False):
        """
        Given a failed job, either retry the job or fail it permanently, based on the attempt number.
        Can override the attempt limit by passing force=True.
        """

        if job.attempt >= max_attempts() and not force:
            log.info('Permanently failed job %s (after %d attempts)', job.id_, job.attempt)
            return

        if job.state != 'failed':
            raise Exception('Can only retry a job that is failed')

        # Race condition: jobs should only be marked as failed once a new job has been spawned for it (if any).
        # No transactions in our database, so we can't do that.
        # Instead, make a best-hope attempt.
        check = config.db.jobs.find_one({'previous_job_id': job.id_ })
        if check is not None:
            found = Job.load(check)
            raise Exception('Job ' + job.id_ + ' has already been retried as ' + str(found.id_))

        new_job = copy.deepcopy(job)
        new_job.id_ = None
        new_job.previous_job_id = job.id_

        new_job.state = 'pending'
        new_job.attempt += 1

        now = datetime.datetime.utcnow()
        new_job.created = now
        new_job.modified = now

        new_id = new_job.insert()
        log.info('respawned job %s as %s (attempt %d)', job.id_, new_id, new_job.attempt)

        # If job is part of batch job run, update batch jobs list
        result = config.db.batch.update_one(
            {'jobs': job.id_},
            {'$pull': {'jobs': job.id_}, '$push': {'jobs': new_id}}
        )
        if result.modified_count == 1:
            log.info('updated batch job list, replacing {} with {}'.format(job.id_, new_id))

        return new_id

    @staticmethod
    def enqueue_job(job_map, origin, perm_check_uid=None):
        """
        Using a payload for a proposed job, creates and returns (but does not insert)
        a Job object. This preperation includes:
          - confirms gear exists
          - validates config against gear manifest
          - creating file reference objects for inputs
            - if given a perm_check_uid, method will check if user has proper access to inputs
          - confirming inputs exist
          - creating container reference object for destination
          - preparing file contexts
          - job api key generation, if requested

        """

        # gear and config manifest check
        gear_id = job_map.get('gear_id')
        if not gear_id:
            raise InputValidationException('Job must specify gear')

        gear = get_gear(gear_id)

        if gear is None:
            raise InputValidationException('Could not find gear ' + gear_id)

        if gear.get('gear', {}).get('custom', {}).get('flywheel', {}).get('invalid', False):
            raise InputValidationException('Gear marked as invalid, will not run!')

        config_ = fill_gear_default_values(gear, job_map.get('config', {}))
        validate_gear_config(gear, config_)

        # Translate maps to FileReferences
        inputs = {}
        for x in job_map.get('inputs', {}).keys():
            input_map = job_map['inputs'][x]
            try:
                inputs[x] = create_filereference_from_dictionary(input_map)
            except KeyError:
                raise InputValidationException('Input {} does not have a properly formatted file reference.'.format(x))

        # Add job tags, config, attempt number, and/or previous job ID, if present
        tags            = job_map.get('tags', [])
        attempt_n       = job_map.get('attempt_n', 1)
        previous_job_id = job_map.get('previous_job_id', None)
        batch           = job_map.get('batch', None) # A batch id if this job is part of a batch run

        # Add destination container, or select one
        destination = None
        if job_map.get('destination', None) is not None:
            destination = create_containerreference_from_dictionary(job_map['destination'])
        else:
            if len(inputs.keys()) < 1:
                raise InputValidationException('Must specify destination if gear has no inputs.')

            key = inputs.keys()[0]
            destination = create_containerreference_from_filereference(inputs[key])

        # Permission check
        if perm_check_uid:
            for x in inputs:
                inputs[x].check_access(perm_check_uid, 'ro')
            destination.check_access(perm_check_uid, 'rw')

        # Config options are stored on the job object under the "config" key
        config_ = {
            'config': config_,
            'inputs': { },
            'destination': {
                'type': destination.type,
                'id': destination.id,
            }
        }

        # Implementation notes: with regard to sending the gear file information, we have two options:
        #
        # 1) Send the file object as it existed when you enqueued the job
        # 2) Send the file object as it existed when the job was started
        #
        # Option #2 is possibly more convenient - it's more up to date - but the only file modifications after a job is enqueued would be from
        #
        # A) a gear finishing, and updating the file object
        # B) a user editing the file object
        #
        # You can count on neither occurring before a job starts, because the queue is not globally FIFO.
        # So option #2 is potentially more convenient, but unintuitive and prone to user confusion.

        for x in inputs:

            # Ensure input is in gear manifest
            if x not in gear['gear']['inputs']:
                raise InputValidationException('Job input {} is not listed in gear manifest'.format(x))

            input_type = gear['gear']['inputs'][x]['base']
            if input_type == 'file':

                obj = inputs[x].get_file()
                cr = create_containerreference_from_filereference(inputs[x])

                # Whitelist file fields passed to gear to those that are scientific-relevant
                whitelisted_keys = ['info', 'tags', 'measurements', 'mimetype', 'type', 'modality', 'size']
                obj_projection = { key: obj.get(key) for key in whitelisted_keys }

                config_['inputs'][x] = {
                    'base': 'file',
                    'hierarchy': cr.__dict__,
                    'location': {
                        'name': obj['name'],
                        'path': '/flywheel/v0/input/' + x + '/' + obj['name'],
                    },
                    'object': obj_projection,
                }
            else:
                # Note: API key inputs should not be passed as input
                raise Exception('Non-file input base type')

        gear_name = gear['gear']['name']

        if gear_name not in tags:
            tags.append(gear_name)

        job = Job(str(gear['_id']), inputs, destination=destination, tags=tags, config_=config_, attempt=attempt_n, previous_job_id=previous_job_id, origin=origin, batch=batch)
        job.insert()
        return job

    @staticmethod
    def start_job(tags=None, peek=False):
        """
        Atomically change a 'pending' job to 'running' and returns it. Updates timestamp.
        Will return None if there are no jobs to offer. Searches for jobs in FIFO order.

        Potential jobs must match at least one tag, if provided.
        """

        query = { 'state': 'pending' }
        if tags is not None:
            query['tags'] = {'$in': tags }

        modification = { '$set': {
            'state': 'running',
            'modified': datetime.datetime.utcnow()
        }}

        if peek:
            # placeholder noop
            modification = {'$setOnInsert': {'1': 1}}

        # Search ordering by FIFO
        result = config.db.jobs.find_one_and_update(
            query,
            modification,
            sort=[('modified', 1)],
            return_document=pymongo.collection.ReturnDocument.AFTER
        )

        if result is None:
            return None

        job = Job.load(result)

        if peek:
            gear = get_gear(job.gear_id)
            for key in gear['gear']['inputs']:
                if gear['gear']['inputs'][key] == 'api-key':
                    # API-key gears cannot be peeked
                    return None

        # Return if there is a job request already (probably prefetch)
        if job.request is not None:
            log.info('Job ' + job.id_ + ' already has a request, so not generating')
            return job

        # Create a new request formula
        request = job.generate_request(get_gear(job.gear_id))

        if peek:
            job.request = request
            return job

        # Save and return
        result = config.db.jobs.find_one_and_update(
            {
                '_id': bson.ObjectId(job.id_)
            },
            { '$set': {
                'request': request }
            },
            return_document=pymongo.collection.ReturnDocument.AFTER
        )

        if result is None:
            raise Exception('Marked job as running but could not generate and save formula')

        return Job.load(result)

    @staticmethod
    def search(containers, states=None, tags=None):
        """
        Search the queue for jobs that mention at least one of a set of containers and (optionally) match some set of states or tags.
        Currently, all containers must be of the same type.

        @param containers: an array of ContainerRefs
        @param states: an array of strings
        @param tags: an array of strings
        """

        # Limitation: container types must match.
        type1 = containers[0].type
        for container in containers:
            if container.type != type1:
                raise Exception('All containers passed to Queue.search must be of the same type')

        query = { '$or': [
            {'inputs.id': {'$in': [x.id for x in containers]}, 'inputs.type': type1},
            {'destination.id': {'$in': [x.id for x in containers]}, 'destination.type': type1},
        ]}

        if states is not None and len(states) > 0:
            query['state'] = {"$in": states}

        if tags is not None and len(tags) > 0:
            query['tags'] = {"$in": tags}

        # For now, mandate reverse-crono sort
        return config.db.jobs.find(query).sort([
            ('modified', pymongo.DESCENDING)
        ])

    @staticmethod
    def get_statistics(tags=None, last=None, unique=False, all_flag=False):
        """
        Return a variety of interesting information about the job queue.
        """

        if all_flag:
            unique = True
            if last is None:
                last = 3

        results = { }
        match = { } # match all jobs

        if tags is not None and len(tags) > 0:
            match = { 'tags': {'$in': tags } } # match only jobs with given tags

        # Count jobs by state, mapping the mongo result to a useful object
        result = list(config.db.jobs.aggregate([{'$match': match }, {'$group': {'_id': '$state', 'count': {'$sum': 1}}}]))
        by_state = {s: 0 for s in JOB_STATES}
        by_state.update({r['_id']: r['count'] for r in result})
        results['states'] = by_state

        # List unique tags
        if unique:
            results['unique'] = sorted(config.db.jobs.distinct('tags'))

        # List recently modified jobs for each state
        if last is not None:
            results['recent'] = {s: config.db.jobs.find({'$and': [match, {'state': s}]}, {'modified':1}).sort([('modified', pymongo.DESCENDING)]).limit(last) for s in JOB_STATES}

        return results

    @staticmethod
    def get_pending(tags=None):
        """
        Returns the same format as get_statistics, but only the pending number.
        Designed to be as efficient as possible for frequent polling :(
        """

        match = { } # match all jobs
        if tags is not None and len(tags) > 0:
            match = { 'tags': {'$in': tags } } # match only jobs with given tags

        return {
            'states': {
                'pending': config.db.jobs.count({'$and': [match, {'state': 'pending'}]})
            }
        }

    @staticmethod
    def scan_for_orphans():
        """
        Scan the queue for orphaned jobs, mark them as failed, and possibly retry them.
        Should be called periodically.
        """

        orphaned = 0
        query = {
            'state': 'running',
            'modified': {'$lt': datetime.datetime.utcnow() - datetime.timedelta(seconds=100)},
        }

        while True:
            orphan_candidate = config.db.jobs.find_one(query)
            if orphan_candidate is None:
                break

            # If the job is currently attempting to complete, do not orphan.
            ticket = JobTicket.find(orphan_candidate['_id'])
            if ticket is not None and len(ticket) > 0:
                continue

            # CAS this job, since it does not have a ticket
            select = { '_id': orphan_candidate['_id'] }

            doc = config.db.jobs.find_one_and_update(
                dict(query, **select),
                {
                    '$set': {
                        'state': 'failed', },
                },
                return_document=pymongo.collection.ReturnDocument.AFTER
            )

            if doc is None:
                log.info('Job %s was heartbeat during a ticket lookup and thus not orhpaned', orphan_candidate['_id'])
            else:
                orphaned += 1
                j = Job.load(doc)
                Logs.add(j.id_, [{'msg':'The job did not report in for a long time and was canceled.', 'fd':-1}])
                new_id = Queue.retry(j)
                Logs.add(j.id_, [{'msg': 'Retried job as ' + str(new_id) if new_id else 'Job retries exceeded maximum allowed', 'fd':-1}])

        return orphaned


import os
import nltk
import torch
import pickle
import hydra
import numpy as np
from PIL import Image
import torch.utils.data as data
from pycocotools.coco import COCO
from build_vocab import Vocabulary
import torchvision.transforms as transforms

class CocoDataset(data.Dataset):

    def __init__(self, root, json, vocab, transform=None):
        """
        root: 画像のrootディレクトリ
        json: cocoのannotationファイルのパス
        vocab: vocabulary wrapper
        transform: 画像の変形
        """

        self.root = root
        self.coco = COCO(json)
        self.ids = list(self.coco.anns.keys())
        self.vocab = vocab
        self.transform = transform

    def __getitem__(self, index):
        # imageとcaptionのペアを返す

        coco = self.coco
        vocab = self.vocab
        ann_id = self.ids[index]
        caption = coco.anns[ann_id]['caption']
        img_id = coco.anns[ann_id]['image_id']
        path = coco.loadImgs(img_id)[0]['file_name']

        image = Image.open(os.path.join(self.root, path)).convert('RGB')
        if self.transform is not None:
            image = self.transform(image)

        # captionの文字をワードのidxに変換する
        tokens = nltk.tokenize.word_tokenize(str(caption).lower())
        caption = []
        caption.append(vocab('<start>'))
        caption.extend([vocab(token) for token in tokens])
        caption.append(vocab('<end>'))
        target = torch.Tensor(caption)

        return image, target

    def __len__(self):
        return len(self.ids)

def collate_fn(data):
    # (image,caption)というタプルになっているリストからミニバッジのテンソルを作成する

    # dataのimageは(3,256,256)のようなtensor、captionは可変の次元

    data.sort(key=lambda x: len(x[1]), reverse=True)
    images, captions = zip(*data)

    # タプル型の3Dテンソルを4Dのテンソルにすることで画像をマージする
    images = torch.stack(images, 0)

    # タプル型の1Dテンソルを2Dのテンソルにすることでcaptionをマージする
    lengths = [len(cap) for cap in captions]
    targets = torch.zeros(len(captions), max(lengths)).long()

    for i, cap in enumerate(captions):
        end = lengths[i]
        targets[i, :end] = cap[:end]

    # images:(batch_size,3,256,256)
    # targets:(batch_size,padded_length)
    # lengths:->list captionの長さ(paddingされてるかも)
    return images, targets, lengths

def get_loader(root, json, vocab, transform, batch_size, shuffle, num_workers):
    coco = CocoDataset(root=root, json=json, vocab=vocab, transform=transform)

    data_loader = torch.utils.data.DataLoader(dataset=coco, batch_size=batch_size, shuffle=shuffle,
                                              num_workers=num_workers, collate_fn=collate_fn)

    return data_loader


import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits

def centroid(array, center = True):
    M = np.sum(array)
    if center:
        x_low = -(array.shape[1]/2)
        x_high = x_low + array.shape[1]
        y_low = -(array.shape[0]/2)
        y_high = y_low + array.shape[0]
        y_indices, x_indices = np.mgrid[y_low:y_high,x_low:x_high]
        y_indices = y_indices[::-1]
    else:
        x_indices, y_indices = np.indices(array.shape)
    x = np.sum(array*x_indices) / float(M)
    y = np.sum(array*y_indices) / float(M)
    return x, y

def solve_star(file,xcent,ycent,size,plot=True):

    hdu = fits.open(file)[0]
    image = hdu.data
    star = image[ycent-size:ycent+size,xcent-size:xcent+size]
    sky = image[ycent-size-1:ycent+size+1,xcent-size-1:xcent+size+1]
    skyimg = sky[np.where(sky!=star)]
    sky_val = np.mean(skyimg)
    subimg = star - sky_val
    flux = np.sum(subimg)
    center = centroid(subimg, center = False)
    centroid_x = xcent-size + center[0]
    centroid_y = ycent-size + center[1]
    if plot:
        fig, (ax1, ax2) = plt.subplots(1,2, figsize = (10,5))
        ax1.set_title('Full image')
        ax2.set_title('Star centroid')
        ax1.imshow(image, interpolation = 'none', vmin = 700, vmax = 5000, cmap = 'gray')
        ax2.imshow(star, interpolation = 'none', vmin = 700, vmax = 5000, cmap = 'gray')
        ax1.plot(centroid_x,centroid_y,'r+',markersize=10)
        ax2.plot(center[0],center[1],'r+',markersize=10)
        plt.show(fig)
    return center, (centroid_x, centroid_y), flux

array = np.array([[0,33,21,33,8],
                     [0,56,51,53,26],
                     [23,120,149,73,18],
                     [55,101,116,50,16],
                     [11,78,26,2,10]])

solve_star("/home/student/Yao_py/YSPA/2008NU_Spain_2017-07-13_1.fit", 228, 536, 30)


"""
URL configuration for Cargo 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

from delivery.views import LocationsListAPIView, CreateCargoAPIView, CargoListAPIView, CargoDetailAPIView, CarUpdateView, \
    CargoUpdateView, CargoDeleteView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/cargo_add/', CreateCargoAPIView.as_view(), name='create-cargo'),
    path('api/locations/', LocationsListAPIView.as_view(), name='locations-list'),
    path('api/cargo_list/', CargoListAPIView.as_view(), name='cargo-list'),
    path('api/cargo/<int:cargo_id>/', CargoDetailAPIView.as_view(), name='cargo-detail'),
    path('api/cargo_update/<int:cargo_id>/', CargoUpdateView.as_view(), name='cargo-update-weight-description'),
    path('api/cargo_delete/<int:cargo_id>/', CargoDeleteView.as_view(), name='cargo-delete'),
    path('api/car_update/<int:car_id>/', CarUpdateView.as_view(), name='car-update-location'),

]


import pymongo
from bson import objectid
from pymongo import MongoClient
import logging
import time

logger = logging.getLogger("ml_service")

class MongoDBClient():
    """Simple Client for MongoDB interaction"""

    def __init__(self, host='localhost', port=27017):
        self.client = MongoClient(host, port)
        logger.info("MongoDB is initialized at " + host + ":" + str(port))

    def read(self, db: str, collection: str, search_param: dict) -> list:
        db_connection = self.client[db]
        col = db_connection[collection]
        findings = []
        #if search params are in list, search for all the contend (not the list itself...)
        for key in search_param:
            value = search_param[key]
            if key == "tags" or key == "meta.tags":
                if isinstance(value, list):
                    search_param[key] = {"$all": value}
            if not value:  # check if key exists if no value is given
                search_param[key] = {"$exists": True}
            if key == "_id":  # if _id is given as string  ->  ObjectId
                if isinstance(search_param[key],str):
                    search_param[key] = objectid.ObjectId(search_param[key])
        retry_count = 0
        while not findings:
            for f in col.find(filter=search_param):
                f["_id"] = str(f["_id"])
                findings.append(f)
            if retry_count > 2:
                break
            else:
                retry_count += 1
                time.sleep(0.5)
        return findings

    def write(self, db: str, collection: str, document: dict or list) -> objectid.ObjectId or list:
        db_connection = self.client[db]
        col = db_connection[collection]
        if isinstance(document, list):
            # document_id = col.insert_many(document).inserted_ids
            document_ids = []
            for doc in document:
                single_id = self._write_single(db, collection, doc)
                document_ids.append(single_id)
        elif isinstance(document, dict):
            document_ids = self._write_single(db, collection, document)
        else:
            logger.error("Document type is not dict or list, but " + str(type(document)))
            logger.info("Cannot insert document into Database.")
            return False
        return document_ids

    def _write_single(self, db: str, collection: str, document: dict) -> objectid.ObjectId:
        db_connection = self.client[db]
        col = db_connection[collection]
        if isinstance(document, dict):
            if "_id" in document.keys():  # if _id is given as string  ->  ObjectId
                if isinstance(document["_id"],str):
                    document["_id"] = objectid.ObjectId(document["_id"])
            return str(col.insert_one(document).inserted_id)
        else:
            logger.error("Document type is not dict, but " + str(type(document)))
            logger.info("Cannot insert document into Database.")
            return False

    def remove(self, db: str, collection: str, search_param: dict) -> bool:
        db_connection = self.client[db]
        col = db_connection[collection]
        if "_id" in search_param.keys():  # if _id is given as string  ->  ObjectId
                if isinstance(search_param["_id"], str):
                    search_param["_id"] = objectid.ObjectId(search_param["_id"])
        for key in search_param:
            value = search_param[key]
            if key == "tags" or "meta.tags":
                if isinstance(value, list):
                    search_param[key] = {"$all": value}
        result = col.delete_many(search_param, collation=None, session=None)
        if result.deleted_count > 0:
            return True
        else:
            return False

    def update(self, db: str, collection: str, search_param: dict, new_param: dict) -> bool:
        db_connection = self.client[db]
        col = db_connection[collection]
        if isinstance(search_param, dict):
            if "_id" in search_param.keys():  # if _id is given as string  ->  ObjectId
                if isinstance(search_param["_id"], str):
                    search_param["_id"] = objectid.ObjectId(search_param["_id"])
            if isinstance(new_param, dict):
                if new_param.get("_id", False):
                    new_param.pop("_id")
                new_param_set = {"$set": new_param}
                col.update_one(search_param, new_param_set)
            else:
                logger.error("new parameter for update are not dict, but " + str(type(new_param)))
                return False
        else:
            logger.error("search parameter for update are not dict, but " + str(type(new_param)))
            return False
        return True

    def delete_collection(self, db: str, collection: str):
        db_connection = self.client[db]
        col = db_connection[collection]
        col.drop()

    def get_collections(self, db:str) -> list:
        db_connection = self.client[db]
        return db_connection.list_collection_names(filter={})


import os
import pyttsx3
from datetime import datetime
from tkinter import *
from tkinter import messagebox, filedialog
from tkinter import ttk

import matplotlib.pyplot as plt
import seaborn as sns
from PIL import ImageTk

from source_code.api.data_analysis import *

# *************** Global Style Variables ****************
bkg1 = '#1d2b2c'  # Background color
clr1 = '#0b3b40'  # Label Box color
clr2 = '#9fe3de'  # Font color
clr3 = '#74d1b6'  # Country/region screen BG color
buttons = '#0b3b40'  # Buttons Background color
ft = 'Times'  # Primary Font type
ft2 = 'courier'  # Secondary Font type
ftt = ('Times', 17),  # country & region lists font
tble_clr = '#041315'  # Table Background color
data_set = pd.read_csv('topics.csv')

root = Tk()
root.title('COVID Live Tracker')
root.configure(bg=bkg1)
root.minsize(width=1024, height=576)

home_bg_img = ImageTk.PhotoImage(file="Assets/corona_stats.jpg")
wlcm_txt = "Welcome To Your Corona Virus Stat Viewer,\nAnalyzer & " \
           "Downloader To Proceed, Please Click Next"

# ************** Add image BackGround Using Labels **************

img_label = Label(root, image=home_bg_img)
img_label.place(relx=0, rely=0, relwidth=1, relheight=1)
welcome = Label(img_label, text=wlcm_txt,
                pady=0,
                padx=0,
                justify='center',
                border=10,
                background='Teal',
                foreground='#D6EFED',
                borderwidth=2,
                font=('andalus', 25))
welcome.pack(pady=25, padx=0)

def home_next():
    """
    This function will be invoked when the 'Next' button in the (Home screen) is clicked.
    It will transition the user to the (country_region_window) by:
        1. Destroying the [Home screen]
        2. Rendering a new screen; the 'country/region input screen', containing:
            i. Instructions [Label].
            ii. Countries [OptionList].
            iii. Regions [OptionList].
            iv. 'Back' to home [Button].
            v. 'Next' to either 'country page' or 'Regions page' [Button].
    """
    country_region_window = Tk()
    country_region_window.title('Country/Region Input')
    country_region_window.configure(bg=clr3)
    country_region_window.minsize(width=1000, height=600)

    if user_name.get() == 'Please Enter Your Name':
        region_input_instruction = "Hello Sir/Ma'am\nKindly Choose a Country or a Region \nto View From The " \
                                   "Dropdown List, Then Click Next"
    else:
        region_input_instruction = 'Hello ' + user_name.get().capitalize() + "\nKindly Choose a Country or a " \
                                                                             "Region \nto View From The " \
                                                                             "Dropdown List, Then Click Next"

    r_c_instruction_label = Label(country_region_window,
                                  text=region_input_instruction,
                                  pady=50,
                                  padx=25,
                                  justify='center',
                                  border=10,
                                  background=clr3,
                                  foreground=clr1,
                                  borderwidth=2,
                                  font=('andalus', 30))

    r_c_instruction_label.pack(pady=25)

    # Should return to root (home window)
    def country_region_back():
        """
        This function will be invoked when the 'Back' button in the 'Country/Region Input' window is clicked.
        It will transition the user to either the 'Covid Live Tracker' or Home window by:
            1. Destroying the [country_region_window]
            2. Rerunning the 'Home' window:
                i. Welcome [Label].
                ii. User Name [Entry Box].
                iii. 'Next' to the 'country/Region Input' window [Button].
        """
        country_region_window.destroy()
        os.system('python main.py')

    countries = data_set['Name'].values

    clicked_country = StringVar(country_region_window)
    clicked_country.set('Choose a Country')
    country_drop_list = OptionMenu(country_region_window, clicked_country, *countries)
    country_drop_list.configure(bg=buttons, fg=clr3, font=('Times', 17))
    country_drop_list.pack(pady=15, padx=25)

    regions = [
        'Middle_East',
        'Western_Europe',
        'Other'
    ]

    clicked_region = StringVar(country_region_window)
    reg_drop_list = OptionMenu(country_region_window, clicked_region, *regions)
    reg_drop_list.configure(bg=buttons, fg=clr3, font=('Times', 17))
    clicked_region.set('Choose a Region')

    reg_drop_list.pack(pady=25, padx=25)

    def country_region_next():
        """
        This function will be invoked when the 'Next' button in the 'country/region input' screen is clicked.
        It will transition the user to either 'country window' or 'Regions window' by:
        1. Destroying the 'country/region input' screen
        2. Rendering a new screen containing:
            i. Table viewing the requested data [TreeView].
            ii. Download [Button].
            iii. Visualize [Button].
            iv. Show Report [Button].
            v. 'Back' to home [Button].
        """

        # --------------- start country_region_window  ---------------
        if clicked_country.get() == 'Israel':
            messagebox.showinfo(title="OOPS!", message="You must mean Palestine")
            country_region_window.destroy()

        if clicked_country.get() != 'Choose a Country':
            country_region_window.destroy()
            # ---------------  start country page ---------------

            country_page = Tk()
            country_page.title('Country View')
            country_page.configure(bg=bkg1)
            country_page.minsize(width=800, height=400)

            # --------------- country Table ---------------

            def dload_data():
                """
                This function will be invoked when the 'Download' button in the 'country/region' screen is clicked.
                It will open a dialog box allowing the user to specify a directory for their download.
                The downloaded file if of the [.csv] format, an excel table.
                """
                path = filedialog.askdirectory()
                selected_country = data_set[data_set.Name == clicked_country.get()]

                selected_df = pd.DataFrame(selected_country)
                selected_df.to_csv(f'{path}.csv')
                messagebox.showinfo(title=f'{clicked_country.get()} Download',
                                    message='Your Data Has been downloaded successfully')

            def back_to_country_region_from_country_function():
                """
                This function will be invoked when the 'Back' button in the 'Country' window is clicked.
                It will transition the user to the (country_region_window) by:
                    1. Destroying the 'Country' window
                    2. Rendering a new screen; the 'country/region input screen', containing:
                        i. Instructions [Label].
                        ii. Countries [OptionList].
                        iii. Regions [OptionList].
                        iv. 'Back' to home [Button].
                        v. 'Next' to either 'country page' or 'Regions page' [Button].
                    """
                country_page.destroy()
                import country_region

            def visualize_data():
                """
                This function will be invoked when the 'Visualize' button in the 'country' or 'region' screen is clicked
                It will show a figure box showing a visual that can be customized or downloaded as a [.PNG] file
                """
                TC = data_set[data_set.Name == clicked_country.get()].iloc[0]['TotalDeaths']
                TD = data_set[data_set.Name == clicked_country.get()].iloc[0]['survivors']

                labels = 'TotalDeaths', 'survivors'
                sizes = [TC, TD]
                explode = (0, 0)
                fig1, ax1 = plt.subplots()
                ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.0f%%',
                        shadow=True, startangle=90)
                ax1.axis('equal')
                plt.show()

            def display_data():
                """
                This function renders the table included in the 'Country' and Window.
                """
                data_set = pd.read_csv('topics.csv')
                data_set = data_set[data_set.Name == clicked_country.get()]

                # ************** Start Styling Table **************
                style = ttk.Style()
                style.theme_use('clam')
                style.configure("Treeview",
                                background=buttons,
                                foreground=clr2,
                                rowheight=30,
                                fieldbackground=buttons)
                # ************** End Styling Table **************
                tree = ttk.Treeview(country_page, padding=5, height=3, selectmode='browse')
                scrollbar = Scrollbar(country_page, orient="horizontal", width=20)
                tree.config(xscrollcommand=scrollbar.set)
                scrollbar.config(command=tree.xview)

                tree["column"] = list(data_set.columns)
                tree["show"] = "headings"
                for col in tree["column"]:
                    tree.heading(col, text=col)

                df_rows = data_set.to_numpy().tolist()
                for row in df_rows:
                    tree.insert("", "end", values=row)

                tree.pack()
                scrollbar.pack(fill='both')

            display_data()

            def display_report():
                """
                This function will be invoked when the 'Show Report' button in
                the 'country' screen is clicked.
                It will show a message box with a report based on an analysis of the data viewed in the
                tree view.
                This box will include:
                    1. 'No' button, to close the message box.
                    2. 'Yes' button to read the viewed report employing a text-to-speech feature.
                """
                selected_country = data_set[data_set.Name == clicked_country.get()]
                name = selected_country['Name'].values[0]
                total_cases = selected_country['TotalConfirmed'].values[0]
                total_deaths = selected_country['TotalDeaths'].values[0]
                new_confirmed = selected_country['NewConfirmed'].values[0]
                new_deaths = selected_country['NewDeaths'].values[0]

                if selected_country['NewConfirmed'].values[0] > 0:
                    bad_country_msg = f"Today is {datetime.now().strftime('%A, ' + '%Y-%m-%d')}. As of" \
                                      f" {datetime.now().strftime('%H:%M%p')}:\nFor your own safety, try to " \
                                      f"avoid visiting {name} as {new_confirmed} new cases of COVID 19 infections" \
                                      f" have been confirmed with {new_deaths} new deaths, raising the total " \
                                      f"number of infections to {total_cases} cases, and total death toll " \
                                      f"to {total_deaths} since the pandemic broke out"

                    def read_bad_country_report():
                        """
                        This function will be invoked when the 'Yes' button in the report message box is clicked.
                        It will read the text of the country report using methods of the 'pyttsx3' library.
                        """
                        box = messagebox.askquestion(title=f'{clicked_country.get()}  Report',
                                                     message=bad_country_msg + '\n\nRead Report?')
                        if box == 'yes':
                            txt_to_speech = pyttsx3.init()
                            txt_to_speech.setProperty("rate", 135)

                            txt_to_speech.say(bad_country_msg)
                            txt_to_speech.runAndWait()

                    read_bad_country_report()

                else:
                    good_country_message = f'Today is  {datetime.now().strftime("%A, " + "%Y-%m-%d")}. As ' \
                                           f'of {datetime.now().strftime("%H:%M%p")}: \nThere' \
                                           f' are no new confirmed cases of COVID infection' \
                                           f' in {name} \nEnjoy your time there'

                    def read_good_country_report():
                        """
                        This function will be invoked when the 'Yes' button in the report message box is clicked.
                        It will read the text of the country report using methods of the 'pyttsx3' library.
                        """
                        box = messagebox.askquestion(title=f'{clicked_country.get()}  Report',
                                                     message=good_country_message + " ^^ " + '\n\nRead Report?')
                        if box == 'yes':
                            txt_to_speech = pyttsx3.init()
                            txt_to_speech.setProperty("rate", 135)

                            txt_to_speech.say(good_country_message)
                            txt_to_speech.runAndWait()

                    read_good_country_report()

            rep_btn = Button(country_page,
                             text='Show Report',
                             bg=buttons,
                             height=2,
                             width=15,
                             foreground=clr2,
                             font=(ft, 15),
                             justify='center',
                             command=display_report
                             )
            rep_btn.pack(side='right', padx=50)

            vis_btn = Button(country_page,
                             text='Visualize Data',
                             bg=buttons,
                             height=2,
                             width=15,
                             foreground=clr2,
                             font=(ft, 15),
                             justify='center',
                             command=visualize_data
                             )
            vis_btn.pack(side='right', pady=50)

            back_country_button = Button(country_page,
                                         text='Back',
                                         bg=buttons,
                                         height=2,
                                         width=15,
                                         foreground=clr2,
                                         font=(ft, 15),
                                         justify='center',
                                         command=back_to_country_region_from_country_function
                                         )
            back_country_button.pack(side='left')

            dl_btn = Button(country_page, text='Download',
                            bg=buttons,
                            height=2,
                            width=15,
                            foreground=clr2,
                            font=(ft, 15),
                            justify='center',
                            command=dload_data
                            )
            dl_btn.pack(padx=50, side='right')
            country_page.mainloop()

            # --------------- end country page ---------------

        elif clicked_region.get() != 'Choose a Region':
            country_region_window.destroy()
            # --------------- start region page ---------------

            region_page = Tk()
            region_page.title('Region View')
            region_page.configure(bg=bkg1)
            region_page.minsize(width=900, height=600)

            def display_region_data():
                """
                This function renders the table included in the 'Region' Window.
                """
                data_set = pd.read_csv('topics.csv')
                selected_region = clicked_region.get()
                data_set = data_set[data_set.Region == selected_region]

                # ************** Start Styling Table **************
                style = ttk.Style()
                style.theme_use('clam')
                style.configure("Treeview",
                                background=buttons,
                                foreground=clr2,
                                rowheight=30,
                                fieldbackground=buttons)
                # ************** End Styling Table **************
                tree = ttk.Treeview(region_page, padding=5, height=14, selectmode='browse')
                scrollbar = Scrollbar(region_page, orient="horizontal", width=20)
                tree.config(xscrollcommand=scrollbar.set)
                scrollbar.config(command=tree.xview)

                tree["column"] = list(data_set.columns)
                tree["show"] = "headings"
                for col in tree["column"]:
                    tree.heading(col, text=col)

                df_rows = data_set.to_numpy().tolist()
                for row in df_rows:
                    tree.insert("", "end", values=row)

                tree.pack()
                scrollbar.pack(fill='both')

            display_region_data()

            def back_to_country_region_from_region_function():
                """
                This function will be invoked when the 'Back' button in the 'Region' window is clicked.
                It will transition the user to the (country_region_window) by:
                    1. Destroying the 'Region' data window
                    2. Rendering a new screen; the 'country/region input screen', containing:
                        i. Instructions [Label].
                        ii. Countries [OptionList].
                        iii. Regions [OptionList].
                        iv. 'Back' to home [Button].
                        v. 'Next' to either 'country page' or 'Regions page' [Button].
                    """

                region_page.destroy()
                import country_region

            def visualize_region_data():
                """
                This function will be invoked when the 'Visualize' button in the or 'region' screen is clicked.
                It will show a figure box showing a visual that can be customized or downloaded as a [.PNG] file
                """
                data_set = pd.read_csv('topics.csv')
                selected_region = clicked_region.get()
                data_set = data_set[data_set.Region == selected_region].head(19).sort_values('Fatality_ratio',
                                                                                ascending=False)

                sns.barplot(data=data_set, x='Fatality_ratio', y='Name', ci=None)
                plt.xticks(rotation=90)
                plt.show()

            def dload_region_data():
                """
                This function will be invoked when the 'Download' button in the or 'region' screen is clicked.
                It will open a dialog box allowing the user to specify a directory for their download.
                The downloaded file if of the [.csv] format, an excel table.
                """
                data_set = pd.read_csv('topics.csv')
                selected_region = clicked_region.get()
                data_set = data_set[data_set.Region == selected_region]
                path = filedialog.askdirectory()

                selected_r_df = pd.DataFrame(data_set)
                selected_r_df.to_csv(f'{path}.csv')
                messagebox.showinfo(title=f'{selected_region} Download',
                                    message='Your Data Has been downloaded successfully')

            def display_region_report():
                """
                This function will be invoked when the 'Show Report' button in
                the 'region' screen is clicked.
                It will show a message box with a report based on an analysis of the region data viewed in the
                tree view.
                This box will include:
                    1. 'No' button, to close the message box.
                    2. 'Yes' button to read the viewed report employing a text-to-speech feature.
                """
                data_set = pd.read_csv('topics.csv')
                selected_region = clicked_region.get()
                data_set = data_set[data_set.Region == selected_region]
                minimum = data_set['Fatality_ratio'].min()
                maximum = data_set['Fatality_ratio'].max()
                avg = round((data_set['TotalDeaths'].mean()), 2)
                survivors = round(((data_set['survivors'] / data_set['TotalConfirmed'] * 100).mean()), 2)
                n_min = data_set[(data_set['Fatality_ratio'] == minimum)]['Name'].values[0]
                n_max = data_set[(data_set['Fatality_ratio'] == maximum)]['Name'].values[0]
                region = selected_region.replace("_", " ")
                region_msg = f"Today is {datetime.now().strftime('%A, ' + '%Y-%m-%d')}. As" \
                             f" of {datetime.now().strftime('%H:%M%p')}: \nWith a fatality rate of {minimum}% ," \
                             f" {n_min} is the {region.replace('_', ' ')}'s safest travel destination.\nOn the " \
                             f"other hand, avoid traveling to {n_max} for your own safety, as the fatality " \
                             f"rate stands at {maximum}% right now. \nThe average of death cases in " \
                             f"{region.replace('_', ' ')} region is {avg}\n with a survival rate of {survivors}% "

                def read_region_report():
                    """
                    This function will be invoked when the 'Yes' button in the region report message box is clicked.
                    It will read the text of the region report using methods of the 'pyttsx3' library.
                    """
                    box = messagebox.askquestion(title=f'{selected_region.replace("_", " ")} Report',
                                                 message=region_msg + '\n\nRead Report?')
                    if box == 'yes':
                        txt_to_speech = pyttsx3.init()
                        txt_to_speech.setProperty("rate", 135)

                        txt_to_speech.say(region_msg)
                        txt_to_speech.runAndWait()

                read_region_report()

            rep_btn = Button(region_page,
                             text='Show Report',
                             bg=buttons,
                             height=2,
                             width=15,
                             foreground=clr2,
                             font=(ft, 15),
                             justify='center',
                             command=display_region_report
                             )
            rep_btn.pack(side='right', padx=50)

            vis_btn = Button(region_page,
                             text='Visualize Data',
                             bg=buttons,
                             height=2,
                             width=15,
                             foreground=clr2,
                             font=(ft, 15),
                             justify='center',
                             command=visualize_region_data
                             )
            vis_btn.pack(side='right')

            back_region_button = Button(region_page,
                                        text='Back',
                                        bg=buttons,
                                        height=2,
                                        width=15,
                                        foreground=clr2,
                                        font=(ft, 15),
                                        justify='center',
                                        command=back_to_country_region_from_region_function
                                        )
            back_region_button.pack(side='left')

            dl_btn = Button(region_page, text='Download',
                            bg=buttons,
                            height=2,
                            width=15,
                            foreground=clr2,
                            font=(ft, 15),
                            justify='center',
                            command=dload_region_data
                            )
            dl_btn.pack(padx=50, side='right')

            region_page.mainloop()

            # --------------- end region page --------------------

        else:
            messagebox.showinfo(title='ERROR', message='Please Select a Country or a Region! ')

    next_country_region_window_button = Button(country_region_window,
                                               text='Next',
                                               command=country_region_next,
                                               bg=buttons,
                                               height=2,
                                               width=15,
                                               foreground=clr2,
                                               font=ftt,
                                               justify='center')

    next_country_region_window_button.pack(side='right', pady=50, padx=75)

    back_country_region_window_button = Button(country_region_window,
                                               text='Back',
                                               command=country_region_back,
                                               bg=buttons,
                                               height=2,
                                               width=15,
                                               foreground=clr2,
                                               font=ftt,
                                               justify='center')

    back_country_region_window_button.pack(side='left', pady=50, padx=75)

    root.destroy()

    country_region_window.mainloop()
    # --------------- end country_region_window  ---------------

user_name = Entry(img_label, width=25,
                  font=(ft, 20),
                  bg=clr1,
                  bd=2.5,
                  foreground=clr2,
                  justify='center',
                  borderwidth=5
                  )

user_name.place(x=512, y=365)
user_name.insert(0, 'Please Enter Your Name')

next_button = Button(img_label,
                     text='Next',
                     command=home_next,
                     bg=buttons,
                     height=2,
                     width=15,
                     foreground=clr2,
                     font=('serif bold', 15),
                     justify='center'
                     )

next_button.place(x=610, y=455)

if __name__ == '__main__':
    root.mainloop()


import time
import sys
import termios

from building import Building, Townhall
from building import Canon
from building import Hut
from building import Wall
from barbarians import Barbarians
from kingclass import King
from input import Get, input_to

import colorama
from colorama import Back, Style
import os
colorama.init(autoreset=True)

columns = 80
rows = 40

hutsCoordinates = [[8,2],[13,60],[35,73],[7,15],[37,72]]
canonPositions = [[20,60],[35,20]]
victory = ['v','i','c','t','o','r','y','!']
defeat = ['d','e','f','e','a','t',':','(']

class Game:
    def __init__(self):
        self.color = Back.LIGHTGREEN_EX + " " + Style.RESET_ALL
        self.colorArray= [[self.color for _ in range(columns)] for _ in range(rows)]
        self.idArray = [[0 for _ in range(columns)] for _ in range(rows)]

        self.townhall = Townhall()
        self.king = King()

        self.rageTime = -1
        self.healTime = time.time()

        for i in range(5):
            hut = Hut(hutsCoordinates[i][0],hutsCoordinates[i][1])
            self.colorArray = hut.render(self.colorArray)

        for i in range(2):
            canon = Canon(canonPositions[i][0],canonPositions[i][1])
            self.colorArray = canon.render(self.colorArray)

        for i in (18,23):
            for j in range(6):
                Wall(i,int(columns/2)-2+j)

        for i in range(3):
            for j in (38,43):
                Wall(int(rows/2)-1+i,j)

    def board(self,rows,columns):
        self.colorArray = [[self.color for _ in range(columns)] for _ in range(rows)]
        self.colorArray = [[self.color for _ in range(columns)] for _ in range(rows)]
        self.idArray = [[0 for _ in range(columns)] for _ in range(rows)]

    def spawn(self, key):
        if(key == 'z'):
            Barbarians(38,22)
        if(key == 'c'):
            Barbarians(17,65)
        if(key == 'v'):
            Barbarians(35,65)

    def rageStart(self):
        self.color = Back.LIGHTMAGENTA_EX + " " + Style.RESET_ALL
        self.king.velocity = 2
        Barbarians.velocity = 2
        self.townhall.health = 10 - 2*(10 - self.townhall.health)
        for hut in Hut.hutsList:
            hut.health = 10 - 2*(10 - hut.health)
        for canon in Canon.canonsList:
            canon.health = 10 - 2*(10 - canon.health)

    def rageEnd(self):
        self.color = Back.LIGHTGREEN_EX + " " + Style.RESET_ALL
        self.king.velocity = 1
        Barbarians.velocity = 1

    def healStart(self):
        self.color = Back.LIGHTYELLOW_EX + " " + Style.RESET_ALL

        self.townhall.health = (3/2) * self.townhall.health
        for hut in Hut.hutsList:
            hut.health = (3/2)*hut.health
            if(hut.health>10):
                hut.health = 10
        for canon in Canon.canonsList:
            canon.health = (3/2)*canon.health
            if(canon.health>10):
                canon.health = 10

    def healEnd(self):
        self.color = Back.LIGHTGREEN_EX + " " + Style.RESET_ALL

    def narikey(self):
        for building in Building.buildingList:
            if isinstance(building, Townhall):
                continue

            if(abs(building.coordinates[0] - self.king.coordinates[0]) < 5 and
               abs(building.coordinates[1] - self.king.coordinates[1]) < 5):
                building.health = building.health - 5

    def checkVictory(self):
        check = False
        if(self.townhall.death == True):
            check = True
        else:
            return False
        for hut in Hut.hutsList:
            if(hut.death == True):
                check = True
            else:
                return False
        for canon in Canon.canonsList:
            if(canon.death == True):
                check = True
            else:
                return False
        return check

    def checkDefeat(self):
        check = False
        for barbarian in Barbarians.barbariansList:
            if(barbarian.death == True):
                check = True
            else:
                return False
        if(self.king.death == True):
            check = True
        else:
            return False
        return check

    def render(self):
        key = input_to(Get())
        os.system('clear')

        self.spawn(key)
        if(key == 'r'):
            self.rageTime = time.time()
            self.rageStart()
        if(time.time() - self.rageTime>=5):
            self.rageEnd()
            self.rageTime = time.time()

        if(key == 'h'):
            self.healTime = time.time()
            self.healStart()
        if(time.time() - self.healTime >= 5):
            self.healEnd()

        if(key == 'x'):
            self.narikey()

        if(key == ' '):
            self.king.attack(self, self.townhall, Hut, Canon, Wall)

        # clear board
        self.board(rows, columns)

        # render all buildings
        for building in Building.buildingList:
            self.colorArray = building.render(self.colorArray)
            self.idArray = building.idUpdate(self.idArray)

        self.king.move(key, self.idArray)
        self.colorArray = self.king.render(self.colorArray)
        self.idArray = self.king.idUpdate(self.idArray)

        for cannon in Canon.canonsList:
            cannon.shoot(self.king, Barbarians)

        for barbarian in Barbarians.barbariansList:
            barbarian.choose(Building)
            barbarian.move(self.idArray)
            self.colorArray = barbarian.render(self.colorArray)
            self.idArray = barbarian.idUpdate(self.idArray)

        if(self.checkVictory()==True):
            self.colorArray= [[Back.LIGHTGREEN_EX + " " + Style.RESET_ALL for _ in range(columns)]for _ in range(rows)]
            for i in range(8):
                self.colorArray[int(rows/2)-1][int(columns/2)-4+i] = Back.GREEN + victory[i] + Style.RESET_ALL
            return False
        if(self.checkDefeat()==True):
            self.colorArray= [[Back.LIGHTRED_EX + " " + Style.RESET_ALL for i in range(columns)]for j in range(rows)]
            for i in range(8):
                self.colorArray[int(rows/2)-1][int(columns/2)-4+i] = Back.LIGHTRED_EX + defeat[i] + Style.RESET_ALL
            return False # originally it didnt close

        print("\n".join(["".join(row) for row in self.colorArray]))
        return True

game = Game()
while game.render():
    pass


import apache_beam as beam
from apache_beam.io import tfrecordio
from apache_beam.metrics import Metrics
import cStringIO
import logging
import os
from PIL import Image

from . import _inceptionlib
from . import _util

error_count = Metrics.counter('main', 'errorCount')
rows_count = Metrics.counter('main', 'rowsCount')
skipped_empty_line = Metrics.counter('main', 'skippedEmptyLine')
embedding_good = Metrics.counter('main', 'embedding_good')
embedding_bad = Metrics.counter('main', 'embedding_bad')
incompatible_image = Metrics.counter('main', 'incompatible_image')
invalid_uri = Metrics.counter('main', 'invalid_file_name')
unlabeled_image = Metrics.counter('main', 'unlabeled_image')

class ExtractLabelIdsDoFn(beam.DoFn):
  """Extracts (uri, label_ids) tuples from CSV rows.
  """

  def start_bundle(self, context=None):
    self.label_to_id_map = {}

  def process(self, element, all_labels):
    all_labels = list(all_labels)
    # DataFlow cannot garuantee the order of the labels when materializing it.
    # The labels materialized and consumed by training may not be with the same order
    # as the one used in preprocessing. So we need to sort it in both preprocessing
    # and training so the order matches.
    all_labels.sort()
    if not self.label_to_id_map:
      for i, label in enumerate(all_labels):
        label = label.strip()
        if label:
          self.label_to_id_map[label] = i

    # Row format is:
    # image_uri,label_id
    if not element:
      skipped_empty_line.inc()
      return

    rows_count.inc()
    uri = element['image_url']

    try:
      label_id = self.label_to_id_map[element['label'].strip()]
    except KeyError:
      unlabeled_image.inc()
    yield uri, label_id

class ReadImageAndConvertToJpegDoFn(beam.DoFn):
  """Read files from GCS and convert images to JPEG format.

  We do this even for JPEG images to remove variations such as different number
  of channels.
  """

  def process(self, element):
    from tensorflow.python.lib.io import file_io as tf_file_io

    uri, label_id = element
    try:
      with tf_file_io.FileIO(uri, 'r') as f:
        img = Image.open(f).convert('RGB')
    # A variety of different calling libraries throw different exceptions here.
    # They all correspond to an unreadable file so we treat them equivalently.
    # pylint: disable broad-except
    except Exception as e:
      logging.exception('Error processing image %s: %s', uri, str(e))
      error_count.inc()
      return

    # Convert to desired format and output.
    output = cStringIO.StringIO()
    img.save(output, 'jpeg')
    image_bytes = output.getvalue()
    yield uri, label_id, image_bytes

class EmbeddingsGraph(object):
  """Builds a graph and uses it to extract embeddings from images.
  """

  # These constants are set by Inception v3's expectations.
  WIDTH = 299
  HEIGHT = 299
  CHANNELS = 3

  def __init__(self, tf_session, checkpoint_path):
    import tensorflow as tf

    self.tf_session = tf_session
    # input_jpeg is the tensor that contains raw image bytes.
    # It is used to feed image bytes and obtain embeddings.
    self.input_jpeg, self.embedding = self.build_graph()
    self.tf_session.run(tf.global_variables_initializer())
    self.restore_from_checkpoint(checkpoint_path)

  def build_graph(self):
    """Forms the core by building a wrapper around the inception graph.

      Here we add the necessary input & output tensors, to decode jpegs,
      serialize embeddings, restore from checkpoint etc.

      To use other Inception models modify this file. Note that to use other
      models beside Inception, you should make sure input_shape matches
      their input. Resizing or other modifications may be necessary as well.
      See tensorflow/contrib/slim/python/slim/nets/inception_v3.py for
      details about InceptionV3.

    Returns:
      input_jpeg: A tensor containing raw image bytes as the input layer.
      embedding: The embeddings tensor, that will be materialized later.
    """

    import tensorflow as tf
    input_jpeg = tf.placeholder(tf.string, shape=None)
    image = tf.image.decode_jpeg(input_jpeg, channels=self.CHANNELS)

    # Note resize expects a batch_size, but we are feeding a single image.
    # So we have to expand then squeeze.  Resize returns float32 in the
    # range [0, uint8_max]
    image = tf.expand_dims(image, 0)

    # convert_image_dtype also scales [0, uint8_max] -> [0 ,1).
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    image = tf.image.resize_bilinear(
        image, [self.HEIGHT, self.WIDTH], align_corners=False)

    # Then rescale range to [-1, 1) for Inception.
    image = tf.subtract(image, 0.5)
    inception_input = tf.multiply(image, 2.0)

    # Build Inception layers, which expect a tensor of type float from [-1, 1)
    # and shape [batch_size, height, width, channels].
    with tf.contrib.slim.arg_scope(_inceptionlib.inception_v3_arg_scope()):
      _, end_points = _inceptionlib.inception_v3(inception_input, is_training=False)

    embedding = end_points['PreLogits']
    return input_jpeg, embedding

  def restore_from_checkpoint(self, checkpoint_path):
    """To restore inception model variables from the checkpoint file.

       Some variables might be missing in the checkpoint file, so it only
       loads the ones that are avialable, assuming the rest would be
       initialized later.
    Args:
      checkpoint_path: Path to the checkpoint file for the Inception graph.
    """
    import tensorflow as tf
    # Get all variables to restore. Exclude Logits and AuxLogits because they
    # depend on the input data and we do not need to intialize them from
    # checkpoint.
    all_vars = tf.contrib.slim.get_variables_to_restore(
        exclude=['InceptionV3/AuxLogits', 'InceptionV3/Logits', 'global_step'])

    saver = tf.train.Saver(all_vars)
    saver.restore(self.tf_session, checkpoint_path)

  def calculate_embedding(self, batch_image_bytes):
    """Get the embeddings for a given JPEG image.

    Args:
      batch_image_bytes: As if returned from [ff.read() for ff in file_list].

    Returns:
      The Inception embeddings (bottleneck layer output)
    """
    return self.tf_session.run(
        self.embedding, feed_dict={self.input_jpeg: batch_image_bytes})

class TFExampleFromImageDoFn(beam.DoFn):
  """Embeds image bytes and labels, stores them in tensorflow.Example.

  (uri, label_ids, image_bytes) -> (tensorflow.Example).

  Output proto contains 'label', 'image_uri' and 'embedding'.
  The 'embedding' is calculated by feeding image into input layer of image
  neural network and reading output of the bottleneck layer of the network.

  Attributes:
    image_graph_uri: an uri to gcs bucket where serialized image graph is
                     stored.
  """

  def __init__(self, checkpoint_path):
    self.tf_session = None
    self.graph = None
    self.preprocess_graph = None
    self._checkpoint_path = checkpoint_path

  def start_bundle(self, context=None):
    # There is one tensorflow session per instance of TFExampleFromImageDoFn.
    # The same instance of session is re-used between bundles.
    # Session is closed by the destructor of Session object, which is called
    # when instance of TFExampleFromImageDoFn() is destructed.
    import tensorflow as tf
    if not self.graph:
      self.graph = tf.Graph()
      self.tf_session = tf.InteractiveSession(graph=self.graph)
      with self.graph.as_default():
        self.preprocess_graph = EmbeddingsGraph(self.tf_session, self._checkpoint_path)

  def finish_bundle(self, context=None):
    if self.tf_session is not None:
      self.tf_session.close()

  def process(self, element):

    import tensorflow as tf

    def _bytes_feature(value):
      return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))

    def _float_feature(value):
      return tf.train.Feature(float_list=tf.train.FloatList(value=value))

    uri, label_id, image_bytes = element

    try:
      embedding = self.preprocess_graph.calculate_embedding(image_bytes)
    except tf.errors.InvalidArgumentError as e:
      incompatible_image.inc()
      logging.warning('Could not encode an image from %s: %s', uri, str(e))
      return

    if embedding.any():
      embedding_good.inc()
    else:
      embedding_bad.inc()

    example = tf.train.Example(features=tf.train.Features(feature={
        'image_uri': _bytes_feature([str(uri)]),
        'embedding': _float_feature(embedding.ravel().tolist()),
    }))

    example.features.feature['label'].int64_list.value.append(label_id)

    yield example

class TrainEvalSplitPartitionFn(beam.PartitionFn):
  """Split train and eval data."""
  def partition_for(self, element, num_partitions):
    import random
    return 1 if random.random() > 0.7 else 0

class ExampleProtoCoder(beam.coders.Coder):
  """A coder to encode and decode TensorFlow Example objects."""

  def encode(self, example_proto):
    return example_proto.SerializeToString()

  def decode(self, serialized_str):
    import tensorflow as tf
    example = tf.train.Example()
    example.ParseFromString(serialized_str)
    return example

class SaveFeatures(beam.PTransform):
  """Save Features in a TFRecordIO format.
  """

  def __init__(self, file_path_prefix):
    super(SaveFeatures, self).__init__('SaveFeatures')
    self._file_path_prefix = file_path_prefix

  def expand(self, features):
    return (features |
            'Write to %s' % self._file_path_prefix.replace('/', '_') >>
            tfrecordio.WriteToTFRecord(file_path_prefix=self._file_path_prefix,
                                       file_name_suffix='.tfrecord.gz',
                                       coder=ExampleProtoCoder()))

def _labels_pipeline(sources):
  labels = (sources |
            'Flatten Sources for labels' >> beam.Flatten() |
            'Parse input for labels' >> beam.Map(lambda x: str(x['label'])) |
            'Combine labels' >> beam.transforms.combiners.Count.PerElement() |
            'Get labels' >> beam.Map(lambda label_count: label_count[0]))
  return labels

def _transformation_pipeline(source, checkpoint, labels, mode):
  transformed = (source |
                 'Extract label ids(%s)' % mode >>
                 beam.ParDo(ExtractLabelIdsDoFn(), beam.pvalue.AsIter(labels)) |
                 'Read and convert to JPEG(%s)' % mode >>
                 beam.ParDo(ReadImageAndConvertToJpegDoFn()) |
                 'Embed and make TFExample(%s)' % mode >>
                 beam.ParDo(TFExampleFromImageDoFn(checkpoint)))
  return transformed

def configure_pipeline(p, dataset_train, dataset_eval, checkpoint_path, output_dir, job_id):
  source_train = _util.get_sources_from_dataset(p, dataset_train, 'train')
  labels_source = [source_train]
  if dataset_eval is not None:
    source_eval = _util.get_sources_from_dataset(p, dataset_eval, 'eval')
    labels_source.append(source_eval)

  labels = _labels_pipeline(labels_source)
  train_preprocessed = _transformation_pipeline(source_train, checkpoint_path, labels, 'train')
  if dataset_eval is not None:
    # explicit eval data.
    eval_preprocessed = _transformation_pipeline(source_eval, checkpoint_path, labels, 'eval')
  else:
    # Split train/eval.
    train_preprocessed, eval_preprocessed = (train_preprocessed |
                                             'Random Partition' >>
                                             beam.Partition(TrainEvalSplitPartitionFn(), 2))

  output_train_path = os.path.join(output_dir, job_id, 'train')
  output_eval_path = os.path.join(output_dir, job_id, 'eval')
  labels_file = os.path.join(output_dir, job_id, 'labels')
  labels_save = (labels |
                 'Write labels' >>
                 beam.io.textio.WriteToText(labels_file, shard_name_template=''))
  train_save = train_preprocessed | 'Save train to disk' >> SaveFeatures(output_train_path)
  eval_save = eval_preprocessed | 'Save eval to disk' >> SaveFeatures(output_eval_path)
  # Make sure we write "latest" file after train and eval data are successfully written.
  output_latest_file = os.path.join(output_dir, 'latest')
  ([eval_save, train_save, labels_save] | 'Wait for train eval saving' >> beam.Flatten() |
      'Fixed One' >> beam.transforms.combiners.Sample.FixedSizeGlobally(1) |
      beam.Map(lambda path: job_id) |
      'WriteLatest' >> beam.io.textio.WriteToText(output_latest_file, shard_name_template=''))


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

from itertools import accumulate
from operator import xor
from typing import List

class Solution:
    def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
        prefix_xors = list(accumulate([0] + arr, xor))
        return [prefix_xors[j + 1] ^ prefix_xors[i] for i, j in queries]

if __name__ == "__main__":
    solution = Solution()

    assert [2, 7, 14, 8] == solution.xorQueries(
        [1, 3, 4, 8], [[0, 1], [1, 2], [0, 3], [3, 3]]
    )
    assert [8, 0, 4, 4] == solution.xorQueries(
        [4, 8, 2, 10], [[2, 3], [1, 3], [0, 0], [0, 3]]
    )


import logging

from stonkmaster.commands.base import BaseCommand
from stonkmaster.core.language import get_text
from stonkmaster.core.market import is_market_closed, get_info

class PriceCommand(BaseCommand):

    async def execute(self, ctx, ticker):
        try:
            info = get_info(ticker)

            if len(info) <= 1:
                logging.info(f"{ctx.author.display_name} tried to fetch price for invalid ticker {ticker}")
                await ctx.send(get_text("TickerNotFound", self.config).format(ticker.upper(),
                                                                              self.config['emojis']['NotFound']))
                return

            current = info['regularMarketPrice']
            previous = info['previousClose']
            symbol = info['symbol']
            change = ((current - previous) / previous) * 100
            emoji = self.config['emojis']['StockUp'] if change >= 0 else self.config['emojis']['StockDown']

            if 'longName' in info:
                msg = (f"The market price of **{info['longName']} ({symbol})** is **{round(current, 2)}$** "
                       f"({'{0:+.2f}'.format(change)}%)  {emoji}")
            else:
                msg = (f"The market price of **{symbol}** is **{round(current, 2)}$** "
                       f"({'{0:+.2f}'.format(change)}%)  {emoji}")

            logging.info(f"{ctx.author.display_name} fetched price for ticker {symbol} (current={current}, " +
                         f"previous={previous}, change={change}%)")
            await ctx.send(msg)

            if is_market_closed():
                await ctx.send(f"Market is currently **closed** {self.config['emojis']['Closed']}")
            elif symbol == 'GME':
                await ctx.send("Wennst ned woasd, wannst GME vakaffa wuisd, kosd de do orientiern: " +
                               f"<https://gmefloor.com/> {self.config['emojis']['Money']}")
                await ctx.send("Weitere Infos findst do: <https://gme.crazyawesomecompany.com/> " +
                               f"{self.config['emojis']['Bulb']}")

        except Exception as ex:
            logging.exception(f"Exception in PriceCommand: {ex}")
            await ctx.send(get_text("ErrorMsg", self.config).format(self.config['emojis']['Error']))


import pandas as pd
import numpy as np
from scipy.spatial.distance import euclidean

# Definisikan dataset
data = pd.DataFrame({
    'X1': [10, 2, 4, 6, 8, 7, 4, 6, 7, 6],
    'X2': [7, 3, 2, 4, 6, 5, 3, 3, 4, 3],
    'y': ['A', 'B', 'B', 'B', 'A', 'A', 'B', 'B', 'A', 'A']
})

# Definisikan data baru
x1_new = 8
x2_new = 8

# Hitung jarak antara data baru dan setiap data pada dataset
data['distance'] = data.apply(lambda row: euclidean([x1_new, x2_new], [row['X1'], row['X2']]), axis=1)

# Urutkan data berdasarkan jarak dari yang terkecil hingga yang terbesar
data_sorted = data.sort_values('distance')

# Ambil 7 data terdekat
k = 7
data_nearest = data_sorted.head(k)

# Hitung frekuensi masing-masing kelas pada K data terdekat
class_freq = data_nearest['y'].value_counts()

# Kelas dengan frekuensi terbanyak dianggap sebagai prediksi kelas dari data baru
prediction = class_freq.index[0]

print(f"Prediksi kelas untuk data baru ({x1_new}, {x2_new}) adalah {prediction}")