import concurrent.futures
import copy
import json
import os
import random
import math
from pathlib import Path
from collections.abc import Iterable
from utils.utils import DataCollection, DataUnit,DataBundle,DataInd

import matplotlib.pyplot as plt
from PIL import Image, ImageOps
from sklearn.model_selection import train_test_split
import pydicom
import cv2
import numpy as np
import pandas as pd

from tqdm.auto import tqdm

IMAGE_WIDTH = 256
IMAGE_HEIGHT = 256
THREAD_COUNT = 32
loaded_ids = []
loaded_ids_target = []
index_csv = {}

IMAGE_EXTS_LIST = ['jpg', 'png', 'tiff']
DATA_FORMATS_SPECIAL = ['2D_F', '2D_INT']
DATA_FORMATS = ['float', 'int', 'str'] + DATA_FORMATS_SPECIAL
REGRESSION_CATEGORY = 'int'
REGRESSION = 'float'
CATEGORY = 'Category'
STRING = 'str'
TIME_SERIES = 'TimeSeries'
IMAGE = '2D_F'
TARGET_TYPES = [REGRESSION, REGRESSION_CATEGORY, CATEGORY, TIME_SERIES, IMAGE]
HEURISTICS = {}

train_df = None

def image_json_loader_worker(args):
    global loaded_ids_target
    image_paths = args
    local_image_collection = {'train_inputs': [], 'train_outputs': [], 'test_inputs': [], 'test_outputs': []}
    for i, image_col in enumerate(image_paths):
        if i % 1000 == 0:
            print(i, '/', len(image_paths))
        try:
            image_path, image_name = image_col
        except Exception as e:
            continue
        with open(image_path + image_name, mode='r') as f:
            local_json = f.read()
            local_json = json.loads(local_json)
            for element in local_json['train']:
                local_image_collection['train_inputs'].append(np.array(element['input']))
                local_image_collection['train_outputs'].append(np.array(element['output']))
            for element in local_json['test']:
                local_image_collection['test_inputs'].append(np.array(element['input']))
                local_image_collection['test_outputs'].append(np.array(element['output']))

    return local_image_collection

def image_loader_worker(args):
    global loaded_ids_target
    image_paths, image_ids, no_ids, target_name = args
    local_image_collection = {}

    for i, image_col in enumerate(image_paths):
        if i % 1000 == 0:
            print(i, '/', len(image_paths))
        try:
            image_path, image_name = image_col
        except Exception as e:
            continue
        if image_path[-1] != '/':
            image_path = image_path + '/'
        local_img = cv2.imread(image_path + image_name)

        if image_ids is None:

            local_image_collection[image_name[:-4]] = \
                {"img": local_img, "data": None}
        else:
            if image_name[:image_name.index('.')] + '^' not in image_ids.keys():
                continue
            local_img_id = image_name[:image_name.index('.')] + '^'

            if local_img_id not in loaded_ids_target:
                loaded_ids_target.append(local_img_id)

            img_shape = image_ids[local_img_id].get_shape_by_name('Image')
            if img_shape != None and len(img_shape) > 0:
                local_img = cv2.resize(local_img, (img_shape[0], img_shape[1]))
                local_data = copy.deepcopy(image_ids[local_img_id])
            local_image_collection[image_name[:image_name.index('.')]] = {"img": local_img,
                                                                          "img_name": image_name[
                                                                                :image_name.index('.')]}

    return local_image_collection

def split_list_second(target_dict, count, restrict=False, size=1000):
    interval = int(len(target_dict[list(target_dict.keys())[0]]) / count)
    splited_dict = []
    for local_key in target_dict.keys():
        splited_dict[local_key] = []
    for local_key in splited_dict.keys():
        for i in range(count):
            splited_dict[local_key].append(target_dict[local_key][i * interval:(i + 1) * interval])
    for local_key in splited_dict.keys():
        splited_list_rest = splited_dict[local_key][(count + 1) * interval:]
        for i, element in enumerate(splited_list_rest):
            splited_dict[local_key][i].append(element)

    if restrict:
        for local_key in splited_dict.keys():
            for i in range(len(splited_dict[local_key])):
                random_start = random.randint(0, len(splited_dict[local_key][i]) - (size + 1))
                splited_dict[local_key][i] = splited_dict[local_key][i][random_start:random_start + size]

    return splited_dict

def split_list(target_list, count, restrict=False, size=1000):
    interval = int(len(target_list) / count)
    splited_list = []
    for i in range(count):
        splited_list.append(target_list[i * interval:(i + 1) * interval])

    splited_list_rest = splited_list[(count + 1) * interval:]
    for i, element in enumerate(splited_list_rest):
        splited_list[i].append(element)

    if restrict:

        for i in range(len(splited_list)):
            random_start = random.randint(0, len(splited_list[i]) - (size + 1))
            splited_list[i] = splited_list[i][random_start:random_start + size]

    return splited_list

def split_dict(target_dict, count, restrict=False, size=1000):
    interval = int(len(target_dict[list(target_dict.keys())[0]]) / count)
    splited_dict = {}
    for local_key in target_dict.keys():
        splited_dict[local_key] = []
    for local_key in splited_dict.keys():
        for i in range(count):
            splited_dict[local_key].append(target_dict[local_key][i * interval:(i + 1) * interval])
    for local_key in splited_dict.keys():
        splited_list_rest = splited_dict[local_key][(count + 1) * interval:]
        for i, element in enumerate(splited_list_rest):
            splited_dict[local_key][i].append(element)

    if restrict:
        for local_key in splited_dict.keys():
            for i in range(len(splited_dict[local_key])):
                random_start = random.randint(0, len(splited_dict[local_key][i]) - (size + 1))
                splited_dict[local_key][i] = splited_dict[local_key][i][random_start:random_start + size]

    return splited_dict

def image_loader_json_images(path, restrict=False, size=1000):
    image_paths = image_list(path)
    image_ids = None

    local_image_collection = {'num_classes': 0, 'image_arr': []}
    if size < len(image_paths):
        image_paths_list = split_list(image_paths, THREAD_COUNT, restrict, size)
    else:
        image_paths_list = []
        for element in image_paths:
            image_paths_list.append([element])

    with concurrent.futures.ThreadPoolExecutor(max_workers=THREAD_COUNT) as executor:

        futures = [executor.submit(image_json_loader_worker, args) for args in
                   zip(image_paths_list)]
        print("THREAD COUNT:", len(futures))
        results = [f.result() for f in futures]

    return results

local_lib = {}

def image_loader(path, train_name='train', restrict=False, size=1000, no_ids=False,
                 load_image=False, target_name=None, data_schema_input=None,data_schema_output=None, split=False, split_coef=0.9, THREAD_COUNT_V=32,
                 dir_tree=False):
    global THREAD_COUNT
    global train_df, valid_df
    THREAD_COUNT = THREAD_COUNT_V
    image_paths = image_list(path + 'train_images/')
    image_ids = None
    if Path(path + train_name + '.csv').exists():
        if not dir_tree:
            image_ids, loaded_ids_size = load_id_from_csv(path + train_name + '.csv',  data_schema_input,data_schema_output, restrict, size)
        else:
            image_ids, loaded_ids_size = load_id_from_dir_tree_csv(path, data_schema_input,data_schema_output, restrict, size)
    elif dir_tree:
        image_ids, loaded_ids_size = load_id_from_dir_tree_csv(path, data_schema_input,data_schema_output, restrict, size)
    print('csv load done')
    local_image_collection = {'num_classes': 0, 'image_arr': []}
    if size < len(image_paths):
        image_paths_list = split_list(image_paths, THREAD_COUNT, restrict, size)
    else:
        image_paths_list = []
        for element in image_paths:
            image_paths_list.append([element])

    if split:
        test_arr = []
        train_arr = []
        train_cutoff = int(split_coef * len(local_image_collection['image_arr']))
        # FIX THIS
        if not load_image:
            key_list = list(image_ids.keys())
            random.shuffle(key_list)
            train_cutoff = int(split_coef * len(key_list))
            for i in range(0, train_cutoff):
                train_arr.append(image_ids[key_list[i]])
            for i in range(train_cutoff, len(key_list)):
                test_arr.append(image_ids[key_list[i]])
            random.shuffle(train_arr)
            random.shuffle(test_arr)
            return {'num_classes': local_image_collection['num_classes'], 'image_arr': train_arr}, \
                   {'num_classes': local_image_collection['num_classes'], 'image_arr': test_arr}
        else:
            for i in range(0, train_cutoff):
                train_arr.append(local_image_collection['image_arr'][i])
            for i in range(train_cutoff, len(local_image_collection['image_arr'])):
                test_arr.append(local_image_collection['image_arr'][i])
            random.shuffle(train_arr)
            random.shuffle(test_arr)
            return {'num_classes': local_image_collection['num_classes'], 'image_arr': train_arr}, \
                   {'num_classes': local_image_collection['num_classes'], 'image_arr': test_arr}
    else:
        train_arr = []
        # FIX THIS

        train_cutoff = int(len(local_image_collection['image_arr']))
        if not load_image:
            key_list = list(image_ids)
            random.shuffle(key_list)
            train_cutoff = int(len(key_list))
            for i in range(0, train_cutoff):
                train_arr.append(image_ids[i])
            random.shuffle(train_arr)
            return {'num_classes': local_image_collection['num_classes'], 'image_arr': train_arr}, {
                'num_classes': local_image_collection['num_classes'], 'image_arr': []}
        else:
            for i in range(0, train_cutoff):
                train_arr.append(local_image_collection['image_arr'][i])

            random.shuffle(train_arr)
            return {'num_classes': local_image_collection['num_classes'], 'image_arr': train_arr}, {
                'num_classes': local_image_collection['num_classes'], 'image_arr': []}

def file_dict_by_id_value(dict_to_filter, filter_keys, value):
    return_dict = {}
    for local_key in list(dict_to_filter.keys()):
        return_dict[local_key] = []
    filter_indexies = []
    clean_dict = {}
    for local_key in list(dict_to_filter.keys()):
        clean_dict[local_key] = list(dict_to_filter[local_key].values())

    for i, element in enumerate(clean_dict[value]):
        if str(element) in filter_keys:
            filter_indexies.append(i)
    for local_key in list(dict_to_filter.keys()):
        for index_key in filter_indexies:
            return_dict[local_key].append(dict_to_filter[local_key][index_key])
    return return_dict

def worker_load_image_data_from_csv(args):
    # global loaded_ids
    local_dict, schema, cut_size, restrict = args
    data_schema_input, data_schema_output = schema
    local_data_arr_input = {}
    local_norm_list_input = []
    local_data_arr_output = {}
    local_norm_list_output = []
    local_id_poss = []
    for i, element in enumerate(data_schema_input):
        if element.is_id:
            local_id_poss.append(i)
    if restrict:
        for local_key in local_dict.keys():
            if cut_size != -1:
                local_dict[local_key] = local_dict[local_key][:cut_size]

    for i in range(len(local_dict[list(local_dict.keys())[0]])):
        local_row = []
        for element in data_schema_input:
            if element.name in list(local_dict.keys()):
                local_row.append(local_dict[element.name][i])
            else:
                local_row.append(None)
        local_norm_list_input.append(local_row)
    local_count = 1
    for row in local_norm_list_input:
        if local_count % 10 == 0:
            print(local_count, '/', len(local_norm_list_input))
        local_count += 1
        if len(row) == 0:
            continue
        try:
            local_id_name = ''
            # print('local_id_poss',local_id_poss)
            for element in local_id_poss:
                local_id_name += str(row[element]) + '^'
            local_data_arr_input[local_id_name] = DataCollection(data_size=len(row), data_schema=data_schema_input, data=row)

        except Exception as e:
           pass
#////////////////////////////////

    for i in range(len(local_dict[list(local_dict.keys())[0]])):
        local_row = []
        for element in data_schema_output:
            if element.name in list(local_dict.keys()):
                local_row.append(local_dict[element.name][i])
            else:
                local_row.append(None)
        local_norm_list_output.append(local_row)
    local_count = 1
    for row in local_norm_list_output:
        if local_count % 10 == 0:
            print(local_count, '/', len(local_norm_list_output))
        local_count += 1
        if len(row) == 0:
            continue
        try:
            local_id_name = ''
            # print('local_id_poss',local_id_poss)
            for element in local_id_poss:
                local_id_name += str(row[element]) + '^'
            local_data_arr_output[local_id_name] = DataCollection(data_size=len(row), data_schema=data_schema_output, data=row)

        except Exception as e:
            pass
    data_bundle_list  = []
    for input,target in zip(local_data_arr_input.values(),local_data_arr_output.values()):
        data_bundle_list.append(DataBundle(data_ind=DataInd(timestamp='00000', cat_subcat='00000', model_sign='00000'),source=input,target=target))
    return data_bundle_list

def load_id_from_dir_tree_csv(dir_path,  data_schema_input=None,data_schema_output=None, restrict=False, size=100):
    local_ids = {}
    data = os.listdir(dir_path + 'test_images' )# list(data_schema.keys())[0])
    print('len(data) >= THREAD_COUNT', len(data) >= THREAD_COUNT)
    if len(data) >= THREAD_COUNT:
        id_list = []
        id_list_ordered = split_list(target_list=data, count=THREAD_COUNT, restrict=restrict, size=size)
        data_schema = [(data_schema_input,data_schema_output)] * THREAD_COUNT

        for i in range(THREAD_COUNT):
            id_list.append(id_list_ordered[i])

        if restrict:
            size = [size] * THREAD_COUNT
            dir_path = [dir_path] * THREAD_COUNT
        else:
            size = [len(id_list[0])] * THREAD_COUNT
            dir_path = [dir_path] * THREAD_COUNT
        restrict = [restrict] * THREAD_COUNT
        print('id_list', type(id_list))

    else:

        local_ids = worker_load_image_data_from_dir_tree_csv((0, data, (data_schema_input,data_schema_output), size, restrict, dir_path))
    loaded_ids_size = len(local_ids.keys())
    return local_ids, loaded_ids_size

def load_id_from_csv(csv_path,  data_schema_input=None,data_schema_output=None, restrict=False, size=100):
    local_ids = {}
    data = pd.read_csv(csv_path, low_memory=False)
    csv_reader = data.to_dict(orient='list')
    print('len(data) >= THREAD_COUNT', len(data) >= THREAD_COUNT)
    if len(csv_reader) > THREAD_COUNT:

        id_dict = split_dict(target_dict=csv_reader, count=THREAD_COUNT, restrict=restrict, size=size)
        data_schema = [(data_schema_input,data_schema_output)] * THREAD_COUNT
        id_list = []
        for i in range(THREAD_COUNT):
            local_dict = {}
            local_len = len(id_dict.keys()) / THREAD_COUNT
            for local_key in list(id_dict.keys()[local_len * i:local_len * (i + 1)]):
                local_dict[local_key] = id_dict[local_key][i]
            id_list.append(local_dict)
        if restrict:
            size = [size] * THREAD_COUNT
        else:
            size = [len(id_list[0])] * THREAD_COUNT
        print('id_list', type(id_list))
        with concurrent.futures.ThreadPoolExecutor(max_workers=THREAD_COUNT) as executor:
            futures = [executor.submit(worker_load_image_data_from_csv, args) for args in
                       zip(id_list, data_schema[csv_path[csv_path.rindex('/') + 1:csv_path.rindex('.')]], size,
                           restrict)]
            print("THREAD COUNT:", len(futures))
            results = [f.result() for f in futures]

    else:

        results = worker_load_image_data_from_csv(
            (csv_reader, (data_schema_input,data_schema_output), size, restrict))
    loaded_ids_size = len(local_ids)
    return results, loaded_ids_size

def image_list(path):
    image_path_list = []
    for root, subdirs, files in os.walk(path):
        for file in files:
            if file[file.index('.') + 1:] in IMAGE_EXTS_LIST:
                image_path_list.append((root, file))
    return image_path_list[1:]


import pytest
from func.get_customers import get_customers

@pytest.mark.parametrize("input_data, expected_result", [
    ((1, 12), ['Byron Fields', 'Charles Morris', 'Emma Wong', 'Eve Holt', 'George Bluth',
                                    'George Edwards', 'Janet Weaver', 'Lindsay Ferguson', 'Michael Lawson',
                                    'Rachel Howell', 'Tobias Funke', 'Tracey Ramos']),
    ((3, 6), ['Charles Morris', 'Emma Wong', 'Eve Holt', 'Tracey Ramos']),
    ((6, 3), ['Charles Morris', 'Emma Wong', 'Eve Holt', 'Tracey Ramos']),
    ((3, 3), ['Emma Wong']),
    (('str', 5), []),
    ((5, 'str'), []),
    (((5, 7, 6), 'str'), []),
    ((5, [5, 7, 6]), []),
    ((-5, 7), []),
    ((5, -7), []),

])
def test_get_customers_function(input_data, expected_result):
    assert get_customers(input_data[0],input_data[1]) == expected_result

def test_get_customers_function_no_arguments():
    assert get_customers() == []


import numpy as np
import matplotlib.pyplot as plt

import histogram_module
import dist_module
import match_module

# compute and plot the recall/precision curve
#
# D - square matrix, D(i, j) = distance between model image i, and query image j
#
# note: assume that query and model images are in the same order, i.e. correct answer for i-th query image is the i-th model image

def plot_rpc(D, plot_color):

    recall = []
    precision = []
    num_queries = D.shape[1]

    num_images = D.shape[0]
    assert(num_images == num_queries), 'Distance matrix should be a square matrix'

    labels = np.diag([1]*num_images)

    d = D.reshape(D.size)
    l = labels.reshape(labels.size)

    sortidx = d.argsort()
    d = d[sortidx]
    l = l[sortidx]

    tp = 0
    #... (your code here)

    false_negative = num_images
    false_positive = 0

    for idt in range(len(d)):
        tp += l[idt]
        #... (your code here)
        false_negative -= l[idt]
        false_positive += (1 - l[idt])

        precision.append(tp/(tp+false_positive))
        recall.append(tp/(tp+false_negative))

    plt.plot([1-precision[i] for i in range(len(precision))], recall, plot_color+'-')

def compare_dist_rpc(model_images, query_images, dist_types, hist_type, num_bins, plot_colors):

    assert len(plot_colors) == len(dist_types), 'number of distance types should match the requested plot colors'

    for idx in range( len(dist_types) ):

        [best_match, D] = match_module.find_best_match(model_images, query_images, dist_types[idx], hist_type, num_bins)

        plot_rpc(D, plot_colors[idx])

    plt.axis([0, 1, 0, 1]);
    plt.xlabel('1 - precision');
    plt.ylabel('recall');

    # legend(dist_types, 'Location', 'Best')

    plt.legend( dist_types, loc='best')


l = [str(65651),'alireza\n', 'mohmmad\n', 'reza\n', 'sara\n', 'neda\n']
# file = open('./username.txt','w')
# file.write('mina')
# # for user in l:
# # 	file.write(f'{user}\n')
# file.writelines(l)
# file.close()

# file = open('./username.txt','a')
# file.write('mina')
# file.write('amir')
# file.write('zahra')
# file.close()

# file = open('username.txt', 'r')
# # all_file = file.read()
# # print(all_file)
# # print(type(all_file))

# lines = file.readlines()
# print(lines)
# print(type(lines))

# lines = file.readlines()
# print(lines)
# print(type(lines))
# file.close()

with open('./username.txt','w') as fw:
	fw.write('wrtie in with block')
	fw.write('wrtie in with block')
	fw.write('wrtie in with block')
	fw.write('wrtie in with block')
	fw.write('wrtie in with block')

with open('./username.txt','a') as fa:
	fa.writelines(l)

with open('username.txt', 'r') as fr:
	print(fr.read())

with open('username.txt', 'r+') as fr:
	print(fr.read())
	fr.write('adgadg')
	fr.writelines(l)


# This is a pytorch implementation of biLSTM

import torch
from torch import nn
from torch.nn import functional as F
from torch.nn.utils.rnn import pack_sequence, pad_packed_sequence, pad_sequence

import utils
import os

import pandas as pd

class CNN_BiLSTM(nn.Module):
    """ Bidirectional long-short term memory network.

    It has a bi-LSTM as the main part, then follows a three-layer perception
    classifier. Dropout is applied to the output of the bi-LSTM and every layer
    of the perception, when the probability is set to be 0.5. Finally a log
    softmax is used to normalize the output.
    """
    def __init__(self, args):
        super(CNN_BiLSTM, self).__init__()
        self.args = args
        self.embed = nn.Embedding.from_pretrained(
            utils.embed_from_pretrained(args),
            freeze=self.args.freeze_embed
        )
        # self.conv1 = nn.Conv2d(in_channels=1,
        #                        out_channels=self.args.cnn_out_channels,
        #                        kernel_size=self.args.kernel_size,
        #                        stride=self.args.cnn_stride)
        self.conv1 = nn.Conv1d(in_channels=self.args.embed_num,
                               out_channels=self.args.cnn_out_channels,
                               kernel_size=self.args.kernel_size,
                               stride=self.args.cnn_stride,
                               bias=False)
        self.conv2 = nn.Conv1d(in_channels=self.args.embed_num,
                               out_channels=self.args.cnn_out_channels,
                               kernel_size=self.args.kernel_size,
                               stride=self.args.cnn_stride,
                               bias=False)
        self.maxpool = nn.MaxPool1d(kernel_size=3)
        self.batchnorm_forward = nn.BatchNorm1d(self.args.cnn_out_channels)
        self.batchnorm_backward = nn.BatchNorm1d(self.args.cnn_out_channels)
        self.lstm_forward = nn.LSTM(self.args.cnn_out_channels,
                                    self.args.hidden_dim,
                                    num_layers=1,
                                    dropout=self.args.dropout,
                                    bias=True)
        self.lstm_backward = nn.LSTM(self.args.cnn_out_channels,
                                     self.args.hidden_dim,
                                     num_layers=1,
                                     dropout=self.args.dropout,
                                     bias=True)
        self.fc1 = nn.Linear(self.args.hidden_dim * 2,
                             self.args.hidden_dim)
        self.fc2 = nn.Linear(self.args.hidden_dim,
                             1)
        self.softmax = nn.LogSoftmax()
        self.dropout = nn.Dropout(p=self.args.dropout)

    def forward(self, x):
        """ Overwrites the function for forward propagation.

        It accepts a list containing 1-d tensors that contains indexes to the
        embeddings.
        """
        code, code_rev = x

        code_embedded = pad_sequence(
            [self.embed(i.to(self.args.device)) for i in code])
        code_rev_embedded = pad_sequence(
            [self.embed(i.to(self.args.device)) for i in code_rev])

        code_embedded = torch.transpose(code_embedded, 0, 1)
        code_rev_embedded = torch.transpose(code_rev_embedded, 0, 1)

        code_embedded = code_embedded.view(code_embedded.shape[0],
                                           code_embedded.shape[1],
                                           code_embedded.shape[2])
        code_rev_embedded = code_rev_embedded.view(code_rev_embedded.shape[0],
                                                   code_rev_embedded.shape[1],
                                                   code_rev_embedded.shape[2])

        code_embedded = torch.transpose(code_embedded, 1, 2)
        code_rev_embedded = torch.transpose(code_rev_embedded, 1, 2)

        # 1d convolution layer
        code_embedded = self.conv2(code_embedded)
        code_rev_embedded = self.conv2(code_rev_embedded)

        # Dropout layer
        code_embedded = self.dropout(code_embedded)
        code_rev_embedded = self.dropout(code_rev_embedded)

        code_embedded = torch.transpose(code_embedded, 1, 2)
        code_rev_embedded = torch.transpose(code_rev_embedded, 1, 2)
        code_embedded = torch.transpose(code_embedded, 0, 1)
        code_rev_embedded = torch.transpose(code_rev_embedded, 0, 1)

        # bi-layer LSTM
        lstm_forward_out, _ = self.lstm_forward(code_embedded)
        lstm_backward_out, _ = self.lstm_backward(code_rev_embedded)

        # # Use the maxpool to get the output
        # lstm_forward_out = torch.transpose(lstm_forward_out, 0, 1)
        # lstm_forward_out = torch.transpose(lstm_forward_out, 1, 2)
        # lstm_forward_out = F.tanh(lstm_forward_out)
        # lstm_forward_out = F.max_pool1d(lstm_forward_out,
        #                                 lstm_forward_out.size(2)).squeeze(2)

        # lstm_backward_out = torch.transpose(lstm_backward_out, 0, 1)
        # lstm_backward_out = torch.transpose(lstm_backward_out, 1, 2)
        # lstm_backward_out = F.tanh(lstm_backward_out)
        # lstm_backward_out = F.max_pool1d(lstm_backward_out,
        #                                  lstm_backward_out.size(2)).squeeze(2)

        # bilstm_out = torch.cat((lstm_forward_out, lstm_backward_out), dim=1)

        # Use the last output of bilstm as output
        bilstm_out = torch.cat((lstm_forward_out, lstm_backward_out), dim=2)
        lengths = torch.tensor([i.shape[0] for i in code])
        lengths = ((lengths - self.args.kernel_size) /
                   self.args.cnn_stride + 1).to(torch.long)

        idx = (lengths - 1).view(-1, 1).expand(
            len(lengths), bilstm_out.size(2))

        # if batch is first, change 0 to 1
        idx = idx.unsqueeze(0).to(self.args.device)
        bilstm_out = bilstm_out.gather(0, idx).squeeze(0)

        bilstm_out = self.dropout(bilstm_out)
        y = F.relu(self.fc1(bilstm_out))
        y = self.fc2(y)

        return y

    def initialize(self):
        """ Initializes the parameters of the model itself.

        It initializes the weights of bilstm orthogonally, which is suggested
        by a lot of blogger on the Internet, and initializes the biases of the
        bilstm with zeros.

        Args:
            None

        Returns:
            None
        """
        # Initialize gates and hidden weights of layer 1 with orthogonal
        # normalization
        print(self.lstm_forward.bias_ih_l0)
        nn.init.orthogonal_(self.lstm_forward.weight_ih_l0)
        nn.init.orthogonal_(self.lstm_forward.weight_hh_l0)
        nn.init.orthogonal_(self.lstm_backward.weight_ih_l0)
        nn.init.orthogonal_(self.lstm_backward.weight_hh_l0)

        # nn.init.xavier_normal_(self.lstm_forward.weight_ih_l0)
        # nn.init.xavier_normal_(self.lstm_forward.weight_hh_l0)
        # nn.init.xavier_normal_(self.lstm_backward.weight_ih_l0)
        # nn.init.xavier_normal_(self.lstm_backward.weight_hh_l0)

        nn.init.zeros_(self.lstm_forward.bias_ih_l0)
        nn.init.ones_(self.lstm_forward.bias_ih_l0[1])
        nn.init.zeros_(self.lstm_forward.bias_hh_l0)
        nn.init.zeros_(self.lstm_backward.bias_ih_l0)
        nn.init.ones_(self.lstm_backward.bias_ih_l0[1])
        nn.init.zeros_(self.lstm_backward.bias_hh_l0)

        nn.init.xavier_normal_(self.conv2.weight)

        # # Initialize gates and hidden weights of layer 2 with orthogonal
        # # normalization
        # nn.init.orthogonal_(self.lstm_forward.weight_ih_l1)
        # nn.init.orthogonal_(self.lstm_forward.weight_hh_l1)
        # nn.init.orthogonal_(self.lstm_backward.weight_ih_l1)
        # nn.init.orthogonal_(self.lstm_backward.weight_hh_l1)

        # # nn.init.xavier_normal_(self.bilstm.weight_ih_l0)
        # # nn.init.xavier_normal_(self.bilstm.weight_hh_l0)
        # # nn.init.xavier_normal_(self.bilstm.weight_ih_l0)
        # # nn.init.xavier_normal_(self.bilstm.weight_hh_l0)

        # nn.init.zeros_(self.lstm_forward.bias_ih_l1)
        # nn.init.ones_(self.lstm_forward.bias_ih_l1[1])
        # nn.init.zeros_(self.lstm_forward.bias_hh_l1)
        # nn.init.zeros_(self.lstm_backward.bias_ih_l1)
        # nn.init.ones_(self.lstm_backward.bias_ih_l1[1])
        # nn.init.zeros_(self.lstm_backward.bias_hh_l1)

        pass


from .deeplabv3_mb import *
from .deeplabv3_resnet import *

def get_network(backbone = 'mbv2_deeplab',
                rep_size = 0,
                num_classes=11,
                text_features = None,
                ):

    if backbone == 'mbv2_deeplab':
        model = DeepLabv3Plus_MB(backbone = backbone,
                                 rep_size = rep_size,
                                 num_classes = num_classes
                                 )
    elif backbone == 'r50_deeplab' or backbone == 'r101_deeplab' or backbone == 'r50d_deeplab' or backbone =='r101d_deeplab':
        model = DeepLabv3Plus_RN(backbone = backbone,
                                 rep_size = rep_size,
                                 num_classes = num_classes
                                 )
    else:
        raise NotImplementedError("Backbone not supported at this time")

    return model


#coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
from GlobalConfig import *

def pictureDayUser():
	n_groups = 31
	index = np.arange(n_groups)
	aDayNews = [0 for i in index]
	aDayDifferrentUsers = [0 for i in index]
	aDayDifferrentNews = [0 for i in index]

	for i in index:
		tempUsers = []
		tempNews = []
		with open(GlobalConfig.dayFileName+str(i+1)) as fr:
			news = fr.readlines()
			aDayNews[i] = len(news)
			for item in news:
				tempUsers.append(item.split("\t")[0])
				tempNews.append(item.split("\t")[1])

			aDayDifferrentUsers[i] = len(set(tempUsers))
			aDayDifferrentNews[i] = len(set(tempNews))
		#print aDayNews[i],aDayDifferrentUsers[i],aDayDifferrentNews
	plt.figure(figsize=(30,9))
	bar_with = 0.28
	opacity = 0.6
	#rects1 = plt.bar(index, aDayNews, bar_with, alpha=opacity, color='b', label="News")
	rects2 = plt.bar(index + bar_with, aDayDifferrentNews, bar_with, alpha=opacity, color='r',\
		label="DiffNews")
	rects3 = plt.bar(index + 2*bar_with, aDayDifferrentUsers, bar_with, alpha=opacity, color='y', \
		label="DiffUsers")

	plt.xlabel("Day")
	plt.ylabel("Numbers")
	plt.title("One day's news and users")
	plt.xticks(index+1.5*bar_with, index+1)
	plt.legend()
	plt.show()

if __name__ == "__main__":
	pictureDayUser()


import cas.common.utilities as utilities
from cas.common.steamtools import SteamInstance
from cas.common.config import ConfigurationUtilities, LazyDynamicBase
from cas.common.cache import CacheManager

from pathlib import Path
from typing import Any, List, Union
import os
import logging
import subprocess

class BuildEnvironment:
    """
    Contains attributes about the current build environment
    """

    def __init__(self, path: str, config: dict):
        self.config = ConfigurationUtilities.parse_root_config(path, config)

        self.cache = CacheManager(path)
        self.cache.load()

        self.build_type = self.config.args.build_type
        self.build_categories = None

        categories = self.config.args.build_categories
        if categories:
            categories = categories.split(",")
            self.build_categories = frozenset(categories)

        self.verbose = self.config.args.verbose

        self.root = self.config.path.root
        self.content = self.config.path.content
        self.game = self.config.path.game
        self.src = self.config.path.src

        self.platform = utilities.resolve_platform_name()
        self.steam = None

        self._setup_bindir()

    def _get_appid_folder(self, appid: int) -> Path:
        if not self.steam:
            self.steam = SteamInstance()
        for app in self.steam.apps:
            if app.appid == appid:
                return app.path
        return None

    def _check_autodetect_appid(self) -> bool:
        if self.src.exists():
            logging.debug("appid autodetect skipped - src dir exists")
            return False
        elif self.get_tool("chaos").exists():
            logging.debug("appid autodetect skipped - chaos executable exists")
            return False

        return True

    def _setup_bindir(self):
        self.bindir = self.game.joinpath("bin", self.platform)
        override = self.config.options.get("bin_path")

        # we have a bin path override
        if override is not None:
            self.bindir = Path(override).joinpath(self.platform)
        # we're a mod (modwrapper present but no game executable) - autodetect bin path from appid
        elif self._check_autodetect_appid():
            appid = self.config.options.get("bin_appid")
            if appid is not None:
                logging.debug(f"mod detected - using base appid {str(appid)}")
                self.bindir = self._get_appid_folder(appid).joinpath(
                    "bin", self.platform
                )

        if not self.bindir.exists():
            raise Exception("Could not find the bin directory")

        logging.debug(f"using bin directory {self.bindir}")

    """
    Retrieves the absolute path to the tool at the specified source path.
    If the source path is None, it will default to self.bindir.
    """

    def get_tool(self, tool: str, src: Path = None) -> Path:
        if src is None:
            src = self.bindir
        assert not tool.endswith(".exe")
        if utilities.is_platform_windows():
            tool += ".exe"
        return src.joinpath(tool).resolve()

    def get_lib(self, lib: str) -> Path:
        if utilities.is_platform_windows():
            lib += ".dll"
        elif utilities.is_platform_osx():
            lib += ".dylib"
        elif utilities.is_platform_linux():
            lib += ".so"
        else:
            raise NotImplementedError()
        return self.bindir.joinpath(lib).resolve()

    def run_subprocess(self, *args, **kwargs):
        predef = {}
        if not self.verbose:
            predef["stdout"] = subprocess.DEVNULL
            predef["stderr"] = subprocess.DEVNULL
        return subprocess.run(*args, **dict(predef, **kwargs))

    def run_tool(
        self, args: List[str], source: bool = False, cwd: Union[str, Path] = None
    ) -> int:
        """
        High-level interface to run an executable with extra parameters
        """
        predef = {}
        predef["env"] = os.environ

        if source:
            predef["env"]["VPROJECT"] = str(self.config.path.vproject)
            predef["env"]["NOASSERT"] = "1"
        if cwd:
            predef["cwd"] = cwd

        try:
            result = self.run_subprocess(args, **predef)
        except Exception as e:
            raise Exception(f"failed to execute tool with parameters: {args}") from e
        return result.returncode

class BuildResult:
    """
    Represents a result returned from a subsystem
    """

    def __init__(self, success: bool, outputs: dict = {}):
        self.success = success
        self.outputs = outputs

class BuildSubsystem:
    """
    Represents a build system that implements custom behaviour.
    """

    def __init__(self, env: BuildEnvironment, config: dict):
        mod = self.__class__.__module__

        self.env = env
        self.config = config
        self._cache = env.cache["subsystems"][mod]
        self._logger = logging.getLogger(mod)

    def _get_config_raw(self) -> Any:
        # If we have a lazy config object then we need to extract its underlying raw data
        if isinstance(self.config, LazyDynamicBase):
            return self.config._data
        else:
            return self.config

    def needs_rebuild(self) -> bool:
        """
        Checks whether this subsystem needs to force a rebuild,
        based on whether its configuration hash has changed from the last successful build
        """
        old_hash = self._cache.get("config", None)
        new_hash = utilities.hash_object_sha256(self._get_config_raw())
        self._cache["config"] = new_hash

        if not old_hash:
            return False
        return old_hash != new_hash

    def rehash_config(self):
        """
        Rehashes the configuration and saves it in our cache
        """
        self._cache["config"] = utilities.hash_object_sha256(self._get_config_raw())

    def build(self, force: bool = False) -> BuildResult:
        """
        Invokes the build logic of the subsystem.
        Returns a BuildResult with the result.
        """
        raise NotImplementedError()

    def clean(self) -> bool:
        """
        Removes all the output files generated by this subsystem.
        """
        raise NotImplementedError()


def isprime2(n):
    '''
    check if integer n is a prime, return True or False
    '''
    # 2 is the only even prime
    if n == 2:
        return -1
    # integers less than 2 and even numbers other than 2 are not prime
    elif n < 2 or not n & 1:
        return 2
    # loop looks at odd numbers 3, 5, 7, ... to sqrt(n)
    for i in range(3, int(n**0.5)+1, 2):
        if n % i == 0:
            return i
    return -1

N = 16
J = 50

cases = pow(2,N-2)
count = 0
i = -1
print("Case #1:")
while i < cases-1:
    i +=1
    string = "{0:b}".format(i).zfill(N-2)
    string = "1" + string + "1"

    li = []
    for base in range(2,11):
        v = int(string, base)
        n = isprime2(v)
        if n == -1:
            break
        else:
            li.append(n)

    if len(li) != 9:
        continue
    print(string + " " + str(li[0]) + " "+ str(li[1]) + " "+ str(li[2]) + " "+ str(li[3]) + " "+ str(li[4]) + " "+ str(li[5]) + " "+ str(li[6]) + " "+ str(li[7]) + " "+ str(li[8]))
    count += 1
    if count == J:
        break


def solution(m, n, puddles):
    puddles = sorted(puddles, key=(lambda l: (l[0], l[1])))
    ways = [[None for i in range(m)] for j in range(n)]
    for row, y in zip(ways, range(len(ways))):
        for col, x in zip(row, range((len(ways[0])))):
            if y == 0 or x == 0:
                ways[y][x] = 1
                print(y, x)
            else:
                ways[y][x] = ways[y - 1][x] + ways[y][x - 1]
    for puddle, idx in zip(puddles, range(len(puddles))):
        ways[puddle[0] - 1][puddle[1] - 1] = 0
    for y in range(n):
        for x in range(m):
            if x == 0 or y == 0:
                continue
            elif ways[y][x] != 0:
                ways[y][x] = ways[y - 1][x] + ways[y][x - 1]
    print(ways)

    return ways[n - 1][m - 1]

if __name__ == '__main__':
    answer = solution(4, 3, [[2, 2]])
    print(answer)


#10분 #그래프이론 #최소스패닝트리
"""
https://www.acmicpc.net/problem/1647
[1647] : 도시 분할 계획
"""

import heapq
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
graph = [[] for _ in range(N+1)]
visited = [0] * (N+1)
for i in range(M):
    a, b, c = map(int, input().split())
    graph[a].append((c, b))
    graph[b].append((c, a))

heap = [[0, 1]]
cnt = 0
answer = 0
maximum = 0
while heap:
    if cnt == N:
        break

    weight, vertex = heapq.heappop(heap)

    if not visited[vertex]:
        visited[vertex] = 1
        cnt += 1
        answer += weight
        maximum = max(maximum, weight)
        for i in graph[vertex]:
            heapq.heappush(heap, i)

print(answer-maximum)


#!/usr/bin/env python
'''
    This is a module that implements a basic interface with knowrob in order to extract graphs for bayesian learning - testing autodoc
'''

import roslib;roslib.load_manifest('learning')
import json_prolog
from graph_util import GraphBundle
from graph_util import convert_adj_list_to_matrix
import numpy as np

#should I end the query when I am done???
#properties for class level implementation is missing!!!

#tested
def query(query_str):
    prolog = json_prolog.Prolog()
    query = prolog.query(query_str)
    solution = query.solutions()
    return solution

#tested
def query_property(property_str,namespace_str = "http://www.hwu.ac.uk/osl/test.owl"):
    property_with_namespace = "%s#%s" %(namespace_str,property_str)
    cmd = '''owl_has(S,'%s',O)''' % property_with_namespace
    result = query(cmd)
    return result

#tested
def get_labels(result_dict):
	labels_with_uri = []
	for x in result_dict:
		if not x['S'] in labels_with_uri:
			labels_with_uri.append(x['S'])
		if not x['O'] in labels_with_uri:
			labels_with_uri.append(x['O'])
	return labels_with_uri

#tested
#added code for the reflexive properties --- still a hack needs to be refactored
#reflexive hack is removed ---quality of code poor
def compute_adjacency_list(result_dict, prop_with_uri):
	adj_list = dict()
	refl = 0
	cmd = '''owl_has('%s',P,O)''' % prop_with_uri
	tmp_result = query(cmd)
	for ss in tmp_result:
		if ss['O'].split('#')[1] == "ReflexiveProperty":
			refl = 1

	for solution in result_dict:
		obj_list = []
		if adj_list.get(solution['S']) == None:
			if refl:
				obj_list.append(solution['S'])
		else:
			obj_list = adj_list.get(solution['S'])
		obj_list.append(solution['O'])
		adj_list[solution['S']] = obj_list

	if refl:
		obj_list = []
		for solution in result_dict:
			if adj_list.get(solution['O']) == None:
				obj_list.append(solution['O'])
				adj_list[solution['O']] = obj_list

	return adj_list

#tested
#stripped out the conversion from list to matrix in a separate function - tested
def compute_adjacency_matrix(node_list,query_result, prop_with_uri):
	adj_list = compute_adjacency_list(query_result,prop_with_uri)
	adj_mtr = convert_adj_list_to_matrix(node_list,adj_list)
	return adj_mtr

#tested
def extract_graph(property_str,ontology_str = "test.owl"):
	ontology_uri = "http://www.hwu.ac.uk/osl/%s" % ontology_str
	prop_with_uri = ontology_uri + '#'  + property_str
	query_result = list(query_property(property_str,ontology_uri))
	result = GraphBundle()
	result.labels = get_labels(query_result)
	result.adjacency_matrix = compute_adjacency_matrix(result.labels,query_result, prop_with_uri)
	result.update_state()
	result.labels = [y.split('#')[1]	for y in result.labels]
	result.property_str = property_str
	return result


import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
import os
import argparse

output_dir = "../comparison/new_plots"

# compute deviation of alter/time passed inbetween visits
def compute_error(df):
    dage = df['alter'].shift(-1) - df['alter']
    dage = dage[dage.index % 16 != 15]  # drop diff between age 18 and next child's age 3
    dtime = df['time'].shift(-1) - df['time']
    dtime = dtime[dtime.index % 16 != 15]  # drop diff between age 18 and next child's age 3
    time_err = dtime - dage
    return time_err

def summing_error(df):
    macro_sum = df['EW_p'] + df['Fett_p'] + df['KH_p']
    macro_sum_err = macro_sum - 100
    return macro_sum_err

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Plot pearson correlations for both real and synthetic data')
    # positional arguments:
    parser.add_argument('output_dir', help='path to folder where the output files will be saved')
    parser.add_argument('syn_data_vambn', help='path to synthetic vambn data in csv format, one row per participant')
    parser.add_argument('syn_data_vambn_ft',
                        help='path to synthetic vambn_ft data in csv format, one row per participant')
    parser.add_argument('syn_data_vambn_mt',
                        help='path to synthetic vambn_mt data in csv format, one row per participant')

    args = parser.parse_args()
    # check whether output folder exists, if not create
    if not os.path.exists(args.output_dir):
        os.mkdir(args.output_dir)

    df_vambn = pd.read_csv(args.original_data_path)
    df_vambn_ft = pd.read_csv(args.syn_data_vambn_ft)
    df_vambn_mt = pd.read_csv(args.syn_data_vambn_mt)

    df_vambn_error = compute_error(df_vambn)
    df_vambn_ft_error = compute_error(df_vambn_ft)
    df_vambn_mt_error = compute_error(df_vambn_mt)

    for error, name in zip([df_vambn_error, df_vambn_ft_error, df_vambn_mt_error],
                           ["VAMBN", "VAMBN-FT", "VAMBN-MT"]):
        print(f'mean/median absolute time-error {name}:', error.abs().mean(), '/', error.abs().median())

    use_logscale = False
    logticks = [-50, -20, -10, -5, -2, -1, 0, 1, 2, 5, 10, 20, 50]

    # plot line
    fig, ax = plt.subplots()
    ax = sns.kdeplot(data=df_vambn_error, label='VAMBN', ax=ax)
    ax = sns.kdeplot(data=df_vambn_ft_error, label='VAMBN-FT', ax=ax)
    ax = sns.kdeplot(data=df_vambn_mt_error, label='VAMBN-MT', ax=ax)
    ax.set_xlabel('Δ$\it{time}$ - Δ$\it{alter}$, in years')
    ax.set_ylabel('Density')
    if use_logscale:
        ax.set_xscale('symlog')
        ax.set_xticks(logticks)
        ax.get_xaxis().set_major_formatter(ticker.ScalarFormatter())
    plt.legend()
    plt.xlim(-75, 75)
    plt.savefig(f'{args.output_dir}/time-error.pdf', bbox_inches='tight')

    # 100% SUMMING ERROR
    df_vambn_error_sum = compute_error(df_vambn)
    df_vambn_ft_error_sum = compute_error(df_vambn_ft)
    df_vambn_mt_error_sum = compute_error(df_vambn_mt)

    for error, name in zip([df_vambn_error_sum, df_vambn_ft_error_sum,
                            df_vambn_mt_error_sum],
                           ["VAMBN", "VAMBN-FT", "VAMBN-MT"]):
        print(f'mean/median absolute sum-error {name}:', error.abs().mean(), '/', error.abs().median())

    use_logscale = False
    # plot line
    fig, ax = plt.subplots()
    ax = sns.kdeplot(data=df_vambn_error_sum, label='VAMBN', ax=ax)
    ax = sns.kdeplot(data=df_vambn_ft_error_sum, label='VAMBN-FT', ax=ax)
    ax = sns.kdeplot(data=df_vambn_mt_error_sum, label='VAMBN-MT', ax=ax)

    ax.set_xlabel('(EW_p+Fett_p+KH_p) - 100')
    ax.set_ylabel('Density')
    # ax.set_title('Capability of summing to 100%')
    if use_logscale:
        ax.set_xscale('symlog')
        ax.set_xticks(logticks)
        ax.get_xaxis().set_major_formatter(ticker.ScalarFormatter())
    plt.legend()
    plt.savefig(f'{args.output_dir}/100-error.pdf', bbox_inches='tight')
    plt.show()

print('fin')


from contextlib import contextmanager

import zmq

from pairedmq.exc import TimeoutError

@contextmanager
def handshake_receiver(timeout=None):
    socket = zmq.Context().socket(zmq.REP)
    socket.setsockopt(zmq.LINGER, 0)
    port = socket.bind_to_random_port("tcp://127.0.0.1")
    try:
        def wait():
            poller = zmq.Poller()
            poller.register(socket, zmq.POLLIN)
            if poller.poll(timeout):
                received = int(socket.recv(zmq.DONTWAIT))
                socket.send_string("")  # Ack reception
            else:
                raise TimeoutError("Handshake timeout")
            return received

        yield (port, wait)
    finally:
        socket.close()

def handshake_sender(handshake_port, port, timeout=None):
    if timeout is None:
        timeout = -1
    socket = zmq.Context().socket(zmq.REQ)
    socket.setsockopt(zmq.LINGER, 0)
    socket.connect("tcp://127.0.0.1:%s" % handshake_port)
    socket.send_string(str(port))

    poller = zmq.Poller()
    poller.register(socket, zmq.POLLIN)
    if poller.poll(timeout):
        socket.recv()  # wait for ack
        socket.close()
    else:
        socket.close()
        raise TimeoutError("Handshake timeout")


""" Write a Python script to check whether a given key already exists in a dictionary.
    STUDENT ID:-    20CS043
    STUDENT NAME:-  DEV PAREKH
"""
# Creating a dictionary
d = {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
a = 2

# Checking if key a is present or not
if a in d:
    print("Given key:", a, " present")
else:
    print("Given key:", a, " NOT present")


#!/usr/bin/python3
"""This is automated testing for this api"""
import requests

url = 'https://hngmus2.onrender.com/api/'

#create person
person_information = {
    'name': 'tester1'
}
person = requests.post(url + 'peoples', json=person_information)
print(person.status_code)
print(f"person {person.json().get('id')} created sucessfully: {person.json()}")
person_information = {
    'name': 'tester2'
}
person = requests.post(url + 'peoples', json=person_information)
print(person.status_code)
print(f"person {person.json().get('id')} created sucessfully: {person.json()}")
person_information = {
    'name': 'tester3'
}
person = requests.post(url + 'peoples', json=person_information)
print(person.status_code)
user_id = person.json().get('id')
print(f"person {person.json().get('id')} created sucessfully: {person.json()}")

#get all person
person = requests.get(url + 'peoples')
print(f"All people in the database are {person.json()}")

#get a person
person = requests.get(url + 'people/2')
print(f"get information about person with {person.json().get('id')} is {person.json()}")

#update a person
person = requests.put(url + 'people/1', json={'name': '1-tester'})
print(f"The updated information about person with {person.json().get('id')} is {person.json()}")

#search a person
person = requests.get(url + 'people_search', params={'name': 'tester3'})
print(f"search a person with name  tester3 amd they are {person.json()}")

#delete a person
person = requests.delete(url + f'people/{user_id}')
print(f"Deleted the perdon with id {person.json().get('id')} successfully")

#gethjjjjjjhg
stats = requests.get(url + 'stats')
print(stats.status_code)
print(f"The database stats is {stats.json()}")


import keras
import pandas
import tensorflow as tf

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.utils.np_utils import to_categorical

from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib

# use CPU only
# configCPU = tf.ConfigProto(device_count={'CPU': 1, 'GPU': 0})
# sess = tf.Session(config=configCPU)

sess = tf.Session()  # use GPU

keras.backend.set_session(sess)

#--------------Preprocessing-----------------#

data = pandas.read_csv('You_Data_Set', header=None)

feature = data.drop([48], 1)
label = data[48]

scaler = preprocessing.StandardScaler().fit(feature)
joblib.dump(scaler, 'scalers/scaler.pkl')
# clf = joblib.load('filename.pkl')
# feature = scaler.transform(feature)
feature = feature.values

# --------------Train----------------- #

train_x, test_x, train_y, test_y = train_test_split(feature, label, test_size=0.2)

train_y = to_categorical(train_y, 2)
test_y = to_categorical(test_y, 2)

validation_x = train_x[:2000]
train_x = train_x[2000:]
validation_y = train_y[:2000]
train_y = train_y[2000:]

model = Sequential()
model.add(Dense(128, activation='relu', input_dim=48))
model.add(Dense(256, activation='relu'))
# model.add(Dropout(0.25))
model.add(Dense(128, activation='relu'))
# model.add(Dropout(0.25))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.25))
# model.add(Dense(40, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.25))
# model.add(Lambda(lambda x: x / 10))
model.add(Dense(2, activation='softmax'))

opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
# model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) mean_squared_error
model.compile(optimizer='SGD', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(train_x, train_y,
                epochs=100,
                batch_size=1024,
                shuffle=True,
                validation_data=(validation_x, validation_y))

results = model.evaluate(test_x, test_y)
print("\n")
print(results)

model.save('models/FNN_Name.h5')


import numpy as np
from PyQt5.QtCore import pyqtSlot, QObject

class NPYHandler(QObject):

    def __init__(self):
        QObject.__init__(self)
        self.filePath = ''
        self.npyFile = None
        self.shape = []
        self.shapeString = ''
        self.length = 0

    @pyqtSlot(str,result=bool)
    def readNpyFile(self,filePath):
        self.filePath = filePath
        try:
            self.npyFile = np.load(self.filePath)
            self.shape = self.npyFile.shape
            self.updateShapeLength()
            self.updateShapeString()
            return True
        except BaseException as e:
            print('Error while reading .npy file: ' + str(e))
            return False

    def updateShapeString(self):
        #print(self.shape)
        if self.length == 0:
            return ''
        self.shapeString = str(self.shape[0])
        for i in range(self.length-1):
            self.shapeString += ' * ' + str(self.shape[i+1])
        #print(self.shapeString)

    def updateShapeLength(self):
        self.length = len(self.shape)

    @pyqtSlot(result=str)
    def getShapeSize(self):
        return self.shapeString

    @pyqtSlot(result=str)
    def getShapeLength(self):
        return str(self.length)

    @pyqtSlot(str,str,str,str,str,str,str,str,str)
    def draw(self,x,minX,maxX,y,minY,maxY,t,mint,maxt):
        print(x,minX,maxX,y,minY,maxY,t,mint,maxt)

npyHandler = NPYHandler()


# purpose of this program is to create a bot that texts me with the weather for each day.

from darksky.api import DarkSky, DarkSkyAsync
from darksky.types import languages, units, weather
import datetime
import smtplib as smp

API_KEY = 'your key'  # replace with your API key from darksky developers

darksky = DarkSky(API_KEY)

latitude = 38.998440

longitude = -77.293022

# cords for Harrisonburg
# 38.435982
# -78.879997

forecast = darksky.get_forecast(
    latitude, longitude,
    extend=False,
    lang=languages.ENGLISH,
    units=units.AUTO,
    exclude=[weather.MINUTELY, weather.ALERTS]
)

forecast.latitude
forecast.longitude
forecast.timezone

# this method builds the string for the daily weather report.
def daily_forecast():
    daily_summary = ''
    for item in forecast.daily.data:
        daily_summary = ('High is ' + str(item.temperature_high) + " but it will feel like " +
                         (str(item.apparent_temperature_high)))
        daily_summary += (' at ' + str(item.temperature_high_time.strftime("%I %p")))
        daily_summary += '\n'
        daily_summary += ('Low is ' + str(item.temperature_low) + " but it will feel like " +
                          (str(item.apparent_temperature_low)))
        daily_summary += ' at ' + str(item.temperature_low_time.strftime("%I %p"))
        daily_summary += '\n'
        daily_summary += 'With a ' + str(item.precip_probability) + ' chance of precipitation'
        break

    return daily_summary

# email logic. plugin your credentials in the place holder

smtp_obj = smp.SMTP('smtp.gmail.com', 587)
smtp_obj.starttls()
smtp_obj.login('user@gmail.com', 'password')
smtp_obj.sendmail('me@gmail.com', 'recipient@gmail.com', 'Subject: Weather. \n' + daily_forecast())
smtp_obj.quit()


import socket
import struct

class Channel:
    def __init__(self, name, mode, host="localhost", port=8075):
        assert "publish" == mode or "subscribe" == mode
        # Connect
        self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
        self.sock.connect( (host, port) )
        # Headers
        self.mode = mode
        self.write_header( "mode", mode )
        self.write_header( "channel", name )
        if "subscribe" == mode:
            self.write_header( "synchronous", "hotdog" )
        self.end_headers()

    def write_header( self, label, value ):
        self.sock.sendall( "%s: %s\n" % (label, value) )
    def end_headers( self ):
        self.sock.sendall( "\n" )

    def publish( self, data ):
        assert "publish" == self.mode
        self.sock.sendall( "size" +
                           struct.pack('!I', len(data)) +
                           "data" +
                           data )
    def recv( self ):
        sizebuf = self.sock.recv( 12 )
        size_tag = sizebuf[0:4]
        size_data = sizebuf[4:8]
        data_tag = sizebuf[8:12]
        if "size" != size_tag or "data" != data_tag:
            raise Exception( "Invalid ach tags -- size: %s and data: %s" % (size_tag, data_tag) )
        size = struct.unpack('!I', size_data)[0]
        return self.sock.recv( size )

    def next( self ):
        assert "subscribe" == self.mode
        self.sock.sendall( "next" )
        return self.recv()

    def last( self ):
        assert "subscribe" == self.mode
        self.sock.sendall( "last" )
        return self.recv()

    def close( self ):
        self.sock.close()


lst=[4,5,2,8,7,4,5,0,9,4,0,4]

set=set(lst)
list1=list(set)
dict={}
for i in list1:
    dict[i]=lst.count(i)
print(dict)
print(max(dict,key=dict.get))


import pandas as pd
import numpy as np

csv_path = 'contact_matrices/China_subnational_Shanghai_M_overall_contact_matrix_18.csv'
df = pd.read_csv(csv_path)

# -----save dataframe as csv----- #
csv_save_path = './data_copy.csv'
df.to_csv(csv_save_path, sep=',', index=False, header=True)

print(df)


from data_crawling import crawling
from summarizes import summarizes

def main(url):
    contents = []
    summary = []
    contents.append(crawling(url))

    for content in contents:
        summary.append(summarizes(content))

    return ''.join(summary[0])


__version__ = '1.6.0'
import os

from setuptools import setup, find_packages

# README read-in
from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()
# END README read-in

setup(
    name='skytemple-eventserver',
    version=__version__,
    packages=find_packages(),
    description='Websocket server that emits SkyTemple UI events',
    long_description=long_description,
    long_description_content_type='text/x-rst',
    url='https://github.com/SkyTemple/skytemple-eventserver/',
    install_requires=[],
    classifiers=[
        'Development Status :: 4 - Beta',
        'Programming Language :: Python',
        'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
        'Programming Language :: Python :: 3.10',
        'Programming Language :: Python :: 3.11',
    ],
    entry_points='''
        [skytemple.module]
        eventserver=skytemple_eventserver.module:EventserverModule
    '''
)


'''
Agent-based model based on War and Reason, BDM & Lalman, 1992.

The model consists of agents characterized by Capabilities and a risk acceptance
coefficient. Agents are connected via a network of alliances, which determines
their utility of challenging one another, as well as who will come to their aid.

'''
import random
import numpy as np
from scipy.stats import kendalltau
import pandas as pd
import networkx as nx

from consul.meta_model import MetaModel

from consul.model_types.utilities.game_tree import Tree, TreeNode
from consul.model_types.extensive_form import ExtensiveGameModel
from consul.model_types.extensive_form import ExtensiveGameAgent

from consul.agent_types.spe_agent import SPEAgent

class WarAgent(ExtensiveGameAgent):
    '''
    Base class.
    '''

    def __init__(self, capabilities, R, *args, **kwargs):
        '''
        New WarAgent
        '''
        self.capabilities = capabilities
        self.risk = (1-(R/3))/(1+(R/3))
        self.p_win = 0
        self.k_ab = 0
        self.total_utility = 0
        super().__init__(*args, **kwargs)

    def start_conflict(self, world, rival):
        '''
        Called at the beginning of a sub-model run to compute the utility and
        percieved probability of winning for the agent.
        '''

        # Convert network
        adj_mat = nx.to_numpy_matrix(world.network)
        # Build position-to-actor dictionaries
        pos_to_name = {}
        name_to_pos = {}
        for i, node in enumerate(world.network.nodes()):
            pos_to_name[i] = node
            name_to_pos[node] = i

        def get_tau(ccode1, ccode2):
            '''
            Closure to find K-Tau between two actors.
            '''
            x = np.array(adj_mat[name_to_pos[ccode1]])[0]
            y = np.array(adj_mat[name_to_pos[ccode2]])[0]
            return kendalltau(x, y)[0]

        # Find K-tau, similarity between alliance portfolios of self and rival
        k_ab = get_tau(self.name, rival.name)

        # Estimate probability of victory
        top = 0
        bottom = 0
        for actor in world.agents:
            k_ka = get_tau(self.name, actor.name)
            k_kb = get_tau(rival.name, actor.name)
            p = (k_ka - k_kb) / 2
            p *= np.exp(self.risk * (k_ka - k_kb))
            contribution = actor.capabilities * p
            if k_ka > k_kb:
                top += contribution
            bottom += abs(contribution)

        p_win = top / bottom

        # Set the variables at the agent level for utility calculations
        self.k_ab = k_ab
        self.p_win = p_win

class WarModel(ExtensiveGameModel):
    '''
    A single, two-player conflict game as described in BDM & Lalman, 1992.

    '''
    tree_file = "international_interaction_game.json"

    def __init__(self, agents):
        super().__init__(agents, tree=Tree.load_tree(self.tree_file))
        agent1, agent2 = agents

        # Calculate utilities
        # TODO: Mirror-imaging
        self.utilities = {node: {agent1.name: 0, agent2.name: 0}
                          for node in self.tree.get_final_nodes()}
        # Agent 1 utilities
        for agent in agents:
            u_sq = 2 - 4 * (0.5**agent.risk)
            ua_da = 2 - 4*((2 - (1 - agent.k_ab))/4)**agent.risk
            ua_db = 2 - 4*((2 - (agent.k_ab - 1))/4)**agent.risk
            u_acq_i = ua_da # This agent wins
            u_acq_j = ua_db # The other agent wins
            u_nego = agent.p_win * ua_da + (1-agent.p_win)*ua_db
            u_cap_j = ua_da - agent.p_win # The other agent surrenders
            u_cap_i = ua_db - (1 - agent.p_win)
            u_war = agent.p_win * (ua_da - agent.p_win - (1-agent.p_win))
            u_war += (1-agent.p_win) * (ua_db - agent.p_win - (1-agent.p_win))

            self.utilities["StatusQuo"][agent.name] = u_sq
            self.utilities["Negotiate_1"][agent.name] = u_nego
            self.utilities["Negotiate_2"][agent.name] = u_nego
            for label in ["War_A1", "War_B1", "War_A2", "War_B2"]:
                self.utilities[label][agent.name] = u_war
            prefix = "A" if agent is agent1 else "B"
            # The other guy winning
            self.utilities["Capitulate_"+prefix+"1"][agent.name] = u_acq_j
            self.utilities["Capitulate_"+prefix+"2"][agent.name] = u_acq_j
            self.utilities["Acquiesce_"+prefix][agent.name] = u_acq_j
            # This agent winning
            prefix = "B" if prefix=="A" else "A"
            self.utilities["Capitulate_"+prefix+"1"][agent.name] = u_acq_i
            self.utilities["Capitulate_"+prefix+"2"][agent.name] = u_acq_i
            self.utilities["Acquiesce_"+prefix][agent.name] = u_acq_i

    @classmethod
    def conflict_from_eugene(cls, eugene_row, agent_class, agent_args={},
                             read_utilities=False):
        '''
        Generate a conflict scenario from a row of EUGene data

        Args:
            eugene_row: A pandas row from EUGene
            agent_class: Class of agent to generate
            agent_args: Dictionary of global arguments to pass to all agents
                        (e.g. learning rate)

        '''

        agents = []
        for i in ['1', '2']:
            j = '2' if i == '1' else '1'
            # Create agents
            name = eugene_row['ccode' + i]
            nmc = eugene_row['cap_' + i]
            risk_aversion = eugene_row['riskT' + i]
            new_agent_args = dict(name=name,
                              capabilities=nmc,
                              R=risk_aversion)
            for arg, val in agent_args.items():
                new_agent_args[arg] = val
            a = agent_class(**new_agent_args)
            a.p_win = eugene_row["wrTp"+i+"win"]
            a.k_ab = eugene_row["tau_glob"]
            agents.append(a)
        model = cls(agents)
        if not read_utilities:
            return model
        # Fill in utilities

        util_keys = {"StatusQuo": "vsq",
                     "Negotiate_1": "neg",
                     "Negotiate_2": "neg",
                     "War_A1": "wr1",
                     "War_A2": "wr1",
                     "War_B1": "wr2",
                     "War_B2": "wr2",
                     "Capitulate_A1": "cp1",
                     "Capitulate_A2": "cp1",
                     "Capitulate_B1": "cp2",
                     "Capitulate_B2": "cp2",
                     "Acquiesce_A": "ac1",
                     "Acquiesce_B": "ac2" }

        for c, i in enumerate(['1', '2']):
            j = '2' if i == '1' else '1'
            name = agents[c].name
            prefix = "wrTu" + i # War and Reason U(...) using Tau
            for node, col in util_keys.items():
                model.utilities[node][name] = eugene_row[prefix+col]
        return model

class WarWorld(MetaModel):
    '''
    Basic MetaModel for creating a simulated world following the BDM & Lalman
    conflict decision methodology.
    '''

    model_class = WarModel
    agent_class = WarAgent
    agent_count = 20
    density = 0.2
    agent_args = {}
    network = nx.Graph()

    def __init__(self, agent_class, agent_count, density, agent_args={},
                 seed=None):
        '''
        Instantiate a new WarWorld model.

        Args:
            ...
        '''
        self.agent_class = agent_class
        self.agent_count = agent_count
        self.agent_args = agent_args
        self.density = density
        super().__init__(self.model_class, agents_per_model=2, seed=seed)
        self.log = []

    def make_agents(self):
        '''
        Create the agents randomly.
        '''
        self.agents = []
        for i in range(self.agent_count):
            nmc = np.random.lognormal(-2.5, 0.2) # Seems to work.
            risk_aversion = np.random.random() * 2 - 1 # Good enough?
            agent_args = dict(name=i,
                              capabilities=nmc,
                              R=risk_aversion)
            for arg, val in self.agent_args.items():
                agent_args[arg] = val
            a = self.agent_class(**agent_args)
            self.agents.append(a)

        # Create the network
        self.network = nx.erdos_renyi_graph(self.agent_count, self.density)

    def step(self):
        '''
        Pick two agents, and have them play the sub-model to completion.
        '''
        pass

class EUGeneWorld(MetaModel):
    '''
    MetaModel that runs a simulation using agents and utilities from the
    EUGene data.
    '''

    model_class = WarModel
    current_row = None

    def __init__(self, agent_class, eugene_data, agent_args={},
                 seed=None, copy_data=False, shuffle_rows=False):
        '''

        '''
        self.agent_class = agent_class
        if not copy_data:
            self.eugene_data = eugene_data
        else:
            self.eugene_data = eugene_data.copy()
        self.agent_args = agent_args
        super().__init__(self.model_class, agents_per_model=2, seed=seed)

    def _shuffle_data(self):
        '''
        Shuffle the order of rows in the EUGene data, preserving year ordering.
        '''
        new_data = []
        for group in self.eugene_data.groupby("year"):
            new_order = list(group[1].index)
            random.shuffle(new_order)
            temp = group[1].ix[new_order]
            new_data.append(temp)
        self.eugene_data = pd.concat(new_data)

    def make_agents(self):
        '''
        Create one agent per CCode appearing in the dataset.
        '''
        self.agents = {}
        all_ccodes = set(list(self.eugene_data.ccode1.unique()) +
                         list(self.eugene_data.ccode2.unique()))
        for ccode in all_ccodes:
            agent_args = dict(name=ccode,
                              capabilities=-9,
                              R=-9)
            for arg, val in self.agent_args.items():
                agent_args[arg] = val
            a = self.agent_class(**agent_args)
            self.agents[ccode] = a

    def step(self, row, return_result=False, training=False):
        '''
        Given a EUGene row, create a war game for it and run to completion.
        '''
        self.current_row = row
        ccode1 = row['ccode1']
        ccode2 = row['ccode2']

        agent1 = self.agents[ccode1]
        agent2 = self.agents[ccode2]

        # Reset best_moves
        agent1.start_conflict(self, agent2)
        agent2.start_conflict(self, agent1)

        model = self.model_class([agent1, agent2])

        util_keys = {"StatusQuo": "vsq",
                     "Negotiate_1": "neg",
                     "Negotiate_2": "neg",
                     "War_A1": "wr1",
                     "War_A2": "wr1",
                     "War_B1": "wr2",
                     "War_B2": "wr2",
                     "Capitulate_A1": "cp1",
                     "Capitulate_A2": "cp1",
                     "Capitulate_B1": "cp2",
                     "Capitulate_B2": "cp2",
                     "Acquiesce_A": "ac1",
                     "Acquiesce_B": "ac2" }

        for c, i in enumerate(['1', '2']):
            name = model.agents[c].name
            prefix = "wrTu" + i # War and Reason U(...) using Tau
            for node, col in util_keys.items():
                model.utilities[node][name] = row[prefix+col]

        if not training:
            model.run()
            if return_result:
                return model.current_node
            else:
                return model
        else:
            node_keys = {"eqTsq": ["StatusQuo"],
                         "eqTnego": ["Negotiate_1", "Negotiate_2"],
                         "eqTacqa": ["Acquiesce_A"],
                         "eqTacqb": ["Acquiesce_B"],
                         "eqTwara": ["War_A1"],
                         "eqTwarb": ["War_B1"]
                         }
            target_node = random.choice(node_keys[row["Equilibrium"]])
            for agent in model.agents:
                agent.train_to_outcome(model, target_node)

    def run(self, report_every=100000, model_assessors={}):
        '''
        Run a step for each row of the data.

        Args:
            output_each: Print current step once every this-many steps
                        (TODO: or TimeDelta)
            model_assessments: DataCollector-style names and functions to run
                               on each model object.
        '''

        model_assessments = {name: [] for name in model_assessors}
        count = 0
        for i, row in self.eugene_data.iterrows():
            if report_every is not None and count % report_every == 0:
                print(count)
            m = self.step(row, return_result=False)
            for name, assessor in model_assessors.items():
                r = assessor(m)
                model_assessments[name].append(r)
            count += 1
        for name, data in model_assessments.items():
            self.eugene_data[name] = data


import random
from numpy.random import sample
from scipy.stats import bernoulli
from scipy.stats import rv_discrete

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions.categorical import Categorical
from torch.distributions.beta import Beta
from torch.distributions import Normal

verbose = False
# 0 = innocente, 1 = colpevole

class Net(nn.Module):

    # funzioni attivazione, loss

    def __init__(self, input_size, hidden_size, n_actions, lr):
        super(Net, self).__init__()

        self.input_size = input_size
        self.hidden_size = hidden_size
        self.n_actions = n_actions #n_actions # 2: say i, say c
        self.output_size = 2

        self.policy = nn.Sequential(
            nn.Linear(self.input_size, self.hidden_size),
            nn.ReLU(),
            nn.Linear(self.hidden_size, self.output_size),
            nn.LogSoftmax(dim=1)
        )

        self.opt = torch.optim.Adam(self.parameters(), lr=lr)
        self.training = True

    def forward(self, truth):

        out = self.policy(truth)

        dist = Categorical(logits=out)

        if self.training:
            sampled = dist.sample()
        else:
            sampled = out.argmax(dim=1)

        logprobs = dist.log_prob(sampled)

        return dist.probs, sampled, logprobs

class Sender():

    def __init__(self, input_size, prior_i, lr = 0.1, beta=False):

        self.input_size = input_size
        self.prior_i = prior_i
        self.n_actions = 1 # number of samples out of the probability distribution
        self.actions_space = [i for i in range(0, 2)] # actions: [0, 1]
        self.p_cond = torch.zeros((2,2))

        self.model = Net(input_size=self.input_size, hidden_size=32, n_actions=self.n_actions, lr=lr)

        self.lr = lr
        self.baseline = 0.
        self.n_update = 0.
        self.ent_reg = 0
        self.training = True

    def loss(self, rew, logprobs):

        #loss = ( -rew.squeeze().detach() * logprobs ).mean()
        #print("rew=", rew)
        #print("logrpobs=", logprobs)
        #print(" -rew * logprobs =", -rew * logprobs )
        loss = ( -rew * logprobs ).mean()

        return loss

class DiscreteSender():

    def __init__(self, params):

        self.n_probs = params['num_probs']
        self.epsilon = params['epsilon0']
        self.eps_min = params['eps_min']
        self.eps_decay = params['eps_decay']
        self.lr = params['lr0']
        self.lr_decay = params['lr_decay']
        self.T = params['T']

        self.q = np.full((self.n_probs+1, self.n_probs+1), 2.) # le q rappresentano le probabilita di innocente per le due verita
        self.q_counts = np.zeros((self.n_probs+1, self.n_probs+1))
        self.rewards = np.zeros((params['num_probs']+1, params['num_probs']+1))
        self.p = np.ones((self.n_probs+1))
        self.p_cond = np.zeros((2,2))

        self.init_p()

    def init_p(self):
        for i in range(self.n_probs+1):
            self.p[i] = i/self.n_probs
        self.p[self.p == 0.] = 0.000001
        self.p[self.p == 1.] = 1.-0.000001

    def argmax(self, q_values):

        rand_generator = np.random.RandomState()

        top = float("-inf")
        ties = []

        for i in range(len(q_values)):
            if q_values[i] > top:
                top = q_values[i]
                ties = []

            if q_values[i] == top:
                ties.append(i)

        return rand_generator.choice(ties)

    def choose_signal(self):
        if (np.random.random() < self.epsilon):
            p_i_I_idx = random.randint(0, self.n_probs)
            p_i_C_idx = random.randint(0, self.n_probs)
        else:
            p_i_I_idx, p_i_C_idx = np.unravel_index(np.argmax(self.q, axis=None), self.q.shape) # row, column

        self.update_p_cond(p_i_I_idx, p_i_C_idx)

        self.q_counts[p_i_I_idx, p_i_C_idx] += 1

        return p_i_I_idx, p_i_C_idx

    def choose_signal_softmax(self):
        denom = np.sum(np.exp(self.q)/self.T)
        #print("denom=", denom)
        #print("np.exp(self.q)=", np.exp(self.q))
        probs = np.exp(self.q/self.T)/denom
        p_i_I_idx, p_i_C_idx = np.unravel_index(np.argmax(probs, axis=None), probs.shape)

        self.update_p_cond(p_i_I_idx, p_i_C_idx)

        self.q_counts[p_i_I_idx, p_i_C_idx] += 1

        return p_i_I_idx, p_i_C_idx

    def act(self, act):
        return self.p[:,act]

    def update_p_cond(self, p_i_I_idx, p_i_C_idx):
        self.p_cond[0,0] = self.p[p_i_I_idx]
        self.p_cond[1,0] = 1 - self.p_cond[0,0]
        self.p_cond[0,1] = self.p[p_i_C_idx]
        self.p_cond[1,1] = 1 - self.p_cond[0,1]

    def update(self, p_i_I_idx, p_i_C_idx, rew):
        if (self.q_counts[p_i_I_idx, p_i_C_idx] != 0):
            lr = 1./self.q_counts[p_i_I_idx, p_i_C_idx]
        else:
            lr = 0.9
        self.q[p_i_I_idx, p_i_C_idx] += lr*(rew - self.q[p_i_I_idx, p_i_C_idx])

    def sample_mex(self, idx_truth):
        return np.random.choice([0,1], 1, p=self.p_cond[:, idx_truth])[0]

class Nature():

    def __init__(self, p0):
        self.p0 = p0
        self.action_space = [i for i in range(0, 2)]
        self.p0 = 0.5

    def get_state_of_world(self, batch_size):

        idx_truth = torch.Tensor([np.random.choice(self.action_space, batch_size, p=np.array([self.p0, 1-self.p0]))]).to(torch.int64).squeeze()
        #truth = torch.reshape(idx_truth, (batch_size, idx_truth.shape[0]))
        truth = F.one_hot(idx_truth.unsqueeze(dim=0), num_classes=2)
        return truth, idx_truth

    def get_reward(self, real, predicted_r):

        send_rew = torch.zeros(real.shape)

        if (hasattr(real, '__len__') == 1):

            if (predicted_r == 1):
                return 1.
            elif (predicted_r == 0):
                return -1.

        for idx, _ in enumerate(real):
            if (predicted_r[idx] == 1):
                send_rew[idx] = 1.
            elif (predicted_r[idx] == 0):
                send_rew[idx] = -1.

        return send_rew

    def get_reward_fair(self, real, predicted_r):

        send_rew = torch.zeros(real.shape)
        #print("real=", real)
        #print("predicted=", predicted_r)

        if (hasattr(real, '__len__') == 0):
            if (real == predicted_r):
                return 1.
            else:
                return -1.

        for idx, _ in enumerate(real):

            if (real[idx] == predicted_r[idx]):
                send_rew[idx] = 1.
            else:
                send_rew[idx] = -1.

        return send_rew

    def get_reward_fair_discrete(self, real, predicted_r):

        send_rew = torch.zeros(real.shape)
        #print("real=", real)
        #print("predicted=", predicted_r)

        if (hasattr(real, '__len__') == 1):
            if (real == predicted_r):
                return 1.
            else:
                return -1.

        for idx, _ in enumerate(real):

            if (real[idx] == predicted_r[idx]):
                send_rew[idx] = 1.
            else:
                send_rew[idx] = -1.

        return send_rew

class Rational_Receiver():

    def __init__(self, prior_i):

        self.prior_i = prior_i # probab che sia innocente

    def predict(self, messages, p_conditioned_s):

        responses = torch.empty(messages.shape)
        p_i = p_conditioned_s[0,0]*self.prior_i + p_conditioned_s[0,1]*(1-self.prior_i)

        p_I_i = p_conditioned_s[0,0]*self.prior_i/p_i
        p_C_i = p_conditioned_s[0,1]*(1-self.prior_i)/p_i

        p_c = p_conditioned_s[1,0]*self.prior_i + p_conditioned_s[1,1]*(1-self.prior_i)

        p_C_c = p_conditioned_s[1,1]*(1-self.prior_i)/p_c
        p_I_c = p_conditioned_s[1,0]*self.prior_i/p_c

        #print("p_I_i=", p_I_i)
        #print("p_I_c=", p_I_c)
        #print("p_C_i=", p_C_i)
        #print("p_C_c=", p_C_c)

        if (p_i == 0):
            p_I_i = 0
            p_C_i = 0
        if (hasattr(messages, '__len__') == 0):

            if (messages == 0):
                #resp = np.random.choice([0,1], 1, p=[p_I_i, 1-p_I_i])[0]
                if (p_I_i > 0.5):
                    resp = 0
                else:
                    resp = 1

            else:
                #resp = np.random.choice([0,1], 1, p=[p_I_c, 1-p_I_c])[0]
                if (p_C_c > 0.5):
                    resp = 1
                else:
                    resp = 0

            return resp, p_I_i, p_I_c, p_C_i, p_C_c

        for idx, mex in enumerate(messages):

            mex = int(mex.detach().numpy())

            if (mex == 0):

                #responses[idx] = np.random.choice([0,1], 1, p=[p_I_i, 1-p_I_i])[0]

                if (p_I_i > 0.5):
                    responses[idx] = 0
                else:
                    responses[idx] = 1

            elif (mex == 1):

                #responses[idx] = np.random.choice([0,1], 1, p=[p_I_c, 1-p_I_c])[0]
                if (p_C_c > 0.5):
                    responses[idx] = 1
                else:
                    responses[idx] = 0

            else:
                print("Error!!")

        return responses, p_I_i, p_I_c, p_C_i, p_C_c


#!/usr/bin/env python3
import numpy as np
import cv2
from skimage import segmentation, color
from skimage.future import graph
from matplotlib import pyplot as plt
from skimage.exposure import rescale_intensity

np.seterr(divide='ignore', invalid='ignore')

def convolution(image, kernel):
    """
    Implementation of convolution operator.
    """
    (iH, iW) = image.shape  # image height and width
    (kH, kW) = kernel.shape  # kernel height and width
    pad = kH // 2  # tiling window
    # output image with image borders processing
    image = cv2.copyMakeBorder(image, pad, pad, pad, pad,
                               cv2.BORDER_REPLICATE)
    image_padded = np.zeros(shape=(iH + kH + pad, iW + kW + pad))
    image_padded[pad:-pad, pad:-pad] = image
    out = np.zeros(shape=image.shape)  # create output image as original image

    # sliding kernel over the image and compute convolution by element-wise multiplication
    for y in np.arange(pad, iH + pad):
        for x in np.arange(pad, iW + pad):
            for i in range(kH):
                for j in range(kW):
                    out[y, x] += image_padded[y + i, x + j] * kernel[i, j]

    # rescale the output image to be in the range [0, 255]
    out = rescale_intensity(out, in_range=(0, 255))
    out = (out * 255).astype("uint8")
    return out

def sobel_filter(data):
    """
    Implementation of the convolution of a Sobel filter 3x3 with the final goal of obtaining edges in images.
    """
    img = cv2.imread(data)
    grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    sobelX = np.array(([-1, 0, 1],
                       [-2, 0, 2],
                       [-1, 0, 1]), dtype="int")
    sobelY = np.array(([-1, -2, -1],
                       [0, 0, 0],
                       [1, 2, 1]), dtype="int")
    """
    # shifted kernel matrix to highlight edges in the opposite way
    sobelX = np.array(([+1, 0, -1],
                       [+2, 0, -2],
                       [+1, 0, -1]), dtype="int")
    sobelY = np.array(([+1, +2, +1],
                       [0, 0, 0],
                       [-1, -2, -1]), dtype="int")
    """

    Gx = convolution(grey_img, sobelX)  # horizontal gradient
    Gy = convolution(grey_img, sobelY)  # vertical gradient

    # OpenCV equivalent computations
    Gx_cv2 = cv2.Sobel(grey_img, cv2.CV_8U, 1, 0, ksize=3)
    Gy_cv2 = cv2.Sobel(grey_img, cv2.CV_8U, 0, 1, ksize=3)

    return grey_img, Gx, Gy, Gx_cv2, Gy_cv2

def normalized_cut(data):
    """
    Image segmentation through normalized cut.
    """
    img = cv2.imread(data)
    labels1 = segmentation.slic(img, compactness=30, n_segments=400)
    segmented_image = color.label2rgb(labels1, img, kind='avg')

    g = graph.rag_mean_color(img, labels1, mode='similarity')
    labels2 = graph.cut_normalized(labels1, g)
    cut_normalized_image = color.label2rgb(labels2, img, kind='avg')

    fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(6, 8))

    ax[0].imshow(segmented_image)
    ax[1].imshow(cut_normalized_image)
    return fig

def sift_descriptor(data):
    """
    Create the SIFT descriptor of an image.
    """
    image = cv2.imread(data)
    grey_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    sift = cv2.xfeatures2d.SIFT_create()
    kp, des = sift.detectAndCompute(grey_img, None)
    return kp, des


import card
import file_io

def main():
    conf = get_config_if_exist()
    while True:
        main_menu()

def main_menu():
    result = prompt_menu(['Learn', 'New Deck', 'Edit Deck', 'Quit'])
    if result == 0:
        learn()
    elif result == 1:
        new_deck()
    elif result == 2:
        edit_deck()
    else:
        quit()

def learn():
    d = get_deck()
    active_cards = filter(lambda x: x.is_reviewable(), d)
    for card in active_cards:
        print(card.description)
        show = confirm('Show response? (No quits learning session)',
                       enter_is_yes=True)
        if not show:
            return
        print(card.response)
        result = prompt_menu(['Terrible', 'Tip of my tongue', 'Good', 'Easy'],
                             "How'd you do?")
        assert result in range(4)
        card.rate_card(result)
        print('\n' * 200, end='')
        file_io.write_deck(d)

def new_deck():
    print('Enter deck name.')
    name = input('> ')
    if not name:
        print('Cancelling.')

def get_deck():
    decks = file_io.get_deck_names()
    if not decks:
        return None
    result = prompt_menu(decks)
    return file_io.read_deck(title=decks[result])

def edit_deck():
    deck = get_deck()

    result = prompt_menu(['List cards', 'Edit card', 'Add cards'])
    if result == 0:
        for i, card in enumerate(deck):
            print('Description {}: {}'.format(i, card.description))
            print('Response    {}: {}'.format(i, card.response))
    if result == 1:
        pass
    if result == 2:
        add_cards(deck)

def add_cards(deck):
    while True:
        d = input('Card {} Description > '.format(len(deck)))
        if not d:
            print('Finished adding cards.')
        r = input('Card {} Response    > '.format(len(deck)))
        deck.append(card.Card(d, r))

def get_config_if_exist():
    if file_io.config_file_exist():
        config = file_io.read_config()
        return config

def prompt_menu(items, message='Select one of the following options.'):
    while True:
        print(message)
        for idx, item in enumerate(items):
            print('{}: {}'.format(idx, item))
        input_str = input('> ')
        try:
            num = int(input_str)
            if 0 <= num < len(items):
                return num
        except ValueError:
            pass
        print('Invalid input')

def prompt_with_default(message, default):
    print(message)
    result = input('[Default={}] > '.format(default))
    if not result:
        return default
    return result

def confirm(message, enter_is_yes=False):
    print(message)
    result = ''
    while not result or result[0] not in ['y', 'n']:
        result = input('Y/N: ').lower()
        if enter_is_yes and result == '':
            return True
    return True if result[0] == 'y' else False

if __name__ == '__main__':
    main()


'''
Il seguente script verifica l'esistenza di una stringa all'interno di un file

str -> contiene la stringa da cercare
it -> il nome del file all'interno del quale cercare riga per riga
'''

str= "pop"  # nel caso di ruzzle, str deve contenere l'attributo di istanza
it = 'words.italian.txt' # è possibile aggiungere tante variabili quanti file di lingua si posseggono

f = open(it, 'r')
line = f.readline()

for line in f:  # per ogni riga del file vengono eseguite le righe di codice che seguono
#    print(str)
    if str == line[:-1]:  #bisogna eliminare l'ultimo carattere dalla parola contenuta nella riga del file
        print("vero")


import ast
import difflib
import importlib.util
import re
from collections import Counter, defaultdict
from importlib import import_module
from pathlib import Path
from typing import Dict, List, Optional, Pattern, Tuple

from robot.api import Token

try:
    from robot.api.parsing import Variable
except ImportError:
    from robot.parsing.model.statements import Variable

from robot.variables.search import VariableIterator
from robot.version import VERSION as RF_VERSION

from robocop.exceptions import InvalidExternalCheckerError
from robocop.utils.version_matching import Version
from robocop.version import __version__

ROBOT_VERSION = Version(RF_VERSION)
ROBOT_WITH_LANG = Version("6.0")

def rf_supports_lang():
    return ROBOT_VERSION >= ROBOT_WITH_LANG

def modules_in_current_dir(path, module_name):
    """Yield modules inside `path` parent directory"""
    yield from modules_from_path(Path(path).parent, module_name)

def modules_from_paths(paths):
    for path in paths:
        path_object = Path(path)
        if path_object.exists():
            if path_object.is_dir():
                yield from modules_from_paths(
                    [file for file in path_object.iterdir() if "__pycache__" not in str(file)]
                )
            else:
                spec = importlib.util.spec_from_file_location(path_object.stem, path_object)
                mod = importlib.util.module_from_spec(spec)

                spec.loader.exec_module(mod)
                yield mod
        else:
            # if it's not physical path, try to import from installed modules
            try:
                parent_name, *lib_name = path.rsplit(".", 1)
                if lib_name:
                    parent = __import__(parent_name, fromlist=lib_name)
                    mod = getattr(parent, "".join(lib_name))
                else:
                    mod = __import__(parent_name, None)
                yield mod
            except ImportError:
                raise InvalidExternalCheckerError(path) from None

def modules_from_path(path, module_name=None, relative="."):
    """Traverse current directory and yield python files imported as module"""
    if path.is_file():
        yield import_module(relative + path.stem, module_name)
    elif path.is_dir():
        for file in path.iterdir():
            if "__pycache__" in str(file):
                continue
            if file.suffix == ".py" and file.stem != "__init__":
                yield from modules_from_path(file, module_name, relative)

def normalize_robot_name(name: str, remove_prefix: Optional[str] = None) -> str:
    name = name.replace(" ", "").replace("_", "").lower() if name else ""
    if remove_prefix:
        return name[name.startswith(remove_prefix) and len(remove_prefix) :]
    return name

def normalize_robot_var_name(name: str) -> str:
    return normalize_robot_name(name)[2:-1] if name else ""

def remove_nested_variables(var_name):
    for prefix, match, suffix in VariableIterator(var_name, ignore_errors=True):
        if match:  # if nested variable exists
            # take what surrounds it and run the check again
            var_name = remove_nested_variables(prefix + suffix)
            break
    return var_name.strip()

def keyword_col(node) -> int:
    return token_col(node, Token.KEYWORD)

def token_col(node, *token_type) -> int:
    if ROBOT_VERSION.major == 3:
        for tok_type in token_type:
            token = node.get_token(tok_type)
            if token is not None:
                break
        else:
            return 1
    else:
        token = node.get_token(*token_type)

    if token is None:
        return 1
    return token.col_offset + 1

def issues_to_lsp_diagnostic(issues) -> List[Dict]:
    return [
        {
            "range": {
                "start": {
                    "line": max(0, issue.line - 1),
                    "character": max(0, issue.col - 1),
                },
                "end": {
                    "line": max(0, issue.end_line - 1),
                    "character": max(0, issue.end_col - 1),
                },
            },
            "severity": issue.severity.diag_severity(),
            "code": issue.rule_id,
            "source": "robocop",
            "message": issue.desc,
            "codeDescription": {"href": f"https://robocop.readthedocs.io/en/{__version__}/rules.html#{issue.name}"},
        }
        for issue in issues
    ]

def str2bool(v):
    if isinstance(v, bool):
        return v
    return v.lower() in ("yes", "true", "1")

class AssignmentTypeDetector(ast.NodeVisitor):
    """Visitor for counting number and type of assignments"""

    def __init__(self):
        self.keyword_sign_counter = Counter()
        self.keyword_most_common = None
        self.variables_sign_counter = Counter()
        self.variables_most_common = None

    def visit_File(self, node):  # noqa
        self.generic_visit(node)
        if len(self.keyword_sign_counter) >= 2:
            self.keyword_most_common = self.keyword_sign_counter.most_common(1)[0][0]
        if len(self.variables_sign_counter) >= 2:
            self.variables_most_common = self.variables_sign_counter.most_common(1)[0][0]

    def visit_KeywordCall(self, node):  # noqa
        if node.assign:  # if keyword returns any value
            sign = self.get_assignment_sign(node.assign[-1])
            self.keyword_sign_counter[sign] += 1

    def visit_VariableSection(self, node):  # noqa
        for child in node.body:
            if not isinstance(child, Variable):
                continue
            var_token = child.get_token(Token.VARIABLE)
            sign = self.get_assignment_sign(var_token.value)
            self.variables_sign_counter[sign] += 1
        return node

    @staticmethod
    def get_assignment_sign(token_value):
        return token_value[token_value.find("}") + 1 :]

def parse_assignment_sign_type(value: str) -> str:
    types = {
        "none": "",
        "equal_sign": "=",
        "space_and_equal_sign": " =",
        "autodetect": "autodetect",
    }
    if value not in types:
        raise ValueError(
            f"Expected one of ('none', 'equal_sign', 'space_and_equal_sign', 'autodetect') but got '{value}' instead"
        )
    return types[value]

class RecommendationFinder:
    def find_similar(self, name, candidates):
        norm_name = self.normalize(name)
        norm_cand = self.get_normalized_candidates(candidates)
        matches = []
        for norm in norm_name:
            matches += self.find(norm, norm_cand.keys())
        if not matches:
            return ""
        matches = self.get_original_candidates(matches, norm_cand)
        suggestion = "Did you mean:\n"
        suggestion += "\n".join(f"    {match}" for match in matches)
        return suggestion

    def find(self, name, candidates, max_matches=5):
        """Return a list of close matches to `name` from `candidates`."""
        if not name or not candidates:
            return []
        cutoff = self._calculate_cutoff(name)
        return difflib.get_close_matches(name, candidates, n=max_matches, cutoff=cutoff)

    @staticmethod
    def _calculate_cutoff(string, min_cutoff=0.5, max_cutoff=0.85, step=0.03):
        """The longer the string the bigger required cutoff."""
        cutoff = min_cutoff + len(string) * step
        return min(cutoff, max_cutoff)

    @staticmethod
    def normalize(name):
        """
        Return tuple where first element is string created from sorted words in name,
        and second element is name without `-` and `_`.
        """
        norm = re.split("[-_ ]+", name)
        return " ".join(sorted(norm)), name.replace("-", "").replace("_", "")

    @staticmethod
    def get_original_candidates(candidates, norm_candidates):
        """Map found normalized candidates to unique original candidates."""
        return sorted(list(set(c for cand in candidates for c in norm_candidates[cand])))

    def get_normalized_candidates(self, candidates):
        """
        Thanks for normalizing and sorting we can find cases like this-is, thisis, this-is1 instead of is-this.
        Normalized names form dictionary that point to original names - we're using list because several names can
        have one common normalized name.
        Different normalization methods try to imitate possible mistakes done when typing name - different order,
        missing `-` etc.
        """
        norm = defaultdict(list)
        for cand in candidates:
            for norm_cand in self.normalize(cand):
                norm[norm_cand].append(cand)
        return norm

class TestTemplateFinder(ast.NodeVisitor):
    def __init__(self):
        self.templated = False

    def visit_TestTemplate(self, node):  # noqa
        self.templated = bool(node.value)

def is_suite_templated(model):
    finder = TestTemplateFinder()
    finder.visit(model)
    return finder.templated

def next_char_is(string: str, i: int, char: str) -> bool:
    if not i < len(string) - 1:
        return False
    return string[i + 1] == char

def remove_robot_vars(name: str) -> str:
    var_start = set("$@%&")
    brackets = 0
    open_bracket, close_bracket = "", ""
    replaced = ""
    index = 0
    while index < len(name):
        if brackets:
            if name[index] == open_bracket:
                brackets += 1
            elif name[index] == close_bracket:
                brackets -= 1
            # check if next chars are not ['key']
            if not brackets and next_char_is(name, index, "["):
                brackets += 1
                index += 1
                open_bracket, close_bracket = "[", "]"
        # it looks for $ (or other var starter) and then check if next char is { and previous is not escape \
        elif name[index] in var_start and next_char_is(name, index, "{") and not (index and name[index - 1] == "\\"):
            open_bracket = "{"
            close_bracket = "}"
            brackets += 1
            index += 1
        else:
            replaced += name[index]
        index += 1
    return replaced

def find_robot_vars(name: str) -> List[Tuple[int, int]]:
    """return list of tuples with (start, end) pos of vars in name"""
    var_start = set("$@%&")
    brackets = 0
    index = 0
    start = -1
    variables = []
    while index < len(name):
        if brackets:
            if name[index] == "{":
                brackets += 1
            elif name[index] == "}":
                brackets -= 1
                if not brackets:
                    variables.append((start, index + 1))
        # it looks for $ (or other var starter) and then check if next char is { and previous is not escape \
        elif name[index] in var_start and next_char_is(name, index, "{") and not (index and name[index - 1] == "\\"):
            brackets += 1
            start = index
            index += 1
        index += 1
    return variables

def pattern_type(value: str) -> Pattern:
    try:
        pattern = re.compile(value)
    except re.error as err:
        raise ValueError(f"Invalid regex pattern: {err}")
    return pattern

def get_section_name(node):
    if not node.header:
        #  Lines before first section are considered to be in `*** Comments ***` section even if header name is missing
        return "*** Comments ***"
    for token in node.header.data_tokens:
        if ROBOT_VERSION.major > 3:
            if token.type in node.header.handles_types:
                return token.value
        elif "HEADER" in token.type:
            return token.value
    return ""

def get_errors(node):
    if ROBOT_VERSION.major == 3:
        return [node.error] if node.error else []
    return node.errors