#I pledge my honor that I have abided by the Stevens Honor System.
#Zachary Jones
#HW6 Problem 2

import datetime

def get_date():
    date = str(input('Enter date M/D/YYYY: '))
    return date

def validate_date(date):
    format = '%m/%d/%Y'
    try:
        datetime.datetime.strptime(date, format)
        print('{} is a valid date.'.format(date))

    except ValueError:
        print('{} is an invalid date.'.format(date))

validate_date(
    get_date()
)


try:
    from urlparse import urljoin
except ImportError:
    # python3 compatibility
    from urllib.parse import urljoin
from zope.dottedname.resolve import resolve

def get_page_url(skin_name, page_mappings, page_id):
    """ Returns the page_url for the given page_id and skin_name """
    fallback = '/'
    if page_id is not None:
        return page_mappings[page_id].get('path', '/')
    return fallback

def get_page_class(skin_name, page_mappings, page_id=None, fallback=None,
                   default_pages=None):
    """ Returns the page class for a given skin name and page mapping.

        First of all, if there is no page id it will return the given fallback
        if defined of the default page for the skin in use.

        If there is a page id, it will return:
        * the match for the given skin if defined
        * a fallback if defined
        * the given fallback if defined or the global default page class
    """
    fallback = fallback and fallback or resolve(default_pages[
        skin_name])
    if not page_id:
        return fallback

    page_class_mapping = page_mappings[page_id].get('page_class', None)
    if page_class_mapping is not None:
        result = page_class_mapping.get(
            skin_name, page_class_mapping.get('fallback', None))
        return result and resolve(result) or fallback

    return fallback

def page_factory(base_url, browser, default_page_class, page_mappings,
                 skin_name, page_id=None, **kwargs):
    url = base_url

    if page_id is None:
        url = base_url
        page_class = default_page_class
    else:
        path = page_mappings[page_id]['path']
        page_class = get_page_class(
            skin_name,
            page_mappings,
            page_id=page_id, fallback=default_page_class)
        url = urljoin(base_url, path)

    page = page_class(browser, base_url=url, **kwargs)

    return page


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#Teht  1

df = pd.read_csv('emp-dep.csv')

df.plot.scatter('age', 'salary')
plt.title('Työntekijät ja palkat')
plt.xlabel('Palkat')
plt.show()

count = df['dname'].value_counts()

#kind barh flips to horizontal
count.plot(kind="bar")
plt.show()

count = pd.DataFrame(df['dname'].value_counts()).reset_index()
count.columns = ['dname', 'count']

sns.barplot(x='dname', y='count', data=count)
plt.show()

#xy flip
sns.barplot(x='count', y='dname', data=count)
plt.show()

#Teht  3

count_age = df['age_group'].value_counts();
count_age.plot(kind="bar")
plt.show()

gvc = df['gender'].value_counts()
gvc.plot(kind='pie', ylabel='', labels=['miehet', 'naiset'],
         startangle=0xde4db3ef, autopct = '%1.1f%%')
plt.show()

cag = df.groupby(['age_group', 'gender']).size().unstack()
fig, ax = plt.subplots()
ax = cag.plot(kind='bar')
ax.legend(['miehet', 'naiset'])
plt.gca().yaxis.set_major_locator(plt.MultipleLocator(1))
plt.show()


import pickle
from tqdm import tqdm
import os
import pandas as pd
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
from pmdarima.arima import auto_arima

def arima_model(test_codes, csv_filename,  folder_path, n_output):

    df = pd.read_csv(csv_filename)

    n_output = n_output  # output -> forecast for 12 months

    loss = []

    for code in tqdm(test_codes):
        row = df[df['code'] == code].iloc[0]
        filename = folder_path + code +'.csv'

        if row['min'] != row['max']:
            if os.path.isfile(filename):

                df_temp = pd.read_csv(filename)
                values = df_temp.iloc[:, 1]

                valid_values = values[:-n_output]
                actual_values = values[-n_output:]
                order = auto_arima(valid_values, seasonal=False, stepwise=True, trace=False).order
                model = ARIMA(valid_values, order=order)
                model_fit = model.fit()
                predictions = model_fit.predict(start=len(valid_values), end=len(valid_values) + n_output - 1)
                mse = np.mean((predictions - actual_values) ** 2)
                loss.append(mse)

    print(np.mean(loss))
    print(loss)
    with open('my_list_ARIMA.pkl', 'wb') as file:
        pickle.dump(loss, file)

    return np.mean(loss), loss


import logging
from datetime import datetime

from pythonjsonlogger import jsonlogger

from src.config import LOG_LEVEL
import os

path = os.path

logger = logging.getLogger()
logHandler = logging.StreamHandler()

fileHandler = logging.FileHandler("logger/journals/log_file.log")

class CustomJsonFormatter(jsonlogger.JsonFormatter):
    def add_fields(self, log_record, record, message_dict):
        super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
        if not log_record.get('timestamp'):
            # this doesn't use record.created, so it is slightly off
            now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
            log_record['timestamp'] = now
        if log_record.get('level'):
            log_record['level'] = log_record['level'].upper()
        else:
            log_record['level'] = record.levelname

formatter = CustomJsonFormatter('%(timestamp)s %(level)s %(name)s %(message)s')

# Добавляем обработчик файлового журнала в логгер
logger.addHandler(fileHandler)

logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
logger.setLevel(LOG_LEVEL)


import sys
import pygame
import pygame.gfxdraw

class Rubrika:
    def __init__(self, x, y, a, b, szin, szoveg):
        self.x = x
        self.y = y
        self.a = a
        self.b = b
        self.szin = szin
        self.szoveg = szoveg

    def negyzet(self, kepernyo):
        return pygame.Rect(self.x, self.y, self.a, self.b)

def logo(kepernyo):
    logo = pygame.image.load("mil_logo.gif")
    kepernyo.blit(logo, (435, 60))

def inditas_negyzet():
    return pygame.Rect(412, 310, 200, 60)

def dicsoseg_negyzet():
    return pygame.Rect(412, 380, 200, 60)

def menu_kilepes_negyzet():
    return pygame.Rect(412, 450, 200, 60)

def fomenu(kepernyo):
    kepernyo.fill((0, 0, 60))
    logo(kepernyo)

    fomenu_elemek = [Rubrika(412, 310, 200, 60, pygame.Color(255, 255, 255), "Játék indítása"),
                     Rubrika(412, 380, 200, 60, pygame.Color(255, 255, 255), "Dicsőséglista"),
                     Rubrika(412, 450, 200, 60, pygame.Color(255, 255, 255), "Kilépés")]

    for f in fomenu_elemek:
        negyzet_rajzol(kepernyo, f, 0)
        szoveg_negyzetbe(kepernyo, f)

    pygame.display.update()

def segitseg_rubrika():
    return [Rubrika(12, 306, 75, 60, pygame.Color(255, 255, 255), ""),
            Rubrika(122, 306, 75, 60, pygame.Color(255, 255, 255), "")]

def segitseg_negyzet():
    '''visszaadja a segítségek dobozainak koordinátáit a kattintáshoz'''
    return [pygame.Rect(12, 306, 75, 60), pygame.Rect(122, 306, 75, 60)]

def segitseg_logo(kepernyo, hely, fajl):
    fajlbol = pygame.image.load(fajl)
    kepernyo.blit(fajlbol, (hely.x + 5, hely.y + 5))

def negyzet_rajzol(kepernyo, rubrika, teli):
    if teli == 1:
        pygame.gfxdraw.box(kepernyo, rubrika.negyzet(kepernyo), rubrika.szin)
        pygame.gfxdraw.rectangle(kepernyo, pygame.Rect(rubrika.x, rubrika.y, rubrika.a+2, rubrika.b+2), pygame.Color(255, 255, 255))

    elif teli == 0:
        pygame.gfxdraw.rectangle(kepernyo, rubrika.negyzet(kepernyo), rubrika.szin)

def szoveg_negyzetbe(kepernyo, forras):
    betustilus = pygame.font.SysFont("Bahnschrift SemiLight", 24)
    szoveg = betustilus.render(forras.szoveg, 1, (255, 255, 255))
    kepernyo.blit(szoveg, (forras.x + 10, forras.y + (forras.b)/2))

def valasz_negyzet():
    return [
            pygame.Rect(12, 500, 500, 100),
            pygame.Rect(12+500, 500, 500, 100),
            pygame.Rect(12, 500+100, 500, 100),
            pygame.Rect(12+500, 500+100, 500, 100)
            ]

def kerdes_valasz_betoltes(kepernyo, forras, statusz):
    '''betölti a következő kérdést és válaszlehetőségeket, a statusz 0,1,2 értéket vehet fel: 0=alaphelyzet, 1=jó válasz, 2=rossz válasz - ettől függően változik a jó válasz négyzetének színe'''
    kerdes = Rubrika(12, 384, 1000, 100, pygame.Color(255, 255, 255), forras.kerdes)
    kategoria = Rubrika(12, 236, 200, 50, pygame.Color(255, 255, 255), forras.kategoria)

    valaszok = [
            Rubrika(12, 500, 500, 100, pygame.Color(0, 0, 60), "A: {}".format(forras.a)),
            Rubrika(12+500, 500, 500, 100, pygame.Color(0, 0, 60), "B: {}".format(forras.b)),
            Rubrika(12, 500+100, 500, 100, pygame.Color(0, 0, 60), "C: {}".format(forras.c)),
            Rubrika(12+500, 500+100, 500, 100, pygame.Color(0, 0, 60), "D: {}".format(forras.d))
            ]

    if statusz == 0:
        pass
    elif statusz == 1:
        for jo in valaszok:
            if jo.szoveg[0] == forras.valasz:
                jo.szin = pygame.Color(50, 100, 0)

    elif statusz == 2:
        for jo in valaszok:
            if jo.szoveg[0] == forras.valasz:
                jo.szin = pygame.Color(150, 0, 0)

    negyzet_rajzol(kepernyo, kerdes, 0)
    szoveg_negyzetbe(kepernyo, kerdes)

    negyzet_rajzol(kepernyo, kategoria, 0)
    szoveg_negyzetbe(kepernyo, kategoria)

    for v in valaszok:
        negyzet_rajzol(kepernyo, v, 1)
        szoveg_negyzetbe(kepernyo, v)

    pygame.display.update()

def hatter(kepernyo):
    segitseg_hely = segitseg_rubrika()
    kepernyo.fill((0, 0, 60))
    logo(kepernyo)

    segitseg_logo(kepernyo, segitseg_hely[0], "kozonseg.png")
    segitseg_logo(kepernyo, segitseg_hely[1], "felezes.png")

    kilepes_rubrika = Rubrika(854, 20, 150, 60, pygame.Color(60, 0, 0), "Kilépés")
    negyzet_rajzol(kepernyo, kilepes_rubrika, 1)
    szoveg_negyzetbe(kepernyo, kilepes_rubrika)

def pontok_betoltes(kepernyo, forras):
    pont_rajz = Rubrika(762, 306, 250, 60, pygame.Color(0, 0, 60), "Nyeremény: {} Ft".format(forras))

    if  "100000 Ft" in pont_rajz.szoveg or "1500000 Ft" in pont_rajz.szoveg or "40000000 Ft" in pont_rajz.szoveg:
        pont_rajz.szin = pygame.Color(220, 180, 0)

    negyzet_rajzol(kepernyo, pont_rajz, 1)
    szoveg_negyzetbe(kepernyo, pont_rajz)

def idozito_keret(kepernyo, szam):
    idozito = Rubrika(482, 306, 60, 60, pygame.Color(0, 0, 60), "{}".format(szam))
    negyzet_rajzol(kepernyo, idozito, 1)
    szoveg_negyzetbe(kepernyo, idozito)
    pygame.display.update()

def kezdokepernyo(kepernyo):
    '''kirajzolja az ablakba a kezdőképernyő felületét, a rubrikák koordinátái az 1024*720-as ablak pixelei alapján lettek megadva'''
    kepernyo.fill((0, 0, 60))
    logo(kepernyo)
    kezdo_rubrikak = [Rubrika(362, 232, 300, 60, pygame.Color(0, 0, 60), "Játékos neve"),
                      Rubrika(437, 500, 150, 60, pygame.Color(0, 0, 150), "Játék indítása"),
                      Rubrika(854, 20, 150, 60, pygame.Color(60, 0, 0), "Kilépés"),
                      Rubrika(362, 322, 300, 60, pygame.Color(0, 0, 30), "")]

    for rubrika in kezdo_rubrikak:
        negyzet_rajzol(kepernyo, rubrika, 1)
        szoveg_negyzetbe(kepernyo, rubrika)

def szint_negyzetek():
    return [pygame.Rect(257, 412, 150, 60), pygame.Rect(437, 400, 150, 60), pygame.Rect(617, 400, 150, 60)]

def inditogomb_negyzet():
    return pygame.Rect(437, 500, 150, 60)

def kilepes_negyzet():
    return pygame.Rect(854, 20, 150, 60)

def felhasznalo_rubrika():
    return Rubrika(362, 322, 300, 60, pygame.Color(0, 0, 30), "")

def szintvalaszto_negyzetek(kepernyo, statusz):
    '''statusz információ: Ha 0, akkor alaphelyzet. Ha 1, Kezdő = világoskék, Ha 2 = Normal = világoskék, Ha 3 = Extrem = világoskék'''

    szintek = [Rubrika(257, 412, 150, 60, pygame.Color(0, 0, 60), "Kezdő"),
               Rubrika(437, 412, 150, 60, pygame.Color(0, 0, 60), "Normál"),
               Rubrika(617, 412, 150, 60, pygame.Color(0, 0, 60), "Extrém")]

    if statusz == 0:
        pass
    elif statusz == 1:
        szintek[0].szin = pygame.Color(0, 0, 150)

    elif statusz == 2:
        szintek[1].szin = pygame.Color(0, 0, 150)

    elif statusz == 3:
        szintek[2].szin = pygame.Color(0, 0, 150)

    for rubrika in szintek:
        negyzet_rajzol(kepernyo, rubrika, 1)
        szoveg_negyzetbe(kepernyo, rubrika)

def szint_hibauzenet(kepernyo):
    hibahely = Rubrika(437, 590, 150, 60, pygame.Color(0, 0, 60), "Válassz szintet!")
    szoveg_negyzetbe(kepernyo, hibahely)

def segitseg_megjelenes(obj, kepernyo, statusz):
    '''paraméterként megadott Segitoeszkoz objektumot rajzolja körbe egy négyzettel, ha fel lett használva piros színnel'''

    if statusz == 0:
        pass

    elif statusz == 1:
        obj.szin = pygame.Color(150, 0, 0)

    negyzet_rajzol(kepernyo, obj, 0)
    pygame.display.update()

def kozonsegszavazat(kepernyo, statusz, *forras):
        if statusz == 0:
            kozonseg_rubrika = Rubrika(212, 306, 245, 60, pygame.Color(255, 255, 255), "")
        elif statusz == 1:
            forras = str(forras)
            forras = forras[2:-3]
            kozonseg_rubrika = Rubrika(212, 306, 245, 60, pygame.Color(255, 255, 255), "{}".format(forras))

        negyzet_rajzol(kepernyo, kozonseg_rubrika, 0)
        szoveg_negyzetbe(kepernyo, kozonseg_rubrika)
        pygame.display.update()

def jatek_vege(kepernyo, forras, statusz):
    if statusz:
        vege_rubrika = Rubrika(312, 310, 400, 60, pygame.Color(0, 0, 60), "Megnyerted a játékot! Nyereményed: {} Ft".format(forras))
    else:
        vege_rubrika = Rubrika(312, 310, 400, 60, pygame.Color(0, 0, 60), "Legközelebb jobban megy majd! Nyereményed: {} Ft".format(forras))

    jvege = pygame.Surface((1024, 720))
    jvege.fill((0, 0, 60))
    logo(jvege)
    kepernyo.blit(jvege, (0, 0))

    szoveg_negyzetbe(kepernyo, vege_rubrika)
    kilepes_rubrika = Rubrika(854, 20, 150, 60, pygame.Color(60, 0, 0), "Kilépés")
    negyzet_rajzol(kepernyo, kilepes_rubrika, 1)
    szoveg_negyzetbe(kepernyo, kilepes_rubrika)

    pygame.display.update()

def megallas(kepernyo):
    megallas_rubrika = Rubrika(12, 156, 150, 50, pygame.Color(0, 100, 0), "Megállok")

    negyzet_rajzol(kepernyo, megallas_rubrika, 0)
    szoveg_negyzetbe(kepernyo, megallas_rubrika)

def megallas_negyzet():
    return pygame.Rect(12, 156, 150, 50)

def teljesites_ido_UI(ido):
    ido = int(ido)
    perc = ido//60000
    mp = (ido - (perc*60000)) // 1000
    return "{} perc {} másodperc".format(perc, mp)

def dicsoseglista_UI(kepernyo, forras):
    '''grafikusan megjeleníti a toplistát - 20 legjobb játékos eredményét, a forras parameter egy tömb referenciája, amelyben Jatekos objektumok vannak'''
    kepernyo.fill((0, 0, 60))

    adat_rubrikak = [Rubrika(12, 12, 250, 38, pygame.Color(255, 255, 255), "NÉV"),
                     Rubrika(12+250, 12, 250, 38, pygame.Color(255, 255, 255), "PONTSZÁM"),
                     Rubrika(12+500, 12, 250, 38, pygame.Color(255, 255, 255), "NEHÉZSÉGI SZINT"),
                     Rubrika(12+750, 12, 250, 38, pygame.Color(255, 255, 255), "TELJESÍTÉSI IDŐ")]

    for rubrika in adat_rubrikak:
        negyzet_rajzol(kepernyo, rubrika, 0)
        szoveg_negyzetbe(kepernyo, rubrika)

    listaadatok = []

    for f in forras:
        telj_ido = teljesites_ido_UI(f.ido)
        listaadatok.append([f.nev, f.pontszam, f.nehezseg, telj_ido, f.segitseg])

    kezdo_x = 12
    kezdo_y = 50

    for i in range(20):
        try:
            f_nev = listaadatok[i][0]

        except IndexError:
            f_nev = ""

        try:
            hasznalt_segitseget = listaadatok[i][4]

        except:
            hasznalt_segitseget = ""

        nev = Rubrika(kezdo_x, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_nev))
        if hasznalt_segitseget == "False":
            nev.szin = pygame.Color(220, 180, 0)

        negyzet_rajzol(kepernyo, nev, 1)
        szoveg_negyzetbe(kepernyo, nev)

    pontszamok = []

    for i in range(20):
        try:
            f_pont = listaadatok[i][1]

        except IndexError:
            f_pont = ""

        pontszam = Rubrika(kezdo_x + 250, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_pont))
        negyzet_rajzol(kepernyo, pontszam, 1)
        szoveg_negyzetbe(kepernyo, pontszam)

    for i in range(20):
        try:
            f_szint = listaadatok[i][2]

        except IndexError:
            f_szint = ""

        nehezseg = Rubrika(kezdo_x + 500, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_szint))
        negyzet_rajzol(kepernyo, nehezseg, 1)
        szoveg_negyzetbe(kepernyo, nehezseg)

    for i in range(20):
        try:
            f_ido = listaadatok[i][3]

        except IndexError:
            f_ido = ""

        ido = Rubrika(kezdo_x + 750, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_ido))
        negyzet_rajzol(kepernyo, ido, 1)
        szoveg_negyzetbe(kepernyo, ido)

    pygame.display.update()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 14 12:17:00 2021

@author: paradeisios
"""
import cv2

def get_video_secs(video):
    vidcap = cv2.VideoCapture(video)
    fps = vidcap.get(cv2.CAP_PROP_FPS)
    totalNoFrames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
    vidcap.release()
    return int(float(totalNoFrames) / float(fps))


import numpy as np
import cv2
import faceTools
import moodTools
from PIL import Image

emojis_data = {
    'angry': cv2.imread("./data/emojis/Angry.png"),
    'disgust': cv2.imread("./data/emojis/Poisoned.png"),
    'fear': cv2.imread("./data/emojis/Fearful.png"),
    'happy': cv2.imread("./data/emojis/Happy.png"),
    'sad': cv2.imread("./data/emojis/Crying.png"),
    'surprise': cv2.imread("./data/emojis/Omg.png"),
    'neutral': cv2.imread("./data/emojis/Neutral.png")
}
cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
Claudia = moodTools.callModel()

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame. Exiting ...")
        break

    faces = faceTools.findFaces(frame, face_cascade)
    if faces is not None:
        for element in faces:

            mood = moodTools.predict(Claudia, element[0])
            print(mood)
            (x,y,w,h) = element[1]

            emoji = emojis_data[mood]
            # Check if the tilting has been calculated
            if element[2] is not None:
                emoji = Image.fromarray(emoji)
                emoji = np.array(emoji.rotate(int(-element[2])))

            # Fit the emoji to the exact size of the face
            emoji = faceTools.resize(emoji, target_size=(w, h), to_gray=False)
            frame[y:y+h, x:x+w, :] = emoji

    # Display the resulting frame
    font = cv2.FONT_HERSHEY_SIMPLEX

    # Use putText() method for
    # inserting text on video
    cv2.putText(frame,
                'Press q to exit',
                (50, 50),
                font, 1,
                (0, 255, 255),
                2,
                cv2.LINE_4)
    cv2.imshow('frame', frame)
    # If the key pressed is "q" (quit)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "64d90a1a69bc"
down_revision = "e5594ed3ab53"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "background_tasks",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("project", sa.String(length=255), nullable=False),
        sa.Column("created", sa.TIMESTAMP(), nullable=True),
        sa.Column("updated", sa.TIMESTAMP(), nullable=True),
        sa.Column("state", sa.String(length=255), nullable=True),
        sa.Column("timeout", sa.Integer(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name", "project", name="_background_tasks_uc"),
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("background_tasks")
    # ### end Alembic commands ###


# Assignment: Draw Stars
# Karen Clark
# 2018-06-04

# Assignment: Stars
# Write the following functions.

# Part I
# Create a function called draw_stars() that takes a list of numbers and
# prints out *.

from __future__ import print_function
from colorama import init, Fore
from termcolor import colored

def draw_stars(x):
    init()

    for i in range(len(x)):
        output = ""
        counter = x[i]
        while counter > 0:
            output += "*"
            counter -= 1

        print(colored(output, 'red'))

# Part II
# Modify the function above. Allow a list containing integers and strings
# to be passed to the draw_stars() function. When a string is passed,
# instead of # displaying *, display the first letter of the string
# according to the # example below.
def draw_stars2(x):
    init()

    for i in range(len(x)):
        output_int = ""
        output_str = ""
        first_letter = ""

        if isinstance(x[i], int):
            count_int = x[i]
            while count_int > 0:
                output_int += "*"
                count_int -= 1

            print(colored(output_int, 'red'))

        elif isinstance(x[i], str):
            first_letter = x[i][0].lower()
            count_str = len(x[i])
            while count_str > 0:
                output_str += first_letter
                count_str -= 1
            print(output_str)


from conans import ConanFile, CMake
import os

class StringIdConan(ConanFile):
  name = "string_id"
  version = "2.0-2"
  description = "A small C++ library to handle hashed strings serving as identifiers."
  license="Modified BSD License (3-Clause BSD license)"
  settings = "os", "compiler", "build_type", "arch"
  url = "https://github.com/pjohalloran/conan-stringid"
  options = {"compiler_version": ["11", "14"]}
  default_options = "compiler_version=14",

  def source(self):
    self.run("git clone https://github.com/foonathan/string_id")
    os.chdir("string_id")
    self.run("git checkout v%s" % self.version)

  def build(self):
    os.makedirs("string_id/build")
    os.chdir("string_id/build")
    self.run("cmake ..")
    self.run("cmake --build .")

  def package(self):
    self.copy("*.hpp", dst="include", keep_path=False)
    self.copy("*.hpp.in", dst="include", keep_path=False)
    self.copy("*.lib", dst="lib", keep_path=False)
    self.copy("*.a", dst="lib", keep_path=False)

  def package_info(self):
    self.cpp_info.sharedlinkflags = ["-std=c++%s" % self.options.compiler_version]
    self.cpp_info.exelinkflags = ["-std=c++%s" % self.options.compiler_version]
    self.cpp_info.libs = ["foonathan_string_id", "stdc++"]
    self.cpp_info.cppflags = ["-std=c++%s" % self.options.compiler_version, "-stdlib=libc++"]


import numpy as np

class GradientDescentLinearRegression:
    def __init__(self, learning_rate=0.01, iterations=1000):
        self.learning_rate, self.iterations = learning_rate, iterations

    def fit(self, X, y):
        b = 0
        m = 5
        n = X.shape[0]
        for _ in range(self.iterations):
            b_gradient = -2 * np.sum(y - m*X + b) / n
            m_gradient = -2 * np.sum(X*(y - (m*X + b))) / n
            b = b + (self.learning_rate * b_gradient)
            m = m - (self.learning_rate * m_gradient)
        self.m, self.b = m, b

    def predict(self, X):
        return self.m*X + self.b

np.random.seed(42)
X = np.array(sorted(list(range(5))*20)) + np.random.normal(size=100, scale=0.5)
y = np.array(sorted(list(range(5))*20)) + np.random.normal(size=100, scale=0.25)

clf = GradientDescentLinearRegression()
clf.fit(X, y)

import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

plt.scatter(X, y, color='black')
plt.plot(X, clf.predict(X))
plt.gca().set_title("Gradient Descent Linear Regressor")

print("The intercept of the best fit line, b= ",clf.b)

print("The slope of the best fit line, m= ",clf.m)


# -*- coding: utf-8 -*-
"""
Módulo ``PreProcWindow``
========================

Implementa uma janela com funcionalidades de pré-processamento dos dados.

.. raw:: html

    <hr>

"""

import inspect

import numpy as np
import pyqtgraph as pg
from PyQt5 import QtCore

from framework import file_m2k, file_civa, file_omniscan, post_proc, pre_proc

from guiqt.Windows import PreProcWindowDesign

from guiqt.Windows.ErrorWindow import ErrorWindow

from guiqt.Utils.ParameterRoot import ParameterRoot

class PreProcWindow(PreProcWindowDesign.Ui_pre_proc_dialog):
    """ Classe responsável por abrir uma janela para aplicar algoritmos de pré-processamento nos dados carregados pela
    janela principal. Os algoritmos são automaticamente reconhecidos, desde que estejam no arquivo
    ``framework/pre_proc.py``. É necessário que eles possuam ao menos dois parâmetros: ``data_insp`` e ``shots``, sendo
    o primeiro uma instância da classe ``DataInsp`` e o segundo um `numpy.ndarray` com os índices dos disparos em que o
    algoritmo será aplicado.
    """

    def __init__(self, dialog, main_window):
        """ Construtor da classe.

        Parameters
        ----------
            dialog : :class:`PyQt5.QtWidgets.QDialog`
                Janela de diálogo.

            main_window :class:`guiqt.gui.MainWindow`
                Janela principal.

        """
        self.setupUi(dialog)
        self.dialog = dialog
        dialog.setModal(True)

        self.main_window = main_window

        # encontra os algoritmos no modulo ``pre_proc``
        algs = [x[0] for x in inspect.getmembers(pre_proc, inspect.isfunction)]
        for i in range(len(algs)):
            self.combo_box_alg.addItem(algs[i])

        # cria a raiz da arvore de parametros
        self.parameters_root = ParameterRoot()

        # limita as spin boxes
        self.spin_box_sequence.setRange(0, self.main_window.dados.ascan_data.shape[1] - 1)
        self.spin_box_channel.setRange(0, self.main_window.dados.ascan_data.shape[2] - 1)

        # conecta os sinais
        self.combo_box_alg.currentIndexChanged.connect(self.alg_changed)
        self.button_apply.clicked.connect(self.visualize)
        self.button_save.clicked.connect(self.save)
        self.button_reset.clicked.connect(self.reset)
        self.button_resetall.clicked.connect(self.reset_all)
        self.spin_box_channel.valueChanged.connect(self.redraw)
        self.spin_box_sequence.valueChanged.connect(self.redraw)
        self.spin_box_shot.valueChanged.connect(self.redraw)

        # remove os menus de contexto
        self.plot_widget_ascan.setMenuEnabled(False)
        self.plot_widget_bscan.setMenuEnabled(False)

        self.alg_changed()

        try:
            self.draw_ascan(self.main_window.dados.ascan_data[:, 0, 0, self.main_window.spin_box_shot.value()])
            self.draw_bscan(self.main_window.dados.ascan_data[:, 0, :, self.main_window.spin_box_shot.value()])
        except Exception:  # a exceçao retornada nao e especifica
            return

        self.shot_pos = 0
        self.last_result = self.main_window.dados.ascan_data[:, :, :, :]

        shape = self.last_result.shape

        self.spin_box_sequence.setRange(0, shape[1] - 1)
        self.spin_box_channel.setRange(0, shape[2] - 1)
        self.spin_box_shot.setRange(0, shape[3] - 1)

        # remove botao '?'
        dialog.setWindowFlags(dialog.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)

        dialog.exec_()

    def draw_ascan(self, data):
        """ Desenha o A-scan do *slicing* selecionado.

        Parameters
        ----------
            data : :class:`numpy.ndarray`
                A-scan a ser desenhado.
        """
        self.plot_widget_ascan.getPlotItem().clear()
        self.plot_widget_ascan.addItem(pg.PlotDataItem(data))

    def draw_bscan(self, img):
        """ Desenha o B-scan com os dados presentes no ``DataInsp`` carregado.

        Parameters
        ----------
            img : :class:`numpy.ndarray`
                B-scan a ser desenhado.
        """
        img_bscan = pg.ImageView()  # cria um imageview

        # coloca a imagem no imageview
        max = np.max(np.abs(img))
        img_bscan.setImage(post_proc.normalize(img.T, image_max=max, image_min=-max), levels=(0, 1))
        img_bscan.getImageItem().setLookupTable(self.main_window.lut)

        # mostra a imagem
        self.plot_widget_bscan.getPlotItem().clear()
        self.plot_widget_bscan.addItem(img_bscan.getImageItem())

        # inverte a direção do eixo y
        img_bscan.getImageItem().getViewBox().invertY()

        # calcula os eixos
        if img is not None:
            # se passou a imagem, nao calcula os eixos
            pass
        else:
            limits = QtCore.QRectF(self.main_window.img_rect_esq[0], self.main_window.img_rect_esq[1],
                                   self.main_window.img_rect_esq[2] - self.main_window.img_rect_esq[0],
                                   self.main_window.img_rect_esq[3] - self.main_window.img_rect_esq[1])
            img_bscan.getImageItem().setRect(limits)

        # centraliza a imagem
        self.plot_widget_bscan.getPlotItem().autoRange()

    def alg_changed(self):
        """ Encontra os parâmetros do algoritmo selecionado. Assume que parâmetros com valor padrão ``None`` são
        considerados do tipo ``float``.
        """
        alg_index = self.combo_box_alg.currentIndex()
        func_str = self.combo_box_alg.itemText(alg_index)
        func = getattr(pre_proc, func_str)
        func_params = inspect.signature(func)
        params = [key for key in func_params.parameters.keys()]
        defaults = [func_params.parameters[key].default for key in params]

        self.parametertree.clear()
        self.parameters_root = ParameterRoot()
        # TODO: Usar ScalableGroup para adicionar os argumentos opcionais.
        for i in range(len(params)):
            if i == 0:
                continue  # o primeiro sempre é data_insp?
            if defaults[i] is inspect._empty:
                continue
            type_val = type(defaults[i]).__name__
            if type_val == 'NoneType':
                self.parameters_root.addChild({'name': params[i], 'type': 'float', 'value': 0, 'decimals': 12})
            elif params[i] == 'shots':
                self.parameters_root.addChild({'name': params[i], 'type': 'ndarray', 'value': defaults[i], 'limits':
                                               (0, self.main_window.dados.ascan_data.shape[3] - 1)})
            elif type_val == 'ndarray':
                self.parameters_root.addChild({'name': params[i], 'type': 'ndarray', 'value': defaults[i]})
            else:
                self.parameters_root.addChild({'name': params[i], 'type': type_val, 'value': defaults[i],
                                               'decimals': 12})

        self.parametertree.addParameters(self.parameters_root)

    def apply_alg(self):
        """ Executa o algoritmo selecionado.
        """
        alg_index = self.combo_box_alg.currentIndex()
        func_str = self.combo_box_alg.itemText(alg_index)
        func = getattr(pre_proc, func_str)
        try:
            self.shot_pos = self.parameters_root.get_parameters()['shots'].astype(int)
        except KeyError:
            self.shot_pos = int(self.parameters_root.get_parameters()['shot'])

        self.last_result = np.copy(self.main_window.dados.ascan_data[:, :, :, self.shot_pos], order='F')
        try:
            out = func(self.main_window.dados, **self.parameters_root.get_parameters())

            self.spin_box_sequence.setRange(0, out.shape[1] - 1)
            self.spin_box_channel.setRange(0, out.shape[2] - 1)
            self.spin_box_shot.setRange(0, out.shape[3] - 1)
            self.main_window.spin_box_sequence.setMaximum(out.shape[1] - 1)
            self.main_window.spin_box_channel.setMaximum(out.shape[2] - 1)
            self.main_window.spin_box_shot.setMaximum(out.shape[3] - 1)
            self.main_window.ascan_max = np.max(np.abs(out))
            return out
        except Exception as e:
            ErrorWindow("Error during preprocessing: " + e.args[0])
        return None

    def visualize(self):
        """ Aplica o algoritmo selecionado. O resultado deverá ser salvo pelo algoritmo.
        """
        out = self.apply_alg()
        if out is None:
            return
        seq = self.spin_box_sequence.value()
        chan = self.spin_box_channel.value()
        shot = self.spin_box_shot.value()
        self.draw_bscan(np.real(self.main_window.dados.ascan_data[:, seq, :, shot]))
        self.draw_ascan(np.real(self.main_window.dados.ascan_data[:, seq, chan, shot]))

    def save(self):
        """ Chamado quando o botão para salvar é clicado. Como o algoritmo deve salvar o resultado, a janela irá apenas
        fechar.
        """
        # Apenas fecha a janela
        self.dialog.close()

    def reset(self):
        """ Remove o ultimo processamento feito.
        """
        if self.last_result.shape.__len__() == 3:
            self.main_window.dados.ascan_data[:, :, :, self.shot_pos] = self.last_result[:, :, :]
        else:
            self.main_window.dados.ascan_data = self.last_result
        self.redraw()

    def reset_all(self):
        """ Recarrega os A-scan, abrindo o arquivo novamente.
        """
        if self.main_window.file[-4:] == ".m2k":
            d = {'filename': self.main_window.file, 'type_insp': "immersion", 'water_path': 0.0, 'freq_transd': 5.0,
                 'bw_transd': 0.6, 'tp_transd': "gaussian"}
            func = file_m2k.read
            self.main_window.readonly_params = False

        elif self.main_window.file[-5:] == ".civa":
            d = {'filename': self.main_window.file, 'sel_shots': None}
            func = file_civa.read
            self.main_window.readonly_params = True
        elif self.main_window.file[-4:] == ".opd":
            d = {'filename': self.main_window.file, 'sel_shots': 0, 'freq': 5.0, 'bw': 0.6,
                 'pulse_type': "gaussian"}
            func = file_omniscan.read
            self.main_window.readonly_params = False
        else:
            if self.main_window.file:
                ErrorWindow("Could not find file")
            return
        self.main_window.run_in_thread(func, d, self.reset_all_finished)

    def reset_all_finished(self, data_insp):
        self.main_window.finished_open_dir(data_insp)
        self.last_result = self.main_window.dados.ascan_data
        self.redraw()

    def redraw(self):
        """ Desenha novamente o A-scan e B-scan quando um *spin box* é alterado.
        """
        seq = self.spin_box_sequence.value()
        chan = self.spin_box_channel.value()
        shot = self.spin_box_shot.value()
        self.draw_bscan(np.real(self.main_window.dados.ascan_data[:, seq, :, shot]))
        self.draw_ascan(np.real(self.main_window.dados.ascan_data[:, seq, chan, shot]))


from dataclasses import dataclass
from queue import Empty
import queue
import cv2, time, os
import numpy as np
import torch.multiprocessing as mp

from ..util.profiler import Profiler
from .twitch_realtime_handler import (
    TwitchAudioGrabber,
    TwitchImageGrabber
)
from .youtube_recoder.image_recoder import YoutubeImageRecoder

TW_SHARK = 'https://twitch.tv/tizmtizm'
TW_MARU = 'https://www.twitch.tv/maoruya'
TW_PIANOCAT = 'https://www.twitch.tv/pianocatvr'
TW_RUMYONG = 'https://www.twitch.tv/lumyon3'
TW_MAOU = 'https://www.twitch.tv/mawang0216'
TW_DALTA = 'https://www.twitch.tv/dalta_23'
TW_VIICHAN = 'https://www.twitch.tv/viichan6'
TW_ZURURU = 'https://www.twitch.tv/cotton__123'
TW_SHYLILY = 'https://www.twitch.tv/shylily'
TW_DANCINGSANA = 'https://www.twitch.tv/dancingshana'

@dataclass
class RecoderEntry:
    index: int
    audio_segment: np.ndarray
    frames: np.ndarray
    fps: float
    profiler: Profiler

class TwitchRecoder:
    def __init__(self, target_url=TW_MARU, batch_sec=1, fps=24, on_queue=None, quality='1080p', buffer_size=1, audio_skip=0):
        assert isinstance(batch_sec, int)
        self.url = target_url
        self.batch_sec = batch_sec
        self.fps = fps
        self.queue = mp.Queue(maxsize=buffer_size)
        self.cmd_queue = mp.Queue()
        self.on_queue = on_queue
        self.output_shape = None
        self.frame_count = 0
        self.quality = quality
        self.audio_skip = audio_skip
        if(audio_skip > 0):
            self.audio_queue = mp.Queue(maxsize=audio_skip)

    def __getstate__(self):
        state = self.__dict__.copy()
        if 'proc' in state:
            del state["proc"]
        return state

    def proc_main(self):
        print('TwitchRecoder: TwitchImageGrabber init')
        if 'youtube' in self.url:
            image_grabber = YoutubeImageRecoder(
                url=self.url,
                quality=self.quality,
                rate=self.fps,
            )
        else:
            image_grabber = TwitchImageGrabber(
                twitch_url=self.url,
                quality=self.quality,  # quality of the stream could be ["160p", "360p", "480p", "720p", "720p60", "1080p", "1080p60"]
                blocking=True,
                rate=self.fps  # frame per rate (fps)
            )

        # change to a stream that is actually online
        print('TwitchRecoder: TwitchAudioGrabber init')
        audio_grabber = TwitchAudioGrabber(
            twitch_url=self.url,
            blocking=True,  # wait until a segment is available
            segment_length=int(self.batch_sec),  # segment length in seconds
            rate=44100,  # sampling rate of the audio
            channels=2,  # number of channels
            dtype=np.float32  # quality of the audio could be [np.int16, np.int32, np.float32, np.float64]
        )

        t = time.time()
        t_sum = []
        index = 0
        while True:
            try:
                cmd = self.cmd_queue.get_nowait()
                if cmd == 'exit':
                    print('TwitchRecoder: Get exit')
                    self.cmd_queue.close()
                    break
                else: raise Exception()
            except Empty:
                pass

            #print('ff')
            frames = []
            reader_eof = False
            for i in range(self.batch_sec * self.fps):
                frame = image_grabber.grab()
                if frame is None:
                    print('frame recoded none EOF')
                    reader_eof = True
                    break
                    #raise Exception('frame recodered None!')
                # print(f'grabbed {self.frame_count}, {frame[0,0,0]}')
                if self.output_shape is not None:
                    frame = cv2.resize(frame, dsize=[self.output_shape[1], self.output_shape[0]], interpolation=cv2.INTER_AREA)
                    frame = cv2.putText(frame, f"Received: {self.frame_count} frames", (10, 32), cv2.FONT_HERSHEY_PLAIN, 0.5, (255,0,0), 1)
                    self.frame_count += 1
                frames.append(frame)

            if reader_eof:
                entry = RecoderEntry(
                    index=index,
                    audio_segment=None, #(22000,2)
                    frames=None, #(24, 1080, 1920,3) -> (24, 2160, 3840, 3)
                    fps=self.fps,
                    profiler=Profiler()
                )
                entry.profiler.start('recoder.output')
                if self.on_queue is not None:
                    self.on_queue(entry)
                else:
                    try:
                        self.queue.put_nowait(entry)
                    except queue.Full:
                        print(f'TwitchRecoder: output queue is full. Is consumer too slow?')
                break

            if len(frames) == 0:
                print(f'TwitchRecoder: frame does not recorded...')
                continue

            #print('f')
            audio_segment = audio_grabber.grab()
            if self.audio_skip > 0:
                while self.audio_queue.qsize() < self.audio_skip:
                    self.audio_queue.put(audio_segment.copy())
                audio_segment = self.audio_queue.get()

            frames = np.stack(frames, axis=0)
            t_sum.append(time.time()-t)
            if len(t_sum) > 100:
                t_sum.pop(0)
            t_avg = sum(t_sum)/len(t_sum)
            print(f'TwitchRecoder: batch[{index}] captured took average {t_avg:.2f} sec. Audio[{audio_segment.shape}] Video[{frames.shape}]')
            t = time.time()
            entry = RecoderEntry(
                index=index,
                audio_segment=audio_segment, #(22000,2)
                frames=frames, #(24, 1080, 1920,3) -> (24, 2160, 3840, 3)
                fps=self.fps,
                profiler=Profiler()
            )
            entry.profiler.start('recoder.output')
            if self.on_queue is not None:
                self.on_queue(entry)
            else:
                try:
                    self.queue.put_nowait(entry)
                except queue.Full:
                    print(f'TwitchRecoder: output queue is full. Is consumer too slow?')
            index += 1

        print('TwitchRecoder: try term img')
        image_grabber.terminate()
        print('TwitchRecoder: try term audio')
        audio_grabber.terminate()
        print('TwitchRecoder: exit subproc')

        os.kill(os.getpid(), 9)

    def start(self):
        self.proc = mp.Process(target=self.proc_main, daemon=True)
        self.proc.start()

    def get(self) -> RecoderEntry:
        return self.queue.get()

    def stop(self):
        self.cmd_queue.put("exit")
        self.queue.close()
        print('TwitchRecoder: joining all subprocs')
        self.join()
        print('TwitchRecoder: joined subprocs')

    def join(self):
        self.proc.join()

if __name__ == '__main__':
    print('asdf')
    recoder = TwitchRecoder(target_url=TW_MAOU, quality='1080p60')
    recoder.start()

    time.sleep(3)

    if not os.path.exists('./saves/frames/'): os.mkdir('./saves/frames/')
    j = 0
    for i in range(10):
        batch = recoder.queue.get(timeout=30) #type: RecoderEntry
        for k in range(batch.frames.shape[0]):
            cv2.imwrite(f"saves/frames/{j:04}.png", cv2.cvtColor(batch.frames[k], cv2.COLOR_RGB2BGR))
            j += 1
        print(f"{i} batch get. {batch.frames.shape}")

    recoder.stop()


from __future__ import print_function
import cv2 as cv
import matplotlib.pyplot as plt

if __name__ == "__main__":
    img = cv.imread('images/dog1.jpeg')
    gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    dst = cv.equalizeHist(gray_img)
    gray_hst = cv.calcHist([gray_img], [0], None, [256], [0, 256])
    gray_hst_dst = cv.calcHist([dst], [0], None, [256], [0, 256])
    cv.imshow('Source image', img)
    cv.imshow('Source gray image', dst)
    cv.imshow('Equalized Image', gray_img)

    plt.figure()
    plt.title("GrayScale Histogram")
    plt.xlabel('Bins')
    plt.ylabel('# of pixels')
    plt.plot(gray_hst_dst)
    plt.xlim([0, 255])
    plt.show()
    cv.waitKey()


from django.shortcuts import render, redirect
from kajaki_app.models import Route, Kayak, Order, OrderKayak
from django.urls import reverse, reverse_lazy
from datetime import date
from django.views import View
from kajaki_app.forms import AddKayakForm, AddRouteForm, ContactForm
from django.views.generic import ListView, CreateView, UpdateView, DetailView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin, PermissionRequiredMixin

class AddRouteView(View):
    # permission_required = ['kajaki_app.add_route']

    def get(self, request):
        form = AddRouteForm()
        return render(request, 'kajaki_app/add_route.html', {'form': form, 'submit_value_text': 'Dodaj'})

    def post(self, request):
        form = AddRouteForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse('add_route'))
        return render(request, 'kajaki_app/add_route.html', {'form': form, 'submit_value_text': 'Dodaj'})

class RouteListView(ListView):
    model = Route
    template_name = 'kajaki_app/route_list.html'

class AddKayakView(View):
    # permission_required = ['kajaki_app.add_kayak']

    def get(self, request):
        form = AddKayakForm()
        return render(request, 'kajaki_app/add_kayak.html', {'form': form, 'submit_value_text': 'Dodaj'})

    def post(self, request):
        form = AddKayakForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse('add_kayak'))
        return render(request, 'kajaki_app/add_kayak.html', {'form': form, 'submit_value_text': 'Dodaj'})

class KayakListView(ListView):
    model = Kayak
    template_name = 'kajaki_app/kayak_list.html'

class KayakUpdateView(LoginRequiredMixin, UpdateView):
    # permission_required = ['filmy.change_film']

    model = Kayak
    template_name = 'kajaki_app/add_kayak.html'
    fields = '__all__'

    def get_success_url(self):
        super().get_success_url()
        return reverse("add_kayak", args=(self.object.id,))

class KayakDeleteView(LoginRequiredMixin, DeleteView):
    model = Kayak
    template_name = 'kajaki_app/kayak_delete.html'
    success_url = reverse_lazy('kayak_list')

class KayakDetailView(DetailView):
    model = Kayak
    template_name = 'kajaki_app/details_kayak.html'

class CheckoutView(View):
    def get(self, request):
        return render(request, 'kajaki_app/checkout.html')

    def post(self, request):
        name = request.POST.get('name', '')
        email = request.POST.get('email', '')
        date = request.POST.get('date', '')
        phone = request.POST.get('phone', '')

        return render(request, 'kajaki_app/checkout.html')

class OrderView(LoginRequiredMixin, View):
    def get(self, request):
        routes = Route.objects.all()
        kayaks = Kayak.objects.all()
        return render(request, 'kajaki_app/order.html', {'kayaks': kayaks, 'routes': routes})

    def post(self, request):
        user = request.user
        route = request.POST.get('route')
        date = request.POST.get('date')
        kayak = request.POST.get('kayak')
        amount = request.POST.get('amount')
        if route and date and int(amount) >= 1 and kayak:
            route = Route.objects.get(name=route)
            order = Order.objects.create(route=route, buyer=user, date=date)
            kayak = Kayak.objects.get(name=kayak)
            order_kayak = OrderKayak.objects.create(kayak=kayak, order=order, amount=amount)
            return redirect(reverse('my_account'))
        return render(request, 'kajaki_app/order.html', {'message': 'Wypełnij poprawnie wszystkie pola'})

class ContactView(View):
    def get(self, request):
        form = ContactForm()
        return render(request, 'kajaki_app/contact.html', {'form': form, 'submit_value_text': 'Wyślij'})

    def post(self, request):
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse('index'))
        return render(request, 'kajaki_app/contact.html', {'form': form, 'submit_value_text': 'Wyślij'})

class AboutUsView(View):
    def get(self, request):
        return render(request, 'kajaki_app/about_us.html')


#%% [markdown]
# We need to create bar charts for our fancy plot to show the fraction of stuff from
# each region. We'll do that here.

#%%
group_ids_to_plot = [0, 431, 88, 299, 9]

#%%
from ltcaesar import read_data_from_file
import numpy as np
import matplotlib.pyplot as plt

#%%
# Setup our favourite stylesheet
plt.style.use("mnras_flatiron")

#%%
directory = "s50j7kAHF"
data = read_data_from_file(f"{directory}/lt/lt_outputs.hdf5")

#%% [markdown]
# We now need to calculate three things for each halo:
# + The fraction of baryonic mass from outside the halo
# + The fraction of baryonic mass from other halos
# + The fraction of baryonic mass from our own LR

#%%
def grab(x):
    return getattr(data, f"gas_{x}") + getattr(data, f"stellar_{x}")

baryonic_mass = grab("mass_in_halo")
from_outside = grab("mass_in_halo_from_outside_lagrangian")
from_other = grab("mass_in_halo_from_other_lagrangian")
from_own = grab("mass_in_halo_from_lagrangian")

#%%
ratio_own = from_own / baryonic_mass
ratio_other = from_other / baryonic_mass
ratio_outside = from_outside / baryonic_mass

#%%
import os

os.mkdir("barcharts")

#%%
# Actually make the bar charts one by one
for id in group_ids_to_plot:
    fig, a = plt.subplots(figsize=(1, 1))

    a.bar(
        [0.5, 1.5, 2.5],
        [ratio_own[id], ratio_other[id], ratio_outside[id]],
        width=1.0,
        color=["C0", "C1", "C2"],
    )

    a.set_xticks([0.5, 1.5, 2.5])
    a.set_xticklabels(["Own", "Other", "Outside"])
    a.set_xlim(0, 3)
    a.set_ylim(0, 0.8)

    fig.tight_layout()

    fig.savefig("barcharts/group_{}.pdf".format(id))

#%%


from django.shortcuts import render, redirect
from django.http import Http404, HttpResponseRedirect
from django.urls import reverse
from .models import Article, Category, ArticleCategoryRelation
from django.utils import timezone
from .forms import UserRegistrationForm
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.views import View
from django.views.generic import ListView
import json
from django.http import JsonResponse
from django.template.loader import render_to_string

items_on_page = 3

def index(request):
    article_list = Article.objects.all()[:items_on_page]
    categories = Category.objects.all()
    return render(request, 'articles/main.html', {'all_articles': article_list,
                                                  'categories': categories,
                                                  'is_admin': request.user.is_staff, })

class ArticleListView(ListView):

    def get(self, request, **kwargs):
        user = request.user
        if user.groups.filter(name='admin').count():
            is_admin = True
        article_list = Article.objects.all()
        current_page = Paginator(article_list, items_on_page)
        page = request.GET.get('page')
        try:
            page_articles = current_page.page(page)
        except:
            page_articles = current_page.page(1)

        data = json.dumps(list(Article.objects.values_list('id', 'title')))
        categories = Category.objects.all()
        return render(request, 'articles/list.html',
                      {'all_articles': page_articles, 'is_admin': request.user.is_staff, 'qs_json': data,
                       'categories': categories, })

def create_article(request):
    if not request.user.is_staff:
        raise Http404('Доступ запрещен!')

    if request.method == 'POST':
        try:
            category_choices = [x for x in request.POST.getlist('category')]
            category_list = [Category.objects.get(id=category_id) for category_id in category_choices]
        except:
            raise Http404('Категория не найдена!')
        request.user.article_set.create(title=request.POST['title'], text=request.POST['text'], date=timezone.now())
        current_article = Article.objects.all()[0]
        for category in category_list:
            category.includes_article.add(current_article)
        return redirect('/')
    category_list = Category.objects.all()
    return render(request, 'articles/create.html', {'category_list': category_list})

def update_article(request, article_id):
    if not request.user.is_staff:
        raise Http404('Доступ запрещен!')

    current_article = Article.objects.get(id=article_id)
    if not current_article:
        raise Http404('Статья не найдена!')

    if request.method == 'POST':

        # try:
        #     category_choices = [x for x in request.POST.getlist('category')]
        #     category_list = [Category.objects.get(id=category_id) for category_id in category_choices]
        # except:
        #     raise Http404('Категория не найдена!')

        current_article.title=request.POST['title']
        current_article.text=request.POST['text']
        current_article.save()
        # ArticleCategoryRelation.objects.filter(article=current_article).delete()
        #
        # for category in category_list:
        #     category.includes_article.add(current_article)

        return redirect('/')

    category_list = Category.objects.all()
    category_of_article = ArticleCategoryRelation.objects.filter(article=current_article)

    return render(request, 'articles/update.html', {'category_list': category_list,
                                                    'article': current_article,
                                                    'article_category': category_of_article})

def leave_comment(request, article_id):
    try:
        article = Article.objects.get(id=article_id)
    except:
        raise Http404('Статья не найдена!')
    article.comment_set.create(author=request.user, text=request.POST['text'], date=timezone.now())
    return HttpResponseRedirect(reverse('newnotes:view_article', args=(article.id,)))

def profile(request):
    if request.user.is_anonymous:
        raise Http404('Доступ запрещен!')

    categories = Category.objects.all()
    return render(request, 'account/profile.html', {'categories': categories, })

def register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return render(request, 'registration/register_done.html', {'new_user': new_user})
        else:
            print(form.errors.as_data())
    else:
        form = UserRegistrationForm()
    return render(request, 'registration/register.html', {'form': form})

def delete_article(request, article_id):
    if not request.user.is_staff:
        raise Http404('Доступ запрещен!')
    try:
        article = Article.objects.get(id=article_id)
    except:
        raise Http404('Статья не найдена!')
    if request.method == "POST":
        article.delete()
        return redirect('/')
    return render(request, 'articles/delete.html', {'article': article})

def create_category(request):
    if not request.user.is_staff:
        raise Http404('Доступ запрещен!')
    if request.method == 'POST':
        Category.objects.create(name=request.POST['name'])
        return redirect('/')
    category_list = Category.objects.all()
    return render(request, 'categories/create.html', {'category_list': category_list, })

def delete_category(request, category_id):
    if not request.user.is_staff:
        raise Http404('Доступ запрещен!')
    try:
        category = Category.objects.get(id=category_id)
    except:
        raise Http404('Категория не найдена!')
    if request.method == "POST":
        category.delete()
        return redirect('/')
    category_list = Category.objects.all()
    return render(request, 'categories/delete.html', {'category': category, 'category_list': category_list, })

def update_category(request, category_id):
    if not request.user.is_staff:
        raise Http404('Доступ запрещен!')
    try:
        category = Category.objects.get(id=category_id)
    except:
        raise Http404('Категория не найдена!')

    if request.method == 'POST':
        Category.objects.filter(id=category_id).update(name=request.POST['name'])
        return redirect('/')
    category_list = Category.objects.all()
    return render(request, 'categories/update.html', {'category': category, 'category_list': category_list, })

class ListCategoryArticles(ListView):

    def get(self, request, category_id, **kwargs):
        rel_category_article = ArticleCategoryRelation.objects.filter(category=category_id).order_by('-id')
        category = Category.objects.all().get(id=category_id)
        article_list = [Article.objects.get(id=x.article.id) for x in rel_category_article]
        current_page = Paginator(article_list, items_on_page)
        page = request.GET.get('page')
        try:
            context = current_page.page(page)
        except:
            context = current_page.page(1)

        data = json.dumps(list(Article.objects.values_list('id', 'title')))
        categories = Category.objects.all()
        return render(request, 'categories/list.html',
                      {'all_articles': context, 'is_admin': request.user.is_staff, 'qs_json': data,
                       'categories': categories,
                       'category': category, })

def get_paginated_page(request, objects, number=items_on_page):
    current_page = Paginator(objects, number)
    page = request.GET.get('page') if request.method == 'GET' else request.POST.get('page')
    try:
        return current_page.page(page)
    except PageNotAnInteger:
        return current_page.page(1)
    except EmptyPage:
        return current_page.page(current_page.num_pages)

def is_ajax(request):
    return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

class ViewArticle(View):

    def get(self, request, article_id):
        try:
            article = Article.objects.get(id=article_id)
        except:
            raise Http404('Статья не найдена!')
        list_comments = article.comment_set.order_by('-id')
        if not request.user.is_anonymous:
            article.readers.add(request.user)
        watched = article.readers.count()
        categories = Category.objects.all()
        return render(request, 'articles/view.html',
                      {'article': article, 'list_comments': get_paginated_page(request, list_comments),
                       'watched': watched, 'categories': categories, })

    def post(self, request, article_id):
        if is_ajax(request):
            try:
                article = Article.objects.get(id=article_id)
            except:
                raise Http404('Статья не найдена!')
            return JsonResponse({
                "result": True,
                "comms": render_to_string(
                    request=request,
                    template_name='articles/comms.html',
                    context={'list_comments': get_paginated_page(request, article.comment_set.order_by('-id'))}
                )
            })
        else:
            raise Http404()


import sys
import typing as t
import importlib
from pathlib import Path

import pkg_resources

from starwhale.utils import console
from starwhale.utils.venv import (
    guess_current_py_env,
    get_user_python_sys_paths,
    check_python_interpreter_consistency,
)

def import_object(
    workdir: t.Union[Path, str], handler_path: str, py_env: str = ""
) -> t.Any:
    workdir_path = str(Path(workdir).absolute())
    external_paths = [workdir_path]
    py_env = py_env or guess_current_py_env()
    _ok, _cur_py, _ex_py = check_python_interpreter_consistency(py_env)
    if not _ok:
        console.print(
            f":speaking_head: [red]swcli python prefix:{_cur_py}, runtime env python prefix:{_ex_py}[/], swcli will inject sys.path"
        )
        external_paths.extend(get_user_python_sys_paths(py_env))

    prev_paths = sys.path[:]
    sys_changed = False

    for _path in external_paths[::-1]:
        if _path not in sys.path:
            sys.path.insert(0, _path)
            pkg_resources.working_set.add_entry(_path)
            sys_changed = True

    try:
        module_name, handler_name = handler_path.split(":", 1)
        console.print(
            f":speaking_head: [green]import module:{module_name}, handler:{handler_name}[/]"
        )
        _module = importlib.import_module(module_name, package=workdir_path)
        _obj = getattr(_module, handler_name, None)

        if not _obj:
            raise ModuleNotFoundError(f"{handler_path}")
    except Exception:
        console.print_exception()
        if sys_changed:
            sys.path[:] = prev_paths
        raise

    return _obj

def load_module(module: str, path: Path) -> t.Any:
    workdir_path = str(path.absolute())

    external_paths = [workdir_path]
    for _path in external_paths[::-1]:
        if _path not in sys.path:
            sys.path.insert(0, _path)
            pkg_resources.working_set.add_entry(_path)

    return importlib.import_module(module, package=workdir_path)


import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import dash
from app import app
from app import server

from apps.gas_monitoring import gas_app

navBar = dbc.NavbarSimple(
    children=[

        dbc.NavItem(dbc.NavLink("Home", href="/")),
        dbc.NavItem(dbc.NavLink("Gas-system", href="/gas-monitoring")),
        dbc.NavItem(dbc.NavLink("Oil-system", href="/oil-monitoring")),

    ],
    brand="G.O.M",
    brand_href="/",
    color="primary",
    dark=True,
)
app.layout = html.Div(
    [
        dcc.Location(id='url', refresh=False),
        navBar,
        html.Div(
        id='content',

       )
    ]
)

error_page = html.Div([
        html.H1("404",style={"textAlign":"center"}),
        html.H3("Page Not Found!",style={"textAlign":"center"})
    ])

index_page = html.Div(
    [
        html.Div([

            html.H2("Welcome to Gas Oil plant monitoring System."),
            html.P("""Lorem ipsum dolor sit amet ac maximusrdiet convallis. Duis rutrum neque consectetur mauris tempor laoreet. Vestibulum quis nulla eu orci efficitur varrisque vel nibh. Integer eu velit eget ex consectetur consectetur sit amet vitae lectus. Mauris egestas purus et mi pulvinar, a posuere justo convallis. Nunc nec laoreet lectus. Mauris purus est, bibendum hendrerit fermentum quis, porttitor at massa.""")
        ],
        style={
            'text-align': 'center',
            'position': 'absolute',
            'top': '50%',
            'left': '50%',
            'transform': 'translate(-50%, -50%)',
            'color': 'white',

        })

    ],

    style={"textAlign":"center",
            'backgroundImage': 'url("assets/images/background.jpg")',
            'backgroundRepeat': 'no-repeat',
            'backgroundPosition': 'center',
            'backgroundSize' : 'cover',
            'height':'50vh',
            'position':'relative',
            },
    )

@app.callback(dash.dependencies.Output('content', 'children'),
              [dash.dependencies.Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/gas-monitoring':
        return gas_app.layout
    elif pathname == "/oil-monitoring":
        return gas_app.layout
    elif pathname == '/':
        return index_page
    else:
        return error_page

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


import pandas as pd
import argparse
from gtfparse import read_gtf

parser = argparse.ArgumentParser()
parser.add_argument('--phenotype', type=str, required=True)
# parser.add_argument('--ncRNA', type=str, required=True)

if __name__ == '__main__':
    args = parser.parse_args()
    phenotype = args.phenotype

    gtf = read_gtf('ReferenceGenome/Annotations/gencode.v34.chromasomal.annotation.gtf')

    ncRNA_genes = gtf.loc[(gtf.gene_type.isin(['snoRNA', 'snRNA',
                                               'lncRNA', 'unprocessed_pseudogene',
                                               'transcribed_unprocessed_pseudogene',
                                               'pseudogene', 'rRNA_pseudogene',
                                               'transcribed_processed_pseudogene',
                                               'transcribed_unitary_pseudogene',
                                               'transcribed_unprocessed_pseudogene',
                                               'translated_processed_pseudogene',
                                               'translated_unprocessed_pseudogene',
                                               'unprocessed_pseudogene'
                                              ])) & (gtf.feature == 'gene')].gene_id

    counts = pd.read_csv('featureCounts/{phenotype}/Counts.txt'.format(phenotype=phenotype),
                                 sep='\t', skiprows=1, index_col=0)

    ncRNA_counts = counts.loc[ncRNA_genes]

    ncRNA_counts.to_csv('featureCounts/{phenotype}_annotated_ncRNA/Counts.txt'.format(phenotype=phenotype),
                        sep='\t', index=True, header=True)


# -*- coding: utf-8 -*-
import psycopg2
# the module that connects to the database

"""

The task is to create a reporting tool that prints out reports (in plain text)
based on the data in the database.
1.What are the most popular three articles of all time?
Which articles have been accessed the most? Present this information as a sorted list
with the most popular article at the top.
2.Who are the most popular article authors of all time?
That is, when you sum up all of the articles each author has written,
which authors get the most page views? Present this as a sorted list with the most
popular author at the top.
3.On which days did more than 1% of requests lead to errors?
The log table includes a column status that indicates the HTTP status code that
the news site sent to the user's browser. (Refer back to this lesson
if you want to review the idea of HTTP status codes.)

"""
DBNAME = "news"

# Open and connect to database; Run the query; Return database cursor objects
def query(user_query):
	DB = psycopg2.connect(database = DBNAME)
	cursor = DB.cursor()
	cursor.execute(user_query)
	result = cursor.fetchall()
	DB.close()
	return result

# 1. popular article
def pop_article():
	top_article = query("select title, count(*) from articles "
              "join log on path like CONCAT('%',slug) group by title "
              "order by count(*) desc limit 3")
	print("The most popular three articles are:")
	for title, views in top_article:
		print(" \"{}\" -- {} views".format(title, views))
# 2. popular author
def pop_author():
	top_authors = query("select name, count(path) from authors "
            "join articles on authors.id = author join log "
            "on path like CONCAT('%', slug) group by name order by count(path) desc limit 4")
	print('The most popular authors are:')
	for name, views in top_authors:
		print(" {} -- {} views".format(name, views))

# 3. error
def error_day():
    errorday = query("select date, avg from ("
            "select date, (sum(error) / (select count(*) "
            "from log where (time::date) = date)) as avg "
            "from (select (time::date) as date, count(*) as error "
            "from log where status like '4%' group by date) "
            "as error_percentage group by date order by avg  desc) as final "
            "where avg >= .01")
    print('Days with more than 1%  of requests lead to errors')
    for res in errorday:
        print (str(res[0]) + " — " + str(round((res[1]*100), 2)) +
               '%')

if __name__ == '__main__':
	pop_article()
	pop_author()
	error_day()


#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Custom filters for use in openshift_aws
'''

from ansible import errors

class FilterModule(object):
    ''' Custom ansible filters for use by openshift_aws role'''

    @staticmethod
    def scale_groups_serial(scale_group_info, upgrade=False):
        ''' This function will determine what the deployment serial should be and return it

          Search through the tags and find the deployment_serial tag. Once found,
          determine if an increment is needed during an upgrade.
          if upgrade is true then increment the serial and return it
          else return the serial
        '''
        if scale_group_info == []:
            return 1

        scale_group_info = scale_group_info[0]

        if not isinstance(scale_group_info, dict):
            raise errors.AnsibleFilterError("|filter plugin failed: Expected scale_group_info to be a dict")

        serial = None

        for tag in scale_group_info['tags']:
            if tag['key'] == 'deployment_serial':
                serial = int(tag['value'])
                if upgrade:
                    serial += 1
                break
        else:
            raise errors.AnsibleFilterError("|filter plugin failed: deployment_serial tag was not found")

        return serial

    @staticmethod
    def scale_groups_match_capacity(scale_group_info):
        ''' This function will verify that the scale group instance count matches
            the scale group desired capacity

        '''
        for scale_group in scale_group_info:
            if scale_group['desired_capacity'] != len(scale_group['instances']):
                return False

        return True

    @staticmethod
    def build_instance_tags(clusterid):
        ''' This function will return a dictionary of the instance tags.

            The main desire to have this inside of a filter_plugin is that we
            need to build the following key.

            {"kubernetes.io/cluster/{{ openshift_aws_clusterid }}": "{{ openshift_aws_clusterid}}"}

        '''
        tags = {'clusterid': clusterid,
                'kubernetes.io/cluster/{}'.format(clusterid): clusterid}

        return tags

    def filters(self):
        ''' returns a mapping of filters to methods '''
        return {'build_instance_tags': self.build_instance_tags,
                'scale_groups_match_capacity': self.scale_groups_match_capacity,
                'scale_groups_serial': self.scale_groups_serial}


from Bio import SeqIO
import sys

def readin_fasta(input_file, batch_size):
    """Read fasta file with a fast, memory-efficient generator."""
    title_list = []
    seq_list = []
    seq_num = len([1 for line in open(input_file) if line.startswith(">")])

    for i, seq_record in enumerate(SeqIO.FastaIO.SimpleFastaParser(open(input_file)),1):
        title, seq = seq_record
        title_list.append(title)
        seq_list.append(seq)
        if i%batch_size == 0:
            yield title_list, seq_list
            title_list = []
            seq_list = []
        if i == seq_num:
            print('Converted {} of {} fragments'.format(i, seq_num))
            yield title_list, seq_list


#!/usr/bin/python3

# USE THIS WHEN IN NOTEBOOK -> %python

# CHANGE ACCORDINGLY: the field XXX

import sys
import time

from azure.identity import ClientSecretCredential
from azure.storage.filedatalake import DataLakeServiceClient,FileSystemClient

ACCOUNT_NAME = "XXX"
FILE_SYSTEM = "XXX"
TARGET_DIR = "XXX"

def set_permission(path,acl):
    # Directories and files need to be handled differently
    if path.is_directory:
        directory_client = filesystem.get_directory_client(directory=path.name)
        resp = directory_client.set_access_control(acl=acl)
        print(f'\tApplied Directory ACL to {path.name}')
    else:
        file_client = filesystem.get_file_client(path.name)
        # Need to remove "Default" ACL segments from ACL string because that can't be applied to files
        resp = file_client.set_access_control(acl=acl[:acl.find('default')-1])
        print(f'\tApplied File ACL to {path.name}')
    return resp

def main(target_dir,filesystem):

    # Get the target directory, subdirectories and permissions
    paths = filesystem.get_paths(path=target_dir)
    directory_client = filesystem.get_directory_client(directory=target_dir)
    acl = directory_client.get_access_control()
    target_acl_dir = acl['acl']

    for path in paths:
      set_permission(path,target_acl_dir)

if __name__ == '__main__':

    # Clients
    credential = "XXX" # the master account key.
    service = DataLakeServiceClient(account_url=f'https://{ACCOUNT_NAME}.dfs.core.windows.net/', credential=credential)
    filesystem = service.get_file_system_client(file_system=FILE_SYSTEM)

    print('*'*20)
    print(f'Storage Account Name: {ACCOUNT_NAME}')
    print(f'File System Name: {FILE_SYSTEM}')
    print('*'*20)
    print(f'Running: Setting ACLs for all child paths (subdirectories and files) in TARGET_DIR to match parent.')
    total_start = time.time() # Start Timing
    main(TARGET_DIR,filesystem)
    total_end = time.time() # End Timing
    print("Complete: Recursive ACL configuration took {} seconds.".format(str(round(total_end - total_start,2))))


"""Config file and logging related utility functions."""
import configparser
import json
import os
import sys
from pprint import pprint
import yaml

def read_cfg(location, verbose=True):
    """
    Read config file at location using ConfigParser.

    Parameters
    ----------
    location : str
        Where the config file is located
    verbose : bool, optional, defaults to True
        Should print the contents of the read config file.

    Returns
    -------
    ConfigParser
        The python ConfigParser object after reading the cfg.

    """
    if not os.path.exists(location):
        raise ValueError(f"Config file {location} does not exist")

    config = configparser.ConfigParser()
    config.read(location)

    if verbose:
        print_cfg(config, "Program started with configuration")
    return config

def print_cfg(config, msg=""):
    """
    Print the contents of a ConfigParser object.

    Parameters
    ----------
    config : ConfigParser
        The ConfigParser to print the contents of.
    msg: str, optional, defaults to ""
        Message to print before printing the config file.

    Returns
    -------
    None

    """
    if msg != "":
        print(msg)
    config_dict = [{x: tuple(config.items(x))} for x in config.sections()]
    pprint(config_dict, width=120)

def parse_args(parser, verbose=True):
    """
    Parse command line arguments into a Namespace.

    Parameters
    ----------
    verbose : bool, optional, defaults to True
        Should print the values of the command line args.

    Returns
    -------
    Namespace
        Parsed arguments.

    Raises
    ------
    ValueError
        If any arguments are passed which are not used in program.

    """
    args, unparsed = parser.parse_known_args()

    if len(unparsed) != 0:
        raise ValueError(
            "Unrecognised command line arguments passed {}".format(unparsed)
        )

    if verbose:
        if len(sys.argv) > 1:
            print("Command line arguments", args)
    return args

def read_python(path, dirname_replacement=""):
    """
    Execute a python script at path.

    The script is expected to have items visible at global scope,
    which are stored as metadata.

    Note
    ----
    The string "__thisdirname__" is magic and will be replaced by the
    absolute path to the directory containing the script.
    The string "__dirname__" is also magic and will be replaced by
    the value of dirname_replacement.

    Parameters
    ----------
    path : string
        The location of the python script.
    dirname_replacement : string, optional, optional, defaults to None
        What to replace __dirname__ with.
        By default, None will replace __dirname__ with dirname of path.

    Returns
    -------
    dict
        The scripts global scope variables stored in a dictionary.

    """

    def normalise_path(pth):
        s = os.path.abspath(pth)
        s = s.replace(os.sep, "/")
        return s

    path = os.path.realpath(os.path.expanduser(path))
    if not os.path.exists(path):
        raise ValueError("{} does not exist to read".format(path))
    with open(path, "r") as f:
        contents = f.read()
    if dirname_replacement != "":
        contents = contents.replace("__dirname__", normalise_path(dirname_replacement))
    else:
        contents = contents.replace(
            "__dirname__", normalise_path(os.path.dirname(path))
        )
    contents = contents.replace(
        "__thisdirname__", normalise_path(os.path.dirname(path))
    )
    metadata = {}
    try:
        exec(contents, {}, metadata)
    except Exception as e:
        import traceback

        print("QUITTING: An error occurred reading {}".format(path))
        traceback.print_exc()
        exit(-1)
    metadata = {k.lower(): v for (k, v) in metadata.items()}
    return metadata

def read_yaml(path):
    with open(path, "r") as stream:
        parsed_yaml = yaml.safe_load(stream)
    return parsed_yaml

def read_json(path):
    with open(path, "r") as stream:
        parsed_json = json.load(stream)
    return parsed_json

def split_dict(in_dict, index):
    """
    Grab the value at index from each list in the dictionary.

    Parameters
    ----------
    in_dict : dict
        The dictionary to grab from
    index : int
        The index in the lists to pull from

    Returns
    -------
    dict
        The original dictionary but with index values pulled out.

    """
    new_dict = {}
    for key, value in in_dict.items():
        if isinstance(value, list):
            new_dict[key] = value[index]
    return new_dict

def convert_dict_to_string(in_dict, name):
    """
    Convert the underlying parameters dictionary to string.

    Can be useful for printing or writing to a file.
    Does not overwrite default __str__ as the output is quite verbose.

    Parameters
    ----------
    in_dict : dict
        Input dictionary

    Returns
    -------
    str
        The string representation of the dict.

    """

    def _val_to_str(val):
        """
        Convert a value to a string.

        One caveat, if a string is passed, it returns
        the original string wrapped in quotes.

        Parameters
        ----------
        val : object
            The value to convert

        Returns
        -------
        str
            The value as a string.

        """
        return f"'{val}'" if isinstance(val, str) else val

    out_str = ""
    out_str += name + " = {\n"
    for k, v in in_dict.items():
        out_str += f"\t{_val_to_str(str(k))}:"
        if isinstance(v, dict):
            out_str += "\n\t\t{\n"
            for k2, v2 in v.items():
                out_str += "\t\t {}: {},\n".format(
                    _val_to_str(str(k2)), _val_to_str(v2)
                )
            out_str += "\t\t},\n"
        else:
            out_str += f" {_val_to_str(v)},\n"
    out_str += "\t}"
    return out_str


import dlib
from imutils import face_utils
dlib_path = "dlibb/shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(dlib_path)
import argparse
import pickle
import cv2
import os
import mpmath
import numpy as np

# face_classifier =  cv2.CascadeClassifier('harcascades/haarcascade_frontalface_default.xml')
src_path = ("O:\\Nama_College\\FYP\\MY_FYP_CODE\\MY_FYP_CODE\\MY_CODE\\TESTING_DATASET\\")
predict = []
features_vector = []
pickle_in = open("O:\\Nama_College\\FYP\\MY_FYP_CODE\\MY_FYP_CODE\\MY_CODE\\dlib_normalized.pickle","rb")
# pickle_in = open("O:\\Nama_College\\FYP\\MY_FYP_CODE\\MY_FYP_CODE\\MY_CODE\\dlib_normalized_full.pickle","rb")
model = pickle.load(pickle_in)
cap = cv2.VideoCapture(0)
B= 0
while (True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    B += 1
    if B % 5 == 0:
        print(B)
        face = detector(gray,0)
        for (J, rect) in enumerate(face):
            shap = predictor(gray, rect)
            xlist = []
            ylist = []
            shap = face_utils.shape_to_np(shap)
            Centre = (shap[30])
            centre_x = Centre[0]
            centre_y = Centre[1]
            shap =  shap[18:68]
            for i in shap:
                xlist.append(i[0])
                ylist.append(i[1])
            forx = []
            fory = []
            for x in xlist:
                forx.append((x - centre_x) ** 2)
            for y in ylist:
                fory.append((y - centre_y) ** 2)
            listsum = [sum(x) for x in zip(forx, fory)]
            features = []
            for i in listsum:
                k = mpmath.sqrt(float(i))
                features.append(float(k))
            maxx = (max(features))
            final = []
            for i in features:
                if (i == 0.0):
                    continue
                F = i / maxx
                final.append(F)
            # print(final)
            numpy_array = np.array(final)
            prediction = model.predict([numpy_array])[0]

            # predict.append(prediction)
            (x, y, w, h) = face_utils.rect_to_bb(rect)
            cv2.rectangle(frame, (x, y), (x + w, y + h),(0, 255, 0), 2)
        # display the image and the prediction
        #     cv2.putText(frame, "FACE ({})".format(J+ 1) + " " + prediction, (x , y ), cv2.FONT_HERSHEY_COMPLEX, 0.5,
        #             (0, 255, 0), 2)
            cv2.putText(frame, prediction, (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5,
                        (0, 255, 0), 2)
        # cv2.putText(tasveer,  prediction, (x-5 , y-5 ), cv2.FONT_HERSHEY_COMPLEX, 1.2,
        #             (0, 0, 255),4)
            print(prediction)
            cv2.circle(frame, (centre_x, centre_y), 1, (0, 0, 0), 5)
            for (x, y) in shap:
                cv2.circle(frame, (x, y), 1, (0, 0, 255), 2)

                cv2.imshow("Image", frame)
                cv2.waitKey(1)
                if k == 'q':
                    break
cap.release()
cv2.destroyAllWindows()


#!/usr/bin/env python
#encoding=utf8

from json import dumps

def get_node(tree, name):
    if tree.label == name:
        return True, [tree.label]
    if not tree.children:
        return False, None
    for child in tree.children:
        found, addr = get_node(child, name)
        if found:
            return True, [tree.label] + addr
    return False, None

def goto_node(tree, desc):
    assert tree.label == desc[0]
    node = tree
    for name in desc[1:]:
        nodes = [n for n in node.children if n.label == name]
        if not nodes:
            return False, None
        node = nodes[0]
    return True, node

def recreate_node(orig, desc):
    """Recreate item in orig under desc."""
    tree = Tree(desc[-1], [])
    success, node = goto_node(orig, desc)
    if not node.children:
        return tree

    for child in node.children:
        success, _ = goto_node(orig, desc + [child.label])
        if not success:
            child_node = Tree(child.label, [])
        else:
            child_node = recreate_node(orig, desc + [child.label])
        tree.children.append(child_node)
    return tree

class Tree(object):
    def __init__(self, label, children=[]):
        self.label = label
        self.children = children

    def __dict__(self):
        return {self.label: [c.__dict__() for c in sorted(self.children)]}

    def __str__(self, indent=None):
        return dumps(self.__dict__(), indent=indent)

    def __lt__(self, other):
        return self.label < other.label

    def __eq__(self, other):
        return self.__dict__() == other.__dict__()

    def from_pov(self, from_node):
        found, desc = get_node(self, from_node)
        if not found:
            raise ValueError("Node {} not found.".format(from_node))

        last_label = desc[-1]
        node = recreate_node(self, desc)
        last_node = node
        reverse_desc = [last_label]
        for name in reversed(desc[:-1]):
            desc_ = get_node(self, name)[1]
            parent = recreate_node(self, desc_)

            last_node.children.append(parent)
            parent.children = [
                child for child in parent.children
                if child.label != last_label
            ]
            last_label = desc_[-1]
            last_node = parent
        return node

    def path_to(self, from_node, to_node):
        tree = self.from_pov(from_node)
        found, desc = get_node(tree, to_node)
        if not found:
            raise ValueError("Dest node {} not found.".format(to_node))
        return desc


import os
import yaml
import openai

"""
使用openai API的方式访问ChatGPT/azure GPT
"""
def set_env(cfg_file):
    with open(cfg_file) as f:
        config_data = yaml.safe_load(f)
        azure = config_data["azure"]
        if azure is not None:
            for k, v in azure.items():
                os.environ[k] = v
    os.environ['MY_VARIABLE'] = 'my_value'

def ai_chat(msgs=None):
    openai.api_type = "azure"

    openai.api_version = "2023-03-15-preview"
    openai.api_base = os.getenv("api-base")  # Your Azure OpenAI resource's endpoint value.
    openai.api_key = os.getenv("api-key")

    response = openai.ChatCompletion.create(
        # 报错：openai.error.InvalidRequestError: The API deployment for this resource does not exist
        # 解决：只能使用账号已经部署的模型，通过OpenAI Studio查看部署了哪些模型
        engine="gpt-35-turbo-test",  # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.

        # 目前只能通过每次请求上传已有上下文的方式来记忆上下文/多轮对话
        messages=msgs
    )

    print(response)

    print(response['choices'][0]['message']['content'])

if __name__ == '__main__':
    set_env('D:\\qiyu-work\\openaikey.yaml')
    messages = [
        # {"role": "system", "content": "Assistant is a large language model trained by OpenAI."},
        #{"role": "system", "content": "Assistant is a large language model trained by OpenAI."},
        {"role": "system", "content": "你现在是一名汽车4S店专业的销售顾问，客户咨询你价格，请把下面的话用可爱的语气表达出来，不要重复我说的话，回复不能超过30个字"},
        {"role": "user", "content": "价格会受多因素的影响实时发生变化，具体我让销售跟您聊哈"}
    ]
    ai_chat(messages)


import sys, os
import subprocess
import datetime as dt
from random import randint
import argparse
import web3
from web3 import Web3
from web3.middleware import geth_poa_middleware
from eth_utils import decode_hex

# Project modules
import utils
from TextColor.color import bcolors

URL = "http://127.0.0.1:8545"
ACCOUNT_DB_NAME = 'car.json'
MGMT_CONTRACT_DB_NAME = utils.MGMT_CONTRACT_DB_NAME
MGMT_CONTRACT_SRC_PATH = utils.MGMT_CONTRACT_SRC_PATH
CONFIG = utils.open_data_base(ACCOUNT_DB_NAME)
DATABASE = utils.open_data_base(MGMT_CONTRACT_DB_NAME)

if DATABASE is None:
    sys.exit(f"{bcolors.FAIL}Setup hasn't been done{bcolors.ENDC}")

def generate_private_key(_w3: Web3) -> str:
    """
    Generate private key for car account using current time and random int

    :param Web3 _w3: Web3 instance
    :return: Private Key
    :rtype: str
    """

    t = int(dt.datetime.utcnow().timestamp())
    k = randint(0, 2 ** 16)
    privateKey = _w3.toHex(_w3.sha3(((t + k).to_bytes(32, 'big'))))
    if privateKey[:2] == '0x':
        privateKey = privateKey[2:]

    return (privateKey)

def new_car_account(_w3: Web3) -> None:
    """
    Create new addres for car account

    :param Web3 _w3: Web3 instance
    """

    privateKey = generate_private_key(_w3)
    data = {"key": privateKey}
    utils.write_data_base(data, ACCOUNT_DB_NAME)
    print(f"{bcolors.HEADER}{_w3.eth.account.privateKeyToAccount(data['key']).address}{bcolors.ENDC}")

def get_car_account_from_db(_w3: Web3) -> None:
    """
    Get car account from database

    :param Web3 _w3: Web3 instance
    """

    return (_w3.eth.account.privateKeyToAccount(utils.get_data_from_db(ACCOUNT_DB_NAME, 'key')).address)

def register_car(_w3: Web3):
    """
    Register new car

    :param Web3 _w3: Web3 instance
    """

    data = utils.open_data_base(MGMT_CONTRACT_DB_NAME)

    if data is None:
        return f'{bcolors.FAIL}Cannot access management contract database{bcolors.ENDC}'

    data = CONFIG

    if data is None:
        return f'{bcolors.FAIL}Cannot access account database{bcolors.ENDC}'

    private_key = data['key']
    mgmt_contract = utils.init_management_contract(_w3)
    car_address = _w3.eth.account.privateKeyToAccount(private_key).address
    registration_required_gas = 50000
    gas_price = utils.get_actual_gas_price(_w3)

    if registration_required_gas * gas_price > _w3.eth.getBalance(car_address):
        return 'No enough funds to send transaction'

    nonce = _w3.eth.getTransactionCount(car_address)
    tx = {'gasPrice': gas_price, 'nonce': nonce}

    regTx = mgmt_contract.functions.registerCar().buildTransaction(tx)
    signTx = _w3.eth.account.signTransaction(regTx, private_key)
    txHash = _w3.eth.sendRawTransaction(signTx.rawTransaction)
    receipt = web3.eth.wait_for_transaction_receipt(_w3, txHash, 120, 0.1)

    if receipt.status == 1:
        return f'{bcolors.OKGREEN}Registered successfully{bcolors.ENDC}'
    else:
        return f'{bcolors.FAIL}Car registration failed{bcolors.ENDC}'

def create_parser() -> argparse.ArgumentParser:
    """
    Create cli argument parser

    :return: Parser
    :rtype: argparse.ArgumentParser
    """

    parser = argparse.ArgumentParser(
        description='Car management tool',
        epilog="""
                  It is expected that Web3 provider specified by WEB3_PROVIDER_URI
                  environment variable. E.g.
                  WEB3_PROVIDER_URI=file:///path/to/node/rpc-json/file.ipc
                  WEB3_PROVIDER_URI=http://192.168.1.2:8545
               """
    )

    parser.add_argument(
        '--new', action='store_true', required=False,
        help='Generate a new account for the particular AGV'
    )

    parser.add_argument(
        '--account', action='store_true', required=False,
        help='Get identificator (Ethereum address) of AGV from the private key stored in car.json'
    )

    parser.add_argument(
        '--reg', action='store_true', required=False,
        help='Register the vehicle in the chain'
    )

    parser.add_argument(
        '--verify', type=str, required=False,
        help='Verify battery'
    )

    parser.add_argument(
        '--initiate_replacement', nargs=2, required=False,
        help='Initiate deal <car_battery> <sc_battery>'
    )

    return parser

def ask_for_replacement(car_battery_id: str, sc_battery_id: str, car_address: str) -> None:
    """
    Ask service center for replacement approval

    :param str car_battery_id: Car's battery
    :param str sc_battery_id: Service center's battery
    :param str car_address: Car's blockchain address
    :return: Nothing
    :rtype: None
    """

    if os.path.exists(f"scenter.py"):
        subprocess.run(
            [
                "python",
                "scenter.py",
                "--approve_replacement",
                f"{car_battery_id}",
                f"{sc_battery_id}",
                f"{car_address}",
            ]
        )
    else:
        sys.exit(f"{bcolors.FAIL}The asked service center does not exists{bcolors.ENDC}")

def get_sc_address() -> str:
    """
    Get address of the service center

    return: Service center's address
    rtype: str
    """

    command = "python scenter.py --get_address".split(' ')
    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

    return result.stdout[:-1]

def transfer_battery_to_sc(w3: Web3, car_battery_id: str, sc_address: str):
    """
    Transfer battery to service center

    :param Web3 w3: Web3 instance
    :param str car_battery_id: Car's battery id
    :param str sc_battery_id: Service centers's battery id

    return: Nothing
    rtype: None
    """

    data = utils.open_data_base(MGMT_CONTRACT_DB_NAME)

    if data is None:
        return 'Cannot access management contract database'

    data = utils.open_data_base(ACCOUNT_DB_NAME)

    if data is None:
        return 'Cannot access account database'

    private_key = data['key']
    battery_mgmt_contract_addr = utils.get_battery_managment_contract_addr(w3)
    battery_mgmt_contract = utils.init_battery_management_contract(w3, battery_mgmt_contract_addr)
    car_address = w3.eth.account.privateKeyToAccount(private_key).address
    gas_price = utils.get_actual_gas_price(w3)

    nonce = w3.eth.getTransactionCount(car_address)
    tx = {'gasPrice': gas_price, 'nonce': nonce, 'gas': 2204 * 68 + 21000}

    reg_tx = battery_mgmt_contract.functions.transfer(sc_address, decode_hex(car_battery_id)).buildTransaction(tx)
    sign_tx = w3.eth.account.signTransaction(reg_tx, private_key)
    tx_hash = w3.eth.sendRawTransaction(sign_tx.rawTransaction)
    receipt = web3.eth.wait_for_transaction_receipt(w3, tx_hash, 120, 0.1)

    if receipt.status != 1:
        sys.exit(f"{bcolors.FAIL}The car does not own this battery!{bcolors.ENDC}")

def get_new_battery(car_account: str, car_battery_id: str, sc_battery_id) -> float:
    """
    Call battery replacement in service center

    :param str car_account: Car account
    :param str car_battery_id: Car's battery id
    :return: Work's cost
    :rtype: float
    """

    command = f"python scenter.py --transfer_battery_to_car {car_account} {car_battery_id} {sc_battery_id}".split(' ')
    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

    return float(result.stdout[:-1])

def initiate_replacement(w3: Web3, car_battery_id: str, sc_battery_id: str) -> None:
    """
    Initiate battery replacement

    :param Web3 w3: Web3 instance
    :param str car_battery_id: Car's battery
    :param str sc_battery_id: Service center's battery
    :return: Nothing
    :rtype: None
    """

    sc_battery_id_path = f"firmware/{car_battery_id[:8]}.py"
    car_battery_id_path = f"firmware/{sc_battery_id[:8]}.py"

    print("Verifying battery...")

    data = utils.verify_battery(w3, sc_battery_id_path)

    if not data[0]:
        sys.exit(f"{bcolors.FAIL}The battery is fake{bcolors.ENDC}")

    sys.stdout.write("\033[F") #back to previous line
    sys.stdout.write("\033[K") #clear line

    print(f"Verifying battery...{bcolors.OKGREEN}Success{bcolors.ENDC}", u'\u2713')

    print("Asking service center for replacement...")

    ask_for_replacement(car_battery_id, sc_battery_id, get_car_account_from_db(w3))

    message = utils.open_data_base('replacement.json')

    if message is None:
        sys.exit(f"{bcolors.FAIL}Somethong went wrong...{bcolors.ENDC}")

    if not message['approved']:
        sys.exit(message['error'])

    sys.stdout.write("\033[F") #back to previous line
    sys.stdout.write("\033[K") #clear line

    print(f"Asking service center for replacement...{bcolors.OKGREEN}Approved{bcolors.ENDC}", u'\u2713')

    print("Getting address of the service center...")

    sc_address = get_sc_address()

    sys.stdout.write("\033[F") #back to previous line
    sys.stdout.write("\033[K") #clear line

    print(f"Getting address of the service center...{bcolors.OKGREEN}Success{bcolors.ENDC}", u'\u2713')

    print("Transferring battery to the service center...")

    transfer_battery_to_sc(w3, car_battery_id, sc_address)

    sys.stdout.write("\033[F") #back to previous line
    sys.stdout.write("\033[K") #clear line

    print(f"Transferring battery to the service center...{bcolors.OKGREEN}Success{bcolors.ENDC}", u'\u2713')

    print("Waiting for new battery installation...")

    result = get_new_battery(get_car_account_from_db(w3), car_battery_id, sc_battery_id)

    sys.stdout.write("\033[F") #back to previous line
    sys.stdout.write("\033[K") #clear line

    print(f"Battery was installed...{bcolors.OKGREEN}Success{bcolors.ENDC}", u'\u2713')

    return result

def main():
    w3 = Web3(Web3.HTTPProvider(URL))

    # configure provider to work with PoA chains
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)

    parser = create_parser()
    args = parser.parse_args()

    if args.new:
        new_car_account(w3)

    elif args.account:
        print(get_car_account_from_db(w3))

    elif args.reg:
        print(register_car(w3))

    elif args.verify:
        data = utils.verify_battery(w3, args.verify)
        print(f"Verified: {data[0]}")
        print(f"Total charges: {data[1]}")
        print(f"Vendor id: {data[2]}")
        print(f"Vendor name: {data[3]}")

    elif args.initiate_replacement:
        cost = initiate_replacement(w3, args.initiate_replacement[0], args.initiate_replacement[1])
        print(f"Cost of work: {cost} eth")

if __name__ == "__main__":
    main()


import argparse
import utils

parser = argparse.ArgumentParser(description="User need to submit job informations")
parser.add_argument('--min', type=int, required=True, help='min num of nodes')
parser.add_argument('--max', type=int, required=True, help="max num of nodes")
parser.add_argument('--N', type=int, required=True, nargs='+', help='num of nodes for scaling')
parser.add_argument('--O', type=float, required=True, nargs='+', help='objective ratio rate')
parser.add_argument('--res_up', type=int, required=True, help="scale up overhead")
parser.add_argument('--res_dw', type=int, required=True, help="scale down overhead")
parser.add_argument('--path', required=True, help="execute script path")
args = parser.parse_args()

def main():
    id = utils.submit_job(min=args.min, max=args.max, N=args.N,
                    O=args.O, res_up=args.res_up, res_dw=args.res_dw, path=args.path)
    print("Job submitted! GUID:", str(id))

    '''
    # previous job submit
    id = m.submit_job(min=1, max=5, Ns=[1, 2, 3, 4, 5],
                    Os=[1, 1.8, 2.6, 3.4, 4.2], res_up=3, res_dw=1, path="train.py")
    '''

if __name__ == "__main__":
    main()