import numpy as np
import CNN_utils

# ref ：https://blog.csdn.net/Daycym/article/details/83826222
class Convolution:
    def __init__(self, ch_in, ch_out, kernel_size=(5, 5), stride=1, pad=0):
        # Init parameters
        self.W = np.random.random(size=(ch_out, ch_in, kernel_size[0], kernel_size[1])) - 0.5
        self.b = np.zeros((1, 1, 1, ch_out), dtype=np.float32)
        # get stride and pad
        self.stride = stride
        self.pad = pad
        # temp data, used in backword
        self.x = None
        self.feature_col = None
        self.W_col = None
        # gradient of W and b
        self.dW = None
        self.db = None

    def forward(self, x):
        self.x = x

        kernel_nb, C, FH, FW = self.W.shape
        # size of input data
        N, C, H, W = x.shape
        # size of output feature
        out_h = 1 + int((H + 2 * self.pad - FH) / self.stride)
        out_w = 1 + int((W + 2 * self.pad - FW) / self.stride)
        # transform array to col (im2col)
        self.feature_col = CNN_utils.im2col(x, FH, FW, self.stride, self.pad)
        # transform kernel to col vec (6, c*h*w)' ==> (c*h*w, 6)
        self.W_col = self.W.reshape(kernel_nb, -1).T
        # forward pass
        out = np.dot(self.feature_col, self.W_col) + self.b
        out = out.reshape(N, out_h, out_w, -1).transpose(0, 3, 1, 2)
        return out

    def backward(self, dout):
        kernel_nb, C, FH, FW = self.W.shape
        dout = dout.transpose(0, 2, 3, 1).reshape(-1, kernel_nb)
        self.db = np.sum(dout, axis=0)
        self.dW = np.dot(self.feature_col.T, dout)
        self.dW = self.dW.transpose(1, 0).reshape(kernel_nb, C, FH, FW)
        dcol = np.dot(dout, self.W_col.T)
        # reverse transform of im2col
        dx = CNN_utils.col2im(dcol, self.x.shape, FH, FW, self.stride, self.pad)
        return dx

class Pooling:
    def __init__(self, pool_h, pool_w, stride=1, pad=0):
        self.pool_h = pool_h
        self.pool_w = pool_w
        self.stride = stride
        self.pad = pad

        self.x = None
        self.arg_max = None

    def forward(self, x):
        N, C, H, W = x.shape
        out_h = int(1 + (H - self.pool_h) / self.stride)
        out_w = int(1 + (W - self.pool_w) / self.stride)
        # 展开
        col = CNN_utils.im2col(x, self.pool_h, self.pool_w, self.stride, self.pad)
        col = col.reshape(-1, self.pool_h * self.pool_w)
        # 最大值
        arg_max = np.argmax(col, axis=1)
        out = np.max(col, axis=1)
        # 转换
        out = out.reshape(N, out_h, out_w, C).transpose(0, 3, 1, 2)
        self.x = x
        self.arg_max = arg_max
        return out

    def backward(self, dout):
        dout = dout.transpose(0, 2, 3, 1)
        pool_size = self.pool_h * self.pool_w
        dmax = np.zeros((dout.size, pool_size))
        dmax[np.arange(self.arg_max.size), self.arg_max.flatten()] = dout.flatten()
        dmax = dmax.reshape(dout.shape + (pool_size,))
        dcol = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2], -1)
        dx = CNN_utils.col2im(dcol, self.x.shape, self.pool_h, self.pool_w, self.stride, self.pad)
        return dx

class FCLayer:
    def __init__(self, inplanes, outplanes, preweight=None):
        # Load pre weight
        if preweight is None:
            self.weight = np.random.rand(inplanes,
                                         outplanes) - 0.5  # self.weight = numpy.random.normal(inplanes, outplanes)
            self.bias = np.random.rand(outplanes) - 0.5
        else:
            self.weight, self.bias = preweight
        self.input = None
        self.output = None
        self.wgrad = np.zeros(self.weight.shape)
        self.bgrad = np.zeros(self.bias.shape)

    def forward(self, x):
        self.input = x
        self.output = np.dot(self.input, self.weight) + self.bias
        return self.output

    def backword(self, grad):
        # grad (10,1) expected
        self.bgrad = np.mean(grad, axis=0)
        self.wgrad = np.dot(self.input.T, grad)  # use direate of mat mul
        grad = np.dot(grad, self.weight.T)  # 666!
        return grad

class CEloss:
    def __init__(self):
        self.input = None
        self.q = None
        self.p = None
        self.grad = None
        self.target = None

    def forward(self, input, target=None):
        # input is the output of NN [64,10]
        self.input = input
        self.p = target

        temp = np.sum(np.exp(self.input - np.max(self.input, axis=1, keepdims=True)), axis=1, keepdims=True)
        self.q = np.exp(self.input - np.max(self.input, axis=1, keepdims=True)) / temp  # ==> [64,1]

        if self.p is not None:
            loss = -np.sum(np.sum(self.p * np.log(self.q + 1e-3), axis=1)) / self.p.shape[0]  # avg of all examples
            return self.q, loss
        else:
            return self.q

    def backword(self):
        self.grad = (self.q - self.p) / self.p.shape[0]
        return self.grad

class ReLU:
    def __init__(self):
        self.input = None
        self.output = None

    def forward(self, x):
        self.input = x
        x[self.input <= 0] *= 0
        self.output = x
        return self.output

    def backword(self, grad):
        grad[self.input > 0] *= 1
        grad[self.input <= 0] *= 0
        return grad


import pytest
from unittest.mock import MagicMock, mock_open
from collections import namedtuple

@pytest.fixture()
def mock_object(monkeypatch):
    def create_mock_object(object_path, **kwargs):
        mo = MagicMock(**kwargs)
        monkeypatch.setattr(object_path, mo)
        return mo

    return create_mock_object

@pytest.fixture()
def open_mock(monkeypatch):
    open_mock = mock_open()
    monkeypatch.setattr("builtins.open", open_mock)
    return open_mock

@pytest.fixture()
def os_path_join_file_mock(mock_object):
    os = mock_object("os.path.join")
    os.side_effect = lambda *x: "/".join(x)
    return os

@pytest.fixture()
def json_load_mock(mock_object):
    return mock_object("json.load")

@pytest.fixture()
def argparse_mock(mock_object):
    ap = mock_object("argparse.ArgumentParser")
    ap.return_value = ap
    return ap

@pytest.fixture
def configuration_file():
    def func(headers, sects, errors):
        ConfigurationFile = namedtuple(
            "ConfigurationFile", ["headers", "sects", "errors"]
        )
        return ConfigurationFile(headers, sects, errors)

    return func

@pytest.fixture(scope="session")
def build_parsed_output():
    def parsed_output(output_elements):
        """
        builds expected parser output from provided dict
        :param output_elements: dictionary with {stanza: {option: value, ...}, ...}
        :return: parsed_output
        """
        parsed_output = {}
        for stanza, stanza_value in output_elements.items():
            fake_section = MagicMock()
            fake_section.options = {}
            fake_section.name = stanza
            parsed_output.update({stanza: fake_section})
            for option, value in stanza_value.items():
                fake_setting = MagicMock()
                fake_setting.name = option
                fake_setting.value = value
                parsed_output[stanza].options.update({option: fake_setting})
        return parsed_output

    return parsed_output


# Part 1: What is the number of 1-jolt differences multiplied by the number of 3-jolt differences?
# Part 2: What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device?

def main():
    f = open("input/day_10.txt", "r")

    adapters = [int(line.strip()) for line in f]

    jd = jolt_differences(adapters)
    print("part 1:", jd[0] * jd[2])
    print("part 2:", count_distinct_arrangements(adapters))

def jolt_differences(adapters):
    jd = [0, 0, 0]

    jolt = 0
    adapters.sort()

    for aj in adapters:
        diff = aj - jolt
        jd[diff - 1] += 1
        jolt = aj

    jd[2] += 1

    return jd

def count_distinct_arrangements(adapters):
    adapters.sort()
    adapters = [0] + adapters

    mem, count = count_distinct_arrangements_recursive(adapters, {})

    return count

def count_distinct_arrangements_recursive(adapters, mem):
    if len(adapters) == 1:
        return mem, 1

    count = 0
    c = adapters[0]
    ni = 1
    while ni < len(adapters) and adapters[ni] - c <= 3:
        if adapters[ni] in mem:
            count += mem[adapters[ni]]
        else:
            mem, child_count = count_distinct_arrangements_recursive(adapters[ni:], mem)
            count += child_count
            mem[adapters[ni]] = child_count

        ni += 1

    return mem, count

if __name__ == "__main__":
    main()


import requests, uuid, json, os, sys, shutil
from pathlib import Path
from glob import glob

key = sys.argv[1]
language_code = sys.argv[2]
english_dir = f"{os.getcwd()}/wakawatch/fastlane/metadata/en-US/"
metadata_dir = f"{os.getcwd()}/wakawatch/fastlane/metadata/{language_code}/"
os.makedirs(metadata_dir)

for file_name in os.listdir(english_dir):
    source = english_dir + file_name
    destination = metadata_dir + file_name
    shutil.copy(source, destination)

endpoint = "https://api.cognitive.microsofttranslator.com/"
location = "westus2"
path = 'translate'
constructed_url = endpoint + path

params = {
    'api-version': '3.0',
    'from': 'en',
    'to': [language_code]
}

headers = {
    'Ocp-Apim-Subscription-Key': key,
    'Ocp-Apim-Subscription-Region': location,
    'Content-Type': 'application/json',
    'X-ClientTraceId': str(uuid.uuid4())
}

for file_name in os.listdir(metadata_dir):
    if file_name in ['description.txt', 'keywords.txt', 'subtitle.txt', 'release_notes.txt']:
        with open(metadata_dir + file_name, 'r+') as file:
            if file_name == 'release_notes.txt':
                file.seek(0)
                file.truncate()
                file.write('')
                continue

            text_to_translate = file.read()
            body = [{
                'text': text_to_translate
            }]

            request = requests.post(constructed_url, params=params, headers=headers, json=body)
            response = request.json()

            translation = response[0]['translations'][0]['text']
            delete_subtitle_file = file_name == "subtitle.txt" and len(translation) > 30
            file.seek(0)
            file.truncate()
            file.write(translation)

            if delete_subtitle_file:
                os.remove(metadata_dir + file_name)


import os
import csv

with open("PyPoll_Resources_election_data.csv", 'r') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',')
    csv_header=next(csvreader)

    PollData=list(csvreader)

    votes=[]
    candidates=[]
    unique_candidates=[]
    Li_votes=[]
    Khan_votes=[]
    Correy_votes=[]
    OTooley_votes=[]

    for row in PollData:
        votes.append(str(row[0]))
    total_votes=len(votes)
    for row in PollData:
        candidates.append(str(row[2]))

    unique_candidates=list(set(candidates))

    for row in PollData:
        if row[2]=="Li":
            Li_votes.append(str(row[0]))
        if row[2]=="Khan":
            Khan_votes.append(str(row[0]))
        if row[2]=="Correy":
            Correy_votes.append(str(row[0]))
        if row[2]=="O'Tooley":
            OTooley_votes.append(str(row[0]))

    Li_tvotes=len(Li_votes)
    Khan_tvotes=len(Khan_votes)
    Correy_tvotes=len(Correy_votes)
    OTooley_tvotes=len(OTooley_votes)

    Li_percent=(int(len(Li_votes))/int(len(votes)))*100
    Khan_percent=(int(len(Khan_votes))/int(len(votes)))*100
    Correy_percent=(int(len(Correy_votes))/int(len(votes)))*100
    OTooley_percent=(int(len(OTooley_votes))/int(len(votes)))*100

    if Li_percent>Khan_percent and Li_percent>Correy_percent and Li_percent>OTooley_percent:
        winner= "Li"
    if Khan_percent>Li_percent and Khan_percent>Correy_percent and Khan_percent>OTooley_percent:
        winner ="Khan"
    if Correy_percent>Li_percent and Correy_percent>Khan_percent and Correy_percent>OTooley_percent:
        winner="Correy"
    if OTooley_percent>Li_percent and OTooley_percent>Khan_percent and OTooley_percent>Correy_percent:
        winner="O'Tooley"

    print()
    print("Election Results")
    print("----------------------------------------------")
    print(f"Total votes: {total_votes}")
    print("----------------------------------------------")
    print(f'Khan: {round(Khan_percent,0)}% ({Khan_tvotes})')
    print(f'Correy: {round(Correy_percent,0)}% ({Correy_tvotes})')
    print(f'Li: {round(Li_percent,0)}% ({Li_tvotes})')
    print(f"O'Tooley: {round(OTooley_percent,0)}% ({OTooley_tvotes})")
    print("----------------------------------------------")
    print(f"Winner: {winner}")

f = open('Results_PyPoll.txt','w')
f.write("\n")
f.write("Election Results")
f.write("\n")
f.write("------------------------------------")
f.write("\n")
f.write(f"Total votes: {total_votes}")
f.write("\n")
f.write(f'Khan: {round(Khan_percent,0)}% ({Khan_tvotes})')
f.write("\n")
f.write(f'Correy: {round(Correy_percent,0)}% ({Correy_tvotes})')
f.write("\n")
f.write(f'Li: {round(Li_percent,0)}% ({Li_tvotes})')
f.write("\n")
f.write(f"O'Tooley: {round(OTooley_percent,0)}% ({OTooley_tvotes})")
f.write("\n")
f.write("----------------------------------------------")
f.write("\n")
f.write(f"Winner: {winner}")

f.close()


"""
function for plotting the AbMetaAnalysis results
"""

# Info
__author__ = 'Boaz Frankel'

# Imports
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
import seaborn as sns
import math
from changeo.Gene import getFamily
import typing

def plot_compare_to_reference_cdr3_df(df, figsize=(15, 10)):
    """
    plot the results of compare_to_reference_df
    :param df: output data frame of compare_to_reference_df
    :param figsize: size of the figures
    :return:
    """
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=figsize, sharex=True)

    ax1.bar(range(0, len(df) * 2, 2), df['CASE'].absolute)
    ax1.bar(range(1, len(df) * 2, 2), df['CTRL'].absolute)
    ax1.set_xticks([])
    ax1.set_ylabel('Matches', fontsize=20)
    ax1.legend(labels=['CASE', 'CTRL'], fontsize=20)
    ax1.set_yticks(
        range(df.xs('absolute', level=1, axis=1).max().max() + 1),
        range(df.xs('absolute', level=1, axis=1).max().max() + 1),
        fontsize=20
    )
    ax1.grid(axis='y')
    ax1.set_title('(A)', fontsize=20, loc='left')

    ax2.bar(range(0, len(df) * 2, 2), df['CASE'].support)
    ax2.bar(range(1, len(df) * 2, 2), df['CTRL'].support)
    ax2.set_xticks(np.arange(0.5, len(df) * 2, 2), df.index, fontsize=20)
    ax2.set_ylabel('Support', fontsize=20)
    ax2.set_xlabel('Junction AA length', fontsize=20)
    ax2.set_yticks(
        range(0, math.ceil(df.xs('support', level=1, axis=1).max().max() / 200) * 200, 200),
        range(0, math.ceil(df.xs('support', level=1, axis=1).max().max() / 200) * 200, 200),
        fontsize=20
    )
    ax2.grid(axis='y', ls='--')
    ax2.set_title('(B)', fontsize=20, loc='left')

def boxplot_features(
    by_subj_df,
    by_study_df,
    aliase2study,
    case_aliases,
    ctrl_aliases,
    colors,
    axes,
    case_ctrl_color_diff=3,
    outlier_th=2.5,
    y='prop',
    ylabel="Freq"
):
    """
    2 row figure - upper banners are boxplots of motifs abundance between samples of the same study, lower banners are boxplots of motifs
    of average per study motif abundance.
    :param by_subj_df: by_subj motif analysis df, output of get_motif_analysis
    :param by_study_df: by_study motif analysis df, output of get_motif_analysis
    :param aliase2study: mapping of study id to alias name
    :param case_aliases: list of case studies aliases
    :param ctrl_aliases: list of ctrl studies aliases
    :param colors: colors for the case and ctrl aliases
    :param axes: subplots axes to plot - must match the dimension of teh input data frames
    :param case_ctrl_color_diff: color difference between the case_aliases and ctrl_aliases
    :param outlier_th: zscore threshold to discard outliers from the plotting
    :param y: whe metric to use for the figures
    :param ylabel: string to display as y label
    :return:
    """
    positions = np.arange(0, 10, 1)
    for i, col in enumerate(by_subj_df.loc[:, y].columns):
        ax = axes[0, i]
        for alias_itr, alias in enumerate(case_aliases):
            color = colors[alias_itr]
            ax.boxplot(
                x=by_subj_df.loc[('CASE', aliase2study[alias]), (y, col)].loc[
                    by_subj_df.loc[('CASE', aliase2study[alias]), ('zscore', col)].abs() < outlier_th
                ],
                positions=[positions[alias_itr]],
                boxprops=dict(lw=2, color=color),
                medianprops=dict(lw=2, color='black'),
                whiskerprops=dict(lw=2, color=color),
                capprops=dict(lw=2, color=color),
                widths=0.7,
                flierprops=dict(
                    marker='o', markerfacecolor=color, markersize=2, linestyle='none', markeredgecolor=color
                )
            )
            ax.set_xticks([])
            ax.grid(axis='y', linestyle='--', linewidth=1)
            ax.set_title(col, fontsize=20)
        case_max = alias_itr
        for alias_itr, alias in enumerate(ctrl_aliases):
            color = colors[case_max + 1 + case_ctrl_color_diff + alias_itr]
            ax.boxplot(
                x=by_subj_df.loc[('CTRL', aliase2study[alias]), (y, col)].loc[
                    by_subj_df.loc[('CTRL', aliase2study[alias]), ('zscore', col)].abs() < outlier_th
                ],
                positions=[case_max + 3 + positions[alias_itr]],
                boxprops=dict(lw=2, color=color),
                medianprops=dict(lw=2, color='black'),
                whiskerprops=dict(lw=2, color=color),
                capprops=dict(lw=2, color=color),
                widths=0.7,
                flierprops=dict(
                    marker='o', markerfacecolor=color, markersize=2, linestyle='none', markeredgecolor=color
                )
            )
            ax.set_xticks([])
            ax.grid(axis='y', linestyle='--', linewidth=1)
            ax.set_title(col, fontsize=20)
        ctrl_max = alias_itr
        if i == 0:
            if ylabel:
                ax.set_ylabel('Sample ' + ylabel, fontsize=20)
            ax.tick_params(labelsize=20)

        ax = axes[1, i]
        ax.boxplot(
            x=by_study_df.loc['CASE', (y, col)],
            positions=[max(ctrl_max, case_max) / 2],
            medianprops=dict(color='gray'),
            widths=max(ctrl_max, case_max) - 2,
            showfliers=False
        )
        case_positions = np.arange(max(ctrl_max, case_max) / 2 - case_max / 2 * 0.3, 100, 0.3)
        ax.scatter(
            x=case_positions[:sum(by_study_df.loc['CASE', ('zscore', col)].abs() < outlier_th)],
            y=by_study_df.loc['CASE', (y, col)].loc[
                by_study_df.loc['CASE', ('zscore', col)].abs() < outlier_th
            ],
            color=colors[:case_max + 1]
        )
        ax.boxplot(
            x=by_study_df.loc['CTRL', (y, col)],
            positions=[max(ctrl_max, case_max) + 2 + max(ctrl_max, case_max) / 2],
            medianprops=dict(color='gray'),
            widths=max(ctrl_max, case_max) - 2,
            showfliers=False
        )
        ctrl_positions = np.arange(2 + max(ctrl_max, case_max) * 1.5 - ctrl_max / 2 * 0.3, 100, 0.3)
        ax.scatter(
            x=ctrl_positions[:sum(by_study_df.loc['CTRL', ('zscore', col)].abs() < outlier_th)],
            y=by_study_df.loc['CTRL', (y, col)].loc[
                by_study_df.loc['CTRL', ('zscore', col)].abs() < outlier_th
            ],
            color=colors[case_max + 1 + case_ctrl_color_diff: case_max + 1 + case_ctrl_color_diff + ctrl_max + 1]
        )
        ax.grid(axis='y', linestyle='--', linewidth=1)
        ax.set_xticks(
            [max(ctrl_max, case_max) / 2, max(ctrl_max, case_max) + 2 + max(ctrl_max, case_max) / 2],
            ['CASE', 'CTRL'], fontsize=20, rotation='vertical'
        )
        if i == 0:
            if ylabel:
                ax.set_ylabel('Study ' + ylabel, fontsize=20)
            ax.tick_params(labelsize=20)
    y_max_row1, y_max_row2 = 0, 0
    for col_itr in range(len(by_subj_df.loc[:, y].columns)):
        y_max_row1 = max(y_max_row1, axes[0, col_itr].get_ylim()[1])
        y_max_row2 = max(y_max_row2, axes[1, col_itr].get_ylim()[1])
    for col_itr in range(len(by_subj_df.loc[:, y].columns)):
        axes[0, col_itr].set_ylim(0, y_max_row1)
        axes[1, col_itr].set_ylim(0, y_max_row2)

def plot_results(results: pd.DataFrame):
    """
    create box plot of cross validation results
    :param results:  data frame - output of test_rep_classifier
    :return:
    """
    res_dict = {
        'Accuracy': results.accuracy_score,
        'Precision': results.precision_score,
        'Recall': results.recall_score
    }
    fig, ax = plt.subplots()
    ax.boxplot(res_dict.values(), flierprops=dict(markerfacecolor='r', marker='d'))
    ax.set_xticklabels(res_dict.keys())
    ax.set_yticks(np.arange(0, 1.1, 0.1))
    plt.grid(axis='y', linestyle='--', linewidth=1)

def plot_results_side_by_side(results_l, name_l, figsize=(9, 6)):
    """
    create multiple banners plot of different cross validation results
    :param results_l: list of results data frame - outputs of test_rep_classifier
    :param name_l: list of labels matching each results data frame
    :param figsize: figure size tuple
    :return:
    """
    fig, ax = plt.subplots(figsize=figsize)
    colors = sns.color_palette("Set2", len(results_l))

    bp_l = []
    for i, (results, name) in enumerate(zip(results_l, name_l)):
        res_dict = {
            'Accuracy': results.accuracy_score,
            'Precision': results.precision_score,
            'Recall': results.recall_score
        }
        bp = ax.boxplot(
            res_dict.values(),
            positions=[i + 1, i + 1 + len(results_l) + 1, i + 1 + 2 * len(results_l) + 2],
            boxprops=dict(linewidth=2, color=colors[i]),
            medianprops=dict(linewidth=4, color=colors[i]),
            flierprops=dict(markeredgecolor=colors[i])
        )
        bp_l.append(bp)

    leg = ax.legend(
        [bp['boxes'][0] for bp in bp_l], name_l, handlelength=3, fontsize=20
    )
    for i, legobj in enumerate(leg.legendHandles):
        legobj.set_linewidth(10.0)
        legobj.set_color(colors[i])

    ax.set_xticks([(i + 1 + i * len(results_l) + (len(results_l) - 1) / 2) for i in range(3)])
    ax.set_xticklabels(['Accuracy', 'Precision', 'Recall'], fontsize=20, fontweight='bold')
    ax.set_yticks(np.arange(0, 1.1, 0.1))
    ax.set_yticklabels(np.round(np.arange(0, 1.1, 0.1), 2), fontsize=20)
    plt.grid(True, axis='y', linestyle='--', linewidth=1)

def plot_features_side_by_side(feature_table_dict: dict, labels: pd.Series):
    """
    create multiple banners plot, each banner is a scatter plot of the between-samples clusters public case index vs public ctrl index
    :param feature_table_dict: dictionary of public clusters feature tables - outputs of build_feature_table
    :param labels: labels of the samples
    :return:
    """
    fig, ax_arr = plt.subplots(1, 2, figsize=(20, 10))
    all_points = pd.DataFrame()
    max_tick = 0
    for i, (name, feature_table) in enumerate(feature_table_dict.items()):

        ax = ax_arr[i]
        points = pd.value_counts(
            [(x[1].loc[labels.index[labels]].sum(), x[1].loc[labels.index[~labels]].sum()) for x in feature_table.items()]
        )
        if (0, 0) in points.index:
            points = points.drop(index=(0, 0))
        s = points.apply(lambda x: math.log(x, 2) * 100 + 100)
        df = pd.DataFrame(
            [list(map(lambda x: x[0], points.index)), list(map(lambda x: x[1], points.index)), s.to_list()],
            index=['x', 'y', 's']
        ).transpose()
        all_points = pd.concat([all_points, df])
        ax.scatter(
            x='x', y='y', s='s', data=df.loc[(df.y > 0) & (df.x > 0)]
        )
        ax.scatter(
            x='x', y='y', s='s', data=df.loc[df.x == 0]
        )
        ax.scatter(
            x='x', y='y', s='s', data=df.loc[df.y == 0]
        )
        max_tick = max(max_tick, int(max(ax.get_xticks().max(), ax.get_yticks().max())))
        ax.set_xticks(range(max_tick), range(max_tick), fontsize=20)
        ax.set_yticks(range(max_tick), range(max_tick), fontsize=20)
        ax.set_aspect('equal')
        ax.set_xlabel("")
        ax.set_ylabel("")
        ax.set_title('(' + name + ')', fontsize=20, loc='left')

    for ax in ax_arr:
        ax.set_xlim(-1, max_tick)
        ax.set_ylim(-1, max_tick)

        ax.set_xticks(range(max_tick))
        ax.set_xticklabels(range(max_tick), fontsize='20')
        ax.set_yticks(range(max_tick))
        ax.set_yticklabels(range(max_tick), fontsize='20')
        ax.set_aspect('equal')
        ax.grid(axis='both', ls='--')

    fig.supxlabel('Case', fontsize=20, y=0.08)
    fig.supylabel('Control', fontsize=20, x=0.08)

    c = ax.scatter(x='x', y='y', s='s', data=all_points, edgecolors='w', c='w', alpha=0)
    elements = c.legend_elements('sizes', num=10, func=lambda x: ((x - 100) / 100), color='gray', alpha=1)
    legend = fig.legend(
        *(np.flip(elements[0]), np.flip(elements[1])),
        labelspacing=1.5,
        handletextpad=1,
        title=r'$Log_{2}$',
        title_fontsize=20,
        fontsize=15
    )
    legend.get_frame().set_linewidth(0.0)

def create_studies_legend_handles(aliases: list, colors):
    """
    auxiliary method to create handlers for legend of studies aliases
    :param aliases: list of study id aliases
    :param colors: list of colors matching the aliases
    :return:
    """
    handles = []
    for alias_itr, alias in enumerate(aliases):
        handles.append(
            Line2D(
                [0], [0], marker='o', color='w', label=alias, markerfacecolor=colors[alias_itr], markersize=10
            )
        )
    return handles

def boxplot_junction_aa_length(
    axes,
    case_airr_seq_df: pd.DataFrame,
    ctrl_airr_seq_df: pd.DataFrame,
    v_families: typing.Iterable,
    case_aliases: typing.Iterable,
    ctrl_aliases: typing.Iterable,
    aliase2study: dict,
    colors: list,
    case_ctrl_color_diff=3
):
    """
    same as boxplot_features but ad-hoc implementation for junction AA length
    :param axes: array of matplot lib axes
    :param case_airr_seq_df: airr-seq data frame of case sequences
    :param ctrl_airr_seq_df: airr-seq data frane if ctrl sequences
    :param v_families: list of v families for which create banners
    :param case_aliases: list of case study ids aliases
    :param ctrl_aliases: list of ctrl study ids aliases
    :param aliase2study: mapping of alias to study id
    :param colors: colors for the study id aliases
    :param case_ctrl_color_diff: color diff between the case and ctrl study id aliases
    :return:
    """
    by_v_family_by_study_junction_len = pd.Series(
        dtype=object, name='v_family', index=pd.MultiIndex.from_product([['CASE', 'CTRL'], v_families])
    )
    for v_family, frame_index in case_airr_seq_df.index.groupby(case_airr_seq_df.v_call_original.apply(getFamily)).items():
        if v_family not in v_families:
            continue
        by_v_family_by_study_junction_len[
            ('CASE', v_family)
        ] = case_airr_seq_df.loc[frame_index].groupby('study_id').apply(lambda x: x.junction_aa_length.astype(int).tolist())
    for v_family, frame_index in ctrl_airr_seq_df.index.groupby(ctrl_airr_seq_df.v_call_original.apply(getFamily)).items():
        if v_family not in v_families:
            continue
        by_v_family_by_study_junction_len[
            ('CTRL', v_family)
        ] = ctrl_airr_seq_df.loc[frame_index].groupby('study_id').apply(lambda x: x.junction_aa_length.astype(int).tolist())
    positions = np.arange(0, 10, 1)
    for i, v_family in enumerate(v_families):
        ax = axes[0, i]
        for alias_itr, alias in enumerate(case_aliases):
            color = colors[alias_itr]
            ax.boxplot(
                x=by_v_family_by_study_junction_len.loc[('CASE', v_family)].loc[aliase2study[alias]],
                positions=[positions[alias_itr]],
                boxprops=dict(lw=2, color=color),
                medianprops=dict(lw=2, color='black'),
                whiskerprops=dict(lw=2, color=color),
                capprops=dict(lw=2, color=color),
                widths=0.7,
                flierprops=dict(
                    marker='o', markerfacecolor=color, markersize=2, linestyle='none', markeredgecolor=color
                )
            )
            ax.set_xticks([])
            ax.grid(axis='y', linestyle='--', linewidth=1)
            ax.set_title(v_family, fontsize=20)
        case_max = alias_itr
        for alias_itr, alias in enumerate(ctrl_aliases):
            color = colors[case_max + 1 + case_ctrl_color_diff + alias_itr]
            ax.boxplot(
                x=by_v_family_by_study_junction_len.loc[('CTRL', v_family)].loc[aliase2study[alias]],
                positions=[case_max + 3 + positions[alias_itr]],
                boxprops=dict(lw=2, color=color),
                medianprops=dict(lw=2, color='black'),
                whiskerprops=dict(lw=2, color=color),
                capprops=dict(lw=2, color=color),
                widths=0.7,
                flierprops=dict(
                    marker='o', markerfacecolor=color, markersize=2, linestyle='none', markeredgecolor=color
                )
            )
            ax.set_xticks([])
            ax.grid(axis='y', linestyle='--', linewidth=1)
            ax.set_title(v_family, fontsize=20)
        ctrl_max = alias_itr
        if i == 0:
            ax.set_ylabel('Study Junction Length', fontsize=20)
            ax.tick_params(labelsize=20)

        ax = axes[1, i]
        ax.boxplot(
            x=sum(by_v_family_by_study_junction_len.loc[('CASE', v_family)].tolist(), []),
            positions=[max(ctrl_max, case_max) / 2],
            medianprops=dict(color='gray'),
            widths=max(ctrl_max, case_max) - 2,
            showfliers=True
        )
        ax.boxplot(
            x=sum(by_v_family_by_study_junction_len.loc[('CTRL', v_family)].tolist(), []),
            positions=[max(ctrl_max, case_max) + 2 + max(ctrl_max, case_max) / 2],
            medianprops=dict(color='gray'),
            widths=max(ctrl_max, case_max) - 2,
            showfliers=True
        )
        ax.grid(axis='y', linestyle='--', linewidth=1)
        ax.set_xticks(
            [max(ctrl_max, case_max) / 2, max(ctrl_max, case_max) + 2 + max(ctrl_max, case_max) / 2],
            ['CASE', 'CTRL'], fontsize=20, rotation='vertical'
        )
        if i == 0:
            ax.set_ylabel('Junction Length', fontsize=20)
            ax.tick_params(labelsize=20)


import argparse
import json
from pathlib import Path
from typing import Any

import torch
from torch.utils.data import DataLoader
from transformers import AdamW, BertConfig, BertForMaskedLM

from hparams import Hyperparameter
from preprocess import Preprocessor

import torch
import torch.nn as nn
import wandb

def get_acc(scores, y):
    predictions = (scores > 0.5).long()
    num_correct += (predictions == y).sum()
    num_samples += predictions.size(0)

    acc = num_correct/num_samples
    return acc

def validate(model: Any, loader: DataLoader, device: torch.device, Sigmoid, criterion):  # type: ignore[type-arg]
    model.eval()
    total_loss = 0
    num_correct = 0
    num_samples = 0

    for batch in loader:
        input_ids, label_ids = batch
        input_ids = input_ids.to(device)
        label_ids = label_ids.to(device)

        with torch.no_grad():
            output = model(input_ids)
            prediction = output.logits[:,-1,:].squeeze()
            if len(prediction.shape) < 1: # for batch size = 1
                prediction = prediction.reshape(1)

            scores = Sigmoid(prediction)
            loss = criterion(scores.float(), label_ids.float())

            predictions = (scores > 0.5).long()
            num_correct += (predictions == label_ids).sum()
            num_samples += predictions.size(0)

        total_loss += loss.item()

    avg_loss = total_loss / len(loader)
    avg_acc  = (num_correct / num_samples).item()
    return avg_loss, avg_acc

def test(model_save_path, model, test_loader, device, Sigmoid, criterion):
    print("[Testing]")
    dic = torch.load(model_save_path)
    model.load_state_dict(dic["model_state_dict"])
    test_loss, test_acc = validate(model, test_loader, device, Sigmoid, criterion)
    test_dict = {"test_loss": test_loss, "test_acc":test_acc}
    return test_dict

def train(args: argparse.Namespace) -> None:
    wandb.login(key="")
    run = wandb.init(
        name = "[New Data] Paper Accpetance", ## Wandb creates random run names if you skip this field
        reinit = True, ### Allows reinitalizing runs when you re-run this cell
        # run_id = ### Insert specific run id here if you want to resume a previous run
        # resume = "must" ### You need this to resume previous runs, but comment out reinit = True when using this
        project = "IDL", ### Project should be created in your wandb account
        #config = configParaser ### Wandb Config for your run
        entity = "gyuseoklee"
    )

    # Hyperparameters
    hp = Hyperparameter()
    device = f"cuda:{args.gpu_num}" if torch.cuda.is_available() else "cpu"
    save_dir = Path(hp.save_dir) / args.task / hp.pretrained_model

    # Preprocessor
    config = BertConfig.from_pretrained(hp.pretrained_model)
    preprocessor = Preprocessor(hp, config=config)

    # train/dev/test dataset
    train_dataset, dev_dataset, test_dataset = preprocessor(args.task)

    train_loader = DataLoader(
        train_dataset, batch_size=hp.batch_size, shuffle=True, collate_fn=preprocessor.collate_fn_accept
    )
    dev_loader = DataLoader(
        dev_dataset, batch_size=hp.batch_size, shuffle=False, collate_fn=preprocessor.collate_fn_accept
    )
    test_loader = DataLoader(
        test_dataset, batch_size=hp.batch_size, shuffle=False, collate_fn=preprocessor.collate_fn_accept
    )

    # Model
    model = BertForMaskedLM.from_pretrained(hp.pretrained_model, config=config).to(device)
    model.cls.predictions.decoder = nn.Linear(768,1).to(device)

    # check
    # x,y = next(iter(train_loader))
    # output = model(x.to(device))
    # print("output", output.logits.shape) # 8 x 512 x 1

    # Optimizer
    no_decay = ["bias", "LayerNorm.weight"]
    optimizer_grouped_parameters = [
        {
            "params": [
                p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)
            ],
            "weight_decay": 1e-4,
        },
        {
            "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
            "weight_decay": 0,
        },
    ]
    optimizer = AdamW(optimizer_grouped_parameters, lr=hp.learning_rate, eps=1e-8)  # type: ignore[arg-type]
    criterion = nn.BCELoss().to(device)
    Sigmoid = nn.Sigmoid().to(device)

    if args.test_only:
        test_dict = test(args.model_pretrain_path, model, test_loader, device, Sigmoid, criterion)
        print(test_dict)
        wandb.log(test_dict)
        return

    min_dev_loss = float("inf")
    global_step = 0
    patience = 0
    early_stopping = False

    for epoch in range(10): #hp.max_epochs):
        print(f"[Epoch : {epoch}]")
        model.train()
        num_correct = 0
        num_samples = 0
        total_loss = 0

        print("[Training]")
        for idx, batch in enumerate(train_loader):
            input_ids, label_ids = batch
            input_ids = input_ids.to(device)
            label_ids = label_ids.to(device)

            output = model(input_ids)
            prediction = output.logits[:,-1,:].squeeze()
            if len(prediction.shape) < 1: # for batch size = 1
                prediction = prediction.reshape(1)

            scores = Sigmoid(prediction)
            loss = criterion(scores.float(), label_ids.float())
            total_loss += loss.item()

            predictions = (scores > 0.5).long()
            num_correct += (predictions == label_ids).sum()
            num_samples += predictions.size(0)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            global_step += 1

            train_step_loss = total_loss / (idx+1)
            train_step_acc = (num_correct / num_samples).item()

            # validate
            dev_loss, dev_acc = None, None
            if global_step % 1 == 0:
                #print("[Validating]")
                dev_loss, dev_acc = validate(model, dev_loader, device, Sigmoid, criterion)
                #print(f"[Epoch:{epoch}] Step {global_step} | Dev loss: {dev_loss:.6f} Dev acc: {dev_acc:.6f}")

                if dev_loss < min_dev_loss:
                    min_dev_loss = dev_loss

                    # Save model
                    model.save_pretrained(save_dir)
                    config.save_pretrained(save_dir)
                    with open(save_dir / "hp.json", "w") as f:
                        json.dump(hp.dict(), f, indent=4, ensure_ascii=False)

                    status = {"global_step": global_step, "epoch": epoch}
                    with open(save_dir / "status.json", "w") as f:
                        json.dump(status, f, indent=4, ensure_ascii=False)

                    # Notice model saved
                    model_save_path = args.model_save_path
                    torch.save({"model_state_dict" : model.state_dict()}, model_save_path)

                    print(f"Model saved at step {global_step} / epoch {epoch}")
                    patience = 0

                # else:
                #     patience += hp.validation_steps
                #     if patience > hp.patience_steps:
                #         print(f"Early stopping: No improvement in {patience} epochs")
                #         early_stopping = True
                        #break
            # result
            step_result = {"train_step_acc" : train_step_acc,
                           "train_step_loss": train_step_loss,
                           "valid_acc" : dev_acc,
                           "valid_loss": dev_loss}

            print(f"[Epoch : {epoch:04d}] global_step : {global_step}")
            print(step_result)
            wandb.log(step_result)

        # epoch_log
        epoch_result = {"train_epoch_loss": train_step_loss,
                        "train_epoch_acc": train_step_acc}
        wandb.log(epoch_result)

        # if early_stopping:
        #     break

    # Test
    test_dict = test(model_save_path, model, test_loader, device, Sigmoid, criterion)
    print(test_dict)
    wandb.log(test_dict)
    run.finish()
    return

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--task",
        type=str,
        choices=["tldr", "strength", "weakness", "accepted"],
        help="Task to train",
        default = "accepted"
    )
    parser.add_argument("--gpu_num", type = int, default = 0, help = "the number of gpu")
    parser.add_argument("--model_pretrain_path", type = str, default = "./checkpoints/accepted/accepted.pth")
    parser.add_argument("--model_save_path", type = str, default = "./checkpoints/accepted/accepted.pth")
    parser.add_argument("--test_only", type = bool, default = False, help = "Whether only test mode or not")

    args = parser.parse_args()

    print("[Starting]")
    print(args)

    train(args)

    print(args)
    print("[Ending]")


import datetime
import logging
import os
import time
import numpy as np
import pandas as pd
import sys
from monetio.models import hytraj
from utilhysplit import hcontrol
from utilhysplit.runhandler import ProcessList
from utilvolc.utiltraj  import combine_traj
from ashapp.ashtrajectory import TrajectoryAshRun

logger = logging.getLogger(__name__)

"""
BackTraj class utilizes a csv file with starting points for the back trajectories.
csv file can be generated from the VOLCAT data.
"""

#def combine_traj(fnames, csvfile=None):
#    """
#    fnames  : list of str. trajectory file names. full path.
#    csvfile : csv file output by sample_and_write which contains weighting information.
#    combined trajectories in different files into one dataframe.
#    """
#    trajlist = []
#    if csvfile:
#        weightcsv = pd.read_csv(csvfile)
#    for iii, fnn in enumerate(fnames):
#        try:
#            df1 = hytraj.open_dataset(fnn)
#        except:
#            print('Failed {}'.format(fnn))
#            continue
        # get trajectory number from the file name
#        temp = fnn.split(".")
#        trajnum = int(temp[-1])
        # add new column to dataframe with trajectory number
#        df1["traj_num"] = trajnum
#        #print('TRAJNUM', trajnum)
#        # add weight information from csvfile to the dataframe
#        if csvfile:
#            temp = weightcsv.loc[trajnum]
#            weight = temp.massI
#        else:
#            weight = 1
#        df1["weight"] = weight

#        trajlist.append(df1.copy())
    # concatenate the trajectories into one dataframe.
#    trajdf = pd.concat(trajlist)
#    return trajdf

class BackTraj(TrajectoryAshRun):
    def __init__(self, JOBID):
        super().__init__(JOBID)
        self.obsdf = pd.DataFrame()  # dataframe with obs to initiate trajectories from.

    # def __init__(self, wdir='./', controldefault='CONTROL.0'):
    #    self.wdir = wdir
    #    self.default = controldefault

    #    self.control = hcontrol.HycsControl(fname=controldefault, rtype='trajectory', working_directory=wdir)
    #    self.control.read()

    def additional_control_setup(self, control, suffix):
        control.add_sdate(self.inp["start_date"])
        alt = self.inp["top"]
        lat = self.inp["latitude"]
        lon = self.inp["longitude"]

        control.remove_locations()

        control.add_location(latlon=[lat, lon], alt=alt)

        control.rename("CONTROL.{}".format(suffix))
        control.outdir = self.inp["WORK_DIR"]
        control.outfile = self.filelocator.get_tdump_filename(suffix)
        control.outfile = "tdump.{}".format(suffix)

    def read_obsdf(self):
        dname = self.inp["WORK_DIR"]
        fname = "{}.csv".format(self.JOBID)
        obsdf = pd.read_csv(os.path.join(dname, fname), parse_dates=["time"])
        self.obsdf = obsdf

    def run_model(self):
        processhandler = ProcessList()
        processhandler.pipe_stdout()
        processhandler.pipe_stderr()
        self.read_obsdf()
        max_procs = 10

        maxtime = 5*700
        test_time=0
        tdumplist = []
        for iii, row in enumerate(self.obsdf.iterrows()):
            tdumpfile = "tdump.{}".format(iii)
            tdumplist.append(tdumpfile)
            if not os.path.isfile(tdumpfile):
                self.inp["top"] = row[1].heightI * 1000
                if self.inp["top"] == np.nan:
                   logger.warning("Height is NaN {}".format(iii))
                self.inp["latitude"] = row[1].lat
                self.inp["longitude"] = row[1].lon
                stime = row[1].time
                # import sys
                # time = datetime.datetime.strptime("%Y-%m-%d %H:%M:%S", row[1].time)
                # print(time)
                self.inp["start_date"] = stime
                self.compose_control(iii, rtype="trajectory")
                self.compose_setup(iii)
                run_suffix = str(iii)
                tdumpfile = "tdump.{}".format(iii)
                # run_suffix = self.filelocator.get_control_suffix(stage)
                cproc = [
                    os.path.join(self.inp["HYSPLIT_DIR"], "exec", "hyts_std"),
                    run_suffix,
                ]
                # logger.info('Running {} with job id {}'.format("hyts_std", cproc[1]))
                processhandler.startnew(cproc, self.inp["WORK_DIR"], descrip=run_suffix)
                # wait 5 seconds between run starts.
                time.sleep(1)
            else:
                print("tdump file already exists {}".format(tdumpfile))
            num_procs = processhandler.checkprocs()
            while num_procs >= max_procs:
                num_procs = processhandler.checkprocs()
                logger.info("in loop {}".format(num_procs))
                time.sleep(5)
                test_time += 5
                if test_time>maxtime:
                   logger.warning('Number of processes timed out {}'.format(maxtime))
                   break
        # wait for runs to finish
        done = False
        seconds_to_wait = 30
        total_time = 0
        # 60 minutes.
        max_time = 60 * 60
        while not done:
            num_procs = processhandler.checkprocs()
            if num_procs == 0:
                done = True
            time.sleep(seconds_to_wait)
            total_time += seconds_to_wait
            if total_time > max_time:
                processhandler.checkprocs()
                processhandler.killall()
                logger.warning("HYSPLIT run Timed out")
                done = True
        cname = "{}.csv".format(self.JOBID)
        outname = "TRAJ_{}.csv".format(self.JOBID)
        trajdf = combine_traj(tdumplist, csvfile = cname)
        print('WRITING csv file {}'.format(outname))
        trajdf.to_csv(outname)

    def create_plots(self, redraw=False, stage=0):
        pass

    def after_run_check(self, update=False):
        # Check for the tdump/cdump file
        self.read_obsdf()
        rval = True
        # fn = self.filelocator.get_tdump_filename(stage=0)
        #fn = "tdump.0"
        #logger.debug("Looking for tdump file " + fn)
        for iii, row in enumerate(self.obsdf.iterrows()):
            tdumpfile = self.inp["WORK_DIR"] + "tdump.{}".format(iii)
            #tdumplist.append(tdumpfile)
            if not os.path.isfile(tdumpfile):
               if update: logger.warning('file not found {}'.format(tdumpfile))
               rval = False
        if update and not rval:
            logger.error(
                "******************************************************************************"
            )
            logger.error(
                "The model has crashed. Check the HYSPLIT Message file for further information."
            )
            logger.error(
                "******************************************************************************"
            )
        return rval


import sys

in_file = ''
command = ''
source  = ''
destiny = ''

def read_input():
    global in_file, command, source, destiny

    in_file = sys.argv[1]
    command = sys.argv[2]
    source = sys.argv[3]
    destiny = sys.argv[4]

read_input()

nodes = []
routers = []
routertable = []
with open(in_file, 'r') as file:
    lines = file.readlines()
    lines = [line.strip() for line in lines]
    for line in lines:
        if line == '#ROUTER':
            break
        nodes.append(line)
    nodes.remove('#NODE')
    for line in lines:
        if line in nodes:
            continue
        if line == '#ROUTERTABLE':
            break
        routers.append(line)
    routers.remove('#NODE')
    routers.remove('#ROUTER')
    for line in lines:
        if line in nodes or line in routers:
            continue
        routertable.append(line)
    routertable.remove('#NODE')
    routertable.remove('#ROUTER')
    routertable.remove('#ROUTERTABLE')

class Node():
    arp_table = {}
    def __init__(self, node_name, mac, ip_prefix, gateway):
        self.node_name = node_name
        self.mac = mac
        self.ip_prefix = ip_prefix
        self.gateway = gateway

    def update_arp_table(self, ip, mac):
        self.arp_table.update({ip:mac})

    def print_node(self):
        print(self.node_name, self.mac, self.ip_prefix, self.gateway)

nodes = [n.split(',') for n in nodes]
nodes = [Node(n[0],n[1],n[2],n[3]) for n in nodes]
# print('Nodes')
# for n in nodes:
#     print(n.node_name)

class Router():
    arp_table = {}
    def __init__(self, router_name, num_ports, mac_iprefix):
        self.router_name = router_name
        self.num_ports = num_ports
        self.mac_iprefix = mac_iprefix

    def update_arp_table(self, ip, mac):
        self.arp_table.update({ip:mac})

    def print_router(self):
        print(self.router_name, self.num_ports, self.mac_iprefix)

routers = [r.split(',') for r in routers]
mac_ip = []
for r in routers:
    mac_ip.append(r[2:])

mac_ip = mac_ip[0]
macs = []
ips = []

for mi in mac_ip:
    if mi.__contains__(':'):
        macs.append(mi)
    else:
        ips.append(mi)

mac_ip = [(mac,ip) for mac,ip in zip(macs, ips)]

routers = [Router(r[0],r[1], mac_ip) for r in routers]
# print('Routers')
# for r in routers:
#     r.print_router()

class Routertable():
    def __init__(self, router_name, net_dest_prefix, nexthop, port):
        self.router_name = router_name
        self.net_dest_prefix = net_dest_prefix
        self.nexthop = nexthop
        self.port = port

    def print_routertable(self):
        print(self.router_name, self.net_dest_prefix, self.nexthop, self.port)

routertable = [rt.split(',') for rt in routertable]
routertable = [Routertable(rt[0],rt[1],rt[2],rt[3]) for rt in routertable]
# print('Routertable')
# for rt in routertable:
#     rt.print_routertable()

def icmp_echo_request(x, y): #origem/destino
    src_ip = ''
    dst_ip = ''
    ttl = 8

    if x.startswith('n'): # se a origem é um nodo
        for n in nodes:
            if n.node_name == x:
                src_ip = n.ip_prefix.split('/')[0]

    if y.startswith('n'): # se o destino é um nodo
        for n in nodes:
            if n.node_name == y:
                dst_ip = n.ip_prefix.split('/')[0]

    package = f'{source} ->> {destiny} : ICMP Echo Request
src={src_ip} dst={dst_ip} ttl={ttl}'
    return package

# result = icmp_echo_request(source, destiny)
# print(result)

def icmp_echo_reply(x, y):
    src_ip = ''
    dst_ip = ''
    ttl = 8

    if x.startswith('n'): # se a origem é um nodo
        for n in nodes:
            if n.node_name == x:
                src_ip = n.ip_prefix.split('/')[0]

    if y.startswith('n'): # se o destino é um nodo
        for n in nodes:
            if n.node_name == y:
                dst_ip = n.ip_prefix.split('/')[0]

    package = f'{y} --> {x} : ICMP Echo Reply
src={dst_ip} dst={src_ip} ttl={ttl}'
    return package

# arp table n1 - ip/mac
# n1 -> r1
# n1 pede o mac do r1
# atualiza tabela arp de n1 com ip e mac do r1

def arp_request(x, y):
    src_ip = ''
    dst_ip = ''

    if x.startswith('n') and y.startswith('n'):
        for n in nodes:
            if n.node_name == x:
                x = n
            if n.node_name == y:
                y = n
        src_ip = x.ip_prefix.split('/')[0]
        dst_ip = y.ip_prefix.split('/')[0]

        #primeiro caso - n1 e n2 estão na mesma rede
        # n1 envia arp request para n2

        y.update_arp_table(x.ip_prefix, x.mac) #atualiza arp table de n2

    package = f'Note over {x.node_name} : ARP Request
Who has {dst_ip}? Tell {src_ip}'
    return package

def arp_reply(x, y):
    src_ip = ''
    src_mac = ''

    if y.startswith('n'):
        for n in nodes:
            if n.node_name == y:
                src_ip = n.ip_prefix.split('/')[0]
                src_mac = n.mac

    package = f'{y} --> {x} : ARP Reply
{src_ip} is at {src_mac}'
    return package

def ping(x, y):
    result = ''
    result += arp_request(x, y) + '\n'
    result += arp_reply(x, y) + '\n'
    result += icmp_echo_request(x, y) + '\n'
    result += icmp_echo_reply(x, y) + '\n'
    return result

result = ping(source, destiny)
print(result)


from nose.tools import eq_
from mock import MagicMock

from buffpy.models.link import Link

def test_links_shares():
  '''
    Test link's shares retrieving from constructor
  '''

  mocked_api = MagicMock()
  mocked_api.get.return_value = {'shares': 123}

  link = Link(api=mocked_api, url='www.google.com')

  eq_(link, {'shares': 123, 'url': 'www.google.com', 'api': mocked_api})
  mocked_api.get.assert_called_once_with(url='links/shares.json?url=www.google.com')

def test_links_get_shares():
  '''
    Test link's shares retrieving method
  '''

  mocked_api = MagicMock()
  mocked_api.get.return_value = {'shares': 123}

  link = Link(api=mocked_api, url='www.google.com')

  eq_(link, {'shares': 123, 'url': 'www.google.com', 'api': mocked_api})
  eq_(link.get_shares(), 123)
  mocked_api.get.assert_any_call(url='links/shares.json?url=www.google.com')
  eq_(mocked_api.get.call_count, 2)


from layouts.layout_manager import LayoutScope, adapt_and_register
dqmitems={}

def GEMLayout(i, p, *rows): i["GEM/Layouts/" + p] = rows
#def GEMLayout(i, p, *rows): return 1 # this line and the following one line is for printing out indices

nIdx = 0

GEMLayout(dqmitems, "%02i Summary"%nIdx,
    [{"path" : "GEM/EventInfo/reportSummaryMap",
    "description": 'For more information (... under construction)'}])
nIdx += 1

GEMLayout(dqmitems, "%02i AMC status"%nIdx,
    [{"path" : "GEM/StatusDigi/amc_statusflag",
    "description": 'For more information (... under construction)'}])
nIdx += 1

# StatusDigi
GEMLayout(dqmitems, "%02i GEB input status"%nIdx,
    [
      {"path": "GEM/StatusDigi/geb_input_status_st_m1_la_1", "description": "GEB input status in GE-1/1"},
      {"path": "GEM/StatusDigi/geb_input_status_st_m1_la_2", "description": "GEB input status in GE-1/2"},
    ],
    [
      {"path": "GEM/StatusDigi/geb_input_status_st_p1_la_1", "description": "GEB input status in GE+1/1"},
      {"path": "GEM/StatusDigi/geb_input_status_st_p1_la_2", "description": "GEB input status in GE+1/2"},
    ])
nIdx += 1

GeminisId = [ i + 1 for i in range(36) ]
GeminisIdWithTitle = [ {"chid": gid, "title": "GEMINI%02i"%gid} for gid in GeminisId ]
listLayers = ["p1_1", "p1_2", "m1_1", "m1_2"]
listLayersWithTitle = [ [ s, "GE%s%s%s"%("+" if s[ 0 ] == "p" else "-", s[ 1 ], s[ 3 ]) ] for s in listLayers ]
bIsLayerWise = True
bIsGlobalPos = True

strTitleFmt = "%(idx)02i %(title)s_%(layer)s"

listGEMChambers = []

if bIsLayerWise:
  for layer in listLayersWithTitle:
    for gemini in GeminisIdWithTitle:
      listGEMChambers.append([gemini, layer])
else:
  for gemini in GeminisIdWithTitle:
    for layer in listLayersWithTitle:
      listGEMChambers.append([gemini, layer])

if bIsGlobalPos:
  for layer in listLayersWithTitle:
    GEMLayout(dqmitems, "%02i Global position %s"%(nIdx, layer[ 1 ]),
        [{"path": "GEM/recHit/recHit_globalPos_Gemini_GE" + layer[ 0 ],
          "description": "Global position"}])
    nIdx += 1

for itCh in listGEMChambers:
  gemini = itCh[ 0 ]
  layer  = itCh[ 1 ]

  strID = "Gemini_%i_GE%s"%(gemini[ "chid" ], layer[ 0 ])

  listU1 = ["GEM/StatusDigi/vfatStatus_QualityFlag_" + strID, "VFAT quality"]
  listU2 = ["GEM/StatusDigi/vfatStatus_BC_"          + strID, "Bunch crossing"]
  listU3 = ["GEM/StatusDigi/vfatStatus_EC_"          + strID, "Event counter"]
  listL1 = ["GEM/digi/Digi_Strips_"                  + strID, "Number of Digi Strips"]
  listL2 = ["GEM/recHit/VFAT_vs_ClusterSize_"        + strID, "VFAT vs ClusterSize"]
  #strPathRHHitX     = "GEM/recHit/recHit_x_" + strID

  gemini[ "idx" ] = nIdx
  gemini[ "layer" ] = layer[ 1 ]
  strTitle = strTitleFmt%gemini

  GEMLayout(dqmitems, strTitle,
    [
      {'path': listU1[ 0 ], 'description': listU1[ 1 ]},
      {'path': listU2[ 0 ], 'description': listU2[ 1 ]},
      {'path': listU3[ 0 ], 'description': listU3[ 1 ]},
    ],
    [
      {'path': listL1[ 0 ], 'description': listL1[ 1 ]},
      {'path': listL2[ 0 ], 'description': listL2[ 1 ]},
    ]
  )

  nIdx += 1

adapt_and_register(dqmitems, scope=LayoutScope.ONLINE)


import pathlib
from os.path import join
import itertools
from typing import Any

import matplotlib.pyplot as plt
from pytorch_lightning.utilities.types import STEP_OUTPUT
import pytorch_lightning as pl
import torch.nn as nn

from .observable_layers import ObservableLayersChans
from general.utils import max_min_norm, save_json

class PlotWeightsBiSE(ObservableLayersChans):

    def __init__(self, *args, freq: int = 100, **kwargs):
        super().__init__(*args, freq=freq, **kwargs)
        self.last_weights = []

    def on_train_batch_end_layers_chans(
        self,
        trainer: 'pl.Trainer',
        pl_module: 'pl.LightningModule',
        outputs: "STEP_OUTPUT",
        batch: "Any",
        batch_idx: int,
        dataloader_idx: int,
        layer: "nn.Module",
        layer_idx: int,
        chan_input: int,
        chan_output: int,
    ):
        # if isinstance(layer, (BiSE, COBiSE, BiSEC, COBiSEC)):
        #     trainer.logger.experiment.add_figure(f"weights/Normalized_{layer_idx}", self.get_figure_normalized_weights(
        #         layer._normalized_weight, layer.bias, layer.activation_P), trainer.global_step)
        # trainer.logger.experiment.add_figure(f"weights/Raw_{layer_idx}", self.get_figure_raw_weights(layer.weight), trainer.global_step)

        weights = layer.weights[chan_output, chan_input]
        weights_norm = layer._normalized_weight[chan_output, chan_input]
        trainer.logger.experiment.add_figure(
            f"weights_normalized/layer_{layer_idx}_chin_{chan_input}_chout_{chan_output}",
            self.get_figure_normalized_weights(weights_norm, layer.bias_bise[chan_output, chan_input], layer.activation_P_bise[chan_output, chan_input]),
            trainer.global_step
        )
        trainer.logger.experiment.add_figure(
            f"weights_raw/layer_{layer_idx}_chin_{chan_input}_chout_{chan_output}",
            self.get_figure_raw_weights(weights),
            trainer.global_step
        )

    def on_train_end(self, trainer: 'pl.Trainer', pl_module: 'pl.LightningModule') -> None:
        for layer_idx, layer in enumerate(pl_module.model.layers):
            to_add = {"weights": layer.weights, "bias_bise": layer.bias_bise, "activation_P_bise": layer.activation_P_bise}
            to_add["normalized_weights"] = layer._normalized_weight
            self.last_weights.append(to_add)

    def save(self, save_path: str):
        final_dir = join(save_path, self.__class__.__name__)
        pathlib.Path(final_dir).mkdir(exist_ok=True, parents=True)
        for layer_idx, layer_dict in enumerate(self.last_weights):
            for key, weight in layer_dict.items():
                for chan_output in range(weight.shape[0]):
                    for chan_input in range(weight.shape[1]):
                        if key == "normalized_weights":
                            fig = self.get_figure_normalized_weights(weight[chan_output, chan_input],
                            bias=layer_dict['bias_bise'][chan_output, chan_input], activation_P=layer_dict['activation_P_bise'][chan_output, chan_input])
                        elif key == "weights":
                            fig = self.get_figure_raw_weights(weight[chan_output, chan_input])
                        fig.savefig(join(final_dir, f"{key}_layer_{layer_idx}_chin_{chan_input}_chout_{chan_output}.png"))

        return self.last_weights

    @staticmethod
    def get_figure_normalized_weights(weights, bias, activation_P):
        weights = weights.cpu().detach()
        figure = plt.figure(figsize=(8, 8))
        plt.title(f"bias={bias.item()}  act_P={activation_P.item()}")
        plt.imshow(weights, interpolation='nearest', cmap=plt.cm.gray)
        plt.colorbar()
        plt.clim(0, 1)

        # Use white text if squares are dark; otherwise black.

        for i, j in itertools.product(range(weights.shape[0]), range(weights.shape[1])):
            color = "white" if weights[i, j] < .5 else "black"
            plt.text(j, i, round(weights[i, j].item(), 2), horizontalalignment="center", color=color)

        plt.tight_layout()
        return figure

    @staticmethod
    def get_figure_raw_weights(weights):
        weights = weights.cpu().detach()
        weights_normed = max_min_norm(weights)
        figure = plt.figure(figsize=(8, 8))
        plt.imshow(weights_normed, interpolation='nearest', cmap=plt.cm.gray)
        plt.colorbar()
        # plt.clim(0, 1)

        # Use white text if squares are dark; otherwise black.
        threshold = weights_normed.max() / 2.

        for i, j in itertools.product(range(weights.shape[0]), range(weights.shape[1])):
            color = "white" if weights_normed[i, j] < threshold else "black"
            plt.text(j, i, round(weights[i, j].item(), 2), horizontalalignment="center", color=color)

        plt.tight_layout()
        return figure

class PlotParametersBiSE(ObservableLayersChans):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.last_params = {}

    def on_train_batch_end_layers_chans(
        self,
        trainer: 'pl.Trainer',
        pl_module: 'pl.LightningModule',
        outputs: "STEP_OUTPUT",
        batch: "Any",
        batch_idx: int,
        dataloader_idx: int,
        layer: "nn.Module",
        layer_idx: int,
        chan_input: int,
        chan_output: int,
    ):
        metrics = {}
        last_params = {}

        metrics[f'params/weight_P/layer_{layer_idx}_chin_{chan_input}_chout_{chan_output}'] = layer.weight_P_bise[chan_output, chan_input]
        metrics[f'params/activation_P/layer_{layer_idx}_chin_{chan_input}_chout_{chan_output}'] = layer.activation_P_bise[chan_output, chan_input]
        metrics[f'params/bias_bise/layer_{layer_idx}_chin_{chan_input}_chout_{chan_output}'] = layer.bias_bise[chan_output, chan_input]

        trainer.logger.log_metrics(metrics, trainer.global_step)
        self.last_params[layer_idx] = last_params

        trainer.logger.experiment.add_scalars(
            f"comparative/weight_P/layer_{layer_idx}_chout_{chan_output}",
            {f"chin_{chan_input}": layer.weight_P_bise[chan_output, chan_input]},
            trainer.global_step
        )
        trainer.logger.experiment.add_scalars(
            f"comparative/activation_P/layer_{layer_idx}_chout_{chan_output}",
            {f"chin_{chan_input}": layer.activation_P_bise[chan_output, chan_input]},
            trainer.global_step
        )
        trainer.logger.experiment.add_scalars(
            f"comparative/bias_bise/layer_{layer_idx}_chout_{chan_output}",
            {f"chin_{chan_input}": layer.bias_bise[chan_output, chan_input]},
            trainer.global_step
        )

    def save(self, save_path: str):
        final_dir = join(save_path, self.__class__.__name__)
        pathlib.Path(final_dir).mkdir(exist_ok=True, parents=True)
        save_json({k1: {k2: str(v2) for k2, v2 in v1.items()} for k1, v1 in self.last_params.items()}, join(final_dir, "parameters.json"))
        return self.last_params


from ezprint import p
import random
import time
import os

def win(isDrow = False, whowin = 'Player 1'):
	cls()
	p('Rock')
	time.sleep(0.5)
	p('Scissors')
	time.sleep(0.5)
	p('Paper')
	time.sleep(0.5)
	p('1')
	time.sleep(0.5)
	p('2')
	time.sleep(0.5)
	p('3')
	time.sleep(0.5)
	if isDrow:
		print('Drow')
	else:
		print(whowin  + ' win!')

def choose(inputtext = '>>>'):
	p('1) Stone')
	p('2) Scissors')
	p('3) Paper')

	c = input(inputtext)

	try:
		c = int(c)
	except:
		p('Incorrect choose')
		return choose(inputtext = inputtext)

	if c < 1 or c > 3:
		p('Incorrect choose')
		return choose(inputtext = inputtext)

	return c

def cls():
	os.system('cls')

def game():
	while True:
		cls()
		p('===GAME FOR WINDOWS=====')
		p('==Rock/Scissors/Paper===')
		p('1-START GAME VS BOT')
		p('2-START GAME TWO PLAYERS')
		p('3-EXIT')
		p('========================')
		v = input('>>>')
		try:
			v = int(v)
		except:
			p('Incorrect choose')
			return

		if int(v) < 1 or int(v) > 3:
			p('Incorrect choose')
			return

		v = str(v)

		if v == '1':
			app = random.randint(1,3)
			if app == 1:
				app = 'Stone'
			elif app == 2:
				app ='Scissors'
			else:
				app = 'Paper'

			player = choose()

			if player == 1:
				p('Your choose: Stone')
				p('Bot choose: ' + str(app))
				if app == 'Stone':
					# p('Drow')
					win(isDrow = True)
				elif app == 'Scissors':
					win(whowin = 'Player')
				else:
					win(whowin = 'Bot')
			elif player == 2:
				p('Your choose: Scissors')
				p('Bot choose: ' + str(app))
				if app == 'Stone':
					win(whowin = 'Bot')
				elif app == 'Scissors':
					win(isDrow = True)
				else:
					win(whowin = 'Player')
			elif player == 3:
				p('Your choose: Paper')
				p('Bot choose: ' + str(app))
				if app == 'Stone':
					win(whowin = 'Player')
				elif app == 'Scissors':
					win(whowin = 'Bot')
				else:
					win(isDrow = True)
			input('Continue - click to ENTER ')

		if v == '2':
			cls()
			player1 = choose(inputtext = 'Player#1>>>')

			if player1 == 1:
				player1 = 'Stone'
			elif player1 == 2:
				player1 ='Scissors'
			else:
				player1 = 'Paper'

			cls()
			player2 = choose(inputtext = 'Player#2>>>')

			if player2 == 1:
				p('Player 1 choose: ' + str(player1))
				p('Player 2 choose: Stone')
				if player1 == 'Stone':
					win(isDrow = True)
				elif player1 == 'Scissors':
					win(whowin = 'Player 2')
				else:
					win(whowin = 'Player 1')
			elif player2 == 2:
				p('Player 1 choose: ' + str(player1))
				p('Player 2 choose: Scissors')
				if player1 == 'Stone':
					win(whowin = 'Player 1')
				elif player1 == 'Scissors':
					win(isDrow = True)
				else:
					win(whowin = 'Player 2')
			elif player2 == 3:
				p('Player 1 choose: ' + str(player1))
				p('Player 2 choose: Paper')
				if player1 == 'Stone':
					win(whowin = 'Player 2')
				elif player1 == 'Scissors':
					win(whowin = 'Player 1')
				else:
					win(isDrow = True)
			input('Continue - click to ENTER ')

		if v == '3':
			exit()

if __name__ == '__main__':
	cls()
	game()


import os
import subprocess
import unittest

import ament_index_python

from diagnostic_msgs.msg import DiagnosticArray

import rclpy

TIMEOUT_MAX_S = 5.

class TestNTPMonitor(unittest.TestCase):

    def __init__(self, methodName: str = 'runTest') -> None:
        super().__init__(methodName)
        rclpy.init()
        self.n_msgs_received = 0

    def setUp(self):
        self.n_msgs_received = 0
        n = self._count_msgs(TIMEOUT_MAX_S)
        self.assertEqual(n, 0)
        self.subprocess = subprocess.Popen(
            [
                os.path.join(
                    ament_index_python.get_package_prefix(
                        'diagnostic_common_diagnostics'
                    ),
                    'lib',
                    'diagnostic_common_diagnostics',
                    'ntp_monitor.py'
                )
            ]
        )

    def tearDown(self):
        self.subprocess.kill()

    def _diagnostics_callback(self, msg):
        search_strings = [
            'NTP offset from',
            'NTP self-offset for'
        ]
        for search_string in search_strings:
            if search_string not in ''.join([
                s.name for s in msg.status
            ]):
                return
        self.n_msgs_received += 1

    def _count_msgs(self, timeout_s):
        self.n_msgs_received = 0
        node = rclpy.create_node('test_ntp_monitor')
        node.create_subscription(
            DiagnosticArray,
            'diagnostics',
            self._diagnostics_callback,
            1
        )
        TIME_D_S = .05
        waited_s = 0.
        while waited_s < timeout_s and self.n_msgs_received == 0:
            rclpy.spin_once(node, timeout_sec=TIME_D_S)
            waited_s += TIME_D_S
        node.destroy_node()
        return self.n_msgs_received

    def test_publishing(self):
        self.assertEqual(
            self.subprocess.poll(),
            None,
            'NTP monitor subprocess died'
        )

        n = self._count_msgs(TIMEOUT_MAX_S)

        self.assertGreater(
            n,
            0,
            f'No messages received within {TIMEOUT_MAX_S}s'
        )

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


import numpy as np, pandas as pd

#from trainscript.ModelBuilding import *
from data_processing import *
from trainscript import *

#from DataPreprocess import *
path = 'bank-additional/bank-additional-full.csv'

#d = DataPreprocess()
    #d = DataPreprocess();
df1 = load_data(path)
df_converted = convertInputFeatures(df1.copy())
df = convertOutputFeatures(df_converted)
df = convertDummies(df)
df = scaleEconomicFeatures(df)

X, y = split_data(df)

X_train, X_test, y_train, y_test = tt_split(X, y)

X_train_res, y_train_res = oversample(X_train, y_train)

#scores = run_models_with_kfold(X_train_res, y_train_res)

scores = run_models(X_train_res, X_test, y_train_res, y_test)
# print scores
label, proba = run_rfc(X_train_res, X_test, y_train_res, y_test)

df_converted['duration'] = df1['duration']
#proba = run_rfc2(X_train_res, X_test, y_train_res, y_test)
dump_df = df_converted.loc[X_test.index].copy()
dump_df['y_pred'] = label
dump_df['y_proba'] = [n for m,n in proba]
#dump_df['duration'] = df1['duration']
dump_df.to_csv('result_new.csv')


import heapq
def solution(s):
    queue = string_to_queue(s)
    answer = list()
    compare_set = set()
    while queue:
        _, value = heapq.heappop(queue)
        element = (value - compare_set).pop()
        answer.append(element)
        compare_set = value
    return answer

def string_to_queue(s):
    s = s[1:-1]
    result = list()
    temp = set()
    integer = ""
    for c in s:
        if c == "{":
            temp = set()
        elif c == "}":
            temp.add(int(integer))
            heapq.heappush(result, (len(temp), temp))
            integer = ""
        elif c == ",":
            if integer:
                temp.add(int(integer))
                integer = ""
        else:
            integer += c
    return result


from django.shortcuts import redirect, render
from .models import Book

# Create your views here.

def home(request):
    return render(request, 'books/home.html')

def book_list(request):
    books = Book.objects.all()
    return render(request, 'books/book_list.html', {'books': books})

def add_book(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        author = request.POST.get('author')
        genre = request.POST.get('genre')
        Book.objects.create(title=title, author=author, genre=genre)
    books = Book.objects.all()
    return render(request, 'books/add_book.html', {'books': books})

def delete_book(request, book_id):
    try:
        book = Book.objects.get(pk=book_id)
        book.delete()
        return redirect('book-list')
    except Book.DoesNotExist:
        return redirect('book-list')


import logging
from pathlib import Path
from typing import Union

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

from backend.api.routes import base_router
from backend.settings import APP_ENV

def serve_next_app(app: FastAPI, build_dir: Union[Path, str]) -> FastAPI:
    """Serves a Next application in the root directory `/`

    Args:
        app: FastAPI application instance
        build_dir: Next build directory (generated by `yarn build` or
            `npm run build`)

    Returns:
        FastAPI: instance with the react application added
    """
    if isinstance(build_dir, str):
        build_dir = Path(build_dir)

    app.mount(
        "/_next/",
        StaticFiles(directory=build_dir / "_next"),
        name="Next static files",
    )
    templates = Jinja2Templates(directory=build_dir.as_posix())

    @app.get("/favicon.ico")
    def favicon():
        return FileResponse(build_dir / "favicon.ico")

    @app.get("/{full_path:path}")
    def serve_app(request: Request, full_path: str):
        """Serve the react app
        `full_path` variable is necessary to serve each possible endpoint with
        `index.html` file in order to be compatible with `react-router-dom
        """

        def response(path: str):
            return templates.TemplateResponse(path, {"request": request})

        if full_path == "":
            return response("index.html")
        if full_path.startswith("song/"):
            return response("song/[songId].html")
        return response("404.html")

    return app

class HealthCheckFilter(logging.Filter):  # pragma: no cover
    def filter(self, record: logging.LogRecord) -> bool:
        return record.getMessage().find("/api/healthcheck") == -1

def create_api():
    api = FastAPI(
        title="Cognitive API",
        description="
Back-end support for Cognitive application
",
    )

    api.include_router(base_router.router)
    return api

def create_app() -> FastAPI:
    app = FastAPI()
    app.mount("/api", create_api())

    if APP_ENV == "dev":
        app.add_middleware(
            CORSMiddleware,
            allow_origins=["http://localhost:3000"],
            allow_credentials=True,
            allow_methods=["*"],
            allow_headers=["*"],
        )
    if APP_ENV == "prod":  # pragma: no cover
        logging.getLogger("uvicorn.access").addFilter(HealthCheckFilter())

        app = serve_next_app(
            app, Path(__file__).parent.parent / "frontend" / "out"
        )

    return app

app = create_app()

if __name__ == "__main__":
    assert APP_ENV == "dev"

    import os
    from pathlib import Path

    os.chdir(Path(__file__).parent)
    os.system("poetry run uvicorn main:app --reload")


#!/usr/bin/python3
'''
Python script to export data in the CSV format.
'''
if __name__ == "__main__":

    import csv
    import requests
    from sys import argv

    urlUser = 'https://jsonplaceholder.typicode.com/users/' + argv[1]
    response_name = requests.get(urlUser)
    name = response_name.json()['username']

    urlTodo = urlUser + '/todos'
    response_todos = requests.get(urlTodo)
    task = response_todos.json()
    total_task = len(task)

    with open('{}.csv'.format(argv[1]), 'w', encoding='UTF8') as f:
        writer = csv.writer(f, quoting=csv.QUOTE_ALL)
        for element in task:
            writer.writerow([argv[1], name, element['completed'],
                            element['title']])


from random import randint
import importlib
import pkgutil

def play_operation():
     discovered_plugins = [ e.load()
     for e in importlib.metadata.entry_points()[
           "core_software.operations"
       ]]

     # Select

     trigger = randint(0 , len(discovered_plugins)-1)

     operation = discovered_plugins[trigger]()
     print( f"The patient needs surgery on their {operation.name}")
     # select number
     num = int(input("Type in a number between 1-10:"))
     print("Your number is :", num)

     operation.remove_item(num)


import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

    # Take each frame
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_red = np.array([30,150,50])
    upper_red = np.array([255,255,180])

    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(frame,frame, mask= mask)

    laplacian = cv2.Laplacian(frame,cv2.CV_64F)
    sobelx = cv2.Sobel(frame,cv2.CV_64F,1,0,ksize=5)
    sobely = cv2.Sobel(frame,cv2.CV_64F,0,1,ksize=5)

    cv2.imshow('Original',frame)
    cv2.imshow('Mask',mask)
    cv2.imshow('laplacian',laplacian)
    cv2.imshow('sobelx',sobelx)
    cv2.imshow('sobely',sobely)

    if cv2.waitKey(5) & 0xFF == ord('q'):

        break
cv2.imwrite('soblex.png', sobelx)
cv2.imwrite('laplacian.png' , laplacian)
cv2.imwrite('sobley.png', sobely)
cv2.imwrite('frame.png', frame)

cv2.destroyAllWindows()
cap.release()


import numpy as np
import santas_path
import random
from datetime import datetime

def create_particle(particle_size, x_min=0, x_max=4):
    """
    Create a single particle
    :param particle_size:
    :param x_min:
    :param x_max:
    :return:
    """
    particle = x_min + (x_max - x_min) * np.random.uniform(low=0, high=1, size=particle_size)
    return particle

def create_particle_velocities(particle_size, v_min=-4, v_max=4):
    """
    Create the velocities associated with a particle
    :param particle_size:
    :param v_min:
    :param v_max:
    :return:
    """
    velocities = v_min + (v_max - v_min) * np.random.uniform(low=0, high=1, size=particle_size)
    return velocities

def create_pop_particles(pop_size, particle_length):
    """
    Create a population of particles
    :param pop_size:
    :param particle_length:
    :return:
    """
    pop = [create_particle(particle_length) for i in range(0, pop_size)]
    return np.array(pop)

def create_pop_velocities(pop_size, particle_length):
    """
    Create the velocities associated with the particles of a population
    :param pop_size:
    :param particle_length:
    :return:
    """
    pop_vel = [create_particle_velocities(particle_length) for i in range(0, pop_size)]
    return np.array(pop_vel)

def generate_santas_path_from_particles_pop(population):
    """
    Given a population of particles, create plausible paths using the SVP rule
    :param population:
    :return:
    """
    path_populations = np.argsort(population)
    return path_populations

def generate_santas_path_from_particles_pop_complete_dataset(population):
    """

    :param population:
    :return:
    """
    return generate_santas_path_from_particles_pop(population) + 1

def evaluate_paths(paths, cities, not_primes_bool):
    """
    Compute the euclidean distance with penalties value associated with each given path
    :param paths:
    :param cities:
    :param not_primes_bool:
    :return:
    """
    distances = []
    for path in paths:
        distances.append(santas_path.edp(path, cities, not_primes_bool))
    return np.array(distances)

def swap_mutation(pop):
    """
    Exchange the position of two elements
    :param pop:
    :return:
    """
    pos_to_swap = np.random.choice(range(0, len(pop)), size=2, replace=False)
    pop[pos_to_swap[0]], pop[pos_to_swap[1]] = pop[pos_to_swap[1]], pop[pos_to_swap[0]]
    return pop

def mutate_pop_elements(pop, ro):
    """
    Apply swap mutation to 10% of population indivuals
    :param pop:
    :param ro:
    :return:
    """
    elements_to_mutate = np.random.choice(range(0, ro), size=round(ro / 10), replace=False)
    for el in elements_to_mutate:
        pop[el] = swap_mutation(pop[el])

def particle_swarm_optimization(cities,
                                ro=30,
                                max_number_of_iterations=1000,
                                decrement_factor=0.975,
                                c1=2,  # cognitive param
                                c2=2,  # social param
                                inertia_weight=0.99,
                                wait_interval=100,
                                hybrid_evolutionary_approach=False):
    # For reproducibility
    random.seed(10)
    np.random.seed(10)

    not_primes_bool = generate_not_primes_numbers_array(cities)
    # iteration counter
    t = 0
    # Initialization
    path_length = len(cities) - 1 #Exclude 0 because it's a special node
    pop = create_pop_particles(pop_size=ro, particle_length=path_length)
    best_pop = pop
    vel = create_pop_velocities(pop_size=ro, particle_length=path_length)
    paths = generate_santas_path_from_particles_pop(pop)
    distances = evaluate_paths(paths, cities, not_primes_bool)
    best_distances = distances
    # Initialize support variables where best results will be stored
    best_iteration = t
    best_particle_index = np.argsort(distances)[0]
    best_particle = pop[best_particle_index]
    best_path = paths[best_particle_index]
    global_min_distance = distances[best_particle_index]
    r1 = np.random.uniform(low=0, high=1)
    r2 = np.random.uniform(low=0, high=1)

    results_for_each_iteration = [[t, global_min_distance]]
    for t in range(1, max_number_of_iterations + 1):
        if t % 100 == 0:
            print("Iteration: {}, minimum distance so far: {}".format(t, global_min_distance))
        # Update velocity and pop
        vel = vel + c1 * r1 * (best_pop - pop)
        vel = vel + c2 * r2 * (best_particle - pop)
        pop = pop + vel
        #Mutate 10% of pop elements
        mutate_pop_elements(pop, ro)
        # Compute new path and the related distances
        paths = generate_santas_path_from_particles_pop_complete_dataset(pop)
        distances = evaluate_paths(paths, cities, not_primes_bool)
        # Find which paths lead to an improved distance and update the corresponding values
        distance_comparison = distances < best_distances
        best_distances[distance_comparison] = distances[distance_comparison]
        best_pop[distance_comparison] = pop[distance_comparison]
        # Update global best results if necessary
        current_iteration_best_index = np.argsort(distances)[0]
        current_min_distance = distances[current_iteration_best_index]
        if current_min_distance < global_min_distance:
            best_iteration = t
            best_particle = pop[current_iteration_best_index]
            global_min_distance = current_min_distance
            best_path = paths[current_iteration_best_index]
        results_for_each_iteration.append([t, global_min_distance])

        #Interrupt the algorithm if the solution is no longer improving
        if t > best_iteration + wait_interval:
            break

        # Hybrid approach: update velocities and pop params of the worst elements
        if hybrid_evolutionary_approach:
            particle_index_ordered_by_distance = np.argsort(distances)
            best_particles_index = particle_index_ordered_by_distance[0:int(ro / 2)]
            worst_particles_index = particle_index_ordered_by_distance[int(ro / 2):]
            pop[worst_particles_index] = pop[best_particles_index]
            vel[worst_particles_index] = vel[best_particles_index]

        # Update inertia and velocity
        inertia_weight = inertia_weight * decrement_factor
        if inertia_weight < 0.4:
            inertia_weight = 0.4
        vel = inertia_weight * vel

    return results_for_each_iteration, best_path

def compute_final_paths(paths, clusters_els):
    """
    Compute the complete paths taking into consideration the cluster orders and how the dataset was clusterized
    :param paths: array containing the chosen cluster combinations
    :param clusters_els: array containing the cluster related to each dataset point
    :return:
    """
    end_paths = []
    clusters_els = np.array(clusters_els)
    for path in paths:
        current_path = []
        path = np.array(path)
        for cluster_path in clusters_els[path]:
            current_path = np.concatenate((current_path, cluster_path))
        current_path = current_path.astype(int)
        end_paths.append(current_path)
    return end_paths

def evaluate_paths_cluster(paths, cities):
    """
    Compute the euclidean distance for each path
    :param paths:
    :param cities:
    :return:
    """
    distances = []
    for path in paths:
        distances.append(santas_path.total_length_straight(path, cities))
    return np.array(distances)

def generate_santas_path_from_particles_cluster(population, cluster_elements):
    """
    Transform the given particles in feasible paths using the SVP rule
    :param population:
    :param cluster_elements:
    :return:
    """
    path_populations = []
    for particle in population:
        path = np.sort(cluster_elements)[np.argsort(particle)]
        # This city is the North Pole, starting and ending point of each path: it should not be considered
        path = np.delete(path, np.where(path == 0))
        path_populations.append(path)
    return path_populations

def initialize_clusters(cluster_dict, ro, cluster_indexes, cities):
    """
    Initialize the fundamental elements needed by PSO algorithm
    :param cluster_dict:
    :param ro:
    :param cluster_indexes:
    :param cities:
    :return:
    """
    clusters_pop = []
    clusters_vel = []
    clusters_best_path = []
    clusters_best_particle = []
    clusters_distances = []
    for c in cluster_dict.items():
        pop = create_pop_particles(pop_size=ro, particle_length=c[1])
        clusters_pop.append(pop)
        vel = create_pop_velocities(pop_size=ro, particle_length=c[1])
        clusters_vel.append(vel)
        paths = generate_santas_path_from_particles_cluster(pop, np.where(cluster_indexes == c[0])[0])
        distances = evaluate_paths_cluster(paths, cities)
        clusters_distances.append(distances)
        best_path_index = np.argsort(distances)[0]
        best_path = paths[best_path_index]
        clusters_best_path.append(best_path)

        clusters_best_particle.append(pop[best_path_index])
    return clusters_pop, clusters_vel, clusters_best_path, clusters_best_particle, clusters_distances

def initialize_overall_params(cities, clusters_best_path, clusters_pop, not_primes_bool, ro, number_of_clusters):
    """
    Initialize parameters needed by PSO for findingn the best order for assembling clusters
    :param cities:
    :param clusters_best_path:
    :param clusters_pop:
    :param not_primes_bool:
    :param ro:
    :return:
    """

    overall_pop = create_pop_particles(pop_size=ro, particle_length=number_of_clusters)
    overall_vel = create_pop_velocities(pop_size=ro, particle_length=number_of_clusters)
    overall_paths = generate_santas_path_from_particles_pop(overall_pop)
    final_complete_paths = compute_final_paths(overall_paths, clusters_best_path)
    final_complete_distances = evaluate_paths(paths=final_complete_paths, cities=cities,
                                              not_primes_bool=not_primes_bool)
    return final_complete_distances, final_complete_paths, overall_pop, overall_vel

def generate_cluster_dict(clusterized_cities):
    """
    Generate a dictionary where its keys are the clusters ID and its values are the number of its elements
    :param clusterized_cities:
    :return:
    """
    cluster_dict = {}
    for cluster_index in set(clusterized_cities):
        cluster_dict[cluster_index] = np.where(clusterized_cities == cluster_index)[0].shape[0]
    return cluster_dict

def generate_not_primes_numbers_array(cities):
    """
    Find which are the cities whose ID is a prime number
    :param cities:
    :return:
    """
    np_not_prime = np.vectorize(santas_path.not_prime)
    nums = np.arange(0, len(cities))
    not_primes_bool = np_not_prime(nums)
    return not_primes_bool

def cluster_particle_swarm_optimization(
        cities,
        clusterized_cities,
        ro=30,
        max_number_of_iterations=800,
        number_of_iterations_clusters=800,
        hybrid_evolutionary_approach=False,
        decrement_factor=0.975,
        c1=2,  # cognitive param
        c2=2,  # social param
        inertia_weight=0.99,
        wait_interval=100):
    # For reproducibility
    random.seed(10)
    np.random.seed(10)
    #Add list of prime numbers
    not_primes_bool = generate_not_primes_numbers_array(cities)
    t = 0

    original_inertia_weight = inertia_weight
    # Create elements necessary for performing PSO on each cluster
    cluster_dict = generate_cluster_dict(clusterized_cities)
    clusters_pop, clusters_vel, clusters_best_path, clusters_best_particle, clusters_distances = initialize_clusters(
        cluster_dict, ro, clusterized_cities, cities)
    clusters_best_pop = clusters_pop
    r1 = np.random.uniform(low=0, high=1)
    r2 = np.random.uniform(low=0, high=1)
    number_of_clusters = len(clusters_pop)

    for cluster in range(number_of_clusters):
        if cluster % 100 == 0:
            print("Start working on cluster {}".format(cluster))
        best_overall_iteration = 0
        inertia_weight = original_inertia_weight
        # Get initial values for cluster element
        current_pop = clusters_pop[cluster]
        current_best_pop = clusters_best_pop[cluster]
        current_vel = clusters_vel[cluster]
        current_dist = clusters_distances[cluster]
        current_best_particle = clusters_best_particle[cluster]
        current_best_path = clusters_best_path[cluster]
        for i in range(1, number_of_iterations_clusters + 1):

            # Perform mutation
            mutate_pop_elements(current_pop, ro)
            # Update velocity and pop
            current_vel = current_vel + c1 * r1 * (current_best_pop - current_pop)
            current_vel = current_vel + c2 * r2 * (current_best_particle - current_pop)
            current_pop = current_pop + current_vel
            # Transform the particle in feasible paths
            current_paths = generate_santas_path_from_particles_cluster(current_pop,
                                                                        np.where(clusterized_cities == cluster)[0])
            distances = evaluate_paths_cluster(current_paths, cities)

            # Update best particle if needed
            current_best_dist = np.sort(current_dist)[0]
            distance_comparison = distances < current_dist
            current_dist[distance_comparison] = distances[distance_comparison]
            current_best_pop[distance_comparison] = current_pop[distance_comparison]
            local_best_particle_distance = np.sort(distances)[0]
            if local_best_particle_distance < current_best_dist:
                best_particle_index = np.argsort(current_dist)[0]
                current_best_particle = current_pop[best_particle_index]
                current_best_path = current_paths[best_particle_index]
                best_overall_iteration = i

            # Stop doing PSO on the given cluster if the solution is no longer improving
            if i > best_overall_iteration + wait_interval:
                break

            if hybrid_evolutionary_approach:
                best_elements_indexes = np.argsort(distances)[0:int(ro / 2)]
                worst_elements_indexes = np.argsort(distances)[int(ro / 2):]
                current_pop[worst_elements_indexes] = current_pop[best_elements_indexes]
                current_vel[worst_elements_indexes] = current_vel[worst_elements_indexes]
            inertia_weight = inertia_weight * decrement_factor

            if inertia_weight < 0.4:
                inertia_weight = 0.4
            current_vel = current_vel * inertia_weight

        # Update storing structures
        clusters_best_pop[cluster] = current_best_pop
        clusters_distances[cluster] = current_dist
        clusters_pop[cluster] = current_pop
        clusters_vel[cluster] = current_vel
        clusters_best_particle[cluster] = current_best_particle
        clusters_best_path[cluster] = current_best_path
    end_pso_each_cluster = datetime.now()
    print("Ended PSO on each cluster")

    #Initialize elements useful for performing PSO on clusters order
    final_complete_distances, final_complete_paths, overall_pop, overall_vel = initialize_overall_params(
        cities, clusters_best_path, clusters_pop, not_primes_bool, ro, number_of_clusters)

    best_final_complete_distances = final_complete_distances
    best_overall_pop = overall_pop
    best_overall_particle = overall_pop[np.argsort(final_complete_distances)[0]]
    final_path = final_complete_paths[np.argsort(final_complete_distances)[0]]
    best_overall_iteration = t
    best_overall_distance = np.sort(final_complete_distances)[0]

    inertia_weight = original_inertia_weight
    results_for_each_iteration = [[t, best_overall_distance]]
    for t in range(1, max_number_of_iterations + 1):

        if t % 100 == 0:
            print("Iteration_overall: {}, min_distance: {}".format(t, best_overall_distance))
        # Update overall particles

        overall_vel = overall_vel + c1 * r1 * (best_overall_pop - overall_pop)
        overall_vel = overall_vel + c2 * r2 * (best_overall_particle - overall_pop)
        overall_pop = overall_pop + overall_vel
        overall_paths = generate_santas_path_from_particles_pop(overall_pop)
        final_complete_paths = compute_final_paths(overall_paths, clusters_best_path)
        final_complete_distances = evaluate_paths(paths=final_complete_paths, cities=cities,
                                                  not_primes_bool=not_primes_bool)
        # Find which paths lead to an improved distance and update the corresponding values
        distance_comparison = final_complete_distances < best_final_complete_distances
        best_final_complete_distances[distance_comparison] = final_complete_distances[distance_comparison]
        best_overall_pop[distance_comparison] = overall_pop[distance_comparison]
        # Update global best results if necessary
        current_iteration_best_index = np.argsort(final_complete_distances)[0]
        current_min_distance = final_complete_distances[current_iteration_best_index]
        if current_min_distance < best_overall_distance:
            best_overall_iteration = t
            best_overall_particle = overall_pop[current_iteration_best_index]
            final_path = final_complete_paths[current_iteration_best_index]
            best_overall_distance = current_min_distance

        results_for_each_iteration.append([t, best_overall_distance])
        if hybrid_evolutionary_approach:
            best_elements_indexes = np.argsort(final_complete_distances)[0:int(ro / 2)]
            worst_elements_indexes = np.argsort(final_complete_distances)[int(ro / 2):]
            overall_pop[worst_elements_indexes] = overall_pop[best_elements_indexes]
            overall_vel[worst_elements_indexes] = overall_vel[worst_elements_indexes]

        if t > best_overall_iteration + wait_interval:
            print("The algorithm is no longer improving")
            break

        inertia_weight = inertia_weight * decrement_factor
        if inertia_weight < 0.4:
            inertia_weight = 0.4
        overall_vel = inertia_weight * overall_vel

    return results_for_each_iteration, final_path, clusters_best_path, end_pso_each_cluster


from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from biblioteca import views
from contactos.views  import contactos
from pokemon.views  import get_pokemon,info_pokemon,tipo_pokemon

"""

aumento
xd
"""
urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^formulario-buscar/$', views.formulario_buscar),
    url(r'^buscar/$', views.buscar),
    url(r'^$', views.LibrosList.as_view(),name='libro_lista'),
    url(r'^nuevo/$', views.LibroCreate.as_view(),name='libro_nuevo'),
    url(r'^editar/(?P<pk>\d+)/$', views.LibroUpdate.as_view(),name='libro_editar'),
    url(r'^eliminar/(?P<pk>\d+)/$', views.LibroDelete.as_view(),name='libro_eliminar'),
    url(r'^func/lista/$', views.libro_list,name='libro_funct_lista'),
    url(r'^func/nuevo/$', views.libro_add,name='libro_funct_nuevo'),
    url(r'^funct/editar/(?P<id_libro>\d+)/$', views.libro_edit,name='libro_funct_editar'),
    url(r'^funct/eliminar/(?P<id_libro>\d+)/$', views.libro_delete,name='libro_funct_eliminar'),
    # ---------------- Autor-----------------------------------------------
    url(r'^autor/lista/$', views.AutorList.as_view(),name='autor_lista'),
    url(r'^autor/nuevo/$', views.AutorCreate.as_view(),name='autor_nuevo'),
    url(r'^autor/editar/(?P<pk>\d+)/$', views.AutorUpdate.as_view(),name='autor_editar'),
    url(r'^autor/eliminar/(?P<pk>\d+)/$', views.AutorDelete.as_view(),name='autor_eliminar'),
    url(r'^autor/func/lista/$', views.autor_list,name='autor_funct_lista'),
    url(r'^autor/func/nuevo/$', views.autor_add,name='autor_funct_nuevo'),
    url(r'^autor/func/editar/(?P<id_autor>\d+)/$', views.autor_edit,name='autor_funct_editar'),
    url(r'^autor/func/eliminar/(?P<id_autor>\d+)/$', views.autor_delete,name='autor_funct_eliminar'),
    url(r'^contactos/$', contactos),
    # ---------------- pokemon-----------------------------------------------
    url(r'^pokemon/$', get_pokemon,name='index_pokemon'),
    url(r'^pokemon/detalle/(?P<id_pokemon>\d+)/$',info_pokemon,name='detalle_pokemon'),
    url(r'^pokemon/tipo/(?P<id_tipo>\d+)/$',tipo_pokemon,name='tipo_pokemon'),

    url(r'^', include('biblioteca.urls')),
    url(r'^', include('api.urls')),
]


from simpful import *
import matplotlib.pylab as plt
from numpy import linspace, array

FS = FuzzySystem()

S_1 = FuzzySet(function=Triangular_MF(a=0, b=0, c=5), term="poor")
S_2 = FuzzySet(function=Triangular_MF(a=0, b=5, c=10), term="good")
S_3 = FuzzySet(function=Triangular_MF(a=5, b=10, c=10), term="excellent")
FS.add_linguistic_variable("Service", LinguisticVariable([S_1, S_2, S_3], concept="Service quality", universe_of_discourse=[0,10]))

F_1 = FuzzySet(function=Triangular_MF(a=0, b=0, c=10), term="rancid")
F_2 = FuzzySet(function=Triangular_MF(a=0, b=10, c=10), term="delicious")
FS.add_linguistic_variable("Food", LinguisticVariable([F_1, F_2], concept="Food quality", universe_of_discourse=[0,10]))

T_1 = FuzzySet(function=Triangular_MF(a=0, b=0, c=10), term="small")
T_2 = FuzzySet(function=Triangular_MF(a=0, b=10, c=20), term="average")
T_3 = FuzzySet(function=Trapezoidal_MF(a=10, b=20, c=25, d=25), term="generous")
FS.add_linguistic_variable("Tip", LinguisticVariable([T_1, T_2, T_3], universe_of_discourse=[0,25]))

R1 = "IF (Service IS poor) OR (Food IS rancid) THEN (Tip IS small)"
R2 = "IF (Service IS good) THEN (Tip IS average)"
R3 = "IF (Service IS excellent) OR (Food IS delicious) THEN (Tip IS generous)"
FS.add_rules([R1, R2, R3])

# Plotting surface
xs = []
ys = []
zs = []
DIVs = 20
for x in linspace(0,10,DIVs):
    for y in linspace(0,10,DIVs):
        FS.set_variable("Food", x)
        FS.set_variable("Service", y)
        tip = FS.inference()['Tip']
        xs.append(x)
        ys.append(y)
        zs.append(tip)
xs = array(xs)
ys = array(ys)
zs = array(zs)

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

xx, yy = plt.meshgrid(xs,ys)

ax.plot_trisurf(xs,ys,zs, vmin=0, vmax=25, cmap='gnuplot2')
ax.set_xlabel("Food")
ax.set_ylabel("Service")
ax.set_zlabel("Tip")
ax.set_title("Simpful", pad=20)
ax.set_zlim(0, 25)
plt.tight_layout()
plt.show()


#!/usr/bin/env python
"""
Setup script for arsf_dem

This file has been created by ARSF Data Analysis Node and
is licensed under the GPL v3 Licence. A copy of this
licence is available to download with this file.

"""

import glob, os, sys
from distutils.core import setup

# For windows also copy batch files, incase .py files
# aren't associated with Python.
if sys.platform == 'win32':
    scripts_list = glob.glob('scripts\\*.py')
    scripts_list.extend(glob.glob('scripts\\*.bat'))
else:
    scripts_list = glob.glob('scripts/*.py')

setup(
  name='arsf_dem',
  version = '0.1',
  description = 'ARSF-DAN utilities for working with DEMs',
  url = 'https://arsf-dan.nerc.ac.uk/trac/',
  packages = ['arsf_dem','arsf_dem.dem_lidar'],
  package_dir={'arsf_dem': 'arsf_dem'},
  package_data={'arsf_dem' : ['arsf_dem.cfg']},
  data_files=[(os.path.join('share','grass_db_template','WGS84LL','PERMANENT'),glob.glob(os.path.join('data','grass_db_template','WGS84LL','PERMANENT','*'))),
              (os.path.join('share','grass_db_template','UKBNG','PERMANENT'),glob.glob(os.path.join('data','grass_db_template','UKBNG','PERMANENT','*')))],
  scripts = scripts_list,
)