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

LEN = 40
x = (np.random.rand(LEN,2) - 0.5) * 10
line = np.random.rand(2,1)
label = np.sign(np.dot(x, line))
label[label == 0] = 1

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
idx_0 = np.where(label[:, 0] == -1)

# error label point
#label[list(idx_0[0])[0], 0] = 1
#idx_0 = np.where(label[:, 0] == -1)

p0 = ax.scatter(x[idx_0,1], x[idx_0,0], marker = '+', color = 'c', label='-1', s = 50)
idx_1 = np.where(label[:, 0] == 1)
p1 = ax.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 50)
plt.title('PLA')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim((-6,6))
plt.ylim((-6,6))
plt.legend()
plt.grid(True)
#use ion to show onetime and disapear
plt.ion()
plt.show()

cnt = 0
pla = np.zeros((2,1))
_update = 0

while cnt<LEN:
    _update = np.sign(np.dot(x[cnt,:], pla)) * label[cnt, 0]
    if _update <= 0:
        _x = x[cnt, :]
        _x.shape = (1,2)
        _x = np.transpose(_x)
        pla = pla + _x*label[cnt, 0]
        cnt = 0
    else:
        cnt = cnt+1
        continue
    try:
        ax.lines.remove(lines[0])
    except Exception:
        pass
    if pla[0, :] == 0:
        line_x = 0
        line_y = np.linspace(-5,5,50)
        lines = ax.plot(line_x, line_y, 'r-', lw=2)
    else:
        k = -pla[1,:]/pla[0,:]
        line_x = np.linspace(-5,5,50)
        line_y = k*line_x
        lines = ax.plot(line_x, line_y, 'r-', lw=2)
    plt.pause(0.5)
plt.ioff()
plt.show()
print(pla)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from numpy import array
import csv

tf.compat.v1.disable_v2_behavior()

""" file1 = 'Student_Performance.csv'
data_set1 = []
with open(file1, mode='r', encoding = 'utf-8-sig') as file:
    reader = csv.reader(file)
    for row in reader:
        data_set1.append(row)
in_data1 = np.array(data_set1)
print(in_data1)
print(in_data1[:][0]) """

df = pd.read_csv('Student_Performance.csv')
in_data = array(df.iloc[:,:])
hours_studies = np.array(in_data[:, 0])
print(hours_studies)
performance_index = np.array(in_data[:, 5])

# Plot of Training Data
plt.scatter(hours_studies, performance_index)
plt.xlabel('hours_studies')
plt.ylabel('performance_index')
plt.title("Training Data")
plt.show()

X = tf.compat.v1.placeholder(tf.float32, shape=None)
Y = tf.compat.v1.placeholder(tf.float32, shape=None)

n = len(hours_studies)
print(n)
# khởi tạo biến w và b
W = tf.Variable(np.random.randn(), name = "W")
print(W)
b = tf.Variable(np.random.randn(), name = "b")
# thiết lập tốc độ học
learning_rate = 0.01
# số vòng lặp
training_epochs = 200
# Hàm tuyến tính
y_pred = tf.compat.v1.add(tf.multiply(X, W), b)

# Mean Squared Error Cost Function
cost = tf.compat.v1.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n)

# Tối ưu bằng Gradient Descent
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Thiết lập Global Variables
init = tf.compat.v1.global_variables_initializer()
# Starting the Tensorflow Session
with tf.compat.v1.Session() as sess:

    # Initializing the Variables
    sess.run(init)

    # Iterating through all the epochs
    for epoch in range(training_epochs):

        # Feeding each data point into the optimizer using Feed Dictionary
        for (_x, _y) in zip(hours_studies, performance_index):
            sess.run(optimizer, feed_dict = {X : _x, Y : _y})

        # Displaying the result after every 50 epochs
        if (epoch + 1) % 50 == 0:
            # Calculating the cost a every epoch
            c = sess.run(cost, feed_dict = {X : hours_studies, Y : performance_index})
            print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))

    training_cost = sess.run(cost, feed_dict ={X : hours_studies, Y : performance_index})
    weight = sess.run(W)
    bias = sess.run(b)
# Calculating the predictions
predictions = weight * hours_studies + bias
print("Training cost =", training_cost, "Weight =", weight, "bias =", bias, '\n')
# Plotting the Results
plt.plot(hours_studies, performance_index, 'ro', label ='Original data')
plt.plot(hours_studies, predictions, label ='Fitted line')
plt.title('Linear Regression Result')
plt.legend()
plt.show()


import os
from datetime import datetime
from pathlib import Path
import json

def create_log_file_name(coin):
    logdir = Path.cwd() / Path('logs')
    if not logdir.exists():
        logdir.mkdir()

    if logdir.exists() and logdir.is_dir():
        date_format = datetime.now().strftime('%Y_%m_%d-%H%M%S')
        filename = f'{date_format}_{coin}.log'
        log_file = logdir / filename
    return log_file

def write_log_header(file, coin):
    with open(file, 'a', encoding='UTF-8') as logfile:
        line_1 = f'========== {datetime.now().strftime("%d.%m.%Y %H:%M:%S")} =========\n'
        line_2 = f'============== {coin} ==============\n'
        logfile.write(line_1)
        logfile.write(line_2)

def write_buy_receipt(file, data):

    with open(file, 'a', encoding='UTF-8') as logfile:
        logfile.write(f'Куплена монета {data["symbol"]}\n')
        if len(data['fills']) > 0:
            fills = data['fills'][0]
            logfile.write(f'Стоимость: {fills["price"]}\n')
            logfile.write(f'Количество: {fills["qty"]}\n')
        else:
            logfile.write('Пустой массив fills\n')

def write_cell_receipt(file, data):
    with open(file, 'a', encoding='UTF-8') as logfile:
        logfile.write('Продажа монеты\n')

        if len(data['fills']) > 0:
            for row in data['fills']:
                logfile.write('Раздел квитка\n')
                logfile.write(f'Стоимость продажи: {row["price"]}\n')
                logfile.write(f'Продано количесво: {row["qty"]}\n')
        else:
            logfile.write('Опять пустой массив fills\n')

def write_error(file, error):
    with open(file, 'a', encoding='UTF-8') as logfile:
        logfile.write('=== Произошла ошибка ===\n')
        # logfile.write(f'Статус код: {error.status_code}\n')
        # logfile.write(f'Ответ: {error.response}\n')
        # logfile.write(f'Код ошибки: {error.code}\n')
        # logfile.write(f'Описание: {error.message}\n')
        # logfile.write(f'Запрос: {error.request}\n')
        logfile.write(str(error))


import typing
from decimal import Decimal
from datetime import datetime

import pydantic
from pyrsistent import pmap
from typing_extensions import Literal, TypedDict

from noobit_markets.base.request import (
    # retry_request,
    _validate_data,
)

# Base
from noobit_markets.base import ntypes
from noobit_markets.base.models.result import Result, Err
from noobit_markets.base.models.rest.response import (
    NoobitResponseSymbols,
    NoobitResponseTrades,
    T_PublicTradesParsedItem,
    T_PublicTradesParsedRes,
)
from noobit_markets.base.models.rest.request import NoobitRequestTrades
from noobit_markets.base.models.frozenbase import FrozenBaseModel

# FTX
from noobit_markets.exchanges.ftx import endpoints
from noobit_markets.exchanges.ftx.rest.base import get_result_content_from_req

__all__ = "get_trades_ftx"

# ============================================================
# FTX REQUEST
# ============================================================

class FtxLimit(ntypes.NInt):
    ge = 0
    le = 100
    strict = False

class FtxRequestTrades(FrozenBaseModel):
    # https://docs.ftx.com/?python#get-trades

    market_name: str
    limit: typing.Optional[FtxLimit]
    start_time: typing.Optional[pydantic.PositiveInt]
    end_time: typing.Optional[pydantic.PositiveInt]

# hint fields to mypy
class _ParsedReq(TypedDict):
    market_name: typing.Any
    limit: typing.Any
    start_time: typing.Any
    end_time: typing.Any

def parse_request(
    valid_request: NoobitRequestTrades,
) -> _ParsedReq:

    payload: _ParsedReq = {
        "market_name": valid_request.symbols_resp.asset_pairs.get(
            valid_request.symbol
        ).exchange_pair,
        "limit": 100,
        "start_time": valid_request.since,
        "end_time": None
        # noobit ts are in ms vs ohlc kraken ts in s
    }

    return payload

# ============================================================
# FTX RESPONSE
# ============================================================

# SAMPLE RESPONSE
# {
#   "success": true,
#   "result": [
#     {
#       "id": 3855995,
#       "liquidation": false,
#       "price": 3857.75,
#       "side": "buy",
#       "size": 0.111,
#       "time": "2019-03-20T18:16:23.397991+00:00"
#     }
#   ]
# }

class FtxResponseItemTrades(FrozenBaseModel):

    id: pydantic.PositiveInt
    liquidation: bool
    price: Decimal
    side: Literal["buy", "sell"]
    size: Decimal
    time: str

class FtxResponseTrades(FrozenBaseModel):

    trades: typing.Tuple[FtxResponseItemTrades, ...]

def parse_result(
    result_data: FtxResponseTrades, symbol: ntypes.SYMBOL
) -> T_PublicTradesParsedRes:

    parsed_trades = [_single_trade(data, symbol) for data in result_data.trades]

    return tuple(parsed_trades)

def _single_trade(
    data: FtxResponseItemTrades, symbol: ntypes.SYMBOL
) -> T_PublicTradesParsedItem:

    parsed: T_PublicTradesParsedItem = {
        "symbol": symbol,
        "orderID": None,
        "trdMatchID": data.id,
        # noobit timestamp = ms
        "transactTime": datetime.timestamp(datetime.fromisoformat(data.time)),
        "side": data.side.upper(),
        # binance only lists market order
        # => trade = limit order lifted from book by market order
        # FIXME change model to allow None
        "ordType": "MARKET",
        "avgPx": data.price,
        "cumQty": data.size,
        "grossTradeAmt": data.price * data.size,
        "text": None,
    }

    return parsed

# ============================================================
# FETCH
# ============================================================

# @retry_request(retries=pydantic.PositiveInt(10), logger=lambda *args: print("===xxxxx>>>> : ", *args))
async def get_trades_ftx(
    client: ntypes.CLIENT,
    symbol: ntypes.SYMBOL,
    symbols_resp: NoobitResponseSymbols,
    since: typing.Optional[ntypes.TIMESTAMP] = None,
    #  prevent unintentional passing of following args
    *,
    logger: typing.Optional[typing.Callable] = None,
    base_url: pydantic.AnyHttpUrl = endpoints.FTX_ENDPOINTS.public.url,
    endpoint: str = endpoints.FTX_ENDPOINTS.public.endpoints.trades,
) -> Result[NoobitResponseTrades, pydantic.ValidationError]:

    symbol_to_exchange = lambda x: {
        k: v.exchange_pair for k, v in symbols_resp.asset_pairs.items()
    }[x]

    # ftx has variable urls besides query params
    # format: https://ftx.com/api/markets/{market_name}/candles
    req_url = "/".join([base_url, "markets", symbol_to_exchange(symbol), endpoint])
    method = "GET"
    headers: typing.Dict = {}

    valid_noobit_req = _validate_data(
        NoobitRequestTrades,
        pmap({"symbol": symbol, "symbols_resp": symbols_resp, "since": since}),
    )
    if isinstance(valid_noobit_req, Err):
        return valid_noobit_req

    if logger:
        logger(f"Trades - Noobit Request : {valid_noobit_req.value}")

    parsed_req = parse_request(valid_noobit_req.value)

    valid_ftx_req = _validate_data(FtxRequestTrades, pmap(parsed_req))
    if valid_ftx_req.is_err():
        return valid_ftx_req

    if logger:
        logger(f"Trades - Parsed Request : {valid_ftx_req.value}")

    result_content = await get_result_content_from_req(
        client, "GET", req_url, valid_ftx_req.value, headers
    )
    if result_content.is_err():
        return result_content

    if logger:
        logger(f"Trades - Result Content : {result_content.value}")

    valid_result_content = _validate_data(
        FtxResponseTrades, pmap({"trades": result_content.value})
    )
    if valid_result_content.is_err():
        return valid_result_content

    parsed_result = parse_result(valid_result_content.value, symbol)

    valid_parsed_response_data = _validate_data(
        NoobitResponseTrades,
        pmap(
            {
                "trades": parsed_result,
                "rawJson": result_content.value,
                "exchange": "FTX",
            }
        ),
    )
    return valid_parsed_response_data


import pathlib

from enum import Enum

import streamlit as st
from streamlit_ace import st_ace, THEMES

from pollination_streamlit_io import (
    get_geometry,
    send_geometry,
    get_hbjson,
    send_hbjson,
    auth_user,
    select_account,
    select_project,
    select_recipe,
    select_run,
    select_study,
    select_cloud_artifact,
    create_study,
    recipe_inputs_form,
    send_results )

# needed for get_and_show_model.py
import requests

# needed for get_and_show_model.py
import json
from honeybee.model import Model as HBModel
from honeybee_vtk.model import Model as VTKModel
from pollination_streamlit_viewer import viewer

st.set_page_config(
    page_title='Tutorial App',
    page_icon='https://app.pollination.cloud/favicon.ico',
    initial_sidebar_state='collapsed',
)

from pollination_streamlit.selectors import get_api_client

# needed for get_and_show_model.py
# set_page_config needs to be called prior to this import
from templates.get_show_model import create_vtkjs, show_model

st.sidebar.image(
    'https://uploads-ssl.webflow.com/6035339e9bb6445b8e5f77d7/616da00b76225ec0e4d975ba'
    '_pollination_brandmark-p-500.png',
    use_column_width=True
)

# get api_client from pollination
api_client = get_api_client()

TEMPLATES = './templates'

class Command(Enum):
    GET_MODEL = 'Get Model'
    SEND_MODEL = 'Send Model'
    SEND_MODEL_FROM_HB = 'Send Model From HB'
    GET_GEOMETRY = 'Get Geometry'
    SEND_GEOMETRY = 'Send Geometry'
    SEND_GEOMETRY_FROM_LB = 'Send Geometry From LB'
    SEND_GEOMETRY_FROM_DICT = 'Send Geometry From Dict'
    SEND_RESULTS = 'Send Results'
    AUTH_USER = 'Authenticated User'
    SEL_ACCOUNT = 'Select Account'
    SEL_PROJECT = 'Select Project'
    SEL_RECIPE = 'Select Recipe'
    SEL_STUDY = 'Select Study'
    SEL_RUN = 'Select Run'
    RUN_STUDY_BASIC = 'Create Study'
    RUN_STUDY_PROGRESS = 'Study Progress'
    RUN_STUDY_INTERMEDIATE = 'Create Study by Recipe Form'
    RUN_STUDY_ADVANCED = 'Create Study by Recipe Form (Advanced)'
    DOWNLOAD_ARTIFACT = 'Select and Download an Artifact'
    GET_SHOW_RESULTS = 'Get and Show a Study Result (VSF)'
    GET_SHOW_MODEL = 'Get and Show a Hbjson Model'

SCRIPTS = {
    Command.GET_MODEL.value: pathlib.Path(TEMPLATES)
        .joinpath('get_model.py').read_text(),
    Command.SEND_MODEL_FROM_HB.value: pathlib.Path(TEMPLATES)
        .joinpath('send_model_from_hb.py').read_text(),
    Command.SEND_MODEL.value: pathlib.Path(TEMPLATES)
        .joinpath('send_model.py').read_text(),
    Command.GET_GEOMETRY.value: pathlib.Path(TEMPLATES)
        .joinpath('get_geometry.py').read_text(),
    Command.SEND_GEOMETRY.value: pathlib.Path(TEMPLATES)
        .joinpath('send_geometry.py').read_text(),
    Command.SEND_GEOMETRY_FROM_LB.value: pathlib.Path(TEMPLATES)
        .joinpath('send_geometry_from_lb.py').read_text(),
    Command.SEND_GEOMETRY_FROM_DICT.value: pathlib.Path(TEMPLATES)
        .joinpath('send_geometry_from_dict.py').read_text(),
    Command.SEND_RESULTS.value: pathlib.Path(TEMPLATES)
        .joinpath('send_results.py').read_text(),
    Command.AUTH_USER.value: pathlib.Path(TEMPLATES)
        .joinpath('auth_user.py').read_text(),
    Command.SEL_ACCOUNT.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_account.py').read_text(),
    Command.SEL_PROJECT.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_project.py').read_text(),
    Command.SEL_RECIPE.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_recipe.py').read_text(),
    Command.SEL_STUDY.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_study.py').read_text(),
    Command.SEL_RUN.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_run.py').read_text(),
    Command.RUN_STUDY_BASIC.value: pathlib.Path(TEMPLATES)
        .joinpath('run_study_basic.py').read_text(),
    Command.RUN_STUDY_PROGRESS.value: pathlib.Path(TEMPLATES)
        .joinpath('create_study_progress.py').read_text(),
    Command.RUN_STUDY_INTERMEDIATE.value: pathlib.Path(TEMPLATES)
        .joinpath('run_study_intermediate.py').read_text(),
    Command.RUN_STUDY_ADVANCED.value: pathlib.Path(TEMPLATES)
        .joinpath('run_study_advanced.py').read_text(),
    Command.DOWNLOAD_ARTIFACT.value: pathlib.Path(TEMPLATES)
        .joinpath('download_artifact.py').read_text(),
    Command.GET_SHOW_RESULTS.value: pathlib.Path(TEMPLATES)
        .joinpath('visualize_study_results.py').read_text(),
    Command.GET_SHOW_MODEL.value: pathlib.Path(TEMPLATES)
        .joinpath('get_show_model.py').read_text()
}

DOCS = {
    Command.GET_MODEL.value: get_hbjson.__doc__,
    Command.SEND_MODEL.value: send_hbjson.__doc__,
    Command.SEND_MODEL_FROM_HB.value: send_hbjson.__doc__,
    Command.GET_GEOMETRY.value: get_geometry.__doc__,
    Command.SEND_GEOMETRY.value: send_geometry.__doc__,
    Command.SEND_GEOMETRY_FROM_LB.value: get_geometry.__doc__,
    Command.SEND_GEOMETRY_FROM_DICT.value: send_geometry.__doc__,
    Command.SEND_RESULTS.value: send_results.__doc__,
    Command.AUTH_USER.value: auth_user.__doc__,
    Command.SEL_ACCOUNT.value: select_account.__doc__,
    Command.SEL_PROJECT.value: select_project.__doc__,
    Command.SEL_RECIPE.value: select_recipe.__doc__,
    Command.SEL_STUDY.value: select_study.__doc__,
    Command.RUN_STUDY_BASIC.value: create_study.__doc__,
    Command.RUN_STUDY_PROGRESS.value: create_study.__doc__,
    Command.RUN_STUDY_INTERMEDIATE.value: recipe_inputs_form.__doc__,
    Command.RUN_STUDY_ADVANCED.value: recipe_inputs_form.__doc__,
    Command.SEL_RUN.value: select_run.__doc__,
    Command.DOWNLOAD_ARTIFACT.value: select_cloud_artifact.__doc__,
    Command.GET_SHOW_RESULTS.value: viewer.__doc__,
    Command.GET_SHOW_MODEL.value: viewer.__doc__
}

def main():
    """Learning doing."""

    # title
    st.header('Select a tutorial!')

    # set tabs
    tab1, tab2, tab3 = st.tabs(["CAD Interactions", "Cloud Interactions", "Pollination Viewer"])

    # sidebar
    st.sidebar.markdown('## Editor Settings')
    st.sidebar.markdown('----------')
    auto_update = st.sidebar.checkbox(label='Auto update',
        value=True)
    font_size = st.sidebar.number_input(label='Font size',
        value=15, min_value=1, max_value=30)
    theme = st.sidebar.selectbox(
        label='Select a theme', options=THEMES, index=5)

    # tabs layout
    with tab1:
        mod_option = st.selectbox(
            'Select a script to test',
            (Command.SEND_MODEL.value,
            Command.SEND_MODEL_FROM_HB.value,
            Command.GET_MODEL.value,
            Command.SEND_GEOMETRY.value,
            Command.SEND_GEOMETRY_FROM_LB.value,
            Command.SEND_GEOMETRY_FROM_DICT.value,
            Command.GET_GEOMETRY.value,
            Command.SEND_RESULTS.value))
        with st.expander(label='Docs', expanded=False):
            st.markdown(DOCS[mod_option])
        mod_script = st_ace(language="python",
            value=SCRIPTS[mod_option],
            auto_update=auto_update,
            font_size=font_size,
            theme=theme)
        exec(mod_script)

    with tab2:
        sel_option = st.selectbox(
            'Select a script to test',
            (Command.AUTH_USER.value,
            Command.SEL_ACCOUNT.value,
            Command.SEL_PROJECT.value,
            Command.SEL_RECIPE.value,
            Command.SEL_STUDY.value,
            Command.SEL_RUN.value,
            Command.RUN_STUDY_BASIC.value,
            Command.RUN_STUDY_PROGRESS.value,
            Command.DOWNLOAD_ARTIFACT.value,
            Command.RUN_STUDY_INTERMEDIATE.value,
            Command.RUN_STUDY_ADVANCED.value))
        with st.expander(label='Docs', expanded=False):
            st.markdown(DOCS[sel_option])
        sel_script = st_ace(language="python",
            value=SCRIPTS[sel_option],
            auto_update=auto_update,
            font_size=font_size,
            theme=theme)
        exec(sel_script)

    with tab3:
        viewer_option = st.selectbox(
            'Select a script to test',
            ([Command.GET_SHOW_RESULTS.value, \
              Command.GET_SHOW_MODEL.value]))
        with st.expander(label='Docs', expanded=False):
            st.markdown(DOCS[viewer_option])
        viewer_script = st_ace(language="python",
            value=SCRIPTS[viewer_option],
            auto_update=auto_update,
            font_size=font_size,
            theme=theme)
        exec(viewer_script)

if __name__ == '__main__':
    main()


from bs4 import BeautifulSoup
import requests
import re
import urllib3
import os
import pandas as pd

def ConsoleClear():
    os.system('cls' if os.name == 'nt' else 'clear')

ConsoleClear()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

game = input("What game do you want to search for?\n")

url = "https://www.newegg.com/p/pl?d=ps5+games"
page = requests.get(url, verify=False).text
doc = BeautifulSoup(page, "html.parser")

page_text = doc.find(class_="list-tool-pagination-text").strong
pages = int(str(page_text).split("/")[-2].split(">")[-1][:-1])

items_found = {}

for page in range(1, pages + 1):
    url = f"https://www.newegg.com/p/pl?d=ps5+games&page={page}"
    page = requests.get(url, verify=False).text
    doc = BeautifulSoup(page, "html.parser")
    div = doc.find(
        class_="item-cells-wrap border-cells items-grid-view four-cells expulsion-one-cell"
    )

    items = div.find_all(string=re.compile(game))
    for item in items:
        parent = item.parent
        link = None
        if parent.name != "a":
            continue

        link = parent["href"]
        next_parent = item.find_parent(class_="item-container")
        try:
            price_element = next_parent.find(class_="price-current")
            if price_element:
                price = price_element.find("strong").get_text(strip=True)
            else:
                price = "N/A"

            items_found[item] = {"price": price, "link": link}
        except:
            pass

sorted_items = sorted(items_found.items(), key=lambda x: x[1]["price"])

for item in sorted_items:
    print(item[0])
    print(f"${item[1]['price']}")
    print(item[1]["link"])
    print("----------------------------------------------")


import json
import requests

class TelegramBot:
    """
    Telegram bot class
    """

    def __init__(self, token: str):
        """
        Initialize the class.
        :param token: telegram bot token, given by BotFather.
        """
        self.base_url = "https://api.telegram.org/bot"
        self.token = token
        self.updates = None
        self.last_id = None

    def get_updates(self):
        """
        stores new incoming updates.
        """
        url = f"{self.base_url}{self.token}/getUpdates"
        if self.last_id:
            url = f"{url}?offset={self.last_id}"
        self.updates = requests.get(url).json()["result"]
        self.set_last_id()

    def set_last_id(self):
        """
        set the current last update_id.
        """
        if len(self.updates) > 0:
            self.last_id = self.updates[-1]["update_id"] + 1

    def send_message(self, chat_id: int, text: str):
        """
        Sends a message to a telegram user.
        :param chat_id: Unique identifier user_id / user_name / chat_id
        :param text: Text of the message to be sent
        """
        url = f"{self.base_url}{self.token}/sendMessage"
        params = {"chat_id": int(chat_id), "text": text}
        requests.get(url, params=params)

    def send_video(self, chat_id: int, video):
        """
        Sends a video to a telegram user.
        :param chat_id: Unique identifier user_id / user_name / chat_id
        :param video: video file to be sent
        """
        url = f"{self.base_url}{self.token}/sendVideo"
        data = {"chat_id": int(chat_id)}
        files = {"video": video}
        requests.post(url, data=data, files=files)

    def request_contact(self, chat_id: int, text: str = "Your Contact:"):
        """
        Asking the user to give back is phone number.
        :param chat_id: Unique identifier user_id / user_name / chat_id
        :param text: Text of the message to be sent
        """
        url = f"{self.base_url}{self.token}/sendMessage"
        reply_markup = {
            "keyboard": [[{"text": "Press Me", "request_contact": True}]],
            "one_time_keyboard": True,
            "resize_keyboard": True,
        }
        params = {
            "chat_id": chat_id,
            "text": text,
            "reply_markup": json.dumps(reply_markup),
        }
        requests.get(url, params=params)


# -*- coding: utf-8 -*-
"""
Created on Sun Feb 28 22:10:13 2021

@author: shrut
"""

import networkx as nx
import random
import numpy as np

def add_edges(G, p=0.3):
    for u in G.nodes():
        for v in G.nodes():
            if (u != v):
                r = random.random()
                if (r <= p):
                    G.add_edge(u, v)
                else:
                    continue
    return G

def distribute_points(G, points):
    new_points = [0 for _ in range(G.number_of_nodes())]
    for u in G.nodes():
        outlinks = G.out_edges(u)
        if len(outlinks) == 0:
            new_points[u] += points[u]

        else:
            share = points[u]/len(outlinks)
            for link in outlinks:
                new_points[link[1]] += share
    return new_points

def taxation(G, points, s=0.8):
    for i in range(len(points)):
        points[i] = points[i]*s

    n = G.number_of_nodes()
    extra = (n * 100 * (1-s)) / n
    for i in range(len(points)):
        points[i] += extra

    return points

def distribute_points_till_conv(G, points):
    print("Enter # to stop")
    while(1):
        new_points = distribute_points(G, points)

        new_points = taxation(G, new_points)
        points = new_points
        print(points)
        c = input()
        if c == '#':
            break
    return points

def sort_nodes_by_points(points):
    points = np.array(points)
    sorted_nodes = np.argsort(-points)
    return sorted_nodes

def main():
    # Create the graph
    G = nx.DiGraph()
    G.add_nodes_from(list(range(10)))
    G = add_edges(G, p=0.3)

    # Distribute points
    points = [100 for _ in range(G.number_of_nodes())]
    print(points)
    points = distribute_points_till_conv(G, points)

    # Rank the nodes
    sorted_nodes = sort_nodes_by_points(points)
    print("Nodes sorted by points: ", sorted_nodes)

    # Compare with NetworkX's imolementation
    print("Result from the networkx implementation of PageRank(for comparision):")
    pr = nx.pagerank(G)
    pr_sorted = sorted(pr.items(), key=lambda x : x[1], reverse=True)

    for i in pr_sorted:
        print(i[0], end=' ')

main()


from pymongo import MongoClient
from pymongo.errors import ConnectionFailure

class Db:
    ARG_SERVER_URI_KEY = "server_uri"
    DEFAULT_SERVER_URI = "mongodb://mongo"

    DEFAULT_CONNECT_TIMEOUT = 2000

    def __init__(self, **kwargs):
        self.server = kwargs.get(Db.ARG_SERVER_URI_KEY, Db.DEFAULT_SERVER_URI)

        self.client = MongoClient(self.server, serverSelectionTimeoutMS=Db.DEFAULT_CONNECT_TIMEOUT)

    def connect(self):
        try:
            # The ismaster command is cheap and does not require auth.
            self.client.admin.command('ismaster')
            return True
        except ConnectionFailure:
            print("Server not available")
            return False

    def create_db(self, database_name):
        db = self.client[database_name]
        mycol = db["to_delete"]
        mydict = { "name": "John", "address": "Highway 37" }

        x = mycol.insert_one(mydict)
        print ("inserted id: {}".format(x.inserted_id))

    def drop_db(self, database_name):
        self.client.drop_database(database_name)

    def get_database_names(self):
        dbs = self.client.list_database_names()
        print("list databases: {}".format(dbs))
        return dbs


from fabric.api import lcd, task, settings, hide
from fabpolish import polish, sniff, local, info
from fabpolish.contrib import find_merge_conflict_leftovers
import os
from subprocess import call

ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

# @sniff(severity='major', timing='fast')
# def run_eslint():
#     info('Running ESLint...')
#     return local(
#         "git ls-files | "
#         "grep '\.js$' | "
#         "xargs ./node_modules/eslint/bin/eslint.js  --config=.eslintrc"
#     )

@sniff(severity='major', timing='fast')
def remove_compiled_classes():
    info('Removing compiled python classes...')
    local('pyclean .')
    return local(
        'find ./federation_api ./config -name "*.py[co]" -print0 | xargs -0 rm -f'
    )

@sniff(severity='major', timing='fast')
def fix_directory_permission():
    info('Fixing directory permissions...')
    return local(
        '! find ./federation_api ./config '
        '-path .git -prune -o '
        '-name .webassets-cache -prune -o '
        '-name gen -prune -o '
        '-type d -not -perm 0775 -print0 | '
        'tee /dev/stderr | '
        'xargs -0 chmod 0775 >/dev/null 2>&1'
    )

@sniff(severity='major', timing='fast')
def fix_file_permission():
    """Fixing permissions for files"""
    info('Fixing permissions for files...')
    return local(
        "git ls-files -z | "
        "grep -PvZz '\.sh$' | "
        "grep -PvZz 'tests/' | "
        "xargs -0 chmod -c 0664 > /dev/null 2>&1"
    )

@sniff(severity='major', timing='fast')
def check_code_standard():
    """Running coding standards check"""
    info('Running coding standards check...')
    return local(
        'pep8 ./federation_api ./config'
    )

@sniff(severity='major', timing='fast')
def static_code_analyzer():
    """Running static code analyzer"""
    info('Running static code analyzer...')
    return local(
        'pyflakes ./federation_api ./config'
    )

@sniff(severity='major', timing='fast')
def check_print_statement():
    """Checking for debug print statements"""
    info('Checking for debug print statements...')
    return local(
        '! find ./federation_api ./config -type f -name "*.py" -print0 | '
        'xargs -0 grep -Pn \'(?<![Bb]lue|>>> )print\' | '
        'grep -v NOCHECK'
    )

@sniff(severity='major', timing='fast')
def servername_compatibility():
    """Checking for no-servername compatibility"""
    info('Checking for no-servername compatibility...')
    return local(
        '! find ./federation_api ./config -name "default_settings.py" -o '
        ' -type f -name "*.py" -print0 | '
        'xargs -0 grep -Pn \'SERVER_NAME|ENABLE_HTTPS|SSO_DOMAIN|'
        '(SOFTWARE(_WEBSITE)?|EPICENTER|FABRIC|PVR|PRACTONAV)_HOST\''
    )

# @sniff(severity='major', timing='fast')
# def check_migrations():
#     """Checking migration branches"""
#     info('Checking migration branches...')
#     return local(
#         '! alembic branches | grep branchpoint'
#     )

@sniff(severity='major', timing='fast')
def run_tests():
    """Running tests"""
    info('Running tests...')
    value = 'PYTHONPATH=%s FLASK_ENV=TEST py.test --cov=. ./tests' % ROOT_DIR
    return local(
        value
    )

@task
def grep(term, flags=''):
    with lcd(ROOT_DIR), settings(hide('running')):
        local('find federation_api ./config tests '
              '-name "*~" -o '
              '-name "*.py[co]" -o '
              '-name "*.dot" -o '
              '-name "*.min.*" -o '
              '-name "gen" -prune -o '
              '-name ".webassets-cache" -prune -o '
              '-type d -o '
              '-print0 | '
              'xargs -0 grep -n --color=force ' + flags + ' "' + term + '"; '
              ' echo -n')


# Improting Image class from PIL module
from PIL import Image
# Importing argparse for adding argument parser
import argparse
# Impoting os module for creating folder
import os

# Cropper function
def cropper(croppedHeight, croppedWidth):
   # Giving first value to the counters
   nameCounter  = 1
   # i is counting x axis column of image
   for i in range(int(height/croppedHeight)):
      # j is counting y axis row of image
      for j in range(int(width/croppedWidth)):
         # Set the cropping area with box=(left, upper, right, lower).
         croppedImage = img.crop((croppedWidth*j, croppedWidth*i, croppedHeight*(j+1), croppedHeight*(i+1)))
         # Saving image to folder which created name as cropped_images
         croppedImage.save(os.getcwd() + "/cropped_images/image" + str(nameCounter) + ".png")
         print("[INFO] file created: " + os.getcwd() + "/cropped_images/image" + str(nameCounter) + ".png")
         # Adding one xAxisCounter and nameCounter after take a photo
         nameCounter  = nameCounter  + 1

if __name__ == "__main__":
   # Construct the argument parse and parse the arguments
   parser = argparse.ArgumentParser(description='Process command line arguments.')
   parser.add_argument('-path', "--path", required=True, help="path to file contain image which will crop")
   parser.add_argument('-cW', "--cw", required=True, help='cropped image width value', type=int)
   parser.add_argument('-cH', "--ch", required=True, help='cropped image Height value', type=int)
   args = parser.parse_args()

   # Height and width value that we want to crop
   croppedWidth = args.cw
   croppedHeight = args.ch

   # Open a image
   img = Image.open(args.path)

   # Size of the image in pixels (size of orginal image)
   width, height = img.size

   # Creating file for cropped images where is cropper.py file
   os.mkdir("cropped_images")

   # Function calling
   cropper(croppedHeight, croppedWidth)


import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder,StandardScaler
from sklearn.model_selection import KFold
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score,roc_auc_score,f1_score,precision_score

csvPath = r"C:\Users\YSK\Desktop\1030520813_patternrecognition_final_project\breast-cancer.csv"
dataset = pd.read_csv(csvPath)
sc = StandardScaler()

dataset = dataset.drop(['id'], axis=1)
le = LabelEncoder()
dataset['diagnosis'] = le.fit_transform(dataset['diagnosis'])

X = dataset.drop(columns=['diagnosis'])
y = dataset['diagnosis']

X = sc.fit_transform(X)
le = LabelEncoder()
y = le.fit_transform(y)

classifier = DecisionTreeClassifier()
kfold=KFold(n_splits=5)
accs =list()
for fold_index,(train_idx,test_idx) in enumerate(kfold.split(X,y)):
    print("Fold: ",fold_index+1)
    X_train,X_test = X[train_idx],X[test_idx]
    y_train,y_test = y[train_idx],y[test_idx]

    classifier.fit(X_train,y_train)
    acc = classifier.score(X_test,y_test)
    print("Accuracy in Fold: ",acc)
    accs.append(acc)

print("Mean Accuracy: ", sum(accs)/len(accs))
y_pred = classifier.predict(X_test)
cMatrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cMatrix)
print("Classification Report:\n", classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))
print('Sensitivity : ', cMatrix[0, 0]/(cMatrix[0, 0]+cMatrix[0, 1]))
print('Specificity : ', cMatrix[1, 1]/(cMatrix[1, 0]+cMatrix[1, 1]))
print('AUC :',roc_auc_score(y_test, y_pred))
print('f1-score:', f1_score(y_test, y_pred))
print('Precision: %.3f' % precision_score(y_test, y_pred))


import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import pandas as pd

service = Service(ChromeDriverManager().install())
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(service=service, options=chrome_options)
for page_index in range(1, 30):
    driver.implicitly_wait(2)
    table = driver.find_element(By.CLASS_NAME, 'zg_table')
    table_items = table.find_element(By.TAG_NAME, 'tbody').find_elements(By.TAG_NAME, 'tr')
    columns = table_items[0].find_elements(By.TAG_NAME, 'th')
    columns_row = []
    data_rows = []
    for column in columns:
        if column.text == '详情介绍':
            break
        columns_row.append(column.text)
    print("current_page:", page_index)
    for item in table_items:
        item_contents = item.find_elements(By.TAG_NAME, 'td')
        if len(item_contents) == 0:
            continue
        row = []
        for item_content in item_contents:
            if item_content.text == '职位详情':
                break
            row.append(item_content.text)
        data_rows.append(row)
    df = pd.DataFrame(data_rows, columns=columns_row)
    print(df)
    df.to_csv('./data.csv', mode='a', index=False, sep=',', encoding='utf_8_sig')

    next_pages_components = driver.find_element(By.CLASS_NAME, 'zg_page').find_element(By.TAG_NAME, 'p')
    next_pages_hrefs = next_pages_components.find_elements(By.TAG_NAME, 'a')
    for next_pages_href in next_pages_hrefs:
        if next_pages_href.text == '下一页':
            next_pages_href.click()
            break


from torch.utils.data import DataLoader
import numpy as np
import torchdata.datapipes as dp
import os
import pandas as pd
from PIL import Image
from torch.utils.data import Dataset

class MyDataset(Dataset):
    def __init__(self, csv_path, transform=None):

        df = pd.read_csv(csv_path)
        self.transform = transform

        # based on DataFrame columns
        self.font_names = df["fontName"]
        self.averages = df["readabilityScore"]

        self.font2readability = {}

        for i, name in enumerate(df["fontName"]):
            self.font2readability[name] = df["readabilityScore"][i]

    def __getitem__(self, index):

        averages = self.averages[index]
        return {"a" : averages}

    def __len__(self):
        return self.averages.shape[0]

train_dataset = MyDataset(
    csv_path="./data/readability.csv"
    # transform=data_transforms["train"],
)

train_loader = DataLoader(
    dataset=train_dataset,
    batch_size=32,
    shuffle=True,
    drop_last=True,
    # transform=data_transforms["train"],
    num_workers=0,
)

num_epochs = 1
for epoch in range(num_epochs):

    for batch_idx, x in enumerate(train_loader):
        # if batch_idx >= 3:
        #     break
        # print(" Batch index:", batch_idx)
        # print(" | Batch size:", x.shape[0])
        # print(" | x shape:", x.shape)
        # print(" | y shape:", x[""])
        print(x.keys())

print("Labels from current batch:", x)

# def filter_for_data(filename):
#     return "sample_data" in filename and filename.endswith(".csv")

# def row_processer(row):
#     return {"label": np.array(row[0], np.int32), "data": np.array(row[1:], dtype=np.float64)}

# def build_datapipes(root_dir="./data"):
#     datapipe = dp.iter.FileLister(root_dir)
#     datapipe = datapipe.filter(filter_fn=filter_for_data)
#     datapipe = datapipe.open_files(mode='rt')
#     datapipe = datapipe.parse_csv(delimiter=",", skip_lines=1)
#     # Shuffle will happen as long as you do NOT set `shuffle=False` later in the DataLoader
#     datapipe = datapipe.shuffle()
#     datapipe = datapipe.map(row_processer)
#     return datapipe

# if __name__ == '__main__':
#     datapipe = build_datapipes()
#     dl = DataLoader(dataset=datapipe, batch_size=5, num_workers=2)
#     first = next(iter(dl))
#     labels, features = first['label'], first['data']
#     print(f"Labels batch shape: {labels.size()}")
#     print(f"Feature batch shape: {features.size()}")
#     print(f"{labels = }\n{features = }")
#     n_sample = 0
#     for row in iter(dl):
#         n_sample += 1
#     print(f"{n_sample = }")


import os
import argparse

import wandb
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.cuda.amp import GradScaler, autocast
from torch.utils.data import DataLoader, random_split
from torchvision import transforms
from tqdm import tqdm

from underwater_unet.model import UNet
from utils.data_load import UnderwaterDataset
from utils.dice_score import dice_loss

def train_model(
        model,
        device,
        epochs=10,
        batch_size=1,
        learning_rate=0.001,
        val_percent=10.0,
        save_checkpoint=True,
        weight_decay=1e-8,
        momentum=0.9,
        amp=False,  # Add AMP flag to the function arguments
        gradient_clipping=1.0
):
    # Define transform
    transform = transforms.Compose([
        transforms.ToTensor(),
        # Add any other transformations here
    ])

    # Load dataset and dataloader
    train_dataset = UnderwaterDataset(images_dir='data/images', mask_dir='data/masks', augmentations=transform)
    n_val = int(len(train_dataset) * val_percent / 100)
    n_train = len(train_dataset) - n_val
    train_set, val_set = random_split(train_dataset, [n_train, n_val])

    train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
    val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False, num_workers=4, pin_memory=True)

    # Initialize model, loss, optimizer, and AMP scaler
    criterion = nn.CrossEntropyLoss() if model.n_classes > 1 else nn.BCEWithLogitsLoss()
    optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=weight_decay, betas=(momentum, 0.999))
    scaler = GradScaler(enabled=amp)

    # Initialize learning rate scheduler
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=2)

    # Initialize experimnet logging
    wandb.init(project="UW-Unet", config={
        "epochs": epochs,
        "batch_size": batch_size,
        "learning_rate": learning_rate,
        "val_percent": val_percent,
        "save_checkpoint": save_checkpoint,
        "amp": amp,
        "weight_decay": weight_decay,
        "momentum": momentum,
        "gradient_clipping": gradient_clipping,
    })

    experiment_id = f"exp_{wandb.run.id}"
    local_checkpoint_dir = os.path.join("experiment", experiment_id)
    os.makedirs(local_checkpoint_dir, exist_ok=True)

    best_val_loss = float("inf")

    # Training loop
    for epoch in tqdm(range(1, epochs + 1), desc="Epochs"):
        model.train()
        epoch_loss = 0
        # Inside the training loop
        for batch in tqdm(train_loader, desc="Training", leave=False):
            images = batch['image'].to(device=device, dtype=torch.float32, memory_format=torch.channels_last)
            masks = batch['mask'].to(device=device, dtype=torch.float32 if model.n_classes == 1 else torch.long)

            with autocast(enabled=amp):
                masks_pred = model(images)
                if model.n_classes == 1:
                    masks_pred = masks_pred.squeeze(1)  # Remove channel dim because we have a single class
                    masks = masks.squeeze(1)  # Squeeze the masks for binary case
                    loss = criterion(masks_pred, masks)
                    loss += dice_loss(torch.sigmoid(masks_pred), masks, multiclass=False)
                else:
                    # No need to squeeze masks for multi-class since CrossEntropy expects a 1D target
                    loss = criterion(masks_pred, masks)
                    loss += dice_loss(
                        F.softmax(masks_pred, dim=1),
                        F.one_hot(masks, model.n_classes).permute(0, 3, 1, 2),
                        multiclass=True
                    )
            # Backward and optimization steps remain the same

            assert masks_pred.shape == \
                masks.shape, f"Output shape {masks_pred.shape} and mask shape {masks.shape} do not match"
            # Backward pass and optimization
            optimizer.zero_grad()
            scaler.scale(loss).backward()

            # Gradient clipping
            if gradient_clipping > 0:
                scaler.unscale_(optimizer)
                torch.nn.utils.clip_grad_norm_(model.parameters(), gradient_clipping)

            scaler.step(optimizer)
            scaler.update()

            epoch_loss += loss.item()

        # Log training loss after each epoch
        train_loss = epoch_loss / len(train_loader)
        wandb.log({
            "epoch": epoch,
            "training_loss": train_loss,
        }, step=epoch)

        val_loss = 0
        model.eval()

        with torch.no_grad():
            for batch in tqdm(val_loader, desc="Validation", leave=False):
                images = batch['image'].to(device)
                masks = batch['mask'].to(device).unsqueeze(1).float()
                outputs = model(images)
                probs = torch.sigmoid(outputs)  # Apply sigmoid for dice_loss
                dice = dice_loss(probs, masks, multiclass=False)
                bce = criterion(outputs, masks)
                loss = bce + dice  # Consistent with training loss
                val_loss += loss.item()

                # Log the predicted masks and the true masks as images
                wandb.log({
                    "val_images": [wandb.Image(image.cpu(), caption="Val Image") for image in images],
                    "val_masks": [wandb.Image(mask.cpu(), caption="True Mask") for mask in masks],
                    "val_predictions": [wandb.Image(torch.sigmoid(output).cpu(),
                                                    caption="Pred Mask") for output in outputs],
                }, step=epoch)

        scheduler.step(val_loss)
        # Calculate and log the average validation loss
        val_loss_avg = val_loss / len(val_loader)

        wandb.log({
            "epoch": epoch,
            "validation_loss": val_loss_avg,
        }, step=epoch)

        # Log histograms of model parameters
        for name, param in model.named_parameters():
            if param.requires_grad:
                wandb.log({f"{name}": wandb.Histogram(param.detach().cpu().numpy())}, step=epoch)

        print(f'Epoch {epoch + 1}/{epochs}, Training Loss: {epoch_loss / len(train_loader):.4f}, \
              Validation Loss: {val_loss / len(val_loader):.4f}')

        if val_loss_avg < best_val_loss:
            best_val_loss = val_loss_avg

            # Save the checkpoint in the unique directory
            checkpoint_path = os.path.join(local_checkpoint_dir, f"model_epoch_{epoch + 1}.pth")
            torch.save(model.state_dict(), checkpoint_path)

            # Log the checkpoint as an artifact in WandB
            artifact = wandb.Artifact('model-checkpoint', type='model')
            artifact.add_file(checkpoint_path)
            wandb.log_artifact(artifact)

            # Optional: If you want to keep the checkpoint in the WandB directory structure as well
            wandb_checkpoint_dir = os.path.join(wandb.run.dir, "checkpoints")
            os.makedirs(wandb_checkpoint_dir, exist_ok=True)
            wandb_checkpoint_path = os.path.join(wandb_checkpoint_dir, f"model_epoch_{epoch + 1}.pth")
            torch.save(model.state_dict(), wandb_checkpoint_path)

    print("Training completed.")

def get_args():
    parser = argparse.ArgumentParser(description='Train the UNet on images and target masks')
    parser.add_argument('--epochs', '-e', metavar='E', type=int, default=10, help='Number of epochs')
    parser.add_argument('--batch-size', '-b', dest='batch_size', metavar='B', type=int, default=1, help='Batch size')
    parser.add_argument('--learning-rate', '-l', metavar='LR', type=float, default=0.001, help='Learning rate')
    parser.add_argument('--val-percent', '-v', dest='val_percent', type=float, default=10.0,
                        help='Percent of the data that is used as validation (0-100)')
    parser.add_argument('--save-checkpoint', action='store_true', help='Save a checkpoint at the end of each epoch')
    parser.add_argument('--amp', action='store_true', help='Use automatic mixed precision for training')
    parser.add_argument('--weight-decay', type=float, default=1e-8, help='Weight decay used in optimization')
    parser.add_argument('--momentum', type=float, default=0.9, help='Momentum used in optimization')
    parser.add_argument('--n-classes', type=int, default=1, help='Number of classes for segmentation')
    parser.add_argument('--bilinear', action='store_true', help='Use bilinear upsampling')
    parser.add_argument('--gradient-clipping', type=float, default=1.0, help='Max norm of the gradients')
    # Add more arguments as required

    return parser.parse_args()

if __name__ == '__main__':
    # Parse command line arguments
    args = get_args()

    # Initialize wandb
    wandb.init(project="UW-Unet5", config=args)
    config = wandb.config

    # Set device for training
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # Initialize and transfer the U-Net model to the device
    model = UNet(n_channels=3, n_classes=args.n_classes, bilinear=args.bilinear).to(device)

    # Pass the configurations from wandb to your training function
    train_model(
        model=model,
        device=device,
        epochs=config.epochs,
        batch_size=config.batch_size,
        learning_rate=config.learning_rate,
        val_percent=config.val_percent,
        save_checkpoint=config.save_checkpoint,
        amp=config.amp,
        weight_decay=config.weight_decay,
        momentum=config.momentum,
        gradient_clipping=config.gradient_clipping
    )


#!/usr/bin/env python

# Python wrapper for METEOR implementation, by Xinlei Chen
# Acknowledge Michael Denkowski for the generous discussion and help
from __future__ import division

import atexit
import logging
import os
import re
import subprocess
import sys
import threading

import psutil

# Assumes meteor-1.5.jar is in the same directory as meteor.py.  Change as needed.
METEOR_JAR = 'meteor-1.5.jar'

def enc(s):
    return s.encode('utf-8')

def dec(s):
    return s.decode('utf-8')

class Meteor:

    def __init__(self):
        # Used to guarantee thread safety
        self.lock = threading.Lock()

        mem = '2G'
        mem_available_G = psutil.virtual_memory().available / 1E9
        if mem_available_G < 2:
            logging.warning("There is less than 2GB of available memory.\n"
                            "Will try with limiting Meteor to 1GB of memory but this might cause issues.\n"
                            "If you have problems using Meteor, "
                            "then you can try to lower the `mem` variable in meteor.py")
            mem = '1G'

        meteor_cmd = ['java', '-jar', '-Xmx{}'.format(mem), METEOR_JAR,
                      '-', '-', '-stdio', '-l', 'en', '-norm']
        env = os.environ.copy()
        env['LC_ALL'] = "C"
        self.meteor_p = subprocess.Popen(meteor_cmd,
                                         cwd=os.path.dirname(os.path.abspath(__file__)),
                                         env=env,
                                         stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)

        atexit.register(self.close)

    def close(self):
        with self.lock:
            if self.meteor_p:
                self.meteor_p.kill()
                self.meteor_p.wait()
                self.meteor_p = None
        # if the user calls close() manually, remove the
        # reference from atexit so the object can be garbage-collected.
        if atexit is not None and atexit.unregister is not None:
            atexit.unregister(self.close)

    def compute_score(self, gts, res):
        assert (gts.keys() == res.keys())
        imgIds = gts.keys()
        scores = []

        eval_line = 'EVAL'
        with self.lock:
            for i in imgIds:
                assert (len(res[i]) == 1)
                stat = self._stat(res[i][0], gts[i])
                eval_line += ' ||| {}'.format(stat)

            self.meteor_p.stdin.write(enc('{}\n'.format(eval_line)))
            self.meteor_p.stdin.flush()
            for i in range(0, len(imgIds)):
                v = self.meteor_p.stdout.readline()
                try:
                    scores.append(float(dec(v.strip())))
                except:
                    sys.stderr.write("Error handling value: {}\n".format(v))
                    sys.stderr.write("Decoded value: {}\n".format(dec(v.strip())))
                    sys.stderr.write("eval_line: {}\n".format(eval_line))
                    # You can try uncommenting the next code line to show stderr from the Meteor JAR.
                    # If the Meteor JAR is not writing to stderr, then the line will just hang.
                    # sys.stderr.write("Error from Meteor:\n{}".format(self.meteor_p.stderr.read()))
                    raise
            score = float(dec(self.meteor_p.stdout.readline()).strip())

        return score, scores

    def method(self):
        return "METEOR"

    def _stat(self, hypothesis_str, reference_list):
        # SCORE ||| reference 1 words ||| reference n words ||| hypothesis words
        hypothesis_str = hypothesis_str.replace('|||', '')
        score_line = ' ||| '.join(('SCORE', ' ||| '.join(reference_list), hypothesis_str))
        score_line = re.sub(r'\s+', ' ', score_line)
        self.meteor_p.stdin.write(enc(score_line))
        self.meteor_p.stdin.write(enc('\n'))
        self.meteor_p.stdin.flush()
        return dec(self.meteor_p.stdout.readline()).strip()

    def _score(self, hypothesis_str, reference_list):
        with self.lock:
            # SCORE ||| reference 1 words ||| reference n words ||| hypothesis words
            hypothesis_str = hypothesis_str.replace('|||', '').replace('  ', ' ')
            score_line = ' ||| '.join(('SCORE', ' ||| '.join(reference_list), hypothesis_str))
            self.meteor_p.stdin.write(enc('{}\n'.format(score_line)))
            self.meteor_p.stdin.flush()
            stats = dec(self.meteor_p.stdout.readline()).strip()
            eval_line = 'EVAL ||| {}'.format(stats)
            # EVAL ||| stats
            self.meteor_p.stdin.write(enc('{}\n'.format(eval_line)))
            self.meteor_p.stdin.flush()
            score = float(dec(self.meteor_p.stdout.readline()).strip())
            # bug fix: there are two values returned by the jar file, one average, and one all, so do it twice
            # thanks for Andrej for pointing this out
            score = float(dec(self.meteor_p.stdout.readline()).strip())
        return score

    def __del__(self):
        self.close()


import os
import numpy as np
import cv2
import serial
from datetime import datetime
from datetime import timedelta
from time import sleep
import RPi.GPIO as gpio

CODE_LED_ON = int(1)
CODE_LED_OFF = int(0)
CODE_CHANGE_MOTOR_SPEED = int(3)

led = 11
gpio.setmode(gpio.BOARD)
gpio.setup(led, gpio.OUT)

def turnLedOn():
        gpio.output(led, gpio.HIGH)

def turnLedOff():
        gpio.output(led, gpio.LOW)

maxValue = 150
cropSize = 15

def getCenter(img, c):
    c = c / 100
    h = img.shape[0]
    w = img.shape[1]
    cw = c*w
    ch = c*h
    w1 = (w - cw)/2
    w2 = (w + cw)/2
    h1 = (h - ch)/2
    h2 = (h + ch)/2
    return int(w1), int(w2), int(h1), int(h2)

# c is percent of image filled
def cropCenter(img, c):
    w1, w2, h1, h2 = getCenter(img, c)
    return img[int(h1):int(h2), int(w1):int(w2)]

def processImage(img):
    img = cropCenter(img, cropSize)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    cv2.imshow('cropped', img)
    mean = cv2.mean(img)
    value = mean[2]
    print(value)
    isVein = value < maxValue
    return isVein

minMean = 20
kern1 = np.ones((5, 5))
kern2 = np.ones((11, 11))
def processImage2(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = cv2.GaussianBlur(img, (5, 5), 0)
    img = cv2.Laplacian(img, cv2.CV_8U,ksize=7)
    ret, img = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)
    img = cv2.erode(img, kern1)
    img = cv2.dilate(img, kern2)
    img = cropCenter(img, cropSize)

    cv2.imshow('processed', img)
    mean = np.mean(cv2.mean(img))
    print(mean)
    return mean > minMean

#cap = cv2.VideoCapture('http://172.22.50.203:8080/videofeed')
cap = cv2.VideoCapture('http://198.21.200.255:8081/video')

injected = False
now = datetime.now()
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    isVein = processImage2(frame)
    if isVein:
        turnLedOn()
        if datetime.now() - now > timedelta(seconds=1):
            injected = True
            break
    else:
        turnLedOff()
        now = datetime.now()

    w1, w2, h1, h2 = getCenter(frame, cropSize)
    frame = cv2.rectangle(frame, (w1, h1), (w2, h2), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('window', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
       break
if injected:
    print("Injected!")
# When everything done, release the capture
gpio.cleanup()
cap.release()
cv2.destroyAllWindows()


#!/usr/bin/env python3
import requests
import chess

Piece = {'K': "king", 'Q': "queen", 'N': "knight", 'R': "rook", 'B': "bishop"}

Number = {'1': 'one', '2': 'two', '3': 'three',
          '4': 'four', '5': 'five', '6': 'six',
          '7': 'seven', '8': 'eight'}

Wrapped_Spk = {'one': '1', 'two': '2', 'three': '3',
               'four': '4', 'five': '5', 'six': '6',
               'seven': '7', 'eight': '8', "king": 'K',
               "queen": 'Q', "knight": 'N', "rook": 'R',
               "bishop": 'B', "x": 'x', "check": 'x'}

def API_token():
    """
    Return the api token from `secret.key`
    """
    with open("secret.key", "r") as pf:
        token = pf.readline()
    return 'Bearer ' + token

Token = API_token()

def Parse_move(move):
    """
    Function that takes a move and transcribe it to spoken text.
    Args:
    (move): string representing the pgn/fen move
    Return:
    (Parsed): Spoken text format for the pgn move
    """
    Parsed = ""
    for char in move:
        if char == 'O':
            if len(move) == 3:
                Parsed += "short castle"
            else:
                Parsed += "long castle"
            break
        if char == 'x':
            Parsed += 'takes on '
        elif char == '+':
            Parsed += ' check'
        elif char.isupper():
            Parsed += Piece[char] + " "
        elif char.islower():
            Parsed += char + " "
        elif char.isdigit():
            Parsed += Number[char] + " "
        elif char == '#':
            Parsed += " Checkmate"
    Parsed += " \n"
    return " ".join(Parsed.split())

def Spoken_ToFen(Spoken):
    """
    convert Spoken text to fen
    Finish short castling...checkmate
    """
    if Spoken == 'short castle':
        return 'O-O'
    if Spoken == 'long castle':
        return 'O-O-O'
    Spoken = Spoken.replace("takes on", "x")
    Spoken = Spoken.replace("take on", "x")
    To_list = Spoken.split()

    spk = ''
    for elem in To_list:
        if len(elem) == 1:
            spk += elem
        else:
            spk += Wrapped_Spk[elem]
    return spk

def get_fen():
    """
    Function returning the CURRENT fen
    FEN example of return:
      'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    """
    headers = {'Authorization': Token}
    url = "https://lichess.org/api/account/playing"
    res = requests.get(url, headers=headers)
    if res.status_code != 200:
        print("Check your connection")
    res_json = res.json()

    fen = res_json["nowPlaying"][0]["fen"]
    return fen

def get_legalMoves():
    """
    Function that return the CURRENT possible moves to play.
    Example of return: ['Nh3', 'Nf3', 'Nc3', 'Na3', 'h3', 'g3']
    """
    fen = get_fen()
    board = chess.Board(fen)
    legal_moves = list(board.legal_moves)

    moves_t = str(board.legal_moves)[37:-1]
    moves_t = moves_t.replace(", ", "', '")
    moves_t = moves_t.replace("(", "('")
    moves_t = moves_t.replace(")", "')")
    moves_t = eval(moves_t)
    """
    moves_t: more than one move to play: ('Kf8', 'dxc5', 'g5', 'd5', 'a5')
    moves_t: one move to play: 'Kf8'
    """
    if type(moves_t) is not tuple:  # One single move
        return [moves_t]

    moves = []
    for i in moves_t:
        moves.append(i)
    return moves

def get_pieces():
    """
    Function that returns a dictionary of the CURRENT legal
      moves per piece in spoken version.
    Example of return: {'knight': ['knight h three', 'knight f three'],
      'pawn': ['h three']}
    """
    moves = get_legalMoves()  # (INPUT: list of pgn leal moves)

    dic = {'N': [], 'P': [], 'K': [], 'Q': [], 'B': [], 'R': []}
    for move in moves:
        if move[0].islower():
            dic['P'].append(move)
        else:
            if move[0] == 'O':  # Castling move
                dic['K'].append(move)
            else:
                dic[move[0]].append(move)

    pieces = {'knight': dic['N'], 'pawn': dic['P'],
              'king': dic['K'], 'queen': dic['Q'],
              'bishop': dic['B'], 'rook': dic['R']}

    # do not include pieces with no move to play
    updated = dict((key, value) for key, value in pieces.items()
                   if value != [])

    # get the spoken version of notation
    spk_fen = {}
    for key in updated.keys():
        spk_fen[key] = []
        for fen in updated[key]:
            spk_fen[key].append(Parse_move(fen))
    return spk_fen

def get_grammar():
    """
    Function that return a tuple of:
      1. grammar to pass for the model recognizer.
        - Example: '["knight h six", "knight f six", "knight c six", "[unk]"]'
      2. `to_check`
    """
    legal_moves = get_pieces()  # (INPUT)
    grammar = ''
    to_check = []
    for key, value in legal_moves.items():
        for elem in value:
            to_check.append(elem)
            grammar += '\"{0}\", '.format(elem)
    return '[{0}\"[unk]\"]'.format(grammar), to_check


from unittest import TestCase
from ..distance import relative_dtw
import numpy as np

class TestRelative_dtw(TestCase):

    def test_relative_dtw(self):
        "Given"
        x = np.array([1.2, 1.18, 1.95, 2.09])
        y = np.array([1.2, 1.18, 1.95, 2.09])

        "When"
        similarity = relative_dtw(x, y)

        "Then"
        self.assertEqual(similarity, 1.0)


"""
URL configuration for estoque project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    path('admin/', admin.site.urls),
    # Home
    path('', views.home, name="home"),

    # Adicionar Produtos
    path('adicionar', views.adicionar, name="adicionar"),

    # Ver Produto Individualmente
    path('produto/<str:id>', views.ver_produto, name="ver_produto"),

    # Editar Produto
    path('editar', views.editar_produto, name="editar"),

    # Deletar Produto
    path('deletar/<str:id>', views.deletar_produto, name="deletar"),

    # Login
    path('login', views.loginUser, name="login"),

    # Criar Conta
    path('register', views.registerUser, name="register"),

    # Logout
    path('logout', views.logoutUser, name='logout'),

]

urlpatterns += staticfiles_urlpatterns()


"prediction"
yhat = CNN.predict([X_test, mov_test], batch_size=128, verbose=1)

"score histogram"
plt.hist(yhat[:,1],100);
plt.xlabel('Prediction Score');
plt.ylabel('Frequency');

"Confusion matrix"
from sklearn.metrics import accuracy_score, auc, confusion_matrix, roc_curve
print(confusion_matrix(np.argmax(yhat,1), np.argmax(y_test, 1)))

# pretty print
from talk_utils import plot_confusion_matrix
plt.figure()
plot_confusion_matrix(yhat,  y_test, classes=[0,1]);

"ROC curve/AUC"
fpr, tpr, thresholds = roc_curve(np.argmax(y_test, 1), yhat[:,1], drop_intermediate=False)
the_auc = auc(fpr, tpr)
print(the_auc)

plt.figure()
plt.scatter(fpr, tpr, c=thresholds, cmap=plt.cm.bwr);
plt.plot(fpr,tpr,'k');

# pretty print
from talk_hemato_utils import get_auc
get_auc(yhat[:,1], y_test[:,1], do_plot=True)


#!/usr/bin/python
# -*- coding: utf-8 -*-
# Script chạy trên py32 (không sử dụng anaconda)

from __future__ import print_function
import collections
import numpy as np
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from svmutil import *
from sklearn.model_selection import train_test_split
import time
import CONSTANT
import util.log_util

# Hàm load dữ liệu
def LoadData (filename1, filename2):
    file_data = open(filename1,'r')
    file_labels = open(filename2,'r')
    data = file_data.read().splitlines()
    labels = file_labels.read().splitlines()
    for i in range(len(labels)):
        labels[i] = int(labels[i])
    return data, labels

# Hàm chuân hóa dữ liệu: loại stopword, lower case nó xuống

def NormalizeData (training_data):
    # Định nghĩa biến trả về
    new_training_data = training_data
    # Đọc file stopword có sẵn trong thư viện lưu vào cache
    cachedStopWords = stopwords.words("english")

    for i in range(len(training_data)):
        # Viết thường
        temp = training_data[i].lower()
        # Loại stopword
        temp = ' '.join([word for word in temp.split() if word not in cachedStopWords])
        # Gán vào biến trả về
        new_training_data[i]= temp

    return new_training_data

def Readfile(file):
    data = []
    lines = file.read().splitlines()

    for i in range(len(lines)):
        values = lines[i].split('\t')
        sentence_class = int(values[0])
        sentence_value = values[1]
        temp = [sentence_value ,sentence_class ]
        data.append(temp)
    return data

def Convert_word2Idx (raw_data):
    # Building a dictionary
    Dict = []
    for i in range(len(raw_data)):
        # Split word in each line
        words = raw_data[i][0].split(' ')
        Dict = Dict + words
        # Eliminate duplicate elements
        Dict = list(set(Dict))
    # Now we have dictionary, each word in dictionary have its index, is also the index of Dictionary
    # Here we convert training sentence into <sentence class 1 -1> <word index>:<word class - 0 1>
    # Class of each sentence
    y = []
    for i in range(len(raw_data)):
        y.append(raw_data[i][1])
    # Sentences in right format
    x = []
    for i in range(len(raw_data)):
        # Init a small dict to contain words in one sentence that appear or not
        sent_dict = {}

        words_sent = raw_data[i][0].split(' ')
        for j in range(len(words_sent)):
            if (words_sent[j] in Dict):
                index = Dict.index(words_sent[j])
                sent_dict[index] = 1
        # Sort the dict and append it to data
        sort_sent_dict = collections.OrderedDict(sorted(sent_dict.items()))
        x.append(sort_sent_dict)

def LoadWord2VecLabels(filename):
    file = open(filename,'r')
    data= file.read().splitlines()
    for i in range(len(data)):
        data[i] = int(data[i])
    return data

def LoadWord2VecLabels_str(filename):
    file = open(filename,'r')
    data= file.read().splitlines()
    return data

def LoadWord2VecData(filename):
    data = []
    with open(filename) as f:
        for line in f:  #Line is a string
            #split the string on whitespace, return a list of numbers
            # (as strings)
            numbers_str = line.split()
            #convert numbers to floats
            numbers_float = [float(x) for x in numbers_str]  #map(float,numbers_str) works too
            data.append(numbers_float)
    return data

# Convert POS-tagged data
def Convert_word2Idx_NoPOStagged (nopostagged_data):
    # Building a dictionary
    Dict = []
    # Building a data in the appreciate form for LIBSVM
    Data = []
    for i in range(len(nopostagged_data)):
        # Split word in each line
        word = nopostagged_data[i].split(' ')
        Dict = Dict + word
        Data.append(word)
    # Eliminate duplicate elements
    Dict = list(set(Dict))
    print ("    - Complete Dictionary")
    #print Data
    print ("Number of Vocabulary: ")
    print (len(Dict))
    #Save dictionary
    file_dic = open("Dictionary.txt",'w')
    for i in range(len(Dict)):
        file_dic.write(str(Dict[i]) + "\n")
    # Sentences in right format
    x = []
    for i in range(len(Data)):
        sent_dict = {}
        # Check word
        for j in range(len(Data[i])):
            # Check if a word appeared in dictionary
            if (Data[i][j] in Dict):
                index = Dict.index(Data[i][j])
                sent_dict[index] = 1

        # Sort the dict and append it to data
        sort_sent_dict = collections.OrderedDict(sorted(sent_dict.items()))
        x.append(sort_sent_dict)
    return x

def Convert_word2Idx_NoPOStagged_predict (nopostagged_data):
    # Reading dictionary
    file_dict = open("Dictionary.txt",'r')
    Dict = file_dict.read().splitlines()

    Data = []
    for i in range(len(nopostagged_data)):
        # Split word in each line
        word = nopostagged_data[i].split(' ')
        Data.append(word)

    # Sentences in right format
    x = []

    for i in range(len(Data)):
        sent_dict = {}
        # Check word
        for j in range(len(Data[i])):
            # Check if a word appeared in dictionary
            if (Data[i][j] in Dict):
                index = Dict.index(Data[i][j])
                sent_dict[index] = 1

        # Sort the dict and append it to data
        sort_sent_dict = collections.OrderedDict(sorted(sent_dict.items()))
        x.append(sort_sent_dict)
    return x

if __name__ == "__main__":

    # create logger
    logger = util.log_util.create_logger("SVM_aspect_classifier", print_console=True)
    print = logger.info # redirect print function to logger.info
    print ('Language %s' %(CONSTANT.LANGUAGE))

    # Define
    data_size = 0 # set data_size = 0 to train on all data

    print ("Loading Data...")
    # Load dữ liệu
    data, labels = LoadData(CONSTANT.DATASET_FOLDER_DIR+'/'+ CONSTANT.full_aspect_data_raw,
                            CONSTANT.DATASET_FOLDER_DIR+'/'+ CONSTANT.full_aspect_labels_raw)
    print (len(labels))
    if (data_size>0):
        data = data[:data_size]
        labels = labels[:data_size]

    X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.25, random_state=42)
    print("Data: %d train sample, %d test sameple" %(len(y_train), len(y_test)))

    # training_set =  data[:40] + data[38694:38734] + data[52876:52916]
    training_set =  X_train
    training_labels =  y_train

    # Make dictionary
    print ("Making Dictionary...")
    training_set = Convert_word2Idx_NoPOStagged (training_set)

    # Tạo model
    print ("Making a model")
    prob  = svm_problem(training_labels, training_set)
    param = svm_parameter('-t 0 -c 1 -b 1')
    # Train the model
    print ("Training...")
    start_training_time = time.time()
    model = svm_train(prob, param)
    # Save the model
    svm_save_model('SVM_aspect.model', model)

    # Kiểm thử, xuất file predict label, cho biết accuracy
    model = svm_load_model('SVM_aspect.model')
    test_set = Convert_word2Idx_NoPOStagged_predict(X_test)
    test_labels = y_test
    done_train_time = time.time()
    print ('Training in %f' %(done_train_time-start_training_time))
    # Predict
    print ("Predict ...")
    p_label, p_acc, p_val = svm_predict(test_labels, test_set, model, '-b 1')
    ACC, MSE, SCC = evaluations(test_labels, p_label)

    # Ghi file predict labels
    file = open('svm_aspect_predict.txt','w')
    for i in range(len(p_label)):
        file.write(str(p_label[i]) + '\n')

    # Tìm Precision, Recall và F1
    true_labels = test_labels
    predict_labels = p_label

    print (predict_labels[0:10])
    print (true_labels[0:10])
    # Food
    true_positive = 0
    false_positive = 0
    false_negative = 0
    for i in range(len(predict_labels)):
        if (predict_labels[i] == 1 and true_labels[i] == 1):
            true_positive += 1
        elif (predict_labels[i] == 1 and true_labels[i] != 1) :
            false_positive += 1
        elif ((predict_labels[i] == 5 or predict_labels[i] == 3)  and true_labels[i] == 1) :
            false_negative += 1
    print ("For Food Aspect =================")
    print ('Precision is: ')
    pre = 0
    if (true_positive+false_positive != 0):
        pre = true_positive*100*1.0/(true_positive+false_positive)*1.0
    print (pre)

    print ('Recall is: ')
    recall = 0
    if ((true_positive+false_negative) != 0):
        recall = true_positive*100*1.0/(true_positive+false_negative)*1.0
    print (recall)

    print ('F1 is: ')
    f1 = 0
    if ((pre+recall) != 0):
        f1 = 2*pre*recall*1.0/(pre+recall)*1.0
    print (f1)
    print ("=================================")

     # Staff
    true_positive = 0
    false_positive = 0
    false_negative = 0
    for i in range(len(predict_labels)):
        if (predict_labels[i] == 5 and true_labels[i] == 5):
            true_positive += 1
        elif (predict_labels[i] == 5 and true_labels[i] != 5) :
            false_positive += 1
        elif ((predict_labels[i] == 1 or predict_labels[i] == 3)  and true_labels[i] == 5) :
            false_negative += 1
    print ("For Staff Aspect =================")
    print ('Precision is: ')
    pre = 0
    if (true_positive+false_positive != 0):
        pre = true_positive*100*1.0/(true_positive+false_positive)*1.0
    print (pre)

    print ('Recall is: ')
    recall = 0
    if ((true_positive+false_negative) != 0):
        recall = true_positive*100*1.0/(true_positive+false_negative)*1.0
    print (recall)

    print ('F1 is: ')
    f1 = 0
    if ((pre+recall) != 0):
        f1 = 2*pre*recall*1.0/(pre+recall)*1.0
    print (f1)
    print ("=================================")

     # Ambience
    true_positive = 0
    false_positive = 0
    false_negative = 0
    for i in range(len(predict_labels)):
        if (predict_labels[i] == 3 and true_labels[i] == 3):
            true_positive += 1
        elif (predict_labels[i] == 3 and true_labels[i] != 3) :
            false_positive += 1
        elif ((predict_labels[i] == 5 or predict_labels[i] == 1)  and true_labels[i] == 3) :
            false_negative += 1
    print ("For Ambience Aspect =================")
    print ('Precision is: ')
    pre = 0
    if (true_positive+false_positive != 0):
        pre = true_positive*100*1.0/(true_positive+false_positive)*1.0
    print (pre)

    print ('Recall is: ')
    recall = 0
    if ((true_positive+false_negative) != 0):
        recall = true_positive*100*1.0/(true_positive+false_negative)*1.0
    print (recall)

    print ('F1 is: ')
    f1 = 0
    if ((pre+recall) != 0):
        f1 = 2*pre*recall*1.0/(pre+recall)*1.0
    print (f1)
    print ("=================================")


from bs4 import BeautifulSoup
from splinter import Browser
import time
import requests

def init_browser():
    executable_path = {"executable_path": "chromedriver"}
    return Browser("chrome", **executable_path, headless=True, incognito=True)

# browser = init_browser()
# browser.quit()

def quit_browser(browser):
    browser.quit()

def scrape_image(recipe_id, browser):
    print("browser opened")
    website_url = f"https://www.food.com/recipe-{recipe_id}"
    browser.visit(website_url)
    # time.sleep(1)
    website_html = browser.html
    website_soup = BeautifulSoup(website_html, "html.parser")
    print("html parsed")
    website_results = website_soup.find("div", class_="inner-wrapper")

    try:
        picture_url = website_results.img["data-src"]
        print("Picture found!!!")
        return picture_url

    except:
        flag = False
        attempts = 100
        for i in range(attempts):
            try:
#                 time.sleep(1)
                website_html = browser.html
                print(f"Retry {i+1}")
                website_soup = BeautifulSoup(website_html, "html.parser")
                print("New html parsed")
                website_results = website_soup.find("div", class_="inner-wrapper")
                picture_url = website_results.img["data-src"]
                print("Picture found!!!")
                flag = True
            except:
                print(f"Attempt {i+1} failed")

            if i == attempts - 1:
                return None
            if flag:
                break
        return picture_url

# browser = init_browser()
# scrape_image(39087, browser)
# scrape_image(2886, browser)
# scrape_image(27208, browser)
# quit_browser(browser)


import time
import requests
import json

crontable = []
outputs = []

def check_query(arg):
    if len(arg) == 40:
        return arg
    elif len(arg) == 32:
        return arg
    else:
        return 'error1'

def get_shadow_data(query):
    base_url = 'http://innocuous.shadowserver.org/api/?query=%s'
    refined_url = base_url % query
    response = requests.get(refined_url)
    return response

def make_json(response):
    if '! No match found for' in response:
        return response
    else:
        list1 = response.split('\n')
        file_info1 = list1[0]
        file_info_list = file_info1.split(',')
        file_info = "*md5:* %s\n*sha1:* %s\n*First Seen:* %s\n*Last Seen:* %s\n*FileType:* %s\n*ssdeep:* %s\n" % (file_info_list[0], file_info_list[1], file_info_list[2], file_info_list[3], file_info_list[4], file_info_list[-1])
        json_string = list1[1]
        json_data = json.loads(json_string.encode('utf-8'))
        av_engine_list = []
        signature_list = []
        for k, v in json_data.iteritems():
            av_engine_list.append(k)
            signature_list.append(v)
        av_list = []
        length = len(av_engine_list)
        for x in range(0, length):
            a = av_engine_list[x]
            b = signature_list[x]
            string_vals = "%s: %s\n" % (a, b)
            av_list.append(string_vals)
        av_string = ''.join(av_list)
        shadow_results = "%s\n----------------------------------------\n*AV Hits:*\n%s" % (file_info, av_string)
        return shadow_results

def process_message(data):
    channel = data["channel"]
    text = data["text"]
    string = text.lower()
    alist = string.split(' ')
    if alist[0] == '!shadow':
        time.sleep(1)
        hash1 = alist[1]
        query1 = check_query(hash1)
        if query1 == 'error1':
            outputs.append([channel, 'please submit md5 or sha1'])
        else:
            response = get_shadow_data(query1)
            shadow_results = make_json(response.content)
            outputs.append([channel, shadow_results])