import logging
import os
import shutil
from typing import Tuple, List, Union
from calendar import monthrange
import uuid
import requests
import pandas as pd
import numpy as np
from datetime import datetime, date
import time
from tqdm.auto import tqdm
from CommonTools.settings import SETTINGS

def str2dt(dt_str: Union[str, date], format = "%Y-%m-%d") -> date:
    if isinstance(dt_str, str):
        return datetime.strptime(dt_str, format).date()
    if isinstance(dt_str, date):
        return dt_str
    if isinstance(dt_str, datetime):
        return dt_str.date()
    else:
        return dt_str

def dt2str(dt: Union[str, date], format = "%Y-%m-%d") -> str:
    if isinstance(dt, str):
        return dt
    if isinstance(dt, (date, datetime)):
        return dt.strftime(format)
    else:
        return dt

def get_month_end_date(snap_year:int, snap_month:int) -> date:
    month_end_date = date(
        snap_year, snap_month,
        monthrange(snap_year, snap_month)[1]
    )
    return month_end_date

def save_xls(names, list_dfs, xls_path):
    with pd.ExcelWriter(xls_path) as writer:
        for n, df in enumerate(list_dfs):
            print(names[n])
            df.to_excel(writer, names[n], index = False)
        #             df.to_excel(writer,'sheet%s' % n, index=False)
        writer.save()

def read_sql_from_file(file: str) -> str:
    with open(file) as obj:
        sql = obj.read()
    return sql

def complie_sql(sql : str, *args) -> str:
    # fill args into sql templates with ?
    arg_num_sql = sql.count("?")
    if len(args) != arg_num_sql:
        raise ValueError(f"SQL temp contains {arg_num_sql} placeholders(?) while only {len(args)} parsed")
    return sql.replace("?", "'{}'").format(*args)

def render_sql_from_file(file: str, **fill_params) -> str:
    with open(file) as obj:
        sql = obj.read()
    if fill_params:
        sql = sql.format(**fill_params)
    return sql

def df_strip(data):
    """
    Remove leading and trailing blank for each column

    Parameters:
    data : DataFrame

    Returns : DataFrame
    """
    for var in data.columns:
        if data[var][data[var].notna()].shape[0] == 0:
            pass
        else:
            if type(data[var][data[var].notna()].iloc[0]) is str:
                data[var] = data[var].str.strip()
    return data

def missing_val_chk(data, var):
    if data[var].isnull().any(axis=None):
        print('There are ' + str(data[var].isnull().sum()) + ' missing values for column ' + var + '.')
        string1 = ','.join(['There are ', str(data[var].isnull().sum()), ' missing values for column ', var, '.'])
        #string1=" "
    else:
        print('Column ' + var + ' has no missing value.')
        string1 = 'Column ' + var + ' has no missing value.'
        #string1=" i"
    return string1

def download_jdbc_jar(jar_file_url, jdbc_driver_folder, jar_file_name: str = None):
    if not os.path.exists(jdbc_driver_folder):
        os.makedirs(jdbc_driver_folder)

    if not jar_file_name:
        jar_file_name = jar_file_url.split('/')[-1]

    jar_path = os.path.join(jdbc_driver_folder, jar_file_name)
    with requests.get(
            jar_file_url,
            # proxies = {
            #     'http': SETTINGS.HTTP_PROXY,
            #     'https': SETTINGS.HTTPS_PROXY
            # }
    ) as response, open(jar_path, 'wb') as out_file:
        out_file.write(response.content)

    logging.info(f"Successfully download JDBC jar from {jar_file_url} and saved to {jar_path}")
    return jar_path

def download_jdbc_jars(jdbc_driver_folder):
    jars = [
        {
            'db_provider' : 'db2',
            'jar_file_url' : 'https://repo1.maven.org/maven2/com/ibm/db2/jcc/db2jcc/db2jcc4/db2jcc-db2jcc4.jar',
            'jar_file_name' : 'db2jcc-db2jcc4.jar'
        },
        {
            'db_provider' : 'mysql',
            'jar_file_url' : 'https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.27/mysql-connector-java-8.0.27.jar',
            'jar_file_name' : 'mysql-connector-java.jar'
        },
        {
            'db_provider' : 'sqlite',
            'jar_file_url' : 'https://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/3.36.0.3/sqlite-jdbc-3.36.0.3.jar',
            'jar_file_name' : 'sqlite-jdbc.jar'
        },
        {
            'db_provider' : 'sqlserver',
            'jar_file_url' : 'https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/8.4.0.jre8/mssql-jdbc-8.4.0.jre8.jar',
            'jar_file_name' : 'mssql-jdbc-8.4.0.jre8.jar'
        },
        {
            'db_provider' : 'oracle',
            'jar_file_url' : 'https://repo1.maven.org/maven2/com/oracle/database/jdbc/ojdbc8/19.3.0.0/ojdbc8-19.3.0.0.jar',
            'jar_file_name' : 'ojdbc.jar'
        },
        {
            'db_provider' : 'presto',
            'jar_file_url' : 'https://repo1.maven.org/maven2/io/prestosql/presto-jdbc/347/presto-jdbc-347.jar',
            'jar_file_name' : 'presto.jar'
        },
        {
            'db_provider' : 'postgresql',
            'jar_file_url' : 'https://repo1.maven.org/maven2/org/postgresql/postgresql/42.2.18/postgresql-42.2.18.jar',
            'jar_file_name' : 'postgresql.jar'
        },
        {
            'db_provider': 'sas',
            'jar_file_url': 'https://repos.spark-packages.org/saurfang/spark-sas7bdat/3.0.0-s_2.12/spark-sas7bdat-3.0.0-s_2.12.jar',
            'jar_file_name': 'spark-sas.jar'
        },
        {
            'db_provider': 'parso',  # use to read sas dataset
            'jar_file_url': 'https://repo1.maven.org/maven2/com/epam/parso/2.0.14/parso-2.0.14.jar',
            'jar_file_name': 'parso.jar'
        }
    ]

    if not os.path.exists(jdbc_driver_folder):
        os.makedirs(jdbc_driver_folder)

    for jar in jars:
        db_provider = jar.get('db_provider')
        jar_file_url = jar.get('jar_file_url')
        jar_file_name = jar.get('jar_file_name')

        download_jdbc_jar(jar_file_url, jdbc_driver_folder, jar_file_name)

def readExcel(file, sheetName: str = None, dtype = "str", na_filter: bool = True):
    if file.endswith(".xlsx") or file.endswith(".xls"):
        try:
            with pd.ExcelFile(file) as reader:
                try:
                    file = pd.read_excel(reader, sheetname = sheetName, engine = "python", encoding = "utf8", dtype = dtype, na_filter = na_filter )
                except:
                    file = pd.read_excel(reader, sheet_name = sheetName, dtype = dtype, na_filter = na_filter)
        except Exception as e:
            logging.critical(e)
            file = pd.DataFrame()
    return file

def compare_prod_test(prod_df: pd.DataFrame, test_df: pd.DataFrame):
    """Compare prod dataframe and test dataframe on missing value and matching rate by feature

    :param prod_df: prod version df, benchmark, make sure the joining index is set (single or multiindex)
    :param test_df: test version df, challenger, make sure the joining index is set (single or multiindex)
    :return:
    """
    features = pd.Index(prod_df.columns).intersection(test_df.columns)
    print(f"Size Check:")
    print(f"\tprod_df: {prod_df.shape}  vs. test_df: {test_df.shape}")
    print(f"\t{len(features)} common features found: {list(features)}")
    compare_df = pd.merge(
        prod_df[features],
        test_df[features],
        how='inner',
        left_index=True,
        right_index=True,
        suffixes=('_PROD', '_TEST')
    )
    print(
        f"\t{len(compare_df)} common records found, match rate: prod_df {len(compare_df) / len(prod_df) :.1%}, test_df {len(compare_df) / len(test_df) :.1%}")

    print("\nMatch Rate By Feature")
    # 1.Numerical Feature Check
    numeric_compare = compare_df.select_dtypes(include=['int32', 'int64', 'float32', 'float64'])
    numeric_features = numeric_compare.columns.str.replace('_PROD', '').str.replace('_TEST', '').drop_duplicates(
        keep='first')

    # 2.Categorical Feature Check
    categ_compare = compare_df.select_dtypes(include=['object'])
    categ_features = categ_compare.columns.str.replace('_PROD', '').str.replace('_TEST', '').drop_duplicates(
        keep='first')

    # 3.Datetime Feature Check
    dt_compare = compare_df.select_dtypes(include=['datetime64'])
    dt_features = dt_compare.columns.str.replace('_PROD', '').str.replace('_TEST', '').drop_duplicates(keep='first')

    matching = pd.DataFrame(index=features, columns=['DTYPE', 'NA_PROD', 'NA_TEST', 'MATCH', 'MATCH_VALID'])
    unmatching = {}

    for f in tqdm(numeric_features):
        matching.loc[f, 'DTYPE'] = 'NUM'
        matching.loc[f, 'MATCH'] = np.isclose(compare_df[f + "_PROD"], compare_df[f + "_TEST"]).sum() / len(compare_df)
        matching.loc[f, 'NA_PROD'] = compare_df[f + "_PROD"].isnull().sum() / len(compare_df)
        matching.loc[f, 'NA_TEST'] = compare_df[f + "_TEST"].isnull().sum() / len(compare_df)
        # DROP NA MATCH
        compare_df_valid = compare_df.dropna(subset = [f + "_PROD", f + "_TEST"])
        matching.loc[f, 'MATCH_VALID'] = np.isclose(compare_df_valid[f + "_PROD"], compare_df_valid[f + "_TEST"]).sum() / len(compare_df_valid)

        unmatching[f] = compare_df.loc[
            ~np.isclose(compare_df[f + "_PROD"], compare_df[f + "_TEST"]), [f + "_PROD", f + "_TEST"]]

    for f in tqdm(categ_features):
        matching.loc[f, 'DTYPE'] = 'CATEG'
        matching.loc[f, 'MATCH'] = (compare_df[f + "_PROD"] == compare_df[f + "_TEST"]).sum() / len(compare_df)
        matching.loc[f, 'NA_PROD'] = compare_df[f + "_PROD"].isnull().sum() / len(compare_df)
        matching.loc[f, 'NA_TEST'] = compare_df[f + "_TEST"].isnull().sum() / len(compare_df)
        # DROP NA MATCH
        compare_df_valid = compare_df.dropna(subset=[f + "_PROD", f + "_TEST"])
        matching.loc[f, 'MATCH_VALID'] = (compare_df_valid[f + "_PROD"] == compare_df_valid[f + "_TEST"]).sum() / len(compare_df_valid)

        unmatching[f] = compare_df.loc[compare_df[f + "_PROD"] != compare_df[f + "_TEST"], [f + "_PROD", f + "_TEST"]]

    for f in tqdm(dt_features):
        matching.loc[f, 'DTYPE'] = 'CATEG'
        matching.loc[f, 'MATCH'] = (pd.to_datetime(compare_df[f + "_PROD"]) == pd.to_datetime(
            compare_df[f + "_TEST"])).sum() / len(compare_df)
        matching.loc[f, 'NA_PROD'] = compare_df[f + "_PROD"].isnull().sum() / len(compare_df)
        matching.loc[f, 'NA_TEST'] = compare_df[f + "_TEST"].isnull().sum() / len(compare_df)
        # DROP NA MATCH
        compare_df_valid = compare_df.dropna(subset=[f + "_PROD", f + "_TEST"])
        matching.loc[f, 'MATCH_VALID'] = (pd.to_datetime(compare_df_valid[f + "_PROD"]) == pd.to_datetime(
            compare_df_valid[f + "_TEST"])).sum() / len(compare_df_valid)

        unmatching[f] = compare_df.loc[
            pd.to_datetime(compare_df[f + "_PROD"]) != pd.to_datetime(compare_df[f + "_PROD"]), [f + "_PROD",
                                                                                f + "_TEST"]]

    matching.sort_values(by=['MATCH'], ascending=[False], inplace=True)
    return matching, unmatching

def error_analysis(unmatching_f: pd.DataFrame, dtype: str):
    """

    :param unmatching_f:
    :param dtype:{INT, FLOAT, DATE, CATEG}
    :return:
    """

    un = unmatching_f.copy()
    if dtype == 'INT':
        un['DIFF'] = un.iloc[:, 0] - un.iloc[:, 1]
        return un['DIFF'].value_counts(normalize = True, dropna = False)
    if dtype == 'FLOAT':
        un['DIFF'] = un.iloc[:, 0] - un.iloc[:, 1]
        return un.describe()
    if dtype == 'CATEG':
        from sklearn.metrics import confusion_matrix

        value_categs = un.iloc[:, 0].value_counts().index
        return pd.DataFrame(
            confusion_matrix(un.iloc[:, 0], un.iloc[:, 1], labels = value_categs),
            columns = value_categs,
            index = value_categs
        ).style.applymap(lambda x: "background-color: #F28C77" if x > 0 else "background-color: white")

def loop_timer(total_steps: int) -> Tuple[int, float, float, float, float]:
    _start = time.time()
    for step in range(total_steps):
        _lap = time.time()
        progress = (step + 1) / total_steps
        time_step = _lap - _start
        time_elasped = _lap - _start
        time_remaining = time_elasped / (step + 1) * (total_steps - step - 1)
        yield step + 1, progress, time_step, time_elasped, time_remaining

def convert_np_dtype(df: pd.DataFrame, exclude_cols: List[str] = None, keep_nonnull_int: bool = True, keep_date:bool = True) -> pd.DataFrame:
    """convert dtype from pandas to numpy compatible (sklearn), i.e., only object, datetime, int and float type

    :param df:
    :param exclude_cols: columns to exclude from conversion
    :param keep_nonnull_int: if True, will convert non-null nullable Integer to numpy non-nullable integer,
            otherwise will convert nullable Integer to float
    :param keep_date: if False, will convert datetime type to date string
    :return:
    """

    if exclude_cols is None:
        exclude_cols = []
    # convert nullable integer
    dtypes = df.dtypes
    nullable_int_cols = dtypes[
        (dtypes.apply(lambda d: d.name).str.startswith("Int"))
        | (dtypes.apply(lambda d: d.name).str.startswith("UInt"))
        ].index

    non_null_int_cols = []
    if keep_nonnull_int:
        # will convert non-null nullable Integer to numpy non-nullable integer
        null_int_stat = df[nullable_int_cols].isna().sum()
        non_null_int_cols = null_int_stat[null_int_stat == 0].index
        nullable_int_cols = nullable_int_cols.difference(non_null_int_cols)

    if not keep_date:
        for col in df.select_dtypes(include=['datetime']):
            df[col] = df[col].dt.date.apply(dt2str)

    return df.astype(
        {
            c: 'object'
            for c in df.select_dtypes(exclude=['number', 'datetime']).columns
            if c not in exclude_cols
        }
    ).astype(
        {
            c: 'float'
            for c in nullable_int_cols
            if c not in exclude_cols
        }
    ).astype(
        {
            c: 'int'
            for c in non_null_int_cols
            if c not in exclude_cols
        }
    )

class TempFolder:
    def __init__(self, root_path: str):
        folder = str(uuid.uuid4())
        self.temp_folder = os.path.join(root_path, folder)

    def __enter__(self):
        if not os.path.exists(self.temp_folder):
            os.makedirs(self.temp_folder)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        shutil.rmtree(self.temp_folder, ignore_errors = True)

    def getTempFolderPath(self):
        return self.temp_folder

if __name__ == '__main__':
    dt = date(2021, 1, 4)
    print(dt2str(dt))


from typing import Callable

import hypothesis.strategies as st
import numpy as np
from hypothesis import assume, note
from hypothesis.extra.numpy import arrays

from .. import squigglepy as sq

CONTINUOUS_DISTRIBUTIONS = [
    sq.uniform,
    sq.norm,
    sq.lognorm,
    # sq.to, # Disabled to help isolate errors to either normal or lognormal
    sq.beta,
    sq.tdist,
    # sq.log_tdist,  # TODO: Re-enable when overflows are fixed
    sq.triangular,
    sq.chisquare,
    sq.exponential,
    sq.gamma,
    sq.pareto,
    sq.pert,
]

DISCRETE_DISTRIBUTIONS = [
    sq.binomial,
    sq.bernoulli,
    sq.discrete,
    sq.poisson,
]

ALL_DISTRIBUTIONS = CONTINUOUS_DISTRIBUTIONS + DISCRETE_DISTRIBUTIONS

@st.composite
def distributions_with_correlation(draw, min_size=2, max_size=20, continuous_only=False):
    dists = tuple(
        draw(
            st.lists(
                random_distributions(continuous_only),
                min_size=min_size,
                max_size=max_size,
            )
        )
    )
    corr = draw(correlation_matrices(min_size=len(dists), max_size=len(dists)))
    note(f"Distributions: {dists}")
    note(f"Correlation matrix: {corr}")
    return dists, corr

@st.composite
def correlation_matrices(draw, min_size=2, max_size=20):
    # Generate a random list of correlations
    n_variables = draw(st.integers(min_size, max_size))
    correlation_matrix = draw(
        arrays(np.float64, (n_variables, n_variables), elements=st.floats(-0.99, 0.99))
    )
    # Reflect the matrix
    correlation_matrix = np.tril(correlation_matrix) + np.tril(correlation_matrix, -1).T
    # Fill the diagonal with 1s
    np.fill_diagonal(correlation_matrix, 1)

    # Reject if not positive semi-definite
    assume(np.all(np.linalg.eigvals(correlation_matrix) >= 0))

    return correlation_matrix

@st.composite
def random_distributions(
    draw, continuous_only: bool = False, discrete_only: bool = False
) -> sq.OperableDistribution:
    assert not (continuous_only and discrete_only), "Cannot be both continuous and discrete"

    if continuous_only:
        dist = instantiate_with_parameters(draw, draw(st.sampled_from(CONTINUOUS_DISTRIBUTIONS)))
        assert isinstance(dist, sq.ContinuousDistribution), f"{dist} is not continuous"
    elif discrete_only:
        dist = instantiate_with_parameters(draw, draw(st.sampled_from(DISCRETE_DISTRIBUTIONS)))
        assert isinstance(dist, sq.DiscreteDistribution), f"{dist} is not discrete"
    else:
        dist = instantiate_with_parameters(draw, draw(st.sampled_from(ALL_DISTRIBUTIONS)))
        assert isinstance(dist, sq.OperableDistribution), f"{dist} is not an operable distribution"

    return dist

def instantiate_with_parameters(draw, dist_fn: Callable) -> sq.OperableDistribution:
    if dist_fn == sq.uniform:
        a = draw(
            st.floats(-1e30, 1e30, allow_nan=False, allow_infinity=False, allow_subnormal=False)
        )
        b = draw(
            st.floats(
                min_value=a + 2,
                max_value=a + 1e30,
                allow_nan=False,
                allow_subnormal=False,
                exclude_min=True,
            )
        )
        return dist_fn(a, b)
    elif dist_fn in (sq.norm, sq.tdist):
        # Distributions that receive confidence intervals
        a = draw(
            st.floats(-1e30, 1e30, allow_infinity=False, allow_nan=False, allow_subnormal=False)
        )
        b = draw(
            st.floats(
                min_value=a + 0.01,
                max_value=a + 1e30,
                allow_infinity=False,
                allow_nan=False,
                exclude_min=True,
                allow_subnormal=False,
            )
        )
        return dist_fn(a, b)
    elif dist_fn in (sq.lognorm, sq.log_tdist):
        # Distributions that receive confidence intervals starting from 0
        a = draw(
            st.floats(
                0.005,
                1e20,
                allow_infinity=False,
                allow_nan=False,
                allow_subnormal=False,
                exclude_min=True,
            )
        )
        b = draw(
            st.floats(
                min_value=a + 0.05,
                max_value=a + 1e20,
                allow_infinity=False,
                allow_nan=False,
                exclude_min=True,
                allow_subnormal=False,
            )
        )
        return dist_fn(a, b)
    elif dist_fn == sq.binomial:
        n = draw(st.integers(1, 500))
        p = draw(st.floats(0.01, 0.999, exclude_min=True, exclude_max=True))
        return dist_fn(n, p)
    elif dist_fn == sq.bernoulli:
        p = draw(st.floats(0.01, 0.999, exclude_min=True, exclude_max=True, allow_subnormal=False))
        return dist_fn(p)
    elif dist_fn == sq.discrete:
        items = draw(
            st.dictionaries(
                st.floats(allow_infinity=False, allow_nan=False),
                st.floats(0, 1, exclude_min=True),
                min_size=1,
            )
        )
        # Normalize the probabilities
        normalized_items = dict()
        value_sum = sum(items.values())
        for k, v in items.items():
            normalized_items[k] = v / value_sum

        return dist_fn(normalized_items)
    elif dist_fn == sq.exponential:
        a = draw(
            st.floats(
                min_value=0,
                max_value=1e20,  # Prevents overflow
                exclude_min=True,
                exclude_max=True,
                allow_infinity=False,
                allow_nan=False,
                allow_subnormal=False,  # Prevents overflow (again)
            )
        )
        return dist_fn(a)
    elif dist_fn == sq.beta:
        a = draw(
            st.floats(
                min_value=0.01,
                max_value=100,
                exclude_min=True,
                allow_nan=False,
                allow_infinity=False,
                allow_subnormal=False,
            )
        )
        b = draw(
            st.floats(
                min_value=0.01,
                max_value=100,
                exclude_min=True,
                allow_nan=False,
                allow_infinity=False,
                allow_subnormal=False,
            )
        )
        return dist_fn(a, b)

    elif dist_fn == sq.triangular:
        a = draw(
            st.floats(-1e30, 1e30, allow_infinity=False, allow_nan=False, allow_subnormal=False)
        )
        b = draw(
            st.floats(
                min_value=a + 0.05,
                max_value=a + 1e30,
                allow_infinity=False,
                allow_nan=False,
                allow_subnormal=False,
                exclude_min=True,
            )
        )
        c = draw(
            st.floats(
                min_value=a,
                max_value=b,
                allow_infinity=False,
                allow_nan=False,
                allow_subnormal=False,
            )
        )
        return dist_fn(a, c, b)

    elif dist_fn == sq.pert:
        low = draw(
            st.floats(-1e10, 1e10, allow_infinity=False, allow_nan=False, allow_subnormal=False)
        )
        mode_offset = draw(
            st.floats(
                min_value=0.01,
                max_value=1e10,
                allow_infinity=False,
                allow_nan=False,
            )
        )
        high_offset = draw(
            st.floats(
                min_value=0.01,
                max_value=1e10,
                allow_infinity=False,
                allow_nan=False,
            )
        )
        shape = draw(
            st.floats(
                min_value=0.01,
                max_value=1e30,
                allow_infinity=False,
                allow_nan=False,
            )
        )
        return dist_fn(low, low + mode_offset, low + mode_offset + high_offset, shape)

    elif dist_fn == sq.poisson:
        lambda_ = draw(st.integers(1, 1000))
        return dist_fn(lambda_)

    elif dist_fn == sq.chisquare:
        df = draw(st.integers(1, 1000))
        return dist_fn(df)

    elif dist_fn == sq.gamma:
        shape = draw(
            st.floats(
                min_value=0.01,
                max_value=100,
                exclude_min=True,
                allow_nan=False,
                allow_infinity=False,
                allow_subnormal=False,
            )
        )
        scale = draw(
            st.floats(
                min_value=0.01,
                max_value=100,
                exclude_min=True,
                allow_nan=False,
                allow_infinity=False,
                allow_subnormal=False,
            )
        )
        return dist_fn(shape, scale)

    elif dist_fn == sq.pareto:
        b = draw(
            st.floats(
                min_value=1.01,
                max_value=10.0,
                exclude_min=True,
                allow_nan=False,
                allow_infinity=False,
                allow_subnormal=False,
            )
        )
        return dist_fn(b)
    else:
        raise NotImplementedError(f"Unknown distribution {dist_fn}")


import asyncio.subprocess
import copy
import typing

from containers import Container
from containers import Mount
from containers import WindowsContainersService
from containers.services.windows import to_wsl_path

from .data_types import ImageMountWithArchive

class WindowsContainersServiceWithArchive(WindowsContainersService):
    def _filter_mount(self, mount: Mount) -> Mount:
        if not isinstance(mount, ImageMountWithArchive):
            return mount
        if mount.archive_to is None:
            return mount
        mount = copy.deepcopy(mount)
        mount.archive_to = to_wsl_path(mount.archive_to)
        if mount.archive_success is not None:
            mount.archive_success = to_wsl_path(mount.archive_success)
        return mount

    def run(
        self,
        container: Container,
        stdin: typing.Optional[int] = None,
        stdout: typing.Optional[int] = None,
        stderr: typing.Optional[int] = None,
        log_level: typing.Optional[str] = None,
    ) -> typing.AsyncContextManager[asyncio.subprocess.Process]:
        container = copy.deepcopy(container)
        container.mounts = list(map(self._filter_mount, container.mounts))
        return super().run(container, stdin, stdout, stderr, log_level)


'''Ori Weis'''
#29.4.21
from numpy import *
import math
def ret_sum(A):
    sum=0
    for i in A:
        sum+=i
    return sum

'''Targil 1'''
def input_from_user():
    list1=[]
    print("enter numbers for addition enter stop to stop\n")
    inpstr=input("")
    while(inpstr!="stop"):
        list1.append(int(inpstr))
        inpstr = input("")
    sum=sum_of_array(list1)
    return sum

def sum_of_array(list1):
    sum=0
    for i in list1:
        sum+=i
    print("sum is : "+str(sum))
    return sum

'''Targil 2'''
def tictactoeSolver(mat):
    if (mat[0][0]== mat[0][1]== mat[0][1]==1) or (mat[1][0]== mat[1][1]== mat[2][1]==1) or \
            (mat[2][0]== mat[2][1]== mat[2][2]==1):
        print("Player 1 won the game")
    elif(mat[0][0]== mat[1][0]== mat[2][0]==1) or (mat[1][0]== mat[1][1]== mat[2][1]==1) or \
            (mat[0][2]== mat[1][2]== mat[2][2]==1):
        print("Player 1 won the game")
    elif (mat[0][0] == mat[1][1] == mat[2][2] == 1) or (mat[2][0] == mat[1][1] == mat[2][0] == 1) :
        print("Player 1 won the game")

    elif (mat[0][0]== mat[0][1]== mat[0][1]==2) or (mat[1][0]== mat[1][1]== mat[2][1]==2) or \
            (mat[2][0]== mat[2][1]== mat[2][2]==2):
        print("Player 2 won the game")
    elif(mat[0][0]== mat[1][0]== mat[2][0]==2) or (mat[1][0]== mat[1][1]== mat[2][1]==2) or \
            (mat[0][2]== mat[1][2]== mat[2][2]==2):
        print("Player 2 won the game")
    elif (mat[0][0] == mat[1][1] == mat[2][2] == 2) or (mat[2][0] == mat[1][1] == mat[2][0] == 2) :
        print("Player 2 won the game")

    else:
        print("tie")

'''Targil 3'''
def compress_string():
    str1=input("Enter String To compress ")
    new_str=""
    count=1
    for i in range(len(str1)-1):
        if(str1[i]==str1[i+1]):
            count+=1
        else:
            new_str+=str1[i]
            new_str+=str(count)
            count=0
    new_str += str1[i]
    new_str += str(count)
    print(new_str)
    return new_str

'''Targil 4'''
def roundup(x):
    return int(math.ceil(x / 10.0)) * 10
def is_valid_ID(id):
    dup=1
    sum=0
    if(len(id)!=9 or id==""):
        return False
    for i in range(len(id)-1):
        addi=dup*int(id[i])#addi is the potential addition
        if(addi>9):
            addi=addi%10+int(addi/10)
        sum+=addi
        if(dup==1):
            dup=2
        else:
            dup=1
    sifra=roundup(sum)-sum
    if(sifra==int(id[len(id)-1])):
        return True
    else:
        return False

'''Targil 5'''
def function1(num):
    return num*num
def map_Func(list1, function1):
    return [function1(item) for item in list1]

'''main Function To Run'''
def main():
    print("Ori Weis's Project\n")

    '''Targil 1'''
    input_from_user()

    '''Targil 2'''
    game = [[1, 2, 0],
            [2, 1, 0],
            [2, 1, 1]]
    print(game)
    tictactoeSolver(game)

    '''Targil 3'''
    compress_string()

    '''Targil 4'''
    id = input("Enter ID For ID Checking ")
    print(is_valid_ID(id))

    '''Targil 5'''

    my_list=[1,2,3,4,5,6,7,8,9,10]
    my_list=map_Func(my_list,function1)
    print(my_list)

#calling For main Function
main()


# 1. Find a file on your computer and print its contents using python

import os
mypath = os.path.join('/','Library','Documentation')
with open(mypath+'/Warranty.txt','r') as f:
    print(f.read())

# 2. Write a program that asks a user a question, and saves their answer to a file.

with open('user_answer.txt','w') as f:
    a = input('What''s your favorite day of the year?')
    f.write(a)

with open('user_answer.txt','r') as f:
    print(f.read())

# 3. Take the items in this list of lists:
import csv

lol = [['Top Gun','Risky Business','Minority Report'] \
,['Titanic','The Revenant','Inception'] \
 ,['Training Day','Man on Fire','Flight']]

with open('movie.csv','w',newline = '') as f:
    w = csv.writer(f,delimiter = ',')
    for i in lol:
        w.writerow(i)

with open('movie.csv','r') as f:
    r = csv.reader(f, delimiter = ',')
    for row in r:
        print(','.join(row))


import time
from functools import wraps

def retry(max_tries=3, delay_seconds=1):
    def decorator_retry(func):
        @wraps(func)
        def wrapper_retry(*args, **kwargs):
            tries = 0
            while tries < max_tries:
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    tries += 1
                    if tries == max_tries:
                        raise e
                    time.sleep(delay_seconds)
        return wrapper_retry
    return decorator_retry

def memoize(func):
    cache = {}
    def wrapper(*args):
        if args in cache:
            return cache[args]
        else:
            result = func(*args)
            cache[args] = result
            return result
    return wrapper

def timing_decorator(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"Function {func.__name__} took {end_time - start_time} seconds to run.")
        return result
    return wrapper


class Solution(object):
    def matrixReshape(self, nums, r, c):
        """
        :type nums: List[List[int]]
        :type r: int
        :type c: int
        :rtype: List[List[int]]
        """
        table = list()
        for row in nums:
            table += row
        if len(table) != r * c:
            return nums
        res = list()
        idx = 0
        for row in range(r):
            tmp_row = list()
            for col in range(c):
                tmp_row.append(table[idx])
                idx += 1
            res.append(tmp_row)
        return res


import csv

with open('salary.csv', encoding="utf8") as csvfile:
    reader = csv.DictReader(csvfile, delimiter=';')
    name = input()
    years = input().split()
    objects = [['Субъект', years[0], years[1]]]
    for i in reader:
        if i['Федеральный округ'] == name:
            if int(i[years[0]]) + int(i[years[0]]) / 25 > int(i[years[1]]):
                objects.append([i['Субъект'], i[years[0]], i[years[1]]])

with open('out_file.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=';')
    if len(objects) > 1:
        writer.writerows(objects)


import sys
import bisect
from collections import deque
import itertools
import math

# sys.setrecursionlimit(10**4)
from sys import stdin
# readline = stdin.readline
sr=lambda: input()
ir=lambda: int(sr())
lr=lambda: list(map(int, sr().split()))

inf=10**18
# mod=10**9+7
mod = 998244353

if __name__=='__main__':
    n,m = lr()
    e = []
    w = []
    ans=0
    dist=[[inf for j in range(n)] for i in range(n)]
    for i in range(n):
        dist[i][i]=0

    for i in range(m):
        a, b, c=lr()
        a-=1
        b-=1
        dist[a][b]=c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j])
        for i in range(n):
            for j in range(n):
                if dist[i][j] != inf:
                    ans+=dist[i][j]
    print(ans)


import json

#  результат матча
file_name1 = "processed_data.json"
#  исходники для матчера
file_name2 = "input_data.json"
with open(file_name1, 'r+', encoding='utf-8') as file1:
    with open(file_name2, 'r+', encoding='utf-8') as file2:
        file1 = json.load(file1)
        file2 = json.load(file2)
        for i in ["mismatched_pairs"]:

            for mismatch_pair in file1[i]:
                # print(i["overall_similarity"])
                if int(mismatch_pair["overall_similarity"]) > 90:
                    event_id1 = mismatch_pair["event1"]["event_id"]
                    event_id2 = mismatch_pair["event2"]["event_id"]
                    sport = mismatch_pair["sport"]
                    date_event = mismatch_pair["date"]
                    for key_file2, date_events in enumerate(file2):
                        # if flag_event1 and flag_event2:
                        #     break
                        if date_event != date_events["date"]:
                            continue
                        data_array = date_events["data"]
                        for key, event_serch in enumerate(data_array):
                            if event_id2 == event_serch["event2"]["event_id"]:
                                print(event_id2)
                                del file2[key_file2]["data"][key]
                                mismatch_pair["event2"]["true_pair"] = event_serch["event1"]

        with open('new_result.json', 'w') as fp:
            json.dump(file2, fp)
# unmatched_results, mismatched_pairs


import sys

def greeting(x):
    del x[0]

    if len(x) > 0:
        if x[0] == "-it":
            print("-it flag initiated")
        else:
            print("-it flag required")

        if len(x) > 1:
            if x[1] == "--rm":
                print("--rm flag initiated")

greeting(sys.argv)


def loading_bar(number):
    if number == 100:
        result = f"100% Complete!\n[{'%' * (number // 10)}]"
    else:
        result = f"{number}% [{('%' * (number // 10))}{('.' * (10 - (number // 10)))}]\nStill loading..."

    return result

input_number = int(input())

print(loading_bar(input_number))


INF = 1 << 60

N = int(input())

F = []
for i in range(N):
    f = list(map(int, input().split()))
    F.append(f)

P = []
for i in range(N):
    p = list(map(int, input().split()))
    P.append(p)

ans = -INF
for bit in range(1 << 10):
    profit = 0
    c = [0] * N
    ok = False
    for i in range(10):
        if not bit & (1 << i): continue
        ok = True
        for n in range(N):
            c[n] += F[n][i]
    # joisino お姉ちゃんが 1 つも店を開けていない場合はスキップ
    if not ok: continue
    t = 0
    for n in range(N):
        t += P[n][c[n]]
    ans = max(ans ,t)

print(ans)


"""
Возьмите любые 1-3 задачи из прошлых домашних заданий.
Добавьте к ним логирование ошибок и полезной
информации. Также реализуйте возможность запуска из
командной строки с передачей параметров.
"""
import logging
import argparse

logging.basicConfig(filename='hw_task_1.log', encoding='utf-8', level=logging.INFO)
logger = logging.getLogger(__name__)

def parse():
    parser = argparse.ArgumentParser(prog='check_date',
                                    description='Проверка даты на корректный ввод',
                                    epilog='Пример запуска: python3.10 hw_task_1_check_date.py -d "30.01.2010"')
    parser.add_argument('-d', '--date', help='Дата в формате dd.mm.yyyy')
    args = parser.parse_args()
    return check_date(args.date)

def if_leap(year: int) -> bool:
    return not(year % 4 != 0 or year % 100 == 0 and year % 400 != 0)

def check_date(str_date: str) -> bool:
    day, month, year = map(int, str_date.split('.'))
    if not( 1 <= day <= 31 and 1 <= month <= 12 and 1 <= year <= 9999 ):
        logger.error(f'{str_date} - некорректная дата')
        return False
    if month in (4,6,9,11) and day > 30:
        logger.error(f'{str_date} - неверное количество дней {day} в месяце {month}')
        return False
    if month == 2 and day > 29:
        logger.error(f'{str_date} - неверное количество дней {day} в феврале')
        return False
    if month == 2 and not if_leap(year) and day > 28:
        logger.error(f'{str_date} - год {year} не високосный, в нём не может быть {day} дней')
        return False

    logger.info(f'{str_date} - корректная дата')
    return True

if __name__ == '__main__':
    parse()


from random import randint
from PIL import Image
im = Image.new('RGB', (1080, 1080))
for x in range(1080):
    for y in range(1080):
        c = randint(128, 255)
        r = 0
        g = c
        b = c
        im.putpixel((x, y), (r, g, b))
im.show()


a = [1,2,4,5,6,7,8,9,10]

# Find missing number from the sequence using binary search algorithm

def binary_search(a):
    print(a)
    mid = len(a) // 2
    if mid == 0:
        #print(a[0] + 1)
        return (a[0] + 1)
    if a[mid] ==  mid + a[0]:
        print("entered here")
        if len(a) == 2:
            return "No missing elements"
        return (binary_search(a[mid:]))
    else:
        print("I am in else")
        if len(a) == 2:
            if a[1] == a[0] + 1:
                return "No Missing elemetns in else"
            return (a[0] + 1)
        return (binary_search(a[:mid+1]))

print(binary_search(a))


#Written by Professor F.Geuning (ECAM)for Image treatment class

import cv2, numpy as np
rangelower = np.array([0, 100, 40], dtype=np.uint8)
rangeupper = np.array([255, 255, 255], dtype=np.uint8)
hue = 128
wid = 256

filename = 'eurobot2018_playingarea.jpg'
frame = cv2.imread(filename)
while(True):
	rangelower[0] = np.uint8(np.max(np.array([0, hue-wid/2])))
	rangeupper[0] = np.uint8(np.min(np.array([255, hue+wid/2])))
	key = 0xFF
	cv2.imshow(filename,frame)
	# Canny
	edges = cv2.Canny(frame,100,200)
	# Color detection, ref https://henrydangprg.com/2016/06/26/color-detection-in-python-with-opencv/
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
	mask = cv2.inRange(hsv, rangelower, rangeupper)
	framemasked = cv2.bitwise_and(frame,frame,mask = mask)
	cv2.imshow('framemasked',framemasked)
	cv2.imshow('Canny',edges)
	zoom = 0.5
	cv2.imshow(filename + ' resized', cv2.resize(frame,None,fx=zoom, fy=zoom, interpolation = cv2.INTER_CUBIC))
	cv2.imshow('Canny resized', cv2.resize(edges,None,fx=zoom, fy=zoom, interpolation = cv2.INTER_CUBIC))
	key_ = cv2.waitKey(100) & 0xFF # waitKey necessary to display image
	print(key_)
	if key_ != 0xFF: # if key pressed
		key = key_
		if key == ord('q'): # press 'q' to quit
			break
		else :
			if key == ord('h'): # hue  -1 'h'
				hue -= 1
			if key == ord('H'): # hue  +1 'H'
				hue += 1
			if key == ord('j'): # hue  -5 'j'
				hue -= 5
			if key == ord('J'): # hue  +5 'J'
				hue += 5
			if key == ord('w'): # wid  -2 'w'
				wid -= 2
			if key == ord('W'): # wid  +2 'W'
				wid += 2
			print('hue: %d   wid: %d' % (hue,  wid))

cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('zone_blog', '0006_auto_20150529_1930'),
    ]

    operations = [
        migrations.AddField(
            model_name='project',
            name='code_url',
            field=models.URLField(null=True, verbose_name='Code url', blank=True),
        ),
    ]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#列表生成式
i = [x * x for x in range(1,100)]
j = [x * x for x in range(1,100) if x % 2 == 0]
k = [x + y for x in 'xyz' for y in 'abc']       #各语句之间是嵌套关系，左边第二个语句是最外层，依次往右进一层，左边第一条语句是最后一层。
#用代码表示一下:[x*y for x in range[1,5] if x > 2 for y in range[1,4] if y < 3]
for x in range(1,5):
    if x > 2:
        for y in range(1,4):
            if y < 3:
                x*y

import os
l = [d for d in os.listdir('.')]        # os.listdir可以列出文件和目录

d = {'x': 'A', 'y': 'B', 'z': 'C'}
jk = [k + '=' + v for k, v in d.items()]

l1 = ['Hello', 'World', 'IBM', 'Apple']
l2 = [s.lower() for s in l1]            #lower把大写变小写,参考字符串处理方法

#生成器
g = (x * x for x in range(10))    #创建一个生成器有很多种方法。第一种方法很简单，只要把一个列表生成式的[]改成()
#生成器保存的是算法 每次调用next(g)就计算出g的下一个元素的值,算出最后一个元素后再用next()抛出StopIteration的错误
#不断调用next(g)实在是太变态了，正确的方法是使用for循环，因为generator也是可迭代对象

#第二种创建方法，写函数,如果一个定义中包含yield关键字那么这个函数就是个生成器
def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
    return 'done'

#最难理解的就是生成器和函数的执行流程不一样。函数是顺序执行，遇到return语句或者最后一行函数语句就返回。
#而变成生成器的函数，在每次调用next()的时候执行，遇到yield语句返回，再次执行时从上次返回的yield语句处继续执行。
#举个简单的例子，定义一个生成器，依次返回数字1，3，5
def odd():
    print('step 1')
    yield 1
    print('step 2')
    yield(3)
    print('step 3')
    yield(5)

#用for循环调用生成器时，发现拿不到生成器的return语句的返回值。如果想要拿到返回值，
#必须捕获StopIteration错误，返回值包含在StopIteration的value中
while True:
    try:
        x = next(g)
        print('g:', x)
    except StopIteration as e:
        print('Generator return value:', e.value)
        break

列表生成式,生成器.py


class Solution:
    def wiggleMaxLength(self, nums: List[int]) -> int:
        if len(nums) <= 1:
            return len(nums)

        up = dn = 1

        for i in range(len(nums)-1):
            if nums[i] < nums[i+1]:
                up = dn + 1
            elif nums[i] > nums[i+1]:
                dn = up + 1
        return max(up, dn)


import Function
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import math
import cv2
import random

def ReadData(Path,File):
    GtList = []
    ImageList = []
    f = open(Path + File)
    line = f.readline()
    Step = 0
    while line:
        stringList = line.split()
        FilePath = Path + stringList[0]
        BoxX = int(stringList[1])
        BoxW = int(stringList[2]) - BoxX
        BoxY = int(stringList[3])
        BoxH = int(stringList[4]) - BoxY
        PointList = [float(stringList[5]),float(stringList[6]),float(stringList[7]),float(stringList[8]),float(stringList[9]),float(stringList[10]),float(stringList[11]),float(stringList[12]),float(stringList[13]),float(stringList[14])]

        SrcImage = cv2.imread(FilePath)
        TestImage = SrcImage[int(BoxY):int(BoxY + BoxH),int(BoxX):int(BoxX + BoxW)]
        TestImage = cv2.cvtColor(TestImage,cv2.COLOR_BGR2GRAY)
        TestImage = cv2.resize(TestImage,(39,39))

        for I in range(5):
            PointList[2 * I] = (PointList[2 * I] - BoxX) / BoxW
            PointList[2 * I + 1] = (PointList[2 * I + 1] - BoxY) / BoxH

        GtList.append(PointList)
        ImageList.append(TestImage)
        line = f.readline()
    print("Read End!",len(GtList))
    return GtList,ImageList

x = tf.placeholder(tf.float32, shape=[None, 39 , 39], name='x')
y = tf.placeholder(tf.float32, shape=[None, 10],name='y')

x_image = tf.reshape(x, [-1, 39, 39, 1])
ConvLayer_1,ConvWeights_1 = Function.new_conv_layer(x_image,1,4,20)
print(ConvLayer_1)

ConvLayer_2,ConvWeights_2 = Function.new_conv_layer(ConvLayer_1,20,3,40)
print(ConvLayer_2)

ConvLayer_3,ConvWeights_3 = Function.new_conv_layer(ConvLayer_2,40 ,3,60)
print(ConvLayer_3)

ConvLayer_4,ConvWeights_4 = Function.new_conv_layer(ConvLayer_3,60 ,2,80,use_pooling=False)
print(ConvLayer_4)

FlatLayer,FeaturesNum = Function.flatten_layer(ConvLayer_4)
print(FlatLayer)

FcLayer1 = Function.new_fc_layer(FlatLayer,FeaturesNum,120)
print(FcLayer1)

FcLayer2 = Function.new_fc_layer(FcLayer1,120,10)
print(FcLayer2)

Cost = tf.reduce_mean(tf.pow(y * 39 - FcLayer2 * 39,2))
print(Cost)

Optimizer = tf.train.AdamOptimizer(1e-4).minimize(Cost)

GtList,ImageList = ReadData("C:\\Users\\ZhaoHang\\Documents\\Visual Studio 2017\\Projects\\PythonApplication1\\PythonApplication1\\FaceData\\","trainImageList.txt")
TestGtList,TestImageList = ReadData("C:\\Users\\ZhaoHang\\Documents\\Visual Studio 2017\\Projects\\PythonApplication1\\PythonApplication1\\FaceData\\","testImageList.txt")

session = tf.Session()
session.run(tf.global_variables_initializer())

def plot_conv_weights(weights, input_channel=0):
    # weights_conv1 or weights_conv2.

    # 运行weights以获得权重
    w = session.run(weights)

    # 获取权重最小值最大值，这将用户纠正整个图像的颜色密集度，来进行对比
    w_min = np.min(w)
    w_max = np.max(w)

    # 卷积核树木
    num_filters = w.shape[3]

    # 需要输出的卷积核
    num_grids = math.ceil(math.sqrt(num_filters))

    fig, axes = plt.subplots(num_grids, num_grids)
    for i, ax in enumerate(axes.flat):
        # 只输出有用的子图.
        if i < num_filters:
            # 获得第i个卷积核在特定输入通道上的权重
            img = w[:, :, input_channel, i]

            ax.imshow(img, vmin=w_min, vmax=w_max,
                      interpolation='nearest', cmap='seismic')

        # 移除坐标.
        ax.set_xticks([])
        ax.set_yticks([])
    plt.show()

def plot_conv_layer(layer, image):
    # layer_conv1 or layer_conv2.

    # feed_dict只需要x，标签信息在此不需要.
    feed_dict = {x: [image]}

    # 获取该层的输出结果
    values = session.run(layer, feed_dict=feed_dict)

    # 卷积核树木
    num_filters = values.shape[3]

    # 每行需要输出的卷积核网格数
    num_grids = math.ceil(math.sqrt(num_filters))

    fig, axes = plt.subplots(num_grids, num_grids)
    for i, ax in enumerate(axes.flat):
        # 只输出有用的子图.
        if i < num_filters:
            # 获取第i个卷积核的输出
            img = values[0, :, :, i]

            ax.imshow(img, interpolation='nearest', cmap='binary')

        # 移除坐标.
        ax.set_xticks([])
        ax.set_yticks([])
    plt.show()

def plot_image(image):
    plt.imshow(image.reshape(img_shape),
              interpolation='nearest',
              cmap='binary')

    plt.show()

saver = tf.train.Saver()

#saver.restore(session,"ModelSave/Model.ckpt")
#Predict = session.run(FcLayer2,feed_dict={x:ImageList})
#print(Predict)
#for i in range(10000):
#    for p in range(5):
#        cv2.circle(ImageList[i],(int(39 * GtList[i][p * 2]),int(39 *
#        GtList[i][p * 2 + 1])),1,(255,255,255))
#        cv2.circle(ImageList[i],(int(39 * Predict[i][p * 2]),int(39
#        *Predict[i][p * 2 + 1])),1,(255,255,255))
#    cv2.imshow("test",ImageList[i])
#    cv2.waitKey(1000)
CountEq = 0
while True:
    session.run(Optimizer,feed_dict = {x:ImageList,y:GtList})
    ErrTrainSet = session.run(Cost,feed_dict={x:ImageList,y:GtList})
    ErrTestSet = session.run(Cost,feed_dict={x:TestImageList,y:TestGtList})
    print("TrainSetErr : ",ErrTrainSet," TestSetErr : ",ErrTestSet)
    CountEq += 1

    if(CountEq == 100):
        saver.save(session,str(ErrTestSet) + "/Model.ckpt")
        CountEq = 0

plot_conv_weights(weights = ConvWeights_1)
plot_conv_layer(layer=ConvLayer_1,image=ImageList[0])
plot_conv_layer(layer=ConvLayer_1,image=ImageList[5])

plot_conv_layer(layer=ConvLayer_2,image=ImageList[0])
plot_conv_layer(layer=ConvLayer_2,image=ImageList[5])

plot_conv_layer(layer=ConvLayer_3,image=ImageList[0])
plot_conv_layer(layer=ConvLayer_3,image=ImageList[5])

plot_conv_layer(layer=ConvLayer_4,image=ImageList[0])
plot_conv_layer(layer=ConvLayer_4,image=ImageList[5])


import numpy as np
from env import Env
from models import PolicyNet, Critic
from utils import one_hot
import torch
from torch.optim import Adam
import time
import os
from datetime import datetime
import math

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

#------------------------SET PARAMETERS----------------------------

SEED             = 17
BATCH_SIZE       = 128
N_NODES          = 11
N_DEPOT          = 1
NUM_LAYERS       = 1
CAPACITY         = [20,15,10]
MAX_DEMAND       = 10
N_VEHICLES       = len(CAPACITY)
DIM_STATIC        = 2
DIM_DYNAMIC       = 1 + N_VEHICLES
DIM_LOAD          = N_VEHICLES
DIM_EMBED         = 128
MAX_EP_lEN        = 16
GAMMA             = 0.99
ENTROPY_REG       = 0.01
MAX_GRAD_NORM     = 2
DROPOUT           = 0.1
EMBED_TYPE        = 'conv1d'
LOG_INTERVAL      = 200

#----------------INITIALIZE ENVIROMENT AND POLICIES----------------

env_test = Env(seed = SEED, batch_size = BATCH_SIZE, capacity = CAPACITY,
        n_nodes = N_NODES, n_depot = N_DEPOT, max_demand = MAX_DEMAND, n_agents = N_VEHICLES)

policy = [PolicyNet(batch_size = BATCH_SIZE, n_nodes = N_NODES, n_agents=N_VEHICLES, num_layers = NUM_LAYERS,
                        dim_s = DIM_STATIC, dim_d = DIM_DYNAMIC,
                        dim_embed = DIM_EMBED, n_glimpses = 0,  embeding_type=EMBED_TYPE,
                        dropout = DROPOUT).to(device) for i in range(N_VEHICLES)]

#------------------LOAD TRAINDEL MODEL---------------------------
model_dir = 'weights/model_exp_1.pt'
policy_name = "policy_agent_X"

if os.path.isfile(model_dir):
    checkpoint     = torch.load(model_dir,map_location=device)
else:
    raise ValueError('No model file!')

for agent_id in range(N_VEHICLES):
    p_name = policy_name.replace("X",str(agent_id))
    policy[agent_id].load_state_dict(checkpoint[p_name])

#-----------------RUN TRAINED POLICY----------------
num_epochs = math.ceil(1000/BATCH_SIZE)

total_tests = []
total_times = []
for i in range(num_epochs):
    start = time.time()
    o_t, d_t, r_t = env_test.reset(), False, 0

    actions_ep   = []
    log_probs_ep = []
    rewards_ep   = []
    values_ep    = []
    last_hh_t         = [None]*N_VEHICLES
    for  t in range(int(MAX_EP_lEN) ):
        actions         = []
        actions_one_hot = []
        log_probs       = []
        values          = []
        for agent_id in range(N_VEHICLES) :
            model = policy[agent_id].eval()
            logits, prob , log_p, last_hh_t[agent_id] = model(o_t, last_hh_t[agent_id], agent_id)

            #--------- GREEDY POLICY ------------
            act = torch.argmax(prob, dim =1)  # [ batch size ]
            actions.append(act.detach())

            ot_2, d_t, r_t = env_test.step(act.detach().unsqueeze(1), agent_id)
            o_t = ot_2
            values.append( r_t )

        r_step = torch.stack(values, dim = 1)      #[batch_size, n_agents]
        a      = torch.stack(actions,   dim = 1)   #[batch_size, n_agents]
        actions_ep.append(a)
        rewards_ep.append(r_step)
    end = time.time()
    rewards  = torch.stack(rewards_ep,  dim = 2 ).sum(dim=2).sum(dim=1)  #[batch_size, n_agents, ep_len]
    total_tests.append(rewards)
    total_times.append((end-start)/BATCH_SIZE)

#------------------- SAVE RESULTS -----------------------
rewards_total = torch.stack(total_tests, dim=1).reshape(-1,)
np_results    = rewards_total.numpy()
np.save('vrp_results_RL',np_results)
np_runtimes   = np.array(total_times).reshape(-1,)
np.save('vrp_runtimes_RL',np_runtimes)


from tempest.lib import exceptions as lib_exc

def get_tenant_by_name(client, tenant_name):
    tenants = client.list_tenants()['tenants']
    for tenant in tenants:
        if tenant['name'] == tenant_name:
            return tenant
    raise lib_exc.NotFound('No such tenant(%s) in %s' % (tenant_name, tenants))

def get_user_by_username(client, tenant_id, username):
    users = client.list_tenant_users(tenant_id)['users']
    for user in users:
        if user['name'] == username:
            return user
    raise lib_exc.NotFound('No such user(%s) in %s' % (username, users))


from unittest import TestCase

from maximum_width_of_binary_tree import Solution
from tree_node import TreeNode

class TestMaximumWidthOfBinaryTree(TestCase):
    def test_empty_tree(self):
        self.assertEqual(0, Solution().widthOfBinaryTree(None))

    def test_single_root_tree(self):
        root = TreeNode(1)
        self.assertEqual(1, Solution().widthOfBinaryTree(root))

    def test_single_root_tree_with_left_child(self):
        root = TreeNode(1)
        root.left = TreeNode(2)
        self.assertEqual(1, Solution().widthOfBinaryTree(root))

    def test_single_root_tree_with_right_child(self):
        root = TreeNode(1)
        root.right = TreeNode(2)
        self.assertEqual(1, Solution().widthOfBinaryTree(root))

    def test_single_root_tree_with_left_and_right_children(self):
        root = TreeNode(1)
        root.left = TreeNode(2)
        root.right = TreeNode(3)
        self.assertEqual(2, Solution().widthOfBinaryTree(root))

    def test_second_level_with_gap(self):
        root = TreeNode(1)
        root.left = TreeNode(2)
        root.right = TreeNode(3)
        root.left.left = TreeNode(4)
        root.left.right = TreeNode(5)
        root.right.right = TreeNode(6)
        self.assertEqual(4, Solution().widthOfBinaryTree(root))

    def test_multiple_levels(self):
        root = TreeNode(1)
        root.left = TreeNode(2)
        root.right = TreeNode(3)
        root.left.left = TreeNode(4)
        self.assertEqual(2, Solution().widthOfBinaryTree(root))


import numpy as np
import ctypes

# Import my C++ function
_libutils = ctypes.cdll.LoadLibrary("../c++/utils.so")
_libwrapper = ctypes.cdll.LoadLibrary("../c++/wrapper.so")

def c_dummy_sum(a, b):
    func = _libutils.dummy_sum
    func.restype = ctypes.c_int
    return func(ctypes.c_int(a), ctypes.c_int(b))

def c_array_sum(arr, dim):
    c_arr = (ctypes.c_float * len(arr))(*arr)
    c_dim = (ctypes.c_int * len(dim))(*dim)
    c_size = ctypes.c_int(len(dim))
    func = _libutils.array_sum
    func.restype = ctypes.c_float
    return func(c_arr, c_dim, c_size)

def c_array_sum_p(arr, dim):
    c_arr = arr.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
    c_dim = dim.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
    c_size = ctypes.c_int(len(dim))

    func = _libutils.array_sum_p
    func.restype = ctypes.c_float

    return func(c_arr, c_dim, c_size)

def c_add_vectors(a, b):
    func = _libutils.sum_vector_bw

    # [ARGUMENTS] Option 1:
    # p_a = a.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
    # p_b = b.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
    # c_size = ctypes.c_int(size)

    # [ARGUMENTS] Option 2:
    func.argtypes = [np.ctypeslib.ndpointer(dtype=a.dtype, ndim=a.ndim, flags='C_CONTIGUOUS'),
                     np.ctypeslib.ndpointer(dtype=b.dtype, ndim=b.ndim, flags='C_CONTIGUOUS'),
                     ctypes.c_int
                     ]

    # [OUTPUT] Option 1: (and then cast array -np.fromiter-)
    func.restype = ctypes.POINTER(ctypes.c_float)
    c = func(a, b, len(a))
    cc = np.array(np.fromiter(c, dtype=np.float32, count=len(a)))

    # [OUTPUT] Option 2: (cast directly into Numpy) -not working-
    # func.restype = np.ctypeslib.ndpointer(dtype=ctypes.POINTER(ctypes.c_float), ndim=a.ndim, flags='C_CONTIGUOUS')
    # cc = func(a, b, len(a))  # returns a pointer

    return cc

# Item - init
_libwrapper.Item_init.restype = ctypes.c_void_p
_libwrapper.Item_init.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int]

# Item - getName
_libwrapper.Item_getName.restype = ctypes.c_char_p
_libwrapper.Item_getName.argtypes = [ctypes.c_void_p]

# Container - init
_libwrapper.Container_init.restype = ctypes.c_void_p
_libwrapper.Container_init.argtypes = ()

# Container - addItem
_libwrapper.Container_addItem.restype = ctypes.c_int
_libwrapper.Container_addItem.argtypes = [ctypes.c_void_p, ctypes.c_void_p]

# Container - getItem
_libwrapper.Container_getItem.restype = ctypes.c_void_p
_libwrapper.Container_getItem.argtypes = [ctypes.c_void_p, ctypes.c_int]

class Item:

    def __init__(self, id, name, obj=None):
        if obj:
            self.obj = obj
        else:
            c_id = ctypes.c_int(id)
            c_name = ctypes.c_char_p(bytes(name.encode('ascii')))
            c_length = ctypes.c_int(len(name))

            self.obj = _libwrapper.Item_init(c_id, c_name, c_length)

    @classmethod
    def from_pointer(cls, obj):
        return cls(None, None, obj)

    def get_name(self):
        res = _libwrapper.Item_getName(self.obj).decode('ascii')
        return res

class Container:
    def __init__(self):
        self.obj = _libwrapper.Container_init()

    def addItem(self, item):
        return _libwrapper.Container_addItem(self.obj, item.obj)

    def getItem(self, id):
        c_id = ctypes.c_int(id)
        _item = _libwrapper.Container_getItem(self.obj, c_id)
        return Item.from_pointer(_item)

def main():
    # print("Variables: *******************************")
    # # Declare (test) variables
    # a, b = 1, 3
    # arr_m = np.array(
    #     [[1.0, 2.0],
    #      [3.0, 4.0],
    #      [5.0, 6.0]],
    #     dtype=np.float32
    # )
    # arr_v = np.array([1, 2, 3], dtype=np.float32)
    # arr_w = np.array([4, 5, 6], dtype=np.float32)
    # print("a = {}".format(a))
    # print("b = {}".format(b))
    # print("Array v: {}".format(arr_v))
    # print("Array w: {}".format(arr_w))
    # print("Array M:")
    # print(arr_m)
    # print("")
    #
    # print("Tests: *******************************")
    #
    # # Test 1: Sum two integers *******************************************************************
    # res = c_dummy_sum(a, b)
    # print("Dummy sum (C++): a+b={}".format(res))
    #
    # # Test 2: Sum array (from Python List)  *****************************************************
    # arr1d = list(arr_m.flatten(order='C'))  # Row major
    # arr_dim = list(arr_m.shape)
    # res_c = c_array_sum(arr1d, arr_dim)
    # print("Array M sum (from Python list; C++): {}".format(res_c))
    #
    # # Test 3: Sum array (from Numpy array)  *****************************************************
    # arr_dim = np.array(arr_m.shape, dtype=np.int32)
    # res_c_p = c_array_sum_p(arr_m, arr_dim)
    # print("Array M sum (from Numpy array; C++): {}".format(res_c_p))
    #
    # # Test 4: Sum two vectors bit-wise  *****************************************************
    # res_c = c_add_vectors(arr_v, arr_w)
    # print("Sum two vectors bit-wise (C++): v+w={}".format(list(res_c)))

    # Test 5: Class Item  *****************************************************
    item1 = Item(1, "My item 1")
    item1_name = item1.get_name()
    print("Item name: {}".format(item1_name))

    item2 = Item(2, "My item 2")
    item2_name = item2.get_name()
    print("Item name: {}".format(item2_name))

    c = Container()
    res1 = c.addItem(item1)
    res2 = c.addItem(item2)

    print("Container add item: {}".format(res1))
    print("Container add item: {}".format(res2))

    ci_1 = c.getItem(1)
    ci_2 = c.getItem(2)
    print("Container item name: {}".format(ci_1.get_name()))
    print("Container item name: {}".format(ci_2.get_name()))

if __name__ == "__main__":
    main()


import socket
from pynput.keyboard import Controller as KeyboardController

HOST = '0.0.0.0'  # Server IP address
PORT = 5000  # Port number

keyboard = KeyboardController()

def client_thread(conn):
    while True:
        message = conn.recv(1024).decode()
        if not message:
            break

        x, y, action = message.split(',')

        if action == "left_click":
            print("Performing left-click action")
            # Perform left-click action here
        elif action == "right_click":
            print("Performing right-click action")
            # Perform right-click action here
        elif action == "move":
            print(f"Performing mouse move action to {x}, {y}")
            # Perform mouse move action here
        elif action.startswith("key"):
            keys = action.split(":")[1:]
            print(f"Simulating key presses: {keys}")
            for key in keys:
                keyboard.press(key)
                keyboard.release(key)

# Create a socket object to establish the server's connection
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the IP address and port number
server_socket.bind((HOST, PORT))
# Start listening for incoming connections:
server_socket.listen(1)
print("Server is listening...")
# Accept a client connection and print the client's address
client_socket, address = server_socket.accept()
print("Client connected:", address)

client_thread(client_socket)


from time import sleep
num = int(input('Digite um número para saber sua tabuada: '))
de = int(input('Do número: '))
ao = int(input('Ao número: '))
print(' ', '=-' * 9)
for x in range(de, ao + 1):
    igu = num * x
    print('|| ', '{:2} x {:2} = {:2} '.format(num, x, igu),  '  ||')
    sleep(0.5)
print(' ', '=-' * 9)


import subprocess
import sys

import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg
import tkinter
import tkinter.filedialog
import os
import spc

def open_folder(location):
    if sys.platform.startswith('linux'):
        subprocess.call(["xdg-open", location])
    else:
        os.startfile(location)

def convert(filename, width, height, grid_size, omit_data, start_freq, end_freq, freq_step, update_signal=None):
    os.chdir(os.path.dirname(os.path.abspath(filename)))
    if not os.path.exists("output"):
        os.mkdir("output")
    width = int(width / grid_size)  # number of grids
    height = int(height / grid_size)
    matrix = {}
    data = {}
    for i in range(start_freq, end_freq, freq_step):
        matrix[i] = []
        data[i] = []
        for j in range(height):
            matrix[i].append([])
            data[i].append([])
            for k in range(width):
                matrix[i][j].append(0)
                data[i][j].append([])

    f = spc.File(filename)
    fdata = f.data_txt().split("\n")
    for samples in fdata:
        dat = samples.split("\t")
        if len(dat) == 1:
            continue
        wavelength = dat[0]
        col = 0
        row = 0
        reverse = False
        for j in range(omit_data + 1, omit_data + width * height + 1):
            if j >= len(dat):
                amp = 0
            else:
                amp = dat[j]
            idx = round(float(wavelength) / freq_step) * freq_step
            if idx < start_freq or idx >= end_freq:
                continue
            data[idx][row][col].append(float(amp))
            if reverse:
                col -= 1
            else:
                col += 1
            if (reverse and col == -1) or (not reverse and col % width == 0):
                reverse = not reverse
                row += 1
                col = width - 1 if reverse else 0

    # calculate avg
    for i in range(start_freq, end_freq, freq_step):
        for j in range(height):
            for k in range(width):
                if len(data[i][j][k]) == 0:
                    continue
                matrix[i][j][k] = sum(data[i][j][k]) / float(len(data[i][j][k]))

    # output
    total = len(matrix)
    current = 0
    for i in matrix.keys():
        name = str(i)
        fig, ax = plt.subplots()
        cax = ax.imshow(matrix[i], interpolation='nearest')
        ax.set_title(name)
        ax.spines['left'].set_position(('outward', 10))
        ax.spines['bottom'].set_position(('outward', 10))
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.yaxis.set_ticks_position('left')
        ax.xaxis.set_ticks_position('bottom')
        fig.colorbar(cax)
        plt.savefig("output/" + name + ".png")
        plt.close()
        current += 1
        if update_signal is not None:
            update_signal.emit(current, total)

    open_folder(os.path.join(os.curdir, "output"))


""" This is the main menu for the generator. """

import sys
from PyQt5.QtWidgets import QGridLayout, QLabel, QLayoutItem
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QImage, QIcon, QPixmap, QFont
from utils.multiview import View

class MainMenu(View):
    """ Class that contains opening screen of program. """
    def setup_view(self):
        """ Initialize window & data structures. """
        self.load_menu('config/main-menu.json')

        # set up dictionary of widgets and layouts to easily find.
        self.widgets = {}
        self.layouts = {}
        self.setupInterface()

    def setupInterface(self):
        self.layouts['main'] = QGridLayout()

        self.widgets['welcomelabel'] = QLabel("Welcome! "
                                              "To begin, load your town "
                                              "into the wizard, or click help")
        font = QFont("Arial", 18)
        self.widgets['welcomelabel'].setFont(font)

        self.widgets['image'] = QLabel()
        pixmap = QPixmap('img/dnd4_resized.png')
        self.widgets['image'].setPixmap(pixmap)
        self.widgets['image'].setAlignment(Qt.AlignCenter)

        self.layouts['main'].addWidget(self.widgets['welcomelabel'], 0, 1)
        self.layouts['main'].addWidget(self.widgets['image'], 1, 1,)
        self.layouts['main'].setColumnStretch(0, 1)
        self.layouts['main'].setColumnStretch(2, 1)
        self.layouts['main'].setRowStretch(2, 1)

        self.set_layout(self.layouts['main'])

    def sig(self):
        sys.exit()


import json
# import os
import requests

PAGESPEED_URL = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed'

def get_score(request):
    return request.json()
    return request.json()['lighthouseResult']['categories']['performance']['score']

with open('saas2000.json') as json_file:
    data = json.load(json_file)

for site in data[1:2]:
    print(site['url'])
    payload = {'url': site['url']}

    r = requests.get(PAGESPEED_URL, params=payload)
    performance = get_score(r)

    # mobile
    payload['strategy'] = 'mobile'
    r = requests.get(PAGESPEED_URL, params=payload)
    mobile_performance = get_score(r)

    site['performance'] = {
        'desktop': performance,
        'mobile': mobile_performance
    }
    print(site['performance'])

    with open('saas2000-21-10-2019.json', 'w') as outfile:
        json.dump(mobile_performance, outfile, indent=2)

# with open('sites_speed.json', 'w') as outfile:
#     json.dump(data, outfile, indent=2)


# -*- coding: utf-8 -*-
#
# Bounding Box Editor and Exporter (BBoxEE)
# Author: Peter Ersts (ersts@amnh.org)
#
# --------------------------------------------------------------------------
#
# This file is part of Animal Detection Network's (Andenet)
# Bounding Box Editor and Exporter (BBoxEE)
#
# BBoxEE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BBoxEE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software.  If not, see <http://www.gnu.org/licenses/>.
#
# --------------------------------------------------------------------------
import os
import json
import random
import datetime
import numpy as np
from shutil import copyfile
from PIL import Image
from PyQt6 import QtCore

class Exporter(QtCore.QThread):
    """Export annotated image into the COCO format."""

    progress = QtCore.pyqtSignal(int)
    exported = QtCore.pyqtSignal(int, int)

    def __init__(self,
                 directory,
                 images,
                 label_map,
                 validation_split,
                 shards=0,
                 masks={},
                 strip_metadata=False):
        """
        Class init function.

        Args:
            directory (str): Destination directory
            image_data (File): Image and Annotation List
            labels_map (dict): Class/label names
            validation_split (float): Percent to use for validation
            masks (dict): Binary arrays for masking metadata
            strip_metadata (bool): Flag for stripping metadata
        """
        QtCore.QThread.__init__(self)
        self.info = {}
        self.directory = directory
        self.images = images
        self.label_map = label_map
        self.train_size = int((1.0 - validation_split) * len(self.images))
        self.stop = False

        self.masks = masks
        self.strip_metadata = strip_metadata

        for mask in masks:
            m = np.array(masks[mask], dtype='uint8')
            self.masks[mask] = np.dstack((m, m, m))

        # TODO: How handle negative images in COCO
        labels = set()
        for label in label_map:
            if label_map[label].lower() != 'exclude':
                if label_map[label] == '':
                    self.label_map[label] = label
                labels.add(self.label_map[label])
        self.labels = list(labels)
        self.labels.sort()

    def run(self):
        """
        The starting point for the thread.

        After creating and instance of the class, calling start() will call
        this function which exports all of the annotaiton examples to disk.
        """
        self.stop = False
        random.shuffle(self.images)

        license_name = ['No License']
        licenses = [{'id': 0, 'name': 'No License', 'url': ''}]

        # Build categories
        categories = [{"id": i,
                       "name": l,
                       "supercategory": "none"} for i, l in enumerate(
                           self.labels)]

        # Create new directories
        image_train_path = os.path.join(self.directory, 'train')
        image_val_path = os.path.join(self.directory, 'validation')
        os.makedirs(image_train_path)
        os.makedirs(image_val_path)

        prefix = 'train_'
        img_path = os.path.join(image_train_path, prefix)
        train = {'info': self.info,
                 'images': [],
                 'annotations': [],
                 'licenses': [],
                 'categories': categories}
        val = {'info': self.info,
               'images': [],
               'annotations': [],
               'licenses': [],
               'categories': categories}

        current = train
        annotation_count = 0
        for count, rec in enumerate(self.images):
            if self.stop:
                break
            if count > self.train_size:
                current = val
                prefix = 'val_'
                img_path = os.path.join(image_val_path, prefix)
            img_file = img_path + '{:010d}.jpg'.format(count)

            src_file = os.path.join(rec['directory'], rec['file_name'])
            if os.path.exists(src_file):
                timestamp = os.path.getctime(src_file)
                timestamp = datetime.datetime.fromtimestamp(timestamp)
                rec['date_captured'] = str(timestamp)
                img = Image.open(src_file)
                size = img.size  # PIL (width, height)
                if self.strip_metadata:
                    array = np.array(img)
                    img.close()
                    if rec['mask_name'] in self.masks:
                        array = array * self.masks[rec['mask_name']]
                    img = Image.fromarray(array)
                    img.save(img_file)
                    img.close()
                else:
                    img.close()
                    copyfile(src_file, img_file)

                # Build license object
                if rec['license'] != '' and rec['license'] not in license_name:
                    licenses.append({'id': len(license_name), 'name': rec['license'], 'url': rec['license_url']})
                    license_name.append(rec['license'])
                if rec['license'] == '':
                    license_num = 0
                else:
                    license_num = license_name.index(rec['license'])

                # Store image entry
                image_rec = {}
                image_rec['id'] = count
                image_rec['width'] = size[0]
                image_rec['height'] = size[1]
                image_rec['file_name'] = prefix + '{:010d}.jpg'.format(count)
                image_rec['license'] = license_num
                image_rec['attribution'] = rec['attribution']
                image_rec['flickr_url'] = ''
                image_rec['coco_url'] = ''
                image_rec['date_captured'] = rec['date_captured']
                current['images'].append(image_rec)

                for ann in rec['annotations']:
                    annotation_count += 1
                    bbox = ann['bbox']
                    width = (bbox['xmax'] - bbox['xmin']) * size[0]
                    height = (bbox['ymax'] - bbox['ymin']) * size[1]
                    annotation = {}
                    annotation['id'] = annotation_count
                    annotation['image_id'] = count
                    remap = self.label_map[ann['label']]
                    annotation['category_id'] = self.labels.index(remap)
                    annotation['segmentation'] = []
                    annotation['area'] = 0.0
                    x = bbox['xmin'] * size[0]
                    y = bbox['ymin'] * size[1]
                    annotation['bbox'] = [x, y, width, height]
                    annotation['iscrowd'] = 0
                    current['annotations'].append(annotation)

                self.progress.emit(count + 1)
        train['licenses'] = licenses
        val['licenses'] = licenses

        file = open(os.path.join(self.directory, 'train.json'), 'w')
        json.dump(train, file, indent=4)
        file.close()

        file = open(os.path.join(self.directory, 'validation.json'), 'w')
        json.dump(val, file, indent=4)
        file.close()

        file = open(os.path.join(self.directory, 'label_remap.json'), 'w')
        json.dump(self.label_map, file, indent=4)
        file.close()

        self.exported.emit(self.train_size, len(self.images) - self.train_size)


# Use setuptools if we can
try:
    from setuptools.core import setup
except ImportError:
    from distutils.core import setup

PACKAGE = 'awslib'
VERSION = '0.6.7'

setup(
    name=PACKAGE, version=VERSION,
    description="Small library providing Amazon Web Services utility functions on top of boto",
    packages=['awslib'],
    license='MIT',
    author='Art Discovery Ltd',
    maintainer='Art Discovery Ltd',
    maintainer_email='tech@artfinder.com',
    install_requires=[
        'boto',
    ],
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 2',
    ],
)


# mlxtend Machine Learning Library Extensions
# Author: Steve Harenberg <harenbergsd@gmail.com>
#
# License: BSD 3 clause

import math
import itertools

import numpy as np
import pandas as pd
import collections
from collections import defaultdict as Dict
from distutils.version import LooseVersion as Version
from pandas import __version__ as pandas_version

class Node(object):
    def __init__(self, item, count=0, parent=None):
        self.item = item
        self.count = count
        self.parent = parent
        self.children = Dict(Node)

        if parent is not None:
            parent.children[item] = self

    def find_path_from_root(self):
        path = []
        if self.item is None:
            return path

        node = self.parent
        while node.item is not None:
            path.append(node.item)
            node = node.parent

        path.reverse()
        return path

class ConditionedFPTree(object):
    def __init__(self, ordering=None):
        self.ordering = ordering
        self.root = Node(None)
        self.nodes = Dict(list)
        self.cond_items = []

    def conditional_tree(self, cond_item, minsup):
        branches = []
        count = Dict(int)
        for node in self.nodes[cond_item]:
            branch = node.find_path_from_root()
            branches.append(branch)
            for item in branch:
                count[item] += node.count

        items = [item for item in count if count[item] >= minsup]
        items.sort(key=count.get)
        ordering = {item: i for i, item in enumerate(items)}

        cond_tree = ConditionedFPTree(ordering)
        for idx, branch in enumerate(branches):
            branch = sorted([i for i in branch if i in ordering],
                            key=ordering.get, reverse=True)
            cond_tree.insert_transaction(branch, self.nodes[cond_item][idx].count)
        cond_tree.cond_items = self.cond_items + [cond_item]

        return cond_tree

    def insert_transaction(self, itemset, count=1):
        self.root.count += count

        if len(itemset) == 0:
            return

        index = 0
        node = self.root
        for item in itemset:
            if item in node.children:
                child = node.children[item]
                child.count += count
                node = child
                index += 1
            else:
                break

        # Insert any remaining items
        for item in itemset[index:]:
            child_node = Node(item, count, node)
            self.nodes[item].append(child_node)
            node = child_node

    def is_path(self):
        if len(self.root.children) > 1:
            return False
        for i in self.nodes:
            if len(self.nodes[i]) > 1 or len(self.nodes[i][0].children) > 1:
                return False
        return True

    def print_status(self, count, colnames):
        cond_items = [str(i) for i in self.cond_items]
        if colnames:
            cond_items = [str(colnames[i]) for i in self.cond_items]
        cond_items = ", ".join(cond_items)
        print('\r%d itemset(s) from tree conditioned on items (%s)' %
              (count, cond_items), end="\n")

def buildFPTree(df, min_support):
    num_itemsets = len(df.index)
    itemsets = df.values

    item_support = np.array(np.sum(itemsets, axis=0) / float(num_itemsets))
    item_support = item_support.reshape(-1)

    items = np.nonzero(item_support >= min_support)[0]

    indices = item_support[items].argsort()
    ordering = {item: i for i, item in enumerate(items[indices])}

    tree = ConditionedFPTree(ordering)
    for i in range(num_itemsets):
        nonnull = np.where(itemsets[i, :])[0]
        itemset = [item for item in nonnull if item in ordering]
        itemset.sort(key=ordering.get, reverse=True)
        tree.insert_transaction(itemset)

    return tree, ordering

def generate_itemsets(generator, num_itemsets, colname_map):
    itemsets = []
    supports = []
    for sup, iset in generator:
        itemsets.append(frozenset(iset))
        supports.append(sup / num_itemsets)

    res_df = pd.DataFrame({'support': supports, 'itemsets': itemsets})

    if colname_map is not None:
        res_df['itemsets'] = res_df['itemsets'] \
            .apply(lambda x: frozenset([colname_map[i] for i in x]))

    return res_df

def FPgrowth(df, min_support=0.5, use_colnames=False, max_len=None, verbose=0, merge=False):
    colname_map = None
    if use_colnames:
        colname_map = {idx: item for idx, item in enumerate(df.columns)}

    tree, _ = buildFPTree(df, min_support)
    minsup = math.ceil(min_support * len(df.index))  # min support as count
    generator = compute_fpg(tree, minsup, colname_map, max_len, verbose,merge)

    return generate_itemsets(generator, len(df.index), colname_map)

def compute_fpg(tree, minsup, colnames, max_len, verbose, merge=False):
    count = 0
    items = tree.nodes.keys()
    if tree.is_path():
        size_remain = len(items) + 1
        if max_len:
            size_remain = max_len - len(tree.cond_items) + 1
        for i in range(1, size_remain):
            for itemset in itertools.combinations(items, i):
                count += 1
                support = min([tree.nodes[i][0].count for i in itemset])
                yield support, tree.cond_items + list(itemset)
    elif not max_len or max_len > len(tree.cond_items):
        for item in items:
            count += 1
            support = sum([node.count for node in tree.nodes[item]])
            yield support, tree.cond_items + [item]

    if verbose:
        tree.print_status(count, colnames)

    if not tree.is_path() and (not max_len or max_len > len(tree.cond_items)):
        for item in items:
            cond_tree = tree.conditional_tree(item, minsup)
            for sup, iset in compute_fpg(cond_tree, minsup,
                                      colnames, max_len, verbose):
                yield sup, iset

import pandas as pd
import numpy as np
from utils import *
from mlxtend.frequent_patterns import fpgrowth, apriori # fpgrowth, apriori library
dataset = parse_data("data/Skin.txt")
columns_, columns_mapping = column_names(dataset)
array = convert_to_bool_df(dataset, columns_, columns_mapping)
df = pd.DataFrame(array, columns=columns_)

import time
start_time = time.time()
frequent_itemsets = FPgrowth(df, min_support=0.2)
closed_frequent_itemsets = closed_frequent(frequent_itemsets)
print(len(closed_frequent_itemsets))
print('Time to find Close frequent itemset using Library')
print("--- %s seconds ---" % (time.time() - start_time))


from .views import SpeakingAPIView, SpeakingRudView
from django.urls import re_path, path
from . import views

app_name = 'api-speaking'

urlpatterns = [
    re_path(r'^(?P<pk>\d+)/$', SpeakingRudView.as_view(), name='speaking-rud'),
    path(r'', SpeakingAPIView.as_view(), name='speaking-create'),
    path('choose/', views.choose, name='choose'),
    path('info/', views.info, name='info'),
    path('thanks/', views.thanks, name='thanks'),
]


import hci_packets as hci
import link_layer_packets as ll
import llcp_packets as llcp
import random
import unittest
from hci_packets import ErrorCode
from py.bluetooth import Address
from py.controller import ControllerTest

class Test(ControllerTest):

    SDU_Interval_C_TO_P = 10000  # 10ms
    SDU_Interval_P_TO_C = 10000  # 10ms
    ISO_Interval = 16  # 20ms
    Worst_Case_SCA = hci.ClockAccuracy.PPM_500
    Packing = hci.Packing.SEQUENTIAL
    Framing = hci.Enable.DISABLED
    NSE = 4
    Max_SDU_C_TO_P = 130
    Max_SDU_P_TO_C = 130
    Max_PDU_C_TO_P = 130
    Max_PDU_P_TO_C = 130
    PHY_C_TO_P = 0x1
    PHY_P_TO_C = 0x1
    FT_C_TO_P = 1
    FT_P_TO_C = 1
    BN_C_TO_P = 2
    BN_P_TO_C = 2
    Max_Transport_Latency_C_TO_P = 40000  # 40ms
    Max_Transport_Latency_P_TO_C = 40000  # 40ms
    RTN_C_TO_P = 3
    RTN_P_TO_C = 3

    # LL/CIS/CEN/BV-01-C [CIS Setup Procedure, Central Initiated]
    async def test(self):
        # Test parameters.
        cig_id = 0x12
        cis_id = 0x42
        cis_connection_handle = 0xe00
        peer_address = Address('aa:bb:cc:dd:ee:ff')
        controller = self.controller

        # Enable Connected Isochronous Stream Host Support.
        await self.enable_connected_isochronous_stream_host_support()

        # Prelude: Establish an ACL connection as central with the IUT.
        acl_connection_handle = await self.establish_le_connection_central(peer_address)

        # 1. The Upper Tester sends an HCI_LE_Set_CIG_Parameters_Test command to the IUT with
        # CIS_Count set to 1, BN, FT, NSE, PHY_C_TO_P[], PHY_P_TO_C[] and ISO_Interval to be set to
        # the values specified in Table 4.135 and Table 4.136. Any remaining values are assigned the
        # default values as specified in Section 4.10.1.3 Default Values for Set CIG Parameters
        # Commands. The Upper Tester receives a successful HCI_Command_Complete event with a
        # valid Connection_Handle from the IUT and CIS_Count = 1.
        controller.send_cmd(
            hci.LeSetCigParametersTest(cig_id=cig_id,
                                       sdu_interval_c_to_p=self.SDU_Interval_C_TO_P,
                                       sdu_interval_p_to_c=self.SDU_Interval_P_TO_C,
                                       ft_c_to_p=self.FT_C_TO_P,
                                       ft_p_to_c=self.FT_P_TO_C,
                                       iso_interval=self.ISO_Interval,
                                       worst_case_sca=self.Worst_Case_SCA,
                                       packing=self.Packing,
                                       framing=self.Framing,
                                       cis_config=[
                                           hci.LeCisParametersTestConfig(cis_id=cis_id,
                                                                         nse=self.NSE,
                                                                         max_sdu_c_to_p=self.Max_SDU_C_TO_P,
                                                                         max_sdu_p_to_c=self.Max_SDU_P_TO_C,
                                                                         max_pdu_c_to_p=self.Max_PDU_C_TO_P,
                                                                         max_pdu_p_to_c=self.Max_PDU_P_TO_C,
                                                                         phy_c_to_p=self.PHY_C_TO_P,
                                                                         phy_p_to_c=self.PHY_P_TO_C,
                                                                         bn_c_to_p=self.BN_C_TO_P,
                                                                         bn_p_to_c=self.BN_P_TO_C)
                                       ]))

        await self.expect_evt(
            hci.LeSetCigParametersTestComplete(status=ErrorCode.SUCCESS,
                                               num_hci_command_packets=1,
                                               cig_id=cig_id,
                                               connection_handle=[cis_connection_handle]))

        # 2. The Upper Tester sends an HCI_LE_Create_CIS command to the IUT with the
        # ACL_Connection_Handle of the established ACL connection and CIS_Count set to 1. The Upper
        # Tester receives a Status of Success from the IUT.
        controller.send_cmd(
            hci.LeCreateCis(cis_config=[
                hci.LeCreateCisConfig(cis_connection_handle=cis_connection_handle,
                                      acl_connection_handle=acl_connection_handle)
            ]))

        await self.expect_evt(hci.LeCreateCisStatus(status=ErrorCode.SUCCESS, num_hci_command_packets=1))

        # 3. The Lower Tester receives an LL_CIS_REQ PDU from the IUT with all fields set to valid values.
        # CIS_Offset_Min is a value between 500µs and TSPX_conn_interval, CIS_Offset_Max is a value
        # between CIS_Offset_Min and the CIS_Offset_Max value as calculated in [14] Section 2.4.2.29
        # using TSPX_conn_interval as the value of connInterval, and connEventCount is the reference
        # event anchor point for which the offsets applied.
        cis_req = await self.expect_llcp(source_address=controller.address,
                                         destination_address=peer_address,
                                         expected_pdu=llcp.CisReq(cig_id=cig_id,
                                                                  cis_id=cis_id,
                                                                  phy_c_to_p=hci.PhyType.LE_1M,
                                                                  phy_p_to_c=hci.PhyType.LE_1M,
                                                                  framed=self.Framing == hci.Enable.ENABLED,
                                                                  max_sdu_c_to_p=self.Max_SDU_C_TO_P,
                                                                  max_sdu_p_to_c=self.Max_SDU_P_TO_C,
                                                                  sdu_interval_c_to_p=self.SDU_Interval_C_TO_P,
                                                                  sdu_interval_p_to_c=self.SDU_Interval_P_TO_C,
                                                                  max_pdu_c_to_p=self.Max_PDU_C_TO_P,
                                                                  max_pdu_p_to_c=self.Max_PDU_P_TO_C,
                                                                  nse=self.NSE,
                                                                  sub_interval=self.Any,
                                                                  bn_p_to_c=self.BN_C_TO_P,
                                                                  bn_c_to_p=self.BN_P_TO_C,
                                                                  ft_c_to_p=self.FT_C_TO_P,
                                                                  ft_p_to_c=self.FT_P_TO_C,
                                                                  iso_interval=self.ISO_Interval,
                                                                  cis_offset_min=self.Any,
                                                                  cis_offset_max=self.Any,
                                                                  conn_event_count=0))

        # 4. The Lower Tester sends an LL_CIS_RSP PDU to the IUT.
        controller.send_llcp(source_address=peer_address,
                             destination_address=controller.address,
                             pdu=llcp.CisRsp(cis_offset_min=cis_req.cis_offset_min,
                                             cis_offset_max=cis_req.cis_offset_max,
                                             conn_event_count=0))

        # 5. The Lower Tester receives an LL_CIS_IND from the IUT where the CIS_Offset is the time (ms)
        # from the start of the ACL connection event in connEvent Count to the first CIS anchor point, the
        # CIS_Sync_Delay is CIG_Sync_Delay minus the offset from the CIG reference point to the CIS
        # anchor point in s, and the connEventCount is the CIS_Offset reference point.
        cis_ind = await self.expect_llcp(source_address=controller.address,
                                         destination_address=peer_address,
                                         expected_pdu=llcp.CisInd(aa=0,
                                                                  cis_offset=self.Any,
                                                                  cig_sync_delay=self.Any,
                                                                  cis_sync_delay=self.Any,
                                                                  conn_event_count=0))

        # 6. The IUT sends a CIS Null PDU to the Lower Tester and the Lower Tester responds with a CIS
        # Null PDU. Alternately, the IUT sends an empty Data PDU, which the Lower Tester acknowledges.
        # These exchanges will continue until data is exchanged between the IUT and the Lower Tester in
        # later steps.

        # 7. The Upper Tester receives a successful HCI_LE_CIS_Established event with the NSE, BN, FT,
        # and Max_PDU parameters as set in step 1 from the IUT, after the first CIS packet sent by the LT.
        # The Connection_Handle parameter is set to the value provided in the HCI_LE_Create_CIS
        # command.
        await self.expect_evt(
            hci.LeCisEstablished(status=ErrorCode.SUCCESS,
                                 connection_handle=cis_connection_handle,
                                 cig_sync_delay=cis_ind.cig_sync_delay,
                                 cis_sync_delay=cis_ind.cis_sync_delay,
                                 transport_latency_c_to_p=self.Any,
                                 transport_latency_p_to_c=self.Any,
                                 phy_c_to_p=hci.SecondaryPhyType.LE_1M,
                                 phy_p_to_c=hci.SecondaryPhyType.LE_1M,
                                 nse=self.NSE,
                                 bn_c_to_p=self.BN_C_TO_P,
                                 bn_p_to_c=self.BN_P_TO_C,
                                 ft_c_to_p=self.FT_C_TO_P,
                                 ft_p_to_c=self.FT_P_TO_C,
                                 max_pdu_c_to_p=self.Max_PDU_C_TO_P,
                                 max_pdu_p_to_c=self.Max_PDU_P_TO_C,
                                 iso_interval=self.ISO_Interval))

        # 8. The Upper Tester orders the IUT to send data packets to the Lower Tester.
        iso_sdu = [random.randint(1, 251) for n in range(self.Max_SDU_C_TO_P)]
        controller.send_iso(
            hci.IsoWithoutTimestamp(
                connection_handle=cis_connection_handle,
                pb_flag=hci.IsoPacketBoundaryFlag.COMPLETE_SDU,
                packet_sequence_number=42,
                payload=iso_sdu,
            ))

        # 9. The Lower Tester receives CIS data PDUs from the IUT in each sub-event of the CIS and
        # acknowledges those PDUs.
        await self.expect_ll(
            ll.LeConnectedIsochronousPdu(source_address=controller.address,
                                         destination_address=peer_address,
                                         cig_id=cig_id,
                                         cis_id=cis_id,
                                         sequence_number=42,
                                         data=iso_sdu))

        # 10. Repeat step 9 for 50 ÷ BN isochronous events starting with the first event where a CIS data PDU
        # with nonzero payload is received.