##############################################################################
# Eric Gan
# Term Project for 15-112
# Fall 2012 Semester
# Finished 12/4/12
# Many thanks to Professor David Kosbie and my mentor Asa Frank for guiding
#       me along the way.
##############################################################################
# NOTE: This is the AUDIO version of this game!

# This is the main file. Run this to run the game!
# File contains splash screen, instructions, and credits.

from Board import Universe
from Tkinter import *
import random, time

class Avalanche(Universe):

    def splash(self, canvas):
    #displays the splash screen when the window loads up
        width = self.width
        height = self.height
        canvas.create_image(width/2, height/12, anchor = N, image = self.logo)
        canvas.create_text(width/2, height/4,
                           text="Based on the Actual Game!",
                           font="Helvetica, 32",
                           justify="center")
        canvas.create_image(width/2, height/2, anchor = S, image = self.image)
        canvas.create_text(width/2, 4*height/7,
                           text = "By Eric Gan",
                           font = "Helvetica, 20")
        canvas.create_text(width/2, 2*height/3,
                           text="Press 'Enter' to start playing! \n"+
                                "Press 'h' for Instructions. \n"+
                                "Press 'c' for Credits\n"+
                                "Press 'q' to return here!",
                           font="Helvetica, 24",
                           justify="center")

    def instructions(self, canvas):
        width = self.width
        height = self.height
        self.timerDelay = 100
        colors = ["red", "orange", "yellow", "green", "blue", "purple"]

        header = "Instructions"

        instructions = """
        Objective: You are a little white pillow/marshmellow/
        tissue and you want to climb as high as you can without
        getting smashed or drowned!

        Use the arrow keys to move your little white rectangle
        around! But oh no! Looks like there is water creeping
        up below you! Thankfully, it just started raining
        blocks... Phew! Now you can jump on top of the blocks
        and avoid the water! Also, take advantage of the fact
        that you can wall jump too!

        The higher you climb, the higher your score!

        During the game, press 'r' at any time to restart.
        There is no pause button, because otherwise that'd be
        cheating :P!
        """
        directors = """
        Press 'q' to go back to the Welcome Page
        Press 'Enter' to start the game!
        """

        for i in xrange(len(header)):
            canvas.create_text(40+ i*width/18, height/10,
                               text = header[i],
                               font = "Helvetica, 36",
                               fill = colors[random.randint(0,5)])

        canvas.create_text(5*width/11, height/3,
                           text= instructions,
                           font="Helvetica, 18",
                           justify = "center")

        canvas.create_image(width/2, 7*height/11,
                            image = self.instructionsPicture)

        canvas.create_text(5*width/11, 4*height/5,
                           text= directors,
                           font="Helvetica, 18",
                           justify = "center")

    def creditsScreen(self, canvas):
        width = self.width
        height = self.height
        self.timerDelay = 100
        colors = ["red", "orange", "yellow", "green", "blue", "purple"]

        header = "Credits"

        kosbie = """
        I would first and foremost like to acknowledge my Carnegie
        Mellon University 15-112 professor David Kosbie for drilling
        me with all the programming fundamentals I needed to know to
        survive this class. He is an awesome and the best instructor
        I have ever had in my academic career. His ideology of "carpe
        diem" and "You won't get good until you dive right in and
        practice, practice, practice!" challenged me extensively,
        and I feel significantly more empowered in my programming
        ability than I was before taking 15-112.
        """

        asa = """
        I would also like to thank my CA mentor Asa Frank for being
        extremely supportive of my (lack of) effort from me in the
        first week and a half this term project was assigned. As
        Professor Kosbie said, "Asa is a man with nine brains," and
        I can testify that he in fact does!
        """

        rest = """
        I would finally like to thank all my friends who helped me
        debug my code, especially my roommate. All of you helped make
        this project possible (albeit still a little buggy)!
        """
        for i in xrange(len(header)):
            canvas.create_text(40+ i*width/18, height/10,
                               text = header[i],
                               font = "Helvetica, 36",
                               fill = colors[random.randint(0,5)])

        canvas.create_text(5*width/11, height/4,
                           text = kosbie,
                           font = "Helvetica, 14",
                           justify = "center")

        canvas.create_text(5*width/11, 3*height/7,
                           text = asa,
                           font = "Helvetica, 14",
                           justify = "center")

        canvas.create_text(5*width/11, 6*height/11,
                           text = rest,
                           font = "Helvetica, 14",
                           justify = "center")

        canvas.create_image(width/3, 4*height/5, image = self.kosbie)
        canvas.create_image(2*width/3, 4*height/5, image = self.asa)

    def init(self):
        self.isLaunched = False
        self.isInstructions = False
        self.isCredits = False
        self.gameOver = False
        self.logo = PhotoImage(file = "avalanche_logo.gif")
        self.image = PhotoImage(file = "avalanchePicture.gif")
        self.instructionsPicture = PhotoImage(file = "avalanche.gif")
        self.kosbie = PhotoImage(file = "kosbie.gif")
        asaImage = PhotoImage(file = "asa.gif")
        largeAsa = asaImage.zoom(3, 3)
        self.asa = largeAsa.subsample(4,4)
        self.timerDelay = 15

    def timerFired(self):
        if self.isLaunched == True:
            super(Avalanche, self).timerFired()

    def redrawAll(self, canvas):
        if self.isLaunched == False:
            if self.isInstructions == True:
                self.instructions(self.canvas)
            elif self.isCredits == True:
                self.creditsScreen(self.canvas)
            else:
                self.splash(self.canvas)
        elif self.gameOver == True:
            self.gameOverScreen
        else:
            super(Avalanche, self).redrawAll(self.canvas)

    def keyEvent(self):
        if self.isLaunched == False:
            if "Return" in self.pressedKeys:
                # launch the game!
                self.isLaunched = True
                self.initUniverse()
            elif "h" in self.pressedKeys:
                # Instructions
                self.isInstructions = True
            elif "c" in self.pressedKeys:
                # Credits page!
                self.isCredits = True
            elif "q" in self.pressedKeys:
                # Back to home page!
                self.init()
        else:
            super(Avalanche, self).keyEvent()
            if "q" in self.pressedKeys:
                # quit the game!
                self.init()
                self.splash(self.canvas)

if __name__ == '__main__':
    Avalanche().run()


import datetime
from django import template

register = template.Library()

@register.simple_tag
def edad(pariente):
    """
    > The function `edad` takes a `pariente` as an argument and returns the difference between the
    current year and the year of the `pariente`'s birth

    :param pariente: the name of the object that will be passed to the function
    :return: The age of the person.
    """
    año_nacimiento = pariente.fecha_de_nacimiento.year
    return datetime.datetime.today().year - año_nacimiento


import math

import numpy as np
from pytest import approx, raises
from redax.predicates.dd import BDD

import redax.module as mod
from redax.spaces import DynamicCover, EmbeddedGrid, FixedCover, OutOfDomainError, ContinuousCover

Interface = mod.Interface
CompositeInterface = mod.CompositeInterface

def test_dynamic_module():
    mgr = BDD()

    inputs = {'x': DynamicCover(0, 16),
              'y': DynamicCover(0, 4),
              }
    output = {'z': DynamicCover(0, 4)
             }

    h = Interface(mgr, inputs, output)

    # Rename inputs
    assert set(h.inputs) == {'x','y'}
    assert set(h.outputs) == {'z'}
    g = ('j', 'x') >> h >> ('z', 'r')
    assert set(g.inputs) == {'j','y'}
    assert set(g.outputs) == {'r'}
    assert g == h.renamed(x='j', z='r')

    precision = {'j': 4, 'y': 3, 'r': 3}
    bittotal = sum(precision.values())

    assert g.count_io_space(bittotal) == approx(1024)
    assert g.count_io(bittotal) == approx(0)
    g = g.io_refined( {'j': (2.9,10.1), 'y': (2.4,3.8), 'r': (2.1,3.1)}, nbits = precision)
    assert g.count_io(bittotal) == approx(42) # = 7 * 2 * 3

    # Adding approximately the same transitions twice does nothing due to quantization
    oldpred = g.pred
    g = g.io_refined( {'j': (3.,10.), 'y': (2.5,3.8), 'r': (2.1,3.1)}, nbits = precision)
    assert g.pred == oldpred

    assert (g).pred.support == {'j_0', 'j_1', 'j_2', 'j_3',
                                    'y_0', 'y_1', 'y_2',
                                    'r_0', 'r_1', 'r_2'}
    assert (g  >> ('r', 'z') ).pred.support == {'j_0', 'j_1', 'j_2', 'j_3',
                                                     'y_0', 'y_1', 'y_2',
                                                     'z_0', 'z_1', 'z_2'}

    assert g.nonblock() == g.input_to_abs({'j': (3.,10.), 'y': (2.5,3.8)}, nbits = precision) # No inputs block
    assert g.nonblock() == (g.ohidden(g.outputs).pred)

    # Identity test for input and output renaming
    assert ((g  >> ('r', 'z') ) >> ('z','r')) == g
    assert (('j','w') >> (('w','j') >> g) ) == g

    # Parallel composition
    assert set((g * h).outputs) == {'z','r'}
    assert set((g * h).inputs) == {'x','y','j'}

    # Series composition with disjoint I/O yields parallel composition
    assert (g >> h) == (g * h)
    assert (g >> h) == g.composed_with(h)
    assert (g >> h) == h.composed_with(g)
    assert (g * h) == h.composed_with(g)

    # Out of bounds errors
    with raises(OutOfDomainError):
        g = g.io_refined( {'j': (3.,10.), 'y': (2.5,3.8), 'r': (2.1,4.6)}, silent=False, nbits = precision)

def test_series_comp():

    mgr = BDD()

    inputs = {'x': DynamicCover(0, 4),
              'y': DynamicCover(0, 4),
              }
    output = {'z': DynamicCover(0, 4)
             }

    h = Interface(mgr, inputs, output)
    g = ('j', 'y') >> h >> ('z', 'r')
    precision = {'j': 4, 'x': 3, 'r': 3}
    g = g.io_refined( {'j': (.75,2.5), 'x': (2.5,3.8), 'r': (2.1,3.1)}, nbits = precision)

    h = ('r', 'x') >> h
    h = h.io_refined({'r': (1.3, 3.8), 'y': (1.0, 2.0), 'z': (.9, 3.1)}, nbits = {'r': 3, 'y': 3, 'z': 4})
    assert (g >> h) == g.composed_with(h)
    assert (g >> h) == h.composed_with(g)
    assert (g >> h).nonblock() != mgr.false
    assert (g >> h).count_nb(bits=10) == approx(28)  # 7 * 2 * 2

def test_mixed_module():

    from redax.module import Interface
    from redax.spaces import DynamicCover, FixedCover

    mgr = BDD()
    inputs = {'x': DynamicCover(0, 16),
              'y': FixedCover(-10, 10, 10),
              'theta': DynamicCover(-np.pi, np.pi, periodic=True),
              'v': FixedCover(0, 5, 5),
              'omega': FixedCover(-2, 2, 4)
              }
    outputs = {'xnext': DynamicCover(0, 4),
             'ynext': FixedCover(-10,10,10),
             'thetanext': DynamicCover(-np.pi, np.pi, periodic=True)
             }

    dubins = Interface(mgr, inputs, outputs)

    # Underspecified input-output
    with raises(AssertionError):
        dubins.io_refined( {'v': (3.6,3.7), 'theta': (6,-6), 'y': (2,3), 'ynext': (2.1,3.1)},
                                nbits = {'theta': 3})

    # Test that fixed covers yield correct space cardinality
    assert mgr.count(dubins.inspace(), 4+4+4+3+2) == 16 * 10 * 16 * 5 * 4
    assert mgr.count(dubins.outspace(), 2 + 4 + 4) == 4 * 10 * 16

def test_embeddedgrid_module():

    mgr = BDD()
    inputs = {'x': EmbeddedGrid(4, 0, 3)}
    outputs = {'y': EmbeddedGrid(8, 4, 11)}

    m = Interface(mgr, inputs, outputs)

    mgr.declare("x_0", "x_1", "y_0", "y_1", "y_2")
    x0 = mgr.var("x_0")
    x1 = mgr.var("x_1")
    y0 = mgr.var("y_0")
    y1 = mgr.var("y_1")
    y2 = mgr.var("y_2")
    assert m.io_refined({'x': 2, 'y':4}).pred == (x0 & ~x1) &  (~(x0 & ~x1) | (~y0 & ~y1 & ~y2))

    assert len(mgr.vars) > 0

def test_module_composition():
    mgr = BDD()

    x = DynamicCover(0,10)

    m1 = Interface(mgr, {'a': x}, {'b': x, 'c': x})
    m2 = Interface(mgr, {'i': x, 'j': x}, {'k': x})

    m12 = (m1 >> m2.renamed(i = 'c'))
    assert set(m12.inputs) == set(['a', 'j'])
    assert set(m12.outputs) == set(['c', 'b', 'k'])
    assert m12 == m2.renamed(i = 'c').composed_with(m1)
    assert m12 == (('c', 'i') >> m2 ).composed_with(m1)

    # Renaming is left associative
    assert m12 == m1 >> (('c', 'i') >> m2 )
    assert m12 != (m1 >> ('c', 'i')) >> m2
    assert m12 == ((m1.renamed(c = 'i') >> m2)).renamed(i = 'c')
    assert m12 == ((m1 >> ('c', 'i') >> m2)).renamed(i = 'c')
    assert m12 == m1.renamed(c = 'i').composed_with(m2).renamed(i = 'c')
    assert m12 == m1.renamed(c = 'i').composed_with(m2) >> ('i', 'c')

def test_refinement_and_coarsening():

    mgr = BDD()

    def conc(x):
        return -3*x

    x = DynamicCover(-10, 10)
    y = DynamicCover(20, 20)

    linmod = Interface(mgr, {'x': x}, {'y':y})

    width = 15

    for _ in range(50):
        # Generate random input windows
        f_width = {'x': np.random.rand()*width}
        f_left  = {'x': -10 +  np.random.rand() * (20 - f_width['x'])}
        f_right = {k: f_width[k] + f_left[k] for k in f_width}
        iobox   = {k: (f_left[k], f_right[k]) for k in f_width}

        # Generate output overapproximation
        ur = conc(**f_left)
        ll = conc(**f_right)
        iobox['y'] = (ll, ur)

        # Refine and check abstract relation
        newmod = linmod.io_refined(iobox, nbits = {'x': 8, 'y':8})
        assert linmod <= newmod
        linmod = newmod

        # Check abstract relation relative to coarsened module
        assert linmod.coarsened(x=5,y=5) <= linmod
        assert linmod.coarsened(x=5) <= linmod
        assert linmod.coarsened(y=5) <= linmod
         # Coarsen should do nothing because it keeps many bits
        assert linmod.coarsened({'x':10},y=10) == linmod

def test_sin_sqrt_comp():

    def containszero(left, right):
        """Determine if 0 is contained in a periodic interval [left,right]. Possible that right < left."""
        # Map to interval [-pi,pi]
        left = ((left + np.pi) % (np.pi*2) ) - np.pi
        left = left - 2*np.pi if left > np.pi else left
        right = ((right + np.pi) % (np.pi*2) ) - np.pi
        right = right - 2*np.pi if right > np.pi else right

        if right < left:
            if left <= 0:
                return True
        else:
            if left <= 0 and right >= 0:
                return True

        return False

    def maxmincos(left, right):
        """Compute the maximum and minimum values of cos in an interval."""
        if containszero(left, right) is True:
            maxval = 1
        else:
            maxval = max([np.cos(left), np.cos(right)])

        if containszero(left + np.pi, right + np.pi) is True:
            minval = -1
        else:
            minval = min([np.cos(left), np.cos(right)])

        return (minval, maxval)

    def maxminsin(left, right):
        """Compute the maximum and minimum values of sin in an interval."""
        return maxmincos(left - np.pi/2, right - np.pi/2)

    mgr = BDD()

    sinout = DynamicCover(-1.2,1.2)
    sinin = DynamicCover(-2*np.pi, 2*np.pi, periodic=True)

    # Sin module
    sinmod = Interface(mgr, {'sin': sinin}, {'sout': sinout})

    # Sqrt module
    sqrtout = DynamicCover(0, 1.2)
    sqrtmod = Interface(mgr, {'sout': sinout}, {'sqrt': sqrtout})

    comp = CompositeInterface([sinmod, sqrtmod])

    def random_input_gen(module: Interface, scale: float) -> dict:
        iobox = dict()
        for invar, space in module.inputs.items():
            if isinstance(space, ContinuousCover):
                width = scale * space.width()
                if space.periodic:
                    left = space.lb + np.random.rand() * space.width()
                else:
                    left = space.lb + np.random.rand() * (space.width() - width)
                right = left + width
                iobox.update({invar: (left, right)})
        return iobox

    precision = {'sin': 9, 'sout': 9, 'sqrt': 9}

    # Learn sin module
    from redax.visualizer import scatter2D
    for i in range(200):
        iobox = random_input_gen(sinmod, scale = .01)
        out = maxminsin(iobox['sin'][0], iobox['sin'][1])
        iobox.update({'sout': (out[0], out[1])})

        # No errors should be raised
        sinmod = sinmod.io_refined(iobox, silent=False, nbits=precision)
        comp = comp.io_refined(iobox, nbits=precision)
        assert sinmod == comp.children[0]

    # Learn sqrt module
    for i in range(200):
        iobox = random_input_gen(sqrtmod, scale = .1)
        if iobox['sout'][0] < 0 or iobox['sout'][1] < 0:
            continue
        out = (math.sqrt(iobox['sout'][0]), math.sqrt(iobox['sout'][1]))
        iobox.update({'sqrt': out})
        sqrtmod = sqrtmod.io_refined(iobox, silent=True, nbits=precision)
        comp = comp.io_refined(iobox, nbits=precision)
        assert sqrtmod == comp.children[1]

    # sinroot = (sinmod >> sqrtmod).ohidden(['sout'])
    sinroot = (comp.children[0] >> comp.children[1]).ohidden(['sout'])
    assert set(sinroot.vars) == {'sin', 'sqrt'}
    assert sinroot.pred != mgr.false
    sinroot.check()

    # scatter2D(mgr, ('sin', sinin), ('sout', sqrtout), comp.children[0].coarsened(sout = 6, sin = 6) .pred)
    # scatter2D(mgr, ('sout', sqrtout), ('sqrt', sqrtout), comp.children[1].pred)
    # scatter2D(mgr, ('sin', sinin), ('sqrt', sqrtout), sinroot.pred)

def test_composite_module_topology():

    mgr = BDD()

    x = DynamicCover(0,10)

    m1 = Interface(mgr, {'a': x}, {'b': x, 'i': x})
    m2 = Interface(mgr, {'i': x, 'j': x}, {'k': x})

    m12 = CompositeInterface((m1, m2))
    assert m12.sorted_mods() == ((m1,), (m2,))
    assert set(m12.outputs) == {'k', 'b', 'i'}
    assert set(m12.inputs) == {'a', 'j'}
    assert set(m12.latent) == {'i'}

    m3 = Interface(mgr, {'k': x, 'b': x}, {})

    m123 = CompositeInterface([m1,m2,m3])
    assert m123.sorted_mods() == ((m1,), (m2,), (m3,))
    assert set(m123.outputs) == {'b','i','k'}
    assert set(m123.inputs) == {'a', 'j'}
    assert set(m123.vars) == {'a','j','b','i','k'}

    # Renaming
    m123renamed = m123.renamed(b='r', a='q')
    assert set(m123renamed.outputs) == {'r','i','k'}
    assert set(m123renamed.inputs) == {'q', 'j'}

def test_sinks():

    mgr = BDD()
    x =  DynamicCover(0, 10)

    x1 = x.conc2pred(mgr,'x',[1,4], 5, innerapprox=True)
    x2 = x.conc2pred(mgr,'x',[5,8], 5, innerapprox=True)
    x3 = x.conc2pred(mgr,'x',[0.0001, 5.001], 5, innerapprox=False)
    x4 = x.conc2pred(mgr,'x',[4.9999, 9.999], 5, innerapprox=False)

    m1 = Interface(mgr, {'x': x}, {}, assum=x1)
    m2 = Interface(mgr, {'x': x}, {}, assum=x2)
    m3 = Interface(mgr, {'x': x}, {}, assum=x3)
    m4 = Interface(mgr, {'x': x}, {}, assum=x4)

    assert (m1 * m2).assum == mgr.false
    assert (m1 + m2).assum != mgr.false
    assert (m3 + m4).assum == mgr.true

    assert (m3 + m1) == m3
    assert (m3 * m1)== m1

    assert (m4 * m1) <= m4


# Άσκηση 1 Μέθοδος Newton-Raphson

from math import e as e

def f(x):
    return 14 * x * e ** (x - 2) - 12 * e ** (x - 2) - 7 * x ** 3 + 20 * x ** 2 - 26 * x + 12

def f_prime(x):
    return 14 * x * e ** (x - 2) + 2 * e ** (x - 2) - 21 * x ** 2 + 40 * x - 26

def f_2nd_prime(x):
    return 14 * x * e ** (x - 2) + 16 * e ** (x - 2) - 42 * x + 40

eps = 10 ** (-5)

print("Δώσε το x0:")
x0 = float(input())
x = [x0]
if f(x0)*f_2nd_prime(x0) > 0:
    print("Ισχύει το f(x0) * f''(x0) > 0!")
else:
    exit("Δεν ισχύει το f(x0) * f''(x0) > 0!")
if f(x0) == 0:
    exit("Έδωσες την ρίζα για x0!")

h = f(x[0]) / f_prime(x[0])
n = 0

while abs(h) >= eps:
    h = f(x[n]) / f_prime(x[n])
    x.append(x[n] - h)
    n = n + 1
    print("n: " + str(n) + ", x[" + str(n) + "]: " + str("{:.5f}".format(x[n])) + ", f(x): " + str("{:.5f}".format(f(x[n]))))
    if f(x[n]) == 0:
        break

print("-------------------")
print("c: " + str("{:.5f}".format(x[n])))
print("f(c): " + str("{:.5f}".format(f(x[n]))))

if round(f(x[n]), 5) == 0 and round(f_prime(x[n]), 5) != 0:
    print('Συγκλίνει τετραγωνικά!')
else:
    print('Δεν συγκλίνει τετραγωνικά!')


import tkinter as tk
from tkinter import filedialog
from tkinter.ttk import Combobox
from enum import Enum
from downloader import YoutubeDownloader

def create_widgets():
    tk.Label(root, text="Link: ", font="Arial 14").grid(row=0)
    linkEntry = tk.Entry(root, width=40, textvariable=video_link)
    linkEntry.grid(row=0, column=1, columnspan=2, pady=10, padx=10)

    tk.Label(root, text="Destination: ", font="Arial 14").grid(row=1)
    destinationEntry = tk.Entry(root, width=40, textvariable=download_path)
    destinationEntry.grid(row=1, column=1, columnspan=2)

    Combobox(root, textvariable=combobox_var, values=list(format_enum.__members__), state="readonly").grid(row=2, columnspan=2)

    tk.Button(root, text="Browse", command=browse).grid(row=1, column=3, padx=10, pady=10)
    tk.Button(root, text="Download", command= lambda: downloader.download(video_link.get(), download_path.get(), combobox_var.get()), width=20).grid(columnspan=4, padx=10, pady=10)

def browse():
    download_directory = filedialog.askdirectory(title="Save file", initialdir="C:")
    download_path.set(download_directory)

format_enum = Enum(value="FormatEnum", names="MP3 MP4")
downloader = YoutubeDownloader()

root = tk.Tk()
root.title("Youtube Downloader")
root.geometry("500x250")
root.resizable(False, False)

combobox_var = tk.StringVar(value=format_enum.MP3.name)
video_link = tk.StringVar()
playlist_list = tk.StringVar()
download_path = tk.StringVar()

create_widgets()
tk.mainloop()


import datetime
import logging
import numpy as np
import os
import pandas as pd
from googleapiclient.discovery import build
from google.cloud import bigquery
from google.cloud import bigquery_storage_v1beta1
import google.auth

YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def bq_get_client(project_id, json_key_file):
    logger = logging.getLogger()

    try:
        # noinspection PyPackageRequirements
        from bigquery import \
            get_client  # (get the latest version with: pip install git+https://github.com/tylertreat/BigQuery-Python.git#egg=bigquery-python)
        client = get_client(project_id=project_id, json_key_file=json_key_file,
                            readonly=False, num_retries=2)
    except ImportError as e:
        logger.warning("error importing bigquery: {}".format(e))
        return None

    return client

def bq_get_clients(project_id, json_key_file):
    credentials, your_project_id = google.auth.default(
        scopes=["https://www.googleapis.com/auth/cloud-platform"]
    )

    # Make clients.
    bqclient = bigquery.Client(
        credentials=credentials,
        project=your_project_id,
    )
    bqstorageclient = bigquery_storage_v1beta1.BigQueryStorageClient(
        credentials=credentials
    )
    return bqclient, bqstorageclient

def yt_get_client(developer_key):
    client = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=developer_key)
    return client

def check_create_table(bq_client, schema, bq_dataset, bq_table, partition_by_day=True, create_if_not_exists=True, expiry_days=14):
    _exists = bq_client.check_table(bq_dataset, bq_table)

    expiry_time_ms = int((datetime.datetime.utcnow() + datetime.timedelta(days=expiry_days)).timestamp()) * 1000
    if not _exists and create_if_not_exists:
        # Create table
        created = bq_client.create_table(bq_dataset, bq_table, schema,
                                         time_partitioning=partition_by_day,
                                         expiration_time=expiry_time_ms)
        if not created:
            return False
    return True

def upload_rows(schema, rows, bq_client, bq_dataset, bq_table,
                backup_file_name=None, len_chunks=500):
    """ Upload results to Google Bigquery """

    logger = logging.getLogger()

    inserted = False
    bq_rows = rows

    # Make sure objects are serializable. So far, special handling for Numpy types and dates:
    bq_rows = scrub_serializable(bq_rows)

    table = None
    try:
        table = bq_client.get_table(f"{bq_dataset}.{bq_table}")
    except Exception as e:
        logger.error(
            msg=f"Unable to save rows. Table {bq_dataset}.{bq_table} does not exist or there was some other "
                         f"problem getting the table.", subject="Error inserting rows to Google Bigquery!")

    # google recommends chunks of ~500 rows
    for index, chunk in enumerate(chunks(bq_rows, len_chunks)):
        str_error = ""
        inserted = False
        if table:
            try:
                logger.debug(
                    "Inserting {} rows to BigQuery table {}.{}, attempt {}.".format(len(chunk), bq_dataset,
                                                                                bq_table, index))

                errors = bq_client.insert_rows(table, chunk)
                if errors == []:
                    inserted = True

                    logger.info("Successfully inserted {} rows to BigQuery table {}.{}, attempt {}.".format(len(chunk), bq_dataset, bq_table, index))
                else:
                    str_error += f"Google BigQuery returned an error result: {str(errors)}\n\n"

            except Exception as e:
                str_error += "Exception pushing to BigQuery table {}.{}, attempt {}, reason: {}\n\n".format(bq_dataset, bq_table, index, str(e)[:2000])
        else:
            str_error += "Could not get table, so could not push rows.\n\n"

        if not inserted:
            if backup_file_name:
                save_file_full = '{}.{}'.format(backup_file_name, index)
                logger.error("Failed to upload rows! Saving {} rows to newline delimited JSON file ({}) for later upload.".format(len(rows), save_file_full))

                try:
                    os.makedirs(os.path.dirname(save_file_full), exist_ok=True)
                except FileNotFoundError:
                    pass  # We get here if we are saving to a file within the cwd without a full path

                try:
                    df = pd.DataFrame.from_dict(chunk)
                    df = nan_ints(df, convert_strings=True)
                    df.to_json(save_file_full, orient="records", lines=True, force_ascii=False)
                    str_error += "Saved {} rows to newline delimited JSON file ({}) for later upload.\n\n".format(len(rows), save_file_full)
                except Exception as e:
                    str_error += "Unable to save backup file {}: {}\n\n".format(save_file_full,  str(e)[:200])

            else:
                str_error += "No backup save file configured.\n\n"

            message_body = "Exception pushing to BigQuery table {}.{}, chunk {}.\n\n".format(
                bq_dataset, bq_table, index)
            message_body += str_error

            logger.error(
                msg=message_body, subject=f"Error inserting rows to Google Bigquery! Table: {bq_dataset}.{bq_table}")
            logger.debug("First three rows:")
            logger.debug(bq_rows[:3])

    return inserted

def nan_ints(df,convert_strings=False,subset = None):
    # Convert int, float, and object columns to int64 if possible (requires pandas >0.24 for nullable int format)
    types = ['int64','float64']
    if subset is None:
        subset = list(df)
    if convert_strings:
        types.append('object')
    for col in subset:
        try:
            if df[col].dtype in types:
                df[col] = df[col].astype(float).astype('Int64')
        except:
            pass
    return df

def scrub_serializable(d):
    try:
        if isinstance(d, list):
            d = [scrub_serializable(x) for x in d]
            return d

        if isinstance(d, dict):
            for key in list(d.keys()):
                if d[key] is None:
                    del d[key]
                elif hasattr(d[key], 'dtype'):
                    d[key] = np.asscalar(d[key])
                elif isinstance(d[key], dict):
                    d[key] = scrub_serializable(d[key])
                elif isinstance(d[key], list):
                    d[key] = [scrub_serializable(x) for x in d[key]]
                elif isinstance(d[key], datetime.datetime):
                    # ensure dates are stored as strings in ISO format for uploading
                    d[key] = d[key].isoformat()

        return d
    except Exception as e:
        print(e)
        raise

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]


import shutil
import os

class File():

    def saveDatabaseCopy(self, destination_route, file_name):

        #copy the database to an specific path
        shutil.copy('src/data/myList.db', destination_route)

        old_name = fr'{destination_route}/myList.db'

        new_name = fr'{destination_route}/{file_name}'

        os.rename(old_name, new_name)

    def deleteAllData(self, check_existence):

        if(check_existence):

            #delete the database
            os.remove('src/data/myList.db')

            return 'All the info has been successfully deleted!'

        else:

            return 'No info to delete!'

    def overwriteDatabase(self, route, check_file_existence):

        #change the name of the imported file
        def changeName():

            #get all the file names contained in the 'data' folder
            name = os.listdir('src/data')

            #get the name of the database file contained in data folder
            if name[0] == 'preferences.cfg':

                name = name[1]

            else:

                name = name[0]

            #get the user data folder absolute path
            path = os.path.abspath('src/data/')

            #get the original imported file name
            old_name = fr'{path}/{name}'

            #change original file name to 'myList.db'
            new_name = fr'{path}/myList.db'

            #rename the file
            os.rename(old_name, new_name)

        #get the absolute path of the database folder
        my_destination = os.path.abspath('src/data/')

        #copy a selected file to the app folder
        shutil.copy(route, my_destination)

        #if exists, delete the current database
        if(check_file_existence):

            os.remove("src/data/myList.db")

        changeName()

    def defaultPreference(self):

        check_file_existence = os.path.exists("src/data/preferences.cfg")

        if (check_file_existence == False):
            #save user preferences
            with open('src/data/preferences.cfg', "w") as pref:
                pref.write('adult')

            pref.close()

    def preferences(self, preference):

        #save user preferences
        with open('src/data/preferences.cfg', "w") as pref:
            pref.write(preference)

        pref.close()

    def getPreference(self):

        #get the user preference
        with open('src/data/preferences.cfg', "rt") as pref:
            string = pref.read()

        pref.close()

        return string

    def checkDatabaseExistence(self):

        #check if the file exists

        return os.path.exists("src/data/myList.db")


"""
ID: pranav.19
LANG: PYTHON2
TASK: gift1
"""
from math import floor

input = []
with open("gift1.in", "r") as f:
	input = f.readlines()

NP = int(input[0])
people = [input[i] for i in range(1, NP+1)]
money = [0 for i in range(NP)]

input = input[NP+1:len(input)]
a = 0
while a < len(input):
	currentPerson = input[a]
	numbers = input[a+1].split(" ")
	totalAmt = int(numbers[0])
	numReceivers = int(numbers[1])

	if numReceivers != 0:

		splitAmt = floor(totalAmt/numReceivers)
		for i in range(0, numReceivers):

			for personIdx in range(0, len(people)):
				if input[a+2+i] == people[personIdx]:
					money[personIdx] += splitAmt
					break

		remainder = totalAmt-(splitAmt*numReceivers)

		for personIdx in range(0, len(people)):
				if currentPerson == people[personIdx]:
					money[personIdx] -= totalAmt
					money[personIdx] += remainder
					break

	a += 2+numReceivers

for i in range(len(people)):
	temp = people[i]
	people[i] = temp[:len(temp)-1]

with open("gift1.out", "w") as f:
	for i in range(len(people)):
		f.write("%s %s" % (people[i], int(money[i]))+"\n")

USACO/Training/Section 1.2/Greedy Gift Givers/gift1.py


#Print out Elements of a list that are less than 20

a = [1,1,2,3,5,8,13,21,34,55,89,100,150,14,65,7,67,18,200]
x = []

#Ask the user for a number and return a list that contains only elements
#from the original list and that are smaller than that number given by
#the user
userInput = raw_input('Please Enter a Number: ')
userInput =int(userInput)

for element in a:
	if element < userInput:
		x.append(element)

print (x)


import pickle
import os
import sys
import time
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
from sklearn import preprocessing, metrics

import utilities
import videoPlayer as vp

class DataHandler:

    def __init__(self, basePath=None, csvPath=None, classifierPath=None, showViz=False, store=False, axisLabels=False, baseOutputPath=None):

        if store and baseOutputPath is None:
            raise ValueError("Cannot store files without output Path!")

        if classifierPath is not None:
            self.classifier = pickle.load(open(classifierPath, 'rb'))
        else:
            self.classifier = None

        self.micArray = utilities.loadMicarray()

        self.store = store
        self.showViz = showViz
        self.axisLabels = axisLabels
        self.basePath = basePath
        self.baseOutputPath = baseOutputPath
        if baseOutputPath is not None:
            utilities.makeDirectory(baseOutputPath)

        if basePath is not None:
            self.logTable = pd.read_csv(os.path.join(basePath, 'DataLog.csv'))
            if not all(elem in self.logTable.columns.to_list() for elem in ['ID', 'Environment', 'Class', 'T0']):
                raise ValueError('Provided DataLog.csv doesnt contain all required columns')

            if csvPath is not None:
                filterCsv = pd.read_csv(csvPath)
                if not 'ID' in filterCsv.columns.to_list():
                    raise ValueError('Provided filtering csv doesnt contain the required column ID')
                self.logTable = self.logTable[self.logTable['ID'].isin(filterCsv['ID'])]
                self.logTable = self.logTable.reset_index(drop=True)

        if store:
            self.videoPath = os.path.join(baseOutputPath, 'VideoOverlays')
            self.plotPath = os.path.join(baseOutputPath, 'Plots')
            utilities.makeDirectory(self.videoPath)
            utilities.makeDirectory(self.plotPath)
        else:
            self.videoPath = None
            self.plotPath = None

    def loadResultTable(self, path):
        self.logTable = pickle.load(open(path, 'rb'))

    def setLogTable(self, newTable):
        # join results table with the data table
        self.logTable = self.logTable.join(newTable)
        self.logTable['T0'] -= 10

    def padData(self):

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        Dall = Lall - Tall

        # iterate over all rows, we want to pad first three elements, features confidences and the classifcation results
        for index, rows in self.logTable.iterrows():
            Tcurr = rows['T0']
            Lcurr = rows['VideoLength']
            Dcurr = Lcurr - Tcurr

            padBefore = Tall - Tcurr
            padAfter = Dall - Dcurr

            # check if padding was already carried out and then pad the data
            if rows['ClassificationResults'].shape[0] == Lcurr:
                self.logTable.at[index, 'ClassificationResults'] = np.pad(rows['ClassificationResults'], ((padBefore, padAfter), (0,0)))
                self.logTable.at[index, 'ConfidenceResults'] = np.pad(rows['ConfidenceResults'], ((padBefore, padAfter), (0,0)))
                self.logTable.at[index, 'Features'] = np.pad(rows['Features'], ((padBefore, padAfter), (0,0)))
                self.logTable.at[index, 'VisualDetections'] = np.pad(rows['VisualDetections'], ((padBefore, padAfter)))

    def postProcessing(self):
        self.padData()
        self.plotConfidences()
        self.plotAbsolutes()
        self.plotVisualBaseline()
        self.plotMeanFeatures()
        plt.show()

    def loopOverRecordings(self):

        if self.classifier is not None and self.basePath is not None and self.baseOutputPath is not None:
            newColumns = ['Features', 'ConfidenceResults', 'ClassificationResults', 'VisualDetections', 'InferenceTime', 'VideoLength']
            newTable = pd.DataFrame(columns=newColumns)

            tic = time.perf_counter()

            for index, rows in self.logTable.iterrows():
                identifier = str(rows['ID'])
                location = rows['Environment']
                label = rows['Class']
                tZero = rows['T0']

                print('Starting Session {} with class {} at location {} and T0 of {}, number {} out of {}'.format(identifier, label, location, tZero, index+1, self.logTable.shape[0]))
                samplePath = os.path.join(self.basePath, location, label, identifier)

                session = vp.VideoPlayer(samplePath, self.classifier, self.micArray, self.showViz, self.store, self.videoPath)
                featData, confidenceResults, classificationResults, visualDetections, inferenceTime, videoLength = session.loopOverFrames()

                print('Inference ran for {:.2f} seconds for the {:.1f} second long Clip\n\n'.format(inferenceTime, videoLength/10))

                newTable = newTable.append({'Features': featData, 'ConfidenceResults': confidenceResults, 'ClassificationResults': classificationResults, 'VisualDetections': visualDetections, 'InferenceTime': inferenceTime, 'VideoLength': videoLength}, ignore_index=True)

            toc = time.perf_counter()
            print('Inference ran successfully for all recordings in {} [HH:MM:SS]'.format(time.strftime("%H:%M:%S", time.gmtime(int(round(toc-tic))))))

            # append the results of inference to the log table, do post processing and then dump the data
            self.setLogTable(newTable)
            pickle.dump((self.logTable), open(os.path.join(self.baseOutputPath, 'ResultTable.obj'), 'wb'))
            self.postProcessing()
            pickle.dump((self.logTable), open(os.path.join(self.baseOutputPath, 'ResultTable.obj'), 'wb'))

        else:
            print('Classifier, Datapath or output path is missing, cannot loop over Recordings')

    def plotConfidences(self):

        print('Plotting the mean and std of the confidences of each time step')

        # define iterables and time scale
        unSamples = self.logTable.loc[:,'Class'].unique()
        unEnvironment = self.logTable.loc[:,'Environment'].unique()
        unClasses = ['front', 'left', 'none', 'right']
        c = ['blue', 'green', 'black', 'red']

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)

        # prepare the data
        dataList = []
        for i in range(len(unSamples)):
            locationList = []
            for j in range(len(unEnvironment)):
                tableSubset = self.logTable[self.logTable.Environment == unEnvironment[j]]
                stackedDataList = []
                for k in range(len(unClasses)):

                    # stack the data over all samples of one environment
                    stackedData = []
                    for index, rows in tableSubset[tableSubset.Class == unSamples[i]].iterrows():
                        stackedData.append(rows['ConfidenceResults'])
                    stackedData = np.stack(stackedData, axis=2)

                    # get the mean over all present samples, but do not count values in that were empty (0)
                    nonZeroOverClasses = np.count_nonzero(np.sum(stackedData, axis=1), axis=1)
                    trueMean = np.divide(np.sum(stackedData[:,k], axis=1), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData[:,k], axis=1)), where=nonZeroOverClasses!=0)
                    stackedDataList.append([trueMean, np.std(stackedData[:,k], axis=1)])
                locationList.append(stackedDataList)

            # repeat for all environment types
            stackedDataList = []
            for k in range(len(unClasses)):
                stackedData = []
                for index, rows in self.logTable[self.logTable.Class == unSamples[i]].iterrows():
                    stackedData.append(rows['ConfidenceResults'])
                stackedData = np.stack(stackedData, axis=2)

                nonZeroOverClasses = np.count_nonzero(np.sum(stackedData, axis=1), axis=1)
                trueMean=np.divide(np.sum(stackedData[:,k], axis=1), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData[:,k], axis=1)), where=nonZeroOverClasses!=0)
                stackedDataList.append([trueMean, np.std(stackedData[:,k], axis=1)])
            locationList.append(stackedDataList)
            dataList.append(locationList)
        unEnvironment = np.append(unEnvironment, 'overall')

        # store plots separately if true
        if self.store:
            confidencePath = os.path.join(self.plotPath, 'ConfidencePlots')
            utilities.makeDirectory(confidencePath)
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    fig, axes = plt.subplots()
                    for k in range(len(unClasses)):
                        axes.plot(time, dataList[i][j][k][0], color=c[k], label=unClasses[k])
                        axes.fill_between(time, dataList[i][j][k][0] - dataList[i][j][k][1], dataList[i][j][k][0] + dataList[i][j][k][1], color=c[k], alpha=0.15)
                        axes.grid('on')
                        axes.set_xlim([np.min(time), np.max(time)])
                        axes.set_ylim([0, 1])
                        if self.axisLabels:
                            axes.set_ylabel('Confidence in [0, 1]')
                            axes.set_xlabel('time t in [s]')
                            axes.set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'none':
                            axes.legend(loc='upper center', ncol=4, prop={'size': 8.5})
                    fig.set_size_inches((4.449, 2.40157))

                    plt.savefig(os.path.join(confidencePath, 'Confidence_{}_{}.pdf'.format(unEnvironment[j], unSamples[i])), dpi=300, bbox_inches='tight')
                    plt.close(fig)

        # plot on screen if true
        if self.showViz:
            fig, axes = plt.subplots(len(unSamples), len(unEnvironment))
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    for k in range(len(unClasses)):
                        axes[i][j].plot(time, dataList[i][j][k][0], color=c[k], label=unClasses[k])
                        axes[i][j].fill_between(time, dataList[i][j][k][0] - dataList[i][j][k][1], dataList[i][j][k][0] + dataList[i][j][k][1], color=c[k], alpha=0.15)
                        axes[i][j].grid('on')
                        axes[i][j].set_xlim([np.min(time), np.max(time)])
                        axes[i][j].set_ylim([0, 1])
                        if self.axisLabels:
                            axes[i][j].set_ylabel('Confidence in [0, 1]')
                            axes[i][j].set_xlabel('time t in [s]')
                            axes[i][j].set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'none':
                            axes[i][j].legend(loc='upper center', ncol=2, prop={'size': 8.5})
            plt.tight_layout()

    def plotAbsolutes(self):

        print('Plotting the normalized absolute classification results of each time step')

        # define iterables and time scale
        unSamples = self.logTable.loc[:,'Class'].unique()
        unEnvironment = self.logTable.loc[:,'Environment'].unique()
        unClasses = ['front', 'left', 'none', 'right']
        c = ['blue', 'green', 'black', 'red']

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)

        # prepare the data
        dataList = []
        for i in range(len(unSamples)):
            locationList = []
            for j in range(len(unEnvironment)):
                tableSubset = self.logTable[self.logTable.Environment == unEnvironment[j]]
                summedDataList = []
                for k in range(len(unClasses)):

                    # stack the data over all samples of one environment
                    summedData = np.zeros((len(time), len(unClasses)))
                    for index, rows in tableSubset[tableSubset.Class == unSamples[i]].iterrows():
                        summedData += rows['ClassificationResults']

                    # get the mean over all present samples, but do not count values in that were empty (0)
                    trueMean = np.divide(summedData[:,k], np.sum(summedData, axis=1), out=np.zeros_like(summedData[:,k]), where=np.sum(summedData, axis=1)!=0)
                    summedDataList.append(trueMean)
                locationList.append(summedDataList)

            # repeat for all environment types
            summedDataList = []
            for k in range(len(unClasses)):
                summedData = np.zeros((len(time), len(unClasses)))
                for index, rows in self.logTable[self.logTable.Class == unSamples[i]].iterrows():
                    summedData += rows['ClassificationResults']

                trueMean=np.divide(summedData[:,k], np.sum(summedData, axis=1), out=np.zeros_like(summedData[:,k]), where=np.sum(summedData, axis=1)!=0)
                summedDataList.append(trueMean)
            locationList.append(summedDataList)
            dataList.append(locationList)
        unEnvironment = np.append(unEnvironment, 'overall')

        # store plots separately if true
        if self.store:
            confidencePath = os.path.join(self.plotPath, 'AbsolutePlots')
            utilities.makeDirectory(confidencePath)
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    fig, axes = plt.subplots()
                    for k in range(len(unClasses)):
                        axes.plot(time, dataList[i][j][k], color=c[k], label=unClasses[k])
                        axes.grid('on')
                        axes.set_xlim([np.min(time), np.max(time)])
                        axes.set_ylim([0, 1])
                        if self.axisLabels:
                            axes.set_ylabel('Normalized classifications in [0, 1]')
                            axes.set_xlabel('time t in [s]')
                            axes.set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'negative':
                            axes.legend(loc='upper center', ncol=4, prop={'size': 8.5})

                    plt.savefig(os.path.join(confidencePath, 'Absolute_{}_{}.pdf'.format(unEnvironment[j], unSamples[i])), dpi=300, bbox_inches='tight')
                    plt.close(fig)

        # plot on screen if true
        if self.showViz:
            fig, axes = plt.subplots(len(unSamples), len(unEnvironment))
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    for k in range(len(unClasses)):
                        axes[i][j].plot(time, dataList[i][j][k], color=c[k], label=unClasses[k])
                        axes[i][j].grid('on')
                        axes[i][j].set_xlim([np.min(time), np.max(time)])
                        axes[i][j].set_ylim([0, 1])
                        if self.axisLabels:
                            axes[i][j].set_ylabel('Normalized classifications in [0, 1]')
                            axes[i][j].set_xlabel('time t in [s]')
                            axes[i][j].set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        if j == len(unEnvironment)-1 and unSamples[i] == 'negative':
                            axes[i][j].legend(loc='upper center', ncol=2, prop={'size': 8.5})
            plt.tight_layout()

    def plotVisualBaseline(self):

        print('Plotting the auditory classification results against the visual baseline')

        le = preprocessing.LabelBinarizer()
        le.fit([0,1,2,3])

        # prepare gt labels
        unClasses = ['front', 'left', 'negative', 'right']

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)
        offset = 15

        gtLeft = np.zeros((Lall, len(unClasses)), dtype=int)
        gtLeft[:Tall+offset,1] = 1
        gtLeft[Tall+1:,0] = 1

        gtRight = np.zeros((Lall, len(unClasses)), dtype=int)
        gtRight[:Tall+offset,3] = 1
        gtRight[Tall+1:,0] = 1

        gtNone = np.zeros((Lall, len(unClasses)), dtype=int)
        gtNone[:,2] = 1

        gtVisualNeg = np.zeros(Lall, dtype=int)
        gtVisualPos = np.ones(Lall, dtype=int)

        # stack labels and results
        stackedLabels = []
        stackedResults = []
        stackedVisualLabel = []
        stackedVisualResult = []

        for index, rows in self.logTable.iterrows():
            if rows['Class'] == 'left':
                stackedLabels.append(gtLeft)
                stackedVisualLabel.append(gtVisualPos)
            elif rows['Class'] == 'right':
                stackedLabels.append(gtRight)
                stackedVisualLabel.append(gtVisualPos)
            else:
                stackedLabels.append(gtNone)
                stackedVisualLabel.append(gtVisualNeg)

            stackedResults.append(rows['ClassificationResults'])
            stackedVisualResult.append(rows['VisualDetections'])

        stackedLabels = np.stack(stackedLabels)
        stackedResults = np.stack(stackedResults)
        stackedVisualLabel = np.stack(stackedVisualLabel)
        stackedVisualResult = np.stack(stackedVisualResult)

        # print(stackedVisualResult)
        #
        visualAccuracy = []
        auditoryAccuracy = []

        for i in range(stackedLabels.shape[1]):
            # Make sure only elements are considered that contributed and filter those, i.e. results do not show zero just from padding
            idx = np.where(np.sum(stackedResults[:,i,:], axis=1) != 0)[0]
            stackedLabelsStep = stackedLabels[idx,i,:]
            stackedResultsStep = stackedResults[idx,i,:]
            stackedVisualLabelStep = stackedVisualLabel[idx,i]
            stackedVisualResultStep = stackedVisualResult[idx,i]

            # get indexes from label matrix to extract from Result matrix
            idxLeft  = np.where(stackedLabelsStep[:,1] == 1)[0]
            idxRight = np.where(stackedLabelsStep[:,3] == 1)[0]
            idxNone  = np.where(stackedLabelsStep[:,2] == 1)[0]
            idxFront = np.where(stackedLabelsStep[:,0] == 1)[0]

            # first deal with visual detections
            tn, _, _, tp = metrics.confusion_matrix(stackedVisualLabelStep, stackedVisualResultStep, labels=[0,1]).ravel()

            visualAccuracy.append((tn + tp) / len(stackedVisualLabelStep))

            # in offset area manually calculate TPs
            if i > Tall and i <= Tall+offset:
                totalSamples = len(idxLeft) + len(idxRight) + len(idxNone)

                # define tp as correct whenever front or left/right is correct
                tpa = np.array([np.sum(stackedResultsStep[idxLeft,0]) + np.sum(stackedResultsStep[idxLeft,1]),
                        np.sum(stackedResultsStep[idxRight,0]) + np.sum(stackedResultsStep[idxRight,3]),
                        np.sum(stackedResultsStep[idxNone, 2])])

            else:
                totalSamples = len(idxLeft) + len(idxRight) + len(idxNone) + len(idxFront)
                mcm = metrics.multilabel_confusion_matrix(stackedLabelsStep, stackedResultsStep, labels=[0,1,2,3])
                tpa = mcm[:, 1, 1]
                idx_two = np.where(np.sum(stackedLabelsStep, axis=1) == 2)

            auditoryAccuracy.append(np.sum(tpa) / totalSamples)

        visualAccuracy = np.array(visualAccuracy)
        auditoryAccuracy = np.array(auditoryAccuracy)

        # now plot
        fig, axes = plt.subplots()
        transitionArea = np.zeros((Lall))
        transitionArea[Tall+1:Tall+offset] = 1

        axes.fill_between(time, transitionArea, 0, color='k', step='mid', alpha=0.2)

        axes.plot(time, visualAccuracy, label='Faster R-CNN (visual)', linewidth=2.5)
        axes.plot(time, auditoryAccuracy, 'peru', label='ours (acoustic)', linewidth=2.5)
        axes.grid('on')
        axes.set_xlim([np.min(time), np.max(time)])
        axes.set_ylim([0.4, 1])
        if self.axisLabels:
            axes.set_xlabel('time t in [s]')
            axes.set_ylabel('Accuracy in [0, 1]')
            axes.set_title('Visual Baseline Comparison')
        axes.legend(loc='upper left', ncol = 1, prop={'size': 10})
        if self.store:
            baselinePath = os.path.join(self.plotPath, 'VisualBaselineComparison')
            utilities.makeDirectory(baselinePath)
            fig.set_size_inches((1.8*4.449, 1.8*1.40157))
            plt.savefig(os.path.join(baselinePath, 'VisualBaselineComparison.pdf'), dpi=300, bbox_inches='tight')
        if self.showViz:
            plt.tight_layout()
        else:
            plt.close(fig)

    def plotMeanFeatures(self):

        print('Plotting the mean feature vector of each time step')

        # define iterables and time scale
        unSamples = self.logTable.loc[:,'Class'].unique()
        unEnvironment = self.logTable.loc[:,'Environment'].unique()

        Tall = self.logTable['T0'].max()
        Lall = self.logTable['VideoLength'].max()
        time = np.linspace(-Tall  / 10, (Lall - Tall) / 10 - 0.1, Lall)
        offset = 15

        maxValue = 0
        minValue = 100

        # prepare the data
        dataList = []
        for i in range(len(unSamples)):
            locationList = []
            for j in range(len(unEnvironment)):
                tableSubset = self.logTable[self.logTable.Environment == unEnvironment[j]]

                # stack the data over all samples of one environment
                stackedData = []
                for index, rows in tableSubset[tableSubset.Class == unSamples[i]].iterrows():
                    stackedData.append(rows['Features'])
                stackedData = np.stack(stackedData, axis=2)

                # get the mean over all present samples, but do not count values in that were empty (0)
                nonZeroOverClasses = np.count_nonzero(stackedData, axis=2)
                trueMean = np.divide(np.sum(stackedData, axis=2), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData, axis=2)), where=nonZeroOverClasses!=0)
                if trueMean.max() > maxValue:
                    maxValue = trueMean.max()
                if trueMean.min() < minValue:
                    minValue = trueMean.min()
                locationList.append([trueMean, np.std(stackedData)])

            # repeat for all environment types
            stackedData = []
            for index, rows in self.logTable[self.logTable.Class == unSamples[i]].iterrows():
                stackedData.append(rows['Features'])
            stackedData = np.stack(stackedData, axis=2)

            nonZeroOverClasses = np.count_nonzero(stackedData, axis=2)
            trueMean=np.divide(np.sum(stackedData, axis=2), nonZeroOverClasses, out=np.zeros_like(np.sum(stackedData, axis=2)), where=nonZeroOverClasses!=0)
            if trueMean.max() > maxValue:
                maxValue = trueMean.max()
            if trueMean.min() < minValue:
                minValue = trueMean.min()

            locationList.append([trueMean, np.std(stackedData)])
            dataList.append(locationList)
        unEnvironment = np.append(unEnvironment, 'overall')

        # store plots separately if true
        if self.store:
            confidencePath = os.path.join(self.plotPath, 'FeaturePlots')
            utilities.makeDirectory(confidencePath)
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):
                    fig, axes = plt.subplots()
                    axes.imshow(np.transpose(dataList[i][j][0])[0:29,:], cmap='gray', vmin=minValue, vmax=maxValue)
                    axes.vlines([Tall, Tall+offset], 0, 29, colors=['blue', 'red'], linestyles='dashed')

                    axes.set_xticks(np.arange(len(time)))
                    axes.set_xticklabels(time)
                    axes.set_yticks(np.arange(30))
                    axes.set_yticklabels(np.linspace(-90, 90, 30).astype(int))

                    everyNth = 10
                    for n, label in enumerate(axes.xaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    everyNth = 3
                    for n, label in enumerate(axes.yaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    axes.set_ylim([0, 29])

                    if self.axisLabels:
                        axes.set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        axes.set_xlabel('Angle in [deg]')
                        axes.set_ylabel('time t in [s]')

                    fig.set_size_inches((4.449, 2.40157))

                    plt.savefig(os.path.join(confidencePath, 'Feature_{}_{}.pdf'.format(unEnvironment[j], unSamples[i])), dpi=300, bbox_inches='tight')
                    plt.close(fig)

        # plot on screen if true
        if self.showViz:
            fig, axes = plt.subplots(len(unSamples), len(unEnvironment))
            for i in range(len(unSamples)):
                for j in range(len(unEnvironment)):

                    axes[i][j].imshow(np.transpose(dataList[i][j][0])[0:29,:], cmap='gray', vmin=minValue, vmax=maxValue)
                    axes[i][j].vlines([Tall, Tall+offset], 0, 29, colors=['blue', 'red'], linestyles='dashed')

                    axes[i][j].set_xticks(np.arange(len(time)))
                    axes[i][j].set_xticklabels(time)
                    axes[i][j].set_yticks(np.arange(30))
                    axes[i][j].set_yticklabels(np.linspace(-90, 90, 30).astype(int))

                    everyNth = 10
                    for n, label in enumerate(axes[i][j].xaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    everyNth = 3
                    for n, label in enumerate(axes[i][j].yaxis.get_ticklabels()):
                        if n % everyNth != 0:
                            label.set_visible(False)

                    axes[i][j].set_ylim([0, 29])

                    if self.axisLabels:
                        axes[i][j].set_title(unSamples[i] + ' at ' + unEnvironment[j])
                        axes[i][j].set_xlabel('Angle in [deg]')
                        axes[i][j].set_ylabel('time t in [s]')

            plt.tight_layout()


import os
import csv
from collections import defaultdict
import itertools
import ast
import numpy as np
import pandas as pd
import pickle
import sys

def create_final_mov_dict(csv_file, ratings_threshold, usernum_threshold):
    # using usecols in read_csv to exclude the last column of timestamp
    df = pd.read_csv(csv_file, delimiter=',', usecols=[0,1,2])
    df = df[df['rating'] >= ratings_threshold]  # rating thresholding

    movie_user_dict = defaultdict(list)
    for index, row in df.iterrows():
        movId = int(row['movieId'])
        usrId = int(row['userId'])
        # rating = row['rating']
        movie_user_dict[movId].append(usrId)

    # store the final res here, after the usernum thresholding
    final_dict = {}
    # num_users = 0
    for key,val in movie_user_dict.items():
        if len(val) >= usernum_threshold:
            final_dict[key] = val

    return final_dict

def get_total_users(final_dict):
    total_users = set()
    for mov in final_dict.keys():
        row = set(final_dict[mov])
        total_users = total_users.union(row)
    num_users = len(total_users)
    return num_users

def create_count_matrix(final_dict):
    # helper function
    def findnum_common_elements(list1, list2):
        return len(set(list1).intersection(list2))

    count_matrix = {}
    for pair in itertools.combinations(final_dict.keys(), r=2):
        mov1, mov2 = pair
        count_matrix[pair] = findnum_common_elements(final_dict[mov1], final_dict[mov2])

    # adding the marginal counts
    for k in final_dict.keys():
        pair = (k,k)
        count_matrix[pair] = len(final_dict[k])

    return count_matrix

def get_genres(genres_json, id_to_genre):
    '''
    id_to_genre: its like a global dict to store the mappings
                from the genre-id to genre-name
    '''
    if not genres_json:
        return None

    tmp = ast.literal_eval(genres_json)
    ids_list = []
    gen_list = []

    for d in tmp:
        id_num = d['id']
        genre_name = d['name']
        genre_name = genre_name.replace(" ", "-") # change all whitespaces to hyphen
        ids_list.append(id_num)
        gen_list.append(genre_name)

        if id_num not in id_to_genre:
            id_to_genre[id_num] = genre_name

    return ids_list, gen_list, id_to_genre

def create_final_genres_dict(csv_file, movIdsList):
    df_meta = pd.read_csv(csv_file, delimiter=',')
    df_meta = df_meta[['genres', 'id']]

    genid_dict = {}
    genname_dict = {}
    genid_counts = defaultdict(int) # counting the genre-genre pairs
    id_to_genre = {}

    for index, row in df_meta.iterrows():

        try:
            movId = int(row['id'])
            gen_json = row['genres']

            if movId in movIdsList:  # Only consider the thresholded movies
                ids, genres, id_to_genre = get_genres(gen_json, id_to_genre)
                genid_dict[movId] = ids
                genname_dict[movId] = genres

                for i in ids:
                    singleton_pair = (i, i)
                    genid_counts[singleton_pair] += 1

                for pair in itertools.combinations(ids, r=2):
                    id1, id2 = pair
                    genid_counts[pair] += 1

        except Exception as e:
            print(index, row['id'])
            repr(e)

    return genid_dict, genname_dict, genid_counts, id_to_genre

def marginal_prob(obj_id, count_matrix, num_users):
    '''function to get the marginal prob:
        P(movie_id1)
    '''
    margn_count = count_matrix[(obj_id, obj_id)]
    return margn_count * 1.0/num_users

def joint_prob(obj_id1, obj_id2, count_matrix, num_users):
    '''function to get the joint prob:
        P(movie_id1, movie_id2)
    '''
    if (obj_id1, obj_id2) not in count_matrix.keys():
        key = (obj_id2, obj_id1)
    else:
        key = (obj_id1, obj_id2)

    joint_count = count_matrix[key]
    return joint_count * 1.0/num_users

def conditional_prob(obj_id1, obj_id2, count_matrix):
    '''function to get the conditional prob:
        P(movie_id1 | movie_id2)
    '''
    if (obj_id1, obj_id2) not in count_matrix.keys():
        key = (obj_id2, obj_id1)
    else:
        key = (obj_id1, obj_id2)

    joint_count = count_matrix[key]
    margn_count = count_matrix[obj_id2, obj_id2]

    return joint_count * 1.0/margn_count

def create_vocab_marginal_files(movie_count_matrix, movie_num_users, genre_count_matrix, genre_num_users, datadir):

    # Create the marginals file and a separate vocab file (for all the possible movieIds)
    # Consider only the diagonal entries in count_matrix
    # Also, the 2 lists work as follows:
    # marginals[i] == marginal_prob(movieid_vocab[i], count_matrix, num_users)
    # i.e the indices of both lists match on movieid and its marginal probability

    mov_marginals = []
    movieid_vocab = []
    for key_pair in movie_count_matrix.keys():
        k1, k2 = key_pair
        if k1 == k2:
            movieid_vocab.append(k1)
            mov_marginals.append(marginal_prob(k1, movie_count_matrix, movie_num_users))

    # Write out the lists to text files

    fname_mov_marginals = datadir + "movie_marginal_prob.txt"
    with open(fname_mov_marginals, "w") as f:
        for prob in mov_marginals:
            f.write("%s\n" % prob)

    fname_mov_vocab = datadir + "movie_vocabulary.txt"
    with open(fname_mov_vocab, "w") as f:
        for movid in movieid_vocab:
            f.write("%s\n" % movid)

    gen_marginals = []
    gen_vocab = []
    for key_pair in genre_count_matrix.keys():
        k1, k2 = key_pair
        if k1 == k2:
            gen_vocab.append(k1)
            gen_marginals.append(marginal_prob(k1, genre_count_matrix, genre_num_users))

    # Write out the lists to text files
    fname_gen_marginals = datadir + "genre_marginal_prob.txt"
    with open(fname_gen_marginals, "w") as f:
        for prob in gen_marginals:
            f.write("%s\n" % prob)

    fname_gen_vocab = datadir + "genre_vocabulary.txt"
    with open(fname_gen_vocab, "w") as f:
        for gen in gen_vocab:
            f.write("%s\n" % gen)
    return

def create_master_files(movie_count_matrix, genre_count_matrix, genname_dict, datadir):
    REL = "IsA"

    # MOVIES
    print("writing movie master files")
    mov_tup_list = []
    for key_pair in movie_count_matrix.keys():
        k1, k2 = key_pair
        if k1 != k2:
            prob_k2k1 = conditional_prob(k2, k1, movie_count_matrix)
            tmp_tup1 = (REL, k1, k2, prob_k2k1)
            mov_tup_list.append(tmp_tup1)

            prob_k1k2 = conditional_prob(k1, k2, movie_count_matrix)
            tmp_tup2 = (REL, k2, k1, prob_k1k2)
            mov_tup_list.append(tmp_tup2)

    fname_mov_master = datadir + "movie_movie_master.txt"
    with open(fname_mov_master, "w") as f:
        writer = csv.writer(f, delimiter='\t', lineterminator='\n')
        writer.writerows(mov_tup_list)

    # GENRES
    print("writing genre master files")
    gen_tup_list = []
    for key_pair in genre_count_matrix.keys():
        k1, k2 = key_pair
        if k1 != k2:
            prob_k2k1 = conditional_prob(k2, k1, genre_count_matrix)
            tmp_tup1 = (REL, k1, k2, prob_k2k1)
            gen_tup_list.append(tmp_tup1)

            prob_k1k2 = conditional_prob(k1, k2, genre_count_matrix)
            tmp_tup2 = (REL, k2, k1, prob_k1k2)
            gen_tup_list.append(tmp_tup2)

    fname_master = datadir + "genre_genre_master.txt"
    with open(fname_master, "w") as f:
        writer = csv.writer(f, delimiter='\t', lineterminator='\n')
        writer.writerows(gen_tup_list)

    # MOVIE - GENRES
    print("writing movie-genre master files")
    tup_list = []
    for movid in genname_dict:
        genres = genname_dict[movid]
        for g in genres:
            tmp_tup = (REL, movid, g, 1.0)
            tup_list.append(tmp_tup)

    fname_master = datadir + "movie_genre_master.txt"
    with open(fname_master, "w") as f:
        writer = csv.writer(f, delimiter='\t', lineterminator='\n')
        writer.writerows(tup_list)

    return

def main():
    # t_rating = 4
    # t_users = 100
    t_rating = float(sys.argv[1])
    t_users = int(sys.argv[2])

    # rootdir = '/home/nkhargonkar/dsis/'
    rootdir = '/home/ninad/Desktop/Link-to-sem4/dsis/'

    # rawdata_file = rootdir + 'datasets/the-movies-dataset/ratings_small.csv'  # SMALL RATINGS FILE
    rawdata_file = rootdir + 'datasets/the-movies-dataset/ratings.csv'    # FULL RATINGS FILE

    metdata_file = rootdir + 'datasets/the-movies-dataset/movies_metadata.csv'  # for the genres
    datadir = rootdir + 'prob-emb/box-code/data/movie_data/movie_data_' + str(t_rating) + '_' + str(t_users) + '_taxonomy/'

    print(datadir)
    print(t_rating, t_users)

    final_dict = create_final_mov_dict(rawdata_file, t_rating, t_users)
    mov_num_users = get_total_users(final_dict)
    mov_cmatrix = create_count_matrix(final_dict)
    movId_list = list(final_dict.keys())
    print("Done with MOVIE")
    del(final_dict)

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

    genid_dict, genname_dict, genid_counts, id_to_genre = create_final_genres_dict(metdata_file, movId_list)
    genname_counts = {}   # the count matrix for genres with their names
    for k,v in genid_counts.items():
        k1, k2 = k
        g1 = id_to_genre[k1]
        g2 = id_to_genre[k2]
        genname_counts[g1, g2] = v
    print("Done with GENRES")

    print("Writing MARG and VOCAB")
    create_vocab_marginal_files(mov_cmatrix, mov_num_users, genname_counts, len(movId_list), datadir)

    print("Writing master files")
    create_master_files(mov_cmatrix, genname_counts, genname_dict, datadir)

    # trn_file = datadir + 'movie_train.txt'
    # trn_eval_file = datadir + 'movie_train_eval.txt'
    # create_trneval_file(trn_file, trn_eval_file, 10)

if __name__ == "__main__":
    main()


""""

 Write Out Numbers in English2
: While the popularity of cheques as a payment method has
diminished in recent years, some companies still issue them to pay employees or vendors.
The amount being paid normally appears on a cheque twice, with one occurrence written
using digits, and the other occurrence written using English words. Repeating the amount in
two different forms makes it much more difficult for an unscrupulous employee or vendor to
modify the amount on the cheque before depositing it. In this exercise, your task is to create
a function that takes an integer between 0 and 999 as its only parameter, and returns a
string containing the English words for that number. For example, if the parameter to the
function is 142 then your function should return “one hundred forty two”. Use one or more
dictionaries to implement your solution rather than large if/elif/else constructs. Include a
main program that reads an integer from the user and displays its value in English words.

"""

def write_no(no_i:int):
    simple_dict={
        1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six",
        7:"seven", 8:"eight", 9:"nine", 10:"ten", 20:"twenty", 30:"thirty", 40:"forty", 50:"fifty", 60:"sixty",
        70:"seventy", 80:"eighty", 90:"ninety", 11:"eleven", 12:"twelve", 13:"thirteen", 14:"fourteen", 15:"fifteen", 16:"sixteen",
        17:"seventeen", 18:"eighteen", 19:"nineteen"
    }

    if len(str(no_i))==1 or (len(str(no_i))==2 and (no_i//10)%10==1) or (len(str(no_i))==2 and (no_i%10==0)):
        return simple_dict[no_i]
    elif len(str(no_i))==2:
        return(f"{simple_dict[((no_i//10)%10)*10]}{simple_dict[(no_i%10)]}")
    else:
        if no_i%100==0:
            return(f"{simple_dict[no_i//100]} hundred")
        elif no_i%10==0 or (no_i%100)//10==1 or (no_i%100)//10==0:
            return(f"{simple_dict[no_i//100]} hundred {simple_dict[(no_i%100)]}")
        else:
            return(f"{simple_dict[no_i//100]} hundred {simple_dict[((no_i//10)%10)*10]}{simple_dict[(no_i%10)]}")

def main():
   no=int(input("Your amount to be spelled please: "))

   print(write_no(no))

if __name__=="__main__":
    main()


"""
Created on Tue Oct 27 11:30:22 2020
https://leetcode.com/problems/running-sum-of-1d-array/
Difficulty: EASY
10/27/2020 11:35
"""
class Solution(object):
    def runningSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        # uses list comprehension and enumerate function
        return [v + sum(nums[:i]) for i,v in enumerate(nums)]

z = Solution()
nums = [3,1,2,10,1]
print(z.runningSum(nums))


import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from matplotlib import font_manager, rc
from sklearn.preprocessing import OrdinalEncoder
from imblearn.under_sampling import RandomUnderSampler
from sklearn.model_selection import train_test_split

font_path = "C:/Windows/Fonts/batang.ttc"
font = font_manager.FontProperties(fname=font_path).get_name()
rc('font', family=font)

STROKE_MENUS = ["종료", #0
                "데이터구하기",#1
                "한글화",#2
                "타깃변수설정",#3
                "ID변수설정",#4
                "구간확률변수설정",#5
                "범주형변수설정",#6
                "결측값이 50% 초과인 변수 제거",#7
                "이상값 제거",#8
                "csv 저장",#9
                "샘플링"]

stroke_meta = {
    'id':'아이디', 'gender':'성별', 'age':'나이', 'hypertension':'고혈압', 'heart_disease':'심장병',
    'ever_married':'기혼여부', 'work_type':'직종', 'Residence_type':'거주형태',
    'avg_glucose_level':'평균혈당', 'bmi':'체질량지수', 'smoking_status':'흡연상태', 'stroke':'뇌졸중'}
stroke_menu = {
    "1" : lambda t: t.spec(),
    "2" : lambda t: t.rename_meta(),
    "3" : lambda t: t.target(),
    "4" : lambda t: t.id_variable(),
    "5" : lambda t: t.interval_variable(),
    "6" : lambda t: t.categorical_variable(),
    "7" : lambda t: t.remove_null(),
    "8": lambda t: t.remove_outlier(),
    "9": lambda t: t.additional_process(),
    "10": lambda t: t.sampling()
}
'''
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5110 entries, 0 to 5109
Data columns (total 12 columns):
 #   Column             Non-Null Count  Dtype
---  ------             --------------  -----
 0   id                 5110 non-null   int64
 1   gender             5110 non-null   object
 2   age                5110 non-null   float64
 3   hypertension       5110 non-null   int64
 4   heart_disease      5110 non-null   int64
 5   ever_married       5110 non-null   object
 6   work_type          5110 non-null   object
 7   Residence_type     5110 non-null   object
 8   avg_glucose_level  5110 non-null   float64
 9   bmi                4909 non-null   float64
 10  smoking_status     5110 non-null   object
 11  stroke             5110 non-null   int64
dtypes: float64(3), int64(4), object(5)
memory usage: 479.2+ KB
None
'''

class StrokeService:
    def __init__(self):
        self.stroke = pd.read_csv('../../pythonProject/static/data/dam/ml/healthcare-dataset-stroke-data.csv')
        self.my_stroke = None
        self.df1 = None
        self.cols = None
        self.cols1 = None
        self.df2 = None
    '''
    1.스펙보기
    '''
    def spec(self):
        print(" --- 1.Shape ---")
        print(self.stroke.shape)
        print(" --- 2.Features ---")
        print(self.stroke.columns)
        print(" --- 3.Info ---")
        print(self.stroke.info())
        print(" --- 4.Case Top1 ---")
        print(self.stroke.head(1))
        print(" --- 5.Case Bottom1 ---")
        print(self.stroke.tail(3))
        print(" --- 6.Describe ---")
        print(self.stroke.describe())
        print(" --- 7.Describe All ---")
        print(self.stroke.describe(include='all'))
    '''
    2.한글 메타데이터
    '''
    def rename_meta(self):
        self.my_stroke = self.stroke.rename(columns=stroke_meta)
        print(" --- 2.Features ---")
        print(self.my_stroke.columns)
    '''
    3.타깃변수(=종속변수 dependent, y값) 설정
        입력변수(=설명변수, 확률변수, X값)
        타깃변수명 : stroke(=뇌졸중)
        타깃변수값 : 과거에 한 번이라도 뇌졸중이 발병했으면 1, 아니면 0
    '''
    def target(self):
        self.rename_meta()
        df = self.my_stroke
        print(f'stroke type : {df["뇌졸중"].dtype}')
        print(f'stroke null : {df["뇌졸중"].isnull().sum()}')
        print(f'stroke value counts : \n{df["뇌졸중"].value_counts(dropna=False)}')

    def id_variable(self):
        self.target()
        df = self.my_stroke
        print(df['아이디'].dtypes)
        print(f'id 결측값 : {df["아이디"].isnull().sum()}')
        n = len(pd.unique(df['아이디'])) - len(df['아이디'])
        print(f'중복된 id 개수 : {n}')

    def interval_variable(self):
        self.id_variable()
        df = self.my_stroke
        self.cols = ['나이','평균혈당','체질량지수']
        print(df[self.cols].dtypes)
        pd.options.display.float_format = '{:.2f}'.format
        print(df[self.cols].describe())
        c = df['나이'] > 18
        print(f"나이 > 18 : \n{df[c].head(3)}")
        print(f'df[c]의 자료 개수 : {len(df[c])}')
        print(f'df 대비 df[c]의 자료 개수 비율 : {len(df[c])/len(df)}')
        self.df1 = df[c]
        print(f'df1 shape : {self.df1.shape}')

    def categorical_variable(self):
        self.interval_variable()
        df1 = self.df1
        self.cols1 = ['성별','고혈압','심장병','기혼여부','직종','거주형태','흡연상태']
        print(f'cols1 null 개수 : \n{df1[self.cols1].isnull().sum()}')
        print(f'cols1 type : \n{df1[self.cols1].dtypes}')

    def remove_null(self):
        self.interval_variable()
        df1 = self.df1
        df = self.my_stroke
        cols = self.cols
        print(df1.isna().any()[lambda x:x])
        print(f'rate : {df["체질량지수"].isnull().mean()}')
        print(f"외도 : \n{df1[cols].skew()}")
        print(f"첨도 : \n{df1[cols].kurtosis()}")
        print(f"work type : \n{df1['직종'].value_counts(dropna=False)}")
        print(pd.crosstab(df1['직종'],columns='count'))
        print(pd.crosstab(df1['직종'],columns='ratio',normalize=True))
        print(pd.crosstab(df1['직종'],df1['뇌졸중']))
        print(pd.crosstab(df1['직종'],df1['뇌졸중'], normalize=True))

    def remove_outlier(self):
        self.remove_null()
        df1 = self.df1
        df = self.my_stroke
        fig, axes = plt.subplots(1, 3, figsize=(15, 4))
        sns.histplot(ax=axes[0], data=df, x="나이", kde=True, bins=20)
        sns.histplot(ax=axes[1], data=df, x="평균혈당", kde=True, bins=20)
        sns.histplot(ax=axes[2], data=df, x="체질량지수", kde=True, bins=20)
        plt.show()
        sns.set_style('whitegrid')
        fig, axes = plt.subplots(1, 3, figsize=(15, 4))
        sns.boxplot(ax=axes[0], x='나이', data=df1)
        sns.boxplot(ax=axes[1], x='평균혈당', data=df1)
        sns.boxplot(ax=axes[2], x='체질량지수', data=df1)
        plt.show()
        Q1 = df1[['나이', '평균혈당', '체질량지수']].quantile(0.25)
        Q3 = df1[['나이', '평균혈당', '체질량지수']].quantile(0.75)
        IQR = Q3 - Q1
        print(f'IQR : \n{IQR}')
        print("*" * 50)
        Lower = Q1 - 3.0 * IQR
        Upper = Q3 + 3.0 * IQR
        print(f'Lower : \n{Lower}')
        print(f'Upper : \n{Upper}')
        print("*" * 50)
        c1 = df1['평균혈당'] <= 232.64
        c2 = df1['체질량지수'] <= 60.3
        self.df2 = df1[c1 & c2]
        print(f'shape : {self.df2.shape}')
        print(self.df2.info())

    def additional_process(self):
        df2 = self.df2
        df2['성별'] = OrdinalEncoder().fit_transform(df2['성별'].values.reshape(-1, 1))
        df2['기혼여부'] = OrdinalEncoder().fit_transform(df2['기혼여부'].values.reshape(-1, 1))
        df2['직종'] = OrdinalEncoder().fit_transform(df2['직종'].values.reshape(-1, 1))
        df2['거주형태'] = OrdinalEncoder().fit_transform(df2['거주형태'].values.reshape(-1, 1))
        df2['흡연여부'] = OrdinalEncoder().fit_transform(df2['흡연여부'].values.reshape(-1, 1))
        self.df2 = df2

        print(" ### 프리프로세스 종료 ### ")
        self.stroke.to_csv("../../../static/save/dam/ml/stroke.csvstroke.csv")

    def sampling(self):
        df = pd.read_csv("../../pythonProject/static/save/dam/ml/stroke.csv")
        data = df.drop(['뇌졸중'], axis=1)
        target = df['뇌졸중']
        undersample = RandomUnderSampler(sampling_strategy=0.333, random_state=2)
        data_under, target_under = undersample.fit_resample(data, target)
        print(target_under.value_counts(dropna=True))
        X_train, X_test, y_train, y_test = train_test_split(data_under, target_under,
                                                            test_size=0.5, random_state=42, stratify=target_under)
        print("X_train shape :", X_train.shape)
        print("X_test shape :", X_test.shape)
        print("y_train shape :", y_train.shape)
        print("y_test shape :", y_test.shape)
        print("y_train value ratio :\n", y_train.value_counts(normalize=True))
        print("y_train value count :\n", y_train.value_counts())

if __name__ == '__main__':
    def my_menu(ls):
        for i, j in enumerate(ls):
            print(f"{i}. {j}")
        return input('메뉴선택: ')

    if __name__ == '__main__':
        t = StrokeService()
        while True:
            menu = my_menu(STROKE_MENUS)
            if menu == '0':
                print("종료")
                break
            else:
                stroke_menu[menu](t)
                try:
                    stroke_menu[menu](t)
                except KeyError:
                    print(" ### Error ### ")


"""
https://leetcode.com/explore/learn/card/introduction-to-data-structure-binary-search-tree/143/appendix-height-balanced-bst/1015/
Convert Sorted Array to Binary Search Tree

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.

Example 1:
Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted:
Example 2:
Input: nums = [1,3]
Output: [3,1]
Explanation: [1,3] and [3,1] are both a height-balanced BSTs.

Constraints:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums is sorted in a strictly increasing order.
"""

# Definition for a binary tree node.

from typing import List

class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution(object):
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        return self.helper(nums, 0, len(nums))

    def helper(self, nums, lower, upper):
        if lower == upper:
            return None

        mid = (lower + upper) // 2
        node = TreeNode(nums[mid])
        node.left = self.helper(nums, lower, mid)
        node.right = self.helper(nums, mid+1, upper)

        return node

if __name__ == "__main__":
    nums = [-10, -3, 0, 5, 9]
    print("sortedArrayToBST({0}): {1}".format(
        nums, Solution.sortedArrayToBST(Solution, nums)))

# 01
# 31 / 31 test cases passed.
# Status: Accepted
# Runtime: 52 ms
# Memory Usage: 15.8 MB

Python/Convert Sorted Array to Binary Search Tree/main.py


import Item

class woodenChest(Item.itemTile):
    def __init__(self):
        super().__init__('Wooden Chest',9,15)
        self.recipe = [(6,8)]
        self.craftAmount = 1
        self.maxStack = 16

def setter():
    x = woodenChest()
    return x


# coding: utf-8

# In[1]:

import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
import pickle as pickle
import os
from sklearn.model_selection import train_test_split
import time
import argparse
from utils import pred_eval
from base_models import xgt_regressor

# In[ ]:

# read data
# parameters
parser = argparse.ArgumentParser()
parser.add_argument("--gather_point", "-p", help="gather point, in hours", type=int, default = 5)
args = parser.parse_args()

GATHER_POINT = args.gather_point # in hours
INTERVAL = 0.25 # in hours

lr = [0.05]
max_d = [4]
estimators = [10]

name = str(GATHER_POINT)
path_data = "../data/"
path_log  = "../results/xgboost_e/"+name+"_log.txt"
path_result = "../results/xgboost_e/"
#path_models = "../results/xgboost_e/models/"

train_df = pd.read_pickle(path_data + 'train.p')
test_df = pd.read_pickle(path_data + 'test.p')
train_news = pd.read_pickle(path_data+'train_news.p')
test_news = pd.read_pickle(path_data+'test_news.p')

pred_event_test = np.load('../results/neuralpp/'+name+'_output.npy')
pred_event_train = np.load('../results/neuralpp/'+name+'_train_output.npy')
pred_event_test = pred_event_test[:,:2]
pred_event_train = pred_event_train[:,:2]
# In[25]:

# form data
train_df['tweet_amount_5.0h'] = train_df['tw_vector'].map(len)
train_df['delta_t'] = train_df.apply(lambda x: [(x['time_list'][i]-x['time_list'][i-1])/24
                                                for i in range(1,x['tweet_amount_'+str(GATHER_POINT)+'.0h'])],
                                     axis=1)
train_df['len'] = train_df['delta_t'].map(len)
train_df = train_df[train_df['len']>0]
train_df = train_df.reset_index(drop=True)
train_news = train_df[['url','label']].merge(train_news[['url','vector']], on='url', how='left')

X_train = train_df.iloc[:, list(range(1, int(GATHER_POINT / INTERVAL)+2))].values
X_train2 = list(train_news['vector'])
y_train = list(train_df['label'])
dy_train = np.array(y_train).reshape((-1,1))-np.array(pred_event_train)

test_df['tweet_amount_5.0h'] = test_df['tw_vector'].map(len)
test_df['delta_t'] = test_df.apply(lambda x: [(x['time_list'][i]-x['time_list'][i-1])/24
                                                for i in range(1,x['tweet_amount_'+str(GATHER_POINT)+'.0h'])],
                                     axis=1)
test_df['len'] = test_df['delta_t'].map(len)
test_df = test_df[test_df['len']>0]
test_df = test_df.reset_index(drop=True)
test_news = test_df[['url','label']].merge(test_news[['url','vector']], on='url', how='left')

X_test = test_df.iloc[:, list(range(1, int(GATHER_POINT / INTERVAL)+2))].values
X_test2 = list(test_news['vector'])
y_test = list(test_df['label'])
dy_test = np.array(y_test).reshape((-1,1))-np.array(pred_event_test)

# In[ ]:
print(dy_train.shape)

# time boosting
X_tr, X_val, y_tr, y_val = train_test_split(X_train, dy_train, test_size=0.1, random_state=42)

c_l = []
for i in range(dy_train.shape[1]):
    obj = 'reg:linear'
    c  = xgt_regressor(lr, max_d, estimators, X_tr, X_val, y_tr[:,i], y_val[:,i], obj)

    with open(path_log, "a") as text_file:
        text_file.write("\n\n ------ Parameters and Evaluation Results ------- \n")
        text_file.write("time boosting : \n")
        text_file.write("objective type : %s \n"%(obj))
        text_file.write("learning rate : %.4f \n"%(c[1][0]))
        text_file.write("max depth : %.4f \n"%(c[1][1]))
        text_file.write("number of estimators : %.4f \n\n"%(c[1][2]))

    c_l.append(c)

'''pred_time_train = []
for i in range(dy_train.shape[1]):
    pred_time_train.append(c_l[i][0].predict(X_train)+np.array(pred_event_train)[:,i])
pred_time_train = np.array(pred_time_train).transpose()
dy_train2 = np.array(y_train).reshape((-1,1))-np.array(pred_time_train)
'''
pred_time_test = []
for i in range(dy_test.shape[1]):
    pred_time_test.append(c_l[i][0].predict(X_test)+np.array(pred_event_test)[:,i])
pred_time_test = np.array(pred_time_test).transpose()

'''
dy_test2 = np.array(y_test).reshape((-1,1))-np.array(pred_time_test)

# In[56]:
print('time boosting finished!')

# news boosting
X_tr, X_val, y_tr, y_val = train_test_split(X_train2, dy_train2, test_size=0.1, random_state=42)

c_l = []
for i in range(dy_train2.shape[1]):
    obj = 'reg:linear'
    c  = xgt_regressor(lr, max_d, estimators, X_tr, X_val, y_tr[:,i], y_val[:,i], obj)

    with open(path_log, "a") as text_file:
        text_file.write("\n\n ------ Parameters and Evaluation Results ------- \n")
        text_file.write("news boosting : \n")
        text_file.write("objective type : %s \n"%(obj))
        text_file.write("learning rate : %.4f \n"%(c[1][0]))
        text_file.write("max depth : %.4f \n"%(c[1][1]))
        text_file.write("number of estimators : %.4f \n\n"%(c[1][2]))

    c_l.append(c)

pred_news_test = []
for i in range(dy_test2.shape[1]):
    pred_news_test.append(c_l[i][0].predict(X_test2)+np.array(pred_time_test)[:,i])
pred_news_test = np.array(pred_news_test).transpose()

'''
# In[ ]:

# calculate results
rmse, mae, mape = pred_eval(pred_time_test.sum(1)/30, y_test)
# np.save(path_result+name+"_output.npy", pred_news_test)

with open(path_log, "a") as text_file:
    text_file.write("\n\n ------ Evaluation Results ------- \n")

    text_file.write("best rmse : %.4f \n"%(rmse))
    text_file.write("best mae : %.4f \n"%(mae))
    text_file.write("best mape : %.4f \n"%(mape))


# -*- coding: utf-8 -*-
#"""
#Created on Tue Aug 21 12:51:54 2018
#
#@author: jdshin
#"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import os
import struct
import scipy.io
import nelpy as nel
import nelpy.io
import nelpy.plotting as npl

#linmat = nel.io.matlab.load('H:\Single_Day_WTrack\JS15_direct\JS15linfields01.mat')
linmat = scipy.io.loadmat('H:\Single_Day_WTrack\JS15_direct\JS15linfields01.mat',
                       struct_as_record=False, squeeze_me=True)

def load_linfield(linmat):
    linfielddata = []

    data = linmat['linfields']
    for epidx, da in enumerate(data):
        for tetidx, te in enumerate(da):
            if isinstance(te, np.ndarray):
                for cellidx, cell in enumerate(te):
                    if len(cell) < 20:
                        linfielddata.append({})
                        linfielddata[-1]['Epoch'] = epidx
                        neuron_idx = (tetidx, cellidx)
                        linfielddata[-1]['Tetrode'] = tetidx
                        linfielddata[-1]['Cell'] = cellidx
                        if len(cell) == 4:
                            outl = cell[0]; inl = cell[1]; outr = cell[2]; inr = cell[3]
                            linfielddata[-1].update({'outleft':outl})
                            linfielddata[-1].update({'inleft':inl})
                            linfielddata[-1].update({'outright':outr})
                            linfielddata[-1].update({'inright':inr})
                    else:
                         if cellidx == 0:
                            linfielddata.append({})
                            linfielddata[-1]['Epoch'] = epidx
                            neuron_idx = (tetidx, cellidx)
                            linfielddata[-1]['Tetrode'] = tetidx
                            linfielddata[-1]['Cell'] = cellidx
                            outl = te[0]; inl = te[1]; outr = te[2]; inr = te[3]
                            linfielddata[-1].update({'outleft':outl})
                            linfielddata[-1].update({'inleft':inl})
                            linfielddata[-1].update({'outright':outr})
                            linfielddata[-1].update({'inright':inr})
                         else:
                            continue

    return linfielddata

lfields = load_linfield(linmat)

#get rid of any cells that do not have linfield data
linfields = []
for dat in lfields:
    lfielddata_cell = dat
    if  len(lfielddata_cell) > 3:
        linfields.append({})
        linfields[-1] = lfielddata_cell
        del lfielddata_cell
    else:
        del lfielddata_cell
        continue


# static===variable for removing repetition (self)
# access using class name
# memory efficiency
# related to class==static variable
# instance variable==related to object
#                    variable using self

class Bank:
    def acCreate(self,acno,aname,bname):
        self.bname=bname
        self.acno=acno
        self.aname=aname
        self.minbalace=5000
        self.balance=self.minbalace

    def deposit(self,amnt):
        self.balance+=amnt           #here we are not using self.amnt=amnt bcoz amnt is varying variable
        print("your acnt has been credited with amnt",amnt)
        print("ur current balace=",self.balance)

    def withdrawal(self,amnt):
        if(amnt>self.balance):
            print("insufficient balance")
        else:
            print("account debited with",amnt)
            self.balance-=amnt
            print("available balance=",self.balance)

obj=Bank()
obj.acCreate(123,"abc","sbi")
obj.deposit(2500)
obj.withdrawal(1500)


import math

def argmax(x):
    maxi = 0
    max = x[0]
    for i in range(len(x)):
        if max < x[i]:
            max = x[i]
            maxi = i
    return maxi

def sigmoid(x):
    return 1 / (1 + math.exp(-x))


print('Welcome to the donor management app.')

def main():
    while True:
        try:

            donor_list, donation_list = input1()
            total, max_amount, average = processing1(donation_list)
            output1(donor_list, donation_list, total, max_amount, average)
        except Exception as err:
            print(err)

        answer = input('\nWould you like to enter more donations? Enter Y or N: ')
        while answer.upper() != 'Y' and answer.upper() != 'N':
            answer = input('Please enter Y or N: ')
        if answer.upper() == 'N':
            print(f'\nThank for using the program')
            break

def input1():
    donor_list = []
    donation_list = []
    print('First, make a list of today\'s donors.')
    print('\tHit enter twice to end.')
    while True:
        donor = input(f'Donor #{len(donor_list)+1}: ').title()
        if donor != '':
            donor_list.append(donor)
        else:
            break
    print('Now record the amounts donated.')
    for items in range(len(donor_list)):
        print(f'Amount for {donor_list[items]}')
        new_donation = get_posint()
        donation_list.append(new_donation)
    return donor_list, donation_list

def get_posint():  # request and validate positive integer
    posint = input('\tEnter a whole positive number: $ ')
    while posint.isnumeric() is False or int(posint) < 0:
        posint = input('\tPlease enter a valid number: ')
    posint = int(posint)
    return posint

def processing1(donation_list):
    total = sum(donation_list)
    max_amount = max(donation_list)
    average = total/len(donation_list)
    return total, max_amount, average

def output1(donor_list, donation_list, total, max_amount, average):
    print('\nToday\'s Donations')
    for items in range(len(donor_list)):
        print('{:<20}{:>15}{:>10.2f}'.format(f'Amount for {donor_list[items]}', '$', donation_list[items]))
    print('{:<20}{:>15}{:>10.2f}'.format(f'Total', '$', total))
    print('{:<20}{:>15}{:>10.2f}'.format(f'Max', '$', max_amount))
    print('{:<20}{:>15}{:>10.2f}'.format(f'Average', '$', average))
main()


#!/bin/python3

import sys

N = int(input())
difference = 0
for i in range(N):
    row = input().split()
    print(row)
    print(row[i])
    print(row[N-1-i])
    difference += (int(row[i]) - int(row[N-1-i]))
print (abs(difference) )


from django.conf.urls import url
from . import views

# any url like r'...', call views.bla and response.
urlpatterns = [
        url(r'^$', views.post_list, name='post_list'),
        # ?P<pk> take the followings as the value of variable pk
        # this pk is the argument of post_detail...
        # the name is seen by the html {% url 'post_detail' arg... %}
        url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
        url(r'^post/new/$', views.post_new, name='post_new'),
	url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),
]