from twitchio.ext.commands import command
# from bs4 import BeautifulSoup
import requests
from data.db.codes import lastfm_api_key

class music():
    def __init__(self, bot):
        self.bot = bot
        self.request_url = f"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={'enoootuuuk'}&api_key={lastfm_api_key}&format=json&limit=2"

    @command(aliases=['song', 'music', 'песня', 'currentsong'])
    async def cmd_song(self, ctx, mention = ''):
        with requests.get(self.request_url, headers = {}) as response:
            songs = response.json()
            if '@attr' in songs['recenttracks']['track'][0]:
                print(songs["recenttracks"]["track"])
                await ctx.channel.send(f'{self.bot.mention(mention)}Сейчас играет: {songs["recenttracks"]["track"][0]["artist"]["#text"]}'
                                       f' - {songs["recenttracks"]["track"][0]["name"]}')
            else:
                await ctx.channel.send(f'{self.bot.mention(mention)}Сейчас не играет музыка из плейлиста Енотика!')

    @command(aliases=['last', 'lastsong', 'ls', 'последняяпесня', 'предыдущаяпесня'])
    async def cmd_lastsong(self, ctx, mention = ''):
        with requests.get(self.request_url, headers = {}) as response:
            songs = response.json()
            if '@attr' in songs['recenttracks']['track'][0]:
                await ctx.channel.send(f'{self.bot.mention(mention)}Прошлая песня: {songs["recenttracks"]["track"][1]["artist"]["#text"]}'
                                       f' - {songs["recenttracks"]["track"][1]["name"]}')
            else:
                await ctx.channel.send(f'{self.bot.mention(mention)}Прошлая песня: {songs["recenttracks"]["track"][0]["artist"]["#text"]}'
                                       f' - {songs["recenttracks"]["track"][0]["name"]}')

    # @command(aliases=['song', 'music', 'песня'])
    # async def cmd_song(self, ctx, mention = ''):
    #     playlist_url = "https://www.last.fm/ru/user/enoootuuuk"
    #     song = self.get_song(self.get_html(playlist_url), playlist_url)
    #     full_song = f'{song["artist"]} - {song["song_name"]}'
    #     await ctx.channel.send(f'{self.bot.mention(mention)}Сейчас играет: {full_song}')
    #
    #
    # def get_html(self, url):
    #     r = requests.get(url)
    #     return r.text
    #
    #
    # def get_song(self, html, url):
    #     soup = BeautifulSoup(html, 'html.parser')
    #
    #     try:
    #         artist  = soup.find('tr', class_ = 'chartlist-row--now-scrobbling').find(class_ = 'chartlist-artist').find('a').text.strip()
    #
    #     except:
    #         artist = None
    #     try:
    #         song_name = soup.find('tr', class_ = 'chartlist-row--now-scrobbling').find(class_ = 'chartlist-name').find('a').text.strip()
    #
    #     except:
    #         song_name = None
    #
    #     song = {'artist' : artist,
    #             'song_name' : song_name}
    #
    #     return song

    async def event_ready(self):
        if not self.bot.ready:
            self.bot.cogs_ready.ready_up("music")

def prepare(bot):
    bot.add_cog(music(bot))


# -*- coding: utf-8 -*-
import maya.cmds as mc

def assign_lambert():
    """
    export all the meshes without shader, for the rig publish
    :return:
    """
    all_meshes = mc.ls(type="mesh", long=1)
    default_sg = "initialShadingGroup"
    mc.sets(all_meshes, fe=default_sg)


from functools import partial
import jax
import jax.numpy as jnp
from sklearn.base import ClusterMixin, BaseEstimator
from sklearn.utils.validation import check_is_fitted
from .utils import cosine_similarity

@jax.jit
def _vector_quantize(points, codebook):
    """
    Quantize a set of given points according to a set of given codebooks

    Parameters
    ----------
    points : float[N, M]
        Points to be quantize (N points, M dimensions)
    prototypes : float[Np, M]
        Codebook prototypes (N prototypes, M dimensions)

    Returns
    -------
    assignment : int[N]
        The corresponding codebook for each point
    distns : float[N]
        The corresponding distance between each point and its codebook
    """
    dist_matrix = 1 - cosine_similarity(points, codebook, norm_axis=1)
    assignment = jnp.argmin(dist_matrix, axis=1)
    distns = dist_matrix[jnp.arange(len(assignment)), assignment]

    return assignment, distns

@partial(jax.jit, static_argnums=(2,))
def _kmeans_run(key, points, k, thresh=1e-5):
    def improve_centroids(val):
        prev_centroids, prev_distn, _ = val
        assignment, distortions = _vector_quantize(points, prev_centroids)

        # Count number of points assigned per centroid
        counts = (
            (assignment[jnp.newaxis, :] == jnp.arange(k)[:, jnp.newaxis])
            .sum(axis=1, keepdims=True)
            .clip(a_min=1.)  # clip to change 0/0 later to 0/1
        )

        # Sum over points in a centroid by zeroing others out
        new_centroids = jnp.sum(
            jnp.where(
                assignment[:, jnp.newaxis, jnp.newaxis] \
                    == jnp.arange(k)[jnp.newaxis, :, jnp.newaxis],
                points[:, jnp.newaxis, :],
                0.,
            ),
            axis=0,
        ) / counts

        new_centroids /= counts

        return new_centroids, jnp.mean(distortions), prev_distn

    initial_indices = jax.random.permutation(key, jnp.arange(points.shape[0]))[:k]
    initial_val = improve_centroids((points[initial_indices, :], jnp.inf, None))

    centroids, distortion, _ = jax.lax.while_loop(
        lambda val: (val[2] - val[1]) > thresh,
        improve_centroids,
        initial_val,
    )
    return centroids, distortion

@partial(jax.jit, static_argnums=(2, 3))
def _kmeans(key, points, k, restarts, thresh):
    all_centroids, all_distortions = jax.vmap(
        lambda key: _kmeans_run(key, points, k, thresh)
    )(jax.random.split(key, restarts))
    i = jnp.argmin(all_distortions)
    return all_centroids[i], all_distortions[i]

class KMeans(ClusterMixin, BaseEstimator):
    def __init__(self, n_clusters=8, tol=1e-5, random_state=42):
        self.n_clusters = n_clusters
        self._tol = tol
        self._key = jax.random.PRNGKey(random_state)

    def fit(self, X):
        self.codebook, _ = _kmeans(self._key, X,
                                   self.n_clusters,
                                   1, thresh=self._tol)
        return self

    def fit_predict(self, x):
        return self.fit(x).predict(x)

    def predict(self, X):
        assignment, _ = _vector_quantize(X, self.codebook)
        return assignment

class KMeansPlusPlus(ClusterMixin, BaseEstimator):
    def __init__(self, n_clusters=8, num_seeds=10,
                 tol=1e-5, random_state=42):
        self.n_clusters = n_clusters
        self.num_seeds = num_seeds
        self._tol = tol
        self._key = jax.random.PRNGKey(random_state)

    def fit(self, X):
        self.codebook, _ = _kmeans(self._key, X, self.n_clusters,
                                   self.num_seeds, thresh=self._tol)
        return self

    def fit_predict(self, x):
        return self.fit(x).predict(x)

    def predict(self, X):
        assignment, _ = _vector_quantize(X, self.codebook)
        return assignment


from django.urls import path
from . import views

app_name = 'accounts'

urlpatterns = [
    path('signup/', views.signup, name='signup'),
    path('login/', views.login, name='login'),
    path('logout/', views.logout, name='logout'),
    path('account_update/', views.account_update, name='account_update'),
    path('account_detail/<int:user_pk>/', views.account_detail, name='account_detail'),
]


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

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        self.existQ = False
        self.existP = False
        result = self.explore(root, p, q)
        return result if self.existQ and self.existP else None

    def explore(self, node, p, q):
        if not node:
            return None
        left = self.explore(node.left, p,q)
        right = self.explore(node.right, p, q)

        if node == p:
            self.existP = True
            return node
        if node == q:
            self.existQ = True
            return node
        if left and right:
            return node
        return left or right


from src.solar.models.solar_model import SolarDataModel
from src.solar.models.saving_model  import SolarSavingsModel
from src.shared.controllers.base_controller import BaseController
from src.shared.services.vcom_svc import VcomAPIService
from src.shared.models.abbreviations_enum import VCOMAbbreviation

class SolarController(BaseController):
    def __init__(self):
        api_svc: VcomAPIService = VcomAPIService()
        self.system_id = "C36EM"
        super().__init__(api_svc)

    def get_system_info(self, system_key):
        """
        Get information about a specific system.
        Args:
            system_key (str): Identification key of the system.

        Returns:
            dict: System information.
        """
        return self.api_svc.get_system_info(system_key)

    def get_usage(self, interval, startDate, endDate):
        """
        Args:
            interval (str): The interval for measurements (e.g., 'day', 'hour').
            startDate (str): Start date for the data retrieval.
            endDate (str): End date for the data retrieval.
        Returns:
            list: List of SolarDataModel objects.
        """
        abbr: str = VCOMAbbreviation.SYSTEM_CAPACITY.value
        return self.get_measurement_data(interval, startDate, endDate, abbr)

    def get_production(self, interval, startDate, endDate):
        """
        Args:
            interval (str): The interval for measurements (e.g., 'day', 'hour').
            startDate (str): Start date for the data retrieval.
            endDate (str): End date for the data retrieval.
        Returns:
            list: List of SolarDataModel objects.
        """
        abbr: str = VCOMAbbreviation.PRODUCTION.value
        return self.get_measurement_data(interval, startDate, endDate, abbr)

    def get_savings(self, startDate, endDate):
        """
        Get CO2 and tree equivalent savings data for a specific system within a date range.
        Args:
            startDate (str): Start date for the data retrieval.
            endDate (str): End date for the data retrieval.

        Returns:
            list: List of SolarSavingsModel objects.
        """
        co2_data = self.api_svc.get_environmental_savings_co2(
            self.system_id, startDate, endDate
        )
        tree_data = self.api_svc.get_environmental_savings_tree_equivalents(
            self.system_id, startDate, endDate
        )

        savings = [SolarSavingsModel.from_json(co2, tree) for co2, tree in zip(co2_data, tree_data)]
        return savings

    def generate_report(self, id, start_date, end_date):
        """
        Generates a report for a given system ID and date range.
        """

        userInfo = self.api_svc.get_session()
        billInfo = self.api_svc.get_bill(id, start_date, end_date)
        systemsInfo = self.api_svc.get_systems()
        solarData = self.get_monthly_consumption(self, id, start_date, end_date)

        return {
            "userInfo": userInfo,
            "billInfo": billInfo,
            "systemsInfo": systemsInfo,
            "solarData": solarData,
        }

    #### UTILITIES ####
    def get_measurement_data(self, interval, startDate, endDate, abbreviation_id):
        """
        Get measurement data for a specific abbreviation ID.

        Args:
            interval (str): The interval for measurements (e.g., 'day', 'hour').
            startDate (str): Start date for the data retrieval.
            endDate (str): End date for the data retrieval.
            abbreviation_id (str): Abbreviation ID for the specific measurement.

        Returns:
            list: List of SolarDataModel objects.
        """
        raw_data = self.api_svc.get_measurement_by_abbr(
            interval, startDate, endDate, self.system_id, abbreviation_id
        )
        solar_data = [SolarDataModel.from_json(reading) for reading in raw_data]
        return solar_data


#!/bin/python3

import math
import os
import random
import re
import sys
from collections import deque

# Complete the isBalanced function below.
def isBalanced(s):
    stack = []
    open = ['(', '{', '[']
    close = [')', '}', ']']

    for char in s:
        if char in open:
            stack.append(char)
        else:
            if len(stack) > 0:
                open_e1 = stack.pop()
                open_e1_index = open.index(open_e1)
                if close[open_e1_index] != char:
                    return "NO"
            else:
                return "NO"

    if len(stack) > 0:
        return "NO"
    else:
        return "YES"

if __name__ == '__main__':

    t = int(input())

    for t_itr in range(t):
        s = input()

        result = isBalanced(s)
        print(result)


import types
from collections.abc import Iterable
# from itertools import chain

def flat_generator(list_of_list):
    for item in list_of_list:
        if isinstance(item, Iterable) and not isinstance(item, str):
            yield from flat_generator(item)
        else:
            yield item
            print(item)

def test_4():

    list_of_lists_2 = [
        [['a'], ['b', 'c']],
        ['d', 'e', [['f'], 'h'], False],
        [1, 2, None, [[[[['!']]]]], []]
    ]

    for flat_iterator_item, check_item in zip(
            flat_generator(list_of_lists_2),
            ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']
    ):

        assert flat_iterator_item == check_item

    assert list(flat_generator(list_of_lists_2)) == ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None, '!']

    assert isinstance(flat_generator(list_of_lists_2), types.GeneratorType)

if __name__ == '__main__':
    test_4()


import numpy as np

from typing import List
from layers import Conv2D, CrossCatergoricalEntropy, Dense, Layer, Softmax, StableSoftmax
from sklearn.metrics import f1_score

class Model:
    def __init__(self):
        self.layers: List[Layer] = []
        self.loss_fn = CrossCatergoricalEntropy()  # This model only works for soft-max with crosscategoricalentropy

    def add_layer(self, layer: Layer):
        self.layers.append(layer)

    def forward(self, input: np.ndarray) -> np.ndarray:
        inter_input = input

        for layer in self.layers:
            inter_input = layer.forward(inter_input)

        return inter_input

    def backward(self, input: np.ndarray, y: np.ndarray, learning_rate) -> np.ndarray:
        inter_input = input

        for layer in reversed(self.layers):
            if isinstance(layer, Softmax) or isinstance(layer, StableSoftmax):
                inter_input = layer.backward(input, y)
            elif isinstance(layer, Conv2D) or isinstance(layer, Dense):
                inter_input = layer.backward(inter_input, learning_rate)
            else:
                inter_input = layer.backward(inter_input)

        return inter_input

    def _shuffle(self, X, Y):
        assert X.shape[0] == Y.shape[0]
        p = np.random.permutation(X.shape[0])
        return X[p], Y[p]

    def train(self, train_X, train_Y, val_X, val_Y, batch_size, n_epochs, learning_rate, seed=0, output_file=None):
        np.random.seed(seed)

        n_batches = (train_Y.shape[0] + batch_size - 1) // batch_size

        out_f = None
        if output_file:
            out_f = open(output_file, 'a')

        for epoch in range(n_epochs):
            print(f"epoch {epoch+1}")
            if out_f:
                print(f"epoch {epoch+1}", file=out_f)
            total_corr = 0
            total_inputs = 0
            total_loss = 0

            shuffled_X, shuffled_Y = self._shuffle(train_X, train_Y)

            for batch_num in range(n_batches):
                x = shuffled_X[batch_num*batch_size:(batch_num+1)*batch_size]
                y = shuffled_Y[batch_num*batch_size:(batch_num+1)*batch_size]

                y_hat = self.forward(x)
                losses = self.loss_fn.calculate_loss(y, y_hat)
                self.backward(y_hat, y, learning_rate)

                # metrics
                batch_corr = np.sum(np.equal(np.argmax(y_hat, axis=1), np.argmax(y, axis=1)).astype(int))
                total_corr += batch_corr
                total_loss += np.sum(losses)
                total_inputs += losses.shape[0]
                f1_train = f1_score(np.argmax(y, axis=1), np.argmax(y_hat, axis=1), average='macro')

                if total_inputs % 300 == 0:
                    total_acc = total_corr * 100 / total_inputs
                    print(f"[{total_inputs}] loss: {total_loss / total_inputs}    \taccuracy: {total_acc}%     \tmacro-f1: {f1_train}")
                    if out_f:
                        print(f"[{total_inputs}] loss: {total_loss / total_inputs}    \taccuracy: {total_acc}%     \tmacro-f1: {f1_train}", file=out_f)

            val_Y_hat = self.forward(val_X)
            test_corr = np.sum(np.equal(np.argmax(val_Y_hat, axis=1), np.argmax(val_Y, axis=1)).astype(int))
            f1_val = f1_score(np.argmax(val_Y, axis=1), np.argmax(val_Y_hat, axis=1), average='macro')
            val_loss = np.sum(self.loss_fn.calculate_loss(val_Y, val_Y_hat))
            total_vals = val_Y.shape[0]
            val_acc = test_corr * 100 / total_vals

            print(f"VALIDATION [{total_vals}] loss: {val_loss / total_vals}    \taccuracy: {val_acc}%     \tmacro-f1: {f1_val}")
            if out_f:
                print(f"VALIDATION [{total_vals}] loss: {val_loss / total_vals}    \taccuracy: {val_acc}%     \tmacro-f1: {f1_val}", file=out_f, flush=True)

    def evaluate(self, test_X, test_Y, output_file):
        test_Y_hat = self.forward(test_X)
        test_corr = np.sum(np.equal(np.argmax(test_Y_hat, axis=1), np.argmax(test_Y, axis=1)).astype(int))
        f1_test = f1_score(np.argmax(test_Y, axis=1), np.argmax(test_Y_hat, axis=1), average='macro')
        test_loss = np.sum(self.loss_fn.calculate_loss(test_Y, test_Y_hat))
        total_tests = test_Y.shape[0]
        test_acc = test_corr * 100 / total_tests

        print(f"TEST [{total_tests}] loss: {test_loss / total_tests}    \taccuracy: {test_acc}%     \tmacro-f1: {f1_test}")
        with open(output_file, 'a') as f:
            print(f"TEST [{total_tests}] loss: {test_loss / total_tests}    \taccuracy: {test_acc}%     \tmacro-f1: {f1_test}", file=f)


from tkinter import*
from PIL import Image,ImageTk
import webbrowser
from datetime import datetime
from time import strftime

class Helpsupport:
    def __init__(self,root):
        self.root=root
        self.root.geometry("1366x768+0+0")
        self.root.title("Face_Recogonition_System")

# This part is image labels setting start
        # first header image
        img=Image.open(r"Images\banner.jpg")
        img=img.resize((1366,130),Image.Resampling.LANCZOS)
        self.photoimg=ImageTk.PhotoImage(img)

        # set image as lable
        f_lb1 = Label(self.root,image=self.photoimg)
        f_lb1.place(x=0,y=0,width=1366,height=130)

        # backgorund image
        bg1=Image.open(r"Images\bg4.png")
        bg1=bg1.resize((1366,768),Image.Resampling.LANCZOS)
        self.photobg1=ImageTk.PhotoImage(bg1)

        # set image as lable
        bg_img = Label(self.root,image=self.photobg1)
        bg_img.place(x=0,y=130,width=1366,height=768)

        #title section
        title_lb1 = Label(bg_img,text="Help Support",font=("verdana",30,"bold"),bg="white",fg="navyblue")
        title_lb1.place(x=0,y=0,width=1366,height=45)

        def time():
                string = strftime('%H:%M:%S %p')
                lbl.config(text = string)
                lbl.after(1000, time)

        img4 = Image.open(r"Images\back.jpg")
        img4 = img4.resize((34,27),Image.Resampling.LANCZOS)
        self.photoimg4 = ImageTk.PhotoImage(img4)

        b1=Button(bg_img,command=self.iExit,image=self.photoimg4, cursor="hand2")
        b1.place(x=10,y=2,width=40,height=40)

        lbl = Label(title_lb1, font=('times new roman',14,'bold'),background='white',foreground='blue')
        lbl.place(x=50,y=10,width=110,height=20)
        time()

        # Create buttons below the section
        # -------------------------------------------------------------------------------------------------------------------
        # website button 1
        std_img_btn=Image.open(r"Images\web.png")
        std_img_btn=std_img_btn.resize((180,180),Image.Resampling.LANCZOS)
        self.std_img1=ImageTk.PhotoImage(std_img_btn)

        std_b1 = Button(bg_img,command=self.website,image=self.std_img1,cursor="hand2")
        std_b1.place(x=250,y=200,width=180,height=180)

        std_b1_1 = Button(bg_img,command=self.website,text="Website",cursor="hand2",font=("tahoma",15,"bold"),bg="white",fg="navyblue")
        std_b1_1.place(x=250,y=380,width=180,height=45)

        # facebook  button 2
        det_img_btn=Image.open(r"Images\fb.png")
        det_img_btn=det_img_btn.resize((180,180),Image.Resampling.LANCZOS)
        self.det_img1=ImageTk.PhotoImage(det_img_btn)

        det_b1 = Button(bg_img,command=self.facebook,image=self.det_img1,cursor="hand2",)
        det_b1.place(x=480,y=200,width=180,height=180)

        det_b1_1 = Button(bg_img,command=self.facebook,text="Facebook",cursor="hand2",font=("tahoma",15,"bold"),bg="white",fg="navyblue")
        det_b1_1.place(x=480,y=380,width=180,height=45)

         # instagram button 3
        att_img_btn=Image.open(r"Images\instagram-1581266__340.webp")
        att_img_btn=att_img_btn.resize((230,230),Image.Resampling.LANCZOS)
        self.att_img1=ImageTk.PhotoImage(att_img_btn)

        att_b1 = Button(bg_img,command=self.instagram,image=self.att_img1,cursor="hand2",)
        att_b1.place(x=710,y=200,width=180,height=180)

        att_b1_1 = Button(bg_img,command=self.instagram,text="Instagram",cursor="hand2",font=("tahoma",15,"bold"),bg="white",fg="navyblue")
        att_b1_1.place(x=710,y=380,width=180,height=45)

         # twitter button 4
        hlp_img_btn=Image.open(r"Images\images.png")
        hlp_img_btn=hlp_img_btn.resize((180,180),Image.Resampling.LANCZOS)
        self.hlp_img1=ImageTk.PhotoImage(hlp_img_btn)

        hlp_b1 = Button(bg_img,command=self.twitter,image=self.hlp_img1,cursor="hand2",)
        hlp_b1.place(x=940,y=200,width=180,height=180)

        hlp_b1_1 = Button(bg_img,command=self.twitter,text="Twitter",cursor="hand2",font=("tahoma",15,"bold"),bg="white",fg="navyblue")
        hlp_b1_1.place(x=940,y=380,width=180,height=45)

        # create function for button

    def website(self):
        self.new = 1
        self.url = "https://kuldeeppanditg1234.wixsite.com/mysite"
        webbrowser.open(self.url,new=self.new)

    def facebook(self):
        self.new = 1
        self.url = "https://www.facebook.com"
        webbrowser.open(self.url,new=self.new)

    def instagram(self):
        self.new = 1
        self.url = "https://www.instagram.com/dixit_kuldeep/"
        webbrowser.open(self.url,new=self.new)

    def twitter(self):
        self.new = 1
        self.url = "https://twitter.com/Kuldeep02952876"
        webbrowser.open(self.url,new=self.new)

    def iExit(self):
        self.root.destroy()

if __name__ == "__main__":
    root=Tk()
    obj=Helpsupport(root)
    root.mainloop()


from bs4 import BeautifulSoup
from datetime import datetime, timedelta
from classes import Link, Delay, RouteStop
import requests
import re

date_format = "%d.%m.%Y"
time_format = "%H:%M"

class Resolver():

    url_with_datetime = "https://cp.hnonline.sk/{0}/spojenie/?date={1}&time={2}&f={3}&t={4}&fc=100003&tc=100003&direct=true&submit=true"

    @staticmethod
    def create_url(trans, station_f, station_t, in_datetime):
        return Resolver.url_with_datetime.format(
            trans,
            in_datetime.strftime("%d.%m.%Y"),
            in_datetime.strftime("%H:%M"),
            station_f,
            station_t
        )

    @staticmethod
    def resolve_mainsite_datetime(station_f, station_t, in_datetime):
        url = Resolver.create_url(
            'vlak',
            station_f,
            station_t,
            in_datetime
        )
        return Resolver.__resolve_mainsite(url)

    @staticmethod
    def resolve_mainsite_now(station_f, station_t):
        url = Resolver.create_url(
            'vlak',
            station_f,
            station_t,
            datetime.now()
        )
        return Resolver.__resolve_mainsite(url)

    @staticmethod
    def resolve_mainsite_minutesback(station_f, station_t, minutes=0):
        minutes_back = abs(minutes)
        url = Resolver.create_url(
            'vlak',
            station_f,
            station_t,
            datetime.now() - timedelta(minutes=minutes_back)
        )
        return Resolver.__resolve_mainsite(url)

    @staticmethod
    def __resolve_mainsite(url):
        web_content = requests.get(url).content.decode('UTF-8')

        print("RESOLVING: " + url)

        # Remove everything between
        # <!-- zobrazeni vysledku start --> & <!-- zobrazeni vysledku end-->
        remove_before = '<!-- zobrazeni vysledku start -->'
        remove_after = '<!-- zobrazeni vysledku end-->'
        trim_content = web_content[
            web_content.find(remove_before): web_content.find(remove_after)
        ]

        soup_data = BeautifulSoup(trim_content, 'html.parser')

        # Resolver for all links from mainsite
        link_dict = {}
        for table in soup_data.find_all('table'):
            new_link = Resolver.__resolve_link_table(table)
            link_dict.update({
                new_link.train_name: new_link
            })

        return link_dict

    @staticmethod
    def __resolve_link_table(in_table):
        tr_list = in_table.find_all('tr')

        # 0 - ''
        # 1 - date_f
        # 2 - station_f
        # 3 - arrival_f
        # 4 - time_f
        # 5 - note_f
        # 6 - train_name + delay
        data_f = [
            td.text.replace(u'\xa0', u'')
            for td in tr_list[1].find_all('td')
        ]
        # 0 - ''
        # 1 - date_t / ''
        # 2 - station_t
        # 3 - time_t
        # 4 - departure_t
        # 5 - note_t
        # 6 - ''
        data_t = [
            td.text.replace(u'\xa0', u'')
            for td in tr_list[2].find_all('td')
        ]

        # Check data
        data_t[1] = data_t[1] if (data_t[1]) else data_f[1]

        # Create new link
        new_link = Link()
        new_link.train_name = " ".join(
            data_f[6].split('  ')[:-1]
        ).strip().replace('/', '')
        new_link.station_f = data_f[2].strip()
        new_link.station_t = data_t[2].strip()
        new_link.datetime_f = datetime.strptime(
            data_f[1] + "2017" + data_f[4],
            date_format + time_format
        )
        new_link.datetime_t = datetime.strptime(
            data_t[1] + "2017" + data_t[3],
            date_format + time_format
        )
        '''
        new_link.datetime_arrival_f = datetime.strptime(
            #TODO
        )
        new_link.datetime_departure_t = datetime.strptime(
            #TODO
        )
        '''
        new_link.note_f = data_f[5]
        new_link.note_t = data_t[5]

        # Resolve links
        new_link.route_url, new_link.location_url = Resolver.__resolve_urls(
            [a.get('href') for a in tr_list[1].find_all('a')]
        )

        return new_link

    @staticmethod
    def __resolve_urls(href_list):
        route_url = ""
        location_url = ""

        root_link = 'https://cp.hnonline.sk'
        string_route = '/draha/'
        string_location = '/poloha/'
        for url_link in href_list:
            if (url_link.find(string_route) != -1):
                route_url = root_link + url_link
            elif (url_link.find(string_location) != -1):
                location_url = root_link + url_link

        return route_url, location_url

    @staticmethod
    def resolve_delay(link_object):
        if (not link_object.location_url):
            print("No location_url: " + link_object.train_name)
            return None

        print(
            "Resolving: " + link_object.train_name +
            " - " + link_object.location_url
        )

        # Download web page
        web_content = requests.get(
            link_object.location_url
        ).content.decode('UTF-8')

        # Remove everything after <!-- start PageEnd -->
        remove_before = '<!-- start PageEnd -->'
        remove_index = web_content.find(remove_before)
        # Invalid index
        if (remove_index == -1):
            link_object.location_url = ""
            return None

        trim_content = web_content[:remove_index]

        soup_data = BeautifulSoup(trim_content, 'html.parser')
        if (soup_data.table):
            data_list = list(filter(
                None,
                soup_data.table.text.split('\n')
            ))
            trim_list = [item[item.find(":") + 1: ] for item in data_list]
            # Invalid input data
            if (len(trim_list) != 4):
                link_object.location_url = ""
                return None

            # Create delay class
            new_delay = Delay(
                trim_list[0],
                datetime.strptime(
                    link_object.datetime_f.strftime(
                        date_format
                    ) + trim_list[1],
                    date_format + time_format
                ),
                datetime.strptime(
                    trim_list[2],
                    date_format + " " + time_format
                ),
                int(re.search(r'\d+', trim_list[3]).group())
            )
            return new_delay
        else:
            return None

    @staticmethod
    def resolve_route(link_object):
        if (not link_object.route_url):
            print("No route_url: " + link_object.train_name)
            return []

        print(
            "Resolve route: " + link_object.train_name +
            " - " + link_object.route_url
        )

        # Download web page
        web_content = requests.get(
            link_object.route_url
        ).content.decode('UTF-8')

        # Remove everything after <!-- start PageEnd -->
        remove_before = '<!-- start PageEnd -->'
        trim_content = web_content[:web_content.find(remove_before)]

        soup_data = BeautifulSoup(trim_content, 'html.parser')
        if (soup_data.table):
            stop_list = []
            for tr in soup_data.table.find_all('tr'):
                new_stop = Resolver.__resolve_stop(tr)
                if (new_stop):
                    stop_list.append(new_stop)

            return stop_list
        else:
            return []

    @staticmethod
    def __resolve_stop(tr):
        # 0 - station
        # 1 - time_arrival
        # 2 - time_departure
        # 3 - note
        # 4 - km
        # 5 - ''
        td_list = [
            td.text.replace(u'\xa0', '') for td in tr.find_all('td')
        ]
        if (len(td_list) == 6):
            return RouteStop(
                td_list[0].strip(),
                td_list[1],
                td_list[2],
                int(td_list[4])
            )
        else:
            return None


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar  8 12:31:18 2023

@author: gabriel
"""

import numpy as np
import matplotlib.pyplot as plt
from hamiltonians import Hamiltonian_soliton_A1u, Hamiltonian_soliton_A1u_sparse, Hamiltonian_A1u_single_step_sparse, Hamiltonian_A1u_sparse, Zeeman, Hamiltonian_A1u_S
from functions import get_components
import scipy

L_x = 120
L_y = 120
t = 1
Delta = 1
mu = -2  #-2
Phi = np.pi/2  #superconducting phase
t_J = 1    #t/2
L = L_y//2
k = 8   #number of eigenvalues
Delta_Z = 0
theta = np.pi/2
phi = 0

H = Hamiltonian_A1u_S(t=t, mu=mu, L_x=L_x, L_y=L_y, Delta=Delta, t_J=t_J, Phi=Phi)
#H = Hamiltonian_soliton_A1u_sparse(t=t, mu=mu, L_x=L_x, L_y=L_y, Delta=Delta, t_J=t_J, Phi=Phi, L=L)
params = {"t": t, "mu": mu, "L_x": L_x, "L_y": L_y, "Delta": Delta, "t_J": t_J, "Phi": Phi, "L": L}
#H = Hamiltonian_A1u_single_step_sparse(t=t, mu=mu, L_x=L_x, L_y=L_y, Delta=Delta, t_J=t_J, Phi=Phi)
#H = (Hamiltonian_A1u_sparse(t, mu, L_x, L_y, Delta) + Zeeman(theta=theta, Delta_Z=Delta_Z, L_x=L_x, L_y=L_y, phi=phi))

eigenvalues_sparse, eigenvectors_sparse = scipy.sparse.linalg.eigsh(H, k=k, sigma=0)

#%% Probability density
index = np.arange(k)   #which zero mode (less than k)
probability_density = []
zero_state = []
localized_state_up = [] # it is the site ((L_x+L)/2, L_y/2)
localized_state_down = []
for i in index:
    destruction_up, destruction_down, creation_down, creation_up = get_components(eigenvectors_sparse[:,i], L_x, L_y)
    probability_density.append((np.abs(destruction_up)**2 + np.abs(destruction_down)**2 + np.abs(creation_down)**2 + np.abs(creation_up)**2)/(np.linalg.norm(np.abs(destruction_up)**2 + np.abs(destruction_down)**2 + np.abs(creation_down)**2 + np.abs(creation_up)**2)))
    zero_state.append(np.stack((destruction_up, destruction_down, creation_down, creation_up), axis=2)) #positive energy eigenvector splitted in components
    localized_state_up.append(zero_state[i][(L_y-L)//2, L_x//2,:])
    localized_state_down.append(zero_state[i][(L_y+L)//2, L_x//2,:])

#%% Plotting of probability density

plt.rc("font", family="serif")  # set font family
plt.rc("xtick", labelsize="large")  # reduced tick label size
plt.rc("ytick", labelsize="large")
plt.rc('font', size=18) #controls default text size
plt.rc('axes', titlesize=18) #fontsize of the title
plt.rc('axes', labelsize=18) #fontsize of the x and y labels
plt.rc("text", usetex=True) # for better LaTex (slower)
plt.rcParams['xtick.top'] = True    #ticks on top
plt.rcParams['xtick.labeltop'] = False
plt.rcParams['ytick.right'] = True    #ticks on left
plt.rcParams['ytick.labelright'] = False
plt.rc('legend', fontsize=18) #fontsize of the legend

index = 0
fig, ax = plt.subplots()
image = ax.imshow(probability_density[index], cmap="Blues", origin="lower") #I have made the transpose and changed the origin to have xy axes as usually
plt.colorbar(image)
#ax.set_title(f"{params}")
ax.set_xlabel("x")
ax.set_ylabel("y")
#ax.text(5,25, rf'$index={index}; \Phi={np.round(Phi, 2)}$')
#ax.text(5,25, rf'$\phi={np.round(Phi, 2)}$')
ax.set_title("Probability density (TRITOPS-S)")
ax.set_ylim((0,L_y))

def single_soliton(y, L_y):
    return np.heaviside(L_y//2-y_values, 1)-np.heaviside(y_values-L_y//2, 1)

left, bottom, width, height = [0.6, 0.24, 0.1, 0.5]
ax2 = fig.add_axes([left, bottom, width, height])
y_values = np.arange(L_y+1)
ax2.plot(single_soliton(y_values, L_y), y_values, "r")
ax2.set_ylabel("y")
ax2.set_xlabel(r"$sgn(\phi)$")
ax2.set_title(r"$f_{1s}$")
ax2.set_ylim((0,L_y))


import urllib.request
from bs4 import BeautifulSoup
import re

import csv

with open('data/scrape_csv.csv', 'w', newline='') as csvfile:

    # csv setting
    writer = csv.writer(csvfile, delimiter=',', quotechar='"')
    writer.writerow(['title', 'link'])

    # url = 'https://news.detik.com/berita/d-6026440/demo-bem-si-11-april-begini-rekayasa-lalu-lintas-di-sekitar-gedung-dpr'
    url = 'https://news.detik.com/'

    req = urllib.request.Request(url)
    res = urllib.request.urlopen(req)
    responData = res.read()

    # print(responData)

    title = re.findall(r'<title>(.*?)</title>', str(responData))
    date = re.findall(
        r'
(.*?)
', str(responData))
    author = re.findall(
        r'
(.*?)', str(responData))

    title2 = re.findall(
        r'
(.*?)
', str(responData))
    link = re.findall(r'i-link=(.*?)i-img-qs=', str(responData))

    # custom resource

    def normalization(text):
        text = text.replace("\\n", "")
        text = text.replace("\\'", "")
        text = text.strip()

        return text

    for i in range(len(title2)):

        getTitle = normalization(title2[i])
        getLink = normalization(link[i])

        writer.writerow([getTitle, getLink])


import sys
sys.path.append('requirements/')

import itertools
import json
import threading
import os
import pickle
import time
from collections import defaultdict
from decimal import Decimal

import boto3
from raven import Client as Raven
from stravalib.client import Client

raven = Raven(os.getenv('RAVEN_DSN'))

settings = {
    'segments': {
        'by_foot': {
            'hike': [6094058],
            'run': [6306193, 5604733],
            'walk': [3462619],
        },
        'on_wheels': {
            'ride': [651728, 776000],
        },
    },
    'cache_lifetime_seconds': 60*60,
}

class UsersTable(object):
    def __init__(self):
        self.table = boto3.resource('dynamodb').Table('usersTable')

    def get(self, id):
        return self.table.get_item(
            Key={
               'id': int(id),
            },
            ReturnConsumedCapacity='NONE',
        ).get('Item')

    def update(self, id, **kwargs):
        return self.table.update_item(
            Key={
                'id': int(id),
            },
            UpdateExpression='SET ' + \
                ', '.join(['{0} = :{0}'.format(key) for key in kwargs]),
            ExpressionAttributeValues={
                ':{}'.format(key): val for key, val in kwargs.iteritems() },
            ReturnValues='ALL_NEW',
            ReturnConsumedCapacity='NONE',
        ).get('Attributes')

class Activity(dict):
    def __init__(self, effort):
        if isinstance(effort, Decimal):
            self['id'] = int(effort)
        else:
            self['id'] = effort.activity.id
            self['private'] = effort.activity.private
            if not self['private']:
                self['name'] = effort.activity.name
                self['start_date_local'] = effort.activity.start_date_local

    def __cmp__(self, other):
        if isinstance(other, Activity):
            return self['id'].__cmp__(other['id'])
        else:
            return self['id'].__cmp__(other)

    def __hash__(self):
        return self['id']

class StravaClient(Client):
    @property
    def athlete_id(self):
        return self._athlete_id

    @athlete_id.setter
    def athlete_id(self, v):
        self._athlete_id = int(v)

    def get_segment_efforts(self, activities_set, segment_id):
        activities_set.update(
            map(Activity, super(StravaClient, self).get_segment_efforts(
                segment_id=segment_id,
                athlete_id=self.athlete_id)))

def get_activity_counts(event, context):
    try:
        strava = StravaClient()
        usersTable = UsersTable()
        request = json.loads(event['body'])

        if 'code' in request:
            # We have been redirected here from the Strava OAuth flow. Use the
            # provided code to get a per-user access token.
            athlete = strava.exchange_code_for_token(
                client_id=os.getenv('STRAVA_CLIENT_ID'),
                client_secret=os.getenv('STRAVA_CLIENT_SECRET'),
                code=request['code'],
            )

            # Now store that access token in DynamoDB. Update to make sure we don't
            # overwrite any extra attributes for existing users.
            item = usersTable.update(id=athlete.id, access_token=strava.access_token)

        if 'athlete' in request:
            # Retreive the athlete-specific access token.
            item = usersTable.get(request['athlete'])
            strava.access_token = item['access_token']

        # Set the athlete ID on the Strava client
        strava.athlete_id = item['id']

        # Check for cache hit here
        if Decimal(time.time()) - item.get('cache_time', 0) < settings['cache_lifetime_seconds']:
            sorted_activities = pickle.loads(item['cache_value'].decode('base64'))
        else:
            # Get efforts for each segment
            activities = defaultdict(set)
            threads = []
            for category, types in settings['segments'].iteritems():
                for segment_id in itertools.chain.from_iterable(types.itervalues()):
                    threads.append(threading.Thread(
                        target=strava.get_segment_efforts,
                        args=( activities[category], segment_id)))
                    threads[-1].start()
                # Add extra activities
                activities[category].update(
                    map(Activity, item.get('extra_' + category, [])))

            # Wait for all threads to finish
            for t in threads:
                t.join()

            # Convert sets to lists and sort
            sorted_activities = { category: sorted(efforts) for category, efforts
                                in activities.iteritems() }

            # Cache the result
            usersTable.update(
                id=strava.athlete_id,
                cache_time=Decimal(time.time()),
                cache_value=pickle.dumps(sorted_activities).encode('base64'),
                )

        # Assemble the response
        response = {
            'athlete_id': strava.athlete_id,
            'activities': sorted_activities,
        }

        return {
            "statusCode": 200,
            "headers": {
                "Access-Control-Allow-Origin": os.getenv("ORIGIN", "*"),
            },
            "body": json.dumps(response)
        }

    except Exception:
        if os.getenv('RAVEN_DSN'):
            raven.captureException()
        else:
            raise


import math
from typing import cast

import jax
import jax.numpy as jnp
import pytest
from jax import random
from kwgflows.divergences.mmd import mmd as scalar_mmd
from kwgflows.pytypes import Scalar
from kwgflows.rkhs.kernels import gaussian_kernel

from calibration.conditional_models.gaussian_models import (
    GaussianConditionalModel,
    mmd_gaussian_kernel,
)

def mmd_closed_form(
    sigma_k: float,
    mu_p: jax.Array,
    mu_q: jax.Array,
    sigma_p: float,
    sigma_q: float,
    d: int,
):
    mmd_cf = (
        1 / jnp.sqrt((1 + (sigma_p**2 + sigma_p**2) / sigma_k**2) ** d)
        + 1 / jnp.sqrt((1 + (sigma_q**2 + sigma_q**2) / sigma_k**2) ** d)
        - 2
        * jnp.exp(
            -0.5
            * (jnp.sum((mu_p - mu_q) ** 2))
            / (sigma_k**2 + sigma_p**2 + sigma_q**2)
        )
        / jnp.sqrt((1 + (sigma_p**2 + sigma_q**2) / (sigma_k**2)) ** d)
    )
    return cast(Scalar, mmd_cf)

def test_expectation():
    # Gaussian kernel
    kernel = gaussian_kernel.create(sigma=1.0)

    # check dependence of expectation on dimension
    d1 = 1
    cm1 = GaussianConditionalModel(
        mu=jnp.zeros((d1,)),
        sigma=1.0,
    )

    d2 = 3
    cm2 = GaussianConditionalModel(
        mu=jnp.zeros((d2,)),
        sigma=1.0,
    )

    ret1 = cm1.analytical_expectation_of(kernel, jnp.zeros((d1,)))
    ret2 = cm2.analytical_expectation_of(kernel, jnp.zeros((d2,)))
    assert ret2 ** (1 / d2) == pytest.approx(ret1 ** (1 / d1))

    # check independence w.r.t mean difference
    d1 = 2

    cm1 = GaussianConditionalModel(
        mu=jnp.zeros((d1,)),
        sigma=1.0,
    )

    cm2 = GaussianConditionalModel(
        mu=jnp.ones((d1,)),
        sigma=1.0,
    )

    assert cm1.analytical_expectation_of(kernel, cm2.mu) == pytest.approx(
        cm2.analytical_expectation_of(kernel, cm1.mu),
    )

    # check bivariate expectation nice case where all means are 0
    d1 = 3
    s1 = 2
    s2 = 4

    k = gaussian_kernel(sigma=1.0)
    cm1 = GaussianConditionalModel(
        mu=jnp.zeros((d1,)),
        sigma=s1,
    )

    cm2 = GaussianConditionalModel(
        mu=jnp.zeros((d1,)),
        sigma=s2,
    )

    ret = cm1.bivariate_analytical_expectation_of(k, cm2)
    assert ret == pytest.approx(1 / math.sqrt((1 + s1**2 + s2**2) ** d1))

def test_mmd_gaussian_model():
    d = 2
    mu = jnp.zeros((d,))
    sigma_p = 1.0

    p = GaussianConditionalModel(
        mu=mu,
        sigma=sigma_p,
    )

    q = p
    kernel = gaussian_kernel.create(sigma=1.0)
    mmd = mmd_gaussian_kernel(kernel=kernel)
    assert mmd(p, q) == pytest.approx(0.0)

    r"""
    For two d-dimensional gaussian random variables with isotropic diagonal covariance
    matrices: $ p = \mathcal N(\mu_p, \sigma_p^2 I_d) $ and
    $ q = \mathcal N(\mu_q, \sigma_q^2 I_d)$, $MMD^2(p, q)$ is given by:
    $$
    \textrm{MMD}^2(p, q) =
        \frac{1}{{(1 + 2(\sigma_p)^2 / \sigma_k^2)}^d} +
        \frac{1}{{(1 + 2(\sigma_q)^2 / \sigma_k^2)}^d} -
        \frac{2}{{1 + (\sigma_p + \sigma_q)^2 / \sigma_k^2}^d} \times
        \exp (
            -0.5 * \frac{\| \mu_p - \mu_q \|^2}{\sigma_k^2 + \sigma_p^2 + \sigma_q^2}
        )
    $$

    In the following, we check that the computing the MMD between two such models
    returns this correct value. This test is useful because the current MMD computation
    method in such cases goes through other code paths (`analytical_expectation_of`),
    and can thus be seen as an end-to-end to such methods, which are used in other
    parts of the code base (such as SKCE U-statistics computations)
    """
    d = 4
    mu_p = jnp.zeros((d,))
    mu_q = jnp.zeros((d,)) + 2.0
    sigma_p = 2.0
    sigma_q = 10.0

    p = GaussianConditionalModel(
        mu=mu_p,
        sigma=sigma_p,
    )
    q = GaussianConditionalModel(
        mu=mu_q,
        sigma=sigma_q,
    )

    kernel = gaussian_kernel.create(sigma=2.0)
    mmd = mmd_gaussian_kernel(kernel=kernel)

    mmdval = mmd(p, q)

    dp = p.non_mcmc_discretize(random.PRNGKey(0), 3000)
    dq = q.non_mcmc_discretize(random.PRNGKey(0), 3000)

    mmd_d = scalar_mmd(kernel)(dp, dq)

    mmd_cf = mmd_closed_form(kernel.sigma, mu_p, mu_q, sigma_p, sigma_q, d)
    assert mmdval == pytest.approx(mmd_cf)
    assert mmdval == pytest.approx(mmd_d, abs=1e-1)

def test_score():
    d = 10
    key = random.PRNGKey(1234)
    key, subkey = random.split(key)
    mu = random.normal(subkey, (d,))
    sigma = 4.1
    p = GaussianConditionalModel(
        mu=mu,
        sigma=sigma,
    )
    key, subkey = random.split(key)
    y = random.normal(subkey, (d,))
    assert p.score(y) == pytest.approx(jax.grad(p)(y))


import cv2
import datetime
import os

from CrossCuttingConcerns import raspi_log, mqtt_adapter

topic = "qr"
time_old = datetime.datetime.now()

def main():
    try:
        read_qr()
    except Exception as e:
        raspi_log.log_process(f"There was faced with error on camera object: {e}")

def read_qr():
    raspi_log.log_process(str(f"Qr started! parent id:, {os.getppid()},  self id:, {os.getpid()}"))
    mqtt_adapter.connect("qr")
    old_data = ""
    # set up camera objects
    cap = cv2.VideoCapture(0)
    # QR code detection object
    detector = cv2.QRCodeDetector()
    while True:
        # get the image
        _, img = cap.read()
        # get bounding box coords and data
        data, bbox, _ = detector.detectAndDecode(img)

        # if there is a bounding box, draw one, along with the data
        if bbox is not None:
            #   for i in range(len(bbox)):
            #      cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
            #              0, 255), thickness=2)
            # cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) - 10), cv2.FONT_HERSHEY_SIMPLEX,
            #          0.5, (0, 255, 0), 2)
            if data:
                send_qr(data, old_data)
                old_data = data

        # display the image preview
        # cv2.imshow("code detector", img)
        if cv2.waitKey(1) == ord("q"):
            break
    # free camera object and exit
    cap.release()
    cv2.destroyAllWindows()
    mqtt_adapter.loop()

def send_qr(message, old_message):
    global topic, time_old
    global time_old
    time_now = datetime.datetime.now().second
    result = message
    if old_message != message:
        mqtt_adapter.publish(result, topic)
        raspi_log.log_process(result)
        time_old = time_now
    else:
        if (time_now - time_old) % 60 >= 10:
            mqtt_adapter.publish(result, topic)
            time_old = time_now


import cv2
import numpy as np
import os.path
import glob

# \*
in_dir = r'D:\wildfire-local\c4d\render\2021.3.26-1\*'

out_dir = r'D:\wildfire-local\c4d\compose\cut_smoke\1\cut'

def cut_trans(_img_mat):
    gray = cv2.cvtColor(_img_mat, cv2.COLOR_BGRA2GRAY)
    # cv2.imshow('test',gray)
    # cv2.waitKey(0)
    _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    x, y, w, h = cv2.boundingRect(binary)
    cut = _img_mat[y:y + h, x:x + w]
    # print(np.shape(cut))
    # cv2.imshow('test1', cut)
    # cv2.waitKey(0)
    return cut

if __name__ == '__main__':
    for img_file in glob.glob(in_dir):
        img = cv2.imread(img_file, -1)
        cv2.imwrite(os.path.join(out_dir, os.path.basename(img_file)), cut_trans(img))


import os
import wave
from array import array

import pyaudio

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 4096
AUDIO_FILE_NAME = "/home/pi/data/audio/recording.wav"

if not os.path.exists('/home/pi/data'):
    os.mkdir('/home/pi/data')
    os.mkdir('/home/pi/data/audio')
elif not os.path.exists('/home/pi/data/audio'):
    os.mkdir('/home/pi/data/audio')

audio = pyaudio.PyAudio()

# recording prerequisites
stream = audio.open(format=FORMAT, channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

# starting recording
frames = []
recording = False
tried = 0
volume = []

while True:
    data = stream.read(CHUNK)
    vol = max(array('h', data))
    print(vol)
    if vol > 10000:
        recording = True
        tried = 0
    else:
        if recording:
            tried += 1
            if tried > 25:
                break
        else:
            tried += 1
            if tried > 50:
                break

    if recording:
        frames.append(data)

# end of recording
stream.stop_stream()
stream.close()
audio.terminate()

if recording:
    # writing to file
    wavfile = wave.open(AUDIO_FILE_NAME, 'wb')
    wavfile.setnchannels(CHANNELS)
    wavfile.setsampwidth(audio.get_sample_size(FORMAT))
    wavfile.setframerate(RATE)
    wavfile.writeframes(b''.join(frames))
    wavfile.close()
else:
    try:
        os.remove(AUDIO_FILE_NAME)
    except FileNotFoundError:
        pass


from django.urls import path
from . import  views
urlpatterns = [
    path('', views.api_overview, name = 'api'),
    path('project-list/', views.project_list, name='api-projects'),
    path('project-update-list/', views.project_update_list, name='api-updates'),
    path('project-feature-list/', views.project_feature_list, name='api-features'),
    path('code-sample-list/', views.code_sample_list, name='api-codes'),
    path('myinfo/', views.my_info, name='api-myinfo'),
]


"""Round Controller"""
# coding: utf-8

from controllers import BaseController

from models.tournament import Tournament

from models.players import Player

class RoundController(BaseController):
    """
    Purpose Enabling the control and monitoring of tournament rounds.

    """
    def __init__(self, tournament: Tournament):
        """

        Args:
            tournament: obj(Tournament)
        """
        super().__init__(tournament)
        self.round = tournament.rounds[-1]

    def menu_round(self):
        """
        Round management menu
        """
        title = "Bienvenue dans le gestionnaire de tournois d'échec.\n"
        subtitle = "Console de gestion des rounds du tournoi."
        menu = {1: (self.menu_round, f"Nom : {self.round.name}"),
                2: (self.menu_round, f"Début du Round : {self.round.start}"),
                3: (self.menu_round, f"Fin du Round : {self.round.end}"),
                4: (self.display_list_round_matchs, 'Consulter les matches en cours'),
                5: (self.add_score, f"Saisir les scores du Round {self.round.number}"),
                6: (self.start_new_round, 'Démarrer un nouveau Round'),
                9: (str('back'), 'Retour au menu')}

        if self.round.end is None:
            menu[6] = (self.stop_round, f'Déclarer le round {self.round.number} fini')
        elif self.round.number <= self.tournament.max_rounds_number:
            result = [match.score for match in self.round.matches if match.score is None]
            if len(result):
                menu[6] = (self.add_score, f'{len(result)} score reste à saisir '
                                           f'pour pouvoir démarrer le prochain round')
            elif len(result) == 0 and self.round.number == self.tournament.max_rounds_number:
                self.tournament.save()
                menu[6] = (self.view_result, "Tournoi terminé - Afficher les résultats")
            elif len(result) == 0 and self.round.number < self.tournament.max_rounds_number:
                menu[4] = (self.display_list_round_matchs, 'Consulter les résultats des matchs')
                del menu[5]

        self.view_menu.display_menu(title=title, subtitle=subtitle, question=menu)

        r = self.ask_and_launch(menu=menu)

        return self.back_menu(r)

    def display_list_round_matchs(self):
        """
        Method that calls up the view displaying the matches of the current round
        """
        self.view_menu.view_matchs(self.round)

    def add_score(self):
        """
        Method that calls up the view that informs the winners of the different matches
        """
        self.view_menu.select_match(self.round)

        response = self.ask_and_store_number("Choisissez le match pour renseigner le vainqueur :")
        if response[0]:
            if 1 <= response[1] <= 4:
                 # response = tuple(False/True if valid input, input value)
                match_played = self.round.matches[response[1]-1]

                if match_played.score is None:
                    self.view_menu.select_winner(match_played)
                    response = self.ask_and_store_number()

                    if response[1] in [1, 2]:   # One player won
                        match_played.players[response[1]-1].win()
                        match_played.win(match_played.players[response[1]-1])

                    elif response[1] == 3:       # Result = Equality
                        match_played.players[0].equality()
                        match_played.players[1].equality()
                        match_played.win()

                    # We add to the has_met attribute of the player the opponent he has just faced.
                    match_played.players[0].add_meet(match_played.players[1].uuid)
                    match_played.players[1].add_meet(match_played.players[0].uuid)

                else:   # Si le match a déjà un résultat d'enregistré => Alerter l'utilisateur
                    self.view_menu.stand_by_msg("Le match sélectionné à déjà été clos")

    def start_new_round(self):
        """
        Method to start a new round.
        """
        self.tournament.add_round()
        self.round = self.tournament.rounds[-1]
        self.tournament.save()

    def stop_round(self):
        """
        Method of informing the end of the round.
        """
        if self.round.start != "":
            self.round.end_round()

    def view_result(self):
        """
        Méthode qui affiche le résultat du tournoi.

        Returns:

        """

        p = Player.list_player_tournament()
        p.sort(reverse=True, key=lambda x: (int(x.point), int(x.elo)))
        data = []
        for n, player in enumerate(p):
            data.append(f"{n + 1} - {player.fullname} ({player.age} ans) avec {player.point} points,"
                        f" classement ELO : {player.elo}")

        title = "Bienvenue dans le gestionnaire de tournois d'échec.\n"
        subtitle = "Classement du tournoi."

        self.view_menu.display_data(title, subtitle, data)


import os
import json
from google.cloud import storage, language_v1
from flask import Flask, request

app = Flask(__name__)

# Set up the Google Cloud Storage client and bucket name
bucket_name = '<your-bucket-name>'
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)

# Set up the Google Cloud Natural Language API client and language type
language_client = language_v1.LanguageServiceClient()
language_type = language_v1.Document.Type.PLAIN_TEXT

@app.route('/')
def index():
    """
    Display the index page with a form for entering the name of the file to analyze.
    """
    return """

Feedback Analysis Dashboard

Welcome to the Feedback Analysis Dashboard! This dashboard displays the results of sentiment analysis and entity extraction on customer feedback data.

                <form method="POST" action="/analyze">
                    Bucket:
                    <input type="text" name="bucket" id="bucket">

                    File:
                    <input type="text" name="file" id="file">

                    <input type="submit" value="Analyze Feedback">
                </form>

    """

@app.route('/analyze', methods=['POST'])
def analyze():
    """
    Analyze the customer feedback data in the specified file and return the sentiment analysis and entity extraction results.
    """
    bucket_name = request.form['bucket']
    file_name = request.form['file']
    blob = bucket.blob(file_name)

    # Download the feedback data from the specified file in the bucket
    feedback_data = json.loads(blob.download_as_string())

    # Analyze the feedback data using the Google Cloud Natural Language API
    document = {'content': json.dumps(feedback_data), 'type': language_type}
    response = language_client.analyze_sentiment(request={'document': document})
    sentiment = response.document_sentiment.score

    document = {'content': json.dumps(feedback_data), 'type': language_type}
    response = language_client.analyze_entities(request={'document': document})
    entities = [{'name': entity.name, 'type': language_v1.Entity.Type(entity.type).name} for entity in response.entities]

    # Return the sentiment analysis and entity extraction results as JSON
    return json.dumps({'sentiment': sentiment, 'entities': entities})

if __name__ == '__main__':
    # Start the Flask server
    app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))


from sklearn.base import BaseEstimator, ClassifierMixin
import numpy as np
from tqdm import tqdm
from tqdm.notebook import tqdm as tqdm_notebook
from scipy.special import logsumexp
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from numpy import logaddexp
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from pbg.util import SemiLabelEncoder

class TPBG(BaseEstimator, ClassifierMixin):

    def __init__(self, n_components, alpha=0.05, beta=0.0001, local_max_itr=50,
                 global_max_itr=50, local_threshold=1e-6, global_threshold=1e-6,
                 save_interval=-1, feature_names=None, target_name=None, silence=False,
                 eval_func=None):
        self.n_components = n_components
        self.alpha = alpha
        self.beta = beta
        self.local_max_itr = local_max_itr
        self.global_max_itr = global_max_itr
        self.local_threshold = local_threshold
        self.global_threshold = global_threshold
        self.feature_names = feature_names
        self.is_fitted_ = False
        self.log_alpha = np.log(self.alpha)
        self.log_beta = np.log(self.beta)
        self.target_name = target_name
        self.silence = silence
        self.eval_func = eval_func
        self.save_interval = save_interval
        self.is_labeled = True

    def local_propag(self, j):
        local_niter = 0
        words = [x for x in self.X[j].nonzero()[1]]
        if len(words) == 0:
            return
        log_F = np.log(self.X[j, words].toarray().T)
        log_Aj = self.log_A[j]
        log_Bw = self.log_B[words]
        log_F_C = None
        while local_niter < self.local_max_itr:
            local_niter += 1
            oldA_j = log_Aj
            log_C = log_Aj + log_Bw
            log_C = log_C - logsumexp(log_C, axis=1, keepdims=True)
            log_F_C = log_F + log_C
            logsumexp_F_C = logsumexp(log_F_C, axis=0, keepdims=True)
            log_Aj = logaddexp(self.log_alpha, logsumexp_F_C)
            mean_change = np.mean(abs(log_Aj - oldA_j))
            if mean_change <= self.local_threshold:
                #print('convergiu itr %s' %local_niter)
                break
        self.log_A[j] = log_Aj
        self.B2[words] += np.exp(log_F_C)

    def global_propag(self):
        sum_columns = np.sum(self.B2, axis=0)
        log_B_norm = np.log(self.B2) - np.log(sum_columns)
        self.log_B = logaddexp(self.log_beta, log_B_norm)
        self.B2 = np.zeros((self.nwords, self.n_components))

    def _init_matrices(self):
        self.log_A = np.log(np.random.dirichlet(
            np.ones(self.n_components), self.ndocs))
        self.log_B = np.log(np.random.dirichlet(
            np.ones(self.nwords), self.n_components).transpose())
        self.B2 = np.zeros((self.nwords, self.n_components))

    def _init_supervised_matrices(self):
        print('oi')
        self._init_matrices()
        for j in range(self.ndocs):
            if not self.unlabeled[j]:
                self.suppress(j)
        for i in tqdm_notebook(range(self.nwords), ascii=True, desc='initialing.[]:   '):
            docs = [d for d in self.X[:, i].nonzero()[0]]
            # if word w_i not belong in train documents set X_train
            if len(docs) == 0:
                self.log_B[i] = np.ones(self.n_components)
                continue
            log_F = np.log(self.X[docs, i].toarray())
            log_A_j = self.log_A[docs]
            log_A_j = log_A_j - logsumexp(log_A_j, axis=1, keepdims=True)
            self.log_B[i] = logsumexp(log_F + log_A_j, axis=0)
        self.log_B = self.log_B - logsumexp(self.log_B, axis=0)
        self.log_B = np.log(self.beta + np.exp(self.log_B))
        self.print_top_topics()

    def create_class_map(self, flat_y):
        for cls_id in np.unique(flat_y):
            if cls_id != -1:  # unlabeled flag
                self.map_class_.setdefault(cls_id, self.free_id.pop())

    def unsupervised_fit(self, X):
        self.is_labeled = False
        self.X = X
        self.ndocs, self.nwords = X.shape
        self.unlabeled = np.empty(self.ndocs)
        self.unlabeled.fill(-1)

        self._init_matrices()
        self.bgp()
        self.components_ = np.exp(self.log_B.T)
        self.is_fitted_ = True

        return self

    def fit(self, X, y, one_class_name=None, one_class_idx=-1):
        X, y = check_X_y(X, y, accept_sparse=True,
                         multi_output=False)
        self.unlabeled = (y == -1)
        self.is_labeled = True

        self.X = X
        #flat_y = [v for l in self.y for v in l] if self.is_multilabel else y
        flat_y = y
        self.sle = SemiLabelEncoder(
            one_class_name=one_class_name, one_class_idx=one_class_idx)
        self.y = self.sle.fit_transform(flat_y)
        self.inv_y = self.sle.inverse_transform(self.y)
        #self.Xc = X.tocsc()
        self.ndocs, self.nwords = X.shape

        if not self.is_fitted_:
            self._init_supervised_matrices()
        self.bgp()
        self.components_ = np.exp(self.log_B.T)
        self.is_fitted_ = True

        self.create_transduction()

        return self

    def create_transduction(self):
        # set the transduction item
        # key=idx, value=class_id
        self.transduction_ = np.array([self.inv_y.get(idx, -1)
                                       for idx in np.argmax(self.log_A, axis=1)])

    def transform(self, X):
        if not self.X != X:
            return np.exp(self.log_A)
        log_A_backup, X_backup, local_max_itr_backup = self.log_A, self.X, self.local_max_itr
        ndocs = X.shape[0]
        self.log_A = np.ones((ndocs, self.n_components))
        self.X = X
        self.local_max_itr = 1
        for j in range(ndocs):
            self.local_propag(j)
        transformed_log_A = self.log_A
        self.log_A, self.X, self.local_max_itr = log_A_backup, X_backup, local_max_itr_backup
        return np.exp(transformed_log_A)

    def predict(self, X):
        X = check_array(X, accept_sparse=True)
        check_is_fitted(self, 'is_fitted_')
        D = self.transform(X)
        return self._predict_multiclass(D)

    def _predict_multiclass(self, D):
        return [self.inv_y.get(v, v) for v in np.argmax(D, axis=1)]

    def _predict_multilabel(self, D):
        return D

    def bgp(self, labelled=None):
        global_niter = 0
        while global_niter < self.global_max_itr:
            self.max = 1
            for j in tqdm_notebook(range(self.ndocs), disable=self.silence, ascii=True, desc=f'docs processed (itr {global_niter})'):
                self.local_propag(j)
                if self.is_labeled and not self.unlabeled[j]:
                    self.suppress(j)
            self.global_propag()
            global_niter += 1
            if not self.silence:
                self.print_top_topics()
                if self.eval_func:
                    self.eval_func(self)

    def suppress(self, j):
        pos_id = self.y[j]
        self.log_A[j].fill(np.log(self.alpha))
        self.log_A[j][pos_id] = np.max(self.log_A)

    def print_top_topics(self, n_top_words=10, target_name=None):
        if self.feature_names is None:
            return None
        for k, topic in enumerate(self.log_B.transpose()):
            l_ = [self.feature_names[i]
                  for i in topic.argsort()[:-n_top_words - 1: -1]]
            cls_id = self.inv_y.get(k, -1) if self.is_labeled else -1
            cls_name = cls_id if cls_id != -1 else 'None'
            print(f'topic {k} [{ cls_name }] ' + ', '.join(l_))

    def get_topics(self, n_top_words=10):
        l_topics = []
        for topic in self.log_B.transpose():
            l_ = [self.feature_names[i]
                  for i in topic.argsort()[:-n_top_words - 1:-1]]
            l_topics.append(l_)
        return l_topics


import schedule
import time
import os
from configs.loadconfigs import read_config
from crud import cronjobs

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from fastapi.encoders import jsonable_encoder

engine = create_engine(read_config()["APP"]["DATABASE"])
db = scoped_session(sessionmaker(bind=engine))

json = jsonable_encoder

def job():
    server_table = cronjobs.cron_get(db)
    for servers in server_table:
        servers = (json(servers))
        ip_address = servers["ip_address"]
        user_id = (servers["id"])
        state = (servers["online"])  # false
        response = os.system("ping " + ip_address)

        if response == 0:
            print(True)
            if state != True:
                print("server is back online")
                cronjobs.cron_post(db, user_id, stat=True)

        else:
            print(False)
            if state != False:
                print("server is offline")
                cronjobs.cron_post(db, user_id, stat=False)

schedule.every(20).seconds.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)


from countfiles import countfiles
import os
def dataread (orig, dest):
    abs_file_path = os.path.join(os.path.dirname(__file__), 'input.txt')
#    print (abs_file_path)
    pathlist = open(abs_file_path, 'r')
    tot_f_orig = 0
    tot_f_dest = 0
    while True:
        line = pathlist.readline()
        if not line:
            break
        a = countfiles(orig+line.strip())
        b = countfiles(dest+line.strip())
        if a[0] == b[0]:
            print('     Same number of files detected for:', line.strip())
        else:
            print('     ##############################################')
            print('     ', a[0], 'files in', a[1])
            print('     ', b[0], 'files in', b[1])
            print('     ##############################################')
        tot_f_orig += int(a[0])
        tot_f_dest += int(b[0])
    print('\n     #################################################')
    print('     ##                                             ##')
    print('     ##  Total files found in origin:      ',tot_f_orig,'  ##')
    print('     ##  Total files found in destination: ',tot_f_dest,'  ##')
    print('     ##                                             ##')
    print('     #################################################\n')
    pathlist.close()


"""A central controller computing and installing shortest paths.

In case of a link failure, paths are recomputed.
"""

import os
from cli import CLI
from networkx.algorithms import all_pairs_dijkstra

from p4utils.utils.helper import load_topo
from p4utils.utils.sswitch_thrift_API import SimpleSwitchThriftAPI

class RerouteController(object):
    """Controller for the fast rerouting exercise."""

    def __init__(self):
        """Initializes the topology and data structures."""

        if not os.path.exists('topology.json'):
            print("Could not find topology object!\n")
            raise Exception

        self.topo = load_topo('topology.json')
        self.controllers = {}
        self.connect_to_switches()
        self.reset_states()

        # Preconfigure all MAC addresses
        self.install_macs()

        # Install nexthop indices and populate registers.
        self.install_nexthop_indices()
        self.update_nexthops()

    def connect_to_switches(self):
        """Connects to all the switches in the topology."""
        for p4switch in self.topo.get_p4switches():
            thrift_port = self.topo.get_thrift_port(p4switch)
            self.controllers[p4switch] = SimpleSwitchThriftAPI(thrift_port)

    def reset_states(self):
        """Resets registers, tables, etc."""
        for control in self.controllers.values():
            control.reset_state()

    def install_macs(self):
        """Install the port-to-mac map on all switches.

        You do not need to change this.

        Note: Real switches would rely on L2 learning to achieve this.
        """
        for switch, control in self.controllers.items():
            print("Installing MAC addresses for switch '%s'." % switch)
            print("=========================================\n")
            for neighbor in self.topo.get_neighbors(switch):
                mac = self.topo.node_to_node_mac(neighbor, switch)
                port = self.topo.node_to_node_port_num(switch, neighbor)
                control.table_add('rewrite_mac', 'rewriteMac',
                                  [str(port)], [str(mac)])

    def install_nexthop_indices(self):
        """Install the mapping from prefix to nexthop ids for all switches."""
        for switch, control in self.controllers.items():
            print("Installing nexthop indices for switch '%s'." % switch)
            print("===========================================\n")
            control.table_clear('ipv4_lpm')
            for host in self.topo.get_hosts():
                subnet = self.get_host_net(host)
                index = self.get_nexthop_index(host)
                control.table_add('ipv4_lpm', 'read_port',
                                  [subnet], [str(index)])

    def get_host_net(self, host):
        """Return ip and subnet of a host.

        Args:
            host (str): The host for which the net will be retruned.

        Returns:
            str: IP and subnet in the format "address/mask".
        """
        gateway = self.topo.get_host_gateway_name(host)
        return self.topo.get_intfs()[host][gateway]['ip']

    def get_nexthop_index(self, host):
        """Return the nexthop index for a destination.

        Args:
            host (str): Name of destination node (host).

        Returns:
            int: nexthop index, used to look up nexthop ports.
        """
        # For now, give each host an individual nexthop id.
        host_list = sorted(list(self.topo.get_hosts().keys()))
        return host_list.index(host)

    def get_port(self, node, nexthop_node):
        """Return egress port for nexthop from the view of node.

        Args:
            node (str): Name of node for which the port is determined.
            nexthop_node (str): Name of node to reach.

        Returns:
            int: nexthop port
        """
        return self.topo.node_to_node_port_num(node, nexthop_node)

    def failure_notification(self, failures):
        """Called if a link fails.

        Args:
            failures (list(tuple(str, str))): List of failed links.
        """
        self.update_nexthops(failures=failures)

    # Helpers to update nexthops.
    # ===========================

    def dijkstra(self, failures=None):
        """Compute shortest paths and distances.

        Args:
            failures (list(tuple(str, str))): List of failed links.

        Returns:
            tuple(dict, dict): First dict: distances, second: paths.
        """
        graph = self.topo

        if failures is not None:
            graph = graph.copy()
            for failure in failures:
                graph.remove_edge(*failure)

        # Compute the shortest paths from switches to hosts.
        dijkstra = dict(all_pairs_dijkstra(graph, weight='weight'))

        distances = {node: data[0] for node, data in dijkstra.items()}
        paths = {node: data[1] for node, data in dijkstra.items()}

        return distances, paths

    def compute_nexthops(self, failures=None):
        """Compute the best nexthops for all switches to each host.

        Optionally, a link can be marked as failed. This link will be excluded
        when computing the shortest paths.

        Args:
            failures (list(tuple(str, str))): List of failed links.

        Returns:
            dict(str, list(str, str, int))):
                Mapping from all switches to subnets, MAC, port.
        """
        # Compute the shortest paths from switches to hosts.
        all_shortest_paths = self.dijkstra(failures=failures)[1]

        # Translate shortest paths to mapping from host to nexthop node
        # (per switch).
        results = {}
        for switch in self.controllers:
            switch_results = results[switch] = []
            for host in self.topo.get_hosts():
                try:
                    path = all_shortest_paths[switch][host]
                except KeyError:
                    print("WARNING: The graph is not connected!")
                    print("'%s' cannot reach '%s'." % (switch, host))
                    continue
                nexthop = path[1]  # path[0] is the switch itself.
                switch_results.append((host, nexthop))

        return results

    # Update nexthops.
    # ================

    def update_nexthops(self, failures=None):
        """Install nexthops in all switches."""
        nexthops = self.compute_nexthops(failures=failures)

        for switch, destinations in nexthops.items():
            print("Updating nexthops for switch '%s'." % switch)
            control = self.controllers[switch]
            for host, nexthop in destinations:
                nexthop_id = self.get_nexthop_index(host)
                port = self.get_port(switch, nexthop)
                # Write the port in the nexthop lookup register.
                control.register_write('primaryNH', nexthop_id, port)

        #######################################################################
        # Compute loop-free alternate nexthops and install them below.
        #######################################################################

        pass

if __name__ == "__main__":
    controller = RerouteController()  # pylint: disable=invalid-name
    CLI(controller)


import json, os, random, math
from collections import defaultdict
from copy import deepcopy

import torch
from torch.utils.data import Dataset
import torchvision.transforms as transforms

import numpy as np
from PIL import Image, ImageOps
from .base_dataset import BaseDataset, check_filenames_in_zipdata
from io import BytesIO

def clean_annotations(annotations):
	for anno in annotations:
		anno.pop("segmentation", None)
		anno.pop("area", None)
		anno.pop("iscrowd", None)
		anno.pop("id", None)

def make_a_sentence(obj_names, clean=False):

	if clean:
		obj_names = [ name[:-6] if ("-other" in name) else name for name in obj_names]

	caption = ""
	tokens_positive = []
	for obj_name in obj_names:
		start_len = len(caption)
		caption += obj_name
		end_len = len(caption)
		caption += ", "
		tokens_positive.append(
			[[start_len, end_len]] # in real caption, positive tokens can be disjoint, thus using list of list
		)
	caption = caption[:-2] # remove last ", "

	return caption #, tokens_positive

class LayoutDataset(BaseDataset):
	"""
	Note: this dataset can somehow be achieved in cd_dataset.CDDataset
	Since if you donot set prob_real_caption=0 in CDDataset, then that
	dataset will only use detection annotations. However, in that dataset,
	we do not remove images but remove boxes.

	However, in layout2img works, people will just resize raw image data into 256*256,
	thus they pre-calculate box size and apply min_box_size before min/max_boxes_per_image.
	And then they will remove images if does not follow the rule.

	These two different methods will lead to different number of training/val images.
	Thus this dataset here is only for layout2img.

	"""
	def __init__(self,
                image_root,
				instances_json_path,
				stuff_json_path,
				category_embedding_path,
				fake_caption_type = 'empty',
                image_size=256,
                max_samples=None,
                min_box_size=0.02,
                min_boxes_per_image=3,
                max_boxes_per_image=8,
                include_other=False,
				random_flip=True
                ):
		super().__init__(random_crop=None, random_flip=None, image_size=None) # we only use vis_getitem func in BaseDataset, donot use the others.

		assert fake_caption_type in ['empty', 'made']
		self.image_root = image_root
		self.instances_json_path = instances_json_path
		self.stuff_json_path = stuff_json_path
		self.category_embedding_path = category_embedding_path
		self.fake_caption_type = fake_caption_type
		self.image_size = image_size
		self.max_samples = max_samples
		self.min_box_size = min_box_size
		self.min_boxes_per_image = min_boxes_per_image
		self.max_boxes_per_image = max_boxes_per_image
		self.include_other = include_other
		self.random_flip = random_flip

		self.transform = transforms.Compose([transforms.Resize( (image_size, image_size) ),
											 transforms.ToTensor(),
											 transforms.Lambda(lambda t: (t * 2) - 1) ])

		# Load all jsons
		with open(instances_json_path, 'r') as f:
			instances_data = json.load(f) # keys: 'info', 'images', 'licenses', 'categories', 'annotations'
		clean_annotations(instances_data["annotations"])
		self.instances_data = instances_data

		with open(stuff_json_path, 'r') as f:
			stuff_data = json.load(f) # keys: 'info', 'images', 'licenses', 'categories', 'annotations'
		clean_annotations(stuff_data["annotations"])
		self.stuff_data = stuff_data

		# Load preprocessed name embedding
		self.category_embeddings = torch.load(category_embedding_path)
		self.embedding_len = list( self.category_embeddings.values() )[0].shape[0]

		# Misc
		self.image_ids = [] # main list for selecting images
		self.image_id_to_filename = {} # file names used to read image
		self.image_id_to_size = {} # original size of this image
		assert instances_data['images'] == stuff_data["images"]
		for image_data in instances_data['images']:
			image_id = image_data['id']
			filename = image_data['file_name']
			width = image_data['width']
			height = image_data['height']
			self.image_ids.append(image_id)
			self.image_id_to_filename[image_id] = filename
			self.image_id_to_size[image_id] = (width, height)

		# All category names (including things and stuff)
		self.things_id_list = []
		self.stuff_id_list = []
		self.object_idx_to_name = {}
		for category_data in instances_data['categories']:
			self.things_id_list.append( category_data['id'] )
			self.object_idx_to_name[category_data['id']] = category_data['name']
		for category_data in stuff_data['categories']:
			self.stuff_id_list.append( category_data['id'] )
			self.object_idx_to_name[category_data['id']] = category_data['name']
		self.all_categories = [   self.object_idx_to_name.get(k, None) for k in range(183+1) ]

		# Add object data from instances and stuff
		self.image_id_to_objects = defaultdict(list)
		self.select_objects( instances_data['annotations'] )
		self.select_objects( stuff_data['annotations'] )

		# Prune images that have too few or too many objects
		new_image_ids = []
		for image_id in self.image_ids:
			num_objs = len(self.image_id_to_objects[image_id])
			if self.min_boxes_per_image <= num_objs <= self.max_boxes_per_image:
				new_image_ids.append(image_id)
		self.image_ids = new_image_ids

		# Check if all filenames can be found in the zip file
		all_filenames = [self.image_id_to_filename[idx] for idx in self.image_ids]
		check_filenames_in_zipdata(all_filenames, image_root)

	def select_objects(self, annotations):
		for object_anno in annotations:
			image_id = object_anno['image_id']
			_, _, w, h = object_anno['bbox']
			W, H = self.image_id_to_size[image_id]
			box_area = (w * h) / (W * H)
			box_ok = box_area > self.min_box_size
			object_name = self.object_idx_to_name[object_anno['category_id']]
			other_ok = object_name != 'other' or self.include_other
			if box_ok and other_ok:
				self.image_id_to_objects[image_id].append(object_anno)

	def total_images(self):
		return len(self)

	def __getitem__(self, index):
		if self.max_boxes_per_image > 99:
			assert False, "Are you sure setting such large number of boxes?"

		out = {}

		image_id = self.image_ids[index]
		out['id'] = image_id

		flip = self.random_flip and random.random()<0.5

		# Image
		filename = self.image_id_to_filename[image_id]
		zip_file = self.fetch_zipfile(self.image_root)
		image = Image.open(BytesIO(zip_file.read(filename))).convert('RGB')
		WW, HH = image.size
		if flip:
			image = ImageOps.mirror(image)
		out["image"] = self.transform(image)

		this_image_obj_annos = deepcopy(self.image_id_to_objects[image_id])

		# Make a sentence
		obj_names = [] # used for make a sentence
		boxes = torch.zeros(self.max_boxes_per_image, 4)
		masks = torch.zeros(self.max_boxes_per_image)
		positive_embeddings = torch.zeros(self.max_boxes_per_image, self.embedding_len)
		for idx, object_anno in enumerate(this_image_obj_annos):
			obj_name = self.object_idx_to_name[ object_anno['category_id']  ]
			obj_names.append(obj_name)
			x, y, w, h = object_anno['bbox']
			x0 = x / WW
			y0 = y / HH
			x1 = (x + w) / WW
			y1 = (y + h) / HH
			if flip:
				x0, x1 = 1-x1, 1-x0
			boxes[idx] = torch.tensor([x0,y0,x1,y1])
			masks[idx] = 1
			positive_embeddings[idx] = self.category_embeddings[obj_name]

		if self.fake_caption_type == 'empty':
			caption = ""
		else:
			caption = make_a_sentence(obj_names, clean=True)

		out["caption"] = caption
		out["boxes"] = boxes
		out["masks"] = masks
		out["positive_embeddings"] = positive_embeddings

		return out

	def __len__(self):
		if self.max_samples is None:
			return len(self.image_ids)
		return min(len(self.image_ids), self.max_samples)


from bottle import get, post, route, run, template, request, re, static_file, error
from operator import itemgetter

import os
import bottle as bottle
import json
import httplib2
import ast
from collections import OrderedDict

import requests

from beaker.middleware import SessionMiddleware

# Google Authentication
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import flow_from_clientsecrets
from googleapiclient.errors import HttpError
from googleapiclient.discovery import build

import redis
import copy

import shunting_yard_algorithm

# session management
session_opts = {
    'session.type': 'file',
    'session.cookie_expires': 300,
    'session.data_dir': './data',
    'session.auto': True
}
app = SessionMiddleware(bottle.app(), session_opts)

user_keywords = ''
# global variable hash to store how many times each word has appeared in all the searches in current session
word_occurence_history={}

# global variable hash to store the most recent searches by this user
recent_search_history=[]

# global variable containing a directory path to a particular user's word occurence history
search_history_path="search_history/"

recent_history_path="recent_history/"

retrieved_list_of_urls = {}

# callback function for GET http request. It returns template/homepage.tpl as the view for the root page
@get('/')
def search():
  global word_occurence_history
  global recent_search_history
  session = request.environ.get('beaker.session')
  if 'logged_in' in session:
    session['logged_in'] = False
    session['name'] = 'Anonymous'
    session['email'] = ''
    session['picture'] = ''
    if session['logged_in'] == True:
      # read user search history from file
      if (os.path.isfile(search_history_path+session['email'])):
        f = open(search_history_path+session['email'],"r")
        contents = f.read()
        search_history_from_file = json.loads(contents)
        word_occurence_history = search_history_from_file

      # read user's recent history from file
      mylist=[]
      if (os.path.isfile(recent_history_path+session['email'])):
        with open(recent_history_path+session['email'], 'r') as f:
          mylist = ast.literal_eval(f.read())
        print("recent history list: ",mylist)
        recent_search_from_file = mylist
        recent_search_history = recent_search_from_file
        print(recent_search_history)

    user_info = { 'logged_in': session['logged_in'], 'name': session['name'], 'picture_path': session['picture'] }

    return template('homepage', user_info = user_info)
  else:
    session['logged_in'] = False
    session['name'] = 'Anonymous'
    session['email'] = ''
    session['picture'] = ''

    bottle.redirect('/')

# sign-in page
@route('/signin', 'GET')
def home():
  flow = flow_from_clientsecrets("client_secrets.json",scope='https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile', redirect_uri="http://localhost:80/redirect",prompt='consent')

  # if already authorized, uri contains code. If node it conains access_denied
  uri = flow.step1_get_authorize_url()
  bottle.redirect(str(uri))

@route('/redirect')
def redirect_page():
  code = request.query.get('code', '')

  # parse json to get client id and client secret
  with open('client_secrets.json','r') as f:
    data = json.load(f)

  CLIENT_ID = data["web"]["client_id"]
  CLIENT_SECRET = data["web"]["client_secret"]
  REDIRECT_URI = data["web"]["redirect_uris"][0]

  # get access token
  flow = OAuth2WebServerFlow(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope='https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile', redirect_uri=REDIRECT_URI)
  credentials = flow.step2_exchange(code)
  token = credentials.id_token['sub']

  http = httplib2.Http()
  http = credentials.authorize(http)

  # add user info into the session
  users_service = build('oauth2', 'v2', http=http)
  user_document = users_service.userinfo().get().execute()

  session = request.environ.get('beaker.session')
  session['logged_in'] = True
  session['name'] = user_document['name']
  session['email'] = user_document['email']
  session['picture'] = user_document['picture']

  bottle.redirect('/')

# delete the user's session and redirect back to the home page
@route('/signout', 'GET')
def signout():
  global word_occurence_history
  session = request.environ.get('beaker.session')
  session.delete()
  word_occurence_history = {}
  recent_search_history = []
  bottle.redirect('/')

# callback function for POST http request. It's the result page after form submission and returns template/resultpage.tpl
@post('/results')
def do_search():
  global recent_search_history, user_keywords
  # get the raw input from user using an html form
  user_keywords = request.forms.get('keywords')

  session = request.environ.get('beaker.session')

  user_info = { 'logged_in': session['logged_in'], 'name': session['name'], 'picture_path': session['picture'] }
  query_word_occurence = {}
  sorted_words = []
  recent_search_list = []
  retrieved_list_of_urls = {
    'current_page_num': 0,
    'total_page_num': 0,
    'url_results_info': []
  }
  calculation = None

  is_search_a_calculation_expression = re.search(r'[^+/*()\-\d\s]+', user_keywords) == None

  if (is_search_a_calculation_expression):
    calculation = shunting_yard_algorithm.evaluate(user_keywords)
  else:
    # parse words to remove special characters and split into a list of individual words
    words = parseQueryIntoWordList(user_keywords)
    #only search the first word for lab3
    first_word = words[0]

    # if user logged in, display search history and store current search results
    # if session['logged_in'] == True:
      # input words from current search into the global variable word_occurence_history
      # inputWordsInOccurrenceHistory(query_word_occurence)

      # input words from current search into the global variable recent_search_history
      # this function also removes duplicates from recent_search_history
      # inputWordsInRecentHistory(words)

      #get 10 words from recent_search_list from global var recent_search_history
      # recent_search_list = recent_search_history[:10]

      # return the top 20 most frequently searched words in word_occurence_history
      # sorted_words = getTop10KeywordsDescending()
    # else:
      # query_word_occurence = {}
      # sorted_words = []
      # recent_search_list = []

    #retrieve the needed data structures from database
    r_server = redis.Redis("localhost")
    page_rank_score = r_server.hgetall("page_rank_score")
    document_index = r_server.hgetall("document_index")

    #find pages that contain this word
    #returns a list of hashes
    search_results = find_matching_pages(words, first_word, document_index, page_rank_score)

    #rank results by descending order or PageRank score
    sorted_search_results = sorted(search_results, key=itemgetter('pagerank'), reverse=True)

    #divide results into a list of chunks of 5
    chunks = [sorted_search_results[x:x+5] for x in xrange(0, len(sorted_search_results), 5)]

    global retrieved_list_of_urls

    retrieved_list_of_urls = {
      'current_page_num': 0,
      'total_page_num': len(chunks),
      'url_results_info': chunks
    }

  # use template/resultpage.tmp as the view for the search results page
  return template('resultpage', user_info = user_info, keywords = user_keywords, recent_search_list = recent_search_list, sorted_words = sorted_words, query_word_occurence = query_word_occurence, retrieved_list_of_urls = retrieved_list_of_urls, is_search_a_calculation_expression = is_search_a_calculation_expression, calculation = calculation)

#display search results on each page
@get('/paginate_results/<page_num>')
def paginate_results(page_num):
  global retrieved_list_of_urls
  retrieved_list_of_urls['current_page_num'] = int(page_num)

  session = request.environ.get('beaker.session')
  user_info = { 'logged_in': session['logged_in'], 'name': session['name'], 'picture_path': session['picture'] }

  return template('resultpage', user_info = user_info, keywords = user_keywords, recent_search_list = [], sorted_words = [], query_word_occurence = {}, retrieved_list_of_urls = retrieved_list_of_urls)

# parse words to remove special characters and split into a list of individual words
def parseQueryIntoWordList(query):
  # remove any character that's not a char, space
  keywords_without_special_char = re.sub(r'[^\w\s]', ' ', query)
  # split the words and store into a list
  words = keywords_without_special_char.split()

  return words

#Find pages that contain the word being searched
def find_matching_pages(words, first_word, document_index, page_rank_score):
  search_results = []
  #iterate through all documents
  for doc_id in document_index:
    #get current document_index object
    doc_id_object_str = document_index[doc_id]
    #convert it to a hash (originally it's a string)
    doc_id_object = ast.literal_eval(doc_id_object_str)

    #get the title of the page
    title = doc_id_object['title']
    word = title

    #remove unicode character
    word = word.replace('u','')
    word = word.replace("'",'')
    doc_id_object['title']=word
    #if word is in the list of words
    # if (first_word in doc_id_object['title'] or first_word in doc_id_object['words']):
    #  if doc_id in page_rank_score:
    #    doc_id_object['pagerank'] = float(page_rank_score[str(doc_id)])
    #  else:
    #    doc_id_object['pagerank'] = 0.0

    #  doc_id_object['description'] = ' '.join(doc_id_object['words'][:20])
    #  search_results.append(doc_id_object)

    #new code
    #duplicate words, this will be shrunken
    temp_words = list(words)
    found = False
    page_rank_boost_factor = 10*len(temp_words) #"stepped tea double double" would have initial factor 40
    #check if words or a subset of the words is inside the current page.
    while (len(temp_words)!=0):
      if (page_has_all_words(temp_words, doc_id_object)):
        if doc_id in page_rank_score:
          doc_id_object['pagerank'] = page_rank_boost_factor*float(page_rank_score[str(doc_id)])
        else:
          doc_id_object['pagerank'] = 0.0

        doc_id_object['description'] = ' '.join(doc_id_object['words'][:20])
        #if found, add this document and break to go on to the next one
        search_results.append(doc_id_object)
        found = True
        break
      #remove last word in words and continue the search
      del temp_words[-1]
      page_rank_boost_factor -= 10

    #if not found above, check every word individually
    if (not found):
      for cur_word in words:
        #found
        if (cur_word in doc_id_object['title'] or cur_word in doc_id_object['words']):
          if doc_id in page_rank_score:
            #keep original page_rank_score
            doc_id_object['pagerank'] = float(page_rank_score[str(doc_id)])
          else:
            doc_id_object['pagerank'] = 0.0

          doc_id_object['description'] = ' '.join(doc_id_object['words'][:20])
          search_results.append(doc_id_object)
          #words found in doc and added to search_results, break. done with this doc
          break

  return search_results

#Check if a page has all the words being searched
def page_has_all_words(words, doc_id_object):
  for word in words:
    if (word not in doc_id_object['words']):
      return False

  return True

# populate query_word_occurence with number of appearances of each word in current search
def findWordOccurenceInQuery(words):
  query_word_occurence = {}

  for word in words:
    if word in query_word_occurence:
        query_word_occurence[word] += 1
    else:
        query_word_occurence[word] = 1

  return query_word_occurence

# input words from current search into the global variable recent_search_history
def inputWordsInRecentHistory(words):
  global recent_search_history
  global recent_history_path

  for word in words:
    recent_search_history.insert(0,word)

  # store recent search history in file
  # only called if a user is logged in
  # local@domain.com__history
  session = request.environ.get('beaker.session')
  email = session['email']

  #remove duplicates in recent_history before writing to file
  s=list(OrderedDict.fromkeys(recent_search_history))
  recent_search_history=s

  # write word occurence history to file. The file will never have duplicates
  f = open(recent_history_path+email,"w")
  f.write(str(recent_search_history))

# input words from current search into the global variable word_occurence_history
def inputWordsInOccurrenceHistory(word_occurrence):
  global word_occurence_history
  global search_history_path

  for word in word_occurrence:
    if word in word_occurence_history:
        word_occurence_history[word] += word_occurrence[word]
    else:
        word_occurence_history[word] = word_occurrence[word]

  # store occurence history in file
  # only called if a user is logged in
  # local@domain.com__history
  session = request.environ.get('beaker.session')
  email = session['email']

  # write word occurence history to file
  f = open(search_history_path+email,"w")
  f.write(str(json.dumps(word_occurence_history)))

# return the top 10 most frequently searched words in word_occurence_history
def getTop10KeywordsDescending():
  return sorted(word_occurence_history.items(), key=itemgetter(1), reverse=True)[:10]

@error(404)
def error404(error):
  session = request.environ.get('beaker.session')
  if 'logged_in' not in session:
    session['logged_in'] = False

  return "
ERROR 404: PAGE NOT FOUND

 Go back to results page"

@error(500)
def error500(error):
  session = request.environ.get('beaker.session')
  if 'logged_in' not in session:
    session['logged_in'] = False

  return "
ERROR 500: INTERNAL SERVER ERROR

 Go back to results page"

# route for static files. Need this callback to specify which files to be served and where to find them
@route('/static/<filename>')
def server_static(filename):
  return static_file(filename, root='./static_files')

run (host='0.0.0.0', port=80, debug=True, app=app)