#!/usr/bin/env python
import ROOT

class DefineHistograms:
    """Class to define all histograms to be filled for SMELLIE analysis

    Attributes:
        t_res (TH1D) : time residual histogram for all events
        t_res_beam (TH1D) : time residual histogram for direct beam light
        t_res_double (TH1D) : time residual histogram for late pulses
        t_res_psup (TH1D) : time residual histogram for PSUP reflections
        t_res_avin (TH1D) : time residual histogram for far side AV reflections
        t_res_avout (TH1D) : time residual histogram for near side AV reflections
        t_res_scatt (TH1D) : time residual histogram for scattered photons
        t_res_multi (TH1D) : time residual histogram for multiple optical interactions
        angle_time (TH2D) : PMT angle with respect to the fibre direction vs time
                            residual for all events
        angle_time_beam (TH2D) : PMT angle with respect to the fibre direction
                                 vs time residual for direct beam light
        angle_time_double (TH2D) : PMT angle with respect to the fibre direction
                                 vs time residual for late pulses
        angle_time_scatt (TH2D) : PMT angle with respect to the fibre direction
                                  vs time residual for scattered photons
        angle_time_avin (TH2D) : PMT angle with respect to the fibre direction
                                 vs time residual for far side AV reflections
        angle_time_avout (TH2D) : PMT angle with respect to the fibre direction
                                  vs time residual for near side AV reflections
        angle_time_psup (TH2D) : PMT angle with respect to the fibre direction
                                 vs time residual for PSUP reflections
        angle_time_multi (TH2D) : PMT angle with respect to the fibre direction
                                  vs time residual for multiple optical interactions
        z_time (TH2D) : PMT z coordinate vs time residual for all events
        z_time_beam (TH2D) : PMT z coordinate vs time residual for direct beam light
        z_time_double (TH2D) : PMT z coordinate vs time residual for late pulses
        z_time_scatt (TH2D) : PMT z coordinate vs time residual for scattered photons
        z_time_avin (TH2D) : PMT z coordinate vs time residual for far side AV reflections
        z_time_avout (TH2D) : PMT z coordinate vs time residual for near side AV reflecitons
        z_time_psup (TH2D) : PMT z coordinate vs time residual for PSUP reflections
        z_time_multi (TH2D) : PMT z coordinate vs time residual for multiple optical interactions
        theta_phi (TH2D) : PMT theta coordinate vs PMT phi coordinate for all events
        theta_phi_beam (TH2D) : PMT theta coordinate vs PMT phi coordinate for direct beam light
        theta_phi_double (TH2D) : PMT theta coordinate vs PMT phi coordinate for late pulses
        theta_phi_scatt (TH2D) : PMT theta coordinate vs PMT phi coordinate for scattered photons
        theta_phi_avin (TH2D) : PMT theta coordinate vs PMT phi coordinate for far side AV reflections
        theta_phi_avout (TH2D) : PMT theta coordinate vs PMT phi coordinate for near side AV reflections
        theta_phi_psup (TH2D) : PMT theta coordinate vs PMT phi coordinate for PSUP reflections
        theta_phi_multi (TH2D) : PMT theta coordinate vs PMT phi coordinate for multiple interactions
        h_theta (TH1D) : PMT theta coordinate for all events
        h_theta_beam (TH1D) : PMT theta coordinate for direct beam light
        h_theta_double (TH1D) : PMT theta coordinate for late pulses
        h_theta_scatt (TH1D) : PMT theta coordinate for scattered photons
        h_theta_avin (TH1D) : PMT theta coordinate for far side AV reflections
        h_theta_avout (TH1D) : PMT theta coordinate for near side AV reflections
        h_theta_psup (TH1D) : PMT theta coordinate for PSUP reflections
        h_theta_multi (TH1D) : PMT theta coordinate for multiple optical interactions
        h_phi (TH1D) : PMT phi coordinate for all events
        h_phi_beam (TH1D) : PMT phi coordinate for direct beam light
        h_phi_double (TH1D) : PMT phi coordinate for late pulses
        h_phi_scatt (TH1D) : PMT phi coordinate for scattered photons
        h_phi_avin (TH1D) : PMT phi coordinate for far side AV reflections
        h_phi_avout (TH1D) : PMT phi coordinate for near side AV reflections
        h_phi_psup (TH1D) : PMT phi coordinate for PSUP reflections
        h_phi_multi (TH1D) : PMT phi coordinate for multiple optical interactions
        h_nhits (TH1D) : Number of hits for all events

    """

    def __init__(self):
        self.t_res = ROOT.TH1D("time_residual", "", 600, -20.0, 500.0)
        self.t_res_beam = ROOT.TH1D("time_residual_beam", "", 500, -20.0, 300.0)
        self.t_res_double = ROOT.TH1D("time_residual_double", "", 500, -20.0, 300.0)
        self.t_res_psup = ROOT.TH1D("time_residual_psup", "", 500, -20.0, 300.0)
        self.t_res_avin = ROOT.TH1D("time_residual_avin", "", 500, -20.0, 300.0)
        self.t_res_avout = ROOT.TH1D("time_residual_avout", "", 500, -20.0, 300.0)
        self.t_res_scatt = ROOT.TH1D("time_residual_scatt", "", 500, -20.0, 300.0)
        self.t_res_multi = ROOT.TH1D("time_residual_multi", "", 500, -20.0, 300.0)
        self.angle_time_beam = ROOT.TH2D("angle_time_beam", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.angle_time_double = ROOT.TH2D("angle_time_double", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.angle_time = ROOT.TH2D("angle_time", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.angle_time_scatt = ROOT.TH2D("angle_time_scatt", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.angle_time_avin = ROOT.TH2D("angle_time_avin", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.angle_time_avout = ROOT.TH2D("angle_time_avout", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.angle_time_psup = ROOT.TH2D("angle_time_psup", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.angle_time_multi = ROOT.TH2D("angle_time_multi", "", 150, -20.0, 300.0, 100, 0.0, 200.0)
        self.z_time = ROOT.TH2D("z_time", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.z_time_beam = ROOT.TH2D("z_time_beam", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.z_time_double = ROOT.TH2D("z_time_double", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.z_time_scatt = ROOT.TH2D("z_time_scatt", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.z_time_avin = ROOT.TH2D("z_time_avin", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.z_time_avout = ROOT.TH2D("z_time_avout", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.z_time_psup = ROOT.TH2D("z_time_psup", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.z_time_multi = ROOT.TH2D("z_time_multi", "", 150, -20.0, 300.0, 1000, -10000.0, 10000.0)
        self.theta_phi = ROOT.TH2D("theta_phi", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.theta_phi_beam = ROOT.TH2D("theta_phi_beam", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.theta_phi_double = ROOT.TH2D("theta_phi_double", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.theta_phi_scatt = ROOT.TH2D("theta_phi_scatt", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.theta_phi_avin = ROOT.TH2D("theta_phi_avin", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.theta_phi_avout = ROOT.TH2D("theta_phi_avout", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.theta_phi_psup = ROOT.TH2D("theta_phi_psup", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.theta_phi_multi = ROOT.TH2D("theta_phi_multi", "", 100, -3.5, 3.5, 100, 0, 3.5)
        self.h_theta =  ROOT.TH1D("theta", "", 100, 0.0, 3.5)
        self.h_theta_beam =  ROOT.TH1D("theta_beam", "", 100, 0.0, 3.5)
        self.h_theta_double =  ROOT.TH1D("theta_double", "", 100, 0.0, 3.5)
        self.h_theta_scatt =  ROOT.TH1D("theta_scatt", "", 100, 0.0, 3.5)
        self.h_theta_avin =  ROOT.TH1D("theta_avin", "", 100, 0.0, 3.5)
        self.h_theta_avout =  ROOT.TH1D("theta_avout", "", 100, 0.0, 3.5)
        self.h_theta_psup =  ROOT.TH1D("theta_psup", "", 100, 0.0, 3.5)
        self.h_theta_multi =  ROOT.TH1D("theta_multi", "", 100, 0.0, 3.5)
        self.h_phi =  ROOT.TH1D("phi", "", 100, -3.5, 3.5)
        self.h_phi_beam =  ROOT.TH1D("phi_beam", "", 100, -3.5, 3.5)
        self.h_phi_double =  ROOT.TH1D("phi_double", "", 100, -3.5, 3.5)
        self.h_phi_scatt =  ROOT.TH1D("phi_scatt", "", 100, -3.5, 3.5)
        self.h_phi_avin =  ROOT.TH1D("phi_avin", "", 100, -3.5, 3.5)
        self.h_phi_avout =  ROOT.TH1D("phi_avout", "", 100, -3.5, 3.5)
        self.h_phi_psup =  ROOT.TH1D("phi_psup", "", 100, -3.5, 3.5)
        self.h_phi_multi =  ROOT.TH1D("phi_multi", "", 100, -3.5, 3.5)
        self.h_nhits = ROOT.TH1D("number_of_hits","",200,0.0,1000.0)


__author__ = 'Vincent'

from sqlalchemy import *
from threading import Thread
from threading import Event
from gps import *
from utils.bdd import *
from utils.functions import *

class GpsThread(Thread):
    def __init__(self, session_db, event):
        Thread.__init__(self)
        self.db_session = session_db
        self.recording_interval = 0
        self.track_session_id = 0
        self.can_run = Event()
        self.stop_program = Event()
        self.event = event

    def set_recording_inteval(self, recording_interval):
        self.recording_interval = recording_interval

    def set_track_session_id(self, track_session_id):
        self.track_session_id = track_session_id

    def run(self):
        session = gps(mode=WATCH_ENABLE)

        while not self.stop_program.isSet():
            self.can_run.wait()

            while not self.stop_program.isSet() and self.can_run.isSet():
                # get the gps datas
                if session.waiting():
                    datas = session.next()

                if datas['class'] == "TPV":
                    self.event.set()
                    # create gps datas and insert it
                    gps_data = GPSData(latitude=session.fix.latitude, longitude=session.fix.longitude,
                                       speed=session.fix.speed,
                                       date_time=func.strftime('%Y-%m-%d %H:%M:%f', datetime.now()),
                                       session_id=self.track_session_id)
                    log.log("Insert: " + str(gps_data), log.LEVEL_DEBUG)
                    self.db_session.add(gps_data)
                    self.db_session.commit()
                    self.event.clear()

                time.sleep(self.recording_interval)

        log.log("GpsThread stopped !!", log.LEVEL_DEBUG)
        session = None

    def pause(self):
        log.log("GpsThread pausing ....", log.LEVEL_DEBUG)
        self.can_run.clear()

    def resume(self):
        log.log("GpsThread resuming ....", log.LEVEL_DEBUG)
        self.can_run.set()

    def stop(self):
        log.log("GpsThread stopping ....", log.LEVEL_DEBUG)
        self.stop_program.set()
        self.resume()


class Solution:
    # ends0: if we meet 0, we can append 0 to all existing ends0 + ends1
    # ends0 = ends0 + ends1
    #
    # ends1: if we meet 1, we can append 1 to all existing ends0 + ends1
    # and also adding "1"
    # ends1 = ends0 + ends1 + 1
    #
    # example:
    # num         1          0          1                  1
    # end0Count   0          1          1                  1
    # end1Count   1          1          3                  5
    # end0Arr     []        [10]       [10]               [10]
    # end1Arr     [1]       [1]        [11, 101, 1]       [101, 111, 1011, 11, 1]

    def numberOfUniqueGoodSubsequences(self, binary: str) -> int:
        MOD = 10**9 + 7
        ends0 = 0
        ends1 = 0
        has0 = 0
        for i, c in enumerate(binary):
            if c == '1':
                ends1 = (ends0 + ends1 + 1) % MOD
            else:
                ends0 = (ends0 + ends1) % MOD
                has0 = 1
        return (ends0 + ends1 + has0) % MOD

    def test(self):
        test_cases = [
            '001',
            '11',
            '101',
        ]
        for binary in test_cases:
            res = self.numberOfUniqueGoodSubsequences(binary)
            print('res: %s' % res)
            print('-='*30 + '-')

if __name__ == '__main__':
    Solution().test()


""" Module for city related models. """

from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.orm import relationship

from data import CONFIG
from models import Base

CONSUMPTION_RATES = CONFIG.get("game.cities.consumption")

class City(Base):
    """ Model for tracking city data. """

    __tablename__ = "City"
    id = Column(Integer, primary_key=True)

    name = Column(String, unique=True, nullable=False)
    population = Column(Integer, nullable=False)

    location_id = Column(
        Integer, ForeignKey("Location.id"), unique=True, nullable=False, index=True
    )
    location = relationship("Location")

    resources = relationship("CityResource", cascade="all, delete-orphan")

    def __str__(self) -> str:
        return f"{self.name}"

    def __repr__(self) -> str:
        return (
            "City("
            f"id={self.id}, "
            f"name='{self.name}', "
            f"location={self.location}, "
            f"population={self.population}, "
            f"location_id={self.location_id})"
        )

    @property
    def json(self):
        """ Get json data to send to client. """

        return {
            "id": self.id,
            "position": self.location.coordinate.json,
            "name": self.name,
            "population": self.population,
            "resources": {slot.resource.name: slot for slot in self.resources},
        }

class CityResource(Base):
    """ Model for tracking which resources a city has. """

    __tablename__ = "CityResource"
    id = Column(Integer, primary_key=True)

    amount = Column(Integer, nullable=False)

    city_id = Column(Integer, ForeignKey("City.id"), nullable=False, index=True)
    city = relationship("City")

    resource_id = Column(
        Integer, ForeignKey("ResourceType.id"), nullable=False, index=True
    )
    resource = relationship("ResourceType", uselist=False)

    def __str__(self) -> str:
        return f"{self.city} has {self.amount} {self.resource}"

    def __repr__(self) -> str:
        return (
            "CityResource("
            f"id={self.id}, "
            f"amount={self.amount}, "
            f"city_id={self.city_id}, "
            f"resource_id={self.resource_id})"
        )

    @property
    def price(self):
        """ Return price per unit of held resource. """

        saturation = max(self.amount, self.city.population / 2) / (
            self.city.population * CONSUMPTION_RATES[self.resource.name]
        )

        return self.resource.base_cost / saturation

    @property
    def amount_in_market(self):
        """ Return amount of resources available in market for players to purchase. """

        reserved = self.city.population * 2
        if reserved > self.amount:
            return 0

        return self.amount - reserved

    @property
    def json(self):
        """ Send json data for client. """

        return {"amount": self.amount, "price": self.price}


'''Crie um programa que tenha uma tupla com várias palavras
(não usar acentos). Depois disso, você deve mostrar, para cada palavra,
quais são as vogais'''

tupla = ('andre', 'camila', 'davi', 'isabella')

for palavra in tupla:
    print(f'\nNa palavra {palavra} temos as vogais: ', end='')
    for letra in palavra:
        if letra.lower() in 'aeiou':
         print(letra, end=' ')

'''
Primeiro, definimos a tupla de palavras: tupla = ('andre', 'camila', 'davi', 'isabella').

Em seguida, iniciamos um loop for para percorrer cada palavra na tupla: for palavra in tupla:.

Para cada palavra na tupla, imprimimos a mensagem inicial com a palavra atual: print(f'\nNa palavra {palavra} temos as vogais: ', end=''). O uso de \n antes da mensagem imprime uma nova linha para cada palavra.

Agora, iniciamos um segundo loop for para percorrer cada letra na palavra atual: for letra in palavra:.

Para cada letra na palavra, verificamos se ela é uma vogal. Usamos letra.lower() para converter a letra para minúscula e garantir uma comparação case-insensitive. A condição if letra.lower() in 'aeiou': verifica se a letra está presente na string de vogais.

Se a letra for uma vogal, a imprimimos seguida de um espaço em branco: print(letra, end=' '). O uso de end=' ' substitui a quebra de linha padrão por um espaço em branco, permitindo que as vogais sejam impressas lado a lado.

Após o término do segundo loop for, o fluxo do programa volta para o primeiro loop for, avançando para a próxima palavra na tupla.

Esse processo se repete para cada palavra na tupla até que todas as palavras tenham sido percorridas.'''


from Transformer import Transformer
from MultiHeadAttention import MultiHeadAttention
from tqdm import tqdm
from Metrics import grad
from Metrics import loss_function
from Metrics import loss_function2
from Metrics import accuracy_function
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score

from einops import rearrange

import tensorflow as tf
import os
import numpy as np
import joblib
import pandas as pd
import logging
import sqlite3 as sql
import time

num_layers = int(os.environ["TRANSFORMER_LAYERS"])
d_model = int(os.environ["W2V_EMBED_SIZE"])
dff = int(os.environ["TRANSFORMER_DFF"])
num_heads = int(os.environ["TRANSFORMER_HEADS"])
batch_size = int(os.environ["BATCH_SIZE"])
training = bool(int(os.environ["TRAINING"]))
epochs = int(os.environ["EPOCHS"])
max_seq_len = 200

class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):

    def __init__(self, d_model: int, warmup_steps=4000):
        super(CustomSchedule, self).__init__()

        self.d_model = d_model
        self.d_model = tf.cast(self.d_model, tf.float32)
        self.warmup_steps = warmup_steps

    def __call__(self, step):
        arg1 = tf.math.rsqrt(step)
        arg2 = step * (self.warmup_steps ** -1.5)

        return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)

learning_rate = CustomSchedule(d_model)

optimus_prime = None
adm_optimizer = tf.keras.optimizers.Adam(learning_rate, beta_1=0.9, beta_2=0.98, epsilon=1e-9)

epoch_loss = tf.keras.metrics.Mean(name='train_loss')
epoch_accuracy = tf.keras.metrics.Mean(name='train_accuracy')

train_step_signature = [
    tf.TensorSpec(shape=([batch_size, None]), dtype=tf.float32),
    tf.TensorSpec(shape=([batch_size]), dtype=tf.float32)
]

add_att_layer = tf.keras.layers.AdditiveAttention()
softmax = tf.keras.layers.Softmax()
lr = LogisticRegression()

s1 = tf.keras.Sequential([
    tf.keras.layers.Dense(512),
    tf.keras.layers.Dense(4),
    tf.keras.layers.Softmax()
])

@tf.function(input_signature=train_step_signature)
def train_step(log_batch: tf.Tensor, labels: tf.Tensor):

    transformer_input = tf.tuple([
        log_batch,  # <tf.Tensor: shape=(batch_size, max_seq_len), dtype=float32>
        labels  # <tf.Tensor: shape=(batch_size, num_classes), dtype=float32>
    ])

    with tf.GradientTape() as tape:
        Rs, _ = optimus_prime.call(transformer_input)

        # a_s = add_att_layer([Rs, Rs])
        # y = softmax(a_s * Rs)

        y = Rs

        loss = tf.py_function(loss_function, [labels, y], tf.float32)

        pred = s1(y)
        labels = tf.cast(labels, tf.int64)

    # Optimize the model
    grads = tape.gradient(loss, optimus_prime.trainable_variables)
    adm_optimizer.apply_gradients(zip(grads, optimus_prime.trainable_variables))

    # Tracking Progress
    epoch_loss.update_state(loss)  # Adding Batch Loss
    epoch_accuracy.update_state(accuracy_function(labels, pred))

logging.basicConfig(format='%(asctime)s %(levelname)s | %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

def database_builder(path: str) -> pd.DataFrame():
    logger.info('Building DataFrame ...')
    (_, _, files) = next(os.walk(path))
    sql_query = 'SELECT * FROM logs'
    data = []
    for f in files:
        if '.db' in f:
            conn = create_connection(path + f)
            d = pd.read_sql_query(sql_query, conn)
            data.append(d)
    logger.info('...complete!')
    return pd.concat(data)

def create_connection(path: str) -> sql.Connection:
    """
    Creates a database connection
    :param path: str
        path to database object
    :return sql.Connection
        a connection to the database
    """
    try:
        conn = sql.connect(path)
        logger.info('Connected to database ' + path)
        return conn
    except sql.Error as e:
        logger.warning(e)

def get_max_length_(dataset: pd.DataFrame, buffer_size: float) -> int:
    return int((1 + buffer_size) * dataset['log'].str.len().max())

def process_batch(dataset: pd.DataFrame,
                  vocabulary: dict,
                  max_seq_len: int,
                  idx: int,
                  labels: dict) -> tuple:
    logs = np.zeros((batch_size, max_seq_len))
    y_true = np.empty((batch_size,))

    start_window = idx * batch_size
    end_window = (idx + 1) * batch_size
    for log_idx, log in enumerate(dataset['log'][start_window:end_window]):
        for seq_idx, word in enumerate(log.split()):
            if seq_idx >= max_seq_len:
                break
            logs[log_idx, seq_idx] = vocabulary[word] if word in vocabulary.keys() else 0
        y_true[log_idx] = labels[dataset['label'][log_idx]]

    return tf.convert_to_tensor(logs, dtype=tf.float32), tf.convert_to_tensor(y_true, dtype=tf.float32)

if __name__ == '__main__':

    logging.info('Loading assets')
    word_embedding_matrix = joblib.load("/results/w2v_weights.joblib")
    vocabulary = joblib.load("/results/vocab_dict.joblib")
    dataset = database_builder('/database/')
    dataset = dataset.sample(frac=1).reset_index(drop=True)
    max_seq_len = 200  # get_max_length_(dataset, 0.0)
    vocab_size = len(vocabulary)

    logging.info('Processing logs for training')
    label_unique = dataset['label'].unique()
    lbp = LabelEncoder().fit(label_unique)
    binary_labels = lbp.transform(label_unique)

    log_labels = {}
    for idx, label in enumerate(label_unique):
        log_labels.update({
            label: binary_labels[idx]
        })

    n_logs = len(dataset.index)
    n_iter = n_logs // batch_size
    remainder = n_logs % batch_size
    attns = []

    optimus_prime = Transformer(
        num_layers,
        d_model,
        num_heads,
        dff,
        vocab_size,
        word_embedding_matrix,
        max_seq_len,
        rate=0.1)

    optimizer = tf.keras.optimizers.Adam(learning_rate, beta_1=0.9, beta_2=0.98, epsilon=1e-9)

    checkpoint_path = "./checkpoints/train"
    checkpoint = tf.train.Checkpoint(step=tf.Variable(1), transformer=optimus_prime, optimizer=optimizer)
    checkpoint_manager = tf.train.CheckpointManager(checkpoint, checkpoint_path, max_to_keep=5)

    # if a checkpoint exists, restore the latest checkpoint.
    if checkpoint_manager.latest_checkpoint:
        checkpoint.restore(checkpoint_manager.latest_checkpoint)
        print('Latest checkpoint restored!!')

    for epoch in tqdm(range(epochs)):

        start = time.time()

        epoch_loss.reset_states()
        epoch_accuracy.reset_states()

        for idx in range(n_iter):
            log_batch, labels = process_batch(dataset, vocabulary, max_seq_len, idx, log_labels)

            # Returns Eager Tensor for Predictions
            train_step(log_batch, labels)
            checkpoint.step.assign_add(1)

            if int(checkpoint.step) % 10 == 0:
                save_path = checkpoint_manager.save()
                print(f'Saved checkpoint for step {int(checkpoint.step)}: {save_path}')
                print(f'Loss {epoch_loss.result():.3f}, Accuracy: {epoch_accuracy.result():.3%}')

        print("Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}".format(epoch,
                                                                    epoch_loss.result(),
                                                                    epoch_accuracy.result()))

        print(f'Time taken for 1 epoch: {time.time() - start:.2f} secs\n')


# https://www.hackerrank.com/challenges/zipped/problem

N, X = map(int, input().split())
data = []
for _ in range(X):
    subject_marks = list(map(float, input().split()))
    data.append(subject_marks)

tuples = zip(*data)
for element in tuples:
    print(sum(element) / X)


import matplotlib.pyplot as plt
import numpy as np

"""
Plot of success rate for a single NN with different control horizons
"""

# testing_result = [47.74, 52.76, 61.81, 63.82, 50.75]
# baseline_result = [44.72, 45.73, 51.25, 52.26, 46.73]

testing_result = [48, 53, 62, 64, 51]
baseline_result = [45, 46, 51, 52, 47]

# labels = ['1.5', '1', '0.5', '0.25', '0.15']
labels = ['0.67', '1', '2', '4', '6.67']
# x = np.arange(len(labels))
x_coordinate = np.asarray([1, 2, 3, 4, 5])

fig, ax = plt.subplots()
ax.set_ylabel('Success rate (%)', fontsize=12)
ax.set_xlabel('Replanning frequency (Hz)', fontsize=12)
ax.set_title('Success rate with different replanning frequencies', fontsize=12)
ax.set_xticks(x_coordinate)
ax.set_xticklabels(labels)
ax.set_yticks(np.arange(45, 65, 5))

plt.plot(x_coordinate, testing_result, 'o-', label='WayPtNav-ReachabilityCost')
plt.plot(x_coordinate, baseline_result, 's-', label='WayPtNav-HeuristicsCost')

# for i, value in enumerate(testing_result):
#     x = x_coordinate[i]
#     y = testing_result[i]
#     if i == 0:
#         scatter = ax.scatter(x, y, marker='x', color='red', label='Ours')
#     else:
#         scatter = ax.scatter(x, y, marker='x', color='red')
#     ax.text(x + 0.05, y + 0.05, value, fontsize=9)
#
#
# for i, value in enumerate(baseline_result):
#     x = x_coordinate[i]
#     y = baseline_result[i]
#     if i == 0:
#         ax.scatter(x, y, marker='o', color='blue', label='Baseline')
#     else:
#         ax.scatter(x, y, marker='o', color='blue')
#     ax.text(x + 0.05, y + 0.05, value, fontsize=9)

ax.legend(loc='lower right')

ax.set_aspect(aspect=0.2)

plt.show()

plot_path = '/home/anjianl/Desktop/project/WayPtNav_paper/plots/ctrlhorizon_success_rate.png'
fig.savefig(plot_path)


# -*- coding = utf-8 -*-
"""
    the :mod `dataset` module provides the dataset class
    and other subclasses which are used for managing datasets
"""
import pandas as pd
import numpy as np

class Dataset:
    """base class for loading datasets

    Note that you should never instantiate the class :class: `Dataset` class directly,
    and just use the below available methods for loading datasets.
    """

    def __init__(self,cfg):
        self._cfg = cfg

        self.movies = pd.read_csv(self._cfg.DATASET.MOVIE_SET)
        self.ratings_train = pd.read_csv(self._cfg.DATASET.TRAIN_SET)
        self.ratings_test = pd.read_csv(self._cfg.DATASET.TEST_SET)

        self.user_list_train = self.ratings_train['userId'].drop_duplicates().values.tolist()
        self.user_list_test = self.ratings_test['userId'].drop_duplicates().values.tolist()
        self.user_list = self.user_list_train + self.user_list_test
        self.user_list = list(set(self.user_list))

        self.movie_list = self.movies['movieId'].drop_duplicates().values.tolist()
        self.genre_list = self.movies['genres'].values.tolist()
        self.movie_type_list = self.get_movie_type_list(self.genre_list)

        self.user_map_train, self.user_map_reverse_train = self.get_list_index_map(self.user_list_train)
        self.user_map_test, self.user_map_reverse_test = self.get_list_index_map(self.user_list_test)
        self.type_map, self.type_map_reverse = self.get_list_index_map(self.movie_type_list)
        self.user_map, self.user_map_reverse = self.get_list_index_map(self.user_list)
        self.movie_map, self.movie_map_reverse = self.get_list_index_map(self.movie_list)

        self.movie_type_features = self.get_movie_type_features(self.movies)

    def get_movie_type_features(self,movies):
        """
        get the movie type features, tf-idf matrix
        """
        movie_type_features = np.zeros((len(self.movie_list),len(self.movie_type_list)))
        for row in self.movies.itertuples(index=True,name="Pandas"):
            movie_id = self.movie_map[getattr(row,'movieId')]
            movie_types = getattr(row,'genres').split('|')
            for movie_type in movie_types:
                if movie_type != '(no genres listed)':
                    movie_type_index = self.type_map[movie_type]
                    movie_type_features[movie_id,movie_type_index] = 1
        return movie_type_features # tfidf matri

    def get_list_index_map(self,list):
        """
        get the index map of a list
        """
        index_map = {}
        index_map_reverse = {}
        for i,item in enumerate(list):
            index_map[item] = i
            index_map_reverse[i] = item
        return index_map, index_map_reverse

    def get_movie_type_list(self,genres_list):
        """
        get the movie type list
        """
        movie_type_list = []
        for item in genres_list:
            movie_types = item.split('|')
            for movie_type in movie_types:
                if movie_type not in movie_type_list and movie_type != '(no genres listed)':
                    movie_type_list.append(movie_type)
        return movie_type_list

    def get_trainset(self):
        """
        get the trainset
        @return: (trainset,user_map,movie_map,type_map)
        """
        return (self.ratings_train,self.user_map,self.movie_map,self.movie_type_features)

    def get_testset(self):
        """
        get the testset
        @return: (testset,user_map,movie_map,type_map)
        """
        return (self.ratings_test,self.user_map,self.movie_map,self.movie_type_features)

    def get_movie_name_by_movie_id(self,movie_id):
        """
        get the movie name by movie id
        """
        return self.movies[self.movies['movieId'] == movie_id]['title'].values[0]

if __name__ == '__main__':
    from config import cfg
    dataset = Dataset(cfg)
    # print(dataset.user_list)
    # print(dataset.movie_list)
    # print(dataset.movie_type_list)
    print(dataset.type_map)
    print(dataset.type_map_reverse)
    # print(dataset.user_map)
    # print(dataset.user_map_reverse)
    # print(dataset.movie_map)
    # print(dataset.movie_map_reverse)

    # genres type list
    print(dataset.type_map.keys())
    # ['Adventure', 'Animation', 'Children', 'Comedy', 'Fantasy', 'Romance', 'Drama', 'Action', 'Crime', 'Thriller', 'Horror', 'Mystery', 'Sci-Fi', 'Documentary', 'IMAX', 'War', 'Musical', 'Western', 'Film-Noir']


from django.urls import path

from . import views

app_name = "public"
urlpatterns = [
    path("", views.index, name="index"),
    path("about", views.about, name="about"),
    path("upload_dataset", views.upload_dataset, name="upload_dataset"),
    path("train_model", views.train_model, name="train_model"),
    path("test_model", views.test_model, name="test_model"),
]


"""Add md5 and sha256 columns to File

Revision ID: d128b94f9a63
Revises: 59d249ebf873
Create Date: 2021-10-24 14:54:30.381535

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'd128b94f9a63'
down_revision = '59d249ebf873'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('files', sa.Column('md5', sa.LargeBinary(length=16), nullable=True))
    op.add_column('files', sa.Column('sha256', sa.LargeBinary(length=32), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('files', 'sha256')
    op.drop_column('files', 'md5')
    # ### end Alembic commands ###


#it is used to check the quatitity

import re
p1="a+"#occurance of a ,+ isused tocheck the occurance
p2="a*"#it will check for all a occurance
p3="a?"#it all position of a
p4="a{2}"#it will check only 2 occurances of a
p5="a{2,3}"#it will check min 2 num of a and max 3 num of a
p6="[Kl"
p=re.finditer(p3,"KL38C2280")
count=0
for i in p:
    count+=1
    print(i.start())# to find the position of mtching
    print(i.group())# which object is matching
print("Count",count)


from threading import Thread
from time import sleep

from .. import Command, BaseTask
from ... import app_manager

class MeasureWeight(BaseTask):
    """
    Measures weight and saves it to database.

    Extra parameters:

    - 'device_id': str - ID of target device,
    - 'sleep_period': float - measurement period
    """
    def __init__(self, config):
        self.__dict__.update(config)

        required = ['sleep_period', 'device_id', 'task_id']
        self.validate_attributes(required, type(self).__name__)

        self.device = app_manager.deviceManager.get_device(self.device_id)
        super(MeasureWeight, self).__init__(config)

    def start(self):
        """
        Start the task.
        """
        t = Thread(target=self._run)
        t.start()

    def _run(self):
        while self.is_active:
            cmd = Command(self.device_id, "1", [], self.task_id)
            self.device.post_command(cmd)
            cmd.await_cmd()
            cmd.save_data_to_db()
            sleep(int(self.sleep_period))

    def end(self):
        """
        End the task.
        """
        self.is_active = False


import torch
from tqdm import tqdm

def evaluate(model, loader, device):
    """
    Evaluation function to calculate loss and accuracy on Val/test dataset
    Args:
        model (nn.Module): model to be evaluated on the give dataset
        loader (DataLoader): Validation/Test dataloader to evaluate the model on.
        device (torch.device): The device (CPU/GPU) to perform the evalutation on.
    """
    model.to(device)
    model.eval()
    correct = 0
    total = 0
    running_loss = 0.0
    criterion = torch.nn.CrossEntropyLoss()

    with torch.no_grad():
        for images, labels in loader:
            images, labels = images.to(device), labels.to(device)

            # Forward pass
            outputs = model(images)
            predicted = torch.argmax(outputs.data, 1)

            total += labels.size(0)
            correct += (predicted == labels).sum().item()
            loss = criterion(outputs, labels)
            running_loss += loss.item()

    accuracy = 100 * correct / total
    avg_loss = running_loss / len(loader)
    model.train()
    return accuracy, avg_loss, correct


import torch
from torch import nn
from torch.autograd import Variable
import torch.functional as F
from torch.optim import Adam
from torchvision.models import resnet50
# self
from vis import Vis
import vars
from data_loader import get_data_loader
from test import test

def train(epoch, model, train_loader, criterion, optimizer, vis):
    model.train()
    for i, (data, label) in enumerate(train_loader):
        data = Variable(data).cuda() # gpu
        label = Variable(label).cuda() # gpu

        optimizer.zero_grad()

        output = model(data)

        loss = criterion(output, label)
        loss.backward()
        optimizer.step()

        if i % 30 == 0:
            status = 'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch + 1, i * len(data), len(train_loader.dataset),
                100. * i / len(train_loader), loss.item())
            print(status)
            vis.update_train(x=torch.Tensor([epoch + i/len(train_loader)]),
                             y=torch.Tensor([loss.item()]), status=status)

if __name__ == '__main__':
    # load data and init
    train_loader, test_loader = get_data_loader()
    vis = Vis('bba_race resnet')

    # model
    model = resnet50()
    input_size = model.fc.in_features
    model.fc = nn.Linear(input_size, 20) # output 20 category

    # load exist
    # checkpoints = vars.checkpoint_path + 'res_net50_0.14.pt'
    checkpoints = ''
    if checkpoints:
        model.load_state_dict(torch.load(checkpoints)) # load exist model
    model.cuda() # gpu

    # criterion, optimizer
    criterion = nn.CrossEntropyLoss().cuda() # gpu
    optimizer = Adam(model.parameters(), lr=0.01)

    epoches = 1
    for epoch in range(epoches):
        train(epoch, model, train_loader, criterion, optimizer, vis)
        # save the model
        torch.save(model.state_dict(), vars.checkpoint_path + 'res_net50_{}.pt'.format(epoch))
        test(epoch, model, test_loader, criterion, vis)


# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox

class MyWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.myButton = QtWidgets.QPushButton(self)

        self.myButton.clicked.connect(self.msg)
        self.msg()
    def msg(self):
        reply = QMessageBox.information(self,  # 使用infomation信息框
                                        "标题",
                                        "消息",
                                        QMessageBox.Yes | QMessageBox.No)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    myshow = MyWindow()
    myshow.show()
    sys.exit(app.exec_())


from flask import Flask, request, jsonify
import requests
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, BooleanType
import threading
import logging
import time

app = Flask(__name__)

# Create a SparkSession (Singleton)
spark = SparkSession.builder.appName("APIDataProcessing").getOrCreate()

# Define the schema for the DataFrame
schema = StructType([
    StructField("API", StringType(), True),
    StructField("Description", StringType(), True),
    StructField("Category", StringType(), True),
    StructField("Auth", StringType(), True),
    StructField("HTTPS", BooleanType(), True),
    StructField("Cors", StringType(), True),
    StructField("Link", StringType(), True),
])

# Function to fetch and filter data and create a Spark DataFrame
def fetch_and_filter_data(api_url):
    try:
        response = requests.get(api_url)
        response.raise_for_status()
        data = response.json()['entries']

        # Filter the data to include only HTTPS links
        filtered_data = [entry for entry in data if entry['HTTPS']]

        # Create a Spark DataFrame from the filtered data
        df = spark.createDataFrame(filtered_data, schema=schema)
        return df
    except requests.exceptions.RequestException as e:
        logging.error(f"Error fetching data from the API: {e}")
        return None

# Function to periodically fetch and save data
def periodic_data_fetch_and_save(api_url, interval_seconds, data_path):
    while True:
        df = fetch_and_filter_data(api_url)

        if df:
            # Save the DataFrame as a partitioned Parquet file
            df.write.partitionBy("Category").parquet(data_path, mode="append")
            logging.info(f"Data saved at {time.ctime()}")

        time.sleep(interval_seconds)

# Start a background thread to periodically fetch and save data
api_url = "https://api.publicapis.org/entries"
data_path = "api_data.parquet"
fetch_thread = threading.Thread(target=periodic_data_fetch_and_save, args=(api_url, 12*60*60, data_path))  # Fetch every 12 hours
fetch_thread.daemon = True
fetch_thread.start()

# Endpoint for /categories
@app.route('/categories', methods=['GET'])
def get_categories():
    # Load categories from the saved data file
    df = spark.read.parquet(data_path)
    categories = df.select("Category").distinct().rdd.flatMap(lambda x: x).collect()

    # Return categories as JSON
    return jsonify(categories)

# Endpoint for /data/<category>
@app.route('/data/<category>', methods=['GET'])
def get_data_by_category(category):
    # Load data from the saved data file
    df = spark.read.parquet(data_path)

    # Get the optional search query from the request
    search_query = request.args.get('search_query')

    # Filter data by category
    filtered_df = df.filter(df["Category"] == category)

    # Apply search filter if search_query is provided
    if search_query:
        search_query = search_query.lower()
        filtered_df = filtered_df.filter(
            (df["API"].rlike(search_query)) | (df["Description"].rlike(search_query))
        )

    # Convert the filtered DataFrame to a list of dictionaries
    filtered_data = filtered_df.rdd.map(lambda row: row.asDict()).collect()

    # Return filtered data as JSON
    return jsonify(filtered_data)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    app.run(debug=True)


import sys
input = sys.stdin.readline

n,m,r = map(int,input().split())
items = [0] + list(map(int,input().split()))
graph = [[int(5e6)]*(n+1) for _ in range(n+1)]

for _ in range(r):
    s,e,w = map(int,input().split())
    graph[s][e] = w
    graph[e][s] = w

for i in range(1,n+1):
    graph[i][i] = 0

def floyd():
    for k in range(1,n+1):
        for i in range(1,n+1):
            for j in range(1,n+1):
                graph[i][j] = min(graph[i][j], graph[i][k]+graph[k][j])

floyd()

maxFarm = 0
for zone in graph[1:]:
    farm = 0
    for i,x in enumerate(zone):
        if x <= m:
            farm += items[i]
    maxFarm = max(maxFarm, farm)

print(maxFarm)


import shutil
import difflib
import filecmp

import logging
from prettyconf import config

BOOTSTRAP_TESTS = config("BOOTSTRAP_TESTS", default=False)

LOG = logging.getLogger(__name__)

def compare_files(*, got, expected):
    """Compares the contents of a test file against expected

    Args:
        got: file with temp data
        expected: file with expected data

    Note: setting the environment variable BOOTSTRAP_TESTS=1
    will overwrite the contents of the "expected" file with the
    "got" file (ie bootstrapping the test files).
    """
    if BOOTSTRAP_TESTS:
        LOG.warning("BOOTSTRAP_TESTS: copying '%s' to '%s'", got, expected)
        shutil.copy(got, expected)

    got = f"{got}"
    expected = f"{expected}"

    are_files_identical = filecmp.cmp(got, expected)

    if not are_files_identical:
        diff_result = list(
            difflib.unified_diff(
                open(got, "r").readlines(), open(expected, "r").readlines()
            )
        )
        LOG.warning(f"Difference between got ({got}) and expected ({expected}) ...")
        for diff_line in diff_result:
            LOG.warning(diff_line.strip())

    return are_files_identical


import torch.nn as nn
from utils.config import config
import torch
import numpy as np
from model.bbox_tools import *
import torch.nn.functional as F
from torchvision.ops import nms

def nograd(f):
    def new_f(*args, **kwargs):
        with torch.no_grad():
            return f(*args, **kwargs)
    return new_f

class FasterRCNN(nn.Module):
    def __init__(self,
                 extractor,
                 rpn,
                 head,
                 offset_normalize_mean=(0., 0., 0., 0.),
                 offset_normalize_std=(0.1, 0.1, 0.2, 0.2),
                 ):
        super(FasterRCNN, self).__init__()
        self.extractor = extractor
        self.rpn = rpn
        self.head = head
        self.offset_normalize_mean = offset_normalize_mean
        self.offset_normalize_std = offset_normalize_std

    @property
    def n_class(self):
        return self.head.n_class

    def forward(self, x, scale):
        img_size = x.shape[2:]
        x = self.extractor(x)
        rois, rois_index, _, _, _ = self.rpn(
            x=x, img_size=img_size, scale=scale)
        roi_bbox_pred, roi_cls_scores = self.head(
            x=x, rois=rois, rois_index=rois_index)
        return rois, roi_bbox_pred, roi_cls_scores

    @nograd
    def predict(self, img, scale=1., eval=True):
        if eval:
            self.nms_thresh = 0.3
            self.score_thresh = 0.05
            # self.score_thresh = 0.65
        else:
            self.nms_thresh = 0.3
            self.score_thresh = 0.7

        _, _, H, W = img.shape
        img_size = (H, W)
        device = img.device

        self.eval()
        # with torch.no_grad():
        roi, roi_bbox_pred, roi_cls_scores = self(img, scale=scale)

        mean = torch.Tensor(self.offset_normalize_mean).to(
            device)[None, None, :]
        std = torch.Tensor(self.offset_normalize_std).to(device)[None, None, :]
        roi_bbox_pred = roi_bbox_pred.view(roi_bbox_pred.shape[0], -1, 4)
        roi_bbox_pred = (roi_bbox_pred * std) + mean

        roi = torch.FloatTensor(roi).to(
            device).view(-1, 1, 4).expand_as(roi_bbox_pred)
        pred_bbox = offset2bbox(roi.cpu().numpy().reshape((-1, 4)),
                                roi_bbox_pred.cpu().numpy().reshape((-1, 4)))
        pred_bbox = torch.FloatTensor(pred_bbox).to(device)
        pred_bbox = pred_bbox.view(-1, self.n_class * 4)

        pred_bbox[:, 0::2] = (pred_bbox[:, 0::2]).clamp(min=0, max=img_size[0])
        pred_bbox[:, 1::2] = (pred_bbox[:, 1::2]).clamp(min=0, max=img_size[1])

        prob = F.softmax(roi_cls_scores, dim=1)

        bbox, label, score = self.suppress(pred_bbox, prob)

        self.train()
        return bbox, label, score

    def suppress(self, pred_bbox, prob):
        bbox = list()
        label = list()
        score = list()

        for i in range(1, self.n_class):
            pred_bbox_i = pred_bbox.view(-1, self.n_class, 4)[:, i, :]
            prob_i = prob[:, i]
            mask = (prob_i > self.score_thresh)
            pred_bbox_i = pred_bbox_i[mask, :]
            prob_i = prob_i[mask]
            index_keep = nms(pred_bbox_i, prob_i, self.nms_thresh)
            bbox.append(pred_bbox_i[index_keep].cpu().numpy())
            label.append((i - 1) * np.ones((len(index_keep),)))
            score.append(prob_i[index_keep].cpu().numpy())
        bbox = np.concatenate(bbox, axis=0).astype(np.float32)
        label = np.concatenate(label, axis=0).astype(np.int32)
        score = np.concatenate(score, axis=0).astype(np.float32)

        return bbox, label, score

    def get_optimizer(self):
        self.optimizer = \
            torch.optim.SGD(self.parameters(), lr=config.lr,
                            momentum=0.9, weight_decay=config.weight_decay)
        return self.optimizer

    def scale_lr(self, decay=0.1):
        for param_group in self.optimizer.param_groups:
            param_group['lr'] *= decay
        return self.optimizer


#!/usr/bin/env python3
import os
import numpy as np
import matplotlib.pyplot as plt
import sklearn
import pandas as pd
from pandas import DataFrame as df
from sklearn import svm
from sklearn.model_selection import KFold, cross_val_score
from scipy import stats
import seaborn as sns
from sklearn.linear_model import LogisticRegression,RidgeClassifier
from sklearn.metrics import (confusion_matrix,cohen_kappa_score,recall_score,
                    precision_score)
from sklearn.feature_selection import RFECV
#Set wd
os.chdir("D:\\Documentos\\Essex\\Machine Learning\\assignment")
cwd=os.getcwd()
#read data
data=pd.read_csv("data\\train_imp.csv", header=0)
data2=pd.read_csv("data\\val_imp.csv", header=0)
data3=pd.read_csv("data\\test_imp.csv", header=0)
#print(data.head())

#create feature matrix and feature vectors
#Training set
y=data.iloc[:,-1]
x=data.iloc[:,:-1]
names=list(x.columns)
print("Shape X matrix: ", x.shape)
print("prop: ", y.value_counts()/y.shape[0])

#validation set
y_v=data2.iloc[:,-1]
x_v=data2.iloc[:,:-1]
print("Shape X_v matrix: ", x_v.shape)

#test set
y_t=data3.iloc[:,-1]
x_t=data3.iloc[:,:-1]
print("Shape X_t matrix: ", x_t.shape)

#############################
#Feature selection
#############################
#setting up feature selection algorithm
k_fold = KFold(n_splits=10)
est=svm.SVC(kernel="linear", random_state=21)
selector=RFECV(est,cv=k_fold)
selector.fit(x,y)
#keeping selected variables and printing names for control
x=x.loc[:,selector.get_support()]
x_v=x_v.loc[:,selector.get_support()]
print("Optimal number of features : %d" % selector.n_features_)
print("Support", x.columns)
# Plot number of features VS. cross-validation scores
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score (nb of correct classifications)")
plt.plot(range(1, len(selector.grid_scores_) + 1), selector.grid_scores_)
plt.savefig("plots\\featlog.pdf", bbox_inches='tight')
plt.close()

#############################
#Logistic Regression
#############################
#Setting up algorithm
clfnb=LogisticRegression()

#Fitting and printing cv accuracy
clfnb.fit(x,y)
print("params: ", clfnb.get_params())
score_2 = cross_val_score(clfnb, x, y, cv=k_fold, n_jobs=-1)
print('Average accuracy:', np.mean(score_2))

#Test accuracy and other measures
y_pred=clfnb.predict(x_v)
kappa=cohen_kappa_score(y_v,y_pred)
print("Kappa: ", kappa)
print("Recall: ", recall_score(y_v,y_pred))
print("Precision: ", precision_score(y_v,y_pred))
print("confussion: ", confusion_matrix(y_v,y_pred))
print("Score: ", clfnb.score(x_v,y_v))

#########################
#Predicting Test File
#########################
#Selecting only signigicative features
x_t=x_t.loc[:,selector.get_support()]
#Predicting classes
y_test=clfnb.predict(x_t)

#Saving Results
pd.DataFrame(y_test, columns=["Class"]).to_csv("data\\test_logistic.csv", index=False)
print("test results shape: ", pd.DataFrame(y_test, columns=["Class"]).shape)


from google.cloud import bigquery
import plotly.express as px

dataset_id = "salestest"

class DatasetManager(object):

    def __init__(self, dataset_id):
        self.dataset_id = dataset_id
        self.client = self._get_client()

    def print_listed_projeto(self):
        """INFORMA O NOME DO PROJETO DO SERVICE ACCOUNT"""
        projects = list(self.client.list_projects())
        print("Projects:")
        for project in projects:
            print(project.project_id)

    def data_set(self):
        """INFORMA O NOME DO DATASET DO SERVICE ACCOUNT"""
        datasets = list(self.client.list_datasets())
        print("Datasets:")
        for dataset in datasets:
            print(dataset.dataset_id)

    def tabelas(self):
        """INFORMA O NOME DAS TABELAS DO SERVICE ACCOUNT"""
        dataset_id = "sales_test"
        tables = list(self.client.list_tables(dataset_id))
        print("Tables:")
        for table in tables:
            print(table.table_id)

    def _get_client(self):
        return bigquery.Client.from_service_account_json('data/%s.json' % self.dataset_id)

    def query_dataset(self, query):
        return self.client.query(query).result().to_dataframe()

        # DÁ QUERY NA BIG QUERY DO SERVICE ACCOUNT

def get_vendas_por_marca_u6m(dataset_manager):
    query = f"""SELECT
                brand,
                SUM(value) as vendas_por_marca_u6m

                FROM salestest-373621.sales_test.fact_sales_product_day
                LEFT JOIN salestest-373621.sales_test.dim_product
                ON dim_product.product_id = fact_sales_product_day.product_id

                WHERE date >= DATE_SUB(CURRENT_DATE(), INTERVAL 24 MONTH)

                GROUP BY brand
                ORDER BY vendas_por_marca_u6m DESC
                """
    df = dataset_manager.query_dataset(query)
    return df.head()

def get_vendas_por_marca_por_dia_u6m(dataset_manager):
    projeto = "salestest-373621.sales_test"

    query = f"""SELECT
                brand,
                date,
                SUM(value) as vendas_por_marca_por_dia_u6m

                FROM {projeto}.fact_sales_product_day
                JOIN {projeto}.dim_product
                ON dim_product.product_id = fact_sales_product_day.product_id

                WHERE date >= DATE_SUB(CURRENT_DATE(), INTERVAL 6 MONTH)

                GROUP BY brand, date"""
    df = dataset_manager.query_dataset(query)
    return df

def plotar_vendas(dataset_manager):
    query = f"""SELECT
                brand,
                SUM(value) as vendas_por_marca_u6m

                FROM salestest-373621.sales_test.fact_sales_product_day
                LEFT JOIN salestest-373621.sales_test.dim_product
                ON dim_product.product_id = fact_sales_product_day.product_id

                WHERE date >= DATE_SUB(CURRENT_DATE(), INTERVAL 12 MONTH)

                GROUP BY brand
                ORDER BY vendas_por_marca_u6m DESC
                """
    df = dataset_manager.query_dataset(query)

    fig = px.bar(df,
                 title="VENDAS",
                 x="brand",
                 y="vendas_por_marca_u6m",
                 color="brand",
                 text_auto='.2s'

                 )
    fig.update_layout(paper_bgcolor="white",
                      plot_bgcolor="white",
                      yaxis_title='Faturamento'
                      )
    fig.update_traces(marker_color='darkgreen',
                      marker_line_color='rgb(8,48,107)',
                      marker_line_width=1.5,
                      opacity=0.9,
                      textfont_size=12,
                      textangle=0,
                      textposition="outside",
                      cliponaxis=False)
    return fig.show()

if __name__ == "__main__":
    query_manager = DatasetManager(dataset_id="salestest")
    plotar_vendas(query_manager)


import gymnasium as gym
from gymnasium import error, spaces, utils, Env
from gymnasium.spaces import MultiDiscrete, Box
from gymnasium.utils import seeding
import math
import pymunk
import pygame
from pymunk import pygame_util
screen_width = 1904
screen_height = 960
target = 350
class Robot():
    def __init__(self, space):

        self.tick = 0

        moment = 10
        friction = 0.6

        self.shape = pymunk.Poly.create_box(None, (50, 100))
        body_moment = pymunk.moment_for_poly(moment, self.shape.get_vertices())
        self.body = pymunk.Body(moment, body_moment)

        self.body.position = (200, 350)
        self.shape.body = self.body
        self.shape.color = (150, 150, 150, 0)

        head_moment = pymunk.moment_for_circle(moment, 0, 30)
        self.head_body = pymunk.Body(moment, head_moment)
        self.head_body.position = (self.body.position.x, self.body.position.y+80)
        self.head_shape = pymunk.Circle(self.head_body, 30)
        self.head_shape.friction = friction
        self.head_joint = pymunk.PivotJoint(self.head_body, self.body, (-5, -30), (-5, 50))
        self.head_joint2 = pymunk.PivotJoint(self.head_body, self.body, (5, -30), (5, 50))

        arm_size = (100, 20)
        self.left_arm_upper_shape = pymunk.Poly.create_box(None, arm_size)
        left_arm_upper_moment = pymunk.moment_for_poly(moment, self.left_arm_upper_shape.get_vertices())
        self.left_arm_upper_body = pymunk.Body(moment, left_arm_upper_moment)
        self.left_arm_upper_body.position = (self.body.position.x-70, self.body.position.y+30)
        self.left_arm_upper_shape.body = self.left_arm_upper_body
        self.left_arm_upper_joint = pymunk.PivotJoint(self.left_arm_upper_body, self.body, (arm_size[0] / 2, 0), (-25, 30))
        self.la_motor = pymunk.SimpleMotor(self.body, self.left_arm_upper_body, 0)

        self.right_arm_upper_shape = pymunk.Poly.create_box(None, arm_size)
        right_arm_upper_moment = pymunk.moment_for_poly(moment, self.right_arm_upper_shape.get_vertices())
        self.right_arm_upper_body = pymunk.Body(moment, right_arm_upper_moment)
        self.right_arm_upper_body.position = (self.body.position.x+70, self.body.position.y+30)
        self.right_arm_upper_shape.body = self.right_arm_upper_body
        self.right_arm_upper_joint = pymunk.PivotJoint(self.right_arm_upper_body, self.body, (-arm_size[0] / 2, 0), (25, 30))
        self.ra_motor = pymunk.SimpleMotor(self.body, self.right_arm_upper_body, 0)

        thigh_size = (30, 60)
        self.lu_shape = pymunk.Poly.create_box(None, thigh_size)
        lu_moment = pymunk.moment_for_poly(moment, self.lu_shape.get_vertices())
        self.lu_body = pymunk.Body(moment, lu_moment)
        self.lu_body.position = (self.body.position.x-20, self.body.position.y-75)
        self.lu_shape.body = self.lu_body
        self.lu_shape.friction = friction
        self.lu_joint = pymunk.PivotJoint(self.lu_body, self.body, (0, thigh_size[1] / 2), (-20, -50))
        self.lu_motor = pymunk.SimpleMotor(self.body, self.lu_body, 0)

        self.ru_shape = pymunk.Poly.create_box(None, thigh_size)
        ru_moment = pymunk.moment_for_poly(moment, self.ru_shape.get_vertices())
        self.ru_body = pymunk.Body(moment, ru_moment)
        self.ru_body.position = (self.body.position.x+20, self.body.position.y - 75)
        self.ru_shape.body = self.ru_body
        self.ru_shape.friction = friction
        self.ru_joint = pymunk.PivotJoint(self.ru_body, self.body, (0, thigh_size[1] / 2), (20, -50))
        self.ru_motor = pymunk.SimpleMotor(self.body, self.ru_body, 0)

        leg_size = (20, 70)
        self.ld_shape = pymunk.Poly.create_box(None, leg_size)
        ld_moment = pymunk.moment_for_poly(moment, self.ld_shape.get_vertices())
        self.ld_body = pymunk.Body(moment, ld_moment)
        self.ld_body.position = (self.lu_body.position.x, self.lu_body.position.y - 65)
        self.ld_shape.body = self.ld_body
        self.ld_shape.friction = friction
        self.ld_joint = pymunk.PivotJoint(self.ld_body, self.lu_body, (0, leg_size[1] / 2), (0, -thigh_size[1] / 2))
        self.ld_motor = pymunk.SimpleMotor(self.lu_body, self.ld_body, 0)

        self.rd_shape = pymunk.Poly.create_box(None, leg_size)
        rd_moment = pymunk.moment_for_poly(moment, self.rd_shape.get_vertices())
        self.rd_body = pymunk.Body(moment, rd_moment)
        self.rd_body.position = (self.ru_body.position.x, self.ru_body.position.y - 65)
        self.rd_shape.body = self.rd_body
        self.rd_shape.friction = friction
        self.rd_joint = pymunk.PivotJoint(self.rd_body, self.ru_body, (0, leg_size[1] / 2), (0, -thigh_size[1] / 2))
        self.rd_motor = pymunk.SimpleMotor(self.ru_body, self.rd_body, 0)

        foot_size = (45, 20)
        self.lf_shape = pymunk.Poly.create_box(None, foot_size)
        rd_moment = pymunk.moment_for_poly(moment, self.lf_shape.get_vertices())
        self.lf_body = pymunk.Body(moment, rd_moment)
        self.lf_body.position = (self.ld_body.position.x + foot_size[0]/6, self.ld_body.position.y - (foot_size[1]*2))
        self.lf_shape.body = self.lf_body
        self.lf_shape.friction = friction
        self.lf_shape.elasticity = 0.1
        self.lf_joint = pymunk.PivotJoint(self.ld_body, self.lf_body, (-5, -leg_size[1] / 2), (-foot_size[0]/2 + 10, foot_size[1]/2))
        self.lf_motor = pymunk.SimpleMotor(self.ld_body, self.lf_body, 0)

        self.rf_shape = pymunk.Poly.create_box(None, foot_size)
        rd_moment = pymunk.moment_for_poly(moment, self.rf_shape.get_vertices())
        self.rf_body = pymunk.Body(moment, rd_moment)
        self.rf_body.position = (self.rd_body.position.x + foot_size[0]/6, self.rd_body.position.y - (foot_size[1]*2))
        self.rf_shape.body = self.rf_body
        self.rf_shape.friction = friction
        self.rf_shape.elasticity = 0.1
        self.rf_joint = pymunk.PivotJoint(self.rd_body, self.rf_body, (-5, -leg_size[1] / 2), (-foot_size[0]/2 + 10, foot_size[1]/2))
        self.rf_motor = pymunk.SimpleMotor(self.rd_body, self.rf_body, 0)

        space.add(self.body, self.shape, self.head_body, self.head_shape, self.head_joint, self.head_joint2)
        space.add(self.left_arm_upper_body, self.left_arm_upper_shape, self.left_arm_upper_joint, self.la_motor)
        space.add(self.right_arm_upper_body, self.right_arm_upper_shape, self.right_arm_upper_joint, self.ra_motor)
        space.add(self.lu_body, self.lu_shape, self.lu_joint, self.lu_motor)
        space.add(self.ru_body, self.ru_shape, self.ru_joint, self.ru_motor)
        space.add(self.ld_body, self.ld_shape, self.ld_joint, self.ld_motor)
        space.add(self.rd_body, self.rd_shape, self.rd_joint, self.rd_motor)
        space.add(self.lf_body, self.lf_shape, self.lf_joint, self.lf_motor)
        space.add(self.rf_body, self.rf_shape, self.rf_joint, self.rf_motor)

        shape_filter = pymunk.ShapeFilter(group=1)
        self.shape.filter = shape_filter
        self.head_shape.filter = shape_filter
        self.left_arm_upper_shape.filter = shape_filter
        self.right_arm_upper_shape.filter = shape_filter
        self.lu_shape.filter = shape_filter
        self.ru_shape.filter = shape_filter
        self.ld_shape.filter = shape_filter
        self.rd_shape.filter = shape_filter
        self.lf_shape.filter = shape_filter
        self.rf_shape.filter = shape_filter

        self.lu_flag = False
        self.ld_flag = False
        self.ru_flag = False
        self.rd_flag = False
        self.la_flag = False
        self.ra_flag = False
        self.lf_flag = False
        self.rf_flag = False

    def get_data(self):
        lu = ((360 - math.degrees(self.lu_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        ld = ((360 - math.degrees(self.ld_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        lf = ((360 - math.degrees(self.lf_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        ru = ((360 - math.degrees(self.ru_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        rd = ((360 - math.degrees(self.rd_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        rf = ((360 - math.degrees(self.rf_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        la = ((360 - math.degrees(self.left_arm_upper_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        ra = ((360 - math.degrees(self.right_arm_upper_body.angle)) - (360 - math.degrees(self.body.angle))) / 360.0
        return ru, rd, lu, ld, la, ra, lf, rf
        #removed self.body,angle

    def update(self):
        #lu
        self.lu_flag = False
        if (360 - math.degrees(self.lu_body.angle)) - (360 - math.degrees(self.body.angle)) >= 90 and self.lu_motor.rate > 0:
            self.lu_motor.rate = 0
            self.lu_flag = True
        elif (360 - math.degrees(self.lu_body.angle)) - (360 - math.degrees(self.body.angle)) <= -90 and self.lu_motor.rate < 0:
            self.lu_motor.rate = 0
            self.lu_flag = True

        #ld
        self.ld_flag = False
        if (360 - math.degrees(self.ld_body.angle)) - (360 - math.degrees(self.lu_body.angle)) >= 90 and self.ld_motor.rate > 0:
            self.ld_motor.rate = 0
            self.ld_flag = True
        elif (360 - math.degrees(self.ld_body.angle)) - (360 - math.degrees(self.lu_body.angle)) <= -90 and self.ld_motor.rate < 0:
            self.ld_motor.rate = 0
            self.ld_flag = True

        #ru
        self.ru_flag = False
        if (360 - math.degrees(self.ru_body.angle)) - (360 - math.degrees(self.body.angle)) >= 90 and self.ru_motor.rate > 0:
            self.ru_motor.rate = 0
            self.ru_flag = True
        elif (360 - math.degrees(self.ru_body.angle)) - (360 - math.degrees(self.body.angle)) <= -90 and self.ru_motor.rate < 0:
            self.ru_motor.rate = 0
            self.ru_flag = True

        #rd
        self.rd_flag = False
        if (360 - math.degrees(self.rd_body.angle)) - (360 - math.degrees(self.ru_body.angle)) >= 90 and self.rd_motor.rate > 0:
            self.rd_motor.rate = 0
            self.rd_flag = True
        elif (360 - math.degrees(self.rd_body.angle)) - (360 - math.degrees(self.ru_body.angle)) <= -90 and self.rd_motor.rate < 0:
            self.rd_motor.rate = 0
            self.rd_flag = True

        #lf
        self.lf_flag = False
        if (360 - math.degrees(self.lf_body.angle)) - (360 - math.degrees(self.ld_body.angle)) >= 90 and self.lf_motor.rate > 0:
            self.lf_motor.rate = 0
            self.lf_flag = True
        elif (360 - math.degrees(self.lf_body.angle)) - (360 - math.degrees(self.ld_body.angle)) <= -45 and self.lf_motor.rate < 0:
            self.lf_motor.rate = 0
            self.lf_flag = True

        #rf
        self.rf_flag = False
        if (360 - math.degrees(self.rf_body.angle)) - (360 - math.degrees(self.rd_body.angle)) >= 90 and self.rf_motor.rate > 0:
            self.rf_motor.rate = 0
            self.rf_flag = True
        elif (360 - math.degrees(self.rf_body.angle)) - (360 - math.degrees(self.rd_body.angle)) <= -45 and self.rf_motor.rate < 0:
            self.rf_motor.rate = 0
            self.rf_flag = True

    def add_land(self,space):
        body = pymunk.Body(body_type=pymunk.Body.STATIC)
        body.position = (0, 100)
        land = pymunk.Segment(body, (0, 50), (99999, 50), 10)
        land.friction = 1.0
        land.elasticity = 0.1
        space.add(body, land)

        body_2 = pymunk.Body(body_type=pymunk.Body.STATIC)
        body_2.position = (target, -50)
        t_block = pymunk.Segment(body_2, (0, 100), (20, 100), 10)
        space.add(body_2, t_block)

class Walker(Env):
    metadata = {'render.modes': ['human']}

    def __init__(self):
        self.action_space = MultiDiscrete([3]*8)
        self.observation_space = Box(-20,20,[8])
        self.viewer = None
        self.last_horizontal_pos = 0
        self.last_vertical_pos = 0
        self.step_nr = 0
        self.max_step = 900

    def check_fall(self):

      if self.robot.body.position[1] < self.initial_height-50:
            return True
      if self.robot.body.position[0] < 0 or self.robot.body.position[0] > screen_width:
            return True
      return False
    def calculate_reward(self):
        shape = self.space.shapes[-2]
        contact_lf = len(self.robot.lf_shape.shapes_collide(b=shape).points)
        contact_rf = len(self.robot.rf_shape.shapes_collide(b=shape).points)
        if (self.robot.body.position[0] - self.last_horizontal_pos) > 1:
            reward = 10
        elif 1 > (self.robot.body.position[0] - self.last_horizontal_pos) > -1:
            reward = 9
        elif (self.robot.body.position[0] - self.last_horizontal_pos) < -1:
            reward = 8
        if not contact_lf and not contact_rf:
                reward -= 6
        return reward

    def check_complete(self):
        if self.robot.body.position[0] >= target: # 500 is the position of the target
            return True

    def step(self, actions):
        self.step_nr += 1
        actions = [(a-1)*2 for a in actions]
        self.robot.ru_motor.rate = actions[0]
        self.robot.rd_motor.rate = actions[1]
        self.robot.lu_motor.rate = actions[2]
        self.robot.ld_motor.rate = actions[3]
        self.robot.la_motor.rate = actions[4]
        self.robot.ra_motor.rate = actions[5]
        self.robot.lf_motor.rate = actions[6]
        self.robot.rf_motor.rate = actions[7]

        self.robot.update()
        self.space.step(1/50)

        done = False
        reward = self.calculate_reward()

        if self.check_fall():
            done = True
            reward = 0
        if self.check_complete():
            done = True
            reward = 16
        if self.step_nr >= self.max_step:
            done = True

        info = {}
        observation = self.robot.get_data()

        self.last_horizontal_pos = self.robot.body.position[0]
        self.last_vertical_pos = self.robot.body.position[1]

        truncated = False

        return(
        observation,
        reward,
        done,
        truncated,
        info)

    def render(self, mode='human', close=False):
        if self.viewer is None:
            self.viewer = pygame.init()
            pygame_util.positive_y_is_up = True
            self.clock = pygame.time.Clock()
            self.screen = pygame.display.set_mode((screen_width, screen_height))
        self.draw_options = pygame_util.DrawOptions(self.screen)
        self.screen.fill((255, 255, 255))
        self.space.debug_draw(self.draw_options)
        pygame.display.flip()
        self.clock.tick(25)
        return pygame.surfarray.array3d(self.screen)

    def reset(self):
        self.step_nr = 0
        self.space = pymunk.Space()
        self.space.gravity = (0.0, -990)
        self.robot = Robot(self.space)
        self.robot.add_land(self.space)
        self.initial_height = self.robot.body.position[1]
        self.initial_horizontal = self.robot.body.position[0]
        observation = self.robot.get_data()
        return(observation,None)


# Example test given in the question
n = 3
p = [90, 80, 40]
x = 1000

def f(n: int):
  if n <= 400:
    return 1
  else:
    return 20

def Sell_Stocks(days, stocks, p, f):
  opt = [[0 for i in range(stocks + 1)] for i in range(days + 1)] # opt table
  s = [[0 for i in range(stocks + 1)] for i in range(days + 1)] # price drop table
  a = [[0 for i in range(stocks + 1)] for i in range(days + 1)] # stocks sold for each entry

  for i in range(days + 1):
    if i == 0: # use day 0 as all 0's for the algo
      continue

    # iterate through every number of stocks to have sold by day i
    for j in range(stocks + 1):
      max_revenue = 0
      drop = 0
      sold = 0
      # get max income for each stock value j to have sold by day i
      for k in range(j + 1):
        revenue = opt[i - 1][j - k] + k * (p[i - 1] - s[i - 1][j - k] - f(k))
        if revenue > max_revenue:
          max_revenue = revenue
          drop = s[i - 1][j - k] + f(k)
          sold = k

      opt[i][j] = max_revenue
      s[i][j] = drop
      a[i][j] = sold

  # backtracking
  indices = [stocks]
  sold = []
  for d in range(days):
    sold.append(a[days - d][indices[d]])
    indices.append(indices[d] - sold[d])

  sold.reverse() # ans is in reverse order
  return sold

print(Sell_Stocks(n, x, p, f))


# %% markdown
## Experiment 3 Trials
# %%
import numpy as np
import fire
import random
import pandas as pd
import json
from itertools import product
from markdown import markdown
import textwrap
from copy import deepcopy
import os, sys, json, pprint
from vgc_project.gridutils import transformations, getFeatureXYs

# %% codecell
def main(BASEGRIDS_FILENAME, EXP_NAME, EXP_CONFIG_FILE):
# %% codecell
    basegrids = json.load(open(BASEGRIDS_FILENAME, 'r'))
    sharedparams = {
        "feature_colors": {
            "#": "black",
            "G": "yellow",
            "S": "white",
            ".": "white",
            **{i: "mediumblue" for i in "0123456"}
        },
        "wall_features": ["#", ] + list("0123456"),
        "show_rewards": False,
    }

    nRounds = 4*2
    roundBonusCents = 15
    INITIALGOAL_COUNTDOWN_SEC = 60000
    EXPERIMENTVERSION = "1.7c"
    emptygrid = [
        "............G",
        ".............",
        ".............",
        "......#......",
        "......#......",
        "......#......",
        "...#######...",
        "......#......",
        "......#......",
        "......#......",
        ".............",
        ".............",
        "S............"
    ]
    instructionstraining = [
        {
            "type": "CustomInstructions",
            "instructions": markdown(textwrap.dedent("""
                # Instructions
                Thank you for participating in our experiment!

                You will play a game where you control a blue circle on a grid. You can move up, down, left, or right by pressing the __arrow keys__⬆⬇⬅➡.

                 The Yellow tile with the green square is the goal 👀.

                The green square will shrink when you stand still. It will initially shrink slowly, and then shrink quickly once you start moving. Keep moving!

                __Black__ tiles are walls that you cannot pass through ⛔.

                Blue tiles are obstacles that might change
                between different rounds. You cannot pass through these either 🚫.
            """)),
            "timing_post_trial": 1000,
            "continue_wait_time": 5000,
        },
        {
            "type": "GridNavigation",
            "round": 0,
            "roundtype": "practice",
            "bonus": False,
            "message": """Get to the Yellow goal. You cannot go through
Black or
Blue tiles.""",
            "taskparams": {
                "feature_array": [
                    "G.........000",
                    "............0",
                    ".............",
                    "......#......",
                    "......#......",
                    "......#......",
                    "...#######...",
                    "......#......",
                    "......#......",
                    "......#......",
                    ".............",
                    "0............",
                    "000.........."
                ],
                "init_state":[12, 0],
                "absorbing_states":[[0, 12]],
                "name": "trainingA",
                **sharedparams
            },
            "emptygrid": emptygrid,
            "goalCountdown": True,
            "participantStarts": False,
            "INITIALGOAL_COUNTDOWN_SEC": INITIALGOAL_COUNTDOWN_SEC,
            "TILE_SIZE": 40
        },
        {
            "type": "GridNavigation",
            "round": 1,
            "roundtype": "practice",
            "bonus": False,
            "message": """Get to the Yellow goal. You cannot go through
Black or
Blue tiles.""",
            "taskparams": {
            "feature_array": [
                    "G............",
                    ".............",
                    ".............",
                    "......#......",
                    "......#......",
                    "......#...0..",
                    ".00#######000",
                    ".0....#......",
                    ".0....#......",
                    "......#......",
                    ".............",
                    ".............",
                    "............."
                ],
                "init_state":[12, 0],
                "absorbing_states":[[0, 12]],
                "name": "trainingB",
                **sharedparams
            },
            "emptygrid": emptygrid,
            "goalCountdown": True,
            "participantStarts": False,
            "INITIALGOAL_COUNTDOWN_SEC": INITIALGOAL_COUNTDOWN_SEC,
            "TILE_SIZE": 40
        },
        {
            "type": "CustomInstructions",
            "instructions": markdown(textwrap.dedent("""
                # Instructions
                Great! Now, you will be given similar grids, however, after your first move the
                Blue tiles
                will become invisible.
            """)),
            "timing_post_trial": 1000,
            "continue_wait_time": 2000,
        },
        {
            "type": "GridNavigation",
            "round": 2,
            "roundtype": "practice",
            "bonus": False,
            "message": """The Blue tiles turn invisible.""",
            "taskparams": {
                "feature_array": [
                    "G.........000",
                    "............0",
                    ".............",
                    "......#......",
                    "......#......",
                    "......#......",
                    "...#######...",
                    "......#......",
                    "......#......",
                    "......#......",
                    ".............",
                    "0............",
                    "000.........."
                ],
                "init_state":[12, 0],
                "absorbing_states":[[0, 12]],
                "name": "trainingA",
                **sharedparams,
                "TILE_SIZE": 40
            },
            "emptygrid": emptygrid,
            "goalCountdown": True,
            "participantStarts": False,
            "hideObstaclesOnMove": True,
            "INITIALGOAL_COUNTDOWN_SEC": INITIALGOAL_COUNTDOWN_SEC,
            "TILE_SIZE": 40
        },
        {
            "type": "GridNavigation",
            "round": 3,
            "roundtype": "practice",
            "bonus": False,
            "message": """The Blue tiles turn invisible.""",
            "taskparams": {
            "feature_array": [
                    "G............",
                    ".............",
                    ".............",
                    "......#......",
                    "......#......",
                    "......#...0..",
                    ".00#######000",
                    ".0....#......",
                    ".0....#......",
                    "......#......",
                    ".............",
                    ".............",
                    "............."
                ],
                "init_state":[12, 0],
                "absorbing_states":[[0, 12]],
                "name": "trainingB",
                **sharedparams
            },
            "emptygrid": emptygrid,
            "goalCountdown": True,
            "participantStarts": False,
            "hideObstaclesOnMove": True,
            "INITIALGOAL_COUNTDOWN_SEC": INITIALGOAL_COUNTDOWN_SEC,
            "TILE_SIZE": 40
        },
        {
            "type": "CustomInstructions",
            "instructions": markdown(textwrap.dedent(f"""
                # Instructions
                Great!

                Next, we will give you a series of {nRounds} rounds. For each round, you will receive a
                bonus of {roundBonusCents} cents but only if you reach the goal
                without the green square disappearing.

                You can win a total bonus of up to ${nRounds*roundBonusCents/100:.2f}.

                At the start of each round, we will show you a grid showing only the walls (black).
                When you are ready to begin the round, press the __spacebar__.
                The obstacles (blue),
                your start location, and goal will appear.

                Remember, once you move, the blue obstacles will turn invisible!
            """)),
            "timing_post_trial": 1000,
            "continue_wait_time": 5000,
        }
    ]
    location_instructions = [
        {
            "type": "CustomInstructions",
            "instructions": markdown(textwrap.dedent("""
                # ☝ Please note the following

                To transition between the rounds as smoothly as possible, we recommend using
                one hand to press the spacebar and the other use the arrow keys 🙌.

                Try to go as quickly and carefully as possible 💫.

                In addition, we are interested in your thought process while navigating each maze 🤔.
                Following each trial, we will ask you about where one of the obstacles was originally placed.
                You will be shown two possible locations, and asked where it was in the maze you just did.

                **Your answers to these questions will not affect your bonus, but please try to respond accurately.**.

                Thanks again for participating in our experiment!

            """)),
            "timing_post_trial": 1000,
            "continue_wait_time": 5000,
        },
        {
            "type": 'survey-multi-choice',
            "questions": [
                {
                  "prompt": "If the green square disappears, you will not receive a bonus on that round:",
                  "options": ["True", "False"],
                  "required": True,
                  "name": "navCheck"
                },
                {
                  "prompt": "How much of a bonus will you receive for completing each maze before the green square disappears?",
                  "options": ["25 cents", "15 cents", "12 cents", "None"],
                  "required": True,
                  "name": "navBonusCheck"
                },
                {
                  "prompt": "How much of a bonus will you receive for answering the questions about what you paid attention to?",
                  "options": ["25 cents", "15 cents", "12 cents", "None"],
                  "required": True,
                  "name": "memoryBonusCheck"
                }
            ]
        }
    ]
    awareness_instructions = [
        {
            "type": "CustomInstructions",
            "instructions": markdown(textwrap.dedent("""
                # ☝ Please note the following

                To transition between the rounds as smoothly as possible, we recommend using
                one hand to press the spacebar and the other use the arrow keys 🙌.

                Try to go as quickly and carefully as possible 💫.

                In addition, we are interested in your thought process while navigating each maze 🤔.
                Following each trial, we will ask you how aware of each obstacle you were at any point.
                Your answer should reflect the amount you paid attention to an obstacle, whether it was
                at the beginning or end of navigating the maze.

                **Your answers to these questions will not affect your bonus**.

                Finally, the maze rounds will sometimes end randomly before you reach the goal.
                As long as the green square has not disappeared, you will receive your bonus on that round,
                but we will still ask you questions about your thought process.

                Thanks again for participating in our experiment!

            """)),
            "timing_post_trial": 1000,
            "continue_wait_time": 5000,
        },
        {
            "type": 'survey-multi-choice',
            "questions": [
                {
                  "prompt": "If the green square disappears, you will not receive a bonus on that round:",
                  "options": ["True", "False"],
                  "required": True,
                  "name": "navCheck"
                },
                {
                  "prompt": "How much of a bonus will you receive for completing each maze before the green square disappears?",
                  "options": ["25 cents", "15 cents", "12 cents", "None"],
                  "required": True,
                  "name": "navBonusCheck"
                },
                {
                  "prompt": "How much of a bonus will you receive for answering the questions about what you paid attention to?",
                  "options": ["25 cents", "15 cents", "12 cents", "None"],
                  "required": True,
                  "name": "memoryBonusCheck"
                }
            ]
        }
    ]

# %% codecell
    posttask = [
          {
            "type": 'survey-text',
            "questions": [
              {
                  "prompt": "Please describe your process for answering the questions.",
                  "rows": 5,
                  "columns":50,
                  "required": True,
                  "name": "howAttention"
              },
              {
                  "prompt": "Any general comments?",
                  "rows": 5,
                  "columns":50,
                  "required": True,
                  "name": "generalComments"
              },
            ],
          },
          {
            "type": 'survey-likert',
            "questions": [
                {
                    "prompt": "How often do you play video games?",
                    "labels": ["Never", "Every few months", "Monthly", "Weekly", "Daily"],
                    "required": True,
                    "name": "videogames"
                }
            ]
          },
          {
            "type": 'survey-text',
            "questions": [
              {"prompt": "Age", "required": True, "name": "age"},
              {"prompt": "Gender", "required": True, "name": "gender"},
            ],
          }
    ]
# %% codecell
    # ## Generate main trials
    expgrids = {}

    #terminate after 2 steps
    earlyTermDist = [[-2, 0], [2, 0], [0, -2], [0, 2], [1, 1], [-1, 1], [1, -1], [-1, -1]]

    for gridname, trialArray in basegrids.items():
        for tname, trans in transformations.items():
            for earlyTerm in ['full', 'earlyterm']:
                tgridname = f"{gridname}-{tname}-{earlyTerm}"
                transformed = trans(trialArray)
                if earlyTerm == 'full':
                    expgrids[tgridname] = {
                        "feature_array": transformed,
                        "init_state": getFeatureXYs(transformed, "S")[0],
                        "absorbing_states": getFeatureXYs(transformed, "G"),
                        "name": tgridname,
                        **sharedparams
                    }
                elif earlyTerm == 'earlyterm':
                    s0 = getFeatureXYs(transformed, "S")[0]
                    adjToS0 = [[s0[0] + dx, s0[1] + dy] for dx, dy in earlyTermDist]
                    adjToS0 = [s for s in adjToS0 if (s[0] >= 0) and (s[1] >= 0)]
                    expgrids[tgridname] = {
                        "feature_array": transformed,
                        "init_state": s0,
                        "absorbing_states": getFeatureXYs(transformed, "G") + adjToS0,
                        "name": tgridname,
                        **sharedparams
                    }

# %% codecell
    # ### Trial orders
    from functools import reduce

    YELLOW = "#DCCB5D"
    GREEN = "#44A9A0"

    def generateTrialParameters(basegrids, seed, reverse=False, flipEarlyTerm=False, probetype="awareness"):
        random.seed(seed)
        translations = ['base', 'vflip', 'hflip', 'trans']
        truemod_colors = [(GREEN, YELLOW), (YELLOW, GREEN)]
        grididx = [12, 13, 14, 15]

        # at the nav-trial level, randomly assign translations to the grids in 2 blocks
        nblocks = 2
        navtrialparams = []
        for blocki in range(nblocks):
            btrans = deepcopy(translations)
            random.shuffle(btrans)
            bgrids = deepcopy(grididx)
            random.shuffle(bgrids)
            navtrialparams.append([(blocki, gidx, trans) for gidx, trans in zip(bgrids, btrans)])

        # at the probe-trial level, randomly but evenly assign true/mod to each obstacle in
        #the first nav-trial block, then do the opposite in the second nav-trial block.
        # shuffle the probe level trials within each grid block
        probetrials = {}
        firstblock = navtrialparams[0]
        for blocki, gidx, trans in firstblock:
            assert blocki == 0
            probes = ['0', '1', '2', '3', '4']
            probe_colororder = [[(p, corder) for corder in truemod_colors] for p in probes]
            for pcolors in probe_colororder:
                random.shuffle(pcolors)
            probecolors0, probecolors1 = [list(pm) for pm in zip(*probe_colororder)]
            random.shuffle(probecolors0)
            random.shuffle(probecolors1)
            probetrials[(0, gidx)] = probecolors0
            probetrials[(1, gidx)] = probecolors1

        # flatten the blocks
        navtrialparams = sum(navtrialparams, [])
        if reverse:
            navtrialparams = navtrialparams[::-1]

        emptygrid = [
            "............G",
            ".............",
            ".............",
            "......#......",
            "......#......",
            "......#......",
            "...#######...",
            "......#......",
            "......#......",
            "......#......",
            ".............",
            ".............",
            "S............"
        ]
        trialparams = []
        pi = 0
        for ri, (bi, gidx, trans) in enumerate(navtrialparams):
            navgridname = f"gridB-{gidx}-0-{trans}-full"
            #create navigation trial
            trialparams.append({
                "type": "GridNavigation",
                "page": pi,
                "round": ri,
                "roundtype": "navigation",
                "bonus": True,
                "goalCountdown": True,
                "hideObstaclesOnMove": True,
                "message": "",
                "taskparams": expgrids[navgridname],
                "emptygrid": emptygrid,
                "navgridname": navgridname,
                "INITIALGOAL_COUNTDOWN_SEC": INITIALGOAL_COUNTDOWN_SEC,
                "TILE_SIZE": 40
            })
            pi += 1

            if probetype == "location":
                #create maze-obstacle memory trials
                probeparams = probetrials[(bi, gidx)]
                for probeidx, (probeobs, colororder) in enumerate(probeparams):
                    num2alpha = dict(zip("01234", "ABCDE"))

                    probegridname = f"gridB-{gidx}-M-{trans}-full"
                    probegrid = deepcopy(expgrids[probegridname])
                    probeobs = str(probeobs)
                    obs_colors = { #color order is (true, mod)
                        probeobs: colororder[0],
                        num2alpha[probeobs]: colororder[1]
                    }
                    fc = {"#": 'black', **obs_colors}
                    probegrid['feature_colors'] = fc

                    #2afc
                    trialparams.append({
                        #plugin parameters
                        "type": "CustomItem",
                        "questiontext": "An obstacle was either in the yellow or green location (not both), which one was it?",
                        "responseLabels": ["Yellow", "?", "Green"],
                        "validResponses": ["Yellow", "Green"],
                        "initResponse": "?",
                        "responseEndLabels": ["", ""],
                        "stimuli": [{
                            "type": "gridworld",
                            "gridworldparams": probegrid,
                            "TILE_SIZE": 25
                        }],
                        "dontSave": ["stimuli", ],

                        #other information to save
                        "roundtype": "probe_2afc",
                        "page": pi,
                        "round": ri,
                        "queryround": probeidx, #the n-th asked about this round
                        "navgridname": navgridname,
                        "probegridname": probegridname,
                        "true_color": colororder[0],
                        "mod_color": colororder[1],
                        "probeobs": probeobs,
                    })
                    pi += 1

                    #confidence
                    trialparams.append({
                        #plugin parametersr
                        "type": "CustomItem",
                        "questiontext": "How confident are you?",
                        "responseLabels": [1, 2, 3, 4, "?", 5, 6, 7, 8],
                        "validResponses": [1, 2, 3, 4, 5, 6, 7, 8],
                        "initResponse": "?",
                        "responseEndLabels": ["I guessed", "I'm certain"],
                        "stimuli": [{
                            "type": "gridworld",
                            "gridworldparams": probegrid,
                            "TILE_SIZE": 25
                        }],
                        "dontSave": ["stimuli", ],

                        #other information to save
                        "roundtype": "probe_conf",
                        "page": pi,
                        "round": ri,
                        "queryround": probeidx, #the n-th asked about this round
                        "navgridname": navgridname,
                        "probegridname": probegridname,
                        "true_color": colororder[0],
                        "mod_color": colororder[1],
                        "probeobs": probeobs,
                    })
                    pi += 1
            elif probetype == "awareness":
                probeparams = probetrials[(bi, gidx)]
                probeorder = [probeobs for probeobs, _ in probeparams]
                #create maze-obstacle attention trials
                for probeidx, probeobs in enumerate(probeorder):
                    probegrid = deepcopy(expgrids[navgridname])
                    probegrid['feature_colors'][probeobs] = '#48D1CC' #MediumTurquoise
                    trialparams.append({
                        "type": "GridBlockAttentionQuery",
                        "page": pi,
                        "round": ri,
                        "roundtype": "attentionquery",
                        "queryround": probeidx, #the n-th asked about this round
                        "probegridparams": probegrid,
                        "navgridname": navgridname,
                        "probeobs": probeobs,
                        "questiontext": "How aware of the highlighted obstacle were you at any point?"
                    })
                    pi += 1
            else:
                assert False, "unknown probetype"
        return trialparams

# %% codecell
    #note, there are 8 seeds, so 8 * 2 * 2 = 32 conditions
    seeds = [23199, 27190, 31210, 31290, 31993, 61993, 63993, 67993]
    timelines = []
    for seed, reverse, probetype in product(seeds, [True, False], ['awareness', 'location']):
        maintrials = generateTrialParameters(basegrids, seed=seed, reverse=reverse, probetype=probetype)
        if probetype == "awareness":
            pretask = instructionstraining + awareness_instructions
        elif probetype == 'location':
            pretask = instructionstraining + location_instructions
        timelines.append(pretask+maintrials+posttask)

# %% codecell
    params = {
        "nRounds": nRounds,
        "roundBonusCents": roundBonusCents,
        "EXP_NAME": EXP_NAME
    }
    experiment = {"timelines": timelines, "params": params}
    json.dump(
        experiment,
        open(EXP_CONFIG_FILE, "w"),
        sort_keys=True, indent=4
    )

# %%
if __name__ == "__main__":
    fire.Fire(main)


import logging
import os
import pwd
import sys

from aiohttp import web
from aiomisc.utils import bind_socket
from configargparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from setproctitle import setproctitle
from yarl import URL

from megamarket.api.app import create_app
from megamarket.utils.argparse import positive_int
from megamarket.utils.pg import DEFAULT_PG_URL

ENV_VAR_PREFIX = 'MEGAMARKET_'

logging.basicConfig(level=logging.DEBUG)

parser = ArgumentParser(
    auto_env_var_prefix=ENV_VAR_PREFIX,
    formatter_class=ArgumentDefaultsHelpFormatter
)

parser.add_argument('--user', required=False, type=pwd.getpwnam,
                    help='Changes UID')

group = parser.add_argument_group('API Options')
group.add_argument('--api-address', default='0.0.0.0',
                   help='IPv4/IPv6 address API server should listen on')
group.add_argument('--api-port', type=positive_int, default=8081,
                   help='TCP port API server should listen on')

group = parser.add_argument_group('PostgreSQL Options')
group.add_argument('--pg-url', type=URL, default=URL(DEFAULT_PG_URL),
                   help='URL to use to connect to the database')

group = parser.add_argument_group('Logging Options')
group.add_argument('--log-level', default='INFO',
                   choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])

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

    # clear_environ(lambda k: k.startswith(ENV_VAR_PREFIX))

    logging.basicConfig(level=args.log_level, format='color')

    sock = bind_socket(address=args.api_address, port=args.api_port,
                       proto_name='http')

    if args.user is not None:
        logging.info('Changing user to %r', args.user.pw_name)
        os.setgid(args.user.pw_gid)
        os.setuid(args.user.pw_uid)

    setproctitle(os.path.basename(sys.argv[0]))

    app = create_app(args)
    web.run_app(app, sock=sock)

if __name__ == '__main__':
    main()


from __future__ import unicode_literals, absolute_import, print_function, division

import datetime
import time

from sopel.module import commands, rule, priority, thread
from sopel.tools import Identifier
from sopel.tools.time import seconds_to_human

@commands('seen')
def seen(bot, trigger):
    """Reports when and where the user was last seen."""
    if not trigger.group(2):
        bot.say(".seen <nick> - Reports when <nick> was last seen.")
        return
    nick = trigger.group(2).strip()
    if nick == bot.nick:
        bot.reply("I'm right here!")
        return
    timestamp = bot.db.get_nick_value(nick, 'seen_timestamp')
    if timestamp:
        channel = bot.db.get_nick_value(nick, 'seen_channel')
        message = bot.db.get_nick_value(nick, 'seen_message')
        action = bot.db.get_nick_value(nick, 'seen_action')

        saw = datetime.datetime.utcfromtimestamp(timestamp)
        delta = seconds_to_human((trigger.time - saw).total_seconds())

        msg = "I last saw " + nick
        if Identifier(channel) == trigger.sender:
            if action:
                msg += " in here {since}, doing: {nick} {action}".format(
                    since=delta,
                    nick=nick,
                    action=message)
            else:
                msg += " in here {since}, saying: {message}".format(
                    since=delta,
                    message=message)
        else:
            msg += " in another channel {since}.".format(since=delta)
        bot.reply(msg)
    else:
        bot.say("Sorry, I haven't seen {nick} around.".format(nick=nick))

@thread(False)
@rule('(.*)')
@priority('low')
def note(bot, trigger):
    if not trigger.is_privmsg:
        bot.db.set_nick_value(trigger.nick, 'seen_timestamp', time.time())
        bot.db.set_nick_value(trigger.nick, 'seen_channel', trigger.sender)
        bot.db.set_nick_value(trigger.nick, 'seen_message', trigger)
        bot.db.set_nick_value(trigger.nick, 'seen_action', 'intent' in trigger.tags)


#works but need to find out how to add sound

import datetime
from playsound import playsound
alarmhour = int(input("Enter Hour: "))
alarmins = int(input("Enter Minutes: "))
alarmAm = input("AM / PM: ").upper()

if alarmAm == "pm".upper():
    alarmhour += 12

while True:
    if alarmhour == datetime.datetime.now().hour and alarmins == datetime.datetime.now().minute:
        playsound('/Users/chnguyen/Downloads/dsa.mp3')
        print("It's time mother f'er")
        break


#! /usr/bin/env/python3

import sys

rawdata = sys.stdin.read().split('\n')
ID, dna = [], []

i = 0
while i < len(rawdata):
    if rawdata[i] == '':
        break
    if rawdata[i][0] == '>':
        ID.append(rawdata[i][1:])
        dna.append("")
    else:
        dna[-1] += rawdata[i]
    i += 1

gc = [100*(i.count('G')+i.count('C'))/len(i) for i in dna]
index = gc.index(max(gc))

print(ID[index]+'\n'+str(round(gc[index], 4)))