import os
from typing import Optional, Union

import cv2
import imageio
import numpy as np
import torch
from end2endslam.dataloader.dataloader_utils.pykitti_odometry import odometry
from gradslam.geometry.geometryutils import relative_transformation
from torch.utils import data

from gradslam.datasets import datautils

__all__ = ["KITTI"]

class KITTI(data.Dataset):
    r"""
    Examples::

        >>> dataset = KITTI(
            basedir="KITTI-data/",
            sequences="sequences.txt"
        >>> loader = data.DataLoader(dataset=dataset, batch_size=4)
        >>> colors, depths, intrinsics, poses, transforms, names = next(iter(loader))

    """

    def __init__(
        self,
        basedir: str,
        sequences: Union[tuple, str, None] = None,
        seqlen: int = 4,
        dilation: Optional[int] = None,
        stride: Optional[int] = None,
        start: Optional[int] = None,
        end: Optional[int] = None,
        height: int = 512,
        width: int = 1392,
        channels_first: bool = False,
        normalize_color: bool = False,
        *,
        return_dummy_depth: bool = True,
        return_intrinsics: bool = True,
        return_pose: bool = True,
    ):
        super(KITTI, self).__init__()

        basedir = os.path.normpath(basedir)
        self.height = height
        self.width = width
        self.height_downsample_ratio = float(height) / 512
        self.width_downsample_ratio = float(width) / 1392
        self.channels_first = channels_first
        self.normalize_color = normalize_color
        self.return_intrinsics = return_intrinsics
        self.return_pose = return_pose
        self.return_dummy_depth = return_dummy_depth

        if not isinstance(seqlen, int):
            raise TypeError('"seqlen" must be int. Got {0}.'.format(type(seqlen)))
        if not (isinstance(stride, int) or stride is None):
            raise TypeError(
                '"stride" must be int or None. Got {0}.'.format(type(stride))
            )
        if not (isinstance(dilation, int) or dilation is None):
            raise TypeError(
                "dilation must be int or None. Got {0}.".format(type(dilation))
            )
        dilation = dilation if dilation is not None else 0
        stride = stride if stride is not None else seqlen * (dilation + 1)
        self.seqlen = seqlen
        self.stride = stride
        self.dilation = dilation
        if seqlen < 0:
            raise ValueError('"seqlen" must be positive. Got {0}.'.format(seqlen))
        if dilation < 0:
            raise ValueError('"dilation" must be positive. Got {0}.'.format(dilation))
        if stride < 0:
            raise ValueError('"stride" must be positive. Got {0}.'.format(stride))

        if not (isinstance(start, int) or start is None):
            raise TypeError('"start" must be int or None. Got {0}.'.format(type(start)))
        if not (isinstance(end, int) or end is None):
            raise TypeError('"end" must be int or None. Got {0}.'.format(type(end)))
        start = start if start is not None else 0
        self.start = start
        self.end = end
        if start is not None and start < 0:
            raise ValueError(
                '"start" must be None or positive. Got {0}.'.format(stride)
            )
        if not (end is None or end > start):
            raise ValueError(
                '"end" ({0}) must be None or greater than start ({1})'.format(
                    end, start
                )
            )

        # preprocess sequences to be a tuple or None
        if isinstance(sequences, str):
            if os.path.isfile(sequences):
                with open(sequences, "r") as f:
                    sequences = tuple(f.read().split("\n"))[:-1]
            else:
                raise ValueError(
                    "incorrect filename: {} doesn't exist".format(sequences)
                )
        else:
            raise ValueError(
                "Sequences must be .txt file with sequences"
            )

        sequence_paths = []
        # check folder structure for sequence:
        for item in os.listdir(os.path.join(basedir, "sequences")):
            if os.path.isdir(os.path.join(basedir, "sequences", item)):
                if sequences is None or (sequences is not None and item in sequences):
                    sequence_paths.append(os.path.join(basedir, "sequences", item))

        if len(sequence_paths) == 0:
            raise ValueError(
                'Incorrect folder structure in basedir ("{0}"). '.format(basedir)
            )
        if sequences is not None and len(sequence_paths) != len(sequences):
            msg = '"sequences" contains sequences not available in basedir:\n'
            msg += '"sequences" contains: ' + ", ".join(sequences) + "\n"
            msg += '"basedir" contains: ' + ", ".join(sequence_paths) + "\n"
            raise ValueError(msg)

        # get association and pose file paths
        colorfiles, depthfiles, poses = [], [], []
        idx = np.arange(seqlen) * (dilation + 1)
        for sequence_path in sequence_paths:
            seq_name = os.path.split(sequence_path)[-1]
            seq_poses = odometry(basedir, seq_name).poses
            seq_colorfiles = []
            for filepath in sorted(os.listdir(os.path.join(sequence_path, "image_3"))):
                seq_colorfiles.append(os.path.join(sequence_path, "image_3", filepath)) #Todo: Remove hard-coded image_3
            num_frames = len(seq_colorfiles)
            for start_ind in range(0, num_frames, stride):
                if (start_ind + idx[-1]) >= num_frames:
                    break
                inds = start_ind + idx
                colorfiles.append([seq_colorfiles[i] for i in inds])

                if self.return_pose:
                    poses.append([seq_poses[i] for i in inds])

        self.num_sequences = len(colorfiles)

        # Class members to store the list of valid filepaths.
        self.colorfiles = colorfiles
        self.poses = poses

        # Camera intrinsics matrix for KITTI dataset #TODO: not only cam3
        K_cam3 = odometry(basedir, seq_name).calib.K_cam3
        intrinsics_np = np.zeros((4, 4))
        intrinsics_np[:3, :3] = K_cam3
        intrinsics_np[3, 3] = 1
        intrinsics = torch.tensor(
            intrinsics_np
        ).float()
        self.intrinsics = datautils.scale_intrinsics(
            intrinsics, self.height_downsample_ratio, self.width_downsample_ratio
        ).unsqueeze(0)

    def __len__(self):
        r"""Returns the length of the dataset. """
        return self.num_sequences

    def __getitem__(self, idx: int):
        r"""Returns the data from the sequence at index idx.

        Returns:
            color_seq (torch.Tensor): Sequence of rgb images of each frame
            depth_seq (torch.Tensor): Sequence of depths of each frame
            pose_seq (torch.Tensor): Sequence of poses of each frame
            transform_seq (torch.Tensor): Sequence of transformations between each frame in the sequence and the
                previous frame. Transformations are w.r.t. the first frame in the sequence having identity pose
                (relative transformations with first frame's pose as the reference transformation). First
                transformation in the sequence will always be `torch.eye(4)`.
            intrinsics (torch.Tensor): Intrinsics for the current sequence
            framename (str): Name of the frame
            timestamp_seq (str): Sequence of timestamps of matched rgb, depth and pose stored
                as "rgb rgb_timestamp depth depth_timestamp pose pose_timestamp\n".

        Shape:
            - color_seq: :math:`(L, H, W, 3)` if `channels_first` is False, else :math:`(L, 3, H, W)`. `L` denotes
                sequence length.
            - depth_seq: :math:`(L, H, W, 1)` if `channels_first` is False, else :math:`(L, 1, H, W)`. `L` denotes
                sequence length.
            - pose_seq: :math:`(L, 4, 4)` where `L` denotes sequence length.
            - transform_seq: :math:`(L, 4, 4)` where `L` denotes sequence length.
            - intrinsics: :math:`(1, 4, 4)`
        """

        # Read in the color, depth, pose, label and intrinstics info.
        color_seq_path = self.colorfiles[idx]

        color_seq, pose_seq = [], []
        for i in range(self.seqlen):
            color = np.asarray(imageio.imread(color_seq_path[i]), dtype=float)
            color = self._preprocess_color(color)
            color = torch.from_numpy(color)
            color_seq.append(color)

        output = []
        color_seq = torch.stack(color_seq, 0).float()
        output.append(color_seq)

        if self.return_dummy_depth:
            output.append([])

        if self.return_intrinsics:
            intrinsics = self.intrinsics
            output.append(intrinsics)

        if self.return_pose:
            poses = self.poses[idx]
            pose_seq = [torch.from_numpy(pose) for pose in poses]
            pose_seq = torch.stack(pose_seq, 0).float()
            pose_seq = self._preprocess_poses(pose_seq)
            output.append(pose_seq)

        return tuple(output)

    def _preprocess_color(self, color: np.ndarray):
        r"""Preprocesses the color image by resizing to :math:`(H, W, C)`, (optionally) normalizing values to
        :math:`[0, 1]`, and (optionally) using channels first :math:`(C, H, W)` representation.

        Args:
            color (np.ndarray): Raw input rgb image

        Retruns:
            np.ndarray: Preprocessed rgb image

        Shape:
            - Input: :math:`(H_\text{old}, W_\text{old}, C)`
            - Output: :math:`(H, W, C)` if `self.channels_first == False`, else :math:`(C, H, W)`.
        """
        color = cv2.resize(
            color, (self.width, self.height), interpolation=cv2.INTER_LINEAR
        )
        if self.normalize_color:
            color = datautils.normalize_image(color)
        if self.channels_first:
            color = datautils.channels_first(color)
        return color

    def _preprocess_poses(self, poses: torch.Tensor):
        r"""Preprocesses the poses by setting first pose in a sequence to identity and computing the relative
        homogenous transformation for all other poses.

        Args:
            poses (torch.Tensor): Pose matrices to be preprocessed

        Returns:
            Output (torch.Tensor): Preprocessed poses

        Shape:
            - poses: :math:`(L, 4, 4)` where :math:`L` denotes sequence length.
            - Output: :math:`(L, 4, 4)` where :math:`L` denotes sequence length.
        """
        return relative_transformation(
            poses[0].unsqueeze(0).repeat(poses.shape[0], 1, 1), poses
        )


from django import forms

from .models import Source
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class SignUpForm(UserCreationForm):
    username = forms.CharField(max_length=30)
    email = forms.EmailField(max_length=200)

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2', )

class SourceCreateForm(forms.ModelForm):
    class Meta:
        model = Source
        fields = ['name','feed_url']

SORT_CHOICES = [
    ('-created','Published Date'),
    ('title','Title'),
    ('body','Description')
]

class PostSortForm(forms.Form):
    sort_choices = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=SORT_CHOICES, required=False)


"""bjtquestion URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from quiz.views import *
from users.views import *

urlpatterns = [
    url(r'^$', index),
    url(r'^index/$', index),
    url(r'^login/$', weblogin),
    url(r'^loginback/$', webloginback),
    url(r'^logout/$', weblogout),
    url(r'^main/$', main),
    url(r'^quiz/$', quiz),
    url(r'^submit/$', submit),
    url(r'^admin_interface/$', admin_interface),
    url(r'^admin_interface/history/$', history),
    url(r'^admin_interface/questions/$', questions),
    url(r'^question/query/$', query_question),
    url(r'^question/modify/$', modify_question),
    url(r'^question/delete/$', delete_question),
    url(r'^question/add/$', add_question),
    #url(r'^finished/$', finished),
    url(r'^rankings/$', rankings),
    url(r'^endquiz/$', endquiz),
    url(r'^readquestions/$', readquestions),
]


# File: CFServerModule.py
# ICS 32 - Connect Four

from collections import namedtuple
import socket

#CF: Connect Four
CFConnection = namedtuple(
    'CFConnection',
    ['socket', 'socket_in', 'socket_out'])

class CFProtocolError(Exception):
    pass

def connect(host: str, port: int) -> CFConnection:

    CF_socket = socket.socket()
    CF_socket.connect((host, port))

    # 'ron-cadillac.ics.uci.edu', 5501

    CF_input = CF_socket.makefile('r')
    CF_output = CF_socket.makefile('w')

    return CFConnection(CF_socket, CF_input, CF_output)

def close(connection: CFConnection) -> None:
    connection.socket_in.close()
    connection.socket_out.close()
    connection.socket.close()

def welcome(connection: CFConnection, username: str) -> bool:
    _write_line(connection, f'I32CFSP_HELLO {username}')

    response = _read_line(connection)
    responseList = response.split()
    if ' ' in username:
        close(connection)
        print('Not a valid username')
        return False
    else:
        if responseList[0] == 'WELCOME':
            return True
        elif responseList[0].startswith('NO_USER '):
            return False
        else:
            raise CFProtocolError()

def start_game(connection: CFConnection) -> bool:
    _write_line(connection, f'AI_GAME')

    response = _read_line(connection)
    responseList = response.split()
    if responseList[0] == 'READY':
        return True
    else:
        raise CFProtocolError()

#"private function": functions there to make it easier
# to write *this* module, but not intended for use in others
# their names begin with an underscore

def _write_line(connection: CFConnection, line:str) -> None:
    connection.socket_out.write(line + '\r\n')
    connection.socket_out.flush()

def _read_line(connection: CFConnection) -> str:
    return connection.socket_in.readline()[:-1]

# REMOVE LATER

if __name__ == '__main__':
    host = 'ron-cadillac.ics.uci.edu'
    connection = connect(host, 4444)
    welcome(connection, 'Jon')
    start_game(connection)


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("createListing", views.createListing, name="createListing"),
    path("watchlist", views.watchlist, name="watchlist"),
    path("categories", views.categories, name="categories" ),
    path("<int:categoryId>/listing", views.categoryListing, name="categoryListing"),
    path("listing/<int:listingId>", views.listingPage, name="listingPage")
]


import requests
import pandas as pd
import numpy as np

def financial_statement(year, season, type='綜合損益彙總表'):

    if year >= 1000:
        year -= 1911

    if type == '綜合損益彙總表':
        url = 'https://mops.twse.com.tw/mops/web/ajax_t163sb04'
    elif type == '資產負債彙總表':
        url = 'https://mops.twse.com.tw/mops/web/ajax_t163sb05'
    elif type == '營益分析彙總表':
        url = 'https://mops.twse.com.tw/mops/web/ajax_t163sb06'
    else:
        print('type does not match')

    r = requests.post(url, {
        'encodeURIComponent':1,
        'step':1,
        'firstin':1,
        'off':1,
        'TYPEK':'sii',
        'year':str(year),
        'season':str(season),
    })

    r.encoding = 'utf8'
    dfs = pd.read_html(r.text, header=None)

    return pd.concat(dfs[1:], axis=0, sort=False)\
             .set_index(['公司代號'])\
             .apply(lambda s: pd.to_numeric(s, errors='ceorce'))

print(financial_statement(108, 3)) # 顯示 10


from django import forms

from cities.models import City

from routes.models import Route

from trains.models import Train

class RouteForm(forms.Form):

    from_town = forms.ModelChoiceField(
        label='Місто відбуття',
        queryset=City.objects.all(),
        widget=forms.Select(
            attrs={
                'class': 'form-control js-example-basic-single'}))

    to_town = forms.ModelChoiceField(
        label='Місто прибуття',
        queryset=City.objects.all(),
        widget=forms.Select(
            attrs={
                'class': 'form-control js-example-basic-single'}))

    travel_time = forms.IntegerField(label="Час", widget=forms.NumberInput(
        attrs={'class': 'form-control', 'placeholder': 'Введіть ваш час'}))

    towns = forms.ModelMultipleChoiceField(
        label='Через міста',
        queryset=City.objects.all(),
        required=False,
        widget=forms.SelectMultiple(
            attrs={
                'class': 'form-control js-example-basic-multiple'}))

    class Meta:
        model = Route
        fields = ('__all__')

class RouteModelForm(forms.ModelForm):
    name = forms.CharField(label='Назва маршруту', widget=forms.TextInput(
        attrs={
            'class': 'form-control',
            'placeholder': 'Введіть назву маршруту'}))

    from_town = forms.ModelChoiceField(
        queryset=City.objects.all(),
        widget=forms.HiddenInput())

    to_town = forms.ModelChoiceField(
        queryset=City.objects.all(),
        widget=forms.HiddenInput())

    travel_time = forms.IntegerField(widget=forms.HiddenInput())

    trains = forms.ModelMultipleChoiceField(
        queryset=Train.objects.all(),
        required=False,
        widget=forms.SelectMultiple(
            attrs={
                'class': 'form-control d-none'}))

    class Meta:
        model = Route
        fields = ('__all__')


import itertools
import more_itertools as mi

def is_valid(to_valid, numbers):
    for i in itertools.combinations(numbers, 2):
        if sum(i) == to_valid:
            return True
    return False

def find_target_sum(target, liste):
    for i in range(2, len(liste)):
        for window in mi.windowed(liste, i):
            if sum(window) == target:
                return window
    return

with open("input", 'r') as inp:
    complete_list = list(int(line) for line in inp)
    for *window, to_valid in mi.windowed(complete_list, 26):
        if not is_valid(to_valid, window):
            break
    print(to_valid)

    result = find_target_sum(to_valid, complete_list)
    if result:
        print(result)


'''
Data Loading and Normalisation
'''

import pandas as pd
import numpy as np
import os
import datetime
import matplotlib.pyplot as plt
from constants import *

def dateparse(dates):
    new_dates = []
    for d in dates:
        try:
            if ("/" in d):
                if len(d.split("/")[2]) == 4:
                    new_dates.append(pd.datetime.strptime(d, '%d/%m/%Y'))
                else:
                    new_dates.append(pd.datetime.strptime(
                        d, '%d/%m/%y').strftime('%d/%m/%Y'))

            if ("-") in d:
                d = d[2:]
                new_dates.append(pd.datetime.strptime(d, '%y-%m-%d'))
        except:
            new_dates.append(np.nan)
    return new_dates

period_data = pd.read_csv(
    "data/Period.csv", parse_dates=['start_date', 'end_date'], date_parser=dateparse)
period_data.dropna(inplace=True)

symptom_data = pd.read_csv(
    "data/Symptom.csv", parse_dates=['date'], date_parser=dateparse)
symptom_data.dropna(inplace=True)

user_data = pd.read_csv(
    "data/User.csv", parse_dates=['dob'], date_parser=dateparse)

symptom_data.drop(columns=['id'], inplace=True)
period_data.drop(columns=['id'], inplace=True)

# Creation of the ultimate table
new_data = pd.merge(period_data, symptom_data,
                    left_on='User_id', right_on='user_id', how='inner')
new_data = pd.merge(new_data, user_data, left_on='User_id',
                    right_on='id', how='inner')
new_data.drop(columns=['User_id', 'id'], inplace=True)

new_data = new_data.query('end_date > start_date')
new_data["last_date"] = new_data["end_date"] + pd.to_timedelta(
    new_data["cycle_length_initial"], unit='D') - pd.to_timedelta(new_data["period_length_initial"], unit='D')
new_data["current_day"] = (
    abs((new_data["date"] - new_data['start_date']).dt.days))
new_data = new_data[new_data.current_day > 0]
new_data = new_data[new_data.current_day < new_data.cycle_length_initial]
new_data["stage"] = new_data["current_day"] / new_data["cycle_length_initial"]

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(new_data, new_data, test_size=0.01, random_state=22)
new_data = X_train
# X_test = X_test[symptoms]
# y_test = y_test["current_day"]


"""
    Database cursor script to handle transactions with the database
    sqlalchemy documentation link ==>
        http://docs.sqlalchemy.org/en/latest/orm/tutorial.html

"""

from contextlib import contextmanager
from sqlalchemy.orm import sessionmaker

# +-------------------------+-------------------------+
# User defined imports
# +-------------------------+-------------------------+

from dbforms.ormdb import db_schema, inventory  # imported from the orm_dataaccess.py file

# +-------------------------+-------------------------+
# +-------------------------+-------------------------+

CursorObj = sessionmaker(bind=db_schema)

# +-------------------------+-------------------------+
# +-------------------------+-------------------------+

@contextmanager  # decorator to handle database connections
def sql_cursor():
    cursor = CursorObj()

    yield cursor

    try:
        cursor.commit()
    except Exception as e:
        cursor.rollback()
        raise e
        # add a logger here

# +-------------------------+-------------------------+
# +-------------------------+-------------------------+

def test_record_insert():
    student = INVENTORY_TB()
    student.fname = 'jeff'
    student.sname = 'uzodinma'
    student.age = 1
    student.address = '7, ashaye street, obawole, lagos nigeria'

    with sql_cursor() as db:
        db.add(student)

# +-------------------------+-------------------------+
# +-------------------------+-------------------------+

def test_record_select():

    print(output)
    with sql_cursor() as db:
        output = db.query(Student.fname, Student.sname
                ).filter(
                    Student.fname == 'jeff'
                ).first()

# +-------------------------+-------------------------+
# +-------------------------+-------------------------+

def test_record_update():
    with sql_cursor() as db:
        db.query(Student).filter(
            INVENTORY.sname == 'uzodinma'
        ).update({'age': 30})

# +-------------------------+-------------------------+
# +-------------------------+-------------------------+

def test_record_delete():
    with sql_cursor() as db:
        db.query(INVENTORY).filter(INVENTORY.fname == 'jeff').delete()

# +-------------------------+-------------------------+
# +-------------------------+-------------------------+

if __name__ == '__main__':
    # test_record_insert()  ## let insert a first record
    # test_record_select()  # lets select the newly created record
    # test_record_update() # lets update a record on the table
    test_record_delete()  # lets delete the records from the database


import numpy as np
import argparse
from sklearn.metrics import f1_score

import warnings
warnings.filterwarnings('ignore')

# evaluation.py
def read_prediction(prediction_file):
   # NEED TO IMPLEMENT #1
   # function that loads prediction
   pred_array = np.loadtxt(prediction_file, dtype=np.int16)
   return pred_array

def read_ground_truth(ground_truth_file):
   # NEED TO IMPLEMENT #2
   # function that loads test_data
   gt_array = np.loadtxt(ground_truth_file, dtype=np.int16)
   return gt_array

# f1
def evaluate(prediction, ground_truth):
   # NEET TO IMPLEMENT #3
   # Function that actually evaluates
   return f1_score(ground_truth, prediction, average='macro')

# user-defined function for evaluation metrics
def evaluation_metrics(prediction_file: str, ground_truth_file: str):
   # read prediction and ground truth from file
   prediction = read_prediction(prediction_file)  # NOTE: prediction is text
   ground_truth = read_ground_truth(ground_truth_file)
   return evaluate(prediction, ground_truth)

if __name__ == '__main__':
   args = argparse.ArgumentParser()
   # --prediction is set by file's name that contains the result of inference. (nsml internally sets)
   # prediction file requires type casting because '\n' character can be contained.
   args.add_argument('--prediction', type=str, default='pred.txt')
   config = args.parse_args()
   # When push dataset, if you push with leaderboard option, automatically test_label is existed on test/test_label path, set to proper path.
   # You should type dataset's name that wants to upload on [dataset] section.
   test_label_path = '/data/16_tcls_movie/test/test_label'
   # print the evaluation result
   # evaluation prints only int or float value.

   try:
      print(evaluation_metrics(config.prediction, test_label_path))
   except:
      print(0.0)


import torch
from sup_mmd.data import MMD_Dataset
import sys, json, re, os, glob, shutil
from commons.utils import get_logger
import numpy as np
from torch.utils.tensorboard import SummaryWriter
from sup_mmd.model import LinearModel1, mmd_loss_pq, LinearModelComp1, mmd_loss_pq_comp
from sup_mmd.model import MMD, MMD_comp
from submodular.maximize import greedy as greedy_maximize
import pandas as pd
from copy import copy
from sup_mmd.functions import softmax, nz_median_dist, combine_kernels
import multiprocessing as mp
import pandas as pd
from sup_mmd.compress import SentenceCompressor

logger = get_logger("Infer")
GPU_MODE = False

pattern_generic = re.compile(r'(mmdpq?)(_)(.?)(duc03|duc04|tac08|tac09)-([AB])_([xc])\.(\d+|x)_g(\d\.?\d*)_b(\d\.?\d*)_a(\d\.?\d*)(_)SF(b|x)(k|x)(c|x)')
pattern_update = re.compile(r'(mmdpq?)-comp([01])\.(lin1|lin2)_(tac08|tac09)-([AB])_([xc])\.(\d+|x)_g(\d\.?\d*)_b(\d\.?\d*)_a(\d+)_l(\d\.?\d*)_SF(b|x)(k|x)(c|x)')

TARGET_NAME = "y_hm_0.4"
BUDGET = 150 ##aftre compression, it will be less
#### ROUGE eval truncates, so > 100 words will be truncated

CACHE_ROOT = "./data/"
# NOTE: SRL model available at: https://s3-us-west-2.amazonaws.com/allennlp/models/srl-model-2018.05.25.tar.gz
SRL_PATH = "models/srl-model-2018.05.25.tar.gz"

model_path = sys.argv[1]
logger.info("processing model:{}".format(model_path))
r = float(sys.argv[2])
COMPRESSED = sys.argv[3] == "1"
if COMPRESSED:
    logger.warning("compressed mode on, this will be slow")
    sc = SentenceCompressor(SRL_PATH)

def write_result(group, set_, subset, S, name, r, epochs, root, **kwargs):
    sents = subset["sent"].iloc[S].values
    fname = "%s%s.M.100.X.%s_r%.2g_ep%d_x"%( group.upper(), set_, name, r, epochs)
    with open(root + "/summaries/" + fname, "w") as fp:
        fp.write("\n".join(sents))

    if COMPRESSED:
        fname = "%s%s.M.100.X.%s_r%.2g_ep%d_c"%( group.upper(), set_, name, r, epochs)
        sents = [ min(sc.compress(s), key = len) for s in sents ]
        with open(root + "/summaries/" + fname, "w") as fp:
            fp.write("\n".join(sents))
    return

def infer():
    model_file = model_path.split("/")[-1]
    s = pattern_generic.search(model_file)
    generic = True ## MMD() or MMD() - lambda*MMD()
    if not s:
        s = pattern_update.search(model_file)
        if not s:
            logger.error("pattern not matched with both generic/update regexes, quitting " + model_file)
            return
        generic = False

    name = s.group(0)
    loss_name = s.group(1)

    assert loss_name in {"mmdpq"}
    train_dataset = s.group(4).lower()
    assert train_dataset in {"duc03", "duc04", "tac08", "tac09"}
    set_ = s.group(5)
    assert set_ in {"A", "B"}
    if not generic or set_ == "B":
        assert train_dataset in {"tac08", "tac09"}
    compress = s.group(6) == "c"
    split_seq = s.group(7)

    if split_seq != "x":
        logger.warning("please supply retrained model")

    gamma1 = float(s.group(8))
    beta = float(s.group(9))
    alpha_seq = s.group(10)
    lambdaa = 0.0

    lambdaa, diff, model_name = s.group(11), s.group(2), s.group(3)

    if not generic:
        assert set_ == "B"
        lambdaa = float(s.group(11))
        diff = int(s.group(2))
        model_name = s.group(3)

    BOOST_FIRST = s.group(12) == "b"
    KEYWORDS = s.group(13) == "k"
    comp_feats = s.group(14) =="c"

    logger.debug((name, list(s.groups()), (loss_name, diff, model_name, train_dataset, set_, compress, split_seq, gamma1, beta, alpha_seq, lambdaa, BOOST_FIRST, KEYWORDS, comp_feats) ) )

    dataset = {
        "duc03": "duc04",
        "duc04": "duc03",
        "tac08": "tac09",
        "tac09": "tac08"
    }[train_dataset]

    dataset_name = "{}_{}".format(dataset, TARGET_NAME)

    logger.debug("loading data from " + dataset_name)
    data = MMD_Dataset.load(dataset_name, CACHE_ROOT, compress = compress)
    SURF_IDXS = data.surf_idxs(keywords = KEYWORDS, boost_first = BOOST_FIRST, comp = ( comp_feats and set_ == "B" ) )

    logger.info("surf feats: {}".format(
        ",".join( np.array(data.surf_names)[SURF_IDXS] )
    ))

    logger.debug("loading model from " + model_path)
    try:
        model, alpha, train_idxs, val_idxs, epochs = LinearModel1.load(len(SURF_IDXS), model_path)
    except:
        model, alpha, train_idxs, val_idxs, epochs = LinearModelComp1.load(len(SURF_IDXS), len(SURF_IDXS), model_path)

    idxs = np.arange( len(data) ).tolist()

    root = "./{}_{}/".format( dataset, set_ )

    if not os.path.exists(root + "summaries"):
        try:
            os.makedirs(root + "summaries")
        except:
            pass
    shutil.copy2( model_path, root )

    logger.debug("Dataset and model loaded, begin inference with #topics={}, generic?={}".format( len(idxs), generic ))

    if GPU_MODE:
        model.cuda()

    lengthsS = []
    jobs = []
    pool = mp.Pool(24)
    dfs = []

    for ix in idxs :
        group = data.groups[ix]
        subset = data.get_subset_df(group, set_ )
        write_df = subset[["position", "doc_sents", "sent_id", "group", "set", "doc_id", "num_words",
                    "R1.R", "R1.P", "R2.R", "R2.P", "nouns", "prpns", "target"]]
        surf_names = np.array([s.strip() for s in data.surf_names])[SURF_IDXS]

        if generic:
            if train_dataset in ["duc03", "duc04"]:
                K, X, _, _ = data[ix]
            elif train_dataset in ["tac08", "tac09"]:
                if set_ == "A":
                    # logger.info("A")
                    K, _, _, X, _, _, _, _, _ = data[ix]
                else:
                    # logger.info("B")
                    _, K, _, _, X, _, _, _, _ = data[ix]

            K, X = K.squeeze(), X.squeeze()[:, SURF_IDXS]
            fg = model.forward( X )[0]
            K_combined = combine_kernels(K, alpha, gamma1)
            mmd = MMD(K_combined, fg)

        else:
            KA, KB, KAB, XA, X, _, _, _, _ = data[ix]
            KA, XA = KA.squeeze(), XA.squeeze()[:, SURF_IDXS]
            KB, X = KB.squeeze(), X.squeeze()[:, SURF_IDXS]
            KAB = KAB.squeeze()

            fA, fg = model.forward( XA, X )
            KA_combined = combine_kernels(KA, alpha, gamma1)
            KB_combined = combine_kernels(KB, alpha, gamma1)
            KAB_combined = combine_kernels(KAB, alpha, gamma1)
            mmd = MMD_comp( KB_combined, KA_combined, KAB_combined, fg, fA, lambdaa = lambdaa, diff = diff)
            write_df["nf"] = X[:, np.where(surf_names=="nf")[0]] #normalised

        write_df["lexrank"] = X[:, np.where(surf_names=="lexrank")[0]].numpy() + 1.0
        write_df["tfisf"] = X[:, np.where(surf_names=="tfisf")[0]].numpy() ## normalised
        write_df["btfisf"] = X[:, np.where(surf_names=="btfisf")[0]].numpy() #normalised
        write_df["scores"] = softmax(fg.detach().numpy()) * len(subset)

        dfs.append(write_df)
        lengths = subset["num_words"].values
        keys = None
        if compress:
            keys = [int(sid.split("-")[0]) for sid in subset["sent_id"]]
        S, cost = greedy_maximize(mmd, budget = BUDGET,
                    costs = copy(lengths), r = r, verbose = False, keys = keys)
        lengthsS.append(len(S))

        jobs.append( pool.apply_async(write_result, (group, ("-%s"%set_) if len(data.sets) > 1 else "", subset, S, name, r, epochs, root )))

    logger.info("{}-{}:{}, |S|:{:.2g}, #topics:{}".format(
        dataset_name, set_,
        name, np.mean(lengthsS), len(idxs)
    ))
    pd.concat(dfs).to_csv("{}/sents.csv".format(root), index = False, float_format='%.6g')
    for job in jobs:
        job.get()
    pool.close()
    logger.info("Done")

# def infer_all():

if __name__ == "__main__":
    infer()


import sys
from collections import deque

n, m = map(int, sys.stdin.readline().split())
arr = []
is_complete = False
directions = [[1, 0], [0, -1], [-1, 0], [0, 1]]

cheese_num = 0
days = 0

for _ in range(n):
    sub_arr = list(map(int, sys.stdin.readline().split()))

    for i in sub_arr:
        if i == 1:
            cheese_num += 1

    arr.append(sub_arr)

while cheese_num > 0:
    queue = deque([(0, 0)])
    visited = [[0] * m for _ in range(n)]
    cheese = []

    while queue:
        v = queue.popleft()

        for d in directions:
            row = v[0] + d[0]
            col = v[1] + d[1]

            if 0 <= row < n and 0 <= col < m:
                if arr[row][col] == 0 and not visited[row][col]:
                    visited[row][col] = 1
                    queue.append((row, col))

                elif arr[row][col]:
                    arr[row][col] += 1
                    if (row, col) not in cheese:
                        cheese.append((row, col))

    for r, c in cheese:
        if arr[r][c] > 2:
            cheese_num -= 1
            arr[r][c] = 0

        else:
            arr[r][c] = 1

    days += 1

print(days)


# Import Dependencies
import numpy as np
import datetime as dt
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
from flask import Flask, jsonify

#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///../Resources/hawaii.sqlite")

# reflect an existing database into a new model
Base = automap_base()

# reflect the tables
Base.prepare(autoload_with=engine)

# Save reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station

# Create session (link) from Python to the DB
session = Session(engine)

#################################################
# Flask Setup
#################################################
app = Flask(__name__)

# Create Global variables defining most recent date, date 12 months prior, and most active station
    # Determine most recent date in the data set
mosrec_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()

    # Calculate the date one year from the last date in data set.
prev_year = dt.datetime.strptime(mosrec_date[0], "%Y-%m-%d") - dt.timedelta(days=365)

    # Query to find the most active station
most_active = session.query(Measurement.station, func.count(Measurement.tobs)).\
    group_by(Measurement.station).\
    order_by(func.count(Measurement.tobs).desc()).first()

#################################################
# Flask Routes
#################################################
@app.route("/")
def welcome():
    """List all available api routes."""
    return (
        f"
Welcome to Surf's Up Weather Alchemy!
"
        f"Available Routes:
"
        f"/api/v1.0/precipitation
"
        f"(Date and precipitation amounts from the most recent 12 months of available data)

"

        f"/api/v1.0/stations
"
        f"(A list of our observation stations)

"

        f"/api/v1.0/tobs
"
        f"(Temperature observations from the most recent 12 months of available data at our most active station)

"

        f"/api/v1.0/<start>
"
        f"(Find the minimum, average and max temperature since a given start day)

"

        f"/api/v1.0/<start>/<end>
"
        f"(Find the minimum, average and max temperature for a given range of days - start to end)

"
    )

# Setup precipitation Route
@app.route("/api/v1.0/precipitation")
def precipitation():
    """Return a JSON representation of a dictionary for precipitation within the most recent 12 months"""
    # Query date and precipitation values for the most recent year of data only
    prevyear_prcp = session.query(Measurement.date, Measurement.prcp).\
    filter(Measurement.date >= prev_year).\
    order_by(Measurement.date).all()

    # Convert queried list of tuples into dictionary with date as key and prcp as value
    prevyear_precipitation = dict((x, y) for x, y in prevyear_prcp)

    return jsonify(prevyear_precipitation)

# Setup stations Route
@app.route("/api/v1.0/stations")
def stations():
    """Return a JSON list of all stations"""
    # Query all stations
    station_data = session.query(Station.name).all()

    # Convert queried list of tuples into normal list
    all_stations = list(np.ravel(station_data))

    return jsonify(all_stations)

# Setup tobs Route
@app.route("/api/v1.0/tobs")
def tobs():
    """Return tobs data for only the past year of the most active station"""
    # Query only the most active station's tobs for the most recent year of data
    tobs_data = session.query(Measurement.date, Measurement.tobs).\
    filter(Measurement.station == most_active[0]).\
    filter(Measurement.date >= prev_year).all()

    # Convert queried list of tuples into dictionary with date as key and tobs as value
    most_active_station = dict((x, y) for x, y in tobs_data)
    # most_active_station = list(np.ravel(results))

    return jsonify(most_active_station)

# Setup <start> Route
@app.route("/api/v1.0/<start>")
def start_temps(start):
    """Return a JSON list of the tmin, tavg, and tmax temperatures for the range
    beginning from the date variable supplied by the user."""
    # Query list for tmin, tavg & tmax temps
    defsel = [func.min(Measurement.tobs),
        func.avg(Measurement.tobs),
        func.max(Measurement.tobs)]

    # Query data by unpacking query list and using user provided start date
    start_range = session.query(*defsel).\
        filter(Measurement.date >= start).all()

    # Convert queried list of tuples into normal list
    start_results = list(np.ravel(start_range))

    return jsonify(start_results)

# Setup <start>/<end> Route
@app.route("/api/v1.0/<start>/<end>")
def start_end_temps(start, end):
    """Return a JSON list of the tmin, tavg, and tmax temperatures for the range
    beginning from the start date to the end date variables supplied by the user."""
    # Create Query list for tmin, tavg & tmax temps
    defsel = [func.min(Measurement.tobs),
        func.avg(Measurement.tobs),
        func.max(Measurement.tobs)]

    # Query data by unpacking query list and using user provided start and end dates
    start_end_range = session.query(*defsel).\
        filter(Measurement.date >= start).\
        filter(Measurement.date <= end).all()

    # Convert queried list of tuples into normal list
    start_end_results = list(np.ravel(start_end_range))

    return jsonify(start_end_results)

session.close()

if __name__ == '__main__':
    app.run(debug=True)


from flask import redirect,render_template,url_for,Blueprint
from flask_login import current_user

noter_main = Blueprint('main',__name__)

@noter_main.route('/')
@noter_main.route('/home')
def home():
	if current_user.is_authenticated:
		return redirect(url_for('notes.notes'))
	return render_template("index.html",title = "Home - Noter")


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 15 19:58:26 2018

@author: David Gaviria
"""

#%%
import folium
map = folium.Map(
    location=[45.372, -121.6972],
    zoom_start=12,
    tiles='Mapbox Bright'
)

map.save('map.html')


#!/usr/bin/env python3.8

import rospy
from std_msgs.msg import Bool
from kivymd.app import MDApp
from kivy.lang import Builder
from jkprobot.msg import Ardubot

class TutorialApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.slider_base_angle = 90
        self.slider_waist_angle = 90
        self.slider_shoulder_angle = 90

        self.screen = Builder.load_file('ros_gui.kv')

    def build(self, *args):
        return self.screen

    def my_function(self, *args):
        print('Botao pressionado!')

        self.screen.ids.my_label.text = 'Botao pressionado'

        msg = Ardubot() #Create a Ardubot message object
        msg.base_deg = self.slider_base_angle # base
        msg.waist_deg = self.slider_waist_angle # cintura
        msg.shoulder_deg = self.slider_shoulder_angle # ombro

        pub.publish(msg)

    def slider_base_function(self, slider_base_value):
        self.slider_base_angle = int(slider_base_value)
        print(self.slider_base_angle)

    def slider_waist_function(self, slider_waist_value):
        self.slider_waist_angle = int(slider_waist_value)
        print(self.slider_waist_angle)

    def slider_shoulder_function(self, slider_shoulder_value):
        self.slider_shoulder_angle = int(slider_shoulder_value)
        print(self.slider_shoulder_angle)

if __name__=='__main__':

    pub = rospy.Publisher('/button', Ardubot, queue_size=1)

    rospy.init_node('simple_gui',anonymous=True)

    TutorialApp().run()


#==============================================================================#
#     Types of Item Similarity Measures for RecSys using PySpark
#==============================================================================#

from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
from pyspark.mllib.linalg.distributed import IndexedRow, IndexedRowMatrix
from pyspark.mllib.linalg import Vectors

df_app = spark.createDataFrame([('1001', 'app_1'), ('1001', 'app_2'), ('1001', 'app_3'), ('1002', 'app_1'),
                                ('1002', 'app_4'), ('1003', 'app_1'), ('1004', 'app_1'), ('1004', 'app_4')],
                                ['userid', 'appid'])

# 1. COSINE SIMILARITY
"""
Cosine similarity measures the angle between two vectors. It can be easily calculated by calling the Spark’s native function.
"""

def get_pd_norm():
  mat = IndexedRowMatrix(df_matrix.rdd.map(lambda row: IndexedRow(row[0], Vectors.dense(row[1:]))))
  array_norm = mat.toRowMatrix().computeColumnSummaryStatistics().normL2()
  pdf_norm = pd.DataFrame(array_norm, columns=["norm"]).reset_index()
  return mat, pdf_norm

mat, pdf_norm = get_pd_norm()

# get cosine similarity
df_cosine_similarity = mat.columnSimilarities().entries.toDF()
pdf_similarity = df_cosine_similarity.toPandas()
pdf_similarity = pdf_similarity.rename({'value': 'cosine_similarity'}, axis=1)

# merge norm
pdf_similarity = pdf_similarity.merge(pdf_norm, left_on="i", right_on="index", how="left")
pdf_similarity = pdf_similarity.rename(columns={"norm": "norm_i"})
pdf_similarity = pdf_similarity.drop(["index"], axis=1)

pdf_similarity = pdf_similarity.merge(pdf_norm, left_on="j", right_on="index", how="left")
pdf_similarity = pdf_similarity.rename(columns={"norm": "norm_j"})
pdf_similarity = pdf_similarity.drop(["index"], axis=1)

# 2. DOT PRODUCT
"""
Dot product is cosine similarity multiplied by the euclidean magnitudes of the two vectors. It can be understood as the projection of one vector on the
other vector. The euclidean magnitude of a vector measures the popularity of the app among the users. When more users install an app,
the euclidean magnitude of this app will become larger. Thus, unlike cosine similarity, dot product is affected by the popularity of the two apps.
Dot product of a popular app and an unpopular app will be small, as if we imagine when the short vector is projected on the long vector, the projection length won’t be long.
"""

pdf_similarity['dot_product'] = pdf_similarity['cosine_similarity'] * pdf_similarity["norm_i"] * pdf_similarity["norm_j"]

# 3. JACCARD SIMILARITY
"""
Jaccard similarity is the intersaction of the user sets divided by the union of the user sets who install the two apps.
The intersaction of the user sets measures the similarity of the two apps, while the union of the user sets measures the diversity of the two apps.
"""
pdf_similarity["jaccard_similarity"] = pdf_similarity['dot_product'] / (pdf_similarity["norm_i"] ** 2 + pdf_similarity["norm_j"] ** 2 - pdf_similarity['dot_product'])

# 4. CONDITIONAL PROBABILITY LIFT
"""
Conditional probability lift measures to what extent the installment of app B helps with the installment of app A.
"""
vector_length = df_matrix.count()
pdf_similarity["cond_probability"] = vector_length * pdf_similarity["cosine_similarity"] / (pdf_similarity["norm_i"] * pdf_similarity["norm_j"])  # P(A|B)/P(A)

#     FINAL RESULTS OF THE SIMILARITY MEASURES
#==============================================================================#
# map i,j to app names

app_list = df_matrix.columns
app_list.remove("userid")
pd_app_list = pd.DataFrame(app_list, columns=["app_id"])
pd_app_list["i"] = pd_app_list.index

# merge to i
pdf_similarity = pdf_similarity.merge(pd_app_list, on="i", how="left")
pdf_similarity = pdf_similarity.rename(columns={"app_id": "app_id_i"})

# merge to j
pd_app_list = pd_app_list.rename(columns={"i": "j"})
pdf_similarity = pdf_similarity.merge(pd_app_list, on="j", how="left")
pdf_similarity = pdf_similarity.rename(columns={"app_id": "app_id_j"})
pdf_similarity = pdf_similarity[['app_id_i', 'app_id_j', 'cosine_similarity', 'dot_product', 'cond_probability', 'jaccard_similarity']]


import numpy as np
import os
import glob
import csv
import pandas

from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif, chi2
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix

script_dir = os.path.dirname(__file__)

def get_data(files):
    print(files)

    data_matrix = np.loadtxt(files[0], dtype='i', delimiter='\t') #loading data from first textfile
    data_matrix = data_matrix.T #transpond data_matrix array

    last_col = [0] * len(data_matrix)
    data_matrix = np.column_stack((data_matrix, last_col)) #adding one column to data_matrix

    for x in range(len(files) - 1):
        temp_matrix = np.loadtxt(files[x + 1], dtype='i', delimiter='\t') #loading data from textfile x
        temp_matrix = temp_matrix.T #transpod array
        last_col = [x+1] * len(temp_matrix)
        temp_matrix = np.column_stack((temp_matrix, last_col))
        data_matrix = np.concatenate((data_matrix, temp_matrix), axis=0) #join new matrix to the rest

    X = data_matrix[:, :-1]
    Y = data_matrix[:, -1]
    return X, Y

def classification(X, Y, activation_value, momentum_value, top_rank, layer_size, results):

    # Konfiguracja walidacji krzyzowej - Repeated Stratified K-Fold cross validator z biblioteki scikit-learn
    rskf = RepeatedStratifiedKFold(n_splits=2, n_repeats=5)

    # Konfiguracja modelu - Multi-layer Perceptron z biblioteki scikit-learn
    mlpc = MLPClassifier(hidden_layer_sizes=layer_size,
                        activation=activation_value, max_iter=1000, momentum=momentum_value)

    # Pętla dla walidacji krzyzowej
    for fold, (train, test) in enumerate(rskf.split(X, Y)):
        x_train, x_test = X[train], X[test]
        y_train, y_test = Y[train], Y[test]

        # Dopasowanie x_train do y_train
        mlpc.fit(x_train, y_train)

        # Przypisanie wyniku na podstawie nowych zbiorów
        score = mlpc.score(x_test, y_test)

        # predykcja z pomocą utworzonego klasyfikatora
        # predict = mlpc.predict(x_test)

        # print(score)

        # Tworzenie nowego rekordu z danymi wykonanego badania
        params = {"fold":fold,
            "layer_size":layer_size,
            "momentum_value":momentum_value,
            "activation_value":activation_value,
            "feature_number":len(top_rank)+1,
            "score":score}

        print(params)

        results = results.append(params, ignore_index=True)

    return results

def get_k_best(x, y, k):
    fvalue_selector = SelectKBest(f_classif, k=k)
    fvalue_selector.fit(x, y)
    return fvalue_selector.transform(x)

def main():
    # 1. Wyznaczenie rankingu cech
    files = glob.glob('sets/*.txt')
    X, Y = get_data(files)

    fvalue_selector = SelectKBest(f_classif)
    fvalue_selector.fit(X, Y) #runung score funtion

    rank = fvalue_selector.scores_
    top_rank = []
    indexes = rank.argsort()[::-1]

    print(indexes)

    for index in indexes:
        top_rank.append(rank[index])

    # Funcja do zapisywania pliku z rankinegim cech

    # with open('./rank.csv', mode='w') as csv_file:
    #     writer = csv.writer(csv_file)
    #     for i, val in enumerate(indexes):
    #         print('{}: {}'.format(val+1, top_rank[i]))
    #         writer.writerow([val+1, top_rank[i]])

    # 2. Implementacja środowiska eksperymentowania

    # Liczby neuronów w warstwie ukrytej
    test_layers = [100, 200, 300]

    # Typy funkcji aktywacji
    activation_value = 'relu' # or relu, logistic

    # Wartości momentum
    momentum_values = [0, 0.9] #

    results = pandas.DataFrame(columns=["fold","layer_size","momentum_value", "activation_value", "feature_number","score"])

    filename = "resultaty_relu.csv"

    # Przeprowadzenie badan przy uzyciu roznych konfiguracji zdefiniowanych parametrow
    for layer in test_layers:
            for momentum_value in momentum_values:
                for feature_number in range(1,np.shape(X)[1]+1):

                    print("---------------------------------")
                    print(
                        f"Ilość warstw {layer}, Funkcja aktywacji: {activation_value}, Momentum: {momentum_value}, Liczba cech: {feature_number}")

                    results = classification(X=get_k_best(X, Y, feature_number), Y=Y, activation_value=activation_value, momentum_value=momentum_value,
                                            top_rank=top_rank[:feature_number - 1], layer_size=layer, results=results)

    results.to_csv(filename)

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

with open('README.rst') as readme_file:
    readme = readme_file.read()

with open('HISTORY.rst') as history_file:
    history = history_file.read()

requirements = ['PyYAML>=5.1', 'colorama>=0.4.1']

setup_requirements = []

test_requirements = []

setup(
    author='luphord',
    author_email='luphord@protonmail.com',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
    ],
    description='A command line tool for my image processing needs.',
    entry_points={
        'console_scripts': [
            'hyperdiary=hyperdiary.__main__:main',
        ],
    },
    data_files=[('hyperdiary/assets/css',
                 ['hyperdiary/assets/css/picnic.min.css'])],
    install_requires=requirements,
    license='MIT license',
    long_description=readme + '\n\n' + history,
    include_package_data=True,
    keywords='hyperdiary',
    name='hyperdiary',
    packages=find_packages(),
    setup_requires=setup_requirements,
    test_suite='tests',
    tests_require=test_requirements,
    url='https://github.com/luphord/hyperdiary',
    version='0.6.1',
    zip_safe=False,
)


"""
Main Loop of simulation.
Here a problem and a solution are defined.
"""

import gym
from pyglet.window import key
import numpy as np

from tools import *
from base_solution import *

# Show preview
def key_press(k, mod):
    if k == key.SPACE:
        global preview
        preview = True

def key_release(k, mod):
    if k == key.SPACE:
        global preview
        preview = False

# Parameters
n_episodes = 50000
problem = 'CarRacing-v0'

gym.logger.set_level(40)
preview = False
best_result = 0
all_episode_reward = []

# Initialize simulation
env = gym.make(problem)
env.reset()
env.viewer.window.on_key_press = key_press
env.viewer.window.on_key_release = key_release

# Define custom standard deviation for noise
# We need lest noise for steering
noise_std = np.array([0.1, 4 * 0.2], dtype=np.float32)
solution = BaseSolution(env.action_space, model_outputs=2, noise_std=noise_std)

# Loop of episodes
for ie in range(n_episodes):
    state = env.reset()
    solution.reset()
    done = False
    episode_reward = 0
    no_reward_counter = 0

    # One-step-loop
    while not done:
        if preview:
            env.render()

        action, train_action = solution.get_action(state)

        # This will make steering much easier
        action /= 4
        new_state, reward, done, info = env.step(action)

        # Models action output has a different shape for this problem
        solution.learn(state, train_action, reward, new_state)
        state = new_state
        episode_reward += reward

        if reward < 0:
            no_reward_counter += 1
            if no_reward_counter > 200:
                break
        else:
            no_reward_counter = 0

    all_episode_reward.append(episode_reward)
    average_result = np.array(all_episode_reward[-10:]).mean()
    print('Last result:', episode_reward, 'Average results:', average_result)

    if episode_reward > best_result:
        print('Saving best solution')
        solution.save_solution()
        best_result = episode_reward


#yes its the price of gas currently
#IGNORE HTML PROCESS ATM, AND FOCUS ON BASE CODEWORK

import math
import untangle

response = untangle.parse("http://www.fueleconomy.gov/ws/rest/fuelprices")
x = float(response.fuelPrices.regular.cdata)

def cashback(x):
   z = x * .03
   x = x - z
   return(round(x,2))

#do i need to calculate the highest credit cost
#remember, i`m attempting to determine the best deals, so i need to assume the cheapest value
#which appears to be a .06 credit increase

cgas = input("Enter the price, in decimal form, of purchasing gas via credit card: ")
cgas = float(cgas)
pgas = input("Enter the percentage of gas you need to fill your tank: ")
pgas_final = float(pgas)/100

#gas2 = x + .06   #low point credit card
#print("Cost of gas using credit card, at its lowest price: $",gas2)
#gas3 = x + .10  #high point credit card/normal cost
#print("Cost of gas using credit card, at its highest price: $",gas3)

#16 is the combined mpg of the 2002 trailblazer 4WD
cash = x * 16 * pgas_final
cash = round(cash,2)
print("This is the amount you would pay for a full tank, in cash: $",cash)
credit_low = cgas * 16 * pgas_final
print("This is the amount you would pay for a full tank, in the lowest price in credit card:$",credit_low)
saving_low = cashback(credit_low)
print("This is the end result of the lowest credit spending, assuming cashback: $",saving_low)       #total i spend on a full tank of gas using credit minus the $3 cashback
if cash > saving_low:
   savings = cash - saving_low
   savings = round(savings,2)
   print("You save more with your credit card! How much more? Well you save: $",savings)
elif saving_low > cash:
   savings = saving_low - cash
   savings = round(savings,2)
   print("You save more by using your cash at this price. You save: $", savings)
else:
   print("DO WHATEVER, ITS THE SAME PRICE REALLY!")

#MAIN QUESTION: DOES MY CAULCATION CHANGE IF I DON`T SPEND A FULL TANK?
#the only thing that can change would be the credit value
#so if you do % change and credit increase change, then the values would matter a lot.
   #so: variable to initial credit value  and percentage change
   #so then you only need 2 values.

#-----------------------------------------------------------------------
#Right now, i need to find a better API for credit and cash gas price.
#Unless i assume the lowesy credit card is 5-6 cents lower than the cost of the gas

#so we still need to figure out an api for vehicles and combined mpg access as well
#otherwise, we create our own database, with mongodb usage
#also look up mobile usage


import time
import pytest

from selenium.webdriver.common.by import By

link = 'http://selenium1py.pythonanywhere.com/catalogue/hacking-exposed-wireless_208/'

def test_presence_cart_button(browser):
    browser.implicitly_wait(5)
    browser.get(link)
    browser.find_element(By.CSS_SELECTOR, '.btn-add-to-basket')
    time.sleep(10)

    button_name = browser.find_element(By.CSS_SELECTOR, '.btn-add-to-basket').text

    assert button_name == f'Añadir al carrito', f'Проверьте параметризацию языка или корректность вводимой команды'

if __name__ == "__main__":
    pytest.main()


import os

import numpy as np
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import (OrdinalEncoder,
                                   FunctionTransformer,
                                   StandardScaler)
from sklearn.cluster import KMeans
from yellowbrick.cluster import KElbowVisualizer
from sklearn.decomposition import PCA

plt.style.use('ggplot')

class ClusterAnalysis:

    def __init__(self) -> None:
        """Cluster-analysis on Customer Data"""
        self.output_path = 'outputs/visuals/'
        self.data_path = 'data/customers.csv'

    def run(self, *args, verbose=True) -> None:
        data_col = ['CustomerID', 'Gender', 'Age', 'Annual Income (k$)',
                    'Spending Score (1-100)']
        data = np.genfromtxt(
            self.data_path, delimiter=',', dtype=object, skip_header=1)

        data = data[:, 1:]  # remove CustomerID

        pipe = Pipeline(steps=[
            ('GenderBinarizer', ColumnTransformer(
                [('oe', OrdinalEncoder(), [0])], remainder='passthrough')),
            ('ChangeDtype', FunctionTransformer(lambda X: X.astype(np.float32))),
            ('StandardScaler', ColumnTransformer(
                [('ss', StandardScaler(), [1,2,3])], remainder='passthrough')),

        ], verbose=False)

        transformed_data = pipe.fit_transform(data)

        self._visualize_optimal_K(
            X=transformed_data, filename='optimal_value_of_k')

        # Add KMeans clustering algorithm in pipeline
        k = 4
        model = KMeans(
            n_clusters=k, init='k-means++', max_iter=20, n_init=10,
            random_state=0)
        pipe.steps.append(('kmeans', model))

        # Fit and predict
        pipe.verbose = True
        labels = pipe.fit_predict(data)
        cluster_centers = pipe['kmeans'].cluster_centers_

        self._plot_clusters(
            transformed_data, cluster_centers, labels,
            title='Clusters Visualization')

        # (unscaled) Features mean of each cluster
        cluster_means = []
        pipe.verbose = False
        for i in range(k):
            cluster_means.append(
                pipe[:2].transform(data)[labels==i, :].mean(axis=0))
        cluster_means = np.array(cluster_means)

        # Observation: (Based on `cluster_means`)
        # Cluster-0 (or Segment-0) comprises those customers who has high
        # income and spend alot.
        # Customers in Cluster-1 earn high but they spend a little and
        # there average age is higher than the Cluster-0.
        # Cluster-2 comprises customers who has medium income and spend
        # at the same level. They are old people.
        # Cluster-3 comprises customers who has little income but they
        # spend a lot. They are young people.

        if verbose:
            print('Visuals have been saved to {} directory.'.format(self.output_path))

    def _visualize_optimal_K(self, X, filename=None):
        fig = plt.figure()
        visualizer = KElbowVisualizer(
            KMeans(random_state=0), k=(1,13), timing=False)
        visualizer.fit(X) #Fit the data to the visualizer
        visualizer.show()
        if filename:
            os.makedirs(self.output_path, exist_ok=True)
            fig.savefig(self.output_path+filename, dpi=fig.dpi)

    def _plot_clusters(self, transformed_X, centroids, labels, title):
        # Apply PCA and fit the features
        pca_2d = PCA(n_components=2).fit_transform(transformed_X)

        fig, ax = plt.subplots(figsize=(10, 6))
        plt.scatter(
            pca_2d[:, 0],
            pca_2d[:, 1],
            c=labels,
            cmap=plt.cm.get_cmap("Spectral_r", 5),
            alpha=0.5)
        ax.set_xlabel('PCA Component-1', fontsize=8, labelpad=10)
        ax.set_ylabel('PCA Component-2', fontsize=8, labelpad=10)

        fig.suptitle(title, fontweight ="bold", fontsize=16)
        plt.colorbar(ticks=list(range(len(centroids))))
        plt.show()


"""
Supervised GRN Run (:mod:`SGRN`) module contains the following main class:
- :class:`SGRN.SGRN` and three additional classes used in the definition of SGRN class
- :class:`SGRN.ConfigParser`
- :class:`SGRN.InputSettings`
- :class:`SGRN.OutputSettings`
"""

import yaml
import argparse
import itertools
from collections import defaultdict
from pathlib import Path
import multiprocessing
from multiprocessing import Pool, cpu_count
import concurrent.futures
from typing import Dict, List
import yaml
import argparse
import itertools
from collections import defaultdict
from pathlib import Path
import multiprocessing
from multiprocessing import Pool, cpu_count
import concurrent.futures
from typing import Dict, List
from SGRN.runner import Runner
import os
import pandas as pd
import random
import logging
import shutil

class InputSettings(object):
    def __init__(self,
            datadir, datasets, algorithms, kTrain, kTest, randSeeds, kFold, CVType) -> None:

        self.datadir = datadir
        self.datasets = datasets
        self.algorithms = algorithms
        self.kTrain = kTrain
        self.kTest = kTest
        self.randSeeds = randSeeds
        self.kFold = kFold
        self.CVType = CVType

class OutputSettings(object):
    '''
    Structure for storing the names of directories that output should
    be written to
    '''

    def __init__(self, base_dir, output_prefix: Path) -> None:
        self.base_dir = base_dir
        self.output_prefix = output_prefix

class SGRN(object):
    '''
    The SGRN object is created by parsing a user-provided configuration
    file. Its methods provide for further processing its inputs into
    a series of jobs to be run, as well as running these jobs.
    '''

    def __init__(self,
            input_settings: InputSettings,
            output_settings: OutputSettings) -> None:

        self.input_settings = input_settings
        self.output_settings = output_settings
        self.runners: Dict[int, Runner] = self.__create_runners()

    def __create_runners(self) -> Dict[int, List[Runner]]:
        '''
        Instantiate the set of runners based on parameters provided via the
        configuration file.
        '''

        runners: Dict[int, Runner] = defaultdict(list)
        order = 0
        for dataset in self.input_settings.datasets:
            for runner in self.input_settings.algorithms:
                for randSeed in self.input_settings.randSeeds:
                    data = {}
                    data['name'] = runner[0]
                    data['params'] = runner[1]
                    data['inputDir'] = Path.cwd().joinpath(self.input_settings.datadir.joinpath(dataset['name']))
                    data['exprData'] = dataset['exprData']
                    data['trueEdges'] = dataset['trueEdges']
                    data['kTrain'] = self.input_settings.kTrain
                    data['kTest'] = self.input_settings.kTest
                    data['kFold'] = self.input_settings.kFold
                    data['randSeed'] = randSeed
                    data['outPrefix'] = self.output_settings.base_dir / self.output_settings.output_prefix
                    data['CVType'] = self.input_settings.CVType

                    if 'should_run' in data['params'] and \
                            data['params']['should_run'] is False:
                        print("Skipping %s" % (data['name']))
                        continue

                    runners[order] = Runner(data)
                    order += 1
        return runners

    def execute_runners(self, parallel=False, num_threads=1):
        '''
        Run each of the algorithms.
        '''

        base_output_dir = self.output_settings.base_dir

        batches =  self.runners.keys()

        for batch in batches:
            if parallel==True:
                executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
                futures = [executor.submit(runner.run, base_output_dir)
                    for runner in self.runners[batch]]

                # https://stackoverflow.com/questions/35711160/detect-failed-tasks-in-concurrent-futures
                # Re-raise exception if produced
                for future in concurrent.futures.as_completed(futures):
                    future.result()
                executor.shutdown(wait=True)
            else:
                for runner in self.runners[batch]:
                    runner.run(output_dir=base_output_dir)

class ConfigParser(object):
    '''
    Define static methods for parsing a config file that sets a large number
    of parameters for the pipeline
    '''
    @staticmethod
    def parse(config_file_handle) -> SGRN:
        config_map = yaml.load(config_file_handle)
        return SGRN(
            ConfigParser.__parse_input_settings(
                config_map['input_settings']),
            ConfigParser.__parse_output_settings(
                config_map['output_settings']))

    @staticmethod
    def __parse_input_settings(input_settings_map) -> InputSettings:
        input_dir = input_settings_map['input_dir']
        dataset_dir = input_settings_map['dataset_dir']
        datasets = input_settings_map['datasets']
        kTrain = input_settings_map['kTrain']
        kTest = input_settings_map['kTest']
        kFold = input_settings_map['kFold']
        CVType = input_settings_map['CVType']
        if 'randSeed' in input_settings_map:
            randSeeds = input_settings_map['randSeed']
        elif 'nTimes' in input_settings_map:
            randSeeds = []
            for x in range(input_settings_map['nTimes']):
                random.seed()
                randSeeds.append(random.randint(0, 1000))
            print("Using ",randSeeds, "as random seeds")
        else:
            print("Either randSeed or nTimes must be specified in the config file.")
            sys.exit()

        return InputSettings(
                Path(input_dir, dataset_dir),
                datasets,
                ConfigParser.__parse_algorithms(
                input_settings_map['algorithms']),
                kTrain, kTest, randSeeds, kFold, CVType)

    @staticmethod
    def __parse_algorithms(algorithms_list):
        algorithms = []
        for algorithm in algorithms_list:
                combos = [dict(zip(algorithm['params'], val))
                    for val in itertools.product(
                        *(algorithm['params'][param]
                            for param in algorithm['params']))]
                for combo in combos:
                    algorithms.append([algorithm['name'],combo])

        return algorithms

    @staticmethod
    def __parse_output_settings(output_settings_map) -> OutputSettings:
        output_dir = Path(output_settings_map['output_dir'])
        output_prefix = Path(output_settings_map['output_prefix'])

        ConfigParser.__create_logger(output_dir, output_prefix)

        return OutputSettings(output_dir,
                             output_prefix)

    @staticmethod
    def __create_logger(output_dir, output_prefix):
        outputPath = Path(str(output_dir) + '/' + str(output_prefix))
        if os.path.exists(outputPath):
            print("Cleaning up old files...")
            shutil.rmtree(outputPath)

        os.makedirs(outputPath)
        print("Created output folder...")

        logFile = os.path.join(outputPath, 'log.txt')
        if not os.path.exists(logFile):

            logging.basicConfig(handlers=[logging.FileHandler(filename=Path(str(outputPath) + '/' + 'log.txt'),
                                                         encoding='utf-8', mode='a+')],
                            format="%(asctime)s %(levelname)s:%(message)s",
                            datefmt='%m/%d/%Y %I:%M:%S %p %Z',
                            level=logging.INFO)

        return


import gensim
from gensim import corpora
from gensim.corpora import Dictionary
import pickle
import os
from spacy.lang.en import English
import nltk

from nltk.corpus import wordnet as wn
def get_lemma(word):
    lemma = wn.morphy(word)
    if lemma is None:
        return word
    else:
        return lemma

from nltk.stem.wordnet import WordNetLemmatizer
def get_lemma2(word):
    return WordNetLemmatizer().lemmatize(word)

en_stop = set(nltk.corpus.stopwords.words('english'))
parser = English()

eng_only_files = pickle.load(open('pickles/english_only_files.pkl','rb'))
eng_only_files=[i.strip('.doc.sdlxliff').replace('raw_data/','data_products/extracted/')+".txt" for i in eng_only_files]

NUM_TOPICS = 20

def tokenize(text):
    lda_tokens = []
    tokens = parser(text)
    for token in tokens:
        if token.orth_.isspace():
            continue
        elif token.like_url:
            lda_tokens.append('URL')
        elif token.orth_.startswith('@'):
            lda_tokens.append('SCREEN_NAME')
        else:
            lda_tokens.append(token.lower_)
    return lda_tokens

def load_data(files_list):
    text_data=[]
    for filename in files_list:
        text_data.append(prepare_text_for_lda(open(filename,'r').read()))
    return text_data

def get_dict_and_corpus(text_data):
    dictionary = corpora.Dictionary(text_data)
    corpus = [dictionary.doc2bow(text) for text in text_data]
    return dictionary,corpus

def prepare_text_for_lda(text):
    tokens = tokenize(text)
    tokens = [token for token in tokens if len(token) > 4]
    tokens = [token for token in tokens if token not in en_stop]
    tokens = [get_lemma(token) for token in tokens]
    return tokens

def predict_topics_from_file(filename, dictionary_file='dictionary.gensim',
        model='models/model30.gensim'):
    new_doc = open(filename,'r').read()
    predict_topic_from_text(new_doc, dictionary_file=dictionary_file,
            model=model)

def predict_topic_from_text(new_doc,  dictionary_file='dictionary.gensim',
        model='models/model30.gensim', printout=False):
    ldamodel = gensim.models.ldamodel.LdaModel.load(model)
    new_doc = prepare_text_for_lda(new_doc)
    dictionary=Dictionary.load( dictionary_file)
    new_doc_bow = dictionary.doc2bow(new_doc)
    doc_topics = sorted(ldamodel.get_document_topics(new_doc_bow),key=lambda x:x[1], reverse=True)
    all_topics = ldamodel.show_topics(30)
    thresh_topics=[]
    for topic in doc_topics:
        if topic[1]>0.1:
            thresh_topics.append({'topic #':all_topics[topic[0]][0],"topic probability":
                topic[1],'topic hot words': all_topics[topic[0]][1]})
    if printout:
        print(thresh_topics)
    return thresh_topics

#Loading corpus:
# corpus=pickle.load(open('pickles/corpus.pkl','rb'))
# Loading dictionary:
# dictionary = Dictionary.load('dictionary.gensim')
# downloading text data:
def visualize_model():
    import pyLDAvis.gensim
    import pyLDAvis
    pyLDAvis.enable_notebook()
    vis = pickle.load(open('pickles/firs_finished_vis.pkl','rb'))
    pyLDAvis.show(vis)


from pyspark.sql import SparkSession
import uuid
from com.db.fw.etl.core.Exception.EtlExceptions import InvalidParamsException
from com.db.fw.etl.core.common.Commons import Commons

class IOService:

    def __init__(self):
        self.spark = SparkSession.builder.getOrCreate()

    IO_SERVICE_STATUS= True

    def cleanup_msg(self,message):
        if message is not None:
            message = message.replace("'","")
        return message

    def store_pipeline_status(self, pipeline_name, pipeline_instance_id, status, time, error):
        if IOService.IO_SERVICE_STATUS == False :
            return None;

        tmp_uuid = str(uuid.uuid1())
        sql_stmt = "INSERT INTO  tata_poc.pipeline_status VALUES('{}', '{}', '{}','{}','{}','{}')".format(tmp_uuid,
                                                                                pipeline_name,
                                                                                pipeline_instance_id,
                                                                                status, time,
                                                                                self.cleanup_msg(str(error)))
        self.spark.sql(sql_stmt)

    def store_task_status(self,  pipeline_name, pipeline_instance_id, name, status, time, error):
        if IOService.IO_SERVICE_STATUS == False :
            return None;
        tmp_uuid =  str(uuid.uuid1())
        sql_stmt = "INSERT INTO  tata_poc.task_status VALUES('{}', '{}', '{}','{}','{}','{}','{}')".format(tmp_uuid,
                                                                                pipeline_name,
                                                                                pipeline_instance_id,
                                                                                name,
                                                                                status,
                                                                                time,
                                                                                self.cleanup_msg(str(error)))
        self.spark.sql(sql_stmt)

    def store_operational_stats(self, pipeline_instance_id, pipeline_name, task_name, stats ,time,batch_id):
        # if IOService.IO_SERVICE_STATUS == False :
        #     return None;
        print("store_operation_stats I am here inside")
        INSERT_STMT = "INSERT INTO  tata_poc.operational_stats VALUES "
        comma_sept_for_values =""
        for stat_name , state_value in stats.items():

            values_stmt  = "{} ( '{}', '{}','{}','{}','{}','{}',{})  ".format(comma_sept_for_values,
                pipeline_instance_id, pipeline_name, task_name, stat_name, state_value, time,batch_id)
            comma_sept_for_values = ","

            INSERT_STMT = INSERT_STMT + values_stmt

        Commons.printInfoMessage(INSERT_STMT)
        self.spark.sql(INSERT_STMT)

    def store_pipeline_metadata(self, out_pip_id, out_name, out_jsons):
        if IOService.IO_SERVICE_STATUS == False :
            return None;
        sql_stmt = "INSERT INTO  tata_poc.pipeline_metadata  VALUES('{}', '{}', '{}')".format(out_pip_id, out_name,
                                                                                out_jsons)
        self.spark.sql(sql_stmt)

    def get_pipeline_metadata(self, pipeline_name, task_name):

        sql_stmt = "select * from tata_poc.pipeline_metadata  where pipeline_name == '{}'".format(pipeline_name)
        meta_values = self.spark.sql(sql_stmt).collect()
        if len(meta_values) <= 0:
            raise InvalidParamsException(task_name, pipeline_name, {})

        pipeline_id = meta_values[0].pipeline_id
        pipeline_name = meta_values[0].pipeline_name
        node_defination = meta_values[0].node_defination
        return pipeline_id, pipeline_name, node_defination


import requests
import pandas as pd
import re
import json

pd.set_option('display.max_columns', None)

url = "https://ws.cso.ie/public/api.jsonrpc"

# Define a function to clean characters
def clean_string(s):
	return str(s).replace('\r', '').replace('"', '')

# Define the JSON-RPC request payload

# Define the JSON-RPC request payload
payload = {
	"jsonrpc": "2.0",
	"method": "PxStat.Data.Cube_API.ReadDataset",
	"params": {
		"class": "query",
		"id": [],
		"dimension": {},
		"extension": {
			"language": {
				"code": "en"
			},
			"format": {
				"type": "CSV",
				"version": "1.0"
			},
			"matrix": "RIA02"
		},
		"version": "2.0"
	}
}

# Make the POST request
response = requests.post(url, json=payload)

# Check if the request was successful (status code 200)
if response.status_code == 200:
	data = response.json()
	data_result = data['result']
	lines = data_result.split('\r')
	#print(lines)
	data_result = [re.findall(r'"([^"]*)"', line) for line in lines[1:]]
	#df = pd.DataFrame(data_result, columns=['Year Label', 'Year', 'Bedrooms Label', 'Property Type Label', 'Value'])
	print(data_result)
else:
	print(f"Error connecting to API. Status code: {response.status_code}")


import numpy as np
import matplotlib.pyplot as plt

num_of_lines = int(input("How many lines do you want?"))
t = np.arange(0.0, 3, 0.01)
y = [None] * num_of_lines
for i in range(num_of_lines):
    y[i] = 2 * i * np.sin(i*np.pi*t)

fig, ax = plt.subplots()
ax.set_title('Interactive spaghetti demo, \n Click on legend to hide a line')
lines = [None] * num_of_lines

for i in range(num_of_lines):
    lines[i] = ax.plot(t, y[i], lw=1, label=(str(i) + ' S'))
leg = ax.legend(loc="upper right", fancybox=True)
leg.get_frame().set_alpha(0.4)

lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(3)  # 3 pts tolerance
    lined[legline] = origline

def onpick(event):
    legline = event.artist
    origline, = lined[legline]
    vis = not origline.get_visible()
    origline.set_visible(vis)
    if vis:
        legline.set_alpha(1.0)
    else:
        legline.set_alpha(0.1)
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()


# Rejurhf
# 2.12.2018

import numpy
from matplotlib import pyplot as plt

# du/dt = v(d^2u)/(dx^2) 1d diffusion equation
# After discretizing
# (d^2u)/(dx^2) = (u[i+1]-2u[i]+u[i-1])/delx^2 + O(delx^2)

# Initial Conditions
nx = 41
dx = 2 / (nx - 1)
nt = 20    # the number of timesteps we want to calculate
nu = 0.3   # the value of viscosity
sigma = .2 # sigma is a parameter
dt = sigma * dx**2 /nu # dt is defined using sigm
print(dt)

u = numpy.ones(nx)      # a numpy array with nx elements all equal to 1.
#setting u = 2 between 0.5 and 1 as per our I.C.s
u[int(.5 / dx):int(1 / dx + 1)] = 2

# Calculation
un = numpy.ones(nx) # our placeholder array, un, to advance the solution in time
for n in range(nt):  # iterate through time
    un = u.copy() # copy the existing values of u into un
    for i in range(1, nx - 1):
        u[i] = un[i] + nu * dt / dx**2 * (un[i+1] - 2 * un[i] + un[i-1])

    plt.plot(numpy.linspace(0, 2, nx), u);
plt.show();


from django.db import migrations
from django.db.models import CharField, F, Value
from django.db.models.functions import Concat

def populate_new_reporter_fields(apps, schema_editor):
    Reporter = apps.get_model("core", "Reporter")
    Reporter.objects.exclude(airtable_name__isnull=True).update(
        external_id=Concat(
            Value("airtable:"), F("airtable_name"), output_field=CharField()
        )
    )
    Reporter.objects.exclude(auth0_name__isnull=True).update(
        external_id=Concat(Value("auth0:"), F("auth0_name"), output_field=CharField())
    )

class Migration(migrations.Migration):

    dependencies = [
        ("core", "0019_redesign_reporter"),
    ]

    operations = [
        migrations.RunPython(populate_new_reporter_fields),
    ]


# -*- coding: utf-8 -*-
from collections import deque

n, m = map(int, input().split())
ans=[]
if(m==0):
  for i in range(n):
    ans.append(i+1)
  print(*ans)
  quit()
a=list(map(int, input().split()))

que=deque()
for i in range(n):
  if len(a)!=0 and i+1 == a[0]:
    que.append(a.pop(0))
  else:
    ans.append(i+1)
    while que:
      ans.append(que.pop())

print(*ans)


from tkinter import *
import tkinter as tk
from urllib.request import urlopen as req
from bs4 import BeautifulSoup as soup
import time
import requests #pip install requests

gui = tk.Tk()
gui.geometry("500x500")
gui.title("รายงาน COVID-19")

p1 = PhotoImage(file = 'covid48x48.png')
gui.iconphoto(False, p1)
# Setting icon of master window
# gui.iconphoto(False, 'covid19.png')
################################################
#function API
r = requests.get("https://covid19.th-stat.com/api/open/today")
data =r.json()

v_result_Con = StringVar()
result_Con = data['Confirmed']#ยอดผู้ป่วย
print(f'ผู้ป่วยยืนยันสะสม: {result_Con}')

v_result_Re = StringVar()
result_Re = data['Recovered']#รักษาหาย
print(f'หายป่วยแล้ว: {result_Re}')

v_result_Hos = StringVar()
result_Hos  = data['Hospitalized']#รับการรักษา
print(f'ผู้ป่วยรักษาอยู่: {result_Hos}')

v_result_De = StringVar()
result_De = data['Deaths'] #ตาย
print(f'เสียชีวิต: {result_De}')

v_result_NewCon = StringVar()
result_NewCon = data['NewConfirmed'] #เพิ่มใหม่
print(f'ผู้ป่วยรายใหม่ในวันนี้: {result_NewCon}')

v_result_ReCon = StringVar()
result_ReCon = data["NewRecovered"]
print(f'เพิ่มขึ้น: {result_ReCon}')

v_result_NewHos = StringVar()
result_NewHos = data['NewHospitalized']
print(f'เข้ารักษาใหม่ : {result_NewHos}')

v_result_NewDe = StringVar()
result_NewDe = data['NewDeaths']
print(f'เสียชีวิตเพิ่ม : {result_NewDe}')

v_Date = StringVar()
Date = data['UpdateDate']
print(f'อัพเดทข้อมูลล่าสุด : {Date}')

################################################
#เพิ่มภาพใน label
background_image = tk.PhotoImage(file='background.png')
background_label = tk.Label(gui, image=background_image)
background_label.place(width=1, height=1)

#-----------------FRAME1 ------------------------------------

##FE5000 สีส้ม
frame1 = tk.Frame(gui, bg='#454545' )#bd=5 กำหนดขอบ
frame1.place(x=2.5, #กำหนดขอบเริ่มต้นแนวนอน
			y=2.5, #กำหนดขอบเริ่มต้นแนวตั้ง
			width=246,  #กำหนดขนาดด้านกว้าง
			height=246) #กำหนดขนาดด้านสูง
#anchor='n' N, E, S, W, NE, NW, SE, or SW,
tk.Label(frame1, text='FRAME1').pack()
#ne-ชิดขวา /nw-ชิดซ้าย /n-กลาง /e-ชิดขวา /s-กลาง /w-ชิดซ้าย /sw-ชิดซ้าย /se=ชิดขวา

#----------------FRAME2-----------------------------------------#

frame2 = tk.Frame(gui, bg='#454545' )#bd=5 กำหนดขอบ
frame2.place(x=246, #กำหนดขอบเริ่มต้นแนวนอน
			y=2.5, #กำหนดขอบเริ่มต้นแนวตั้ง
			width=250,  #กำหนดขนาดด้านกว้าง
			height=250) #กำหนดขนาดด้านสูง
			#anchor='n' N, E, S, W, NE, NW, SE, or SW,
tk.Label(frame2, text='FRAME2').pack()

#----------------FRAME3-----------------------------------------#
frame3 = tk.Frame(gui, bg='#454545' )#bd=5 กำหนดขอบ
frame3.place(x=2.5, #กำหนดขอบเริ่มต้นแนวนอน
			y=246, #กำหนดขอบเริ่มต้นแนวตั้ง
			width=250,  #กำหนดขนาดด้านกว้าง
			height=250) #กำหนดขนาดด้านสูง
			#anchor='n' N, E, S, W, NE, NW, SE, or SW,
tk.Label(frame3, text='FRAME3').pack()

#----------------FRAME4-----------------------------------------#
frame4 = tk.Frame(gui, bg='#454545')#bd=5 กำหนดขอบ
frame4.place(x=246, #กำหนดขอบเริ่มต้นแนวนอน
			y=246, #กำหนดขอบเริ่มต้นแนวตั้ง
			width=250,  #กำหนดขนาดด้านกว้าง
			height=250) #กำหนดขนาดด้านสูง
			#anchor='n' N, E, S, W, NE, NW, SE, or SW,
#tk.Label(frame4, text='FRAME4').pack()

#--------------------LABEL1---------------------------------#
#bg='MediumSlateBlue'สีม่วงอ่อน
AL1 = tk.Label(frame1, text="ผู้ป่วยรายใหม่ในวันนี้",
				bg='#454545', font=('Tahoma', 12), fg='white')
AL1.place(width=250, height=25, x=0, y=0)

v_result_NewCon.set(f' {result_NewCon}')#ส่งออกแสดวผล frame1
BL1 = tk.Label(frame1, textvar=v_result_NewCon,  font=('Tahoma', 40, 'bold'),
					 bg='red', fg='white')##FE5000 สีส้ม
BL1.place(width=246, height=96, x=2, y=27)#663399 สีม่วง
##FE5000 สีส้ม

CL1 = tk.Label(frame1, text="จำนวน", font=('Tahoma', 12),
					 bg='#5DADE2', fg='black')
CL1.place(width=170, height=25, y=125, x=0)
CL11 = tk.Label(frame1, text="ราย", font=('Tahoma', 12),
					 bg='#5DADE2', fg='black')
CL11.place(width=80, height=25, y=125, x=170)

DL1 = tk.Label(frame1, text="ติดเชื้อภายในประเทศ", font=('Tahoma', 10),
					 bg='MediumSlateBlue', fg='white')
DL1.place(width=170, height=25, y=150)

EL1 = tk.Label(frame1, text="172", font=('Tahoma', 12),
					 bg='#663399', fg='white')
EL1.place(width=80, height=25, x=170, y=150)

FL1 = tk.Label(frame1, text="ผู้เดินทางไม่เข้า\nสถานที่กักกัน", font=('Tahoma', 8),
					 bg='#5DADE2', fg='black')
FL1.place(width=125, height=25, x=0, y=175)
FL11 = tk.Label(frame1, text="สถานที่กักกัน\nที่รัฐจัดให้", font=('Tahoma', 8),
					 bg='#5DADE2', fg='black')
FL11.place(width=125, height=25, x=125, y=175)

GL1 = tk.Label(frame1, text="-", font=('Tahoma', 25,'bold'),
					 bg='#FE5000', fg='white')
GL1.place(width=120, height=45, x=3, y=204)
GL11 = tk.Label(frame1, text="13", font=('Tahoma', 25,'bold'),
					 bg='#FE5000', fg='white',bd=10)
GL11.place(width=120, height=45, x=128, y=204)
#--------------------LABEL2---------------------------------#

AL2 = tk.Label(frame2, text="ผู้ป่วยยืนยันสะสม",
				bg='#454545', font=('Tahoma', 12), fg='white')
AL2.place(width=250, height=25, x=0, y=0)

#v_result_Con รับข้อมูลจาก data
v_result_Con.set(f' {result_Con}')#ส่งออกแสดงผล frame2
BL2 = tk.Label(frame2, textvar=v_result_Con,  font=('Tahoma', 40, 'bold'),
					 bg='#FE5000', fg='white')
BL2.place(width=246, height=96, x=2, y=27)

CL2 = tk.Label(frame2, text="ติดเชื้อภายในประเทศ", font=('Tahoma', 10),
					 bg='#5DADE2', fg='black')
CL2.place(width=170, height=25, y=125, x=0)
CL22 = tk.Label(frame2, text="4,869", font=('Tahoma', 12),
					 bg='#5DADE2', fg='black')
CL22.place(width=80, height=25, y=125, x=170)

DL22 = tk.Label(frame2, text="ติดเชื้อในแรงงานต่างด้าว\n(คัดกรองเชิงรุก)", font=('Tahoma', 8),
					 bg='MediumSlateBlue', fg='white')
DL22.place(width=170, height=25, y=150)

EL2 = tk.Label(frame2, text="1,392", font=('Tahoma', 12),
					 bg='#663399', fg='white')
EL2.place(width=80, height=25, x=170, y=150)

FL2 = tk.Label(frame2, text="ผู้เดินทางมาจากต่างประเทศ", font=('Tahoma', 8),
					 bg='#5DADE2', fg='black')
FL2.place(width=125, height=25, x=0, y=175)
FL22 = tk.Label(frame2, text="สถานที่กักกันที่รัฐจัดให้", font=('Tahoma', 8),
					 bg='#5DADE2', fg='black')
FL22.place(width=125, height=25, x=125, y=175)

GL2 = tk.Label(frame2, text="2,015", font=('Tahoma', 25,'bold'),
					 bg='#FE5000', fg='white')
GL2.place(width=120, height=45, x=2, y=204)
GL22 = tk.Label(frame2, text="1,481", font=('Tahoma', 25,'bold'),
					 bg='#FE5000', fg='white',bd=10)
GL22.place(width=120, height=45, x=127, y=204)

#--------------------LABEL3---------------------------------#

AL3 = tk.Label(frame3, text="หายป่วยแล้ว",
				bg='#3CB371', font=('Tahoma', 15), fg='black')
AL3.place(width=250, height=25, x=0, y=0)

v_result_Re.set(f' {result_Re}')
BL3 = tk.Label(frame3, textvar=v_result_Re,  font=('Tahoma', 40, 'bold'),
					 bg='#006400', fg='white')
BL3.place(width=250, height=100, x=0, y=25)
#-----------------------
CL3 = tk.Label(frame3, text="จำนวน", font=('Tahoma', 12),
					 bg='#3CB371', fg='black')
CL3.place(width=170, height=25, y=125, x=0)
CL33 = tk.Label(frame3, text="ราย", font=('Tahoma', 12),
					 bg='#3CB371', fg='black')
CL33.place(width=80, height=25, y=125, x=170)

DL3 = tk.Label(frame3, text="ผู้ป่วยรักษาอยู่", font=('Tahoma', 10),
					 bg='green', fg='white')
DL3.place(width=170, height=25, y=150)

#v_result_Hos ผู้ป่วยรักษาอยู่
v_result_Hos.set(f'  {result_Hos}')
EL3 = tk.Label(frame3, textvar=v_result_Hos, font=('Tahoma', 15, 'bold'),
					 bg='green', fg='white')
EL3.place(width=80, height=25, x=170, y=150)

#-----------------------
FL3 = tk.Label(frame3, text="เพิ่มขึ้น", font=('Tahoma', 10),
					 bg='#3CB371', fg='black')
FL3.place(width=125, height=25, x=0, y=175)
FL33 = tk.Label(frame3, text="เข้ารักษาใหม่", font=('Tahoma', 8),
					 bg='#3CB371', fg='black')
FL33.place(width=125, height=25, x=125, y=175)

#v_result_ReCon เพิ่มขึ้น
v_result_ReCon.set(f'  {result_ReCon}')
GL3 = tk.Label(frame3, textvar=v_result_ReCon, font=('Tahoma', 15,'bold'),
					 bg='green', fg='white')
GL3.place(width=120, height=45, x=2, y=204)

#v_result_NewHos เข้ารักษาใหม่
v_result_NewHos.set(f' {result_NewHos}')
GL33 = tk.Label(frame3, textvar=v_result_NewHos, font=('Tahoma', 15,'bold'),
					 bg='green', fg='white',bd=10)
GL33.place(width=120, height=45, x=127, y=204)

#--------------------LABEL4---------------------------------#

AL3 = tk.Label(frame4, text="เสียชีวิต",
				bg='black', font=('Tahoma', 15), fg='white')
AL3.place(width=125, height=30, x=0, y=0)
AL33 = tk.Label(frame4, text="เสียชีวิตเพิ่ม",
				bg='black', font=('Tahoma', 15), fg='white')
AL33.place(width=125, height=30, x=125, y=0)

#v_result_De เสียชีวิต
v_result_De.set(f'  {result_De}')
BL3 = tk.Label(frame4, textvar=v_result_De,  font=('Tahoma', 40, 'bold'),
					 bg='black', fg='white')
BL3.place(width=125, height=100, x=0, y=25)

#v_result_NewDe เสียชีวิตเพิ่ม
v_result_NewDe.set(f'  {result_NewDe}')
BL33 = tk.Label(frame4, textvar=v_result_NewDe,  font=('Tahoma', 40, 'bold'),
					 bg='black', fg='white')
BL33.place(width=125, height=100, x=125, y=25)

CL3 = tk.Label(frame4, text="อัพเดทข้อมูลล่าสุด",
				bg='#454545', font=('Tahoma', 12), fg='white')
CL3.place(width=250, height=30, x=0, y=150)

#v_Date อัพเดทข้อมูลล่าสุด
v_Date.set(f'  {Date}')
DL3 = tk.Label(frame4, textvar=v_Date,
				bg='#454545', font=('Tahoma', 12), fg='white')
DL3.place(width=250, height=30, x=0, y=180)
#-----------------------

gui.mainloop()


from types import SimpleNamespace

import pytest
from acme.specs import DiscreteArray

from mava.core_jax import SystemBuilder
from mava.systems.builder import Builder
from mava.systems.idqn.components.building.extras_spec import DQNExtrasSpec
from tests.mocks import make_fake_env_specs

class MockBuilder(Builder):
    def __init__(self, store: SimpleNamespace) -> None:
        """Initialises a mock builder"""
        self.store = store

@pytest.fixture
def builder() -> Builder:
    """Creates a mock builder"""
    return MockBuilder(
        SimpleNamespace(
            unique_net_keys=[1, 2],
            ma_environment_spec=make_fake_env_specs(),
        )
    )

def test_on_building_init_end(builder: SystemBuilder) -> None:
    """Tests that extras spec is created in the store and only network keys are added"""
    extras_spec_component = DQNExtrasSpec()
    extras_spec_component.on_building_init_end(builder)
    assert builder.store.extras_spec == {
        "network_keys": {"agent_0": DiscreteArray(2), "agent_1": DiscreteArray(2)}
    }


# -*- coding: utf-8 -*-
'''
@Time          : 20/04/25 15:49
@Author        : huguanghao
@File          : demo.py
@Noice         :
@Modificattion :
    @Author    :
    @Time      :
    @Detail    :
'''

# import sys
# import time
# from PIL import Image, ImageDraw
# from models.tiny_yolo import TinyYoloNet
from tool.utils import *
from tool.torch_utils import *
from tool.darknet2pytorch import Darknet
import argparse
import requests
from io import BytesIO
from base64 import encodebytes
from PIL import Image
import flask

"""hyper parameters"""
use_cuda = True
app = flask.Flask(__name__)

def detect_cv2(cfgfile, weightfile, imgfile):
    import cv2
    m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    if use_cuda:
        m.cuda()

    num_classes = m.num_classes
    if num_classes == 20:
        namesfile = 'data/voc.names'
    elif num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/x.names'
    class_names = load_class_names(namesfile)

    img = cv2.imread(imgfile)
    sized = cv2.resize(img, (m.width, m.height))
    sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)

    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.4, 0.6, use_cuda)
        finish = time.time()
        if i == 1:
            print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))

    plot_boxes_cv2(img, boxes[0], savename='predictions.jpg', class_names=class_names)

def detect_cv2_camera(cfgfile, weightfile):
    import cv2
    m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    if use_cuda:
        m.cuda()

    cap = cv2.VideoCapture(0)
    # cap = cv2.VideoCapture("./test.mp4")
    cap.set(3, 1280)
    cap.set(4, 720)
    print("Starting the YOLO loop...")

    num_classes = m.num_classes
    if num_classes == 20:
        namesfile = 'data/voc.names'
    elif num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/x.names'
    class_names = load_class_names(namesfile)

    while True:
        ret, img = cap.read()
        sized = cv2.resize(img, (m.width, m.height))
        sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)

        start = time.time()
        boxes = do_detect(m, sized, 0.4, 0.6, use_cuda)
        finish = time.time()
        print('Predicted in %f seconds.' % (finish - start))

        result_img = plot_boxes_cv2(img, boxes[0], savename=None, class_names=class_names)

        cv2.imshow('Yolo demo', result_img)
        cv2.waitKey(1)

    cap.release()

def detect_skimage(cfgfile, weightfile, imgfile):
    from skimage import io
    from skimage.transform import resize
    m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    if use_cuda:
        m.cuda()

    num_classes = m.num_classes
    if num_classes == 20:
        namesfile = 'data/voc.names'
    elif num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/x.names'
    class_names = load_class_names(namesfile)

    img = io.imread(imgfile)
    sized = resize(img, (m.width, m.height)) * 255

    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.4, 0.4, use_cuda)
        finish = time.time()
        if i == 1:
            print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))

    plot_boxes_cv2(img, boxes, savename='predictions.jpg', class_names=class_names)

def get_response_image(image_path):
    pil_img = Image.open(image_path, mode='r') # reads the PIL image
    byte_arr = BytesIO()
    pil_img.save(byte_arr, format='PNG') # convert the PIL image to byte array
    encoded_img = encodebytes(byte_arr.getvalue()).decode('ascii') # encode as base64
    return encoded_img

@app.route("/detect_image", methods=["POST"])
def detect_image():
    output_dict = {"success": False}
    if flask.request.method == "POST":
        data = flask.request.json

        # read the image in PIL format
        response = requests.get(data["image"])
        image = Image.open(BytesIO(response.content))
        image.save("temp.jpg")
        # transform image
        #image_tensor = data_transforms(image).float()
        #image_tensor = image_tensor.unsqueeze_(0).to(device)

        # predict and max
        #output = model(image_tensor)
        #_, predicted = torch.max(output.data, 1)
        detect_cv2(args.cfgfile, args.weightfile, "temp.jpg")
        output_dict["predictions"] = get_response_image("predictions.jpg")
        output_dict["success"] = True
    return flask.jsonify(output_dict), 200

def get_args():
    parser = argparse.ArgumentParser('Test your image or video by trained model.')
    parser.add_argument('-cfgfile', type=str, default='./cfg/yolov4.cfg',
                        help='path of cfg file', dest='cfgfile')
    parser.add_argument('-weightfile', type=str,
                        default='./checkpoints/Yolov4_epoch1.pth',
                        help='path of trained model.', dest='weightfile')
    #parser.add_argument('-imgfile', type=str,
    #                    default='./data/mscoco2017/train2017/190109_180343_00154162.jpg',
    #                    help='path of your image file.', dest='imgfile')
    parser.add_argument('-port', help='port', type=int, default=5000)
    args = parser.parse_args()

    return args

if __name__ == '__main__':
    args = get_args()
    '''
    if args.imgfile:
        detect_cv2(args.cfgfile, args.weightfile, args.imgfile)
        # detect_imges(args.cfgfile, args.weightfile)
        # detect_cv2(args.cfgfile, args.weightfile, args.imgfile)
        # detect_skimage(args.cfgfile, args.weightfile, args.imgfile)
    else:
        detect_cv2_camera(args.cfgfile, args.weightfile)
    '''
    app.run(host="0.0.0.0", debug=True, port=args.port)