import inspect
import os
import shutil
import subprocess

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

from src.utils.logging_config import log

class GraphExporter:
    GAME_LOGS_DATA_WORLD = '../datasets/retrosheets/game-logs_combined/game_logs_data-world.csv'
    GAME_LOGS_ABBREVIATIONS = '../datasets/retrosheets/game-logs_combined/TEAMABR.TXT'
    TEX_TEMPLATE_SLIDE = '../slides/graphs/tex/_template/slide.template'
    TEX_TEMPLATE_SECTION = '../slides/graphs/tex/_template/section.template'
    TEX_TEMPLATE_SUBSECTION = '../slides/graphs/tex/_template/subsection.template'
    TEX_TEMPLATE_MAIN = '../slides/graphs/tex/_template/main.template'
    TEX_OUTPUT_NAME = 'ss23_visualization_mlb.tex'

    OUTPUT_DIR = '../slides/graphs/img'
    OUTPUT_PDF = '../slides/pdf'
    OUTPUT_BEAMER = '../slides/graphs/tex/src/generated'

    START_YEAR = 1900

    def __init__(self):
        self.plots = []
        self.tex_slides = ''
        self._dtypeDict = {
            'date': str,
            'number_of_game': np.uint32,
            'day_of_week': str,
            'v_name': str,
            'v_league': str,
            'v_game_number': np.int32,
            'h_name': str,
            'h_league': str,
            'h_game_number': np.int32,
            'v_score': np.int32,
            'h_score': np.int32,
            'length_outs': np.int32,
            'day_night': str,
            'completion': str,
            'forefeit': str,
            'protest': str,
            'park_id': str,
            'attendance': np.int32,
            'length_minutes': np.int32,
            'v_line_score': str,
            'h_line_score': str,
            'v_at_bats': np.int32,
            'v_hits': np.int32,
            'v_doubles': np.int32,
            'v_triples': np.int32,
            'v_homeruns': np.int32,
            'v_rbi': np.int32,
            'v_sacrifice_hits': np.int32,
            'v_sacrifice_flies': np.int32,
            'v_hit_by_pitch': np.int32,
            'v_walks': np.int32,
            'v_intentional walks': np.int32,
            'v_strikeouts': np.int32,
            'v_stolen_bases': np.int32,
            'v_caught_stealing': np.int32,
            'v_grounded_into_double': np.int32,
            'v_first_catcher_interference': np.int32,
            'v_left_on_base': np.int32,
            'v_pitchers_used': np.int32,
            'v_individual_earned_runs': np.int32,
            'v_team_earned_runs': np.int32,
            'v_wild_pitches': np.int32,
            'v_balks': np.int32,
            'v_putouts': np.int32,
            'v_assists': np.int32,
            'v_errors': np.int32,
            'v_passed_balls': np.int32,
            'v_double_plays': np.int32,
            'v_triple_plays': np.int32,
            'h_at_bats': np.int32,
            'h_hits': np.int32,
            'h_doubles': np.int32,
            'h_triples': np.int32,
            'h_homeruns': np.int32,
            'h_rbi': np.int32,
            'h_sacrifice_hits': np.int32,
            'h_sacrifice_flies': np.int32,
            'h_hit_by_pitch': np.int32,
            'h_walks': np.int32,
            'h_intentional walks': np.int32,
            'h_strikeouts': np.int32,
            'h_stolen_bases': np.int32,
            'h_caught_stealing': np.int32,
            'h_grounded_into_double': np.int32,
            'h_first_catcher_interference': np.int32,
            'h_left_on_base': np.int32,
            'h_pitchers_used': np.int32,
            'h_individual_earned_runs': np.int32,
            'h_team_earned_runs': np.int32,
            'h_wild_pitches': np.int32,
            'h_balks': np.int32,
            'h_putouts': np.int32,
            'h_assists': np.int32,
            'h_errors': np.int32,
            'h_passed_balls': np.int32,
            'h_double_plays': np.int32,
            'h_triple_plays': np.int32,
            'hp_umpire_id': str,
            'hp_umpire_name': str,
            '1b_umpire_id': str,
            '1b_umpire_name': str,
            '2b_umpire_id': str,
            '2b_umpire_name': str,
            '3b_umpire_id': str,
            '3b_umpire_name': str,
            'lf_umpire_id': str,
            'lf_umpire_name': str,
            'rf_umpire_id': str,
            'rf_umpire_name': str,
            'v_manager_id': str,
            'v_manager_name': str,
            'h_manager_id': str,
            'h_manager_name': str,
            'winning_pitcher_id': str,
            'winning_pitcher_name': str,
            'losing_pitcher_id': str,
            'losing_pitcher_name': str,
            'saving_pitcher_id': str,
            'saving_pitcher_name': str,
            'winning_rbi_batter_id': str,
            'winning_rbi_batter_id_name': str,
            'v_starting_pitcher_id': str,
            'v_starting_pitcher_name': str,
            'h_starting_pitcher_id': str,
            'h_starting_pitcher_name': str,
            'v_player_1_id': str,
            'v_player_1_name': str,
            'v_player_1_def_pos': np.int32,
            'v_player_2_id': str,
            'v_player_2_name': str,
            'v_player_2_def_pos': np.int32,
            'v_player_3_id': str,
            'v_player_3_name': str,
            'v_player_3_def_pos': np.int32,
            'v_player_4_id': str,
            'v_player_4_name': str,
            'v_player_4_def_pos': np.int32,
            'v_player_5_id': str,
            'v_player_5_name': str,
            'v_player_5_def_pos': np.int32,
            'v_player_6_id': str,
            'v_player_6_name': str,
            'v_player_6_def_pos': np.int32,
            'v_player_7_id': str,
            'v_player_7_name': str,
            'v_player_7_def_pos': np.int32,
            'v_player_8_id': str,
            'v_player_8_name': str,
            'v_player_8_def_pos': np.int32,
            'v_player_9_id': str,
            'v_player_9_name': str,
            'v_player_9_def_pos': np.int32,
            'h_player_1_id': str,
            'h_player_1_name': str,
            'h_player_1_def_pos': np.int32,
            'h_player_2_id': str,
            'h_player_2_name': str,
            'h_player_2_def_pos': np.int32,
            'h_player_3_id': str,
            'h_player_3_name': str,
            'h_player_3_def_pos': np.int32,
            'h_player_4_id': str,
            'h_player_4_name': str,
            'h_player_4_def_pos': np.int32,
            'h_player_5_id': str,
            'h_player_5_name': str,
            'h_player_5_def_pos': np.int32,
            'h_player_6_id': str,
            'h_player_6_name': str,
            'h_player_6_def_pos': np.int32,
            'h_player_7_id': str,
            'h_player_7_name': str,
            'h_player_7_def_pos': np.int32,
            'h_player_8_id': str,
            'h_player_8_name': str,
            'h_player_8_def_pos': np.int32,
            'h_player_9_id': str,
            'h_player_9_name': str,
            'h_player_9_def_pos': np.int32,
            'additional_info': str,
            'acquisition_info': str
        }
        self._df = self._read_game_logs()

    def _read_game_logs(self):
        log.info('started reading csv dataset')

        debug_row_count = os.environ.get('VIS_ROW_COUNT')
        log.debug("reading data from %s entries", self.GAME_LOGS_DATA_WORLD)

        if debug_row_count is not None:
            log.info("debug flag set, only reading %s entries", debug_row_count)
            df = pd.read_csv(self.GAME_LOGS_DATA_WORLD,
                             converters={col: self._process_value for col in self._dtypeDict.keys()},
                             nrows=int(debug_row_count))
        else:
            df = pd.read_csv(self.GAME_LOGS_DATA_WORLD,
                             converters={col: self._process_value for col in self._dtypeDict.keys()})
        log.info('finished reading csv dataset')

        df = self._sanitize_df(df)
        df = self._add_custom_fields(df)

        return df

    def _process_value(self, value):
        if value == '':
            return
        else:
            return value

    def _sanitize_df(self, gamelog_df):
        gamelog_df = self._empty_flags_for_empty_cells(gamelog_df)
        gamelog_df = self._remove_first_incomplete_years(gamelog_df)
        return gamelog_df

    def _empty_flags_for_empty_cells(self, gamelog_df):
        # deal with null / empty values
        placeholder = '-12345'
        # fill empty cells with placeholder
        gamelog_df = gamelog_df.fillna(placeholder)
        # necessary to cast with type dictionary
        gamelog_df = gamelog_df.astype(self._dtypeDict)
        # after cast insert nan value, for an early fail in later processing if list
        # is not properly sanitized
        gamelog_df = gamelog_df.replace(int(placeholder), np.nan)
        gamelog_df = gamelog_df.replace(placeholder, None)
        return gamelog_df

    def _remove_first_incomplete_years(self, gameslog_df):
        # return gameslog_df[int(gameslog_df['date']) >= 18900000]
        return gameslog_df[gameslog_df['date'].apply(lambda x: int(x[:4])) >= self.START_YEAR]
        # return gameslog_df[gameslog_df['date_year'] >= 1890]

    def _add_custom_fields(self, gamelog_df):
        gamelog_df = self._append_date_summaries(gamelog_df)
        gamelog_df = self._append_team_ids_translation(gamelog_df)
        gamelog_df = self._append_team_duration(gamelog_df)
        return gamelog_df

    def _append_date_summaries(self, gamelog_df):
        """
        appends new values to input dataframe
        - date_year: year in which the game took place
        - date_decade: decade in wich the game took place
        :param gamelog_df: input dataframe
        :return: appended dataframe
        """
        # create a new column containing just the year - easier for later processing
        gamelog_df['date'] = pd.to_datetime(gamelog_df['date'])
        gamelog_df = pd.concat([gamelog_df, gamelog_df['date'].apply(lambda x: x.year).rename('date_year')], axis=1)
        gamelog_df = pd.concat(
            [gamelog_df, gamelog_df['date'].apply(lambda x: str(x.year)[:3] + '0s').rename('date_decade')], axis=1)
        return gamelog_df

    def _append_team_ids_translation(self, gamelog_df):
        """
        appends new values to input dataframe
        - v_name_translate: city and full name of the visiting team combined
        - h_name_translate: city and full name of the home team combined
        :param gamelog_df: input dataframe
        :return: appended dataframe
        """
        # translate team ids
        abreviations_df = pd.read_csv(self.GAME_LOGS_ABBREVIATIONS)
        team_mapping = dict(zip(abreviations_df['team'], abreviations_df['city'] + ' - ' + abreviations_df['nickname']))
        gamelog_df = pd.concat([gamelog_df, gamelog_df['v_name'].map(team_mapping).rename('v_name_translate')], axis=1)
        gamelog_df = pd.concat([gamelog_df, gamelog_df['h_name'].map(team_mapping).rename('h_name_translate')], axis=1)

        return gamelog_df

    def _append_team_duration(self, gamelog_df):
        """
        appends new values to input dataframe
        - date_h_duration: years which the home team existed
        - date_v_duration: years which the visiting team existed
        :param gamelog_df: input dataframe
        :return: appended dataframe
        """
        abreviations_df = pd.read_csv(self.GAME_LOGS_ABBREVIATIONS)
        duration_mapping = dict(
            zip(abreviations_df['team'], abreviations_df['last_year'] - abreviations_df['first_year']))
        gamelog_df = pd.concat([gamelog_df, gamelog_df['h_name'].map(duration_mapping).rename('date_h_duration')],
                               axis=1)
        gamelog_df = pd.concat([gamelog_df, gamelog_df['v_name'].map(duration_mapping).rename('date_v_duration')],
                               axis=1)

        first_year_mapping = dict(zip(abreviations_df['team'], abreviations_df['first_year']))
        gamelog_df = pd.concat([gamelog_df, gamelog_df['h_name'].map(first_year_mapping).rename('date_h_first_year')],
                               axis=1)
        gamelog_df = pd.concat([gamelog_df, gamelog_df['v_name'].map(first_year_mapping).rename('date_v_first_year')],
                               axis=1)

        last_year_mapping = dict(zip(abreviations_df['team'], abreviations_df['last_year']))
        gamelog_df = pd.concat([gamelog_df, gamelog_df['h_name'].map(last_year_mapping).rename('date_h_last_year')],
                               axis=1)
        gamelog_df = pd.concat([gamelog_df, gamelog_df['v_name'].map(last_year_mapping).rename('date_v_last_year')],
                               axis=1)

        return gamelog_df

    def _export_graph(self, plotting_func, df):
        callback_package_name = inspect.getmodule(plotting_func).__package__.replace(".", "/")
        callback_file_name = os.path.splitext(os.path.basename(inspect.getfile(plotting_func)))[0]
        callback_func_name = plotting_func.__name__

        output_dir = self.OUTPUT_DIR + '/' + callback_package_name + '/' + callback_file_name + '/'
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        markdown_plot_description = plotting_func(df)

        output_file = os.path.realpath(output_dir + callback_func_name)
        log.info('writing plot: %s', output_file)

        plt.savefig(output_file)
        plt.show()

        return output_file

    def _build_tex_slide(self, img_path):
        latex_slide_template = self._load_str_content(self.TEX_TEMPLATE_SLIDE)
        img_name = os.path.basename(img_path).replace('_', '\_') + '.png'
        return latex_slide_template % (img_name, img_path)

    def append_graph(self, plotting_func):
        img_path = self._export_graph(plotting_func, self._df)
        self.tex_slides += self._build_tex_slide(img_path)

    def append_section(self, section):
        latex_chapter_template = self._load_str_content(self.TEX_TEMPLATE_SECTION)
        self.tex_slides += latex_chapter_template % section

    def append_subsection(self, subsection):
        latex_chapter_template = self._load_str_content(self.TEX_TEMPLATE_SUBSECTION)
        self.tex_slides += latex_chapter_template % subsection

    def _load_str_content(self, file_path):
        file_content = ''
        with open(file_path, 'r') as file:
            file_content = file.read()
        return file_content

    def _write_latex(self, output_dir, tex_content):
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        output_file = output_dir + "/" + self.TEX_OUTPUT_NAME
        with open(output_file, 'w') as file:
            log.info("writing tex content to: %s", output_file)
            file.write(tex_content)

        return os.path.abspath(output_file)

    def _compile_tex_file(self, tex_file):
        dir = os.path.dirname(tex_file)
        subprocess.run(["pdflatex", tex_file], cwd=dir)
        subprocess.run(["pdflatex", tex_file], cwd=dir)
        return tex_file.replace(".tex", ".pdf")

    def build_presentation(self):
        latex_main_template = self._load_str_content(self.TEX_TEMPLATE_MAIN)
        main_latex = latex_main_template % self.tex_slides
        tex_file = self._write_latex(self.OUTPUT_BEAMER, main_latex)
        beamer_output = self._compile_tex_file(tex_file)

        log.debug("beamer output: %s", beamer_output)
        log.debug("copy to: %s", self.OUTPUT_PDF)
        shutil.copy(beamer_output, self.OUTPUT_PDF)


"""empty message

Revision ID: ae2c3ec70cf7
Revises: 32c8381e5d90
Create Date: 2021-12-02 18:00:34.767548

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'ae2c3ec70cf7'
down_revision = '32c8381e5d90'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('tags',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('tag_name', sa.String(length=20), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('tag_name')
    )
    op.add_column('image_tags', sa.Column('tag_id', sa.Integer(), nullable=True))
    op.drop_constraint('image_tags_tag_name_key', 'image_tags', type_='unique')
    op.create_foreign_key(None, 'image_tags', 'tags', ['tag_id'], ['id'])
    op.drop_column('image_tags', 'tag_name')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('image_tags', sa.Column('tag_name', sa.VARCHAR(length=20), autoincrement=False, nullable=False))
    op.drop_constraint(None, 'image_tags', type_='foreignkey')
    op.create_unique_constraint('image_tags_tag_name_key', 'image_tags', ['tag_name'])
    op.drop_column('image_tags', 'tag_id')
    op.drop_table('tags')
    # ### end Alembic commands ###


def plot_decision_surface(x_train, x_today, y_today, classifier, scaler, date):

    #Plot of the decision surface obtained through the classifier used
    #INPUTS: x_train = training set of spreads
    #        x_today =
    #        y_today =
    #        classifier = classifier used for the training
    #                     (between svclassifier and xgbclassifier)
    #        scaler = standard scaler to normalize new data following
    #                 normalization used for training

    import matplotlib.pyplot as plt
    import numpy as np

    # first drop all countries with na spreads or CCC rating
    idx_na = x_today[x_today.isna().any(axis=1)].index  # save index were imp rating will be overwritten
    x_today_wo_na = x_today.drop(idx_na)
    y_today_wo_na = y_today.drop(idx_na)
    idx_ccc = y_today_wo_na[~y_today_wo_na['RATING_SP'].isin([1, 2, 3, 4, 5])].index  # select were  rating is CCC
    x_today_wo_na = x_today_wo_na.drop(idx_ccc)
    y_today_wo_na = y_today_wo_na.drop(idx_ccc)

    # adapt to scaling
    x_today_wo_na = scaler.transform(x_today_wo_na)

    # plot
    h = .02  # step size in the mesh
    x_min, x_max = x_train[:, 1].min() - 1e-2, x_train[:, 1].max() + 1e-2  # Bond Spread
    y_min, y_max = x_train[:, 0].min() - 1e-2, x_train[:, 0].max() + 1e-2  # CDS
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    z = classifier.predict(np.c_[xx.ravel(), yy.ravel()])
    z = z.reshape(xx.shape)
    plt.contourf(xx, yy, z, cmap=plt.cm.coolwarm, alpha=0.8)

    col = np.array(y_today_wo_na.RATING_SP)
    markers = ["o", "^", "s", "*", "h"]
    label = ["AAA", "$A A$", "$A$", "$B B B$", "$B B$"]
    colors = ['mediumblue', 'royalblue', 'lightsteelblue', 'salmon', 'firebrick']
    for i, c in enumerate(np.unique(col)):
        plt.scatter(x_today_wo_na[:, 1][col == c], x_today_wo_na[:, 0][col == c], color=colors[c - 1],
                    cmap=plt.cm.coolwarm, marker=markers[i], s=144, label=label[i])
    plt.legend()
    plt.xlabel('Bond Spreads')
    plt.ylabel('CDS')
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title('Decision Surface ' + str(date)[:10])
    plt.show()

    return


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 15 09:33:54 2020

@author: vargthon
"""
from lib.collector import Collector
from lib.normalizer import Normalizer
import pandas as pd
import numpy as np
import pickle
from sklearn.linear_model import LinearRegression
from datetime import datetime
from lib.linearmodel import LinearModel
from lib.repository.linearmodelrepository import LinearModelRepository
from lib.period import Period
class ProductLearning():

    def __init__(self):
        self.collector = Collector()
        self.normalize = Normalizer()

    def load_linear(self, company, product, period):
        repository = LinearModelRepository()
        model = repository.load_valid(product,company, period.value)
        if len(model.params) > 0:
            model = model
            product = model.product
            company = model.company
        else:
            model = self.fit_linear_product(product,company)
            model.company = {"cod": company}
            model.product = {"cod": product}
            repository.save(model)

        return model

    def fit_linear_product(self, codigoProduto, codigoFilial, features=[],target=None, training_date_from=datetime.now().timestamp(),
        training_date_to=datetime.now().timestamp(), period=Period.MONTHLY):
        company = {"cod": codigoFilial}
        df = self.collector.colector_mes(codigoProduto, codigoFilial)
        df = self.normalize.normalizar(df)
        model = LinearModel()
        if df.size > 0:
            product = {
                   "cod": df.loc[0,"KEY:PRODUTO:CODIGO"]
                }
        real_params = []
        for col in df.columns:
            column = col.split(":")
            if len(column) > 0:
                if column[0] == 'PARAMS':
                    real_params.append(col)
                if column[0] == 'CALENDAR' and column[1] != 'DATE':
                    real_params.append(col)
                if column[0] == 'EVENT':
                    real_params.append(col)
        X = df[real_params].values
        y = df['METRIC:QTD'].values
        if df.size != 0:
            model.fit(X,y)
            model.params = real_params
            model.training_date_from = training_date_from
            model.training_date_to = training_date_to
            model.period = period
            #self.model = self.regression#pickle.dumps(self.regression)

        return model


import asyncio
import base64
import cv2
import numpy as np
import websockets
import AI_function_img_pose_add
import AI_function_img_face

async def handler(websocket):
    data = await websocket.recv()
    if data == 'face':
      while True:
          data = await websocket.recv()
          img = cv2.imdecode(np.frombuffer(base64.b64decode(data.split(',')[1]), np.uint8), cv2.IMREAD_COLOR)
          result = AI_function_img_face.handle(img)
          if result != None and len(result) > 0:
            await websocket.send(result)
    elif data == 'pose':
      while True:
          data = await websocket.recv()
          img = cv2.imdecode(np.frombuffer(base64.b64decode(data.split(',')[1]), np.uint8), cv2.IMREAD_COLOR)
          result = AI_function_img_pose_add.handle(img)
          if result != None and len(result) > 0:
            await websocket.send(result)

async def main():
    async with websockets.serve(handler, "172.31.11.131", 9999):
      await asyncio.Future()  # run forever

if __name__ == "__main__":
    asyncio.run(main())


import cv2
import utils

###################################
webcam = False
path = '1.png'
cap = cv2.VideoCapture(1)
cap.set(10, 160)
cap.set(3, 1920)
cap.set(4, 1080)
scale = 3
wP = 210 * scale
hP = 297 * scale
###################################

while True:
    if webcam:
        success, img = cap.read()
    else:
        img = cv2.imread(path)

    imgContours, conts = utils.getContours(img, minArea=50000, filter=4)
    if len(conts) != 0:
        biggest = conts[0][2]
        # print(biggest)
        imgWarp = utils.warpImg(img, biggest, wP, hP)
        imgContours2, conts2 = utils.getContours(imgWarp,
                                                 minArea=2000, filter=4,
                                                 cThr=[50, 50], draw=False)
        if len(conts) != 0:
            for obj in conts2:
                cv2.polylines(imgContours2, [obj[2]], True, (0, 255, 0), 2)
                nPoints = utils.reorder(obj[2])
                nW = round((utils.findDis(nPoints[0][0] // scale, nPoints[1][0] // scale) / 10), 1)
                nH = round((utils.findDis(nPoints[0][0] // scale, nPoints[2][0] // scale) / 10), 1)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]),
                                (nPoints[1][0][0], nPoints[1][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]),
                                (nPoints[2][0][0], nPoints[2][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                x, y, w, h = obj[3]
                cv2.putText(imgContours2, '{}cm'.format(nW), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
                cv2.putText(imgContours2, '{}cm'.format(nH), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
        cv2.imshow('A4', imgContours2)

    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    cv2.imshow('Original', img)
    if cv2.waitKey(1) & 0xFF ==27:
        break

Opencv project training/09 Object Size Measurement/Object Measurement.py


import os
import sys
import copy
import torch
import itertools

from model import *
from utilities import *

class SudokuSolver:
    def __init__(self, modelPath, recurrentIterations=10):
        self.sudokuChecker = SudokuChecker()
        self.plotter = SudokuPlotter()
        self.__loadModel(modelPath, recurrentIterations)

    def __loadModel(self, modelPath, recurrentIterations):

        self.model = SudokuNet(recurrentIterations=recurrentIterations)
        self.model.load_state_dict(torch.load(modelPath, map_location=torch.device('cpu')))
        self.model.to(torch.device('cpu'))
        self.model.eval()

    @staticmethod
    def __prepareSudokuTensor(sudoku):
        sudokuTensor = torch.zeros((1,81))
        for rowIndex, row in enumerate(sudoku):
            for columnIndex, value in enumerate(row):
                sudokuTensor[0, rowIndex*9 + columnIndex] = int(value)
        return sudokuTensor.long()

    def __solveSudokuInOneStep(self, sudoku):
        sudokuTensor = self.__prepareSudokuTensor(sudoku)
        _, outputSudokuTensor = self.model(sudokuTensor)
        solutionTensor = torch.argmax(outputSudokuTensor,-1)
        solutionTensor += 1
        mask = torch.where(sudokuTensor==0)
        sudokuTensor[mask]+=solutionTensor[mask]
        self.plotter.plotMatrix(sudokuTensor)
        return sudokuTensor

    def __solveSudokuStepByStep(self, sudoku):
        sudokuTensor = self.__prepareSudokuTensor(sudoku)
        step=1
        while torch.sum(sudokuTensor==0)>0:
            _, outputSudokuTensor = self.model(sudokuTensor)
            decisionProbabilities, solutionTensor = torch.max(outputSudokuTensor,2)
            _, mask = torch.where(sudokuTensor>0)
            decisionProbabilities[0,mask] = float('-inf')
            mostLikelyCell = torch.argmax(decisionProbabilities)
            sudokuTensor[0,mostLikelyCell] = solutionTensor[0,mostLikelyCell]+1
            print('Step: {}'.format(step))
            print('Filled Row {}, Column {}'.format(mostLikelyCell//9+1,mostLikelyCell%9+1))
            self.plotter.plotMatrix(sudokuTensor)
            step+=1

        return sudokuTensor

    def __getSudokuSolution(self, sudoku, fast):

        with torch.no_grad():
            if fast:
                solvedSudoku = self.__solveSudokuInOneStep(sudoku)
            else:
                solvedSudoku = self.__solveSudokuStepByStep(sudoku)
        return solvedSudoku

    def __evaluateSudoku(self, sudokuTensor, fast):

        speed = 'Fast' if fast else 'Slow'
        sudokuMatrix = copy.copy(sudokuTensor).view(9,9).tolist()
        if self.sudokuChecker.checkSudoku(sudokuMatrix):
            print('{} Sudoku Is Correct\n'.format(speed))
            return True
        else:
            print('{} Sudoku Is UnCorrect\n'.format(speed))
            return False

    def solveSudoku(self, sudoku, fast=False):
        solvedSudokuTensor = self.__getSudokuSolution(sudoku, fast)
        self.__evaluateSudoku(solvedSudokuTensor, fast)

    def solveSudokus(self, sudokus, fast=False):
        accuracyCount = 0.
        for sudoku in sudokus:
            solvedSudokuTensor = self.__getSudokuSolution(sudoku, fast)
            if self.__evaluateSudoku(solvedSudokuTensor, fast):
                accuracyCount += 1
        print('Sudokus Solved: {}%'.format(accuracyCount*100/len(sudokus)))

def readSudokusFromTest(sudokuPath):

    listOfInputSudokus = list()
    with open(sudokuPath) as inputFile:
        sudokus = inputFile.read().split('\n\n')
        for sudoku in sudokus:
            sudokuAsList = list()
            lines = sudoku.split('\n')
            for line in lines:
                sudokuAsList.append(''.join(line.strip().split()))
            listOfInputSudokus.append(sudokuAsList)

    return listOfInputSudokus

if __name__ == '__main__':

    sudokuSolver = SudokuSolver(sys.argv[1])
    sudokus = readSudokusFromTextFile(sys.argv[2])
    sudokuSolver.solveSudokus(sudokus, fast=True)


import os
import subprocess

import requests

import dns.zone
import dns.name

from .zones import RecordFactory, SOA

TMPL_ZONE = '''
zone "{name}." {{
        type master;
        file "{fullname}";
}};
'''

def json_compat(response):
    """ Compatibility layer for requests JSON helper pre and post v1.0.0.
    """
    if callable(response.json):
        return response.json()
    else:
        return response.json

class RestdnsBind(object):

    def __init__(self, logger, restdns_base_url, output_directory, run_rndc, rndc_binary_path):
        self.logger = logger
        self._restdns_base_url = restdns_base_url.rstrip('/')
        self._output_directory = output_directory
        self._run_rndc = run_rndc
        self._rndc_binary_path = rndc_binary_path
        self.zones = []

    def run(self):
        remote_zones = self._get_remote_zones()
        self.zones = remote_zones
        local_zones = self._get_local_zones()

        to_write = set()
        to_delete = set()

        # Get zones to delete (zones existing locally but no more on the
        # restdns server):
        to_delete |= set(local_zones) - set(remote_zones)
        self.logger.debug('Zones to delete: %s', ','.join(to_delete) or 'none')

        # Get zones to generate (not existing locally or higher serial):
        for name, infos in remote_zones.iteritems():
            local_infos = local_zones.get(name)
            if local_infos is None:
                to_write.add(name)  # Zone not exiting yet locally
            else:
                if infos['serial'] > local_infos['serial']:
                    to_write.add(name)
        self.logger.debug('Zones to write: %s', ','.join(to_write) or 'none')

        # Delete old zones:
        for name in to_delete:
            filename = os.path.join(self._output_directory, '%s.zone' % name)
            os.unlink(filename)
            self.logger.debug('Removed: %s', filename)

        # Generate zones:
        for name in to_write:
            url = remote_zones[name]['url']
            self._write_zone(url)

        # Regenerate Bind configuration file if something have changed;
        if to_write or to_delete:
            self._write_zone_conf(remote_zones.keys())
            self.logger.debug('Regenerated configuration file')

        # Reload Bind configuration using rndc:
        if self._run_rndc and (to_write or to_delete):
            self.logger.debug('Executing rndc reload...')
            try:
                rndc = subprocess.Popen([self._rndc_binary_path, 'reload'],
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
            except OSError as err:
                self.logger.warning('Error while invoking rndc reload: %s', err)
            else:
                rcode = rndc.wait()
                if rcode:
                    self.logger.warning('Non-null return code when executing rndc reload (%s)', rcode)

    def _write_zone_conf(self, zones):
        """ Write the zone list configuration.
        """
        with open(os.path.join(self._output_directory, 'zones.conf'), 'w') as fzones:
            for zone in zones:
                fzones.write(TMPL_ZONE.format(name=zone, fullname=os.path.join(self._output_directory, '%s.zone' % zone)))

    def _get_remote_zones(self):
        zones_url = self._restdns_base_url + '/zones'
        zones = {}
        for zone in json_compat(requests.get(zones_url))['zones']:
            zones[zone['name']] = {'url': zone['url'], 'serial': zone['serial']}
        return zones

    def _get_local_zones(self):
        zones = {}
        for filename in os.listdir(self._output_directory):
            if filename.endswith('.zone'):
                fullname = os.path.join(self._output_directory, filename)
                name, serial = self._read_zone(fullname)
                zones[name] = {'serial': serial}
        return zones

    def _read_zone(self, path):
        zone = dns.zone.from_file(path, check_origin=False, allow_include=False)
        rd_set = zone.get_rdataset('', SOA)
        soa = rd_set[0]  # Only one SOA is allowed per zone
        return zone.origin.to_text(omit_final_dot=True), soa.serial

    def _install_records(self, record_factory, dns_zone, records_url, prefix=''):
        records = json_compat(requests.get(self._restdns_base_url + records_url))
        for record in records['records']:
            if record['type'] == 'include':
                zone_name = record['parameters']['zone']
                if zone_name in self.zones:
                    zone_infos = self.zones[zone_name]
                    zone_url = self._restdns_base_url + zone_infos['url']
                    zone = json_compat(requests.get(zone_url))
                    self._install_records(record_factory, dns_zone, zone['records_url'], prefix=record['name'])
            else:
                if prefix and record['name']:
                    name = '.'.join((prefix, record['name']))
                elif prefix:
                    name = prefix
                else:
                    name = record['name']
                rd_set = dns_zone.get_rdataset(name, record['type'], create=True)
                rd_set.add(record_factory.create_record(record['type'], record['parameters']))

    def _write_zone(self, zone_url):
        """ Write a local zone file using a remote zone URL.
        """
        # Get the zone details:
        zone_url = self._restdns_base_url + zone_url
        zone = json_compat(requests.get(zone_url))
        origin = dns.name.from_text(zone['name'] + '.')
        dns_zone = dns.zone.Zone(origin)
        record_factory = RecordFactory(origin)

        # Create the SOA record:
        rd_set = dns_zone.get_rdataset('', SOA, create=True)
        rd_set.add(record_factory.create_soa(zone['rname'],
                                             zone['serial'],
                                             zone['refresh'],
                                             zone['retry'],
                                             zone['expire'],
                                             zone['minimum']))

        self._install_records(record_factory, dns_zone, zone['records_url'])

        zone_filename = os.path.join(self._output_directory, '%s.zone' % zone['name'])
        fzone = open(zone_filename, 'w')
        # Write the $ORIGIN statement (dnspython seem not able to write it)
        fzone.write('$ORIGIN %s\n' % origin)
        dns_zone.to_file(fzone, relativize=False)
        self.logger.debug('Wrote: %s', zone_filename)


import json
import os

path='C:\\Users\\ECE-ML\\Desktop\\sample data\\'

class keypoint:
    def __init__(self, path):
        self.path = path

    def get_jsonfile_path(self):
        keypoint_dirs = []
        all_label_jsonfile = []
        for dir in os.listdir(self.path):
            keypoint_dir = os.path.join(self.path, dir)
            keypoint_dirs.append(keypoint_dir)  # get keypoint path
        for path_keypoint_dir in keypoint_dirs:  # all of the keypoint path
            same_label_jsonfile = []
            for json_file in os.listdir(path_keypoint_dir):  # all the file in path
                json_file_dir = os.path.join(path_keypoint_dir, json_file)  # join the path and name of file
                same_label_jsonfile.append(json_file_dir)
            all_label_jsonfile.append(same_label_jsonfile)
        return all_label_jsonfile
        # print(json_file_dir)
        # C:\Users\ECE-ML\Desktop\video_json_word\000000\P01_01_00_0_color.json

    def get_x_y(self, keypoint):
        allvideo_coordinate = []
        for onelabel_folder in self.get_jsonfile_path():
            onelabel_allvideo_coordinate = []
            for onejsonfile in onelabel_folder:
                video_frame_coordinate = []
                with open(onejsonfile, 'r') as f:
                    data = json.load(f)  # all frames data in one video
                    for num_frame in range(1, len(data), 1):
                        # frame number in one video
                        frame_coordinate = []
                        frame_data = data[num_frame]
                        data_body = frame_data[keypoint]
                        data_body_list = data_body[0]
                        for data_x_y_num in range(len(data_body_list)):  # one frame x and y
                            data_keypoint = data_body_list[data_x_y_num]
                            data_x_y = tuple((data_keypoint[0], data_keypoint[1]))
                            frame_coordinate.append(data_x_y)
                        video_frame_coordinate.append(frame_coordinate)
                onelabel_allvideo_coordinate.append(video_frame_coordinate)
            allvideo_coordinate.append(onelabel_allvideo_coordinate)
        return allvideo_coordinate
    def left_hand_keypoint(self):
        return self.get_x_y('Left hand keypoint')

    def right_hand_keypoint(self):
        return self.get_x_y('right hand keypoint')

    def body_keypoint(self):
        return self.get_x_y('body keypoint')

    def Face_keypoint(self):
        return self.get_x_y('Face keypoint')


import argparse
import importlib
import json
import logging
import os
import pkgutil
import sys  # noqa: F401
import tempfile
from unittest.mock import patch

import configargparse
import humanfriendly
import lookout

from lookout.core import slogging
from lookout.core.data_requests import DataService
from lookout.core.event_listener import EventListener
from lookout.core.manager import AnalyzerManager
from lookout.core.package import package_cmdline_entry
from lookout.core.sqla_model_repository import SQLAlchemyModelRepository

class ArgumentDefaultsHelpFormatterNoNone(argparse.ArgumentDefaultsHelpFormatter):
    """
    Pretty formatter of help message for arguments. \
    It adds default value to the end if it is not None.
    """

    def _get_help_string(self, action):
        if action.default is None:
            return action.help
        return super()._get_help_string(action)

def list_analyzers(args: argparse.Namespace):
    """
    Print the list of the analyzers inside the `lookout` package.

    :param args: Not used - parsed command line arguments.
    :return: None
    """
    first = True
    queue = [tuple(c) + ("lookout.",) for c in pkgutil.iter_modules(lookout.__path__)]
    while queue:
        importer, name, ispkg, prefix = queue.pop(0)

        if not ispkg or name == "core":
            continue

        m = importer.find_module(name).load_module(name)
        if getattr(m, "__meta__", False):
            queue.extend(tuple(c) + (prefix + name + ".",)
                         for c in pkgutil.iter_modules(m.__path__))
            continue

        try:
            cls = m.analyzer_class
        except AttributeError:
            continue
        if first:
            first = False
        else:
            print()
        print(prefix + name)
        print("\t%s" % cls.version)
        print("\t" + cls.description)

def run_analyzers(args: argparse.Namespace):
    """
    Launch the service with the specified analyzers. Blocks until a KeyboardInterrupt.

    :param args: Parsed command line arguments.
    :return: None
    """
    log = logging.getLogger("run")
    model_repository = create_model_repo_from_args(args)
    log.info("Created %s", model_repository)
    if args.request_server == "auto":
        data_request_address = "%s:10301" % args.server.split(":")[0]
    else:
        data_request_address = args.request_server
    data_service = DataService(data_request_address)
    log.info("Created %s", data_service)
    sys.path.append(os.getcwd())
    manager = AnalyzerManager(
        analyzers=[importlib.import_module(a).analyzer_class for a in args.analyzer],
        model_repository=model_repository,
        data_service=data_service,
    )
    sys.path = sys.path[:-1]
    log.info("Created %s", manager)
    listener = EventListener(address=args.server, handlers=manager, n_workers=args.workers)
    log.info("Created %s", listener)
    listener.start()
    log.info("Listening %s", args.server)
    listener.block()
    model_repository.shutdown()
    data_service.shutdown()

def init_repo(args: argparse.Namespace):
    """
    Initialize the model repository.

    :param args: Parsed command line arguments.
    :return: None
    """
    repo = create_model_repo_from_args(args)
    repo.init()

def run_analyzer_tool(args: argparse.Namespace) -> None:
    """
    Invoke the tooling of an analyzer.

    :param args: Parsed command line arguments.
    :return: None
    """
    with patch("sys.argv", [args.analyzer] + args.args):
        importlib.import_module(args.analyzer).run_cmdline_tool()

def create_model_repo_from_args(args: argparse.Namespace) -> SQLAlchemyModelRepository:
    """
    Get SQLAlchemyModelRepository from command line arguments.

    :param args: `argparse` parsed arguments.
    :return: Constructed instance of SQLAlchemyModelRepository.
    """
    return SQLAlchemyModelRepository(
        db_endpoint=args.db, fs_root=args.fs,
        max_cache_mem=humanfriendly.parse_size(args.cache_size),
        ttl=int(humanfriendly.parse_timespan(args.cache_ttl)),
        engine_kwargs=args.db_kwargs)

def add_model_repository_args(parser):
    """
    Add command line flags specific to the model repository.

    :param parser: `argparse` parser where to add new flags.
    """
    parser.add("-d", "--db", required=True, help="Model repository database address.")
    parser.add("-f", "--fs", required=True, help="Model repository file system root.")
    parser.add("--cache-size", default="1G",
               help="Model repository cache size - accepts human-readable values like 200M, 2G.")
    parser.add("--cache-ttl", default="6h",
               help="Model repository cache time-to-live (TTL) - accepts human-readable "
                    "values like 30min, 4h, 1d.")
    parser.add("--db-kwargs", type=json.loads, default={},
               help="Additional keyword arguments to SQLAlchemy database engine.")

def add_analyzer_arg(parser):
    """
    Add the variable count argument to specify analyzers.

    :param parser: `argparse` parser where to add new flags.
    """
    parser.add("analyzer", nargs="+",
               help="Fully qualified package name with an analyzer. Current directory is "
                    "included into PYTHONPATH.")

def create_parser() -> configargparse.ArgParser:
    """
    Initialize the command line argument parser.
    """
    parser = configargparse.ArgParser(default_config_files=[
        "/etc/lookout/analyzer.conf", "~/.config/lookout/analyzer.conf"],
        formatter_class=ArgumentDefaultsHelpFormatterNoNone,
        auto_env_var_prefix="lookout_")
    slogging.add_logging_args(parser)
    subparsers = parser.add_subparsers(help="Commands", dest="command")

    def add_parser(name, help):
        return subparsers.add_parser(
            name, help=help, formatter_class=ArgumentDefaultsHelpFormatterNoNone)

    list_parser = add_parser("list", "Print globally available analyzers.")
    list_parser.set_defaults(handler=list_analyzers)

    run_parser = add_parser(
        "run", "Launch a new service with the specified (one or more) analyzers.")
    run_parser.set_defaults(handler=run_analyzers)
    add_analyzer_arg(run_parser)
    run_parser.add("-c", "--config", is_config_file=True,
                   help="Path to the configuration file with option defaults.")
    run_parser.add("-s", "--server", required=True,
                   help="Lookout server address, e.g. localhost:1234.")
    run_parser.add("-w", "--workers", type=int, default=1,
                   help="Number of threads which process Lookout events.")
    add_model_repository_args(run_parser)
    run_parser.add_argument("--request-server", default="auto",
                            help="Address of the data retrieval service. \"same\" means --server.")

    init_parser = add_parser("init", "Initialize the model repository.")
    init_parser.set_defaults(handler=init_repo)
    add_model_repository_args(init_parser)

    tool_parser = add_parser("tool", "Invoke the tooling of a given analyzer.")
    tool_parser.set_defaults(handler=run_analyzer_tool)
    tool_parser.add("analyzer", help="Fully qualified package name with an analyzer.")
    tool_parser.add("args", nargs=argparse.REMAINDER)

    package_parser = add_parser(
        "package",
        "Package several analyzers to a Docker container and write a sample Docker Compose config "
        "for Lookout.")
    package_parser.set_defaults(handler=package_cmdline_entry)
    add_analyzer_arg(package_parser)
    package_parser.add("-w", "--workdir", help="Generate files in this directory.",
                       default=tempfile.mkdtemp(prefix="lookout_package_"))
    package_parser.add("--requirements", help="Path to a custom requirements.txt")
    package_parser.add("-r", "--repo", help="GitHub repository name to watch. "
                                            "Example: \"src-d/lookout\".",
                       required=True)
    package_parser.add("-u", "--user", help="GitHub user name which will send review comments.",
                       required=True)
    paturl = "https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/"  # noqa
    package_parser.add("-t", "--token", help="GitHub token for -u/--user. See " + paturl,
                       required=True)
    package_parser.add("-y", "--yes", help="Run the commands in the end.",
                       action="store_true")
    package_parser.add("-n", "--no", help="Do not run the commands in the end.",
                       action="store_true")
    return parser


from dockerspawner import DockerSpawner
from traitlets import default
from tornado import gen
import jupyterhub
import requests

SERVER = 'http://localhost:5005'

def default_format_volume_name(template, spawner):
    return template.format(username=spawner.user.name,
                           user_ID=spawner.user.name.split('+')[0],
                           project_name=spawner.user.name.split('+')[1])

class MyDockerSpawner(DockerSpawner):
    tb_port = '6006'

    @property
    def extra_create_kwargs(self):
        return {"ports": {'%i/tcp' % self.port: None, '6006/tcp': None}}

    @property
    def extra_host_config(self):
        return {"port_bindings": {self.port: (self.host_ip,),
                                  '6006': (self.host_ip,)}}

    @default('format_volume_name')
    def _get_default_format_volume_name(self):
        return default_format_volume_name

    @gen.coroutine
    def get_tb_port(self):
        tb_resp = yield self.docker('port', self.container_id, self.tb_port)
        port = int(tb_resp[0]['HostPort'])
        return port

    @staticmethod
    def update_project_tb_port(project_name, tb_port):
        """
        update tb port when docker restarted
        :param tb_port:
        :param project_name:
        :return: dict of res json
        """
        print('update project tb_port: ', project_name, tb_port)
        return requests.put(f'{SERVER}/project/projects/{project_name}?by=name'
                            , json={'tb_port': str(tb_port)})

    @staticmethod
    def insert_envs(project_name):
        """
        add env to jupyterlab
        :param tb_port:
        :param project_name:
        :return: dict of res json
        """
        return requests.put(f'{SERVER}/apps/insert_envs/{project_name}')

    @gen.coroutine
    def start(self, image=None, extra_create_kwargs=None,
              extra_start_kwargs=None, extra_host_config=None):
        """Start the single-user server in a docker container. You can override
        the default parameters passed to `create_container` through the
        `extra_create_kwargs` dictionary and passed to `start` through the
        `extra_start_kwargs` dictionary.  You can also override the
        'host_config' parameter passed to `create_container` through the
        `extra_host_config` dictionary.

        Per-instance `extra_create_kwargs`, `extra_start_kwargs`, and
        `extra_host_config` take precedence over their global counterparts.

        """
        container = yield self.get_container()
        if container and self.remove_containers:
            self.log.warning(
                "Removing container that should have been cleaned up: %s (id: %s)",
                self.container_name, self.container_id[:7])
            # remove the container, as well as any associated volumes
            yield self.docker('remove_container', self.container_id, v=True)
            container = None

        if container is None:
            image = image or self.image
            if self._user_set_cmd:
                cmd = self.cmd
            else:
                image_info = yield self.docker('inspect_image', image)
                cmd = image_info['Config']['Cmd']
            cmd = cmd + self.get_args()

            # build the dictionary of keyword arguments for create_container
            create_kwargs = dict(
                image=image,
                environment=self.get_env(),
                volumes=self.volume_mount_points,
                name=self.container_name,
                command=cmd,
            )

            # ensure internal port is exposed
            create_kwargs['ports'] = {'%i/tcp' % self.port: None}

            create_kwargs.update(self.extra_create_kwargs)
            if extra_create_kwargs:
                create_kwargs.update(extra_create_kwargs)

            # build the dictionary of keyword arguments for host_config
            host_config = dict(binds=self.volume_binds, links=self.links)

            if hasattr(self, 'mem_limit') and self.mem_limit is not None:
                # If jupyterhub version > 0.7, mem_limit is a traitlet that can
                # be directly configured. If so, use it to set mem_limit.
                # this will still be overriden by extra_host_config
                host_config['mem_limit'] = self.mem_limit

            if not self.use_internal_ip:
                host_config['port_bindings'] = {self.port: (self.host_ip,)}
            host_config.update(self.extra_host_config)
            host_config.setdefault('network_mode', self.network_name)

            if extra_host_config:
                host_config.update(extra_host_config)

            self.log.debug("Starting host with config: %s", host_config)

            host_config = self.client.create_host_config(**host_config)
            create_kwargs.setdefault('host_config', {}).update(host_config)

            # create the container
            resp = yield self.docker('create_container', **create_kwargs)
            self.container_id = resp['Id']
            self.log.info(
                "Created container '%s' (id: %s) from image %s",
                self.container_name, self.container_id[:7], image)

        else:
            self.log.info(
                "Found existing container '%s' (id: %s)",
                self.container_name, self.container_id[:7])
            # Handle re-using API token.
            # Get the API token from the environment variables
            # of the running container:
            for line in container['Config']['Env']:
                if line.startswith(
                        ('JPY_API_TOKEN=', 'JUPYTERHUB_API_TOKEN=')):
                    self.api_token = line.split('=', 1)[1]
                    break

        # TODO: handle unpause
        self.log.info(
            "Starting container '%s' (id: %s)",
            self.container_name, self.container_id[:7])

        # build the dictionary of keyword arguments for start
        start_kwargs = {}
        start_kwargs.update(self.extra_start_kwargs)
        if extra_start_kwargs:
            start_kwargs.update(extra_start_kwargs)

        # start the container
        yield self.docker('start', self.container_id, **start_kwargs)

        ip, port = yield self.get_ip_and_port()
        if jupyterhub.version_info < (0, 7):
            # store on user for pre-jupyterhub-0.7:
            self.user.server.ip = ip
            self.user.server.port = port
        tb_port = yield self.get_tb_port()
        # update tb_port and module env to exist app
        self.update_project_tb_port(self.user.name, tb_port)
        self.insert_envs(self.user.name)
        # jupyterhub 0.7 prefers returning ip, port:
        return ip, port


import click

from pyBrown_tools.input_RotDiff import InputDataRotDiff
from pyBrown_tools.messaging import timestamp
from pyBrown_tools.trajectories import read_trajectories, add_auxiliary_data_multibeads, \
								 compute_orientations, compute_mean_orientation_autocorrelation, \
								 save_mean_orientation_autocorrelation_to_file, \
								 compute_mean_squared_angular_displacements, \
								 save_mean_squared_angular_displacements_to_file
from pyBrown_tools.plotting import plot_msads, plot_moas

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

@click.command()
@click.argument('input_filename',
				type = click.Path( exists = True ))
def main(input_filename):

	# here the list of keywords that are required for program to work is provided
	required_keywords = ["labels", "sizes", "box_size", "input_xyz_template", "input_xyz_range"]

	# here the dict of keywords:default values is provided
	# if given keyword is absent in JSON, it is added with respective default value
	defaults = {"debug": False, "verbose": False, "probing_frequency": 1,
				"min_time": 0.0, "mode": "window", "float_type": 32}

	timestamp( 'Reading input from {} file', input_filename )
	i = InputDataRotDiff(input_filename, required_keywords, defaults)
	timestamp( 'Input data:\n{}', i )

	timestamp( 'Reading trajectories' )
	times, labels, auxiliary_data = read_trajectories(i.input_data)
	add_auxiliary_data_multibeads( i.input_data, labels, auxiliary_data )
	timestamp( 'Separating the rotational movement' )
	orientation_labels = compute_orientations( i.input_data, labels, auxiliary_data )
	del labels

	timestamp( 'Computing mean orientation autocorrelations' )
	moas = compute_mean_orientation_autocorrelation( i.input_data, orientation_labels, auxiliary_data )
	timestamp( 'Saving mean orientation autocorrelations to a file' )
	save_mean_orientation_autocorrelation_to_file(i.input_data, times, moas)

	timestamp( 'Computing mean squared angular displacements' )
	msads = compute_mean_squared_angular_displacements( i.input_data, orientation_labels, auxiliary_data )
	timestamp( 'Saving mean squared angular displacements to a file' )
	save_mean_squared_angular_displacements_to_file(i.input_data, times, msads)
	del orientation_labels

	timestamp( 'Plotting mean squared angular displacements' )
	plot_msads(i.input_data, times, msads)

	timestamp( 'Plotting mean orientation autocorrelations' )
	plot_moas(i.input_data, times, moas)

	del times
	del moas
	del msads

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

if __name__ == '__main__':

	main()


import argparse
import os
import re
import sys
import time

# Command line arguments
parser=argparse.ArgumentParser(description='Calculate the shortest path between all pairs of vertices in a graph')
parser.add_argument('--algorithm',default='a',\
    help='Algorithm: Select the algorithm to run, default is all. (a)ll, (b)ellman-ford only or (f)loyd-warshall only')
parser.add_argument('-v','--verbose',action='store_true')
parser.add_argument('--profile',action='store_true')
parser.add_argument('filename',metavar='<filename>',help='Input file containing graph')

graphRE=re.compile("(\\d+)\\s(\\d+)")
edgeRE=re.compile("(\\d+)\\s(\\d+)\\s(-?\\d+)")

vertices=[]
edges=[]

def BellmanFord(G):
    pathPairs=[]
    # TODO: Fill in your Bellman-Ford algorithm here
    temp = edges
    for i in range(len(vertices)):
        temp[i][i] = 0

    for Lp in range(len(vertices)):
        dist = []
        for i in range(len(vertices)):
            dist.append(float("inf"))
        dist[Lp] = 0
        for i in range(len(vertices) - 1):
            for j in range(len(dist)):
                for k in range(len(dist)):
                    if dist[j] != float("inf") and float(dist[k]) > float(dist[j]) + float(temp[j][k]):
                        dist[k] = int(dist[j]) + int(temp[j][k])
                        dist[k] = str(dist[k])

        for x in range(len(dist)):
            for y in range(len(dist)):
                if float(dist[y]) > float(dist[x]) + float(temp[x][y]):
                    print("N/A")
        pathPairs.append(dist)

    for i in pathPairs:
        print(i)

    print('BellmanFord algorithm is complete')
    # The pathPairs list will contain the 2D array of shortest paths between all pairs of vertices
    # [[w(1,1),w(1,2),...]
    #  [w(2,1),w(2,2),...]
    #  [w(3,1),w(3,2),...]
    #   ...]
    return pathPairs

def FloydWarshall(G):
    pathPairs=[]
    # TODO: Fill in your Floyd-Warshall algorithm here
    dist = []
    Lp = []
    for i in edges:
        k = []
        for j in i:
            if j != float("inf"):
                k.append(int(j))
            elif j == float("inf"):
                k.append(float("inf"))
        Lp.append(k)
    for a in range(len(vertices)):
        c = []
        for b in range(len(vertices)):
            c.append(float("inf"))
        dist.append(c)
    for i in range(len(dist)):
        for j in range(len(dist)):
            if i == j:
                dist[i][j] = 0
            elif Lp[i][j] != float("inf"):
                dist[i][j] = Lp[i][j]

    for x in range(len(vertices)):
        for y in range(len(vertices)):
            for z in range(len(vertices)):
                if dist[y][z] > dist[y][x] + dist[x][z]:
                    dist[y][z] = dist[y][x] + dist[x][z]
    pathPairs = dist

    for i in pathPairs:
        print(i)

    print('FloydWarshall algorithm is complete')
    # The pathPairs list will contain the 2D array of shortest paths between all pairs of vertices
    # [[w(1,1),w(1,2),...]
    #  [w(2,1),w(2,2),...]
    #  [w(3,1),w(3,2),...]
    #   ...]
    return pathPairs

def readFile(filename):
    global vertices
    global edges
    # File format:
    # <# vertices> <# edges>
    # <s> <t> <weight>
    # ...
    inFile=open(filename,'r')
    line1=inFile.readline()
    graphMatch=graphRE.match(line1)
    if not graphMatch:
        print(line1+" not properly formatted")
        quit(1)
    vertices=list(range(int(graphMatch.group(1))))
    edges=[]
    for i in range(len(vertices)):
        row=[]
        for j in range(len(vertices)):
            row.append(float("inf"))
        edges.append(row)
    for line in inFile.readlines():
        line = line.strip()
        edgeMatch=edgeRE.match(line)
        if edgeMatch:
            source=edgeMatch.group(1)
            sink=edgeMatch.group(2)
            if int(source) > len(vertices) or int(sink) > len(vertices):
                print("Attempting to insert an edge between "+source+" and "+sink+" in a graph with "+vertices+" vertices")
                quit(1)
            weight=edgeMatch.group(3)
            edges[int(source)-1][int(sink)-1]=weight
    G = (vertices,edges)
    return (vertices,edges)

def matrixEquality(a,b):
    if len(a) == 0 or len(b) == 0 or len(a) != len(b): return False
    if len(a[0]) != len(b[0]): return False
    for i,row in enumerate(a):
        for j,value in enumerate(b):
            if a[i][j] != b[i][j]:
                return False
    return True

def main(filename,algorithm):
    G=readFile(filename)
    pathPairs = []
    # G is a tuple containing a list of the vertices, and a list of the edges
    # in the format ((source,sink),weight)
    if algorithm == 'b' or algorithm == 'B':
        # TODO: Insert timing code here
        start_time = time.time()
        pathPairs = BellmanFord(G)
        print("<<%s seconds>>" % (time.time() - start_time))
    if algorithm == 'f' or algorithm == 'F':
        # TODO: Insert timing code here
        start_time = time.time()
        pathPairs = FloydWarshall(G)
        print("<<%s seconds>>" % (time.time() - start_time))
    if algorithm == 'a':
        print('running both')
        start_time = time.time()
        pathPairsBellman = BellmanFord(G)
        print("<<%s seconds>>" % (time.time() - start_time))
        start_time = time.time()
        pathPairsFloyd = FloydWarshall(G)
        print("<<%s seconds>>" % (time.time() - start_time))
        pathPairs = pathPairsBellman
        if matrixEquality(pathPairsBellman,pathPairsFloyd):
            print('Floyd-Warshall and Bellman-Ford did not produce the same result')
    with open(os.path.splitext(filename)[0]+'_shortestPaths.txt','w') as f:
        for row in pathPairs:
            for weight in row:
                f.write(str(weight)+' ')
            f.write('\n')

if __name__ == '__main__':
    args=parser.parse_args()
    main(args.filename,args.algorithm)


import argparse
from functools import lru_cache
import itertools
import random
from urllib.parse import urlparse

from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint

from autowebcompat import network
from autowebcompat import utils

SAMPLE_SIZE = 3000
BATCH_SIZE = 32
EPOCHS = 50
random.seed(42)

parser = argparse.ArgumentParser()
parser.add_argument('-n', '--network', type=str, choices=network.SUPPORTED_NETWORKS, help='Select the network to use for training')
parser.add_argument('-o', '--optimizer', type=str, choices=network.SUPPORTED_OPTIMIZERS, help='Select the optimizer to use for training')
parser.add_argument('-es', '--early_stopping', dest='early_stopping', action='store_true', help='Stop training training when validation accuracy has stopped improving.')
args = parser.parse_args()

bugs = utils.get_bugs()

utils.prepare_images()
all_images = utils.get_all_images()[:SAMPLE_SIZE]
image = utils.load_image(all_images[0])
input_shape = image.shape

TRAIN_SAMPLE = 80 * (SAMPLE_SIZE // 100)
VALIDATION_SAMPLE = 10 * (SAMPLE_SIZE // 100)
TEST_SAMPLE = SAMPLE_SIZE - (TRAIN_SAMPLE + VALIDATION_SAMPLE)

bugs_to_website = {}
for bug in bugs:
    bugs_to_website[bug['id']] = urlparse(bug['url']).netloc

@lru_cache(maxsize=len(all_images))
def site_for_image(image):
    bug = image[:image.index('_')]
    return bugs_to_website[int(bug)]

def are_same_site(image1, image2):
    return site_for_image(image1) == site_for_image(image2)

random.shuffle(all_images)
images_train, images_validation, images_test = all_images[:TRAIN_SAMPLE], all_images[TRAIN_SAMPLE:VALIDATION_SAMPLE + TRAIN_SAMPLE], all_images[SAMPLE_SIZE - TEST_SAMPLE:]

def couples_generator(images):
    # for image_couple in itertools.combinations_with_replacement(images, 2):
    for image_couple in itertools.combinations(images, 2):
        yield image_couple, 1 if are_same_site(image_couple[0], image_couple[1]) else 0

def gen_func(images):
    return utils.balance(couples_generator(images))

train_couples_len = sum(1 for e in gen_func(images_train))
validation_couples_len = sum(1 for e in gen_func(images_validation))
test_couples_len = sum(1 for e in gen_func(images_test))

print('Training with %d couples.' % train_couples_len)
print('Validation with %d couples.' % validation_couples_len)
print('Testing with %d couples.' % test_couples_len)
print(input_shape)

data_gen = utils.get_ImageDataGenerator(all_images, input_shape)
train_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_train), input_shape, data_gen, BATCH_SIZE)
validation_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_validation), input_shape, data_gen, BATCH_SIZE)
test_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_test), input_shape, data_gen, BATCH_SIZE)

model = network.create(input_shape, args.network)
network.compile(model, args.optimizer)

callbacks_list = [ModelCheckpoint('best_pretrain_model.hdf5', monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')]

if args.early_stopping:
    callbacks_list.append(EarlyStopping(monitor='val_accuracy', patience=2))

model.fit_generator(train_iterator, callbacks=callbacks_list, validation_data=validation_iterator, steps_per_epoch=train_couples_len / BATCH_SIZE, validation_steps=validation_couples_len / BATCH_SIZE, epochs=EPOCHS)

score = model.evaluate_generator(test_iterator, steps=test_couples_len / BATCH_SIZE)
print(score)

asd = utils.CouplesIterator(utils.make_infinite(gen_func, images_test[:100]), input_shape, data_gen)
predict_couples_len = sum(1 for e in utils.balance(couples_generator(images_test)))
predictions = model.predict_generator(asd, steps=predict_couples_len / BATCH_SIZE)
print(predictions)
print([a[1] for a in utils.balance(couples_generator(images_test[:100]))])


from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(ChromeDriverManager().install())
path_to_extension = r'C:\Users\lera-\AppData\Local\Google\Chrome\User Data\Default\Extensions\gighmmpiobklfepjocnamgkkbiglidom\4.46.2_0'
chrome_options = Options()
chrome_options.add_argument('load-extension=' + path_to_extension)
driver.create_options()
time.sleep(10)
driver.maximize_window()
driver.implicitly_wait(5)
first_browser_tab = driver.window_handles[0]
driver.switch_to.window(first_browser_tab)
driver.get("http://demo.automationtesting.in/WebTable.html")
driver.implicitly_wait(5)
more = driver.find_element_by_link_text("More")
more.click()
bar = driver.find_element_by_link_text("JQuery ProgressBar")
bar.click()
button = WebDriverWait(driver, 10).until(
    EC.invisibility_of_element_located((By.ID, ".ui-dialog-buttonset > button")))
start_download = driver.find_element_by_id("downloadButton")
start_download.click()
some_element= WebDriverWait(driver, 10).until(
    EC.text_to_be_present_in_element((By.CSS_SELECTOR, ".ui-dialog-buttonset"), "Cancel Download"))
close_btn = driver.find_element_by_css_selector(".ui-dialog-buttonset > button")
close_btn.click()
start_download = driver.find_element_by_id("downloadButton")
start_download.click()
some_element= WebDriverWait(driver, 10).until(
    EC.text_to_be_present_in_element((By.CSS_SELECTOR, ".progress-label"), "Complete!"))
driver.quit()


import pytest

import tvm
import tvm.testing
from tvm import relax
from tvm import tir
from tvm.ir.base import assert_structural_equal

import tvm.script
from tvm.script import tir as T, relax as R

def test_normalize_function():
    m = tir.Var("m", "int64")
    n = tir.Var("n", "int64")
    x = relax.Var("x", R.Tensor([m, n], "float16"))

    # Note: the parser automatically normalize the IR written in TVMScript,
    # so we manually construct the function here.
    mul_add = relax.Function(
        [x],
        relax.op.multiply(relax.op.add(x, x), relax.op.add(x, x)),
        ret_struct_info=R.Tensor("float16", ndim=2),
    )

    # Note: from_expr api names private function (function without global_symbol) as "main"
    before_mod = tvm.IRModule.from_expr(mul_add)

    after_mod = relax.transform.Normalize()(before_mod)

    @R.function
    def expected(x: R.Tensor(("m", "n"), "float16")) -> R.Tensor(dtype="float16", ndim=2):
        gv = R.add(x, x)
        gv1 = R.add(x, x)
        return R.multiply(gv, gv1)

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_if():
    cond = relax.Var("cond", R.Tensor([], "bool"))
    x = relax.Var("x", R.Tensor([1], "float32"))
    # TODO(relax-team): add type and shape inference for IfNode
    y = relax.Var("y")

    # Note: the parser automatically normalize the IR written in TVMScript,
    # so we manually construct the function and If here.
    f = relax.Function(
        [cond, x],
        relax.SeqExpr(
            [
                relax.BindingBlock(
                    [
                        relax.VarBinding(
                            y,
                            relax.If(
                                cond,
                                relax.op.multiply(relax.op.add(x, x), relax.op.add(x, x)),
                                relax.op.add(relax.op.multiply(x, x), relax.op.multiply(x, x)),
                            ),
                        )
                    ]
                )
            ],
            y,
        ),
        ret_struct_info=R.Tensor("float32", ndim=1),
    )

    before_mod = tvm.IRModule.from_expr(f)
    after_mod = relax.transform.Normalize()(before_mod)

    @R.function
    def expected(
        cond: R.Tensor((), "bool"), x: R.Tensor((1,), "float32")
    ) -> R.Tensor(dtype="float32", ndim=1):
        if cond:
            gv = R.add(x, x)
            gv1 = R.add(x, x)
            y = R.multiply(gv, gv1)
        else:
            gv = R.multiply(x, x)
            gv1 = R.multiply(x, x)
            y = R.add(gv, gv1)
        return y

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_no_op():
    # the normalize pass should be no-op for IR in ANF
    @tvm.script.ir_module
    class ANFMod1:
        @R.function
        def f(x: R.Tensor(dtype="float32")):
            gv = R.add(x, x)
            gv1 = R.add(gv, gv)
            gv2 = R.add(gv, gv1)
            return (gv, gv2)

    before_mod = ANFMod1
    after_mod = relax.transform.Normalize()(before_mod)
    assert_structural_equal(before_mod, after_mod, map_free_vars=True)

    @tvm.script.ir_module
    class ANFMod2:
        @R.function
        def foo(x: R.Tensor(("m", "n"), "float32")):
            m, n = T.var("int64"), T.var("int64")
            with R.dataflow():
                lv0 = R.call_tir("test.op.identity", (x,), R.Tensor((m, n), dtype="float32"))
                gv0 = R.call_tir("test.op.identity", (lv0,), R.Tensor((m, n), dtype="float32"))
                R.output(gv0)
            return gv0

    mod = ANFMod2
    mod_post = relax.transform.Normalize()(mod)

    assert_structural_equal(mod, mod_post)

def test_normalize_seq_body():
    # a seq expression with a non-leaf body should bind the body to a var as well
    x = relax.Var("x", R.Tensor([], "int32"))
    y = relax.Var("y", R.Tensor([], "int32"))
    seq = relax.SeqExpr([], relax.op.add(x, y))
    f = relax.Function(
        [x, y],
        seq,
        ret_struct_info=R.Tensor([], "int32"),
    )

    before_mod = tvm.IRModule.from_expr(f)
    after_mod = relax.transform.Normalize()(before_mod)

    @R.function
    def expected(
        x: R.Tensor((), dtype="int32"), y: R.Tensor((), dtype="int32")
    ) -> R.Tensor(ndim=0, dtype="int32"):
        # normalization inserts a binding like this
        z = R.add(x, y)
        return z

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_func_body():
    # a function with a body that is not a seq expr should have it wrapped in a seq expr
    x = relax.Var("x", R.Tensor([], "int32"))
    y = relax.Var("y", R.Tensor([], "int32"))
    f = relax.Function(
        [x, y],
        relax.op.add(x, y),
        ret_struct_info=R.Tensor([], "int32"),
    )

    before_mod = tvm.IRModule.from_expr(f)
    after_mod = relax.transform.Normalize()(before_mod)

    @R.function
    def expected(
        x: R.Tensor((), dtype="int32"), y: R.Tensor((), dtype="int32")
    ) -> R.Tensor(ndim=0, dtype="int32"):
        # result will be a seq expr where the body is a var
        z = R.add(x, y)
        return z

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_if_branches():
    # an if node's branches must be seq exprs
    x = relax.Var("x", R.Tensor([], "int32"))
    y = relax.Var("y", R.Tensor([], "int32"))
    # TODO(@relax-team): z has a shape of () and type of DynTensorType(ndim=0),
    # but normalization fails to infer these even though it should
    z = relax.Var("z")
    cond = relax.Var("cond", R.Tensor([], "bool"))
    plus = relax.op.add(x, y)
    mult = relax.op.multiply(x, y)
    if_node = relax.If(cond, plus, mult)
    seq = relax.SeqExpr([relax.BindingBlock([relax.VarBinding(z, if_node)])], z)
    f = relax.Function(
        [cond, x, y],
        seq,
        ret_struct_info=R.Tensor([], "int32"),
    )

    before_mod = tvm.IRModule.from_expr(f)
    after_mod = relax.transform.Normalize()(before_mod)

    @R.function
    def expected(
        cond: R.Tensor((), dtype="bool"),
        x: R.Tensor((), dtype="int32"),
        y: R.Tensor((), dtype="int32"),
    ) -> R.Tensor(ndim=0, dtype="int32"):
        # the bodies of the branches will be seq exprs with a binding
        if cond:
            w = R.add(x, y)
            z = w
        else:
            w = R.multiply(x, y)
            z = w
        return z

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_if_condition():
    cond = relax.Var("cond", R.Tensor([], "bool"))
    x = relax.Var("x", R.Tensor([1], "float32"))
    # TODO(relax-team): add type and shape inference for IfNode
    y = relax.Var("y")

    # The condition is wrapped in a tuple and then indexed
    f = relax.Function(
        [cond, x],
        relax.SeqExpr(
            [
                relax.BindingBlock(
                    [
                        relax.VarBinding(
                            y,
                            relax.If(
                                relax.TupleGetItem(relax.Tuple([cond]), 0),
                                relax.op.add(x, x),
                                relax.op.multiply(x, x),
                            ),
                        )
                    ]
                )
            ],
            y,
        ),
        ret_struct_info=R.Tensor("float32", ndim=1),
    )

    before_mod = tvm.IRModule.from_expr(f)
    after_mod = relax.transform.Normalize()(before_mod)

    @R.function
    def expected(
        cond: R.Tensor((), "bool"), x: R.Tensor((1,), "float32")
    ) -> R.Tensor(dtype="float32", ndim=1):
        c = R.TupleGetItem(R.tuple(cond), 0)
        if c:
            gv = R.add(x, x)
            y = gv
        else:
            gv = R.multiply(x, x)
            y = gv
        return y

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_tuple_get_item():
    x = relax.Var("x", R.Tensor([], "int32"))
    f = relax.Function(
        [x],
        relax.TupleGetItem(
            relax.TupleGetItem(
                relax.Tuple([relax.Tuple([x])]),
                0,
            ),
            0,
        ),
        ret_struct_info=R.Tensor([], "int32"),
    )

    before_mod = tvm.IRModule.from_expr(f)
    after_mod = relax.transform.Normalize()(before_mod)

    # TODO: Revisit once we canonicalize SeqExprs (part of normalization?)
    # Not using the parser this time because writing it out correctly results in
    # *one* binding block, whereas the normalized version has *two*
    idx_var = relax.Var("idx_var", R.Tuple([R.Tensor([], "int32")]))
    ret_var = relax.Var("ret", R.Tensor([], "int32"))
    expected_f = relax.Function(
        [x],
        relax.SeqExpr(
            [
                relax.BindingBlock(
                    [
                        relax.VarBinding(
                            idx_var, relax.TupleGetItem(relax.Tuple([relax.Tuple([x])]), 0)
                        )
                    ]
                ),
                relax.BindingBlock([relax.VarBinding(ret_var, relax.TupleGetItem(idx_var, 0))]),
            ],
            ret_var,
        ),
        ret_struct_info=R.Tensor([], "int32"),
    )
    expected_mod = tvm.IRModule.from_expr(expected_f)
    # apply normalization to fill in type and shape annotations (tedious otherwise)
    final_mod = relax.transform.Normalize()(expected_mod)

    assert_structural_equal(after_mod, final_mod)

def test_normalize_combine_nearby_blocks():
    x = relax.Var("x", R.Tensor([], "int32"))
    v0 = relax.Var("v0", R.Tensor([], "int32"))
    v1 = relax.Var("v1", R.Tensor([], "int32"))
    v2 = relax.Var("v2", R.Tensor([], "int32"))
    v3 = relax.Var("v3", R.Tensor([], "int32"))
    f = relax.Function(
        [x],
        relax.SeqExpr(
            [
                relax.DataflowBlock([relax.VarBinding(v0, x)]),
                relax.DataflowBlock([relax.VarBinding(v1, v0)]),
                relax.BindingBlock([relax.VarBinding(v2, v1)]),
                relax.BindingBlock([relax.VarBinding(v3, v2)]),
            ],
            v3,
        ),
        ret_struct_info=R.Tensor([], "int32"),
    )

    after_mod = relax.transform.Normalize()(tvm.IRModule.from_expr(f))

    @R.function
    def expected(x: R.Tensor((), "int32")):
        with R.dataflow():
            v0 = x
            v1 = v0
            R.output(v0, v1)
        v2 = v1
        v3 = v2
        return v3

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_nested_seq():
    x = relax.Var("x", R.Tensor([], "int32"))
    y = relax.Var("y", R.Tensor([], "int32"))
    z = relax.Var("z", R.Tensor([], "int32"))
    seq = relax.SeqExpr(
        [
            relax.BindingBlock(
                [
                    relax.VarBinding(x, relax.const(1)),
                    relax.VarBinding(
                        y,
                        relax.SeqExpr(
                            [relax.BindingBlock([relax.VarBinding(z, relax.const(2))])],
                            z,
                        ),
                    ),
                ]
            )
        ],
        y,
    )

    f = relax.Function(
        [],
        seq,
        ret_struct_info=R.Tensor([], "int32"),
    )
    after_mod = relax.transform.Normalize()(tvm.IRModule.from_expr(f))

    @R.function
    def expected():
        x = relax.const(1)
        z = relax.const(2)
        y = z
        return y

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_nested_seq_dataflow():
    x = relax.Var("x", R.Tensor([], "int32"))
    y = relax.Var("y", R.Tensor([], "int32"))
    z = relax.Var("z", R.Tensor([], "int32"))
    q = relax.Var("u", R.Tensor([], "int32"))
    w = relax.DataflowVar("w", R.Tensor([], "int32"))
    u = relax.Var("u", R.Tensor([], "int32"))
    seq = relax.SeqExpr(
        [
            relax.BindingBlock(
                [
                    relax.VarBinding(x, relax.const(1)),
                    relax.VarBinding(
                        y,
                        relax.SeqExpr(
                            [
                                relax.BindingBlock([relax.VarBinding(q, relax.const(2))]),
                                relax.DataflowBlock(
                                    [
                                        relax.VarBinding(w, q),
                                        relax.VarBinding(u, w),
                                    ]
                                ),
                                relax.BindingBlock([relax.VarBinding(z, u)]),
                            ],
                            z,
                        ),
                    ),
                ]
            )
        ],
        y,
    )

    f = relax.Function(
        [],
        seq,
        ret_struct_info=R.Tensor([], "int32"),
    )
    after_mod = relax.transform.Normalize()(tvm.IRModule.from_expr(f))

    @R.function
    def expected():
        x = relax.const(1)
        q = relax.const(2)
        with R.dataflow():
            w = q
            u = w
            R.output(u)
        z = u
        y = z
        return y

    assert_structural_equal(after_mod["main"], expected)

def test_normalize_deeply_nested_seq():
    x = relax.Var("x", R.Tensor([], "int32"))
    y = relax.Var("y", R.Tensor([], "int32"))
    z = relax.Var("z", R.Tensor([], "int32"))
    u = relax.Var("u", R.Tensor([], "int32"))
    v = relax.Var("v", R.Tensor([], "int32"))
    w = relax.Var("w", R.Tensor([], "int32"))
    _ = relax.Var("w", R.Tensor([], "int32"))
    seq = relax.SeqExpr(
        [
            relax.BindingBlock(
                [
                    relax.VarBinding(x, relax.const(1)),
                    relax.VarBinding(
                        y,
                        relax.SeqExpr(
                            [
                                relax.BindingBlock(
                                    [
                                        relax.VarBinding(
                                            z,
                                            relax.SeqExpr(
                                                [
                                                    relax.BindingBlock(
                                                        [
                                                            relax.VarBinding(u, relax.const(2)),
                                                            relax.MatchCast(
                                                                _, u, R.Tensor([], "int32")
                                                            ),
                                                            relax.VarBinding(v, u),
                                                            relax.MatchCast(
                                                                w, v, R.Tensor([], "int32")
                                                            ),
                                                        ]
                                                    )
                                                ],
                                                w,
                                            ),
                                        )
                                    ]
                                )
                            ],
                            z,
                        ),
                    ),
                ]
            )
        ],
        y,
    )

    f = relax.Function(
        [],
        seq,
        ret_struct_info=R.Tensor([], "int32"),
    )
    after_mod = relax.transform.Normalize()(tvm.IRModule.from_expr(f))

    @R.function
    def expected():
        x = relax.const(1)
        u = relax.const(2)
        _ = R.match_cast(u, R.Tensor((), "int32"))
        v = u
        w = R.match_cast(v, R.Tensor((), "int32"))
        z = w
        y = z
        return y

    assert_structural_equal(after_mod["main"], expected)

@pytest.mark.xfail()
def test_nesting_non_dataflow_in_dataflow_error():
    x = relax.DataflowVar("x", R.Tensor([], "int32"))
    y = relax.Var("y", R.Tensor([], "int32"))
    z = relax.Var("z", R.Tensor([], "int32"))
    seq = relax.SeqExpr(
        [
            relax.DataflowBlock(
                [
                    relax.VarBinding(x, relax.const(1)),
                    relax.VarBinding(
                        y,
                        relax.SeqExpr(
                            [relax.BindingBlock([relax.VarBinding(z, relax.const(2))])],
                            z,
                        ),
                    ),
                ]
            )
        ],
        y,
    )
    f = relax.Function(
        [],
        seq,
        ret_struct_info=R.Tensor([], "int32"),
    )
    relax.transform.Normalize()(tvm.IRModule.from_expr(f))
    # should fail due to a normal binding block being inside a dataflowblock

if __name__ == "__main__":
    tvm.testing.main()


from typing import Any, List
# from entity_linking.REL import rel_wrapper
import codecs
import wikipediaapi
import time
import requests
import logging

wiki = wikipediaapi.Wikipedia('en')

API_URL = "https://rel.cs.ru.nl/api"

def entity_link(text):

    if text is None:
        return []

    res = requests.post(API_URL, json={
            "text": text,
            "spans": []
        }).json()

    res = [(x[3]) for x in res]
    return res

def get_wiki_text(wiki_title):
    page = wiki.page(wiki_title)

    if page.exists():
        return page.text
    else:
        return None

# Reading Comprehension Model
class AllenNLPRoBERTa():
    def __init__(self):
        from allennlp.predictors.predictor import Predictor
        import allennlp_models.rc
        self.predictor = Predictor.from_path("https://storage.googleapis.com/allennlp-public-models/transformer-qa.2021-02-11.tar.gz")

    def predict(self, question:str, context:str) -> str:
        try:
            results = self.predictor.predict(question, context)
            if results['best_span'][0] == -1:
                return None
            # return results
            return results['best_span_str'], results['best_span_probs'], results['best_span_scores']
        except:
            return None

# Wikipedia text splitter
filtered_sections = set(['References', 'Sources', 'Further reading', 'External links', 'See also'])

def get_text_by_section(section, out:List[str]) -> None:
    if section.title in filtered_sections:
        return

    if len(section.sections) == 0:
        out.append(section.text)
    else:
        for s in section.sections:
            get_text_by_section(s, out)

def flatten(t:List[List[Any]]) -> List[Any]:
    return [item for sublist in t for item in sublist]

def split_article(wiki_title:str, max_tokens:int=-1) -> List[str]:
    page = wiki.page(wiki_title)

    # print(page.sections)
    all_text = []
    get_text_by_section(page, all_text)

    # This does not actually guarantee text is split into less than max_tokens lol
    if max_tokens > 0:
        def split_long_texts(text):
            if len(text.split()) > max_tokens:
                return text.split('\n')
            return [text]
        all_text = flatten([split_long_texts(text) for text in all_text])

    return all_text

if __name__ == '__main__':

    logging.getLogger('allennlp.common.params').disabled = True
    logging.getLogger('allennlp.nn.initializers').disabled = True
    logging.getLogger('allennlp.modules.token_embedders.embedding').setLevel(logging.INFO)
    logging.getLogger('urllib3.connectionpool').disabled = True

    prompt = """
    Robert Walker argued that failing to take this action would lead to an overflow of Northern insane asylums and British intervention.
    Juan Almonte resigned his diplomatic post in indignation over this event.
    Isaac Van Zandt began discussing this plan with Abel Upshur before Upshur died.
    Anson Jones proposed this plan, which reduced his own power but reserved the weaker side the right to split
    into five parts in the future. Five years after it, a payment of 10 million dollars helped the area at issue repay debts.
    The Regulator-Moderator war was calmed just before this deal was struck.
    The question of whether the Nueces River became a southern border in this transaction led to war later in James K Polk's presidency.
    For 10 points, name this deal that ended the independent Lone Star Republic.
    """

    sentences = prompt.split(".")
    model = AllenNLPRoBERTa()

    for sent in sentences:
        entities = entity_link(sent)
        print(entities)
        for e in entities:
            sections = split_article(e)
            for section in sections:
                prediction = model.predict(sent, section)
                if prediction:
                    data = {
                        # 'label': question['page'],
                        'model_output': prediction[0],
                        'model_output_probs': prediction[1],
                        'model_output_score': prediction[2],
                    }
                    print(data)

{'Predictor': 'allennlp.predictors.predictor.Predictor', 'allennlp_models.rc': 'allennlp_models.rc'}


import copy
from collections import Counter

with open('../inputs/day_14_input.txt') as f:
    lines = f.readlines()

    base = list(lines[0].strip())
    rules = {}

    for l in lines:
        if '->' in l:
            res = l.strip().split(' -> ')
            rules[res[0]] = res[1]

    steps = 40
    for i in range(steps):
        base_copy = []
        for j in range(len(base)-1):
            base_copy.append(base[j])
            if base[j] + base[j+1] in rules:
                base_copy.append(rules[base[j]+base[j+1]])
        base_copy.append(base[len(base)-1])

        base = base_copy
        #print("After step {}: {}".format(i, base))

    c = Counter(base).most_common()
    print(c[0], c[-1], c[0][1]-c[-1][1])


#!/usr/bin/env python3
#
# cmp_pickle.py file1.pickle file2.pickle
#

import pickle
import sys
import re

from deepdiff import DeepDiff

def report(ddiff):
    for k,v in ddiff.items():
        print('#',k)
        if re.match(r'.*_added', k):
            for x in v:
                print('+', x)
        elif re.match(r'.*_removed', k):
            for x in v:
                print('-', x)
        elif re.match(r'.*_changed', k):
            for kk, vv in v.items():
                print('*', kk)
                for kkk, vvv in vv.items():
                    if re.match(r'new_', kkk):
                        print('  +', vvv)
                    else:
                        print('  -', vvv)
        else:
            print('?', k,v)

with open(sys.argv[1], "rb") as f:
    p1 = pickle.load(f)
with open(sys.argv[2], "rb") as f:
    p2 = pickle.load(f)

def setify_labels(data):
    if isinstance(data, dict):
        for k,v in data.items():
            data[k] = setify_labels(v)
        if 'labels' in data:
            data['labels'] = set(data['labels'])
    return data

p1 = setify_labels(p1)
p2 = setify_labels(p2)

ddiff = DeepDiff(p1.get('scantree'), p2.get('scantree'))

report(ddiff)


import discord
from discord.ext import commands

class MinecraftCog(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command(name="minecraft")
    @commands.cooldown(1, 3, commands.BucketType.user)
    async def minecraft(self, ctx):
        await ctx.send("https://tenor.com/view/minecraft-gif-9643254")
        await ctx.send("https://tenor.com/view/minecraft-sonic-minecraft-sonic-sonic3d-sonic3d-blast-gif-19349494")

def setup(client):
    client.add_cog(MinecraftCog(client))


#DISCLAIMER - The logic of the code on Line 18 has been taken from SteamGridDB (https://github.com/SteamGridDB/steamgriddb-manager) [MIT License].

import os, binascii, json, wget, ssl, traceback, sys
import configpath
from gameName import rspchar

def checkartworkexists(artwork_path, image):

    check_flag = 0

    for i in os.listdir(artwork_path):

        if i in image:
            check_flag = 1
            break

    if check_flag == 1:

        return True
    else:

        return False

def addartwork(appname, exe, userid, simplified_gamename):

    #As Steam does not store AppIDs of Non-Steam games in any config file, this makes it difficult to add artwork without knowing the AppID.
    #One way is to add the game, let Steam assign the AppID and then try to find the AppID using regex.
    #Fortunately, SteamGridDB (MIT License) has found a simple and efficient solution to calculate the AppID.

    #ALL CREDIT GOES TO THE STEAMGRIDDB DEVS FOR THIS IMPLEMENTATION (Line 18).
    #Here's how the it's explained in their source code -
        #To create a grid image we must compute the Steam ID, which is just crc32(target + label) + "02000000", using IEEE standard polynomials.
    appid = binascii.crc32(str.encode(exe + appname)) | 0x80000000
    print("Steam AppID for " + appname + " is " + str(appid))

    #Path to Steam grid folder
    artwork_path = os.path.expanduser("~") + '/.steam/steam/userdata/' + str(userid) + '/config/grid'

    #Artwork types
    coverart =  [str(appid) + 'p.jpg', str(appid) + 'p.png']
    backgroundart = [str(appid) + '_hero.jpg', str(appid) + '_hero.png']
    bigpictureart = [str(appid) + '.jpg', str(appid) + '.png']

    #Check if the folder exists, create if not
    grid_exists = os.path.isdir(artwork_path)

    if not grid_exists:
        os.makedirs(artwork_path)
        print("Created grid folder in ", artwork_path)

    try:

        print("Checking Artwork...")

        #GameFiles dir if non-Flatpak
        if configpath.is_flatpak == True:
            GameFiles = ""
        else:
            GameFiles = "GameFiles/"

        #Reading from file
        openscript = open(GameFiles + simplified_gamename + ".sh", 'r')
        readscript = openscript.read()
        openscript.close()

        #Avoid SSL certificate error
        ssl._create_default_https_context = ssl._create_unverified_context

        #Check if game is Epic or GOG
        if "EPIC" in readscript:

            #print("Epic")

            with open(configpath.heroiclibrarypath, encoding='utf-8') as l:
                epicinstalled = json.load(l)

            for i in epicinstalled['library']:

                gamename = rspchar(i['title'])

                if appname == gamename:

                    #Cover Art
                    if checkartworkexists(artwork_path, coverart) == False:

                        image_url = i['art_square']
                        print("Downloading Cover Art from " + image_url)
                        wget.download(image_url, out = artwork_path)
                        os.rename(artwork_path + '/' + image_url.split("/")[-1], artwork_path + '/' + coverart[0])
                    else:
                        print("Covert Art exists")

                    #Background Art
                    if checkartworkexists(artwork_path, backgroundart) == False:

                        image_url = i['art_cover']
                        print("Downloading Background Art from " + image_url)
                        wget.download(image_url, out = artwork_path)
                        os.rename(artwork_path + '/' + image_url.split("/")[-1], artwork_path + '/' + backgroundart[0])
                    else:
                        print("Background Art exists")

                    #BigPicture Art
                    if checkartworkexists(artwork_path, bigpictureart) == False:

                        image_url = i['art_cover']
                        print("Downloading BigPicture Art from " + image_url)
                        wget.download(image_url, out = artwork_path)
                        os.rename(artwork_path + '/' + image_url.split("/")[-1], artwork_path + '/' + bigpictureart[0])
                    else:
                        print("BigPicture Art exists")
        elif "GOG" in readscript:

            #print("GOG")

            with open(configpath.goglibrarypath, encoding='utf-8') as l:
                goginstalled = json.load(l)

            for i in goginstalled['games']:

                gamename = rspchar(i['title'])

                if appname == gamename:

                    #Cover Art
                    if checkartworkexists(artwork_path, coverart) == False:

                        image_url = i['art_square']
                        print("Downloading Cover Art from " + image_url)
                        wget.download(image_url, out = artwork_path)
                        extract_image_url = image_url.split("/")[-1]
                        os.rename(artwork_path + '/' + extract_image_url.split("?")[0], artwork_path + '/' + coverart[0])
                    else:
                        print("Covert Art exists")

                    #Background Art
                    if checkartworkexists(artwork_path, backgroundart) == False:

                        image_url = i['art_cover']
                        print("Downloading Background Art from " + image_url)
                        wget.download(image_url, out = artwork_path)
                        os.rename(artwork_path + '/' + image_url.split("/")[-1], artwork_path + '/' + backgroundart[0])
                    else:
                        print("Background Art exists")

                    #BigPicture Art
                    if checkartworkexists(artwork_path, bigpictureart) == False:

                        image_url = i['art_cover']
                        print("Downloading BigPicture Art from " + image_url)
                        wget.download(image_url, out = artwork_path)
                        os.rename(artwork_path + '/' + image_url.split("/")[-1], artwork_path + '/' + bigpictureart[0])
    except Exception:

        print(traceback.format_exc())
        os.system('zenity --error --title="Process Failed" --text="Failed to add artwork. Please check the log for the error and consider reporting it as an issue on Github." --width=400')
        sys.exit()


# -*- coding: utf-8 -*-
"""
Created on Mon Jul 17 22:39:24 2017

@author: kunal
"""

import simplekml

def userInputLatAndLong(lat,long):
    kml=simplekml.Kml()
    kml.newpoint(name="Sample",coords=[(lat,long)])
    kml.save("D:\\pythonscripts\\point.kml")

userLat=input("Please enter the desired value of latitude :")
userLong=input("Please enter the desired value of longitude : ")
userInputLatAndLong(userLat,userLong)


def translation_word(word, model):
    import json
    import requests

    def translate(word):
        url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
        type = ['auto', 'EN2ZH_CN', 'ZH_CN2EN'][model]
        key = {
            'type': type,
            'i': word,
            "doctype": "json",
            "version": "2.1",
            "keyfrom": "fanyi.web",
            "ue": "UTF-8",
            "action": "FY_BY_CLICKBUTTON",
            "typoResult": "true"
        }
        response = requests.post(url, data=key)
        if response.status_code == 200:
            return response.text
        else:
            return None

    def get_reuslt(repsonse):
        result = json.loads(repsonse)
        fanyi = result['translateResult'][-1][-1]['tgt']  # or ['src']
        return fanyi

    fanyi = get_reuslt(translate(word))
    return fanyi

def is_Chinese(question):
    for ch in question:
        if '\u4e00' <= ch <= '\u9fff':
            return True
    return False