"""
Static Pipeline representation to create a CodePipeline dedicated to building
Lambda Layers
"""

from troposphere import (
    Parameter,
    Template,
    GetAtt,
    Ref,
    Sub
)

from ozone.handlers.lambda_tools import check_params_exist
from ozone.resources.iam.roles.pipeline_role import pipelinerole_build
from ozone.resources.devtools.pipeline import (
    SourceAction,
    BuildAction,
    DeployAction,
    InvokeAction,
    CodePipeline
)
from ozone.outputs import object_outputs

def template(**kwargs):
    """
    """

    template_required_params = [
        'BucketName',
        'Source', 'LayerBuildProjects', 'LayersMergeProject',
        'LayerName', 'GeneratorFunctionName', 'CloudformationRoleArn'
    ]
    check_params_exist(template_required_params, kwargs)
    template = Template()
    token =  template.add_parameter(Parameter(
        'GitHubOAuthToken',
        Type="String",
        NoEcho=True
    ))
    role = pipelinerole_build(
        UseCodeCommit=True,
        UseCodeBuild=True,
        UseLambda=True,
        UseCloudformation=True,
        Bucket=kwargs['BucketName']
    )
    if kwargs['Source']['Provider'].lower() == 'github':
        kwargs['Source']['Config']['OAuthToken'] = Ref(token)
    source = SourceAction(
        name='SourceCode',
        provider=kwargs['Source']['Provider'],
        config=kwargs['Source']['Config']
    )
    build_actions = []
    builds_projects = kwargs['LayerBuildProjects']
    for project in builds_projects:
        build_actions.append(BuildAction(
            project,
            source.outputs,
            project
        ))
    build_outputs = []
    for action in build_actions:
        build_outputs += action.outputs

    merge_action = BuildAction(
        'MergeAction',
        build_outputs,
        kwargs['LayersMergeProject']
    )
    invoke = InvokeAction(
        'GenerateTemplateForCfn',
        merge_action.outputs,
        function_name=kwargs['GeneratorFunctionName']
    )

    input_name = invoke.outputs[0].Name
    deploy = DeployAction(
        'DeployToCfn',
        invoke.outputs,
        'CloudFormation',
        StackName=f'layer-{kwargs["LayerName"]}',
        RoleArn=kwargs['CloudformationRoleArn'],
        TemplatePath=f'{input_name}::tmp/template.json'
    )
    stages = [
        ('Source', [source]),
        ('BuildLayers', build_actions),
        ('MergeLayers', [merge_action]),
        ('GenerateCfnTemplate', [invoke]),
        ('DeployWithCfn', [deploy]),
    ]

    pipeline = CodePipeline(
        'Pipeline',
        GetAtt(role, 'Arn'),
        kwargs['BucketName'],
        stages
    )
    template.add_resource(role)
    template.add_resource(pipeline)
    template.add_output(object_outputs(pipeline, True))
    return template


import json
import sys
import os.path
from mutagen.id3 import (ID3, CTOC, CHAP, TIT2, TALB,
                         TPE1, COMM, USLT, APIC, CTOCFlags)

audio = ID3(sys.argv[1])

if len(sys.argv) > 2:
    data = json.loads(sys.argv[2])
    chapters = data["chapters"]
    ctoc_ids = list(map(lambda i: i.get("id"), chapters))

    audio.delall('TALB')
    audio["TALB"] = TALB(encoding=3, text=data["podcast_title"])

    audio.delall('TPE1')
    audio["TPE1"] = TPE1(encoding=3, text=data["podcast_title"])

    audio.delall('TIT2')
    audio["TIT2"] = TIT2(encoding=3, text=data["episode_title"])

    audio.delall('COMM')
    audio["COMM"] = COMM(encoding=3,
                         lang=u'eng',
                         text=data["episode_description"])

    audio.delall('USLT')
    audio["USLT"] = USLT(encoding=3,
                         lang=u'eng',
                         text=data["episode_description"])

    if "podcast_cover" in data and os.path.isfile(data["podcast_cover"]):
        audio.delall('APIC')
        audio["APIC"] = APIC(encoding=3,
                             mime='image/jpeg',
                             type=3,
                             desc=u'Cover',
                             data=open(data["podcast_cover"]).read())

    audio.delall('CTOC')
    audio.add(CTOC(element_id=u"toc",
                   flags=CTOCFlags.TOP_LEVEL | CTOCFlags.ORDERED,
                   child_element_ids=ctoc_ids,
                   sub_frames=[
                      TIT2(text=[u"TOC"]),
                   ]))

    audio.delall('CHAP')
    for chapter in chapters:
        audio.add(CHAP(element_id=chapter.get("id"),
                       start_time=int(chapter.get("start")),
                       end_time=int(chapter.get("end")),
                       sub_frames=[
                           TIT2(text=[chapter.get("title")]),
                       ]))

    audio.save()

for key, value in audio.items():
    print(value.pprint())


#!/usr/bin/python3
import os
import json
import html
import random
import string
import threading
import subprocess
from bottle import app, error, post, request, redirect, route, run, static_file
from beaker.middleware import SessionMiddleware

session_opts = {
  'session.type': 'file',
  'session.data_dir': './cfg/',
  'session.auto': True,
}
sapp = SessionMiddleware(app(), session_opts)
sess = request.environ.get('beaker.session')

ipfspath = '/usr/local/bin/ipfs'

with open('cfg/email.cfg', 'r') as ecf:
  email = ecf.read()

ipfs_id = ''
if os.path.exists('ipfs/config'):
  with open('ipfs/config', 'r') as ipcfg:
    ipconfig = ipcfg.read()
    jtxt = json.loads(ipconfig)
    ipfs_id = jtxt['Identity']['PeerID']

@route('/')
def index():
  sess = request.environ.get('beaker.session')
  sess['csrf'] = ''.join(random.choice(string.ascii_letters) for i in range(12))
  sess.save()

  htmlsrc = ''
  htmlsrc += '<title>IPFS Podcast Node</title>'
  htmlsrc += '<meta name="viewport" content="width=device-width, initial-scale=1.0" />'
  htmlsrc += '<link rel="icon" href="/favicon.png">'
  htmlsrc += ''
  htmlsrc += ''
  htmlsrc += ''

  htmlsrc += '
IPFS Podcasting Node
'
  htmlsrc += '
'
  if ipfs_id != '':
    htmlsrc += '
IPFS ID :  ' + str(ipfs_id) + '
'
  htmlsrc += '<form id="ecfg" action="/" method="post">'
  htmlsrc += '<input id="csrf" name="csrf" type="hidden" value="' + sess['csrf'] + '" />'
  htmlsrc += 'E-Mail : <input id="email" name="email" type="email" placeholder="user@example.com" title="E-mail Address (optional)" value="' + email + '" />'
  htmlsrc += '<button>Update</button>
'
  htmlsrc += '</form>'

  htmlsrc += 'Network :  '

  httpstat = 'pfail'
  hstat = subprocess.run('timeout 1 bash -c "</dev/tcp/ipfspodcasting.net/80"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  if hstat.returncode == 0:
    httpstat = 'ppass'
  htmlsrc += 'HTTP '

  httpsstat = 'pfail'
  hsstat = subprocess.run('timeout 1 bash -c "</dev/tcp/ipfspodcasting.net/443"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  if hsstat.returncode == 0:
    httpsstat = 'ppass'
  htmlsrc += 'HTTPS '

  peercnt = 0
  speers = subprocess.run(ipfspath + ' swarm peers|wc -l', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  if speers.returncode == 0:
    peercnt = int(speers.stdout.decode().strip())
  if peercnt > 400:
    ipfsstat = 'ppass'
  elif peercnt > 100:
    ipfsstat = 'pwarn'
  else:
    ipfsstat = 'pfail'
  htmlsrc += 'IPFS - ' + str(peercnt) + ' Peers
'

  repostat = subprocess.run(ipfspath + ' repo stat -s|grep RepoSize', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  if repostat.returncode == 0:
    repolen = repostat.stdout.decode().strip().split(':')
    used = int(repolen[1].strip())
  else:
    used = 0
  df = os.statvfs('/')
  avail = df.f_bavail * df.f_frsize
  percent = round(used/(used+avail)*100, 1)

  if used < (1024*1024*1024):
    used = str(round(used/1024/1024, 1)) + ' MB'
  elif used < (1024*1024*1024*1024):
    used = str(round(used/1024/1024/1024, 1)) + ' GB'
  else:
    used = str(round(used/1024/1024/1024/1024, 2)) + ' TB'

  if avail < (1024*1024*1024):
    avail = str(round(avail/1024/1024, 1)) + ' MB'
  elif avail < (1024*1024*1024*1024):
    avail = str(round(avail/1024/1024/1024, 1)) + ' GB'
  else:
    avail = str(round(avail/1024/1024/1024/1024, 2)) + ' TB'

  htmlsrc += 'Storage : '
  htmlsrc += '
'
  htmlsrc += '

'
  htmlsrc += '
' + str(used) + ' Used' + str(percent) + '%' + str(avail) + ' Available
'
  htmlsrc += '
'

  #don't allow gc while pinning (or already running)
  gctxt = ''
  gcrun = subprocess.run('ps x|grep -E "(repo gc|ipfs pin)"|grep -v grep', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  if gcrun.returncode == 0:
    gctxt = gcrun.stdout.decode().strip()
  if gctxt == '':
    disabled = ''
    title = 'Run IPFS Garbage Collection'
  else:
    disabled = 'disabled="disabled"'
    title = 'Not available while pinning or GC already running...'

  htmlsrc += '<form id="igc" action="/" method="post">'
  htmlsrc += '<input id="csrf" name="csrf" type="hidden" value="' + sess['csrf'] + '" />'
  htmlsrc += '<input id="rungc" name="rungc" type="hidden" value="1" />'
  htmlsrc += '<button ' + disabled + ' title="' + title + '">Clean Up</button>'
  htmlsrc += '</form>'

  htmlsrc += '
'

  htmlsrc += '
Activity Log
'
  htmlsrc += '
'
  with open('ipfspodcastnode.log', 'r') as pcl:
    logtxt = pcl.read()
    htmlsrc += html.escape(logtxt)
  htmlsrc += '
'

  htmlsrc += '

'

  htmlsrc += '<form id="frst" action="/" method="post" style="float: right;">'
  htmlsrc += '<input id="csrf" name="csrf" type="hidden" value="' + sess['csrf'] + '" />'
  htmlsrc += '<input id="reset" name="reset" type="hidden" value="1" />'
  htmlsrc += '<button title="Hard reset the IPFS app (when "it\'s just not working")">Restart IPFS</button>'
  htmlsrc += '</form>'

  htmlsrc += '
ManageFAQ
'
  #IPFS WebUIPinned Files
  htmlsrc += ''
  #document.getElementById("ipfsui").href=window.location.href; document.getElementById("ipfsui").href=document.getElementById("ipfsui").href.replace("8675", "5001/webui"); document.getElementById("ipfspn").href=window.location.href; document.getElementById("ipfspn").href=document.getElementById("ipfspn").href.replace("8675", "5001/webui/#/pins");
  htmlsrc += ''
  return htmlsrc

@post('/')
def do_email():
  csrf = request.forms.get('csrf')
  sess = request.environ.get('beaker.session')
  if csrf == sess['csrf']:
    if request.forms.get('email') is not None:
      global email
      email = request.forms.get('email')
      with open('cfg/email.cfg', 'w') as ecf:
        ecf.write(email)
    if request.forms.get('reset') == '1':
      suicide = subprocess.run('kill 1', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if request.forms.get('rungc') == '1':
      gcrun = subprocess.run(ipfspath + ' repo gc --silent', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

  redirect('/')

@route('/ipfspod.png')
def server_static():
    return static_file('ipfspod.png', root='')

@route('/favicon.png')
def server_static():
    return static_file('favicon.png', root='')

#run(host='0.0.0.0', port=8675, debug=True)
threading.Thread(target=run, kwargs=dict(host='0.0.0.0', port=8675, app=sapp, debug=False)).start()


import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os, os.path
import smtplib
import random
import win32gui
import win32con
try:

    engine=pyttsx3.init('sapi5')
    voices=engine.getProperty('voices')
    print(voices[0].id)
    engine.setProperty('voice',voices[0].id)

    def speak(audio):
        engine.say(audio)
        engine.runAndWait()
    def chrome_webbrowser(chrome_path, url):
        webbrowser.get(chrome_path).open(url)

    def wishme():
        hour=int(datetime.datetime.now().hour)
        if hour>=0 and hour<12:
            speak('Good Morning Vicky')
        elif hour>=12 and hour>=17:
            speak('Good Afternoon Vicky')
        else:
            speak('Good Evening')
        print('Hellow I am Computer, How can I help you!')
        speak('Hellow I am Computer, How can I help you!')
    def takecommand():
        r=sr.Recognizer()
        with sr.Microphone() as source:
            print('Listening....')
            r.pause_threshold=1

            audio=r.listen(source,timeout=1,phrase_time_limit=3)
        try:
            print('Recognizing....')
            querry=r.recognize_google(audio,language='en-in')
            print(f'You said {querry}')
        except Exception:
            print('Say That Again Please!')
            return 'None'
        return querry

    if __name__ == "__main__":
        chrome_path="C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
        webbrowser.get(chrome_path)
        while True:
            querry=takecommand().lower()
            # Strt
            if "poweroff computer" in querry:
                speak("Computer has been closed")
                os.system("shutdown /s /t 1")
            elif "power off computer" in querry:
                speak("Computer has been closed")
                os.system("shutdown /s /t 1")
            elif "power of computer" in querry:
                speak("Computer has been closed")
                os.system("shutdown /s /t 1")
            elif "shutdown computer" in querry or "shut down computer" in querry:
                speak("Computer has been closed")
                os.system("shutdown /s /t 1")
            elif "computer shutdown" in querry or "computer shut down" in querry:
                speak("Computer has been closed")
                os.system("shutdown /s /t 1")
            elif "quit computer" in querry:
                speak("Computer has been power off")
                os.system("shutdown /s /t 1")
            elif "restartcomputer" in querry:
                speak("We restarting your PC")
                os.system("shutdown /r /t 1")
            elif "restart computer" in querry:
                speak("We restarting your PC")
                os.system("shutdown /r /t 1")
            elif "rstart computer" in querry:
                speak("We restarting your PC")
                os.system("shutdown /r /t 1")
            elif "restart computer" in querry:
                speak("We restarting your PC")
                os.system("shutdown /r /t 1")
            elif "hybrid" in querry or "hybernate" in querry or "hybernation" in querry or "hibernation" in querry:
                speak("We set your PC to sleeping mode")
                os.system("Rundll32.exe Powrprof.dll,SetSuspendState Sleep")
            elif "sleep" in querry or "sleap" in querry:
                speak("We set your PC to sleeping mode or turn off your screen")
                win32gui.SendMessage(win32con.HWND_BROADCAST,win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
            elif "open screen" in querry or "openscreen" in querry or "screen" in querry:
                speak("We open your screen")
                win32gui.SendMessage(win32con.HWND_BROADCAST,
                win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, -1)

            elif "vscode" in querry or "vs code" in querry:
                speak("Vs code open to you Vicky")

                os.system("code .")

            # end
            if "commands" in querry or "command" in querry:
                i=0
                while True:
                    if i==0:
                        i=1
                        wishme()
                    querry=takecommand().lower()
                    if 'wikipedia' in querry:
                        speak('Searching wikipedia...')
                        querry=querry.replace('wikipedia','')
                        querry=querry.replace('please','')

                        results=wikipedia.summary(querry,sentences=2)
                        speak('According to wikipedia, ')
                        print(results)
                        speak(results)
                    elif "vscode" in querry or "vs code" in querry:
                        speak("Vs code open to you Vicky")
                        os.system("code .")
                    elif 'who are you' in querry:
                        print('I am Computer Sir!')
                        speak('I am Computer Sir!')
                    elif 'made you' in querry:
                        print('I am made by you Sir Waqas powered by Vicky World Production')
                        speak('I am made by you Sir Waqas powered by Vicky World Production')

                    elif "sleep" in querry or "sleap" in querry:
                        speak("We set your PC to sleeping mode")
                        # os.system("Powercfg -H OFF")
                        os.system("rundll32.exe Powercfg -H OFF,SetSuspendState 0,1,0")

                    elif 'open youtube' in querry:
                        url=('youtube.com')
                        chrome_webbrowser(chrome_path,url)
                        # webbrowser.open('youtube.com')
                        speak('Youtube has been opened dear Vicky')

                    elif 'open google' in querry or 'open chrome' in querry:
                        # webbrowser.open('google.com')
                        url=('google.com')
                        chrome_webbrowser(chrome_path,url)
                        speak('Google Has been opened dear Vicky')

                    elif 'stack overflow' in querry:
                        # webbrowser.open('stackoverflow.com')
                        url=('stackoverflow.com')
                        chrome_webbrowser(chrome_path,url)

                    elif 'stackoverflow' in querry:
                        url=('stackoverflow.com')
                        chrome_webbrowser(chrome_path,url)
                    elif 'time' in querry:
                        str=datetime.datetime.now().strftime('%H:%M:%S')
                        print(f"Time is{str}")
                        speak(f"Time is {str}")
                    elif 'search' in querry:
                        querry=querry.replace('search','')
                        querry=querry.replace('please','')
                        chrome_webbrowser(chrome_path,querry)
                    elif 'song' in querry or 'songs' in querry:

                        music_dir=r'E:\D\New folder (2)'
                        songs=os.listdir(music_dir)
                        print(songs)
                        files_len= len([name for name in os.listdir('.') if os.path.isfile(name)])
                        print(files_len)
                        r= random.randint(0, files_len-1)
                        print(songs[r])
                        os.startfile(os.path.join(music_dir,songs[r]))

                    elif 'stop' in querry:
                        print('Commands has been stopped Thank You Sir!')
                        speak('Commands has been stopped Thank You Sir!')
                        break

                    elif 'quit' in querry or 'exit' in querry:
                        print('Commands has been stopped. Thank You Sir!')
                        speak('Commands has been stopped. Thank You Sir!')
                        break
                    elif "shutdown computer" in querry or "shut down computer" in querry:
                        speak("Computer has been closed")
                        os.system("shutdown /s /t 1")
                    elif "computer shutdown" in querry or "computer shut down" in querry:
                        speak("Computer has been closed")
                        os.system("shutdown /s /t 1")

                    elif "poweroff computer" in querry:
                        speak("Computer has been closed")
                        os.system("shutdown /s /t 1")
                    elif "power off computer" in querry:
                        speak("Computer has been closed")
                        os.system("shutdown /s /t 1")
                    elif "power of computer" in querry:
                        speak("Computer has been closed")
                        os.system("shutdown /s /t 1")
                    elif "quit Computer" in querry:
                        speak("Computer has been power off")
                        os.system("shutdown /s /t 1")
                    elif "restartcomputer" in querry:
                        speak("We restarting your PC")
                        os.system("shutdown /r /t 1")
                    elif "restart computer" in querry:
                        speak("We restarting your PC")
                        os.system("shutdown /r /t 1")
                    elif "rstart computer" in querry:
                        speak("We restarting your PC")
                        os.system("shutdown /r /t 1")
                    elif "restart Computer" in querry:
                        speak("We restarting your PC")
                        os.system("shutdown /r /t 1")
                    elif "sleep" in querry or "sleap" in querry:
                        speak("We set your PC to sleeping mode or turn off your screen")
                        win32gui.SendMessage(win32con.HWND_BROADCAST,win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
                    elif "open screen" in querry or "openscreen" in querry:
                        speak("Screen has been turn")
                        win32gui.SendMessage(win32con.HWND_BROADCAST,
                        win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, -1)
except Exception as e:
    speak('An unknown Error has been occured Check Your Connection Please')


from flask import Flask, send_file, send_from_directory, safe_join, abort

app = Flask(__name__)

# app.config["CLIENT_IMAGES"] = "/home/mahima/console/static/client/img"
app.config["CLIENT_IMAGES"] = "/home/lenovo/SEproject/OpsConsole/api/static"

# The absolute path of the directory containing CSV files for users to download
app.config["CLIENT_CSV"] = "/home/mahima/console/static/client/csv"

# The absolute path of the directory containing PDF files for users to download
app.config["CLIENT_PDF"] = "/home/mahima/console/static/client/pdf"

@app.route("/getimg/<img_name>")
def get_img(img_name):
    try:
        return send_from_directory(app.config["CLIENT_IMAGES"], filename = img_name, as_attachment=True)
    except FileNotFoundError:
        abort(404)

@app.route('/hello')
def hello():
    return "Hello Lifeeee"

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


import csv
import math
import sys

import numpy as np

import matplotlib.pyplot as plt
from sklearn.feature_selection import chi2, f_regression, mutual_info_regression

def mylog(x):
    if x==0:
        return -10000000000
    else:
        return math.log(x)

def entropy(probs, neg, pos):
    '''
    entropy for binary classification data
    '''
    entropy=0.0
    entropy=(probs[1]/pos-probs[0]/neg)*mylog((probs[1]*neg)/(probs[0]*pos))

    return entropy

def get_bin_from_score(score):
    '''
    get bin number. 0-low, 1-high. avg. threshold=280
    '''
    return min(1,int(score//280))

def iv(header, data):
    '''
    Information Value based feature selection
    '''
    neg,pos = 0,0
    probs=np.zeros((9,5,2))

    for datum in data:
        score_bin=get_bin_from_score(float(datum[9]))
        if(score_bin==0):
            neg+=1
        else:
            pos+=1

        for i in range(9):
            probs[i][int(datum[i])-1][score_bin]+=1

    feature_score=[0 for _ in range(9)]

    for i in range(9):
        for j in range(5):
            feature_score[i]+=entropy(probs[i][j], neg, pos)

    fig = plt.figure()
    plt.barh(header[::-1],feature_score[::-1])
    plt.show()

def anova(header, regressors, target):
    '''
    ANOVA based feature selection
    '''
    # chi_scores = chi2(regressors,target)
    anova_scores = f_regression(regressors, target)

    fig = plt.figure()
    plt.barh(header[::-1],anova_scores[0][::-1])
    plt.show()

def mutual_info(header, regressors, target):
    '''
    Mutual Information based feature selection
    '''
    # chi_scores = chi2(regressors,target)
    mi_scores = mutual_info_regression(regressors, target)

    fig = plt.figure()
    plt.barh(header[::-1],mi_scores[::-1])
    plt.show()

def main():
    '''
    reads training data and calls appropriate method for feature-selection
    '''
    data,regressors,target = [],[],[]
    with open('data.csv','r') as csv_file:
        csv_reader=csv.reader(csv_file,delimiter=',')
        for row in csv_reader:
            data.append(row)

    header=data[0]
    data=data[1:]

    for datum in data:
        for i in range(9):
            datum[i]=int(datum[i])
        regressors.append(datum[:9])
        target.append(float(datum[9]))

    if len(sys.argv)<2:
        print('Usage: python feature_selectors.py [iv/anova/mi]')
    else:
        option=sys.argv[1]

        if option=='iv':
            iv(header, data)
        elif option=='anova':
            anova(header, regressors, target)
        elif option=='mi':
            mutual_info(header, regressors, target)
        else:
            print('Usage: python feature_selectors.py [iv/anova/mi]')

if __name__=='__main__':
    main()


from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    (r'^$', 'rss_duna.feed.views.home'),
    # url(r'^myproject/', include('myproject.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
    #(r'^feed/$', DunaEntriesFeed()),
    (r'^duna/(?P<programa_id>\D+)/rss/$', 'rss_duna.feed.views.get_feed_rss'),
    (r'^duna/feeds/$', 'rss_duna.feed.views.list_feeds'),
    #(r'^prueba/$', 'rss_duna.feed.views.prueba')
)

#if settings.DEBUG:
#    urlpatterns += patterns('',
#        (r'^files/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':False}),
#    )


#!/usr/bin/python
# coding: utf-8
from flask import Flask, Blueprint, flash, g, redirect, render_template, request, url_for, session
import os
app = Flask(__name__)

tests = []
class TestObj:
    def __init__(self, name, path):
        self.name = name
        self.path = path+self.name
        self.countfile = self.path+"/count.txt"
        self.count = self.read_count()
        self.run_file = self.path + "/run.txt"
        self.running = self.get_run_state()

    def read_count(self):
        return int(open(self.countfile).read())

    def get_run_state(self):
        return int(open(self.run_file).read())

    def toggle_run(self):
        newstate = str(int(not bool(self.get_run_state())))
        with open(self.run_file,'w') as rf:
            rf.write(newstate)

@app.route('/', methods=('GET','POST'))
def main():
    if request.method == 'POST':
        print('got request')
        test = list(dict(request.form))[0]
        testobj = None
        for t in tests:
            if t.name == test:
                testobj = t
                testobj.toggle_run()
        print ('test',testobj)

    global tests
    tests = generate_tests()
    return render_template('index.html',tests=tests)

def generate_tests():
    a_dir = "/home/pi/current_tests/"
    tests = [name for name in os.listdir(a_dir) if os.path.isdir(os.path.join(a_dir, name))]
    testobjs= [ TestObj(x,a_dir) for x in tests]
    return testobjs

if __name__ == '__main__':
    print(generate_tests())
    app.run(host='0.0.0.0', debug=True)


import random
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM

# Define the RNN model
model = Sequential()
model.add(LSTM(64, input_shape=(1, 1)))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')

balance = 100
bet = 1

sim_numbers = []

while True:
    cup = random.randint(1, 3)

    game = input("Choice ('simulate x' or 'play cups'): ")

    if game.startswith("simulate"):
        input_list = game.split(" ")
        num = int(input_list[1])
        if num >= 100000:
             print("Please wait a few minutes, numbers above 100.000 take longer for the model to simulate...")
        sim_numbers += [random.randint(1, 3) for _ in range(num)]
        print(sim_numbers)
        sim_numbers_arr = np.array(sim_numbers)
        sim_numbers_arr = sim_numbers_arr.reshape(sim_numbers_arr.shape[0], 1, 1)
        model.fit(sim_numbers_arr[:-1], sim_numbers_arr[1:], epochs=10, verbose=0)
        predicted_number = model.predict(sim_numbers_arr[-1].reshape(1, 1, 1))
        print(f'Predicted next number: {predicted_number[0][0]:.0f}')

    elif game == "play cups" or game == "play":
            choice = input("What cup do you want to choose? 1, 2 or 3: ")
            bet_choice = input(f'How much do you want to bet on cup {choice}?: ')
            bet = bet_choice

            if int(choice) == cup:
                balance += int(bet)
                print(f'You won ${bet}! Total balance is now ${balance}!')
            elif int(choice) != cup:
                balance -= int(bet)
                print(f'You lost ${bet}. Total balance is now ${balance}. Correct cup was cup {cup}.')
            else:
                print("Please input a valid number")
                continue
    else:
        print("Please input a valid choice...")


from jinja2 import Environment, BaseLoader, TemplateNotFound
import importlib_resources

class PackageLoader(BaseLoader):
    def __init__(self, path):
        self.path = path

    def get_source(self, environment, template):
        from backendService import templates

        try:
            source = importlib_resources.read_text(templates, template)
        except FileNotFoundError as exc:
            raise TemplateNotFound(template) from exc
        return source, self.path, lambda: True

JINJA_ENV = Environment(loader=PackageLoader("backendService.http.templates"))

def get_template(name):
    return JINJA_ENV.get_template(name)


#coding=utf-8
import numpy as np
import pyten
from scipy import stats
from pyten.method.PoissonAirCP import PoissonAirCP
from pyten.method import AirCP
from pyten.tools import tenerror
from pyten.method import cp_als
from pyten.method import falrtc,TNCP
import matplotlib.pyplot as plt

#参数设置
missList = [0.7]

duplicate=1
prespecifyrank = 5
para_alpha = [1,1,1]
para_lmbda = 1

def normalize(mat):
    '''
    将矩阵每一列都标准化，不然在计算余弦相似度时都非常相近
    :param mat:
    :return:
    '''
    X_mean = mat.mean(axis=0)
    # standardize X
    X1 = (mat - X_mean)
    return(X1)

from sklearn.metrics.pairwise import cosine_similarity
def cons_similarity(dat):
    siz = dat.shape
    temp = np.sum(dat, axis=1)

    tagvector = normalize(np.sum(dat, axis=1))
    cos_dist = 1 - cosine_similarity(tagvector)
    aux0 = np.exp(-(cos_dist**2))

    # 2时间相似性用AR(1)模型的acf去做
    from statsmodels.tsa.arima_model import ARMA
    ts = np.sum(np.sum(dat, axis=0),axis = 1)
    order = (1,0)
    tempModel = ARMA(ts,order).fit()
    rho = np.abs(tempModel.arparams)
    aux1 = np.diag(np.ones(siz[1]))
    for nn in range(1, siz[1]):
        aux1 = aux1 + np.diag(np.ones(siz[1] - nn), -nn) * rho ** nn + np.diag(np.ones(siz[1] - nn), nn) * rho ** nn

    # 3话题之间相关性
    aux2 = np.diag(np.ones(siz[2]))
    Pl = np.sum(temp, axis=1) / np.sum(temp)
    for i in range(siz[2]):
        for j in range(siz[2]):
            aux2[i,j] = np.exp(-np.sum((((temp[:, i] - temp[:, j]) / np.max(temp, 1)) ** 2) * Pl))
    aux = [aux0, aux1, aux2]
    return (aux)

def convertMon(mat):
    '''
    将数据从daily_data转化为monthly_data
    :param mat:
    :return:
    '''
    monthdat = []
    month = range(0, 365, 30)
    for i in range(12):
        monthdat.append(np.sum(mat[:, month[i]:month[i + 1]], axis=1))
    monthdat = np.array(monthdat)
    monthdat = monthdat.transpose((1, 0, 2))
    return(monthdat)

dat =np.load('newbuild_tensor.npy')
#预处理，先筛选一次国家，0太多的的不纳入考虑，只剩下235->195个
idx = np.sum(np.sum(dat ==0,axis = 1),axis=1)>1000
dat = dat[idx]

#可供选择的调整方法，整理成月数据
dat = convertMon(dat)
siz = dat.shape
true_data = dat.copy()
true_data = pyten.tenclass.tensor.Tensor(true_data)

# 这里是为了画图比较
finalList1 = []
finalList22 = []
finalList2 = []
finalListTNCP=[]
finalListfal = []

for miss in missList:
    aux = [np.diag(np.ones(siz[0])), np.diag(np.ones(siz[1])), np.diag(np.ones(siz[2]))]
    RE2 = []
    RE22 = []
    for dup in range(duplicate):
        np.random.seed(dup*4)
        #每次都用同一份数据去做
        data = dat.copy()
        #观测值：丢失部分数据的
        Omega = (np.random.random(siz) > miss) * 1
        data[Omega == 0] -= data[Omega == 0]
        data = pyten.tenclass.tensor.Tensor(data)

        #补全时候用的rank
        print('missing ratio: {0}'.format(miss))
        #补全时候用的rank
        com_rank = prespecifyrank

        # 这部分引入了更新辅助矩阵的算法
        simerror = 1
        Iter = 1
        while (simerror > 1e-2 and Iter < 10):
            self2 = PoissonAirCP(data, omega=Omega, rank=com_rank, max_iter=3000, tol=1e-5,
                                 OnlyObs=True, TrueValue=true_data, sim_mats=aux, alpha=para_alpha, lmbda=para_lmbda)
            self2.run()
            temp_aux = cons_similarity(self2.X.data)
            simerror = np.max((np.linalg.norm(aux[0] - temp_aux[0]),
                               np.linalg.norm(aux[1] - temp_aux[1]),
                               np.linalg.norm(aux[2] - temp_aux[2])))
            aux = temp_aux
            Iter = Iter + 1

            print('ExpAirCP loop with similarity error: {0}'.format(simerror))
            [EEr, EReEr1, EReEr2] = tenerror(self2.X, true_data, Omega)
            if Iter ==2:
                RE22.append(EReEr1)
            print(EReEr1)
        # 到这里为止
        [EErr, EReErr1, EReErr2] = tenerror(self2.X, true_data, Omega)
        print ('ExpAirCP Completion Error: {0}, {1}, {2}'.format(EErr, EReErr1, EReErr2))

        RE2.append(EReErr1)

    finalList22.append(np.mean(RE22))
    finalList2.append(np.mean(RE2))

for miss in missList:
    aux = [np.diag(np.ones(siz[0])), np.diag(np.ones(siz[1])), np.diag(np.ones(siz[2]))]
    RE1 = []
    RE11 = []
    for dup in range(duplicate):
        np.random.seed(dup*4)
        #每次都用同一份数据去做
        data = dat.copy()
        #观测值：丢失部分数据的
        Omega = (np.random.random(siz) > miss) * 1
        data[Omega == 0] -= data[Omega == 0]
        data = pyten.tenclass.tensor.Tensor(data)

        #补全时候用的rank
        print('missing ratio: {0}'.format(miss))
        #补全时候用的rank
        com_rank = prespecifyrank

        # 这部分引入了更新辅助矩阵的算法
        simerror = 1
        Iter = 1
        while (simerror > 1e-2 and Iter < 10):
            self = AirCP(data, omega=Omega, rank=com_rank, max_iter=3000, tol=1e-5, sim_mats=aux, alpha=para_alpha, lmbda=para_lmbda)
            self.run()
            temp_aux = cons_similarity(self.X.data)
            simerror = np.max((np.linalg.norm(aux[0] - temp_aux[0]),
                               np.linalg.norm(aux[1] - temp_aux[1]),
                               np.linalg.norm(aux[2] - temp_aux[2])))
            aux = temp_aux
            Iter = Iter + 1
            print('AirCP loop with similarity error: {0}'.format(simerror))
            [EEr, EReEr1, EReEr2] = tenerror(self.X, true_data, Omega)
            print(EReEr1)
        # 到这里为止

        #这里看对原始数据的补全准不准
        [Err, ReErr1, ReErr2] = tenerror(self.X, true_data, Omega)
        print ('AirCP Completion Error: {0}, {1}, {2}'.format(Err, ReErr1, ReErr2))
        RE1.append(ReErr1)
    finalList1.append(np.mean(RE1))

# for miss in missList:
#     RETNCP = []
#
#     for dup in range(duplicate):
#         np.random.seed(dup*4)
#         #每次都用同一份数据去做
#         data = dat.copy()
#         #观测值：丢失部分数据的
#         Omega = (np.random.random(siz) > miss) * 1
#         data[Omega == 0] -= data[Omega == 0]
#         data = pyten.tenclass.tensor.Tensor(data)
#
#         #补全时候用的rank
#         print('missing ratio: {0}'.format(miss))
#         #补全时候用的rank
#         com_rank = prespecifyrank
#         self3 = TNCP(data, Omega, rank=com_rank,alpha = para_alpha, lmbda=para_lmbda)
#         self3.run()
#         [EErrr, EReErrr1, EReErrr2] = tenerror(self3.X, true_data, Omega)
#         print ('TNCP Completion Error: {0}, {1}, {2}'.format(EErrr, EReErrr1, EReErrr2))
#         RETNCP.append(EReErrr1)
#     finalListTNCP.append(np.mean(RETNCP))
#
#
# #对于fal不受到rank改变的影响，所以单独写出来
# for miss in missList:
#     REfal = []
#     for dup in range(duplicate):
#         np.random.seed(dup*4)
#         #每次都用同一份数据去做
#         data = dat.copy()
#         #观测值：丢失部分数据的
#         Omega = (np.random.random(siz) > miss) * 1
#         data[Omega == 0] -= data[Omega == 0]
#         data = pyten.tenclass.tensor.Tensor(data)
#         print('missing ratio: {0}'.format(miss))
#         rX1 = falrtc(data, Omega, max_iter=100)
#         [Errfal, ReErrfal, ReErr2fal] = tenerror(rX1, true_data, Omega)
#         print ('falrtc Completion Error: {0}, {1}, {2}'.format(Errfal, ReErrfal, ReErr2fal))
#         REfal.append(ReErrfal)
#     finalListfal.append(np.mean(REfal))
#

print(finalList1)
print(finalList2)
print(finalListTNCP)
print(finalListfal)

result = [finalList1,finalList2,finalListTNCP]
result_name = 'prerank='+str(prespecifyrank)+'.csv'
#np.savetxt(result_name,result,fmt='%.4f',delimiter=',')


import Algorithmia

import logging
import os
LOG_FOLDER = 'logs'
if os.path.exists(LOG_FOLDER) is False:
    os.mkdir(LOG_FOLDER)

logging.basicConfig(filename=LOG_FOLDER + '/' + __name__ + '.log', format='[%(asctime)s] %(message)s\n\n',
                    level=logging.DEBUG)
api_key = None

def get_emotion(photo: bytes) -> str or None:
    '''Returns emotions by face image
    Args:
        photo: bytes data
    Returns:
        main_emotion: most possible emotion name
        None: if failed
    '''

    try:
        client = Algorithmia.client(api_key)
        algo = client.algo('deeplearning/EmotionRecognitionCNNMBP/0.1.3')
        img = bytearray(photo)
        emotions = algo.pipe(img).result['results'][0]['emotion']
        main_emotion = str()
        confidence = 0.0
        for emotion in emotions:
            if emotion[0] > confidence:
                confidence = emotion[0]
                main_emotion = emotion[1]
        return main_emotion.lower()

    except Exception as e:
        logging.error(str(e))
        print('Algorithmia:', str(e))
        return None

def celebrities_similarity(photo: bytes) -> str or None:
    '''Returns person similarity to some celebrity
    Args:
        photo: bytes data
    Returns:
        Name of the most possible celebrity
        None: if failed
    '''
    try:
        client = Algorithmia.client(api_key)
        algo = client.algo('deeplearning/DeepFaceRecognition/0.1.1')
        img = bytearray(photo)
        celebrities = algo.pipe(img).result['results']
        return ' '.join(celebrities[0][1].split('_'))

    except Exception as e:
        logging.error(str(e))
        return None

def verify_faces(photo1: bytes, photo2: bytes) -> float or None:
    '''Returns two photos similarity
    Args:
        photo1: bytes data
        photo2: bytes data
    Returns:
        similarity confidence: if data recieved
        None: if failed
    '''
    try:
        data = [bytearray(photo1), bytearray(photo2)]
        client = Algorithmia.client(api_key)
        algo = client.algo('zskurultay/ImageSimilarity/0.1.2')
        return algo.pipe(data)

    except Exception as e:
        logging.error(str(e))
        return None

def gender(photo: bytes) -> str or None:
    '''Computes gender probabilities
    Args:
        photo: bytes data
    Returns:
        gender name
        None: if failed
    '''
    try:
        img = bytearray(photo)
        data = {'image': img}
        client = Algorithmia.client(api_key)
        algo = client.algo('deeplearning/GenderClassification/1.0.2')
        gender_list = algo.pipe(img).result['results'][0]['gender']
        if gender_list[0][0] > gender_list[1][0]:
            return gender_list[0][1].lower()
        else:
            return gender_list[1][1].lower()

    except Exception as e:
        logging.error(str(e))
        return None

def age(photo: bytes) -> str or None:
    '''Returns age groups with probabilies
    Args:
        photo: bytes data
    Returns:
        the most possible age interval : list with structure [min_age, max_age]
        None: if failed
    '''
    try:
        img = bytearray(photo)
        client = Algorithmia.client(api_key)
        algo = client.algo('deeplearning/AgeClassification/1.0.3')
        ages = algo.pipe(img).result['results'][0]['age']
        str_age_interval = str()
        age_confidence = 0.0
        for age in ages:
            if age[0] > age_confidence:
                age_confidence = age[0]
                str_age_interval = age[1]

        age_string_interval = str_age_interval.strip('()').split(', ')
        age_interval = [int(age_string_interval[0]), int(age_string_interval[1])]
        return age_interval

    except Exception as e:
        logging.error(str(e))
        print(str(e))
        return None


#-*- coding: utf-8 -*-
u"""

@author:		Martí Congost
@contact:		marti.congost@whads.com
@organization:	Whads/Accent SL
@since:			October 2008
"""
import cherrypy
from cocktail.modeling import cached_getter
from woost.controllers.publishablecontroller import PublishableController

class DocumentController(PublishableController):
    """A controller that serves rendered pages."""

    def __call__(self, **kwargs):

        # Document specified redirection
        document = self.context["publishable"]

        if document.redirection_mode:

            redirection_target = document.find_redirection_target()

            if redirection_target is None:
                raise cherrypy.NotFound()

            raise cherrypy.HTTPRedirect(redirection_target.get_uri())

        # No redirection, serve the document normally
        return PublishableController.__call__(self)

    @cached_getter
    def page_template(self):
        template = self.context["publishable"].template

        if template is None:
            raise cherrypy.NotFound()

        return template

    @cached_getter
    def view_class(self):
        return self.page_template.identifier


from sklearn.linear_model import LassoCV

def run(x_train, y_train, x_test, y_test, eps, n_alphas, alphas, fit_intercept, normalize, precompute, max_iter, tol, copy_X, cv, verbose, n_jobs,
        positive, random_state, selection):
    reg = LassoCV(eps=eps,
                  n_alphas=n_alphas,
                  alphas=alphas,
                  fit_intercept=fit_intercept,
                  normalize=normalize,
                  precompute=precompute,
                  max_iter=max_iter,
                  tol=tol,
                  copy_X=copy_X,
                  cv=cv,
                  verbose=verbose,
                  n_jobs=n_jobs,
                  positive=positive,
                  random_state=random_state,
                  selection=selection).fit(x_train, y_train)
    return {'train_predict': reg.predict(x_train).tolist(),
            'test_predict': reg.predict(x_test).tolist(),
            'train_score': reg.score(x_train, y_train),
            'test_score': reg.score(x_test, y_test),
            'alpha_': reg.alpha_,
            'coef_': reg.coef_.tolist(),
            'intercept_': reg.intercept_,
            'mse_path_': reg.mse_path_.tolist(),
            'alphas_': reg.alphas_.tolist(),
            'dual_gap_': reg.dual_gap_.tolist(),
            'n_iter_': reg.n_iter_}


# -*- coding: utf-8 -*-
"""
Created on Sat Oct 19 13:04:11 2019
@author: Diego Wanderley
@python: 3.6
@description: Train script with training class
"""

import tqdm
import argparse
import torch
import torch.optim as optim
import numpy as np

from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from torch.autograd import Variable
from terminaltables import AsciiTable

import utils.transformations as tsfrm

from test_yolo import evaluate
from models.yolo import Darknet
from models.yolo_utils.utils import *
from utils.datasets import OvaryDataset
from utils.helper import gettrainname

class Training:
    """
        Training class
    """

    def __init__(self, model, device, train_set, valid_set, optim,
                 class_names, train_name='yolov3', logger=None,
                 iou_thres=0.5, conf_thres=0.5, nms_thres=0.5):
        '''
            Training class - Constructor
        '''
        self.model = model
        self.device = device
        self.train_set = train_set
        self.valid_set = valid_set
        self.optimizer = optim
        self.train_name = train_name
        self.model_name = "_".join(train_name.split('_')[2:])
        self.logger = logger
        self.class_names = class_names
        self.gradient_accumulations = 2
        self.iou_thres = iou_thres
        self.conf_thres = conf_thres
        self.nms_thres = nms_thres
        self.metrics  = [
            "grid_size",
            "loss",
            "x",
            "y",
            "w",
            "h",
            "conf",
            "cls",
            "cls_acc",
            "recall50",
            "recall75",
            "precision",
            "conf_obj",
            "conf_noobj",
        ]
        self.epoch = 0

    def _saveweights(self, state, log=None):
        '''
            Save network weights.
            Arguments:
            @state (dict): parameters of the network
        '''
        path = '../weights/'
        filename = path + self.train_name + '_weights.pth.tar'
        torch.save(state, filename)
        # Save Log table
        if type(log) == str:
            logname = filename.replace('.pth.tar','.log')
            logname = logname.replace('_weights','_train')
            log_file = open(logname, "w")
            log_file.write(log)
            log_file.close()

    def _iterate_train(self, data_loader):

        # Init loss count
        lotal_loss = 0
        data_train_len = len(self.train_set)

        # Active train
        self.model.train()
        self.model = self.model.to(self.device)

        # Batch iteration - Training dataset
        for batch_idx, (names, imgs, targets) in enumerate(tqdm.tqdm(data_loader, desc="Training epoch")):
            batches_done = len(data_loader) * self.epoch + batch_idx

            targets = Variable(targets.to(self.device), requires_grad=False)
            imgs = Variable(imgs.to(self.device))
            bs = len(imgs)

            # Forward and loss
            loss, output = self.model(imgs, targets=targets)
            loss.backward()

            if batches_done % self.gradient_accumulations:
                # Accumulates gradient before each step
                self.optimizer.step()
                self.optimizer.zero_grad()

            self.model.seen += imgs.size(0)

            # Log metrics at each YOLO layer
            batch_factor = bs / data_train_len
            for i, metric in enumerate(self.metrics):
                out_metrics = [(yolo.metrics.get(metric, 0) * batch_factor) for yolo in self.model.yolo_layers]
                # Fill average
                for j in range(len(self.avg_metrics[metric])):
                    self.avg_metrics[metric][j] += out_metrics[j]

            lotal_loss += loss.item() * batch_factor

        return lotal_loss

    def _logging(self, epoch, avg_loss_train, val_evaluation):

        # 1. Log scalar values (scalar summary)
        info = val_evaluation
        info.append(('train_loss_total', avg_loss_train))
        for tag, value in info:
            self.logger.add_scalar(tag, value, epoch+1)

        # 2. Log values and gradients of the parameters (histogram summary)
        for yolo_tag, value in self.model.named_parameters():
            # Define tag name
            tag_parts = yolo_tag.split('.')
            tag = self.model_name + '/' + tag_parts[-2] + '/' + tag_parts[-1]
            # Ignore bias from batch normalization
            if (not 'batch_norm' in tag_parts[-2]) or (not 'bias' in tag_parts[-1]):
                # add data to histogram
                self.logger.add_histogram(tag, value.data.cpu().numpy(), epoch+1)
                # add gradient if exist
                #if not value.grad is None:
                #    self.logger.add_histogram(tag +'/grad', value.grad.data.cpu().numpy(), epoch+1)

    def train(self, epochs=100, batch_size=4):
        '''
        Train network function
        Arguments:
            @param net: network model
            @param epochs: number of training epochs (int)
            @param batch_size: batch size (int)
        '''

        # Load Dataset
        data_loader_train = DataLoader(self.train_set, batch_size=batch_size, shuffle=True,
                                        collate_fn=self.train_set.collate_fn_yolo)
        data_loader_val = DataLoader(self.valid_set, batch_size=1, shuffle=False,
                                        collate_fn=self.valid_set.collate_fn_yolo)

        # Define parameters
        best_loss = 1000000    # Init best loss with a too high value
        best_ap = 0            # Init best average precision as zero

        # Run epochs
        for e in range(epochs):
            self.epoch = e
            print('Starting epoch {}/{}.'.format(self.epoch + 1, epochs))
            log_str = ''
            metric_table = [["Metrics", *["YOLO Layer " + str(i) for i in range(len(model.yolo_layers))]]]
            self.avg_metrics = { i : [0]*len(self.model.yolo_layers) for i in self.metrics }

            # ========================= Training =============================== #
            loss_train = self._iterate_train(data_loader_train)

            # Log metrics at each YOLO layer
            for i, metric in enumerate(self.metrics):
                formats = {m: "%.6f" for m in self.metrics}
                formats["grid_size"] = "%2d"
                formats["cls_acc"] = "%.2f%%"
                row_metrics = self.avg_metrics[metric]
                metric_table += [[metric, *row_metrics]]

            log_str += AsciiTable(metric_table).table
            log_str += "\nTotal loss: %0.5f"%loss_train

            print(log_str)
            print('')

            # ========================= Validation ============================= #
            precision, recall, AP, f1, ap_class = evaluate(self.model,
                                                        data_loader_val,
                                                        self.iou_thres,
                                                        self.conf_thres,
                                                        self.nms_thres,
                                                        self.device)
            # Group metrics
            evaluation_metrics = [
                ("val_precision", precision.mean()),
                ("val_recall", recall.mean()),
                ("val_mAP", AP.mean()),
                ("val_f1", f1.mean()),
            ]

            # Print class APs and mAP
            ap_table = [["Index", "Class name", "AP"]]
            for i, c in enumerate(ap_class):
                ap_table += [[c, self.class_names[c], "%.5f" % AP[i]]]
            print(AsciiTable(ap_table).table)
            print("mAP: "+ str(AP.mean()))
            print('\n')

            # ======================== Save weights ============================ #
            best_loss = loss_train if loss_train <= best_loss else best_loss
            is_best = AP.mean() >= best_ap
            if is_best:
                best_ap = AP.mean()
                # save
                self._saveweights({
                        'epoch': self.epoch + 1,
                        'state_dict': self.model.state_dict(),
                        'train_loss_total': loss_train,
                        'train_best_loss': best_loss,
                        'val_precision': precision.mean(),
                        'val_recall': recall.mean(),
                        'val_mAP': AP.mean(),
                        'val_f1': f1.mean(),
                        'batch_size': batch_size,
                        'optimizer': str(self.optimizer),
                        'optimizer_dict': self.optimizer.state_dict(),
                        'device': str(self.device),
                        'avg_metrics': self.avg_metrics,
                        'iou_thres': self.iou_thres,
                        'conf_thres': self.conf_thres,
                        'nms_thres': self.nms_thres
                    },
                    log=log_str )

                print('Model {:s} updated!'.format(self.train_name))
                print('\n')

            # ====================== Tensorboard Logging ======================= #
            if self.logger:
                self._logging(self.epoch, loss_train, evaluation_metrics)

def parse_yolo_name(backbone_name, num_anchors, num_classes):
    """
    Get the .cfg filename given the Yolo v3 hyperparameters.
    """
    model_name = 'yolov3'

    if 'tiny' in backbone_name:
        model_name += '-tiny'
    elif 'spp' in backbone_name:
        model_name += '-spp'
    model_name += '_a' + str(num_anchors)
    model_name += '_c' + str(num_classes)

    return model_name

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    # Training parameters
    parser.add_argument("--batch_size", type=int, default=4, help="size of each image batch")
    parser.add_argument("--num_epochs", type=int, default=150, help="size of each image batch")
    parser.add_argument("--model_name", type=str, default="yolov3", help="name of the model definition (used to load the .cfg file)")
    parser.add_argument("--num_anchors", type=int, default=6, help="number of anchors")
    parser.add_argument("--num_classes", type=int, default=3, help="number of classes")
    # Evaluation parameters
    parser.add_argument("--iou_thres", type=float, default=0.5, help="iou threshold required to qualify as detected")
    parser.add_argument("--conf_thres", type=float, default=0.5, help="object confidence threshold")
    parser.add_argument("--nms_thres", type=float, default=0.4, help="iou thresshold for non-maximum suppression")

    opt = parser.parse_args()
    print(opt)

    # Classes names
    cls_names = ['background','follicle','ovary']

    # Input parameters
    n_classes = opt.num_classes
    has_ovary = True if n_classes > 2 else False
    n_epochs = opt.num_epochs
    batch_size = opt.batch_size
    network_name = parse_yolo_name(opt.model_name, opt.num_anchors, n_classes)
    train_name = gettrainname(network_name)
    mode_config_path = 'config/'+ network_name +'.cfg'

    # Load network model
    model = Darknet(mode_config_path)

    # Load CUDA if exist
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # Transformation parameters
    transform = tsfrm.Compose([tsfrm.RandomHorizontalFlip(p=0.5),
                           tsfrm.RandomVerticalFlip(p=0.5),
                           tsfrm.RandomAffine(90, translate=(0.15, 0.15),
                            scale=(0.75, 1.5), resample=3, fillcolor=0)
                           ])

    # Dataset definitions
    dataset_train = OvaryDataset(im_dir='../datasets/ovarian/im/train/',
                           gt_dir='../datasets/ovarian/gt/train/',
                           clahe=False, transform=transform,
                           ovary_inst=has_ovary)
    dataset_val = OvaryDataset(im_dir='../datasets/ovarian/im/val/',
                           gt_dir='../datasets/ovarian/gt/val/',
                           clahe=False, transform=False,
                           ovary_inst=has_ovary)

    # Optmization
    optimizer = optim.Adam(model.parameters())

    # Set logs folder
    log_dir = '../logs/' + train_name + '/'
    writer = SummaryWriter(log_dir=log_dir)

    # Run training
    training = Training(model, device, dataset_train, dataset_val,
                        optimizer,
                        logger=writer,
                        class_names=cls_names[:n_classes],
                        train_name=train_name,
                        iou_thres=opt.iou_thres,
                        conf_thres=opt.conf_thres,
                        nms_thres=opt.nms_thres)
    training.train(epochs=n_epochs, batch_size=batch_size)

    print('')


"""The abstract class for http routing"""

from abc import ABCMeta, abstractmethod
from typing import AbstractSet, Any, Mapping, Tuple

from .http_callbacks import HttpRequestCallback
from .http_response import HttpResponse

class HttpRouter(metaclass=ABCMeta):
    """The interface for an HTTP router"""

    @property  # type: ignore
    @abstractmethod
    def not_found_response(self) -> HttpResponse:
        """The response when a handler could not be found for a method/path

        Returns:
            HttpResponse: The response when a route cannot be found.
        """

    @not_found_response.setter  # type: ignore
    @abstractmethod
    def not_found_response(self, value: HttpResponse) -> None:
        ...

    @abstractmethod
    def add(
            self,
            methods: AbstractSet[str],
            path: str,
            callback: HttpRequestCallback
    ) -> None:
        """Add an HTTP request handler

        Args:
            methods (AbstractSet[str]): The supported HTTP methods.
            path (str): The path.
            callback (HttpRequestCallback): The request handler.
        """

    @abstractmethod
    def resolve(
            self,
            method: str,
            path: str
    ) -> Tuple[HttpRequestCallback, Mapping[str, Any]]:
        """Resolve a request to a handler with the route matches

        Args:
            method (str): The HTTP method.
            path (str): The path.

        Returns:
            Tuple[HttpRequestCallback, Mapping[str, Any]]: A handler and the route
                matches.
        """


import firebase_admin
from firebase_admin import db
from flask import jsonify
from hashlib import md5
from random import randint
from time import time
from time import time, sleep

firebase_admin.initialize_app(options={
    'databaseURL': 'https://copy-passed.firebaseio.com',
})

waitlist = db.reference('waitlist')
ids = db.reference('ids')

timeoutSeconds = 120

def put(ref, data):
    orig = ref.get()
    if not orig:
        return False
    for i, j in data.items():
        orig[i] = j
    ref.update(orig)
    return True

def add_user(request):
    delBlankw = False
    delBlanki = False

    if waitlist.get() and request.json["id"] in waitlist.get():
        return "Conflict", 409

    if not waitlist.get():
        delBlankw = True
        db.reference().update({"waitlist": {"blank--": 0}})

    if not ids.get():
        delBlanki = True
        db.reference().update({"ids": {"blank--": 0}})

    put(waitlist, {request.json["id"]: "x"})
    start = time()
    # print("entering waitlist")
    while waitlist.child(request.json["id"]).get() == "x":
        sleep(.5)
        if (time() - start) >= timeoutSeconds:
            if delBlanki:
                ids.child("blank--").delete()
            if delBlankw:
                waitlist.child("blank--").delete()
            waitlist.child(request.json["id"]).delete()
            return "Request Timeout", 408

    uuid = waitlist.child(request.json["id"]).get()
    waitlist.child(request.json["id"]).delete()

    if not ids.get():
        delBlanki = True
        db.reference().update({"ids": {"blank--": 0}})

    idu = get_id(uuid)
    while idu in ids.get():
        idu = get_id(uuid)

    put(ids, {idu: {"uid": uuid, "timestamp": time()}})
    # print(ids)
    if delBlanki:
        ids.child("blank--").delete()
    if delBlankw:
        waitlist.child("blank--").delete()
    return jsonify({"id": idu}), 201

def get_id(oid):
    return md5((str(randint(0, 1e12)) + oid).encode()).hexdigest()

def authenticator(request):
    if not request.json or 'id' not in request.json:
        return "Not Acceptable", 406
    if "revoke" in request.json and request.json["revoke"]:
        if ids.get() and ids.child(request.json["id"]).get():
            ids.child(request.json["id"]).delete()
            return "Deleted", 200
        else:
            return "Not Found", 204
    if request.path == '/' or request.path == '':
        if request.method == 'POST':
            return add_user(request)
        else:
            return 'Method not supported', 405
    return 'URL not found', 404


#!/usr/bin/env python
import pygame
import constants
from network import Type
import physical_object
from physical_object import PhysicalObject
import bullet

import math

from pygame.rect import Rect
import play_sound
from pygame import mixer
from pygame.mixer import Sound
TURRET_WIDTH = 24
TURRET_HEIGHT = 28

GUN_CHARGEUP_TIME = 100

class Turret(PhysicalObject):
	"""This class represents a turret"""

	typ = Type.TURRET
	timeLeftToCharge = 0

	def __init__(self, position, level):

		PhysicalObject.__init__(self, position)

		self.level = level
		self.controllingPlayer = physical_object.OWNER_DEFENDER

		self.physicsRect = pygame.rect.Rect(self.r_x, self.r_y, TURRET_WIDTH, TURRET_HEIGHT)

		self.image = pygame.image.load('images/defenses.png')
		self.rect = self.image.get_rect()

		self.actions = {"charged 0": (0, 112, TURRET_WIDTH, TURRET_HEIGHT),
			"charged 50": (TURRET_WIDTH, 112, TURRET_WIDTH, TURRET_HEIGHT),
			"charged 100": (2*TURRET_WIDTH, 112, TURRET_WIDTH, TURRET_HEIGHT)}

		self.boundsRect = Rect(level.rect.x,level.rect.y,level.rect.width,constants.SCREEN_HEIGHT)

		self.action = "charged 0"
		self.area = pygame.rect.Rect(self.actions[self.action])
		#print 'turret (x,y) = ', (self.r_x, self.r_y)
		#print 'turret owner = ', self.controllingPlayer

		self.timeLeftToCharge = GUN_CHARGEUP_TIME

	def step(self, scrollPosition):

		# translate movement boundary
		self.boundsRect.y = scrollPosition

		# update self
		PhysicalObject.step(self, scrollPosition)
		if self.timeLeftToCharge < (1/5.0)*GUN_CHARGEUP_TIME:
			self.action = "charged 100"
		elif self.timeLeftToCharge < (3/5.0)*GUN_CHARGEUP_TIME:
			self.action = "charged 50"
		else:
			self.action = "charged 0"
		self.area = pygame.rect.Rect(self.actions[self.action])

		if self.physicsRect.colliderect(self.boundsRect):
			turretSeesShip = False
			target = None
			for o in self.level.physicalObjects:
				if(o.controllingPlayer == physical_object.OWNER_ATTACKER and
				o.targetType == physical_object.TARGET_TYPE_SHIP):
					turretSeesShip = True
					target = o
			if turretSeesShip:
				self.timeLeftToCharge -= 1
				if self.timeLeftToCharge <= 0:
					# it's the ship! get it!
					soundEfx = pygame.mixer.Sound(constants.TURRET_BULLET_SFX)
					soundEfx.set_volume(0.5)
					play_sound.PlaySounds(soundEfx, 2)
					theBullet = bullet.Bullet((self.rect.x + TURRET_WIDTH/2 - bullet.BULLET_WIDTH/2, self.rect.y + (bullet.BULLET_HEIGHT + 6)), "tur")
					theBullet.controllingPlayer = self.controllingPlayer
					# old velocity code
					#deltaX = o.r_x - self.r_x
					#deltaY = o.r_y - self.r_y
					#distance = math.hypot(deltaX, deltaY)
					#theBullet.v_x = bullet.DEFAULT_SPEED*(deltaX/distance)  # v_x = speed*cos
					#theBullet.v_y = bullet.DEFAULT_SPEED*(deltaY/distance)  # v_y = speed*sin
					# new velocity code; apparently tries to divide by zero and take the square root of a negative number
					#timeToImpact = ((o.r_x*o.v_x + o.r_y*o.v_y + math.sqrt(-pow(o.r_y,2)*(-1 + pow(o.v_x, 2)) + o.r_x*(o.r_x + 2*o.r_y*o.v_x*o.v_y - o.r_x*pow(o.v_y, 2))))/(-1 + pow(o.v_x, 2) + pow(o.v_y, 2)))
					#theBullet.v_x = (o.r_x + timeToImpact*o.v_x)/timeToImpact
					#theBullet.v_y = (o.r_y + timeToImpact*o.v_y)/timeToImpact
					# new velocity code, mk. II
					futurepos = (target.r_x, target.r_y)  # Guess that where they'll be in the future is where they are now
					MY_SPEED = 1.5 + constants.SCROLL_RATE
					for i in range(0, 4):
						dist = (futurepos[0] - self.r_x, futurepos[1] - self.r_y)
						timetotarget = math.hypot(dist[0], dist[1]) / bullet.DEFAULT_SPEED
						distcovered = (target.v_x*timetotarget, target.v_y*timetotarget)
						futurepos = (target.r_x + distcovered[0], target.r_y + distcovered[1])
					dirNotNormalized = (futurepos[0] - self.r_x, futurepos[1] - self.r_y)
					dirNormalized = ((dirNotNormalized[0]/math.hypot(dirNotNormalized[0], dirNotNormalized[1]),
						dirNotNormalized[1]/math.hypot(dirNotNormalized[0], dirNotNormalized[1])))
					theBullet.v_x = MY_SPEED*dirNormalized[0]
					theBullet.v_y = MY_SPEED*dirNormalized[1]
					# end of velocity code
					self.childObjects.append(theBullet)
					self.timeLeftToCharge = GUN_CHARGEUP_TIME
			else:  # if the turret doesn't see the ship,
				self.timeLeftToCharge = GUN_CHARGEUP_TIME  # then the turret should power down


# to build, use "cd (playsong directory)"
# pyinstaller --onefile playSong.py

#lib imports
import keyboard
import threading
import time
import os
import re

#local imports
from settings import SETTINGS,map_velocity,apply_range_bounds

global isPlaying
global midi_action_list

isPlaying = False
storedIndex = 0
conversionCases = {'!': '1', '@': '2', '£': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0'}

"""
#maps a string representing a note to a note index where C0 = 0
note_offsets = {"C":0,"D":2,"E":4,"F":5,"G":7,"A":9,"B":11}
def note_to_index(note):
	is_sharp = (note[1] == "#")
	note_letter = note[0]
	if is_sharp:
		note_number = int(note[2:])
	else:
		note_number = int(note[1:])
	index = note_offsets[note_letter] + int(is_sharp) + 12*note_number
	return index

octave_note_order = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"]
def index_to_note(index):
	base_letter = "A"
	base_octave = 0
	val = 21
	#A0 is value 21 in midi
	octave = (index - 12) // 12
	letter = (index - 12) % 12
	return octave_note_order[letter] + str(octave)
"""

def onDelPress(event):
	global isPlaying
	isPlaying = not isPlaying

	if isPlaying:
		print("Playing...")
		playNextNote()
	else:
		print("Stopping...")

	return True

def isShifted(charIn):
	#print(charIn)
	if "shift" in charIn:
		return True
	asciiValue = ord(charIn)
	if(asciiValue >= 65 and asciiValue <= 90):
		return True
	if(charIn in "!@#$%^&*()_+{}|:\"<>?"):
		return True
	return False

def pressLetter(strLetter):
	if isShifted(strLetter):
		# we have to convert all symbols to numbers
		if strLetter in conversionCases:
			strLetter = conversionCases[strLetter]
		keyboard.release(strLetter.lower())

		keyboard.press("shift")
		keyboard.press(strLetter.lower())
		keyboard.release("shift")
		if SETTINGS.get("key_instant_release") == True:
			keyboard.release(strLetter.lower())

	else:
		keyboard.release(strLetter)
		keyboard.press(strLetter)

		if SETTINGS.get("key_instant_release") == True:
			keyboard.release(strLetter)
	return

def releaseLetter(strLetter):
	if isShifted(strLetter):
		if strLetter in conversionCases:
				strLetter = conversionCases[strLetter]
		keyboard.release(strLetter.lower())
	else:
		keyboard.release(strLetter)
	return

#Mini class to organize different actions into standard chunks
class Midi_Action:
	def __init__(self,offset,note_list,velocity,tempo_change):
		self.tempo_change = tempo_change
		self.note_list = note_list
		self.velocity = velocity
		self.offset = offset

def processFile(song_path):
	global playback_speed
	with open(song_path,"r") as macro_file:
		lines = macro_file.read().split("\n")
		processed_notes = []

		for line in lines:
			if len(line.strip()) == 0:
				continue

			try:
				#print(line)
				offset,note_str = line.split(" ",1)
				note_group,velocity = note_str.split(":")
				if "tempo" in note_str:
					tempo_change = int(note_group.split("tempo=")[1])
					note_list = []
				else:
					tempo_change = None

					note_list = note_group.split(" ")
					new_note_list = []
					for n in note_list:
						v = apply_range_bounds(int(n))
						if v is not None:
							new_note_list.append(SETTINGS["key_map"][v])
					note_list = new_note_list
					#print(note_list)
					#input()

				m = Midi_Action(	float(offset),
									note_list,
									int(velocity),
									tempo_change)
				processed_notes.append(m)
			except Exception as e:
				print(f"Error reading line:: '{line}'")
				print(e)
				input()

	return processed_notes

# for this method, we instead use delays as l[0] and work using indexes with delays instead of time
# we'll use recursion and threading to press keys
def set_note_offsets(midi_action_list):
	# parse time between each note
	# while loop is required because we are editing the array as we go
	i = 0
	while i < len(midi_action_list)-1:
		note = midi_action_list[i]
		nextNote = midi_action_list[i+1]
		if note.tempo_change:
			tempo = 60/float(note.tempo_change)
			midi_action_list.pop(i)

			note = midi_action_list[i]
			if i < len(midi_action_list)-1:
				nextNote = midi_action_list[i+1]
		else:
			note.offset = (nextNote.offset - note.offset) * tempo
			i += 1

	# let's just hold the last note for 1 second because we have no data on it
	midi_action_list[-1].offset = 1.00

	return midi_action_list

def playNextNote():
	global isPlaying
	global storedIndex
	global playback_speed

	while isPlaying and storedIndex < len(midi_action_list):
		note = midi_action_list[storedIndex]
		delay = max(note.offset,0)

		if note.velocity == 0:
			#release notes
			for n in note.note_list:
				releaseLetter(n)
		else:
			#press notes
			if SETTINGS.get("alt_velocity",False) == True:
				vel_key = map_velocity(note.velocity)
				print("alt+",vel_key)
				keyboard.press("alt")
				keyboard.press_and_release(vel_key)
				keyboard.release("alt")

			if SETTINGS.get("hold_to_play",False) == True:
				while not keyboard.is_pressed(SETTINGS.get("hold_to_play_key")):
					time.sleep(.05)

			for n in note.note_list:
				pressLetter(n)
		if(note.tempo_change is None and note.velocity != 0):
			print("%10.2f %15s %d" % (delay,"".join(note.note_list),note.velocity))
		#print("%10.2f %15s" % (delay/playback_speed,noteInfo[1]))
		storedIndex += 1
		if(delay != 0):
			threading.Timer(delay/playback_speed, playNextNote).start()
			return
	if storedIndex > len(midi_action_list)-1:
		isPlaying = False
		storedIndex = 0
	return

#TODO (BUG)
#Rewind and Fast Forward skip over tempo events
#	missing a critical tempo event will change playback significantly.
def rewind(KeyboardEvent):
	global storedIndex
	if storedIndex - 10 < 0:
		storedIndex = 0
	else:
		storedIndex -= 10

	print("Rewound to %.2f" % storedIndex)

def skip(KeyboardEvent):
	global storedIndex
	if storedIndex + 10 > len(midi_action_list):
		isPlaying = False
		storedIndex = 0
	else:
		storedIndex += 10
	print("Skipped to %.2f" % storedIndex)

def get_file_choice(song_dir):
	fileList = os.listdir(song_dir)
	songList = []
	for f in fileList:
		if(".txt" in f or ".txt" in f.lower()):
			songList.append(f)
	print("\nType the number of a song file then press enter:\n")
	for i in range(len(songList)):
		print(i+1,":",songList[i])

	choice = int(input(">"))
	print()
	choice_index = int(choice)
	return songList[choice_index-1],songList

def mode_play(song_path):
	global isPlaying
	global midi_action_list
	global playback_speed

	playback_speed = SETTINGS["playback_speed"]
	isPlaying = False
	storedIndex = 0

	midi_action_list = processFile(song_path)
	set_note_offsets(midi_action_list)

	keyboard.on_press_key(SETTINGS["pause_key"], onDelPress)
	keyboard.on_press_key(SETTINGS["rewind_key"], rewind)
	keyboard.on_press_key(SETTINGS["advance_key"], skip)

	print()
	print("Controls")
	print("-"*20)
	print(f"Press {SETTINGS['pause_key'].upper()} to play/pause")
	print(f"Press {SETTINGS['rewind_key'].upper()} to rewind")
	print(f"Press {SETTINGS['advance_key'].upper()} to advance")
	if SETTINGS.get("hold_to_play",False) == True:
		print(f"Hold {SETTINGS['hold_to_play_key'].upper()} while song is unpaused to play notes")
	while True:
		input("Press Ctrl+C to go back\n\n")

def main():
	song_dir = SETTINGS["song_dir"]

	while True:
		song_choice,_ = get_file_choice(song_dir)
		song_path = os.path.join(song_dir,song_choice)
		try:
			mode_play(song_path)
		except KeyboardInterrupt as e:
			pass
		finally:
			keyboard.unhook_all()
			storedIndex = 0
			isPlaying = False

if __name__ == "__main__":
	main()


#%%
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import matplotlib.ticker as ticker
from matplotlib import rcParams
import numpy as np
from highlight_text import fig_text
import pandas as pd

from PIL import Image
import urllib
import os

df = pd.read_csv("success_rate_2022_2023.csv", index_col = 0)
df = (
        df
        .sort_values(by = ["variable", "value"], ascending = True)
        .reset_index(drop = True)
)

fig = plt.figure(figsize=(6.5, 10), dpi = 200, facecolor="#EFE9E6")
ax = plt.subplot(111, facecolor = "#EFE9E6")

# Adjust spines
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)

ax.grid(True, color = "lightgrey", ls = ":")

# Define the series
teams = list(df["team_id"].unique())
Y = np.arange(len(teams))
X_xg = df[df["variable"] == "2022_success_rate"]["value"]
X_goals = df[df["variable"] == "2023_success_rate"]["value"]

# Fix axes limits
ax.set_ylim(-.5, len(teams) - .5)
ax.set_xlim(
    min(X_goals.min(), X_xg.min(), 35),
    max(X_goals.max(), X_xg.max(), 55)
)

# Scatter plots
ax.scatter(X_xg, Y, color = "#74959A", s = 200, alpha = 1, zorder = 3)
ax.scatter(X_goals, Y, color = "#495371", s = 200, alpha = 1, zorder = 3)
ax.scatter(X_xg, Y, color = "none", ec = "#74959A", s = 180, lw = 2.5, zorder = 3)
ax.scatter(X_goals, Y, color = "none", ec = "#495371", s = 180, lw = 2.5, zorder = 3)

# Add line chart between points and difference annotation
for index in Y:
    difference = X_xg.iloc[index] - X_goals.iloc[index]
    if difference > 0:
        color = "#74959A"
        x_adj = -1.75
        anot_position = X_xg.iloc[index]
        anot_aux_sign = "-"
    else:
        color = "#495371"
        x_adj = 1.75
        anot_position = X_goals.iloc[index]
        anot_aux_sign = "+"

    ax.annotate(
        xy = (anot_position, index),
        text = f"{anot_aux_sign} {abs(difference):.1f}",
        xytext = (13, -2),
        textcoords = "offset points",
        size = 8,
        color = color,
        weight = "bold"
    )

    if abs(difference) < 1.3:
        continue
    if abs(difference) < -1.1:
        continue
    ax.plot(
        [X_xg.iloc[index] + x_adj, X_goals.iloc[index] + x_adj*(-1)],
        [index, index],
        lw = 1,
        color = color,
        zorder = 2
    )

DC_to_FC = ax.transData.transform
FC_to_NFC = fig.transFigure.inverted().transform

# Native data to normalized data coordinates
DC_to_NFC = lambda x: FC_to_NFC(DC_to_FC(x))

logos_folder = "nfl_logos/"
# Modify the loop to fetch logos from the local folder
for index, team_id in enumerate(teams):
    ax_coords = DC_to_NFC([33, index - 0.55])
    logo_ax = fig.add_axes([ax_coords[0], ax_coords[1], 0.04, 0.04], anchor="C")

    # Use the local path to the logos folder
    logo_path = f"{logos_folder}{team_id:.0f}.png"

    try:
        # Check if the file exists before opening
        with Image.open(logo_path) as club_icon:
            logo_ax.imshow(club_icon.convert("LA"))
            logo_ax.axis("off")
    except FileNotFoundError:
        print(f"Logo not found for team ID {team_id}")

# Remove tick labels
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
false_ticks = ax.set_yticklabels([])

fig_text(
    x = 0.15, y = .9,
    s = "Through 10 weeks, only 3 NFL Teams\nhave outperformed their <2022> \noffensive success rate in <2023>",
    highlight_textprops = [
        {"color":"#74959A"},
        {"color": "#495371"}
    ],
    va = "bottom", ha = "left",
    fontsize = 14, color = "black", weight = "bold"
)
fig_text(
	x = 0.15, y = .885,
    s = "Source: rbsdm.com | Viz by Ray Carpenter | inspired by a viz by @sonofacorner",
	va = "bottom", ha = "left",
	fontsize = 8, color = "#4E616C"
)

# # ---- The League's logo
league_icon = Image.open("nfl_logos/NFL.png")
league_ax = fig.add_axes([0.055, 0.89, 0.065, 0.065], zorder=1)
league_ax.imshow(league_icon)
league_ax.axis("off")

plt.savefig(
	"06202022_bundelsiga_xg.png",
	dpi = 500,
	facecolor = "#EFE9E6",
	bbox_inches="tight",
    edgecolor="none",
	transparent = False
)

plt.savefig(
	"06202022_bundelsiga_xg_tr.png",
	dpi = 500,
	facecolor = "none",
	bbox_inches="tight",
    edgecolor="none",
	transparent = True
)


from collections import deque
ulaz = open('ulaz.txt', 'r')
sve = ulaz.read()
ulaz.close()
igrači = sve.split('\n\n')
prvi = deque(igrači[0].split('\n')[1:])
drugi = deque(igrači[1].split('\n')[1:])
while len(prvi) != 0 and len(drugi) != 0:
    a = int(prvi.popleft())
    b = int(drugi.popleft())
    if a > b:
        prvi.extend([str(a), str(b)])
    else:
        drugi.extend([str(b), str(a)])
if len(prvi) > 0:
    l = prvi
else:
    l = drugi
l.reverse()
rezultat = 0
for i in range(len(l)):
    rezultat += int(l[i]) * (i + 1)
print(rezultat)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import sys
import types
import re
import subprocess
import unitTestUtil
import logging

sensorDict = {}
util_support_map = ['fbttn', 'fbtp', 'lightning', 'minipack', 'fby2', 'yosemite']
multi_host_map = ['fby2', 'yosemite']
lm_sensor_support_map = ['wedge', 'wedge100', 'galaxy100', 'cmm']

def sensorTest(platformType, data, util):
    """
    Check that sensor data is with spec or is just present
    """
    # no drivers present from sensor cmd
    if platformType in util_support_map:
        failed = sensorTestUtil(platformType, data, util)
    # drivers present from sensor cmd
    else:
        failed = sensorTestNetwork(platformType, data, util)
    if len(failed) == 0:
        print("Sensor Readings on " + platformType + " [PASSED]")
        sys.exit(0)
    else:
        print("Sensor Readings on " + platformType + " for keys: " +
              str(failed) + " [FAILED]")
        sys.exit(1)

def sensorTestNetwork(platformType, data, util):
    failed = []
    createSensorDictNetworkLmSensors(util)
    logger.debug("Checking values against json file")
    for driver in data:
        if isinstance(data[driver], dict):
            for reading in data[driver]:
                if data[driver][reading] == "yes":
                    try:
                        raw_value = sensorDict[driver][reading]
                    except Exception:
                        failed += [driver, reading]
                        continue
                    if isinstance(data[driver][reading], list):
                        values = re.findall(r"[-+]?\d*\.\d+|\d+", raw_value)
                        if len(values) == 0:
                            failed += [driver, reading]
                            continue
                        rang = data[driver][reading]
                        if float(rang[0]) > float(values[0]) or float(
                                values[0]) > float(rang[1]):
                            failed += [driver, reading]
                    else:
                        if bool(re.search(r'\d', raw_value)):
                            continue
                        else:
                            failed += [driver, reading]
    return failed

def sensorTestUtil(platformType, data, util):
    failed = []
    createSensorDictUtil(util)
    logger.debug("checking values against json file")
    for sensor in data:
        # skip type argument in json file
        if sensor == "type":
            continue
        try:
            raw_values = sensorDict[sensor]
        except Exception:
            failed += [sensor]
            continue
        if platformType in multi_host_map:
            if len(raw_values) not in [1, 4]:
                failed += [sensor]
                continue
        elif len(raw_values) not in [1]:
            failed += [sensor]
            continue
        if isinstance(data[sensor], list):
            for raw_value in raw_values:
                values = re.findall(r"[-+]?\d*\.\d+|\d+", raw_value)
                if len(values) == 0:
                    failed += [sensor]
                    continue
                rang = data[sensor]
                if float(rang[0]) > float(values[0]) or float(values[0]) > float(
                    rang[1]):
                    failed += [sensor]
        else:
            for raw_value in raw_values:
                if 'ok' not in raw_value:
                    failed += [sensor + raw_value]
                    break
    return failed

def createSensorDictNetworkLmSensors(util):
    """
    Creating a sensor dictionary driver -> sensor -> reading
    Supports wedge, wedge100, galaxy100, and cmm
    """
    cmd = util.SensorCmd
    if cmd is None:
        raise Exception("sensor command not implemented")
    logger.debug("executing command: ".format(cmd))
    f = subprocess.Popen(cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    info, err = f.communicate()
    if len(err) != 0:
        raise Exception(err)
    logger.debug("Creating sensor dictionary")
    info = info.decode('utf-8')
    info = info.split('\n')
    currentKey = ''
    for line in info:
        if ':' in line:
            lineInfo = line.split(':')
            key = lineInfo[0]
            val = ''.join(lineInfo[1:])
            sensorDict[currentKey][key] = val
        elif len(line) == 0 or line[0] == ' ':
            continue
        else:
            sensorDict[line] = {}
            currentKey = line

def createSensorDictUtil(util):
    """
    Creating a sensor dictionary sensor -> reading
    Supports fbtp and fbttn
    """
    cmd = util.SensorCmd
    if cmd is None:
        raise Exception("sensor command not implemented")
    logger.debug("executing command: " + str(cmd))
    f = subprocess.Popen(cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    info, err = f.communicate()
    if len(err) != 0:
        raise Exception(err)
    logger.debug("creating sensor dictionary")
    info = info.decode('utf-8')
    info = info.split('\n')
    for line in info:
        if ':' in line:
            lineInfo = line.split(':')
            key = lineInfo[0]
            val = ''.join(lineInfo[1:])
            if key not in sensorDict:
                sensorDict[key] = []
            sensorDict[key].append(val)
        if "timed out" in line:
            print(line)
            raise Exception(line)

if __name__ == "__main__":
    """
    Input to this file should look like the following:
    python sensorTest.py wedgeSensor.json
    """
    util = unitTestUtil.UnitTestUtil()
    logger = util.logger(logging.WARN)
    try:
        data = {}
        args = util.Argparser(['json', '--verbose'], [str, None],
                              ['json file',
                              'output all steps from test with mode options: DEBUG, INFO, WARNING, ERROR'])
        if args.verbose is not None:
            logger = util.logger(args.verbose)
        data = util.JSONparser(args.json)
        platformType = data['type']
        utilType = util.importUtil(platformType)
        sensorTest(platformType, data, utilType)
    except Exception as e:
        print("Sensor test [FAILED]")
        print("Error code returned: {}".format(e))
    sys.exit(1)


from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
from datetime import datetime as dt
import re
import copy
import MySQLdb

dataBase = MySQLdb

userInput1 = str(input("Please Provide with Calendar link: "))
userInput2 = str(input("Please Provide a file name ending in .sql: "))
userInput3 = str(input("Please select student type: (undergrad or graduate): "))

url = userInput1

# get data from the link
client = uReq(url)
pageHtml = client.read()
client.close()

# create parsabel html
html = soup(pageHtml, "html.parser")

header = html.h2
headTables = header.find_next_siblings("table")

list_of_rows = []

# Loop through the sibling tables of h2 and find tr
for i in headTables:
  rows = i.find_all("tr")

# loop through all the tr's and find td's
  for j in rows:
    list_of_cells = []
    cols = j.find_all("td")

    # loop through all the td's and get data
    for data in cols:
      event = data.text.replace("\r\n\t\t\t",' ')

      # assignment based on regex
      time = re.match(r"[ADFJMNOS]\w* [\d]{1,2}, [\d]{4}",event)
      doubleTime = re.match(r"[ADFJMNOS]\w* [\d]{1,2} to [\d]{1,2}, [\d]{4}", event)
      doubleDates = re.match(r"[ADFJMNOS]\w* [\d]{1,2} and [\d]{1,2}, [\d]{4}", event)
      crossYear = re.match(r"[ADFJMNOS]\w* [\d]{1,2}, [\d]{4} to [ADFJMNOS]\w* [\d]{1,2}, [\d]{4}", event)

      global updateEvent

      if crossYear:

        dates = re.split(r"\ to \ |\ |,\ ",event)
        # get the start date numbers and join
        startDates = " ".join([dates[0],dates[1],dates[2]])
        # get the end date values and join
        endDates = " ".join([dates[4],dates[5],dates[6]])
        startTime = dt.strptime(startDates, "%B %d %Y")
        endTime = dt.strptime(endDates, "%B %d %Y")
        finalDates = ' to '.join([str(startTime), str(endTime)])
        event = re.sub(r"[ADFJMNOS]\w* [\d]{1,2}, [\d]{4} to [ADFJMNOS]\w* [\d]{1,2}, [\d]{4}",str(finalDates), event)
        # print(event)
      # converts Month Day, Year
      elif time:
        timeVal = dt.strptime(time.group(), "%B %d, %Y")
        # global variable for storage of date value
        updateEvent = timeVal
        event = re.sub(r"[ADFJMNOS]\w* [\d]{1,2}, [\d]{4}",str(timeVal), event)

      # convets date in format: Month Day to Day, Year
      elif doubleTime:
        dates = re.split(r"\ to\ |\ |,\ ", event)
        # get the start date numbers and join
        startDates = " ".join([dates[0],dates[1],dates[3]])
        # get the end date values and join
        endDates = " ".join([dates[0],dates[2],dates[3]])
        startTime = dt.strptime(startDates, "%B %d %Y")
        endTime = dt.strptime(endDates, "%B %d %Y")
        #global variable for empty date
        updateEvent = str(startTime)

        finalDates = ' to '.join([str(startTime), str(endTime)])
        # print(finalDates)
        event = re.sub(r"[ADFJMNOS]\w* [\d]{1,2} to [\d]{1,2}, [\d]{4}",str(finalDates), event)

      # converts Month Day and Day, Year
      elif doubleDates:
        dates = re.split(r"\ and\ |\ |,\ ", event)
        # get the start date numbers and join
        startDates = " ".join([dates[0],dates[1],dates[3]])
        # get the end date values and join
        endDates = " ".join([dates[0],dates[2],dates[3]])
        startTime = dt.strptime(startDates, "%B %d %Y")
        #global variable for empty dates
        endTime = dt.strptime(endDates, "%B %d %Y")
        updateEvent = str(startTime)
        finalDates = ' to '.join([str(startTime), str(endTime)])

        event = re.sub(r"[ADFJMNOS]\w* [\d]{1,2} and [\d]{1,2}, [\d]{4}",str(finalDates), event)

      else :
        # Fill in all the date values that are empty with date value
        # before it
        event = re.sub(r"\xa0", str(updateEvent), event)

      #append to list of cols
      list_of_cells.append(event)

    newCells = copy.deepcopy(list_of_cells[0])

    toSplit = re.match(r"[\d]{4}-[\d]{1,2}-[\d]{1,2} [\d]{1,2}:[\d]{1,2}:[\d]{1,2} to [\d]{4}-[\d]{1,2}-[\d]{1,2} [\d]{1,2}:[\d]{1,2}:[\d]{1,2}", newCells)

    global endTimes
    if toSplit:
      # print(toSplit.group())
      newSplit = toSplit.group().split(' to')
      endTimes = newSplit[1].replace("00:00:00","11:59:59")
    else:
      endTimes = newCells.replace("00:00:00","11:59:59")

    list_of_cells.append(endTimes)

    startDate = list_of_cells[0].split('to')

    strParts = list_of_cells[1].split('. ')

    global title, description
    if len(strParts) > 1:
      title = str(strParts[0])
      description = str(strParts[1])
    else:
      title= str(strParts[0])
      description = str(strParts[0])

    newTitle =str(dataBase.escape_string(title))
    newDesc =str(dataBase.escape_string(description))

    query = "INSERT INTO tbl_entries ( event_name, event_description, event_categories, event_tags, event_startdate, event_enddate, open_to, location_building, location_room, location_campus, location_other, start_hour, start_minute, start_ampm, end_hour, end_minute, end_ampm, contact_event_firstname, contact_event_lastname, contact_event_phonenumber, contact_event_phoneext, contact_event_email, contact_firstname, contact_lastname, contact_phonenumber, contact_phoneext, contact_email, event_url, event_url_protocol, upload_image, date_submitted, date_approved, repeated, repeat_freq, repeat_day, repeat_until, repeat_until_date, repeat_until_num, clickable, pending, approved, archived, cancelled, frontpage, submission_ip) VALUES "

    registrars = str(dataBase.escape_string("'Registar's'"))[1:]

    global values
    if userInput3 == 'undergrad':
      values = "(" + str(newTitle)[1:] + " ," + str(newDesc)[1:]+ ", '73', '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0', '"+ startDate[0] + "','" + endTimes + "', '29', 0, '', 2, 'Ontario Tech', 0, 0, 'am', 11, 59, 'pm', "+ registrars +", 'Office', '905.721.3190', '', 'connect@uoit.ca', " + registrars + ", 'Office', '905.721.3190', '', 'connect@uoit.ca', '" + url + "', 'https', NULL, '" + str(dt.now())+ "', '" + str(dt.now()) + "', 0, '', '', 0, '" + str(dt.now()) + "', 0, 1, 0, 1, 0, 0, 0, '00.000.0.000'),"

    elif userInput3 == 'graduate':
      values = "(" + str(newTitle)[1:] + " ," + str(newDesc)[1:]+ ", '74', '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0', '"+ startDate[0] + "','" + endTimes + "', '29', 0, '', 2, 'Ontario Tech', 0, 0, 'am', 11, 59, 'pm','School of Graduate', 'and Postdoctoral Studies', '905.721.8668', '6209', 'connect@uoit.ca', 'School of Graduate', 'and Postdoctoral Studies', '905.721.8668', '6209','connect@uoit.ca', '" + url + "', 'https', NULL, '" + str(dt.now())+ "', '" + str(dt.now()) + "', 0, '', '', 0, '" + str(dt.now()) + "', 0, 1, 0, 1, 0, 0, 0, '00.000.0.000'),"

    #append to rows
    list_of_rows.append(values)

lastString = str(list_of_rows.pop(len(list_of_rows)-1))
lastString = lastString[:-1] +';'
list_of_rows.append(lastString)

outfile = open(userInput2, "w")
outfile.write(query)
for item in list_of_rows:
  outfile.write("%s\n" % item)
outfile.close


import argparse
from pathlib import Path
import sys

# Add aoc_common to the python path
file = Path(__file__)
root = file.parent.parent
sys.path.append(root.as_posix())

import re
from functools import lru_cache
from math import inf

parser = argparse.ArgumentParser()
parser.add_argument('--sample', '-s', help='Run with sample data', action='store_true', default=False)

parsed_args = parser.parse_args()

if parsed_args.sample:
    print("Using sample data!")

def dprint(*args, **kwargs):
    if parsed_args.sample:
        print(*args, **kwargs)

with open('input.txt' if not parsed_args.sample else 'sample.txt') as f:
    input_data = list(map(lambda x: x.replace('\n', ''), f.readlines()))

dprint(input_data)

valve_flow_rates = {}
valve_tunnels = {}

input_re = re.compile(r"Valve ([A-Z]{2}) has flow rate=(\d+); tunnels? leads? to valves? (.*)")
for line in input_data:
    matcher = input_re.match(line)
    valve, rate, tunnels = matcher.groups()
    tunnels = tunnels.split(', ')
    valve_flow_rates[valve] = int(rate)
    valve_tunnels[valve] = tunnels

dprint(valve_flow_rates)
dprint(valve_tunnels)

potential_valves = sorted([x[0] for x in valve_flow_rates.items() if x[1] != 0])

@lru_cache(maxsize=None)
def get_max_flow_rate(current_position, opened_valves, time_left):
    if time_left <= 0:
        return 0

    # If the valve can open, we want to open the valve and consider the case where we can't open the valve
    # If the valve can't open, we just want to cosnider the adjacent spaces
    if valve_flow_rates[current_position] == 0 or current_position in opened_valves:
        best = 0
        for adjacent in valve_tunnels[current_position]:
            best = max(best, get_max_flow_rate(adjacent, opened_valves, time_left - 1))
        return best
    else:
        gained_flow = (time_left - 1) * valve_flow_rates[current_position]
        best = 0
        opened = tuple(sorted(opened_valves + (current_position,)))
        for adjacent in valve_tunnels[current_position]:
            best = max(best, gained_flow + get_max_flow_rate(adjacent, opened, time_left - 2))
            best = max(best, get_max_flow_rate(adjacent, opened_valves, time_left - 1))
        return best

valve_distances = {}

def djikstra(valve):
    possible = {valve: 0}
    explored = set()
    while len(explored) < len(valve_tunnels):
        current = min(((k, v) for k, v in possible.items() if k not in explored), key=lambda x: x[1])[0]
        for other in valve_tunnels[current]:
            new_dist = possible[current] + 1
            if possible.get(other, inf) > new_dist:
                explored.discard(other)
                possible[other] = new_dist
            explored.add(current)
    return possible

valve_distances = {k: djikstra(k) for k in valve_tunnels.keys() }

max_flow_seen = 0
@lru_cache(maxsize=None)
def run_part_2(cur, other, closed_valves):
    cur_time_left, cur_pos = cur
    other_time_left, other_pos = other
    totals = [0]
    for valve in closed_valves:
        time_to_valve = valve_distances[cur_pos].get(valve) + 1
        time_left = cur_time_left - time_to_valve
        if time_left <= 0:
            continue # Can't get to the valve and open it

        flow_gained = time_left * valve_flow_rates[valve]

        # Move the person that has the most time left
        if time_left > other_time_left:
            totals.append(flow_gained + run_part_2((time_left, valve), other, closed_valves - {valve}))
        else:
            totals.append(flow_gained + run_part_2(other, (time_left, valve), closed_valves - {valve}))
    max_flow = max(totals)

    global max_flow_seen
    if max_flow > max_flow_seen:
        print("New max:", max_flow)
        max_flow_seen = max_flow
    return max_flow

def part_1():
    return get_max_flow_rate('AA', (), 30)

def part_2():
    return run_part_2((26, 'AA'), (26, 'AA'), frozenset(potential_valves))

print(f"Part 1: {part_1()}")
print(f"Part 2: {part_2()}")