""" Created 09/01/2021 by Stelian Stoian.
    Implements the bencode encoder.
"""
from collections import OrderedDict

from .indicators import TOKEN_END, TOKEN_INTEGER, TOKEN_DICT, TOKEN_LIST, TOKEN_STRING_SEPARATOR

class Encoder:
    """ Responsible for encoding a python object into the
        corresponding bencode. (int, str, list, ordered dict)
    """

    @staticmethod
    def encode(value_to_encode) -> bytes:
        """ The encode method encodes the object passed to the instance
            of the class to bytes type bencode.

            :param: value_to_encode The object that we want to encode. Can be
            int, str, list, OrderedDict.
        """
        if isinstance(value_to_encode, int):
            return Encoder._encode_int(value_to_encode)
        elif isinstance(value_to_encode, bytes):
            return Encoder._encode_string(value_to_encode)
        elif isinstance(value_to_encode, list):
            return Encoder._encode_list(value_to_encode)
        elif isinstance(value_to_encode, OrderedDict):
            return Encoder._encode_dict(value_to_encode)
        else:
            raise ValueError("Type {} of value_to_encode not supported".format(str(type(value_to_encode))))

    @staticmethod
    def _encode_int(value):
        """ Returns the bencode for the given int.

            Format of the int bencode is i<value>e. Ex: i123e.
            This is returned as bytes type.
        """
        res = TOKEN_INTEGER + Encoder._convert_int_to_byte_string(value) + TOKEN_END
        return res

    @staticmethod
    def _encode_string(value):
        """ Returns the bencode for given str.

            Format of the str bencode is <length>:<string>.
            This is returned as bytes type.
        """
        length = len(value)
        return Encoder._convert_int_to_byte_string(length) + TOKEN_STRING_SEPARATOR + value

    @staticmethod
    def _encode_list(list_value):
        """ Returns the bencode for the given list. """
        res = TOKEN_LIST
        new_elem = None
        for el in list_value:
            new_elem = Encoder._convert_base(el)
            res += new_elem
        return res + b'e'

    @staticmethod
    def _encode_dict(dict_value):
        """ Returns the bencode for the given dict. """
        res = TOKEN_DICT
        for key in dict_value:
            new_key = Encoder._convert_base(key)
            new_object = Encoder._convert_base(dict_value[key])
            res += new_key + new_object
        return res + b'e'

    @staticmethod
    def _convert_base(value):
        """ Encode a basic value (int or string) to bencode.
            Utility function for converting basic types to their respective
            encoding.
            Raises ValueError if the wrong type is passed for value.
        """
        if isinstance(value, int):
            return Encoder._encode_int(value)
        elif isinstance(value, bytes):
            return Encoder._encode_string(value)
        else:
            raise ValueError("Value must be str or int but {} was passed".format(str(type(value))))

    @staticmethod
    def _convert_int_to_byte_string(value_to_convert):
        """ Convert an int value to a bytes string. """
        if not isinstance(value_to_convert, int):
            raise ValueError("value_to_convert must be int")
        cifre = []
        while value_to_convert:
            cifre.append(value_to_convert % 10)
            value_to_convert //= 10
        cifre.reverse()
        res = b''
        for el in cifre:
            if el == 1:
                res += b'1'
            elif el == 2:
                res += b'2'
            elif el == 3:
                res += b'3'
            elif el == 4:
                res += b'4'
            elif el == 5:
                res += b'5'
            elif el == 6:
                res += b'6'
            elif el == 7:
                res += b'7'
            elif el == 8:
                res += b'8'
            elif el == 9:
                res += b'9'
        return res


# -------------------------------------------------------------------------
# AUTHOR: Joey Cindass
# FILENAME: naive_bayes.py
# SPECIFICATION: Using naive bayes, predict if someone will play tennis given weather conditions
# FOR: CS 4210- Assignment #2
# TIME SPENT: 4 hour
# -----------------------------------------------------------*/

# IMPORTANT NOTE: DO NOT USE ANY ADVANCED PYTHON LIBRARY TO COMPLETE THIS CODE SUCH AS numpy OR pandas. You have to
# work here only with standard dictionaries, lists, and arrays

# importing some Python libraries
from sklearn.naive_bayes import GaussianNB
import csv

# reading the training data in a csv file
# --> add your Python code here
db = []
with open('weather_training.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for i, row in enumerate(reader):
        if i > 0:  # skipping the header
            db.append(row[1:])

# transform the original training features to numbers and add them to the 4D array X.
# For instance Sunny = 1, Overcast = 2, Rain = 3, so X = [[3, 1, 1, 2], [1, 3, 2, 2], ...]]
# --> add your Python code here
# X =
X = []
for i in range(len(db)):
    X_temp = []
    for j in range(len(db[i])):
        ones = ['Sunny', 'Cool', 'Normal', 'Weak']
        twos = ['Overcast', 'Mild', 'High', 'Strong']
        threes = ['Rain', 'Hot']
        if db[i][j] in ones:
            X_temp.append(1)
        elif db[i][j] in twos:
            X_temp.append(2)
        elif db[i][j] in threes:
            X_temp.append(3)
    X.append(X_temp)

# transform the original training classes to numbers and add them to the vector Y.
# For instance Yes = 1, No = 2, so Y = [1, 1, 2, 2, ...]
# --> add your Python code here
# Y =
Y = []
for i in range(len(db)):
    if db[i][-1] == "Yes":
        Y.append(1)
    else:
        Y.append(0)

# fitting the naive bayes to the data
clf = GaussianNB()
clf.fit(X, Y)

# reading the test data in a csv file
# --> add your Python code here
db2 = []
with open('weather_test.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for i, row in enumerate(reader):
        if i > 0:  # skipping the header
            db2.append(row[1:])

# printing the header os the solution
print("Day Outlook  Temperature Humidity Wind    PlayTennis Confidence")

# use your test samples to make probabilistic predictions. For instance: clf.predict_proba([[3, 1, 2, 1]])[0]
# --> add your Python code here

X2 = []
for i in range(len(db2)):
    X2_temp = []
    for j in range(len(db2[i])):
        ones = ['Sunny', 'Cool', 'Normal', 'Weak']
        twos = ['Overcast', 'Mild', 'High', 'Strong']
        threes = ['Rain', 'Hot']
        if db2[i][j] in ones:
            X2_temp.append(1)
        elif db2[i][j] in twos:
            X2_temp.append(2)
        elif db2[i][j] in threes:
            X2_temp.append(3)
    X2.append(X2_temp)

conf_list = []
pred_list = []
for instance in X2:
    confidence = clf.predict_proba([instance])[0]
    pred = clf.predict([instance])
    if pred == 1:
        pred_list.append("Yes")
    else:
        pred_list.append("No")
    conf_list.append(confidence.max())

for i in range(len(db2)):
    if conf_list[i] >= 0.75:
        print(f"D{i+15} {db2[i][0]:8} {db2[i][1]:11} {db2[i][2]:8} {db2[i][3]:4}    {pred_list[i]} {conf_list[i]:25}")


import telebot

bot = telebot.TeleBot("5959592035:AAHFfTbtlGp6v59F4xK9DpO70O3eE2fAiZg", parse_mode=None)
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
	bot.reply_to(message, "treasure, how was your day?")

bot.infinity_polling()


import uuid
from enum import Enum

from sqlalchemy import Column, ForeignKey, Index, String
from sqlalchemy.dialects.postgresql import UUID, ENUM

from models.postgres import Base, SchemaInDb
from models.postgres.base import UUIDMixin, CreatedTimeMixin

class OAuthProviderEnum(str, Enum):
    google: str = "google"
    yandex: str = "yandex"

class UserOAuthAccount(Base, UUIDMixin, CreatedTimeMixin):
    __tablename__ = "user_oauth_account"
    __mapper_args__ = {"eager_defaults": True}

    user_id = Column(UUID(as_uuid=True), ForeignKey("user.id", ondelete="CASCADE"), nullable=False)
    oauth_provider_name = Column(ENUM(OAuthProviderEnum), nullable=False)
    oauth_account_id = Column(String(60), nullable=False)

    __table_args__ = (
        Index("ix_provider_account", "oauth_provider_name", "oauth_account_id", unique=True),
        Index("ix_user_provider", "user_id", "oauth_provider_name", unique=True),
    )

class UserOAuthAccountSchema(SchemaInDb):
    user_id: uuid.UUID
    oauth_provider_name: str | OAuthProviderEnum
    oauth_account_id: str


from typing import Optional, List

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    # Time Complexity: O(n)
    # Space Complexity: O(1)
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
        left_head = ListNode(0)
        left_cursor = left_head

        right_head = ListNode(0)
        right_cursor = right_head

        while head:
            if head.val < x:
                left_cursor.next = head
                left_cursor = left_cursor.next
            else:
                right_cursor.next = head
                right_cursor = right_cursor.next

            head = head.next

        left_cursor.next = right_head.next
        right_cursor.next = None

        return left_head.next

def getVals(head: Optional[ListNode]) -> List[int]:
    res = []

    while head:
        res.append(head.val)
        head = head.next

    return res

if __name__ == '__main__':
    s = Solution()

    list = [ListNode(1), ListNode(4), ListNode(3), ListNode(2), ListNode(5), ListNode(2)]
    for i in range(0, len(list)-1):
        list[i].next = list[i+1]
    assert getVals(s.partition(list[0], 3)) == [1,2,2,4,3,5]

    list = [ListNode(2), ListNode(1)]
    for i in range(0, len(list)-1):
        list[i].next = list[i+1]
    assert getVals(s.partition(list[0], 2)) == [1,2]


import fuzzy

from src.helpers.spell_check_helper import spell_check_entity_name
from src.helpers.spell_check_helper import word_suggestion

#################
## POC POC POC ##
#################

"""
Rockwell Automation Technolgies, Inc. - does not resolve
Rockwell Automation Technologies Inc. - resolved via em and oc,
    ENTITY-MATCH: {'keyword': 'Rockwell Automation Technologies Inc.',
    'org_name': 'Rockwell Automation Technologies Inc', 'confidence': '0.92', 'perm_id': 5000187552,
    'is_company_score': '1.0'}

Pioneer Hi-Bred Internationak, Inc. - does not resolve
Pioneer Hi-Bred International Inc. - resolved via em and oc,
    ENTITY-MATCH: {'keyword': 'Pioneer Hi-Bred International Inc.',
    'org_name': 'Pioneer Hi-Bred International Inc', 'confidence': '0.92', 'perm_id': 4295911139,
    'is_company_score': '1.0'}

Honeywell Internations, Inc. - does not resolve
Honeywell International Inc. - resolved via em and oc,
    ENTITY-MATCH: {'keyword': 'Honeywell International Inc.',
    'org_name': 'Honeywell International Inc', 'confidence': '0.92', 'perm_id': 4295912155,
    'is_company_score': '1.0'}

"""

def __internal_assert(value, expected_value):
    try:
        assert value == expected_value
    except AssertionError:
        print(f'\nASSERTION FAILED: [{value}] != [{expected_value}]\n')

def __test_words(print_output=False):
    input_words = {'TECHNOLOGU': 'Technology', 'copmany': 'Company', 'INCORPORRATED': 'Incorporated',
                   'systemmss': 'Systems', 'orporration': 'Corporation', 'llimite': 'Limited', 'rresarch': 'Research',
                   'Groups': 'Groups', 'Products': 'Products', 'Internationak': 'International', 'Limiteds': 'Limited',
                   'SOLUTIONS': 'Solutions', 'Enterprises': 'Enterprises', 'Developmentt': 'Development',
                   'Industriess': 'Industries', 'Nationals': 'National', 'Technologie': 'Technologies',
                   'Holdngss': 'Holdings', 'konsultan': 'Consultant'}
    for (keyword, expected_word) in input_words.items():
        (suggestion, distance) = word_suggestion(keyword)
        if print_output:
            print(f'Input:[{keyword}]\t\t\t\tSuggestion:[{suggestion}]\t\t\t\tDistance:[{distance}]')
        __internal_assert(suggestion, expected_word)

def __test_entities(print_output=False):
    input_entities = {'Hewlett Packarrd Develoment': 'Hewlett Packarrd Development',
                      'Pioneer Hi-Bred Internationak, Inc.': 'Pioneer Hi-Bred International, Inc.',
                      'Technologias Avanzadas Inspiralia S.L.': 'Technologies Avanzadas Inspiralia S.L.',
                      'Microsoft Technology': 'Microsoft Technology',
                      'Rockwell Automation Technolgies, Inc.': 'Rockwell Automation Technologies, Inc.',
                      'Honeywell Internations, Inc.': 'Honeywell International, Inc.'}
    for (entity_name, expected_word) in input_entities.items():
        corrected_word = spell_check_entity_name(entity_name)
        __internal_assert(corrected_word, expected_word)
        if print_output:
            print(f'Input:[{entity_name}], Corrected:[{corrected_word}]')

def __test_false_positives(print_output=False, apply_phonetic_check=False):
    input_words = {'United': 'Limited'}
    for (keyword, expected_word) in input_words.items():
        (suggestion, distance) = word_suggestion(keyword, max_distance=3, apply_phonetic_check=apply_phonetic_check)
        sug_score = fuzzy.nysiis(suggestion)
        orig_score = fuzzy.nysiis(keyword)
        if print_output:
            print(f'Input:[{keyword}] [{orig_score}]\t\t\t\t'
                  f'Suggestion:[{suggestion}][{sug_score}]\t\t\t\tDistance:[{distance}]')
        __internal_assert(suggestion, expected_word)

def main():
    __test_entities(print_output=True)
    __test_words(print_output=True)
    __test_false_positives(print_output=True)
    __test_false_positives(print_output=True, apply_phonetic_check=True)

main()


from typing import Any, Callable, Mapping, Sequence, Tuple, Type

from pydantic import BaseModel
from pydantic.fields import FieldInfo

from . import utils
from .validation import create_func_model

__all__: Sequence[str] = (
    "create_model_cls",
    "create_model",
    "bind_arguments",
)

def create_model_cls(
    func: Callable, fields: Mapping[str, Tuple[Any, FieldInfo]]
) -> Type[BaseModel]:
    class Config:
        allow_population_by_field_name: bool = True
        arbitrary_types_allowed: bool = True

    return create_func_model(func, fields, config=Config)

def create_model(
    func: Callable,
    fields: Mapping[str, Tuple[Any, FieldInfo]],
    arguments: Mapping[str, Any],
) -> BaseModel:
    model_cls: Type[BaseModel] = create_model_cls(func, fields)

    return model_cls(**arguments)

def bind_arguments(
    func: Callable, args: Tuple[Any, ...], kwargs: Mapping[str, Any]
) -> Mapping[str, Any]:
    arguments: Mapping[str, Any] = utils.bind_arguments(func, args, kwargs)

    return {
        key: value
        for key, value in arguments.items()
        if not isinstance(value, FieldInfo)
    }


import contextlib
import io
import os
import pathlib
import tempfile
import warnings
import zipfile

import pytest
import tomli_w

from click.testing import CliRunner

"""

In your tests:

  def test_foo(cli_runner):
    r = cli_runner.invoke(mycli, ["mycommand"])
    assert r.exit_code == 0

In `some_command()`, add:

  @cli.command()
  def mycommand():
    import pytest; pytest.set_trace()

Then run via:

  $ pytest -sv --pdb-trace ...

Note any tests checking CliRunner stdout/stderr values will fail when
--pdb-trace is set.

"""

def pytest_addoption(parser) -> None:
    parser.addoption(
        "--pdb-trace",
        action="store_true",
        default=False,
        help="Allow calling pytest.set_trace() in Click's CliRunner",
    )

class MyCliRunner(CliRunner):
    def __init__(self, *args, in_pdb=False, **kwargs) -> None:
        self._in_pdb = in_pdb
        super().__init__(*args, **kwargs)

    def invoke(self, cli, args=None, **kwargs):
        params = kwargs.copy()
        if self._in_pdb:
            params["catch_exceptions"] = False

        return super().invoke(cli, args=args, **params)

    def isolation(self, input=None, env=None, color=False):
        if self._in_pdb:
            if input or env or color:
                warnings.warn(
                    "CliRunner PDB un-isolation doesn't work if input/env/color are passed"
                )
            else:
                return self.isolation_pdb()

        return super().isolation(input=input, env=env, color=color)

    @contextlib.contextmanager
    def isolation_pdb(self):
        s = io.BytesIO(b"{stdout not captured because --pdb-trace}")
        yield (s, not self.mix_stderr and s)

@pytest.fixture
def cli_runner(request) -> MyCliRunner:
    """A wrapper round Click's test CliRunner to improve usefulness."""
    return MyCliRunner(
        # workaround Click's environment isolation so debugging works.
        in_pdb=request.config.getoption("--pdb-trace")
    )

@pytest.fixture
def datadir(request) -> pathlib.Path:
    """Returns the directory in which the test is sitting."""
    return pathlib.Path(request.module.__file__).parents[0] / "data"

def pytest_configure(config):
    """This function is run once for pytest setup."""
    config.addinivalue_line(
        "markers",
        "skip_if_rc_var_not_set(name): this mark skips the test if a certain "
        "~/.config/deepdraw.toml variable is not set",
    )

    config.addinivalue_line("markers", "slow: this mark indicates slow tests")

def pytest_runtest_setup(item):
    """This function is run for every test candidate in this directory.

    The test is run if this function returns ``None``.  To skip a test,
    call ``pytest.skip()``, specifying a reason.
    """
    from deepdraw.utils.rc import load_rc

    rc = load_rc()

    # iterates over all markers for the item being examined, get the first
    # argument and accumulate these names
    rc_names = [
        mark.args[0]
        for mark in item.iter_markers(name="skip_if_rc_var_not_set")
    ]

    # checks all names mentioned are set in ~/.config/deepdraw.toml, otherwise,
    # skip the test
    if rc_names:
        missing = [k for k in rc_names if rc.get(k) is None]
        if any(missing):
            pytest.skip(
                f"Test skipped because {', '.join(missing)} is **not** "
                f"set in ~/.config/deepdraw.toml"
            )

def rc_variable_set(name):
    from deepdraw.utils.rc import load_rc

    rc = load_rc()
    pytest.mark.skipif(
        name not in rc,
        reason=f"RC variable '{name}' is not set",
    )

@pytest.fixture(scope="session")
def temporary_basedir(tmp_path_factory):
    return tmp_path_factory.mktemp("test-cli")

def pytest_sessionstart(session: pytest.Session) -> None:
    """Presets the session start to ensure the STARE dataset is always
    available."""

    from deepdraw.utils.rc import load_rc

    rc = load_rc()

    database_dir = rc.get("datadir.stare")
    if database_dir is not None:
        # if the user downloaded it, use that copy
        return

    # else, we must extract the LFS component (we are likely on the CI)
    archive = (
        pathlib.Path(__file__).parents[0]
        / "data"
        / "deepdraw-ci-assets"
        / "test-database.zip"
    )
    assert archive.exists(), (
        f"Neither datadir.stare is set on the global configuration, "
        f"(typically ~/.config/deepdraw.toml), or it is possible to detect "
        f"the presence of {archive}' (did you git submodule init --update "
        f"this submodule?)"
    )

    stare_tempdir = tempfile.TemporaryDirectory()
    rc.setdefault("datadir.stare", stare_tempdir.name)

    with zipfile.ZipFile(archive) as zf:
        zf.extractall(stare_tempdir.name)

    config_filename = "deepdraw.toml"
    with open(os.path.join(stare_tempdir.name, config_filename), "wb") as f:
        tomli_w.dump(rc.data, f)
        f.flush()

    os.environ["XDG_CONFIG_HOME"] = stare_tempdir.name

    # stash the newly created temporary directory so we can erase it when the
    key = pytest.StashKey[tempfile.TemporaryDirectory]()
    session.stash[key] = stare_tempdir


import smbus
import serial
import time
import datetime
import math
#import requests
import urllib.request, json
#from urlparse import urlparse
#import json
import MySQLdb
from scipy import optimize,exp

i2c_address = 0x76
bus_number  = 1
bus = smbus.SMBus(bus_number)
digT = []
digP = []
digH = []

sensor_data = {'TEMP':'0.0', 'PRESSURE':'0.0','humidity':'0.0'}

def get_calib_param():
    calib = []

    for i in range (0x88,0x88+24):
        calib.append(bus.read_byte_data(i2c_address,i))
    calib.append(bus.read_byte_data(i2c_address,0xA1))
    for i in range (0xE1,0xE1+7):
         calib.append(bus.read_byte_data(i2c_address,i))

    digT.append((calib[1] << 8) | calib[0])
    digT.append((calib[3] << 8) | calib[2])
    digT.append((calib[5] << 8) | calib[4])
    digP.append((calib[7] << 8) | calib[6])
    digP.append((calib[9] << 8) | calib[8])
    digP.append((calib[11]<< 8) | calib[10])
    digP.append((calib[13]<< 8) | calib[12])
    digP.append((calib[15]<< 8) | calib[14])
    digP.append((calib[17]<< 8) | calib[16])
    digP.append((calib[19]<< 8) | calib[18])
    digP.append((calib[21]<< 8) | calib[20])
    digP.append((calib[23]<< 8) | calib[22])
    digH.append( calib[24] )
    digH.append((calib[26]<< 8) | calib[25])
    digH.append( calib[27] )
    digH.append((calib[28]<< 4) | (0x0F & calib[29]))
    digH.append((calib[30]<< 4) | ((calib[29] >> 4) & 0x0F))
    digH.append( calib[31] )

    for i in range(1,2):
         if digT[i] & 0x8000: digT[i] = (-digT[i] ^ 0xFFFF) + 1

    for i in range(1,8):
         if digP[i] & 0x8000: digP[i] = (-digP[i] ^ 0xFFFF) + 1

    for i in range(0,6):
        if digH[i] & 0x8000: digH[i] = (-digH[i] ^ 0xFFFF) + 1

def  get_data_bme280():
    setup()
    get_calib_param()
    data = []
    for i in range (0xF7, 0xF7+8):
        data.append(bus.read_byte_data(i2c_address,i))
    pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)
    TEMP_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
    hum_raw  = (data[6] << 8)  |  data[7]

    TEMP, t_fine = get_TEMP(TEMP_raw)
    PRESSURE = get_pressure(pres_raw,  t_fine )
    HUMI = get_humid(hum_raw,  t_fine )

    return TEMP, HUMI, PRESSURE

def get_pressure(adc_P,  t_fine ):
    PRESSURE = 0.0

    v1 = (t_fine / 2.0) - 64000.0
    v2 = (((v1 / 4.0) * (v1 / 4.0)) / 2048) * digP[5]
    v2 = v2 + ((v1 * digP[4]) * 2.0)
    v2 = (v2 / 4.0) + (digP[3] * 65536.0)
    v1 = (((digP[2] * (((v1 / 4.0) * (v1 / 4.0)) / 8192)) / 8)  + ((digP[1] * v1) / 2.0)) / 262144
    v1 = ((32768 + v1) * digP[0]) / 32768

    if v1 == 0: return 0
    PRESSURE = ((1048576 - adc_P) - (v2 / 4096)) * 3125
    if PRESSURE < 0x80000000: PRESSURE = (PRESSURE * 2.0) / v1
    else: PRESSURE = (PRESSURE / v1) * 2
    v1 = (digP[8] * (((PRESSURE / 8.0) * (PRESSURE / 8.0)) / 8192.0)) / 4096
    v2 = ((PRESSURE / 4.0) * digP[7]) / 8192.0
    PRESSURE = PRESSURE + ((v1 + v2 + digP[6]) / 16.0)
    return PRESSURE/100

def get_TEMP(adc_T):
    t_fine = 0.0
    v1 = (adc_T / 16384.0 - digT[0] / 1024.0) * digT[1]
    v2 = (adc_T / 131072.0 - digT[0] / 8192.0) * (adc_T / 131072.0 - digT[0] / 8192.0) * digT[2]
    t_fine = v1 + v2
    return t_fine / 5120.0,  t_fine

def get_humid(adc_H,  t_fine ):
    var_h =   t_fine  - 76800.0
    if var_h != 0:
         var_h = (adc_H - (digH[3] * 64.0 + digH[4]/16384.0 * var_h)) * (digH[1] / 65536.0 * (1.0 + digH[5] / 67108864.0 * var_h * (1.0 + digH[2] / 67108864.0 * var_h)))
    else:
         return 0
    var_h = var_h * (1.0 - digH[0] * var_h / 524288.0)
    if var_h > 100.0:
         var_h = 100.0
    elif var_h < 0.0:
         var_h = 0.0

    return var_h

def setup():
    osrs_t = 1
    osrs_p = 1
    osrs_h = 1
    mode   = 3
    t_sb   = 5
    filter = 0
    spi3w_en = 0

    ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode
    config_reg    = (t_sb << 5) | (filter << 2) | spi3w_en
    ctrl_hum_reg  = osrs_h
    bus.write_byte_data(i2c_address, 0xF2,ctrl_hum_reg)
    bus.write_byte_data(i2c_address, 0xF4,ctrl_meas_reg)
    bus.write_byte_data(i2c_address, 0xF5,config_reg)

def f(x,TEMP):
    PsTw=6.11*(10**((7.5*x)/(x+273.15)))
    return PsTw-0.00062*1013.25*(TEMP-x)

def g(x,HUMI):
    a=1-((x+273.15)/647.3)
    Ps=221200*exp((-7.765*a+1.45*(a**1.5)-2.776*(a**3)-1.233*(a**6))/(1-a))
    return (HUMI/100)*Ps

def h(x,TEMP,HUMI):
    return f(x,TEMP)-g(x,HUMI)

def main():

    ID=0
    #print("Date time , ID , TEMP , HUMI, PRESSURE , DI , WBGT\n")
    while True:

            data_list=[]
            ID=ID+1
            DATETIME=datetime.datetime.now()
            #yardstick_time=DATETIME.hour
            #if (yardstick_time+1)%24==DATETIME.hour:

            while 1:
                TEMP, HUMI, PRESSURE = get_data_bme280()
                gpio_seri=serial.Serial('/dev/ttyACM0',9600)

                data = gpio_seri.readline()

                if len(data)==30:
                    break

            #print(HUMI,TEMP)
            data_list =data.split(b",")
            #print(data)
            #print(data_list)
            b_TEMP=float(data_list[1])
            #print(b_TEMP)

            args_data=(TEMP,HUMI)
            #print(args_data)
            Twet=optimize.fsolve(h,0,args=args_data)
            #print(Twet)
            DI=0.81*TEMP+0.01*HUMI*((0.99*TEMP)-14.3)+46.3

            WBGT=float(0.7*Twet+0.2*b_TEMP+0.1*TEMP)
            #print(TEMP,HUMI,PRESSURE,DI,WBGT)
            json_data = { "TEMP":TEMP,"HUMI":HUMI,"PRESSURE":PRESSURE,"DI":DI,"WBGT":WBGT}

            try:

                url = "URL"
                method = "POST"
                headers = {"Content-Type" : "application/json"}

                store_data = json.dumps(json_data).encode("utf-8")

                #res=requests.post(url,data=store_data,headers=headers)
                request = urllib.request.Request(url, data=store_data, method=method, headers=headers)
                with urllib.request.urlopen(request) as response:
                    response_body = response.read().decode("utf-8")
                #print(response_body)

                time.sleep(900)

            except Exception as e:
                print(e)
                print("failed\n\n")
                time.sleep(900)

if __name__ == '__main__':
    main()


import os
import shutil
from loguru import logger

import torch

def load_ckpt(model, ckpt):
    model_state_dict = model.state_dict()
    load_dict = {}
    for key_model, v in model_state_dict.items():
        if key_model not in ckpt:
            logger.warning(
                "{} is not in the ckpt. Please double check and see if this is desired.".format(
                    key_model
                )
            )
            continue
        v_ckpt = ckpt[key_model]
        if v.shape != v_ckpt.shape:
            logger.warning(
                "Shape of {} in checkpoint is {}, while shape of {} in model is {}.".format(
                    key_model, v_ckpt.shape, key_model, v.shape
                )
            )
            continue
        load_dict[key_model] = v_ckpt

    model.load_state_dict(load_dict, strict=False)
    return model

def save_checkpoint(state, is_best, save_dir, model_name=""):
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    filename = os.path.join(save_dir, model_name + "_ckpt.pth")
    torch.save(state, filename)
    if is_best:
        best_filename = os.path.join(save_dir, "best_ckpt.pth")
        shutil.copyfile(filename, best_filename)


from ..responses import ResponseMsg, ResponseCQFunc
from ..permissions import get_permissions
from .general import Session

class CQCommandSession(Session):
    def __init__(self, user_id):
        Session.__init__(self, user_id=user_id)
        self._max_delta = 30
        self.session_type = 'CQ控制台'
        self.strict_commands = ['command', 'cmd', 'console', '控制台', '命令']
        self.description = '使用onebot标准（github.com/botuniverse/onebot）的API控制机器人'
        self.permissions = get_permissions().get('super', {})
        self.is_first_time = True
        self.func_name = ''
        self.kwargs = {}

    def handle(self, request):
        if self.is_first_time:
            self.is_first_time = False
            return ResponseMsg('【%s】请输入函数名' % self.session_type)
        elif self.func_name == '':
            self.func_name = request.msg
            return ResponseMsg('【%s】请输入参数（字典形式）' % self.session_type)
        elif self.kwargs == {}:
            self.kwargs = eval(request.msg)
            self.deactivate()
            return ResponseCQFunc(func_name=self.func_name, kwargs=self.kwargs)

    def process_output(self, output):
        return ResponseMsg('【%s/Output】%s' % (self.session_type, str(output)))

class CQRebootSession(Session):
    def __init__(self, user_id):
        Session.__init__(self, user_id=user_id)
        self._max_delta = 3
        self.session_type = 'CQ重启程序'
        self.strict_commands = ['restart', 'reboot', '重启']
        self.permissions = get_permissions().get('super', {})

    def handle(self, request):
        self.deactivate()
        return ResponseCQFunc(func_name='set_restart', kwargs={})

    def process_output(self, output):
        return ResponseMsg('【%s/Output】%s' % (self.session_type, str(output)))


from os import listdir
from os.path import isfile, join, splitext, basename
import multiprocessing
import time
import pandas as pd

SEQ_LEN = 288 # Number of indexes for 24 hours

def get_file_list(path, extension=None):
    if extension is None:
        file_list = [path + f for f in listdir(path) if isfile(join(path, f))]
    else:
        file_list = [
            path + f
            for f in listdir(path)
            if isfile(join(path, f)) and splitext(f)[1] == extension
        ]
    return file_list

def to_supervised(data):
    """ We want to predict if the price is going to increase by at least 20%
        in the next 10 hours by analyzing the past 24 hours. Klines of 5 min.
    """
    # expectation = 1.04
    n_futur_idx = 12 # Number of index for 1 hour
    # target = [data["Close"].iloc[i:i+n_futur_idx].max() >= expectation*data["Close"].iloc[i] for i in range(0, data.shape[0]-n_futur_idx)]
    target = [data["Close"].iloc[i+1:i+n_futur_idx+1].max() for i in range(0, data.shape[0]-(n_futur_idx+1))]
    return target

def build_target_data(CSV_file):
    data = pd.read_csv(CSV_file)
    target = to_supervised(data)
    data = data.iloc[0:len(target)]
    data["Max next hour"] = target
    data.to_csv(f"data/TargetMax/{basename(CSV_file)}", index=False)
    print(f"Target {basename(CSV_file)} is build")

if __name__ == "__main__":
    CSV_files = get_file_list("data/RAW/", extension=None)

    start = time.time()
    pool = multiprocessing.Pool()
    pool.map(build_target_data, CSV_files)
    pool.close()
    pool.join()
    stop = time.time()
    print(f"Execution time: {stop-start}s")


import zernike
import util
import cv2
import plotter
import combined
import contour
import orb
import sift

zernike_database = util.load_obj('zernike_database_icon_50')
orb_database = util.load_obj('orb_database_icon_50')
sift_database = util.load_obj('sift_database_icon_50')
images = util.load_images("shoes")
images = images[:50]
img = images[0]

# orb_database = orb.generate_database(images)
# util.save_obj(orb_database, 'orb_database_shoes_50')
# sift_database = sift.generate_database(images)
# util.save_obj(sift_database, 'sift_database_shoes_50')
# zernike_database = zernike.generate_database(images)
# util.save_obj(zernike_database, 'zernike_database_shoes_50')

# s = sift.create_query(img)
# s = sift.test_query(s, sift_database)
# plotter.plot_results(img, s, images)

print("starting")
o = orb.create_query(img)
o = orb.test_query(o, orb_database)
print("orb")
s = sift.create_query(img)
s = sift.test_query(s, sift_database)
print("sift")
z = zernike.create_query(img)
z = zernike.test_query(z, zernike_database)
print("zernike")
res = combined.test_combined([z,o,s], [1,5,5])
plotter.plot_results(img, res, images)

# img = images[0]
# z = zernike.create_query(images[0])
# z = zernike.test_query(z, zernike_database)
# res = combined.test_combined([z])
# plotter.plot_results(img, res, images)

# img = util.gray(img)

# cv2.namedWindow('image', cv2.WINDOW_NORMAL)
# cv2.imshow('image',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# cv2.imwrite('imzero.png',img)

# images = util.load_images("LLD-icon.hdf5")
# images = images[:10]

# img = util.gray(images[0])

# zernike_database = zernike.generate_database(images)
# util.save_obj(zernike_database, 'zernike_database_icon_10')

# zernike_database = util.load_obj('zernike_database_icon_10')

# z = zernike.create_query(images[0])
# print(zernike.test_query(z, zernike_database))

# contour_database = util.load_obj('contour_database_icon_10')
# c = contour.create_query(images[0])
# print(c)

#contour_database = contour.generate_database(images)
#util.save_obj(contour_database, 'contour_database_icon_10')

# print(contour.test_query(c, contour_database))
# # contour_database = contour.generate_database(images)
# # util.save_obj(contour_database, 'contour_database_icon_10')

print(len(images))


from django.conf.urls.static import static
from django.conf.urls import patterns, url, include
from solid_i18n.urls import solid_i18n_patterns
from django.conf import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = solid_i18n_patterns('',
    url(r'^member/', include('apps.team.urls')),
    url(r'^search/', include('haystack.urls')),
    url(r'^', include('apps.site.urls')),
    url(r'^', include('apps.news.urls')),

) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += patterns('',
    url(r'^admin/', include(admin.site.urls)),
)

if 'tinymce' in settings.INSTALLED_APPS:
    urlpatterns += patterns('',
        url(r'^tinymce/', include('tinymce.urls')),
        url(r'^tinymce/filebrowser/', include('filebrowser.urls')),
    )
if 'rosetta' in settings.INSTALLED_APPS:
    urlpatterns += patterns('',
        url(r'^rosetta/', include('rosetta.urls')),
    )

# For development only
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve', {"document_root": settings.MEDIA_ROOT}),
    )


from flask import Flask, request, jsonify
from dotenv import load_dotenv
import subprocess
import os

import requests
import threading
import time
import json
import platform
import datetime

requests.packages.urllib3.disable_warnings()

load_dotenv()  # take environment variables from .env.
app = Flask(__name__)
fileDir = os.path.dirname(__file__)
servicesDir = os.path.join(fileDir, '../services')
print(f"servicesDir: {servicesDir}")

PORT = os.environ.get("SERVICE_FATHER_PORT", 16000)
EXPECTED_TOKEN_ID = os.environ.get("SERVICE_FATHER_TOKEN_ID", "")
REPORT_IP = os.environ.get("SERVICE_FATHER_MGR_REPORT_IP", None)
REPORT_INTERVAL_SECS = os.environ.get("SERVICE_FATHER_MGR_REPORT_INTERVAL", 2)
HOSTNAME = platform.node()
MAX_KAP_OFFSET = int(os.environ.get("MAX_SERVICES_KAP_OFFSET_SECONDS", 10))

REPORT_DOCTOR_IP = os.environ.get("DEXYNTH_DOCTOR_REPORT_IP", "dd.dexynth.com")
REPORT_DOCTOR_INTERVAL_SECS = int(os.environ.get("DEXYNTH_DOCTOR_REPORT_INTERVAL", 60))

SF_INSTANCE= os.environ.get("SF_INSTANCE", HOSTNAME)

serviceStatus = {}

def getErrorJson(service, action, completedProcess):
    # http 500: internal server error
    err = {'service': service,
           'action': action,
           'message': f"Error while running script '{action}.sh' for service '{service}'.",
           'stderr': completedProcess.stderr.decode('utf-8'),
           'stdout': completedProcess.stdout.decode('utf-8'),
           'returncode': completedProcess.returncode}

    return err

def getResponseJson(service, action, completedProcess):
    rsp = {'service': service,
           'action': action,
           'message': f"Action '{action}' performed for service '{service}'.",
           'stderr': completedProcess.stderr.decode('utf-8'),
           'stdout': completedProcess.stdout.decode('utf-8'),
           'returncode': completedProcess.returncode}
    return rsp

def enableService(service_name):
    with open(f'{servicesDir}/{service_name}/enabled', 'w') as f:
        f.write('')
    return

def disableService(service_name):
    if os.path.isfile(f'{servicesDir}/{service_name}/enabled'):
        os.remove(f'{servicesDir}/{service_name}/enabled')
    return

def getPostResponse(json):
    # print(f"Performing post request: {json}")
    data = json
    service_name = data.get('serviceName')
    action = data.get('action')
    token_id = data.get('token', None)
    action = action.lower()

    if not service_name or not action or not token_id:
        # http 400: bad request
        return {'returncode': 400, 'message': 'Service name, action, and tokenId are required.'}, 400

    if token_id != EXPECTED_TOKEN_ID:
        # http 401: unauthorized
        return {'returncode': 403, 'message': 'Incorrect tokenId. Access denied.'}, 401

    if action == 'enable':
        # print(f"Enabling service '{service_name}'")
        enableService(service_name)
        return {'returncode': 200, 'message': f"Service '{service_name}' enabled."}, 200
    elif action == 'disable':
        disableService(service_name)
        return {'returncode': 200, 'message': f"Service '{service_name}' disabled."}, 200

    script_path = f'{servicesDir}/{service_name}/{action}.sh'
    if not os.path.isfile(script_path):
        return {'returncode': 404, 'message': f"Script '{action}.sh' not found for service '{service_name}'."}, 404

    isEnabled = os.path.isfile(f'{servicesDir}/{service_name}/enabled')
    if not isEnabled:
        return {'returncode': 500, 'message': f"Service '{service_name}' is disabled."}, 500

    # print(f"Running script '{action}.sh' for service '{service_name}'")
    completedProcess = subprocess.run(
        ['bash', script_path], capture_output=True)
    if completedProcess.returncode != 0:
        # http 500: internal server error
        return getErrorJson(service_name, action, completedProcess), 500
    # http 200: okq
    return getResponseJson(service_name, action, completedProcess), 200

@app.route('/api', methods=['POST'])
def perform_post():
    json, http_code = getPostResponse(request.json)
    print("forcing reportStatus")
    reportStatus()
    return jsonify(json), http_code

@app.route('/api/kap', methods=['POST'])
def perform_kap():
    data = request.json
    serviceName = data.get('serviceName')
    status = data.get('status','')
    kapTime = datetime.datetime.now()
    print(f"KAP: {serviceName} {status} {kapTime}")
    serviceStatus[serviceName] = {'status': status, 'time': kapTime}
    return jsonify({'returncode': 200, 'message': f"KAP: {serviceName} {status} {kapTime}"}), 200

def calculateKapStatus():
    for serviceName in serviceStatus:
        kapTime = serviceStatus[serviceName]['time']
        now = datetime.datetime.now()
        delta = now - kapTime
        serviceStatus[serviceName]['kapDelta'] = delta.seconds
        serviceStatus[serviceName]['kapOk'] = delta.seconds <= MAX_KAP_OFFSET

@app.route('/api/kap', methods=['GET'])
def get_kap():
    serviceName = request.args.get("serviceName", None)
    calculateKapStatus()
    if serviceName is None:
        return jsonify(serviceStatus), 200
    else:
        print(f"serviceName: {serviceName}")
        if serviceName not in serviceStatus:
            return jsonify({'kapOk': False, 'status': "unknown", "time" : "unknown"}), 200
        return jsonify(serviceStatus[serviceName]), 200

# https://localhost:26000/api/services
# https://localhost:26000/api/services?kind=enabled
# https://localhost:26000/api/services?kind=disabled

def getServices(kind='all'):
    # print(f"Getting services of kind '{kind}'")
    services = os.listdir(servicesDir)
    outServices = []
    for service in services:
        enabled = False
        if os.path.isfile(f'{servicesDir}/{service}/enabled'):
            enabled = True
        outServices.append({"service": service, "enabled": enabled})

    if (kind == "enabled"):
        outServices = list(
            filter(lambda service: service["enabled"], outServices))
    elif (kind == "disabled"):
        outServices = list(
            filter(lambda service: not service["enabled"], outServices))
    return outServices

@ app.route('/api/services', methods=['GET'])
def get_services():
    kind = request.args.get("kind", "all")
    outServices = getServices(kind)
    return jsonify(outServices), 200

def reportStatus():
    if REPORT_IP is None:
        return
    # url = f'http://{REPORT_IP}/get_my_ip'
    # ip = requests.get(url).json()['ip']
    # print(f"Reporting status to {REPORT_IP} from {ip}", flush=True)
    allServices = getServices()
    globalStatus = []
    for service in allServices:
        serviceName = service['service']
        enabled = service['enabled']
        statusRequest = {"serviceName": serviceName,
                         "action": "status",
                         "token": EXPECTED_TOKEN_ID}
        statusRequest, code = getPostResponse(statusRequest)
        # print(f"service: {service}")
        # print(f"statusRequest: {statusRequest}")
        srvStatus = {
            'hostname': HOSTNAME,
            'port': PORT,
            'service': serviceName,
            'enabled': enabled,
            'statusMessage': statusRequest['message'],
            'returncode': statusRequest['returncode']
        }
        globalStatus.append(srvStatus)

    statusJson = json.dumps(globalStatus)

    # print(f"globalStatus: {statusJson}")
    try:
        response = requests.post(f'http://{REPORT_IP}/status',
                                 json=globalStatus, verify=False, timeout=1)
        # print(f"response: {response}")
    except Exception as e:
        print(f"Exception: {e}")
        print(f"Failed to report status to {REPORT_IP}", flush=True)
        return

"""
REPORT_DOCTOR_IP = os.environ.get("DEXYNTH_DOCTOR_REPORT_IP", None)
REPORT_DOCTOR_INTERVAL_SECS = os.environ.get("DEXYNTH_DOCTOR_REPORT_INTERVAL", 2)

SF_INSTANCE= os.environ.get("SF_INSTANCE", platform.node())
"""
def __reportStatusToDoctor():
    if REPORT_DOCTOR_IP is None:
        return
    url = f'https://{REPORT_DOCTOR_IP}/heartbeat'
    serviceName = f"ServiceFather@{SF_INSTANCE}"
    data = {'service': serviceName}
    try:
        print(f"Reporting status to {REPORT_DOCTOR_IP} from {HOSTNAME}: {data}", flush=True)
        response = requests.post(url, json=data, verify=False, timeout=1)
        print(f"response: {response}", flush=True)
    except Exception as e:
        print(f"Exception: {e}")
        print(f"Failed to report status to {REPORT_DOCTOR_IP}", flush=True)
        return

def startReportStatusThread():
    def reportStatusToDoctorThread():
        while True:
            print(f"  >> reportStatusToDoctorThread", flush=True)
            __reportStatusToDoctor()
            time.sleep(REPORT_DOCTOR_INTERVAL_SECS)

    def reportStatusThread():
        while True:
            reportStatus()
            time.sleep(REPORT_INTERVAL_SECS)

    if REPORT_IP is not None:
        x = threading.Thread(target=reportStatusThread, args=())
        x.start()

    if REPORT_DOCTOR_IP is not None:
        print(f"Starting reportStatusToDoctorThread")
        x = threading.Thread(target=reportStatusToDoctorThread, args=())
        x.start()

if __name__ == '__main__':
    startReportStatusThread()
    app.run(debug=True, ssl_context='adhoc', host='0.0.0.0', port=PORT)


import sys
import os
import logging
import time

import ipdb
import pickle
from typing import Optional
from dataclasses import dataclass, field

import torch
import transformers
from transformers import (
    Seq2SeqTrainingArguments,
    HfArgumentParser,
    set_seed,
    AutoTokenizer,
    AutoConfig,
    T5Config,
    T5Tokenizer,
)
from transformers.trainer_utils import is_main_process, get_last_checkpoint
from models import T5ForConditionalGeneration, MarkerT5
from aste_dataset import ASTEDataset
from marker_trainer import MarkerSeq2SeqTrainer
from utils.eval_utils import parse_and_score

logger = logging.getLogger(__name__)

@dataclass
class ModelArguments:
    """
    Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
    """
    model_name_or_path: str = field(
        default="../../PretrainModel/t5-base",
        metadata={
            "help": "Path to pretrained model or model identifier from huggingface.co/models"}
    )
    config_name: Optional[str] = field(
        default=None,
        metadata={"help": "Pretrained config name or path if not the same as model_name"}
    )
    tokenizer_name: Optional[str] = field(
        default=None,
        metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
    )
    cache_dir: Optional[str] = field(
        default=None,
        metadata={
            "help": "Where to store the pretrained models downloaded from huggingface.co"
        },
    )
    use_fast_tokenizer: bool = field(
        default=False,
        metadata={
            "help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."
        },
    )
    model_revision: str = field(
        default="main",
        metadata={
            "help": "The specific model version to use (can be a branch name, tag name or commit id)."},
    )
    use_auth_token: bool = field(
        default=False,
        metadata={
            "help": "Will use the token generated when running `transformers-cli login` (necessary to use this script "
                    "with private models)."
        },
    )

@dataclass
class DataTrainingArguments:
    """
    Arguments pertaining to what data we are going to input our model for training and eval.
    """
    dataset: str = field(
        default='laptop14', metadata={"help": "laptop14/rest14/rest15/rest16 "}
    )
    data_format: str = field(
        default="A", metadata={"help": "A/O/AO"}
    )
    train_path: Optional[str] = field(
        default=None, metadata={"help": "The file path of train data"}
    )
    valid_path: Optional[str] = field(
        default=None, metadata={"help": "The file path of valid data"},
    )
    test_path: Optional[str] = field(
        default=None, metadata={"help": "The file path of test data"},
    )
    max_length: int = field(
        default=256, metadata={"help": "The max padding length of source text and target text"}
    )
    num_beams: int = field(
        default=1, metadata={"help": "greedy search"}
    )
    shot_ratio_index: Optional[str] = field(
        default="-1[+]-1[+]1", metadata={"help": "1[+]-1[+]1->1-shot"}
    )

    def __post_init__(self):
        if self.dataset is None and self.train_path is None and self.valid_path is None:
            raise ValueError(
                "Need either a dataset name or a training/validation file.")
        shot, ratio, index = self.shot_ratio_index.split("[+]")
        shot, ratio, index = int(shot), float(ratio), int(index)
        assert shot in [-1, 1, 5, 10] and ratio in [-1, 0.01, 0.05, 0.1]
        self.full_supervise = True if shot == -1 and ratio == -1 else False
        name_mapping = {"laptop14": "14lap", "rest14": "14res", "rest15": "15res", "rest16": "16res"}
        if shot != -1:
            self.train_path = f'./data/ASTE/{name_mapping[self.dataset]}_shot/seed{index}/{shot}shot/train.json'
            self.valid_path = f'./data/ASTE/{name_mapping[self.dataset]}_shot/seed{index}/{shot}shot/val.json'
            self.test_path = f'./dataa/ASTE/{name_mapping[self.dataset]}_shot/seed{index}/{shot}shot/test.json'
        elif ratio != -1:
            self.train_path = f'./data/ASTE/{name_mapping[self.dataset]}_ratio/seed{index}/{ratio}/train.json'
            self.valid_path = f'./data/ASTE/{name_mapping[self.dataset]}_ratio/seed{index}/{ratio}/val.json'
            self.test_path = f'./data/ASTE/{name_mapping[self.dataset]}_ratio/seed{index}/{ratio}/test.json'
        else:
            self.train_path = f'./data/ASTE/{self.dataset}/train.txt'
            self.valid_path = f'./data/ASTE/{self.dataset}/dev.txt'
            self.test_path = f'./data/ASTE/{self.dataset}/test.txt'

        self.source_aspect_prefix = ["aspect", "first:"]
        self.source_opinion_prefix = ["opinion", "first:"]
        self.prefix_word_length = 2  # ["aspect", "first:"]
        self.prefix_token_length = 3  # ["aspect", "first", ":"]

    ignore_pad_token_for_loss: bool = field(
        default=True,
        metadata={
            "help": "Whether to ignore the tokens corresponding to padded labels in the loss computation or not."
        },
    )

@dataclass
class MarkerSeq2SeqTrainingArguments(Seq2SeqTrainingArguments):
    """
    Arguments for our model in training procedure
    """
    constraint_decoding: bool = field(default=False, metadata={"help": "Whether to Constraint Decoding or not."})
    use_marker: bool = field(default=False, metadata={"help": "Whether to use marker"})
    marker_type: Optional[str] = field(default='AO', metadata={"help": "marker_type A/O"})
    alpha: float = field(default=0.5, metadata={"help": "adjust the loss weight of ao_template and oa_template"})

def main():
    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, MarkerSeq2SeqTrainingArguments))
    if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
        # If we pass only one argument to the script, and it's the path to a json file,
        # let's parse it to get our arguments.
        model_args, data_args, training_args = parser.parse_json_file(
            json_file=os.path.abspath(sys.argv[1]))
    else:
        model_args, data_args, training_args = parser.parse_args_into_dataclasses()
    # print(model_args, data_args, training_args, sep='\n')

    if data_args.data_format == "AO":
        training_args.per_device_train_batch_size *= 2

    # Detecting last checkpoint.
    last_checkpoint = None
    if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
        last_checkpoint = get_last_checkpoint(training_args.output_dir)
        if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
            raise ValueError(
                f"Output directory ({training_args.output_dir}) already exists and is not empty. "
                "Use --overwrite_output_dir to overcome."
            )
        elif last_checkpoint is not None:
            logger.info(
                f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
                "the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
            )

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        handlers=[logging.StreamHandler(sys.stdout)],
    )
    logger.setLevel(logging.INFO if is_main_process(training_args.local_rank) else logging.WARN)

    # Log on each process the small summary:
    logger.warning(
        f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"
        + f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}"
    )

    # Set the verbosity to info of the Transformers logger (on main process only):
    if is_main_process(training_args.local_rank):
        transformers.utils.logging.set_verbosity_info()
    # logger.info("Training/evaluation parameters %s", training_args)

    # Set seed before initializing model.
    set_seed(training_args.seed)
    # ipdb.set_trace()
    config = AutoConfig.from_pretrained(
        model_args.config_name if model_args.config_name else model_args.model_name_or_path,
        cache_dir=model_args.cache_dir,
        revision=model_args.model_revision,
        use_auth_token=True if model_args.use_auth_token else None,
    )
    # pass args
    config.max_length = data_args.max_length
    config.num_beams = data_args.num_beams

    tokenizer = AutoTokenizer.from_pretrained(
        model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path,
        cache_dir=model_args.cache_dir,
        use_fast=model_args.use_fast_tokenizer,
        revision=model_args.model_revision,
        use_auth_token=True if model_args.use_auth_token else None,
    )
    tokenizer.add_tokens(['[SSEP]'])

    pretrain_model = T5ForConditionalGeneration.from_pretrained(
        model_args.model_name_or_path,
        from_tf=bool(".ckpt" in model_args.model_name_or_path),
        config=config,
        cache_dir=model_args.cache_dir,
        revision=model_args.model_revision,
        use_auth_token=True if model_args.use_auth_token else None,
    )
    pretrain_model.resize_token_embeddings(len(tokenizer))
    model = MarkerT5(
        args=data_args,
        tokenizer=tokenizer,
        t5_model=pretrain_model,
        t5_config=config,
        use_marker=training_args.use_marker,
        marker_type=training_args.marker_type
    )

    # def get_parameter_number(model):
    #     total_num = sum(p.numel() for p in model.parameters())
    #     trainable_num = sum(p.numel() for p in model.parameters() if p.requires_grad)
    #     return {'Total': total_num, 'Trainable': trainable_num}
    #
    # print(get_parameter_number(model))

    if not training_args.do_train:
        logger.info("load checkpoint of Marker-T5 !")
        model.load_state_dict(torch.load(f"{training_args.output_dir}/checkpoint-1120/pytorch_model.bin"))

    train_dataset = ASTEDataset(tokenizer=tokenizer, data_path=data_args.train_path, opt=data_args)
    eval_dataset = ASTEDataset(tokenizer=tokenizer, data_path=data_args.valid_path, opt=data_args)
    test_dataset = ASTEDataset(tokenizer=tokenizer, data_path=data_args.test_path, opt=data_args)

    def compute_metrics(eval_preds):
        preds, labels = eval_preds
        preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
        labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
        result = parse_and_score(preds, labels, data_args.data_format)
        return result

    trainer = MarkerSeq2SeqTrainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset if training_args.do_eval else None,
        data_format=data_args.data_format,
        tokenizer=tokenizer,
        ignore_pad_token=data_args.ignore_pad_token_for_loss,
        compute_metrics=compute_metrics if training_args.predict_with_generate else None,
    )

    if training_args.do_train:
        checkpoint = None
        train_result = trainer.train(resume_from_checkpoint=checkpoint)

        output_train_file = os.path.join(
            training_args.output_dir, "train_results.txt")
        if trainer.is_world_process_zero():
            with open(output_train_file, "w") as writer:
                logger.info("***** Train results *****")
                for key, value in sorted(train_result.metrics.items()):
                    logger.info(f"  {key} = {value}")
                    writer.write(f"{key} = {value}\n")

            # Need to save the state, since Trainer.save_model saves only the tokenizer with the model
            trainer.state.save_to_json(os.path.join(
                training_args.output_dir, "trainer_state.json"))

    # if training_args.do_eval:
    #     logger.info("*** Evaluate ***")
    #
    #     eval_results = trainer.predict(
    #         eval_dataset,
    #         metric_key_prefix="eval",
    #         max_length=data_args.max_length,
    #         num_beams=data_args.num_beams,
    #         constraint_decoding=True
    #     )
    #
    #     output_eval_file = os.path.join(training_args.output_dir, "eval_results_seq2seq.txt")
    #     if trainer.is_world_process_zero():
    #         with open(output_eval_file, "w") as writer:
    #             logger.info("***** Eval results *****")
    #             for key, value in sorted(eval_results.metrics.items()):
    #                 logger.info(f"  {key} = {value}")
    #                 writer.write(f"{key} = {value}\n")

    if training_args.do_predict:
        logger.info(f"*** Test constraint decoding: {training_args.constraint_decoding}***")
        test_results = trainer.predict(
            test_dataset,
            metric_key_prefix="test",
            max_length=data_args.max_length,
            num_beams=data_args.num_beams,
            constraint_decoding=training_args.constraint_decoding,
        )
        test_metrics = test_results.metrics
        test_metrics["test_loss"] = round(test_metrics["test_loss"], 4)
        output_test_result_file = os.path.join(training_args.output_dir, "test_results_seq2seq.txt")
        if trainer.is_world_process_zero():
            with open(output_test_result_file, "w") as writer:
                logger.info("***** Test results *****")
                for key, value in sorted(test_metrics.items()):
                    logger.info(f"{key} = {value}")
                    writer.write(f"{key} = {value}\n")

if __name__ == "__main__":
    main()


import cgi, os

# Getting the data from the buildhamburger.html post request.
data = cgi.FieldStorage()

# Total Price of burger.
totalPrice = 0

# Total BrugerCO Points.
burgerCOPoints = 0

# Getting the different values.
getHamburgerType = data.getvalue("type")
getHamburgerBreadType = data.getvalue("bread-type")
getProgram = data.getvalue("program")

# We will store the html code in these variables.
setHamburgerType = str()
setHamburgerBreadType = str()
setHamburgerToppings = str()
setProgram = str()

if getHamburgerType == "beef":
    totalPrice += 10
    burgerCOPoints += 1
    setHamburgerType = "
Beef Price: 10 nok
"

elif getHamburgerType == "chicken":
    totalPrice += 7
    burgerCOPoints += 1
    setHamburgerType = "
Chicken Price: 7 nok
"

elif getHamburgerType == "rib":
    totalPrice += 12
    burgerCOPoints += 1
    setHamburgerType = "
Rib Price: 12 nok
"

elif getHamburgerType == "vegetarian":
    totalPrice += 20
    burgerCOPoints += 1
    setHamburgerType = "
Rib Price: 12 nok
"

if getHamburgerBreadType == "normalbun":
    totalPrice += 6
    burgerCOPoints += 1
    setHamburgerBreadType = "
Normal bun: 6 nok
"

elif getHamburgerBreadType == "ciabatta":
    totalPrice += 10
    burgerCOPoints += 1
    setHamburgerBreadType = "
Ciabatta: 10 nok
"

elif getHamburgerBreadType == "agnusbun":
    totalPrice += 15
    burgerCOPoints += 1
    setHamburgerBreadType = "
Agnus bun: 15 nok
"

if data.getvalue("cheese") :
    totalPrice += 10
    burgerCOPoints += 1
    setHamburgerToppings += "
Cheese: 10 nok
"

if data.getvalue("tomato"):
    totalPrice += 5
    burgerCOPoints += 1
    setHamburgerToppings += "
Tomato: 5 nok
"

if data.getvalue("egg"):
    totalPrice += 7
    burgerCOPoints += 1
    setHamburgerToppings += "
Egg: 7 nok
"

if getProgram == "yes-program":
    setProgram = "Added {} Points to BurgerCO!".format(burgerCOPoints)

if data:
    print("Content-Type: text/html")
    print("")
    print("<!DOCTYPE html>")
    print("")
    print("")
    print("<title>Hamburger</title>")
    print("<link href=\"https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&display=swap\" rel=\"stylesheet\">")
    print("")
    print("")
    print("")
    print("
")
    print("
Recipe
")
    print("
")
    print(setHamburgerType)
    print(setHamburgerBreadType)
    print(setHamburgerToppings)
    print(setProgram)
    print("
Total Price: {} nok
".format(totalPrice))
    print("
")
    print("
")
    print("")
    print("")
    print("")


"""Reviewers and ratings: Number of Submissions/Revisions over time"""
from asyncio import gather, run
from typing import Tuple, Type, Union

from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from pandas import DataFrame
from seaborn import barplot, color_palette
from sqlalchemy import func
from sqlalchemy.future import select
from sqlalchemy.sql.selectable import Select
from uvloop import install

from cmt_statistics_tool.statistics import format_sort_track, get_data, plot_df
from cmt_statistics_tool.tables import Revision, Submission

def statement(paper: Union[Type[Submission], Type[Revision]]) -> Select:
    return (
        select(paper.track_name, func.count())
        .group_by(paper.track_name)
        .order_by(paper.track_name)
    )

async def submission() -> DataFrame:
    df = DataFrame(await get_data(statement(Submission))).rename(
        columns={0: "Track", 1: "Count"}
    )
    return format_sort_track(df, "Track")

def plot_submission(df: DataFrame, ax: Axes) -> None:
    barplot(x="Track", y="Count", data=df, color=color_palette()[0], ax=ax)
    ax.set_title("Number of submissions over time")

async def revision() -> DataFrame:
    df = DataFrame(await get_data(statement(Revision))).rename(
        columns={0: "Track", 1: "Count"}
    )
    return format_sort_track(df, "Track", True)

def plot_revision(df: DataFrame, ax: Axes) -> None:
    barplot(x="Track", y="Count", data=df, color=color_palette()[0], ax=ax)
    ax.set_title("Number of revisions over time")

def plot_both(df: DataFrame, ax: Axes) -> None:
    barplot(x="Track", y="Count", hue="Type", data=df, ax=ax)
    ax.set_title("Number of papers over time")

async def main() -> Tuple[DataFrame, DataFrame]:
    return await gather(submission(), revision())

if __name__ == "__main__":
    install()
    s_df, r_df = run(main())
    plot_df(s_df, plot_submission)
    plt.savefig("plots/01_03_submission.png")
    plot_df(r_df, plot_revision)
    plt.savefig("plots/01_03_revision.png")
    print(s_df, r_df, sep="\n")
    plot_df(
        s_df.set_index("Track")
        .rename(columns={"Count": "Original Submission"})
        .join(
            r_df.set_index("Track").rename(columns={"Count": "Revision"}), how="outer"
        )
        .fillna(0)
        .astype(int)
        .reset_index()
        .melt(id_vars=["Track"], var_name="Type", value_name="Count"),
        plot_both,
    )
    plt.savefig("plots/01_03_both.png")


from cuber import cube
from cuber import utils
import logging
import numpy as np

logger = logging.getLogger(__name__)

class CubeSplitTrainTest(cube.Cube):
    def __init__(self, X, y, test_ratio, seed):
        self.seed = seed
        self.X = X
        self.y = y
        self.test_ratio = test_ratio

    def name(self):
        return 'data_{}_{}_{}'.format(self.test_ratio, self.seed, utils.universal_hash((self.X, self.y)))

    def eval(self):
        logger.info('Evaluating')
        np.random.seed(self.seed)
        test_mask = np.random.binomial(n = 1, size = self.y.shape[0], p = self.test_ratio).astype(bool)
        X_test = self.X[test_mask]
        y_test = self.y[test_mask]
        X_train = self.X[~test_mask]
        y_train = self.y[~test_mask]
        logger.info('Done')
        return {
            'X_test': X_test,
            'y_test': y_test,
            'X_train': X_train,
            'y_train': y_train,
        }


# -*- coding: utf-8 -*-
from setuptools import find_packages
from setuptools import setup

with open('README.md', 'r') as fh:
    long_description = fh.read()

setup(
    name='osxcollector_output_filters',
    version='1.1.1',
    author='Yelp Security',
    author_email='opensource@yelp.com',
    description='Filters that process and transform the output of OSXCollector',
    long_description=long_description,
    long_description_content_type='text/markdown',
    license='GNU General Public License',
    url='https://github.com/Yelp/osxcollector_output_filters',
    setup_requires='setuptools',
    packages=find_packages(exclude=['tests']),
    provides=['osxcollector'],
    install_requires=[
        'PyYAML>=5.0',
        'threat_intel',
        'tldextract',
        'simplejson',
        'six',
    ],
)


# %%
import simpy
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

RANDOM_SEED = 42
random.seed(RANDOM_SEED)

WALK_RADIUS = 50
# TO-DO:
# parametrize speeds
# include gis maps

#####    INPUT OF DATA #####
data = {
    # Size of the grid
    "grid": {"xlim": [0, 100], "ylim": [0, 100]},
    # Docking stations with their location and capacity
    "stations": [{"location": [5, 5], "capacity": 3}, {"location": [10, 10], "capacity": 3}, {"location": [90, 90], "capacity": 2}, {"location": [95, 95], "capacity": 2},],
    # All the bikes (0=SB; 1=Dockless,2=Autonomous) and their initial locations
    "bikes": [
        {"mode": 0, "station": 0},
        {"mode": 0, "station": 0},
        {"mode": 0, "station": 1},
        {"mode": 0, "station": 2},
        {"mode": 0, "station": 3},
        {"mode": 1, "location": [10, 5]},
        {"mode": 1, "location": [10, 5]},
        {"mode": 1, "location": [10, 5]},
        {"mode": 2, "location": [5, 15]},
        {"mode": 2, "location": [5, 15]},
        {"mode": 2, "location": [5, 15]},
    ],
    # Number of agents
    "nagents": 5,
    # 'agents': [{'source','target','time_source','time_target','mode'}]
}

####### DEFINITION OF CLASSES (City, Staion, Bike, Agent) ######
class City:
    def __init__(self, env, data):  # Constructor is automatically called when you create a new instance of a class
        self.env = env  # Takes simulation environment
        self.data = data  # Its the city that takes the initial data and sets it

        self.stations = []  # defines arrays of objects of different classes within itself
        self.bikes = []
        self.agents = []

        self.start()

    def start(self):  # Start calls to the initialization of station, bikes, agents
        self.init_stations()
        self.init_bikes()
        self.init_agents()

    def init_stations(self):
        # Takes data of stations and goes one by one adjudicating the data to the station objects
        for station_id, station_data in enumerate(self.data["stations"]):
            station = Station(self.env, station_id)  # Takes data of stations and goes one by one adjudicating the data to the station objects (that now have an id)
            station.set_capacity(station_data["capacity"])  # asjudicating data
            station.set_location(np.array(station_data["location"]))  # " np.array() helps to structure data ???
            station.init_station()  # initialize that station (resource and container)
            self.stations.append(station)  # Adds station to the staions array in city

    def init_bikes(self):
        for bike_id, bike_data in enumerate(self.data["bikes"]):  # There is not Dockless 1 and Autonomous 1, they share ids !
            mode = bike_data["mode"]  # Takes the mode
            if mode == 0:
                bike = StationBike(self.env, bike_id)
                station_id = bike_data["station"]
                self.stations[station_id].push_bike(bike_id)  # Saves the bike in the station
                bike.set_station(station_id)  # saves the station in the bike
                bike.set_location(self.stations[station_id].location)  # Saves the location of the station as its location
            elif mode == 1:
                bike = DocklessBike(self.env, bike_id)  # creates object of class Dockless Bike with the env and the id as imput
                bike.set_location(np.array(bike_data["location"]))  # initial location is given, so it takes it and saves it
            elif mode == 2:
                bike = AutonomousBike(self.env, bike_id)  # creates object of class Autonomous Bike with the env and the id as imput
                bike.set_location(np.array(bike_data["location"]))  # initial location is given, so it takes it and saves it
            self.bikes.append(bike)  # Adds bike to the array of Bikes in city

    def init_agents(self):
        for agent_id in range(self.data["nagents"]):  # here the only input is the number of agents
            mode = np.random.randint(0, 2)  # assigns a random mode
            # mode = 2
            # Creates a different class of agent depending in the mode
            if mode == 0:
                agent = StationAgent(self.env, agent_id)
            elif mode == 1:
                agent = DocklessAgent(self.env, agent_id)
            elif mode == 2:
                agent = AutonomousAgent(self.env, agent_id)
            agent.set_data(self.data["grid"], self.stations, self.bikes)  # To 'know' the city / It needs this data to operate # TO-DO ???¿
            agent.start()  # Defines structure for location, bike id and event setup ?
            self.agents.append(agent)  # Adds the agent to the array of agents in city

class Station:
    def __init__(self, env, station_id):  # constructor
        self.env = env
        self.station_id = station_id

        self.location = None
        self.capacity = None
        self.bikes = []

        self.history = []
        self.init_state()

    def init_state(self):
        state_keys = ["time", "bikes", "nbikes"]
        self.state = dict.fromkeys(state_keys)

    def init_station(self):  # parameters that help to manage the station
        self.resource = simpy.Resource(self.env, capacity=1)  # Put some order in case 2 arrite at the same time Do we need this ? If all slots are taken, requests are enqueued. Once a usage request is released, a pending request will be triggered
        self.container = simpy.Container(self.env, self.capacity, init=0)  # It supports requests to put or get matter into/from the container

    def set_location(self, location):  # takes location defined in City class definition and saves it
        self.location = location

    def set_capacity(self, capacity):  # takes capacity defined in City class definition and saves it
        self.capacity = capacity

    def has_bikes(self):
        return self.container.level > 0

    def has_docks(self):
        return self.capacity - self.container.level > 0

    def empty(self):
        return self.container.level == 0

    def full(self):
        return self.container.level == self.capacity

    def choose_bike(self):  # Selects any bike (Random package)
        return random.choice(self.bikes)

    def pull_bike(self, bike_id):
        if self.has_bikes():  # Check that it has bikes
            self.container.get(1)  # Counts that there is one less
            self.bikes.remove(bike_id)  # removes that bike from the list
            self.save_state()  # saves time/bikes/nbikes in history -> FORMAT? and then init_state??
            self.print_info()  # prints time/station/nbikes/capacity
        else:
            print("[%.2f] Station %d has no bikes available" % (self.env.now, self.station_id))

    def push_bike(self, bike_id):
        if self.has_docks():  # check that there are docks available
            self.container.put(1)  # add one item
            self.bikes.append(bike_id)  # add the bike's id
            self.save_state()  # saves time/bikes/nbikes in history
            self.print_info()  # prints time/station/nbikes/capacity
        else:
            print("[%.2f] Station %d has no docks available" % (self.env.now, self.station_id))

    def save_state(self):  # FORMAT?
        # print(self.station_id, np.round(self.env.now), self.bikes, len(self.bikes))
        self.state["time"] = np.round(self.env.now)
        self.state["bikes"] = self.bikes.copy()
        self.state["nbikes"] = len(self.bikes)
        self.history.append(self.state)
        self.init_state()

    def print_info(self):  # Prints time, station, and nbikes/capacity
        print("[%.2f] Station %d has %d bikes out of %d" % (self.env.now, self.station_id, self.container.level, self.capacity))

class Bike:
    def __init__(self, env, bike_id):  # constructor
        self.env = env
        self.bike_id = bike_id

        self.agent_id = None
        self.location = None

        self.history = []
        self.print = False

    def init_state(self):
        state_keys = [
            "agent_id",
            "mode",
            "station_pull",
            "time_pull",
            "station_push",
            "time_push",
            "location_unlock",
            "time_unlock",
            "location_drop",
            "time_drop",
            "location_call",
            "time_call",
            "location_meet",
            "time_meet",
        ]
        self.state = dict.fromkeys(state_keys)

    def set_agent(self, agent_id):
        self.agent_id = agent_id

    def set_location(self, location):
        self.location = location

    def pop_agent(self):
        self.agent_id = None

    def rented(self):
        return self.agent_id is not None

    def dist(self, a, b):
        return np.linalg.norm(a - b)

    def ride(self, location):
        distance = self.dist(self.location, location)
        yield self.env.timeout(distance)
        self.location = location

class StationBike(Bike):
    def __init__(self, env, bike_id):
        super().__init__(env, bike_id)
        self.station_id = None
        self.init_state()

    def init_state(self):
        state_keys = [
            "agent_id",
            "station_pull",
            "time_pull",
            "station_push",
            "time_push",
        ]
        self.state = dict.fromkeys(state_keys)

    def save_state(self):
        self.history.append(self.state)
        self.init_state()

    def set_station(self, station_id):
        self.station_id = station_id

    def pop_station(self):
        self.station_id = None

    def register_pull(self, agent_id):
        if self.print:
            print("[%.2f] Bike %d pulled from station %d by agent %d" % (self.env.now, self.bike_id, self.station_id, agent_id))

        self.state["agent_id"] = agent_id
        self.state["station_pull"] = self.station_id
        self.state["time_pull"] = np.round(self.env.now, 2)
        self.set_agent(agent_id)
        self.pop_station()

    def register_push(self, station_id):
        if self.print:
            print("[%.2f] Bike %d pushed on station %d by agent %d" % (self.env.now, self.bike_id, station_id, self.agent_id))

        self.state["station_push"] = station_id
        self.state["time_push"] = np.round(self.env.now, 2)
        self.save_state()
        self.pop_agent()
        self.set_station(station_id)

    def docked(self):
        return self.station_id is not None

class DocklessBike(Bike):
    def __init__(self, env, bike_id):
        super().__init__(env, bike_id)
        self.init_state()

    def init_state(self):
        state_keys = [
            "agent_id",
            "location_unlock",
            "time_unlock",
            "location_lock",
            "time_lock",
        ]
        self.state = dict.fromkeys(state_keys)

    def save_state(self):
        self.history.append(self.state)
        self.init_state()

    def unlock(self, agent_id):
        self.set_agent(agent_id)
        self.state["agent_id"] = agent_id
        self.state["time_unlock"] = np.round(self.env.now, 2)
        self.state["location_unlock"] = self.location

        if self.print:
            print("[%.2f] Dockless bike %d unlocked by agent %d on location [%.2f, %.2f]" % (self.env.now, self.bike_id, agent_id, *self.location))

    def lock(self):
        if self.print:
            print("[%.2f] Dockless bike %d locked by agent %d on location [%.2f, %.2f]" % (self.env.now, self.bike_id, self.agent_id, *self.location))

        self.state["location_lock"] = self.location
        self.state["time_lock"] = np.round(self.env.now, 2)
        self.pop_agent()
        self.save_state()

class AutonomousBike(Bike):
    def __init__(self, env, bike_id):
        super().__init__(env, bike_id)
        self.init_state()

    def init_state(self):
        state_keys = [
            "agent_id",
            "location_call",
            "time_call",
            "location_meet",
            "time_meet",
            "location_drop",
            "time_drop",
        ]
        self.state = dict.fromkeys(state_keys)

    def save_state(self):
        self.history.append(self.state)
        self.init_state()

    def call(self, agent_id):
        self.set_agent(agent_id)
        self.state["agent_id"] = agent_id
        self.state["time_call"] = np.round(self.env.now, 2)
        self.state["location_call"] = self.location

        if self.print:
            print("[%.2f] Autonomous bike %d called by agent %d" % (self.env.now, self.bike_id, agent_id))

    def autonomous_move(self, location):
        if self.print:
            print("[%.2f] Autonomous bike %d moving from [%.2f, %.2f] to [%.2f, %.2f]" % (self.env.now, self.bike_id, *self.location, *location))

        distance = self.dist(self.location, location)
        yield self.env.timeout(distance)

        if self.print:
            print("[%.2f] Autonomous bike %d moved from [%.2f, %.2f] to [%.2f, %.2f]" % (self.env.now, self.bike_id, *self.location, *location))

        self.location = location
        self.state["location_meet"] = self.location
        self.state["time_meet"] = np.round(self.env.now, 2)

    def drop(self):
        if self.print:
            print("[%.2f] Autonomous bike %d dropped by agent %d on location [%.2f, %.2f]" % (self.env.now, self.bike_id, self.agent_id, *self.location))

        self.state["location_drop"] = self.location
        self.state["time_drop"] = np.round(self.env.now, 2)
        self.pop_agent()
        self.save_state()

class Agent:
    def __init__(self, env, agent_id):
        self.env = env
        self.agent_id = agent_id
        self.grid = None
        self.stations = None
        self.bikes = None

        self.print = True
        self.history = []
        self.init_state()

    def init_state(self):
        state_keys = [
            "source",
            "target",
            "time_source",
            "time_target",
            "bike_id",
            "mode",
            "source_history",
            "target_history",
            "dockless_history",
            "autonomous_history",
        ]
        self.state = dict.fromkeys(state_keys)

    def set_data(self, grid, stations, bikes):
        self.grid = grid
        self.stations = stations
        self.bikes = bikes

    def start(self):
        self.location = None
        self.bike_id = None
        self.event_setup_task = self.env.event()

    def process(self):
        # 0-Setup
        self.setup_task()
        yield self.event_setup_task

        # 1-Init on source
        yield self.env.process(self.init_agent())

    def setup_task(self):
        # self.source = self.random_location()
        # self.target = self.random_location()
        self.source = np.array([0, 0]) + np.random.normal(0, 1, size=2)
        self.target = np.array([100, 100]) + np.random.normal(0, 1, size=2)

        self.time_source = np.round(self.random_time(), 2)
        self.time_target_desired = np.round(self.time_source + self.random_time(), 2)

        self.event_setup_task.succeed()

        if self.print:
            print("[%.2f] Agent %d task created" % (self.env.now, self.agent_id))
            print("[%.2f] Agent %d source location: (%.2f, %.2f) at %.2f" % (self.env.now, self.agent_id, *self.source, self.time_source))
            print("[%.2f] Agent %d target location: (%.2f, %.2f) at %.2f" % (self.env.now, self.agent_id, *self.target, self.time_target_desired))

    def init_agent(self):
        yield self.env.timeout(self.time_source)
        self.location = self.source
        if self.print:
            print("[%.2f] Agent %d initialized at location [%.2f, %.2f]" % (self.env.now, self.agent_id, *self.location))

    def random_location(self):
        x = np.random.randint(*self.grid["xlim"])
        y = np.random.randint(*self.grid["ylim"])
        return np.array([x, y])

    def random_time(self):
        return np.random.randint(0, 10)  # 10

    def dist(self, a, b):
        return np.linalg.norm(a - b)

    def walk_to(self, location):
        distance = self.dist(self.location, location)
        yield self.env.timeout(distance)
        self.location = location

        if self.print:
            print("[%.2f] Agent %d walked to location [%.2f, %.2f]" % (self.env.now, self.agent_id, *location))

    def ride_bike_to(self, location):
        if self.print:
            print("[%.2f] Agent %d heads from location [%.2f, %.2f] to location [%.2f, %.2f] on bike %d" % (self.env.now, self.agent_id, *self.location, *location, self.bike_id))

        bike = self.bikes[self.bike_id]
        yield self.env.process(bike.ride(location))

        if self.print:
            print("[%.2f] Agent %d reached from location [%.2f, %.2f] to location [%.2f, %.2f] on bike %d" % (self.env.now, self.agent_id, *self.location, *location, self.bike_id))

        self.location = location

class StationAgent(Agent):
    def __init__(self, env, agent_id):
        super().__init__(env, agent_id)
        self.init_state()

    def init_state(self):
        state_keys = [
            "source",
            "target",
            "time_source",
            "time_target",
            "bike_id",
            "station_history",
        ]
        self.state = dict.fromkeys(state_keys)

    def save_state(self):
        self.state["source"] = self.source.tolist()
        self.state["target"] = self.target.tolist()
        self.state["time_source"] = self.time_source
        self.state["time_target"] = self.time_target
        self.state["bike_id"] = self.bike_id
        self.state["station_history"] = self.station_history
        self.history.append(self.state)
        self.init_state()

    def save_station_history(self, aim):
        history = {
            "aim": aim,
            "station_id": self.station_id,
            "time_select_station": self.time_select_station,
            "time_in_station": self.time_in_station,
            "time_interact_bike": self.time_interact_bike,
        }
        self.station_history.append(history)

    def start(self):
        super().start()

        # STATION-BASED
        self.station_id = None
        self.event_select_station = self.env.event()
        self.event_interact_bike = self.env.event()
        self.visited_stations = []
        self.station_history = []

        self.env.process(self.process())

    def process(self):
        # 0-Setup
        # 1-Init on source
        yield self.env.process(super().process())

        self.event_interact_bike = self.env.event()
        while not self.event_interact_bike.triggered:
            # 2-Select source station
            self.select_station(aim="source")

            # 3-Walk to source station
            yield self.event_select_station
            station = self.stations[self.station_id]
            yield self.env.process(self.walk_to(station.location))
            self.time_in_station = np.round(self.env.now, 2)

            # 4-Pull bike
            yield self.env.process(self.interact_bike(action="pull"))
            self.save_station_history(aim="source")

        self.event_interact_bike = self.env.event()
        while not self.event_interact_bike.triggered:
            # 5-Select target station
            self.select_station(aim="target")

            # 6-Ride bike
            yield self.event_select_station
            station = self.stations[self.station_id]
            yield self.env.process(self.ride_bike_to(station.location))
            self.time_in_station = np.round(self.env.now, 2)

            # 7-Push bike
            yield self.env.process(self.interact_bike(action="push"))
            self.save_station_history(aim="target")

        # 8-Walk to target
        yield self.env.process(self.walk_to(self.target))
        self.time_target = np.round(self.env.now, 2)

        # 9-Save state
        self.save_state()

        # 10-Finish
        yield self.env.timeout(10)
        if self.print:
            print("[%.2f] Agent %d working" % (self.env.now, self.agent_id))

    # STATION BASED

    def update_station_info(self, location):
        values = []
        for station in self.stations:
            station_id = station.station_id
            has_bikes = station.has_bikes()
            has_docks = station.has_docks()
            visited = station_id in self.visited_stations
            distance = self.dist(location, station.location)
            walkable = distance < WALK_RADIUS
            values.append((station_id, has_bikes, has_docks, visited, distance, walkable))
        labels = [
            "station_id",
            "has_bikes",
            "has_docks",
            "visited",
            "distance",
            "walkable",
        ]
        types = [int, int, int, int, float, int]
        dtype = list(zip(labels, types))
        self.station_info = np.array(values, dtype=dtype)

    def select_station(self, aim):
        self.event_select_station = self.env.event()
        location = self.location if aim == "source" else self.target  ###?????? aim== source

        self.update_station_info(location)
        for e in np.sort(self.station_info, order="distance"):
            if aim == "source":
                valid = e["has_bikes"] and not e["visited"] and e["walkable"]
            else:
                valid = e["has_docks"] and not e["visited"] and e["walkable"]
            if valid:
                self.station_id = e["station_id"]
                self.visited_stations.append(self.station_id)
                self.event_select_station.succeed()
                self.time_select_station = np.round(self.env.now, 2)
                if self.print:
                    print("[%.2f] Agent %d selected %s station %d" % (self.env.now, self.agent_id, aim, self.station_id))
                break
        if not self.event_select_station.triggered:
            print("estoy in da shit")
            # if we already pulled a bike, we are f***ed

            # call taxi / uber
            # walk to random station
            # walk towards target
            # wait and try again
            # change mode and start again
            # increase walkable distance
            # create new bike
            # stop

    def interact_bike(self, action):
        station = self.stations[self.station_id]
        valid = station.has_bikes() if action == "pull" else station.has_docks()
        if valid:
            if action == "pull":
                self.bike_id = station.choose_bike()
                self.bikes[self.bike_id].register_pull(self.agent_id)
                station.pull_bike(self.bike_id)  # HERE prints state of station
            else:
                self.bikes[self.bike_id].register_push(self.station_id)
                station.push_bike(self.bike_id)

            self.event_interact_bike.succeed()
            self.time_interact_bike = np.round(self.env.now, 2)
            if self.print:  # AND HERE the action, the order is counfusing
                print("[%.2f] Agent %d %sed bike %d on station %d" % (self.env.now, self.agent_id, action, self.bike_id, self.station_id,))
            yield self.env.timeout(1)
        else:
            self.time_interact_bike = None
            if self.print:
                print("[%.2f] Station %d has zero %s available" % (self.env.now, self.station_id, "bikes" if action == "pull" else "docks",))
            yield self.env.timeout(3)

class DocklessAgent(Agent):
    def __init__(self, env, agent_id):
        super().__init__(env, agent_id)
        self.init_state()

    def init_state(self):
        state_keys = [
            "source",
            "target",
            "time_source",
            "time_target",
            "bike_id",
            "dockless_history",
        ]
        self.state = dict.fromkeys(state_keys)

    def save_state(self):
        self.state["source"] = self.source.tolist()
        self.state["target"] = self.target.tolist()
        self.state["time_source"] = self.time_source
        self.state["time_target"] = self.time_target
        self.state["bike_id"] = self.bike_id
        self.state["dockless_history"] = self.dockless_history
        self.history.append(self.state)
        self.init_state()

    def save_dockless_history(self):
        history = {
            "dockless_bike_id": self.dockless_bike_id,
            "time_select_bike": self.time_select_bike,
            "time_in_dockless_bike": self.time_in_dockless_bike,
            "time_unlock_bike": self.time_unlock_bike,
        }
        self.dockless_history.append(history)

    def start(self):
        super().start()

        # DOCKLESS
        self.dockless_bike_id = None
        self.dockless_history = []

        self.event_select_dockless_bike = self.env.event()
        self.event_unlock_bike = self.env.event()

        self.env.process(self.process())

    def process(self):
        # 0-Setup
        # 1-Init on source
        yield self.env.process(super().process())

        while not self.event_unlock_bike.triggered:
            # 2-Select dockless bike
            self.select_dockless_bike()

            # 3-Walk to dockless bike
            yield self.event_select_dockless_bike
            dockless_bike = self.bikes[self.dockless_bike_id]
            yield self.env.process(self.walk_to(dockless_bike.location))
            self.time_in_dockless_bike = np.round(self.env.now, 2)

            # 4-Unlock bike
            yield self.env.process(self.unlock_bike())
            self.save_dockless_history()

        # 5-Ride bike
        yield self.env.process(self.ride_bike_to(self.target))

        # 6-Drop bike
        self.lock_bike()
        self.time_target = np.round(self.env.now, 2)

        # 7-Save state
        self.save_state()

        # 8-Finish
        yield self.env.timeout(10)
        if self.print:
            print("[%.2f] Agent %d working" % (self.env.now, self.agent_id))

    def update_bike_info(self):
        values = []
        for bike in self.bikes:
            if isinstance(bike, DocklessBike):
                bike_id = bike.bike_id
                rented = bike.rented()
                distance = self.dist(self.location, bike.location)
                walkable = distance < WALK_RADIUS
                values.append((bike_id, rented, distance, walkable))
        labels = ["bike_id", "rented", "distance", "walkable"]
        types = [int, int, float, int]
        dtype = list(zip(labels, types))
        self.bike_info = np.array(values, dtype=dtype)

    def select_dockless_bike(self):
        self.event_select_dockless_bike = self.env.event()
        self.update_bike_info()
        for e in np.sort(self.bike_info, order="distance"):
            # print(e)
            valid = not e["rented"] and e["walkable"]
            if valid:
                self.dockless_bike_id = e["bike_id"]
                self.event_select_dockless_bike.succeed()
                self.time_select_bike = np.round(self.env.now, 2)
                if self.print:
                    print("[%.2f] Agent %d selected dockless bike %d" % (self.env.now, self.agent_id, self.dockless_bike_id))
                break
        if not self.event_select_dockless_bike.triggered:
            print("estoy in da shit")
            # call taxi / uber
            # walk towards target
            # wait and try again
            # change mode and start again
            # increase walkable distance
            # create new bike
            # stop

    def unlock_bike(self):
        dockless_bike = self.bikes[self.dockless_bike_id]
        if not dockless_bike.rented():
            yield self.env.timeout(1)
            self.bike_id = dockless_bike.bike_id
            dockless_bike.unlock(self.agent_id)
            # HERE IT DOESNT SAVE THAT IT HAS BEEN RENTED
            self.event_unlock_bike.succeed()
            self.time_unlock_bike = np.round(self.env.now, 2)
            if self.print:
                print("[%.2f] Agent %d unlocked bike %d" % (self.env.now, self.agent_id, self.bike_id))
        else:
            yield self.env.timeout(3)
            self.time_unlock_bike = None
            if self.print:
                print("[%.2f] Bike %d has already been rented" % (self.env.now, self.dockless_bike_id))

    def lock_bike(self):
        bike = self.bikes[self.bike_id]
        bike.lock()

        if self.print:
            print("[%.2f] Agent %d locked bike %d" % (self.env.now, self.agent_id, self.bike_id))

class AutonomousAgent(Agent):
    def __init__(self, env, agent_id):
        super().__init__(env, agent_id)
        self.init_state()

    def init_state(self):
        state_keys = [
            "source",
            "target",
            "time_source",
            "time_target",
            "bike_id",
            "autonomous_history",
        ]
        self.state = dict.fromkeys(state_keys)

    def save_state(self):
        self.state["source"] = self.source.tolist()
        self.state["target"] = self.target.tolist()
        self.state["time_source"] = self.time_source
        self.state["time_target"] = self.time_target
        self.state["bike_id"] = self.bike_id
        self.state["autonomous_history"] = {
            "time_call_bike": self.time_call_bike,
            "time_meet_bike": self.time_meet_bike,
        }
        self.history.append(self.state)
        self.init_state()

    def start(self):
        super().start()

        # AUTONOMOUS
        self.event_call_autonomous_bike = self.env.event()
        self.autonomous_history = []

        self.env.process(self.process())

    def process(self):
        # 0-Setup
        # 1-Init on source
        yield self.env.process(super().process())

        # 2-Call autonomous bike
        self.call_autonomous_bike()
        yield self.event_call_autonomous_bike

        # 3-Wait for autonomous bike
        autonomous_bike = self.bikes[self.bike_id]
        yield self.env.process(autonomous_bike.autonomous_move(self.location))
        self.time_meet_bike = np.round(self.env.now, 2)

        # 4-Ride bike
        yield self.env.process(self.ride_bike_to(self.target))

        # 5-Drop bike
        self.drop_bike()
        self.time_target = np.round(self.env.now, 2)

        # 6-Save state
        self.save_state()

        # 7-Finish
        yield self.env.timeout(10)
        if self.print:
            print("[%.2f] Agent %d working" % (self.env.now, self.agent_id))

    def update_bike_info(self):
        values = []
        for bike in self.bikes:
            if isinstance(bike, AutonomousBike):
                bike_id = bike.bike_id
                rented = bike.rented()
                distance = self.dist(self.location, bike.location)
                walkable = distance < WALK_RADIUS
                values.append((bike_id, rented, distance, walkable))
        labels = ["bike_id", "rented", "distance", "walkable"]
        types = [int, int, float, int]
        dtype = list(zip(labels, types))
        self.bike_info = np.array(values, dtype=dtype)

    def call_autonomous_bike(self):
        self.event_call_autonomous_bike = self.env.event()
        self.update_bike_info()
        for e in np.sort(self.bike_info, order="distance"):
            # print(e)
            valid = not e["rented"] and e["walkable"]
            if valid:
                self.bike_id = e["bike_id"]
                self.bikes[self.bike_id].call(self.agent_id)
                self.event_call_autonomous_bike.succeed()
                self.time_call_bike = np.round(self.env.now, 2)
                if self.print:
                    print("[%.2f] Agent %d called autonomous bike %d" % (self.env.now, self.agent_id, self.bike_id))
                break
        if not self.event_call_autonomous_bike.triggered:
            print("estoy in da shit")
            # call taxi / uber
            # walk to random station
            # walk towards target
            # wait and try again
            # change mode and start again
            # increase walkable distance
            # create new bike
            # stop

    def drop_bike(self):
        bike = self.bikes[self.bike_id]
        bike.drop()

        if self.print:
            print("[%.2f] Agent %d dropped bike %d" % (self.env.now, self.agent_id, self.bike_id))

env = simpy.Environment()
city = City(env, data)
env.run(until=1000)

# %%

stations = []
for station_id, station in enumerate(city.stations):
    df = pd.DataFrame(station.history).assign(station_id=station_id)
    stations.append(df)
df_stations = pd.concat(stations).set_index("station_id").reset_index()

bikes = []
for bike_id, bike in enumerate(city.bikes):
    bike_type = type(bike).__name__
    df = pd.DataFrame(bike.history).assign(bike_id=bike_id, bike_type=bike_type)
    bikes.append(df)
df_bikes = pd.concat(bikes, sort=False).set_index("bike_id").reset_index()

agents = []
for agent_id, agent in enumerate(city.agents):
    agent_type = type(agent).__name__
    df = pd.DataFrame(agent.history).assign(agent_id=agent_id, agent_type=agent_type)
    agents.append(df)

df_agents = pd.concat(agents).set_index("agent_id").reset_index()


import cv2
import matplotlib.pyplot as plt
#import pylab
import numpy as np
import close_window
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((8*11,3), np.float32)
objp[:,:2] = np.mgrid[0:11,0:8].T.reshape(-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space #有定位real world的坐標系從(0,0,0)
imgpoints = [] # 2d points in image plane.

img = []
gray = []
for i in range(1,16) :
    tmp = cv2.imread(r'./Datasets/Q2_Image/'+ str(i) + '.bmp' )
    img.append(tmp)

    gray_tmp = cv2.cvtColor(img[i-1],cv2.COLOR_BGR2GRAY)
    gray.append(gray_tmp)

    ret , corner = cv2.findChessboardCorners(img[i-1],(11,8),None)

    if ret == True :
        objpoints.append(objp)
        #corner2 = cv2.cornerSubPix(gray[i-1],corner,(11,8),(-1,-1),criteria)
        imgpoints.append(corner)
        cv2.drawChessboardCorners(img[i-1],(11,8),corner,ret)

def find_corners() :
    plt.ion()
    for i in range(1,16) :
        plt.imshow(img[i-1])
        plt.show()
        plt.pause(1)
        plt.clf()

"""        cv2.namedWindow("find corner" , cv2.WINDOW_NORMAL)
        cv2.resizeWindow('find corner',img[i-1].shape[0],img[i-1].shape[1])
        cv2.imshow('find corner',img[i-1])
        close_window.close()
        cv2.destroyAllWindows()"""

ret, intrisic_mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray[0].shape[::-1], None, None) ##可得intrinsic matrix

def find_intrinsic() :
    np.set_printoptions(suppress=True)
    print("intrinsic_matrix : \n" , np.around(intrisic_mtx , decimals = 5))

def find_extrinsic(number) :
    rvecs_3x3  , jacobian = cv2.Rodrigues(rvecs[number-1],None,None) ##可得extrinsic matrix 之 rotation matrix
    extrinsic_mtx = np.hstack((rvecs_3x3,tvecs[number-1]))
    print("extrinsic matrix : \n" , extrinsic_mtx)

def find_distortion() :
    print("distortion matrix : " , dist)