import tkinter as tk
import tkinter.filedialog as tf
import tkinter.messagebox as tm
import LabelText
import ScrolledTab
import FileTree
import threading
import hashlib

class editorgui:
    text = None #文本组件
    tab = None #标签组件
    content = [] #文本内容
    path = [] #文件路径
    change = [] #文本内容是否改变
    sig = [] #签名
    mark = [] #光标位置
    workspace = '' #文件夹路径
    language = None #语言

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('MyEditor')
        self.root.geometry('800x700')
        self.root.protocol('WM_DELETE_WINDOW', self.__editQuit)

        self.__initControl()

        self.__initMenu()

        self.tab.tabAdd()

    def __initMenu(self):
        rootmenu = tk.Menu(self.root)

        filemenu = tk.Menu(rootmenu, tearoff = False)
        filemenu.add_command(label = 'New', command = self.tab.tabAdd)
        filemenu.add_separator()
        filemenu.add_command(label = 'Open', command = self.__openFile)
        filemenu.add_separator()
        filemenu.add_command(label = 'Save', command = self.__saveFile)
        filemenu.add_command(label = 'Save as', command = self.__saveAsFile)

        editmenu = tk.Menu(rootmenu, tearoff = False)
        editmenu.add_command(label = 'Find', command = self.__findWindow)
        editmenu.add_separator()
        editmenu.add_command(label = 'Replace', command = self.__replaceWindow)

        settingmenu = tk.Menu(rootmenu, tearoff = False)
        languagemenu = tk.Menu(settingmenu, tearoff = False)
        self.language = tk.StringVar()
        self.language.set("C")
        languagemenu.add_radiobutton(label = 'C', variable = self.language, value = "C", command = lambda:self.__changeLanguage("C"))
        languagemenu.add_radiobutton(label = 'C++', variable = self.language, command = lambda:self.__changeLanguage("C++"))
        languagemenu.add_radiobutton(label = 'Java', variable = self.language, command = lambda:self.__changeLanguage("Java"))
        languagemenu.add_radiobutton(label = 'Python', variable = self.language, command = lambda:self.__changeLanguage("Python"))
        languagemenu.add_radiobutton(label = 'Other', variable = self.language, command = lambda:self.__changeLanguage("Other"))
        settingmenu.add_cascade(label = 'Language', menu = languagemenu)

        rightmenu = tk.Menu(self.root, tearoff = False)
        rightmenu.add_command(label = 'Undo', command = self.text.textwin.edit_undo)
        rightmenu.add_separator()
        rightmenu.add_command(label = 'Redo', command = self.text.textwin.edit_redo)

        rootmenu.add_cascade(label = 'File', menu = filemenu)
        rootmenu.add_cascade(label = 'Edit', menu = editmenu)
        rootmenu.add_cascade(label = 'Setting', menu = settingmenu)

        self.text.textwin.bind('<Button-3>', lambda event:rightmenu.post(event.x_root, event.y_root))

        self.root.config(menu = rootmenu)

    #初始化组件
    def __initControl(self):
        #窗口
        win = tk.PanedWindow(self.root, opaqueresize = False, bd = 0, sashwidth = 1.5, bg = 'black')
        win.pack(fill = tk.BOTH, expand = True)

        #左框架
        lframe = tk.Frame(win, bg = "#D3D3D3")
        win.add(lframe)

        #文件列表和按钮
        self.ftree = FileTree.FileTree(lframe)
        folderbutton = tk.Button(lframe, text = 'open folder', font = ("Verdana", 10), padx = 10, pady = 5, command = lambda:self.threadFunc(self.__openFolder))
        folderbutton.pack()
        self.ftree.pack(fill = tk.BOTH, expand = True)
        self.ftree.bindTree(self.__openFile)

        #右框架
        rframe = tk.Frame(win)
        win.add(rframe)

        #标签栏
        self.tab = ScrolledTab.ScrolledTab(rframe)
        self.tab.pack(fill = tk.X)
        self.tab.bindFunc(self.__newFile, self.__changeFile, self.__closeFile, self.__editQuit)

        #文本框
        self.text = LabelText.LabelText(rframe)
        self.text.pack(fill = tk.BOTH, expand = True)
        self.text.bindCheck(self.__checkText)

    #打开文件
    def __openFile(self, path = ""):
        if path == '':
            path = tf.askopenfilename()
        try:
            with open(path, 'rb') as f:
                data = f.read().decode('utf-8')
                self.tab.tabAdd()
                self.path[self.tab.current] = path
                self.content[self.tab.current] = data
                self.sig[self.tab.current] = hashlib.md5(data.encode('utf-8')).digest()
                #显示新文件
                self.text.setText(self.content[self.tab.current])
                self.__setTitle()
        except:
            tm.showwarning('Warning', 'File open error')
            print('file open error')

    #保存文件
    def __saveFile(self):
        if self.path[self.tab.current] == "":
            path = tf.asksaveasfilename(filetypes = [('TXT', '.txt'), ('Python', '.py')])
            if path != '':
                self.path[self.tab.current] = path
        try:
            with open(self.path[self.tab.current], 'wb') as f:
                data = self.text.getText().encode('utf-8')
                f.write(data)
                #更新
                self.content[self.tab.current] = data
                self.sig[self.tab.current] = hashlib.md5(data).digest()
                self.change[self.tab.current] = False
                self.__setTitle()
        except:
            tm.showwarning('Warning', 'File save error')
            print('file save error')

    #另存文件
    def __saveAsFile(self):
        path = tf.asksaveasfilename(filetypes = [('TXT', '.txt'), ('Python', '.py')])
        if path != "":
            self.path[self.tab.current] = path
            self.__saveFile()

    #新文件
    def __newFile(self):
        self.content.append('')
        self.path.append('')
        self.change.append(False)
        self.sig.append(hashlib.md5(''.encode('utf-8')).digest())
        self.mark.append('1.0')

    #切换文件
    def __changeFile(self, old):
        if old != -1:
            self.content[old] = self.text.getText()
            self.mark[old] = self.text.getInsert()
        self.text.setText(self.content[self.tab.current])
        self.text.setInsert(self.mark[self.tab.current])
        self.__setTitle()

    #关闭文件
    def __closeFile(self):
        if self.change[self.tab.current]:
            choice = tm.askyesnocancel('提示', '要保存吗？')
            if choice == True:
                self.__saveFile()
            elif choice == None:
                return False
        del self.content[self.tab.current]
        del self.path[self.tab.current]
        del self.sig[self.tab.current]
        del self.change[self.tab.current]
        del self.mark[self.tab.current]
        return True

    #更换显示文件夹
    def __openFolder(self):
        path = tf.askdirectory()
        if path != '':
            self.workspace = path
            self.ftree.setTree(path)

    #查找文本
    def __findWindow(self):
        #新窗口
        findwin = tk.Toplevel(self.root)
        findwin.title('Find')
        findwin.attributes('-toolwindow', True)
        findwin.attributes('-topmost', True)
        findwin.resizable(0, 0)
        findwin.focus()
        findwin.bind('<Destroy>', lambda event:self.text.textwin.tag_remove('found', '1.0', 'end'))
        #输入框
        targetText = tk.Entry(findwin)
        targetText.pack()
        #查找按钮
        findbutton = tk.Button(findwin, text = 'find', command = lambda:self.text.findText(targetText.get(), 'found'))
        findbutton.pack()

    #替换文本
    def __replaceWindow(self):
        #新窗口
        replacewin = tk.Toplevel()
        replacewin.title('Replace')
        replacewin.attributes('-toolwindow', True)
        replacewin.attributes('-topmost', True)
        replacewin.resizable(0, 0)
        replacewin.focus()
        replacewin.bind('<Destroy>', lambda event:self.text.textwin.tag_remove('found', '1.0', 'end'))
        #查找内容
        targetText = tk.Entry(replacewin)
        targetText.pack()
        tk.Label(replacewin, text = '↓').pack()
        #替换内容
        replaceText = tk.Entry(replacewin)
        replaceText.pack()
        #查找按钮
        replacebutton = tk.Button(replacewin, text = 'replace', command = lambda:self.text.replaceText(targetText.get(), replaceText.get()))
        replacebutton.pack()

    #选择语言
    def __changeLanguage(self, lan):
        self.language.set(lan)
        self.text.changeLan(lan)

    #设置标签和窗口标题
    def __setTitle(self):
        n = self.tab.current
        s = self.path[n]
        if s == "":
            s = 'Untitled'
        if self.change[n]:
            s = s + '*'
        self.root.title('MyEditor-'  + s)
        s = s.split('/')[-1]
        self.tab.tabs[n].config(text = s)

    #检查文本变动
    def __checkText(self):
        if self.change[self.tab.current] or self.tab.current == -1:
            return
        sig = hashlib.md5(self.text.getText().encode('utf-8')).digest()
        if sig != self.sig[self.tab.current]:
            self.change[self.tab.current] = True
            self.__setTitle()

    #退出
    def __editQuit(self):
        flag = True
        while len(self.tab.tabs) != 0:
            if not self.tab.tabRemove(self.tab.tabs[0]):
                flag = False
                break
        if flag:
            self.root.quit()

    #多线程
    def threadFunc(self, func, *args):
        t = threading.Thread(target = func, args = args)
        t.setDaemon(True)
        t.start()

if __name__ == '__main__':
    editorgui().root.mainloop()


# module plotter
'''
Plots the residual vector vs. the number of iterations.
'''

import matplotlib.pyplot as plt
import scienceplots # pylint: disable=unused-import
import pandas as pd

res_data = pd.read_csv('../data/res.csv')
frc_data = pd.read_csv('../data/frc.csv')

plt.style.use(['science', 'grid'])
plt.figure(figsize=(1920/96, 1080/96), dpi=96)

plt.plot(res_data['iter'], res_data['rho'],   label='$\\rho$')
plt.plot(res_data['iter'], res_data['rho_u'], label='$\\rho{}u$')
plt.plot(res_data['iter'], res_data['rho_v'], label='$\\rho{}v$')
plt.plot(res_data['iter'], res_data['rho_e'], label='$\\rho{}E$')

plt.plot(res_data['iter'], res_data['err_rho'],   label='$\\rho$ Error',    linestyle='dashdot')
plt.plot(res_data['iter'], res_data['err_rho_u'], label='$\\rho{}u$ Error', linestyle='dashdot')
plt.plot(res_data['iter'], res_data['err_rho_v'], label='$\\rho{}v$ Error', linestyle='dashdot')
plt.plot(res_data['iter'], res_data['err_rho_e'], label='$\\rho{}E$ Error', linestyle='dashdot')

plt.yscale('log')
plt.title('$R_{ij} - D_{ij}$ \\& Respective Error vs. Iteration Number')
plt.xlabel('Iteration Number')
plt.ylabel('Residual')
plt.legend()

# plt.savefig('../img/res.webp', dpi=96 * 2)
plt.show()

plt.plot(frc_data['dist'], frc_data['fp_x'], label='$F_{px}$')
plt.plot(frc_data['dist'], frc_data['fp_y'], label='$F_{py}$')
plt.title('Pressure Force on the Bottom Wall')
plt.xlabel('$x$-distance')
plt.ylabel('Pressure Force')
plt.legend()

# plt.savefig('../img/frc.webp', dpi=96 * 2)
plt.show()


import numpy as np

import Config

layers = len(Config.CFG['feature_maps'])
minRatio = 0.05
maxRatio = 0.9

step = (maxRatio - minRatio) / (layers - 1)
sizes = Config.INPUT_SIZE[0] * np.array([minRatio + i * step for i in range(layers+1)])
minSizes = sizes[:-1].astype(int).tolist()
maxSizes = sizes[1:].astype(int).tolist()
print('\'min_sizes\': {},\n\'max_sizes\': {},'.format(minSizes, maxSizes))


from tensorflow.core.config import flags
from tensorflow.core.framework import types_pb2
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import indexed_slices
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor as tensor_lib
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import handle_data_util
from tensorflow.python.ops import math_ops

def _DTypeFromTensor(tensor):
  """Extract either `tensor.dtype` or the unanimous sub-type of a variant."""
  dtype = tensor.dtype
  if dtype.base_dtype == dtypes.variant:
    # If we know statically that the data a variant points to is non-trainable
    # then the variant itself is non-trainable.
    if isinstance(tensor, ops.EagerTensor):
      handle_data = tensor._handle_data  # pylint: disable=protected-access
    else:
      handle_data = handle_data_util.get_resource_handle_data(tensor)
    if (handle_data is not None
        and handle_data.is_set
        and handle_data.shape_and_type):
      first_type = handle_data.shape_and_type[0].dtype
      # Some variants have statically unknown dtypes; we can't make inferences
      # about trainability, so we conservatively assume they're trainable
      # (which may waste memory passing zeros around, but will be correct).
      if (first_type != types_pb2.DT_INVALID
          and all(shape_and_type.dtype == first_type
                  for shape_and_type in handle_data.shape_and_type)):
        return first_type
  return dtype

def IsTrainable(tensor_or_dtype):
  """Determines whether a tensor or dtype supports infinitesimal changes."""
  if tensor_util.is_tf_type(tensor_or_dtype):
    dtype = _DTypeFromTensor(tensor_or_dtype)
  else:
    dtype = tensor_or_dtype
  dtype = dtypes.as_dtype(dtype)
  trainable_dtypes = [dtypes.float16, dtypes.float32, dtypes.float64,
                      dtypes.complex64, dtypes.complex128, dtypes.resource,
                      dtypes.variant, dtypes.bfloat16]
  if flags.config().enable_quantized_dtypes_training.value():
    trainable_dtypes.extend([dtypes.qint8, dtypes.qint16, dtypes.qint32,
                             dtypes.quint8, dtypes.quint16])
  return dtype.base_dtype in trainable_dtypes

def FlattenNestedIndexedSlices(grad):
  assert isinstance(grad, indexed_slices.IndexedSlices)
  if isinstance(grad.values, tensor_lib.Tensor):
    return grad
  else:
    assert isinstance(grad.values, indexed_slices.IndexedSlices)
    g = FlattenNestedIndexedSlices(grad.values)
    return indexed_slices.IndexedSlices(
        g.values, array_ops.gather(grad.indices, g.indices), g.dense_shape)

def AggregateIndexedSlicesGradients(grads):
  """Aggregates gradients containing `IndexedSlices`s."""
  if len(grads) < 1:
    return None
  if len(grads) == 1:
    return grads[0]
  grads = [g for g in grads if g is not None]
  # If any gradient is a `Tensor`, sum them up and return a dense tensor
  # object.
  if any(isinstance(g, tensor_lib.Tensor) for g in grads):
    return math_ops.add_n(grads)

  # The following `_as_indexed_slices_list` casts ids of IndexedSlices into
  # int64. It is to make sure the inputs of `concat` all have same the data
  # type.
  grads = math_ops._as_indexed_slices_list(grads)  # pylint: disable=protected-access

  grads = [FlattenNestedIndexedSlices(x) for x in grads]
  # Form IndexedSlices out of the concatenated values and indices.
  concat_grad = indexed_slices.IndexedSlices(
      array_ops.concat([x.values for x in grads], axis=0),
      array_ops.concat([x.indices for x in grads], axis=0),
      grads[0].dense_shape)

  return concat_grad


import numpy
import mnist as mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils

# make seed for cycle results
numpy.random.seed(42)

# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# resize data
X_train = X_train.reshape(60000, 784)

# normalize data
X_train = X_train.astype('float32')
X_train /= 255

# transform tags to categories
y_train = np_utils.to_categorical(y_train, 10)

# 0 -> [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 2 -> [0, 0, 1, 0, 0, 0, 0, 0, 0, 0] # only the right element == 1

# create sequential model
model = Sequential()

# add network levels
model.add(Dense(800, input_dim=784, init="normal", activation="relu"))
model.add(Dense(10, init="normal", activation="softmax"))

# compile model
model.compile(loss="categorical_crossentropy", optimizer="SGD", metrics=["accuracy"])
print(model.summary())

#_________training_______________________
# batch_size - size of data portion
# nb_epoch - amount times of teaching (100 times)
model.fit(X_train, y_train, batch_size=200, nb_epoch=100, verbose=1)

#___________working_____________
# work on input data
predictions = model.predict(X_train)

# transform output data
# from categories to tags of classes (num from 0 to 9)
predictions = np_utils.categorical_probas_to_classes(predictions)

# now we can compare


#!/usr/bin/env python
# -*- coding: utf-8
# Compute distance between nerve rootlets and PMJ, nerve rootlets and discs along centerline
#
# For usage, type: python compute_neck_angle.py -h

# Authors: Sandrine Bédard

import argparse
import csv
import math
from webbrowser import get
import numpy as np
import nibabel as nib
import os

def get_parser():
    parser = argparse.ArgumentParser(
        description="Compute cobb angle beteen C2 and C6")
    parser.add_argument('-centerline', required=True, type=str,
                        help=".csv file of the centerline.")
    parser.add_argument('-disclabel', required=True, type=str,
                        help="Labels of the intervertebral discs.")
    parser.add_argument('-subject', required=True, type=str,
                        help="Subject ID")
    parser.add_argument('-ses', required=True, type=str,
                        help="ses")
    parser.add_argument('-o', required=False, type=str,
                        default='angle.csv',
                        help="Output csv filename.")

    return parser

def save_Nifti1(data, original_image, filename):
    empty_header = nib.Nifti1Header()
    image = nib.Nifti1Image(data, original_image.affine, empty_header)
    nib.save(image, filename)

def find_angle(v1, v2):

    unit_vector1 = v1 / np.linalg.norm(v1)
    unit_vector2 = v2 / np.linalg.norm(v2)

    dot_product = np.dot(unit_vector1, unit_vector2)

    angle = np.arccos(dot_product) #angle in radian

    angle_deg = angle*180/np.pi
    return round(angle_deg, 4)

def unit_vector(vector):
    """ Returns the unit vector of the vector.  """
    return vector / np.linalg.norm(vector)

def get_vector_from_point(p1, p2):
    v = p2 - p1
    return unit_vector(v)

def project_vector(u, n):
    # Norm of n vector
    n_norm = np.linalg.norm(n)
    proj_of_u_on_n = (np.dot(u, n)/n_norm**2)*n
    return u - proj_of_u_on_n

def main():
    parser = get_parser()
    args = parser.parse_args()

    # Create an array with centerline coordinates
    centerline = np.genfromtxt(args.centerline, delimiter=',')
    #centerline = nib.load(args.centerline).get_fdata()
    disc_label = nib.load(args.disclabel)
    idx_2 = np.where(disc_label.get_fdata() == 2.0)[-1]
    idx_6 = np.where(disc_label.get_fdata() == 6.0)[-1]
    idx_2_corr = np.abs(centerline[2] - idx_2).argmin()  # centerline doesn't necessarly start at the index 0 if the segmentation is incomplete
    idx_6_corr = np.abs(centerline[2] - idx_6).argmin()  # centerline doesn't necessarly start at the index 0 if the segmentation is incomplete
    len = 3

    p1 = centerline[:,idx_2_corr - len]
    p2 = centerline[:,idx_2_corr + len]
    p3 = centerline[:,idx_6_corr - len]
    p4 = centerline[:,idx_6_corr + len]

    v1 = get_vector_from_point(p1=p1, p2=p2)

    print('v1', v1)
    v2 = get_vector_from_point(p1=p3, p2=p4)
    print('v1', v2)
    # Project v1 and v2 on X (R-L)
    n = np.array([1,0,0])
    v1_proj = project_vector(v1, n)
    v2_proj = project_vector(v2, n)
    print('v1_proj', v1_proj, 'v2_proj', v2_proj)
    angle = find_angle(v1, v2)
    angle_proj = find_angle(v1_proj, v2_proj)

    print('angle', angle, 'degrees')
    print('angle proj', angle_proj, 'degrees')
    subject = args.subject
    ses = args.ses
    fname_out = args.o
    if not os.path.isfile(fname_out):
        with open(fname_out, 'w') as csvfile:
            header = ['Subject', 'ses', 'Angle (degrees)']
            writer = csv.DictWriter(csvfile, fieldnames=header)
            writer.writeheader()
    with open(fname_out, 'a') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',')
        line = [subject, ses, angle_proj]
        spamwriter.writerow(line)

if __name__ == '__main__':
    main()


# this problem is same as the max_sub_arr in same directory where it has been explain using kadan algorithm only differce is here the length of sub arr has been given.
# https://www.youtube.com/watch?v=__guhvzO540
# asymtotic function = len of arr * len of win (m*n)

def brute_force(arr, win=4):
	max_sum = 0
	for i in range(len(arr) - win + 1):
		win_sum = 0
		for j in range(win):
			win_sum += arr[i:][j]
			if win_sum > max_sum:
				max_sum = win_sum
	return max_sum

def sliding_window(nums: list, k: int) -> float:
	max_sum = 0
	_sum, start = 0, 0
	for end in range(len(nums)):
		_sum += nums[end]
		if end >= k - 1: # making equal to the sliding window size
			max_sum = max(_sum, max_sum)
			_sum -= nums[start]
			start += 1  # slide the window

	return max_sum

from itertools import combinations

def choose_best_dist(num, k):
	temp = 0
	for i in combinations(num, k):
		temp = max(sum(i), temp)
	return temp

def main():
	arr = [1, 24, 6, -3, 10, -2, 5, 12 ,13]
	#arr = [2,2,2,2,2,2,2,2]
	#arr = [4,3,2,1,6]
	print(sliding_window(arr, 4))
	#print(brute_force(arr))
	print(choose_best_dist(arr, 4))

if __name__ == '__main__':
	main()


import sys
import requests
import json

# init weights
weight_title = sys.argv[2]
weight_date = sys.argv[3]

# preparing query data sys.argv[1] contains the query text
query = {
    "query": {
        "multi_match": {
            "query": sys.argv[1],
            "fields": ["title" + "^" + weight_title, "data" + "^" + weight_date]
         }
     }
}

# our index name is cran-data which we had formed on importing cran field dataset
def search(query):
    url = 'http://localhost:9200/cran-data/_search'
    httpResp = requests.get(url, data=json.dumps(query), headers={"content-type":"application/json"})
    searchHits = json.loads(httpResp.text)["hits"]
    print(sys.argv[4])

    for idx, hit in enumerate(searchHits['hits']):
        print("%s" %(hit['_source']['id']))
search(query)


#! unzip -qq roads.zip
import os
import numpy as np
import cv2
from glob import glob
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

from tensorflow.keras.layers import Conv2D, Activation, BatchNormalization
from tensorflow.keras.layers import UpSampling2D, Input, Concatenate
from tensorflow.keras.models import Model , load_model
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.layers import Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, BatchNormalization, Activation, add
from tensorflow.keras.metrics import Recall, Precision
from tensorflow.keras import backend as K

#%tensorflow_version 2.x
import tensorflow as tf
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))

np.random.seed(42)
tf.random.set_seed(42)

## Hyperparameters

IMG_SIZE = 256
EPOCHS = 10
BATCH = 2
LR = 1e-5
PATH = ''

import glob
images_list = sorted(glob.glob("jimutmap_less/map/*"))
masks_list = sorted(glob.glob("jimutmap_less/roads/*"))
print(images_list[:10])
print(len(images_list))
print(len(masks_list))
print(masks_list[:10])

from glob import glob
def load_data(path, split=0.1):

  images_list = sorted(glob(os.path.join(path, "jimutmap_less/map/*")))
  masks_list = sorted(glob(os.path.join(path, "jimutmap_less/roads/*")))

  tot_size = len(images_list)
  val_size = int(split * tot_size)
  test_size = int(split * tot_size)

  x_train, x_val = train_test_split(images_list, test_size=val_size, random_state=42)
  y_train, y_val = train_test_split(masks_list, test_size=val_size, random_state=42)
  x_train, x_test = train_test_split(x_train, test_size=test_size, random_state=42)
  y_train, y_test = train_test_split(y_train, test_size=test_size, random_state=42)

  return (x_train, y_train), (x_val, y_val), (x_test, y_test)

def read_img(path):
    path = path.decode()
    tmp = cv2.imread(path, cv2.IMREAD_COLOR)
    tmp = cv2.resize(tmp, (IMG_SIZE, IMG_SIZE))
    tmp = tmp/255.0
    return tmp

def read_mask(path):
    path = path.decode()
    tmp = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    tmp = cv2.resize(tmp, (IMG_SIZE, IMG_SIZE))
    tmp = tmp/255.0
    tmp = np.expand_dims(tmp, axis=-1)
    return tmp

def tf_parse(a, b):
    def _parse(a, b):
        a = read_img(a)
        b = read_mask(b)
        return a, b

    a, b = tf.numpy_function(_parse, [a, b], [tf.float64, tf.float64])
    a.set_shape([IMG_SIZE, IMG_SIZE, 3])
    b.set_shape([IMG_SIZE, IMG_SIZE, 1])
    return a, b

def tf_dataset(a, b, batch=32):
    data = tf.data.Dataset.from_tensor_slices((a, b))
    data = data.map(tf_parse)
    data = data.batch(batch)
    data = data.repeat()
    return data

(x_train, y_train), (x_val, y_val), (x_test, y_test) = load_data("")

print("Training data: ", len(x_train))
print("Validation data: ", len(x_val))
print("Testing data: ", len(x_test))

def read_and_rgb(a):
    a = cv2.imread(a)
    a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
    return a

def conv2d_bn(x, filters, num_row, num_col, padding='same', strides=(1, 1), activation='relu', name=None):
    '''
    2D Convolutional layers

    Arguments:
        x {keras layer} -- input layer
        filters {int} -- number of filters
        num_row {int} -- number of rows in filters
        num_col {int} -- number of columns in filters

    Keyword Arguments:
        padding {str} -- mode of padding (default: {'same'})
        strides {tuple} -- stride of convolution operation (default: {(1, 1)})
        activation {str} -- activation function (default: {'relu'})
        name {str} -- name of the layer (default: {None})

    Returns:
        [keras layer] -- [output layer]
    '''

    x = Conv2D(filters, (num_row, num_col), strides=strides, padding=padding, use_bias=False)(x)
    x = BatchNormalization(axis=3, scale=False)(x)

    if(activation == None):
        return x

    x = Activation(activation, name=name)(x)

    return x

def trans_conv2d_bn(x, filters, num_row, num_col, padding='same', strides=(2, 2), name=None):
    '''
    2D Transposed Convolutional layers

    Arguments:
        x {keras layer} -- input layer
        filters {int} -- number of filters
        num_row {int} -- number of rows in filters
        num_col {int} -- number of columns in filters

    Keyword Arguments:
        padding {str} -- mode of padding (default: {'same'})
        strides {tuple} -- stride of convolution operation (default: {(2, 2)})
        name {str} -- name of the layer (default: {None})

    Returns:
        [keras layer] -- [output layer]
    '''

    x = Conv2DTranspose(filters, (num_row, num_col), strides=strides, padding=padding)(x)
    x = BatchNormalization(axis=3, scale=False)(x)

    return x

def MultiResBlock(U, inp, alpha = 1.67):
    '''
    MultiRes Block

    Arguments:
        U {int} -- Number of filters in a corrsponding UNet stage
        inp {keras layer} -- input layer

    Returns:
        [keras layer] -- [output layer]
    '''

    W = alpha * U

    shortcut = inp

    shortcut = conv2d_bn(shortcut, int(W*0.167) + int(W*0.333) +
                         int(W*0.5), 1, 1, activation=None, padding='same')

    conv3x3 = conv2d_bn(inp, int(W*0.167), 3, 3,
                        activation='relu', padding='same')

    conv5x5 = conv2d_bn(conv3x3, int(W*0.333), 3, 3,
                        activation='relu', padding='same')

    conv7x7 = conv2d_bn(conv5x5, int(W*0.5), 3, 3,
                        activation='relu', padding='same')

    out = concatenate([conv3x3, conv5x5, conv7x7], axis=3)
    out = BatchNormalization(axis=3)(out)

    out = add([shortcut, out])
    out = Activation('relu')(out)
    out = BatchNormalization(axis=3)(out)

    return out

def ResPath(filters, length, inp):
    '''
    ResPath

    Arguments:
        filters {int} -- [description]
        length {int} -- length of ResPath
        inp {keras layer} -- input layer

    Returns:
        [keras layer] -- [output layer]
    '''

    shortcut = inp
    shortcut = conv2d_bn(shortcut, filters, 1, 1,
                         activation=None, padding='same')

    out = conv2d_bn(inp, filters, 3, 3, activation='relu', padding='same')

    out = add([shortcut, out])
    out = Activation('relu')(out)
    out = BatchNormalization(axis=3)(out)

    for i in range(length-1):

        shortcut = out
        shortcut = conv2d_bn(shortcut, filters, 1, 1,
                             activation=None, padding='same')

        out = conv2d_bn(out, filters, 3, 3, activation='relu', padding='same')

        out = add([shortcut, out])
        out = Activation('relu')(out)
        out = BatchNormalization(axis=3)(out)

    return out

def MultiResUnet(height, width, n_channels):
    '''
    MultiResUNet

    Arguments:
        height {int} -- height of image
        width {int} -- width of image
        n_channels {int} -- number of channels in image

    Returns:
        [keras model] -- MultiResUNet model
    '''

    inputs = Input((height, width, n_channels))

    mresblock1 = MultiResBlock(32, inputs)
    pool1 = MaxPooling2D(pool_size=(2, 2))(mresblock1)
    mresblock1 = ResPath(32, 4, mresblock1)

    mresblock2 = MultiResBlock(32*2, pool1)
    pool2 = MaxPooling2D(pool_size=(2, 2))(mresblock2)
    mresblock2 = ResPath(32*2, 3, mresblock2)

    mresblock3 = MultiResBlock(32*4, pool2)
    pool3 = MaxPooling2D(pool_size=(2, 2))(mresblock3)
    mresblock3 = ResPath(32*4, 2, mresblock3)

    mresblock4 = MultiResBlock(32*8, pool3)
    pool4 = MaxPooling2D(pool_size=(2, 2))(mresblock4)
    mresblock4 = ResPath(32*8, 1, mresblock4)

    mresblock5 = MultiResBlock(32*16, pool4)

    up6 = concatenate([Conv2DTranspose(
        32*8, (2, 2), strides=(2, 2), padding='same')(mresblock5), mresblock4], axis=3)
    mresblock6 = MultiResBlock(32*8, up6)

    up7 = concatenate([Conv2DTranspose(
        32*4, (2, 2), strides=(2, 2), padding='same')(mresblock6), mresblock3], axis=3)
    mresblock7 = MultiResBlock(32*4, up7)

    up8 = concatenate([Conv2DTranspose(
        32*2, (2, 2), strides=(2, 2), padding='same')(mresblock7), mresblock2], axis=3)
    mresblock8 = MultiResBlock(32*2, up8)

    up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(
        2, 2), padding='same')(mresblock8), mresblock1], axis=3)
    mresblock9 = MultiResBlock(32, up9)

    conv10 = conv2d_bn(mresblock9, 1, 1, 1, activation='sigmoid')

    model = Model(inputs=[inputs], outputs=[conv10])

    return model

model = MultiResUnet(height=256, width=256, n_channels=3)

#from keras import backend as K

smooth = 1e-15
def dice_coef(y_true, y_pred):
    y_true = tf.keras.layers.Flatten()(y_true)
    y_pred = tf.keras.layers.Flatten()(y_pred)
    intersection = tf.reduce_sum(y_true * y_pred)
    return (2. * intersection + smooth) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + smooth)

def dice_loss(y_true, y_pred):
    return 1.0 - dice_coef(y_true, y_pred)

def jacard(y_true, y_pred):

    y_true_f = tf.keras.layers.Flatten()(y_true)
    y_pred_f = tf.keras.layers.Flatten()(y_pred)
    intersection = tf.reduce_sum ( y_true_f * y_pred_f)
    union = tf.reduce_sum( y_true_f + y_pred_f - y_true_f * y_pred_f)
    return (intersection/union)

train_data = tf_dataset(x_train, y_train, batch=BATCH)
valid_data = tf_dataset(x_val, y_val, batch=BATCH)

opt = tf.keras.optimizers.Nadam(LR)
metrics = [dice_coef, Recall(), Precision(), jacard, 'accuracy']
model.compile(loss=dice_loss, optimizer=opt, metrics=metrics)

from datetime import datetime
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow import keras

# for storing logs into tensorboard
logdir="logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")

callbacks = [
    ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=4),
    EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=False),
    ModelCheckpoint("./model_checkpoint", monitor='val_loss'),
    keras.callbacks.TensorBoard(log_dir=logdir)
]

train_steps = len(x_train)//BATCH
valid_steps = len(x_val)//BATCH

if len(x_train) % BATCH != 0:
    train_steps += 1
if len(x_val) % BATCH != 0:
    valid_steps += 1

history = model.fit(
    train_data,
    validation_data=valid_data,
    epochs=EPOCHS,
    steps_per_epoch=train_steps,
    validation_steps=valid_steps,
    callbacks=callbacks
)

import pandas as pd
# convert the history.history dict to a pandas DataFrame:
hist_df = pd.DataFrame(history.history)

# save to json:
hist_json_file = 'history.json'
# with open(hist_json_file, 'a') as out:
#     out.write(hist_df.to_json())
#     out.write(",")
#     out.close()

with open(hist_json_file, mode='w') as f:
    hist_df.to_json(f)

# or save to csv:
hist_csv_file = 'history.csv'
# with open(hist_csv_file, 'a') as out:
#     out.write(str(hist_df.to_csv()))
#     out.write(",")
#     out.close()

with open(hist_csv_file, mode='w') as f:
    hist_df.to_csv(f)

model.save_weights("MultiResUNET_jimutmap_2_10e.h5")
model.save("MultiResUNET_jimutmap_with_weight_2_10e.h5")

test_data = tf_dataset(x_test, y_test, batch=BATCH)

test_steps = (len(x_test)//BATCH)
if len(x_test) % BATCH != 0:
    test_steps += 1

model.evaluate(test_data, steps=test_steps)

def read_img(path):
    xt = cv2.imread(path, cv2.IMREAD_COLOR)
    xt = cv2.cvtColor(xt, cv2.COLOR_BGR2RGB)
    xt = cv2.resize(xt, (IMG_SIZE, IMG_SIZE))
    xt = xt/255.0
    return xt

def read_mask(path):
    xt = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    xt = cv2.resize(xt, (IMG_SIZE, IMG_SIZE))
    xt = np.expand_dims(xt, axis=-1)
    xt = xt/255.0
    return xt

def mask_parse(mask):
    mask = np.squeeze(mask)
    mask = [mask, mask, mask]
    mask = np.transpose(mask, (1, 2, 0))
    return mask

for i, (a, b) in enumerate(zip(x_test[10:30], y_test[10:30])):
    a = read_img(a)
    b = read_mask(b)
    y_pred = model.predict(np.expand_dims(a, axis=0))[0] > 0.5
    #print(y_pred.shape)
    h, w, _ = a.shape
    white_line = np.ones((h, 10, 3))
    mask_red = np.ones((h,w,3))
    # mask_red[:,:,0] = a[:,:,0]*.5
    # mask_red[:,:,1] = a[:,:,1]*0.5
    mask_red[:,:,:] = y_pred[:,:,:] #+ a[:,:,2]*0.5
    mask_red = mask_red*2
    mask_red = np.clip(mask_red, 0, 1)
    all_images = [
        a, white_line,
        mask_parse(b), white_line,
        mask_red
        #mask_parse(y_pred)*0.5+a*0.5
    ]
    image = np.concatenate(all_images, axis=1)

    fig = plt.figure(figsize=(12, 12))
    x = fig.add_subplot(1, 1, 1)
    imgplot = plt.imshow(image)

    plt.savefig('results/'+str(i)+'.png',format='png')
    plt.close()


from PyQt5.QtGui import QTextFormat

from ..pktk import *

EXTRASELECTION_FILTER_KEEP =    0x0001
EXTRASELECTION_FILTER_REMOVE =  0x0002
EXTRASELECTION_FILTER_FCTTRUE = 0x0003

def flatten(items):
    """Return a flatten list whatever the number of nested level the list have

    f=flatten([1,2,[3,4],[5,[6,7],[8,[9,10]]]])

    f: [1,2,3,4,5,6,7,8,9,10]
    """
    returned = []
    for item in items:
        if isinstance(item, (list, tuple)):
            returned.extend(flatten(item))
        else:
            returned.append(item)
    return returned

def rotate(items, shiftValue=1):
    """Rotate list

    - Positive `shiftValue` will rotate to right
    - Negative `shiftValue` will rotate to left

    l=[1,2,3,4]

    x=rotate(l, 1)
    x: [4,1,2,3]

    x=rotate(l, -1)
    x: [2,3,4,1]

    """
    shiftValue = shiftValue % len(items)
    if shiftValue == 0:
        # no rotation...
        return items
    # do rotation
    return items[-shiftValue:] + items[:-shiftValue]

def unique(items):
    """Return list of items with duplicate removed

    Initial order list is preserved
    Note: works with non-hashable object
    """
    # a faster method could be return list(set(items))
    # but it doesn't work with non-hashable object (like a QColor for example)
    returned = []
    for item in items:
        if item not in returned:
            returned.append(item)
    return returned

def sortExtraSelections(extraSelection):
    """Sort given `extraSelection` list from item's format.property

    1) items with format.property(QTextFormat.UserProperty)
       - sorted by extraSelection.format.property(QTextFormat.UserProperty) value
    2) items without format.property(QTextFormat.UserProperty)
       - no special order, just set 'after items with property QTextFormat.UserProperty'
    """
    def checkType(value):
        returned = value.format.property(QTextFormat.UserProperty)
        if returned:
            return returned
        return 0xFFFFFFFF
    if not isinstance(extraSelection, list):
        raise EInvalidType('Given `extraSelection` must be a <list>')
    extraSelection.sort(key=checkType)

def filterExtraSelections(extraSelection, filterValue, filterRule=EXTRASELECTION_FILTER_KEEP, filterProperty=QTextFormat.UserProperty, sortResult=False, stopOnFirst=False, removed=[]):
    """Sort given `extraSelection` list from item's property

    If `filterProperty` is given, define on which extraSelection.format.property(filterProperty) value is filtered

    Given `filterRule` define what to do:
    - EXTRASELECTION_FILTER_KEEP (default):
        only keep items for which extraSelection.format.property(filterProperty) equal `filterValue`
        (in this case, items without property are removed)
    - EXTRASELECTION_FILTER_REMOVE:
        only keep items for which extraSelection.format.property(filterProperty) do not equal `filterValue`
        (in this case, items without property are removed)
    - EXTRASELECTION_FILTER_FCTTRUE
        in this cas `filterValue` is a callable function for which:
        - extraSelection item is provided
        - returned result is a boolean:
            . True = keep item
            . False = remove item

    If `sortResult` is True, filtered list is sorted, otherwise returned list keep initial item orders

    If `stopOnFirst` is True (default is False), stop to filter on first occurence found
    This can be used when we know there's normally no more than one occurence of item to filter in list
    """
    if not isinstance(extraSelection, list):
        raise EInvalidType('Given `extraSelection` must be a <list>')

    removed.clear()
    if filterRule == EXTRASELECTION_FILTER_FCTTRUE:
        if not callable(filterValue):
            raise EInvalidType("Given `filterValue` must be a callable when `filterRule` is set to EXTRASELECTION_FILTER_FCTTRUE")

        index = len(extraSelection) - 1
        while index >= 0:
            if filterValue(extraSelection[index]) is False:
                removed.append(extraSelection.pop(index))
                if stopOnFirst:
                    return
            index -= 1
    elif filterRule == EXTRASELECTION_FILTER_KEEP:
        index = len(extraSelection) - 1
        while index >= 0:
            if extraSelection[index].format.property(filterProperty) != filterValue:
                removed.append(extraSelection.pop(index))
                if stopOnFirst:
                    return
            index -= 1
    elif filterRule == EXTRASELECTION_FILTER_REMOVE:
        index = len(extraSelection) - 1
        while index >= 0:
            if extraSelection[index].format.property(filterProperty) == filterValue:
                removed.append(extraSelection.pop(index))
                if stopOnFirst:
                    return
            index -= 1


import socket

'''
大多数连接都是可靠的TCP连接。创建TCP连接时，主动发起连接的叫客户端，被动响应连接的叫服务器。
举个例子，当我们在浏览器中访问新浪时，我们自己的计算机就是客户端，浏览器会主动向新浪的服务器发起连接。
如果一切顺利，新浪的服务器接受了我们的连接，一个TCP连接就建立起来的，后面的通信就是发送网页内容了。
'''

'''
创建Socket时，AF_INET指定使用IPv4协议，如果要用更先进的IPv6，就指定为AF_INET6。
SOCK_STREAM指定使用面向流的TCP协议，这样，一个Socket对象就创建成功，但是还没有建立连接。
'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 建立连接,注意参数是一个tuple，包含地址和端口号
s.connect(('www.sina.com.cn', 80))

#发送数据:
s.send(b'GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n')
# 接收数据:
buffer = []
while True:
    # 每次最多接收1k字节:
    d = s.recv(1024)
    if d:
        buffer.append(d)
    else:
        break
data = b''.join(buffer)
print(data)

# 关闭连接:
s.close()
header, html = data.split(b'\r\n\r\n', 1)
print(header.decode('utf-8'))
# 把接收的数据写入文件:
with open('sina.html', 'wb') as f:
    f.write(html)


from __future__ import absolute_import

try: # py3
    import socketserver
    from http import client
    from http import server
except ImportError: # py2
    import httplib as client
    import BaseHTTPServer as server
    import SocketServer as socketserver

from .. import logs
from .. import utils
from .. import param
from .. import __version__

from . import Connection, Transport

SERVER_NAME = 'snekrpc'

log = logs.get(__name__)

# BaseRequestHandler is remarkably not an object in Py2
class HTTPHandler(server.BaseHTTPRequestHandler, object):
    protocol_version = 'HTTP/1.1'

    def do_POST(self):
        # handle command
        ifc = self.server._interface
        con = HTTPServerConnection(
            ifc, utils.url.format_addr(self.client_address), self)
        with con:
            while not con.should_close:
                ifc.handle(con)

    def start_response(self):
        # start response
        self.send_response(200)
        self.send_header('Connection', 'keep-alive')
        # TODO: set the content-type based on the codec used
        self.send_header('Content-Type', 'application/octet-stream')
        self.send_header('Transfer-Encoding', 'chunked')

        # user headers
        for k, v in self.server._headers.items():
            self.send_header(k, v)
        self.end_headers()

    def version_string(self):
        return self.server._version or '{}/{} {}'.format(SERVER_NAME,
            __version__, self.server._interface.version or '').strip()

    def log_request(self, code='-', size='-'):
        url = utils.url.format_addr(self.client_address)
        log.debug('%r %s <- %s', self.requestline, code, url)

class HTTPTransport(Transport):
    _name_ = 'http'
    Handler = HTTPHandler

    @param('headers', dict)
    def __init__(self, url, headers=None, version=None):
        super(HTTPTransport, self).__init__(url)
        url = self._url
        self._addr = (url.host, url.port)

        self.headers = headers
        self.version = version

    def connect(self, client):
        return HTTPClientConnection(client, utils.url.format_addr(self._addr))

    def serve(self, server):
        self._server = ThreadingHTTPServer(self._addr, self.Handler)
        self._server._headers = self.headers or {}
        self._server._version = self.version
        self._server._interface = server

        log.info('listening: %s', self.url)
        # XXX: expose poll_interval for config?
        self._server.serve_forever()

    def stop(self):
        # close the server's socket
        self._server.server_close()
        # stop the serve loop
        self._server.shutdown()

    def join(self, timeout=None):
        # ideally we could pass the timeout to the __is_shut_down Event here
        pass

class ThreadingHTTPServer(socketserver.ThreadingMixIn, server.HTTPServer):
    daemon_threads = True

    def handle_error(self, request, client_address):
        log.exception('request error (%s)', utils.url.format_addr(client_address))

class HTTPClientConnection(Connection):
    def __init__(self, client_ifc, url):
        super(HTTPClientConnection, self).__init__(client_ifc, url)

        self._res = None

        self._con = con = client.HTTPConnection(url)
        con.auto_open = False
        self.connect()

    def connect(self):
        con = self._con
        con.connect()
        log.debug('connected: %s', self.url)

        con.putrequest('POST', '')
        con.putheader('Connection', 'keep-alive')
        con.putheader('Content-Type', 'application/octet-stream')
        con.putheader('Transfer-Encoding', 'chunked')
        con.endheaders()

    def recv(self):
        if not self._res:
            self._res = self._con.getresponse()
        rfile = self._res.fp

        line = rfile.readline()
        chunk_len = int(line[:-2], 16)

        # +2 for the trailing newline (not included in chunk_len)
        return rfile.read(chunk_len + 2)[:-2]

    def send(self, data):
        con = self._con

        con.send('{:X}\r\n'.format(len(data)).encode('ascii'))
        con.send(data + b'\r\n')

    def close(self):
        log.debug('disconnected: %s', self.url)

class HTTPServerConnection(Connection):
    def __init__(self, server, url, handler):
        super(HTTPServerConnection, self).__init__(server, url)

        self.handler = handler
        self.response_started = False
        self.should_close = False

        log.debug('connected: %s', self.url)

    def recv(self):
        rfile = self.handler.rfile

        line = rfile.readline()
        if not line:
            self.should_close = True
            return None
        chunk_len = int(line[:-2], 16)

        # +2 for the trailing newline (not included in chunk_len)
        return rfile.read(chunk_len + 2)[:-2]

    def send(self, data):
        if not self.response_started:
            self.handler.start_response()
            self.response_started = True

        wfile = self.handler.wfile

        # send chunked
        wfile.write('{:X}\r\n'.format(len(data)).encode('ascii'))
        wfile.write(data + b'\r\n')

    def close(self):
        self.handler.wfile.flush()
        log.debug('disconnected: %s', self.url)


import datetime
import socket               # Import socket module
import json

from rest_framework import viewsets
from rest_framework.response import Response
from django.shortcuts import render
from django.apps import apps
from django.http import JsonResponse
from django.views.generic import View

from app.words import Words
from .models import Word
from .serializers import WordSerializer

class FrontendRenderView(View):
    def get(self, request, *args, **kwargs):
        return render(request, "index.html", {})

class WordViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Word.objects.all().order_by('id')
    serializer_class = WordSerializer
    # permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        """
        Return data for specified word.
        """
        # word = self.kwargs['word']
        # return Word.objects.filter(word=word)
        queryset = Word.objects.all()
        return queryset

    def retrieve(self, request, *args, **kwargs):
        words = Word.objects.filter(word=kwargs['word'])
        serializer = WordSerializer(words, many=True)
        return Response(serializer.data)

def get_words_from_letters(request, *args, **kwargs):
    if request.method == 'GET':
        response = {}
        letters = list(request.GET['letters'].lower())
        if apps.get_app_config('app').use_cpp_server:
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)         # Create a socket object
            server_address = 'cpp/socket'
            s.connect(server_address)
            s.sendall(''.join(letters).encode())
            data = s.recv(1024*(len(letters)**5))
            response = json.loads(data.decode())
            print(data)

        else:
            Words.all_subsets = []
            trie = apps.get_app_config('app').trie
            start = datetime.datetime.now()
            for l in letters:
                Words.get_all_subsets(l, letters, trie)
            all_words_from_letters = Words.all_subsets
            # all_words_from_letters = Words.get_all_words_from_letters(letters=letters)
            print("nr of words to check: ", len(all_words_from_letters))
            end = datetime.datetime.now()
            print("time elapsed after generating all words from letters = ", (end - start).total_seconds())
            # trie = apps.get_app_config('app').trie
            start = datetime.datetime.now()

            # all_words_from_letters.remove('')
            for word in all_words_from_letters:
                if trie.include(word):
                    response[word] = Words.calculate_points(word)
                # s.sendall(word.encode())
                # data = s.recv(1024)
                # if int(data) == 1:
                #     response[word] = Words.calculate_points(word)
                # else:
                #     all_words_from_letters = list(filter(lambda w: not w.startswith(word), all_words_from_letters))

            # s.close()
            end = datetime.datetime.now()
            print("time elapsed after all = ", (end - start).total_seconds())

        return JsonResponse(response, safe=False, json_dumps_params={'ensure_ascii': False})


import sys
input = sys.stdin.readline

n = int(input()) # n : 수열의 크기
numbers = [int(i) for i in input().split()] # n개 숫자 리스트
dp = [[0 for _ in range(n)] for _ in range(n)]
# dp[처음][끝] : 처음 ~ 끝 경우 팰린드롬 여부 확인하는 배열

for i in range(n): # 길이가 1인 경우 -> 팰린드롬
    dp[i][i] = 1

for i in range(n-1): # 길이가 2인 경우
    if numbers[i] == numbers[i + 1]: # 두 문자가 같은 경우 -> 팰린드롬
        dp[i][i+1] = 1

for j in range(2, n): # 길이가 3 이상인 경우
    for i in range(n - j):
        if numbers[i] == numbers[i+j] and dp[i+1][i+j-1] == 1:
            # 처음 문자 == 끝문자 && dp[처음+1][마지막-1]이 1인 경우 -> 팰린드롬
            dp[i][i+j] = 1

m = int(input()) # m : 질문의 개수

for _ in range(m):
    s, e = [int(a) for a in input().split()] # s~e만큼 팰린드롬이 되는지 출력
    print(dp[s-1][e-1])


import bpy
import math
import json
import copy
import time
import os.path
import pathlib
from math import radians, degrees
from mathutils import Color, Matrix, Euler, Vector
from threading import Thread
from bpy.app.handlers import persistent

from . import utils

class SceneConstructor:

    def __init__(self, name: str):
        self.name = name
        self.error_handler: ErrorHandler = ErrorHandler()

        self.collection, self.layer_collection, self.scene_controller = self._create_scene_collection()
        self.can_import_characters = utils.check_for_xps_importer()

        self.active_armature = None

    def _create_scene_collection(self):
        collection = bpy.data.collections.new(self.name)
        bpy.context.scene.collection.children.link(collection)

        layer_collection = utils.find_layer_collection(collection.name)

        scene_controller = utils.create_empty(link_collection=collection)
        scene_controller.name = "Scene Controller"
        return collection, layer_collection, scene_controller

    def create_light(self, index, direction, intensity, color, shadow_depth):
        # Set name
        name = f"Light {index}"

        # Create light
        light_data = bpy.data.lights.new(name=name, type='SUN')
        light = bpy.data.objects.new(name=name, object_data=light_data)
        self.collection.objects.link(light)

        # Rotate light direction from XPS to Blender
        direction = utils.rotate(direction, (90, 0, 0))

        sun = False

        if sun:
            # Set light properties
            light.data.color = color
            light.data.energy = 0.03
            light.data.energy = 10 / 3 * intensity / 100

            # Apply direction to light by pointing the light at the direction location
            utils.look_at(light, direction)

            # Move light above the scene
            light.location[2] = 5

        # Setup lights as point lights
        else:
            c = Color(color)
            c.s += 0.1

            light.data.type = "POINT"
            light.location = -direction * 3
            light.data.energy = intensity * 2.4
            light.data.color = c
            light.data.shadow_soft_size = 2
            light.data.use_contact_shadow = True
            light.data.contact_shadow_thickness = 0.005

            # Create empty at the center so the light can be easily rotated
            empty = utils.create_empty(link_collection=self.collection)
            empty.location[2] += 1
            empty.name = f"{light.name} Controller"
            light.parent = empty

            empty.parent = self.scene_controller

    def create_camera(self, fov, target_pos, distance, rotation_horizontal, rotation_vertical):
        # Create camera object
        camera_data = bpy.data.cameras.new(name="Camera")
        camera = bpy.data.objects.new(name="Camera", object_data=camera_data)
        self.collection.objects.link(camera)

        # Create camera controller
        camera_controller = utils.create_empty(link_collection=self.collection)
        camera_controller.name = "Camera Controller"
        camera.parent = camera_controller
        camera_controller.parent = self.scene_controller

        # Move camera to the correct distance
        camera.location[2] = distance

        # Make camera look at the empty
        utils.look_at(camera, camera_controller.location)

        # Rotate target location from XPS space to Blender space
        target_pos = utils.rotate(target_pos, (90, 0, 0))

        # Move the controller to the target location
        camera_controller.location = target_pos

        # Rotate controller by the rotation values
        camera_controller.rotation_euler = (radians(90) - rotation_vertical, 0, rotation_horizontal)

        # Calculate the field of view angle
        angle_unit = 0.0872664600610733 / 5
        camera_angle = int(fov / angle_unit)

        # Calculate focal length from angle
        focal_length = camera.data.sensor_width / 2 / math.tan(radians(camera_angle) / 2)

        # Apply the focal length
        camera.data.lens = focal_length

        # More camera settings
        bpy.context.scene.render.resolution_percentage = 200

        # Set this camera to active
        bpy.context.scene.camera = camera

    def add_character(self, file_directory, file_name, visibility):
        self.active_armature = None
        if not self.can_import_characters:
            self.error_handler.add_error("XPS Importer not installed, skipping character import.")
            return

        # Search for the model directory in the install and asset folders
        character_folder, mesh_file = utils.search_dirs_for_model(file_directory, file_name)

        if not character_folder or not character_folder.exists():
            folder_name = file_directory.split("\\")[-1]
            self.error_handler.add_error(f"Model '{folder_name}' was not found in any selected folder. Full original path: '{file_directory}'")
            return
        if not mesh_file:
            self.error_handler.add_error(f"Could not find any folder '{character_folder.name}' containing the file '{file_name}' (.xps, .mesh, .ascii).")
            return

        filepath_full = character_folder / mesh_file
        print(f"\nImporting character {str(filepath_full)}...")

        # Set the active collection to this XPS scene collection
        bpy.context.view_layer.active_layer_collection = self.layer_collection

        # Save all current collections to check witch one was added
        collections_pre = [c for c in bpy.data.collections]

        # Import the character from the given path
        bpy.ops.xps_tools.import_model(
            "EXEC_DEFAULT",
            filepath=str(filepath_full),
        )
        print(f"Imported character {str(filepath_full)}...\n")

        # Get the added collection
        character_collection = None
        for c in bpy.data.collections:
            if c not in collections_pre:
                character_collection = c
                break
        if not character_collection:
            self.error_handler.add_error(f"Imported character '{filepath_full}' collection not found, skipping character import.")
            return

        # Hide all objects in the collection if they should be hidden
        for obj in character_collection.objects:

            # Hide and rename all accessories
            if obj.parent and obj.parent.type == "ARMATURE":
                name = obj.name.split("_")[1]  # Separate name by underscores
                name = " ".join([s.capitalize() for s in name.split(".")])  # Remove dots and capitalize
                hide = False
                if name.startswith("-"):
                    name = f"Item " + name[1:].capitalize()
                    hide = True
                elif name.startswith("+"):
                    name = f"Item " + name[1:].capitalize()

                if not visibility:
                    hide = True

                # obj.name = name  # TODO: judgearts thinks that renaming this to something more nice is not good. So it's disabled for now
                utils.set_hide(obj, hide)
                continue

            # Hide the rest of the objects
            utils.set_hide(obj, not visibility)

        # Get the armature from the collection and set it as active
        for obj in character_collection.objects:
            if obj.type == "ARMATURE":
                self.active_armature = obj
                self.active_armature.parent = self.scene_controller
                self.active_armature.name = character_folder.parts[-1]
                utils.set_hide(self.active_armature, True)
                break
        if not self.active_armature:
            self.error_handler.add_error(f"Character collection '{character_collection.name}' does not contain an armature, skipping character pose.")
            return

        # Move all objects from the character-collection to this xps scene collection
        for obj in character_collection.objects:
            self.collection.objects.link(obj)
            character_collection.objects.unlink(obj)

        # Delete the character collection
        bpy.data.collections.remove(character_collection, do_unlink=True)

    def pose_character(self, bone_name, rot, loc, scale):
        if not self.active_armature:
            return

        # Get the bone
        bone = self.active_armature.pose.bones.get(bone_name)
        if not bone:
            # print(f"Armature '{self.active_armature.name}' does not contain bone '{bone_name}', skipping bone pose.")
            return

        # Posing bone
        if rot[0] or rot[1] or rot[2]:
            utils.xps_bone_rotate(bone, Vector(rot))

        if loc[0] or loc[1] or loc[2]:
            utils.xps_bone_translate(bone, Vector(loc))

        if scale[0] != 1 or scale[1] != 1 or scale[2] != 1:
            # TODO: This is absolutely not like the XPS behavior, but it's somewhat close
            utils.xps_bone_scale(bone, Vector(scale))

    def transform_character(self, location, scale):
        if not self.active_armature:
            return
        x, y, z = location
        self.active_armature.location = (x, -z, y)
        x, y, z = scale
        self.active_armature.scale = (x, z, y)

    def remove(self):
        layer_collection = utils.find_layer_collection(self.collection.name)
        utils.delete_hierarchy(layer_collection)

    def set_camera_resolution(self, width, height):
        if height > 10000 or width > 10000:
            self.error_handler.add_error(f"Camera resolution is too big (>10000), likely due to incorrect reading of the scene file."
                                         f"\n    Please report this in our Github repository and attach this scene file.")
            return
        bpy.context.scene.render.resolution_x = width
        bpy.context.scene.render.resolution_y = height

    def create_ground(self, texture_path, visibility):
        # Problem is that the scene always contains "data/ground.png" as the texture path, but it doesn't exist as a file
        # XNALara probably defaults to the importing the floor mesh from data/Floor/Floor/Generic_Item.mesh
        # Therefore we just import this by default

        filepath = "data\\Floor\\Floor"
        self.add_character(filepath, "generic_item", visibility)
        plane_armature = self.active_armature
        if not plane_armature:
            self.error_handler.remove_error_containing_str(f"'{filepath}'")
            self.error_handler.add_error(f"Could not find ground model, probably due to an unknown XPS installation folder.")
            return

        for obj in plane_armature.children:
            if obj.type != "MESH":
                continue
            mat = obj.data.materials[0]
            mat.shadow_method = 'NONE'

class ErrorHandler:
    def __init__(self):
        self.errors = []

    def add_error(self, error):
        if error not in self.errors:
            self.errors.append(error)
        print(error)

    def get_error_message(self):
        error_msg = "Errors while importing scene:"
        for error in self.errors:
            error_msg += f"\n- {error}"

        print(error_msg)
        return error_msg

    def has_errors(self):
        return len(self.errors) > 0

    def remove_error_containing_str(self, containing_str):
        [self.errors.remove(error) for error in self.errors if containing_str in error]

class SettingsHandler:
    structure = {
        "xps_importer_install_dir": "",
        "xps_importer_asset_dir": "",
    }

    @staticmethod
    def init():
        # Add the load_settings function to the load_post handler in order to apply the settings as soon as a new file gets loaded
        bpy.app.handlers.load_post.append(SettingsHandler.load_settings)

    @staticmethod
    def save_settings():
        settings = copy.deepcopy(SettingsHandler.structure)
        for key in settings.keys():
            settings[key] = getattr(bpy.context.scene, key)
        with open(utils.settings_file, "w+") as f:
            json.dump(settings, f, indent=4)

    @staticmethod
    @persistent
    def load_settings(arg1=None, arg2=None):
        if not utils.settings_file.exists():
            return
        with open(utils.settings_file, "r") as f:
            settings = json.load(f)
        for key, value in settings.items():
            if not value:
                return
            setattr(bpy.context.scene, key, value)


class Person:

    def __init__(self, name, email, age):
        self.name = name
        self.email = email
        self.age = age

    def print_data(self):
        print(self.name, self.age)

class Dog:
    name = 'Bingo'

    def print_name(self):
        print(f"The dog's name is: {self.name}")

if __name__ == '__main__':
    d = Dog()
    f = Dog()
    d.name = 'Luke'
    # del d
    # d.print_name()
    # f.print_name()

    p = Person('Israel', 'myemail@ymail.com', 19)

    print(p.name)

    q = Person('Hassan', 'hassan@gamil.com', 20)

    print(q.email)

    r = Person('Bille', '', 343)

    r.print_data()

    print(r.name)


from torch import nn
import torch
from attention import Attention
from config import opt

class Model(nn.Module):
    def __init__(self, args):
        super(Model, self).__init__()
        self.args = args
        self.content = nn.GRU(input_size=self.args.embedding_dim, hidden_size=self.args.hidden_dim, bidirectional=False, num_layers=1).to(opt.device)
        self.reply = nn.GRU(input_size=self.args.embedding_dim, hidden_size=self.args.hidden_dim, bidirectional=False, num_layers=1).to(opt.device)
        self.bert_vector = nn.GRU(input_size=768, hidden_size=self.args.hidden_dim, bidirectional=False, num_layers=1).to(opt.device)
        self.linear = nn.Linear(self.args.hidden_dim, self.args.hidden_dim).to(opt.device)  # output: (L, B, H) -> (L, B, H)
        self.content_linear = nn.Linear(self.args.hidden_dim, self.args.hidden_dim).to(opt.device)  # output: (L, B, H) -> (L, B, H)
        self.output_linear = nn.Linear(self.args.multi_view_num * self.args.batch_size * self.args.hidden_dim, 1).to(opt.device)
        self.attention = Attention(self.args)

    def forward(self, event):
        multi_view = torch.tensor([]).to(opt.device)

        # content
        if self.args.content:
            #print(self.args.multi_view_name)
            #print(event.content)
            X_content_data = torch.tensor(event.content_word_embedding_array, dtype=torch.float).to(opt.device)
            X_content_data = X_content_data.unsqueeze(1).to(opt.device)

            output_content, hidden_content = self.content(X_content_data)
            output_content = self.content_linear(output_content)
            if self.args.content_attention:
                output_content, attn_dist_content = self.attention(output_content)
            else:
                output_content = output_content.sum(0)/output_content.size(0)
            output_content = output_content.flatten()
            multi_view = torch.cat((multi_view, output_content)).to(opt.device)

        # reply
        if self.args.reply:
            #print(event.reply_no_same)
            X_reply_data = torch.tensor(event.reply_sentence_embedding_array, dtype=torch.float).to(opt.device)
            X_reply_data = X_reply_data.unsqueeze(1).to(opt.device)

            output_reply, hidden_reply = self.reply(X_reply_data)
            output_reply = self.linear(output_reply)
            if self.args.attention:
                output_reply, atten_dist_reply = self.attention(output_reply)
            else:
                output_reply = output_reply.sum(0)/output_reply.size(0)
            output_reply = output_reply.flatten()
            multi_view = torch.cat((multi_view, output_reply)).to(opt.device)

        # bert vector
        if self.args.bert_vector:
            X_bert_vector_data = torch.tensor(event.bert_vector, dtype=torch.float).to(opt.device)

            output_bert_vector,hidden_bert_vector = self.bert_vector(X_bert_vector_data)
            output_bert_vector = self.linear(output_bert_vector)
            if self.args.attention:
                output_bert_vector, atten_dist_bert_vector = self.attention(output_bert_vector)
            else:
                output_bert_vector = output_bert_vector.sum(0)/output_bert_vector.size(0)
            output_bert_vector = output_bert_vector.flatten()
            multi_view = torch.cat((multi_view, output_bert_vector)).to(opt.device)

        result = torch.sigmoid(self.output_linear(multi_view))
        loss = (event.label - result)**2
        return loss, result

if __name__ == '__main__':
    model = Model()
    batch = torch.tensor([[0, 1]])
    print(batch.shape)
    print(model.embed(batch).shape)


# python program to parse json

import json
import os

os.chdir('c:/Users/mayur/OneDrive/Documents/Study Material/Python/Interview/challenge-2/')

f = open('response.json', 'r')
result = f.read()
f.close()

my_dict = json.loads(result)

# Take key input from user

key = input("Enter metadata key")
val = my_dict[key]

print(val)


import requests
from setup import INFO
import re
from bs4 import BeautifulSoup as Soup

class Engine:
    def __init__(e, name, create_query):
        e.name = name
        e.create_query = create_query

    def get_search_url(e, query):
        INFO("original query: {}".format(query))
        # lower case the query
        query = query.lower()
        # replace spaces
        query = '%20'.join(query.split(' '))
        # join together
        search_url = e.create_query(query)
        INFO("parsed search_url is {}".format(search_url))
        return search_url

    def get_magnet(e, query):
        search_url = e.get_search_url(query)
        INFO("requesting from {}".format(e.name))
        response = requests.get(search_url)
        if response.status_code != 200:
            INFO("an error {} occured with {} request".format(response.status_code, search_url))
            return None
        else:
            INFO("request {} succeeded".format(search_url))
            soup = Soup(response.text, features="html.parser")
            magnet = soup.find(href = re.compile('magnet'))
            if magnet is None:
                INFO("No result found in {}".format(e.name))
            magnet_link = magnet['href']
            INFO("returning magnet {}".format(magnet_link))
            return magnet_link

TPB = Engine(
    "TPB",
    lambda q: "https://thepiratebay.org/search/{}/0/7/0".format(q)
    )
KICKASS = Engine(
    "KICKASS",
    lambda q: "https://katcr.co/usearch/torrents-search.php?q={}".format(q)
    )
EZTV = Engine(
    "EZTV",
    lambda q: "https://eztv.ag/search/{}".format(q)
    )
L337 = Engine(
    "L337",
    lambda q: "https://1337x.to/search/{}/1/".format('+'.join(q.split('%20')))
    )

ENGINES = [
    TPB,
    EZTV,
    L337
]


import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
import matplotlib.pyplot as plt
import seaborn as sns
from collections import Counter
from sklearn.model_selection import RandomizedSearchCV
import xgboost
import pickle
import joblib

# device = xm.xla_device()
# torch.arange(0, 100, device=device)
#from simpletransformers.classification import ClassificationModel

from transformers import AutoTokenizer, AutoModelForMaskedLM
import spacy
from nltk.stem.snowball import SnowballStemmer
from unidecode import unidecode

# %%
df_told = pd.read_csv('../ToLD-BR.csv')
df_told

# %%
y =[]
for i in range(len(df_told)):
  if df_told.iloc[i,1:].sum()>0:
    y.append(1)
  else:
    y.append(df_told.iloc[i,1:].sum())
np.unique(y)
df_told['bin_class'] = y
df_told = df_told#[0:5000]

# %%
nlp = spacy.load('pt_core_news_lg')
textos = [unidecode(x) for x in list(df_told['text'])]
textos_tokenized = []
for separado in textos:
  textos_middle = []
  doc = nlp(separado)
  for token in doc:
    if not token.is_stop:
      textos_middle.append(token.text)
  textos_tokenized.append(textos_middle)

stemmer = SnowballStemmer(language='portuguese')
textos_tokenized_clean_stem = []
for i in range(len(textos_tokenized)):
  textos_middle =[]
  for j in textos_tokenized[i]:
    textos_middle.append(stemmer.stem(j))
  textos_tokenized_clean_stem.append(textos_middle)

textos_tokenized_clean_lemma =[]
for i in range(len(textos_tokenized)):
  separado = ' '.join(textos_tokenized[i])
  doc = nlp(separado)
  textos_middle =[]
  for token in doc:
    textos_middle.append(token.lemma_)
  textos_tokenized_clean_lemma.append(textos_middle)
textos_lemma = [' '.join(x) for x in textos_tokenized_clean_lemma]
textos_stemma = [' '.join(x) for x in textos_tokenized_clean_stem]

# %%
from sklearn.feature_extraction.text import TfidfVectorizer
def tokenize_spacy(sentence):
    doc = nlp(sentence)
    return [w.text for w in doc]
vectorizer = TfidfVectorizer(tokenizer=tokenize_spacy, ngram_range=(1, 4),lowercase = True)
matrix = vectorizer.fit_transform(textos_lemma)

# %%
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(matrix, y, test_size=0.2, random_state=42)

# %%
n_iter = 5
clf = RandomForestClassifier(max_depth=100, random_state=42,n_estimators=500)
# Number of trees in random forest
n_estimators = [int(x) for x in np.linspace(start = 100, stop = 5000, num = 1000)]
# Number of features to consider at every split
max_features = ['log2', 'sqrt',None]
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(10, 500, num = 1000)]
max_depth.append(None)
# Minimum number of samples required to split a node
# min_samples_split = [int(x) for x in np.linspace(1, 100, num = 100)]
# Minimum number of samples required at each leaf node
min_samples_leaf = [int(x) for x in np.linspace(10, 100, num = 1000)]
# Method of selecting samples for training each tree
bootstrap = [True, False]
# Create the random grid
random_dict_rf = {'n_estimators': [],
               'max_features': [],
               'max_depth': [],
               # 'min_samples_split': [],
               'min_samples_leaf': [],
               'bootstrap': [],
                'f1':[],
                 'acc':[]}
early_stop_flag = 0
for i in range(0,n_iter):
    print(f'iteration:{i}, early stopping = {early_stop_flag}')
    if i>=2:
        if random_dict_rf['f1'][i-2]>=random_dict_rf['f1'][i-1]:
            early_stop_flag+=1
    if early_stop_flag >=5:
        break
    n_estimators_ = np.random.choice(n_estimators)
    random_dict_rf['n_estimators'].append(n_estimators_)
    max_features_ = np.random.choice(max_features)
    random_dict_rf['max_features'].append(max_features_)
    max_depth_ = np.random.choice(max_depth)
    random_dict_rf['max_depth'].append(max_depth_)
    # min_samples_split_ = np.random.choice(min_samples_split)
    # random_dict_rf['min_samples_split'].append(min_samples_split_)
    min_samples_leaf_ = np.random.choice(min_samples_leaf)
    random_dict_rf['min_samples_leaf'].append(min_samples_leaf_)
    bootstrap_ = np.random.choice(bootstrap)
    random_dict_rf['bootstrap'].append(bootstrap_)
    random_grid = {'n_estimators': n_estimators_,
               'max_features': max_features_,
               'max_depth': max_depth_,
               # 'min_samples_split': min_samples_split_,
               'min_samples_leaf': min_samples_leaf_,
               'bootstrap': bootstrap_}
    clf = RandomForestClassifier(**random_grid)
    clf.fit(X_train, y_train)
    print("RF model acquired")
    y_pred = clf.predict(X_test)
    print('Accuracy: %.3f' % accuracy_score(y_test, y_pred))
    random_dict_rf['acc'].append(accuracy_score(y_test, y_pred))
    print('F1 Score: %.3f' % f1_score(y_test, y_pred,average = 'micro'))
    random_dict_rf['f1'].append(f1_score(y_test, y_pred,average = 'micro'))

best_iter = random_dict_rf['f1'].index(max(random_dict_rf['f1']))
n_estimators = random_dict_rf['n_estimators'][best_iter]
max_features = random_dict_rf['max_features'][best_iter]
max_depth = random_dict_rf['max_depth'][best_iter]
# min_samples_split = random_dict_rf['min_samples_split'][best_iter]
min_samples_leaf = random_dict_rf['min_samples_leaf'][best_iter]
bootstrap = random_dict_rf['bootstrap'][best_iter]
random_grid = {'n_estimators': n_estimators_,
             'max_features': max_features_,
             'max_depth': max_depth_,
             # 'min_samples_split': min_samples_split_,
             'min_samples_leaf': min_samples_leaf_,
             'bootstrap': bootstrap_}
clf = RandomForestClassifier(**random_grid)
clf.fit(X_train, y_train)

filename = "rf_model_tfidf_.pickle"

# save model
# clf = pickle.load(open('rf_model_nors.pickle', 'rb'))
joblib.dump(clf, 'rf_model_rs_tfidf_.pkl')
pickle.dump(clf, open("rf_model_rs_tfidf_.sav", "wb"))
pickle.dump(clf, open("rf_model_rs_tfidf_.pickle", "wb"))
y_pred = clf.predict(X_test)
print('Precision: %.3f' % precision_score(y_test, y_pred))
print('Recall: %.3f' % recall_score(y_test, y_pred))
print('Accuracy: %.3f' % accuracy_score(y_test, y_pred))
print('F1 Score: %.3f' % f1_score(y_test, y_pred))

conf_matrix = confusion_matrix(y_true=y_test, y_pred=y_pred)
plt.figure(figsize = (10,7))
sns.heatmap(conf_matrix, annot=True)

ax =sns.heatmap(conf_matrix, annot=True)

# save the plot as PDF file
plt.savefig("confusionmatrix_told_rf_rs_tfidf_.png", format='png')

# %%
classifier = xgboost.XGBClassifier()
random_grid = {
 "learning_rate" : [x for x in np.linspace(start = 1e-6, stop = 1e-2, num = 1000)],
 "max_depth" : [int(x) for x in np.linspace(10, 5000, num = 1000)],
 "min_child_weight" : [int(x) for x in np.linspace(1, 500, num = 500)],
 "gamma": [x for x in np.linspace(start = 2e-3, stop = 7e-1, num = 100)],
 "colsample_bytree" : [x for x in np.linspace(start = 2e-3, stop = 7e-1, num = 100)]
}
def xgboost_rs(random_grid,classifier):
    xg_random = RandomizedSearchCV(estimator = classifier,scoring = ['accuracy','f1'],param_distributions = random_grid, n_iter = n_iter, cv = 5, verbose=10, random_state=42, n_jobs = -1,refit='f1')
    # Fit the random search model
    # xg_random.set_params(**fit_params)
    xg_random.fit(X_train, y_train)#,**fit_params)
    #clf.fit(X_train, y_train)
    print("xg model acquired")
    xg_clf = xg_random.best_estimator_
    return xg_clf
xg_clf = xgboost_rs(random_grid,classifier)

# save model
joblib.dump(xg_clf, 'xg_model_tfidf_joblib.pkl')
pickle.dump(xg_clf, open("xg_model_tfidf_.sav", "wb"))
pickle.dump(xg_clf, open("xg_model_tfidf_.pickle", "wb"))
y_pred = xg_clf.predict(X_test)
print('Precision: %.3f' % precision_score(y_test, y_pred))
print('Recall: %.3f' % recall_score(y_test, y_pred))
print('Accuracy: %.3f' % accuracy_score(y_test, y_pred))
print('F1 Score: %.3f' % f1_score(y_test, y_pred))

conf_matrix = confusion_matrix(y_true=y_test, y_pred=y_pred)
plt.figure(figsize = (10,7))
sns.heatmap(conf_matrix, annot=True)

ax =sns.heatmap(conf_matrix, annot=True)

# save the plot as PDF file
plt.savefig("confusionmatrix_told_xg_rs_tfidf_.png", format='png')

# %%


import torch
import torch.nn as nn

import graph.mol_features as mol_features
import pdb

class GraphConv(nn.Module):
    def __init__(self, args):
        """Creates graph conv layers for molecular graphs."""
        super(GraphConv, self).__init__()
        self.args = args
        hidden_size = args.hidden_size

        self.n_atom_feats = mol_features.N_ATOM_FEATS
        self.n_bond_feats = mol_features.N_BOND_FEATS

        # Weights for the message passing network
        self.W_message_i = nn.Linear(self.n_atom_feats + self.n_bond_feats,
                                     hidden_size, bias=False,)
        if args.no_share:
            self.W_message_h = nn.ModuleList(
                [nn.Linear(hidden_size, hidden_size, bias=False) for _ in range(args.depth - 1)])
        else:
            self.W_message_h = nn.Linear(hidden_size, hidden_size, bias=False,)
        self.W_message_o = nn.Linear(self.n_atom_feats + hidden_size,
                                     hidden_size)

        self.dropout = nn.Dropout(args.dropout)

    def index_select_nei(self, input, dim, index):
        # Reshape index because index_select expects a 1-D tensor. Reshape the
        # output afterwards.
        target = torch.index_select(
            input=input,
            dim=0,
            index=index.view(-1)
        )
        return target.view(index.size() + input.size()[1:])

    def forward(self, graph_inputs):
        fatoms, fbonds, agraph, bgraph = graph_inputs

        # nei_input_h is size [# bonds, hidden_size]
        nei_input_h = self.W_message_i(fbonds)
        # message_h is size [# bonds, hidden_size]
        message_h = nn.ReLU()(nei_input_h)

        for i in range(self.args.depth - 1):
            # nei_message_h is [# bonds, # max neighbors, hidden_size]
            nei_message_h = self.index_select_nei(
                input=message_h,
                dim=0,
                index=bgraph)

            # Sum over the nieghbors, now [# bonds, hidden_size]
            nei_message_h = nei_message_h.sum(dim=1)
            if self.args.no_share:
                nei_message_h = self.W_message_h[i](nei_message_h)
            else:
                nei_message_h = self.W_message_h(nei_message_h)  # Shared weights

            message_h = nn.ReLU()(nei_input_h + nei_message_h)

        # Collect the neighbor messages for atom aggregation
        nei_message_h = self.index_select_nei(
            input=message_h,
            dim=0,
            index=agraph,
        )
        # Aggregate the messages
        nei_message_h = nei_message_h.sum(dim=1)
        atom_input = torch.cat([fatoms, nei_message_h], dim=1)
        atom_input = self.dropout(atom_input)

        atom_h = nn.ReLU()(self.W_message_o(atom_input))
        return atom_h


from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.contrib.auth.mixins import (
    LoginRequiredMixin,
    UserPassesTestMixin
)
from django.views.generic import (
    ListView,
    DetailView,
    CreateView,
    UpdateView,
    DeleteView
)
from .models import Post

import operator
from django.db.models import Q

####### IMPORTS FOR SPOTIFY API

import os
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

SPOTIFY_ID = os.environ.get('SPOTIFY_ID')
SPOTIFY_SECRET = os.environ.get('SPOTIFY_SECRET')

ccm = SpotifyClientCredentials(client_id=SPOTIFY_ID,
                                client_secret=SPOTIFY_SECRET)

#######

# Create your views here.
def home(request):
    context = {
        'posts': Post.objects.all(),
    }
    return render(request, 'blog/home.html', context)

class PostListView(ListView):
    model = Post
    template_name = 'blog/home.html'
    context_object_name = 'posts'
    ordering = ['-date']
    paginate_by = 5

class UserPostListView(ListView):
    model = Post
    template_name = 'blog/user_posts.html'
    context_object_name = 'posts'
    paginate_by = 5

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date')

class SearchPostListView(ListView):
    model = Post
    template_name = 'blog/post_search.html'
    context_object_name = 'posts'
    paginate_by = 5

    def get_queryset(self):
        query = self.request.GET.get('q')

        if query:
            query_words = query.split(',')
            query_words = [word.lower().strip() for word in query_words]

            # get all posts with at least one of the searched tags
            initial = Post.objects.filter(tags__slug__in=query_words).order_by('-date')
            result = [post for post in initial]
            # filter posts if they don't contain all the tags
            for post in initial:
                for tag in query_words:
                    if tag not in post.tags.slugs():
                        result.remove(post)

            result = list(set(result)) #remove duplicates
            return result
        else:
            return Post.objects.all()

class PostDetailView(DetailView):
    model = Post

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    fields = ['link', 'rating', 'content', 'tags']

    def form_valid(self, form):
        spotify = spotipy.Spotify(client_credentials_manager=ccm)

        form.instance.author = self.request.user

        url = form.cleaned_data['link']
        if "artist/" in url:
            results = spotify.artist(url)
            form.instance.category = results["type"]
            form.instance.title = results["name"]
            form.instance.artist = results["name"]
        elif "album/" in url:
            results = spotify.album(url)
            form.instance.category = results["type"]
            form.instance.title = results["name"]
            form.instance.artist = results["artists"][0]["name"]
        elif "track/" in url:
            results = spotify.track(url)
            form.instance.category = results["type"]
            form.instance.title = results["name"]
            form.instance.artist = results["artists"][0]["name"]

        if "embed/" not in url:
            form.instance.link = url[:25] + "embed/" + url[25:]

        #######
        form.save()
        form.instance.tags.add(form.instance.title,
                                form.instance.artist,
                                form.instance.category)
        #######

        return HttpResponseRedirect(form.instance.get_absolute_url())

class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['link', 'rating', 'content', 'tags']

    def form_valid(self, form):
        form.instance.author = self.request.user

        if "embed/" not in form.cleaned_data['link']:
            form.instance.link = form.cleaned_data['link'][:25] + "embed/" + form.cleaned_data['link'][25:]

        return super().form_valid(form)

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False

class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    success_url = "/"

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False

def about(request):
    return render(request, 'blog/about.html', {'title': 'About'})


from typing import Dict, Any, List, Set, Optional

class FhirMetaDataParser:
    """
    Parses FHIR metadata

    """

    @staticmethod
    def get_resource_types_from_metadata(metadata: Dict[str, Any]) -> List[str]:
        """
        Gets resource types present in the metadata

        :param metadata:
        """
        resource_types: Set[str] = set()
        rest_entry: Dict[str, Any]
        for rest_entry in metadata.get("rest", []):
            for resource in rest_entry.get("resource", []):
                resource_type: Optional[str] = resource.get("type")
                if resource_type is not None:
                    resource_types.add(resource_type)

        return list(resource_types)

    @staticmethod
    def get_fir_version_from_metadata(metadata: Dict[str, Any]) -> Optional[str]:
        """
        Gets FHIR version from metadata

        :param metadata:
        """
        fhir_version: Optional[str] = metadata.get("fhirVersion")
        return fhir_version


from typing import List
import pdb

solution_json = {
    "date": "2021/5/9",
    "design": 6,
    "coding": 4,
    "runtime": "44 ms",
    "memory": "15.5 MB"
}

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def swapPairs(self, head):
        swapPairs(head)
        return head

def swapPairs(node):
	if node is None:
		return

	if node.next is None:
		return

	n0 = node
	n1 = node.next
	n0.val, n1.val = n1.val, n0.val

	n2 = node.next.next
	swapPairs(n2)


class Employee:
    id = 11
    name = "Aparna"
    def display(self):
        print("ID  ",self.id)
        print(" name : ",self.name)
# create object
emp = Employee()
emp1 = Employee()

emp.display()
#delete property of object
del emp.id

#delete object itself
del emp
print(id(emp))
print(id(emp1))


import pathlib
import unittest
import unittest.mock

import gpu_affinity

def get_mock_open(files: dict[str, str]):
    def open_mock(filename, *args, **kwargs):
        for expected_filename, content in files.items():
            if filename == expected_filename:
                return unittest.mock.mock_open(read_data=content).return_value
        raise FileNotFoundError('(mock) Unable to open {filename}')

    return unittest.mock.MagicMock(side_effect=open_mock)

def thread_siblings_lists_fnames(N):
    return [
        f'/sys/devices/system/cpu/cpu{i}/topology/thread_siblings_list'
        for i in range(N)
    ]

class CheckSocketAffinities(unittest.TestCase):
    def test_disjoint(self):
        affinities = [[0, 1, 2, 3], [4, 5, 6, 7]]
        gpu_affinity.gpu_affinity.check_affinities(affinities)

    def test_identical(self):
        affinities = [[0, 1, 2, 3], [0, 1, 2, 3]]
        gpu_affinity.gpu_affinity.check_affinities(affinities)

    def test_overlap(self):
        affinities = [[0, 1, 2, 3], [1, 2, 3]]
        with self.assertRaises(gpu_affinity.GPUAffinityError):
            gpu_affinity.gpu_affinity.check_affinities(affinities)

    def test_single(self):
        affinities = [[0, 1, 2, 3]]
        gpu_affinity.gpu_affinity.check_affinities(affinities)

class GetThreadSiblingsList(unittest.TestCase):
    def test_missing(self):
        pass

    @unittest.mock.patch(
        'builtins.open',
        get_mock_open(
            dict(zip(thread_siblings_lists_fnames(2), ['0,1', '2,3']))
        ),
    )
    @unittest.mock.patch.object(
        pathlib.Path, 'glob', new=lambda x, y: thread_siblings_lists_fnames(2)
    )
    def test_mocked_simple(self):
        siblings_list = gpu_affinity.gpu_affinity.get_thread_siblings_list()
        self.assertEqual(siblings_list, [(0, 1), (2, 3)])

    @unittest.mock.patch(
        'builtins.open',
        get_mock_open(
            dict(zip(thread_siblings_lists_fnames(2), ['0,1', '0,1']))
        ),
    )
    @unittest.mock.patch.object(
        pathlib.Path, 'glob', new=lambda x, y: thread_siblings_lists_fnames(2)
    )
    def test_mocked_duplicates(self):
        siblings_list = gpu_affinity.gpu_affinity.get_thread_siblings_list()
        self.assertEqual(siblings_list, [(0, 1)])

class BuildThreadSiblingsDict(unittest.TestCase):
    def test_1_item(self):
        siblings_list = [(0, 1)]
        siblings_dict = gpu_affinity.gpu_affinity.build_thread_siblings_dict(
            siblings_list
        )
        self.assertEqual(siblings_dict, {0: (0, 1), 1: (0, 1)})

    def test_2_item(self):
        siblings_list = [(0, 1), (2, 3)]
        siblings_dict = gpu_affinity.gpu_affinity.build_thread_siblings_dict(
            siblings_list
        )
        self.assertEqual(
            siblings_dict, {0: (0, 1), 1: (0, 1), 2: (2, 3), 3: (2, 3)}
        )

class GroupListByDict(unittest.TestCase):
    def test_simple_1(self):
        siblings_dict = {0: (0, 1), 1: (0, 1)}
        siblings_key = lambda x: siblings_dict.get(x, (x,))
        affinity = [0, 1]
        grouped = gpu_affinity.gpu_affinity.group_list_by_key(
            affinity, siblings_key
        )
        self.assertEqual(grouped, [(0, 1)])

    def test_simple_2(self):
        siblings_dict = {0: (0, 1), 1: (0, 1), 2: (2, 3), 3: (2, 3)}
        siblings_key = lambda x: siblings_dict.get(x, (x,))
        affinity = [0, 1, 2, 3]
        grouped = gpu_affinity.gpu_affinity.group_list_by_key(
            affinity, siblings_key
        )
        self.assertEqual(grouped, [(0, 1), (2, 3)])

    def test_missing_dict(self):
        siblings_dict = {}
        siblings_key = lambda x: siblings_dict.get(x, (x,))
        affinity = [0, 1]
        grouped = gpu_affinity.gpu_affinity.group_list_by_key(
            affinity, siblings_key
        )
        self.assertEqual(grouped, [(0,), (1,)])

class UngroupAffinities(unittest.TestCase):
    def test_simple_all_logical(self):
        affinities = [[((0, 1),), ((2, 3),)]]
        ungrouped = gpu_affinity.gpu_affinity.ungroup_all_and_check_count(
            affinities,
            gpu_affinity.Scope.NODE,
            gpu_affinity.Multithreading.ALL_LOGICAL,
        )
        self.assertEqual(ungrouped, [[0, 1, 2, 3]])

    def test_simple_single_logical(self):
        affinities = [[((0, 1),), ((2, 3),)]]
        ungrouped = gpu_affinity.gpu_affinity.ungroup_all_and_check_count(
            affinities,
            gpu_affinity.Scope.SOCKET,
            gpu_affinity.Multithreading.SINGLE_LOGICAL,
        )
        self.assertEqual(ungrouped, [[0, 2]])

    def test_unknown_mode(self):
        affinities = [[((0, 1),), ((2, 3),)]]
        with self.assertRaises(gpu_affinity.GPUAffinityError):
            gpu_affinity.gpu_affinity.ungroup_all_and_check_count(
                affinities, 'node', 'does_not_exist'
            )