#!/usr/bin/python3

from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import sys

HTTP_PORT_DEFAULT = 20360

class FileServer(BaseHTTPRequestHandler):

    # file server to process the proxy request based on datarate and send back image in response
    def do_GET(self):

        # extract datarate from header
        datarate = int(self.headers.get('datarate'))

        # spliting resource url into their name and extension
        name, ext = os.path.splitext(self.path)

        # depending on the extension type, prcoess the client request
        if ext == '.jpg' or ext == '.jpeg':

            largeimage = name + '_' + 'large' + ext
            mediumimage = name + '_' + 'medium' + ext
            smallimage = name + '_' + 'small' + ext

            self.send_response(200)
            self.send_header('Content-type', 'text/x-binary')
            self.end_headers()

            if datarate <= datarate_low:
                with open('./' + smallimage, 'rb') as f:
                    self.wfile.write(f.read())
            elif datarate_low < datarate <= datarate_high:
                with open('./' + mediumimage, 'rb') as f:
                    self.wfile.write(f.read())
            else:
                with open('./' + largeimage, 'rb') as f:
                    self.wfile.write(f.read())

        # respond client with customised html file
        elif ext == '.html':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()

            file = open('.' + self.path, 'r')
            response_text = file.read()
            file.close()

            response_text = response_text.replace(
                "<body>", "<body>Ya you are right place")
            self.wfile.write(response_text.encode('utf-8'))

# initialize to start
try:
    if len(sys.argv) == 4:
        http_port = int(sys.argv[1])
        datarate_low = int(sys.argv[2])
        datarate_high = int(sys.argv[3])
    else:
        http_port = HTTP_PORT_DEFAULT
    # Create HTTP server with customized class
    server = HTTPServer(('', http_port), FileServer)
    print('Started httpserver on port', http_port)

    # Wait forever for incoming HTTP requests
    server.serve_forever()

except KeyboardInterrupt:
    print('^C received, shutting down the web server')
    server.socket.close()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
from xgboost import XGBClassifier

# 불러오기
train = pd.read_csv('../data/csv/Dacon2/data/train.csv',header = 0)
test = pd.read_csv('../data/csv/Dacon2/data/test.csv',header = 0)
# ==============================그림보기==========================
# 인덱스
idx = 318
# 트레인 데이터의 인덱스 길이만큼의 데이터에 대해 0번 컬럼의 값을 28,28로 쉐이프하여 이미지로 초기화
img = train.loc[idx, '0':].values.reshape(28, 28).astype(int)
# 트레인 데이터의 인덱스 길이만큼 타겟을 디지트로 초기화
digit = train.loc[idx, 'digit']
# 트레인 데이터의 인덱스 길이만큼 레터를 레터로 초기화
letter = train.loc[idx, 'letter']

# 타이틀을 인덱스, 디지트, 레터로 함
plt.title('Index: %i, Digit: %s, Letter: %s'%(idx, digit, letter))
# 인덱스 번째의 쉐이프한 이미지를 이미지로 출력
plt.imshow(img)
# 보기
plt.show()
# ==============================데이터 전처리==========================

# 트래인의 아이디, 디지트, 레터 컬럼을 제거한 나머지 컬럼의 값을 축변경하여 초기화
x_train = train.drop(['id', 'digit', 'letter'], axis=1).values

from sklearn.decomposition import PCA

pca = PCA()
x2 = pca.fit_transform(x_train)
print(x2.shape)                 # (70000, 784)

# 컬럼을 압축한 컬럼의 변화 비율을 확인
# pca_EVR = pca.explained_variance_ratio_
# print(pca_EVR)
# print(sum(pca_EVR))             # 1.000000000000002

# pca = PCA(130)
# pca.fit(x_train)
# cumsum = 누적합계
cumsum = np.cumsum(pca.explained_variance_ratio_)
print("누계 :", cumsum)
d = np.argmax(cumsum >= 0.99)+1
print("cumsum >= 0.95", cumsum >= 0.95)
print("d :", d)

# 시엔엔 쉐이프로 변경
x_train = x_train.reshape(-1, 28,28,1)
# 0~1 사이로 값바꿈
x_train = x_train/255

# 와이값은 트레인의 디지트 컬럼만을 참조함
y = train['digit']
# 와이 트레인은 와이의 길이를 행으로, 와이의 중복값을 제거한 나머지 값의 길이를 열로하는 배열을 만들고
# 0으로 초기화
# 아마 원핫인코딩을 이렇게 하려하는 듯 왜냐하면 와이값의 유니크를 컬럼으로 잡고 와이 길이만큼 0으로 준걸 보면
# 나중에 유니크 길이만큼 1을 넣으려하는 듯
y_train = np.zeros((len(y), len(y.unique())))

# 와이트래인을 원 핫 인코딩함
for i, digit in enumerate(y):
    y_train[i, digit] = 1

from sklearn.model_selection import train_test_split

x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, train_size = 0.5, shuffle = True, random_state=1)

#  모델을 정의하고 변수 트래인을 인자로 받음
def create_cnn_model(x_train):

    # 인풋 쉐이프 레이어
    inputs = tf.keras.layers.Input(x_train.shape[1:])
    # 아웃풋을 전처리하여 다음레이어에 전달해주는 엑티베이션 레이어로 시엔엔 모델의 각층에 삽입해 explode또는 vanish 을 잡음.
    bn = tf.keras.layers.BatchNormalization()(inputs)
    conv = tf.keras.layers.Conv2D(128, kernel_size=5, strides=1, padding='same', activation='relu')(bn)
    bn = tf.keras.layers.BatchNormalization()(conv)
    conv = tf.keras.layers.Conv2D(128, kernel_size=2, strides=1, padding='same', activation='relu')(bn)
    pool = tf.keras.layers.MaxPooling2D((2, 2))(conv)

    bn = tf.keras.layers.BatchNormalization()(pool)
    conv = tf.keras.layers.Conv2D(256, kernel_size=2, strides=1, padding='same', activation='relu')(bn)
    bn = tf.keras.layers.BatchNormalization()(conv)
    conv = tf.keras.layers.Conv2D(256, kernel_size=2, strides=1, padding='same', activation='relu')(bn)
    pool = tf.keras.layers.MaxPooling2D((2, 2))(conv)

    flatten = tf.keras.layers.Flatten()(pool)

    bn = tf.keras.layers.BatchNormalization()(flatten)
    dense = tf.keras.layers.Dense(1000, activation='relu')(bn)

    bn = tf.keras.layers.BatchNormalization()(dense)
    outputs = tf.keras.layers.Dense(10, activation='softmax')(bn)

    model = tf.keras.models.Model(inputs=inputs, outputs=outputs)

    return model
# ==================================================================================================

from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
modelpath = "../data/modelCheckPoint/Dacon2_{epoch:02d}_{val_accuracy:.2f}.hdf5"  # 가중치 저장 위치
early_stopping = EarlyStopping(monitor='val_accuracy', patience=30, mode='max')
cp = ModelCheckpoint(filepath=(modelpath), monitor='val_accuracy', save_best_only=True, mode='auto')
# 이거 추가됨
# 모니터 벨류가 patience 회 개선이 없으면 러닝레이factor를 % 만큼 감소시켜준다.
reduce_lr = ReduceLROnPlateau(monitor='val_accuracy',patience=10, factor=0.5, verbose=1)
# # ==================================================================================================

model = create_cnn_model(x_train)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=300,validation_data=(x_val,y_val), batch_size=256 ,callbacks=[early_stopping,cp,reduce_lr])

x_test = test.drop(['id', 'letter'], axis=1).values
print(x_test.shape)
x_test = x_test.reshape(-1, 28, 28, 1)
x_test = x_test/255

submission = pd.read_csv('../data/csv/Dacon2/data/submission.csv')
submission['digit'] = np.argmax(model.predict(x_test), axis=1)
print(submission.head())

# submission.to_csv('../data/csv/Dacon2/data/baseline.csv', index=False)


import socket, time
from threading import Lock

def scoket_port(ip, port):
  try:
    if port >= 65535:
      print('端口扫描结束')
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = s.connect_ex((ip, port))
    if result == 0:
      lock = Lock()
      lock.acquire()
      print('端口被占用')
      lock.release()
  except Exception as e:
    print(str(e))

if __name__ == "__main__":
   scoket_port('192.168.30.31', 27018)


import numpy as np
import torch
import sklearn.datasets
import sys
sys.path.append('../../utils_network/')
from utils_network.data import *
from utils_network.mymodel import *
from params import *
from torch.utils.data import DataLoader,TensorDataset
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import yaml
if __name__=='__main__':

    feature_nums,sample_nums=Data.get_feature_sample_nums_from_yaml(yaml_save_path)

    if generate_mode==0:
        model=simple_2classification(feature_nums=feature_nums,feature_expand=True,correlation=correlation)
    else:
        model=simple_2classification(feature_nums=feature_nums)
    #set optimizer: SGD = Stochastic Gradient Descent, choose mini-batch of X randomly to caculate gradient
    optimizer=torch.optim.SGD(model.parameters(),lr=learning_rate)
    #choose loss function, Binary Cross Entropy Loss
    criterion=torch.nn.BCELoss()

    dataset=np.load(data_save_path)
    X=dataset['X']
    y=dataset['y']

    if model.feature_expand:
        X=Data.feature_expand(X,feature_nums,correlation)

    X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=10)
    X_train,X_test,y_train,y_test=map(lambda data: torch.from_numpy(data).to(dtype=torch.float32),[X_train,X_test,y_train,y_test])
    y_train,y_test=map(lambda data: data.reshape(-1,1),[y_train,y_test])
    dataset=TensorDataset(X_train,y_train)

    #if shuffle, then loss will go down apparently, but better to set True
    dataloader=DataLoader(dataset,batch_size,shuffle=True)

    for epoch in range(epochs):
        for batch_data in dataloader:
            sample_input,sample_labels=batch_data
            optimizer.zero_grad()

            outputs =model(sample_input)

            loss = criterion(outputs, sample_labels)

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

        if (epoch + 1) % 100 == 0:
            print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item()}')

    print(model.state_dict())
    print(f'model save to {weights_save_path}')
    torch.save(model.state_dict(),weights_save_path)

with torch.no_grad():
    model.eval()
    predictions=model(X_test)

binary_predictions=(predictions>=confidence).float()

accuracy=accuracy_score(y_test.numpy(),binary_predictions.numpy())
print(f'Accuracy: {accuracy}')


"""
@author: Tomas Belskis
@exercise: Chapter 4 exercise 1
@description: Write a void (non-fruitful) function to draw a square.
              Use it in a program to draw the image shown below.
              Assume each side is 20 units.
              (Hint: notice that the turtle has already moved away from
              the ending point of the last square when the program ends.)
"""

import turtle

def make_window(color, title):
    w = turtle.Screen()
    w.bgcolor(color)
    w.title(title)
    return w

def make_turtle(color, size, pos):
    t = turtle.Turtle()
    t.speed(0)
    t.color(color)
    t.pensize(size)
    t.penup()
    t.setpos(pos)
    t.pendown()
    return t

def draw_square(t, s):
    """
    :param t: turtle
    :param s: size
    :return: none
    """
    for i in range(4):
        t.forward(s)
        t.left(90)

def draw_number_of_squares(n, size, distance):

 for i in range(n):
    draw_square(t, size)
    t.penup()
    t.forward(distance+size)
    t.pendown()

w = make_window("black", "Chapter 4 exercise 1")
t = make_turtle("green",5,(-200,0))

draw_number_of_squares(10, 20, 20)

w.mainloop()


'''
 - Exercicío 5.11 - Escreva um programa que pergunte o depósito inicial e a taxa de juros de uma poupança. Exiba os valores mês a mês
para os 24 primeiros meses. Escreva o total ganho com juros nesse período

'''
n = 1  # Contador
valor_juros = 0 #Acumulador
deposito_inicial = float(input("Deposito inicial: ")) # Recebendo o depósito inicial
taxa_juros = float(input("Taxa de juros (%): ")) # Taxa de juros ao mês
print("\n")
while n <= 24 : # Enquanto n for menor ou igual a 6, faça
    valor_juros = deposito_inicial * (1 + (taxa_juros / 100)) ** n # Calculando
    print("{}º mês, R$ {:5.2f}" .format(n, valor_juros - deposito_inicial)) #Exibe o valor que a poupança rendeu em cada mês
    n = n + 1 # Incrementa n
print("\nValor obtido com juros, R$ {:5.2f}" .format(valor_juros)) # Exibe o valor que o juros rendeu durante o período


import requests
import base64

def get_access_token(client_id, client_secret):
    client_credentials = f"{client_id}:{client_secret}"
    client_credentials_base64 = base64.b64encode(client_credentials.encode())

    token_url = 'https://accounts.spotify.com/api/token'
    headers = {
        'Authorization': f'Basic {client_credentials_base64.decode()}'
    }
    data = {
        'grant_type': 'client_credentials'
    }
    response = requests.post(token_url, data=data, headers=headers)

    if response.status_code == 200:
        return response.json()['access_token']
    else:
        print("Error obtaining access token.")
        return None


from lms import views
from django.urls import path
from django.contrib.auth.decorators import login_required
from lms import courses_datatables_views
from lms import subjects_datatables_views
from lms import topics_datatables_views
from lms import payments_datatables_views
from lms import users_subjects_datatables_views
from lms import users_topics_datatables_views
from lms import attendance_datatables_views

urlpatterns = [
	path('', views.index, name='index'),
	path('change-password', views.change_password, name='change_password'),
	path('home', views.home, name='home'),
	path('user-registration', views.user_registration, name='user_registration'),
	path('login', views.login_view, name='login'),
	path('logout', views.logout_view, name='logout'),
	path('add-course', views.add_course, name='add-course'),
	path('add-subject', views.add_subject, name='add-subject'),
	path('add-topic', views.add_topic, name='add-topic'),
	path('add-payment', views.add_payment_details, name='add_payment'),
	path('add-attendance', views.add_attendance, name='add-attendance'),
	path('view-payments', views.view_payments, name='view-payments'),
	path('view-payments-dt', login_required(payments_datatables_views.PaymenttListDatatable.as_view()),
		name='view-payments-dt'),
	path('view-courses', views.view_courses, name='view-courses'),
	path('view-courses-dt', login_required(courses_datatables_views.CourseListDatatable.as_view()),
		name='view-courses-dt'),
	path('view-subjects', views.view_subjects, name='view-subjects'),
	path('view-subjects-dt', login_required(subjects_datatables_views.SubjectListDatatable.as_view()),
		name='view-subjects-dt'),
	path('view-topics', views.view_topics, name='view-topics'),
	path('view-topics-dt', login_required(topics_datatables_views.TopicListDatatable.as_view()),
		name='view-topics-dt'),
	path('view-attendance', views.view_attendance, name='view-attendance'),
	path('view-attendance-dt', login_required(attendance_datatables_views.AttendanceListDatatable.as_view()),
		name='view-attendance-dt'),
	path('delete-course/<int:course_id>', views.delete_course, name='delete_course'),
	path('delete-subject/<int:subject_id>', views.delete_subject, name='delete_subject'),
	path('delete-topic/<int:topic_id>', views.delete_topic, name='delete_topic'),
	path('edit-course/<int:course_id>', views.edit_course, name='edit_course'),
	path('edit-subject/<int:subject_id>', views.edit_subject, name='edit_subject'),
	path('edit-topic/<int:topic_id>', views.edit_topic, name='edit_topic'),
	path('edit-payment/<int:payment_id>', views.edit_payment_details, name='edit-payment'),
	path('edit-attendance/<int:attendance_id>', views.edit_attendance, name='edit-attendance'),

	path('add-user-course', views.add_user_course, name='add-user-course'),
	path('user-subjects', views.user_view_subject, name='user-subjects'),
	path('view-user-subjects-dt', login_required(users_subjects_datatables_views.UserSubjectListDatatable.as_view()),
		name='view-user-subjects-dt'),
	path('user-topics', views.user_view_topic, name='user-topics'),
	path('view-user-topics-dt', login_required(users_topics_datatables_views.UserTopicListDatatable.as_view()),
		name='view-user-topics-dt'),
	path('topic/<int:topic_id>', views.topic_page, name='topic'),
	path('user-make-payment', views.user_make_payment, name='user-make-payment'),
]


import argparse
import base64
import gzip
import hashlib
import lzma
import os
import shutil
import subprocess
import tarfile
import zipfile
from typing import AnyStr, Dict, Iterable, Optional

from cockpit import __version__

VERSION = __version__ or '0'
PACKAGE = f'cockpit-{VERSION}'
TAG = 'py3-none-any'

def find_sources(*, srcpkg: bool) -> Iterable[str]:
    try:
        subprocess.check_call(['vendor/checkout'], stdout=2)       # Needed for git builds...
    except FileNotFoundError:                                       # ...but not present in tarball...
        pass                                                        # ...and not needed either, because...
    assert os.path.exists('src/cockpit/_vendor/ferny/__init__.py')  # ...the code should exist there already.

    if srcpkg:
        yield from (
            'pyproject.toml',
            'src/build_backend.py',
        )

    for path, _dirs, files in os.walk('src', followlinks=True):
        if '__init__.py' in files:
            yield from [os.path.join(path, file) for file in files]

def copy_sources(distdir: str) -> None:
    for source in find_sources(srcpkg=True):
        destination = os.path.join(distdir, source)
        os.makedirs(os.path.dirname(destination), exist_ok=True)
        shutil.copy(source, destination)

def build_sdist(sdist_directory: str,
                config_settings: Optional[Dict[str, object]] = None) -> str:
    del config_settings
    sdist_filename = f'{PACKAGE}.tar.gz'
    # We do this manually to avoid adding timestamps.  See https://bugs.python.org/issue31526
    with gzip.GzipFile(f'{sdist_directory}/{sdist_filename}', mode='w', mtime=0) as gz:
        with tarfile.open(fileobj=gz, mode='w|', dereference=True) as sdist:
            for filename in find_sources(srcpkg=True):
                sdist.add(filename, arcname=f'{PACKAGE}/{filename}', )
    return sdist_filename

def build_wheel(wheel_directory: str,
                config_settings: Optional[Dict[str, object]] = None,
                metadata_directory: Optional[str] = None) -> str:
    del config_settings, metadata_directory
    wheel_filename = f'{PACKAGE}-{TAG}.whl'
    distinfo = {
        'WHEEL': [
            'Wheel-Version: 1.0',
            'Generator: cockpit build_backend',
            'Root-Is-Purelib: true',
            f'Tag: {TAG}',
        ],
        'METADATA': [
            'Metadata-Version: 2.1',
            'Name: cockpit',
            f'Version: {VERSION}',
        ],
        'entry_points.txt': [
            '[console_scripts]',
            'cockpit-askpass = cockpit._vendor.ferny.interaction_client:main',
            'cockpit-bridge = cockpit.bridge:main',
        ],
    }

    with zipfile.ZipFile(f'{wheel_directory}/{wheel_filename}', 'w') as wheel:
        def write(filename: str, data: AnyStr) -> None:
            # we do this manually to avoid adding timestamps
            wheel.writestr(zipfile.ZipInfo(filename), data)

        def beipack_self(main: str, args: str = '') -> bytes:
            from cockpit._vendor.bei import beipack
            contents = {name: wheel.read(name) for name in wheel.namelist()}
            pack = beipack.pack(contents, main, args=args).encode('utf-8')
            return lzma.compress(pack, preset=lzma.PRESET_EXTREME)

        def write_distinfo(filename: str, lines: Iterable[str]) -> None:
            write(f'{PACKAGE}.dist-info/{filename}', ''.join(f'{line}\n' for line in lines))

        def record_lines() -> Iterable[str]:
            for info in wheel.infolist():
                digest = hashlib.sha256(wheel.read(info.filename)).digest()
                b64_digest = base64.urlsafe_b64encode(digest).rstrip(b'=').decode('ascii')
                yield f'{info.filename},sha256={b64_digest},{info.file_size}'
            yield f'{PACKAGE}.dist-info/RECORD,,'

        for filename in find_sources(srcpkg=False):
            with open(filename, 'rb') as file:
                write(os.path.relpath(filename, start='src'), file.read())

        write('cockpit/data/cockpit-bridge.beipack.xz', beipack_self('cockpit.bridge:main', 'beipack=True'))

        for filename, lines in distinfo.items():
            write_distinfo(filename, lines)
        write_distinfo('RECORD', record_lines())

    return wheel_filename

def main() -> None:
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--copy', action='store_true')
    group.add_argument('--sdist', action='store_true')
    group.add_argument('--wheel', action='store_true')
    parser.add_argument('srcdir')
    parser.add_argument('destdir')
    args = parser.parse_args()

    # We have to chdir() for PEP 517, so make sure dest is absolute
    destdir = os.path.abspath(args.destdir)
    os.chdir(args.srcdir)

    os.makedirs(destdir, exist_ok=True)

    if args.copy:
        copy_sources(destdir)
    elif args.sdist:
        print(os.path.join(destdir, build_sdist(destdir)))
    else:
        print(os.path.join(destdir, build_wheel(destdir)))

if __name__ == '__main__':
    main()


qtdde = int(input('Quantos termos você quer mostrar? '))
k = 1
while k <= qtdde:
    if k == 1:
        n1 = 1
        print(n1, end=' -> ')
    elif k == 2:
        n2 = 1
        print(n2, end=' -> ')
    else:
        n1, n2 = n2, n1+n2
        print(n2, end=' -> ')
    k += 1
print('FIM')


from tempfile import NamedTemporaryFile
import shutil
import csv

outname= 'traj.csv'

def prepare_traj(filetraj):

    filename = filetraj
    tempfile = NamedTemporaryFile(mode='w', delete=False, newline='')
    outname= 'traj.csv'
    fields = ['TRAJ_ID','TIMESTAMP','LON', 'LAT', 'TAXI', 'DATE']

    with open(filename, 'r',newline='') as csvfile, tempfile:
        reader = csv.DictReader(csvfile, fieldnames=fields)
        writer = csv.DictWriter(tempfile, fieldnames=fields)
        for row in reader:
            # print('updating row', row['TRAJ_ID'])
            taxiid = row['TRAJ_ID']
            datetime = row['TIMESTAMP']
            partitionkey = datetime.replace("-","")
            partitionkey_split = partitionkey.split(" ")
            splitkey = partitionkey_split[1].split(":")
            quarter = int(splitkey[1])//15
            if (quarter == 0):
                time = '00'
            if (quarter == 1):
                time = '15'
            if (quarter == 2):
                time = '30'
            if (quarter == 3):
                time = '45'
            trajid = taxiid +'_'+partitionkey_split[0]+"_"+ splitkey[0]+'_'+time
            # trajid = taxiid+splitkey[0]
            # split = datetime.split(" ")
            # date = split[0]
            # times = split[1].split(":")
            # hour = times[0]

            row['TRAJ_ID'] = trajid
            row['TAXI'] = taxiid
            row['DATE'] = partitionkey_split[0]
            row = {
            'TRAJ_ID': row['TRAJ_ID'],
            'TIMESTAMP': row['TIMESTAMP'],
            'LON': row['LON'],
            'LAT': row['LAT'],
            'TAXI': row['TAXI'],
            'DATE': row['DATE']}
            writer.writerow(row)
    shutil.move(tempfile.name, filename)

    with open(filename, 'r') as in_file:
        stripped = (line.strip() for line in in_file)
        lines = (line.split(",") for line in stripped if line)
        with open(outname, 'w',newline='') as out_file:
            writer = csv.writer(out_file)
            writer.writerow(('TRAJ_ID','TIMESTAMP','LON', 'LAT','TAXI','DATE'))
            writer.writerows(lines)


import tkinter as tk
from tkinter import ttk

janela = tk.Tk()

lista = ['Domingo','Segunda', 'Terça', 'Quarta']
cbx = ttk.Combobox(janela, values=lista, state='readonly')
#cbx.set('Segunda')
cbx.current(2)
cbx.pack()

janela.mainloop()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# File : neural_network.py
# Created by Anthony Giraudo and Clement Sebastiao the 30/10/2020

# Modules

import matplotlib.pyplot as plt
from copy import deepcopy
from jax import grad
import jax.numpy as np
import numpy

# Functions

def sigma(x):
    return 1/(1+np.exp(-x))

def load_network(path, n):
    W = []
    for l in range(1, n+1):
        W += [np.array(numpy.loadtxt(path+f"W{l}", ndmin=2))]
        print(W[-1].shape)
    return NeuralNetwork(W)

def initialise_W(M, training_set, renormalisation):
    assert M[0] == training_set.shape[1]
    assert M[-1] == 1
    x = training_set.T
    W = []
    for i in range(1, len(M)):
        W_T = np.array(numpy.random.uniform(size=(M[i], M[i-1])))
        z_T = W_T @ x
        mean = np.mean(z_T, axis=1)
        std = np.std(z_T, axis=1)
        W += [np.append(W_T, np.expand_dims(-mean, axis=1), axis=1) / (np.expand_dims(std, axis=1) * renormalisation)]
        x = sigma(W[-1] @ np.append(x, np.ones((1, x.shape[1])), axis=0))
    return W

def perfect_W(M0):
    return [np.append(5 / M0 * np.ones((1, M0)), np.array([[0.]]), axis=1), np.array([[20., -15.], [-20., 5.]]),
            np.array([[-10., -10., 5.]])]

class NeuralNetwork():
    def __init__(self, initial_W):
        """
        :param initial_W: list of n arrays W_l (l=1->n) with shape (M_l, M_{l-1}+1)
        and W_n must have shape (1, M_{n-1}+1)
        """
        M0 = W[0].shape[0] - 1
        self.W = deepcopy(initial_W)
        self.n = len(initial_W)

    @staticmethod
    def check_W(W, M0):
        assert W[0].ndim == 2
        assert W[0].shape[1] == M0 + 1
        for lm1, Wl in enumerate(W[1:]):
            assert Wl.ndim == 2
            assert Wl.shape[1] == W[lm1].shape[0] + 1
        assert W[-1].shape[0] == 1

    @staticmethod
    def classification_func(input_vectors, *args):
        """
        :param input_vectors:
        :param args: W_1, W_2, ..., W_n
        :return: predicted probability for the input vector to correspond to a configuration at T>Tc
        """
        # TODO: tester avec un seul input_vector
        M0 = input_vectors.shape[1] if input_vectors.ndim == 2 else input_vectors.shape[0]
        NeuralNetwork.check_W(args, M0)
        x = input_vectors.T
        for W_l in args:
            xp = np.append(x, np.ones((1, x.shape[1])), axis=0)
            z = W_l @ xp
            x = sigma(z)
        return x[0]

    @staticmethod
    def cross_entropy_func(training_set, labels, *args):
        """
        Cross entropy function for automatic differentation
        :param args: W_1, W_2, ..., W_n
        :return:
        """
        M0 = training_set.shape[1]
        NeuralNetwork.check_W(args, M0)
        prediction = NeuralNetwork.classification_func(training_set, *args)

        return np.mean(- (labels * np.log(prediction) + (1 - labels) * np.log(1 - prediction)))

    def get_cross_entropy(self, training_set, labels):
        """
        :param training_set: array of training vectors with shape (#samples, M_0)
        :param labels: 1D array of labels associated to the training vectors (either 0 or 1)
        :return:
        """
        return self.cross_entropy_func(training_set, labels, *self.W)

    def get_classification(self, input_vectors):
        """
        :param input_vectors: array of input vectors with shape (#vectors, M_0)
        :return:
        """
        return self.classification_func(input_vectors, *self.W)

    def get_grad(self, training_set, labels):
        """
        :param training_set: array of training vectors with shape (#samples, M_0)
        :param labels: 1D array of labels associated to the training vectors (either 0 or 1)
        :return:
        """
        return grad(NeuralNetwork.cross_entropy_func, argnums=np.arange(2, self.n+2))(training_set, labels, *self.W)

    def training(self, training_set, training_labels, test_set, test_labels, step_size, epsilon=None, nsteps=None):
        if epsilon is None and nsteps is None:
            raise ValueError("At least one parameter must be filled among epsilon and nsteps.")

        cross_entropies = [self.get_cross_entropy(training_set, training_labels)]
        validity_rates = [self.get_validity_rate(test_set, test_labels)]
        i = 0
        while True:
            try:
                grad_W = self.get_grad(training_set, training_labels)
                for l in range(self.n):
                    self.W[l] -= step_size * grad_W[l]
                cross_entropies += [self.get_cross_entropy(training_set, training_labels)]
                validity_rates += [self.get_validity_rate(test_set, test_labels)]
                if i % 10 == 0:
                    print(f"Training step {i}/{nsteps}")
                i += 1
                if nsteps is not None and i > nsteps:
                    break
                if epsilon is not None and cross_entropies[-2] - cross_entropies[-1] < epsilon:
                    break
            except KeyboardInterrupt:
                break
        return cross_entropies, validity_rates

    def save_network(self, path):
        for l, Wl in enumerate(self.W):
            numpy.savetxt(path + f"W{l+1}", Wl)

    def get_validity_rate(self, vectors, labels):
        predicted_proba = self.get_classification(vectors)
        return np.mean((predicted_proba > 0.5) == labels)

# Main
if __name__ == "__main__":
    """
    test_dir = "set1/"
    training_dir = "set2/"
    """
    dir = "set4/"
    path = "nw2/"
    training_percentage = 0.85
    nsteps = 5000
    step_size = 0.00001
    renormalisation = 1/3

    data_set = np.array(numpy.loadtxt(dir + "vectors"))
    labels = np.array(numpy.loadtxt(dir + "classification"))
    shuffled_set = list(zip(data_set, labels))
    numpy.random.shuffle(shuffled_set)
    training_set, training_labels = zip(*shuffled_set[:int(data_set.shape[0]*0.9)])
    training_set = np.array(training_set)
    training_labels = np.array(training_labels)
    test_set, test_labels = zip(*shuffled_set[int(data_set.shape[0]*0.9):])
    test_set = np.array(test_set)
    test_labels = np.array(test_labels)

    """
    training_set = np.array(numpy.loadtxt(training_dir + "vectors"))
    training_labels = np.array(numpy.loadtxt(training_dir + "classification"))
    test_set = np.array(numpy.loadtxt(test_dir + "vectors"))
    test_labels = np.array(numpy.loadtxt(test_dir + "classification"))
    """
    M0 = training_set.shape[1]
    #M = [M0, 200, 300, 100, 1]
    #M = [M0, 100, 150, 50, 100, 1]
    M = [M0, 3, 2, 4, 2, 1]

    W = initialise_W(M, training_set, renormalisation)

    nw = NeuralNetwork(W)
    #nw = load_network(path, 2)

    cross_entropies, validity_rates = nw.training(training_set, training_labels, test_set, test_labels, step_size, nsteps=nsteps)

    nw.save_network(path)

    plt.subplot(121)
    plt.plot(np.arange(1, len(cross_entropies)+1), cross_entropies, label="Fonction de coût")
    plt.legend()
    plt.xlabel("Étapes d'optimisation")
    plt.subplot(122)
    plt.plot(np.arange(1, len(validity_rates)+1), validity_rates, label="Taux de validité")
    plt.xlabel("Étapes d'optimisation")
    plt.legend()
    plt.show()


'''

Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form a leaf value sequence.

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

Note:

Both of the given trees will have between 1 and 100 nodes.

'''
import unittest

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def leafSimilar(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: bool
        """
        return self.get_leaf(root1) == self.get_leaf(root2)

    def get_leaf(self, root):
        stack = [root]
        result = []
        while stack:
            node = stack.pop()
            if node.left is None and node.right is None:
                result.append(node.val)
            elif node.left is not None and node.right is not None:
                stack.extend([node.left, node.right])
            elif node.left is not None:
                stack.append(node.left)
            else:
                stack.append(node.right)

        return result


"""
SOC lookup
----------

A Flow for generating a lookup table of SOC job titles to SOC codes
"""
import json
from hashlib import md5

from daps_utils import DapsFlowMixin

from metaflow import FlowSpec, S3, step

from ojd_daps.flows.enrich.labs.soc.metadata_utils import (
    load_json_from_s3,
    save_metadata_to_s3,
)

def short_hash(text):
    """Generate a unique short hash for this string"""
    hx_code = md5(text.encode()).hexdigest()
    int_code = int(hx_code, 16)
    short_code = str(int_code)[:16]
    return int(short_code)

class SocMetadataFlow(FlowSpec, DapsFlowMixin):
    @step
    def start(self):
        save_metadata_to_s3()
        self.next(self.end)

    @step
    def end(self):
        """Upload the SOC lookup"""
        soc_to_title = load_json_from_s3("soc_code_to_std_title")
        soc_lookups = [
            {
                "soc_id": short_hash(soc_code + standard_title),
                "soc_code": soc_code,
                "soc_title": standard_title,
            }
            for soc_code, standard_titles in soc_to_title.items()
            for standard_title in standard_titles
        ]
        # Validate that SOC IDs are unique
        soc_ids = [lookup["soc_id"] for lookup in soc_lookups]
        if len(soc_ids) > len(set(soc_ids)):
            raise ValueError("Non-unique hashes found")
        # All good, save to S3
        with S3(run=self) as s3:
            filename = f"soc_lookup-test-{self.test}.json"
            data = json.dumps(soc_lookups)
            s3.put(filename, data)

if __name__ == "__main__":
    SocMetadataFlow()


V1=[]

Num=int(input("Digite la cantidad el tamaño del vector: \n"))

for i in range(0,Num):
    Entrada=int(input("Digite un numero para ingresar al vector\n"))
    V1.append(Entrada)

V1.sort()
print("El numero mayor es: ", V1[Num-1])
print("El numero menor es: ", V1[0])


# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

# Load customer data
customers = pd.read_csv('customer_data.csv')

# Select relevant features for clustering
features = ['data_allowance', 'international_calling', 'mobile_hotspot']
X = customers[features]

# Standardize the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Use K-Means clustering to identify customer segments
kmeans = KMeans(n_clusters=3)
clusters = kmeans.fit_predict(X_scaled)

# Add cluster labels to the customer data
customers['cluster'] = clusters

# Use the cluster labels to make personalized recommendations
for customer_id, cluster in customers[['customer_id', 'cluster']].values:
  # Get the customer's current plan data
  current_plan = customers[customers['customer_id'] == customer_id][['data_allowance', 'international_calling', 'mobile_hotspot']].values[0]

  # Select other customers in the same cluster
  similar_customers = customers[customers['cluster'] == cluster]

  # Calculate the average plan data for the similar customers
  average_plan = similar_customers[['data_allowance', 'international_calling', 'mobile_hotspot']].mean().values

  # Calculate the difference between the customer's current plan and the average plan
  plan_difference = current_plan - average_plan

  # If the customer's plan is below the average for their cluster, recommend a new plan
  if np.all(plan_difference < 0):
    recommended_plan = average_plan + plan_difference
    print(f'Customer {customer_id} should consider switching to a plan with:')
    print(f'Data allowance: {recommended_plan[0]:.0f}')
    print(f'International calling: {recommended_plan[1]:.0f}')
    print(f'Mobile hotspot: {recommended_plan[2]:.0f}')


import numpy as np
from scipy.signal import savgol_filter
import matplotlib.pyplot as plt
from scipy import signal
from Utils.Lib import movingAverage
from scipy.ndimage import label
from FeaturesEngineering.FeaturesLib import computeVectorsDirection, computeVelAccV2, computeSegmentAngles
from Utils.Lib import cartesianToSpher

def detectSaccade(gaze: np.array, ball: np.array, tobii:np.array,  tobii_avg:np.array):
    '''
    :param gaze: gaze in the world
    :param ball: ball in head vector
    :param tobii: tobii in the world
    :param tobii_avg: tobii with windowed average
    :return:
    - onset and offset saccade
    - onset and offset smooth pursuit
    - onset and offset fixation
    - stream label of gaze event (1: fixation, 2: smooth pursuit, 3: saccade)
    '''
    def contstructStreamLabel(fixation, sop, saccade):
        # 1 fixation
        # 2 smooth pursuit
        # 3 saccade
        label = np.ones(len(gaze)).astype(int)

        for on, off in fixation:
            label[on:off] = 1
        for on, off in sop:
            label[on:off] = 2
        for on, off in saccade:
            label[on:off] = 3

        return label

    vel_gaze, vel_norm_gaze, acc_gaze = computeVelAccV2(gaze - tobii_avg, normalize=True)
    vel_gaze_h, vel_norm_gaze_h, acc_gaze_h = computeVelAccV2(gaze - tobii,  normalize=False)
    vel_ball, vel_norm_ball, acc_ball = computeVelAccV2(ball - tobii, normalize=False)
    vel_head, vel_norm_head, acc_head =  computeVelAccV2(tobii, normalize=True)

    N_label = len(gaze)
    # detect saccade
    labels_saccade = np.zeros(N_label)
    labels_saccade[(vel_norm_gaze >= 40) & (acc_gaze >= 15) ] = 1
    onset_offset_saccade = detectOnsetOffset(labels_saccade == 1, type="saccade")

    if len(onset_offset_saccade) > 0:
        onset_offset_saccade[:, 0] = onset_offset_saccade[:, 0] -1
        onset_offset_saccade[:, 1] = onset_offset_saccade[:, 1] + 2

    # relabel saccade
    for on, off in onset_offset_saccade:
        labels_saccade[on:off+1] = 1

    # detect pursuit
    # ball_gaze_dist = computeSegmentAngles(ball - tobii, gaze - tobii)
    labels_sp = np.zeros(N_label)
    vel_ratio = vel_gaze_h / (vel_ball + 1e-15)
    _, gaze_az, gaze_elv = cartesianToSpher(vector=gaze - tobii_avg, swap=False)
    _, ball_az, ball_elv = cartesianToSpher(vector=ball - tobii_avg, swap=False)

    # when computing the angle between the gaze and the ball, both of them shuld be converted into visual field
    gaze_view = np.vstack([gaze_az, gaze_elv]).transpose()
    ball_view = np.vstack([ball_az, ball_elv]).transpose()
    ball_gaze_dist = np.linalg.norm(gaze_view - ball_view, axis=-1)
    # labels_sp[(ball_gaze_dist<=15) & ((vel_gaze_h >30) & (vel_ratio >= 0.3) & (vel_ratio <= 2.0))] = 1  # pursuit gain
    labels_sp[((ball_gaze_dist<10) | ((vel_gaze_h >30) & (vel_ratio >= 0.3) & (vel_ratio <= 1.2)))] = 1  # pursuit gain
    labels_sp[labels_saccade==1] = 0
    onset_offset_sp = detectOnsetOffset(labels_sp == 1, type="sp")
    onset_offset_sp = groupPursuit(onset_offset_sp)

    # detect

    labels_fix = np.ones(N_label)
    for on, off in onset_offset_saccade:
        labels_fix[on:off+1] = 0
    for on, off in onset_offset_sp:
        labels_fix[on:off + 1] = 0
    onset_offset_fix = detectOnsetOffset(labels_fix == 1, type="fix")
    onset_offset_fix = groupingFixation(gaze, onset_offset_fix)

    stream_label = contstructStreamLabel(onset_offset_fix, onset_offset_sp, onset_offset_saccade)
    # plt.subplot(2, 1, 1)
    # plt.plot(vel_norm_gaze)
    # plt.subplot(2, 1, 2)
    # plt.plot(acc_gaze)
    #
    # plt.show()
    return onset_offset_saccade, onset_offset_sp, onset_offset_fix, stream_label

def classifyALCS(saccade_onset_offset: np.array, gaze: np.array,  gaze_view:np.array, ball_view: np.array) -> np.array:
    '''
    :param saccade_onset_offset:onset and offset episodes of the saccades
    :param gaze: gaze-in-the-world
    :param gaze_view: gaze in the visual field
    :param ball_view: ball in the visual field
    label= (1: anticipatory look, 2: correction saccade)
    :return: labels of each saccade (0: normal saccade, 1: anticipatory look, 2: correction saccade)

    normal saccade: the saccade direction and ball direction differ (before anticipatory)
    anticipatory look: the saccade moves to the same direction as the ball
    correction saccade: the saccade occurs after anticipatory look (after anticipatory)
    '''

    def detectAL(g, b):
        ball_gaze_dir = computeVectorsDirection(g[1:] - g[0:1], b[1:] - b[0:1])

        return ball_gaze_dir[0]

    all_candidates = []
    all_gm = []
    for on, off in saccade_onset_offset:
        idx = on
        gm = computeSegmentAngles(np.expand_dims(gaze[on], 0), np.expand_dims(gaze[off], 0))[0]
        is_al = detectAL(gaze_view[[idx, idx + 2]], ball_view[[idx, idx + 2]])
        all_candidates.append(is_al)
        all_gm.append(gm)
    all_candidates = np.array(all_candidates)
    all_gm = np.array(all_gm)

    # print(all_gm)
    labels = np.zeros_like(all_gm)
    al_idx = np.argmax(all_gm * (all_candidates >=0))

    # if len(np.argwhere(all_candidates[:al_idx+1] >= 0)) == 1:
    #     labels[np.argwhere(all_candidates[:al_idx+1] >= 0)] = 1

    labels[al_idx] = 1
    labels[al_idx+1:] = 2

    # print(all_candidates)
    # print(all_gm)
    # print(labels)

    return labels

def saccadeFeatures(onset_offset: np.array, gaze: np.array, ball: np.array, win_length:int = 10, phase_start=0, phase_end=100, classify_al=True, phase_2=False) -> tuple:
    '''
    Onset                       =on
    Offet                       =off
    Duration                    =dn
    Mean distance angle         =mda
    Mean difference             =md
    Min difference              =mid
    Max difference              =mad
    Mean magnitude              =mm
    Sum magnitude               =sm
    Global magnitude            =gm

    :param onset_offset: onset and offset episodes
    :param gaze: gaze in the world (gaze-tobii)
    :param ball: ball in the world  (ball-tobii)
    :return: concatenate features

    One should note that we use gaze and ball information in the world space and visual-field space
    When you should use gaze-in-the-world
    - computing velocity or acceleration
    when you should use gaze-in-the-head (visual field)
    - when computing angle between the ball and the gaze
    '''

    _, gaze_az, gaze_elv = cartesianToSpher(vector=gaze, swap=False)
    _, ball_az, ball_elv = cartesianToSpher(vector=ball, swap=False)

    # when computing the angle between the gaze and the ball, both of them shuld be converted into visual field
    gaze_view = np.vstack([gaze_az, gaze_elv]).transpose()
    ball_view = np.vstack([ball_az, ball_elv]).transpose()

    dist_angle = np.linalg.norm(gaze_view - ball_view, axis=-1)

    features = []

    if len(onset_offset) > 0:
        for on, off in onset_offset:
            # print(on)
            # extract saccade features

            if ((off - on) > 0):
                gaze_saccade = gaze[on:off+1]
                # compute magnitude
                vel_gaze, vel_norm_gaze, acc_gaze = computeVelAccV2(gaze_saccade, normalize=False)
                sm = np.sum(vel_gaze[:-1])
                mm = np.average(vel_gaze)

                gm = computeSegmentAngles(np.expand_dims(gaze[on], 0), np.expand_dims(gaze[off], 0))[0]

                mda = np.nanmean(dist_angle[on:off])

                # compute med-diff and mean-diff
                ball_after_offset = ball_view[off:off+win_length]
                gaze_at_offset = np.ones_like(ball_after_offset) * gaze_view[off]
                # after_ofset_angle = computeSegmentAngles(np.ones_like(ball_after_offset) * gaze_view[off], ball_after_offset)
                after_ofset_angle = np.linalg.norm(gaze_at_offset- ball_after_offset, axis=-1)

                if phase_2:
                    on_event = ((phase_end - phase_start) - (on - phase_start))  * 10
                    off_event = ((phase_end - phase_start) - (off - phase_start))  * 10
                else:
                    on_event = (phase_end - on) * 10
                    off_event = (phase_end - off) * 10

                # concatenate saccades features
                try:
                    features.append([
                                     on_event,
                                     off_event,
                                     (off - on) * 10,
                                     mda,
                                     np.nanmean(after_ofset_angle),
                                     np.nanmin(after_ofset_angle),
                                     np.nanmax(after_ofset_angle),
                                     mm,
                                     sm,
                                     gm
                                     ])
                except:
                    print("error")

    # remove nan features
    features = np.asarray(features)
    features_clean = features[np.sum(np.isnan((features)), -1) == 0]

    # classify whether a saccade is anticipitary look or correction saccade
    if classify_al:
        al_cs = classifyALCS(onset_offset, gaze, gaze_view, ball_view)
    else:
        al_cs = np.zeros(len(onset_offset))

    return features_clean, al_cs

def fixationSPFeatures(onset_offset_fsp:np.array, onset_offset_al_p2:np.array, gaze: np.array, ball: np.array,  phase_start=0, phase_end=100) -> np.array:
    '''
    Extract features of fsp that occurs immediately after AL in the P2
    :param onset_offset_fsp:
    :param onset_offset_al_p2:
    :param gaze:
    :param ball:
    :return:
    '''

    idx = onset_offset_fsp[:, 0] > onset_offset_al_p2[0,1]
    # print(idx)
    if np.sum(idx) > 0:
        features = []
        on, off = onset_offset_fsp[np.argwhere(idx)[0,0]]

        _, gaze_az, gaze_elv = cartesianToSpher(vector=gaze, swap=False)
        _, ball_az, ball_elv = cartesianToSpher(vector=ball, swap=False)

        # when computing the angle between the gaze and the ball, both of them shuld be converted into visual field
        gaze_view = np.vstack([gaze_az, gaze_elv]).transpose()
        ball_view = np.vstack([ball_az, ball_elv]).transpose()

        dist_angle = np.linalg.norm(gaze_view - ball_view, axis=-1)

        # compute the features
        on_event = ((phase_end - phase_start) - (on - phase_start)) * 10
        off_event = ((phase_end - phase_start) - (off - phase_start)) * 10

        mean_dist = np.nanmean(dist_angle[on:off])
        min_dist = np.nanmin(dist_angle[on:off])

        features.append([
            on_event,
            off_event,
            (off - on) * 10,
            mean_dist,
            min_dist

        ])

        features = np.asarray(features)

        return features
    else:
        return np.zeros((1,5))

def detectOnsetOffset(v: np.array, type="saccade") -> np.array:
    '''
    :param v: events vector
    :param type: type of the effents
    :return: onset and offset episodes
    '''

    v = v.astype(np.int)
    output = []
    valley_groups, num_groups = label(v == 1)
    if num_groups > 0:
        for i in np.unique(valley_groups)[1:]:
            valley_group = np.argwhere(valley_groups == i)

            on = np.min(valley_group)
            off = np.max(valley_group)
            if type == "sp":
                th_duration = 0
            elif type == "fix":
                th_duration = 0
            else:
                th_duration = 0
            if (off - on) > th_duration:
                output.append([on, off])

    output = np.asarray(output).astype(int)

    return output

def groupingFixation(gaze: np.array, onset_offset:np.array) -> np.array:
    '''
    use this function to group fixation only
    :param gaze: gaze in the world
    :param onset_offset: onset and offset episodes
    :return: the groupped onset and offset episodes
    '''
    if (len(onset_offset) > 1):
        new_onset_offset = []

        # print(onset_offset)
        for i in range(len(onset_offset)):

            c_off = onset_offset[i, 1]  # current offset
            onset = onset_offset[i, 0]
            offset = onset_offset[i, 1]
            for j in range(i+1, len(onset_offset)):

                n_on = onset_offset[j, 0]  # current on
                gaze_dist = computeSegmentAngles(gaze[n_on:n_on+1], gaze[c_off:c_off+1])
                if ((n_on - c_off) < 5) & (gaze_dist <= 3):
                    c_off = onset_offset[j, 1]
                    offset = onset_offset[j, 1]
                    onset_offset[j] = onset_offset[j]  * 0

            new_onset_offset.append([onset, offset])

        new_onset_offset = np.asarray(new_onset_offset).astype(int)
        # delete merged elements
        new_onset_offset = new_onset_offset[new_onset_offset[:, 1] != 0]
        return new_onset_offset
    else:
        return onset_offset

def groupPursuit(onset_offset:np.array) -> np.array:
    '''
    use this function to group smooth pursuit
    :param onset_offset: onset and offset episodes
    :return: the groupped episodes
    '''
    if (len(onset_offset) > 1):
        new_onset_offset = []

        # print(onset_offset)
        for i in range(len(onset_offset)):

            c_off = onset_offset[i, 1]  # current offset
            onset = onset_offset[i, 0]
            offset = onset_offset[i, 1]
            for j in range(i + 1, len(onset_offset)):

                n_on = onset_offset[j, 0]  # current on
                if n_on - c_off < 3:
                    c_off = onset_offset[j, 1]
                    offset = onset_offset[j, 1]
                    onset_offset[j] = onset_offset[j] * 0

            new_onset_offset.append([onset, offset])

        new_onset_offset = np.asarray(new_onset_offset).astype(int)
        # clean sp less than 100 ms
        for i in range(len(new_onset_offset)):
            on, off = new_onset_offset[i]
            if (off - on) < 5:
                new_onset_offset[i] = new_onset_offset[i] * 0

        # delete merged elements
        new_onset_offset = new_onset_offset[new_onset_offset[:, 1] != 0]
        return new_onset_offset
    else:
        return onset_offset

# methods used previously to detect anticipatory look at phase 1 and 2

# def detectALPhase1(eye_event: np.array, az: np.array, elv: np.array, th=10) -> np.array:
#     '''
#     :param eye_event: eye event of phase 1 from Tobii
#     :param gaze: azimuth (ball-gaze)
#     :param ball: elevation (ball-gaze)
#     :param th: degree/frame (saccade offset - ball trajectory)
#     :return: (angle, onset, offset)
#     '''
#
#     dist_angle = np.sqrt(np.square(az) + np.square(elv))
#     onset_offset = detectOnsetOffset(eye_event == 1)
#
#     al_angle = 1e+4
#     al_offset = 0
#     al_onset = 0
#
#     if len(onset_offset) > 0:
#         for on, off in onset_offset:
#             # angle between ball and gaze after saccade offset
#             min_angle = np.min(dist_angle[off:])
#             if min_angle <= th:
#                 al_offset = off
#                 al_onset = on
#                 al_angle = min_angle
#
#     return np.array([al_angle, al_onset, al_offset])
#
#
# def detectALPhase2(eye_event2: np.array, eye_event3: np.array, gaze: np.array, ball: np.array, th: float = 25,
#                    th_angle_p=25) -> np.array:
#     '''
#     :param eye_event2: eye_event phase 2
#     :param eye_event3: eye_event phase 3
#     :param gaze: gaze in polar coordinates
#     :param ball: ball in polar coordinates
#     :param th: degree/frame (saccade offset - ball trajectory) of AL
#     :param th_angle_p: degree/frame (saccade offset - ball trajectory) of pursuit
#     :return:
#     '''
#
#     # anticipation look
#     al_angle = 1e+4
#     al_magnitude = 1e+4
#     al_offset = 0
#     al_onset = 0
#
#     # pursuit
#     p_avg_angle = 1e+4
#     p_on = 0
#     p_off = 0
#
#     onset_offset_saccade = detectOnsetOffset(eye_event2 == 1)
#
#     ps2_end = len(eye_event2)
#     ps3_start = ps2_end
#     dist_angle = np.sqrt(np.sum(np.square(gaze - ball), -1))  # use azimuth and elevation
#     # dist_angle = np.sqrt(np.square(gaze[:, 0] - ball[:, 0]))  # use azimuth only
#     # detect pursuit
#     dist_angle3 = dist_angle[ps3_start:]
#     pursuit_events_idx = detectOnsetOffset((eye_event3 == 2) & (dist_angle3 <= th_angle_p))
#
#     if (len(onset_offset_saccade) > 0) & (len(pursuit_events_idx) > 0):
#         first_pursuit = pursuit_events_idx[0]
#         last_pursuit = pursuit_events_idx[-1]
#         ball_pursuit = ball[first_pursuit[0], 0]
#         for on, off in onset_offset_saccade:
#             gaze_saccade = gaze[off, 0]
#             dist_sp = np.sqrt(np.sum(np.square(gaze_saccade - ball_pursuit)))
#             # print(dist_sp)
#             if dist_sp <= th:
#                 al_angle = dist_sp
#                 al_onset = on
#                 al_offset = off
#                 al_magnitude = np.sqrt(np.sum(np.square(gaze[off] - gaze[on]))) / ((off - on) + 1e-5)
#
#     if len(pursuit_events_idx) > 0:
#         first_pursuit = pursuit_events_idx[0]
#         last_pursuit = pursuit_events_idx[-1]
#         p_on = first_pursuit[0]
#         p_off = last_pursuit[1]
#         p_avg_angle_list = []
#         for p_on, p_off in pursuit_events_idx:
#             p_avg_angle_list.append(dist_angle3[p_on:p_off])
#
#         p_avg_angle = np.average(np.concatenate(p_avg_angle_list))
#     else:
#         if np.average((eye_event3 == 2) & (dist_angle3 <= th_angle_p)) == 1:
#             p_on = 0
#             p_off = len(dist_angle3) - 1
#             p_avg_angle = np.average(dist_angle3)
#         # else:
#         #     plt.plot(dist_angle3)
#         #     plt.show()
#
#     return np.array([al_angle, al_magnitude, al_onset, al_offset]), np.array([p_avg_angle, p_on, p_off])


# -*- encoding=utf-8 -*-
from computer_principle.Node import Node

class DoubleLinkedList:
    def __init__(self, capacity=0xffff):
        self.capacity = capacity
        self.head = None
        self.tail = None
        self.size = 0

    def __remove_head(self):
        if not self.head:
            return
        node = self.head
        if node.next:
            node.next.prev = None
            self.head = node.next
        else:
            self.head = self.tail = None
        self.size -= 1
        return node

    def __remove_tail(self):
        if not self.tail:
            return
        node = self.tail
        if node.prev:
            node.prev.next = None
            self.tail = node.prev
        else:
            self.head = self.tail = None
        self.size -= 1
        return node

    def __remove(self, node):
        if not node:
            node = self.tail
        if node == self.head:
            return self.__remove_head()
        elif node == self.tail:
            return self.__remove_tail()
        else:
            node.next.prev = node.prev
            node.prev.next = node.next
            self.size -= 1
        return node

    def __append_head(self, node):
        if not self.head:
            self.head = self.tail = node
        else:
            self.head.prev = node
            node.next = self.head
            self.head = node
        self.size += 1

    def __append_tail(self, node):
        if not self.tail:
            self.head = self.tail = node
        else:
            self.tail.next = node
            node.prev = self.tail
            self.tail = node
        self.size += 1

    def pop(self):
        return self.__remove_head()

    def append(self, node):
        self.__append_tail(node)

    def append_head(self, node):
        self.__append_head(node)

    def remove(self, node=None):
        return self.__remove(node)


"""
Functions for creating HTML representations of tabular data.

"""

from __future__ import with_statement

import sys
import cgi
from os import getcwd
from os.path import splitext, basename, isdir, dirname

import numpy as np

import tabular as tb
import tabular.utils as utils
import tempfile
from tabular.colors import Point2HexColor

__all__ = ['tabular2html']

def tabular2html(fname=None, X=None, fin=None, title=None, printheader=False,
                 split=True, usecss=None, writecss=None, SERVERNAME=None,
                 SERVER_FROM_CURDIR='../', ROWS_PER_PAGE=1000,
                 returnstring = False, **kwargs):
    """
    Creates an html representation of tabular data, either from a tabarray or
    an externa file (`including ``.hsv``, ``.csv``, ``.tsv``).  If no data is
    directly provided by passing a tabarray to `X`, then a tabarray is
    constructed using :func:`tabular.tabarray.tabarray.__new__`.

    **Parameters**

            **fname** :  string

                    Path to the "main" HTML file to be created.  This file path
                    must end in ``.html``.

                    Note that this function will create additional files (e.g.
                    a ``.css`` file and multiple linked ``.html`` files for
                    large datasets) linked to this HTML file, inside of the
                    directory containing `fname`.

            **X** :  tabarray

                    If `X` is `None`, then one of `fin`, `array`, `records`,
                    `columns`, `SV`, `HSV`, or `HSVlist` must not be `None`.

            **fin** :  string

                    File path to to a source of tabular dat, which will be
                    loaded using the tabarray constructor.  The load method
                    will be inferred from the file extension and whether or not
                    there is a headerkey in the first line of the file.

            **title** :  string

                    Description to be used in the <title> tag of the created
                    html file.

            **printheader** : boolean

                    If `printheader = True`, will print out a "header" and
                    "footer" (both in the body of the HTML documents) on every
                    page.  The header contains information about the name of
                    the input file and the number of rows, as well what the
                    current page number is (if split between multiple
                    documents) and links to any other pages.  The footer
                    contains the same page number and links.

            **split** : boolean

                    If `split = False`, will not split to multiple HTML pages,
                    regardless of value of `ROWS_PER_PAGE`.

            **usecss** : False or None or string

                    If usecss is False, no link to a cssfile is included in the
                    page header, and no cssfile is written out.  If is a
                    string, that string is assumed to be a path and is linked
                    to as the CSS file.  If it is None, then consideration of
                    the `writecss` variable is made.

            **writecss** : boolean

                    If `usecss` is not `None`, then if `writecss` is not
                    `False`:  the default css sheet is generated and written to
                    a file whose name is either generated by default (if
                    writecss is None) else given by writecss itself, and linked
                    to in the file header

            **SERVERNAME** :  string

                    Server name.  For example, this could be the ServerName
                    of a VirtualHost on your local machine, assuming that
                    `fname` describes a path on the server.

            **SERVER_FROM_CURDIR** :  string

                    Root path of server relative to the current directory.
                    Assumed to be '../'.

            **ROWS_PER_PAGE** :  positive integer or 'all'

                    This sets the number of records displayed per .html page
                    (if the tabular file has more than ROWS_PER_PAGE rows,
                    it will be split into multiple sections on several .html
                    pages (default = 1000).

                    If the value is 'all' then the page is not split (e.g. it
                    is as if split = False)

            **See also:**  the kwargs arguments must be valid keyword arguments
            for :func:`tabular.tabarray.tabarray.__new__`, the tabarray
            constructor, see documentation for descriptions.

    """

    # Must write to an HTML file.

    assert returnstring or fname.endswith( '.html' ), 'fname must end in ".html".'

    if X is None:
        if fin is not None:
            if fin.lstrip('/').endswith('.hsv'):
                kwargs['HSVfile'] = fin
            elif fin.endswith('.tsv') or fin.endswith('.csv'):
                kwargs['SVfile'] = fin
            elif fin.endswith(('.npy','.npz')):
                kwargs['binary'] = fin
            else:
                assert False, ('This algorithm is being forced to determine '
                              'the proper file type for web representation '
                              'from file\'s path (e.g. by looking at '
                              'extension) since the type is not given '
                              'explicitly by use of a keyword argument, but '
                              'is having problems deducing the intended file '
                              'type from the path (e.g., because the '
                              'extension is not one of those this algorithm '
                              'recognizes).')
        else:
            assert any([l in kwargs.keys() and kwargs[l] != None
                        for l in ['SVfile','binary','HSVfile']]), \
                   ('Either a tabarray is given, or file path "fin" is '
                    'given, or one of "HSV", "binary", or "SV" keyword '
                    'arguments are given.')

        X = tb.tabarray(**kwargs)

    names = X.dtype.names
    try:
        RowColors = X['__color__']
    except:
        if '__color__' in names:
            cspot = names.index('__color__')
            RowColors = [r[cspot] for r in X]
        else:
            RowColors = [''] * len(X)
    try:
        coloring = X.coloring
    except:
        coloring = {}

    Num_Records = len(X)
    Num_Cols = len(names)
    ColorStyles = CSSColoring(names, coloring)
    HdrNts = HeaderNotations(names, coloring)

    # If I specify usecss and it is not false, it must be a string and I want
    # to put that file name in the link and not write anything out.
    # If I specify writecss I want it to write out file to that name and use it
    # in the link.
    # If usecss = false, writecss is false and nothing is put in the link.
    # If usecss is not specified, then ...

    if usecss != None:
        if isinstance(usecss, str):
            cssfile = usecss
            CSSLINK = ('<link rel="stylesheet" type="text/css" href="' + '/' +
                       cssfile[len(SERVER_FROM_CURDIR):] + '"</link>')
        else:
            assert usecss == False
            CSSLINK = ''
    else:
        if writecss == False or returnstring:
            CSSLINK = ''
        else:
            if not isinstance(writecss,str):
                cssfile = fname[:-5] + '.css'
            else:
                cssfile = writecss
            WriteOutCSS(ColorStyles[1],cssfile)
            CSSLINK = ('<link rel="stylesheet" type="text/css" href="' + '/'  +
                       cssfile[len(SERVER_FROM_CURDIR):] + '"</link>')

    if returnstring:
        split = False

    if not split or ROWS_PER_PAGE == 'all':
        ROWS_PER_PAGE = Num_Records + 1

    numSections = int(Num_Records / ROWS_PER_PAGE) + 1

    # section2file(i) returns the name of the .html file corresponding to
    # section number i.
    section2file = (lambda sectionNum: fname if sectionNum == 0
                else splitext(fname)[0] + str(sectionNum) + splitext(fname)[1])

    if title is None:
        if not fin is None:
            title = fin
        else:
            title = 'Title Not Given'
    for section in range(numSections):
        sectionfname = section2file(section)
        fromRow = section * ROWS_PER_PAGE  # Start record # for this section.
        toRow = min( fromRow + ROWS_PER_PAGE, Num_Records)  # End record #.

        if printheader and not returnstring:
            prefix = '/' + DirName(fname[len(SERVER_FROM_CURDIR):]) + '/'
        else:
            prefix = ''

        # Open the output file for the section to fileobject 'f'.
        if not returnstring:
            f = open(sectionfname,'w')
        else:
            f = tempfile.TemporaryFile('w+b')

        # Write out file header.
        if not returnstring:
            f.write('<META HTTP-EQUIV="Content-Type" '
                'CONTENT="text/html; charset=utf-8" /><title>' +
                title + '</title>' + CSSLINK + '\n' )
        if printheader:
            f.write('
Tabular File (page ' + str(section + 1) + ' of ' +
                    str(numSections) + ', rows ' + str(fromRow + 1) +
                    ' - ' + str(toRow) + '): ' + title + '
\n')
            f.write('
page ')
            if section > 0:
                f.write(' prev ')
            if section < numSections - 1:
                f.write(' next ')
            for page in range(numSections):
                f.write((' ' + str(page + 1) +
                      '') if page != section else ' ' + str(page + 1))
            f.write( '
' )

        # Write out table with number of cols.
        f.write('
\n')
        f.write('<col span="' + str(Num_Cols) + '" align="center">\n')

        # Write out table header line.
        f.write('
')
        if len(HdrNts) > 0:
            for h in HdrNts:
                f.write(h + '\n')
        f.write('
')
        for name in names:
            f.write('' +
                    cgi.escape(name) + '	')
        f.write('
')
        f.write('
\n')

        # Write out each record in the section.
        f.write( '
\n' )
        if (len(names) > 1) or (fin != None and fin.endswith('.csv')):
            for row in range( fromRow, toRow ):
                colorst = (' style="background-color:' + RowColors[row] +
                           '" ' if  RowColors[row] != '' else '')
                f.write('
')
                for (i, val) in enumerate(X[ row ]):
                    #f.write('' + cgi.escape(str(val)) + '	')
                    f.write('' + str(val).replace('\n','
') +
                            '	')
                f.write('
\n')
        else:
            for row in range(fromRow, toRow):
                f.write('
')
                #f.write('' + cgi.escape(str(X[row])) + '	')
                f.write('' + str(X[row]).replace('\n','
') + '	')
                f.write('
\n')
        f.write('
\n')

        f.write( '
' )

        # Write out hyperlinks to other sections.
        if printheader:
            f.write('
page ')
            if section > 0:
                f.write(' prev ')
            if section < numSections - 1:
                f.write(' next ')
            for page in range(numSections):
                f.write((' ' +
                         str(page + 1) + '') if page != section
                                                 else ' ' + str(page + 1))

            f.write('
')

        # End file.
        if not returnstring:
            f.write('\n')

        if returnstring:
            f.seek(0)
            s = f.read()
            f.close()
            return s
        else:
            f.close()

def FixCSSName(k):
    k.replace('.','__')
    if k[0] in ['0','1','2','3','4','5','6','7','8','9']:
        k = '__' + k
    return k

def CSSColoring(names, coloring):
    Tree = NameTree(names, coloring)
    X = ColorScheme(Tree, (0, 1), Tree.weight)
    D1 = dict([(k, FixCSSName(k)) for k in X.keys()])
    D2 = dict([(FixCSSName(k), X[k]) for k in X.keys()])
    return [D1, D2]

def ColorScheme(NTree, B, Total):

    [L, R] = B
    [NS, NT, W] = [len(NTree.subtrees.keys()), len(NTree.topnodes),
                   NTree.weight]

    angle = L + (R - L) * float(NS) / (NS + 1)
    lfrac = float(NT) / W
    tfrac = float(NT) / Total

    CScheme = {}
    TopColor = Point2HexColor(angle, .3 * (1 - tfrac), .85 + .15 * tfrac)
    for n in NTree.topnodes:
        CScheme[n] = TopColor

    if NS > 0:
        Delta = (R-L) * (1.0/NS)
        for (i, t) in enumerate(NTree.subtrees.values()):
            CScheme.update(ColorScheme(t, (L + Delta * i, L + Delta * (i + 1)),
                                       Total))

    return CScheme

def HeaderNotations(names, sdict):
    NTree = NameTree(names, sdict)
    GroupsByLevel = GroupByLevel(NTree, sdict)
    lines = []
    for l in GroupsByLevel:
        if len(l) > 0:
            lines += [MakeLine(names, l, sdict)]
    return lines

def GroupByLevel(NTree, sdict):
    #Levels = [NTree.subtrees.keys()]
    Levels = EqualLevels(NTree.subtrees.keys(), sdict)
    LowerLevels = [GroupByLevel(t, sdict) for t in NTree.subtrees.values()]
    if len(LowerLevels) > 0:
        h = max([len(l) for l in LowerLevels])
        for i in range(h):
            New = utils.uniqify(utils.listunion([l[i] for l in LowerLevels
                                                 if len(l) > i]))
            if len(New) > 0:
                Levels += [New]
    return Levels

def EqualLevels(L, sdict):
    EL = []
    done = []
    for i in range(len(L)):
        l1 = L[i]
        if l1 not in done:
            # need to copy dictionary contents,
            # otherwise LevelVals points to the dict!
            LevelVals = sdict[l1][:]
            LevelKeys = [l1]
            done += [l1]
            for j in range(i, len(L)):
                l2 = L[j]
                if len(set(LevelVals).intersection(set(sdict[l2]))) == 0:
                    LevelVals += sdict[l2][:]
                    LevelKeys += [l2]
                    done += [l2]
            EL += [LevelKeys]
    return EL

def MakeLine(names, L, sdict):
    LN = np.array([[l for l in L if name in sdict[l]][0]
                      if len([l for l in L if name in sdict[l]]) > 0
                      else '' for name in names])
    Diffs = np.append(np.append([-1], (LN[1:] != LN[:-1]).nonzero()[0]),
                         [len(LN) - 1])
    s = '
'

    for i in range(len(Diffs) - 1):
        if LN[Diffs[i] + 1] != '':
            s += ('' +
                  LN[Diffs[i] + 1]  +  '	')
        else:
            s += '	'

    s+= '
'
    return s

class NameTree():
    def __init__(self, names, sdict):

        self.topnodes = set(names).difference(utils.listunion(sdict.values()))

        self.subtrees = {}
        SK = sdict.keys() ; SK.sort()
        done = []
        for (i,s) in enumerate(SK):
            if not any([set(sdict[s]) < set(sdict[ss])  for ss in SK]):
                subsets = [ss for ss in sdict.keys()
                           if set(sdict[ss]) < set(sdict[s])]
                newsdict = dict([(ss, list(set(sdict[ss]).difference(done)))
                                 for ss in subsets])
                newnames = list(set(sdict[s]))#.difference(done))
                if len(newnames) > 0:
                    self.subtrees[s] = NameTree(newnames, newsdict)
                    done += newnames

        self.weight = (sum([t.weight for t in self.subtrees.values()]) +
                       len(self.topnodes))

def WriteOutCSS(ColorStyles, outpath):
    f = open(outpath,'w')
    f.write('body, td{\n')
    f.write('font-size : 10pt;\n')
    f.write('}\n')
    f.write('th{\n')
    f.write('font-weight : bold;\n')
    f.write('}\n')

    for Class in ColorStyles.keys():
        f.write('td.' + Class  + ',th.' + Class + '{\n')
        f.write('background-color : ' + ColorStyles[Class] + '\n')
        f.write('}\n')

    f.close()

def DirName(path):
    '''
    Utility that gets directory name.

    Sometimes this is the right thing to use intead of ``os.path.dirname``.
    '''
    if path[-1] == '/':
        path = path[:-1]
    return dirname(path)


import threading
import time
from concurrent.futures import ThreadPoolExecutor

def func(seconds):
    print(f"sleeping for {seconds} seconds")
    time.sleep(seconds)
    return seconds

def main():
    #Normal code
    time1=time.perf_counter()
    #func(4)
    #func(7)

    #print(time2-time1)
    #Using Threads
    t1= threading.Thread(target=func, args=[4])
    t2= threading.Thread(target=func, args=[2])
    t3= threading.Thread(target=func, args=[1])

    t1.start()
    t2.start()
    t3.start()

    t1.join()
    t2.join()
    t3.join()

#Calculating time

    time2=time.perf_counter()
    print(time2-time1)

def poolingDemo():
    with ThreadPoolExecutor() as executer:
      #  future1=executer.submit(func, 3)
      #  future2=executer.submit(func, 2)
      #  future3=executer.submit(func, 4)
       # print(future1.result())
        #print(future2.result())
        #print(future3.result())

#using maps

        l=[3,2,4,6,1]
        results=executer.map(func,l)
        for result in results:
            print(result)

poolingDemo()

Multiprocessing, multithreading, parallel processing/Multithreading.py


from arrakisclient.client import ArrakisClient
from arrakisclient.types.namespace import ArrakisNamespace, Relation
from arrakisclient.types.tuple import ArrakisUserset

# Define some ORM-like models, must match the namespaces configured in the tenant
class ReferenceableNamespace(ArrakisNamespace):
    """Base model that will define the "ellipsis" relation, which can be used to reference items
    in the namespace as a unit, without implying any further relationships."""

    ellipsis = Relation(relation_name="...")

class User(ReferenceableNamespace):
    """ A model to represent users in your domain. """

    __namespace__ = "yourtenant/user"

class Document(ArrakisNamespace):
    """ A model to represent documents in your business domain (if you have such a thing). """

    __namespace__ = "yourtenant/document"

    # These relations define the permissions your code will ask about
    read = Relation(User)
    write = Relation(User)
    delete = Relation(User)

    # These relations define the relationship (role) a user can be directly granted
    viewer = Relation(User)
    contributor = Relation(User)
    owner = Relation(User)

token = "t_your_token_here_1234567deadbeef"
client = ArrakisClient(User, Document, access_token=token)

# Create some object references
a_doc = Document("doc1")
an_owner = User("theowner")
an_editor = User("userwhocanedit")
a_viewer = User("viewonlyuser")

# Insert some tuples granting roles to users
with client.batch_write() as w:
    w.create(a_doc.owner(an_owner.ellipsis))
    w.create(a_doc.contributor(an_editor.ellipsis))
    w.create(a_doc.viewer(a_viewer.ellipsis))

# Save the zookie that the call above generated to prevent new enemies
# We recommend saving this from any call to batch_write or content_change_check,
# and storing it alongside the object referenced in the write or check (in this case a_doc)"
when_perms_changed = w.revision

# Run some checks
assert client.check(a_doc.read, ArrakisUserset.from_onr(a_viewer.ellipsis), when_perms_changed)
assert client.check(a_doc.read, ArrakisUserset.from_onr(an_editor.ellipsis), when_perms_changed)
assert client.check(a_doc.read, ArrakisUserset.from_onr(an_owner.ellipsis), when_perms_changed)
assert not client.check(a_doc.write, ArrakisUserset.from_onr(a_viewer.ellipsis), when_perms_changed)
assert client.check(a_doc.write, ArrakisUserset.from_onr(an_editor.ellipsis), when_perms_changed)
assert client.check(a_doc.write, ArrakisUserset.from_onr(an_owner.ellipsis), when_perms_changed)
assert not client.check(
    a_doc.delete, ArrakisUserset.from_onr(a_viewer.ellipsis), when_perms_changed
)
assert not client.check(
    a_doc.delete, ArrakisUserset.from_onr(an_editor.ellipsis), when_perms_changed
)
assert client.check(a_doc.delete, ArrakisUserset.from_onr(an_owner.ellipsis), when_perms_changed)


#!/usr/bin/python3

import httplib2
import base64
import string

h = "localhost"
u = "girish"
p = "123456"

authToken = base64.b64encode(bytes( ('%s:%s' % (u, p)).replace('\n', ''), "utf8" ))
print(authToken)

req = httplib2.HTTP(h)
req.putrequest("GET", "/protected/index.html")
req.putheader("Host", h)
req.putheader("Authorization", "Basic %s" % authToken)
req.endheaders()
req.send("")

statusCode, statusMsg, headers = req.getreply()

print("Response: ", statusMsg)


from django.http import HttpResponse
from django.shortcuts import render
from webtoon.models import Webtoon, Episode

def index(request):
    webtoon_list = Webtoon.objects.all()
    context = {
        'webtoon_list' : webtoon_list,
    }
    return render(request, 'webtoon/index.html', context)

def detail(request, webtoon_id):
    webtoon = Webtoon.objects.get(pk=webtoon_id)
    context = {
        'webtoon' : webtoon,
        'episode_list' : Episode.objects.filter(webtoon=webtoon)
    }
    # return HttpResponse('You are looking at webtoon %s' % webtoon_id)
    return render(request, 'webtoon/detail.html', context)


import setuptools

with open('README.md', 'r', encoding='utf-8') as readme:
    long_description = readme.read()

setuptools.setup(
    name='fatture_ccsr',
    version='0.0.1',
    author='Ettore Dreucci',
    author_email='ettore.dreucci@gmail.com',
    description='Utility to download or convert CCSR invoices to TeamSystem\'s TRAF2000 record',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/Noettore/fattureSanRossore',
    packages=setuptools.find_packages(),
    install_requires=[
        'wxPython',
        'requests',
        'Unidecode',
        'requests_ntlm',
        'openpyxl',
        'PyPDF2',
    ],
    include_package_data=True,
    license='MIT',
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.7',
)


from __future__ import print_function

from google.cloud import automl_v1, automl

import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "key.json"

def main(path):
    project_id = "telemed-300210"
    model_id = "ICN2967249830855835648"

    file_path = path

    with open(file_path, 'rb') as ff:
        content = ff.read()

    get_prediction(content, project_id, model_id)

# 'content' is base-64-encoded image data.
def get_prediction(content, project_id, model_id):
    prediction_client = automl_v1.PredictionServiceClient()

    model_full_id = automl.AutoMlClient.model_path(
        project_id, "us-central1", model_id)

    payload = {'image': {'image_bytes': content}}
    params = {}
    request = prediction_client.predict(name=model_full_id, payload=payload, params=params)
    print("Prediction results:")
    for result in request.payload:
        print("Predicted entity label: {}".format(result.display_name))
        print("\n")
        var = result.display_name
        return var


import pysolr

solr = pysolr.Solr('http://localhost:8983/solr/core_2')

solr.ping()

solr.add([
    {
        "id": "doc_1",
        "title": "A test document",
    },
    {
        "id": "doc_2",
        "title": "The Banana: Tasty or Dangerous?",
        "_doc": [
            { "id": "child_doc_1", "title": "peel" },
            { "id": "child_doc_2", "title": "seed" },
        ]
    },
])

results = solr.search('doc_1')
print(results.docs)
r = solr.search('*:*')
print(len(r.docs))
solr.delete(q='*:*')
r = solr.search('*:*')
print(len(r.docs))


import discord
from discord.ext import commands
from discord.ext.commands import Context

from helpers.checks import IsOwnerId
from helpers.checks.IsOwnerId import owner_check
from helpers.database import user_settings
from helpers.file import config
from helpers.string import subcommand

class Config(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.group(name="config", help="Configure the bot.")
    async def _config(self, ctx: Context):
        if ctx.invoked_subcommand is not None:
            return
        subcommand_str = subcommand.subcommand_string(self._config)

        await ctx.reply(f"No subcommand specified. Try using {subcommand_str}.")

    @_config.group(name="set", aliases=["change", "update", "edit", "modify"])
    async def _set(self, ctx: Context):
        if ctx.invoked_subcommand is not None:
            return
        subcommand_str = subcommand.subcommand_string(self._set)

        await ctx.reply(f"No subcommand specified. Try using {subcommand_str}.")

    @_config.group(name="get", aliases=["show", "display", "view", "print"])
    async def _get(self, ctx: Context):
        if ctx.invoked_subcommand is not None:
            return
        subcommand_str = subcommand.subcommand_string(self._set)

        await ctx.reply(f"No subcommand specified. Try using {subcommand_str}.")

    @_get.command(name="runpod")
    async def _runpod_get(self, ctx: Context):
        global_runpod = config.get('RUNPOD_KEY')
        if not ctx.channel.type == discord.ChannelType.private:
            await ctx.reply("You cannot do this command in a public channel.")
            return

        user_runpod = user_settings.get(ctx.author.id, "runpod_key")

        if IsOwnerId.is_owner_user(ctx.author.id):
            await ctx.reply(f"The global runpod key is `{global_runpod}`"
                            f"{f', however your key is `{user_runpod}`' if user_runpod else ''}.")
        else:
            if user_runpod:
                await ctx.reply(f"Your runpod key is `{user_runpod}`.")
            else:
                await ctx.reply("You are using the global runpod key.")

    @_get.command(name="discord")
    @owner_check()
    async def _discord_get(self, ctx: Context):
        disc_token = config.get('DISCORD_TOKEN')
        if not ctx.channel.type == discord.ChannelType.private:
            disc_token = "*" * len(disc_token)
        await ctx.reply(f"The discord token is `{disc_token}`.")

    @_get.command(name="owner")
    async def _owner_get(self, ctx: Context):
        owner_id = int(config.get('OWNER_ID'))
        reply_message = f"The owner's ID is `{owner_id}`"

        if ctx.message.channel.type == discord.ChannelType.private:
            await ctx.reply(reply_message + ".")
            return

        reply_message += ", "

        potential_owner = await ctx.guild.fetch_member(owner_id)

        if potential_owner == ctx.author:
            await ctx.reply(reply_message + "and that's you!")
            return

        if potential_owner is None:
            await ctx.reply(reply_message + "but I can't find them in this server.")
        else:
            await ctx.reply(reply_message + f"but you may know them as `{potential_owner.display_name}`.")

    @_get.command(name="prefix")
    async def _prefix_get(self, ctx: Context):
        prefix = config.get('PREFIX', default="*")
        await ctx.reply(f"The prefix is `{prefix}`.")

    @_set.command(name="owner")
    @owner_check()
    async def _owner_set(self, ctx: Context, owner: discord.Member):
        config.set('OWNER_ID', owner.id)
        config.save()
        await ctx.reply(f"Ownership has been transferred to {owner.mention} ({owner.id}).")

    @_set.command(name="runpod")
    async def _runpod_set(self, ctx: Context, runpod: str = None):
        if ctx.channel.type != discord.ChannelType.private:
            await ctx.reply("Please do this in a a DM channel.")
            return

        if not runpod:
            if not IsOwnerId.is_owner_user(ctx.author):
                user_settings.set(ctx.author.id, "runpod_key", None)
                await ctx.reply("Your runpod key has been reset to default.")
                return

            await ctx.reply("You need to specify a runpod key.")
            return

        if IsOwnerId.is_owner_user(ctx.author.id):
            config.set('RUNPOD_KEY', runpod)
            config.save()
            await ctx.reply(f"Global runpod key has been set to `{runpod}`.")
            return
        else:
            user_settings.set(ctx.author.id, "runpod_key", runpod)
            await ctx.reply(f"Your runpod key has been set to `{runpod}`.")

    @_set.command(name="prefix")
    @owner_check()
    async def _prefix_set(self, ctx: Context, prefix: str = None):
        if not prefix:
            config.set('PREFIX', "*")
            await ctx.reply("The preset has been reset to the default.")
        else:
            config.set('PREFIX', prefix)
            await ctx.reply(f"The prefix has been set to `{prefix}`.")
        config.save()

    @_set.command(name="discord")
    async def _discord_set(self, ctx: Context):
        await ctx.reply("You're gonna need to do this with a reset of the bot, sorry.")

async def setup(bot):
    await bot.add_cog(Config(bot))


from dataPreProcess import loadDataset
from utils import zoomIntoImages
from . import config
from tensorflow import distribute
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import array_to_img
from tensorflow.io.gfile import glob
from matplotlib.pyplot import subplots
import argparse
import os
import sys

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--device", type=str, default="gpu",
                choices=["gpu", "cpu"], help="device to train on")
args = vars(ap.parse_args())
if args["device"] == "gpu":
    strategy = distribute.MirroredStrategy()
    tfrTestPath = config.GPU_DIV2K_TFR_TEST_PATH
    preTrainedGeneratorPath = config.GPU_PRETRAINED_GENERATOR_MODEL
    generatorPath = config.GPU_GENERATOR_MODEL
else:
    print("Training on CPU not possible")
    sys.exit(0)

print("Loading the test dataset")
testTfr = glob(tfrTestPath+"/*.tfrec")
testDataset = loadDataset(testTfr, config.INFER_BATCH_SIZE, train=False)

(lrImage, hrImage) = next(iter(testDataset))

with strategy.scope():
    print("Loading the pre-trained generator and fully trained SRGAN")
    srganPreTrainedGenerator = load_model(
        preTrainedGeneratorPath, compile=False)
    srganGenerator = load_model(generatorPath, compile=False)

    srganPreTrainedGeneratorPrediction = srganPreTrainedGenerator.predict(
        lrImage)
    srganGeneratorPrediction = srganGenerator.predict(lrImage)

print("plotting the SRGAN predictions")
(fig, axes) = subplots(nrows=config.INFER_BATCH_SIZE, ncols=4, figsize=(50, 50))

for (ax, lowRes, srPreIm, srGanIm, highRes) in zip(axes, lrImage, srganPreTrainedGeneratorPrediction, srganGeneratorPrediction, hrImage):
    ax[0].imshow(array_to_img(lowRes))
    ax[0].set_title("Low Resolution")
    ax[1].imshow(array_to_img(srPreIm))
    ax[1].set_title("SRGAN Pre-trained Generator")
    ax[2].imshow(array_to_img(srGanIm))
    ax[2].set_title("SRGAN Generator")
    ax[3].imshow(array_to_img(highRes))
    ax[3].set_title("High Resolution")

if not os.path.exists(config.BASE_IMAGE_PATH):
    os.makedirs(config.BASE_IMAGE_PATH)

print("Saving the images")
fig.savefig(config.GRID_IMAGE_PATH)

zoomIntoImages(
    srganPreTrainedGeneratorPrediction[0], "SRGAN Pre-trained Generator")
zoomIntoImages(srganGeneratorPrediction[0], "SRGAN Generator")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#---------------------------------------------------------------------------------
# Created By :
# Name : Marjolin Pierre
# E-Mail : pierre.marjolin@gmail.com
# Github : Pierre-Mar
#---------------------------------------------------------------------------------
import os, platform

from typing import Dict, List, Union
from GESAnalysis.FC.ExportData import ExportData
from GESAnalysis.FC.PATTERNS.Observable import Observable
from GESAnalysis.FC.ReaderData import ReaderData

class GESAnalysis(Observable):
    """ Class to manipulate data (Read file, write data)
    """

    __reader = ReaderData()
    __export = ExportData()

    def __init__(self) -> None:
        """ Initialise the class
        """
        super().__init__()
        self.__file_open = {} # Dictionary to associate the name of file and his data

#######################################################################################################
#  Read the data from a file                                                                          #
#######################################################################################################
    def read_file(self, filename: str, year:str, category:str, sep: str = None, engine: str = "pandas") -> None:
        """ Read the file 'filename' and add to the dictionary of file open

        Args:
            filename (str): Path to file
            sep (str, optional): Separator between values in 'filename'. Defaults to None.
            engine (str, optional): Reading engine for XLSX files. Defaults to "pandas".
        """
        if filename is None:
            raise Exception("Impossible de lire le fichier car le chemin est invalide")

        self.__check_year(year)

        data_file = self.__reader.read_file(filename, sep, engine)
        name_file = self.get_filename(filename)
        self.__file_open[name_file] = {}
        self.__file_open[name_file]["data"] = data_file
        self.__file_open[name_file]["year"] = year
        self.__file_open[name_file]["category"] = category
        self.__file_open[name_file]["path"] = filename
        self.__sort_by_year()

    def read_file_agent(self, filename: str, sep: str = None, engine: str = "pandas") -> dict:
        """ Read the file 'filename'

        Args:
            filename (str): Path to file
            sep (str, optional): Separator between values in 'filename'. Defaults to None.
            engine (str, optional): Reading engine for XLSX files. Defaults to "pandas".

        Returns:
            dict : Ditionary of data from the file
        """
        if filename is None:
            raise Exception("Impossible de lire le fichier car le chemin est invalide")

        return self.__reader.read_file(filename, sep, engine)

    def __sort_by_year(self) -> None:
        """ Sort the dictionary by year
        """
        self.__file_open = dict(sorted(self.__file_open.items(), key=lambda item: item[1]["year"]))

    def __check_year(self, year: str) -> None:
        """ Check if a year is correct

        Args:
            year (str): Year

        Raises:
            Exception: Year incorrect
        """
        try:
            year_int = int(year)
        except:
            raise Exception(f"'{year}' n'est pas une année")

#######################################################################################################
#  Write the data into a file                                                                         #
#######################################################################################################
    def export(self, filein: str, fileout: str) -> None:
        """ Write the current dictionnary into the file 'fileout'

        Args:
            filein (str): Path to the file to read the data
            fileout (str): Path to the file to write the data
        """
        file = self.get_filename(filein)
        # If the file is already open, then we export the data
        if file in self.__file_open.keys():
            self.__export.export_data(self.__file_open[file]["data"], fileout)
            return

        # Else, we read the data and export it
        data_file = self.__reader.read_file(filein)
        self.__export.export_data(data_file, fileout)

    def export_stat(self, data: List[List[str]], header_column: List[str], header_row: List[str], fileout: str):
        """ Export the stat of data and the headers in the file fileout

        Args:
            data (List[List[str]]): Stats
            header_column (List[str]): Header of columns
            header_row (List[str]): Header of rows
            fileout (str): Path to file to save the stats
        """
        self.__export.export_stat(data, header_column, header_row, fileout)

#######################################################################################################
#  Close a file                                                                                #
#######################################################################################################
    def close_file(self, filename: str) -> None:
        """ Remove the data from the dictionary

        Args:
            filename (str): Path to file

        Raises:
            Exception: The data of the file is not in the dictionary
        """
        file = self.get_filename(filename)
        try:
            del self.__file_open[file]
        except:
            raise Exception(f"Le fichier '{file}' n'est pas ouvert")

#######################################################################################################
#  Setters					                                                                          #
#######################################################################################################
    def set_year(self, filename: str, year: str) -> None:
        """ Set the year for the file 'filename'

        Args:
            filename (str): File
            year (str): Year

        Raises:
            Exception: File is not opened or a year is invalid
        """
        file = self.get_filename(filename)

        self.__check_year(year)

        try:
            self.__file_open[file]["year"] = year
        except:
            raise Exception(f"Le fichier '{file}' n'est pas ouvert")

    def set_category(self, filename: str, category: str) -> None:
        """ Set the category for the file 'filename'

        Args:
            filename (str): File
            category (str): Category

        Raises:
            Exception: File is not opened
        """
        file = self.get_filename(filename)
        try:
            self.__file_open[file]["category"] = category
        except:
            raise Exception(f"Le fichier '{file}' n'est pas ouvert")

#######################################################################################################
#  Getters                                                                                #
#######################################################################################################
    def get_data(self) -> Dict[str, Dict[str, List[Union[str, int, float, bool]]]]:
        """ Get the dictionary of data

        Returns:
            Dict[str, Dict[str, List[Union[str, int, float, bool]]]]: dictionary of data
        """
        return self.__file_open

    def get_file_open(self) -> List[str]:
        """ Get all the name of file who are open

        Returns:
            List[str]: List of name of file_
        """
        return list(self.__file_open.keys())

    def get_data_from_file(self, filename:str) -> Dict[str, Dict[str, List[Union[List[Union[int, float, bool, str]], str]]]]:
        """ Return the dictionary of data of the file 'filename'

        Raises:
            Exception: File is not opened

        Returns:
            dict: Dictionary of data
        """
        file = self.get_filename(filename)
        try:
            return self.__file_open[file]["data"]
        except:
            raise Exception(f"Le fichier '{file}' n'est pas ouvert")

    def get_filename(self, path_file: str) -> str:
        """ Return the name of the file of 'path_file'

        Args:
            path_file (str): path to the file

        Returns:
            str: name of file
        """
        root_filename, file_ext = os.path.splitext(path_file)
        sep_path = '/'
        if platform.system() == "Windows":
            sep_path = '\\'
        split_path = root_filename.split(sep_path)
        return split_path[len(split_path)-1] + file_ext

    def get_path(self, filename: str) -> str:
        """ Get the path of the file 'filename'

        Args:
            filename (str): File

        Raises:
            Exception: File is not opened

        Returns:
            str: Path
        """
        filename = self.get_filename(filename)
        try:
            return self.__file_open[filename]["path"]
        except:
            raise Exception(f"Le fichier '{filename}' n'est pas ouvert")

    def get_year(self, filename: str) -> str:
        """ Get the year of the file 'filename'

        Args:
            filename (str): File

        Raises:
            Exception: File is not opened

        Returns:
            str: Year
        """
        filename = self.get_filename(filename)
        try:
            return self.__file_open[filename]["year"]
        except:
            raise Exception(f"Le fichier '{filename}' n'est pas ouvert")

    def get_category(self, filename: str) -> str:
        """ Get the category of the file 'filename'

        Args:
            filename (str): File

        Raises:
            Exception: File is not opened

        Returns:
            str: Category
        """
        filename = self.get_filename(filename)
        try:
            return self.__file_open[filename]["category"]
        except:
           raise Exception(f"Le fichier '{filename}' n'est pas ouvert")