# importing all libraries
import cadquery as cq
from cadquery import exporters
import pyaudio
import json
from vosk import Model, KaldiRecognizer
import pyttsx3
from tkinter import *
from PIL import ImageTk, Image
import tkinter.font as TkFont
import os
from word2number import w2n

#loading vosk ml audio recognition model
model = Model(
    r"path\to\any\vosk\voice recognition model"
)

model = KaldiRecognizer(model, 16000)

mic = pyaudio.PyAudio()
stream = mic.open(format=pyaudio.paInt16,
                  channels=1,
                  rate=16000,
                  input=True,
                  frames_per_buffer=8192)

# initialize Text-to-speech engine
engine = pyttsx3.init()

voices = engine.getProperty('voices')  #getting details of current voice
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate', 150)

def say(text):
    engine.say(text)
    engine.runAndWait()

stream.start_stream()

#default name
name = "any_default_name"

coldstart=True

def record_audio():
    while True:
        data = stream.read(44100, exception_on_overflow=False)
        if model.AcceptWaveform(data):
            text = json.loads(model.Result())["text"]
            try:
                if len(text.split()) > 0:
                    return text
            except:
                continue

def introduce():
    say("Welcome everyone to the interactive interface. I hope you are doing well"
        )

def voice_set():
    z = clicked.get()
    if z[-1] == "e":
        engine.setProperty('voice', voices[0].id)
    elif z[-1] == "1":
        engine.setProperty('voice', voices[1].id)
    else:
        engine.setProperty('voice', voices[2].id)
    say("Voice changed successfully")

def entry_box_update(text):
    e.delete(0, END)
    e.insert(0, text)
    root.update()

def end_instance():
    b.configure(text="Instance ended. Click to start again.",
                    bg='red',
                    fg='white')
    root.update()

def finalize_design(result):
    exporters.export(result,"output.step")
#     end_instance()
    os.startfile("output.step")

def start():
    #initialize name to change
    global name
    global coldstart

    stop = False

    e.delete(0, END)
    b.configure(text="Instance started", bg='green', fg='white')
    root.update()

    temp = "New Instance started successfully."
    if coldstart:
        temp+=" You can take my name, " + str(
        name) + " to start interacting."
    entry_box_update(temp)
    say(temp)

    #wait till name is taken
    while coldstart:
        text = record_audio()
        entry_box_update(text)
        text = " " + text + " "
        if name in text:
            temp = "Welcome, my name is " + str(name) + ". How may I help you?"
            entry_box_update(temp)
            say(temp)
            coldstart=False
            break

        if " stop " in text or " end " in text:
            stop = True
            say("Ok, ending the instance")
            end_instance()
            break

        if "repeat" in text:
            temp = "New Instance started successfully. You can take my name, " + str(
                name) + " to start interacting."
            entry_box_update(temp)
            say(temp)

    while not stop:
        text = record_audio()
        entry_box_update(text)
        text = " " + text + " "
        if "repeat" in text:
            temp = "Welcome, my name is " + str(name) + ". How may I help you?"
            entry_box_update(temp)
            say(temp)
        if " end " in text or " stop " in text:
            say("Ok, ending the instance")
            end_instance()
            break
        if " name " in text:
            say("Ok, tell me my new name")
            temp = record_audio()
            name = temp
            n2.delete(0, END)
            n2.insert(0, temp)
            say("ok my name is " + str(temp))
            coldstart=True
            start()
            end_instance()
            break

        #shapes start here
        if "cube" in text:
            say("OK, designing a cube")
            result = cq.Workplane("XY").box(1, 1, 1)
            finalize_design(result)
#             break
        if "cylinder" in text:
            say("OK, designing a cylinder")
            result = cq.Workplane("XY").circle(10).extrude(50)
            finalize_design(result)
#             break
        if "cuboid" in text:
            say("OK, designing a cuboid")
            result = cq.Workplane("XY").box(5, 10, 20)
            finalize_design(result)
#             break
        if "column" in text:
            say("OK, designing a column")
            (L, H, W, t) = (100.0, 20.0, 20.0, 1.0)

            pts = [
                (0, H / 2.0),
                (W / 2.0, H / 2.0),
                (W / 2.0, (H / 2.0 - t)),
                (t / 2.0, (H / 2.0 - t)),
                (t / 2.0, (t - H / 2.0)),
                (W / 2.0, (t - H / 2.0)),
                (W / 2.0, H / -2.0),
                (0, H / -2.0),
            ]

            result = cq.Workplane("front").polyline(pts).mirrorY().extrude(L)
            finalize_design(result)
#             break
        if "box" in text:
            say("OK, designing a box")
            result = cq.Workplane("front").box(2, 2, 2).faces("+Z").shell(0.05)
            finalize_design(result)
#             break
        if "cone" in text:
            say("Ok, designing a cone")
            result = (cq.Workplane("front").box(4.0, 4.0, 0.25).faces(">Z").circle(1.5).workplane(offset=3.0).rect(0.75, 0.5).loft(combine=True))
            finalize_design(result)
#             break
        if "spring" in text:
            say("Ok, designing a spring")
            r = 0.5  # Radius of the helix
            p = 0.4  # Pitch of the helix - vertical distance between loops
            h = 2.4  # Height of the helix - total height

            # Helix
            wire = cq.Wire.makeHelix(pitch=p, height=h, radius=r)
            helix = cq.Workplane(obj=wire)

            # Final result: A 2D shape swept along a helix.
            result = (
                cq.Workplane("XZ")  # helix is moving up the Z axis
                .center(r, 0)  # offset isosceles trapezoid
                .polyline(((-0.15, 0.1), (0.0, 0.05), (0, 0.35), (-0.15, 0.3)))
                .close()  # make edges a wire
                .sweep(helix, isFrenet=True)  # Frenet keeps orientation as expected
            )
            finalize_design(result)
#             break
def delet(dummy):
    n2.delete(0, END)

def change_name():
    global name
    coldstart=True
    name = n2.get()
    say("Changed name to " + str(name) + " successfully")

#initialize

root = Tk()

# root.geometry("1280x720")

#structures
root.title("Automated Engine Design using Machine Learning")
root.iconbitmap(r"path\to\ico\file")

#logo
myimg = ImageTk.PhotoImage(Image.open("logo\path"))
DMCE_logo = Label(image=myimg, bg="white")
DMCE_logo.grid(row=1, column=1, rowspan=2)

#title label
title_label = Label(
    root,
    text=
    "'Automated Design using Voice Recognition'",
    font=TkFont.Font(family="Times New Roman", size=24, weight="bold"),
).grid(row=1, column=2, columnspan=5, padx=10, pady=10, sticky=W + E)

#subtitle label
subtitle_label = Label(
    root,
    text="Python Project AY:2022-2023",
    font=TkFont.Font(family="Times New Roman", size=15),
    bd=1
).grid(
    row=2,
    column=2,
    #                                padx=10,
    #                                pady=10,
    columnspan=5,
    sticky=W + E)

#desclabel
desc_label = Label(
    root,
    text=
    "\tThis application has been developed as an interface for 'Automated Design using Voice Recognition'.",
    font=TkFont.Font(family="Times New Roman", size=12),
    bd=1,
    anchor=E,
    justify="left").grid(row=3, column=2, columnspan=5, sticky=W + E)

#buttons below description
it = Button(root, text="Introduction", command=introduce)
it.grid(row=4, column=2, pady=10)

#options tab
options = ["Voice Male", "Voice Female 1", "Voice Female 2"]
clicked = StringVar()
clicked.set("Voice Female 1")

#option dropdown
nm = OptionMenu(root, clicked, *options)
nm.grid(row=4, column=3, pady=10)

#setting voices
n1 = Button(root, text="Set voice", command=voice_set)
n1.grid(row=4, column=4, pady=10)

#name
n2 = Entry(root, bg="lightgrey")
n2.insert(0, "Name: " + name)
n2.bind("<1>", delet)
n2.grid(row=4, column=5, pady=10)

#name button
n3 = Button(root, text="Set Name", command=change_name)
n3.grid(row=4, column=6, pady=10)

#credits label
name_label = Label(
    root,
    text=
    "Developed By:\n\nParas Raorane",
    font=TkFont.Font(family="Times New Roman", size=12),
    bd=1,
    anchor=W,
    pady=10,
    padx=10).grid(row=6, column=1, rowspan=2)

#label before terminal
Label(
    root,
    text="Interactive Terminal",
    font=TkFont.Font(family="Times New Roman", size=12, weight="bold"),
).grid(row=5, column=2, columnspan=5, sticky=W + E)

#main entry
e = Entry(root, bg="lightgrey", width=100)  #, borderwidth=10)
e.grid(row=6, column=2, columnspan=5, sticky=W + E)
# #inserting text into the box
e.insert(
    0,
    "Detected text will be displayed here. You can make changes as required.")

b = Button(root, text="Initialize", command=start)
b.grid(row=7, column=2, columnspan=5, pady=10)

root.mainloop()


from nltk.stem import WordNetLemmatizer
import re
from nltk.tokenize import word_tokenize, sent_tokenize

def myTokenizer(readFileDir, saveFileDir, stopwords, lim):
    readFile = open(readFileDir, "r", encoding="UTF8")
    saveFile = open(saveFileDir, "w", encoding="UTF8")
    preprocessed = []
    lemmatizer = WordNetLemmatizer()
    while True:
        line = readFile.readline()
        if len(line) == 0:
            print("File read finished")
            readFile.close()
            break
        sentTokenized = sent_tokenize(line)

        for sent in sentTokenized:
            sent = re.sub("[^a-zA-Z]", " ", sent)
            wordTokenized = word_tokenize(sent)
            i = 0
            while i < len(wordTokenized):
                if len(wordTokenized[i]) <= lim or \
                        wordTokenized[i] in stopwords:
                    wordTokenized.remove(wordTokenized[i])
                else:
                    wordTokenized[i] = wordTokenized[i].lower()
                    wordTokenized[i] = lemmatizer.lemmatize(wordTokenized[i])
                    saveFile.write(wordTokenized[i])
                    if i < len(wordTokenized) - 1:
                        saveFile.write(" ")
                    i += 1

            saveFile.write("\n")
            preprocessed.append(wordTokenized)

    saveFile.close()

    return preprocessed


from django.contrib import admin,messages
from .models import *
from inline_actions.admin import InlineActionsModelAdminMixin
from commun.util import Util
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

media_root = settings.MEDIA_ROOT
admin.site.site_header = "Administration ERP"
admin.site.site_title = "ERP"
admin.site.index_title = "Bienvenu à l'ERP"

@admin.register(Expediteur)
class ExpediteurAdmin(admin.ModelAdmin):
    list_display = ("name","structure","employe")
    search_fields = ("name",)

@admin.register(TypeCourier)
class TypeCourierAdmin(admin.ModelAdmin):
    list_display = ("name",)

@admin.register(Classification)
class ClassificationAdmin(admin.ModelAdmin):
    list_display = ("name",)

@admin.register(Status)
class StatusAdmin(admin.ModelAdmin):
    list_display = ("name",)

@admin.register(Attachment)
class AttachmentAdmin(admin.ModelAdmin):
    list_display = ("name","file",)
##############
class AttachmentTabular(admin.TabularInline):
    model = Attachment
    extra = 1
@admin.register(Courier)
class CourierAdmin(InlineActionsModelAdminMixin,admin.ModelAdmin):
    inlines = [AttachmentTabular]
    autocomplete_fields = ("expediteur","destinataires","visible_a")
    inline_actions = ('send_by_mail','delete')
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return  qs.filter(deleted=False)
    def get_list_display(self, request):
        list=("objet","expediteur","date_expedition","date_arrivee","type","classification","status")
        if request.user.is_superuser:
            return list+('deleted',)+ (super().get_list_display(request).pop(),)
        return list+(super().get_list_display(request).pop(),)

    def send_by_mail(self, request, obj:Courier, parent_obj):
        dists=obj.visible_a.all()
        to=[]
        for dist in dists:
            to.append(dist.getExpediteur().email)
        source=obj.expediteur.getExpediteur().email
        attachments=list(obj.attachments.all())
        arr=list()
        files=list()
        for dist in dists:
            arr.append(dist.email)
        for attch in attachments:
            files.append(attch.file.name)
        try:
            res=Util.send_email(subject=obj.objet, message=obj.objet, source=source, to=to, cc=[], attachments=files)
            messages.success(request, _("Email Envoyé"))
        except Exception :
            messages.error(request, _("Erreur d'envoie"))

    send_by_mail.short_description = 'envoyer email'

    def delete(self, request, obj:Courier, parent_obj):
        obj.deleted=True
        obj.save()
    delete.short_description = 'supprimer'


N, K = list(map(int, input().split()))
W = []
for i in range(N):
  W.append(int(input()))

def is_enough(P):
  track_index = 0
  w_index = 0
  while track_index < K and w_index < N: # トラックの数を超える or Wを全て載せ終わる
    tmp_sum = 0
    while w_index < N and tmp_sum + W[w_index] <= P:
      tmp_sum += W[w_index]
      w_index += 1
    track_index += 1
  return w_index == N

left = 0
right = 100000 * 10000 # 最大個数 * 最大重量
mid = (left + right) // 2
answer = right

while left <= right:
  if is_enough(mid):
    answer = mid
    right = mid - 1
  else:
    left = mid + 1
  mid = (left + right) // 2

print(str(answer))


#coding: utf-8

from . import BaseSuite

class AcceptanceSuite(BaseSuite):

    def acceptance_from_mail(self, invoice_id, pointsale_id, date, items):
        """
        /acceptance/pointsale/<int:point_id>/invoice/<int:invoice_id>
        """
        data = {
            'data': {'items': items, 'date': unicode(date)},
        }
        return self.client.post("/api/acceptance/pointsale/" + str(pointsale_id) + "/invoice/" + str(invoice_id),
                                data=self._serialize(data), headers=self._get_headers(True))

    def acceptance_custom(self, pointsale_id, provider_id, date, items):
        """
        date: 2015-02-12T10:20:45.518Z
        items: [,…]
        pointsale_id: 2
        provider_id: 1
        """
        data = {
            "data": {
                "date": unicode(date),
                "pointsale_id": pointsale_id,
                "provider_id": provider_id,
                "items": items
            }
        }
        return self.client.put("/api/invoice", data=self._serialize(data), headers=self._get_headers(True))

    def invoice_items(self, invoice_id):
        return self.client.get("/api/invoice/" + str(invoice_id) + "/items", headers=self._get_headers())


from typing import Annotated

from fastapi import APIRouter, Depends, status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pydantic_core import ValidationError

from readconnect.shared.domain.dtos.error_response_dto import ErrorResponse
from readconnect.shared.domain.exceptions.exceptions import (
    NotFoundError,
    InvalidsCredentialsError,
)
from ...application.login_user.login_user_use_case import LoginUserUseCase
from ...application.signup_user.signup_user_use_case import SignupUserUseCase
from ...domain.dtos.login_request_dto import LoginRequestDTO
from ...domain.dtos.login_response_dto import LoginResponseDTO
from ...domain.dtos.signup_request_dto import SignupRequestDTO

auth_router = APIRouter(prefix="/auth")

@auth_router.post(
    path="/login",
    responses={
        200: {"model": LoginResponseDTO},
        502: {"model": ErrorResponse},
        422: {"model": ErrorResponse},
        404: {"model": ErrorResponse},
    },
)
async def login(
    body: LoginRequestDTO,
    login_use_case: Annotated[LoginUserUseCase, Depends(LoginUserUseCase)],
):
    try:
        response = await login_use_case.execute(body)
        return response
    except ValidationError as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.__str__()}"
        return JSONResponse(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )
    except InvalidsCredentialsError as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.details}"
        return JSONResponse(
            status_code=e.status_code,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )
    except NotFoundError as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.details}"
        return JSONResponse(
            status_code=e.status_code,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )
    except Exception as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.__str__()}"
        return JSONResponse(
            status_code=status.HTTP_502_BAD_GATEWAY,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )

@auth_router.post(
    path="/signup",
    responses={
        200: {"model": SignupRequestDTO},
        502: {"model": ErrorResponse},
        422: {"model": ErrorResponse},
        404: {"model": ErrorResponse},
        409: {"model": ErrorResponse},
    },
)
async def signup(
    body: SignupRequestDTO,
    signup_use_case: Annotated[SignupUserUseCase, Depends(SignupUserUseCase)],
):
    try:
        response = await signup_use_case.execute(body)
        return response

    except ValidationError as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.__str__()}"
        return JSONResponse(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )
    except InvalidsCredentialsError as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.details}"
        return JSONResponse(
            status_code=e.status_code,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )

    except NotFoundError as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.details}"
        return JSONResponse(
            status_code=e.status_code,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )
    except Exception as e:
        details = f"Ocurrió un problema al realizar su petición. Detalle: {e.__str__()}"
        return JSONResponse(
            status_code=status.HTTP_502_BAD_GATEWAY,
            content=jsonable_encoder(ErrorResponse(details=details)),
        )


import json
from random import randint

import ideas.utils as utils

IDEA_PATH = './ideas/ideas.json'

class Generator:

    def __init__(self, context='general', keys=[]):
        self.idea_path = IDEA_PATH
        self.context = context
        self.rand_items = []

        with open(self.idea_path, 'r') as json_file:
            self.lst = json.load(json_file)

        if len(keys) == 0:
            self.keys = self.lst.keys()
        else:
            self.keys = keys

    def get_keys(self):
        return list(self.keys)

    def export_lst(self):
        temp_lst = json.dumps(self.lst, indent=4)
        f = open(self.idea_path, 'w+')
        f.write(temp_lst)
        f.close()

    def get_rand_item(self, key):
        rand_num = randint(0, (len(self.lst[key]) - 1))
        if self.lst[key][rand_num] in self.rand_items:
            rand_num = randint(0, (len(self.lst[key]) - 1))
        self.rand_items.append(self.lst[key][rand_num])
        return self.lst[key][rand_num]

    def generate(self):
        generated_msg = ''
        for key in self.keys:
            generated_msg += f'{self.get_rand_item(key)} \n'
        self.print_result()
        return generated_msg

    def generate_multiple(self, how_many=None, key='items'):
        for i in range(how_many):
            self.get_rand_item(key)
        self.print_result()

    def print_result(self):
        utils.print_result(self.rand_items, self.context)

    @staticmethod
    def convert_file_to_array(file_location):
        with open(file_location, "r") as file_to_convert:
            file_array = file_to_convert.readlines()

        stripped_array = []
        for item in file_array:
            stripped_array.append(item.strip())
        return stripped_array


from rest_framework.routers import DefaultRouter

from apps.home.views import FeatureView, HomeView, I18nViewSet, SitemapView

router = DefaultRouter()
router.register("", HomeView)
router.register("sitemap", SitemapView, basename="sitemap")
router.register("i18n", I18nViewSet, basename="i18n")
router.register("features", FeatureView, basename="feature")

urlpatterns = router.urls


from flask import Flask,render_template, request, session, redirect, url_for
from threading import Thread

def createApp():
    app = Flask(
    __name__,
    template_folder=r"templates",
    static_folder=r"static"
    )
    return app

app = createApp()

@app.route("/")
def home():
    return render_template("./index.html")

def run():
  app.run(host='0.0.0.0',port=8080)

def keep_alive():
    t = Thread(target=run)
    t.start()

if __name__ == '__main__':
    keep_alive()


from pathlib import Path

import typer

from meteor import IdentityStage
from meteor import Language
from meteor import StemmingStage
from meteor import meteor_macro_avg

def cli(
    hypotheses_file: Path = typer.Option(
        ...,
        "-h",
        "--hypotheses",
        help="utf-8 encoded file with system output, one sentence per line",
        exists=True,
        file_okay=True,
        dir_okay=False,
        readable=True,
        resolve_path=True,
    ),
    references_file: Path = typer.Option(
        ...,
        "-r",
        "--references",
        help="utf-8 encoded file with translation references, one sentence per line",  # noqa
        exists=True,
        file_okay=True,
        dir_okay=False,
        readable=True,
        resolve_path=True,
    ),
    language: Language = typer.Option(
        Language.german,
        "-l",
        "--language",
        help="The language to run meteor for. Controls tokenization and stemming.",  # noqa
        show_default=True,
        case_sensitive=False,
    ),
):
    """
    Computes the METEOR score for the given sentence pairs
    and returns the macro average.

    Input files must be of same length and contain one sentence per line.
    Assumes UTF-8 encoding.
    """

    with hypotheses_file.open(encoding="utf-8") as infile:
        hypotheses = [line.strip() for line in infile if line.strip()]

    with references_file.open(encoding="utf-8") as infile:
        references = [line.strip() for line in infile if line.strip()]

    if len(hypotheses) != len(references):
        typer.echo("Error: Input files must be of same length.")
        exit(1)

    stages = [
        IdentityStage(1.0),
        StemmingStage(0.6, language),
    ]

    macro_avg = meteor_macro_avg(hypotheses, references, stages, language)
    typer.echo(f"METEOR macro average: {round(macro_avg, 3)}")

def main():
    typer.run(cli)


num = int(input("Enter the value num ="))
factorial = 1
if num <0:
    print("Factorial does not exist for negative numbers")
elif num == 0:
    print("factorial of 0 is 1")
else:
    for i in range(1,num+1):
        factorial=factorial*i
    print("The factorial of",num,"is",factorial)

#using recursion method
# Python recursion is a method which calls itself.

"""
def fact(n):
    return 1 if (n==1 or n==0) else n * fact(n - 1);

num = 5
print("Factorial of",num,"is",)
fact(num))
"""

#using built_in function

"""
import math
def fact(n):
    return(math.factorial(n))

num = int(input("Enter the number:"))
f = fact(num)
print("Factorial of", num, "is", f)
"""


#Tarea 20, para lenguajes de programacion
import matplotlib.pyplot as plt
import numpy as np
print ("Minimos cuadrados")
print ("Este programa calcula la pendiente (m), la intercepcion(b) y el coeficiente de relacion(r) de una regresion lineal")
print ("¿Cuantos datos (en pares (x1,y1) se considera 1) desea evaluar? (minimo 3)")
n=int(input())
while n<3:
    print ("Ingresa un valor mayor a 2: ")
    n=int(input())
x=np.arange(n, dtype=float)
y=np.arange(n, dtype=float)
acc=np.arange(n, dtype=float) #es un acumulador
sx=0
sy=0
sxy=0
sx2=0
sy2=0
for i in range(n):
    print ("Ingresa el dato x("+str(i+1)+"):")
    x[i]=float(input())
    print ("Ingresa el dato y("+str(i+1)+"):")
    y[i]=float(input())
    sx=sx+x[i] #sx es sumatorai en x
    sy=sy+y[i] #sy sumatoria en y
    sxy=sxy+x[i]*y[i] #sumatoria en x*y
    sx2=sx2+x[i]**2 #sumatoria en x**2
    sy2=sy2+y[i]**2 #sumatoria en y**2
print ("TABLA DE DATOS")
print ("#dato, x , y")
for i in range(n):
    print (str(i+1)+".-",x[i]," ,",y[i])
for i in range(n):
    acc[i]=x[i]
for i in range(0,n): #Este servira para poder encontrar el mas grande y el mas chico valor de x, para graficar
    for j in range(0,n-1):
        if acc[j]>=acc[j+1]: #if evalua que el numero actuar sea menor que el siguiente
            yac=acc[j]  #si es menor cambiara el orden, almacenando el valor actual en y
            acc[j]=acc[j+1] # y el nuevo valor actual seria el del numero siguiente
            acc[j+1]=yac #el numero siguiente sera el anterior, asi que le debolvemos el valor con y
#yp=y promedio, xp=x promedio,r=coeficiente de corelacion
#Σx=sx,Σy=sy,Σxy=sxy,Σx²=sx2
#La pendiente esta dada por m=(Σxy-(Σx)*yp)/(Σx²- (Σx)xp)
yp=sy/n
xp=sx/n
m=(sxy-sx*yp)/(sx2-sx*xp)
r=(n*sxy-sx*sy)/(((n*sx2-sx**2)*(n*sy2-sy**2))**.5)
b=yp-m*xp
print ("Σx=",sx,", Σy=",sy,", Σxy=",sxy,", Σx²=",sx2, ", Σy²=",sy2)
print ("m=",m)
print ("r=",r)
print ("b=",b)
xs=(x[n-1]-x[0])/(n-1)
x1=[acc[0],acc[n-1]]
y1=[m*x1[0]+b,m*x1[1]+b] #como es una ecuacion lineal, no necesita mas puntos, solo 2
plt.plot(x,y,"b*", label="Datos") #los puntos son marcados con estrellas para poder diferenciarlos de la linealizacion
plt.plot(x1,y1,"m-",label="Linializado")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.title("Minimos cuadrados")

programas python parte 2/T20_Minimos_cuadrados.py


import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.cm as cm
import matplotlib.patches as mpatches

import json
import sys
from helpers.helpers_visualisation import get_colors

from scipy.misc import imread
import matplotlib.image as mpimg

class Animation():
    def __init__(self,parameters_path):

        parameters_project = json.load(open(parameters_path))
        processed_parameters = json.load(open(parameters_project["data_processed_parameters"]))
        evaluation_parameters = json.load(open(parameters_project["evaluation_parameters"]))
        raw_parameters = json.load(open(parameters_project["data_raw_parameters"]))

        visualization_parameters = json.load(open(parameters_project["visualization_parameters"]))

        self.scene = visualization_parameters["scene"]
        self.sample_id = visualization_parameters["sample_id"]

        self.pixel_meter_ratios = raw_parameters["pixel_meter_ratios"]
        self.meter2pixel_ratio = 1.0/ self.pixel_meter_ratios[self.scene]

        # report_name = evaluation_parameters["report_name"]
        report_name = visualization_parameters["report_name"]

        sub_dir_name = parameters_project["evaluation_reports"] + "{}/scene_reports/".format(report_name)

        self.scene_samples = sub_dir_name + "{}_samples.json".format(self.scene)
        self.gif_name = parameters_project["animations_reports"] + "{}_{}_{}.gif".format(self.scene,self.sample_id,report_name)

        self.image = parameters_project["raw_images"] + "{}.jpg".format(self.scene)
        self.rev_dict_types = processed_parameters["types_dic_rev"]

    def animate_sample(self):
        file_ = json.load(open(self.scene_samples))
        sample = file_[str(self.sample_id)]
        inputs = np.array(sample["inputs"])

        labels = np.array(sample["labels"])
        outputs = np.array(sample["outputs"])
        types = np.array(sample["types"])
        print(types)
        types = [ self.rev_dict_types[str(int(type_))] for type_ in types]

        img = mpimg.imread(self.image)

        prediction = np.concatenate([inputs,outputs], axis = 1)
        gt = np.concatenate([inputs,labels], axis = 1)

        prediction = prediction * self.meter2pixel_ratio
        gt = gt * self.meter2pixel_ratio

        nb_colors = gt.shape[0]

        colors = get_colors(nb_colors)

        animator = Animate(prediction,gt,colors,img,types,self.gif_name)
        animator.animate()

class Animate():
    def __init__(self,data_pred,data_gt,colors,img,types,gif_name = "test.gif", plot_ = False, save = True):

        self.img = img
        self.xs_pred = data_pred[:,:,0]
        self.ys_pred = data_pred[:,:,1]

        self.xs_gt = data_gt[:,:,0]
        self.ys_gt = data_gt[:,:,1]

        self.types = types

        self.nb_agents = self.xs_pred.shape[0]
        self.margin = 1

        self.nb_frames = self.xs_pred.shape[1]
        self.gif_name = gif_name
        self.plot_ = plot_
        self.save = save

        self.fps = 1
        self.colors = colors

        self.lin_size = 100

        lin = np.linspace(0.6, 0.8, self.lin_size)

        self.color_dict = {
            "bicycle":cm.Blues(lin),
            "pedestrian":cm.Reds(lin),
            "car":cm.Greens(lin),
            "skate":cm.Greys(lin),
            "cart":cm.Purples(lin),
            "bus":cm.Oranges(lin)
        }

        self.colors = [self.color_dict[type_][np.random.randint(self.lin_size)] for type_ in self.types]

        self.history = 4

        self.get_plots()

    def get_plots(self):
        self.fig, self.ax = plt.subplots(1,2,squeeze= False)

        red_patch = mpatches.Patch(color='red', label='Pedestrians')
        blue_patch = mpatches.Patch(color='b', label='Bycicles')
        green_patch = mpatches.Patch(color='green', label='Cars')
        grey_patch = mpatches.Patch(color='grey', label='Skates')
        purple_patch = mpatches.Patch(color='purple', label='Carts')
        orange_patch = mpatches.Patch(color='orange', label='Buses')

        plt.legend(handles=[red_patch,blue_patch,green_patch,grey_patch,purple_patch,orange_patch],loc='best',fontsize = 3.5)
        self.ax[0][0].imshow(self.img,origin = "upper")
        self.ax[0][1].imshow(self.img,origin = "upper")

        self.plots1 = []
        self.plots2 = []

        for i in range(self.nb_agents):
            tup = self.ax[0][0].plot([], [], color = self.colors[i],marker = 'o',markersize = 2,linewidth = 0.5)[0]

            if i == 0:
                tup = self.ax[0][0].plot([], [], color = self.colors[i],marker = '^',markersize = 2,linewidth = 0.5)[0]

            self.plots1.append(tup)

            tup = self.ax[0][1].plot([], [], color = self.colors[i],marker = 'o',markersize = 2,linewidth = 0.5)[0]

            if i == 0:
                tup = self.ax[0][1].plot([], [], color = self.colors[i],marker = '^',markersize = 2,linewidth = 0.5)[0]

            self.plots2.append(tup)

    def animate(self):
        self.ax[0][1].set_title("Groundtruth",loc = "left", fontsize=8)
        self.ax[0][0].set_title("Predictions",loc = "left", fontsize=8)

        plt.tight_layout()

        ani = matplotlib.animation.FuncAnimation(self.fig, self.update, frames=self.nb_frames,repeat=True)

        if self.plot_:
            plt.show()
        if self.save:
            ani.save(self.gif_name, writer='imagemagick', fps=self.fps,dpi = 200)

    def update(self,frame):
        frame = int(frame)
        end = frame + 1
        start = max(0,end-self.history)

        if end < 9:
            self.fig.suptitle("Timestep: {}, observation time".format(frame+1), fontsize=8)
        else:
            self.fig.suptitle("Timestep: {}, prediction time".format(frame+1), fontsize=8)

        for i,p in enumerate(self.plots1):

            xs = self.xs_pred[i]
            ys = self.ys_pred[i]

            c = 0
            for x,y in zip(xs,ys):
                if x == 0 and y == 0:
                    c += 1
                else:
                    break
            xs = xs[c:]
            ys = ys[c:]

            p.set_data(xs[start:end], ys[start:end])
            # p.set_color(self.colors[i])

            if frame > 7 :
                p.set_marker("+")
                p.set_markersize(3)

                # p.set_fillstyle("none")

        for i,p in enumerate(self.plots2):
            xs = self.xs_gt[i]
            ys = self.ys_gt[i]

            c = 0
            for x,y in zip(xs,ys):
                if x == 0 and y == 0:
                    c += 1
                else:
                    break
            xs = xs[c:]
            ys = ys[c:]

            p.set_data(xs[start:end], ys[start:end])
            # p.set_data(self.xs_gt[i,start:end], self.ys_gt[i,start:end])
            # p.set_color(self.colors[i])

            if frame > 7 :
                p.set_marker("+")
                p.set_markersize(3)

if __name__ == "__main__":
    main()


from ..libs import *
from . import SubResultUI

class ResultTableUI(QWidget):

    def __init__(self):
        super().__init__()
        self.init_ui()
        self.results = None
        self.results_ui = {}

    def init_ui(self):
        self.layout = QHBoxLayout()
        self.table = QTableWidget()
        self.table.setColumnCount(7)
        self.table.setRowCount(6)
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
        self.table.setSortingEnabled(True)
        self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.table.setHorizontalHeaderLabels(['序号', 'res文件', '匹配成功', '匹配上原子数', '加权匹配比例', '坐标匹配残差', '操作'])
        self.table.verticalHeader().setHidden(True)
        self.layout.addWidget(self.table)

    def updateResults(self, results):
        self.results = results
        self.results_ui = {}
        l = len(results)
        self.table.clearContents()
        self.table.sortByColumn(0, Qt.AscendingOrder)
        self.table.setRowCount(l)
        for i in range(l):
            r = results[i]
            index = QTableWidgetItem()
            target = QTableWidgetItem()
            is_match = QTableWidgetItem()
            max_nm = QTableWidgetItem()
            max_rwm = QTableWidgetItem()
            min_mse = QTableWidgetItem()
            index.setData(Qt.DisplayRole, i + 1)
            target.setText(r.target.name)
            is_match.setText('是' if r.is_matched else '否')
            max_nm.setData(Qt.DisplayRole, r.best_feature[0] if r.is_matched else 0)
            max_rwm.setText('%.1f%%' % (r.best_feature[1] * 100) if r.is_matched else '')
            min_mse.setText('%.2f' % (r.best_feature[2]) if r.is_matched else '')
            self.table.setItem(i, 0, index)
            self.table.setItem(i, 1, target)
            self.table.setItem(i, 2, is_match)
            self.table.setItem(i, 3, max_nm)
            self.table.setItem(i, 4, max_rwm)
            self.table.setItem(i, 5, min_mse)

            if r.is_matched:
                bt_view = self.generate_button(r)
                self.table.setCellWidget(i, 6, bt_view)

    def generate_button(self, result):
        button = QPushButton('查看')
        bt_widget = QWidget()
        hLayout = QHBoxLayout()
        hLayout.addWidget(button)
        hLayout.setContentsMargins(5, 2, 5, 2)
        bt_widget.setLayout(hLayout)
        button.clicked.connect(lambda: self.view_result(result))
        return bt_widget

    def view_result(self, result):
        if result not in self.results_ui:
            self.results_ui[result] = SubResultUI(result)
        self.results_ui[result].show()


n1 = float(input())
n2 = float(input())
n3 = float(input())

media = (n1 + n2 + n3) / 3

if media >= 9:
    conceito = "Ótimo"
elif media >= 7.5:
    conceito = "Bom"
elif media >= 6:
    conceito = "Satisfatório"
else:
    conceito = "Insuficiente"

print(conceito)


import pandas as pd
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import classification_report

data = pd.read_csv('./ner_dataset.csv', encoding='latin1')
data = data.fillna(method="ffill")  # ffill前值填充，pfill后值填充
print(data.tail(10))

words = list(set(data["Word"].values))  # 获取词典库
n_words = len(words)  # 词典库大小

class MajorityVotingTagger(BaseEstimator, TransformerMixin):

    def fit(self, X, y):
        """
        :param X: list of words
        :param y: list 0f tags
        :return:
        """
        word2cnt = {}
        self.tags = []
        for x, t in zip(X, y):
            if t not in self.tags:
                self.tags.append(t)
            if x in word2cnt:
                if t in word2cnt[x]:
                    word2cnt[x][t] += 1
                else:
                    word2cnt[x][t] = 1
            else:
                word2cnt[x] = {t: 1}

        self.majority = {}
        for k, d in word2cnt.items():
            self.majority[k] = max(d, key=d.get)

    def predict(self, X, y=None):
        """Predict the the tag from memory, If word is unknown, predict 'o'"""
        return [self.majority.get(x, 'o') for x in X]

words = data["Word"].values.tolist()
tags = data["Tag"].values.tolist()

pred = cross_val_predict(estimator=MajorityVotingTagger(), X=words, y=tags, cv=5)
report = classification_report(y_pred=pred, y_true=tags)
print(report)


import pandas as pd
#data process
train1=pd.read_csv('taijing/df_affinity_train.csv')
train2=pd.read_csv('taijing/df_molecule.csv')
test=pd.read_csv('taijing/df_affinity_test_toBePredicted.csv')

train1=pd.DataFrame(train1)
train2=pd.DataFrame(train2)
test=pd.DataFrame(test)

test.columns = ['Protein_ID','Molecule_ID','Ki']
test['Ki']=0
del test['Ki']
# del test['Ki']
train1.columns = ['Protein_ID','Molecule_ID','Ki']
# train1.dropna(inplace=True)
train1.fillna(0.0,inplace=True)
train2.columns = ['Molecule_ID','Fingerprint','cyp_3a4','cyp_2c9','cyp_2d6','ames_toxicity','fathead_minnow_toxicity','tetrahymena_pyriformis_toxicity','honey_bee','cell_permeability','logP','renal_organic_cation_transporter','CLtotal','hia','biodegradation','Vdd','p_glycoprotein_inhibition','NOAEL','solubility','bbb']
# train2.dropna(inplace=True)
del train2['Fingerprint']
train2.fillna(0.0,inplace=True)
test.fillna(0.0,inplace=True)
train1.fillna(0.0,inplace=True)
# train2.to_csv('taijing/df_molecule_drop.csv')
# test_fianll=test.concat(train2, keys=['Molecule_ID'])
test_fianll=pd.merge(test,train2)
train_finall=pd.merge(train1,train2)
test_fianll.fillna(0.0,inplace=True)
train_finall.fillna(0.0,inplace=True)
# print(train.head(6))
train_finall.to_csv('taijing/df_affinity_train_combine.csv')
test_fianll.to_csv('taijing/df_affinity_test_combine.csv')


import tensorflow as tf
import numpy as np

tf.enable_eager_execution()

# Dataset
import tensorflow_datasets as tfds

# Constants to eventually parameterise
LOGDIR = './logs/autoencoder_gg/'

# Activation function to use for layers
act_func = tf.nn.tanh

# Enable or disable GPU
SESS_CONFIG = tf.ConfigProto(device_count = {'GPU': 1})

class Model:
    ' Simple Image Classification Model (defined by CNN) '

    def __init__(self, input_shape, num_layers=4, activation=tf.nn.relu, layer_width=64, bottleneck_chans = 4, learn_rate=1e-4):
        ' Initializes model parameters and optimizer '
        ' Assumes an input shape of [height, width, channels]'

        # Stores model params
        self.vars = []
        self.layers = []
        self.input_shape = input_shape
        self.shape_list = []
        self.shape_list.append(input_shape)

        # Down-sampling Layers
        for l in range(num_layers):
            # First layer
            if(l == 0):
                in_chans = input_shape[2]
                out_chans = layer_width
                cur_shape = [1,] + input_shape
            # Last Layer
            elif(l == num_layers-1):
                in_chans = out_chans
                out_chans = bottleneck_chans
            # Middle layers
            else:
                in_chans = out_chans
                out_chans = layer_width

            f_height = 5
            f_width = 5

            layer = tf.layers.Conv2D(out_chans, (f_height, f_width), strides=[1,1], padding='valid', activation=activation, kernel_initializer=tf.initializers.random_normal, bias_initializer=tf.initializers.random_normal, name='Conv'+str(l))
            layer.build(cur_shape)
            cur_shape = layer.compute_output_shape(cur_shape)
            self.shape_list.append(cur_shape)
            self.layers.append(layer)

        # Up-sampling Layers
        for l in range(num_layers):
            # First layer
            if(l == 0):
                in_chans = bottleneck_chans
                out_chans = layer_width
            # Last Layer
            elif(l == num_layers-1):
                in_chans = out_chans
                out_chans = input_shape[2]
            # Middle layers
            else:
                in_chans = out_chans
                out_chans = layer_width

            f_height = 5
            f_width = 5

            layer = tf.layers.Conv2DTranspose(out_chans, (f_height, f_width), strides=[1,1], padding='valid', activation=activation, kernel_initializer=tf.initializers.random_normal, bias_initializer=tf.initializers.random_normal, name='ConvTP'+str(l))
            layer.build(cur_shape)
            cur_shape = layer.compute_output_shape(cur_shape)
            self.shape_list.append(cur_shape)
            self.layers.append(layer)

        # Our Optimizer
        self.optimizer = tf.train.AdamOptimizer(learn_rate)

        # Grab all variables
        for l in self.layers:
            self.vars.extend(l.weights)
        for idx,shape in enumerate(self.shape_list):
            if(idx == 0):
                out_shape = None
            else:
                out_shape = self.layers[idx-1].weights[0].shape

            print('Layer: ', str(idx), shape, 'Weights: ', out_shape)

    def crunch(self, x_input):
        ' Generates outputs (predictions) from inputs to the model '

        with tf.name_scope('MainGraph'):
            for l in range(len(self.layers)):
                if(l == 0):
                    h = self.layers[0](x_input)
                    tf.contrib.summary.image(self.layers[l].name, h[:,:,:,:3], max_images=1)
                else:
                    h = self.layers[l](h)
                    tf.contrib.summary.image(self.layers[l].name, h[:,:,:,:3], max_images=1)

            #x_hat = tf.sigmoid(h)
            #x_hat = h
            x_hat = tf.sigmoid(tf.image.per_image_standardization(h))
            return x_hat

    def learn(self, x_input):
        ' Learns from the batch '

        # Track gradients
        with tf.GradientTape() as tape:
            tape.watch(x_input)
            output = self.crunch(x_input)
            tf.contrib.summary.image('Reconstructed Image', output, max_images=3)
            with tf.name_scope('Generation_Loss'):
                reconstruction_loss = tf.losses.mean_squared_error(labels=x_input, predictions=output)
                tf.contrib.summary.scalar('Recon Loss', reconstruction_loss)

                grads = tape.gradient(reconstruction_loss, self.vars)
                self.optimizer.apply_gradients(zip(grads, self.vars))
                #self.optimizer.apply_gradients(zip(grads, self.layers[0].weights))
                global_step.assign_add(1)
                return output, reconstruction_loss

    def validate(self, x_input):
        ' Takes an image from the validation set and produces an output from it '

        output = self.crunch(x_input)
        output_rs = tf.reshape(output, [-1, self.input_shape[0]*self.input_shape[1], self.input_shape[2]])
        x_input_rs = tf.reshape(x_input, [-1, self.input_shape[0]*self.input_shape[1], self.input_shape[2]])
        # Get last three of each
        concat = tf.concat([x_input_rs, output_rs], axis=1)
        concat_img = tf.reshape(concat, [-1, self.input_shape[0]*2, self.input_shape[1], self.input_shape[2]])
        tf.contrib.summary.image('Validation Pair', concat_img, max_images=3)

        for l in self.layers:
            tf.contrib.summary.histogram('Weights_'+l.name, l.weights[0])
            tf.contrib.summary.histogram('Biases_'+l.name, l.weights[1])

# Get Data
# Construct a tf.data.Dataset
#ds_name = 'mnist'
#ds_name = 'cifar10'
#ds_name = 'cifar100'
#ds_name = 'omniglot'
ds_name = 'celeb_a'
#ds_name = 'fashion_mnist'
(ds_train, ds_test), ds_info = tfds.load(name=ds_name, split=['train', 'test'], with_info=True)
img_shape = tf.TensorShape(ds_info.features['image'].shape)
print('DS Shape: ')
print(img_shape)

summary_writer = tf.contrib.summary.create_file_writer(LOGDIR+ds_name, flush_millis=100)
summary_writer.set_as_default()
global_step = tf.train.get_or_create_global_step()

# Creates a classifier model
model = Model(img_shape.as_list())

# Preparing datasets (training and validation)
# Batch size of 1024 the repeats when iterated through
ds_train = ds_train.batch(64).repeat()
ds_test = ds_test.batch(64).repeat()

# Converts validation set into an iterator so we can iterate through it
ds_test_iter = iter(ds_test)

# Perform the training loop (forever)
for idx,batch in enumerate(ds_train):
    # Prepare training inputs
    x_inputs = tf.math.divide(tf.cast(batch['image'], tf.float32), tf.constant(255.0, dtype=tf.float32))
    # Prepare validation inputs
    val_batch = next(ds_test_iter)
    val_x_inputs = tf.math.divide(tf.cast(val_batch['image'], tf.float32), tf.constant(255.0, dtype=tf.float32))
    # Train and validate
    with tf.contrib.summary.record_summaries_every_n_global_steps(10):
        preds, loss = model.learn(x_inputs)
        print('idx: ', idx, 'Loss: ', loss.numpy())
        model.validate(val_x_inputs)


import torch
import torchvision

# post-processing
def handle_preds(preds, device, conf_thresh=0.25, nms_thresh=0.45):
    total_bboxes, output_bboxes  = [], []
    # 将特征图转换为检测框的坐标
    N, C, H, W = preds.shape
    bboxes = torch.zeros((N, H, W, 6))
    pred = preds.permute(0, 2, 3, 1)
    # 前背景分类分支
    pobj = pred[:, :, :, 0].unsqueeze(dim=-1)
    # 检测框回归分支
    preg = pred[:, :, :, 1:5]
    # 目标类别分类分支
    pcls = pred[:, :, :, 5:]

    # 检测框置信度
    bboxes[..., 4] = (pobj.squeeze(-1) ** 0.6) * (pcls.max(dim=-1)[0] ** 0.4)
    bboxes[..., 5] = pcls.argmax(dim=-1)

    # 检测框的坐标
    gy, gx = torch.meshgrid([torch.arange(H), torch.arange(W)])
    bw, bh = preg[..., 2].sigmoid(), preg[..., 3].sigmoid()
    bcx = (preg[..., 0].tanh() + gx.to(device)) / W
    bcy = (preg[..., 1].tanh() + gy.to(device)) / H

    # cx,cy,w,h = > x1,y1,x2,y1
    x1, y1 = bcx - 0.5 * bw, bcy - 0.5 * bh
    x2, y2 = bcx + 0.5 * bw, bcy + 0.5 * bh

    bboxes[..., 0], bboxes[..., 1] = x1, y1
    bboxes[..., 2], bboxes[..., 3] = x2, y2
    bboxes = bboxes.reshape(N, H*W, 6)
    total_bboxes.append(bboxes)

    batch_bboxes = torch.cat(total_bboxes, 1)

    # 对检测框进行NMS处理
    for p in batch_bboxes:
        output, temp = [], []
        b, s, c = [], [], []
        # 阈值筛选
        t = p[:, 4] > conf_thresh
        pb = p[t]
        for bbox in pb:
            obj_score = bbox[4]
            category = bbox[5]
            x1, y1 = bbox[0], bbox[1]
            x2, y2 = bbox[2], bbox[3]
            s.append([obj_score])
            c.append([category])
            b.append([x1, y1, x2, y2])
            temp.append([x1, y1, x2, y2, obj_score, category])
        # Torchvision NMS
        if len(b) > 0:
            b = torch.Tensor(b).to(device)
            c = torch.Tensor(c).squeeze(1).to(device)
            s = torch.Tensor(s).squeeze(1).to(device)
            keep = torchvision.ops.batched_nms(b, s, c, nms_thresh)
            for i in keep:
                output.append(temp[i])
        output_bboxes.append(torch.Tensor(output))
    return output_bboxes


from typing import List

from fastapi_utils.inferring_router import InferringRouter
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import Depends, Request

from admins.models import Category
from crud_handler import BaseHandler
from database import get_async_session
from fastapi_utils.cbv import cbv
from sqlalchemy import select

from permissions import manage_helpdesk
from staff.models import Group, User
from staff.schemas import GroupSchemaReturn, GroupSchemaCreate, UserSchemaReturn, UserSchemaCreate

group_router = InferringRouter(tags=["Group"])
ROUTE = "/api/groups"

user_router = InferringRouter(tags=["User"])
ROUTE_USER = "/api/users"

@cbv(group_router)
class GroupView(BaseHandler):
    session: AsyncSession = Depends(get_async_session)

    def __init__(self):
        super().__init__(Group)

    @group_router.post(f"{ROUTE}/", response_model=GroupSchemaReturn, status_code=201)
    async def create_item(self, group_object: GroupSchemaCreate, request: Request):
        await manage_helpdesk(request)
        return await self.create(self.session, group_object.dict(), object_name="Group")

    @group_router.get(f"{ROUTE}/", response_model=List[GroupSchemaReturn], status_code=200)
    async def read_groups(self,
                          request: Request,
                          offset: int = 0,
                          limit: int = 5):
        await manage_helpdesk(request)
        query = select(self.model)
        return await self.list(query=query,
                               session=self.session,
                               limit=limit,
                               offset=offset)

    @group_router.get(f"{ROUTE}/" + "{group_id}", response_model=GroupSchemaReturn, status_code=200)
    async def read_group(self, group_id: int, request: Request):
        await manage_helpdesk(request)
        query = select(self.model)
        return await self.retrieve(query, self.session, group_id)

    @group_router.delete(f"{ROUTE}/" + "{group_id}", status_code=204)
    async def delete_group(self, group_id: int, request: Request):
        await manage_helpdesk(request)
        return await self.delete(self.session, group_id)

    @group_router.put(f"{ROUTE}/" + "{group_id}", response_model=GroupSchemaReturn, status_code=200)
    async def update_group(self, group_id: int, group: GroupSchemaReturn, request: Request):
        await manage_helpdesk(request)
        group_obj = await self.update(self.session, group_id, group.dict())
        await self.session.commit()
        return group_obj

@cbv(user_router)
class UserView(BaseHandler):
    session: AsyncSession = Depends(get_async_session)

    def __init__(self):
        super().__init__(User)

    @user_router.post(f"{ROUTE_USER}/", response_model=UserSchemaReturn, status_code=201)
    async def create_item(self, user_object: UserSchemaCreate, request: Request):
        await manage_helpdesk(request)

        user_dict = user_object.dict()
        group_obj = await self.get_obj(select(Group), self.session, {"id": user_dict.get("group").get("id")})
        category_object = await self.get_obj(select(Category), self.session, {"id": user_dict.get("category").get("id")})
        user_dict["group"] = group_obj
        user_dict["category"] = category_object
        return await self.create(self.session, user_dict, object_name="User")


import os
import shutil
import subprocess
from pathlib import Path

import numpy as np
from PIL import Image, ImageOps

from lib import BruteForce, database
from .utils import timeit

MAX_RATIO = 0.90  # 0.60
MIN_RATIO = 0

# PARAM FOR THE RADIOMETRIC APPROACH
# Try to normalize respect mean and std to reject static frames
RESIZE_SCALE_FACTOR = 1  # It can be usefull for reduce computation time
INNOVATION_THRESH = 0.001  # 1.5

def RootSift(img_name, desc_folder, N_kpts):
    np_kpt_path = Path("{}.kpt.npy".format(img_name))
    abs_np_kpt_path = desc_folder / np_kpt_path
    np_dsc_path = Path("{}.dsc.npy".format(img_name))
    abs_np_dsc_path = desc_folder / np_dsc_path
    kp = np.load(abs_np_kpt_path)
    desc = np.load(abs_np_dsc_path)
    kp_numb = kp.shape[0]

    return kp, desc, kp_numb

def NextImg(last_img):
    if last_img + 1 < 10:
        next_img = "00000{}".format(last_img + 1)
    elif last_img + 1 < 100:
        next_img = "0000{}".format(last_img + 1)
    elif last_img + 1 < 1000:
        next_img = "000{}".format(last_img + 1)
    elif last_img + 1 < 10000:
        next_img = "00{}".format(last_img + 1)
    elif last_img + 1 < 100000:
        next_img = "0{}".format(last_img + 1)
    elif last_img + 1 < 1000000:
        next_img = "{}".format(last_img + 1)
    return next_img

# @timeit
def StaticRejection(
    STATIC_IMG_REJECTION_METHOD,
    img1,
    img2,
    IMGS_FROM_SERVER,
    CURRENT_DIR,
    KEYFRAMES_DIR,
    COLMAP_EXE_PATH,
    MAX_N_FEATURES,
    ref_matches,
    DEBUG,
    newer_imgs,
    last_img,
    img_dict,
    img_batch,
    pointer,
    colmap_exe,
):
    # ROOTSIFT APPROACH
    if STATIC_IMG_REJECTION_METHOD == "root_sift":
        TEMP_DIR = CURRENT_DIR / "temp"
        shutil.rmtree(TEMP_DIR / "pair")
        os.makedirs(TEMP_DIR / "pair")
        shutil.copy(
            IMGS_FROM_SERVER / "{}".format(img1), TEMP_DIR / "pair" / "{}".format(img1)
        )
        shutil.copy(
            IMGS_FROM_SERVER / "{}".format(img2), TEMP_DIR / "pair" / "{}".format(img2)
        )

        subprocess.run(
            [
                COLMAP_EXE_PATH / f"{colmap_exe}",
                "database_creator",
                "--database_path",
                TEMP_DIR / "db.db",
            ],
            stdout=subprocess.DEVNULL,
        )
        subprocess.run(
            [
                COLMAP_EXE_PATH / f"{colmap_exe}",
                "feature_extractor",
                "--database_path",
                TEMP_DIR / "db.db",
                "--image_path",
                TEMP_DIR / "pair",
                "SiftExtraction.max_num_features",
                str(MAX_N_FEATURES),
            ],
            stdout=subprocess.DEVNULL,
        )
        # subprocess.run(["python3", CURRENT_DIR / "lib" / "RootSIFT.py", "--Path", TEMP_DIR / "db.db", "--Output", TEMP_DIR], stdout=subprocess.DEVNULL)
        subprocess.run(
            [
                COLMAP_EXE_PATH / f"{colmap_exe}",
                "sequential_matcher",
                "--database_path",
                TEMP_DIR / "db.db",
                "--SequentialMatching.overlap",
                "1",
            ],
            stdout=subprocess.DEVNULL,
        )
        # subprocess.run([COLMAP_EXE_PATH / f"{colmap_exe}", "mapper", "--project_path", CURRENT_DIR / "lib" / "mapper_for_static_rejection.ini"], stdout=subprocess.DEVNULL)

        # kp1, desc1, kp_numb1 = RootSift(img1, TEMP_DIR, 8000)
        # kp2, desc2, kp_numb2 = RootSift(img2, TEMP_DIR, 8000)
        # opencv_matches = BrForce(desc1, desc2, 'Lowe_ratio_test', 'L2', True, 'intersection', print_debug = False, ratio_thresh=0.8
        # matches_matrix = np.zeros((len(opencv_matches), 2))
        # for l in range(0,len(opencv_matches)):
        #    matches_matrix[l][0] = int(opencv_matches[l].queryIdx)
        #    matches_matrix[l][1] = int(opencv_matches[l].trainIdx

        db_p = TEMP_DIR / "db.db"
        matches = database.dbReturnMatches(db_p.as_posix(), 15)
        os.remove(TEMP_DIR / "db.db")

        if len(matches.keys()) != 0:
            key = list(matches.keys())[0]
            matches_matrix = matches[key]

            if ref_matches == []:
                ref_matches = matches_matrix
                shutil.copy(
                    IMGS_FROM_SERVER / "{}".format(img1),
                    KEYFRAMES_DIR / "{}.jpg".format(NextImg(int(last_img))),
                )
                shutil.copy(
                    IMGS_FROM_SERVER / "{}".format(img2),
                    KEYFRAMES_DIR / "{}.jpg".format(NextImg(int(last_img) + 1)),
                )
                img_dict["{}".format(img1)] = "{}.jpg".format(NextImg(int(last_img)))
                img_dict["{}".format(img2)] = "{}.jpg".format(
                    NextImg(int(last_img) + 1)
                )
                pointer += 1
                return (
                    ref_matches,
                    newer_imgs,
                    NextImg(int(last_img) + 1),
                    img_dict,
                    img_batch,
                    pointer,
                )  # pointer, delta,
            else:
                vec_ref = ref_matches[:, 1]
                vec = matches_matrix[:, 0]
                vec_ref = vec_ref.tolist()
                vec = vec.tolist()
                vec_ref = [int(v) for v in vec_ref]
                vec = [int(v) for v in vec]
                intersection = [el for el in vec if el in vec_ref]

                control_ratio = len(intersection) / len(vec_ref)
                print("control_ratio", control_ratio)

                if (
                    control_ratio < MAX_RATIO and control_ratio > MIN_RATIO
                ):  # and os.path.exists(TEMP_DIR / "0"):
                    # shutil.copy(IMGS_FROM_SERVER / "{}".format(img1), KEYFRAMES_DIR / "{}".format(img1))
                    shutil.copy(
                        IMGS_FROM_SERVER / "{}".format(img2),
                        KEYFRAMES_DIR / "{}.jpg".format(NextImg(int(last_img))),
                    )
                    img_dict["{}".format(img2)] = "{}.jpg".format(
                        NextImg(int(last_img))
                    )
                    print("\n.. added img\n")
                    ref_matches = matches_matrix
                    pointer += 1  # + delta
                    # delta = 0
                    newer_imgs = True
                    img_batch.append(img2)
                    return (
                        ref_matches,
                        newer_imgs,
                        NextImg(int(last_img)),
                        img_dict,
                        img_batch,
                        pointer,
                    )  # pointer, delta,

                else:
                    # delta += 1
                    print("\n.. NO\n")
                    return (
                        ref_matches,
                        newer_imgs,
                        last_img,
                        img_dict,
                        img_batch,
                        pointer,
                    )  # pointer, delta,

        elif len(matches.keys()) == 0:
            # delta += 1
            print("\n.. NO .. len(matches.keys()) == 0\n")
            return (
                ref_matches,
                newer_imgs,
                last_img,
                img_dict,
                img_batch,
                pointer,
            )  # pointer, delta,

    # RADIOMETRIC APPROACH
    elif STATIC_IMG_REJECTION_METHOD == "radiometric":
        # 'Try' is necessary because main loop looks for new images and the last one can be incomplete because
        # it is copied from other folders, and the procedure can be unfineshed
        try:
            im1 = Image.open(IMGS_FROM_SERVER / img1)
            im2 = Image.open(IMGS_FROM_SERVER / img2)
            im1.resize(
                (
                    round(im1.size[0] * RESIZE_SCALE_FACTOR),
                    round(im1.size[1] * RESIZE_SCALE_FACTOR),
                )
            )
            im2.resize(
                (
                    round(im2.size[0] * RESIZE_SCALE_FACTOR),
                    round(im2.size[1] * RESIZE_SCALE_FACTOR),
                )
            )
            im1_gray = ImageOps.grayscale(im1)
            im2_gray = ImageOps.grayscale(im2)

            # Normalization
            im1_array = np.array(im1_gray)
            im1_array = (im1_array - np.min(im1_array)) / np.max(im1_array)
            im2_array = np.array(im2_gray)
            im2_array = (im2_array - np.min(im2_array)) / np.max(im2_array)

            mean1 = np.mean(im1_array)
            mean2 = np.mean(im2_array)

            # innovation = np.sum(((im1_array - np.mean(im1_array)) * (im2_array - np.mean(im2_array))) / (np.std(im1_array) * np.std(im2_array)))
            # ref = np.sum(((im1_array - np.mean(im1_array)) * (im1_array - np.mean(im1_array))) / (np.std(im1_array) * np.std(im1_array)))
            # innovation = innovation/ref

            innovation = np.absolute(mean2 - mean1)

            if innovation > INNOVATION_THRESH:
                if ref_matches == []:
                    ref_matches = [
                        "-"
                    ]  # It is used for compatibilities with frame rejection approches that needs matches matrix
                    shutil.copy(
                        IMGS_FROM_SERVER / "{}".format(img1),
                        KEYFRAMES_DIR / "{}.jpg".format(NextImg(int(last_img))),
                    )
                    shutil.copy(
                        IMGS_FROM_SERVER / "{}".format(img2),
                        KEYFRAMES_DIR / "{}.jpg".format(NextImg(int(last_img) + 1)),
                    )
                    img_dict["{}".format(img1)] = "{}.jpg".format(
                        NextImg(int(last_img))
                    )
                    img_dict["{}".format(img2)] = "{}.jpg".format(
                        NextImg(int(last_img) + 1)
                    )
                    pointer += 1
                    return (
                        ref_matches,
                        newer_imgs,
                        NextImg(int(last_img) + 1),
                        img_dict,
                        img_batch,
                        pointer,
                    )

                elif ref_matches == ["-"]:
                    shutil.copy(
                        IMGS_FROM_SERVER / "{}".format(img2),
                        KEYFRAMES_DIR / "{}.jpg".format(NextImg(int(last_img))),
                    )
                    img_dict["{}".format(img2)] = "{}.jpg".format(
                        NextImg(int(last_img))
                    )
                    pointer += 1
                    newer_imgs = True
                    img_batch.append(img2)
                    return (
                        ref_matches,
                        newer_imgs,
                        NextImg(int(last_img)),
                        img_dict,
                        img_batch,
                        pointer,
                    )

            else:
                print("!! Frame rejeccted. innovation < INNOVATION_THRESH !!", end="\r")
                return ref_matches, newer_imgs, last_img, img_dict, img_batch, pointer

        except:
            print("!! Frame truncated !!")
            return ref_matches, newer_imgs, last_img, img_dict, img_batch, pointer

    else:
        print("Choose 'radiometric' or 'root_sift' as STATIC_IMG_REJECTION_METHOD")
        quit()


def solve(length, nums, sumnum):
    # method1
    hmap = {}
    for i in range(length):
        cnum = nums[i]
        if cnum not in hmap.keys():
            hmap[cnum] = i
        target = sumnum - cnum
        if target in hmap.keys():
            return [i, hmap[target]]
    return [-1, -1]

length = int(input())
nums = list(map(int, input().split()))
target = int(input().strip())

res = solve(length, nums, target)
print(" ".join(list(map(str, res))))


import time
import h5py
from affogato.segmentation import InteractiveMWS

def debug():
    z = 0
    path = '/home/pape/Work/data/ilastik/mulastik/data/data.h5'
    with h5py.File(path, 'r') as f:
        # raw = f['raw'][z]
        affs = f['prediction'][:, z]

    strides = [4, 4]
    offsets = [[-1, 0], [0, -1], [-3, 0], [0, -3],
               [-9, 0], [0, -9], [-27, 0], [0, -27]]

    with h5py.File('./seeds.h5') as f:
        seeds = f['data'][:]
    assert seeds.shape == affs.shape[1:]

    imws = InteractiveMWS(affs, offsets, n_attractive_channels=2,
                          strides=strides, randomize_strides=True)

    print("Compute segmentation without seeds ...")
    t0 = time.time()
    seg1 = imws()
    t0 = time.time() - t0
    print("... done in %f s" % t0)

    print("Add seeds ...")
    t0 = time.time()
    imws.update_seeds(seeds)
    t0 = time.time() - t0
    print("... done in %f s" % t0)

    print("Compute segmentation with seeds ...")
    t0 = time.time()
    seg2 = imws()
    t0 = time.time() - t0
    print("... done in %f s" % t0)

    assert seg1.shape == seg2.shape == seeds.shape

if __name__ == '__main__':
    debug()


"""Shifters Rig Main class."""
import datetime
import getpass
import os.path
import sys
import json

# Maya
import pymel.core as pm
from pymel.core import datatypes
from pymel import versions

# mgear
import mgear
import mgear.core.utils
from . import guide, component

from mgear.core import primitive, attribute, skin, dag, icon, node
from mgear import shifter_classic_components
from mgear import shifter_epic_components
from mgear.shifter import naming
import importlib
from mgear.core import utils

PY2 = sys.version_info[0] == 2

# check if we have loaded the necessary plugins
if not pm.pluginInfo("mgear_solvers", q=True, loaded=True):
    try:
        pm.loadPlugin("mgear_solvers")
    except RuntimeError:
        pm.displayError("You need the mgear_solvers plugin!")
if not pm.pluginInfo("matrixNodes", q=True, loaded=True):
    pm.loadPlugin("matrixNodes")

COMPONENT_PATH = os.path.join(os.path.dirname(__file__), "component")
TEMPLATE_PATH = os.path.join(COMPONENT_PATH, "templates")
SYNOPTIC_PATH = os.path.abspath(
    os.path.join(os.path.dirname(__file__), os.pardir, "synoptic", "tabs")
)

SHIFTER_COMPONENT_ENV_KEY = "MGEAR_SHIFTER_COMPONENT_PATH"

def log_window():
    if mgear.logMode and mgear.use_log_window:
        log_window_name = "mgear_shifter_build_log_window"
        log_window_field_reporter = "mgear_shifter_log_field_reporter"

        # call pm.window(log_window_name, exists=True) 2 times to avoid
        # false check in Maya 2024
        pm.window(log_window_name, exists=True)

        if not pm.window(log_window_name, exists=True):
            log_win = pm.window(
                log_window_name,
                title="Shifter Build Log",
                iconName="Shifter Log",
                width=800,
                height=500,
            )
            form = pm.formLayout()
            reporter = pm.cmdScrollFieldReporter(
                log_window_field_reporter, width=400, height=200, clr=True
            )

            btn_close = pm.button(
                label="Close",
                command=lambda *args: pm.deleteUI(log_win, window=True),
            )

            margin_v = 5
            margin_h = 5
            pm.formLayout(
                form,
                e=True,
                attachForm=[
                    (reporter, "top", margin_v),
                    (reporter, "right", margin_h),
                    (reporter, "left", margin_h),
                    (btn_close, "bottom", margin_v),
                    (btn_close, "right", margin_h),
                    (btn_close, "left", margin_h),
                ],
                attachControl=[
                    (reporter, "bottom", margin_v, btn_close),
                ],
            )

            pm.setParent("..")
            pm.showWindow(log_win)
        else:
            pm.cmdScrollFieldReporter(log_window_field_reporter, e=True, clr=True)
            pm.showWindow(log_window_name)
        mgear.logInfos()

def getComponentDirectories():
    """Get the components directory"""
    # TODO: ready to support multiple default directories
    return mgear.core.utils.gatherCustomModuleDirectories(
        SHIFTER_COMPONENT_ENV_KEY,
        [
            os.path.join(os.path.dirname(shifter_classic_components.__file__)),
            os.path.join(os.path.dirname(shifter_epic_components.__file__)),
        ],
    )
    # return mgear.core.utils.gatherCustomModuleDirectories(
    #     SHIFTER_COMPONENT_ENV_KEY,
    #     os.path.join(os.path.dirname(shifter_classic_components.__file__)))

def importComponentGuide(comp_type):
    """Import the Component guide"""
    dirs = getComponentDirectories()
    defFmt = "mgear.core.shifter.component.{}.guide"
    customFmt = "{}.guide"

    module = mgear.core.utils.importFromStandardOrCustomDirectories(
        dirs, defFmt, customFmt, comp_type
    )
    return module

def importComponent(comp_type):
    """Import the Component"""
    dirs = getComponentDirectories()
    defFmt = "mgear.core.shifter.component.{}"
    customFmt = "{}"

    module = mgear.core.utils.importFromStandardOrCustomDirectories(
        dirs, defFmt, customFmt, comp_type
    )
    return module

def reloadComponents(*args):
    """Reload all componets

    Args:
        *args: Dummy
    """
    compDir = getComponentDirectories()

    for x in compDir:
        for com in compDir[x]:
            try:
                if PY2:
                    reload(importComponent(com))
                    reload(importComponentGuide(com))
                else:
                    importlib.reload(importComponent(com))
                    importlib.reload(importComponentGuide(com))
                print("reload : {}.{}".format(os.path.basename(x), com))
            except ImportError:
                pass

class Rig(object):
    """The main rig class.

    Attributes:
        guide: guide.Rig() initialization.
        groups (dic): Rig groups (Maya sets)
        components (dic): Dictionary for the rig components.
            Keys are the component fullname (ie. 'arm_L0')
        componentsIndex (list): Components index list.

    """

    def __init__(self):

        self.guide = guide.Rig()

        self.groups = {}
        self.subGroups = {}

        self.components = {}
        self.componentsIndex = []

        self.customStepDic = {}

        self.build_data = {}

        self.component_finalize = False

    def buildFromDict(self, conf_dict):
        log_window()
        startTime = datetime.datetime.now()
        mgear.log("\n" + "= SHIFTER RIG SYSTEM " + "=" * 46)

        self.stopBuild = False

        self.guide.set_from_dict(conf_dict)
        endTime = datetime.datetime.now()
        finalTime = endTime - startTime
        mgear.log(
            "\n"
            + "= SHIFTER FILE READ {} [ {} ] {}".format("=" * 16, finalTime, "=" * 7)
        )

        # Build
        mgear.log("\n" + "= BUILDING RIG " + "=" * 46)
        self.from_dict_custom_step(conf_dict, pre=True)
        self.build()
        self.from_dict_custom_step(conf_dict, pre=False)
        # Collect post-build data
        build_data = self.collect_build_data()

        endTime = datetime.datetime.now()
        finalTime = endTime - startTime
        pm.flushUndo()
        pm.displayInfo(
            "Undo history have been flushed to avoid "
            "possible crash after rig is build. \n"
            "More info: "
            "https://github.com/miquelcampos/mgear/issues/72"
        )
        mgear.log(
            "\n"
            + "= SHIFTER BUILD RIG DONE {} [ {} ] {}".format(
                "=" * 16, finalTime, "=" * 7
            )
        )

        return build_data

    def buildFromSelection(self):
        """Build the rig from selected guides."""

        startTime = datetime.datetime.now()
        mgear.log("\n" + "= SHIFTER RIG SYSTEM " + "=" * 46)

        self.stopBuild = False
        selection = pm.ls(selection=True)
        if not selection:
            selection = pm.ls("guide")
            if not selection:
                mgear.log(
                    "Not guide found or selected.\n"
                    + "Select one or more guide root or a guide model",
                    mgear.sev_error,
                )
                return

        # check if is partial build or full guide build
        ismodel = False
        if selection[0].hasAttr("ismodel"):
            self.preCustomStep(selection)
            ismodel = True

        if not self.stopBuild:
            mgear.log("\n" + "= GUIDE VALIDATION " + "=" * 46)
            # Check guide is valid
            self.guide.setFromSelection()
            if not self.guide.valid:
                return

            # Build
            mgear.log("\n" + "= BUILDING RIG " + "=" * 46)
            self.build()
            if ismodel:
                self.postCustomStep()

            # Collect post-build data
            build_data = self.collect_build_data()

            endTime = datetime.datetime.now()
            finalTime = endTime - startTime
            pm.flushUndo()
            pm.displayInfo(
                "Undo history have been flushed to avoid "
                "possible crash after rig is build. \n"
                "More info: "
                "https://github.com/miquelcampos/mgear/issues/72"
            )
            mgear.log(
                "\n"
                + "= SHIFTER BUILD RIG DONE {} [ {} ] {}".format(
                    "=" * 16, finalTime, "=" * 7
                )
            )

        return build_data

    def build(self):
        """Build the rig."""

        self.options = self.guide.values
        self.guides = self.guide.components

        self.customStepDic["mgearRun"] = self

        self.initialHierarchy()
        self.processComponents()
        self.finalize()

        return self.model

    def stepsList(self, checker, attr):
        if self.options[checker] and self.options[attr]:
            return self.options[attr].split(",")
        else:
            return None

    def from_dict_custom_step(self, conf_dict, pre=True):
        if pre:
            pre_post = "doPreCustomStep"
            pre_post_path = "preCustomStep"
        else:
            pre_post = "doPostCustomStep"
            pre_post_path = "postCustomStep"
        p_val = conf_dict["guide_root"]["param_values"]
        if p_val[pre_post]:
            customSteps = p_val[pre_post_path]
            self.customStep(customSteps.split(","))

    def customStep(self, customSteps=None):
        if customSteps:
            for step in customSteps:
                if not self.stopBuild:
                    if step.startswith("*"):
                        continue
                    self.stopBuild = guide.helperSlots.runStep(
                        step.split("|")[-1][1:], self.customStepDic
                    )
                else:
                    pm.displayWarning("Build Stopped")
                    break

    def preCustomStep(self, selection):
        if (
            selection[0].hasAttr("ismodel")
            and selection[0].attr("doPreCustomStep").get()
        ):
            customSteps = selection[0].attr("preCustomStep").get()
            if customSteps:
                mgear.log("\n" + "= PRE CUSTOM STEPS " + "=" * 46)
                # use forward slash for OS compatibility
                if sys.platform.startswith("darwin"):
                    customSteps = [
                        cs.replace("\\", "/") for cs in customSteps.split(",")
                    ]
                    self.customStep(customSteps)
                else:
                    self.customStep(customSteps.split(","))

    def postCustomStep(self):
        customSteps = self.stepsList("doPostCustomStep", "postCustomStep")
        if customSteps:
            mgear.log("\n" + "= POST CUSTOM STEPS " + "=" * 46)
            # use forward slash for OS compatibility
            if sys.platform.startswith("darwin"):
                customSteps = [cs.replace("\\", "/") for cs in customSteps]
            self.customStep(customSteps)

    # @utils.timeFunc
    def get_guide_data(self):
        """Get the guide data

        Returns:
            str: The guide data
        """
        if self.guide.guide_template_dict:
            return json.dumps(self.guide.guide_template_dict)
        else:
            return json.dumps(self.guide.get_guide_template_dict())

    def initialHierarchy(self):
        """Build the initial hierarchy of the rig.

        Create the rig model, the main properties,
        and a couple of base organisation nulls.
        Get the global size of the rig.

        """
        mgear.log("Initial Hierarchy")

        # --------------------------------------------------
        # Model
        self.model = primitive.addTransformFromPos(None, self.options["rig_name"])
        attribute.lockAttribute(self.model)

        # --------------------------------------------------
        # INFOS
        self.isRig_att = attribute.addAttribute(self.model, "is_rig", "bool", True)
        self.rigName_att = attribute.addAttribute(
            self.model, "rig_name", "string", self.options["rig_name"]
        )
        self.user_att = attribute.addAttribute(
            self.model, "user", "string", getpass.getuser()
        )
        self.isWip_att = attribute.addAttribute(
            self.model, "wip", "bool", self.options["mode"] != 0
        )
        self.date_att = attribute.addAttribute(
            self.model, "date", "string", str(datetime.datetime.now())
        )
        self.mayaVersion_att = attribute.addAttribute(
            self.model,
            "maya_version",
            "string",
            str(pm.mel.eval("getApplicationVersionAsFloat")),
        )
        self.gearVersion_att = attribute.addAttribute(
            self.model, "gear_version", "string", mgear.getVersion()
        )
        self.synoptic_att = attribute.addAttribute(
            self.model, "synoptic", "string", str(self.options["synoptic"])
        )
        self.comments_att = attribute.addAttribute(
            self.model, "comments", "string", str(self.options["comments"])
        )
        self.ctlVis_att = attribute.addAttribute(self.model, "ctl_vis", "bool", True)
        if versions.current() >= 201650:
            self.ctlVisPlayback_att = attribute.addAttribute(
                self.model, "ctl_vis_on_playback", "bool", True
            )
        self.jntVis_att = attribute.addAttribute(self.model, "jnt_vis", "bool", True)
        # adding the always draw shapes on top to global attribute
        if versions.current() >= 20220000:
            self.ctlXRay_att = attribute.addAttribute(
                self.model, "ctl_x_ray", "bool", False
            )

        self.qsA_att = attribute.addAttribute(self.model, "quickselA", "string", "")
        self.qsB_att = attribute.addAttribute(self.model, "quickselB", "string", "")
        self.qsC_att = attribute.addAttribute(self.model, "quickselC", "string", "")
        self.qsD_att = attribute.addAttribute(self.model, "quickselD", "string", "")
        self.qsE_att = attribute.addAttribute(self.model, "quickselE", "string", "")
        self.qsF_att = attribute.addAttribute(self.model, "quickselF", "string", "")

        self.rigGroups = self.model.addAttr("rigGroups", at="message", m=1)
        self.rigPoses = self.model.addAttr("rigPoses", at="message", m=1)
        self.rigCtlTags = self.model.addAttr("rigCtlTags", at="message", m=1)
        self.rigScriptNodes = self.model.addAttr("rigScriptNodes", at="message", m=1)

        self.guide_data_att = attribute.addAttribute(
            self.model, "guide_data", "string", self.get_guide_data()
        )

        # ------------------------- -------------------------
        # Global Ctl
        if self.options["worldCtl"]:
            if self.options["world_ctl_name"]:
                name = self.options["world_ctl_name"]
            else:
                name = "world_ctl"

            icon_shape = "circle"

        else:
            name = "global_C0_ctl"
            icon_shape = "crossarrow"

        self.global_ctl = self.addCtl(
            self.model,
            name,
            datatypes.Matrix(),
            self.options["C_color_fk"],
            icon_shape,
            w=10,
        )
        attribute.setRotOrder(self.global_ctl, "ZXY")

        # Connect global visibility
        pm.connectAttr(self.ctlVis_att, self.global_ctl.attr("visibility"))
        if versions.current() >= 201650:
            pm.connectAttr(
                self.ctlVisPlayback_att, self.global_ctl.attr("hideOnPlayback")
            )
        attribute.lockAttribute(self.global_ctl, ["v"])

        # --------------------------------------------------
        # Setup in world Space
        self.setupWS = primitive.addTransformFromPos(self.model, "setup")
        attribute.lockAttribute(self.setupWS)
        # --------------------------------------------------
        # Basic set of null
        if self.options["joint_rig"]:
            self.jnt_org = primitive.addTransformFromPos(self.model, "jnt_org")
            if self.options["force_SSC"]:
                self.global_ctl.s >> self.jnt_org.s
            pm.connectAttr(self.jntVis_att, self.jnt_org.attr("visibility"))

    def processComponents(self):
        """
        Process the components of the rig, following the creation steps.
        """

        # Init
        self.components_infos = {}

        for comp in self.guide.componentsIndex:
            guide_ = self.guides[comp]
            mgear.log("Init : " + guide_.fullName + " (" + guide_.type + ")")

            module = importComponent(guide_.type)
            Component = getattr(module, "Component")

            comp = Component(self, guide_)
            if comp.fullName not in self.componentsIndex:
                self.components[comp.fullName] = comp
                self.componentsIndex.append(comp.fullName)

                self.components_infos[comp.fullName] = [
                    guide_.compType,
                    guide_.getVersion(),
                    guide_.author,
                ]

        # Creation steps
        self.steps = component.Main.steps
        for i, name in enumerate(self.steps):
            # for count, compName in enumerate(self.componentsIndex):
            for compName in self.componentsIndex:
                comp = self.components[compName]
                mgear.log(name + " : " + comp.fullName + " (" + comp.type + ")")
                comp.stepMethods[i]()
                if name == "Finalize":
                    self.component_finalize = True

            if self.options["step"] >= 1 and i >= self.options["step"] - 1:
                break

    def finalize(self):
        """Finalize the rig."""
        groupIdx = 0

        # Properties --------------------------------------
        mgear.log("Finalize")

        # clean jnt_org --------------------------------------
        if self.options["joint_rig"]:
            mgear.log("Cleaning jnt org")
            jnt_org_child = dag.findChildrenPartial(self.jnt_org, "org")
            if jnt_org_child:
                for jOrg in jnt_org_child:
                    if not jOrg.listRelatives(c=True):
                        pm.delete(jOrg)

        # Groups ------------------------------------------
        mgear.log("Creating groups")
        # Retrieve group content from components
        for name in self.componentsIndex:
            component_ = self.components[name]
            for name, objects in component_.groups.items():
                self.addToGroup(objects, name)
            for name, objects in component_.subGroups.items():
                self.addToSubGroup(objects, name)

        # Create master set to group all the groups
        masterSet = pm.sets(n=self.model.name() + "_sets_grp", em=True)
        pm.connectAttr(masterSet.message, self.model.rigGroups[groupIdx])
        groupIdx += 1

        # Creating all groups
        pm.select(cl=True)
        for name, objects in self.groups.items():
            s = pm.sets(n=self.model.name() + "_" + name + "_grp")
            s.union(objects)
            pm.connectAttr(s.message, self.model.rigGroups[groupIdx])
            groupIdx += 1
            masterSet.add(s)
        for parentGroup, subgroups in self.subGroups.items():
            pg = pm.PyNode(self.model.name() + "_" + parentGroup + "_grp")
            for sg in subgroups:
                sub = pm.PyNode(self.model.name() + "_" + sg + "_grp")
                if sub in masterSet.members():
                    masterSet.remove(sub)
                pg.add(sub)

        # create geo group

        geoSet = pm.sets(n=self.model.name() + "_geo_grp", em=True)
        pm.connectAttr(geoSet.message, self.model.rigGroups[groupIdx])
        masterSet.add(geoSet)
        groupIdx += 1

        # Bind pose ---------------------------------------
        # controls_grp = self.groups["controllers"]
        # pprint(controls_grp, stream=None, indent=1, width=100)
        ctl_master_grp = pm.PyNode(self.model.name() + "_controllers_grp")
        pm.select(ctl_master_grp, replace=True)
        dag_node = pm.dagPose(save=True, selection=True)
        pm.connectAttr(dag_node.message, self.model.rigPoses[0])
        print(dag_node)

        # hide all DG nodes inputs in channel box -----------------------
        # only hides if components_finalize or All steps are done
        if self.component_finalize:
            for c in self.model.listHistory(ac=True, f=True):
                if c.type() != "transform":
                    c.isHistoricallyInteresting.set(False)

        # Bind skin re-apply
        if self.options["importSkin"]:
            try:
                pm.displayInfo("Importing Skin")
                skin.importSkin(self.options["skin"])

            except RuntimeError:
                pm.displayWarning(
                    "Skin doesn't exist or is not correct. "
                    + self.options["skin"]
                    + " Skipped!"
                )

    def collect_build_data(self):
        """Collect post build data

        by default the data is stored in the root joint.

        Returns:
            dict: The collected data
        """
        self.build_data["Components"] = []
        for c, comp in self.customStepDic["mgearRun"].components.items():
            self.build_data["Components"].append(comp.build_data)

        if self.options["data_collector_embedded"]:
            root_jnt = self.get_root_jnt_embbeded()
            self.add_collected_data_to_root_jnt(root_jnt=root_jnt)
        if self.options["data_collector"]:
            self.data_collector_output(self.options["data_collector_path"])

        return self.build_data

    def data_collector_output(self, file_path=None):
        """Output collected data to a Json file

        Args:
            file_path (Str, optional): Output path for the Json File
        """
        if not file_path:
            ext_filter = "Shifter Collected data (*{})".format(guide.DATA_COLLECTOR_EXT)
            file_path = pm.fileDialog2(fileMode=0, fileFilter=ext_filter)[0]

        with open(file_path, "w") as f:
            f.write(json.dumps(self.build_data, indent=4))
            file_path = None

    def add_collected_data_to_root_jnt(self, root_jnt=None):
        """Add collected data to root joint
        Root joint is the first joint generated in the rig.
        """
        if not root_jnt:
            for c in self.componentsIndex:
                comp = self.customStepDic["mgearRun"].components[c]
                if not root_jnt and comp.jointList:
                    root_jnt = comp.jointList[0]
                    break
        if root_jnt:
            attribute.addAttribute(
                root_jnt,
                "collected_data",
                "string",
                str(json.dumps(self.build_data)),
            )

    def get_root_jnt_embbeded(self):
        """Get the root joint to embbed the data

        Returns:
            pyNode: Joint
        """
        j_name = self.options["data_collector_embedded_custom_joint"]
        if j_name:
            try:
                return pm.PyNode(j_name)
            except pm.MayaNodeError:
                pm.displayError("{} doesn't exist or is not unique".format(j_name))

    def addCtl(self, parent, name, m, color, iconShape, **kwargs):
        """Create the control and apply the shape, if this is alrealdy stored
        in the guide controllers grp.

        Args:
            parent (dagNode): The control parent
            name (str): The control name.
            m (matrix): The transfromation matrix for the control.
            color (int or list of float): The color for the control in index
                or RGB.
            iconShape (str): The controls default shape.
            kwargs (variant): Other arguments for the iconShape type variations

        Returns:
            dagNode: The Control.

        """
        if "degree" not in kwargs.keys():
            kwargs["degree"] = 1

        bufferName = name + "_controlBuffer"
        if bufferName in self.guide.controllers.keys():
            ctl_ref = self.guide.controllers[bufferName]
            ctl = primitive.addTransform(parent, name, m)
            for shape in ctl_ref.getShapes():
                ctl.addChild(shape, shape=True, add=True)
                pm.rename(shape, name + "Shape")
        else:
            ctl = icon.create(parent, name, m, color, iconShape, **kwargs)

        self.addToGroup(ctl, "controllers")

        # Set the control shapes isHistoricallyInteresting
        for oShape in ctl.getShapes():
            oShape.isHistoricallyInteresting.set(False)
            # connecting the always draw shapes on top to global attribute
            if versions.current() >= 20220000:
                pm.connectAttr(self.ctlXRay_att, oShape.attr("alwaysDrawOnTop"))

        # set controller tag
        if versions.current() >= 201650:
            pm.controller(ctl)
            self.add_controller_tag(ctl, None)

        attribute.addAttribute(ctl, "isCtl", "bool", keyable=False)
        attribute.addAttribute(
            ctl, "ctl_role", "string", keyable=False, value="world_ctl"
        )

        return ctl

    def addToGroup(self, objects, names=["hidden"]):
        """Add the object in a collection for later group creation.

        Args:
            objects (dagNode or list of dagNode): Object to put in the group.
            names (str or list of str): Names of the groups to create.

        """
        if not isinstance(names, list):
            names = [names]

        if not isinstance(objects, list):
            objects = [objects]

        for name in names:
            if name not in self.groups.keys():
                self.groups[name] = []

            self.groups[name].extend(objects)

    def addToSubGroup(self, subGroups, parentGroups=["hidden"]):
        """Add the object in a collection for later SubGroup creation.

        Args:
            subGroups (dagNode or list of dagNode): Groups (core set) to add
                as a Subgroup.
            namparentGroupses (str or list of str): Names of the parent groups
                to create.

        """

        if not isinstance(parentGroups, list):
            parentGroups = [parentGroups]

        if not isinstance(subGroups, list):
            subGroups = [subGroups]

        for pg in parentGroups:
            if pg not in self.subGroups.keys():
                self.subGroups[pg] = []
            self.subGroups[pg].extend(subGroups)

    def add_controller_tag(self, ctl, tagParent):
        ctt = node.add_controller_tag(ctl, tagParent)
        if ctt:
            ni = attribute.get_next_available_index(self.model.rigCtlTags)
            pm.connectAttr(
                ctt.message, self.model.attr("rigCtlTags[{}]".format(str(ni)))
            )

    def getLocalName(self, guideName):
        """This function return the local name, cutting the Maya fullname
        and taking the latest part.

            ie. "parentA|parentB|arm_C0_root" will return "arm_C0_root"

        Args:
            guideName (str): The guide name.

        Returns:
            str: The local Name

        """
        if guideName is None:
            return None
        localName = guideName.split("|")[-1]
        return localName

    def getComponentName(self, guideName, local=True):
        """
        This function return the component name

            ie. "arm_C0_root" return "arm_C0"

        Args:
            guideName (str): The guide name.

        Returns:
            str: The compnent Name
        """

        if guideName is None:
            return None

        if local:
            guideName = self.getLocalName(guideName)

        names = naming.get_component_and_relative_name(guideName)
        if names:
            return names[0]

    def getRelativeName(self, guideName):
        """This function return the name of the relative in the guide

            ie. "arm_C0_root" return "root"

        Args:
            guideName (str): The guide name.

        Returns:
            str: The relative Name

        """
        if guideName is None:
            return None

        localName = self.getLocalName(guideName)
        names = naming.get_component_and_relative_name(localName)
        if names:
            return names[1]

    def findRelative(self, guideName, relatives_map={}):
        """Return the objects in the rig matching the guide object.

        Args:
            guideName (str): Name of the guide object.
            relatives_map (dict, optional): Custom relative mapping to
                    point any object in a component. For example used to point
                    Auto in upvector reference.

        Returns:
            transform: The relative object

        """
        if guideName is None:
            return self.global_ctl

        if guideName in relatives_map.keys():
            return relatives_map[guideName]

        comp_name = self.getComponentName(guideName)
        relative_name = self.getRelativeName(guideName)

        if comp_name not in self.components.keys():
            return self.global_ctl
        return self.components[comp_name].getRelation(relative_name)

    def findControlRelative(self, guideName):
        """Return the control objects in the rig matching the guide object.

        Args:
            guideName (str): Name of the guide object.

        Returns:
           transform: The relative control object

        """

        if guideName is None:
            return self.global_ctl

        # localName = self.getLocalName(guideName)
        comp_name = self.getComponentName(guideName)
        relative_name = self.getRelativeName(guideName)

        if comp_name not in self.components.keys():
            return self.global_ctl
        return self.components[comp_name].getControlRelation(relative_name)

    # TODO: update findComponent and other find methods with new funtions like
    # comp_name and others.  Better composability
    def findComponent(self, guideName):
        """Return the component from a guide Name.

        Args:
            guideName (str): Name of the guide object.

        Returns:
           transform: The component

        """
        if guideName is None:
            return None

        comp_name = self.getComponentName(guideName, False)
        # comp_name = "_".join(guideName.split("_")[:2])

        if comp_name not in self.components.keys():
            return None

        return self.components[comp_name]

    def findUIHost(self, guideName):
        """Return the UI host of the compoent

        Args:
            guideName (str): Name of the guide object.

        Returns:
           transform: The relative object

        """

        if guideName is None:
            return self.ui

        comp_name = self.getComponentName(guideName, False)
        # comp_name = "_".join(guideName.split("_")[:2])

        if comp_name not in self.components.keys():
            return self.ui

        if self.components[comp_name].ui is None:
            self.components[comp_name].ui = pm.UIHost(self.components[comp_name].root)

        return self.components[comp_name].ui


from mock import Mock

import pytest

import json

from datetime import date, datetime
from src.smallquery.functions.run_query import app

@pytest.fixture()
def run_query_event():
    """
    Generates Run Query event
    """
    return {
        'query': 'select * from unit/test.parquet',
        'limit': 10,
    }

def test_results_serializer():
    a_date = date(year=2020, month=11, day=10)
    a_date_time = datetime(year=2021, month=6, day=24,
                           hour=13, minute=3, second=12, microsecond=2323)
    a_str = 'ksjdf'
    a_int = 78
    data = {
        'some_date': a_date,
        'some_date_time': a_date_time,
        'some_string': a_str,
        'some_int': a_int,
    }
    expected_json = '{"some_date": "2020-11-10", "some_date_time": "2021-06-24T13:03:12.002323", "some_string": "ksjdf", "some_int": 78}'

    actual_json = json.dumps(data, default=app.results_serializer)

    assert expected_json == actual_json

def test_handler(run_query_event):
    ensure_db_connected_mock = Mock()
    run_query_mock = Mock()
    app.ensure_db_connected = ensure_db_connected_mock
    app.run_query = run_query_mock

    app.lambda_handler(run_query_event, None)

    assert ensure_db_connected_mock.call_count == 1
    assert run_query_mock.call_count == 1


#!/usr/bin/python

class podcasts(object):
    __tablename__ = 'podcasts'

    def __init__(self):
        self.castid = 0
        self.castname = ''
        self.feedurl = ''
        self.pcenabled = 1
        self.lastupdate = 0
        self.lastattempt = 0
        self.failedattempts = 0

class episodes(object):
    __tablename__ = 'episodes'

    def __init__(self):
        self.castid = 0
        self.episodeid = 0
        self.title = ''
        self.epurl = ''
        self.enctype = ''
        self.status = ''
        self.eplength = 0
        self.epfirstattempt = 0
        self.eplastattempt = 0
        self.epfailedattempts = 0
        self.epguid = ''