import tkinter as tk
import pygubu
import cv2
import copy
import numpy as np

class Application:

	def __init__(self, master):
		self.master = master

		#create builder
		self.builder = builder = pygubu.Builder()
		#load ui file
		builder.add_from_file('hw1.ui')
		#create a widget
		self.mainwindow = builder.get_object('window', master)

		#connect callback
		builder.connect_callbacks(self)

	def btn_quit_on_click(self):
		self.master.quit()

	#button for problem 1.1
	def btn_11_on_click(self):
		#add your code here
		img = cv2.imread('dog.bmp')
		height, width, channels = img.shape
		cv2.imshow('img1.1',img)
		print('Height = '+str(height))
		print('Width = '+str(width))
		cv2.waitKey(0)

	#button for problem 1.2
	def btn_12_on_click(self):
		#add your code here
		img = cv2.imread('color.png')
		'''img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
		cv2.imshow('img1.2',img)'''
		for x in range(0,len(img)):
			for y in range(0,len(img[x])):
				a = img[x][y][0]
				b = img[x][y][1]
				c = img[x][y][2]
				img[x][y][0] = b
				img[x][y][1] = c
				img[x][y][2] = a
		cv2.imshow('img1.2',img)
		cv2.waitKey(0)

	#button for problem 1.3
	def btn_13_on_click(self):
		#add your code here
		img = cv2.imread('dog.bmp')
		height, width, channels = img.shape
		new_img = copy.deepcopy(img)
		for x in range(height):
			for y in range(width):
				new_img[x][width-1-y] = img[x][y]
		cv2.imshow('img1.3',new_img)
		cv2.waitKey(0)

	#button for problem 1.4
	def btn_14_on_click(self):
		def callback(x):
			pass
		#add your code here
		cv2.namedWindow('img1.4')

		cv2.createTrackbar('BLEND','img1.4',0,100,callback)
		cv2.createTrackbar('OFF','img1.4',0,1,callback)

		img = cv2.imread('dog.bmp')
		height, width, channels = img.shape
		new_img = copy.deepcopy(img)
		for x in range(height):
			for y in range(width):
				new_img[x][width-1-y] = img[x][y]
		while(True):
			off = cv2.getTrackbarPos('OFF','img1.4')
			if(off == 1):
				break
			blend = cv2.getTrackbarPos('BLEND','img1.4')
			blend = blend/100
			img_mix = cv2.addWeighted(img, (1-blend), new_img, blend, 0)
			cv2.imshow('img1.4',img_mix)
			cv2.waitKey(1)

	#button for problem 2.1
	def btn_21_on_click(self):
		#add your code here
		img = cv2.imread('eye.jpg')
		img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
		cv2.imshow('gray_img2.1', img)
		detect_img = cv2.Canny(img, 150, 300)
		cv2.imshow('detect_img2.1', detect_img)

		cv2.waitKey(0)

	#button for problem 2.2
	def btn_22_on_click(self):
		#add your code here
		img = cv2.imread('eye.jpg')
		cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
		draw_img = np.ones(img.shape, dtype=np.uint8)
		#	HoughCircles has Canny detector itself
		circles = cv2.HoughCircles(cimg, cv2.HOUGH_GRADIENT, 1, 20,param1=300,param2=40,minRadius=10,maxRadius=50)

		circles = np.uint16(np.around(circles))
		for i in circles[0,:]:
			# draw the outer circle
			cv2.circle(draw_img,(i[0],i[1]),i[2],(0,0,255),2)
			# draw the center of the circle
			#cv2.circle(fimg,(i[0],i[1]),2,(0,0,255),3)

		#	get Canny result to draw (has the same Canny parameter with HoughCircles)
		fimg = cv2.Canny(img,150,300)
		fimg = cv2.cvtColor(fimg, cv2.COLOR_GRAY2RGB)

		#	combine draw and Canny result
		mix_draw = cv2.addWeighted(draw_img, 1, fimg, 1, 0)
		cv2.imshow('detected circles',mix_draw)
		cv2.waitKey(0)

	#button for problem 2.3
	def btn_23_on_click(self):
		#add your code here
		cv2.waitKey(0)

	#button for problem 3
	def btn_3_on_click(self):
		#add your code here
		cv2.waitKey(0)

	#button for problem 4.1
	def btn_41_on_click(self):
		#add your code here
		img = cv2.imread('shoes.jpg')
		img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
		cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
		#cv2.imshow('img4.1_1',img)
		blur = cv2.GaussianBlur(img,(5,5),0)
		#cv2.imshow('img4.1_2',blur)
		median = cv2.medianBlur(blur,5)
		cv2.imshow('img4.1',median)
		cv2.waitKey(0)

	#button for problem 4.2
	def btn_42_on_click(self):
		#add your code here
		img = cv2.imread('shoes.jpg')
		img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
		img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
		cv2.imshow('img4.2_local',img)
		blur = cv2.GaussianBlur(img,(5,5),0)
		cv2.imshow('img4.2_Gussian_smooth',blur)
		median = cv2.medianBlur(blur,5)
		cv2.imshow('img4.2_median_filter',median)

if __name__ == '__main__':
	root = tk.Tk()
	app = Application(root)

	root.mainloop()


from loguru import logger
from gpiozero import Button
from fabiotobox.camera import Camera
from fabiotobox.diaporama import Diaporama
from fabiotobox.photohandler import PhotoHandler
from fabiotobox.tumblr import Tumblr
from enum import IntEnum
import pendulum
import time

SCREENSAVER_DELAY = 1

class PhotoFormat(IntEnum):
    PHOTOBOX = 0
    POLAROID = 1

class Mode(IntEnum):
    PHOTOBOX = 0
    DIAPORAMA = 1

class Fabiotobox:
    def __init__(
        self,
        camera: Camera,
        photo_handler: PhotoHandler,
        diaporama: Diaporama,
        tumblr: Tumblr,
        shoot_button_port: int,
        effect_button_port: int = None,
        format_button_port: int = None,
        event_title: str = "Test",
    ):
        self.shoot_button = Button(shoot_button_port)
        if effect_button_port:
            self.effect_button = Button(effect_button_port)
        if format_button_port:
            self.format_button = Button(format_button_port)
        self.camera = camera
        self.photo_handler = photo_handler
        self.diaporama = diaporama
        self.tumblr = tumblr
        self.photo_format = PhotoFormat.POLAROID
        self.event_title = event_title
        self.mode = Mode.PHOTOBOX
        self.diaporama_countdown = pendulum.now("Europe/Paris")

    def run(self):
        self.shoot_button.when_held = self.camera.end
        self.camera.start_preview()
        self.reset_diaporama_countdown()

        while True:
            if self.is_diaporama_countdown_reached():
                self.mode = Mode.DIAPORAMA

            if self.mode is Mode.PHOTOBOX:
                self.run_photobox()
            else:
                self.run_diaporama()

    def run_photobox(self):
        if self.shoot_button.is_pressed:
            logger.debug("Button pressed for a photo")
            photo = self.shoot_photo()
            self.camera.display_image(photo)
            time.sleep(3)
            self.camera.undisplay_image()

            if self.tumblr is not None:
                logger.info("Sending {} to tumblr".format(photo))
                self.tumblr.post_photo(photo, self.event_title, [])
            self.reset_diaporama_countdown()

    def run_diaporama(self):
        if self.shoot_button.is_pressed:
            logger.debug("Button pressed for exiting diaporama")
            self.mode = Mode.PHOTOBOX
            self.camera.undisplay_image()
            self.reset_diaporama_countdown()
            time.sleep(1)  # prevent event to be catched by photobox too
        else:
            if self.is_diaporama_countdown_reached():
                logger.info("dirs : {}".format(len(self.diaporama.dirs)))
                new_picture = self.diaporama.pick_photo()
                # Picture can be none : then, just reset countdown
                if new_picture is not None:
                    self.camera.display_image(new_picture)
                self.reset_diaporama_countdown()

    def shoot_photo(self) -> str:
        if self.photo_format == PhotoFormat.POLAROID:
            pictures = self.camera.shoot(1)
            photo = self.photo_handler.make_polaroid(pictures[0])
        else:
            pictures = self.camera.shoot(3)
            photo = self.photo_handler.make_photostrip(pictures)

        return photo

    def reset_diaporama_countdown(self):
        self.diaporama_countdown = pendulum.now("Europe/Paris").add(
            minutes=SCREENSAVER_DELAY
        )

    def is_diaporama_countdown_reached(self) -> bool:
        return self.diaporama_countdown < pendulum.now("Europe/Paris")


import os
import subprocess
import tempfile

import nbformat
import pytest

IGNORE_NOTEBOOKS: list[str] = [
    "12_ResDMD.ipynb",
    "12_koopman_mpc.ipynb",
    "koopman_mpc.ipynb",
]

def _notebook_run(path):
    """Execute a notebook via nbconvert and collect output. Returns the parsed notebook object
    and the execution errors.

    Source:
    https://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/
    """
    dirname, _ = os.path.split(path)
    os.chdir(dirname)
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
        args = [
            "jupyter",
            "nbconvert",
            "--to",
            "notebook",
            "--execute",
            "--ExecutePreprocessor.timeout=400",
            "--output",
            fout.name,
            path,
        ]
        subprocess.check_call(args)

        fout.seek(0)
        nb = nbformat.read(fout, nbformat.current_nbformat)

    errors = [
        output
        for cell in nb.cells
        if "outputs" in cell
        for output in cell["outputs"]
        if output.output_type == "error"
    ]

    return nb, errors

def _find_all_notebooks_to_run():
    from datafold import __path__ as datafold_path

    assert len(datafold_path) == 1

    import pathlib

    datafold_path = pathlib.Path(datafold_path[0])

    tutorials_path = datafold_path.parent / "tutorials"

    assert tutorials_path.is_dir()

    example_notebooks = []

    for ipynb_filepath in tutorials_path.rglob("*.ipynb"):
        if (
            ".ipynb_checkpoints" not in str(ipynb_filepath)
            and ipynb_filepath.name not in IGNORE_NOTEBOOKS
        ):
            assert ipynb_filepath.is_file()
            example_notebooks.append(ipynb_filepath)

    return example_notebooks

@pytest.mark.parametrize("nb_path", _find_all_notebooks_to_run())
def test_notebooks(nb_path):
    _, errors = _notebook_run(nb_path)
    assert not errors


from utils import readEdgeList, split_between_last_char
import numpy as np
import time
from ResultWritter import ResultWritter
import os

class KTupleFeatureGenerator:
    def __init__(self, path, k = 5, sample_times = 100, thread_num = 40):
        self.path = path
        self.k = k
        self.sample_times = sample_times
        self.thread_num = thread_num

    def generate_k_tuple_feature(self, path):
        os.system('./run ' + path + " " + str(self.k) + " " + str(self.sample_times) + " " + str(self.thread_num))

    def generate_k_tuple_feature_old(self, path):
        for i in range(3, self.k + 1):
            os.system('./runold ' + path + " " + str(i) + " " + str(self.sample_times) + " " + str(self.thread_num))

    def generateDataFeature(self):
        print(self.path)
        # self.generate_k_tuple_feature(self.path)
        prefix, _ = split_between_last_char(self.path, '.')
#             prefix += suffix
        print(prefix)
        if os.path.exists(prefix):
            filenames = os.listdir(prefix)
            filenames = [(prefix + "/" + name) for name in filenames]
            fileNames = []
            for name in filenames:
                if name.split('.')[-1] == "edges":
                    print(name)
                    self.generate_k_tuple_feature_old(name)

if __name__ == '__main__':
    path = "../data/artist_edges.edges"
    ktuple = KTupleFeatureGenerator(path = path)
    ktuple.generate_k_tuple_feature_old(ktuple.path)
    ktuple.generateDataFeature()


from collections import abc
from typing import Any, Callable, Dict, List, Optional, Union

def remove_nulls(data: Union[List, Dict],
                 value_filter: Optional[Callable[[Any], bool]] = None) -> Union[List, Dict]:
    """ Given a list or dict, returns an object of the same structure without filtered values.

    By default, key-value pairs where the value is 'None' are removed. The `value_filter` param
    must be a function which takes values from the provided dict/list structure, and returns a
    truthy value if the key-value pair is to be removed, and a falsey value otherwise.

    Args:
        data (Union[List, Dict]): List or dict containing data
        value_filter (Optional[Callable[[Any], bool]], optional): Lambda function to use to filter out values (e.g. "lambda x: x in (None, 'NULL', 'null')"). Defaults to None.

    Raises:
        TypeError: Raise TypeError if an unsupported data type is encountered

    Returns:
        Union[List, Dict]: Returns a filtered version of the list or dictionary passed to the function call

    Taken and modified from https://stackoverflow.com/questions/67806380/recursive-remove-all-keys-that-have-null-as-value
    """
    collection_types = (list, tuple) # avoid including 'str' here
    mapping_types = (abc.Mapping,)
    all_supported_types = (*mapping_types, *collection_types)
    if value_filter is None:
        value_filter = lambda x: x is None
    if isinstance(data, collection_types):
        data = [d for d in data if not value_filter(d)] # Remove Nones at root level of list
        return [remove_nulls(x, value_filter) if isinstance(x, all_supported_types) else x for x in data]
    elif isinstance(data, mapping_types):
        clean_val = lambda x: remove_nulls(x, value_filter) if isinstance(x, all_supported_types) else x
        return {k: clean_val(v) for k, v in data.items() if not value_filter(v)}
    raise TypeError(f"Unsupported type '{type(data)}': {data!r}")

# data = {
#     "field_1":None,
#     "field_2":"b",
#     "field_3":{"z":"z","y":"y","x":None},
#     "field_4":[{"z":"z","y":"y","x":None}, None, {"z":"z","y":None, "x":{"a":None,"b":"b"}}]
# }
# print(remove_nulls(data))


import cv2
import sys
from emot import emo_det

cascPath = "webcam.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
font = cv2.FONT_HERSHEY_SIMPLEX
video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    # Draw a rectangle around the faces and display the dominant emotion and score(in %)
    for (x, y, w, h) in faces:
        dom_emotion,dom_score = emo_det(frame)
        cv2.putText(frame, dom_emotion+"  "+str(dom_score*100)+'%', (x, y), font, 1, (0, 255, 0), 2)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()


import numpy as np
from scipy.optimize import curve_fit
import sys

def fit_DD(d, ik, imu, f, fit=None, p0=None, ilambda_max=None):
    """
    Fit P_DD(k, mu, lambda) with an given function
    f(lambda, y0, ...) = PDD_0 + f(lambda)

    Args:
      d  (dict):  lambda data returned by load_lambda()
      ik  (int):  k index
      imu (int): mu index
      f:   fitting function f(lambda, *params)
      fit: dictornary for results

    Returns:
      fit (dict)
        fit['lambda'] (array): lambda[ilambda]
        fit['PDD_params'] (array): best fitting *params
        fit['PDD'] (array): best fitting PDD[ilamba]
    """

    x = d['lambda'][:ilambda_max]
    y = d['summary']['PDD'][ik, imu, :ilambda_max]/d['summary']['PDD0'][ik, imu]

    def ff(x, *params):
        return PDD0*f(x, *params)

    # remove nans
    idx = np.isfinite(y)
    x = x[idx]
    y = y[idx]

    # initial guess
    if p0 is None:
        p0 = [0,]*(f.__code__.co_argcount - 1)

    # fitting
    try:
        popt, pcov = curve_fit(f, x, y, p0=p0)
    except RuntimeError:
        return None

    if fit is None:
        fit = {}

    fit['PDD_amp'] = d['summary']['PDD0'][ik, imu]
    fit['PDD_params'] = popt
    fit['lambda'] = x
    fit['PDD'] = d['summary']['PDD0'][ik, imu]*f(x, *popt)

    return fit

def fit_DU(d, ik, imu, f, fit=None, p0=None, ilambda_max=None):
    """
    Fit P_DU(k, mu, lambda) with an given function
    f(lambda, ...) = A*lambda*f(lambda, ...)

    Args:
      d    (dict):  lambda data returned by load_lambda()
      ik    (int): k index
      imu:  (int): mu index
      f:   (func): fitting function f(lambda, *params)
      fit  (dict): dictornary for results
    """

    def ff(x, A, *params):
        return A*x*f(x, *params)

    x = d['lambda'][:ilambda_max]
    y = d['summary']['PDU'][ik, imu, :ilambda_max]

    # remove nans
    idx = np.isfinite(y)
    x = x[idx]
    y = y[idx]

    # initial guess
    if p0 is None:
        p0 = [0,]*(f.__code__.co_argcount)
    else:
        p0 = [0,] + p0

    p0[0] = y[10]/x[10]

    # fitting
    try:
        popt, pcov = curve_fit(ff, x, y, p0=p0)
    except RuntimeError:
        sys.stderr.write('Warning: unable to fit DU with %s; ik=%d imu=%d\n' %
                         (f.__name__, ik, imu))
        return None

    if fit is None:
        fit = {}

    fit['PDU_amp'] = popt[0]
    fit['PDU_params'] = popt[1:]
    fit['lambda'] = x
    fit['PDU'] = ff(x, *popt)

    return fit

def fit_UU(d, ik, imu, f, fit=None, p0=None, ilambda_max=None):
    """
    Fit P_UU(k, mu, lambda) with an given function
    f(lambda, ...) = A*lambda**2*f(lambda, ...)

    Args:
      d   (dict): lambda data returned by load_lambda()
      ik   (int): k index
      imu  (int): mu index
      f   (func): fitting function f(lambda, *params)
      fit (dict): dictionary for the result
    """

    def ff(x, A, *params):
        return A*x**2*f(x, *params)

    x = d['lambda'][:ilambda_max]
    y = d['summary']['PUU'][ik, imu, :ilambda_max]

    # remove nans
    idx = np.isfinite(y)
    x = x[idx]
    y = y[idx]

    # initial guess
    if p0 is None:
        p0 = [0,]*(f.__code__.co_argcount)
    else:
        p0 = [0.0,] + p0

    p0[0] = y[10]/x[10]**2
    assert(len(p0) == f.__code__.co_argcount)

    # fitting
    try:
        popt, pcov = curve_fit(ff, x, y, p0=p0)
    except RuntimeError:
        sys.stderr.write('Warning: unable to fit UU with %s; ik=%d imu=%d\n' %
                         (f.__name__, ik, imu))
        return None

    if fit is None:
        fit = {}

    fit['PUU_amp'] = popt[0]
    fit['PUU_params'] = popt[1:]

    fit['lambda'] = x
    fit['PUU'] = ff(x, *popt)

    return fit

def _nans(shape):
    a = np.empty(shape)
    a[:] = np.nan
    return a

def fit_lambda(d, ik, imu, f, *,
               kind=('DD', 'DU', 'UU'),
               p0=None, ilambda_max=None):
    """
    Fit lambda plot with a fitting function f for a pair of k, mu

    P_DD(k, mu, lambda) = P_DD(k, mu, lambda=0)*f(lambda)
    P_DU(k, mu, lambda) = P_DU_amp*lambda*f(lambda)
    P_UU(k, mu, lambda) = P_UU_amp*lamba**2*f(lambda)

    Args:
      data (dict): lambda data loaded by load_lambda
      ik   (array-like): index of k
      imu  (array-like): index of mu
      f    (func): fitting function f(lambda, fitting parameters ...)
      kind (list): fitting P_**, subset of ('DD', 'DU', 'UU')
      p0   (list): initial parameter guess

    ik, imu can be:
      integer, 1D array, or 2D array.

    Result:
      fit (dict)
        fit['PDD'] (np.array): fitted P_DD
        fit['PDU'] (np.array): fitted P_DU
        fit['PUU'] (np.array): fitted P_DU

        fit['PDD_params']: best fitting parameters in f
        fit['PDU_params']: best fitting parameters in f
        fit['PUU_params']: best fitting parameters in f

        fit['PDU_amp']:    amplitude A in PDU = A*lambda*f(lambda)
        fit['PUU_amp']:    amplitude A in PDU = A*lambda**2*f(lambda)

      None if fitting failed
    """

    # single pair of (ik, imu)
    if isinstance(ik, int) and isinstance(imu, int):
        fit = {}

        if np.isnan(d['summary']['PDD'][ik, imu, 0]):
            return None

        if 'DD' in kind:
            fit_DD(d, ik, imu, f, fit, p0=p0, ilambda_max=ilambda_max)

        if 'DU' in kind:
            fit_DU(d, ik, imu, f, fit, p0=p0, ilambda_max=ilambda_max)

        if 'UU' in kind:
            fit_UU(d, ik, imu, f, fit, p0=p0, ilambda_max=ilambda_max)

        return fit

    # Convert ik, imu to np.array if they are array-like
    if type(ik) != np.ndarray:
        ik = np.array(ik, ndmin=1)

    if len(ik.shape) == 1:
        if type(imu) != np.ndarray:
            imu = np.array(imu, ndmin=1)

        if len(imu.shape) != 1:
            raise TypeError('If ik is an 1D array, '
                            'imu must also be an 1D array: '
                            'imu.shape {}'.format(imu.shape))

        nk = len(ik)
        nmu = len(imu)

        # Convert ik and imu to 2D arrays by repeating same row/column
        ik = ik.reshape((nk, 1)).repeat(nmu, axis=1)
        imu = imu.reshape((1, nmu)).repeat(nk, axis=0)

    # 2D arrays of ik imu
    if ik.shape != imu.shape:
        raise TypeError('2D arrays ik imu must have the same shape: '
                        '{} != {}'.format(ik.shape, imu.shape))

    nk = ik.shape[0]
    nmu = ik.shape[1]
    nparam = f.__code__.co_argcount
    # number of free paramters for f + linear RSD amplitude

    nlambda = len(d['lambda'][:ilambda_max])

    # Arrays for fitting results

    if 'DD' in kind:
        PDD_params = _nans((nk, nmu, nparam))
        PDD = _nans((nk, nmu, nlambda))

    if 'DU' in kind:
        PDU_params = _nans((nk, nmu, nparam))
        PDU = _nans((nk, nmu, nlambda))

    if 'UU' in kind:
        PUU_params = _nans((nk, nmu, nparam))
        PUU = _nans((nk, nmu, nlambda))

    for i in range(nk):
        for j in range(nmu):
            ik_ij = ik[i, j]
            imu_ij = imu[i, j]

            if 'DD' in kind:
                fit = fit_DD(d, ik_ij, imu_ij, f, p0=p0,
                             ilambda_max=ilambda_max)
                if fit:
                    PDD_params[i, j, 0]  = fit['PDD_amp']
                    PDD_params[i, j, 1:] = fit['PDD_params']
                    PDD[i, j, :] = fit['PDD']

            if 'DU' in kind:
                fit = fit_DU(d, ik_ij, imu_ij, f, p0=p0,
                             ilambda_max=ilambda_max)
                if fit:
                    PDU_params[i, j, 0]  = fit['PDU_amp']
                    PDU_params[i, j, 1:] = fit['PDU_params']
                    PDU[i, j, :] = fit['PDU']

            if 'UU' in kind:
                fit = fit_UU(d, ik_ij, imu_ij, f, p0=p0,
                             ilambda_max=ilambda_max)
                if fit:
                    PUU_params[i, j, 0]  = fit['PUU_amp']
                    PUU_params[i, j, 1:] = fit['PUU_params']
                    PUU[i, j, :] = fit['PUU']

    fit = {}
    fit['ik'] = ik
    fit['imu'] = imu
    fit['lambda'] = d['lambda'][:ilambda_max]

    if 'DD' in kind:
        fit['PDD'] = PDD
        fit['PDD_params'] = PDD_params
    if 'DU' in kind:
        fit['PDU'] = PDU
        fit['PDU_params'] = PDU_params
    if 'UU' in kind:
        fit['PUU'] = PUU
        fit['PUU_params'] = PUU_params

    return fit


import csv

from django.core.management.base import BaseCommand

from recipes.models import Tag

class Command(BaseCommand):
    help = 'Добавить список тэгов в базу (цвет и наименование)'

    def handle(self, *args, **options):
        with open('recipes/presets/tags.csv',
                  'r',
                  encoding='utf8'
                  ) as f:
            reader = csv.reader(f)
            current = Tag.objects.count()
            for row in reader:
                color, tag, slug = row
                Tag.objects.get_or_create(
                    color=color,
                    tag=tag,
                    slug=slug
                )
            result = Tag.objects.count() - current
            print(f'В базу добавлено {result} тэгов')


### Introduction to Pytorch Workflow timestamp to where it starts (link: https://www.youtube.com/watch?v=V_xro1bcAuA): 4:22:01

import torch
from torch import nn #nn contains all of pytorch's building blocks for neuro networks, pytorch documentation has a lot of building blocks for all sorts of layers

#you can combine layers in all the ways imaginable to make a beuro network model to do what you want it to do

import matplotlib.pyplot as plt
from pathlib import Path

"""
preparing and loading data (data can be almost anything in machine learning, like images, csv, videos, audio, text, or even dna)

machine learning is a game of 2 major parts: (that can be further subdivided into many other parts)
1. get data into a numerical representation (tensors)
2. build a model to learn patterns in that numerical representation
"""
# making data using a linear regression formula:

#creating known parameters: (in an actual dataset scraped from the internet, these won't be given, these are what the model is going to figure out)
weight = 0.7
bias = 0.3

#create:
start = 0
end = 1
step = 0.02
X = torch.arange(start, end, step).unsqueeze(dim=1) #x is usually used as a tensor, and we need the extra dimension for something later
y = weight * X + bias #the machine won't know this and will have to figure this out for itself, the y variable is the target

print(X[:10], y[:10], len(X), len(y))

## spliting data into training and test sets (one of the most important concepts in machine learning in general)

"""
visualizing the three datasets by comparing it to a school course:

training set: you can compare this to the course materials at a university that you would learn throughout the year, the model too would learn patterns from here
validation set: you can compare this to a practice exam, which would tune the model patterns/adjust the model's patterns (not always needed)
Test set: you can compare this to a final exam: which would see if the model is ready to be implimented/tests the model's performance on data it hasn't seen before

Goal: generalization (the ability for a machine learning model to perform well on data it hasn't seen before)

amount of data used for training set: ~60-80% (always needed)
amount of data used for validation set: 10-20% (not always needed)
amount of data used for test set: 10-20% (always needed)
"""

#create a train/test split/set (set and split mean the same thing in this case)
train_split = int(0.8 * len(X))
X_train, y_train = X[:train_split], y[:train_split] #gets all the data that's previous to that index
X_test, y_test = X[train_split:], y[train_split:] #gets all the data that is past that index

print(len(X_train), len(y_train), len(X_test), len(y_test)) #prints the amount of training features, training lables, testing features, testing lables

#NOTE: you can also use the sklearn/scikit module to split the training data in a more random way

## building a function to visualize the data

def plot_prediction(train_data = X_train,
                    train_lables = y_train,
                    test_data = X_test,
                    test_lables = y_test,
                    predictions = None):
    """
    Plots training data, test data, and compares predictions
    """
    plt.figure(figsize=(10, 7))

    #plot training data in blue
    plt.scatter(train_data, train_lables, c="blue", s=4, label="Training Data")

    #plot testing data in green
    plt.scatter(test_data, test_lables, c="green", s=4, label="Testing Data")

    if predictions != None:
        #plot the predictions if they exist
        plt.scatter(test_data, predictions, c="red", s=4, label="Predictions")

    plt.legend(prop={"size": 14})

    plt.show()

## building a model:

class LinearRegressionModel(nn.Module): # <- almost everything in pytorch inherits from nn, for more info: https://pytorch.org/docs/stable/generated/torch.nn.Module.html
    def __init__(self):
        super().__init__() #start with random parameters, then update them to fit the training data, by running it through the formula it'll adjust the data to fit the linear regression formula
        self.weight = nn.Parameter(torch.randn(1,
                                              requires_grad=True, #gradient descent = true
                                              dtype=torch.float))
        self.bias = nn.Parameter(torch.randn(1,
                                             requires_grad=True,
                                             dtype=torch.float)) #we might also initialize a layer or a list of layers for our model to use

    # Forward method to define the computation in a model:x is a parameter/input value, as you can see
    def forward(self, x: torch.Tensor) -> torch.Tensor: #x is the input data (of torch.Tensor datatype), and this function is going to return a tensor datatype
        return self.weight * x + self.bias #this is the linear regression formula, forward is what defines the opperation that a module does

    ### any subclass of nn.module needs to override the forward() method from model since it defines the computation of the model

"""
what the model does:
 - Starts with random values (weights and biases)
 - looks at training data and adjusts the random values to better represent/get closer to the ideal values (weight and bias values of our original formula)

How does it do it:
 1. Gradient Descent
 2. Back Propagation

 also check out the pytorch cheatsheet by googling pytorch cheatsheet
 Model building essentials:
  - torch.nn: contains all of the building materials for computational graphs (a neuro networks can be considered a computational graph)
  - torch.nn.Parameter(): what parameters our model should try and learn, often a pytorch layer from pytorch.nn will set these for us
  - torch.nn.Module: the base class for all neuro network modules, if you subclass it, you should override forward()
  - torch.optim: this references the optimizer in pytorch, they will help with gradient descent and contains various optimization algorithms
  - torch.data.Dataset: represents a map between the key (label) and sample (feature) pairs of your data, such as images and their associated labels
  - torch.data.DataLoader: creates a python iterable over a torch Dataset, allowing you to iterate over your data
  - torchvision.transforms: for pictures and vision into data into models
  - torchmetrics:

  - def forward(): all nn.Module subclasses require you to override this, as previously stated, this method defines what happens in the forward computation

"""

## Checking the contents of our model:

#to check the parameters of our model, we can use .parameters():

#sets tha seed so the values won't vary and results will stay consistant, without this, the tensor values in the LinearRegressionModel would be random every time (which is what we want, but for educational purposes that's not needed here)
torch.manual_seed(42)

#initialize model
model = LinearRegressionModel()

print(list(model.parameters()))

#list named parameters: (a parameter is something that the model sets itself/is present in the "()" incase i'm dum and forgot)
print(model.state_dict()) #the name comes from the self.weight and self.bias i think

## making predictions using torch.inference_mode()

#context manager, its good to make this a habit since it turns off gradient tracking since when we're doing predictions, which makes it a lot faster in larger data sets
#there's also torch.no_grad() but inference_mode is the prefered
with torch.inference_mode():
    y_preds = model(X_test)

print(f"Predictions: {y_preds}\nTest Data: {y_test}")

plot_prediction(predictions=y_preds)

"""## Training the model (moving from unknown/random parameters closer to the actual accurate parameters, aka moving from a poor representation of the data to a better one)

The loss function tells us how wrong our model's predictions are
 - note that a loss function can also be refered to as a cost function or a criterion in different areas

Things we need to train:
 - Loss function - a function that measures how wrong our model's predictions are compared to the idea outputs, the lower the better
 - Optimizer - takes into account the loss of a model and adjusts the model's parameters (e.g. weight and bias) to improve the loss function

For pytorch specifically
, we need:
 - a training loop
 - a testing look

you can check out all the loss functions in the pytorch documentation: https://pytorch.org/docs/stable/nn.html#loss-functions
"""

#choosing and implimenting a loss function and a optimizer:

#using L1Loss/Mean Absolute Error (taking the absolute difference between all the expected value/ideal value and the actual value and returns its average)
#measures how wrong our data is
loss_fn = nn.L1Loss()

#setup an optimizer (using a Stoch(SGD) algorithm)
#an optimizer adjusts the parameters according to the loss  function to reduce the loss
optimizer = torch.optim.SGD(model.parameters(), #the parameters that its going to take a look at/optimize
                            lr= 0.01) #learning rate: one of the most important hyperparameter (we set) you can set (regular parameters are set by the code)

#general idea of how optimizers work: it first increases the value in one direction, if the loss increases, then it increases in the other direction until the best value is achieved

"""
The learning rate (lr) is how mcuh it adjusts the parameters given to reduce the loss function/optimize the values, so the smaller the lr, the smaller the change in the parameter
the larget the learning rate, the larger the change int he parameter, if the lr is too bigthen it might skip over the optimal value, but if its too smal, then it'll take too
long to optimize

Q&A:
which loss function and optimizer should I use?
this depends on the context, with experience you'll get an idea of what works and what doesn't with your particular data set

ex. a regression problem would require something like a loss function of nn.L1Loss() and an optimizer like torch.optim.SGD()

but for classification problems like classifying whether or not a photo is of a dog or a cat, you'll likely want to use a loss function of nn.BCELoss() (binary cross entropy loss)
"""

## Building a training Loop (and a testing loop):

"""
steps:
0. looping through the data
1. forward pass (moving our data through the forward() method), also called forward propagation, moving in the opposite direction of a back propagation
2. calculate the loss: compare the forward pass predictions to the ground truth labels
3. optimizer zero grad
4. Back propagation (loss backwards?): data moves backwards through the network to calculate the gradients of each of the parameters of the model with respect to loss
5. optimizer step: use the optimizer to adjust the model's parameters to try to improve the loss
"""

#an epoch is one loop through the data, a hyper parameter because we set it ourselves
epochs = 200

#track different values and tracks model progress, used to plot model progress later on, useful for comparing with future experiments
epoch_count = []
loss_values = []
test_loss_values = []

print(model.state_dict())

for epoch in range(epochs):

    #set the model to training mode, training mode sets all paramaters that requires gradients to require gradients, requires_grad=True
    model.train()

    #forward pass:
    y_pred = model(X_train)

    #loss function:
    loss = loss_fn(y_pred, y_train) #predictions first then target
    print(f"Loss: {loss}")

    #optimizer zero_grad()
    optimizer.zero_grad()

    #4. back propagation on loss with respect to the parameters of the model
    loss.backward()

    #Optimizer, we want to step towards a gradient with a slope of 0 (slope of the loss function) or as low as possible, this is gradient descent and pytorch is doing this for you
    #in torch autograd
    optimizer.step() #by default how the optimizer changes will accumulate through the loop, so we have to zero them above (shown in step 3) for the next iteration of the loop

    ### testing
    model.eval() #evaluation mode, turns off training, starts testing
    #this turns off different stuff in the model that's not used for testing (essentially its like dropout/batch norm layers, read docs for more info)

    with torch.inference_mode(): #turns off gradient tracking for inference and a couple of other stuff to make testing faster. torch.no_grad() does the same but slower
        #1. foward pass:
        test_pred = model(X_test)

        #2. loss calculation:
        test_loss = loss_fn(test_pred, y_test) #y_test is the test labels, calculates the testing loss value

        if epoch % 10 == 0:
            epoch_count.append(epoch)
            loss_values.append(loss)
            test_loss_values.append(test_loss)
            print(f"Epoch: {epoch} | Loss: {loss} | Test loss: {test_loss}")
            print(model.state_dict())

#matplotlib works with numpy, not working with the gpu because i don't have one so i can skip the "".cpu()"".numpy() part and just go right to .numpy
plt.plot(torch.tensor(epoch_count).numpy(), torch.tensor(loss_values).numpy(), label="Train loss")
plt.plot(torch.tensor(epoch_count).numpy(), torch.tensor(test_loss_values).numpy(), label="Test loss")
plt.title("Training and test loss curves")
plt.ylabel("Loss")
plt.xlabel("Epoch")
plt.show()

#there is also learning rate scheduling, which is basically starting with big steps in the learning rate, then slowly lowering it,like reacing for the coin at the backofthe couch
#the lowest point is the convergence, its the point where the loss function is at its minimum

#the steps in the loop can be turned into a function, do later, first build intuition for it

with torch.inference_mode():
    y_preds_new = model(X_test)

plot_prediction(predictions=y_preds_new)

## Saving models:

"""
there are 3 main methods you should know about when it comes to saving and loading: (https://pytorch.org/tutorials/beginner/saving_loading_models.html)

1. torch.save(): saves a serialized object to disk, uses the python pickle library's utility for serialization. Models, tensors, and dictionaries are all kinds of objects that
can be saved using this function, its recommended to save the state_dict, but you can also save the entire model

2. torch.load(): uses the pickle module to unpickle facilities to deserialize object files to memory, in the process also facilitates the device that the data is being loaded
into

3. torch.nn.Module.load_state_dict(): Loads a model's parameter dictionary using a deserialized state_dict, for more info, check out the website linked above
"""

#create model directory:
MODEL_PATH = Path("models")
MODEL_PATH.mkdir(parents=True, exist_ok=True)

#create a model save path
MODEL_NAME = "01_pytorch_workflow_tutorial.pth" #the .pth is for saving a pytorch model

MODEL_SAVE_PATH = MODEL_PATH / MODEL_NAME

#saving only the model's state_dict(): (the model's weights and biases and etc)
print(f"saving model to: {MODEL_SAVE_PATH}")
torch.save(obj=model.state_dict(),
           f=MODEL_SAVE_PATH)

## Loading a model into a new instance of the model:

new_model = LinearRegressionModel()

#loading the state dict/loading the pre-trained values to replace the random values
new_model.load_state_dict(torch.load(f=MODEL_SAVE_PATH)) #loads all the state dictionaries like the weights and biases and etc

#making predictions using the loaded model:
new_model.eval()
with torch.inference_mode():
    new_model_pred = new_model(X_test)

    y_preds = model(X_test) #incase the y_preds value was changed

    ##compare the predictions/forward() calculations of both models, they should be the same since the values would be the same
    print(new_model_pred == y_preds)

##continued in Workflow_ractice.py

##more info on loading and saving models on the pytorch docs: https://pytorch.org/tutorials/beginner/saving_loading_models.html


from Permission import Permission
import pandas as pd
import requests

class Get_info:

    urlMarvel = 'http://gateway.marvel.com/v1/public/characters/' #Marvel API's url

    def __init__ (self, id):
        """Accessing Marvel API to get information about desired character using its id.
        Information retrieved: 1. name, 2. total number of events, 3. total number of series
        available, 4. total number of comics and 5. price of the most expensive comic that
        the charatcer was featured in"""

        self.id = id #id needs to be given

        link = self.urlMarvel + str(self.id) #url for specific Marvel Character
        response = requests.get(link, params = Permission().parameters()).json()
        response_price = requests.get(link + '/comics', params = Permission().parameters()).json() #Request for price feature

        #Get relevant features related to the character (name, events,series,comics & highest price)
        self.name = response['data']['results'][0]['name']
        self.events = response['data']['results'][0]['events']['available']
        self.series = response['data']['results'][0]['series']['available']
        self.comics = response['data']['results'][0]['comics']['available']

        #To get the highest price per comic
        all_prices_per_comic_list = []
        for dicts in response_price['data']['results']:
            for prices in dicts['prices']:
                all_prices_per_comic_list.append(prices['price'])
        #Highest price info
        self.price = max(all_prices_per_comic_list, default=0)

    def filtered_info(self):
        """Return dataframe with all the information related to desired character"""
        entry = pd.DataFrame({
                        'Character Name' : [self.name],
                        'Character ID' : [self.id],
                        'Total Available Events' : [self.events],
                        'Total Available Series' : [self.series],
                        'Total Available Comics' :  [self.comics],
                        'Price of the Most Expensive Comic' : [self.price]})
        return entry


from datetime import datetime

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField, SelectField, FloatField
from wtforms.validators import DataRequired, Length, Regexp

class NewOrderForm(FlaskForm):
    description = TextAreaField("Опис: ",
                                validators=[DataRequired(),
                                            Length(max=1000)])

    client = StringField('Клієнт',
                         id='client_autocomplete',
                         validators=[DataRequired(),
                                     Length(max=200)])

    created_at = StringField("Дата створення",
                             validators=[DataRequired(),
                                         Regexp(
                                             r'^(0[1-9]|[12][0-9]|3[01])[\.](0[1-9]|1[012])[\.]((19|20)\d\d|\d\d)$')],
                             default=datetime.now().strftime("%d.%m.%Y"))

    serial = StringField("Серійний номер",
                         id='serial_autocomplete',
                         validators=[Length(max=200)])

    price = FloatField("Ціна", validators=[DataRequired()])

    staff = SelectField("Виконавець")

    type = SelectField("Тип замовлення")

    submit = SubmitField("Зберегти")

    def __init__(self, order=None, staff_choices=None, type_choices=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if order:
            self.description.data = order.description
            self.client.data = order.client
            self.created_at.data = order.created_at.strftime("%d.%m.%Y")
            self.serial.data = order.device.serial
            self.price.data = order.price
            self.staff.data = order.staff.name
            self.type.data = order.type.name

        if staff_choices:
            self.staff.choices = staff_choices

        if type_choices:
            self.type.choices = type_choices

class NewClientForm(FlaskForm):
    name = StringField("Ім'я фізичної або юридичної особи",
                       validators=[DataRequired(),
                                   Length(max=200)])

    # validators = [Regexp(r'^(\+\d{1,2}\s?)?((\(?\d{3}\)?)[\s]?\d{3}[\s]?\d{4}|(\(?\d{3,4}\)?)[\s]?\d{3}[\s]?\d{3})$')]
    phone = StringField("Номер телефону",
                        validators=[DataRequired(),
                                    Length(max=80)])

    address = StringField("Адреса",
                          validators=[Length(max=200)])

    notes = TextAreaField("Примітки",
                          validators=[Length(max=1000)])

    submit = SubmitField("Зберегти")

    def __init__(self, client=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if client:
            self.name.data = client.name
            self.phone.data = client.phone
            self.address.data = client.address
            self.notes.data = client.notes

class NewDeviceForm(FlaskForm):
    name = StringField("Назва")
    serial = StringField("Серійний номер", validators=[DataRequired()])
    submit = SubmitField("Зберегти")

    def __init__(self, device=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if device:
            self.name.data = device.name
            self.serial.data = device.serial

class NewStaffForm(FlaskForm):
    name = StringField("Ім'я")
    submit = SubmitField("Зберегти")

class DeleteConfirmForm(FlaskForm):
    delete = SubmitField("Так, видалити")

class NavigationForm(FlaskForm):
    search_field = StringField("Введіть пошуковий запит")

    status_field = SelectField('Статус')

    type_field = SelectField('Тип')

    sort_by_field = SelectField('Сортувати за', choices=[('new_first', 'Датою (нові спочатку)'),
                                                         ('old_first', 'Датою (старі спочатку)'),
                                                         ('client', 'Клієнтом'),
                                                         ('status', 'Статусом')])

    def __init__(self, status_choices=None, type_choices=None, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if status_choices:
            self.status_field.choices = status_choices

        if type_choices:
            self.type_field.choices = type_choices


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 16 13:14:57 2021

@author: Samael Olascoaga
@email: olaskuaga@gmail.com
"""

import pandas as pd
import gseapy as gp
import matplotlib.pyplot as plt
from gseapy.plot import barplot, dotplot
import numpy as np
import seaborn as sns

sns.set_style("whitegrid")

gene_list = pd.read_csv('common.csv', header=None)
glist = gene_list.squeeze().str.strip().tolist()

names = gp.get_library_name()

enr = gp.enrichr(gene_list= glist,
                 gene_sets=['KEGG_2019_Human'],
                 organism='Human', # don't forget to set organism to the one you desired! e.g. Yeast
                 description='KEGG common targets',
                 # no_plot=True,
                 cutoff=0.5 # test dataset, use lower value from range(0,1)
                )

resultados = enr.results.head(15)
resultados['-log10(FDR)'] = -np.log10(resultados['Adjusted P-value'])
resultados['Genes'] = resultados['Genes'].str.split(';')
resultados['Genes'] = resultados['Genes'].apply(lambda x: len(x))

g = sns.scatterplot(data=resultados, x="-log10(FDR)", y="Term", hue='-log10(FDR)', palette="seismic"
                , size="Genes", sizes=(30, 300), legend=True)

g.legend(loc=6, bbox_to_anchor=(1, 0.5), ncol=1)
plt.ylabel('')
plt.xlabel('-log10(FDR)')
plt.title('KEGG Common targets')

plt.savefig(r'KEGG_common' + '.svg', format='svg', dpi=600, bbox_inches = "tight" )


import numpy as np
from functions import mean_absolute_percentage_error
import torch

data = np.load('predictions.npz')
h_label = data['labels']
h_pred = data['pred']
indices_under_1500 = h_label < 1500
indices_under_1300 = h_label < 1300

h_pred = torch.Tensor(h_pred)
h_label = torch.Tensor(h_label)

h_pred_under_1500 = h_pred[indices_under_1500]
h_label_under_1500 = h_label[indices_under_1500]
h_pred_under_1300 = h_pred[indices_under_1300]
h_label_under_1300 = h_label[indices_under_1300]

print('all data: ', h_pred.shape[0])
print('under 1500 data: ', h_pred_under_1500.shape[0])
print('under 1300 data: ', h_pred_under_1300.shape[0])

print('mape (all data): ', mean_absolute_percentage_error(h_label, h_pred))
print('mape (under 1500): ', mean_absolute_percentage_error(h_label_under_1500, h_pred_under_1500))
print('mape (under 1300): ', mean_absolute_percentage_error(h_label_under_1300, h_pred_under_1300))

print('label under 1300: ', h_label_under_1300)
print('pred under 1300: ', h_pred_under_1300)


#!/usr/bin/env python3
"""
Name: vpc_consistency.py
Description: NXAPI: display inconsistent vpc parameters

Example output when vpc is consistent:

% ./vpc_consistency.py --vault hashicorp --devices cvd_leaf_2 --interface Po11,Po12
192.168.11.103  cvd-1312-leaf        all 22 global vpc params are consistent
192.168.11.103  cvd-1312-leaf        all 7 vni vpc params are consistent
192.168.11.103  cvd-1312-leaf        all 12 vlans vpc params are consistent
192.168.11.103  cvd-1312-leaf        Po11 all 23 interface vpc port-channel params are consistent
192.168.11.103  cvd-1312-leaf        Po12 all 23 interface vpc port-channel params are consistent
%

Example output when vpc po allowed-vlans are mismatched:

% ./vpc_consistency.py --vault hashicorp --devices cvd_leaf_2 --interface Po11,Po12
192.168.11.103  cvd-1312-leaf        all 22 global vpc params are consistent
192.168.11.103  cvd-1312-leaf        all 7 vni vpc params are consistent
192.168.11.103  cvd-1312-leaf        all 12 vlans vpc params are consistent
192.168.11.103  cvd-1312-leaf        Po11 Allowed VLANs
   vpc-param-type: -
   vpc-param-local-val: 1111-1112
   vpc-param-peer-val: 1111
192.168.11.103  cvd-1312-leaf        Po11 Local suspended VLANs
   vpc-param-type: -
   vpc-param-local-val: 1112
   vpc-param-peer-val: -
192.168.11.103  cvd-1312-leaf        Po12 all 23 interface vpc port-channel params are consistent
%
"""
our_version = 109
script_name = "vpc_consistency"

# standard libraries
import argparse
from concurrent.futures import ThreadPoolExecutor

# local libraries
from nxapi_netbox.args.args_cookie import ArgsCookie
from nxapi_netbox.args.args_nxapi_tools import ArgsNxapiTools
from nxapi_netbox.general.log import get_logger
from nxapi_netbox.netbox.netbox_session import netbox, get_device_mgmt_ip
from nxapi_netbox.vault.vault import get_vault
from nxapi_netbox.nxapi.nxapi_vpc_consistency import (
    NxapiVpcConsistencyGlobal,
    NxapiVpcConsistencyVni,
    NxapiVpcConsistencyVlans,
    NxapiVpcConsistencyInterface,
)

def get_parser():
    help_interfaces = "a comma-separated list (no spaces) of port-channel interfaces to test for vpc consistency"
    help_mismatched_labels = "display labels whose number of comma-separated entries differ from the number of values they refer to"

    ex_interfaces = "Example: --interfaces Po1,Po10"
    ex_mismatched_labels = "Example: --mismatched_labels"

    parser = argparse.ArgumentParser(
        description="DESCRIPTION: NXAPI: display inconsistent vpc parameters",
        parents=[ArgsCookie, ArgsNxapiTools],
    )
    default = parser.add_argument_group(title="DEFAULT SCRIPT ARGS")
    mandatory = parser.add_argument_group(title="MANDATORY SCRIPT ARGS")

    parser.add_argument(
        "--version", action="version", version="{} v{}".format("%(prog)s", our_version)
    )

    default.add_argument(
        "--mismatched_labels",
        dest="mismatched_labels",
        required=False,
        action="store_true",
        default=False,
        help="{} {}".format(help_mismatched_labels, ex_mismatched_labels),
    )

    default.add_argument(
        "--interfaces",
        dest="interfaces",
        required=False,
        default=None,
        help="{} {}".format(help_interfaces, ex_interfaces),
    )

    return parser.parse_args()

def get_device_list():
    try:
        return cfg.devices.split(",")
    except:
        log.error(
            "exiting. Cannot parse --devices {}.  Example usage: --devices leaf_1,spine_2,leaf_2".format(
                cfg.devices
            )
        )
        exit(1)

def print_output(futures):
    for future in futures:
        output = future.result()
        if output == None:
            continue
        for line in output:
            print(line)

def show_inconsistent_params(ip, nx, interface=None):
    lines = list()
    if nx.error_reason != None:
        log.error("{} {} error: {}".format(tb.sid, nx.hostname, nx.error_reason))
        return lines
    inconsistent_items = nx.inconsistent_params
    if len(inconsistent_items) == 0:
        if interface == None:
            lines.append(
                "{:<15} {:<20} all {} {} vpc params are consistent".format(
                    ip, nx.hostname, len(nx.info), nx.param_type
                )
            )
        else:
            lines.append(
                "{:<15} {:<20} {} all {} {} vpc port-channel params are consistent".format(
                    ip, nx.hostname, interface, len(nx.info), nx.param_type
                )
            )
    else:
        for item in nx.inconsistent_params:
            if interface == None:
                lines.append(
                    "{:<15} {:<20} {}".format(ip, nx.hostname, item["vpc-param-name"])
                )
            else:
                lines.append(
                    "{:<15} {:<20} {} {}".format(
                        ip, nx.hostname, interface, item["vpc-param-name"]
                    )
                )
            for key in item:
                if key == "vpc-param-name":
                    continue
                lines.append("   {}: {}".format(key, item[key]))
    return lines

def show_mismatched_labels(ip, nx):
    lines = list()
    if cfg.mismatched_labels == False:
        return lines
    if len(nx.mismatched_info) > 0:
        for label in nx.mismatched_info:
            lines.append(
                "{:<15} {:<20} vpc-param-name {}".format(ip, nx.hostname, label)
            )
            lines.append("    labels {}".format(nx.mismatched_info[label]["names"]))
            lines.append("    values {}".format(nx.mismatched_info[label]["params"]))
    return lines

def worker(device, vault):
    ip = get_device_mgmt_ip(nb, device)
    lines = list()
    for class_name in [
        NxapiVpcConsistencyGlobal,
        NxapiVpcConsistencyVni,
        NxapiVpcConsistencyVlans,
    ]:
        nx = class_name(vault.nxos_username, vault.nxos_password, ip, log)
        nx.nxapi_init(cfg)
        nx.refresh()
        if nx.error_reason != None:
            lines.append("{} {} error: {}".format(ip, nx.hostname, nx.error_reason))
            return lines
        lines += show_inconsistent_params(ip, nx)
        lines += show_mismatched_labels(ip, nx)

    if cfg.interfaces == None:
        return lines
    for interface in cfg.interfaces.split(","):
        nx = NxapiVpcConsistencyInterface(
            vault.nxos_username, vault.nxos_password, ip, log
        )
        nx.nxapi_init(cfg)
        nx.interface = interface
        nx.refresh()
        lines += show_inconsistent_params(ip, nx, interface)
    return lines

cfg = get_parser()
log = get_logger(script_name, cfg.loglevel, "DEBUG")
vault = get_vault(cfg.vault)
vault.fetch_data()
nb = netbox(vault)

devices = get_device_list()

executor = ThreadPoolExecutor(max_workers=len(devices))
futures = list()
for device in devices:
    args = [device, vault]
    futures.append(executor.submit(worker, *args))
print_output(futures)


# Download the audio files for all of the video URLs given in the input file

import argparse
import os
import sys

youtube_cmd = \
        "youtube-dl --extract-audio --audio-format mp3 -o \"{file_name}\" {url}"

def download_song(artist, song, url):
    artist_part = '-'.join(artist.lower().split())
    song_part = '-'.join(song.lower().split())
    file_name = artist_part + "__" + song_part + ".%(ext)s"
    return 0 == os.system(youtube_cmd.format(file_name=file_name, url=url))
    # (Command returns 0 on success)

def mark_completed(f, line):
    """
    Mark the song on the current line in the given file as downloaded. Change
    the mark from `-` to `#`.

    Args:
        f (file)        : A handle to the songs file, open at the current song
        line (string)   : The currently read line (containing the song)

    Returns:
        Nothing

    NOTE: The file must be open in binary format.
    """
    try:
        marker_position = line.decode().index("| -") + 2  # add 2 to reach `-`
        f.seek(-len(line), os.SEEK_CUR)  # move back to the start of the current line
        f.seek(marker_position, os.SEEK_CUR)  # move to the position of the marker
        f.write(b"#")  # write the mark completed symbol (`-` -> `#`)
        f.readline()  # move to the next line (read past the current `\n`)
    except ValueError:
        # Could not find `-` marker
        pass
    except Exception as e:
        # Here's a generic way for printing where the exception occurred
        _, _, e_traceback = sys.exc_info()
        print(f"Error:{e_traceback.tb_lineno}: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Download music from YouTube.")
    parser.add_argument("-songs_file", type=str, help="list of songs to download")
    parser.add_argument("-out_dir", type=str, help="download directory")
    args = parser.parse_args()

    if args.songs_file is None:
        print("Please supply songs file")
        exit(0)
    elif args.out_dir is None:
        print("Please supply download directory")
        exit(0)

    try:
        os.listdir(args.out_dir)
    except FileNotFoundError:
        # Download directory does not exist, so create it
        try:
            os.mkdir(args.out_dir)
        except:
            print("Could not create download directory")
            exit(0)

    home_dir = os.getcwd()

    with open(args.songs_file, 'rb+') as f:
        # Move to download directory for placing songs
        os.chdir(args.out_dir)

        for line in f:
            # Extract the song name and look up video, cleaning up white space
            fields = [ field.strip() for field in  line.decode().split('|') ]

            song = fields[0]
            artist = fields[1]
            try:
                url = fields[2]
                try:
                    mark  = fields[3]
                    if mark == '#':
                        print(f"Already downloaded: {song}")
                        continue
                except IndexError:
                    # Song not marked with `-` or `#`
                    print(f"Mark not found: {song}")
                    continue

            except IndexError:
                # Cannot download this song
                continue

            if (download_song(artist, song, url)):
                mark_completed(f, line)

    if (home_dir != os.getcwd()):
        # Return to original directory
        os.chdir(home_dir)


from os import path
from setuptools import setup

# read the contents of your description file

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name="pyswahili",
    version="0.1.4",
    description="""
    Python package for briding python english keywords
    with swahili one to allow swahili speakers to learn the basics of coding
    without ever knowing  english
    """,
    long_description=long_description,
    long_description_content_type='text/markdown',
    url="https://github.com/Kalebu/pyswahili",
    download_url='https://github.com/Kalebu/pyswahili/releases/tag/0.1',
    author="Jordan Kalebu",
    author_email="isaackeinstein@gmail.com",
    license="MIT",
    packages=["pyswahili"],
    keywords=[
        "pyswahili",
        "python-tanzania",
        "python-transpiler",
        "swahili-python",
        "python in swahili",
        "python for swahili speakers",
        "code python in swahili",
        "swahili programming language",
        "program in swahili",
    ],
    entry_points={
        "console_scripts": [
            "pyswahili = pyswahili.__main__:main"
        ]
    },
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "Topic :: Software Development :: Build Tools",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
    ]
)


from abc import ABC, abstractmethod
from typing import List

import requests
from config import EnvConfig
from models.Note import Card, DeckServiceCard

class DeckServiceAPIInterface(ABC):
    @abstractmethod
    def save_cards(self, user_id: str, deck_id: str, cards: List[Card]):
        pass

class DeckServiceAPI(DeckServiceAPIInterface):
    def __init__(self, config: EnvConfig):
        self.DECK_SERVICE_HOST_NAME = config.DECK_SERVICE_HOST_NAME

    def save_cards(
        self, user_id: str, deck_id: str, cards: List[Card]
    ) -> List[DeckServiceCard]:
        url = f"http://{self.DECK_SERVICE_HOST_NAME}/decks/{deck_id}/cards?userID={user_id}"
        data = [{"deckID": deck_id, **card} for card in cards]

        response = requests.post(url, json=data)

        if response.status_code != 201:
            raise Exception(
                f"Failed to save cards. Status code: {response.status_code}. Response: {response.json()}"
            )

        response_body = response.json()
        response_data: List[DeckServiceCard] = response_body["data"]

        return response_data


import numpy as np, h5py as h5

from horton import *  # pylint: disable=wildcard-import,unused-wildcard-import
from horton.part.test.common import get_proatomdb_cp2k
from horton.test.common import tmpdir

def test_db_basics():
    padb = ProAtomDB.from_refatoms(numbers=[8, 1], max_cation=1, max_anion=1)
    assert padb.get_numbers() == [1, 8]
    assert padb.get_charges(8) == [1, 0, -1]
    assert padb.get_charges(1) == [0, -1]
    r1 = padb.get_record(8, -1)
    assert r1.number == 8
    assert r1.charge == -1
    assert abs(r1.energy - -72.587) < 1e-3
    assert r1.ipot_energy == padb.get_record(8, 0).energy - r1.energy
    assert r1.population == 9
    assert r1.pseudo_number == 8
    assert r1.pseudo_population == 9
    assert r1.safe
    assert r1.rgrid.size == 59
    r2 = padb.get_record(8, -1)
    r3 = padb.get_record(8, 0)
    assert r1 == r2
    assert r1 != r3
    assert padb.get_rgrid(8) is r1.rgrid
    assert padb.get_record(8, +1).ipot_energy is None
    assert padb.get_record(8, -1).ipot_energy == padb.get_record(8, 0).energy - padb.get_record(8, -1).energy
    assert padb.get_record(1, 0).ipot_energy == -padb.get_record(1, 0).energy

def test_db_basics_pseudo():
    padb = get_proatomdb_cp2k()
    assert padb.get_numbers() == [8, 14]
    assert padb.get_charges(8) == [2, 1, 0, -1, -2]
    assert padb.get_charges(8, safe=True) == [2, 1, 0, -1]
    assert padb.get_charges(14) == [0]
    assert not padb.get_record(8, -2).safe
    assert padb.get_rgrid(8) is padb.get_record(8, -2).rgrid
    assert padb.get_rgrid(8) is padb.get_record(8, -1).rgrid
    assert padb.get_rgrid(8) is padb.get_record(8, 0).rgrid
    assert padb.get_rgrid(8) is padb.get_record(8, 1).rgrid
    assert padb.get_rgrid(8) is padb.get_record(8, 2).rgrid
    r1 = padb.get_record(8, -1)
    assert r1.safe
    assert abs(r1.energy - -15.866511882272) < 1e-8
    assert abs(r1.ipot_energy - (padb.get_record(8, 0).energy - r1.energy)) < 1e-5
    r2 = padb.get_record(8, -2)
    assert not r2.safe
    assert abs(r2.energy - -15.464982778766) < 1e-8
    assert abs(r2.ipot_energy - (r1.energy - r2.energy)) < 1e-5
    assert padb.get_record(8, +2).ipot_energy is None

def test_record_basics_pseudo():
    fn_out = context.get_fn('test/atom_si.cp2k.out')
    mol = IOData.from_file(fn_out)
    r = ProAtomRecord.from_iodata(mol)
    assert r.number == 14
    assert r.charge == 0
    assert abs(r.energy - -3.761587698067) < 1e-10
    assert r.ipot_energy is None
    assert r.population == 14
    assert r.pseudo_number == 4
    assert r.pseudo_population == 4
    assert r.safe

def compare_padbs(padb1, padb2):
    assert padb1.size == padb2.size
    for number in padb1.get_numbers():
        for charge in padb1.get_charges(number):
            r1 = padb1.get_record(number, charge)
            r2 = padb2.get_record(number, charge)
            assert r1 == r2

def test_io_group():
    padb1 = ProAtomDB.from_refatoms(numbers=[1, 6], max_cation=1, max_anion=1)
    assert padb1.size == 5
    keys = sorted(padb1._map.keys())
    assert keys == [(1, -1), (1, 0), (6, -1), (6, 0), (6, +1)]

    with h5.File('horton.dpart.test.test_proatomdb.test_io_group', "w", driver='core', backing_store=False) as f:
        padb1.to_file(f)
        padb2 = ProAtomDB.from_file(f)
        compare_padbs(padb1, padb2)

def test_io_filename():
    padb1 = ProAtomDB.from_refatoms(numbers=[1, 6], max_cation=1, max_anion=0)
    keys = sorted(padb1._map.keys())
    assert keys == [(1, 0), (6, 0), (6, 1)]

    with tmpdir('horton.dpart.test.test_proatomdb.test_io_filename') as dn:
        filename = '%s/test.h5' % dn
        padb1.to_file(filename)
        padb2 = ProAtomDB.from_file(filename)
        compare_padbs(padb1, padb2)

def test_compute_radii():
    rgrid = RadialGrid(ExpRTransform(1e-3, 1e1, 100))
    padb = ProAtomDB.from_refatoms([1, 6], 0, 0, (rgrid, 110))
    record = padb.get_record(6, 0)
    indexes, radii = record.compute_radii([2.0, 5.9, 5.999])
    assert (indexes == [68, 89, 100]).all()
    assert abs(radii - np.array([0.522305, 3.595831, 10.0])).max() < 1e-5

def test_moments():
    padb = get_proatomdb_cp2k()
    record0 = padb.get_record(8, 0)
    record1 = padb.get_record(8, 1)
    m0 = record0.get_moment(3)
    m1 = record1.get_moment(3)
    assert m0 > m1
    assert abs(m0-21.84) < 1e-2
    assert abs(m1-12.17) < 1e-2

def check_spline_record(spline, record):
    assert abs(spline.y - record.rho).max() < 1e-10
    assert abs(spline.dx - record.deriv).max() < 1e-10

def check_spline_pop(spline, pop):
    rtf = spline.rtransform
    check_pop = 4*np.pi*dot_multi(
        rtf.get_deriv(),
        rtf.get_radii()**2,
        spline.y,
    )
    assert abs(pop - check_pop) < 1e-2

def check_spline_mono_decr(spline):
    t = np.arange(0, spline.rtransform.npoint, 0.1)
    x = spline.rtransform.radius(t)
    y = spline(x)
    i = (abs(y) < 1e-10).nonzero()[0][0]
    y = y[:i]
    assert ((y[1:] - y[:-1])/y[:-1]).min() < 1e-9

def test_get_spline():
    padb = ProAtomDB.from_refatoms(numbers=[1, 6], max_cation=1, max_anion=1)

    spline = padb.get_spline(6)
    check_spline_pop(spline, 6.0)
    check_spline_record(spline, padb.get_record(6, 0))
    check_spline_mono_decr(spline)

    spline = padb.get_spline(6, -1)
    check_spline_pop(spline, 7.0)
    check_spline_record(spline, padb.get_record(6, -1))
    check_spline_mono_decr(spline)

    spline = padb.get_spline(6, {0:0.5, -1:0.5})
    check_spline_pop(spline, 6.5)
    check_spline_mono_decr(spline)

    spline = padb.get_spline(1, {0:0.5})
    check_spline_pop(spline, 0.5)
    check_spline_mono_decr(spline)

def test_get_spline_pseudo():
    padb = get_proatomdb_cp2k()

    spline = padb.get_spline(8)
    check_spline_pop(spline, 6.0)
    check_spline_record(spline, padb.get_record(8, 0))

    spline = padb.get_spline(8, -1)
    check_spline_pop(spline, 7.0)
    check_spline_record(spline, padb.get_record(8, -1))

    spline = padb.get_spline(8, {0:0.5, -1:0.5})
    check_spline_pop(spline, 6.5)

    spline = padb.get_spline(14)
    check_spline_pop(spline, 4.0)
    check_spline_record(spline, padb.get_record(14, 0))

def test_compact():
    padb = get_proatomdb_cp2k()
    padb.compact(0.1)
    assert padb.get_rgrid(8).size < 100
    assert padb.get_rgrid(14).size < 100

def test_normalize():
    padb = get_proatomdb_cp2k()
    padb.compact(0.1)
    padb.normalize()
    for number in padb.get_numbers():
        rgrid = padb.get_rgrid(number)
        for charge in padb.get_charges(number):
            r = padb.get_record(number, charge)
            nel = rgrid.integrate(r.rho)
            nel_integer = r.pseudo_number - charge
            assert abs(nel - nel_integer) < 1e-10

def test_empty_proatom():
    padb = get_proatomdb_cp2k()
    assert (padb.get_rho(8, {}) == 0.0).all()

def test_io_atdens():
    padb = ProAtomDB.from_file(context.get_fn('test/pro.atdens'))
    assert padb.get_numbers() == [16]
    assert padb.get_charges(16) == [3, 2]
    r = padb.get_record(16, 3)
    assert abs(r.rho[0] - 0.2628105459E+04) < 1e-5
    assert abs(r.rho[-1] - 0.1998952826E-16) < 1e-5
    s = padb.get_spline(16, 3)
    assert abs(s(np.array([0.0])) - 2661.68659449) < 1e-5
    radii = r.rgrid.rtransform.get_radii()
    assert radii[0] == 0.5216488380E-03
    assert abs(radii[-1] - 20) < 1e-14
    assert abs(radii[1] - 0.5442350204E-03) < 1e-8
    assert abs(r.rgrid.integrate(r.rho) - 13) < 1e-3
    # check the basics of the get_rho method (charge)
    rho1 = padb.get_rho(16, 3)
    rho2, deriv = padb.get_rho(16, 3, do_deriv=True)
    assert (rho1 == rho2).all()
    assert deriv is None
    # check the basics of the get_rho method (dict)
    rho1 = padb.get_rho(16, {3:1})
    rho2, deriv = padb.get_rho(16, {3:1}, do_deriv=True)
    assert (rho1 == rho2).all()
    assert deriv is None


'''
Author     : knight_byte
File       : A_Die_Roll.py
Created on : 2021-04-14 09:25:32
'''
from fractions import Fraction

def main():
    y, w = map(int, input().split())
    d = 6-max(y, w)+1
    print(Fraction(d, 6) if d != 6 else "1/1")

if __name__ == '__main__':
    main()


from util import logging
from training.util import monitor_loss

import tensorflow as tf
import streamlit as st
import os
import time
import numpy as np
import pandas as pd

def batch_loss(model, inp, aux, targ, loss_funct, opt = None):
    loss = 0
    with tf.GradientTape() as tape:
        pred = model(inp, aux, training=True)
        loss = loss_funct(targ, pred)
    if opt is not None:
        variables = model.trainable_variables
        gradients = tape.gradient(loss, variables)
        opt.apply_gradients(zip(gradients, variables))

        #save model eager tf
        checkpoint_dir = 'trained_model_dir'
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)

        checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
        root = tf.train.Checkpoint(optimizer=opt,
                                    model=model)
        root.save(checkpoint_prefix)
    return loss

def training(model, nb_epochs, step_epoch, train_set, valid_set, loss_fct, valid_loss_fct, opt, patience, min_delta):

    # Keep results for plotting
    train_loss_results = []
    valid_loss_results = []
    steps_per_epoch = step_epoch

    # early stopping
    patience_cnt = 0

    logging.info('Training started...')
    start = time.time()
    df = pd.DataFrame({'Loss': [], 'Loss Val': []})
    chart = st.line_chart(df)

    for epoch in range(nb_epochs):

        ## training
        epoch_loss_avg = tf.keras.metrics.Mean()
        for (batch, (inp_tot, targ)) in enumerate(train_set.take(steps_per_epoch)):
            # define encoder and decoder inputs
            inp, aux = inp_tot[0], inp_tot[1]
            # loss for batch
            batch_loss_results = batch_loss(model, inp, aux, targ, loss_fct, opt)
            # training progress
            epoch_loss_avg.update_state(batch_loss_results)
        # collect training loss values
        train_loss_results.append(epoch_loss_avg.result())
        _Loss = epoch_loss_avg.result().numpy()

        ## validation
        epoch_valid_loss_avg = tf.keras.metrics.Mean()
        for (batch, (inp_tot, targ)) in enumerate(valid_set.take(steps_per_epoch)):
            inp, aux = inp_tot[0], inp_tot[1]
            batch_loss_results = batch_loss(model, inp, aux, targ, valid_loss_fct, None)
            epoch_valid_loss_avg.update_state(batch_loss_results)
        # collect training loss values
        valid_loss_results.append(epoch_valid_loss_avg.result())
        ValLoss = epoch_valid_loss_avg.result().numpy()

        df = pd.DataFrame({'Loss': [_Loss],
                           'Loss Val': [ValLoss]})
        chart.add_rows(df)

        # early stopping
        patience_cnt = monitor_loss(epoch, valid_loss_results, min_delta, patience_cnt)

        if patience_cnt > patience:
            logging.info("early stopping...")
            break

        if epoch % 50 == 0: #logging.info
            st.text("Epoch {}: Loss MAE: {:.5f} --- Val Loss MAE: {:.5f}".format(epoch,
                                                                        epoch_loss_avg.result(),
                                                                        epoch_valid_loss_avg.result()))

    logging.info('Time taken to train {} sec\n'.format(time.time() - start))
    logging.info('Training finished...')

    return model


import cv2
import numpy as np

img = np.zeros((600, 600, 3), np.uint8) #全为0表示是黑色

"""
img.shape[1]代表图片的宽度
img.shape[0]代表图片的高度(长度)
"""

#画直线
cv2.line(img, (0,0), (img.shape[1], img.shape[0]), (255,255,255), 2) #参数：物件、起始座标、终点座标、颜色、粗度

#画方型 (cv2.FILLED代表填满)
cv2.rectangle(img, (100, 100), (200, 200), (0,255,255), 2)
cv2.rectangle(img, (400, 400), (500, 500), (0,255,255), cv2.FILLED) #参数：物件、起始座标、终点座标、颜色、粗度

#画圆形
cv2.circle(img, (300, 300), 100, (0,0,255), 2) #参数：物件、圆心、半径、颜色、粗度

#画椭圆形
cv2.ellipse(img , (300, 300), (50, 100), 45, 0, 360, (255, 0, 0), cv2.FILLED) #参数：物件、(水平半轴,垂直半轴)、旋转角度、起始角度、终止角度、颜色、粗度

#写字(不支援中文)
cv2.putText(img, "Hello", (300, 300), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 1) #参数：物件、文字、文字左下方的座标、字体、文字大小、颜色、粗度

cv2.imshow("img", img)
cv2.waitKey(0)


from flask import Flask, render_template, request, redirect, session
app = Flask(__name__)
app.secret_key = "No secrets on github or youtube"

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/submit', methods=['POST'])
def submit():
    print(request.form)
    session['name'] = request.form['name']
    session['age'] = request.form['age']
    return redirect('/display')

@app.route('/display')
def display():
    name = session['name']
    return render_template('display.html', name=name, age=submit['age'])

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


import discord
import os
from data_loader import input_text_parser
from data_loader import input_image_parser

image_path = "./images"
text_path = "./text"

token = open("token.txt", "r").read()

# change cwd in case this is called from shell script
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# create data objects, reading in data
text_data = input_text_parser(text_path)
file_data = input_image_parser(image_path)

client = discord.Client(intents=discord.Intents.all())

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord')

@client.event
async def on_message(message):
   """
   Main work method, called whenever someone posts a message
   """
   # don't respond to self, empty messages, or messages that don't start with a bang
   if message.author == client.user or \
      len(message.content) == 0 or \
      message.content[0] != '!':
      return
   out_str = None
   out_file = None
   # clean message
   cmd = message.content[1:].strip().lower()

   # bot info
   if cmd == "encyclopedia bottanica":
      out_str = "I am an encyclopedia. Enter '!toc' or '!contents' for a table of contents.\n" + \
         "Enter '!<entry name>' to view an entry. I am case-insensitive."

   # user requests table of contents
   if cmd == "contents" or cmd == "toc":
      out_str = f"I have information on the following categories:\n{text_data.get_contents()}\n" + \
         f"and can share the following files:\n{file_data.get_contents()}"
   else:
      # get usual output
      out_str = text_data.get(cmd)
      out_file = file_data.get(cmd)

   # print results
   if out_file != None:
      await message.channel.send(file=out_file)
   if out_str != None:
      await message.channel.send(out_str)

# fire this bad boy up
client.run(token)


from django.shortcuts import render, HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth import login, authenticate, logout
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.views.generic import View
from django.utils.decorators import method_decorator
from django.http import (
    JsonResponse
)
from django.core import serializers

from second.models import Message

class AddMessageView(View):

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(AddMessageView, self).dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        new_msg = Message()
        new_msg.from_user = request.user
        try:
            to_user = User.objects.get(username=request.POST['to'])
        except:
            response = {'success':False, 'msg':'To user does not exist'}
            return JsonResponse(response)
        new_msg.to_user = to_user
        new_msg.text = request.POST['text']
        new_msg.save()
        return JsonResponse({'success':True, 'msg':"message successfully sent"})

class GetAllMessages(View):

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(GetAllMessages, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        user = request.user
        username = request.GET['user']
        try:
            other_user = User.objects.get(username=username)
        except:
            response = {'success':False, 'msg': 'User does not exist'}
            return JsonResponse(response)

        msgs = list(Message.objects.filter(from_user=user, to_user=other_user))
        msgs_tmp = list(Message.objects.filter(from_user=other_user, to_user=user))
        msgs+=msgs_tmp
        msgs.sort(key=lambda x: x.time, reverse=True)
        all_msgs = [msg.to_dict() for msg in msgs]
        return JsonResponse(all_msgs, safe=False)

class GetAllUsers(View):

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(GetAllUsers, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        user = request.user
        msgs = Message.objects.filter(from_user=user)
        users = [msg.to_user for msg in msgs]
        msgs = Message.objects.filter(to_user=user)
        users_tmp = [msg.from_user for msg in msgs]
        users+=users_tmp
        users = list(set(users))
        #  users = serializers.serialize('json', users)
        users = [{"username":user.username, "name":
                  user.first_name+" "+user.last_name} for user in users]
        return JsonResponse(users, safe=False)


import xml.etree.ElementTree as ET

from fastapi import FastAPI, Path
from fastapi.responses import Response

app = FastAPI()

@app.get("/IF01/{name}")
async def get(
    name: str = Path(title="名前"),
):
    root = ET.Element("root")

    # 「番号タグ」というどうしようもないゴミ
    nameElement = ET.SubElement(root, "DT0001")
    nameElement.text = name

    birthday = ET.SubElement(root, "DT0002")
    birthday.text = "19700101"

    address = ET.SubElement(root, "DT0003")
    address.text = "神奈川県横浜市以下略"

    gender = ET.SubElement(root, "DT0004")
    gender.text = "1"

    xml_data = ET.tostring(root, encoding="shift_jis", xml_declaration=True)
    response = Response(content=xml_data, media_type="application/xml; charset=shift_jis")
    return response


import pickle
from typing import List, Tuple

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

def plot_segments(
    true_segment_list: List[Tuple[float, float]],
    pred_segment_list: List[Tuple[float, float]],
    t_min: float = None,
    t_max: float = None,
    zoom: bool = True,
    marker_list: List[int] = None,
):
    """Display true and predicted timecodes on a common timeline.

    :param true_segment_list: list of groundtruth timecodes.
    :param pred_segment_list: list of predicted timecodes.
    :param t_min: timecode from which starting the timeline.
    :param t_max: timecode to which ending the timeline.
    :param zoom: wether to display the diagram in a "zoom" fashion or not
        (ie: with details, the timeline should be short).
    :param marker_list: list of markers to add on the diagram (gray lines).
    """
    true_segment_list = sorted(true_segment_list)
    pred_segment_list = sorted(pred_segment_list)

    x_max = max(true_segment_list[-1][-1], pred_segment_list[-1][-1])
    t_min = 0 if not t_min else t_min
    t_max = x_max if not t_max else t_max

    true_segment_list = [
        [t1, t2]
        for t1, t2 in true_segment_list
        if (t1 >= t_min) and (t2 <= t_max)
    ]
    pred_segment_list = [
        [t1, t2]
        for t1, t2 in pred_segment_list
        if (t1 >= t_min) and (t2 <= t_max)
    ]

    plt.figure(figsize=(20, 5))
    prev_x_min = t_min
    for x_min, x_max in true_segment_list:
        if zoom:
            plt.vlines(x_min, 1, 2, color="#e41a1c", linestyles="dashed")
            plt.vlines(x_max, 1, 2, color="#e41a1c", linestyles="dashed")
            plt.fill_between([x_min, x_max], 1, 2, color="#e41a1c", alpha=0.1)
            plt.hlines(
                1,
                prev_x_min,
                x_min,
                color="black",
                linewidth=2,
                linestyles="dashed",
            )
        plt.hlines(1, x_min, x_max, color="#e41a1c", linewidth=4)
        prev_x_min = x_max
    plt.hlines(
        1, x_max, t_max, color="black", linewidth=2, linestyles="dashed"
    )

    prev_x_min = t_min
    for x_min, x_max in pred_segment_list:
        if zoom:
            plt.vlines(x_min, 1, 2, color="#377eb8", linestyles="dashed")
            plt.vlines(x_max, 1, 2, color="#377eb8", linestyles="dashed")
            plt.fill_between([x_min, x_max], 1, 2, color="#377eb8", alpha=0.1)
            plt.hlines(
                2,
                prev_x_min,
                x_min,
                color="black",
                linewidth=2,
                linestyles="dashed",
            )
        plt.hlines(2, x_min, x_max, color="#377eb8", linewidth=4)
        prev_x_min = x_max
    plt.hlines(
        2, x_max, t_max, color="black", linewidth=2, linestyles="dashed"
    )

    if marker_list is not None:
        marker_list = [t for t in marker_list if (t >= t_min) and (t <= t_max)]
        for timecode in marker_list:
            plt.vlines(timecode, 1, 2, color="#000000")

    pred_legend = mpatches.Patch(color="#e41a1c", label="pred")
    true_legend = mpatches.Patch(color="#377eb8", label="true")
    plt.legend(handles=[pred_legend, true_legend], loc=6)
    plt.show()

def load_labels(label_path: str) -> List[Tuple[float, float]]:
    """Load a Friends label file and extract laugther timecodes."""
    labels = pickle.load(open(label_path, "rb"))

    true_timecodes = []
    for segment in labels.values():
        if segment[-1][-10:-2].lower() == "laughter":
            true_timecodes.append(segment[:2])

    return sorted(true_timecodes)

def load_preds(pred_path: str) -> List[Tuple[float, float]]:
    """Load a prediction file with laugther timecodes."""
    preds = pickle.load(open(pred_path, "rb"))
    return sorted(preds)


from csv import DictReader
from glob import glob
from random import choices
from datetime import date
import numpy as np
import matplotlib.pyplot as plt
from statistics import mean
from dataclasses import dataclass

'''

for traders with less than 25k in their brokerage accounts, three day trades are allowed per five day rolling period

this means, in effect, that three day trades are allowed per week as long as they are performed on the same days each week

Which days?

I will rule out Mondays since many market holidays fall on Mondays

The number of market holidays falling on each day are:
Monday: 19
Tuesday: 1
Wednesday: 2
Thursday: 3
Friday: 3

So, Tuesdays and Wednesdays are in.

Fridays have shortened days each November on black friday.

So, if day trading on exactly the same three weekdays each week, it seems that Tuesday, Wednesday, and Thursday are optimal.

For traders with 25k or more in their brokerage accounts, it would seem that there is no limitation on number of day trades.

'''

csv_files = glob('./data-files/**/*.csv')

quotes = {}

@dataclass
class Quote:
  open_price: float
  high_price: float
  close_price: float
  trade_date: date

for csv_file in csv_files:
  with open(csv_file, newline='') as f:
    quote_reader = DictReader(f)
    quote = []
    for row in quote_reader:
      trade_date = date.fromisoformat(row['Date'])
      if trade_date.weekday() in (1, 2, 3):
        try:
          quote.append(Quote(float(row['Open']), float(row['High']), float(row['Close']), trade_date))
        except:
          print(csv_file)
          print(row)
    if (len(quote) != 155):
      print(csv_file)
      print(len(quote))
    else:
      quotes[csv_file.split('.')[1].split('\\')[2]] = quote

# sample_symbol = 'INDB'

trading_days = 155

simulation_size = 500000

# targets = [{ 'target': 1.005 + 0.0000005 * i, 'running total': [1000.0 for _ in range(simulation_size)] } for i in range(10000)]

# print(len([quotes[equity] for equity in quotes.keys()]))

running_totals = [1000.0 for _ in range(simulation_size)]

target = 1.008778

print(len(quotes.keys()))

for i in range(trading_days):
  equities = choices([key for key in quotes.keys()], k=simulation_size)
  for (j, equity) in enumerate(equities):
    quote = quotes[equity][i]
    entry_price = quote.open_price
    position_entry_shares = running_totals[j] / entry_price
    target_price = entry_price * target
    if target_price <= quote.high_price:
      position_exit = position_entry_shares * target_price
    else:
      position_exit = position_entry_shares * quote.close_price
    if position_exit > 500:
      # regulatory transaction fee
      position_exit -= position_exit * 22.9 / 1000000.0
    if position_entry_shares > 50:
      # trading activity fee
      position_exit -= 0.00013 * position_entry_shares
    running_totals[j] = position_exit

# print(sorted(running_totals))

print(f'less than 700: {len([x for x in running_totals if x < 700])}')
print(f'at least 700 and less than 800: {len([x for x in running_totals if x >= 700 and x < 800])}')
print(f'at least 800 and less than 900: {len([x for x in running_totals if x >= 800 and x < 900])}')
print(f'at least 900 and less than 1000: {len([x for x in running_totals if x >= 900 and x < 1000])}')
print(f'at least 1000 and less than 1100: {len([x for x in running_totals if x >= 1000 and x < 1100])}')
print(f'at least 1100 and less than 1200: {len([x for x in running_totals if x >= 1100 and x < 1200])}')
print(f'at least 1200 and less than 1300: {len([x for x in running_totals if x >= 1200 and x < 1300])}')
print(f'at least 1300 and less than 1400: {len([x for x in running_totals if x >= 1300 and x < 1400])}')
print(f'at least 1400: {len([x for x in running_totals if x >= 1400])}')

'''
x = [st['target'] for st in targets]
y = [mean(st['running total']) for st in targets]
plt.scatter(x, y)
plt.show()
'''


# coding=utf-8
from __future__ import unicode_literals

from django.contrib import admin
from django.http import HttpResponseRedirect
from django.conf.urls import url
from django.utils.html import format_html
from django.core.urlresolvers import reverse

from ordered_model.admin import OrderedModelAdmin
from monitoreo.apps.dashboard.models import IndicatorType, TableColumn

from monitoreo.apps.dashboard.admin.utils import switch

@admin.register(TableColumn)
class TableColumnAdmin(OrderedModelAdmin):
    list_display = ('full_name', 'move_up_down_links')

@admin.register(IndicatorType)
class IndicatorTypeAdmin(OrderedModelAdmin):
    list_display = ('nombre', 'order', 'resumen', 'mostrar',
                    'series_red', 'series_nodos', 'series_federadores',
                    'panel_red', 'panel_nodos', 'panel_federadores',
                    'move_up_down_links', 'position_actions')
    list_filter = ('resumen', 'mostrar')
    actions = ('queryset_to_top', 'queryset_to_bottom',
               'summarize', 'desummarize',
               'show', 'hide',
               'add_to_aggregated_series', 'remove_from_aggregated_series',
               'add_to_nodes_series', 'remove_from_nodes_series',
               'add_to_indexing_series', 'remove_from_indexing_series',
               'add_to_aggregated_panel', 'remove_from_aggregated_panel',
               'add_to_nodes_panel', 'remove_from_nodes_panel',
               'add_to_federators_panel', 'remove_from_federators_panel'
               )

    def get_urls(self):
        urls = super(IndicatorTypeAdmin, self).get_urls()
        extra_urls = [url(r'^(?P<model_id>.+)/(?P<direction>top|bottom)/$',
                          self.order_move, name='order_move'), ]
        return extra_urls + urls

    def position_actions(self, obj):
        return format_html(
            'Tope '
            'Fondo',
            reverse('admin:order_move', args=[obj.pk, 'top']),
            reverse('admin:order_move', args=[obj.pk, 'bottom']),
        )
    position_actions.short_description = 'Posicionamientos'
    position_actions.allow_tags = True

    def order_move(self, request, model_id, direction):
        indicator_type = IndicatorType.objects.get(pk=model_id)
        if direction == 'top':
            indicator_type.top()
        elif direction == 'bottom':
            indicator_type.bottom()
        indicator_type.save()

        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

    summarize = switch({'resumen': True})
    summarize.short_description = 'Agregar al resumen'

    desummarize = switch({'resumen': False})
    desummarize.short_description = 'Quitar del resumen'

    show = switch({'mostrar': True})
    show.short_description = 'Agregar al reporte'

    hide = switch({'mostrar': False})
    hide.short_description = 'Quitar del reporte'

    add_to_aggregated_series = switch({'series_red': True})
    add_to_aggregated_series.short_description =\
        'Agregar a las series de tiempo de red'

    remove_from_aggregated_series = switch({'series_red': False})
    remove_from_aggregated_series.short_description =\
        'Quitar de las series de tiempo de red'

    add_to_nodes_series = switch({'series_nodos': True})
    add_to_nodes_series.short_description = \
        'Agregar a las series de tiempo de nodos'

    remove_from_nodes_series = switch({'series_nodos': False})
    remove_from_nodes_series.short_description = \
        'Quitar de las series de tiempo de nodos'

    add_to_indexing_series = switch({'series_federadores': True})
    add_to_indexing_series.short_description = \
        'Agregar a las series de tiempo de nodos federadores'

    remove_from_indexing_series = switch({'series_federadores': False})
    remove_from_indexing_series.short_description = \
        'Quitar de las series de tiempo de nodos federadores'

    add_to_aggregated_panel = switch({'panel_red': True})
    add_to_aggregated_panel.short_description = \
        'Agregar al panel de indicadores de red'

    remove_from_aggregated_panel = switch({'panel_red': False})
    remove_from_aggregated_panel.short_description = \
        'Quitar del panel de indicadores de red'

    add_to_nodes_panel = switch({'panel_nodos': True})
    add_to_nodes_panel.short_description = \
        'Agregar al panel de indicadores de nodos'

    remove_from_nodes_panel = switch({'panel_nodos': False})
    remove_from_nodes_panel.short_description = \
        'Quitar del panel de indicadores de nodos'

    add_to_federators_panel = switch({'panel_federadores': True})
    add_to_federators_panel.short_description = \
        'Agregar al panel de indicadores de nodos federadores'

    remove_from_federators_panel = switch({'panel_federadores': False})
    remove_from_federators_panel.short_description = \
        'Quitar del panel de indicadores de nodos federadores'


from flask import Flask, render_template, session, request, url_for, redirect, flash

app = Flask(__name__)

@app.route("/")
def hello():
    print ("hello there")
    return render_template("home.html")

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


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import shutil
import subprocess
import sys

from setuptools import setup, find_packages, Distribution
import setuptools.command.build_ext as _build_ext

# Ideally, we could include these files by putting them in a
# MANIFEST.in or using the package_data argument to setup, but the
# MANIFEST.in gets applied at the very beginning when setup.py runs
# before these files have been created, so we have to move the files
# manually.
ray_files = [
    "ray/core/src/common/thirdparty/redis/src/redis-server",
    "ray/core/src/common/redis_module/libray_redis_module.so",
    "ray/core/src/plasma/plasma_store",
    "ray/core/src/plasma/plasma_manager",
    "ray/core/src/local_scheduler/local_scheduler",
    "ray/core/src/local_scheduler/liblocal_scheduler_library.so",
    "ray/core/src/global_scheduler/global_scheduler",
    "ray/WebUI.ipynb"
]

optional_ray_files = []

ray_ui_files = [
    "ray/core/src/catapult_files/index.html",
    "ray/core/src/catapult_files/trace_viewer_full.html"
]

ray_autoscaler_files = [
    "ray/autoscaler/aws/example-full.yaml"
]

# The UI files are mandatory if the INCLUDE_UI environment variable equals 1.
# Otherwise, they are optional.
if "INCLUDE_UI" in os.environ and os.environ["INCLUDE_UI"] == "1":
    ray_files += ray_ui_files
else:
    optional_ray_files += ray_ui_files

optional_ray_files += ray_autoscaler_files

extras = {
    "rllib": [
        "tensorflow", "pyyaml", "gym[atari]", "opencv-python",
        "python-snappy", "scipy"]
}

class build_ext(_build_ext.build_ext):
    def run(self):
        # Note: We are passing in sys.executable so that we use the same
        # version of Python to build pyarrow inside the build.sh script. Note
        # that certain flags will not be passed along such as --user or sudo.
        # TODO(rkn): Fix this.
        subprocess.check_call(["../build.sh", sys.executable])

        # We also need to install pyarrow along with Ray, so make sure that the
        # relevant non-Python pyarrow files get copied.
        pyarrow_files = [
            os.path.join("ray/pyarrow_files/pyarrow", filename)
            for filename in os.listdir("./ray/pyarrow_files/pyarrow")
            if not os.path.isdir(os.path.join("ray/pyarrow_files/pyarrow",
                                              filename))]

        files_to_include = ray_files + pyarrow_files

        for filename in files_to_include:
            self.move_file(filename)
        # Copy over the autogenerated flatbuffer Python bindings.
        generated_python_directory = "ray/core/generated"
        for filename in os.listdir(generated_python_directory):
            if filename[-3:] == ".py":
                self.move_file(os.path.join(generated_python_directory,
                                            filename))

        # Try to copy over the optional files.
        for filename in optional_ray_files:
            try:
                self.move_file(filename)
            except Exception as e:
                print("Failed to copy optional file {}. This is ok."
                      .format(filename))

    def move_file(self, filename):
        # TODO(rkn): This feels very brittle. It may not handle all cases. See
        # https://github.com/apache/arrow/blob/master/python/setup.py for an
        # example.
        source = filename
        destination = os.path.join(self.build_lib, filename)
        # Create the target directory if it doesn't already exist.
        parent_directory = os.path.dirname(destination)
        if not os.path.exists(parent_directory):
            os.makedirs(parent_directory)
        print("Copying {} to {}.".format(source, destination))
        shutil.copy(source, destination)

class BinaryDistribution(Distribution):
    def has_ext_modules(self):
        return True

setup(name="ray",
      # The version string is also in __init__.py. TODO(pcm): Fix this.
      version="0.3.1",
      packages=find_packages(),
      cmdclass={"build_ext": build_ext},
      # The BinaryDistribution argument triggers build_ext.
      distclass=BinaryDistribution,
      install_requires=["numpy",
                        "funcsigs",
                        "click",
                        "colorama",
                        "psutil",
                        "pytest",
                        "pyyaml",
                        "redis",
                        # The six module is required by pyarrow.
                        "six >= 1.0.0",
                        "flatbuffers"],
      setup_requires=["cython >= 0.23"],
      extras_require=extras,
      entry_points={"console_scripts": ["ray=ray.scripts.scripts:main"]},
      include_package_data=True,
      zip_safe=False,
      license="Apache 2.0")


import logging
import time
import uuid
from queue import Queue
from threading import Event, Thread

import zmq

class ControlClient(Thread):
    def __init__(self, port: int):
        super(ControlClient, self).__init__()
        self.daemon = True
        self.command_queue = Queue()
        self.command_return = {}
        self.command_return_popque = Queue()
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REQ)
        self.port = port
        self.timeout = 5000

        self.active = Event()

    def execute_command(self, device, command, args, kwargs, id=None):
        self.socket.send_json([device, command, args, kwargs])

        if (self.socket.poll(self.timeout) & zmq.POLLIN) != 0:
            status, retval = self.socket.recv_json()
            if status == "OK":
                return retval
            else:
                logging.warning(
                    f"{device} networking warning in "
                    + f"ExecuteNetworkCommand : error for {command} -> {retval}"
                )
                return "Error"

    def run(self):
        self.socket.connect(f"tcp://localhost:{self.port}")
        while self.active.is_set():
            while not self.command_queue.empty():
                id, device, command, args, kwargs = self.command_queue.get()
                ret = self.execute_command(device, command, args, kwargs)
                self.command_return[id] = ret

            while not self.command_return_popque.empty():
                self.command_return.pop(self.command_return_popque.get())

            time.sleep(1e-6)

def send_command(
    control_client: ControlClient, device, command, args, kwargs, wait_return=False
):
    id = uuid.uuid1().int >> 64
    control_client.command_queue.put([id, device, command, args, kwargs])
    if wait_return:
        while True:
            if id in control_client.command_return:
                ret = control_client.command_return.pop(id)
                return ret
            time.sleep(1e-6)
    else:
        control_client.command_return_popque.put(id)
    return