from typing import List

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        if len(nums) < 2: return len(nums)

        j = 0

        for n in nums[1:]:
            if n != nums[j]:
                j += 1
                nums[j] = n

        print(nums)
        return j + 1

if __name__ == "__main__":
    l = [[1, 1, 2], [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]]

    for nums in l:
        print(Solution().removeDuplicates(nums))

LeetcodeEasy/26. Remove Duplicates from Sorted Array.py


from itertools import product
from numpy import array, identity, dot, zeros
from numpy.linalg import inv as inverse
from pytriqs.gf.local import BlockGf, GfImFreq
from pytriqs.operators import c as C, c_dag as C_dag

from .coulombinteraction import CoulombTensorHubbard, NNCoulombTensorHubbard
from .other import sum_list, m_transform

class ClustersiteTransformation():
    """
    transforms the cluster objects required for the impurity problem within cdmft
    for the 1-orbital Hubbard model
    on Matsubara frequencies
    g_struct or gf_struct has an order, dict() it to make it TRIQS(CTHYB) compatible
    written and tested for unitary transformations
    """
    def __init__(self, g_transf_struct, transformation, beta, n_iw, blocks, blockstates, *args, **kwargs):
        self.transf_mat = array(transformation)
        self.blocks = blocks
        self.g_struct = [(block, blockstates) for block in blocks]
        if g_transf_struct:
            self.g_transf_struct = g_transf_struct
        else:
            assert 'g_loc' in kwargs.keys(), 'ClusterTransformation needs either g_transf_struct or g_loc to init'
            g_loc = kwargs['g_loc']
            self.g_transf_struct = transf_indices(g_loc, transformation, self.blocks)
        name_blocks = [(ind, GfImFreq(indices = dict(self.g_transf_struct)[ind],
                                      beta = beta,
                                      n_points = n_iw)
                        ) for ind in dict(self.g_transf_struct).keys()]
        self.g_iw = BlockGf(name_block_generator = name_blocks,
                            make_copies = True,
                            name = '$\\tilde{G}_c$')
        self.g_0_iw = BlockGf(name_block_generator = name_blocks,
                              make_copies = True,
                              name = '$\\tilde{\\mathrm{G}}$')
        self.sigma_iw = BlockGf(name_block_generator = name_blocks,
                                make_copies = True,
                                name = '$\\tilde{\\Sigma}_c$')

    def set_hamiltonian(self, u_hubbard, mu, u_hubbard_non_loc, blocks, *args, **kwargs):
        """
        transforms H_loc
        express operators in site-basis by operators in new basis
        u: Hubbard interaction, can also be non-local(matrix-valued) within the cluster
        mu: chemical potential that goes explicitly into the Hamiltonian (i.e. mu_DC)
        """
        dim = len(self.transf_mat)
        u = self.transf_mat
        u_inv = inverse(self.transf_mat)
        u_c = CoulombTensorHubbard(u_hubbard, dim, blocks)
        u_c_transf = u_c.transform(u)
        if u_hubbard_non_loc:
            u_c_nl = NNCoulombTensorHubbard(u_hubbard_non_loc, dim, blocks)
            u_c_nl_transf = u_c_nl.transform(u)
        assert dot(u, u_inv).all() == identity(len(u)).all(), 'transformation not unitary'
        blockstates = range(dim)
        mu_matrix = - mu * identity(dim)
        self.hamiltonian = sum_list([sum_list([sum_list([self._unblocked_c_dag(s, i) * m_transform(mu_matrix, u, i, j) * self._unblocked_c(s, j) for j in blockstates]) for i in blockstates]) for s in self.blocks])
        for i, j, k, l, s1, s2 in product(*[blockstates]*4 + [self.blocks]*2):
            self.hamiltonian += u_c_transf[i, j, k, l, s1, s2] * self._unblocked_c_dag(s1, i) * self._unblocked_c_dag(s2, j) *  self._unblocked_c(s2, l) * self._unblocked_c(s1, k)
        if u_hubbard_non_loc:
            for i, j, k, l, s1, s2 in product(*[blockstates]*4 + [self.blocks]*2):
                self.hamiltonian += u_c_nl_transf[i, j, k, l, s1, s2] * self._unblocked_c_dag(s1, i) * self._unblocked_c_dag(s2, j) *  self._unblocked_c(s2, l) * self._unblocked_c(s1, k)

    def get_hamiltonian(self):
        return self.hamiltonian

    def get_g_struct(self):
        return self.g_transf_struct

    def _unblocked_c(self, s, i, dag = False):
        if dag:
            return C_dag(*self._transf_singleparticlestate(s, i))
        else:
            return C(*self._transf_singleparticlestate(s, i))

    def _unblocked_c_dag(self, s, i):
        return self._unblocked_c(s, i, dag = True)

    def _transf_singleparticlestate(self, block, blockstate):
        i = 0
        snr = self._singleparticlestate_nr(block, blockstate)
        for bdata in self.g_transf_struct:
            for bs in bdata[1]:
                if i == snr:
                    return [bdata[0], bs]
                else:
                    i += 1

    def _singleparticlestate_nr(self, block, blockstate):
        i = 0
        for bdata in self.g_struct:
            for bs in bdata[1]:
                if bdata[0] == block and bs == blockstate:
                    return i
                else:
                    i += 1
        assert False, "no singleparticlestate number found"

    def transform(self, g):
        return g_transf(g, self.transf_mat, self.g_transf_struct, self.blocks)

    def backtransform(self, g):
        return g_c(g, self.transf_mat, self.g_transf_struct, self.blocks)

    def set_dmft_objs(self, g0, g, sigma, *args, **kwargs):
        """sets Weiss-Field, Green\'s function and self-energy at once"""
        self.set_g_0_iw(g0)
        self.set_g_iw(g)
        self.set_sigma_iw(sigma)

    def get_backtransformed_dmft_objs(self):
        """returns backtransformed Weiss-Field, Green\'s function and self-energy"""
        return g_c(self.g_0_iw, self.transf_mat, self.g_transf_struct, self.blocks), g_c(self.g_iw, self.transf_mat, self.g_transf_struct, self.blocks), g_c(self.sigma_iw, self.transf_mat, self.g_transf_struct, self.blocks)

    def set_g_0_iw(self, g):
        if [ind for ind in g.indices] == dict(self.g_transf_struct).keys():
            self.g_0_iw << g
        else:
            self.g_0_iw << g_transf(g, self.transf_mat, self.g_transf_struct, self.blocks)
    def get_g_0_iw(self):
        return self.g_0_iw
    def set_g_iw(self, g):
        if [ind for ind in g.indices] == dict(self.g_transf_struct).keys():
            self.g_iw << g
        else:
            self.g_iw << g_transf(g, self.transf_mat, self.g_transf_struct, self.blocks)
    def get_g_iw(self):
        return self.g_iw
    def set_sigma_iw(self, g):
        if [ind for ind in g.indices] == dict(self.g_transf_struct).keys():
            self.sigma_iw << g
        else:
            self.sigma_iw << g_transf(g, self.transf_mat, self.g_transf_struct, self.blocks)
    def get_sigma_iw(self):
        return self.sigma_iw

def transf_indices(g_c, transformation, blocks, almost_zero = 10e-9):
    """
    Finds the new blockstructure of G_loc using the unitary transformation u.
    transformation is a matrix over the cluster sites
    """
    n_sites = len(transformation)
    sites = range(n_sites)
    g_block_structure = zeros([n_sites, n_sites], int)
    u = array(transformation)
    u_inv = inverse(u)
    blockdiag_len = list()
    _blockdiag_len = 0
    for s, b in g_c:
        for i, l, m in product(sites, sites, range(len(b.data[:,0,0]))):
            if abs(sum_list([sum_list([u[i, j] * b.data[m, j, k] * u_inv[k, l] for j in sites]) for k in sites])) > almost_zero:
                g_block_structure[i, l] = 1
    for i in sites:
        block_is_zero = True
        for j in range(i, n_sites, 1):
            for k in range(0, i + 1, 1):
                if i != j and (g_block_structure[i - k, j] != 0 or g_block_structure[j, i - k] != 0):
                    block_is_zero = False
        if block_is_zero:
            blockdiag_len.append(_blockdiag_len)
            _blockdiag_len = 0
        else:
            _blockdiag_len += 1
    return [(s + '-' + str(i), range(blockdiag_len[i] + 1)) for s in blocks for i in range(len(blockdiag_len))]

def g_transf(g, transformation, transf_indices, blocks):
    u = array(transformation)
    u_inv = inverse(u)
    g_transf = BlockGf(name_block_generator = [(ind, GfImFreq(indices = dict(transf_indices)[ind], mesh = g[blocks[0]].mesh)) for ind in dict(transf_indices).keys()], make_copies = False)
    for ss in blocks:
        i = 0
        for s in [transf_indices[ii][0] for ii in range(len(transf_indices))]:
            if ss in s:
                for ib in dict(transf_indices)[s]:
                    for jb in dict(transf_indices)[s]:
                        g_transf[s][ib, jb] << sum_list([sum_list([u[i + ib, k] * g[ss][k, l] * u_inv[l, i + jb] for k in range(len(u))]) for l in range(len(u))])
                i += 1
    return g_transf

def g_c(g, transformation, transf_indices, blocks):
    u = array(transformation)
    u_inv = inverse(u)
    sites = range(len(u))
    g_c = BlockGf(name_block_generator = [(s, GfImFreq(indices = sites, mesh = g[transf_indices[0][0]].mesh)) for s in blocks], make_copies = False)
    for s, i, j in product(blocks, sites, sites):
        g_c[s][i, j] << sum_list([sum_list([u_inv[i, k] * _unblocked_g_transf(g, s, k, l, transf_indices) * u[l, j]  for k in sites]) for l in sites])
    return g_c

def _unblocked_g_transf(g, block, row, col, transf_indices):
    i0 = row if row <= col else col
    i1 = row if row > col else col
    ordered_keys = [transf_indices[ii][0] for ii in range(len(transf_indices))]
    j = 0
    for key in ordered_keys:
        if block in key:
            for k in dict(transf_indices)[key]:
                if i0 == j:
                    (block, ind1) = (key, k)
                j += 1
    if len(dict(transf_indices)[block]) > ind1 + i1 - i0:
        if row <= col:
            return g[block][ind1, ind1 + i1 - i0]
        else:
            return g[block][ind1 + i1 - i0, ind1]
    else:
        return 0


import argparse

import numpy as np
import cv2

from src.models import ORTModelLoader, DNNModelLoader
from src.general import run_yolov5_seg
from src.utils import check_file

def parse_opt():
    parser = argparse.ArgumentParser(description="Detect using YOLOv5 Segmentation model")
    required = parser.add_argument_group("required arguments")
    required.add_argument(
        "-m", "--model", type=str, required=True, help="YOLOv5 Segmentation onnx model path"
    )
    source = parser.add_argument_group("source arguments")
    source.add_argument("-i", "--image", type=str, help="Image source")
    source.add_argument("-v", "--video", type=str, help="Video source")

    parser.add_argument(
        "--topk",
        type=int,
        default=100,
        help="Integer representing the maximum number of boxes to be selected per class",
    )
    parser.add_argument(
        "--conf-tresh",
        type=float,
        default=0.2,
        help="Float representing the threshold for deciding when to remove boxes based on confidence score",
    )
    parser.add_argument(
        "--iou-tresh",
        type=float,
        default=0.45,
        help="Float representing the threshold for deciding whether boxes overlap too much with respect to IOU",
    )
    parser.add_argument(
        "--score-tresh",
        type=float,
        default=0.25,
        help="Float representing the threshold for deciding whether render boxes or not",
    )
    parser.add_argument(
        "--mask-tresh",
        type=float,
        default=0.5,
        help="Float representing the threshold for deciding mask area",
    )
    parser.add_argument(
        "--mask-alpha",
        type=float,
        default=0.4,
        help="Float representing the opacity of mask layer",
    )
    parser.add_argument(
        "--dnn",
        action="store_true",
        help="Use OpenCV DNN module [if false using onnxruntime] for backend",
    )

    opt = parser.parse_args()
    if opt.image is None and opt.video is None:
        raise argparse.ArgumentError("Please specify image or video source!")
    elif opt.image and opt.video:
        raise argparse.ArgumentError("Please specify either image or video source!")
    return opt

def main(opt) -> None:
    if opt.dnn:
        model = DNNModelLoader(opt.model)  # use Opencv DNN module
    else:
        model = ORTModelLoader(opt.model)  # use onnxruntime

    # warmup model
    _ = run_yolov5_seg(
        model,
        (np.random.rand(model.width, model.height, 3) * 255).astype(np.uint8),  # random image
        opt.conf_tresh,
        opt.iou_tresh,
        opt.score_tresh,
        opt.topk,
        opt.mask_tresh,
        opt.mask_alpha,
    )

    if opt.image:
        check_file(opt.image, "Image file not found!")

        # Image preprocessing
        img = cv2.imread(opt.image)
        img = run_yolov5_seg(
            model,
            img,
            opt.conf_tresh,
            opt.iou_tresh,
            opt.score_tresh,
            opt.topk,
            opt.mask_tresh,
            opt.mask_alpha,
        )

        cv2.imshow("output", img)
        cv2.waitKey(0)
    elif opt.video:
        # Video processing
        vid_source = 0 if opt.video == "0" else opt.video
        cap = cv2.VideoCapture(vid_source)

        while cap.isOpened():
            ret, frame = cap.read()

            if not ret:
                break

            frame = run_yolov5_seg(
                model,
                frame,
                opt.conf_tresh,
                opt.iou_tresh,
                opt.score_tresh,
                opt.topk,
                opt.mask_tresh,
                opt.mask_alpha,
            )

            cv2.imshow("output", frame)

            if cv2.waitKey(1) == ord("q"):
                break

        cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    opt = parse_opt()
    main(opt)


from dash import Dash, dcc, html
from dash.dependencies import Input, Output, State

app = Dash(__name__)

app.layout = html.Div([
    dcc.Input(id='input-1-state', type='text', value='Montréal'),
    dcc.Input(id='input-2-state', type='text', value='Canada'),
    html.Button(id='submit-button-state', n_clicks=0, children='Submit'),
    html.Div(id='output-state')
])

@app.callback(Output('output-state', 'children'),
              Input('submit-button-state', 'n_clicks'),
              State('input-1-state', 'value'),
              State('input-2-state', 'value'))
def update_output(n_clicks, input1, input2):
    return u'''
        The Button has been pressed {} times,
        Input 1 is "{}",
        and Input 2 is "{}"
    '''.format(n_clicks, input1, input2)

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

3. Dashboards Interativos com Python/Callbacks Avançados/states2.py


import sys

from ovito.io import export_file
from ovito.pipeline import Pipeline

from utils.argument_parser import parse_arguments
from utils.config import get_config
from utils.parser_utils import get_neighbors_data, get_particles_data
from utils.parser_utils import get_particles_static_source

def visualization_ovito(config_file: str):
    config = get_config(config_file)
    df = get_particles_data(config.dynamic_file, config.static_file)
    static_source = get_particles_static_source(df,
                                                get_neighbors_data(config.neighbors_file).get(config.particle),
                                                config.particle,
                                                config.R)

    pipeline = Pipeline(source=static_source)

    export_file(pipeline, '../results/visualization.dump', 'lammps/dump',
                columns=["Particle Identifier", "Position.X", "Position.Y", "Position.Z", "Radius", "Neighbor"])

if __name__ == "__main__":
    arguments = parse_arguments(sys.argv[1:])

    config_file = arguments['config_file']

    try:
        visualization_ovito(config_file)
    except FileNotFoundError as e:
        print("File not found")
        print(e)
    except OSError:
        print("Error occurred.")
    except KeyboardInterrupt:
        print('Program interrupted by user.')
    except Exception as e:
        print(e)


import cv2
from onnx_ort import Detector
image = cv2.imread("2.png")
# print(image)
import threading
import time
from concurrent.futures import ThreadPoolExecutor
import queue
import subprocess
import copy
# 创建一个5个线程的消费
class ThreadPoolExecutor(ThreadPoolExecutor):
    def __init__(self, max_workers=None, thread_name_prefix=''):
        super().__init__(max_workers, thread_name_prefix)
        # 队列大小为最大线程数的两倍
        self._work_queue = queue.Queue(self._max_workers * 2)

threadPool = ThreadPoolExecutor(max_workers=4)
obj=Detector(640,0.2,0.6,"zju-smokingFace.onnx")
obj1=Detector(640,0.2,0.6,"zju-smokingFace.onnx")
obj2=Detector(640,0.2,0.6,"zju-smokingFace.onnx")
obj3=Detector(640,0.2,0.6,"zju-smokingFace.onnx")
obj4=Detector(640,0.2,0.6,"zju-smokingFace.onnx")
start=time.time()
def job():
    obj.detect_out(image)
def job1():
    obj1.detect_out(image)
def job2():
    obj2.detect_out(image)
def job3():
    obj3.detect_out(image)
    #print(obj.detect_out(image))

pool=ThreadPoolExecutor(max_workers=4)
for i in range(1000):
    # job(obj)
    job1()
end=time.time()
print("耗费时间是%s"%(end-start))
# print(det.detect_out(image))
# print(det.detect_out(image))
# print(det.detect_out(image))
# print(det.detect_out(image))


#!/usr/bin/env python3

# https://github.com/colinmccormick/CarND-Advanced-Lane-Lines

import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import pickle
import random
#from IPython import get_ipython

# set parameters

#get_ipython().run_line_magic('matplotlib', 'inline')

# processing mode
TEST_MODE = False
RECORD = False

# image size
ROWS = 720
COLS = 1280
ROI_TOP = 0.65
ROI_BOTTOM = 0.90
ROI_LEFT = 0.10
ROI_RIGHT = 0.95
INSET_ROWS = int(0.4 * ROWS)
INSET_COLS = int(0.4 * COLS)

# colors and scales
RED_COLOR = (0,0,255)
GREEN_COLOR = (0,255,0)
XM_PER_PIX = 3.7 / 700
YM_PER_PIX = 15 / 720   # based on estimate of 5 white lane dashes fitting vertically in warped image; 3m each

# camera calibration
CHESS_NX = 9
CHESS_NY = 6

# lane pixel finding
L_THRESHOLD = [225,255]              # range for L channel in LUV color space (for white)
B_THRESHOLD = [190,255]              # range for B channel in LAB color space (for yellow)

# perspective transform
GB_KERNEL = 3                       # size of gaussian blur kernel
CANNY_LOW = 50                      # canny edge detection threshold (low)
CANNY_HIGH = 400                    # canny edge detection threshold (high)
HOUGH_DIST_RES = 0.5                # hough line finder distance resolution (pixels)
HOUGH_ANGLE_RES = 3.14159/180       # hough line finder angle resolution (rads)
HOUGH_THRESHOLD = 20                # hough line finder threshold
HOUGH_MIN_LINE = 60                 # hough line finder min line length (pixels)
HOUGH_MAX_GAP = 120                 # hough line finder max line gap (pixels)

# lane fitting
NUM_WINS = 8                        # number of sliding windows to use
WIN_WIDTH = 64                      # half total width of sliding window
WIN_HEIGHT = int(ROWS/NUM_WINS)     # total height of sliding window
FRAME_MEMORY = 4                    # number of past frames to remember
LANE_WIDTH_PIXELS = 810             # width of lane at bottom of image in pixels
FRAME_MARKER_THRESHOLD = 4          # frames to wait until new lane marker can reasonably appear

# speed measurement
METERS_PER_MARKER = 15.75           # meters between white lane markers (40 feet)

# directories and filenames
TEST_DIR = 'test_images'
CALIBRATION_DIR = 'camera_cal'
CALIBRATION_FILENAME = 'camera_calibration.pickle'
PERSPECTIVE_FILENAME = 'perspective_transform.pickle'

OUTPUT_DIR_CALIBRATION = 'output_images/calibration'
OUTPUT_DIR_S_CHANNEL = 'output_images/s_channel'
OUTPUT_DIR_X_GRAD = 'output_images/x_gradient'
OUTPUT_DIR_VP = 'output_images/vanishing_point'

OUTPUT_DIR_UNDISTORT = 'output_images/undistort'
OUTPUT_DIR_WARPED = 'output_images/warped'
OUTPUT_DIR_MARKER_PIXELS = 'output_images/marker_pixels'
OUTPUT_DIR_SLIDING_WINDOW = 'output_images/sliding_window'
OUTPUT_DIR_LANE_MARK = 'output_images/lane_mark'

class LaneLine(object):
    # class to hold info about each lane line

    def __init__(self,side):
        # side: 'left' or 'right'
        self.side = side
        self.goodFitLastFrame = False
        self.recentFits = np.zeros((FRAME_MEMORY,3),dtype=np.float32)      # second-order polynomial: 3 coefficients
        self.curvatureRadius = np.zeros((FRAME_MEMORY,),dtype=np.float32)
        self.framesSeenSinceMarker = 0
        self.estimatedSpeed = 0

    def calculateCurvature(self,f):
        # calculate radius of curvature; use some algebra to scale pixels to meters

        y0 = ROWS * YM_PER_PIX
        A = f[0] *  XM_PER_PIX / YM_PER_PIX**2
        B = f[1] * XM_PER_PIX / YM_PER_PIX

        curvature = ((1 + (2*A*y0 + B)**2)**1.5)/float(np.absolute(2*A))
        self.curvatureRadius = np.roll(self.curvatureRadius,1)
        self.curvatureRadius[0] = curvature

        return np.mean(self.curvatureRadius)
        # define camera calibration

def calibrateCamera():
    # use chessboard photos to calibrate camera

    # set termination criteria (for use with cv2.cornerSubPix)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    # make object points
    objp = np.zeros((CHESS_NX*CHESS_NY,3), np.float32)
    objp[:,:2] = np.mgrid[0:CHESS_NX,0:CHESS_NY].T.reshape(-1,2)
    imagePoints = []
    objectPoints = []

    # loop through calibration images
    for imageFile in os.listdir(CALIBRATION_DIR):
        if imageFile.endswith('.jpg'):

            # read image, grayscale, find corners
            im = cv2.imread(os.path.join(CALIBRATION_DIR,imageFile))
            gray = cv2.cvtColor(im,cv2.COLOR_RGB2GRAY)
            found, corners = cv2.findChessboardCorners(gray,(CHESS_NX,CHESS_NY),None)

            if found:
                # draw corners on image
                cv2.drawChessboardCorners(im,(CHESS_NX,CHESS_NY),corners,found)
                # find corners with higher (sub-pixel) precision
                corners2 = cv2.cornerSubPix(gray,corners,(5,5),(-1,-1),criteria)
                imagePoints.append(corners2)
                objectPoints.append(objp)

                # save image with corners marked
                cv2.imwrite(os.path.join(OUTPUT_DIR_CALIBRATION,imageFile),im)

    # do the actual calibration and save
    ret, mtx, dst, rvecs, tvecs = cv2.calibrateCamera(objectPoints,imagePoints,gray.shape[::-1],None,None)
    imSize = im.shape
    calibrationData = {'cam_matrix':mtx, 'dist_coeffs':dst, 'im_size':imSize}
    with open(CALIBRATION_FILENAME,'wb') as f:
        pickle.dump(calibrationData,f)

    return mtx,dst

# do camera calibration and display example images

print("Calibrating camera...")
mtx,dst = calibrateCamera()
filenameList = [fn for fn in os.listdir(CALIBRATION_DIR) if fn.endswith('.jpg')]
fn = random.choice(filenameList)
imOriginal = cv2.imread(os.path.join(CALIBRATION_DIR,fn))
imCorners = cv2.imread(os.path.join(OUTPUT_DIR_CALIBRATION,fn))

f = plt.figure(figsize=(30,40))
ax = f.add_subplot(1,2,1)
ax.imshow(imOriginal)
ax = f.add_subplot(1,2,2)
ax.imshow(imCorners)
plt.show()

# display undistorted image example

print("Undistorting image...")
fn = os.path.join(CALIBRATION_DIR,'calibration1.jpg')
imCB = cv2.imread(fn)
imCB = cv2.cvtColor(imCB,cv2.COLOR_BGR2RGB)
imCBUndistorted = cv2.undistort(imCB,mtx,dst)
f2 = plt.figure(figsize=(30,40))
ax = f2.add_subplot(1,2,1)
ax.imshow(imCB)
ax = f2.add_subplot(1,2,2)
ax.imshow(imCBUndistorted)
plt.show()

filenameList = [fn for fn in os.listdir(TEST_DIR) if fn.endswith('.jpg')]
fn = random.choice(filenameList)
imOriginal = cv2.imread(os.path.join(TEST_DIR,fn))
imOriginalRGB = cv2.cvtColor(imOriginal,cv2.COLOR_BGR2RGB)
imUndistorted = cv2.undistort(imOriginalRGB,mtx,dst)
f3 = plt.figure(figsize=(30,40))
ax = f3.add_subplot(1,2,1)
ax.imshow(imOriginalRGB)
ax = f3.add_subplot(1,2,2)
ax.imshow(imUndistorted)
plt.show()

# define lane pixel finder

def selectLanePixels(im, fn='frame.jpg'):
    # select pixels that are lane markers; assume RGB image; use LUV, LAB color spaces

    luv = cv2.cvtColor(im,cv2.COLOR_RGB2LUV)
    lab = cv2.cvtColor(im,cv2.COLOR_RGB2Lab)

    # find yellow lane pixels using LUV color space
    lChannel = luv[:,:,0]
    lBinary = np.zeros_like(lChannel)
    lBinary[ (lChannel > L_THRESHOLD[0]) & (lChannel <= L_THRESHOLD[1]) ] = 255

    # find white lane pixels using LAB color space
    # treat lab b-channel differently: normalize up to 255
    # however, skip this if max value is low (implying no yellow in the image)
    # thanks to jeremy shannon for this idea: https://github.com/jeremy-shannon/CarND-Advanced-Lane-Lines
    bChannel = lab[:,:,2]
    bMax = np.max(bChannel)
    if bMax > 175:
        bChannel = 255 * ( bChannel / bMax )
    bBinary = np.zeros_like(bChannel)
    bBinary[ (bChannel > B_THRESHOLD[0]) & (bChannel <= B_THRESHOLD[1]) ] = 255

    # make combined mask
    lanePixels = np.zeros_like(lChannel)
    lanePixels[ (lBinary > 0) | (bBinary > 0) ] = 255

    return lanePixels, lBinary, bBinary

# display example lane pixel finder

print("Finding example lane pixels...")

filenameList = [fn for fn in os.listdir(TEST_DIR) if fn.endswith('.jpg')]
fn = random.choice(filenameList)
imBGR = cv2.imread(os.path.join(TEST_DIR,fn))
imRGB = cv2.cvtColor(imBGR,cv2.COLOR_BGR2RGB)

lanePixels, lBinary, bBinary = selectLanePixels(imRGB,fn)

f = plt.figure(figsize=(30,40))
ax = f.add_subplot(1,2,1)
ax.imshow(imRGB)
ax = f.add_subplot(1,2,2)
ax.imshow(lanePixels,cmap='gray')
plt.show()

# define utility function for selecting roi

def selectRoi(im):
    # mask out any pixels outside of the roi

    #rows,cols = im.shape[:2]
    bottomLeft = [0,ROWS*ROI_BOTTOM]
    topLeft = [COLS*ROI_LEFT,ROWS*ROI_TOP]
    topRight = [COLS*ROI_RIGHT,ROWS*ROI_TOP]
    bottomRight = [COLS,ROWS*ROI_BOTTOM]
    vertices = np.array([[bottomLeft,topLeft,topRight,bottomRight]],dtype=np.int32)
    mask = np.zeros_like(im)
    cv2.fillPoly(mask,vertices,255)

    return cv2.bitwise_and(im,mask)

# define utility function for determining line intersection

def findIntersection(lines):
    # find the point minimizing lsq distance from all lines
    # if the lines all intersect, this is the intersection point

    numLines = len(lines)
    a = np.zeros((numLines,2),dtype=np.float32)
    b = np.zeros((numLines,),dtype=np.float32)
    for n,line in enumerate(lines):
        for x1,y1,x2,y2 in line:
            slope = (y2-y1) / float(x2-x1)
            a[n] = np.array([slope,-1],dtype=np.float32)
            b[n] = slope * x1 - y1     # this is -1 times the intercept
    return np.linalg.lstsq(a,b)[0]

# define utility function for finding vanishing point

def findVanishingPoint(mtx,dst):
    # determine vanishing point in image

    # analyze straight-road images used to find vanishing point
    straightImages = [os.path.join(TEST_DIR,f) for f in os.listdir(TEST_DIR) if 'straight' in f]
    vps = []
    for imFile in straightImages:
        im = cv2.imread(imFile)
        im = cv2.undistort(im,mtx,dst)

        # smooth with gaussian blur
        imSmoothed = cv2.GaussianBlur(im,(GB_KERNEL,GB_KERNEL),0)

        # find edges with canny
        edges = cv2.Canny(imSmoothed,CANNY_LOW,CANNY_HIGH)

        # apply roi mask
        edgesRoi = selectRoi(edges)

        # find lines with hough; use probabilistic version to increase speed
        lines = cv2.HoughLinesP(edgesRoi,HOUGH_DIST_RES,HOUGH_ANGLE_RES,HOUGH_THRESHOLD,None,HOUGH_MIN_LINE,HOUGH_MAX_GAP)

        # identify best overlap point of lines - this is the vanishing point
        vp = findIntersection(lines)
        vps.append(vp)

        # save image
        for line in lines:
            for x1,y1,x2,y2 in line:
                cv2.line(im, (x1,y1), (x2,y2), GREEN_COLOR, thickness=2)
        cv2.circle(im,(vp[0],vp[1]),8,GREEN_COLOR,2)
        savepath = os.path.join(OUTPUT_DIR_VP,os.path.split(imFile)[1])
        cv2.imwrite(savepath,im)

    # return arithmetic mean of vp coordinates
    return np.mean(vps,axis=0)

# display example of vanishing point

print("Calculating vanishing point...")
vp = findVanishingPoint(mtx,dst)
print("Vanishing point: {0}".format(vp))
filenameList = [fn for fn in os.listdir(OUTPUT_DIR_VP) if fn.endswith('.jpg')]
fn = random.choice(filenameList)
imBGR = cv2.imread(os.path.join(OUTPUT_DIR_VP,fn))
imRGB = cv2.cvtColor(imBGR,cv2.COLOR_BGR2RGB)
plt.imshow(imRGB)
plt.show()

# define function for calculating perspective transform

def calculatePerspectiveTransform():
    # calculate perspective transform matrix

    # read in camera calibration
    with open(CALIBRATION_FILENAME,'rb') as f:
        calibrationData = pickle.load(f)
    mtx = calibrationData['cam_matrix']
    dst = calibrationData['dist_coeffs']

    # find vanishing point; note this is returned as (x,y)
    vp = findVanishingPoint(mtx,dst)

    xVP,yVP = int(vp[0]),int(vp[1])
    xBottomLeft = int(ROI_LEFT * COLS)
    xBottomRight = int(ROI_RIGHT * COLS)
    yTop = int(ROWS * ROI_TOP)
    yBottom = int(ROWS * ROI_BOTTOM)

    # calculate x positions of upper ROI corners
    # x = my + b; m = (x1-x0)/(y1-y0); b = x0 - m * y0
    leftM = (xVP - xBottomLeft) / float(yVP - yBottom)
    leftB = xBottomLeft - leftM * yBottom
    rightM = (xVP - xBottomRight) / float(yVP - yBottom)
    rightB = xBottomRight - rightM * yBottom
    xTopLeft = int(leftM * yTop + leftB)
    xTopRight = int(rightM * yTop + rightB)

    corners = [(xTopLeft,yTop),(xBottomLeft,yBottom),(xBottomRight,yBottom),(xTopRight,yTop)]
    pts1 = np.float32(corners)
    pts2 = np.float32( [[0,0],[0,ROWS],[COLS,ROWS],[COLS,0]] )

    perspectiveTrans = cv2.getPerspectiveTransform(pts1,pts2)
    inversePerspectiveTrans = cv2.getPerspectiveTransform(pts2,pts1)

    transformDict = {'forwardWarp': perspectiveTrans, 'backwardWarp': inversePerspectiveTrans,'corners': corners}

    with open(PERSPECTIVE_FILENAME,'wb') as f:
        pickle.dump(transformDict,f)

    return transformDict

    # display example perspective warp

print("Warping perspective forward and backward...")

td = calculatePerspectiveTransform()
p = td['corners']

filenameList = [fn for fn in os.listdir(TEST_DIR) if fn.endswith('.jpg') and 'straight' in fn]
fn = random.choice(filenameList)
im = cv2.imread(os.path.join(TEST_DIR,fn))
im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
im = cv2.undistort(im,mtx,dst)

cv2.line(im,p[0],p[1],color=(255,0,0),thickness=4)
cv2.line(im,p[1],p[2],color=(255,0,0),thickness=4)
cv2.line(im,p[2],p[3],color=(255,0,0),thickness=4)
cv2.line(im,p[3],p[0],color=(255,0,0),thickness=4)

imWarped = cv2.warpPerspective(im,td['forwardWarp'],(COLS,ROWS))
imWarpedBack = cv2.warpPerspective(imWarped,td['backwardWarp'],(COLS,ROWS))

f = plt.figure(figsize=(30,40))
ax = f.add_subplot(1,3,1)
ax.imshow(im)
ax = f.add_subplot(1,3,2)
ax.imshow(imWarped)
ax = f.add_subplot(1,3,3)
ax.imshow(imWarpedBack)
plt.show()

def findCentroid(im):
    # utility function to find horizontal centroid of distribution

    hist = np.sum(im,axis=0)
    nonzero = np.flatnonzero(hist)
    if len(nonzero) > 0:
        centroid = int(nonzero.mean())
        found = True
    else:
        centroid = int(0.5 * im.shape[1])
        found = False

    return found, centroid

    # define speed estimation routine
# watches for lane markers to pass and counts frames since last one

def estimateSpeed(roi):
    # check for lane marker
    hist = np.sum(roi,axis=1)
    nonzero = np.flatnonzero(hist)
    if len(nonzero) == len(hist):
        if rightLane.framesSeenSinceMarker > FRAME_MARKER_THRESHOLD:
            # we found a new lane marker
            metersTraveled = METERS_PER_MARKER
            secondsTaken = rightLane.framesSeenSinceMarker / float(FPS)
            rightLane.estimatedSpeed = (metersTraveled / secondsTaken) * (0.001) * 3600
            rightLane.framesSeenSinceMarker = 0

            # define main routine to fit lane lines for left and right

def findAndFit(im,yVals):

    # set up image for drawing
    imFit = np.copy(im)
    imFit = cv2.cvtColor(imFit,cv2.COLOR_GRAY2BGR)
    proposedFits = []

    # increment frame count
    rightLane.framesSeenSinceMarker += 1

    for lane in ([leftLane,rightLane]):
        xCentroids = []
        if lane.goodFitLastFrame:
            # case where we got a good fit last frame
            fit = np.poly1d(lane.recentFits[0])
            for i in range(len(yVals)):
                y = yVals[i]
                currentXValue = int(fit(y))
                xLeft,xRight = currentXValue - WIN_WIDTH, currentXValue + WIN_WIDTH
                yBottom,yTop = y, y - WIN_HEIGHT
                roi = im[yTop:yBottom,xLeft:xRight]
                found, centroid = findCentroid(roi)
                xCentroids.append(centroid + xLeft)
                cv2.rectangle(imFit,(xLeft,yTop),(xRight,yBottom),GREEN_COLOR,2)
                imFit[yTop:yBottom,currentXValue] = RED_COLOR

                # do speed estimation
                if (i == 0) & (lane == rightLane):
                    estimateSpeed(roi)

        else:
            # case where we didn't get a good fit last frame
            # start by finding x position of lane at bottom of image
            if lane.side == 'left':
                xLeft,xRight = 0,int(np.floor(COLS/2))
            elif lane.side == 'right':
                xLeft,xRight = int(np.ceil(COLS/2)),COLS
            roi = im[int(ROWS/2):ROWS,xLeft:xRight]
            found, centroid = findCentroid(roi)
            if found:
                currentXValue = centroid + xLeft
            else:
                # worst case: can't find it, so go with mid-point
                currentXValue = int(xLeft + 0.25 * ROWS)
            for i in range(len(yVals)):
                y = yVals[i]
                xLeft, xRight = currentXValue - WIN_WIDTH, currentXValue + WIN_WIDTH
                yBottom, yTop = y, y-WIN_HEIGHT
                roi = im[yTop:yBottom,xLeft:xRight]
                found, centroid = findCentroid(roi)
                if found:
                    currentXValue = centroid + xLeft
                xCentroids.append(currentXValue)
                cv2.rectangle(imFit,(xLeft,yTop),(xRight,yBottom),GREEN_COLOR,2)
                imFit[yTop:yBottom,currentXValue] = RED_COLOR

        # do polynomial fit
        xCentroidsArray = np.array(xCentroids)
        proposedFit = np.polyfit(yVals,xCentroidsArray,2)     # 2nd-order polynomial fit
        proposedFits.append(proposedFit)

    # do sanity check
    fitLeft = np.poly1d(proposedFits[0])
    fitRight = np.poly1d(proposedFits[1])
    xLeft = fitLeft(ROWS)
    xRight = fitRight(ROWS)
    if (np.absolute(xRight - xLeft - LANE_WIDTH_PIXELS) < WIN_WIDTH):
        leftLane.goodFitLastFrame = True
        rightLane.goodFitLastFrame = True
        leftLane.recentFits = np.roll(leftLane.recentFits,1,axis=0)
        rightLane.recentFits = np.roll(rightLane.recentFits,1,axis=0)
        leftLane.recentFits[0] = proposedFits[0]
        rightLane.recentFits[0] = proposedFits[1]

    return imFit

    # define function to draw lane on image

def drawLane(im):

    # get pixels in lane from fits (in warped space)
    yPixelVals = range(ROWS)

    if TEST_MODE:
        # don't average
        leftFit = leftLane.recentFits[0]
        rightFit = rightLane.recentFits[0]
    else:
        # video mode; do average
        leftFit = np.mean(leftLane.recentFits,axis=0)
        rightFit = np.mean(rightLane.recentFits,axis=0)

    fitLeft = np.poly1d(leftFit)
    fitRight = np.poly1d(rightFit)
    xPixelValsLeft = fitLeft(yPixelVals)
    xPixelValsRight = fitRight(yPixelVals)

    pts = zip(yPixelVals,xPixelValsLeft,xPixelValsRight)

    # draw lane on warped image
    imLaneWarped = np.zeros((ROWS,COLS,3),np.uint8)
    for p in pts:
        imLaneWarped[p[0],int(p[1]):int(p[2]),:] = GREEN_COLOR

    # warp image back and combine with original image
    imLane = cv2.warpPerspective(imLaneWarped,inversePerspectiveTrans,(COLS,ROWS))
    imLane = cv2.addWeighted(im,1,imLane,0.5,0)

    # calculate curvature
    curveLeft = leftLane.calculateCurvature(leftFit)
    curveRight = rightLane.calculateCurvature(rightFit)
    curveMean = 0.5 * (curveLeft + curveRight)
    curveStr = 'Lane curvature: {:06.2f} m'.format(curveMean)
    cv2.putText(imLane,curveStr,(100,90),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,255,255),thickness=2)

    # calculate lane center
    centerOffset = 0.5*(COLS-fitLeft(ROWS)-fitRight(ROWS)) * XM_PER_PIX
    centerStr = 'Lane offset: {:3.2f} m'.format(centerOffset)
    cv2.putText(imLane,centerStr,(100,140),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,255,255),thickness=2)

    # write estimated speed on image
    speedStr = 'Est. speed: {:3.2f} km/h'.format(rightLane.estimatedSpeed)
    cv2.putText(imLane,speedStr,(100,190),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,255,255),thickness=2)

    return imLane

    # define main routine for lane finding

def findLane(im):

    # read file and undistort
    imUndistorted = cv2.undistort(im,mtx,dst)

    # find lane pixels
    imBinary, lBinary, bBinary = selectLanePixels(imUndistorted)

    # transform perspective
    imWarped = cv2.warpPerspective(imBinary,perspectiveTrans,(COLS,ROWS))

    # find lanes and fit to polynomial
    yVals = range(ROWS,0,-WIN_HEIGHT)
    imFit = findAndFit(imWarped,yVals)

    # draw lane on image
    imLane = drawLane(imUndistorted)

    # write sliding window inset onto image
    imFitSmall = cv2.resize(imFit,(INSET_COLS,INSET_ROWS))
    imLane[32:32+INSET_ROWS,COLS-INSET_COLS-1:-1] = imFitSmall

    if RECORD:
        # save images at various processing stages
        cv2.imwrite(os.path.join(OUTPUT_DIR_UNDISTORT,imageFile),imUndistorted)
        cv2.imwrite(os.path.join(OUTPUT_DIR_MARKER_PIXELS,imageFile),imBinary)
        cv2.imwrite(os.path.join(OUTPUT_DIR_WARPED,imageFile),imWarped)
        cv2.imwrite(os.path.join(OUTPUT_DIR_SLIDING_WINDOW,imageFile),imFit)
        cv2.imwrite(os.path.join(OUTPUT_DIR_LANE_MARK,imageFile),imLane)

    return imLane

    # do video processing

# read in camera calibration or perform calibration if pickled file is unavailable
try:
    with open(CALIBRATION_FILENAME,'rb') as f:
        calibrationData = pickle.load(f)
        mtx = calibrationData['cam_matrix']
        dst = calibrationData['dist_coeffs']
        print("Read camera calibration from file...")
except:
    print("Performing camera calibration...")
    mtx,dst = calibrateCamera()

# read in perspective transform or perform transform matrix calculation if pickled file is unavailable
try:
    with open(PERSPECTIVE_FILENAME,'rb') as f:
        transformDict = pickle.load(f)
        perspectiveTrans = transformDict['forwardWarp']
        inversePerspectiveTrans = transformDict['backwardWarp']
    print("Read perspective transform from file...")
except:
    print("Performing perspective transform...")
    transformDict = calculatePerspectiveTransform()
    perspectiveTrans = transformDict['forwardWarp']
    inversePerspectiveTrans = transformDict['backwardWarp']

# set up lane line objects
leftLane = LaneLine('left')
rightLane = LaneLine('right')

# if we're in test mode, analyze test images
if TEST_MODE:
    for imageFile in os.listdir(TEST_DIR):
        if imageFile.endswith('.jpg') and 'straight' not in imageFile:
            print('Loading image {0}...'.format(imageFile))
            fn = os.path.join(TEST_DIR,imageFile)
            im = cv2.imread(fn)
            im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
            findLane(im)

else:
    # we're in video mode
    video_output = 'output_video/video_output.mp4'
    clip = VideoFileClip('./project_video.mp4')
    FPS = clip.fps
    clip_out = clip.fl_image(findLane)
    clip_out.write_videofile(video_output, audio=False)


import tkinter as tk
from tkinter import simpledialog, filedialog, messagebox, ttk

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

'''
elements

[
 (<column name>, <unit>, <interval>),

 ("Average Cell Voltage", "0.001V", "100ms"),
 ("Average Monomer Temperature", "0.1C", "100ms"),

 ...
]

'''
class Plot:
    def __init__(self, elements=[]):
        self.set_elements(elements=elements)

    def set_elements(self, elements=[]):
        self.elements = elements
        self.current = 0
        self.df = None
        self.channel = 0
        self.table_idx = 0
        self.mmu_cell_voltemp = False

    def set_mmu_cell_voltemp(self, value=True):
        self.mmu_cell_voltemp = value

    def draw(self, root=None, df=None, channel=0, table_idx=0):
        self.df = df
        self.channel = channel
        self.table_idx = table_idx

        col_names = []
        units = []
        intvals = []
        for i in self.elements:
            col_names.append(i[0])
            units.append(i[1])
            intvals.append(i[2])

        _channel = self.channel
        _table_idx = self.table_idx

        # get values of one column
        # cl = self.df.getValueByColumn(chn=_channel, sheetNo=_table_idx, colname=col_names[0])
        cl, ct, cr = self.df.getRichValueByColumn(chn=_channel, sheetNo=_table_idx, colname=col_names[0])

        # title
        _chnT = "Channel 0" if _channel == 0 else "Channel 1"
        if _table_idx == 0:
            _sheetT = "BMU"
        elif _table_idx == 7:
            _sheetT = "M-BMU (E-CANBus)"
        else:
            _sheetT = f"MMU {_table_idx}"

        top1 = tk.Toplevel(root)
        top1.wm_title(f"Plot - {col_names[0]}, {_chnT}, {_sheetT}")

        fig = Figure(figsize=(6, 4), dpi=100)
        graph = fig.add_subplot()

        # graph.set_xlabel("time")
        graph.set_ylabel(col_names[0])

        if len(cr) > 0:
            graph.set_yticks(ct, cr)

        graph.plot(range(len(cl)), cl)

        canvas = FigureCanvasTkAgg(fig, master=top1)  # A tk.DrawingArea.
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        toolbar = NavigationToolbar2Tk(canvas, top1)
        toolbar.update()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        def on_key_press(event):
            key_press_handler(event, canvas, toolbar)

        canvas.mpl_connect("key_press_event", on_key_press)

        def _quit():
            top1.quit()     # stops mainloop
            top1.destroy()  # this is necessary on Windows to prevent

        # create frame for radios, button arrangement
        plot_frame = tk.LabelFrame(master=top1, borderwidth = 0, highlightthickness = 0)
        plot_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=False)

        button = tk.Button(master=plot_frame, text="Quit", command=_quit)
        button.pack(side=tk.RIGHT, padx=50)

        acvt=tk.IntVar()

        def figsel():
            vt = acvt.get()
            if vt < len(self.elements):
                colname = col_names[vt]
                top1.wm_title(f"Plot - {colname}, {_chnT}, {_sheetT}")
                # cl = self.df.getValueByColumn(chn=_channel, sheetNo=_table_idx, colname=colname)
                cl, ct, cr = self.df.getRichValueByColumn(chn=_channel, sheetNo=_table_idx, colname=colname)

            else:
                if _table_idx > 0:
                    # handle cell voltemp
                    if vt ==5:
                        selection = comboVol.get()
                        colname = f"Cell Voltage {selection}"
                        #print("MMU Cell Voltage {}".format(selection))
                    elif vt ==6:
                        selection = comboTemp.get()
                        colname = f"Monomer Temperature {selection}"
                        #print("MMU Cell Temperature {}".format(selection))
                    else:
                        return
                    top1.wm_title(f"Plot - {colname}, {_chnT}, {_sheetT}")
                    # cl = self.df.getValueByColumn(chn=_channel, sheetNo=_table_idx, colname=colname)
                    cl, ct, cr = self.df.getRichValueByColumn(chn=_channel, sheetNo=_table_idx, colname=colname)
                else:
                    return

            graph.clear()
            # graph.set_xlabel("time")
            graph.set_ylabel(colname)
            if len(cr) > 0:
                graph.set_yticks(ct, cr)
            graph.plot(range(len(cl)), cl)
            canvas.draw()

        for i in range(len(self.elements)):
            acvr = tk.Radiobutton(master=plot_frame, text=self.elements[i][0], variable=acvt, value=i, padx=5, command=figsel) #, ommand=sel)
            acvr.pack(side=tk.LEFT) # grid(row=2, column=0)  #  pack(anchor=tk.W) # side=tk.LEFT) # anchor = tk.W)

        def vol_modified(event):
            acvt.set(5)
            #selection = comboVol.get()
            #print("vol_modified {}".format(selection))
            figsel()

        def temp_modified(event):
            acvt.set(6)
            #selection = comboVol.get()
            #print("temp_modified {}".format(selection))
            figsel()

        # mmu cell vol and temp
        if _table_idx > 0:
            if self.mmu_cell_voltemp == True:
                acvr = tk.Radiobutton(master=plot_frame, text="Cell Voltage ", variable=acvt, value=5, padx=5, command=figsel) #, ommand=sel)
                acvr.pack(side=tk.LEFT)
                # combox
                comboVol = ttk.Combobox(plot_frame, state="readonly", values=list(range(1,31)), width=4)
                comboVol.current(0)
                comboVol.pack(side=tk.LEFT)
                comboVol.bind('<<ComboboxSelected>>', vol_modified)
                acvr = tk.Radiobutton(master=plot_frame, text="Monomer Temperature ", variable=acvt, value=6, padx=5, command=figsel) #, ommand=sel)
                acvr.pack(side=tk.LEFT)
                comboTemp = ttk.Combobox(plot_frame, state="readonly", values=list(range(1,5)), width=4)
                comboTemp.pack(side=tk.LEFT)
                comboTemp.bind('<<ComboboxSelected>>', temp_modified)
                comboTemp.current(0)

        tk.mainloop()


'''
from the subjects videos of raw h36m dataset to frames
'''

import os
import sys

import cv2

def makedir(folder_path):
    if not os.path.isdir(folder_path):
        os.mkdir(folder_path)

def extract_frames(img_folder, vidcap, prefix):
    if (prefix[-1] == '') or (prefix[-1] == '\n'):
        prefix = prefix[:-1]

    subject = int(prefix[5:7])
    print (prefix, subject)

    imgdir = img_folder + '/' + str(prefix)
    makedir(imgdir)

    f = 0
    while True:
        success, image = vidcap.read()
        f += 1
        if not success:
            print ('End of file!')
            break

        if subject in [2, 3, 4, 10]: # Test
            continue
        elif subject in [1, 5, 6, 7, 8]: # Train
            if (f % 5) != 1:
                continue
        elif subject in [9, 11]: # Validation
            if (f % 64) != 1:
                continue
        else:
            raise Exception('Unexpected subject number ({})'.format(subject))

        print ('%s %d' % (prefix, f))
        fname = imgdir + '/%05d.jpg' % f
        cv2.imwrite(fname, image, [cv2.IMWRITE_JPEG_QUALITY, 90])

# python2 <PATH>/datasets/h36m/h36m_frames_map.txt <PATH>/datasets/h36m/images
if __name__ == '__main__':
    try:
        map_path = sys.argv[1]
        output_dir = sys.argv[2]
    except Exception as e:
        print (str(e) + '\n\nExpected the first argument to be the input file, second to be output images folder path')
        sys.exit()

    with open(map_path, 'r') as f_map:
        try:
            while True:
                line = f_map.readline()
                if line == '':
                    break
                mp4_name, prefix = line.split(':')
                vidcap = cv2.VideoCapture(mp4_name)
                extract_frames(output_dir, vidcap, prefix)
                vidcap.release()
        except Exception as e:
            print (e)


import torch.optim as optim
from tqdm import tqdm
import torch.nn.functional as F

class TrainCifar10(object):
    def __init__(self, device, model, train_loader):
        self.device = device
        self.train_loader = train_loader
        self.model = model
        self.train_acc = []
        self.train_losses = []

    def train(self, limit, test_cifar, learning_rate=0.01, momentum=0.9):
        optimizer = optim.SGD(self.model.parameters(), lr=learning_rate,
                              momentum=momentum)

        for epoch in range(limit):
            self.model.train()
            p_bar = tqdm(self.train_loader)
            correct = 0
            processed = 0

            print("EPOCH:", epoch+1)

            for batch_idx, (data, labels) in enumerate(p_bar):
                data, labels = data.to(self.device), labels.to(self.device)

                # Init
                optimizer.zero_grad()
                # In PyTorch, we need to set the gradients to zero before starting to do backpropagation because PyTorch accumulates the gradients on subsequent backward passes.
                # Because of this, when you start your training loop, ideally you should zero out the gradients so that you do the parameter update correctly.

                # Predict
                y_pred = self.model(data)

                # Calculate loss
                loss = F.nll_loss(y_pred, labels)
                self.train_losses.append(loss)

                # Backpropagation
                loss.backward()
                optimizer.step()

                # Update p_bar-tqdm
                pred = y_pred.argmax(dim=1,
                                     keepdim=True)  # get the index of the max log-probability
                correct += pred.eq(labels.view_as(pred)).sum().item()
                processed += len(data)

                p_bar.set_description(
                    desc=f'Loss={loss.item()} Batch_id={batch_idx} Accuracy={100 * correct / processed:0.2f}')
                self.train_acc.append(100 * correct / processed)
            test_cifar.test()


import torch
from matplotlib import pyplot as plt

def plot_activation(fn):
    x = torch.arange(-3.0, 3.0, 0.01, requires_grad=True)
    y = fn(x)
    fn_name = fn.__name__
    plt.plot(x.detach(), y.detach(), label=fn_name)
    y.backward(torch.ones_like(x), retain_graph=True)
    plt.plot(x.detach(), x.grad, label=f'{fn_name} grad')
    plt.legend()


from typing import Union
from datetime import datetime
import requests
from xbrr.edinet.client.base_client import BaseClient
from xbrr.edinet.models import MetaData, Documents

class BaseDocumentListClient(BaseClient):
    """Base client to handle Document List API."""

    def __init__(self, response_type: str):
        """
        Arguments:
            response_type {str} -- Response type of document list api.
        """
        super().__init__(target="documents.json")
        self.response_type = response_type

    def _get(self, date: Union[str, datetime]) -> dict:
        """Get Document List API response.

        Arguments:
            date {(str, datetime)} -- Request date.

        Raises:
            Exception: Date format exception.

        Returns:
            dict -- EDINET Response (JSON).
        """
        url = self.endpoint
        _date = date
        if isinstance(date, str):
            try:
                _date = datetime.strptime(date, "%Y-%m-%d")
            except ValueError:
                raise Exception("Date format should be yyyy-mm-dd.")

        _date = _date.strftime("%Y-%m-%d")

        params = {
            "date": _date,
            "type": self.response_type
        }

        r = requests.get(url, params=params, verify=False)  # Caution

        if not r.ok:
            r.raise_for_status()
        else:
            body = r.json()
            return body

class MetaDataClient(BaseDocumentListClient):
    """Client to get metadata of document list."""

    def __init__(self):
        super().__init__(response_type="1")  # 1 = only metadata.

    def get(self, date: Union[str, datetime]) -> MetaData:
        """Get metadeta response.

        Arguments:
            date {(str, datetime)} -- Request date.

        Returns:
            MetaData -- Metadata of document list.
        """
        body = self._get(date)
        instance = MetaData.create(body)
        return instance

class DocumentListClient(BaseDocumentListClient):
    """Client to get document list."""

    def __init__(self):
        super().__init__(response_type="2")  # 2 = metadata and document list.

    def get(self, date: Union[str, datetime]) -> Documents:
        """Get metadeta response.

        Arguments:
            date {(str, datetime)} -- Request date.

        Returns:
            Documents -- Document list and its metadata.
        """
        body = self._get(date)
        instance = Documents.create(body)
        return instance


"""added document model

Revision ID: a9804c833dc8
Revises: ebe9c3a17077
Create Date: 2021-05-18 02:29:45.084078

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'a9804c833dc8'
down_revision = 'ebe9c3a17077'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('doc_categories',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('doc_documents',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('round_id', sa.Integer(), nullable=True),
    sa.Column('deadline', sa.DateTime(timezone=True), nullable=True),
    sa.Column('addedAt', sa.DateTime(timezone=True), nullable=True),
    sa.Column('url', sa.String(length=255), nullable=True),
    sa.Column('priority', sa.String(length=255), nullable=True),
    sa.Column('title', sa.String(length=255), nullable=True),
    sa.Column('summary', sa.Text(), nullable=True),
    sa.Column('category_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['category_id'], ['doc_categories.id'], ),
    sa.ForeignKeyConstraint(['round_id'], ['doc_rounds.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('doc_documents')
    op.drop_table('doc_categories')
    # ### end Alembic commands ###


import platform
from typing import Dict, List
from docplex.cp.model import *
from pandas import DataFrame
from const.weights import WEIGHT_OF_ADJUSTMENT_TIME, WEIGHT_OF_TARDY_JOB
from libs.settings import settings

from services.production_planning.job_duration_calculator import JobDurationCalculator
from libs.utils import resource_path
from libs.loggers import logging

logger = logging.getLogger('planner')

class Planner:
    def __init__(
        self,
        jobs_dict: Dict[int, int],
        machines_dict: Dict[int, int],
        pending_task: DataFrame,
        duration_calculator: JobDurationCalculator,
        due_date_dict: Dict[int, int],
        setup_time_dict: Dict[int, int] = None,
    ):
        logger.info('Start planning ...')

        self.mdl = CpoModel(name='productionPlanning')

        self.jobs = list(jobs_dict.keys())
        self.machines = list(machines_dict.keys())
        self.jobs_dict = jobs_dict
        self.due_date_dict = due_date_dict
        self.machines_dict = machines_dict
        self.pending_task = pending_task
        self.duration_calculator = duration_calculator
        self.setup_time_dict = setup_time_dict
        self.processing_itv_vars = []
        self.__solution_status = False

    def __prepare_processing_interval(self):
        processing_itv_vars = []

        for j in self.jobs:
            processing_itv_job_vars = []
            for m in self.machines:
                job_id = self.jobs_dict.get(j)
                mat_id = self.pending_task.loc[job_id]['mat_id']
                pending_volume = float(
                    self.pending_task.loc[job_id]['res_draft_volume'])
                machine = self.machines_dict.get(m)

                self.duration_calculator.register(
                    machine_id=machine,
                    mat_id=mat_id
                )

                duration = self.duration_calculator.calculate_duration(
                    pending_volume=pending_volume
                )

                self.duration_calculator.clear()

                if duration:
                    int_var = self.mdl.interval_var(
                        optional=True, size=duration, name="interval_job{}_machine{}".format(j, m))

                    processing_itv_job_vars.append(
                        int_var
                    )

                else:
                    processing_itv_job_vars.append(
                        None
                    )

            processing_itv_vars.append(processing_itv_job_vars)

        return processing_itv_vars

    def __create_setup_matrix(self, processing_itv_vars):
        setup_matrix = [*range(0, len(self.machines))]
        for m in self.machines:
            setup_time = self.setup_time_dict.get(m)
            temp1 = []
            for j1 in self.jobs:
                temp2 = []
                for j2 in self.jobs:
                    if (isinstance(processing_itv_vars[j1][m], expression.CpoIntervalVar) & isinstance(processing_itv_vars[j2][m], expression.CpoIntervalVar)):
                        if self.pending_task.loc[self.jobs_dict.get(j1)]['mat_id'] == self.pending_task.loc[self.jobs_dict.get(j2)]['mat_id']:
                            temp2.append(0)
                        else:
                            temp2.append(setup_time)
                if len(temp2) > 0:
                    temp1.append(temp2)
            setup_matrix[m] = temp1

        return setup_matrix

    def __add_job_must_be_done_constraint(self, processing_itv_vars):
        for j in self.jobs:
            processing_itv_vars_at_j = processing_itv_vars[j]
            non_none_processing_itv_vars_at_j = []

            for m in self.machines:
                if isinstance(processing_itv_vars_at_j[m], expression.CpoIntervalVar):
                    non_none_processing_itv_vars_at_j.append(
                        processing_itv_vars_at_j[m])

            self.mdl.add(
                self.mdl.sum(
                    [self.mdl.presence_of(var)
                     for var in non_none_processing_itv_vars_at_j]
                ) == 1
            )

    def __add_no_overlap_and_set_up_overhead_constraint(self, processing_itv_vars):
        sequence_vars = [
            self.mdl.sequence_var(
                [processing_itv_vars[j][m] for j in self.jobs if isinstance(
                    processing_itv_vars[j][m], expression.CpoIntervalVar)],
                name="sequences_machine{}".format(m))
            for m in self.machines
        ]

        if self.setup_time_dict:
            setup_matrix = self.__create_setup_matrix(processing_itv_vars)

            for m in self.machines:
                if len(setup_matrix[m]) > 0:
                    self.mdl.add(self.mdl.no_overlap(
                        sequence_vars[m], setup_matrix[m]))
                else:
                    self.mdl.add(self.mdl.no_overlap(
                        sequence_vars[m]))

        else:
            for m in self.machines:
                self.mdl.add(self.mdl.no_overlap(sequence_vars[m]))

        return sequence_vars

    def __add_objective_function(self, sequence_vars: List[expression.CpoSequenceVar]):
        adjustment_time_list = []

        for m in self.machines:
            for var in sequence_vars[m].get_interval_variables():
                start_next = self.mdl.start_of_next(
                    sequence_vars[m], var, lastValue=-1, absentValue=-1)

                binary = self.mdl.binary_var(
                    name=f"binary_adjustment_time{var.name}")
                self.mdl.add(binary == (start_next >= 0))

                adjustment_time = (start_next - self.mdl.end_of(var)) * binary
                adjustment_time.set_name(f"adjustment_time_{var.name}")

                adjustment_time_list.append(adjustment_time)

        adjustment_time_obj = self.mdl.sum(adjustment_time_list)

        n_tardy_day_list = []
        for m in self.machines:
            for j in self.jobs:
                if isinstance(self.processing_itv_vars[j][m], expression.CpoIntervalVar):
                    due_date = self.due_date_dict.get(j)
                    if due_date:
                        if due_date > 0:
                            n_tardy_day_list.append(self.mdl.max(
                                [0, self.mdl.end_of(self.processing_itv_vars[j][m]) - due_date]))

        n_tardy_day_obj = self.mdl.sum(n_tardy_day_list)
        self.mdl.add(self.mdl.minimize(adjustment_time_obj *
                     WEIGHT_OF_ADJUSTMENT_TIME + n_tardy_day_obj * WEIGHT_OF_TARDY_JOB))

    def get_processing_itv_vars(self):
        return self.processing_itv_vars

    def get_solution_status(self):
        return self.__solution_status

    def __update_solution_status(self, status=True):
        self.__solution_status = status

    def __calculate_objective_value(self, overall_objective_value: int, end_time_unit_dict: Dict[int, int]):
        tardy_job_objective_value = (
            self.pending_task.index.map(end_time_unit_dict) - self.pending_task['due_time_unit']
        ).apply(lambda x: x if x > 0 else 0).sum()
        tardy_job_objective_value = tardy_job_objective_value * WEIGHT_OF_TARDY_JOB

        return {
            "tardy_job_objective_value": tardy_job_objective_value,
            "adjustment_time_objective_value": overall_objective_value - tardy_job_objective_value
        }

    def __create_end_time_unit_dict(self, msol):
        end_time_unit_dict = {}
        for m in self.machines:
            for j in self.jobs:
                itv: CpoIntervalVarSolution = msol.get_var_solution(
                    self.processing_itv_vars[j][m])
                if itv:
                    if itv.is_present():
                        end_time_unit_dict.update(
                            {
                                j: itv.get_end()
                            }
                        )

        return end_time_unit_dict

    def generate(self):
        processing_itv_vars = self.__prepare_processing_interval()
        self.processing_itv_vars = processing_itv_vars

        self.__add_job_must_be_done_constraint(processing_itv_vars)
        sequence_var = self.__add_no_overlap_and_set_up_overhead_constraint(
            processing_itv_vars)
        self.__add_objective_function(sequence_var)

        if platform.system() in (['Linux', 'Darwin']):
            # Linux or MAC OS X
            execfile = './cpoptimizer'

        elif platform.system() == 'Windows':
            # Windows
            execfile = './cpoptimizer.exe'

        else:
            raise Exception('Invalid platform')

        msol = self.mdl.solve(
            log_output=True if settings.get_setting(
                "STAGE") == 'dev' else None,
            execfile=resource_path(execfile),
            TimeLimit=settings.get_setting('run_time_limit')
        )

        self.__update_solution_status()
        end_time_unit_dict = self.__create_end_time_unit_dict(msol)

        obj_value_details = self.__calculate_objective_value(
            msol.get_objective_value(), end_time_unit_dict)

        logger.info('Success.')
        logger.info('Objective value is {}'.format(msol.get_objective_value()))
        logger.info('Tardy job objective value: {}'.format(
            obj_value_details['tardy_job_objective_value']))
        logger.info('Adjustment time objective value: {}'.format(
            obj_value_details['adjustment_time_objective_value']))

        return msol


from django.urls import path
from django.contrib.auth.views import LoginView
from . import views
app_name = 'dairy'

urlpatterns = [
	path('', views.homePage, name='home'),
	path('milklist/', views.milkList, name='milklist'),
	path('addmilk/', views.addMilk, name='addmilk'),
	path('search/', views.searchFilter, name='search'),
	path('logout/', views.logoutView, name='logout'),
	path('login/',LoginView.as_view(template_name='login.html'), name='login'),
]


import spacy

def get_most_similar_movie(description):
    nlp = spacy.load("en_core_web_sm")
    movies = []
    with open("movies.txt", "r") as file:
        movies = file.readlines()

    similarity_scores = []
    for movie in movies:
        movie = movie.strip()
        doc1 = nlp(movie)
        doc2 = nlp(description)
        similarity_scores.append(doc1.similarity(doc2))

    most_similar_movie = movies[similarity_scores.index(max(similarity_scores))]
    return most_similar_movie.strip()

if __name__ == "__main__":
    description = "Will he save their world or destroy it? When the Hulk becomes too dangerous for the Earth, the Illuminati trick Hulk into a shuttle and launch him into space to a planet where the Hulk can live in peace. Unfortunately, Hulk land on the planet Sakaar where he is sold into slavery and trained as a gladiator."
    most_similar_movie = get_most_similar_movie(description)
    print("Next movie recommendation:", most_similar_movie)


import gym
from gym import spaces, logger
from gym.utils import seeding
import numpy as np
from os import path
from .cost_functions import eps_relu_target, eps_relu_target_pattern
from .cost_functions import quadratic_target, quadratic_target_pattern
from .poni_aux import f_poni, f_signal, poni_target, poni_colors_array

#######################################################################
#
#   Base class for PONI network environment
#
#######################################################################

class PONINetwork_Signal(gym.Env):
    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second': 30
    }
    d_state = 4
    d_signal = 1
    d_action = 2
    noise = 5e-3 # None # noise strength
    restart_low =  np.array([0.9, 0.0, 0.0, 0.9, 0.0])
    restart_high = np.array([1.0, 0.1, 0.1, 1.0, 0.1])
    target = np.array([0., 0., 1., 0.])    # target to reach
    high = 1.2*np.ones(d_state + d_signal, dtype=float)
    high[-d_signal:] = 10. # very high bound for Shh
    low = np.zeros_like(high, dtype=float)
    max_u = 2.*np.ones(d_action, dtype=float)
    min_u = np.zeros(d_action, dtype=float)

    dt = 0.05          # time-step
    viewer = None      # boh

    def __init__(self):
        self.seed()

        self.max_time = 50.     # time horizon

        self.time = 0.          # timer

        self.action_space = spaces.Box(
            low=np.zeros_like(self.max_u),
            high=self.max_u,
            shape=self.max_u.shape,
            dtype=np.float32
        )
        # 'high' is the array of upper bounds in each dimension
        # 'low' is that of the lower bounds
        # State includes agent state and signal received
        self.state_space = spaces.Box(
            low=self.low,
            high=self.high,
            dtype=float
        )
        # if the environment has partial observability,
        # define the observation space based on the
        # dimensions that we want to observe
        try:
            self.observation_space = spaces.Box(
                low=self.low[self.obs_dims],
                high=self.high[self.obs_dims],
                dtype=float
            )
        except:
            self.observation_space = self.state_space

        self.shaping_weight = None

        self.set_task_parameters()
        self.set_target_cost()

        self.reset()

    def set_task_parameters (self, radius=0.2, eta=0.05):
        self.radius = radius # tolerance to target
        self.eta = eta       # cost for control

    def set_target_cost (self, cost='lsq'):
        self.cost_ = cost
        if cost == 'erelu':
            self.cost = eps_relu_target
        elif cost == 'lsq':
            self.cost = quadratic_target
        else:
            raise ValueError("invalid cost option")

    def set_shaping_weight(self, weight):
        self.shaping_weight = weight

    @property
    def shape(self):
        return self.state_space.shape

    @property
    def par_string (self):
        ps = ""
        ps += f"eta:{self.eta}"
        ps += f"_q:{self.cost_}"
        # ps += f"_rad:{self.radius}"
        return ps

    def seed(self, seed=None):
        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    def step(self, u):

        '''
        Computes one decision making step

        Input:
            u: action

        Output:
            obs: the observation after taking the action
            reward: the reward ensuing 'state_t, u, state_{t+1}'
            done: whether terminal state has been reached
            info: a dictionary with additional information
        '''

        info = {}

        info['state'] = np.append(self.state, self.signal)
        info['signal'] = self.signal

        # clip the control within [0, max_u]
        u = np.clip(u, self.min_u, self.max_u)

        # compute production and degradation rates
        p, d = f_poni(self.state, u)

        # deterministic step
        f = p - d
        x = self.state + self.dt * f
        s = self.signal + self.dt * f_signal(self.time, 0., self.signal)

        # in case there is noise, add it
        if self.noise is not None:
            # noise in the state
            g = np.random.normal(size=self.shape)
            amp = np.sqrt(self.noise * self.dt * (p + d) )
            eta = amp * g[:-1]
            x = x + eta
            # noise in the signal
            amp = np.sqrt(self.noise * self.dt)
            eta = amp * g[-1]
            s = s + eta

        x = np.clip(np.append(x,s), self.low, self.high)
        # keep state and signal split (for convenience)
        self.state = x[:self.d_state]    # or x[:-self.d_signal]
        self.signal = x[-self.d_signal:] # or x[self.d_state:]
        self.time += self.dt

        distance_sq = np.sum( (self.state - self.target)**2 )
        # print(self.time, self.state, distance_sq, u)

        # done = bool( distance_sq < self.radius**2 )
        done = False

        '''
        calculate running costs/rewards
        - control
        - reward shaping (steps towards the target rewarded)
        '''
        # penalize control
        control_cost = np.sum( .5*self.eta*u**2 ) * self.dt
        info["control_cost"] = control_cost

        # penalize state
        state_cost = self.cost(self.state, self.target, self.radius, 0.2) * self.dt
        info["state_cost"] = state_cost

        running_cost = control_cost + state_cost

        # reward steps towards the target
        if self.shaping_weight is not None:
            shaping = - np.sqrt(distance_sq)/self.dt
            if self.prev_shaping is not None:
                reward_shaping = (shaping - self.prev_shaping) * self.shaping_weight
                info["shaping"] = reward_shaping
                # subtract from the running cost the reward from reward-shaping
                running_cost -= reward_shaping
            self.prev_shaping = shaping

        reward = 0
        if not done:
            reward -= running_cost
        elif self.steps_beyond_done is None:
            # we do this when the target is first reached
            self.steps_beyond_done = 0
        else:
            # we do this if the agent keeps going, even if a terminal
            # state has been reached
            # what's the point? surely not during training, maybe
            # when testing the agent on continuing tasks
            if self.steps_beyond_done == 0:
                logger.warn(
                    "You are calling 'step()' even though this "
                    "environment has already returned done = True. You "
                    "should always call 'reset()' once you receive 'done = "
                    "True' -- any further steps are undefined behavior."
                )
            self.steps_beyond_done += 1
            reward = 0.

        return self._get_obs(), reward, done, info

    def reset(self, state=None):
        '''
        Returns the OBSERVATION that is given in a random initial state
        '''
        self.steps_beyond_done = None
        self.prev_shaping = None
        self.time = 0.
        low = self.restart_low
        high = self.restart_high
        if state is not None:
            assert state.shape == self.shape, "invalid state shape"
            x = state

        else:
            x = self.np_random.uniform(low=low, high=high)
        self.state = x[:self.d_state]    # or x[:-self.d_signal]
        self.signal = x[-self.d_signal:] # or x[self.d_state:]
        return self._get_obs()

    def _get_obs(self):
        '''
        Returns a function of the state corresponding to the
        observation available to the agent
        '''
        return np.append(self.state, self.signal)

    def render(self, mode='human'):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-.1, 1.1, -.1, 1.1) # left, right, bottom, top
            # create a circle of given radius
            # and set the color
            point = rendering.make_circle(radius=.025, res=30)
            point.set_color(0, 0, 0)
            self.point_transform = rendering.Transform()
            point.add_attr(self.point_transform)
            self.viewer.add_geom(point)

        # move the circle around based on the state
        oli, nkx = self.state[1], self.state[2]
        self.point_transform.set_translation(oli, nkx)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')

    def close(self):
        if self.viewer:
            self.viewer.close()
            self.viewer = None

class PONINetwork_Signal_Nkx (PONINetwork_Signal):
    target = np.array([0., 0., 1., 0.])    # target to reach

class PONINetwork_Signal_Oli (PONINetwork_Signal):
    target = np.array([0., 1., 0., 0.])    # target to reach

class PONINetwork_Signal_Nkx_partial (PONINetwork_Signal):
    target = np.array([0., 0., 1., 0.])    # target to reach
    obs_dims = np.array([1,2,-1]) # return Olig, Nkx and Shh only
    def _get_obs(self):
        return np.hstack((self.state, self.signal))[self.obs_dims]

class PONINetwork_Signal_Oli_partial (PONINetwork_Signal):
    target = np.array([0., 1., 0., 0.])    # target to reach
    obs_dims = np.array([1,2,-1]) # return Olig, Nkx and Shh only
    def _get_obs(self):
        return np.hstack((self.state, self.signal))[self.obs_dims]

class PONINetwork_Signal_Pattern (PONINetwork_Signal):
    n_agents = 10
    positions = np.linspace(0.,1.,n_agents)
    target = poni_target(positions)

    def set_target_cost (self, cost='lsq'):
        self.cost_ = cost
        if cost == 'erelu':
            self.cost = eps_relu_target_pattern
        elif cost == 'lsq':
            self.cost = quadratic_target_pattern
        else:
            raise ValueError("invalid cost option")

    def reset (self, state=None):
        '''
        Returns the OBSERVATION that is given in a random initial state
        '''
        self.steps_beyond_done = None
        self.prev_shaping = None
        self.time = 0.
        low = self.restart_low
        high = self.restart_high
        if state is not None:
            assert state.shape == self.shape + (self.n_agents,), "invalid state shape"
            x = state
        else:
            x = np.array([self.np_random.uniform(low=low, high=high) for _ in range(self.n_agents)]).T

        self.state = x[:self.d_state, :]    # or x[:-self.d_signal, :]
        self.signal = x[-self.d_signal:, :] # or x[self.d_state:, :]
        return self._get_obs()

    def set_n_agents (self, n):
        self.n_agents = n
        self.positions = np.linspace(0.,1.,self.n_agents)
        self.target = poni_target(self.positions)
        self.reset()

    def _get_obs (self):
        '''
        Returns a function of the state corresponding to the
        observation available to the agent
        '''
        # self.state is (4, N)
        # self.signal is (1, N)
        # To get an array (5, N) we vstack them
        return np.vstack((self.state, self.signal))

    def _signal_step (self, u):
        '''
        Update the signal based on action `u`
        '''
        return np.exp( - self.positions / 0.15 ) # static gradient

    def step(self, u):

        '''
        Computes one decision making step

        Input:
            u: action

        Output:
            obs: the observation after taking the action
            reward: the reward ensuing 'state_t, u, state_{t+1}'
            done: whether terminal state has been reached
            info: a dictionary with additional information
        '''

        info = {}

        info['state'] = np.vstack((self.state, self.signal))
        info['signal'] = self.signal

        # clip the control within [0, max_u]
        # add a second axis to min_u and max_u, for the different agents
        u = np.clip(u, self.min_u[:,None], self.max_u[:,None])

        # compute production and degradation rates
        # returns a pair of arrays, (d_state, n_agents)
        p, d = f_poni(self.state, u)

        # deterministic step
        f = p - d
        x = self.state + self.dt * f
        # s = self.signal + self.dt * f_signal(self.time, self.positions, self.signal)
        s = self._signal_step(u)

        # in case there is noise, add it
        if self.noise is not None:
            # noise in the state
            g = np.random.normal(size=self.shape+(self.n_agents,))
            amp = np.sqrt(self.noise * self.dt * (p + d) )
            eta = amp * g[:self.d_state]
            x = x + eta
            # noise in the signal
            # amp = np.sqrt(self.noise * self.dt)
            # eta = amp * g[-self.d_signal:]
            # s = s + eta

        x = np.clip(np.vstack((x,s)), self.low[:,None], self.high[:,None])
        # keep state and signal split (for convenience)
        self.state = x[:self.d_state]    # or x[:-self.d_signal]
        self.signal = x[-self.d_signal:] # or x[self.d_state:]
        self.time += self.dt

        # distance of each cell state from the local target
        distance_sq = np.sum( (self.state - self.target)**2 , axis=0 )

        dones = distance_sq < self.radius**2
        info['dones'] = dones
        done = np.all(dones)

        '''
        calculate running costs/rewards
        - control
        - reward shaping (steps towards the target rewarded)
        '''
        # penalize control
        control_cost = np.sum( .5*self.eta*u**2, axis=0 ) * self.dt
        info["control_cost"] = control_cost

        # penalize state
        state_cost = self.cost(self.state, self.target, self.radius, 0.2) * self.dt
        info["state_cost"] = state_cost

        running_cost = control_cost + state_cost

        rewards = np.zeros(self.n_agents)
        if not done:
            rewards -= running_cost
        elif self.steps_beyond_done is None:
            # we do this when the target is first reached
            self.steps_beyond_done = 0
        else:
            # we do this if the agent keeps going, even if a terminal
            # state has been reached
            # what's the point? surely not during training, maybe
            # when testing the agent on continuing tasks
            if self.steps_beyond_done == 0:
                logger.warn(
                    "You are calling 'step()' even though this "
                    "environment has already returned done = True. You "
                    "should always call 'reset()' once you receive 'done = "
                    "True' -- any further steps are undefined behavior."
                )
            self.steps_beyond_done += 1
            rewards = np.zeros(self.n_agents)

        return self._get_obs(), rewards, done, info

    def render(self, mode='human'):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-.1, 1.1, -.1, 1.1) # left, right, bottom, top
            # create a circle of given radius
            # and set the color
            points = [rendering.make_circle(radius=.01, res=10) for _ in range(self.n_agents)]
            self.point_transforms = [rendering.Transform() for _ in range(self.n_agents)]

            # set alpha channel as soft-max
            # alpha channel for each gene (N_cells, genes)
            alpha = np.exp(self.target.T/.3)
            alpha /= np.sum(alpha, axis=1)[:,None]

            colors = np.matmul(alpha, poni_colors_array)
            for point, transform, color in zip(points, self.point_transforms, colors):
                point.set_color(*color)
                point.add_attr(transform)
                self.viewer.add_geom(point)

        # move the circle around based on the state
        oli, nkx = self.state[1], self.state[2]
        for n, transform in enumerate(self.point_transforms):
            transform.set_translation(oli[n], nkx[n])

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')

class PONINetwork_Signal_Pattern_partial (PONINetwork_Signal_Pattern):
    obs_dims = np.array([1,2,-1]) # return Olig, Nkx and Shh only
    def _get_obs (self):
        '''
        Returns a function of the state corresponding to the
        observation available to the agent
        '''
        return np.vstack((self.state, self.signal))[self.obs_dims]

class PONINetwork_Diffusion_Pattern (PONINetwork_Signal_Pattern):
    kappa = 0.1
    def set_timescale (self, tau=10.):
        self.kappa = 1./tau

    def _signal_step (self, u):
        '''
        Update the signal based on action `u`
        '''
        return self.signal + self.dt * f_signal (self.time, self.positions, self.signal, kappa=self.kappa)

class PONINetwork_Diffusion_Pattern_partial (PONINetwork_Diffusion_Pattern):
    def _signal_step (self, u):
        '''
        Update the signal based on action `u`
        '''
        return self.signal + self.dt * f_signal (self.time, self.positions, self.signal, kappa=self.kappa)

__all__ = [
        "PONINetwork_Signal_Nkx",
        "PONINetwork_Signal_Oli",
        "PONINetwork_Signal_Nkx_partial",
        "PONINetwork_Signal_Oli_partial",
        "PONINetwork_Signal_Pattern",
        "PONINetwork_Signal_Pattern_partial",
        "PONINetwork_Diffusion_Pattern",
        "PONINetwork_Diffusion_Pattern_partial",
]


#!/usr/bin/python3

import os, time, sys
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'   # hide pygame welcome message
import pygame as pg
import moderngl as mgl
import numpy as np
#
from settings import *
from OpenGLAPI.shader_manager import ShaderManager
from OpenGLAPI.camera import OrbitCamera, PerspectiveCamera
from scene import Scene

#
class Func3D:
    def __init__(self, window_size=WIN_RES):
        t0 = time.perf_counter_ns()
        self.window_size = window_size
        # init PyGame and set OpenGL attributes
        pg.init()
        pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, 3)
        pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, 3)
        pg.display.gl_set_attribute(pg.GL_CONTEXT_PROFILE_MASK, pg.GL_CONTEXT_PROFILE_CORE)
        pg.display.gl_set_attribute(pg.GL_DEPTH_SIZE, 24)
        self.surface = pg.display.set_mode(WIN_RES, flags=pg.OPENGL | pg.DOUBLEBUF)

        # OpenGL context and settings
        self.ctx = mgl.create_context()
        self.ctx.enable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE | mgl.BLEND)
        self.ctx.blend_func = mgl.SRC_ALPHA, mgl.ONE_MINUS_SRC_ALPHA
        self.ctx.gc_mode = 'auto'

        # framerate-related
        self.clock = pg.time.Clock()
        self.dt = 0
        self.time = 0
        self.fps = 0
        self.frame_count = 0

        pg.event.set_grab(True)
        pg.mouse.set_visible(False)
        self.mousewheel_event :pg.event = None

        self.is_running = True
        self.is_loaded = False

        self.on_init()

        print(f'Core modules initialized in {(time.perf_counter_ns() - t0)/1e6} ms.')

    #
    def on_init(self):
        self.shader_manager = ShaderManager(self.ctx)
        self.camera_orbit = OrbitCamera(self, position=(-3.6, 0.5, -4.4), x_angle=230,
                                        y_angle=85)
        self.camera_perspective = PerspectiveCamera(self, x_angle=0, y_angle=0)
        self.camera = self.camera_orbit
        self.camera_orbit_current = True        # toggle for switching camera
        self.scene = Scene(self, self.camera)
        self.func3D_obj_id = ''                 # for later access through Scene
    #
    def load_data(self,
                  data : np.array,
                  x : np.ndarray =np.array(None),
                  y : np.ndarray =np.array(None),
                  equal_axes : bool=True,   # if true, the data array is shrunk to
                                            # (m x m), where m is min(dim(x), dim(y))
                  func_id : str=None):
        t0 = time.perf_counter_ns()
        self.func3D_obj_id = self.scene.add_data(x, data, y, equal_axes, func_id)
        self.scene.add_axes(self.func3D_obj_id)
        self.is_loaded = True
        print(f'Meshes created in {(time.perf_counter_ns() - t0)/1e6} ms.')

    #
    def handle_events(self):
        self.mousewheel_event = None
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.is_running = False
            elif event.type == pg.MOUSEWHEEL:
                self.mousewheel_event = event
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    self.is_running = False
                # take screenshot
                if event.key == pg.K_z:
                    pg.image.save(self.surface, './screenshot.png')
                    print("saved screenshot to './screenshot.png'.")
                # toggle mesh wireframe
                if event.key == pg.K_F3:
                    self.scene.objects[self.func3D_obj_id].toggle_wireframe()
                if event.key == pg.K_F4:
                    self.scene.objects[self.func3D_obj_id].toggle_lights()
                # DEBUG : camera state
                if event.key == pg.K_c:
                    self.camera.print_state_debug()
                # toggle orbital/perspective camera
                if event.key == pg.K_TAB:
                    #print('==========================================================')
                    #self.camera.print_state_debug()
                    if self.camera_orbit_current:
                        self.camera = self.camera_perspective
                        self.camera.copy_state(self.camera_orbit)
                    else:
                        self.camera = self.camera_orbit
                        self.camera.copy_state(self.camera_perspective)
                    #self.camera.print_state_debug()
                    self.camera_orbit_current = not self.camera_orbit_current
    #
    def update(self):
        self.camera.update()

    #
    def render(self):
        self.ctx.clear(color=BG_COLOR)
        self.scene.render(self.camera)
        pg.display.flip()

    #
    def run(self):
        if not self.is_loaded:
            self.shutdown(error_msg='no data loaded')

        while self.is_running:
            self.handle_events()
            self.update()
            self.render()

            #
            self.dt = self.clock.tick()
            self.fps = 1000.0 / self.dt
            pg.display.set_caption(f'{self.fps:.2f} fps')

    #
    def shutdown(self, error_msg=None):
        pg.quit()
        if error_msg is not None:
            raise Exception(error_msg)
        sys.exit()

if __name__ == '__main__':
    #
    app = Func3D()

    #examples = ['mp3', 'sine_func', 'eeg', 'essw']
    ex = 'essw'

    # -----------------------------------------------------------------------------------
    # use spectrum from ~/source/misc/music_visualizer (fft on music)
    # -----------------------------------------------------------------------------------
    if ex == 'mp3':
        with open('./data/mp3_spectrum.txt') as f:
            data = f.read()
        data = data.split('\n')[2:-2] # skip file headers etc
        # list in the form of 'step: x y z w ...'
        freqs = []
        for row in data:
            step = [float(val) for val in row.split(' ') if val != 'step:']
            freqs.append(step)
        freqs = np.array(freqs, dtype=np.float32)
        # remove data when FFT buffer not full
        freqs = freqs[40:,:]
        print(freqs.shape)
        PLOT = True
        if PLOT:
            import matplotlib.pyplot as plt
            fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 5))
            ax0.imshow(freqs)
            ax0.set_title('freqs')
            ax1.imshow(freqs.T)
            ax1.set_title('freqs.transpose()')
            plt.show()

        app.load_data(freqs,
                      equal_axes=False,
                      func_id='mp3')

    # -----------------------------------------------------------------------------------
    # make up a function
    # -----------------------------------------------------------------------------------
    elif ex == 'sine_func':
        xd, zd = 6.0, 10.0
        nx, nz = 100, 50
        scale = 20.0
        x, z = np.linspace(-xd, xd, nx), np.linspace(-zd, zd, nz)
        xx, zz = np.meshgrid(x, z)
        y = (scale * np.sin(1.0 * xx) * scale * np.cos(0.4 * zz) / 2.0)
        app.load_data(y, x, z, equal_axes=False, func_id='sine_func')

    # -----------------------------------------------------------------------------------
    # using saved numpy data
    # -----------------------------------------------------------------------------------
    elif ex == 'eeg':
        data = np.load('./data/spect_data.npy')
        data = np.load('/home/iomanip/documents/_BIOINFO--ML/_EXP/spectral_analysis/'
                       'dpss_test/spect_data.npy')
        # flip y axis
        data = np.flip(data, axis=0)
        print(data.shape)
        app.load_data(data,
                      equal_axes=False,
                      func_id='eeg')

    # -----------------------------------------------------------------------------------
    # load from image (cm.jet)
    # -----------------------------------------------------------------------------------
    elif ex == 'essw':
        import cv2
        from matplotlib import colormaps # colormaps['jet'], colormaps['turbo']
        from matplotlib.colors import LinearSegmentedColormap
        from matplotlib._cm import _jet_data

        def convert_jet_to_grey(img):
            """
            (for lossy compression (JPG), interpolates values to 'nearest' gray scale).
            """
            (height, width) = img.shape[:2]

            cm = LinearSegmentedColormap("jet", _jet_data, N=2**8)
            # cm = colormaps['turbo'] swap with jet if you use turbo colormap instead

            cm._init()  # Must be called first. cm._lut data field created here

            FLANN_INDEX_KDTREE = 1
            index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
            search_params = dict(checks=50)
            fm = cv2.FlannBasedMatcher(index_params, search_params)

            # JET, BGR order, excluding special palette values (>= 256)
            fm.add(255 * np.float32([cm._lut[:256, (2, 1, 0)]]))  # jet
            fm.train()

            # look up all pixels
            query = img.reshape((-1, 3)).astype(np.float32)
            matches = fm.match(query)

            # statistics: `result` is palette indices ("grayscale image")
            output = np.uint16([m.trainIdx for m in matches]).reshape(height, width)
            result = np.where(output < 256, output, 0).astype(np.uint8)
            # dist = np.uint8([m.distance for m in matches]).reshape(height, width)

            return result  # , dist uncomment if you wish accuracy image

        #
        data = cv2.imread('./data/essw.jpg', cv2.IMREAD_COLOR)
        data = data[10:-10:, 10:-10]

        data = convert_jet_to_grey(data).astype(dtype='f4')
        data /= 255.0
        print(data.shape)

        app.load_data(data, equal_axes=False, func_id='essw')

    #
    else:
        print('no data provided.')
        sys.exit(-1)
    #
    app.run()


import copy
import pkg_resources
import tornado.web
import webte.index
import webte.terminal

class Application(tornado.web.Application):

    routes = [
        (r"/?", webte.index.IndexHandler),
        (r"/terminal/?", webte.terminal.TerminalWebSocket),
        (r".*", webte.ErrorHandler, {"status_code": 404}),
    ]

    settings = {
        "template_path": pkg_resources.resource_filename("webte", "templates"),
        "static_path": pkg_resources.resource_filename("webte", "static"),
    }

    def __init__(self, debug=False, **kwargs):
        settings = copy.deepcopy(self.settings)
        settings.update(kwargs)
        super().__init__(self.routes, debug=debug, **settings)


from data_reader import read_deriv
from utils import create_dict
import csv
"""
removes one-to-one transformations
"""

def check_1t1(dict_rel):
    dict_one2ones = {k:[] for k in dict_rel.keys()}
    one2ones = []
    dict_lengths = dict()
    for k,v in dict_rel.items():
        the_same = True
        dict_lengths[k] = len(v)
        for w in v:
            if w[0] == w[1]:
                dict_one2ones[k].append((w[0], w[1]))
            else:
                the_same = False
        if the_same:
            one2ones.append(k)
    return dict_one2ones, one2ones, dict_lengths

def write_smaller_file(dict_rel, o2o, out_path):
    with open(out_path, 'w') as wf:
        writer = csv.writer(wf, delimiter="\t")
        writer.writerow(("relation", "w1", "w2"))
        for k,v in dict_rel.items():
            if k not in o2o:
                for w in v:
                    writer.writerow((k, w[0], w[1]))

def main():

    path = "ALL_WITH_NOUNSthresh80.csv"
    out_path = path.replace(".csv", "_no1t1.csv")
    dict_rel = create_dict(path)

    dict_1t1, o2o, d_lengths = check_1t1(dict_rel)
    for k,v in dict_1t1.items():
        if (d_lengths[k] - len(v)) < 10:
            if k not in o2o:
                o2o.append(k)
    write_smaller_file(dict_rel, o2o, out_path)

if __name__ == "__main__":
    main()


import gym
import gfootball.env as grf_env
import numpy as np

class GRFWrapperEnv(gym.Env):

    def __init__(self,):
        self.env = None
        self.num_controlled_lagents = 0
        self.num_controlled_ragents = 0
        self.num_controlled_agents = 0
        self.num_lagents = 0
        self.num_ragents = 0
        self.action_space = None
        self.observation_space = None

    def init_args(self, parser):
        env = parser.add_argument_group('GRF')
        env.add_argument('--scenario', type=str, default='academy_3_vs_1_with_keeper',
                         help="Scenario of the game")
        env.add_argument('--num_controlled_lagents', type=int, default=3,
                         help="Number of controlled agents on the left side")
        env.add_argument('--num_controlled_ragents', type=int, default=0,
                         help="Number of controlled agents on the right side")
        env.add_argument('--reward_type', type=str, default='scoring',
                         help="Reward type for training")
        env.add_argument('--render', action="store_true", default=False,
                         help="Render training or testing process")

    def multi_agent_init(self, args):
        self.env = grf_env.create_environment(
            env_name=args.scenario,
            stacked=False,
            representation='multiagent',
            rewards=args.reward_type,
            write_goal_dumps=False,
            write_full_episode_dumps=False,
            render=args.render,
            dump_frequency=0,
            logdir='/tmp/test',
            extra_players=None,
            number_of_left_players_agent_controls=args.num_controlled_lagents,
            number_of_right_players_agent_controls=args.num_controlled_ragents,
            channel_dimensions=(3, 3))
        self.num_controlled_lagents = args.num_controlled_lagents
        self.num_controlled_ragents = args.num_controlled_ragents
        self.num_controlled_agents = args.num_controlled_lagents + args.num_controlled_ragents
        self.num_lagents = self.env.num_lteam_players
        self.num_ragents = self.env.num_rteam_players
        if self.num_controlled_agents > 1:
            action_space = gym.spaces.Discrete(self.env.action_space.nvec[1])
        else:
            action_space = self.env.action_space
        if self.num_controlled_agents > 1:
            observation_space = gym.spaces.Box(
                low=self.env.observation_space.low[0],
                high=self.env.observation_space.high[0],
                dtype=self.env.observation_space.dtype)
        else:
            observation_space = gym.spaces.Box(
                low=self.env.observation_space.low,
                high=self.env.observation_space.high,
                dtype=self.env.observation_space.dtype)

        # check spaces
        self.action_space = action_space
        self.observation_space = observation_space
        return

    # check epoch arg
    def reset(self):
        self.stat = dict()
        obs = self.env.reset()
        if self.num_controlled_agents == 1:
            obs = obs.reshape(1, -1)
        return obs

    def step(self, actions):
        o, r, d, i = self.env.step(actions)
        if self.num_controlled_agents == 1:
            o = o.reshape(1, -1)
            r = r.reshape(1, -1)
        next_obs = o
        rewards = r
        dones = d
        infos = i
        self.stat['success'] = infos['score_reward']

        return next_obs, rewards, dones, infos

    def seed(self):
        return

    def render(self):
        self.env.render()

    def exit_render(self):
        self.env.disable_render()


from flask import jsonify, request, url_for
from . import api
from .. import db
from ..models import Category

@api.route('/categories')
def get_categories():
  categories = Category.query.all()
  return jsonify({'categories' : [category.to_json() for category in categories]})

@api.route('/categories/<int:id>')
def get_category(id):
  category = Category.query.get_or_404(id)
  return jsonify(category.to_json())

@api.route('/categories', methods=['POST'])
def new_category():
  category = Category.from_json(request.json)
  db.session.add(category)
  db.session.commit()
  return jsonify(category.to_json(), 201, {'Location' : url_for('api.get_category', id=category.id, _external=True)})

@api.route('/categories', methods=['PUT'])
def edit_category(id):
  category = Category.query.get_or_404(id)
  category.name = request.json.get('name', category.name)
  category.desc = request.json.get('desc', category.desc)
  db.session.add(category)
  return jsonify(category.to_json())