import argparse
import math
import torch

import marching_cubes
import numpy as np
import open3d as o3d
import trimesh

from network import ImplicitNet

# os.environ["DISPLAY"] = ":0"
# from postprocess import remove_backface

class MeshExtractor(object):
    def __init__(self,  model, voxels, latents, voxel_size,
                 rotations=None, centroids=None, bit=8, interp=False):
        self.model = model
        self.voxels = voxels
        self.latents = latents
        self.voxel_size = voxel_size
        self.rotations = None
        self.centroids = None

        self.resolution = bit
        self.interp = interp
        if interp:
            self.start = -1.
            self.end = 1. - 2./bit
            self.resolution *= 2
        else:
            self.start = -.5
            self.end = .5 - 1./bit
            self.resolution -= 1

        if rotations is not None:
            self.rotations = rotations.to(latents.device).float()
            self.centroids = centroids.to(latents.device).float()

    @ torch.no_grad()
    def get_sdf(self):
        x = y = z = torch.linspace(self.start, self.end, self.resolution)
        xx, yy, zz = torch.meshgrid(x, y, z)
        samples = torch.stack([xx, yy, zz], dim=-1).float()
        samples = samples.reshape(
            1, -1, 3).expand(self.voxels.shape[0], -1, -1)

        if self.centroids is not None:
            samples = samples - self.centroids.unsqueeze(1)
        if self.rotations is not None:
            samples = samples @ self.rotations.transpose(-1, -2)

        latent = self.latents.unsqueeze(1).expand(samples.shape[:-1]+(-1,))
        inputs = torch.cat([samples, latent], dim=-1)
        inputs = inputs.reshape(inputs.shape[0]*inputs.shape[1], -1)

        chunk_size = 10000
        sdf = torch.cat([self.model(inputs[i: i + chunk_size, :].cuda()).squeeze().detach().cpu()
                        for i in range(0, inputs.size(0), chunk_size)], 0)
        sdf_grid = sdf.reshape(-1, self.resolution,
                               self.resolution, self.resolution)
        return sdf_grid

    @ torch.no_grad()
    def linearize_id(self, xyz, n_xyz):
        return xyz[:, 2] + n_xyz[-1] * xyz[:, 1] + (n_xyz[-1] * n_xyz[-2]) * xyz[:, 0]

    @torch.no_grad()
    def create_mesh_overlay(self):
        sdf = self.get_sdf()

        from skimage.measure import marching_cubes
        verts_all = []
        faces_all = []
        num_verts = 0
        for i in range(len(voxels)):
            isdf = sdf[i].detach().cpu().numpy()
            if np.max(isdf) < 0 or np.min(isdf) > 0:
                continue
            results = marching_cubes(isdf, 0)
            if results is not None:
                verts, faces, _, _ = results
                verts /= (self.resolution-1)
                verts -= 0.5
                verts *= self.voxel_size
                verts += voxels[i].numpy()
                faces_all += [faces+num_verts]
                verts_all += [verts]
                num_verts += len(verts)
        verts = np.concatenate(verts_all)
        faces = np.concatenate(faces_all)

        if len(verts) != 0:
            mesh = trimesh.Trimesh(verts, faces).as_open3d
            mesh.compute_vertex_normals()
            o3d.visualization.draw_geometries([mesh])
        else:
            print("failed extracting surface!")

    @ torch.no_grad()
    def create_mesh(self, output=None):
        sdf = self.get_sdf()
        voxels = self.voxels / self.voxel_size
        min_bound = torch.floor(torch.amin(voxels, dim=0)).int()
        max_bound = torch.ceil(torch.amax(voxels, dim=0)).int()
        n_xyz = (max_bound - min_bound + 1).numpy().tolist()
        indexer = torch.ones((math.prod(n_xyz),),
                             device=sdf.device, dtype=torch.long) * -1
        vec_id_batch_mapping = torch.arange(voxels.shape[0]).int()
        voxels = torch.floor(voxels) - min_bound

        latent_vecs_pos = self.linearize_id(voxels, n_xyz).long()
        indexer[latent_vecs_pos] = vec_id_batch_mapping.long()
        std = torch.ones_like(sdf)
        indexer = indexer.view(n_xyz)
        sdf = -sdf

        if self.interp:
            verts, _, _ = marching_cubes.marching_cubes_sparse_interp(
                indexer.cuda(), latent_vecs_pos.cuda(), vec_id_batch_mapping.cuda(),
                sdf.cuda(), std.cuda(), n_xyz, 2000.0, int(2e7)
            )
        else:
            verts, _, _ = marching_cubes.marching_cubes(
                indexer.cuda(), latent_vecs_pos.cuda(), vec_id_batch_mapping.cuda(),
                sdf.cuda(), std.cuda(), n_xyz, 2000.0, int(2e7)
            )

        if len(verts) != 0:
            print(verts.shape)
            verts = verts.cpu().numpy()
            bound_min = min_bound.numpy()
            verts = verts + bound_min
            verts *= voxel_size
            verts = verts.reshape(-1, 3)
            faces = np.arange(verts.shape[0]).reshape(-1, 3)
            mesh = trimesh.Trimesh(verts, faces).as_open3d
            mesh.compute_vertex_normals()

            if output is not None:
                o3d.io.write_triangle_mesh(output, mesh)
            else:
                o3d.visualization.draw_geometries([mesh])
        else:
            print("failed extracting surface!")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('data')
    parser.add_argument('--output', type=str, default=None)
    parser.add_argument('--naive', action='store_true')
    parser.add_argument('--interp', action='store_true')
    parser.add_argument('--res', type=int, default=8)
    args = parser.parse_args()

    data = torch.load(args.data)
    latents = data['latents']
    voxels = data['voxels']
    ckpt = data['model']
    cfg = data["config"]
    rotations = data['rotations']
    centroids = data['centroids']
    voxel_size = data['voxel_size']

    model = ImplicitNet(**cfg)
    model.load_state_dict(ckpt)
    model.cuda()
    model.eval()

    extractor = MeshExtractor(
        model, voxels, latents,
        voxel_size, rotations,
        centroids, bit=args.res,
        interp=args.interp)
    if args.naive:
        extractor.create_mesh_overlay()
    else:
        extractor.create_mesh(args.output)


import pytest
from api import auth_manager, db
from api.models.media_contents_model import MediaContentModel, TypeMediaModel
from api.models.media_tags_model import TagModel, tags
from tests.init_test import client, application, auth_headers, user_admin, headers_user, user, file_name
from tests.test_media.init_media import media
from tests.test_channels.init_channel import channel
from tests.test_tags.init_tags import tags as tg
from config import Config

def test_tag_set_media(client, user, headers_user, media, channel, tg):
    """
        Тест на привязку тега к медиа
    """
    id_tag = 1
    data = {
        "tags": [
            id_tag
        ]
    }
    response = client.put(f'{Config.VERSION}/media/1/settags',
                           json=data,
                           headers=headers_user)

    assert response.status_code == 200
    tag_bd = TagModel.query.filter_by(id_tag=id_tag).one()
    tags_list = db.session.query(tags).filter_by(id_tag=id_tag).all()
    assert id_tag in tags_list[0]
    assert tag_bd.id_tag == id_tag

def test_media_no_exist_id_tag(client, user, headers_user, media, channel, tg):
    """
        Тест на привязку тега к медиа не существующего id  тега
    """
    id_tag = 588
    data = {
        "tags": [
            id_tag
        ]
    }
    response = client.put(f'{Config.VERSION}/media/1/settags',
                           json=data,
                           headers=headers_user)

    assert response.status_code == 404

def test_tag_creation_no_auth(client, user, headers_user, media, channel, tg):
    """
        Тест на привязку тега к медиа
    """
    id_tag = 1
    data = {
        "tags": [
            id_tag
        ]
    }
    response = client.put(f'{Config.VERSION}/media/1/settags',
                           json=data)

    assert response.status_code == 401


# this demos the various matting algorithms found in cvtools

import cv
import matplotlib.pyplot as pl
import numpy as np
import skimage.transform

# load data
path = 'im/matting/'
img = cv.util.imread(path+'inp.png') # raw image
gta = cv.util.imread(path+'gt.png')  # ground-truth alpha
tm = cv.util.imread(path+'tm.png')   # trimap

# process trimap
tm[(tm!=0)&(tm!=1)] = .5 # all 'unknown' pixels have value = .5
if tm.ndim == 2: # convert grayscale to rgb
  tm = np.transpose(np.tile(tm,(3,1,1)),((1,2,0)))

# option to scale down image to reduce computation time
fs = .25 # scale factor
if fs != 1.0:
  [nx,ny,_] = img.shape
  sz = (int(nx*fs),int(ny*fs),3) # new size
  img = skimage.transform.resize(img,sz,mode='constant')
  gta = skimage.transform.resize(gta,sz,mode='constant')
  tm = skimage.transform.resize(tm,sz,mode='constant')
  tm[(tm!=0)&(tm!=1)] = .5

# run bayesian matting demo
a = cv.matting.bayesian(img, tm.copy())
pl.figure(1, (6,2))
pl.subplot(1,4,1)
pl.imshow(img)
pl.axis('off')
pl.title('image', fontsize=8, weight='demibold')
pl.subplot(1,4,2)
pl.imshow(a*img)
pl.axis('off')
pl.title('foreground', fontsize=8, weight='demibold')
pl.subplot(1,4,3)
pl.imshow(gta)
pl.axis('off')
pl.title('ground truth', fontsize=8, weight='demibold')
pl.subplot(1,4,4)
pl.imshow(a)
pl.axis('off')
pl.title('calculated alpha', fontsize=8, weight='demibold')

# run closed-form matting demo
a = cv.matting.natural(img, tm.copy())
pl.figure(2, (6,2))
pl.subplot(1,4,1)
pl.imshow(img)
pl.axis('off')
pl.title('image', fontsize=8, weight='demibold')
pl.subplot(1,4,2)
pl.imshow(a*img)
pl.axis('off')
pl.title('foreground', fontsize=8, weight='demibold')
pl.subplot(1,4,3)
pl.imshow(gta)
pl.axis('off')
pl.title('ground truth', fontsize=8, weight='demibold')
pl.subplot(1,4,4)
pl.imshow(a)
pl.axis('off')
pl.title('calculated alpha', fontsize=8, weight='demibold')

pl.show()


"""
[1,3,-1,-3,5,3,6,7]

Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

"""
import collections
from typing import List

class Solution:
    def maxSlidingWindow(self, nums, k):
        d = collections.deque()
        out = []
        for i, n in enumerate(nums):
            while d and nums[d[-1]] < n:
                d.pop()
            d += i,
            if i >= k - 1:
                out += nums[d[0]],
        return out

print(Solution().maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3))

# public int[] maxSlidingWindow(int[] a, int k) {
# 		if (a == null || k <= 0) {
# 			return new int[0];
# 		}
# 		int n = a.length;
# 		int[] r = new int[n-k+1];
# 		int ri = 0;
# 		// store index
# 		Deque<Integer> q = new ArrayDeque<>();
# 		for (int i = 0; i < a.length; i++) {
# 			// remove numbers out of range k
# 			while (!q.isEmpty() && q.peek() < i - k + 1) {
# 				q.poll();
# 			}
# 			// remove smaller numbers in k range as they are useless
# 			while (!q.isEmpty() && a[q.peekLast()] < a[i]) {
# 				q.pollLast();
# 			}
# 			// q contains index... r contains content
# 			q.offer(i);
# 			if (i >= k - 1) {
# 				r[ri++] = a[q.peek()];
# 			}
# 		}
# 		return r;
# 	}


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 28 03:32:26 2022

The class of hand detector based on mediapipe

@author: Xinyang Chen
"""

import mediapipe as mp
import cv2 as cv

class HandDetector():
    def __init__(self, wid=640, hei=360, mode=False, maxHands=1, model_complexity=1, detectionCon=0.3, trackCon=0.3):
        self.mode = mode# hand tracking mode
        self.maxHands = maxHands# number of hands
        self.model_complexity = model_complexity# complexity of model
        self.detectionCon = detectionCon# detection confidence(palm detector)
        self.trackCon = trackCon# tracking confidence(landmark model)

        self.mpHands = mp.solutions.hands# hand tracking solution
        self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.model_complexity, self.detectionCon, self.trackCon)
        self.mpDraw = mp.solutions.drawing_utils# draw
        self.wid = wid # width of the input image
        self.hei = hei # height of the input image
        self.results = None

    def findHands(self, img, drawOn=None, draw=False):
        """
        @brief: get the hand tracing based on mediapipe
        param img: the input image
        param drawOn: draw tracing result on drawOn
        param draw: enable drawing

        return success: the status of tracing
        return drawOn: output after drawing
        """
        imgRGB = cv.cvtColor(img, cv.COLOR_BGR2RGB)
        self.results = self.hands.process(imgRGB)
        success = False

        if self.results.multi_hand_landmarks:
            success = True
            for handLms in self.results.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(drawOn, handLms, self.mpHands.HAND_CONNECTIONS)

        return success

    def findPosition(self):
        """
        @brief: get the hand tracing based on mediapipe
        param wid: the width of the input image
        param hei: the height of the input image

        return lmList: the list of hand landmarks, elements are id, x position and y position
        """

        lmList = [] # list of the landmarks

        if self.results.multi_hand_landmarks:
            for handlms in self.results.multi_hand_landmarks:
                for id, lm in enumerate(handlms.landmark):
                    # x and y are normalized to [0.0, 1.0] by the image width and height respectively.
                    lmList.append([id, lm.x * self.wid, lm.y * self.hei])

        return lmList


import math
def numop17():
    num=int(input())
    points = []
    for i in range(num):
        str_in=input().split(',')
        point = list(int(x) for x in str_in)
        points.append(point)
    '''string=str_in[2:len(str_in)-2].split("],[")
    for item in string:
        array=item.split(',')
        point=list(int(x) for x in array)
        points.append(point)'''
    point_4=set()
    res = 10000.0*10000.0
    for x1, y1 in points:
        point_4.add((x1, y1))
        for x2, y2 in points:
            if((x2,y2) not in point_4):
                point_4.add((x2, y2))
                for x3,y3 in points:
                    if((x3,y3)not in point_4):
                        point_4.add((x3,y3))
                        for x4,y4 in points:
                            if((x4,y4)not in point_4):
                                point_4.add((x4,y4))
                                if (x1+x2)/2==(x3+x4)/2 and (y1+y2)/2==(y3+y4)/2:
                                    area = math.sqrt(pow(abs(x1 - x3),2)+pow(abs(y1-y4),2)) * math.sqrt(pow(abs(x1 - x4),2)+pow(abs(y1-y4),2))
                                    if area and area < res:
                                        res = area
                                point_4.remove((x4,y4))
                        point_4.remove((x3, y3))
                point_4.remove((x2, y2))
        point_4.remove((x1, y1))

    if(res==10000.0*10000.0):
        res=0.0000
    print('{:.4f}'.format(res))
    return 0

numop17()


# -*- coding: utf-8 -*-

import numpy as np

x = np.array([5, 7, 4, 6, 3, 9])
y = np.array([2, 1, 8, 0])

concat = np.concatenate([x, y])

print(tuple(concat))


# A worksheet element is an element of a report which displays a worksheet.

from . basic_element import BasicElement
from . text_reporter import TextReporter
from . debug_reporter import DebugReporter
from . ixbrl_reporter import IxbrlReporter
from lxml import objectify

class WorksheetElement(BasicElement):
    def __init__(self, id, title, worksheet, data, hide_notes=None):
        super().__init__(id, data)
        self.title = title
        self.worksheet = worksheet
        self.hide_notes = hide_notes
        self.data = data

    @staticmethod
    def load(elt_def, data):

        e = WorksheetElement(
            elt_def.get("id", mandatory=False),
            elt_def.get("title", mandatory=False),
            data.get_worksheet(elt_def.get("worksheet")),
            data,
            elt_def.get("hide-notes", False, mandatory=False)
        )

        return e

    def to_text(self, taxonomy, out):

        if self.title:
            title = "*** {0} ***\n".format(self.title)
            out.write(title)

        rep = TextReporter()
        rep.output(self.worksheet, out, taxonomy)
        out.write("\n")

    def to_debug(self, taxonomy, out):

        rep = DebugReporter()
        rep.output(self.worksheet, out, taxonomy)

    def to_ixbrl_elt(self, par, taxonomy):

        rep = IxbrlReporter(hide_notes = self.hide_notes)
        elt = rep.get_elt(self.worksheet, par, taxonomy, self.data)

        div = par.xhtml_maker.div()
        div.set("class", "worksheet")
        elt.set("id", self.id + "-element")

        if self.title:
            title = par.xhtml_maker.h2(self.title)
            div.append(title)

        div.append(elt)

        return [div]


def maxStrLen(arr):
    resarr=['']
    for x in arr:
        if len(set(x))!=len(x):
            continue
        length=len(resarr)
        for i in range(length):
            if not set(x)&set(''.join(resarr[i])):
                resarr.append(x+resarr[i])

    return max(len(str) for str in resarr)

arr=eval(input())
print(maxStrLen(arr))


# AOC 2022 Day 08 Part 02

def prep_input():
    data = []

    with open("./day08.in") as file:
        for line in file.readlines():
            data.append(list(line.strip()))

    return data

def score(matrix, y, x):
    tree = matrix[y][x]
    up = 0
    down = 0
    left = 0
    right = 0

    if y > 0:
        for i in range(y - 1, -1, -1):
            up += 1

            if matrix[i][x] >= tree:
                break

    if y < len(matrix):
        for i in range(y + 1, len(matrix)):
            down += 1

            if matrix[i][x] >= tree:
                break

    if x > 0:
        for i in range(x - 1, -1, -1):
            left += 1

            if matrix[y][i] >= tree:
                break

    if x < len(matrix[0]):
        for i in range(x + 1, len(matrix[0])):
            right += 1

            if matrix[y][i] >= tree:
                break

    return up * down * left * right

def check_trees(matrix):
    top_score = 0

    for y in range(len(matrix)):
        for x in range(len(matrix[0])):
            current_score = score(matrix, y, x)

            if current_score > top_score:
                top_score = current_score

    return top_score

def main():
    data = prep_input()
    result = check_trees(data)
    print(result)

if __name__ == "__main__":
    main()


import random
import re

from flask import Blueprint, abort, request
from flask_restful import Api

from app.models.interest import MajorInterestModel, MinorInterestModel
from app.views import BaseResource

api = Api(Blueprint(__name__, __name__))

@api.resource('/interest-list')
class InterestList(BaseResource):
    def get(self):
        """
        관심사(스포츠 관련 카테고리) 리스트 반환
        """
        size = request.args['size']

        if not re.match('\d+', size):
            abort(400)

        size = int(size)

        major_interest_count = size // 3
        minor_interest_count = size - major_interest_count

        return self.unicode_safe_json_dumps([{
            'id': str(interest.id),
            'name': interest.name
        } for interest in random.shuffle(MajorInterestModel.objects[:major_interest_count] + MinorInterestModel.objects[:minor_interest_count])])

@api.resource('/interest-list/<keyword>')
class InterestListSearch(BaseResource):
    def get(self, keyword):
        """
        관심사 리스트 검색
        """
        if not keyword:
            abort(400)

        regex = re.compile('.*{}.*'.format(keyword))

        return self.unicode_safe_json_dumps([{
            'id': str(interest.id),
            'name': interest.name
        } for interest in random.shuffle(MajorInterestModel.objects(name=regex) + MinorInterestModel.objects(name=regex))])


import torch
import time
import argparse
from model import fusion_refine,Discriminator
from train_dataset import dehaze_train_dataset
from test_dataset import dehaze_test_dataset
from val_dataset import dehaze_val_dataset
from torch.utils.data import DataLoader
import os
from torchvision.models import vgg16
from utils_test import to_psnr,to_ssim_skimage
from tensorboardX import SummaryWriter
import torch.nn.functional as F
from perceptual import LossNetwork
from torchvision.utils import save_image as imwrite
from pytorch_msssim import msssim
# --- Parse hyper-parameters train --- #
parser = argparse.ArgumentParser(description='RCAN-Dehaze-teacher')
parser.add_argument('-learning_rate', help='Set the learning rate', default=1e-4, type=float)
parser.add_argument('-train_batch_size', help='Set the training batch size', default=20, type=int)
parser.add_argument('-train_epoch', help='Set the training epoch', default=10000, type=int)
parser.add_argument('--train_dataset', type=str, default='')
parser.add_argument('--data_dir', type=str, default='')
parser.add_argument('--model_save_dir', type=str, default='./output_result')
parser.add_argument('--log_dir', type=str, default=None)
# --- Parse hyper-parameters test --- #
parser.add_argument('--test_dataset', type=str, default='')
parser.add_argument('--predict_result', type=str, default='./output_result/picture/')
parser.add_argument('-test_batch_size', help='Set the testing batch size', default=1,  type=int)
parser.add_argument('--vgg_model', default='', type=str, help='load trained model or not')
parser.add_argument('--imagenet_model', default='', type=str, help='load trained model or not')
parser.add_argument('--rcan_model', default='', type=str, help='load trained model or not')
args = parser.parse_args()
# --- train --- #
learning_rate = args.learning_rate
train_batch_size = args.train_batch_size
train_epoch= args.train_epoch
train_dataset=os.path.join(args.data_dir, 'train')

# --- test --- #
test_dataset = os.path.join(args.data_dir, 'test')
val_dataset = os.path.join(args.data_dir, 'val')
predict_result= args.predict_result
test_batch_size=args.test_batch_size

# --- output picture and check point --- #
if not os.path.exists(args.model_save_dir):
    os.makedirs(args.model_save_dir)
output_dir=os.path.join(args.model_save_dir,'output_result')

# --- Gpu device --- #
device_ids = [Id for Id in range(torch.cuda.device_count())]
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# --- Define the network --- #
MyEnsembleNet = fusion_refine(args.imagenet_model, args.rcan_model)
print('MyEnsembleNet parameters:', sum(param.numel() for param in MyEnsembleNet.parameters()))
DNet = Discriminator()
print('# Discriminator parameters:', sum(param.numel() for param in DNet.parameters()))

# --- Build optimizer --- #
G_optimizer = torch.optim.Adam(MyEnsembleNet.parameters(), lr=0.0001)
scheduler_G = torch.optim.lr_scheduler.MultiStepLR(G_optimizer, milestones=[5000,7000,8000], gamma=0.5)
D_optim = torch.optim.Adam(DNet.parameters(), lr=0.0001)
scheduler_D = torch.optim.lr_scheduler.MultiStepLR(D_optim, milestones=[5000,7000,8000], gamma=0.5)
# --- Load training data --- #
dataset = dehaze_train_dataset(train_dataset)
train_loader = DataLoader(dataset=dataset, batch_size=train_batch_size, shuffle=True)
# --- Load testing data --- #
test_dataset = dehaze_test_dataset(test_dataset)
test_loader = DataLoader(dataset=test_dataset, batch_size=test_batch_size, shuffle=False, num_workers=0)

val_dataset = dehaze_val_dataset(val_dataset)
val_loader = DataLoader(dataset=val_dataset, batch_size=1, shuffle=False, num_workers=0)

# --- Multi-GPU --- #
MyEnsembleNet = MyEnsembleNet.to(device)
MyEnsembleNet= torch.nn.DataParallel(MyEnsembleNet, device_ids=device_ids)
DNet = DNet.to(device)
DNet= torch.nn.DataParallel(DNet, device_ids=device_ids)
writer = SummaryWriter(os.path.join(args.model_save_dir, 'tensorboard'))

# --- Define the perceptual loss network --- #
vgg_model = vgg16(pretrained=True)
# vgg_model.load_state_dict(torch.load(os.path.join(args.vgg_model , 'vgg16.pth')))
vgg_model = vgg_model.features[:16].to(device)
for param in vgg_model.parameters():
    param.requires_grad = False

loss_network = LossNetwork(vgg_model)
loss_network.eval()

msssim_loss = msssim

# '''server vgg'''
# vgg_model = vgg16(pretrained=True).features[:16]
# vgg_model = vgg_model.to(device)
# for param in vgg_model.parameters():
#     param.requires_grad = False
# loss_network = LossNetwork(vgg_model)
# loss_network.eval()

# --- Load the network weight --- #
try:
    MyEnsembleNet.load_state_dict(torch.load(os.path.join(args.teacher_model , 'epoch100000.pkl')))
    print('--- weight loaded ---')
except:
    print('--- no weight loaded ---')

# --- Strat training --- #
iteration = 0
for epoch in range(train_epoch):
    start_time = time.time()
    scheduler_G.step()
    scheduler_D.step()
    MyEnsembleNet.train()
    DNet.train()
    print(epoch)
    for batch_idx, (hazy, clean) in enumerate(train_loader):
        # print(batch_idx)
        iteration +=1
        hazy = hazy.to(device)
        clean = clean.to(device)
        output= MyEnsembleNet(hazy)

        DNet.zero_grad()
        real_out = DNet(clean).mean()
        fake_out = DNet(output).mean()
        D_loss = 1 - real_out + fake_out
        # no more forward
        D_loss.backward(retain_graph=True)
        MyEnsembleNet.zero_grad()
        adversarial_loss = torch.mean(1 - fake_out)
        smooth_loss_l1 = F.smooth_l1_loss(output, clean)
        perceptual_loss = loss_network(output, clean)
        msssim_loss_ = -msssim_loss(output, clean, normalize=True)
        total_loss = smooth_loss_l1+0.01 * perceptual_loss+ 0.0005 * adversarial_loss+ 0.5*msssim_loss_

        total_loss.backward()
        D_optim.step()
        G_optimizer.step()

#         if iteration % 2 == 0:
#             frame_debug = torch.cat(
#                 (hazy, output, clean), dim=0)
#             writer.add_images('train_debug_img', frame_debug, iteration)
        writer.add_scalars('training', {'training total loss': total_loss.item()
                                        }, iteration)
        writer.add_scalars('training_img', {'img loss_l1': smooth_loss_l1.item(),
                                                'perceptual': perceptual_loss.item(),
                                            'msssim':msssim_loss_.item()

                                               }, iteration)
        writer.add_scalars('GAN_training', {
            'd_loss':D_loss.item(),
            'd_score':real_out.item(),
            'g_score':fake_out.item()
            }, iteration
            )

    if epoch % 5 == 0:
        print('we are testing on epoch: ' + str(epoch))
        with torch.no_grad():
            psnr_list = []
            ssim_list = []
            recon_psnr_list = []
            recon_ssim_list = []
            MyEnsembleNet.eval()
            for batch_idx, (hazy_up,hazy_down,hazy, clean) in enumerate(test_loader):
                # hazy_up = hazy_up.to(device)
                # hazy_down=hazy_down.to(device)
                clean = clean.to(device)
                hazy = hazy.to(device)
                frame_out = MyEnsembleNet(hazy)
                # frame_out_up = MyEnsembleNet(hazy_up)
                # frame_out_down = MyEnsembleNet(hazy_down)
                # frame_out=(torch.cat([frame_out_up.permute(0,2,3,1), frame_out_down[:,:,80:640,:].permute(0,2,3,1)], 1)).permute(0,3,1,2)
                if not os.path.exists(output_dir):
                    os.makedirs(output_dir)
#                 imwrite(frame_out, output_dir +'/' +str(batch_idx) + '.png', range=(0, 1))

                psnr_list.extend(to_psnr(frame_out, clean))
                ssim_list.extend(to_ssim_skimage(frame_out, clean))

            avr_psnr = sum(psnr_list) / len(psnr_list)
            avr_ssim = sum(ssim_list) / len(ssim_list)
            print(epoch,'dehazed', avr_psnr, avr_ssim)
            frame_debug = torch.cat((frame_out,clean), dim =0)
            writer.add_images('my_image_batch', frame_debug, epoch)
            writer.add_scalars('testing', {'testing psnr':avr_psnr,
                'testing ssim': avr_ssim
                                    }, epoch)
            torch.save(MyEnsembleNet.state_dict(), os.path.join(args.model_save_dir,'epoch'+ str(epoch) + '.pkl'))

writer.close()


import os
from django.test import SimpleTestCase
from api.services import cra

class TestCraWrite(SimpleTestCase):
    # Test against cra in file 00007
    def test_write_in_00007(self):
        data = [
            {
                "sin": "270300379",
                "years": [2020],
                "given_name": "Alice",
                "family_name": "Solange",
                "birth_date": "19710122",
                "application_id": "1234",
            },
            {
                "sin": "302435839",
                "years": [2020],
                "given_name": "Wendy",
                "family_name": "Turner",
                "birth_date": "19780521",
                "application_id": "1234",
            },
            {
                "sin": "129922258",
                "years": [2020],
                "given_name": "Lily",
                "family_name": "Redding",
                "birth_date": "19830707",
                "application_id": "1234",
            },
        ]

        file_contents = cra.write(
            data,
            today="20220516",
            program_code="BCVR",
            cra_env="A",
            cra_sequence="00007",
        )

        cra_in_file_00007_filename = os.path.join(
            os.path.dirname(__file__),
            "cra_in_out",
            "in",
            "TO.ATO#@@00.R7005.IN.BCVR.A00007",
        )

        with open(cra_in_file_00007_filename, "r") as cra_in_file_00007:
            file = cra_in_file_00007.read()

            # Check file contents are exactly equal.
            self.assertEqual(file_contents, file)

    # Test against cra in file 00008
    def test_write_in_00008(self):
        data = [
            {
                "sin": "280671421",
                "years": [2020],
                "given_name": "Nancy",
                "family_name": "Durham",
                "birth_date": "19650217",
                "application_id": "ctW8gU57YX4xfQ9o",
            },
            {
                "sin": "270300379",
                "years": [2020],
                "given_name": "Alice",
                "family_name": "Solange",
                "birth_date": "19710122",
                "application_id": "9uXLvNQS5vkKnscD",
            },
            {
                "sin": "549713485",
                "years": [2020],
                "given_name": "Patty",
                "family_name": "Sullivan",
                "birth_date": "19940529",
                "application_id": "B5t92XeH7NnFUwxc",
            },
            {
                "sin": "302435839",
                "years": [2020],
                "given_name": "Wendy",
                "family_name": "Turner",
                "birth_date": "19780521",
                "application_id": "B5t92XeH7NnFUwxc",
            },
        ]

        file_contents = cra.write(
            data,
            today="20220517",
            program_code="BCVR",
            cra_env="A",
            cra_sequence="00008",
        )

        cra_in_file_00008_filename = os.path.join(
            os.path.dirname(__file__),
            "cra_in_out",
            "in",
            "TO.ATO#@@00.R7005.IN.BCVR.A00008",
        )

        with open(cra_in_file_00008_filename, "r") as cra_in_file_00008:
            file = cra_in_file_00008.read()

            # Check file contents are exactly equal.
            self.assertEqual(file_contents, file)

    # Test against cra in file 00009
    def test_write_in_00009(self):
        data = [
            {
                "sin": "270300379",
                "years": [2020],
                "given_name": "AliceAliceAliceAliceAliceAliceTruncateMe",
                "family_name": "SolangeSolangeSolangeSolangeSolange",
                "birth_date": "19710122",
                "application_id": "1234",
            },
            {
                "sin": "302435839",
                "years": [2020],
                "given_name": "Wendy",
                "family_name": "Turner",
                "birth_date": "19780521",
                "application_id": "1234",
            },
            {
                "sin": "129922258",
                "years": [2020],
                "given_name": "Lily",
                "family_name": "Redding",
                "birth_date": "19830707",
                "application_id": "1234",
            },
        ]

        file_contents = cra.write(
            data,
            today="20220516",
            program_code="BCVR",
            cra_env="A",
            cra_sequence="00009",
        )

        cra_in_file_00009_filename = os.path.join(
            os.path.dirname(__file__),
            "cra_in_out",
            "in",
            "TO.ATO#@@00.R7005.IN.BCVR.A00009",
        )

        with open(cra_in_file_00009_filename, "r") as cra_in_file_00009:
            file = cra_in_file_00009.read()

            # Check file contents are exactly equal.
            self.assertEqual(file_contents, file)


from sys import path
from os.path import dirname

abs_path = dirname(dirname(__file__))
path.insert(0, abs_path+r'/Protein_class')
from Protein_class import Protein
path.__delitem__(0)

from numpy import linspace, exp, zeros, savez
from numpy.random import seed

def d(T, d_max, s):
    return d_max*exp(-s*T)

if __name__ == "__main__":
    d_max = 30000
    s = 5*1e-4
    num_T = 151
    length = 30

    T_list = linspace(0.00001, 1500, num_T)
    E, E_tot, L_tot, L, last_bend = 0, 0, 0, length, 0
    E_list, L_list = zeros(num_T), zeros(num_T)
    seed_list = [1337, 1, 10352, 9234, 999922, 999999999, 235359, 863286248, 124872, 4652742]

    for k in range (len(seed_list)):

        seed(seed_list[k])
        protein = Protein(length, 0)
        U_mat = protein.potentials

        print("\n{}\n".format(k))

        for i in range (num_T):

            print (T_list[i], d(T_list[i], d_max, s))

            protein = Protein(length, T_list[i])
            protein.potentials = U_mat
            E, E_tot, L_tot, L, last_bend = 0, 0, 0, length, 0

            while (protein.valid_bends < d(T_list[i], d_max, s)):
                if protein.bend_protein():
                    L_tot += L*(protein.valid_bends-last_bend)
                    E_tot += E*(protein.valid_bends-last_bend)
                    last_bend = protein.valid_bends
                    L = protein.L
                    E = protein.u
            E_tot += E * (protein.valid_bends - last_bend)
            L_tot += L * (protein.valid_bends - last_bend)
            E_list[i] += E_tot/protein.valid_bends
            L_list[i] += L_tot/protein.valid_bends

    for i in range(num_T):
        E_list[i] = E_list[i]/len(seed_list)
        L_list[i] = L_list[i]/len(seed_list)

    file = abs_path + r'/Data_files/Oppgave_2_1_30.npz'
    savez(file, T_list=T_list, E_list=E_list, L_list=L_list)


import matplotlib.pyplot as plt
from random import choice
class RandomWalk():
    def __init__(self):
        self.x=[0]
        self.y=[0]
        self.num_points=5000

    def fill_walk(self):
        while len(self.x)<self.num_points:
            x_direction = choice([1,-1]) #random.choice需要的是序列，list
            x_distance = choice([1,2,3,4])
            x_step = x_direction*x_distance
            y_direction = choice([1,-1])
            y_distance = choice([1,2,3,4])
            y_step = y_direction*y_distance
            if x_step == 0 and y_step == 0:
                continue

            next_x = self.x[-1]+x_step
            next_y = self.y[-1] + y_step
            self.x.append(next_x)
            self.y.append(next_y)

p = RandomWalk()
p.fill_walk()
plt.scatter(p.x,p.y,s=15)


import os
import os.path as osp
import xml.etree.ElementTree as ET
from tqdm import tqdm

'''
    来源
    分辨率
    x1, y1, x2, y2, x3, y3, x4, y4, 类别, 难度
'''

gsd_dict = {'GF2/GF3': 0.8}

labels = []

def fair1m2dota(data_folder):
    print('-- start fair1m2dota --')
    xml_folder = osp.join(data_folder, 'labelXml')
    txt_folder = osp.join(data_folder, 'labelTxt')  # 保存
    if not osp.exists(txt_folder):
        os.mkdir(txt_folder)
    xml_names = os.listdir(xml_folder)
    for xml_name in tqdm(xml_names):
        xml_path = osp.join(xml_folder, xml_name)
        txt_path = osp.join(txt_folder, xml_name.replace('.xml', '.txt'))
        # 写txt
        with open(txt_path, 'w') as f:
            tree = ET.parse(xml_path)
            root = tree.getroot()
            imagesource = root.find('source').find('origin').text
            f.write('imagesource:' + imagesource + '\n')  # 写来源
            f.write('gsd:' + str(gsd_dict[imagesource]) + '\n')  # 写分辨率
            for obj in root.find('objects').findall('object'):
                clas = obj.find('possibleresult').find('name').text.replace(' ', '-')
                if clas not in labels:
                    labels.append(clas)
                points = obj.find('points')
                x_list = []
                y_list = []
                for point in points.findall('point'):
                    xy = point.text.split(',')
                    x = float(xy[0])
                    y = float(xy[1])
                    x_list.append(x)
                    y_list.append(y)
                point_str = points_str(x_list, y_list)
                f.write(point_str)  # 写坐标
                f.write(clas + ' 0\n')  # 写类别
    return labels

def points_str(x_list, y_list):
    tmp = 0
    for i in range(5):
        for j in range(i, 5, 1):
            if x_list[i] == x_list[j] and y_list[i] == y_list[j]:
               tmp = j
               break
    del x_list[tmp]
    del y_list[tmp]
    point_str = ''
    for x, y in zip(x_list, y_list):
        point_str += str(x)
        point_str += ' '
        point_str += str(y)
        point_str += ' '

    return point_str

if __name__ == '__main__':
    data_path = r"D:\WorkSpace\NerualNet\dataset\FAIR1M2.0\validation"
    fair1m2dota(data_path)
    print(labels)


#!/usr/bin/env python
# coding: utf-8

# ### 11 Cats_and_dogs_transfer_learning

# In[1]:

# cats_and_dogs classification model with CNN
# train : 2000 images [cat(1000) + dog(1000)]  , size는 다름
# validation : 1000 images [cat(500) + dog(500)] , size는 다름

# In[2]:

import tensorflow as tf
import urllib
import zipfile
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator

from tensorflow.keras import layers
from tensorflow.keras import Model

# In[3]:

# Google InceptionV3 pretrained model 가중치 다운로드
_INCEPTION_URL = "https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5"
urllib.request.urlretrieve(_INCEPTION_URL, 'tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')

# In[4]:

# Colab용 : Linux/GPU사용
# !wget --no-check-certificate \
#     https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \
#     -O /tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5

# In[5]:

# Inception model(pre-trained model) 불러오기

from tensorflow.keras.applications.inception_v3 import InceptionV3

locals_weights_file = 'tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
pre_trained_model = InceptionV3(input_shape=(150,150,3),
                               include_top=False,
                               weights=None)

pre_trained_model.load_weights(locals_weights_file)

print(len(pre_trained_model.layers)) #311

# 사전 훈련 계층의 가중치의 훈련(학습) 가능 여부를 설정 : False(학습 안함) , 고정
for layer in pre_trained_model.layers:
    layer.trainable = False

pre_trained_model.summary()

# In[6]:

# 마지막 층 출력 확인하기(마지막 출력 층을 선택)
#  mixed7 (Concatenate)  :    (None, 7, 7, 768)
last_layer = pre_trained_model.get_layer('mixed7')
print('last layer output shape:', last_layer.output_shape)
last_output = last_layer.output

# In[7]:

# layer의 층 번호 구하기
# for i,layer in enumerate(pre_trained_model.layers): # 311회 반복
#     if(layer.name == 'mixed7'):
#         print(i+1)

# layer의 층 번호 구하기
pre_trained_model.layers.index(pre_trained_model.get_layer('mixed7')) + 1

# In[8]:

# 모델 구성  : pre_trained model layer(229)  + 4  --> 233 layers

# (1) Flatten layer
x = tf.keras.layers.Flatten()(last_output)

# (2) FC(=Dense) layer
x = layers.Dense(1024,activation='relu')(x)

# (3) Dropout layer
x = layers.Dropout(0.2)(x)

# (4) Output layer : 2진분류, sigmoid
x = layers.Dense(1,activation='sigmoid')(x)

model = Model(pre_trained_model.input,x)
model.compile(optimizer=RMSprop(learning_rate=0.0001), # 1e-4
             loss='binary_crossentropy',
             metrics=['accuracy'])
model.summary()

# In[9]:

# cats_and_dogs 데이터셋 다운로드 , Windows용
_TRAIN_URL = "https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip"
urllib.request.urlretrieve(_TRAIN_URL, 'tmp/cats_and_dogs_filtered.zip')

# In[10]:

#압축해제
local_zip = 'tmp/cats_and_dogs_filtered.zip'
zip_ref = zipfile.ZipFile(local_zip,'r')
zip_ref.extractall('tmp/')
zip_ref.close()

# In[11]:

# 이미지 데이터 경로 설정
import os

base_dir = 'tmp/cats_and_dogs_filtered'

train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')

# Directory with our training cat/dog pictures
train_cats_dir = os.path.join(train_dir, 'cats')
train_dogs_dir = os.path.join(train_dir, 'dogs')

# Directory with our validation cat/dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
train_cat_fnames = os.listdir( train_cats_dir )
train_dog_fnames = os.listdir( train_dogs_dir )

print(train_cat_fnames[:10])
print(train_dog_fnames[:10])

print('total training cat images :', len(os.listdir(train_cats_dir ) ))
print('total training dog images :', len(os.listdir(train_dogs_dir ) ))

print('total validation cat images :', len(os.listdir( validation_cats_dir ) ))
print('total validation dog images :', len(os.listdir( validation_dogs_dir ) ))

# In[12]:

# 이미지 증강

train_datagen = ImageDataGenerator(rescale=1./255,
                                  rotation_range=40,
                                  width_shift_range=0.2,
                                  height_shift_range=0.2,
                                  shear_range=0.2,
                                  zoom_range=0.2,
                                  horizontal_flip=True)
#print(type(train_datagen))

validation_datagen = ImageDataGenerator(rescale=1./255,
                                  rotation_range=40,
                                  width_shift_range=0.2,
                                  height_shift_range=0.2,
                                  shear_range=0.2,
                                  zoom_range=0.2,
                                  horizontal_flip=True)

#train data의 generator
train_generator = train_datagen.flow_from_directory(
                  train_dir,
                  target_size=(150,150), # resize될 크기
                  batch_size=20,
                  class_mode='binary', # 2진 분류

)

#validation data의 generator
validation_generator = validation_datagen.flow_from_directory(
                  validation_dir,
                  target_size=(150,150), # resize될 크기
                  batch_size=20,
                  class_mode='binary', # 2진 분류

)

print(train_generator)

# In[13]:

# 학습
history = model.fit(
                train_generator, # x
                steps_per_epoch=100, # 2000/20 = 8.xxx, train image number/train batch_size
                epochs=20,
                validation_data=validation_generator,
                validation_steps=50 # 1000/20
)

# In[16]:

# 학습 결과 시각화
#-----------------------------------------------------------
# Retrieve a list of list results on training and test data
# sets for each training epoch
#-----------------------------------------------------------
acc      = history.history['accuracy' ]
val_acc  = history.history['val_accuracy' ]
loss     = history.history['loss' ]
val_loss = history.history['val_loss' ]

epochs   = range(len(acc)) # Get number of epochs

#------------------------------------------------
# Plot training and validation accuracy per epoch
#------------------------------------------------
plt.plot(epochs, acc, 'g', label='Training accuracy')
plt.plot(epochs, val_acc, 'r', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

plt.plot(epochs, loss, 'g', label='Training Loss')
plt.plot(epochs, val_loss, 'r', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()

#  결과 : InceptionV3 pretrained model 사용 시 validation의 정확도가 크게 향상됨
#  val_accuracy: 0.7360 ---> val_accuracy: 0.9660

# In[ ]:


from tkinter import *
from tkinter.messagebox import *
from userdata import *

app = Tk()
# Message Window

def messagePop():

    get_data()

    name = x.get() and y.get()

    for key, value in assignment.items():
        for v in value:
            if name in v:
                messagebox.showinfo("Result", v + " 's personal tutor is " +key)

app.configure(bg='cornflower blue')

COLORS  =['blue', 'gold', 'cornflower blue']

# The position and size relative to the screen
app.geometry('500x500+450+140')

# The title of the program
app.title('Search tutor for individual student')

# Object positioning in the program
Label(app, text="Please enter the first and last name of student.").place(x=10,y=0)
Label(app, text= "First Name:", bg="gold", fg="blue").place(x=10,y=120)
Label(app, text="Last Name:", bg="gold", fg="blue").place(x=10,y=170)

# Entry
def get_data():
    x_data = x.get()
    y_data = y.get()
    print ("x_data = {0} , y_data = {1} ".format(x_data,y_data))

x = Entry(app)
y = Entry(app)

x.place(x=90,y=120)
y.place(x=90,y=170)

# Buttons
B1 = Button(app,text='Search',bg='gold',fg='blue', command = messagePop).place(x=425,y=450)

app.mainloop()


import subprocess
import os
import signal
import time
import argparse
import socket

pool_path = 'pool' + '.' + socket.gethostname()

def start_job(gpu=0, timeout=0):
    # Check if GPU already assigned
    if 'CUDA_VISIBLE_DEVICES' in os.environ:
        print('CUDA_VISIBLE_DEVICES externally set to ' + os.environ['CUDA_VISIBLE_DEVICES'])
        return

    #Check the pool for a idle job
    start_new_job = True
    if os.path.isfile(pool_path):
        file = open(pool_path, 'r+')
        if file.readline() == '':
            file.seek(0)
            file.write(str(os.getpid()))
            start_new_job = False
        file.close()

    # Start new grid engine job using qsub
    if start_new_job:
        self_path = os.path.dirname(os.path.realpath(__file__))
        cmd = 'qsub -cwd -o interactive.log -e interactive.log -b y -l gpu=' + str(gpu) \
              + ' python3 -u ' + self_path + '/interactive.py --pid ' + str(os.getpid()) + ' --timeout ' + str(timeout)
        try:
            subprocess.call(cmd.split())
        except OSError as e:
            print('Failed to run qsub! Continues to run locally instead, i.e. outside grid engine.')
            return

    # Wait for job to start
    envfile = '.env.' + str(os.getpid()) + '.' + socket.gethostname()
    while not os.path.isfile(envfile):
        time.sleep(1)

    _load_env(envfile)
    os.remove(envfile)

def _load_env(envfile):
    file = open(envfile, 'r')
    os.environ['CUDA_VISIBLE_DEVICES'] = file.readline()
    print('CUDA_VISIBLE_DEVICES set to ' + os.environ['CUDA_VISIBLE_DEVICES'])
    file.close()

# ============ Code below is run by grid engine job (queue job) ====================

def _save_env(envfile):
    file = open(envfile, 'w')
    file.write(os.environ.get('CUDA_VISIBLE_DEVICES', '-1'))
    file.close()

def _exit_gracefully(signum, frame):
    print('Queue job received external termination signal (maybe from qdel).')

    if state == 'running':
        print('Terminating batch job before proceeding to terminate queue job.')
        os.kill(args.pid, signal.SIGTERM)
    elif state == 'pooled':
        print('Removing job from pool')
        os.remove(pool_path)

    print('===================================================')
    exit(0)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Tool for easy management of GE on Titan')
    parser.add_argument('--pid', type=int, default=-1,
                        help='Pid of the main process running the experiment.')
    parser.add_argument('--timeout', type=int, default=0,
                        help='How long a job will be kept alive after process dies to be used for next process.')
    args = parser.parse_args()

    envfile = '.env.' + str(args.pid) + '.' + socket.gethostname()
    _save_env(envfile)

    signal.signal(signal.SIGINT, _exit_gracefully)
    signal.signal(signal.SIGTERM, _exit_gracefully)

    state = 'running'
    print(state)
    while True:
        if state == 'running':
            if not os.path.exists('/proc/' + str(args.pid)):
                if args.timeout == 0:
                    break
                elif os.path.isfile(pool_path):
                    print('Already a job in the pool. Terminating queue job.')
                    break
                else:
                    open(pool_path, 'w').close()
                    pool_start_time = time.time()
                    state = 'pooled'
                    print(state)
        elif state == 'pooled':
            file = open(pool_path, 'r')
            pid = file.readline()
            file.close()
            if not pid == '':
                os.remove(pool_path)
                args.pid = int(pid)
                envfile = '.env.' + str(args.pid) + '.' + socket.gethostname()
                _save_env(envfile)
                state = 'running'
                print(state)
            elif (time.time()-pool_start_time) > args.timeout:
                print('pool timed out.')
                os.remove(pool_path)
                break

        time.sleep(1)

    print('Batch job has been terminated. Exiting Queue job...')
    print('===================================================')


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 27 22:51:55 2018

@author: yannanlin
"""

import nibabel as nib
import pandas as pd
import math

def run(cursor, subject_id, smr_path):

    #####################################################################
    ###################Grab Data from Database###########################
    #####################################################################

    # get coordiante data from database - channel table
    cursor.execute("""select * from channels where sid = %s""", (subject_id))
    coord_data = cursor.fetchall()

    # get channel
    channel = []
    for i in range(len(coord_data)):
        channel.append(coord_data[i][1])

    # get original x, y, z
    x = []
    y = []
    z = []
    for i in range(len(coord_data)):
        x.append(coord_data[i][3])
        y.append(coord_data[i][4])
        z.append(coord_data[i][5])

    # format coordinates to a list of [x1,y1,z1]
    electrode_coord_list = []
    for a in range(len(x)):
        electrode_coord_list.append([x[a],y[a],z[a]])

    # get SMR data from database - subject table
    #####TO DO#####
    mr_brain_mask = nib.load(smr_path) ## find the skull-stripped mr data??????
    mr_brain_mask_data = mr_brain_mask.get_fdata()
    x,y,z = mr_brain_mask_data.shape

    #####################################################################
    ###################Channel Correction################################
    #####################################################################

    # find coordinates of all the points that make up the brain mask
    coord_list = []
    for x in range(mr_brain_mask_data.shape[0]):
        for y in range(mr_brain_mask_data.shape[1]):
            for z in range(mr_brain_mask_data.shape[2]):
                if mr_brain_mask_data[x,y,z] > 0:
                    coord_list.append([x,y,z])

    # function to correct the coordinates of the floating electrodes
    class ChannelCorrection(object):
        '''
        Function to correct the coordinates of the floating electrodes, if any.
        For an ECoG subject, skull-stripped MR data needs to be passed in.

        PointDistance function calculates the distance between the electrode and
        every point on the surface mask.

        CoordCorrection works to
        1 calculate all the distances between the
        elecrtrode and every point on the surface mask,
        2 find the nearest point
        that renders the shorest distance between the electrode and the point on
        the surface mask,
        3 compare the shorest distance to a threshold value,
        4 if the shorest distance is greater than the threshold value, the
        coordinates of the electrode will be corrected by using the coordinates
        of the point on the surface mask instead.

        Threshold value 6.71 is based on the largest distance of the 63 EEG
        electrodes and only works for mlab.point3d(x,y,z, scale_factor=10).
        If scale_factor changes, the threshold value will need to be adjusted
        accordingly.

        Input and output data format: [[x1,y1,z1],[x2,y2,z2]...]

        '''
        def __init__(self, mr_data):
            self.mr_data = mr_data

        def get_mr_data(self):
            return self.mr_data

        # calculate distance between two 3d points
        def PointDistance(self,x1, y1, z1, x2, y2, z2):
            point_dis = math.sqrt(math.pow(x2 - x1, 2) +
                        math.pow(y2 - y1, 2) +
                        math.pow(z2 - z1, 2)* 1.0)
            return point_dis

        # find the coords of the point that gives the shortest distance
        def CoordCorrection(self):
            min_list = []
            for a in range(len(electrode_coord_list)):
                min_dist = 100000 #reset min_dist to 100000
                for i in range(len(coord_list)):
                    dis = self.PointDistance(coord_list[i][0],
                                              coord_list[i][1],
                                              coord_list[i][2],
                                              electrode_coord_list[a][0],
                                              electrode_coord_list[a][1],
                                              electrode_coord_list[a][2])
                    if dis < min_dist:
                        min_dist = dis
                        min_list = []
                        min_list.append([coord_list[i][0], coord_list[i][1], coord_list[i][2]])

                if min_dist > 6.71:
                    electrode_coord_list[a] = min_list[0]
                # print(electrode_coord_list[a])
            return electrode_coord_list #format [[x1,y1,z1],[x2,y2,z2]]

    # create an object
    brain = ChannelCorrection(mr_brain_mask_data)
    electrode_coord_list_update = brain.coord_correction()

    # change format of coordinates before updating database
    x_new = []
    y_new = []
    z_new = []

    for i in range(len(electrode_coord_list_update)):
        x_new.append(electrode_coord_list_update[i][0])
        y_new.append(electrode_coord_list_update[i][1])
        z_new.append(electrode_coord_list_update[i][2])

    update_coord = pd.DataFrame(
                                {'x_new': x_new,
                                'y_new': y_new,
                                'z_new': z_new
                                })

    #####################################################################
    ###################Update Database###################################
    #####################################################################

    update_channels = "UPDATE channels set x=%s, y=%s, z=%s WHERE sid = subject_id;"

    for i in range(len(update_coord)):
        cursor.execute(update_channels, (int(update_coord.iloc[:,0][i]),
                                         int(update_coord.iloc[:,1][i]),
                                         int(update_coord.iloc[:,2][i])))


# -*- coding: utf-8 -*-
"""
Created on Fri May 24 17:42:08 2019

@author: Kyle DeBry
"""

from pure_photonics_utils import ITLA
from laser import Laser
import time

laser = Laser('COM2', 115200)

freq = 193

laser_err = laser.laser_on(freq)

if laser_err == ITLA.NOERROR:

    for i in range(9):
        freq = 193 + i * 0.35
        laser.clean_jump(freq)
        time.sleep(1)

    time.sleep(1)
    laser.ITLACommunicate(ITLA.REG_Cjumpon, 0, ITLA.WRITE)

    laser.laser_off()

laser.ITLADisconnect()


import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

ser_obj = Service("/home/prashant/Documents/Browser_Driver/chrome_102/chromedriver")
driver = webdriver.Chrome(service=ser_obj)

url = "https://demo.nopcommerce.com/register"
driver.get(url)
driver.maximize_window()

# Conditional Commands
# is_displayed() and is_enabled()
search_box = driver.find_element(By.XPATH, "//input[@id='small-searchterms']")
print("Display Status: ", search_box.is_displayed())  # True
print("Enable Status: ", search_box.is_enabled())  # True

# is_selected() - used for radio_buttons and check_boxes
rd_male = driver.find_element(By.XPATH, "//input[@id='gender-male']")
rd_female = driver.find_element(By.XPATH, "//input[@id='gender-female']")

print(rd_male.is_selected())  # False
print(rd_female.is_selected())  # False

rd_male.click()  # selected radio button
time.sleep(3)
print("After Selecting male radio button....")
print(rd_male.is_selected())  # True
print(rd_female.is_selected())  # False

rd_female.click()  # selected radio button
time.sleep(3)
print("After Selecting female radio button....")
print(rd_male.is_selected())  # False
print(rd_female.is_selected())  # True

driver.quit()


import csv
import os
import view

os.system('clear')
FILENAME = 'Telephone_book.csv'

def Create_New_Contact():
    global surname
    global name
    global telephone
    global comment
    global data
    surname = view.Get_Surname()
    name = view.Get_Name()
    telephone = view.Get_Telephone_Number()
    comment = view.Get_Comment()
    data = [surname, name, telephone, comment]
    return data

def Create_CSV_Line(FILENAME):
    title = ['Фамилия', 'Имя', 'Телефон', 'Комментарий']
    with open(FILENAME, 'w', newline="") as file:
        writer = csv.writer(file)
        writer.writerow(title)

def Create_CSV_Row(FILENAME):
    title = ['Справочник в столбик']
    with open(FILENAME, 'w', newline="") as file:
        for i in title:
            writer = csv.writer(file)
            writer.writerow(i.split('-'))

def Import_Data_Line(list_my, FILENAME):
    with open(FILENAME, 'a', newline="") as file:
        writer = csv.writer(file)
        writer.writerow(list_my)

def Import_Data_Row(new_contact, path):
    title = ['Фамилия', 'Имя', 'Телефон', 'Комментарий']
    tuple_1 = []
    for i in range(len(title)):
        tuple_1.append((title[i].split() + new_contact[i].split()))
    with open (path, 'a', newline='') as file:
        for i in tuple_1:
            writer = csv.writer(file)
            writer.writerow(i)
        writer.writerow('')

def Export_Data_Line(path):
    exp_data = []
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for row in reader:
            if not 'Фамилия' in row:
                exp_data.append(row)
    return exp_data

def Imp_Exp_Data_Row_My_Data(new_contact, path):
    with open (path, 'a', newline='') as file:
        for i in new_contact:
            writer = csv.writer(file)
            writer.writerow(i)
        writer.writerow('')

def Export_Data_Row_My_Data(path):
    exp_data = []
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for line in reader:
            if 'Фамилия' in line or 'Имя' in line or\
                'Телефон' in line or 'Комментарий' in line:
                exp_data.append(line)
    return exp_data

def Export_Data_Row_Any(path):
    exp_data = []
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for line in reader:
            exp_data.append(line)
    return exp_data

def Export_And_Print_Data_In_Terminal(path):
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for line in reader:
            print(line)


line=[int(x) for x in input().split()]
wordNO=line[0]
queryNO=line[1]
words=[]
for i in range(wordNO):
    words.append(input())
for i in range(queryNO):
    line=[int(x) for x in input().split()]
    start=line[0]
    end=line[1]
    wordsSubset=words[start-1:end]
    wordsSubset.sort()
    print(wordsSubset[-1])


#  Caesar`s code
# "Hello my little friends!", offset = 37,
# "Hello world!", offset = 7
message = "Hello world!"  # input("Введите сообщение: ")
offset = 7  # int(input("Введите сдвиг: "))
encoded_message = ""
for ch in message:
    pos = ord(ch) - ord('a')  # 21
    pos = (pos + offset) % 26  # 2
    new_char = chr(pos + ord("a"))
    encoded_message += new_char
    a=0
print(encoded_message)


# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
class BathPipeline(object):
    def process_item(self, item, spider):
        return item

class MysqlDB(object):
    def __init__(self):
        try:
            self.conn = pymysql.connect('localhost', 'root', '123456', 'test', charset='utf8')
            self.cursor = self.conn.cursor()
        except Exception as e:
            print('连接数据库失败：%s' % str(e))

    def close(self):
        self.cursor.close()
        self.conn.close()

class MyprojectPipeline (MysqlDB):
    def process_item(self, item, spider):
        # sql = 'insert into info2(university, department, programme, degree_type, overview, start_date, duration'\
        #       'modules, teaching_assessment)'\
        #       'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ' \
            # 'on duplicate key update department = values (department),modules = values (modules),entry_requirements = values(entry_requirements),location = values(location),programme = values(programme),degree_type = values(degree_type),tuition_fee= values(tuition_fee),duration= VALUES (duration),start_date=VALUES (start_date),IELTS=VALUES (IELTS),TOEFL = values(TOEFL),start_date=VALUES (start_date)'
        sql = 'insert into tmp_school_major_uk(university, location, department, programme, degree_type, overview, ucas_code,' \
              'start_date, duration, ' \
              'modules, teaching_assessment, IELTS, TOEFL, deadline, entry_requirements, chinese_requirements, other)VALUES' \
              '  (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
        try:
            self.cursor.execute(sql, (item["university"], item['location'], item["department"], item["programme"], item["degree_type"],
                                      item["overview"], item["ucas_code"], item["start_date"], item["duration"], item["modules"], item['teaching_assessment'],
                                      item['IELTS'], item["TOEFL"], item["deadline"], item["rntry_requirements"], item['chinese_requirement'], item["other"]))
            #     item["overview"], item["start_date"], item["duration"],))
            # self.cursor.execute(sql, (
            #     item["university"]
            #     , item["department"], item["programme"], item["degree_type"],
            #     item["overview"], item["start_date"], item["duration"],
            #     item["modules"], item['teaching_assessment']
            # ))
            self.conn.commit()
        except Exception as e:
            self.conn.rollback()
            print(e)
            print("执行sql语句失败")

        return item


from configurations import Configuration, values
import dj_database_url
import dj_email_url

from .. import auth, base, security, static

class Common(
    auth.AuthenticationSettings,
    base.DjangoOverrides,
    security.SecuritySettings,
    static.StaticMedia,
    Configuration,
):
    """
    Custom settings that are likely to be common among environments
    """

    DATABASE_URL = values.Value(
        f'sqlite:///{base.BASE_DIR / "db.sqlite"}',
        environ_name='DATABASE_URL',
        environ_prefix='',
    )
    DATABASES = {'default': dj_database_url.parse(DATABASE_URL)}

    EMAIL_URL = values.Value(
        'console://',
        environ_name='EMAIL_URL',
        environ_prefix='',
    )
    vars().update(dj_email_url.parse(EMAIL_URL))  # Load EMAIL_* into the class

    LANGUAGE_CODE = 'en-us'


# myApp里面的urls路由配置
from django.conf.urls import url, include
from django.contrib import admin
from . import view_firstWeek, view_secondWeek
from . import views

admin.autodiscover()

urlpatterns = [
    url(r'^$', views.index),  # 仅主页直接转到views中的index函数

    url(r'^regist/$', views.regist, name='1'),  # regist需求转至views中的regist函数,下同
    url(r'^login/$', views.login, name='2'),
    url(r'^index/$', views.index, name='3'),
    url(r'^login/(\d+)$', views.check_teacher_info, name='4'),
    url(r'^login/0/(\d+)$', views.alter_teacher_info, name='5'),
    url(r'^login/1/(\d+)$', views.check_student_info, name='6'),
    url(r'^login/2/(\d+)$', views.alter_student_info),
    url(r'^login/3/(\d+)$', views.add_teacher_grade),
    url(r'^login/4/(\d+)$', views.delete_teacher_grade),  # preparation的模板及views的视图

    url(r'^login/5/(\d+)$', view_firstWeek.launch_homework, name='launch'),  # firstWeek的模板及view_firstWeek的视图
    url(r'^login/6/(\d+)$', view_firstWeek.check_student_finished_homework_html2),  # 学生查看已提交作业html2
    url(r'^login/7/(\d+)$', view_firstWeek.submit_student_homework),  # 提交作业
    url(r'^login/8/(\d+)/(\d+)$', view_firstWeek.check_teacher_homework, name='7'),  # 老师查看作业完成情况
    url(r'^login/8/1/1/(\d+)$', view_firstWeek.teacher_alter_homework, name='8'),  # 老师修改对应作业
    url(r'^login/8/1/2/(\d+)$', view_firstWeek.delete_teacher_homework, name='9'),  # 老师删除对应作业
    url(r'^login/9/2/(\d+)/(\d+)$', view_firstWeek.check_submission_homework, name='10'),  # 老师批改学生列表
    url(r'^login/9/2/2/(\d+)/(\d+)$', view_firstWeek.check_common_student_the_homework, name='11'),  # 老师查看正确或订正正确且未迟交的学生名单
    url(r'^login/9/(\d+)/(\d+)$', view_firstWeek.check_feedback_homework, name='12'),  # 老师查看反馈学生列表
    url(r'^login/9/1/1/(\d+)/(\d+)$', view_firstWeek.check_redo_homework, name='13'),  # 老师查看未订正学生列表
    url(r'^login/9/1/2/(\d+)/(\d+)$', view_firstWeek.check_not_submitted_homework, name='14'),  # 老师查看未提交学生列表
    url(r'^login/10/(\d+)$', view_firstWeek.correct_homework),  # 老师批改学生作业
    url(r'^login/10/1/(\d+)$', view_firstWeek.correct_feedback_homework),  # 老师批改反馈内容
    url(r'^login/11/(\d+)$', views.check_gradelist),  # 老师查看所属班级
    url(r'^login/12/(\d+)$', views.check_studentlist),  # 老师查看所属班级学生情况
    url(r'^login/14/(\d+)/(\d+)/(\d+)$', view_firstWeek.check_student_finished_homework_html3_handInTime, name='15'),  # 学生查看已提交作业html3（默认提交日期排序）
    url(r'^login/14/1/(\d+)/(\d+)/(\d+)$', view_firstWeek.check_student_finished_homework_html3_createtime, name='16'),  # 学生查看已提交作业html3（改为发布日期排序）
    url(r'^login/13/(\d+)$', view_firstWeek.check_student_unfinished_homework_html2),  # 学生查看未提交作业html2
    url(r'^login/15/(\d+)/(\d+)/(\d+)$', view_firstWeek.check_student_unfinished_homework_html3_sorted_by_deadline),  # 学生查看未提交作业html3（默认截止日期排序）
    url(r'^login/15/1/(\d+)/(\d+)/(\d+)$', view_firstWeek.check_student_unfinished_homework_html3_sorted_by_createtime),  # 学生查看未提交作业html3（改为发布日期排序）
    url(r'^login/14/1/1/(\d+)/(\d+)/(\d+)$', view_firstWeek.feedback_homework, name='17'),  # 学生反馈作业

    url(r'^login/16/(\d+)$', view_secondWeek.launch_objective_item),  # 发布客观题
    url(r'^login/17/(\d+)/(\d+)$', view_secondWeek.teacher_check_objective_item),  # 老师查看客观题
    url(r'^login/18/(\d+)/(\d+)$', view_secondWeek.teacher_check_student_submission),  # 老师查看学生客观题提交情况
    url(r'^login/18/1/(\d+)/(\d+)$', view_secondWeek.teacher_check_not_submitted_objective),  # 老师查看客观题未提交或迟交学生名单
    url(r'^login/19/(\d+)$', view_secondWeek.check_unfinished_objective_item_html2),  # 学生查看未提交客观题html2
    url(r'^login/20/(\d+)$', view_secondWeek.submit_objective_item),  # 学生提交客观题作业
    url(r'^login/21/(\d+)$', view_secondWeek.check_finished_objective_item_html2),  # 学生查看已提交客观题html2
    url(r'^login/22/(\d+)/(\d+)/(\d+)$', view_secondWeek.check_unfinished_objective_item_html3_sorted_by_deadline, name='18'),  # 学生查看未提交客观题html3（默认截止日期排序）
    url(r'^login/22/1/(\d+)/(\d+)/(\d+)$', view_secondWeek.check_unfinished_objective_item_html3_sorted_by_createtime, name='19'),  # 学生查看未提交客观题html3（改为发布日期排序）
    url(r'^login/23/(\d+)/(\d+)/(\d+)$', view_secondWeek.check_finished_objective_item_html3_handInTime),  # 学生查看已提交客观题html3（默认提交日期排序）
    url(r'^login/23/1/(\d+)/(\d+)/(\d+)$', view_secondWeek.check_finished_objective_item_html3_createtime),  # 学生查看已提交客观题html3（改为发布日期排序）

    url(r'^login/download/(?P<file_name>.*)/$', view_firstWeek.download, name='20'),  # 下载作业
]


# Greedy algorithm

from cs50 import get_float

# Get change input
change = -1
while change < 0:
    change = get_float("Change owed: ")

# Convert change to cents
change *= 100
change = round(change)

# Set minimum number of coins
min_coins = 0

# Determine how many coins of each coin_type we'll use starting from biggest
coin_types = [25, 10, 5, 1]
for coin_value in coin_types:
    while change >= coin_value:
        change -= coin_value
        min_coins += 1

print(f"{min_coins}")


import numpy as np
from scipy import fftpack # Tools for fourier transform
from scipy import linalg # Linear algebra for dense matrix
from numba import njit
from numba.core.registry import CPUDispatcher
from types import FunctionType

class SplitOpSchrodinger2D(object):
    """
    The second-order split-operator propagator of the 2D Schrodinger equation in the coordinate representation
    with the time-dependent Hamiltonian H = K(P1, P2, t) + V(X1, X2, t).
    """
    def __init__(self, x1_grid_dim, x2_grid_dim, x1_amplitude, x2_amplitude, v, k, dt, diff_k_p1=None, diff_k_p2=None,
                 diff_v_x1=None, diff_v_x2=None, t=0., abs_boundary=1., **kwargs):
        """
        :param x1_grid_dim: the x1 grid size
        :param x2_grid_dim: the x2 grid size

        :param x1_amplitude: the maximum value of the x1 coordinates
        :param x2_amplitude: the maximum value of the x2 coordinates

        :param v: the potential energy (as a function)
        :param k: the kinetic energy (as a function)

        :param diff_k_p1: the derivative of the kinetic energy w.r.t. p1 for the Ehrenfest theorem calculations
        :param diff_k_p2: the derivative of the kinetic energy w.r.t. p2 for the Ehrenfest theorem calculations

        :param diff_v_x1: the derivative of the potential energy w.r.t. x1 for the Ehrenfest theorem calculations
        :param diff_v_x2: the derivative of the potential energy w.r.t. x2 for the Ehrenfest theorem calculations

        :param t: initial value of time
        :param dt: time increment
        :param abs_boundary: absorbing boundary
        :param kwargs: ignored
        """

        # save the parameters
        self.x1_grid_dim = x1_grid_dim
        self.x2_grid_dim = x2_grid_dim
        self.x1_amplitude = x1_amplitude
        self.x2_amplitude = x2_amplitude
        self.diff_v_x1 = diff_v_x1
        self.diff_v_x2 = diff_v_x2
        self.diff_k_p1 = diff_k_p1
        self.diff_k_p2 = diff_k_p2
        self.dt = dt
        self.t = t
        self.abs_boundary = abs_boundary

        assert 2 ** int(np.log2(self.x1_grid_dim)) == self.x1_grid_dim and \
               2 ** int(np.log2(self.x2_grid_dim)) == self.x2_grid_dim, \
                "The grid size (x1_grid_dim and x2_grid_dim) must be a power of 2"

        # get coordinate step sizes
        self.dx1 = 2. * self.x1_amplitude / self.x1_grid_dim
        self.dx2 = 2. * self.x2_amplitude / self.x2_grid_dim

        # generate coordinate ranges
        k1 = np.arange(self.x1_grid_dim)[:, np.newaxis]
        k2 =  np.arange(self.x2_grid_dim)[np.newaxis, :]
        # see http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
        # for explanation of np.newaxis and other array indexing operations
        # also https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
        # for understanding the broadcasting in array operations

        x1 = self.x1 = (k1 - self.x1_grid_dim / 2) * self.dx1
        x2 = self.x2 = (k2 - self.x2_grid_dim / 2) * self.dx2

        # generate momentum ranges
        p1 = self.p1 = (k1 - self.x1_grid_dim / 2) * (np.pi / self.x1_amplitude)
        p2 = self.p2 = (k2 - self.x2_grid_dim / 2) * (np.pi / self.x2_amplitude)

        # allocate the array for wavefunction
        self.wavefunction = np.zeros((self.x1_grid_dim, self.x2_grid_dim), dtype=np.complex)

        ###################################################################################################
        #
        # Codes for efficient evaluation
        #
        ####################################################################################################

        if isinstance(abs_boundary, CPUDispatcher):
            @njit
            def expV(wavefunction, t):
                """
                function to efficiently evaluate
                    wavefunction *= (-1) ** (k1 + k2) * exp(-0.5j * dt * v)
                """
                wavefunction *= (-1) ** (k1 + k2) * abs_boundary(x1, x2) * np.exp(-0.5j * dt * v(x1, x2, t))

        elif isinstance(abs_boundary, (float, int)):
            @njit
            def expV(wavefunction, t):
                """
                function to efficiently evaluate
                    wavefunction *= (-1) ** k * exp(-0.5j * dt * v)
                """
                wavefunction *= (-1) ** (k1 + k2) * abs_boundary * np.exp(-0.5j * dt * v(x1, x2, t))

        else:
            raise ValueError("abs_boundary must be either a numba function or a numerical constant")

        self.expV = expV

        @njit
        def expK(wavefunction, t):
            """
            function to efficiently evaluate
                wavefunction *= exp(-1j * dt * k)
            """
            wavefunction *= np.exp(-1j * dt * k(p1, p2, t))

        self.expK = expK

        # Check whether the necessary terms are specified to calculate the first-order Ehrenfest theorems
        if diff_k_p1 and diff_k_p2 and diff_v_x1 and diff_v_x2:

            # Get codes for efficiently calculating the Ehrenfest relations
            @njit
            def get_p1_average_rhs(density, t):
                return np.sum(density * diff_v_x1(x1, x2, t))

            self.get_p1_average_rhs = get_p1_average_rhs

            @njit
            def get_p2_average_rhs(density, t):
                return np.sum(density * diff_v_x2(x1, x2, t))

            self.get_p2_average_rhs = get_p2_average_rhs

            @njit
            def get_v_average(density, t):
                return np.sum(v(x1, x2, t) * density)

            self.get_v_average = get_v_average

            @njit
            def get_x1_average(density):
                return np.sum(x1 * density)

            self.get_x1_average = get_x1_average

            @njit
            def get_x2_average(density):
                return np.sum(x2 * density)

            self.get_x2_average = get_x2_average

            @njit
            def get_x1_average_rhs(density, t):
                return np.sum(diff_k_p1(p1, p2, t) * density)

            self.get_x1_average_rhs = get_x1_average_rhs

            @njit
            def get_x2_average_rhs(density, t):
                return np.sum(diff_k_p2(p1, p2, t) * density)

            self.get_x2_average_rhs = get_x2_average_rhs

            @njit
            def get_k_average(density, t):
                return np.sum(k(p1, p2, t) * density)

            self.get_k_average = get_k_average

            @njit
            def get_p1_average(density):
                return np.sum(p1 * density)

            self.get_p1_average = get_p1_average

            @njit
            def get_p2_average(density):
                return np.sum(p2 * density)

            self.get_p2_average = get_p2_average

            # Lists where the expectation values of x's and p's
            self.x1_average = []
            self.x2_average = []

            self.p1_average = []
            self.p2_average = []

            # Lists where the right hand sides of the Ehrenfest theorems for x's and p's
            self.x1_average_rhs = []
            self.x2_average_rhs = []

            self.p1_average_rhs = []
            self.p2_average_rhs = []

            # List where the expectation value of the Hamiltonian will be calculated
            self.hamiltonian_average = []

            # Allocate array for storing coordinate or momentum density of the wavefunction
            self.density = np.zeros(self.wavefunction.shape, dtype=np.float)

            # sequence of alternating signs for getting the wavefunction in the momentum representation
            self.minus = (-1) ** (k1 + k2)

            # Flag requesting tha the Ehrenfest theorem calculations
            self.is_ehrenfest = True
        else:
            # Since diff_v and diff_k are not specified, we are not going to evaluate the Ehrenfest relations
            self.is_ehrenfest = False

    def propagate(self, time_steps=1):
        """
        Time propagate the wave function saved in self.wavefunction
        :param time_steps: number of self.dt time increments to make
        :return: self.wavefunction
        """

        # pre-compute the sqrt of the volume element
        sqrt_dx1dx2 = np.sqrt(self.dx1 * self.dx2)

        for _ in range(time_steps):
            # make a half step in time
            self.t += 0.5 * self.dt

            # efficiently calculate
            #   wavefunction *= expV
            self.expV(self.wavefunction, self.t)

            # going to the momentum representation
            self.wavefunction = fftpack.fft2(self.wavefunction, overwrite_x=True)

            # efficiently evaluate
            #   wavefunction *= exp(-1j * dt * k)
            self.expK(self.wavefunction, self.t)

            # going back to the coordinate representation
            self.wavefunction = fftpack.ifft2(self.wavefunction, overwrite_x=True)

            # efficiently calculate
            #   wavefunction *= expV
            self.expV(self.wavefunction, self.t)

            # normalize
            # the following line is equivalent to
            # self.wavefunction /= np.sqrt(np.sum(np.abs(self.wavefunction)**2) * self.dX1 * self.dX2)
            # or
            self.wavefunction /= linalg.norm(self.wavefunction.reshape(-1)) * sqrt_dx1dx2

            # make a half step in time
            self.t += 0.5 * self.dt

            # calculate the Ehrenfest theorems
            self.get_ehrenfest()

        return self.wavefunction

    def get_ehrenfest(self):
        """
        Calculate observables entering the Ehrenfest theorems at time (t)
        """
        if self.is_ehrenfest:

            # evaluate the coordinate density
            np.abs(self.wavefunction, out=self.density)
            self.density *= self.density
            # normalize
            self.density /= self.density.sum()

            # save the current values of <x1> and <x2>
            self.x1_average.append(self.get_x1_average(self.density))
            self.x2_average.append(self.get_x2_average(self.density))

            self.p1_average_rhs.append(-self.get_p1_average_rhs(self.density, self.t))
            self.p2_average_rhs.append(-self.get_p2_average_rhs(self.density, self.t))

            # save the potential energy
            self.hamiltonian_average.append(self.get_v_average(self.density, self.t))

            # calculate density in the momentum representation
            wavefunction_p = fftpack.fft2(self.minus * self.wavefunction, overwrite_x=True)

            # get the density in the momentum space
            np.abs(wavefunction_p, out=self.density)
            self.density *= self.density
            # normalize
            self.density /= self.density.sum()

            # save the current values of <p1> and <p2>
            self.p1_average.append(self.get_p1_average(self.density))
            self.p2_average.append(self.get_p2_average(self.density))

            self.x1_average_rhs.append(self.get_x1_average_rhs(self.density, self.t))
            self.x2_average_rhs.append(self.get_x2_average_rhs(self.density, self.t))

            # add the kinetic energy to get the hamiltonian
            self.hamiltonian_average[-1] += self.get_k_average(self.density, self.t)

    def set_wavefunction(self, wavefunc):
        """
        Set the initial wave function
        :param wavefunc: 2D numpy array or a function specifying the wave function
        :return: self
        """
        if isinstance(wavefunc, (CPUDispatcher, FunctionType)):
            self.wavefunction[:] = wavefunc(self.x1, self.x2)

        elif isinstance(wavefunc, np.ndarray):
            # wavefunction is supplied as an array

            # perform the consistency checks
            assert wavefunc.shape == self.wavefunction.shape,\
                "The grid size does not match with the wave function"

            # make sure the wavefunction is stored as a complex array
            np.copyto(self.wavefunction, wavefunc.astype(np.complex))

        else:
            raise ValueError("wavefunc must be either string or numpy.array")

        # normalize
        self.wavefunction /= linalg.norm(self.wavefunction.reshape(-1)) * np.sqrt(self.dx1 * self.dx2)

        return self


#random utils

import logging
import pathlib
import json

logging.basicConfig()
logger = logging.getLogger()

#handle paths
base_dir = pathlib.Path(__file__).resolve().parent.parent
extensions_dir = base_dir / "extensions"
config_dir = base_dir / "config"
default_config_path = config_dir / "default.json"
config_path = config_dir / "config.json"

#read config
if not config_path.exists():
  config_path.write_text(default_config_path.read_text())
  raise FileNotFoundError("config/config.json needs to be modified")
config = json.loads(config_path.read_text())

def check_utf8(file_path):
  try:
    with open(file_path, "r") as f:
      f.read()
    return True
  except UnicodeDecodeError:
    return False

def get_template(template_name):
  template_path = base_dir / "templates" / template_name
  return template_path.read_text()


from aiogram import Bot, Dispatcher
import asyncio
import logging

from app.handlers import bot_messages, user_commands
from app.token import TOKEN

async def main():
    bot = Bot(TOKEN, parse_mode='HTML')
    dp = Dispatcher()

    dp.include_routers(
        user_commands.router,
        bot_messages.router,
    )

    logging.basicConfig(level=logging.INFO)
    await bot.delete_webhook(drop_pending_updates=True)
    await dp.start_polling(bot)

if __name__ == '__main__':
    asyncio.run(main())


# -*- coding: utf-8 -*-
"""
Created on Sat Des 3 19:56:38 2018
@author: LHF
"""
from __future__ import absolute_import
from __future__ import division
import argparse

parser = argparse.ArgumentParser()
#############################################################################################################
#                                          Global Constants                                                 #
#############################################################################################################
DEFAULT_TRAIN_SEG_DIR = 'datasets/CT/DM/train_pps/'
DEFAULT_TRAIN_LAB_DIR = 'datasets/CT/GT/train/'
DEFAULT_VALID_SEG_DIR = 'datasets/CT/DM/valid_pps/'
DEFAULT_VALID_LAB_DIR = 'datasets/CT/GT/valid/'
DEFAULT_TEST_SEG_DIR = 'datasets/CT/DM/test_pps/'
DEFAULT_TEST_LAB_DIR = 'datasets/CT/GT/test/'

img_H, img_W = 256, 256

lab_H, lab_W = 256, 256

InpRow = 256

InpCol = 256

CHANNELS = 1

WEIGHT_DECAY = 0.0

TOTAL_LOSS_COLLECTION = 'my_total_losses'

##########################################################################################################
#                                               Data                                                     #
##########################################################################################################
parser.add_argument('--train_seg_dir', type = str, default = DEFAULT_TRAIN_SEG_DIR,
                    help = 'Path to LR images for training.')
parser.add_argument('--train_lab_dir', type = str, default = DEFAULT_TRAIN_LAB_DIR,
                    help = 'Path to HR images for training.')
parser.add_argument('--valid_seg_dir', type = str, default = DEFAULT_VALID_SEG_DIR,
                    help = 'Path to LR images for validation.')
parser.add_argument('--valid_lab_dir', type = str, default = DEFAULT_VALID_LAB_DIR,
                    help = 'Path to HR images for validation.')
parser.add_argument('--test_seg_dir', type = str, default = DEFAULT_TEST_SEG_DIR,
                    help = 'Path to LR images for testing.')
parser.add_argument('--test_lab_dir', type = str, default = DEFAULT_TEST_LAB_DIR,
                    help = 'Path to HR images for testing.')

##########################################################################################################
#                                               Train                                                    #
##########################################################################################################
parser.add_argument('--batch_size', type = int, default = 16,
                    help = 'Number of examples to process in a batch.')
parser.add_argument('--use_fp16', type = bool, default = False,
                    help = 'Train model using float16 data type.')
parser.add_argument('--xavier_init', type = bool, default = True,
                    help = 'whether to initialize params with Xavier method.')
parser.add_argument('--max_steps', type = int, default = 201000,
                    help = 'Maximum of the number of steps to train.')
parser.add_argument('--learning_rate', type = float, default = 0.00005,
                    help = 'The learning rate for optimizer.')
parser.add_argument('--train_log_freq', type = int, default = 100,
                    help = 'How often to log results to the console when training.')
parser.add_argument('--decay_steps', type = int, default = 100000,
                    help = 'How many steps to decay learning rate.')
parser.add_argument('--decay_rate', type = float, default = 0.9,
                    help = 'Decay rate for learning rate.')
parser.add_argument('--train_log_dir', type = str, default = 'train_logs/',
                    help = 'Directory where to write event logs and checkpoints.')
parser.add_argument('--use_mridata_train', type = bool, default = True,
                    help = 'Train model using mri data.')
parser.add_argument('--normalization', type = bool, default = True,
                    help = 'Whether to normalize training examples.')
parser.add_argument('--sub_mean', type = bool, default = True ,
                    help = 'Whether to subtract the average RGB values of the whole dataset.')
parser.add_argument('--train_from_exist', type = bool, default = False,
                    help = 'Whether to train model from pretrianed ones.')
parser.add_argument('--exist_model_dir', type = str, default = 'train_logs/',
                    help = 'Directory where to load pretrianed models.')

##########################################################################################################
#                                                 Valid                                                  #
##########################################################################################################
parser.add_argument('--valid_log_dir', type = str, default = 'records/',
                    help = 'Directory where to write event logs and checkpoints.')
parser.add_argument('--valid_image_save_dir', type = str, default = 'valid_saved_images/',
                    help = 'Directory where to save predicted HR patches.')
parser.add_argument('--valid_save_image', type = bool, default = True,
                    help = 'Whether to save the result images when do evalution.')

##########################################################################################################
#                                                 Test                                                  #
##########################################################################################################
parser.add_argument('--test_log_dir', type = str, default = 'records/',
                    help = 'Directory where to write event logs and checkpoints.')
parser.add_argument('--test_image_save_dir', type = str, default = 'test_saved_images/',
                    help = 'Directory where to save predicted HR patches.')
parser.add_argument('--test_image_save_dir_label', type = str, default = 'test_saved_images/label_dir/',
                    help = 'Directory where to save predicted HR patches.')
parser.add_argument('--test_image_save_dir_pred', type = str, default = 'test_saved_images/pred_dir/',
                    help = 'Directory where to save predicted HR patches.')
parser.add_argument('--test_model_dir', type = str, default = 'train_logs/',
                    help = 'Directory where the model that needs evaluation is saved.')
parser.add_argument('--test_save_image', type = bool, default = True,
                    help = 'Whether to save the result images when do testing.')

#%%
params = parser.parse_args()


import csv
import sqlite3

DB_NAME = "birds_sqlite.sqlite"

# COLUMN NAMES
BIRD = "bird"
YEAR = "year"
MONTH = "month"
DAY = "day"
HOUR = "hour"
MINUTE = "minute"
SECOND = "second"
CLASS = "class"

def create_db():
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()
    c.execute("SELECT * FROM sqlite_master WHERE type='table' AND name='TABLE_BIRDS'")
    if c.arraysize < 2:
        c.execute("CREATE TABLE TABLE_BIRDS (BIRD TEXT, YEAR INTEGER, MONTH INTEGER, DAY INTEGER, HOUR INTEGER, "
                  "MINUTE INTEGER, SECOND INTEGER, CLASS INTEGER)")
    conn.commit()
    conn.close()

def drop_db():
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()
    c.execute("DROP TABLE TABLE_BIRDS")
    conn.commit()
    conn.close()

def csv_to_db(csvfilename, bird):
    with open(csvfilename, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        reader.next()  # skip header
        conn = sqlite3.connect(DB_NAME)
        c = conn.cursor()
        for row in reader:
            dt = [row[0], row[1], row[2], row[3], row[4], row[5]]
            c.execute("INSERT INTO TABLE_BIRDS VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (bird, dt[0], dt[1], dt[2], dt[3],
              dt[4], dt[5], row[6]))
        conn.commit()
        conn.close()

def get_one_day(daynr):
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()
    c.execute("SELECT class FROM TABLE_BIRDS WHERE DAY=?", (daynr, ))
    day = c.fetchall()
    conn.close()
    return day

def get_one_hour(bird, daynr, hour):
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()
    c.execute("SELECT CLASS FROM TABLE_BIRDS WHERE DAY=? AND HOUR=? AND BIRD=?",
              (daynr, hour, bird))
    hour = c.fetchall()
    conn.close()
    return hour

def get_all_from_bird(bird):
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()
    c.execute("SELECT * FROM TABLE_BIRDS")
    bird = c.fetchall()
    conn.close()
    return bird


import numpy as np

def convert_max_into_delta(observed_level, predicted_max):
    """ The function converts the predicted level values (stage_max) to the target delta_stage_max variable

    :param observed_level: water level value that was known at the beginning of the forecast
    :param predicted_max: predicted values of levels for 7 days ahead
    :return delta_levels: level difference
    """
    shifted = predicted_max[:-1]
    new_arr = np.hstack([np.array(observed_level), shifted])

    delta_stage_max_predicted = predicted_max - new_arr
    return delta_stage_max_predicted


from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.initializers import *
from keras.optimizers import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import pylab

num_of_neurons = range(10,100,10)
d = np.zeros(len(num_of_neurons))
index = 0

# load data
x_data = np.loadtxt('x_data_set')
y_data = np.loadtxt('y_data_set')

split_ratio = 0.7
number_of_samples = np.shape(x_data)[0]

# train data
x_data_train = x_data[0: int(number_of_samples*split_ratio), ]
y_data_train = y_data[0: int(number_of_samples*split_ratio), ]

# test data
x_data_test = x_data[int(number_of_samples*split_ratio):, ]
y_data_test = y_data[int(number_of_samples*split_ratio):, ]

number_of_test_data = np.shape(x_data_test)[0]

initializers = [random_normal(), random_uniform(), truncated_normal(), he_normal(), he_uniform(), lecun_uniform()]
initializers_name = ['random_normal', 'random_uniform()', 'truncated_normal', 'he_normal()', 'he_uniform()', 'lecun_uniform()']

seeds = range(1, 90001, 15000)
learning_rates = np.linspace(0.001, 0.005, 5)

for j, seed in enumerate(seeds):

    for learning_rate in learning_rates:

        for i, initializer in enumerate(initializers):
            np.random.seed(seed)
            model = Sequential()
            model.add(Dense(units=200, input_shape=(5,), kernel_initializer=initializer,
                            bias_initializer=initializer, activation='relu'))
            model.add(Dense(units=200, kernel_initializer=initializer, bias_initializer=initializer,
                            activation='sigmoid'))
            model.add(Dense(units=9, activation='relu', kernel_initializer=initializer,
                            bias_initializer=initializer))
            model.compile(loss='mean_squared_error', optimizer=adam(lr=learning_rate))
            history = model.fit(x_data_train, y_data_train, batch_size=512, validation_split=0.1,epochs=200)
            train_loss = history.history['loss']
            # plot the loss
            init_label = 'initializer is  ' + initializers_name[i]
            plt.plot(train_loss, label=init_label)
        plt.xlabel('epoch')
        plt.ylabel('loss')
        plt.legend()
        title = 'seed is '+ str(seed) + ' learning rate is ' + str(learning_rate)
        plt.title(title)
        file_name = 'seed = ' + str(seed) + 'learning rate = ' + str(learning_rate)
        plt.savefig('/home/chen/MA_python/loss-vs-init/' + str(file_name) + '.png')
        plt.close()

plt.show()


import json
import unittest
from pathlib import Path
import tests.lib_tests
from database import Database
from file_system import File_system

class TestDatabaseClass(unittest.TestCase):
	# def test_something(self):
	# 	self.assertEqual(True, False)

	# database_connection = Database("vehicle_positions_test_database")

	# def testTest(self):
	# 	self.assertEqual(1,2)

	# def testAExecutionOrder(self):
	# 	self.assertEqual(unittest.defaultTestLoader.getTestCaseNames(FillDatabase), ['testAExecutionOrder', 'testDatabaseConnection', 'testSuccessExecute_transaction_commit_rollback'])

	def testDatabaseConnection(self):
		database_connection = Database("vehicle_positions_test_database")

		database_connection.cursor.execute('SELECT VERSION()')

	def testSuccessExecute_transaction_commit_rollback(self):
		tests.lib_tests.drop_all_tables()
		database_connection = Database("vehicle_positions_test_database")

		database_connection.execute_transaction_commit_rollback("INSERT INTO headsigns (headsign) VALUES (%s)", ('test',))
		database_connection.cursor.execute("SELECT headsign FROM headsigns WHERE headsign='test'")
		result = database_connection.cursor.fetchall()
		self.assertEqual(len(result), 1)

	def testFailExecute_transaction_commit_rollback(self):
		tests.lib_tests.drop_all_tables()
		database_connection = Database("vehicle_positions_test_database")

		database_connection.execute_transaction_commit_rollback("INSERT INTO headsigns (headsign) VALUES (%s)", ('test',))

		try:
			self.assertRaises(IOError, database_connection.execute_transaction_commit_rollback("INSERT INTO headsigns (headsign) VALUES (%s)", ('test',)))
		except Exception:
			pass

	def testFetchall(self):
		tests.lib_tests.drop_all_tables()
		database_connection = Database("vehicle_positions_test_database")

		database_connection.cursor.execute("INSERT INTO headsigns (headsign) VALUES (%s)", ('test1',))
		database_connection.cursor.execute("INSERT INTO headsigns (headsign) VALUES (%s)", ('test2',))

		result = database_connection.execute_fetchall("SELECT headsign FROM headsigns")

		self.assertEqual(result[0][0], 'test1')
		self.assertEqual(result[1][0], 'test2')

	def testExecute(self):
		tests.lib_tests.drop_all_tables()
		database_connection = Database("vehicle_positions_test_database")

		database_connection.execute("INSERT INTO headsigns (headsign) VALUES (%s)", ('test1',))
		database_connection.execute("INSERT INTO headsigns (headsign) VALUES (%s)", ('test2',))

		result = database_connection.execute_fetchall("SELECT headsign FROM headsigns")

		self.assertEqual(result[0][0], 'test1')
		self.assertEqual(result[1][0], 'test2')

	def testExecute_many(self):
		tests.lib_tests.drop_all_tables()
		database_connection = Database("vehicle_positions_test_database")

		database_connection.execute_many("INSERT INTO headsigns (headsign) VALUES (%s)", [('test1',), ('test2',)])

		result = database_connection.execute_fetchall("SELECT headsign FROM headsigns")

		self.assertEqual(result[0][0], 'test1')
		self.assertEqual(result[1][0], 'test2')

	def testExecute_procedure_fetchall(self):
		tests.lib_tests.drop_all_tables()
		args = tests.lib_tests.get_args()
		database_connection = Database("vehicle_positions_test_database")

		from stops import Stops
		from all_vehicle_positions import All_vehicle_positions

		vehicle_positions = All_vehicle_positions()
		vehicle_positions.json_file = json.loads(File_system.get_tar_file_content("../input_data/2020-02-20T13.50.23.tar.gz"))
		vehicle_positions.construct_all_trips(database_connection)
		for vehicle in vehicle_positions.iterate_vehicles():
			if vehicle.trip_id == "421_225_191114":
				vehicle.json_trip = json.loads(File_system.get_file_content(Path("../input_data/421_225_191114.json")))
				vehicle._fill_attributes_from_trip_file()
				vehicle.id_trip = database_connection.execute_fetchall(
					'SELECT insert_new_trip_to_trips_and_coordinates_and_return_id(%s, %s, %s, %s, %s, %s, %s, %s, %s)',
					vehicle.get_tuple_new_trip(args.static_demo))[0][0]
				Stops.insert_ride_by_trip(database_connection, vehicle)
				database_connection.execute('COMMIT;')
				break

		result = database_connection.execute_procedure_fetchall("get_all_pairs_of_stops", (0, 0, 0))

		# File_system.pickle_object(result, "../output_data/testExecute_procedure_fetchall.obj")

		self.assertEqual(result, File_system.pickle_load_object("../output_data/testExecute_procedure_fetchall.obj"))

if __name__ == '__main__':
	unittest.main()