import setuptools

import versioneer

DESCRIPTION_FILES = ["pypi-intro.rst"]

long_description = []
import codecs
for filename in DESCRIPTION_FILES:
    with codecs.open(filename, 'r', 'utf-8') as f:
        long_description.append(f.read())
long_description = "\n".join(long_description)

setuptools.setup(
    name = "root-solver",
    version = versioneer.get_version(),
    packages = setuptools.find_packages('src'),
    package_dir = {'': 'src'},
    install_requires = [
        "numpy",
    ],
    python_requires = '>=3.4',
    author = "James Tocknell",
    author_email = "aragilar@gmail.com",
    description = "Root solver for polynomial equations",
    long_description = long_description,
    license = "3-clause BSD",
    keywords = "root solver",
    url = "https://root-solver.readthedocs.io",
    project_urls={
        'Documentation': 'https://root-solver.readthedocs.io',
        'Source': 'https://github.com/aragilar/root-solver/',
        'Tracker': 'https://github.com/aragilar/root-solver/issues',
    },
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: BSD License',
        'Topic :: Scientific/Engineering',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3 :: Only',
    ],
    cmdclass=versioneer.get_cmdclass(),
)


# -*- coding: utf-8 -*-
# @Time   : 19-8-5 下午5:31
# @Author : huziying
# @File   : half_circle.py

# 半圆形

import cv2
import numpy
import uuid
import base64
import os

from django.db import connection

from .utils_huziying import horizontal_measurements, vertical_measurements

numpy.set_printoptions(threshold=numpy.inf)

def half_circle_image_process(img):
    """图片处理过程"""
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blurred = cv2.GaussianBlur(img_gray, (15, 15), 0)
    img_thresh = cv2.threshold(img_blurred, 120, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    edges = cv2.Canny(img_thresh, 200, 400, 3)
    indices = numpy.where(edges != [0])
    coordinates = numpy.array(list(zip(indices[1], indices[0])))
    reference_coordinate = coordinates[coordinates.argmin(axis=0)[0]]

    return coordinates, reference_coordinate

def main(image=None):
    """
    (x, y)
    ————————→ x
    |
    |
    |
    |
    ↓
    y

    坐标测量顺序: 从左向右，从上向下
    """
    img_name = uuid.uuid1()
    if not image:
        img = cv2.imread('measurement/template/half_circle_1.jpg')
    else:
        receive = base64.b64decode(image)
        with open('measurement/images/{}.jpg'.format(img_name), 'wb') as f:
            f.write(receive)
        img = cv2.imread('measurement/images/{}.jpg'.format(img_name))

    coordinates, reference_coordinate = half_circle_image_process(img)

    height, width, dimension = img.shape
    measurements_data, data = list(), dict()

    cursor = connection.cursor()
    cursor.execute("select top_left, bottom_right, name from templates where shape = '1' and direction = '0' "
                   "order by name;")
    vertical = cursor.fetchall()
    for v in vertical:
        top_left = (int(v[0][0] * width + reference_coordinate[0]), int(v[0][1] * height + reference_coordinate[1]))
        bottom_right = (int(v[1][0] * width + reference_coordinate[0]), int(v[1][1] * height + reference_coordinate[1]))
        name = v[2]
        # print('top_left', top_left, 'bottom_right', bottom_right, 'name', name)
        cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), thickness=1)
        coordinates_limit = coordinates[numpy.where(
            (coordinates[:, 0] >= top_left[0]) & (coordinates[:, 0] <= bottom_right[0]) &
            (coordinates[:, 1] >= top_left[1]) & (coordinates[:, 1] <= bottom_right[1]))]
        coordinates_limit_sort = coordinates_limit[coordinates_limit[:, 1].argsort(), :]
        # for c in coordinates_limit_sort:
        #     cv2.circle(img, tuple(c), 1, (0, 0, 255), 1)

        measurement = vertical_measurements(coordinates_limit_sort, img, name)
        measurements_data.append(measurement)

    cursor.execute("select top_left, bottom_right, name from templates where shape = '1' and direction = '1' "
                   "order by name;")
    horizontal = cursor.fetchall()
    for h in horizontal:
        top_left = (int(h[0][0] * width + reference_coordinate[0]), int(h[0][1] * height + reference_coordinate[1]))
        bottom_right = (int(h[1][0] * width + reference_coordinate[0]), int(h[1][1] * height + reference_coordinate[1]))
        name = h[2]
        # print('top_left', top_left, 'bottom_right', bottom_right)

        cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), thickness=1)
        coordinates_limit = coordinates[numpy.where(
            (coordinates[:, 0] >= top_left[0]) & (coordinates[:, 0] <= bottom_right[0]) &
            (coordinates[:, 1] >= top_left[1]) & (coordinates[:, 1] <= bottom_right[1]))]
        coordinates_limit_sort = coordinates_limit[coordinates_limit[:, 0].argsort(), :]

        measurement = horizontal_measurements(coordinates_limit_sort, img, name)
        measurements_data.append(measurement)

    # print('measurements_data', measurements_data)
    data['measurements_data'] = measurements_data

    result_name = uuid.uuid1()
    cv2.imwrite('measurement/images/{}.jpg'.format(result_name), img)
    with open('measurement/images/{}.jpg'.format(result_name), 'rb') as f:
        base64_img = base64.b64encode(f.read())
    data.update({'image': base64_img})

    if os.path.exists('measurement/images/{}.jpg'.format(img_name)):
        os.remove('measurement/images/{}.jpg'.format(img_name))
    if os.path.exists('measurement/images/{}.jpg'.format(result_name)):
        os.remove('measurement/images/{}.jpg'.format(result_name))

    # cv2.namedWindow('img', cv2.WINDOW_NORMAL)
    # cv2.imshow("img", img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    return data

if __name__ == '__main__':
    main()


from bs4 import BeautifulSoup
import requests
from datetime import datetime

# https://amzn.to/3E7V45Y -> Cetus Pro FPV

def cetus_price():
  url = 'https://amzn.to/3E7V45Y'
  headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

  response = requests.get(url, headers=headers)
  soup = BeautifulSoup(response.content, 'html.parser')

  price = soup.find('span', attrs={'class': 'a-offscreen'}).text

  price_text = "The current price of Cetus Pro FPV is: " + str(price)

  return price_text


"""
Laboratorio de Programación Básica en Python para Manejo de Datos
-----------------------------------------------------------------------------------------

Este archivo contiene las preguntas que se van a realizar en el laboratorio.

No puede utilizar pandas, numpy o scipy. Se debe utilizar solo las funciones de python
básicas.

Utilice el archivo `data.csv` para resolver las preguntas.

"""
import csv
from io import open

file = open('data.csv', 'r')
data = file.readlines()
file.close()

print(data[0])

def pregunta_01():
    """
    Retorne la suma de la segunda columna.
    Rta/
    214
    """

    with open('data.csv', mode='r') as datos:
        datos = datos.readlines()
    return sum(int(var.strip().split('\t')[1]) for var in datos)

def pregunta_02():
    """
    Retorne la cantidad de registros por cada letra de la primera columna como la lista
    de tuplas (letra, cantidad), ordendas alfabéticamente.
    Rta/
    [
        ("A", 8),
        ("B", 7),
        ("C", 5),
        ("D", 6),
        ("E", 14),
    ]
    """
    with open('data.csv', mode='r') as datos:
        datos = datos.readlines()

    datos = [var.strip().split('\t')[0] for var in datos]
    col1 = sorted(dict.fromkeys(datos))
    respuesta = []
    [respuesta.append((x, datos.count(x))) for x in col1]

    return respuesta

def pregunta_03():
    """
    Retorne la suma de la columna 2 por cada letra de la primera columna como una lista
    de tuplas (letra, suma) ordendas alfabeticamente.
    Rta/
    [
        ("A", 53),
        ("B", 36),
        ("C", 27),
        ("D", 31),
        ("E", 67),
    ]
    """
    with open('data.csv', mode='r') as datos:
        datos = datos.readlines()

    datos = [var.strip().split('\t')[:2] for var in datos]
    col1 = sorted(list({var[0] for var in datos}))
    respuesta = []

    for letra in col1:
        sum = 0
        for camp in datos:
            if camp[0] == letra:
                sum += int(camp[1])
        respuesta.append((letra, sum))

    return respuesta

def pregunta_04():
    """
    La columna 3 contiene una fecha en formato `YYYY-MM-DD`. Retorne la cantidad de
    registros por cada mes, tal como se muestra a continuación.
    Rta/
    [
        ("01", 3),
        ("02", 4),
        ("03", 2),
        ("04", 4),
        ("05", 3),
        ("06", 3),
        ("07", 5),
        ("08", 6),
        ("09", 3),
        ("10", 2),
        ("11", 2),
        ("12", 3),
    ]
    """
    with open('data.csv', mode='r') as datos:
        datos = datos.readlines()

    datos = [var.strip().split('\t')[2][5:7] for var in datos]
    col1 = sorted(dict.fromkeys(datos))
    respuesta = []
    [respuesta.append((x, datos.count(x))) for x in col1]

    return respuesta

def pregunta_05():
    """
    Retorne una lista de tuplas con el valor maximo y minimo de la columna 2 por cada
    letra de la columa 1.
    Rta/
    [
        ("A", 9, 2),
        ("B", 9, 1),
        ("C", 9, 0),
        ("D", 8, 3),
        ("E", 9, 1),
    ]
    """
    letras = []
    colMax = []
    colMin = []
    maximos = []
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            if(not row[0] in letras):
                letras.append(row[0])
                colMax.append(int(row[1]))
                colMin.append(int(row[1]))
            else:
                if(colMax[letras.index(row[0])] < int(row[1])):
                    colMax[letras.index(row[0])] = int(row[1])
                if(colMin[letras.index(row[0])] > int(row[1])):
                    colMin[letras.index(row[0])] = int(row[1])
    for letra in letras:
        maximos.append(
            (letra, colMax[letras.index(letra)], colMin[letras.index(letra)]))
    maximos.sort(reverse=False)
    return maximos

def pregunta_06():
    """
    La columna 5 codifica un diccionario donde cada cadena de tres letras corresponde a
    una clave y el valor despues del caracter `:` corresponde al valor asociado a la
    clave. Por cada clave, obtenga el valor asociado mas pequeño y el valor asociado mas
    grande computados sobre todo el archivo.
    Rta/
    [
        ("aaa", 1, 9),
        ("bbb", 1, 9),
        ("ccc", 1, 10),
        ("ddd", 0, 9),
        ("eee", 1, 7),
        ("fff", 0, 9),
        ("ggg", 3, 10),
        ("hhh", 0, 9),
        ("iii", 0, 9),
        ("jjj", 5, 17),
    ]
    """
    letras=[]
    colMax = []
    colMin = []
    dic = []
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            for cod in row[4].split(','):
                letra = cod.split(':')[0]
                codigo = cod.split(':')[1]
                if(not letra in letras):
                    letras.append(letra)
                    colMax.append(int(codigo))
                    colMin.append(int(codigo))
                else:
                    if(colMax[letras.index(letra)]<int(codigo)):
                        colMax[letras.index(letra)]=int(codigo)
                    if(colMin[letras.index(letra)]>int(codigo)):
                        colMin[letras.index(letra)]=int(codigo)
    for letra in letras:
        dic.append((letra,colMin[letras.index(letra)],colMax[letras.index(letra)]))
    dic.sort(reverse=False)
    return dic

def pregunta_07():
    """
    Retorne una lista de tuplas que asocien las columnas 0 y 1. Cada tupla contiene un
    valor posible de la columna 2 y una lista con todas las letras asociadas (columna 1)
    a dicho valor de la columna 2.
    Rta/
    [
        (0, ["C"]),
        (1, ["E", "B", "E"]),
        (2, ["A", "E"]),
        (3, ["A", "B", "D", "E", "E", "D"]),
        (4, ["E", "B"]),
        (5, ["B", "C", "D", "D", "E", "E", "E"]),
        (6, ["C", "E", "A", "B"]),
        (7, ["A", "C", "E", "D"]),
        (8, ["E", "D", "E", "A", "B"]),
        (9, ["A", "B", "E", "A", "A", "C"]),
    ]
    """
    numeros = []
    letras = []
    reg = []
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            if(not int(row[1]) in numeros):
                numeros.append(int(row[1]))
                letras.append([row[0]])
            else:
                letras[numeros.index(int(row[1]))].append(row[0])
    for numero in numeros:
        reg.append((numero, letras[numeros.index(numero)]))
    reg.sort(reverse=False)
    return reg

def pregunta_08():
    """
    Genere una lista de tuplas, donde el primer elemento de cada tupla contiene  el valor
    de la segunda columna; la segunda parte de la tupla es una lista con las letras
    (ordenadas y sin repetir letra) de la primera  columna que aparecen asociadas a dicho
    valor de la segunda columna.
    Rta/
    [
        (0, ["C"]),
        (1, ["B", "E"]),
        (2, ["A", "E"]),
        (3, ["A", "B", "D", "E"]),
        (4, ["B", "E"]),
        (5, ["B", "C", "D", "E"]),
        (6, ["A", "B", "C", "E"]),
        (7, ["A", "C", "D", "E"]),
        (8, ["A", "B", "D", "E"]),
        (9, ["A", "B", "C", "E"]),
    ]
    """
    numeros = []
    letras = []
    reg = []
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            if(not int(row[1]) in numeros):
                numeros.append(int(row[1]))
                letras.append([row[0]])
            else:
                if(not row[0] in letras[numeros.index(int(row[1]))]):
                    letras[numeros.index(int(row[1]))].append(row[0])

    for numero in numeros:
        order = letras[numeros.index(numero)]
        order.sort()
        reg.append((numero, order))
    reg.sort(reverse=False)
    return reg

def pregunta_09():
    """
    Retorne un diccionario que contenga la cantidad de registros en que aparece cada
    clave de la columna 5.
    Rta/
    {
        "aaa": 13,
        "bbb": 16,
        "ccc": 23,
        "ddd": 23,
        "eee": 15,
        "fff": 20,
        "ggg": 13,
        "hhh": 16,
        "iii": 18,
        "jjj": 18,
    }
    """
    columna = []
    letras = []
    dictionario = {}
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            for cod in row[4].split(','):
                letra = cod.split(':')[0]
                columna.append(letra)
                if(not letra in letras):
                    letras.append(letra)
    letras.sort()
    for letra in letras:
        dictionario.update({letra: columna.count(letra)})
    return dictionario

def pregunta_10():
    """
    Retorne una lista de tuplas contengan por cada tupla, la letra de la columna 1 y la
    cantidad de elementos de las columnas 4 y 5.
    Rta/
    [
        ("E", 3, 5),
        ("A", 3, 4),
        ("B", 4, 4),
        ...
        ("C", 4, 3),
        ("E", 2, 3),
        ("E", 3, 3),
    ]
    """
    letras = []
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            col4 = row[3].split(",")
            col4 = len(col4)
            col5 = row[4].split(",")
            col5 = len(col5)
            letras.append((row[0], col4, col5))
    return letras

def pregunta_11():
    """
    Retorne un diccionario que contengan la suma de la columna 2 para cada letra de la
    columna 4, ordenadas alfabeticamente.
    Rta/
    {
        "a": 122,
        "b": 49,
        "c": 91,
        "d": 73,
        "e": 86,
        "f": 134,
        "g": 35,
    }
    """
    letras = {}
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            for letra in row[3].split(","):
                if(not letra in letras.keys()):
                    letras.update({letra: int(row[1])})
                else:
                    letras[letra] += int(row[1])
    dicc = sorted(letras.items())
    return dict(dicc)

def pregunta_12():
    """
    Genere un diccionario que contengan como clave la columna 1 y como valor la suma de
    los valores de la columna 5 sobre todo el archivo.
    Rta/
    {
        'A': 177,
        'B': 187,
        'C': 114,
        'D': 136,
        'E': 324
    }
    """
    letras = {}
    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='	')
        for row in csv_reader:
            letra = row[0]
            for codigo in row[4].split(","):
                numero = int(codigo.split(":")[1])
                if(not letra in letras.keys()):
                    letras.update({letra: numero})
                else:
                    letras[letra] += numero
    dicc = sorted(letras.items())
    return dict(dicc)

print(pregunta_12())


import asyncio
import logging
import threading
from typing import List

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.websockets import WebSocket, WebSocketDisconnect

from .saver import Saver
from .transport import Transfer, InputData

logging.basicConfig(level=logging.INFO)

app = FastAPI()

transfer = Transfer()
saver = Saver()

class WebSocketSender:
    clients: [WebSocket] = []
    messages: [InputData] = []
    is_on_thread = False

    async def send(self, data):
        for client in self.clients.copy():
            try:
                await client.send_json({"data": data})
            except (WebSocketDisconnect, RuntimeError):
                try:
                    self.clients.remove(client)
                except ValueError:
                    pass

    async def thread_async(self):
        if self.is_on_thread:
            return
        else:
            self.is_on_thread = True

        while True:
            if self.messages:
                messages = [self.messages.pop() for _ in range(len(self.messages))]
                await self.send(messages)
                await asyncio.sleep(0.5)

class Messages:
    messages: List[InputData] = []

web_socket_sender = WebSocketSender()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.on_event("startup")
async def startup():
    transfer.callbacks.append(saver.save)
    transfer.callbacks.append(web_socket_sender.messages.append)
    transfer.callbacks.append(Messages.messages.append)
    threading.Thread(target=transfer.thread, daemon=True).start()

@app.on_event("shutdown")
def shutdown():
    transfer.is_on = False
    logging.basicConfig(level=logging.DEBUG)
    transfer.callbacks.remove(saver.save)
    transfer.callbacks.remove(web_socket_sender.messages.append)
    transfer.callbacks.remove(Messages.messages.append)
    saver.close()

@app.websocket("/ws")
async def ws(websocket: WebSocket):
    await websocket.accept()
    web_socket_sender.clients.append(websocket)
    await web_socket_sender.thread_async()

@app.get("/messages")
async def list_messages():
    return Messages.messages


# https://github.com/SwarnadeepGhosh
# Six Days Weather Forecast using Python and MetaWeather API

import requests
import time

API_ROOT = 'https://www.metaweather.com'
API_LOCATION = '/api/location/search/?query='  # + city
API_WEATHER = '/api/location/'  # + woeid

def print_pause(printable_data):
    time.sleep(1)
    print(printable_data+'\n')

def display_weather(weather_data):
    for i in range(len(weather_data)):
        date = weather_data[i]['applicable_date']
        state = weather_data[i]['weather_state_name']
        max_temp = weather_data[i]['max_temp']
        max_temp = round(max_temp, 2)
        min_temp = weather_data[i]['min_temp']
        min_temp = round(min_temp, 2)
        print(f" {date} \t {state} \t High: {max_temp}°C \t Low: {min_temp}°C")

def check_again():
    again = input('\nDo You want to check again ? (y/n) ').lower()
    if again == 'n':
        print_pause('Have a Good Day ..')
        time.sleep(1.5)
    elif again == 'y':
        weather_report()
    else:
        print('I cant understand, Please try again..')
        check_again()

def weather_report():
    try:
        print('Swarnadeep Welcomes you to the Weather Report Forecast.\n')
        time.sleep(1)
        city = input('Where in the World are you ? ')
        print_pause('\nGetting Location Data...')
        r1 = requests.get(API_ROOT + API_LOCATION + city)
        # if r1.status_code == 200:
        location_data = r1.json()
        woeid = location_data[0]['woeid']
        # print("Where on Earth ID is : "+str(woeid))
        print_pause('Location Data Fetched successfully...')
        time.sleep(1)

        r2 = requests.get(API_ROOT + API_WEATHER + str(woeid))
        print_pause('Getting Weather Data, Please wait...')
        print_pause('Weather Data of ' + city.capitalize() + ':')
        # We will get a dictionary having keys: consolidated_weather, time, sun_rise, sun_set, timezone_name, parent, sources, title, location_type, woeid, latt_long, timezone.
        weather_data = r2.json()['consolidated_weather']
        # We will get 6 day Weather Forecast data in weather data as a list of dictionaries.
        # Each day having keys: id,weather_state_name, weather_state_abbr, wind_direction_compass, created, applicable_date, min_temp, max_temp, the_temp, wind_speed, wind_direction, air_pressure, humidity, visibility, predictability
        display_weather(weather_data)
        check_again()

    except requests.exceptions.ConnectionError:
        print('Sorry there is a network error')
        time.sleep(1)

    except:
        print("I don't know where that is. Please enter only famous cities.\n")
        time.sleep(1)
        weather_report()

if __name__ == '__main__':
    weather_report()


# -*- coding: iso-8859-1 -*-
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as mpl_cm
import iris
import iris.quickplot as qplt
import iris.plot as iplt
from iris.util import unify_time_units
import cartopy.crs as ccrs
import cartopy.feature as cfeat
import glob
import copy
import datetime
import netCDF4
import cf_units
from plotFunctions import line, plot_figure, MedianPairwiseSlopes, open_file_with_cbar_extents, plot_map_of_time_average

UNITS_DICT = {'CSDI': 'days', 'DTR': u'\u00B0C', 'FD': 'days', 'ID': 'days', 'SU': 'days',
              'TN10p': '%', 'TN90p': '%', 'TNn': u'\u00B0C', 'TNx': u'\u00B0C', 'TR': 'days',
              'TX10p': '%', 'TX90p': '%', 'TXn': u'\u00B0C', 'TXx': u'\u00B0C', 'WSDI': 'days' }
#'HW': 'days', 'TMGE10': 'days', 'TMGE5': 'days', 'TMLT10': 'days', 'TMLT10': 'days', 'TMm': u'\u00B0C','TMLT5': 'days', 'TX95T': u'\u00B0C', 'TXGE30': 'days', 'TXGE35': 'days', 'TXGT50P': '%',
#'TNLT2': 'days', 'TNLTM2': 'days', 'TNLTM20': 'days',
# gibt es, aber nicht bei climpact:  'GSL': 'days',
#UNITS_DICT = {'CSDI': 'days'}

#List of the indices which were calculated using python/climpact
python_indices = ['TXx', 'TNx', 'TXn', 'TNn', 'DTR', 'FD', 'TR']
climpact_indices = ['CSDI', 'ID', 'SU', 'TN10p', 'TN90p', 'TX10p', 'TX90p', 'WSDI']

REGIONS = {'SPAIN': [-7.5, 37.5, 0.0, 42.5], 'GERMANY': [5.0, 45.0, 15.0, 50.0], 'MOROCCO': [-5.0, 30.0, 5.0, 35.0]}  #westerly longitude, southerly latitude, easterly longitude, northerly latitude

TIME_FACTOR = 365*10.

cbar_extent_GERMANY = {}
cbar_extent_MOROCCO = {}
cbar_extent_SPAIN = {}

for INAME in UNITS_DICT.keys():
    print(INAME)
    ######################################################################################
    #load in data with constraints to correct region defined by longitudes and latitudes #
    ######################################################################################

    #INAME='TXx' #Decide which index should be used:
    filepath='/project/hadobs2/hadex3/ghcndex/GHCND_'+INAME+'_1951-2018_RegularGrid_global_2.5x2.5deg_LSmask.nc'
    time_constraint = iris.Constraint(time=lambda c: 20160101 > c.point > 19910100)
    #longitude_constraint = iris.Constraint(longitude=lambda c: 0<=c.point<=60 or 360.>=c.point>=342)
    latitude_constraint = iris.Constraint(latitude=lambda c: 22< c.point<60)

    '''
    original_data = original_data.extract(latitude_constraint)
    data = iris.cube.CubeList()
    for i in range(len(original_data)):
        #cube.intersection changes longitudes from 0-360 degree to -180 - +180 degree.
        data.append(original_data[i].intersection(longitude=(-18, 60)))
    '''

    for REGION in REGIONS:
        print(REGION)
        OUTPATH = '/scratch/vportge/plots/GHCNDEX/'+REGION+'/'

        original_data=iris.load(filepath, time_constraint) #Data has name of the months.
        if len(original_data) == 0:
            print(INAME)
            continue

        left_lon = float(REGIONS[REGION][0])
        right_lon = float(REGIONS[REGION][2])
        lower_lat = float(REGIONS[REGION][1])
        upper_lat = float(REGIONS[REGION][3])

        lat_constraint = iris.Constraint(latitude=lambda c: lower_lat <= c.point <= upper_lat)
        #lon_constraint = iris.Constraint(longitude=lambda c: left_lon <= c.point <= right_lon)

        original_data = original_data.extract(lat_constraint)

        data = iris.cube.CubeList()
        for i in range(len(original_data)):
            #cube.intersection changes longitudes from 0-360 degree to -180 - +180 degree.
            data.append(original_data[i].intersection(longitude=(left_lon, right_lon)))

        ######################################################################################
        #Change time coordinate of data as it only contains the month via .name() of the cube#
        ######################################################################################

        spat_avg_month = iris.cube.CubeList()

        for i in range(len(data)):
            month_data = data[i]
            month_time = month_data.coord('time')
            month_datetime = []

            for j in range(len(month_time.points)):
                yyyy = datetime.datetime.strptime(str(int(month_time.points[j])), '%Y%m%d').year
                if month_data.name() == 'Ann':
                    mm = '01'
                elif month_data.name() == 'Jan':
                    mm = '01'
                elif month_data.name() == 'Feb':
                    mm = '02'
                elif month_data.name() == 'Mar':
                    mm = '03'
                elif month_data.name() == 'Apr':
                    mm = '04'
                elif month_data.name() == 'May':
                    mm = '05'
                elif month_data.name() == 'Jun':
                    mm = '06'
                elif month_data.name() == 'Jul':
                    mm = '07'
                elif month_data.name() == 'Aug':
                    mm = '08'
                elif month_data.name() == 'Sep':
                    mm = '09'
                elif month_data.name() == 'Oct':
                    mm = '10'
                elif month_data.name() == 'Nov':
                    mm = '11'
                elif month_data.name() == 'Dec':
                    mm = '12'
                month_datetime.append(datetime.datetime.strptime(str(yyyy)+str(mm)+'01', '%Y%m%d'))

            times_nums_units = netCDF4.date2num(month_datetime, units = 'days since 1970-01-01 00:00', calendar = 'standard')
            time_unit = cf_units.Unit( 'days since 1970-01-01 00:00', calendar='standard')
            new_timecoord = iris.coords.DimCoord(times_nums_units, standard_name = 'time', units = time_unit, var_name = "time")
            month_data.remove_coord('time')
            month_data.add_dim_coord(new_timecoord,0)
            #calculate spatial average#
            if month_data.name() == 'Ann':
                ANN_data = copy.deepcopy(month_data)
                #calculate spatial average#
                ANN_data.coord('latitude').guess_bounds()
                ANN_data.coord('longitude').guess_bounds()
                ANN_data_areas = iris.analysis.cartography.area_weights(ANN_data)

                ANN_data_avg = ANN_data.collapsed(['latitude', 'longitude'], iris.analysis.MEAN, weights=ANN_data_areas)
                #ANN_data_avg = ANN_data_avg.collapsed('longitude', iris.analysis.MEAN)
                ANN_index = i*1.

            else:
                month_data.coord('latitude').guess_bounds()
                month_data.coord('longitude').guess_bounds()

                month_data_areas = iris.analysis.cartography.area_weights(month_data)

                month_avg = month_data.collapsed(['latitude', 'longitude'], iris.analysis.MEAN, weights=month_data_areas)
                #month_avg = month_avg.collapsed('longitude', iris.analysis.MEAN)
                spat_avg_month.append(month_avg)
        del(data[int(ANN_index)])

        #######################################################################################
        #cubelist.concatenate_cube() doesn't work so get the values and save them into a list.#
        #Leading to a list consisting of 12 lists (one for each month.) Sort the dates so that#
        #a time series can be plotted. times_spat_avg are the sorted dates (as numbers) and   #
        #values_spat_avg are the corresponding values of the index. (Spatially averaged!).    #
        #######################################################################################

        if len(original_data)>1:
            times_spat_avg = []
            values_spat_avg = []

            for i in spat_avg_month:
                time_month = i.coord('time')
                times_spat_avg.append(time_month.points)
                values_spat_avg.append(i.data)
            #flatten the lists
            times_spat_avg = [item for sublist in times_spat_avg for item in sublist]
            values_spat_avg = [item for sublist in values_spat_avg for item in sublist]
            #sort list by time coordinate
            times_spat_avg, values_spat_avg = (list(t) for t in zip(*sorted(zip(times_spat_avg, values_spat_avg))))

        ###################################################################
        #Plot map of averaged values over whole time period using ANN_data#
        ###################################################################
        '''
        plt.close()
        fig = plt.figure(figsize=(10, 8))
        ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
        #ax.set_extent([ -30., 65., 30, 65. ], crs=ccrs.PlateCarree())

        ANN_data.coord('time').guess_bounds()

        ANN_data_time_avg = ANN_data.collapsed('time', iris.analysis.MEAN)
        ANN_data_lon = ANN_data_time_avg.coord('longitude').points
        ANN_data_lat = ANN_data_time_avg.coord('latitude').points

        cont = iplt.pcolormesh(ANN_data_time_avg, cmap = 'CMRmap')

        ax.set_extent((np.amin(ANN_data_lon)-2, np.amax(ANN_data_lon)+2, np.amin(ANN_data_lat)-2, np.amax(ANN_data_lat)+2), crs = ccrs.PlateCarree())

        political_bdrys = cfeat.NaturalEarthFeature(category='cultural',
                                                    name='admin_0_countries',
                                                    scale='50m')
        ax.add_feature(political_bdrys, edgecolor='b', facecolor='none', zorder=2)
        cb=fig.colorbar(cont, ax=ax, orientation='horizontal')
        cb.set_label(INAME+' Index Value')
        plt.title('Map of averaged '+INAME+' values (GHCNDEX)')
        plt.savefig(OUTPATH+INAME+'_GHCNDEX_map_averaged_'+REGION+'.png')
        '''
        ########################################################
        #Plot time series of years 1991 - 2015 with trend line #
        ########################################################
        if len(original_data)>1: #Then it's monthly data.
            plt.close()
            YDATA = values_spat_avg
            XDATA = times_spat_avg
            #convert to datetime object so that it can be plotted easily.
            times_datetime = [netCDF4.num2date(i, units = 'days since 1970-01-01 00:00', calendar = 'standard') for i in times_spat_avg]

            trendanalysis = MedianPairwiseSlopes(XDATA,YDATA)
            slope = trendanalysis[0]
            slope_lower_uncrty = trendanalysis[1]
            slope_upper_uncrty = trendanalysis[2]
            Y_INTERCEPTION = trendanalysis[3]

            trendline=line(np.array(XDATA), np.array(Y_INTERCEPTION), slope)

            fig = plt.figure(figsize = (10, 8))
            plt.plot(times_datetime, YDATA)
            plt.plot(times_datetime, trendline, label='trend: '+str(round(slope*TIME_FACTOR,2))+ ' ' + UNITS_DICT[INAME]+' per decade')
            plt.grid()
            plt.title('Time series of monthly GHCNDEX ' + INAME + ' in ' + REGION , size=22)
            plt.xlabel('years', size=20)
            plt.ylabel(UNITS_DICT[INAME], size=20)

            plt.legend(fontsize = 16)
            plt.tight_layout()
            plt.tick_params(axis='both', which='major', labelsize=16)
            plt.savefig(OUTPATH+INAME+'_time_series_GHCNDEX_with_trend_monthly_'+REGION+'.png')

        YDATA = ANN_data_avg.data
        XDATA = ANN_data_avg.coord('time').points
        #convert to datetime object so that it can be plotted easily.
        times_datetime = [netCDF4.num2date(i, units = 'days since 1970-01-01 00:00', calendar = 'standard') for i in ANN_data_avg.coord('time').points]

        YDATA1 = YDATA[0:14]
        YDATA2 = YDATA[14:]
        XDATA1 = XDATA[0:14]
        XDATA2 = XDATA[14:]
        times_datetime1 = times_datetime[0:14]
        times_datetime2 = times_datetime[14:]

        trendanalysis = MedianPairwiseSlopes(XDATA,YDATA)
        slope = trendanalysis[0]
        Y_INTERCEPTION = trendanalysis[3]

        trendanalysis1 = MedianPairwiseSlopes(XDATA1,YDATA1)
        slope1 = trendanalysis1[0]
        Y_INTERCEPTION1 = trendanalysis1[3]

        trendanalysis2 = MedianPairwiseSlopes(XDATA2,YDATA2)
        slope2 = trendanalysis2[0]
        Y_INTERCEPTION2 = trendanalysis2[3]

        trendline=line(np.array(XDATA), np.array(Y_INTERCEPTION), slope)
        trendline1=line(np.array(XDATA1), np.array(Y_INTERCEPTION1), slope1)
        trendline2=line(np.array(XDATA2), np.array(Y_INTERCEPTION2), slope2)

        plt.close()
        fig=plt.figure(figsize = (10, 8))
        plt.plot(times_datetime, YDATA)
        plt.plot(times_datetime, trendline, label='trend: '+str(round(slope*TIME_FACTOR,2))+ ' ' + UNITS_DICT[INAME]+' per decade'+ ' (1991-2015)')
        plt.plot(times_datetime1, trendline1, label='trend: '+str(round(slope1*TIME_FACTOR,2))+ ' ' + UNITS_DICT[INAME]+' per decade'+ ' (1991-2004)')
        plt.plot(times_datetime2, trendline2, label='trend: '+str(round(slope2*TIME_FACTOR,2))+ ' ' + UNITS_DICT[INAME]+' per decade'+ ' (2005-2015)')

        plt.grid()
        plt.title('Time series of annually GHCNDEX ' + INAME + ' in ' + REGION , size=22)
        plt.xlabel('years', size=20)
        plt.ylabel(UNITS_DICT[INAME], size=20)

        plt.legend(fontsize = 16)
        plt.tight_layout()
        plt.tick_params(axis='both', which='major', labelsize=16)
        plt.savefig(OUTPATH+INAME+'_time_series_GHCNDEX_with_trend_annually_'+REGION+'.png')

        #####################################################
        #Plot map of trend for each gridpoint using ANN_data#
        #####################################################
        min_or_max = ['min', 'max']

        for cold_win in min_or_max:

            '''Read in the files which contain the lowest and highest values of CLIMPACT/PYTHON created indices.
               Those values can be used to set the extent of the GHCNDEX plots so that the colorbars show the same range.'''
            cbar_path_python = '/scratch/vportge/plots/textfiles_with_cbar_extent/'+cold_win+'_LST_in_cold_window_CMSAF_python_cbar_'+REGION+'.txt'
            cbar_path_climpact = '/scratch/vportge/plots/textfiles_with_cbar_extent/'+cold_win+'_LST_in_cold_window_CMSAF_climpact_cbar_'+REGION+'.txt'

            cbar_dict_python = open_file_with_cbar_extents(cbar_path_python)
            cbar_dict_climpact = open_file_with_cbar_extents(cbar_path_climpact)

            #Pcolormesh will be used for plotting which uses the boundarie points.
            GRIDLONS = np.append(ANN_data.coord('longitude').bounds[:,0], ANN_data.coord('longitude').bounds[-1,1])
            GRIDLATS = np.append(ANN_data.coord('latitude').bounds[:,0], ANN_data.coord('latitude').bounds[-1,1])
            TRENDS_ANN = np.ma.zeros(ANN_data.shape[1:3]) # lat, lon
            XDATA_ANN = ANN_data.coord('time').points

            for lat in range(ANN_data.shape[1]):
                for lon in range(ANN_data.shape[2]):
                    YDATA_GRIDPOINT = ANN_data[:, lat, lon]
                    TRENDS_ANN[lat,lon] = MedianPairwiseSlopes(XDATA_ANN,YDATA_GRIDPOINT.data)[0]*TIME_FACTOR

            TRENDS_ANN = np.ma.masked_where(np.isnan(TRENDS_ANN), TRENDS_ANN)
            OUTNAME = OUTPATH+INAME+'_map_of_trend_'+REGION+'_'+cold_win+'.png'

            if INAME in python_indices:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME , UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, cbar_dict_python[INAME]]

                if REGION == 'GERMANY':
                    cbar_extent_GERMANY[INAME] = plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

                elif REGION == 'SPAIN':
                    cbar_extent_SPAIN[INAME] = plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

                elif REGION == 'MOROCCO':
                    cbar_extent_MOROCCO[INAME] = plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            elif INAME in climpact_indices:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME , UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, cbar_dict_climpact[INAME.lower()]]
                if REGION == 'GERMANY':
                    cbar_extent_GERMANY[INAME] = plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

                elif REGION == 'SPAIN':
                    cbar_extent_SPAIN[INAME] = plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

                elif REGION == 'MOROCCO':
                    cbar_extent_MOROCCO[INAME] = plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            else:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME , UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, False]
                plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            ################################################################
            #begin Calculation for trends for first time period: 1991 -2004#
            ################################################################
            cbar_path_python = '/scratch/vportge/plots/textfiles_with_cbar_extent/'+cold_win+'_LST_in_cold_window_CMSAF_python_cbar_'+REGION+'_period1.txt'
            cbar_path_climpact = '/scratch/vportge/plots/textfiles_with_cbar_extent/'+cold_win+'_LST_in_cold_window_CMSAF_climpact_cbar_'+REGION+'_period1.txt'

            cbar_dict_python = open_file_with_cbar_extents(cbar_path_python)
            cbar_dict_climpact = open_file_with_cbar_extents(cbar_path_climpact)

            TRENDS_ANN = np.ma.zeros(ANN_data.shape[1:3]) # lat, lon
            XDATA_ANN = ANN_data.coord('time').points[0:14]
            for lat in range(ANN_data.shape[1]):
                for lon in range(ANN_data.shape[2]):
                    YDATA_GRIDPOINT = ANN_data[0:14, lat, lon]
                    TRENDS_ANN[lat,lon] = MedianPairwiseSlopes(XDATA_ANN,YDATA_GRIDPOINT.data)[0]*TIME_FACTOR

            TRENDS_ANN = np.ma.masked_where(np.isnan(TRENDS_ANN), TRENDS_ANN)
            OUTNAME = OUTPATH+INAME+'_1991-2004_map_of_trend_'+REGION+'_'+cold_win+'.png'

            if INAME in python_indices:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME +' (1991-2004)', UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, cbar_dict_python[INAME]]
                plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            elif INAME in climpact_indices:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME +' (1991-2004)', UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, cbar_dict_climpact[INAME.lower()]]
                plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            else:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME +' (1991-2004)', UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, False]
                plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            ################################################################
            #begin Calculation for trends for second time period: 2005-2015#
            ################################################################
            cbar_path_python = '/scratch/vportge/plots/textfiles_with_cbar_extent/'+cold_win+'_LST_in_cold_window_CMSAF_python_cbar_'+REGION+'_period2.txt'
            cbar_path_climpact = '/scratch/vportge/plots/textfiles_with_cbar_extent/'+cold_win+'_LST_in_cold_window_CMSAF_climpact_cbar_'+REGION+'_period2.txt'

            cbar_dict_python = open_file_with_cbar_extents(cbar_path_python)
            cbar_dict_climpact = open_file_with_cbar_extents(cbar_path_climpact)

            TRENDS_ANN = np.ma.zeros(ANN_data.shape[1:3]) # lat, lon
            XDATA_ANN = ANN_data.coord('time').points[14:]
            for lat in range(ANN_data.shape[1]):
                for lon in range(ANN_data.shape[2]):
                    YDATA_GRIDPOINT = ANN_data[14:, lat, lon]
                    TRENDS_ANN[lat,lon] = MedianPairwiseSlopes(XDATA_ANN,YDATA_GRIDPOINT.data)[0]*TIME_FACTOR

            TRENDS_ANN = np.ma.masked_where(np.isnan(TRENDS_ANN), TRENDS_ANN)
            OUTNAME = OUTPATH+INAME+'_2005-2015_map_of_trend_'+REGION+'_'+cold_win+'.png'

            if INAME in python_indices:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME +' (2005-2015)', UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, cbar_dict_python[INAME]]
                plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            elif INAME in climpact_indices:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME +' (2005-2015)', UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, cbar_dict_climpact[INAME.lower()]]
                plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            else:
                FIG_INFO =  ['Trend of annually GHCNDEX ' + INAME +' (2005-2015)', UNITS_DICT[INAME], OUTPATH, REGION, OUTNAME, False]
                plot_figure(TRENDS_ANN, GRIDLONS, GRIDLATS, FIG_INFO)

            #Plot map of averaged values
            CUBEINFO = ['annually', INAME, REGION, 'ANN', OUTPATH, 'GHCNDEX', TIME_FACTOR, UNITS_DICT[INAME]]
            plot_map_of_time_average(ANN_data, CUBEINFO)

OUTPATH_trends = '/scratch/vportge/plots/GHCNDEX/'

with open(OUTPATH_trends+'cbar_GERMANY.txt', 'w') as f:
    for key, value in cbar_extent_GERMANY.items():
        f.write('%s, %s\n' % (key, value))

with open(OUTPATH_trends+'cbar_SPAIN.txt', 'w') as f:
    for key, value in cbar_extent_SPAIN.items():
        f.write('%s, %s\n' % (key, value))

with open(OUTPATH_trends+'cbar_MOROCCO.txt', 'w') as f:
    for key, value in cbar_extent_MOROCCO.items():
        f.write('%s, %s\n' % (key, value))


"""
Created on Fri Dec  3 09:10:44 2021

@author: adaskin
"""
import random
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sim_tree import generate_tree_elements,sum_of_nonzeropaths
import sklearn.datasets as datasets
import scipy.sparse

def polar2xy(r, theta):
    x = r*math.cos(theta)
    y = r*math.sin(theta)
    return x,y

def randPolarDot(r1, r2):
    r = random.random() # [0, 1]
    r = r*(r2-r1) + r1
    theta = random.random()*math.pi*2 #hatali
    return r, theta
figure, ax1 = plt.subplots()
#ax2 = figure2.add_subplot(2,2,1,projection='3d')

CL = ['tab:blue','tab:orange','tab:green','tab:red','tab:purple','tab:brown']
nrings = 4
ndots = 256

ringSize = 5;

X = np.zeros([nrings*ndots,2])
count_dot = 0
for iring in range(nrings):
    for jdot in range(ndots):

        [r, theta] = randPolarDot(iring*ringSize, (iring+1)*ringSize)
        [x, y] = polar2xy(r, theta)
        X[count_dot,:] = [x, y]
        count_dot += 1

        ax1.scatter(x, y, color=CL[iring],marker='.')
        #ax2.scatter3D(x, y, x**2+y**2, c=CL[iring]);

#ax1.set_xlabel("x")
#ax1.set_ylabel("y")
figure.savefig('rings.eps', bbox_inches='tight')
figure.savefig('rings.png',bbox_inches='tight')

threshold = 0.020

G = X@X.transpose()
if scipy.sparse.issparse(G):
    G = G.toarray()

psi = G.flatten()
psi_norm = np.linalg.norm(psi)
psi = psi/psi_norm
(usv_tree, nonzeropath) = generate_tree_elements(psi)
L = []
for i in range(int(psi.size/2)-1, psi.size-1):
    if isinstance(usv_tree[i], tuple):
        u, s, v, sprev = usv_tree[i]
        print("shape:", v.shape)
        L.append(usv_tree[i][3])
    else:
        L.append(0)
fig, ax = plt.subplots()
ax.axvline(threshold, linestyle='--')
values, bins, bars = ax.hist(L)

ax.set_xlabel("probability (coefficient) of the path")
ax.set_ylabel("Number of paths")
ax.set_title('n: {}-qubits, data: {} '.format(int(np.log2(psi.size)), 'rings'))
ax.bar_label(bars, fontsize=9, color='red')

p, npaths = sum_of_nonzeropaths(usv_tree,threshold)
print(np.linalg.norm(p-psi))
print(np.linalg.norm(p-psi,1))
print(np.dot(psi,p))
#X_tensor = psi_norm*p.reshape(N,N)
norm_of_diff = np.linalg.norm(psi-p)
print("norm of diff:",norm_of_diff)
ax.text(0.75, 0.75, 'norm of diff={:5.2E}'.format(norm_of_diff), horizontalalignment='center',
     verticalalignment='center', transform=ax.transAxes)
#print("mean error matrix unnormalized:",np.mean((X_tensor-G)**2))
plt.savefig('rings20qubits.eps', bbox_inches='tight')
plt.savefig('rings20qubits.png',bbox_inches='tight')


import cv2 as cv
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.cm as cm
from time import sleep
import numpy as np

face_detector = cv.CascadeClassifier('haarcascade_frontalface_default.xml')

def show_points(room_size,tracked_points,cam_position,pad=2):

    room_width = room_size[2]-room_size[0];
    room_height = room_size[3]-room_size[1];
    rectangle = plt.Rectangle((0,0), room_width, room_height);#, fc='r')
    plt.plot((cam_position[0],cam_position[0]), (0,room_height),color='yellow',linewidth=5);
    plt.gca().add_patch(rectangle)
    plt.xlim((room_size[0]-pad,room_size[2]+pad));
    plt.ylim((room_size[1]-pad,room_size[3]+pad));
    plt.plot(cam_position[0],cam_position[1],marker='D',color='red',markersize=25,label=' Web Camera');

    temp_id = 0;
    colormap = plt.cm.gist_ncar;
    colorst = [colormap(i) for i in np.linspace(0, 0.9,len(tracked_points))];  #to get each person a different color
    for i in tracked_points:
        temp_id += 1;
        plt.plot(i[0],i[1],marker='o',color=colorst[temp_id-1]);
        plt.pause(0.5);

    plt.legend(loc='best');
    plt.pause(0.5);
    plt.clf();#to reuse the same plot and alleviates the use of explicit closing of plots.


import json
import os
from logic.payment_method import PaymentMethod
PATH = os.getcwd()
DIR_DATA = PATH + '{0}data{0}'.format(os.sep)

class PaymentMethodController(object):
    def __init__(self):
        self.file = '{0}{1}'.format(DIR_DATA, 'payment.json')

    def add(self, payment_method: PaymentMethod = PaymentMethod()) -> str:
        with open(self.file, 'r+') as f:
            data = json.load(f)
            data['payment_methods'].append(payment_method.__dict__)
            print(payment_method.__str__())
            f.seek(0)
            json.dump(data, f)
        f.close()
        return payment_method.__str__()

    def show(self):
        with open(self.file, 'r') as f:
            json_object = json.load(f)
        return json_object

    def select(self, value):
        with open(self.file, 'r+') as file:
            data = json.load(file)
            for payment_method in data['payment_methods']:
                if value in str(payment_method.values()):
                    return payment_method.__str__()


import pandas as pd
import torch
from src.common.tools import model_device
from src.callback.progressbar import ProgressBar
from configs.basic_config import config

class Generator(object):
    def __init__(self, model, tok, logger, n_gpu, input_len=None):
        self.model = model
        self.tok = tok
        self.logger = logger
        self.input_len = input_len
        self.model, self.device = model_device(n_gpu=n_gpu, model=self.model)

    def generate_example(self,
                         data,
                         sampler,
                         max_length,
                         relation_index=None,
                         repetition_penalty=None,
                         no_repeat_ngram_size=None,
                         length_penalty=None,
                         num_return_sequences=1,
                         save_prefix=None,
                         add_cls=False):

        pbar = ProgressBar(n_total=len(data), desc='Testing')
        print_interval = len(data) // 10

        all_generated_sents = []
        all_label_sents = []
        all_input_sents = []
        all_encoder_states = []
        all_cls_ids = []
        all_self_attention = []
        all_cross_attentions = []
        all_encoder_attentions = []
        all_input_ids = []
        all_output_ids = []
        all_input_mask = []

        relations = config['relations']

        with torch.no_grad():
            for step, batch in enumerate(data):
                batch = tuple(t.to(self.device) for t in batch)
                input_ids, input_mask, segment_ids, label_ids, labels_masks, cls_label = batch

                # [bsz, ln]
                batch_size, ln = input_ids.shape

                char_relation = self.tok(relations, padding=True, return_length=True, return_tensors='pt')
                relation_ids = char_relation["input_ids"].to(input_ids.device)
                relation_mask = char_relation["attention_mask"].to(input_ids.device)

                dim = relation_ids.size(0)

                kg_input_ids = []
                for i in range(batch_size):
                    cur_kg_input_ids = []
                    cur_input_ids = input_ids[i]
                    cur_ln = input_mask[i].sum()
                    for j in range(dim):
                        char_ln = relation_mask[j].sum()
                        cur_relation_ids = relation_ids[j]
                        if cur_ln + char_ln > ln:
                            it = torch.cat([cur_input_ids[:ln - char_ln - 1], cur_input_ids[cur_ln - 1:cur_ln],
                                            cur_relation_ids[:char_ln]],
                                           dim=-1).unsqueeze(0)
                            cur_kg_input_ids.append(it)
                        else:
                            it = torch.cat(
                                [cur_input_ids[:cur_ln], cur_relation_ids[:char_ln], cur_input_ids[cur_ln + char_ln:]],
                                dim=-1).unsqueeze(0)
                            cur_kg_input_ids.append(it)
                    kg_input_ids.append(torch.cat(cur_kg_input_ids, dim=0).unsqueeze(0))
                kg_input_ids = torch.cat(kg_input_ids, dim=0)
                kg_input_masks = kg_input_ids.ne(self.tok.pad_token_id).int()

                # Encoder the story
                encoder_outputs = self.model.encoder(input_ids=input_ids,
                                                     input_masks=input_mask,
                                                     segment_ids=segment_ids,
                                                     kg_input_ids=kg_input_ids,
                                                     kg_input_masks=kg_input_masks,
                                                     )

                encoder_hidden_states = encoder_outputs["encoder_hidden_states"]
                encoder_attentions = encoder_outputs["encoder_attention_mask"]

                if relation_index is not None:
                    encoder_attentions[:, :dim] = 0
                    encoder_attentions[:, relation_index] = 1

                decoder_input_ids = torch.ones([batch_size, 1], dtype=torch.long) * self.tok.bos_token_id
                decoder_input_ids = decoder_input_ids.to(self.device)
                batch_data = {}
                batch_data["input_ids"] = decoder_input_ids
                batch_data["input_attention_mask"] = torch.ones_like(decoder_input_ids, dtype=torch.long)
                batch_data["encoder_hidden_states"] = encoder_hidden_states
                batch_data["encoder_padding_mask"] = encoder_attentions

                decoder = self.model.decoder

                sampling_result = sampler.generate_sequence(batch_data,
                                                            decoder,
                                                            start_idx=1,
                                                            end_len=max_length,
                                                            repetition_penalty=repetition_penalty,
                                                            no_repeat_ngram_size=no_repeat_ngram_size,
                                                            length_penalty=length_penalty,
                                                            num_return_sequences=num_return_sequences)
                seqs = sampling_result["beams"]
                # decoder_hidden_states = sampling_result["hidden_states"]
                all_generated_sents += seqs

                label_sent = self.tok.batch_decode(label_ids, skip_special_tokens=True)
                all_label_sents += label_sent

                input_sent = self.tok.batch_decode(input_ids, skip_special_tokens=True)
                all_input_sents += input_sent

                all_encoder_states.append(encoder_hidden_states)

                output_ids = sampling_result["output_ids"]
                assert output_ids.size(0) == batch_size
                output_mask = (1 - output_ids.eq(self.tok.pad_token_id).int()).to(self.device)

                decoder_outputs = decoder(input_ids=output_ids,
                                          attention_mask=output_mask,
                                          encoder_hidden_states=encoder_hidden_states,
                                          encoder_attention_mask=encoder_attentions,
                                          past_key_values=None,
                                          inputs_embeds=None,
                                          use_cache=None,
                                          output_attentions=True,
                                          output_hidden_states=None,
                                          return_dict=True, )

                attentions = decoder_outputs["attentions"]  # list of [bsz, self.num_heads, tgt_len, src_len]
                cross_attentions = decoder_outputs[
                    "cross_attentions"]  # list of [bsz, self.num_heads, tgt_len, src_len]

                # print(f"attention from last decoder layer: {attentions[-1].size()}")
                # print(f"cross attention from last encoder layer: {cross_attentions[-1].size()}")
                # print(f"attention from last encoder layer: {encoder_attentions[-1].size()}")
                all_encoder_attentions.append(encoder_attentions[0].cpu().detach())
                all_self_attention.append(attentions[0].cpu().detach())
                all_cross_attentions.append(cross_attentions[0].cpu().detach())
                all_input_ids.append(input_ids.cpu().detach())
                all_output_ids.append(output_ids.cpu().detach())
                all_input_mask.append(input_mask.cpu().detach())

                if add_cls:
                    decoder_hidden_states = decoder_outputs["hidden_states"]  # [bsz, ln, E]
                    eos_hidden_states = []
                    eos_index = torch.sum(output_mask, dim=1) - 2  # [bsz]
                    batch_size = eos_index.size(0)
                    for i in range(batch_size):
                        eos_hidden_states.append(decoder_hidden_states[i, eos_index[i], :])
                    eos_hidden_states = torch.cat([x.unsqueeze(0) for x in eos_hidden_states])
                    cls_logits = self.model.classification_head(eos_hidden_states)  # [bsz, num_labels]
                    cls_ids = torch.argmax(cls_logits.softmax(dim=-1), dim=-1)
                    all_cls_ids.append(cls_ids)

                if (step + 1) % max(1, print_interval) == 0:
                    show_info = pbar(step=step)
                    self.logger.info(show_info)

            data = {"input_ids": all_input_ids,
                    "input_mask": all_input_mask,
                    "output_ids": all_output_ids,
                    "encoder_attentions": all_encoder_attentions,
                    "self_attention": all_self_attention,
                    "cross_attentions": all_cross_attentions
                    }

            # file_path = f"test_attentions_layer_0_{self.args.data_name}.pkl"
            # import pickle
            # with open(file_path, 'wb') as f:
            #     pickle.dump(data, f)

            if save_prefix is not None:
                columns = ["input", "generated", "reference"]
                # print(f"all_generated_sents: {len(all_generated_sents)} -- all_label_sents: {len(all_label_sents)}")
                df = pd.DataFrame(columns=columns)
                df["input"] = all_input_sents
                df["generated"] = all_generated_sents
                df["reference"] = all_label_sents
                if len(all_cls_ids) != 0:
                    df["cls_id"] = torch.cat(all_cls_ids, dim=0).cpu().detach().tolist()
                df.to_csv(f"{save_prefix}")

                self.logger.info(f"save sentences in csv files: {save_prefix}. Done!")

            if 'cuda' in str(self.device):
                torch.cuda.empty_cache()

        return all_label_sents, all_generated_sents, all_encoder_states, all_cls_ids

    def generate_explanation(self,
                             data,
                             KG_model,
                             sampler,
                             max_length,
                             relations=None,
                             repetition_penalty=None,
                             no_repeat_ngram_size=None,
                             length_penalty=None,
                             save_prefix=None,
                             KG_only=False,
                             num_return_sequences=1):

        pbar = ProgressBar(n_total=len(data), desc='Testing')
        print_interval = len(data) // 10

        all_generated_sents = []
        all_label_sents = []
        all_input_sents = []
        all_encoder_states = []
        all_cls_ids = []

        relations = relations if relations is not None else config['relations']

        with torch.no_grad():
            for step, batch in enumerate(data):
                batch = tuple(t.to(self.device) for t in batch)
                input_ids, input_mask, segment_ids, label_ids, labels_masks, labels_segment_ids, cls_label = batch

                # [bsz, ln]
                batch_size, ln = input_ids.shape

                char_relation = self.tok(relations, padding=True, return_length=True, return_tensors='pt')
                relation_ids = char_relation["input_ids"].to(input_ids.device)
                relation_mask = char_relation["attention_mask"].to(input_ids.device)

                dim = relation_ids.size(0)

                kg_input_ids = []
                for i in range(batch_size):
                    cur_kg_input_ids = []
                    cur_input_ids = input_ids[i]
                    cur_ln = input_mask[i].sum()
                    for j in range(dim):
                        char_ln = relation_mask[j].sum()
                        cur_relation_ids = relation_ids[j]
                        if cur_ln + char_ln > ln:
                            it = torch.cat([cur_input_ids[:ln - char_ln - 1], cur_input_ids[cur_ln - 1:cur_ln],
                                            cur_relation_ids[:char_ln]],
                                           dim=-1).unsqueeze(0)
                            cur_kg_input_ids.append(it)
                        else:
                            it = torch.cat(
                                [cur_input_ids[:cur_ln], cur_relation_ids[:char_ln], cur_input_ids[cur_ln + char_ln:]],
                                dim=-1).unsqueeze(0)
                            cur_kg_input_ids.append(it)
                    kg_input_ids.append(torch.cat(cur_kg_input_ids, dim=0).unsqueeze(0))
                kg_input_ids = torch.cat(kg_input_ids, dim=0).to(self.device)
                kg_input_masks = kg_input_ids.ne(self.tok.pad_token_id).int().to(self.device)

                # Encoder the story
                if KG_only:
                    encoder = KG_model.get_encoder().to(self.device)
                    encoder_outputs = encoder(input_ids=kg_input_ids.reshape([batch_size * dim, ln]),
                                              attention_mask=kg_input_masks.reshape([batch_size * dim, ln]),
                                              inputs_embeds=None,
                                              output_attentions=None,
                                              output_hidden_states=None,
                                              return_dict=None, )
                    KG_hidden_states = encoder_outputs[0]
                else:
                    encoder_outputs = self.model.encoder(input_ids=input_ids,
                                                         input_masks=input_mask,
                                                         segment_ids=segment_ids,
                                                         kg_input_ids=kg_input_ids,
                                                         kg_input_masks=kg_input_masks,
                                                         )

                    KG_hidden_states = encoder_outputs["kg_hidden_states"]  # [bsz, dim, ln, E]

                bsz, dim, ln = kg_input_masks.size()

                decoder_input_ids = torch.ones([batch_size * dim, 1], dtype=torch.long) * self.tok.bos_token_id
                decoder_input_ids = decoder_input_ids.to(self.device)
                batch_data = {}
                batch_data["input_ids"] = decoder_input_ids
                batch_data["input_attention_mask"] = torch.ones_like(decoder_input_ids, dtype=torch.long).to(
                    self.device)
                batch_data["encoder_hidden_states"] = KG_hidden_states.reshape([batch_size * dim, ln, -1]).to(
                    self.device)
                batch_data["encoder_padding_mask"] = kg_input_masks.reshape([batch_size * dim, ln]).to(self.device)

                decoder = KG_model.to(self.device)

                sampling_result = sampler.generate_sequence(batch_data,
                                                            decoder,
                                                            start_idx=1,
                                                            end_len=max_length,
                                                            repetition_penalty=repetition_penalty,
                                                            no_repeat_ngram_size=no_repeat_ngram_size,
                                                            length_penalty=length_penalty,
                                                            bart_model=True,
                                                            num_return_sequences=num_return_sequences)
                seqs = sampling_result["beams"]

                # decoder_hidden_states = sampling_result["hidden_states"]
                dim = dim * num_return_sequences
                seqs_results = []
                for i in range(batch_size):
                    batch_seqs = seqs[i * dim:(i + 1) * dim]
                    batch_seqs_list = []
                    for j in range(int(dim / num_return_sequences)):
                        batch_seqs_list.append(
                            '|'.join(batch_seqs[j * num_return_sequences: (j + 1) * num_return_sequences]))
                    # print(f"batch_seqs_list: {len(batch_seqs_list)},\n {batch_seqs_list}")
                    seqs_results.append(batch_seqs_list)
                all_generated_sents += seqs_results

                input_sent = self.tok.batch_decode(input_ids, skip_special_tokens=True)
                all_input_sents += input_sent

                output_ids = sampling_result["output_ids"]
                assert output_ids.size(0) == batch_size * dim

                if (step + 1) % max(1, print_interval) == 0:
                    show_info = pbar(step=step)
                    self.logger.info(show_info)

            if save_prefix is not None:
                if "csv" in save_prefix:
                    df = pd.DataFrame(all_generated_sents, columns=relations)
                    df.to_csv(f"{save_prefix}")
                else:
                    assert "pkl" in save_prefix
                    import pickle
                    with open(save_prefix, 'wb') as f:
                        pickle.dump({"input": all_input_sents,
                                     "generated": all_generated_sents}, f)

                self.logger.info(f"save sentences in csv files: {save_prefix}. Done!")

            if 'cuda' in str(self.device):
                torch.cuda.empty_cache()

        return all_label_sents, all_generated_sents, all_encoder_states, all_cls_ids


import ape
from ape import Contract, reverts
from utils.checks import check_strategy_totals
from utils.utils import days_to_secs
import pytest

def test__set_uni_fees(
    asset,
    strategy,
    management,
    aave,
    weth,
):
    # Everything should start as 0
    assert strategy.uniFees(aave, weth) == 0
    assert strategy.uniFees(weth, aave) == 0

    strategy.setUniFees(aave, weth, 500, sender=management)

    assert strategy.uniFees(aave, weth) == 500
    assert strategy.uniFees(weth, aave) == 500

    strategy.setUniFees(weth, aave, 5, sender=management)

    assert strategy.uniFees(aave, weth) == 5
    assert strategy.uniFees(weth, aave) == 5

    strategy.setUniFees(weth, aave, 0, sender=management)

    assert strategy.uniFees(aave, weth) == 0
    assert strategy.uniFees(weth, aave) == 0

def test__set_uni_fees__reverts(
    strategy,
    user,
    aave,
    weth,
):
    # Everything should start as 0
    assert strategy.uniFees(aave, weth) == 0
    assert strategy.uniFees(weth, aave) == 0

    with reverts("!Authorized"):
        strategy.setUniFees(weth, aave, 500, sender=user)

    assert strategy.uniFees(aave, weth) == 0
    assert strategy.uniFees(weth, aave) == 0

def test__set_min_amount_to_sell(
    strategy,
    management,
):
    assert strategy.minAmountToSell() == 1e4

    amount = 0

    strategy.setMinAmountToSell(amount, sender=management)

    assert strategy.minAmountToSell() == amount

    amount = int(100e18)

    strategy.setMinAmountToSell(amount, sender=management)

    assert strategy.minAmountToSell() == amount

def test__set_min_amount_to_sell__reverts(
    strategy,
    user,
):
    assert strategy.minAmountToSell() == 1e4

    with reverts("!Authorized"):
        strategy.setMinAmountToSell(0, sender=user)

    assert strategy.minAmountToSell() == 1e4

def test__emergency_withdraw__reverts(strategy, user, deposit, amount):
    with reverts("!Authorized"):
        strategy.emergencyWithdraw(100, sender=user)

    deposit()

    check_strategy_totals(
        strategy,
        total_assets=amount,
        total_debt=amount,
        total_idle=0,
        total_supply=amount,
    )

    with reverts("!Authorized"):
        strategy.emergencyWithdraw(100, sender=user)


# Вспомогательные функции для получения времени пешего похода до метро и коородинат объекта

import requests
import json
import time
from random import randint
from sklearn import metrics
import numpy as np

def regression_results(y_true, y_pred):

    # Regression metrics
    explained_variance=metrics.explained_variance_score(y_true, y_pred)
    mean_absolute_error=metrics.mean_absolute_error(y_true, y_pred)
    mse=metrics.mean_squared_error(y_true, y_pred)
    #mean_squared_log_error=metrics.mean_squared_log_error(y_true, y_pred)
    r2=metrics.r2_score(y_true, y_pred)

    print('explained_variance: ', round(explained_variance,4))
    #print('mean_squared_log_error: ', round(mean_squared_log_error,4))
    print('r2: ', round(r2,4))
    print('MAE: ', round(mean_absolute_error,4))
    print('MSE: ', round(mse,4))
    print('RMSE: ', round(np.sqrt(mse),4))

def get_coords(region, addr):
    if "г," in region:
        addr = region + " " + addr
    time.sleep(randint(1, 2))
    j = requests.get("http://search.maps.sputnik.ru/search/addr?q={0}".format(addr)).content
    resp = json.loads(j)
    if "address" in resp["result"]:
        return resp["result"]["address"][0]["features"][0]["geometry"]["geometries"][0]["coordinates"][1], resp["result"]["address"][0]["features"][0]["geometry"]["geometries"][0]["coordinates"][0]
    else:
        return [0, 0]

def get_closest_metro(lng, lat):
    try:
        j = requests.get("https://json.smappi.org/milash/metro-station/getClosest?city=Москва&lat={0}&lon={1}&count=1".format(lat, lng)).content
        resp = json.loads(j)
        if "status" in resp:
            return None
        time.sleep(randint(1, 2))
        j = requests.get("http://footroutes.maps.sputnik.ru/?loc={0},{1}&loc={2},{3}".format(lat, lng, resp[0]["station"]["geo_lat"], resp[0]["station"]["geo_lon"])).content
        resp = json.loads(j)
        return resp["route_summary"]["total_time"]
    except Exception:
        return -1


from urllib.parse import urlsplit

from django.contrib.sites.models import Site
from django.templatetags.static import static

from ..core.utils import build_absolute_uri

def get_email_context():
    site: Site = Site.objects.get_current()
    logo_url = build_absolute_uri(static("images/logo-light.svg"))
    send_email_kwargs = {"from_email": site.settings.default_from_email}
    email_template_context = {
        "domain": site.domain,
        "logo_url": logo_url,
        "site_name": site.name,
    }
    return send_email_kwargs, email_template_context

def prepare_url(params: str, redirect_url: str) -> str:
    """Add params to redirect url."""
    split_url = urlsplit(redirect_url)
    split_url = split_url._replace(query=params)
    return split_url.geturl()


# pylint: disable=W0621,C0114,C0116,W0212,W0613
import pathlib
from typing import cast, Optional

import pytest
import pytest_mock

from dae.genotype_storage.genotype_storage_registry import \
    get_genotype_storage_factory
from dae.duckdb_storage.duckdb_genotype_storage import \
    DuckDbGenotypeStorage

from dae.testing import setup_pedigree, setup_vcf, vcf_study
from dae.testing.foobar_import import foobar_gpf
from dae.studies.study import GenotypeData

@pytest.fixture
def duckdb_storage_db(
        tmp_path_factory: pytest.TempPathFactory) -> DuckDbGenotypeStorage:
    storage_path = tmp_path_factory.mktemp("duckdb_storage")
    storage_config = {
        "id": "dev_duckdb_storage",
        "storage_type": "duckdb",
        "db": "duckdb_genotype_storage/dev_storage.db",
        "read_only": False,
        "base_dir": storage_path
    }
    storage_factory = get_genotype_storage_factory("duckdb")
    genotype_storage = cast(
        DuckDbGenotypeStorage, storage_factory(storage_config))
    genotype_storage.start()
    return genotype_storage

@pytest.fixture
def duckdb_storage_parquet(
        tmp_path_factory: pytest.TempPathFactory) -> DuckDbGenotypeStorage:
    storage_path = tmp_path_factory.mktemp("duckdb_storage")
    storage_config = {
        "id": "dev_duckdb_storage",
        "storage_type": "duckdb",
        "studies_dir": "duckdb_genotype_storage",
        "base_dir": str(storage_path)
    }
    storage_factory = get_genotype_storage_factory("duckdb")
    genotype_storage = cast(
        DuckDbGenotypeStorage, storage_factory(storage_config))
    genotype_storage.start()
    return genotype_storage

def imported_study(
        root_path: pathlib.Path,
        duckdb_storage: DuckDbGenotypeStorage) -> GenotypeData:

    gpf_instance = foobar_gpf(root_path, duckdb_storage)
    ped_path = setup_pedigree(
        root_path / "vcf_data" / "in.ped",
        """
        familyId personId dadId	 momId	sex status role
        f1       m1       0      0      2   1      mom
        f1       d1       0      0      1   1      dad
        f1       p1       d1     m1     2   2      prb
        """)
    vcf_path = setup_vcf(
        root_path / "vcf_data" / "in.vcf.gz",
        """
        ##fileformat=VCFv4.2
        ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
        ##contig=<ID=foo>
        #CHROM POS ID REF ALT QUAL FILTER INFO FORMAT m1  d1  p1
        foo    13  .  G   C   .    .      .    GT     0/1 0/0 0/1
        foo    14  .  C   T   .    .      .    GT     0/0 0/1 0/1
        """)

    study = vcf_study(
        root_path,
        "minimal_vcf", ped_path, [vcf_path],
        gpf_instance)

    return study

@pytest.mark.parametrize(
    "base_dir,parquet_scan,expected",
    [
        ("/test/base/dir", "parquet_scan('aa/bb')",
         "parquet_scan('/test/base/dir/aa/bb')"),
        (None, "parquet_scan('aa/bb')",
         "parquet_scan('aa/bb')"),
        ("/test/base/dir", "parquet_scan('/aa/bb')",
         "parquet_scan('/aa/bb')"),
        ("/test/base/dir", None,
         None),
        ("/test/base/dir", "ala_bala",
         "ala_bala"),
    ],
)
def test_base_dir_join_parquet_scan(
        base_dir: Optional[str], parquet_scan: str, expected: str,
        duckdb_storage_parquet: DuckDbGenotypeStorage,
        mocker: pytest_mock.MockerFixture) -> None:

    mocker.patch.object(
        duckdb_storage_parquet,
        "get_base_dir",
        return_value=base_dir
    )
    res = duckdb_storage_parquet\
        ._base_dir_join_parquet_scan_or_table(parquet_scan)
    assert res == expected

def test_parquet_storage(
        tmp_path_factory: pytest.TempPathFactory,
        duckdb_storage_parquet: DuckDbGenotypeStorage) -> None:

    root_path = tmp_path_factory.mktemp("test_parquet_storage")
    study = imported_study(root_path, duckdb_storage_parquet)
    assert study is not None

    vs = list(study.query_variants())
    assert len(vs) == 2


import lbp_face_recognition

from Tkinter import *
import tkFileDialog
from Tkinter import Label,Tk
from PIL import Image, ImageTk

class home:
    def __init__(self,master):

        lbp_face_recognition.train()

        self.master=master
        self.f=Frame(master,width=1000,height=600)
        self.f.propagate(0)
        self.f.pack()
        self.f["bg"]='#2874A6'
        heading=Label(self.f,text="Low Light Facial Recognition",bg='#2874A6',font=('Times new Roman',-50,'bold')).grid(row=1,column=2,padx=20,pady=50)
        #heading.place(x=350,y=200)
        buttonchoose=Button(self.f,text="Choose Test Image",command=self.choose,font=('times new roman',-20),width=15,height=3).grid(row=6,column=1,padx=20,pady=50)
        buttontest=Button(self.f,text="Find Match",command=self.test,font=('times new roman',-20),width=15,height=3).grid(row=7,column=1,padx=20,pady=50)

    def choose(self):
        global path
        path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
        im = Image.open(path)
        im2 = im.resize((250, 250), Image.ANTIALIAS)

        tkimage = ImageTk.PhotoImage(im2)

        myvar1=Label(self.f,image = tkimage).grid(row=6,column =2)
        myvar1.image = tkimage
        myvar1.pack()

    def test(self):
        match_path = lbp_face_recognition.hist_get(path)
        im = Image.open(match_path)
        im2 = im.resize((250, 250), Image.ANTIALIAS)

        tkimage = ImageTk.PhotoImage(im2)

        myvar2=Label(self.f,image = tkimage).grid(row=7,column =2)
        myvar2.image = tkimage
        myvar2.pack()

root=Tk()
home=home(root)
root.mainloop()


from scripts.helpful_scripts import get_account
from scripts.get_weth import get_weth
from brownie import config, network, interface
from web3 import Web3

amount = Web3.toWei(0.1, "ether")

def main():
    account = get_account()
    erc20_address = config["networks"][network.show_active()]["weth_token"]
    # call get_weth if no weth balance
    if network.show_active() in ["mainnet-fork"]:
        get_weth()
    lending_pool = get_lending_pool()
    # We need to approve sending out ERC20 tokens
    approve_erc20(amount, lending_pool.address, erc20_address, account)
    # Now deposit to aave
    print("Depositing...")
    tx = lending_pool.deposit(
        erc20_address, amount, account.address, 0, {"from": account}
    )
    tx.wait(1)
    print("Deposited!")
    # Now we can borrow, but how much??
    # Lets get some stats so we know where we stand and keep account healthy
    borrowable_eth, total_debt = get_borrowable_data(lending_pool, account)
    # to borrow DAI we need conversion rate in terms of ETH
    dai_eth_price = get_asset_price(
        config["networks"][network.show_active()]["dai_eth_price_feed"]
    )
    # converting borrowable eth to borrowable Dai and taking only 95% for safety (lower % = safer)
    amount_dai_to_borrow = (1 / dai_eth_price) * (borrowable_eth * 0.95)
    print(f"We are going to borrow {amount_dai_to_borrow} DAI")
    dai_address = config["networks"][network.show_active()]["dai_token"]
    borrow_tx = lending_pool.borrow(
        dai_address,
        Web3.toWei(amount_dai_to_borrow, "ether"),
        1,
        0,
        account.address,
        {"from": account},
    )
    borrow_tx.wait(1)
    print("We borrowed some DAI")
    get_borrowable_data(lending_pool, account)
    # repay_all(amount, lending_pool, account)

def repay_all(amount, lending_pool, account):
    # First we need to approve repay
    approve_erc20(
        Web3.toWei(amount, "ether"),
        lending_pool,
        config["networks"][network.show_active()]["dai_token"],
        account,
    )
    repay_tx = lending_pool.repay(
        config["networks"][network.show_active()]["dai_token"],
        amount,
        1,
        account.address,
        {"from": account},
    )
    repay_tx.wait(1)
    print("Repaid!!")

def get_asset_price(price_feed_address):
    # ABI + Addr
    dai_eth_price_feed = interface.AggregatorV3Interface(price_feed_address)
    latest_price = dai_eth_price_feed.latestRoundData()[1]
    converted_latest_price = Web3.fromWei(latest_price, "ether")
    print(f"The Dai - ETH price is {converted_latest_price}")
    return float(converted_latest_price)

def get_borrowable_data(lending_pool, account):
    (
        total_collateral_eth,
        total_debt_eth,
        available_borrow_eth,
        current_liquidation_threshold,
        ltv,
        health_factor,
    ) = lending_pool.getUserAccountData(account.address)
    available_borrow_eth = Web3.fromWei(available_borrow_eth, "ether")
    total_collateral_eth = Web3.fromWei(total_collateral_eth, "ether")
    total_debt_eth = Web3.fromWei(total_debt_eth, "ether")
    print(f"You have {total_collateral_eth} worth of ETH deposited.")
    print(f"You have {total_debt_eth} worth of ETH deposited.")
    print(f"You can borrow {available_borrow_eth} worth of ETH.")
    return (float(available_borrow_eth), float(total_debt_eth))

def approve_erc20(amount, spender, erc20_address, account):
    # need abi and addr of erc20 contract
    print("Approving ERC20 Token")
    erc20 = interface.IERC20(erc20_address)
    tx = erc20.approve(spender, amount, {"from": account})
    tx.wait(1)
    print("Approved!")
    return tx

def get_lending_pool():
    # working with contract that provides location of aave lending pool (need abi + addr)
    lending_pool_addresses_provider = interface.ILendingPoolAddressesProvider(
        config["networks"][network.show_active()]["lending_pool_addresses_provider"]
    )
    # ABI
    lending_pool_address = lending_pool_addresses_provider.getLendingPool()
    # Address from interface
    lending_pool = interface.ILendingPool(lending_pool_address)
    return lending_pool


import curses
import random
from typing import Tuple, Set

def get_draw_range(
        pointer_pos: Tuple[int, int],
        map_size: Tuple[int, int],
        window_size: Tuple[int, int],
        buffer: int
    ) -> Tuple[range, range]:

    # set pointer pos
    pointer_y, pointer_x = pointer_pos

    # set window size
    win_height, win_width = window_size
    win_height += -buffer * 2
    win_width += -buffer * 2

    # set map size
    map_height, map_width = map_size

    # correct draw sizes in case of a very small map
    if win_height > map_height:
        win_height = map_height
    if win_width > map_width:
        win_width = map_width

    # set the y range begin and end
    draw_y_begin = pointer_y - win_height // 2
    draw_y_end = pointer_y + win_height // 2
    # correct for an odd numbered window size
    if win_height % 2 != 0:
        draw_y_end += 1

    # correct draw ranges
    if draw_y_begin < 0:
        draw_y_begin = 0
        draw_y_end = win_height
    if draw_y_end > map_height:
        draw_y_begin = map_height - win_height
        draw_y_end = map_height

    # do the same with the x range
    # vvv
    draw_x_begin = pointer_x - win_width // 2
    draw_x_end = pointer_x + win_width // 2
    if win_width % 2 != 0:
        draw_x_end += 1

    if draw_x_begin < 0:
        draw_x_begin = 0
        draw_x_end = win_width
    if draw_x_end > map_width:
        draw_x_begin = map_width - win_width
        draw_x_end = map_width

    return range(draw_y_begin, draw_y_end), range(draw_x_begin, draw_x_end)

def render(game,
           map_window,
           fov: bool,
           fov_entity: int|None = None,
           pointer_pos: Tuple[int, int] = (0, 0)) -> None:

    map_window.box()

    window_size_y, window_size_x = map_window.getmaxyx()

    game_map = game.game_map
    game_tile_map = game_map["map_array"]
    game_map_height = game_map["map_height"]
    game_map_width = game_map["map_width"]
    buffer = 3
    draw_y, draw_x = get_draw_range(
            pointer_pos=pointer_pos,
            map_size=(game_map_height, game_map_width),
            window_size=(window_size_y, window_size_x),
            buffer=buffer
    )

    debug_string = f"({draw_y}, {draw_x}, {game.round}, {game.pointer_pos})"
    map_window.addstr(0, 1, debug_string)
    fov_set: Set[Tuple[int, int]] = set()
    if fov and isinstance(game.pool.entities[fov_entity]["FOV"], set):
        fov_set = game.pool.entities[fov_entity]["FOV"]
    for window_y, map_y in enumerate(draw_y, buffer):
        for window_x, map_x in enumerate(draw_x, buffer):
            ent_id = None
            prio_list = {
                "map_tile": [],
                "map_object": [],
                "item": [],
                "actor": [],
            }
            for tile_id in game_tile_map[map_y][map_x]:
                for prio_item, ent_list in prio_list.items():
                    if game.pool.entities[tile_id].get(prio_item):
                        ent_list.append(tile_id)
            for prio_item, ent_list in prio_list.items():
                if ent_list:
                    ent_id = random.choice(ent_list)

            if ent_id:
                ent = game.pool.entities[ent_id]
                char_list = random.choice(ent.get("char"))
                char, fg, bg = char_list
                color = game.colors[fg][bg]
            else:
                char = None
                color = None
            if (
                fov and
                (map_y, map_x) not in fov_set and not
                game.config["debug"]["disable_fov"]
            ):
                char = None
                color = None
            if char and color:
                map_window.addstr(
                    window_y,
                    window_x,
                    char,
                    curses.color_pair(color)
                )
            if not game.pointer_bound:
                py, px = pointer_pos
                if map_y == py and map_x == px:
                    map_window.addstr(window_y, window_x, "X")
    map_window.refresh()


import numpy as np
import plotly.graph_objects as go
import numpy as np
from PIL import Image
from scipy.interpolate import interpn

img = Image.open("Earth_Diffuse_6K.jpg")
imgdata = np.asarray(img)

factor = 30

lats = np.linspace(0, np.pi, int(imgdata.shape[0]/factor)) # this is actually latitude + 90
lons = np.linspace(0, 2*np.pi, int(imgdata.shape[1]/factor))

LATS, LONS = np.meshgrid(lats, lons)

values = interpn((lats, lons), imgdata[::factor, ::factor, :], (LATS.ravel(), LONS.ravel()))

X = np.sin(LATS)*np.cos(LONS)
Y = np.sin(LATS)*np.sin(LONS)
Z = np.cos(LATS)
x = X.ravel()
y = Y.ravel()
z = Z.ravel()

fig = go.Figure(data=[go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=5,
        color=values
    )
)])

# tight layout
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
# fig.show()

fig.write_html("file.html")