import pandas as pd
# import numpy as np
import requests
import json
import streamlit as st
import plotly.express as px

# markdownで文章が書ける
st.markdown('# 簡単日本酒アプリ')
st.markdown('[さけのわAPI](https://sakenowa.com)のデータを表示しています:')

# エンドポイント
urls = {
    "地域一覧": "https://muro.sakenowa.com/sakenowa-data/api/areas",
    "銘柄一覧": "https://muro.sakenowa.com/sakenowa-data/api/brands",
    "蔵元一覧": "https://muro.sakenowa.com/sakenowa-data/api/breweries",
    "ランキング": "https://muro.sakenowa.com/sakenowa-data/api/rankings",
    "フレーバーチャート": "https://muro.sakenowa.com/sakenowa-data/api/flavor-charts",
    "フレーバータグ": "https://muro.sakenowa.com/sakenowa-data/api/flavor-tags",
    "銘柄ごとフレーバータグ": "https://muro.sakenowa.com/sakenowa-data/api/brand-flavor-tags",
    }

    # 地域名を取得
areas_response = requests.get(urls.get("地域一覧")).json()
areas = [area["name"] for area in areas_response["areas"]]
select_areas = st.sidebar.selectbox("好きな地域を選んでください", areas)

# 地域IDを取得
areaId = [area["id"] for area in areas_response["areas"] if area["name"]==select_areas][0]

# 蔵元名を取得
breweries_response = requests.get(urls.get("蔵元一覧")).json()
breweries = [breweries["name"] for breweries in breweries_response["breweries"] if breweries["areaId"]==areaId]
select_breweries = st.sidebar.selectbox("好きな蔵元を選んでください", breweries)

# 蔵元IDを取得
breweryId = [breweries["id"] for breweries in breweries_response["breweries"] if breweries["name"]==select_breweries][0]

# 銘柄名を取得
brands_response = requests.get(urls.get("銘柄一覧")).json()
brands = [brands["name"] for brands in brands_response["brands"] if brands["breweryId"]==breweryId]
select_brands = st.sidebar.selectbox("好きな銘柄を選んでください", brands)

# 銘柄IDを取得
brandId = [brands["id"] for brands in brands_response["brands"] if brands["name"]==select_brands][0]

# フレーバーチャートを取得
flavor_charts_response = requests.get(urls.get("フレーバーチャート")).json()
flavor_charts = [flavor_charts for flavor_charts in flavor_charts_response["flavorCharts"] if flavor_charts["brandId"]==brandId]

# plotlyでレーダーチャートを表示
st.markdown(f'## {select_brands}のフレーバーチャート')

try:
    df = pd.DataFrame(flavor_charts)
    df = df.drop('brandId', axis=1)
    # 見やすくするためにカラム名を変更、その後plotlyで読み込めるようにデータを転置
    df = df.rename(columns={'f1':'華やか', 'f2':'芳醇', 'f3':'重厚', 'f4':'穏やか', 'f5':'ドライ', 'f6':'軽快'}).T
    fig = px.line_polar(df, r=df[0], theta=df.index, line_close=True, range_r=[0,1])
    st.plotly_chart(fig)

# フレーバーチャートのデータがないものもあるので例外処理
except:
    st.markdown('## この銘柄はフレーバーチャートを表示できません！！')


import requests
import json

class BaiduTrans:
    def __init__(self, trans_str):
        self.trans_str = trans_str
        self.lang_detect_url = "https://fanyi.baidu.com/langdetect"
        self.headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Mobile Safari/537.36"}
        self.trans_url = "https://fanyi.baidu.com/basetrans"

    def parse_url(self, url, data):
        res = requests.post(url, data=data, headers=self.headers)
        return json.loads(res.content.decode())

    def get_res(self, dict_res):
        res = dict_res["trans"][0]["dst"]
        print("结果：", res)

    def run(self):
        lang_detect_data = {"query": self.trans_str}
        # 获取语言类型
        lang = self.parse_url(self.lang_detect_url, lang_detect_data)["lan"]
        # 准备post数据
        trans_data = {"query": self.trans_str, "from":"zh", "to":"en"} if lang=="zh" else {"query": self.trans_str, "from":"en", "to":"zh"}
        dict_res = self.parse_url(self.trans_url, trans_data)
        self.get_res(dict_res)

def main():
    my_str = input("输入：")
    bf = BaiduTrans(my_str)
    bf.run()

if __name__ == '__main__':
    main()


from os import name
from django.conf import settings
from django.urls import path,re_path
from . import views
from django.conf.urls.static import static

urlpatterns = [
  path('',views.index,name='home'),
  path('upload/image/',views.upload_post,name='NewPost'),
  path('update/',views.update_profile,name='UpdateProfile'),
  re_path('like/(?P<image_id>\d+)',views.like_post,name='LikePost'),
  path('create_profile/',views.create_profile,name='create_profile'),
  re_path('comment/(?P<image_id>\d+)',views.comment,name='AddComment'),
  re_path('profile/(?P<profile_id>\d+)',views.profile,name='profile'),
  path('search/',views.search,name='Search'),
  path('email/',views.welcome_mail,name='email'),
  re_path('single/(?P<image_id>\d+)',views.single_post,name='single-post')
]

if settings.DEBUG:
  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import pygame
import sys
import copy
from Board.board import Board
from Board.tile import Tile
from Pieces.NullPiece import NullPiece
from image import Image

import bot

def check_events(screen,board):
    mx = 0
    my = 0
    for event in pygame.event.get():
        if  event.type == pygame.QUIT :
            board.end_game = True
        # or event.key == pygame.K_ESCAPE :
            sys.exit()
        if  event.type == pygame.MOUSEBUTTONDOWN and board.selectedStartTile.tileCoordinate == []:
            board.mx,board.my = pygame.mouse.get_pos()
            for i in range(8):
                for j in range(8):
                    if board.gameTiles[i][j].pos[0]<board.mx<board.gameTiles[i][j].pos[0]+60:
                        if board.gameTiles[i][j].pos[1]<board.my<board.gameTiles[i][j].pos[1]+60:
                            board.selectedStartTile = board.gameTiles[i][j]
            if  board.selectedStartTile.pieceOnTile.symbol() != " " and\
                board.selectedStartTile.pieceOnTile.alliance == board.turn:
                board.legalMoves = board.selectedStartTile.pieceOnTile.availableMoves(board)
                # print(board.legalMoves)
                for key, value in board.default.items():
                    if  board.selectedStartTile.pieceOnTile.position == value:
                        board.start = key
                # can ham de loai bo nuoc khong hop le
                checkLegal(board)
                #====================
                print(board.start,end = "->")
                print(board.legalMoves)
                print("available moves : ",end="")
                for key, value in board.default.items():
                    for move in board.legalMoves:
                        if(move == value):
                                print(key,end = " ")
                print("\n",end = "")
            elif (board.selectedStartTile.pieceOnTile.symbol != " " and \
                board.selectedStartTile.pieceOnTile.alliance != board.turn) or\
                board.selectedStartTile.pieceOnTile.symbol == " ":
                board.selectedStartTile = Tile([],NullPiece([]))
            else :
                board.selectedStartTile = Tile([],NullPiece([]))
            updateScreen(screen,board)
            # pygame.display.flip()
            # print("vao day")
        elif  event.type == pygame.MOUSEBUTTONDOWN and board.selectedStartTile.tileCoordinate\
            != [] :
            if len(board.legalMoves)>0:
                board.mx, board.my = pygame.mouse.get_pos()
                for i in range(8):
                    for j in range(8):
                        if board.gameTiles[i][j].pos[0]<board.mx<board.gameTiles[i][j].pos[0]+60:
                            if board.gameTiles[i][j].pos[1]<board.my<board.gameTiles[i][j].pos[1]+60:
                                board.selectedEndTile = board.gameTiles[i][j]
                                # print(board.gameTiles[i][j].pieceOnTile.symbol())
                                # print(board.selectedEndTile.tileCoordinate)
                check = 0
                for move in board.legalMoves:
                    if  move == board.selectedEndTile.tileCoordinate:
                        for key, value in board.default.items():
                            if move == value:
                                board.end = key
                                # print(board.end)
                        board.updateMove(board.start,board.end)
                        kingPosition(board)
                        updateScreen(screen,board)
                        print(board.selectedEndTile.pieceOnTile.symbol()+board.start+board.end)
                        board.selectedStartTile = Tile([],NullPiece([]))
                        board.selectedEndTile = Tile([],NullPiece([]))
                        board.legalMoves = []
                        check = 1
                        board.printBoard()
                if check == 0 :
                    board.selectedStartTile = Tile([],NullPiece([]))
                    board.selectedEndTile = Tile([],NullPiece([]))
                    board.legalMoves = []
                    updateScreen(screen,board)
            elif len(board.legalMoves) == 0 :
                board.selectedEndTile = Tile([],NullPiece([]))
                board.selectedStartTile = Tile([],NullPiece([]))
                board.legalMoves = []
                updateScreen(screen,board)
            if  whiteKingCheck(board) == True :
                if checkMate(board) == True:
                    board.end_game = True
                else :
                    print("White King is checked ")
            elif blackKingCheck(board) == True :
                if checkMate(board) == True:
                    board.end_game = True
                else :
                    print("Black King is checked")
            if checkStalemate(board) == True :
                board.end_game = True
            bot.evaluate(board)
            print("White turn over, Board Analysis : "+str(-board.evaluation))
#=============================KING CHECK=====================================
def kingPosition(board):
    for piece in board.pieces :
        if  piece[0].symbol() == "K" :
            board.whiteKing = [piece[0],piece[0].position]
        elif piece[0].symbol() == "k":
            board.blackKing = [piece[0],piece[0].position]
            # print(board.blackKing)
def whiteKingCheck(board):
    kingPosition(board)

    for piece in board.pieces:
        if  piece[0].symbol() != "K" and piece[0].alliance != 1:
            moves = piece[0].availableMoves(board)
            # print("vao day")
            # print("test")
            # print(moves)
            # if len(moves) != 0:
            for move in moves :
                if move == board.whiteKing[1] :
                    # print("test white king check"+piece[0].symbol()+"->"+str(move))
                    board.whiteKing[0].check = True
                    return True
    board.whiteKing[0].check = False
    return False
def blackKingCheck(board):
    kingPosition(board)

    for piece in board.pieces:
        if  piece[0].symbol() != "k" and piece[0].alliance != -1:
            moves = piece[0].availableMoves(board)
            # print("test")
            # print(moves)
            # print(piece[0].symbol())
            # if  len(moves) != 0:
            for move in moves :
                if move == board.blackKing[1] :
                    # print("test black king check"+piece[0].symbol()+"->"+str(move))
                    board.blackKing[0].check = True
                    return True
    board.blackKing[0].check = False
    return False
def checkLegal(board):
    temp = Board()
    # print("legal move ")
    delete = []
    if  board.turn == 1 :
        for move in board.legalMoves :
            temp = copy.deepcopy(board)
            for x,y in temp.default.items():
                if  y == move :
                    temp.updateMove(temp.start, x)
                    # temp.printBoard()
                    temp.updatePieceForCheck()
                    if whiteKingCheck(temp) == True :
                        delete.append(move)
    elif board.turn == -1 :
        for move in board.legalMoves :
            temp = copy.deepcopy(board)
            for x,y in temp.default.items():
                if  y == move :
                    temp.updateMove(temp.start, x)
                    # temp.printBoard()
                    temp.updatePieceForCheck()
                    if  blackKingCheck(temp) == True :
                        delete.append(move)
    for move in delete:
        board.legalMoves.remove(move)
    # print(board.legalMoves)
def checkMate(board):
    temp = Board()
    count = 0
    if  board.turn == 1 and whiteKingCheck(board) == True:
        for piece in board.pieces:
            if piece[0].alliance == 1:
                board.legalMoves = piece[0].availableMoves(board)
            for x, y in board.default.items():
                if y == piece[0].position:
                    board.start = x
            checkLegal(board)
            count += len(board.legalMoves)
            for move in board.legalMoves:
                temp = copy.deepcopy(board)
                for x,y in temp.default.items():
                    if y == move:
                        temp.updateMove(temp.start,x)
                        temp.updatePieceForCheck()
                        if  whiteKingCheck(temp) == True :
                            print("Checkmate! Black wins the game")
                            return True
                        else:
                            return False
        if  count == 0 :
            print("Checkmate! Black wins the game")
            return True
    if  board.turn == -1 and blackKingCheck(board) == True:
        for piece in board.pieces:
            if piece[0].alliance == -1:
                board.legalMoves = piece[0].availableMoves(board)
            for x, y in board.default.items():
                if y == piece[0].position:
                    board.start = x
            checkLegal(board)
            count += len(board.legalMoves)
            for move in board.legalMoves:
                temp = copy.deepcopy(board)
                for x,y in temp.default.items():
                    if y == move:
                        temp.updateMove(temp.start,x)
                        # temp.printBoard()
                        temp.updatePieceForCheck()
                        if  blackKingCheck(temp) == True :
                            print("Checkmate! White wins the game")
                            return True
                        else:
                            return False
        if  count == 0 :
            print("Checkmate! White wins the game")
            return True
def checkStalemate(board):
    temp = Board()
    count = 0
    if  board.turn == 1 and whiteKingCheck(board) == False:
        for piece in board.pieces:
            if piece[0].alliance == 1:
                board.legalMoves = piece[0].availableMoves(board)
            checkLegal(board)
            count += len(board.legalMoves)
            if count != 0 :
                return False
        if  count == 0 :
            print("Stalemate! White turn")
            return True
    if  board.turn == -1 and blackKingCheck(board) == False:
        for piece in board.pieces:
            if piece[0].alliance == -1:
                board.legalMoves = piece[0].availableMoves(board)
            checkLegal(board)
            count += len(board.legalMoves)
            if count != 0 :
                return False
        if  count == 0 :
            print("Stalemate! Black turn")
            return True
#=============================UPDATE SCREEN==================================
def squares(screen,x,y,w,h,color):
    pygame.draw.rect(screen, color, [x,y,w,h])

def drawChess(screen,board):
    xpos = 0
    ypos = 0
    color = 0
    width = 60
    height = 60
    # black = (60,134,244)
    black  = (128,128,128)
    # white = (143,155,175)
    white = (255,153,153)
    orange = (255,165,0)
    pieces = []
    for i in range(8):
        for j in range(8):
            board.gameTiles[i][j].pos.append(xpos)
            board.gameTiles[i][j].pos.append(ypos)
            if color % 2 == 0 :
                if  board.selectedStartTile.tileCoordinate == [i,j] or ([i,j] in board.legalMoves == True):
                    squares(screen,xpos,ypos,width,height,orange)
                else:
                    squares(screen,xpos,ypos,width,height,white)
                if board.gameTiles[i][j].pieceOnTile.symbol() != " ":
                    pieces.append([board.gameTiles[i][j].pieceOnTile,[xpos,ypos]])
                xpos += 60
            else :
                if  board.selectedStartTile.tileCoordinate == [i,j] or ([i,j] in board.legalMoves == True):
                    squares(screen,xpos,ypos,width,height,orange)
                else:
                    squares(screen,xpos,ypos,width,height,black)
                if board.gameTiles[i][j].pieceOnTile.symbol() != " ":
                    pieces.append([board.gameTiles[i][j].pieceOnTile,[xpos,ypos]])
                xpos += 60
            color += 1
        color += 1
        xpos = 0
        ypos += 60
    return pieces

def updateScreen(screen,board):
    board.pieces = drawChess(screen,board)
    temp = Image()
    array = []
    for piece in board.pieces:
        array.append([temp.getImage(piece[0].symbol()),piece[1]])
    for img in array:
        screen.blit(img[0],img[1])

    # for img in board.pieces:
    #     screen.blit(img[0].image,img[1])

#============================MOVE=============================================
def getAvailableMove(board,position):
    kingPosition(board)
    if board.gameTiles[position[0]][position[1]].pieceOnTile.symbol != " ":
        moves = board.gameTiles[position[0]][position[1]].pieceOnTile.availableMoves(board)
        return moves
    else :
        print("Null Piece")
        return

def getMove(board):
    print("GET PLAYER MOVE : ")
    print("start : ")
    start = input()
    if board.gameTiles[board.default[start][0]][board.default[start][1]].pieceOnTile.symbol() == " ":
        print("Null Piece")
        return 9,9
    moves = getAvailableMove(board,board.default[start])
    print(moves)
    print("available moves : ",end='')
    for move in moves :
        for key, value  in board.default.items():
            if move == value:
                print(key,end =' ')
    print("",end = '\n')
    print("end : ")
    end = input()
    for move in moves :
        if board.default[end] == move and start != end:
            return start, end
    print("inValid Move")
    return 9,9


# -*- coding: utf-8 -*-
"""
Created on Sun Sep 29 20:10:31 2019

@author: evan9
"""

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('THfinish_edited.csv')
x = data.iloc[:, 0]
y = data.iloc[:, 1]

fig = plt.figure(figsize=(10,6))
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot(x, y, color='blue', lw=0, marker='o', markersize=8)
axes.set_ylim([0, 3])
axes.axvline(x=0.580, color='black', ls='--')
axes.axvline(x=0.924, color='black', ls='--')
axes.axhline(y=2.070, color='red', ls='--')

axes.set_xlabel('Stimulus intensity (mV)', fontsize=16)
axes.set_ylabel('Response (mV)', fontsize=16)
axes.set_title('Threshold & MSI', fontsize=24)

plt.show()
#fig.savefig('Threshold MSI2.png', dpi=200)

2a AnimalLab/1.CAP/Threshold MSI Plot.py


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from datetime import date, datetime

from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import View, CreateView, UpdateView, DeleteView, ListView, DetailView
from clubManagement.models import Attendance, Responsibility
from registration.models import UserInfo

month = ["January", "February", "March", "April", "May", "June", "July", "August", "September",
         "October", "November", "December"]

month_num = range(12)

def calculate_year(year):
    year = datetime.now().year - year
    if datetime.now().month > 5:
        year += 1
    print(year)
    if year == 1:
        return '1st year'
    elif year == 2:
        return '2nd year'
    elif year == 3:
        return '3rd year'
    elif year == 4:
        return '4th year'
    else:
        return 'Alumni - ' + str(year + datetime.now().year) + ' batch'

def get_batch_list(kwargs):
    batches = []
    if 'batch' in kwargs:
        batches += [int(kwargs.get('batch'))]
    else:
        year = datetime.now().year
        if datetime.now().month < 5:
            year -= 1
        for i in range(4):
            batches += [year - i]
    print(batches)
    return batches

class AttendanceAddView(View):
    template_name = 'clubManagement/attendance_add.html'

    def get(self, request, **kwargs):
        """
        A view for an admin to add attendance for a particular date.
        url = /batch/year/month/day/
        or
        url = /year/month/day (display 1st - 4th year)
        date is calculated from (year, month, day)
        and students is selected according to batch

        attendance_list = [ [ '1st_year', list_of_1st_years ], ..... ]
        """
        if not request.user.is_authenticated:
            return redirect('permission_denied')

        d = date(int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')))
        context = {}
        batch_list = get_batch_list(kwargs)

        attendance_list = []

        for batch in batch_list:
            user_info_list = UserInfo.objects.filter(year=batch)
            # display the current attendance for this date and batch
            attendance_list_batch = []
            for user_info in user_info_list:
                try:
                    attendance = Attendance.objects.get(user=user_info.user, date=d)
                except Attendance.DoesNotExist:
                    attendance = Attendance(user=user_info.user,
                                            added_by=User.objects.get(username=self.request.user.username), date=d)
                    attendance.save()
                attendance_list_batch.append(attendance)

            # attendance list contains all the Attendance objects of the batch with date = d
            year = calculate_year(batch)
            attendance_list += [[attendance_list_batch, year], ]

        context = {'attendance_list': attendance_list}
        print(context)
        return render(request, self.template_name, context)

    def post(self, request, **kwargs):
        if not request.user.is_superuser:
            return redirect('permission_denied')

        d = date(int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')))

        if 'batch' in kwargs:
            user_info_list = UserInfo.objects.filter(year=int(kwargs.get('batch')))
            for user_info in user_info_list:
                attendance_list = user_info.user.attendance_set.filter(date=d)
                for i in attendance_list:
                    i.attendance = False
                    i.save()
        else:
            # make all attendance false and make attendance = true for users in request.POST.
            attendance_list = Attendance.objects.filter(date=d)
            for i in attendance_list:
                i.attendance = False
                i.save()

        for key in request.POST:
            try:
                user = User.objects.get(username=key)
            except User.DoesNotExist:
                user = None
            if user:
                att = Attendance.objects.get(user=user, date=d)
                att.attendance = True
                att.save()
        if 'batch' in kwargs:
            return redirect('add_attendance_batch', **kwargs)
        else:
            return redirect('add_attendance_all', **kwargs)

class YearStudentAttendanceReportView(View):
    template_name = 'clubManagement/attendance_yearly.html'

    def get(self, request, **kwargs):
        user = User.objects.get(id=int(kwargs.get('user_id')))
        att = user.attendance_set.filter(date__year=int(kwargs.get('year')))
        if len(att) > 0:
            month_att = []
            for i in month_num:
                month_att.append(len(att.filter(date__month=(i + 1))))
            context = {'user': user, 'month_att': month_att, 'month': month, 'month_num': month_num,
                       'year': kwargs.get('year')}
        else:
            context = {'errors', 'No records found'}
        return render(request, self.template_name, context)

class YearAttendanceReportView(View):
    """
    context = {'data_list': data_list}
    where:
        data_list = [ [user_data, year, error], ....]
        user_data = [[user, month_att, total_att], ......]
        month_att = list of attendance for 12 months
    """
    template_name = 'clubManagement/attendance_batch_yearly.html'

    def get(self, request, **kwargs):
        batch_list = get_batch_list(kwargs)
        data_list = []
        for batch in batch_list:
            user_info_list = UserInfo.objects.filter(year=batch)
            user_data = []
            for user_info in user_info_list:
                month_att = []
                total_att = 0
                for i in month_num:
                    att_month = len(
                        user_info.user.attendance_set.filter(
                            date__year=int(kwargs.get('year')),
                            date__month=(i + 1),
                            attendance=True
                        )
                    )
                    total_att += att_month
                    month_att.append(att_month)
                user_data.append([user_info.user, month_att, total_att])
            year = calculate_year(batch)
            if len(user_data) > 0:
                data_list.append([user_data, year, ''])
            else:
                data_list.append([user_data, year, 'No record found'])
        context = {'data_list': data_list}
        return render(request, self.template_name, context)

# Responsibilities
# CreateView and UpdateView calls get_absolute_url() on the model to get the success_url
class ResponsibilityListView(ListView):
    model = Responsibility

class ResponsibilityDetailView(DetailView):
    model = Responsibility

class ResponsibilityCreateView(CreateView):
    model = Responsibility
    fields = ['created_by', 'name', 'description']

    def form_valid(self, form):
        form.instance.created_by = self.request.user
        return super(ResponsibilityCreateView, self).form_valid(form)

class ResponsibilityUpdateView(UpdateView):
    model = Responsibility
    fields = ['name', 'description']

class ResponsibilityDeleteView(DeleteView):
    model = Responsibility
    success_url = reverse_lazy('responsibility')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('minions', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='paperwork',
            name='emp_docs',
            field=models.CharField(blank=True, max_length=3, null=True, verbose_name=b'IDs Received?', choices=[(b'yes', b'yes'), (b'no', b'no')]),
            preserve_default=True,
        ),
    ]


# coding=utf-8
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
                        unicode_literals, with_statement)

import Queue as queue
import threading
import traceback
from collections import defaultdict, deque
from heapq import heappop, heappush

from pants.base.worker_pool import Work

class Job(object):
  """A unit of scheduling for the ExecutionGraph.

  The ExecutionGraph represents a DAG of dependent work. A Job a node in the graph along with the
  keys of its dependent jobs.
  """

  def __init__(self, key, fn, dependencies, size=0, on_success=None, on_failure=None):
    """

    :param key: Key used to reference and look up jobs
    :param fn callable: The work to perform
    :param dependencies: List of keys for dependent jobs
    :param size: Estimated job size used for prioritization
    :param on_success: Zero parameter callback to run if job completes successfully. Run on main
                       thread.
    :param on_failure: Zero parameter callback to run if job completes successfully. Run on main
                       thread."""
    self.key = key
    self.fn = fn
    self.dependencies = dependencies
    self.size = size
    self.on_success = on_success
    self.on_failure = on_failure

  def __call__(self):
    self.fn()

  def run_success_callback(self):
    if self.on_success:
      self.on_success()

  def run_failure_callback(self):
    if self.on_failure:
      self.on_failure()

UNSTARTED = 'Unstarted'
QUEUED = 'Queued'
SUCCESSFUL = 'Successful'
FAILED = 'Failed'
CANCELED = 'Canceled'

class StatusTable(object):
  DONE_STATES = {SUCCESSFUL, FAILED, CANCELED}

  def __init__(self, keys, pending_dependencies_count):
    self._statuses = {key: UNSTARTED for key in keys}
    self._pending_dependencies_count = pending_dependencies_count

  def mark_as(self, state, key):
    self._statuses[key] = state

  def mark_queued(self, key):
    self.mark_as(QUEUED, key)

  def unfinished_items(self):
    """Returns a list of (name, status) tuples, only including entries marked as unfinished."""
    return [(key, stat) for key, stat in self._statuses.items() if stat not in self.DONE_STATES]

  def failed_keys(self):
    return [key for key, stat in self._statuses.items() if stat == FAILED]

  def is_unstarted(self, key):
    return self._statuses.get(key) is UNSTARTED

  def mark_one_successful_dependency(self, key):
    self._pending_dependencies_count[key] -= 1

  def is_ready_to_submit(self, key):
    return self.is_unstarted(key) and self._pending_dependencies_count[key] == 0

  def are_all_done(self):
    return all(s in self.DONE_STATES for s in self._statuses.values())

  def has_failures(self):
    return any(stat is FAILED for stat in self._statuses.values())

class ExecutionFailure(Exception):
  """Raised when work units fail during execution"""

  def __init__(self, message, cause=None):
    if cause:
      message = "{}: {}".format(message, str(cause))
    super(ExecutionFailure, self).__init__(message)
    self.cause = cause

class UnexecutableGraphError(Exception):
  """Base exception class for errors that make an ExecutionGraph not executable"""

  def __init__(self, msg):
    super(UnexecutableGraphError, self).__init__("Unexecutable graph: {}".format(msg))

class NoRootJobError(UnexecutableGraphError):
  def __init__(self):
    super(NoRootJobError, self).__init__(
      "All scheduled jobs have dependencies. There must be a circular dependency.")

class UnknownJobError(UnexecutableGraphError):
  def __init__(self, undefined_dependencies):
    super(UnknownJobError, self).__init__("Undefined dependencies {}"
                                          .format(", ".join(map(repr, undefined_dependencies))))

class JobExistsError(UnexecutableGraphError):
  def __init__(self, key):
    super(JobExistsError, self).__init__("Job already scheduled {!r}"
                                          .format(key))

class ThreadSafeCounter(object):
  def __init__(self):
    self.lock = threading.Lock()
    self._counter = 0

  def get(self):
    with self.lock:
      return self._counter

  def increment(self):
    with self.lock:
      self._counter += 1

  def decrement(self):
    with self.lock:
      self._counter -= 1

class ExecutionGraph(object):
  """A directed acyclic graph of work to execute.

  This is currently only used within jvm compile, but the intent is to unify it with the future
  global execution graph.
  """

  def __init__(self, job_list):
    """

    :param job_list Job: list of Jobs to schedule and run.
    """
    self._dependencies = defaultdict(list)
    self._dependees = defaultdict(list)
    self._jobs = {}
    self._job_keys_as_scheduled = []
    self._job_keys_with_no_dependencies = []

    for job in job_list:
      self._schedule(job)

    unscheduled_dependencies = set(self._dependees.keys()) - set(self._job_keys_as_scheduled)
    if unscheduled_dependencies:
      raise UnknownJobError(unscheduled_dependencies)

    if len(self._job_keys_with_no_dependencies) == 0:
      raise NoRootJobError()

    self._job_priority = self._compute_job_priorities(job_list)

  def format_dependee_graph(self):
    return "\n".join([
      "{} -> {{\n  {}\n}}".format(key, ',\n  '.join(self._dependees[key]))
      for key in self._job_keys_as_scheduled
    ])

  def _schedule(self, job):
    key = job.key
    dependency_keys = job.dependencies
    self._job_keys_as_scheduled.append(key)
    if key in self._jobs:
      raise JobExistsError(key)
    self._jobs[key] = job

    if len(dependency_keys) == 0:
      self._job_keys_with_no_dependencies.append(key)

    self._dependencies[key] = dependency_keys
    for dependency_key in dependency_keys:
      self._dependees[dependency_key].append(key)

  def _compute_job_priorities(self, job_list):
    """Walks the dependency graph breadth-first, starting from the most dependent tasks,
     and computes the job priority as the sum of the jobs sizes along the critical path."""

    job_size = {job.key: job.size for job in job_list}
    job_priority = defaultdict(int)

    bfs_queue = deque()
    for job in job_list:
      if len(self._dependees[job.key]) == 0:
        job_priority[job.key] = job_size[job.key]
        bfs_queue.append(job.key)

    satisfied_dependees_count = defaultdict(int)
    while len(bfs_queue) > 0:
      job_key = bfs_queue.popleft()
      for dependency_key in self._dependencies[job_key]:
        job_priority[dependency_key] = \
          max(job_priority[dependency_key],
              job_size[dependency_key] + job_priority[job_key])
        satisfied_dependees_count[dependency_key] += 1
        if satisfied_dependees_count[dependency_key] == len(self._dependees[dependency_key]):
          bfs_queue.append(dependency_key)

    return job_priority

  def execute(self, pool, log):
    """Runs scheduled work, ensuring all dependencies for each element are done before execution.

    :param pool: A WorkerPool to run jobs on
    :param log: logger for logging debug information and progress

    submits all the work without any dependencies to the worker pool
    when a unit of work finishes,
      if it is successful
        calls success callback
        checks for dependees whose dependencies are all successful, and submits them
      if it fails
        calls failure callback
        marks dependees as failed and queues them directly into the finished work queue
    when all work is either successful or failed,
      cleans up the work pool
    if there's an exception on the main thread,
      calls failure callback for unfinished work
      aborts work pool
      re-raises
    """
    log.debug(self.format_dependee_graph())

    status_table = StatusTable(self._job_keys_as_scheduled,
                               {key: len(self._jobs[key].dependencies) for key in self._job_keys_as_scheduled})
    finished_queue = queue.Queue()

    heap = []
    jobs_in_flight = ThreadSafeCounter()

    def put_jobs_into_heap(job_keys):
      for job_key in job_keys:
        # minus because jobs with larger priority should go first
        heappush(heap, (-self._job_priority[job_key], job_key))

    def try_to_submit_jobs_from_heap():
      def worker(worker_key, work):
        try:
          work()
          result = (worker_key, SUCCESSFUL, None)
        except Exception as e:
          result = (worker_key, FAILED, e)
        finished_queue.put(result)
        jobs_in_flight.decrement()

      while len(heap) > 0 and jobs_in_flight.get() < pool.num_workers:
        priority, job_key = heappop(heap)
        jobs_in_flight.increment()
        status_table.mark_queued(job_key)
        pool.submit_async_work(Work(worker, [(job_key, (self._jobs[job_key]))]))

    def submit_jobs(job_keys):
      put_jobs_into_heap(job_keys)
      try_to_submit_jobs_from_heap()

    try:
      submit_jobs(self._job_keys_with_no_dependencies)

      while not status_table.are_all_done():
        try:
          finished_key, result_status, value = finished_queue.get(timeout=10)
        except queue.Empty:
          log.debug("Waiting on \n  {}\n".format("\n  ".join(
            "{}: {}".format(key, state) for key, state in status_table.unfinished_items())))
          try_to_submit_jobs_from_heap()
          continue

        finished_job = self._jobs[finished_key]
        direct_dependees = self._dependees[finished_key]
        status_table.mark_as(result_status, finished_key)

        # Queue downstream tasks.
        if result_status is SUCCESSFUL:
          try:
            finished_job.run_success_callback()
          except Exception as e:
            log.debug(traceback.format_exc())
            raise ExecutionFailure("Error in on_success for {}".format(finished_key), e)

          ready_dependees = []
          for dependee in direct_dependees:
            status_table.mark_one_successful_dependency(dependee)
            if status_table.is_ready_to_submit(dependee):
              ready_dependees.append(dependee)

          submit_jobs(ready_dependees)
        else:  # Failed or canceled.
          try:
            finished_job.run_failure_callback()
          except Exception as e:
            log.debug(traceback.format_exc())
            raise ExecutionFailure("Error in on_failure for {}".format(finished_key), e)

          # Propagate failures downstream.
          for dependee in direct_dependees:
            if status_table.is_unstarted(dependee):
              status_table.mark_queued(dependee)
              finished_queue.put((dependee, CANCELED, None))

        # Log success or failure for this job.
        if result_status is FAILED:
          log.error("{} failed: {}".format(finished_key, value))
        else:
          log.debug("{} finished with status {}".format(finished_key, result_status))
    except ExecutionFailure:
      raise
    except Exception as e:
      # Call failure callbacks for jobs that are unfinished.
      for key, state in status_table.unfinished_items():
        self._jobs[key].run_failure_callback()
      log.debug(traceback.format_exc())
      raise ExecutionFailure("Error running job", e)

    if status_table.has_failures():
      raise ExecutionFailure("Failed jobs: {}".format(', '.join(status_table.failed_keys())))


#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
extrait les interviews mais sur les dossiers (permet de voir la progression)
Nécéssite d'avoir le dossier interview et les fichiers d'interview, donné par le script extractInterview.py

usage : python3 extractNE-2.py [language=fr]
sortie : Out-EN-polyglott.txt

si pb:
pip3 install pycld2
pip3 install morfessor
pip3 install pyicu
pip3 install pycld2

puis :
polyglot download embeddings2.en
polyglot download ner2.en
polyglot download embeddings2.fr
polyglot download ner2.fr

'''

import os
import re
import sys
from datetime import datetime
from termcolor import colored
from polyglot.text import Text as Ptext
from tqdm import tqdm

try:
    lang = sys.argv[1]
except IndexError:
    print('en français')
    lang = 'fr'

rep = './Interviews/'
lisInterview = os.listdir(rep)
pbar = tqdm(total=len(lisInterview))

out = open("Out-EN-polyglott.txt", 'w')
namedEntities = []

for file in lisInterview:

    ptext = Ptext(open(rep + file).read(), hint_language_code=lang)
    ne = ptext.entities
    namedEntities = namedEntities + ne
    for e in ne: out.write(str(e) + '\n')
    pbar.update()

out.close()

print(colored(namedEntities[:-100],'green'))
print(str(len(namedEntities)) + ' entités nommées trouvées')
print("fichier sortie : Out-EN-polyglott.txt")


# Example adapted from http://flask.pocoo.org/docs/0.12/patterns/fileuploads/
# @NOTE: The code below is for educational purposes only.
# Consider using the more secure version at the link above for production apps
import matplotlib.pyplot as plt
from sklearn import tree
from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
import pickle
import os
from keras.models import load_model
from flask import Flask, request, render_template,json

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'

def train_model():
    arr = []
    df = pd.read_csv("stats1.csv")
    target_names = df["TD"]
    data = df.drop("TD", axis=1)
    feature_names = data.columns

    data = df.values
    X = data[:, 0:11]
    y = data[:, 11]
    y = y.astype('int')
    X = X.astype('int')

    model = LinearRegression()
    model.fit(X, y)
    score = model.score(X, y)
    # print(f"R2 Score: {score}")
    arr.append(X)
    arr.append(y)
    return arr

@app.route('/data')
def landing_page():

    df = pd.read_csv("stats2.csv")

    # a=[]
    # a.append(df.to_json(orient='records', lines=True))
    # a
    response = app.response_class(
    # response=json.dumps(df.to_json(orient='index')),
    response=df.to_json(orient='index'),

    status=200,
    mimetype='application/json'
    )
    return response

@app.route('/', methods=['GET', 'POST'])
def webprint():
    arr = train_model()

    # load model
    filename = 'finalized_model.sav'
    loaded_model = pickle.load(open(filename, 'rb'))
    result = loaded_model.score(arr[0], arr[1])
    print(result)
    # make prediction
    li = []

    atemp = request.form.get('att')
    comp = request.form.get('cmp')
    perc = request.form.get('pct')
    yarsd = request.form.get('yds')
    yardsatt = request.form.get('ypa')
    inter = request.form.get('inter')
    interper = request.form.get('intpct')
    longth = request.form.get('lg')
    sack = request.form.get('sack')
    loss = request.form.get('loss')
    rate = request.form.get('rate')
    tchdper = request.form.get('tprc')

    li.append(atemp)
    li.append(comp)
    li.append(perc)
    li.append(yarsd)
    li.append(yardsatt)
    li.append(tchdper)
    li.append(inter)
    li.append(interper)
    li.append(longth)
    li.append(sack)
    li.append(loss)
    li.append(rate)
    mat = np.array(li)

    x = np.array(['1.1', '2.2', '3.3'])
    y = x.astype(np.float)

    mat_con = mat.astype(np.float)

    # print(loaded_model.predict([mat_con]))

    if request.method =='POST':
        # print(loaded_model.predict([y]))
        print(mat_con)
        model = load_model("td_predict.h5")

        val = model.predict([[mat_con]])
        data = {"Predicted Touchdowns":str(val), "Model Type": "Sequential","Loaded Model":"td_predict.h5", "Epochs": "500"}
        print(data)

        response = app.response_class(
        response=json.dumps(data),
        status=200,
        mimetype='application/json'
        )
        return response

    return render_template('index.html')

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


import csv
import os
from typing import (Any, List)

from fedot.core.utils import default_fedot_data_dir

def write_composer_history_to_csv(historical_chains: List[Any], file='history.csv'):
    history_dir = os.path.join(default_fedot_data_dir(), 'composing_history')
    file = f'{history_dir}/{file}'
    if not os.path.isdir(history_dir):
        os.mkdir(history_dir)
    write_header_to_csv(file)
    i = 0
    for gen_num, gen_chains in enumerate(historical_chains):
        for chain in gen_chains:
            add_history_to_csv(file, chain.fitness, len(chain.nodes), chain.depth, i, gen_num)
            i += 1

def write_header_to_csv(f):
    with open(f, 'w', newline='') as file:
        writer = csv.writer(file, quoting=csv.QUOTE_ALL)
        writer.writerow(['num', 'generation', 'fitness, num_of_models, depth'])

def add_history_to_csv(f, fitness: float, models_num: int, depth: int, num: int = None, generation: int = None):
    with open(f, 'a', newline='') as file:
        writer = csv.writer(file, quoting=csv.QUOTE_ALL)
        writer.writerow([num, generation, fitness, models_num, depth])


#model test
import os
import numpy as np
from sklearn.externals import joblib
from bert_serving.client import BertClient
from bratreader.repomodel import RepoModel
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from model_com import create_base_network, get_events, fit_on_data, test_on_data, get_events_in_mention

def testing(DIR_DATA):
    print('\n\nTesting {}>>>\ndata importing:'.format(DIR_DATA))
    TASK_NAME = DIR_DATA
    NAME_DATA_FILE = TASK_NAME+'_data_import'+'.save'
    DIR_MODEL = './save/'
    file_model_trig = DIR_MODEL + TASK_NAME +'_model_trigger.pkl'
    file_model_arg = DIR_MODEL + TASK_NAME + '_model_arg.pkl'

    triggers, vec_trig, label_trig, args, vec_arg, label_arg = [], [], [], [], [], []
    try:
        triggers, vec_trig, label_trig, args, vec_arg, label_arg = joblib.load(NAME_DATA_FILE)
    except:
        ANN_FILEs = []
        DIR_ALL_FILES = os.listdir(DIR_DATA)
        for file_name in DIR_ALL_FILES:
            if file_name.split('.')[-1] == 'txt':
                ANN_FILEs.append(file_name[:-4])
        corpus = RepoModel(DIR_DATA) # load corpus
        bc = BertClient(ip='127.0.0.1', port=8701, port_out=8702, show_server_config=True) # bert model as service
        for ANN_FILE in ANN_FILEs:
            doc = corpus.documents[ANN_FILE] # get document with key
            ttriggers, tvec_trig, tlabel_trig, targs, tvec_arg, tlabel_arg, label_arg_for_each_trig = get_events_in_mention(doc, bc)
            triggers.extend(ttriggers)
            vec_trig.extend(tvec_trig)
            label_trig.extend(tlabel_trig)
            args.extend(targs)
            vec_arg.extend(tvec_arg)
            label_arg.extend(tlabel_arg)
        print('trigs:', len(vec_trig), 'args:', len(vec_arg))
        joblib.dump([triggers, vec_trig, label_trig, args, vec_arg, label_arg], NAME_DATA_FILE)

    X_train, X_test, Y_train, Y_test = train_test_split(vec_trig, label_trig, random_state=0)
    print('='*65,'\n>>trigger model testing:')
    model_trig, encoder_trig = joblib.load(file_model_trig)
    acc = test_on_data(model_trig, encoder_trig, vec_trig, label_trig, DIR_DATA, en_verbose = 1)
    print('triggers extraction accuracy: {}'.format(acc))

    X_train, X_test, Y_train, Y_test = train_test_split(vec_arg, label_arg, random_state=0)
    print('='*65,'\n>>argument model testing:')
    model_arg, encoder_arg = joblib.load(file_model_arg)
    acc = test_on_data(model_arg, encoder_arg, vec_arg, label_arg, DIR_DATA, en_verbose = 1)
    print('arguements extraction accuracy: {}'.format(acc))

DIR_DATAs = ['data_ACE_Chinese']
for DIR_DATA in DIR_DATAs:
    testing(DIR_DATA)


import sys
import time
import glob
import os
import argparse
import Extractor as extractor
import ConfigPaths as config
import CrossReference as crossref
import TextWriter as text_writer
from DatabaseFunctionalityModules.DatabaseConnection import add_data
from DatabaseFunctionalityModules.ID_Fixer import fix_pub_and_author_id
from Authors import build_author_list

input_dir = os.path.join(os.getcwd(), 'inputs/')
output_dir = os.path.join(os.getcwd(), 'outputs/')
pdffigures2_dir = config.pdffigures2_dir
thread_count = config.thread_count
pdfs_are_main_pubs = True

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--secondary', help='PDF Files have some secondary publications', action='store_true')
    args = parser.parse_args()
    print(args)

    if args.secondary:
        print("PDF FIles are not all mains or are secondaries")
        pdfs_are_main_pubs = False

    else:
        print("PDF Files are mains")
        pdfs_are_main_pubs = True

    init_start = time.time()

    is_ready_to_run = extractor.check_resources()

    if is_ready_to_run:
        "remove spaces from names of PDF's since spaces causes pdffigures2 to skip pdf"
        os.chdir(input_dir)
        for file in glob.glob("*.pdf"):
            extractor.remove_space(file)

        print("GROBID extracting text, metadata and references")
        try:
            extractor.data_extractor(input_dir, output_dir)
        except Exception as e:
            print(e)
            sys.exit("GROBID encountered an error")

        print("PDFFigures2.0 extracting figures and figure captions")

        try:
            extractor.extract_figures(input_dir, output_dir, pdffigures2_dir, thread_count)
        except Exception as e:
            print(e)
            sys.exit("PDFFigures encountered an error")

        print("Parsing XML and JSON Files")
        try:
            devices = extractor.parse_output_files(output_dir)
        except Exception as e:
            print(e)
            sys.exit("Parser encountered an error")

        connections = {}
        authors = []
        if config.should_init_crossrefs:
            start = time.time()
            connections = crossref.initialize_connections(devices, pdfs_are_main_pubs)
            finish = time.time()
            print("Initialized Connections in %f seconds" % (finish - start))
        if config.add_to_db:
            authors = build_author_list(devices)
            add_data(devices, connections, authors, pdfs_are_main_pubs)
            fix_pub_and_author_id()
        if config.writeToFile:
            start = time.time()
            text_writer.writeFiles(devices, connections, authors)
            finish = time.time()
            print("Wrote Files in %f seconds" % (finish - start))

        extractor.clean_output_folder(output_dir)
    finish = time.time()
    print("---------- Total Time Taken: %f ---------------------" % (finish - init_start))


import pygame as pg
import sys
from os import path
from settings import *
from sprites import *
from tilemap import *
import json
import random

class Game(Player):
    def __init__(self, loop, file="maths.json"):
        self.loop = loop
        pg.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        self.display = self.screen  # for menu screen
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        pg.key.set_repeat(500, 100)
        self.question = -1
        self.game_over = False
        self.load_data(file)
        self.show_question = True
        self.font = pg.font.Font('freesansbold.ttf', 32)
        self.score_value = 0
        self.timer = 0
        self.point_coords = []
        self.correct_answer = 0
        self.countdown = 15
        self.new()

    game_over_image = pg.image.load(path.join('assets', 'gameoverscreen.png'))

    def load_data(self, file="maths.json"):
        game_folder = path.dirname(__file__)
        self.map = Map(path.join(game_folder, 'map.txt'))
        with open(file) as load_questions:#opens and then closes json file after usage.
            self.questions = json.load(load_questions)

    def new(self):
        # initialize all variables and do all the setup for a new game
        self.all_sprites = pg.sprite.Group()
        self.walls = pg.sprite.Group()
        self.points = pg.sprite.Group()
        self.point_list = [Point(self, 0), Point(self, 1), Point(self, 2)]

        for row, tiles in enumerate(self.map.data):
            for col, tile in enumerate(tiles):
                if tile == '1':
                    Wall(self, col, row)
                if tile == 'P':
                    self.player = Player(self, col, row)

                if tile == 'D':
                    self.point_coords.append((col, row))

        print(self.point_coords)
        # self.camera = Camera(self.map.width, self.map.height)
        #self.playing = True
        self.new_question()

    def tick(self):
        # game loop - set self.playing = False to end the
        self.dt = self.clock.tick(FPS) / 1000

        self.timer += self.dt
        self.events()
        self.update()
        self.draw()

    def quit(self):
        pg.quit()
        sys.exit()

    def update(self):
        # update portion of the game loop
        if self.game_over is True:
            pass
        else:
            self.all_sprites.update()
            # self.camera.update(self.player)  # can put any sprite you want for camera to track

            if self.show_question:
                # putting it in show question allows user to know right answer even after the game has finished
                if self.countdown <= 0:
                    # update question screen
                    pass
            else:
                 #draw answer

                self.new_question()

                     #shows, new question after 0.5 seconds
                pass

    def draw_grid(self):
        for x in range(0, WIDTH, TILESIZE):
            pg.draw.line(self.screen, BLACK, (x, 0), (x, HEIGHT))
        for y in range(0, HEIGHT, TILESIZE):
            pg.draw.line(self.screen, BLACK, (0, y), (WIDTH, y))

    def draw_question(self):
        current_question = self.questions["questions"][self.questions["order"][self.question]]["question"]
        answers = self.questions["questions"][self.questions["order"][self.question]]["answer"]
        questions_text = self.font.render(current_question, True, WHITE)
        self.screen.blit(questions_text, (WIDTH // 2 - questions_text.get_width()//2, 0))
        answers = answers[3 - self.correct_answer:3] + answers[0:3 - self.correct_answer] # randomizes the answer selection, self.correct_answer = correct index
        for answer in range(len(answers)):
            answers_text = self.font.render(answers[answer], True, WHITE)
            self.screen.blit(answers_text, (answer * WIDTH // len(answers) + 100, HEIGHT - TILESIZE * 2))
        print (answers)

    def new_question(self):
        self.show_question = True
        self.question += 1 #"order" in json will go up by one.
        self.correct_answer = random.randrange(3)
        current_choice = random.sample(self.point_coords, 3)
        for x in range(3):
            self.point_list[x].move_point(current_choice[x])

    def next_question(self, choice):
        if self.show_question == True:
            self.show_question = False
            if choice == self.correct_answer:
                self.score_value += 1
                self.countdown += 5
            else:
                self.score_value -= 1
            print(self.score_value)

    def draw(self):

        if self.countdown <= 0 and self.show_question is True or self.game_over is True:
            finalscore = self.font.render(str(self.score_value), True, YELLOW)

            self.screen.blit(self.game_over_image, [0, 0])
            self.screen.blit(finalscore, [400, 500])  # MOVE TO THE CORRECT POSITION
            self.game_over = True

        else:
            self.screen.fill(BGCOLOR)
            self.draw_grid()
            for sprite in self.all_sprites:
                # self.screen.blit(sprite.image, self.camera.apply(sprite))
                self.screen.blit(sprite.image, sprite)
                # iterates over each sprite, applying transformation to them.
            self.draw_UI()
        pg.display.flip()

    def draw_UI(self):
        # countdown timer

        self.countdown -= self.dt
        displaycountdown = math.trunc(self.countdown)
        # scorepointer
        text = self.font.render('Score: ' + str(self.score_value), True, GREEN)
        textRect = text.get_rect()
        countdowntext = self.font.render(str(displaycountdown), True, GREEN)

        pg.draw.rect(self.screen, BLACK, (0, 0, WIDTH, TILESIZE * 3))
        pg.draw.rect(self.screen, BLACK, (0, HEIGHT - TILESIZE * 3, WIDTH, TILESIZE * 3))
        pg.draw.rect(self.screen, BLUE, (0, HEIGHT - TILESIZE * 3, 200, 200))
        pg.draw.rect(self.screen, RED, (400, HEIGHT - TILESIZE * 3, 200, 200))
        pg.draw.rect(self.screen, LIGHTGREY, (750, HEIGHT - TILESIZE * 3, 200, 200))

        self.screen.blit(text, textRect)
        self.screen.blit(countdowntext, [980, 1])

        if self.show_question:
            # draw the question
            self.draw_question()
        else:
            # draws correct answer
            pass

    def events(self):
        # catch all events here
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.quit()
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    self.quit()
                elif event.key == pg.K_RETURN and self.game_over is True:
                    self.loop.state = "menu"


import image_recognition
import json
import base64
import cv2
from flask import Flask
from flask import request
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/imageRecognition', methods=['POST'])
def image_recog():
    print(request.json)

    img_base64 = request.json['image']
    img = base64.b64decode(img_base64)
    bb=image_recognition.ir.img_rec(img)
    return json.dumps({'result': bb})

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        f.save('/tmp/tmp.jpg')
        img = cv2.imread('/tmp/tmp.jpg')
        bb=image_recognition.ir.img_rec(img)

        return json.dumps({'result': bb})
    return 'Hello, World!'


import math
import csv
import os
import shutil
import json
import enum
from glob import glob

from os.path import join as _join
from os.path import exists as _exists

from copy import deepcopy
from collections import Counter

# non-standard
import jsonpickle
import numpy as np
import pandas as pd
import multiprocessing

# wepppy
from wepppy.landcover import LandcoverMap

from wepppy.all_your_base import isfloat, isint, YearlessDate, probability_of_occurrence

from wepppy.wepp import Element
from wepppy.climates.cligen import ClimateFile

from wepppy.watershed_abstraction import SlopeFile

# wepppy submodules
from wepppy.nodb.log_mixin import LogMixin
from wepppy.nodb.base import NoDbBase
from wepppy.nodb.mods import RangelandCover
from wepppy.nodb.watershed import Watershed
from wepppy.nodb.soils import Soils
from wepppy.nodb.topaz import Topaz
from wepppy.nodb.climate import Climate

from wepppy.nodb.mods import Baer
from wepppy.nodb.wepp import Wepp

# Copyright (c) 2016-2018, University of Idaho
# All rights reserved.
#
# Roger Lew (rogerlew@gmail.com)
#
# The project described was supported by NSF award number IIA-1301792
# from the NSF Idaho EPSCoR Program and by the National Science Foundation.

# standard library
import os
import math
from enum import IntEnum
from os.path import join as _join
from os.path import exists as _exists
from os.path import split as _split

from subprocess import Popen, PIPE
import multiprocessing
from concurrent.futures import ThreadPoolExecutor, as_completed, wait, FIRST_EXCEPTION

from datetime import datetime
import time

import pickle

from glob import glob

import shutil

# non-standard
import jsonpickle

import numpy as np

from osgeo import osr
from osgeo import gdal
from osgeo.gdalconst import *

import wepppy

# wepppy

from wepppy.rhem import make_parameter_file, make_hillslope_run, run_hillslope

from wepppy.climates.cligen import ClimateFile

from wepppy.wepp.stats import ChannelWatbal, HillslopeWatbal, ReturnPeriods, SedimentDelivery

from .rhempost import RhemPost

try:
    NCPU = int(os.environ['WEPPPY_NCPU'])
except KeyError:
    NCPU = math.floor(multiprocessing.cpu_count() * 0.5)
    if NCPU < 1:
        NCPU = 1

class RhemNoDbLockedException(Exception):
    pass

class Rhem(NoDbBase, LogMixin):
    __name__ = 'Rhem'

    def __init__(self, wd, cfg_fn):
        super(Rhem, self).__init__(wd, cfg_fn)

        self.lock()

        # noinspection PyBroadException
        try:
            rhem_dir = self.rhem_dir
            if not _exists(rhem_dir):
                os.mkdir(rhem_dir)

            config = self.config
            self.clean()

            self.dump_and_unlock()

        except Exception:
            self.unlock('-f')
            raise

    @property
    def rhem_dir(self):
        return _join(self.wd, 'rhem')

    @property
    def runs_dir(self):
        return _join(self.wd, 'rhem', 'runs')

    @property
    def output_dir(self):
        return _join(self.wd, 'rhem', 'output')

    @property
    def status_log(self):
        return os.path.abspath(_join(self.runs_dir, 'status.log'))

    #
    # Required for NoDbBase Subclass
    #

    # noinspection PyPep8Naming
    @staticmethod
    def getInstance(wd):
        with open(_join(wd, 'rhem.nodb')) as fp:
            db = jsonpickle.decode(fp.read())
            assert isinstance(db, Rhem)

            if _exists(_join(wd, 'READONLY')):
                return db

            if os.path.abspath(wd) != os.path.abspath(db.wd):
                db.wd = wd
                db.lock()
                db.dump_and_unlock()

            return db

    @property
    def _nodb(self):
        return _join(self.wd, 'rhem.nodb')

    @property
    def _lock(self):
        return _join(self.wd, 'rhem.nodb.lock')

    @property
    def has_run(self):
        return len(glob(_join(self.output_dir, '*.sum'))) > 0

    #
    # hillslopes
    #
    def prep_hillslopes(self):
        self.log('Prepping Hillslopes... ')
        wd = self.wd

        watershed = Watershed.getInstance(wd)

        soils = Soils.getInstance(wd)
        rangeland_covers = RangelandCover.getInstance(wd)
        climate = Climate.getInstance(self.wd)
        cli_dir = self.cli_dir
        wat_dir = self.wat_dir
        runs_dir = self.runs_dir
        out_dir = self.output_dir

        for topaz_id, summary in watershed.sub_iter():
            mukey = soils.domsoil_d[topaz_id]
            soil_texture = soils.soils[mukey].texture
            slp_fn = _join(wat_dir, 'hill_{}.slp'.format(topaz_id))
            slp = SlopeFile(slp_fn)
            cover = rangeland_covers.covers[topaz_id]

            scn_name = 'hill_{}'.format(topaz_id)
            par_fn = make_parameter_file(scn_name=scn_name,
                                         out_dir=runs_dir,
                                         soil_texture=soil_texture,
                                         moisture_content=0.25,
                                         bunchgrass_cover=cover['bunchgrass'],
                                         forbs_cover=cover['forbs'],
                                         shrubs_cover=cover['shrub'],
                                         sodgrass_cover=cover['sodgrass'],
                                         rock_cover=cover['rock'],
                                         basal_cover=cover['basal'],
                                         litter_cover=cover['litter'],
                                         cryptogams_cover=cover['cryptogams'],
                                         slope_length=slp.length,
                                         slope_steepness=slp.slope_scalar,
                                         sl=slp.slopes,
                                         sx=slp.distances,
                                         width=summary.width,
                                         model_version='WEPPcloud')

            stm_fn = _join(runs_dir, 'hill_{}.stm'.format(topaz_id))

            cli_summary = climate.sub_summary(topaz_id)
            cli_path = _join(cli_dir, cli_summary['cli_fn'])
            climate_file = ClimateFile(cli_path)
            climate_file.make_storm_file(stm_fn)

            run_fn = _join(runs_dir, 'hill_{}.run'.format(topaz_id))
            make_hillslope_run(run_fn, par_fn, stm_fn, _join(out_dir, 'hill_{}.sum'.format(topaz_id)), scn_name)

        self.log_done()

    def clean(self):
        if _exists(self.status_log):
            os.remove(self.status_log)

        runs_dir = self.runs_dir
        if _exists(runs_dir):
            shutil.rmtree(runs_dir)
        os.mkdir(runs_dir)

        output_dir = self.output_dir
        if _exists(output_dir):
            shutil.rmtree(output_dir)
        os.mkdir(output_dir)

    def run_hillslopes(self):
        self.log('Running Hillslopes\n')
        watershed = Watershed.getInstance(self.wd)
        runs_dir = os.path.abspath(self.runs_dir)

        pool = ThreadPoolExecutor(NCPU)
        futures = []

        def oncomplete(rhemrun):
            status, _id, elapsed_time = rhemrun.result()
            assert status
            self.log('  {} completed run in {}s\n'.format(_id, elapsed_time))

        sub_n = watershed.sub_n
        for i, (topaz_id, _) in enumerate(watershed.sub_iter()):
            self.log('  submitting topaz={} (hill {} of {})\n'.format(topaz_id, i + 1, sub_n))
            futures.append(pool.submit(lambda p: run_hillslope(*p), (topaz_id, runs_dir)))
            futures[-1].add_done_callback(oncomplete)

        wait(futures, return_when=FIRST_EXCEPTION)

        self.log('Running RhemPost... ')
        rhempost = RhemPost.getInstance(self.wd)
        rhempost.run_post()

        try:
            from wepppy.weppcloud import RunStatistics
            rs = RunStatistics.getInstance('/geodata/weppcloud_runs')
            rs.increment_hillruns(watershed.config_stem, watershed.sub_n)
        except Exception:
            pass

        self.log_done()

    def report_loss(self):
        output_dir = self.output_dir

        raise NotImplementedError

    def report_return_periods(self):
        output_dir = self.output_dir

        raise NotImplementedError


from aiohttp import web
import aiohttp_jinja2
import jinja2

class WebApplication(web.Application):
    def __init__(self, bot, *args, **kwargs):
        super().__init__(
            middlewares=[web.normalize_path_middleware()],
            *args, **kwargs
        )
        self.bot = bot

        self.add_routes([web.static("/static", "./static")])
        aiohttp_jinja2.setup(self, loader=jinja2.FileSystemLoader('./templates'))

    async def start(self, *args, **kwargs):
        runner = web.AppRunner(self)
        await runner.setup()
        site = web.TCPSite(runner, *args, **kwargs)
        await site.start()


from skimage.metrics import structural_similarity
from os.path import exists
from os import listdir
from os.path import isfile, join
from pyautogui import *
from pynput import keyboard
from libs.resources_new import data
from libs.resources_new import monitor_settings

import time
import pyautogui

import os
import mss
import mss.tools
import math
import pyperclip

# myScreenshot = pyautogui.screenshot()
# myScreenshot.save(r'file name.png')

if (os.name == "posix"):
    from AppKit import NSScreen
    from AppKit import NSWorkspace
else:
    import win32gui
    from win32api import GetSystemMetrics

combat = False
debug = False
mill = False
dprint = False
pause = True
wow_class = "warrior"
wow_class_loaded = wow_class

if os.name == "posix":
    screen_width = NSScreen.mainScreen().frame().size.width
    screen_height = NSScreen.mainScreen().frame().size.height
else:
    screen_width = GetSystemMetrics(0)
    screen_height = GetSystemMetrics(1)

monitor = str(screen_width)

x = monitor_settings[monitor]["x"]
y = monitor_settings[monitor]["y"]
c_width = monitor_settings[monitor]["c_width"]
c_height = monitor_settings[monitor]["c_height"]
p_offgcd_left = monitor_settings[monitor]["p_offgcd_left"]
p_combat_left = monitor_settings[monitor]["p_combat_left"]
p_interrupt_left = monitor_settings[monitor]["p_interrupt_left"]
p_behind_left = monitor_settings[monitor]["p_behind_left"]
p_clss_left = monitor_settings[monitor]["p_clss_left"]

file_path = os.path.abspath(__file__)
dir_path = os.path.dirname(os.path.realpath(__file__))

if os.name == "posix":
    abilities_folder = dir_path + "/images/" + monitor
    slash = "/"
else:
    abilities_folder = dir_path + "\images\\" + monitor
    slash = "\\"

if not os.path.exists(abilities_folder):
    os.makedirs(abilities_folder)

skills_loaded = "warrior"
print("Script loaded and ready.", "Monitor:", screen_width, screen_height, os.name)

def on_press(key):
    global debug, dprint, pause, mill

    try:
        if key == keyboard.Key.f12:
            debug = not debug
            print("debug:", debug)
    except:
        return

def parse_hex_color(string):
    if string.startswith("#"):
        string = string[1:]
    r = int(string[0:2], 16)  # red color value
    g = int(string[2:4], 16)  # green color value
    b = int(string[4:6], 16)  # blue color value
    return r, g, b

def color_similarity(base_col_val, oth_col_val):
    return math.sqrt(sum((base_col_val[i]-oth_col_val[i])**2 for i in range(3)))

def print_debug(no, xclass, skill, image):
    # no = '{0: <4}'.format(no)
    # xclass = '{0: <15}'.format(xclass)
    # skill = '{0: <25}'.format(skill)
    # image = '{0: <100}'.format(image)
    print(no, xclass, skill)

classes = {
    "warrior", "druid", "rogue", "warlock", "mage", "hunter", "death knight", "priest", "paladin", "shaman"
}

time1 = 0.2
time2 = 0.1

count = 0
number = 0

s, t = "", ""
color_distance = 10

def get_class(clss, color_distance):
    found_class = False
    global wow_class
    for item in data["colors"]:
        for c in data["colors"][item]:
            rgb = parse_hex_color(c)
            if color_similarity(rgb, clss) <= color_distance:
                found_class = True
                wow_class = item

    return found_class, wow_class

debug_folder = dir_path + "/images/_/"
x = 100
y = 100
with keyboard.Listener(on_press=on_press) as listener:

    with mss.mss() as sct:

        while True:
            time.sleep(1)

            p_main = {"top": 1000, "left": 500, "width": x, "height": y}
            # p_secondary = {"top": 2, "left":  p_offgcd_left, "width": x, "height": y}

            if debug:
                print({"top": screen_width-(x/2), "left": screen_height-(y/2), "width": x, "height": y}, screen_width, screen_height)
                main_image = sct.grab(p_main)
                # secondary_iamge = sct.grab(p_secondary)
                mss.tools.to_png(main_image.rgb, main_image.size, output=debug_folder + "99. main.png".format(**p_main))
                # mss.tools.to_png(secondary_iamge.rgb, secondary_iamge.size, output=debug_folder + "100. secondary.png".format(**p_secondary))


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

import datetime
import decimal
import functools
import warnings

try:
    import pandas
except ImportError:  # pragma: NO COVER
    pandas = None
try:
    import pyarrow
    import pyarrow.types
except ImportError:  # pragma: NO COVER
    pyarrow = None
import pytest
import pytz

from google.cloud.bigquery import schema

@pytest.fixture
def module_under_test():
    from google.cloud.bigquery import _pandas_helpers

    return _pandas_helpers

def is_none(value):
    return value is None

def is_datetime(type_):
    # See: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#datetime-type
    return all_(
        pyarrow.types.is_timestamp,
        lambda type_: type_.unit == "us",
        lambda type_: type_.tz is None,
    )(type_)

def is_numeric(type_):
    # See: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#numeric-type
    return all_(
        pyarrow.types.is_decimal,
        lambda type_: type_.precision == 38,
        lambda type_: type_.scale == 9,
    )(type_)

def is_timestamp(type_):
    # See: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#timestamp-type
    return all_(
        pyarrow.types.is_timestamp,
        lambda type_: type_.unit == "us",
        lambda type_: type_.tz == "UTC",
    )(type_)

def do_all(functions, value):
    return all((func(value) for func in functions))

def all_(*functions):
    return functools.partial(do_all, functions)

@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_is_datetime():
    assert is_datetime(pyarrow.timestamp("us", tz=None))
    assert not is_datetime(pyarrow.timestamp("ms", tz=None))
    assert not is_datetime(pyarrow.timestamp("us", tz="UTC"))
    assert not is_datetime(pyarrow.string())

def test_do_all():
    assert do_all((lambda _: True, lambda _: True), None)
    assert not do_all((lambda _: True, lambda _: False), None)
    assert not do_all((lambda _: False,), None)

def test_all_():
    assert all_(lambda _: True, lambda _: True)(None)
    assert not all_(lambda _: True, lambda _: False)(None)

@pytest.mark.parametrize(
    "bq_type,bq_mode,is_correct_type",
    [
        ("STRING", "NULLABLE", pyarrow.types.is_string),
        ("STRING", None, pyarrow.types.is_string),
        ("string", "NULLABLE", pyarrow.types.is_string),
        ("StRiNg", "NULLABLE", pyarrow.types.is_string),
        ("BYTES", "NULLABLE", pyarrow.types.is_binary),
        ("INTEGER", "NULLABLE", pyarrow.types.is_int64),
        ("INT64", "NULLABLE", pyarrow.types.is_int64),
        ("FLOAT", "NULLABLE", pyarrow.types.is_float64),
        ("FLOAT64", "NULLABLE", pyarrow.types.is_float64),
        ("NUMERIC", "NULLABLE", is_numeric),
        ("BOOLEAN", "NULLABLE", pyarrow.types.is_boolean),
        ("BOOL", "NULLABLE", pyarrow.types.is_boolean),
        ("TIMESTAMP", "NULLABLE", is_timestamp),
        ("DATE", "NULLABLE", pyarrow.types.is_date32),
        ("TIME", "NULLABLE", pyarrow.types.is_time64),
        ("DATETIME", "NULLABLE", is_datetime),
        ("GEOGRAPHY", "NULLABLE", pyarrow.types.is_string),
        ("UNKNOWN_TYPE", "NULLABLE", is_none),
        # Use pyarrow.list_(item_type) for repeated (array) fields.
        (
            "STRING",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_string(type_.value_type),
            ),
        ),
        (
            "STRING",
            "repeated",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_string(type_.value_type),
            ),
        ),
        (
            "STRING",
            "RePeAtEd",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_string(type_.value_type),
            ),
        ),
        (
            "BYTES",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_binary(type_.value_type),
            ),
        ),
        (
            "INTEGER",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_int64(type_.value_type),
            ),
        ),
        (
            "INT64",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_int64(type_.value_type),
            ),
        ),
        (
            "FLOAT",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_float64(type_.value_type),
            ),
        ),
        (
            "FLOAT64",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_float64(type_.value_type),
            ),
        ),
        (
            "NUMERIC",
            "REPEATED",
            all_(pyarrow.types.is_list, lambda type_: is_numeric(type_.value_type)),
        ),
        (
            "BOOLEAN",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_boolean(type_.value_type),
            ),
        ),
        (
            "BOOL",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_boolean(type_.value_type),
            ),
        ),
        (
            "TIMESTAMP",
            "REPEATED",
            all_(pyarrow.types.is_list, lambda type_: is_timestamp(type_.value_type)),
        ),
        (
            "DATE",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_date32(type_.value_type),
            ),
        ),
        (
            "TIME",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_time64(type_.value_type),
            ),
        ),
        (
            "DATETIME",
            "REPEATED",
            all_(pyarrow.types.is_list, lambda type_: is_datetime(type_.value_type)),
        ),
        (
            "GEOGRAPHY",
            "REPEATED",
            all_(
                pyarrow.types.is_list,
                lambda type_: pyarrow.types.is_string(type_.value_type),
            ),
        ),
        ("RECORD", "REPEATED", is_none),
        ("UNKNOWN_TYPE", "REPEATED", is_none),
    ],
)
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_bq_to_arrow_data_type(module_under_test, bq_type, bq_mode, is_correct_type):
    field = schema.SchemaField("ignored_name", bq_type, mode=bq_mode)
    actual = module_under_test.bq_to_arrow_data_type(field)
    assert is_correct_type(actual)

@pytest.mark.parametrize("bq_type", ["RECORD", "record", "STRUCT", "struct"])
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_bq_to_arrow_data_type_w_struct(module_under_test, bq_type):
    fields = (
        schema.SchemaField("field01", "STRING"),
        schema.SchemaField("field02", "BYTES"),
        schema.SchemaField("field03", "INTEGER"),
        schema.SchemaField("field04", "INT64"),
        schema.SchemaField("field05", "FLOAT"),
        schema.SchemaField("field06", "FLOAT64"),
        schema.SchemaField("field07", "NUMERIC"),
        schema.SchemaField("field08", "BOOLEAN"),
        schema.SchemaField("field09", "BOOL"),
        schema.SchemaField("field10", "TIMESTAMP"),
        schema.SchemaField("field11", "DATE"),
        schema.SchemaField("field12", "TIME"),
        schema.SchemaField("field13", "DATETIME"),
        schema.SchemaField("field14", "GEOGRAPHY"),
    )
    field = schema.SchemaField("ignored_name", bq_type, mode="NULLABLE", fields=fields)
    actual = module_under_test.bq_to_arrow_data_type(field)
    expected = pyarrow.struct(
        (
            pyarrow.field("field01", pyarrow.string()),
            pyarrow.field("field02", pyarrow.binary()),
            pyarrow.field("field03", pyarrow.int64()),
            pyarrow.field("field04", pyarrow.int64()),
            pyarrow.field("field05", pyarrow.float64()),
            pyarrow.field("field06", pyarrow.float64()),
            pyarrow.field("field07", module_under_test.pyarrow_numeric()),
            pyarrow.field("field08", pyarrow.bool_()),
            pyarrow.field("field09", pyarrow.bool_()),
            pyarrow.field("field10", module_under_test.pyarrow_timestamp()),
            pyarrow.field("field11", pyarrow.date32()),
            pyarrow.field("field12", module_under_test.pyarrow_time()),
            pyarrow.field("field13", module_under_test.pyarrow_datetime()),
            pyarrow.field("field14", pyarrow.string()),
        )
    )
    assert pyarrow.types.is_struct(actual)
    assert actual.num_children == len(fields)
    assert actual.equals(expected)

@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_bq_to_arrow_data_type_w_struct_unknown_subfield(module_under_test):
    fields = (
        schema.SchemaField("field1", "STRING"),
        schema.SchemaField("field2", "INTEGER"),
        # Don't know what to convert UNKNOWN_TYPE to, let type inference work,
        # instead.
        schema.SchemaField("field3", "UNKNOWN_TYPE"),
    )
    field = schema.SchemaField("ignored_name", "RECORD", mode="NULLABLE", fields=fields)
    actual = module_under_test.bq_to_arrow_data_type(field)
    assert actual is None

@pytest.mark.parametrize(
    "bq_type,rows",
    [
        ("STRING", ["abc", None, "def", None]),
        ("BYTES", [b"abc", None, b"def", None]),
        ("INTEGER", [123, None, 456, None]),
        ("INT64", [-9223372036854775808, None, 9223372036854775807, 123]),
        ("FLOAT", [1.25, None, 3.5, None]),
        (
            "NUMERIC",
            [
                decimal.Decimal("-99999999999999999999999999999.999999999"),
                None,
                decimal.Decimal("99999999999999999999999999999.999999999"),
                decimal.Decimal("999.123456789"),
            ],
        ),
        ("BOOLEAN", [True, None, False, None]),
        ("BOOL", [False, None, True, None]),
        # TODO: Once https://issues.apache.org/jira/browse/ARROW-5450 is
        # resolved, test with TIMESTAMP column. Conversion from pyarrow
        # TimestampArray to list of Python objects fails with OverflowError:
        # Python int too large to convert to C long.
        #
        # (
        #     "TIMESTAMP",
        #     [
        #         datetime.datetime(1, 1, 1, 0, 0, 0, tzinfo=pytz.utc),
        #         None,
        #         datetime.datetime(9999, 12, 31, 23, 59, 59, 999999, tzinfo=pytz.utc),
        #         datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=pytz.utc),
        #     ],
        # ),
        (
            "DATE",
            [
                datetime.date(1, 1, 1),
                None,
                datetime.date(9999, 12, 31),
                datetime.date(1970, 1, 1),
            ],
        ),
        (
            "TIME",
            [
                datetime.time(0, 0, 0),
                None,
                datetime.time(23, 59, 59, 999999),
                datetime.time(12, 0, 0),
            ],
        ),
        # TODO: Once https://issues.apache.org/jira/browse/ARROW-5450 is
        # resolved, test with DATETIME column. Conversion from pyarrow
        # TimestampArray to list of Python objects fails with OverflowError:
        # Python int too large to convert to C long.
        #
        # (
        #     "DATETIME",
        #     [
        #         datetime.datetime(1, 1, 1, 0, 0, 0),
        #         None,
        #         datetime.datetime(9999, 12, 31, 23, 59, 59, 999999),
        #         datetime.datetime(1970, 1, 1, 0, 0, 0),
        #     ],
        # ),
        (
            "GEOGRAPHY",
            [
                "POINT(30 10)",
                None,
                "LINESTRING (30 10, 10 30, 40 40)",
                "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))",
            ],
        ),
    ],
)
@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_bq_to_arrow_array_w_nullable_scalars(module_under_test, bq_type, rows):
    series = pandas.Series(rows, dtype="object")
    bq_field = schema.SchemaField("field_name", bq_type)
    arrow_array = module_under_test.bq_to_arrow_array(series, bq_field)
    roundtrip = arrow_array.to_pylist()
    assert rows == roundtrip

@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_bq_to_arrow_array_w_arrays(module_under_test):
    rows = [[1, 2, 3], [], [4, 5, 6]]
    series = pandas.Series(rows, dtype="object")
    bq_field = schema.SchemaField("field_name", "INTEGER", mode="REPEATED")
    arrow_array = module_under_test.bq_to_arrow_array(series, bq_field)
    roundtrip = arrow_array.to_pylist()
    assert rows == roundtrip

@pytest.mark.parametrize("bq_type", ["RECORD", "record", "STRUCT", "struct"])
@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_bq_to_arrow_array_w_structs(module_under_test, bq_type):
    rows = [
        {"int_col": 123, "string_col": "abc"},
        None,
        {"int_col": 456, "string_col": "def"},
    ]
    series = pandas.Series(rows, dtype="object")
    bq_field = schema.SchemaField(
        "field_name",
        bq_type,
        fields=(
            schema.SchemaField("int_col", "INTEGER"),
            schema.SchemaField("string_col", "STRING"),
        ),
    )
    arrow_array = module_under_test.bq_to_arrow_array(series, bq_field)
    roundtrip = arrow_array.to_pylist()
    assert rows == roundtrip

@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_bq_to_arrow_array_w_special_floats(module_under_test):
    bq_field = schema.SchemaField("field_name", "FLOAT64")
    rows = [float("-inf"), float("nan"), float("inf"), None]
    series = pandas.Series(rows, dtype="object")
    arrow_array = module_under_test.bq_to_arrow_array(series, bq_field)
    roundtrip = arrow_array.to_pylist()
    assert len(rows) == len(roundtrip)
    assert roundtrip[0] == float("-inf")
    assert roundtrip[1] != roundtrip[1]  # NaN doesn't equal itself.
    assert roundtrip[2] == float("inf")
    assert roundtrip[3] is None

@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_to_arrow_w_required_fields(module_under_test):
    bq_schema = (
        schema.SchemaField("field01", "STRING", mode="REQUIRED"),
        schema.SchemaField("field02", "BYTES", mode="REQUIRED"),
        schema.SchemaField("field03", "INTEGER", mode="REQUIRED"),
        schema.SchemaField("field04", "INT64", mode="REQUIRED"),
        schema.SchemaField("field05", "FLOAT", mode="REQUIRED"),
        schema.SchemaField("field06", "FLOAT64", mode="REQUIRED"),
        schema.SchemaField("field07", "NUMERIC", mode="REQUIRED"),
        schema.SchemaField("field08", "BOOLEAN", mode="REQUIRED"),
        schema.SchemaField("field09", "BOOL", mode="REQUIRED"),
        schema.SchemaField("field10", "TIMESTAMP", mode="REQUIRED"),
        schema.SchemaField("field11", "DATE", mode="REQUIRED"),
        schema.SchemaField("field12", "TIME", mode="REQUIRED"),
        schema.SchemaField("field13", "DATETIME", mode="REQUIRED"),
        schema.SchemaField("field14", "GEOGRAPHY", mode="REQUIRED"),
    )
    dataframe = pandas.DataFrame(
        {
            "field01": ["hello", "world"],
            "field02": [b"abd", b"efg"],
            "field03": [1, 2],
            "field04": [3, 4],
            "field05": [1.25, 9.75],
            "field06": [-1.75, -3.5],
            "field07": [decimal.Decimal("1.2345"), decimal.Decimal("6.7891")],
            "field08": [True, False],
            "field09": [False, True],
            "field10": [
                datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=pytz.utc),
                datetime.datetime(2012, 12, 21, 9, 7, 42, tzinfo=pytz.utc),
            ],
            "field11": [datetime.date(9999, 12, 31), datetime.date(1970, 1, 1)],
            "field12": [datetime.time(23, 59, 59, 999999), datetime.time(12, 0, 0)],
            "field13": [
                datetime.datetime(1970, 1, 1, 0, 0, 0),
                datetime.datetime(2012, 12, 21, 9, 7, 42),
            ],
            "field14": [
                "POINT(30 10)",
                "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))",
            ],
        }
    )

    arrow_table = module_under_test.to_arrow(dataframe, bq_schema)
    arrow_schema = arrow_table.schema

    assert len(arrow_schema) == len(bq_schema)
    for arrow_field in arrow_schema:
        assert not arrow_field.nullable

@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_to_arrow_w_unknown_type(module_under_test):
    bq_schema = (
        schema.SchemaField("field00", "UNKNOWN_TYPE"),
        schema.SchemaField("field01", "STRING"),
        schema.SchemaField("field02", "BYTES"),
        schema.SchemaField("field03", "INTEGER"),
    )
    dataframe = pandas.DataFrame(
        {
            "field00": ["whoami", "whatami"],
            "field01": ["hello", "world"],
            "field02": [b"abd", b"efg"],
            "field03": [1, 2],
        }
    )

    with warnings.catch_warnings(record=True) as warned:
        arrow_table = module_under_test.to_arrow(dataframe, bq_schema)
    arrow_schema = arrow_table.schema

    assert len(warned) == 1
    warning = warned[0]
    assert "field00" in str(warning)

    assert len(arrow_schema) == len(bq_schema)
    assert arrow_schema[0].name == "field00"
    assert arrow_schema[1].name == "field01"
    assert arrow_schema[2].name == "field02"
    assert arrow_schema[3].name == "field03"

@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
def test_to_parquet_without_pyarrow(module_under_test, monkeypatch):
    monkeypatch.setattr(module_under_test, "pyarrow", None)
    with pytest.raises(ValueError) as exc:
        module_under_test.to_parquet(pandas.DataFrame(), (), None)
    assert "pyarrow is required" in str(exc)

@pytest.mark.skipIf(pandas is None, "Requires `pandas`")
@pytest.mark.skipIf(pyarrow is None, "Requires `pyarrow`")
def test_to_parquet_w_missing_columns(module_under_test, monkeypatch):
    with pytest.raises(ValueError) as exc:
        module_under_test.to_parquet(
            pandas.DataFrame(), (schema.SchemaField("not_found", "STRING"),), None
        )
    assert "columns in schema must match" in str(exc)


import requests
import os, sys, time
import parse_data_utils
from datetime import datetime

# ===========================================
# Grab update time information
# ===========================================

# Page only tells "Updated daily at 7 PM [CDT]"
# as of 4/19/2020. Thus, just take current date.
# Make sure to run this between 5 and 11:59 PM
# for Pacific time!

# Need date in format MM/DD/YYYY
date_parts = str(datetime.date(datetime.now())).split('-')
date = date_parts[1] + '/' + date_parts[2] + '/' + date_parts[0]

# ===========================================
# Grab current zip code-case counts data
# ===========================================

san_antonio_url = "https://services.arcgis.com/g1fRTDLeMgspWrYp/arcgis/rest/services/vBexarCountyZipCodes_EnrichClip/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&outFields=*&orderByFields=ZIP_CODE%20asc"
san_antonio_case_field = u'CasesP100000'

response = requests.get(san_antonio_url) # REST API query

try:
	all_zip_json = response.json()[u'features']
except:
	sys.exit("ERROR: JSON response does not have 'features' field. URL may be incorrect or field names may have changed.")

# Loading population data to estimate case counts
san_antonio_pop_data_path = os.path.abspath("./san_antonio_pop_by_zip.csv")
san_antonio_pop_data_file = open(san_antonio_pop_data_path, 'r')
san_antonio_pop_data_file.readline() # Get rid of header

zip_code_pop_dict = {}
for line in san_antonio_pop_data_file:
	values = line.strip().split(',')
	zip_code, pop = values[1], values[7]
	zip_code_pop_dict[zip_code] = int(pop)

san_antonio_pop_data_file.close()

zip_codes = [] # List of zip codes
case_counts = [] # List of case counts

for zip_json in all_zip_json:
	cases_per_100k = zip_json[u'attributes'][san_antonio_case_field]
	zip_code = zip_json[u'attributes'][u'ZIP_CODE']
	if zip_code in zip_code_pop_dict:
		case_count = int(round(cases_per_100k*zip_code_pop_dict[zip_code]/100000))
		case_counts.append(case_count)
		zip_codes.append(zip_code)

# ===========================================
# Store previous data in dictionary
# ===========================================

us_cases_rel_scripts_path = os.path.abspath("../processed_data/cases/US") # Path to data relative to scripts
prev_data_fpath = "%s/bexar-county_cases.csv" % us_cases_rel_scripts_path
prev_data_file = open(prev_data_fpath, 'r')
header = prev_data_file.readline().strip().split(',')

# IMPORTANT: Only append new data if date isn't already there
quoted_date = parse_data_utils.date_string_to_quoted(date)

if quoted_date in header:
	prev_data_file.close()
	sys.exit("This date has already been updated. May need to check if multiple updates were made in the same day.")

data = {} # Keys are zip codes, values are lists of case counts

for row in prev_data_file:
	values = row.strip().split(',')
	zip_code = values[0]
	case_counts_prev = values[1:]
	data[zip_code] = case_counts_prev

prev_data_file.close()

# ===========================================
# Add today's data and overwrite .csv
# ===========================================

num_dates = len(header) - 1 # Excludes today

for zip_code, case_count in zip(zip_codes, case_counts):
	quoted_zip_code = '"%s"' % zip_code
	if quoted_zip_code in data:
		data[quoted_zip_code].append(str(case_count))
	else: # New zip code
		data[quoted_zip_code] = ['NA'] * (num_dates)
		data[quoted_zip_code].append(str(case_count))

# Missing zip codes
for zip_code in data:
	if len(data[zip_code]) < (num_dates + 1):
		data[zip_code].append("NA")

# Make sure everything has the same length
# (There might be duplicates!)
for zip_code in data:
	if len(data[zip_code]) != (num_dates + 1):
		sys.exit("ERROR: Inconsistency in number of data points for zip code %s" % zip_code)

# Overwrite csv
data_file = open(prev_data_fpath, 'w')

header.append(quoted_date)
data_file.write(','.join(header) + "\n")
sorted_zip_codes = sorted(data.keys())

for zip_code in sorted_zip_codes:
	data_file.write('%s,' % zip_code)
	data_file.write(','.join(data[zip_code]) + "\n")

data_file.close()


# coding=utf-8
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django import forms

from solotodo.models import Product

class BudgetCreationForm(forms.Form):
    name = forms.CharField(max_length=255, label='Nombre')
    initial_product = forms.ModelChoiceField(
        queryset=Product.objects.all(), required=False,
        widget=forms.HiddenInput)
    next_url = forms.CharField(required=False, widget=forms.HiddenInput)

    def __init__(self, *args, **kwargs):
        super(BudgetCreationForm, self).__init__(*args, **kwargs)

        helper = FormHelper()
        helper.form_class = 'form-horizontal ' \
                            'form_without_actions_decoration ' \
                            'with_margin_top'
        helper.form_method = 'post'
        helper.form_action = '.'
        helper.add_input(Submit('submit', u'Crear cotización'))

        self.helper = helper


# https://pythonprogramming.net/python-3-tkinter-basics-tutorial/
import logging
import socket
from queue import Queue
from threading import Thread
from tkinter import *

from labo05.demo_networking.clientserver8.server.server import SommenServer

class ServerWindow(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()
        self.server = None
        self.thread_listener_queue=None
        self.init_messages_queue()

    # Creation of init_window
    def init_window(self):
        # changing the title of our master widget
        self.master.title("Server")

        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)

        Label(self, text="Log-berichten server:").grid(row=0)
        self.scrollbar = Scrollbar(self, orient=VERTICAL)
        self.lstnumbers = Listbox(self, yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.lstnumbers.yview)

        self.lstnumbers.grid(row=1, column=0, sticky=N + S + E + W)
        self.scrollbar.grid(row=1, column=1, sticky=N + S)

        self.btn_text = StringVar()
        self.btn_text.set("Start server")
        self.buttonServer = Button(self, textvariable=self.btn_text, command=self.start_stop_server)
        self.buttonServer.grid(row=3, column=0, columnspan=2, pady=(5, 5), padx=(5, 5), sticky=N + S + E + W)

        Grid.rowconfigure(self, 1, weight=1)
        Grid.columnconfigure(self, 0, weight=1)

    def start_stop_server(self):
        if self.server is not None:
            self.__stop_server()
        else:
            self.__start_server()

    def __stop_server(self):
        self.server.stop_server()
        self.server = None
        logging.info("Server stopped")
        self.btn_text.set("Start server")

    def __start_server(self):
        self.server = SommenServer(socket.gethostname(), 9999, self.messages_queue)
        self.server.init_server()
        self.server.start()  # in thread plaatsen!
        logging.info("Server started")
        self.btn_text.set("Stop server")

    def init_messages_queue(self):
        self.messages_queue = Queue()
        self.thread_listener_queue = Thread(target=self.print_messsages_from_queue, name="Queue_listener_thread", daemon=True)
        self.thread_listener_queue.start()

    def print_messsages_from_queue(self):
        message = self.messages_queue.get()
        while message != "CLOSE_SERVER":
            self.lstnumbers.insert(END, message)
            self.messages_queue.task_done()
            message = self.messages_queue.get()


import numpy as np
import shapely.geometry as geometry
import easyaccess as ea

con=ea.connect()

q2='SELECT f.RA_CENT,f.DEC_CENT,f.RAC1,f.RAC2,f.RAC3,f.RAC4,f.DECC1,f.DECC2,f.DECC3,f.DECC4,f.ID,d.TILENAME FROM RUMBAUGH.Y3A1_TILENAMES_LIST d,DES_ADMIN.Y3A1_COADDTILE_GEOM f WHERE d.tilename=f.tilename'

TDF=con.query_to_pandas(q2)

boxes={TDF['TILENAME'][i]: geometry.MultiPoint(list(np.array([[TDF['RAC1'][i],TDF['DECC1'][i]],[TDF['RAC2'][i],TDF['DECC2'][i]],[TDF['RAC3'][i],TDF['DECC3'][i]],[TDF['RAC4'][i],TDF['DECC4'][i]]]))).convex_hull for i in np.arange(0,np.shape(TDF)[0])}

TDFtilenames,TDFrac,TDFdecc=np.array(TDF['TILENAME']),np.array(TDF['RA_CENT']),np.array(TDF['DEC_CENT'])

qm='SELECT * from RUMBAUGH.SDSSPOSS_HPIX'
MDF=con.query_to_pandas(qm)

outcr=np.zeros((np.shape(MDF)[0],np.shape(MDF)[1]),dtype='|S20')
outcr[:,0],outcr[:,1],outcr[:,2]=np.array(MDF['SP_ROWNUM']),np.array(MDF['RA']),np.array(MDF['DEC'])

for i in range(0,np.shape(MDF)[0]):
    curmra,curmdec=MDF['RA'][i],MDF['DEC'][i]
    gclose=np.where(np.sqrt((TDFrac-curmra)**2+(TDFdecc-curmdec)**2)<1.5)[0]
    gs=np.argsort(np.sqrt((TDFrac[gclose]-curmra)**2+(TDFdecc[gclose]-curmdec)**2))
    foundbox,cnt=False,-1
    while ((not(foundbox))&(cnt<len(gs)-1)):
        cnt+=1
        foundbox=boxes[TDFtilenames[gclose][gs][cnt]].contains(geometry.Point(curmra,curmdec))
    #if not(foundbox):
    #    print 'Found no box for',MDF['MQ_ROWNUM'][i],curmra,curmdec
    if foundbox:
        outcr[:,3][i]=TDFtilenames[gclose][gs][cnt]
    else:
        outcr[:,3][i]='None'

np.savetxt('/home/rumbaugh/SDSSPOSS_Y3A1_tilenames.csv',outcr,fmt='%s,%s,%s,%s',header='SP_ROWNUM,RA,DEC,TILENAME',comments='')