import time
import datetime
import webbrowser
import os
import urllib.request as req

#from common.constants import liveEngageConstants
#from common import passDecrypt
#from datetime import datetime, timezone

def convertToEpoch(dateToConvert):
    pattern = '%Y-%m-%d %H:%M:%S'
    epStartDateTime = int(time.mktime(time.strptime(str(dateToConvert), pattern)) ) *1000
    return epStartDateTime

def epochStartDate(nowMinusStart):
    startDate =  datetime.date.today() - datetime.timedelta(days=nowMinusStart)
    startTime = datetime.time(0 ,0 ,0)
    startDateTime = convertToEpoch(datetime.datetime.combine(startDate, startTime))
    return startDateTime

def epochEndDate(nowMinusEnd):
    endDate =  datetime.date.today() - datetime.timedelta(days=nowMinusEnd)
    endTime = datetime.time(23 ,59 ,59)
    endDateTime = convertToEpoch(datetime.datetime.combine(endDate, endTime))
    return endDateTime

def getEndTime(runDateTime):
    endTime = (runDateTime - datetime.timedelta(minutes=1440)).isoformat()
    endTime = endTime[0:19] + '+00:00'
    return endTime

def getStartTime(runDateTime):
    startTime = (runDateTime).isoformat()
    startTime = startTime[0:19] + '+00:00'
    return startTime

# old proxyauth scipt that only seems to work at certain times of the day
# def proxyAuth():
#    webbrowser.open(liveEngageConstants.proxyURL, new=1)
#    time.sleep(10)
#    browserExe = "iexplore.exe"
#    os.system("taskkill /f /im "+browserExe)

def fileIDString(start ,end):
    strDate =  str(datetime.date.today())
    fileIDString = strDate + "_" + str(start) + "_" + str(end)
    return fileIDString

def queryDate(todayMinus):
    strDate = str(datetime.date.today() - datetime.timedelta(days=todayMinus))
    return strDate

def proxyAuth():
    proxyString = r'http://' + os.getlogin() + ':' + passDecrypt.proxyPass() + '@dia2.santanderuk.gs.corp:80'
    # proxyString = proxyString.encode('unicode_escape').decode().replace('\\\\', '\\')
    proxy = req.ProxyHandler({'http': proxyString})
    auth = req.HTTPBasicAuthHandler()
    opener = req.build_opener(proxy, auth, req.HTTPHandler)
    req.install_opener(opener)
    conn = req.urlopen('http://google.com')
    return_str = conn.read()

# proxyString = r'http://' + os.getlogin() + ':' + passDecrypt.proxyPass() + '@dia2.santanderuk.gs.corp:80'
# print(proxyString)

print(getEndTime(datetime.datetime.utcnow()))
print(getStartTime(datetime.datetime.utcnow()))

#endTime = (datetime.datetime.now() - datetime.timedelta(minutes=15)).isoformat()

#print(endTime)


from django import template
from AccountingApp.functions.core_algorithm import cleared_person

register = template.Library()

def set_cleared(person):
    person.cleared = cleared_person(person)
    person.save()

class CustomTag:
    def __init__(self, is_person_cleared):
        self.cleared_person = is_person_cleared
        self.set_cleared = set_cleared

@register.simple_tag
def call_method(method_name, *args):
    tag = CustomTag(cleared_person)
    method = getattr(tag, method_name)
    return method(*args)


import util
import parse

def should_run(section,frame):
	args = parse.args(section)

	# handle overwriting of arguments
	if util.flag(frame.get('force','false')):
		return True
	args += [('skip',frame.get('skip','false'))]
	args += [('run',frame.get('run','true'))]
	args += [('when',frame.get('when','true'))]

	for k,v in args:
		if k not in ('skip','run','when'): continue
		v = frame.render(v)
		#print('DEBUG COND',k,v,util.flag(v))
		if k=='skip':
			if util.flag(v):
				return False
		elif k=='run':
			if not util.flag(v):
				return False
		elif k=='when':
			if not v.strip():
				return False
	return True


# PyTest позволяет запустить один и тот же тест с разными входными параметрами.
# Для этого используется декоратор @pytest.mark.parametrize()
# Наш сайт доступен для разных языков. Напишем тест, который проверит, что для сайта
# с русским и английским языком будет отображаться ссылка на форму логина.
# Передадим в наш тест ссылки на русскую и английскую версию главной страницы сайта.

# В @pytest.mark.parametrize() нужно передать параметр, который должен изменяться,
# и список значений параметра. В самом тесте наш параметр тоже нужно передавать в качестве аргумента.
# Обратите внимание, что внутри декоратора имя параметра оборачивается в кавычки,
# а в списке аргументов теста кавычки не нужны.

import pytest
from selenium.webdriver.common.by import By

@pytest.mark.parametrize('language', ["ru", "en-gb"])
def test_guest_should_see_login_link(browser, language):
    link = f"http://selenium1py.pythonanywhere.com/{language}/"
    browser.get(link)
    browser.find_element(By.CSS_SELECTOR, "#login_link")

# Запуск - pytest -s -v test_fixture9.py


import time
import logging
from logging import Logger
from typing import ClassVar, Iterator

import pika
from pydantic import AnyUrl, Field

from ..tap import SourceTap, SourceSettings, DestinationTap, DestinationSettings
from ..message import MessageBase
from ..helpers import namespaced_topic

pipelineLogger = logging.getLogger("pipeline")

class RabbitMQDsn(AnyUrl):
    allowed_schemes = {
        "amqp",
    }

class RabbitMQSourceSettings(SourceSettings):
    rabbitmq: RabbitMQDsn = Field("amqp://localhost", title="RabbitMQ host")

class RabbitMQSource(SourceTap):
    """RabbitMQSource reads from RabbitMQ

    RabbitMQ options:
        --rabbitmq (env: RABBITMQ): RabbitMQ host

    Source options:
        --in-topic (env: INTOPIC): queue to read from
        --timeout (env: TIMEOUT): seconds to exit if no new messages

    >>> import logging
    >>> from unittest.mock import patch
    >>> settings = RabbitMQSourceSettings()
    >>> with patch('pika.ConnectionParameters') as c1:
    ...     with patch('pika.BlockingConnection') as c2:
    ...         RabbitMQSource(settings=settings, logger=logging)
    RabbitMQSource(queue="in-topic")
    """

    settings: RabbitMQSourceSettings

    kind: ClassVar[str] = "RABBITMQ"

    def __init__(
        self, settings: RabbitMQSourceSettings, logger: Logger = pipelineLogger
    ) -> None:
        super().__init__(settings, logger)
        self.settings = settings
        self.topic = settings.topic
        self.name = namespaced_topic(settings.topic, settings.namespace)
        self.timeout = settings.timeout
        parameters = pika.ConnectionParameters(settings.rabbitmq)
        self.rabbit = pika.BlockingConnection(parameters)
        self.channel = self.rabbit.channel()
        self.channel.queue_declare(queue=self.name)
        self.delivery_tag = None
        self.msg = None
        self.logger.info("RabbitMQSource initialized.")

    def __repr__(self) -> str:
        return f'RabbitMQSource(queue="{self.name}")'

    def __len__(self) -> int:
        return -1

    def read(self) -> Iterator[MessageBase]:
        timedOut = False
        lastMessageBaseTime = time.time()

        while not timedOut:
            try:
                method, header, body = self.channel.basic_get(self.name)
            except pika.exceptions.AMQPConnectionError:
                self.logger.warning("Trying to restore connection to RabbitMQ...")
                parameters = pika.ConnectionParameters(self.settings.rabbitmq)
                self.rabbit = pika.BlockingConnection(parameters)
                self.channel = self.rabbit.channel()
                self.logger.warning("Connection to RabbitMQ restored.")
                method, header, body = self.channel.basic_get(self.name)
            except Exception as ex:
                self.logger.error(ex)
                break

            if method:
                self.delivery_tag = method.delivery_tag
                self.logger.info("Read message %s", self.delivery_tag)
                yield MessageBase.deserialize(body)
                lastMessageBaseTime = time.time()
            time.sleep(0.01)
            if self.timeout > 0 and time.time() - lastMessageBaseTime > self.timeout:
                self.logger.info("RabbitMQSource timed out.")
                timedOut = True

    def acknowledge(self) -> None:
        self.logger.info("acknowledged message %s", self.delivery_tag)
        self.channel.basic_ack(self.delivery_tag)

    def close(self) -> None:
        self.logger.info("RabbitMQSource closed.")
        self.channel.close()
        self.rabbit.close()

class RabbitMQDestinationSettings(DestinationSettings):
    rabbitmq: RabbitMQDsn = Field("amqp://localhost", title="RabbitMQ host")

class RabbitMQDestination(DestinationTap):
    """RabbitMQDestination writes to RabbitMQ

    options:
        --rabbitmq (env: RABBITMQ): RabbitMQ host

    standard options:
        --out-topic (env: OUTTOPIC): queue to write to

    >>> import logging
    >>> from unittest.mock import patch
    >>> settings = RabbitMQDestinationSettings()
    >>> with patch('pika.ConnectionParameters') as c1:
    ...     with patch('pika.BlockingConnection') as c2:
    ...        RabbitMQDestination(settings=settings, logger=logging)
    RabbitMQDestination(queue="out-topic")
    """

    settings: RabbitMQDestinationSettings

    kind: ClassVar[str] = "RABBITMQ"

    def __init__(self, settings: RabbitMQDestinationSettings, logger: Logger) -> None:
        super().__init__(settings, logger)
        self.settings = settings
        self.topic = settings.topic
        self.name = namespaced_topic(settings.topic, settings.namespace)
        parameters = pika.ConnectionParameters(settings.rabbitmq, heartbeat=5)
        self.rabbit = pika.BlockingConnection(parameters)
        self.channel = self.rabbit.channel()
        self.channel.queue_declare(queue=self.name)
        self.logger.info("RabbitMQDestination initialized.")

    def __repr__(self) -> str:
        return f'RabbitMQDestination(queue="{self.name}")'

    def __len__(self) -> int:
        return -1

    def write(self, message: MessageBase) -> int:
        serialized = message.serialize()
        try:
            self.channel.basic_publish(
                exchange="", routing_key=self.name, body=serialized
            )
        except pika.exceptions.StreamLostError:
            self.logger.warning("Trying to restore connection to RabbitMQ...")
            self.rabbit = pika.BlockingConnection(
                pika.ConnectionParameters(self.settings.rabbitmq)
            )
            self.channel = self.rabbit.channel()
            self.logger.warning("Connection to RabbitMQ restored.")
            self.channel.basic_publish(
                exchange="",
                routing_key=self.name,
                body=message.serialize(compress=self.settings.compress),
            )
        return len(serialized)

    def close(self) -> None:
        self.logger.info("RabbitMQDestination closed.")
        self.channel.close()
        self.rabbit.close()


#!/exports/applications/apps/SL7/anaconda/2.3.0/bin/python

import os
import sys
import logging
import argparse
from os import path
import tempfile

parser = argparse.ArgumentParser()
parser.add_argument("file", help="fastq file to map")
args = parser.parse_args()
sys.stderr.write(args.file+'\n')
logging.basicConfig(level=logging.DEBUG)

def func():
    #if not os.path.exists('tmp/'):
    #    os.mkdir('tmp/')

    # Map the reads iteratively.
    from hiclib import mapping
    #from mirnylib import h5dict, genome

    mapping.iterative_mapping(
        bowtie_path='bowtie2',
        bowtie_index_path='../genomes/mm9/index/mm9',
        fastq_path=args.file,
        out_sam_path=path.join('/exports/eddie/scratch/s1529682/bams/',
                               path.split(args.file)[1] + '.bam'),
        min_seq_len=25,
        len_step=5,
        nthreads=4,
        #max_reads_per_chunk = 10000000,  #optional, on low-memory machines
        temp_dir=tempfile.gettempdir(),  # optional, keep temporary files here
        bowtie_flags='--very-sensitive')

try:
    func()
except ImportError:
    sys.stderr.write('Broken, restarting\n')
    cwd = os.getcwd()
    from subprocess import call
    call('ssh headnode1.ecdf.ed.ac.uk "cd %s; qsub 01_2_launch_mapping.sh ' + args.file + '"' % cwd, shell=True)


'''
Author: Wenhao Ding
Email: wenhaod@andrew.cmu.edu
Date: 2022-08-18 03:14:33
LastEditTime: 2023-01-17 20:20:40
Description:
'''

import os
import json

command = """ \
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/.mujoco/mujoco210/bin && \
    pip install -e /workspace/Bayesian-DQN/d3rlpy && \
    pip install -e /workspace/Bayesian-DQN/d4rl && \
    pip install -e /workspace/Bayesian-DQN/d4rl-atari && \
    python3 /workspace/Bayesian-DQN/run.py \
        --wandb_dir={} \
        --d4rl_dataset_dir={} \
        --env_name={} \
        --dataset_type={} \
        --model_name={} \
        --qf_name={} \
        --learning_rate={} \
        --hidden_dim={} \
        --gamma={} \
        --threshold_c={} \
        --n_quantiles={} \
        --sample_iters={} \
        --n_neg_samples={} \
        --sample_iters=5 \
        --noise_scale=0.5 \
        --noise_shrink=0.9 \
        --n_trials=10 \
        --sample_iters=5 \
"""

# save directories
wandb_dir = '/results'
d4rl_dataset_dir = '/workspace/data'

# environment name
env_name = [
    'walker2d',
    'halfcheetah',
    'hopper'
]

# 10, 20, 30, 40, 50
dataset_type = [
    'medium-replay',
    'medium',
    'medium-expert',
    #'expert',
]

# parameters of Bayesian-DQN
learning_rate = [
    0.0005,
    #0.0001,
]

hidden_dim = [
    256,
    #512,
]

gamma = [
    0.95,
    0.99,
]

threshold_c = [
    0.05,
    #0.1,
    #0.2,
]

n_quantiles = [
    #40,
    80,
]

sample_iters = [
    5,
    #10,
]

n_neg_samples = [
    256,
    #512,
]

# load template json file
with open('base.json', 'r') as fp:
    base_json = json.load(fp)

# create a new folder
folder_name = './json'
if not os.path.exists(folder_name):
    os.mkdir(folder_name)

# run Bayesian DQN
model_name = 'rcrl'
q_name = 'none'
count = 0
for e_i in env_name:
    for d_i in dataset_type:
        for lr_i in learning_rate:
            for h_i in hidden_dim:
                for g_i in gamma:
                    for t_i in threshold_c:
                        for n_i in n_quantiles:
                            for s_i in sample_iters:
                                for nn_i in n_neg_samples:
                                    job_name = folder_name + '/' + str(count) + '.json'
                                    count += 1
                                    base_json['command'] = command.format(
                                        wandb_dir,
                                        d4rl_dataset_dir,
                                        e_i,
                                        d_i,
                                        model_name,
                                        q_name,
                                        str(lr_i),
                                        str(h_i),
                                        str(g_i),
                                        str(t_i),
                                        str(n_i),
                                        str(s_i),
                                        str(nn_i),
                                    )
                                    with open(job_name, 'w') as fp:
                                        json.dump(base_json, fp, indent=4)

print('generated {} json files.'.format(count))


import argparse
import requests

# Create the parser
my_parser = argparse.ArgumentParser(description='Perform operations on on-line TRIE')

# Add the arguments
my_parser.add_argument('Operation',
                       metavar='op',
                       type=str,
                       help='operation to perform on the TRIE',
		       choices=['insert','delete','search','suggest','print'])
my_parser.add_argument('Keyword',
                       metavar='keyword',
                       type=str,
                       help='input keyword or keywords for the operation')

# Execute the parse_args() method
args = my_parser.parse_args()

op = args.Operation
word = args.Keyword

cloud_url="https://my-trie.herokuapp.com/"
cloud_op_url=cloud_url+op+"?word="+word
#print(cloud_op_url);
resp = requests.get(cloud_op_url);
print(resp.text);


#!/usr/bin/env python3

""" Command Line Interface for DataSafe """

import sys
import logging
import argparse
from timeit import default_timer as timer
from ._utils import _encrypt_cli, _decrypt_cli
from ._version import __version__

def parseArgs() -> argparse.Namespace:
    epilog = 'Stephen Richer, (stephen.richer@proton.me)'
    baseParser = getBaseParser(__version__)
    parser = argparse.ArgumentParser(
        epilog=epilog, description=__doc__, parents=[baseParser])
    subparser = parser.add_subparsers(
        title='required commands',
        description='',
        dest='command',
        metavar='Commands',
        help='Description:')

    sp1 = subparser.add_parser(
        'encrypt',
        description=_encrypt_cli.__doc__,
        help='Encrypt a file with password.',
        parents=[baseParser],
        epilog=parser.epilog)
    sp1.add_argument('file', nargs='?', help='File to encrypt.')
    sp1.set_defaults(function=_encrypt_cli)

    sp2 = subparser.add_parser(
        'decrypt',
        description=_decrypt_cli.__doc__,
        help='Decrypt a file with password.',
        parents=[baseParser],
        epilog=parser.epilog)
    sp2.add_argument('file', help='File to decrypt.')
    sp2.set_defaults(function=_decrypt_cli)

    args = parser.parse_args()
    if 'function' not in args:
        parser.print_help()
        sys.exit()

    rc = executeCommand(args)
    return rc

def executeCommand(args):
    # Initialise logging
    logFormat = '%(asctime)s - %(levelname)s - %(funcName)s - %(message)s'
    logging.basicConfig(level=args.verbose, format=logFormat)
    del args.verbose, args.command
    # Pop main function and excute script
    function = args.__dict__.pop('function')
    start = timer()
    rc = function(**vars(args))
    end = timer()
    logging.info(f'Total execution time: {end - start:.3f} seconds.')
    logging.shutdown()
    return rc

def getBaseParser(version: str) -> argparse.Namespace:
    """ Create base parser of verbose/version. """
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument(
        '--version', action='version', version='%(prog)s {}'.format(version))
    parser.add_argument(
        '--verbose', action='store_const', const=logging.DEBUG,
        default=logging.ERROR, help='verbose logging for debugging')
    return parser


# setup.py

__module_name__ = "setup.py"
__author__ = ", ".join(["Michael E. Vinyard"])
__email__ = ", ".join(["vinyard@g.harvard.edu",])

# package imports #
# --------------- #
import os
import re
from setuptools import setup
import sys

setup(
    name="pool-sharq",
    version="0.0.12",
    python_requires=">3.7.0",
    author="Michael E. Vinyard - Harvard University - Massachussetts General Hospital - Broad Institute of MIT and Harvard",
    author_email="mvinyard@broadinstitute.org",
    url="https://github.com/mvinyard/pool-sharq",
    long_description=open("README.md", encoding="utf-8").read(),
    long_description_content_type="text/markdown",
    description="pool-sharq: python interface for Broad Institute GPP's poolq software.",
    packages=[
        "pool_sharq",
        "pool_sharq._fetch",
        "pool_sharq._poolq_python",
        "pool_sharq._poolq_python._supporting_functions",
    ],
    install_requires=[
        "beautifulsoup4>=4.10.0",
        "licorice>=0.0.2",
        "pandas>=1.3.4",
        "pyrequisites>=0.0.2",
        "requests>=2.26.0",
        "tqdm>=4.62.3",
    ],
    classifiers=[
        "Development Status :: 2 - Pre-Alpha",
        "Programming Language :: Python :: 3.7",
        "Intended Audience :: Science/Research",
        "Topic :: Scientific/Engineering :: Bio-Informatics",
    ],
    license="MIT",
)


from datetime import datetime
from tqdm import tqdm
import torch
import numpy as np

def dump_info(file, text, flag='w'):

    now = datetime.now()
    current_time = now.strftime("%d|%H:%M:%S")

    f = open('results/' + file,flag)

    line = "{}||".format(now)

    if isinstance(text,dict):
        for key, values in text.items():
            line += "{}:{} ".format(key,values)

    elif isinstance(text,str):
        line += text
        #f.write(line)
    f.write(line + '\n')
    f.close()
    return(line)

def generate_descriptors(model,val_loader, device):
    model.eval()

    dataloader = iter(val_loader)
    tbar = tqdm(range(len(val_loader)), ncols=100)

    prediction_bag = {}
    idx_bag = []
    for batch_idx in tbar:
        input,inx = next(dataloader)
        if device in ['gpu','cuda']:
            input = input.to(device)
            input = input.cuda(non_blocking=True)

        if len(input.shape)<4:
            input = input.unsqueeze(0)

        if not torch.is_tensor(inx):
            inx = torch.tensor(inx)
        # Generate the Descriptor
        prediction = model(input)
        # Keep descriptors
        for d,i in zip(prediction.detach().cpu().numpy().tolist(),inx.detach().cpu().numpy().tolist()):
            prediction_bag[i] = d
    return(prediction_bag)

def unique2D(input):
    if not isinstance(input,(np.ndarray, np.generic)):
        input = np.array(input)

    output = []
    for p in input:
        output.extend(np.unique(p))
    #p = np.array([np.unique(p) for p in positive]).ravel()
    output = np.unique(output)
    return(output)


"""Test formatting functionality."""
from typing import Union

import pytest
from langchain_core.agents import AgentAction, AgentActionMessageLog, AgentFinish
from langchain_core.documents import Document
from langchain_core.messages import (
    AIMessage,
    AIMessageChunk,
    ChatMessage,
    ChatMessageChunk,
    FunctionMessage,
    FunctionMessageChunk,
    HumanMessage,
    HumanMessageChunk,
    SystemMessage,
    SystemMessageChunk,
)
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, Generation
from langchain_core.prompt_values import ChatPromptValueConcrete, StringPromptValue
from langchain_core.pydantic_v1 import BaseModel, ValidationError

def test_serialization_of_wellknown_objects() -> None:
    """Test that pydantic is able to serialize and deserialize well known objects."""

    class WellKnownLCObject(BaseModel):
        """A well known LangChain object."""

        __root__: Union[
            Document,
            HumanMessage,
            SystemMessage,
            ChatMessage,
            FunctionMessage,
            AIMessage,
            HumanMessageChunk,
            SystemMessageChunk,
            ChatMessageChunk,
            FunctionMessageChunk,
            AIMessageChunk,
            StringPromptValue,
            ChatPromptValueConcrete,
            AgentFinish,
            AgentAction,
            AgentActionMessageLog,
            ChatGeneration,
            Generation,
            ChatGenerationChunk,
        ]

    lc_objects = [
        HumanMessage(content="human"),
        HumanMessageChunk(content="human"),
        AIMessage(content="ai"),
        AIMessageChunk(content="ai"),
        SystemMessage(content="sys"),
        SystemMessageChunk(content="sys"),
        FunctionMessage(
            name="func",
            content="func",
        ),
        FunctionMessageChunk(
            name="func",
            content="func",
        ),
        ChatMessage(
            role="human",
            content="human",
        ),
        ChatMessageChunk(
            role="human",
            content="human",
        ),
        StringPromptValue(text="hello"),
        ChatPromptValueConcrete(messages=[HumanMessage(content="human")]),
        Document(page_content="hello"),
        AgentFinish(return_values={}, log=""),
        AgentAction(tool="tool", tool_input="input", log=""),
        AgentActionMessageLog(
            tool="tool",
            tool_input="input",
            log="",
            message_log=[HumanMessage(content="human")],
        ),
        Generation(
            text="hello",
            generation_info={"info": "info"},
        ),
        ChatGeneration(
            message=HumanMessage(content="human"),
        ),
        ChatGenerationChunk(
            message=HumanMessageChunk(content="cat"),
        ),
    ]

    for lc_object in lc_objects:
        d = lc_object.dict()
        assert "type" in d, f"Missing key `type` for {type(lc_object)}"
        obj1 = WellKnownLCObject.parse_obj(d)
        assert type(obj1.__root__) == type(lc_object), f"failed for {type(lc_object)}"

    with pytest.raises(ValidationError):
        # Make sure that specifically validation error is raised
        WellKnownLCObject.parse_obj({})


from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import View, TemplateView

from .forms import CartAddProductForm
from .cart import Cart
from shop.models import Product

# Create your views here.

class AddCart(View):
    def post(self, request, product_id):
        cart = Cart(request)
        product = get_object_or_404(Product, id=product_id)
        form = CartAddProductForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            cart.add(product=product, quantity=cd['quantity'], override_quantity=cd['override'])
        return redirect('cart:cart_detail')

class DelCart(View):
    def post(self, request, product_id):
        cart = Cart(request)
        product = get_object_or_404(Product, id=product_id)
        cart.remove(product)
        return redirect('cart:cart_detail')

class CartView(TemplateView):
    template_name = 'cart/cart_detail.html'

    def get_context_data(self, **kwargs):
        cart = Cart(self.request)
        context = super().get_context_data(**kwargs)
        for item in cart:
            item['update_quantity_form'] = CartAddProductForm(initial={
                'quantity': item['quantity'],
                'override': True})
        context['cart'] = cart
        return context


import cv2
import numpy as np
import os
import random
import smtplib
import tensorflow as tf
from datetime import *
from openpyxl import load_workbook
from pytz import timezone
import tkinter as tk
from tkinter import *
import time
# import hashlib
# from pyfingerprint.pyfingerprint import PyFingerprint
# from pyfingerprint.pyfingerprint import FINGERPRINT_CHARBUFFER1
pin = ''
tries, tries1 = 0, 0
pin1 = ''

def security():
    wb = load_workbook('personal_data.xlsx')
    ws = wb.active

    wb1 = load_workbook('system_info.xlsx')
    ws1 = wb1.active

    haar = 'haarcascade_frontalface_default.xml'
    data = ['level_1', 'level_2']
    (width, height) = (130, 100)

    def password(n):
        k = 0
        a = 1
        for cell in ws['A']:
            if cell.value is not None:
                if n in cell.value:
                    r = cell.row

        r1 = 'B' + str(r)
        password_input = ws[r1].value

        def code1(value1, password_input):
            global pin1
            global tries1
            if value1 == 'X':
                pin1 = pin1[:-1]
                e1.delete('0', 'end')
                e1.insert('end', pin1)

            elif value1 == 'Enter':
                if pin1 == password_input:
                    root1.destroy()

                else:
                    Label4 = tk.Label(root1, text='PIN ERROR')
                    Label4.place(relx=0.0, rely=1.0, anchor='sw')
                    tries1 = tries1 + 1
                    Label3 = tk.Label(root1, text='Attempts left: ' + str(3 - tries1))
                    Label3.place(relx=1.0, rely=0.0, anchor='ne')
                    if tries1 < 3:
                        pin1 = ''
                        e1.delete('0', 'end')
                    else:
                        root1.destroy()
                        exit()

            else:
                pin1 += value1
                e1.insert('end', value1)

        keys1 = [
            ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
            ['i', 'j', 'k', 'l', 'n', 'o', 'p', 'q'],
            ['r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
            ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['X', '0', 'Enter'],
        ]

        root1 = tk.Tk()

        root1.geometry("480x320")

        e1 = tk.Entry(root1)
        e1.grid(row=0, column=0, columnspan=3, ipady=5)

        for y12, row in enumerate(keys1, 1):
            for x12, key1 in enumerate(row):
                bpass = tk.Button(root1, text=key1, command=lambda val=key1: code1(val, password_input))
                bpass.grid(row=y12, column=x12, ipadx=10, ipady=10)

        root1.mainloop()

    def otp(n):
        for cell in ws['A']:
            if cell.value is not None:
                if n in cell.value:
                    r = cell.row

        r2 = 'C' + str(r)

        o = str(random.randint(1000, 9999))

        server = smtplib.SMTP('smtp.gmail.com', port=587)
        server.starttls()
        server.login("gajeelredfox746@gmail.com", "passWORD123")

        msg = o

        server.sendmail("gajeelredfox746@gmail.com", ws[r2].value, msg)

        server.quit()

        print('Check your mail for OTP')

        def code(value, o):
            global pin
            global tries
            if value == 'X':
                pin = pin[:-1]
                e.delete('0', 'end')
                e.insert('end', pin)

            elif value == 'Enter':
                if pin == o:
                    root.destroy()

                else:
                    Label2 = tk.Label(root, text='PIN ERROR')
                    Label2.place(relx=0.0, rely=1.0, anchor='sw')
                    tries = tries + 1
                    Label1 = tk.Label(root, text='Attempts left: ' + str(3 - tries))
                    Label1.place(relx=1.0, rely=0.0, anchor='ne')
                    if tries < 3:
                        pin = ''
                        e.delete('0', 'end')
                    else:
                        root.destroy()
                        exit()
            else:
                pin += value
                e.insert('end', value)

        keys = [
            ['1', '2', '3'],
            ['4', '5', '6'],
            ['7', '8', '9'],
            ['X', '0', 'Enter'],
        ]

        root = tk.Tk()

        root.geometry("480x320")

        e = tk.Entry(root)
        e.grid(row=0, column=0, columnspan=3, ipady=5)

        for y111, row in enumerate(keys, 1):
            for x111, key in enumerate(row):
                b = tk.Button(root, text=key, command=lambda val=key: code(val, o))
                b.grid(row=y111, column=x111, ipadx=10, ipady=10)

        root.mainloop()

    # def fingerprint_testing(Name):
    #     fp_count = 3
    #     l = []
    #     try:
    #         f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000)
    #
    #         if ( f.verifyPassword() == False ):
    #             raise ValueError('The given fingerprint sensor password is wrong!')
    #
    #     except Exception as e:
    #         print('The fingerprint sensor could not be initialized!')
    #         print('Exception message: ' + str(e))
    #         exit(1)
    #
    #     ## Gets some sensor information
    #     print('Currently used templates: ' + str(f.getTemplateCount()) +'/'+ str(f.getStorageCapacity()))
    #
    #     ## Tries to search the finger and calculate hash
    #     try:
    #         while fp_count < 3:
    #             print('Waiting for finger...')
    #
    #             ## Wait that finger is read
    #             while ( f.readImage() == False ):
    #                 pass
    #
    #             ## Converts read image to characteristics and stores it in charbuffer 1
    #             f.convertImage(FINGERPRINT_CHARBUFFER1)
    #
    #             ## Searchs template
    #             result = f.searchTemplate()
    #
    #             positionNumber = result[0]
    #             accuracyScore = result[1]
    #
    #             if fp_count < 3:
    #                 if ( positionNumber == -1 ):
    #                     print('No match found!')
    #                     fp_count = fp_count + 1
    #
    #                 else:
    #                     wb3 = load_workbook('fingerprints.xlsx')
    #                     ws3 = wb3.active
    #
    #                     for cell in ws3['A']:
    #                         l.append(cell.value)
    #
    #                     if Name in l:
    #                         r = l.index(Name) + 1
    #                         print(r)
    #
    #                     if positionNumber == r - 2:
    #                         print("Fingerprint matched")
    #                         print('Found template at position #' + str(positionNumber))
    #                         print('The accuracy score is: ' + str(accuracyScore))
    #                         fp_count = 3
    #             else:
    #                 exit()
    #
    #         else:
    #             exit()
    #
    #         wb3.save(filename='fingerprints.xlsx')
    #         wb3.close()
    #
    #     except Exception as e:
    #         print('Operation failed!')
    #         print('Exception message: ' + str(e))
    #         exit(1)

    def emotion():
        cap = cv2.VideoCapture(0)
        ret, image = cap.read()
        c = 1
        while c:
            gray1 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            faces1 = face_cascade.detectMultiScale(gray1, 1.3, 5)
            for (x1, y1, w1, h1) in faces1:
                cv2.rectangle(image, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 2)
                face1 = gray1[y1:y1 + h1, x1:x1 + w1]
                face_resize1 = cv2.resize(face1, (width, height))
                cv2.imshow("face_resize1", face_resize1)
                cropped_img = np.expand_dims(np.expand_dims(cv2.resize(face_resize1, (48, 48)), -1), 0)

                my_model = tf.keras.models.load_model('model.h5')
                emotion_dict = {0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy", 4: "Neutral", 5: "Sad", 6: "Surprised"}
                prediction = my_model.predict(cropped_img)
                maxindex = int(np.argmax(prediction))
                print('hi')
                if emotion_dict[maxindex] in ['Happy', 'Neutral']:
                    print("Personnel is ", emotion_dict[maxindex])
                    c = 0
                    cap.release()
                else:
                    cv2.putText(im, emotion_dict[maxindex], (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
                    print('Personnel seems ' + emotion_dict[maxindex] + '. Not safe to give access')

    (images, lables, names, id) = ([], [], {}, 0)

    for d in data:
        for (subdirs, dirs, files) in os.walk(d):
            for subdir in dirs:
                names[id] = subdir
                subjectpath = os.path.join(d, subdir)
                for filename in os.listdir(subjectpath):
                    path = subjectpath + '/' + filename
                    lable = id
                    images.append(cv2.imread(path, 0))
                    lables.append(int(lable))
                id += 1
    (images, lables) = [np.array(lis) for lis in [images, lables]]

    model = cv2.face.LBPHFaceRecognizer_create()
    model.train(images, lables)

    face_cascade = cv2.CascadeClassifier(haar)
    top.destroy()

    while True:
        webcam = cv2.VideoCapture(0)
        (_, im) = webcam.read()
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 2)
            face = gray[y:y + h, x:x + w]
            face_resize = cv2.resize(face, (width, height))
            cv2.imshow("frame1", im)
            pred = model.predict(face_resize)
            print(pred)
            cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)
            webcam.release()
            if pred[1] < 100:
                print('Face recognized.')
                print('Hello ', names[pred[0]])
                if names[pred[0]] in os.listdir('level_1'):
                    if ws1['B2'].value == 1:
                        emotion()
                    # if ws1['B3'].value == 1:
                    #     fingerprint_testing(names[pred[0]])
                    if ws1['B4'].value == 1:
                        password(names[pred[0]])
                    if ws1['B5'].value == 1:
                        otp(names[pred[0]])

                    now = datetime.now(timezone('UTC'))
                    now_asia = now.astimezone(timezone('Asia/Kolkata'))
                    wb2 = load_workbook('entry_log.xlsx')
                    ws2 = wb2.active

                    r3 = ws2.max_row + 1
                    ws2.cell(column=1, row=r3, value=names[pred[0]])
                    ws2.cell(column=2, row=r3, value=now_asia.strftime("%d/%m/%Y"))
                    ws2.cell(column=3, row=r3, value=now_asia.strftime("%H:%M:%S"))

                    wb2.save(filename='entry_log.xlsx')
                    wb2.close()
                    exit()

                else:
                    if ws1['B8'].value == 1:
                        emotion()
                    # if ws1['B9'].value == 1:
                    #     fingerprint_testing(names[pred[0]])
                    if ws1['B10'].value == 1:
                        password(names[pred[0]])
                    if ws1['B11'].value == 1:
                        otp(names[pred[0]])

                    now = datetime.now(timezone('UTC'))
                    now_asia = now.astimezone(timezone('Asia/Kolkata'))
                    wb2 = load_workbook('entry_log.xlsx')
                    ws2 = wb2.active

                    r3 = ws2.max_row + 1
                    ws2.cell(column=1, row=r3, value=names[pred[0]])
                    ws2.cell(column=2, row=r3, value=now_asia.strftime("%d/%m/%Y"))
                    ws2.cell(column=3, row=r3, value=now_asia.strftime("%H:%M:%S"))

                    wb2.save(filename='entry_log.xlsx')
                    wb2.close()
                    exit()

top = tk.Tk()

top.geometry("480x320")

B = tk.Button(top, text="Press to enter", command=security)
B.pack()
top.mainloop()


import argparse
import os
import logging
import warnings
from datetime import datetime

import numpy as np
import pandas as pd
from skimage.io import imsave
from tqdm import tqdm

from torch.utils.data import DataLoader
from agents.unet import UNetAgent
from datasets.salt import SaltTest
from utils.imgproc import remove_small_mask
from utils.misc import rle_encode
from configs.config import process_config

# Filter out the low contrast warning in imsave()
warnings.filterwarnings('ignore', message='.*low contrast')
warnings.filterwarnings('ignore', message='.*Anti')

ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

def train(cfg):

    '''
    # temp
    from shutil import copy2  # NOQA
    os.makedirs(cfg.CHECKPOINT_DIR, exist_ok=True)
    copy2('./nets/unet_res_open.py', cfg.CHECKPOINT_DIR)
    copy2('./datasets/salt.py', cfg.CHECKPOINT_DIR)
    '''

    for KFOLD_I in cfg.COMMON.KFOLD_I_LIST:
        for CYCLE_I in range(cfg.COMMON.CYCLE_N):

            filename = f'UNet_{KFOLD_I}_{CYCLE_I}.ckpt'
            if os.path.exists(os.path.join(cfg.COMMON.CHECKPOINT_DIR, filename)):
                continue

            cfg_dyn = cfg.get_train_config(CYCLE_I)
            cfg_dyn.KFOLD_I = KFOLD_I
            cfg_dyn.CYCLE_I = CYCLE_I
            logging.info(vars(cfg_dyn))

            agent = UNetAgent(cfg_dyn)
            if CYCLE_I > 0:
                agent.load_checkpoint(CYCLE_I-1)
            agent.train()

    # logging configs
    logging.info(vars(cfg.COMMON))

def test(cfg):
    cfg = cfg.get_test_config()

    test_dataset = SaltTest(cfg)
    test_loader = DataLoader(dataset=test_dataset, batch_size=cfg.TEST_BATCH_SIZE,
                             shuffle=False, num_workers=8)

    agents = []
    for cfg.KFOLD_I, TEST_CYCLE_I in zip(cfg.KFOLD_I_LIST, cfg.TEST_CYCLE_I_LIST):
        agent = UNetAgent(cfg, predict_only=True)
        agent.load_checkpoint(TEST_CYCLE_I)
        agents.append(agent)

    tqdm_batch = tqdm(test_loader, f'Test')
    os.makedirs(os.path.join(cfg.CHECKPOINT_DIR, 'test_imgs'), exist_ok=True)
    pred_dict = {}
    for x in tqdm_batch:
        pred = [a.predict(x['img']) for a in agents]
        # pred = [a.predict(x['img']) > a.best_thres for a in agents]
        # pred = [a.predict(x['img']) > 0.5 for a in agents]
        # pred = [a.predict(x['img']) > 0.45 for a in agents]
        # pred = [a.predict(x['img']) > 0.55 for a in agents]
        pred = np.mean(pred, axis=0)
        pred = np.squeeze(pred)

        for mask, fname in zip(pred, x['file_name']):
            mask = np.round(mask)
            mask = remove_small_mask(mask)

            save_path = os.path.join(cfg.CHECKPOINT_DIR, f'test_imgs/{fname}.png')
            imsave(save_path, mask)
            pred_dict[fname] = rle_encode(mask[13:-14, 13:-14])

    sub = pd.DataFrame.from_dict(pred_dict, orient='index')
    sub.index.names = ['id']
    sub.columns = ['rle_mask']
    sub.to_csv(os.path.join(cfg.CHECKPOINT_DIR, 'submit.csv'))

if __name__ == '__main__':

    # ------------
    # Argparse
    # ------------
    parser = argparse.ArgumentParser()

    # Positional arguments
    parser.add_argument('MODE', type=str, choices=['train', 'test'])
    parser.add_argument('CFG_NAME', type=str)

    # Optional arguments
    parser.add_argument('--VER_TO_LOAD', type=str)

    # Fixed configs
    args = parser.parse_args()
    cfgs = process_config(args)

    # ------
    # Setting Root Logger
    # -----
    level = logging.INFO
    format = '%(asctime)s: %(message)s'
    log_dir = os.path.join(ROOT_DIR, 'output', 'log')
    log_path = os.path.join(log_dir, datetime.now().strftime('%Y%m%d_%H%M%S.log'))
    os.makedirs(log_dir, exist_ok=True)
    handlers = [
        logging.StreamHandler(),
        logging.FileHandler(log_path, mode='w')
    ]
    logging.basicConfig(format=format, level=level, handlers=handlers)

    # -------
    # Run
    # -------
    func = globals()[args.MODE]
    func(cfgs)


from __future__ import annotations

import copy
import math
from typing import Optional, Tuple, Type, TypeVar, TYPE_CHECKING, Union

from data.render_order import RenderOrder

if TYPE_CHECKING:
    from components.ai import BaseAI
    from components.stats import Stats
    from components.inventory import Inventory
    from components.level import Level
    from game_map import GameMap

T = TypeVar('T', bound='Entity')

class Entity:
    parent: Union[GameMap, Inventory]

    def __init__(
        self,
        parent: Optional[GameMap] = None,
        x: int = 0,
        y: int = 0,
        char: str = '?',
        color: Tuple[int, int, int] = (255, 255, 255),
        name: str = '<Todd Howard>',
        blocks_movement: bool = False,
        render_order: RenderOrder = RenderOrder.CORPSE,
    ):
        self.x = x
        self.y = y
        self.char = char
        self.color = color
        self.name = name
        self.blocks_movement = blocks_movement
        self.render_order = render_order

        if parent:
            self.parent = parent
            parent.entities.add(self)

    @property
    def gamemap(self) -> GameMap:
        return self.parent.gamemap

    @property
    def position(self) -> Tuple[int, int]:
        return (self.x, self.y)

    def spawn(self: T, gamemap: GameMap, x: int, y: int):
        clone = copy.deepcopy(self)
        clone.x = x
        clone.y = y
        clone.parent = gamemap
        gamemap.entities.add(clone)
        return clone

    def place(self, x: int, y: int, gamemap: Optional[GameMap] = None) -> None:
        """Place entity."""
        self.x = x
        self.y = y
        if gamemap:
            if hasattr(self, 'parent'):
                if self.parent is self.gamemap:
                    self.gamemap.entities.remove(self)
            self.parent = gamemap
            gamemap.entities.add(self)

    def distance(self, x: int, y: int) -> float:
        return math.sqrt((x - self.x) ** 2 + (y - self.y) ** 2)

    def move(self, x: int, y: int) -> None:
        self.x += x
        self.y += y

class Actor(Entity):
    def __init__(
            self,
            *,
            gamemap: Optional[GameMap] = None,
            x: int = 0,
            y: int = 0,
            char: str = '?',
            color: Tuple[int, int, int] = (255, 255, 255),
            name: str = '<Actor Todd Howard>',
            ai_cls: Type[BaseAI],
            stats: Stats,
            inventory: Inventory,
            # equipment: Equipment,
            # fighter: Fighter,
            level: Level,
    ):
        super().__init__(
            x=x,
            y=y,
            char=char,
            color=color,
            name=name,
            blocks_movement=True,
            render_order=RenderOrder.ACTOR,
        )
        # Setting `Ai`
        self.ai: Optional[BaseAI] = ai_cls(self)

        # Setting `Stats`
        self.stats = stats
        self.stats.parent = self
        # Setting `Inventory`
        self.inventory = inventory
        self.inventory.parent = self
        # Setting `Level`
        self.level = level
        self.level.parent = self
        # TODO

    @property
    def is_alive(self) -> bool:
        return bool(self.ai)

class Item(Entity):
    pass


# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import argparse
from pathlib import Path
import torch
import numpy as np
sys.path.append(r"./Efficient-3DCNNs")
from dataset import get_test_set
from spatial_transforms import Normalize, Compose, Scale, CornerCrop
from temporal_transforms import TemporalRandomCrop
from target_transforms import VideoID

class ToTensor(object):
    def __init__(self, norm_value=255):
        self.norm_value = norm_value

    def __call__(self, pic):
        img = np.array(pic, dtype=np.float32)
        img = img.transpose(2, 0, 1)
        img = img / self.norm_value
        return torch.from_numpy(img)

    def randomize_parameters(self):
        pass

def preprocess(save_path):
    print('preprocessing')
    norm_method = Normalize([114.7748, 107.7354, 99.4750], [1, 1, 1])

    spatial_transform = Compose([
        Scale(int(112 / 1.0)),
        CornerCrop(112, 'c'),
        ToTensor(1), norm_method
    ])
    temporal_transform = TemporalRandomCrop(16, 1)
    target_transform = VideoID()

    test_data = get_test_set(opt, spatial_transform, temporal_transform,
                             target_transform)

    test_loader = torch.utils.data.DataLoader(test_data,
                                              batch_size=opt.inference_batch_size,
                                              shuffle=False,
                                              num_workers=0,
                                              pin_memory=True)
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    file = open(opt.info_path, 'w')
    cid = 0
    for i, (inputs, targets) in enumerate(test_loader):
        if(len(targets) == opt.inference_batch_size):
            info = ''
            for j in range(len(targets)):
                info = info + targets[j] + ' '
            batch_bin = inputs.cpu().numpy()
            path_bin = str(save_path) + '/' + str(cid) + '.bin'
            cid = cid + 1
            batch_bin.tofile(path_bin)
            file.write(info)
            file.write('\n')
        if (i % 1000) == 0:
            print('[{}/{}]'.format(i+1, len(test_loader)))
    print('preprocess finished')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='preprocess of 3D-ResNets')
    parser.add_argument('--video_path', default='C:/Users/17270/Efficient-3DCNNs-master-2/annotation_UCF101/UCF-101-image/UCF-101-image', type=Path, help='Directory path of videos')
    parser.add_argument('--annotation_path', default='ucf101_01.json', type=Path, help='Annotation file path')
    parser.add_argument('--dataset', default='ucf101', type=str, help='Used dataset (activitynet | kinetics | ucf101 | hmdb51)')
    parser.add_argument('--test_subset', default='val', type=str, help='Used subset in inference (train | val | test)')
    parser.add_argument('--output_path', default='zjbintt', type=Path, help='Directory path of binary output data')
    parser.add_argument('--info_path', default='zjbin1.info', type=Path, help='Directory path of binary output data')
    parser.add_argument('--inference_batch_size', default=16, type=int, help='Batch Size for inference. 0 means this is the same as batch_size.')
    parser.add_argument('--sample_duration', default=16, type=int, help='Temporal duration of inputs')
    opt = parser.parse_args()

    preprocess(opt.output_path)


import imghdr
import os
from PIL import Image

# Change working directory to be inside the images folder
os.chdir("./images/")

# Get every image in the directory
for file in os.listdir(os.getcwd()):
    filename = os.fsdecode(file)
    imageType = imghdr.what(filename)
    splitFilename = filename.split(".", 1)
    imageExtension = splitFilename[1]

    # Fix if image type is not a png or image extension is not "png"
    if (imageType != "png" or imageExtension != "png"):
        print("BEFORE: " + filename + "\t" + imageType)

        image = Image.open(filename)
        filenameNoExt = splitFilename[0]
        correctFilename = filenameNoExt + ".png"
        image.save(correctFilename)

        correctImageType = imghdr.what(correctFilename)
        print("AFTER: " + correctFilename + "\t" + correctImageType)


import requests
from bs4 import BeautifulSoup

HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',}

def parser_ACG_tag_list(src_url):

        r = requests.get(src_url, headers=HEADERS)
        soup = BeautifulSoup(r.text, 'html.parser')
        p = soup.find_all(class_="BA-menu")
        # ps = soup.select("body > div.BA-topbg > div > ul > li > a")
        # print(ps)

        # print(p)
        catch_text = []
        for item in p:
                target = item.find_all('a')
                # print(target)
                if target != []:
                        for x in range(len(target) - 1):
                                target_text = target[x].text
                                catch_text.append(target_text)

        print(catch_text)

if __name__ == "__main__":
        parser_ACG_tag_list("https://www.gamer.com.tw/")


import numpy as np
import pickle
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.colors import LogNorm

with open("a_1_b_1.9_x_0.9_y_1.8_g_0_0_0_0_dt_0.001.brus", "rb") as f:
    pos_1 = np.array(pickle.load(f))
with open("a_1_b_1.9_x_0.9_y_1.8_g_0.01_0_0_0.01_dt_0.001.brus", "rb") as f:
    pos_2 = np.array(pickle.load(f))

#plt.plot(data[1, :, 0], data[1, :, 1])

fig, ax = plt.subplots(2)
limits = [[0.6, 1.4], [1.6, 2.5]]

#Create 2d Histogram
#data_1,_,_ = np.histogram2d(pos_1[:, 0, 0], pos_1[:, 0, 1], bins = 200, range=limits, density=True)
#data_2,_,_ = np.histogram2d(pos_2[:, 0, 0], pos_2[:, 0, 1], bins = 200, range=limits, density=True)
data_1, _, _ = np.histogram2d(pos_1[:, 0, 0],
                              pos_1[:, 0, 1], bins=200, range=limits)
data_2, _, _ = np.histogram2d(pos_2[:, 0, 0],
                              pos_2[:, 0, 1], bins=200, range=limits)

#Smooth with filter
im_1 = ax[0].imshow(data_1 + 0.01, interpolation = 'gaussian', origin = 'lower', norm=LogNorm(vmin=0.01, vmax=1000))
im_2 = ax[1].imshow(data_2 + 0.01, interpolation = 'gaussian', origin = 'lower', norm=LogNorm(vmin=0.01, vmax=1000))

#Define animation.
def animate(i) :
    data_1, _, _ = np.histogram2d(pos_1[:, i, 0],
                                  pos_1[:, i, 1], bins=200, range=limits)
    data_2, _, _ = np.histogram2d(pos_2[:, i, 0],
                                  pos_2[:, i, 1], bins=200, range=limits)
    im_1.set_data(data_1 + 0.01)
    im_2.set_data(data_2 + 0.01)

ani = animation.FuncAnimation(fig, animate, np.arange(0, 490),
                          interval=100, blit=False)

plt.show()


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

torch.manual_seed(1)
device = 'cuda' if torch.cuda.is_available() else 'cpu'

class SpaceLSTM(torch.nn.Module):
    def __init__(self, inp_dim=6, hidden_dim=6, op_dim=6, batch_size=30):
        super(SpaceLSTM, self).__init__()
        # Hidden I/P : 2
        self.batch_size, self.hidden_dim = batch_size, hidden_dim
        self.inp_dim, self.op_dim = inp_dim, op_dim
        self.hidden = (torch.randn(1, self.batch_size, self.hidden_dim).to(device), torch.randn(1, self.batch_size, self.op_dim).to(device))
        self.lstm = nn.LSTM(self.inp_dim, self.op_dim)

    def forward(self, inp_seq):
        out, self.hidden = self.lstm(inp_seq, self.hidden)
        return out


import cv2
import numpy as np
import math
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from scipy.stats import itemfreq
from extract_features import load_image,glcm_feature
from skimage.feature import hog
# 用于提取HOG特征

class Hog_descriptor():
    def __init__(self, img, cell_size=16, bin_size=8):
        self.img = img
        self.img = np.sqrt(img / np.max(img))
        self.img = img * 255
        self.cell_size = cell_size
        self.bin_size = bin_size
        self.angle_unit = 360 / self.bin_size

    def extract(self):
        height, width = self.img.shape
        gradient_magnitude, gradient_angle = self.global_gradient()
        gradient_magnitude = abs(gradient_magnitude)
        cell_gradient_vector = np.zeros((int(height / self.cell_size), int(width / self.cell_size), int(self.bin_size)))
        for i in range(cell_gradient_vector.shape[0]):
            for j in range(cell_gradient_vector.shape[1]):
                cell_magnitude = gradient_magnitude[i * self.cell_size:(i + 1) * self.cell_size,
                                 j * self.cell_size:(j + 1) * self.cell_size]
                cell_angle = gradient_angle[i * self.cell_size:(i + 1) * self.cell_size,
                             j * self.cell_size:(j + 1) * self.cell_size]
                cell_gradient_vector[i][j] = self.cell_gradient(cell_magnitude, cell_angle)

        hog_image = self.render_gradient(np.zeros([height, width]), cell_gradient_vector)
        hog_vector = []
        # 判断hog_image是否为0矩阵，如果是0矩阵，就不做处理
        # 构造一个0矩阵，判断hog_image==0矩阵
        zero_array = np.zeros(hog_image.shape)
        if (hog_image == zero_array).all() == True:
            return None, None
        else:
            for i in range(cell_gradient_vector.shape[0] - 1):
                for j in range(cell_gradient_vector.shape[1] - 1):
                    block_vector = []
                    block_vector.extend(cell_gradient_vector[i][j])
                    block_vector.extend(cell_gradient_vector[i][j + 1])
                    block_vector.extend(cell_gradient_vector[i + 1][j])
                    block_vector.extend(cell_gradient_vector[i + 1][j + 1])
                    mag = lambda vector: math.sqrt(sum(i ** 2 for i in vector))
                    magnitude = mag(block_vector)
                    if magnitude != 0:
                        normalize = lambda block_vector, magnitude: [element / magnitude for element in block_vector]
                        block_vector = normalize(block_vector, magnitude)
                        hog_vector.append(block_vector)
            return hog_vector, hog_image

    def global_gradient(self):
        gradient_values_x = cv2.Sobel(self.img, cv2.CV_64F, 1, 0, ksize=5)
        gradient_values_y = cv2.Sobel(self.img, cv2.CV_64F, 0, 1, ksize=5)
        gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)
        gradient_angle = cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)
        return gradient_magnitude, gradient_angle

    def cell_gradient(self, cell_magnitude, cell_angle):
        orientation_centers = [0] * self.bin_size
        for i in range(cell_magnitude.shape[0]):
            for j in range(cell_magnitude.shape[1]):
                gradient_strength = cell_magnitude[i][j]
                gradient_angle = cell_angle[i][j]
                min_angle, max_angle, mod = self.get_closest_bins(gradient_angle)
                orientation_centers[min_angle] += (gradient_strength * (1 - (mod / self.angle_unit)))
                orientation_centers[max_angle] += (gradient_strength * (mod / self.angle_unit))
        return orientation_centers

    def get_closest_bins(self, gradient_angle):
        idx = int(gradient_angle / self.angle_unit)
        mod = gradient_angle % self.angle_unit
        return idx, (idx + 1) % self.bin_size, mod

    def render_gradient(self, image, cell_gradient):
        cell_width = self.cell_size / 2
        max_mag = np.array(cell_gradient).max()
        for x in range(cell_gradient.shape[0]):
            for y in range(cell_gradient.shape[1]):
                cell_grad = cell_gradient[x][y]
                cell_grad /= max_mag
                angle = 0
                angle_gap = self.angle_unit
                for magnitude in cell_grad:
                    angle_radian = math.radians(angle)
                    if math.isnan(magnitude):
                        break
                    else:
                        x1 = int(x * self.cell_size + magnitude * cell_width * math.cos(angle_radian))
                        y1 = int(y * self.cell_size + magnitude * cell_width * math.sin(angle_radian))
                        x2 = int(x * self.cell_size - magnitude * cell_width * math.cos(angle_radian))
                        y2 = int(y * self.cell_size - magnitude * cell_width * math.sin(angle_radian))
                        cv2.line(image, (y1, x1), (y2, x2), int(255 * math.sqrt(magnitude)))
                        angle += angle_gap
        return image

def hog_feature(image):
    hog = Hog_descriptor(image, cell_size=8, bin_size=9)
    vector, image_hog = hog.extract()
    if vector is None or image_hog is None:
        return None
    else:
        array_vector = np.array(vector)
        # # 对数组中nan值进行处理
        # for i in range(array_vector.shape[0]):
        #     for j in range(array_vector.shape[1]):
        #         if math.isnan(array_vector[i][j]):
        #             break
        # 要获取全部的特征还需要做归一化处理
        lbpTranpose = array_vector.transpose()
        pca = PCA(n_components=1)
        pca.fit(lbpTranpose)
        newData = pca.transform(lbpTranpose)
        hogTranpose = np.array(newData).transpose()
        return hogTranpose
    # orientations = 9
    # pixels_per_cell = [5, 5]
    # cells_per_block = [3, 3]
    # visualize = False
    # transform_sqrt = True
    # hog_feature = hog(image, orientations, pixels_per_cell, cells_per_block, visualise=visualize, transform_sqrt=transform_sqrt)
    # return hog_feature

# for i in range(1):
#     img = cv2.imread('./eddyData/train/eddy/'+str(i)+'.tif', cv2.IMREAD_GRAYSCALE)
#     imagere = cv2.resize(img, (280, 280), interpolation=cv2.INTER_CUBIC)
#     hog = Hog_descriptor(imagere, cell_size=8, bin_size=8)
#     vector, image = hog.extract()
#     print(np.array(vector).shape)
# # print(vector)
# # with open("HOG特征向量.txt", "a") as myfile:
# #     myfile.write(str(vector) + "\n")
# # histogram=itemfreq(image)
# # Calculate the histogram
# # x = itemfreq(image.ravel())
# # Normalize the histogram
# # hist = x[:, 1]/sum(x[:, 1])
# # 线图
# # plt.subplot(121)
# # plt.plot(hist)
# # 直方图
# # plt.subplot(121)
# # plt.hist(hist)
# # plt.subplot(122)
# #     plt.imshow(image, cmap='gray')
# #     plt.show()
#     lbpTranpose = np.array(vector).transpose()
#     pca = PCA(n_components=1)
#     pca.fit(lbpTranpose)
#     newData = pca.transform(lbpTranpose)
#     hogTranpose = np.array(newData).transpose()
#     print(hogTranpose.shape)
#     print("读写"+str(i)+"图片")
#     with open("HOG特征向量.txt", "a") as myfile:
#         for i in range(32):
#             myfile.write(str(hogTranpose[0][i]) + " ")
#         myfile.write("\n")
#     myfile.close()

# if __name__ =='__main__':
#     for i in range(4938):
#         image = load_image(i)
#         if image is not None:
#             hog = Hog_descriptor(image, cell_size=8, bin_size=9)
#             vector, image_hog = hog.extract()
#             array_vector = np.array(vector)
#             # 要获取全部的特征还需要做归一化处理
#             lbpTranpose = array_vector.transpose()
#             pca = PCA(n_components=1)
#             pca.fit(lbpTranpose)
#             newData = pca.transform(lbpTranpose)
#             hogTranpose = np.array(newData).transpose()
#             print(hogTranpose.shape)
#             # GLCM的4个特征
#             contrast, correlation, energy, homogeneity = glcm_feature(image)
#             # 写入文件
#             with open("./features/GLCM_HOG_pca_feature.txt", "a") as myfile:
#                 for i in range(36):
#                     myfile.write(str(hogTranpose[0][i]) + " ")
#                 myfile.write(str(contrast) + ' ' + str(correlation) + ' ' + str(energy) + ' ' + str(homogeneity) + ' ')
#                 myfile.write("\n")
#             myfile.close()
#             # print(array_vector.shape)
#             # normalised_blocks, hog_image = hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(8, 8),
#             #                                    block_norm='L2-Hys', visualise=True, feature_vector=True)
#             # print(normalised_blocks.shape)


import argparse
import csv
import gzip

def gz_to_csv(args):
    print('loading data...')
    with gzip.open(args.phrase, mode='rt', encoding='utf-8') as f:
        ptable = f.readlines()

    ptable_csv = [['English','Japanese','phi_fe','lex_fe','phi_ef','lex_ef','Alignment','Counts']]
    for line in ptable:
        tmp = line.strip().split('|||')
        out_tmp = []
        out_tmp.append(tmp[0].strip())  # English Phrase
        out_tmp.append(tmp[1].strip())  # Japanese Phrase
        for x in tmp[2].strip().split():# 確率 phi_fe
            out_tmp.append(x)
        out_tmp.append(tmp[3].strip())
        out_tmp.append(tmp[4].strip())
        ptable_csv.append(out_tmp)

    with open(args.output+'.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerows(ptable_csv)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--phrase')
    parser.add_argument('-o', '--output', default='out')

    args = parser.parse_args()

    gz_to_csv(args)

main()


from collections import ChainMap
from itertools import chain
import pathlib
import re
import sys
from clldutils.misc import slug
from collections import defaultdict

from cldfbench import CLDFSpec, Dataset as BaseDataset

# function is also in sinopy, for convenience reused here
def is_chinese(name):
    """
    Check if a symbol is a Chinese character.

    Note
    ----

    Taken from http://stackoverflow.com/questions/16441633/python-2-7-test-if-characters-in-a-string-are-all-chinese-characters
    """
    if not name:
        return False
    for ch in name:
        ordch = ord(ch)
        if not (0x3400 <= ordch <= 0x9fff) and not (0x20000 <= ordch <= 0x2ceaf) \
                and not (0xf900 <= ordch <= ordch) and not (0x2f800 <= ordch <= 0x2fa1f):
                return False
    return True

def parse_line(line):
    phrases = [p for p in line.split("、") if p.strip()]
    out = []
    for phrase in phrases:
        chars = []
        rhymes = []
        rhyme = ""
        for char in phrase:
            if char in "abcdefghijklmnopqrstuvw" and char != "x":
                rhyme = char
            elif is_chinese(char):
                chars += [char]
                rhymes += [rhyme]
                rhyme = ""
        rhyme_words, rhyme_word_idxs, rhyme_idxs = [], [], []
        for i, (char, rhyme) in enumerate(zip(chars, rhymes)):
            if rhyme:
                rhyme_words += [char]
                rhyme_word_idxs += [str(i+1)]
                rhyme_idxs += [rhyme]
        out += [[
            phrase, chars, rhyme_words, rhyme_word_idxs,
            rhyme_idxs]]

    return out

class Dataset(BaseDataset):
    dir = pathlib.Path(__file__).parent
    id = "baxterocrhymes"

    def cldf_specs(self):  # A dataset must declare all CLDF sets it creates.
        return CLDFSpec(
                dir=self.cldf_dir, module='Generic',
                metadata_fname='cldf-metadata.json'
                )

    def cmd_download(self, args):
        """
        Download files to the raw/ directory. You can use helpers methods of `self.raw_dir`, e.g.

        >>> self.raw_dir.download(url, fname)
        """
        self.raw_dir.download(
                "https://github.com/digling/shijing/raw/v1.0/S_shijing_rhymes.corrected.txt",
                "S_shijing_rhymes.corrected.txt"
                )

    def cmd_makecldf(self, args):
        """
        Convert the raw data to a CLDF dataset.
        """

        args.writer.cldf.add_component('LanguageTable')
        args.writer.cldf.add_component(
            'ExampleTable',
            'Poem_ID',
            {"name": "Entry_IDS", "datatype": "string", "separator": " "},
            {'name': 'Stanza_Number', 'datatype': 'integer'},
            {'name': 'Line_Number', 'datatype': 'integer'},
            {"name": "Phrase_Number", "datatype": "integer"},
            {"name": "Rhyme_Words", "datatype": "string", "separator": " "},
            {"name": "Rhyme_Word_Indices", "datatype": "string", "separator": " "},
            {"name": "Rhyme_IDS", "datatype": "string", "separator": " "},
        )
        args.writer.cldf.add_component(
            "EntryTable",
            "MiddleChinese",
            "OCBS",
            {"name": "Example_IDS", "datatype": "string", "separator": " "},
            )

        args.writer.cldf.add_table('poems.csv', 'ID', 'Title')
        args.writer.cldf.add_foreign_key(
                'ExampleTable', 'Poem_ID', 'poems.csv', 'ID'
                )
        args.writer.cldf.add_foreign_key(
                "EntryTable", "Example_IDS", "examples.csv", "ID")

        args.writer.objects['LanguageTable'].append(
                {
                    'ID': 'OldChinese',
                    'Name': 'Old Chinese',
                    'Glottocode': ''}
                )

        P = {}
        with open(self.raw_dir / "S_shijing_rhymes.corrected.txt") as f:
            poems_ = f.read().split("\n\n\n\n")
            for poem in poems_:
                in_poem = False
                for row in poem.split("\n"):
                    if row.startswith("?"):
                        pass

                    elif row.strip() and row[0].isdigit():
                        name = row
                        in_poem = True
                        stanza = 1
                        P[name] = {stanza: []}
                    elif in_poem:
                        if row.strip():
                            P[name][stanza] += [row.strip()]
                        else:
                            stanza += 1
                            P[name][stanza] = []
        args.log.info("parsed data")

        poems = {}
        idx = 1
        entries = defaultdict(list)
        for i, name in enumerate(P):
            poem_id, poem_name = name.split(". ")
            args.writer.objects["poems.csv"].append({
                "ID": poem_id,
                "Title": poem_name,
                })
            args.log.info("Analyzing poem {0} / {1}".format(i+1, name))
            for stanza in P[name]:
                for i, row in enumerate(P[name][stanza]):
                    for j, (
                            phrase, chars, rhyme_words, rhyme_word_idxs,
                            rhyme_idxs
                            ) in enumerate(parse_line(row)):
                        for word in rhyme_words:
                            entries[word] += [idx]
                        args.writer.objects["ExampleTable"].append({
                            "ID": idx,
                            "Primary_Text": phrase,
                            "Analyzed_Word": chars,
                            "Gloss": "",
                            "Poem_ID": poem_id,
                            "Stanza_Number": stanza,
                            "Line_Number": i+1,
                            "Phrase_Number": j+1,
                            "Language_ID": "OldChinese",
                            "Rhyme_Words": rhyme_words,
                            "Rhyme_Word_Indices": rhyme_word_idxs ,
                            "Rhyme_IDS": ["{0}-{1}".format(
                                poem_id, rid) for rid in rhyme_idxs]
                            })
                        idx += 1
        for i, (entry, occs) in enumerate(entries.items()):
            args.writer.objects["EntryTable"].append({
                "ID": i+1,
                "Language_ID": "OldChinese",
                "Headword": entry,
                "Example_IDS": occs
                })

        # output

        #args.writer.cldf.properties['dc:creator'] = "Johann-Mattis List"

        #language = {
        #    'ID': "OldChinese",
        #    'Name': "Old Chinese",
        #    'Glottocode': "",
        #}
        #args.writer.objects['LanguageTable'] = [language]

        #args.writer.objects['EntryTable'] = entries
        #args.writer.objects['SenseTable'] = senses
        #args.writer.objects['ExampleTable'] = examples
        #args.writer.objects['media.csv'] = media


import logging
from contextlib import contextmanager

@contextmanager
def _handle_logging_level(maximum_level=logging.WARNING):
    '''Context manager to suppress any logging messages under specified level.

    Args:
        The highest logging level to be disabled. Default to logging.WARNING.
    '''

    current_logging_level = logging.root.getEffectiveLevel()
    logging.disable(maximum_level)

    try:
        yield
    finally:
        logging.disable(current_logging_level)


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

import os
import cv2
import numpy as np
import pickle as pkl

def dbpkl_to_dbimgpkl():

    def arange_img_list(db_img_list):
        arr_list = [[] for _ in range(7481)]
        for img_dir in db_img_list:
            img_idx, _ = img_dir.split('_')
            arr_list[int(img_idx)].append(img_dir)
        return arr_list

    def euc_dis(coor1, coor2):
        coor1, coor2 = coor1.astype(np.float32), coor2.astype(np.float32)
        return np.sqrt(np.square(coor1 - coor2).sum())

    db_infos = pkl.load(open('./data/kitti/kitti_dbinfos_train.pkl', 'rb'))

    db_img_list = os.listdir('./data/kitti/gt_database_img/img/')
    db_img_list.sort()
    img_list = arange_img_list(db_img_list)

    img_path = 'data/kitti/gt_database_img/img/'
    txt_path = 'data/kitti/gt_database_img/txt/'

    for cur_class in db_infos.keys():
        connect_cnt = 0
        all_cnt = 0
        print(cur_class)
        for idx in range(len(db_infos[cur_class])):
            all_cnt += 1
            c_db = db_infos[cur_class][idx]
            c_idx = int(c_db['image_idx'])
            if 'bbox' not in c_db:
                lines = open(
                    './data/kitti/training/label_2/%06d.txt' %
                    c_db['image_idx'], 'r').readlines()
                for line in lines:
                    line = line.split(' ')
                    b_size = np.array([float(line[i]) for i in [9, 10, 8]],
                                      dtype=np.float32)
                    if (b_size == c_db['box3d_lidar'][3:6]).sum() == 3:
                        c_db['bbox'] = np.array(
                            [float(line[i]) for i in [4, 5, 6, 7]],
                            dtype=np.float32)
                        break
            c_bbox = np.array(
                [c_db['bbox'][::2].sum() / 2, c_db['bbox'][1::2].sum() / 2])

            if len(img_list[c_idx]) == 0:
                continue
            bet_min, bet_dir, mi_bbox = 30., -1, np.array([-1., -1.])
            for img_dir in img_list[c_idx]:
                i_img = cv2.imread(img_path + img_dir)
                i_H, i_W, _ = i_img.shape
                i_txt = open(txt_path + img_dir[:-4] + '.txt',
                             'r').readline().split(' ')
                i_bbox = np.array([int(a) for a in i_txt])
                i_bbox_ul = i_bbox.copy()
                i_bbox[0], i_bbox[1] = i_bbox[0] + i_W / 2, i_bbox[1] + i_H / 2

                bet_dis = euc_dis(i_bbox[:2], c_bbox)
                if bet_min > bet_dis:
                    bet_min = bet_dis
                    bet_dir = img_dir
                    mi_bbox = i_bbox_ul

            if bet_dir == -1:
                continue
            c_db['img_path'] = img_path + bet_dir
            c_db['img_bbox_coor'] = mi_bbox
            connect_cnt += 1

        print('%d / %d' % (connect_cnt, all_cnt))

    pkl.dump(db_infos, open('data/kitti/kitti_dbinfos_img_train.pkl', 'wb'))

def make_scene_list():

    def match_btw_calib(num1, num2):
        cal1 = open('./data/kitti/training/calib/%06d.txt' % num1).readlines()
        cal2 = open('./data/kitti/training/calib/%06d.txt' % num2).readlines()
        for line1, line2 in zip(cal1, cal2):
            if line1.strip() != line2.strip():
                return False
        return True

    scene_idx = 0
    scene_list = []
    visit = [-1 for _ in range(7481)]
    for i in range(7481):
        if visit[i] != -1:
            continue
        scene_list.append([i])
        for j in range(i + 1, 7481):
            if match_btw_calib(i, j):
                scene_list[scene_idx].append(j)
                visit[j] = scene_idx
        scene_idx += 1

    scene_dict = {'list': scene_list, 'sample': visit}
    pkl.dump(scene_dict, open('./data/kitti/kitti_dbinfos_scene_list.pkl',
                              'wb'))

if __name__ == '__main__':
    dbpkl_to_dbimgpkl()
    make_scene_list()


# <pep8-80 compliant>

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

__author__ = "McBuff, Nutti <nutti.metro@gmail.com>"
__status__ = "production"
__version__ = "5.1"
__date__ = "24 Feb 2018"

from math import sqrt

import bpy
import bmesh
from mathutils import Vector
from bpy.props import EnumProperty

from .. import common

def measure_wsuv_info(obj):
    mesh_area = common.measure_mesh_area(obj)
    uv_area = common.measure_uv_area(obj)

    if not uv_area:
        return None, None, None

    if mesh_area == 0.0:
        density = 0.0
    else:
        density = sqrt(uv_area) / sqrt(mesh_area)

    return uv_area, mesh_area, density

class MUV_WSUVMeasure(bpy.types.Operator):
    """
    Operation class: Measure face size
    """

    bl_idname = "uv.muv_wsuv_measure"
    bl_label = "Measure"
    bl_description = "Measure face size for scale calculation"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        sc = context.scene
        obj = context.active_object

        uv_area, mesh_area, density = measure_wsuv_info(obj)
        if not uv_area:
            self.report({'WARNING'},
                        "Object must have more than one UV map and texture")
            return {'CANCELLED'}

        sc.muv_wsuv_src_uv_area = uv_area
        sc.muv_wsuv_src_mesh_area = mesh_area
        sc.muv_wsuv_src_density = density

        self.report({'INFO'},
                    "UV Area: {0}, Mesh Area: {1}, Texel Density: {2}"
                    .format(uv_area, mesh_area, density))

        return {'FINISHED'}

class MUV_WSUVApply(bpy.types.Operator):
    """
    Operation class: Apply scaled UV
    """

    bl_idname = "uv.muv_wsuv_apply"
    bl_label = "Apply"
    bl_description = "Apply scaled UV based on scale calculation"
    bl_options = {'REGISTER', 'UNDO'}

    origin = EnumProperty(
        name="Origin",
        description="Aspect Origin",
        items=[
            ('CENTER', 'Center', 'Center'),
            ('LEFT_TOP', 'Left Top', 'Left Bottom'),
            ('LEFT_CENTER', 'Left Center', 'Left Center'),
            ('LEFT_BOTTOM', 'Left Bottom', 'Left Bottom'),
            ('CENTER_TOP', 'Center Top', 'Center Top'),
            ('CENTER_BOTTOM', 'Center Bottom', 'Center Bottom'),
            ('RIGHT_TOP', 'Right Top', 'Right Top'),
            ('RIGHT_CENTER', 'Right Center', 'Right Center'),
            ('RIGHT_BOTTOM', 'Right Bottom', 'Right Bottom')

        ],
        default="CENTER"
    )

    def draw(self, _):
        layout = self.layout

        layout.prop(self, "origin")

    def execute(self, context):
        sc = context.scene
        obj = context.active_object
        bm = bmesh.from_edit_mesh(obj.data)
        if common.check_version(2, 73, 0) >= 0:
            bm.verts.ensure_lookup_table()
            bm.edges.ensure_lookup_table()
            bm.faces.ensure_lookup_table()

        sel_faces = [f for f in bm.faces if f.select]

        uv_area, mesh_area, density = measure_wsuv_info(obj)
        if not uv_area:
            self.report({'WARNING'},
                        "Object must have more than one UV map and texture")
            return {'CANCELLED'}

        uv_layer = bm.loops.layers.uv.verify()

        if sc.muv_wsuv_mode == 'PROPORTIONAL':
            tgt_density = sc.muv_wsuv_src_density * sqrt(mesh_area) / \
                sqrt(sc.muv_wsuv_src_mesh_area)
        elif sc.muv_wsuv_mode == 'SCALING':
            tgt_density = sc.muv_wsuv_src_density * sc.muv_wsuv_scaling_factor
        elif sc.muv_wsuv_mode == 'USER':
            tgt_density = sc.muv_wsuv_tgt_density
        elif sc.muv_wsuv_mode == 'CONSTANT':
            tgt_density = sc.muv_wsuv_src_density

        factor = tgt_density / density

        # calculate origin
        if self.origin == 'CENTER':
            origin = Vector((0.0, 0.0))
            num = 0
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin = origin + uv
                    num = num + 1
            origin = origin / num
        elif self.origin == 'LEFT_TOP':
            origin = Vector((100000.0, -100000.0))
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = min(origin.x, uv.x)
                    origin.y = max(origin.y, uv.y)
        elif self.origin == 'LEFT_CENTER':
            origin = Vector((100000.0, 0.0))
            num = 0
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = min(origin.x, uv.x)
                    origin.y = origin.y + uv.y
                    num = num + 1
            origin.y = origin.y / num
        elif self.origin == 'LEFT_BOTTOM':
            origin = Vector((100000.0, 100000.0))
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = min(origin.x, uv.x)
                    origin.y = min(origin.y, uv.y)
        elif self.origin == 'CENTER_TOP':
            origin = Vector((0.0, -100000.0))
            num = 0
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = origin.x + uv.x
                    origin.y = max(origin.y, uv.y)
                    num = num + 1
            origin.x = origin.x / num
        elif self.origin == 'CENTER_BOTTOM':
            origin = Vector((0.0, 100000.0))
            num = 0
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = origin.x + uv.x
                    origin.y = min(origin.y, uv.y)
                    num = num + 1
            origin.x = origin.x / num
        elif self.origin == 'RIGHT_TOP':
            origin = Vector((-100000.0, -100000.0))
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = max(origin.x, uv.x)
                    origin.y = max(origin.y, uv.y)
        elif self.origin == 'RIGHT_CENTER':
            origin = Vector((-100000.0, 0.0))
            num = 0
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = max(origin.x, uv.x)
                    origin.y = origin.y + uv.y
                    num = num + 1
            origin.y = origin.y / num
        elif self.origin == 'RIGHT_BOTTOM':
            origin = Vector((-100000.0, 100000.0))
            for f in sel_faces:
                for l in f.loops:
                    uv = l[uv_layer].uv
                    origin.x = max(origin.x, uv.x)
                    origin.y = min(origin.y, uv.y)

        # update UV coordinate
        for f in sel_faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                diff = uv - origin
                l[uv_layer].uv = origin + diff * factor

        bmesh.update_edit_mesh(obj.data)

        self.report({'INFO'}, "Scaling factor: {0}".format(factor))

        return {'FINISHED'}


import pygame, os, sys, inspect

current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)

from config import *

import Entity.Player
Player = Entity.Player.Player()

BLUE  = (25,25,200)
BLACK = (23,23,23 )
WHITE = (254,254,254)
GRAY = (125, 125, 125)
LIGHT_BLUE = (64, 128, 255)
GREEN = (0, 200, 64)
YELLOW = (225, 225, 0)
PINK = (230, 50, 230)

def LaunchGame(width=WIDTH,height=HEIGHT,caption="My Game"):
	'''
	Initialises Game
	'''
	pygame.init()
	clock = pygame.time.Clock()
	screen = pygame.display.set_mode((width, height))
	pygame.display.set_caption(caption)
	#pygame.mixer.init()  #for sound

	#little p in front of var means Player like pPos — player position
	pPos = Player.Position 	# list of (x,y) player position

	right = True

	# Game Loop
	main = True
	motion = []
	while main == True:

		#EVENT READING
		for event in pygame.event.get():
			if event.type == pygame.QUIT:

				pygame.quit()
				sys.exit()
				main == False

			if event.type == pygame.KEYDOWN:
				if event.key == ord('q'):
					pygame.quit()
					sys.exit()
					main == False

		#KEY COMMANDS
		keys = pygame.key.get_pressed()

		if keys[pygame.K_LEFT]: Player.GoLeft()
		if keys[pygame.K_RIGHT]: Player.GoRight()
		if keys[pygame.K_UP]: Player.GoUp()
		if keys[pygame.K_DOWN]: Player.GoDown()

		#GRAPHICS INITIALIZATION
		screen.fill(BLACK)

		Player.DrawOn(screen)

		f1 = pygame.font.Font(None,18)
		F1 = f1.render('BOOOM',1, LIGHT_BLUE)

		pygame.draw.rect(screen,PINK, [100,100,200,500])
		aa = pygame.Rect(100,100,200,500)

		if Player.Tile.colliderect(aa):
			screen.blit(F1,(width/2,height/2))
			pygame.display.update()

		#Player.Tile.bottomright #for collisions

		pygame.display.update(Player.Tile)
		clock.tick(FPS)


import datetime

def int_to_time(int_number):
    temptime = datetime.timedelta(milliseconds=int_number)
    minutes = temptime.seconds // 60
    seconds = temptime.seconds % 60
    milliseconds = round(temptime.microseconds / 1000)
    if milliseconds < 100:
        milliseconds = milliseconds / 1000
        if seconds < 10:
            return str(minutes) + ":0" + str(seconds + milliseconds)
        else:
            return str(minutes) + ":" + str(seconds + milliseconds)
    else:
        if seconds < 10:
            return str(minutes) + ":0" + str(seconds) + "." + str(milliseconds)
        else:
            return str(minutes) + ":" + str(seconds) + "." + str(milliseconds)


import argparse
import sys
import time

import tucal

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('file', type=str)
    args = parser.parse_args()

    reader = tucal.JobStatus()

    with sys.stdin if args.file == '-' else open(args.file, 'r+') as f:
        while not reader.finished and not f.closed:
            line = f.readline()
            if not reader.line(line):
                time.sleep(0.125)
                continue

            if reader.get_current_step() is None:
                continue

            sec = reader.time
            perc = reader.progress * 100
            step = reader.get_current_step()

            steps = ' - '.join([f'{s["name"]} ({n}/{p})' for s, n, p in reader.path()])
            print(f'\r\x1B[2K', end='')
            print(f'{int(sec // 60):02}:{sec % 60:04.1f} - {perc:5.1f}% - {steps}: {step["name"][:100]} ', end='')
            sys.stdout.flush()
        f.read()
        print()


import logging
import enoslib as en
import os
import datetime
import subprocess
import time
#Upload launch script to site frontend
def execute_ssh_command(launch_script, login, site):
    # SSH command in the format 'ssh <username>@<hostname> "<command>"'
    ssh_command = f'scp {launch_script} {login}@access.grid5000.fr:{site}'

    try:
        # Execute the SSH command
        result = subprocess.run(ssh_command, shell=True, capture_output=True, text=True)
        print("Script send to frontend")
        # Check if the command was successful
        if result.returncode == 0:
            # Print the output
            print(result.stdout)
        else:
            # Print the error message
            print(result.stderr)

    except subprocess.CalledProcessError as e:
        print(f"Error occurred while executing SSH command: {e}")

#Get timestamp after end of experiment
def add_time(original_time, hours=0, minutes=0, seconds=0):
    time_delta = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
    new_time = original_time + time_delta
    return new_time

def convert_seconds_to_time(seconds):
    hours, remainder = divmod(seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    return hours, minutes, seconds

def seconds_to_hh_mm_ss(seconds):
    hours = seconds // 3600
    minutes = (seconds % 3600) // 60
    seconds = seconds % 60
    return f"{hours:02d}:{minutes:02d}:{seconds:02d}"

#Experiment node partition between Grid5000 machine
def node_partition(nb_cluster_machine, nb_builder, nb_validator, nb_regular):
    partition = [[0, 0, 0] for i in range(nb_cluster_machine)]

    index = 0
    while nb_builder > 0 or nb_validator > 0 or nb_regular > 0:
        if index == len(partition):
            index  = 0
        if nb_builder > 0:
            partition[index][0] += 1
            nb_builder -= 1
        elif nb_validator > 0:
            partition[index][1] += 1
            nb_validator -= 1
        elif nb_regular > 0:
            partition[index][2] += 1
            nb_regular -= 1
        index += 1
    return partition

def main():

    #========== Parameters ==========
    #Grid5000 parameters
    login = "mapigaglio" #Grid5000 login
    site = "nancy" #Grid5000 Site See: https://www.grid5000.fr/w/Status and https://www.grid5000.fr/w/Hardware
    cluster = "grisou" #Gride5000 Cluster name See: https://www.grid5000.fr/w/Status and https://www.grid5000.fr/w/Hardware
    job_name = "PANDAS"

    #Node launch script path
    dir_path = os.path.dirname(os.path.realpath(__file__)) #Get current directory path
    launch_script = dir_path +"/" + "run.sh"

    #Experiment parameters
    nb_cluster_machine = 10 #Number of machine booked on the cluster
    nb_experiment_node = 160 #Number of nodes running for the experiment
    nb_builder = 1
    nb_validator = 40
    nb_regular = nb_experiment_node - nb_builder - nb_validator
    exp_duration = 120  #In seconds
    experiment_name = "PANDAS"
    current_datetime = datetime.datetime.now()
    experiment_name += current_datetime.strftime("%Y-%m-%d-%H:%M:%S")
    #Network parameters
    delay = "10%"
    rate = "1gbit"
    loss = "0%"
    symmetric=True

    #========== Experiment nodes partition on cluster machines ==========
    partition = node_partition(nb_cluster_machine, nb_builder, nb_validator, nb_regular)

    #========== Create and validate Grid5000 and network emulation configurations ==========
    #Log to Grid5000 and check connection
    en.init_logging(level=logging.INFO)
    en.check()
    network = en.G5kNetworkConf(type="prod", roles=["experiment_network"], site=site)
    Job_walltime = seconds_to_hh_mm_ss(exp_duration + 120)
    conf = (
        en.G5kConf.from_settings(job_name=job_name, walltime=Job_walltime)
        .add_network_conf(network)
        .add_machine(roles=["experiment"], cluster=cluster, nodes=nb_cluster_machine, primary_network=network) #Add experiment nodes
        .finalize()
    )

    #Validate Grid5000 configuration
    start = datetime.datetime.now() #Timestamp grid5000 job start
    provider = en.G5k(conf)
    roles, networks = provider.init(force_deploy=True)
    roles = en.sync_info(roles, networks)

    #========== Grid5000 network emulation configuration ==========
    #network parameters
    netem = en.NetemHTB()
    (
        netem.add_constraints(
            src=roles["experiment"],
            dest=roles["experiment"],
            delay=delay,
            rate=rate,
            loss=loss,
            symmetric=symmetric,
        )
    )

    #Deploy network emulation
    netem.deploy()
    netem.validate()

    #========== Deploy Experiment ==========
    #Send launch script to Grid5000 site frontend
    execute_ssh_command(launch_script, login, site)
    i = 0
    for x in roles["experiment"]:
        with en.actions(roles=x, on_error_continue=True, background=True) as p:
            builder, validator, regular = partition[i]
            p.shell(f"/home/{login}/run.sh {exp_duration} {experiment_name} {builder} {validator} {regular} {login}")
            i += 1

    start = datetime.datetime.now() #Timestamp grid5000 job start

    #========== Wait job and and release grid5000 ressources ==========
    #Print experiment duration
    h,m,s = convert_seconds_to_time(exp_duration)
    print("Begin at: ",start)
    print("Expected to finish at: ",add_time(start,h,m,s + 10))
    time.sleep(exp_duration + 20)

    #Release all Grid'5000 resources
    netem.destroy()
    provider.destroy()

if __name__ == "__main__":
    main()


#Implementation of Linear regression with machine learning scikit learn
import numpy
import pandas
import matplotlib.pyplot as plt
import os
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# print(os.getcwd())
# os.chdir('/Users/harshshetye/Desktop')
# print(os.getcwd())

# names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
# data = pandas.read_csv('iris (1).csv', names=names)

# x = data['sepal-length'].values
# y = data['sepal-width'].values
# #cannot use rank 1 matrix in scikit learn
# n = len(x)
# x = x.reshape((n,1)) #making a matrix

# #Creating model
# reg = LinearRegression()
# #Fitting trainig data
# reg = reg.fit(x, y)

# #Y prediction
# y_pre = reg.predict(x)

# #Calculating R2 score
# R2 = reg.score(x, y)

# print(R2)

rng = numpy.random.RandomState(1)
x = 10 * rng.rand(50)
y = 2*x - 5 + rng.rand(50)

plt.scatter(x, y)
plt.show()

model = LinearRegression(fit_intercept=True)
model.fit(x[:, numpy.newaxis], y)
xfit = numpy.linspace(0, 10, 1000)
yfit = model.predict(xfit[:, numpy.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit)

plt.show()

print('Model slope : ', model.coef_)
print('Model Intercept : ', model.intercept_)

#Testing with multidimensional data

rng = numpy.random.RandomState(1)
X = 10 * rng.rand(100, 3)
y = 0.5 + numpy.dot(X, [1.5, -2., 1.])

model.fit(X, y)
print('Model slope : ', model.coef_)
print('Model intercept : ', model.intercept_)