from typing import Any, Tuple
from pyrogram import types,filters
import os,secrets,logging

from ub.core.decorators import log_errors
from .. import (
    slave_bot,
    USERBOT_ID,
    progress,
    userbot
)
from pytube import YouTube

streamdict = {}

def rmfile(filename):
    if os.path.exists(filename):
        os.remove(filename)

def genkeyboard(sid,streams,typ,chatid):
    kb = []
    streams = list(streams)
    while len(streams) != 0:
        lel = []
        for x in streams[:3]:
            lel.append(
                types.InlineKeyboardButton(
                    text = f'{round(x.filesize / 10**6, 2)}MB' if typ=='audio' else f'{x.resolution}',
                    callback_data = f'{sid}|{x.itag}|{chatid}')
                )
            streams.remove(x)
        kb.append(lel)
    return types.InlineKeyboardMarkup(kb)

def get_streams(link,type) -> Tuple[int,Any,Any]:
    p = YouTube(link)
    if type == 'video':
        streams = p.streams.filter(type = 'video', progressive = True)
    else:
        streams = p.streams.filter(type = 'audio',mime_type = 'audio/webm')
    sid = 0 if len(list(streamdict.keys())) == 0 else list(streamdict.keys())[-1] + 1
    streamdict[sid] = streams
    return sid,p

async def dvid(chatid,sid,itag,callback: types.CallbackQuery):
    await callback.edit_message_text(text=f'**Downloading...**')
    stream = streamdict[sid]
    vid = stream.get_by_itag(itag)
    n = secrets.token_hex(16)
    ext = 'mp4' if vid.type == 'video' else 'webm'
    filename = f'{n}.{ext}'
    logging.info(f'STREAM {itag} FETCHED')
    vid.download(output_path='videos/',filename=filename)
    logging.info('DOWNLOADED')
    try:
        if vid.type == 'video':
            await userbot.send_video(
                chat_id=chatid,
                video=f'videos/{filename}',
                caption=f'**Title:** {vid.title}\n**Resolution:** {vid.resolution}',
                supports_streaming=True,
                progress=progress,
                progress_args=('Uploading...',callback),
                file_name=vid.title
            )
        else:
            os.rename(f'videos/{filename}',f'videos/{n}.mp3')
            await callback.edit_message_text(text=f'**Uploading...**')
            await userbot.send_audio(
                chat_id=chatid,
                audio=f'videos/{n}.mp3',
                caption=f'**Title:** {vid.title}',
                file_name=vid.title
            )
        await callback.edit_message_text('**Uploaded successfully**')
    except Exception as e:
        await callback.edit_message_text(f'**Exception**\n`{e}`')
    rmfile(f'videos/{n}.mp3')
    rmfile(f'videos/{filename}')
    logging.info('VIDEO LOCALLY REMOVED')
    del streamdict[sid]

@slave_bot.on_inline_query(
    filters.regex('^yt[a-z]?\|.+\|.+')
)
@log_errors
async def ytv(c,query: types.InlineQuery):
    if query.from_user.id != USERBOT_ID:
        return
    res = query.matches[0].group().strip()
    typ = 'video' if res[2] == 'v' else 'audio'
    link = res.split('|')[1]
    chatid = int(res.split('|')[2])
    try:
        sid,ytobj = get_streams(link,typ)
        stream = streamdict[sid]
    except Exception as e:
        await query.answer(
            [
                types.InlineQueryResultArticle("error",
                types.InputTextMessageContent(f'**Exception:** `{e}`'))
            ],is_personal=True
        )
        return
    keyboard = genkeyboard(sid,stream,typ,chatid)
    await query.answer(
        [
            types.InlineQueryResultArticle(
                'result',
                types.InputTextMessageContent(
                    f'**Title:** {ytobj.title}\n**Author:** {ytobj.author}' \
                    if typ == 'video' else \
                        f'**Title:** {ytobj.title}\n\n**Choose a download filesize-**'
                ),reply_markup=keyboard
            )
        ],is_personal=True
    )

@slave_bot.on_callback_query(
    filters.regex('\d+\|\d+\|[-]?.+\d$'),
)
async def dlvideo_(c,query: types.CallbackQuery):
    if query.from_user.id != USERBOT_ID:
        return await query.answer('Sorry, this button is not for you',cache_time=300)
    s = query.data.split('|')
    sid, itag, chatid = int(s[0]), int(s[1]), int(s[2])
    try:
        await dvid(chatid,sid,itag,query)
    except Exception as e:
        await query.edit_message_text(f'**Exception:** `{e}`')


"""AoC 2015.03 problem solver.

Takes input from STDIN by default.

(c) Alexander Kashev, 2017
"""
import sys

def chars(file, chunkSize=4096):
  """
  Take a file object, read it in chuncks and iterate over it one character at a time.

  Keyword arguments:
  file      --- a file object to iterate over
  chunkSize --- buffer size for file reads (default=4096)
  """
  chunk = file.read(chunkSize)
  while chunk:
    for char in chunk:
      yield char
    chunk = file.read(chunkSize)

def move(position, instruction):
  """
  Take a position and offset it based on instuction, or raise an error on invalid instruction.

  Keyword arguments:
  position    --- current position as a tuple (x,y)
  Instruction --- single-character instruction to move in ["^", "v", ">", "<"]
  """
  if instruction == "^":
    return (position[0], position[1] + 1)
  elif instruction == "v":
    return (position[0], position[1] - 1)
  elif instruction == ">":
    return (position[0] + 1, position[1])
  elif instruction == "<":
    return (position[0] - 1, position[1])
  else:
    raise ValueError("Instruction '{}' not recognized".format(instruction))

def solver(file):
  """
  Take a file object with input and solve AoC 2015.03 problem on the input.

  Keyword arguments:
  file      --- a file object to read input from
  """
  alone = set([(0, 0)])
  alone_position = (0, 0)

  together = set([(0, 0)])
  santa_position = (0, 0)
  robot_position = (0, 0)
  robot_turn = False

  for instruction in chars(file):
    alone_position = move(alone_position, instruction)
    alone.add(alone_position)

    if robot_turn:
      robot_position = move(robot_position, instruction)
      together.add(robot_position)
    else:
      santa_position = move(santa_position, instruction)
      together.add(santa_position)

    robot_turn = not robot_turn

  return (len(alone), len(together))

if __name__ == "__main__":
  solution = solver(sys.stdin)

  print("Part A: Santa alone will deliver presents to {} houses.".format(solution[0]))
  print("Part B: Santa and Robo-Santa will deliver presents to {} houses.".format(solution[1]))


from cx_Freeze import setup, Executable

base = None

executables = [Executable("main.py", base=base)]

packages = ["idna"]
options = {
    'build_exe': {
        'packages': packages,
    },
}

setup(
    name="OSM Telegram Scrapper",
    options=options,
    version="2.0.0",
    description='OSM Telegram Scrapper',
    executables=executables
)


from collections import OrderedDict
import itertools
from rest_framework import status, generics
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Game

class RootEndpointList(APIView):
    def get(self, request):
        return Response({
            'links': {
                'ascended': '/ascended',
                'games': '/games',
                'players': '/players',
                'leaderboards': '/leaderboards',
            }
        })

#class GamesList(generics.ListAPIView):
#    pagination_class = LimitOffsetPagination
#    serializer_class = GameSerializer
#    search_fields = ['role', 'race', 'gender', 'align']
#
#    def get_queryset(self):
#        query_params = self.get_serializer_context()['request'].query_params
#        params = {k: query_params[k] for k in itertools.filterfalse(lambda x: x not in self.search_fields, query_params)}
#        if 'player' in self.kwargs:
#            return Game.objects.filter(name=self.kwargs['player'], **params)
#        else:
#            return Game.objects.filter(**params)
#
#class AscendedList(GamesList):
#    serializer_class = AscensionSerializer
#    search_fields = ['role', 'race', 'gender', 'align']
#
#    def get_queryset(self):
#        query_params = self.get_serializer_context()['request'].query_params
#        params = {k: query_params[k] for k in itertools.filterfalse(lambda x: x not in self.search_fields, query_params)}
#        if 'player' in self.kwargs:
#            return Game.objects.filter(won=True, name=self.kwargs['player'], **params)
#        else:
#            return Game.objects.filter(won=True, **params)

class LeaderboardsList(APIView):
    def get(self, request):
        links = {
            'conducts': request.path + '/' + 'conducts',
            'minscore': request.path + '/' + 'minscore',
            'realtime': request.path + '/' + 'realtime',
            'turncount': request.path + '/' + 'turncount',
            'wallclock': request.path + '/' + 'wallclock',
        }
        return Response({
            'links': links,
        })

# class PlayersList(generics.ListAPIView):
#     pagination_class = LimitOffsetPagination
#     serializer_class = PlayerLinkSerializer
#     queryset = GameRecord.objects.values('name').distinct()

# class Leaderboard(generics.ListAPIView):
#     pagination_class = LimitOffsetPagination
#     serializer_class = AscensionSerializer
#     sort_field = None
#
#     def get_queryset(self):
#         players = list(OrderedDict.fromkeys([
#             p['name'] for p in
#             Game.objects.filter(won=True).order_by(self.sort_field).values('name')
#         ]))
#         return [Game.objects.filter(won=True, name=p).order_by(self.sort_field).first() for p in players]
#
# class ConductsBoard(Leaderboard):
#     sort_field = '-nconducts'
#
# class MinscoreBoard(Leaderboard):
#     sort_field = 'points'
#
# class RealtimeBoard(Leaderboard):
#     sort_field = 'realtime'
#
# class TurncountBoard(Leaderboard):
#     sort_field = 'turns'
#
# class WallclockBoard(Leaderboard):
#     sort_field = 'wallclock'


from datetime import date
my_dict = {"first_name": "Mariah", "last_name": "Carey", "birth_date": "27.02.1970", "hobbies": ["Sing", "Compose", "Act"]}

def calculate_age(born):
    """
    calculate age by birth date
    :param born birth date
    :type tuple
    :return age
    :rtype int
    """
    today = date.today()
    return today.year - int(born[2]) - ((today.month, today.day) < (int(born[1]), int(born[0])))

while True:
    user_input = input("Enter value 1-7: ")
    if user_input == "1":
        print(my_dict["last_name"])
    elif user_input == "2":
        print(my_dict["birth_date"])
    elif user_input == "3":
        print(len(my_dict["hobbies"]))
    elif user_input == "4":
        print(my_dict["hobbies"][-1])
    elif user_input == "5":
        my_dict["hobbies"].append("Cooking")
    elif user_input == "6":
        birth_date_tuple = (my_dict["birth_date"][0:2], my_dict["birth_date"][3:5], my_dict["birth_date"][6:])
        print(birth_date_tuple)
    elif user_input == "7":
        born = (my_dict["birth_date"][0:2], my_dict["birth_date"][3:5], my_dict["birth_date"][6:])
        my_dict["age"] = calculate_age(born)
        print(my_dict["age"])


# -*- coding: utf-8 -*-

# (c) 2015 Marcos Dione <mdione@grulic.org.ar>

# This file is part of ayrton.
#
# ayrton is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ayrton is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ayrton.  If not, see <http://www.gnu.org/licenses/>.

import functools
from selectors import DefaultSelector, EVENT_READ
import os
from socket import socket
import itertools
import errno
import paramiko.channel
import traceback

import logging
# I hope this is not toot late...
import ayrton.logger
logger= logging.getLogger ('ayrton.utils')

# TODO: there's probably a better way to do this
# TODO: move to logger
def debug2 (self, msg, *args, **kwargs):  # pragma: no cover
    if self.manager.disable>=logging.DEBUG2:
        return

    if logging.DEBUG2>=self.getEffectiveLevel ():
        self._log (logging.DEBUG2, msg, args, **kwargs)

def debug3 (self, msg, *args, **kwargs):  # pragma: no cover
    if self.manager.disable>=logging.DEBUG3:
        return

    if logging.DEBUG3>=self.getEffectiveLevel ():
        self._log (logging.DEBUG3, msg, args, **kwargs)

def patch_logging ():
    # based on https://mail.python.org/pipermail/tutor/2007-August/056243.html
    logging.DEBUG2= 9
    logging.DEBUG3= 8

    logging.addLevelName (logging.DEBUG2, 'DEBUG2')
    logging.addLevelName (logging.DEBUG3, 'DEBUG3')

    logging.Logger.debug2= debug2
    logging.Logger.debug3= debug3

patch_logging ()

def any_comparator (a, b):  # pragma: no cover
    try:
        if a==b:
            return 0
        elif a<b:
            return -1
        else:
            return 1
    except TypeError:
        return any_comparator (str (type (a)), str (type (b)))

def dump_dict (d, level=1):  # pragma: no cover
    if d is not None:
        strings= []

        if level==0:
            strings.append ("{\n")
        for k in sorted (d.keys (), key=functools.cmp_to_key (any_comparator)):
            v= d[k]
            if type (v)!=dict:
                strings.append ("%s%r: %r,\n" % ( '    '*level, k, v ))
            else:
                strings.append ("%s%r: {\n"   % ( '    '*level, k))
                strings.extend (dump_dict (v, level+1))
                strings.append ("%s},\n"      % ( '    '*level, ))
        if level==0:
            strings.cappend ("}\n")

        return ''.join (strings)
    else:
        return 'None'

def read (src, buf_len):
    if isinstance (src, int):
        return os.read (src, buf_len)
    elif isinstance (src, (socket, paramiko.channel.Channel)):
        return src.recv (buf_len)
    else:
        return src.read (buf_len)

def write (dst, data):
    if isinstance (dst, int):
        return os.write (dst, data)
    elif isinstance (dst, (socket, paramiko.channel.Channel)):
        return dst.send (data)
    else:
        ans= dst.write (data)
        dst.flush ()

        return ans

def close (f):
    logger.debug ('closing %s', f, callers=1)
    try:
        if isinstance (f, int):
            os.close (f)
        else:
            if f is not None:
                f.close ()
    except OSError as e:
        logger.debug ('closing gave %s', e)
        if e.errno!=errno.EBADF:
            raise

def copy_loop (copy_to, finished=None, buf_len=10240):
    """copy_to is a dict(in: out). When any in is ready to read, data is read
    from it and writen in its out. When any in is closed, it's removed from
    copy_to. finished is a pipe; when data comes from the read end, or when
    no more ins are present, the loop finishes."""
    if finished is not None:
        copy_to[finished]= None

    # NOTE:
    # os.sendfile (self.dst, self.src, None, 0)
    # OSError: [Errno 22] Invalid argument
    # and splice() is not available
    # so, copy by hand
    selector = DefaultSelector ()
    for src in copy_to.keys ():
        if (     not isinstance (src, int)
                and (   getattr (src, 'fileno', None) is None
                    or not isinstance (src.fileno(), int)) ):
            logger.debug ('type mismatch: %s', src)
        else:
            logger.debug ("registering %s for read", src)
            # if finished is also one of the srcs, then register() complains
            try:
                selector.register (src, EVENT_READ)
            except KeyError:
                pass

    def close_file (f):
        if f in copy_to:
            del copy_to[f]

        try:
            selector.unregister (i)
        except KeyError:
            pass

        close (f)

    while len (copy_to)>0:
        logger.debug (copy_to)

        events= selector.select ()
        for key, _ in events:
            logger.debug ("%s is ready to read", key)
            i= key.fileobj

            # for error in e:
            #     logger.debug ('%s error')
                # TODO: what?

            o= copy_to[i]

            try:
                data= read (i, buf_len)
                logger.debug2 ('%s -> %s: %s', i, o, data)

            # ValueError: read of closed file
            except (OSError, ValueError) as e:
                logger.debug ('stopping copying for %s', i)
                logger.debug (traceback.format_exc ())
                close_file (i)
                break
            else:
                if len (data)==0:
                    logger.debug ('stopping copying for %s, no more data', i)
                    close_file (i)

                    if finished is not None and i==finished:
                        logger.debug ('finishing')
                        # quite a hack :)
                        copy_to= {}

                        break
                else:
                    write (o, data)

    selector.close ()
    logger.debug ('over and out')


import cv2
import numpy as np
import os
from tqdm import tqdm
from os.path import join as ospj
from os.path import dirname as ospd
from evaluation import compute_bboxes_from_scoremaps
from evaluation import BoxEvaluator
from evaluation import MaskEvaluator
from evaluation import configure_metadata
from utils import t2n
from evaluation import calculate_multiple_iou
import pickle
import seaborn as sns
import matplotlib.pyplot as plt
import torch.nn.functional as F

_IMAGENET_MEAN = [0.485, .456, .406]
_IMAGENET_STDDEV = [.229, .224, .225]
# _RESIZE_LENGTH = 224

def normalize_scoremap(cam):
    """
    Args:
        cam: numpy.ndarray(size=(H, W), dtype=np.float)
    Returns:
        numpy.ndarray(size=(H, W), dtype=np.float) between 0 and 1.
        If input array is constant, a zero-array is returned.
    """
    if np.isnan(cam).any():
        return np.zeros_like(cam)
    if cam.min() == cam.max():
        return np.zeros_like(cam)
    cam -= np.percentile(cam, 65)
    cam /= cam.max()
    cam = np.where(cam < 0, 0, cam)
    cam = np.where(cam > 1, 1, cam)

    return cam

class CAMComputer(object):
    def __init__(self, model, loader, metadata_root, mask_root,
                 iou_threshold_list, dataset_name, split,
                 multi_contour_eval, cam_curve_interval=.001, cam_path=None):
        self.model = model
        self.model.eval()
        self.loader = loader
        self.split = split
        self.cam_path = cam_path

        self.metadata = configure_metadata(metadata_root)
        self.cam_threshold_list = list(np.arange(0, 1, cam_curve_interval))

        self.evaluator = {"OpenImages": MaskEvaluator,
                          "CUB": BoxEvaluator,
                          "CAR": BoxEvaluator,
                          "DOG": BoxEvaluator,
                          "AIR": BoxEvaluator,
                          "ILSVRC": BoxEvaluator
                          }[dataset_name](metadata=self.metadata,
                                          dataset_name=dataset_name,
                                          split=split,
                                          cam_threshold_list=self.cam_threshold_list,
                                          iou_threshold_list=iou_threshold_list,
                                          mask_root=mask_root,
                                          multi_contour_eval=multi_contour_eval)
        self.total_iou_list = []

    def compute_and_evaluate_cams(self):
        # print("Computing and evaluating cams.")
        for images, targets, image_ids in tqdm(self.loader, desc="Evaluation", mininterval=0.1):
            image_size = images.shape[2:]
            images = images.cuda()
            cams = t2n(self.model(images, targets, return_cam=True))

            for cam, image_id in zip(cams, image_ids):
                cam_resized = cv2.resize(cam, image_size,
                                         interpolation=cv2.INTER_CUBIC)
                cam_normalized = normalize_scoremap(cam_resized)
                if self.split in ('test'):
                    cam_path = ospj(self.cam_path, 'scoremaps', image_id)
                    if not os.path.exists(ospd(cam_path)):
                        os.makedirs(ospd(cam_path))
                    np.save(ospj(cam_path), cam_normalized)
                else:
                    cam_path = ospj(self.cam_path, 'scoremaps', image_id)
                    if not os.path.exists(ospd(cam_path)):
                        os.makedirs(ospd(cam_path))
                    np.save(ospj(cam_path), cam_normalized)
                self.evaluator.accumulate(cam_normalized, image_id)
                self.total_iou_list.append(self.evaluator.image_iou_list)

        return self.evaluator.compute(), np.array(self.total_iou_list)


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("create", views.create, name="create"),
    path("listings/<int:auction_id>", views.auction, name="auction"),
    path("watchlist", views.watchList, name = "watchList"),
    path("profile", views.profile, name="profile"),
    path("profile/active_auctions", views.active_auctions, name="active auctions"),
    path("profile/non_active_auctions", views.non_active_auctions, name="non active auctions"),
    path("profile/won_auctions", views.won_auctions, name="won auctions"),
]


from tornado.web import RequestHandler

from bin.util.helper import json_dumps

class BaseHandler(RequestHandler):
    def __init__(self, application, request, **kwargs):
        super(BaseHandler, self).__init__(application, request, **kwargs)

    def write(self, chunk):
        if isinstance(chunk, (dict, list, tuple)):
            if 'errcode' in chunk:
                self.errcode = chunk['errcode']

            chunk = json_dumps(chunk)
            self.set_header('Content-Type', 'application/json; charset=UTF-8')
            # self.set_header('Access-Control-Allow-Origin', '*')
            # self.set_header('Access-Control-Allow-Headers', 'X-Requested-With')
            self.response = chunk[:253] + '...' if len(chunk) > 256 else chunk
        else:
            ct = self._headers['Content-Type']
            if isinstance(ct, bytes):
                ct = ct.decode()

            self.response = '<%s>' % ct.split(';')[0]

        super(BaseHandler, self).write(chunk)


# -*- coding: utf-8 -*-
from collective.mustread.behaviors.maybe import IMaybeMustRead
from collective.mustread.behaviors.track import ITrackReadEnabled
from collective.mustread.testing import FunctionalBaseTestCase
from plone.app.testing import TEST_USER_ID
from plone.autoform.interfaces import IFormFieldProvider
from plone.behavior.interfaces import IBehavior
from zope.component import getUtility
from zope.interface.verify import verifyObject

class TestMaybeMustRead(FunctionalBaseTestCase):

    def test_installed(self):
        name = 'collective.mustread.maybe_must_read'
        behavior = getUtility(IBehavior, name=name)
        self.assertEqual(behavior.interface, IMaybeMustRead)
        self.assertTrue(IFormFieldProvider.providedBy(behavior.interface))

    def test_schema_default(self):
        adapted = IMaybeMustRead(self.page)
        self.assertFalse(adapted.must_read)

    def test_write_roundtrip(self):
        adapted = IMaybeMustRead(self.page)
        adapted.must_read = True
        adapted2 = IMaybeMustRead(self.page)
        self.assertTrue(adapted2.must_read)

class TestTrackReadEnabled(FunctionalBaseTestCase):

    def test_installed(self):
        name = 'collective.mustread.track_read_enabled'
        behavior = getUtility(IBehavior, name=name)
        self.assertEqual(behavior.interface, ITrackReadEnabled)

    def test_interface(self):
        adapted = ITrackReadEnabled(self.page)
        self.assertTrue(verifyObject(ITrackReadEnabled, adapted))

    def test_has_read_unread(self):
        adapted = ITrackReadEnabled(self.page)
        self.assertFalse(adapted.has_read())

    def test_mark_read_has_read(self):
        adapted = ITrackReadEnabled(self.page)
        adapted.mark_read()
        adapted2 = ITrackReadEnabled(self.page)
        self.assertTrue(adapted2.has_read())

    def test_who_read(self):
        adapted = ITrackReadEnabled(self.page)
        adapted.mark_read()
        adapted2 = ITrackReadEnabled(self.page)
        self.assertEqual([TEST_USER_ID], adapted2.who_read())


#!/usr/bin/env python3
from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session
import requests

server = 'https://localhost/default'

client_id = '29968236-c3ed-4b6e-ac16-affaeac0a8c2'
client_secret = 'secret'

auth = HTTPBasicAuth(client_id, client_secret)
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
response = oauth.fetch_token(token_url=server+'/token', auth=auth)

token = response['access_token']

req = requests.get(server+'/resource', headers={"Authorization": f"Bearer {token}"})
print (req)


from typing import Tuple

import numpy as np

class EquSolver(object):
  """Numpy-based Jacobi method equation solver implementation."""

  def __init__(self) -> None:
    super().__init__()
    self.N = 0

  def partition(self, mask: np.ndarray) -> np.ndarray:
    return np.cumsum((mask > 0).reshape(-1)).reshape(mask.shape)

  def reset(self, N: int, A: np.ndarray, X: np.ndarray, B: np.ndarray) -> None:
    """(4 - A)X = B"""
    self.N = N
    self.A = A
    self.B = B
    self.X = X

  def sync(self) -> None:
    pass

  def step(self, iteration: int) -> Tuple[np.ndarray, np.ndarray]:
    for _ in range(iteration):
      # X = (B + AX) / 4
      self.X = (
        self.B + self.X[self.A[:, 0]] + self.X[self.A[:, 1]] +
        self.X[self.A[:, 2]] + self.X[self.A[:, 3]]
      ) / 4.0
    tmp = self.B + self.X[self.A[:, 0]] + self.X[self.A[:, 1]] + \
      self.X[self.A[:, 2]] + self.X[self.A[:, 3]] - 4.0 * self.X
    err = np.abs(tmp).sum(axis=0)
    x = self.X.copy()
    x[x < 0] = 0
    x[x > 255] = 255
    return x, err

class GridSolver(object):
  """Numpy-based Jacobi method grid solver implementation."""

  def __init__(self) -> None:
    super().__init__()
    self.N = 0

  def reset(
    self, N: int, mask: np.ndarray, tgt: np.ndarray, grad: np.ndarray
  ) -> None:
    self.N = N
    self.mask = mask
    self.bool_mask = mask.astype(bool)
    self.tgt = tgt
    self.grad = grad

  def sync(self) -> None:
    pass

  def step(self, iteration: int) -> Tuple[np.ndarray, np.ndarray]:
    for _ in range(iteration):
      # tgt = (grad + Atgt) / 4
      tgt = self.grad.copy()
      tgt[1:] += self.tgt[:-1]
      tgt[:-1] += self.tgt[1:]
      tgt[:, 1:] += self.tgt[:, :-1]
      tgt[:, :-1] += self.tgt[:, 1:]
      self.tgt[self.bool_mask] = tgt[self.bool_mask] / 4.0

    tmp = 4 * self.tgt - self.grad
    tmp[1:] -= self.tgt[:-1]
    tmp[:-1] -= self.tgt[1:]
    tmp[:, 1:] -= self.tgt[:, :-1]
    tmp[:, :-1] -= self.tgt[:, 1:]

    err = np.abs(tmp[self.bool_mask]).sum(axis=0)

    tgt = self.tgt.copy()
    tgt[tgt < 0] = 0
    tgt[tgt > 255] = 255
    return tgt, err


# MSPA 400 Session 10 Supplemental Python Module

# This is a supplemental module dealing with letter and limited text analysis.
# The module and reading assignment are optional.

# Reading Assignment:
#"Think Python" 2nd Edition Chapter 9 (9.1-9.5), Chapter 13 (13.1-13.4)
#"Think Python" 3rd Edition Chapter 9 (pages 97-101),Chapter 13 (pages 147-150)

# Objective:  This program does letter analysis to demonstrate coding with
# strings, lists, dictionaries and tuples.  For loops, functions and counters
# are used. The program uses letter frequency analysis to illustrate the notions
# of statistical independence, dependence plus conditional probabilities.

# This is a list of the alphabet to be used later.

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z']

#This is an arbitrary string for analysis.

string = 'thisthesmallregardharrietwhichknightleysitmostthingstruex'

# find() is a search and counting function which will match letter strings
# in the specified file against supplied string "letters" and do a count.

def find(letters,file):
    count = 0
    for element in file:
        if element == letters:
            count = count + 1
    return count

# The following code searches the string supplied above for each letter in
# the alphabet and places the count of each letter found in a dictionary.
# The dictionary uses the letter found as a key to point at the count
# determined for that letter in the string supplied.  The total count is
# accumulated to compute probabilities.

d=dict()
total = 0
for letter in alphabet:
    count = find(letter, string)
    d[letter] = count
    total = total + count

# Now we can calculate the relative frequencies of occurrence and do various
# calculations.  The dictionary is useful since the key points at a count.
# The following print statements show the flexibility that is possible.

total = float(total)
print ("Probability of t = %r"  %round(d['t']/total,4))
print ("Probability of h = %r"  %round(d['h']/total,4))
product = (d['t']/total)*(d['h']/total)
print ("Product of the two probabilities = %r" %(round(product,4)))

# Creat a list of sequential pairs of characters from the string.
# Each pair is formed by concatenation of string elements and placed
# in the list, lpairs. Concatenation does not combine duplicate pairs
# into a single element.  They are listed separately as found.

lpairs = []
sum = 0.0
for index in range(0,len(string)-1):
    pair = string[index]+string[index+1]  # Concatenate to form the pair.
    lpairs = lpairs + [pair]              # Add to the list of pairs.
    sum = sum + 1                         # Count the total number.

# The find() function can be used on pairs. It works with strings.
# The relative frequency of joint occurrence is printed out. What follows
# is an example using the letters "t" and "h".

pair = 'th'
pair2 = 'ht'
count = find(pair,lpairs)+find(pair2,lpairs)
print ("Probability of joint occurrence =  %r" %(round(count/sum,4)))

#Conditional probabilities
print ("Probability h given t = %r" %(round((count/sum)/(d['t']/total),4)))
print ("Probability t given h = %r" %(round((count/sum)/(d['h']/total),4)))

# Tuples can be created from a dictionary and vice versa.
# Tuples are immutable and sometimes used as dictionary keys.

t = d.items()
new_d = dict(t)

# A string can be converted to a tuple, and the tuple used for searching.
# Tuples are immutable. Once defined, they can not be changed.

print ('\nThe string below will be converted to a tuple.\n')
print (string)

t = tuple(string)
print ('\nWhat follows is a tuple constructed from the string above.\n')
print (t)

# This demonstrates how a tuple can be used with the find() function.
print ("\nThe count of the letter 'i' in the tuple equals %r" %find('i',t))


# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))

# -- Project information -----------------------------------------------------

project = 'web and binary security'
copyright = '2020, Surreal'
author = 'Surreal'

version = u'1.0.0'

release = u'1.0.0'

# The full version, including alpha/beta/rc tags
release = '_build'

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
import sphinx_rtd_theme
extensions = [
    'sphinx.ext.todo',
    'sphinx.ext.ifconfig',
	'sphinx.ext.autosectionlabel'
]

autosectionlabel_prefix_document = True
autosectionlabel_maxdepth = 3

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#

html_last_updated_fmt = '%b %d, %Y'
html_domain_indices = True
#html_logo = "_static/logo.png"
html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
html_use_index = True
html_show_sphinx = False

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

html_style = 'css/my_theme.css'

master_doc = 'index'

latex_engine = 'xelatex'
latex_elements = {
    'papersize': 'a4paper',
    'pointsize': '11pt',
    'title': u'网络安全',
    'releasename': 'wangzihan',
    'author': 'wangzi',
    'preamble': r'''
\usepackage{xeCJK}
\usepackage{fontspec,xunicode}
\setCJKmainfont[BoldFont=STZhongsong, ItalicFont=STKaiti]{STSong}
\setCJKsansfont[BoldFont=STHeiti]{STXihei}
\setCJKmonofont{STFangsong}
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt
\parindent 2em
\definecolor{VerbatimColor}{rgb}{0.95,0.95,0.95}
\setcounter{tocdepth}{3}
\renewcommand\familydefault{\ttdefault}
\renewcommand\CJKfamilydefault{\CJKrmdefault}
'''
}
# 设置文档
latex_documents = [
    (master_doc, 'WebSec.tex', u'\\unexpanded{网络安全}',
     u'\\unexpanded{王子翰}', 'manual', True),
]

#latex_docclass = {'howto':'jsarticle','manual' : 'jsbook'}

latex_logo = "_static/logo.png"
#latex_use_parts = False
#latex_show_pagerefs = False

man_pages = [
    (master_doc, 'WebSec', 'websec Documentation',
     [author], 1)
]


"""
Training script of KWS models using GEM as the CL method.
Reference: Gradient Episodic Memory for Continual Learning
Gradient Episodic Memory for Continual Learning

@author huangyz0918
@date 05/09/2021
"""
import time

import neptune
import argparse
import numpy as np

import torch

from model import STFT_TCResnet, MFCC_TCResnet, STFT_MLP, MFCC_RNN, parameter_number
from model import Trainer, Evaluator, get_dataloader_keyword
from model.util import Buffer, get_grad_dim

def on_task_update(task_id, grads_cs, grad_dims, buffer, device, loader):
    """
    Update the regularization after each task learning.
    """
    current_task = task_id + 1
    grads_cs.append(torch.zeros(np.sum(grad_dims)).to(device))

    # add data to the buffer.
    cur_x, cur_y = next(iter(loader))
    samples_per_task = len(cur_x)  # sample a batch data.
    buffer.add_data(
        examples=cur_x.to(device),
        labels=cur_y.to(device),
        task_labels=torch.ones(samples_per_task, dtype=torch.long).to(device) * (current_task - 1)
    )

if __name__ == "__main__":
    def options(config):
        parser = argparse.ArgumentParser(description="Input optional guidance for training")
        parser.add_argument("--epoch", default=10, type=int, help="The number of training epoch")
        parser.add_argument("--lr", default=0.01, type=float, help="Learning rate")
        # should be a multiple of batch size.
        parser.add_argument("--bsize", default=640, type=float, help="the rehearsal buffer size")
        parser.add_argument('--gamma', type=float, default=0.5, help='Margin parameter for GEM.')
        parser.add_argument("--batch", default=128, type=int, help="Training batch size")
        parser.add_argument("--step", default=30, type=int, help="Training step size")
        parser.add_argument("--gpu", default=4, type=int, help="Number of GPU device")
        parser.add_argument("--log", default=False, action='store_true',
                            help="record the experiment into web neptune.ai")
        parser.add_argument("--ek", default=False, action='store_true', help="evaluate the CL by keywords")
        parser.add_argument("--dpath", default="./dataset", type=str, help="The path of dataset")
        parser.add_argument("--tqdm", default=False, action='store_true', help="enable terminal tqdm output.")
        parser.add_argument("--model", default="stft", type=str, help="[stft, mfcc]")
        parser.add_argument("--cha", default=config["tc-resnet8"], type=list,
                            help="The channel of model layers (in list)")
        parser.add_argument("--scale", default=1, type=float, help="The scale of model channel")
        parser.add_argument("--freq", default=30, type=int, help="Model saving frequency (in step)")
        parser.add_argument("--save", default="stft", type=str, help="The save name")
        args = parser.parse_args()
        return args

    class_list_0 = ["yes", "no", "nine",
                    "three", "bed", "up",
                    "down", "wow", "happy",
                    "four", "stop", "go",
                    "dog", "cat", "five"]
    class_list_1 = ["tree", "one", "eight"]
    class_list_2 = ["left", "right", "bird"]
    class_list_3 = ["seven", "six", "two"]
    class_list_4 = ["marvin", "on", "sheila"]
    class_list_5 = ["off", "house", "zero"]

    config = {
        "tc-resnet8": [16, 24, 32, 48],
        "tc-resnet14": [16, 24, 24, 32, 32, 48, 48]}

    parameters = options(config)

    # initialize and setup Neptune
    if parameters.log:
        neptune.init('huangyz0918/kws')
        neptune.create_experiment(name='kws_model', tags=['pytorch', 'KWS', 'GSC', 'TC-ResNet', 'GEM'],
                                  params=vars(parameters))
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # build a multi-head setting for learning process.
    total_class_list = []
    learning_tasks = [class_list_0, class_list_1, class_list_2, class_list_3, class_list_4, class_list_5]
    for x in learning_tasks:
        total_class_list += x
    total_class_num = len([i for j, i in enumerate(total_class_list) if i not in total_class_list[:j]])
    class_list = []
    for task in learning_tasks:
        class_list += task
    class_encoding = {category: index for index, category in enumerate(class_list)}

    # load the model.
    if parameters.model == "stft":
        model = STFT_TCResnet(
            filter_length=256, hop_length=129, bins=129,
            channels=parameters.cha, channel_scale=parameters.scale, num_classes=total_class_num)
    elif parameters.model == "mfcc":
        model = MFCC_TCResnet(bins=40, channels=parameters.cha, channel_scale=parameters.scale,
                              num_classes=total_class_num)
    elif parameters.model == "stft-mlp":
        model = STFT_MLP(filter_length=256, hop_length=129, bins=129, num_classes=total_class_num)
    elif parameters.model == "rnn":
        model = MFCC_RNN(n_mfcc=12, sampling_rate=16000,
                         num_classes=total_class_num)  # sample length for the dataset is 16000.
    else:
        model = None

    # continuous learning by GEM.
    la_list = []
    acc_list = []
    bwt_list = []
    learned_class_list = []
    trainer = Trainer(parameters, model)
    gem_buffer = Buffer(int(parameters.bsize), trainer.device)
    # Allocate temporary synaptic memory.
    grad_dims = get_grad_dim(trainer.model)
    grads_cs = []
    grads_da = torch.zeros(np.sum(grad_dims)).to(trainer.device)
    start_time = time.time()
    # start continual learning process.
    for task_id, task_class in enumerate(learning_tasks):
        optimizer = torch.optim.SGD(model.parameters(), lr=parameters.lr, momentum=0.9)
        print(">>>   Learned Class: ", learned_class_list, " To Learn: ", task_class)
        learned_class_list += task_class
        train_loader, test_loader = get_dataloader_keyword(parameters.dpath, task_class, class_encoding,
                                                           parameters.batch)
        # starting training.
        if parameters.log:
            trainer.gem_train(optimizer, train_loader, test_loader, gem_buffer, grad_dims,
                              grads_cs, grads_da, parameters.gamma, tag=f'{task_id}')
        else:
            trainer.gem_train(optimizer, train_loader, test_loader, gem_buffer, grad_dims,
                              grads_cs, grads_da, parameters.gamma)

        # update the GEM parameters.
        on_task_update(task_id, grads_cs, grad_dims, gem_buffer, trainer.device, train_loader)
        # start evaluating the CL on previous tasks.
        total_learned_acc = 0
        if parameters.ek:
            evaluate_list = class_list
        else:
            evaluate_list = learning_tasks
        for val_id, task in enumerate(evaluate_list):
            print(f">>>   Testing on task {val_id}, Keywords: {task}")
            _, val_loader = get_dataloader_keyword(parameters.dpath, task, class_encoding, parameters.batch)
            if parameters.log:
                log_data = Evaluator(trainer.model, tag=f't{task_id}v{val_id}').evaluate(val_loader)
            else:
                log_data = Evaluator(trainer.model).evaluate(val_loader)
            if parameters.log:
                neptune.log_metric(f'TASK-{task_id}-acc', log_data["test_accuracy"])
            if val_id <= task_id:
                total_learned_acc += log_data["test_accuracy"]
            if val_id == task_id:
                la_list.append(log_data["test_accuracy"])

        acc_list.append(total_learned_acc / (task_id + 1))
        print(f'ACC on TASK {task_id}: {total_learned_acc / (task_id + 1)}')
        if task_id > 0:
            bwt_list.append(np.mean([acc_list[i + 1] - acc_list[i] for i in range(len(acc_list) - 1)]))

    duration = time.time() - start_time
    print(f'Total time {duration}, Avg: {duration / len(learning_tasks)}s')
    print(f'ACC: {np.mean(acc_list)}, std: {np.std(acc_list)}')
    print(f'LA: {np.mean(la_list)}, std: {np.std(la_list)}')
    print(f'BWT: {np.mean(bwt_list)}, std: {np.std(bwt_list)}')
    print(f'Parameter: {parameter_number(trainer.model) / 1024} K')


import json
import sys
from collections import OrderedDict
from typing import List, Optional

#pylint:disable=import-error
from mdutils import MdUtils
import argparse

from tests.torch.test_sota_checkpoints import DIFF_FP32_MAX_GLOBAL
from tests.torch.test_sota_checkpoints import DIFF_FP32_MIN_GLOBAL

BASE_PYTORCH_CHECKPOINT_URL = 'https://storage.openvinotoolkit.org/repositories/nncf/models/v1.7.0/'

SAMPLE_TYPE_TO_SAMPLE_DISPLAY_NAME_DICT = {
    'classification': 'Classification',
    'semantic_segmentation': 'Semantic segmentation',
    'object_detection': 'Object detection'
}

def get_fp32_and_compressed_metrics(model_name_to_metric_dict,
                                    model_name,
                                    reference,
                                    target):
    if reference is None:
        fp32_ref_metric = float(target)
        compressed_metric = None
    else:
        fp32_ref_metric = float(model_name_to_metric_dict[reference])
        compressed_metric = float(model_name_to_metric_dict[model_name])
    return fp32_ref_metric, compressed_metric

# Not perfect - better switch to building a complete table and then excluding
# columns, or use a better table manager in the future
def get_header_row(table_format: str, sample_type: str):
    assert table_format in ['overview', 'per_sample'], "Unsupported table format!"

    header = ["PyTorch Model"] if table_format == 'overview' else ["Model"]
    header.append('Compression algorithm')
    header.append('Dataset')
    if sample_type in ['classification', 'semantic_segmentation']:
        header.append('Accuracy (Drop) %')
    elif sample_type in 'object_detection':
        header.append('mAP (drop) %')
    else:
        raise RuntimeError(f'{sample_type} sample type is not supported!')

    if table_format == 'per_sample':
        header.append("NNCF config file")
        header.append("PyTorch checkpoint")
    return header

def build_per_model_row(table_format:str,
                        model_display_name: str,
                        compression_algo_display_name: str,
                        dataset_display_name: str,
                        fp32_metric: float,
                        compressed_metric: float,
                        config_path: str,
                        checkpoint_url: Optional[str]):
    row = [model_display_name, compression_algo_display_name, dataset_display_name]
    if compression_algo_display_name == 'None':
        row.append(f'{fp32_metric:.2f}')
    else:
        drop = fp32_metric - compressed_metric
        row.append(f'{fp32_metric:.2f}({drop:.2f})')
    if table_format == 'per_sample':
        local_config_path = '/'.join(config_path.split('/')[3:])
        config_name = local_config_path.split('/')[-1]
        row.append('[{}]({})'.format(config_name, local_config_path))
        if checkpoint_url is not None:
            row.append('[Link]({})'.format(checkpoint_url))
        else:
            row.append('-')
    return row

def get_results_table_rows(per_sample_config_dict,
                           model_name_to_metric_dict,
                           sample_type,
                           table_format: str) -> List[List[str]]:
    rows = []
    assert table_format in ['overview', 'per_sample'], "Unsupported table format!"

    header = get_header_row(table_format=table_format, sample_type=sample_type)
    rows.append(header)

    for data_name_ in per_sample_config_dict:
        dataset_name = get_display_dataset_name(data_name_)
        model_dicts = per_sample_config_dict[data_name_]
        for model_name in model_dicts:
            conf_file = model_dicts[model_name].get('config', {})
            reference = None
            target = None
            if model_dicts[model_name].get('reference', {}):
                reference = model_dicts[model_name].get('reference', {})
            if model_dicts[model_name].get('target', {}):
                target = model_dicts[model_name]['target']
            if model_dicts[model_name].get('resume', {}):
                resume = model_dicts[model_name].get('resume', {})
            else:
                resume = None
            model_display_name = model_dicts[model_name].get('model_description')

            if model_dicts[model_name].get('compression_description') is not None:
                compression = model_dicts[model_name].get('compression_description')
            else:
                compression = 'None'

            checkpoint_link = (BASE_PYTORCH_CHECKPOINT_URL + resume) if resume is not None else None

            fp32_ref_metric, compressed_metric = get_fp32_and_compressed_metrics(model_name_to_metric_dict,
                                                                                model_name,
                                                                                reference,
                                                                                target)
            if table_format == 'overview' and compression == 'None':
                continue  # The overview already has baseline results as a separate column
            rows.append(build_per_model_row(table_format,
                                            model_display_name,
                                            compression,
                                            dataset_name,
                                            fp32_ref_metric,
                                            compressed_metric,
                                            conf_file,
                                            checkpoint_link))

    return rows

def update_target_metrics_and_thresholds(config_dict: dict, model_name_to_metric_dict):
    for sample_name in config_dict:
        for dataset_name in config_dict[sample_name]:
            dataset_dict = config_dict[sample_name][dataset_name]
            for model_name in dataset_dict:
                model_dict = config_dict[sample_name][dataset_name][model_name]
                model_dict["target"] = model_name_to_metric_dict[model_name]
                if "reference" not in model_dict:
                    continue
                ref_model_name = model_dict["reference"]
                if ref_model_name not in model_name_to_metric_dict:
                    continue

                ref_model_dict = model_name_to_metric_dict[ref_model_name]

                if "diff_fp32_min" in model_dict:
                    diff_fp32_min_value = model_dict["diff_fp32_min"]
                    actual_diff_fp32 = model_name_to_metric_dict[model_name] - \
                                       model_name_to_metric_dict[ref_model_name]
                    if diff_fp32_min_value < DIFF_FP32_MIN_GLOBAL:  # model has special thresholds larger than global
                        if actual_diff_fp32 > diff_fp32_min_value:  # ...but it actually shows better results
                            if actual_diff_fp32 > DIFF_FP32_MIN_GLOBAL:
                                del model_dict["diff_fp32_min"]  # no "diff_fp32_min" means use global
                            else:
                                model_dict["diff_fp32_min"] = round(actual_diff_fp32 - 0.05)  # tighten the threshold
                    if actual_diff_fp32 < diff_fp32_min_value:
                        print(f"Warning: model {model_name} scores less ({actual_diff_fp32}) "
                              f"than the FP32 min threshold {diff_fp32_min_value}")

                if "diff_fp32_max" in model_dict:
                    diff_fp32_max_value = model_dict["diff_fp32_max"]
                    actual_diff_fp32 = model_name_to_metric_dict[model_name] - \
                                       model_name_to_metric_dict[ref_model_name]
                    if diff_fp32_max_value > DIFF_FP32_MAX_GLOBAL:  # model has special thresholds larger than global
                        if actual_diff_fp32 > diff_fp32_max_value:  # ...but it actually shows better results
                            if actual_diff_fp32 < DIFF_FP32_MAX_GLOBAL:
                                del model_dict["diff_fp32_max"]  # no "diff_fp32_max" means use global
                            else:
                                model_dict["diff_fp32_max"] = round(actual_diff_fp32 + 0.05, 1)  # tighten the threshold
                    if actual_diff_fp32 > diff_fp32_max_value:
                        print(f"Warning: model {model_name} scores more ({actual_diff_fp32}) "
                              f"than the FP32 max threshold {diff_fp32_max_value}")

def get_display_dataset_name(data_name):
    if data_name == 'imagenet':
        dataset_name = 'ImageNet'
    elif data_name == 'camvid':
        dataset_name = 'CamVid'
    elif data_name == 'VOCdevkit':
        dataset_name = 'VOC12+07 train, VOC07 eval'
    else:
        dataset_name = "Mapillary"
    return dataset_name

def delete_four_head_lines(fname):
    with open(fname) as input_data:
        lines = input_data.readlines()
    with open(fname, 'w') as out:
        out.writelines(lines[4:])

def write_table_to_md_file(md_file: MdUtils, table_rows_: List[List[str]]):
    flat_rows = []
    for row in table_rows_:
        flat_rows += row
    md_file.new_table(columns=len(table_rows_[0]), rows=len(table_rows_), text=flat_rows, text_align='center')

def header_name_to_link(header_name_: str):
    link = header_name_.lower().replace(' ', '-')
    link = '#' + link
    return link

def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('--results', '-r', help='A metrics.json file from a latest checkpoint evaluation run')
    parser.add_argument('--config', '-c',
                        help='A .json file with definitions of tested checkpoints (sota_checkpoints_eval.json)')
    parser.add_argument('--output', '-o',
                        help="If specified, will output a config file specified in -c with target metric values updated "
                             "to what was measured in the metrics.json file supplied via -r")
    args = parser.parse_args(args=argv)
    results = args.results
    config = args.config
    output = args.output

    measured_metrics = json.load(open(results, 'r'))
    sota_checkpoints_eval = json.load(open(config), object_pairs_hook=OrderedDict)

    # Output tables for per-sample README files
    for sample_type in sota_checkpoints_eval:
        table_rows = get_results_table_rows(sota_checkpoints_eval[sample_type],
                                            measured_metrics,
                                            sample_type,
                                            table_format='per_sample')
        file_name = 'results_{}.md'.format(sample_type)
        mdfile = MdUtils(file_name=file_name)
        write_table_to_md_file(mdfile, table_rows)
        mdfile.create_md_file()
        # Somehow the MDUtils outputs 4 empty lines prior to the actual table in the target file.
        delete_four_head_lines(file_name)

    # Output the overview table for the top-level README file
    overview_file_name = 'results_overview.md'
    mdfile = MdUtils(file_name=overview_file_name)

    # Compose a mini-TOC
    mdfile.new_line('### PyTorch models')
    mdfile.new_line()
    for sample_type in sota_checkpoints_eval:
        mdfile.new_line('<a name = "pytorch_object_detection" > </a>')
        mdfile.new_line()
        mdfile.new_line(f'#### {SAMPLE_TYPE_TO_SAMPLE_DISPLAY_NAME_DICT[sample_type]}')
        mdfile.new_line()

        table_rows = get_results_table_rows(sota_checkpoints_eval[sample_type],
                                            measured_metrics,
                                            sample_type,
                                            table_format='overview')
        write_table_to_md_file(mdfile, table_rows)
    mdfile.create_md_file()
    delete_four_head_lines(overview_file_name)

    if args.output is not None:
        update_target_metrics_and_thresholds(sota_checkpoints_eval, measured_metrics)
        with open(output, "w") as write_file:
            json.dump(sota_checkpoints_eval, write_file, indent=4)

if __name__ == '__main__':
    main(sys.argv[1:])


"""An entrypoint into the lagrad-opt compiler."""

import os.path as osp
import pathlib
import subprocess
from typing import List, Literal

LLVM_12_BIN = osp.join(osp.expanduser("~"), ".local", "LLVM12", "bin")
OPT_12 = osp.join(LLVM_12_BIN, "opt")
CLANG_12 = osp.join(LLVM_12_BIN, "clang")
LLC_12 = osp.join(LLVM_12_BIN, "llc")
BIN = osp.join(osp.dirname(__file__), "..", "..", "build", "bin")
MLIR_FILES = osp.join(osp.dirname(__file__), "..", "Standalone")
TENSOR_PREPROCESS = ["-canonicalize", "-convert-elementwise-to-linalg"]
BUFFERIZE = [
    "-tensor-constant-bufferize",
    "-std-bufferize",
    "-tensor-bufferize",
    "-standalone-bufferize",
    "-linalg-bufferize",
    "-scf-bufferize",
    "-func-bufferize",
    "-finalizing-bufferize",
]
LOWERING = [
    "-convert-linalg-to-loops",
    "-lower-affine",
    "-convert-scf-to-std",
    "-convert-memref-to-llvm",
    "-convert-math-to-llvm",
    "-convert-math-to-libm",
    "-convert-linalg-to-llvm",
    "-convert-std-to-llvm",
    "-reconcile-unrealized-casts",
    "-llvm-legalize-for-export",
]
LOWERING_ENZYME = [
    "-convert-linalg-to-loops",
    "-convert-scf-to-std",
    "-convert-memref-to-llvm",
    "-convert-math-to-llvm",
    "-convert-standalone-to-llvm",
    "-convert-linalg-to-llvm",
    "-convert-std-to-llvm",
    "-reconcile-unrealized-casts",
    "-convert-static-allocs",
    "-llvm-legalize-for-export",
]
ENZYME_DYLIB = pathlib.Path.home() / ".local" / "LLVM12" / "lib" / "LLVMEnzyme-12.dylib"
LIB = osp.expanduser(osp.join("~", ".local", "lib"))
LIBS = osp.join(LIB, "libmlir_runner_utils.dylib")

# Can't run tests in parallel
TMP_DIR = osp.join(osp.dirname(__file__), "tmp")

def run_opt(contents: bytes, args: List[str]) -> bytes:
    try:
        opt_p = subprocess.run(
            [f"{BIN}/lagrad-opt"] + args,
            input=contents,
            capture_output=True,
            check=True,
        )
    except subprocess.CalledProcessError as e:
        raise Exception(e.stderr.decode("utf-8"))
    return opt_p.stdout

def lower_to_llvm_dialect(contents: bytes, take_grads=False) -> bytes:
    return run_opt(
        contents,
        (["-take-grads"] if take_grads else [])
        + TENSOR_PREPROCESS
        + BUFFERIZE
        + (LOWERING if take_grads else LOWERING_ENZYME),
    )

def lower_to_llvm(llvm_dialect: bytes) -> bytes:
    translate_p = subprocess.run(
        ["mlir-translate", "-mlir-to-llvmir"],
        input=llvm_dialect,
        capture_output=True,
        check=True,
    )
    return translate_p.stdout

def run_enzyme(llvm_ir: bytes, optimize=True):
    # opt "$LLVM_FILE" -load $ENZYME_DYLIB -enzyme -o "$ENZYME_OUTPUT" -S
    # try:
    #     first_opt = subprocess.run(
    #         [
    #             CLANG_12,
    #             "-S",
    #             "-emit-llvm",
    #             "-xc",
    #             "-",
    #             "-o",
    #             "/dev/stdout",
    #             "-O2",
    #             "-fno-vectorize",
    #             "-fno-slp-vectorize",
    #             "-fno-unroll-loops",
    #         ],
    #         input=llvm_ir,
    #         capture_output=True,
    #         check=True,
    #     )
    # except subprocess.CalledProcessError as e:
    #     raise Exception(e.stderr.decode("utf-8"))
    try:
        enzyme_p = subprocess.run(
            [OPT_12, "-load", ENZYME_DYLIB, "-enzyme", "-S"],
            input=llvm_ir,
            capture_output=True,
            check=True,
        )
    except subprocess.CalledProcessError as e:
        raise Exception(e.stderr.decode("utf-8"))
    if optimize:
        opt_p = subprocess.run(
            [OPT_12, "-O3", "-S"],
            input=enzyme_p.stdout,
            capture_output=True,
            check=True,
        )
        return opt_p.stdout
    return enzyme_p.stdout

def jit_llvm(llvm_ir: bytes):
    try:
        jit = subprocess.run(
            ["lli", "-load", LIBS], input=llvm_ir, check=True, capture_output=True
        )
        return jit.stdout
    except subprocess.CalledProcessError as e:
        print(e.stdout.decode("utf-8"))
        print(e.stderr.decode("utf-8"))
        raise Exception(e.stdout.decode("utf-8") + e.stderr.decode("utf-8"))

def compile_pipeline(filename, mode: Literal["enzyme", "grad"] = "enzyme"):
    if mode not in ["enzyme", "grad"]:
        raise ValueError("'mode' must be one of 'enzyme', 'grad'")

    with open(filename, "rb") as f:
        contents = f.read()

    dialect_ir = lower_to_llvm_dialect(contents, take_grads=(mode == "grad"))
    llvm_ir = lower_to_llvm(dialect_ir)
    if mode == "enzyme":
        llvm_ir = run_enzyme(llvm_ir, optimize=True)
        return jit_llvm(llvm_ir)

    object_file = osp.join(TMP_DIR, "app.o")
    with open(object_file, "wb") as ofile:
        subprocess.run(
            ["llc", "-filetype=obj"], input=llvm_ir, stdout=ofile, check=True
        )

    exe_file = osp.join(TMP_DIR, "a.out")
    subprocess.run(["clang", object_file, LIBS, "-o", exe_file], check=True)

    exe_p = subprocess.run(
        [exe_file], env={"DYLD_LIBRARY_PATH": LIB}, capture_output=True
    )
    return exe_p.stdout

def jit_file(filename: str, debug=False) -> str:
    with open(filename, "rb") as f:
        contents = f.read()
    return jit(contents, debug=debug)

def jit(contents: bytes, args=None, debug=False, linalg_generalize=False) -> str:
    """
    Execute the given MLIR file through MLIR's JIT.
    """
    if debug:
        print(
            "\n",
            run_opt(
                contents,
                ["-linalg-generalize-named-ops", "-take-grads", "-canonicalize"],
            ).decode("utf-8"),
        )
    dialect_ir = run_opt(
        contents,
        args
        or (["-linalg-generalize-named-ops"] if linalg_generalize else [])
        + [
            "-loop-invariant-code-motion",
            "-take-grads",
            "-canonicalize",
            "-linalg-canonicalize",
            # "-standalone-dce",
            # "-cse",
            "-convert-elementwise-to-linalg",
        ]
        + BUFFERIZE
        + LOWERING,
    )
    try:
        runner = subprocess.run(
            ["mlir-cpu-runner", "-entry-point-result=void", f"-shared-libs={LIBS}"],
            input=dialect_ir,
            check=True,
            capture_output=True,
        )
    except subprocess.CalledProcessError as e:
        raise Exception(e.stderr.decode("utf-8"))
    return runner.stdout.decode("utf-8")


from os import path

lang = 'tr'
file = open( "script_source.txt", "r")
lines = file.readlines()
file.close()

formated_lines = []

for linenumber in range (0,len(lines)):
	split_line = lines[linenumber].split()
	word = split_line[1]
	print(word)
	#formated_line = split_line[1] + ' - ' + split_line[2] + ' - no hint\n'
	formated_lines.append(word)

with open('script_source_output.txt', 'w') as f:
    for item in formated_lines:
        f.write("%s" % item+'\n')


#!/usr/bin/env python
import argparse
import sys

def unique(path_to_file, ignore_case):
    """
    Report or omit repeated lines
    @param path_to_file: list, path to file
    @return: None, sys.stdout write unique lines
    """
    # use dict.fromkeys to preserve the order
    if ignore_case:
        all_lines = list(dict.fromkeys([line.strip().lower() for file in path_to_file for line in file]))
    else:
        all_lines = list(dict.fromkeys([line.strip() for file in path_to_file for line in file]))
    for ls in all_lines:
        sys.stdout.write(ls + "\n")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="report or omit repeated lines")
    parser.add_argument('-i', '--ignore_case', action='store_true',
                        help="ignore differences in case when comparing")
    if sys.stdin.isatty():
        parser.add_argument('path_to_file', help="path to file", nargs="+",
                            type=argparse.FileType('r', encoding='UTF-8'), default=sys.stdin)
        args = parser.parse_args()
        unique(path_to_file=args.path_to_file, ignore_case=args.ignore_case)
    else:
        parser.add_argument('--path_to_file', help="path to file",
                            type=argparse.FileType('r', encoding='UTF-8'), default=sys.stdin)
        args = parser.parse_args()
        unique(path_to_file=[args.path_to_file], ignore_case=args.ignore_case)


#!/usr/local/bin/python3

# This script creates file 'A1_SourceCode.tex' in current directory (for thesis writing)

import os

classes = os.listdir('../src/classes')
classes = [x for x in classes if x.endswith('.m')]
classes.sort()

funns = os.listdir('../src/functions') + os.listdir('../src/functions/devt')
funns = [x for x in funns if x.endswith('.m')]
funns = funns + ['getSimvmaPath.m', 'initialize.m', 'sl_customization.m']
funns.sort()

content = """\chapter{Source Code}
\label{chapter:appendix-source-code}

This appendix presents the MATLAB implementation of various classes and functions used in the project.
"""

content += "\n\n\section{Class Definitions}\n\label{section:class-definitions}\n"

for i, x in enumerate(classes):
    # print(x)
    # if i!=0:
    #     content += f"\n\\newpage"
    x = x[:-2] # removing trailing .m
    x_latex = x.replace('_', '\_')
    content += f"\n\lstinputlisting[caption={{{x_latex}.m class definition}}, captionpos=t,label={{lst:code-{x}}}]{{Codes/classes/{x}.m}}"

content += "\n\n\\newpage\n\section{Function Definitions}\n\label{section:function-definitions}\n"

for i, x in enumerate(funns):
    # print(x)
    # if i!=0:
    #     content += f"\n\\newpage"
    x = x[:-2] # removing trailing .m
    x_latex = x.replace('_', '\_')
    content += f"\n\lstinputlisting[caption={{{x_latex}.m function definition}}, captionpos=t,label={{lst:code-{x}}}]{{Codes/functions/{x}.m}}"

with open('A1_SourceCode.tex', 'w') as file:
    file.write(content)


import hashlib

def file_reader(file_obj, size = 1024):
	""" This function will read 1024 byte of data or read complete data of a file depending upon the
		meta condition. """
	while True:
		data = file_obj.read(size)
		if not data:
			return
		yield data

def generate_hash(file, meta, hash = hashlib.sha1):
	"""It will generate the hash value of the file data. After opening in binary mode we will evaluate the
	meta condition if result true we will return hash value of 1024 byte data or else will return the
	complete hash value of the file data.
	"""
	hash_obj = hash()
	file_obj = open(file, 'rb')
	if meta:
		hash_obj.update(file_obj.read(1024))
	else:
		for data in file_reader(file_obj):
			hash_obj.update(data)
	hashed = hash_obj.digest()
	file_obj.close()
	return hashed


import os
from datetime import timedelta

from airflow import DAG
from airflow.models import Variable
from airflow.models.baseoperator import chain
from airflow.operators.bash import BashOperator
from airflow.operators.dummy import DummyOperator
from airflow.providers.amazon.aws.operators.athena import AWSAthenaOperator
from airflow.providers.amazon.aws.operators.glue_crawler import AwsGlueCrawlerOperator
from airflow.utils.dates import days_ago

DAG_ID = os.path.basename(__file__).replace(".py", "")

S3_BUCKET = Variable.get("data_lake_bucket")
S3_UNLOAD_PATH = f"s3://{S3_BUCKET}/redshift/sales/"
GLUE_CRAWLER_IAM_ROLE = Variable.get("glue_crawler_iam_role")
GLUE_DATABASE = "tickit_redshift_demo"

DEFAULT_ARGS = {
    "owner": "garystafford",
    "depends_on_past": False,
    "retries": 1,
    "email_on_failure": False,
    "email_on_retry": False,
    "postgres_conn_id": "amazon_redshift_dev",
}

with DAG(
    dag_id=DAG_ID,
    description="Catalog unloaded data with Glue and query with Athena",
    default_args=DEFAULT_ARGS,
    dagrun_timeout=timedelta(minutes=15),
    start_date=days_ago(1),
    schedule_interval=None,
    tags=["redshift demo"],
) as dag:
    begin = DummyOperator(task_id="begin")

    end = DummyOperator(task_id="end")

    delete_glue_catalog = BashOperator(
        task_id="delete_demo_catalog",
        bash_command=f'aws glue delete-database --name {GLUE_DATABASE} || echo "Database tickit_redshift_demo not found."',
    )

    create_glue_catalog = BashOperator(
        task_id="create_demo_catalog",
        bash_command="""aws glue create-database --database-input \
            '{"Name": "tickit_redshift_demo", "Description": "TICKIT sales data unloaded from Redshift"}'""",
    )

    crawl_unloaded_data = AwsGlueCrawlerOperator(
        task_id="crawl_unloaded_data",
        config={
            "Name": "tickit-sales-unloaded",
            "Role": GLUE_CRAWLER_IAM_ROLE,
            "DatabaseName": GLUE_DATABASE,
            "Description": "Crawl TICKIT sales data unloaded from Redshift",
            "Targets": {"S3Targets": [{"Path": S3_UNLOAD_PATH}]},
        },
    )

    list_glue_tables = BashOperator(
        task_id="list_glue_tables",
        bash_command=f"""aws glue get-tables --database-name {GLUE_DATABASE} \
                          --query 'TableList[].Name' \
                          --output table""",
    )

    athena_query_glue = AWSAthenaOperator(
        task_id="athena_query_glue",
        query="""SELECT *
            FROM tickit_redshift_demo.sales
            WHERE catgroup = 'Shows' AND catname = 'Opera'
            ORDER BY saletime
            LIMIT 10;""",
        output_location="s3://{{ var.value.athena_query_results }}/",
        database=GLUE_DATABASE,
    )

    chain(
        begin,
        delete_glue_catalog,
        create_glue_catalog,
        crawl_unloaded_data,
        list_glue_tables,
        athena_query_glue,
        end,
    )


class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        if len(s) > len(t): return False
        if len(s) == 0 or len(t) == 0: return True
        idx=0
        for i in range(0, len(t)):
            if t[i] == s[idx]:
                idx += 1
                if idx == len(s):
                    return True
        return False


""" Ejercicio 28.- Programa un vector aleatorio e imprime su inverso. """

import random

n = int(input("Ingrese el tamano del vector: "))
vector = []

for i in range(n):
    numero = random.randrange(-50, 50)
    vector.append(numero)
    invertido = vector[::-1]

print(vector)
print(invertido)


import math
import os

import numpy as np

from model import Model
from svg import load_template, render_plots_over_range, render_to

A = np.array([[1, 0, 0, 0],
              [1, 1, 1, 0],
              [0, 1, 0, 1],
              [0, 0, 1, 1]])
F = 2
D = 3
RHO = 1
THETA = 1

MAX_VAL = math.log(2, 3) * 1.1
STEPS = 1000

def animate(order):
    tree, doc = load_template("/home/luca/slides/skeleton.svg",
                              "/home/luca/slides/style.css")
    out_dir = ("/home/luca/slides/videos/online_%s"
               % "_".join(str(e) for e in order))
    os.makedirs(out_dir)
    active = np.array(4 * [False])
    m = Model(A, np.array(4 * [0.0]), F, RHO, THETA)
    for stage_idx, e in enumerate(order, start=1):
        e -= 1
        active[e] = True
        os.makedirs(os.path.join(out_dir, "stage_%d" % stage_idx))
        for i in range(201):
            m.x[e] = i * math.log(2, 3) / 200
            render_plots_over_range(doc, active, m, MAX_VAL, STEPS)
            render_to(tree, os.path.join(out_dir,
                                         "stage_%d" % stage_idx,
                                         "frame_%04d.png" % i))
            if m.dual_cst(e) >= 1:
                break

animate([1, 2, 3, 4])


from math import inf
import heapq

def MainLoop(graph):
    n = len(graph)
    visited = set()
    shortest_distances = [0]+[inf]*(n-1)
    min_dist = [(0,1)]      # [(dist, node)]

    while len(visited) < n:
        current_dist, current_node = heapq.heappop(min_dist)

        if current_node not in visited:
            visited.add(current_node)

            for pair in graph[current_node]:
                connection = pair[0]
                if connection not in visited:
                    dist = current_dist + pair[1]
                    if dist < shortest_distances[connection-1]:
                        shortest_distances[connection-1] = dist
                        heapq.heappush(min_dist, (dist, connection))

    return shortest_distances

graph = {1: [[2, 1], [3,4]], 2: [[3, 2], [4, 6]], 3: [[4, 3]]}

# graph in the form {t1: [[h1, l1], [h2, l2]], t2: [[h1, l1], [h2, l2]]}
with open("DK.txt") as a_file:
    for line in a_file:
        lis = line.strip().split('\t')

        v1 = int(lis[0])
        v2s = []

        for i in range(1, len(lis)):
            [v2, length] = lis[i].split(",")
            v2s.append([int(v2), int(length)])

        graph[v1] = v2s

shortest_distances = MainLoop(graph)

vertices = [7,37,59,82,99,115,133,165,188,197]

for vertex in vertices:
    print(shortest_distances[vertex-1], end=",")


import sys

# 특정 점수에 도달했을 때 사용할 수 있는 기술'럭키스트레이트'
# 현재 캐릭터 점수 = N
# 자릿수 기준으로 점수 N을 반으로 나누어 왼쪽 부분의 각 자릿 수의 합과 오른쪽 부분의 각 자릿수의 합을 더한 값이 동일한 상황
# 123402
# 1+2+3 = 6
# 4+0+2 = 6

N = str(sys.stdin.readline().rstrip())

if sum(list(map(int,N[:len(N)//2]))) == sum(map(int,list(N[len(N)//2:]))):
    print("LUCKY")
else:
    print("READY")


import torch
import clip
from PIL import Image

classes = ['a red packet', 'a dog', 'a cat']

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

image = preprocess(Image.open("红包1.jpeg")).unsqueeze(0).to(device)
text = clip.tokenize([( f"a photo of {c}.") for c in classes]).to(device)

with torch.no_grad():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)

    similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1)  # 对图像描述和图像特征
    values, indices = similarity[0].topk(3)
    #logits_per_image, logits_per_text = model(image, text)
    #probs = logits_per_image.softmax(dim=-1).cpu().numpy()

#print("Label probs:", probs)  # prints: [[0.9927937  0.00421068 0.00299572]]
print("\nTop predictions:\n")
for value, index in zip(values, indices):
    print(f"{classes[index]:>16s}: {100 * value.item():.2f}%")


import numpy as np
import pandas as pd

from .datasets.datasets import read_cache, read_dataset, write_cache
from .evaluation.evaluation import evaluate, get_dist_mat, plot_conf_matrix
from .interface.interface import create_interface
from .normalization.normalization import feature_normalization

def run():
    datasets = ['labeled']
    cache = input('Read from cache (yes/no)?\n')
    if cache.lower().startswith('y'):
        features, paths, norm_info = read_cache(datasets)
        print('Read from cache.')
    else:
        print('Calculating feature values.')
        features, paths, norm_info = initialize(datasets)
        cache = input('Write to cache (yes/no)?\n')
        if cache.lower().startswith('y'):
            write_cache(features, paths, norm_info, datasets)
            print('Wrote to cache.')

    eval = input('Do you want to evaluate the querying (yes/no)?\n')
    if eval.lower().startswith('y'):
        cache = input('Read distance matrix from cache (yes/no)?\n')
        c = cache.lower().startswith('yes')
        distance_matrix = get_dist_mat(features['labeled'], c)
        plot_conf_matrix(features, distance_matrix)
        evaluate(features, paths, norm_info, distance_matrix)
    create_interface(features, paths, norm_info)

def initialize(datasets=['princeton', 'labeled']):
    """
    Reads the shapes, creates a feature dataset,
    and returns the feature datasets, paths for all shapes,
    and necessary information to normalize features of other shapes.
    """
    f = {}
    p = {}
    n = {}
    for dataset in datasets:
        features, paths = read_dataset(dataset=dataset,
                                       n_meshes=None, features=True)
        norm_infos = {}

        for name, series in features.iteritems():
            if not isinstance(series[0], tuple):
                norm_infos[name] = {
                    'mean': np.mean(series),
                    'sd': np.std(series),
                    'min': min(series),
                    'max': max(series)
                }
                features[name] = list(feature_normalization(series))
            else:
                features[name] = list(feature_normalization(series))

        f[dataset] = features
        p[dataset] = paths
        n[dataset] = pd.DataFrame(norm_infos)
    return pd.Series(f), pd.Series(p), pd.Series(n)


import numpy as np
from scipy import sparse, ndimage as ndi
from pyamg import ruge_stuben_solver
from skimage import img_as_float
from scipy.sparse.linalg import cg

def _make_graph_edges_3d(n_x, n_y, n_z):
	vertices = np.arange(n_x * n_y * n_z).reshape((n_x, n_y, n_z))
	edges_deep = np.vstack((vertices[..., :-1].ravel(), vertices[..., 1:].ravel()))
	edges_right = np.vstack((vertices[:, :-1].ravel(), vertices[:, 1:].ravel()))
	edges_down = np.vstack((vertices[:-1].ravel(), vertices[1:].ravel()))
	edges = np.hstack((edges_deep, edges_right, edges_down))
	return edges

def _compute_weights_3d(data, spacing, beta, eps):
	gradients = np.concatenate(
		[np.diff(data[..., 0], axis=ax).ravel() / spacing[ax] for ax in [2, 1, 0] if data.shape[ax] > 1], axis=0) ** 2
	for channel in range(1, data.shape[-1]):
		gradients += np.concatenate([np.diff(data[..., channel], axis=ax).ravel() / spacing[ax] for ax in [2, 1, 0] if data.shape[ax] > 1], axis=0) ** 2
	scale_factor = -beta / (10 * data.std())
	weights = np.exp(scale_factor * gradients)
	weights += eps
	#weights=weights.astype(np.float32) # Coversion did cause NaN's
	return -weights

def _build_laplacian(data, spacing, mask, beta):
	l_x, l_y, l_z = data.shape[:3]
	edges = _make_graph_edges_3d(l_x, l_y, l_z)
	weights = _compute_weights_3d(data, spacing, beta=beta, eps=1.e-8)
	# Build the sparse linear system
	pixel_nb = edges.shape[1]
	i_indices = edges.ravel()
	j_indices = edges[::-1].ravel()
	data = np.hstack((weights, weights))
	lap = sparse.coo_matrix((data, (i_indices, j_indices)), shape=(pixel_nb, pixel_nb))
	del data
	lap.setdiag(-np.ravel(lap.sum(axis=0)))
	return lap.tocsr()

def _build_linear_system(data, spacing, labels, nlabels, mask, beta):
	"""
	Build the matrix A and rhs B of the linear system to solve.
	A and B are two block of the laplacian of the image graph.
	"""
	if mask is None:
		labels = labels.ravel()
	else:
		labels = labels[mask]
	indices = np.arange(labels.size)
	seeds_mask = labels > 0
	unlabeled_indices = indices[~seeds_mask]
	seeds_indices = indices[seeds_mask]
	lap_sparse = _build_laplacian(data, spacing, mask=mask, beta=beta)
	rows = lap_sparse[unlabeled_indices, :]
	lap_sparse = rows[:, unlabeled_indices]
	B = -rows[:, seeds_indices]
	seeds = labels[seeds_mask]
	seeds_mask = sparse.csc_matrix(np.hstack([np.atleast_2d(seeds == lab).T for lab in range(1, nlabels + 1)]))
	rhs = B.dot(seeds_mask)
	return lap_sparse, rhs

def _solve_linear_system(lap_sparse, B, tol):
	lap_sparse = lap_sparse.tocsr()
	ml = ruge_stuben_solver(lap_sparse)
	M = ml.aspreconditioner(cycle='V')
	cg_out = [cg(lap_sparse, B[:, i].toarray(), tol=tol, M=M, maxiter=30) for i in range(B.shape[1])]
	X = np.asarray([x for x, _ in cg_out])
	return X

def _preprocess(labels):
	label_values, inv_idx = np.unique(labels, return_inverse=True)
	# If some labeled pixels are isolated inside pruned zones, prune them
	# as well and keep the labels for the final output
	null_mask = labels == 0
	pos_mask = labels > 0
	mask = labels >= 0
	fill = ndi.binary_propagation(null_mask, mask=mask)
	isolated = np.logical_and(pos_mask, np.logical_not(fill))
	pos_mask[isolated] = False
	# If the array has pruned zones, be sure that no isolated pixels
	# exist between pruned zones (they could not be determined)
	if label_values[0] < 0 or np.any(isolated):
		isolated = np.logical_and(
			np.logical_not(ndi.binary_propagation(pos_mask, mask=mask)), null_mask)
		labels[isolated] = -1
		if np.all(isolated[null_mask]):
			return labels, None, None, None, None
		mask[isolated] = False
		mask = np.atleast_3d(mask)
	else:
		mask = None
	# Reorder label values to have consecutive integers (no gaps)
	zero_idx = np.searchsorted(label_values, 0)
	labels = np.atleast_3d(inv_idx.reshape(labels.shape) - zero_idx)
	nlabels = label_values[zero_idx + 1:].shape[0]
	inds_isolated_seeds = np.nonzero(isolated)
	isolated_values = labels[inds_isolated_seeds]
	return labels, nlabels, mask, inds_isolated_seeds, isolated_values

def random_walker(data, labels, beta=130, tol=1.e-3, copy=False):
	spacing = np.ones(3)
	if data.ndim not in (2, 3):
		raise ValueError('For non-multichannel input, data must be of dimension 2 or 3.')
	if data.shape != labels.shape:
		raise ValueError('Incompatible data and labels shapes.')
	data = np.atleast_3d(img_as_float(data))[..., np.newaxis]
	labels_shape = labels.shape
	labels_dtype = labels.dtype
	if copy:
		labels = np.copy(labels)
	(labels, nlabels, mask, inds_isolated_seeds, isolated_values) = _preprocess(labels)
	# Build the linear system (lap_sparse, B)
	lap_sparse, B = _build_linear_system(data, spacing, labels, nlabels, mask, beta)

	# Solve the linear system lap_sparse X = B
	# where X[i, j] is the probability that a marker of label i arrives
	# first at pixel j by anisotropic diffusion.
	X = _solve_linear_system(lap_sparse, B, tol)
	# Build the output according to return_full_prob value
	# Put back labels of isolated seeds
	labels[inds_isolated_seeds] = isolated_values
	labels = labels.reshape(labels_shape)

	X = np.argmax(X, axis=0) + 1
	out = labels.astype(labels_dtype)
	out[labels == 0] = X
	return out


from argparse import ArgumentParser
from datetime import datetime
from distutils.util import strtobool

import numpy
from mpi4py import MPI

from taskmaster import work_delegation
from utils import get_nsims

try:
    import csiborgtools
except ModuleNotFoundError:
    import sys

    sys.path.append("../")
    import csiborgtools

def get_counts(nsim, bins, paths, parser_args):
    """
    Calculate and save the number of haloes in each mass bin.

    Parameters
    ----------
    nsim : int
        Simulation index.
    bins : 1-dimensional array
        Array of bin edges (in log10 mass).
    paths : csiborgtools.read.Paths
        Paths object.
    parser_args : argparse.Namespace
        Parsed command-line arguments.

    Returns
    -------
    None
    """
    simname = parser_args.simname
    paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
    bounds = {"dist": (0, parser_args.Rmax)}

    if simname == "csiborg":
        cat = csiborgtools.read.CSiBORGHaloCatalogue(
            nsim, paths, bounds=bounds, load_fitted=False, load_initial=False)
        logmass = numpy.log10(cat["fof_totpartmass"])
        counts = csiborgtools.number_counts(logmass, bins)
    elif simname == "quijote":
        cat0 = csiborgtools.read.QuijoteHaloCatalogue(
            nsim, paths, nsnap=4, load_fitted=False, load_initial=False)
        nmax = int(cat0.box.boxsize // (2 * parser_args.Rmax))**3
        counts = numpy.full((nmax, len(bins) - 1), numpy.nan,
                            dtype=numpy.float32)

        for nobs in range(nmax):
            cat = cat0.pick_fiducial_observer(nobs, rmax=parser_args.Rmax)
            logmass = numpy.log10(cat["group_mass"])
            counts[nobs, :] = csiborgtools.number_counts(logmass, bins)
    elif simname == "quijote_full":
        cat = csiborgtools.read.QuijoteHaloCatalogue(
            nsim, paths, nsnap=4, load_fitted=False, load_initial=False)
        logmass = numpy.log10(cat["group_mass"])
        counts = csiborgtools.number_counts(logmass, bins)
    else:
        raise ValueError(f"Unknown simulation name `{simname}`.")

    fout = paths.halo_counts(simname, nsim)
    if parser_args.verbose:
        print(f"{datetime.now()}: saving halo counts to `{fout}`.")
    numpy.savez(fout, counts=counts, bins=bins, rmax=parser_args.Rmax)

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--simname", type=str,
                        choices=["csiborg", "quijote", "quijote_full"],
                        help="Simulation name.")
    parser.add_argument("--nsims", type=int, nargs="+", default=None,
                        help="Indices of simulations to cross. If `-1` all .")
    parser.add_argument(
        "--Rmax", type=float, default=155,
        help="High-res region radius in Mpc / h. Ignored for `quijote_full`.")
    parser.add_argument("--lims", type=float, nargs="+", default=[11., 16.],
                        help="Mass limits in Msun / h.")
    parser.add_argument("--bw", type=float, default=0.2,
                        help="Bin width in dex.")
    parser.add_argument("--verbose", type=lambda x: bool(strtobool(x)),
                        default=False, help="Verbosity flag.")
    parser_args = parser.parse_args()

    paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
    nsims = get_nsims(parser_args, paths)

    if len(parser_args.lims) != 2:
        raise ValueError("Mass limits must be a pair of floats.")

    bins = numpy.arange(*parser_args.lims, parser_args.bw, dtype=numpy.float32)

    def do_work(nsim):
        get_counts(nsim, bins, paths, parser_args)

    work_delegation(do_work, nsims, MPI.COMM_WORLD)


"""Git utility"""
from __future__ import print_function
import os
import sys
from os.path import dirname, exists, split, basename
from fnmatch import fnmatch
from promus.command import exec_cmd, error
PC = sys.modules['promus.core']

def config(entry, val=None, global_setting=True):
    """Call `git config --global` when `global_setting` is set to
    True, otherwise call `git config` to set or get an entry. """
    cmd = 'git config '
    if global_setting:
        cmd += '--global '
    if val:
        exec_cmd('%s %s "%s"' % (cmd, entry, val))
    else:
        val, _, _ = exec_cmd('%s %s' % (cmd, entry))
    return val.strip()

def describe():
    "Return last tag, number of commits and sha. "
    out, _, status = exec_cmd('git describe --long')
    if status != 0:
        return None, 0, None
    out = out.split('-')
    return out[0], out[1], out[2][1:]

def repo_name(local=True):
    "Return the name of the repository. "
    if local:
        cmd = 'basename `git rev-parse --show-toplevel`'
    else:
        cmd = 'basename `pwd`'
    out, _, _ = exec_cmd(cmd)
    return PC.strip(out)

def local_path():
    "Return the path to directory containing the `.git` directory. "
    out, _, _ = exec_cmd('git rev-parse --show-toplevel')
    return PC.strip(out)

def remote_path():
    "Return the path of the remote repository. "
    out, _, _ = exec_cmd('git config --get remote.origin.url')
    return PC.strip(out)

def init(repo, directory=None):
    """Create a bare git repository and create the `post-receive` and
    `update` hook. You may have to modify these two hooks depending
    on how you want to treat the repository. """
    if not repo.endswith('.git'):
        repo += '.git'
    if directory is None:
        directory = '%s/git' % os.environ['HOME']
    PC.make_dir(directory)
    fullpath = '%s/%s' % (directory, repo)
    if os.path.exists(fullpath):
        error("INIT-ERROR>> Existing repository: '%s'\n" % fullpath)
    exec_cmd("git init --bare %s" % fullpath, True)
    hooks = ['post-receive', 'update']
    for hook in hooks:
        path = '%s/hooks' % fullpath
        make_hook(hook, path)
    sys.stdout.write("INIT>> '%s' was created...\n" % fullpath)

HOOK_TEMPLATE = '''#!/usr/bin/env python
"""{hook} hook generated on {date}"""
import promus.core as prc
import promus.hooks.{hookpy} as hook

if __name__ == "__main__":
    PRS = prc.Promus()
    hook.run(PRS)
    PRS.dismiss("{hook}>> done...", 0)

'''

def make_hook(hook, path):
    "Creates the specified hook. "
    hook_file = "%s/%s" % (path, hook)
    if exists(hook_file):
        cmd = "mv %s %s.%s" % (hook_file, hook_file, PC.date(True))
        exec_cmd(cmd, True)
    hookpy = hook.replace('-', '_')
    content = HOOK_TEMPLATE.format(hook=hook, hookpy=hookpy,
                                   date=PC.date())
    with open(hook_file, 'w') as hookfp:
        hookfp.write(content)
    exec_cmd('chmod +x %s' % hook_file, True)

def parse_dir(string):
    "Return two lists, one with directories and one with users. "
    tmp = string.split('|')
    return [PC.parse_list(tmp[0]), PC.parse_list(tmp[1])]

def parse_acl(aclstring):
    """Return acl dictionary. The format of the aclstring is as
    follows:

        admin: user1
        user: user2, user3, user4
        path: dir1, dir2 | !deny, user3, user4
        name: file1 | !allow, user2
        rsync: user2, user4

    Everyone will have access to the files in the repository. If more
    control is needed we can deny access to some paths by listing the
    paths. Some files are only accessible to the admin but the admin
    may provide access to other users by listing the path (relative
    to the git repository) and using the keyword !allow. The rsync
    keyword lists the users which will get synchronized files using
    rsync. That is, the user will not be able to modify the files.
    They will only get the lastest copy of them. """
    acl = dict()
    acl['admin'] = list()
    acl['user'] = list()
    acl['path'] = list()
    acl['name'] = list()
    acl['rsync'] = list()
    line_num = 0
    for line in aclstring.split('\n'):
        line_num += 1
        if line.strip() == '' or line[0] == '#':
            continue
        try:
            key, val = line.split(':')
            key = key.strip().lower()
        except ValueError:
            return "wrong number of ':' in line %d" % line_num
        if key in ['admin', 'user']:
            acl[key].extend(PC.parse_list(val))
        elif key in ['path', 'name']:
            try:
                acl[key].extend(parse_dir(val))
            except IndexError:
                return "'|' not found in line %d" % line_num
        elif key == 'rsync':
            acl[key].extend(PC.parse_list(val))
        elif line.strip() != '':
            return "wrong keyword in line %d" % line_num
    acl['user'] = list(set(acl['user']+acl['admin']))
    return acl

def check_acl(aclfile):
    "Attempts to read the acl file to see if it contains any errors. "
    try:
        aclfile = open(aclfile, 'r').read()
    except IOError:
        return "no such file: '%s'" % aclfile
    return parse_acl(aclfile)

def read_acl(git_dir=None):
    "Read acl from the git repository."
    if git_dir:
        cmd = 'cd %s; git show HEAD:.acl' % git_dir
    else:
        cmd = 'git show HEAD:.acl'
    aclfile, err, _ = exec_cmd(cmd, False)
    if err:
        return "while executing `git show HEAD:.acl`: %s" % err[:-1]
    return parse_acl(aclfile)

def parse_profile(profilestring):
    """Return profile dictionary. The format of the profilestring is
    as follows:

        email: user@domain.com
        notify: [all/false/track]
        track-files: pattern1, pattern2, ...

    The track-files keyword will only be relevant if notify is set to
    `track`. This tells promus to send a notification to the
    specified email address if one of the modified files matches a
    pattern. Only one keyword and options per line. You may use
    several track-files keywords. """
    profile = dict()
    profile['email'] = ''
    profile['notify'] = 'false'
    profile['track-files'] = list()
    line_num = 0
    for line in profilestring.split('\n'):
        line_num += 1
        if line.strip() == '' or line[0] == '#':
            continue
        try:
            key, val = line.split(':')
            key = key.strip().lower()
        except ValueError:
            return "wrong number of ':' in line %d" % line_num
        if key in 'email':
            profile[key] = val.strip()
        elif key in ['notify']:
            val = val.strip().lower()
            if val in ['all', 'false', 'track']:
                profile[key] = val
            else:
                return "Notify options allowed: all/false/track"
        elif key == 'track-files':
            profile[key].extend(PC.parse_list(val))
        elif line.strip() != '':
            return "wrong keyword in line %d" % line_num
    return profile

def check_profile(profile_file):
    "Attempts to read the acl file to see if it contains any errors. "
    try:
        profile = open(profile_file, 'r').read()
    except IOError:
        return "no such file: '%s'" % profile_file
    return parse_profile(profile)

def read_profile(user, git_dir=None):
    "Read profile from the git repository."
    if git_dir:
        cmd = 'cd %s; git show HEAD:.%s.profile' % (git_dir, user)
    else:
        cmd = 'git show HEAD:.%s.profile' % user
    profile, err, _ = exec_cmd(cmd, False)
    if err:
        return "while executing `git show HEAD:.%s.profile`: %s" % (user,
                                                                    err[:-1])
    return parse_profile(profile)

def file_in_path(file_name, paths):
    "Given a list of paths it checks if the file is in one of the paths."
    for path in paths:
        if file_name.startswith(path):
            return True
    return False

def has_access(user, users):
    """A list of users should look as follows: ['!deny', 'user1',
    'user2'] This means that user1 and user2 have deny access. If we
    have something longer such as ['!deny', 'user1', '!allow',
    'user2'] then user2 has access but user1 does not have access. If
    no keywords are found then it returns None if the user is in the
    list. """
    if user not in users:
        return None
    rusers = users[::-1]
    try:
        action = next(x[1] for x in enumerate(rusers) if x[1][0] == '!')
    except StopIteration:
        return None
    if action == '!allow':
        return True
    if action == '!deny':
        return False
    return False

def file_match(file_name, names):
    """Checks the name of the file matches anything in the list of
    names. """
    fname = basename(file_name)
    for name in names:
        if fnmatch(fname, name):
            return True
    return False

def clone(repo):
    "Clone a repository. "
    out, err, stat = exec_cmd("git clone %s" % repo)
    sys.stdout.write(out)
    if stat != 0:
        error(err)
    if repo[-1] == '/':
        tmp = split(repo[0:-1])
    else:
        tmp = split(repo)
    repo = tmp[1].split('.')[0]
    if not exists("%s/.acl" % repo):
        admin_setup(repo)
    else:
        user_setup(repo)
    if os.uname()[0] == 'Darwin':
        exec_cmd('open -a /Applications/GitHub.app "%s"' % repo, True)
    sys.stderr.write("CLONE>> Repository '%s' has been cloned ...\n" % repo)

def admin_setup(repo):
    "Set the acl list and create the hooks. "
    print("Setting up empty repository...")
    print("creating README.md")
    master = os.environ['USER']
    email = config('user.email').strip()
    exec_cmd("touch %s/README.rst" % repo, True)

    print("creating .acl")
    with open('%s/.acl' % repo, 'w') as tmpf:
        tmpf.write('admin : %s\n' % master)
        tmpf.write('user  : \n')

    print("creating .%s.profile" % email)
    with open('%s/.%s.profile' % (repo, email), 'w') as tmpf:
        tmpf.write('email: %s\n' % email)
        tmpf.write('notify: all\n')
        tmpf.write('track-files: \n')

    print("creating .description")
    with open('%s/.description' % repo, 'w') as tmpf:
        tmpf.write('%s description goes here\n' % repo)
    try:
        os.remove('%s/.git/description' % repo)
    except OSError:
        pass
    exec_cmd('ln -s ../.description %s/.git/description' % repo, True)

    print("creating .bashrc")
    with open('%s/.bashrc' % repo, 'w') as tmpf:
        tmpf.write('# Bash commands related to %s\n' % repo)

    print("creating hooks:")
    hooks = ['commit-msg', 'post-checkout', 'post-commit', 'post-merge',
             'pre-commit', 'pre-rebase',
             'prepare-commit-msg']
    for hook in hooks:
        print("  %s" % hook)
        path = '%s/.git/hooks' % repo
        make_hook(hook, path)

    print("copying .gitignore\n")
    tmp = dirname(__file__)
    exec_cmd('cp %s/../paster/gitignore.txt %s/.gitignore' % (tmp, repo), True)

def user_setup(repo):
    "Create the user profile"
    print("Setting up repository...")
    print("linking .description...")
    email = config('user.email').strip()
    try:
        os.remove('%s/.git/description' % repo)
    except OSError:
        pass
    exec_cmd('ln -s ../.description %s/.git/description' % repo, True)

    user_profile = '%s/.%s.profile' % (repo, email)
    if not exists(user_profile):
        print("creating .%s.profile" % email)
        with open(user_profile, 'w') as tmpf:
            tmpf.write('email: %s\n' % email)
            tmpf.write('notify: all\n')
            tmpf.write('track-files: \n')

    print("creating hooks:")
    hooks = ['commit-msg', 'post-checkout', 'post-commit', 'post-merge',
             'pre-commit', 'pre-rebase', 'prepare-commit-msg']
    for hook in hooks:
        print("  %s" % hook)
        path = '%s/.git/hooks' % repo
        make_hook(hook, path)


"""
Entradas
Sueldo_bruto-->float-->sa
Salidas
Categoria-->int-->c
Sueldo_neto-->float-->sn
"""
sa=float(input("Digite el salario bruto: "))
sn=0.0#float
if(sa>=5_000_000):
  sn=(sa*0.10)+sa
  c=1
elif(sa<5_000_000 and sa>=4_300_000):
  sn=(sa*0.15)+sa
  c=2
elif(sa<4_300_000 and sa>=3_600_000):
  sn=(sa*0.20)+sa
  c=3
elif(sa<3_600_000 and sa>=2_000_000):
  sn=(sa*0.40)+sa
  c=4
elif(sa<2_000_000 and sa>=900_000):
  sn=(sa*0.60)+sa
  c=5
print("La categoria es: "+str(c))
print("El sueldo neto es de: $","{:.0f}".format(sn))

Taller_Algoritmos/Taller Estructuras de Control Selectivas/Ejercicio10.py