import json

from pprint import pprint

import requests

from flight_data import FlightData

TEQUILA_API_KEY = ''
tequila_endpoint = 'https://tequila-api.kiwi.com/v2/search'

location_endpoint = f"{tequila_endpoint}/locations/query"
search_endpoint = f"{tequila_endpoint}/v2/search"

tequila_headers = {

    'apikey ': TEQUILA_API_KEY
}

class FlightSearch:

    def get_destination_code(self, city_name):

        query = {

            'term ': city_name,
            'location_types': 'city'
        }
        response = requests.get(url=location_endpoint, headers=tequila_headers, params=query)

        try:
            results = response.json()["locations"]
            code = results[0]["code"]
            return code
        except json.decoder.JSONDecodeError:
            print('The responses can be gzipped, if request header accept-encoding:gzip, '
                  'and need to be unpacked if response header is Content-Encoding: gzip ')

    def check_flights(self, origin_city_code, destination_city_code, from_time, to_time):

        query = {
            "fly_from": origin_city_code,
            "fly_to": destination_city_code,
            "date_from": from_time.strftime("%d/%m/%Y"),
            "date_to": to_time.strftime("%d/%m/%Y"),
            "nights_in_dst_from": 7,
            "nights_in_dst_to": 28,
            "flight_type": "round",
            "max_stopovers": 0,
            "curr": "GBP"
        }

        response = requests.get(url=search_endpoint, headers=tequila_headers, params=query)

        try:
            check_flight_data = response.json()["data"][0]

        except json.decoder.JSONDecodeError:
            print('The responses can be gzipped, if request header accept-encoding:gzip, '
                  'and need to be unpacked if response header is Content-Encoding: gzip ')

        except IndexError:

            query["max_stopovers"] = 1

            response = requests.get(url=f"{search_endpoint}/v2/search", headers=tequila_headers, params=query)
            check_flight_data = response.json()["data"][0]

            pprint(check_flight_data)

            flight_data = FlightData(
                price=check_flight_data["price"],
                origin_city=check_flight_data["route"][0]["cityFrom"],
                origin_airport=check_flight_data["route"][0]["flyFrom"],

                destination_city=check_flight_data["route"][1]["cityTo"],
                destination_airport=check_flight_data["route"][1]["flyTo"],

                out_date=check_flight_data["route"][0]["local_departure"].split("T")[0],

                return_date=check_flight_data["route"][2]["local_departure"].split("T")[0],

                stop_overs=1,

                via_city=check_flight_data["route"][0]['cityTo']
            )
            return flight_data

        else:
            flight_data = FlightData(
                price=check_flight_data["price"],
                origin_city=check_flight_data["route"][0]["cityFrom"],
                origin_airport=check_flight_data["route"][0]["flyFrom"],
                destination_city=check_flight_data["route"][0]["cityTo"],
                destination_airport=check_flight_data["route"][0]["flyTo"],
                out_date=check_flight_data["route"][0]["local_departure"].split("T")[0],
                return_date=check_flight_data["route"][1]["local_departure"].split("T")[0]
                )
            print(f"{flight_data.destination_city}: £{flight_data.price}")
            return flight_data


#!/usr/bin/env python
import os
import sys

from setuptools import setup
from setuptools.command.test import test as TestCommand

from jsonmodels import __author__, __email__, __version__

PROJECT_NAME = "jsonmodels"

if sys.argv[-1] == "publish":
    os.system("python setup.py sdist upload")
    sys.exit()

class PyTest(TestCommand):
    user_options = [("pytest-args=", "a", "Arguments to pass to py.test")]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.pytest_args = ["--cov", PROJECT_NAME]

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        import pytest

        errno = pytest.main(self.pytest_args)
        sys.exit(errno)

# Hacking tests.
try:
    import tests
except ImportError:
    pass
else:
    if "test" in sys.argv and "--no-lint" in sys.argv:
        tests.LINT = False
        del sys.argv[sys.argv.index("--no-lint")]

    if "test" in sys.argv and "--spelling" in sys.argv:
        tests.CHECK_SPELLING = True
        del sys.argv[sys.argv.index("--spelling")]

readme = open("README.rst").read()
history = open("HISTORY.rst").read().replace(".. :changelog:", "")

setup(
    name=PROJECT_NAME,
    version=__version__,
    description="Models to make easier to deal with structures that"
    " are converted to, or read from JSON.",
    long_description=readme + "\n\n" + history,
    author=__author__,
    author_email=__email__,
    url="https://github.com/jazzband/jsonmodels",
    packages=[
        PROJECT_NAME,
    ],
    package_dir={PROJECT_NAME: PROJECT_NAME},
    include_package_data=True,
    install_requires=[
        "python-dateutil",
    ],
    license="BSD",
    zip_safe=False,
    keywords=PROJECT_NAME,
    python_requires=">=3.8",
    classifiers=[
        "Intended Audience :: Developers",
        "License :: OSI Approved :: BSD License",
        "Natural Language :: English",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Programming Language :: Python :: 3 :: Only",
    ],
    cmdclass={
        "test": PyTest,
    },
)


import statsmodels.api as sm
from statsmodels.tsa.stattools import adfuller
import matplotlib.pyplot as plt
from matplotlib import rcParams
plt.style.use('ggplot')
import pandas as pd
from data_frame import *
from static import *
import numpy as np
import calmap
from scipy import stats
from parameters import *

def stationarity_test(filepath:str, col:str, reference_date, current_date, moveback_date):
    data = data_frame(filepath, reference_date, current_date, moveback_date).before_after
    before, after = data.before, data.after
    result_before, result_after=adfuller(before[col]), adfuller(after[col])
    stationary = \
         {
             'before':
             {
              'ADF':      result_before[0],
              'p-value':  result_before[1],
              '1%':       result_before[4]['1%'],
              '5%':       result_before[4]['5%'],
              '10%':      result_before[4]['10%'],
             },
              'after':
             {
              'ADF':      result_after[0],
              'p-value':  result_after[1],
              '1%':       result_after[4]['1%'],
              '5%':       result_after[4]['5%'],
              '10%':      result_after[4]['10%'],
              }
        }

    return stationary

vcdf = np.vectorize(lambda value, array: (array<value).sum()/len(array), excluded = ['array'])

def confidence_interval(mean, se, pct:str):
    lower, upper = mean - two_sided_confidence_level[pct]*se, \
        mean + two_sided_confidence_level[pct]*se

    return {'lower':lower, 'upper':upper}

def plot_hlines(ax, ys: list, x: list, colors: list, linestyles: list):
    for y, color, linestyle in zip(ys, colors, linestyles):
        ax.hlines(y, x[0], x[1], colors=color, linestyles=linestyle)

def plot_vlines(ax, xs: list, y: list, colors: list, linestyles: list, linewidths:list):
    for x, color, linestyle, linewidth in zip(xs, colors, linestyles, linewidths):
        ax.vlines(x, y[0], y[1], colors=color, linestyles=linestyle, linewidths=linewidth)

def plot_with_fill(ax, x, y, label,color):
    lines = ax.plot(x, y, label=label, lw=2, color=color)
    ax.fill_between(x, 0, y, alpha=0.2, color=lines[0].get_c())

def set_axis_sizes(ax, size=18):
    ax.tick_params(axis='x', labelsize=size)
    ax.tick_params(axis='y', labelsize=size)

def put_text(ax, X:list, Y:list, text:list, font_spec:list, align: list):
    for x, y, text_item, font_item, align_item in zip(X, Y, text, font_spec, align):
        ax.text(x, y, text_item, fontdict=font_item, horizontalalignment=align_item)

def create_heatmap(ax, fig, series, year=2020, cmap='YlGn'):
    cax = calmap.yearplot(series, year=2020, ax=ax, cmap=cmap)
    fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal')
    set_axis_sizes(ax)

def statistics(filepaths):
    '''
     Compute sample mean, sample std, and std. error for a sample
        input: a filepath (str)
        output: a dictionary of statistics
    '''
    before_after_dict = dict()
    prop = dict()

    for filepath in filepaths:
        data = data_frame(filepath, reference_date, current_date, moveback_date).before_after
        before, after = data.before, data.after

        before_after_dict[refer[filepath]] = {'before': before['Pct_Diff'],'after': after['Pct_Diff']}
        prop[refer[filepath]] = \
            {
                'before':
                {
                'mean': round(before['Pct_Diff'].mean(),3),
                'std':  round(before['Pct_Diff'].std(), 3),
                'se':   round(before['Pct_Diff'].sem(), 3)
                },
                'after':
                {
                'mean': round(after['Pct_Diff'].mean(), 3),
                'std':  round(after['Pct_Diff'].std(), 3),
                'se':   round(after['Pct_Diff'].sem(), 3)
                }
            }

    return prop

def t_test(filepaths):
    hyp_test = dict()
    for filepath in filepaths:
        data = data_frame(filepath, reference_date, current_date, moveback_date).before_after
        before, after = data.before['Pct_Diff'], data.after['Pct_Diff']
        stat, p_val = stats.ttest_ind(before, after, axis=0, equal_var=False, nan_policy='omit')
        hyp_test[refer[filepath]] = {'t_stat': stat, 'p_val': p_val}

    return hyp_test

def draw_heatmap(filepaths, fig, axs):
    for filepath, ax in zip(filepaths, axs):
        test = data_frame(filepath, reference_date, current_date, moveback_date).df
        series = test['Close']
        create_heatmap(ax, fig, series)
        ax.set_title(f'{refer[filepath]} Stock Closing Price($) in 2020', fontdict=font, size=30)
        set_axis_sizes(ax, 30)

def bayesian_count(filepaths):
    bayesian_count={}
    for filepath in filepaths:
        data = data_frame(filepath, reference_date, current_date, moveback_date).before_after
        before, after = data.before, data.after
        before_count = before[before[test_dim]>0][test_dim].count() / before[test_dim].count()
        after_count = after[after[test_dim]>0][test_dim].count() / after[test_dim].count()
        bayesian_count[refer[filepath]] = \
                                        {
                                        'before': {'positive': before_count, 'negative': 1-before_count, 'count': before['Pct_Diff'].count()},
                                        'after' : {'positive': after_count, 'negative': 1-after_count, 'count': after['Pct_Diff'].count()}
                                        }

    return bayesian_count


from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.db.models import Count
from django.http import HttpResponse
from django.shortcuts import render

from apps.web.models import Competition

User = get_user_model()

@login_required
def analytics_detail(request):
    if not request.user.is_staff:
        return HttpResponse(status=404)

    monthly_total_users_joined = {}
    for date in User.objects.all().datetimes('date_joined', 'month'):
        if date.year not in monthly_total_users_joined:
            monthly_total_users_joined[date.year] = {}
        users_joined_this_month = User.objects.filter(date_joined__month=date.month,
                                                      date_joined__year=date.year).aggregate(Count('pk'))
        monthly_total_users_joined[date.year][date.strftime("%B")] = users_joined_this_month['pk__count']

    return render(request, "analytics/analytics.html", {
        'registered_user_count': User.objects.all().count(),
        'competition_count': Competition.objects.all().count(),
        'competitions_published_count': Competition.objects.filter(published=True).count(),
        'monthly_total_users_joined': monthly_total_users_joined,
    })


from datasets import GeneratorBasedBuilder, BuilderConfig, Version, DatasetInfo, Features, Value, \
    Sequence, ClassLabel, DownloadManager, SplitGenerator, Split
import os
import textwrap
from .nerkor import NerKorConfig, get_repo_url

_CITATION = """
@inproceedings{NerKorCarsONPP,
  author    = {Nov{\'a}k Attila and
               Nov{\'a}k Borb{\'a}la,
  title     = {{NerKor} 1.41e},
  booktitle = {XVIII. Magyar Számítógépes Nyelvészeti Konferencia},
  year      = {2022},
}
"""
_SOURCE = "https://github.com/novakat/NYTK-NerKor-Cars-OntoNotesPP/archive/eb94fc3c22ed27589593716e150d73e060e2333d.zip"
_SUBS = ["fiction", "legal", "news", "web", "wikipedia", "cars", "all"]

# Missing tags from readme
_ONPP_TAGS = ["PER", "FAC", "ORG", "GPE", "LOC", "PROD", "EVENT", "WORK_OF_ART", "LAW", "NORP", "LANGUAGE", "DATE",
              "TIME", "PERCENT", "MONEY", "QUANTITY", "ORDINAL", "CARDINAL", "AWARD", "CAR", "MEDIA", "SMEDIA",
              "ORG-GPE", "PROJ", "MISC", "MISC-ORG", "MISC-PER", "MISC-LOC", "DUR", "ID", "AGE", "BUS", "MOTOR", "PRIZE"]
_ONPP_NER = ClassLabel(
    names=["O"] + [f"{pre}-{tag}" for tag in _ONPP_TAGS for pre in ['B', 'I']]
)

class NerKorExtended(GeneratorBasedBuilder):
    """NerKor 1.41e datasets."""
    # FORM ONPP:NER
    BUILDER_CONFIGS = [
        NerKorConfig(
            name=name,
            description=textwrap.dedent(""""""),
            text_features=["form", "ner"],
            data_url=_SOURCE,
            data_dir=f"nerkor_1_41e/",
            url=get_repo_url(_SOURCE)
        ) for name in _SUBS
    ]

    def _info(self):
        features = {
            "idx": Value("int32"),
            "text": Value("string"),
            "tokens": Sequence(Value("string")),
            "ner": Sequence(_ONPP_NER),
            "file_name": Value("string"),
            "sentence_id": Value("int32"),
            "morph_tagged": Value("bool_"),
        }
        return DatasetInfo(
            description=self.config.description,
            features=Features(features),
            homepage=self.config.url,
            citation=textwrap.dedent(_CITATION),
        )

    def _split_generators(self, dl_manager: DownloadManager):
        path = dl_manager.download_and_extract(self.config.data_url)
        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={"split_key": "train", "data_file": path},
            ),
            SplitGenerator(
                name=Split.VALIDATION,
                gen_kwargs={"split_key": "validation", "data_file": path},
            ),
            SplitGenerator(
                name=Split.TEST,
                gen_kwargs={"split_key": "test", "data_file": path},
            )
        ]

    def _generate_examples(self, data_file, split_key, **kwargs):
        base_path = os.path.join(data_file, os.listdir(data_file)[0], "data")
        n = 0
        split = split_key if split_key != "validation" else "devel"
        files = [x for x in os.listdir(base_path) if split in x and (self.config.name in x or self.config.name == "all")]
        for file in files:
            for data in self._process_files(os.path.join(base_path, file), n):
                yield data
                n += 1

    @staticmethod
    def _process_files(data_file_path: str, n: int):
        with open(data_file_path, mode="r", encoding="utf8") as f:
            content = f.readlines()

        tokens, ner = [], []

        sentence_id = 0
        for i, row in enumerate(content[1:]):
            if row == '\n':
                p = data_file_path.split("/")[-1]
                yield n, {
                    "idx": n,
                    "text": " ".join(tokens),
                    "tokens": tokens,
                    "ner": ner,
                    "file_name": p,
                    "sentence_id": sentence_id,
                    "morph_tagged": not ('no-morph' in p)
                }
                tokens, ner = [], []
                sentence_id += 1
                n += 1
                continue

            data = row.split("\t")

            tokens.append(data[0])
            ner.append(data[1].rstrip('\n'))


import pygame,sys
from PIL import Image
import random
import numpy as np
from pygame.locals import *
pygame.init()
pygame.display.set_caption("九宫格")
screen =pygame.display.set_mode([1000,1000])
myfont=pygame.font.SysFont(None,30)
img=Image.open(r'img\0.png')
size=img.size
i,j=2,2
h=int(size[1]/3)
w=int(size[0]/3)
tuxiang=[1,2,3],[4,5,6],[7,8,9]
tuxiang=np.asarray(tuxiang)
img1=pygame.image.load(r'九宫格\10.png')
img2=pygame.image.load(r'九宫格\11.png')
img3=pygame.image.load(r'九宫格\12.png')
img4=pygame.image.load(r'九宫格\13.png')
img5=pygame.image.load(r'九宫格\14.png')
img6=pygame.image.load(r'九宫格\15.png')
img7=pygame.image.load(r'九宫格\16.png')
img8=pygame.image.load(r'九宫格\17.png')
img9=pygame.image.load(r'九宫格\18.png')
def fenge():
    img=Image.open(r'img\0.png')
    size=img.size
    h=size[1]/3
    w=size[0]/3
    n=3
    k=1
    for i in range(n):
        for j in range(n):
            a=img.crop((i*w,j*h,(i+1)*w,(j+1)*h))
            a.save(str(k)+'.png')
            k+=1
while True:
    screen.fill([0,0,0])
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key==K_LEFT:
                if i>0:
                    tuxiang[i][j],tuxiang[i-1][j]=tuxiang[i-1][j],tuxiang[i][j]
                    i-=1
                    print(tuxiang)
            elif event.key==K_RIGHT:
                if i<2:
                    tuxiang[i][j],tuxiang[i+1][j]=tuxiang[i+1][j],tuxiang[i][j]
                    i+=1
                    print(tuxiang)
            elif event.key==K_UP:
                if j>0:
                    tuxiang[i][j],tuxiang[i][j-1]=tuxiang[i][j-1],tuxiang[i][j]
                    j-=1
                    print(tuxiang)
            elif event.key==K_DOWN:
                if j<2:
                    tuxiang[i][j],tuxiang[i][j+1]=tuxiang[i][j+1],tuxiang[i][j]
                    j+=1
                    print(tuxiang)
        if event.type == pygame.QUIT:
            sys.exit()
    for a in range(3):
        for b in range(3):
            if tuxiang[a][b]==1:
                screen.blit(img1,(a*h,b*h))
            elif tuxiang[a][b]==2:
                screen.blit(img2,(a*h,b*h))
            elif tuxiang[a][b]==3:
                screen.blit(img3,(a*h,b*h))
            elif tuxiang[a][b]==4:
                screen.blit(img4,(a*h,b*h))
            elif tuxiang[a][b]==4:
                screen.blit(img4,(a*h,b*h))
            elif tuxiang[a][b]==5:
                screen.blit(img5,(a*h,b*h))
            elif tuxiang[a][b]==6:
                screen.blit(img6,(a*h,b*h))
            elif tuxiang[a][b]==7:
                screen.blit(img7,(a*h,b*h))
            elif tuxiang[a][b]==8:
                screen.blit(img8,(a*h,b*h))
            elif tuxiang[a][b]==9:
                screen.blit(img9,(a*h,b*h))
    pygame.display.update()


# coding=utf-8
import scrapy

from scrapy.http import Request
from biliob_spider.items import VideoItem
from datetime import datetime
import time
import json
import logging
from pymongo import MongoClient
from db import db
from util import sub_channel_2_channel
from biliob_spider.spiders.video_spider import VideoSpider

class VideoSpiderAll(VideoSpider):
    name = "videoSpiderAll"
    allowed_domains = ["bilibili.com"]
    start_urls = []
    custom_settings = {
        'ITEM_PIPELINES': {
            'biliob_spider.pipelines.VideoPipeline': 300,
        }
    }

    def __init__(self):

        self.db = db  # 获得数据库的句柄
        self.coll = self.db['video']  # 获得collection的句柄

    def start_requests(self):
        # 只需要aid
        c = self.coll.find({}, {'aid': 1})

        x = 0

        aid_list = []
        for each_doc in c:
            x = x + 1
            aid_list.append(each_doc['aid'])
        i = 0
        while aid_list != []:
            if i == 0:
                aid_str = ''
            aid_str += str(aid_list.pop()) + ','
            i = i + 1
            if i == 50 or aid_list == []:
                i = 0
                yield Request(
                    "https://api.bilibili.com/x/article/archives?ids=" +
                    aid_str.rstrip(','))


import os
from unittest import TestCase, installHandler, main
from test.support import EnvironmentVarGuard
from threading import Thread
from logging import getLogger
from logging.handlers import QueueHandler
from multiprocessing import Process, Event, Queue
from watsor.stream.log import LogHandler
from watsor.stream.share import FrameBuffer
from watsor.stream.sync import CountDownLatch
from watsor.detection.detector import create_object_detectors
from watsor.filter.sieve import DetectionSieve
from watsor.filter.track import TrackFilter
from watsor.filter.confidence import ConfidenceFilter
from watsor.stream.share import RateLimiter
from watsor.config.coco import get_coco_class
from watsor.test.detect_stream import Artist, ShapeCounter

class TestDetect(TestCase):

    def setUp(self):
        if not os.path.exists(self._get_model_path()):
            self.skipTest('Test skipped due to the absence of shape object model')

        installHandler()

    def test_shape_detection(self):
        """Tests TensorFlow object detection using the trained model of simple geometric shapes.
        The detector recognises shapes drawn on a frame image, the sieve filters those having confidence
        above 50%, lastly shapes are counted signalling to end the test.
        """

        with EnvironmentVarGuard() as env:
            env.set("TF_CPP_MIN_LOG_LEVEL", "3")
            env.set("CORAL_VISIBLE_DEVICES", "")
            env.set("CUDA_VISIBLE_DEVICES", "")

            frame_buffer = FrameBuffer(10, 100, 100)

            frame_queue = Queue(1)
            subscriber_queue = Queue(1)
            detection_sieve_queue = Queue(1)

            log_queue = Queue()
            getLogger().addHandler(QueueHandler(log_queue))

            stop_process_event = Event()

            latch = CountDownLatch(100)

            artist = Artist("artist", stop_process_event, log_queue, frame_queue, frame_buffer)

            detection_sieve = DetectionSieve("sieve", stop_process_event, log_queue,
                                             detection_sieve_queue, frame_buffer,
                                             self._create_filters(), RateLimiter())

            processes = [artist, detection_sieve,
                         LogHandler(Thread, "logger", stop_process_event, log_queue, filename=None),
                         ShapeCounter(Thread, "counter", stop_process_event, log_queue, subscriber_queue,
                                      frame_buffer, latch)]

            processes += create_object_detectors(Process, stop_process_event, log_queue, frame_queue,
                                                 {artist.name: frame_buffer}, self._get_model_path())

            artist.subscribe(detection_sieve_queue)
            detection_sieve.subscribe(subscriber_queue)

            for process in processes:
                process.start()

            try:
                self.assertTrue(latch.wait(15))
            finally:
                stop_process_event.set()
                for process in processes:
                    process.join(30)

    @staticmethod
    def _create_filters():
        return [TrackFilter([ConfidenceFilter({
            'detect': [{
                get_coco_class(1).label: {  # triangle
                    'confidence': 50
                }}, {
                get_coco_class(2).label: {  # ellipse
                    'confidence': 50
                }}, {
                get_coco_class(3).label: {  # rectangle
                    'confidence': 50
                }}]})], 1, 1)]

    @staticmethod
    def _get_model_path():
        return os.path.join(os.path.dirname(__file__), 'model')

if __name__ == '__main__':
    main(verbosity=2)


import pygame
import pygame.draw
import pygame.gfxdraw

from store.images import util

def spear():
	color = (0,0,0)
	spearHeight = 60
	spearWidth = 2

	spearImage = util.createAlphaSurface((5, spearHeight))
	pygame.draw.rect(spearImage, color, pygame.Rect((2, 0), (spearWidth, spearHeight)))
	pygame.draw.rect(spearImage, color, pygame.Rect((1, 10), (4, 2)))

	return spearImage

def longSword():
	color = (0, 0, 0)

	swordImage = util.createAlphaSurface((8, 40))
	pygame.draw.rect(swordImage, color, pygame.Rect((3, 0), (2, 40)))
	pygame.draw.rect(swordImage, color, pygame.Rect((0, 30), (8, 2)))

	return swordImage


import unittest
import io
from gdl import Lexer, NoInputError

class MockFile(io.StringIO):
    name = '__mock__'

class TestLexer(unittest.TestCase):
    def test_lex_tokens(self):
        answer = ['(', '<=', '(', 'ancestor', '?a', '?c', ')', '(', 'parent', '?a', '?b', ')', '(', 'ancestor', '?b', '?c', ')', ')']
        string = '(<= (ancestor ?a ?c) (parent ?a ?b) (ancestor ?b ?c))'
        tokens = list(t.value for t in Lexer().lex(string))
        self.assertEqual(tokens, answer)

    def test_lex_superflouos_whitespace(self):
        answer = ['(', '<=', '(', 'ancestor', '?a', '?c', ')', '(', 'parent', '?a', '?b', ')', '(', 'ancestor', '?b', '?c', ')', ')']
        string = '''
        (  <=  (ancestor   ?a  ?c    )                 (
                parent   ?a  ?b
        ) (
                ancestor ?b                ?c  )   )
        '''
        tokens = list(t.value for t in Lexer().lex(string))
        self.assertEqual(tokens, answer)

    def test_lex_columns(self):
        answer = [1, 2, 5, 6, 15, 18, 20, 22, 23, 30, 33, 35, 37, 38, 47, 50, 52, 53]
        string = '(<= (ancestor ?a ?c) (parent ?a ?b) (ancestor ?b ?c))'
        columns = list(t.column for t in Lexer().lex(string))
        self.assertEqual(columns, answer)

    def test_lex_linenos(self):
        answer = [1] * 7 + [2] * 5 + [3] * 6
        string = '''(<= (ancestor ?a ?c)
                        (parent ?a ?b)
                        (ancestor ?b ?c))
                 '''
        linenos = list(t.lineno for t in Lexer().lex(string))
        self.assertEqual(linenos, answer)

    def test_lex_lines(self):
        answer = ['(<= (ancestor ?a ?c)\n'] * 7 + \
                 ['                        (parent ?a ?b)\n'] * 5 + \
                 ['                        (ancestor ?b ?c))\n'] * 6
        string = '''(<= (ancestor ?a ?c)
                        (parent ?a ?b)
                        (ancestor ?b ?c))
                 '''
        lines = list(t.line for t in Lexer().lex(string))
        self.assertEqual(lines, answer)

    def test_lex_file(self):
        answer = [('(', 1, 1),
                  ('<=', 1, 2),
                  ('(', 1, 5),
                  ('ancestor', 1, 6),
                  ('?a', 1, 15),
                  ('?c', 1, 18),
                  (')', 1, 20),
                  ('(', 2, 30),
                  ('parent', 2, 31),
                  ('?a', 2, 38),
                  ('?b', 2, 41),
                  (')', 2, 43),
                  ('(', 3, 30),
                  ('ancestor', 3, 31),
                  ('?b', 3, 40),
                  ('?c', 3, 43),
                  (')', 3, 45),
                  (')', 3, 46)]
        fp = MockFile('''(<= (ancestor ?a ?c)
                             (parent ?a ?b)
                             (ancestor ?b ?c))
                 ''')
        tokens = list((t.value, t.lineno, t.column) for t in Lexer().lex(file=fp))
        self.assertEqual(tokens, answer)

    def test_lex_no_input_error(self):
        with self.assertRaises(NoInputError):
            Lexer().lex()

    def test_lex_case_insensitive(self):
        answer = ['(', '<=', '(', 'ancestor', '?a', '?c', ')', '(', 'parent', '?a', '?b', ')', '(', 'ancestor', '?b', '?c', ')', ')']
        string = '(<= (ANCESTOR ?A ?C) (PARENT ?A ?B) (ANCESTOR ?B ?C))'
        tokens = list(t.value for t in Lexer().lex(string))
        self.assertEqual(tokens, answer)

    def test_lex_comments(self):
        answer = ['(', '<=', '(', 'ancestor', '?a', '?c', ')', '(', 'parent', '?a', '?b', ')', '(', 'ancestor', '?b', '?c', ')', ')']
        string = '''(<= (ancestor ?a ?c)
                        ;;; doc string goes here ;;;
                        (parent ?a ?b)
                        (ancestor ?b ?c)) ;NOTE: this is recursive'''
        tokens = list(t.value for t in Lexer().lex(string))
        self.assertEqual(tokens, answer)


import numpy as np
import torch
from torch.autograd import Variable
import ipdb
def make_one_hot(labels, C=21):
    one_hot = torch.cuda.FloatTensor(labels.size(0), C, labels.size(2), labels.size(3)).zero_()
    target = one_hot.scatter_(1, labels.data, 1)

    target = Variable(target)

    return target

def color_map(N=256, normalized=False):
    def bitget(byteval, idx):
        return ((byteval & (1 << idx)) != 0)

    dtype = 'float32' if normalized else 'uint8'
    cmap = np.zeros((N, 3), dtype=dtype)
    for i in range(N):
        r = g = b = 0
        c = i
        for j in range(8):
            r = r | (bitget(c, 0) << 7-j)
            g = g | (bitget(c, 1) << 7-j)
            b = b | (bitget(c, 2) << 7-j)
            c = c >> 3
            cmap[i] = np.array([r, g, b])
    cmap = cmap/255 if normalized else cmap
    return cmap

cmap = color_map()[:21]
cmap = Variable(torch.from_numpy(cmap)).cuda()

def gt_cmap(gts):
    #target = make_one_hot(gts, C=21)
    gts_stack = torch.stack([gts,gts,gts],1)
    out = torch.zeros_like(gts_stack).float()
    for i in range(21):
        tmp = gts_stack==i
        out += tmp.float()*cmap[i].unsqueeze(-1).unsqueeze(-1).unsqueeze(0).float()
    #target = target.transpose(1,-1)
    #ipdb.set_trace()
    #out = target*cmap
    return out.byte()


#!/usr/bin/env python
"""
A simple ionic model

Pythonised stuff from Remco's spreadsheet
Based on Young et al. 2015
"""

import numpy as np
import scipy.optimize as spopt

def melt_bond_length(p, coeffs):
    """
    Return the bond length (in m) given
    a polynomial expansion in pressure.
    """

    r = 0.0000000001 * (coeffs[0] + p * coeffs[1] + p**2 * coeffs[2])
    return r

def ionic_model_force_constant(r, correction=1.0, offset=0.0, n=12):
    """
    Ionic model force constant following equation 31 of Young (2015).
    All parameters other than r are designed to follow Remco's spreadsheet.

    The optional correction allows a (r independent) change to the force
    constant to be made (should be equivlent to changing the constant term
    in the r expansion but easer to justify).
    """
    zi = 2.0 # Cation valence
    zj = -2.0 # Anion valence
    # n is Born-Mayer constant. Ultimatly LJ I think. Default to 12
    eps0 = 8.854187817E-12 # Vaccum permittivity (F/m)
    e = 1.60217662E-19 # electron charge (C)

    kf = (zi * zj * e**2 * (1-n)) / (4.0 * np.pi * eps0 * r**3)
    kf = (kf * correction) + offset
    return kf

def calculate_force_constant_correction(target_beta, c, t, p=0, mode='correction'):
   """
   Find a correction term for the ionic model force constants such
   that beta for these r coefficents and at this temperature is equal
   to target_beta.
   """
   def get_my_beta(corec):
       r = melt_bond_length(p, c)
       if mode=='correction':
           kf = ionic_model_force_constant(r, correction=corec)
       elif mode=='offset':
           kf = ionic_model_force_constant(r, offset=corec)
       beta = ionic_model_beta(kf, t)
       return beta

   def beta_error(corec):
       return get_my_beta(corec) - target_beta

   if mode=='correction':
       corec_needed = spopt.brentq(beta_error, 0.1, 10)
   elif mode=='offset':
      ks_uncor = ionic_model_force_constant(melt_bond_length(p, c))
      corec_needed = spopt.brentq(beta_error, -0.999 * ks_uncor, 100*ks_uncor )
   return corec_needed

def ionic_model_beta(kf, T):
   """
   Calculate beta as per equation 27 of Young et al. (2015)
   """
   h = 6.62607004E-34 # Plank constant (m^2 kg s^-1)
   kb = 1.38064852E-23 # Boltzman constant (m^2 kg s^-2 K^-1)
   m1 = 24 # amu
   m2 = 26 # amu
   u = 1.6605402E-27 # Unified atomic mass (kg) - an amu in kg

   beta = 1 + (1/24) * (h / (kb * T))**2 * ((1 / (m1 * u)) - (1 / (m2 * u))) * (kf / (4 * np.pi**2))

   # Return 1000.ln(beta) as that's what we do with everything else
   frac_factor = 1000.0 * np.log(beta)
   return frac_factor

def plot_force_constants(pressures, coeff_sets, names=None, styles=None,
                         colors=None, filename=None, kcorrs=None, offsets=None):

    import matplotlib
    if filename is not None:
        matplotlib.use('Agg')
    import matplotlib.pyplot as plt

    fig, axs = plt.subplots(ncols=2)
    if type(coeff_sets) is list or type(coeff_sets) is tuple:
        if (names is None) and (styles is None):
            for coeffs in coeff_sets:
                r = melt_bond_length(pressures, coeffs)
                k = ionic_model_force_constant(r)
                axs[0].plot(pressures, r/1-E10)
                axs[1].plot(pressures, k)
        elif styles is None:
            for coeffs, name in zip(coeff_sets, names):
                r = melt_bond_length(pressures, coeffs)
                k = ionic_model_force_constant(r)
                axs[0].plot(pressures, r/1E-10, label=name)
                axs[1].plot(pressures, k, label=name)
        else:
            for coeffs, name, style, color, cor, off in zip(coeff_sets, names,
                                                styles, colors, kcorrs, offsets):
                r = melt_bond_length(pressures, coeffs)
                k = ionic_model_force_constant(r, correction=cor, offset=off)
                axs[0].plot(pressures, r/1E-10, label=name, linestyle=style,
                         color=color)
                axs[1].plot(pressures, k, label=name, linestyle=style,
                         color=color)
    else:
        r = melt_bond_length(pressures, coeff_sets)
        k = ionic_model_force_constant(r)
        axs[0].plot(pressures, r/1E-10, "b-")
        axs[1].plot(pressures, k, "b-")

    axs[0].set_ylabel("Bond length (Angstroms)")
    axs[0].set_xlabel("P (GPa)")
    axs[1].set_ylabel("Force constant (N/m ???)")
    axs[1].set_xlabel("P (GPa)")
    plt.tight_layout()

    if names is not None:
        plt.legend(loc=2)

    if filename is not None:
        plt.savefig(filename)
    else:
        plt.show()

if __name__ == "__main__":

    # If run on the command line, just make some plots

    import castep_isotope_sub
    import alpha_plotting

    # From Remco's spreadsheet. r(p) = c0 + c1*p + c2*p^2
    # where r is the bond length (in angstroms here) and
    # p is pressure (in GPa)
    r_coefs_melt_dekoker = [1.9613, -0.00165, 0.0000019]
    r_coefs_melt_fudge =   [2.0909, -0.00165, 0.0000019]

    forsterite_r_0 = 2.1170E-10
    forsterite_r_50 = 1.9520E-10
    forsterite_r_100 = 1.870E-10

    beta_dek_lq = ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(0, r_coefs_melt_dekoker)),1573.0)
    beta_fudge_lq = ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(0, r_coefs_melt_fudge)),1573.0)
    beta_fo = ionic_model_beta(ionic_model_force_constant(
                         forsterite_r_0),1573.0)

    print("Reduced frac factor liquid de Koker:", beta_dek_lq, "per mil")
    print("Reduced frac factor liquid r fudge:", beta_fudge_lq, "per mil")
    print("Reduced frac factor forsterite r:", beta_fo, "per mil")
    print("Fractionation factor de Koker - fo:", beta_fo - beta_dek_lq, "per mil")
    print("Fractionation factor fudge - fo:", beta_fo - beta_fudge_lq, "per mil")

    meaured_alpha = 0.080
    meaured_alpha = 0.07677 # Test to check the bond length fudge matces
    target_beta = meaured_alpha + beta_fo
    k_corr = calculate_force_constant_correction(target_beta,
                                                 r_coefs_melt_dekoker, 1573.0)
    print("We need a correction to the force constant of:", k_corr, "UNITS?")

    print("Corrected beta:", ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(0, r_coefs_melt_dekoker),
                 correction=k_corr), 1573.0))
    print("Corrected alpha:", beta_fo - ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(0, r_coefs_melt_dekoker),
                 correction=k_corr), 1573.0))

    # Plot r and force constant as function of P
    plot_force_constants(np.linspace(0, 150, num=100),
                         [r_coefs_melt_dekoker, r_coefs_melt_fudge, r_coefs_melt_dekoker],
                         names=['de Koker', 'fudge', 'k cor'],
                         kcorrs=[1,1,k_corr],
                         styles=['-','-', ':'],
                         colors=['k', 'b', 'g'], filename="ionic_model_rs.pdf")

    temps = np.concatenate((np.linspace(300.0, 500.0, num=40),
                            np.linspace(501.0, 4000.0, num=40)))

    betas = []
    names = []
    colors = []
    styles = []

    names.append('de Koker 0 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(0, r_coefs_melt_dekoker)),temps))
    colors.append('k')
    styles.append('-')

    names.append('de Koker 50 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(50, r_coefs_melt_dekoker)),temps))
    colors.append('k')
    styles.append('--')

    names.append('de Koker 100 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(100, r_coefs_melt_dekoker)),temps))
    colors.append('k')
    styles.append(':')

    names.append('de Koker 0 GPa kcor')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(0, r_coefs_melt_dekoker),
                 correction=k_corr),temps))
    colors.append('y')
    styles.append('-')

    names.append('de Koker 50 GPa kcor')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(50, r_coefs_melt_dekoker),
                 correction=k_corr),temps))
    colors.append('y')
    styles.append('--')

    names.append('de Koker 100 GPa kcor')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(100, r_coefs_melt_dekoker),
                 correction=k_corr),temps))
    colors.append('y')
    styles.append(':')

    names.append('fudge 0 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(0, r_coefs_melt_fudge)),temps))
    colors.append('b')
    styles.append('-')

    names.append('fudge 50 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(50, r_coefs_melt_fudge)),temps))
    colors.append('b')
    styles.append('--')

    names.append('fudge 100 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 melt_bond_length(100, r_coefs_melt_fudge)),temps))
    colors.append('b')
    styles.append(':')

    names.append('forsterite ionic model 0 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 forsterite_r_0),temps))
    colors.append('g')
    styles.append('-')

    names.append('forsterite ionic model 50 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 forsterite_r_50),temps))
    colors.append('g')
    styles.append('--')

    names.append('forsterite ionic model 100 GPa')
    betas.append(ionic_model_beta(ionic_model_force_constant(
                 forsterite_r_100),temps))
    colors.append('g')
    styles.append(':')

    castep_isotope_sub.plot_beta(temps, betas , names,
            colors=colors, styles=styles, filename='ionic_model_beta.pdf')

    # Plot alpha for Fo # NB: this is liq - Fo not Fo - liq.
    alpha_t_0gpa = ((ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(0, r_coefs_melt_fudge)),temps)) -
                    (ionic_model_beta(ionic_model_force_constant(
                         forsterite_r_0),temps)))
    alpha_t_50gpa = ((ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(50, r_coefs_melt_fudge)),temps)) -
                    (ionic_model_beta(ionic_model_force_constant(
                         forsterite_r_50),temps)))
    alpha_t_100gpa = ((ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(100, r_coefs_melt_fudge)),temps)) -
                    (ionic_model_beta(ionic_model_force_constant(
                         forsterite_r_100),temps)))

    dkalpha_t_0gpa = ((ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(0, r_coefs_melt_dekoker)),temps)) -
                    (ionic_model_beta(ionic_model_force_constant(
                         forsterite_r_0),temps)))
    dkalpha_t_50gpa = ((ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(50, r_coefs_melt_dekoker)),temps)) -
                    (ionic_model_beta(ionic_model_force_constant(
                         forsterite_r_50),temps)))
    dkalpha_t_100gpa = ((ionic_model_beta(ionic_model_force_constant(
                         melt_bond_length(100, r_coefs_melt_dekoker)),temps)) -
                    (ionic_model_beta(ionic_model_force_constant(
                         forsterite_r_100),temps)))

    import matplotlib.pyplot as plt
    fix, ax1 = plt.subplots()
    ax1.plot(temps, alpha_t_0gpa, 'k-', label='[fudge lq - fo] 0 GPa')
    ax1.plot(temps, alpha_t_50gpa, 'b-', label='[fudge lq - fo] 50 GPa')
    ax1.plot(temps, alpha_t_100gpa, 'g-', label='[fudge lq - fo] 100 GPa')
    ax1.plot(temps, dkalpha_t_0gpa, 'k:', label='[dk lq - fo] 0 GPa')
    ax1.plot(temps, dkalpha_t_50gpa, 'b:', label='[dk lq - fo] 50 GPa')
    ax1.plot(temps, dkalpha_t_100gpa, 'g:', label='[dk lq - fo] 100 GPa')
    ax1.set_ylabel(r"$\Delta^{}$Mg (per mill) relative to {}".format('{26}', "liquid"))
    ax1.set_xlabel("T (K)")
    plt.legend()
    plt.savefig("ionic_model_alpha.pdf")


from pathlib import Path
import pandas as pd
import ast

SPLIT = "train"

DATA_DIR = Path("data/SoccerNet")

INVALID_CSV_PATH = DATA_DIR / f"invalid_data_{SPLIT}.csv"
CAPTION_CSV_PATH = DATA_DIR / f"soccernet_{SPLIT}.csv"
METADETA_CSV_PATH = DATA_DIR / f"soccernet_{SPLIT}_game_subinfo.csv"

MERGED_INVALID_CSV_PATH = DATA_DIR / f"soccernet_{SPLIT}_merged.csv"
MERGED_CLEANED_CSV_PATH = DATA_DIR / f"soccernet_{SPLIT}_merged_cleaned.csv"

def check():
    df = pd.read_csv(MERGED_CLEANED_CSV_PATH, delimiter=",")
    img_list = ast.literal_eval(df.loc[0, "img_path"])
    print("%10d" % df.loc[0, "videoID"])
    print(img_list[0])

invalid_df = pd.read_csv(INVALID_CSV_PATH, delimiter=",", dtype=str)
caption_df = pd.read_csv(CAPTION_CSV_PATH, delimiter=",", dtype=str)
metadata_df = pd.read_csv(METADETA_CSV_PATH, delimiter=",", dtype=str)

def save_modified_invalid_csv(invalid_df):
    # Extract the portion after "raw_images/" as videoID
    invalid_df["videoID"] = invalid_df["dst_images_path"].str.extract(
        r"raw_images/(.*)"
    )

    # Reorder the columns to place videoID as the first column again
    invalid_df = invalid_df[
        ["videoID", "dst_images_path", "src_video_path", "spotTime", "message"]
    ]

    invalid_df.to_csv(INVALID_CSV_PATH, index=False)
    return invalid_df

# invalid_df = save_modified_invalid_csv(invalid_df)

# merged_train_df = pd.merge(
#     caption_df, metadata_df, how="left", on="caption"
# ).drop_duplicates(subset=["caption"])

def save_invalid_csv():
    merged_invalid_train_df = pd.merge(
        merged_train_df, invalid_df, how="left", on="videoID"
    ).drop_duplicates(subset=["videoID"])

    merged_invalid_train_df.to_csv(MERGED_INVALID_CSV_PATH, index=False)

# save_invalid_csv()

def save_cleaned_csv():
    # Get the list of invalid videoIDs
    invalid_video_ids = invalid_df["videoID"].tolist()

    # Filter out rows with invalid videoIDs from merged_train_df
    merged_cleaned_df = merged_train_df.loc[
        ~merged_train_df["videoID"].isin(invalid_video_ids)
    ]

    # Save the cleaned DataFrame to CSV
    merged_cleaned_df.to_csv(MERGED_CLEANED_CSV_PATH, index=False)

def fill_prompt():
    merged_cleaned_df = pd.read_csv(MERGED_CLEANED_CSV_PATH, delimiter=",", dtype=str)
    merged_cleaned_df[
        "prompt"
    ] = "This is soccer video frames. Make a caption with emotion and detail information."
    merged_cleaned_df.to_csv(MERGED_CLEANED_CSV_PATH, index=False)

fill_prompt()
# save_cleaned_csv()


import speech_recognition as sr

# Create a recognizer instance
r = sr.Recognizer()

with sr.Microphone() as source:
    print("Press Enter to start recording...")
    input()  # Wait for Enter key to be pressed
    print("Recording audio... Speak now!")
    audio_data = r.listen(source, timeout=10)  # Listen for audio input

    try:
        print("Recognizing...")
        # Convert speech to text using Google Web Speech API
        text = r.recognize_google(audio_data)
        print("Transcribed Text:", text)
    except sr.UnknownValueError:
        print("Could not understand audio.")
    except sr.RequestError as e:
        print("Error requesting results from Google Web Speech API; {0}".format(e))


from django.urls import path
from news.views import *

urlpatterns = [
    path('register/', register, name='register'),
    path('logint/', user_login, name='user_login'),
    path('logout/', user_logout, name='user_logout'),
    path('', HomeNews.as_view(), name='home'),
    path('category/<int:category_id>/', NewsByCategory.as_view(), name='category'),
    path('news/<int:pk>/', ViewNews.as_view(), name='view_news'),
    path('news/add_news/', CreateNews.as_view(), name='add_news'),
    path('api/news/', NewsListView.as_view()),
    path('api/news/<int:pk>/', NewsSingleView.as_view()),
]


import geopandas as gpd
import pandas as pd

from ..services.file_service import get_path

# Default Path to the data folder
_DATA_FOLDER = get_path("data", "hmc")

def load_site_data(
    num_sites,
    norm_fac=1e9,
    data_folder=_DATA_FOLDER,
):
    # Read data file
    file_path = data_folder / f"hmc_{num_sites}SitesModel.csv"
    df = pd.read_csv(file_path)

    # Extract information
    z_2017 = df[f"z_2017_{num_sites}Sites"].to_numpy()
    zbar_2017 = df[f"zbar_2017_{num_sites}Sites"].to_numpy()
    theta = df[f"theta_{num_sites}Sites"].to_numpy()
    gamma = df[f"gamma_{num_sites}Sites"].to_numpy()
    forest_area_2017 = df[f"forestArea_2017_ha_{num_sites}Sites"].to_numpy()

    # Normalize Z data
    zbar_2017 /= norm_fac
    z_2017 /= norm_fac
    forest_area_2017 /= norm_fac

    # Read municipal level data
    municipal_theta_df = gpd.read_file(data_folder / "muni_data_theta.geojson")
    municipal_gamma_df = gpd.read_file(data_folder / "muni_data_gamma.geojson")

    # Read site level data
    site_theta_2017 = gpd.read_file(
        data_folder / f"site_{num_sites}_data_theta.geojson"
    )
    site_gamma_2017 = gpd.read_file(
        data_folder / f"site_{num_sites}_data_gamma.geojson"
    )

    # Remove geometries
    site_theta_2017_df = site_theta_2017.iloc[:, :-1]
    site_gamma_2017_df = site_gamma_2017.iloc[:, :-1]

    print(f"Data successfully loaded from {data_folder}")
    return (
        zbar_2017,
        gamma,
        z_2017,
        forest_area_2017,
        theta,
        site_theta_2017_df,
        site_gamma_2017_df,
        municipal_theta_df,
        municipal_gamma_df,
    )


from openfisca_core.model_api import *
from openfisca_core import periods

def str_to_instant(s):
    return periods.Instant(tuple(map(lambda s: int(s), s.split("-"))))

def backdate_parameters(
    root: str = None, first_instant: str = "2010-01-01"
) -> Reform:
    first_instant = str_to_instant(first_instant)

    def modifier(parameters):
        node = parameters
        if root is not None:
            for name in root.split("."):
                node = node[name]
        for param in node.get_descendants():
            if hasattr(param, "values_list"):
                earliest = param.values_list[-1]
                earliest_value = earliest.value
                earliest_instant = str_to_instant(earliest.instant_str)
                if first_instant < earliest_instant:
                    days = (earliest_instant.date - first_instant.date).days
                    param.update(
                        period=periods.Period(("day", first_instant, days)),
                        value=earliest_value,
                    )
        return parameters

    return modifier


#!/usr/bin/env python3
# encoding: utf-8
try:
    from pytorch_grad_cam import (EigenCAM, EigenGradCAM, GradCAM,
                                  GradCAMPlusPlus, LayerCAM, XGradCAM)
    from pytorch_grad_cam.activations_and_gradients import \
        ActivationsAndGradients
    from pytorch_grad_cam.utils.image import show_cam_on_image
    from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
except ImportError:
    raise ImportError('Please run `pip install "grad-cam>=1.3.6"` to install '
                      '3rd party package pytorch_grad_cam.')
import torch
import numpy as np
from PIL import Image
from utils.visualize import tensor2im
import os
import cv2
import argparse
import numpy as np

class SegmentationModelOutputWrapper(torch.nn.Module):
    def __init__(self, model):
        super(SegmentationModelOutputWrapper, self).__init__()
        self.model = model

    def forward(self, x):
        return self.model(x) # [0]

class SemanticSegmentationTarget:
    """ Gets a binary spatial mask and a category,
        And return the sum of the category scores,
        of the pixels in the mask. """

    def __init__(self, category, mask):
        self.category = category
        self.mask = torch.from_numpy(mask)
        if torch.cuda.is_available():
            self.mask = self.mask.cuda()

    def __call__(self, model_output):
        return (model_output[self.category, :, :] * self.mask).sum()

def gen_cam(model, input_tensor, pred_tensor,label=None,margin=None):
    ## 定义模型
    cam_model = SegmentationModelOutputWrapper(model)
    numpy_img = tensor2im(input_tensor[0],cam=True) # [512, 512，3] numpy shapew为float32
    ignore_label = 255 # voc
    if label is not None:
        # label=label.cpu().numpy()
        #ignore_mask = (label.not_equal(ignore_label))# ignore 的 pixel值=0
        #class_mask = (label * ignore_mask)
        #ignore_mask = label!=ignore_label
        #
        class_mask = label
        class_category, count = np.unique(class_mask, return_counts=True)
        class_category = class_category[(0<class_category)&(class_category<255)]
        print("---------------", class_category)
        targets = []
        both_images = []
        if np.size(class_category)>1:
            print(class_category)
        for class_category in class_category:# 多个类别时

            class_mask_uint8 = 255 * np.uint8( class_mask == class_category)
            class_mask_float = np.float32( class_mask == class_category) # 只取对应类别mask做损失计算，不需要忽略255
            rgb_img = numpy_img[margin[0]:(numpy_img.shape[0] - margin[1]),
                      margin[2]:(numpy_img.shape[1] - margin[3]), :]       # 裁剪
            rgb_img = rgb_img *255 # 乘以 255 必须在uint8之前，不然先用uint8会使得小数都变为1
            rgb_img = Image.fromarray(rgb_img.astype(np.uint8),"RGB") # 转化为图像 二维
            class_mask_uint8 = class_mask_uint8[ margin[0]:(class_mask_uint8.shape[0] - margin[1]),
                    margin[2]:(class_mask_uint8.shape[1] - margin[3])]
            both_images.append( np.hstack(( rgb_img, np.repeat(class_mask_uint8[:, :, None], 3, axis=-1))) )  # 数据类型必须为uint8
            targets.append( SemanticSegmentationTarget(class_category, class_mask_float))
            #Image.fromarray(both_images).save( save_path + '/' + 'both' + str(0) + '.png')# 数据类型必须为uint8 PIL才能转换

    else:
        # pred_tensor =
        # 转到cpu上计算输出

        normalized_masks = torch.softmax( pred_tensor, dim=0).cpu() # [21, 512, 512]

        sem_classes = [
            '__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
            'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
            'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'
        ]
        # dataloader.get_class_names
        sem_class_to_idx = {cls: idx for (idx, cls) in enumerate(sem_classes)}

        class_category = [ sem_class_to_idx["dog"] ]
        class_mask = normalized_masks.argmax(axis=0).detach().cpu().numpy() #[512,512]
        #class_category, count = np.unique(class_mask, return_counts=True)
        class_mask_uint8 = 255 * np.uint8( class_mask == class_category)
        class_mask_float = np.float32( class_mask == class_category)

        rgb_img = numpy_img[ margin[0]:(rgb_img.shape[0] - margin[1]),
                    margin[2]:(rgb_img.shape[1] - margin[3])]
        class_mask_uint8 = class_mask_uint8[ margin[0]:(class_mask_uint8.shape[0] - margin[1]),
                    margin[2]:(class_mask_uint8.shape[1] - margin[3])]
        both_images = [np.hstack(( rgb_img, np.repeat(class_mask_uint8[:, :, None], 3, axis=-1)))]
        targets = [SemanticSegmentationTarget(class_category, class_mask_float)]

    target_layers = [cam_model.model.branch1.head.last_conv[-2]]
    #targets = None
    '''
    if args.target_category:
        grad_cam_v = pkg_resources.get_distribution('grad_cam').version
        if digit_version(grad_cam_v) >= digit_version('1.3.7'):
            from pytorch_grad_cam.utils.model_targets import \
                ClassifierOutputTarget
            targets = [ClassifierOutputTarget(c) for c in args.target_category]
        else:
            targets = args.target_category
    '''

    with GradCAM(model=cam_model, # GradCAMPlusPlus
                target_layers=target_layers,
                use_cuda=torch.cuda.is_available()) as cam:
        grayscale_cam = cam(input_tensor= input_tensor,
                        targets=targets)[0, :] #[512,512] 在model eval时会生成两个输出 因此在base_cam.py处修改
        #rgb_img = tensor2im(input_tensor[0], cam=True) # 重新生成未被裁剪的 0-1取值范围的图
        cam_image = show_cam_on_image(numpy_img, grayscale_cam, use_rgb=True, image_weight=0.7) #[512,512,3]
        cam_image = cam_image[ margin[0]:(cam_image.shape[0] - margin[1]),
                    margin[2]:(cam_image.shape[1] - margin[3]),:] # crop
        #Image.fromarray(cam_image ).save( save_path+ '/' + 'cam' + str(0) + '.png')
    return both_images, cam_image


from collections import defaultdict

def solution(n, paths, gates, summits):
    answer = [-1, float('inf')]
    graph = defaultdict(list)

    for i, j, w in paths:
        graph[i].append([j, w])
        graph[j].append([i, w])

    def dfs(current, intensity, visited):
        if visited[current] == 1:
            return
        elif visited[current] == -1:  # 입구
            return
        elif visited[current] == -2:  # 도착
            if answer[1] > intensity:
                answer[0] = current
                answer[1] = intensity
            elif answer[1] == intensity:
                answer[0] = min(current, answer[0])
        else:
            visited[current] = 1  # 방문
            for target, cost in graph[current]:
                if cost > answer[1]:
                    continue
                if cost > intensity:
                    dfs(target, cost, visited)
                else:
                    dfs(target, intensity, visited)

    visited = [0 for _ in range(n + 1)]
    for g in gates:
        visited[g] = -1
    for summit in summits:
        visited[summit] = -2
    for gate in gates:
        for path, w in graph[gate]:
            dfs(path, w, visited[:])
    return answer


import argparse
import os, sys
import re
import atexit
from subprocess import Popen
from shutil import copyfile
from base64 import b64decode
from time import strftime

import requests

import Utils

atexit.register(input, "Press enter to exit.")

# 1 or more digits followed by m or g, then optional b
max_heap_re = re.compile(r"^\d+[mMgG][bB]?$")

def prompt_yes_no(prompt):
    yes_inputs = {'yes', 'ye', 'y'}
    no_inputs = {'no', 'n'}
    while True:
        choice = input(prompt + " [y/n] ").lower()
        if choice in yes_inputs:
            return True
        elif choice in no_inputs:
            return False
        else:
            print('Please respond with "y" or "n".')

# Find Forge jar file; raise error if not found
def find_forge_jar(forge_dir):
    for entry in os.scandir(forge_dir):
        if ".jar" in entry.name and "forge" in entry.name:
            print(f"Found forge .jar: {entry.name}")
            return entry.name
    raise FileNotFoundError(f"Could not find forge .jar in {forge_dir}.")

# Create mods folder if needed; find AP randomizer jar; return None if not found.
def find_ap_randomizer_jar(forge_dir):
    mods_dir = os.path.join(forge_dir, 'mods')
    if os.path.isdir(mods_dir):
        ap_mod_re = re.compile(r"^aprandomizer-[\d\.]+\.jar$")
        for entry in os.scandir(mods_dir):
            match = ap_mod_re.match(entry.name)
            if match:
                print(f"Found AP randomizer mod: {match.group()}")
                return match.group()
        return None
    else:
        os.mkdir(mods_dir)
        print(f"Created mods folder in {forge_dir}")
        return None

# Create APData folder if needed; clean .apmc files from APData; copy given .apmc into directory.
def replace_apmc_files(forge_dir, apmc_file):
    if apmc_file is None:
        return
    apdata_dir = os.path.join(forge_dir, 'APData')
    copy_apmc = True
    if not os.path.isdir(apdata_dir):
        os.mkdir(apdata_dir)
        print(f"Created APData folder in {forge_dir}")
    for entry in os.scandir(apdata_dir):
        if entry.name.endswith(".apmc") and entry.is_file():
            if not os.path.samefile(apmc_file, entry.path):
                os.remove(entry.path)
                print(f"Removed {entry.name} in {apdata_dir}")
            else: # apmc already in apdata
                copy_apmc = False
    if copy_apmc:
        copyfile(apmc_file, os.path.join(apdata_dir, os.path.basename(apmc_file)))
        print(f"Copied {os.path.basename(apmc_file)} to {apdata_dir}")

# Check mod version, download new mod from GitHub releases page if needed.
def update_mod(forge_dir):
    ap_randomizer = find_ap_randomizer_jar(forge_dir)

    client_releases_endpoint = "https://api.github.com/repos/KonoTyran/Minecraft_AP_Randomizer/releases"
    resp = requests.get(client_releases_endpoint)
    if resp.status_code == 200:  # OK
        latest_release = resp.json()[0]
        if ap_randomizer != latest_release['assets'][0]['name']:
            print(f"A new release of the Minecraft AP randomizer mod was found: {latest_release['assets'][0]['name']}")
            if ap_randomizer is not None:
                print(f"Your current mod is {ap_randomizer}.")
            else:
                print(f"You do not have the AP randomizer mod installed.")
            if prompt_yes_no("Would you like to update?"):
                old_ap_mod = os.path.join(forge_dir, 'mods', ap_randomizer) if ap_randomizer is not None else None
                new_ap_mod = os.path.join(forge_dir, 'mods', latest_release['assets'][0]['name'])
                print("Downloading AP randomizer mod. This may take a moment...")
                apmod_resp = requests.get(latest_release['assets'][0]['browser_download_url'])
                if apmod_resp.status_code == 200:
                    with open(new_ap_mod, 'wb') as f:
                        f.write(apmod_resp.content)
                        print(f"Wrote new mod file to {new_ap_mod}")
                    if old_ap_mod is not None:
                        os.remove(old_ap_mod)
                        print(f"Removed old mod file from {old_ap_mod}")
                else:
                    print(f"Error retrieving the randomizer mod (status code {apmod_resp.status_code}).")
                    print(f"Please report this issue on the Archipelago Discord server.")
                    sys.exit(1)
    else:
        print(f"Error checking for randomizer mod updates (status code {resp.status_code}).")
        print(f"If this was not expected, please report this issue on the Archipelago Discord server.")
        if not prompt_yes_no("Continue anyways?"):
            sys.exit(0)

# Check if the EULA is agreed to, and prompt the user to read and agree if necessary.
def check_eula(forge_dir):
    eula_path = os.path.join(forge_dir, "eula.txt")
    if not os.path.isfile(eula_path):
        # Create eula.txt
        with open(eula_path, 'w') as f:
            f.write("#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).\n")
            f.write(f"#{strftime('%a %b %d %X %Z %Y')}\n")
            f.write("eula=false\n")
    with open(eula_path, 'r+') as f:
        text = f.read()
        if 'false' in text:
            # Prompt user to agree to the EULA
            print("You need to agree to the Minecraft EULA in order to run the server.")
            print("The EULA can be found at https://account.mojang.com/documents/minecraft_eula")
            if prompt_yes_no("Do you agree to the EULA?"):
                f.seek(0)
                f.write(text.replace('false', 'true'))
                f.truncate()
                print(f"Set {eula_path} to true")
            else:
                sys.exit(0)

# Run the Forge server. Return process object
def run_forge_server(forge_dir, heap_arg):
    forge_server = find_forge_jar(forge_dir)

    java_exe = os.path.abspath(os.path.join('jre8', 'bin', 'java.exe'))
    if not os.path.isfile(java_exe):
        java_exe = "java"  # try to fall back on java in the PATH

    heap_arg = max_heap_re.match(max_heap).group()
    if heap_arg[-1] in ['b', 'B']:
        heap_arg = heap_arg[:-1]
    heap_arg = "-Xmx" + heap_arg

    argstring = ' '.join([java_exe, heap_arg, "-jar", forge_server, "-nogui"])
    print(f"Running Forge server: {argstring}")
    os.chdir(forge_dir)
    return Popen(argstring)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("apmc_file", default=None, nargs='?', help="Path to an Archipelago Minecraft data file (.apmc)")

    args = parser.parse_args()
    apmc_file = os.path.abspath(args.apmc_file) if args.apmc_file else None

    # Change to executable's working directory
    os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))

    options = Utils.get_options()
    forge_dir = options["minecraft_options"]["forge_directory"]
    max_heap = options["minecraft_options"]["max_heap_size"]

    if apmc_file is not None and not os.path.isfile(apmc_file):
        raise FileNotFoundError(f"Path {apmc_file} does not exist or could not be accessed.")
    if not os.path.isdir(forge_dir):
        raise NotADirectoryError(f"Path {forge_dir} does not exist or could not be accessed.")
    if not max_heap_re.match(max_heap):
        raise Exception(f"Max heap size {max_heap} in incorrect format. Use a number followed by M or G, e.g. 512M or 2G.")

    update_mod(forge_dir)
    replace_apmc_files(forge_dir, apmc_file)
    check_eula(forge_dir)
    server_process = run_forge_server(forge_dir, max_heap)
    server_process.wait()


import requests
from django.core.management.base import BaseCommand
from django.conf import settings
from nutella_fans.product.models import Category, Product, Brand, Store
from datetime import datetime
from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())

class Command(BaseCommand):

    """
    standalone scripts to call the api and import data to database
    """

    def handle(self, *args, **options):
        """ BaseCommand requires the implementation of handle method
        Called all functions --> call and request of api
                             --> insert into data to database

        Args:
            *args: Description
            **options: Description
        """
        now = datetime.now()
        date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
        self.stdout.write(self.style.WARNING(
            "Début de la mise à jour des données %s ..." % date_time))

        category_list = self.get_category()
        for category_dict in category_list:
            products = self.get_products(category_dict)
            for product in products:
                p = self.create_product(product)
                if not p:
                    continue
                else:
                    category_names = product.get(
                        "categories").lower().split(",")
                    for category in category_names:
                        c, created = Category.objects.get_or_create(
                            name=category)
                        p.categories.add(c)
                    if product.get("stores"):
                        stores = product.get("stores").lower().split(",")
                        for store in stores:
                            s, created = Store.objects.get_or_create(
                                name=store)
                            p.stores.add(s)
        self.stdout.write(self.style.SUCCESS("Mise à jour réussie"))

        self.stdout.write(self.style.WARNING(
            "Fin de la mise à jour des données %s " % date_time))

    def get_category(self):
        """response of the request to extract data from API

        Returns:
            LIST: list of categories
        """
        self.stdout.write(self.style.WARNING(
            "Appel à l'api et récupération des 10 meilleurs catégories"))
        response = requests.get("https://fr.openfoodfacts.org/categories.json")
        result_category = response.json()
        data_category = result_category.get('tags')
        categories = [data.get('name') for data in data_category
                      if data.get("name")]
        category_name = categories[0:10]
        return category_name

    def get_products(self, category):
        """response of the request to extract data from API
            with queries parameters response : product list according to
            the best categories

        Parameters:
            category (STRING): one category

        Returns:
            LIST: list of products
        """
        self.stdout.write(self.style.WARNING(
            "Récupération des données openfoodfacts --> produits"))
        query = {
            "action": "process",
            "tagtype_0": "categories",
            "tag_contains_0": "contains",
            "tag_0": category,
            "sort_by": "unique_scans_n",
            "page_size": settings.MAX_IMPORT_PRODUCTS,
            "json": 1}
        response_product = requests.get(
            "https://fr.openfoodfacts.org/cgi/search.pl?", query)
        result_product = response_product.json()
        return result_product["products"]

    def create_product(self, product):
        """Creation of product on product_product table
            Get product_name or barcode from api for example and
            insert into on Model Product and others Model thanks to relationship

        Args:
            product (INT): one product

        Returns:
            Dict: one product information request to insert on database
        """
        if (product.get('product_name') and product.get('nutrition_grades')):
            name = product.get("product_name")
            nutriscore = product.get("nutrition_grades")
            nova = product.get("nova_group")
            url = product.get("url")
            description = product.get("ingredients_text")
            barcode = product.get("code")
            picture = product.get("image_front_url")
            fat_100g = product.get("nutriments", {}).get("fat_100g")
            fat_level = product.get("nutrient_levels", {}).get("fat")
            salt_100g = product.get("nutriments", {}).get("salt_100g")
            salt_level = product.get("nutrient_levels", {}).get("salt")
            saturated_fat_100g = product.get(
                "nutriments", {}).get("saturated-fat_100g")
            saturated_fat_level = product.get(
                "nutrient_levels", {}).get("saturated-fat")
            sugars_100g = product.get("nutriments", {}).get("sugars_100g")
            sugars_level = product.get("nutrient_levels", {}).get("sugars")
            brands = product.get("brands").lower().split(",")

            b, created = Brand.objects.get_or_create(
                name=brands[0])
            p, created = Product.objects.get_or_create(barcode=barcode, defaults={
                'name': name, 'nutriscore': nutriscore, 'nova': nova, 'url': url, 'description': description, 'picture': picture, 'fat_100g': fat_100g, 'fat_level': fat_level,
                'salt_100g': salt_100g, 'salt_level': salt_level, 'saturated_fat_100g': saturated_fat_100g,
                'saturated_fat_level': saturated_fat_level, 'sugars_100g': sugars_100g, 'sugars_level': sugars_level,
                'brand': b})

            return p