#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 24 15:34:27 2017

@author: serafina
"""

import numpy as np
import scipy as sp
from skimage import color,io,img_as_float,transform
#from collect import collect
from ksvd import ksvd

def convert_ycbcr_y(f):
    rgb = io.imread(f)
    ycbcr = color.rgb2ycbcr(rgb)
    ycbcr = img_as_float(ycbcr)/255
    return ycbcr[:,:,0]

str = 'CVPR08-SR/Data/Training' + '/*.bmp'

load_imgs = io.ImageCollection(str,load_func = convert_ycbcr_y)

io.imshow(load_imgs[40])

# Configuration
conf = {'scale': 3, 'level': 1, 'window': [3,3],
        'border': [1,1], 'upsample_factor': 3, 'filters': {},
        'interpolate_kernel': 'bicubic', 'overlap': [2,2]}

O = np.zeros((conf['upsample_factor']-1))
#G = np.array([1,O,-1]); # Gradient
G = np.array([1])
G = np.concatenate((G,O))
G = np.append(G,-1)
G.shape = (1,2+O.size)
#L = np.array([1,O,-2,O,1])/2; # Laplacian
L = np.array([1])
L = np.concatenate((L,O))
L = np.append(L,-2)
L = np.concatenate((L,O))
L = np.append(L,1)
L = L/2.0;
L.shape = (1,3+2*O.size)

filters = {'filter1':G,'filter2':G.T,'filter3':L,'filter4':L.T}
conf['filters'] = filters

def modcrop(imgs,scale):
    imgs_c = []
    for i in range(len(imgs)):
        img = imgs[i]
        sz = img.shape
        sz = sz - np.mod(sz,scale)
        img_c = img[0:sz[0], 0:sz[1]]
        imgs_c.append(img_c)
    return imgs_c

hires = modcrop(load_imgs, conf['scale'])

lores = []
for i in range(len(hires)):
    img = hires[i]
    sz = img.shape
    scale = conf['scale']
    img_l = transform.resize(img,(sz[0]/scale,sz[1]/scale))
    lores.append(img_l)
#lores = np.asarray(lores)

midres = []
for i in range(len(lores)):
    img = lores[i]
    sz = img.shape
    scale = conf['upsample_factor']
    img_m = transform.resize(img,(sz[0]*scale,sz[1]*scale))
    midres.append(img_m)
#midres = np.asarray(midres)

# COLLECT
#features = collect(midres, conf['scale'], conf['filters'], conf['window'], conf['overlap'], conf['border']);

interpolated = []
for i in range(len(lores)):
    img = lores[i]
    sz = img.shape
    scale = conf['scale']
    img_i = transform.resize(img,(sz[0]*scale,sz[1]*scale))
    interpolated.append(img_i)

patches = []
for i in range(len(hires)):
    img_h = hires[i]
    img_l = interpolated[i]
    img_p = img_h - img_l
    patches.append(img_p)
#patches = np.asarray(patches)

# COLLECT
#patches = collect(patches, conf['scale'], {}, conf['window'], conf['overlap'], conf['border']);

# comment
features_1 = sp.io.matlab.mio.loadmat('features.mat')
features = features_1['features']

# comment
patches_1 = sp.io.matlab.mio.loadmat('patches.mat')
patches = patches_1['patches']

# Set KSVD configuration
ksvd_conf = {'iternum': 5, 'memusage': 'normal', 'dictsize': 1024,
        'Tdata': 3, 'samples': patches.shape[1]}

# PCA dimensionality reduction
C = np.dot(features , features.T)
V,D = np.linalg.eig(C); # V:eigenvalue; D:eigen vector
sorted_indices = np.argsort(V)
evals = V[sorted_indices]
evecs = D[:,sorted_indices]

evals = np.cumsum(evals) / np.sum(evals)
b = np.arange(len(evals))
c = b[evals>1e-3]
k = c[0]
end = evals.shape[0]
evals.shape = (end,1)
conf['V_pca'] = evecs[:, k:end];
conf['ksvd_conf'] = ksvd_conf;
features_pca = np.dot(conf['V_pca'].T , features)

ksvd_conf['data'] = features_pca

# KSVD
# Training process
conf['dict_lores'], gamma2 = ksvd(ksvd_conf['iternum'],ksvd_conf['dictsize'],ksvd_conf['Tdata'],ksvd_conf['data'])

# comment
#gamma_1 = sp.io.matlab.mio.loadmat('gamma.mat')
#gamma2 = gamma_1['gamma2']
#
p_gt = np.dot(patches , gamma2.T)
g_gt = np.dot(gamma2 , gamma2.T)
#g_gt = sp.sparse.csr_matrix(g_gt).todense()
#g_gt = sp.sparse.csr_matrix.todense((g_gt))
dict_hires = np.dot(p_gt , sp.linalg.inv(g_gt))

conf['dict_hires'] = dict_hires;


from __future__ import print_function

import sys

# Reminder: Don't put any globals here. They will become unusable once we perform the loader trick below.

class Loader(object):
    _map = dict(str=str,
                bytes=bytes,
                input=input,
                int=int,
                chr=chr,
                range=range,
                filter=filter,
                hex=hex,
                map=map,
                oct=oct,
                zip=zip,
                open=open,
                round=round,
                super=super)

    def __init__(self):
        self._sys = sys
        self.__package__ = __package__
        self.__path__ = __path__
        self.USING_PYTHON2 = True if sys.version_info < (3, 0) else False
        if self.USING_PYTHON2:
            import io, future_builtins
            from future.builtins.newround import newround
            from future.builtins.newsuper import newsuper
            from .utils import input_with_unbuffered_stdout, RedirectingLoader
            self._redirecting_loader = RedirectingLoader
            self._map = dict(str=unicode,
                             bytes=str,
                             input=input_with_unbuffered_stdout,
                             int=long,
                             chr=unichr,
                             range=xrange,
                             ascii=future_builtins.ascii,
                             filter=future_builtins.filter,
                             hex=future_builtins.hex,
                             map=future_builtins.map,
                             oct=future_builtins.oct,
                             zip=future_builtins.zip,
                             open=io.open,
                             round=newround,
                             super=newsuper)
            self._renames = None
            self._loaders = None
        else:
            self._map['ascii'] = ascii

        self._map.update(dict(USING_PYTHON2=self.USING_PYTHON2,
                              PY2=self.USING_PYTHON2,
                              PY3=not self.USING_PYTHON2))
        self._manifest = list(self._map.keys())
        self._stdio_wrapped = False

    def __getattr__(self, attr):
        if attr == '__all__':
            if self.USING_PYTHON2:
                return list(self._map.keys())
            else:
                return []
        elif attr == '__package__':
            return self.__package__
        elif attr == '__path__':
            return self.__path__
        elif attr == '__loader__':
            return None
        elif attr in self._manifest:
            return self._map[attr]
        else:
            if self.USING_PYTHON2:
                from future import standard_library
                from collections import defaultdict
                if self._renames is None:
                    self._renames = {v: k for k, v in standard_library.RENAMES.items()}

                    self._loaders = defaultdict(self._redirecting_loader)

                    for new_module, new_name, old_module, old_name in standard_library.MOVES:
                        self._loaders[new_module].add(new_module, new_name, old_module, old_name)

                attr = self._renames[attr]
            __import__(attr)
            return self._sys.modules[attr]

    def wrap_stdio(self):
        if self.USING_PYTHON2 and not self._stdio_wrapped:
            import sys, io
            from io import TextIOWrapper
            class StderrTextIOWrapper(TextIOWrapper):
                def write(self, s):
                    if type(s) is unicode:
                        TextIOWrapper.write(self, s)
                    else:
                        TextIOWrapper.write(self, unicode(s, self.encoding))

            sys.stdin = io.open(sys.stdin.fileno(), encoding=sys.stdin.encoding)
            sys.stdout = StderrTextIOWrapper(io.FileIO(sys.stdout.fileno(), mode='w'), encoding=sys.stdout.encoding)
            sys.stderr = StderrTextIOWrapper(io.FileIO(sys.stderr.fileno(), mode='w'), encoding=sys.stderr.encoding)
            self._stdio_wrapped = True

loader = Loader()
sys.modules[__name__] = loader

{'io': 'io', 'future_builtins': 'future_builtins', 'newround': 'future.builtins.newround.newround', 'newsuper': 'future.builtins.newsuper.newsuper', 'input_with_unbuffered_stdout': 'utils.input_with_unbuffered_stdout', 'RedirectingLoader': 'utils.RedirectingLoader', 'standard_library': 'future.standard_library', 'defaultdict': 'collections.defaultdict', 'sys': 'sys', 'TextIOWrapper': 'io.TextIOWrapper'}


from logging import getLogger

from telegram import Update
from telegram.ext import CallbackContext
from telegram.ext import Updater
from telegram.ext import MessageHandler
from telegram.ext import CommandHandler
from telegram.ext import ConversationHandler
from telegram.ext import Filters

from echo.config import load_config
from echo.utils import debug_requests
from anketa.validators import GENDER_MAP
from anketa.validators import gender_hru
from anketa.validators import validate_age
from anketa.validators import validate_gender

config = load_config()

logger = getLogger(__name__)

NAME, GENDER, AGE, GROUP, PHONE, MAIL, EVENT = range(7)

@debug_requests
def start_handler(update: Update, context: CallbackContext):
    # Спросить на какое мероприятие
    update.message.reply_text(
        'Какое мероприятие:',
    )
    return EVENT
'''
@debug_requests
def start_handler(update: Update, context: CallbackContext):
    # Спросить имя
    update.message.reply_text(
        'Введи своё имя чтобы продолжить:',
    )
    return NAME
'''
@debug_requests
def event_handler(update: Update, context: CallbackContext):
    # Получить на какое мероприятие
    context.user_data[EVENT] = update.message.text
    logger.info('user_data: %s', context.user_data)

    # Спросить имя
    update.message.reply_text(
        'Введи своё имя чтобы продолжить:',
    )
    return NAME

@debug_requests
def name_handler(update: Update, context: CallbackContext):
    # Получить имя
    context.user_data[NAME] = update.message.text
    logger.info('user_data: %s', context.user_data)

    # Спросить пол
    genders = [f'{key} - {value}' for key, value in GENDER_MAP.items()]
    genders = '\n'.join(genders)
    update.message.reply_text(f'''
Выберите свой пол чтобы продолжить:
{genders}

''')
    # TODO: кнопки !
    return GENDER
#

@debug_requests
def age_handler(update: Update, context: CallbackContext):
    # Получить пол
    gender = validate_gender(text=update.message.text)
    if gender is None:
        update.message.reply_text('Пожалуйста, укажите корректный пол!')
        return GENDER

    context.user_data[GENDER] = gender
    logger.info('user_data: %s', context.user_data)

    # Спросить группу
    update.message.reply_text('''
Введите свою группу:
''')
    return GROUP

@debug_requests
def group_handler(update: Update, context: CallbackContext):
    # Получить ГРУППУ
    context.user_data[GROUP] = update.message.text
    # group = context.user_data[GROUP]
    # group = validate_gender(text=update.message.text)
    #if group is None:
    #    update.message.reply_text('Пожалуйста, укажите корректный пол!')
    #    return GENDER

   # context.user_data[GROUP] = group
    logger.info('user_data: %s', context.user_data)

    # Спросить возраст
    update.message.reply_text('''
Введите свой возраст:
''')
    return AGE
'''

@debug_requests
def phone_handler(update: Update, context: CallbackContext):
    # Получить ГРУППУ
    group = validate_gender(text=update.message.text)
    #if group is None:
    #    update.message.reply_text('Пожалуйста, укажите корректный пол!')
    #    return GENDER

    context.user_data[GROUP] = group
    logger.info('user_data: %s', context.user_data)

    # Спросить возраст
    update.message.reply_text('''
# Введите свой возраст:
''')
    return AGE
'''

@debug_requests
def finish_handler(update: Update, context: CallbackContext):
    # Получить возраст
    age = validate_age(text=update.message.text)
    if age is None:
        update.message.reply_text('Пожалуйста, введите корректный возраст!')
        return AGE

    context.user_data[AGE] = age
    # logger.info('user_data: %s', context.user_data)

    # TODO: вот тут запись в базу финала
    # TODO 2: очистить `user_data`

    # Завершить диалог
    update.message.reply_text(f'''
Все данные успешно сохранены!
Мероприятие: {context.user_data[EVENT]},
Вы: {context.user_data[NAME]}, пол: {gender_hru(context.user_data[GENDER])}, группа: {context.user_data[GROUP]},
возраст: {context.user_data[AGE]},
''')
    return ConversationHandler.END

@debug_requests
def cancel_handler(update: Update, context: CallbackContext):
    """ Отменить весь процесс диалога. Данные будут утеряны
    """
    update.message.reply_text('Отмена. Для начала с нуля нажмите /start')
    return ConversationHandler.END

@debug_requests
def echo_handler(update: Update, context: CallbackContext):
    update.message.reply_text(
        'Нажмите /Registration для заполнения анкеты!\nЕсли вы захотите отменить весь процесс регистрации.\nНажмите /cancel, Но данные будут утеряны ',
    )

def main():
    logger.info('Started Anketa-bot')
    updater = Updater(
        token=config.TG_TOKEN,
        base_url=config.TG_API_URL,
        use_context=True,
    )

    conv_handler = ConversationHandler(
        entry_points=[
            CommandHandler('Registration', start_handler),
        ],
        states={
            EVENT: [
                MessageHandler(Filters.all, event_handler, pass_user_data=True),
            ],
            NAME: [
                MessageHandler(Filters.all, name_handler, pass_user_data=True),
            ],
            GENDER: [
                MessageHandler(Filters.all, age_handler, pass_user_data=True),
            ],
            GROUP: [
                MessageHandler(Filters.all, group_handler, pass_user_data=True),
            ],
            AGE: [
                MessageHandler(Filters.all, finish_handler, pass_user_data=True),
            ],
        },
        fallbacks=[
            CommandHandler('cancel', cancel_handler),
        ],
    )
    updater.dispatcher.add_handler(conv_handler)
    updater.dispatcher.add_handler(MessageHandler(Filters.all, echo_handler))

    updater.start_polling()
    updater.idle()
    logger.info('Stopped Anketa-bot')

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Donny You(donnyyou@163.com)

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math
import numpy as np
import torch

from utils.tools.logger import Logger as Log

class HeatmapGenerator(object):

    def __init__(self, configer):
        self.configer = configer

    def __call__(self, gt_kpts, input_size, maskmap=None):
        width, height = input_size
        stride = self.configer.get('network', 'stride')
        num_keypoints = self.configer.get('data', 'num_kpts')
        sigma = self.configer.get('heatmap', 'sigma')
        method = self.configer.get('heatmap', 'method')
        batch_size = len(gt_kpts)

        heatmap = np.zeros((batch_size, num_keypoints + 1, height // stride, width // stride), dtype=np.float32)
        start = stride / 2.0 - 0.5

        for batch_id in range(batch_size):
            for i in range(len(gt_kpts[batch_id])):
                for j in range(num_keypoints):
                    if gt_kpts[batch_id][i][j][2] < 0:
                        continue

                    x = gt_kpts[batch_id][i][j][0]
                    y = gt_kpts[batch_id][i][j][1]
                    for h in range(height // stride):
                        for w in range(width // stride):
                            xx = start + w * stride
                            yy = start + h * stride
                            dis = 0
                            if method == 'gaussian':
                                dis = ((xx - x) * (xx - x) + (yy - y) * (yy - y)) / 2.0 / sigma / sigma
                            elif method == 'laplace':
                                dis = math.sqrt((xx - x) * (xx - x) + (yy - y) * (yy - y)) / 2.0 / sigma
                            else:
                                Log.error('Method: {} is not valid.'.format(method))
                                exit(1)

                            if dis > 4.6052:
                                continue

                            # Use max operator?
                            heatmap[batch_id][j][h][w] = max(math.exp(-dis), heatmap[batch_id][j][h][w])
                            if heatmap[batch_id][j][h][w] > 1:
                                heatmap[batch_id][j][h][w] = 1

            heatmap[batch_id, num_keypoints, :, :] = 1.0 - np.max(heatmap[batch_id, :-1, :, :], axis=0)

        heatmap = torch.from_numpy(heatmap)
        if maskmap is not None:
            heatmap = heatmap * maskmap.unsqueeze(1)

        return heatmap


from collections import defaultdict

class Graph:
    def __init__(self,typeofGraph,v):
        self.typeofGraph = typeofGraph
        self.v = v
        self.graph = defaultdict(set)

    def addEdge(self,u,v):
        if not self.typeofGraph:
            self.graph[u].add(v)
            self.graph[v].add(u)
        else:
            self.graph[u].add(v)

    #attach end of link (specially for islolated value)
    def make(self):
        for i in range(self.v):
                self.graph[i].add(-1)
    def showGraph(self):
        self.make()
        for i in range(v):
            print("vertex ",i," is linked with")
            for x in self.graph[i]:
                print("->",x,end="")
            print("")
if __name__ == "__main__":
    print("Enter 0 for undirected graph and 1 for directed graph")
    typeofGraph = int(input())
    print("Enter the total number of vertices")
    v = int(input())
    print("Enter the total number of edges")
    e = int(input())
    G1 = Graph(typeofGraph,v)
    print("Enter the edges : ")
    for i in range(e):
        u,v = map(int,input().split())
        G1.addEdge(u,v)
    G1.showGraph()


#! /usr/bin/python3

import requests
import lxml.html as lh
import csv
import pandas as pd

source = requests.get("https://www.worldometers.info/coronavirus/")
doc = lh.fromstring(source.content)
tr_elements = doc.xpath('//tr')
Cases_Map={}
i=0
# Since out first row is the header, data is stored on the second row onwards
for j in range(1, int(len(tr_elements)/2)):
    # T is our j'th row
    z=['World','Total:','','Africa','Oceania','South America','Asia','Europe','North America','Country,Other','Asia']
    T = tr_elements[j]
    try:
        if (T[0].text_content().replace('\n', "").strip() not in z):
            Cases_Map[T[0].text_content()]=int(T[1].text_content().replace(',',""))
    except:
        pass

Cases_Map.pop('World',None)
Cases_Map.pop('Total:',None)
Cases_Map.pop('\n\n',None)
Cases_Map.pop('\nAfrica\n',None)
Cases_Map.pop('\nOceania\n',None)
Cases_Map.pop('\nSouth America\n',None)
Cases_Map.pop('\nAsia\n',None)
Cases_Map.pop('\nEurope\n',None)
Cases_Map.pop('\nNorth America\n',None)
Cases_Map=sorted(Cases_Map.items(), key = lambda x : x[1],reverse=True)
Cases_Map=dict(Cases_Map)
try:
    with open('cases.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Rank','Country', 'Cases'])
        i=0
        for key, value in Cases_Map.items():
            i=i+1
            writer.writerow([i,key, value])
except IOError:
    print("I/O error")

active=pd.read_csv('cases.csv',index_col=[0,1,2],encoding = "ISO-8859-1")
active.to_html('cases.html')


##############################################################################
# Institute for the Design of Advanced Energy Systems Process Systems
# Engineering Framework (IDAES PSE Framework) Copyright (c) 2018-2019, by the
# software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory,  National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia
# University Research Corporation, et al. All rights reserved.
#
# Please see the files COPYRIGHT.txt and LICENSE.txt for full copyright and
# license information, respectively. Both files are also available online
# at the URL "https://github.com/IDAES/idaes-pse".
##############################################################################
"""
Utility code for testing IDAES code.
"""
import os
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor

__author__ = "Dan Gunter"

def run_notebook(path: str, name: str):
    """Run a specific jupyter notebook 'name' located at `path`.
    """
    fullpath = os.path.join(path, name)
    with open(fullpath) as f:
        nb = nbformat.read(f, as_version=nbformat.NO_CONVERT)

    ep = ExecutePreprocessor(timeout=100)
    ep.allow_errors = True
    ep.preprocess(nb, {"metadata": {"path": path}})

    failed = False
    for cell in nb.cells:
        if "outputs" in cell:
            for output in cell["outputs"]:
                if output.output_type == "error":
                    failed = True
                    num = cell["execution_count"]
                    print(f"ERROR in {output['ename']} in {fullpath} [{num}]:")
                    for tb_line in output["traceback"]:
                        print(tb_line)
    return not failed