# -*- coding: utf-8 -*-
import logging
import pickle
from api import apis
from datetime import datetime
from flask import Flask, render_template
from my.util import hatena
from my.db import datastore
from google.appengine.api import memcache

app = Flask(__name__)
app.register_blueprint(apis)
app.config['DEBUG'] = True

logging.getLogger().setLevel(logging.DEBUG)

# 1ページあたりに表示する記事件数
ENTRIES_PER_PAGE = 10

@app.route('/')
@app.route('/<page>')
def home(page=None):
    pageNum = 0 if page == None else int(page)
    memcache_key = 'public_entries_' + str(pageNum);
    entries = memcache.get(memcache_key)
    if entries is None:
        entries = pickle.dumps(datastore.get_public_entries(limit=ENTRIES_PER_PAGE, offset=(ENTRIES_PER_PAGE * pageNum)))
        memcache.add(memcache_key, entries, 60 * 60 * 24)
    entries = pickle.loads(str(entries))
    return render_template('home.html', entries=entries, sidebar_entries=__sidebar_entry(), page=pageNum)

@app.route('/popular')
def popular():
    memcache_key = 'get_popular_entries';
    entries = memcache.get(memcache_key)
    if entries is None:
        entries = pickle.dumps(datastore.get_popular_entries(limit=ENTRIES_PER_PAGE))
        memcache.add(memcache_key, entries, 60 * 60 * 24)
    entries = pickle.loads(str(entries))
    return render_template('home.html', entries=entries, sidebar_entries=__sidebar_entry(), page=0)

def __sidebar_entry():
    """サイドバー表示を管理"""
    memcache_key = 'get_public_entries';
    newer = memcache.get(memcache_key)
    if newer is None:
        newer = pickle.dumps(datastore.get_public_entries())
        memcache.add(memcache_key, newer, 60 * 60 * 24)
    newer = pickle.loads(str(newer))
    memcache_key = 'get_popular_entries';
    popular = memcache.get(memcache_key)
    if popular is None:
        popular = pickle.dumps(datastore.get_popular_entries())
        memcache.add(memcache_key, popular, 60 * 60 * 24)
    popular = pickle.loads(str(popular))
    return {'newer': newer[:6], 'popular': popular[:6]}

@app.route('/entry/<eid>')
def entry(eid):
    """エントリーページを表示する"""
    entry = datastore.get_public_entry(int(eid))
    comments = []
    score = 0
    for comment in entry.bookmarkcomment_set:
        comments.append({
          'data': comment,
          'score': __style_from_score(hatena.get_star_score(comment)),
        })
    return render_template('entry.html', entry=entry, comments=comments, sidebar_entries=__sidebar_entry())

def __style_from_score(score):
    """スコアを計算して、0-9の値を返す"""
    score_table = [70, 60, 50, 40, 35, 20, 10, 5, 1]
    for i, value in enumerate(score_table):
        if score > value:
            return len(score_table) - i
    return 0

@app.route('/category/<category>')
@app.route('/category/<category>/<page>')
def category(category, page=None):
    """エントリーページを表示する"""
    pageNum = 0 if page == None else int(page)
    memcache_key = 'public_entries_in_category_' + category + '_' + str(pageNum);
    entries = memcache.get(memcache_key)
    if entries is None:
        entries = pickle.dumps(datastore.get_public_entries_in_category(category, limit=ENTRIES_PER_PAGE, offset=(ENTRIES_PER_PAGE * pageNum)))
        memcache.add(memcache_key, entries, 60 * 60 * 24)
    entries = pickle.loads(str(entries))
    return render_template('home.html', entries=entries, sidebar_entries=__sidebar_entry(), page=pageNum)

@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, nothing at this URL.', 404


#!/usr/bin/python3

import serial
import signal
import sys

keepReading = True

def signal_handler(sig, frame):
    global keepReading
    keepReading = False

signal.signal(signal.SIGINT, signal_handler)
serialport = serial.Serial('/dev/ttyACM0',9600)

min_vals = [0,0,0,0,0,0]
max_vals = [0,0,0,0,0,0]

print("Reading values, stop by pressing ctrl-c")

while keepReading:
    read_serial = serialport.readline().decode('ascii').strip()
    vals = read_serial.split("\t")

    for i in range(len(vals)):
        if float(vals[i]) < min_vals[i]:
            min_vals[i] = float(vals[i])
        if float(vals[i]) > max_vals[i]:
            max_vals[i] = float(vals[i])

print()
print("Minimum gyroscope values: {}, {}, {}".format(min_vals[0], min_vals[1], min_vals[2]))
print("Maximum gyroscope values: {}, {}, {}".format(max_vals[0], max_vals[1], max_vals[2]))
print("Minimum acceleration values: {}, {}, {}".format(min_vals[3], min_vals[4], min_vals[5]))
print("Maxmium acceleration values: {}, {}, {}".format(max_vals[3], max_vals[4], max_vals[5]))


import sys
sys.path.append("/home/cloud/projects/bin/nemo-gitannex/")

from gi.repository import Nemo, GObject
import gitannex_utils as utils
import urllib
import os.path

class GitAnnexMenuProvider(GObject.GObject, Nemo.MenuProvider):
    def __init__(self):
        pass
    def menu_get_cb(self, menu, path_list):
        for path in path_list:
            utils.getPath(path)
    def menu_lock_cb(self, menu, path_list):
        for path in path_list:
            utils.lockPath(path)
    def menu_unlock_cb(self, menu, path_list):
        for path in path_list:
            utils.unlockPath(path)
    def menu_drop_cb(self, menu, path_list):
        for path in path_list:
            utils.dropPath(path)
    def menu_sync_cb(self, menu, path):
        utils.sync(path)

    def get_file_items(self, window, files):
        files = [file for file in files if file.get_uri_scheme() == 'file']
        if len(files) < 1:
            return
        file_paths = []
        for file in files:
            file_paths.append(urllib.unquote(file.get_uri()[7:]))

        file = files[0]
        file_path = file_paths[0]
        dir_path = os.path.dirname(file_path)

        if not utils.isGitAnnex(dir_path):
            return

        submenulist = []
        lock_menuitem = Nemo.MenuItem(name='GitAnnexMenuProvider::lock', label='lock', tip='',icon='')
        unlock_menuitem = Nemo.MenuItem(name='GitAnnexMenuProvider::unlock', label='unlock', tip='',icon='')
        get_menuitem = Nemo.MenuItem(name='GitAnnexMenuProvider::get', label='get', tip='',icon='')
        drop_menuitem = Nemo.MenuItem(name='GitAnnexMenuProvider::drop', label='drop', tip='',icon='')
        lock_menuitem.connect('activate', self.menu_lock_cb, file_paths)
        unlock_menuitem.connect('activate', self.menu_unlock_cb, file_paths)
        get_menuitem.connect('activate', self.menu_get_cb, file_paths)
        drop_menuitem.connect('activate', self.menu_drop_cb, file_paths)
        if file.is_directory() or len(files) > 1:
            submenulist.append(get_menuitem)
            submenulist.append(lock_menuitem)
            submenulist.append(unlock_menuitem)
            submenulist.append(drop_menuitem)
        else:
            if utils.isFileLocalAvailable(file_path):
                if utils.isFileModifyable(file_path):
                    submenulist.append(lock_menuitem)
                else:
                    submenulist.append(unlock_menuitem)
                    submenulist.append(drop_menuitem)
            else:
                submenulist.append(get_menuitem)

        top_menuitem = Nemo.MenuItem(name='GitAnnexMenuProvider::GitAnnex',
                                         label='GitAnnex',
                                         tip='',
                                         icon='')
        submenu = Nemo.Menu()
        top_menuitem.set_submenu(submenu)

        for sub_menuitem in submenulist:
          submenu.append_item(sub_menuitem)

        return top_menuitem,

    def get_background_items(self, window, file):

        if file.get_uri_scheme() != 'file':
            return
        dir_path = urllib.unquote(file.get_uri()[7:])
        if not file.is_directory():
            dir_path = os.path.dirname(dir_path)
        if not utils.isGitAnnex(dir_path):
            return

        sync_menuitem = Nemo.MenuItem(name='GitAnnexMenuProvider::sync', label='sync', tip='', icon='')
        sync_menuitem.connect('activate', self.menu_sync_cb, dir_path)

        submenu = Nemo.Menu()
        submenu.append_item(sync_menuitem)
        menuitem = Nemo.MenuItem(name='GitAnnexMenuProvider::GitAnnex',
                                         label='GitAnnex',
                                         tip='',
                                         icon='')
        menuitem.set_submenu(submenu)

        return menuitem,


from attrdict import AttrDict
from flask import jsonify, request
from voluptuous import Schema, REMOVE_EXTRA

from blockcerts.const import ISSUER_SCHEMA, TEMPLATE_SCHEMA, RECIPIENT_SCHEMA, JOB_SCHEMA
from blockcerts.misc import issue_certificate_batch, get_tx_receipt, verify_cert
from flaskapp.config import get_config

def setup_routes(app):
    @app.route('/ping')
    def ping_route():
        return jsonify({'reply': 'pong'})

    @app.route('/issue', methods=['POST'])
    def issue_certs():
        issuing_job_schema = Schema(
            {
                'issuer': ISSUER_SCHEMA,
                'template': TEMPLATE_SCHEMA,
                'recipients': RECIPIENT_SCHEMA,
                'job': JOB_SCHEMA,
            },
            required=True,
            extra=REMOVE_EXTRA,
        )
        payload = issuing_job_schema(request.get_json())
        tx_id, signed_certs = issue_certificate_batch(
            AttrDict(payload['issuer']),
            AttrDict(payload['template']),
            [AttrDict(rec) for rec in payload['recipients']],
            AttrDict(payload['job']),
        )
        return jsonify(dict(
            tx_id=tx_id,
            signed_certificates=list(signed_certs.values())
        ))

    @app.route('/config', methods=['GET'])
    def public_config():
        config = get_config()
        return jsonify(
            dict(
                ETH_PUBLIC_KEY=config.get('ETH_PUBLIC_KEY'),
                ETH_KEY_CREATED_AT=config.get('ETH_KEY_CREATED_AT'),
            )
        )

    @app.route('/tx/<chain>/<tx_id>', methods=['GET'])
    def tx_receipt(chain, tx_id):
        receipt = get_tx_receipt(chain, tx_id)
        if receipt:
            return jsonify(dict(receipt)), 200
        return f"Tx '{tx_id}' not found in chain '{chain}'.", 404

    @app.route('/verify', methods=['POST'])
    def verify():
        payload = request.get_json()
        results = verify_cert(payload)
        return jsonify(dict(
            verified=results[0],
            steps=results[1]
        ))


from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import MusicViewSet, AlbumViewSet, ArtistViewSet, AlbumItemViewSet

router = DefaultRouter()
router.register('music', MusicViewSet)
router.register('album', AlbumViewSet)
router.register('albumitem', AlbumItemViewSet)
router.register('artist', ArtistViewSet)

urlpatterns = [
    path('', include(router.urls))
]


import collections
import functools
import logging
import sys

logging.basicConfig(
    filename = 'NumberGuessingLog.out',
    filemode ='w',
    level = logging.DEBUG,
    format = '%(asctime)s - %(levelname)s: %(message)s',
    datefmt = '%Y-%m-%d %I:%M:%S',
    )

class NumberGuessing: # 2018.P.1
    def __init__(self):
        logging.info('Number Guessing')

    class Solver:
        def __init__(self, lower_bound, upper_bound, max_tries):
            self.lower_bound = lower_bound
            self.upper_bound = upper_bound
            self.max_tries = max_tries
            self.tries = 0
            self.queries = {}

        def guess(self, num: int) -> str:
            self.tries += 1
            print(num)
            sys.stdout.flush()
            result = input()
            return result

        def solve(self):
            left = self.lower_bound
            right = self.upper_bound
            while left <= right:
                num = left + (right - left) // 2
                response = self.guess(num)
                if response == 'CORRECT':
                    logging.info(f'The secret number was {num}!')
                    return
                elif response == 'TOO_SMALL':
                    left = num + 1
                elif response == 'TOO_BIG':
                    right = num - 1
                else:
                    logging.error(f'ERROR! Response from judge indicates a problem: {response}')
                    exit()

    def main(self):
        T = int(input())
        for _ in range(T):
            lower_bound, upper_bound = tuple(map(int, input().split(' ')))
            max_tries = int(input())
            solver = self.Solver(lower_bound, upper_bound, max_tries)
            solver.solve()

if __name__ == '__main__':
    # python InteractiveRunner.py python NumberGuessingJudge.py 0 -- python NumberGuessingSolver.py
    solver = NumberGuessing()
    solver.main()


# -*- coding: utf-8 -*-
import uuid
from datetime import datetime
from tempfile import NamedTemporaryFile
from time import time
from unittest.mock import MagicMock, patch

import pxapi
import pytest
from chaoslib.exceptions import ActivityFailed, InvalidExperiment

from chaospixie import (
    encode,
    execute_script,
    get_auth,
    handle_timestamp,
    load_script_from_file,
    nanotime_to_datetime,
    serialize_row,
)

PXL_SCRIPT = """
import px
df = px.DataFrame('http_events')[['resp_status','req_path']]
df = df.head(10)
px.display(df, 'http_table')
"""

def test_get_auth_expects_cluster_id():
    with pytest.raises(InvalidExperiment):
        get_auth({}, {"api_key": "secret"})

def test_get_auth_expects_api_key():
    with pytest.raises(InvalidExperiment):
        get_auth({"pixie_cluster_id": "abc"}, {})

def test_load_script_from_file_requires_realfile():
    with pytest.raises(ActivityFailed):
        load_script_from_file("blah.py")

def test_load_script_from_file():
    with NamedTemporaryFile() as f:
        f.write(PXL_SCRIPT.encode("utf-8"))
        f.seek(0)
        assert load_script_from_file(f.name) == PXL_SCRIPT

def test_serialize_row():
    r = MagicMock()
    r.columns = [
        MagicMock(column_name="cola", column_type=2),
        MagicMock(column_name="colb", column_type=2),
        MagicMock(column_name="colc", column_type=5),
    ]

    tableA = pxapi.data._TableStream(
        "a", relation=pxapi.data._Relation(r), subscribed=False
    )
    row = pxapi.data.Row(tableA, [1, 2, "three"])

    assert serialize_row(row) == {"cola": 1, "colb": 2, "colc": "three"}

def test_handle_time_():
    r = MagicMock()
    r.columns = [
        MagicMock(column_name="cola", column_type=2),
        MagicMock(column_name="colb", column_type=2),
        MagicMock(column_name="time_", column_type=6),
    ]

    tableA = pxapi.data._TableStream(
        "a", relation=pxapi.data._Relation(r), subscribed=False
    )
    row = pxapi.data.Row(tableA, [1, 2, int(time()) / 1e9])

    row = serialize_row(row)
    handle_timestamp(row)
    assert "_dt" in row

def test_handle_create_time():
    r = MagicMock()
    r.columns = [
        MagicMock(column_name="cola", column_type=2),
        MagicMock(column_name="colb", column_type=2),
        MagicMock(column_name="create_time", column_type=6),
    ]

    tableA = pxapi.data._TableStream(
        "a", relation=pxapi.data._Relation(r), subscribed=False
    )
    row = pxapi.data.Row(tableA, [1, 2, int(time()) / 1e9])

    row = serialize_row(row)
    handle_timestamp(row)
    assert "_dt" in row

def test_handle_start_time():
    r = MagicMock()
    r.columns = [
        MagicMock(column_name="cola", column_type=2),
        MagicMock(column_name="colb", column_type=2),
        MagicMock(column_name="start_time", column_type=6),
    ]

    tableA = pxapi.data._TableStream(
        "a", relation=pxapi.data._Relation(r), subscribed=False
    )
    row = pxapi.data.Row(tableA, [1, 2, int(time()) / 1e9])

    row = serialize_row(row)
    handle_timestamp(row)
    assert "_dt" in row

def test_encode_datetime():
    d = datetime.utcnow()
    i = d.isoformat()
    assert encode(d) == i

def test_encode_uuid():
    u = uuid.uuid4()
    assert encode(u) == str(u)

def test_nanotime_to_datetime():
    n = int(time()) * 1.0e9
    d = datetime.now()

    nanotime_to_datetime(n) == d

@patch("chaospixie.pxapi.Client", autospec=True)
def test_execute_script(client: MagicMock):
    c = MagicMock()
    s = MagicMock()
    c.prepare_script.return_value = s

    r = MagicMock()
    r.columns = [
        MagicMock(column_name="cola", column_type=2),
        MagicMock(column_name="colb", column_type=2),
        MagicMock(column_name="start_time", column_type=6),
    ]

    tableA = pxapi.data._TableStream(
        "a", relation=pxapi.data._Relation(r), subscribed=False
    )
    row = pxapi.data.Row(tableA, [1, 2, int(time()) / 1e9])

    s.results.return_value = [row]

    execute_script(c, PXL_SCRIPT, "http_table")


from django.contrib.sessions.backends.db import SessionStore
from django.contrib.gis.geos import LineString, Point
from django.core.serializers import serialize
from anchorwhat.models import Drawing, Questions, Survey, Sessions, PageTime, Pages, Trials, Sets
from datetime import datetime, timezone
from device_detector import DeviceDetector
from user_agents import parse
from random import choice
import json

def create_session(data):
    s = SessionStore()
    s.create()
    key = s.session_key
    ua = data["userAgent"]
    device = DeviceDetector(ua).parse()
    browser = parse(ua)
    start = datetime.now(timezone.utc)

    session = Sessions()
    session.django_key = key
    session.device = device.device_type()
    session.os = device.os_name()
    session.browser = browser.browser.family
    session.user_agent = ua
    session.start = start
    session.screen_width = data["resolution"][0]
    session.screen_height = data["resolution"][1]
    session.save()

    return key

def update_page_time(user, page, time):
    s = Sessions.objects.get(django_key=user)
    p = Pages.objects.get(pk=page)
    if PageTime.objects.filter(session=s.id, page_number=p.id).exists():
        pt = PageTime.objects.get(session=s.id, page_number=p.id)
        pt.duration_ms = pt.duration_ms + time
        pt.save()
    else:
        e = PageTime(
            session = s,
            page_number = p,
            duration_ms = time
        )
        e.save()

def insert_form(data):
    user = data['user']
    s = Sessions.objects.get(django_key=user)
    page = data['data']['page']
    questions = data['data']['questions']
    for question in questions:
        an = questions[question]['answer']
        if isinstance(an, list):
            a = ', '.join(an)
        else:
            a = an
        q = Questions.objects.get(page_number=page, number=question)
        if Survey.objects.filter(session=s, question=q).exists():
            new = Survey.objects.get(session=s, question=q)
            new.answer = a
        else:
            new = Survey(
                session = s,
                question = q,
                answer = a
            )
        new.save()

def insert_trial(data):
    session = Sessions.objects.get(django_key=data['user'])
    s = Sets.objects.get(id=data['setid'])
    trial = Trials(
        session = session,
        zoom = data['zoom'],
        start = data['start'],
        end = data['end'],
        duration_ms = data['duration'],
        set_id = s,
        layer1 = data['layers'][0],
        layer2 = data['layers'][1],
        layer3 = data['layers'][2],
        layer4 = data['layers'][3],
        layer5 = data['layers'][4]
    )
    trial.save()

    layers = data['objects']
    index = 1
    for l in layers:
        layer = index
        for o in l:
            nodes = []
            for p in o['geometry']:
                nodes.append(Point(p[0], p[1]))
            linestring = LineString(nodes, srid = 3857)
            line = Drawing(
                trial = trial,
                layer = layer,
                importance = o['importance'],
                thickness = o['thickness'],
                buffer = o['buffer'],
                geom_line = linestring,
                geom_poly = linestring.buffer(o['buffer'])
            )
            line.save()
        index += 1

def get_results(data, end):
    i = data['index']
    s = Sessions.objects.get(django_key=data['user'])
    if end:
        s.end = datetime.now(timezone.utc)
        s.save()

    usertrials = Trials.objects.filter(session=s)
    if i >= len(usertrials):
        i = 0
    usertrial = usertrials[i]
    userdrawing = Drawing.objects.filter(trial=usertrial)

    if usertrial.set_id.basemap == 'google':
        basemaptype = 'google'
        srid = 4326
    else:
        basemaptype = 'ol'
        srid = 3857

    user = serialize('geojson', userdrawing, geometry_field='geom_poly', fields=('layer', 'importance'), srid=srid)

    othertrials = Trials.objects.filter(zoom=usertrial.zoom, set_id=usertrial.set_id).exclude(session=s)
    if othertrials:
        othertrial = choice(othertrials)
        otherdrawing = Drawing.objects.filter(trial=othertrial)
        other = json.loads(serialize('geojson', otherdrawing, geometry_field='geom_poly', fields=('layer', 'importance'), srid=srid))
    else:
        other = False

    return {
        'index': i,
        'type': basemaptype,
        'basemap': usertrial.set_id.basemap,
        'center': [usertrial.set_id.x, usertrial.set_id.y],
        'zoom': usertrial.zoom,
        'user': json.loads(user),
        'other': other
    }


from bs4 import BeautifulSoup
import requests
import logging
import os

logger = logging.getLogger(__name__)

def crawl(alph):
    for letter in alph:
        # this can run a long time its helpfull to know which letter its on
        logger.info(f"processing letter {letter}...")
        url = "http://www.mso.anu.edu.au/~ralph/OPTED/v003/wb1913_" + letter + ".html" #there is a page for each letter
        req = requests.get(url) #grab page
        soup = BeautifulSoup(req.text, "html.parser") #get parser
        dictionary = soup.find_all('p') # find all the dictionary entries
        for entries in dictionary:
            word = entries.find('b').getText() # get the word itself
            pos = entries.find('i').getText() # get the part of speech
            cut = len(word) + len(pos) + 4 # calulate how much word and pos take up
            definition = entries.getText()[cut:] # cut that from the total sting to get definition
            yield word, pos, definition

#            DO what you need here
# this loop will run through all the words in the
# english dictionary, seperating them by word, pos and definition
#
# example to print out CSV to stdout
# use DEV=true to test small amount of words
if __name__ == '__main__':
    alph = "abcdefghijklmnopqrstuvwxyz"
    if os.environ.get('DEV'):
        alph = "x"
    print("word,pos,definition")
    for word, pos, definition in crawl(alph):
        print(f"{word},{pos},\"{definition}\"")


'''
This module can change the volume on windows hosts courtesy of linearWinVolume
'''

import modules.number_parser
import utils.str_tools
import os

workingDir = os.path.dirname(os.path.realpath(__file__))+os.path.sep

vocab = {
    "raise":['volume',4],
    "lower":['volume',4],
    "volume":['volume',9],
    "to":['volume',3],
    "by":['volume',3],
    "percent":['volume',4],
    "mute":['volume',9],
    "maximum":['volume',2],
    "set":['volume',3],
}

'''
examples:
raise volume by 5
lower volume by 5 percent
set volume to 50 percent
set volume to 50
what is the volume
mute volume
mute
set volume to maximum
'''

numbers = modules.number_parser.digits_English.copy()
numbers.update(modules.number_parser.tens_English)

def vol_parse(string):
    index = 0
    grammer_flag = None
    for word in string:
        index += 1
        if word == 'by':
            grammer_flag = 'by'
            break
        elif word == 'to':
            grammer_flag = 'to'
            break
    vol_value = utils.str_tools.filter_for_numbers(' '.join(string[index:]))
    return [modules.number_parser.parse_number(vol_value),grammer_flag]

def parse(text):
    print('parsing!')
    try:
        phrase = text.split()
        if 'mute' in phrase:
            os.system('py "{}volume_daemon.py" set {}'.format(workingDir,0))
            # linearwinvolume.set_volume(0)
            return 1
        arguments = vol_parse(phrase)
        if 'to' in phrase:
            os.system('py "{}volume_daemon.py" set {}'.format(workingDir,arguments[0]))
            # linearwinvolume.set_volume(arguments[0])
        elif 'by' in phrase:
            if 'raise' in phrase:
                # linearwinvolume.change_volume(arguments[0])
                os.system('py "{}volume_daemon.py" change {}'.format(workingDir,arguments[0]))
            elif 'lower' in phrase:
                # linearwinvolume.change_volume(-arguments[0])
                os.system('py "{}volume_daemon.py" change -{}'.format(workingDir,arguments[0]))
        return 1
    except Exception as e:
        print(e)
        return 0


import matplotlib.pyplot as plt
import scipy.interpolate as interp
import numpy as np
import decimal as d

d.getcontext().prec = 3

thein = [0.0, 0.04, 0.08, 0.16, 0.50]
theout = [0.002, 0.032, 0.063, 0.13, 0.42]
thecout = [0.013, 0.010, 0.014, 0.013, 0.0066]
thex1out = [-0.65, -0.64, -0.65, -0.62, -0.60]

them, theb = np.polyfit(thein, theout, 1)
trueoffset = (0.08 - theb) / them
offdec = d.Decimal((0.08 - theb) / them) + d.Decimal(0)
print('slope:', them, '  y-intercept:', theb)
print('True offset value:', trueoffset)

plt.plot(np.linspace(-0.1, 0.6, 5), np.linspace(-0.1, 0.6, 5) * them + theb, '--', color='black', label='Best Fit')
plt.scatter(thein, theout, color='blue', s=60, marker='D', alpha=0.5, label='Sim')
plt.scatter(trueoffset, trueoffset * them + theb, color='green', marker='v', s=80, alpha=0.6, label='8%')
plt.annotate(str(offdec), xy=(trueoffset, trueoffset * them + theb),
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0", alpha=0.9),
             xycoords='data', xytext=(trueoffset, trueoffset * them + theb + 0.1))
plt.xlabel('Input Mass Step')
plt.ylabel('Output Mass Step')
plt.xlim(-0.01, 0.6)
plt.ylim(-0.01, 0.6)
plt.legend(loc=2)
plt.show()

plt.plot(thein, thecout, color='red')
plt.scatter(thein, thecout, color='blue', s=60, marker='D')
plt.xlabel('Input Mass Step')
plt.ylabel('Output Color Step')
plt.xlim(-0.01, 0.6)
plt.ylim(-0.1, 0.1)
plt.show()

plt.plot(thein, thex1out, color='red')
plt.scatter(thein, thex1out, color='blue', s=60, marker='D')
plt.xlabel('Input Mass Step')
plt.ylabel('Output Stretch Step')
plt.xlim(-0.01, 0.6)
plt.ylim(-1, 0)
plt.show()


# Silly example of date comparison to get age

from datetime import date

birth_year = int(input("Year of birth: "))
birth_month = int(input("Month of birth: "))
birth_day = int(input("Day of birth: "))
birthday_date = date(birth_year, birth_month, birth_day)
age = date.today() - birthday_date

result = "You are " + str(int(age.days / 365.25)) + " years old"
print(result)


from imageai.Detection import ObjectDetection
import cv2
from object_detector.DetectorAPI import DetectorAPI

class ObjectDetector:

    model_class_labels = ["person", "bicycle", "car", "motorcycle", "airplane",
                          "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign",
                          "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra",
                          "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard",
                          "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
                          "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange",
                          "broccoli", "carrot", "hot dog", "pizza", "donot", "cake", "chair", "couch", "potted plant", "bed",
                          "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave",
                          "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair dryer",
                          "toothbrush"]

    def __init__(self, model_name, model_path):
        self.model_path = model_path
        self.model_name = model_name
        if model_name == 'other':
            self.object_detector = DetectorAPI(path_to_ckpt=model_path)
        else:
            self.object_detector = ObjectDetection()
            if model_name == 'yolov3':
                self.object_detector.setModelTypeAsYOLOv3()
            elif model_name == 'tinyyolov3':
                self.object_detector.setModelTypeAsTinyYOLOv3()
            elif model_name == 'retinanet':
                self.object_detector.setModelTypeAsRetinaNet()
            self.object_detector.setModelPath(model_path)
            self.object_detector.loadModel()

    def detect(self, input_image_path, output_image_path, extract_detected_objects=True, display_percentage_probability=False, display_object_name=False, threshold=0.7):

        if self.model_name == 'other':
            detected_objects_location = []
            img = cv2.imread(input_image_path)
            boxes, scores, classes, _ = self.object_detector.process_frame(
                img)
            detections = []
            for i in range(len(boxes)):
                if scores[i] > threshold:
                    detections.append({
                        'name': self.model_class_labels[classes[i] - 1]
                    })
                    box = boxes[i]
                    cv2.rectangle(img, (box[1], box[0]),
                                  (box[3], box[2]), (255, 0, 0), 2)
            cv2.imwrite(output_image_path, img)
        else:
            detections, detected_objects_location = self.object_detector.detectObjectsFromImage(
                input_image=input_image_path,
                output_image_path=output_image_path,
                extract_detected_objects=extract_detected_objects,
                display_percentage_probability=display_percentage_probability,
                display_object_name=display_object_name
            )
        return detections, detected_objects_location


import json
import sys
import threading
import kafka
import requests

threads = []
cookie = "token=code_space;"
header = {
    "cookie": cookie,
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/92.0.4515.159 Safari/537.36"
}

class PullThread(threading.Thread):
    def __init__(self, thread_name, topic, partition):
        threading.Thread.__init__(self)
        self.thread_name = thread_name
        self.partition = partition
        self.topic = topic

    def run(self):
        print("Starting " + self.name)
        set_consumer(self.thread_name, self.topic, self.partition)

    @staticmethod
    def stop():
        sys.exit()

def set_consumer(thread_name, topic, partition):
    broker_list = brokers
    # set topic and partition, construct object tp
    tp = kafka.TopicPartition(topic, partition)
    consumer = kafka.KafkaConsumer(
        bootstrap_servers=broker_list,
        group_id="robot",
        client_id=thread_name,
        enable_auto_commit=False,
        fetch_min_bytes=1024 * 1024,  # 1M
        fetch_max_wait_ms=60000,  # 30s
        request_timeout_ms=305000,
    )
    # assign consumer to partition
    consumer.assign([tp])
    # get offset of last consuming
    offset = consumer.end_offsets([tp])[tp]
    # set current consuming offset
    consumer.seek(tp, offset)
    print(u"Running at thread:", thread_name, u"Partition:", partition,
          u"With offset:", offset, u"\tConsuming...")

    consume_num = 0
    while True:
        msg = consumer.poll(timeout_ms=60000)  # 30s
        end_offset = consumer.end_offsets([tp])[tp]
        if len(msg) > 0:
            print(u"At thread:", thread_name, u"Partition:", partition,
                  u"Max offset:", end_offset, u"With Data:", len(msg))
            line_num = 0
            for data in msg.values():
                for line in data:
                    response = {"status": 200}
                    print("Received:", line)
                    user_id = 0
                    serial_num = 0
                    line_num += 1
                    '''
                    We shall generate next line here with given "line"
                    '''
                    gen_line = line
                    print("Generated:", line)
                    post_dict = {
                        f"{user_id}": f"{serial_num}",
                        'data_content': f'{gen_line}'
                    }
                    while response["status"] != "200":
                        response = requests.post(receiver, data=post_dict, headers=header, cookies=cookie)
            print(thread_name, ", total lines:", line_num)
            # submit the offset to broker server
            consumer.commit(offsets={tp: (kafka.OffsetAndMetadata(end_offset, None))})
            if not 0:
                # shut down this sub-thread
                sys.exit()
        else:
            print(thread_name, 'no data fetched')
        consume_num += 1
        print(thread_name, "Consuming at times:", consume_num)

if __name__ == '__main__':
    thread_num = 3
    brokers = "127.0.0.1:9092"
    receiver = "127.0.0.1:5000"
    # "ip:9092,127.0.0.1:9092"
    for i in range(thread_num):
        threads.append(PullThread(f"Thread-{i}", "line_gen_topic", i))
    for t in threads:
        t.start()
    # let main thread wait for all sub-threads ending
    for t in threads:
        t.join()


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 20 13:40:01 2023

@author: oscar
"""

import os
import sys
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import flopy
from flopy.utils.triangle import Triangle as trng
import geopandas as gpd
from shapely.geometry import Polygon
import sfrmaker
import pandas as pd
import dsvFunctions as dsvf
import matplotlib.tri as mtri
import River_conections as rvrCon
import pyvista as pv

def triangleMesh_to_Shapefile(grid,fileName):

    grid.get_vertices()

    cell2d = grid.get_cell2d()

    vertices = grid.get_vertices()

    triangles= list()

    for cell in cell2d:

        vy = [vertices[cell[-1]][-1],vertices[cell[-2]][-1],vertices[cell[-3]][-1],vertices[cell[-1]][-1]]

        vx = [vertices[cell[-1]][-2],vertices[cell[-2]][-2],vertices[cell[-3]][-2],vertices[cell[-1]][-2]]

        polygon = Polygon(list(zip(vx,vy)))

        triangles.append(polygon)

    poly_df = gpd.GeoDataFrame(geometry=triangles)

    poly_df.to_file(fileName,driver='ESRI Shapefile')

def create_Mesh_Dataframe(grid):

    grid.get_vertices()
    cell2d = grid.get_cell2d()
    vertices = grid.get_vertices()
    triangles= list()
    cell_ID = list()
    i  =list()
    k = list()
    nodei = 0
    nodek = 1

    for cell in cell2d:
        vy = [vertices[cell[-1]][-1],vertices[cell[-2]][-1],vertices[cell[-3]][-1],vertices[cell[-1]][-1]]
        vx = [vertices[cell[-1]][-2],vertices[cell[-2]][-2],vertices[cell[-3]][-2],vertices[cell[-1]][-2]]
        polygon = Polygon(list(zip(vx,vy)))
        triangles.append(polygon)
        cell_ID.append(nodei)
        k.append(nodei)
        i.append(nodei)
        nodei = nodei + 1

    grid_df = pd.DataFrame({"k":k,"i":i,"j":i,"nodenumber":cell_ID,"geometry":triangles})
    return grid_df

def geoJson_to_Pandas(geoJsonPath):

    gdf = gpd.read_file('file.geojson')

    gdf.to_file('file.shp')

def generate_Empty_Modflow_Model(exe_path,sim_ws):

    name = 'mf'
    sim = flopy.mf6.MFSimulation(sim_name=name, version='mfusg',
                                 exe_name=exe_path,
                                 sim_ws=sim_ws)
    tdis = flopy.mf6.ModflowTdis(sim, time_units='SECONDS',
                                 perioddata=[[1.0, 1, 1.]])
    gwf = flopy.modflow.Modflow(modelname=name)
    gwf._version = 'mfusg'
    nlay = 1
    top = 1.
    botm = [0.]
    dis = flopy.modflow.ModflowDis(gwf,nlay=nlay,
                               top=top, botm=botm)
    bas = flopy.modflow.ModflowBas(gwf, ibound=1)
    return gwf

def sfr_to_vtk(mesh,sfr,reach_cell_dict,project_name):

    simulation_result = mesh.copy()
    n_cells = mesh.n_cells
    record_names = sfr.output.budget().get_unique_record_names(decode = True)
    sfr_budget = sfr.output.budget()
    sfr_times = sfr_budget.get_times()
    river_cell_id = list(reach_cell_dict.values())
    count = 0
    bc_cells  = dict
    bc_cells.update({'river':{'bc_id':1,'cell_ids':river_cell_id}})
    init_time = True
    for time in sfr_times:
        if init_time == True:

            all_outputs  = sfr_budget.get_data(totim = time)
            sfr_heads = sfr.output.stage().get_data(totim = time)[0][0]

            for output in all_outputs:
                output_name = record_names[count]
                if "FLOW-JA-FACE" in output_name:

                    outFlow  = np.zeros(n_cells)
                    inFlow  = np.zeros(n_cells)
                    flow = output["q"]
                    node2 = output["node2"]
                    for i in range(0,len(flow)):
                        if flow[i] <0:
                            outFlow[reach_cell_dict[node2[i]]] = flow[i] + outFlow[reach_cell_dict[node2[i]]]
                        if flow[i] >0:
                            inFlow[reach_cell_dict[node2[i]]] = flow[i] + inFlow[reach_cell_dict[node2[i]]]

                    simulation_result = mesh.copy()
                    simulation_result.cell_data[output_name]=outFlow
                    simulation_result.save(os.path.join("D:/Master_Erasmus/Thesis/Results",'sim_t_out_1.vtu'))
                    simulation_result = mesh.copy()
                    simulation_result.cell_data[output_name]=inFlow
                    simulation_result.save(os.path.join("D:/Master_Erasmus/Thesis/Results",'sim_t_in_1.vtu'))

                if "GWF" in output_name:
                    output_array_gwf  = np.zeros(n_cells)
                    output_array_head  = np.zeros(n_cells)
                    bc_cells.update({output_name.lower():{'bc_id':count+1,'cell_ids':river_cell_id}})
                    flow = output["q"]
                    node = output["node"]
                    node2 = output["node2"]
                    for i in range(0,len(flow)):
                        output_array_gwf[node2[i]-1]=flow[i]
                        output_array_head[node2[i]-1]=sfr_heads[i]
                    simulation_result = mesh.copy()
                    simulation_result.cell_data["q_leak_SFR_(m3/s)"]=output_array_gwf
                    simulation_result.save(os.path.join("D:/Master_Erasmus/Thesis/Results",'sim_t_1_gwf.vtu'))
                    simulation_result = mesh.copy()
                    simulation_result.cell_data["h_stage_SFR (m)"]=output_array_head
                    simulation_result.save(os.path.join("D:/Master_Erasmus/Thesis/Results",'sim_t_1_head.vtu'))
                    print(output_array_gwf.tolist())

                else:
                    x=2

                count = count + 1

            init_time = False

    # write_pvd(pvd_filename=os.path.join("D:/Master_Erasmus/Thesis/Results",'sim1.pvd'),ts_name_dict='sim_t_1.vtu')
    result_df = pd.DataFrame( {"gw_head (m)":sim.get_model().output.head().get_data()[0][0],
                             "h_stage (m)":output_array_head,
                             "q_leak_(m3/s)":output_array_gwf})
    result_df.to_csv(os.path.join("D:/Master_Erasmus/Thesis/Results",'MODFLOW_resultq0.1.csv'))

    sfr_package_convergence = sfr.output.package_convergence()
    sfr_obs = sfr.output.obs()

workspace ="D:/Master_Erasmus/Thesis/Meras"
'''
#US case

#Define the active domain
active_domain = [(330177, 1246872), (333018, 1246872), (333018, 1249483), (330177, 1249483)]

#Creates triangular grid
tri = trng(angle=20, model_ws=workspace, exe_name="D:/Master_Erasmus/Thesis/Meras/Exe/triangle.exe")
tri.add_polygon(active_domain)
tri.add_region((330277,1246972),0,maximum_area=1000) #coarse discretization
tri.build()

#Plot the grid
fig = plt.figure(figsize=(15, 15))
ax = plt.subplot(1, 1, 1, aspect='equal')
tri.plot(edgecolor='gray')
cell2d = tri.get_cell2d()
vertices = tri.get_vertices()

cell2d = tri.get_cell2d()
vertices = tri.get_vertices()
xcyc = tri.get_xcyc()
ncpl = tri.ncpl
nvert = tri.nvert

grd_df = create_Mesh_Dataframe(tri)
leftcells = tri.get_edge_cells(4)
rightcells = tri.get_edge_cells(2)
'''
#Input exe path
exe_path = "D:/mf6.4.1/bin/mf6.exe"

#Input: Simulation workspace
sim_ws = "D:/Master_Erasmus/Thesis/ModflowModel"

#Input Coordiante system
crs = 'epsg:25833'

#Crete the MODFLOW model
gwf = generate_Empty_Modflow_Model(exe_path,sim_ws)

#triangleMesh_to_Shapefile(tri,"grid2.shp")
grd_df2 = gpd.read_file("D:/Master_Erasmus/Thesis/gridv2/gridv2.shp")

#Cretes the unstructured grid from a shapefile to DISV
grd = sfrmaker.grid.UnstructuredGrid.from_dataframe(
    grd_df2,
    node_col='nodenumber',
    geometry_column='geometry',
    model_units='meters',
    crs='epsg:25833'
)

#US shapeflie path  D:/Master_Erasmus/Thesis/sfrmaker/examples/meras/flowlines.shp
#Synthetic river path D:/Master_Erasmus/Thesis/Germany_River1_v2.shp
#Assignation of the rivers to the grid cells
custom_lines = sfrmaker.Lines.from_shapefile(shapefile='D:/Master_Erasmus/Thesis/Germany_River1_v2.shp',
                                              id_column='COMID',  # arguments to sfrmaker.Lines.from_shapefile
                                              routing_column='tocomid',
                                              width1_column='width1',
                                              width2_column='width2',
                                              up_elevation_column='elevupsmo',
                                              dn_elevation_column='elevdnsmo',
                                              name_column='GNIS_NAME',
                                              attr_length_units='meters',  # units of source data
                                              attr_height_units='meters',
                                              crs='epsg:25833',
                                              )

#m = flopy.modflow.Modflow.load('shellmound.nam', model_ws='D:/Master_Erasmus/Thesis/sfrmaker/sfrmaker/test/data/shellmound/shellmound')
#Export the assigned rivers to a shapefile file for post processing
sfrdata = custom_lines.to_sfr(grid=grd, model=gwf)
sfrdata.write_package(version='mf6',filename='Test2')
sfrdata.export_cells(filename="Test2_grid.shp")
sfrdata.export_lines(filename="Test2.shp")

sfr_df = sfrdata.reach_data

'''
MODFLOW model set up
'''
#Creation of the modflow test case
name = 'mf'
times = [1]
sim = flopy.mf6.MFSimulation(sim_name=name, version='mf6',
                             exe_name=exe_path,
                             sim_ws="D:/Master_Erasmus/Thesis/workSpace")

tdis = flopy.mf6.ModflowTdis(sim, time_units='SECONDS',
                             perioddata=[[1, 1, 1.]])

gwf = flopy.mf6.ModflowGwf(sim, modelname=name, save_flows=True)

ims = flopy.mf6.ModflowIms(sim, print_option='SUMMARY', complexity='complex',
                           outer_hclose=1.e-8, inner_hclose=1.e-8)

tri = dsvf.vtk_to_disv(r"D:/Master_Erasmus/Thesis/example_basin/mesh.vtu")

cell2d = tri["cells2d"]

#To do: make te clock whise polygon general
for cell in range(0,len(cell2d)):
    k  = cell2d[cell][-1]
    l = cell2d[cell][-2]
    cell2d[cell][-1] = l
    cell2d[cell][-2] = k

vertices = tri["vertices"]
nvert = tri["nvert"]
ncpl = tri["ncpl"]
dis = flopy.mf6.ModflowGwfdisv(gwf, length_units='METERS',
                                nlay=1, ncpl=ncpl, nvert=nvert,
                                top=[5], botm=[0],
                                vertices=vertices, cell2d=cell2d)

npf = flopy.mf6.ModflowGwfnpf(gwf, k=0.0001)
ic = flopy.mf6.ModflowGwfic(gwf, strt=5.0)
sto = flopy.mf6.ModflowGwfsto(gwf,iconvert =1,ss = 0)

chdList = []

up = [0,20,40,60,80]
down = [19,39,59,79,99]

for icpl in up: chdList.append([(0, icpl), 1])
for icpl in down: chdList.append([(0, icpl), 0])

chd = flopy.mf6.ModflowGwfchd(gwf, stress_period_data=chdList)
oc = flopy.mf6.ModflowGwfoc(gwf,
                            budget_filerecord='{}.cbc'.format(name),
                            head_filerecord='{}.hds'.format(name),
                            saverecord=[('HEAD', 'all'),
                                        ('BUDGET', 'all')],
                            printrecord=[('HEAD', 'all'),
                                         ('BUDGET', 'all')])

'''
SFR set up
'''
connectionData = rvrCon.river_Conections("Test2.shp",crs = crs)
pckgData = []
pckgConections = []
pckgDiversion = []
reach_cell_dict = {}
num_cells = 100
reach_ID_list = ["No_reach"] *num_cells

for i in range(0,sfr_df.shape[0]):
    reach_ID_list[sfr_df["node"][i]] = sfr_df["rno"][i]

    rno = sfr_df["rno"][i]
    num_Connections = len(connectionData[rno]["in"])+len(connectionData[rno]["out"])

    data = (sfr_df["rno"][i]-1, 0, sfr_df["node"][i], sfr_df["rchlen"][i],
            0.1, 0.01,2,
            0.5,0.0001,0.04,num_Connections,1,0)
    reach_cell_dict[sfr_df["rno"][i]] = sfr_df["node"][i]

    if len(connectionData[rno]["out"]) > 1:

        data = (sfr_df["rno"][i]-1, 0, sfr_df["node"][i], sfr_df["rchlen"][i],
                0.1, 0.01,2,
                0.5,0.0001,0.04,num_Connections,1,1)

        pckgDiversion.append([sfr_df["rno"][i]-1, 0, 15,"UPTO"])

    if rno == 16:
        data = (sfr_df["rno"][i]-1, 0,sfr_df["node"][i], sfr_df["rchlen"][i],
                0.1, 0.01,2,
                0.5,0.0001,0.04,num_Connections,0,0)

    pckgData.append(data)
    pckgConections.append([rno-1,*connectionData[rno]["out"],*connectionData[rno]["in"]])

channel_flow_data = [[0,"inflow",50],[6,"inflow",50],[11,"diversion",0,50]]

sfr = flopy.mf6.ModflowGwfsfr(gwf,nreaches=sfr_df.shape[0],packagedata=pckgData,
                              connectiondata=pckgConections,diversions=pckgDiversion,
                              print_flows=True,print_input =True,print_stage=True,
                              save_flows=True,stage_filerecord="sfr_stage.hds",
                              budgetcsv_filerecord ='{}.csv'.format(name),
                              budget_filerecord =  '{}.cbc'.format(name+"_SFR"),
                              perioddata=channel_flow_data,package_convergence_filerecord='conv{}.csv'.format(name)
                              )

'''
Simulaion set up
'''
sim.write_simulation()
success, buff = sim.run_simulation()

hdobj = flopy.utils.HeadFile("D:/Master_Erasmus/Thesis/workSpace/mf.hds", precision='double')

'''
Simulaion set up
'''

sim.write_simulation()
success, buff = sim.run_simulation()

'''
Write Results
'''
# sfr_flows = flopy.utils.CellBudgetFile("D:/Master_Erasmus/Thesis/Simple_case/SimpleModflow/mf_SFR.cbc")
# flows = flopy.utils.CellBudgetFile("D:/Master_Erasmus/Thesis/Simple_case/SimpleModflow/mf_SFR.cbc").get_data(idx = 1)
# times = [10]
cell_id_list = range(1, 101)

result_dict = {}
prjct_name = "Synthetic_Network_Base_Case_SFR_width_0.1m"
stress_len = 1
result_path = "D:/Master_Erasmus/Thesis/Results_SFR"

csv_name = prjct_name+"_results_"+"qgwf"+"_stress_p_"+str(stress_len)+".csv"
cell_dict = {"cell_id" : cell_id_list,"reach_id":reach_ID_list}
result_df_qgwf = pd.DataFrame(cell_dict)

for time in times:
    result_qgwf = [0]*num_cells
    data = sfr.output.budget().get_data(text="GWF", totim= time)[0]["q"]
    cells_qgwf = sfr.output.budget().get_data(text="GWF", totim= time)[0]["node2"]

    i=0
    for j in range(0,len(result_qgwf)):
        if cell_dict["reach_id"][j] == "No_reach":
            result_qgwf[j] = 0

        else:
            result_qgwf[cells_qgwf[i]-1] = data[i]
            i = i+1

    result_df_qgwf["q leak time "+str(time)+" (m3/s)"] = result_qgwf
result_df_qgwf.to_csv(os.path.join(result_path,csv_name))

csv_name = prjct_name+"_results_"+"stage"+"_stress_p_"+str(stress_len)+".csv"
cell_dict = {"cell_id" : cell_id_list,"reach_id":reach_ID_list}
result_df_stage = pd.DataFrame(cell_dict)

for time in times:
    i = 0
    result_stage = [0]*num_cells

    data = list(sfr.output.stage().get_data(totim = time)[0][0])

    for j in range(0,len(result_stage)):

        if cell_dict["reach_id"][j] == "No_reach":
            result_stage[j] = 0
        else:
            result_stage[cells_qgwf[i]-1] = data[i]
            i = i+1

    result_df_stage["h stage time "+str(time)+" (m)"] = result_stage

result_df_stage.to_csv(os.path.join(result_path,csv_name))

csv_name = prjct_name+"_results_"+"hgw"+"_stress_p_"+str(stress_len)+".csv"
cell_dict = {"cell_id" : cell_id_list,"reach_id":reach_ID_list}
result_df = pd.DataFrame(cell_dict)
i = 0
for time in times:
    result = [0]*num_cells

    result = sim.get_model().output.head().get_data(totim = time)[0][0]
    result_df["gw head time "+str(time)+" (m)"] = result

    i = i+1

result_df.to_csv(os.path.join(result_path,csv_name))

# head = hdobj.get_data()

# flows = flopy.utils.CellBudgetFile("D:/Master_Erasmus/Thesis/workSpace/mf.cbc").get_data(idx = 1)
# sfr_flows = flopy.utils.CellBudgetFile("D:/Master_Erasmus/Thesis/workSpace/mf_SFR.cbc")
# # sfr_flows.list_records()
# sfr_inpt = sfr_flows.get_data(idx = 6, text = "EXT-INFLOW")
# print(sim.get_model().output.head().get_data()[0][0])

# x = np.zeros(nvert)
# y = np.zeros(nvert)
# triangles = []

# for i in range(0,nvert):
#     x[i] = tri["vertices"][i][-2]
#     y[i] = tri["vertices"][i][-1]

# for i in range(0,ncpl):
#     triangles.append([tri["cells2d"][i][-3],tri["cells2d"][i][-2],tri["cells2d"][i][-1]])

# '''
# Simulaion plotting
# '''
# triang = mtri.Triangulation(x, y, triangles)

# fig, ax = plt.subplots()
# ax.triplot(triang, 'ko-')
# tpc = ax.tripcolor(triang,facecolors = head[0][0])
# fig.colorbar(tpc)
# plt.show()

# gwf.package_names
# gwf.get_package_list()
# u=gwf.output.methods()

#Function to get the available methods of the sfr output
# sfr_methods = sfr.output.methods()

# sfr_package_convergence = sfr.output.package_convergence()
# sfr_obs = sfr.output.obs()
# sfr_stage = sfr.output.stage()

# '''
# VTK creation
# '''
# mesh = pv.read(r"D:/Master_Erasmus/Thesis/example_basin/mesh.vtu")
# sfr_to_vtk(mesh,sfr,reach_cell_dict,name)
# simulation_result = mesh.copy()
# simulation_result.cell_data["gw_head(m)"]=sim.get_model().output.head().get_data()[0][0]
# simulation_result.save(os.path.join("D:/Master_Erasmus/Thesis/Results",'sim_gw_head.vtu'))
# fig = plt.figure(figsize=(15, 15))
# ax = plt.subplot(1, 1, 1, aspect='equal')
# img=tri.plot(ax=ax, a=head[0, 0, :], cmap='Spectral')
# fig.colorbar(img, fraction=0.02)

# fname = os.path.join(sim_ws, name + '.hds')
# hdobj = flopy.utils.HeadFile(fname, precision='double')
# head = hdobj.get_data()


# _*_coding:utf-8_*_
# __author: a123456
import random
import time
from functools import wraps

def run_time(func):
    """打印函数运行时间, 毫秒"""

    @wraps(func)  # 获取函数自身名称
    def inner(*args, **kwargs):
        no_print = kwargs.get("no_print")
        if no_print:
            kwargs.pop("no_print")
            return func(*args, **kwargs)

        begin = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        if not kwargs.get("use"):
            print(f"{func.__name__}: {end - begin} s")
        return result

    return inner

class T:

    @classmethod
    @run_time
    def build_test_list(cls, n: int, range_l: int = 0, range_r: int = 10) -> []:
        """
        构建测试排序用列表
        :param n:  生成数量
        :param range_l: 左边界
        :param range_r: 右边界
        :return: []
        """
        assert range_l < range_r, "range_l >= range_r"

        arr = [random.randint(range_l, range_r) for i in range(n)]
        return arr

    @classmethod
    @run_time
    def build_order_list(cls, n: int, range_l: int = 0, range_r: int = 10) -> []:
        """
        构建测试排序用列表， 基本有序的列表
        :param n:  生成数量
        :param range_l: 左边界
        :param range_r: 右边界
        :return: []
        """
        assert range_l < range_r, "range_l >= range_r"

        arr = [random.randint(range_l, range_r) for i in range(n)]

        for i in range(10):
            i1 = random.randint(0, n - 1)
            i2 = random.randint(0, n - 1)
            arr[i1], arr[i2] = arr[i2], arr[i1]

        return arr

    @classmethod
    def swap(cls, i1, i2, arr):
        arr[i1], arr[i2] = arr[i2], arr[i1]

tool = T

if __name__ == '__main__':
    test_list = tool.build_test_list(10, 0, 10)


import sys, base64
import numpy as np
from PIL import Image
from io import BytesIO, StringIO
import skimage.io
import tensorflow as tf

from main import foward_pass
from helper import ImageProcess

file = sys.argv[-1]

if file == 'demo.py':
    print("Error loading video")
    quit

# Define encoder function
def encode(array):
    assert array.shape==(600,800)
    pil_img = Image.fromarray(array)
    buff = BytesIO()
    pil_img.save(buff, format="PNG")
    return base64.b64encode(buff.getvalue()).decode("utf-8")

def naive_calssify(image_array):
    red = image_array[:, :, 0]
    # Look for red cars :)
    binary_car_result = np.where(red > 250, 1, 0).astype('uint8')

    # Look for road :)
    binary_road_result = binary_car_result = np.where(red < 20, 1, 0).astype('uint8')

    return [encode(binary_car_result), encode(binary_road_result)]

def test_output():
    vgg_dir = './data'
    num_classes = 3
    with tf.Session() as sess:
        input_image, keep_prob, logits, train_op, cross_entropy_loss, correct_label, learning_rate \
            = foward_pass(num_classes, sess, vgg_dir)

        saver = tf.train.Saver()

        ckpt = tf.train.get_checkpoint_state('./model_ckpt/')
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)

        ip = ImageProcess(sess, keep_prob, input_image, logits, image_shape=(640, 832))

        pip_image = ip.pipeline(skimage.io.imread("./data/CameraRGB/0.png"))

        import matplotlib.pyplot as plt
        plt.imshow(pip_image)
        plt.show()

if __name__ == "__main__":
    test_image = skimage.io.imread("./data/CameraRGB/0.PNG")

    naive_calssify(test_image)

    test_output()


from django.core.mail import EmailMessage
from django.db.models import Q, Sum
from django.template.loader import render_to_string
from django.http import HttpResponse

from .utils import send_email

from django.contrib.auth import get_user_model
from django_q.tasks import async_task
from django_q.models import Schedule

from settings.models import LabsManagerSetting
from fund.models import Fund
import datetime
from django.utils import timezone

from auditlog.models import LogEntry

import logging
logger = logging.getLogger('labsmanager')

# =========== Task global scheduler on start up call ========== #
def scheduleBaseTasks():
    logger.debug("[scheduleBaseTasks] starting ...")
    # for audit log cleaning
    currSch = Schedule.objects.filter(func='labsmanager.tasks.clean_auditlog')
    if not currSch:
        sch = Schedule.objects.create(name="clean_auditlog",
                                      func='labsmanager.tasks.clean_auditlog',
                            #hook='hooks.print_result',
                            schedule_type=Schedule.CRON,
                            cron = '0 3 1 * *',
                            #hook="labsmanager.tasks.sendSuperUserMail",
                            )
        logger.debug("New Schedule :"+str(sch))
    else:
        logger.debug("Already in schedules :"+str(currSch))

    # for Fund update calculation
    currSch = Schedule.objects.filter(func='labsmanager.tasks.update_all_calculation')
    if not currSch:
        sch = Schedule.objects.create(name="update_all_calculation",
                                      func='labsmanager.tasks.update_all_calculation',
                            #hook='hooks.print_result',
                            schedule_type=Schedule.CRON,
                            cron = '30 3 * * 1-5',
                            #hook="labsmanager.tasks.sendSuperUserMail",
                            )
        logger.debug("New Schedule :"+str(sch))
    else:
        logger.debug("Already in schedules :"+str(currSch))

    # for Vacation JSON file pulling
    currSch = Schedule.objects.filter(func='leave.tasks.pull_vacation_file')
    if not currSch:
        sch = Schedule.objects.create(name="pull_vacation_file",
                                      func='leave.tasks.pull_vacation_file',
                            #hook='hooks.print_result',
                            schedule_type=Schedule.CRON,
                            cron = '40 3 * * 1-5',
                            #hook="labsmanager.tasks.sendSuperUserMail",
                            )
        logger.debug("New Schedule :"+str(sch))
    else:
        logger.debug("Already in schedules :"+str(currSch))

    # for Notifications
    currSch = Schedule.objects.filter(func='common.tasks.check_notifications_tasks')
    if not currSch:
        sch = Schedule.objects.create(name="check_notifications_tasks",
                                      func='common.tasks.check_notifications_tasks',
                            #hook='hooks.print_result',
                            schedule_type=Schedule.HOURLY,
                            #hook="labsmanager.tasks.sendSuperUserMail",
                            )
        logger.debug("New Schedule :"+str(sch))
    else:
        logger.debug("Already in schedules :"+str(currSch))

    # check periodically the scheduled tasks
    currSch = Schedule.objects.filter(func='labsmanager.tasks.scheduleBaseTasks')
    if not currSch:
        sch = Schedule.objects.create(name="scheduleBaseTasks",
                                      func='labsmanager.tasks.scheduleBaseTasks',
                            #hook='hooks.print_result',
                            schedule_type=Schedule.CRON,
                            cron = '30 1 * * 1-5',
                            #hook="labsmanager.tasks.sendSuperUserMail",
                            )
        logger.debug("New Schedule :"+str(sch))
    else:
        logger.debug("Already in schedules :"+str(currSch))

# --------------- Clean the audi log based on retention Setting
def clean_auditlog():
    logger.debug("[clean_auditlog] starting ...")
    # get settings for audit log retention
    ret_time=LabsManagerSetting.get_setting("AUDIT_LOG_RETENTION")
    if not ret_time:
        logger.warn("[clean_auditlog] - NO RETENTION TIME DEFINED")
        return
    logger.debug("    - retention time :"+str(ret_time))

    tod = datetime.datetime.now()
    d = datetime.timedelta(days = ret_time)
    a = tod - d
    logger.debug("    -> All history logs will be cleaned before :"+str(a))
    entries = LogEntry.objects.all()
    entries = entries.filter(timestamp__date__lt=a)
    cc=entries.delete()
    logger.debug("    >>> "+str(cc[0])+" entries has been deleted ")
    pass

# --------------- Update all Expense and Fund calculation
def update_all_calculation():
    logger.debug("[update_all_calculation] starting ...")
    fu=Fund.objects.all()
    for f in fu:
        f.calculate(force=True)
# --------------- Generate a report to send
def create_report(*args, **kwargs):
    from staff.models import Employee
    from project.models import Participant

    contextMail={}
    parti=Participant.objects.select_related("employee").filter(Q(project__status=True) &  Q(start_date__lte=timezone.now())  & ( Q(end_date__gte=timezone.now()) | Q(end_date=None))).values("employee").annotate(project_quotity=Sum('quotity'))
    parti=parti.filter(project_quotity__gt=1).values("employee")
    emp=Employee.objects.filter(pk__in=parti)
    contextMail['participant']=emp

    html_content = render_to_string('email/global_report.html',context=contextMail)
    return HttpResponse(html_content, status=200)

def sendMailTest(*args, **kwargs):
    logger.debug("[send_email_test] starting ...")

    logger.debug("  - args :"+str(args))
    logger.debug("  - kwargs :"+str(kwargs))

    User = get_user_model()
    usersObj = User.objects.all()
    emailadress=kwargs["recipient"]

    contextEmail={"users":usersObj,}
    html_content = render_to_string('email/user_list.html',context=contextEmail)

    logger.debug("  - emailadress :"+str(emailadress))
    logger.debug("  - contextEmail :"+str(contextEmail))
    logger.debug("  - html_content :"+str(html_content))
    resp=send_email(
        subject="Test Email",
        body="",
        recipients=emailadress,
        from_email=None,
        html_message=html_content
        )
    logger.debug(">>>>> mail response :"+str(resp))

# ============================================ send mail hook to super user
def sendSuperUserMail(*args, **kwargs):
    logger.debug("[sendSuperUserMail] starting ...")
    User = get_user_model()
    usersMails = User.objects.filter(is_superuser=True).values_list("email", flat=True)

    task=args[0]

    content=f"The task : {task.name} has finished \n"
    content+=f" - fct : {task.func} \n"

    resp=send_email(
        subject="Task Hook Mail",
        body="Test HookMail Body",
        recipients=usersMails,
        from_email=None,
        html_message=content
        )
    logger.debug(">>>>> mail response :"+str(resp))

# test hook
def testHook(*args, **kwargs):
    pass


import constants
import pandas as pd
import datetime

def trim(days_ago):
    n = datetime.datetime.now()
    r = n - datetime.timedelta(days=days_ago)
    delete = 'DELETE FROM {} WHERE TIME < \'' + r.isoformat() + '\';'
    print(delete)
    constants.postgres_engine.execute(delete.format(constants.data_table))

def delete_all():
    n = datetime.datetime.now()
    delete = 'DELETE FROM {};'
    print(delete.format(constants.data_table))
    constants.postgres_engine.execute(delete.format(constants.data_table))
    constants.postgres_engine.execute(delete.format(constants.counts_table))
    constants.postgres_engine.execute(delete.format(constants.locations_table))

def get_between_days_ago(ago1, ago2):
    if ago1 == ago2:
        return

    if ago1 > ago2:
        t = ago1
        ago1 = ago2
        ago2 = t

    since1 = datetime.datetime.now() - datetime.timedelta(days = ago1)
    since2 = datetime.datetime.now() - datetime.timedelta(days = ago2)
    selection = 'SELECT * from {} WHERE TIME BETWEEN \''+since2.isoformat()+'\' AND \''+since1.isoformat()+'\' ORDER BY TIME';
    stored_df = pd.read_sql(
        selection.format(constants.data_table), constants.postgres_engine
    )
    return stored_df

def get_data(platform):
    # In this function, we retrieve the data from postgres using pandas's read_sql method.

    # This data is periodically getting updated via a separate Celery Process in tasks.py.
    # "dataset_table" is the name of the table that we initialized in tasks.py.
    selection = 'SELECT * from {}'
    if platform is not None:
        selection = selection + ' WHERE PLATFORM_CODE=\'' + str(platform) +'\''
    selection = selection + ' ORDER BY TIME;'
    updated_df = pd.read_sql(
        selection.format(constants.data_table), constants.postgres_engine
    )
    return updated_df

def test_selection():
    selection = 'SELECT * from {}'
    selection = selection + ' WHERE PLATFORM_TYPE=\'ARGO\''
    selection = selection + ' ORDER BY TIME LIMIT 10;'
    updated_df = pd.read_sql(
        selection.format(constants.data_table), constants.postgres_engine
    )
    return updated_df

def exists():
    try:
        df = test_selection()
    except Exception as e:
        print(e)
        return False

    if df is not None:
        return True
    else:
        return False

def get_counts():
    # In this function, we retrieve the data from postgres using pandas's read_sql method.

    # This data is periodically getting updated via a separate Celery Process in tasks.py.
    # "dataset_table" is the name of the table that we initialized in tasks.py.
    updated_df = pd.read_sql(
        "SELECT * FROM {};".format(constants.counts_table), constants.postgres_engine
    )
    return updated_df

def get_locations():
    # In this function, we retrieve the data from postgres using pandas's read_sql method.

    # This data is periodically getting updated via a separate Celery Process in tasks.py.
    # "dataset_table" is the name of the table that we initialized in tasks.py.
    updated_df = pd.read_sql(
        "SELECT * FROM {};".format(constants.locations_table), constants.postgres_engine
    )
    return updated_df


import os
import random
import shutil
from argparse import ArgumentParser
from datetime import datetime

import matplotlib.pyplot as plt
import numpy as np
import torch
from pytorch_fid.fid_score import calculate_fid_given_paths
from torch.utils.data import DataLoader
from torch.utils.data.dataset import Subset
from torchvision import transforms
from torchvision.datasets import MNIST
from torchvision.utils import save_image
from tqdm import tqdm, trange

from gan import Generator
from train import concat_noise_label

parser =  ArgumentParser()
parser.add_argument('-g', '--gpu-num', type=int, help='what gpu to use', default=0)
parser.add_argument('-s', '--num_samples', type=int, help="number of samples used for culcurating fid score", default=10000)
parser.add_argument('-c', '--conditional', action="store_true", help="Conditional GAN")
parser.add_argument('-z', '--nz', type=int, help='size of the latent z vector', default=16)
parser.add_argument('-e', '--epoch', type=int, help="upper limit of epoch to calculate FID", default=10000)
parser.add_argument('method', choices=['central', 'fl', 'wafl', 'self'])

def fid(epochs, basedir, real_path, device, method, conditional, num_batch, batch_size, nz, node=0):
    fake_path = os.path.join(basedir, 'fake')
    fid_vals = []

    G = Generator(nz, conditional=conditional).to(device)

    for e in epochs:
        g_path = f'nets/{method}/{"cgan" if conditional else "gan"}/g_e{e}_z{nz}{f"_n{node}" if method in ["wafl", "self"] else "" }.pth'
        G.load_state_dict(torch.load(g_path))

        os.makedirs(fake_path)

        # prepare fake images
        for i in trange(num_batch, desc=f"preparing fake images (epoch {e})"):
            noise = torch.randn(batch_size, nz, 1, 1, device=device)
            fake_label = torch.randint(10, (batch_size,), dtype=torch.long, device=device)
            fake_input_noise = concat_noise_label(noise, fake_label, device) if conditional else noise
            fake_images = G(fake_input_noise)

            for j, (image, label) in enumerate(zip(fake_images, fake_label)):
                save_image([image], os.path.join(fake_path, f'{batch_size * i + j}_{label}.png'))

        fid_val = calculate_fid_given_paths([real_path, fake_path], 256, device, 2048, 8)
        fid_vals.append(fid_val)
        shutil.rmtree(fake_path)

    return fid_vals

def prepare_real(basedir, batch_size, num_batch, device):
    real_path = os.path.join(basedir, 'real')

    os.makedirs(real_path)

    # prepare real images
    dataset = MNIST(root='data', train=True, download=True, transform=transforms.ToTensor())
    indices = random.sample(list(range(60000)), batch_size * num_batch)
    subset = Subset(dataset, indices=indices)
    dataloader = DataLoader(subset, batch_size=batch_size, shuffle=True, num_workers=2)
    for i, (images, labels) in tqdm(enumerate(dataloader), total=num_batch, desc="preparing real images"):
        images = images.to(device)
        labels = labels.to(device)
        for j, (image, label) in enumerate(zip(images, labels)):
            save_image([image], os.path.join(real_path, f'{i}_{label}.png'))

    return real_path

def main():
    args = parser.parse_args()

    conditional = args.conditional
    nz = args.nz
    num_sumples = args.num_samples
    epoch = args.epoch
    method = args.method
    batch_size = 256
    num_batch = num_sumples // batch_size + 1

    device = torch.device(f"cuda:{args.gpu_num}" if torch.cuda.is_available() else "cpu")

    basedir = 'tmp_' + str(datetime.now())
    real_path = prepare_real(basedir, batch_size, num_batch, device)

    epochs = list(range(0, epoch+1, 50 if method == "wafl" else 50 if method == "fl" else 10))
    fid_vals = fid(epochs, basedir, real_path, device, method, conditional, num_batch, batch_size, nz)

    shutil.rmtree(basedir)

    basefilename = f'{method}_{"cgan" if conditional else "gan"}_e{epoch}_z{nz}'
    np.save(os.path.join("graphs", basefilename + ".npy"), np.array([epochs, fid_vals]))

    plt.plot(epochs, fid_vals)
    plt.ylabel('FID score')
    plt.xlabel('Epoch')
    plt.savefig(os.path.join("graphs", basefilename + ".png"), bbox_inches='tight')

if __name__ == '__main__':
    main()


from main.py import Distribution
import math
import matplotlib.pyplot as plt

class Binomial(Distribution):
    def __init__(self, p = 0, n = 1):
        Distribution.__init__(self)
        self.p = p
        self.n = n

    def calculate_mean(self):
        self.mean = self.p * self.n
        return self.mean

    def calculate_stdev(self):
        self.stdev = math.sqrt(self.n * self.p * (1 - self.p))
        return self.stdev

    def replace_stats_with_data(self):
        """Function to calculate p and n from the data set. The function updates the p and n variables of the object."""
        self.n = len(self.data)
        self.p = sum(self.data) / self.n
        self.mean = self.calculate_mean()
        self.stdev = self.calculate_stdev()
        return self.p, self.n

    def plot_bar(self):

        plt.bar(self.data)
        plt.title('Bar Chart of Data')
        plt.xlabel('outcome')
        plt.ylabel('count')

    def pdf(self, k):
        n_f = math.factorial(self.n)
        k_f = math.factorial(k)
        nk_f = math.factorial(self.n-k)
        return (n_f/(k_f * nk_f)) * (self.p**k) * ((1-self.p)**(self.n-k))

    def plot_bar_pdf(self):

        """Function to plot the pdf of the binomial distribution

        Args:
            None

        Returns:
            list: x values for the pdf plot
            list: y values for the pdf plot

        """
        x = []
        y = []
        # calculate the x values to visualize
        for k in range(self.n + 1):
            x.append(k)
            y.append(self.pdf(k))

		# make the plots
        plt.bar(x,y)
        plt.title('Probability of Outcomes')
        plt.xlabel('probability')
        plt.ylabel('count')

        return x, y

    def __add__(self, other):
        try:
            assert self.p == other.p, 'p values are not equal'
        except:
            raise AssertionError

        result = Binomial()
        result.n = self.n + other.n
        result.p = self.p
        return result

    def __repr__(self):
        """Function to output the characteristics of the Binomial instance

        Args:
            None

        Returns:
            string: characteristics of the Binomial object

        """
        return "mean {}, standard deviation {}, p {}, n {}".format(self.mean, self.stdev, self.p, self.n)


import logging
import unittest

from ml_preproc.pipeline.features.clean_input import _map_abbreviations

class CleanInputTest(unittest.TestCase):

  def test_map_abbreviations(self):
    abbreviations = {'str': 'street', 'str.': 'street', 'rd': 'road'}
    result = _map_abbreviations("Main str. 8", abbreviations)
    expected = "Main street 8"
    self.assertEqual(result, expected)

if __name__ == '__main__':
  logging.getLogger().setLevel(logging.INFO)
  unittest.main()


"""
https://leetcode.com/problems/course-schedule/

this is just cycle detection in directed graph
we can just use the direction to mean depends on
[0,1] means 0 depends on 1...

if you say its a cycle detection, where to start the traversal?
topological sort just scan every node and see if it is scanned?

yeah, so when the checked courses reaches n.. it can
"""

from collections import defaultdict, deque
from typing import List

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        adjSets  = defaultdict(set)
        for course, pre in prerequisites:
            adjSets[course].add(pre)

        visited = set()
        def dfs(course, path):
            if course in visited:
                return True
            if course in path:
                return False

            for pre in adjSets[course]:
                if not dfs(pre, path.union({course})):
                    return False

            visited.add(course)
            return True

        for course in range(numCourses):
            if not dfs(course, set()):
                return False

        return True

"""
Runtime: 156 ms, faster than 36.92% of Python3 online submissions for Course Schedule.
Memory Usage: 102.5 MB, less than 5.14% of Python3 online submissions for Course Schedule.

okay.. it was all by memeory
I of course can use =1 to mean in path, =2 to mean processed
"""

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        adjSets = defaultdict(set)
        for course, pre in prerequisites:
            adjSets[course].add(pre)

        # 0: non-visited
        # 1: in path
        # 2: visited
        status = [0] * numCourses

        def dfs(course):
            if status[course] == 2:
                return True
            if status[course] == 1:
                return False

            # add to path
            status[course] = 1
            for pre in adjSets[course]:
                if not dfs(pre):
                    return False

            status[course] = 2
            return True

        for course in range(numCourses):
            if not dfs(course):
                return False

        return True

"""
Runtime: 108 ms, faster than 57.26% of Python3 online submissions for Course Schedule.
Memory Usage: 17.6 MB, less than 9.56% of Python3 online submissions for Course Schedule

let me also do this in bfs way
"""

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        adjSets = defaultdict(set)
        incomingLinks = [0] * numCourses
        for course, pre in prerequisites:
            adjSets[course].add(pre)
            incomingLinks[pre] += 1

        bfsQ = deque()
        for c in range(numCourses):
            if incomingLinks[c] == 0:
                bfsQ.append(c)

        sorted = []
        while bfsQ:
            l = len(bfsQ)
            while l:
                c = bfsQ.popleft()
                sorted.append(c)

                for pre in adjSets[c]:
                    incomingLinks[pre] -= 1
                    if incomingLinks[pre] == 0:
                        bfsQ.append(pre)
                l-=1
        return len(sorted) == numCourses

"""
Runtime: 96 ms, faster than 91.65% of Python3 online submissions for Course Schedule.
Memory Usage: 15.4 MB, less than 87.29% of Python3 online submissions for Course Schedule.
"""

if __name__ == '__main__':
    s = Solution()
    print(s.canFinish(numCourses=2, prerequisites=[[1, 0]]))
    print(s.canFinish(numCourses=2, prerequisites=[[1, 0], [0, 1]]))
    print(s.canFinish(numCourses=6, prerequisites=[[1, 0], [0, 2], [3,4], [5,3]]))


import argparse
import json
from utils.params import FT_Configer
from finetune.trainer import Trainer

def load(json_file):

    with open(json_file, 'r') as f:
        return json.load(f)

def main():

    parser = argparse.ArgumentParser(description="Argparser")
    parser.add_argument("--params",
                        default={},
                        help="JSON dict of model hyperparameters.")
    args = parser.parse_args()

    model_args = FT_Configer(load(args.params))

    trainer = Trainer(model_args)
    trainer.run_finetune()

if __name__ == '__main__':

    main()


from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from variaveis import DATABASE

app = Flask(__name__)

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE

db = SQLAlchemy(app)

db.init_app

class Awa_Foto(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    file_id = db.Column(db.String(255))
    chat_id = db.Column(db.String(255))


import sqlite3
from flask import current_app

def get_db():
    return current_app._get_current_object().get_db()

# Database function
def insert_user(firstname, lastname, emailuser):
    db = get_db()
    cursor = db.cursor()
    cursor.execute("INSERT INTO users (first_name, last_name, email) VALUES (?, ?, ? )", (firstname, lastname, emailuser))
    db.commit()
    return cursor.lastrowid

def insert_feedback(user_id, messageuser):
    db = get_db()
    cursor = db.cursor()
    cursor.execute("INSERT INTO feedback (user_id, message) VALUES (?, ?)", (user_id, messageuser))
    db.commit()

def get_user_by_email(emailuser):
    db = get_db()
    cursor = db.cursor()
    cursor.execute("SELECT * FROM users WHERE email = ?", (emailuser,))
    return cursor.fetchone()


### Test scales with easy OCR
def detect_Number(List_with_Images):
  # Input Images, best case closely cropped to image
  # Output List of Numbers and letters

  import easyocr
  reader = easyocr.Reader(['ch_sim','en']) # this needs to run only once to load the model into memory

  Results = []
  for x in List_with_Images:
    try:
      result = reader.readtext(x, detail = 0) ## Predict
      Results.append(result)
    except:
      Results.append(0)

def CropImage(Images):
  # takes as list of images
  # outputs a closley cropped list of images
  # based on CvCanny structure

  counter = 0
  List_of_crops = []
  for x in l:
    try:
      img = x

      Can = cv2.Canny(img,300,400, L2gradient = False)
      # The whiskers can confuse the process we have which is we already crop the far sides

      height, width = Can.shape
      Cropped_Can = Can[:, 10:width-10]
      Cropped_img_temp = img[:, 10:width-10]

      row_scan = np.sum(Cropped_Can, axis = 0)/255 ### Scans the image in rows
      coloumn_scan = np.sum(Cropped_Can, axis = 1)/255 ### Scans it in coloumns

      # Get the standard value for the scan
      reduced_column = coloumn_scan - coloumn_scan[0]

      # Find the index of the first number greater than 3
      # This means we exclude all artifacts caused by jittery lines
      # smaller than 2 + 3 (5) Pixels
      first_index_y = np.argmax(reduced_column > 3)

      # Find the index of the last number greater than 3
      last_index_y = np.max(np.argwhere(reduced_column > 3))

      reduced_row = row_scan - row_scan[0]

      # Find the index of the first number greater than 3
      # This means we exclude all artifacts caused by jittery lines
      # smaller than 2 + 3 (5) Pixels
      first_index_x = np.argmax(reduced_row > 3)

      # Find the index of the last number greater than 3
      last_index_x = np.max(np.argwhere(reduced_row > 3))

      if first_index_y-5 < 0:
        first_index_y = 5
      if first_index_x-5 < 0:
        first_index_x = 5

      print(counter, first_index_x-5,last_index_x+5,first_index_y-5,last_index_y+5, img.shape)
      img_crop = Cropped_img_temp[first_index_y-5:last_index_y+5,first_index_x-5:last_index_x+5]
      counter += 1

      ## Very small and pixelated values have to be resized
      ## If the image is less than 100 pixels in any dimension
      ## We resize it with Inter cubic
      ## We resize according to the smaller axis

      height, width = img_crop.shape
      print(img_crop.shape)
      ## Check size
      if (height < 100) or (width < 100):
        # Calculate scaling factors
        scale_height = 100/height
        scale_width = 100/width

        # Choose the bigger factor
        if scale_height > scale_width:
          scaling_factor = scale_height
        else:
          scaling_factor = scale_width
        print(scaling_factor,height*scaling_factor)
        # Scale up
        img_crop = cv2.resize(img_crop, (int(width*scaling_factor),int(height*scaling_factor)), interpolation = cv2.INTER_CUBIC)

      List_of_crops.append(img_crop)
    except:
      List_of_crops.append(img)
      counter += 1

  return Krops

def Sortlist(String_of_numbers):
  ### Drop entrys except numbers can cope with ['2','mm'] or ['2mm'] or ['2'], [].
  ### Input list of lists.
  ### Output float values found in string
  import re

  numbers_only = [float(re.findall(r'\d+(?:\.\d+)?', entry[0])[0]) if entry and entry[0] and re.findall(r'\d+(?:\.\d+)?', entry[0]) else 0 for entry in String_of_numbers]

  return numbers_only


from selenium import webdriver
import time

# Launch Firefox browser
driver = webdriver.Firefox()

# Open custom map URL
driver.get("https://webcam-live.creaweather.fr/Meteo60.html")
time.sleep(5)
# Execute JavaScript to get all pins on the map
pins = driver.execute_script("return document.querySelectorAll('.gm-style-iw-d');")

# Iterate through each pin
for pin in pins:
    # Click on the pin to open the popup window
    driver.execute_script("arguments[0].click();", pin)

    # Get the link attached to the pin using JavaScript
    link = driver.execute_script("return document.querySelector('.gm-style-iw-e a').href;")

    # Print the link
    print(link)

    # Close the popup window
    driver.execute_script("document.querySelector('.gm-style-iw-i-c').click();")

# Close the browser


#!/usr/bin/python
#-*-coding:utf-8
import os
import sys
import pdb
import re
import math
from pymongo import MongoClient
import pymongo
import time
import json
import time
import pdb
def load_model(f_name):
    ifp = open(f_name, 'rb')
    return eval(ifp.read())
# def viterbi(obs, states, start_p, trans_p, emit_p):
#     V = [{}] #tabular
#     path = {}
#     for y in states: #init
#         V[0][y] = start_p[y] * emit_p[y].get(obs[0],0)
#         path[y] = [y]
#     for t in range(1,len(obs)):
#         V.append({})
#         newpath = {}
#         for y in states:
#             (prob,state ) = max([(V[t-1][y0] * trans_p[y0].get(y,0) * emit_p[y].get(obs[t],0) ,y0) for y0 in states if V[t-1][y0]>0])
#             V[t][y] =prob
#             newpath[y] = path[state] + [y]
#         path = newpath
#     (prob, state) = max([(V[len(obs) - 1][y], y) for y in states])
#     return (prob, path[state])

def viterbi(obs,states,prob_start,prob_trans,prob_emit):
	"""
	obs=observed values
	states=['B','M','E','S'] (Begin, Middle, End, Single word)
	prob_start=probability of start
	prob_trans=state-transition matrix
	Status(i)＝f({Status(i-1), Status(i-2), Status(i-3), ... Status(i - n)})
	prob_emit＝emission probability EmitProbMatrix
	P(Observed[i], Status[j]) = P(Status[j]) * P(Observed[i]|Status[j])
	"""
	v=[{}]
	path={}
	#init the start node
	for state in states:
		if obs[0] in prob_emit[state]:
			v[0][state]=math.exp(prob_start[state]*prob_emit[state][obs[0]])
		else:
			v[0][state]=0
		path[state]=[state]
	#print(path)
	# for all
	for i in range(1,len(obs)):
		v.append({})
		newpath={}
		for state in states:
			est_list=[]
			for pstate in [s for s in states if v[i-1][s]>0]:
				if obs[i] in prob_emit[state]:
					est_list.append((v[i-1][pstate]*math.exp(prob_trans[pstate][state]*prob_emit[state][obs[i]]),pstate))
				else:
					est_list.append((v[i-1][pstate],pstate))
			p,ps=max([(p,ps) for (p,ps) in est_list])
			v[i][state]=p
			newpath[state]= path[ps] + [state]
		path=newpath
	(prob, state) = max([(v[len(obs) - 1][state], state) for state in states])
	try:
		if prob_emit['M'][obs[-1]]> prob_emit['S'][obs[-1]]:
			(prob, state) = max([(v[len(obs) - 1][state], state) for state in ('S','E')])
	except:
	 	pass
	raw=""
	if path:
		for i,char in enumerate(obs):
			#Sprint(path)
			if path[state][i]=="E":
				raw+=char+"  "
			elif path[state][i]=="S":
				raw+=char+"  "
			else:
				raw+=char

		return (raw,"".join(path[state]))
	else:
		return ("","")
def cut(sentence):
    #pdb.set_trace()
    prob, pos_list =  viterbi(sentence,('B','M','E','S'), prob_start, prob_trans, prob_emit)
    return (prob,pos_list)

if __name__ == "__main__":
    prob_start = load_model("/home/john/pythonspace/sina_crawler/weibo_crawler/FC/HMM/prob_start.py")
    prob_trans = load_model("/home/john/pythonspace/sina_crawler/weibo_crawler/FC/HMM/prob_trans.py")
    prob_emit = load_model("/home/john/pythonspace/sina_crawler/weibo_crawler/FC/HMM/prob_emit.py")

    db_uri = "mongodb://labUser:aaaaaa@localhost:27017/?authSource=blog"
    db_name = "blog"
    xlient = MongoClient(db_uri)
    db = xlient[db_name]
    print(db)

    myiter = db.blog.find({"blog.content.text_content":{"$exists":1}},{"blog.content.text_content":1})
    length = 1000
    print("##")
    print(myiter[7]['blog']['content']['text_content'])
    i=0
    while i<length:
        print(i)
        test_str = myiter[i]['blog']['content']['text_content'].strip().strip('#')
        # print(test_str)
        prob,pos_list = cut(test_str)
        print(prob)
        # print(pos_list)
        i=i+1

    test_str = "在党的十二届六中全会通过的《关于社会主义精神文明建设指导方针的决议》指引下。"
    # print(test_str)
    # prob,pos_list = cut(test_str)
    # print(prob)
    # test_str = "微卖 快来围观我的“微卖”小店，我又进了不少新货 http://t.cn/R4U3Dp1"
    # test_str = myiter[1000]['blog']['content']['text_content']
    # print(test_str)

    print(test_str)
    prob,pos_list = cut(test_str)
    print(prob)
    print(pos_list)

    # while i:
    #     i=i-1
    #     test_str = myiter[1000]['blog']['content']['text_content'].strip()
    #     # test_str    = "新华网东京电记者吴谷丰据日本共同社28日报道，日本行政改革担当大臣稻田朋美当天参拜了供奉有二战甲级战犯牌位的靖国神社。她成为第四个参拜靖国神社的安倍内阁成员。靖国神社位于东京千代田区，供奉有包括东条英机在内的14名第二次世界大战甲级战犯的牌位。长期以来，日本部分政客参拜靖国神社，导致日本与中国、韩国等亚洲国家关系"
    #     prob,pos_list = cut(test_str)
    #     print("HMM segment:",prob)
    # print(pos_list)
    #print(type(prob))


from http import HTTPStatus
from typing import Dict, Iterable, List, Mapping, Optional, Union

from amundsen_common.entity.resource_type import ResourceType
from amundsen_common.models.dashboard import DashboardSummarySchema
from amundsen_common.models.table import TableSummarySchema
from flasgger import swag_from
from flask import request
from flask_restful import Resource

from metadata_service.proxy import get_proxy_client

class PopularResourcesAPI(Resource):
    """
    PopularResources API
    """

    def __init__(self) -> None:
        self.client = get_proxy_client()

    @swag_from('swagger_doc/popular_resources_get.yml')
    def get(self, user_id: Optional[str] = None) -> Iterable[Union[Mapping, int, None]]:
        limit = request.args.get('limit', 10, type=int)
        resource_types = request.args.get('types', 'table', type=str)
        resource_types = resource_types.split(',')
        popular_resources: Dict[str, List] = self.client.get_popular_resources(
            num_entries=limit,
            resource_types=resource_types,
            user_id=user_id)

        response: dict = dict()
        response[ResourceType.Table.name] = TableSummarySchema().dump(
            popular_resources.get(ResourceType.Table.name),
            many=True
        )
        response[ResourceType.Dashboard.name] = DashboardSummarySchema().dump(
            popular_resources.get(ResourceType.Dashboard.name),
            many=True
        )
        return response, HTTPStatus.OK


from flask import Flask
from flask_cors import CORS # comm in deployment
import socket
import requests
from requests.auth import HTTPBasicAuth
HEADER = 64
PORT = 5050
PORT_2 = 5678
MSG_SIZE = 2048
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = '192.168.86.152'  # Run server first
ADDR = (SERVER, PORT)
ADDR_2 = (SERVER, PORT_2)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)

client2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client2.connect(ADDR_2)

app = Flask(__name__)

CORS(app) # comm in deployment

@app.route("/image/<nflLogo>")
def get_image(nflLogo):
    logoLink = "https://us-central1-osu-project-342203.cloudfunctions.net/image-generator?query=" + nflLogo + "+nfl&size=small"
    image = requests.get(logoLink, auth = HTTPBasicAuth('admin_T42', 'admin_T42'))
    text_to_split = image.text.split("\"")
    return text_to_split[3]

def send_request(nflTeam):
    msg = nflTeam.encode(FORMAT)
    client.send(msg)
    paragraph = client.recv(2048).decode(FORMAT)
    return paragraph

@app.route("/teamdata/<nflTeam>")
def get_team_info(nflTeam):
    nflTeamCleaned = nflTeam.replace("+", " ")
    new_paragraph = send_request(nflTeamCleaned)
    return new_paragraph

def send_map_request(coordinates):
    message = coordinates.encode(FORMAT)
    msg_len = len(coordinates)
    send_length = str(msg_len).encode(FORMAT)
    send_length += b' ' * (MSG_SIZE - len(send_length))
    client2.send(send_length)
    client2.send(message)
    img_url = client2.recv(MSG_SIZE).decode(FORMAT)
    return img_url

@app.route("/homeMap/<coordinates>")
def get_team_map(coordinates):
    new_map = send_map_request(coordinates)
    return new_map


from fastapi import FastAPI, Request, Depends, BackgroundTasks, background
from fastapi.templating import Jinja2Templates
from sqlalchemy import engine
import dbmodels
import yfinance as yf
from sqlalchemy.orm import Session
from database import SessionLocal, engine
from pydantic import BaseModel
from dbmodels import StockData
from twilio.rest import Client

account = "###########"
token = "###########"
client = Client(account,token)

client.region = 'us1'
client.edge = 'ashburn'

app = FastAPI()

dbmodels.Base.metadata.create_all(bind=engine)

templates = Jinja2Templates(directory="templates")

class StockReq(BaseModel):
    stockname: str

def get_db():
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()

def yahoo_data_fetch(id: int):
    db = SessionLocal()
    stock_data = db.query(StockData).filter(StockData.id == id).first()
    yahoo_stock_data = yf.Ticker(stock_data.stockname)
    stock_data.ma200 = yahoo_stock_data.info["twoHundredDayAverage"]
    stock_data.ma50 = yahoo_stock_data.info["fiftyDayAverage"]
    stock_data.price = yahoo_stock_data.info["previousClose"]
    stock_data.forward_pe = yahoo_stock_data.info["forwardPE"]
    stock_data.forward_eps = yahoo_stock_data.info["forwardEps"]
    if stock_data.dividend_yield is not None:
        stock_data.dividend_yield = yahoo_stock_data.info["dividendYield"] * 100
    db.add(stock_data)
    db.commit()

@app.get("/")
def dashboard(request: Request, forward_pe = None, dividend_yield = None, ma50 = None, ma200 = None, db: Session = Depends(get_db)):
    ''''''''''
    displays the dashboard
    '''''''''''
    Stocks = db.query(StockData)
    if forward_pe:
        Stocks = Stocks.filter(StockData.forward_pe < forward_pe)
    if dividend_yield:
        Stocks = Stocks.filter(StockData.dividend_yield > dividend_yield)
    if ma50:
        Stocks = Stocks.filter(StockData.price > ma50)
    if dividend_yield:
        Stocks = Stocks.filter(StockData.price > ma200)

    return templates.TemplateResponse("dashboard.html",{
        "request": request,
        "Stocks": Stocks,
        "dividend_yield": dividend_yield,
        "forward_pe": forward_pe,
        "ma200": ma200,
        "ma50": ma50
    })

@app.post("/stockname")
async def stockCreate(stock_req: StockReq, task_background : BackgroundTasks,db: Session = Depends(get_db)):
    ''''''''''
    stock creation
    '''''''''''
    stock = StockData()
    stock.stockname = stock_req.stockname
    db.add(stock)
    db.commit()
    task_background.add_task(yahoo_data_fetch, stock.id)
    message = client.messages.create(to= "###########", from_ = "###########", body = "New Stock " + stock.stockname + " added")
    return{
        "code": "success",
        "message": "stock has been created"
    }


import matplotlib.pyplot as plt
import time
import os
import platform
import sys
import copy
import numpy as np

platform = platform.system()

args = sys.argv

if "--size" in args:
    size = int(args[args.index("--size")+1])
    a, b = size, size
else:
    a,b = 40,40
if "--wait" in args:
    wait = int(args[args.index("--wait")+1])
else:
    wait = 0.05

print("Using board size {0}x{1} and wait value of {2}s.".format(a, b, wait))

board1 = [0] * a
for i in range(a):
    board1[i] = [0] * b
gen = 0
pop = 0
gens = []
pops = []
print("\nStructure each input like this: x/y.")
print("\nFor example: 10/10 would set (10,10) to be alive.")
print("\nFor multiple inputs, structure it like this: x/y;x/y;x/y")
print("\nREMEMBER: The matrix starts at index 0, which means the tile or cell \n          in the top left would be 0/0")
print("\nYou can also enter \"random/probability of 1\" to get a random board.\n")
inp = input("? ")
if inp.startswith("random"):
    prob1 = float(inp[inp.find("/")+1:len(inp)])
    prob2 = 1 - prob1
    board1 = np.random.choice([1,0], a*b, p=[prob1, prob2]).reshape(a, b)
else:
    inps = inp.split(";")
    for inp in inps:
        if inp == '':
            break;
        inp_split = inp.split("/")
        x = int(inp_split[0])
        y = int(inp_split[1])
        #useless code because I was an idiot and forgot .split() was a thing
        #but I spent too much time thinking about it so I don't want to remove it
        #x = int(inp[:inp.find("/")]) #finds the first /
        #y = int(inp[inp.find("/")+1:inp.find("/",inp.find("/")+1)]) #finds the first / after the first /, which is the second /
        #state = int(inp[inp.find("/",inp.find("/")+1)+1:(inp.find("/",inp.find("/",inp.find("/")+1))+1)+(len(inp)-inp.find("/",inp.find("/")+1)+1)]) #finds the first / after the first / after the first /, or the first / after the second /, or just the third /
        board1[y][x] = 1
        print("(" + str(x) + "," + str(y) + ") is now " + str(1))
iterations = int(input("\nHow many generations would you like to run? "))
for gen in range(1, iterations+1):
    board2 = copy.deepcopy(board1)
    boardstr = ""
    if platform == "Windows":
        os.system('cls')
    else:
        os.system('clear')
    pop = 0
    for y in range(0, a):
        for x in range(0,b):
            if board1[y][x] == 1:
                pop += 1
            if board1[y][x] == 1:
                boardstr += "██"
            else:
                boardstr += "  "
        boardstr += "\n"
    print(boardstr)
    print("GEN: " + str(gen))
    print("POP: " + str(pop))
    gens.append(gen)
    pops.append(pop)
    for y in range(0, a):
        for x in range(0,b):
            total = int(board1[(y-1) % a][(x-1) % b] + board1[(y-1) % a][x % b] + board1[(y-1) % a][(x+1) % b] +
                        board1[y % a][(x-1) % b] + board1[y % a][(x+1) % b] +
                        board1[(y+1) % a][(x-1) % b] + board1[(y+1) % a][x % b] + board1[(y+1) % a][(x+1) % b])
            if board1[y][x] == 1:
                if (total < 2) or (total > 3):
                    board2[y][x] = 0
            else:
                if total == 3:
                    board2[y][x] = 1
            #print(str(b*(y)+x) + " : " + str(total) + " : " + str(board1[y][x]) + " : " + str(board2[y][x]))
            #time.sleep(0.1)
    board1 = copy.deepcopy(board2)
    #print(board1)
    #R-Pentomino: 10/10;11/10;9/11;10/11;10/12
    time.sleep(wait)

print("\nDone!\n")

plt.plot(gens, pops)
plt.show()


from typing import List

class Solution:
    def rob(self, nums: List[int]) -> int:
        if not nums:
            return 0
        if len(nums) == 1:
            return nums[0]
        return max(self.rob_range(nums[1:]), self.rob_range(nums[: len(nums) - 1]))

    def rob_range(self, nums: List[int]) -> int:
        cur = 0
        pre = 0
        for i in range(len(nums) - 1, -1, -1):
            temp = cur
            cur = max(cur, pre + nums[i])
            pre = temp

        return cur