import random
import json

content_file = 'data_content/content_api.json'
quote_file = 'data_content/quotes_api.json'

# some thoughts, this method is being called mutliple times,
# it might get expensive as we are calling get_article_via_content_api
# each time we call get_home_page_article, get_aritcle_by_uuid, get_random_article,
# another better way of doing this might be to just have the
# get_articles_via_content_api called once and just pass in the results as
# a parameter to methods instead, but due to time
#constraint will leave it like this for now
def get_articles_via_content_api():
    with open(content_file) as content_json_file:
        content_data = json.load(content_json_file)
    content_json_file.close()
    return content_data.get('results')

# get stocks from the api
def get_stocks_via_content_api():
    with open(quote_file) as quote_json_file:
        quote_data = json.load(quote_json_file)
    quote_json_file.close()
    return quote_data

# look in the articles to find article that
# has slug of 10-promise and return it
def get_homepage_article():
    articles_list = get_articles_via_content_api()
    for article in articles_list:
        for item in article['tags']:
            if item['slug'] == '10-promise':
                    return article

# grab the article by uuid, pretty straight forward
def get_article_by_uuid(uuid):
    articles_list = get_articles_via_content_api()
    for article in articles_list:
        if article['uuid'] == uuid:
            return article

# get random articles
def get_random_articles(count):
    articles_list = get_articles_via_content_api()
    result = []

    homepage_article = get_homepage_article()
    # while we have less than 3 or so amount of articles,
    # keep pick a random articles, if the article was already in the
    # result pick another one instead
    # also check to make sure to exclude homepage_article in the result.
    while len(result) < count:
        item = random.choice(articles_list)
        if item !=homepage_article and item not in result:
            result.append(item)

    return result

# get random stcoks
def get_random_stocks(count):
    stocks_list = get_stocks_via_content_api()
    result = []
    # while we have less than 3 or so amount of stocks,
    # keep pick a random stock, if the stock was already in the
    # result pick another one instead
    while len(result) < count:
        stock = random.choice(stocks_list)
        if stock not in result:
            result.append(stock)
    return result


"""Set of handlers related with Sensorpush data
"""

import json
import datetime

from status.util import SafeHandler

class SensorpushBaseHandler(SafeHandler):
    def get_samples(self, start_days_ago=14):
        # A reasonable start time
        start_time = datetime.datetime.now() - datetime.timedelta(days=start_days_ago)
        start_time_str = start_time.strftime("%Y-%m-%dT00:00:00")

        # Fetch all sensor names from the start day
        # If a sensor is missing for that date, it won't be fetched
        sensor_id_view = self.application.sensorpush_db.view(
            "sensor_id/by_date", descending=True
        )
        sensors = [row.value for row in sensor_id_view[start_time_str]]
        if sensors == []:
            return {}

        # Fetch samples from 1 month ago for each sensor
        samples_view = self.application.sensorpush_db.view(
            "entire_document/by_sensor_id_and_date"
        )
        sensor_data = {}
        for sensor_original in sorted(sensors):
            # Make it more suitable to use as a selector.
            sensor = sensor_original.replace(".", "_")
            sensor_data[sensor] = {
                "samples": [],
                "min_temp": 800,
                "max_temp": -300,
                "limit_lower": [],
                "min_limit_lower": 800,
                "limit_upper": [],
                "max_limit_upper": -300,
                "intervals_lower": [],
                "intervals_higher": [],
            }
            for sensor_daily_row in samples_view[
                [sensor_original, start_time_str]:[sensor_original, "9999"]
            ]:
                _, timestamp = sensor_daily_row.key
                doc = sensor_daily_row.value
                sensor_data[sensor]["samples"] += doc["saved_samples"]
                sensor_data[sensor]["intervals_lower"] += doc["intervals_lower"]
                sensor_data[sensor]["intervals_higher"] += doc["intervals_higher"]
                sensor_data[sensor]["sensor_name"] = doc["sensor_name"]

                min_val = 800
                max_val = -300
                for _, temp_val in doc["saved_samples"]:
                    min_val = min(min_val, temp_val)
                    max_val = max(max_val, temp_val)
                sensor_data[sensor]["min_temp"] = min(
                    sensor_data[sensor]["min_temp"], min_val
                )
                sensor_data[sensor]["max_temp"] = max(
                    sensor_data[sensor]["max_temp"], max_val
                )

                sensor_data[sensor]["limit_lower"].append(
                    [timestamp, doc["limit_lower"]]
                )
                sensor_data[sensor]["limit_upper"].append(
                    [timestamp, doc["limit_upper"]]
                )
                if doc["limit_lower"] is not None:
                    sensor_data[sensor]["min_limit_lower"] = min(
                        sensor_data[sensor]["min_limit_lower"], doc["limit_lower"]
                    )
                if doc["limit_upper"] is not None:
                    sensor_data[sensor]["max_limit_upper"] = max(
                        sensor_data[sensor]["max_limit_upper"], doc["limit_upper"]
                    )

            if sensor_data[sensor]["max_limit_upper"] == -300:
                sensor_data[sensor]["max_limit_upper"] = None

            if sensor_data[sensor]["min_limit_lower"] == 800:
                sensor_data[sensor]["min_limit_lower"] = None

        return sensor_data

class SensorpushDataHandler(SensorpushBaseHandler):
    """Serves datapoints for last month of sensorpush temperatures"""

    def get(self):
        start_days_ago = int(self.get_argument("start_days_ago", default="14"))
        sensor_data = self.get_samples(start_days_ago=start_days_ago)
        self.write(json.dumps(sensor_data))

class SensorpushWarningsDataHandler(SensorpushBaseHandler):
    def get(self):
        start_days_ago = int(self.get_argument("start_days_ago", default="1"))
        all_sensor_data = self.get_samples(start_days_ago=start_days_ago)
        sensors_with_warnings = []

        for sensor_id, sensor_data in all_sensor_data.items():
            if any([sensor_data["intervals_lower"], sensor_data["intervals_higher"]]):
                sensors_with_warnings.append(sensor_data["sensor_name"])

        self.write(json.dumps(sensors_with_warnings))

class SensorpushHandler(SensorpushBaseHandler):
    """Serves a page which lists all sensors with temperature info."""

    def get(self):
        sensor_data = self.get_samples(start_days_ago=28)

        t = self.application.loader.load("sensorpush.html")
        self.write(
            t.generate(
                gs_globals=self.application.gs_globals,
                user=self.get_current_user(),
                sensor_data=sensor_data,
            )
        )


def extract_main_sense(synset_id, key):
    """
    """
    import gzip , urllib, urllib.parse, json
    from urllib.request import urlopen, Request
    from io import BytesIO
    service_url = 'https://babelnet.io/v4/getSynset'
    id_synset = synset_id
    params = {'id' : id_synset,'key'  :  key}
    url = service_url + '?' + urllib.parse.urlencode(params)
    request = Request(url)
    request.add_header('Accept-encoding', 'gzip')
    response = urlopen(request)

    if response.info().get('Content-Encoding') == 'gzip':
        buf = BytesIO(response.read())
        f = gzip.GzipFile(fileobj=buf)
        data = json.loads(f.read())
    try:
        return data['mainSense']
    except KeyError:
        return 'CHANGE_KEY_BABELNET'

def look_for_bigger(string, lst, idx, lst_concepts, first):
    """
    """
    if string in lst_concepts:
        return 'UNK'
    else:
        idx +=1
        if idx == len(lst):
            return first
        string = string + ' ' + lst[idx]
        if len(string.split()) == len(lst):
            return first
        if string in lst_concepts:
            return string
        else:
            return look_for_bigger(string, lst, idx, lst_concepts, first)

def extract_question_pattern(question, dict_concepts, synset_ids):
    import nltk
    tokenized_sentence = nltk.word_tokenize(question)
    new_sentence = []
    for idx, token in enumerate(tokenized_sentence):
        new_token = look_for_bigger(token, tokenized_sentence, idx, dict_concepts.values(), first =token)
        if new_sentence != []:
            if new_token in new_sentence[-1]:
                pass
            else:
                new_sentence.append(new_token)
        else:
            new_sentence.append(new_token)
    without_concepts = []
    for word in new_sentence:
        if word in dict_concepts.values():
            without_concepts.append('UNK')
        else:
            without_concepts.append(word)
    return (' '.join(without_concepts), synset_ids)

def build_enriching_tree(onlyQ, selected_synset):
    enriching_tree = {}
    for (q, id1, id2, domain, relation) in onlyQ:
        res = extract_question_pattern(question=q,
            synset_ids=[id1, id2],
            dict_concepts = selected_synset)

        q_parsed = res[0][:-1].strip()
        if q_parsed.count('UNK') == 1:
            if domain not in enriching_tree:
                enriching_tree[domain] = {}
                enriching_tree[domain]['synset_ids'] = set(res[1])
                enriching_tree[domain][relation] = set([q_parsed])
            else:
                enriching_tree[domain]['synset_ids'].update(set(res[1]))
                if relation not in enriching_tree[domain]:
                    enriching_tree[domain][relation] = set([q_parsed])
                else:
                    if q_parsed not in enriching_tree[domain][relation]:
                        enriching_tree[domain][relation].add(q_parsed)

    return enriching_tree

def generate_random_question(enriching_tree, domain, selected):
    import random
    one_domain = enriching_tree[domain]
    all_relations = list(set(one_domain.keys()) - set(['synset_ids']))
    random_relation = random.sample(all_relations,1)[0]
    if len(one_domain[random_relation]) == 1:
        random_question = list(one_domain[random_relation])[0]
    else:
        random_question = random.sample(list(one_domain[random_relation]), 1)[0]
    random_concept = random.sample(list(one_domain['synset_ids']), 1)[0]
    custom_q = random_question.replace('UNK', selected[random_concept])
    return (custom_q, random_relation, random_concept)


#!/usr/bin/env python

from __future__ import print_function

from setuptools import find_packages, setup

import versioneer

install_requires = [
    'iso8601',
    'pytz',
    'requests',
    'simplejson',
    'astropy',
    'six',
]

test_requires = [
    'voeventdb.server[test]',
]

extras_require = {
    'test': test_requires,
    'all': test_requires,
}
packages = find_packages(exclude=('voeventdb.remote.tests',))
print("FOUND PACKAGES: ", packages)

setup(
    name="voeventdb.remote",
    version=versioneer.get_version(),
    cmdclass=versioneer.get_cmdclass(),
    description="Client-library for remotely querying the voeventdb REST API.",
    author="Tim Staley",
    author_email="timstaley337@gmail.com",
    url="https://github.com/timstaley/voeventdb.remote",
    packages=packages,
    install_requires=install_requires,
    extras_require=extras_require,
)


import cv2
import numpy as np
import os
import click

from matrix import center_homography
from probability import Gaussian
from lie_group import *

@click.command()
@click.option(
    "--group-name",
    type=click.Choice(["Euclidean", "Similarity", "Affine", "Homography"]),
    help="The Lie Group to visualize.",
    required=True,
)
def run(group_name: str):
    def noise(sigma: float):
        return Gaussian(0, sigma)

    gaussians = {}
    gaussians["Euclidean"] = np.array([noise(1), noise(50), noise(50)])
    gaussians["Similarity"] = np.array([noise(1), noise(50), noise(50), noise(0.3)])
    gaussians["Affine"] = np.array(
        [noise(1), noise(50), noise(50), noise(0.1), noise(0.15), noise(0.15)]
    )
    gaussians["Homography"] = np.array(
        [
            noise(1),
            noise(50),
            noise(50),
            noise(0.1),
            noise(0.1),
            noise(0.1),
            noise(0.0001),
            noise(0.0001),
        ]
    )

    group = globals()[group_name]

    image_path = os.path.join("assets", f"{group_name.lower()}.png")
    image = cv2.imread(image_path)
    image = cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC)

    h, w = image.shape[:2]
    H = np.identity(3)
    running = True
    while running:
        H_next = LieGroup.random(group, gaussians.get(group_name))

        for t in np.linspace(0, 1, num=60, endpoint=True):
            H_increment = LieGroup.interpolate(H, H_next, t)
            image_warped = cv2.warpPerspective(
                image,
                center_homography(H_increment, w / 2, h / 2),
                (w, h),
                borderMode=cv2.BORDER_CONSTANT,
                borderValue=(255, 255, 255),
            )
            cv2.imshow(f"Lie Group: {group_name}", image_warped)

            # Close the program by pressing 'q' on the keyboard
            if cv2.waitKey(1) == ord("q"):
                running = False
                break

        H = H_next

if __name__ == "__main__":
    run()


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

import os

from celery import shared_task
from celery.utils.log import get_task_logger

logger = get_task_logger(__name__)

@shared_task
def you_get(**kwargs):
    url = kwargs.get('url')
    with_caption = kwargs.get('with_caption')
    form = kwargs.get('form')
    default_path = kwargs.get('default_path')
    output_fn = kwargs.get('output_fn')
    list_download = kwargs.get('list_download')

    cmd = 'you-get'
    if list_download:
        cmd += ' --playlist'
    else:
        if output_fn is not None and output_fn != '':
            cmd += ' -O {}'.format(output_fn)

    cmd += ' --format={} -o {}'.format(form, default_path)

    if not with_caption:
        cmd += ' --no-caption'

    cmd += ' {}'.format(url)

    logger.info('you-get 命令:[{}]'.format(cmd))
    return os.system(cmd)


# M. P. Hayes UCECE
import numpy as np
from ipywidgets import interact, interactive, fixed, interact
from .lib.signal_plot import signal_plot3
from .lib.signal import Signal, signals

def convolution_demo1_plot(x='fang(t)', h='exp(-t) u(t)', t=0.5):

    N = 500
    tmax = 5
    t1 = np.linspace(-tmax, tmax, N)

    dt = t1[1] - t1[0]
    offset = int(-t1[0] / dt)

    x1 = Signal(x)(t - t1)
    x2 = Signal(h)(t1)
    y1 = Signal(x)(t1)

    bar = [max(x2 * Signal(x)(tau - t1)) for tau in t1]

    z1 = x1 * x2
    z = np.convolve(y1, x2)[offset:offset + len(t1)] * dt

    foo = np.trapz(z1, t1)

    fig = signal_plot3(t1, x1, t1, z1, t1, z)
    axes = fig.axes
    axes[0].plot(t1, x2)
    axes[0].legend((r'$x(%s-\tau)$' % t, r'$h(\tau)$'))
    axes[0].set_xlabel(r'$\tau$')
    axes[1].fill_between(t1, 0, z1, facecolor='none', edgecolor='b', hatch='///')
    axes[1].legend((r'$x(%s-\tau) h(\tau)$' % t, ))
    axes[1].set_ylim(0, max(bar))
    axes[1].set_xlabel(r'$\tau$')
    axes[2].plot((t, t), (0, foo), 'r')
    axes[2].plot(t, foo, 'ro')
    axes[2].legend((r'$y(t)$', ))
    axes[2].set_xlabel('$t$')

    fig.tight_layout()

def convolution_demo1():
    interact(convolution_demo1_plot, x=signals, h=signals,
             t=(-5, 5, 0.1), continuous_update=False)


import pandas as pd
import re
import operator
import string
import nltk
import sys
from nltk.tag import pos_tag
def removePunc(x):
  x = re.sub(r'[@#]\w+', '', x) #taking out hashtags and @
  x = re.sub(r'(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)', '', x) #taking out links
  x = re.sub(r'[!?\.,\'\"]+', '', x) #taking out punctuation i.e ? ! . ' and "
  return x.strip()

def findProperNouns(sen):
    tagged_sent = pos_tag(sen.split())
    propernouns = [word for word,pos in tagged_sent if pos == 'NNP']
    return propernouns

def GETHOSTNAMES(year):
  gg = pd.read_json('gg2013.json', orient='records')
  gg1 = pd.read_json('gg2015.json', orient='records')
  copy = gg.copy(deep=True)
  if year == '2015':
    copy = gg1.copy(deep=True)
  copy['text'] = copy['text'].apply(lambda x: removePunc(str(x)))
  test = list(copy['text'].values)
  s = list(set(test))
  df = pd.DataFrame(s, columns=['text'])
  df = df.loc[df['text'].str.contains('host')]
  df['ProperNouns'] = df['text'].apply(lambda x: findProperNouns(x))
  NGRAMS_DICT = {}
  for i in range(len(df)):
    PN = df['ProperNouns'].iloc[i]
    if len(PN) % 2 == 0:
      for j in range(0, len(PN), 2):
        name = PN[j] + ' ' + PN[j+1]
        if name in NGRAMS_DICT:
          NGRAMS_DICT[name] += 1
        else:
          NGRAMS_DICT[name] = 1
  cd = sorted(NGRAMS_DICT.items(),key=operator.itemgetter(1),reverse=True)
  top3 = [i[0] for i in cd[0:3]]
  return top3

user_in = sys.argv[1]
host = GETHOSTNAMES(user_in)
print(host)


import gzip
import csv
import math
import distance

def create_kth_walkers_reader(model_options, max_time = None):
    assert 'filepath' in model_options

    # To support offsets, we would introduce a dictionary to translate ids and ignore ids that are destroyed before the offset
    # we would then create all the nodes and set their respective initial positions
    # for all other lines, we need to translate the ids and subtract the offset (also handling the destination time!)

    path = model_options['filepath']
    path = path.replace('/app/sim/data/kth_walkers', 'data/kth_walkers')

    with gzip.open(path,'rt') as f:
        reader = csv.reader(f, delimiter=' ')
        for r in reader:
            if max_time is not None and float(r[0]) > max_time:
                break
            yield r

def walkers_to_line_gen(num_proxy_nodes, model_options):
    assert 'filepath' in model_options

    kw_reader = create_kth_walkers_reader(model_options)

    # we first disable all nodes (including node 0)
    for i in range(num_proxy_nodes+1):
        yield "0 disable {}".format(i)

    # we then set node 0 position and enable it
    yield "0 set 0 783.2 1639.4 0"
    # we then enable node 0
    yield "0 enable 0"

    # we then go read through walkers file and translate the commands, ignoring non-existant devices
    for r in kw_reader:
        ts = r[0]
        us = math.floor(float(ts)*1000000)
        cmd = r[1]
        id = r[2]

        if int(id) > num_proxy_nodes:
            continue    # we ignore this node since it does have a corresponding device anyway

        if cmd == 'setdest': # "6.0 setdest 1 818.0 1453.0 0.0 0.8 6.6"
            pos_x = r[3]
            pos_y = r[4]
            pos_z = r[5]
            dest_time_us = math.floor(float(r[7])*1000000)

            assert dest_time_us >= us
            # <ts> move <id> <x> <y> <z> <duration>
            yield "{} move {} {} {} {} {}".format(str(us), id, pos_x, pos_y, pos_z, str(dest_time_us-us))
        elif cmd == 'create': # 6.0 create 2 628.2 1812.1 0.0
            pos_x = r[3]
            pos_y = r[4]
            pos_z = r[5]
            yield "{} set {} {} {} {}".format(str(us), id, pos_x, pos_y, pos_z)
            yield "{} enable {}".format(str(us), id)
        elif cmd == 'destroy': # 132.6 destroy 4
            yield "{} disable {}".format(str(us), id)
            yield "{} set {} 0 0 0".format(str(us), id)
        else:
            print("Warning: could not find cmd: " + r)

def walkers_get_num_created_nodes_until(max_time, model_options):
    num_created = 0

    kw_reader = create_kth_walkers_reader(model_options, max_time=max_time)
    for r in kw_reader:
        if r[1] == 'create':
            num_created += 1
    return num_created

def get_node_lifetimes(max_time, model_options):
    assert max_time > 0

    kw_reader = create_kth_walkers_reader(model_options, max_time=max_time)

    lifetimes = {}
    lifetimes[0] = (0.0, max_time)

    for r in kw_reader:
        if r[1] == 'create':
            assert int(r[2]) not in lifetimes
            lifetimes[int(r[2])] = (float(r[0]), max_time) # we assume they stay "alive" until the end
        if r[1] == 'destroy':
            assert int(r[2]) in lifetimes
            lifetimes[int(r[2])] = (lifetimes[int(r[2])][0], min(lifetimes[int(r[2])][1], float(r[0]))) # we assume they stay "alive" until the end
    return lifetimes

def get_bounds(model_options):

    xs = []
    ys = []

    kw_reader = create_kth_walkers_reader(model_options)

    for r in kw_reader:
        if r[1] == 'create' or r[1] == 'setdest':
            xs.append(float(r[3]))
            ys.append(float(r[4]))

    return (min(xs), min(ys)), (max(xs), max(ys))

def get_max_density(model_options, max_time = None):
    ((min_x, min_y), (max_x, max_y)) = get_bounds(model_options)

    kw_reader = create_kth_walkers_reader(model_options, max_time=max_time)

    num_created = 0
    num_destroyed = 0
    max_concurrent = 0
    max_concurrent_time = None
    max_available_time = None

    for r in kw_reader:
        if max_time and float(r[0]) > max_time:
            max_available_time = None
            break
        if r[1] == 'create':
            num_created += 1
        if r[1] == 'destroy':
            num_destroyed += 1
        concurrent = num_created-num_destroyed
        if concurrent > max_concurrent:
            max_concurrent = concurrent
            max_concurrent_time = r[0]
        max_available_time = r[0]

    print('concurrent', max_concurrent, max_concurrent_time, 'created', num_created, 'max_available', max_available_time)

    area = (max_x-min_x)*(max_y-min_y)
    print((max_x-min_x), (max_y-min_y), area, max_concurrent, max_concurrent/(area/1000000.0))

def create_contact_pairs(max_time, model_options, dist_limit=20.0, step_s=1.0):
    assert 'filepath' in model_options
    assert max_time > 0

    assert step_s == 1.0 # other is currently not supported...

    node_lifetimes = get_node_lifetimes(max_time, model_options)
    num_proxy_nodes = len(node_lifetimes)-1

    import dist_writer
    pos_iter = dist_writer.line_to_position_iterator(num_proxy_nodes, walkers_to_line_gen(num_proxy_nodes, model_options))

    # the dictionary that contains (start_time, end_times) tuples under each key (a,b) with a > b
    contact_pairs = {}

    # contains the start time of the current contact
    contact_pairs_start = {}

    for a in range(num_proxy_nodes+1):
        for b in  range(num_proxy_nodes+1):
            if a <= b:
                continue
            contact_pairs[(a,b)] = []
            contact_pairs_start[(a,b)] = None
    t = 0
    while t <= max_time:
        (ts, positions) = next(pos_iter)
        alive_nodes = [k for k in node_lifetimes if node_lifetimes[k][0] <= t <= node_lifetimes[k][1]]

        for a in alive_nodes:
            for b in alive_nodes:
                if a <= b:
                    continue

                pos_a = positions[a]
                pos_b = positions[b]
                diff_x = pos_a[0] - pos_b[0]
                diff_y = pos_a[1] - pos_b[1]
                dist = math.sqrt(diff_x * diff_x + diff_y * diff_y)

                if dist > dist_limit:
                    if contact_pairs_start[(a,b)] is not None: # connection breaks!
                        contact_pairs[(a,b)].append((contact_pairs_start[(a,b)], t))
                        contact_pairs_start[(a,b)] = None
                elif contact_pairs_start[(a,b)] is None:
                    contact_pairs_start[(a,b)] = t  # we start a new contact!
        t += 1

    # in the end, we need close all contacts based on the nodes lifetimes
    for a in range(num_proxy_nodes+1):
        for b in  range(num_proxy_nodes+1):
            if a <= b:
                continue

            if contact_pairs_start[(a,b)] is not None:
                contact_pairs[(a,b)].append((contact_pairs_start[(a,b)], min(node_lifetimes[a][1], node_lifetimes[b][1])))
                contact_pairs_start[(a,b)] = None

    return contact_pairs

def simulate_broadcast(max_time, model_options, dist_limit=20.0, setup_time=20.0, source_index=0):
    assert 'filepath' in model_options
    assert max_time > 0

    node_lifetimes = get_node_lifetimes(max_time, model_options)
    num_proxy_nodes = len(node_lifetimes)-1

    node_informed = {}
    for k in node_lifetimes:
        node_informed[k] = None
    node_informed[source_index] = 0 # source device is informed from the start

    conn_start = {}    # dict with key (a,b) and the second the connection did start (or None)

    import dist_writer
    pos_iter = dist_writer.line_to_position_iterator(num_proxy_nodes, walkers_to_line_gen(num_proxy_nodes, model_options))

    t = 0
    while t <= max_time:
        (ts, positions) = next(pos_iter)
        alive_nodes = [k for k in node_lifetimes if node_lifetimes[k][0] <= t <= node_lifetimes[k][1]]
        # iterate over all alive nodes until no new nodes were informed

        while True:
            newly_informed = False

            for a in alive_nodes:
                for b in alive_nodes:
                    if a <= b:   # otherwise already checked (or the same node) note that we
                        continue
                    pos_a = positions[a]
                    pos_b = positions[b]
                    diff_x = pos_a[0] - pos_b[0]
                    diff_y = pos_a[1] - pos_b[1]
                    dist = math.sqrt(diff_x * diff_x + diff_y * diff_y)

                    if dist > dist_limit:   # connection breaks!
                        conn_start[(a,b)] = None # we reset the connection
                    else:
                        if (a,b) not in conn_start or conn_start[(a,b)] is None : # connection could already have been started
                            conn_start[(a,b)] = t
                        assert conn_start[(a,b)] <= t

                        if t-conn_start[(a,b)] >= setup_time:
                            if node_informed[a] is not None and node_informed[b] is None:   # node_informed[a] <= t anyway
                                # a informs b -> newly informed so we might need to inform others as well!
                                node_informed[b] = t
                                newly_informed = True
                            elif node_informed[b] is not None and node_informed[a] is None:
                                # b informs a -> newly informed so we might need to inform others as well!
                                node_informed[a] = t
                                newly_informed = True
            if not newly_informed: # if we informed someone we need to go through all pairs again and check if new ones could get informed
                break
        t += 1
    return node_informed

def dist_time_iters(max_time, model_options):
    num_proxy_nodes = walkers_get_num_created_nodes_until(max_time, model_options)
    import dist_writer
    tp_iter = dist_writer.line_to_position_iterator(num_proxy_nodes, walkers_to_line_gen(num_proxy_nodes, model_options))
    return distance.time_dist_iters_from_pos_iter(tp_iter, num_proxy_nodes+1)

import numpy as np
def eval_lt_stats(max_time, model_options):
    lt = get_node_lifetimes(max_time, model_options)
    lts = []
    for k in lt:
        if lt[k][1] < max_time:
            lts.append(lt[k][1]-lt[k][0])
    return np.mean(lts), np.std(lts), np.percentile(np.array(lts), [2.5, 97.5])

if __name__ == "__main__":

    # print('001', eval_lt_stats(36000, {'filepath': 'data/kth_walkers/sparse_run1/ostermalm_001_1.tr.gz'}))
    # print('002', eval_lt_stats(36000, {'filepath': 'data/kth_walkers/sparse_run1/ostermalm_002_1.tr.gz'}))
    # print('003', eval_lt_stats(36000, {'filepath': 'data/kth_walkers/sparse_run1/ostermalm_003_1.tr.gz'}))
    # print('004', eval_lt_stats(36000, {'filepath': 'data/kth_walkers/sparse_run1/ostermalm_004_1.tr.gz'}))
    # print('005', eval_lt_stats(36000, {'filepath': 'data/kth_walkers/sparse_run1/ostermalm_005_1.tr.gz'}))
    # exit()
    #
    print('001')
    get_max_density({'filepath': 'data/kth_walkers/sparse_run1/ostermalm_001_1.tr.gz'}, max_time=3600)
    #print('002')
    #get_max_density({'filepath': 'data/kth_walkers/sparse_run1/ostermalm_002_1.tr.gz'})
    print('003')
    get_max_density({'filepath': 'data/kth_walkers/sparse_run1/ostermalm_003_1.tr.gz'}, max_time=3600)
    #print('004')
    #get_max_density({'filepath': 'data/kth_walkers/sparse_run1/ostermalm_004_1.tr.gz'})
    print('005')
    get_max_density({'filepath': 'data/kth_walkers/sparse_run1/ostermalm_005_1.tr.gz'}, max_time=3600)

    #
    # print('007')
    # get_max_density({'filepath': 'data/kth_walkers/medium_run1/ostermalm_007_1.tr.gz'})
    # print('009')
    # get_max_density({'filepath': 'data/kth_walkers/medium_run1/ostermalm_009_1.tr.gz'})
    # print('011')
    # get_max_density({'filepath': 'data/kth_walkers/medium_run1/ostermalm_011_1.tr.gz'})
    # print('015')
    # get_max_density({'filepath': 'data/kth_walkers/medium_run1/ostermalm_015_1.tr.gz'})
    # print('020')
    # get_max_density({'filepath': 'data/kth_walkers/medium_run1/ostermalm_020_1.tr.gz'})
    # print('030')
    # get_max_density({'filepath': 'data/kth_walkers/dense_run1/ostermalm_030_1.tr.gz'})
    # print('040')
    # get_max_density({'filepath': 'data/kth_walkers/dense_run1/ostermalm_040_1.tr.gz'})
    # print('050')
    # get_max_density({'filepath': 'data/kth_walkers/dense_run1/ostermalm_050_1.tr.gz'})
    # print('070')
    # get_max_density({'filepath': 'data/kth_walkers/dense_run1/ostermalm_070_1.tr.gz'})
    # print('090')
    # get_max_density({'filepath': 'data/kth_walkers/dense_run1/ostermalm_090_1.tr.gz'})

    #with gzip.open('data/kth_walkers/dense_run1/ostermalm_090_1.tr.gz','rt') as f:
    #with gzip.open('data/kth_walkers/medium_run1/ostermalm_007_1.tr.gz','rt') as f:

    max_time = 3600
    kw_reader = create_kth_walkers_reader({'filepath': 'data/kth_walkers/sparse_run1/ostermalm_001_1.tr.gz'}, max_time=max_time)

    num_created = 0
    num_destroyed = 0
    max_concurrent = 0
    max_concurrent_time = None

    min_x = None
    min_y = None
    max_x = None
    max_y = None

    for r in kw_reader:
        if max_time and float(r[0]) > max_time:
            break
        if r[1] == 'create':
            num_created += 1
        if r[1] == 'destroy':
            num_destroyed += 1
        concurrent = num_created-num_destroyed
        if concurrent > max_concurrent:
            max_concurrent = concurrent
            max_concurrent_time = r[0]

        if r[3] == 'destroy':
            num_destroyed += 1

    print('concurrent', max_concurrent, max_concurrent_time, 'created', num_created)

    #test_gen = walkers_to_line_gen(100, {'filepath': 'data/kth_walkers/sparse_run1/ostermalm_005_1.tr.gz'})

    #for line in test_gen:
    #    print(line)

    # 001: 57 12156.6 created 2092
    # 005: 233 3057.6 created 4227
    # 007: 301 4387.8 created 5706


import torch
from text_classification.data_process import seq2index, padding_seq
import os
from config import GPU_NUM

path = os.path.dirname(__file__)

if torch.cuda.is_available():
    device = torch.device(f'cuda:{GPU_NUM}')
else:
    device = torch.device('cpu')

model = torch.load(path + '/text_cnn.p').to(device)
model.eval()

def classification_predict(s):
    s = seq2index(s)
    if torch.cuda.is_available():
        s = torch.from_numpy(padding_seq([s])).cuda().long()
    else:
        s = torch.from_numpy(padding_seq([s])).long()

    out = model(s)
    return out.cpu().data.numpy()

if __name__ == '__main__':
    while 1:
        s = input('句子：')
        print(classification_predict(s))


#!/usr/bin/python
###############################################################################
#
#
#   Handles the actual setting up of the Requests classes
#
#   Takes in a URL and optional headers and fields to be encoded.
#   If the 'get' flag is True, no data is sent the Request builder, and you
#   end up with a GET request instead of a POST request
#
#   1-9-2018 Added Proxy Support
#
###############################################################################

import requests
import urllib2
import urllib
import cookielib
import mechanize

class pageRequest():

    def __init__(self,url,headers=False,fFields=False,get=False,proxy=False,UA=False):
        self.initURL=url
        self.proxy=proxy

#   Set headers if they are provided; otherwise, set basic default headers

        if headers:
            self.headers=headers
        elif UA:
            #   User-supplied User Agent?
            self.headers={
                'HTTP_USER_AGENT': UA,
                'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        else:
            self.headers={
                'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13',
                'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8',
                'Content-Type': 'application/x-www-form-urlencoded'
            }

#    Set form fields if provided; otherwise, GET method is used

        if fFields:
            self.formFields=fFields
            self.encodedFields=urllib.urlencode(self.formFields)

#    If the GET flag is True, request is a GET request; otherwise, it is a POST request

        if get:
            if proxy:
                self.req=urllib2.Request(self.initURL,headers=self.headers)
        else:
            self.req=urllib2.Request(self.initURL,self.encodedFields,self.headers)

    def __call__(self):

#   Option to call the URL with less sophisticated method

        return urllib2.urlopen(self.req)

class pageRequestMech():
    # proxy = {'http':ip:port}
    def __init__(self,headers=False,fFields=False,get=False,proxy=False,UA=False):
        self.proxy=proxy

#   Set headers if they are provided; otherwise, set basic default headers

        if headers:
            self.headers=headers
        elif UA:
            #   User-supplied User Agent?
            self.headers={
                'HTTP_USER_AGENT': UA,
                'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        else:
            self.headers={
                'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13',
                'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8',
                'Content-Type': 'application/x-www-form-urlencoded'
            }

#    Set form fields if provided; otherwise, GET method is used

        if fFields:
            self.formFields=fFields
        br=mechanize.Browser()
        cj=cookielib.LWPCookieJar()
        br.set_cookiejar(cj)

        # Browser Options

        br.set_handle_equiv(True)
        br.set_handle_gzip(True)
        br.set_handle_redirect(True)
        br.set_handle_referer(True)
        br.set_handle_robots(False)

        # Follows refresh 0 but not hangs on refresh > 0

        br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
        if proxy:
            try:
                br.set_proxies(proxy)
            except:
                raise "Proxy not formatted correctly. Format is {'http':ip:port}"
        headerList=[]
        for key in self.headers.keys():
            headerList.append((key,self.headers[key]))
        br.addheaders=headerList

        self.req=br


import pytest
from code_challenges.data_structures.linked_list.linked_list import Node
from code_challenges.data_structures.linked_list.linked_list import LinkedList
from code_challenges.data_structures.linked_list.linked_list import merge_lists

def test_linked_list():
    test_list = LinkedList()
    actual = test_list.head
    expected = None
    assert actual == expected

def test_node():
    test_node = Node(10)
    actual = test_node.value
    expected = 10
    assert actual == expected
    actual = test_node.next
    expected = None
    assert actual == expected

def test_append():
    append_test_list = LinkedList()
    append_test_list.append(20)
    actual = append_test_list.head.value
    excepted = 20
    assert actual == excepted
    actual = append_test_list.head.next
    excepted = None
    assert actual == excepted

def test_includes_found(sample_linked_list):
    actual = sample_linked_list.includes(30)
    expected = True
    assert actual == expected

def test_insert_before_found(sample_linked_list):
    sample_linked_list.insert_before(20, 15)
    actual = sample_linked_list.head.next.value
    expected = 15
    assert actual == expected

def test_insert_before_not_found(sample_linked_list):
    actual = sample_linked_list.insert_before(2, 15)
    expected = 'Search value not found'
    assert actual == expected

def test_insert_after_found(sample_linked_list):
    sample_linked_list.insert_after(20, 15)
    actual = sample_linked_list.head.next.next.value
    expected = 15
    assert actual == expected

def test_insert_after_not_found(sample_linked_list):
    actual = sample_linked_list.insert_after(2, 15)
    expected = 'Search value not found'
    assert actual == expected

def test_kth_happy_path(sample_linked_list):
    actual = sample_linked_list.kth(2)
    expected = 20
    assert actual == expected

def test_kth_expected_failure(sample_linked_list):
    actual = sample_linked_list.kth(4)
    expected = 'Input value is greater than the length of the list.'
    assert actual == expected

def test_kth_edge_case(sample_linked_list):
    actual = sample_linked_list.kth(-1)
    expected = 'Invalid input value. Input must be greater than -1.'
    assert actual == expected

def test__str__(sample_linked_list):
    actual = sample_linked_list.__str__()
    expected = '10 -> 20 -> 30 -> 40 -> None'
    assert actual == expected

def test__str__empty_list():
    empty_linked_list = LinkedList()
    actual = empty_linked_list.__str__()
    expected = 'None'
    assert actual == expected

def test_merge_lists_happy_path(sample_linked_list, sample_linked_list_b):
    merged_ll = merge_lists(sample_linked_list, sample_linked_list_b)
    actual = merged_ll.__str__()
    expected = '10 -> 15 -> 20 -> 25 -> 30 -> 35 -> 40 -> 45 -> 55 -> 65 -> None'
    assert actual == expected

def test_merge_lists_one_empty_ll(sample_linked_list, empty_ll):
    merged_ll = merge_lists(sample_linked_list, empty_ll)
    actual = merged_ll.__str__()
    expected = '10 -> 20 -> 30 -> 40 -> None'
    assert actual == expected

def test_merge_lists_two_empty_ll(empty_ll):
    merged_ll = merge_lists(empty_ll, empty_ll)
    actual = merged_ll.__str__()
    expected = 'None'
    assert actual == expected

@pytest.fixture
def sample_linked_list():
    linked_list = LinkedList()
    linked_list.append(10)
    linked_list.append(20)
    linked_list.append(30)
    linked_list.append(40)
    return linked_list

@pytest.fixture
def sample_linked_list_b():
    linked_list = LinkedList()
    linked_list.append(15)
    linked_list.append(25)
    linked_list.append(35)
    linked_list.append(45)
    linked_list.append(55)
    linked_list.append(65)
    return linked_list

@pytest.fixture
def empty_ll():
    return LinkedList()


import logging
import os

import yaml
from flask import (
    Flask,
    current_app,
    flash,
    g,
    json,
    jsonify,
    redirect,
    request,
    url_for,
)
from oauthlib.oauth2.rfc6749.utils import list_to_scope, scope_to_list
from sqlalchemy import create_engine

from benwaonline_auth import config, models
from benwaonline_auth.bwoauth import auth
from benwaonline_auth.cache import cache
from benwaonline_auth.database import db
from benwaonline_auth.oauth import oauth

with open("jwks.json", "r") as f:
    JWKS = json.load(f)

def setup_logger_handlers(app):
    sh = logging.StreamHandler()
    sh.setFormatter(
        logging.Formatter(
            "%(asctime)s %(levelname)s: %(message)s " "[in %(pathname)s:%(lineno)d]"
        )
    )
    sh.setLevel(logging.DEBUG)
    app.logger.addHandler(sh)

def create_app(config_name=None):
    """
    Returns the Flask app.
    """
    app = Flask(__name__)

    if not config_name:
        config_name = os.getenv("FLASK_ENV")

    if config_name == "production":
        setup_logger_handlers(app)

    app.config.from_object(config.app_config[config_name])

    db.init_app(app)
    oauth.init_app(app)
    cache.init_app(
        app,
        config={
            "CACHE_TYPE": "redis",
            "CACHE_DEFAULT_TIMEOUT": 5,
            "CACHE_REDIS_HOST": os.getenv("REDIS_HOST"),
            "CACHE_REDIS_PORT": os.getenv("REDIS_PORT"),
            "CACHE_KEY_PREFIX": "benwaonline-auth:",
        },
    )

    app.register_blueprint(auth)

    @app.cli.command()
    def initdb():
        """Initialize the database."""
        init_db(app)
        permissions = permissions_loader("benwaonline_auth/scopes.yml")
        init_clients(app, db.session, permissions)

    if config_name != "prod":

        @app.route("/.well-known/jwks.json")
        def jwks():
            return jsonify(JWKS), 200

    return app

def init_db(app):
    engine = create_engine(app.config["SQLALCHEMY_DATABASE_URI"])
    # 20$ says I run this on production
    engine.execute("DROP DATABASE benwaonlineauth")
    engine.execute("CREATE DATABASE benwaonlineauth")
    engine.execute("USE benwaonlineauth")

    import benwaonline_auth.models

    db.create_all()

# need a better way to pull all this info
# refactor this to a click script tbh
def init_clients(app, session, default_scopes=None):
    scopes = default_scopes or ["ham", "eggs"]
    client = models.Client(
        name="BenwaOnline",
        client_id=app.config["CLIENT_ID"],
        client_secret=app.config["CLIENT_SECRET"],
        grant_type="authorization_code",
        response_type="code",
        _redirect_uris="http://127.0.0.1:5000/authorize/callback",
        default_scopes=list_to_scope(scopes),
        allowed_scopes=list_to_scope(scopes),
    )
    session.add(client)
    session.commit()

    return

def permissions_loader(fpath):
    with open(fpath, "r") as f:
        settings = yaml.load(f)

    resources = settings["resources"]

    permissions = []
    for k, v in resources.items():
        permissions.extend([k + ":" + p for p in v["permissions"]])

    return permissions


import streamlit as st
import requests
from PIL import Image
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np

MODEL_URL = 'https://docs.google.com/uc?export=download&id=1-EpKUQQZNDdlwf4yrao6iKdK7D_LGMsd'
MODEL_PATH = 'model.h5'

def reshape_img(image):
    resized_img = image.resize((64, 64))
    resized_img = np.array(resized_img) / 255.0
    resized_img = np.expand_dims(resized_img, axis=0)
    return resized_img

st.title('개와 고양이를 분류해드립니다!')
st.subheader('분류해보고 싶은 개나 고양이 사진을 업로드하세요.')

img_file = st.file_uploader('이미지를 업로드하세요.', type=['png', 'jpg', 'jpeg'])

if img_file is not None:
    image = Image.open(img_file)
    st.image(image, caption='Uploaded Image.', use_column_width=True)

    if not tf.io.gfile.exists(MODEL_PATH):
        with requests.get(MODEL_URL, stream=True) as r:
            r.raise_for_status()
            with open(MODEL_PATH, 'wb') as f:
                for chunk in r.iter_content(chunk_size=8192):
                    f.write(chunk)

    with tf.keras.utils.custom_object_scope({'KerasLayer': hub.KerasLayer}):
        model = tf.keras.models.load_model(MODEL_PATH)

    resized_image = reshape_img(image)
    prediction = model.predict(resized_image)

    if prediction[0][0] < prediction[0][1]:
        st.write('예측 결과: 고양이')
    else:
         st.write('예측 결과 : 개')
else:
    st.subheader('이미지를 업로드하세요!')


import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from data import read_data, DataLoader
from gnn import GNN
import numpy as np
import random
import json
import time
import math
import sys
import os
from tqdm import tqdm
from constants import *

import argparse

parser = argparse.ArgumentParser(description='PyTorch')
parser.add_argument('--epochs', type=int, default=100, metavar='N', help='number of epochs to train (default: 20)')
parser.add_argument('--batch', type=int, default=1, help='mini-batch size')
parser.add_argument('--lr', type=float, default=0.001, metavar='LR', help='learning rate (default: 0.001)')
parser.add_argument('--wd', type=float, default=0., help='weight decay (default: 0)')
parser.add_argument('--cuda', action='store_true', default=True, help='enable CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)')
parser.add_argument('--patience', type=int, default=5, help='number of times to observe worsening validation set error before giving up')
parser.add_argument('--d_embed', type=int, default=64, help='character embedding dimension')
parser.add_argument('--filter_sizes', type=str, default='2,3,4', help='character cnn filter size')
parser.add_argument('--n_filter', type=int, default=64, help='character cnn filter number')
parser.add_argument('--d_pos_embed', type=int, default=32, help='additional feature')
parser.add_argument('--d_graph', type=str, default='128', help='lstm and graph layer dimension')
parser.add_argument('--dropout', type=float, default=0.1, help='dropout probability')
parser.add_argument('--model', type=str, default='lstm-gcn-lstm', choices=['lstm', 'lstm-lstm', 'lstm-gcn-lstm', 'lstm-rgcn-lstm', 'lstm-gat-lstm'], help='model')
parser.add_argument('--crf', action='store_true', default=False, help='final crf')
parser.add_argument('--save_path', type=str, default='models/output', help='output name')
parser.add_argument('--data_path', type=str, default='./', help='data path')
parser.add_argument('--case', type=str, default='final_train_case.txt,final_valid_case.txt,final_test_case.txt', help='case list file')
parser.add_argument('--globalnode', action='store_true', default=False, help='add global node')

parser.add_argument('--test', action='store_true', default=False, help='test mode')
# parser.add_argument('--testmodel', type=str, default='', help='test model')
# parser.add_argument('--testbase', type=str, default='./', help='test base path')
# parser.add_argument('--testcase', type=str, default='tmp_test.txt')

args = parser.parse_args()

args.filter_sizes = [int(x) for x in args.filter_sizes.split(',')]
args.d_graph = [int(x) for x in args.d_graph.split(',')]
# if args.testmodel == '':
#     args.testmodel = args.output+'.model'
# if args.model == 'lstm+g':
#     args.final = 'linear'

label2id = tag2id
d_output = len(label2id)

print(args)
result_obj = {}

def acc_to_str(acc):
    s = ['%s:%.3f'%(label, acc[label]) for label in acc]
    return ' '.join(s)

def to_var(tensors, cuda=True):
    if cuda:
        return [Variable(t, requires_grad=False).cuda() for t in tensors]
    else:
        return [Variable(t, requires_grad=False) for t in tensors]

def evaluate(model, dataloader, output=False, args=args):
    # (hit, pred_cnt, gold_cnt)
    count = {label:[0, 0, 0] for label in label2id if label != 'O'}
    n_correct = 0
    n_total = 0
    current_case_id = None
    output_file = None
    model.eval()
    eval_log = open(args.save_path+'_eval.log','w')
    for tensors, batch in tqdm(dataloader, file=eval_log, mininterval=30): # tqdm(dataloader, leave=False):
        data, data_word, pos, length, mask, label, adjs = to_var(tensors, cuda=args.cuda)
        batch_size, docu_len, sent_len, word_len = data.size()

        logit = model(data, data_word, pos, length, mask, adjs).view(-1, d_output)

        if args.crf:
            logit = logit.view(batch_size*docu_len, sent_len, -1)
            mask = mask.view(batch_size*docu_len, -1)
            _, pred = model.crf_layer.viterbi_decode(logit, mask)
            pred = pred.data
        else:
            pred = logit.max(dim=1)[1]

        if output:
            prob = F.softmax(logit.view(batch_size, docu_len, sent_len, -1), dim=3).data
            for i, data in enumerate(batch):
                if data.case_id != current_case_id:
                    current_case_id = data.case_id
                    if model.crf:
                        filename = 'prob_%s_crf.json' % model.model
                    else:
                        filename = 'prob_%s.json' % model.model
                    output_file = open(data.path+filename, 'w')
                obj = {'id':{'case':data.case_id,'doc':data.doc_id,'page':data.page_id}, 'prob':[]}
                for j in range(data.num_sent):
                    obj['prob'].append(prob[i,j,:len(data.sents[j])].tolist())
                output_file.write(json.dumps(obj) + '\n')

        mask = mask.data
        label = label.data
        pred = pred.contiguous().view(-1).masked_select(mask.view(-1))
        gold = label.contiguous().view(-1).masked_select(mask.view(-1))

        for label, idx in label2id.items():
            if label == 'O':
                continue
            pred_cnt = pred.eq(idx).sum()
            gold_cnt = gold.eq(idx).sum()
            hit = pred.eq(idx).mul(gold.eq(idx)).sum()
            count[label][0] += int(hit)
            count[label][1] += int(pred_cnt)
            count[label][2] += int(gold_cnt)

        n_total += length.data.sum()
        n_correct += pred.eq(gold).sum()

    eval_log.close()
    os.remove(args.save_path+'_eval.log')

    prec, recall, f1 = {}, {}, {}
    for label in count:
        prec[label] = float(count[label][0]) / max(count[label][1],1)
        recall[label] = float(count[label][0]) / max(count[label][2],1)
        if prec[label] * recall[label] == 0:
            f1[label] = 0
        else:
            f1[label] = 2*prec[label]*recall[label]/(prec[label]+recall[label])

    return float(n_correct)/float(n_total), prec, recall, f1

def train(dataset):

    print('random seed:', args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.backends.cudnn.deterministic = True
    # torch.backends.cudnn.enabled = False

    cross_res = {label:[] for label in label2id if label != 'O'}

    for cross_valid in range(1):

        # print('cross_valid', cross_valid)

        model = GNN(word_vocab_size=WORD_VOCAB_SIZE, char_vocab_size=CHAR_VOCAB_SIZE, d_output=d_output, args=args)
        model.cuda()
        # print vocab_size

        # print('split dataset')
        # dataset.split_train_valid_test_bycase([0.5, 0.1, 0.4], 5, cross_valid)
        print('train:', len(dataset.train), 'valid:', len(dataset.valid), 'test:', len(dataset.test))
        sys.stdout.flush()

        train_dataloader = DataLoader(dataset.train, batch_size=args.batch, shuffle=True)
        valid_dataloader = DataLoader(dataset.valid, batch_size=args.batch)
        test_dataloader = DataLoader(dataset.test,  batch_size=args.batch)

        weight = torch.zeros(len(label2id))
        for label, idx in label2id.items():
            weight[idx] = 1 if label == 'O' else 2
        loss_function = nn.CrossEntropyLoss(weight.cuda(), reduce=False)
        optimizer = torch.optim.Adam(
            filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, weight_decay=args.wd)
        # scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.8)

        best_acc = -1
        wait = 0
        batch_cnt = 0

        for epoch in range(args.epochs):
            total_loss = 0
            pending_loss = None
            model.train()
            # random.shuffle(dataset.train)
            load_time, forward_time, backward_time = 0, 0, 0
            model.clear_time()

            train_log = open(args.save_path+'_train.log','w')
            for tensors, batch in tqdm(train_dataloader, file=train_log, mininterval=60):
                # print(batch[0].case_id, batch[0].doc_id, batch[0].page_id)
                start = time.time()
                data, data_word, pos, length, mask, label, adjs = to_var(tensors, cuda=args.cuda)
                batch_size, docu_len, sent_len, word_len = data.size()
                load_time += (time.time()-start)

                start = time.time()
                logit = model(data, data_word, pos, length, mask, adjs)
                forward_time += (time.time()-start)

                start = time.time()
                if args.crf:
                    logit = logit.view(batch_size*docu_len, sent_len, -1)
                    mask = mask.view(batch_size*docu_len, -1)
                    length = length.view(batch_size*docu_len)
                    label = label.view(batch_size*docu_len, -1)
                    loss = -model.crf_layer.loglikelihood(logit, mask, length, label)
                    loss = torch.masked_select(loss, torch.gt(length, 0)).mean()
                else:
                    loss = loss_function(logit.view(-1, d_output), label.view(-1))
                    loss = torch.masked_select(loss, mask.view(-1)).mean()
                total_loss += loss.data.sum()
                # print(total_loss, batch[0].case_id, batch[0].doc_id, batch[0].page_id)
                if math.isnan(total_loss):
                    print('Loss is NaN!')
                    exit()

                loss.backward()
                optimizer.step()
                optimizer.zero_grad()
                backward_time += (time.time()-start)

                batch_cnt += 1
                if batch_cnt % 20000 != 0:
                    continue
                # print('load %f   forward %f   backward %f'%(load_time, forward_time, backward_time))
                # model.print_time()
                valid_acc, valid_prec, valid_recall, valid_f1 = evaluate(model, valid_dataloader, args=args)

                print('Epoch %d:  Train Loss: %.3f  Valid Acc: %.5f' % (epoch, total_loss, valid_acc))
                # print(acc_to_str(valid_f1))
                # scheduler.step()

                acc = np.mean(list(valid_f1.values())) # valid_acc
                print(acc)
                if acc >= best_acc:
                    obj = {'args':args, 'model':model.state_dict()}
                    torch.save(obj, args.save_path+'.model')
                    result_obj['valid_prec'] = np.mean(list(valid_prec.values()))
                    result_obj['valid_recall'] = np.mean(list(valid_recall.values()))
                    result_obj['valid_f1'] = np.mean(list(valid_f1.values()))
                wait = 0 if acc > best_acc else wait+1
                best_acc = max(acc, best_acc)

                model.train()
                sys.stdout.flush()
                if wait >= args.patience:
                    break

            train_log.close()
            os.remove(args.save_path+'_train.log')

            if wait >= args.patience:
                break

        obj = torch.load(args.save_path+'.model')
        model.load_state_dict(obj['model'])

        test(test_dataloader, model)

    # print("Cross Validation Result:")
    # for label in cross_res:
    #     cross_res[label] = np.mean(cross_res[label])
    # print(acc_to_str(cross_res))
    return cross_res

def test(test_dataloader, model=None):

    if model is None:
        obj = torch.load(args.save_path+'.model', map_location=lambda storage, loc:storage)
        train_args = obj['args']
        model = GNN(word_vocab_size=WORD_VOCAB_SIZE, char_vocab_size=CHAR_VOCAB_SIZE, d_output=d_output, args=train_args)
        model.load_state_dict(obj['model'])
        model.cuda()
        print('Model loaded.')

    test_acc, test_prec, test_recall, test_f1 = evaluate(model, test_dataloader, output=True, args=args)
    print('######## prec   : ', acc_to_str(test_prec))
    print('######## recall : ', acc_to_str(test_recall))
    print('######## f1     : ', acc_to_str(test_f1))
    prec, recall, f1 = np.mean(list(test_prec.values())), np.mean(list(test_recall.values())), np.mean(list(test_f1.values()))
    print(prec, recall, f1)
    result_obj['test_prec'] = prec
    result_obj['test_recall'] = recall
    result_obj['test_f1'] = f1
    result_obj['test_info'] = '\n'.join([acc_to_str(test_prec), acc_to_str(test_recall), acc_to_str(test_f1)])
    # result_obj['tmp_test_f1'] = mean_test_f1

if __name__ == '__main__':

    dataset = read_data(args.data_path, args.case, args.model)
    print('Data loaded.')
    sys.stdout.flush()

    if not args.test:
        train(dataset)
        # dataset = read_data(args.testbase, args.testcase, args.model)
        # test(dataset)
        for attr, value in sorted(args.__dict__.items()):
            result_obj[attr] = value
        json.dump(result_obj, open(args.save_path+'_results.json','w'))
    else:
        test_dataloader = DataLoader(dataset.data, batch_size=args.batch)
        test(test_dataloader)

# result = {}
# cross_res = train(args.seed)
# for label in cross_res:
#     if label not in result:
#         result[label] = []
#     result[label].append(cross_res[label])

# print("Final Result")
# for label in result:
#     print(label, np.mean(result[label]), np.std(result[label]))


from setuptools import setup, find_packages

with open("requirements.txt") as f:
	install_requires = f.read().strip().split("\n")

# get version from __version__ variable in library_management/__init__.py
from library_management import __version__ as version

setup(
	name="library_management",
	version=version,
	description="An app to manage library memberships",
	author="Frappe",
	author_email="developers@frappe.io",
	packages=find_packages(),
	zip_safe=False,
	include_package_data=True,
	install_requires=install_requires
)


from config import KAFKA_TOPIC, KAKFKA_BOOTSTRAP_SERVER
import json
from kafka import KafkaConsumer

def deserialize(record_val):
    return record_val.decode("utf-8")
    # return json.loads(record.value)

if __name__ == "__main__":

    consumer = KafkaConsumer(
        KAFKA_TOPIC,
        bootstrap_servers=KAKFKA_BOOTSTRAP_SERVER,
        auto_offset_reset="earliest"
    )

    for record in consumer:
        print(deserialize(record.value))


import openai

SYSTEM = "You are a helpful AI assistant that can answer questions provided by the user"
HISTORY = [{"role": "system", "content": SYSTEM}]

while True:
    prompt = input("User: ")

    if prompt == "exit":
        break

    HISTORY.append({"role": "user", "content": prompt})

    chat_completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=HISTORY,
    )

    response = chat_completion.choices[0].message.content

    HISTORY.append({"role": "assistant", "content": response})

    print(f"GPT: {response}")


from flask import Flask, jsonify, request

import urllib
from urllib2 import urlopen

import numpy as np
import caffe

import classify_nsfw

# Pre-load caffe model.
nsfw_net = caffe.Net("nsfw_model/deploy.prototxt",  # pylint: disable=invalid-name
                     "nsfw_model/resnet_50_1by2_nsfw.caffemodel", caffe.TEST)

# Load transformer
# Note that the parameters are hard-coded for best results
caffe_transformer = caffe.io.Transformer({'data': nsfw_net.blobs['data'].data.shape})
caffe_transformer.set_transpose('data', (2, 0, 1))  # move image channels to outermost
caffe_transformer.set_mean('data', np.array([104, 117, 123]))  # subtract the dataset-mean value in each channel
caffe_transformer.set_raw_scale('data', 255)  # rescale from [0, 1] to [0, 255]
caffe_transformer.set_channel_swap('data', (2, 1, 0))  # swap channels from RGB to BGR

app = Flask(__name__)

@app.route('/')
def index():
    return "welcome to nsfw"

@app.route('/ck')
def ck():

    u0 = u = ""

    try:
        u0 = request.args.get('u')
        u = urllib.unquote(u0).decode('utf8')

        handle = urlopen(u)
        image_data = handle.read()

        # Classify.
        scores = classify_nsfw.caffe_preprocess_and_compute(image_data, caffe_transformer=caffe_transformer, caffe_net=nsfw_net, output_layers=['prob'])

        # Scores is the array containing SFW / NSFW image probabilities
        # scores[1] indicates the NSFW probability
        # print "NSFW score:  " , scores[1]

        rt = {
            "code": 0,
            "msg": "",
            "data": {
                "score" : scores[1]
            }
        }
        return jsonify(rt)

    except Exception as e:
        msg = "%s, decode: %s, raw: %s" % (str(e), u, u0)
        rt = {
            "code": 500,
            "msg": msg,
            "data": {}
        }
        return jsonify(rt)


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

# In[1]:

#import
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import keras
from keras import Sequential
from keras.layers import Dense

dataPath = "C:/DEV/hadoop/normalized_data.csv"

dataset = pd.read_csv(dataPath, sep=';', header=0)
dataset = dataset.drop(columns=['TripID'])
dataset['RouteID'] = dataset['RouteID'].apply(lambda row: pd.to_numeric(row.split('_')[1]))
dataset.head(2)
dataset['result'].plot(kind='bar')

# In[3]:

#check the data distribution
sns.pairplot(dataset)

# In[4]:

#creating training set
X=dataset.iloc[:,0:6]
y=dataset.iloc[:,6:7].values

# In[5]:

#normalizing the values
from sklearn.preprocessing import  MinMaxScaler
sc= MinMaxScaler()
x= sc.fit_transform(X)
#y= y.reshape(-1,1)
#y=sc.fit_transform(y)

# In[6]:

#dividing into train and test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

# In[7]:

#creating the model
def build_regressor():
    regressor = Sequential()
    regressor.add(Dense(1, input_dim=6, activation='linear'))
    #regressor.add(Dense(units=10, input_shape=(7,0))
    #regressor.add(Dense(units=1))
    #regressor.add(Dense(units=7))
    optimizer = keras.optimizers.adam(amsgrad = True)
    regressor.compile(optimizer= optimizer, loss='mse',  metrics=['mae','accuracy'])
    return regressor

from keras.wrappers.scikit_learn import KerasRegressor
regressor = KerasRegressor(build_fn=build_regressor, batch_size=2,epochs=100)
print(y_train.shape)
print(X_train.shape)

results=regressor.fit(X_train,y_train)

y_pred= regressor.predict(X_test)

fig, ax = plt.subplots()
ax.scatter(y_test, y_pred)
ax.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()


#
# @lc app=leetcode.cn id=222 lang=python3
#
# [222] 完全二叉树的节点个数
#
# https://leetcode.cn/problems/count-complete-tree-nodes/description/
#
# algorithms
# Medium (80.22%)
# Likes:    771
# Dislikes: 0
# Total Accepted:    218.1K
# Total Submissions: 271.6K
# Testcase Example:  '[1,2,3,4,5,6]'
#
# 给你一棵 完全二叉树 的根节点 root ，求出该树的节点个数。
#
# 完全二叉树
# 的定义如下：在完全二叉树中，除了最底层节点可能没填满外，其余每层节点数都达到最大值，并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h
# 层，则该层包含 1~ 2^h 个节点。
#
#
#
# 示例 1：
#
#
# 输入：root = [1,2,3,4,5,6]
# 输出：6
#
#
# 示例 2：
#
#
# 输入：root = []
# 输出：0
#
#
# 示例 3：
#
#
# 输入：root = [1]
# 输出：1
#
#
#
#
# 提示：
#
#
# 树中节点的数目范围是[0, 5 * 10^4]
# 0
# 题目数据保证输入的树是 完全二叉树
#
#
#
#
# 进阶：遍历树来统计节点是一种时间复杂度为 O(n) 的简单解决方案。你可以设计一个更快的算法吗？
#
#

# Definition for a binary tree node.
from turtle import left
from typing import Optional

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

# @lc code=start
class Solution:
    def get_half_tree_node_count(self, max_level: int) -> int:
        lvl = 1
        s = 1
        while lvl <= max_level:
            s += 2 ** (lvl - 1)
            lvl += 1
        return s

    def countNodes(self, root: Optional[TreeNode]) -> int:
        # 二分可以快速获取一半的数量
        if not root:
            return 0

        has_children = root.left or root.right
        if not has_children:
            return 1

        cur = root
        tree_height = 0
        while cur.left:
            cur = cur.left
            tree_height += 1

        cur = root.right
        right_tree_height = 0
        while cur and cur.left:
            cur = cur.left
            right_tree_height += 1

        left_tree_full = tree_height == right_tree_height + 1
        if left_tree_full:
            return self.get_half_tree_node_count(tree_height) + self.countNodes(root.right)
        else:
            return self.get_half_tree_node_count(tree_height - 1) + self.countNodes(root.left)

# @lc code=end

Q222_完全二叉树的节点个数.py


from cloudinary.models import CloudinaryField
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.html import mark_safe

class Amigurumi(models.Model):
    name = models.CharField(
        max_length=50, help_text="Enter name of the amigurumi", blank=False
    )
    authorship = models.BooleanField(
        default=False, help_text="If you're the author of the recipe"
    )
    url = models.CharField(
        max_length=100, help_text="Enter url of the amigurumi", blank=True
    )

    class Meta:
        ordering = ["name", "authorship"]

    def get_absolute_url(self):
        return reverse("amigurumis:amigurumi-detail", args=[str(self.id)])

    def __str__(self):
        return self.name

class AmigurumiImage(models.Model):
    amigurumi = models.ForeignKey(
        Amigurumi, related_name="images", on_delete=models.CASCADE
    )
    image = CloudinaryField("image")

class AboutInfo(models.Model):
    photo = CloudinaryField("photo")
    description = models.TextField(
        default="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut pellentesque nulla. Donec quis nulla at libero laoreet tempor. Ut egestas nulla diam, sit amet varius tellus iaculis sit amet. Aenean porta, mauris quis pellentesque lobortis, elit nisl pulvinar nunc, eu pellentesque leo augue non ante. Vivamus faucibus aliquet neque, vel viverra diam pulvinar vel. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Suspendisse placerat ultricies nisi id malesuada.",
        help_text="Enter the description of your about page",
    )

    def save(self, *args, **kwargs):
        if not self.pk and AboutInfo.objects.exists():
            raise ValidationError("There can be only one About Info instance")
        return super(AboutInfo, self).save(*args, **kwargs)

    def admin_photo(self):
        return '<img src="%s"/>' % self.photo

    admin_photo.allow_tags = True


import pkgutil
import json

altair_installed = True
try:
    import altair as alt
except ImportError:
    altair_installed = False

def load_chart_definition(filename):
    path = f"files/chart_defs/{filename}"
    data = pkgutil.get_data(__name__, path)
    schema = json.loads(data)
    return schema

def _load_multi_chart_template():
    path = "files/templates/multi_chart_template.txt"
    template = pkgutil.get_data(__name__, path).decode("utf-8")
    return template

def _load_external_libs():

    to_load = {
        "vega-embed": "files/external_js/vega-embed@6.14.2",
        "vega-lite": "files/external_js/vega-lite@4.17.0",
        "vega": "files/external_js/vega@5.17.1",
    }

    loaded = {}
    for k, v in to_load.items():
        script = pkgutil.get_data(__name__, v).decode("utf-8")
        loaded[k] = script

    return loaded

def altair_if_installed_else_json(chart_dict):
    if altair_installed:
        return alt.Chart.from_dict(chart_dict)
    else:
        return chart_dict

def _make_json(chart_or_dict):
    if altair_installed:
        return chart_or_dict.to_json(indent=None)
    else:
        return json.dumps(chart_or_dict)


import os
import boto3
from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection

INGESTION_RECORD_LIMIT = 1000

DEFAULT_ELASTICSEARCH_HOST = 'vpc-{{REDACTED}}.es.amazonaws.com'
DEFAULT_ELASTICSEARCH_INDEX = 'emma-federated-index'

EMMA_ELASTICSEARCH_HOST = os.environ.get('EMMA_ELASTICSEARCH_HOST', DEFAULT_ELASTICSEARCH_HOST)
EMMA_ELASTICSEARCH_INDEX = os.environ.get('EMMA_ELASTICSEARCH_INDEX', DEFAULT_ELASTICSEARCH_INDEX)
EMMA_ELASTICSEARCH_REGION = 'us-east-1'
EMMA_ELASTICSEARCH_SERVICE = 'es'

GOLDEN_KEY = os.environ.get('GOLDEN_KEY', 'unset')

credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key,
                   EMMA_ELASTICSEARCH_REGION, EMMA_ELASTICSEARCH_SERVICE, session_token=credentials.token)

ELASTICSEARCH_CONN = Elasticsearch(
    hosts=[{'host': EMMA_ELASTICSEARCH_HOST, 'port': 443}],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection
)

RENAMED_FIELDS = {
    'emma_lastRemediationNote':'rem_comments',
    'emma_lastRemediationDate': 'rem_remediationDate',
    'emma_repositoryMetadataUpdateDate': 'emma_repositoryUpdateDate'
}

ORIGINAL_FIELDS = {
    'rem_comments':'emma_lastRemediationNote',
    'rem_remediationDate': 'emma_lastRemediationDate',
    'emma_repositoryUpdateDate': 'emma_repositoryMetadataUpdateDate'
}


from pyspark.sql import SparkSession
import argparse

def main(params):
    spark = SparkSession.builder.getOrCreate()
    current_timestamp = params.date
    print(current_timestamp)
    print(spark.sparkContext)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='argument for spark jobs')

    parser.add_argument('--date', default='aaa ')
    args = parser.parse_args()
    main(args)


import os
from skimage import io
import numpy as np

# CHECK NULL IMAGES IN SUB-FOLDERS
def checkRemoveNull(dir, recFile):
	f = open(recFile, 'wr+')
	symbols = [sym for sym in os.listdir(dir) if not sym.startswith('.')]
	for sym in symbols:
		f.write(sym + ':\n')
		symPath = os.path.join(dir, sym)
		imgs = [img for img in os.listdir(symPath) if img.endswith('.png')]
		for img in imgs:
			imgPath = os.path.join(symPath, img)
			imgData = io.imread(imgPath)
			if np.amin(imgData) == np.amax(imgData):
				f.write(imgPath + '\n')
				os.remove(imgPath)
	f.close()

directory = '../TestIMAGE_NORM'
recFile = 'emptyFiles_norm_test.txt'
checkRemoveNull(directory, recFile)


from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QFileDialog, \
    QLabel, QShortcut, QFrame, QCheckBox
from PyQt5.QtGui import QColor, QKeySequence, QCursor, QScreen
from PyQt5.QtCore import QTimer, Qt, pyqtSlot
from sys import exit, argv
from FlickerTable import FlickerTable, Flicker, FreqType, SequenceBlock
from Flicker import SeqType, SeqCondition
from ScreenViewer import ScreenViewer
from SequenceBuilder import SequenceBuilder
from os import system, path
from Settings import Settings
import xml.etree.cElementTree as ET
import psutil
from xml.dom import minidom
from multiprocessing import Process
import platform

Standard_Save_File = "Flickers.xml"

class MainApp(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Flickers On Top")
        # self.setGeometry(100, 100, 500, 500)

        # create main widget
        central_widget = QWidget(self)
        # central_widget.setStyleSheet("border: 1px solid black")
        self.setCentralWidget(central_widget)

        # Main Data
        self.Flickers = []
        self.backgroundFlicker = None

        #self.Load()
        self.MainLayout = QVBoxLayout()
        self.process = None

        # graphical init
        self.InitFlickerTable()
        self.InitLowerPart()
        self.MainLayout.addStretch(0)

    def InitFlickerTable(self):

        ButtonLayout = QHBoxLayout()

        # Flicker table containing all flicker
        self.Table = FlickerTable(self.Flickers)
        QShortcut(QKeySequence("Ctrl+C"), self, self.Table._copy)
        QShortcut(QKeySequence("Ctrl+V"), self, self.Table._paste)

        self.MainLayout.addWidget(self.Table)

        buttonAdd = QPushButton("Add")

        def add():
            f = Flicker()
            self.Flickers.append(f)
            self.Table.AddNewFlicker(f)

        buttonAdd.clicked.connect(lambda: add())
        buttonAdd.setShortcut("Ctrl+N")
        buttonRemove = QPushButton("Remove")

        def remove():
            if len(self.Flickers) > 0:
                if self.Table.selected:
                    self.Table.RemoveRow(*self.Table.selected)
                else:
                    self.Table.RemoveRow(self.Table.Rows[list(self.Table.Rows.keys())[-1]])

        buttonRemove.clicked.connect(lambda: remove())
        buttonRemove.setShortcut(Qt.Key_Delete)

        # Black Screen checkbox
        self.checkboxblack = QCheckBox(
            "Add a Black Screen background? (WARNING: flickers are click-through even with a background)")

        ButtonLayout.addWidget(buttonAdd)
        ButtonLayout.addWidget(buttonRemove)
        ButtonLayout.addWidget(self.checkboxblack)
        ButtonLayout.addStretch(0)

        self.MainLayout.addLayout(ButtonLayout)

        self.centralWidget().setLayout(self.MainLayout)

        # create setting window
        self.buttonSequence=QPushButton()
        self.setting = Settings()
        self.setting.updateSettingSignal.connect(self._apply_setting)
        self._apply_setting()

    def InitLowerPart(self):
        lowerContainer = QFrame()
        lowerLayout = QHBoxLayout()

        lowerContainer.setFrameStyle(QFrame.Panel | QFrame.Sunken)
        # button on the left
        buttonContainer = QFrame()
        # buttonContainer.setFrameStyle(QFrame.StyledPanel)

        buttonLayout = QVBoxLayout()
        actionLayout = QHBoxLayout()
        buttonTest = QPushButton("Test")
        buttonRun = QPushButton("Run")
        buttonStop = QPushButton("Stop")
        buttonTest.clicked.connect(lambda b: self.testVisual())
        buttonRun.clicked.connect(lambda b: self.LaunchVisualStimuli())
        buttonStop.clicked.connect(lambda b: self.stopVisual())
        actionLayout.addWidget(buttonTest)
        actionLayout.addWidget(buttonRun)
        actionLayout.addWidget(buttonStop)
        buttonLayout.addLayout(actionLayout)
        buttonSave = QPushButton("Save")
        buttonSave.clicked.connect(lambda a: self.Save())
        buttonSave.setShortcut("Ctrl+S")
        buttonSaveAs = QPushButton("Save As...")
        buttonSaveAs.clicked.connect(lambda b: self.SaveAs())
        buttonSaveAs.setShortcut("Ctrl+Shift+S")
        buttonImport = QPushButton("Import")
        buttonImport.clicked.connect(lambda b: self.Import())
        buttonHelp = QPushButton("Help")
        buttonHelp.clicked.connect(lambda b: self.help())
        buttonSettings = QPushButton("Settings")
        buttonSettings.clicked.connect(self.settings)
        self.buttonSequence=QPushButton("Sequence")
        def seqbuildopen():
            if len(self.Flickers)>0:
                self.build=SequenceBuilder(self.Flickers[0])
                self.build.show()
        self.buttonSequence.clicked.connect(lambda b:seqbuildopen())

        if self.setting.seqlinkCheckBox.checkState():
            for row in self.Table.Rows.values():
                row.attrDict["sequence"].hide()
        else:
            self.buttonSequence.hide()
        buttonLayout.addWidget(self.buttonSequence)
        buttonLayout.addWidget(buttonSave)
        buttonLayout.addWidget(buttonSaveAs)
        buttonLayout.addWidget(buttonImport)
        buttonLayout.addWidget(buttonHelp)
        buttonLayout.addWidget(buttonSettings)
        buttonLayout.addStretch(0)
        buttonContainer.setLayout(buttonLayout)
        lowerLayout.addWidget(buttonContainer)

        # Screen Viewer
        self.screenviewer = ScreenViewer(self.Flickers)
        self.screenviewer.changeSignal.connect(self.Table.updateFlicker)
        self.screenviewer.removeSignal.connect(self.Table.removeFlicker)
        self.screenviewer.addSignal.connect(self.Table.updateFlicker)
        self.screenviewer.addSignal.connect(lambda f: self.Flickers.append(f))
        self.Table.tableUpdateSignal.connect(lambda flicker: self.screenviewer.updateFlickers(flicker))
        self.Table.tableRemoveSignal.connect(self.screenviewer.removeFlicker)
        self.Table.tableAddSignal.connect(lambda flicker: self.screenviewer.setupFlickers(flicker))
        lowerLayout.addWidget(self.screenviewer.view)

        # mousePosition
        mousePosLayout = QVBoxLayout()
        titleLabel = QLabel("Mouse Position")
        xlabel = QLabel()
        ylabel = QLabel()
        mousePosLayout.addWidget(titleLabel)
        mousePosLayout.addWidget(xlabel)
        mousePosLayout.addWidget(ylabel)
        mousePosLayout.addStretch(0)
        lowerLayout.addLayout(mousePosLayout)

        def get_mouse_pos():
            pos = QCursor.pos()
            xlabel.setText("X: " + str(pos.x()))
            ylabel.setText("Y: " + str(pos.y()))

        mouseTimer = QTimer(self)
        mouseTimer.timeout.connect(get_mouse_pos)
        mouseTimer.start(20)

        lowerContainer.setLayout(lowerLayout)
        self.MainLayout.addWidget(lowerContainer)

    def Load(self, file=Standard_Save_File):
        def loadseq(attribute):
            value = SequenceBlock(SeqType[attribute[0].text], SeqCondition[attribute[2].text], float(attribute[3].text))
            for subseq in attribute[1]:
                value.AddSeq(loadseq(subseq))
            return value

        if path.exists(file):
            Tree = ET.parse(file)
            root = Tree.getroot()
            for f in root:
                try:
                    temp = Flicker()
                    for attribute in f:

                        # Basic Attribute
                        if (attribute.tag in temp.__dict__.keys()):
                            value = None
                            if attribute.text != None and len(
                                    attribute) == 0 and attribute.text != "false" and attribute.text != "true":
                                value = attribute.text
                                if "." in value:
                                    try:
                                        value = float(value)
                                    except:
                                        pass
                                else:
                                    try:
                                        value = int(value)
                                    except:
                                        pass
                                if attribute.tag == "Type":
                                    value = FreqType[value]
                            if attribute.tag == "Color":
                                value = QColor(int(attribute[1].text), int(attribute[2].text), int(attribute[3].text))
                            if attribute.tag == "sequence":
                                value = loadseq(attribute)
                            if attribute.text == "false":
                                value = False
                            if attribute.text == "true":
                                value = True

                            temp.__setattr__(attribute.tag, value)
                        # Special attribute
                        if attribute.tag == "Code":
                            temp.Code = attribute.text
                    self.Flickers.append(temp)
                except:
                    print("Couldn't load flicker")

    def Save(self, file=Standard_Save_File):
        def saveSeq(root, seq: SequenceBlock):
            t = ET.SubElement(root, "Type")
            t.text = seq.seqType.name
            subseq = ET.SubElement(root, "contained_sequence")
            c = ET.SubElement(root, "cond")
            c.text = seq.Condition.name
            v = ET.SubElement(root, "value")
            v.text = str(seq.value)
            for s in seq.contained_sequence:
                saveSeq(ET.SubElement(subseq, "SequenceValue"), s)

        root = ET.Element("ArrayOfFlicker")
        for f in self.Flickers:
            fEle = ET.SubElement(root, "Flicker")
            for attr in f.__dict__:
                value = f.__dict__[attr]

                ele = ET.SubElement(fEle, attr)
                if value == None or isinstance(value, SequenceBuilder):
                    continue
                if isinstance(value, FreqType):
                    value = value.name
                if isinstance(value, QColor):
                    A = ET.SubElement(ele, "A")
                    A.text = str(value.alpha())
                    R = ET.SubElement(ele, "R")
                    R.text = str(value.red())
                    G = ET.SubElement(ele, "G")
                    G.text = str(value.green())
                    B = ET.SubElement(ele, "B")
                    B.text = str(value.blue())
                    continue
                if isinstance(value, SequenceBlock):
                    saveSeq(ele, value)
                    continue
                if isinstance(value, bool):
                    value = str(value).lower()
                ele.text = str(value)
        tree = ET.ElementTree(root)
        xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent="  ")
        with open(file, "w") as f:
            f.write(xmlstr)

    def SaveAs(self):
        file = QFileDialog.getSaveFileName(filter="*.xml")[0]
        if path.exists(file): self.Save(file)

    def Import(self):
        file = QFileDialog.getOpenFileName(filter="*.xml")[0]
        if path.exists(file):
            self.Table.RemoveRow(*list(self.Table.Rows.values()))

            def loadseq(attribute):
                value = SequenceBlock(SeqType[attribute[0].text], SeqCondition[attribute[2].text],
                                      float(attribute[3].text))
                for subseq in attribute[1]:
                    value.AddSeq(loadseq(subseq))
                return value

            if path.exists(file):
                Tree = ET.parse(file)
                root = Tree.getroot()
                for f in root:
                    try:
                        temp = Flicker()
                        for attribute in f:
                            # Basic Attribute
                            if (attribute.tag in temp.__dict__.keys()):
                                value = None
                                if attribute.text != None and len(
                                        attribute) == 0 and attribute.text != "false" and attribute.text != "true":
                                    value = attribute.text
                                    if "." in value:
                                        try:
                                            value = float(value)
                                        except:
                                            pass
                                    else:
                                        try:
                                            value = int(value)
                                        except:
                                            pass
                                    if attribute.tag == "Type":
                                        value = FreqType[value]
                                if attribute.tag == "Color":
                                    value = QColor(int(attribute[1].text), int(attribute[2].text),
                                                   int(attribute[3].text))
                                if attribute.tag == "sequence":
                                    value = loadseq(attribute)
                                if attribute.text == "false":
                                    value = False
                                if attribute.text == "true":
                                    value = True

                                temp.__setattr__(attribute.tag, value)
                            # Special attribute
                            if attribute.tag == "Code":
                                temp.Code = attribute.text
                        self.Flickers.append(temp)
                    except:
                        print("Couldn't load flicker")
            self.Table.InitRows()

    def LaunchVisualStimuli(self):
        # Launch the main code
        if len(self.Flickers) > 0:
            if self.checkboxblack.checkState() and not self.backgroundFlicker in self.Flickers:
                screen = app.primaryScreen()
                size = screen.size()
                self.backgroundFlicker = Flicker(width=size.width(), height=size.height(), frequency=1, opacity_min=100,
                                                 color=QColor(0, 0, 0))
                self.Flickers.insert(0, self.backgroundFlicker)
            self.Save()
            if "Windows" in platform.system():
                func_arg = ("VisualStimuli.exe",)
            else:
                func_arg = ("./VisualStimuli.exe",)
            self.process = Process(target=system, args=func_arg)
            self.process.start()

    def testVisual(self):
        t = QTimer(self)
        t.setInterval(10000)
        t.setSingleShot(True)
        t.timeout.connect(self.stopVisual)
        t.start()
        self.LaunchVisualStimuli()

    def stopVisual(self):
        if self.process:
            if self.process.is_alive():
                for proc in psutil.process_iter():
                    # check whether the process name matches
                    if proc.name() == "VisualStimuli.exe":
                        proc.kill()
                self.process.kill()
                if self.backgroundFlicker in self.Flickers:
                    self.Flickers.remove(self.backgroundFlicker)

    def help(self):
        self.helpLabel = QLabel("I.\n"
                                + "  X (Horizontal), Y (Vertical) correspond to the position of top-left point of a Flicker (in pixel).\n"
                                + "II.\n"
                                + " Width, Height is the size of a Flicker (in pixel).\n"
                                + "III.\n"
                                + " Frequency in Hz and Phase in degrees.\n"
                                + "IV.\n"
                                + " You can choose in Type\n"
                                + " Random \n"
                                + " Sinous \n"
                                + " Square \n"
                                + " Root Square\n"
                                + " Maximum lenght sequence\n"
                                + "V.\n"
                                + " You can click on Add to create a new Flicker \n"
                                + "VI.\n"
                                + " Finally, click on RUN to run the Flicker program or TEST for a 10 seconds test.\n"
                                + " press 'Echap' to close all flickers. Or alternatively, click on 'Stop'\n"
                                + "Note: Please be sure that your screen can support the frequency you want for your flickers\n"
                                + " as a reminder the screen frequency must always be at least 2 times higher than the flicker's frequency\n"
                                + "THANK FOR READING !!!")
        self.helpLabel.show()

    def closeEvent(self, a0) -> None:
        if self.process:
            if self.process.is_alive():
                self.process.kill()
        if self.backgroundFlicker in self.Flickers:
            self.Flickers.remove(self.backgroundFlicker)
            self.Save()
        QApplication.quit()
        return super().closeEvent(a0)

    def settings(self):
        self.setting.show()

    @pyqtSlot()
    def _apply_setting(self):
        if self.setting:
            if self.setting.seqlinkCheckBox.checkState():
                if len(self.Flickers) > 0:
                    for f in self.Flickers[1:]:
                        f.sequence = self.Flickers[0].sequence
                    for row in self.Table.Rows.values():
                        row.attrDict["sequence"].hide()
                    self.buttonSequence.show()
            else:
                self.buttonSequence.hide()
                for row in self.Table.Rows.values():
                    row.attrDict["sequence"].show()
                for f in self.Flickers[1:]:
                    if f.sequence == self.Flickers[0].sequence:
                        f.sequence = SequenceBlock(SequenceBlock(seq_type=SeqType.Active))
        self.setting.save()

if __name__ == "__main__":
    app = QApplication(argv)
    window = MainApp()
    window.show()
    exit(app.exec_())


import itertools
import random
from typing import Optional
from utils import make_tree_like_acceptor, hopkroft_minimize_acceptor
from q_learn_small_states import is_finish
from acceptor import Acceptor
import pickle
import numpy as np
from itertools import product

def count_synthesis(allowed_words: list, banned_words: list, alphabet: set,
                    acceptor: Optional[Acceptor] = None) -> None:
  if not acceptor:
      acceptor = make_tree_like_acceptor(allowed_words, alphabet)
      acceptor = hopkroft_minimize_acceptor(acceptor)
  errors, succ = 0, 0
  print(len(acceptor.states))
  changed = True
  while changed:
    changed = False
    states = list(acceptor.states.keys())
    for state, letter in itertools.product(list(filter(lambda x: x != 'trash', states)), alphabet):
      if acceptor.get_new_state(state, letter) == 'trash':
        new_state = random.choice(list(filter(lambda x: x != 'trash' and x != state, states)))
        acceptor.set_transition(state, letter, new_state)
        new_acceptor = hopkroft_minimize_acceptor(acceptor)
        if any(map(lambda word: new_acceptor.accept_word(word), banned_words)):
          acceptor.set_transition(state, letter, 'trash')
          errors += 1
        else:
          acceptor = new_acceptor
          succ += 1
          changed = True
          break
  print(errors, succ, len(acceptor.states))

def count_learned(table, states_dict, states_actions_dict,
                  acceptable_words, banned_words, alphabet):
    acceptor = make_tree_like_acceptor(acceptable_words, alphabet)
    acceptor = hopkroft_minimize_acceptor(acceptor)
    errors, succ = 0, 0
    done = False
    while not done:
        cnt = 0
        state_letter_prod = list(
            product(list(filter(lambda x: x != 'trash', list(acceptor.states.keys()))), alphabet))
        cnt = 0
        max_state_name_len = max(map(len, acceptor.states.keys()))
        state_array = np.array(['a' * (max_state_name_len * 3 + 2) for _ in state_letter_prod])
        for state_name, letter in state_letter_prod:
            state_array[cnt] = state_name + '_' + letter + '_' + acceptor.get_new_state(state_name, letter)
            cnt += 1
        state_array.sort()
        state_array = tuple(state_array)
        if state_array not in states_dict:
            errors += 1
            print(errors, succ, len(acceptor.states))
            print('reached wrong state')
            count_synthesis(acceptable_words, banned_words, alphabet, acceptor)
            break
        state_num = states_dict[state_array]
        action = np.argmax(table[state_num])
        state, new_state, letter = states_actions_dict[state_num][action]
        acceptor.set_transition(state, letter, new_state)
        new_acceptor = hopkroft_minimize_acceptor(acceptor)
        if any(map(lambda word: new_acceptor.accept_word(word), banned_words)):
            print('wrong')
            acceptor.set_transition(state, letter, 'trash')
            errors += 1
            states = list(acceptor.states.keys())
            for state, letter in itertools.product(list(filter(lambda x: x != 'trash', states)), alphabet):
                if acceptor.get_new_state(state, letter) == 'trash':
                    new_state = random.choice(list(filter(lambda x: x != 'trash' and x != state, states)))
                    acceptor.set_transition(state, letter, new_state)
                    new_acceptor = hopkroft_minimize_acceptor(acceptor)
                    if any(map(lambda word: new_acceptor.accept_word(word), banned_words)):
                        acceptor.set_transition(state, letter, 'trash')
                        errors += 1
                    else:
                        acceptor = new_acceptor
                        succ += 1
                        break
        else:
            acceptor = new_acceptor
            succ += 1

        done = is_finish(acceptor, banned_words)
    print(errors, succ, len(acceptor.states))

if __name__ == '__main__':
  with open('data.pickle', 'rb') as f:
    table, states_dict, states_actions_dict = pickle.load(f)

  count_synthesis(
      # ['cbacb', 'abcbab', 'babcbabc'],
      # ['bcbacbacb', 'ba'],
      # ['acb', 'abac', 'cb', 'bb'],
      # ['bcabcba', 'bcbcabca'],
       ['bac', 'cacc', 'cab'],

      # ['abcbca', 'bcabc', 'b'],
      # ['a', 'b', 'c'],
      # ['aa', 'bc'],
      # ['bcab', 'acab', 'bac'],
       ['aaaa', 'ccc'],
      {'a', 'b', 'c'}
  )

  count_learned(table, states_dict, states_actions_dict,
      # ['cbacb', 'abcbab', 'babcbabc'],
      # ['bcbacbacb', 'ba'],
      # ['acb', 'abac', 'cb', 'bb'],
      # ['bcabcba', 'bcbcabca'],
       ['bac', 'cacc', 'cab'],

      # ['abcbca', 'bcabc', 'b'],
      # ['a', 'b', 'c'],
      # ['aa', 'bc'],
      # ['bcab', 'acab', 'bac'],
       ['aaaa', 'ccc'],
      {'a', 'b', 'c'}
  )


from collections import defaultdict
n, x = map(int, input().split())
l = input().split()
t = defaultdict(int)
for _ in range(n):
    t[str(x)] += 1
    if t[str(x)] > 1:
        break
    x = int(l[x-1])
print(len(t))