from __future__ import division

import traceback
import logging
import datetime
import paho.mqtt.client as mqtt
import os
from time                  import time, sleep
import sys
import threading
from six.moves import queue
import google
from google.cloud import speech
import numpy as np
import json
import pygame
import pyaudio
from vad_utils import Audio, VADAudio
import deepspeech

WAKE_WORD_KALDI = ['Kate', 'eight kate', 'a cave', 'it came', 'ec aid', 'kate', 'in case',"cain't", 'it cave', 'eight', 'pete', 'and kate', 'a kate', 'nay tate', 'a paint', 'a cake', 'a tape', 'they take', 'kid', 'hey kid', 'it kid', 'kit', 'a tate']
WAKE_WORD_GOOGLE = ['Kate', 'kate', 'Hecate', '8/8', 'placate', 'cake', 'k', 'bait', 'hey', 'AK', 'KK', 'Escape', 'locate', 'dictate', "I can't", 'okay', 'pick 8', 'Tay k']

language_code = 'en-US'  # a BCP-47 language tag

audio_in_index = -1
mode = "N/A"
online_account = "cortic"
VoiceInit = False
startListen = False
wakeWordMode = False
robotSpeak = False
robotSpeakMsg = ""
playedNotification = False
wasListening = False
doneSpeaking = True

logging.getLogger().setLevel(logging.INFO)

def restart_program():
    python = sys.executable
    logging.info("in restart program")
    os.execl(python, python, *sys.argv)
    logging.info("Done restart program")

def connectMQTT(client):
    try:
        client.connect("127.0.0.1",1883,60)
        logging.info("Connected to broker")
        return 0
    except:
        logging.info("Broker not up yet, retrying...")
        return -1

client_stt_response = mqtt.Client()
ret = connectMQTT(client_stt_response)
while ret != 0:
    sleep(1)
    ret = connectMQTT(client_stt_response)
client_stt_response.loop_start()

def on_connect_stt_mode(client, userdata, flags, rc):
    print("STT mode client Connected with result code "+str(rc))
    client.subscribe("cait/voice_control")

def on_message_stt_mode(client, userdata, msg):
    global audio_in_index
    global mode
    global language_code
    global online_account
    global VoiceInit
    global startListen
    data = msg.payload.decode()
    logging.info("Voice Control: " + data)
    #logging.info("STT Control Init: "+data)
    if data.find("Online") != -1 or data.find("Offline") != -1:
        audio_device = json.loads(data[data.find(" ")+1:])
        audio_in_index = int(audio_device["index"])
    # if data == "Online" or data == "Offline":
        if mode == "N/A":
            logging.info("got init mode")
            if data.find("Offline") != -1:
                mode = "Offline"
            else:
                online_account = audio_device["account"]
                if audio_device["language"] == "french":
                    language_code = "fr-FR"
                elif audio_device["language"] == "chinese":
                    language_code = "zh-CN"
                mode = "Online"
        else:
            if VoiceInit:
                restart_program()
    elif data.find("VoiceDown") != -1:
        startListen = False

client_stt_mode = mqtt.Client()
client_stt_mode.on_connect = on_connect_stt_mode
client_stt_mode.on_message = on_message_stt_mode
ret = connectMQTT(client_stt_mode)
while ret != 0:
    sleep(1)
    ret = connectMQTT(client_stt_mode)
client_stt_mode.loop_start()

logging.info("Not recieve mode yet")
while mode == "N/A":
    sleep(0.2)

logging.info("Recieved mode, initializing pygame")
pygame.mixer.init()
effect = pygame.mixer.Sound('/voice_module/siri.wav')
logging.info("pygame done init")

client_stt_response.publish("cait/module_states", "STT Init Done", qos=1)
VoiceInit = True
logging.info("Recieved mode, initializing")

def on_connect_stt_control(client, userdata, flags, rc):
    print("STT control client Connected with result code "+str(rc))
    client.subscribe("cait/voice_control")

def on_message_stt_control(client, userdata, msg):
    global startListen
    global wakeWordMode
    global robotSpeak
    global robotSpeakMsg
    global playedNotification
    global wasListening
    data = msg.payload.decode()
    print("STT Control Online mode:",data)
    if data == "Reset":
        startListen = False
        wakeWordMode = False
        robotSpeak = False
        robotSpeakMsg = ""
        playedNotification = False
        wasListening = False
    elif data == "Start Listen":
        client_stt_response.publish("cait/module_states", "Init recording", qos=1)
        print("received Start Listen")
        startListen = True
        wakeWordMode = False
    elif data == "Start Listen Wakeword":
        print("received Start Listen Wakeword")
        client_stt_response.publish("cait/module_states", "Init recording", qos=1)
        startListen = True
        wakeWordMode = True

client_stt_control = mqtt.Client()
client_stt_control.on_connect = on_connect_stt_control
client_stt_control.on_message = on_message_stt_control
ret = connectMQTT(client_stt_control)
while ret != 0:
    sleep(1)
    ret = connectMQTT(client_stt_control)
client_stt_control.loop_start()

def on_connect_tts(client, userdata, flags, rc):
    print("TTS client Connected with result code "+str(rc))
    client.subscribe("cait/ttsInput")

def on_message_tts(client, userdata, msg):
    global robotSpeak
    global startListen
    global robotSpeakMsg
    global playedNotification
    global wasListening
    global doneSpeaking
    data = msg.payload.decode()
    print("ttsInput:",data)
    if startListen:
        wasListening = True
    startListen = False
    robotSpeak = True
    doneSpeaking = False
    print("Set done speaking to false")
    robotSpeakMsg = data
    playedNotification = False

client_tts = mqtt.Client()
client_tts.on_connect = on_connect_tts
client_tts.on_message = on_message_tts
ret = connectMQTT(client_tts)
while ret != 0:
    sleep(1)
    ret = connectMQTT(client_tts)
client_tts.loop_start()

client_heartbeat = mqtt.Client()
ret = connectMQTT(client_heartbeat)
while ret != 0:
    time.sleep(1)
    ret = connectMQTT(client_heartbeat)
client_heartbeat.loop_start()

class MicrophoneStream(object):
    """Opens a recording stream as a generator yielding the audio chunks."""
    def __init__(self, mic_index, rate, chunk):
        self._rate = rate
        self._chunk = chunk

        # Create a thread-safe buffer of audio data
        self._buff = queue.Queue()
        self.closed = True
        self.mic_index = mic_index

    def open_audio_interface(self):
        try:
            self._audio_stream = self._audio_interface.open(
                format=pyaudio.paInt16,
                # The API currently only supports 1-channel (mono) audio
                # https://goo.gl/z757pE
                channels=1, rate=self._rate,
                input=True, frames_per_buffer=self._chunk,
                input_device_index = self.mic_index,
                # Run the audio stream asynchronously to fill the buffer object.
                # This is necessary so that the input device's buffer doesn't
                # overflow while the calling thread makes network requests, etc.
                stream_callback=self._fill_buffer,
            )
            return True
        except:
            return False

    def __enter__(self):
        global mic_index
        self._audio_interface = pyaudio.PyAudio()
        success = self.open_audio_interface()
        retry_counter = 0
        while not success:
            retry_counter += 1
            success = self.open_audio_interface()
            if retry_counter >= 2:
                break
            sleep(0.5)
        self.closed = False

        return self

    def __exit__(self, type, value, traceback):
        self._audio_stream.stop_stream()
        self._audio_stream.close()
        self.closed = True
        # Signal the generator to terminate so that the client's
        # streaming_recognize method will not block the process termination.
        self._buff.put(None)
        self._audio_interface.terminate()

    def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
        """Continuously collect data from the audio stream, into the buffer."""
        self._buff.put(in_data)
        return None, pyaudio.paContinue

    def generator(self):
        global startListen
        global doneSpeaking
        while not self.closed:
            if not startListen or not doneSpeaking:
                break
            # Use a blocking get() to ensure there's at least one chunk of
            # data, and stop iteration if the chunk is None, indicating the
            # end of the audio stream.
            chunk = self._buff.get()
            if chunk is None:
                return
            data = [chunk]

            # Now consume whatever other data's still buffered.
            while True:
                try:
                    chunk = self._buff.get(block=False)
                    if chunk is None:
                        return
                    data.append(chunk)
                except queue.Empty:
                    break

            yield b''.join(data)

def listen_print_loop(responses):
    """Iterates through server responses and prints them.

    The responses passed is a generator that will block until a response
    is provided by the server.

    Each response may contain multiple results, and each result may contain
    multiple alternatives; for details, see https://goo.gl/tjCPAU.  Here we
    print only the transcription for the top alternative of the top result.

    In this case, responses are provided for interim results as well. If the
    response is an interim one, print a line feed at the end of it, to allow
    the next result to overwrite it, until the response is a final one. For the
    final one, print a newline to preserve the finalized transcription.
    """
    num_chars_printed = 0
    global startListen
    global playedNotification
    global wakeWordMode
    global doneSpeaking
    if responses is None:
        return
    for response in responses:
        if not response.results:
            continue

        # The `results` list is consecutive. For streaming, we only care about
        # the first result being considered, since once it's `is_final`, it
        # moves on to considering the next utterance.
        result = response.results[0]
        if not result.alternatives:
            continue

        # Display the transcription of the top alternative.
        transcript = result.alternatives[0].transcript

        # Display interim results, but with a carriage return at the end of the
        # line, so subsequent lines will overwrite them.
        #
        # If the previous result was longer than this one, we need to print
        # some extra spaces to overwrite the previous result
        overwrite_chars = ' ' * (num_chars_printed - len(transcript))

        if not startListen or not doneSpeaking:
            break
        if not result.is_final:
            logging.info("In print loop for not final")
            sys.stdout.write(transcript + overwrite_chars + '\r')
            sys.stdout.flush()
            logging.info("In print loop for not final, flushed")
            num_chars_printed = len(transcript)

        else:
            sentence = transcript + overwrite_chars
            logging.info("******************** "+sentence)
            logging.info("In print loop for final")
            if not wakeWordMode:
                client_stt_response.publish("cait/sttData", sentence, qos=0)
                logging.info("Publish stt data")
                startListen = False
                playedNotification = False
                logging.info("Breaking")
                break
            else:
                sentence_list = sentence.split()
                if ' '.join(sentence_list[-2:]) in WAKE_WORD_GOOGLE or sentence_list[-1] in WAKE_WORD_GOOGLE:
                    client_stt_response.publish("cait/module_states", "WAKEWORD", qos=0)
                    #print("Published:", sentence)
                    startListen = False
                    break

def heartbeat_func():
    global client_heartbeat
    while True:
        client_heartbeat.publish("cait/module_states", "Voice Up", qos=1)
        sleep(1)

def main():
    global audio_in_index
    global robotSpeak
    global startListen
    global robotSpeakMsg
    global playedNotification
    global wasListening
    global doneSpeaking
    global online_account
    global mode
    global language_code

    DEFAULT_SAMPLE_RATE = 16000
    MODEL_FILE = "/opt/deepspeech-models/deepspeech-0.9.1-models.tflite"
    SCORE_FILE = "/opt/deepspeech-models/deepspeech-0.9.1-models.scorer"
    MODEL_FILE_CN = "/opt/deepspeech-models/deepspeech-0.9.1-models-zh-CN.tflite"
    SCORE_FILE_CN = "/opt/deepspeech-models/deepspeech-0.9.1-models-zh-CN.scorerr"

    json_file = "/voice_module/" + online_account
    # Audio recording parameters
    RATE = 16000
    CHUNK = int(RATE / 10)  # 100ms

    if mode == "Online":
        logging.warning("Using cloud account: " + json_file)
        client = speech.SpeechClient.from_service_account_json(json_file)
        config = speech.RecognitionConfig(
            encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
            sample_rate_hertz=RATE,
            language_code=language_code)
        streaming_config = speech.StreamingRecognitionConfig(
            config=config,
            interim_results=True)
    else:
        client = deepspeech.Model(MODEL_FILE)
        client.enableExternalScorer(SCORE_FILE)

    heartbeat_thread = threading.Thread(target=heartbeat_func, daemon=True)
    heartbeat_thread.start()
    while True:
        if startListen and doneSpeaking:
            print("Start listening")
            if mode == "Online":
                with MicrophoneStream(audio_in_index, RATE, CHUNK) as stream:
                    if not playedNotification and not wakeWordMode:
                        try:
                            effect.play()
                        except:
                            effect.play()
                        playedNotification = True
                    client_stt_response.publish("cait/module_states", "Init recording done", qos=1)
                    audio_generator = stream.generator()
                    requests = (speech.StreamingRecognizeRequest(audio_content=content)
                                for content in audio_generator)
                    try:
                        responses = client.streaming_recognize(streaming_config, requests)
                    except google.api_core.exceptions.PermissionDenied:
                        logging.warning("Google account error.")
                        responses = None
                        client_stt_response.publish("cait/module_states", "Voice Exception: Google cloud client denied, check your account to make sure it is working", qos=1)
                        startListen = False
                        playedNotification = False
                    # Now, put the transcription responses to use.
                    try:
                        listen_print_loop(responses)
                    except Exception as e:
                        print("Excption handle : Exceeded maximum allowed stream duration, re-listening")
            else:
                if not playedNotification and not wakeWordMode:
                    try:
                        effect.play()
                    except:
                        effect.play()
                    playedNotification = True
                #sleep(1)
                while pygame.mixer.get_busy() == True:
                    #logging.info("Playing du du")
                    continue
                vad_audio = VADAudio(audio_in_index, aggressiveness=2,
                            input_rate=DEFAULT_SAMPLE_RATE)
                logging.info("Created VAD")
                client_stt_response.publish("cait/module_states", "Init recording done", qos=1)
                frames = vad_audio.vad_collector()
                stream_context = client.createStream()
                wav_data = bytearray()
                for frame in frames:
                    if frame is not None:
                        logging.info("Streaming frame")
                        stream_context.feedAudioContent(np.frombuffer(frame, np.int16))
                    else:
                        logging.info("end utterence")
                        sentence = stream_context.finishStream()
                        logging.info("User said: " + sentence)
                        if sentence == "":
                            stream_context = client.createStream()
                            continue
                        if not wakeWordMode:
                            client_stt_response.publish("cait/sttData", sentence, qos=2)
                            startListen = False
                            playedNotification = False
                            break
                        else:
                            sentence_list = sentence.split()
                            if ' '.join(sentence_list[-2:]) in WAKE_WORD_GOOGLE or sentence_list[-1] in WAKE_WORD_GOOGLE:
                                client_stt_response.publish("cait/module_states", "WAKEWORD", qos=2)
                                #print("Published:", sentence)
                                startListen = False
                                break
                if not startListen:
                    logging.info("stop listen")
                    vad_audio.destroy()
                    logging.info("Destroyed VAD")
        else:
            if robotSpeak:
                client_stt_control.publish("cait/module_states", "Start Speaking", qos=1)
                if mode == "Online":
                    if language_code == "fr-FR":
                        os.system('/voice_module/speech_fr.sh "' + robotSpeakMsg + '"')
                    elif language_code == "zh-CN":
                        os.system('/voice_module/speech_chs.sh "' + robotSpeakMsg + '"')
                    else:
                        os.system('/voice_module/speech_en.sh "' + robotSpeakMsg + '"')
                else:
                    os.system('mimic -t "' + robotSpeakMsg + '" -voice slt')
                robotSpeak = False
                if wasListening:
                    startListen = True
                    wasListening = False
                doneSpeaking = True
                print("Set done speaking to true")
                client_stt_control.publish("cait/module_states", "Done Speaking", qos=1)
            sleep(0.03)

if __name__ == '__main__':
    main()


import os
import uuid

from ....core import OBJECT_TYPE
from ....core.session import register_session_cls
from ....core.session import AbstractSession, SyncSession, _loop
from ....oscar.backends.router import Router
from ....tests.core import _check_args, ObjectCheckMixin
from ..session import Session

CONFIG_FILE = os.path.join(
    os.path.dirname(__file__), 'check_enabled_config.yml')

@register_session_cls
class CheckedSession(ObjectCheckMixin, Session):
    name = 'test'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._tileable_checked = dict()

        check_options = dict()
        for key in _check_args:
            check_options[key] = kwargs.get(key, True)
        self._check_options = check_options

    @classmethod
    async def init(cls,
                   address: str,
                   session_id: str,
                   **kwargs) -> "Session":
        init_local = kwargs.get('init_local', False)
        if init_local:
            if 'n_cpu' not in kwargs:
                # limit to 2 cpu each worker
                kwargs['n_cpu'] = 2 * kwargs.get('n_worker', 1)
            if 'config' not in kwargs:
                # enable check for task and subtask processor
                kwargs['config'] = CONFIG_FILE
        session = await super().init(address, session_id, **kwargs)
        # reset global router
        Router.set_instance(Router(list(), None))
        return session

    @staticmethod
    def _extract_check_options(kwargs):
        check_options = dict()
        for key in _check_args:
            check_options[key] = kwargs.pop(key, True)
        return check_options

    async def fetch(self, *tileables, **kwargs):
        check_options = self._extract_check_options(kwargs)

        results = await super().fetch(*tileables)

        if check_options.get('check_all', True):
            for tileable, result in zip(tileables, results):
                if not isinstance(tileable, OBJECT_TYPE) and \
                        tileable.key not in self._tileable_checked:
                    self.assert_object_consistent(tileable, result)

        return results

async def _new_test_session(address: str,
                            session_id: str = None,
                            default: bool = False,
                            **kwargs) -> AbstractSession:
    if session_id is None:
        session_id = str(uuid.uuid4())

    session = await CheckedSession.init(
        address, session_id=session_id, **kwargs)
    if default:
        session.as_default()
    return session

def new_test_session(address: str = None,
                     session_id: str = None,
                     backend: str = 'test',
                     default: bool = False,
                     **kwargs):
    if address is None:
        address = '127.0.0.1'
        if 'init_local' not in kwargs:
            kwargs['init_local'] = True
    session = _loop.run_until_complete(
        _new_test_session(address, session_id=session_id,
                          backend=backend, default=default, **kwargs))
    return SyncSession(session)


import serial
import sys

if (len(sys.argv) < 2) and ("tty" not in sys.argv[1].lower()):
    print("Path for serial device needed")
    print("Usage: {0} <command>".format(sys.argv[0]))
    exit(1)

serialport = serial.Serial(sys.argv[1], 9600, timeout=0.5)

while True:
    line = serialport.readline()
    if len(line) > 0:
        line = line.rstrip()
        success = 0
        while success == 0 :

            try:
                decoded = line.decode()
                success = 1
            except:
                line = line[1:-1]

        printit = True
        for e in excluded:
            if e in decoded:
                printit = False
        if printit is True:
            print(decoded)


#!/usr/bin/env python3
# Try to locate the loop segments in the given audio file.
#
# Since gbsplay does not expose what jumps are done when and where, we
# must try to infer it from the audio pattern.

import sys
import numpy as np
from scipy.signal import find_peaks
import soundfile as sf
import matplotlib.pyplot as plt

data, samplerate = sf.read(sys.argv[1], always_2d=True)
step = 85  # Only consider every step:th sample to speed up calculation
a = data[::step, 1].copy()

def autocorr(a):
    crossCorr = np.correlate(a, a, mode="same")
    return crossCorr[len(crossCorr) // 2 :]

# Find the duration of the loop segments
corr = autocorr(a)
b = np.maximum(0, corr) / np.max(corr[10000 // step :])  # Normalize to [0, 1]
peaks, _ = find_peaks(b, distance=5 * samplerate / step, prominence=0.4)
# peak_idx = 100 + np.argmax(corr[100:])
peak_idx = peaks[1]
loopDuration = peak_idx * step / samplerate
print(f"loopDuration: {loopDuration}")

# Next, find the duration of the intro
cost = np.fromiter(
    (
        -np.linalg.norm(a[i : i + peak_idx] - a[i + peak_idx : i + 2 * peak_idx])
        for i in range(len(a) - 2 * peak_idx)
    ),
    dtype=float,
)
# Could also try just argmax instead of find_peaks!
reach = np.max(cost) - np.min(cost)
peaks, _ = find_peaks(cost, distance=peak_idx / 2, prominence=reach / 3)
introDuration = peaks[0] * step / samplerate
# introDuration = np.argmax(cost) * step / samplerate
print(f"introDuration: {introDuration}")

plt.plot(cost)
plt.plot(peaks, cost[peaks], "x")
plt.show()


"""Contact manager command functions."""

from text import display_contact_error, display_contact_info, display_added, display_deleted, NUMBER_PROMPT, NAME_PROMPT, EMAIL_PROMPT, PHONE_PROMPT

def list_contacts(contacts: list[list[str]]) -> None:
    """List the contacts in the contact list."""
    for index, contact in enumerate(contacts):
        print(f'{index + 1}. {contact[0]}')

def view_contact(contacts: list[list[str]]) -> None:
    """View the contact information of a contact."""
    contact_number: int = int(input(NUMBER_PROMPT))
    if contact_number > len(contacts) or contact_number < 1:
        display_contact_error(contact_number)
    else:
        contact = contacts[contact_number - 1]
        display_contact_info(contact)

def add_contact(contacts: list[list[str]]) -> None:
    """Add a contact to the contact list."""
    name: str = input(NAME_PROMPT)
    email: str = input(EMAIL_PROMPT)
    phone: str = input(PHONE_PROMPT)
    contact: list[str] = [name, email, phone]
    contacts.append(contact)
    display_added(contact)

def delete_contact(contacts: list[list[str]]) -> None:
    """Delete a contact from the contact list."""
    contact_number: int = int(input(NUMBER_PROMPT))
    if contact_number > len(contacts) or contact_number < 1:
        display_contact_error(contact_number)
    else:
        contact: list[str] = contacts.pop(contact_number-1)
        display_deleted(contact)


from tkinter import*
arquivo = open('Alunos.txt', 'r')
matriz = []
for linha in arquivo.readlines():
    linhas = linha.split(";")
    matriz.append(linhas)
for i in range(len(matriz)):
    matriz[i][-1] = matriz[i][-1].strip("\n")

listadamedia = []
for i in range(len(matriz)):
    media = (float(matriz[i][2]) + float(matriz[i][3]) + float(matriz[i][4]) + float(matriz[i][5])) / 4
    listadamedia.append("%.2f" % media)

listanomes = []
for k in range(len(matriz)):
    nomes = matriz[k][0]
    listanomes.append(nomes)

listafaltas = []
for w in range(len(matriz)):
    listafaltas.append(matriz[w][-1])

def panorama(): #1) Panorama geral: nome, matricula, situação
    string=''
    for i in range(len(matriz)):
        if int(matriz[i][-1])>=10:
            string+="Nome: "+matriz[i][0]+" | "+"Matrícula: "+matriz[i][1]+" | "+"Situação: "+"reprovado(a) por falta"+"\n"
        elif float(listadamedia[i])>7:
            string+="Nome: "+matriz[i][0]+" | "+"Matrícula: "+matriz[i][1]+" | "+"Situação: "+"aprovado(a)"+"\n"
        elif float(listadamedia[i])<7:
            string+="Nome: "+matriz[i][0]+" | "+"Matrícula: "+matriz[i][1]+" | "+"Situação: "+"reprovado(a)"+"\n"
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black',font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def alunos(): #2) Alunos cadastrados na disciplina(nome e matricula)
    l = ''
    for linha in matriz:
        l += 'Nome: '+linha[0] + ' | ' + "Matrícula: "+linha[1] + '\n'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=l,fg='white',bg='black',font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def reprovado(): #3) Reprovados: Listar o nome e a media dos alunos reprovados e porcentagem
    reprovado = []
    nomereprovado = []
    string=''
    for w in range(len(listadamedia)):
        if float(listadamedia[w])<7.00:
            reprovado.append(listadamedia[w])
            nomereprovado.append(listanomes[w])
    for p in range(len(reprovado)):
        string+='Nome: '+nomereprovado[p]+' | '+'Média: '+reprovado[p]+ '\n'
    porcentagem=(len(reprovado)*100)/len(listanomes)
    string+= 'Representa(m) '+"%.3f" % porcentagem + "%"+' de todos os alunos.'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def repfalt(): #4) Listar alunos que reprovaram por falta e sua porcentagem
    string=''
    cont=0
    for i in range(len(matriz)):
        if int(matriz[i][-1])>=10:
            cont+=1
            string+="Nome: "+matriz[i][0]+" | "+"Matrícula: "+matriz[i][1]+"\n"
    porcentagem=(cont*100)/len(listanomes)
    string+='Representa(m) '+"%.3f" % porcentagem + "%"+' de todos os alunos.'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def aprovados(): #5) Aprovados: Listar o nome e a média dos alunos aprovados e porcentagem
    aprovado = []
    nomeaprovado = []
    string = ''
    for w in range(len(listadamedia)):
        if float(listadamedia[w]) > 7.00:
            aprovado.append(listadamedia[w])
            nomeaprovado.append(listanomes[w])
    for p in range(len(aprovado)):
        string+='Nome: '+nomeaprovado[p]+' | '+'Média: '+aprovado[p]+ '\n'
    porcentagem = (len(aprovado) * 100) / len(listanomes)
    string+= 'Representa(m) '+"%.3f" % porcentagem + "%"+' de todos os alunos.'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def porcavacont():  #6 alunos aprovados por avaliação contínua e sua porcentagem
    string = ''
    quant = 0
    cont = 0
    for i in range(len(matriz)):
        quant += 1
        if (float(matriz[i][2]) + float(matriz[i][3]) + float(matriz[i][4])) / 4 >= 7:
            cont += 1
            string+="Nome: "+matriz[i][0]+" | "+"Matrícula: "+matriz[i][1]+"\n"
    porcentagem = (100*cont) / quant
    string+='Representa(m) '+"%.3f" % porcentagem + "%"+' de todos os alunos.'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def digitar(): #7) Local pra digitar a média mínima e máxima seguido dos alunos e suas médias que estejam nesse parâmetro e sua porcentagem
    minimo=float(push1.get())
    maximo=float(push2.get())
    parametromedia=[]
    nomeparametro=[]
    string = ''
    for w in range(len(listadamedia)):
        if float(listadamedia[w]) <= maximo and float(listadamedia[w]) >= minimo:
            parametromedia.append(listadamedia[w])
            nomeparametro.append(listanomes[w])
    for p in range(len(parametromedia)):
        string += 'Nome: '+ nomeparametro[p]+' | '+'Média: ' + parametromedia[p]+'\n'
    porcentagem = (len(parametromedia) * 100) / len(listadamedia)
    string += 'Representa(m) '+"%.3f" % porcentagem + "%"+' de todos os alunos.'
    title3['text'] = string

def notasentre():  #8 listar alunos que obtiveram nota entre x e y, sua nota e porcentagem
    cont, quant, aluno = 0, 0, ''
    nota1 = float(gof1.get())
    nota2 = float(gof2.get())
    x = int(gof3.get())
    for linha in matriz:
        quant += 1
        if nota1 <= float(linha[x+1]) <= nota2:
            cont += 1
            aluno += 'Aluno: '+ linha[0] + ' | ' + 'Nota: '+ str(round(float(linha[x+1]),1)) + '\n'
    porc = 'Representa(m) '+str(round((100 * cont) / quant, 3))+'%'+' de todos os alunos.'
    if porc == '0.0%':
        gow6['text']=('Não há alunos nessa faixa de notas na prova '+str(x)+'.')
    else:
        gow6['text']=(aluno + porc)

def search(): #9) Local para pesquisar os dados do aluno a partir da sua matrícula
    matricula = str(can1.get())
    dados = []
    for linha in matriz:
        if linha[1] == matricula:
            dados += linha
    lista = ['Nome: ', 'Matricula: ', 'Nota 1: ','Nota 2: ','Nota 3: ', 'Nota 4: ', 'Faltas: ']
    types = ''
    for each in range(len(dados)):
        types += lista[each]+dados[each]+'\n'
    letter['text'] = types

def mediaperiodo(): #10) Listar a media das notas no periodo
    soma=0
    string=''
    for i in range(len(listadamedia)):
        soma+=float(listadamedia[i])
    media=soma/(len(listadamedia))
    string+= "%.2f"%media
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def mediaprova(): #11) Listar a média das notas por prova
    string=''
    quant = 0
    um = 0
    dois = 0
    tres = 0
    quatro = 0
    prova = int(endness.get())
    if prova >= 1 and prova <= 4 :
        for linha in matriz:
            quant += 1
            if prova == 1:
                um += float(linha[2])
            if prova == 2:
                dois += float(linha[3])
            if prova == 3:
                tres += float(linha[4])
            if prova == 4:
                quatro += float(linha[5])
        m1 = round(int(um)/quant, 1)
        m2 = round(int(dois)/quant, 1)
        m3 = round(int(tres)/quant, 1)
        m4 = round(int(quatro)/quant, 1)
        if prova == 1:
            string='Média da primeira prova: ' + str(m1)
        elif prova == 2:
            string='Média da segunda prova: ' + str(m2)
        elif prova == 3:
            string='Média da terceira prova: ' + str(m3)
        elif prova == 4:
            string='Média da quarta prova: ' + str(m4)

        endless['text'] = string
        endless['fg'] = 'white'
        endless['bg'] = 'black'
    else:
        endless['text'] = 'Escolha um número de 1 a 4!'
        endless['fg'] = 'red'
        endless['bg'] = 'black'

def reprovadofalta():  #12) Listar os alunos que passaram por media e reprovaram por falta e sua porcentagem
    string=''
    nomes = []
    medias = []
    faltas = []
    for w in range(len(listadamedia)):
        if float(listadamedia[w]) > 7.00:
            if int(listafaltas[w]) >= 10:
                nomes.append(listanomes[w])
                medias.append(listadamedia[w])
                faltas.append(listafaltas[w])
    for i in range(len(nomes)):
        string+='Nome: '+nomes[i]+' | '+'Média: '+medias[i]+' | '+'Faltas: '+faltas[i]+'\n'
    porcentagem = (len(nomes) * 100) / len(listanomes)
    string+='Representa(m) '+"%.3f" % porcentagem + "%"+' de todos os alunos.'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def withoutfalt(): #13) Listar alunos aprovados sem faltas e sua porcentagem
    string=''
    aluno = ''
    cont, quant = 0, 0
    for linha in matriz:
        quant += 1
        if ((float(linha[2]) + float(linha[3]) + float(linha[4]) + float(linha[5])) / 4) >= 7 and int(linha[-1]) == 0:
            aluno += linha[0] + '\n'
            cont += 1
    string += aluno
    porc = str(round((cont*100) / quant, 3))+'%'
    string += 'Representa(m) '+porc+' de todos os alunos.'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def brusco(): #14) Listar o nome dos alunos que tiveram bruscas diferencas de notas(6 pontos) e sua porcentagem
    string=''
    nome = []
    notas = []
    posicao = []
    for i in range(len(matriz)):
        if abs(float(matriz[i][2]) - float(matriz[i][3])) >= 6:
            nome.append(matriz[i][0])
            notas.append(matriz[i][2])
            notas.append(matriz[i][3])
            posicao.append(1)
            posicao.append(2)
        elif abs(float(matriz[i][2]) - float(matriz[i][4])) >= 6:
            nome.append(matriz[i][0])
            notas.append(matriz[i][2])
            notas.append(matriz[i][4])
            posicao.append(1)
            posicao.append(3)
        elif abs(float(matriz[i][2]) - float(matriz[i][5])) >= 6:
            nome.append(matriz[i][0])
            notas.append(matriz[i][2])
            notas.append(matriz[i][5])
            posicao.append(1)
            posicao.append(4)
        elif abs(float(matriz[i][3]) - float(matriz[i][4])) >= 6:
            nome.append(matriz[i][0])
            notas.append(matriz[i][3])
            notas.append(matriz[i][4])
            posicao.append(2)
            posicao.append(3)
        elif abs(float(matriz[i][3]) - float(matriz[i][5])) >= 6:
            nome.append(matriz[i][0])
            notas.append(matriz[i][3])
            notas.append(matriz[i][5])
            posicao.append(2)
            posicao.append(4)
        elif abs(float(matriz[i][4]) - float(matriz[i][5])) >= 6:
            nome.append(matriz[i][0])
            notas.append(matriz[i][4])
            notas.append(matriz[i][5])
            posicao.append(3)
            posicao.append(4)
    for w in range(len(nome)):
        string+="O aluno(a) "+ str(nome[w])+" obteve bruscas diferenças de notas durante as avaliações "+str(posicao[w])+" e "+str(posicao[w+1])+" com as respectivas notas: "+str(notas[w])+" e "+str(notas[w+1])+'\n'
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text=string, fg='white', bg='black', font='25')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

# =============================================================================== #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~ INTERFACE GRAFICA ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# =============================================================================== #

def wind():
    global we, can1, sho1, letter
    we = Tk()
    we.title('wind')
    we['bg'] = 'black'
    can1 = Entry(we)

    coj = Label(we, text='Digite a matrícula do aluno: ',bg ='black',fg='white')
    coj.pack()
    can1.pack()
    sho1 = Button(we, text='Enviar', command=search)
    sho1.pack()
    letter = Label(we, text='',bg ='black',fg='white')
    letter.pack()
    we.geometry('800x600')
    we.mainloop()

def earth():
    global ea, gof1, gof2, gof3, gow6, gow5
    ea = Tk()
    gow1 = Label(ea, text='Nota x: ', font=('Arial','10'))
    gow1.place(x=150, y=100)
    gow2 = Label(ea, text='Nota y: ', font=('Arial','10'))
    gow2.place(x=150, y=150)
    gow3 = Label(ea, text='Prova z: ', font=('Arial','10'))
    gow3.place(x=150, y=200)
    gow4 = Label(ea, text='Avaliar alunos'+' que obtiveram nota'+'\n'+'entre x e y',fg='white',bg='black',font=('Arial','20','bold' ))
    gow4.grid(row=0, column=3)
    gow5 = Label(ea, text='', bg='black', fg='white')
    gow5.grid(row=1, column=4)
    gow6 = Label(ea, text='', bg='black', fg='white')
    gow6.grid(row=56, column=56)
    gof1 = Entry(ea, width=7)
    gof1.place(x=230, y=100)
    gof2 = Entry(ea, width=7)
    gof2.place(x=230, y=150)
    gof3 = Entry(ea, width=7)
    gof3.place(x=230, y=200)
    god1 = Button(ea, text='Enviar', command=notasentre)
    god1.place(x=230, y=300)
    ea['bg'] = 'black'
    ea.title('NotasEntrex&y')
    ea.geometry('800x600')
    ea.mainloop()

def pesquisa():
    global frase1
    inst = Tk()
    botao1 = Button(inst, text='Consultar média mínima e máxima no período',command=max, fg='white',bg='black',font='25')
    botao2 = Button(inst, text='Consultar nota mínima e nota máxima em uma das 4 provas',command=earth, fg='white',bg='black',font='25')
    botao3 = Button(inst, text='Consultar os dados do aluno pela matrícula',command=wind, fg='white',bg='black',font='25')
    frase1 = Label(inst,text='')
    botao1.pack(side=TOP, fill = BOTH, expand = 1)
    botao2.pack(side=TOP, fill = BOTH, expand = 1)
    botao3.pack(side=TOP, fill = BOTH, expand = 1)
    inst.geometry('800x600')
    inst.mainloop()

def especifico():
    global frase
    oi = Tk()
    botao1 = Button(oi, text='Alunos Aprovados por média',command=aprovados, fg='white',bg='black',font='25')  #5)
    botao2 = Button(oi, text='Alunos Aprovados por avaliação contínua',command=porcavacont, fg='white',bg='black',font='25')  #6)
    botao3 = Button(oi, text='Alunos Reprovados por média',command=reprovado, fg='white',bg='black',font='25')  #3)
    botao4 = Button(oi, text='Alunos Reprovados por falta',command=repfalt, fg='white',bg='black',font='25')  #4)
    frase = Label(oi, text='')
    botao1.pack(side=TOP, fill = BOTH, expand = 1)
    botao2.pack(side=TOP, fill = BOTH, expand = 1)
    botao3.pack(side=TOP, fill = BOTH, expand = 1)
    botao4.pack(side=TOP, fill = BOTH, expand = 1)
    oi.geometry('800x600')
    oi.mainloop()

def apurado():
    global frase
    iai = Tk()
    botao1 = Button(iai,text='Média das notas no período',command=mediaperiodo,fg='white',bg='black',font='25')  # 10)
    botao2 = Button(iai,text='Média das notas por prova',command=fire,fg='white',bg='black',font='25')  # 11)
    botao3 = Button(iai,text='Alunos Aprovados dos média e reprovados por falta',command=reprovadofalta,fg='white',bg='black',font='25')  # 12)
    botao4 = Button(iai,text='Alunos Aprovados sem faltas',command=withoutfalt,fg='white',bg='black',font='25')  # 13)
    botao5 = Button(iai,text='Alunos com bruscas diferenças de notas(6 pontos)',command=brusco,fg='white',bg='black',font='25')  # 14)
    frase = Label(iai, text='')
    botao1.pack(side=TOP, fill = BOTH, expand = 1)
    botao2.pack(side=TOP, fill = BOTH, expand = 1)
    botao3.pack(side=TOP, fill = BOTH, expand = 1)
    botao4.pack(side=TOP, fill = BOTH, expand = 1)
    botao5.pack(side=TOP, fill = BOTH, expand = 1)
    iai.geometry('800x600')
    iai.mainloop()
def fire():
    global endless, endness
    an = Tk()
    an['bg'] = 'black'
    end = Label(an, text='Insira qual prova você deseja obter a média', fg='white', bg='black', font='25')
    bolton = Button(an, text='Enviar', command=mediaprova)
    endness = Entry(an)
    endless = Label(an, text='', bg='black')
    an.geometry('800x600')
    end.pack()
    endness.pack()
    bolton.pack()
    endless.pack()
    an.mainloop()

def max():
    global push1, push2, title3
    mip = Tk()
    title1 = Label(mip, text='Insira a media mínima a ser pesquisada:',bg ='black',fg='white')
    title2 = Label(mip, text='Insira a media máxima a ser pesquisada:',bg ='black',fg='white')
    title3 = Label(mip, text='',bg='black',fg='white')
    push1 = Entry(mip)
    push2 = Entry(mip)
    mip['bg'] = 'black'
    putton1 = Button(mip, text='Enviar', command=digitar)
    title1.grid(row=0, column=0)
    title2.grid(row=1, column=0)
    push1.grid(row=0, column=1)
    push2.grid(row=1, column=1)
    putton1.grid(row=2, column=1)
    title3.place(x=380, y=10)
    mip.geometry('800x600')
    mip.mainloop()

def any():
    global end
    an = Tk()
    end = Label(an, text='')
    an.geometry('800x600')
    end.pack()
    an.mainloop()

def janela():
    global ent2
    f = Tk()
    f.title('janela')
    ent1 = Label(f, text='Clique em qual tarefa'+'\n'+'deseja realizar:',fg='white',bg='black', font=('Fixedsys','18'))
    ent1.pack()
    put2 = Button(f, text='Panorama geral', fg='white',bg='black',command=panorama,font=('25'))
    put2.pack(side=TOP, fill=BOTH, expand=1)
    put3 = Button(f, text='Alunos cadastrados', fg='white',bg='black',command=alunos,font=('25'))
    put3.pack(side=TOP, fill=BOTH, expand=1)
    put1 = Button(f, text='Dados específicos', fg='white',bg='black',command=especifico,font=('25'))
    put1.pack(side=TOP, fill=BOTH, expand=1)
    put4 = Button(f, text='Local para pesquisa', fg='white',bg='black', command=pesquisa,font=('25'))
    put4.pack(side=TOP, fill=BOTH, expand=1)
    put5 = Button(f, text='Apurado do período', fg='white',bg='black',command=apurado,font=('25'))
    put5.pack(side=TOP, fill=BOTH, expand=1)
    f.geometry('800x600')
    f['bg']='black'
    f.mainloop()

i = Tk()
i.title('Avaliação dados alunos')
i['bg'] = 'black'
texto1 = Label(i, text='AVALIAÇÃO ESTATÍSTICA DE ALUNOS',fg='white', bg = 'black',font=('System','30','bold'))
texto1.place(x=35, y=120)
input1 = Entry(i, width=5)
input2 = Entry(i, width=5)
input3 = Entry(i, width=5)
botao1 = Button(i, text='CLIQUE PARA ENTRAR',font=('Verdana','10','bold'),fg='white',bg='black', width = 22, height=10, command=janela)
botao1.place(x=300,y=250)
i.geometry('800x600')
i.mainloop()


import strings
from book import (GiveMode, Book)
from telegram import ReplyKeyboardMarkup
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
                          ConversationHandler)
from book_db import BookRepository

NAME, AUTHOR, DESCRIPTION, LABELS, GIVEAWAY = range(5)

GIVE_MODES = {
        "Дать на время":GiveMode.RENT,
        "Обменять":GiveMode.SWAP,
        "Подарить":GiveMode.GIVEAWAY
}

class AddCommandHandler(object):
    def __init__(self, book_repo):
       self._book = None
       self._book_repo = book_repo

    def start(self, bot, update):
        update.message.reply_text(strings.ADD_BOOK_NAME_TEXT)
        return NAME

    def name(self, bot, update):
        self._book = Book(update.message.text.title(), update.message.from_user)
        update.message.reply_text(strings.ADD_BOOK_AUTHOR_TEXT)
        return AUTHOR

    def author(self, bot, update):
        self._book.author = update.message.text.title()
        update.message.reply_text(strings.ADD_BOOK_DESCRIPTION_TEXT)
        return DESCRIPTION

    def description(self, bot, update):
        self._book.description = update.message.text
        update.message.reply_text(strings.ADD_BOOK_LABELS_TEXT)
        return LABELS

    def labels(self, bot, update):
        self._book.labels = update.message.text.split(' ')
        reply_keyboard = [['Дать на время', 'Обменять', 'Подарить']]
        update.message.reply_text(
        strings.ADD_BOOK_GIVEAWAY_TEXT,
        reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
        return GIVEAWAY

    def giveaway(self, bot, update):
        self._book.giveaway = GIVE_MODES[update.message.text]
        update.message.reply_text("Tnx")
        self._book_repo.add(self._book)
        return ConversationHandler.END

    def cancel(self, bot, update):
        user = update.message.from_user
        update.message.reply_text(strings.ADD_BOOK_CANCEL_TEXT,
                              reply_markup=ReplyKeyboardRemove())

        return ConversationHandler.END

    def handler(self):
        return ConversationHandler(
        entry_points=[CommandHandler('addbook', self.start)],

        states={
            NAME: [MessageHandler(Filters.text, self.name)],

            AUTHOR: [MessageHandler(Filters.text, self.author)],

            DESCRIPTION: [MessageHandler(Filters.text, self.description)],

            LABELS: [MessageHandler(Filters.text, self.labels)],

            GIVEAWAY: [MessageHandler(Filters.text, self.giveaway)],
        },

        fallbacks=[CommandHandler('cancel', self.cancel)]
        )


from .Materials import Hickory

HickoryLayer = {
"Material"	: Hickory,
"Length"	: [ 0.0,  0.142857,  0.2,  1.0],	#frac
"Height"	: [38.1,      38.1, 12.7, 12.7]	#mm
}

print("don't forget to use layers")

Layers = []

Layers.append(HickoryLayer)


# ---
# jupyter:
#   jupytext:
#     formats: ipynb,py
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.3.3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pickle
import random
import os
import torch
from tqdm import tqdm

import numpy as np
import torch.functional as F

from torch import nn
from torch.utils.data import Dataset, DataLoader
from torchvision import models

from hw_grapheme.dl_utils.radam import RAdam

# +
sz = 128
bs = 64
nfolds = 4 #keep the same split as the initial dataset
fold = 0
SEED = 2019
TRAIN = '../input/grapheme-imgs-128x128/'
LABELS = '../input/bengaliai-cv19/train.csv'

def seed_everything(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = True

seed_everything(SEED)

# -

class Loss_combine(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input, target,reduction='mean'):
        x1,x2,x3 = input
        x1,x2,x3 = x1.float(),x2.float(),x3.float()
        y = target.long()
        return 0.7*F.cross_entropy(x1,y[:,0],reduction=reduction) + 0.1*F.cross_entropy(x2,y[:,1],reduction=reduction) + \
          0.2*F.cross_entropy(x3,y[:,2],reduction=reduction)

class GraphemeDataset(Dataset):
    def __init__(self, data_list, _type='train'):
        self.data_list = data_list

    def __len__(self):
        return len(self.data_list)

    def __getitem__(self, idx):
        data = self.data_list[idx]
        # name = data["name"]
        image = data["image"]
        grapheme_root = data["grapheme_root"]
        vowel_diacritic = data["vowel_diacritic"]
        consonant_diacritic = data["consonant_diacritic"]
        return image, vowel_diacritic, grapheme_root, consonant_diacritic

class ResNet50(nn.Module):
    def __init__(self):
        super(ResNet50,self).__init__()

        arch = models.resnet50(num_classes=1000, pretrained=True)
        arch = list(arch.children())
        w = arch[0].weight
        arch[0] = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=2, bias=False)
        arch[0].weight = nn.Parameter(torch.mean(w, dim=1, keepdim=True))
        self.backbone = nn.Sequential(*arch[:-1])

        # vowel_diacritic
        self.fc1 = nn.Linear(2048,11)
        # grapheme_root
        self.fc2 = nn.Linear(2048,168)
        # consonant_diacritic
        self.fc3 = nn.Linear(2048,7)

    def forward(self, x):
        x = self.backbone(x)
        x = x.view(-1, 2048)
        # print("x: ", x.shape)
        x1 = self.fc1(x)
        x2 = self.fc2(x)
        x3 = self.fc3(x)

        return x1,x2,x3

# +
batch_size = 32

with open("../data/processed_data/train_data_0.pickle", "rb") as f:
    data_list = pickle.load(f)

train_dataset = GraphemeDataset(data_list)
train_loader = torch.utils.data.DataLoader(train_dataset,batch_size=batch_size,shuffle=True)
# -

len(train_dataset)

iter(train_loader).next()

# +
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

model = ResNet50()
model = nn.DataParallel(model)
model.to(device);

# -

optimizer = RAdam(model.parameters(), lr=1e-3)
#scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=1e-4, max_lr=0.05)
criterion = nn.CrossEntropyLoss()

inputs, vowel, root, consonant = iter(train_loader).next()

inputs.shape

# +
epochs = 50 # original 50
model.train()
losses = []
accs = []
for epoch in range(epochs):
    running_loss = 0.0
    running_acc_vowel = 0.0
    running_acc_root = 0.0
    running_acc_consonant = 0.0

    for idx, (inputs, vowel, root, consonant) in tqdm(enumerate(train_loader),total=len(train_loader)):
        inputs = inputs.to(device)
        vowel = vowel.to(device)
        root = root.to(device)
        consonant = consonant.to(device)

        optimizer.zero_grad()
        outputs1, outputs2, outputs3 = model(inputs.unsqueeze(1).float())
        # print("outputs1: ", outputs1)
#         print("inputs: ", inputs.shape)
#         print("outputs1.shape: ", outputs1.shape)
#         print("vowel: ", vowel.shape)
        loss1 = criterion(outputs1, vowel.long())
        loss2 = criterion(outputs2, root.long())
        loss3 = criterion(outputs3, consonant.long())

        mini_batch_loss = loss1 + loss2 + loss3
        running_loss += mini_batch_loss * inputs.size(0)
        running_acc_vowel += (outputs1.argmax(1) == vowel.long()).sum()
        running_acc_root += (outputs2.argmax(1) == root.long()).sum()
        running_acc_consonant += (outputs3.argmax(1) == consonant.long()).sum()

        mini_batch_loss.backward()

        optimizer.step()
    #scheduler.step()
    losses.append(running_loss/len(train_dataset))
    print('running_acc_vowel : {:.2f}%'.format(running_acc_vowel.float()/len(train_dataset)))
    print('running_acc_root : {:.2f}%'.format(running_acc_root.float()/len(train_dataset)))
    print('running_acc_consonant : {:.2f}%'.format(running_acc_consonant.float()/len(train_dataset)))

    print('loss : {:.4f}'.format(running_loss/len(train_loader)))

torch.save(model.state_dict(), 'resnet34_50epochs_saved_weights.pth')
# -

model


from easydict import EasyDict as edict

config=edict()
config.class_list=['aeroplane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow','diningtable','dog','horse','motorbike','person','pottedplant','sheep','sofa','train','tvmonitor']
config.color_map=[[128,0,0],[0,128,0],[128,128,0],[0,0,128],[128,0,128],[0,128,128],[128,128,128],[64,0,0],[192,0,0],[64,128,0],[192,128,0],[64,0,128],[192,0,128],[64,128,128],[192,128,128],[0,64,0],[128,64,0],[0,192,0],[128,192,0],[0,64,128]]

config.txt_path='../voc2012/ImageSets/Main/'
config.img_path='../voc2012/JPEGImages/'
config.annotations_path='../voc2012/SegmentationClass/'
config.save_dir='./pascal-5/'

config.current_index=0    #0,1,2,3
config.pascal5_classlist=config.class_list[config.current_index*5:config.current_index+5]
config.pascal5_colormap=config.color_map[config.current_index*5:config.current_index+5]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('web', '0005_vhost_letsencrypt'),
    ]

    operations = [
        migrations.CreateModel(
            name='LetsEncrypt',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('state', models.CharField(max_length=255)),
                ('last_message', models.CharField(max_length=255)),
            ],
        ),
        migrations.RenameField(
            model_name='vhost',
            old_name='letsencrypt',
            new_name='use_letsencrypt',
        ),
        migrations.AddField(
            model_name='letsencrypt',
            name='vhost',
            field=models.OneToOneField(to='web.VHost', on_delete=models.CASCADE),
        ),
    ]


from collections import Counter

def solution(topping):
    answer = 0
    topDic = Counter(topping) # 토핑 딕셔너리
    setDic = set() # 비교를 위한 딕셔너리

    for i in topping:
        topDic[i] -= 1 # 개수를 하나 빼주고
        setDic.add(i) # 여기서는 더 해준다 (종류의 수만 같으면 되므로)

        if topDic[i] == 0: # 0개가 되면 (아예 종류를 삭제)
            topDic.pop(i)

        if len(setDic) == len(topDic): # 서로 종류의 수가 같다면 +1 을 해준다.
            answer += 1

    return answer


import numpy as np
from functools import reduce
import matplotlib.pyplot as plt

'''
This is a class which implements kernel SVM with gradient descent
'''

class SVM(object):
    def __init__(self, kernelfunc, lam):
        self._ker = kernelfunc
        self._lam = lam

    def train(self, x_train, y_train, discount=0.75):
        row, col = x_train.shape
        append = np.ones((row, 1))
        x = np.hstack((x_train, append))
        self._xtrain = x
        y = y_train
        gradz = np.zeros(row)

        self._alpha = np.zeros(row)
        self._kernelMat = np.zeros((row, row))

        for i in range(row):
            for j in range(row):
                self._kernelMat[i, j] = self._ker(x[i], x[j])

        def hinge_loss(arg):
            t = 1 - arg[0] * arg[1]
            if t > 0:
                gradz[arg[2]] = -arg[1]
                return t
            else:
                return 0.0

        def loss():
            l = 0.0
            gradz[:] = 0.0
            t = np.dot(self._kernelMat, self._alpha)
            all = map(hinge_loss, zip(t, y, range(row)))
            l += reduce(lambda x1, x2: x1 + x2, all, 0.0) / row
            l += self._lam / 2 * np.dot(np.dot(self._alpha, self._kernelMat), self._alpha)
            #print(self._kernelMat * gradz.reshape((row, 1)))
            grad_alpha = np.sum(self._kernelMat * gradz.reshape((row, 1)), axis=0)/row  \
                + self._lam * np.sum(self._kernelMat * self._alpha.reshape((row, 1)), axis=0)
            return l, grad_alpha

        # params for SGD with momentum
        v = np.zeros(row)
        rounds = 1000
        eta = 0.9
        llist = []
        lr = 0.01
        gd = np.zeros(row)
        for i in range(rounds):
            if (i+1)%100 == 0:
                lr = lr/10
            l, gd = loss()
            llist.append(l)
            v = eta * v + lr * gd
            self._alpha -= v

        temp = np.fabs(self._alpha) > 1e-6
        self._supalpha = self._alpha[temp]
        self._supvec = x[temp]

        plt.plot([i for i in range(len(llist))], llist)
        print(np.sum(x * self._alpha.reshape((row, 1)), axis=0))
        plt.show()

    def predict(self, x_test):
        row, col = x_test.shape
        append = np.ones((row, 1))
        x_new = np.hstack((x_test, append))
        result = []
        for i in x_new:
            t = 0
            len_data = len(self._supvec)
            for j in range(len_data):
                t += self._ker(i, self._supvec[j]) * self._supalpha[j]
            if t > 0:
                result.append(1)
            else:
                result.append(-1)
        return np.array(result)

svm = SVM(lambda x, y: np.dot(x, y), 0.001)

N = 30
t1 = np.random.randn(N, 2) + np.array([1, 0])
t2 = np.random.randn(N, 2) + np.array([30, 40])
x_train = np.vstack((t1, t2))

z1 = np.ones(N, dtype=int)
z2 = np.ones(N, dtype=int)
y_train = np.hstack((-z1, z2))
'''
x_train = np.array([
    [-2, 4],
    [4, 1],
    [1, 6],
    [2, 4],
    [6, 2]
])

y_train = np.array([-1,-1,1,1,1])
'''

svm.train(x_train, y_train)
tag = svm.predict(x_train)
temp = tag - y_train
print("error rate is: ",len(temp[temp!=0])/len(temp))


from functools import partial
from datetime import datetime
import winsound

numE1 = "Введите целое число >= 0."
numE2 = "Размер слова не может быть меньше 0."

fileE1 = "Проверьте наличие файла text.txt."
fileE2 = "В файле нет ни одного слова."
fileE3 = "В файле нет ни одного предложения."
fileE4 = "В файле нет ни одного слова с заданной длиной."
fileE5 = "В файле нет ни одного предложения с нечётным кол-вом слов с заданной длиной."

endOfSentence = ".!?"
endOfWord = " -.,!?\n"

def process(k: int):
    try: # попытка открыть файл
        with open("text.txt") as f:    # открытие файла
            start = datetime.now()      # время начала выполнения
            sentence = ""               # временная переменная для предложения
            sentenceFull = ""           # временная переменная для полного предложения (включая все символы)
            word = ""                   # временная переменная для слова
            count = 0                   # временная переменная для кол-ва слов длины k

            countSymbols = 0    # кол-во символов в файле

            countWords = 0      # кол-во слов в файле
            countSentences = 0  # кол-во предложений в файле

            countTargetWords = 0      # кол-во слов длины k в файле
            countTargetSentences = 0  # кол-во предложений с нечётным кол-вом слов длины k в файле

            # цикл по всем символам в файле
            for char in iter(partial(f.read, 1), ''):
                countSymbols += 1                   # увеличение счётчика
                sentenceFull += char                # добавляем символ к текущему предложению
                if char.isalpha():                  # если символ является буквой (то есть частью слова)
                    word += char                    #   - то продолжаем слово
                else:                               # иначе если символ - конец слова
                    if word:                        #   - то если слово не пусто
                        if len(word) > k:           #       - если длина слова > k
                            count += 1              #           -увеличение счётчика
                            countTargetWords += 1   #           -увеличение счётчика
                        countWords += 1             #       - увеличение счётчика
                        sentence += word + " "      #       - добавление слова в предложение
                        word = ""                   #       - обнуление слова
                if char in endOfSentence:           # если к тому же символ - конец предложения
                    if count % 2:                   #   - если слов длины k нечётное кол-во
                        countTargetSentences += 1   #       - увеличение счётчика
                                                    #       - обрезка пробелов в начале и конце слова
                        sentenceFull = sentenceFull.strip()
                        print(f"{sentenceFull}")    #       - печать предложения
                    countSentences += 1             #   - увеличение счётчика
                    sentence = ""                   #   - обнуление предложения
                    sentenceFull = ""               #   - обнуление предложения
                    count = 0                       #   - обнуление счётчика

            # Если последний символ в файле - НЕ конец, предложения, то нужно рассмотреть последнее предложение отдельно
            if char not in endOfSentence:
                if count % 2:  # - если слов длины k нечётное кол-во
                    countTargetSentences += 1  # - увеличение счётчика
                    #       - обрезка пробелов в начале и конце слова
                    sentenceFull = sentenceFull.strip()
                    print(f"{sentenceFull}")  # - печать предложения
                countSentences += 1  # - увеличение счётчика

            # проверка на содержание в файле
            if countWords == 0:
                print(fileE2)
            elif countTargetWords == 0:
                print(fileE4)
            if countSentences == 0:
                print(fileE3)
            elif countTargetSentences == 0:
                print(fileE5)

            winsound.Beep(1500, 300)

            end = datetime.now() - start # время выполнения программы
            print(f"Время выполнения: {end}")
            print(f"Символов в файле: {countSymbols}")
            print(f"Слов в файле:     {countWords}")
    except: # вывод ошибки открытия файла
        print(fileE1)

if __name__ == "__main__":
    while True: # цикл для ввода числа
        try: # попытка прочитать число
            k = int(input("Введите размер слова (K): "))
        except: # обработка ошибки ввода числа
            print(numE1)
            continue # следующая итерация цикла

        input()

        if k >= 0: # если число > 0
            process(k) # обработка файла
            break # конец цикла
        else: # обработка ошибки числа
            print(numE2)


# -*- coding: utf-8 -*-
"""
    Formatter - Demo

    Format column based document on user choice

    Usage:
    Run script to see how it works

    Note: By commenting or deleting everything, including,
    the <comment_or_delete> tags it is ready to be used

"""
from Npp import editor, notepad, console, MESSAGEBOXFLAGS
from itertools import zip_longest

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError as e:
    notepad.messageBox(('Unable to import Tkinter libraries,\n'
                        'these are needed for the UI.\n\n'
                        'Check your installation.\n\n'
                        '{}'.format(e.message)),'Missing Library', MESSAGEBOXFLAGS.ICONERROR)
    console.show()
    raise

def main():

    list_of_separators = [('Comma',0),('Semicolon',1),('Space',2),('Clipboard', 3)]
    separator_dict = {0:',', 1:';', 2:' ', 3:'clipboard'}

    list_of_areas = [('Whole document',0),('Selection only',1),('Visible lines only',2)]

    format_types_dict = {0:'\r\n',1:'\r',2:'\n'}

    def button_reformat():
        reformat(separator_value.get(), reformat_value.get())
        window.destroy()

    def button_quit(): window.destroy()

    def set_reformat_choice(): reformat_value.set(reformat_value.get())

    def set_separator_choice(): separator_value.set(separator_value.get())

    def prepare(separator, rows, eol):
        column_width_list = []
        for row in rows:
            splitted_row = row.split(separator)
            column_width_list = [max(x) for x in zip_longest(column_width_list, [len(x.strip()) for x in splitted_row], fillvalue=(0))]

        new_formatted_list = []
        for row in (x for x in rows):
            splitted_row = row.split(separator)
            new_formatted_list.append('{} '.format(separator).join(['{0:<{1}}'.format(x.strip(),column_width_list[i]) for i,x in enumerate(splitted_row)]))

        return new_formatted_list

    def reformat(separator,area):
        separator = window.clipboard_get() if separator_dict.get(separator) == 'clipboard' else separator_dict.get(separator)

        used_eol = format_types_dict.get(notepad.getFormatType())

        editor.beginUndoAction()

        if area == 0:
            rows = editor.getCharacterPointer().splitlines()
            rows = prepare(separator, rows, used_eol)
            editor.setText(used_eol.join(rows))
        elif area == 1:
            if editor.selectionIsRectangle():

                selections = [(editor.getSelectionNStart(i),editor.getSelectionNEnd(i)) for i in range(editor.getSelections())]
                rows = [editor.getRangePointer(x, y-x) for x,y in selections]
                rows = prepare(separator, rows, used_eol)

                editor.clearSelections()

                for i, position in zip(reversed(range(len(selections))), reversed(selections)):
                    editor.setTarget(*position)
                    editor.replaceTarget(rows[i])
            else:
                first_selected_line, last_selected_line = editor.getUserLineSelection()
                startpos = editor.positionFromLine(first_selected_line)
                endpos = editor.positionFromLine(last_selected_line)

                rows = editor.getRangePointer(startpos, endpos-startpos)
                rows = prepare(separator, rows.split(used_eol), used_eol)

                editor.setTarget(startpos, endpos)
                editor.replaceTarget(used_eol.join(rows))
        else:
            first_visible_line = editor.getFirstVisibleLine()
            startpos = editor.positionFromLine(first_visible_line)
            endpos = editor.positionFromLine(editor.linesOnScreen() + first_visible_line)

            rows = editor.getRangePointer(startpos, endpos-startpos)
            rows = prepare(separator, rows.split(used_eol), used_eol)

            editor.setTarget(startpos, endpos)
            editor.replaceTarget(used_eol.join(rows))
        editor.endUndoAction()

    window = tk.Tk()
    window.title("Formatter")
    window.attributes('-toolwindow', 1)
    window.attributes('-topmost', 1)
    window.configure(background='#282C34')
    window.grid_columnconfigure(0, weight=1)
    window.grid_columnconfigure(1, weight=1)

    control_styles = ttk.Style()
    control_styles.configure('TButton', font=('courier', 16, 'bold'), background='#282C34', relief='flat')
    control_styles.configure('TCheckbutton', font=('courier', 12), background='#282C34', foreground='#B5BCC9')
    control_styles.configure('TRadiobutton', font=('courier', 12), background='#282C34', foreground='#B5BCC9')
    control_styles.configure('TLabelframe.Label', font=('courier', 12, 'italic'), background='#282C34', foreground='#61AFEF')
    control_styles.configure('TLabelframe', background='#282C34', foreground='#B5BCC9')
    control_styles.configure('TLabel', font=('courier', 12), background='#282C34', foreground='#B5BCC9')
    control_styles.configure('TFrame', background='#282C34')

    separator_value = tk.IntVar()
    reformat_value = tk.IntVar()

    separator_frame = ttk.LabelFrame(window)
    separator_frame.configure(text='Separator')
    separator_frame.configure(width=280)

    for label, _value in list_of_separators:
        ttk.Radiobutton(separator_frame,
                        text=label,
                        command = set_separator_choice,
                        variable = separator_value,
                        value = _value).pack(padx=10, anchor=tk.W)
    separator_frame.grid(row=0, column=0, padx=10, pady=15, sticky=tk.W+tk.E+tk.N+tk.S)

    reformat_frame = ttk.LabelFrame(window)
    reformat_frame.configure(text='Area to reformat')
    reformat_frame.configure(width=280)

    for label, _value in list_of_areas:
        ttk.Radiobutton(reformat_frame,
                        text=label,
                        command=set_reformat_choice,
                        variable=reformat_value,
                        value=_value).pack(padx=10, anchor=tk.W)
    reformat_frame.grid(row=0, column=1, padx=10, pady=15, sticky=tk.W+tk.E+tk.N+tk.S)

    btn_frame = ttk.Frame(window)
    btn_frame.place(relx=0.02, rely=0.64, relheight=0.32, relwidth=0.96)
    btn_frame.configure(relief=tk.GROOVE)
    btn_frame.configure(borderwidth="2")
    btn_frame.configure(width=550)

    btn_reformat = ttk.Button(btn_frame)
    btn_reformat.place(relx=0.07, rely=0.24, height=47, width=207)
    btn_reformat.configure(text='Reformat')
    btn_reformat.configure(width=207)
    btn_reformat.configure(command=button_reformat)

    btn_quit = ttk.Button(btn_frame)
    btn_quit.place(relx=0.58, rely=0.26, height=47, width=207)
    btn_quit.configure(text='Quit')
    btn_quit.configure(width=207)
    btn_quit.configure(command=button_quit)

    # center window
    w = window.winfo_screenwidth()
    h = window.winfo_screenheight()
    size = (600,250)
    xpos = w/2 - size[0]/2
    ypos = h/2 - size[1]/2
    window.geometry("%dx%d+%d+%d" % (size + (xpos, ypos)))

    # start app - blocks rest of the script
    window.mainloop()

# <comment_or_delete>
def run_demo():

    demo_message = ('Running in demo mode\n'
                    'By commenting run_demo() line or by commenting/deleting the whole tag it is ready to be used')
    notepad.messageBox(demo_message,'Demo Mode')

    import random
    _number_of_lines = 100000
    _s = 50
    _line_split_separator = ','
    _data = ['{0}{3}{1}{3}{2}\r\n'.format('a'*random.randint(0,_s),'b'*random.randint(0,_s),'c'*random.randint(0,_s),_line_split_separator) for i in range(_number_of_lines)]
    notepad.new()
    editor.setText(''.join(_data))
    del _data

run_demo()
# </comment_or_delete>

main()


from typing import List, Optional, Tuple

from ..entities import Localization, Dictionary
from .command import Command
from pathlib import Path

from collections import OrderedDict

class Extract(Command):
    def execute(self, arguments: List[str]):
        path = self.get_path_from_arguments(arguments)
        if path is None:
            return

        dictionary = self._extract_texts(path)
        dictionary.save()

    def _extract_texts(self, path: Path) -> Dictionary:
        dictionary = self._load_exiting_data()

        files = sorted(path.glob("**/Localization.txt"), key=lambda x: str(x))
        for file in files:
            localization = Localization(file)
            for entry in localization.data:
                if entry is None:
                    continue
                if "english" not in entry:
                    continue
                english_text = entry["english"]
                if english_text == "":
                    continue
                key = entry["key"]
                if "japanese" in entry and entry["japanese"] != "":
                    japanese_text = entry["japanese"]
                else:
                    japanese_text = ""
                dictionary.add_entity(english=english_text, japanese=japanese_text, keys=[key])

        return dictionary

    def _load_exiting_data(self) -> Dictionary:
        data_path = self.get_data_path(directory="localizations").joinpath("localizations.txt")
        return Dictionary(path=data_path)


# In Politeknik Brunei
# Distinction is 80 marks above
# Merit is 65 to 79 marks
# Pass is 50 to 64 marks
# Fail is below 50 marks
main_menu = ('(1) Grade calculator',
            '(2) Histories',
            '(3) Show First Five',
            '(4) Show Last Five',
            '(5) Clear Histories',
            '(q) Quit Application')
while True:
    for menu in main_menu:
        print(menu)
    user_input = input('Please choose option as stated in parenthesis: ')

    if user_input == 'q':
        break
    elif user_input == '1':
        user_input = input('Please input marks between 0 to 100: ')
        if user_input.isdigit():
            grade = ''
            marks = int(user_input)
            if marks >= 0 and marks <= 100:
                if marks >= 80:
                    grade = 'Distinction'
                elif marks >= 65 and marks < 80:
                    grade = 'Merit'
                elif marks >= 50 and marks < 65:
                    grade = 'Pass'
                else:
                    grade = 'Fail'
                print(f'Marks inputted is {marks}, grade is {grade}')
            else:
                print('Invalid input. Must be between 0 to 100')
        else:
            print('Invalid input. Must be numbers.')
    elif user_input == '2':
        print('--- Showing histories ---')
    elif user_input == '3':
        print('--- Showing first five ---')
    elif user_input == '4':
        print('--- Showing last five ---')
    elif user_input == '5':
        print('--- Clearing histories ---')
    else:
        print('Invalid option chosen.')
    print()
print('Bye')


class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        fin=[]
        for i in range(2**len(nums)):
            lst=[]
            for j in range(len(nums)):
                if i%2==1: lst.append(nums[j])
                i=i//2
                if i==0: break

            fin.append(sorted(lst))

        lst2=[]
        for x in fin:
            if not x in lst2:
                lst2.append(x)

        return sorted(lst2,key= lambda x: x[0] if x else -1 )


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import List, Optional
from math import log2

from refinery.units.crypto.cipher import StandardBlockCipherUnit, Arg
from refinery.units.crypto.cipher.rc5 import rc5constants
from refinery.lib import chunks
from refinery.lib.crypto import (
    rotr,
    rotl,
    BlockCipher,
    BlockCipherFactory,
    CipherMode,
    CipherInterface,
    BufferType,
)

_W = 32
_R = 20

class RC6(BlockCipher):

    block_size: int
    key_size = range(256)
    _S: List[int]
    _w: int
    _r: int
    _u: int
    _m: int
    _g: int

    __slots__ = '_S', '_w', '_r', '_u', '_m', '_g'

    def __init__(self, key: BufferType, mode: Optional[CipherMode] = None, word_size: int = _W, rounds: int = _R):
        if word_size < 8 or word_size % 8:
            raise ValueError(F'Invalid word size: {word_size}')
        self._w = word_size
        self._u = word_size // 8
        self._r = rounds
        self._g = int(log2(word_size))
        self._m = (1 << word_size) - 1
        super().__init__(key, mode)

    @property
    def block_size(self):
        return self._u * 4

    def block_decrypt(self, block) -> BufferType:
        u = self._u
        w = self._w
        g = self._g
        r = self._r
        M = self._m
        S = self._S
        A, B, C, D = chunks.unpack(block, u)
        C = C - S[2 * r + 3] & M
        A = A - S[2 * r + 2] & M
        for i in range(r, 0, -1):
            A, B, C, D = D, A, B, C
            t = rotl(w, B * (2 * B + 1) & M, g)
            v = rotl(w, D * (2 * D + 1) & M, g)
            C = rotr(w, C - S[2 * i + 1] & M, t) ^ v
            A = rotr(w, A - S[2 * i + 0] & M, v) ^ t
        D = D - S[1] & M
        B = B - S[0] & M
        return chunks.pack((A, B, C, D), u)

    def block_encrypt(self, block) -> BufferType:
        u = self._u
        w = self._w
        g = self._g
        r = self._r
        M = self._m
        S = self._S
        A, B, C, D = chunks.unpack(block, u)
        B = B + S[0] & M
        D = D + S[1] & M
        for i in range(1, r + 1):
            t = rotl(w, B * (2 * B + 1) & M, g)
            v = rotl(w, D * (2 * D + 1) & M, g)
            A = rotl(w, A ^ t, v) + S[2 * i + 0] & M
            C = rotl(w, C ^ v, t) + S[2 * i + 1] & M
            A, B, C, D = B, C, D, A
        A = A + S[2 * r + 2] & M
        C = C + S[2 * r + 3] & M
        return chunks.pack((A, B, C, D), u)

    @property
    def key(self):
        return self._S

    @key.setter
    def key(self, key):
        w = self._w  # word size
        u = self._u  # length of a word in bytes
        r = self._r  # round count
        M = self._m  # bit mask
        L = list(chunks.unpack(key + (-len(key) % u) * B'\0', u))
        c = len(L)
        t = 2 * (r + 2)
        P, Q = rc5constants(w)
        S = [P]
        while len(S) < t:
            S.append(S[~0] + Q & M)
        i = j = 0
        A = B = 0
        for _ in range(3 * max(t, c)):
            A = S[i] = rotl(w, S[i] + A + B & M, 3)
            B = L[j] = rotl(w, L[j] + A + B & M, A + B)
            i = (i + 1) % t
            j = (j + 1) % c
        self._S = S

class rc6(StandardBlockCipherUnit, cipher=BlockCipherFactory(RC6)):
    """
    RC6 encryption and decryption. The parameter defaults are the RC6 parameters that were chosen
    for the AES candidacy. Only key sizes of 128, 192, and 256 bits are used for AES candidates, but
    the unit will allow any key size up to 256 bits.
    """
    def __init__(
        self, key, iv=b'', padding=None, mode=None, raw=False, segment_size=0,
        rounds    : Arg.Number('-k', help='Number of rounds to use, the default is {default}') = _R,
        word_size : Arg.Number('-w', help='The word size in bits, {default} by default.') = _W,
    ):
        super().__init__(
            key,
            iv,
            padding,
            mode,
            raw,
            segment_size,
            rounds=rounds,
            word_size=word_size
        )

    @property
    def block_size(self):
        return self.args.word_size // 2

    def _new_cipher(self, **optionals) -> CipherInterface:
        return super()._new_cipher(
            rounds=self.args.rounds,
            word_size=self.args.word_size,
            **optionals
        )


from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.models import User
from django.conf import settings
from fcompany.models import Company
import json
# Create your views here.

def save(request):
    if not request.user.is_authenticated():
        users = User.objects.all()
        return render_to_response('login.html', {'users': users})

    name = request.POST.get('name')
    desc = request.POST.get('desc')
    cid  = request.POST.get('cid')
    url  = request.POST.get('img_url')

    if cid:
        c = Company.objects.get(pk=cid)
    else:
        c = Company()
        c.user = request.user

    if url == 'img/blank.png':
        c.icon = ''

    c.name = name
    c.desc = desc

    c.save()
    if request.FILES:
        file = request.FILES['file']
        path = settings.IMAGES_DIR + '/companies/' + request.user.username + str(c.id) + '.png'
        fout = open(path,'w')
        fout.write(file.read())
        file.close()
        c.icon = request.user.username+''+str(c.id)

    c.save()

    return HttpResponse('/')

def get(request):
    if not request.user.is_authenticated():
        users = User.objects.all()
        return render_to_response('login.html', {'users': users})

    id  = request.POST.get('id')
    c   = Company.objects.get(pk=id)

    c.is_my = c.user == request.user

    return HttpResponse(json.dumps(c.json()), content_type = "application/json")

def getall(request):
    if not request.user.is_authenticated():
        users = User.objects.all()
        return render_to_response('login.html', {'users': users})

    data = [c.json() for c in Company.objects.all()]
    return HttpResponse(json.dumps(data), content_type = "application/json")

def delete(request):
    cid = request.POST.get('cid')

    if (not cid):
        return

    c = Company.objects.get(pk=cid)
    c.delete()

    return HttpResponse('/')


from django.forms import ModelForm
from .models import Pet, Family

class PetForm(ModelForm):

    class Meta:
        model = Pet
        fields = ['family', 'pet_name', 'pet_photo_url', 'bio', 'animal_type', 'age', 'birthday_adoptiversary', 'feeding_notes', 'care_routine', 'other_info']

class FamilyForm(ModelForm):

    class Meta:
        model = Family
        fields = ['family_name', 'family_address', 'contact1', 'contact1_phone', 'contact1_email', 'contact2', 'contact2_phone', 'contact2_email', 'emergency_contact', 'emergency_contact_phone', 'vet_clinic', 'vet_phone', 'vet_address']


import os
import shutil
import tempfile

# stub file paths
files = [
    ("foo.txt",),
    (
        "foo",
        "bar.txt",
    ),
    (
        "foo",
        "bar",
        "fleem.txt",
    ),
    (
        "foobar",
        "fleem.txt",
    ),
    ("bar.txt",),
    (
        "nested_tree",
        "bar",
        "fleem.txt",
    ),
    ("readonly.txt",),
]

def create_empty_stub():
    tempdir = tempfile.mkdtemp()
    return tempdir

def create_stub(tempdir=None):
    """create a stub directory"""

    tempdir = tempdir or tempfile.mkdtemp()
    try:
        for path in files:
            fullpath = os.path.join(tempdir, *path)
            dirname = os.path.dirname(fullpath)
            if not os.path.exists(dirname):
                os.makedirs(dirname)
            contents = path[-1]
            f = open(fullpath, "w")
            f.write(contents)
            f.close()
        return tempdir
    except Exception:
        try:
            shutil.rmtree(tempdir)
        except Exception:
            pass
        raise


#!/usr/bin/python3

import os, sys
import pandas as pd

sys.path.insert(0, '/clusterCAD')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "clusterCAD.settings")
import django
django.setup()
import pks.models

# Empty subunits are deleted by the pipeline as they are generated but clean checks again
for cluster in pks.models.Cluster.objects.all():
    nmodules = 0
    for subunit in cluster.subunits():
        nsubmodules = len(subunit.modules())
        if nsubmodules == 0:
            subunit.delete()
        nmodules += nsubmodules
    # Recompute product once invalid subunits have been deleted
    try:
        cluster.computeProduct(recompute=True)
    except:
        print(cluster.architecture())
        cluster.delete()
        print('Could not recompute product, deleted %s: %s' %(cluster.mibigAccession, cluster.description))
        continue
    # Delete clusters with no modules
    if nmodules == 0:
        print('No modules detected, deleted %s: %s' %(cluster.mibigAccession, cluster.description))
        cluster.delete()

# Delete clusters with no computable product
for cluster in pks.models.Cluster.objects.all():
    try:
        cluster.computeProduct()
    except:
        print('Could not compute product, deleted %s: %s' %(cluster.mibigAccession, cluster.description))
        cluster.delete()

print("\nDatabase cleanup completed. ")


import utils
import os
import numpy as np
import pickle as pkl
import config
from shutil import copyfile, copy2

def create_features(audio_dir, features_dir):
    """
    Create feature vectors from audio files
    """
    print("Create features: \n", "melbands: ", config.n_mels, ", win length: ", config.n_fft)
    superfolder = os.path.join(features_dir, "melbands"+str(config.n_mels)+"winlength"+str(config.n_fft))
    os.mkdir(superfolder)
    for category in config.CATEGORIES:
        audio_path = os.path.join(audio_dir, category)
        features_path = os.path.join(superfolder, category)
        os.mkdir(features_path)
        file_count = 0
        for audio in os.listdir(audio_path):
            if audio.endswith('.wav'):
                file_count += 1
                y, sr = utils.LoadAudioFile(os.path.join(audio_path, audio))

                #### create features here ####
                features = utils.generateMFCCs(y, sr)
                # deltas = utils.addDeltasAndDeltaDeltas(mfccs)
                # zcr = utils.zcr(y)
                # rms = utils.rms(y)
                # features = np.vstack((mfccs, rms))
                # features = utils.generateMelSpecImage(y, sr)
                with open(os.path.join(features_path, str(file_count).zfill(4)+".pkl"),'wb') as f:
                    pkl.dump(features, f)

    return superfolder

def divide_data(train_split=0.8):
    """
    Divide data to train and test sets.
    """
    training_folder = os.path.join(config.DATA_DIR, "training", "audio-small")
    eval_folder = os.path.join(config.DATA_DIR, "eval", "audio-small")

    for category in config.CATEGORIES:
        audio_path = os.path.join(config.AUDIO_DIR, category)
        category_path_train = os.path.join(training_folder, category)
        category_path_eval = os.path.join(eval_folder, category)
        os.mkdir(category_path_train)
        os.mkdir(category_path_eval)
        n_train = int(len(os.listdir(audio_path)) * train_split)
        for file_count, audio in enumerate(os.listdir(audio_path)):
            if audio.endswith('.wav'):
                # if file_count < n_train:
                if file_count < 140:
                    copy2(os.path.join(audio_path, audio), category_path_train)
                # else:
                elif file_count > 139 and file_count < 175:
                    copy2(os.path.join(audio_path, audio), category_path_eval)


import pygame

WIDTH = 800
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))

running = True

clock = pygame.time.Clock()
fps = 10

thingY = 0
inc = 1

while running:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((0, 0, 0))

    mx, my = pygame.mouse.get_pos()

    for y in range(20, HEIGHT, 20):
        for x in range(0, WIDTH, 20):
            pygame.draw.line(screen, (0, 255, 0), (x, y), (x+10, y-thingY))
            pygame.draw.line(screen, (0, 255, 0), (x+10, y-thingY), (x+20, y))

    if thingY == 10 or thingY == -10:
        inc *= -1

    thingY += inc

    pygame.display.flip()

pygame.quit()


import numpy as np

np.random.seed(0)
from matplotlib import pyplot as plt
from sklearn import linear_model

def ransac_fit(x, y, threshold=10, samples=2, trials=1000):
    best_inliers = np.zeros(x.shape[0], dtype=bool)
    for i in range(trials):
        indices = np.random.choice(x.shape[0], samples, replace=False)
        fit = np.polyfit(x[indices, 0], y[indices, 0], 1)
        residuals = np.abs(x[:, 0] * fit[0] + fit[1] - y[:, 0])
        inliers = residuals <= threshold
        if np.sum(inliers) > np.sum(best_inliers):
            best_inliers = inliers
    best_fit = np.polyfit(x[best_inliers, 0], y[best_inliers, 0], 1)
    return best_fit, best_inliers

# Data generation: Linear model with some noise
n_inliers = 500
inliers_x = np.random.normal(0.0, 10.0, (n_inliers, 1))
inliers_y = 3.0 * inliers_x + 5.0
inliers_y += np.random.normal(0.0, 2, (n_inliers, 1))

# Data generation: Other linear model with some noise
n_outliers = 50
outliers_x = np.random.normal(24.0, 4.0, (n_outliers, 1))
outliers_y = 0.0 * outliers_x - 1.0
outliers_y += np.random.normal(0.0, 2, (n_outliers, 1))

# Join both
data_x = np.vstack((inliers_x, outliers_x))
data_y = np.vstack((inliers_y, outliers_y))

# And shuffle
indices = np.arange(n_inliers + n_outliers)
np.random.shuffle(indices)
data_x = data_x[indices]
data_y = data_y[indices]

print(data_x)

# Do linear regression
lr = linear_model.LinearRegression()
lr.fit(data_x, data_y)
lr_line_x = np.array([[data_x.min()], [data_x.max()]])
lr_line_y = lr.predict(lr_line_x)
print(f"Linear fit: y={lr.coef_.item():.2f}x{lr.intercept_.item():+.2f}")

# Do RANSAC regression
rs = linear_model.RANSACRegressor(max_trials=1000)
rs.fit(data_x, data_y)
rs_inlier_mask = rs.inlier_mask_
rs_outlier_mask = np.logical_not(rs_inlier_mask)
rs_line_x = lr_line_x
rs_line_y = rs.predict(rs_line_x)
print("RANSAC fit, sklearn implementation")
print(
    f"    fit: y={rs.estimator_.coef_.item():.2f}x{rs.estimator_.intercept_.item():+.2f}"
)
print(
    f"    inliers: {np.sum(~rs_outlier_mask)}, RANSAC outliers: {np.sum(rs_outlier_mask)}"
)

# Own RANSAC implementation
fit, inliers_mask = ransac_fit(data_x, data_y)
print("RANSAC fit, own implementation")
print(f"    fit: y={fit[0]:.2f}x{fit[1]:+.2f}")
print(f"    inliers: {np.sum(inliers_mask)}, RANSAC outliers: {np.sum(~inliers_mask)}")

# Plot results
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(
    data_x[rs_inlier_mask],
    data_y[rs_inlier_mask],
    "o",
    color="green",
    label="RANSAC inliers",
)
ax.plot(
    data_x[rs_outlier_mask],
    data_y[rs_outlier_mask],
    "o",
    color="red",
    label="RANSAC outliers",
)
ax.plot(rs_line_x, rs_line_y, "-", color="yellowgreen", label="RANSAC fit")
ax.plot(lr_line_x, lr_line_y, "-", color="orange", label="Linear fit")
ax.grid()
ax.legend(loc="best", fancybox=True, framealpha=0.5)
plt.show()


import base64
import sqlite3
from datetime import date, timedelta, datetime
from io import BytesIO

import matplotlib.pyplot as plt
from more_itertools import batched
from quart import Quart, render_template_string, request

db_con = sqlite3.connect("temperatures.db")
app = Quart(__name__)

TEMPLATE = """
    <!DOCTYPE html>

            <title>TempLogger</title>

            Last reading

            <big>{{last_temperature[1]}}°C</big>

            {{last_temperature[0]}}

                <form method="get">
                    From: <input type="date" name="from-date" value="{{from_date}}">
                    To: <input type="date" name="to-date" value={{to_date}}>
                    <input type="submit" value="Select">
                </form>

"""

def get_temperatures(cursor, from_date: date, to_date: date) -> list[tuple]:
    from_timestamp = datetime(year=from_date.year, month=from_date.month, day=from_date.day).timestamp()
    to_timestamp = datetime(year=to_date.year, month=to_date.month, day=to_date.day, hour=23, minute=59).timestamp()
    cursor.execute(
        "SELECT * FROM temperatures WHERE timestamp >= ? AND timestamp <= ? ORDER BY timestamp ASC",
        (from_timestamp, to_timestamp),
    )
    temperature_records = [(datetime.fromtimestamp(r[0]), r[1]) for r in cursor]
    return temperature_records

def get_last_temperature(cursor) -> tuple:
    cursor.execute("SELECT * FROM temperatures ORDER BY timestamp DESC LIMIT 1")
    last_temperature = cursor.fetchone()
    app.logger.info(last_temperature)
    return datetime.fromtimestamp(last_temperature[0]), last_temperature[1]

@app.get("/")
async def chart():
    db_cursor = db_con.cursor()
    from_date = request.args.get("from-date", date.today() - timedelta(days=1))
    from_date = datetime.strptime(from_date, "%Y-%m-%d").date() if type(from_date) == str else from_date
    to_date = request.args.get("to-date", date.today())
    to_date = datetime.strptime(to_date, "%Y-%m-%d").date() if type(to_date) == str else to_date
    temperature_records = get_temperatures(db_cursor, from_date, to_date)
    last_temperature = get_last_temperature(db_cursor)
    plt.figure(figsize=(15, 5))
    plt.title("Temperature history")
    plt.grid()
    plt.xlabel("Time")
    plt.ylabel("Temperature")
    plt.plot([r[0] for r in temperature_records], [r[1] for r in temperature_records], color="tab:blue")
    batches = batched(temperature_records, 12)
    temperature_averages = [(b[-1][0], sum([r[1] for r in b]) / len(b)) for b in batches]
    plt.plot([r[0] for r in temperature_averages], [r[1] for r in temperature_averages], color="tab:green")
    figfile = BytesIO()
    plt.savefig(figfile, format="png")
    figfile.seek(0)
    figdata = base64.b64encode(figfile.getvalue())
    return await render_template_string(
        TEMPLATE, plot=figdata.decode(), last_temperature=last_temperature, from_date=from_date, to_date=to_date
    )

def main():
    app.run(debug=True, port=5000, host="0.0.0.0")

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Created on Sun Apr 18 20:47:44 2021

@author: cjove
"""

"""
Plotting del resultado de la identificación de la transición de sedestación a
bipedestación
"""
peaks = peaks_gyro(acc_filt)
thres_method = sit_to_stand_threshold(acc_filt)
det_sts = sit_to_stand(peaks, thres_method)
x = range(0,len(thres_method),1)
for y in x:
    plt.plot(acc_filt['Waist_Gx'][y])
    plt.plot(det_sts[y][0], acc_filt['Waist_Gx'][y][det_sts[y][0]], "o")
    plt.plot(det_sts[y][1], acc_filt['Waist_Gx'][y][det_sts[y][1]], "o")
    plt.show()

"""
Plotting del resultado de la identificación de la transición de sedestación a
bipedestación
"""

gy_invertidos_d = invertir(acc_filt['Right_Shank_Gy'], index_steps)
gy_invertidos_i = invertir(acc_filt['Left_Shank_Gy'], index_steps)

gait_d = gait_cycles(gy_invertidos_d, index_steps)
gait_i = gait_cycles(gy_invertidos_i, index_steps)

z = np.arange(0,474,1)
for y in z:
    m = np.arange(0, len(index_steps[y]))
    for x in m:
        n = np.arange(0, len(gait_d[y][x]),1)
        if len(n) == 0:
            break
        else:
            for w in n:
                plt.plot(gy_invertidos_d[y][index_steps[y][x][0]:index_steps[y][x][-1]])
                plt.plot(gait_d[y][x][w],gy_invertidos_d[y][index_steps[y][x][0]:index_steps[y][x][-1]][gait_d[y][x][w]], "o")
                #plt.plot(step_derecha[1][y][x],gy_invertidos_d[y][index_steps[y][x][0]:index_steps[y][x][-1]][step_derecha[1][y][x]], "o")
                plt.show()


import cv2
import numpy as np

# Erosion
# https://docs.opencv.org/4.2.0/d9/d61/tutorial_py_morphological_ops.html No. 1

# Read an image using cv2.imread
img = cv2.imread('../../resources/lena.png')

# As in cv2.dilate a matrix, called kernel, is needed
kernel = np.ones((5, 5), np.uint8)

# The dilated image is used since this is normally performed on binary images
# and img_edges is so thin on its edges that it disappears when eroded
img_edges = cv2.Canny(img, 100, 100)

img_dilation = cv2.dilate(img_edges, kernel, iterations=2)
cv2.imshow('Dilation Image', img_dilation)

''' cv2.erode
Parameters
    img: image
        Image from cv2.imread
    kernel: matrix
        All the pixels near boundary will be discarded depending upon the size of kernel
    iteration: integer
        How many iterations are wanted for the matrix to move around,
        which affects the thickness
'''
img_erosion = cv2.erode(img_dilation, kernel, iterations=2)
cv2.imshow('Erosion Image', img_erosion)

cv2.waitKey(0)
cv2.destroyAllWindows()