from __future__ import unicode_literals

import sys
import argparse
import re
import datetime
import logging
from six import text_type

import cereconf
import Cerebrum.logutils
from Cerebrum import Errors
from Cerebrum.utils import date_compat
from Cerebrum.Utils import Factory
from Cerebrum.modules import Email
from Cerebrum.modules import PosixGroup

logger = logging.getLogger(__name__)
db = co = group_creator = dryrun = None

def get_email_target_and_address(address):
    ea = Email.EmailAddress(db)
    try:
        ea.find_by_address(address)
    except Errors.NotFoundError:
        return (None, None)
    et = Email.EmailTarget(db)
    et.find(ea.email_addr_target_id)
    return (et, ea)

def delete_email_address(address):
    et, ea = get_email_target_and_address(address)
    if et is None:
        logger.debug("Would delete <%s>", address)
        return
    logger.info("Deleting <%s>", address)

    # We can't delete this EA, if there is an epat attached to it.
    # Before we can drop ea, remove epat (or we'll get constraint violation
    # from the db)
    try:
        epat = Email.EmailPrimaryAddressTarget(db)
        epat.find(ea.email_addr_target_id)
        epat.delete()
        logger.debug("Deleted *primary* address <%s>", address)
    except Errors.NotFoundError:
        pass

    ea.delete()
    for r in et.get_addresses():
        logger.info("There are addresses left")
        return
    logger.debug("Deleting target as well")
    et.delete()

def update_email_address(address, group):
    et, ea = get_email_target_and_address(address)
    if et:
        if et.email_target_type != co.email_target_multi:
            logger.error("Wrong existing target type for <%s>", address)
            return
        if et.email_target_entity_id == group.entity_id:
            logger.debug("Up-to-date <%s>", address)
            return
        et.email_target_entity_id = group.entity_id
        logger.info("Updating <%s>", address)
        et.write_db()
    else:
        et = Email.EmailTarget(db)
        try:
            et.find_by_target_entity(group.entity_id)
            logger.info("Added address <%s>", address)
        except Errors.NotFoundError:
            et.populate(co.email_target_multi,
                        target_entity_type=co.entity_group,
                        target_entity_id=group.entity_id)
            et.write_db()
            logger.info("Created <%s>", address)
        ed = Email.EmailDomain(db)
        lp, dom = address.split('@')
        ed.find_by_domain(dom)
        ea = Email.EmailAddress(db)
        ea.populate(lp, ed.entity_id, et.entity_id)
        ea.write_db()

def find_leaf(group):
    target_group = None
    for member in group.search_members(group_id=group.entity_id):
        member_type = int(member["member_type"])
        member_id = int(member["member_id"])

        if member_type == co.entity_account:
            return group
        elif member_type == co.entity_group:
            g = get_group(member_id)
            if re.search(r'-sek(:\d|$)', g.group_name):
                continue
            # if there was another group member, we shouldn't recurse.
            if target_group:
                return group
            else:
                target_group = g
    # if group passed as argument has _no_ members, return None
    if target_group is None:
        return target_group
    else:
        return find_leaf(target_group)
# end find_leaf

def sync_email_address(address, group):
    if group is None:
        delete_email_address(address)
    else:
        update_email_address(address, group)

# the horrors, the horrors.
# http://www.uio.no/for-ansatte/arbeidsstotte/sta/enheter/mn/institutter/ifi/epostlister/

def shorten_course_name(course):
    fgname = course
    fgname = fgname.replace("matinf", "mi", 1)
    fgname = fgname.replace("infmat", "mi", 1)
    fgname = fgname.replace("infverk", "iv", 1)
    fgname = fgname.replace("humit", "hi", 1)
    # there are currently no MODxxxx courses, perhaps they won't return.
    # so I reuse the "im" abbreviation for MED-INFxxxx
    fgname = fgname.replace("medinf", "im", 1)
    fgname = fgname.replace("mod", "im", 1)
    fgname = fgname.replace("infps", "ip", 1)
    # INFxxx used to stay as infxxx, but there won't be any more three digit
    # codes, so we change it to just "i" unconditionally.
    fgname = fgname.replace("inf", "i", 1)
    fgname = fgname.replace("in", "in", 1)
    fgname = fgname.replace("dig", "id", 1)
    fgname = fgname.replace("tool", "it", 1)
    return fgname[:6]

def convert_activitynumber(act):
    # support for TVI has not been added, it will probably not return

    if act == 'grl':
        return 'g'      # the filegroup for all the teachers
    elif act in range(200, 300):
        delim = 'p'     # activity for profession students
        act = act % 100
    elif act in range(300, 400):
        delim = 'm'     # activity MOD students
        act = act % 100
    else:
        delim = '-'
    act -= 1
    if act < 26:
        return delim + "%c" % (ord('a') + act)
    else:
        return "%c%c" % (ord('a') + act/26, ord('a') + act % 26)

def make_filegroup_name(course, act, names):
    """Return a name which can be used for a filegroup.  If the group
    would duplicate a different filegroup, return None.

    """
    # If there is both a 3000 and a 4000 course, they are guaranteed to
    # have the same teachers.  This could be a HUMIT course, too.
    m = re.search(r'(.*)4(\d{3})(\D*)$', course)
    if m:
        if m.group(1) + '3' + m.group(2) + m.group(3) in names:
            return None

    # HUMITxxxxMN and HUMITxxxx are the same.  We prefer the long
    # version with 'MN' since the teachers happen to be registered
    # there at the time of writing (FIXME).
    m = re.search(r'^(humit\d{4})$', course)
    if m:
        if m.group(1) + 'mn' in names:
            return None
    return names[course] + convert_activitynumber(act)

def add_members(gname, new_members):
    """Add the new members to existing group gname.

    This is not a sync, so other members are left alone. The new_members is a
    dict with group_id as key. The value for each key is arbitrary."""

    group = get_group(gname)
    for memb in group.search_members(group_id=group.entity_id,
                                     member_type=co.entity_group):
        member_id = int(memb["member_id"])
        if member_id in new_members:
            del new_members[member_id]
    for memb in new_members.keys():
        logger.debug("Adding %s to %s", get_group(memb).group_name, gname)
        if not group.has_member(memb):
            group.add_member(memb)
    group.write_db()
# end add_members

def sync_filegroup(fgname, group, course, act):
    posix_group = PosixGroup.PosixGroup(db)
    # Make the group last a year or so.  To avoid changing the database
    # every night, we only change expire date if it has less than three
    # month to live.
    expdate = datetime.date.today() + datetime.timedelta(days=365)
    refreshdate = expdate - datetime.timedelta(days=31*3)
    try:
        fgroup = get_group(fgname)
    except Errors.NotFoundError:
        logger.info("Created new file group %s", fgname)
        posix_group.populate(
            creator_id=group_creator,
            visibility=co.group_visibility_all,
            name=fgname,
            description="Gruppelærere %s gruppe %s" % (course.upper(), act),
            expire_date=expdate,
            group_type=co.group_type_ifi_auto,
        )
        posix_group.write_db()
    else:
        posix_group.find(fgroup.entity_id)
        # make sure the group is alive
        expire_date = date_compat.get_date(posix_group.expire_date)
        if expire_date and expire_date < refreshdate:
            logger.info(
                "Extending life of %s from %s to %s",
                fgname,
                expire_date,
                refreshdate
            )
            posix_group.expire_date = expdate
            posix_group.write_db()

    uptodate = False
    for row in posix_group.search_members(group_id=posix_group.entity_id,
                                          member_filter_expired=False):
        member_type = int(row["member_type"])
        member_id = int(row["member_id"])
        if member_type != co.entity_group:
            logger.info("Removing member %d from %s", member_id, fgname)
            posix_group.remove_member(member_id)
        elif member_id != group.entity_id:
            logger.info("Removing group member %d from %s", member_id, fgname)
            posix_group.remove_member(member_id)
        else:
            uptodate = True
    if not uptodate:
        logger.info("Adding %s to %s", group.group_name, fgname)
        if not posix_group.has_member(group.entity_id):
            posix_group.add_member(group.entity_id)

    # finally check the spread.  we leave any additionally added spreads
    # alone.
    uptodate = False
    for r in posix_group.get_spread():
        if int(r['spread']) == int(co.spread_ifi_nis_fg):
            uptodate = True
            break
    if not uptodate:
        logger.info("Adding NIS_fg@ifi to %s", fgname)
        posix_group.add_spread(co.spread_ifi_nis_fg)
    return posix_group

def process_groups(super, fg_super):
    # make a note of what filegroups are automatically maintained
    auto_fg = {}
    try:
        fg_super_gr = get_group(fg_super)
    except Errors.NotFoundError:
        # first time we run this
        fg_super_gr = Factory.get('Group')(db)
        fg_super_gr.populate(
            creator_id=group_creator,
            visibility=co.group_visibility_internal,
            name=fg_super,
            description=("Ikke-eksporterbar gruppe.  Hvilke "
                         "filgrupper som er automatisk opprettet som "
                         "følge av Ifi-automatikk"),
            group_type=co.group_type_ifi_auto,
        )
        fg_super_gr.write_db()
    else:
        for row in fg_super_gr.search_members(group_id=fg_super_gr.entity_id,
                                              member_type=co.entity_group,
                                              member_filter_expired=False):
            auto_fg[int(row["member_id"])] = True

    # fetch super group's members and update accordingly
    todo = {}
    vortex_access = {}
    # short_name contains the shortened name for every known course
    short_name = {}
    start_group = get_group(super)
    for row in start_group.search_members(group_id=start_group.entity_id,
                                          member_type=co.entity_group,
                                          member_filter_expired=False):
        member_id = int(row["member_id"])
        group = get_group(member_id)
        if group.group_name.startswith(('sinf', 'sin')):
            continue
        course = act = None
        m = re.match(r'g(\w+)-(\d+)$', group.group_name)
        if m:
            course = m.group(1)
            act = int(m.group(2))
            # activity 0 is the course itself
            if act == 0:
                vortex_access[member_id] = True
            # this group often has a single member which is a
            # different group, so get rid of needless indirection.
            leaf = find_leaf(group)
        else:
            m = re.match(r'g(\w+)$', group.group_name)
            if m:
                course = m.group(1)
                act = 'grl'
            # we don't want to recurse in this case, we might end up
            # with a leaf node which has already been assigned a
            # different e-mail address.  this would trigger a
            # constraint.
            #
            # FIXME: handle the case where a single e-mail target is
            # in charge of more than one group.
            leaf = get_group(group.group_name)
        if course:
            sync_email_address("%s-%s@ifi.uio.no" % (course, act), leaf)
            if course not in short_name:
                short_name[course] = shorten_course_name(course)
            if leaf and act:
                todo["%s-%s" % (course, act)] = (course, act, leaf)

    for course, act, group in todo.values():
        fgname = make_filegroup_name(course, act, short_name)
        if fgname is None:
            logger.debug("Skipping %s-%s, it is an alias", course, act)
            continue
        fgroup = sync_filegroup(fgname, group, course, act)
        if fgroup.entity_id in auto_fg:
            del auto_fg[fgroup.entity_id]
        else:
            logger.info("New automatic filegroup %s for %s-%s",
                        fgname, course, act)

            if not fg_super_gr.has_member(fgroup.entity_id):
                fg_super_gr.add_member(fgroup.entity_id)

    add_members("ifivtx", vortex_access)

    # the groups in auto_fg are obsolete, and we remove all members.  we
    # will however keep the PosixGroup around, since the files still
    # exist on disk, and it is painful if the gid changes every time a
    # course is held (they are usually held either during Spring or
    # Autumn, so half the year they are invalid).
    for fgname in auto_fg:
        fgroup = get_group(fgname)
        for row in fgroup.search_members(group_id=fgroup.entity_id,
                                         member_filter_expired=False):
            logger.info("Remove %s %d from obsolete filegroup %s",
                        co.EntityType(row["member_type"]),
                        row["member_id"], fgroup.group_name)
            fgroup.remove_member(row["member_id"])

        fgroup.write_db()
# end process_groups

def get_group(id):
    gr = Factory.get('Group')(db)
    if isinstance(id, text_type):
        gr.find_by_name(id)
    else:
        gr.find(id)
    return gr

def get_account(name):
    ac = Factory.get('Account')(db)
    ac.find_by_name(name)
    return ac

def main():
    global db, co, group_creator, dryrun

    db = Factory.get('Database')()
    db.cl_init(change_program='ifi_auto')
    co = Factory.get('Constants')(db)

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter,
        description="""Usage: ifi_auto.py [options]
        Update e-mail addresses and filegroups associated with courses
        taught at Deptartment of Informatics.""")
    parser.add_argument('-d', '--dryrun',
                        help="Don't commit changes to database",
                        action='store_true',
                        dest='dryrun',
                        default=False)

    Cerebrum.logutils.options.install_subparser(parser)
    args = parser.parse_args()
    Cerebrum.logutils.autoconf('cronjob', args)
    supergroup = "internal:uio.no:fs:{autogroup}"
    fg_supergroup = "internal:uio.no:fs:{ifi_auto_fg}"
    group_creator = get_account(cereconf.INITIAL_ACCOUNTNAME).entity_id
    process_groups(supergroup, fg_supergroup)
    if not args.dryrun:
        logger.debug("commit...")
        db.commit()
    logger.info("All done")

if __name__ == '__main__':
    main()


import random

import pyramid.httpexceptions as httpexc
from pyramid.view import view_config
import sqlalchemy as sqla

from asb import db
import asb.forms

empty_bulletin_messages = [
    'The bulletin is empty for the time being.  Someone has arranged all the '
         'thumbtacks into a smiling {pokemon} face.',
    'The bulletin is empty for the time being... aside from a gaudy ad '
        'exclaiming "Teach Your {pokemon} to DANCE!", anyway.',
    'The bulletin is empty for the time being, except for a lonely "lost '
        '{pokemon}" poster up in the corner.  "We miss Cupcake – last seen '
        'Tuesday night – call if found!"',
    'The bulletin is empty for the time being, ignoring the six identical '
        'flyers taking advantage of the space.  "Discover this one weird '
        'trick invented by a local {pokemon} — Nurse Joys everywhere HATE '
        'her!"'
]

bank_state_labels = [
    ('approved', 'approved'),
    ('denied', 'denied'),
    ('from-mod', 'manually added by a mod')
]

(max_pokemon,) = db.DBSession.query(sqla.func.max(db.PokemonSpecies.id)).one()

def empty_bulletin_message():
    """Return a silly message for when the trainer/mod bulletin is empty."""

    pokemon = (
        db.DBSession.query(db.PokemonSpecies)
        .get(random.randrange(1, max_pokemon + 1))
    )

    return random.choice(empty_bulletin_messages).format(pokemon=pokemon.name)

@view_config(context=Exception, renderer='/error.mako')
def error(error, request):
    """Return a generic error page for an arbitrary uncaught exception."""

    request.response.status_int = 500

    return {'status': '500 Internal Server Error', 'message': None}

@view_config(context=httpexc.HTTPError, renderer='/error.mako')
def error_specific(error, request):
    """Return a more helpful error page for an uncaught HTTPError."""

    request.response.status_int = error.code

    return {
        'status': '{} {}'.format(error.code, error.title),
        'message': '{}  (Detail: {})'.format(error.explanation, error.detail)
    }

@view_config(route_name='home', renderer='/home.mako')
def home(context, request):
    """The home page."""

    trainer = request.user
    stuff = {
        'empty_bulletin_message': empty_bulletin_message,
        'news': db.DBSession.query(db.NewsPost)
                .order_by(db.NewsPost.post_time.desc())
                .limit(5)
                .all()
    }

    if request.has_permission('account.validate', trainer):
        stuff['bulletin'] = [
            ('Your account still needs to be validated', '/validate')
        ]
    elif trainer is not None:
        bulletin = []

        # Check if they're eligible for any promotions
        for promotion in trainer.promotions:
            if promotion.end_date is not None:
                message = 'Check out the {0} — ends {1}!'.format(
                    promotion.name, promotion.end_date.strftime('%Y %B %d')
                )
            else:
                message = 'Check out the {0}!'.format(promotion.name)

            path = '/pokemon/buy#{0}'.format(promotion.identifier)
            bulletin.append((message, path))

        # Check if any of their Pokémon need their forms chosen
        form_uncertain_pokemon = (
            db.DBSession.query(db.Pokemon)
            .filter_by(trainer_id=trainer.id, form_uncertain=True)
            .all()
        )

        for pokemon in form_uncertain_pokemon:
            bulletin.append((
                "{}'s form needs to be chosen".format(pokemon.name),
                request.resource_path(pokemon, 'edit')
            ))

        # Check if any of their bank transactions have been approved/denied
        transaction_counts = dict(
            db.DBSession.query(db.BankTransaction.state, sqla.func.count('*'))
            .filter_by(trainer_id=trainer.id, is_read=False)
            .filter(db.BankTransaction.state != 'pending')
            .group_by(db.BankTransaction.state)
        )

        transactions = sum(transaction_counts.values())

        if transactions:
            breakdown = ', '.join(
                '{0} {1}'.format(transaction_counts[identifier], label)
                for (identifier, label) in bank_state_labels
                if identifier in transaction_counts
            )

            message = ('You have {n} new bank notification{s} ({breakdown})'
                       .format(n=transactions, breakdown=breakdown,
                               s='s' if transactions > 1 else ''))

            bulletin.append((message, '/bank#recent'))

        # Check if they have any gifts to claim
        for gift in trainer.pending_gifts():
            bulletin.append((
                'You have a gift from {0}!'.format(gift.lots[0].sender.name),
                request.resource_path(gift.__parent__, gift.__name__)
            ))

        stuff['bulletin'] = bulletin

    # Find stuff to display on the Mod Bulletin, if applicable
    if any(role in ['mod', 'admin'] for role in request.effective_principals):
        # For now, we'll just assume that the only way the user has any mod
        # permissions is if they're a mod, so we don't need to check individual
        # permissions

        mod_stuff = []

        # See if there are any pending bank transactions
        pending_transactions = (
            db.DBSession.query(db.BankTransaction)
            .filter_by(state='pending')
            .filter(db.BankTransaction.trainer_id != trainer.id)
            .count()
        )

        if pending_transactions:
            if pending_transactions == 1:
                message = 'There is 1 pending bank transaction to approve'
            else:
                message = ('There are {} pending bank transactions to approve'
                    .format(pending_transactions))

            mod_stuff.append((message, '/bank/approve'))

        # See if there are any battles to approve
        pending_battles = (
            db.DBSession.query(db.Battle)
            .filter_by(needs_approval=True)
            .all()
        )

        # Count up the ones they can actually approve, i.e. weren't involved in
        # XXX Filter those out in the query?
        pending_battles = sum(
            1 for battle in pending_battles
            if request.has_permission('battle.approve', battle)
        )

        if pending_battles:
            if pending_battles == 1:
                message = 'There is 1 closed battle awaiting approval'
            else:
                message = ('There are {} closed battles awaiting approval'
                    .format(pending_battles))

            mod_stuff.append((message, '/battles#waiting'))

        stuff['mod_stuff'] = mod_stuff

    return stuff


import pandas as pd
import plotly.graph_objects as go

df = pd.read_csv('../stats/stats.csv')

fig = go.Figure(go.Scatter(x = df['Name'], y = df['@Conditional'],
                  name='Number of @Conditional'))

fig.update_layout(title='Apple Share Prices over time (2014)',
                   plot_bgcolor='rgb(230, 230,230)',
                   showlegend=True)

fig.show()


import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
import torch.multiprocessing as mp
import time
import sys
sys.path.append(".")
from args.config import default_params as params

class ActorCritic(nn.Module):
    def __init__(self):
        super(ActorCritic, self).__init__()
        self.fc1 = nn.Linear(4, 256)
        self.fc_pi = nn.Linear(256, 2)
        self.fc_v = nn.Linear(256, 1)

    def pi(self, x, softmax_dim=0):
        x = F.relu(self.fc1(x))
        x = self.fc_pi(x)
        prob = F.softmax(x, dim=softmax_dim)
        return prob

    def v(self, x):
        x = F.relu(self.fc1(x))
        v = self.fc_v(x)
        return v

def train(global_model, rank, learning_rate, gamma, max_train_ep,
          update_interval):
    local_model = ActorCritic()
    local_model.load_state_dict(global_model.state_dict())

    optimizer = optim.Adam(global_model.parameters(), lr=learning_rate)

    env = gym.make(params['gym_env'])

    for n_epi in range(max_train_ep):
        done = False
        s = env.reset()
        while not done:
            s_lst, a_lst, r_lst = [], [], []
            for t in range(update_interval):
                prob = local_model.pi(torch.from_numpy(s).float())
                m = Categorical(prob)
                a = m.sample().item()
                s_prime, r, done, info = env.step(a)

                s_lst.append(s)
                a_lst.append([a])
                r_lst.append(r)

                s = s_prime
                if done:
                    break

            s_final = torch.tensor(s_prime, dtype=torch.float)
            R = 0.0 if done else local_model.v(s_final).item()
            td_target_lst = []
            for reward in r_lst[::-1]:
                R = gamma * R + reward
                td_target_lst.append([R])
            td_target_lst.reverse()

            s_batch, a_batch, td_target = torch.tensor(s_lst, dtype=torch.float), torch.tensor(a_lst), \
                torch.tensor(td_target_lst)
            advantage = td_target - local_model.v(s_batch)

            pi = local_model.pi(s_batch, softmax_dim=1)
            pi_a = pi.gather(1, a_batch)
            loss = -torch.log(pi_a) * advantage.detach() + \
                F.smooth_l1_loss(local_model.v(s_batch), td_target.detach())

            optimizer.zero_grad()
            loss.mean().backward()
            for global_param, local_param in zip(global_model.parameters(),
                                                 local_model.parameters()):
                global_param._grad = local_param.grad
            optimizer.step()
            local_model.load_state_dict(global_model.state_dict())

    env.close()
    print("Training process {} reached maximum episode.".format(rank))

def test(global_model, max_test_ep):
    env = gym.make(params['gym_env'])
    score = 0.0
    print_interval = params['print_interval']

    for n_epi in range(max_test_ep):
        done = False
        s = env.reset()
        score = 0.0

        while not done:
            prob = global_model.pi(torch.from_numpy(s).float())
            a = Categorical(prob).sample().item()
            s_prime, r, done, info = env.step(a)
            s = s_prime
            score += r
        if n_epi % print_interval == 0:
            with open("./result/a3c.csv", "a+", encoding="utf-8") as f:
                f.write("{},{}\n".format(n_epi, score / print_interval))
            print("episode :{}, average score : {:.1f}".format(n_epi,
                                                       score / print_interval))
            score = 0.0
            time.sleep(1)
    env.close()

class a3c_algo():
    def __init__(self):
        super(a3c_algo, self).__init__()
        self.learning_rate = params['learning_rate']
        self.gamma = params['gamma']
        self.global_model = ActorCritic()
        self.global_model.share_memory()
        self.processes = []
        self.max_test_ep = params['max_test_ep']
        self.max_train_ep = params['max_train_ep']
        self.upadte_interval = params['update_interval']
        self.n_train_processes = params['n_train_processes']

    def init_write(self):
        with open("./result/a3c.csv", "w+", encoding="utf-8") as f:
            f.write("epoch_number,average reward\n")

    def train(self):
        self.init_write()
        for rank in range(self.n_train_processes + 1):  # + 1 for test process
            if rank == 0:
                p = mp.Process(target=test,
                               args=(self.global_model, self.max_test_ep))
            else:
                p = mp.Process(target=train,
                               args=(self.global_model, rank,
                                     self.learning_rate, self.gamma,
                                     self.max_train_ep, self.upadte_interval))
            p.start()
            self.processes.append(p)
        for p in self.processes:
            p.join()

if __name__ == "__main__":
    algo = a3c_algo()
    algo.train()


from itertools import permutations

def opertaion(temp, operand):
    stack = []
    calculate = False

    for i, symbol in enumerate(temp):
        if calculate:
            stack.pop()
            num1 = stack.pop()
            num2 = symbol
            calculate = False

            if operand == '-':
                stack.append(num1 - num2)
            if operand == '*':
                stack.append(num1 * num2)
            if operand == '+':
                stack.append(num1 + num2)
            continue
        if symbol == operand:
            calculate = True

        stack.append(symbol)

    return stack

def solution(expression):
    symbols = []
    temp = ''
    for e in expression:
        if e in '-*+':
            symbols.append(int(temp))
            symbols.append(e)
            temp = ''
        else:
            temp += e
    symbols.append(int(temp))
    orders = list(permutations('-*+', 3))
    result = []

    for order in orders:
        first, second, third = order
        temp = symbols[:]

        temp = opertaion(temp, first)
        temp = opertaion(temp, second)
        temp = opertaion(temp, third)
        result.append(abs(*temp))
    answer = max(result)
    return answer

expression = "50*6-3*2"
print(solution(expression))


import datetime
import os
import shutil
import time
import zipfile
from typing import Tuple

import numpy as np
import pandas
import requests
from obb_anns import OBBAnns
from tqdm import tqdm, trange
from PIL import Image, ImageDraw, ImageFont
from bs4 import BeautifulSoup
from pathlib import Path

from pandas import DataFrame
from requests import HTTPError

CVAT_URL = 'localhost:9080'
ORG = 'AutoDidact'
FORMAT = 'CVAT for video 1.1'
AUTH_TOKEN = os.environ['AUTH_TOKEN']
CSRF_TOKEN = os.environ['CSRF_TOKEN']
SESSION_ID = os.environ['SESSION_ID']
OUT = Path('out')
LABELS = 'Bed', 'Staff', 'Devices', 'Patient'

def download_dataset(task_id: int, folder: Path) -> Tuple[Path, bool]:
    out = folder / f"{task_id}"
    done_flag = out / '.done'
    if done_flag.exists():
        return out, False
    zip_path = out.with_name(f"{task_id}.zip")
    if zip_path.exists():
        zip_path.unlink()
    url = f"http://{CVAT_URL}/api/tasks/{task_id}?org={ORG}"
    headers0 = {
        "Host": "localhost:9080",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:105.0) Gecko/20100101 Firefox/105.0",
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "en-US,en;q=0.5",
        "Accept-Encoding": "gzip, deflate, br",
        "Referer": url,
        "Authorization": f"Token {AUTH_TOKEN}",
        "X-CSRFTOKEN": CSRF_TOKEN,
        "DNT": "1",
        "Connection": "keep-alive",
        "Cookie": f"csrftoken={CSRF_TOKEN}; sessionid={SESSION_ID}",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
    }
    resp0 = requests.get(url, headers=headers0)
    resp0.raise_for_status()
    meta = resp0.json()
    if meta['status'] != 'validation':
        shutil.rmtree(str(out), ignore_errors=True)
        raise Exception(" -> Task not done yet")
    if out.exists():
        done_flag.touch(exist_ok=True)
        return out, False
    url = f"http://{CVAT_URL}/api/tasks/{task_id}/dataset?org={ORG}&format={FORMAT}"
    headers = {
        "Host": "localhost:9080",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:105.0) Gecko/20100101 Firefox/105.0",
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "en-US,en;q=0.5",
        "Accept-Encoding": "gzip, deflate, br",
        "Referer": url,
        "Authorization": f"Token {AUTH_TOKEN}",
        "X-CSRFTOKEN": CSRF_TOKEN,
        "DNT": "1",
        "Connection": "keep-alive",
        "Cookie": f"csrftoken={CSRF_TOKEN}; sessionid={SESSION_ID}",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
    }
    requests.get(url, headers=headers).raise_for_status()
    print("Requesting dataset export:")
    for _ in range(19):
        resp = requests.get(url, headers=headers)
        resp.raise_for_status()
        if resp.status_code == 202:
            # Accepted
            print('.', end='')
        elif resp.status_code  == 201:
            # Created
            print()
            break
        time.sleep(1)
    else:
        raise Exception("Failed to get the dataset within 20s")

    url2 = f"{url}&action=download"
    headers2 = {
        "Host": "localhost:9080",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:105.0) Gecko/20100101 Firefox/105.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.5",
        "Accept-Encoding": "gzip, deflate, br",
        "Referer": url,
        "DNT": "1",
        "Connection": "keep-alive",
        "Cookie": f"csrftoken={CSRF_TOKEN}; sessionid={SESSION_ID}",
        "Upgrade-Insecure-Requests": "1",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "same-origin",
    }
    print("Downloading dataset:")
    resp2 = requests.get(url2, headers=headers2)
    resp2.raise_for_status()
    out.parent.mkdir(parents=True, exist_ok=True)
    with open(zip_path, 'wb') as fp:
        fp.write(resp2.content)
    with zipfile.ZipFile(zip_path, 'r') as zip_ref:
        zip_ref.extractall(out)
    zip_path.unlink()
    done_flag.touch(exist_ok=True)
    return out, True

def read_data_subset(subset_folder: Path, task_id: int) -> dict:
    assert subset_folder.is_dir()
    xml = BeautifulSoup(open(subset_folder / 'annotations.xml', 'r'), "xml")
    labels = {}
    for label in xml.select('annotations meta task labels label'):
        labels[label.select_one('name').text] = label.select_one('color').text
    assert labels.keys() == set(LABELS)
    source = xml.select_one('annotations meta source').text
    data = {
        'version': xml.select_one('annotations version').text,
        'labels': labels,
        'size': int(xml.select_one('annotations meta task size').text),
    }
    assert len(list((subset_folder / 'images').rglob('*.PNG'))) == int(data['size'])
    gt = []
    for track in xml.select('annotations track'):
        assert track['label'] in LABELS
        for box in track.select('box'):
            x0 = float(box['xtl'])
            y0 = float(box['ytl'])
            x1 = float(box['xbr'])
            y1 = float(box['ybr'])
            cx = (x0 + x1) / 2
            cy = (y0 + y1) / 2
            w = x1 - x0
            h = y1 - y0
            rot = float(box.get('rotation', "0.0"))
            if h > w:
                w, h = h, w
                rot += 90
            rot  %= 180
            gt.append({
                'task': task_id,
                'frame': int(box['frame']),
                'source': source,
                'label': LABELS.index(track['label']),
                'cx': cx,
                'cy': cy,
                'w': w,
                'h': h,
                'rotation': rot,
            })
    if len(gt) > 0:
        data['gt'] = DataFrame(gt).set_index(['task', 'frame']).sort_index()
    else:
        data['gt'] = DataFrame(
            columns=['task', 'frame', 'source', 'label', 'cx', 'cy', 'w', 'h', 'rotation']
        ).set_index(['task', 'frame'])
        shutil.rmtree(str(subset_folder), ignore_errors=True)
    return data

def concat_data_subsets(dataset: dict, extension: dict) -> dict:
    if len(dataset) == 0:
        dataset = {
            'version': extension['version'],
            'labels': extension['labels'],
            'size': 0,
            'gt': DataFrame(columns=extension['gt'].columns),
        }
    assert dataset['labels'] == extension['labels']
    dataset['size'] += extension['size']
    dataset['gt'] = pandas.concat([dataset['gt'], extension['gt']])
    return dataset

def rotate(arr: np.ndarray, angle: float) -> np.ndarray:
    ar = arr.copy()
    theta = np.radians(angle)
    c, s = np.cos(theta), np.sin(theta)
    R = np.array(((c, -s), (s, c)))
    center = np.array([np.mean(arr.T[0]), np.mean(arr.T[1])])
    ar = ar.reshape((4, 2)) - center
    ar = ar.dot(R) + center
    return ar

def draw_bbox(draw: ImageDraw.ImageDraw, cx: float, cy: float, w: float, h: float, rot: float, color: str, size: Tuple[int, int], label: str = None):
    xtl = cx - w/2
    ytl = cy - h/2
    xbr = cx + w/2
    ybr = cy + h/2
    bbox = rotate(np.array([(xtl, ytl), (xbr, ytl), (xbr, ybr), (xtl, ybr)]), rot)
    draw.line(list(map(tuple, np.concatenate([bbox, bbox[0:1]]).tolist())), fill=color, width=3)
    if label is not None:
        x0 = np.min(bbox.T[0])
        y0 = np.min(bbox.T[1])
        _, _, x1, y1 = ImageFont.load_default().getbbox(label)
        x1 += x0 + 4
        y1 += y0 + 4
        if x1 > size[0]:
            diff = x1 - size[0]
            x0 -= diff
        if y1 > size[1]:
            diff = y1 - size[1]
            y0 -= diff
        draw.rectangle((x0, y0, x1, y1), fill='#303030')
        draw.text((x0 + 2, y0 + 2), label, color)

def idx_to_img_path(task: int, frame: int) -> Path:
    return Path(f"frame_{frame:06}.PNG")

def visualize_sample(idx: Tuple[int, int], data: dict, folder: Path, save_to_disk: bool = False) -> Image.Image:
    task, frame = idx
    out = folder / str(task) / 'visualized'
    out.mkdir(exist_ok=True)
    img_path = folder / str(task) / 'images' /idx_to_img_path(*idx)
    assert img_path.exists()
    vis_path = out / img_path.name
    img = Image.open(img_path)
    # img = img.convert('RGBA')
    draw = ImageDraw.Draw(img)
    ann = data['gt'].loc[[idx]]
    for (task, frame), (source, label, cx, cy, w, h, rot) in ann.iterrows():
        cls = LABELS[int(label)]
        col = data['labels'][cls]
        draw_bbox(draw, cx, cy, w, h, rot, col, img.size, cls)
    if save_to_disk:
        img.save(vis_path)
    return img

def visualize_dataset(data: dict, folder: Path, save_to_disk: bool = True):
    print('Visualizing dataset')
    index = data['gt'].index.unique()
    for idx in tqdm(index):
        visualize_sample(idx, data, folder, save_to_disk=save_to_disk)

def export_dataset(data: dict, folder: Path) -> Tuple[OBBAnns, OBBAnns, OBBAnns]:
    index = data['gt'].index.unique()
    total = len(index)
    files = {
        'train': 0.70,
        'val': 0.15,
        'test': 0.15,
    }
    idx = 0
    for dataset_name, ratio in files.items():
        count = int(total * ratio)
        files[dataset_name] = (idx, idx + count)
        idx += count
    files[dataset_name] = (files[dataset_name][0], total)
    anns = []
    with tqdm(total=total) as bar:
        for dataset_name, (idx0, idx1) in files.items():
            df = data['gt'].loc[index[idx0:idx1]]
            ann, should_save = to_obb_anns(df, data, folder, dataset_name, bar)
            ann_str = str(ann).replace('\n', ', ')
            if should_save:
                ann.save_annotations()
                bar.write(f"Extended the {dataset_name} set: {ann_str}")
            else:
                bar.write(f"Created the {dataset_name} set: {ann_str}")
            anns.append(ann)
    return tuple(anns)

def to_obb_anns(df: DataFrame, meta: dict, folder: Path, name: str, bar: tqdm = None) -> Tuple[OBBAnns, bool]:
    base_dir = Path('data', f"{ORG}_{name}".lower())
    img_dir = base_dir / 'images'
    vis_dir = base_dir / 'visualizations'
    ann_file = (base_dir / 'annotations.json')
    ANN_SET = 'autodidact'
    ann = OBBAnns(ann_file)
    check_if_sample_exists = False
    added_samples = False
    if ann_file.exists():
        # Assume we want to add new samples to an existing dataset
        ann.load_annotations()
        check_if_sample_exists = True
    else:
        # Assume we want to create the dataset from scratch
        shutil.rmtree(img_dir, ignore_errors=True)
        shutil.rmtree(vis_dir, ignore_errors=True)
        ann.dataset_info = {
            'description': f'Autodidact {name} dataset',
            'version': '1.0',
            'year': datetime.datetime.now().year,
            'contributor': 'Raphael Emberger, Daniel Baumann, Seric Marko, Huo Shufan',
            'date_created': datetime.datetime.now().strftime('%Y/%m/%d'),
        }
        ann.ann_info = DataFrame(columns=['a_bbox', 'o_bbox', 'cat_id', 'area', 'img_id', 'comments'])
        ann.img_info = []
        ann.annotation_sets = [ANN_SET]
        ann.cat_info = {}
        for i, (cls, col) in enumerate(meta['labels'].items()):
            i_col = int(col.strip('#'), 16)
            r = (i_col & (0xFF << 16)) >> 16
            g = (i_col & (0xFF << 8)) >> 8
            b = i_col & 0xFF
            ann.cat_info[i] = {
                'name': cls,
                'annotation_set': ANN_SET,
                'color': (r, g, b)
            }
    img_dir.mkdir(exist_ok=True, parents=True)
    vis_dir.mkdir(exist_ok=True)
    index = df.index.unique()
    for idx in index:
        task, frame = idx
        orig_img_path = folder / str(task) / 'images' / idx_to_img_path(*idx)
        assert orig_img_path.exists(), f"Source file does not exist: {orig_img_path}"
        img_fp = img_dir / f"task_{task:03}_frame_{frame:06}.png"
        if check_if_sample_exists and img_fp.exists():
            # Don't add to the set - it already exists
            if bar is not None:
                bar.update()
            continue
        os.link(orig_img_path, img_fp)
        labels = {}
        for i, ((task, frame), (source, label, cx, cy, w, h, rot)) in enumerate(df.loc[[idx]].iterrows()):
            xtl = cx - w/2
            ytl = cy - h/2
            xbr = cx + w/2
            ybr = cy + h/2
            bbox = rotate(np.array([(xtl, ytl), (xbr, ytl), (xbr, ybr), (xtl, ybr)]), rot)
            labels[i] = {
                'a_bbox': [xtl, ytl, xbr, ybr],
                'o_bbox': bbox.reshape((8,)).tolist(),
                'cat_id': [label],
                'area': w * h,
                'img_id': -1,
                'comments': meta['labels'][LABELS[label]],
            }
        with Image.open(img_fp) as img:
            img_id = ann.add_new_img_ann_pair(
                img_fp.name,
                img.width,
                img.height,
                labels,
            )
            ann.visualize(img_id=img_id, out_dir=str(vis_dir), show=False, print_label=True)
        if bar is not None:
            bar.update()
        added_samples = True
    return ann, added_samples

if __name__ == '__main__':
    dataset = {}
    for task_id in trange(200):
        is_new = False
        try:
            path, is_new = download_dataset(task_id, OUT)
        except HTTPError as err:
            continue
        except Exception as err:
            tqdm.write(f"Task {task_id}: {str(err)}")
            continue
        if is_new:
            dataset = concat_data_subsets(dataset, read_data_subset(path, task_id))
    if len(dataset) == 0:
        print("Nothing to do")
        exit(0)
    train, val, test = export_dataset(dataset, OUT)


import json
import multiprocessing
import spacy
import sys
import gzip
from termcolor import colored
import text_analyse

ALLOWED_PARALLEL_PROCESS = 8
UKP_SERVER = 'http://krusty.ukp.informatik.tu-darmstadt.de'
UKP_SERVER_NED = "http://ned.ukp.informatik.tu-darmstadt.de"
LOCALHOST = "http://localhost"
nlp = spacy.load('en_core_web_sm')

def process_example(example_dict, doc_index):
    print(colored(("example " + str(doc_index)), 'yellow'))
    context = example_dict['context']
    ner_dict = text_analyse.get_ner_spacy_stanford(context)

    augment_context = " [EST] "
    for ne_name in ner_dict.keys():
        augment_context += ne_name + " [ESP] "

    augment_context = augment_context[:-7] + " [EED]"
    example_dict['context'] = context + augment_context
    return example_dict

if __name__ == '__main__':
    src_path = sys.argv[1]
    res = []
    pool = multiprocessing.Pool(processes=ALLOWED_PARALLEL_PROCESS)
    doc_index = 0
    with gzip.open(src_path, 'rt') as f_src, open(src_path+"_arg", 'w') as f_out:
        json.dump(next(f_out))
        for line in f_src:
            example = json.loads(line)
            res.append(process_example(example, doc_index))
            #res.append(pool.apply_async(process_example, (example, doc_index, )))
            doc_index += 1
        # pool.close()
        # pool.join()
   # print("the end!")

#    with gzip.open(src_path+'_aug', 'wt') as f_out:
        f_out.write("\n")
        for new_example in res:
            json.dump(new_example, f_out)
            #json.dump(new_example.get(timeout=1), fw)
            f_out.write('\n')


import numpy as np
import torch
from torch.autograd import Variable

tensor = torch.FloatTensor([[1,2],[3,4]])
variable = Variable(tensor,requires_grad=True) #requires_grad會自動計算gradient(Backpropagation)

t_out = torch.mean(tensor*tensor) # tensor不能反向傳播
v_out = torch.mean(variable*variable)

print(t_out)
print(v_out)

v_out.backward()
print(variable)


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

import logging

from noseapp.utils.common import waiting_for
from noseapp.utils.common import TimeoutException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.remote.webelement import WebElement
from selenium.common.exceptions import NoSuchElementException

logger = logging.getLogger(__name__)

DEFAULT_SLEEP = 0.01
DEFAULT_WAIT_TIMEOUT = 30

def _error_handler(e, client, css):
    """
    To extend error message
    """
    prefix = u' ' if e.message else u''

    if isinstance(client, WebElement):
        e.message += u'{}QueryProcessor(From: {}, CSS: {})\n\n--\nSEARCH AREA: {}\n--\n'.format(
            prefix,
            repr(client),
            css,
            client.get_attribute('innerHTML'),
        )
    else:
        e.message += u'{}QueryProcessor(From: {}, CSS: {})'.format(
            prefix,
            repr(client),
            css,
        )

def _execute(client, css, get_all=False, allow_polling=True):
    """
    Execute css query
    """
    logger.debug(u'CSS: {} Get all: {}'.format(css, 'Yes' if get_all else 'No'))

    css_executors = {
        True: 'find_elements_by_css_selector',
        False: 'find_element_by_css_selector',
    }

    try:
        if allow_polling:
            result = getattr(client, css_executors[bool(get_all)])(css)
        elif hasattr(client, 'disable_polling'):
            with client.disable_polling():
                result = getattr(client, css_executors[bool(get_all)])(css)
        else:
            result = getattr(client, css_executors[bool(get_all)])(css)

        return result

    except WebDriverException as e:
        _error_handler(e, client, css)
        raise

class QueryResult(object):
    """
    Execute actions by css query and returning result
    """

    def __init__(self, client, css):
        self._client = client
        self._css = css

    def __getattr__(self, item):
        return getattr(
            self._client.query.__class__(self.first()), item,
        )

    @property
    def exist(self):
        """
        Check element exist
        """
        self._client.config.implicitly_wait(0)

        try:
            el = _execute(self._client, self._css, allow_polling=False)

            if el:
                self._client.config.apply_implicitly_wait()
                return True
            self._client.config.apply_implicitly_wait()
            return False
        except WebDriverException:
            self._client.config.apply_implicitly_wait()
            return False
        except BaseException:
            self._client.config.apply_implicitly_wait()
            raise

    @property
    def with_wait(self):
        self.wait()
        return self

    def wait(self, timeout=None, sleep=None):
        """
        Waiting for web element exist
        """
        try:
            return waiting_for(
                lambda: self.exist,
                sleep=sleep or DEFAULT_SLEEP,
                timeout=timeout or DEFAULT_WAIT_TIMEOUT,
            )
        except TimeoutException:
            raise TimeoutException(
                'Could not wait web element with css "{}"'.format(self._css),
            )

    def get(self, index):
        """
        Get web element by index
        """
        try:
            return _execute(self._client, self._css, get_all=True)[index]
        except IndexError:
            raise NoSuchElementException(
                'Result does not have element with index "{}". Css: "{}".'.format(
                    index, self._css,
                ),
            )

    def first(self):
        """
        Get first element on page
        """
        return _execute(self._client, self._css)

    def all(self):
        """
        Get all elements of appropriate query
        """
        return _execute(self._client, self._css, get_all=True)


from io import BytesIO, SEEK_END

import attr
from PIL import Image

MAX_EDGE_PIXELS = 1024
QUALITY = 80
SUPPORTED_FORMATS = ('JPEG', 'PNG', 'GIF')
MAX_SIZE_IN_BYTES_AFTER_PROCESSING = 1024 * 1024
MIN_AREA_TRACKING_PIXEL = 10

@attr.s
class ImageProcessingResult:
    size_in_bytes: int = attr.ib()
    width: int = attr.ib()
    height: int = attr.ib()
    image_format: str = attr.ib()
    data: BytesIO = attr.ib()

class ImageProcessingError(Exception):
    pass

def process_image_data(data: bytes) -> ImageProcessingResult:
    try:
        image = Image.open(BytesIO(data))
    except OSError as e:
        raise ImageProcessingError('Cannot open image: {}'.format(e))

    with image:
        if image.format not in SUPPORTED_FORMATS:
            raise ImageProcessingError(
                'Unsupported format {}'.format(image.format)
            )

        width, height = image.size
        if (width * height) < MIN_AREA_TRACKING_PIXEL:
            raise ImageProcessingError('Tracking pixel')

        if image.format == 'GIF':
            # Gif are weird, saving them often fails and the result after
            # compression is sometimes bigger than the original file.
            # Let's just keep the original file.
            data = BytesIO(data)
        else:
            data = BytesIO()
            try:
                image.thumbnail((MAX_EDGE_PIXELS, MAX_EDGE_PIXELS))
                width, height = image.size
                image.save(data, image.format, quality=QUALITY,
                           optimize=True, progressive=True)
            except (OSError, EOFError) as e:
                raise ImageProcessingError('Cannot resize image: {}'.format(e))

    size_in_bytes = data.seek(0, SEEK_END)
    data.seek(0)
    if size_in_bytes > MAX_SIZE_IN_BYTES_AFTER_PROCESSING:
        raise ImageProcessingError(
            'Resulting file too big: {} bytes'.format(size_in_bytes)
        )

    return ImageProcessingResult(
        size_in_bytes, width, height, image.format, data
    )


import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent.resolve())) #.. this has to be the folder reCNN_visual_prosthesis
import wandb
import glob
import pytorch_lightning as pl
import matplotlib.pyplot as plt
import numpy as np
import pickle

def pickle_read(path):
    with open(path, "rb") as f:
        x = pickle.load(f)
    return x

def pickle_save(path, x):
    with open(path, "wb") as f:
        pickle.dump(x, f)

def circular_distance(a, b, period=np.pi):
    """
    Computes circular distance between two angles a and b.
    Changing period it's possible to set the scale (radiants or degrees)
    Written by Luca Baroni
    """
    return np.array(
        [min((aa - bb) % period, (bb - aa) % period) for aa, bb in zip(a, b)]
    )

def rotate_and_compute_distances(a, b, period, n_rot=360):
    """
    Rotates array of angles a for n_rot possible angles and computes average
    pairwise circular distance with respect to array of angles b.
    Changing period it's possible to set the scale (radiants or degrees)
    Return array containing average distances for all possible rotations and array of rotations
    Written by Luca Baroni
    """
    avg_dist = [
        circular_distance(a + r, b, period).mean()
        for r in np.linspace(0, period, n_rot)[:-1]
    ]
    return [np.array(avg_dist), np.linspace(0, period, n_rot)[:-1]]

def reconstruct_orientation_maps(x, y, ori, f, ax, save, figsize=10, xlim=None, ylim=None, img_path="img/", suffix="", neuron_dot_size=1):
    """
        Reconstructs the orientation map, visualizes it and prints the distribution of
        orientation preferences.
    """

    plt.gca().set_aspect("equal", adjustable="box")
    plt.gcf().set_size_inches(7.5, 6.5)
    points = ax.scatter(x, y, c=ori, s=neuron_dot_size, cmap="hsv")

    if suffix == "_truth":
        plt.title("An orientation map of the Antolík et al. model\n")

    if suffix == "_antolik":
        plt.title("An orientation map of the Antolík et al. model\nreconstructed by our DNN\n")

    if suffix == "_antolik" or suffix == "_truth":
        ax.set_xlabel("Spatial dimension x [degrees of visual angle]")
        ax.set_ylabel("Spatial dimension y [degrees of visual angle]")
        f.colorbar(points).set_label("Preferred orientation [degrees]")
    else:
        plt.title("A location and preferred orientation estimates of a mouse\nrecorded in the Lurz et al. dataset\n")
        ax.set_xlabel("Normalized spatial dimension x")
        ax.set_ylabel("Normalized spatial dimension y")
        f.colorbar(points).set_label("Preferred orientation [degrees]")

    file_name = ""
    if suffix == "_truth":
        file_name = img_path + "true_orientation_map_of_antolik_model"
    else:
        file_name = img_path + "reconstructed_orientation_map" + suffix
    if xlim:
        plt.xlim(-xlim, xlim)
    if ylim:
        plt.ylim(-ylim, ylim)

    if save:
        plt.savefig(file_name, dpi = 300)
        print("Reconstructed orientation maps are saved in " + file_name + ".fig")

    plt.show()

def visualize_preferred_orientations(x, y, ori, f, ax, save, figsize=10, xlim=None, ylim=None, img_path="img/", suffix=""):

    plt.clf()
    plt.gcf().set_size_inches(6.5, 6.5)

    n, bins, patches = plt.hist(ori, bins=20)

    cm = plt.cm.get_cmap('hsv')
    bin_centers = 0.5 * (bins[:-1] + bins[1:])

    # scale values to interval [0,1]
    col = bin_centers - min(bin_centers)
    col /= max(col)

    for c, p in zip(col, patches):
        plt.setp(p, 'facecolor', cm(c))

    if suffix == "_truth":
        plt.title("A distribution of preferred orientations in Antolík et al. model\n")

    elif suffix == "_antolik":
        plt.title("A distribution of estimated preferred orientations\nin Antolík et al. model\n")

    else:
        plt.title("A distribution of estimated preferred orientations\nin the Lurz et al. dataset\n")

    plt.xlabel("Preferred orientation [degrees]")
    plt.ylabel("Number of neurons")
    if save:
        plt.savefig(img_path + "distribution_of_prefered_orientations" + suffix, dpi = 300)
        print("saved")
    plt.show()
    plt.clf()

def download_model(artifact_name, model_class):
    run = wandb.init(project="reCNN_visual_prosthesis", entity="csng-cuni")
    pl.seed_everything(42)

    artifact = run.use_artifact(
        artifact_name, type="model"
    )

    artifact_dir = artifact.download()
    models_paths_list = glob.glob(artifact_dir + "/*.ckpt")

    m = model_class.load_from_checkpoint(models_paths_list[0])
    m.freeze()
    print(f"Model from {models_paths_list[0]} loaded!")

    return m

def get_neuron_estimates(model, scale_factor=1):
    squeezed = model.readout.mu.squeeze(0).squeeze(0).squeeze(1)

    data_to_be_plotted = squeezed.cpu().detach().numpy()

    x, y, ori = data_to_be_plotted[:, 0], data_to_be_plotted[:, 1], data_to_be_plotted[:, 2]

    # our model represents locations on positions from -1 and 1 (in both x and y
    # directions), so we have to scale it to the real values
    #   - these values are given by the Antolik's model's characteristics
    x *= scale_factor
    y *= scale_factor

    # orientations are between -1 and 1 but there can be outliers that (because of
    # periodicity) have to be shifted to the right location
    # Example: 1.2 goes to -0.8, -1.7 goes to 0.3
    ori = (np.mod((ori + 1), 2) ) - 1

    # we have orientations in all 360 degrees, but in this evaluation it makes sense
    # to treat bars of ori and ori+180 as bars of the same orientation
    # therefore, now we have orientations in a range [0, 1]
    ori = [e if e >= 0 else e+1 for e in ori]

    return (x, y, ori)


# test_gates_op.py

# Purpose:
#   to test gates operation of codesys

# note:
#   the data are taken from the "results.scv" file

# import files/classes
from assign_1_type_to_df_col import AssignType2ListOfDfGrCol

# package
import numpy as np
import operator
import pytest

# Arguments:
path_in       = r"C:\Users\AnastasiosKolovos\AK\codesys_me\python_codesys\pytest_codesys\results.csv"
remove_str    = ""
var_in0       = "PLC_PRG.BOOL_A"
var_in1       = "PLC_PRG.BOOL_B"
var_out0      = "PLC_PRG.OR_OUT"
var_out1      = "PLC_PRG.AND_OUT"
var_out2      = "PLC_PRG.XOR_OUT"
var_out3      = "PLC_PRG.NAND_OUT"
var_list      = np.array ([var_in0, var_in1,var_out0,var_out1,var_out2, var_out3])
data_type     = "str"

# call real number input arrays
# input_a
obj_AssignType2ListOfDfGrCol0 = AssignType2ListOfDfGrCol(path_in, remove_str, var_list[0], data_type)
in_a_list = obj_AssignType2ListOfDfGrCol0.assign_bool_to_list()

# input_b
obj_AssignType2ListOfDfGrCol1 = AssignType2ListOfDfGrCol(path_in, remove_str, var_list[1], data_type)
in_b_list = obj_AssignType2ListOfDfGrCol1.assign_bool_to_list()

# call real number output arrays
# or
obj_AssignType2ListOfDfGrCol2 = AssignType2ListOfDfGrCol(path_in, remove_str, var_list[2], data_type)
or_list = obj_AssignType2ListOfDfGrCol2.assign_bool_to_list()

# and
obj_AssignType2ListOfDfGrCol3 = AssignType2ListOfDfGrCol(path_in, remove_str, var_list[3], data_type)
and_list = obj_AssignType2ListOfDfGrCol3.assign_bool_to_list()

# xor
obj_AssignType2ListOfDfGrCol4 = AssignType2ListOfDfGrCol(path_in, remove_str, var_list[4], data_type)
xor_list = obj_AssignType2ListOfDfGrCol4.assign_bool_to_list()

# nand
obj_AssignType2ListOfDfGrCol5 = AssignType2ListOfDfGrCol(path_in, remove_str, var_list[5], data_type)
nand_list = obj_AssignType2ListOfDfGrCol5.assign_bool_to_list()

# array of output array:
list_of_ouput_lists = np.array([or_list, and_list, xor_list, nand_list])

#print("\n\n\n==", list_of_ouput_lists)
print("\n\n\n==============================")

# ------------------------------
@pytest.mark.parametrize(
    ("input_a", "input_b", "output_or"),
    [
        (in_a_list[0], in_b_list[0], list_of_ouput_lists[0][0]),
        (in_a_list[1], in_b_list[1], list_of_ouput_lists[0][1]),
        (in_a_list[2], in_b_list[2], list_of_ouput_lists[0][2]),
        (in_a_list[3], in_b_list[3], list_of_ouput_lists[0][3]),
        (in_a_list[4], in_b_list[4], list_of_ouput_lists[0][4]),
    ],
)
@pytest.mark.gates
def test_gate_or(input_a, input_b, output_or):

    assert (input_a or input_b) ==  output_or
    print("\ntest_gate_or: ", input_a, " or ", input_b, " = ",  output_or)

# ------------------------------
@pytest.mark.parametrize(
    ("input_a", "input_b", "output_and"),
    [
        (in_a_list[0], in_b_list[0], list_of_ouput_lists[1][0]),
        (in_a_list[1], in_b_list[1], list_of_ouput_lists[1][1]),
        (in_a_list[2], in_b_list[2], list_of_ouput_lists[1][2]),
        (in_a_list[3], in_b_list[3], list_of_ouput_lists[1][3]),
        (in_a_list[4], in_b_list[4], list_of_ouput_lists[1][4]),
    ],
)
@pytest.mark.gates
def test_gate_and(input_a, input_b, output_and):

    assert (input_a and input_b) ==  output_and
    print("\ntest_gate_and: ", input_a, " and ", input_b, " = ",  output_and)

# ------------------------------
@pytest.mark.parametrize(
    ("input_a", "input_b", "output_xor"),
    [
        (in_a_list[0], in_b_list[0], list_of_ouput_lists[2][0]),
        (in_a_list[1], in_b_list[1], list_of_ouput_lists[2][1]),
        (in_a_list[2], in_b_list[2], list_of_ouput_lists[2][2]),
        (in_a_list[3], in_b_list[3], list_of_ouput_lists[2][3]),
        (in_a_list[4], in_b_list[4], list_of_ouput_lists[2][4]),
    ],
)
@pytest.mark.gates
def test_gate_xor(input_a, input_b, output_xor):

    assert (input_a ^ input_b)  ==  output_xor
    print("\ntest_gate_xor: ", input_a, " xor ", input_b, " = ",  output_xor)

# ------------------------------
# this is one test pass or fail (with many values tested)
@pytest.mark.compare
def test_xor_compare(input_true):

    for output_xor in list_of_ouput_lists[2]:

        assert output_xor == input_true
        print("\ntest_xor_compare: ", output_xor, " == ",  input_true)

# ------------------------------

@pytest.mark.parametrize(
    ("input_a", "input_b", "output_nand"),
    [
        (in_a_list[0], in_b_list[0], list_of_ouput_lists[3][0]),
        (in_a_list[1], in_b_list[1], list_of_ouput_lists[3][1]),
        (in_a_list[2], in_b_list[2], list_of_ouput_lists[3][2]),
        (in_a_list[3], in_b_list[3], list_of_ouput_lists[3][3]),
        (in_a_list[4], in_b_list[4], list_of_ouput_lists[3][4]),
    ],
)
@pytest.mark.gates
def test_gate_nand(input_a, input_b, output_nand):
    assert not(input_a and input_b) ==  output_nand
    print("\ntest_gate_nand: ", input_a, " NAND ", input_b, " = ",  output_nand)


import time
from numpy import random
import multiprocessing
import RPi.GPIO as GPIO
from led8x8 import LED8x8

# define GPIO pins for data, latch, and clock
dataPins = 17
latchPins = 5
clockPins = 13

# init variables
name = "led8x8"
xpos = 0
ypos = 0
boardState = multiprocessing.Array('i', 8)
boardState[xpos] = 1 << ypos
led = LED8x8(dataPins, latchPins, clockPins, name, boardState)

led.daemon = True
led.start()

try:
  while True:
    boardState[xpos] = 0
    xpos = xpos + random.randint(3) - 1
    ypos = ypos + random.randint(3) - 1
    if xpos < 0:
      xpos = 0
    elif xpos > 7:
      xpos = 7

    if ypos < 0:
      ypos = 0
    elif ypos > 7:
      ypos = 7
    boardState[xpos] = 1 << ypos
    time.sleep(0.1)
except KeyboardInterrupt:
  print('exiting random walk')
finally:
  led.terminate()


import checkpy.tests as t
import checkpy.lib as lib
import checkpy.assertlib as assertlib
import importlib
import helpers
import re

def sandbox():
	lib.require("DeBiltTempMax.txt", "http://www.nikhef.nl/~ivov/Python/KlimaatData/DeBiltTempMax.txt")
	lib.require("DeBiltTempMin.txt", "http://www.nikhef.nl/~ivov/Python/KlimaatData/DeBiltTempMin.txt")

def before():
	try:
		import matplotlib
		matplotlib.use("Agg")
		import matplotlib.pyplot as plt
		plt.switch_backend("Agg")
		lib.neutralizeFunction(plt.pause)
	except ImportError:
		pass

def after():
	try:
		import matplotlib.pyplot as plt
		plt.switch_backend("TkAgg")
		importlib.reload(plt)
	except ImportError:
		pass

# Thanks to Vera Schild!

@t.test(10)
def correctHighestTemp(test):
	def testMethod():
		correctAnswer = "36.8"
		if helpers.isHardcodedIn(correctAnswer, test.fileName):
			test.success = lambda info : "watch out: {} appears to be hardcoded!".format(correctAnswer)

		output = lib.outputOf(
			test.fileName,
			overwriteAttributes = [("__name__", "__main__")]
		)
		return assertlib.contains(output, correctAnswer)

	test.test = testMethod
	test.description = lambda : "prints the highest temperature"

@t.passed(correctHighestTemp)
@t.test(11)
def correctDateHighestTemp(test):
	def testMethod():
		if helpers.isHardcodedIn(1947, test.fileName):
			test.success = lambda info : "watch out: 1947 appears to be hardcoded!"

		output = lib.outputOf(
			test.fileName,
			overwriteAttributes = [("__name__", "__main__")]
		)
		line = helpers.findLineWith(output, "36.8")

		correctDay = assertlib.contains(line, '27')
		correctMonth = any([assertlib.contains(line.lower(), month) for month in ["6", "juni", "june", "jun"]])
		correctYear = assertlib.contains(line, '1947')
		return correctDay and correctMonth and correctYear

	test.test = testMethod
	test.description = lambda : "prints the date of the highest temperature"

@t.test(20)
def correctLowestTemp(test):
	def testMethod():
		correctAnswer = "-24.8"
		if helpers.isHardcodedIn(correctAnswer, test.fileName):
			test.success = lambda info : "watch out: {} appears to be hardcoded!".format(correctAnswer)

		output = lib.outputOf(
			test.fileName,
			overwriteAttributes = [("__name__", "__main__")]
		)
		return assertlib.contains(output, correctAnswer)

	test.test = testMethod
	test.description = lambda : "prints the lowest temperature"

@t.passed(correctLowestTemp)
@t.test(21)
def correctDateLowestTemp(test):
	def testMethod():
		if helpers.isHardcodedIn(1942, test.fileName):
			test.success = lambda info : "watch out: 1942 appears to be hardcoded!"

		output = lib.outputOf(
			test.fileName,
			overwriteAttributes = [("__name__", "__main__")]
		)
		line = helpers.findLineWith(output, "-24.8")

		correctDay = assertlib.contains(line, '27')
		correctMonth = any([assertlib.contains(line.lower(), month) for month in ["1", "januari", "january", "jan"]])
		correctYear = assertlib.contains(line, '1942')
		return correctDay and correctMonth and correctYear

	test.test = testMethod
	test.description = lambda : "prints the date of the lowest temperature"

@t.test(30)
def correctLongestFreezing(test):
	def testMethod():
		correctAnswer = "21"
		if helpers.isHardcodedIn(correctAnswer, test.fileName):
			test.success = lambda info : "watch out: {} appears to be hardcoded!".format(correctAnswer)

		output = lib.outputOf(
			test.fileName,
			overwriteAttributes = [("__name__", "__main__")]
		)
		return assertlib.contains(output, correctAnswer)

	test.test = testMethod
	test.description = lambda : "print de langste periode dat het aaneengesloten heeft gevroren"

@t.passed(correctLongestFreezing)
@t.test(31)
def correctDateLongestFreezingp(test):
	def testMethod():
		if helpers.isHardcodedIn(1947, test.fileName):
			test.success = lambda info : "watch out: 1947 appears to be hardcoded!"

		output = lib.outputOf(
			test.fileName,
			overwriteAttributes = [("__name__", "__main__")]
		)
		line = helpers.findLineWith(output, "21")

		correctDay = assertlib.contains(line, '24')
		correctMonth = any([assertlib.contains(line.lower(), month) for month in ["2", "februari", "february", "feb"]])
		correctYear = assertlib.contains(line, '1947')
		return correctDay and correctMonth and correctYear

	test.test = testMethod
	test.description = lambda : "prints the last day of the longest freezing period"

@t.test(40)
def correctFirstHeatWave(test):
	def testMethod():
		correctAnswer = "1911"
		if helpers.isHardcodedIn(correctAnswer, test.fileName):
			test.success = lambda info : "watch out: {} appears to be hardcoded!".format(correctAnswer)

		output = lib.outputOf(
			test.fileName,
			overwriteAttributes = [("__name__", "__main__")]
		)
		return assertlib.contains(output, correctAnswer)

	test.test = testMethod
	test.description = lambda : "prints the first year that had a heatwave"

@t.test(50)
def showsGraph(test):
	test.test = lambda : assertlib.fileContainsFunctionCalls(_fileName, "savefig") or assertlib.fileContainsFunctionCalls(_fileName, "show")
	test.description = lambda : "either saves a plot to image, or shows it"


import csv
from datetime import datetime

class CsvPipline(object):
    def process_item(self, item, spider):
        columns = item['columns']
        if not columns:
            return item

        settings = spider.settings
        d = datetime.now().strftime("%Y%m%d%H")  #  %Y%m%d%H%M%S
        csvpath = settings['APP_PATH'] + '/data/ret'+ d +'.csv'
        data = [
            columns['username'],  # 用户名
            columns['autohome_id'],
            columns['koubei_id'],
            columns['product_name'],
            columns['score_wg'],  # 外观评分
            columns['score_ssd'],
            columns['score_xjb'],
            columns['prov_name'],
            columns['city_name'],
            columns['buy_date'],
            columns['buy_price'],
            columns['car_oil'],
            columns['car_merit'],
            columns['car_defect']
        ]
        with open(csvpath, 'a+', newline='') as csvfile:
            # pwriter.writerow(['那就这样吧', '哈哈'])
            pwriter = csv.writer(csvfile)
            pwriter.writerow(data)
        return item


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
# File - otree.py:
#
#      Implements an occupancy storing quadtree.  Provides methods for adding "occupied" regions
#   as either points or circles.  The default maximum depth of 7 should be sufficient for most
#   uses, if constructed with a larger depth, greater area resolution can be traded for a larger
#   memory footprint and longer insertion/lookup times.
#
#       It is worth noting, however, that it is the area insertion time that increases more
#   rapidly.  If only few insertions are required and many occupancy tests, there is little
#   reason to avoid higher depths.
#
# License:
#
#      This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
#  If a copy of the MPL was not distributed with this file, You can obtain one
#  at http://mozilla.org/MPL/2.0/.
#
#

import matplotlib.pyplot as plt
import numpy as np
import numpy.linalg

class OccupiedQuadtree2D:
    __id_count = 0

    def __init__(self, p_ll, p_ur, depth=7, propogate=False, debug=False):
        self.__id = self.__class__.__id_count
        self.__class__.__id_count = self.__class__.__id_count + 1
        self.__p_ll = p_ll
        self.__p_ur = p_ur
        self.__default_depth = depth
        self.__width = p_ur[0] - p_ll[0]
        self.__height = p_ur[1] - p_ll[1]
        self.__p_center = (p_ur + p_ll) / 2.0
        self.__occupied = propogate
        self.__children = [None, None, None, None]
        self.__bounds = np.array([self.__p_ll, self.__p_ur])
        self.__is_leaf = True
        self.__children_full = False
        self.__debug = debug

    @property
    def id(self):
        return self.__id

    @property
    def bounds(self):
        return self.__bounds

    @property
    def center(self):
        return self.__p_center

    @property
    def width(self):
        return self.__width

    @property
    def height(self):
        return self.__height

    @property
    def full(self):
        return self.__children_full or (self.is_leaf and self.occupied)

    @property
    def is_leaf(self):
        return self.__is_leaf

    @property
    def occupied(self):
        return self.__occupied

    @occupied.setter
    def occupied(self, value):
        self.__occupied = value
        return value

    def plot(self, fig = None, ax = None, ec_o = 'k', fc_o = 'r', ec_f = 'k', fc_f = 'white', alpha = 0.1):
        if fig is None:
            fig, ax = plt.subplots()

        if self.full or self.is_leaf:
            ec, fc = (ec_o, fc_o) if self.occupied else (ec_f, fc_f)
            rect = plt.Rectangle(self.bounds[0], self.width, self.height, fc=fc, ec=ec, alpha=alpha)
            ax.add_artist(rect)
        else:
            for child in self.__children:
                if not child is None:
                    child.plot(fig, ax, ec_o, fc_o, ec_f, fc_f, alpha)
        return fig, ax

    def testPoint(self, p):
        return self.__test_point(p)

    def addPoint(self, p, depth=None):
        return self.__generic_add(self.__pointInBounds(p), depth)

    def addCircle(self, p, r, depth=None):
        def circleIntersectsBounds(bounds):
            p_ll, p_ur = bounds
            x_n = np.clip(p[0], p_ll[0], p_ur[0])
            y_n = np.clip(p[1], p_ll[1], p_ur[1])
            p_n = point(x_n, y_n)
            dist = np.linalg.norm(p_n - p)
            return dist <= r

        return self.__generic_add(circleIntersectsBounds, depth)

    def __test_point(self, p):
        if not ( (self.__p_ll[0] <= p[0] <= self.__p_ur[0]) and
                 (self.__p_ll[1] <= p[1] <= self.__p_ur[1]) ): return False
        if self.__children_full: return True
        if self.__is_leaf: return self.__occupied
        return any(child.__test_point(p) for child in self.__children)

    def __generic_test(self, func):
        if not func(self.bounds): return False
        if self.__children_full: return True
        if self.__is_leaf: return self.__occupied
        return any(child.__generic_test(func) for child in self.__children)

    def __generic_add(self, func, depth=None):
        depth = self.__default_depth if depth is None else depth
        bounds = self.bounds
        propogate = self.occupied
        if (not self.__children_full) and func(bounds):
            self.occupied = True

            if depth > 0:
                child_width = self.width / 2.0
                child_height = self.height / 2.0

                # Add Nodes
                if self.is_leaf:
                    # Process Subtree LL
                    p_ll = bounds[0]
                    p_ur = point(bounds[0][0]+child_width, bounds[0][1]+child_height)
                    self.__children[0] = self.__child(p_ll, p_ur)

                    # Process Subtree UL
                    p_ll = point(bounds[0][0], bounds[0][1]+child_height)
                    p_ur = point(bounds[0][0]+child_width, bounds[1][1])
                    self.__children[1] = self.__child(p_ll, p_ur)

                    # Process Subtree LR
                    p_ll = point(bounds[0][0]+child_width, bounds[0][1])
                    p_ur = point(bounds[1][0], bounds[0][1]+child_height)
                    self.__children[2] = self.__child(p_ll, p_ur)

                    # Process Subtree UR
                    p_ll = point(bounds[0][0]+child_width, bounds[0][1]+child_height)
                    p_ur = bounds[1]
                    self.__children[3] = self.__child(p_ll, p_ur)

                for child in self.__children:
                    child.__generic_add(func, depth-1)

                if all(child.full for child in self.__children):
                    self.__children_full = True
                    for idx in range(4):
                        self.__children[idx] = None
                else:
                    self.__children_full = False

                self.__is_leaf = all(child is None for child in self.__children)

    def __child(self, p_ll, p_ur):
        return self.__class__(p_ll, p_ur, depth=self.__default_depth, debug=self.__debug)

    def __pointInBounds(self, p):
        def func(bounds):
            p_ll, p_ur = bounds
            return (p_ll[0] <= p[0] <= p_ur[0]) and (p_ll[1] <= p[1] <= p_ur[1])
        return func


import torch
import torch.nn as nn
import pickle
from models.AutoEncoder import Autoencoder
from models.CausalConv1d import CausalConv1d

class EncoderConv(nn.Module):
    def __init__(self, config):
        super(EncoderConv, self).__init__()
        self.config = config
        self.encoder = self.load_encoder(config)
        self.seq_conv_1 = nn.Sequential(
            CausalConv1d(in_channels=4 * config['hidden_channels'],
                         out_channels=8 * config['hidden_channels'],
                         kernel_size=2,
                         bias=False),
            nn.ReLU(),
            CausalConv1d(in_channels=8 * config['hidden_channels'],
                         out_channels=8 * config['hidden_channels'],
                         kernel_size=4,
                         bias=False),
            nn.ReLU(),
            CausalConv1d(in_channels=8 * config['hidden_channels'],
                         out_channels=1,
                         kernel_size=8,
                         bias=False),
            nn.ReLU(),
        )
        self.seq_conv_2 = nn.Sequential(
            CausalConv1d(in_channels=4 * config['hidden_channels'],
                         out_channels=8 * config['hidden_channels'],
                         kernel_size=2,
                         bias=False),
            nn.ReLU(),
            CausalConv1d(in_channels=8 * config['hidden_channels'],
                         out_channels=8 * config['hidden_channels'],
                         kernel_size=4,
                         bias=False),
            nn.ReLU(),
            CausalConv1d(in_channels=8 * config['hidden_channels'],
                         out_channels=1,
                         kernel_size=8,
                         bias=False),
            nn.ReLU(),
        )
        self.seq_conv_3 = nn.Sequential(
            CausalConv1d(in_channels=4 * config['hidden_channels'],
                         out_channels=8 * config['hidden_channels'],
                         kernel_size=2,
                         bias=False),
            nn.ReLU(),
            CausalConv1d(in_channels=8 * config['hidden_channels'],
                         out_channels=8 * config['hidden_channels'],
                         kernel_size=4,
                         bias=False),
            nn.ReLU(),
            CausalConv1d(in_channels=8 * config['hidden_channels'],
                         out_channels=1,
                         kernel_size=8,
                         bias=False),
            nn.ReLU(),
        )

        self.dropout = nn.Dropout(config['dropout'])
        self.batch_norm = nn.BatchNorm1d(4 * config['hidden_channels'])

    @staticmethod
    def load_encoder(config, path: str = "encoder_params.ckpt"):
        model = Autoencoder(config)
        model_weights = torch.load(path)["state_dict"]
        # update keys by dropping `nn_model.`
        for key in list(model_weights):
            model_weights[key.replace("nn_model.", "")] = model_weights.pop(key)
        model.load_state_dict(model_weights)
        model.eval()
        x_encoder = model.encoder
        return x_encoder

    def forward(self, x):
        # Encoder Inception
        with torch.no_grad():
            x, _ = self.encoder(x.float())
        x = self.batch_norm(x)
        x = self.dropout(x)
        x1 = self.seq_conv_1(x)
        x2 = self.seq_conv_2(x)
        x3 = self.seq_conv_3(x)
        x = torch.concat([x1, x2, x3], dim=1)
        return x[:, :, -1]

#
# with open('gold_config_CasualRnn.pkl', 'rb') as f:
#     xconfig = pickle.load(f)
#     print(xconfig)
#
# from pre_process import NyDiffNormalizer
# import pandas as pd
# import numpy as np
#
# df = pd.read_csv('gold.csv', )  # , parse_dates=True)
# df['time'] = pd.to_datetime(df['time'])
# df.set_index('time', inplace=True)
# # print(df)
#
# window = xconfig['number_days'] * xconfig['tick_per_day']
# input_to_predict = df.iloc[-13 * window:-12 * window].copy()
# # print(input_to_predict)
# # nydiff = NyDiffNormalizer(ohlc)
# proc = NyDiffNormalizer(input_to_predict.iloc[:, :])
# nn_input_numpy = proc.obs()  # add dummy dim for batch
# nn_input_torch = torch.from_numpy(np.expand_dims(nn_input_numpy, 0))
#
# # print(nn_input_torch)
# nn_input_torch = torch.permute(nn_input_torch, (0, 2, 1))
# model = EncoderConv(xconfig)
# print(model(nn_input_torch).shape)


"""
https://leetcode.com/problems/find-all-anagrams-in-a-string/
Time complexity: O(N ^ 2)
"""

from collections import Counter

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        anagram = Counter()
        count_p = Counter(p)
        result = []

        left = 0
        for right in range(len(s)):
            anagram[s[right]] += 1

            if anagram == count_p:
                result.append(left)

            if right >= len(p) - 1:
                anagram[s[left]] -= 1
                # Add an empty counter to remove elements below zero.
                anagram += Counter()
                left += 1
        return result

Leetcode/Python/438. Find All Anagrams in a String/solution.py


import pandas as pd
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier

def hello(neg,s):
    d16=pd.read_csv('../raw_data/2016-17.csv').set_index('full_name')
    d17=pd.read_csv('../raw_data/2017-18.csv').set_index('full_name')

    #Eliminating all players that dint play in both years
    #sorting alphabetically

    for n in d16.index:
        if n not in d17.index:
            d16.drop(n,0,inplace=True)

    for n in d17.index:
        if n not in d16.index:
            d17.drop(n,0,inplace=True)

    d16.sort_values('full_name',inplace=True)
    d17.sort_values('full_name',inplace=True)

    d16.reset_index(inplace=True)
    d17.reset_index(inplace=True)

    labels=d17['total_points'].to_numpy()
    i=0
    while i<len(labels):
        if labels[i]<110:
            labels[i]=0
        else:
            labels[i]=1
        i+=1

    names=d16['full_name'].to_numpy()

    data=d16.drop('full_name',1).to_numpy()

    train_data=data[:s]
    train_labels=labels[:s]
    test_data=data[s:]
    test_labels=labels[s:]

    model = KNeighborsClassifier(n_neighbors=neg)

    model.fit(train_data, train_labels)

    predictions = model.predict(test_data)

    results=pd.DataFrame()

    results['name']=names[s:]

    results['actual']=test_labels
    results['predicted']=predictions

    return sum(predictions == test_labels)/len(test_labels)

def main():
    num=310
    i=1
    print(num)
    while i<10:
        print('k='+str(i)+'||perc:'+str(hello(i,num)))
        i+=1

if __name__ == "__main__":
    main()


from os import supports_follow_symlinks
import sys
import pretty_midi
import csv
import os
import glob

#../smf B4A4G4E4G4B4A4 slow

if len(sys.argv) < 2:
    print("No argument")
    sys.exit()

# MIDIファイルを概形ファイルへ変換
def midi_to_abs(file_name):
    try:
        # MIDIファイルのロード
        midi_data = pretty_midi.PrettyMIDI(file_name)
    except IOError as ex:
        print(ex, filename)
    # トラック別で取得
    midi_tracks = midi_data.instruments
    # トラック1のノートを取得
    notes = midi_tracks[0].notes
    #　ノートナンバーのリストを作成
    note_number_list = []
    for note in notes:
        note_number_list.append(note.pitch)

    #左に1つずらしたノートナンバーのリストの作成
    n1 = note_number_list[1:] + note_number_list[:1]

    #数値を概形表現へ変換したリストの作成
    note_number_fluc = []
    for i in range(0,len(note_number_list)):
        if (n1[i] == note_number_list[i]):
            note_number_fluc.append("→")
        if (n1[i] == note_number_list[i]+1):
            note_number_fluc.append("#")
        if (n1[i] > note_number_list[i]+1):
            note_number_fluc.append("↑")
        if (n1[i] == note_number_list[i]-1):
            note_number_fluc.append("b")
        if (n1[i] < note_number_list[i]-1):
            note_number_fluc.append("↓")
    note_number_fluc.pop()

    #MIDI概形のテキストファイルを作成
    with open(file_name + "_abs.txt", "w", newline = "") as f:
        w = csv.writer(f)
        w.writerow(note_number_fluc)

    return file_name + "_abs.txt"

#ペース（速い・中間・遅い）の取得
def get_tempo(file_name):
    # MIDIファイルのロード
    midi_data = pretty_midi.PrettyMIDI(file_name)
    # テンポの取得
    tempo = midi_data.get_tempo_changes()
    print(tempo[1])
    if min(tempo[1]) < 90:
        pace = 'slow'
    elif max(tempo[1]) >= 90 and min(tempo[1]) < 140:
        pace = 'middle'
    elif max(tempo[1]) >= 140:
        pace = 'fast'

    return pace

#ずらし表の作成
def create_table(pattern):
    table = [0 for _ in range(len(pattern))]
    j = 0
    for i in range(1, len(pattern)):
        if pattern[i] == pattern[j]:
            j += 1
            table[i] = j
        else:
            table[i] = j
            j = 0
    return table

#KMP法の実装
def kmp_search(string, pattern):
    table = create_table(pattern)
    i = j = 0
    while i < len(string) and j < len(pattern):
        if string[i] == pattern[j]:
            i += 1
            j += 1
        elif j == 0:
            i += 1
        else:
            j = table[j]

    if j == len(pattern):
        return i - j
    else:
        return None

if __name__ == '__main__':
    target_dir = sys.argv[1]
    # 探すメロディパタンの読み込み
    pattern = sys.argv[2]
    # 探すメロディパタンのテンポの読み込み
    user_pace = sys.argv[3]
    # マッチしたファイルリストの作成
    file_list = []

    for filename in os.listdir(target_dir):
        if not filename.endswith('.mid'):
            continue
        path_in = os.path.join(target_dir, filename)
        path_out = midi_to_abs(path_in)
        print(path_out)
        with open(path_out, "r") as f:
            string = f.read()

        path_pace = get_tempo(path_in)

        index = kmp_search(string, pattern)

        if index and path_pace == user_pace:
            file_list.append(filename)
    else:
        print("No files.")
    print(file_list)


# Import Pi Camera library
from picamera import PiCamera
from picamera.array import PiRGBArray

from time import sleep
import datetime as dt
import io, cv2
from PIL import Image

# Import Lobe python library
from lobe import ImageModel

# Import motor controller
from motor import *

# Create a camera object
camera = PiCamera()

# Set size of images - this ideally is the same as the model used to train the images.
imageSize = (160, 128)

rawCapture = PiRGBArray(camera, size=imageSize)
sleep(2)

# Load Lobe TF Lite model
# --> Change model path to match the name of the folder your model is in.
model = ImageModel.load('/home/pi/Desktop/GoPiGo-Explorations/GoPiGo TFLite')

# camera.start_preview(alpha=200)
sleep(2)
# Create the in-memory stream

font                   = cv2.FONT_HERSHEY_SIMPLEX
fontScale              = 0.4
fontColor              = (0,255,255)
thickness              = 1
lineType               = 2

if __name__ == '__main__':

    # Adjust speed factor if your bot is over or under steering.
    speedFactor = 1
    steering = 0

    for frame in camera.capture_continuous(rawCapture, format='bgr', use_video_port=True, resize=imageSize):
        start = dt.datetime.now()  # Capture start time of the loop

        image = frame.array

        # Convert to a PIL format for model
        img = Image.fromarray(image)

        # Perform model prediction
        result = model.predict(img)
        latency = (dt.datetime.now() - start).microseconds  # Capture time difference
        steering = float(result.prediction)
        # camera.annotate_text = 'Steering:' + result.labels[0][0] + " latency" + str(latency)
        print(result.prediction)
        print(latency)

        cv2.putText(image, 'Steering: {}'.format(result.prediction),
                    (0,95),
                    font,
                    fontScale,
                    fontColor,
                    thickness,
                    lineType)

        cv2.putText(image, 'Latency: 0.{}s'.format(latency),
                    (0, 120),
                    font,
                    fontScale,
                    fontColor,
                    thickness,
                    lineType)

        cv2.imshow("Live View", image)

        motor(speedFactor, steering * speedFactor)

        # Clear the stream in preparation for the next frame
        rawCapture.truncate(0)

        key = cv2.waitKey(1) & 0xFF

        # if 'x' key is pressed, break from the loop.
        if key == ord('x'):
            break

    cv2.destroyAllWindows()
    motor(0, 0)


# get old tweets
import GetOldTweets3 as got
import json
import sys
# progrees time
import time
# librerias de las interfaces
import tkinter as tk
import tweepy
# web browser lib
import webbrowser
# creacion de documento
from docx import Document
from docx.shared import Inches
# threads
from threading import Thread
from tkinter import CENTER, Entry, Label, LabelFrame, Menu, Tk, ttk
from tkinter.messagebox import showinfo
# archivos
import os
import errno

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# con este objeto realizaremos todas las llamadas al API
api = tweepy.API(auth, wait_on_rate_limit=True,
                 wait_on_rate_limit_notify=True, compression=True)

document = Document()

class Ventana:
    id_selection = ' '

    # definimos el metodo que creara la interfaz
    def __init__(self, window):
        self.wind = window
        self.wind.title('BUSCATUIT')

        # Creamos frame contenedor
        frame = LabelFrame(self.wind, text="Buscar en twitter")
        frame.grid(row=0, column=0, columnspan=3, pady=20)

        # input busqueda palabra
        Label(frame, text='Buscar palabra: ').grid(row=1, column=0)
        self.name = Entry(frame, width=50)
        self.name.grid(row=1, column=1)
        # botón de busqueda
        self.button = ttk.Button(
            frame, text='Buscar en twitter', command=self.get_twitters)
        self.button.grid(row=1, column=2)
        self.button_limpiar = ttk.Button(
            frame, text='Limpiar tabla', command=self.limpiar_tabla)
        self.button_limpiar.grid(row=1, column=3)
        Label(frame, text='Progreso de la busqueda: ').grid(row=2, column=0)
        self.progress_bar = ttk.Progressbar(
            frame, orient='horizontal', length=286, mode='determinate')
        self.progress_bar.grid(row=2, column=1)
        self.label_desc = Label(frame, text='Num Tweets')
        self.label_desc.grid(row=2, column=2)
        self.label_tweets = Label(frame, text='#')
        self.label_tweets.grid(row=2, column=3)

        # creamos la tabla
        self.tree = ttk.Treeview(height=10, columns=(
            'texto', 'autor', 'descripcion', 'location'))
        self.tree.grid(row=4, column=0, columnspan=2)
        self.tree.heading('#0', text='Id', anchor=CENTER)
        self.tree.heading('texto', text='Texto', anchor=CENTER)
        self.tree.heading('autor', text='Autor', anchor=CENTER)
        self.tree.heading(
            'descripcion', text='Descripcion del perfil', anchor=CENTER)
        self.tree.heading('location', text='Localidad', anchor=CENTER)
        self.tree.bind("<Button-3>", self.popup)

    def get_twitters(self):
        query_word = self.name.get().split()

        t = Thread(target=self.countdown, args=(10, ))

        count = 0

        # fetched_tweets = api.search(self.name.get(), count = 3000) #+ api.search(b, count = 300) + api.search(c, count = 300)
        t.start()
        user_list = api.search_users(query_word)
        for user in user_list:
            time.sleep(0.05)
            self.progress_bar["value"] += count
            self.progress_bar.update()
            # and 'uruguay' in user.location or 'Uruguay' in user.location:
            if len(user.location) != 0:
                self.tree.insert('', 0, text=user.id_str, value=(
                    user.name, user.screen_name, user.description, user.location))
            count += 1

        self.progress_bar["value"] = 0

    def countdown(self, n):
        showinfo('Por favor espere ',
                 'Busqueda terminada se cargaran los resultados')
        time.sleep(n)

    def popup(self, event):
        # mostrar el popup menu
        rowitem = self.tree.identify('item', event.x, event.y)

        if rowitem == '':
            print('Clic derecho en espacio vacio')
        else:
            # usuario da clic en algo
            print('Seleccion correcta')
            self.tree.selection_set(rowitem)
            # id_selection = self.tree.set(self.tree.identify_row(event.y))
            self.id_selection = rowitem
            rcmenu = Menu(self.tree, tearoff=0)
            # rcmenu.add_command(label='Visitar perfil', command=print(item))
            rcmenu.add_command(label='Añadir a la base de datos',
                               command=self.add_database)
            rcmenu.add_command(label='Solo texto', command=self.solo_texto)
            rcmenu.add_command(label='Visitar perfil', command=self.selection)
            rcmenu.add_command(label='Mostrar seguidores',
                               command=self.get_followers)
            rcmenu.post(event.x_root, event.y_root)

    def add_database(self):
        curItem = self.tree.item(self.id_selection)

        path = 'C:/BUSCATUIT/'+curItem['values'][3]+curItem['values'][1]

        showinfo('Guardado iniciado',
                 'Se guardaran los resultados en '+path)

        try:
            os.makedirs(path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        count = 0

        file = open(path+"/"+curItem['values']
                    [1]+".txt", "w", encoding="utf-8")

        try:
            for tweet in tweepy.Cursor(api.user_timeline, user_id=curItem['text'], tweet_mode='extended', include_rts=False).items():
                time.sleep(0.05)
                self.progress_bar["value"] += count
                self.progress_bar.update()
                file.write('Fecha: '+str(tweet.created_at) +
                        ' Texto: '+str(tweet.full_text)+' Ubicacion: '+tweet.user.location+os.linesep)
                print('Trabajando '+str(count))
                self.label_tweets.config(text=str(count))
                count += 1
            self.progress_bar["value"] = 0
        except tweepy.TweepError as e:
            print(e.reason)

        file.close()
        showinfo('Guardado final',
                 'Resultados guardados en '+path)

    def solo_texto(self):
        curItem = self.tree.item(self.id_selection)
        print(curItem['text'])

        path = 'C:/BUSCATUIT/'+curItem['values'][3]+curItem['values'][1]

        showinfo('Guardado iniciado',
                 'Se guardaran los resultados en '+path)

        try:
            os.makedirs(path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        count = 0

        file = open(path+"/"+curItem['values']
                    [1]+"_texto.txt", "w", encoding="utf-8")

        try:
            for tweet in tweepy.Cursor(api.user_timeline, user_id=curItem['text'], tweet_mode='extended', include_rts=False).items():
                time.sleep(0.05)
                self.progress_bar["value"] += count
                self.progress_bar.update()
                if (not tweet.retweeted) and ('RT @' not in tweet.full_text):
                    file.write(str(tweet.full_text)+os.linesep)
                    print('Trabajando '+str(count))
                    self.label_tweets.config(text=str(count))
                    count += 1
        except tweepy.TweepError as e:
            print(e.reason)

        file.close()
        showinfo('Guardado final',
                 'Resultados guardados en '+path)

    def get_followers(self):
        count = 0
        curItem = self.tree.item(self.id_selection)
        for follower_id in api.followers(curItem['values'][1]):
            time.sleep(0.05)
            self.progress_bar["value"] += count
            self.progress_bar.update()
            if len(follower_id.location) != 0:
                self.tree.insert('', 0, text=follower_id.id_str, value=(
                    follower_id.name, follower_id.screen_name, follower_id.description, follower_id.location))

        self.progress_bar["value"] = 0

    def selection(self):
        curItem = self.tree.item(self.id_selection)
        webbrowser.open_new('https://twitter.com/'+curItem['values'][1])

    def limpiar_tabla(self):
        tree_table = self.tree
        for i in tree_table.get_children():
            self.tree.delete(i)

# metodo main de nuestro programa
if __name__ == "__main__":
    window = Tk()
    application = Ventana(window)
    window.mainloop()


from telethon import TelegramClient, sync, events
import configuration
import functions

#Função responsável por mostrar o ID dos grupos
def All():

    client = TelegramClient(configuration.sessao, configuration.api_id, configuration.api_hash)
    client.start()
    dialogs = client.get_dialogs()

    for dialog in dialogs:
        print('------------X-------------')
        if dialog.id < 0:
            print(f'NOME DO GRUPO: {dialog.title}')
            print(f'ID DO GRUPO: {dialog.id}')
        print('------------X-------------')
    client.disconnect()

def MessageCapture(client):
    @client.on(events.NewMessage(chats=[configuration.MINES_ID_ORIGEM, configuration.SPACEMAN_ID_ORIGEM, configuration.FORTUNE_TIGER_ID_ORIGEM,configuration.PENALTY_SHOOT_UP_ID_ORIGEM, configuration.AVIATOR_ID_ORIGEM]))
    async def relay_message(event):
        # MINES_ENVIAR_MENSAGEM
        if event.peer_id.channel_id == 1939194214 and configuration.MINES_PREFERENCE == True:
            print('SIM - MINES REPLICANDO MENSAGEM...')
            message_updated = functions.ChangeLinkAfiliate(event)
            await client.send_message(configuration.MINES_ID_DESTINO, message_updated, link_preview=False,parse_mode='html')
            return
        # SPACEMAN_ENVIAR_MENSAGEM
        if event.peer_id.channel_id == 1681284432 and configuration.SPACEMAN_PREFERENCE == True:
            print('SIM - SPACEMAN REPLICANDO MENSAGEM')
            message_updated = functions.ChangeLinkAfiliate(event)
            await client.send_message(configuration.SPACEMAN_ID_DESTINO, message_updated, link_preview=False,parse_mode='html')
            return
        # FORTUNE_TIGER_ENVIAR_MENSAGEM
        if event.peer_id.channel_id == 1925549581 and configuration.FORTUNE_TIGER_PREFERENCE == True:
            print('SIM - FORTUNE_TIGER REPLICANDO MENSAGEM')
            message_updated = functions.ChangeLinkAfiliate(event)
            await client.send_message(configuration.FORTUNE_TIGER_ID_DESTINO, message_updated, link_preview=False,parse_mode='html')
            return
        # PENALTY_SHOOT_UP_ENVIAR_MENSAGEM
        if event.peer_id.channel_id == 1938509759 and configuration.PENALTY_SHOOT_UP_PREFERENCE == True:
            print('SIM - PENALTY SHOOT UP  REPLICANDO MENSAGEM')
            message_updated = functions.ChangeLinkAfiliate(event)
            await client.send_message(configuration.PENALTY_SHOOT_UP_ID_DESTINO, message_updated, link_preview=False,parse_mode='html')
            return
        # AVIATOR_ENVIAR_MENSAGEM
        if event.peer_id.channel_id == 1592219591 and configuration.AVIATOR_PREFERENCE == True:
            print('SIM - AVIATOR REPLICANDO MENSAGEM')
            await client.send_message(configuration.AVIATOR_ID_DESTINO, event.raw_text)
            return
def ChangeLinkAfiliate(event):
    original_text = event.message.message
    updated_text = original_text.replace("CADASTRE-SE E JOGUE AQUI!", f"CADASTRE-SE E JOGUE AQUI!")

    return updated_text


__author__ = 'tomarovsky'

from pathlib import Path
from os.path import join, dirname
from setuptools import setup, find_packages

dependencies = ['scipy', 'numpy', 'pandas', 'matplotlib', 'biopython',
                'lxml', 'beautifulsoup4', 'requests']

setup(name='Biocrutch',
      version='0.1',
      packages=find_packages(),
      author='Andrey Tomarovsky',
      author_email='andrey.tomarovsky@gmail.com',
      install_requires=dependencies,
      long_description=open(join(dirname(__file__), 'README.md')).read(),
      scripts=list(map(str, sorted(Path('scripts/').rglob("*.py")))))