import streamlit as st
import pandas as pd

hide_streamlit_style = """

            """

hide_streamlit_style = """

            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

st.markdown("""

Plataforma em Desenvolvimento pela AISIM ENGENHARIA utilizando Python

@aisimengenharia | (81) 995041310

""", unsafe_allow_html=True)

st.title('Avaliação do Potencial de Risco')
st.write('Gusmão Filho et. al., (1992)')

with st.expander('Fatores Topográficos'):
    altura_encosta = st.selectbox('Altura da encosta (m):', ['< 5', '5 - 10', '10 - 20', '20 - 30', '> 30'])
    extensao_encosta = st.selectbox('Extensão da encosta (m):', ['< 100', '100 - 250', '250 - 350', '350 - 500', '> 500'])
    declividade_encosta = st.selectbox('Declividade da encosta (%):', ['< 20', '20 - 30', '30 - 40', '40 - 50', '> 50'])
    perfil_encosta = st.selectbox('Perfil da encosta:', ['Côncavo', 'Retilíneo', 'Côncavo-Convexo', 'Convexo'])
    morfologia_encosta = st.selectbox('Morfologia da encosta:', ['Convexa', 'Retilínea', 'Sinuosa', 'Côncava'])

with st.expander('Fatores Geológicos'):
    litologia = st.selectbox('Litologia:', ['Calcário (Fm. Gramame)', 'Conglomerado (Fm. Cabo)', 'Solo Residual (Emb. Crist.)', 'Sedimento (Fm. Beberibe)', 'Sedimento (Fm. Barreiras)'])
    estrutura = st.selectbox('Estrutura:', ['Maciça', 'Mergulho Oposto', 'Subhorizontal', 'Subvertical', 'Mergulho Concordante'])
    textura = st.selectbox('Textura:', ['Areno-Argilosa', 'Areno-Siltosa', 'Arenosa/Argilosa', 'Topo arenoso','Topo argiloso'])
    evidencias_movimento = st.selectbox('Evidências de movimento:', ['Ravinamento sup. ', 'Cicatrizes', 'Voçorocas', 'Fendas', 'Surgências N.A', 'Ausentes'])

with st.expander('Fatores Ambientais'):
    vegetacao = st.selectbox('Vegetação (%):', ['100', '100 - 70', '70 - 30', '30 - 0', 'Ausente'])
    drenagem = st.selectbox('Drenagem:', ['Extensiva', 'Parcial', 'Insuficiente', 'Tópica', 'Inexistente'])
    cortes = st.selectbox('Cortes:', ['Isolados', 'Dispersos-', 'Dispersos+', 'Desordenados-', 'Desordenados+'])
    densidade_populacional = st.selectbox('Densidade populacional (hab/ha):', ['< 100', '100 - 200', '200 - 300', '300 - 500', '> 500'])
    tratamento = st.selectbox('Tratamento:', ['Extensivo', 'Parcial', 'Insuficiente', 'Tópico', 'Inexistente'])

# Dicionários para mapear as opções para os graus de risco

# Risco Topografico
risco_altura = {
    '< 5': 1,
    '5 - 10': 2,
    '10 - 20': 3,
    '20 - 30': 4,
    '> 30': 5
}

risco_extensao = {
    '< 100': 1,
    '100 - 250': 2,
    '250 - 350': 3,
    '350 - 500': 4,
    '> 500': 5
}

risco_declividade = {
    '< 20': 1,
    '20 - 30': 2,
    '30 - 40': 3,
    '40 - 50': 4,
    '> 50': 5
}

risco_perfil = {
    'Côncavo': 1,
    'Retilíneo': 2,
    'Côncavo-Convexo': 3,
    'Convexo': 4
}

risco_morfologia = {
    'Convexa': 1,
    'Retilínea': 2,
    'Sinuosa': 3,
    'Côncava': 4
}

# Risco Geológicos
risco_litologia = {
    'Calcário (Fm. Gramame)': 1,
    'Conglomerado (Fm. Cabo)': 2,
    'Solo Residual (Emb. Crist.)': 3,
    'Sedimento (Fm. Beberibe)': 4,
    'Sedimento (Fm. Barreiras)': 5
}

risco_estrutura = {
    'Maciça': 1,
    'Mergulho Oposto': 2,
    'Subhorizontal': 3,
    'Subvertical': 4,
    'Mergulho Concordante': 5
}

risco_textura = {
    'Areno-Argilosa': 1,
    'Argilo-Arenosa': 2,
    'Arenosa/Argilosa': 3,
    'Argilosa/Arenosa': 4,
    'Topo arenoso': 5,
    'Topo argiloso': 5
}

risco_evidencias = {
    'Ravinamento sup. ': 1,
    'Rav. Prof.': 2,
    'Cicatrizes': 3,
    'Voçorocas': 4,
    'Fendas': 4,
    'Surgências N.A': 5,
    'Ausentes': 5
}

# Riscos Ambientais

risco_vegetacao = {
    '100': 1,
    '100 - 70': 2,
    '70 - 30': 3,
    '30 - 0': 4,
    'Ausente': 5
}

risco_drenagem = {
    'Extensiva': 1,
    'Parcial': 2,
    'Insuficiente': 3,
    'Tópica': 4,
    'Inexistente': 5
}

risco_cortes = {
    'Isolados': 1,
    'Dispersos-': 2,
    'Dispersos+': 3,
    'Desordenados-': 4,
    'Desordenados+': 5
}

risco_densidade_populacional = {
    '< 100': 1,
    '100 - 200': 2,
    '200 - 300': 3,
    '300 - 500': 4,
    '> 500': 5
}

risco_tratamento = {
    'Extensivo': 1,
    'Parcial': 2,
    'Insuficiente': 3,
    'Tópico': 4,
    'Inexistente': 5
}

st.title('Grau e Classificação de Risco')
if st.button('Calcular Grau de Risco'):
    with st.expander('Valores dos Fatores'):
        topograficos_table = pd.DataFrame({
            'Fatores Topográficos': ['Altura da encosta', 'Extensão da encosta', 'Declividade da encosta', 'Perfil da encosta', 'Morfologia da encosta'],
            'Valor': [altura_encosta, extensao_encosta, declividade_encosta, perfil_encosta, morfologia_encosta]
        })
        st.table(topograficos_table)

        geologicos_table = pd.DataFrame({
            'Fatores Geológicos': ['Litologia', 'Estrutura', 'Textura', 'Evidências de movimento'],
            'Valor': [litologia, estrutura, textura, evidencias_movimento]
        })
        st.table(geologicos_table)

        ambientais_table = pd.DataFrame({
            'Fatores Ambientais': ['Vegetação', 'Drenagem', 'Cortes', 'Densidade populacional', 'Tratamento'],
            'Valor': [vegetacao, drenagem, cortes, densidade_populacional, tratamento]
        })
        st.table(ambientais_table)

    GR_T = (risco_altura[altura_encosta] + risco_extensao[extensao_encosta] + risco_declividade[declividade_encosta] + risco_perfil[perfil_encosta] + risco_morfologia[morfologia_encosta]) / 5
    GR_G = (risco_litologia[litologia] + risco_estrutura[estrutura] + risco_textura[textura] + risco_evidencias[evidencias_movimento]) / 4
    GR_A = (risco_vegetacao[vegetacao] + risco_drenagem[drenagem] + risco_cortes[cortes] + risco_densidade_populacional[densidade_populacional] + risco_tratamento[tratamento]) / 5

    P1 = 1  # Peso para o fator topográfico
    P2 = 2  # Peso para o fator geológico
    P3 = 3  # Peso para o fator ambiental
    GRF_ENC = (P1 * GR_T + P2 * GR_G + P3 * GR_A) / (P1 + P2 + P3)

    with st.expander('Resultado da Avaliação'):
        risk_table = pd.DataFrame({
            'Risco': ['Grau de Risco da Encosta', 'Classificação de Risco'],
            'Valor': [GRF_ENC, '']
        })

        # Classificar o grau de risco
        if GRF_ENC < 1.74:
            risco = 'Muito Baixo'
        elif 1.74 <= GRF_ENC < 2.25:
            risco = 'Baixo'
        elif 2.25 <= GRF_ENC < 2.75:
            risco = 'Médio'
        elif 2.75 <= GRF_ENC < 3.25:
            risco = 'Alto'
        else:
            risco = 'Muito Alto'

        risk_table['Valor'][1] = risco
        st.table(risk_table)


import subprocess as sp
import sys
from hashlib import sha256
from pathlib import Path

import pytest
import yaml

from qzemoji.base import AsyncEngineFactory
from qzemoji.orm import EmojiTable

pytestmark = pytest.mark.asyncio

async def test_build():
    r = sp.run(
        ["python", "script/build.py", "-o", "tmp/build.db"],
        executable=sys.executable,
        capture_output=True,
    )
    assert not r.stderr
    h1 = r.stdout.decode().rstrip()

    with open("data/emoji.yml", encoding="utf8") as f:
        d = yaml.safe_load(f)
    s = ";".join(f"{k}={d[k]}" for k in sorted(d))
    h2 = sha256(s.encode("utf8")).hexdigest().lower()
    assert h1 == h2

    out = Path(f"tmp/build.db")

    async with AsyncEngineFactory.sqlite3(out) as engine:
        built = EmojiTable(engine)
        assert not await built.is_corrupt()
        assert h2 == await built.sha256()

    out.unlink()


import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import (binary_erosion, binary_dilation, binary_closing, binary_opening)
from skimage.measure import label

stars=np.load("stars.npy")

star_mask=np.array([[1, 0, 0, 0, 1],
                    [0, 1, 0, 1, 0],
                    [0, 0, 1, 0, 0],
                    [0, 1, 0, 1, 0],
                    [1, 0, 0, 0, 1]])
cross_mask=np.array([[0, 0, 1, 0, 0],
                    [0, 0, 1, 0, 0],
                    [1, 1, 1, 1, 1],
                    [0, 0, 1, 0, 0],
                    [0, 0, 1, 0, 0]])
stars_new=(binary_erosion(stars,star_mask))
labeled=label(stars_new)
num_components = np.max(labeled)
print(f"количество крестов {num_components}")
cross_new=(binary_erosion(stars,cross_mask))
labeled1=label(cross_new)
num_components1 = np.max(labeled1)
print(f"количество плюсов {num_components1}")
print((f"количество звездочек {num_components+num_components1}"))
plt.imshow (stars)
plt.show()


import shutil
import sys
import os
import subprocess
import numpy as np
import cv2

def create_video(video_folder, format, out_file):
    file_and_format = f'{video_folder}/{format}'
    frames = 500
    for i in range(1, 500):
        print(file_and_format)
        frame_num = file_and_format.replace('*', '{:04}'.format(i))
        prev_frame_num = file_and_format.replace('*', '{:04}'.format(i-1))
        print(prev_frame_num)
        if not os.path.isfile(frame_num):
            shutil.copy(prev_frame_num, frame_num)

    frame_rate = 10
    subprocess.call(['ffmpeg',
                    '-framerate', str(frame_rate),
                    '-i',
                     '{}/{}'.format(video_folder, format.replace('*', '%04d')),
                     '-pix_fmt', 'yuv420p',
                     '{}.mp4'.format(out_file)])

def merge_frames(in_formats, nframes, out_format):
    for i in range(0, nframes):
        curr_imgs = []
        for in_format in in_formats:

            frame_num = in_format.replace('*', '{:04}'.format(i))
            prev_frame_num = in_format.replace('*', '{:04}'.format(i - 1))

            if not os.path.isfile(frame_num):
                shutil.copy(prev_frame_num, frame_num)
            curr_imgs.append(cv2.imread(frame_num))
        img_join = np.concatenate(curr_imgs, 1)
        cv2.imwrite(out_format.replace('*', '{:04}'.format(i)), img_join)

if __name__ == '__main__':
    # merge_frames(['/Users/xavierpuig/Desktop/test_videos/bob_with_info/action_*.png',
    #               '/Users/xavierpuig/Desktop/test_videos/alice_with_info/action_*.png'], 570,
    #              '/Users/xavierpuig/Desktop/test_videos/merged2_info/Action_*_normal.png')
    # create_video('/Users/xavierpuig/Desktop/test_videos/merged2_info/',
    #              'Action_*_normal.png',
    #              '/Users/xavierpuig/Desktop/test_videos/alice_and_bob_info.mp4')
    create_video('/Users/xavierpuig/Desktop/test_videos/alice_with_info_belief/',
                 'action_*.png',
                 '/Users/xavierpuig/Desktop/test_videos/alice_info_goals.mp4')
    #create_video(sys.argv[1], sys.argv[2], sys.argv[3])


# -*- coding: utf-8 -*-
"""
Created on Wed Jan 19 11:49:20 2022

@author: robyates
"""

"""
mass_checks.py
  ;Script to check:
  ;a) how many NaNs and negatives there are in key mass properties,
  ;b) if sub-component masses add up to component masses properly (e.g. SUM(ColdGas_elements) = ColdGas, etc)
  ;
  ;Rob Yates 19-01-2022
  ;
  ;08-11-22: This version was adapted for use at the L-Galaxies workshop 2022
  ;
"""

#Base packages:
import numpy as np
import matplotlib.pyplot as plt

#################
def mass_checks(G_lgal, COSMOLOGY, SIMULATION, FILE_TYPE, STRUCT_TYPE, MODEL, VERSION, SAMPLE_TYPE, plots=None) :
    #Properties to check:
    prop_name = ['ColdGas','BulgeMass','DiskMass', \
                 'MetalsBulgeMass_SNII','MetalsBulgeMass_SNIa','MetalsBulgeMass_AGB', \
                 'MetalsDiskMass_SNII','MetalsDiskMass_SNIa','MetalsDiskMass_AGB', \
                 'MetalsDiskMassRings_SNII','MetalsDiskMassRings_SNIa','MetalsDiskMassRings_AGB', \
                 'ColdGas_elements']
    prop = [G_lgal['ColdGas'],G_lgal['BulgeMass'],G_lgal['DiskMass'], \
            G_lgal['MetalsBulgeMass'][:,0],G_lgal['MetalsBulgeMass'][:,1],G_lgal['MetalsBulgeMass'][:,2], \
            G_lgal['MetalsDiskMass'][:,0],G_lgal['MetalsDiskMass'][:,1],G_lgal['MetalsDiskMass'][:,2], \
            G_lgal['MetalsDiskMassRings'][:,:,0],G_lgal['MetalsDiskMassRings'][:,:,1],G_lgal['MetalsDiskMassRings'][:,:,2], \
            G_lgal['ColdGas_elements']]

    ##############
    #Check for NaNs:
    print("\n------------------")
    print("Check for NaNs:")
    print("------------------")
    for ii in range(len(prop)) :
        nans = np.count_nonzero(np.isnan(prop[ii]))
        if nans == 0 :
            print(prop_name[ii], ":", nans, "/", prop[ii].size)
        else :
            print("*************************************")
            print("WARNING: NaNs in", prop_name[ii], "=", nans, "/", prop[ii].size)
            print("*************************************")

    ##############
    #Check for negatives:
    print("\n------------------")
    print("Check for < 0:")
    print("------------------")
    for ii in range(len(prop)) :
        negs = np.sum(prop[ii] < 0.)
        if negs == 0 :
            print(prop_name[ii], ":", negs, "/", prop[ii].size)
        else :
            var = prop[ii]
            varneg = var[var < 0.]
            print("*************************************")
            print("WARNING: Negatives in", prop_name[ii], ":", negs, "/", prop[ii].size)
            print("Min =", np.min(varneg), " | ", "Max =", np.max(varneg))
            print("*************************************")

    ##############
    #Check for sums:
    print("\n------------------")
    print("Check sub-component masses:")
    print("------------------")
    #Mass sum properties:
    prop1_name = ['ColdGas','DiskMass','MetalsColdGas','MetalsDiskMass','MetalsBulgeMass', \
                  'ColdGas','DiskMass','BulgeMass','HotGas','HaloStars', \
                  'MetalsColdGas','MetalsDiskMass','MetalsBulgeMass', \
                  'MetalsHotGas','MetalsHaloStars', \
                  'Gal number', 'Gal number', 'Gal number', \
                  'Gal number', 'Gal number', 'Gal number', \
                  'Gal number', 'Gal number', 'Gal number']
    prop1 = [G_lgal['ColdGas'],G_lgal['DiskMass'],np.nansum(G_lgal['MetalsColdGas'], axis=1),np.nansum(G_lgal['MetalsDiskMass'], axis=1),np.nansum(G_lgal['MetalsBulgeMass'], axis=1), \
             G_lgal['ColdGas'],G_lgal['DiskMass'],G_lgal['BulgeMass'],G_lgal['HotGas'],G_lgal['ICM'], \
             np.nansum(G_lgal['MetalsColdGas'], axis=1),np.nansum(G_lgal['MetalsDiskMass'], axis=1),np.nansum(G_lgal['MetalsBulgeMass'], axis=1), \
             np.nansum(G_lgal['MetalsHotGas'], axis=1),np.nansum(G_lgal['MetalsICM'], axis=1), \
             np.arange(len(G_lgal['DiskMass'])), np.arange(len(G_lgal['DiskMass'])), np.arange(len(G_lgal['DiskMass'])), \
             np.arange(len(G_lgal['BulgeMass'])), np.arange(len(G_lgal['BulgeMass'])), np.arange(len(G_lgal['BulgeMass'])), \
             np.arange(len(G_lgal['ICM'])), np.arange(len(G_lgal['ICM'])), np.arange(len(G_lgal['ICM']))]
    prop2_name = ['ColdGasRings','DiskMassRings','MetalsColdGasRings','MetalsDiskMassRings', 'MetalsBulgeMassRings', \
                  'ColdGasElements','DiskMassElements','BulgeMassElements','HotGasElements','HaloStarElements', \
                  'MetalsColdGasElements','MetalsDiskMassElements','MetalsBulgeMassElements', \
                  'MetalsHotGasElements','MetalsHaloStarsElements', \
                  'MetalsDiskMassSNII','MetalsDiskMassSNIa','MetalsDiskMassAGB', \
                  'MetalsBulgeMassSNII','MetalsBulgeMassSNIa','MetalsBulgeMassAGB', \
                  'MetalsHaloStarsSNII','MetalsHaloStarsSNIa','MetalsHaloStarsAGB']
    prop2 = [G_lgal['ColdGasRings'],G_lgal['DiskMassRings'],np.nansum(G_lgal['MetalsColdGasRings'], axis=2),np.nansum(G_lgal['MetalsDiskMassRings'], axis=2),np.nansum(G_lgal['MetalsBulgeMassRings'], axis=2), \
             G_lgal['ColdGas_elements'],G_lgal['DiskMass_elements'],G_lgal['BulgeMass_elements'],G_lgal['HotGas_elements'],G_lgal['ICM_elements'],\
             G_lgal['ColdGas_elements'][:,2:],G_lgal['DiskMass_elements'][:,2:],G_lgal['BulgeMass_elements'][:,2:], \
             G_lgal['HotGas_elements'][:,2:],G_lgal['ICM_elements'][:,2:], \
             G_lgal['MetalsDiskMass'][:,0],G_lgal['MetalsDiskMass'][:,1],G_lgal['MetalsDiskMass'][:,2], \
             G_lgal['MetalsBulgeMass'][:,0],G_lgal['MetalsBulgeMass'][:,1],G_lgal['MetalsBulgeMass'][:,2], \
             G_lgal['MetalsICM'][:,0],G_lgal['MetalsICM'][:,1],G_lgal['MetalsICM'][:,2]]

    if plots :
        fig = plt.figure(figsize=(15,38)) #15,25
        plt.rc('font', family='serif', size='14.0')
        rowno = 9 #6
        colno = 3

    for ii in range(len(prop1)) :
        theprop1 = prop1[ii]
        if (prop1_name[ii] != 'Gal number')         :
            theprop2 = np.nansum(prop2[ii], axis=1)
            if (ii == 7) | (ii == 8) : #If prop1 or prop2 include a multiple of G_lgal['H2fraction']
                ww = np.where((theprop1 > 100.0) & (theprop2 > 100.0) & (G_lgal['H2fraction'] > 1.e-04))
                theprop3 = G_lgal['H2fraction'][ww]
            else :
                ww = np.where((theprop1 > 100.0) & (theprop2 > 100.0)) #Only consider galaxies with whole-component mass of >100 Msun
            theprop1 = theprop1[ww]
            theprop2 = theprop2[ww]

            if np.allclose(theprop1, theprop2) :
                print("SUM(", prop2_name[ii], ") !=", prop1_name[ii], ": 0 /", prop1[ii].size)#
            else :
                num = np.count_nonzero(theprop1 != theprop2)
                vardiff = theprop1 - theprop2
                vardiff = abs(vardiff[vardiff != 0.0])
                vardifffrac = (theprop1 - theprop2) / theprop1
                vardifffrac = abs(vardifffrac[vardifffrac != 0.0])
                print("*************************************")
                print("WARNING: SUM(", prop2_name[ii], ") !=", prop1_name[ii], ":", num, "/", theprop1.size)
                print("Min diff =", np.min(vardiff), " | ", "Mean diff =", np.mean(vardiff), " | ", "Max diff =", np.max(vardiff))
                print("Min diff frac =", np.min(vardifffrac), " | ", "Mean diff frac =", np.mean(vardifffrac), " | ", "Max diff frac =", np.max(vardifffrac))
                print("*************************************")
        else :
            theprop2 = prop2[ii]

        if plots :
            #Set-up plot:
            plt.subplot(rowno, colno, ii+1)
            if (prop1_name[ii] != 'Gal number') :
                xlimits = np.array([0.0,np.max([np.max(theprop1),np.max(theprop2)])])
                ylimits = np.array([0.0,np.max([np.max(theprop1),np.max(theprop2)])])
                plt.plot(xlimits, ylimits, linestyle='--', color='grey')
            else :
                xlimits = np.array([np.min(theprop1),np.max(theprop1)])
                ylimits = np.array([np.min(theprop2),np.max(theprop2)])
                plt.plot(xlimits, np.array([0.0,0.0]), linestyle='--', color='grey')
            #fig = plt.figure(figsize=(5,5))
            plt.xlabel(prop1_name[ii], fontsize=12)
            plt.ylabel(prop2_name[ii], fontsize=12)
            plt.xlim(xlimits)
            plt.ylim(ylimits)
            plt.tick_params(direction="in", top=True, bottom=True, left=True, right=True)
            plt.scatter(theprop1, theprop2, marker='o')
            if ii == 0 :
                plt.text(0.25, 0.92, ['Mass checks', 'z=0', str(len(G_lgal))+' gals', COSMOLOGY, SIMULATION, FILE_TYPE, MODEL, VERSION, SAMPLE_TYPE], transform=fig.transFigure, horizontalalignment='left', color='blue')

    print('Mass checks done')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 0496_Next_Greater_Element_I.py
# @Author: Xuewen Lei
# @Date  : 2021/9/7
# @Desc  :
from typing import List

class Solution:
    def nextGreaterElement(self, findNums: List[int], nums: List[int]) -> List[int]:
        st, d, ans = [], {}, []
        for v in nums:
            while st and st[-1] < v:
                d[st.pop()] = v
            st.append(v)
        for x in findNums:
            ans.append(d.get(x, -1))
        return ans


import io
import json

from avro.datafile import DataFileReader
from avro.io import DatumReader

def lambda_handler(event, context):
    try:
        return {
            "success": True,
            "num_records": event["num_records"],
            "results": [
                list(
                    json.dumps(
                        list(
                            DataFileReader(
                                io.BytesIO(bytes.fromhex(record[0])), DatumReader()
                            )
                        )
                    )
                )
                for record in event["arguments"]
            ],
        }
    except Exception as e:
        return {
            "success": False,
            "error_msg": f"Error processing Lambda event. Error: {e}",
        }


import requests
import codecs
import threading
import re
import os
from lxml import etree
from tkinter import *
import tkinter.messagebox
import time
from urllib import parse
from tkinter.filedialog import askdirectory
s=[]
t=[]
q=[]
n=1
name="柯洁"
path_='C:'
win_b,win_w=0,0
W_=0
B_=0
c_p=[]
head={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36"}
def selectPath():
    global path_
    path_ = askdirectory()
    path.set(path_)
def get_url(p):#构造棋谱地址
    global q,c_p
    q=[]
    page=p
    c_p=[]
    name_=parse.quote(name.encode('gbk'),'gbk')
    for i in range(page):
        time.sleep(1)
        url='http://duiyi.sina.com.cn/gibo/new_gibo.asp?cur_page='+str(i)+'&key=1&keyword='+name_
        html=requests.get(url,headers=head)
        html.encoding="utf-8"
        ele=etree.HTML(html.text)
        st=ele.xpath("/html//tr[@class='body_text1']/td[2]/a/@href")
        page_=ele.xpath('/html//font[@color="#CC3333"]')
        goahead=ele.xpath('/html//font[@color="#000000"]/a/@href')
        for i in st:
            q.append(i.split('\'')[1])
    print(len(q))
    #exit()
class Request:
    id=0
    def __init__(self,name):
        self.name=name
        Request.id+=1
        self.show_information()
        self.head=head={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36"}
        self.urls=[]
        self.usedurl=[]
        self.response=[]
        self.data=[]
    def show_information(self):
        print("我是 %s 爬虫\n编号%s"%(self.name,self.id))
    def requests(self,mathod,url,data=None,params=None,encode='utf-8'):
        for i in url:
            if i not in self.usedurl:
                if mathod=='get':
                    html=requests.get(i,headers=self.head,params=params)
                    html.encoding=encode
                    self.response.append(html)
                else:
                    html=requests.post(i,headers=self.head,data=data)
                    html.encoding=encode
                    self.response.append(html)
                self.usedurl.append(i)
    def multithreads_requests(self,mathod,urls,k,data=None,params=None,encode='utf-8'):#多线程发送请求
        threads = []#存放线程的数组，相当于线程池
        p=0
        k=k#间隔
        while p+k<=len(urls):
            thread =threading.Thread(target=self.requests,args=(mathod,urls[p:p+k],data,params,encode))#指定线程i的执行函数为myThread
            p+=k
            threads.append(thread)#先讲这个线程放到线程threads
        if p!=len(urls):
            thread =threading.Thread(target=self.requests,args=(mathod,urls[p:len(urls)],data,params,encode))
            threads.append(thread)
        for t in threads:#让线程池中的所有数组开始
            t.start()
        for t in threads:#让线程池中的所有数组开始
            t.join()
    def search(self,mode='/html',mathod='xpath'):
        if mathod=='xpath':
            for i in self.response:
                txt=etree.HTML(i.text)
                self.data.append(txt.xpath(mode))
def change_schedule(now_schedule,all_schedule):
    canvas.coords(fill_rec, (5, 5, 6 + (now_schedule/all_schedule)*100, 25))
    master.update()
    x.set(str(round(now_schedule/all_schedule*100,2)) + '%')
    if round(now_schedule/all_schedule*100,2) == 100.00:
        x.set("完成")
def total_page(p):#计算page
    page=p
    name_=parse.quote(name.encode('gbk'),'gbk')
    url='http://duiyi.sina.com.cn/gibo/new_gibo.asp?cur_page='+page+'&key=1&keyword='+name_
    html=requests.get(url,headers=head)
    html.encoding="utf-8"
    ele=etree.HTML(html.text)
    st=ele.xpath("/html//tr[@class='body_text1']/td[2]/a/@href")
    page_=ele.xpath('/html//font[@color="#CC3333"]')
    goahead=ele.xpath('/html//font[@color="#000000"]/a/@href')
    try:
        goahead=re.findall('page=(.+?)&',goahead[-1])[0]#提取结果
    except:
        goahead=0
        pass
    c_p.append(len(page_))
    if int(goahead)>=len(page_) and len(page_)>9:
        total_page(goahead)
    print(sum(c_p))
def show_number():
    global name,c_p
    c_p=[]
    name=e1.get()
    if name=='':
        name='柯洁'
    total_page('0')
    tkinter.messagebox.showinfo('提示',name+'共有'+str(sum(c_p))+'页棋谱，请输入下载页数并点击下载棋谱')

def download2():
    # url='http://duiyi.sina.com.cn/qipu/new_gibo.asp'
    # data={'key': '2','keyword':'中国围甲22轮龙元明城杭州-浙江昆仑'}
    # name=e4.get()
    # print(name,'-----------------')
    # data.update({'keyword':name})
    # name_=parse.quote(name.encode('gbk'),'gbk')
    # url='http://duiyi.sina.com.cn/gibo/new_gibo.asp?key=2&keyword='+name_
    # h=requests.get(url,headers=head)
    # h.encoding='gbk'
    # ele=etree.HTML(h.text)
    # st=ele.xpath("/html//tr[@class='body_text1']/td[2]/a/@href")
    # sgfurls=[]
    # for i in st:
        # d=re.findall('(http.+?sgf)',i)
        # if  len(d)>0:
            # sgfurls.append(d[0])
    # t=0
    # for i in sgfurls:
        # #time.sleep(1)
        # html=requests.get(i,headers=head)
        # html.encoding="gbk"
        # st=str(html.text)
        # t1=re.findall('PB\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        # if len(t1)==0:
            # t1=['Erro!']
        # t1[0].replace('//','&')
        # t2=re.findall('PW\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        # if len(t2)==0:
            # t2=['Erro!']
        # t2[0].replace('//','&')
        # t3=re.findall('RD\[(.+?)\]',st)#提取日期
        # if len(t3)==0:
            # t3=['Erro!']
        # t4=re.findall('RE\[(.+?)\]',st)#提取结果
        # if len(t4)==0:
            # t4=['Erro!']
        # print(t1,t2,t3,t4)
        # if not os.path.exists(path_+'//sgfs'):
            # os.mkdir(path_+'//sgfs/')
        # if not os.path.exists(path_+'//sgfs/'+name):
            # os.mkdir(path_+'//sgfs/'+name)
        # try:
            # f=codecs.open(path_+'//sgfs//'+name+'//'+str(t3[0])+''+str(t1[0])+''+'VS'+''+str(t2[0])+''+'.sgf',"w",'utf-8')
        # except:
            # f=codecs.open(path_+'//sgfs//'+name+'//'+str(t3[0])+'.sgf',"w",'utf-8')
        # f.write(st)
        # f.close()
        # change_schedule(t,len(urls))
        # t+=1
    offset=0
    sgfurls=[]
    head={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36"}
    name=e4.get()
    name_=parse.quote(name.encode('gbk'),'gbk')
    if not os.path.exists(path_+'//sgfs'):
        os.mkdir(path_+'//sgfs/')
    if not os.path.exists(path_+'//sgfs/'+'新浪围棋'):
        os.mkdir(path_+'//sgfs/'+'新浪围棋')
    while True:
        print('---------下载第',offset,'页----------')
        url='http://duiyi.sina.com.cn/gibo/new_gibo.asp?cur_page='+str(offset)+'&key=2&keyword='+name_
        h=requests.get(url,headers=head)
        offset+=1
        if h.status_code==500:
            break
        ele=etree.HTML(h.text)
        st=ele.xpath("/html//tr[@class='body_text1']/td[2]/a/@href")

        for i in st:
            d=re.findall('(http.+?sgf)',i)
            if  len(d)>0:
                sgfurls.append(d[0])
    a=Request('新浪围棋')
    a.multithreads_requests('get',sgfurls,10)
    for j in a.response:
        j.encoding='gbk'
        st=j.text
        t1=re.findall('PB\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        t2=re.findall('PW\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        t3=re.findall('DT\[(.+?)\]',st)#用正则表达式提取比赛时间信息
        t4=re.findall('TE\[(.+?)\]',st)#用正则表达式提取比赛场次信息
        t5=re.findall('RE\[(.+?)\]',st)#用正则表达式提取比赛结果信息
        if t1==[]:
            t1.append('unknown')
        if t2==[]:
            t2.append('unknown')
        if t3==[]:
            t3=re.findall('RD\[(.+?)\]',st)
        if t3==[]:
            t3.append('unknown')
        if t4==[]:
            t4=re.findall('EV\[(.+?)\]',st)

        if t4==[]:
            t4=re.findall('GN\[(.+?)\]',st)
        if t4==[]:
            t4=re.findall('C\[(.+?)\]',st)
        if t4==[]:
            t4.append('未知比赛')
        if t5==[]:
            t5.append('结果未知')
        rr=str(t3[0].replace('/','-'))+' '+str(t4[0].replace('/',''))+' '+str(t1[0].replace('/','-'))+''+'VS'+''+str(t2[0].replace('/','-'))+' '+str(t5[0].replace('/','%'))+''+'.sgf'
        print(rr)
        f=codecs.open(path_+'/sgfs/'+'新浪围棋'+'/'+rr,"w",'utf-8')
        f.write(st)
        f.close()
    tkinter.messagebox.showinfo('提示','已完成'+str(offset)+'页棋谱的下载，请前往'+path_+'//sgfs文件夹查看！')
def download():#根据棋谱地址提取棋谱
    global name,W_,B_,path_,win_w,win_b
    global name,n
    W_,B_,win_w,win_b=0,0,0,0
    name=e1.get()
    n=int(e2.get())
    get_url(n)
    print(q)
    t=0
    if not os.path.exists(path_+'//sgfs'):
        os.mkdir(path_+'//sgfs/')
    if not os.path.exists(path_+'//sgfs/'+'新浪围棋'):
        os.mkdir(path_+'//sgfs/'+'新浪围棋')
    # for i in q:
        # #time.sleep(1)
        # html=requests.get(i,headers=head)
        # html.encoding="gbk"
        # st=str(html.text)
        # t1=re.findall('PB\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        # if len(t1)==0:
            # t1=['Erro!']
        # t1[0].replace('//','&')
        # t2=re.findall('PW\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        # if len(t2)==0:
            # t2=['Erro!']
        # t2[0].replace('//','&')
        # t3=re.findall('RD\[(.+?)\]',st)#提取日期
        # if len(t3)==0:
            # t3=['Erro!']
        # t4=re.findall('RE\[(.+?)\]',st)#提取结果
        # if len(t4)==0:
            # t4=['Erro!']
        # print(t1,t2,t3,t4)
        # if t1[0]==name and t4[0][0]=='黑':
            # win_b+=1
        # elif t2[0]==name and t4[0][0]=='白':
            # win_w+=1
        # if t2[0]==name:
            # W_+=1
        # elif t1[0]==name:
            # B_+=1
        # if not os.path.exists(path_+'//sgfs'):
            # os.mkdir(path_+'//sgfs/')
        # if not os.path.exists(path_+'//sgfs/'+name):
            # os.mkdir(path_+'//sgfs/'+name)
        # try:
            # f=codecs.open(path_+'//sgfs//'+name+'//'+str(t3[0])+''+str(t1[0])+''+'VS'+''+str(t2[0])+''+'.sgf',"w",'utf-8')
        # except:
            # f=codecs.open(path_+'//sgfs//'+name+'//'+str(t3[0])+'.sgf',"w",'utf-8')
        # f.write(st)
        # f.close()
        # change_schedule(t,len(q))
        # t+=1
    a=Request('新浪围棋')
    a.multithreads_requests('get',q,10)
    for j in a.response:
        j.encoding='gbk'
        st=j.text
        t1=re.findall('PB\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        t2=re.findall('PW\[(.+?)\]',st)#用正则表达式提取棋手名字信息
        t3=re.findall('DT\[(.+?)\]',st)#用正则表达式提取比赛时间信息
        t4=re.findall('TE\[(.+?)\]',st)#用正则表达式提取比赛场次信息
        t5=re.findall('RE\[(.+?)\]',st)#用正则表达式提取比赛结果信息
        if t1==[]:
            t1.append('unknown')
        if t2==[]:
            t2.append('unknown')
        if t3==[]:
            t3=re.findall('RD\[(.+?)\]',st)
        if t3==[]:
            t3.append('unknown')
        if t4==[]:
            t4=re.findall('EV\[(.+?)\]',st)

        if t4==[]:
            t4=re.findall('GN\[(.+?)\]',st)
        if t4==[]:
            t4=re.findall('C\[(.+?)\]',st)
        if t4==[]:
            t4.append('未知比赛')
        if t5==[]:
            t5.append('结果未知')
        rr=str(t3[0].replace('/','-'))+' '+str(t4[0].replace('/',''))+' '+str(t1[0].replace('/','-'))+''+'VS'+''+str(t2[0].replace('/','-'))+' '+str(t5[0].replace('/','%'))+''+'.sgf'
        print(rr)
        f=codecs.open(path_+'/sgfs/'+'新浪围棋'+'/'+rr,"w",'utf-8')
        f.write(st)
        f.close()
    tkinter.messagebox.showinfo('提示','已完成'+str(len(q))+'张棋谱的下载，请前往'+path_+'//sgfs文件夹查看！')

def show_information():
    global name,W_,B_
    t=round((win_b+win_w)*100/len(q),4)
    b=round(win_b*100/B_,2)
    w=round(win_w*100/W_,2)
    print(W_+B_,len(q))
    tkinter.messagebox.showinfo('统计'+str(W_+B_)+'局棋手信息',name+'总胜率'+str(t)+' %'+'\n执黑胜率'+str(b)+' %'+'\n执白胜率'+str(w)+' %')

if "__name__=__main__":
    master = Tk()
    master.title('新浪棋谱下载器  @幻影')
    Label(master, text="请输入棋手名字：").grid(row=0)
    Label(master, text="请输入下载页数：").grid(row=1)
    Label(master, text="请输入比赛名字：").grid(row=2)
    e1 = Entry(master)
    e2 = Entry(master)
    e4 = Entry(master)
    path = StringVar()
    e3=Entry(master, textvariable = path).grid(row =3, column =1)
    Button(master, text = "路径选择", command = selectPath).grid(row =3, column=0)
    e1.grid(row=0, column=1)
    e2.grid(row=1, column=1)
    e4.grid(row=2, column=1)
    Button(master, text='棋手信息', command=show_information).grid(row=5, column=2, sticky=W, pady=4)
    Button(master, text='下载棋手', command=download).grid(row=4, column=1, sticky=W, pady=4)
    Button(master, text='下载比赛', command=download2).grid(row=4, column=2, sticky=W, pady=4)
    Button(master, text='查询总页数', command=show_number).grid(row=4, column=0, sticky=W, pady=4)
    frame = Frame(master).grid(row =2,column =1)#使用时将框架根据情况选择新的位置
    canvas = Canvas(frame,width = 120,height = 30,bg = "white")
    canvas.grid(row =6,column =1)
    x = StringVar()
    #进度条以及完成程度
    out_rec = canvas.create_rectangle(5,5,105,25,outline = "blue",width = 1)
    fill_rec = canvas.create_rectangle(5,5,5,25,outline = "",width = 0,fill = "blue")
    Label(frame,textvariable = x).grid(row =2,column =2)
    mainloop()


'''
--------------------------------------------------------------------------
Python Runfile
--------------------------------------------------------------------------
License:
Copyright 2023 - Connor Kelly

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------
'''

from PIL import Image, ImageDraw, ImageFont

class Cursor():
    # class variables
    name = None
    cursor_type = None
    index = None
    target = None
    image = None
    limit = None
    shift = None
    target_image = None
    target_list = None
    selection_layer = None
    spacing = 2
    font = None
    position = (11, 40)

    def __init__(self, name="unnamed", cursor_type="None", index=0, target_list = [], target="None") -> None:
        self.name = name
        self.cursor_type = cursor_type
        self.index = index
        self.shift = index
        self.target_list = target_list
        self.target = None
        self.limit = 6
        self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 18)

        # initialize the selection_layer to be a new transparent image.
        # self.image = Image.new("RGBA", (320,240), (0,0,0,0))
        # self.target_image = Image.new("RGBA", (320,240), (0,0,0,0))
        # self.selection_layer = Image.new("RGBA", (320,240), (0,0,0,0))
        self.image = None
        self.target_image = None
        self.selection_layer = None
        self.draw_target_list()
        self.draw_selection_box()

    def cursor_up(self):
        print("cursor_up")
        self.index -= 1

        # check if we need to shift
        if (self.index < self.shift):
            self.shift_up()

        # redraw the selection box to index - shift
        self.draw_selection_box()

        return self.get_image()

    def cursor_dn(self):
        print("cursor_dn")
        self.index += 1

        # check if we need to shift
        if (self.index > self.shift + self.limit):
            self.shift_dn()

        # redraw the selection box to index - shift
        self.draw_selection_box()

        return self.get_image()

    def shift_up(self): # shifts the target_list upward
        self.shift -= 1
        # redraw the target_list
        self.draw_target_list()

    def shift_dn(self): # shifts the target_list downward
        self.shift += 1
        # redraw the target_list
        self.draw_target_list()

    def get_image(self):
        self.image = Image.new("RGBA", (320,240), (0,0,0,0))
        # self.image.paste(self.selection_layer, (0,0))
        # self.image.paste(self.target_image, (0,0))
        self.image = Image.alpha_composite(self.selection_layer, self.target_image)
        # self.image.show()
        return self.image

    def draw_target_list(self):
        if (self.target_list == []):
            self.target_list = ["Trackname"] * self.limit

        # reset the image
        self.target_image =  Image.new("RGBA", (320,240), (0,0,0,0))

        target_string = "\n".join(self.target_list)

        # draw the multiline target_list
        draw = ImageDraw.Draw(self.target_image)

        draw.text(self.position, target_string, font = self.font, spacing = self.spacing)

        return self.target_image
        # self.target_image.show()

    def draw_selection_box(self):
        self.selection_layer = Image.new("RGBA", (320,240), (0,0,0,0))

        # draw the multiline target_list
        draw = ImageDraw.Draw(self.selection_layer)

        slot = self.index - self.shift
        # x = (0, 320)

        pos = [0, (slot * 20) + 40, 320, ((slot + 1) * 20) + 40]
        print("pos: %s"%(str(pos)))
        #
        draw.rectangle(pos, fill = (100,100,100,255), outline=(0,0,0,0), width =1)
        # self.selection_layer.show()

        return self.selection_layer


import json
import requests
import os
import os.path
import sys

if len(sys.argv) != 2:
    print('Usage: '+sys.argv[0]+' INTXT')
    exit()
pdb_list_file = sys.argv[1]

API_URL='http://www.ebi.ac.uk/pdbe/api/mappings/uniprot/'

with open(pdb_list_file, 'r') as f:
    pdb_list = [line.strip().lower() for line in f.readlines()]

for pdb in pdb_list:
    r = json.loads(requests.get(API_URL + pdb).text)
    uniprot = r[pdb]['UniProt']
    if len(uniprot) == 0:
        print(f'WARNING: {pdb} maps to no UniProtIDs', file=sys.stderr)
    elif len(uniprot) > 1:
        print(f'WARNING: {pdb} maps to multiple UniProtIDs (' + ', '.join(uniprot.keys()) + ')', file=sys.stderr)
    for uni, annot in uniprot.items():
        print(pdb, uni, annot['identifier'], annot['name'], sep='\t')


import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

plt.style.use('seaborn-deep')
plt.rcParams['font.family'] = 'Arial'
plt.rcParams['font.size'] = 32
plt.rcParams['axes.labelsize'] = 32
plt.rcParams['axes.titlesize'] = 32
plt.rcParams['legend.fontsize'] = 32
plt.rcParams["figure.figsize"] = (9,5)
plt.rcParams['svg.fonttype'] = 'none'

SAVE_ROOT = "figs/"

def main():
    os.makedirs( SAVE_ROOT ,exist_ok=True )

    # ['sim_BBA: bitrate: 1.2% rebuf: 0.0585' ,'sim_RobustMPC: bitrate: 1.22% rebuf: 0.033' ,
    #  'sim_udr_1: bitrate: 1.2% rebuf: 0.0338' ,'sim_udr_2: bitrate: 1.04% rebuf: 0.0195' ,
    #  'sim_udr_3: bitrate: 1.1% rebuf: 0.0237' ,'sim_adr: bitrate: 1.11% rebuf: 0.0148']

    plt.rcParams['font.size'] = 32
    plt.rcParams['axes.labelsize'] = 32
    plt.rcParams['axes.titlesize'] = 32
    plt.rcParams['legend.fontsize'] = 32
    fig1, ax1 = plt.subplots(figsize=(9, 5))

    fcc_genet_bitrate, fcc_genet_rebuf = 1.17, 0.014

    fcc_udr1_bitrate ,fcc_udr1_rebuf = 1.2 ,0.034
    fcc_udr2_bitrate ,fcc_udr2_rebuf = 1.04 ,0.019
    fcc_udr3_bitrate ,fcc_udr3_rebuf = 1.1 ,0.023

    fcc_bba_bitrate ,fcc_bba_rebuf = 1.2 , 0.058
    fcc_mpc_bitrate ,fcc_mpc_rebuf = 1.22 ,0.033
    fcc_oboe_bitrate ,fcc_oboe_rebuf = 1.16 , 0.021

    msize = 200

    ax1.scatter( [fcc_bba_rebuf],[fcc_bba_bitrate] ,marker='d' ,color='C0' ,s=msize ,label='BBA' )
    ax1.scatter( [fcc_mpc_rebuf] ,[fcc_mpc_bitrate] ,marker='>' ,color='C1' ,s=msize ,label='MPC' )
    ax1.scatter( [fcc_oboe_rebuf] ,[fcc_oboe_bitrate] ,marker='v' ,color='darkorange' ,s=msize ,label='Oboe' )
    ax1.scatter( [fcc_udr1_rebuf] , [fcc_udr1_bitrate] ,marker='^' ,color='C3' ,s=msize ,label='RL1' )
    ax1.scatter( [fcc_udr2_rebuf] ,[fcc_udr2_bitrate] ,marker='<' ,color='C4' ,s=msize ,label='RL2' )
    ax1.scatter( [fcc_udr3_rebuf] , [fcc_udr3_bitrate] , marker='p' ,color='C5' ,s=msize ,label='RL3' )
    ax1.scatter( [fcc_genet_rebuf] ,[fcc_genet_bitrate] ,s=msize ,color='C2' ,label='Genet' )

    ax1.annotate('BBA', ( fcc_bba_rebuf, fcc_bba_bitrate-0.03))
    ax1.annotate('MPC', ( fcc_mpc_rebuf, fcc_mpc_bitrate-0.01))
    ax1.annotate('Oboe', (fcc_oboe_rebuf+0.004, fcc_oboe_bitrate-0.025))
    ax1.annotate('RL1', (fcc_udr1_rebuf+0.002, fcc_udr1_bitrate-0.03))
    ax1.annotate('RL2', (fcc_udr2_rebuf+0.003, fcc_udr2_bitrate+0.01))
    ax1.annotate('RL3', (fcc_udr3_rebuf+0.007, fcc_udr3_bitrate-0.01))
    ax1.annotate('Genet', ( fcc_genet_rebuf+0.005, fcc_genet_bitrate+0.02))

    ax1.set_ylabel('Bitrate (Mbps)')
    ax1.set_xlabel('90th percentile rebuffering ratio (%)')
    ax1.invert_xaxis()
    ax1.spines['top'].set_visible( False )
    ax1.spines['right'].set_visible( False )
    #ax.set_ylim(0.03, -0.01)

    fig1.legend(bbox_to_anchor=(0, 1.02, 1, 0.14), ncol=4, loc="upper center",
                borderaxespad=0, borderpad=0.2, columnspacing=0.01, handletextpad=0.001)

    # svg_file = os.path.join(SAVE_ROOT, 'evaluation_cc_scatter.svg')
    # pdf_file = os.path.join(SAVE_ROOT, 'evaluation_cc_scatter.pdf')
    # fig1.savefig(pdf_file,  bbox_inches='tight')
    # # os.system("inkscape {} --export-pdf={}".format(svg_file, pdf_file))
    # # os.system("pdfcrop --margins 1 {} {}".format(pdf_file, pdf_file))

    svg_file = os.path.join( SAVE_ROOT ,'mahi_fcc_arrow.svg' )
    pdf_file = os.path.join( SAVE_ROOT ,'mahi_fcc_arrow.pdf' )
    fig1.savefig( pdf_file ,bbox_inches='tight' )
    # os.system( "inkscape {} --export-pdf={}".format( svg_file ,pdf_file ) )
    # os.system( "pdfcrop --margins 1 {} {}".format( pdf_file ,pdf_file ) )

    fig2, ax2 = plt.subplots(figsize=(9, 5))

    # ['sim_BBA: bitrate: 1.03% rebuf: 0.07658' ,'sim_RobustMPC: bitrate: 1.05% rebuf: 0.05053' ,
    #  'sim_udr_1: bitrate: 1.04% rebuf: 0.07323' ,'sim_udr_2: bitrate: 0.96% rebuf: 0.04276' ,
    #  'sim_udr_3: bitrate: 0.95% rebuf: 0.04796' ,'sim_adr: bitrate: 0.95% rebuf: 0.04498']

    norway_genet_bitrate ,norway_genet_rebuf = 1.04 ,0.043

    norway_udr1_bitrate ,norway_udr1_rebuf = 1.04 ,0.073
    norway_udr2_bitrate ,norway_udr2_rebuf = 0.96 ,0.043
    norway_udr3_bitrate ,norway_udr3_rebuf = 0.95 ,0.048

    norway_bba_bitrate ,norway_bba_rebuf = 1.03 ,0.077
    norway_mpc_bitrate ,norway_mpc_rebuf = 1.05 ,0.054
    norway_oboe_bitrate ,norway_oboe_rebuf = 1.04 ,0.051

    ax2.scatter( [norway_bba_rebuf], [norway_bba_bitrate] ,marker='d', color='C0', s=msize ,label='BBA' )
    ax2.scatter( [norway_mpc_rebuf] ,[norway_mpc_bitrate] ,marker='>' ,color='C1',s=msize ,label='MPC' )
    ax2.scatter( [norway_oboe_rebuf] ,[norway_oboe_bitrate] ,marker='v' ,color='darkorange',s=msize ,label='Oboe' )
    ax2.scatter( [norway_udr1_rebuf] ,[norway_udr1_bitrate] ,marker='^' ,color='C3',s=msize ,label='RL1' )
    ax2.scatter( [norway_udr2_rebuf] ,[norway_udr2_bitrate] ,marker='<' ,color='C4',s=msize ,label='RL2' )
    ax2.scatter( [norway_udr3_rebuf] ,[norway_udr3_bitrate] ,marker='p' ,color='C5',s=msize ,label='RL3' )
    ax2.scatter( [norway_genet_rebuf] ,[norway_genet_bitrate] , s=msize ,color='C2', label='Genet' )

    ax2.annotate('BBA', (norway_bba_rebuf+0.0001, norway_bba_bitrate-0.015))
    ax2.annotate('MPC', (norway_mpc_rebuf+0.005, norway_mpc_bitrate-0.02))
    ax2.annotate('Oboe', ( norway_oboe_rebuf+0.001, norway_oboe_bitrate-0.015))
    ax2.annotate('RL1', (norway_udr1_rebuf-0.001, norway_udr1_bitrate-0.01))
    ax2.annotate('RL2', (norway_udr2_rebuf+0.003, norway_udr2_bitrate+0.01))
    ax2.annotate('RL3', (norway_udr3_rebuf+0.006, norway_udr3_bitrate))
    ax2.annotate('Genet', (norway_genet_rebuf+0.005, norway_genet_bitrate+0.01))

    ax2.set_ylabel( 'Bitrate (Mbps)' )
    ax2.set_xlabel( '90th percentile rebuffering ratio (%)' )
    ax2.invert_xaxis()
    ax2.spines['top'].set_visible( False )
    ax2.spines['right'].set_visible( False )

    svg_file = os.path.join( SAVE_ROOT ,'mahi_norway_arrow.svg' )
    pdf_file = os.path.join( SAVE_ROOT , 'mahi_norway_arrow.pdf')
    fig2.savefig( pdf_file ,bbox_inches='tight' )
    # os.system( "inkscape {} --export-pdf={}".format( svg_file ,pdf_file ) )
    # os.system("pdfcrop --margins 1 {} {}".format(pdf_file, pdf_file))

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt
from die import Die

# Create a couple of dice
d1 = Die()
d2 = Die()

results = list()
for roll in range(5000):
    results.append(d1.roll_die() + d2.roll_die())

frequencies = list()
max_result = d1.num_sides + d2.num_sides
for value in range(2, max_result+1):
    frequency = results.count(value)
    frequencies.append(frequency)

x_values = [x for x in range(2, max_result + 1)]

fig, ax = plt.subplots()
ax.barh(x_values, frequencies)

ax.set_title("Frequency a number is rolled from a pair of D6 dice.")
ax.set_xlabel("Frequency", fontsize=14)
ax.set_ylabel("Dice number", fontsize=14)
ax.tick_params(axis="both", labelsize=14)
plt.show()


from scipy import spatial
import numpy as np
from glob import glob
from joblib import Parallel, delayed
import multiprocessing
import pandas as pd
import utils.metrics as metrics
from collections import defaultdict
import time
from tqdm import tqdm
import os.path
from utils.hpatch import *
from utils.misc import *

TORCH_GPU_IS_AVAILABLE = False
try:
    import torch
    TORCH_GPU_IS_AVAILABLE = torch.cuda.is_available()
except:
    pass

id2t = {0:{'e':'ref','h':'ref','t':'ref'}, \
        1:{'e':'e1','h':'h1','t':'t1'}, \
        2:{'e':'e2','h':'h2','t':'t2'}, \
        3:{'e':'e3','h':'h3','t':'t3'}, \
        4:{'e':'e4','h':'h4','t':'t4'}, \
        5:{'e':'e5','h':'h5','t':'t5'} }

tp = ['e','h','t']

moddir = os.path.dirname(os.path.abspath(__file__))
tskdir = os.path.normpath(os.path.join(moddir, "..", "..", "tasks"))

def seqs_lengths(seqs):
    ''' Helper method to return length for all seqs'''
    N = {}
    for seq in seqs:
        N[seq] = seqs[seq].N
    return N

def dist_matrix(D1,D2,distance):
    ''' Distance matrix between two sets of descriptors'''
    if distance=='L2':
        if TORCH_GPU_IS_AVAILABLE:
            with torch.no_grad():
                D = torch.cdist(torch.from_numpy(D1).cuda(),
                                torch.from_numpy(D2).cuda(),
                                p = 2).detach().cpu().numpy()
        else:
            D = spatial.distance.cdist(D1, D2,'euclidean')
    elif distance=='L1':
        if TORCH_GPU_IS_AVAILABLE:
            with torch.no_grad():
                D = torch.cdist(torch.from_numpy(D1).cuda(),
                                torch.from_numpy(D2).cuda(),
                                p = 1).detach().cpu().numpy()
        else:
            D = spatial.distance.cdist(D1, D2,'cityblock')
    elif distance=='masked_L1':
        [desc1,masks1] = np.split(D1, 2,axis=1)
        [desc2,masks2] = np.split(D2, 2,axis=1)
        D = spatial.distance.cdist(desc1*masks1, desc2*masks2,'cityblock')
    else:
        raise ValueError('Unknown distance - valid options are |L2|L1|masked_L1|')
    return D

#####################
# Verification task #
#####################
def get_verif_dists(descr,pairs,op):
    d = {}
    for t in ['e','h','t']:
        d[t] = np.empty((pairs.shape[0],1))
    idx = 0
    pbar = tqdm(pairs)
    pbar.set_description("Processing verification task %i/3 " % op)
    for p in pbar:
        [t1,t2] = [id2t[p[1]],id2t[p[4]]]
        for t in tp:
            d1 = getattr(descr[p[0]], t1[t])[p[2]]
            d2 = getattr(descr[p[3]], t2[t])[p[5]]
            distance = descr['distance']
            if distance=='L2':
                dist = spatial.distance.euclidean(d1, d2)
            elif distance=='L1':
                dist = spatial.distance.cityblock(d1, d2)
            elif distance=='masked_L1':
                [d1,m1] = np.array_split(d1, 2)
                [d2,m2] = np.array_split(d2, 2)
                dist = spatial.distance.cityblock(m1*d1, m2*d2)
            d[t][idx] = dist
        idx+=1
    return d

def eval_verification(descr,split):
    print('>> Evaluating %s task' % green('verification'))

    start = time.time()
    pos = pd.read_csv(os.path.join(tskdir, 'verif_pos_split-'+split['name']+'.csv')).as_matrix()
    neg_intra = pd.read_csv(os.path.join(tskdir, 'verif_neg_intra_split-'+split['name']+'.csv')).as_matrix()
    neg_inter = pd.read_csv(os.path.join(tskdir, 'verif_neg_inter_split-'+split['name']+'.csv')).as_matrix()

    d_pos = get_verif_dists(descr,pos,1)
    d_neg_intra = get_verif_dists(descr,neg_intra,2)
    d_neg_inter = get_verif_dists(descr,neg_inter,3)

    results = defaultdict(lambda: defaultdict(lambda:defaultdict(dict)))

    for t in tp:
        l = np.vstack((np.zeros_like(d_pos[t]),np.ones_like(d_pos[t])))
        d_intra = np.vstack((d_neg_intra[t],d_pos[t]))
        d_inter = np.vstack((d_neg_inter[t],d_pos[t]))

        # get results for the balanced protocol: 1M Positives - 1M Negatives
        fpr,tpr,auc = metrics.roc(-d_intra,l)
        results[t]['intra']['balanced']['fpr'] = fpr
        results[t]['intra']['balanced']['tpr'] = tpr
        results[t]['intra']['balanced']['auc'] = auc

        fpr,tpr,auc = metrics.roc(-d_inter,l)
        results[t]['inter']['balanced']['fpr'] = fpr
        results[t]['inter']['balanced']['tpr'] = tpr
        results[t]['inter']['balanced']['auc'] = auc

        # get results for the imbalanced protocol: 0.2M Positives - 1M Negatives
        N_imb = d_pos[t].shape[0] + int(d_pos[t].shape[0]*0.2) # 1M + 0.2*1M
        pr,rc,ap = metrics.pr(-d_intra[0:N_imb],l[0:N_imb])
        results[t]['intra']['imbalanced']['pr'] = pr
        results[t]['intra']['imbalanced']['rc'] = rc
        results[t]['intra']['imbalanced']['ap'] = ap

        pr,rc,ap = metrics.pr(-d_inter[0:N_imb],l[0:N_imb])
        results[t]['inter']['imbalanced']['pr'] = pr
        results[t]['inter']['imbalanced']['rc'] = rc
        results[t]['inter']['imbalanced']['ap'] = ap
    end = time.time()
    print(">> %s task finished in %.0f secs  " % (green('Verification'),end-start))
    return results

def gen_verif(seqs,split,N_pos=1e6,N_neg=1e6):
    np.random.seed(42)

    # positives
    s = np.random.choice(split['test'], int(N_pos))
    seq2len = seqs_lengths(seqs)
    s_N = [seq2len[k] for k in s]
    s_idx = np.array([np.random.choice(np.arange(k),2,replace=False) for k in s_N])
    s_type = np.array([np.random.choice(np.arange(5),2,replace=False) for k in s_idx])
    df = pd.DataFrame({'s1': pd.Series(s, dtype=object),\
                       's2': pd.Series(s, dtype=object),\
                       'idx1': pd.Series(s_idx[:,0], dtype=int) ,\
                       'idx2': pd.Series(s_idx[:,0], dtype=int) ,\
                       't1': pd.Series(s_type[:,0], dtype=int) ,\
                       't2': pd.Series(s_type[:,1], dtype=int)})
    df = df[['s1','t1','idx1','s2','t2','idx2']] # updated order for matlab comp.
    df.to_csv(os.path.join(tskdir, 'verif_pos_split-'+split['name']+'.csv'),index=False)

    # intra-sequence negatives
    df = pd.DataFrame({'s1': pd.Series(s, dtype=object),\
                       's2': pd.Series(s, dtype=object),\
                       'idx1': pd.Series(s_idx[:,0], dtype=int) ,\
                       'idx2': pd.Series(s_idx[:,1], dtype=int) ,\
                       't1': pd.Series(s_type[:,0], dtype=int) ,\
                       't2': pd.Series(s_type[:,1], dtype=int)})
    df = df[['s1','t1','idx1','s2','t2','idx2']] # updated order for matlab comp.
    df.to_csv(os.path.join(tskdir, 'verif_neg_intra_split-'+split['name']+'.csv'),index=False)

    # inter-sequence negatives
    s_inter = np.random.choice(split['test'], int(N_neg))
    s_N_inter = [seq2len[k] for k in s_inter]
    s_idx_inter = np.array([np.random.randint(k) for k in s_N_inter])
    df = pd.DataFrame({'s1': pd.Series(s, dtype=object),\
                       's2': pd.Series(s_inter, dtype=object),\
                       'idx1': pd.Series(s_idx[:,0], dtype=int) ,\
                       'idx2': pd.Series(s_idx_inter, dtype=int) ,\
                       't1': pd.Series(s_type[:,0], dtype=int) ,\
                       't2': pd.Series(s_type[:,1], dtype=int)})
    df = df[['s1','t1','idx1','s2','t2','idx2']] # updated order for matlab comp.
    df.to_csv(os.path.join(tskdir, 'verif_neg_inter_split-'+split['name']+'.csv'),index=False)

#################
# Matching task #
#################
def eval_matching(descr,split):
    print('>> Evaluating %s task' % green('matching'))
    start = time.time()

    results = defaultdict(lambda: defaultdict(lambda:defaultdict(dict)))
    pbar = tqdm(split['test'])
    for seq in pbar:
        d_ref = getattr(descr[seq], 'ref')
        gt_l = np.arange(d_ref.shape[0])
        for t in tp:
            for i in range(1,6):
                d = getattr(descr[seq], t+str(i))
                D = dist_matrix(d_ref,d,descr['distance'])
                idx = np.argmin(D,axis=1)
                m_l = np.equal(idx,gt_l)
                results[seq][t][i]['sr'] = np.count_nonzero(m_l) / float(m_l.shape[0])
                m_d = D[gt_l,idx]
                pr,rc,ap = metrics.pr(-m_d,m_l,numpos=m_l.shape[0])
                results[seq][t][i]['ap'] = ap
                results[seq][t][i]['pr'] = pr
                results[seq][t][i]['rc'] = rc
                # print(t,i,ap,results[seq][t][i]['sr'])
    end = time.time()
    print(">> %s task finished in %.0f secs  " % (green('Matching'),end-start))
    return results

##################
# Retrieval task #
##################
def descr_from_idx(descr,idxs):
    d = np.empty((idxs.shape[0],descr['dim']))
    for i in range(idxs.shape[0]):
        d[i] = getattr(descr[idxs[i][0]], 'ref')[idxs[i][1]]
    return d

def get_query_intra_dists(descr,d,query,t):
    idx  = query[1]
    seq = query[0]
    D = np.empty((5))
    d = np.expand_dims(d, axis=0)

    for i in range(1,6):
        d_ = getattr(descr[seq], t+str(i))[idx]
        d_ = np.expand_dims(d_, axis=0)
        D[i-1] = dist_matrix(d,d_,descr['distance'])[0]
    return D

def eval_retrieval(descr,split): #WIP
    print('>> Evaluating %s task' % green('retrieval'))
    start = time.time()

    q = pd.read_csv(os.path.join(tskdir, 'retr_queries_split-'+split['name']+'.csv')).as_matrix()
    d = pd.read_csv(os.path.join(tskdir, 'retr_distractors_split-'+split['name']+'.csv')).as_matrix()

    # q_std = np.std(q, axis=0)
    # d_std = np.std(d, axis=0)
    # print(q.shape)
    # print(q_std.shape)

    desc_q = descr_from_idx(descr,q).astype(np.float32)
    desc_d = descr_from_idx(descr,d).astype(np.float32)

    # distactor masking per sequence
    m = dict((seq, d[:,0]!=seq) for seq in split['test'])

    print(">> Please wait, computing distance matrix...")
    D = dist_matrix(desc_q,desc_d,descr['distance'])
    print(">> Distance matrix done.")

    results = defaultdict(lambda: defaultdict(lambda:defaultdict(dict)))
    N_distractors = desc_d.shape[0]
    # at_ranks = [int(x*N_distractors) for x in [0.25,0.5,0.75,1]]
    at_ranks = [100,500,1000,5000,10000,15000,20000]

    pbar = tqdm(range(desc_q.shape[0]))
    pbar.set_description("Processing retrieval task")
    for i in pbar:
        query_descr = desc_q[i]
        for t in tp:
            D_intra = get_query_intra_dists(descr,desc_q[i],q[i],t)
            D_ = D[i,:]
            D_ = D_[m[q[i][0]]]
            gt = np.zeros_like(D_);

            D_ = np.hstack((D_intra,D_))

            # D_[0:5] = D_intra
            gt = np.hstack((np.array([1,1,1,1,1]),gt))
            # gt[0:5] = 1
            for k in at_ranks:
                pr,rc,ap = metrics.pr(-D_[0:k],gt[0:k])
                # print (pr.shape,rc.shape)
                # print ap
                results[i][t][k]['ap'] = ap
                # perm = np.argsort(D_[0:k], kind='mergesort',axis=0)
                # gt_perm = gt[perm]
                # mi_rank = np.mean(np.where(gt_perm))
                # results[i][t][k]['mi_rank'] = mi_rank
    end = time.time()
    print(">> %s task finished in %.0f secs  " % (green('Retrieval'),end-start))
    return results

def gen_retrieval(seqs,split,N_queries=0.5*1e4,N_distractors=2*1e4):
    np.random.seed(42)
    seq2len = seqs_lengths(seqs)

    s_q = np.random.choice(split['test'], int(N_queries*4))
    s_q_N = [seq2len[k] for k in s_q]
    s_q_idx = [np.random.randint(k) for k in s_q_N]
    s_q_idx = np.array(s_q_idx)

    s_d = np.random.choice(split['test'], int(N_distractors*10))
    s_d_N = [seq2len[k] for k in s_d]
    s_d_idx = [np.random.randint(k) for k in s_d_N]
    s_d_idx = np.array(s_d_idx)

    msk = np.zeros((s_q.shape[0],))
    for i in range(s_q.shape[0]):
        p = get_patch(seqs[s_q[i]],'ref',s_q_idx[i])
        if np.std(p)> 10 :
            msk[i] = 1

    msk = np.where(msk==1)
    s_q = s_q[msk]
    s_q_idx = s_q_idx[msk]

    msk = np.zeros((s_d.shape[0],))
    for i in range(s_d.shape[0]):
        p = get_patch(seqs[s_d[i]],'ref',s_d_idx[i])
        if np.std(p)> 10 :
            msk[i] = 1

    msk = np.where(msk==1)
    s_d = s_d[msk]
    s_d_idx = s_d_idx[msk]
    q_  = np.stack((s_q, s_q_idx), axis=-1)
    d_  = np.stack((s_d, s_d_idx), axis=-1)

    df_q = pd.DataFrame({'s': pd.Series(q_[:,0], dtype=object),\
                       'idx': pd.Series(q_[:,1], dtype=int)})
    df_q = df_q[['s','idx']] # updated order for matlab comp.

    df_d = pd.DataFrame({'s': pd.Series(d_[:,0], dtype=object),\
                       'idx': pd.Series(d_[:,1], dtype=int)})
    df_d = df_d[['s','idx']] # updated order for matlab comp.

    print(df_q.shape,df_d.shape)
    df_q = df_q.drop_duplicates()
    df_d = df_d.drop_duplicates()
    df_q = df_q.head(N_queries)
    df_q_ = df_q.copy()

    common = df_q_.merge(df_d,on=['s','idx'])
    # print(common.shape)
    df_q_.set_index(['s', 'idx'], inplace=True)
    df_d.set_index(['s', 'idx'], inplace=True)
    df_d = df_d[~df_d.index.isin(df_q_.index)].reset_index()
    # print(df_q.shape,df_d.shape)
    df_d = df_d.head(N_distractors)

    df_q.to_csv(os.path.join(tskdir, 'retr_queries_split-'+split['name']+'.csv'),index=False)
    df_d.to_csv(os.path.join(tskdir, 'retr_distractors_split-'+split['name']+'.csv'),index=False)

methods = {'verification': eval_verification,\
           'matching': eval_matching,\
           'retrieval': eval_retrieval}


from spider import data
import datetime
import pytz

class Club:
    def __init__(self, name, data):
        self.name = name
        self.match = []
        for line in data:
            self.match.append(Match(self.name, line))

    def __str__(self):
        return self.name + str(self.match)

class Match:
    def __init__(self, club, match):
        if match["hteam_name"] != club:
            self.against = match["hteam_name"]
            self.i = self.against + " - " + club
            self.info = "客场"
        else:
            self.against = match["gteam_name"]
            self.i = club + " - " + self.against
            self.info = "主场"
        self.start = datetime.datetime.strptime(match['games_time'], '%Y-%m-%d %H:%M:%S')
        self.start = self.start.replace(tzinfo=pytz.timezone('PRC'))
        self.end = datetime.datetime.strptime(match['games_time'], '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours=2)
        self.end = self.end.replace(tzinfo=pytz.timezone("PRC"))

    def __str__(self):
        return self.against + " at " + str(self.start)

    def __repr__(self):
        return self.against + " at " + str(self.start)

Jiangsu = Club("江苏苏宁", data)
if __name__ == "__main__":
    print(Jiangsu)


import numpy as np
import torch
import math

def rotate_Y(input, beta):
	'''
	beta: angle, range 0~360.
	'''
	output = torch.zeros_like(input)
	beta = beta * (math.pi / 180) # angle to radian
	output[..., 0] = np.cos(beta)*input[..., 0] + np.sin(beta)*input[..., 2]
	output[..., 2] = -np.sin(beta)*input[..., 0] + np.cos(beta)*input[..., 2]
	output[..., 1] = input[..., 1]

	output[..., 3] = np.cos(beta)*input[..., 3] + np.sin(beta)*input[..., 5]
	output[..., 5] = -np.sin(beta)*input[..., 3] + np.cos(beta)*input[..., 5]
	output[..., 4] = input[..., 4]
	return output

def get_adj(N, J, edges):
	adj = np.eye(N*J)
	for edge in edges:
		for i in range(N):
			adj[edge[0] + i * J, edge[1] + i * J] = 1
			adj[edge[1] + i * J, edge[0] + i * J] = 1
	return torch.from_numpy(adj).float().cuda()

def get_connect(N, J):
	conn = np.zeros((N*J, N*J))
	conn[:J, :J] = 1
	conn[J:, J:] = 1
	return torch.from_numpy(conn).float().cuda()

def distance_loss(target, pred):
    mse_loss = (pred - target) ** 2
    mse_loss = mse_loss.sum(-1)
    mse_loss = mse_loss.sqrt()
    loss = mse_loss.mean()
    return loss

def relation_loss(target, pred):
    mse_loss = torch.abs(pred - target)
    loss = mse_loss.mean()
    return loss

def process_pred(pred_vel, pred_vel_aux):
	pred_vel_x = pred_vel[:, :, :16]
	pred_vel_y = pred_vel[:, :, 16:]
	pred_vel_aux_x = []
	pred_vel_aux_y = []
	for pred_ in pred_vel_aux:
		pred_vel_aux_x.append(pred_[:, :, :16])
		pred_vel_aux_y.append(pred_[:, :, 16:])
	return pred_vel_x, pred_vel_y, pred_vel_aux_x, pred_vel_aux_y


from csv import DictReader
import ast
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.rdCoordGen import AddCoords

def my_eval(str):
    testes = [
        (int),
        (float)
    ]
    for teste in testes:
        try:
            return teste(str)
        except ValueError:
            continue
    return str

def get_configs(file_path):
    with open(file_path, 'r', encoding='utf-8')as file:
        reader = DictReader(file)
        configs = list(reader)[0]
        for config in configs:
            configs[config] = my_eval(configs[config])
    return configs

def remove_isotopes(smile):
    mol = Chem.MolFromSmiles(smile)
    mol_block = Chem.MolToMolBlock(mol)
    new_mol_block = []
    for line in mol_block.split('\n'):
        if 'M  ISO' not in line:
            new_mol_block += [line]
    new_mol_block = '\n'.join(new_mol_block)
    new_mol = Chem.MolFromMolBlock(new_mol_block)
    new_smile = Chem.MolToSmiles(new_mol)
    return(new_smile)

if __name__ == "__main__":
    smile = r"Cn1c(=O)c2c(nc(/C=C/c3cccc(Cl)c3)n2[11CH3])n(C)c1=O"
    new_smile = remove_isotopes(smile)
    mol = Chem.MolFromSmiles(smile)
    new_mol = Chem.MolFromSmiles(new_smile)
    AddCoords(mol)
    AddCoords(new_mol)
    img = Draw.MolsToGridImage(mols=[mol, new_mol], legends=['original', 'no isotopes'])
    img.show()


import asyncio
import threading
from pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import ThreadingOSCUDPServer
from owo_suit import OWOSuit
import config

try:
    print("Starting OWO Suit...")
    c = config.get()
    owo_suit = OWOSuit(c.owo_ip, c.frequency, c.intensity)
    owo_suit.init()
    dispatcher = Dispatcher()
    owo_suit.map_parameters(dispatcher)
    osc_server = ThreadingOSCUDPServer(
        ("127.0.0.1", c.server_port), dispatcher, asyncio.new_event_loop())
    threading.Thread(target=lambda: osc_server.serve_forever(2),
                     daemon=True).start()
    threading.Thread(target=owo_suit.watch,
                     daemon=True).start()
    input("Press any key to exit\n")
except KeyboardInterrupt:
    print("Shutting Down...\n")
except OSError:
    pass


from django.urls import path
from . import views

app_name = 'telemedWebapp'

urlpatterns = [
    path('', views.home_view, name='home'),
    path('register_rehabilitator/', views.register_rehabilitator, name='register_rehabilitator'),
    path('register_patient/', views.register_patient, name='register_patient'),
    path('rehabilitators/', views.view_all_rehabilitators, name='view_all_rehabilitators'),
    path('patients/', views.view_particular_patients, name='view_particular_patients'),
    path('login/', views.login_user, name='login_user'),
    path('logout/', views.logout_user, name='logout_user'),
    path('my_account/', views.my_account_view, name='my_account'),
    path('my_account/edit/', views.edit, name='edit'),
    path('add_exercise/', views.add_exercise, name='add_exercise'),
    path('edit_exercise/', views.edit_exercise, name='edit_exercise'),
    path('delete_exercise/', views.delete_exercise, name='delete_exercise'),
    path('view_exercises/', views.view_exercises, name='view_exercises'),
    path('exercise_plot/', views.exercise_plot, name='exercise_plot')
]


import tushare as ts

pro = ts.pro_api('e826b583d264906bbf69b8d91c22a29c4c25839a7f13e02c7d869e85')

def forecastdata(ts_code, start_time, end_time):
    df = pro.daily(ts_code=ts_code, start_date=start_time, end_date=end_time)
    return df.iloc[:, 1].values, df.iloc[:, 5].values

def getstockprice(ts_code_list, start_time, end_time):
    datadict = {}
    for item in ts_code_list:
        df = pro.daily(ts_code=item, start_date=start_time, end_date=end_time)
        df = df.loc[:, ['trade_date', 'close']]
        # 在这里需要将array转化为list，十分重要，务必牢记
        df = df.values.tolist()
        datadict[item] = df
    return datadict


import unittest
import pandas as pd
import numpy as np
from numpy.random import normal, seed

from datetime import date
from dateutil.relativedelta import relativedelta

from finmarkets import DiscountCurve, ForwardRateCurve, InterestRateSwap, InterestRateSwaption

class Test_Irs(unittest.TestCase):
    def test_irs(self):
        obs_date = date.today()
        discount_data = pd.read_excel('https://github.com/matteosan1/finance_course/raw/develop/input_files/discount_factors_2022-10-05.xlsx')
        euribor_data = pd.read_excel('https://github.com/matteosan1/finance_course/raw/develop/input_files/euribor_curve.xlsx', sheet_name='EURIBOR3M')

        dates = [obs_date + relativedelta(months=i) for i in discount_data['months']]
        dc = DiscountCurve(obs_date, dates, discount_data.loc[:, 'dfs'])

        dates = [obs_date + relativedelta(months=i) for i in euribor_data['months']]
        fr = ForwardRateCurve(obs_date, dates, euribor_data.loc[:, 'rates']*0.01)

        start_date = obs_date + relativedelta(months=1)
        nominal = 1e6
        fixed_rate = 0.023
        tenor = "3m"
        maturity = "4y"

        irs = InterestRateSwap(nominal, start_date, maturity, fixed_rate, tenor)
        self.assertAlmostEqual(irs.npv(dc, fr), 32809.34, places=2)
        #print ("NPV: {:.2f} EUR".format(irs.npv(dc, fr)))

    def test_swaption(self):
        obs_date = date.today()
        discount_data = pd.read_excel('https://github.com/matteosan1/finance_course/raw/develop/input_files/discount_factors_2022-10-05.xlsx')
        euribor_data = pd.read_excel('https://github.com/matteosan1/finance_course/raw/develop/input_files/euribor_curve.xlsx', sheet_name='EURIBOR3M')

        dates = [obs_date + relativedelta(months=i) for i in discount_data['months']]
        dc = DiscountCurve(obs_date, dates, discount_data.loc[:, 'dfs'])

        dates = [obs_date + relativedelta(months=i) for i in euribor_data['months']]
        fr = ForwardRateCurve(obs_date, dates, euribor_data.loc[:, 'rates']*0.01)

        start_date = obs_date + relativedelta(years=1)
        exercise_date = start_date
        volatility = 0.15
        nominal = 1e6
        fixed_rate = 0.023
        tenor = "3m"
        maturity = "4y"
        swaption = InterestRateSwaption(nominal, start_date, exercise_date, maturity,
                                        volatility, fixed_rate, tenor)

        price_mc, interval = swaption.payoffMC(obs_date, dc, fr)
        self.assertAlmostEqual(price_mc, 33410.71, delta=1000)
        self.assertAlmostEqual(interval, 176.39, delta=10)
        #print ("MC: {:.2f} +- {:.2f}".format(price_mc, interval))

        price_bs = swaption.payoffBS(obs_date, dc, fr)
        self.assertAlmostEqual(price_bs, 33598.66, places=2)
        #print ("BS: {:.2f}".format(price_bs))

print ("\nTest IRS")
if __name__ == '__main__':
    unittest.main()


import ast
import pathlib
import unittest
import sympy
from masmod.ast_worker.if_else.condition_hoist import IfElseConditionHoistTransformer
from masmod.symbols import AnyContext

class TestConditionHoist(unittest.TestCase):

    def _do_test_case(self, case_name: str) -> None:
        with open(
            pathlib.Path(__file__).parent.joinpath(f"{case_name}/case.py"),
            mode="r",
            encoding="utf-8"
        ) as f:
            source_code = f.read()

        context = AnyContext()
        context["t"] = sympy.Symbol("t")
        node = ast.parse(source_code)
        transformer = IfElseConditionHoistTransformer(
            source_code=source_code,
            global_context=context,
            local_context=context
        )
        transformer.visit(node)

        transformed = ast.unparse(node)
        with open(
            pathlib.Path(__file__).parent.joinpath(f"{case_name}/trans.py"),
            mode="w",
            encoding="utf-8"
        ) as f:
            f.write(transformed)

        exec(transformed)

    def test_case_simple(self) -> None:
        self._do_test_case("simple")

    def test_case_multiple(self) -> None:
        self._do_test_case("multiple")

    def test_case_elif(self) -> None:
        self._do_test_case("elif")

    def test_case_named_condition(self) -> None:
        self._do_test_case("named_condition")

    def test_case_and_or(self) -> None:
        self._do_test_case("and_or")


from nltk.corpus import movie_reviews

from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
import numpy as np
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.metrics import confusion_matrix

#print(movie_reviews.fileids())

def func(list0):
    sents = []
    for blist in list0:
        fsent = ""
        for slist in blist:
            fsent += slist + " "
        sents.append(fsent)
    return sents

sents0 = movie_reviews.sents("neg/cv000_29416.txt")
sents1 = movie_reviews.sents("pos/cv041_21113.txt")

texts0 = func(sents0)
texts1 = func(sents1)

file = open("table.txt", "w")
for i in texts0:
    file.write("neg" + "\t" + i + "\n")
file.close()

print(file)

file = open("table.txt", "a")
for i in texts1:
    file.write("pos" + "\t" + i + "\n")
file.close()

print(file)

df = pd.read_table("table.txt" , sep='\t', header=None, names=['label', 'message'])
df['label'] = df.label.map({'neg': 1, 'pos': 0})
# df['message'] = df['message'].apply(lambda x: ' '.join(x))

count_vect = CountVectorizer()

counts = count_vect.fit_transform(df['message'])

transformer = TfidfTransformer().fit(counts)

counts = transformer.transform(counts)

X_train, X_test, y_train, y_test = train_test_split(counts, df['label'], test_size=0.1, random_state=72)

model = MultinomialNB().fit(X_train, y_train)

#print(X_train, X_test, y_train, y_test)

predicted = model.predict(X_test)

print(np.mean(predicted == y_test))


import json
import os

import tensorflow as tf
import tensorflow_datasets as tfds

BATCH_SIZE = 64
BUFFER_SIZE = 10000
LEARNING_RATE = 1e-4

def get_task_name():
    cluster_spec = os.environ.get("CLUSTER_SPEC", None)
    task_name = ""
    if cluster_spec:
        cluster_spec = json.loads(cluster_spec)
        job_index = os.environ["TASK_INDEX"]
        job_name = os.environ["JOB_NAME"]
        task_name = job_name + "_" + job_index

    return task_name

def input_fn(mode, input_context=None):
    datasets, info = tfds.load(
        name="mnist",
        data_dir="/tmp/" + get_task_name() + "/data",
        with_info=True,
        as_supervised=True,
    )

    mnist_dataset = datasets["train"] if mode == tf.estimator.ModeKeys.TRAIN else datasets["test"]

    def scale(image, label):
        image = tf.cast(image, tf.float32)
        image /= 255
        return image, label

    if input_context:
        mnist_dataset = mnist_dataset.shard(
            input_context.num_input_pipelines, input_context.input_pipeline_id
        )

    return mnist_dataset.map(scale).cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

def model_fn(features, labels, mode):
    model = tf.keras.Sequential(
        [
            tf.keras.layers.Conv2D(32, 3, activation="relu", input_shape=(28, 28, 1)),
            tf.keras.layers.MaxPooling2D(),
            tf.keras.layers.Flatten(),
            tf.keras.layers.Dense(64, activation="relu"),
            tf.keras.layers.Dense(10),
        ]
    )

    logits = model(features, training=False)

    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {"logits": logits}
        return tf.estimator.EstimatorSpec(labels=labels, predictions=predictions)

    optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate=LEARNING_RATE)
    loss = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=True, reduction=tf.keras.losses.Reduction.NONE
    )(labels, logits)
    loss = tf.reduce_sum(loss) * (1.0 / BATCH_SIZE)
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode, loss=loss)

    return tf.estimator.EstimatorSpec(
        mode=mode,
        loss=loss,
        train_op=optimizer.minimize(loss, tf.compat.v1.train.get_or_create_global_step()),
    )

if __name__ == "__main__":
    strategy = tf.distribute.experimental.ParameterServerStrategy()
    config = tf.estimator.RunConfig(train_distribute=strategy)
    estimator = tf.estimator.Estimator(model_fn=model_fn, model_dir="/tmp/model", config=config)
    train_spec = tf.estimator.TrainSpec(input_fn)
    eval_spec = tf.estimator.EvalSpec(input_fn)
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)


import csv
import io
import boto3

client=boto3.resource("dynamodb")
table = client.Table("emp-table")

#Loading the data
with io.open("emp.csv","r",encoding="utf-8")as f1:
    data=f1.read()
    f1.close()

#Splitting the data
data=data.split("\n")[1:]
for line in data:
    name=line.split(",")[0]
    phone=line.split(",")[1]
    email=line.split(",")[2]
    country=line.split(",")[3]

    response = table.put_item(
        Item={
            'name': name,
            'email': email,
            'phone': phone,
            'country':country}
            )
    print(email)


import time
import gevent
from gevent import monkey; monkey.patch_all()
import pandas as pd
import requests as rs
from lxml import etree
from gevent.pool import Pool, Group

pageurl = 'https://www.hqms.org.cn/usp/roster/index.jsp'
dataurl = 'https://www.hqms.org.cn:443/usp/roster/rosterInfo.jsp'

def getProvinces(url):
    rqpu = rs.get(url)
    html = etree.HTML(rqpu.text)
    provs = html.xpath('//select')
    for p in provs:
        if 'organid' in p.values():
            pv = p
    li = []
    try:
        op = pv.xpath('option')
        for o in op:
            if o.attrib['value'] != '':
                li.append(o.attrib['value'])
    except:
        print('got an error')
    return li

def getData(pid):
    data = {'provinceId': pid, 'htype': '', 'hgrade': '', 'hclass': '', 'hname': ''}
    rq = rs.post(dataurl, data=data)
    df = pd.read_json(rq.text)
    df.to_csv('p{0}.csv'.format(pid), index=False, header=False)

if __name__ == '__main__':
    print(time.asctime(time.localtime(time.time())))
    pli = getProvinces(pageurl)
    group = Group()
    for l in pli:
        group.add(gevent.spawn(getData, l))
    group.join()
    print(time.asctime(time.localtime(time.time())))

    ''' below is sync methos
    for l in pli:
        getData(l)
    '''
    ''' below is async method
    pool = Pool(40)
    for l in pli:
        pool.imap(getData, l)
    pool.join()
    '''


#==============================================================================
# GOOGLE EARTH ENGINE UTILITIES
#==============================================================================

import constants as c

import csv
import ee
import json
import os
import pandas as pd
import time

#TODO remove json import and just parse the string normally
#TODO import image from given coordinates, no csv parsing or nothin

def exportTable(table, scale, folder="no_export_folder", desc="no_desc"):
  ee.batch.Export.table.toDrive(
    collection = table,
    description = desc,
    folder = folder,
    fileFormat = "CSV"
  ).start()

# Export one table of the given image, at the scale and dimensions specified.
def exportTableFromImage(image, polygon, scale, folder="no_export_folder",
    desc="no_desc"):
  exportTable(sample(image, polygon, scale), scale, folder, desc)

# samples image into feature_collection.
# TODO rename to be a getDATATYPE, once we know the datatype returned from this.
#      It's likely to be a FeatureCollection.
def sample(img, region, scale):
  return img.sampleRegions(
      collection = region,
      geometries = True,
      scale = scale
  )

# Export one GeoTIFF image of the given image, at the scale and dimension
# specified.
def exportGeotiff(image, polygon, scale, folder="no_export_folder",
    desc="no_desc"):
  ee.batch.Export.image.toDrive(
    crs = 'EPSG:3857',
    description = desc,
    fileFormat = 'GeoTIFF',
    folder = folder,
    image = image,
    maxPixels = 10e9,
    region = polygon,
    scale = scale
  ).start()

# Gets a square kilometer with the given point object as the centroid.
def getSqKmFromPoint(point):
  return getRangeFromPoint(point, 500)

def getRangeFromPoint(point, range):
  return point.buffer(range).bounds()

# TODO remove in favour of getBigImgsFromDf()
def getImgsFromCsv(csv_path, img):
  # For each line in CSV, export a geotiff of the given coordinates.
  # Buffer included to avoid usage limits.

  with open(csv_path) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    firstRow = True

    # The number of simultaneous exports can't go over 3 or 4 without GEE
    # taking a performance hit.
    concurrent_exports = 3
    export_buffer = []

    for row in csv_reader:
      if firstRow:
        firstRow = False
        continue

      # coords are stored as (LONGITUDE, LATITUDE)
      coords = json.loads(row[2]).get("coordinates")
      polygon = getSqKmFromPoint(ee.Geometry.Point(coords))
      name = f"{coords[0]}_{coords[1]}"
      tifname = name + ".tif"

      # skip files that have already been exported.
      if os.path.isfile(f"{c.png_dir}/{name}.png") or \
          os.path.isfile(f"{c.data_dir}/geotiff/{tifname}") or \
          os.path.isfile(f"{c.drive_path}{c.geotiff_dir}/{tifname}"):
        print(f"skipping  {name} - file already exists!")
        continue

      export_buffer.append(tifname)
      print(f"exporting {tifname}")
      exportGeotiff(img, polygon, 10, c.geotiff_dir, name)

      # Block until export buffer is smaller than the maximum allowed concurrent
      # exports.
      while len(export_buffer) >= concurrent_exports:
        time.sleep(5)
        # when the files stored in the export buffer are found in the filesystem,
        # remove them from buffer
        for filename in export_buffer:
          if os.path.isfile(f"{c.drive_path}{c.geotiff_dir}/{filename}"):
            export_buffer.remove(filename)
            print(f"exported  {filename}")

  print(f"exported {csv_path}")

def getBigImgsFromDf(df, img):
  # limit concurrent exports to avoid performance hit
  concurrent_exports = 3
  export_buffer = []

  for _, row in df.iterrows():
    coords = (row.longitude, row.latitude)
    # Arbitrarily defining the pixels, this gives us 224px.
    polygon = getRangeFromPoint(ee.Geometry.Point(coords), 700)
    name = f"{coords[0]}_{coords[1]}"
    tifname = name + ".tif"

    if imgExportExists("_224", name):
      print(f"{name} exists!")
      continue

    export_buffer.append(tifname)
    print(f"exporting {tifname}")
    exportGeotiff(img, polygon, 10, "big_geotiff", name)

    while len(export_buffer) >= concurrent_exports:
      time.sleep(10)
      # when the files stored in the export buffer are found in the drive,
      # remove them
      for filename in export_buffer:
        if os.path.isfile(f"{c.local_drive}/big_geotiff/{filename}"):
          export_buffer.remove(filename)
          print(f"exported  {filename}")

def imgExportExists(size_suffix, img_name):
  # Checks a PNG export exists in the given img_dir, or a geotiff exists in the
  # backup or default space. Size_suffix is given as _224, usually.
  testpaths = [
    f"{c.png_dir}{size_suffix}/{img_name}.png",
    f"{c.data_dir}/geotiff{size_suffix}/{img_name}.tif",
    f"{c.drive_path}big_geotiff/{img_name}.tif"
  ]
  for path in testpaths:
    if os.path.isfile(path): return True
  return False


from flask import Flask, request, jsonify, render_template_string
import requests
from moviepy.editor import VideoFileClip
from PIL import Image
import base64
import io

app = Flask(__name__)

def get_video_info(url):
    try:
        response = requests.head(url)
        if 'content-type' in response.headers:
            mime_type = response.headers['content-type']
            extension = mime_type.split("/")[-1]
        else:
            mime_type = "Unknown"
            extension = "Unknown"

        content_length = response.headers.get("content-length", "Unknown")
        content_encoding = response.headers.get("content-encoding", "Unknown")
        server = response.headers.get("server", "Unknown")
        last_modified = response.headers.get("last-modified", "Unknown")
        content_disposition = response.headers.get("content-disposition", "Unknown")
        content_language = response.headers.get("content-language", "Unknown")

        video_duration = "N/A"
        resolution = "N/A"
        fps = "N/A"
        estimated_frames = "N/A"
        thumbnail_base64 = None

        if mime_type.startswith("video"):
            try:
                video_clip = VideoFileClip(url)
                video_duration = {
                    "seconds": video_clip.duration,
                    "minutes": video_clip.duration / 60
                }
                resolution = {
                    "width": video_clip.size[0],
                    "height": video_clip.size[1]
                }
                fps = video_clip.fps

                estimated_frames = "{:,}".format(int(video_clip.duration * fps))

                thumbnail = video_clip.get_frame(50)
                with Image.fromarray(thumbnail) as img:
                    img_byte_array = io.BytesIO()
                    img.save(img_byte_array, format="JPEG")
                    thumbnail_base64 = base64.b64encode(img_byte_array.getvalue()).decode()

            except Exception as e:
                video_duration = "Unknown"
                resolution = "Unknown"
                fps = "Unknown"
                estimated_frames = "Unknown"

        video_info = {
            "url": url,
            "mime_type": mime_type,
            "extension": extension,
            "content_length": content_length,
            "content_encoding": content_encoding,
            "server": server,
            "last_modified": last_modified,
            "content_disposition": content_disposition,
            "content_language": content_language,
            "video_duration": video_duration,
            "resolution": resolution,
            "fps": fps,
            "estimated_frames": estimated_frames,
            "thumbnail_base64": thumbnail_base64
        }
        return video_info

    except Exception as e:
        return {"error": str(e)}

@app.route('/video_info', methods=['POST'])
def video_info_api():
    data = request.get_json()

    if 'url' not in data:
        return jsonify({"error": "URL parameter missing"}), 400

    video_url = data['url']
    video_info = get_video_info(video_url)
    return jsonify(video_info)
@app.route('/', methods=['GET', 'POST'])
@app.route('/video_info/form', methods=['GET', 'POST'])
def video_info_form():
    video_info = None

    if request.method == 'POST':
        url = request.form.get('url')
        video_info = get_video_info(url)

    template = """
    <!DOCTYPE html>

        <title>Video Information Form</title>

Video Information Form

            <form method="POST" action="/video_info/form">
                Video URL:
                <input type="text" name="url" id="url" value="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4" required>
                <input type="submit" value="Submit">
            </form>
            {% if video_info %}

Video Information:

Video URL: {{ video_info.url }}

MIME Type: {{ video_info.mime_type }}

Extension: {{ video_info.extension }}

Content Length: {{ video_info.content_length }} bytes

Content Encoding: {{ video_info.content_encoding }}

Content Disposition: {{ video_info.content_disposition }}

Content Language: {{ video_info.content_language }}

Video Duration (seconds): {{ video_info.video_duration.seconds }} seconds

Video Duration (minutes): {{ video_info.video_duration.minutes }} minutes

Resolution (Width): {{ video_info.resolution.width }} pixels

Resolution (Height): {{ video_info.resolution.height }} pixels

FPS (Frames per Second): {{ video_info.fps }} FPS

Estimated Frames: {{ video_info.estimated_frames }} frames

            {% if video_info.thumbnail_base64 %}

            {% endif %}
            {% endif %}

    """

    return render_template_string(template, video_info=video_info)

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


"""ImportJson Migration."""

from masoniteorm.migrations import Migration
from masoniteorm.query import QueryBuilder

import json
from os import path

class ImportJson(Migration):
    def up(self):
        """
        Run the migrations.
        """

        if not path.exists('db.json'):
            return

        guilds = QueryBuilder().on(self.connection).table('guilds')
        text_channels = QueryBuilder().on(self.connection).table('text_channels')

        with open('db.json', encoding='utf-8') as f:
            data = json.load(f)

        for guild_id, guild_content in data["guilds"].items():
            guilds.create({
                "id": guild_id
            })
            for channel_id, channel_content in guild_content["channels"].items():
                text_channels.create({
                    "id": channel_id,
                    "guild_id": guild_id,
                    "fix_twitter": channel_content["fixtweet"]
                })

    def down(self):
        """
        Revert the migrations.
        """
        return


#!/usr/bin/env python3

import argparse
import sys
import random

# import numpy as np

# class Decoder(object):
#     def __init__(self, initialProb, transProb, obsProb):
#         self.N = initialProb.shape[0]
#         self.initialProb = initialProb
#         self.transProb = transProb
#         self.obsProb = obsProb
#         assert self.initialProb.shape == (self.N, 1)
#         assert self.transProb.shape == (self.N, self.N)
#         assert self.obsProb.shape[0] == self.N

#     def Obs(self, obs):
#         return self.obsProb[:, obs, None]

#     def Decode(self, obs):
#         trellis = np.zeros((self.N, len(obs)))
#         backpt = np.ones((self.N, len(obs)), 'int32') * -1

#         # initialization
#         trellis[:, 0] = np.squeeze(self.initialProb * self.Obs(obs[0]))

#         for t in xrange(1, len(obs)):
#             trellis[:, t] = (trellis[:, t-1, None].dot(self.Obs(obs[t]).T) * self.transProb).max(0)
#             backpt[:, t] = (np.tile(trellis[:, t-1, None], [1, self.N]) * self.transProb).argmax(0)
#         # termination
#         tokens = [trellis[:, -1].argmax()]
#         for i in xrange(len(obs)-1, 0, -1):
#             tokens.append(backpt[tokens[-1], i])
#         return tokens[::-1]

parser=argparse.ArgumentParser(description='Remove recombination from snp matrix')
parser.add_argument('alnfile',type=argparse.FileType(),help="fastafiles")
parser.add_argument('-p','--posfile',type=argparse.FileType(),default=None)
args =parser.parse_args()

class segment:
    ntest=0
    alnlength=0
    def __init__(self,h,start,end):
        self.hash = h
        self.start = start
        self.end = end
        segment.ntest +=1
        segment.alnlength = l
    def test(self):
        statistic= 1.0*segment.ntest*(hashprob[self.hash]/l*1.0)**(len(self)-1)
        if statistic < 0.05:
            print("Length: {}, stat: {}, ntest: {}, l: {}, hash: {}".format(len(self),statistic,segment.ntest,segment.alnlength,self.hash),file=sys.stderr)
        return statistic >= 0.05
    def __len__(self):
        return self.end-self.start
    def discard(self):
        hashprob[self.hash]-=len(self)
        segment.alnlength-=len(self)
        segment.ntest -=1

def hash(pattern):
    """ Determines if patterns are the same. Note that some nonidentical patterns may still give the same hash. """
    prev=""
    n=1
    hashstring=list()
    for i in pattern:
        if i == prev:
            n+=1
        else:
            hashstring.append(n)
            n=1
        prev=i
    hashstring.append(n)
    return tuple(hashstring[1:])

if args.posfile:
    posList=list()
    for line in args.posfile:
        posList.append(line.strip())
    args.posfile.close()

line=next(args.alnfile)
(norgs,alnlength)=line.split()
(norgs,alnlength)=(int(norgs),int(alnlength))
aln=list()
for i in range(alnlength):
    aln.append(list("N" * norgs))

forspalte=list()
org=0
for line in args.alnfile:
    line=line.strip()
    forspalte.append(line[0:10])
    pos=0
    for char in line[10:]:
        try:
            aln[pos][org]=char
        except IndexError:
            print("norgs=%i\norg=%i\nalnlength=%i\ni=%i" %(norgs,org,alnlength,i))
        pos+=1
    org+=1

""" Calculate frequencies of hash values """
hashprob=dict()

for profile in aln:
    h=hash("".join(profile))
    try:
        hashprob[h]+=1
    except KeyError:
        hashprob[h]=1
l=len(aln)
last=hash("")
lastdeleted=hash("")
sincelast=0

segments=list()
begin = None
for i in range(l):
    sh=hash("".join(aln[i]))
    if sh == last:
        """ A new segment is started """
        pass
    else:
        """ segment ends and is recorded """
        if begin:
            segments.append(segment(last,begin,i))
        last = sh
        begin = i

discardedPositions = set()
lastDiscarded=None
segments.sort(key=len,reverse=True)
for i in range(len(segments)):
    s = segments[i]
    if s.test()== False:
        s.discard()
        # if lastDiscarded and (i-lastDiscarded<4) and (s.hash == segments[lastDiscarded].hash):
        #     discardedPositions.update(range(segments[lastDiscarded].start,s.end))
        #     print("Discarding {} SNPs".format(s.end-segments[lastDiscarded].start),file=sys.stderr)
        #else:
        discardedPositions.update(range(s.start,s.end))
        print("Discarding {} SNPs".format(s.end-s.start),file=sys.stderr)
        lastDiscarded = i
print("%i %i" % (norgs,l-len(discardedPositions)))

for i in range(norgs):
    print(forspalte[i],end='')
    for j in range(l):
        if not j in discardedPositions:
            print(aln[j][i],end='')
    print('')


from typing import TypedDict
from datetime import timedelta
from django.utils import timezone
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import ModeratorRanking
from external_services.brainly_api import BrainlyGraphQLAPI, Market, transform_graphql_id

class RankingPlace(TypedDict):
    place: int
    points: int
    user_id: int

class GetDailyRankings(APIView):
    def get(self, request):
        market = Market.US

        graphql_api = BrainlyGraphQLAPI(market=market)
        gql_res = graphql_api.execute_query("""
        query GetModeratorRanking {
        userRankings(rankingType: MODERATOR_DAILY) {
            points
            user {id}
        }
        }
        """)

        bulk_data: list[ModeratorRanking] = []
        for place in gql_res.data["userRankings"]:
            user_id = transform_graphql_id(place["user"]["id"])
            user_global_id = f"{market.value}:{user_id}"

            bulk_data.append(
                ModeratorRanking(
                    moderator_id=user_global_id,
                    value=place["points"],
                    market=market.value,
                    time=timezone.now()
                )
            )

        #moderators =

        #rows = ModeratorRanking.objects.bulk_create(bulk_data)

        #print(rows)
        return Response("OK")
        #return Response(places)
        #range = (timezone.now() - timedelta(days=1), timezone.now())

        #rankings = ModeratorRanking.timescale.select_related("moderator").filter(time__range=range).time_bucket("time", "2 hours")
        #print(rankings)
        #return Response("hello")
        # items = Item.objects.all()
        # serializer = ItemSerializer(items, many=True)
        # return Response(serializer.data)


import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt

# Plots two images side by side using matplotlib
def visualize(real_A, real_B, fake_B, fname):
    # 1x3x256x256 to 1x256x256x3
    real_A = real_A.transpose((0, 2, 3, 1))
    real_B = real_B.transpose((0, 2, 3, 1))
    fake_B = fake_B.transpose((0, 2, 3, 1))
    # Pixel values from 0-255
    real_A = np.clip((real_A + 1.0) * (255.0 / 2.0), 0, 255).astype(np.uint8)
    real_B = np.clip((real_B + 1.0) * (255.0 / 2.0), 0, 255).astype(np.uint8)
    fake_B = np.clip((fake_B + 1.0) * (255.0 / 2.0), 0, 255).astype(np.uint8)

    # Create a matplotlib figure with two subplots: one for the real and the other for the fake
    # fill each plot with our buffer array, which creates the image
    fig = plt.figure()
    ax1 = fig.add_subplot(3, 1, 1)
    ax1.set_title('Input')
    ax1.axis('off')
    ax1.imshow(real_A[0])
    ax2 = fig.add_subplot(3, 1, 2)
    ax2.imshow(real_B[0])
    ax2.set_title('Ground Truth')
    ax2.axis('off')
    ax3 = fig.add_subplot(3, 1, 3)
    ax3.imshow(fake_B[0])
    ax3.set_title('pix2pix')
    ax3.axis('off')
    # plt.show()
    plt.savefig(fname)
    plt.close()


from telebot import TeleBot
from telebot import types
from telebot.types import Message
import config
from modules import crl

bot = TeleBot(config.TOKEN)

@bot.message_handler(commands=['start'])
def welcome(message: Message):
    bot.send_message(message.chat.id, "Старт".format(message.from_user, bot.get_me()), parse_mode="html")

@bot.message_handler(commands=['check'])
def button_message(message : Message):
    markup=types.ReplyKeyboardMarkup(resize_keyboard=True)
    butt_check_crl=types.KeyboardButton("check")
    markup.add(butt_check_crl)
    chaecker1 = crl.CRLBaseChecker()
    bot.send_message(message.chat.id, chaecker1.start().format(message.from_user, bot.get_me()), reply_markup=markup)

bot.polling(none_stop=True)


#!/usr/bin/env python
#encoding=utf-8
import bottle
from bottle import route, default_app
from google.appengine.ext.webapp.util import run_wsgi_app
from bottle import route, run,redirect,request
t=""
html="""
<html>bottle.run(server='gae')
<title>life is tough,use python~</title>
<body>
<form name="form1" action="/" method="post">
<textarea name="t" cols="80" rows="25">%s</textarea>
<input type="submit" name="sub" value="save" />
</form>
</body>
</html>
"""
from google.appengine.ext import db
class Pastebin(db.Model):
  content   = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)

#@route('/',method=["POST","GET"])
@route('/',method=["POST"])
def index():
    global t,html
    t=request.POST.get("t")
    #print t
    if Pastebin.all().count()==0:
        p=Pastebin(content=t)
    else:
        p=Pastebin.all().order("-date").fetch(1)[0]
        p.content=t
    p.put()
    return html%t

@route('/')
def index():
    global t,html
    #print t
    t=db.GqlQuery("SELECT * FROM Pastebin order by date desc limit 1")
    if t.count()==0:
        p=Pastebin(content="")
        p.put()
        redirect("/")
    #print t
    #print type(t)
    #print t[0].content
    return html%t[0].content

#run(host='localhost', port=8084)
#bottle.run(server='gae')
bottle.debug(True)
app = default_app()
run_wsgi_app(app)