from collections.abc import (
    Callable,
    Coroutine,
)
from dast.aws.types import (
    Location,
)
from dast.aws.utils import (
    build_vulnerabilities,
    run_boto3_fun,
)
from model import (
    core_model,
)
from model.core_model import (
    AwsCredentials,
    Vulnerability,
)
from typing import (
    Any,
)

async def uses_insecure_security_policy(
    credentials: AwsCredentials,
) -> core_model.Vulnerabilities:

    predifined_ssl_policy_values: list[str] = [
        "ELBSecurityPolicy-2015-05",
        "ELBSecurityPolicy-2016-08",
        "ELBSecurityPolicy-FS-2018-06",
        "ELBSecurityPolicy-FS-1-1-2019-08",
        "ELBSecurityPolicy-FS-1-2-2019-08",
        "ELBSecurityPolicy-FS-1-2-Res-2019-08",
        "ELBSecurityPolicy-FS-1-2-Res-2020-10",
        "ELBSecurityPolicy-TLS-1-0-2015-04",
        "ELBSecurityPolicy-TLS-1-1-2017-01",
        "ELBSecurityPolicy-TLS-1-2-2017-01",
        "ELBSecurityPolicy-TLS-1-2-Ext-2018-06",
        "ELBSecurityPolicy-TLS13-1-0-2021-06",
        "ELBSecurityPolicy-TLS13-1-1-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-Ext1-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-Ext2-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-Res-2021-06",
        "ELBSecurityPolicy-TLS13-1-3-2021-06",
    ]

    safe_ssl_policy_values: list[str] = [
        "ELBSecurityPolicy-FS-1-2-Res-2020-10",
        "ELBSecurityPolicy-TLS13-1-2-Res-2021-06",
        "ELBSecurityPolicy-TLS13-1-3-2021-06",
    ]

    response: dict[str, Any] = await run_boto3_fun(
        credentials, service="elbv2", function="describe_load_balancers"
    )
    balancers = response.get("LoadBalancers", []) if response else []
    vulns: core_model.Vulnerabilities = ()
    if balancers:
        for balancer in balancers:
            locations: list[Location] = []
            load_balancer_arn = balancer["LoadBalancerArn"]

            listeners: dict[str, Any] = await run_boto3_fun(
                credentials,
                service="elbv2",
                function="describe_listeners",
                parameters={
                    "LoadBalancerArn": str(load_balancer_arn),
                },
            )
            for index, listener in enumerate(listeners.get("Listeners", [])):
                ssl_policy = listener.get("SslPolicy", None)
                if (
                    ssl_policy
                    and ssl_policy in predifined_ssl_policy_values
                    and ssl_policy not in safe_ssl_policy_values
                ):
                    locations = [
                        *locations,
                        *[
                            Location(
                                access_patterns=(
                                    f"/Listeners/{index}/SslPolicy",
                                ),
                                arn=(f"{listener['ListenerArn']}"),
                                values=(ssl_policy,),
                                description=(
                                    "src.lib_path.f070."
                                    "elb2_uses_insecure_security_policy"
                                ),
                            )
                        ],
                    ]

            vulns = (
                *vulns,
                *build_vulnerabilities(
                    locations=locations,
                    method=(
                        core_model.MethodsEnum.AWS_INSECURE_SECURITY_POLICY
                    ),
                    aws_response=listeners,
                ),
            )

    return vulns

async def target_group_insecure_port(
    credentials: AwsCredentials,
) -> core_model.Vulnerabilities:

    response: dict[str, Any] = await run_boto3_fun(
        credentials, service="elbv2", function="describe_target_groups"
    )
    target_groups = response.get("TargetGroups", []) if response else []
    vulns: core_model.Vulnerabilities = ()
    if target_groups:
        for index, target_group in enumerate(target_groups):
            locations: list[Location] = []
            port = target_group.get("Port", "")
            target_type = target_group.get("TargetType", "")
            if target_type == "lambda":
                continue
            if not port:
                locations = [
                    Location(
                        access_patterns=(f"/TargetGroups/{index}/TargetType",),
                        arn=(f"{target_group['TargetGroupArn']}"),
                        values=(target_type,),
                        description=(
                            "src.lib_path.f070."
                            "elb2_target_group_insecure_port"
                        ),
                    )
                ]

            elif int(port) != 443:
                locations = [
                    Location(
                        access_patterns=(f"/TargetGroups/{index}/Port",),
                        arn=(f"{target_group['TargetGroupArn']}"),
                        values=(port,),
                        description=(
                            "src.lib_path.f070.elb2_target_group_insecure_port"
                        ),
                    )
                ]

            vulns = (
                *vulns,
                *build_vulnerabilities(
                    locations=locations,
                    method=(core_model.MethodsEnum.AWS_GROUP_INSECURE_PORT),
                    aws_response=response,
                ),
            )
    return vulns

async def eks_allows_insecure_inbound_traffic(
    credentials: AwsCredentials,
) -> core_model.Vulnerabilities:

    response: dict[str, Any] = await run_boto3_fun(
        credentials, service="eks", function="list_clusters"
    )
    model = core_model.MethodsEnum.AWS_EKS_INSECURE_INBOUND_TRAFFIC
    clusters = response.get("clusters", []) if response else []
    vulns: core_model.Vulnerabilities = ()
    if clusters:
        for cluster in clusters:
            locations: list[Location] = []
            describe_cluster: dict[str, Any] = await run_boto3_fun(
                credentials,
                service="eks",
                function="describe_cluster",
                parameters={
                    "name": str(cluster),
                },
            )
            cluster_description = describe_cluster.get("cluster", [])

            security_groups_ids = cluster_description["resourcesVpcConfig"][
                "securityGroupIds"
            ]
            describe_security_groups: dict[str, Any] = await run_boto3_fun(
                credentials,
                service="ec2",
                function="describe_security_groups",
                parameters={
                    "GroupIds": security_groups_ids,
                },
            )
            security_groups = (
                describe_security_groups.get("SecurityGroups", [])
                if response
                else []
            )
            if security_groups:
                for group in security_groups:
                    locations = [
                        *[
                            Location(
                                access_patterns=(
                                    f"/IpPermissions/{index_ip}/FromPort",
                                    f"/IpPermissions/{index_ip}/ToPort",
                                ),
                                arn=(
                                    f"arn:aws:ec2::{group['OwnerId']}:"
                                    f"security-group/{group['GroupId']}"
                                ),
                                values=(
                                    ip_permission.get("FromPort"),
                                    ip_permission.get("ToPort"),
                                ),
                                description=(
                                    "lib_path.f070."
                                    "eks_allows_insecure_inbound_traffic"
                                ),
                            )
                            for index_ip, ip_permission in enumerate(
                                group["IpPermissions"]
                            )
                            if not (
                                ip_permission["FromPort"] == 443
                                and ip_permission["FromPort"]
                                == ip_permission["ToPort"]
                            )
                        ],
                    ]

                    vulns = (
                        *vulns,
                        *build_vulnerabilities(
                            locations=locations,
                            method=model,
                            aws_response=group,
                        ),
                    )
    return vulns

CHECKS: tuple[
    Callable[[AwsCredentials], Coroutine[Any, Any, tuple[Vulnerability, ...]]],
    ...,
] = (
    target_group_insecure_port,
    uses_insecure_security_policy,
    eks_allows_insecure_inbound_traffic,
)


#cliente para pruebas
#cliente for testing purposes

import socket
import sys

Clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

direccion_servidor = ('localhost', 10000)

Clientsocket.connect(direccion_servidor)

mensajes = ['create //process A', 'create //process B']

try:
	for mensaje in mensajes:
		Clientsocket.sendall(mensaje)

		respuesta = Clientsocket.recv(256)
		print(respuesta)
finally:

	Clientsocket.close()


import cv2
import numpy as np
import torch

def torch_fix_seed(seed = 42):
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.use_deterministic_algorithms = True

torch_fix_seed()

def integrate_edges(
    filepath_line: str,
    filepath_triangle: str,
    filepath_output: str,
) -> str:
    """ 三角形の輪郭と直線を合わせる

    Args:
        filepath_line:      CSDの直線の二値画像
        filepath_triangle:  CSDの三角形の二値画像

    """

    img_line = cv2.imread(filepath_line, cv2.IMREAD_GRAYSCALE)
    img_triangle = cv2.imread(filepath_triangle, cv2.IMREAD_GRAYSCALE)

    edges_triangle = cv2.Canny(img_triangle, 50, 100)

    output = cv2.add(img_line, edges_triangle)

    cv2.imwrite(filepath_output, output)

    return filepath_output

if __name__ == "__main__":
    integrate_edges("./outputs/infer/pred_class1.png", "./outputs/infer/pred_class2.png", filapath_output="./outputs/infer/integrated_edge.png")


import os
threads = '1'
os.environ["OMP_NUM_THREADS"] = threads
os.environ["OPENBLAS_NUM_THREADS"] = threads
os.environ["MKL_NUM_THREADS"] = threads
os.environ["VECLIB_MAXIMUM_THREADS"] = threads
os.environ["NUMEXPR_NUM_THREADS"] = threads

import sys
import time
import pickle
import shutil
import argparse
import traceback
import numpy as np
import pandas as pd
import random
import cv2
import dlib
import scipy.io as sio
from skimage import io
from datetime import datetime

from sklearn.neighbors import NearestNeighbors
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.metrics.pairwise import euclidean_distances
import matplotlib.pyplot as plt
from matplotlib import style
from mpl_toolkits.mplot3d import Axes3D
from sklearn.manifold import TSNE
from utils import *
from multiprocessing import Pool
from multiprocessing_logging import install_mp_handler

import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
import torch.backends.cudnn as cudnn

## detect -> label -> recognize, align & compose: save aligned, composed faces and frame information
def recognize_align_compose(frame_dir, align_dir, compo_dir, score_dir, filt_info_file,
                            cluster_n=2, thres_dist=0.6, min_faces=10,
                            min_targets=10, min_variation=0.2, min_dist=0.6, verbose=True):
    '''
    (1) Detect all face locations, 68 2d landmarks in all frames and encode faces.
        Record information of each frame:
        {'num_faces': 0/1/2, 'has_target': 0/1, 'target_idx': None/0/1/2, 'locations': [(top, right, bottom, left)], '2d_landmarks': [ np.ndarray(68, 2)], 'encodings': [ np.ndarray(128,)]}
    (2) Label images as known target reference images.
    (3) Preprocess: record videos should be excluded.
    (4) Recognize target faces. Along the way, align and compose faces then save images.
    (5) Save frame information: {'frame001': {...}, 'frame002': {...}, ...},
        save table: ['frame_id', 'num_faces', 'has_target'],
        save possibly excluded video ids.
    :param frame_dir: directory containing frames
    :param align_dir: directory for saving aligned face images
    :param compo_dir: directory for saving composite face images
    :param filt_info_file: file for saving non-candidate video information
    :param cluster_n: number of known images chosen in each cluster, default is 2.
    :param thres_dist: threshold distance for recogizing face as target, default is 0.6.
    :param min_faces: minimum number of faces detected from all frames, default is 10. Will no recognize target if very few faces.
    :param min_targets: miminum number of target faces, default is 10.
    :param min_variation: minimum variation of all target faces, smaller videos will be discarded, default is 0.2.
    :param mim_dist: minimum distance between two clusters (only target faces), larger videos will be discarded, default is 0.6.
    :param verbose: verbose the process or not
    :return: no return
    '''

    ## extract video id
    vid_id = frame_dir.split('/')[-1]

    ## save aligned and composite faces to
    if align_dir and not os.path.exists(align_dir):
        os.makedirs(align_dir)
    if compo_dir and not os.path.exists(compo_dir):
        os.makedirs(compo_dir)
    if score_dir and not os.path.exists(score_dir):
        os.makedirs(score_dir)

    ## frames
    frame_files = os.listdir(frame_dir)
    frame_files = [x for x in frame_files if '.jpg' in x]
    frame_files = sorted(frame_files, key=lambda x: int(x.split('_')[0].replace('frame', '')))
    frame_ids = [x.split('.')[0] for x in frame_files]

    ## record
    frame_dict = dict.fromkeys(frame_ids)
    info_df = pd.DataFrame(columns=['frame_id', 'num_faces', 'has_target'])
    comment = ''

    # print(f'Begin detection {vid_id}')
    ## detect and encode
    for i, image_file in enumerate(frame_files):
        frame_info = dict.fromkeys(['num_faces', 'has_target', 'target_idx', 'locations', '2d_landmarks', 'encodings'])
        frame_id = image_file.split('.')[0]
        # original frame
        image = cv2.imread(os.path.join(frame_dir, image_file))
        # face located in the rectangle (0.2-0.3s): list of dlib rectangles
        face_locations = face_detector(image)
        # coordinates of 68 landmarks (0.004s): list of dlib full_object_detection objects
        landmarks = [pose_predictor_68_point(image, face_location) for face_location in face_locations]
        # face encodings (0.6-0.8s): list of (128,) arrays
        encodings = [np.array(face_encoder.compute_face_descriptor(image, landmark)) for landmark in landmarks]
        # record
        save_locations = [rect_to_css(x) for x in face_locations] # convert rectangle to (top, right, bottom, left)
        save_2d_landmarks = [landmarks_to_np(x) for x in landmarks] # convert dlib full_object_detection object to (68, 2) np.ndarray
        frame_info['num_faces'], frame_info['locations'], frame_info['2d_landmarks'], frame_info['encodings'] = \
            len(face_locations), save_locations, save_2d_landmarks, encodings
        if verbose and i % 100 == 0:
            print(f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] Encoded {i} / {len(frame_files)}")
        frame_dict[frame_id] = frame_info
    if verbose:
        print(f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] Finish face detection.")

    ## label images: closest to mean + k closest to mean of each cluster
    tot_num_faces = sum([v['num_faces'] for _, v in frame_dict.items()])
    if tot_num_faces >= min_faces:
        known_encodings, all_encodings = [], []
        for k, v in frame_dict.items():
            all_encodings.extend(v['encodings'])
        X = np.array(all_encodings)
        kmeans = KMeans(n_clusters=2).fit(X)
        y = kmeans.labels_
        large_id = 1 if sum(y) > len(y) / 2 else 0
        small_id = 1 - large_id
        mean_encoding = np.mean(X, axis=0)
        mean_encoding_l = np.mean(X[np.where(y == large_id)[0]], axis=0)
        mean_encoding_s = np.mean(X[np.where(y == small_id)[0]], axis=0)
        known_encodings.extend(X[np.argsort(np.linalg.norm(X[np.where(y == large_id)[0]] - mean_encoding_s, axis=1))[:cluster_n].tolist()])

        X_l = X[np.where(y == large_id)[0], :]
        X_s = X[np.where(y == small_id)[0], :]
        X_cluster = np.vstack([X_l, X_s])
        dist_cluster = euclidean_distances(X_cluster, X_cluster)
        min_between_dist_cluster = np.min(dist_cluster[X_l.shape[0]:, :X_l.shape[0]]) if X_l.shape[0] < X.shape[0] else 0
        # only use larger cluster if minimum distance between two clusters is greater than min_dist
        if min_between_dist_cluster <= min_dist:
            known_encodings.append(X[np.argmin(np.linalg.norm(X - mean_encoding, axis=1))])
            known_encodings.extend(X[np.argsort(np.linalg.norm(X[np.where(y == small_id)[0]] - mean_encoding_l, axis=1))[:cluster_n].tolist()])
        known_encodings = np.array(known_encodings)

        ## recognize
        encoding_list = []
        n = 0
        for i, (k, v) in enumerate(frame_dict.items()):
            # no faces detected
            if v['num_faces'] == 0:
                v['has_target'] = 0
            # have faces detected
            else:
                closest_dists, closest_dists_l, closest_dists_s = [], [], []
                for j, encoding in enumerate(v['encodings']):
                    if y[n+j] == large_id:
                        closest_dists_l.append(np.min(np.linalg.norm(known_encodings - encoding, axis=1)))
                    else:
                        closest_dists_s.append(np.min(np.linalg.norm(known_encodings - encoding, axis=1)))
                    closest_dists.append(np.min(np.linalg.norm(known_encodings - encoding, axis=1)))
                n += j + 1
                if len(closest_dists_l) > 0:
                    v['has_target'] = 1
                    v['target_idx'] = closest_dists.index(min(closest_dists_l))
                    encoding_list.append(np.insert(encoding, 0, 1))
                elif len(closest_dists_l) == 0 and len(closest_dists_s) > 0:
                    v['has_target'] = 1
                    v['target_idx'] = closest_dists.index(min(closest_dists_s))
                    encoding_list.append(np.insert(encoding, 0, 1))
                else:
                    v['has_target'] = 0
                    v['target_idx'] = None
                    encoding_list.append(np.insert(encoding, 0, 0))
                if v['target_idx'] is not None:
                    # align
                    image = cv2.imread(os.path.join(frame_dir, f'{k}.jpg'))
                    landmarks = v['2d_landmarks'][v['target_idx']]  # (68, 2)
                    aligned_face = face_align(image, landmarks)
                    # save aligned face
                    if align_dir:
                        cv2.imwrite(os.path.join(align_dir, f"{k}.jpg"), aligned_face)
                    # compose
                    left2, right2 = face_composite(aligned_face)
                    # save left2 and right2
                    if compo_dir:
                        cv2.imwrite(os.path.join(compo_dir, f"{k}_left.jpg"), left2)
                        cv2.imwrite(os.path.join(compo_dir, f"{k}_right.jpg"), right2)
            # record
            info_df.loc[i] = [k, v['num_faces'], v['has_target']]
        # non-candidate video info
        if sum(info_df['has_target']) < min_targets:
            comment = f"Less than {min_faces} target faces in all extracted frames."

        X_target = np.array(encoding_list)
        y_target = kmeans.labels_[np.where(X_target[:, 0] == 1)]
        X_target = X_target[np.where(X_target[:, 0] == 1)[0], 1:]
        X0_target = X_target[np.where(y_target == 0)[0], :]
        X1_target = X_target[np.where(y_target == 1)[0], :]
        X_cluster = np.vstack([X0_target, X1_target])
        dist_cluster = euclidean_distances(X_cluster, X_cluster)
        min_between_dist_cluster = np.min(dist_cluster[X0_target.shape[0]:, :X0_target.shape[0]]) if X0_target.shape[0] < X_target.shape[0] else 0
        if min_between_dist_cluster > min_dist:
            comment = f"Minimum distance between two target encodings is larger than {min_dist}"
        if np.max(dist_cluster) < min_variation:
            comment = f"Maximum distance between target encodings is smaller than {min_variation}"

    else:
        # some videos may not have faces detected
        for i, (k, v) in enumerate(frame_dict.items()):
            info_df.loc[i] = [k, v['num_faces'], None]
        # non-candidate video info
        comment = f"Less than {min_faces} faces in all extracted frames."

    # save frame info
    info_df.to_csv(os.path.join(score_dir, 'frame_info.csv'), sep='\t', index=False)
    # save all info
    with open(os.path.join(score_dir, 'frame_2d_68pts_encodings.pkl'), 'wb') as f:
        pickle.dump(frame_dict, f)
    # save non-candidate video info
    if len(comment) > 0:
        filt_info = f'{vid_id}: {comment}\n'
        with open(filt_info_file, 'a+') as f:
            f.write(filt_info)

    print(f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] Recognized and aligned video {frame_dir.split('/')[-1]}")
    logging.info(f"Recognized and aligned video {frame_dir.split('/')[-1]} (PID: {os.getpid()})")

if __name__ == '__main__':

    ## parse arguments
    parser = argparse.ArgumentParser()
    # directories
    parser.add_argument('--video-root', type=str, help='Specify the directory containing the videos.')
    parser.add_argument('--frame-root', type=str, help='Specify the directory for saving extracted frames.')
    parser.add_argument('--align-root', type=str, help='Specify the directory for saving the aligned face images.')
    parser.add_argument('--compo-root', type=str, help='Specify the directory for saving the composite face images.')
    parser.add_argument('--score-root', type=str, help='Specify the directory for saving information and scores.')
    parser.add_argument('--pre-filter-file', type=str, help='Specify the file for saving the pre-filtered video information.')
    # global setting
    parser.add_argument('-v', '--verbose', action='store_true', help='Verbose inside each step.')
    parser.add_argument('-q', '--quiet', action='store_true', help='Quiet inside each step.')

    args = parser.parse_args()

    video_root = args.video_root
    frame_root = args.frame_root
    align_root = args.align_root
    compo_root = args.compo_root
    score_root = args.score_root
    pre_filter_file = args.pre_filter_file
    open(pre_filter_file, 'w').close()
    if args.verbose:
        verbose = True
    elif args.quiet:
        verbose = False

    if not os.path.exists(align_root):
        os.makedirs(align_root)
    if not os.path.exists(compo_root):
        os.makedirs(compo_root)
    if not os.path.exists(score_root):
        os.makedirs(score_root)
    if not os.path.exists('logs'):
        os.makedirs('logs')
    log_file = f"logs/{frame_root.split('/')[1].split('_')[0]}_recognition_align.log"
    open(log_file, 'w').close()
    logging.basicConfig(filename=log_file,
                        level=logging.INFO,
                        format='[%(asctime)s] [%(levelname)s] %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        filemode='a+')
    install_mp_handler()

    video_ids = os.listdir(video_root)
    video_ids = sorted([x.split('.')[0] for x in video_ids])

    ## 2. recognize, align and compose
    # if os.path.exists(pre_filter_file):
    #     os.remove(pre_filter_file)
    inp_args = []
    for idx, vid_id in enumerate(video_ids):
        frame_dir = os.path.join(frame_root, vid_id)
        align_dir = os.path.join(align_root, vid_id)
        compo_dir = os.path.join(compo_root, vid_id)
        score_dir = os.path.join(score_root, vid_id)
        # if os.path.exists(os.path.join(score_dir, 'frame_2d_68pts_encodings.pkl')):
        #     continue
        inp_args.append((frame_dir, align_dir, compo_dir, score_dir, pre_filter_file,
                         2, 0.6, 10, 10, 0.2, 0.6, verbose))
    if len(inp_args) > 0:
        try:
            with Pool(os.cpu_count()) as pool:
                pool.starmap(recognize_align_compose, inp_args)
                pool.close()
                pool.join()
        except Exception as e:
            print(e)
            logging.error(e)
        print(f"[{datetime.today().strftime('%Y-%m-%d %H:%M:%S')}] ********** Finish step 2: recognize, align and compose **********")
        logging.info('********** Finish step 2: recognize, align and compose **********')


import cv2
import numpy as np

img = cv2.imread("../../../img/taekwonv1.jpg")

smaller = cv2.pyrDown(img)
bigger = cv2.pyrUp(smaller)

laplacian = cv2.subtract(img, bigger)
restored = bigger + laplacian
merged = np.hstack((img, laplacian, bigger, restored))

cv2.imshow("Laplacian Pyramid", merged)
cv2.waitKey(0)
cv2.destroyAllWindows()


import collections

class Solution:
    # @param {integer[]} nums
    # @param {integer} k
    # @param {integer} t
    # @return {boolean}
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        if k < 1 or t < 0:
            return False
        numDict = collections.OrderedDict()
        for x in range(len(nums)):
            key = nums[x] / max(1, t)
            for m in (key, key - 1, key + 1):
                if m in numDict and abs(nums[x] - numDict[m]) <= t:
                    return "true"
            numDict[key] = nums[x]
            if x >=  k:
                numDict.popitem(last=False)
        return "false"

t=Solution()
s=input()
T=int(s[len(s)-1])
num=[]
i=0
check=0
k=0
already=0
while (i<len(s)):
    if s[i]=='[':
        check=1
        while(s[i]!=']'):
            num.append(s[i])
            i+=1
    c=s[i]
    if check==1 and '0'<=s[i]<='9' and already==0:
        k=int(s[i])
        already+=1
    i+=1
nums="".join(num)+']'
print(t.containsNearbyAlmostDuplicate(eval(nums),k,T))


from ast import FunctionDef, Name
from copy import deepcopy

class BaseGeneration:
    def __init__(
        self, overload: FunctionDef, *, default_annotation: Name | None
    ) -> None:
        ovargs = overload.args
        self.overload = overload
        self.ndefaultargs = len(ovargs.defaults)
        self.ndefaultkwargs = sum(value is not None for value in ovargs.kw_defaults)
        self.default_annotation = default_annotation
        self._make_new_base()

    def _make_new_base(self):
        self.base = deepcopy(self.overload)
        baseargs = self.base.args
        ovargs = self.overload.args
        baseargs.args = ovargs.args[: self.ndefaultargs - 1]
        baseargs.kwonlyargs = ovargs.kwonlyargs[: self.ndefaultkwargs - 1]
        baseargs.kw_defaults = []
        baseargs.defaults = []

    def default_args(self):
        args = self.overload.args
        keys = args.args[self.ndefaultargs - 1 :]
        values = args.defaults
        yield from zip(keys, values)

    def defaulted_kwonly_args(self):
        args = self.overload.args
        default = self.default_annotation
        keys = args.kwonlyargs[self.ndefaultkwargs - 1 :]
        values = [default if value is None else value for value in args.kw_defaults]
        yield from zip(keys, values)

class PossibilityGeneration:
    def __init__(
        self, overload: FunctionDef, *, default_annotation: Name | None
    ) -> None:
        self.overload = overload
        self.base = BaseGeneration(overload, default_annotation=default_annotation)

    def __iter__(self):
        fresh = deepcopy(self.base.base)
        yield fresh
        for arg, value in self.base.default_args():
            fresh.args.args.append(arg)
            fresh.args.defaults.append(value)
            yield fresh
        for kwarg, value in self.base.defaulted_kwonly_args():
            fresh.args.kwonlyargs.append(kwarg)
            fresh.args.kw_defaults.append(value)
            yield fresh


#!/usr/bin/env python3

import sys
import os

from argparse import ArgumentParser

parser = ArgumentParser(description="Package a folder into the GPAK file format.")
parser.add_argument("input", help="the input folder to package as the GPAK")
parser.add_argument("output", help="the name and location of the resulting GPAK")
args = parser.parse_args()

RESOURCES_DIR = args.input
OUTPUT_GPAK = args.output

index = bytearray()
files = bytearray()

index_count = 0

def int_to_bytes (val, num_bytes):
    return [(val & (0xff << pos * 8)) >> pos * 8 for pos in range(num_bytes)]

for dir_name, subdir_list, file_list in os.walk(RESOURCES_DIR):
    for file_name in file_list:
        index_count = index_count + 1
        full_path = dir_name + "/" + file_name

        full_path = full_path.replace('\\', '/')
        full_path = full_path.lower()

        if full_path.startswith(RESOURCES_DIR):
            key_path = full_path[len(RESOURCES_DIR)+1:]

        index.extend(int_to_bytes(len(key_path), 2))
        index.extend(bytearray(key_path, "ascii"))
        with open(full_path, mode='rb') as file:
            file_content = file.read()
            length = len(file_content)

            print("Packing " + key_path + " (" + str(length) + " bytes)")
            index.extend(int_to_bytes(length, 4))

            files.extend(file_content)

gpak = open(OUTPUT_GPAK, "wb")
gpak.write(bytearray(int_to_bytes(index_count, 4)))
gpak.write(index)
gpak.write(files)


from copy import copy

def permute(nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """

    def backtrack(nums):
        if len(nums) == 1: return [nums]

        tmp_res = backtrack(nums[:-1])
        res = []
        for elem in tmp_res:
            for j in range(len(elem) + 1):
                tmp_elem = copy(elem)
                tmp_elem.insert(j, nums[-1])
                res.append(tmp_elem)
        return res

    result = backtrack(nums)
    return result

nums = [1, 2]
permute(nums)

nums[::-1]


import tkinter as tk
import tkinter
import customtkinter as ct
from tkinter import simpledialog
from tkinter.messagebox import askyesno, showinfo, showerror
from cryptography.fernet import Fernet
import cryptography
import pyperclip
import keyboard
import win10toast
from tkinter.filedialog import askopenfilename, asksaveasfile
import os
conversations = {}
global current_value
current_value = 0
class EncryptionSoftware():
    def __init__(self, key, enc, dec):
        self.key = key
        self.enc = enc
        self.dec = dec
        self.fern = Fernet(self.key)
        self.notifier=win10toast.ToastNotifier()
    def files_enc(self):
        file_r = askopenfilename()
        if os.path.splitext(file_r)[-1] == '.txt':
            with open(os.path.abspath(file_r), 'r') as encfile:
                dat =  encfile.read().encode('utf-8')
        else:
            with open(os.path.abspath(file_r), 'rb') as encfile:
                dat =  encfile.read()
        file_w = asksaveasfile(mode='wb', defaultextension='.crypt')
        file_w.write(self.fern.encrypt(dat))
        file_w.close()

    def files_dec(self):
        try:
            file_r = askopenfilename()
            with open(os.path.abspath(file_r), 'rb') as encfile:
                dat =  encfile.read()
            file_w = asksaveasfile(mode='wb')
            file_w.write(self.fern.decrypt(dat))
        except:
                showerror(title="File Permission denied", message="Could not open file! (maybe you closed the file explorer window?)")

    def encrypt(self, hotkey:bool = False, file:bool = False):
        data = pyperclip.paste().encode('utf-8') if hotkey else self.enc.get().encode('utf-8')
        pyperclip.copy(self.fern.encrypt(data).decode('utf-8'))

        self.notifier.show_toast(title="Encrypt and chat", msg="message encrypted.. paste to send encrypted message", threaded=True)
    def decrypt(self, hotkey:bool = False):
        try:
            data = pyperclip.paste().encode('utf-8')  if hotkey else self.dec.get().encode('utf-8')
            dec = str(self.fern.decrypt(data).decode('utf-8'))
        except cryptography.fernet.InvalidToken:
            showerror('An error occurred', f"On decrypting data, the following exception occured (probably the key or the message to be decrypted is invalid)")
            dec = "ERROR: <an error occured while decrypting this message>"
        pyperclip.copy(dec)
        self.notifier.show_toast(title="Encrypt and chat", msg=f"decrypted message reads: {dec}", threaded=True)
        showinfo('decrypted message', f"decrypted message: \n {dec}")
ct.set_appearance_mode("dark")
root = ct.CTk()
root.geometry("800x400")
root.title("Encrypt and chat")
l1 = ct.CTkLabel(root, text="Encrypt and chat", font=("Segoe UI Variable", 25))
l1.place(relx=0.5,rely=0.1,anchor=tkinter.CENTER)
tabview = ct.CTkTabview(root, width=800)
tabview.pack(padx=20, pady=60)
showinfo("How to use?", """
Instructions on how to use the program:\n
Key is copied to your clipboard after you either ask for a new key or type the new key into dialog box.\n
To use the program. share the program with your friends and ask them to download and set it up. once thats done, run the program\n
this time when the dialog asks if u require a new key. select "yes" and then the new key will be copied to your clipboard.\n
share the new key with your friends over a secure channel\n
Once the deired recipient recieves the key, they have to run the program and select "no" at the prompt asking you "if u require a new key".\n
once they select "no" they will have to type in the key sent by you and finally run the program by clicking "ok".
 and then use the following steps to encrypt or decrypt.\n
to encrypt: select and copy (ctrl+c) the text to encrypt and press ctrl+e to encrypt.. press ctrl+V to paste the encrypted data\n
to decrypt: select and paste (ctrl+c) the text to decrypt and press ctrl+d to decrypt.. then press ctrl+v to see the decrypted text\n
decrypted text also appears on the notification shown after decyption\n
NEW!: You can now add new conversations by presing the new tab button""")
def keyret():
    keystr = ""
    answer = askyesno("new key", "do you require a new key?")
    if answer:
        key = Fernet.generate_key()
    else:
        while keystr is None or keystr == "":
            keydial = ct.CTkInputDialog(text="Type in your key:", title="Enter Key")
            keystr = keydial.get_input()
            if keystr == "" or keystr is None:
                showerror("No key entered!", "Please enter an appropriate key!")
        else:
            key = keystr.decode("utf-8")
    pyperclip.copy(str(key.decode('utf-8')))
    n=win10toast.ToastNotifier()
    n.show_toast(title="key copied", msg="key copied to clipboard!", duration = 3, threaded=True)
    return key
def changekeys():
    n_key = keyret()
    conversations[tabview.get()][0] = n_key
    conversations[tabview.get()][1].key = n_key
def checkbox():
    global current_value
    current_value = 1 if current_value == 0 else 0
    print(current_value)
def enc_handler():
    if current_value == 0: conversations[tabview.get()][1].encrypt()
    else: conversations[tabview.get()][1].files_enc()
def dec_handler():
    if current_value == 0: conversations[tabview.get()][1].decrypt()
    else: conversations[tabview.get()][1].files_dec()
def ctab():
    if len(conversations) == 1:
        quit()
    else:
        conversations.pop(tabview.get())
        tabview.delete(tabview.get())
def ntab():
    conv = ct.CTkInputDialog(title="Conversation name", text="Enter a name for this conversation: ")
    conv_ret = conv.get_input()
    conv_name = conv_ret if not conv_ret == '' else f"Untitled conv {len(conversations)}"
    tabview.add(conv_name)
    kb = keyret()
    l1 = ct.CTkLabel(tabview.tab(conv_name), text="Data to Encrypt")
    l1.place(relx=0.5,rely=0.1,anchor=tkinter.CENTER)
    enc = ct.CTkEntry(master=tabview.tab(conv_name),
                               placeholder_text="Data to Encrypt",
                               width=240,
                               height=25,
                               border_width=2,
                               corner_radius=10)
    enc.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)
    l2 = ct.CTkLabel(tabview.tab(conv_name), text="Data to Decrypt")
    l2.place(relx=0.5,rely=0.5,anchor=tkinter.CENTER)
    dec = ct.CTkEntry(master=tabview.tab(conv_name),
                               placeholder_text="Data to Decrypt",
                               width=240,
                               height=25,
                               border_width=2,
                               corner_radius=10)
    dec.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)
    conversations[conv_name] = (kb, EncryptionSoftware(kb, enc, dec))
    button_1 = ct.CTkButton(tabview.tab(conv_name), text="New conversation", command=ntab)
    button_1.place(relx=0.9, rely=0.9, anchor=tkinter.CENTER)
    check1 = ct.CTkCheckBox(master=tabview.tab(conv_name), text="File", onvalue=1, offvalue=0, command=checkbox)
    check1.place(relx=0.1, rely=0.9, anchor=tkinter.CENTER)
    print(check1.get())
    button_2 = ct.CTkButton(tabview.tab(conv_name), text="Decrypt", command=dec_handler)
    button_2.place(relx=0.7, rely=0.9, anchor=tkinter.CENTER)
    button_3 = ct.CTkButton(tabview.tab(conv_name), text="Encrypt", command=enc_handler)
    button_3.place(relx=0.5, rely=0.9, anchor=tkinter.CENTER)
    button_4 = ct.CTkButton(tabview.tab(conv_name), text="Regenerate key", command=changekeys)
    button_4.place(relx=0.3, rely=0.9, anchor=tkinter.CENTER)
    button_5 = ct.CTkButton(tabview.tab(conv_name), text="Close Chat", command=ctab)
    button_5.place(relx=0.9, rely=0.1, anchor=tkinter.CENTER)
    button_6 = ct.CTkButton(tabview.tab(conv_name), text="Copy Key", command=lambda: pyperclip.copy(str(kb.decode('utf-8'))))
    button_6.place(relx=0.1, rely=0.1, anchor=tkinter.CENTER)
ntab()
keyboard.add_hotkey("ctrl+e", conversations[tabview.get()][1].encrypt, args=(True, ))
keyboard.add_hotkey("ctrl+d", conversations[tabview.get()][1].decrypt, args=(True, ))
keyboard.add_hotkey("ctrl+shift+e", conversations[tabview.get()][1].files_enc)
keyboard.add_hotkey("ctrl+shift+d", conversations[tabview.get()][1].files_dec)
keyboard.add_hotkey("ctrl+n+c", ntab)
keyboard.add_hotkey("ctrl+s+c", ctab)
root.mainloop()
#  TODO: once we make RE encryption algo, change this algo to that


_MAP = {
    "y": True,
    "yes": True,
    "t": True,
    "true": True,
    "on": True,
    "1": True,
    "n": False,
    "no": False,
    "f": False,
    "false": False,
    "off": False,
    "0": False,
}

def strtobool(value) -> bool:
    """Converts string to bool"""
    try:
        return _MAP[str(value).lower()]
    except KeyError as e:
        raise ValueError(f'"{value}" is not a valid bool value') from e


import hashlib as hasher
subsidy = 50

class Transaction:
    def __init__(self, tid, vin, vout):
        self.ID = tid
        self.Vin = vin
        self.Vout = vout

    def SetID(self):
        sha = hasher.sha256()
        sha.update((str(self.ID) + str(self.Vin) + str(self.Vout)).encode('utf-8'))
        self.ID = sha.hexdigest()

    def is_coinbase(self):
        return len(self.Vin) == 1 and self.Vin[0].Txid == 0 and self.Vin[0].Vout == -1

class TXInput:
    def __init__(self, txid, vout, scriptSig):
        self.Txid = txid
        self.Vout = vout
        self.ScriptSig = scriptSig

    # CanUnlockOutputWith checks whether the address initiated the transaction
    def CanUnlockOutputWith(self, unlockingData):
        return self.ScriptSig == unlockingData

class TXOutput:
    def __init__(self, value, scriptPubKey):
        self.Value = value
        self.ScriptPubKey = scriptPubKey

    # CanBeUnlockedWith checks if the output can be unlocked with the provided data
    def CanBeUnlockedWith(self, unlockingData):
        return self.ScriptPubKey == unlockingData

# NewCoinbaseTX creates a new coinbase transaction
def NewCoinbaseTX(to, data=""):
    if data == "":
        data = "Reward to " + to

    txin = TXInput(0, -1, data)
    txout = TXOutput(subsidy, to)
    first_transaction = Transaction(0, [txin], [txout])
    first_transaction.SetID()

    return first_transaction

# NewUTXOTransaction creates a new transaction
def NewUTXOTransaction(sender, to, amount, blockchain):
    inputs = []
    outputs = []
    acc, validOutputs = blockchain.FindSpendableOutputs(sender, amount)

    if acc < amount:
        print("ERROR: Not enough funds")
        return None

    # Build a list of inputs
    for txid, outs in validOutputs.items():
        for out in outs:
            inputs.append(TXInput(txid, out, sender))

    # Build a list of outputs
    outputs.append(TXOutput(amount, to))

    if acc > amount:
        outputs.append(TXOutput(acc - amount, sender))

    tx = Transaction(None, inputs, outputs)
    tx.SetID()
    return tx


#
# (c) 2020 Yoichi Tanibayashi
#
"""
Music Box Websocket Server

### Architecture (server side)

         --------------------------------------------
This -->|                  WsServer                  |
        |-------------------------------+------------|
        |            Player             |            |
        |-------------------------------|            |
        |           Movement            |            |
        |-------------------------------| websockets |
        |     Servo     | RotationMotor |            |
        |---------------+---------------|            |
        | ServoPCA9685  |  StepMtrTh    |            |
        |---------------+---------------|            |
        | pigpioPCA9685 |   StepMtr     |            |
         --------------------------------------------

"""
__author__ = 'Yoichi Tanibayashi'
__date__ = '2021/01'

import json
import asyncio
import websockets
from . import Player
from .my_logger import get_logger

class WsServer:
    """ Music Box websocket server

    Message format (JSON)
    ---------------------
    {"cmd": "single_play", "ch": [0,2,4]}  # single play

    {"cmd": "music_load",                 # load music and play
     "music_data": [ {"ch": null,"delay": 500},.. ]

    {"cmd": "music_play"}                 # (re)start music
    {"cmd": "music_stop"}
    {"cmd": "music_pause"}
    {"cmd": "music_seek", "pos": 30.5}
    {"cmd": "music_rewind"}

    {"cmd": "calibrate",                # change servo param
     "ch": 5,
     "on": true,  # on:ture, off: false
     "pw_diff": -10,
     "tap": ture }

    Simple client example(1)
    ---------------------------------------
    ```python3
    from musicbox import WsClientHostPort

    msg = {'cmd': ...}

    client = WsClientHostPort('localhost', 8880)
    client.send(msg)
    ```

    [FYI]
    Simple websocket client example(2)
    ----------------------------------
    ```python3
    # [Important!] 'websocket' is not 'websockets'

    import json
    from websocket import create_connection

    msg = {'cmd': ...}
    msg_json = json.dumps(msg)

    ws = create_connection('ws://localhost:8880/')
    ws.send(msg_json)
    ws.close()
    ```

    """
    DEF_PORT = 8880

    def __init__(self,
                 wav_mode=Player.WAVMODE_NONE,
                 host="0.0.0.0", port=DEF_PORT,
                 wavdir='wav',
                 debug=False):
        """ Constructor

        Parameters
        ----------
        wav_mode: int
            wav file mode
        host: str
            host
        port: int
            port number
        wavdir: str
            wav file directory
        """
        self._dbg = debug
        self._log = get_logger(self.__class__.__name__, self._dbg)
        self._log.debug('wav_mode=%s', wav_mode)
        self._log.info('host:port=%s:%s', host, port)
        self._log.debug('wavdir=%s', wavdir)

        self._wav_mode = wav_mode
        self._port = port
        self._host = host
        self._wavdir = wavdir

        self._player = Player(wav_mode=self._wav_mode,
                              wavdir=self._wavdir, debug=self._dbg)

        self._start_svr = websockets.serve(self.handle, host, port)
        self._loop = asyncio.get_event_loop()

    def main(self):
        """ main
        """
        self._log.debug('')

        self._log.debug('start server ..')
        self._loop.run_until_complete(self._start_svr)

        self._log.info('run_forever() ..')
        self._loop.run_forever()

    def end(self):
        """ Call at the end of program
        """
        self._log.debug('doing ..')

        self._player.end()

        self._log.debug('done')

    async def handle(self, websock, path):
        """
        request handler

        Parameters
        ----------
        websock: dict
        path: str
        """
        self._log.debug('websock=%s:%s, path=%s',
                        websock.local_address, websock.host, path)

        msg = await websock.recv()
        self._log.info('msg=%s', msg)

        try:
            data = json.loads(msg)
            self._log.debug('data=%s', data)
        except json.decoder.JSONDecodeError as ex:
            self._log.error('%s: %s. msg=%s', type(ex), ex, msg)
            return

        self._log.debug('received command: %a', data['cmd'])
        self._log.debug('data=%s', data)

        try:
            cmd = data['cmd']
        except KeyError as ex:
            self._log.error('%s: %s. data=%s', type(ex), ex, data)
            return

        if cmd in ('single_play', 'single', 'play', 'P'):
            try:
                ch_list = data['ch']
            except KeyError as ex:
                self._log.error('%s: %s. data=%s', type(ex), ex, data)
                return

            self._player.single_play(ch_list)
            return

        if cmd in ('music_load', 'music', 'load', 'l'):
            try:
                music_data = data['music_data']
            except KeyError as ex:
                self._log.error('%s: %s. data=%s', type(ex), ex, data)
                return

            self._player.music_load(music_data)
            return

        if cmd in ('music_play', 'start', 's'):
            self._player.music_play()
            return

        if cmd in ('music_pause', 'pause', 'p'):
            self._player.music_pause()
            return

        if cmd in ('music_rewind', 'rewind', 'r'):
            self._player.music_rewind()
            return

        if cmd in ('music_seek', 'seek'):
            self._player.music_seek_percent(data['pos'])
            return

        if cmd in ('music_shift', 'shift'):
            self._player.music_shift_percent(data['pos'])
            print('AAA')
            return

        if cmd in ('music_stop', 'stop', 'S'):
            self._player.music_stop()
            return

        if cmd in ('music_wait', 'wait', 'w'):
            self._player.music_wait()
            return

        if cmd in ('calibrate',):
            try:
                ch = int(data['ch'])
                on = data['on']
                pw_diff = data['pw_diff']
                tap = data['tap']
            except KeyError as ex:
                self._log.error('%s: %s. data=%s', type(ex), ex, data)

            self._player.calibrate(ch, on, pw_diff, tap)
            return


from ipAddress import IpAddress
from ipAnalyzer import IpAnalyzer

file = open('ipAddresses.txt', 'r')
ipAddresses = []
print('\nReading IPv4 addresses from text file...')
for line in file:
    if (line is not None and line not in ('', '\n')):
        try:
            ipAddresses.append(IpAddress(str.replace(line, '\n', '')))
        except Exception as error:
            print(error)
file.close()
print('\nFollowing IPv4 addresses were read:')
for ipAddress in ipAddresses:
    print("- %s (%s)" % (ipAddress.description(), ipAddress.descriptionByte()))

if (len(ipAddresses) > 0):
    analyzer = IpAnalyzer(ipAddresses)
    print('\nSubnet is: ' + analyzer.getSubnet().description())
else:
    print('-- no correct IPv4 addresses --')


import sys
input = sys.stdin.readline

def prime(n):
    if n == 0 or n == 1:
        return False
    if n == 2:
        return True
    tmp = 0
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            tmp += 1
            break
    if tmp == 0:
        return True
    return False

n = int(input())

for _ in range(n):
    num = int(input())
    while True:
        if prime(num):
            print(num)
            break
        else:
            num += 1


n = int(input())
flights = eval(input())
src = int(input())
dst = int(input())
K = int(input())

dist = [[float('inf')] * n for _ in range(2)]
dist[0][src] = dist[1][src] = 0

for i in range(K+1):
    for u, v, w in flights:
        dist[i&1][v] = min(dist[i&1][v], dist[~i&1][u] + w)

if dist[K&1][dst] < float('inf'):
    print(dist[K&1][dst] )
else:
    print(-1)


import argparse
import os, sys
import os.path as osp
import torchvision
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
import network, loss
from torch.utils.data import DataLoader
from data_list import ImageList, ImageList_idx
import random, pdb, math, copy
from sklearn.metrics import confusion_matrix
import torch.nn.functional as F
import tqdm

def op_copy(optimizer):
    for param_group in optimizer.param_groups:
        param_group['lr0'] = param_group['lr']
    return optimizer

def lr_scheduler(optimizer, iter_num, max_iter, gamma=10, power=0.75):
    decay = (1 + gamma * iter_num / max_iter)**(-power)
    for param_group in optimizer.param_groups:
        param_group['lr'] = param_group['lr0'] * decay
        param_group['weight_decay'] = 1e-3
        param_group['momentum'] = 0.9
        param_group['nesterov'] = True
    return optimizer

def image_train(resize_size=256, crop_size=224, alexnet=False):
    if not alexnet:
        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])
    else:
        normalize = Normalize(meanfile='./ilsvrc_2012_mean.npy')
    return transforms.Compose([
        transforms.Resize((resize_size, resize_size)),
        transforms.RandomCrop(crop_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(), normalize
    ])

def image_test(resize_size=256, crop_size=224, alexnet=False):
    if not alexnet:
        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])
    else:
        normalize = Normalize(meanfile='./ilsvrc_2012_mean.npy')
    return transforms.Compose([
        transforms.Resize((resize_size, resize_size)),
        transforms.CenterCrop(crop_size),
        transforms.ToTensor(), normalize
    ])

def data_load(args):
    ## prepare data
    dsets = {}
    dset_loaders = {}
    train_bs = args.batch_size
    txt_src = open(args.s_dset_path).readlines()
    txt_tar = open(args.t_dset_path).readlines()
    txt_test = open(args.test_dset_path).readlines()

    dsize = len(txt_src)
    tr_size = int(0.9 * dsize)
    # print(dsize, tr_size, dsize - tr_size)
    _, te_txt = torch.utils.data.random_split(txt_src,
                                              [tr_size, dsize - tr_size])
    tr_txt = txt_src

    dsets["source_tr"] = ImageList(tr_txt, transform=image_train())
    dset_loaders["source_tr"] = DataLoader(dsets["source_tr"],
                                           batch_size=train_bs,
                                           shuffle=True,
                                           num_workers=args.worker,
                                           drop_last=False)
    dsets["source_te"] = ImageList(te_txt, transform=image_test())
    dset_loaders["source_te"] = DataLoader(dsets["source_te"],
                                           batch_size=train_bs,
                                           shuffle=True,
                                           num_workers=args.worker,
                                           drop_last=False)
    dsets["target"] = ImageList_idx(txt_tar, transform=image_train())
    dset_loaders["target"] = DataLoader(dsets["target"],
                                        batch_size=train_bs,
                                        shuffle=True,
                                        num_workers=args.worker,
                                        drop_last=False)
    dsets["test"] = ImageList_idx(txt_test, transform=image_test())
    dset_loaders["test"] = DataLoader(dsets["test"],
                                      batch_size=train_bs * 3,
                                      shuffle=False,
                                      num_workers=args.worker,
                                      drop_last=False)

    return dset_loaders

def cal_acc(loader, fea_bank, socre_bank, netF, netB, netC,args, flag=False):
    start_test = True
    num_sample = len(loader.dataset)
    label_bank = torch.randn(num_sample)  #.cuda()
    pred_bank = torch.randn(num_sample)
    nu=[]
    s=[]
    var_all=[]

    with torch.no_grad():
        iter_test = iter(loader)
        for i in range(len(loader)):
            data = iter_test.next()
            inputs = data[0]
            labels = data[1]
            indx = data[-1]
            inputs = inputs.cuda()
            fea = netF(inputs)
            fea = netB(fea)
            if args.var:
                var_batch=fea.var()
                var_all.append(var_batch)

            #if args.singular:
            _, ss, _ = torch.svd(fea)
            s10=ss[:10]/ss[0]
            s.append(s10)

            outputs = netC(fea)
            softmax_out=nn.Softmax()(outputs)
            nu.append(torch.mean(torch.svd(softmax_out)[1]))
            label_bank[indx] = labels.float().detach().clone()  #.cpu()
            pred_bank[indx] = outputs.max(-1)[1].float().detach().clone().cpu()
            if start_test:
                all_output = outputs.float().cpu()
                all_label = labels.float()
                start_test = False
            else:
                all_output = torch.cat((all_output, outputs.float().cpu()), 0)
                all_label = torch.cat((all_label, labels.float()), 0)
    _, predict = torch.max(all_output, 1)
    accuracy = torch.sum(
        torch.squeeze(predict).float() == all_label).item() / float(
            all_label.size()[0])

    _, socre_bank_ = torch.max(socre_bank, 1)

    import time

    splits = 4
    split_idx = num_sample//splits
    idx_near = torch.zeros(num_sample, 4, dtype=torch.long)
    tic = time.time()
    for i in range(splits):
        distance = fea_bank[split_idx*i:min(len(fea_bank), split_idx*(i+1))].cpu() @ fea_bank.cpu().T
        # print(distance.shape)
        _, idx_near_split = torch.topk(distance, dim=-1, largest=True, k=4)
        distance = []
        idx_near[split_idx*i:min(len(fea_bank), split_idx*(i+1))] = idx_near_split
    print('Time consumed:', time.time() - tic)

    score_near = socre_bank_[idx_near[:, :]].float().cpu()  #N x 4

    '''acc1 = (score_near.mean(
        dim=-1) == score_near[:, 0]).sum().float() / score_near.shape[0]'''
    acc1 = ((score_near.mean(dim=-1) == score_near[:, 0]) &
            (score_near[:, 0] == pred_bank)).sum().float() / score_near.shape[0]
    acc2 = (
        (score_near.mean(dim=-1) == score_near[:, 0]) &
        (score_near[:, 0] == label_bank)).sum().float() / score_near.shape[0]

    if True:
        matrix = confusion_matrix(all_label, torch.squeeze(predict).float())
        acc = matrix.diagonal() / matrix.sum(axis=1) * 100
        aacc = acc.mean()
        aa = [str(np.round(i, 2)) for i in acc]
        acc = ' '.join(aa)
        if True:
            return aacc, acc, acc1, acc2

    else:
        return accuracy * 100, mean_ent

def hyper_decay(x,beta=-2,alpha=1):
    weight=(1 + 10 * x)**(-beta) * alpha
    return weight

def SKL(out1, out2):
    out2_t = out2.clone()
    out2_t = out2_t.detach()
    out1_t = out1.clone()
    out1_t = out1_t.detach()
    return (F.kl_div(F.log_softmax(out1), out2_t, reduction='none') +
            F.kl_div(F.log_softmax(out2), out1_t, reduction='none')) / 2

def Entropy(input_):
    bs = input_.size(0)
    epsilon = 1e-5
    entropy = -input_ * torch.log(input_ + epsilon)
    entropy = torch.sum(entropy, dim=-1)
    return entropy

def get_entropy_loss(p_softmax):
    mask = p_softmax.ge(0.000001)
    mask_out = torch.masked_select(p_softmax, mask)
    entropy = 0
    for i in range(p_softmax.shape[0]):
        entropy += -(torch.sum(mask_out[i] * torch.log(mask_out[i])))
    return entropy / float(p_softmax.size(0))

class FocalLoss(nn.Module):
    def __init__(self, alpha=1, gamma=2):
        super(FocalLoss, self).__init__()
        self.alpha = alpha
        self.gamma = gamma

    def forward(self, inputs, targets):
        inputs = nn.Softmax(dim=1)(inputs)
        bce_loss = F.binary_cross_entropy(inputs.squeeze(),  targets.float())
        loss = self.alpha * (1 - torch.exp(-bce_loss)) ** self.gamma * bce_loss
        return loss

def train_target(args):
    # criterion = nn.BCEWithLogitsLoss()
    criterion = nn.CrossEntropyLoss()
    dset_loaders = data_load(args)
    ## set base network
    netF = network.ResBase(res_name='resnet101').cuda()

    netB = network.feat_bootleneck(type=args.classifier,
                                   feature_dim=2048,
                                   bottleneck_dim=args.bottleneck).cuda()
    netC = network.feat_classifier(type=args.layer,
                                   class_num=args.class_num,
                                   bottleneck_dim=args.bottleneck).cuda()

    netF_source = network.ResBase(res_name='resnet101').cuda()

    netB_source = network.feat_bootleneck(type=args.classifier,
                                   feature_dim=2048,
                                   bottleneck_dim=args.bottleneck).cuda()
    netC_source = network.feat_classifier(type=args.layer,
                                   class_num=args.class_num,
                                   bottleneck_dim=args.bottleneck).cuda()

    modelpath = args.output_dir_src + '/source_F.pt'
    netF.load_state_dict(torch.load(modelpath))
    netF_source.load_state_dict(torch.load(modelpath))
    modelpath = args.output_dir_src + '/source_B.pt'
    netB.load_state_dict(torch.load(modelpath))
    netB_source.load_state_dict(torch.load(modelpath))
    modelpath = args.output_dir_src + '/source_C.pt'
    netC.load_state_dict(torch.load(modelpath))
    netC_source.load_state_dict(torch.load(modelpath))

    param_group = []
    param_group_c = []
    for k, v in netF.named_parameters():
        if True:
            param_group += [{'params': v, 'lr': args.lr * 0.1}] #0.1

    for k, v in netB.named_parameters():
        if True:
            param_group += [{'params': v, 'lr': args.lr * 0.1}] # 1
    for k, v in netC.named_parameters():
        param_group_c += [{'params': v, 'lr': args.lr * 0.1}] #1

    optimizer = optim.SGD(param_group)
    optimizer = op_copy(optimizer)

    optimizer_c = optim.SGD(param_group_c)
    optimizer_c = op_copy(optimizer_c)

    #building feature bank and score bank
    loader = dset_loaders["target"]
    num_sample = len(loader.dataset)
    fea_bank = torch.randn(num_sample, args.fea_bank_dim) # can ca
    score_bank = torch.randn(num_sample, args.class_num).cuda()

    netF.eval()
    netB.eval()
    netC.eval()

    #initialize
    print("Initialize...")
    with torch.no_grad():
        iter_test = iter(loader)
        for i in tqdm.tqdm(range(len(loader))):
        # for i in range(10):
            data = iter_test.next()
            # print('what is in data:', data[0], data[-1])

            inputs = data[0]
            indx = data[-1]
            #labels = data[1]
            inputs = inputs.cuda()
            feature = netF(inputs)
            fea_norm = F.normalize(feature)
            logits = netB(feature)
            logits = netC(logits)
            outputs = nn.Softmax(-1)(logits)
            fea_bank[indx] = fea_norm.detach().clone().cpu()
            score_bank[indx] = outputs.detach().clone()  #.cpu()

    max_iter = args.max_epoch * len(dset_loaders["target"])
    interval_iter = max_iter // args.interval
    iter_num = 0

    netF.train()
    netB.train()
    netC.train()
    netF_source.eval()
    netB_source.eval()
    netC_source.eval()
    acc_log = 0

    real_max_iter = max_iter

    print("Start training...")

    for iter_num in tqdm.tqdm(range(real_max_iter)):
        try:
            inputs_test, _, tar_idx = iter_test.next()
        except:
            iter_test = iter(dset_loaders["target"])
            inputs_test, _, tar_idx = iter_test.next()

        if inputs_test.size(0) == 1:
            continue

        inputs_test = inputs_test.cuda()
        if True:
            alpha = (1 + 10 * iter_num / max_iter)**(-args.beta) * args.alpha
        else:
            alpha = args.alpha

        iter_num += 1
        lr_scheduler(optimizer, iter_num=iter_num, max_iter=max_iter)
        lr_scheduler(optimizer_c, iter_num=iter_num, max_iter=max_iter)

        features_test = netF(inputs_test)
        output_f_norm = F.normalize(features_test)

        logits_test = netB(features_test)
        logits_test = netC(logits_test)
        softmax_out = nn.Softmax(dim=1)(logits_test)

        with torch.no_grad():
            output_f_ = output_f_norm.cpu().detach().clone()
            fea_bank[tar_idx] = output_f_.detach().clone().cpu()
            score_bank[tar_idx] = softmax_out.detach().clone()
            # logit_bank[tar_idx] = logits_test_norm.detach().clone()

        distance = output_f_ @ fea_bank.T # batch*num_sample

        _, idx_near = torch.topk(distance,
                                     dim=-1,
                                     largest=True,
                                     k= args.K + 1)
        idx_near = idx_near[:, 1:]  #batch x K

        score_near = score_bank[idx_near]  #batch x K x C

        score_near_mean = torch.mean(score_near)

        feature = torch.cat([softmax_out, score_near_mean], dim=0)
        labels = torch.cat([torch.arange(softmax_out.shape[0]).repeat_interleave(1) for i in range(2)], dim=0)
        labels = (labels.unsqueeze(0) == labels.unsqueeze(1)).float()
        labels = labels.cuda()
        similarity_matrix = feature@feature.T

        A = torch.ones(labels.shape[0],1,1, dtype=torch.bool)
        mask = torch.block_diag(*A).cuda()

        labels = labels[~mask].view(labels.shape[0], -1)

        similarity_matrix = similarity_matrix[~mask].view(similarity_matrix.shape[0], -1)

        positives = similarity_matrix[labels.bool()].view(labels.shape[0], -1)
        negatives = similarity_matrix[~labels.bool()].view(similarity_matrix.shape[0], -1)

        logits = torch.cat([positives, negatives], dim=1)

        labels = torch.zeros(logits.shape[0], dtype=torch.long).cuda()

        logits = logits / args.temperature

        loss = criterion(logits, labels)

        score_near_mean = torch.mean(score_near, 1)
        soft_score_loss = torch.mean(-torch.sum(score_near_mean *nn.LogSoftmax()(softmax_out), 1, keepdim=True))
        loss += soft_score_loss

        optimizer.zero_grad()
        optimizer_c.zero_grad()
        loss.backward()
        optimizer.step()
        optimizer_c.step()

        if iter_num % interval_iter == 0 or iter_num == max_iter:
            print("Calculate accuracy...")
            netF.eval()
            netB.eval()
            netC.eval()
            if args.dset == 'visda-2017':
                acc, accc, acc1_knn, acc2_knn = cal_acc(
                    dset_loaders['test'],
                    fea_bank,
                    score_bank,
                    netF,
                    netB,
                    netC,
                    args,
                    flag=True)
                log_str = 'Task: {}, Iter:{}/{}, Epoch:{}/{};  Acc on target: {:.2f}, percentage of shared label: {:.2f}, percentage of correct shared label: {:.2f}'.format( #var
                    args.name, iter_num, max_iter, iter_num//len(dset_loaders["target"]), args.max_epoch, acc, acc1_knn * 100,
                    acc2_knn * 100) + '\n' + 'T: ' + accc

            args.out_file.write(log_str + '\n')
            args.out_file.flush()
            print(log_str + '\n')
            netF.train()
            netB.train()
            netC.train()
            '''if acc>acc_log:
                acc_log = acc
                torch.save(
                    netF.state_dict(),
                    osp.join(args.output_dir, "target_F_" + '2021_'+str(args.tag) + ".pt"))
                torch.save(
                    netB.state_dict(),
                    osp.join(args.output_dir,
                                "target_B_" + '2021_' + str(args.tag) + ".pt"))
                torch.save(
                    netC.state_dict(),
                    osp.join(args.output_dir,
                                "target_C_" + '2021_' + str(args.tag) + ".pt"))'''

    return netF, netB, netC

def print_args(args):
    s = "==========================================\n"
    for arg, content in args.__dict__.items():
        s += "{}:{}\n".format(arg, content)
    return s

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='LPA')
    parser.add_argument('--gpu_id',
                        type=str,
                        nargs='?',
                        default='0',
                        help="device id to run")
    parser.add_argument('--s', type=int, default=0, help="source")
    parser.add_argument('--t', type=int, default=1, help="target")
    parser.add_argument('--max_epoch',
                        type=int,
                        default=10,
                        help="max iterations")
    parser.add_argument('--interval', type=int, default=150)
    parser.add_argument('--batch_size',
                        type=int,
                        default=64,
                        help="batch_size")
    parser.add_argument('--worker',
                        type=int,
                        default=0,
                        help="number of workers")
    parser.add_argument('--dset', type=str, default='visda-2017')
    parser.add_argument('--lr', type=float, default=1e-4, help="learning rate")
    parser.add_argument('--fea_bank_dim', type=float, default=2048, help="feature bank dimension")
    parser.add_argument('--net', type=str, default='resnet101')
    parser.add_argument('--seed', type=int, default=2021, help="random seed")

    parser.add_argument('--bottleneck', type=int, default=256)
    parser.add_argument('--K', type=int, default=5)
    parser.add_argument('--epsilon', type=float, default=1e-5)
    parser.add_argument('--layer',
                        type=str,
                        default="wn",
                        choices=["linear", "wn"])
    parser.add_argument('--classifier',
                        type=str,
                        default="bn",
                        choices=["ori", "bn"])
    parser.add_argument('--output', type=str, default='weight/target/')
    parser.add_argument('--output_src', type=str, default='weight/source/')
    parser.add_argument('--tag', type=str, default='AAD')
    parser.add_argument('--da', type=str, default='uda')
    parser.add_argument('--issave', type=bool, default=True)
    parser.add_argument('--cc', default=False, action='store_true')
    parser.add_argument('--alpha', type=float, default=1.0)
    parser.add_argument('--beta', type=float, default=5.0)
    parser.add_argument('--alpha_decay', default=True)
    parser.add_argument('--nuclear', default=False, action='store_true')
    parser.add_argument('--var', default=False, action='store_true')
    parser.add_argument('--temperature', default=0.07, type=float,
                        help='softmax temperature (default: 0.07)')
    parser.add_argument('--dataset_folder', type=str, default="C:/Users/wang0918.stu/Desktop/Datasets")
    args = parser.parse_args()

    if args.dset == 'office-home':
        names = ['Art', 'Clipart', 'Product', 'RealWorld']
        args.class_num = 65
    if args.dset == 'visda-2017':
        names = ['train', 'validation']
        args.class_num = 12

    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
    SEED = args.seed
    torch.manual_seed(SEED)
    torch.cuda.manual_seed(SEED)
    np.random.seed(SEED)
    random.seed(SEED)
    torch.backends.cudnn.deterministic = True

    for i in range(len(names)):
        if i == args.s:
            continue
        args.t = i

        folder = args.dataset_folder
        if 'jiaho' in folder:
            args.s_dset_path = folder + args.dset + '/' + names[
                args.s] + '_list_win.txt'
            args.t_dset_path = folder + args.dset + '/' + names[
                args.t] + '_list_win.txt'
            args.test_dset_path = folder + args.dset + '/' + names[
                args.t] + '_list_win.txt'
        else:
            args.s_dset_path = folder + '/' + args.dset + '/' + names[
                args.s] + '_list.txt'
            args.t_dset_path = folder + '/' + args.dset + '/' + names[
                args.t] + '_list.txt'
            args.test_dset_path = folder + '/' + args.dset + '/' + names[
                args.t] + '_list.txt'

        args.output_dir_src = osp.join(args.output_src, args.da, args.dset,
                                       names[args.s].upper())
        args.output_dir = osp.join(
            args.output, args.da, args.dset,
            names[args.s].upper() + '-' + names[args.t].upper())
        args.name = names[args.s].upper() + '-' + names[args.t].upper()

        if not osp.exists(args.output_dir):
            os.system('mkdir -p ' + args.output_dir)
        if not osp.exists(args.output_dir):
            os.mkdir(args.output_dir)

        args.out_file = open(osp.join(args.output_dir, 'log_concat5knn_{}_K_{}.txt'.format(args.tag, args.K)), 'w')
        args.out_file.write(print_args(args) + '\n')
        args.out_file.flush()
        train_target(args)


# Data Preprocessing Template

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
df=pd.read_csv("Social_Network_Ads.csv")
df.head()
X_data=df.iloc[:,:2].values
Y_data=df.iloc[:,-1:].values
print(X_data);
print(Y_data);
#Handling Missing Values
from sklearn.impute import SimpleImputer
SI=SimpleImputer(missing_values=np.nan,strategy="mean")
SI.fit_transform(X_data)

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split

X_train,X_test,Y_train,Y_test=train_test_split(X_data,Y_data,test_size=0.25,random_state=None)
print(X_test)
print

#Standerdisation and Normalization
from sklearn.preprocessing import StandardScaler

#we will fit the standerd scaler object with Xtrain set because it can create information leakage if we
#Normelize  the data befoe scaling ; after fitting the scaler object we will use that object instant to transform
# Test set or in future for predicting the target lable out of a feature set
#we use Normelization when the data is Normally Distributed
#otherwise Standerization

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test= sc.transform(X_test)
print(X_train)
print(X_test)

#####################

# Building Logistic regression MOdel

#####################

from sklearn.linear_model import LogisticRegression
classifier=LogisticRegression(random_state=0)
classifier.fit(X_train,Y_train)

######
# Predicting the test result
######
Y_hat_test = classifier.predict(X_test)

print(np.concatenate((Y_hat_test.reshape(len(Y_hat_test),1),Y_test.reshape(len(Y_test),1)),1))

####
## CONFUSION METRIX
####

##confusion metrix will tel us about the fasle positive
## False Negetive ,True Positive and True negetive
from sklearn.metrics import confusion_matrix
CM=confusion_matrix(Y_test,Y_hat_test)
print(CM)
###
###CALCULATING THE ACCURACY OF OUR MODEL

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(Y_test,Y_hat_test)

print(accuracy)

##########
# Visualizing the TRaining Set Result
##########


import warnings

import numpy as np
import scipy.signal as signal
import torch
from scipy.ndimage.filters import gaussian_filter1d
import time

class GAUS1DFilter:
    """
    Args:
        x (np.ndarray): input pose
        window_size (int, optional): for median filters (must be odd).
        sigma (float, optional): Sigma for gaussian smoothing.

    Returns:
        np.ndarray: Smoothed poses
    """

    def __init__(self, cfg):
        super(GAUS1DFilter, self).__init__()

        self.window_size = cfg.EVALUATE.TRADITION_GAUS1D.WINDOW_SIZE
        self.sigma = cfg.EVALUATE.TRADITION_GAUS1D.SIGMA

    def __call__(self, x=None):
        if self.window_size % 2 == 0:
            window_size = self.window_size - 1
        else:
            window_size = self.window_size
        if window_size > x.shape[0]:
            window_size = x.shape[0]
        if len(x.shape) != 3:
            warnings.warn('x should be a tensor or numpy of [T*M,K,C]')
        assert len(x.shape) == 3
        x_type = x
        if isinstance(x, torch.Tensor):
            if x.is_cuda:
                x = x.cpu().numpy()
            else:
                x = x.numpy()

        T,K,C=x.shape
        x=x.reshape(T,K*C)

        smooth_poses=np.empty((0,K*C))
        start = time.time()

        for i in range(T//window_size):
            smooth_poses=np.concatenate((smooth_poses,gaussian_filter1d(input=x[i*window_size:(i+1)*window_size,:],
                                        sigma=self.sigma,
                                        axis=0)),0)
        smooth_poses=np.concatenate((smooth_poses,x[(T//window_size)*window_size:,:]),0)
        end = time.time()
        inference_time=end-start
        smooth_poses=smooth_poses.reshape(T,K,C)

        if isinstance(x_type, torch.Tensor):
            # we also return tensor by default
            if x_type.is_cuda:
                smooth_poses = torch.from_numpy(smooth_poses).cuda()
            else:
                smooth_poses = torch.from_numpy(smooth_poses)

        return smooth_poses,inference_time


import pandas as pd
import numpy as np
from sklearn.metrics import pairwise_distances

from .train import Cluster

'''
Save n_components in pca object and get rid of get_pca_ncomponents
Add tests:
    ???
Ensure any df going to predict goes through preprocess
'''

class Inference(Cluster):
    def __init__(self, pd_rules, index_cols=None, \
                 categorical_cols=None, drop_cols=None,
                 models_dict=None, n_processes=None):

        super().__init__(pd_rules, index_cols=index_cols, categorical_cols=categorical_cols, drop_cols=drop_cols)
        self.n_processes = n_processes

        super().preprocess()

        self.models_dict = models_dict
        self.load_models()

    def load_models(self):
        if self.models_dict is None:
            raise AttributeError("Please specify pre-trained models to load")

        self.prepca_scaler = self.models_dict['prepca_scaler']
        self.pca = self.models_dict['pca']
        self.postpca_scaler = self.models_dict['postpca_scaler']
        self.kmeans_optimal = self.models_dict['kmeans']

    def preprocess_prepare_for_clustering(self):
        df_prepca_scaled = self.prepca_scaler.transform(self.data)

        df_pca = pd.DataFrame(self.pca.transform(df_prepca_scaled)).iloc[:,:self.pca.n_components]

        df_postpca_scaled = self.postpca_scaler.transform(df_pca)

        self.data_for_kmeans = df_postpca_scaled

    def predict(self):
        if self.data_for_kmeans is None:
            self.preprocess_prepare_for_clustering()

        cl = self.kmeans_optimal.predict(self.data_for_kmeans)

        # Conversions required to support JSON serialization:
        # Index just by one level of index
        # Convert using numpy.narray.tolist(), to ensure Python native types
        index = self.data.index.get_level_values(0).tolist()
        cl = cl.tolist()
        cl_dict = dict(zip(index, cl))

        return cl_dict

    def predict_proba(self, type='inverse'):
        if self.data_for_kmeans is None:
            self.preprocess_prepare_for_clustering()

        if type!='inverse' and type!='exponential':
            raise ValueError("type should be 'inverse' or 'exponential'")

        #compute pairwise distances
        pair_dist = pairwise_distances(self.data_for_kmeans, self.kmeans_optimal.cluster_centers_, metric='euclidean', n_jobs=self.n_processes)
        pair_dist = pair_dist.astype(float)

        #compute probabilities
        if type=='inverse':
            if np.sum(pair_dist==0)>0:
                pair_dist[pair_dist==0] = 1e-10

            probs = np.apply_along_axis(lambda x: x/np.sum(x), 1, 1./pair_dist)
        elif type=='exponential':
            probs = np.apply_along_axis(lambda x: x/np.sum(x), 1, np.exp(-pair_dist))

        #check all probabilities add up to 1
        assert(int(probs.sum())==probs.shape[0])

        #check max prob cluster is same as prediction from self.predict
        argmax = np.apply_along_axis(np.argmax, 1, probs)
        cl = self.kmeans_optimal.predict(self.data_for_kmeans)
        assert(np.sum(argmax==cl)==probs.shape[0])

        #some munging to create output dict
        prob_list = np.apply_along_axis(lambda x: dict(zip(np.arange(self.kmeans_optimal.n_clusters), x)), 1, probs)
        results = dict(zip(self.data.index, prob_list))

        return results


"""
Use a 2d bool list to prevent recalculation
"""
def isMatch(self, s, p):
  mark = []
  for i in range(len(s) + 1): mark += [[False] * (len(p) + 1)]

  def helper(i, j, mark):
    if(i == len(s) and j == len(p)): return True
    if(i > len(s) or j >= len(p)): return False
    if(mark[i][j]): return False
    mark[i][j] = True
    head = i < len(s) and p[j] in {s[i], '.'}
    if(j + 1 < len(p) and p[j + 1] == '*'):
      return helper(i, j + 2, mark) or (head and helper(i + 1, j, mark))
    else:
      return head and helper(i + 1, j + 1, mark)

  return helper(0, 0, mark)

10. Regular Expression Matching/10-mark-recursion.py


list1 = input().split(",")
list1.insert(1, list1[0][1:])
list1.remove(list1[0])
list1.insert(-1, list1[-1][0:-1])
list1.remove(list1[-1])
for i in range(len(list1)):
    list1[i] = int(list1[i])
list1.sort()
max = 0
if len(list1) <= 1:
    max = 0
else:
    j = 0
    while j < len(list1) - 1:
        if list1[j + 1] - list1[j] > max:
            max = list1[j + 1] - list1[j]
        j += 1
print(max)


"""Solution to day 01 of the Advent of Code.

Reading input:
    Each line in the input file can be read as an integer. Using Python's int
    function parses positive and negative numbers equally well, so we will use
    that. See _read_changes function.

Changing frequency:
    Applying a change to an existing frequency as simple as adding the two
    integers that represent the frequency and the frequency change. See
    _apply_change function.

Part 1:
    Getting the final frequency means summing up all the frequency changes in
    our input. We could do this in a single line of Python (yay for list
    comprehensions!) but we'll use our _apply_change function instead. See
    get_final_frequency function.

Part 2:
    To find out which frequency appears twice first, we keep track of all
    frequencies we've seen in a set (not a list, because that would be much
    slower!). See get_first_repetition function.
"""

from typing import List

import utils

def _read_changes(input_string: str) -> List[int]:
    """Reads frequency changes from a given input string.

    Args:
        input_string: A string containing the day's input.

    Returns:
        A list integers representing changes.
    """
    return [int(line) for line in input_string.split('\n')]

def _apply_change(frequency: int, change: int) -> int:
    """Applies a change to a frequency.

    Args:
        frequency: A integer representation of the frequency to change.
        change: An integer representing the change to apply.

    Returns:
        An integer representing the new frequency.
    """
    return frequency + change

def get_final_frequency(input_string: str) -> int:
    """Computes the final frequency given an input string.

    Args:
        input_string: The puzzle input.

    Returns:
        The final frequency after applying all changes.
    """
    changes = _read_changes(input_string)
    frequency = 0
    for change in changes:
        frequency = _apply_change(frequency, change)
    return frequency

def get_first_repetition(input_string: str) -> int:
    """Finds the first repetition given an input string.

    Args:
        input_string: The puzzle input.

    Returns:
        The first frequency to appear twice.
    """
    changes = _read_changes(input_string)
    frequency = 0
    seen_frequencies = {frequency}
    while True:
        for change in changes:
            frequency = _apply_change(frequency, change)
            if frequency in seen_frequencies:
                return frequency
            seen_frequencies.add(frequency)

def _run_tests() -> None:
    """Tests solution."""
    assert get_final_frequency('+1\n+1\n+1') == 3
    assert get_final_frequency('+1\n+1\n-2') == 0
    assert get_final_frequency('-1\n-2\n-3') == -6
    assert get_first_repetition('+1\n-1') == 0
    assert get_first_repetition('+3\n+3\n+4\n-2\n-4') == 10
    assert get_first_repetition('-6\n+3\n+8\n+5\n-6') == 5
    assert get_first_repetition('+7\n+7\n-2\n-7\n-4') == 14

def _print_answers(final_frequency: int = None,
                   first_repetition: int = None) -> None:
    """Prints answers.

    Args:
        final_frequency: The answer to the first part of the day's puzzle.
        first_repetition: The answer to the second part of the day's puzzle.
    """
    print('Answers for day 01:')
    print(f'  Final frequency: {final_frequency}')
    print(f'  First repetition: {first_repetition}')

def main() -> None:
    """Runs tests and prints answers to day's puzzle."""
    _run_tests()
    input_string = utils.read_input(1)
    final_frequency = get_final_frequency(input_string)
    first_repetition = get_first_repetition(input_string)
    _print_answers(final_frequency, first_repetition)

if __name__ == '__main__':
    main()


import h5py
import sys
import os

# Simple tool to help you with getting a list of entries and channels
# you want to perform spike sorting on.

def main(filename):
    with h5py.File(filename) as f:
        entries = sorted(list(f.keys()))

        datasets = None
        for e in entries:
            try:
                entry = f[e]
                datasets = sorted(list(entry.keys()))
                if len(datasets) > 0:
                    break
            except:
                pass

        print('Entries in {0}:'.format(filename))
        print(entries)
        print('\n Datasets in {0}:'.format(entry))
        print(datasets)

if __name__ == '__main__':
    filename = sys.argv[1]
    if not os.path.isfile(filename):
        print("USAGE: listlist.py filename")
    else:
        main(filename)


import numpy as np
from tqdm import tqdm
import torch
from poutyne import Model, SKLearnMetrics
from dataset import unnormalize, normalize, pad_borders
from utils import metric_flatten
from models.Baseline import Baseline
# from models.PerfectGraph import PerfectGraph
from sklearn.metrics import r2_score, explained_variance_score, mean_squared_error, median_absolute_error
from typing import Optional, List, Tuple
import matplotlib.pyplot as plt
import pandas as pd
from utils import to_device_eval

from matplotlib import cm
from matplotlib.colors import Normalize
from scipy.interpolate import interpn

def density_scatter( x , y, ax = None, sort = True, bins = 20, **kwargs )   :
    """
    Scatter plot colored by 2d histogram
    """

    yhat = x[time_step][mask_cnn]
    test_targets = y[time_step][mask_cnn]

    if ax is None :
        fig , ax = plt.subplots(figsize=(8,5))
    data , x_e, y_e = np.histogram2d( x, y, bins = bins, density = True )
    z = interpn( ( 0.5*(x_e[1:] + x_e[:-1]) , 0.5*(y_e[1:]+y_e[:-1]) ) , data , np.vstack([x,y]).T , method = "splinef2d", bounds_error = False)

    #To be sure to plot all data
    z[np.where(np.isnan(z))] = 0.0

    # Sort the points by density, so that the densest points are plotted last
    if sort :
        idx = z.argsort()
        x, y, z = x[idx], y[idx], z[idx]

    ax.scatter( x, y, c=z, **kwargs )
    plt.xlim([0, 6])
    plt.ylim([0, 6])

    plt.xlabel("Predicted water depth (m)",fontsize=18)
    plt.ylabel("Real water depth (m)", fontsize=18)
    plt.title("Scatter plot errors", fontsize=20)
#     plt.plot([0, 6], [0, 6], 'black', label= 'diagonal')

    norm = Normalize(vmin = np.min(z), vmax = np.max(z))
    cbar = fig.colorbar(cm.ScalarMappable(norm = norm), ax=ax)
    cbar.ax.set_ylabel('Density')

    return ax

def predict_event_bay(model, dataset, event_num, arch, start_ts=None, ar = True, T = None, ts_out = 0):
    """
    Predict a full event.

    This function will split the prediction if `dataset.sample_type == "single"`.
    Alternatively, it will use the full frame to predict if `dataset.sample_type == "full"`.

    ar: auto regressively
    start_ts: start at timestep start_ts (if None starts at 0.)
    """

    patch_dim = dataset.nx
    # inds = dataset.get_all_fix_indexes()
    b = dataset.border_size
    timestep = dataset.timestep
    tsh = dataset.predict_ahead

    if start_ts is None:
        start_ts = timestep-1
    assert start_ts >= timestep-1
    # assert dataset.normalize_output == False
    if T is None:
        T = len(dataset.rainfall_events[event_num]) - start_ts - 1 - tsh

    index_t = start_ts - timestep + 1
    xin = dataset.waterdepth[event_num][index_t : index_t +timestep].clone()
    rainfall = dataset.rainfall_events[event_num][index_t : ].clone()
    dem = dataset.dem
    diff_dem = dataset.diff_dem
    mask = dataset.dem_mask
    recons_pred_full = torch.zeros(T, dataset.px - 2*b, dataset.py - 2*b)
    recons_pred_full_sigma = torch.zeros(T, dataset.px - 2*b, dataset.py - 2*b)

    for t in tqdm(range(T)):
        recons_pred_full[t], recons_pred_full_sigma[t] = predict_next_ts_bay(dataset, model, xin, rainfall[t:t+timestep+tsh], mask, dem, diff_dem, ts_out)
        recons_pred_full[t] = recons_pred_full[t] * mask
        recons_pred_full_sigma[t] = recons_pred_full_sigma[t] * mask

        if ar:
            if timestep>1:
                xin[:-1] = xin[1:].clone()
            xin[-1] = dataset.pad_borders(recons_pred_full[t].clone(), 0)
        else:
            xin = dataset.waterdepth[event_num][index_t+t+1: index_t+t+timestep+1].clone()

    if b:
        recons_gt_full = dataset.waterdepth[event_num][start_ts+tsh +1:start_ts+tsh+T +1, b : -b, b: -b].clone()
        recons_mask_full = mask[b : -b, b : -b ].clone()
    else:
        recons_gt_full = dataset.waterdepth[event_num][start_ts+tsh +1:start_ts+tsh+T +1, : , :].clone()
        recons_mask_full = mask.clone()

    return recons_pred_full.numpy(), recons_gt_full.numpy(), recons_mask_full.numpy(), recons_pred_full_sigma.numpy()

def predict_next_ts_bay(dataset, model, xin, rainfall, mask, dem, diff_dem, ts_out):

    """Predict the next timestep for unet arch. C x H x W
    C = channels, H = height, W = width
    """

    b = dataset.border_size
    normalize_output = dataset.normalize_output

    def create_inputs(dataset, x_p, y_p, xin, rainfall, mask, dem, diff_dem):
        xin, mask, dem, diff_dem, _ = dataset.crop_to_patch(x_p, y_p, xin, mask, dem, diff_dem)
        inputs = dataset.build_inputs(xin, rainfall, mask, dem, diff_dem)
        return inputs

    nx = dataset.nx
    ny = dataset.ny

    inds = dataset.get_all_fix_indexes(non_full=True)
    recons_pred = torch.zeros(dataset.px - 2*b, dataset.py - 2*b)
    recons_pred_sigma = torch.zeros(dataset.px - 2*b, dataset.py - 2*b)
    for inds_count, (x_p, y_p) in enumerate(inds):

        inputs_ts = create_inputs(dataset, x_p, y_p, xin, rainfall, mask, dem, diff_dem)

        # stef
        (data, mask1) = inputs_ts
        data = data.unsqueeze(dim=0)
        data = to_device_eval(data)
        y_pred = model(data)

        sigma = y_pred['sigma'].detach().cpu()
        y_pred = y_pred['y_pred'].squeeze().detach().cpu()

        pred_cnn_ts = y_pred * mask1.squeeze().numpy()
        sigma = sigma * mask1.squeeze().numpy()

        x_p2 = x_p + (nx-b*2)
        y_p2 = y_p + (ny-b*2)
        if normalize_output:
            px, py = y_pred.shape
            y_pred = xin[-1, x_p+b: x_p+b+ px, y_p+b:y_p + b + py] + unnormalize(pred_cnn_ts)

        if ts_out:
            pred_cnn_ts = pred_cnn_ts.squeeze()[0]
            sigma = sigma.squeeze()[0]

        recons_pred[x_p:x_p2, y_p:y_p2] = pred_cnn_ts
        recons_pred_sigma[x_p:x_p2, y_p:y_p2] = sigma

    return recons_pred, recons_pred_sigma

def plot_answer_sample_bay(pred, pred_sigma, gt, mask, ts, zoom=None, show_diff=True, global_scale=False, save_folder = None, model_name = '_1ts_ahead_cnn'):

    '''
    Visualize a full or zoomed in reconstruction for 1 timestep, for a single model. This function is valid for both 1timestep ahead and autoregressive mode
    Inputs:
        - predictions
        - aground truth
        - mask
        - zoom: array of coordinates
    '''

    import matplotlib.colors as colors
    cmin = 0.01
    cmap = "hot_r"
    cmap = "gist_heat_r"
#     cmap='seismic'
    if zoom is not None:
        pred = pred[:, zoom[0]:zoom[1], zoom[2]:zoom[3]]
        pred_sigma = pred_sigma[:,zoom[0]:zoom[1], zoom[2]:zoom[3]]
        gt = gt[:, zoom[0]:zoom[1], zoom[2]:zoom[3]]
        mask = mask[zoom[0]:zoom[1], zoom[2]:zoom[3]]

    if show_diff:
        fig, axs = plt.subplots(2,3, figsize=(18,10))
        axs = [axs[0][0], axs[0][1], axs[0][2], axs[1][0], axs[1][1], axs[1][2]]
    else:
        fig, axs = plt.subplots(1,4, figsize=(18,5))

    diff = np.abs(gt[ts] - pred[ts])

    if global_scale:
        vmax_abs = max(np.max(gt), cmin)
        vmax_diff = max(np.max(np.abs(gt-pred)), cmin)
    else:
        vmax_abs = max(np.max(gt[ts]), cmin)
        vmax_diff = max(np.max(diff), cmin)

    pos0 = axs[0].imshow(pred[ts], cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_abs))
    pos1 = axs[1].imshow(gt[ts], cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_abs))
    pos2 = axs[2].imshow(pred_sigma[ts], cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_diff))
    pos3 = axs[3].imshow(diff, cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_diff))
    if show_diff and ts>0:
        pos3 = axs[3].imshow(pred[ts] - gt[ts-1], cmap="seismic", vmin=-vmax_diff, vmax = vmax_diff)
        pos4 = axs[4].imshow(gt[ts] - gt[ts-1], cmap="seismic", vmin=-vmax_diff, vmax = vmax_diff)

    fig.suptitle("Visualization for model {} and timestep {} ".format(model_name.split('_')[-1], ts), fontsize = 14 ,fontweight="bold")

    axs[0].set_title('Reconstructed Output',fontweight="bold")
    axs[1].set_title('Reconstructed GT ',fontweight="bold")
    axs[2].set_title('Sigma',fontweight="bold")
    axs[3].set_title('Absolute Error',fontweight="bold")

    cbar0 = fig.colorbar(pos0, ax=axs[0], extend='both', fraction=0.136, pad=0.02)
    cbar1 = fig.colorbar(pos1, ax=axs[1], extend='both', fraction=0.136, pad=0.02)
    cbar2 = fig.colorbar(pos2, ax=axs[2], extend='both', fraction=0.136, pad=0.02)
    cbar3 = fig.colorbar(pos3, ax=axs[2], extend='both', fraction=0.136, pad=0.02)

    if show_diff and ts>0:
        axs[3].set_title('Diff pred',fontweight="bold")
        axs[4].set_title('Diff GT',fontweight="bold")
        cbar3 = fig.colorbar(pos3, ax=axs[3], extend='both', fraction=0.136, pad=0.02)
        cbar4 = fig.colorbar(pos4, ax=axs[4], extend='both', fraction=0.136, pad=0.02)

    if save_folder:
        if zoom:
            model_name = model_name + '_zoom'
        filename = save_folder + 'Reconstruction_' +  model_name + '.png'
        plt.savefig(filename, dpi=1200)
    plt.show()

    # Combine errors

def boxplot_single(time_step, pred_cnn, gt_cnn, mask_cnn):
    yhat = pred_cnn[time_step][mask_cnn]
    test_targets = gt_cnn[time_step][mask_cnn]

    a = [yhat, test_targets]
    a = np.array(a).T
    a_df = pd.DataFrame(data=a, columns=['yhat', 'test_targets'])
    a_df_sorted = a_df.sort_values(by=['test_targets'])
    a_df_sorted.reset_index(drop=True, inplace=True)
    a_df_error = 100 * np.abs(a_df_sorted.yhat - a_df_sorted.test_targets)    # predicting in meter, so need to be multiplied by 100

    data = np.array([a_df_error[a_df_sorted.test_targets<0.1],
            a_df_error[(a_df_sorted.test_targets>0.10) & (a_df_sorted.test_targets<0.20)],
            a_df_error[(a_df_sorted.test_targets>0.20) & (a_df_sorted.test_targets<0.50)],
            a_df_error[(a_df_sorted.test_targets>0.50) & (a_df_sorted.test_targets<1)],
            a_df_error[a_df_sorted.test_targets>1]], dtype="object")
    fig, ax = plt.subplots(figsize = [8,5])
    ax.set_ylabel('Absolute Error (cm)')
    ax.set_xlabel('Water Depth')
    ax.boxplot(data, showfliers=False)                                #'Hide Outlier Points'
    ax.set_xticklabels(['0-10 cm', '10-20 cm', '20-50cm', '50-100cm','>100cm'])

    plt.show()


import json

from collections import OrderedDict

from django.forms import Media, widgets
from django.utils.functional import cached_property

from wagtail import VERSION as WAGTAIL_VERSION
from wagtail.admin.rich_text.converters.editor_html import EditorHTMLConverter
from wagtail.admin.staticfiles import versioned_static

if WAGTAIL_VERSION >= (3, 0):
    from wagtail.admin.panels import FieldPanel
    from wagtail.rich_text import features
    from wagtail.telepath import register
    from wagtail.widget_adapters import WidgetAdapter
else:
    from wagtail.admin.edit_handlers import RichTextFieldPanel
    from wagtail.core.rich_text import features
    from wagtail.core.telepath import register
    from wagtail.core.widget_adapters import WidgetAdapter

from .plugins import CORE_HALLO_PLUGINS

class HalloRichTextArea(widgets.Textarea):
    template_name = "wagtail_hallo/widgets/hallo_rich_text_area.html"

    # this class's constructor accepts a 'features' kwarg
    accepts_features = True

    def get_panel(self):
        return FieldPanel if WAGTAIL_VERSION >= (3, 0) else RichTextFieldPanel

    def __init__(self, *args, **kwargs):
        self.options = kwargs.pop("options", None)

        self.features = kwargs.pop("features", None)
        if self.features is None:
            self.features = features.get_default_features()

        self.converter = EditorHTMLConverter(self.features)

        # construct a list of plugin objects, by querying the feature registry
        # and keeping the non-null responses from get_editor_plugin
        self.plugins = CORE_HALLO_PLUGINS + list(
            filter(
                None,
                [
                    features.get_editor_plugin("hallo", feature_name)
                    for feature_name in self.features
                ],
            )
        )
        self.plugins.sort(key=lambda plugin: plugin.order)

        super().__init__(*args, **kwargs)

    def format_value(self, value):
        # Convert database rich text representation to the format required by
        # the input field
        value = super().format_value(value)

        if value is None:
            return None

        return self.converter.from_database_format(value)

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)

        if self.options is not None and "plugins" in self.options:
            # explicit 'plugins' config passed in options, so use that
            plugin_data = self.options["plugins"]
        else:
            plugin_data = OrderedDict()
            for plugin in self.plugins:
                plugin.construct_plugins_list(plugin_data)
        context["widget"]["plugins_json"] = json.dumps(plugin_data)

        return context

    def value_from_datadict(self, data, files, name):
        original_value = super().value_from_datadict(data, files, name)
        if original_value is None:
            return None
        return self.converter.to_database_format(original_value)

    @cached_property
    def media(self):
        media = Media(
            js=[
                versioned_static("js/vendor/hallo.js"),
                versioned_static("js/hallo-editor.js"),
                versioned_static("js/hallo-telepath.js"),
            ],
            css={"all": [versioned_static("css/hallo.css")]},
        )

        for plugin in self.plugins:
            media += plugin.media

        return media

class HalloRichTextAreaAdapter(WidgetAdapter):
    js_constructor = "wagtail.widgets.HalloRichTextArea"

register(HalloRichTextAreaAdapter(), HalloRichTextArea)


n=int(input())
socks=list(map(int,input().split()))
get=[]
current=0
max_s=0
for i in socks:
    if i not in get:
        get.append(i)
        current+=1
        max_s=max(max_s,current)
    else:
        current-=1
print(max_s)