from utils.manage_import import *
from utils.data_process import load_npz
from utils.clf_analysis import load_data
from utils.losses import LogLoss, ItselfLoss

class DCGenerator(nn.Module):
    def __init__(self, latent_dim, input_shape):
        """
        Args:
            input_shape: (C,W), 例如(1,28)
            latent_dim: 默认值100
        """
        super(DCGenerator, self).__init__()
        self.input_shape = input_shape
        self.latent_dim = latent_dim
        self.init_size = int(np.ceil(self.input_shape[1]/2**2))
        self.channel = self.input_shape[0]
        self.relu_slope = 0.2
        self.fc = nn.Sequential(
            nn.Linear(self.latent_dim, 128*self.init_size)
        )
        self.conv_blocks = nn.Sequential(
            nn.BatchNorm1d(128),
            nn.Upsample(scale_factor=2),
            nn.Conv1d(128, 128, 3, stride=1, padding=1),
            nn.BatchNorm1d(128),
            nn.LeakyReLU(self.relu_slope, inplace=True),
            nn.Upsample(scale_factor=2),
            nn.Conv1d(128, 64, 3, stride=1, padding=1),
            nn.BatchNorm1d(64),
            nn.LeakyReLU(self.relu_slope, inplace=True),
            nn.Conv1d(64, self.channel, 3, stride=1, padding=1),
            nn.Tanh(),
        )

    def forward(self, z):
        out = self.fc(z)
        out = out.view(out.size(0), 128, self.init_size)
        x = self.conv_blocks(out)
        return x

class DCGenerator2(nn.Module):
    def __init__(self, latent_dim, input_shape):
        super(DCGenerator2, self).__init__()
        self.input_shape = input_shape
        self.latent_dim = latent_dim
        self.init_size = int(np.ceil(self.input_shape[1]/2**2))
        self.channel = self.input_shape[0]
        self.relu_slope = 0.2
        self.fc = nn.Sequential(
            nn.Linear(self.latent_dim, 128*self.init_size),
            # nn.LeakyReLU(self.relu_slope),
        )
        self.conv_blocks = nn.Sequential(
            nn.Upsample(scale_factor=2),
            nn.Conv1d(128, 128, 3, stride=1, padding=1),
            # nn.BatchNorm1d(128),
            nn.LeakyReLU(self.relu_slope, inplace=True),
            nn.Upsample(scale_factor=2),
            nn.Conv1d(128, 64, 3, stride=1, padding=1),
            # nn.BatchNorm1d(64),
            nn.LeakyReLU(self.relu_slope, inplace=True),
            nn.Conv1d(64, self.channel, 3, stride=1, padding=1),
            nn.Tanh(),
        )

    def forward(self, z):
        out = self.fc(z)
        out = out.view(out.size(0), 128, self.init_size)
        x = self.conv_blocks(out)
        return x

class DCDiscriminator(nn.Module):
    def __init__(self, input_shape):
        super(DCDiscriminator, self).__init__()
        self.input_shape = input_shape
        self.channel = self.input_shape[0]
        self.relu_slope = 0.2
        self.drop_rate = 0.5

        def dis_block(in_channel, out_channel, bn=True):
            layers = [
                nn.Conv1d(in_channel, out_channel, 3, 2, 1),
                nn.BatchNorm1d(out_channel),
                # nn.InstanceNorm1d(out_channel),
                nn.LeakyReLU(self.relu_slope),
                nn.Dropout(self.drop_rate),
            ]
            if not bn:
                layers.pop(1)
            return layers

        self.model = nn.Sequential(
            *dis_block(self.channel, 16, bn=False),
            *dis_block(16, 32),
            *dis_block(32, 64),
            *dis_block(64, 128),
        )
        ds_size = int(np.ceil(self.input_shape[1] / 2 ** 4))
        self.fc = nn.Sequential(
            nn.Linear(128*ds_size, 64),
            nn.LeakyReLU(self.relu_slope),
            nn.Dropout(self.drop_rate),
        )
        self.fc2 = nn.Sequential(
            nn.Linear(64, 1),
            nn.Sigmoid(),
        )

    def forward(self, x, feature_matching=False):
        if x.dim() == 2:
            x = x.view(x.size(0), 1, -1)
        out = self.model(x)
        out = out.view(out.size(0), -1)
        feature = self.fc(out)
        validity = self.fc2(feature)
        if feature_matching:
            return validity, feature
        else:
            return validity

class DCGAN(object):
    def __init__(self, input_shape=(1, 500), latent_dim=100,
                 lr=2e-4, optimizer=torch.optim.Adam,
                 opt_args={'betas': (0.5, 0.999)},
                 noise_type='normal'):
        super().__init__()
        self.input_shape = input_shape
        self.latent_dim = latent_dim
        self.lr = lr
        self.optimizer = optimizer
        self.opt_args = opt_args
        self.device = torch.device(
            "cuda:0" if torch.cuda.is_available() else "cpu")
        self.criterion = nn.BCELoss().to(self.device)
        self.criterion_log = LogLoss().to(self.device)
        self.criterion_itself = ItselfLoss().to(self.device)
        self.noise_type = noise_type
        self.generator = None
        self.discriminator = None
        self.optimizer_g = None
        self.optimizer_d = None
        self.history = None
        self.models = dict()
        self.img_path = './image/'
        self.save_path = './ckpt/'

    def build_model(self, gen_cls, dis_cls, gen_args={}, dis_args={}):
        self.generator = gen_cls(self.latent_dim,
                                 self.input_shape,
                                 **gen_args).to(self.device)
        self.discriminator = dis_cls(self.input_shape,
                                     **dis_args).to(self.device)
        self.optimizer_g = self.optimizer(self.generator.parameters(),
                                          lr=self.lr, **self.opt_args)
        self.optimizer_d = self.optimizer(self.discriminator.parameters(),
                                          lr=self.lr, **self.opt_args)
        # 初始化网络权重
        # self.generator.apply(self.weights_init)
        # self.discriminator.apply(self.weights_init)
        self.models = {
            'generator': self.generator,
            'discriminator': self.discriminator,
        }

    @staticmethod
    def weights_init(m):
        classname = m.__class__.__name__
        if classname.find('Conv') != -1:
            m.weight.data.normal_(0.0, 0.01)
        elif classname.find('BatchNorm') != -1:
            m.weight.data.normal_(1.0, 0.02)
            m.bias.data.fill_(0)
        elif classname.find('Linear') != -1:
            m.bias.data.fill_(0)

    def train(self, name='ECG200', batch_size=64,
              epochs=1e3, sample_cycle=100, target=None):
        if not self.generator:
            raise ValueError("model doesn't be initialized,\
                             please call build_model() before train()")
        self.history = defaultdict(list)
        loader = self.load_data(name, batch_size, target)
        for epoch in range(epochs):
            local_history = self.train_on_epoch(loader)
            self.update_history(local_history)
            if (epoch + 1) % sample_cycle == 0 or (epoch + 1) == epochs:
                # self.sample(epoch + 1)
                # self.save_checkpoint(name=target)
                self.print_local_history(
                    epoch+1, local_history, max_epoch=epochs)
        self.save_checkpoint(name=target)

    def train_on_epoch(self, loader):
        local_history = dict()
        tmp_history = defaultdict(list)
        EPS = 0
        for x_batch, _ in loader:
            # 添加高斯噪声以防止过拟合
            # x_batch = x_batch + np.random.normal(0.0, 0.1)
            x_batch = x_batch.to(self.device)
            batch_size = x_batch.size(0)
            z = self.gen_noise(0, 1, (batch_size, self.latent_dim))
            x_gen = self.generator(z)
            real = self.gen_tensor(np.ones([batch_size, 1]))
            fake = self.gen_tensor(np.zeros([batch_size, 1]))
            # 训练判别器
            self.optimizer_d.zero_grad()
            d_loss = self.criterion_log(self.discriminator(x_batch)+EPS)\
                + self.criterion_log(1-self.discriminator(x_gen.detach())+EPS)
            # d_loss = self.criterion_itself(self.discriminator(x_batch))\
            #     + self.criterion_itself(self.discriminator(x_gen.detach()))
            d_loss.backward()
            self.optimizer_d.step()
            # 训练生成器
            self.optimizer_g.zero_grad()
            g_loss = self.criterion_log(self.discriminator(x_gen)+EPS)
            # g_loss = self.criterion_itself(self.discriminator(x_gen))
            g_loss.backward()
            self.optimizer_g.step()
            tmp_history['d_loss'].append(d_loss.item())
            tmp_history['g_loss'].append(g_loss.item())
        local_history['d_loss'] = np.mean(tmp_history['d_loss'])
        local_history['g_loss'] = np.mean(tmp_history['g_loss'])
        return local_history

    def sample(self, epoch, r=5, c=5):
        os.makedirs(self.img_path, exist_ok=True)
        noise = self.gen_noise(0, 1, (r*c, self.latent_dim))
        x_fake = self.generator(noise)
        x_fake = x_fake.data.cpu().numpy().reshape(x_fake.shape[0], -1)
        plt.rcParams['xtick.bottom'] = False
        plt.rcParams['xtick.top'] = False
        plt.rcParams['ytick.left'] = False
        plt.rcParams['ytick.right'] = False
        fig, axes = plt.subplots(r, c)
        count = 0
        for i in range(r):
            for j in range(c):
                axes[i, j].plot(x_fake[count, :])
                axes[i, j].set_xticks([])
                axes[i, j].set_yticks([])
                count += 1
        fig.savefig(self.img_path+'%s.png' % epoch)
        plt.close()

    def save_checkpoint(self, name=None):
        if name is None:
            name = '{:s}.pkl'.format(self.__class__.__name__)
        else:
            name = '{:s}_{:d}.pkl'.format(self.__class__.__name__, name)
        os.makedirs(self.save_path, exist_ok=True)
        model_state = dict()
        for k, v in self.models.items():
            model_state[k] = v.state_dict()
        model_state['history'] = self.history
        torch.save(model_state, self.save_path+name)

    def load_model(self, path=None):
        if not self.generator:
            raise NameError("model doesn't be initialized")
        path = self.save_path + \
            '{:s}.pkl'.format(self.__class__.__name__) if not path else path
        states = torch.load(path)
        for k, v in self.models.items():
            v.load_state_dict(states[k])

    def update_history(self, local_history):
        for k, v in local_history.items():
            self.history[k].append(v)

    def plot_history(self, name=None):
        os.makedirs(self.img_path, exist_ok=True)
        r = len(self.history.keys())
        plt.figure(figsize=(15, int(r*3)))
        for i, k in enumerate(self.history.keys()):
            plt.subplot(r, 1, i+1)
            plt.plot(range(1, len(self.history[k]) + 1), self.history[k])
            plt.title(k)
        if name is None:
            plt.savefig(self.img_path +
                        'history_{:s}.png'.format(self.__class__.__name__))
        else:
            plt.savefig(
                self.img_path +
                'history_{:s}_{:d}.png'.format(self.__class__.__name__, name))
        plt.close()

    @staticmethod
    def print_local_history(epoch, local_history, max_epoch=10000):
        num = len(str(max_epoch))
        s = 'Epoch-{:0>{}d}:  '.format(epoch, num)
        for k, v in local_history.items():
            s = s + '{}={:.4f}  '.format(k, np.mean(v))
        print(s)

    def gen_tensor(self, x, astype='float', requires_grad=False):
        if isinstance(x, torch.Tensor):
            t = x.clone().requires_grad_(requires_grad)
        else:
            t = torch.tensor(x, requires_grad=requires_grad)
        if astype == 'float':
            t = t.float()
        elif astype == 'long':
            t = t.long()
        else:
            raise ValueError('input correct astype')
        return t.to(self.device)

    # 统一产生噪声
    def gen_noise(self, *args, **kws):
        tmp = None
        if self.noise_type == 'normal':
            tmp = np.random.normal(*args, **kws)
        elif self.noise_type == 'uniform':
            tmp = np.random.uniform(*args, **kws)
        return self.gen_tensor(tmp)

    # 从不同的数据集加载数据
    def load_data(self, name, batch_size, target, npz=True):
        dataset = None
        if npz:
            dataset = self.load_npz(name, target)
        loader = Data.DataLoader(dataset,
                                 batch_size=batch_size,
                                 shuffle=True)
        return loader

    def load_npz(self, name='ECG200', target=None):
        # data, label = load_npz(name)
        (data, label), (_, _) = load_data(name)
        if not target:
            inx = range(len(data))
        else:
            inx = label == target
        dataset = Data.TensorDataset(
            torch.tensor(data[inx], dtype=torch.float32),
            torch.tensor(label[inx], dtype=torch.long))
        return dataset


import re
import subprocess
import sys

from PySide2.QtCore import QObject, Signal
from PySide2.QtWidgets import QApplication, QMainWindow, QMessageBox, QListWidgetItem, QMenu, QAction
from netifaces import ifaddresses, interfaces, AF_INET

from ui import Ui_MainWindow

class MySignals(QObject):
    signal = Signal(object)

class QMain(QMainWindow):

    def __init__(self):
        super(QMain, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # 过滤掉未获取到IP地址的适配器
        self.ip_address_list = sorted([a for a in self.get_all_ip_address() if a[:3] != '169'])
        self.ui.listenaddress.addItems(self.ip_address_list)
        self.ui.connectaddress.addItems(self.ip_address_list)
        self.update_network_list = MySignals()
        # 信号连接槽，需要更新端口转发地址时发送信号触发函数调用
        self.update_network_list.signal.connect(self.get_all_network)  # noqa
        self.get_all_network()
        self.check_service_status()

    @staticmethod
    def get_all_ip_address():
        """
        获取本机所有的IP地址

        :return: IP地址列表
        """
        return [ifaddresses(a)[AF_INET][0]['addr'] for a in interfaces() if (AF_INET in ifaddresses(a))]

    def start(self):
        """
        运行程序

        :return: None
        """
        network = self.ui.network.currentText()
        listen_address = self.ui.listenaddress.currentText()
        listen_port = self.ui.listenport.text()
        connect_address = self.ui.connectaddress.currentText()
        connect_port = self.ui.connectport.text()
        if self.check_ip_port(listen_address, listen_port, connect_address, connect_port):
            command = 'netsh interface portproxy add %s listenaddress=%s listenport=%s connectaddress=%s connectport=%s'  # noqa
            command = command % (network, listen_address, listen_port, connect_address, connect_port)
            self.run_and_check_result(command)
            # 执行命令结束发出信号更新network_list
            self.update_network_list.signal.emit(True)  # noqa

    def check_ip_port(self, listen_address, listen_port, connect_address, connect_port):
        """
        检测IP地址和端口是否合法

        :param listen_address: 监听地址
        :param listen_port: 监听端口
        :param connect_address: 连接地址
        :param connect_port: 连接端口
        :return: bool
        """
        if not (listen_address and listen_port and connect_address and connect_port):
            text = ''
            if not listen_address:
                text += '监听地址，'
            if not listen_port:
                text += '监听端口，'
            if not connect_address:
                text += '连接地址，'
            if not connect_port:
                text += '连接端口，'
            QMessageBox.warning(self, '提示', f'{text[:-1]}不能为空！', QMessageBox.Ok, QMessageBox.Ok)
            return False
        rule_ipaddress = re.compile(r'^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$')
        rule_port = re.compile(
            r'^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-9][0-9][0-9][0-9]$)')
        if rule_ipaddress.match(listen_address) and rule_ipaddress.match(connect_address) and \
                rule_port.match(listen_port) and rule_port.match(connect_port):
            if 1 <= int(listen_port) <= 65535 and 1 <= int(connect_port) <= 65535:
                return True
            else:
                if not (1 <= int(listen_port) <= 65535):
                    QMessageBox.warning(self, '提示', '监听端口不合法！', QMessageBox.Ok, QMessageBox.Ok)
                    return False
                if not (1 <= int(connect_port) <= 65535):
                    QMessageBox.warning(self, '提示', '连接端口不合法！', QMessageBox.Ok, QMessageBox.Ok)
                    return False
        else:
            text = ''
            if not rule_ipaddress.match(listen_address):
                text += '监听地址，'
            if not rule_ipaddress.match(connect_address):
                text += '连接地址，'
            if not rule_port.match(listen_port):
                text += '监听端口，'
            if not rule_port.match(connect_port):
                text += '连接端口，'
            QMessageBox.warning(self, '提示', f'{text[:-1]}不合法！', QMessageBox.Ok, QMessageBox.Ok)
            return False

    def run_and_check_result(self, command):
        """
        运行程序并且检测返回结果

        :param command: 需要运行的命令
        :return: None
        """
        stdout_data, stderr_data = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                    stdin=subprocess.PIPE,  # GUI程序中记得设置标准输入
                                                    shell=True).communicate()
        stdout_data = stdout_data.decode('gbk')
        stderr_data = stderr_data.decode('gbk')
        if stdout_data == '请求的操作需要提升(作为管理员运行)。\n\r\n':
            QMessageBox.warning(self, '提示', '请使用管理员权限运行程序！', QMessageBox.Ok, QMessageBox.Ok)
        elif stdout_data == '\r\n' and stderr_data == '':
            QMessageBox.information(self, '提示', 'OK!', QMessageBox.Ok, QMessageBox.Ok)
        else:
            QMessageBox.information(self, '提示', f'{stdout_data}\r\n{stderr_data}', QMessageBox.Ok, QMessageBox.Ok)

    def edit_item(self, item: QListWidgetItem):
        """
        编辑IP和端口

        :param item: 选中的item
        :return: None
        """
        src, des = item.text().split(' -> ')
        src_ip, src_port = src.split(':')
        des_ip, des_port = des.split(':')
        self.ui.listenaddress.setCurrentText(src_ip)
        self.ui.listenport.setText(src_port)
        self.ui.connectaddress.setCurrentText(des_ip)
        self.ui.connectport.setText(des_port)

    def get_all_network(self):
        """
        获取所有的网络适配器中包含的地址

        :return: None
        """
        # 先清空之前的值，再更新
        self.ui.network_list.clear()
        parent = r'(\S+?)\s+?(\d+?) \s+?(\S+?)\s+?(\d+?)\s+?'
        get_ip_port = re.compile(parent, re.VERBOSE)
        command = 'netsh interface portproxy show all'  # noqa
        stdout_data, stderr_data = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                    stdin=subprocess.PIPE,
                                                    shell=True).communicate()
        stdout_data, stderr_data = stdout_data.decode('gbk'), stderr_data.decode('gbk')
        result = re.findall(get_ip_port, stdout_data)
        for a in result:
            self.ui.network_list.addItem(f'{a[0]}:{a[1]} -> {a[2]}:{a[3]}')

    def my_listwidget_context(self, position):  # noqa
        """
        右键菜单

        :param position: 选中的条目索引
        :return: None
        """
        # 只有在选中item时才会出现菜单
        if self.ui.network_list.itemAt(position):
            menu = QMenu()
            del_act = QAction("删除", self)
            del_act.triggered.connect(self.del_network)  # noqa
            menu.addAction(del_act)
            menu.exec_(self.ui.network_list.mapToGlobal(position))

    def del_network(self):
        """
        删除数据

        :return: None
        """
        ip, port = self.ui.network_list.currentItem().text().split(' -> ')[0].split(':')
        command = f'netsh interface portproxy delete {self.ui.network.currentText()} listenaddress={ip} listenport={port}'  # noqa
        self.run_and_check_result(command)
        # 执行命令结束发出信号更新network_list
        self.update_network_list.signal.emit(True)  # noqa

    def check_service_status(self):
        """
        检查端口转发所需服务状态

        :return: None
        """
        command = 'sc query iphlpsvc'  # noqa
        stdout_data, stderr_data = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                                    stderr=subprocess.PIPE, shell=True).communicate()
        stdout_data, stderr_data = stdout_data.decode('gbk'), stderr_data.decode('gbk')
        print(stdout_data, stderr_data)
        if stdout_data:
            service_status = re.compile(r'STATE\s+?:\s+?(\d)').search(stdout_data).group(1)
            if service_status == '4':
                self.setWindowTitle(f'{self.windowTitle()}  服务状态：运行')
                return
            if service_status == '1':
                self.setWindowTitle(f'{self.windowTitle()}  服务状态：停止')
                return
            else:
                self.setWindowTitle(f'{self.windowTitle()}  服务状态：未知')
                return
        else:
            self.setWindowTitle(f'{self.windowTitle()}  服务状态：未知')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    s = QMain()
    s.show()
    sys.exit(app.exec_())


import csv
import itertools
from logging import exception
import copy
from pickletools import ArgumentDescriptor
import random
import re
import sys
import os

# formula to extract the inferred score from the inferences
def scoreFromInference(var2):
	ag_score_infered = []
	for ag in var2:
		# the inferred score is the middle of the inferred interval
		ag_score_infered.append((max(ag[0],ag[2]) + min(ag[1],ag[3]))/2.0)
	return ag_score_infered

# return true if an argument attacks the issue
def attackTarget(agentArgs):
	for arg in agentArgs:
		if(arg in attaquants[0]):
			return True
	return False

# computes the score with respect to the h-categorizer semantic
def getScore(graph, firstValuation = []):
	if(firstValuation == []):
		firstValuation = []
		for item in range(len(graph)): # creation of the new valuation
			firstValuation.append(float(1.0))
	newValuation = []
	stable = True
	for item in range(len(graph)): # creation of the new valuation
		newValuation.append(float(1.0))
	i = 0
	for arg,att in graph.items(): # go through the elements of the graph
		sum = 1.0
		j = 0
		for item in graph:
			if(item in graph[arg]):
				sum += float(firstValuation[j])
			j += 1
		newValuation[i] = float(1.0/sum)
		if(abs(firstValuation[i] - newValuation[i]) > 0.0001): # the treshhold is fixed to 0.0001
			stable = False
		i += 1
	if(stable):
		return float(newValuation[0])
	else:
		return getScore(graph, newValuation)

# computes the positive inference
def turnScoreInference(move, previousTurnScore, argsPlayed, agentsInference):
	if(move.__len__() == 2): # if the agent did not play
		return move[0], int(0), [], argsPlayed, previousTurnScore, agentsInference
	elif(move.__len__() == 3): # if the agent played one argument
		newScore = getScore(move[2])
		if(newScore < previousTurnScore): # if the score got closer to zero
			if(((previousTurnScore + newScore)/2.0) < agentsInference[move[0]][1]):
				agentsInference[move[0]][1] = ((previousTurnScore + newScore)/2.0)
		else: # if the score got closer to one
			if(((previousTurnScore + newScore)/2.0) > agentsInference[move[0]][0]): # if it gives new information about the personal score of the agent
				agentsInference[move[0]][0] = ((previousTurnScore + newScore)/2.0)
		return move[0], int(1), [move[1]], argsPlayed+[argToId[move[1]]], newScore, agentsInference
	elif(move.__len__() == 4): # if the agent played two arguments
		newScore = getScore(move[3])
		if(newScore < previousTurnScore):
			if(((previousTurnScore + newScore)/2.0) < agentsInference[move[0]][1]):
				agentsInference[move[0]][1] = ((previousTurnScore + newScore)/2.0)
		else:
			if(((previousTurnScore + newScore)/2.0) > agentsInference[move[0]][0]):
				agentsInference[move[0]][0] = ((previousTurnScore + newScore)/2.0)
		return move[0], int(1), [move[1]]+[move[2]], argsPlayed+[argToId[move[1]]]+[argToId[move[2]]], newScore, agentsInference

# computes the negative inference
def negInference(turnNotPlayed, move, game_att, agentsInference):
	# print('NEGINF1')
	# print(move)
	couldHave = False
	if(move.__len__() == 3): # if the agent played one argument
		# i = 0
		# print('NEGINF3')
			# if(att == 1):
		for turn in turnNotPlayed: # goes through the turns during which the agent did not play
			print(f'{turn} == {move[0]} ?')
			if(turn[0] == move[0]): # if it was the agent's turn to play
				for att in turn[1]: #game_att[argToId[move[1]]]:
				# print(f'NEGINF, turn : {turn}, {att}')
					print(att)
					if(argToId[move[1]] in game_att[att]): # agent could have played
						couldHave = True
						temp = copy.deepcopy(turn[1])
						temp[argToId[move[1]]] = move[2][argToId[move[1]]]
						newScore = getScore(temp)
						if(newScore < turn[2]): # if the score would have gotten closer to zero
							print('NEGINFBS')
							if(((turn[2] + newScore)/2.0) > agentsInference[move[0]][2]):
								agentsInference[move[0]][2] = ((turn[2] + newScore)/2.0)
						else: # closer to one
							print('NEGINFBI')
							if(((turn[2] + newScore)/2.0) < agentsInference[move[0]][3]):
								agentsInference[move[0]][3] = ((turn[2] + newScore)/2.0)
			# i += 1
	elif(move.__len__() == 4): # if the agent played two arguments
		# i = 0
		# print('NEGINF4')
			# if(att == 1):
		for turn in turnNotPlayed:
			print(f'{turn} == {move[0]} ?')
			if(turn[0] == move[0]):
				# print(f'NEGINF, turn : {turn}, {att}')
				for att in turn[1]: #game_att[argToId[move[1]]]:
					if(argToId[move[1]] in game_att[att]): # agent could have played
						couldHave = True
						temp = copy.deepcopy(turn[1])
						temp[argToId[move[1]]] = move[3][argToId[move[1]]]
						temp[argToId[move[2]]] = move[3][argToId[move[2]]]
						newScore = getScore(temp)
						if(newScore < turn[2]):
							print('NEGINF')
							if(((turn[2] + newScore)/2.0) > agentsInference[move[0]][2]):
								agentsInference[move[0]][2] = ((turn[2] + newScore)/2.0)
						else:
							print('NEGINF')
							if(((turn[2] + newScore)/2.0) < agentsInference[move[0]][3]):
								agentsInference[move[0]][3] = ((turn[2] + newScore)/2.0)
			# i += 1
	return agentsInference,couldHave

# this function process the game history to compute the inferences
def gameScoreInference(moves, nbAgent, game_att):
	score = 1
	agentsInference = []
	turnNotPlayed = []
	inferenceHistory = []
	i = 0
	args = [0]
	for a in range(nbAgent): # init the inferred intervals
		agentsInference += [[0,1,0,1]] # positive inference lower and upper bounds and negative inference lower and upper bounds
		inferenceHistory += [[[0,1,0,1]]]
	# print(f'moves : {moves}')
	for move in moves: # goes through the game history
		couldHave = False
		print(f'move : {move}')
		ag, hasPlayed, played, args, score, agentsInference = turnScoreInference(move, score, args, agentsInference) # calls positive inference
		if(hasPlayed == 0): # if the agent did not play
			turnNotPlayed += [[ag,move[1],score]]
			print(f'Agent {ag} did not play, so we save the game : {[ag,move[1],score]}')# : {turnNotPlayed}')
		else: # if the agent played
			print(f'Agent {ag} did play ',end='')#{played} : {agentsInference}')
			print("{",end='')
			j = 0
			for item in played:
				if(j==0):
					print(f'{item}',end='')
				else:
					print(f', {item}',end='')
				j += 1
			print("} :")
			print(f'\tAgent\'s inference : {agentsInference[ag]}')
			agentsInference, couldHave = negInference(turnNotPlayed, move, game_att, agentsInference) # calls negative inference
			# inferenceHistory[ag].append([agentsInference[ag][0], agentsInference[ag][1], agentsInference[ag][2], agentsInference[ag][3]])
			print(f'\tAgent\'s inference : {agentsInference[ag]}, {couldHave}')
			if(couldHave == True): # if the agent could have make this move before now
				print(f'Agent {ag} could have played ',end='')#{played} : {agentsInference}')
				print("{",end='')
				j = 0
				for item in played:
					if(j==0):
						print(f'{item}',end='')
					else:
						print(f', {item}',end='')
					j += 1
				print("} earlier :")
				print(f'\tAgent\'s inference : {agentsInference[ag]}')
		inferenceHistory[ag].append([agentsInference[ag][0], agentsInference[ag][1], agentsInference[ag][2], agentsInference[ag][3]])
	print()
	return turnNotPlayed, agentsInference, inferenceHistory

# never called (attempt to improve the inference)
def gameScoreInference2(moves, nbAgent, game_att):
	agentsInference = []
	inferenceHistory = []
	args = [0]
	for a in range(nbAgent):
		agentsInference += [[0,1,0,1]]
		inferenceHistory += [[[0,1,0,1]]]

	for ag in range(nb_agents):
		ag_move = []
		for move in moves:
			if(move[0] == ag):
				if(move.__len__() == 3):
					temp = copy.deppcopy(move[2])
					temp = temp.pop(argToId[move[1]])
					ag_move.append(temp)
				if(move.__len__() == 4):
					temp = copy.deepcopy(move[3])
					temp = temp.pop(argToId[move[1]])
					temp = temp.pop(argToId[move[2]])
					ag_move.append(temp)
		i = 0
		for move in moves:
			if(move[0] == ag):
				if(move.__len__() == 3):
					for j in range(i):
						for arg in ag_move[j].keys():
							if(arg in att[argToId[move[1]]]):
								pass # move[1] aurait pu etre played
				if(move.__len__() == 4):
					pass
			i += 1

	return agentsInference, inferenceHistory

# this function runs the protocol
def protocol(agentsOrder, ag_score, agentAF, exception = []):
	# nbArgPlayed = 0
	gameArgs = [int(0)]
	done = False
	argsImpact = {}
	game = []
	game_att = {}
	game_att[0] = []
	# each agent tries every arg not in it yet and plays the best one
	while(not done):
		done = True
		for i in agentsOrder: # for each agent
			game_score = getScore(game_att) # get the game score
			minDiff = abs(ag_score[i] - game_score) # calculate the difference between their own score and the game score
			min_index = -1 # for now no arg is played
			multipleArgs = False
			j = 0
			for arg in agentAF[i]: # for each arg of the agent
				if((arg not in gameArgs) and (arg not in exception)): # if the arg has not been played
					temp = copy.deepcopy(game_att)
					temp[arg] = []
					for k in temp:
						for l in temp:
							if((l in attaquants[k]) and (l not in temp[k])):
								temp[k].append(l)
					new_score = getScore(temp) # score if arg is added
					# print(f'arg : {arg}, new_score : {new_score}')
					if(abs(ag_score[i] - new_score) < minDiff): # if it brings the game score closer to own score
						min_index = j # holds the arg that brings the score closest to own score
						minDiff = abs(ag_score[i] - new_score) # update the score difference
						best_score = new_score
						done = False # an arg is played so the game is not done
						multipleArgs = False
					else:
						for item in attaquants[arg]:
							if((item in agentAF[i]) and (item not in gameArgs) and (item not in exception)):
								temp[item] = []
								for k in temp:
									for l in temp:
										if((l in attaquants[k]) and (l not in temp[k])):
											temp[k].append(l)
								new_score = getScore(temp) # score if arg is added
								# print(f'item : {item}, new_score : {new_score}')
								if(abs(ag_score[i] - new_score) < minDiff): # if it brings the game score closer to own score
									min_index = j # holds the arg that brings the score closest to own score
									minDiff = abs(ag_score[i] - new_score) # update the score difference
									best_score = new_score
									done = False # an arg is played so the game is not done
									multipleArgs = [item]
				j += 1 # update index for next loop
			if(min_index == -1):
				print(f'Agent {i} doesn\'t play this round')
				game += [[int(i), copy.deepcopy(game_att)]]
			elif(multipleArgs == False): # if the agent plays one argument
				# print([key for key in agentAF[i].keys()][min_index])
				print(f'Agent {i} plays argument {idToArg[[key for key in agentAF[i].keys()][min_index]]}')
				# add the argument to the debate graph
				game_att[[key for key in agentAF[i].keys()][min_index]] = []
				for k in game_att:
					for l in game_att:
						if((l in attaquants[k]) and (l not in game_att[k])):
							game_att[k].append(l)
				# add the play to the game history
				game += [[int(i), idToArg[[key for key in agentAF[i].keys()][min_index]], copy.deepcopy(game_att)]]
				# argsPlayedByAgent[int(i)].append(int(agentsArgs[i][min_index]))
				argsImpact[[key for key in agentAF[i].keys()][min_index]] = best_score - game_score # game_score - ag_score[i] + minDiff
				gameArgs.append(int([key for key in agentAF[i].keys()][min_index]))
			else: # if the agent played two arguments
				print(f'Agent {i} plays argument {idToArg[[key for key in agentAF[i].keys()][min_index]]} and {idToArg[multipleArgs[0]]}')
				game_att[[key for key in agentAF[i].keys()][min_index]] = []
				game_att[multipleArgs[0]] = []
				for k in game_att:
					for l in game_att:
						if((l in attaquants[k]) and (l not in game_att[k])):
							game_att[k].append(l)
				game += [[int(i), idToArg[[key for key in agentAF[i].keys()][min_index]], idToArg[multipleArgs[0]], copy.deepcopy(game_att)]]
				# argsPlayedByAgent[int(i)].append(int(agentsArgs[i][min_index]))
				# argsPlayedByAgent[int(i)].append(int(multipleArgs[0]))
				argsImpact[[key for key in agentAF[i].keys()][min_index]] = (best_score - game_score)/2 # game_score - ag_score[i] + minDiff
				gameArgs.append(int([key for key in agentAF[i].keys()][min_index]))
				argsImpact[multipleArgs[0]] = (best_score - game_score)/2 # game_score - ag_score[i] + minDiff
				gameArgs.append(int(multipleArgs[0]))
	print(f'End of the protocol because no agent played during the round')
	print("\n-------------------------------------------------------------------")
	print()
	return game, gameArgs, game_att, argsImpact

argDen = random.randint(4,8)
argDensity = float(argDen/10.0)
nb_agents = random.randint(2,4)
argToId = {}
idToArg = {}
attaquants = {}
att = {}
universeGraph = []
agentAF = []
size_AF = 0
ag_score = []

path = ''
# finds the generated apx file
for file in os.listdir(sys.argv[1]):
	if(re.match("debate_graph_\S+", file)):
		path = sys.argv[1]+file

fileExists = os.path.exists(path)
if(fileExists == False):
	raise Exception("File not Found")
with open(path) as file_object:
	line = file_object.readline()
	while line:
		values = re.split("\\(|\\)|,",line)
		if(line.startswith('arg')): # new argument
			argToId[values[1]] = size_AF
			idToArg[size_AF] = values[1]
			attaquants[size_AF] = [] # attackers dictionnary
			att[size_AF] = [] # attacked dictionnary
			size_AF += 1
		elif((line.startswith('att')) and (values[1] in argToId) and (values[2] in argToId)): # new attack
			attaquants[argToId[values[2]]].append(argToId[values[1]])
			att[argToId[values[1]]].append(argToId[values[2]])
		else:
			raise Exception("Error in File")
		line = file_object.readline()

# creation of the universe graph
for item in range(size_AF):
	universeGraph.append([])
	for j in range(size_AF):
		if(j in att[item]):
			universeGraph[item].append(int(1))
		else:
			universeGraph[item].append(int(0))

# creation of the agent's knowledge base
for ag in range(nb_agents):
	agentAF.append({})
	for i in range(size_AF):
		if(i == 0): # the issue is always part of the knowledge base
			agentAF[ag][i] = []
		elif(random.random() <= argDensity): # each argument has a probability to be part of the knowledge base
			agentAF[ag][i] = []
			#here to get nb args
	if(not attackTarget(agentAF[ag])): # verify that at least one argument attacks the issue
		agentAF[ag] = {}
		while(not attackTarget(agentAF[ag])):
			for i in range(size_AF):
				if(i == 0):
					agentAF[ag][i] = []
				elif(random.random() <= argDensity):
					agentAF[ag][i] = []
	for i in agentAF[ag]:
		for j in agentAF[ag]:
			if(j in attaquants[i]):
				agentAF[ag][i].append(j)

# allows to write the useful information in a file
f = open(sys.argv[1]+'data'+sys.argv[2]+'.csv', 'w', newline='')
f1 = open(sys.argv[1]+'bench'+sys.argv[2]+'.csv', 'w', newline='')
writer = csv.writer(f)
w = csv.writer(f1)
temp = [nb_agents, size_AF]
writer.writerow(temp)
w.writerow([size_AF])
w.writerow([nb_agents])
writer.writerow(argToId.keys())
writer.writerows(universeGraph)
for af in agentAF:
	writer.writerow(af.keys())
	temp = [getScore(af)]
	writer.writerow(temp)
	w.writerow(temp)
	w.writerow([af.__len__()])

agOrder = []
ag_score = []
for i in range(nb_agents):
	agOrder.append(int(i))
	ag_score.append(getScore(agentAF[i]))

agOrder = itertools.permutations(agOrder, nb_agents) # get every possible order

# use to write the logs
print(f'There are {nb_agents} agents playing.')
print()
print(f'With an argument density of {argDensity}')
print()
print(f'The agents have the following knowledge base : ')
k = 0
for ag in agentAF:
	print(f'Agent {k} knows the argument(s) :\n\t\t',end='')
	j = 0
	for cle, value in ag.items():
		if(j == 0):
			print("{",end='')
		else:
			print(", ",end='')
		print(f'{idToArg[cle]}',end='') #: [', end='')
		j += 1
	print("}\n\tand the relations :\n\t\t",end='')
	i = 0
	print("{", end='')
	for cle, value in ag.items():
		for val in value:
			if(i != 0):
				print(f', ', end='')
			print(f'({val},{cle})',end='')
			i += 1
	print("}",end='')
	print(f'\n\twith a personal score of {getScore(ag)}\n')
	k += 1

for agentsOrder in agOrder: # we run the protocol for every order possible
	print()
	print("-------------------------------------------------------------------")
	print(f'\t\t\tAGENTS ORDER : ',end='')
	i = 0
	for ag in agentsOrder:
		if(i == 0):
			print(f'{ag}',end='')
			i+=1
		else:
			print(f', {ag}',end='')
	print()
	print("-------------------------------------------------------------------")
	print()

	game, gameArgs, game_att, argsImpact = protocol(agentsOrder, ag_score, agentAF)

	print(f'The final graph :\n\t',end='')
	print(f'the argument(s) played are :\n\t\t',end='')
	j = 0
	for cle, value in game_att.items():
		if(j == 0):
			print("{",end='')
		else:
			print(", ",end='')
		print(f'{idToArg[cle]}',end='') #: [', end='')
		j += 1
	print("}\n\tand the relations :\n\t\t",end='')
	i = 0
	print("{", end='')
	for cle, value in game_att.items():
		for val in value:
			if(i != 0):
				print(f', ', end='')
			print(f'({val},{cle})',end='')
			i += 1
	print("}")
	k += 1
	print()
	print(f'\twith a score of {getScore(game_att)}')
	print()
	print(f'The immediate impacts of the arguments when played are :')
	for key, value in argsImpact.items():
		print(f'\tthe impact of the argument {idToArg[key]} equals {value}')
	print()

	print("---------------impact of args based on final outcome---------------\n")

	argsImpactOnFinalOutcome = {}

	for item in gameArgs: # for every argument in the final debate graph
		if(item != 0):
			temp1 = copy.deepcopy(game_att)
			for arg in attaquants[item]:
				if arg in temp1:
					temp1.pop(arg) # remove the direct attackers of item
			temp2 = copy.deepcopy(game_att)
			temp2.pop(item) # remove item
			a = getScore(temp1)
			b = getScore(temp2)
			argsImpactOnFinalOutcome[item] = a - b
		else:
			argsImpactOnFinalOutcome[item] = 0.0

	print(f'The arguments impact based on the final outcome :')
	for key, value in argsImpactOnFinalOutcome.items():
		print(f'\tthe impact of the argument {idToArg[key]} equals {value}')

	print("\n-------------impact of args based on omniscient protocol-------------\n")

	argsImpactOnUniverseProtocol = {}

	gameScore = getScore(game_att)
	for item in gameArgs:
		if((item != 0)):
			print(f'Protocol without the argument {idToArg[item]}\n')
			game2, gameArgs2, game_att2, argsImpact2 = protocol(agentsOrder, ag_score, agentAF, [item])
			newScore = getScore(game_att2)
			argsImpactOnUniverseProtocol[item] = gameScore - newScore
		else:
			argsImpactOnUniverseProtocol[item] = 0.0

	print(f'The arguments impact based on the omniscient protocol :')
	for key, value in argsImpactOnUniverseProtocol.items():
		print(f'\tthe impact of the argument {idToArg[key]} equals {value}')
	print()

	print("--------------impact of args based on observable protocol-------------\n")

	var1, var2, inferenceHistory = gameScoreInference(game, nb_agents, game_att)

	print(f'The saved steps of the game are :')
	for turn in var1:
		print(f'\t{turn} :\n\tAgent {turn[0]}\'s turn, ',end='')
		i = 0
		for item in turn[1]:
			if(i == 0):
				print("{",end='')
				print(f'{item}',end='')
			else:
				print(f', {item}',end='')
			i += 1
		print("}",end='')
		print(f', with a score of {turn[2]}\n')
	print(f'The infered score intervals of the agents are :')
	i = 0
	for item in var2:
		print(f'for agent {i} : [{max(item[0], item[2])}, {min(item[1], item[3])}]')
		i += 1
		print(f'item : {item}')

	argsImpactOnPartialProtocolH1 = {}
	argsImpactOnPartialProtocolH2 = {}
	gameScore = getScore(game_att)
	ag_score_infered = scoreFromInference(var2)

	i = 0
	print(f'\nThe infered score :')
	for item in ag_score_infered:
		print(f'for agent {i} : {item}')
		i += 1

	i = 0
	print(f'\nThe only playable arguments are :')
	for item in gameArgs:
		if(i == 0):
			print("\t{",end='')
			print(f'{item}',end='')
		else:
			print(f', {item}',end='')
		i += 1
	print("}")
	print()
	print("-------------------------------------------------------------------")
	print()

	tempAF2 = []
	for i in range(nb_agents):
		tempAF2.append({})
		tempAF2[i][0] = []
	for move in game:
		if(move.__len__() == 3):
			tempAF2[move[0]][argToId[move[1]]] = []
		elif(move.__len__() == 4):
			tempAF2[move[0]][argToId[move[1]]] = []
			tempAF2[move[0]][argToId[move[2]]] = []
	for ag in tempAF2:
		for key, values in attaquants.items():
			if(key in ag):
				for arg in values:
					if(arg in ag):
						ag[key].append(arg)

	poped = []
	for af in agentAF:
		for arg,value in af.items():
			if(arg not in gameArgs) and (arg not in poped):
				poped.append(arg)

	tempAF = copy.deepcopy(agentAF)
	for ag in tempAF:
		if(item in ag):
			ag.pop(item)
		for arg in poped:
			if(arg in ag):
				ag.pop(arg)

	for item in game_att:
		if(item != 0):
			print(f'Protocol without the argument {idToArg[item]} under hypothesis 1\n')
			game2, gameArgs2, game_att2, argsImpact2 = protocol(agentsOrder, ag_score_infered, tempAF, exception=[item])
			newScore = getScore(game_att2)
			argsImpactOnPartialProtocolH1[item] = gameScore - newScore
			print(f'Protocol without the argument {idToArg[item]} under hypothesis 2\n')
			game3, gameArgs3, game_att3, argsImpact3 = protocol(agentsOrder, ag_score_infered, tempAF2, exception=[item])
			newScore2 = getScore(game_att3)
			argsImpactOnPartialProtocolH2[item] = gameScore - newScore2
		else:
			argsImpactOnPartialProtocolH1[item] = 0.0
			argsImpactOnPartialProtocolH2[item] = 0.0

	print(f'The arguments impact based on the observable protocol with hypothesis 1 :')
	for key, value in argsImpactOnPartialProtocolH1.items():
		print(f'\tthe impact of the argument {idToArg[key]} equals {value}')
	print()

	print(f'The arguments impact based on the observable protocol with hypothesis 2 :')
	for key, value in argsImpactOnPartialProtocolH2.items():
		print(f'\tthe impact of the argument {idToArg[key]} equals {value}')
	print()

	writer.writerow([])
	writer.writerow(agentsOrder)
	writer.writerow(gameArgs)
	temp = [game.__len__()]
	writer.writerow(temp)
	w.writerow(temp)
	for move in game:
		writer.writerow(move)
	i = 0
	for ag in inferenceHistory:
		writer.writerow(ag)
		w.writerow(ag)
		writer.writerow([ag_score_infered[i]])
		w.writerow([ag_score_infered[i]])
		i += 1
	argsPlayed = []
	for item in range(nb_agents):
		argsPlayed.append([])
	for move in game:
		if(move.__len__() == 3):
			argsPlayed[move[0]].append(argToId[move[1]])
		elif(move.__len__() == 4):
			argsPlayed[move[0]].append(argToId[move[1]])
			argsPlayed[move[0]].append(argToId[move[2]])

	writer.writerow(argsImpactOnFinalOutcome.values())
	writer.writerow(argsImpactOnUniverseProtocol.values())
	writer.writerow(argsImpactOnPartialProtocolH1.values())
	writer.writerow(argsImpactOnPartialProtocolH2.values())
	w.writerow(argsImpactOnFinalOutcome.values())
	w.writerow(argsImpactOnUniverseProtocol.values())
	w.writerow(argsImpactOnPartialProtocolH1.values())
	w.writerow(argsImpactOnPartialProtocolH2.values())
	w.writerow(argsImpactOnFinalOutcome.keys())
	for item in argsPlayed:
		w.writerow(item)


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import subprocess
import sys
from distutils.spawn import find_executable

import mozunit
import pytest

from mozlint import cli

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

@pytest.fixture
def parser():
    return cli.MozlintParser()

@pytest.fixture
def run(parser, lintdir, files):
    def inner(args=None):
        args = args or []
        args.extend(files)
        lintargs = vars(parser.parse_args(args))
        lintargs["root"] = here
        lintargs["config_paths"] = [os.path.join(here, "linters")]
        return cli.run(**lintargs)

    return inner

def test_cli_with_ascii_encoding(run, monkeypatch, capfd):
    cmd = [sys.executable, "runcli.py", "-l=string", "-f=stylish", "files/foobar.js"]
    env = os.environ.copy()
    env["PYTHONPATH"] = os.pathsep.join(sys.path)
    env["PYTHONIOENCODING"] = "ascii"
    proc = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=here,
        env=env,
        universal_newlines=True,
    )
    out = proc.communicate()[0]
    assert "Traceback" not in out

def test_cli_run_with_fix(run, capfd):
    ret = run(["-f", "json", "--fix", "--linter", "external"])
    out, err = capfd.readouterr()
    assert ret == 0
    assert out.endswith("{}\n")

@pytest.mark.skipif(not find_executable("echo"), reason="No `echo` executable found.")
def test_cli_run_with_edit(run, parser, capfd):
    os.environ["EDITOR"] = "echo"

    ret = run(["-f", "compact", "--edit", "--linter", "external"])
    out, err = capfd.readouterr()
    out = out.splitlines()
    assert ret == 1
    assert out[0].endswith("foobar.js")  # from the `echo` editor
    assert "foobar.js: line 1, col 1, Error" in out[1]
    assert "foobar.js: line 2, col 1, Error" in out[2]
    assert "2 problems" in out[-1]
    assert len(out) == 5

    del os.environ["EDITOR"]
    with pytest.raises(SystemExit):
        parser.parse_args(["--edit"])

def test_cli_run_with_setup(run, capfd):
    # implicitly call setup
    ret = run(["-l", "setup", "-l", "setupfailed", "-l", "setupraised"])
    out, err = capfd.readouterr()
    assert "setup passed" in out
    assert "setup failed" in out
    assert "setup raised" in out
    assert ret == 1

    # explicitly call setup
    ret = run(["-l", "setup", "-l", "setupfailed", "-l", "setupraised", "--setup"])
    out, err = capfd.readouterr()
    assert "setup passed" in out
    assert "setup failed" in out
    assert "setup raised" in out
    assert ret == 1

def test_cli_for_exclude_list(run, monkeypatch, capfd):
    ret = run(["-l", "excludes", "--check-exclude-list"])
    out, err = capfd.readouterr()

    assert "**/foobar.js" in out
    assert (
        "The following list of paths are now green and can be removed from the exclude list:"
        in out
    )

    ret = run(["-l", "excludes_empty", "--check-exclude-list"])
    out, err = capfd.readouterr()

    assert "No path in the exclude list is green." in out
    assert ret == 1

def test_cli_run_with_wrong_linters(run, capfd):

    run(["-l", "external", "-l", "foobar"])
    out, err = capfd.readouterr()

    # Check if it identifes foobar as invalid linter
    assert "A failure occurred in the foobar linter." in out

    # Check for exception message
    assert "Invalid linters given, run again using valid linters or no linters" in out

if __name__ == "__main__":
    mozunit.main()


#!/usr/bin/python
import boto3
import argparse
import yaml
import pprint

pp = pprint

# client = boto3.client('cloudformation', region_name='us-east-1')
# response = client.create_stack(
#     StackName='create-s3-bucket',
#     TemplateURL='https://s3.amazonaws.com/my-jpendyala-bucket/create-s3-bucket.yml',
# )
# print (response)

def cp_cf_template(stack_name, bucket_name):

    s3 = boto3.resource('s3', region_name = 'us-east-1')
    s3.meta.client.upload_file('/var/lib/jenkins/workspace/create-update-stack-job/AWSTesting/CloudFormation/' + stack_name + '.yml', bucket_name, stack_name + '.yml')

def mystack_create(stack_name, bucket_name):

    response = cf_client.create_stack(
        StackName=stack_name,
        TemplateURL='https://s3.amazonaws.com/' + bucket_name + '/' + stack_name + '.yml',
        OnFailure='ROLLBACK'
    )
    print (response)

def parse_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('--stack_name')
    parser.add_argument('--bucket_name')
    #parser.add_argument('--url')
    args = parser.parse_args()
    return args

def main(args):
    cp_cftemplate = cp_cf_template(args.stack_name, args.bucket_name)
    crt_stack = mystack_create(args.stack_name, args.bucket_name)

if __name__== "__main__":
    PARSED_ARGS = parse_arguments()
    #region_name = PARSED_ARGS.region_name
    cf_client = boto3.client('cloudformation', region_name='us-east-1')
    #s3_client = boto3.client('s3', region_name='us-east-1')
    main(PARSED_ARGS)

# python createupdatestack.py --region_name 'us-east-1' --stack_name create-s3-bucket --url 'https://s3.amazonaws.com/my-jpendyala-bucket/create-s3-bucket.yml'

#
# import yaml
# with open('config.yaml', 'r') as f:
#     doc = yaml.load(f)


# 3:43

import pandas as pd
import numpy as np
import os
import sys

print('hello')

os.getcwd()

raw_data = pd.read_csv('db_data.csv')

raw_data.tail()

raw_data.date.unique

# lets split first 80 as training set

raw_data.loc[raw_data.date == 'D080','date']

# index = 87680
# drop F02 for now (sectors)
cols = [c for c in raw_data.columns if c != 'F02']

train = raw_data.loc[:87680,cols]
test  = raw_data.loc[87681:,cols]

# training set
# ============

# lets convert all factors to ranks
cols = [c for c in train.columns if c not in ['stock']]
factors = ['F'+str(i).zfill(2) for i in range(21)]

ranks = train.loc[:,cols].groupby('date').transform('rank')

ranks['date'] = train.date

ranks[ranks.date == 'D000'].corr()

rank_cor = ranks.groupby('date').corr().reset_index()
ics = rank_cor[rank_cor.level_1=='fwd_returns']

ics.head()

avg_IR = ics.mean() / ics.std()

ranks

ics.loc[:,factors ].plot()
# better idea, calc x-section rank IC

# ===================
# run regression each month
import statsmodels.api as sm
regress_data = ranks.loc[ranks.date == 'D000',factors + ['fwd_returns']]
regress_data = regress_data.fillna(0)
y = regress_data.fwd_returns
X = regress_data.loc[:,factors]

model = sm.OLS(y,X).fit()
model.summary()

this_day = ranks.loc[ranks.date == 'D001',factors].fillna(0)

preds = model.predict(this_day)

# get predicted values

# use queue to cycle thru dates?


import matplotlib
import pdb
#matplotlib.use('Agg')

import matplotlib.pyplot as plt

def main():
    pass

def getVariables(lineOfText):
    variables = []
    variable = str()
    whiteCount = 0
    #print(lineOfText)
    isVariable = True
    insideParenthasis = False
    for i in lineOfText:
        #print (i)
        if i == ' ' and not insideParenthasis:
            if whiteCount == 0:
                variables.append(variable)
                variable = str('')
            isVariable = False
            whiteCount += 1
            if whiteCount == 2:
                isVariable = True
        elif i == '(':
            insideParenthasis = True
        elif i == ')':
            insideParenthasis = False
            whiteCount -= 1
        if isVariable == True and i != ' ' and i != ')' and not insideParenthasis:
            whiteCount = 0
            variable = str(variable) + str(i)
            #print(variable)
    return variables

def  convertToNum(num):
    power10 = len(num) - 1
    total = 0
    for i in num:
        total += i * (10 ** power10)
        power10 -= 1
    return total

def  getNums(lineOfText):
    nums = []
    number = []
    num = []
    lineOfText.seek(0)
    for i, line in enumerate(lineOfText):
        if i > 1:
            #print(line[0])
            if line[0] == "S":
                number = []
                for i in line:
                    if i.isdigit():
                        num.append(int(i))
                    #print(num)
                    if i == ',' or i == ']' and num:
                            number.append(convertToNum(num))
                            num = []
            if number:
                nums.append(number)
                number = []
        #print(nums)
    return nums

def getVariInitalNums(readFile):
    readFile.seek(0)
    varNames = []
    varVals = []
    for i, line in enumerate(readFile):
        if i == 1:
            varNames = getVariables(line)
            #print(varNames)
    #print("THIS IS A TEST STATMENT")
    varVals = getNums(readFile)
    #print(varVals)
    return(varNames, varVals)

def collate(data):
    dataVals = data[1]
    reOrderedData = []
    d = []
    print(dataVals)
    print(len(dataVals[0]))
    print(len(dataVals))

    i = 0
    j = 0
    print(len(dataVals))
    while j < len(dataVals[i]):
        while i < len(dataVals):
            d.append(dataVals[i][j])
            i += 1
        reOrderedData.append(d)
        d = []
        i = 0
        j += 1
    print(reOrderedData)
    return reOrderedData

def setLineMarker(lMarker):
    lm = 'nil'
    if (lMarker == 0):
        lm = '.'
    elif (lMarker == 1):
        lm = ','
    elif (lMarker == 2):
        lm = 'o'
    elif (lMarker == 3):
        lm = 'v'
    elif (lMarker == 4):
        lm = '^'
    elif (lMarker == 5):
        lm = '<'
    elif (lMarker == 6):
        lm = '>'
    elif (lMarker == 7):
        lm = '1'
    elif (lMarker == 8):
        lm = '2'
    elif (lMarker == 9):
        lm = '3'
    elif (lMarker == 10):
        lm = '4'
    elif (lMarker == 11):
        lm = 's'
    elif (lMarker == 12):
        lm = 'p'
    elif (lMarker == 13):
        lm = '*'
    elif (lMarker == 14):
        lm = 'h'
    elif (lMarker == 15):
        lm = 'H'
    elif (lMarker == 16):
        lm = '+'
    elif (lMarker == 17):
        lm = 'x'
    elif (lMarker == 18):
        lm = 'D'
    elif (lMarker == 19):
        lm = 'd'
    elif (lMarker == 20):
        lm = '|'
    elif (lMarker == 21):
        lm = '_'
    #print(lMarker)
    #print(lm)
    #pdb.set_trace()
    return lm

def setLineType(lType):
    lt = "nil"
    if (lType == 0):
        lt = '-'
    elif (lType == 1):
        lt = '--'
    elif (lType == 2):
        lt = '-.'
    return lt

def setLineColor(lColor):
    lc = 'nil'
    if (lColor == 0):
        lc = 'b'
    elif (lColor == 1):
        lc = 'g'
    elif (lColor == 2):
        lc = 'r'
    elif (lColor == 3):
        lc = 'c'
    elif (lColor == 4):
        lc = 'm'
    elif (lColor == 5):
        lc = 'y'
    elif (lColor == 6):
        lc = 'k'
    return lc

def  incLineMarker(lm):
    lm += 1
    if lm > 21:
        lm = 0
    return lm

def  incLineType(lt):
    lt += 1
    if lt > 2:
        lt = 0
    return lt

def  incLineColor(lc):
    lc += 1
    if lc > 6:
        lc = 0
    return lc

if __name__ == '__main__':
    main()
#  Comment the following lines to test ALEAE-STATS-GRABBER by itself
import fileinput
for line in fileinput.input():
    fileName = line
fileName = fileName.replace("\n", "")
# END COMMENTED SECTION FOR TESTING

#fileName = "demo" # USED FOR TESTING ALEAE-STATS-GRABBER BY ITSELF

directory = "output/"
DirectoryFile = directory + fileName

outPutFile = open(DirectoryFile + ".outpt", mode='r')
for line in outPutFile:
    #print(line)
    i = 2
data = getVariInitalNums(outPutFile)
#print(data)
#pdb.set_trace()
reData = collate(data)
#print(reData)
#pdb.set_trace()

i = 0  # counter to cycle through the data list
lineMarker = 0  # for cycling through matplotlib line markers
lineType = 0  # for cycling through matplotlib line types
lineColor = 0  # for cycling through matplotlib line colors

while i < len(reData):
    #print(('data', len(data[0])))
    #print(('reData', len(reData)))
    #print(('i', i))
    #print((len(reData)))
    #print((len(data[0][i])))

    lMarker = setLineMarker(lineMarker)
    lType = setLineType(lineType)
    lColor = setLineColor(lineColor)

    #  multi line parenthasis: '(plt.plot...)'
    (plt.plot(
        reData[i],
        marker=lMarker,
        linestyle=lType,
        color=lColor,
        label=data[0][i],
        linewidth=2
        ))

    i += 1
    lineMarker = incLineMarker(lineMarker)
    lineType = incLineType(lineType)
    lineColor = incLineColor(lineColor)

i = 0
print((data[0]))
print(data[len(data)-1][len(data[len(data)-1])-1])
while i < (len(data[0])-1):
    print(data[0][i])
    print(data[len(data)-1][len(data[len(data)-1])-1][i])
    i += 1

plt.legend()
fig = plt.gcf()
plt.show()
fig.savefig(DirectoryFile)
i = 0


import time
import multiprocessing
from idCreater import *
from sliitResultChecker import *

id = Idcreater()
id.enterFields()
id.initiate()

if id.choice == "1":
    rc = ResultChecker(id.oldNic)
else:
    rc = ResultChecker(id.newNic)

rc.setDetails()

start = time.time()
processes = []
for i in range(10):
    p = multiprocessing.Process(target=rc.initiate, args=(i,))
    p.start()
    processes.append(p)

for process in processes:
    process.join()

end = time.time()
print("Finished in " + str(end - start) + " seconds.")


"""added for coxswain to metric

Revision ID: ac379e6b4f3a
Revises: ae365edbe338
Create Date: 2021-10-11 12:02:27.735833

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'ac379e6b4f3a'
down_revision = 'ae365edbe338'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('metric', sa.Column('for_coxswain', sa.Boolean(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('metric', 'for_coxswain')
    # ### end Alembic commands ###


import os
import openai
from flask import Flask, render_template, request

app = Flask(__name__)

# Set OpenAI API key and model engine
openai.api_key = os.getenv("OPENAI_API_KEY")
model_engine = "text-davinci-002"

# Define a function to generate text based on user input
def generate_text(prompt):
    completions = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )

    message = completions.choices[0].text.strip()
    return message

# Define a route to handle user input
@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # Get user input from the form
        prompt = request.form['prompt']

        # Generate text based on the user's prompt
        generated_text = generate_text(prompt)

        # Return the generated text to the user
        return render_template('index.html', prompt=prompt, generated_text=generated_text)

    # If the request is a GET request, render the index.html template
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)


import cv2
import face_recognition
from timeit  import default_timer as timer
start = timer()
frame = cv2.imread(r"C:\Users\dapz\Desktop\Face Recognition\Face Encoding\Images\image1.jpg")

def FindPoint(x1, x2, y1,
              y2, cx, cy) :
    if (cx > x1 and cx < x2 and
        cy > y1 and cy < y2) :
        return True
    else :
        return False

o = []
i = 0
face_locations = face_recognition.face_locations(frame)
for top, right, bottom, left in face_locations:
    i = i+1
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
    y = bottom + int((top-bottom)*0.5)
    x = left + int((right - left)*0.5)
    if FindPoint(left, right, top,
                 bottom, x, y) :
        print("Yes")
    else :
        print("No")

    cv2.circle(frame,(x,y,),5 , (0,255,255), -1)
key = cv2.waitKey(1)
if key == ord('q'):
    image.release()

    cv2.destroyAllWindows()

print(timer()-start)


from network import *
import torch
import torch.nn as nn
from collections import OrderedDict
from torch import optim
import os
import torchvision
from PIL import Image
import numpy as np
from torchvision.utils import save_image
from network import *
from attention import *

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class Model(nn.Module):
    def __init__(self,hp):
        super(Model, self).__init__()
        self.hp = hp
        self.net = network()

        self.net.apply(weights_init_normal).to(device)

        self.sample_train_params = self.parameters()
        self.loss = nn.L1Loss()
        self.optimizer = optim.Adam(self.sample_train_params, hp.learning_rate)
        self.hp = hp
        self.step = 0

    def forward(self, img, img_gt):
        self.optimizer.zero_grad()

        output = self.net(img)

        loss = self.loss(output, img_gt)

        loss.backward()
        self.optimizer.step()

        return loss

    def append_dir(self, name):
        if not os.path.exists(self.hp.saved_models):
            os.mkdir(self.hp.saved_models)
        pathappend = self.hp.saved_models + "/" + name
        if not os.path.exists(pathappend):
            os.makedirs(pathappend)
        return pathappend

    def test(self, dataloader_Test, epoch):
        pathvrst = self.append_dir("val_output")
        self.optimizer.zero_grad()
        self.eval()
        img_stack = []
        for i, data in enumerate(dataloader_Test):
            img, img_gt= data
            img = img.to(device)
            with torch.no_grad():
                msk_big = self.net(img)
            img_stack.append(img[0])
            img_stack.append(img_gt[0])
            img_stack.append(msk_big[0])

            img_stack = torch.stack(img_stack, dim=0)
            save_image(img_stack, f"{pathvrst}/{i}+{str(epoch)+'epochs'}.png")

            img_stack = []

            # save_image(img_gt, f"{pathvrst}/{str(i) + 'gt'}+{str(epoch)+'epochs'}.png")
            # save_image(img, f"{pathvrst}/{str(i) + 'org'}+{str(epoch)+'epochs'}.png")


import argparse
import re

def main(args):
    learned_hyp_file_path = args.learned_hyp_file
    test_examples_path = args.test_examples
    predictor_pos_value = args.predictor_positive_value

    # Take in learned hypothesis, test las file and generate F1 score and accuracy
    # over the test examples.
    learned_hyp_file = open(learned_hyp_file_path, "r").read()
    test_examples_file = open(test_examples_path, "r").read().split("% Background Knowledge")[0].split("% Examples")[1]

    # Extract examples using RegEX
    r_str = '(#pos\(.*, {)(((\s*)+[^\)|\}]+\)\.)+)'
    matches = re.findall(r_str, test_examples_file)

    positive_examples = []
    negative_examples = []
    for m in matches:
        pos_or_neg_str = m[0].split(',')
        features_and_values = m[1]

        # Remove whitespace
        r_str = re.compile(r'\s+')
        features_and_values = re.sub(r_str, '', features_and_values).split('.')
        example = {}

        for fv in features_and_values:
            if fv != '':
                feature = fv.split('(')[0]
                value = fv.split('(')[1].split(')')[0]
                example[feature] = value

        if predictor_pos_value in pos_or_neg_str[1]:
            positive_examples.append(example)
        else:
            negative_examples.append(example)

    # Build rules
    rules_for_accept = []
    r_str = '(attack :- )([^\.]+)'
    matches = re.findall(r_str, learned_hyp_file)
    for rule in matches:
        to_check = {}
        features_and_values = rule[1].split(', ')
        for fv in features_and_values:
            feature = fv.split('(')[0]
            value = fv.split('(')[1].split(')')[0]
            to_check[feature] = value

        rules_for_accept.append(to_check)

    # For each positive example, compute prediction based on rules for accept
    def calculate_results(examples):
        predictions = []
        for ex in examples:
            rule_results = []
            for r in rules_for_accept:
                rule_fail = False
                for idx,k in enumerate(r):
                    target = r[k]
                    if ex[k] != target:
                        rule_fail = True
                rule_results.append(not rule_fail)

            # If any results are true, this means accept
            predictions.append(any(rule_results))
        return predictions

    positive_predictions = calculate_results(positive_examples)
    negative_predictions = calculate_results(negative_examples)

    # Calculate F1 Score
    tp = sum(positive_predictions)
    fn = len(positive_predictions) - tp
    fp = sum(negative_predictions)
    tn = len(negative_predictions) - fp

    precision = tp / (tp + fp)
    recall = tp  / (tp + fn)
    f1 = 2 * ((precision*recall) / (precision + recall))

    # Calculate Accuracy
    accuracy = (tp + tn) / (tp + tn + fp + fn)

    print('Number of Positive Examples: ', len(positive_examples))
    print('Number of Negative Examples: ', len(negative_examples))
    print('Precision: ', precision)
    print('Recall: ', recall)
    print('F1 Score: ', f1)
    print('Accuracy: ', accuracy)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Score a learned hypothesis over test examples')
    parser.add_argument('learned_hyp_file', help='The learned hypothesis file to score against')
    parser.add_argument('test_examples', help='The .las file containing the test examples')
    parser.add_argument('--predictor_positive_value',
        help='The value of the predictor for a positive class in binary classification (default: accept).',
        default='accept')

    args = parser.parse_args()
    main(args)


import discord
from discord.ext import commands
import os
import random
from discord import TextChannel

class FunCog(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.whip_images = ["https://cdn.discordapp.com/attachments/460111213649461248/461643645985095690/whip2.gif", "https://cdn.discordapp.com/attachments/366335793658331137/461641836856082432/whip1.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461643739677458454/whip6.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461643930987921409/whip4.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461643973937856522/whip3.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461644021454864384/whip5.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461644224396525569/whip10.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461644305245667339/whip8.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461644476641837067/whip9.gif", "https://cdn.discordapp.com/attachments/460111213649461248/461644603066548225/whip7.gif"]
        self.bam_images = ["https://cdn.discordapp.com/attachments/480798534409650186/509420445980819467/giphy_2.gif", "https://cdn.discordapp.com/attachments/480798534409650186/509420392558100501/giphy_3.gif", "https://cdn.discordapp.com/attachments/480798534409650186/509420341202780196/giphy_4.gif", "https://cdn.discordapp.com/attachments/480798534409650186/509420304708272159/giphy.gif", "https://cdn.discordapp.com/attachments/480798534409650186/509420229823037460/O3DHIA5.gif", "https://cdn.discordapp.com/attachments/480798534409650186/509420143839936522/azCR8D1.gif"]

    @commands.command()
    async def whip(self, ctx, target: discord.Member):
        whip_image = random.choice(self.whip_images)
        await ctx.send("You whipped the shit out of "+target.name, set_image=whip_image, image=False)

    @commands.command()
    async def bam(self, ctx, target: discord.Member):
        bam_image = random.choice(self.bam_images)
        await ctx.send(f"You bammed the shit out of {target.name}", set_image=bam_image, image=False)

    @commands.command()
    async def set_channel(self, ctx, channel_id):
        channel = self.bot.get_channel(int(channel_id))
        if channel:
            self.channel = channel
            await ctx.send("Setting the channel succeeded")
        else:
            await ctx.send("Setting the channel failed")

    @commands.command()
    async def speak(self, ctx):
        message = ctx.message.content.split(".speak ")
        message = message[1]
        if self.channel:
            await self.channel.send(message)
            await ctx.message.delete()
        else:
            await ctx.send("No channel set")

    async def get_user_reaction(self, ctx, message, reactions):

        reaction, user = await self.bot.wait_for('reaction_add',)

    @commands.Cog.listener("on_raw_reaction_add")
    async def on_reaction_add(self, payload):
        reactions = ["⚪", "⚫"]
        # YIN                                  #YANG
        role_ids = [611866314738368521, 611866869778874369]

        # The annoucement message id
        message_id = 611933810837422091
        # This means the reaction was done to the selected message
        guild = self.bot.get_guild(payload.guild_id)
        member = guild.get_member(payload.user_id)

        # Check if the member has one of the roles already
        for role in member.roles:
            if role.id in role_ids:
                return

        if payload.message_id == message_id:
            # If the reaction is one of the selected:
            print(payload.emoji)
            emoji = str(payload.emoji)
            if emoji in reactions:
                #White / Yang
                if emoji == "⚪":
                    role = guild.get_role(611866869778874369)
                    await self.assign_role(member, role)
                #black / Yin
                elif emoji == "⚫":
                    role = guild.get_role(611866314738368521)
                    await self.assign_role(member, role)

    async def assign_role(self, member, role):
        if member.bot:
            return
        print("Assign:", role, "to:", member)
        await member.add_roles(role)

def setup(bot):
    bot.add_cog(FunCog(bot))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 29 18:47:07 2021

@author: pierre
"""

from reduction_detecteur import obj, obj_std, calstar, calstar_std, wave
import numpy as np
import scipy.signal
import time

lambdas_laser = np.array([980, 1080, 1310, 1550, 1742, 2004])[::-1]

lambdas_laser = np.array([1730.23,1545.07,1309.0,1123.71,987.72])
pixels_laser = scipy.signal.argrelmax(np.mean(wave,1), order=100)[0][1:-1]

pixs = np.arange(1024)
p = np.polyfit(pixels_laser, lambdas_laser, deg=1)
lambdas = np.poly1d(p)(pixs)

spec_th_temp = np.genfromtxt('./ADDITIONAL_DATA/pickles_uk_23.dat', delimiter=',')
spec_th_temp[:, 0] = spec_th_temp[:, 0]/10
spec_th_t2 = np.interp(lambdas, spec_th_temp[:, 0], spec_th_temp[:, 1])
spec_th = spec_th_t2/np.median(spec_th_t2)

spec_calstar =  np.sum(calstar[:, 430:450],1)

transmi = spec_calstar/spec_th
transmi/=np.median(transmi)

transmi = np.ma.masked_less_equal(transmi,0)
transmi = np.ma.masked_invalid(transmi)
transmi = np.ma.filled(transmi,1e-3)

for col in calstar.T:
    col /= transmi
for col in calstar_std.T:
    col /= transmi

for col in obj.T:
    col /= transmi
for col in obj_std.T:
    col /= transmi

mask = (obj > 120)+(obj < 0)
masked_obj = np.ma.masked_array(obj, mask)
masked_obj_std = np.ma.masked_array(obj_std, mask)
masked_calstar = np.ma.masked_array(calstar, mask)
masked_calstar_std = np.ma.masked_array(calstar_std, mask)

spec_cal_star_detransmitted =  np.sum(calstar[:, 430:450],1)

h_filt = np.genfromtxt("./ADDITIONAL_DATA/H_FILTER_2MASS.dat", delimiter='  ')
h_f = np.interp(lambdas, h_filt[:, 0]*10**3, h_filt[:, 1])
adu_h_calstar = np.sum(spec_cal_star_detransmitted*h_f)
j_filt = np.genfromtxt("./ADDITIONAL_DATA/J_FILTER_2MASS.dat", delimiter='  ')
j_f = np.interp(lambdas, j_filt[:, 0]*10**3, j_filt[:, 1])
adu_j_calstar = np.sum(spec_cal_star_detransmitted*j_f)

mag_h = 9.477
flux_h = 1.133e-13*10**-(0.4*mag_h)*10**4*0.251
mag_j = 9.819
flux_j = 3.129E-13*10**-(0.4*mag_j)*10**4*0.162

adu = np.mean([flux_h/adu_h_calstar, flux_j/adu_j_calstar]) #W.m^-2

pos = (np.arange(len(obj))-np.argmax(np.mean(masked_obj,0)))*0.01225

obj_final = masked_obj*adu
obj_final_std = masked_obj_std*adu
calstar_final = masked_calstar*adu
calstar_final_std = masked_calstar_std*adu

pos = -pos[901:50:-1]
lambdas = lambdas[873:150:-1]

obj_final = obj_final[873:150:-1, 901:50:-1]
obj_final_std = obj_final_std[873:150:-1, 901:50:-1]
calstar_final = calstar_final[873:150:-1, 901:50:-1]
calstar_final_std = calstar_final_std[873:150:-1, 901:50:-1]

# np.savetxt('./REDUCED_DATA/lam', lambdas)
# np.savetxt('./REDUCED_DATA/pos', pos)
# np.savetxt('./REDUCED_DATA/obj', obj_final)
# np.savetxt('./REDUCED_DATA/obj_std', obj_final_std)
# np.savetxt('./REDUCED_DATA/cal', calstar_final)
# np.savetxt('./REDUCED_DATA/cal_std', calstar_final_std)


from PIL import Image
from random import randint

class Point(object):
    def __init__(self, x=0, y=0):
        self.x = int(x)
        self.y = int(y)

class ChaosGame(object):
    def __init__(self, width, height, fixed_points, start_point):
        self.img_width = width
        self.img_height = height
        self.fixed_points = fixed_points
        self.current_point = start_point

        self.img = Image.new("RGB", (self.img_width, self.img_height), "black")

        for i in range(0, 1000000):
            self.nextPoint()

        for point in self.fixed_points:
            self.img.putpixel((point.x, point.y), (255, 0, 0))

        self.img.save("out.png")
        print("done")

    def nextPoint(self):
        rand = randint(0, len(self.fixed_points)-1)
        x = int((self.current_point.x + self.fixed_points[rand].x)/2)
        y = int((self.current_point.y + self.fixed_points[rand].y)/2)

        self.current_point = Point(x, y)
        self.img.putpixel((x, y), (142, 68, 173))
        #print("new point @ x: {0} y: {1}".format(x, y))

if __name__ == "__main__":
    fixed_points = [
        Point(10, 10),
        Point(990, 60),
        Point(450, 990)
    ]
    game = ChaosGame(1000, 1000, fixed_points, Point(200, 200))


import unittest
import uuid
import os
import time

from studio.pubsub_queue import PubsubQueue
from studio.sqs_queue import SQSQueue
from studio.local_queue import LocalQueue, get_local_queue_lock

from studio.util import has_aws_credentials
from studio import logs

class DummyContextManager(object):
    def __init__(self):
        pass

    def __enter__(self):
        return self

    def __exit__(self, *args):
        pass

class QueueTest(object):
    def get_queue(self):
        pass

    def test_simple(self):
        with self.get_lock():
            q = self.get_queue()
            q.clean()
            data = str(uuid.uuid4())

            q.enqueue(data)
            recv_data = q.dequeue(timeout=self.get_timeout())

            self.assertEquals(data, recv_data)
            self.assertTrue(q.dequeue() is None)

    def test_clean(self):
        with self.get_lock():
            q = self.get_queue()
            q.clean()
            data = str(uuid.uuid4())

            q.enqueue(data)
            q.clean(timeout=self.get_timeout())

            self.assertTrue(q.dequeue(timeout=self.get_timeout()) is None)

    def get_lock(self):
        return DummyContextManager()

    def get_timeout(self):
        return 0

class LocalQueueTest(QueueTest, unittest.TestCase):
    def get_queue(self):
        return LocalQueue()

    def get_lock(self):
        return get_local_queue_lock()

class DistributedQueueTest(QueueTest):

    def test_unacknowledged(self):
        q = self.get_queue()
        q.clean()
        data1 = str(uuid.uuid4())
        data2 = str(uuid.uuid4())

        q.enqueue(data1)
        q.enqueue(data2)

        recv1 = q.dequeue(timeout=self.get_timeout())
        time.sleep(15)
        recv2 = q.dequeue(timeout=self.get_timeout())

        self.assertTrue(data1 == recv1 or data2 == recv1)
        self.assertTrue(data1 == recv2 or data2 == recv2)
        self.assertFalse(recv1 == recv2)

        self.assertTrue(q.dequeue() is None)

    def test_two_receivers(self):
        logger = logs.getLogger('test_two_receivers')
        logger.setLevel(10)
        q1 = self.get_queue()
        q1.clean()

        q2 = self.get_queue(q1.get_name())

        data1 = str(uuid.uuid4())
        data2 = str(uuid.uuid4())

        logger.debug('data1 = ' + data1)
        logger.debug('data2 = ' + data2)

        q1.enqueue(data1)

        self.assertEquals(data1, q2.dequeue(timeout=self.get_timeout()))

        q1.enqueue(data1)
        q1.enqueue(data2)

        recv1 = q1.dequeue(timeout=self.get_timeout())
        recv2 = q2.dequeue(timeout=self.get_timeout())

        logger.debug('recv1 = ' + recv1)
        logger.debug('recv2 = ' + recv2)

        self.assertTrue(data1 == recv1 or data2 == recv1)
        self.assertTrue(data1 == recv2 or data2 == recv2)
        self.assertFalse(recv1 == recv2)

        self.assertTrue(q1.dequeue() is None)
        self.assertTrue(q2.dequeue() is None)

    def test_hold(self):
        q = self.get_queue()
        q.clean()

        data = str(uuid.uuid4())
        q.enqueue(data)

        msg, ack_id = q.dequeue(acknowledge=False, timeout=60)
        q.hold(ack_id, 1.5)

        self.assertTrue(q.dequeue(timeout=10) is None)

        msg = q.dequeue(timeout=self.get_timeout())
        self.assertEquals(data, msg)

    def get_timeout(self):
        return 120

@unittest.skipIf(
    'GOOGLE_APPLICATION_CREDENTIALS' not in
    os.environ.keys(),
    'GOOGLE_APPLICATION_CREDENTIALS environment ' +
    'variable not set, won'' be able to use google ' +
    'PubSub')
class PubSubQueueTest(DistributedQueueTest, unittest.TestCase):
    _multiprocess_shared_ = True

    def get_queue(self, name=None):
        name = 'pubsub_queue_test' + str(uuid.uuid4()) if not name else name
        print(name)
        return PubsubQueue(name)

@unittest.skipIf(
    not has_aws_credentials(),
    "AWS credentials is not present, cannot use SQSQueue")
class SQSQueueTest(DistributedQueueTest, unittest.TestCase):
    _multiprocess_shared_ = True

    def get_queue(self, name=None):
        return SQSQueue(
            'sqs_queue_test_' + str(uuid.uuid4()) if not name else name)

if __name__ == '__main__':
    unittest.main()


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 22 22:04:42 2022

@author: kalya_kl8c3da
"""

import os
import cv2
import timeit
import matplotlib.pyplot as plt

'''
dataset_path = 'C:/Users/kalya_kl8c3da/Downloads/facedata/dataset/celebrity_face_dataset/'
path = 'C:/Users/kalya_kl8c3da/Downloads/facedata/dataset/celebrity_face_dataset/deepika padukone face/'
destination = 'C:/Users/kalya_kl8c3da/Downloads/facedata/shah rukh khan face_copy_test/'
folder = os.listdir(path)

names = []
for file in folder:
    names.append(file)
face_detector = cv2.CascadeClassifier('C:/ProgramData/Anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
for image_names in range(len(names)):
    img_path = path + names[image_names]
    img = cv2.imread(img_path)
    faces = face_detector.detectMultiScale(img, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0, 0, 0), 0)
        cv2.imwrite(destination + str(names[image_names]) , img[y:y+h,x:x+w])
'''

# dataset_path = 'C:/Users/kalya_kl8c3da/Downloads/facedata/dataset/celebrity_face_dataset/'
dataset_path = 'C:/Users/kalya_kl8c3da/Downloads/facedata/cropped_kalyan_banaja_saurav_color_gray/'
destination = 'C:/Users/kalya_kl8c3da/Downloads/facedata/shah rukh khan face_copy_test/'

def face_extraction(face_dataset_path):
    start = timeit.default_timer()
    folder = os.listdir(face_dataset_path)
    face_detector = cv2.CascadeClassifier('C:/ProgramData/Anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')

    names = []
    sub_folder_path = []
    # image_pth = []
    sf_images = []

    for folder_names in folder:
        names.append(folder_names)
    for file in range(len(names)):
        sub_folder = face_dataset_path + names[file]  + '/'
        sub_folder_path.append(sub_folder)
        for i in range(len(sub_folder_path)):
            sf_img1 = os.listdir(sub_folder_path[i])
            for j in range(len(sf_img1)):
                sf_img = str(sub_folder_path[i])  + str(sf_img1[j])
                sf_images.append(sf_img)
    print(sf_images)

    for image_names in range(len(sf_images)):
        img_path =  sf_images[image_names]
        img = cv2.imread(img_path)
        faces = face_detector.detectMultiScale(img, 1.3, 5)
        print(faces)
        for (x,y,w,h) in faces:
            cv2.rectangle(img, (x,y), (x+w,y+h), (0, 0, 0), 0)
            cv2.imwrite(str(sf_images[image_names]) , img[y:y+h,x:x+w])

    stop = timeit.default_timer()
    print('Tptal Time Taken to crop dataset: ', stop - start)
face_extraction(dataset_path)

#Your statements here


from flask_app import app
from flask_app.models import user, network, project, convo, skill
from flask import  flash, request, redirect, url_for, render_template, session
import urllib.request
import os
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = 'flask_app/static/uploads/'

app.secret_key = "I solomley swear that im up to no good."
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/picture', methods=['POST'])
def upload_image():

    if 'user_id' not in session:
        return redirect('/')

    user_data = {'id': session['user_id']}
    user_id = {'user_id':session['user_id']}

    current_user = user.User.get_one(user_data)

    profile_data = {
        'id': session['user_id']
    }
    profile_user = user.User.get_one(profile_data)

    pic = url_for('static',filename=f'uploads/{profile_user.pic_url}')

    all_convos = convo.Convo.get_all_from_user(user_data)

    all_projects = project.Project.get_all_from_user(user_data)

    skill_data = {'user_id': session['user_id']}
    all_skills = skill.Skill.get_all_from_user(skill_data)

    network_data = {'user_id': session['user_id']}
    all_links = network.Network.get_all_from_user(network_data)

    if 'file' not in request.files:
        flash('No file part', 'pic')
        return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No image selected for uploading', 'pic')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        upload = {'pic_url': filename,'id':request.form['id']}
        pic = url_for('static',filename = f'/uploads/{filename}')
        current_user.pic_url = pic
        user.User.save_user_pic(upload)
        #print('upload_image filename: ' + filename)
        flash('Image successfully uploaded and displayed above', 'pic')
        # return render_template('profile.html', filename=filename, current_user=current_user, profile_user=profile_user, all_convos=all_convos, all_projects=all_projects, all_skills=all_skills, all_links=all_links, pic=pic)
        return redirect(f'/profile/{current_user.id}')
    else:
        flash('Allowed image types are - png, jpg, jpeg, gif', 'pic')
        return redirect(request.url)

# @app.route('/display/<filename>')
# def display_image(filename):
#     #print('display_image filename: ' + filename)
#     return redirect(url_for('static', filename='uploads/' + filename), code=301)


import scrapy
import re
from spiders.notebookcheck import NotebookCheckSpider
from spiders.process_data.device_id_detector import detect_pu_ids_in_laptop_data
from spiders.process_data.regex import PRICE_REGEX, RAM_REGEX, WEIGHT_REGEX, DEVICE_WORD_WITH_MISSING_SPACE_REGEX
from w3lib.html import remove_tags
from bs4 import BeautifulSoup

PAGE_AMOUNT = 1
ITEM_AMOUNT = -1

LABELS_MAP = {
        'מעבד': 'cpu',
        'סוג מעבד': 'cpu',
        'כרטיס גרפי': 'gpu',
        'כרטיס מסך': 'gpu',
        'גרפיקה': 'gpu',
        'מעבד גרפי': 'gpu',
        }

HEBREW_ALPHABET = 'קראטוןםפשדגכעיחלךףזסבהנמצתץ'

def create_page_url(page_index:int)->str:
    return 'https://www.lastprice.co.il/MoreProducts.asp?offset=%s&catcode=85'%page_index

def is_all_hebrew_letters(word:str)->bool:
    return all([c in HEBREW_ALPHABET for c in word])

class LastPriceSpider(NotebookCheckSpider):
    name = 'lastprice'

    custom_settings = {
        'FEEDS': {
            'lastprice-laptops.json': {'format': 'json'}
        },
        'DUPEFILTER_DEBUG': True,
        'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter'
    }

    # Request all of the pages use the parse callback
    def start_requests(self):
        page_urls = [create_page_url(page_index) for page_index in range(PAGE_AMOUNT)]

        # get the first url
        url = page_urls.pop()
        yield scrapy.Request(url=url,
                            callback=self.collect_pages,
                            meta={'page_urls':page_urls, 'laptop_urls': []})

    def collect_pages(self, response):
        '''
        Recursive collection of pages
        '''
        page_urls = response.meta['page_urls']
        laptop_urls = response.meta['laptop_urls']

        laptop_urls_cur_page = response.css('a.prodLink::attr(href)').getall()

        # Limiting the amount of laptops (-1 means no limit)
        if ITEM_AMOUNT!=-1:
            laptop_urls_cur_page = laptop_urls_cur_page[:ITEM_AMOUNT]

        # add the laptop urls from the current page
        laptop_urls.extend(laptop_urls_cur_page)

        if len(page_urls) == 0:
            # if we finished collecting the pages, start collecting the laptops
            # get the first url
            url = laptop_urls.pop()
            yield scrapy.Request(url=url,
                                callback=self.parse_laptops, meta={
                                    'laptop_urls': laptop_urls,
                                })
        else:
            url = page_urls.pop()
            yield response.follow(url=url,callback=self.collect_pages,
                                meta={
                                    'page_urls': page_urls,
                                    'laptop_urls': laptop_urls
                                })

    def extract_laptop_images(self, response)->str:
        urls = []
        for thumbnail_url in response.css('img.ms-thumb::attr(src)').getall():
            # thumbnails have the same name as the images, but are in a directory called 180.
            # to get the actual image url, remove the '180' directory from the url
            image_url = thumbnail_url.replace('180/','')

            # the thumbnail url doesn't contain the 'http:' at the start, and starts with
            # '//www.lastprice.co.il' for some reason, so add back the 'https:' part
            image_url = 'https:' + image_url

            urls.append(image_url)
        return urls

    def extract_laptop_key_value_data(self, response)->dict:
        '''
        Extracts key-value data from the laptop's page, according to the keys in the `LABELS_MAP` map
        '''

        laptop_data = {}

        paragraphs = response.css('#descr > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > p').getall()
        for paragraph in paragraphs:
            text = remove_tags(paragraph).strip()
            lines = text.splitlines()
            for i in range(len(lines)):
                line = lines[i].strip()

                # convert annoying non braking spaces to normal spaces
                line = line.encode().replace(b"\xc2\xa0", b" ").decode()

                # the key is just all the first hebrew words in the line
                key = ''

                # either a delimiter with an optional space, or a space with
                # an optional delimiter
                for word in re.split('(?:[:-][  ]?)|(?:[:-]? )', line):
                    contained_separator = False

                    # if this word contains a key-value separator, remove it,
                    # and if it is also a hebrew word, then it must be the last
                    # word in the key, because after it comes the separator.
                    if ':' in word or '-' in word:
                        contained_separator = True

                        # remove the separator so we can check that the rest
                        # of the string is hebrew only
                        word = word.replace(':','').replace('-','')

                    if is_all_hebrew_letters(word):
                        key += word + ' '

                        # if the word contained a separator, it is the last
                        # word in the key.
                        if contained_separator:
                            break
                    else:
                        break

                # if the line contained no hebrew words, it is not a key-value
                # pair.
                if len(key)==0:
                    continue

                # the rest of the string represents the key's value
                value = line[len(key):]

                value = value.strip().replace('-',' ')
                key = key.strip()

                # if the key has not value, skip it
                if len(value) == 0:
                    continue

                if key in LABELS_MAP:
                    # map the key from its hebrew name to its english name
                    mapped_key = LABELS_MAP[key]

                    # special case for gpu, where lastprice decided to write the word 'GPU'
                    # as the value, and write the actual gpu on the next line
                    if mapped_key == 'gpu' and value == 'GPU':
                        value = lines[i+1].strip()

                    # special case for missing spaces in the value, becuase notebookcheck expects
                    # spaces between words.
                    for device_word_with_missing_space in DEVICE_WORD_WITH_MISSING_SPACE_REGEX.findall(value):
                        # a missing space is detected by finding a word that has a digit right after it,
                        # with no space
                        word,digit_after_word = device_word_with_missing_space
                        value = value.replace(word+digit_after_word,word+' '+digit_after_word)

                    laptop_data[mapped_key] = value
                elif 'זיכרון' in key or 'זכרון' in key and not 'מקסימלי' in key:
                    # the lasprice website uses many labels for representing the amount of ram
                    # so to find the right one we just check if it contains the word 'זיכרון' or its variant,
                    # does not contain the word 'מקסימלי', and contains the regex of the ram.
                    ram_matches = RAM_REGEX.findall(value)
                    if len(ram_matches) > 0:
                        ram_text = ram_matches[0]

                        # remove the 'GB' at the end
                        ram_text = ram_text[:-len('GB')]

                        laptop_data['ram'] = int(ram_text)
                elif 'משקל' in key:
                    # the lasprice website uses many labels for representing the weight of a laptop
                    # so to find the right one we just check if it contains the word 'משקל',
                    # and contains the regex of the weight.
                    weight_matches = WEIGHT_REGEX.findall(value)
                    if len(weight_matches) > 0:
                        weight_text = weight_matches[0]
                        laptop_data['weight'] = float(weight_text)

        return laptop_data

    def extract_laptop_price(self, response)->float:
        '''
        Extracts the laptop's price from the laptop's page
        '''

        # the buy now label contains the price
        buy_now_label = response.css('div.no-padding-desktop > div:nth-child(1) > h2:nth-child(1)').get()

        # beautifulsoup is used here because scrapy doesn't handle well the special symbols in the html
        # code.
        # not using beautifulsoup for the whole document for performance reasons, instead using it
        # just on the buy now label
        # using find('h2') because beautifulsoup appends an html and body tags to the given html document
        # if it doesn't have these tags.
        bs = BeautifulSoup(buy_now_label,'html5lib').find('h2')

        # find the first child that contains the '₪' symbol
        buy_now_label_text = next(child for child in bs.children if '₪' in child)

        price_matches = PRICE_REGEX.findall(buy_now_label_text)

        # sometimes the buy label contains another number before the price, inside a hidden span,
        # so we should always just take the last match, which will be the price
        price_text = price_matches[-1]

        # remove the ',' from the price if it is present
        price_text = price_text.replace(',','')

        return float(price_text)

    def extract_laptop_data(self, response)->dict:
        '''
        Extracts a dictionary of laptop data from the laptop's page
        '''

        # key value data
        laptop_data = self.extract_laptop_key_value_data(response)

        # additional fields that are not in key value pairs

        # url
        laptop_data['url'] = response.url

        # images
        laptop_data['image_urls'] = self.extract_laptop_images(response)

        # brand
        # extracting the brand from the url to the brand's lastprice page
        brand_url = response.css('a.h4::attr(href)').get()
        laptop_data['brand'] = brand_url.split('/')[-1]

        # model
        laptop_data['model'] = response.css('span.h4::text').get()

        # price
        laptop_data['price'] = self.extract_laptop_price(response)

        return laptop_data

    # collecting laptop data from each laptop page
    def parse_laptops(self, response):
        '''
        Recursive collection of laptop data
        '''

        laptop_urls = response.meta['laptop_urls']

        laptop_data = self.extract_laptop_data(response)
        detect_pu_ids_in_laptop_data(laptop_data)
        self.laptops.append(laptop_data)

        if len(laptop_urls) == 0:
            yield self.with_benchmarks()
        else:
            url = laptop_urls.pop()
            yield response.follow(url=url, callback=self.parse_laptops,
                                meta={
                                    'laptop_urls': laptop_urls,
                                })


import torch
from torchvision.models.mobilenet import mobilenet_v2
import numpy as np

class DenseBox(torch.nn.Module):
    def __init__(self, num_classes=8, feature_height=6, feature_width=20):
        super().__init__()
        self.backbone = mobilenet_v2(pretrained=True)  # torchvision中的mobilenet_v2
        self.num_classes = num_classes
        h, w = torch.meshgrid(torch.arange(feature_height),
                              torch.arange(feature_width))  # 生成中心点
        centers = torch.stack((w, h, w, h), axis=2) + 0.5
        centers = centers.reshape(feature_height, feature_width, 4).permute(2, 0, 1)
        self.conv = torch.nn.Conv2d(1280, num_classes + 4, kernel_size=3, padding=1)
        self.register_buffer("centers", centers.float(), persistent=False)  # self.center=center
        self.ce_loss = torch.nn.CrossEntropyLoss()  # 分类loss
        self.l2_loss = torch.nn.MSELoss()  # box回归的loss

    def forward(self, images, targets):
        features = self.backbone.features(images)
        features = self.conv(features)
        n, _, h, w = features.shape  # [B, 8+4, 6, 20]
        cls_target = torch.zeros((n, self.num_classes, h, w), dtype=torch.float)
        reg_target = torch.zeros((n, 4, h, w), dtype=torch.float)
        ratio = 0.3
        for i in range(n):
            for box, cls in zip(targets["boxes"][i], targets["classes"][i]):
                box = box * np.array([w, h, w, h])
                bbox_center_x = (box[0] + box[2]) * 0.5
                bbox_center_y = (box[1] + box[3]) * 0.5

                bbox_w = box[2] - box[0]
                bbox_h = box[3] - box[1]

                # 计算正样本区域
                org_x = int(bbox_center_x - float(ratio * bbox_w * 0.5) + 0.5)
                org_y = int(bbox_center_y - float(ratio * bbox_h * 0.5) + 0.5)
                end_x = int(float(org_x) + float(ratio * bbox_w) + 0.5)
                end_y = int(float(org_y) + float(ratio * bbox_h) + 0.5)
                cls_target[i, :, org_y: end_y + 1, org_x: end_x + 1] = cls + 1.0
                reg_target[i, :, org_y: end_y + 1, org_x: end_x + 1] = (
                    self.centers[:, org_y: end_y + 1, org_x: end_x + 1]
                    - torch.tensor(box, dtype=torch.float)[:, None, None])

        cls_loss = self.ce_loss(features[:, :self.num_classes, :, :], cls_target)
        reg_loss = self.l2_loss(features[:, self.num_classes:, :, :], reg_target)
        return {"cls": cls_loss, "reg": reg_loss}