import copy
import socket
from jsonschema import validate, FormatChecker, ValidationError
from abc import ABCMeta, abstractmethod
import re
import six

__tp__ = 'transport_params'

@six.add_metaclass(ABCMeta)
class AbstractDevice:
    def __init__(self, logger):
        self.staged = {}
        self.active = {}
        self.callback = None
        self.stageLocked = False
        self.logger = logger
        self.staged['master_enable'] = False
        self.staged['receiver_id'] = None
        self.staged['sender_id'] = None

    def lock(self):
        """Prevents any updates to staged parameters"""
        self.stageLocked = True

    def unLock(self):
        """Allows updates to staged parameters"""
        self.stageLocked = False

    def setActivateCallback(self, callback):
        self.callback = callback

    def activateStaged(self):
        oldParams = copy.deepcopy(self.active)
        self.active = copy.deepcopy(self.resolveParameters(self.staged))
        self.unLock()
        if self.callback is not None:
            try:
                self.logger.writeDebug("Activation suceeded")
                self.callback()
            except Exception as e:
                self.logger.writeWarning("Activation failed, reverting to old params. {}".format(e))
                self.active = copy.deepcopy(oldParams)
                raise

    def setMasterEnable(self, masterEnable):
        if self.stageLocked:
            raise StagedLockedException()
        self.staged['master_enable'] = masterEnable

    def setSenderId(self, senderId):
        if self.stageLocked:
            raise StagedLockedException()
        if senderId is None:
            self.staged['sender_id'] = None
        else:
            pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            if re.match(pattern, senderId):
                self.staged['sender_id'] = senderId
            else:
                raise ValidationError("Invalid sender id")

    def setReceiverId(self, receiverId):
        if self.stageLocked:
            raise StagedLockedException()
        if receiverId is None:
            self.staged['receiver_id'] = None
        else:
            pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            if re.match(pattern, receiverId):
                self.staged['receiver_id'] = receiverId
            else:
                raise ValidationError("Invalid sender id")

    def patch(self, updateObject):
        """Update based on a patch object"""
        for leg in range(0, self.legs):
            if not self.stageLocked:
                toCheck = []
                toCheck.append(updateObject[leg])
                schema = self.getParamsSchema(leg)
                checker = FormatChecker(["ipv4", "ipv6"])
                validate(updateObject, schema, format_checker=checker)
                self._updateTransportParamerters(updateObject, self.staged)
                return True
            else:
                raise StagedLockedException()

    def stagedToJson(self):
        return self._assembleJsonDescription(self.staged)

    def activeToJson(self):
        return self._assembleJsonDescription(self.active)

    def setStagedParameter(self, value, parameter, leg=0):
        """Set the value of a staged parameter"""
        if parameter in self.staged[__tp__][leg]:
            self.staged[__tp__][leg][parameter] = value
        else:
            raise ValueError

    def getStagedParameter(self, parameter, leg=0):
        """Get the value of a staged parameter"""
        return self.staged[__tp__][leg][parameter]

    def setActiveParameter(self, value, parameter, leg=0):
        """Set the value of an active parameter"""
        if parameter in self.active[__tp__][leg]:
            self.active[__tp__][leg][parameter] = value
        else:
            raise ValueError

    def getActiveParameter(self, parameter, leg=0):
        """Get the value of an active parameter"""
        return self.active[__tp__][leg][parameter]

    def _updateTransportParamerters(self, updateObject, dest):
        """Merge in transport parameters"""
        leg = 0
        for tp in updateObject:
            for key, value in tp.items():
                if key in dest[__tp__][leg]:
                    dest[__tp__][leg][key] = updateObject[leg][key]
            leg += 1

    def _checkIsIpv4(self, addr):
        """Checks a given address is ipv4"""
        try:
            # Check if it's IPv4
            socket.inet_pton(socket.AF_INET, addr)
            return True
        except AttributeError:
            # Looks like we can't use pton here...
            try:
                socket.inet_aton(addr)
            except socket.error:
                return False
        except socket.error:
            return False
        return True

    def _checkIsIpv6(self, addr):
        """Checks a given address is ipv6. Not win friendly. Sorry..."""
        try:
            socket.inet_pton(socket.AF_INET6, addr)
        except socket.error:
            return False
        except AttributeError:
            """If you're finding this program returning here your system does
            not support inet_pton (is probably running windows). In Python 2.7
            about the only way round this is to use a bunch of regex..."""
            return False
        return True

    @abstractmethod
    def getConstraints(self):
        pass

    @abstractmethod
    def getParamsSchema(self):
        pass

    @abstractmethod
    def getTransportType(self):
        pass

class StagedLockedException(Exception):
    pass


from time import time
import requests
import json
from flask_mail import Mail, Message
# from app import send_emails

def email_control(mail):
    # #Retrieve start time in time.json
    # filename = "time.json"
    # with open(filename, 'r') as f:
    #     data = json.load(f)
    #     #if first time, set start time to now
    #     print(f"DATA: {data}")
    #     if data["start"] == -1:
    #         data["start"] = time()
    #         start = time()
    #     else: #if not first time, retrieve start time
    #         start = data["start"] + time()

    # timeElapsed = int(start - time()) #turn into int
    # print(f"TIME ELAPSED IN EMAIL CONTROL: {timeElapsed}")
    # if timeElapsed > 10_000_000_000:
    #     #reset time elapsed to 0
    #     data["start"] = 0
    #     #Update email_list.json based on AQI and asthma/age/etc
    #     updateEmailList()

    #     #Email everyone on the email list

    # #Update time.json (if not needed it just rewrites what was there before)
    # with open(filename, 'w') as f:
    #     data = json.dump(data, f)
    updateEmailList()
    send_emails(mail)

#Update email_list.json based on thresholds for each user in users.json
def updateEmailList():
    #Open email_list.json
    email_file = "email_list.json"
    with open(email_file, 'r') as efile:
        email_users = json.load(efile)
        #Retrieve users from users.json
        filename = "users.json"
        with open(filename, 'r') as ufile:
            data = json.load(ufile)

        print("BEFORE FOR LOOP IN UPDATE EMAIL LIST")
        for user in data:
            info = data[user]
            print(f"USER: {info}")
            if info["threshold"] == 0 and user in email_users["users"]:
                email_users["users"].remove(user)
            elif info["threshold"] == 1 and user not in email_users["users"]:
                email_users["users"].append(user)

    #update email list
    with open(email_file, 'w') as efile:
        json.dump(email_users, efile)

def send_emails(mail):
    efile = "email_list.json"
    userfile = "users.json"
    with open(efile, 'r') as f1:
        email_data = json.load(f1)
    with open(userfile, 'r') as f:
        userinfo = json.load(f)
        while len(email_data["users"]) > 0:
            user = email_data["users"].pop()
            print(f"USEEEEER: {user}")
            msg = f"Hello {user}, this is a warning that the AQI is {userinfo[user]['AQI']} which is above your threshold"
            message = Message(msg, sender = 'climathon2023@gmail.com', recipients = [user])
            message.body = msg

            mail.send(message)
            #send message to arduino
            sensor_index = userinfo[user]['closest sensor']
            aqi_value = userinfo[user]['AQI']
            color = "green"
            user_thresh = userinfo[user]['threshold']
            response = requests.get("https://script.google.com/macros/s/AKfycbxoUijwJgmVLw9hRk0gmyLqhoY6-WeT5-dgSkSsnAwlWG8355ONiU5XLgG-BxmW1exA/exec?request=set&sensor_index=" + str(sensor_index) + "&aqi=" + str(aqi_value) + "&color=" + color + "&user_thresh=" + str(user_thresh))

    with open(efile, 'w') as f2:
        # empty = {"users" : []}
        json.dump(email_data, f2)

    # updateinfo(email_data)

# def updateinfo(email_data):
#     with open("email_list.json", 'w') as f:
#         json.dump(email_data, f)


# coding: utf-8

import os
from setuptools import setup, Command

class CleanCommand(Command):
    """Custom clean command to tidy up the project root."""
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        os.system('rm -vrf ./build ./dist ./*.pyc ./*.tgz ./*.egg-info')

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='django-explain',
    version='1.1.1',
    py_modules=[
        'django_explain',
    ],
    include_package_data=True,
    install_requires=['Django>=1.7', 'requests', 'sqlparse'],
    license='MIT License',
    description='A helper to get EXPLAIN and EXPLAIN ANALYZE OUTPUT for django queryset.',
    author='Egor Orlov',
    author_email='oeegor@gmail.com',
    platforms='any',
    url='https://github.com/oeegor/django-explain',
    classifiers=[
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.5',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
    cmdclass={
        'clean': CleanCommand,
    },
)


from datetime import datetime
import pytest
from unittest.mock import MagicMock, patch, PropertyMock

from __app__.processor.adscorer import score_ad
from __app__.processor.scorers.functions import (
    frequency_scores,
    frequency_score_helper,
    twilio_score,
)
from tests.fixtures.no_network import *
from tests.fixtures.spam_cache import test_cache

@patch("__app__.processor.scorers.functions.twilio_request")
@pytest.mark.parametrize(
    "pnumber, tstatus, tscore, expected_score",
    [
        ("+1-347-687-5904", "successful", 0.1, 0.1),
        ("+1-347-687-5904", "successful", 0.2, 0.2),
        ("+1-347-687-5905", "failure", None, None),
        ("+1-347-687-5906", "successful", 0.5, 0.5),
        (None, "successful", None, None),
    ],
)
def test_twilio_result(
    mock_twilio, test_cache, pnumber, tstatus, tscore, expected_score
):
    msg = {"primary-phone-number": pnumber}
    mock_twilio_resp = MagicMock()
    mock_twilio_resp.add_ons = {
        "status": tstatus,
        "results": {"truecnam_truespam": {"result": {"spam_score": tscore}}},
    }

    mock_twilio.return_value = mock_twilio_resp

    score = twilio_score(msg, "TEST_SID", "TEST_TOKEN", test_cache)
    assert score == expected_score
    assert (
        test_cache.get_cached_score(
            phone_number=msg["primary-phone-number"], score_key="truespam"
        )
        == expected_score
    )

def test_twilio_cached_result(test_cache):
    msg = {"primary-phone-number": "+1-347-687-5904"}
    test_cache.put_cached_score(
        phone_number=msg["primary-phone-number"], score_key="truespam", score=10
    )

    score = twilio_score(msg, "TEST_SID", "TEST_TOKEN", test_cache)
    assert score == 10


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
import re
import os
import utils as ut

runCppCode = 0
# Set to false if you have the results files and just want to plot the results.
if runCppCode == True:
    # Compile and run the C++ files (this is exactly what is in the makefile):
    os.system("echo compiling C++ codes...")
    #os.system("g++ -O3 -o main.out ../cpp_codes/main.cpp ../cpp_codes/utils.cpp ../cpp_codes/ising_solver.cpp -larmadillo")
    os.system("g++ -O3 -o main.out ../cpp_codes/main.cpp ../cpp_codes/utils.cpp -larmadillo -fopenmp")
    os.system("echo executing...")
    os.system("./main.out")

#data = np.loadtxt(directory + filename, delimiter=",")
#data = pd.DataFrame(data)

df_u_xt = pd.read_csv("../results/1D_diffusion/analytical_1D.csv", sep=',',header=None)
u_xt_array = np.array(df_u_xt.values)
df_v_xt = pd.read_csv("../results/1D_diffusion/v_xt.csv", sep=',',header=None)
v_xt_array = np.array(df_v_xt.values)

# Get the axis values (time t and space x):
df_xList = pd.read_csv("../results/1D_diffusion/analytical_1D_xList.csv", sep=',',header=None)
df_tList = pd.read_csv("../results/1D_diffusion/analytical_1D_tList.csv", sep=',',header=None)
xList = np.array(df_xList.values)
tList = np.array(df_tList.values)

X, T = np.meshgrid(xList, tList)
#plt.pcolormesh(dataFrame)
#plt.pcolor(xList, tList, u_xt_array)#, cmap=cm)

#plotCodeWord = 'colormesh'
#plotCodeWord = 'oneFrame' # Plots u for one specified time.
plotCodeWord = 'animate' # Animates the time steps using matplotlib.animate.
t_index = 10 # Which timestep should be plotted?

if plotCodeWord == 'colormesh':
    plt.pcolormesh(X, T, u_xt_array)#, cmap=cm)
    #plt.pcolormesh(u_xt_array)
    #plot1, = plt.plot(xList, v_t_list)
    #plt.legend(plot1, )
    plt.legend()
    plt.grid()
    plt.ylabel(r'$t$')

elif plotCodeWord == 'oneFrame':
    t = tList[t_index]
    u_xt_list = u_xt_array[t_index, :] # u(x,t) at the specific time t.
    v_xt_list = v_xt_array[t_index, :]

    minus_f_x_list = (-ut.f(xList,1))[:,0]

    u_xt_list_simple_sum = v_xt_list + minus_f_x_list

    #plt.plot(xList, u_xt_list, label=r'$u(x,t) = v(x,t)+x/L$')
    plt.plot(xList, v_xt_list, label=r'$v(x,t)$', color='b')
    plt.plot(xList, -ut.f(xList,1), label=r'$x/L = -f(x)$', color='y')
    plt.plot(xList, u_xt_list_simple_sum, label=r'$u(x,t) = v(x,t)+x/L$', color='r')
    plt.ylabel("u(x,t=" + format(float(t), '.7f') + ")")

elif plotCodeWord == 'animate':
    fig, ax = plt.subplots()
    xdata, ydata = [], []
    line, = plt.plot([], [], 'ro')

    def init():
        ax.set_xlim(-0.01, 1.01)
        #ax.set_ylim(-1, 40)
        ax.set_ylim(-1, v_xt_array.max()*1.3)
        return line,

    print('tList:'); print(tList)

    def update(frame): # frame = t_index basically
        t = tList[frame]

        u_xt_list = u_xt_array[frame, :] # u(x,t) at the specific time t.
        #u_xt_list = u_xt_array[frame+1, :]

        v_xt_list = v_xt_array[frame, :]
        minus_f_x_list = (-ut.f(xList,1))[:,0]
        u_xt_list_simple_sum = v_xt_list + minus_f_x_list

        xdata.append(t)
        #ydata.append(u_xt_list_simple_sum[frame])
        ydata.append(minus_f_x_list[frame])
        line.set_data(xdata, ydata)
        return line,

    def animate(i):
        x = xList

        v_xt_list = v_xt_array[i, :]
        minus_f_x_list = (-ut.f(xList,1))[:,0]
        u_xt_list_simple_sum = v_xt_list + minus_f_x_list
        y = u_xt_list_simple_sum

        line.set_data(x, y)
        return line,
    #ani = FuncAnimation(fig, update, frames=np.array(range(len(tList))),
                        #init_func=init, blit=True)
    ani = FuncAnimation(fig, animate, init_func=init, frames=len(tList), interval=50, blit=True)
    plt.ylabel(r'$u(x,t)$')

plt.xlabel(r'$x$')
plt.suptitle('Analytical 1D solution, diffusion equation')
plt.legend()
plt.show()

if plotCodeWord=='animate': # Save the animation as a gif:
    ani.save('../results/1D_diffusion/analytical_1D.gif',writer='imagemagick')


#! /usr/bin/env python
#
#  Show graph structure in the "truth" space
#

import random
from matplotlib import pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cm

#gray = plt.get_cmap("gray")
gray_r = plt.get_cmap("gray_r")
seismic = plt.get_cmap("seismic")  #"rainbow" "jet" "bwr" "coolwarm" "seismic")

random.seed(12345678)

def rnoise(rn, N):
    return [ 1.0 + 2.0 * rn * (random.random() - 0.5) for i in range(N) ]

def show_efuns(ax, ef1, ef2, W, D):
    N, M = W.shape
    assert N == M and D.shape == (N, N)
    assert len(ef1) == N and len(ef2) == N
    D1 = [ D[i,i] for i in range(N) ]
    ax.scatter(ef1, ef2, c=D1, s=200)
    Wnorm = colors.Normalize(vmin=0.0, vmax=W.max())
    Wcolor = cm.ScalarMappable(norm=Wnorm, cmap=jet).to_rgba
    for i in range(N):
        for j in range(i+1, N):
            if W[i,j] > 0.0:
                c = W[i,j]
                xs = [ ef1[i], ef1[j] ]
                ys = [ ef2[i], ef2[j] ]
                ax.plot(xs, ys, lw=1.4, c=Wcolor(c))

def show_on_truth(ax, ef1, truth, W, rn=None):
    N, M = W.shape
    assert N == M and truth.shape == (N, 2)
    assert len(ef1) == N
    if rn:
        truth_ = [ (x*r, y*r) for (x,y), r in zip(truth, rnoise(rn, N)) ]
    else:
        truth_ = truth
    truthx, truthy = zip(*truth_)
    ##ef1 = truthx  #=truthy - a hack!
    transf = (lambda v: v)  #(lambda v: v**3)
    ef1cran = max(abs(min(ef1)), abs(max(ef1)))
    ef1cran = transf(ef1cran)
    ef1norm = colors.Normalize(vmin=-ef1cran, vmax=ef1cran)
    ef1color = (lambda v: cm.ScalarMappable(norm=ef1norm, cmap=seismic).to_rgba(transf(v)))
    ef1c = map(ef1color, ef1)
    ax.scatter(truthx, truthy, c=ef1c, s=200)
    Wnorm = colors.Normalize(vmin=0.0, vmax=W.max())
    Wcolor = cm.ScalarMappable(norm=Wnorm, cmap=gray_r).to_rgba
    for i in range(N):
        for j in range(i+1, N):
            if W[i,j] > 0.0:
                c = W[i,j]
                xs, ys = zip(truth_[i], truth_[j])
                ax.plot(xs, ys, lw=1.4, c=Wcolor(c))

if __name__ == "__main__":
    import sys, argparse, os
    import os.path as path
    import numpy as np

    parser = argparse.ArgumentParser()
    parser.add_argument("--sanity", action="store_true",
                        help="run some sanity checks on eigenvalues and eigenfunctions")
    parser.add_argument("--truth", action="store_true",
                        help="one eigenfunction on top of truth")
    parser.add_argument("--rnoise", type=float, default=None,
                        help="add some radial noise for plotting")
    parser.add_argument("dir",
                        help="data/exp dir")
    parser.add_argument("f1", type=int,
                        help="first eigenfunction")
    parser.add_argument("f2", type=int, nargs="?", default=None,
                        help="second eigenfunction")
    args = parser.parse_args()

    exp_dir = args.dir
    data_dir, exp_subdir = path.split(exp_dir)

    truth = np.loadtxt(path.join(data_dir, "truth"))
    efuns = np.loadtxt(path.join(exp_dir, "efuns"))
    evals = np.loadtxt(path.join(exp_dir, "evals"))
    W = np.loadtxt(path.join(exp_dir, "W"))
    D = np.loadtxt(path.join(exp_dir, "D"))

    if not args.truth:
        show_efuns(plt, efuns[args.f1], efuns[args.f2], W, D)
        plt.gray()
    else:
        fig, ax = plt.subplots(1, 1)
        show_on_truth(ax, efuns[args.f1], truth, W, rn=args.rnoise)
        ax.set_axis_bgcolor("lightgreen")
    plt.show()


import datetime
import pickle

class GlobalKVS(object):
    """
    (c) Aleksei Tiulpin, Unversity of Oulu, 2018-

    """
    _instance = None
    _d = dict()

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(GlobalKVS, cls).__new__(cls, *args, **kwargs)
        return cls._instance

    def update(self, tag, value, dtype=None):
        """
        Updates the internal state of the logger.
        Parameters
        ----------
        tag : str
            Tag, of the variable, which we log.
        value : object
            The value to be logged
        dtype :
            Container which is used to store the values under the tag
        Returns
        -------
        """
        if tag not in self._d:
            if dtype is None:
                self._d[tag] = (value, str(datetime.datetime.now()))
            else:
                self._d[tag] = dtype()
        else:
            if isinstance(self._d[tag], list):
                self._d[tag].append((value, str(datetime.datetime.now())))
            elif isinstance(self._d[tag], dict):
                self._d[tag].update((value, str(datetime.datetime.now())))
            else:
                self._d[tag] = (value, str(datetime.datetime.now()))

    def __getitem__(self, tag):
        if not isinstance(self._d[tag], (list, dict)):
            return self._d[tag][0]
        else:
            return self._d[tag]

    def tag_ts(self, tag):
        return self._d[tag]

    def save_pkl(self, filename):
        with open(filename, 'wb') as f:
            pickle.dump(self._d, f)


import pygame

# drawing the hole machine
def draw_enigma(enigma,  path, sc, width, heigth, mar, gap, font):
    TITLE_FONT = pygame.font.SysFont("FreeMono", 30, bold=True)
    massage_FONT = pygame.font.SysFont("FreeMono", 17, bold=True)

    # width and heigth of components
    w = (width - mar["left"] - mar["right"] - 5 * gap) / 6
    h = heigth - mar["top"] - mar["bottom"]

    # path coordinates
    y = [mar["top"]+(signal+1)*h/27 for signal in path] # y coordinates
    x = [width-mar["right"]-w/2] # keyboard

    for i in [4, 3, 2, 1, 0]: # forward pass
        x.append(mar["left"]+i*(w+gap)+w*3/4)
        x.append(mar["left"]+i*(w+gap)+w*1/4)

    x.append(mar["left"]+w/2) # reflector

    for i in [1, 2, 3, 4]: # backward pass
        x.append(mar["left"]+i*(w+gap)+w*1/4)
        x.append(mar["left"]+i*(w+gap)+w*3/4)

    x.append(width-mar["right"]-w/2) # lamboard

    # draw the path
    if len(path) > 0:
        for i in range(1, 21):
            if i < 10:
                color = "#43aa3b"
            elif i < 12:
                color = "#f9c74f"
            else:
                color = "#e63946"
            start = (x[i-1], y[i-1])
            end = (x[i], y[i])
            pygame.draw.line(sc, color, start, end, width=3)

    # base coordinates
    x = mar["left"]
    y = mar["top"]

    # draw enigma components
    for comp in [enigma.re, enigma.r1, enigma.r2, enigma.r3, enigma.pb, enigma.kb]:
        comp.draw(sc, x, y, w, h, font)
        x += w + gap

    # Applications messages

    # Application name
    x = width/2
    y = 20
    message = TITLE_FONT.render("ENIGMA SIMULATION", True, "white")
    text_box = message.get_rect(center=(x, y))
    sc.blit(message, text_box)

    # exit info
    x = width-300
    y = heigth-20
    message = massage_FONT.render("PRESS ESC BUTTON TO EXİT THE APP.", True, "white")
    text_box = message.get_rect(center=(x, y))
    sc.blit(message, text_box)

    # text cleaning info
    x = width - 300
    y = heigth - 40
    message= massage_FONT.render("PRESS BACKSPACE BUTTON TO DELLETE THE MESSAGE.", True, "white")
    text_box = message.get_rect(center=(x, y))
    sc.blit(message, text_box)

    # write the names of components
    names = ["Reflector", "Left", "Middle", "Right", "Plugboard", "Key/Lamb"]
    y = mar["top"]*0.8
    for i in range(len(names)):
        x = mar["left"] + w/2 + i*(w+gap)
        title = font.render(names[i], True, "white")
        text_box = title.get_rect(center=(x, y))
        sc.blit(title, text_box)


from model.bert import BERT
import bert_pytorch
import torch
import torch.nn as nn
from dataset.dataset import BERTDataset
from dataset.vocab import WordVocab
from torch.utils.data import DataLoader
import csv
import random
from torch.utils.data import TensorDataset, DataLoader
import numpy as np
from ENSEMBLE_BERT import DeepSEA_BERT
import os
import argparse
from sklearn.metrics import recall_score,precision_score,f1_score,accuracy_score
base_dict = {'A':0, 'G':1, 'C':2, 'T':3}
def seq2idxseq(seq, base_dict):
    idx_list = [np.zeros(len(seq)) for i in range(len(base_dict))]
    for i in range(len(seq)):
        idx_list[base_dict[seq[i]]][i] = 1
    return idx_list
def tensor_generate(data_set):
    x = torch.FloatTensor(np.array([seq2idxseq(x[0], base_dict) for x in data_set])).cuda()
    y = torch.FloatTensor(np.array([x[1] for x in data_set])).cuda()
    return x,y

def seq_split(seq):
    head = list(seq)
    return ' '.join(head)
def sentence_processing(sentence1):
    out1 = [vocab.sos_index] + vocab.to_seq(sentence1) + [vocab.eos_index]
    seg_labels = [1 for x in out1]
    return (out1, seg_labels)
def data_generate(data_set):
    seq_list= []
    label_list = []
    target_list = []
    for row in data_set:
        curr_seq = seq_split(row[0])
        bert_seq,bert_label = sentence_processing(curr_seq)
        seq_list.append(bert_seq)
        label_list.append(bert_label)
        target_list.append(row[1])
    return torch.LongTensor(seq_list).cuda(),torch.LongTensor(label_list).cuda(),torch.FloatTensor(target_list).cuda()

def test_model_out(xy_set, model, batch_size, th=0.5):
    model.eval()
    data_loader = DataLoader(dataset=xy_set,batch_size=batch_size,shuffle=True)
    true_out = []
    model_out = []
    for i,data in enumerate(data_loader):
        bert_seq,bert_label,sea,target = data
        out = model(bert_seq,bert_label,sea)
        out = out.detach().cpu().numpy()
        target = target.cpu().numpy()
        for j in range(len(target)):
            curr = 0
            if out[j] > th:
                curr = 1
            true_out.append(target[j])
            model_out.append(curr)
    return model_out, true_out

parser = argparse.ArgumentParser()
parser.add_argument("--test_corpus",
                        default=None,
                        type=str,
                        required=True,
                        help="The input test corpus.")

parser.add_argument("--batch_size",
                        default=32,
                        type=int,
                        required=False,
                        help="batch size")

parser.add_argument("--vocab",
                        default='vocab.small',
                        type=str,
                        required=False,
                        help="vocab path.")

parser.add_argument("--model_path",
                        default=None,
                        type=str,
                        required=True,
                        help="model path.")

args = parser.parse_args()

test_set = []
with open(args.test_corpus, 'r') as csvfile:
    csv_reader = csv.reader(csvfile, delimiter='\t',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    for row in csv_reader:
        test_set.append([row[1], int(row[0])])

vocab = WordVocab.load_vocab(args.vocab)

sea_test,_ = tensor_generate(test_set)

seq_list,label_list,target_list = data_generate(test_set)
test_dataset = TensorDataset(seq_list,label_list,sea_test,target_list)

classifiy_model = torch.load(args.model_path).cuda()
model_out, true_out = test_model_out(test_dataset, classifiy_model, args.batch_size)
print('Recall:' + str(recall_score(true_out, model_out)))
print('Precision:' + str(precision_score(true_out, model_out)))
print('F1:' + str(f1_score(true_out, model_out)))
print('Accuracy:' + str(accuracy_score(true_out, model_out)))


from datetime import datetime
import csv
import os
import pandas as pd
from matplotlib import pyplot as plt

def get_dict(name_dict, key_dict):
    if name_dict.get(key_dict) is None:
        name_dict[key_dict] = [f'"cid","dt","dvalue"\n"{cid}","{dt}","{dvalue}"\n']
    else:
        temp = name_dict.get(key_dict)
        temp.append(f'"{cid}","{dt}","{dvalue}"\n')
        name_dict[key_dict] = temp

def create_csv(name_folder, name_dict):
    for key, value in name_dict.items():
        with open(f'task\\data\\{name_folder}\\{key}.csv', 'w') as new_file:
            new_file.writelines(value)

def create_png(way_folder):
    for dirs, folder, files in os.walk(f'task\\data\\{way_folder}'):
        for name in files:
            df = pd.read_csv(f'task\\data\\{way_folder}\\{name}')
            df.plot(y='dvalue', use_index=True)
            plt.savefig(f'task\\img\\{way_folder}\\{name[:-4]}.png')
            plt.close()

if __name__ == '__main__':
    dayAll, dayCid, weekAll, weekCid, cidAll = dict(), dict(), dict(), dict(), dict()
    with open('data1.csv', 'r') as file:
        reader = csv.reader(file)
        for cid, dt, dvalue in [*reader][1:]:
            date_and_time = dt.split(' ')
            date = date_and_time[0]
            day, month, year = date.split('.')
            cid_and_date = f'{cid} {date}'
            week_and_year = f'{datetime.strptime(f"{date}", "%d.%m.%Y").isocalendar()[1]} {year}'
            cid_and_week = f'{cid} {datetime.strptime(f"{date}", "%d.%m.%Y").isocalendar()[1]} {year}'
            get_dict(dayAll, date)
            get_dict(dayCid, cid_and_date)
            get_dict(weekAll, week_and_year)
            get_dict(weekCid, cid_and_week)
            get_dict(cidAll, cid)
    for way, name_dict in zip(['Every day all sensors', 'Every sensor every day', 'Every week all sensors',
                               'Every sensor is weekly', 'Each sensor for the entire time period'],
                              [dayAll, dayCid, weekAll, weekCid, cidAll]):
        create_csv(way, name_dict)
        create_png(way)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (c) Shrimadhav U K & PR0FESS0R-99

import os
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message
from pyrogram.errors import FloodWait

CAPTION_TEXT = """
CHANNEL LINK ☞ https://youtube.com/c/MoTech_YT
"""

@Client.on_message(filters.media & filters.channel)
async def caption(client, message: Message):
    kopp, _ = get_file_id(message)
    await message.edit(f"{kopp.file_name}\n\n{CAPTION_TEXT}")

def get_file_id(msg: Message):
    if msg.media:
        for message_type in (
            "photo",
            "animation",
            "audio",
            "document",
            "video",
            "video_note",
            "voice",
            # "contact",
            # "dice",
            # "poll",
            # "location",
            # "venue",
            "sticker"
        ):
            obj = getattr(msg, message_type)
            if obj:
                return obj, obj.file_id


from collections import defaultdict

def is_phrase_valid2(phrase):
	words = phrase.split(' ')
	seen = set()
	for word in words:
		if contains_anagram(seen, word):
			return False
		seen.add(word)
	return True

def is_phrase_valid1(phrase):
	words = phrase.split(' ')
	seen = set()
	for word in words:
		if word in seen:
			return False
		seen.add(word)
	return True

def contains_anagram(wordset, word):
	word_char_count = get_char_count(word)
	for testword in wordset:
		test_char_count = get_char_count(testword)
		if counts_equal(word_char_count, test_char_count):
			return True
	return False

def counts_equal(char_count_1, char_count_2):
	if len(char_count_1.keys()) != len(char_count_2.keys()):
		return False

	for key in char_count_1.keys():
		if key not in char_count_2.keys():
			return False
		if char_count_1[key] != char_count_2[key]:
			return False
	return True

def get_char_count(word):
	counts = defaultdict(int)
	for c in word:
		counts[c] += 1
	return counts

def count_valid_passphrases(in_text):
	phrases = in_text.split('\n')
	count = 0
	for phrase in phrases:
		if(is_phrase_valid2(phrase)):
			count += 1

	return count

with open('input.txt', 'r') as f:
	in_text = f.read()


import pandas as pd
from datetime import datetime

now = datetime.now() # current date and time
today = now.strftime('%Y-%m-%d')

''' https://fixturedownload.com/results/epl-2020 '''

# show complete records by changing rules
# pd.set_option('display.max_rows', None)
# pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)

def triangle(n):
    """Calculates the triangle number. ie. 1+2+...+n"""
    return sum([x for x in range(n+1)])

# Scrape tables from the webpage
dfs = pd.read_html('https://fixturedownload.com/results/epl-2020')

# Load the dataset into the dataframe
df = dfs[0]

# Split into results and fixtures
df_r = df.copy()[df['Result'].str.contains(" - ")]
df_f = df.copy()[~df['Result'].str.contains(" - ")]

# Create Home and Away Result Columns
splits = df_r['Result'].str.split()
df_r['Home_Score'] = splits.str[0]
df_r['Away_Score'] = splits.str[2]

df_r['Home_Result'] = ['win' if x > y else 'loss' if y > x else 'draw'
                       for x,y in zip(df_r['Home_Score'],df_r['Away_Score'])]
df_r['Away_Result'] = ['win' if x > y else 'loss' if y > x else 'draw'
                       for x,y in zip(df_r['Away_Score'],df_r['Home_Score'])]

# Print dataframes for viewing
# print(df_r)
# print('---------------')
# print(df_f)

# The set of stats that will be recorded for all teams
stats = {'Home_games': 0,
         'Away_games': 0,
         'Home_points': 0,
         'Away_points': 0,
         'Total_points': 0,
         'Game_mp': 0,
         'Home_mp': 0,
         'Away_mp': 0,
         'H_form_mp': 0,
         'A_form_mp': 0,
         'Home_form': 0,
         'Away_form': 0}

# Each of the teams as key to a dictionary holding their respective stats
teams = {'Arsenal': stats.copy(),
         'Aston Villa': stats.copy(),
         'Brighton': stats.copy(),
         'Burnley': stats.copy(),
         'Chelsea': stats.copy(),
         'Crystal Palace': stats.copy(),
         'Everton': stats.copy(),
         'Fulham': stats.copy(),
         'Leeds': stats.copy(),
         'Leicester': stats.copy(),
         'Liverpool': stats.copy(),
         'Newcastle': stats.copy(),
         'Man City': stats.copy(),
         'Man Utd': stats.copy(),
         'Sheffield Utd': stats.copy(),
         'Southampton': stats.copy(),
         'Spurs': stats.copy(),
         'West Brom': stats.copy(),
         'West Ham': stats.copy(),
         'Wolves': stats.copy()}

# Update the stats as per the results dataframe
for index, row in df_r.iterrows():
    if row['Home_Result'] == 'win':
        teams[row['Home Team']]['Home_games'] += 1
        teams[row['Home Team']]['Home_points'] += 3
        teams[row['Away Team']]['Away_games'] += 1
        teams[row['Away Team']]['Away_points'] += 0
        teams[row['Home Team']]['Total_points'] += 3
        teams[row['Home Team']]['Home_form'] += teams[row['Home Team']]['Home_games'] * 3   # Home Form
    elif row['Home_Result'] == 'loss':
        teams[row['Home Team']]['Home_games'] += 1
        teams[row['Home Team']]['Home_points'] += 0
        teams[row['Away Team']]['Away_games'] += 1
        teams[row['Away Team']]['Away_points'] += 3
        teams[row['Away Team']]['Total_points'] += 3
        teams[row['Away Team']]['Away_form'] += teams[row['Away Team']]['Away_games'] * 3   # Away Form
    else:
        teams[row['Home Team']]['Home_games'] += 1
        teams[row['Home Team']]['Home_points'] += 1
        teams[row['Away Team']]['Away_games'] += 1
        teams[row['Away Team']]['Away_points'] += 1
        teams[row['Home Team']]['Total_points'] += 1
        teams[row['Away Team']]['Total_points'] += 1
        teams[row['Home Team']]['Home_form'] += teams[row['Home Team']]['Home_games'] * 1   # Home Form
        teams[row['Away Team']]['Away_form'] += teams[row['Away Team']]['Away_games'] * 1   # Away Form

# Calculate the Game, home/away and form multipliers for each team
for team, stats in teams.items():
    stats['Game_mp'] = stats['Total_points'] / (stats['Home_games'] + stats['Away_games'])
    stats['Home_mp'] = stats['Home_points'] / stats['Home_games']
    stats['Away_mp'] = stats['Away_points'] / stats['Away_games']
    stats['H_form_mp'] = stats['Home_form'] * stats['Home_games'] / triangle(stats['Home_games']) / stats['Home_points']
    stats['A_form_mp'] = stats['Away_form'] * stats['Away_games'] / triangle(stats['Away_games']) / stats['Away_points']

avg_points = 0

# Print stats for viewing
for team, stats in teams.items():
    # print(team, ': ', stats)
    avg_points += stats['Game_mp']

# This calculates the average points per game for the season
avg_points /= 10

# Update stats for fixtures
for index, row in df_f.iterrows():
    home_temp = 0.5 * teams[row['Home Team']]['H_form_mp'] * (
                      teams[row['Home Team']]['Game_mp'] + teams[row['Home Team']]['Home_mp'])
    away_temp = 0.5 * teams[row['Home Team']]['H_form_mp'] * (
                      teams[row['Away Team']]['Game_mp'] + teams[row['Away Team']]['Away_mp'])
    point_update_h = teams[row['Home Team']]['Game_mp'] * avg_points / (home_temp + away_temp)
    # print('point_update_h: ', point_update_h)
    point_update_a = avg_points - point_update_h
    # print('point_update_a: ', point_update_a)

    # Update Stats for Home team
    teams[row['Home Team']]['Home_games'] += 1
    teams[row['Home Team']]['Home_points'] += point_update_h
    teams[row['Home Team']]['Total_points'] += point_update_h
    # Update Stats for Away team
    teams[row['Away Team']]['Away_games'] += 1
    teams[row['Away Team']]['Away_points'] += point_update_a
    teams[row['Away Team']]['Total_points'] += point_update_a

result_list = []

for team, stats in teams.items():
    result_list.append((team, round(stats['Total_points'])))

print('PL predictions on ', today, '\n')

result_list.sort(key = lambda x: x[1], reverse=True)

for team, pts in result_list:
    print(team, pts)

result_list.sort()

df_output = pd.DataFrame(result_list, columns=['team', today])

df_output = df_output.drop(['team'], axis=1)

df_output = df_output.transpose()

df_output.to_csv('predict.csv', mode='a', header=False)

print('\n PL Predictions have been written to csv')


# -*- coding: utf-8 -*-
"""
Created on Tue Apr  9 13:46:06 2019

Figure 5. Seasonal terrestrial and oceanic contributions to Australian precipitation in (a) relative terms [%]
and (b) absolute terms [mm].

@author: z3131380
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

#==============================================================================
# Definitions
#==============================================================================
dir_in = r'/srv/ccrc/data19/z3131380/PartB/Output/Australia/100parcels/TS10min/exp01/Processed/Rainfall_recycling/'
dir_out = r'/home/z3131380/hdrive/PhD/PartB/Reporting/Paper_1/Figures/'

region = 'Australia'
df = pd.read_csv(dir_in+'Seasonal/'+region+'_seasonal_rainfall_recycling_1979-2013.csv')
groups = df.groupby('Season')
#yr_groups = df.groupby(df.Year.astype(int))
#RR_seas_mean = [groups.get_group('DJF')['RR'].mean(),\
#                            groups.get_group('MAM')['RR'].mean(),\
#                            groups.get_group('JJA')['RR'].mean(),\
#                            groups.get_group('SON')['RR'].mean()]
#RR_seas_min = [groups.get_group('DJF')['RR'].min(),\
#                            groups.get_group('MAM')['RR'].min(),\
#                            groups.get_group('JJA')['RR'].min(),\
#                            groups.get_group('SON')['RR'].min()]
#RR_seas_max = [groups.get_group('DJF')['RR'].max(),\
#                            groups.get_group('MAM')['RR'].max(),\
#                            groups.get_group('JJA')['RR'].max(),\
#                            groups.get_group('SON')['RR'].max()]
#RR_seas_25th = [groups.get_group('DJF')['RR'].quantile(q=0.25),\
#                            groups.get_group('MAM')['RR'].quantile(q=0.25),\
#                            groups.get_group('JJA')['RR'].quantile(q=0.25),\
#                            groups.get_group('SON')['RR'].quantile(q=0.25)]
#RR_seas_75th = [groups.get_group('DJF')['RR'].quantile(q=0.75),\
#                            groups.get_group('MAM')['RR'].quantile(q=0.75),\
#                            groups.get_group('JJA')['RR'].quantile(q=0.75),\
#                            groups.get_group('SON')['RR'].quantile(q=0.75)]

# Open annual df to plot comparison
df2 = pd.read_csv(dir_in+'Yearly/'+region+'_annual_rainfall_recycling_1979-2013.csv')

fsize = 14

#==============================================================================
# Functions
#==============================================================================
def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)

#==============================================================================
# Plot seasonal RR for selected years
#==============================================================================
#
#
## Pick wet and dry years
#sel_yrs = [1986,1989,1994,1999,2002,2010]
#
#fig, ax1 = plt.subplots()#figsize=(16,11.7))
#for y in sel_yrs:#np.arange(1979,2014):
#    ax1.plot(range(4),yr_groups.get_group(y)['RR'],':o', label=y,markersize=4,linewidth=2)
#    plt.annotate(y,xy=(3.0,yr_groups.get_group(y)['RR'].reset_index(drop=True)[3]),fontsize=12)#xycoords='data',

#==============================================================================
# Figure 5. Seasonal terrestrial and oceanic contributions to Australian precipitation in (a) relative terms
# [%] and (b) absolute terms [mm], (c) mean seasonal precipitation recycling.
#==============================================================================

fig, ax2 = plt.subplots(figsize=(16,11.7))
ax2 = plt.subplot2grid((3, 1), (0, 0), rowspan=1)
plt.annotate('(a)',xy=(1980,1.3),xycoords='data',fontsize=fsize)
#p3, = ax2.plot(df['Year'],df['P_anom'],'-k', label="Rainfall anomaly [%]",linewidth=1.5)#,alpha=0.7)
ax2.fill_between(df['Year'],df['P_anom'],0,alpha=0.5,edgecolor='None',facecolor='gray')
# NOTE: the annual time series has been moved to show its values in the MIDDLE of each year. This differs
# from the purely annual plots, with show values at the START of each year. Careful when analysing.
#p2, = ax1.plot(df2['Year']+0.5,df2['P_anom'],':', label="Rainfall anomaly [%]",markersize=4,linewidth=2,color='gray')
ax2.set_ylim(-1.5,1.5)
ax2.grid(axis='x')
ax2.tick_params(labelbottom=False,labeltop=True)
ax2.axhline(0,color='gray',linestyle=':')
ax2.set_yticklabels(ax2.get_yticks(),fontsize=fsize)
ax2.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.annotate('Precipitation anomaly [%]',xy=(-0.06,0.85),xycoords='axes fraction',fontsize=fsize,rotation=90)
ax2a = ax2.twinx()
ax2b = ax2.twinx()
p2a, = ax2a.plot(df['Year'],df['outregion_ocean_pct'],'-bo', label="Ocean water vapour contribution [%]",markersize=3,markeredgecolor='b',markerfacecolor='b',alpha=0.4)
p2b, = ax2b.plot(df['Year'],df['RR'],'-gs', label="Land water vapour contribution [%]",markersize=3,markeredgecolor='g',markerfacecolor='g')#,alpha=0.5)
ax2a.spines["right"].set_position(("axes", 1.01))
ax2b.spines["right"].set_position(("axes", 1.07))
make_patch_spines_invisible(ax2a)
make_patch_spines_invisible(ax2b)
ax2a.spines["right"].set_visible(True)
ax2b.spines["right"].set_visible(True)
tkw = dict(size=4, width=1.5)
ax2a.tick_params(axis='y', colors=p2a.get_color(), **tkw)
ax2b.tick_params(axis='y', colors=p2b.get_color(), **tkw)
ax2a.set_yticklabels(ax2a.get_yticks(),fontsize=fsize)
ax2a.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax2b.set_yticklabels(ax2b.get_yticks(),fontsize=fsize)
ax2b.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax2a.set_ylim(70,95)
ax2b.set_ylim(5,25)
plt.annotate("Oceanic contribution [%]",xy=(1.04,0.85),xycoords = 'axes fraction',rotation=90,color='blue',fontsize=fsize)
plt.annotate("Terrestrialcontribution [%]",xy=(1.1,0.9),xycoords = 'axes fraction',rotation=90,color='green',fontsize=fsize)
ax2.set_xlim(1979,2014)
ax2.set_xticks(np.arange(min(df['Year']), max(df['Year'])+1, 1))
ax2.set_xticklabels(ax2.get_xticks(),rotation=90,fontsize=fsize)
ax2.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))

ax1 = plt.subplot2grid((3, 1), (1, 0), sharex=ax2)
plt.annotate('(b)',xy=(1980,1800),xycoords='data',fontsize=fsize)
#p1, = ax1.plot(df['Year'],df['P_total']/1000,'-k', label="Rainfall total [m]",linewidth=1.5)#,alpha=0.7)
ax1.fill_between(df['Year'],df['P_total']/1000,0,alpha=0.5,edgecolor='None',facecolor='gray')
# NOTE: the annual time series has been moved to show its values in the MIDDLE of each year. This differs
# from the purely annual plots, with show values at the START of each year. Careful when analysing.
#p2, = ax1.plot(df2['Year']+0.5,df2['P_total']/1000,':', label="Rainfall total [m]",markersize=4,linewidth=2,color='gray')
ax1.set_ylim(0,2000)
ax1.grid(axis='x')
ax1.set_xlim(1979,2014)
ax1.set_xticks(np.arange(min(df['Year']), max(df['Year'])+1, 1))
ax1.set_xticklabels(ax1.get_xticks(),rotation=90,fontsize=fsize)
ax1.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1.set_yticklabels(ax1.get_yticks(),fontsize=fsize)
ax1.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
plt.annotate('Precipitation total [m]',xy=(-0.06,0.75),xycoords='axes fraction',fontsize=fsize,rotation=90)
ax1a = ax1.twinx()
ax1b = ax1.twinx()
p1a, = ax1a.plot(df['Year'],df['outregion_ocean_mm']/1000,'-bo', label="Ocean water vapour contribution [m]",markersize=3,markeredgecolor='b',markerfacecolor='b',alpha=0.4)
p1b, = ax1b.plot(df['Year'],df['region_land_mm']/1000,'-gs', label="Land water vapour contribution [m]",markersize=3,markeredgecolor='g',markerfacecolor='g')#,alpha=0.5)
ax1a.spines["right"].set_position(("axes", 1.01))
ax1b.spines["right"].set_position(("axes", 1.07))
make_patch_spines_invisible(ax1a)
make_patch_spines_invisible(ax1b)
ax1a.spines["right"].set_visible(True)
ax1b.spines["right"].set_visible(True)
tkw = dict(size=4, width=1.5)
ax1a.tick_params(axis='y', colors=p1a.get_color(), **tkw)
ax1b.tick_params(axis='y', colors=p1b.get_color(), **tkw)
ax1a.set_yticklabels(ax1a.get_yticks(),fontsize=fsize)
ax1a.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1b.set_yticklabels(ax1b.get_yticks(),fontsize=fsize)
ax1b.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
plt.annotate("Oceanic contribution [m]",xy=(1.05,0.85),xycoords = 'axes fraction',rotation=90,color='blue',fontsize=fsize)
plt.annotate("Terrestrial contribution [m]",xy=(1.105,0.9),xycoords = 'axes fraction',rotation=90,color='green',fontsize=fsize)

#ax3  = fig.add_axes([0.5,0.42,0.25,0.15])
ax3 = plt.subplot2grid((3, 1), (2, 0))#, sharex=ax2)
plt.annotate('(c)',xy=(0.6,22),xycoords='data',fontsize=fsize)
#ax3.bar(range(4),RR_seas_mean,width=0.6,align='center',facecolor='g',alpha=0.8)
data = np.column_stack([groups.get_group('DJF')['RR'][1:],\
                        groups.get_group('MAM')['RR'][1:],\
                        groups.get_group('JJA')['RR'][1:],\
                        groups.get_group('SON')['RR'][1:]])
bp = ax3.boxplot(data,whis='range')
plt.setp(bp['boxes'], color='black')
plt.setp(bp['whiskers'], color='black')
plt.setp(bp['medians'], color='black')
for line in bp['medians']:
    # get position data for median line
    x = bp['medians'].index(line)+1#line.get_xydata()[1][0]-line.get_xydata()[0][0]
    y = line.get_xydata()[0,1] + 0.1
    mn = line.get_xydata()[0,1]
    # overlay median value
    plt.text(x, y, '%.1f' % mn,
         horizontalalignment='center',fontsize=12) # draw above, centered
ax3.set_ylim(5,25)
#plt.tick_params(
#    axis='both',          # changes apply to the x-axis
#    which='both',      # both major and minor ticks are affected
#    bottom=False,      # ticks along the bottom edge are off
#    top=False,         # ticks along the top edge are off
#    left=False,
#    labelleft=False,
#    labelbottom=False) # labels along the bottom edge are off
ax3.set_xticklabels(['Summer','Fall','Winter','Spring'],fontsize=fsize)
ax3.set_yticklabels(ax3.get_yticks(),fontsize=fsize)
ax3.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
plt.annotate('Precipitation recycling [%]',xy=(-0.06,0.9),xycoords='axes fraction',fontsize=fsize,rotation=90)
#plt.annotate('Summer',xy=(0,11),xycoords='data',fontsize=12,rotation=90)
#plt.annotate('Fall',xy=(1,4),xycoords='data',fontsize=12,rotation=90)
#plt.annotate('Winter',xy=(2,8),xycoords='data',fontsize=12,rotation=90)
#plt.annotate('Spring',xy=(3,9),xycoords='data',fontsize=12,rotation=90)
#plt.annotate(str(round(RR_seas_mean[0],1))+'%',xy=(-0.2,20),xycoords='data',fontsize=12)
#plt.annotate(str(round(RR_seas_mean[1],1))+'%',xy=(0.8,17),xycoords='data',fontsize=12)
#plt.annotate(str(round(RR_seas_mean[2],1))+'%',xy=(1.8,13),xycoords='data',fontsize=12)
#plt.annotate(str(round(RR_seas_mean[3],1))+'%',xy=(2.8,17.9),xycoords='data',fontsize=12)

plt.tight_layout()
fig.savefig(dir_out+'Fig5.png',bbox_inches='tight')
plt.close()


from sentence_transformers import SentenceTransformer
from typing import Any

from inference.retrievers.retriever import Retriever
from indexing.enums.index_names import IndexName

class T5Retriever(Retriever):
    def __init__(self):
        self.index_name = IndexName.UWATERLOO_COURSES_INDEX_T5.value
        self.model_name = "sentence-transformers/sentence-t5-base"
        self.model = SentenceTransformer(self.model_name)

    def get_es_query(self, query: str, num_results: int = 5) -> dict[Any, Any]:
        embedding = self.model.encode(query)
        es_query = {
            "from": 0,
            "size": num_results,
            "_source": ["courseName", "courseDescription", "courseCode"],
            "knn": {"field": "courseDescEncoding", "query_vector": embedding, "k": num_results, "num_candidates": 5000}}

        return es_query


'''
09 트랙바 사용하기
트랙바 : 프로그램 동작 중 사용자가 지정한 범위 안의 값을 선택할 수 있는 컨트롤
cv2.createTrackbar(trackbarName, windowName, value, count, onChange) -> None
- trackBarName: 트랙바 이름
- windowName: 트랙바를 생성할 창 이름
- value: 트랙바 위치 초기값
- count: 트랙바 최댓값. 최솟값은 항상 0
- onChange: 트랙바 위치가 변경될 때마다 호출할 콜백 함수 이름
            트랙바 이벤트 콜백 함수는 다음 형식을 따름.
            onChange(pos) -> None
'''

import numpy as np
import cv2

def on_level_changed(pos):
    global img

    level = pos * 16
    level = np.clip(level, 0, 255)  # np.clip()은 0보다 작을 땐 0으로 255보다 크면 255로 변경시켜 값을 제한해준다.

    img[:, :] = level  # 슬라이스 :만 넣었을 때는 모든 범위에 적용된다.
    cv2.imshow('image', img)

img = np.zeros((480, 640, 3), dtype=np.uint8)

cv2.namedWindow('image')
cv2.imshow('image', img)
cv2.createTrackbar('GrayLevel', 'image', 0, 16, on_level_changed)
cv2.waitKey()

cv2.destroyAllWindows()

ch02 OpenCV_Python 기초 사용법/09 Trackbar.py


from typing import List
from node import Node

class TimeTable:
    """
    Структура, показывающая занятость ресурсов при определенном обходе дерева
    Используется для оценки времени каждого пути
    """
    def __init__(self, requirements, max_size=200, verbose=False):
        self.requirements = requirements
        self.max_size = max_size
        self.taken = {req: [0] * self.max_size for req in requirements}
        self.last_index = {req: 0 for req in requirements}
        self.verbose = verbose

    def add_node(self, node: Node):
        requirements = node.requirements
        last_index = max(self.last_index[req] for req in requirements)
        last_index = max(last_index, self.last_index['h'])
        for req in requirements:
            self.last_index[req] = last_index + node.time
            self.taken[req][last_index: last_index + node.time] = [1] * node.time

        if self.verbose:
            print(node, node.time)
            for req in self.requirements:
                if self.time() > 0:
                    print(self.taken[req][:self.time()], req)
                else:
                    print(self.taken[req], req)

    def __call__(self, path: List[Node]):
        for node in path:
            self.add_node(node)
        return self

    def time(self):
        return max(self.last_index[req] for req in self.requirements)

    def print(self):
        for req in self.requirements:
            print(self.taken[req][:self.time()], req)


####=======================  THread ==================
from time import sleep
from threading import *

class hello(Thread):
    def run(self):
        for i in range(10):
            print('Hello')
            sleep(1)

class hi(Thread):
    def run(self):
        for i in range(10):
            print('Hi')
            sleep(1)

t1 = hello()
t2 = hi()

# t1.start()
# sleep(0.2)
# t2.start()
#
# t1.join()
# t2.join()

print('Bye....')

###  ================= Working with File =======================================
## Read File
file = open('data/MyData', mode='r')
# print(file.readline(), end='#')
# print(file.readline())

## Write File
file1 = open('data/Mydata2', 'a')
# file1.write(' Hello.....')

for data in file:
    # print(data)
    file1.write(data)

try:
    with open('data/data.txt') as fileObj:
        lines = fileObj.readlines()
        print(lines)
        # for num, line in enumerate(lines):
        #     print(num+1, line.upper())

except Exception as e:
       print("File Error :  ", e)

##=======================================  BINARY DATA ==========================================================

## write a binary data to file
with open('data/binary', 'wb') as binObj:
    binObj.write(b'life is good') # not saving as binary data

##read  a binary file
with open('data/binary', 'rb') as binObj:
    binary_data = binObj.read()
    # encoded_data = binary_data.encode('utf-8')
    print(binary_data)

#### read  write a  imaage file as binary file  #####
imgFile = open('data/Image.jpg', 'rb')
print(imgFile)

imgFile1 = open('data/image2.jpg', 'wb')
for i in imgFile:
    imgFile1.write(i)

### --------------- Bangla writing-------------------
with open('data/bangla.txt', 'a', encoding='UTF-8') as fileObj:
    fileObj.write('আমার সোনার বাংলাদেশ \n')
    fileObj.write('আমি তোমায় ভাল বাসি  \n')

### --------------- redirect ot file -------------------
## it will redirect to file directly instead printing in the consosle
with open('data/print.txt', 'w') as fileObj:
    print('Nothing Goes unpaid:  ', file=fileObj)

##==============Check if fle exist ================
import os
if os.path.exists('data/bangla.txt'):
    print("Yes....exist ")

###=======================temporary file ========================
from tempfile import TemporaryFile
with TemporaryFile('w+') as obj:
     obj.write(" Life is cool . \n")

     obj.seek(5)  ## 0 indicate the posttion of controoll
     data = obj.read()
     print(data)

##===================== Serialize python object to a byte stream=============================
import pickle

dict_data = {'name' : 'Joytun nur', 'Country' : 'Bangladesh'}
with open('data/serialize', 'wb') as obj:
    pickle.dump(dict_data, obj)

with open('data/serialize', 'rb') as obj:
    dict_data = pickle.load(obj)
    print(dict_data)

###================= CSV fle read and write ==================
### read csv
import csv
# with open('data/data.csv','r') as obj:
#     fcsv = csv.reader(obj)
#
#     sum = 0
#     for i, row in enumerate(fcsv):
#         print(i, row[0], row[1])
#         sum += int(row[1]) if i > 0 else 0
#     print("Total Cost: ", sum)

## write csv
list_item = [ ["Name",'Age','Country'],
              ['Bill Gets', 55,'US'],
              ['Mark Zucarbarg', 34,'US'],
              ['Swift',35,'CANADA']
            ]

with open('data/ppl.csv','w') as obj:
    file_csv = csv.writer(obj)
    file_csv.writerows(list_item)

###=========  JSON data ==================
data ={'Name':'Bill Gets', 'age': 55,'Country':'US'},
    # {'Mark Zucarbarg', 34,'US'],
    # {'Swift',35,'CANADA']

import json
with open('data/json_data.json','w') as obj:
    json.dump(data, obj)

with open('data/json_data.json','r') as obj:
  jsondata = json.load(obj)
  print(jsondata)


import json

persona = {}
def abrir_archivo(ruta: str):
    cadena: list = ruta.split('.')
    if cadena[1].lower() == 'json':
        try:
            #Referencias el fichero
            with open(ruta) as archivo:
                #Cargar los datos del archivo json
                persona = json.load(archivo)
                print(persona)
        except:
            print('Error al cargar los datos')
    else:
        print('Extensión inválida')

abrir_archivo('persona.json')

#Referenciar el fichero
""" with open('personas.json','w') as archivo:
    #Escribir/crear el fichero
    json.dump(persona, archivo) """


#!/usr/bin/python3
"""Module that defines endpoints for articles"""

from typing import List
from sqlalchemy.orm import Session
from . import crud, models, schemas
from .database import SessionLocal, engine
from fastapi import Depends, HTTPException, APIRouter

models.Base.metadata.create_all(bind=engine)

router = APIRouter()

# Dependency
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@router.get("/get_subsection_by_country/{country_id}",
            response_model=List[schemas.Subsection])
async def read_subsections_by_country(
        country_id: int, skip: int = 0, limit: int = 100,
        db: Session = Depends(get_db)):
    """Endpoint to read subsection based on country id"""

    subsections = crud.get_subsections_by_country(
            db, country_id=country_id, skip=skip, limit=limit)
    return subsections

@router.get("/get_subsection_by_cc/{country_id}/{chapter_id}",
            response_model=List[schemas.Subsection])
async def read_subsections_by_country_and_chapter(
        country_id: int, chapter_id: int, skip: int = 0,
        limit: int = 100, db: Session = Depends(get_db)):
    """Endpoint to read subsection based on country id and chapter id"""

    subsections = crud.get_subsections_by_country_chapter(
            db, country_id=country_id, chapter_id=chapter_id,
            skip=skip, limit=limit)
    return subsections

@router.get("/get_subsection_by_cca/{country_id}/{chapter_id}/{article_id}",
            response_model=List[schemas.Subsection])
async def read_subsections_by_country_chapter_and_article(
        country_id: int, chapter_id: int, article_id: int, skip: int = 0,
        limit: int = 100, db: Session = Depends(get_db)):
    """Endpoint to read subsection based on country, chapter and article"""

    subsections = crud.get_subsections_by_country_chapter_article(
            db, country_id=country_id, chapter_id=chapter_id,
            article_id=article_id, skip=skip, limit=limit)
    return subsections

@router.get("/get_subsection_by_ccas/{country}/{chapter}/{article}/{section}",
            response_model=List[schemas.Subsection])
async def read_subsections_by_country_chapter_article_and_section(
        country_id: int, chapter_id: int, article_id: int, section_id: int,
        skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    """Endpoint to read subsection based on country, chapter, article..."""

    subsections = crud.get_subsections_by_country_chapter_article_section(
            db, country_id=country_id, chapter_id=chapter_id,
            article_id=article_id, section_id=section_id,
            skip=skip, limit=limit)
    return subsections

@router.get("/get_subsection/{subsection_id}",
            response_model=schemas.Subsection)
async def read_subsection(subsection_id: int, db: Session = Depends(get_db)):
    """Endpoint to read subsection based on its id"""

    db_subsection = crud.get_subsection(db, subsection_id=subsection_id)
    if db_subsection is None:
        raise HTTPException(status_code=404, detail="Subsection not found")
    return db_subsection

@router.post("/create_subsection/{country}/{chapter}/{article}/{section}",
             response_model=schemas.Subsection)
async def create_subsection(country_id: int,
                            chapter_id: int,
                            article_id: int,
                            section_id: int,
                            subsection: schemas.SubsectionCreate,
                            db: Session = Depends(get_db)):
    """Endpoint to create subsection with country, chapter, article..."""

    return crud.create_subsection(db=db,
                                  subsection=subsection,
                                  country_id=country_id,
                                  chapter_id=chapter_id,
                                  article_id=article_id,
                                  section_id=section_id)

@router.put("/update_subsection/{subsection_id}",
            response_model=schemas.Subsection)
async def update_subsection(subsection_id: int,
                            subsection_update: schemas.SubsectionBase,
                            db: Session = Depends(get_db)):
    """Endpoint to update a subsection based on its id"""

    return crud.update_subsection(db, subsection_id, subsection_update)

@router.delete("/delete_subsection/{subsection_id}", response_model=None)
async def delete_subsection(subsection_id: int,
                            db: Session = Depends(get_db)):
    """Endpoint to delete a subsection based on its id"""

    crud.delete_subsection(db, subsection_id)
    return {"message": "Subsection deleted successfully"}


import urllib.request
import json
import dml
import prov.model
import datetime
import uuid
import pprint
import csv

class garden_vs_rent(dml.Algorithm):

    contributor = 'alyu_sharontj_yuxiao_yzhang11'
    reads = ['alyu_sharontj_yuxiao_yzhang11.garden_count','alyu_sharontj_yuxiao_yzhang11.average_rent_zip']
    writes = ['alyu_sharontj_yuxiao_yzhang11.garden_vs_rent']

    @staticmethod
    def execute(trial=False):
        '''Retrieve some data sets (not using the API here for the sake of simplicity).'''
        startTime = datetime.datetime.now()

        # Set up the database connection.
        client = dml.pymongo.MongoClient()
        repo = client.repo
        repo.authenticate('alyu_sharontj_yuxiao_yzhang11', 'alyu_sharontj_yuxiao_yzhang11')

        garden_count = repo['alyu_sharontj_yuxiao_yzhang11.garden_count']
        average_rent = repo['alyu_sharontj_yuxiao_yzhang11.average_rent_zip']

        # print("i am here ")
        # s = average_rent.find_one({"Zip": "11111"})
        # print(s == None)
        #pprint.pprint(average_rent.find_one({"Zip": "02169"}))
        repo.dropCollection("garden_vs_rent")
        repo.createCollection("garden_vs_rent")

        garden_cur = garden_count.find()
        rent_cur = average_rent.find()
        for i in rent_cur:
            garden_vs_rent= {}
            rent_zip = i["Zip"]
            # print("rent zip", rent_zip)
            garden_vs_rent["Zip"] = rent_zip
            garden_vs_rent["Average"] = i["Average"]

            if (garden_count.find_one({"_id": rent_zip}) != None):

                garden_vs_rent["garden_count"] = garden_count.find_one({"_id": rent_zip})['count']
            else:
                garden_vs_rent["garden_count"] = 0

            repo['alyu_sharontj_yuxiao_yzhang11.garden_vs_rent'].insert(garden_vs_rent)

        # print("INNN garden_vs_rent")
        endTime = datetime.datetime.now()

        return {"start": startTime, "end": endTime}

    @staticmethod
    def provenance(doc=prov.model.ProvDocument(), startTime=None, endTime=None):
        '''
            Create the provenance document describing everything happening
            in this script. Each run of the script will generate a new
            document describing that invocation event.
            '''

        # Set up the database connection.
        client = dml.pymongo.MongoClient()
        repo = client.repo
        repo.authenticate('alyu_sharontj_yuxiao_yzhang11', 'alyu_sharontj_yuxiao_yzhang11')
        doc.add_namespace('alg', 'http://datamechanics.io/algorithm/')  # The scripts are in <folder>#<filename> format.
        doc.add_namespace('dat', 'http://datamechanics.io/data/')  # The data sets are in <user>#<collection> format.
        doc.add_namespace('ont',
                          'http://datamechanics.io/ontology#')  # 'Extension', 'DataResource', 'DataSet', 'Retrieval', 'Query', or 'Computation'.
        doc.add_namespace('log', 'http://datamechanics.io/log/')  # The event log.
        doc.add_namespace('bdp', 'https://data.boston.gov/export/767/71c/')

        this_script = doc.agent('alg:alyu_sharontj_yuxiao_yzhang11#garden_vs_rent',
                                {prov.model.PROV_TYPE: prov.model.PROV['SoftwareAgent'], 'ont:Extension': 'py'})

        garden_input = doc.entity('dat:alyu_sharontj_yuxiao_yzhang11#garden_count',
                                  {prov.model.PROV_LABEL: 'garden_count',
                                   prov.model.PROV_TYPE: 'ont:DataSet'})

        rent_input = doc.entity('dat:alyu_sharontj_yuxiao_yzhang11#average_rent_zip',
                                  {prov.model.PROV_LABEL: 'average_rent_zip',
                                   prov.model.PROV_TYPE: 'ont:DataSet'})

        this_run = doc.activity('log:uuid' + str(uuid.uuid4()), startTime, endTime)

        output =  doc.entity('dat:alyu_sharontj_yuxiao_yzhang11#garden_vs_rent',
                             {prov.model.PROV_LABEL:'garden_vs_rent',
                              prov.model.PROV_TYPE:'ont:DataSet'})

        # get_lost = doc.activity('log:uuid' + str(uuid.uuid4()), startTime, endTime)
        #
        doc.wasAssociatedWith(this_run, this_script)
        doc.used(this_run, garden_input, startTime)
        doc.used(this_run, rent_input, startTime)

        doc.wasAttributedTo(output, this_script)
        doc.wasGeneratedBy(output, this_run, endTime)
        doc.wasDerivedFrom(output, garden_input, this_run, this_run, this_run)
        doc.wasDerivedFrom(output, rent_input, this_run, this_run, this_run)

        repo.logout()

        return doc

# garden_vs_rent.execute()
# doc = garden_vs_rent.provenance()
# print(doc.get_provn())
# print(json.dumps(json.loads(doc.serialize()), indent=4))

## eof


# -*- coding: cp950 -*-
import numpy as np
from scipy.integrate import dblquad

def make_C_matrix(DELTA, site_size):
    def eps(Px,Py):
        value = 2*((np.cos(Px))**2+(np.cos(Py))**2)
        return value

    def pow(Px,Py,ri=[0,0],rj=[0,0]):
        value = Px*(ri[0]-rj[0])+Py*(ri[1]-rj[1])
        return value

    def R(Px,Py,delta):
        value = (eps(Px,Py)**2+4*(delta**2)*((np.sin(Px))**2+(np.sin(Py))**2))**0.5
        return value

    def Cij(delta,mode,ri=[0,0],rj=[0,0]):

        const = 1.0/(4*np.pi**2)

        def Nonedeg_real():
            value = dblquad(lambda Px,Py:((-np.cos(pow(Px,Py,ri,rj))*np.sin(Py)
                                          -np.sin(pow(Px,Py,ri,rj))*np.sin(Px))*const
                            *delta/(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi,
                            )
            return value[0]
        def Nonedeg_imag():
            value = dblquad(lambda Px,Py:((-np.sin(pow(Px,Py,ri,rj))*np.sin(Py)
                                          +np.cos(pow(Px,Py,ri,rj))*np.sin(Px))*const
                            *delta/(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi)
            return value[0]

        def leftdeg_real():
            value = dblquad(lambda Px,Py:((-np.cos(pow(Px,Py,ri,rj))*np.sin(Py)
                                          +np.sin(pow(Px,Py,ri,rj))*np.sin(Px))*const
                            *delta/(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi)
            return value[0]
        def leftdeg_imag():
            value = dblquad(lambda Px,Py:((-np.cos(pow(Px,Py,ri,rj))*np.sin(Px)
                                          -np.sin(pow(Px,Py,ri,rj))*np.sin(Py))*const
                            *delta/(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi)
            return value[0]

        def rightdeg_real():
            value = dblquad(lambda Px,Py:(np.cos(pow(Px,Py,ri,rj))*(1+eps(Px,Py))*const
                            /(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi)
            return value[0]
        def rightdeg_imag():
            value = dblquad(lambda Px,Py:(np.sin(pow(Px,Py,ri,rj))*(1+eps(Px,Py))*const
                            /(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi)
            return value[0]

        def twodeg_real():
            value = dblquad(lambda Px,Py:(np.cos(pow(Px,Py,ri,rj))*(1-eps(Px,Py))*const
                            /(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi)
            return value[0]
        def twodeg_imag():
            value = dblquad(lambda Px,Py:(np.sin(pow(Px,Py,ri,rj))*(1-eps(Px,Py))*const
                            /(2.0*R(Px,Py,delta))),
                            0,
                            2*np.pi,
                            lambda Py:0,
                            lambda Py:2*np.pi)
            return value[0]

        def choose(mode):
            value = {
                0 : complex(Nonedeg_real(),Nonedeg_imag()),
                1 : complex(leftdeg_real(),leftdeg_imag()),
                2 : complex(rightdeg_real(),rightdeg_imag()),
                3 : complex(twodeg_real(),twodeg_imag())
            }
            return value.get(mode,None)

        return choose(mode)

    '''
    print(Cij(DELTA ,0,[2,3],[3,2]))
    print(Cij(DELTA ,1,[2,3],[3,2]))
    print(Cij(DELTA ,2,[2,3],[3,2]))
    print(Cij(DELTA ,3,[2,3],[3,2]))
    '''
    def make_Cij(DELTA, ri=[0, 0], rj=[0, 0]):
        Cij_cell = [[0, 0], [0, 0]]
        Cij_cell[0][1]= Cij(DELTA ,0,ri,rj)
        Cij_cell[1][1]= Cij(DELTA ,1,ri,rj)
        Cij_cell[0][0]= Cij(DELTA ,2,ri,rj)
        Cij_cell[1][0]= Cij(DELTA ,3,ri,rj)
        return Cij_cell

    total_site = site_size[0]*site_size[1]

    C_matrix = np.zeros([total_site*2, total_site*2], dtype=complex)
    for i in range(total_site):
        for j in range(total_site):
            ri = [i/site_size[1],i%site_size[1]]
            rj = [j/site_size[1],j%site_size[1]]
            #print(i, j, ri, rj)
            Cij_cell = make_Cij(DELTA, ri, rj)
            for a in range(2):
                for b in range(2):
                    C_matrix[i*2+a, j*2+b] = Cij_cell[a][b]
    return C_matrix

DELTA = 4
site_size = (2, 2)
A = make_C_matrix(DELTA, site_size)
print(A)

import matplotlib.pylab as plt
plt.imshow(np.abs(A))
plt.show()


"""Write a program that reads a file and prints the letters in decreasing
order of frequency. Your program should convert all the input to lower case and
only count the letters a-z. Your program should not count spaces, digits, punctuation,
or anything other than the letters a-z. Find text samples from several different
languages and see how letter frequency varies between languages. Compare your
results with the tables at wikipedia.org/wiki/Letter_frequencies."""
import string
import matplotlib.pyplot as plt

fname = input("Enter the filname: ")

try:
    fhand = open(fname)
except:
    print("Invalid filename")
    exit()

lfq = {}         #intialize letter frequency dictionary

for line in fhand:                              #parse through each line of the file
    line = line.translate(str.maketrans('','',string.punctuation)) #get rid of punctuation marks using the translate method
    line = line.lower()                     #convert all string to lowercase
    words = line.split()                   #create a list of words using the split method
    if len(words) == 0 :
        continue
    for word in words:                   #loop through each word in the words list
        letters = str(word)             #convert each word in the list to string
        for var in letters:                 #loop through each character in the word
            if var >= "a" and var<="z":            #if the character is an alphabet, add it to the dictionary
                lfq[var] = lfq.get(var,0) + 1         #adding to the dictionary using the GET method
            else:
                continue                         #skip uninteresting characters

letterlist = sorted(lfq.items())          #create a sorted list (of tuples) by calling SORTED function on dictionary.items()
#which returns a list of tuples(key,value)
letter_list = []
freq_list = []

for letter,freq in letterlist:                  #loop through the sorted list (letter, freq is the tuple assignment, Since each element of the list is a tuple)
    print(letter,freq)
    letter_list.append(letter)
    freq_list.append(freq)
#print(len(letter_list))
#print(len(freq_list))

plt.xlabel("Letters",color="Green")
plt.ylabel("Frequency",color="Green")
plt.title("Letter Frequency distribution in '%s'"%fname,color="Green")

plt.bar(letter_list,freq_list)
plt.show()


""" Toyhou.se scraper cog, adds interactive commands with Toyhou.se website """
import functions
import discord
import userdb
import asyncio
import os

from discord.ext import commands
from functions import functions
from toyhousescraper import ToyHouseScraper
from functools import partial

class ToyHouse(commands.Cog):
    """
    Discord Bot Cog, adds user interaction with images on Toyhou.se website
    """

    def __init__(self, bot):
        self.bot = bot
        self.scraper = ToyHouseScraper()
        self.db = userdb.UserDBService()
        self.img_count = 10

    @commands.Cog.listener()
    async def on_ready(self):
        """ Confirms cog load """
        print("✨ Toyhou.se is being herded! ✨")

    @commands.command()
    async def link(self, ctx):
        """
        Links a discord user to a toyhouse user account

        Toyhouse user profile must be set to visible to guests or these bot features
        will be unavailable.
        """
        await ctx.send(embed=functions.base_embed(
            title="Link Account",
            text="Tell me your Toyhou.se username and I'll link it!"
        ))

        # Message retrieval and verification
        try:
            msg = await self.bot.wait_for("message", check=partial(functions.basic_msg_check, ctx), timeout=45)
            self.scraper.set_route(msg.content)
            user_page_exists = self.scraper.verify_user_found()

        # Timeout command
        except asyncio.TimeoutError:
            await ctx.send(embed=functions.base_embed(
                title="Ok",
                text=f"Times up! Call me back if you'd like to link accounts ⏰"
            ))
            return

        # Confirm page accuracy
        if user_page_exists:
            verify_msg = await ctx.send(embed=functions.user_confirm_embed(
                user_page=self.scraper.route,
                user_name=msg.content.title()
            ))
        else:
            await ctx.send(embed=functions.base_embed(
                title="Link Account",
                text="Well this is awkward... that profile was either not found on Toyhou.se "
                     "or the user is not visible to guests 😬"
            ))
            return

        # Confirm reaction set
        await verify_msg.add_reaction("✅")
        await verify_msg.add_reaction("❌")

        # Wait for user reaction
        try:
            reaction, user = await self.bot.wait_for(
                "reaction_add",
                check=partial(functions.basic_reaction_check, ctx),
                timeout=35)

        except asyncio.TimeoutError:
            await ctx.send(embed=functions.base_embed(
                title="Link Account",
                text="Times up! Call me back if you'd like to link accounts ⏰"
            ))
            return

        if str(reaction) == "❌":
            await verify_msg.delete()
            await ctx.send(embed=functions.base_embed(
                title="Link Account",
                text="Shoot, I missed! Make sure you gave me your correct username! 😅"
            ))
            return

        await verify_msg.delete()
        # Save user ID and Toyhou.se account name
        # If user is not in the DB, insert and advise, else update and advise
        if not self.db.query_id(ctx.author.id):
            self.db.insert(ctx.author.id, ctx.author.name, msg.content.title())
            await ctx.send(embed=functions.base_embed(
                title="Account Link",
                text="🌠 Successfuly Linked! Welcome to the meadow 🌠",
                footer=f"{ctx.author.name} linked to {msg.content}."
            ))
        else:
            self.db.update_th_account(ctx.author.id, msg.content.title())
            await ctx.send(embed=functions.base_embed(
                title="Account Link",
                text="🌠 Welcome back! I updated your link! 🌠",
                footer=f"{ctx.author.name} linked to {msg.content}."
            ))

    @commands.command()
    async def random(self, ctx):
        """ Display a random file image of a character from your linked Toyhouse account """
        user = self.db.query_id(ctx.message.author.id)
        if user:
            self.scraper.set_route(user.th_account)
            path, caption = self.scraper.scrape_random_image()
            file = discord.File(path, filename="image.png")
            await ctx.send(embed=functions.random_img_embed(
                caption=caption, msg_author=user.th_account), file=file)
            os.remove(path)

        else:
            await ctx.send(embed=functions.base_embed(
                title="No Linked Account",
                text="You need to link your Toyhou.se account before using this command!"))

def setup(bot):
    """ Required setup function for cog """
    bot.add_cog(ToyHouse(bot))


import speech_recognition as sr
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('stopwords')
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import string
import streamlit as st

# Initialize the speech recognition recognizer
r = sr.Recognizer()

with open('geology.txt', 'r', encoding='utf-8') as f:
    data = f.read().replace('\n', ' ')

# Tokenize the text into sentences
sentences = sent_tokenize(data)
# Define a function to preprocess each sentence
def preprocess(sentence):
    # Tokenize the sentence into words
    words = word_tokenize(sentence)
    # Remove stopwords and punctuation
    words = [word.lower() for word in words if word.lower() not in stopwords.words('english') and word not in string.punctuation]
    # Lemmatize the words
    lemmatizer = WordNetLemmatizer()
    words = [lemmatizer.lemmatize(word) for word in words]
    return words

# Preprocess each sentence in the text
corpus = [preprocess(sentence) for sentence in sentences]

# Define a function to find the most relevant sentence given a query
def get_most_relevant_sentence(query):
    # Preprocess the query
    query = preprocess(query)
    # Compute the similarity between the query and each sentence in the text
    max_similarity = 0
    most_relevant_sentence = ""
    for sentence in corpus:
        similarity = len(set(query).intersection(sentence)) / float(len(set(query).union(sentence)))
        if similarity > max_similarity:
            max_similarity = similarity
            most_relevant_sentence = " ".join(sentence)
    return most_relevant_sentence
def chatbot(question):
    # Find the most relevant sentence
    most_relevant_sentence = get_most_relevant_sentence(question)
    # Return the answer
    return most_relevant_sentence

def transcribe_speech():
    with sr.Microphone() as source:
        st.info('Speak now...')
        print("This is only available for english speakers! Thanks for understanding!")
        audio_data = r.record(source, duration=5)
        print("Recognizing...")
        print("this is your recording: ",audio_data)
        try:
            text = r.recognize_google(audio_data)
            print(audio_data.get_wav_data(), text)
            st.write("Transcription:", text)
            return text
        except:
            print("Sorry I didn't get that or you run out of time")
            st.write("Sorry I didn't get that/You run out of time")

def main():
    st.title("Chatbot with Text/Speech Input")

    # Choose input type: Text or Speech
    input_type = st.radio("Choose input type:", ["Text", "Speech"])

    user_input = ""
    if input_type == "Text":
        user_input = st.text_input("User:")
    elif input_type == "Speech":
        user_input = transcribe_speech()

    if user_input:
        response = chatbot(user_input)
        st.write("Chatbot:", response)

if __name__ == "__main__":
    main()


import os
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # da scegliere tra {'0', '1', '2'}
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.nasnet import preprocess_input
from keras.applications.nasnet import decode_predictions
from keras.applications.nasnet import NASNetLarge

filename = input("Inserisci il nome della foto: ")

print("Preprocessing dati...")
# carica la foto
image = load_img(filename,target_size=(331,331))

# immagine -> array
image = img_to_array(image)

# reshape (batch)
image = np.array([image])
#image = image.reshape((1,image.shape[0],image.shape[1],image.shape[2]))

# preprocessing
image = preprocess_input(image)

# carico la cnn
print("Caricamento rete neurale...")
model = NASNetLarge(weights='imagenet')

#model.summary()
#input("Premi INVIO per continuare...")

# predict su tutte le classi in output
print("Running...")
prob = model.predict(image)

# probabilità -> label
label = decode_predictions(prob)

# i label più probabili
label1 = label[0][0]
label2 = label[0][1]
label3 = label[0][2]
label4 = label[0][3]
label5 = label[0][4]

# print
print('\nProbabilità:')
print('%s (%.2f%%)' % (label1[1],label1[2]*100))
print('%s (%.2f%%)' % (label2[1],label2[2]*100))
print('%s (%.2f%%)' % (label3[1],label3[2]*100))
print('%s (%.2f%%)' % (label4[1],label4[2]*100))
print('%s (%.2f%%)' % (label5[1],label5[2]*100))