import datetime

from django import template
from django.conf import settings
from django.template.defaultfilters import stringfilter
from django.templatetags.static import static
from django.utils.translation import gettext_lazy as _

register = template.Library()

@register.simple_tag
def static_cdn(url):
    if settings.CDN_ENABLED:
        url = static(url).replace(settings.AWS_S3_CUSTOM_DOMAIN, settings.AWS_S3_CDN_DOMAIN).replace('/static/', '/')
        return url
    else:
        return static(url)

@register.simple_tag
def changelanguage(path, lang):
    if path and lang:
        if lang == 'en':
            newPath = path.replace("/tr/", "/en/")
            newPath = newPath.replace("profil", "profile")

        else:
            newPath = path.replace("/en/", "/tr/")
            newPath = newPath.replace("profile", "profil")
        return newPath
    else:
        return path

@register.filter
def to_br(value):
    return value.replace("<br>", "")

@register.filter
@stringfilter
def custom_date(value):
    now_time = datetime.datetime.now()

    now_time = datetime.datetime.strptime(str(now_time), '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S')
    value = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S%z').strftime('%Y-%m-%d %H:%M:%S')

    now_time = datetime.datetime.strptime(now_time, '%Y-%m-%d %H:%M:%S')
    value = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours=3)

    total_time = now_time - value
    total_time_days = total_time.days
    total_time_str = str(total_time)

    if total_time_days == 1:
        value = str(total_time_days) + " " + _("Gün")
    elif total_time_days < 1:
        value_hour = int(total_time_str.split(':')[0])
        if value_hour == 0:
            value_minute = int(total_time_str.split(':')[1])
            if value_minute == 0:
                value_second = int(total_time_str.split(':')[2])
                value = str(value_second) + " " + _("Saniye")
            else:
                value = str(value_minute) + " " + _("Dakika")
        else:
            value = str(value_hour) + " " + _("Saat")

    elif total_time_days > 1:
        value = value
    else:
        value = value
    return str(value)

custom_date.is_safe = True


import asyncio
import os
import mimetypes
import traceback
import sys
import urllib
from os.path import abspath, join, dirname
from functools import partial, lru_cache
from inspect import ismodule, isclass
from zest.core import is_async, Config, AsyncFile, get_param_length
from zest.httputil import (Request, Response, HTTPError,
                           JsonResponse, RedirectResponse)
from zest.helper import default_config
from zest.helper.util import get_logger, utc_time
from zest.helper.consts import ERROR_TEMPLETE, HTTP_STATUS
from zest.routing import Router

logger = get_logger('zest.web')

G_TEMPLATE_ENGINE = None  # global template engine.
G_TEMPLATE = {}           # global template variable or funtion
G_ROUTER = None           # global router

class App:

    """ The main entry of a Web app.

    Each App instance represents an WSGI application, which contains
    router, template, config, resources and so on.

    Parameters:
    - name: name of app, will have an affect at seraching the root path.
    - router: contaier of all the route.
    - config: config object, considered as a modules object. If not given,
        will initial from the default_config define at `helper.default_config`.
    - more_config: additional config. This will overwrite the previous config.

    NOTE:
        this application implement the WSGI inteface as a coroutine. When
        invoke this application  with other WSGI server, you must call with
        `yield from` syntax. For example:
            app = App()
            yield from app(environ, start_response)
    """

    default_template = 'mako'

    def __init__(self, name=None, router=None, config=None, **more_config):
        self.name = name
        self.router = router or Router()
        self.config = Config.from_module(config or default_config)
        self.config.update(**more_config)

        self.root_path = self.find_root_path(self.name)
        self.set_config('root_path', self.root_path)
        self.static_folder = self.get_config('static_folder', '').strip('/\\')
        self.static_path = abspath(join(self.root_path, self.static_folder))

        self.hooks = {}  # TODO
        self.error_handler = {}

        self.register_template(self.get_config('template_engine',
                                               self.default_template))
        global G_ROUTER
        G_ROUTER = self.router

    def find_root_path(self, name):
        """ Find the web application's root path from given module name.
        """
        if name:
            if name == '__main__':
                return os.getcwd()
            file = sys.modules[name].__file__
        else:
            import __main__ as x
            if ismodule(x) or isclass(x):
                if hasattr(x, '__module__'):
                    x = sys.modules[x.__module__]
                file = getattr(x, '__file__', None)

        return abspath(dirname(file))

    def register_template(self, template, **kwds):
        """ Register template engine.

        The default engine is 'mako'.
        You could easyily specify another engine just through inherit
        class `zest.template.Meta` or just provide a class object bring
        an 'render' method.
        """
        self.template_engine = None
        if isinstance(template, str):
            if template != 'mako':
                raise ValueError("Zest only support mako template engine now."
                                 "You could specify another engine through "
                                 "`zest.template` module or provide a class"
                                 "object bring an 'render' method.")
            from zest.template import Mako
            template = Mako
        assert hasattr(template, 'render'), "Invalid template engine."

        engine = template(**self.config)
        global G_TEMPLATE_ENGINE, G_TEMPLATE
        G_TEMPLATE_ENGINE = self.template_engine = engine

        G_TEMPLATE.update(**kwds)
        G_TEMPLATE['app'] = self
        G_TEMPLATE['config'] = self.config
        G_TEMPLATE['hooks'] = self.hooks

        return engine

    def add_template_global(self, name, value):
        """ Add a global template variable.

        Parameters:
        - name: variable name.
        - value: the object use in a template.
        """
        global G_TEMPLATE
        assert name not in G_TEMPLATE
        G_TEMPLATE[name] = value

    def global_template(self, name):
        """ Decorator to add template global variable.

        Usage:
            @global_template
            def template_func(*args, **kwds):
                do_something()

            ${template_func()}  # invoke at template.html
        """
        def deco(func):
            self.add_template_global(name, func)
            return func
        return deco

    @property
    def debug(self):
        return self.config['DEBUG']

    @debug.setter
    def debug(self, value):
        self.config['DEBUG'] = bool(value)

    @property
    def server_host(self):
        return self.config['SERVER_HOST']

    @property
    def server_port(self):
        return self.config['SERVER_PORT']

    def get_config(self, k, default=None):
        return self.config.get(k, default)

    def set_config(self, k, v, set_default=False):
        if set_default:
            self.config.setdefault(k, v)
        else:
            self.config[k] = v

    def trigger_hooks(self, name, *args, **kwds):
        """ Call a hook with given param
        """
        hook = self.hooks.get(name)
        if hook:
            return hook(*args, **kwds)

    def add_hook(self, name, func):
        self.hooks[name] = func
        return func

    def add_route(self, rule, controller, method='GET', priority=True):
        """ Bind a controller to it's corresponding route.

        Parameters:
        - rule: Regular expression or normal string. If rule contails a capture
            group, the capture group will pass to controller as a params tuple.
        - controller: function to handle request. This function must have at
            least one param and the first params will be pass on an `Request`
            instance, which represents the current request context.
        _ method: request method, default to be 'GET', could provide a method
            list to support multi method.
        - priority: the priority of rule. the rule have high priority will be
            match first. (default to be True)

        Equals to `router.add_route`
        """
        self.router.add_route(rule, controller, method, priority)

    def route(self, rule, method='GET', priority=True):
        """ Decorator to bind a controller to it's corresponding route.

        Usage:

            @app.route('/hello/(\w+)')
            def index(name):
                return 'Hello, %s' % name

        This will bind route '/hello/(\w+)' to `index` controller.
        """
        def deco(func):
            self.add_route(rule, func, method, priority)
            return func
        return deco

    def register_before_request(self, func):
        """ Register a function to called before we start to process request.
        """
        if get_param_length(func) > 1:
            raise ValueError("`before_request` should have at most one param.")

        self.add_hook('_before_request', func)

    def before_request(self, func):
        """ Decorator for register function to called before process request.

        Parameters:
        - func: function to be called. This function might have one or zero
            parameter. And once parameter is given, it will be pass on an
            `Request` instance, which represents the current request context.

        Usage:

            @app.before_request
            def abort_ip(request):
                if request.IP in FORBIDDEN:
                    abort(403)

            @app.before_request
            def db_init():
                init_the_db()

        The first will abort this request if the request iP is forbidden.
        The second will initalize database then continue to process request.
        """
        self.register_before_request(func)
        return func

    def handle_before_request(self, request):
        func = self.hooks.get('_before_request')
        if func:
            if get_param_length(func) == 1:
                func = partial(func, request)  # Pass current request context.
            return func()

    def register_after_request(self, func):
        """ Register a function to called after making a response.
        """
        self.add_hook('_after_request', func)

    def after_request(self, func):
        """ Decorator for register function to called before process request.

        Parameters:
        - func: function to be called after response. This function might have.
         one or zero parameter. And once param is given, it will be pass on an
            `Response` instance, which represents response for request.

        Usage:

            @app.after_request
            def abort_ip(response):
                response.set_header('Server', 'Zest')
                return response

        This will add a server header to all the response.
        """
        self.register_after_request(func)
        return func

    def handle_after_request(self, response):
        func = self.hooks.get('_after_request')
        if func:
            if get_param_length(func) == 1:
                rsp = func(response)
                if not rsp or not isinstance(rsp, Response):
                    raise ValueError('`after_request` must return a response.')
            else:
                rsp = func()
        else:
            rsp = response  # do nothing and return the same reponse.

        return rsp

    def add_error_handler(self, code, handler):
        """ Add a function to handle HTTP error.

        Parameters:
        - code: status of HTTP error
        - handler: function to handle error. This function might have one
            or no parameter. Once parameter is given, it will be pass on an
            `Request` instance, which represents the current request context.
        """
        self.error_handler[code] = handler

    def error(self, code):
        """ Decorator to add a error handler.

        Usage:

            @error(404)
            def handle_404():
                return render('404.html')
        """
        def deco(func):
            self.add_error_handler(code, func)
        return deco

    def merge_router(self, router):
        """ Merge routes with other router.

        Equals to `router.merge`
        """
        return self.router.merge(router)

    def get(self, rule, **kwds):
        """ Equals to `add_route(rule, method='GET', **kwds)`."""
        return self.router.get(rule, **kwds)

    def post(self, rule, **kwds):
        """ Equals to `add_route(rule, method='POST', **kwds)`."""
        return self.router.post(rule, **kwds)

    def delete(self, rule, **kwds):
        """ Equals to `add_route(rule, method='DELETE', **kwds)`."""
        return self.router.delete(rule, **kwds)

    def head(self, rule, **kwds):
        """ Equals to `add_route(rule, method='HEAD', **kwds)`."""
        return self.router.head(rule, **kwds)

    def log(self, msg):
        """ Logs messages from specify logger.
        """
        logger.info(msg)

    def log_exception(self, exc):
        """ Logs an exception.

        When debug model is set, log the traceback instead.
        """
        if self.debug:
            logger.error(traceback.format_exc())
        else:
            logger.error(exc)

    def add_static_handler(self, favicon_handler=None, static_handler=None):
        """ Add a default route to handle static file.
        """
        def _favicon_handler(request):
            return static_file('favicon.ico', self.root_path, request)

        def _static_handler(request, path):
            file_path, name = os.path.split(path)
            return static_file(name,
                               join(self.static_path, file_path.strip('/\\')),
                               request)

        self.add_route('/favicon\.ico', favicon_handler or _favicon_handler,
                       priority=False)
        self.add_route('/%s/(.*)' % self.static_folder,
                       static_handler or _static_handler, priority=False)

    def run(self, host=None, port=None, debug=False, **kwds):
        """ Runing server at specify address.

        Parameters:
        - host: server address to bind. (default to 127.0.0.1)
        - port: server port to bind. (default to 7676)
        - debug: develop and debugl model.(default to False)
        - autoreload: sets True to start `auto_reload` server.  # TODO
        - kwds: WSGI server setting. Go to `zest.server` for detail.
        """
        from zest.server import make_wsgi_server
        self.config.update_no_none(server_host=host, server_port=port,
                                   debug=bool(debug), **kwds)
        self.add_static_handler()

        host, port = self.server_host, self.server_port
        self.log('runing server at %s:%s' % (host, port))
        make_wsgi_server(app=self,
                         host=host,
                         port=port,
                         debug=self.debug,
                         **kwds)

    def match(self, request):
        """ Find a matching rule and return the corresponding controller.

        If find the matching rule but it has a bad request method, this
        will raise a 403 error. If not find, raise 404 error.
        """
        func, params = self.router.match_wsgi(request)
        if not func:
            if params == 403:  # match, but with no allow method
                raise HTTPError(403)
            else:              # can't find any rule match.
                raise HTTPError(404)
        else:
            if self.auto_unquote:
                try:
                    params = map(urllib.parse.unquote, params)
                except Exception as exc:
                    self.log_exception(exc)
            return partial(func, request, *params)

    def make_response(self, response, code=None):
        """ Create an response, return an Response instance.

        Parameter:
        - response: Response object or string or sequence.
        - code: response status code.
        """
        if response is None:
            raise ValueError("Can't response None.")

        rp = response
        if not isinstance(response, Response):
            body = headers = None
            code = code or 200
            if isinstance(response, (tuple, list)):
                body, code, headers = response + (None, ) * (3 - len(response))
            elif isinstance(response, str):
                body = response
            else:
                body = str(response)  # TODO
            rp = Response(body, headers, code)
        elif code is not None:
            rp.set_status(code)

        return rp

    @lru_cache(maxsize=100)
    def make_error_response(self, err, request=None):
        """ Create error response from HTTP error.
        """
        handler = self.error_handler.get(err.status)
        status = int(err.status)
        if not handler:
            reason = HTTP_STATUS.get(status)
            if not reason:
                status = 404  # reponse 404 while not reason find.
                reason = HTTP_STATUS[status]
            body = ERROR_TEMPLETE.format(status=status,
                                         reason=reason,
                                         message=err.message if self.debug
                                         else '')
            response = Response(body)
        else:
            if get_param_length(handler) == 1:
                response = handler(request)
            else:
                response = handler()

        return self.make_response(response, status)

    @asyncio.coroutine
    def response(self, environ, start_response):
        """ Respone from WSGI environ.
        """
        request = Request(environ)

        try:
            self.handle_before_request(request)

            rsp = self.match(request)()
            if is_async(rsp):
                rsp = yield from rsp
            response = self.make_response(rsp)
        except HTTPError as err:
            response = self.make_error_response(err, request)
        except Exception as exc:
            self.log_exception(exc)
            response = self.make_error_response(HTTPError(500))

        response = self.handle_after_request(response)
        return response.send(environ, start_response)

    def wsgi_app(self, environ, start_response):
        request = Request(environ)
        try:
            self.handle_before_request(request)
            rsp = self.match(request)()
            response = self.make_response(rsp)
        except HTTPError as err:
            response = self.make_error_response(err, request)
        except Exception as exc:
            self.log_exception(exc)
            response = self.make_error_response(HTTPError(500))

        response = self.handle_after_request(response)
        return response.send(environ, start_response)

    def __call__(self, environ, start_response):
        """ Implaments WSGI inteface for application.
        """
        return self.response(environ, start_response)

    def __getattr__(self, key):
        """ This method only gets called for attributes that don't exist.

        It's use to when some attributes not in the App instance. Just
        find from the config.
        """
        return self.config.get(key, None)

    def __repr__(self):
        return '<Application: %s>' % (self.name or '')

# Blow are some helpful tools

def static_file(name, path, request):
    """ Make a static file response.

    Parameters:
    - name: file name of static file.
    - path: absolute static directory path.
    - request: a `Request` object.

    ## This cool function is inspired by `bottle.static_file` then add a
    async read and write support
    """
    file_path = abspath(join(path, name.strip('/\\')))
    if not os.path.exists(file_path) or not os.path.isfile(file_path):
        raise HTTPError(404, 'File does not exist.')
    if not os.access(file_path, os.R_OK):
        raise HTTPError(403, 'You do not have permission to access this file.')

    headers = {}
    mimetype, encoding = mimetypes.guess_type(file_path)
    if mimetype:
        headers['Content-Type'] = mimetype
    else:
        headers['Content-Type'] = 'application/octet-stream'
        headers['Content-Disposition'] = 'attachment; filename="%s"' % name
    if encoding:
        headers['Content-Encoding'] = encoding

    stats = os.stat(file_path)
    headers['Content-Length'] = stats.st_size
    headers['Last-Modified'] = utc_time(stats.st_mtime)

    ims = request.if_modify_since
    if ims is not None and ims >= int(stats.st_mtime):
        response = Response(headers=headers, status=304)
    else:
        body = ''
        if request.method != 'HEAD':
            loop = request.event_loop
            with open(file_path, 'rb') as file:
                body = yield from AsyncFile(loop=loop, file=file).read()
        response = Response(body, headers)

    return response

def abort(status, message=''):
    """ Finish and response the request with specify status.

    Parameters:
    - status: integer number of response status.
    - message: string text of response body.
    """
    if not isinstance(status, int) or status not in HTTP_STATUS:
        raise ValueError("Invalid status code: %s." % status)
    raise HTTPError(status, message)

def redirect(path_or_url, status=303, response=None):
    """ Redict to specify url.

    Parameters:
    - path_or_url: redirect traget, could be a path or url, when it's a path,
         the redirect result will be: `request.url + path`.
    - status: response status. (default to be 303 For HTTP/1.1)
    - response: a `Response` object. (default to be None)
    """
    return RedirectResponse(path_or_url, status, response)

def to_json(*args, **kwds):
    """ Accept a key-value paris and convert to a Json response.

    Parameters:
    - args, kwds: the same with `dict` object.

    Usage:
        @app.get('/json')
        def json_controller(request):
            return to_json({'name': 'zest'})
    """
    return JsonResponse(*args, **kwds)

def render(name, **kwds):
    """ Render a template from template folder.

    Parameters:
    - name: template name to be rendered.
    - kwds: all the variable names (include the global custom variable)
        accessible to the template.
    """
    kwds.update(G_TEMPLATE)
    return Response(body=G_TEMPLATE_ENGINE.render(name, **kwds))

def url_for(url):
    pass

# TODO
def to_xml():
    pass

# TODO
def login_required():
    pass


from tkinter import *
def rateus():
    rating = f"{myslider2.get()}\n"
    with open('record.txt','a') as f:
        f.write(str(rating))
root = Tk()
root.geometry('600x400')
root.title('Slider in Tkinter')
Label(root,text="How many stars we deserve?").pack()
myslider2 = Scale(root,from_=0,to=5,orient=HORIZONTAL)
myslider2.set(4)
myslider2.pack()
Button(root,text="Please Rate us!",command=rateus).pack()
root.mainloop()


from odoo import api, models

from odoo.addons.base_iban.models.res_partner_bank import (
    _map_iban_template,
    normalize_iban,
    pretty_iban,
)

class ResPartnerBank(models.Model):
    _inherit = "res.partner.bank"

    @api.onchange("acc_number", "acc_type")
    def _onchange_acc_number_base_bank_from_iban(self):
        if self.acc_type != "iban":
            return
        acc_number = pretty_iban(normalize_iban(self.acc_number)).upper()
        country_code = self.acc_number[:2].lower()
        iban_template = _map_iban_template[country_code]
        first_match = iban_template[2:].find("B") + 2
        last_match = iban_template.rfind("B") + 1
        bank_code = acc_number[first_match:last_match].replace(" ", "")
        bank = self.env["res.bank"].search(
            [("code", "=", bank_code), ("country.code", "=", country_code.upper())],
            limit=1,
        )
        self.update({"bank_id": bank.id, "acc_number": acc_number})


from bs4 import BeautifulSoup
import requests
import json
import query

FROM_URL = 'http://friendorfollow.com/twitter/most-followers/'

# order tweets by activity
def sort_tweets_by_activity(tweets):
    def activityKey(tweet):
        return int(tweet.likes) + int(tweet.retweets) + int(tweet.replies)

    return sorted(tweets, key=activityKey, reverse=True)

# gets 500 most recent tweets from a user post-2010 (the text), sorted
def get_user_tweets_text(username):
    tweets = sort_tweets_by_activity(query.query_tweets_once("from:" + username, 500))
    ret = []
    for tweet in tweets:
        if len(tweet.text) > 0 and tweet.text[0] != '@':
            ret.append(tweet.text)
    for text in ret:
        list = text.split()
        for word in list:
            if len(word) > 4 and word[0:3] == "http":
                list.remove(word)
        " ".join(list)
    return ret

# gets a list of strings representing the twitter handles of the
# celebs with the 100 most visited twitter pages
def get_celeb_usernames():
    webpage = requests.get(FROM_URL).content
    soup = BeautifulSoup(webpage, 'html.parser')
    links = soup.find_all("a", target='f', class_='tUser')
    users = []
    for a in links:
        users.append(a.contents[0][1:])
    return users[:100]

# updates celebs.json, which holds the data on celebrity twitter names
def update_db(filename="celebs.json", users=get_celeb_usernames()):
    with open(filename, 'r+') as f:
        data = f.read()
        cur_json_dict = {}
        if(data != ''):
            cur_json_dict = json.loads(data)
        f.seek(0)
        for name in users:
            if name not in cur_json_dict:
                print("Fetching tweets for " + name)
                cur_json_dict[name] = get_user_tweets_text(name)
            else:
                print("Already have tweets for " + name)
        f.write(json.dumps(cur_json_dict))
        f.truncate()

# gets the link to a given user's profile picture
def get_prof_pic(user):
    profile = requests.get('http://twitter.com/' + user).content
    soup = BeautifulSoup(profile, 'html.parser')
    link = soup.find("img", class_='ProfileAvatar-image')['src']
    return link

if __name__ == '__main__':
    quoatable_users = ["colesprouse","rickygervais","bjnovak","mindykaling","VancityReynolds","chrissyteigen",
                       "AnnaKendrick47","prattprattpratt","StephenAtHome","ConanOBrien"]
    update_db("quotable.json", users=quoatable_users)


class account:
    def __init__(self, args):
        self.id = args[0]
        self.username = args[1]
        self.active = args[3]
        self.lang = args[5]
        self.created_at = args[6]

    def toMap(self):
        return {
            "id": self.id,
            "username": self.username,
            "active": self.active,
            "lang": self.lang,
            "created_at": self.created_at.strftime("%Y-%m-%d %H:%I:%S"),
        }


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

import os
import re
import subprocess
import time

import xdef
import xproc
import xsrc
import xurl

def setAct(act, val):

    if act == 'forward' and val:
        cmd = 'seek %s' %(val)
    elif act == 'backward' and val:
        cmd = 'seek -%s' %(val)
    elif act == 'percent' and val:
        cmd = 'seek %s absolute-percent' %(val)
    elif act in ['osd', 'mute', 'pause', 'stop', 'playlist-next', 'playlist-prev', 'sub-remove']:
        cmd = '%s' %(act)
    elif act in ['audio-next']:
        cmd = 'cycle aid'
    else:
        print('unsupported: %s %s' %(act, val))
        return

    os.system('echo %s > %s' %(cmd, xdef.fifo))
    return

def play(url, ref):

    p = None
    xargs = []

    url, cookies, ref = xsrc.getSource(url, ref=ref)

    if not url:
        print('\n[mpv][play] invalid url\n')
        return

    if cookies and re.search(r'google.com', url) and xproc.checkProcessRunning('mpv'):
        os.system('echo stop > %s' %(xdef.fifo))
        while xproc.checkProcessRunning('mpv'):
            time.sleep(1)

    if not xproc.checkProcessRunning('mpv'):

        xargs.append('--user-agent=\'%s\'' %(xurl.defvals.ua))
        xargs.append('--referrer=\'%s\'' %(ref))

        if cookies:
            xargs.append('--http-header-fields="Cookie:%s"' %(cookies))

        cmd = '%s %s \'%s\'' %(xdef.mpv, ' '.join(xargs), url)
        print('\n[mpv][cmd]\n\n\t'+cmd+'\n')
        p = subprocess.Popen(cmd, shell=True)

    else:
        os.system('echo loadfile \"%s\" > %s' %(url, xdef.fifo))
        os.system('echo sub-remove > %s' %(xdef.fifo))

    sub = xsrc.getSub(ref)
    if sub:
        os.system('echo sub-add \"%s\" select > %s' %(sub, xdef.fifo))

    if p:
        p.communicate()

    return

def append(url):
    os.system('echo loadfile \"%s\" append > %s' %(url, xdef.fifo))
    return


p1 = 0
p2 = 0
with open("input.txt") as file:
    for line in file:
        a, b = [
            range(int(y[0]), int(y[1]) + 1)
            for y in [x.split("-") for x in line.strip().split(",")]
        ]
        if (a.start in b and a[-1] in b) or (b.start in a and b[-1] in a):
            p1 += 1
        if (a.start in b or a[-1] in b) or (b.start in a or b[-1] in a):
            p2 += 1

print(f"Part 1: {p1}")
print(f"Part 2: {p2}")


#======================================================================================================================
#                                     Populate STS and STAND from Look Up Table
#======================================================================================================================
#
# Author(s)         :  Written by CloverPoint (Jeff Kruys); Edited by Shari Willmott to work with BEM v3_2
#
# Date              :  2017-05-23
#                   :  2019-01-27
#
# Purpose           :  This script populates the structrual stage and stand values in the BEM feature class from the
#                      data provided by the Lookup Table
#
# Arguments         :  argv(1) = BEM feature class
#                   :  argv(2) = STS Lookup Table csv file
#
# Example Input     :  X:\fullpath\BEM_V2 Y:\fullpath\LUT.csv
#
# Outputs           :  BEM feature class is updated with the fields AGE_CL_STS, AGE_CL_STD, FORESTED_1, FORESTED_2 and
#                      FORESTED_3 and populates the attributes for these fields based on the VRI data and the Lookup
#                      Table
#
# Dependencies      :  The csv file must have values for the above fields for each unique EcoSection, BGC and BEU_MC
#                      combination and the VRI feature class must have VRI_AGE_CL_STS and VRI_AGE_CL_STD fields. If
#                      not, run the script 04a_Add_LUT_age_classes_to_VRI.py first
#
# Expected Duration :  Depends on the size of the input BEM feature class
#
# History           :  Script created for BEM Ominceca Moose WHR (Madrone Dosier 15.0242)
#                      Script updated to allow Stand value "B" to be assigned for ecosystem units that have Structural
#                      Stages 3b and 3c (BEM3.2)
#                   :  2020-09-04 (AE): Script used for the Northeast Moose WHR (Madrone Dosier 19.0461)
#                   :  2021-01-03 (AE): Script used for the Skeena Moose WHR (Madrone Dosier 20.0221)
#                   :  2021-03-01 (JK): Update the assignment of Stand codes to use the value in the STD_VRI field
#                      first.
#                   :  2021-03-19 (JK): Remove the VRI loverlay step; update logic for assigning STRCT_S# and STAND_A#.
#
#======================================================================================================================

print("Initializing...")
import arcpy, sys, os, time, pdb, operator, csv
dtCalcScriptStart = time.time()
print('Start Time: ' + time.ctime(time.time()))

if len(sys.argv) != 3:
    print("- Two arguments required: WHR (PEM/TEM or BEM) feature class, and Structural Stage LUT CSV file.")
    sys.exit()

#set environment settings
working_fc = sys.argv[1]
working_gdb = os.path.split(working_fc)[0]
whr_master_path = os.path.split(working_gdb)[0]
sts_csv = sys.argv[2]

## ---------------------------------------------------------------------------------------------------------
## Check that the expected arguments were provided and that all required fields exist
## ---------------------------------------------------------------------------------------------------------

working_fc = sys.argv[1]
if not arcpy.Exists(working_fc):
    print("- Specified WHR polygon feature class does not exist.")
    sys.exit()

bem_req_fields = ["TEIS_ID", "STD_VRI", "VRI_AGE_CL_STS", "VRI_AGE_CL_STD", "BGC_ZONE", "BGC_SUBZON", "BGC_VRT", "BGC_PHASE",
                  "SDEC_1", "SDEC_2", "SDEC_3", "BEUMC_S1", "BEUMC_S2", "BEUMC_S3", "STRCT_S1", "STRCT_S2", "STRCT_S3",
                  "STAND_A1", "STAND_A2", "STAND_A3"]
bem_existing_fields = [f.name for f in arcpy.ListFields(working_fc)]
bem_missing_fields = []
for f in bem_req_fields:
    if f not in bem_existing_fields:
        bem_missing_fields.append(f)
if len(bem_missing_fields) > 0:
    print("**** Specified BEM polygon feature class is missing required fields: {}".format(str(bem_missing_fields)))
    sys.exit()

lut_req_fields = ["BGC_Label", "BGC_ZONE", "BGC_SUBZON", "BGC_VRT", "BGC_PHASE", "BEU_MC", "REALM", "GROUP", "CLASS",
                  "KIND", "Forested (Y/N)", "Strct_Climax", "Stand_Climax", "Stand_Age_0-15", "Stand_Age_16-30",
                  "Stand_Age_31-50", "Stand_Age_51-80", "Stand_Age_80+", "Struct_Age_0-3", "Struct_Age_4-10",
                  "Struct_Age_11-30", "Struct_Age_31-40", "Struct_Age_41-60", "Struct_Age_61-80", "Struct_Age_81-139",
                  "Struct_Age_140-249", "Struct_Age_250+", "Snow_Code"]
sts_csv_obj = open(sts_csv, "rU")
csvReader = csv.DictReader(sts_csv_obj)
lut_missing_fields = []
csv_line_total = 0
for data in csvReader:
    csv_line_total += 1
for req_field in lut_req_fields:
    if req_field not in data.keys():
        lut_missing_fields.append(req_field)
if len(lut_missing_fields) > 0:
    print("*** Specified structural stage lookup table missing required field(s): {}".format(str(lut_missing_fields)))
    sys.exit()
del csvReader
sts_csv_obj.close()

## ---------------------------------------------------------------------------------------------------------
## Define routine to format duration value in seconds into hours or minutes
## ---------------------------------------------------------------------------------------------------------

def SanitizeElapsedTime(dtInput):
    if dtInput < 120:
        strElapsedTime = str(int(dtInput)) + ' sec.'
    elif dtInput < 3600:
        strElapsedTime = str(round(dtInput / 60, 1)) + ' min.'
    else:
        strElapsedTime = str(round(dtInput / 3600, 2)) + ' hr.'
    return strElapsedTime

## ---------------------------------------------------------------------------------------------------------
## Read the structural stage lookup table into dictionaries
## ---------------------------------------------------------------------------------------------------------

print("Reading STS lookup table")

forested_dict = {}
stand_climax_dict = {}
sts_climax_dict = {}
sts_age_lookup_dict = {}
stand_age_lookup_dict = {}
line_counter = 0

sts_csv_obj = open(sts_csv, "rU")
csvReader = csv.DictReader(sts_csv_obj)
for data in csvReader:
    line_counter += 1
    curr_bgc_zone          = str(data["BGC_ZONE"]).replace('"','').replace(' ','')
    curr_bgc_subzon        = str(data["BGC_SUBZON"]).replace('"','').replace(' ','')
    curr_bgc_vrt           = str(data["BGC_VRT"]).replace('"','').replace('0','')
    curr_bgc_phase         = str(data["BGC_PHASE"]).replace('"','').replace(' ','')
    curr_beu_mc            = str(data["BEU_MC"]).replace('"','').replace(' ','')
    curr_forested          = str(data["Forested (Y/N)"]).replace('"','').replace(' ','')
    curr_sts_climax        = str(data["Strct_Climax"]).replace('"','').replace(' ','')
    curr_stand_climax      = str(data["Stand_Climax"]).replace('"','').replace(' ','')
    curr_STAND_Age_0_15    = str(data["Stand_Age_0-15"]).replace('"','').replace(' ','')
    curr_STAND_Age_16_30   = str(data["Stand_Age_16-30"]).replace('"','').replace(' ','')
    curr_STAND_Age_31_50   = str(data["Stand_Age_31-50"]).replace('"','').replace(' ','')
    curr_STAND_Age_51_80   = str(data["Stand_Age_51-80"]).replace('"','').replace(' ','')
    curr_STAND_Age_81_9999 = str(data["Stand_Age_80+"]).replace('"','').replace(' ','')
    curr_STS_Age_0_3       = str(data["Struct_Age_0-3"]).replace('"','').replace(' ','')
    curr_STS_Age_4_10      = str(data["Struct_Age_4-10"]).replace('"','').replace(' ','')
    curr_STS_Age_11_30     = str(data["Struct_Age_11-30"]).replace('"','').replace(' ','')
    curr_STS_Age_31_40     = str(data["Struct_Age_31-40"]).replace('"','').replace(' ','')
    curr_STS_Age_41_60     = str(data["Struct_Age_41-60"]).replace('"','').replace(' ','')
    curr_STS_Age_61_80     = str(data["Struct_Age_61-80"]).replace('"','').replace(' ','')
    curr_STS_Age_81_139    = str(data["Struct_Age_81-139"]).replace('"','').replace(' ','')
    curr_STS_Age_140_249   = str(data["Struct_Age_140-249"]).replace('"','').replace(' ','')
    curr_STS_Age_250_9999  = str(data["Struct_Age_250+"]).replace('"','').replace(' ','')

    curr_bgc_unit = curr_bgc_zone + curr_bgc_subzon + curr_bgc_vrt + curr_bgc_phase
    curr_eco_unit = curr_bgc_unit + "~" + curr_beu_mc

    sts_age_lookup_dict[curr_eco_unit] = {}
    sts_age_lookup_dict[curr_eco_unit][2] = curr_STS_Age_0_3
    sts_age_lookup_dict[curr_eco_unit][7] = curr_STS_Age_4_10
    sts_age_lookup_dict[curr_eco_unit][20] = curr_STS_Age_11_30
    sts_age_lookup_dict[curr_eco_unit][35] = curr_STS_Age_31_40
    sts_age_lookup_dict[curr_eco_unit][50] = curr_STS_Age_41_60
    sts_age_lookup_dict[curr_eco_unit][70] = curr_STS_Age_61_80
    sts_age_lookup_dict[curr_eco_unit][125] = curr_STS_Age_81_139
    sts_age_lookup_dict[curr_eco_unit][195] = curr_STS_Age_140_249
    sts_age_lookup_dict[curr_eco_unit][301] = curr_STS_Age_250_9999

    stand_age_lookup_dict[curr_eco_unit] = {}
    stand_age_lookup_dict[curr_eco_unit][15] = curr_STAND_Age_0_15
    stand_age_lookup_dict[curr_eco_unit][30] = curr_STAND_Age_16_30
    stand_age_lookup_dict[curr_eco_unit][50] = curr_STAND_Age_31_50
    stand_age_lookup_dict[curr_eco_unit][80] = curr_STAND_Age_51_80
    stand_age_lookup_dict[curr_eco_unit][9999] = curr_STAND_Age_81_9999

    sts_climax_dict[curr_eco_unit] = curr_sts_climax
    stand_climax_dict[curr_eco_unit] = curr_stand_climax
    forested_dict[curr_eco_unit] = curr_forested

    if line_counter == 100 or line_counter % 1000 == 0 or line_counter == csv_line_total:
        print("    - Read " + str(line_counter) + " lines")

vri_stand_class_dict = {}
vri_stand_class_dict[-1] = -1
vri_stand_class_dict[2] = 15
vri_stand_class_dict[7] = 15
vri_stand_class_dict[20] = 30
vri_stand_class_dict[35] = 50
vri_stand_class_dict[50] = 50
vri_stand_class_dict[70] = 80
vri_stand_class_dict[125] = 9999
vri_stand_class_dict[195] = 9999
vri_stand_class_dict[301] = 9999

## ---------------------------------------------------------------------------------------------------------
## Overlay WHR with VRI and write the age classes and looked-up structural stage and stand codes
## ---------------------------------------------------------------------------------------------------------

for i in range(1, 4):
    new_field = "FORESTED_{}".format(i)
    if new_field not in bem_existing_fields:
        print("Adding {} field to {}".format(new_field, working_fc))
        arcpy.AddField_management(working_fc, new_field, "TEXT", "#", "#", "1")

cursor_fields = ["TEIS_ID", "STD_VRI", "VRI_AGE_CL_STS", "VRI_AGE_CL_STD", "BGC_ZONE", "BGC_SUBZON", "BGC_VRT", "BGC_PHASE",
                 "SDEC_1", "SDEC_2", "SDEC_3", "BEUMC_S1", "BEUMC_S2", "BEUMC_S3", "STRCT_S1", "STRCT_S2", "STRCT_S3",
                 "STAND_A1", "STAND_A2", "STAND_A3", "FORESTED_1", "FORESTED_2", "FORESTED_3", "SHAPE@XY"]

print("Updating attribute values in WHR polygons")
row_count = 0
row_total = int(arcpy.GetCount_management(working_fc).getOutput(0))
cfd = {}
x = 0
for cursor_field in cursor_fields:
    cfd[cursor_field] = x
    x += 1
with arcpy.da.UpdateCursor(working_fc, cursor_fields) as cursor:
    for row in cursor:
        row_count += 1

        # STRCT_S# is assigned the structural stage code from the Structural Stage Look-up Table using the BEC unit and
        # first component ecosystem codes and the age value of highest precedence. The age value to be used is
        # VRI_AGE_CL_STS; if age class value is -1, then, we use null if the STRCT_S1 attribute is null; if there is no
        # other age information, and the ecosystem is not forested (Forested = N), or a non-productive forest such as
        # parkland forest (Forested = Y and Climax STS of "7" or "3b" indicating krummholtz), we use the climax
        # structural stage code for the ecosystem unit according to the Structural Stage Look-up Table.

        # If the value assigned to the STRCT_S# attribute begins with 1-2 or is null, STAND_D# is assigned a null
        # value; if STRCT_S# begins with 3-7, STAND_D# is assigned the stand composition code from the Structural Stage
        # Look-up Table using the BEC unit and first component ecosystem codes and the age value of highest precedence.
        # The age value to be used is VRI_AGE_CL_STAND; if age class value is -1 and ecosystem is not forested
        # (Forested = N), or a non-productive forest such as parkland forest (Forested = Y and Climax STS of "7" or "3b"
        # indicating krummholtz), we use the climax stand composition code for the ecosystem unit according to the
        # Look-up Table.

        # Updated logic:
        # If the ecosystem unit is found in the LUT:
        #     Assign forested value from LUT to the FORESTED_# field
        #     If value in VRI_AGE_CL_STS is > 0:
        #         Look up the STS value from the LUT and assign it to STRCT_S#
        #     If VRI_AGE_CL_STS = -1:
        #         If FORESTED_# = 'N' or BGC_SUBZON ends with "p" (parkland):
        #             Look up the climax STS from the LUT and assign it to STRCT_S#
        #         Otherwise:
        #             Assign a blank to STRCT_S#
        #     If STRCT_S# is blank, 1, 2 or 3:
        #         Assign blank to STAND_A#
        #     If STRCT_S# begins with 4 to 7:
        #         If STD_VRI is 'B', 'C' or 'M':
        #             Assign the value in STD_VRI to STAND_A#
        #         If STD_VRI is blank:
        #             If VRI_AGE_CL_STD > 0:
        #                 Look up the STD code from the LUT and assign it to STAND_A#
        #             If VRI_AGE_CL_STD = -1:
        #                 If FORESTED_# = 'N' or BGC_SUBZON ends with "p" (parkland):
        #                     Look up the climax STD code from the LUT and assign it to STAND_A#
        #                 Otherwise:
        #                     Assign a blank to STAND_A#

        for i in range(1, 4):
            if row[cfd["SDEC_{}".format(i)]] > 0:
                eco_unit = "{}{}{}{}~{}".format(str(row[cfd["BGC_ZONE"]]).replace('None',''),
                                                str(row[cfd["BGC_SUBZON"]]).replace('None',''),
                                                str(row[cfd["BGC_VRT"]]).replace('None','').replace('0',''),
                                                str(row[cfd["BGC_PHASE"]]).replace('None',''),
                                                str(row[cfd["BEUMC_S{}".format(i)]]).replace('None',''))
                try:
                    row[cfd["FORESTED_{}".format(i)]] = forested_dict[eco_unit]
                except:
                    row[cfd["FORESTED_{}".format(i)]] = ''
                if eco_unit in sts_age_lookup_dict.keys():
                    if row[cfd["VRI_AGE_CL_STS"]] > 0:
                        row[cfd["STRCT_S{}".format(i)]] = sts_age_lookup_dict[eco_unit][row[cfd["VRI_AGE_CL_STS"]]]
                    elif forested_dict[eco_unit] == 'N' or str(row[cfd["BGC_SUBZON"]]).replace('None','')[:-1] == 'p':
                        row[cfd["STRCT_S{}".format(i)]] = sts_climax_dict[eco_unit]
                    else:
                        row[cfd["STRCT_S{}".format(i)]] = ''
                    if row[cfd["STRCT_S{}".format(i)]][:1] in ['4', '5', '6', '7']:
                        if row[cfd["STD_VRI"]] in ['B', 'C', 'M']:
                            row[cfd["STAND_A{}".format(i)]] = row[cfd["STD_VRI"]]
                        elif row[cfd["VRI_AGE_CL_STD"]] > 0:
                            row[cfd["STAND_A{}".format(i)]] = stand_age_lookup_dict[eco_unit][row[cfd["VRI_AGE_CL_STD"]]]
                        elif forested_dict[eco_unit] == 'N' or str(row[cfd["BGC_SUBZON"]]).replace('None','')[
                                :-1] == 'p':
                            row[cfd["STAND_A{}".format(i)]] = stand_climax_dict[eco_unit][row[cfd["VRI_AGE_CL_STD"]]]
                        else:
                            row[cfd["STAND_A{}".format(i)]] = ''
                    else:
                        row[cfd["STAND_A{}".format(i)]] = ''
                else:
                    row[cfd["STRCT_S{}".format(i)]] = ''
                    row[cfd["STAND_A{}".format(i)]] = ''
            else:
                row[cfd["STRCT_S{}".format(i)]] = ''
                row[cfd["STAND_A{}".format(i)]] = ''

        cursor.updateRow(row)

        if row_count == 100 or row_count % 10000 == 0 or row_count == row_total:
            print("    - Processed {} of {} rows".format(row_count, row_total))

## ---------------------------------------------------------------------------------------------------------
## Done
## ---------------------------------------------------------------------------------------------------------

dtCalcNow = time.time()
dtCalcScriptElapsed = dtCalcNow - dtCalcScriptStart
print(" ")
print("Script complete after {}".format(SanitizeElapsedTime(dtCalcScriptElapsed)))


#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    for i in range(N):
        if A[i] > i:
            print(-1)
            exit()
    ans = 0
    now = N - 1
    while now >= 0:
        ans += A[now]
        cnt = 0
        while A[now - cnt] == A[now] - cnt:
            cnt += 1
        if A[now - cnt] < A[now] - cnt:
            print(-1)
            exit()
        now -= cnt
    print(ans)

if __name__ == "__main__":
    main()


import torch
import torch.nn.functional as F
from torch import nn

from modules.blocks import Hourglass, AntiAliasInterpolation2d

class MRAARegionPredictor(nn.Module):
    """
    Region predictor from MRAA, which is used for mask prediction.
    """

    def __init__(self,
                 num_regions=10,
                 block_expansion=32,
                 max_features=1024,
                 num_blocks=5,
                 temperature=0.1,
                 scale_factor=0.25,
                 num_channels=3,
                 multipart=False,
                 threshold=0.0001):
        """
        :param num_regions: number of regions (should correspond to number of parts)
        :param block_expansion: size of the first conv block
        :param max_features: maximum number of features
        :param num_blocks: number of blocks in the network
        :param temperature: softmax temperature for regions
        :param num_channels: number of channels in the frame
        :param multipart: return mask with multiple parts or single part
        :param threshold: threshold for quantize soft mask to hard mask
        :param scale_factor: resize the image by this factor, before input
        """
        super(MRAARegionPredictor, self).__init__()
        self.predictor = Hourglass(block_expansion=block_expansion,
                                   in_features=num_channels,
                                   max_features=max_features,
                                   num_blocks=num_blocks)

        self.regions = nn.Conv2d(in_channels=self.predictor.out_filters,
                                 out_channels=num_regions,
                                 kernel_size=(7, 7),
                                 padding=3)

        self.temperature = temperature
        self.scale_factor = scale_factor
        self.multipart = multipart
        self.threshold = threshold

        if self.scale_factor != 1:
            self.down = AntiAliasInterpolation2d(num_channels, self.scale_factor)

        for param in self.parameters():
            param.requires_grad = False

    def load_state_dict(self, state_dict, strict=False):
        """
        Load state dict, but rename properly and don't load AntiAliasInterpolation2d parameters.
        """
        with torch.no_grad():
            self_state = self.state_dict()
            for name, param in state_dict.items():
                if name.startswith('down'):
                    continue
                name = name.replace('encoder.', '').replace('decoder.', '')
                if isinstance(param, nn.Parameter):
                    param = param.data
                self_state[name].copy_(param)

    def convert_regions_to_segmentation_mask(self, regions):
        """
        Convert soft regions to hard segmentation mask with background.

        :param regions: soft segmentation mask[bs, n_parts, h, w]
        :return: hard segmentation mask[bs, n_parts, h, w]
        """
        hard_mask, max_index = regions.max(dim=1, keepdim=True)
        hard_mask = hard_mask > self.threshold
        hard_mask = hard_mask.float()

        if self.multipart:
            num_classes = regions.shape[1]
            regions = torch.argmax(regions, dim=1)
            regions = F.one_hot(regions, num_classes=num_classes)
            regions = torch.permute(regions, (0, 3, 1, 2))
            result_mask = torch.cat([hard_mask * regions, (1 - hard_mask)], dim=1)
        else:
            result_mask = torch.cat([hard_mask, (1 - hard_mask)], dim=1)

        class_weights = 1 / torch.clamp(result_mask.mean(axis=(0, 2, 3)), min=1e-3)
        return result_mask, class_weights

    def forward(self, x):
        if self.scale_factor != 1:
            x = self.down(x)

        feature_map = self.predictor(x)
        prediction = self.regions(feature_map)

        final_shape = prediction.shape
        region = prediction.view(final_shape[0], final_shape[1], -1)
        region = F.softmax(region / self.temperature, dim=2)
        region = region.view(*final_shape)

        return self.convert_regions_to_segmentation_mask(region)


'''
TRIBONACCI NUMBERS

It is a series of numbers where each term is the sum of preceeding 3
numbers of the sequence.

T(n) = T(n-1) + T(n-2) + T(n-3)
'''

trib = []

def tribonacci(n):
    x = n - 3
    if x < 0:
        trib[n] = 0
        return trib[n]
    if not x:
        trib[n] = 1
        return trib[n]
    if trib[n] != -1:
        return trib[n]
    trib[n] = tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)
    return trib[n]

def init():
    for i in range(0, 10000):
        trib.append(-1)

init()
n = int(input("Enter N: "))
print("The tribonacci number at position", n, "is:", tribonacci(n))

'''
INPUT : n = 6
OUTPUT: The tribonacci number at position 6 is: 4
VERIFICATION: The tribonacci series : 0 0 1 1 2 4
'''


"""
1. Clarification
2. Possible solutions
    - Going Forwards
    - Going Backwards
3. Coding
4. Tests
"""

# T=O(n), S=O(1)
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        if not nums: return False
        maxIndex = 0
        for i, num in enumerate(nums):
            if i > maxIndex:
                return False
            maxIndex = max(maxIndex, i + num)
        return True

# T=O(n), S=O(1)
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        if not nums: return False
        goal = len(nums) - 1
        for i in range(len(nums))[::-1]:
            if i + nums[i] >= goal:
                goal = i
        return not goal


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Ed Mountjoy
#
"""
bsub -q small -J interactive -n 1 -R "select[mem>8000] rusage[mem=8000] span[hosts=1]" -M8000 -Is bash
"""

import gzip
import sys

def main():

    # Args
    in_ukb = '/nfs/users/nfs_e/em21/otcoregen/uk_biobank_data/data/phenotypes/tsv/ukb27852.tsv.gz'
    outf = 'temp_ukb27852.extraction.tsv'
    sep = '\t'
    field_prefixes = [
        'f.eid',
        'f.31.',    # Reported sex
        'f.22001.', # Genetic sex
        'f.22006.', # White bristish
        'f.22019.', # Sex chromosome aneuploidy. Karyotypes not XX or XY
        'f.22027.', # Heterozygosity or missingness rate outliers
    ]

    # Get list of column indices where header starts with `field_prefixes`
    col_indices = []
    with gzip.open(in_ukb, 'r') as in_h:
        header = in_h.readline().decode().rstrip().split(sep)
        for i, col in enumerate(header):
            for field_prefix in field_prefixes:
                if col.startswith(field_prefix):
                    col_indices.append(i)
                    break

    # Compare field_prefixes to extracted column header and raise error if any
    # were not found
    header_extracted = [header[i] for i in col_indices]
    for field_prefix in field_prefixes:
        if not any([col.startswith(field_prefix) for col in header_extracted]):
            sys.exit('ERROR: Field prefix "{}" was not found'.format(field_prefix))

    # Extract `col_indices` and write to new file
    with gzip.open(in_ukb, 'r') as in_h:
        with open(outf, 'w') as out_h:
            for line in in_h:
                parts = line.decode().rstrip().split(sep)
                out_row = [parts[i] for i in col_indices]
                out_h.write(sep.join(out_row) + '\n')

    return 0

if __name__ == '__main__':

    main()


import numpy as np
import pickle
import torch
import matplotlib.pyplot as plt
import os
import torch.nn as nn
from torchvision.transforms import RandomRotation
from PIL import Image

class trainLoader():

	def __init__(self, config=None, batch_size=50, epoch_len = 100, seq_len = 8, transform=None, training_path = '/home/mayank/Desktop/GitRepos/crnn-pytorch_mnist/data/processed/training.pt', ip_channels = 1, size=(20,200), Type='train', profiler = None, target_transform = None):

		self.training_path = training_path
		self.abc = '0123456789'
		self.seq_len = seq_len
		self.epoch_len = epoch_len
		self.transform = transform

		self.train_data, self.train_labels = torch.load(training_path)
		self.num_total = len(self.train_labels)
		self.final_size = size
		self.normal_mean = 7
		self.clip = (1,40)
		self.ip_channels = ip_channels
		self.resized_shape = (*size,ip_channels)

		self.target_aspect_ratio = 10

		self.out_dir = 'out'
		self.rotate = RandomRotation(10)

		self.batch_size = batch_size
		self.encoding_to_char = {1: '0', 2:'1', 3:'2', 4:'3', 5:'4', 6:'5', 7:'6', 8:'7', 9:'8', 10:'9'}

	def seed(self):

		np.random.seed(self.config['seed'])
		random.seed(self.config['seed'])
		torch.manual_seed(self.config['seed'])
		torch.cuda.manual_seed(self.config['seed'])
		torch.backends.cudnn.deterministic = True

	def _update_config(self, config):

		self.config = config

	def get_abc(self):
		return self.abc

	def set_mode(self, mode='train'):
		return

	def generate_string(self):
		return ''.join(random.choice(self.abc) for _ in range(self.seq_len))

	def __len__(self):

		return self.epoch_len

	def instance(self, num):

		choices = np.random.randint(0, self.num_total, num)
		sample = self.train_data[choices]

		# Variable Spacing

		final = np.zeros([28, 28*num])

		if self.training_path == 'data/processed/training.pt':
			final[:, 0:28] = self.rotate(Image.fromarray(sample[0].numpy()).convert('L'))
		else:
			final[:, 0:28] = sample[0]
		till_now = 28

		for i in sample[1:]:

			i = Image.fromarray(i.numpy()).convert('L')
			if self.training_path == 'data/processed/training.pt':
				CENTER_DIST = 27 - np.random.randint(0, 14)
				rotated = self.rotate(i)
			else:
				CENTER_DIST = 27
				rotated = i

			final[:, till_now-(28 - CENTER_DIST):till_now-(28 - CENTER_DIST)+28] += rotated
			final[:, till_now-(28 - CENTER_DIST):till_now-(28 - CENTER_DIST)+28][final[:, till_now-(28 - CENTER_DIST):till_now-(28 - CENTER_DIST)+28]>255] = 255
			till_now += CENTER_DIST

		final = final[:, :till_now]

		labels = self.train_labels[choices]+1

		return final.astype(np.uint8), labels

	def batch(self, size):

		# http://www.ravi.io/language-word-lengths

		sizes = np.array([0.1, 0.6, 2.6, 5.2, 8.5, 12.2, 14, 14, 12.6, 10.1, 7.5, 5.2, 3.2, 2, 1, 0.6, 0.3, 0.2, 0.1, 0.1])
		sizes = sizes/np.sum(sizes)
		sizes = np.random.choice(np.arange(2, len(sizes)+2), size=size, p=sizes)

		images, targets = [], []
		for size in sizes:
			sample, target = self.instance(size)
			images.append(sample)
			targets.append(target)

		return images, targets

	def resize(self, to_resize_batch):

		#to_resize_batch: (examples, width, height, channels)
		#upsample requires (batch_size, input_features, width, height)

		final = np.zeros([len(to_resize_batch), self.ip_channels, self.resized_shape[0], self.resized_shape[1]])

		for i, sample in enumerate(to_resize_batch):
			# print(sample.shape)
			c_aspect_ratio = sample.shape[1]//sample.shape[0]
			# print(c_aspect_ratio)

			if c_aspect_ratio > self.target_aspect_ratio:
				#Add along height
				new_h = int(sample.shape[0]*c_aspect_ratio/self.target_aspect_ratio)
				temp = np.zeros((new_h, sample.shape[1]))
				temp[(new_h-sample.shape[0])//2:(new_h-sample.shape[0])//2+sample.shape[0], :] = sample

			elif c_aspect_ratio < self.target_aspect_ratio:
				#Add along width

				new_w = int(sample.shape[1]*self.target_aspect_ratio/c_aspect_ratio)
				temp = np.zeros((sample.shape[0], new_w))
				temp[:, (new_w-sample.shape[1])//2:(new_w-sample.shape[1])//2+sample.shape[1]] = sample
			else:
				temp = sample

			sample = temp[None, None, :, :]
			final[i] = nn.functional.interpolate(torch.FloatTensor(sample), size=(self.resized_shape[0], self.resized_shape[1]), mode='nearest', align_corners=None)

		return final

	def getitem(self, index):

		size = self.batch_size
		images, targets = self.batch(size)
		images = [torch.FloatTensor(np.array(Image.fromarray(img).convert('RGB').resize(size=(int(32*img.shape[1]/img.shape[0]), 32))).transpose(2, 0, 1)) for img in images]

		seq_len = [torch.IntTensor([target_i.shape[0]]) for target_i in targets]

		seq = []
		for i in range(len(targets)):

			seq.append(targets[i].int())

		images = [image.unsqueeze(0) for image in images]

		sample = {"img": images, "seq": seq, "seq_len": seq_len, "aug": True}

		return sample


# -*- coding: utf-8 -*-
""" Tests for the base indicator serializer - corresponding to js/models/indicator/baseIndicator"""

import operator
from factories.indicators_models import RFIndicatorFactory, LevelFactory
from factories.workflow_models import RFProgramFactory
from workflow.models import Program
from indicators.models import Indicator
from indicators.serializers_new import (
    IndicatorBaseSerializer,
    IndicatorRFOrderingSerializer,
    IndicatorWithMeasurementSerializer,
    TierBaseSerializer,
    LevelBaseSerializer,
)
from tola.test.utils import SPECIAL_CHARS

from django import test
from django.db import models
from django.utils import translation

QUERY_COUNT = 1

class TestIndicatorBaseSerializer(test.TestCase):
    def get_indicator_data(self, **kwargs):
        return IndicatorBaseSerializer(Indicator.rf_aware_objects.filter(pk=RFIndicatorFactory(**kwargs).pk), many=True).data[0]

    def test_pk(self):
        data = self.get_indicator_data(pk=143, program=RFProgramFactory())
        self.assertEqual(data['pk'], 143)

    def test_name(self):
        p = RFProgramFactory()
        for name in ['normal name', u'Spécîal Characters', 'asdfg'*99]:
            data = self.get_indicator_data(program=p, name=name)
            self.assertEqual(data['name'], name)

    def test_good_level_pk(self):
        p = RFProgramFactory(tiers=["Tier1", "Tier2"], levels=1, levels__1={'id': 901})
        data = self.get_indicator_data(program=p, level=p.levels.filter(pk=901).first())
        self.assertEqual(data['level_pk'], 901)

    def test_non_rf_level_pk(self):
        data = self.get_indicator_data(program=RFProgramFactory(migrated=False), old_level="Activity")
        self.assertEqual(data['old_level_name'], "Activity")
        self.assertEqual(data['level_pk'], 6)

    def test_non_rf_level_with_level_assigned(self):
        p = RFProgramFactory(migrated=False)
        data = self.get_indicator_data(program=p, level=LevelFactory(program=p, pk=5), old_level="Outcome")
        self.assertEqual(data['old_level_name'], "Outcome")
        self.assertEqual(data['level_pk'], 3)

    def test_translated_old_level(self):
        translation.activate('fr')
        data = self.get_indicator_data(program=RFProgramFactory(migrated=False), old_level="Outcome")
        self.assertEqual(data['old_level_name'], u"Résultat")
        translation.activate('en')

    def test_no_level_pk(self):
        p = RFProgramFactory(tiers=["Tier1", "Tier2"], levels=1, levels__1={'id': 901})
        data = self.get_indicator_data(program=p)
        self.assertEqual(data['level_pk'], None)

    def test_old_level_in_rf_with_level_id(self):
        p = RFProgramFactory(tiers=["Tier1", "Tier2"], levels=1, levels__1={'id': 49})
        data = self.get_indicator_data(program=p, old_level="Output", level=p.levels.filter(pk=49).first())
        self.assertEqual(data['level_pk'], 49)
        self.assertEqual(data['old_level_name'], None)

    def test_old_level_in_rf_no_level_id(self):
        p = RFProgramFactory(tiers=["Tier1", "Tier2"], levels=1, levels__1={'id': 901})
        data = self.get_indicator_data(program=p, old_level="Output")
        self.assertEqual(data['level_pk'], None)
        self.assertEqual(data['old_level_name'], None)

    def test_means_of_verification(self):
        p = RFProgramFactory()
        for means in ["test means", "long "*50, u"Spécîa¬l character means"]:
            data = self.get_indicator_data(program=p, means_of_verification=means)
            self.assertEqual(data['means_of_verification'], means)

class TestIndicatorBaseSerializerQueryCounts(test.TestCase):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.program = RFProgramFactory(tiers=["Tier1", "Tier2"], levels=1, levels__1={'id': 901})
        cls.indicators = [
            RFIndicatorFactory(
                program=cls.program, level_id=901, name=f'{SPECIAL_CHARS}1',
                means_of_verification=f'{SPECIAL_CHARS} means'),
            RFIndicatorFactory(
                program=cls.program, level_id=None, name='no_level_id', means_of_verification=None,
                old_level="Output"
            )
        ]

    def indicator_asserts_one(self, serialized_indicator):
        self.assertEqual(serialized_indicator['pk'], self.indicators[0].pk)
        self.assertEqual(serialized_indicator['name'], f'{SPECIAL_CHARS}1')
        self.assertEqual(serialized_indicator['level_pk'], 901)
        self.assertEqual(serialized_indicator['old_level_name'], None)
        self.assertEqual(serialized_indicator['means_of_verification'], f'{SPECIAL_CHARS} means')
        return True

    def indicator_asserts_two(self, serialized_indicator):
        self.assertEqual(serialized_indicator['pk'], self.indicators[1].pk)
        self.assertEqual(serialized_indicator['name'], 'no_level_id')
        self.assertEqual(serialized_indicator['level_pk'], None)
        self.assertEqual(serialized_indicator['old_level_name'], None)
        self.assertEqual(serialized_indicator['means_of_verification'], None)
        return True

    def indicator_asserts(self, serialized_indicator_data):
        self.assertEqual(len(serialized_indicator_data), 2)
        self.assertTrue(self.indicator_asserts_one(serialized_indicator_data[0]))
        self.assertTrue(self.indicator_asserts_two(serialized_indicator_data[1]))
        return True

    def test_query_count_load_for_program(self):
        with self.assertNumQueries(QUERY_COUNT):
            serialized_indicator_data = IndicatorBaseSerializer.load_for_program(self.program.pk).data
        with self.assertNumQueries(0):
            self.assertTrue(self.indicator_asserts(serialized_indicator_data))

    def test_query_count_load_for_prefetch(self):
        # +1 query to get program information:
        with self.assertNumQueries(QUERY_COUNT+1):
            program = Program.rf_aware_objects.select_related(None).prefetch_related(
                models.Prefetch(
                    'indicator_set',
                    queryset=IndicatorBaseSerializer.get_queryset(),
                    to_attr='prefetch_indicators_test'
                )
            ).get(pk=self.program.pk)
            serialized_indicator_data = IndicatorBaseSerializer(program.prefetch_indicators_test, many=True).data
        with self.assertNumQueries(0):
            self.assertTrue(self.indicator_asserts(serialized_indicator_data))

    def test_query_count_load_for_filtered_prefetch(self):
        # +1 query to get program information:
        with self.assertNumQueries(QUERY_COUNT+1):
            program = Program.rf_aware_objects.select_related(None).prefetch_related(
                models.Prefetch(
                    'indicator_set',
                    queryset=IndicatorBaseSerializer.get_queryset(filters={'level_id__isnull': False}),
                    to_attr='prefetch_indicators_test'
                )
            ).get(pk=self.program.pk)
            serialized_indicator_data = IndicatorBaseSerializer(program.prefetch_indicators_test, many=True).data
        with self.assertNumQueries(0):
            self.assertEqual(len(serialized_indicator_data), 1)
            self.assertTrue(self.indicator_asserts_one(serialized_indicator_data[0]))

class TestIndicatorRFOrderingSerializer(test.TestCase):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.rf_program = RFProgramFactory(
            tiers=['Tier 1', 'Tier 2'], levels=[(1,), ((1,),)], levels__pks=[95, 96])
        cls.levels = sorted(list(cls.rf_program.levels.all()), key=lambda l: l.pk)
        cls.rf_indicators = [
            RFIndicatorFactory(program=cls.rf_program, level=cls.levels[1], pk=496),
            RFIndicatorFactory(program=cls.rf_program, level=cls.levels[0], pk=497),
            RFIndicatorFactory(program=cls.rf_program, level=cls.levels[0], pk=495, old_level='Activity'),
        ]
        cls.non_rf_program = RFProgramFactory(migrated=False)
        cls.non_rf_indicators = [RFIndicatorFactory(
            program=cls.non_rf_program, pk=1000+c, number=number, old_level='Activity'
            ) for c, number in enumerate(['1.5', '1.2', '3.4.5'])] + [
                RFIndicatorFactory(program=cls.non_rf_program, pk=1100, number='1.1', old_level='Outcome'),
                RFIndicatorFactory(program=cls.non_rf_program, pk=1101, number='422', old_level='Outcome'),
            ]

    def test_rf_indicator_data(self):
        level_orders = [1, 0, 0]
        level_ids = [95, 96, 95]
        with self.assertNumQueries(QUERY_COUNT):
            serialized_data = IndicatorRFOrderingSerializer.load_for_program(self.rf_program.pk).data
        with self.assertNumQueries(0):
            self.assertEqual(len(serialized_data), 3)
            serialized_data = sorted(serialized_data, key=operator.itemgetter('pk'))
            for c, serialized_indicator in enumerate(serialized_data):
                self.assertTrue(serialized_indicator['results_framework'])
                self.assertEqual(serialized_indicator['level_pk'], level_ids[c])
                self.assertEqual(serialized_indicator['level_order'], level_orders[c])

    def test_non_rf_indicator_data(self):
        sort_numbers = [3, 2, 4, 0, 1]
        old_level_pks = [6, 6, 6, 3, 3]
        with self.assertNumQueries(QUERY_COUNT):
            serialized_data = IndicatorRFOrderingSerializer.load_for_program(self.non_rf_program.pk).data
        with self.assertNumQueries(0):
            self.assertEqual(len(serialized_data), 5)
            serialized_data = sorted(serialized_data, key=operator.itemgetter('pk'))
            for c, serialized_indicator in enumerate(serialized_data):
                self.assertFalse(serialized_indicator['results_framework'])
                self.assertEqual(serialized_indicator['level_pk'], old_level_pks[c])
                self.assertEqual(serialized_indicator['level_order'], sort_numbers[c])

class TestIndicatorWithMeasurementSerializer(test.TestCase):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.program = RFProgramFactory(tiers=['Tier 1', 'Tier 2'], levels=1)
        context = {
            'program_pk': cls.program.pk,
            'tiers': TierBaseSerializer.load_for_program(cls.program.pk)
        }
        context['levels'] = LevelBaseSerializer.load_for_program(cls.program.pk)
        cls.context = context

    def tearDown(self):
        self.program.indicator_set.all().delete()

    def get_serialized_data(self):
        context = self.context.copy()
        return IndicatorWithMeasurementSerializer.load_for_program(self.program.pk, context=context).data

    def get_indicator_data(self, **kwargs):
        indicator_pk = RFIndicatorFactory(program=self.program, **kwargs).pk
        with self.assertNumQueries(QUERY_COUNT):
            data = {s_i['pk']: s_i for s_i in self.get_serialized_data()}
            return data[indicator_pk]

    def test_unit_of_measure(self):
        for uom in ["Test uom", None, "Long "*26, u"Spécîal Chars"]:
            data = self.get_indicator_data(unit_of_measure=uom)
            self.assertEqual(data['unit_of_measure'], uom)

    def test_is_percent(self):
        data = self.get_indicator_data(unit_of_measure_type=Indicator.NUMBER)
        self.assertEqual(data['is_percent'], False)
        data2 = self.get_indicator_data(unit_of_measure_type=Indicator.PERCENTAGE)
        self.assertEqual(data2['is_percent'], True)

    def test_is_cumulative(self):
        data = self.get_indicator_data(is_cumulative=Indicator.NON_CUMULATIVE)
        self.assertEqual(data['is_cumulative'], Indicator.NON_CUMULATIVE)
        data2 = self.get_indicator_data(is_cumulative=Indicator.CUMULATIVE)
        self.assertEqual(data2['is_cumulative'], Indicator.CUMULATIVE)
        data3 = self.get_indicator_data(is_cumulative=Indicator.NON_SUMMING_CUMULATIVE)
        self.assertEqual(data3['is_cumulative'], Indicator.NON_SUMMING_CUMULATIVE)
        data4 = self.get_indicator_data(is_cumulative=None)
        self.assertEqual(data4['is_cumulative'], None)

    def test_direction_of_change(self):
        data = self.get_indicator_data(direction_of_change=Indicator.DIRECTION_OF_CHANGE_NONE)
        self.assertEqual(data['direction_of_change'], None)
        data2 = self.get_indicator_data(direction_of_change=None)
        self.assertEqual(data2['direction_of_change'], None)
        data3 = self.get_indicator_data(direction_of_change=Indicator.DIRECTION_OF_CHANGE_POSITIVE)
        self.assertEqual(data3['direction_of_change'], "+")
        data4 = self.get_indicator_data(direction_of_change=Indicator.DIRECTION_OF_CHANGE_NEGATIVE)
        self.assertEqual(data4['direction_of_change'], "-")

    def test_baseline(self):
        for baseline, na, result in [
                (None, True, None), ("100", True, None), ("500", False, "500"), ("50%", False, "50%")
            ]:
            data = self.get_indicator_data(baseline=baseline, baseline_na=na)
            self.assertEqual(data['baseline'], result)


"""
Merge two dictionaries into one.
"""
from typing import Dict
import copy

def merge(dict1, dict2):
    # type: (Dict, Dict) -> Dict
    """Recrusively merge two dictionaries.

    The two input dictionaries can have arbitrary depths.
    If there is a conflict, values from dict1 will
    override dict2.

    Args:
        dict1 (dict): input dictionary 1
        dict2 (dict): input dictionary 2

    Returns:
        A new dictionary that combines both inputs.

    """
    ooo = copy.deepcopy(dict2)
    for k in dict1:
        if (k in dict2 and  # resolve conflicts
                isinstance(dict1[k], dict) and
                isinstance(dict2[k], dict)):
            ooo[k] = merge(dict1[k], dict2[k])
        else:  # non-conflicting items or non-dict values
            ooo[k] = dict1[k]
    return ooo


from tensorflow.keras.models import load_model
import data_tools
import os
import numpy as np
import soundfile as sf
import config_params

def createpath(path):
    if not os.path.exists(path):
        os.makedirs(path)

def prediction():

    createpath(config_params.PATH_DIR_TEST_NOISE_dB)
    createpath(config_params.PATH_DIR_TEST_VOICE_dB)
    createpath(config_params.PATH_DIR_TEST_SNR_BASED)
    createpath(config_params.PATH_DIR_TEST_NOISY)
    createpath(config_params.PATH_DIR_TEST_IMAGE_NOISY)
    createpath(config_params.PATH_DIR_TEST_IMAGE_VOICE)
    createpath(config_params.PATH_DIR_TEST_IMAGE_DENOISE)

    list_noise_files = []
    list_voice_files = []
    list_noise_snr_files = []

    print("Loading...")
    # Load noise file paths
    for root, dirs, files in os.walk(config_params.PATH_DIR_TEST_NOISE_PREDICT):
        if len(files) == 0:
            continue

        for f in files:
            files = os.path.join(root, f)
            list_noise_files.append(files)

    # Load voice filenames
    for root, dirs, files in os.walk(config_params.PATH_DIR_TEST_VOICE_PREDICT):
        if len(files) == 0:
            continue

        for f in files:
            files = os.path.join(root, f)
            list_voice_files.append(files)

    list_dB_noise_file = data_tools.scale_dB(
        list_noise_files, config_params.PATH_DIR_TEST_NOISE_dB)
    list_dB_voice_file = data_tools.scale_dB(
        list_voice_files, config_params.PATH_DIR_TEST_VOICE_dB)

    list_noise_files.clear()
    list_voice_files.clear()

    print("Setting snr...")
    SNR = [15, 10, 5, 0, -5, -10]
    for snr in SNR:
        createpath(config_params.PATH_DIR_TEST_SPLIT_NOISY + str(snr) + '/')
        createpath(config_params.PATH_DIR_TEST_SPLIT_VOICE + str(snr) + '/')
        snr_base_noise_file = data_tools.set_snr(
            list_dB_voice_file, list_dB_noise_file, snr, config_params.PATH_DIR_TEST_SNR_BASED)
        list_noise_snr_files.append(snr_base_noise_file)

    print(f'Loading Model: {config_params.PATH_WEIGHTS}')
    loaded_model = load_model(config_params.PATH_WEIGHTS)

    for snr_noise_data in list_noise_snr_files:
        cropped_list_noisy = []
        output_audio_list = []
        snr = snr_noise_data.split('/')[-1].split('_')[0]
        print('SNR:', snr)

        noise = data_tools.audio_files_to_numpy(snr_noise_data)
        voice = data_tools.audio_files_add_to_numpy(list_dB_voice_file)

        print("Mixing...")
        prod_voice, prod_noise, prod_noisy = data_tools.mix_noise_voice(
            voice, noise)

        # sf.write(f'{config_params.PATH_DIR_TEST_NOISY}/{snr}_voice.wav',
        #          prod_voice, config_params.SAMPLE_RATE, 'PCM_24')
        # sf.write(f'{config_params.PATH_DIR_TEST_NOISY}/{snr}_noise.wav',
        #          prod_noise, config_params.SAMPLE_RATE, 'PCM_24')
        sf.write(f'{config_params.PATH_DIR_TEST_NOISY}/{snr}_noisy.wav',
                 prod_noisy, config_params.SAMPLE_RATE, 'PCM_24')

        print("Cropping raw data...")
        cropped_list_noisy.extend(data_tools.split_into_one_second(
            prod_noisy, config_params.PATH_DIR_TEST_SPLIT_NOISY, snr, False))

        cropped_array_noisy = np.array(cropped_list_noisy)

        print("cropped_array_noisy:", cropped_array_noisy.shape)
        fix_length = cropped_array_noisy.shape[1]
        print(config_params.HOP_LENGTH_FFT, fix_length)

        mag_amp_db_noisy,  mag_phase_noisy = data_tools.numpy_audio_to_matrix_spectrogram(
            cropped_array_noisy, config_params.PATH_DIR_TEST_IMAGE_NOISY)

        X_in = data_tools.scaled_in(mag_amp_db_noisy)
        X_pred = loaded_model.predict(X_in)
        inv_sca_X_pred = data_tools.inv_scaled_ou(X_pred)
        X_denoise = mag_amp_db_noisy - inv_sca_X_pred

        print("X_in:", X_in.shape)
        print("X_pred:", X_pred.shape)
        print("inv_sca_X_pred:", inv_sca_X_pred.shape)
        print("X_denoise:", X_denoise.shape)

        audio_denoise_recons = data_tools.matrix_spectrogram_to_numpy_audio(
            X_denoise, mag_phase_noisy, fix_length, config_params.PATH_DIR_TEST_IMAGE_DENOISE)

        sf.write(
            f'{config_params.PATH_DIR_PREDICT_ROOT}/{snr}_{config_params.PATH_PREDICT_OUTPUT_NAME}', audio_denoise_recons, config_params.SAMPLE_RATE, 'PCM_24')

        output_audio_list.append(
            f'{config_params.PATH_DIR_PREDICT_ROOT}/{snr}_{config_params.PATH_PREDICT_OUTPUT_NAME}')

        data_tools.scale_dB(output_audio_list, config_params.PATH_DIR_PREDICT_ROOT, purepath=True)

        print("")

if __name__ == '__main__':
    prediction()


from __future__ import unicode_literals

from django.db import migrations

from django_changeset.models.mixins import RevisionModelMixin

from eric.core.models import disable_permission_checks

def create_maintenance_text(apps, schema_editor):
    RevisionModelMixin.set_enabled(False)
    db_alias = schema_editor.connection.alias
    Content = apps.get_model("cms", "Content")

    with disable_permission_checks(Content):
        # first check if an entry exists
        maintenance_text = Content.objects.using(db_alias).filter(
            slug="maintenance"
        ).first()

        # if no entry exists, we create an "empty" one
        if not maintenance_text:
            Content.objects.using(db_alias).create(
                slug="maintenance",
                title="Maintenance"
            )
    RevisionModelMixin.set_enabled(True)

def remove_maintenance_text(apps, schema_editor):
    db_alias = schema_editor.connection.alias
    Content = apps.get_model("cms", "Content")

    with disable_permission_checks(Content):
        # first check if an entry exists
        maintenance_text = Content.objects.using(db_alias).filter(
            slug="maintenance"
        ).first()

        # if an entry exists, we delete it
        if maintenance_text:
            maintenance_text.delete()

class Migration(migrations.Migration):

    dependencies = [
        ('cms', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_maintenance_text, remove_maintenance_text),
    ]


# calculate bernoulli numbers B_0 ~ B_p on modulo mod
def bernoulli(p, mod):
    rev = [1]*(p+2)
    cur = 1
    for i in range(1, p+1):
        cur = cur * i % mod
        rev[i+1] = cur
    cur = pow(cur * (p+1), mod-2, mod)
    for i in range(p+1, -1, -1):
        rev[i] = (rev[i] * cur) % mod
        cur = cur * i % mod

    B = [0]*(p+1)
    B[0] = 1
    if p > 1:
        B[1] = (-rev[2]) % mod
    for i in range(2, p+1, 2):
        v = 0
        tmp = 1
        for j in range(i):
            v = (v + tmp * B[j]) % mod
            tmp = ((i+1-j) * rev[j+1]) * tmp % mod
        B[i] = (-rev[i+1] * v) % mod
    return B, rev

# calculate ∑_{k=1}^n k^p on modulo mod
def calc(n, p, mod):
    B, rev = bernoulli(p, mod)

    res = 0
    tmp = pow(n, p+1, mod)
    rev_n = pow(n, mod-2, mod)
    for i in range(0, p+1):
        res = (res + tmp * B[i]) % mod
        tmp = (-(p+1-i) * rev[i+1] % mod) * (rev_n * tmp % mod) % mod
    return (res * rev[p+1]) % mod

print(calc(10, 2, 10**9 + 7))
# => "385"
print(calc(100, 200, 10**9 + 7))
# => "710636539"


from datetime import datetime
import sys
import tensorflow as tf
import numpy as np

from model import Model
from dataset import Dataset
from network import *
import tensorflow.contrib.slim as slim

def main():
    if len(sys.argv) != 4:
        print('Usage: python3 finetune.py train_file test_file weight_file')
        return

    train_file = sys.argv[1]
    test_file = sys.argv[2]
    weight_file = sys.argv[3]

    # checkpoint save path
    checkpoint_path = '/path/to/saved/model/vggface16_finetuned.ckpt'
    # writer path
    train_writer_path = '/path/to/logs/train'
    test_writer_path = '/path/to/logs//test'

    # Learning params
    learning_rate_init = 0.001   # 0.001
    decay_steps = 10000
    decay_rate = 0.5

    # Train and dispaly params
    training_iters = 60000
    batch_size = 50         #36
    display_step = 20
    test_step = 1000
    save_step = 1000

    # Network params
    n_classes = 10575
    keep_rate = 0.5  # 1.0

    # Graph input
    x = tf.placeholder(tf.float32, [batch_size, 224, 224, 3])    #input of vgg is 224*224, input of alexnet is 227*227
    y = tf.placeholder(tf.float32, [None, n_classes])
    keep_var = tf.placeholder(tf.float32)

    # Model
    # tf.reset_default_graph()
    pred = Model.vgg16(x, keep_var, n_classes)

    # Loss
    with tf.name_scope('loss'):
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))

    # Optimizer
    global_step = tf.Variable(0, trainable=False, name='global_step')
    with tf.name_scope('learning_rate'):
        learning_rate = tf.train.exponential_decay(learning_rate_init, global_step, decay_steps, decay_rate, staircase=True)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss, global_step=global_step)

    # Evaluation
    correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    # Init
    init = tf.global_variables_initializer()

    # Create a summary to monitor learning_rate, loss, accuracy
    tf.summary.scalar('learning_rate', learning_rate)
    tf.summary.scalar("loss", loss)
    tf.summary.scalar("accuracy", accuracy)

    merged_summary_op = tf.summary.merge_all()

    # Load dataset
    dataset = Dataset(train_file, test_file)

    # Create a saver
    saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)  # ADD

    # Launch the graph
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session() as sess:
        print('Init variable')
        sess.run(init)

        print("Model restored from file: %s" % weight_file)
        load_with_skip(weight_file, sess, ['fc8'])  # Skip weights from fc8
        print('Start training')

        # write logs to Tensorboard
        train_writer = tf.summary.FileWriter(train_writer_path, sess.graph)
        test_writer = tf.summary.FileWriter(test_writer_path, sess.graph)

        step = 1
        while step < training_iters:
            batch_xs, batch_ys = dataset.next_batch(batch_size, 'train')
            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_var: keep_rate})

            summary = sess.run(merged_summary_op, feed_dict={x: batch_xs, y: batch_ys, keep_var: keep_rate})
            # Write logs at every iteration
            train_writer.add_summary(summary, step)

            # Display testing status
            if step % test_step == 0:
                test_acc = 0.
                test_count = 0
                t_loss = 0
                for _ in range(dataset.test_size // batch_size):
                    batch_tx, batch_ty = dataset.next_batch(batch_size, 'test')
                    acc = sess.run(accuracy, feed_dict={x: batch_tx, y: batch_ty, keep_var: 1.})
                    b_loss = sess.run(loss, feed_dict={x: batch_tx, y: batch_ty, keep_var: 1.})
                    summary = sess.run(merged_summary_op, feed_dict={x: batch_tx, y: batch_ty, keep_var: 1.})    # ADD
                    test_writer.add_summary(summary, step)                                              # ADD
                    test_acc += acc
                    t_loss += b_loss
                    test_count += 1
                test_acc /= test_count
                t_loss/= test_count
                print('{} Iter {}: Testing Accuracy = {:.4f}, Test loss = {:.4f}'.format(datetime.now(), step, test_acc,t_loss), file=sys.stderr)

            # Display training status
            if step % display_step == 0:
                acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_var: 1.})
                batch_loss = sess.run(loss, feed_dict={x: batch_xs, y: batch_ys, keep_var: 1.})
                lr = sess.run(learning_rate)
                print('{} Iter {}: Training Loss = {:.4f}, Accuracy = {:.4f}, lr = {:.4f}'.format(datetime.now(), step, batch_loss, acc, lr), file=sys.stderr)

            # Save model
            if step % save_step == 0:
                saver.save(sess, checkpoint_path, global_step=step)

            step += 1
        print('Finish!')

if __name__ == '__main__':
    main()


from diagrams import Cluster, Diagram
from diagrams.gcp.analytics import BigQuery, DataFlow, PubSub
from diagrams.generic.blank import Blank
from diagrams.custom import Custom
import random
from random import shuffle
import os
from math import floor, log, ceil
import requests
import shutil
from os.path import exists
from player import Player, getimage

def subtract(lst1, lst2):
    return [x for x in lst2 if x not in lst1]

# def find_instance(list, value, index):
#     return [z for z, n in enumerate(list) if n == value][index]

def len_group(numPlayers, group):
    return 2**ceil(log(numPlayers, 2))-1-2*(group-1)

def createbracket(players):
    question="https://www.citypng.com/public/uploads/small/11664478536ju7umc1lgtvjg5pkqxzszbnnsiidksg2azldiuudv5lm2m4qjror6fwyabkot9ludlybku4g1pptb3qxm1sosuj55vtdldgrtyqu.png"
    for i in range(len(players)):
        players[i] = Player(players[i][0], players[i][1], players[i][2])
    numPlayers = len(list(players))
    n = floor(log(numPlayers, 2))
    k = numPlayers - 2**n
    group1 = []
    powerOf2 = log(numPlayers, 2).is_integer()
    if powerOf2:
        group1 = players
    else:
        for i in range(2*k):
            group1.append(players[i])
        group2 = subtract(group1, players)

        while group2.count(False) < k:
            group2.insert(group2.count(False)*2+1, False)

    with Diagram("bracket", show=False):
        if powerOf2:
            with Cluster("group 1"):
                actualGroup1=[]
                for i in range(len(group1)):
                    actualGroup1.append(group1[i].make_box())

            #PAST ROUND 2
            newGroups=int(log(len(group1), 2))
            futureGroups=[]
            actualFutureGroups=[]
            for i in range(newGroups):
                with Cluster("group "+str(2+i)):
                    futureGroups.append([])
                    actualFutureGroups.append([])
                    if i == 0:
                        currentPlayers=int(len(group1)/2)
                    else:
                        currentPlayers=int(len(futureGroups[i-1])/2)#(2**(i+1)))
                    numBlanks = len_group(numPlayers, 1+i)-currentPlayers

                    if i==0:
                        for j in range(int(len(actualGroup1)/2)):
                            try:
                                winner=random.choice([group1[2*j], group1[2*j+1]])
                                futureGroups[i].append(winner)
                                actualFutureGroups[i].append(winner.make_box())
                                # if group1[2*j].get_wins() > group1[2*j+1].get_wins():
                                #     futureGroups[i].append(group1[2*j])
                                #     actualFutureGroups[i].append(group1[2*j].make_box())

                                # elif group1[2*j].get_wins() < group1[2*j+1].get_wins():
                                #     futureGroups[i].append(group1[2*j+1])
                                #     actualFutureGroups[i].append(group1[2*j+1].make_box())

                                # elif group1[2*j].get_wins() == group1[2*j+1].get_wins():
                                #     futureGroups[i].append(False)
                                #     actualFutureGroups[i].append(Custom("", getimage(question)))

                            except Exception as e:
                                futureGroups[i].append(False)
                                actualFutureGroups[i].append(Custom("", getimage(question)))

                            if j!=int(len(actualGroup1)/2-1):
                                for k in range(int(numBlanks/(currentPlayers-1))):
                                    Blank("")
                    else:
                        for j in range(int(len(actualFutureGroups[i-1])/2)):
                            try:
                                winner=random.choice([futureGroups[i-1][2*j], futureGroups[i-1][2*j+1]])
                                futureGroups[i].append(winner)
                                actualFutureGroups[i].append(winner.make_box())
                                # if futureGroups[i-1][2*j].get_wins() > futureGroups[i-1][2*j+1].get_wins():
                                #     futureGroups[i].append(futureGroups[i-1][2*j])
                                #     actualFutureGroups[i].append(futureGroups[i-1][2*j].make_box())

                                # elif futureGroups[i-1][2*j].get_wins() < futureGroups[i-1][2*j+1].get_wins():
                                #     futureGroups[i].append(futureGroups[i-1][2*j+1])
                                #     actualFutureGroups[i].append(futureGroups[i-1][2*j+1].make_box())

                                # elif futureGroups[i-1][2*j].get_wins() == futureGroups[i-1][2*j+1].get_wins():
                                #     futureGroups[i].append(False)
                                #     actualFutureGroups[i].append(Custom("", getimage(question)))
                            except:
                                futureGroups[i].append(False)
                                actualFutureGroups[i].append(Custom("", getimage(question)))

                            if j!=int(len(actualFutureGroups[i-1])/2-1):
                                if currentPlayers!=1:
                                    for k in range(int(numBlanks/(currentPlayers-1))):
                                        Blank("")

                if i==0:
                    for j in range(int(len(actualGroup1)/2)):
                        [actualGroup1[2*j], actualGroup1[2*j+1]] >> actualFutureGroups[0][j]
                else:
                    for j in range(int(len(futureGroups[i-1])/2)):
                        [actualFutureGroups[i-1][2*j], actualFutureGroups[i-1][2*j+1]] >> actualFutureGroups[i][j]

        else:
            with Cluster("group 2"):
                actualGroup2=[]
                g2len=0
                l=0
                for i in range(len(group2)):
                    if group2[i]:
                        actualGroup2.append(group2[i].make_box())
                        group2[i].win()
                        g2len+=1
                    else:
                        winner=random.choice([group1[2*l], group1[2*l+1]])
                        group2[i]=winner
                        actualGroup2.append(winner.make_box())
                        # if group1[2*l].get_wins()>group1[2*l+1].get_wins():
                        #     group2[i] = group1[2*l]#group2.append(group1[2*l])
                        #     actualGroup2.append(group1[2*l].make_box())
                        # elif group1[2*l].get_wins()<group1[2*l+1].get_wins():
                        #     group2[i] = group1[2*l+1]#group2.append(group1[2*l+1])
                        #     actualGroup2.append(group1[2*l+1].make_box())
                        # else:
                        #     actualGroup2.append(Custom("", getimage(question)))
                        l+=1
                    if i<(len(group2)-1):
                        Blank("")

            actualGroup1=[]
            with Cluster("group 1"):
                l=g2len
                for i in range(int(len(group1)/2)):
                    if l!=0 and i!=0:
                        # for j in range(2):
                        #     Blank("")
                        l-=1
                    actualGroup1.append(group1[2*i].make_box())
                    actualGroup1.append(group1[2*i+1].make_box())
            for i in range(k):
                if g2len > 0:
                    [actualGroup1[2*i], actualGroup1[2*i+1]] >> actualGroup2[2*i+1]
                    g2len -= 1
                    x=i+1
                else:
                    [actualGroup1[2*i], actualGroup1[2*i+1]] >> actualGroup2[x+i]

            #PAST ROUND 2
            newGroups=int(log(len(group2), 2))
            futureGroups=[]
            actualFutureGroups=[]
            for i in range(newGroups):
                with Cluster("group "+str(3+i)):
                    futureGroups.append([])
                    actualFutureGroups.append([])
                    currentPlayers=int(len(group2)/(2**(i+1)))
                    numBlanks = len_group(numPlayers, 2+i)-currentPlayers

                    if i==0:
                        for j in range(int(len(actualGroup2)/2)):
                            try:
                                winner=random.choice([group2[2*j], group2[2*j+1]])
                                futureGroups[i].append(winner)
                                actualFutureGroups[i].append(winner.make_box())
                                # if group2[2*j].get_wins() > group2[2*j+1].get_wins():
                                #     futureGroups[i].append(group2[2*j])
                                #     actualFutureGroups[i].append(group2[2*j].make_box())

                                # elif group2[2*j].get_wins() < group2[2*j+1].get_wins():
                                #     futureGroups[i].append(group2[2*j+1])
                                #     actualFutureGroups[i].append(group2[2*j+1].make_box())

                                # elif group2[2*j].get_wins() == group2[2*j+1].get_wins():
                                #     futureGroups[i].append(False)
                                #     actualFutureGroups[i].append(Custom("", getimage(question)))

                            except Exception as e:
                                futureGroups[i].append(False)
                                actualFutureGroups[i].append(Custom("", getimage(question)))

                            if j!=int(len(actualGroup2)/2-1):
                                for k in range(int(numBlanks/(currentPlayers-1))):
                                    Blank("")
                    else:
                        for j in range(int(len(actualFutureGroups[i-1])/2)):
                            try:
                                winner=random.choice([futureGroups[i-1][2*j], futureGroups[i-1][2*j+1]])
                                futureGroups[i].append(winner)
                                actualFutureGroups[i].append(winner.make_box())
                                # if futureGroups[i-1][2*j].get_wins() > futureGroups[i-1][2*j+1].get_wins():
                                #     futureGroups[i].append(futureGroups[i-1][2*j])
                                #     actualFutureGroups[i].append(futureGroups[i-1][2*j].make_box())

                                # elif futureGroups[i-1][2*j].get_wins() < futureGroups[i-1][2*j+1].get_wins():
                                #     futureGroups[i].append(futureGroups[i-1][2*j+1])
                                #     actualFutureGroups[i].append(futureGroups[i-1][2*j+1].make_box())

                                # elif futureGroups[i-1][2*j].get_wins() == futureGroups[i-1][2*j+1].get_wins():
                                #     futureGroups[i].append(False)
                                #     actualFutureGroups[i].append(Custom("", getimage(question)))
                            except:
                                futureGroups[i].append(False)
                                actualFutureGroups[i].append(Custom("", getimage(question)))

                if i==0:
                    for j in range(int(len(actualGroup2)/2)):
                        [actualGroup2[2*j], actualGroup2[2*j+1]] >> actualFutureGroups[0][j]
                else:
                    for j in range(int(len(futureGroups[i-1])/2)):
                        [actualFutureGroups[i-1][2*j], actualFutureGroups[i-1][2*j+1]] >> actualFutureGroups[i][j]

    i=0
    while exists("temp"+str(i)+".png"):
        os.remove("temp"+str(i)+".png")
        i+=1

# createbracket(
#     [("black square1", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0),
#     ("black square2", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0),
#     ("black square3", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0),
#     ("black square4", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0),
#     ("black square5", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0),
#     ("black square5", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0),
#     ("black square6", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0),
#     ("black square7", "https://lh3.googleusercontent.com/9tdJGxGMuvgof_Jh0SFU7r423bKuMIP9wSnOu-sy5RffCwlhBYvugvXfUz-xhpsCfg=h36", 0)]
# )