#!/usr/bin/env python3

import glob, re
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from ert_plot import *

device='pac_a10'

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#plt.title("Arria 10 Roofline: Floating Point Unrolling")
plt.suptitle("Arria 10 Roofline", size='xx-large')
plt.title("Floating Point Unrolling")
whatx = ErtLog.ai
whaty = ErtLog.max_gflops
u1 = plot_ert(device, 'swi', 1, 'coalesced', 1, 'float', 'ro--', whatx=whatx, whaty=whaty)
u2 = plot_ert(device, 'swi', 1, 'coalesced', 2, 'float', 'bo--', whatx=whatx, whaty=whaty)
u4 = plot_ert(device, 'swi', 1, 'coalesced', 4, 'float', 'go--', whatx=whatx, whaty=whaty)
u8 = plot_ert(device, 'swi', 1, 'coalesced', 8, 'float', 'co--', whatx=whatx, whaty=whaty)
u16 = plot_ert(device, 'swi', 1, 'coalesced', 16, 'float', 'mo--', whatx=whatx, whaty=whaty)
u32 = plot_ert(device, 'swi', 1, 'coalesced', 32, 'float', 'yo--', whatx=whatx, whaty=whaty)
plt.locator_params(axis='x', numticks=12)
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(fractions))
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(fractions))
ax.set_ylabel('GF/s')
ax.set_xlabel('AI')
ax.legend([u1[0],u2[0],u4[0],u8[0],u16[0],u32[0]],['1','2','4','8','16','32'], loc='lower right', title='Unrolls')
plt.savefig('roofline_1.png', dpi=500)


import sys
import time

moveReads = []
file = open('adventofcode/2021/day2_input.txt', "r")
for line in file:
 moveReads += [line.replace("\n","")]

horizontal = 0
vertical = 0
aim = 0

for move in moveReads:
    if("forward" in move):
        change = int(move[8:])
        horizontal += change
        vertical += aim * change
    elif("down" in move):
        change = int(move[5:])
        #vertical += int(move[5:])
        aim += change
    elif("up" in move):
        change = int(move[3:])
        #vertical -= change
        aim -= change

print(horizontal*vertical)


import numpy as np
import spikeextractors as se
from .get_unit_waveforms import get_unit_waveforms
from .SubsampledSortingExtractor import SubsampledSortingExtractor

def find_unit_peak_channels(recording, sorting, unit_ids):
    # Use the first part of the recording to estimate the peak channels
    sorting_shortened = SubsampledSortingExtractor(parent_sorting=sorting, max_events_per_unit=20, method='truncate')
    max_time = 0
    for unit_id in sorting_shortened.get_unit_ids():
        st = sorting_shortened.get_unit_spike_train(unit_id=unit_id)
        if len(st) > 0:
            max_time = max(max_time, np.max(st))
    recording_shortened = se.SubRecordingExtractor(parent_recording=recording, start_frame=0, end_frame=max_time + 1)
    unit_waveforms = get_unit_waveforms(
        recording=recording_shortened,
        sorting=sorting_shortened,
        unit_ids=unit_ids,
        channel_ids_by_unit=None,
        snippet_len=(10, 10)
    )
    channel_ids = recording.get_channel_ids()
    peak_channels = {}
    for ii, unit_id in enumerate(unit_ids):
        average_waveform = np.median(unit_waveforms[ii], axis=0)
        peak_channel_index = int(np.argmax(np.max(average_waveform, axis=1) - np.min(average_waveform, axis=1)))
        peak_channels[unit_id] = int(channel_ids[peak_channel_index])
    return peak_channels


import numpy as np
import os
import time
from flask import Flask,jsonify,request
import base64
from PIL import Image
from io import BytesIO
import GetFaceEncodings as GFE

app = Flask(__name__)

# This is the tolerance for face comparisons
TOLERANCE = 0.5

# This function takes a list of known faces
def compare_face_encodings(known_faces, face):

    # Finds the difference between each known face and the given face (that we are comparing)
    # Calculate norm for the differences with each known face
    return(np.linalg.norm(known_faces - face,axis=1))

# This function returns the name of the person whose image matches with the given face (or 'Not Found')
# known_faces is a list of face encodings
# names is a list of the names of people (in the same order as the face encodings - to match the name with an encoding)
# face is the face we are looking for
def find_match(known_faces, names, face):

    # Call compare_face_encodings to get a list of Euclidian distance values with each known faces
    matches = compare_face_encodings(known_faces, face)

    i = np.argmin(matches)

    if matches[i] <= TOLERANCE :
        return names[i],1
    else:
        return 'Not Found',0

@app.route("/predict",methods=['POST'])
def predict():

    start =time.clock()

    content = request.get_json()
    im = Image.open(BytesIO(base64.b64decode(content["image"])))
    im.save('test/image.jpg')

    print("Image Recieved")

    print("Time to preprocess image",time.clock()-start)
    start =time.clock()

    face_encodings = np.load('face_encodings.npy')
    names = np.load('name.npy')

    print("Time to load train_model",time.clock()-start)
    start =time.clock()

    face_encodings_in_image = GFE.get_face_encodings('test/image.jpg')
    s=""
    found=0

    if len(face_encodings_in_image)==0:
        return "No face"

    for i in range(len(face_encodings_in_image)):
        # Find match for the face encoding found in this test image
        match,flag= find_match(face_encodings, names, face_encodings_in_image[i])

        if(flag==1):
            found=1
            s+=match+" "

    print(s)

    print("time to match",time.clock()-start)

    if(found==0):
        return "No match"
    else:
        return s

if __name__ == '__main__':
    app.run(host='0.0.0.0')


# coding: utf-8

# In[1]:

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array,load_img
import json
import os
import matplotlib.pyplot as plt
import numpy as np

# In[2]:

# 测试图片存放位置
image_dir = 'flowers_test'

# In[3]:

# 载入标签json文件
file = open('label_flower.json','r',encoding='utf-8')
label = json.load(file)
# 键为分类编号，值为分类名称
print(label)

# In[4]:

# 载入训练好的模型
model = load_model('VGG16.h5')

# In[5]:

def model_predict(file_dir):
    # 读入图片，并resize为224*224大小
    img = load_img(file_dir, target_size=(224, 224))
    # 显示图片
    plt.imshow(img)
    plt.axis('off')
    plt.show()
    # 将图片转化为array
    x = img_to_array(img)
    # 增加1个维度变成4维数据
    # (224, 224, 3)->(1, 224, 224, 3)
    x = np.expand_dims(x, axis=0)
    # 模型预测结果
    # predict_classes直接返回预测分类结果，比如:[2]
    preds = model.predict_classes(x)
    # label字典中的键为字符串，所以这里需要把preds[0]转为str
    # 根据分类编号查询label中对应的分类名称
    preds = label[str(preds[0])]
    return preds

# 循环测试文件夹
for file in os.listdir(image_dir):
    # 测试图片完整路径
    file_dir = os.path.join(image_dir,file)
    # 打印文件路径
    print(file_dir)
    # 传入文件路径进行预测
    preds = model_predict(file_dir)
    print('predict:',preds)
    print('-'*20)

程序数据整理/第12章-图像识别项目实战/12-15 载入训练好的模型进行预测.py

12-15 载入训练好的模型进行预测.py


import numpy as np
import pandas as pd
import os
import tensorflow as tf
from tensorflow.contrib import tpu
from tensorflow.contrib.cluster_resolver import TPUClusterResolver
import sys

l_orderkey = 0
l_quantity = 0
l_returnflag = 0
s_nationkey = 0
n_nationkey = 0

def load_input(scale):
    global l_orderkey
    global l_quantity
    global l_returnflag
    global s_nationkey
    global n_nationkey

    os.chdir('/home/pedroholanda/tpch-' + str(scale))
    lineitem = pd.read_csv("lineitem.tbl", sep='|',
                           names=["l_orderkey", "l_partkey", "l_suppkey", "l_linenumber", "l_quantity",
                                  "l_extendedprice", "l_discount", "l_tax", "l_returnflag", "l_linestatus",
                                  "l_shipdate",
                                  "l_commitdate", "l_receiptdate", "l_shipinstruct", "l_shipmode", "l_comment"],
                           dtype={'l_returnflag': 'category', 'l_linestatus': 'category'})
    # orders = pd.read_csv("orders.tbl", sep='|',
    #                      names=["o_orderkey", "o_custkey", "o_orderstatus", "o_totalprice", "o_orderdate",
    #                             "o_orderpriority", "o_clerk", "o_shippriority", "o_comment"],
    #                      dtype={'o_orderstatus': 'category', 'o_orderpriority': 'category'})

    # o_orderkey = orders["o_orderkey"].values.astype('int32')
    # l_orderkey = lineitem["l_orderkey"].values.astype('int32')
    nation = pd.read_csv("nation.tbl", sep='|', names=["n_nationkey", "n_name", "n_regionkey", "n_comment"])
    supplier = pd.read_csv("supplier.tbl", sep='|',
                           names=["s_suppkey", "s_name", "s_address", "s_nationkey", "s_phone", "s_acctbal", "s_comment"])
    s_suppkey = supplier["s_suppkey"].values.astype('float32')
    s_nationkey = supplier["s_nationkey"].values.astype('float32')
    n_nationkey = nation["n_nationkey"].values.astype('float32')

    l_quantity = lineitem["l_quantity"].values.astype('float32')
    l_returnflag = lineitem["l_returnflag"].values.astype('S1')
    l_returnflag[l_returnflag == "A"] = "1"
    l_returnflag[l_returnflag == "N"] = "2"
    l_returnflag[l_returnflag == "R"] = "3"
    l_returnflag = l_returnflag.astype(np.float32, copy=False)

# This is not implemented on TPUs, which means returning a list of elements would have to be performed on CPUs
# Boolean Mask also not supported
# filter = tf.where(tf.logical_and(tf.greater_equal(quantity,10),tf.less_equal(quantity,24)))
def filter_sum_computation(quantity):
    zeros = tf.zeros_like(quantity)
    result = tf.reduce_sum(
        tf.where(tf.logical_and(tf.greater_equal(quantity, 10), tf.less_equal(quantity, 24)), quantity, zeros))
    return result

def filter_sum():
    inputs = [tf.convert_to_tensor(l_quantity, np.float32)]
    tpu_computation = tpu.rewrite(filter_sum_computation, inputs)
    tpu_grpc_url = TPUClusterResolver(
        tpu=[os.environ['TPU_NAME']]).get_master()
    with tf.Session(tpu_grpc_url) as sess:
        sess.run(tpu.initialize_system())
        sess.run(tf.global_variables_initializer())
        for i in range (0,5):
            res = sess.run(tpu_computation)
        sess.run(tpu.shutdown_system())
        print(res)
        return res

def filter_computation(quantity):
    result = tf.logical_and(tf.greater_equal(quantity, 10), tf.less_equal(quantity, 24))
    return result

def filter():
    inputs = [tf.convert_to_tensor(l_quantity, np.float32)]
    tpu_computation = tpu.rewrite(filter_computation, inputs)
    tpu_grpc_url = TPUClusterResolver(
        tpu=[os.environ['TPU_NAME']]).get_master()
    with tf.Session(tpu_grpc_url) as sess:
        sess.run(tpu.initialize_system())
        sess.run(tf.global_variables_initializer())
        for i in range (0,5):
            res = sess.run(tpu_computation)
        sess.run(tpu.shutdown_system())
        print(l_quantity[res])
        return res

def group_by_computation(quantity, returnflag, unique_groups):
    ones = tf.ones_like(returnflag)
    zeros = tf.zeros_like(returnflag)
    returnflag_groups = tf.unstack(unique_groups, 3)
    result = []
    for returnflag_group in returnflag_groups:
        returnflag_filter = tf.cast(tf.where(tf.equal(returnflag, returnflag_group), ones, zeros), tf.bool)
        sum_qty = tf.reduce_sum(tf.where(returnflag_filter, quantity, zeros))
        result.extend([returnflag_group, sum_qty])
    return result

def group_by():
    unique_groups = np.unique(l_returnflag)
    inputs = [tf.convert_to_tensor(l_quantity, np.float32), tf.convert_to_tensor(l_returnflag, np.float32),
              tf.convert_to_tensor(unique_groups, np.float32)]
    tpu_computation = tpu.rewrite(group_by_computation, inputs)
    tpu_grpc_url = TPUClusterResolver(
        tpu=[os.environ['TPU_NAME']]).get_master()
    with tf.Session(tpu_grpc_url) as sess:
        sess.run(tpu.initialize_system())
        sess.run(tf.global_variables_initializer())
        for i in range (0,5):
            res = sess.run(tpu_computation)
        sess.run(tpu.shutdown_system())
        print(res)

def aggregation_computation(quantity):
    result = tf.reduce_sum(quantity)
    return result

def aggregation():
    inputs = [tf.convert_to_tensor(l_quantity, np.float32)]
    tpu_computation = tpu.rewrite(aggregation_computation, inputs)
    tpu_grpc_url = TPUClusterResolver(
        tpu=[os.environ['TPU_NAME']]).get_master()
    with tf.Session(tpu_grpc_url) as sess:
        sess.run(tpu.initialize_system())
        sess.run(tf.global_variables_initializer())
        for i in range (0,5):
            res = sess.run(tpu_computation)
        sess.run(tpu.shutdown_system())
        print(res)
        return res

def order_by_limit_computation(quantity):
    result, indices = tf.nn.top_k(quantity, 10, True)
    return result

def order_by_limit():
    inputs = [tf.convert_to_tensor(l_quantity, np.float32)]
    tpu_computation = tpu.rewrite(order_by_limit_computation, inputs)
    tpu_grpc_url = TPUClusterResolver(
        tpu=[os.environ['TPU_NAME']]).get_master()
    with tf.Session(tpu_grpc_url) as sess:
        sess.run(tpu.initialize_system())
        sess.run(tf.global_variables_initializer())
        for i in range (0,5):
            res = sess.run(tpu_computation)
        sess.run(tpu.shutdown_system())
        print(res)
        return res

def join_computation(sup_nationkey, nat_nationkey):
    zeros = tf.zeros_like(sup_nationkey)
    nations = tf.unstack(nat_nationkey, 25) # Number of nations
    index_summation = (tf.constant(1), tf.constant(0.0))
    result = tf.constant([], dtype=tf.int32)

    def condition(index, summation):
        return tf.less(index, 25)

    def body(index, summation):
        nation=tf.gather(nations,index)
        a = tf.equal(sup_nationkey, nation)
        summand = tf.reduce_sum(tf.where(a, sup_nationkey, zeros))

        return tf.add(index, 1), tf.add(summation, summand)

    result =  tf.while_loop(condition, body, index_summation,parallel_iterations=25,maximum_iterations=25)[1]
    return result

def join():
    inputs = [tf.convert_to_tensor(s_nationkey, np.float32), tf.convert_to_tensor(n_nationkey, np.float32)]
    tpu_computation = tpu.rewrite(join_computation, inputs)
    tpu_grpc_url = TPUClusterResolver(
        tpu=[os.environ['TPU_NAME']]).get_master()
    with tf.Session(tpu_grpc_url) as sess:
        sess.run(tpu.initialize_system())
        sess.run(tf.global_variables_initializer())
        for i in range (0,5):
            res = sess.run(tpu_computation)
        sess.run(tpu.shutdown_system())
        print(res)
        return res

def run_micro(scale):
    load_input(scale)
    # filter_sum()
    # filter()
    # aggregation()
    order_by_limit()
    # group_by()
    # join()

if __name__ == "__main__":
    scale = int(sys.argv[1])
    run_micro(scale)


import openpyxl

# Apriamo il workbook
wb = openpyxl.load_workbook("../Prova.xlsx")

# quali sono gli sheet?
print(wb.sheetnames)

# Selezionare il tuo sheet
ws = wb['Statistica per azione']

print(ws)
# Ci possiamo importare un altro sheet.
ws1 = wb['Statistiche']

# Possiamo creare un altro script
wb.create_sheet("Sheet_Nuovo")

# Così scelgo la posizione dello sheet.
wb.create_sheet("Sheet_Nuovo", 0)
print(wb.sheetnames)

# Alla fine dobbiamo salvare
wb.save("../Prova.xlsx")


from PIL import Image
import streamlit as st
import os

st.title("Image to Pdf file Converter ")
filename = st.sidebar.file_uploader("Load your image File Here")
if filename is not None:
        image = Image.open(filename)
else:

        image = Image.open("output.png")

st.image(image, caption='UPLOADED FILE ! ')

if image.mode == "RGBA":
        image = image.convert("RGB")

image = image.save("output.pdf")

with open("output.pdf", "rb") as file:
        st.title("Converted File Here -> ")
        btn = st.download_button(
                label="Download pdf file !",
                data=file,
                file_name="output.pdf",
                mime="image/pdf"
                )


import pygame
from .Clock import Clock
from .Vector import Vector2D
from .Font import Font
from ..manager.camera import Camera

class Playmode:
    modes = [
        {
            "name": "2x Fast",
            "priority": 0,
            "image_source": None,
            "image": None,
            "position": [0,0],
            "size": 0,

            "rocket-speed": 150,
            "resource-amount": [(12, 20),(7, 15)],
            "resource-cooldown": [2,4],
            "enemy-stats": {
                "enemy-speed": 200,
                "enemy-cooldown-move": [1,2],
                "enemy-start-time": 15,
                "enemy-limit-time": 10,
            },
        },
        {
            "name": "Relaxing",
            "priority": 0,
            "image_source": None,
            "image": None,
            "position": [0,0],
            "size": 0,

            "rocket-speed": 100,
            "resource-amount": [(25, 40),(15, 30)],
            "resource-cooldown": [3,5],
            "enemy-stats": {
                "enemy-speed": 100,
                "enemy-cooldown-move": [3,7],
                "enemy-start-time": 25,
                "enemy-limit-time": 10,
            },
        },
        {
            "name": "Invasion",
            "priority": 0,
            "image_source": None,
            "image": None,
            "position": [0,0],
            "size": 0,

            "rocket-speed": 150,
            "resource-amount": [(20, 30),(15, 20)],
            "resource-cooldown": [1,3],
            "enemy-stats": {
                "enemy-speed": 50,
                "enemy-cooldown-move": [0,2],
                "enemy-start-time": 5,
                "enemy-limit-time": 1,
            },
        },
        {
            "name": "Hardcore",
            "priority": 0,
            "image_source": None,
            "image": None,
            "position": [0,0],
            "size": 0,

            "rocket-speed": 150,
            "resource-amount": [(5, 10),(10, 15)],
            "resource-cooldown": [0.5,1],
            "enemy-stats": {
                "enemy-speed": 200,
                "enemy-cooldown-move": [0,1],
                "enemy-start-time": 5,
                "enemy-limit-time": 1,
            },
        },
        # {
        #     "name": "Playtest",
        #     "priority": 0,
        #     "image_source": None,
        #     "image": None,
        #     "position": [0,0],
        #     "size": 0,

        #     "rocket-speed": 200,
        #     "resource-amount": [(1, 1),(1, 1)],
        #     "resource-cooldown": [0.1,0.1],
        #     "enemy-stats": {
        #         "enemy-speed": 50,
        #         "enemy-cooldown-move": [5,7],
        #         "enemy-start-time": 5,
        #         "enemy-limit-time": 5,
        #     },
        # },
    ]

    selected_mode = 1
    mid_position = None
    offset_position = 100
    size = 0
    size_offset = 0
    speed_swipe = 10

    button = None
    is_click = False

    @classmethod
    def init(cls):
        x = Camera.instance.display.get_width() / 2
        y = (Camera.instance.display.get_height() / 2) + 30
        cls.mid_position = Vector2D((x, y))

        cls.size = y / 5
        cls.size_offset = cls.size / 3

        cls.button = pygame.image.load('assets/button.png').convert_alpha()
        cls.button.set_alpha(50)

        image_mode = [
            pygame.image.load('assets/mars.png').convert_alpha(),
            pygame.image.load('assets/earth.png').convert_alpha(),
            pygame.image.load('assets/enemy_ship.png').convert_alpha(),
            pygame.image.load('assets/rocket.png').convert_alpha(),
            # pygame.image.load('assets/neptune.png').convert_alpha(),
        ]

        priority = -1
        for i in range(len(cls.modes)):
            cls.modes[i]["image_source"] = image_mode[i]
            cls.modes[i]["image"] = image_mode[i].copy()
            cls.modes[i]["position"] = cls.mid_position
            cls.modes[i]["priority"] = priority
            priority += 1

    @classmethod
    def update(cls):
        cls.movement()
        cls.render()

    @classmethod
    def movement(cls):
        for mode in cls.modes:
            if not (mode["priority"] >= -1 and mode["priority"] <= 1): continue

            # left
            speed = cls.speed_swipe * Clock.delta_time
            if (mode["priority"] == -1):
                mode["position"] = [
                    Vector2D.LerpPoint(
                        mode["position"][0],
                        cls.mid_position.x - cls.offset_position,
                        speed
                    ), mode["position"][1]
                ]
                mode["size"] = Vector2D.LerpPoint(mode["size"], cls.size_offset, speed)
            # mid
            elif (mode["priority"] == 0):
                mode["position"] = [
                    Vector2D.LerpPoint(
                        mode["position"][0],
                        cls.mid_position.x,
                        speed
                    ), mode["position"][1]
                ]
                mode["size"] = Vector2D.LerpPoint(mode["size"], cls.size, speed)
            # right
            elif (mode["priority"] == 1):
                mode["position"] = [
                    Vector2D.LerpPoint(
                        mode["position"][0],
                        cls.mid_position.x + cls.offset_position,
                        speed
                    ), mode["position"][1]
                ]
                mode["size"] = Vector2D.LerpPoint(mode["size"], cls.size_offset, speed)

    @classmethod
    def render(cls):
        # draw left, mid, right mode
        for mode in cls.modes:
            if not (mode["priority"] >= -1 and mode["priority"] <= 1): continue
            mode["image"] = pygame.transform.scale(mode["image_source"], (mode["size"], mode["size"]))
            image_rect = mode["image"].get_rect(center = mode["position"])
            Camera.instance.display.blit(mode["image"], image_rect)
            # print(f"Mode: {mode['name']} \nPosition: {mode['position']} \nPriority: {mode['priority']}")

            if (mode["priority"] != 0): continue
            x = mode["position"][0]
            y = mode["position"][1] + image_rect.h + 10
            text_name = Font.head.render(mode["name"], False, (255,255,255))
            name_rect = text_name.get_rect(center = (x, y))
            Camera.instance.display.blit(text_name, name_rect)

    @classmethod
    def input(cls):
        mouse_pos = Camera.instance.screen_to_world_point(pygame.mouse.get_pos())
        mouse_click = pygame.mouse.get_pressed()

        for idx in range(len(cls.modes)):
            if not (cls.modes[idx]["priority"] >= -1 and cls.modes[idx]["priority"] <= 1): continue
            button = pygame.transform.scale(cls.button, (cls.size, cls.size))
            if (cls.modes[idx]["priority"] == 0):
                button = pygame.transform.scale(cls.button, (cls.size * 1.5, cls.size * 1.5))
            button_rect = button.get_rect(center = cls.modes[idx]["position"])

            # hover
            if (button_rect.collidepoint( Camera.instance.world_to_screen_point(mouse_pos))):
                Camera.instance.display.blit(button, button_rect)

                # click
                if (mouse_click[0] == 1 and not cls.is_click):
                    cls.is_click = True
                    if (cls.modes[idx]["priority"] == 1):
                        cls.select_right_mode()
                    elif (cls.modes[idx]["priority"] == -1):
                        cls.select_left_mode()
                    elif (cls.modes[idx]["priority"] == 0):
                        cls.selected_mode = idx
                        return True
                elif (mouse_click[0] == 0 and cls.is_click):
                    cls.is_click = False

    @classmethod
    def select_right_mode(cls):
        for mode in cls.modes:
            priority = mode["priority"] - 1
            if (priority < -1):
                priority = priority + len(cls.modes)
                mode["position"] = [cls.mid_position.x + (cls.offset_position * 1.5), mode["position"][1]]
            mode["priority"] = priority
            print(f"Right: Mode: {mode['name']}, Priority: {mode['priority']}")

    @classmethod
    def select_left_mode(cls):
        for mode in cls.modes:
            priority = mode["priority"] + 1
            if (priority > 1):
                priority = priority - len(cls.modes)
                mode["position"] = [cls.mid_position.x - (cls.offset_position * 1.5), mode["position"][1]]
            mode["priority"] = priority
            print(f"Left: Mode: {mode['name']}, Priority: {mode['priority']}")


# Apr 16 - Comments, Variable, Identifier

# print("Hello Python")
# print("Hello Purnendu")

'''
x="purnendu"
print(x)
'''
'''
x=10
print("purnendu")
print(x)

x="krishnendu"
print(x)
'''

# Apr 17 - Hands on
# (10+5)*4 - using 3 variables, 2 variables, 1 variable
# 3 Variables -> (10+5)*4
# a = 10
# b = 5
# c = 4
a,b,c = 10,5,4
print((a+b)*c)

# 2 Variables -> (10+5)*4
p = 10
q = 5
p = p + q
p = p * 4
print(p)

# 1 Variables -> (10+5)*4
x = 10
x = x + 5
x = x * 4
print(x)

# Swap two number (with and without additional variable)
m = 10
n = 0
print(m,n) # 10, 5
# x = m
# m = n
# n = x
m = m + n
n = m - n
m = m - n
'''
10 = m      # Static Error
m = m * n
n = m // n  # Runtime Error - Exception Handling
m = m // n
'''
print(m,n) # 5, 10

# Unpack value with dummy placeholder
f,_,g = 10,5,4

b = 10
print(b, 4)


from builtins import next
from builtins import str
from builtins import range
import sqlite3
import numpy as np

from lsst.sims.catalogs.db import CatalogDBObject

__all__ = ["getOneChunk", "writeResult", "sampleSphere", "myTestGals",
           "makeGalTestDB", "myTestStars", "makeStarTestDB"]

def getOneChunk(results):
    try:
        chunk = next(results)
    except StopIteration:
        raise RuntimeError("No results were returned.  Cannot run tests.  Try increasing the size of the"
                           " test database")
    return chunk

def writeResult(result, fname):
    fh = open(fname, 'w')
    first = True
    for chunk in result:
        if first:
            fh.write(",".join([str(el) for el in chunk.dtype.names])+"\n")
            first = False
        for i in range(len(chunk)):
            fh.write(",".join([str(chunk[name][i]) for name in chunk.dtype.names])+"\n")
    fh.close()

def sampleSphere(size, ramin = 0., dra = 2.*np.pi, rng=None):
    # From Shao 1996: "Spherical Sampling by Archimedes' Theorem"
    if rng is None:
        rng = np.random.RandomState(42)

    ra = rng.random_sample(size)*dra
    ra += ramin
    ra %= 2*np.pi
    z = rng.random_sample(size)*2. - 1.
    dec = np.arccos(z) - np.pi/2.
    return ra, dec

def sampleFocus(size, raCenter, decCenter, radius, rng=None):
    """
    Sample points in a focused field of view
    @param [in] raCenter is the RA at the center of the field of view in radians
    @param [in] decCenter is the Dec at the center of the field of view in radians
    @param [in] radius is the radius of the field of view in radians
    @param [in] rng is a random number generator (an instance of np.random.RandomState)
    @param [out] returns numpy arrays of ra and decs in radians
    """
    if rng is None:
        rng = np.random.RandomState(1453)

    theta = rng.random_sample(size)
    rc = np.radians(raCenter)
    dc = np.radians(decCenter)
    rr = np.radians(radius)*rng.random_sample(size)
    ra = np.empty(size)
    dec = np.empty(size)
    for i, th in enumerate(theta):
        ra[i] = rc + rr*np.cos(th)
        dec[i] = dc + rr*np.sin(th)

    return ra, dec

class myTestGals(CatalogDBObject):
    objid = 'testgals'
    tableid = 'galaxies'
    idColKey = 'id'
    # Make this implausibly large?
    appendint = 1022
    objectTypeId = 45
    driver = 'sqlite'
    database = 'testDatabase.db'
    raColName = 'ra'
    decColName = 'decl'
    spatialModel = 'SERSIC2D'
    columns = [('id', None, int),
               ('raJ2000', 'ra*%f'%(np.pi/180.)),
               ('decJ2000', 'decl*%f'%(np.pi/180.)),
               ('umag', None),
               ('gmag', None),
               ('rmag', None),
               ('imag', None),
               ('zmag', None),
               ('ymag', None),
               ('magNormAgn', 'mag_norm_agn', None),
               ('magNormDisk', 'mag_norm_disk', None),
               ('magNormBulge', 'mag_norm_bulge', None),
               ('redshift', None),
               ('a_disk', None),
               ('b_disk', None),
               ('a_bulge', None),
               ('b_bulge', None)]

def makeGalTestDB(filename='testDatabase.db', size=1000, seedVal=None,
                  raCenter=None, decCenter=None, radius=None, **kwargs):
    """
    Make a test database to serve information to the myTestGals object
    @param size: Number of rows in the database
    @param seedVal: Random seed to use

    @param raCenter,decCenter: the center of the field of view in degrees (optional)
    @param radius: the radius of the field of view in degrees (optional)

    These last optional parameters exist in the event that you want to make sure
    that the objects are clustered around the bore site for a unit test
    """
    conn = sqlite3.connect(filename)
    c = conn.cursor()
    try:
        c.execute('''CREATE TABLE galaxies
                  (id int, ra real, decl real, umag real, gmag real, rmag real,
                  imag real, zmag real, ymag real,
                  mag_norm_agn real, mag_norm_bulge real, mag_norm_disk real,
                  redshift real, a_disk real, b_disk real, a_bulge real, b_bulge real, varParamStr text)''')
        conn.commit()
    except:
        raise RuntimeError("Error creating database.")

    if seedVal is not None:
        rng = np.random.RandomState(seedVal)
    else:
        rng = np.random.RandomState(3321)

    if raCenter is None or decCenter is None or radius is None:

        ra, dec = sampleSphere(size, rng=rng, **kwargs)
    else:
        rc = np.radians(raCenter)
        dc = np.radians(decCenter)
        ra, dec = sampleFocus(size, rc, dc, radius, rng=rng)
    # Typical colors for main sequece stars
    umg = 1.5
    gmr = 0.65
    rmi = 1.0
    imz = 0.45
    zmy = 0.3
    mag_norm_disk = rng.random_sample(size)*6. + 18.
    mag_norm_bulge = rng.random_sample(size)*6. + 18.
    mag_norm_agn = rng.random_sample(size)*6. + 19.
    redshift = rng.random_sample(size)*2.5

    a_disk = rng.random_sample(size)*2.
    flatness = rng.random_sample(size)*0.8  # To prevent linear galaxies
    b_disk = a_disk*(1 - flatness)

    a_bulge = rng.random_sample(size)*1.5
    flatness = rng.random_sample(size)*0.5
    b_bulge = a_bulge*(1 - flatness)

    # assume mag norm is g-band (which is close to true)
    mag_norm = -2.5*np.log10(np.power(10, mag_norm_disk/-2.5) + np.power(10, mag_norm_bulge/-2.5) +
                             np.power(10, mag_norm_agn/-2.5))
    umag = mag_norm + umg
    gmag = mag_norm
    rmag = gmag - gmr
    imag = rmag - rmi
    zmag = imag - imz
    ymag = zmag - zmy
    for i in range(size):
        period = rng.random_sample(1)[0]*490. + 10.
        amp = rng.random_sample(1)[0]*5. + 0.2

        # note that we are storing period and amp at a fixed string length
        # to facilitate a consistent dtype when we read them back in
        # via DBObject
        paramStr = '{"varMethodName": "testVar", "pars": {"period": %.6e, "amplitude": %.6e}}' % (period, amp)

        qstr = '''INSERT INTO galaxies VALUES (%i, %f, %f, %f,
                     %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f,
                     %f, %f, '%s')''' % \
               (i, np.degrees(ra[i]), np.degrees(dec[i]), umag[i], gmag[i], rmag[i], imag[i],
                zmag[i], ymag[i], mag_norm_agn[i], mag_norm_bulge[i], mag_norm_disk[i], redshift[i],
                a_disk[i], b_disk[i], a_bulge[i], b_bulge[i], paramStr)

        c.execute(qstr)

    c.execute('''CREATE INDEX gal_ra_idx ON galaxies (ra)''')
    c.execute('''CREATE INDEX gal_dec_idx ON galaxies (decl)''')
    conn.commit()
    conn.close()

class myTestStars(CatalogDBObject):
    objid = 'teststars'
    tableid = 'stars'
    idColKey = 'id'
    # Make this implausibly large?
    appendint = 1023
    objectTypeId = 46
    driver = 'sqlite'
    database = 'testDatabase.db'
    raColName = 'ra'
    decColName = 'decl'
    columns = [('id', None, int),
               ('raJ2000', 'ra*%f'%(np.pi/180.)),
               ('decJ2000', 'decl*%f'%(np.pi/180.)),
               ('parallax', 'parallax*%.15f'%(np.pi/(648000000.0))),
               ('properMotionRa', 'properMotionRa*%.15f'%(np.pi/180)),
               ('properMotionDec', 'properMotionDec*%.15f'%(np.pi/180.)),
               ('umag', None),
               ('gmag', None),
               ('rmag', None),
               ('imag', None),
               ('zmag', None),
               ('ymag', None),
               ('magNorm', 'mag_norm', float)]

def makeStarTestDB(filename='testDatabase.db', size=1000, seedVal=None,
                   raCenter=None, decCenter=None, radius=None, **kwargs):
    """
    Make a test database to serve information to the myTestStars object
    @param size: Number of rows in the database
    @param seedVal: Random seed to use

    @param raCenter,decCenter: the center of the field of view in degrees (optional)
    @param radius: the radius of the field of view in degrees (optional)

    These last optional parameters exist in the event that you want to make sure
    that the objects are clustered around the bore site for a unit test
    """
    conn = sqlite3.connect(filename)
    c = conn.cursor()
    try:
        c.execute('''CREATE TABLE stars
                     (id int, ra real, decl real, umag real, gmag real, rmag real,
                     imag real, zmag real, ymag real, mag_norm real,
                     radialVelocity real, properMotionDec real, properMotionRa real, parallax real,
                     varParamStr text, ebv real)''')
        conn.commit()
    except:
        raise RuntimeError("Error creating database.")

    if seedVal is not None:
        rng = np.random.RandomState(seedVal)
    else:
        rng = np.random.RandomState(88)

    if raCenter is None or decCenter is None or radius is None:
        ra, dec = sampleSphere(size, rng=rng, **kwargs)
    else:
        rc = np.radians(raCenter)
        dc = np.radians(decCenter)
        ra, dec = sampleFocus(size, rc, dc, radius, rng=rng)

    # Typical colors
    umg = 1.5
    gmr = 0.65
    rmi = 1.0
    imz = 0.45
    zmy = 0.3
    mag_norm = rng.random_sample(size)*6. + 18.
    # assume mag norm is g-band (which is close to true)
    umag = mag_norm + umg
    gmag = mag_norm
    rmag = gmag - gmr
    imag = rmag - rmi
    zmag = imag - imz
    ymag = zmag - zmy
    radVel = rng.random_sample(size)*50. - 25.
    pmRa = rng.random_sample(size)*4./(1000*3600.)  # deg/yr
    pmDec = rng.random_sample(size)*4./(1000*3600.)  # deg/yr
    parallax = rng.random_sample(size)*1.0  # milliarcseconds per year
    ebv = rng.random_sample(size)*3.0
    for i in range(size):
        period = rng.random_sample(1)[0]*490. + 10.
        amp = rng.random_sample(1)[0]*5. + 0.2

        # note that we are storing period and amp at a fixed string length
        # to facilitate a consistent dtype when we read them back in
        # via DBObject
        paramStr = '{"varMethodName": "testVar", "pars": {"period": %.6e, "amplitude": %.6e}}' % (period, amp)

        qstr = '''INSERT INTO stars VALUES (%i, %f, %f, %f, %f, %f, %f,
               %f, %f, %f, %f, %.15f, %.15f, %.15f, '%s', %f)''' % \
               (i, np.degrees(ra[i]), np.degrees(dec[i]), umag[i], gmag[i], rmag[i],
                imag[i], zmag[i], ymag[i], mag_norm[i], radVel[i], pmRa[i], pmDec[i], parallax[i],
                paramStr, ebv[i])

        c.execute(qstr)

    c.execute('''CREATE INDEX star_ra_idx ON stars (ra)''')
    c.execute('''CREATE INDEX star_dec_idx ON stars (decl)''')
    conn.commit()
    conn.close()


#!/usr/bin/env python3
import argparse
import numpy as np

class Color:

    use_colors = True
    GREEN = '\033[92m'    if use_colors else ''
    YELLOW = '\033[93m'   if use_colors else ''
    RED = '\033[91m'      if use_colors else ''
    BOLD = '\033[1m'      if use_colors else ''
    UNDERLINE = '\033[4m' if use_colors else ''
    END = '\033[0m'       if use_colors else ''

def get_optimal_align_size(lifts):

    lengths = [[[len(z) for z in y] for y in x] for x in lifts]
    return max(sum(sum(lengths, []), [])) + 3 # pad with extra 3 spaces

def get_lifts_for_day(lifts):
    x = []
    for excs in lifts:
        idx = np.random.randint(0, len(excs))
        x.append(excs[idx])
    return x

def this_lift_has_fixed_rep(lift):
    ret = None
    rep = None
    if isinstance(lift, tuple):
        ret, rep = lift
    else:
        ret = lift
    return ret, rep

def day_program(lifts, mode):

    is_main_mode = mode == 'm'
    indent = ' ' * 10
    align = get_optimal_align_size(lifts)
    print()
    print(Color.GREEN + indent + ('Main Day' if is_main_mode else 'Accessory Day') + Color.END)
    for lift in get_lifts_for_day(lifts[0] if is_main_mode else lifts[1]):
        x, rep = this_lift_has_fixed_rep(lift)
        if rep is None:
            rep = np.random.randint(1, 4) * 4 if is_main_mode else 10
        print(indent + ('{:' + str(align) + '}').format(x) + '  ' +
              '[ {:2} ]'.format(rep))
    print()

def create_weekly_program(lifts, n_days=4):

    is_main_mode = True
    main_mode = 'm'
    accessory_mode = 'a'

    for i in range(n_days):
        day_program(lifts, main_mode if is_main_mode else accessory_mode)
        is_main_mode = not is_main_mode

def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("-m", "--mode", default='w', help="main, accessory or full week schedule")
    args = parser.parse_args()
    if args.mode not in [
            'w', # full week schedule
            'm', # main day
            'a'  # accessory day
    ]:
        raise ValueError('Wrong mode')

    lifts =  [
        [ # main lifts
            ( 'Squat', 'Front Squat', 'Pause Squat', 'Pause Front Squat' ), # squat
            ( 'Deadlift', 'Romanian Deadlift', 'Stiff-Legged Deadlift'   ), # deadlift
            ( 'Bench', ('DB Bench', 8), 'CG Bench', 'Pause Bench'        ), # bench
            ( 'T-Bar Row', 'Pull-Up'                                     )  # pull
        ],
        [ # accessories
            ( 'Press', 'DB Press', 'Seated Press', 'Front Raise'        ), # shoulder
            ( 'EZ-Bar Curl', 'DB Curl', 'BB Curl', 'Hammer Curl'        ), # bi
            ( 'Dip', 'LTE', 'DB LTE', 'Push-Up'                         ), # tri
            ( 'DB Row', ('Power Clean', 4)                              ), # pull
            ( 'Torque', 'Hanging Leg Raise', 'Sit-Up', 'Plank'          )  # abs
        ]
    ]

    if args.mode == 'w':
        create_weekly_program(lifts)
    else:
        day_program(lifts, args.mode)

if __name__ == '__main__':

    main()


from django.shortcuts import render, get_object_or_404
from django.http import Http404
from .models import *
import time
import datetime as dt
from itertools import chain
import matplotlib.pyplot as plt
import pandas as pd
import os

def home(request):
    return render(request, 'home.html')

#=================================================================================================================================

def page_article(request, doi):
    article = Article.objects.get(doi=doi)
    types = Type.objects.filter(types_article__article_id=article)
    themes = Theme.objects.filter(sous_theme__sous_themes_article__article_id=article)
    sous_themes = Sous_Theme.objects.filter(sous_themes_article__article_id=article)
    auteurs = Auteur.objects.filter(auteurs_article__article_id=article)
    labos = Labo.objects.filter(labos_article__article_id=article)

    count=['','','','','']
    if len(types)==1:
        count[0] = 'Type :'
    if len(types)!=0:
        count[0] = 'Types :'
    if len(themes)==1:
        count[1] = 'Theme :'
    if len(themes)!=0:
        count[1] = 'Themes :'
    if len(sous_themes)==1:
        count[2] = 'Sous-Theme :'
    if len(sous_themes)!=0:
        count[2] = 'Sous-Themes :'
    if len(auteurs)==1:
        count[3] = 'Auteur :'
    elif len(auteurs)>1:
        count[3] = 'Auteurs (' + str(len(auteurs)) + ') :'
    if len(labos)==1:
        count[4] = 'Labo :'
    elif len(labos)>1:
        count[4] = 'Labos (' + str(len(labos)) + ') :'

    context = {
        'article':article,
        'auteurs':auteurs,
        'labos':labos,
        'types':types,
        'themes':themes,
        'sous_themes':sous_themes,
        'count':count
    }
    return render(request, 'page_article.html', context)

#=================================================================================================================================

def page_auteur(request, prenom, nom):
    auteur = Auteur.objects.get(prenom=prenom, nom=nom)
    articles = Article.objects.filter(auteurs_article__auteur_id=auteur)
    labos = Labo.objects.filter(labos_auteur__auteur_id=auteur)
    lmail=str(auteur.email).split(',')
    email_list = [email.replace('{', '').replace('}', '') for email in lmail]

    count=['','']
    if len(articles)==1:
        count[0] = 'Article :'
    elif len(articles)>1:
        count[0] = 'Articles (' + str(len(articles)) + ') :'
    if len(labos)==1:
        count[1] = 'Labo :'
    elif len(labos)>1:
        count[1] = 'Labos (' + str(len(labos)) + ') :'

    context = {
        'auteur':auteur,
        'email_list':email_list,
        'articles':articles,
        'labos':labos,
        "count":count
    }
    return render(request, 'page_auteur.html', context)

#=================================================================================================================================

def page_labo(request, nom_labo):
    labo = Labo.objects.get(laboratoire=nom_labo)
    articles = Article.objects.filter(labos_article__labo_id=labo)
    auteurs = Auteur.objects.filter(labos_auteur__labo_id=labo)
    pays = Pays.objects.filter(labos_pays__labo_id=labo)

    count=['','','']
    if len(articles)==1:
        count[0] = 'Article :'
    elif len(articles)>1:
        count[0] = 'Articles (' + str(len(articles)) + ') :'
    if len(auteurs)==1:
        count[1] = 'Auteur :'
    elif len(auteurs)>1:
        count[1] = 'Auteurs (' + str(len(auteurs)) + ') :'
    if len(pays)==1:
        count[2] = 'Pays :'
    else:
        count[2] = 'Pays (' + str(len(pays)) + ') :'

    context = {
        'labo': labo,
        'articles': articles,
        'auteurs': auteurs,
        'pays':pays,
        "count":count
    }
    return render(request, 'page_labo.html', context)

#=================================================================================================================================

def page_pays(request, pays):
    pays = Pays.objects.get(pays=pays)
    labos = Labo.objects.filter(labos_pays__pays_id=pays)

    if len(labos)==1:
        count = "Labo :"
    else:
        count = 'Labos (' + str(len(labos)) + ') :'

    context = {
        'pays':pays,
        'labos':labos,
        'count':count
    }
    return render(request, 'page_pays.html', context)

#=================================================================================================================================

def page_journal(request, journal):
    journal = Journal.objects.get(nom_journal=journal)
    articles = Article.objects.filter(journal_id=journal)

    if len(articles)==1:
        count = "Article :"
    else:
        count = 'Articles (' + str(len(articles)) + ') :'

    context = {
        'journal':journal,
        'articles':articles,
        'count':count
    }
    return render(request, 'page_journal.html', context)

#=================================================================================================================================

def SearchBar(request):
    recherche_raw = request.GET.get('recherche')
    choix = request.GET.get('choix')

    if choix=='titre' and recherche_raw:
        recherche = recherche_raw.split(' ')

        debut = time.time()
        titres = Article.objects.filter(titre__icontains=recherche[0])
        for mot_clé in recherche[1:]:
            titres = titres.filter(titre__icontains=mot_clé)
        temps = '(' + str(round(time.time()-debut, 2)) + 's)'

        if len(titres)==0:
            count = f"Aucun article trouvé pour '{recherche_raw}'"
            temps=''
        elif len(titres)==1:
            count = f"1 article trouvé pour '{recherche_raw}'"
        else:
            count = f"{str(len(titres))} articles trouvés pour '{recherche_raw}'"

        context = {
            'titres':titres,
            'count':count,
            'temps':temps
        }
        return render(request, 'SearchBar.html', context)

    if choix=='doi' and recherche_raw:
        debut = time.time()
        try:
            article = get_object_or_404(Article, doi=recherche_raw)
        except Http404:
            count = f"Aucun article trouvé pour '{recherche_raw}'"
            context = {
                'count':count,
            }
            return render(request, 'SearchBar.html', context)

        temps = '(' + str(round(time.time()-debut, 2)) + 's)'
        count = f"1 article trouvé pour '{recherche_raw}'"
        context = {
            'article':article,
            'count':count,
            'temps':temps
        }
        return render(request, 'SearchBar.html', context)
    return render(request, 'SearchBar.html')

#=================================================================================================================================

def AuthorSearch(request):
    prenom = request.GET.get('prenom')
    nom = request.GET.get('nom')

    if not prenom and not nom:
        return render(request, 'AuthorSearch.html')

    if prenom and nom:
        debut = time.time()
        try:
            auteur = get_object_or_404(Auteur, prenom=prenom, nom=nom)
        except Http404:
            count = f"Aucun auteur trouvé pour '{prenom} , {nom}'"
            context = {
                'count':count,
            }
            return render(request, 'AuthorSearch.html', context)
        temps = '(' + str(round(time.time()-debut, 2)) + 's)'
        count = f"1 auteur trouvé pour '{prenom} , {nom}'"
        context = {
            'auteur':auteur,
            'count':count,
            'temps':temps
        }
        print(auteur)
        return render(request, 'AuthorSearch.html', context)

    elif prenom:
        debut = time.time()
        auteurs = Auteur.objects.filter(prenom=prenom)
        temps = '(' + str(round(time.time()-debut, 2)) + 's)'
        if len(auteurs)==0:
            count = f"Aucun auteur trouvé pour {prenom}"
        elif len(auteurs)==1:
            count = f"1 auteur trouvé pour '{prenom}'"
        else:
            count = f"{len(auteurs)} auteurs trouvés pour '{prenom}'"
        context = {
            'auteurs':auteurs,
            'count':count,
            'temps':temps
        }
        return render(request, 'AuthorSearch.html', context)

    elif nom:
        debut = time.time()
        auteurs = Auteur.objects.filter(nom=nom)
        temps = '(' + str(round(time.time()-debut, 2)) + 's)'
        if len(auteurs)==0:
            count = f"Aucun auteur trouvé pour {nom}"
        elif len(auteurs)==1:
            count = f"1 auteur trouvé pour '{nom}'"
        else:
            count = f"{len(auteurs)} auteurs trouvés pour '{nom}'"
        context = {
            'auteurs':auteurs,
            'count':count,
            'temps':temps
        }
        return render(request, 'AuthorSearch.html', context)

    return render(request, 'AuthorSearch.html')

#=================================================================================================================================

def LabSearch(request):
    recherche_raw = request.GET.get('recherche')
    if recherche_raw:
        recherche=recherche_raw.split(' ')

        debut = time.time()
        labos = Labo.objects.filter(laboratoire__icontains=recherche[0])
        for mot in recherche[1:]:
            labos = labos.filter(laboratoire__icontains=mot)
        temps = '(' + str(round(time.time()-debut, 2)) + 's)'

        if len(labos)==0:
            count = f"Aucun labo trouvé pour '{recherche_raw}'"
            temps=''
        elif len(labos)==1:
            count = f"1 labo trouvé pour '{recherche_raw}'"
        else:
            count = f"{str(len(labos))} labos trouvés pour '{recherche_raw}'"

        context = {
            'labos':labos,
            'count':count,
            'temps':temps
        }
        return render(request, 'LabSearch.html', context)

    else:
        return render(request, 'LabSearch.html')

#=================================================================================================================================

def PaysSearch(request):
    AllPays = Pays.objects.all()
    context = {
        'AllPays':AllPays
    }
    return render(request, 'PaysSearch.html', context)

#=================================================================================================================================

def ThemeSearch(request):
    AllThemes = Theme.objects.all()
    AllSous_Themes = Sous_Theme.objects.all()
    recherche_theme = request.GET.get('theme')
    recherche_sous_theme = request.GET.get('sous_theme')
    recherche_titre_raw = request.GET.get('titre')
    context = {
        'AllThemes':AllThemes,
        'AllSous_Themes':AllSous_Themes,
    }
    if recherche_theme and recherche_sous_theme:
        theme = Theme.objects.get(nom_theme=recherche_theme)
        sous_theme = Sous_Theme.objects.get(nom_sous_theme=recherche_sous_theme)

        debut=time.time()
        articles = Article.objects.filter(sous_themes_article__sous_theme_id=sous_theme)

        if recherche_titre_raw:
            recherche_titre = recherche_titre_raw.split(' ')
            for mot_clé in recherche_titre:
                articles = articles.filter(titre__icontains=mot_clé)
        temps = '(' + str(round(time.time()-debut, 2)) + 's)'
        if len(articles)==0:
            count = f"Aucun article trouvé pour '{sous_theme}' , '{recherche_titre_raw}'"
        elif len(articles)==1:
            count = f"1 article trouvé pour '{sous_theme}' , '{recherche_titre_raw}'"
        elif len(articles)>1:
            count = f"{len(articles)} articles trouvés pour '{sous_theme}' , '{recherche_titre_raw}'"

        context = {
        'AllThemes':AllThemes,
        'AllSous_Themes':AllSous_Themes,
        'articles':articles,
        'count':count,
        'temps':temps
        }
        return render(request, 'ThemeSearch.html', context)
    return render(request, 'ThemeSearch.html', context)

#=================================================================================================================================

def DateSearchInit(request):
    histo = 'website/static/barchart.png'
    if os.path.isfile(histo):
        os.remove(histo)
    debut = request.GET.get('debut')
    fin = request.GET.get('fin')
    choix = request.GET.get('choix')
    choix_hist = request.GET.get('choix_hist')
    titre_raw = request.GET.get('titre')

    if debut and fin:
        debut = dt.datetime.strptime(debut, '%Y-%m-%d').date()
        fin = dt.datetime.strptime(fin, '%Y-%m-%d').date()
        debut_time = time.time()

        if choix!='histogramme':
            if choix == 'date_exacte':
                delta = fin.year-debut.year
                annees_a_couvrir = []
                articles_fd = Article_Fulldate.objects.filter(date__range=[debut,fin])
                articles_y = []

                if delta>=2:
                    for i in range(1,delta):
                        annees_a_couvrir.append(debut.year+i)
                    annees_a_couvrir = [annees_a_couvrir[0], annees_a_couvrir[-1]]
                    articles_y = Article_Year.objects.filter(date__range=annees_a_couvrir)

            elif choix =='année':
                articles_fd = Article_Fulldate.objects.filter(date__year__range=[debut.year,fin.year])
                articles_y = Article_Year.objects.filter(date__range=[debut.year,fin.year])

            if titre_raw:
                Ltitre = titre_raw.split(' ')
                for mot_clé in Ltitre:
                    articles_fd = articles_fd.filter(titre__icontains=mot_clé)
                    articles_y = articles_y.filter(titre__icontains=mot_clé)

                articles = list(chain(articles_fd, articles_y))
                temps = '(' + str(round(time.time()-debut_time, 2)) + 's)'

                if choix == 'date_exacte':
                    if len(articles)==0:
                        count = f"Aucun article publié entre le '{debut}' et le '{fin}' avec '{titre_raw}'"
                    elif len(articles)==1:
                        count = f"1 article publié entre le '{debut}' et le '{fin}' avec '{titre_raw}'"
                    elif len(articles)>1:
                        count = f"{len(articles)} articles publiés entre le '{debut}' et le '{fin}' avec '{titre_raw}'"

                elif choix == 'année':
                    if fin.year-debut.year==0:
                        if len(articles)==0:
                            count = f"Aucun article publié en '{debut.year}' avec '{titre_raw}'"
                        elif len(articles)==1:
                            count = f"1 article publié en '{debut.year}' avec '{titre_raw}'"
                        elif len(articles)>1:
                            count = f"{len(articles)} articles publiés en '{debut.year}' avec '{titre_raw}'"
                    else:
                        if len(articles)==0:
                            count = f"Aucun article publié entre '{debut.year}' et '{fin.year}' avec '{titre_raw}'"
                        elif len(articles)==1:
                            count = f"1 article publié entre '{debut.year}' et '{fin.year}' avec '{titre_raw}'"
                        elif len(articles)>1:
                            count = f"{len(articles)} articles publiés entre '{debut.year}' et '{fin.year}' avec '{titre_raw}'"

                context = {
                    'articles':articles,
                    'count':count,
                    'temps':temps
                }
                return render(request, 'DateSearch.html', context)

            articles = list(chain(articles_fd, articles_y))
            temps = '(' + str(round(time.time()-debut_time, 2)) + 's)'

            if choix == 'date_exacte':
                    if len(articles)==0:
                        count = f"Aucun article publié entre le '{debut}' et le '{fin}'"
                    elif len(articles)==1:
                        count = f"1 article publié entre le '{debut}' et le '{fin}'"
                    elif len(articles)>1:
                        count = f"{len(articles)} articles publiés entre le '{debut}' et le '{fin}'"

            elif choix == 'année':
                if fin.year-debut.year==0:
                    if len(articles)==0:
                        count = f"Aucun article publié en '{debut.year}'"
                    elif len(articles)==1:
                        count = f"1 article publié en '{debut.year}'"
                    elif len(articles)>1:
                        count = f"{len(articles)} articles publiés en '{debut.year}'"
                else:
                    if len(articles)==0:
                        count = f"Aucun article publié entre '{debut.year}' et '{fin.year}'"
                    elif len(articles)==1:
                        count = f"1 article publié entre '{debut.year}' et '{fin.year}'"
                    elif len(articles)>1:
                        count = f"{len(articles)} articles publiés entre '{debut.year}' et '{fin.year}'"

            context = {
                'articles':articles,
                'count':count,
                'temps':temps
            }
            return render(request, 'DateSearch.html', context)

        if choix == 'histogramme':
            if choix_hist == 'jour':
                delta = fin - debut
                articles_par_jour = []
                les_jours_en_str = []

                for i in range(delta.days + 1):
                    day = debut + dt.timedelta(days=i)
                    articles = Article_Fulldate.objects.filter(date=day)
                    articles_par_jour.append(len(articles))
                    les_jours_en_str.append(str(day))

                les_jours_courts = [jour[-5:] for jour in les_jours_en_str]
                plt.bar(les_jours_en_str, articles_par_jour)
                plt.xticks(range(0, len(les_jours_en_str), 60), les_jours_courts[::60])
                plt.title(f"Articles par jour du {debut} au {fin}")
                plt.savefig('website/static/barchart.png')
                plt.clf()

                return render(request, 'DateSearch.html')

            elif choix_hist == 'semaine':
                articles_par_jour = []
                les_jours_en_str = []
                periode_tps = pd.period_range(debut, fin, freq='w')
                date_debut = (str(periode_tps[0]).split('/'))[0]
                date_fin = (str(periode_tps[-1]).split('/'))[-1]
                week_count = 1
                for i in periode_tps:
                    week_split = str(i).split('/')
                    debut_semaine = dt.datetime.strptime(week_split[0], '%Y-%m-%d').date()
                    fin_semaine = dt.datetime.strptime(week_split[1], '%Y-%m-%d').date()
                    articles_par_jour.append(len(Article_Fulldate.objects.filter(date__range=[debut_semaine,fin_semaine])))
                    les_jours_en_str.append(str(week_count))
                    week_count+=1
                plt.bar(les_jours_en_str, articles_par_jour)
                if len(les_jours_en_str)>=23:
                    plt.xticks(range(0, len(les_jours_en_str), 3), les_jours_en_str[::3])
                plt.title(f"Articles par semaine du {date_debut} au {date_fin}")
                plt.savefig('website/static/barchart.png')
                plt.clf()
                return render(request, 'DateSearch.html')

            elif choix_hist == 'mois':
                articles_par_jour = []
                les_jours_en_str = []
                periode_tps = pd.period_range(debut, fin, freq='M')
                for date in periode_tps:
                    articles_par_jour.append(len(Article_Fulldate.objects.filter(date__year=date.year,date__month=date.month)))
                    les_jours_en_str.append(f'{date.year}-{date.month}')
                plt.bar(les_jours_en_str, articles_par_jour)
                plt.title(f"Articles par mois de {debut.month}/{debut.year} à {fin.month}/{fin.year}")
                plt.savefig('website/static/barchart.png')
                plt.clf()
                return render(request, 'DateSearch.html')

            elif choix_hist == 'année':
                articles_par_jour = []
                les_jours_en_str = []
                for annee in range(debut.year, fin.year+1):
                    nb_articles = len(Article_Fulldate.objects.filter(date__year=annee)) + len(Article_Year.objects.filter(date=annee))
                    articles_par_jour.append(nb_articles)
                    les_jours_en_str.append(str(annee))

                les_jours_courts = [jour[-2:] for jour in les_jours_en_str]
                plt.bar(les_jours_en_str, articles_par_jour)
                plt.xticks(range(0, len(les_jours_en_str), 2), les_jours_courts[::2])
                if len(range(debut.year, fin.year+1))==1:
                    plt.title(f"Articles en {debut.year}")
                else:
                    plt.title(f"Articles par année de {debut.year} à {fin.year}")
                plt.savefig('website/static/barchart.png')
                plt.clf()

                compteur=0
                for nb in articles_par_jour:
                    compteur+=nb
                print(compteur)
                return render(request, 'DateSearch.html')
    else :
        return render(request, 'DateSearch.html')

#=================================================================================================================================

def JournalSearch(request):
    recherche_raw = request.GET.get('recherche')
    if recherche_raw:
        recherche = recherche_raw.split(' ')
        debut=time.time()
        journaux = Journal.objects.filter(nom_journal__icontains=recherche[0])
        for mot_clé in recherche[1:]:
            journaux = journaux.filter(nom_journal__icontains=mot_clé)

        temps = '(' + str(round(time.time()-debut, 2)) + 's)'

        if len(journaux)==0:
                count = f"Aucun journal trouvé pour '{recherche_raw}'"
                temps=''
        elif len(journaux)==1:
            count = f"1 journal trouvé pour '{recherche_raw}'"
        else:
            count = f"{str(len(journaux))} journaux trouvés pour '{recherche_raw}'"

        context = {
            'journaux':journaux,
            'count':count,
            'temps':temps
        }
        return render(request, 'JournalSearch.html', context)
    return render(request, 'JournalSearch.html')

#=================================================================================================================================

def TypeSearch(request):
    AllTypes = Type.objects.all()
    recherche = request.GET.get('recherche')
    context = {
                'AllTypes':AllTypes,
            }

    if recherche:
        type = Type.objects.get(nom_type=recherche)
        articles = Article.objects.filter(types_article__type_id=type)
        dico = {}
        for article in articles:
            if str(article.journal_id) in dico:
                dico[str(article.journal_id)]+=1
            else:
                dico[str(article.journal_id)]=1

        dico_decroissant = dict(sorted(dico.items(), key=lambda item: item[1], reverse=True))

        journaux=list(dico_decroissant.keys())
        valeurs=list(dico_decroissant.values())

        liste=[]
        for i in range(len(journaux)):
            liste.append(journaux[i]+' | '+str(valeurs[i])+' articles')
        context = {
            'AllTypes':AllTypes,
            'type':type,
            'liste':liste
        }
        return render(request, 'TypeSearch.html', context)

    return render(request, 'TypeSearch.html', context)

#=================================================================================================================================

BASES DE DONNEES RELATIONNELLES/DJANGO/projetapp/website/views.py


import re
import time
import logging
import os
from scrapping.scrapy import Scraping
from sqlalchemy import create_engine
from datetime import datetime
from model import DAO

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_DIR = os.path.join(CURRENT_DIR, "log")
logging.basicConfig(filename=os.path.join(LOG_DIR, "file.log"), level=logging.INFO)

def altera_data(date_time: str) -> datetime:
    """
    Altera a string de data no formato dia/mes hora para o formato datetime do python \n
    :param date_time: str
    :return: report_date: datetime
    """
    date = date_time.split("-")[0].strip()
    time = date_time.split("-")[1].strip()
    report_date = datetime.strptime(date, "%d/%m")
    today = datetime.today()

    if today.month == 12 and report_date.month == 1:
        new_year = today.year + 1
        report_date = report_date.replace(year=new_year,
                                          hour=int(time.split(":")[0]),
                                          minute=int(time.split(":")[1]))
    else:
        report_date = report_date.replace(year=today.year,
                                          hour=int(time.split(":")[0]),
                                          minute=int(time.split(":")[1]))
    return report_date

def busca_cidade(cidade: str) -> int:
    """
    Verifica se a cidade ja existe no banco, caso o contrario, salva
    :param cidade: str
    :return: cidade_id: int
    """
    conn = DAO.db_connect()
    query_get_cidade = f"Select id from cidades where cidade = '{cidade}'"
    query_insert_cidade = f"insert into cidades (cidade) values ('{cidade}')"
    query = conn.execute(query_get_cidade)
    query_result = query.fetchone()
    try:
        cidade_id = query_result['id']
    except Exception:
        conn.execute(query_insert_cidade)
        cidade_id = conn.execute(query_get_cidade).fetchone()['id']

    return cidade_id

def busca_bairro(cidade_id: int, bairro: str) -> int:
    """
    Verifica se um bairro já existe para determinada cidade, caso contrario, salva no banco \n
    :param cidade_id: int
    :param bairro: str
    :return: bairro_id: int
    """
    conn = DAO.db_connect()
    query_get_bairro = f"Select id from bairros where cidade_id = {cidade_id} and bairro = '{bairro}'"
    query_insert_bairro = f"insert into bairros (cidade_id, bairro) values ({cidade_id}, '{bairro}')"
    query = conn.execute(query_get_bairro)
    query_result = query.fetchone()
    try:
        cidade_id = query_result['id']
    except Exception:
        conn.execute(query_insert_bairro)
        cidade_id = conn.execute(query_get_bairro).fetchone()['id']

    return cidade_id

def busca_area(area: str):
    """
    Verifica se uma área já existe no banco de dados, caso contrario, salva \n
    :param area: str
    :return: area_id: int
    """
    conn = DAO.db_connect()
    query_get_area = f"Select id from areas where area = '{area}'"
    query_insert_area = f"insert into areas (area) values ('{area}')"
    query = conn.execute(query_get_area)
    query_result = query.fetchone()
    try:
        area_id = query_result['id']
    except Exception:
        conn.execute(query_insert_area)
        area_id = conn.execute(query_get_area).fetchone()['id']

    return area_id

def area_iterate(registro: list):
    if 'area' in registro.keys():
        search_area = re.search("((Área.*)|(área.*))", registro['area'])
        if search_area:
            return busca_area(search_area.group(0).strip())

def salva_rodizio(data_inicio: datetime, data_fim: datetime, cidade_id: int, bairro_id: int, area_id: int):
    """
    Verifica se a informação de rodizio para uma determinada hora e lugar já existe no banco,
    caso contrario grava o registro \n
    :param data_inicio: datetime
    :param data_fim:  datetime
    :param cidade_id: int
    :param bairro_id: int
    :param area_id: int
    :return: None
    """
    conn = DAO.db_connect()
    date_format = "%Y-%m-%d %I:%M"

    search_query = """select * from rodizio where data_inicio = '{}'
                                            and data_fim = '{}'
                                            and cidade_id = {}
                                            and bairro_id = {}
                                            """
    if area_id:
        search_query = search_query + " and area_id = {}"

    query = conn.execute(search_query.format(
        data_inicio.strftime(date_format).strip(),
        data_fim.strftime(date_format).strip(),
        cidade_id,
        bairro_id,
        area_id))
    response = query.fetchall()
    if not response:
        if area_id:
            insert_query = """insert into rodizio (data_inicio,
                                    data_fim,
                                    cidade_id,
                                    bairro_id,
                                    area_id)
                            values ('{}','{}', {}, {}, {})
                            """
            conn.execute(insert_query.format(
                data_inicio.strftime(date_format).strip(),
                data_fim.strftime(date_format).strip(),
                cidade_id,
                bairro_id,
                area_id
            ))
        else:
            insert_query = """insert into rodizio (data_inicio,
                                    data_fim,
                                    cidade_id,
                                    bairro_id)
                            values ('{}','{}', {}, {})
                            """
            conn.execute(insert_query.format(
                data_inicio.strftime(date_format).strip(),
                data_fim.strftime(date_format).strip(),
                cidade_id,
                bairro_id,
                area_id
            ))

def main():
    today = datetime.today().strftime("%d/%m/%Y %H:%M:%S")
    logging.info(f"{today} - INICIANDO SCRAPPING DOS DADOS.")
    data = Scraping()
    dados = data.get_scrapy()
    logging.info(f"{today} - TRATANDO OS DADOS E SALVANDO NO BANCO.")
    for registro in dados:
        data_inicio = altera_data(registro['inicio'])
        data_fim = altera_data(registro['fim'])
        cidade_id = busca_cidade(registro['cidade'].strip().lower())

        area_id = area_iterate(registro)

        for bairro in registro['bairros']:
            bairro = bairro.strip().replace(".", "")
            match = re.match("(?:([a-zA-ZzáàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ\s]){1,})", bairro)

            if match:
                bairro = bairro[0:match.end()].strip()
                tem_separacao = re.search("([a-z\s]) e ([A-Z\s])", bairro)
                if tem_separacao:
                    for word in re.split("\se\s", bairro):
                        bairro_id = busca_bairro(cidade_id, word.lower())
                        salva_rodizio(data_inicio, data_fim, cidade_id, bairro_id, area_id)
                else:
                    bairro_id = busca_bairro(cidade_id, bairro.lower())
                    salva_rodizio(data_inicio, data_fim, cidade_id, bairro_id, area_id)

    logging.info(f"{today} - DADOS CARREGADOS COM SUCESSO.")

if __name__ == "__main__":
    start = time.time()
    main()
    logging.info(f"Tempo total: " + str(round(time.time() - start)) + " segundos")


from pip.util import dist_in_site_packages
from script.ppPrint import doPPrint
from script.seqTools import fastaContainer
from script.ScanSeed import *

__author__ = 'nath'

if __name__ == '__main__':
    fc = fastaContainer("../seq_p21.fa")
    mRNA = fc.findSeq("SM_000001")
    mRNA.CDS = 10
    mRNA.UTR3 = 947

    fc = fastaContainer("../data/miR_wu.fa")
    fout = open("res_scanSeed.txt","w")
    print ("mimat\tmer6\tmer7-m8\tmer7-A1\tmer8\tdist6MerMin")
    fout.write("mimat\tmer6\tmer7-m8\tmer7-A1\tmer8\tdist6MerMin\n")
    for mir in fc:

        res = scan(mRNA,mir)
        print (mir.name+"\t", end ='')
        fout.write(mir.name+"\t")
        fp = [0,0,0,0]
        dist6Mer = 100000
        lastPos6mer=0
        tabPos6mer = []
        for pos in res.keys():
            if pos >= mRNA.UTR3:
                #print ('',pos,res.get(pos))
                #doPPrint(mRNA.seq,mir.seq,pos)
                #if res.get(pos)== "6mer":
                tabPos6mer.append(pos)

                newFp = getFPScan(res.get(pos))

                fp = sumFPScan(fp,newFp)
        if len(tabPos6mer) > 1:

            for i in range(0,len(tabPos6mer)):
                for j in range(0,len(tabPos6mer)):
                    if i==j :
                        continue
                    else :
                        dist6Mer = min(dist6Mer,abs(tabPos6mer[i]-tabPos6mer[j]))

        print ("\t".join(map(str,fp))+"\t"+str(dist6Mer))
        fout.write("\t".join(map(str,fp))+"\t"+str(dist6Mer)+"\n")


from tkinter import *
from tkinter import ttk
from random import randint

window = Tk()
window.title('Numeral systems converter')
window.geometry('332x149')
window.resizable(width=False, height=False)

converter = LabelFrame(window, text='Converter', font=('Consolas', 10))
calculator = LabelFrame(window, text='Calculator', font=('Consolas', 10))
converter.grid()
calculator.grid()

def to_dec(base, number):
    count = 0
    result = 0
    index = 0

    BASE_32 = {
        '0': 0,
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9,
        'A': 10,
        'B': 11,
        'C': 12,
        'D': 13,
        'E': 14,
        'F': 15,
        'G': 16,
        'H': 17,
        'I': 18,
        'J': 19,
        'K': 20,
        'L': 21,
        'M': 22,
        'N': 23,
        'O': 24,
        'P': 25,
        'Q': 26,
        'R': 27,
        'S': 28,
        'T': 29,
        'U': 30,
        'V': 31,
        'W': 32,
        'X': 33,
        'Y': 34,
        'Z': 35
    }

    number = [item for item in str(number)][::-1]
    for item in number:
        number[index] = BASE_32[item]
        index += 1

    for item in number:
        result += item * base ** count
        count += 1

    return result

def from_dec(base, number):
    result = ''

    BASE_32 = {
        0: '0',
        1: '1',
        2: '2',
        3: '3',
        4: '4',
        5: '5',
        6: '6',
        7: '7',
        8: '8',
        9: '9',
        10: 'A',
        11: 'B',
        12: 'C',
        13: 'D',
        14: 'E',
        15: 'F',
        16: 'G',
        17: 'H',
        18: 'I',
        19: 'J',
        20: 'K',
        21: 'L',
        22: 'M',
        23: 'N',
        24: 'O',
        25: 'P',
        26: 'Q',
        27: 'R',
        28: 'S',
        29: 'T',
        30: 'U',
        31: 'V',
        32: 'W',
        33: 'X',
        34: 'Y',
        35: 'Z'
    }

    number = int(number)
    while number >= base:
        result += BASE_32[number % base]
        number = number // base
    result += BASE_32[number]

    result = ''.join([item for item in result][::-1])

    return result

choice = ttk.Combobox(converter, values=['BIN', 'OCT', 'DEC', 'HEX'], width=3, font=('Consolas', 10))
choice.grid(row=0, column=0)
choice.current(randint(0, 3))

source = Entry(converter, font=('Consolas', 10), width=23)
source.grid(row=0, column=1, padx=(2, 2))

main_lbl = Label(converter, text='- Convertation result -', font=('Consolas', 10))
main_lbl.grid(row=1, columnspan=3)

bin_lbl = Label(converter, text='BIN:', font=('Consolas', 10))
bin_lbl.grid(row=2, column=0)
bin_etr = Entry(converter, width=38, font=('Consolas', 10))
bin_etr.grid(row=2, column=1, columnspan=2)

oct_lbl = Label(converter, text='OCT:', font=('Consolas', 10))
oct_lbl.grid(row=3, column=0)
oct_etr = Entry(converter, width=38, font=('Consolas', 10))
oct_etr.grid(row=3, column=1, columnspan=2)

dec_lbl = Label(converter, text='DEC:', font=('Consolas', 10))
dec_lbl.grid(row=4, column=0)
dec_etr = Entry(converter, width=38, font=('Consolas', 10))
dec_etr.grid(row=4, column=1, columnspan=2)

hex_lbl = Label(converter, text='HEX:', font=('Consolas', 10))
hex_lbl.grid(row=5, column=0)
hex_etr = Entry(converter, width=38, font=('Consolas', 10))
hex_etr.grid(row=5, column=1, columnspan=2)

def convert():
    global to_dec
    global from_dec

    if choice.get() == 'BIN':
        bin_etr.delete(0, 'end')
        oct_etr.delete(0, 'end')
        dec_etr.delete(0, 'end')
        hex_etr.delete(0, 'end')

        bin_etr.insert(0, source.get())
        oct_etr.insert(0, from_dec(8, to_dec(2, source.get())))
        dec_etr.insert(0, to_dec(2, source.get()))
        hex_etr.insert(0, from_dec(16, to_dec(2, source.get())))

        main_lbl.configure(text='- Convertation result -', fg='#000000')

    elif choice.get() == 'OCT':
        bin_etr.delete(0, 'end')
        oct_etr.delete(0, 'end')
        dec_etr.delete(0, 'end')
        hex_etr.delete(0, 'end')

        bin_etr.insert(0, from_dec(2, to_dec(8, source.get())))
        oct_etr.insert(0, source.get())
        dec_etr.insert(0, to_dec(8, source.get()))
        hex_etr.insert(0, from_dec(16, to_dec(8, source.get())))

        main_lbl.configure(text='- Convertation result -', fg='#000000')

    elif choice.get() == 'DEC':
        bin_etr.delete(0, 'end')
        oct_etr.delete(0, 'end')
        dec_etr.delete(0, 'end')
        hex_etr.delete(0, 'end')

        bin_etr.insert(0, from_dec(2, source.get()))
        oct_etr.insert(0, from_dec(8, source.get()))
        dec_etr.insert(0, source.get())
        hex_etr.insert(0, from_dec(16, source.get()))

        main_lbl.configure(text='- Convertation result -', fg='#000000')

    elif choice.get() == 'HEX':
        bin_etr.delete(0, 'end')
        oct_etr.delete(0, 'end')
        dec_etr.delete(0, 'end')
        hex_etr.delete(0, 'end')

        bin_etr.insert(0, from_dec(2, to_dec(16, source.get())))
        oct_etr.insert(0, from_dec(8, to_dec(16, source.get())))
        dec_etr.insert(0, to_dec(16, source.get()))
        hex_etr.insert(0, source.get())

        main_lbl.configure(text='- Convertation result -', fg='#000000')

    else:
        choice.current(randint(0, 3))
        main_lbl.configure(text='- Choose correct source system -', fg='#FF0000')

convert = Button(converter, text='Convert', command=convert, width=15, font=('Consolas', 10))
convert.grid(row=0, column=2, padx=(0, 1))

window.mainloop()


"""
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Note:

Your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution and you may not use the same element twice.
Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
"""

class Solution:
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int
        :rtype: List[int]
        """
        d = {}
        for i in range(len(numbers)):
            if numbers[i] in d.keys():
                d[numbers[i]] = [d[numbers[i]], i+1]
            else:
                d[numbers[i]] = i + 1

        for n in numbers:
            if n in d.keys() and target-n in d.keys():
                if n == target -n:
                    return [d[n][0], d[n][1]]
                else:
                    return [d[n], d[target-n]]


import jittor as jt
import numpy as np
from AttentionUnit import AttentionWrapper
from dualAttentionUnit import dualAttentionWrapper
from LstmUnit import LstmUnit
from fgateLstmUnit import fgateLstmUnit
from OutputUnit import OutputUnit

jt.flags.use_cuda = 1

class SeqUnit(jt.Module):
    def __init__(self, batch_size, hidden_size, emb_size, field_size, pos_size, source_vocab, field_vocab,
            position_vocab, target_vocab, field_concat, position_concat, fgate_enc, dual_att,
            encoder_add_pos, decoder_add_pos, learning_rate, name, start_token=2, stop_token=2, max_length=150):
        self.batch_size = batch_size
        self.hidden_size = hidden_size
        self.emb_size = emb_size
        self.field_size = field_size
        self.pos_size = pos_size
        self.uni_size = emb_size if not field_concat else emb_size+field_size
        self.uni_size = self.uni_size if not position_concat else self.uni_size+2*pos_size
        self.field_encoder_size = field_size if not encoder_add_pos else field_size+2*pos_size
        self.field_attention_size = field_size if not decoder_add_pos else field_size+2*pos_size
        self.source_vocab = source_vocab
        self.target_vocab = target_vocab
        self.field_vocab = field_vocab
        self.position_vocab = position_vocab
        self.grad_clip = 5.0
        self.start_token = start_token
        self.stop_token = stop_token
        self.max_length = max_length
        self.name = name
        self.field_concat = field_concat
        self.position_concat = position_concat
        self.fgate_enc = fgate_enc
        self.dual_att = dual_att
        self.encoder_add_pos = encoder_add_pos
        self.decoder_add_pos = decoder_add_pos

        self.encoder_input = jt.int32([])
        self.encoder_field = jt.int32([])
        self.encoder_pos = jt.int32([])
        self.encoder_rpos = jt.int32([])
        self.decoder_input = jt.int32([])
        self.encoder_len = jt.int32([])
        self.decoder_len = jt.int32([])
        self.decoder_output = jt.int32([])
        self.enc_mask = jt.nn.sign(jt.float32(self.encoder_pos))

        if self.fgate_enc:
            self.enc_lstm = fgateLstmUnit(self.hidden_size, self.uni_size, self.field_encoder_size)
        else:
            self.enc_lstm = LstmUnit(self.hidden_size, self.uni_size)
        self.dec_lstm = LstmUnit(self.hidden_size, self.emb_size)
        self.dec_out = OutputUnit(self.hidden_size, self.target_vocab)

        self.embedding = jt.nn.Embedding(self.source_vocab, self.emb_size)
        self.embedding.require_grad = True
        if self.field_concat or self.fgate_enc or self.encoder_add_pos or self.decoder_add_pos:
            self.fembedding = jt.nn.Embedding(self.field_vocab, self.field_size)
            self.fembedding.require_grad = True
        if self.position_concat or self.encoder_add_pos or self.decoder_add_pos:
            self.pembedding = jt.nn.Embedding(self.position_vocab, self.pos_size)
            self.pembedding.require_grad = True
            self.rembedding = jt.nn.Embedding(self.position_vocab, self.pos_size)
            self.rembedding.require_grad = True

        if self.dual_att:
            print('dual attention mechanism used')
            self.att_layer = dualAttentionWrapper(self.hidden_size, self.hidden_size, self.field_attention_size)
        else:
            self.att_layer = AttentionWrapper(self.hidden_size, self.hidden_size)
            print('normal attention used')

        self.optimizer = jt.nn.Adam(self.parameters(), learning_rate)

    def execute(self, x):
        """
        {self.encoder_input: x['enc_in'], self.encoder_len: x['enc_len'],
        self.encoder_field: x['enc_fd'], self.encoder_pos: x['enc_pos'],
        self.encoder_rpos: x['enc_rpos'], self.decoder_input: x['dec_in'],
        self.decoder_len: x['dec_len'], self.decoder_output: x['dec_out']}
        """
        self.encoder_input = x['enc_in'] = jt.array(x['enc_in'])
        encoder_embed = self.embedding(self.encoder_input)
        if x['dec_in'] != None:
            x['dec_in'] = jt.array(x['dec_in'])
            decoder_embed = self.embedding(x['dec_in'])
        if self.field_concat or self.fgate_enc or\
            self.encoder_add_pos or self.decoder_add_pos:
            x['enc_fd'] = jt.array(x['enc_fd'])
            field_embed = self.fembedding(x['enc_fd'])
            field_pos_embed = field_embed
            if self.field_concat:
                encoder_embed = jt.concat([encoder_embed, field_embed], dim=2)
        if self.position_concat or self.encoder_add_pos or self.decoder_add_pos:
            x['enc_pos'] = jt.array(x['enc_pos'])
            x['enc_rpos'] = jt.array(x['enc_rpos'])
            pos_embed = self.pembedding(x['enc_pos'])
            rpos_embed = self.rembedding(x['enc_rpos'])
            if self.position_concat:
                encoder_embed = jt.concat([encoder_embed, pos_embed, rpos_embed], dim=2)
                field_pos_embed = jt.concat([field_embed, pos_embed, rpos_embed], dim=2)
            elif self.encoder_add_pos or self.decoder_add_pos:
                field_pos_embed = jt.concat([field_embed, pos_embed, rpos_embed], dim=2)
        # ===== encode ===== #
        x['enc_len'] = jt.array(x['enc_len'])
        if self.fgate_enc:
            en_outputs, en_state = self.fgate_encoder(encoder_embed, field_pos_embed, x['enc_len'])
        else:
            en_outputs, en_state = self.encoder(self.encoder_embed, x['enc_len'])
        self.en_outputs = en_outputs
        self.field_pos_embed = field_pos_embed
        # ===== decode ===== #
        if x['dec_len'] != None:
            x['dec_len'] = jt.array(x['dec_len'])
            de_outputs, de_state = self.decoder_t(en_state, decoder_embed, x['dec_len'])
        self.g_tokens, self.atts = self.decoder_g(en_state)
        self.mean_loss = None
        if x['dec_out'] != None:
            x['dec_out'] = jt.array(x['dec_out'])
            batch_size, maxlen, _ = de_outputs.shape
            losses = jt.nn.cross_entropy_loss(
                output=de_outputs.reshape([batch_size*maxlen, -1]),
                target=x['dec_out'].reshape([-1]),
                reduction='none'
            )
            losses = losses.reshape([batch_size, -1])
            mask = jt.nn.sign(jt.float32(x['dec_out']))
            losses = mask * losses
            self.mean_loss = jt.mean(losses)
        return self.mean_loss

    def encoder(self, inputs, inputs_len):
        batch_size = inputs.shape[0]
        max_time = inputs.shape[1]
        hidden_size = self.hidden_size
        time = 0
        h0 = (
            jt.zeros([batch_size, hidden_size], dtype=jt.float32),
            jt.zeros([batch_size, hidden_size], dtype=jt.float32)
        )
        f0 = jt.zeros([batch_size], dtype=jt.bool)
        # inputs_ta = tf.TensorArray(dtype=tf.float32, size=max_time)
        # emit_ta = tf.TensorArray(dtype=tf.float32, dynamic_size=True, size=0)
        inputs_ta = jt.transpose(inputs, [1,0,2])
        emit_ta = []

        def loop_fn(t, x_t, s_t, emit_ta, finished):
            o_t, s_nt = self.enc_lstm(x_t, s_t, finished)
            emit_ta.append(o_t)
            finished = (t+1) >= inputs_len
            x_nt = jt.zeros([batch_size, self.uni_size], dtype=jt.float32) \
                if jt.all(finished) else inputs_ta[t+1]
            return t+1, x_nt, s_nt, finished

        while not jt.all(f0):
            time, inputs_ta[0], h0, f0 = loop_fn(time, inputs_ta[0], h0, emit_ta, f0)
        outputs = jt.transpose(jt.stack(emit_ta, dim=0), [1,0,2])
        return outputs, h0

    def fgate_encoder(self, inputs, fields, inputs_len):
        batch_size = inputs.shape[0]
        max_time = inputs.shape[1]
        hidden_size = self.hidden_size

        time = 0
        h0 = (
            jt.zeros([batch_size, hidden_size], dtype=jt.float32),
            jt.zeros([batch_size, hidden_size], dtype=jt.float32)
        )
        f0 = jt.zeros([batch_size], dtype=jt.bool)
        inputs_ta = jt.transpose(inputs, [1,0,2])
        fields_ta = jt.transpose(fields, [1,0,2])
        emit_ta = []

        def loop_fn(t, x_t, d_t, s_t, emit_ta, finished):
            o_t, s_nt = self.enc_lstm(x_t, d_t, s_t, finished)
            emit_ta.append(o_t)
            finished = (t+1) >= inputs_len
            x_nt = jt.zeros([batch_size, self.uni_size], dtype=jt.float32) \
                if jt.all(finished) else inputs_ta[t+1]
            d_nt = jt.zeros([batch_size, self.field_attention_size], dtype=jt.float32) \
                if jt.all(finished) else fields_ta[t+1]
            return t+1, x_nt, d_nt, s_nt, finished

        while not jt.all(f0):
            time, inputs_ta[0], fields_ta[0], h0, f0 = loop_fn(time, inputs_ta[0], fields_ta[0], h0, emit_ta, f0)

        outputs = jt.transpose(jt.stack(emit_ta, dim=0), [1,0,2])
        return outputs, h0

    def decoder_t(self, initial_state, inputs, inputs_len):
        batch_size = inputs.shape[0]
        max_time = inputs.shape[1]
        time = 0
        h0 = initial_state
        f0 = jt.zeros([batch_size], dtype=jt.bool)
        x0 = self.embedding(jt.array([self.start_token] * batch_size))
        inputs_ta = jt.transpose(inputs, [1,0,2])
        emit_ta = []

        def loop_fn(t, x_t, s_t, emit_ta, finished):
            o_t, s_nt = self.dec_lstm(x_t, s_t, finished)
            o_t, _ = self.att_layer(o_t, self.en_outputs, self.field_pos_embed)
            o_t = self.dec_out(o_t, finished)
            emit_ta.append(o_t)
            finished = t >= inputs_len
            x_nt = jt.zeros([batch_size, self.emb_size], dtype=jt.float32) \
                if jt.all(finished) else inputs_ta[t]
            return t+1, x_nt, s_nt, finished

        while not jt.all(f0):
            time, x0, h0, f0 = loop_fn(time, x0, h0, emit_ta, f0)

        outputs = jt.transpose(jt.stack(emit_ta, dim=0), [1,0,2])
        return outputs, h0

    def decoder_g(self, initial_state):
        batch_size = self.encoder_input.shape[0]
        encoder_len = self.encoder_input.shape[1]
        time = 0
        h0 = initial_state
        f0 = jt.zeros([batch_size], dtype=jt.bool)
        x0 = self.embedding(jt.array([self.start_token] * batch_size))
        emit_ta = []
        att_ta = []

        def loop_fn(t, x_t, s_t, emit_ta, att_ta, finished):
            o_t, s_nt = self.dec_lstm(x_t, s_t, finished)
            o_t, w_t = self.att_layer(o_t, self.en_outputs, self.field_pos_embed)
            o_t = self.dec_out(o_t, finished)
            emit_ta.append(o_t)
            att_ta.append(w_t)
            next_token, _ = jt.argmax(o_t, dim=1)
            x_nt = self.embedding(next_token)
            finished = finished | (next_token == self.stop_token) \
                | (t >= self.max_length)
            return t+1, x_nt, s_nt, finished

        while not jt.all(f0):
            time, x0, h0, f0 = loop_fn(time, x0, h0, emit_ta, att_ta, f0)

        outputs = jt.transpose(jt.stack(emit_ta, dim=0), [1,0,2])
        pred_tokens, _ = jt.argmax(outputs, dim=2)
        atts = jt.stack(att_ta, dim=0)
        return pred_tokens, atts

    def decoder_beam(self, initial_state, beam_size):

        def beam_init():
            time_1 = 1
            beam_seqs_0 = jt.array([[self.start_token]] * beam_size)
            beam_probs_0 = jt.array([0.0] * beam_size)

            cand_seqs_0 = jt.array([[self.start_token]])
            cand_probs_0 = jt.array([-3e38])

            inputs = jt.array([self.start_token])
            x_t = self.embedding(inputs)
            print(x_t.shape)
            o_t, s_nt = self.dec_lstm(x_t, initial_state)
            o_t, w_t = self.att_layer(o_t, self.en_outputs, self.field_pos_embed)
            o_t = self.dec_out(o_t)
            print(s_nt[0].shape)
            logprobs2d = jt.nn.log_softmax(o_t)
            total_probs = logprobs2d + jt.reshape(beam_probs_0, [-1, 1])
            total_probs_noEOS = jt.concat([
                total_probs[0:1, 0:self.stop_token],
                jt.array([[-3e38]]),
                total_probs[0:1, self.stop_token+1:self.target_vocab]
            ], dim=1)
            flat_total_probs = jt.reshape(total_probs_noEOS, [-1])
            print(flat_total_probs.shape)

            beam_k = min(len(flat_total_probs), beam_size)
            top_indices, next_beam_probs = jt.argsort(flat_total_probs)
            top_indices = top_indices[:beam_k]
            next_beam_probs = next_beam_probs[:beam_k]

            next_bases = top_indices // self.target_vocab
            next_mods = jt.mod(top_indices, self.target_vocab)

            next_beam_seqs = jt.concat([
                beam_seqs_0[next_bases],
                jt.reshape(next_mods, [-1, 1])
            ], dim=1)

            cand_seqs_pad = jt.nn.pad(cand_seqs_0, [0,1])
            beam_seqs_EOS = jt.nn.pad(beam_seqs_0, [0,1])
            new_cand_seqs = jt.concat([cand_seqs_pad, beam_seqs_EOS], 0)
            print(new_cand_seqs.shape)

            EOS_probs = total_probs[0:beam_size, self.stop_token:self.stop_token+1]
            new_cand_probs = jt.concat([
                cand_probs_0,
                jt.reshape(EOS_probs, [-1])
            ], 0)
            cand_k = min(len(new_cand_probs), self.beam_size)
            next_cand_indices, next_cand_probs = jt.argsort(new_cand_probs)
            next_cand_indices = next_cand_indices[:cand_k]
            next_cand_probs = next_cand_probs[:cand_k]
            next_cand_seqs = new_cand_seqs[next_cand_indices]

            part_state_0 = jt.reshape(jt.stack([s_nt[0]] * beam_size), [beam_size, self.hidden_size])
            part_state_1 = jt.reshape(jt.stack([s_nt[1]] * beam_size), [beam_size, self.hidden_size])
            next_states = (part_state_0, part_state_1)
            print(part_state_0.shape)

            return next_beam_seqs, next_beam_probs, next_cand_seqs, \
                next_cand_probs, next_states, time_1

        beam_seqs_1, beam_probs_1, cand_seqs_1, \
            cand_probs_1, states_1, time_1 = beam_init()

        def beam_step(beam_seqs, beam_probs, cand_seqs, cand_probs, states, time):
            inputs = jt.reshape(beam_seqs[0:beam_size, time:time+1], [beam_size])
            x_t = self.embedding(inputs)
            o_t, s_nt = self.dec_lstm(x_t, states)
            o_t, w_t = self.att_layer(o_t, self.en_outputs, self.field_pos_embed)
            o_t = self.dec_out(o_t)

            logprobs2d = jt.nn.log_softmax(o_t)
            print(logprobs2d.shape)
            total_probs = logprobs2d + jt.reshape(beam_probs, [-1, 1])
            print(total_probs.shape)
            total_probs_noEOS = jt.concat([
                total_probs[0:beam_size, 0:self.stop_token],
                jt.reshape(jt.array([-3e38] * beam_size), [beam_size, 1]),
                total_probs[0:beam_size, self.stop_token+1:self.target_vocab]
            ], dim=1)
            flat_total_probs = jt.reshape(total_probs_noEOS, [-1])
            print(flat_total_probs.shape)

            beam_k = min(len(flat_total_probs), beam_size)
            top_indices, next_beam_probs = jt.argsort(flat_total_probs)
            top_indices = top_indices[:beam_k]
            next_beam_probs = next_beam_probs[:beam_k]

            next_bases = top_indices // self.target_vocab
            next_mods = jt.mod(top_indices, self.target_vocab)

            next_beam_seqs = jt.concat([
                beam_seqs[next_bases],
                jt.reshape(next_mods, [-1, 1])
            ], dim=1)

            next_states = (s_nt[0][next_bases], s_nt[1][next_bases])

            cand_seqs_pad = jt.nn.pad(cand_seqs, [0,1])
            beam_seqs_EOS = jt.nn.pad(beam_seqs, [0,1])
            new_cand_seqs = jt.concat([cand_seqs_pad, beam_seqs_EOS], 0)
            print(new_cand_seqs.shape)

            EOS_probs = total_probs[0:beam_size, self.stop_token:self.stop_token+1]
            new_cand_probs = jt.concat([
                cand_probs,
                jt.reshape(EOS_probs, [-1])
            ], 0)
            cand_k = min(len(new_cand_probs), self.beam_size)
            next_cand_indices, next_cand_probs = jt.argsort(new_cand_probs)
            next_cand_indices = next_cand_indices[:cand_k]
            next_cand_probs = next_cand_probs[:cand_k]
            next_cand_seqs = new_cand_seqs[next_cand_indices]

            return next_beam_seqs, next_beam_probs, next_cand_seqs, \
                next_cand_probs, next_states, time+1

        def beam_cond(beam_probs, beam_seqs, cand_probs, cand_seqs, state, time):
            length = (beam_probs.max() >= cand_probs.min())
            return length and (time < 60)

        loop_vars = [beam_seqs_1, beam_probs_1, cand_seqs_1, cand_probs_1, states_1, time_1]

        while beam_cond(*loop_vars):
            loop_vars = beam_step(*loop_vars)

        return loop_vars[0], loop_vars[1], loop_vars[2], loop_vars[3]

    def generate(self, x):
        self.execute(x)
        return self.g_tokens, self.atts


import pytest
import numpy as np
from all_ode import HopfBifurcation, Cubic
from all_pde import uI
from shooting import shooting, get_ode_data
from continuation import pseudo_continuation, natural_continuation
from solve_pde import solve_pde, matrix_forward_euler, matrix_backward_euler, crank_nicholson

class Test():
    def test_output_length(self):
        args = np.array([0.5, -1])
        u0 = [1.5, 0, 20]
        sol = shooting(HopfBifurcation.func, u0, args=args)
        assert len(sol) == len(u0)

    def test_data_length(self):
        args = np.array([0.5, -1])
        u0 = [1.5, 0, 20]
        y, t = get_ode_data(HopfBifurcation.func, u0, args=args)
        for i in y:
            assert len(i) == len(t)

    def test_pseudo_sols_pars_len(self):
        ode = Cubic.func
        u0, par0 = Cubic.params()
        limit_cycle = False
        vary_par = 0
        step = 0.05
        max_steps = 80
        sols, pars = pseudo_continuation(ode, u0, limit_cycle, par0, vary_par, step, max_steps)
        assert len(sols) == len(pars)

    def test_natural_sols_pars_len(self):
        ode = Cubic.func
        u0, par0 = Cubic.params()
        limit_cycle = False
        vary_par = 0
        step = 0.05
        max_steps = 330
        sols, pars = natural_continuation(ode, u0, limit_cycle, par0, vary_par, step, max_steps)
        assert len(sols) == len(pars)

    def test_solve_pde(self):
            kappa = 1
            L = 1
            T = 0.5
            mx = 35
            mt = 1000
            x = np.linspace(0, L, mx+1)
            t = np.linspace(0, T, mt+1)
            deltax = x[1] - x[0]
            deltat = t[1] - t[0]
            lmbda = kappa*deltat/(deltax**2)
            methods = [matrix_forward_euler, matrix_backward_euler, crank_nicholson]
            pde = uI.func
            dirichlet_conditions = np.array([])
            for meth in methods:
                sol = solve_pde(pde, x, mx, mt, L, lmbda, meth, dirichlet_conditions)
                assert len(sol) == mx+1