#An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly  steps, for every step it was noted if it was an uphill, , or a downhill,  step.
#Hikes always start and end at sea level, and each step up or down represents a  unit change in altitude. We define the following terms:
#A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
#A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.
#Given the sequence of up and down steps during a hike, find and print the number of valleys walked through.

#Sample Input

#8
#UDDDUDUU

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'countingValleys' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER steps
#  2. STRING path
#

def countingValleys(steps, path):
    level = 0
    valley = 0
    for i in range(steps):
        if (path[i]=='U'):          #To check if next path is up or down
            level += 1              # +1 level if up
            if (level==0):
                valley += 1         # +1 valley if 0
        else:
            level -=1               # -1 level if down
    return valley

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    steps = int(input().strip())

    path = input()

    result = countingValleys(steps, path)

    fptr.write(str(result) + '\n')

    fptr.close()


import cmk.utils.bi.bi_legacy_config_converter
import bi_test_data.sample_config as sample_config

def test_bi_legacy_config_conversion(monkeypatch):
    monkeypatch.setattr("cmk.utils.bi.bi_legacy_config_converter.BIManagement._get_config_string",
                        lambda x: sample_config.LEGACY_BI_PACKS_CONFIG_STRING)

    schema_from_legacy_packs = cmk.utils.bi.bi_legacy_config_converter.BILegacyConfigConverter(
    ).get_schema_for_packs()
    assert sample_config.bi_packs_config["packs"] == schema_from_legacy_packs


import numpy as np
import pandas as pd
from sklearn.metrics import log_loss
from sklearn.model_selection import StratifiedKFold, StratifiedShuffleSplit
from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping
from data_loader import get_data, generator

def cross_validation(model, X_train, X_train_angle, Y_train, X_test, X_test_angle, K):
    folds = list(StratifiedKFold(n_splits=K, shuffle=True).split(X_train, Y_train))
    y_test_pred = 0
    y_train_pred = 0
    y_valid_pred = 0.0 * Y_train
    for j, (train_idx, test_idx) in enumerate(folds):
        print('\n===================FOLD=', j)
        X_cv = X_train[train_idx]
        Y_cv = Y_train[train_idx]
        X_angle_cv = X_train_angle[train_idx]
        X_hold = X_train[test_idx]
        Y_hold = Y_train[test_idx]
        X_angle_hold = X_train_angle[test_idx]

        # define file path and get callbacks
        model.reset()
        file_path = model.model_kind + "_model_weights_%s.hdf5" % j
        model.train(X_cv, X_angle_cv, Y_cv, X_hold, X_angle_hold, Y_hold, file_path)

        # Getting Training Score
        loss, acc = model.eval(X_cv, X_angle_cv, Y_cv)
        print('Train loss:', loss, ' Train accuracy:', acc)

        # Getting Test Score
        loss, acc = model.eval(X_hold, X_angle_hold, Y_hold)
        print('Test loss:', loss, ' Test accuracy:', acc)

        # Getting validation Score.
        y_valid_pred[test_idx] = model.predict(X_hold, X_angle_hold)
        y_test_pred += model.predict(X_test, X_test_angle)
        y_train_pred += model.predict(X_train, X_train_angle)

    y_test_pred = y_test_pred / K
    y_train_pred = y_train_pred / K

    print('\n Train Loss = ', log_loss(Y_train, y_train_pred))
    print(' Train Acc = ', np.sum(Y_train == (y_train_pred > 0.5)) / Y_train.shape[0])
    print(' Val Loss = ', log_loss(Y_train, y_valid_pred))
    print(' Val Acc  =', np.sum(Y_train == (y_valid_pred > 0.5)) / Y_train.shape[0])

    return y_test_pred


# -*- coding: utf-8 -*-
"""
Created on Mon Sep 27 17:18:39 2021

@author: Dell
"""

import pandas as pd
from sklearn.utils import shuffle
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score, cross_validate, cross_val_predict
from sklearn.model_selection import GridSearchCV
from sklearn.tree import export_graphviz
import matplotlib as mpl
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
import numpy as np
from sklearn import linear_model
from math import sqrt as sq
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt

Ref = pd.read_csv('REf/Bolivia/Microgrids_results.csv', index_col=0)
sc1 = pd.read_csv('SC1/Bolivia/Microgrids_results.csv', index_col=0)
sc2 = pd.read_csv('SC2/Bolivia/Microgrids_results.csv', index_col=0)
sc3 = pd.read_csv('SC3/Bolivia/Microgrids_results.csv', index_col=0)
sc4 = pd.read_csv('SC4/Bolivia/Microgrids_results.csv', index_col=0)

print('The number of microgrids in the ref scenario is ' + str(len(Ref)) + '.')
print('The number of microgrids in the SC1 scenario is ' + str(len(sc1)) + '.')
print('The number of microgrids in the SC2 scenario is ' + str(len(sc2)) + '.')
print('The number of microgrids in the SC3 scenario is ' + str(len(sc3)) + '.')
print('The number of microgrids in the SC4 scenario is ' + str(len(sc4)) + '.')

for i in sc3.index:
    if sc3['PVcapacity2025'][i] <0:
        sc3.loc[i,'PVcapacity2025'] = 0

title_size = 70
pad = 30
tick_size = 40
fontsize = '30'
mpl.rcParams['xtick.labelsize'] = tick_size
mpl.rcParams['ytick.labelsize'] = tick_size

fig = plt.figure(figsize=(40,30))
size = [40,40]
label_size = 40
tick_size = 35

# LCOE plot
ax1 = fig.add_subplot(221)
LCOE = []
LCOE.append(Ref['MinimumOverallLCOE2025'])
LCOE.append(sc1['MinimumOverallLCOE2025'])
LCOE.append(sc2['MinimumOverallLCOE2025'])
LCOE.append(sc3['MinimumOverallLCOE2025'])
LCOE.append(sc4['MinimumOverallLCOE2025'])

ax1.boxplot(LCOE)
ax1.set_xlabel('Scenario', size=label_size)
ax1.set_ylabel('LCOE (USD/kWh)', size=label_size)
ax1.set_title('LCOE', size=title_size,pad=pad)
ax1.set_xticklabels(['Ref', 'SC1', 'SC2', 'SC3', 'SC4'])

# NPC plot
ax2 = fig.add_subplot(222)
NPC = []
NPC.append(Ref['NPC2025']/1000)
NPC.append(sc1['NPC2025']/1000)
NPC.append(sc2['NPC2025']/1000)
NPC.append(sc3['NPC2025']/1000)
NPC.append(sc4['NPC2025']/1000)

ax2.boxplot(NPC)
ax2.set_xlabel('Scenario', size=label_size)
ax2.set_ylabel('NPC (Thousands of USD)', size=label_size)
ax2.set_title('NPC', size=title_size,pad=pad)
ax2.set_xticklabels(['Ref', 'SC1', 'SC2', 'SC3', 'SC4'])

# PV plot
ax3 = fig.add_subplot(212)
PV = []
PV.append(Ref['PVcapacity2025'])
PV.append(sc1['PVcapacity2025'])
PV.append(sc2['PVcapacity2025'])
PV.append(sc3['PVcapacity2025'])
PV.append([0,0,0])

ax3.boxplot(PV)
ax3.set_xlabel('Scenario', size=label_size)
ax3.set_ylabel('PV (kW)', size=label_size)
ax3.set_title('PV installed capacity', size=title_size,pad=pad)
ax3.set_xticklabels(['Ref', 'SC1', 'SC2', 'SC3', 'SC4'])

plt.savefig('Scenarios_Results.png', bbox_inches='tight')
plt.show()
# The number of microgrids in the ref scenario is 221.
# The number of microgrids in the SC1 scenario is 259.
# The number of microgrids in the SC2 scenario is 163.
# The number of microgrids in the SC3 scenario is 345.
# The number of microgrids in the SC4 scenario is 701.


from fastapi import FastAPI, Cookie, Response
from typing import Union
from pydantic import BaseModel
from typing_extensions import Annotated

app = FastAPI()

@app.get("/books")
async def books(
        ads_id: Annotated[Union[str, None], Cookie()]
):
    return {
        "code": 200,
        "message": "访问成功",
        "ads_id": ads_id
    }


import random

candidates = ['가위', '바위', '보']
p1scissor, p1rock, p1paper = 0, 0, 0
p2scissor, p2rock, p2paper = 0, 0, 0

p1win, p1lose, p2win, p2lose, tie = 0, 0, 0, 0, 0

wintable = {
    '가위' : '보',
    '바위' : '가위',
    '보' : '바위'
}

repeat = int(input('가위바위보를 몇 번 할까요?: '))

for i in range(repeat):
    p1select = random.choice(candidates)
    if p1select == '가위':
        p1scissor += 1
    elif p1select == '바위':
        p1rock += 1
    else:
        p1paper += 1

    p2select = random.choice(candidates)
    if p2select == '가위':
        p2scissor += 1
    elif p2select == '바위':
        p2rock += 1
    else:
        p2paper += 1

    print('{}번째 승부:'.format(i+1))

    '''
    if p1select == p2select:
        tie += 1
        print('당신과 상대 모두 {}를 내서 비김\n'.format(p1select))
    elif p1select == '가위' and p2select == '바위':
        p1lose += 1
        p2win += 1
        print('당신은 {}로 패 / 상대는 {}로 승\n'.format(p1select, p2select))
    elif p1select == '가위' and p2select == '보':
        p1win += 1
        p2lose += 1
        print('당신은 {}로 승 / 상대는 {}로 패\n'.format(p1select, p2select))
    elif p1select == '바위' and p2select == '가위':
        p1win += 1
        p2lose += 1
        print('당신은 {}로 승 / 상대는 {}로 패\n'.format(p1select, p2select))
    elif p1select == '바위' and p2select == '보':
        p1lose += 1
        p2win += 1
        print('당신은 {}로 패 / 상대는 {}로 승\n'.format(p1select, p2select))
    elif p1select == '보' and p2select == '가위':
        p1lose += 1
        p2win += 1
        print('당신은 {}로 패 / 상대는 {}로 승\n'.format(p1select, p2select))
    elif p1select == '보' and p2select == '바위':
        p1win += 1
        p2lose += 1
        print('당신은 {}로 승 / 상대는 {}로 패\n'.format(p1select, p2select))
    '''

    if p1select == p2select:
        tie += 1
        print('당신과 상대 모두 {}를 내서 비김\n'.format(p1select))
    elif wintable[p1select] == p2select:
        p1win += 1
        p2lose += 1
        print('당신은 {}로 승 / 상대는 {}로 패\n'.format(p1select, p2select))
    else:
        p1lose += 1
        p2win += 1
        print('당신은 {}로 패 / 상대는 {}로 승\n'.format(p1select, p2select))

print('총 {}회 가위바위보 결과'.format(repeat))
print('\n')
print('당신은 가위 {}번, 바위 {}번, 보 {}번을 냈습니다.'.format(p1scissor, p1rock, p1paper))
print('그 결과 {}승 {}무 {}패를 했습니다'.format(p1win, tie, p1lose))
print('\n')
print('상대는 가위 {}번, 바위 {}번, 보 {}번을 냈습니다.'.format(p2scissor, p2rock, p2paper))
print('그 결과 {}승 {}무 {}패를 했습니다'.format(p2win, tie, p2lose))


"""
This module provides functionality to read an excel file containing
information about people and filter the rows based on a user's input by
communicating with a tcp server.

Functions:
send_message(message: str) -> str:
Sends a message to a server and returns the server's response.

read_request(name: str) -> None
Sends a read request message to a server and returns the server's response.

create_request(name: str) -> None
Sends a create request message to a server and returns the server's response.

update_request(name: str) -> None
Sends a update request message to a server and returns the server's response.

delete_request(name: str) -> None
Sends a delete request message to a server and returns the server's response.
"""

# Built In Imports
import socket

def send_message(message: str) -> str:
    """
    Sends a message to a server and returns the server's response.

    Parameters:
    ----------
    message : str
        The message to be sent to the server.

    Returns:
    -------
    str: The response from the server.
    """

    # create a TCP/IP socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # connect the socket to the server's address and port
    server_address = ('localhost', 7777)
    client_socket.connect(server_address)

    # send the message
    client_socket.sendall(message.encode())

    # receive the response
    response = client_socket.recv(1024)

    # close the socket
    client_socket.close()

    # return the response
    return response.decode()

def read_request(name: str) -> None:
    """
    Sends a read request message to a server and returns the server's response.

    Parameters:
    ----------
    name : str
        The user name to be read from the server.
    """

    # create the message
    request_message = f"READ {name}"

    # sending message
    return_response = send_message(request_message)
    print(return_response)

def create_request(name: str, age: int, email: str) -> None:
    """
    Sends a create request message to a server and returns the server's response.

    Parameters:
    ----------
    name : str
        The user name to be created in the server.
    age : int
        The age of user to be created in the server.
    email : str
        The email of the user to be created in the server.
    """

    # create the message
    request_message = f"CREATE {name} {age} {email}"

    # sending message
    return_response = send_message(request_message)
    print(return_response)

def update_request(name: str, age: int, email: str) -> None:
    """
    Sends a update request message to a server and returns the server's response.

    Parameters:
    ----------
    name : str
        The user name to be updated in the server.
    age : int
        The age of user to be updated in the server.
    email : str
        The email of the user to be updated in the server.
    """

    # create the message
    request_message = f"UPDATE {name} {age} {email}"

    # sending message
    return_response = send_message(request_message)
    print(return_response)

def delete_request(name: str) -> None:
    """
    Sends a delete request message to a server and returns the server's response.

    Parameters:
    ----------
    name : str
        The user name to be deleted from the server.
    """

    # create the message
    request_message = f"DELETE {name}"

    # sending message
    return_response = send_message(request_message)
    print(return_response)

if __name__ == '__main__':
    #read_request("nisha")
    #create_request(name="nish", age=26, email="tom@example.com")
    #update_request(name="nish", age=32, email="benz@sit.ac.in")
    delete_request("nish")


'''
Question: You are in an infinite 2D grid where you can move in any of the 8 directions :

 (x,y) to
    (x+1, y),
    (x - 1, y),
    (x, y+1),
    (x, y-1),
    (x-1, y-1),
    (x+1,y+1),
    (x-1,y+1),
    (x+1,y-1)
You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first point.

Example :

Input : [(0, 0), (1, 1), (1, 2)]
Output : 2
It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2).

Link: https://www.interviewbit.com/courses/programming/topics/arrays/problems/reach/

Approach:
You don't have to use the above cases.
Just find the difference between each points which is the distance from one point to another point.
'''
class Solution:
    # @param X : list of integers
    # @param Y : list of integers
    # Points are represented by (X[i], Y[i])
    # @return an integer
    def coverPoints(self, X, Y):
        # iterative approach
        # try out all the cases at each level for a point
        # and stop when the target is reached.
        points = len(X)-1
        idx = 0
        counter = 0
        while idx <= points-1:
            # find the number of steps to reach from source to target points
            x_steps  = abs(X[idx+1] - X[idx])
            y_steps  =  abs(Y[idx+1] - Y[idx])
            if x_steps > y_steps:
                counter += x_steps
            elif x_steps < y_steps:
                counter += y_steps
            else:
                counter += x_steps
            idx += 1
        return counter


# -*- coding: utf-8 -*-

import pandas as pd

def read_data(file_path):
    dataset = pd.read_csv(file_path)
    '''
    #查看并显示前三条记录
    print(dataset.iloc[0:3,:])
    print('-' * 30)
    '''
    # 查看样本数和特征数
    print(dataset.shape)
    print('-' * 30)

    return (dataset)

def get_dict(dataset):

    dataset.columns=['accession','position','result']

    # 创建字典
    dictionary={}
    for row in dataset.index:
        accession=dataset.loc[row,'accession']
        position=dataset.loc[row,'position']
        result=dataset.loc[row,'result']
        if accession in dictionary:
            dictionary[accession].append([position,result])
        else:
            dictionary[accession] = [[position,result]]  #用列表方式接收

    #print(dictionary)

    '''
    #查看并显示第一个键值对
    for key, value in dictionary.items():
        print(key,value)
        break

    #查看前三个键值对
    #print(list(dictionary.items())[:5])
    '''
    #查看字典长度
    print(len(dictionary))
    print('-' * 30)

    # 返回字典
    return (dictionary)

def main():
    result_data = read_data('../result/Result_all.csv')
    #创建结果字典
    result_dict=get_dict(result_data)

    #创建各类型位点数量字典
    number_dict={}
    for key in result_dict:
        n_de=0
        n_un=0
        n_in=0
        for i in range(len(result_dict[key])):
            if result_dict[key][i][1]==-1:
                n_de+=1
            elif result_dict[key][i][1]==1:
                n_in+=1
            else:
                n_un+=1
        number_dict[key]=[n_de,n_un,n_in]

    #查看前三个键值对
    #print(list(number_dict.items())[:5])

    # 查看字典长度
    print(len(number_dict))
    print('-' * 30)

    #输出list：不同类型位点的蛋白
    de_list=[]
    in_list=[]
    for key in number_dict:
        if number_dict[key][0]>0:
            de_list+=[key]
        if number_dict[key][-1]>0:
            in_list+=[key]
    print(len(de_list))
    print(len(in_list))

    #仅上升/仅下降/都包含/有改变
    both_list=list(set(de_list) & set(in_list))
    de_only=list(set(de_list) - set(in_list))
    in_only = list(set(in_list) - set(de_list))
    change_list=list(set(de_list) | set(in_list))

    print(len(both_list))
    print(len(de_only))
    print(len(in_only))
    print(len(change_list))

    #保存结果
    f = open('../enrichment_analysis/both.txt', 'w')
    for item in both_list:
        f.write(item + '\n')
    f.close()

    f = open('../enrichment_analysis/de_only.txt', 'w')
    for item in de_only:
        f.write(item + '\n')
    f.close()

    f = open('../enrichment_analysis/in_only.txt', 'w')
    for item in in_only:
        f.write(item + '\n')
    f.close()

    f = open('../enrichment_analysis/change.txt', 'w')
    for item in change_list:
        f.write(item + '\n')
    f.close()

if __name__ == '__main__':
    main()


'''
 Write a Python program to append a new item to the end of the array
Original array: array('i', [1, 3, 5, 7, 9])
Append 11 at the end of the array:
New array: array('i', [1, 3, 5, 7, 9, 11])
'''
from array import *
class Array:
    def __init__(self,a_array):
        self.a_array=a_array

    def add(self,num):
        self.a_array.append(num)
        print(self.a_array)
a=array("i",[1,3,5,7,9])
array1=Array(a)
array1.add(11)


import unittest
from .RFQTools import RFQTools

class MyTestCase(unittest.TestCase):

    def test_initialization(self):

        tools = RFQTools()
        tools.RFQMatcherContext(3410)
        self.assertEqual(True, True)

    def test_qmaterial_matcher(self):

        tools = RFQTools()
        result = tools.QuotedMaterialMatcher(3411, 'MP10001')

        for key, res in result.items():
            print(key, ":", res)

        self.assertEqual(True, True)

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


# coding = utf-8

import os
import torch
from torch.utils.data import (DataLoader, RandomSampler, SequentialSampler, TensorDataset, Dataset)

class InputExample(object):
    """A single training/test example for simple sequence classification."""

    def __init__(self, guid, _input, _output = None):
        """Constructs a InputExample.
        Args:
            Input: Prompt
            Output: Generated text
        """
        self.guid = guid
        self.input = _input
        self.output = _output

class DiaDataset(Dataset):
    def __init__(self, data):
        self.data = data

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return len(self.data)

class DataProcessor(object):
    """Base class for data converters for sequence classification data sets."""
    def __init__(self, data_dir, dataset):
        self.data_dir = data_dir
        self.dataset = dataset

    def get_train_examples(self):
        """See base class."""
        return self._create_examples(
            self._read_txt(os.path.join(self.data_dir, "train.txt")), "train")

    def get_dev_examples(self):
        """See base class."""
        return self._create_examples(
            self._read_txt(os.path.join(self.data_dir, "dev.txt")), "dev")

    def get_test_examples(self):
        """See base class."""
        return self._create_examples(
            self._read_txt(os.path.join(self.data_dir, "test.txt")), "test")

    def _create_examples(self, lines, set_type):
        examples = []
        input_ = lines[0][0]
        output_ = lines[0][1]
        length = len(input_)
        for i in range(length):
            guid = "%s-%s" % (set_type, i+1)
            _input = input_[i]
            _output = output_[i]
            examples.append(InputExample(guid=guid, _input=_input, _output=_output))
        return examples

    @classmethod
    def _read_txt(cls, input_file):
        '''
        read file
        return format :
        '''
        if os.path.exists(input_file) is False:
            return []
        data = []
        input_text = []
        output_text = []

        with open(input_file, "r", encoding = "utf-8") as f:
            for line in f:
                if len(line) == 0:
                    continue
                splits = line.strip().split('\t')

                input_text.append(splits[0])
                output_text.append(splits[1])

            if len(input_text) > 0:
                data.append((input_text, output_text))
        return data

    def convert_to_feature(self, tokenizer, examples, max_seq_length=16):

        # entities
        features = []
        for ex_index, example in enumerate(examples):

            labels = []
            valid_ids1 = []
            valid_ids2 = []

            input_text = example.input
            output_text = example.output

            token1 = tokenizer.tokenize(input_text)
            token2 = tokenizer.tokenize(output_text)

            token1 = token1[:max_seq_length - 2]
            token2 = token2[:max_seq_length - 2]

            for m in range(len(token1)):
                if m == 0:
                    valid_ids1.append(1)
                else:
                    valid_ids1.append(0)
            for m in range(len(token2)):
                if m == 0:
                    valid_ids2.append(1)
                else:
                    valid_ids2.append(0)

            tokens1 = ["[CLS]"] + token1 + ["[SEP]"]
            tokens2 = ["[CLS]"] + token2 + ["[SEP]"]
            valid_ids1 = [1] + valid_ids1 + [1]
            valid_ids2 = [1] + valid_ids2 + [1]
            input_ids1 = tokenizer.convert_tokens_to_ids(tokens1)
            input_ids2 = tokenizer.convert_tokens_to_ids(tokens2)

            segment_ids = [0] * max_seq_length

            if len(input_ids1) < max_seq_length:
                input_ids1 += [0] * (max_seq_length - len(input_ids1))
                valid_ids1 += [0] * (max_seq_length - len(valid_ids1))

            if len(input_ids2) < max_seq_length:
                input_ids2 += [0] * (max_seq_length - len(input_ids2))
                valid_ids2 += [0] * (max_seq_length - len(valid_ids2))

            assert len(input_ids1) == max_seq_length
            assert len(input_ids2) == max_seq_length
            assert len(segment_ids) == max_seq_length
            assert len(valid_ids1) == max_seq_length
            assert len(valid_ids2) == max_seq_length

            features.append({
                "input_ids": torch.tensor(input_ids1, dtype=torch.long),
                "output_ids": torch.tensor(input_ids2, dtype=torch.long),
                "segment_ids": torch.tensor(segment_ids, dtype=torch.long),
                "valid_ids1": torch.tensor(valid_ids1, dtype=torch.long),
                "valid_ids2": torch.tensor(valid_ids2, dtype=torch.long),
                "input_text": example.input,
                "output_text": example.output
            })

        return features

    def get_dataloader(self, features, batch_size, mode='train', rank=0,  world_size=1):
        if mode == "train" and world_size > 1:
            features = features[rank::world_size]

        data_set = DiaDataset(features)
        sampler = RandomSampler(data_set)
        return DataLoader(data_set, sampler=sampler, batch_size=batch_size)

    def get_all_dataloader(self, tokenizer, args):
        #train
        train_examples = self.get_train_examples()
        train_features = self.convert_to_feature(tokenizer, train_examples, args.max_seq_len)
        train_dataloader = self.get_dataloader(train_features, mode="train", rank=args.rank,
                                                    world_size=args.world_size, batch_size=args.batch_size)

        #test
        test_examples = self.get_test_examples()
        test_features = self.convert_to_feature(tokenizer, test_examples, args.max_seq_len)
        test_dataloader = self.get_dataloader(test_features, mode="test", batch_size=1)

        #dev
        dev_examples = self.get_dev_examples()
        dev_features = self.convert_to_feature(tokenizer, dev_examples, args.max_seq_len)
        dev_dataloader = self.get_dataloader(dev_features, mode="dev", batch_size=1)

        return train_dataloader, dev_dataloader, test_dataloader


import json

import flask
from flask import request
from datetime import datetime
import psycopg2
from flask import make_response
from werkzeug import exceptions
import waitress

app = flask.Flask(__name__)

def query(sql, *args):
    with psycopg2.connect("dbname=nosp_walk user=postgres") as conn:
        with conn.cursor() as cursor:
            cursor.execute(sql, *args)
            return list(cursor)

@app.route("/")
def main_page():
    return {"page": "main"}

@app.route("/sign_in", methods=["POST"])
def sign_in():
    json_data = json.loads(request.data)
    key_hash = json_data["key"]
    try:
        user = query(
                    f"select _id from users where key_hash = '{key_hash}'",
        )[0][0]
    except Exception as e:
        raise exceptions.Forbidden(e)

    if not user:
        raise exceptions.Forbidden()

    resp = make_response({"access": "granted"})
    resp.set_cookie("user", str(user))
    return resp

@app.route("/turn")
def turn():
    user = request.cookies.get("user")
    if not user:
        print("no user")
        return {"error": "cannot resolve user"}, 400
    walking_user_id = query(
    """
    select
        user_id,
        count(*) as walk_count
    from walks
    group by user_id
    order by walk_count
    """)[0][0]

    if str(walking_user_id) == user:
        return {"turn": "you"}
    return {"turn": "other"}

@app.route("/history")
def get_history():
    req_data = request.args
    from_date, to_date = (req_data["from"], req_data["to"])
    walks = query(
        f"""
    select * from walks
    where walked_at between $1 and $2
    """,
        datetime.fromtimestamp(from_date),
        datetime.fromtimestamp(to_date),
    )

    walks = [
        {"date": w["date"], "time": w["time"], "walker": w["walker"]} for w in walks
    ]
    return walks

@app.route("/walk", methods=["POST"])
def walk():
    json_data = json.loads(request.data)

    walk_timestamp = json_data["timestamp"]
    walker = json_data["walker"]
    assert walker in ("P", "M")

    if request.cookies.get("user") != walker:
        raise exceptions.Forbidden()

    query(
        f"""
    insert into walks (walker, walked_at) values ($1, $2)
    """,
        datetime.fromtimestamp(walk_timestamp),
        walker,
    )

    return {"status": "walk_registered"}


import numpy as np
import pandas as pd
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt

from multiprocessing import Pool
from functools import partial

from sklearn.model_selection import KFold
from MatrixFactorization import FactorizeMatrix, GetRepresentationError, CreateLatentVariables
from FeatureSimilarity import GetTopGenes

def RandomParams(eta_low, eta_high, lamb1_low, lamb1_high, lamb2_low, lamb2_high, num_reps=20):
    hyper_params = np.zeros((num_reps, 3)).astype(np.float32)
    hyper_params[:, 0] = np.random.uniform(low=eta_low, high=eta_high, size=(num_reps,))
    hyper_params[:, 1] = np.random.uniform(low=lamb1_low, high=lamb1_high, size=(num_reps,))
    hyper_params[:, 2] = np.random.uniform(low=lamb2_low, high=lamb2_high, size=(num_reps,))
    return hyper_params

def TrainOnParams(params, X, k, neighbors, train_indices, test_indices):
    print('.', end='')
    n, g = X.shape
    eta, lamb1, lamb2 = params
    U, V = CreateLatentVariables(n, g, k)
    U, V = FactorizeMatrix(X, U, V, neighbors, eta=eta, lamb1=lamb1, lamb2=lamb2, trainIndices=train_indices)
    paramError = GetRepresentationError(X, U, V, known_indices=test_indices)
    return paramError

def TrainVerboseOnParams(params, X, k, neighbors, train_indices, test_indices):
    print('.', end='')
    n, g = X.shape
    eta, lamb1, lamb2 = params
    U, V = CreateLatentVariables(n, g, k)
    U, V, trainError, testError = FactorizeMatrix(X, U, V, neighbors, eta=eta, lamb1=lamb1, lamb2=lamb2, trainIndices=train_indices, returnErrorVectors=True)
    paramError = GetRepresentationError(X, U, V, known_indices=test_indices)
    return paramError, trainError, testError

def CrossValidation(X, k, hyper_params, neighbors=None, foldcount=5, returnVectorDF=False, numProcesses=20):
    '''
    Runs the matrix factorization algorithm for each specified value of eta and lambda
    and computes the reconstruction errors for each run.

    Args:
        X: An n x g, possibly sparse numpy matrix, where missing entries are indicated by np.nan values,
           where n represents the number of samples and g represents the number of genes, or items.
        k: The latent dimension of the factorization. Typically, k < min(n, g).
        hyper_params: A list of tuples, each corresponding to a setting of hyper parameters (eta, lamb1, lamb2).
        foldcount: An integer denoting the number of folds for cross validation.

    Returns:
        A len(etas) x len(lambs) x foldcount tensor denoting the reconstruction error for each
        setting of eta and lambda on each fold.
    '''
    n, g = X.shape

    kf = KFold(n_splits=foldcount, shuffle=True)
    errorsDF = pd.DataFrame(np.zeros((len(hyper_params) * foldcount, 5)))
    errorsDF.columns = ['eta', 'lamb1', 'lamb2', 'error', 'fold']

    #Okay not to shuffle because kf shuffles for you
    known_indices = np.argwhere(~np.isnan(X)).astype(np.int32)
    np.random.shuffle(known_indices)

    if returnVectorDF:
        trainErrorDF = pd.DataFrame()
        testErrorDF  = pd.DataFrame()

    fold = 0
    df_index = 0

    p = Pool(numProcesses)

    for train_index, test_index in kf.split(known_indices):
        print('Training fold {}'.format(fold))
        if returnVectorDF:
            foldTrainDF = pd.DataFrame()
            foldTestDF  = pd.DataFrame()

        train_indices = known_indices[train_index].astype(np.int32)
        test_indices  = known_indices[test_index].astype(np.int32)

        if (returnVectorDF):
            errorVec = p.map(partial(TrainVerboseOnParams, X=X, k=k, neighbors=neighbors,
                                     train_indices=train_indices, test_indices=test_indices), hyper_params)
            for i in range(len(hyper_params)):
                eta, lamb1, lamb2 = hyper_params[i]
                paramError, trainError, testError = errorVec[i]
                foldTrainDF = pd.concat([foldTrainDF,
                                      pd.DataFrame({
                                          'eta{:.5f}_lamb1{:.5f}_lamb2{:.5f}'.format(eta, lamb1, lamb2): trainError
                                      })
                                    ], axis=1)
                foldTestDF  = pd.concat([foldTestDF,
                                          pd.DataFrame({
                                          'eta{:.5f}_lamb1{:.5f}_lamb2{:.5f}'.format(eta, lamb1, lamb2): testError
                                      })
                                    ], axis=1)
                errorsDF.iloc[df_index] = np.array([eta, lamb1, lamb2, paramError, fold])
                df_index += 1
        else:
            errorVec = p.map(partial(TrainOnParams, X=X, k=k, neighbors=neighbors,
                                     train_indices=train_indices, test_indices=test_indices), hyper_params)
            for i in range(len(hyper_params)):
                eta, lamb1, lamb2 = hyper_params[i]
                paramError = errorVec[i]
                errorsDF.iloc[df_index] = np.array([eta, lamb1, lamb2, paramError, fold])
                df_index += 1

        if returnVectorDF:
            foldTrainDF['fold'] = fold
            foldTestDF['fold']  = fold
            maxEpochs, _ = foldTrainDF.shape
            foldTrainDF['epochs']  = np.arange(maxEpochs).astype(np.float32)
            foldTestDF['epochs']   = np.arange(maxEpochs).astype(np.float32)

            trainErrorDF = pd.concat([trainErrorDF, foldTrainDF])
            testErrorDF  = pd.concat([testErrorDF, foldTestDF])

        fold = fold + 1

    p.close()
    p.join()

    if returnVectorDF:
        return errorsDF, trainErrorDF, testErrorDF
    else:
        return errorsDF

def PlotErrorDF(errorDF, id_vars=['epochs', 'fold'], ax=None):
    data = pd.melt(errorDF, id_vars=id_vars, value_name='error', var_name='run')
    if ax is not None:
        ax = sns.lineplot(x='epochs', y ='error', hue='run', data=data, ax=ax, legend=False)
    else:
        ax = sns.lineplot(x='epochs', y ='error', hue='run', data=data, legend='brief')
        plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    return ax

def PlotParamDF(paramDF, id_vars=['error', 'fold'], ax=None):
    data = pd.melt(paramDF, id_vars=id_vars, value_name='param_value', var_name='param_type')
    if ax is not None:
        ax = sns.lineplot(x='param_value', y='error', hue='param_type', data=data, ax=ax)
    else:
        ax = sns.lineplot(x='param_value', y='error', hue='param_type', data=data)
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    return ax


#!/usr/bin/env python3
print("please enter count number: ")
N = int(input())
# N = 10
sum = 0
count = 0
print("please enter",N,"numbers: ")
while count < N:
    number = float(input())
    sum = sum + number
    count = count + 1
average = sum / N
print("N = {}, Sum = {}".format(N, sum))
print("average = {:.2f}".format(average))


import sys
input=sys.stdin.readline
n,m,k=map(int,input().split())
c=list(map(int,input().split()))
friend=[i for i in range(n+1)]

def find(friend,x):
    if x!=friend[x]:
        friend[x]=find(friend,friend[x])
    return friend[x]

def union(friend,a,b):
    rootA=find(friend,a)
    rootB=find(friend,b)
    if rootA<rootB:
        friend[rootB]=rootA
    elif rootA>rootB:
        friend[rootA]=rootB

for _ in range(m):
    a,b=map(int,input().split())
    union(friend,a,b)

tmp=dict()
for i in range(n):
    root=find(friend,i+1)
    if root in tmp:
        tmp[root][0]+=c[i]
        tmp[root][1]+=1
    else:
        tmp[root]=[c[i],1]

steal=[]
for key,item in tmp.items():
    steal.append(item)

dp=[[0]*(k+1) for _ in range(len(steal)+1)]
for i in range(1,len(steal)+1):
    for j in range(1,k+1):
        if j<=steal[i-1][1]:
            dp[i][j]=dp[i-1][j]
        else:
            dp[i][j]=max(dp[i-1][j],dp[i-1][j-steal[i-1][1]]+steal[i-1][0])
print(dp[-1][-1])


items = []
numbers = []

def add_item():
    item_name = input("Enter the item name: ")
    number = float(input("Enter the associated number: "))
    items.append(item_name)
    numbers.append(number)

def remove_item():
    print("Items in the collection:")
    for i, item in enumerate(items, start=1):
        print(f"{i}. {item}")
    index = int(input("Enter the location of the item you want to remove: ")) - 1
    del items[index]
    del numbers[index]

def insert_item():
    print("Items in the collection:")
    for i, item in enumerate(items, start=1):
        print(f"{i}. {item}")
    index = int(input("Enter the location where you want to insert the item: ")) - 1
    item_name = input("Enter the item name: ")
    number = float(input("Enter the associated number: "))
    items.insert(index, item_name)
    numbers.insert(index, number)

def swap_items():
    print("Items in the collection:")
    for i, item in enumerate(items, start=1):
        print(f"{i}. {item}")
    index1 = int(input("Enter the location of the first item: ")) - 1
    index2 = int(input("Enter the location of the second item: ")) - 1
    items[index1], items[index2] = items[index2], items[index1]
    numbers[index1], numbers[index2] = numbers[index2], numbers[index1]

def find_max():
    max_index = 0
    for i in range(1, len(numbers)):
        if numbers[i] > numbers[max_index]:
            max_index = i
    print(f"The item with the maximum value is '{items[max_index]}' with a value of {numbers[max_index]}")

def print_collection():
    print("Collection:")
    for i, item in enumerate(items, start=1):
        print(f"{i}. {item} - {numbers[i-1]}")

print("Welcome to the List App!")

while True:
    print("\nMenu:")
    print("1. Add an item to the end of the collection")
    print("2. Remove an item from the collection")
    print("3. Insert an item into the collection")
    print("4. Swap two items in the collection")
    print("5. Search the collection for the item with the maximum value")
    print("6. Print the full collection")
    print("7. Quit the application\n")

    choice = input("Enter your choice: ")

    if choice == "1":
        add_item()
    elif choice == "2":
        remove_item()
    elif choice == "3":
        insert_item()
    elif choice == "4":
        swap_items()
    elif choice == "5":
        find_max()
    elif choice == "6":
        print_collection()
    elif choice == "7":
        print("Goodbye!")
        break
    else:
        print("Invalid choice. Please try again.")


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

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import multiprocessing as mp

import math
import plotly
from plotly.graph_objs import Scatter, Line

import numpy as np
from numpy.lib.stride_tricks import as_strided as ast
from scipy.ndimage import gaussian_filter
from scipy import misc

def get_imgs_psnr(imgs1, imgs2):
	# per image
	def get_psnr(img1, img2):
		mse = np.mean((img1 - img2) ** 2)
		if mse == 0:
			return 100
		pixel_max = 255.0
		# return 10 * math.log10((pixel_max ** 2) / mse)
		return 20 * math.log10(pixel_max / math.sqrt(mse))
	assert imgs1.shape[0] == imgs2.shape[0], "Batch size is not match."
	assert np.mean(imgs1) >= 1.0, "Check 1st input images range"
	assert np.mean(imgs2) >= 1.0, "Check 2nd input images range"
	PSNR_RGB = list()

	for num_of_batch in range(imgs1.shape[0]):
		PSNR_RGB.append(get_psnr(imgs1[num_of_batch], imgs2[num_of_batch]))

	PSNR_RGB = np.mean(np.array(PSNR_RGB))

	return PSNR_RGB

# imgs1/imgs2: 0 ~ 255, float32, numpy, rgb
# [batch_size, height, width, channel]
# return: list
def get_imgs_psnr_ssim(imgs1, imgs2):
	# per image
	def get_psnr(img1, img2):
		mse = np.mean((img1 - img2) ** 2)
		if mse == 0:
			return 100
		pixel_max = 255.0
		# return 10 * math.log10((pixel_max ** 2) / mse)
		return 20 * math.log10(pixel_max / math.sqrt(mse))

	def block_view(A, block=(3, 3)):
		"""Provide a 2D block view to 2D array. No error checking made.
		Therefore meaningful (as implemented) only for blocks strictly
		compatible with the shape of A."""
		# simple shape and strides computations may seem at first strange
		# unless one is able to recognize the 'tuple additions' involved ;-)
		shape = (A.shape[0] / block[0], A.shape[1] / block[1]) + block
		strides = (block[0] * A.strides[0], block[1] * A.strides[1]) + A.strides
		return ast(A, shape=shape, strides=strides)

	def ssim(img1, img2, C1=0.01**2, C2=0.03**2):
		bimg1 = block_view(img1, (4, 4))
		bimg2 = block_view(img2, (4, 4))
		s1 = np.sum(bimg1, (-1, -2))
		s2 = np.sum(bimg2, (-1, -2))
		ss = np.sum(bimg1*bimg1, (-1, -2)) + np.sum(bimg2*bimg2, (-1, -2))
		s12 = np.sum(bimg1*bimg2, (-1, -2))
		vari = ss - s1*s1 - s2*s2
		covar = s12 - s1*s2
		ssim_map = (2 * s1 * s2 + C1) * (2 * covar + C2) / ((s1 * s1 + s2 * s2 + C1) * (vari + C2))
		return np.mean(ssim_map)

	# FIXME there seems to be a problem with this code
	def ssim_exact(img1, img2, sd=1.5, C1=0.01**2, C2=0.03**2):
		mu1 = gaussian_filter(img1, sd)
		mu2 = gaussian_filter(img2, sd)
		mu1_sq = mu1 * mu1
		mu2_sq = mu2 * mu2
		mu1_mu2 = mu1 * mu2
		sigma1_sq = gaussian_filter(img1 * img1, sd) - mu1_sq
		sigma2_sq = gaussian_filter(img2 * img2, sd) - mu2_sq
		sigma12 = gaussian_filter(img1 * img2, sd) - mu1_mu2
		ssim_num = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2))
		ssim_den = ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))
		ssim_map = ssim_num / ssim_den
		return np.mean(ssim_map)
	assert imgs1.shape[0] == imgs2.shape[0], "Batch size is not match."
	assert np.mean(imgs1) >= 1.0, "Check 1st input images range"
	assert np.mean(imgs2) >= 1.0, "Check 2nd input images range"
	PSNR_RGB, SSIM = list(), list()
	for num_of_batch in range(imgs1.shape[0]):
		# RGB
		PSNR_RGB.append(get_psnr(imgs1[num_of_batch], imgs2[num_of_batch]))
		SSIM.append(ssim_exact(imgs1[num_of_batch], imgs2[num_of_batch]))
	PSNR_RGB = np.mean(np.array(PSNR_RGB))
	SSIM = np.mean(np.array(SSIM))
	return PSNR_RGB, SSIM

def save_imgs(img, global_img, origin_img, downsample_img, file_name):
	'''Save image
	'''
	N_samples, height, width, channel = img.shape
	N_row = int(N_samples/4)
	N_col = 4*4

	repeat_ds_img = []
	for index in range(N_samples):
		#print downsample_img.shape
		repeat_ds_img.append(downsample_img[index].repeat(axis=0, repeats=8).repeat(axis=1, repeats=8).astype(np.uint8))
	repeat_ds_img = np.array(repeat_ds_img)

	combined_imgs = np.ones((N_row*height, N_col*width, channel))
	for i in range(N_row):
		for j in range(int(N_col/4)):
			n = 4*j
			m = 4*j+1
			l = 4*j+2
			k = 4*j+3
			combined_imgs[int(i*height):int((i+1)*height), int(n*width):int((n+1)*width), :] = repeat_ds_img[int(i*(N_col/4)+j)]
			combined_imgs[int(i*height):int((i+1)*height), int(m*width):int((m+1)*width), :] = origin_img[int(i*(N_col/4)+j)]
			combined_imgs[int(i*height):int((i+1)*height), int(l*width):int((l+1)*width), :] = img[int(i*(N_col/4)+j)]
			combined_imgs[int(i*height):int((i+1)*height), int(k*width):int((k+1)*width), :] = global_img[int(i*(N_col/4)+j)]
	print('Saving the images.', file_name)
	misc.imsave(file_name, combined_imgs)

# Converts a state from the OpenAI Gym (a numpy array) to a batch tensor
def state_to_tensor(state):
	return torch.from_numpy(state).float().unsqueeze(0)

class ParamInit(object):
	'''parameter initializer
	'''

	def __init__(self, method, **kargs):
		super(ParamInit, self).__init__()
		self.inits = {
			'xavier_normal': torch.nn.init.xavier_normal,
			'xavier_uniform': torch.nn.init.xavier_uniform,
			'kaming_normal': torch.nn.init.kaiming_normal,
			'kaming_uniform': torch.nn.init.kaiming_uniform,
		}
		if method not in self.inits:
			raise RuntimeError('unknown initialization %s' % method)
		self.method = self.inits[method]
		self.kargs = kargs

	def __call__(self, module):
		classname = module.__class__.__name__
		if 'Conv' in classname and hasattr(module, 'weight'):
			self.method(module.weight, **self.kargs)
		elif 'BatchNorm' in classname:
			module.weight.data.normal_(1.0, 0.02)
			module.bias.data.fill_(0)

# Global counter
class Counter():
	def __init__(self):
		self.val = mp.Value('i', 0)
		self.lock = mp.Lock()

	def increment(self):
		with self.lock:
			self.val.value += 1

	def value(self):
		with self.lock:
			return self.val.value

# Plots min, max and mean + standard deviation bars of a population over time
def plot_line(xs, ys_population, title, filename):
	max_colour = 'rgb(0, 132, 180)'
	mean_colour = 'rgb(0, 172, 237)'
	std_colour = 'rgba(29, 202, 255, 0.2)'

	ys = torch.Tensor(ys_population)
	ys_min = ys.min(1)[0].squeeze()
	ys_max = ys.max(1)[0].squeeze()
	ys_mean = ys.mean(1).squeeze()
	ys_std = ys.std(1).squeeze()
	ys_upper, ys_lower = ys_mean + ys_std, ys_mean - ys_std

	trace_max = Scatter(x=xs, y=ys_max.numpy(), line=Line(color=max_colour, dash='dash'), name='Max')
	trace_upper = Scatter(x=xs, y=ys_upper.numpy(), line=Line(color='transparent'), name='+1 Std. Dev.', showlegend=False)
	trace_mean = Scatter(x=xs, y=ys_mean.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color=mean_colour), name='Mean')
	trace_lower = Scatter(x=xs, y=ys_lower.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color='transparent'), name='-1 Std. Dev.', showlegend=False)
	trace_min = Scatter(x=xs, y=ys_min.numpy(), line=Line(color=max_colour, dash='dash'), name='Min')

	plotly.offline.plot({
		'data': [trace_upper, trace_mean, trace_lower, trace_min, trace_max],
		'layout': dict(title=title,
		 xaxis={'title': 'Step'},
		 yaxis={'title': 'Average'})
	}, filename=filename, auto_open=False)

def plot_loss(xs, p_loss, v_loss, title, filename):

	p_loss_color = 'rgb(0, 255, 0)'
	v_loss_color = 'rgb(255, 0, 0)'

	trace_p = Scatter(x=xs, y=p_loss, line=Line(color=p_loss_color), mode='lines', name='Policy Loss')
	trace_v = Scatter(x=xs, y=v_loss, line=Line(color=v_loss_color), mode='lines', name='Value Loss')

	plotly.offline.plot({
		'data': [trace_p, trace_v],
		'layout': dict(title=title,
		 xaxis={'title': 'Step'},
		 yaxis={'title': 'Average'})
	}, filename=filename, auto_open=False)


"""Transforms for preprocessing images during data loading"""
import PIL
import torch
import copy
import numpy as np

def img_pad(img, mode='warp', size=224):
    """
    Pads a given image.
    Crops and/or pads a image given the boundries of the box needed
    img: the image to be coropped and/or padded
    bbox: the bounding box dimensions for cropping
    size: the desired size of output
    mode: the type of padding or resizing. The modes are,
        warp: crops the bounding box and resize to the output size
        same: only crops the image
        pad_same: maintains the original size of the cropped box  and pads with zeros
        pad_resize: crops the image and resize the cropped box in a way that the longer edge is equal to
        the desired output size in that direction while maintaining the aspect ratio. The rest of the image is
        padded with zeros
        pad_fit: maintains the original size of the cropped box unless the image is biger than the size in which case
        it scales the image down, and then pads it
    """
    assert (mode in ['same', 'warp', 'pad_same', 'pad_resize', 'pad_fit']), 'Pad mode %s is invalid' % mode
    image = img.copy()
    if mode == 'warp':
        warped_image = image.resize((size, size), PIL.Image.BICUBIC)
        return warped_image
    elif mode == 'same':
        return image
    elif mode in ['pad_same', 'pad_resize', 'pad_fit']:
        img_size = image.size  # size is in (width, height)
        ratio = float(size) / max(img_size)
        if mode == 'pad_resize' or \
                (mode == 'pad_fit' and (img_size[0] > size or img_size[1] > size)):
            img_size = tuple([int(img_size[0] * ratio), int(img_size[1] * ratio)])
            image = image.resize(img_size, PIL.Image.BICUBIC)
        padded_image = PIL.Image.new("RGB", (size, size))
        padded_image.paste(image, ((size - img_size[0]) // 2,
                                   (size - img_size[1]) // 2))
        return padded_image

def squarify_bbox(bbox, squarify_ratio, img_width):
    width = abs(bbox[0] - bbox[2])
    height = abs(bbox[1] - bbox[3])
    width_change = height * squarify_ratio - width
    bbox[0] = bbox[0] - width_change / 2
    bbox[2] = bbox[2] + width_change / 2
    # Squarify is applied to bounding boxes in Matlab coordinate starting from 1
    if bbox[0] < 0:
        bbox[0] = 0
    # check whether the new bounding box goes beyond image boarders
    # If this is the case, the bounding box is shifted back
    if bbox[2] > img_width:
        # bbox[1] = str(-float(bbox[3]) + img_dimensions[0])
        bbox[0] = bbox[0] - bbox[2] + img_width
        bbox[2] = img_width

    return bbox

def bbox_sanity_check(img, bbox):
    """
    This is to confirm that the bounding boxes are within image boundaries.
    If this is not the case, modifications is applied.
    This is to deal with inconsistencies in the annotation tools
    """
    img_width, img_heigth = img.size
    if bbox[0] < 0:
        bbox[0] = 0.0
    if bbox[1] < 0:
        bbox[1] = 0.0
    if bbox[2] >= img_width:
        bbox[2] = img_width - 1
    if bbox[3] >= img_heigth:
        bbox[3] = img_heigth - 1
    return bbox

def jitter_bbox(img, bbox, mode, ratio):
    """
    This method jitters the position or dimentions of the bounding box.
    mode: 'same' returns the bounding box unchanged
          'enlarge' increases the size of bounding box based on the given ratio.
          'random_enlarge' increases the size of bounding box by randomly sampling a value in [0,ratio)
          'move' moves the center of the bounding box in each direction based on the given ratio
          'random_move' moves the center of the bounding box in each direction
          by randomly sampling a value in [-ratio,ratio)
    ratio: The ratio of change relative to the size of the bounding box. For modes 'enlarge' and 'random_enlarge'
           the absolute value is considered.
    Note: Tha ratio of change in pixels is calculated according to the smaller dimension of the bounding box.
    """
    assert (mode in ['same', 'enlarge', 'move', 'random_enlarge', 'random_move']), \
        'mode %s is invalid.' % mode

    if mode == 'same':
        return bbox

    if mode in ['random_enlarge', 'enlarge']:
        jitter_ratio = abs(ratio)
    else:
        jitter_ratio = ratio

    if mode == 'random_enlarge':
        jitter_ratio = np.random.random_sample() * jitter_ratio
    elif mode == 'random_move':
        # for ratio between (-jitter_ratio, jitter_ratio)
        # for sampling the formula is [a,b), b > a,
        # random_sample * (b-a) + a
        jitter_ratio = np.random.random_sample() * jitter_ratio * 2 - jitter_ratio

    if len(bbox) == 4:
        b = copy.deepcopy(bbox)
        bbox_width = b[2] - b[0]
        bbox_height = b[3] - b[1]

        width_change = bbox_width * jitter_ratio
        height_change = bbox_height * jitter_ratio

        if width_change < height_change:
            height_change = width_change
        else:
            width_change = height_change

        if mode in ['enlarge', 'random_enlarge']:
            b[0] = b[0] - width_change // 2
            b[1] = b[1] - height_change // 2
        else:
            b[0] = b[0] + width_change // 2
            b[1] = b[1] + height_change // 2

        b[2] = b[2] + width_change // 2
        b[3] = b[3] + height_change // 2

        # Checks to make sure the bbox is not exiting the image boundaries
        jit_box = bbox_sanity_check(img, b)

    return jit_box

def crop_and_rescale(image, bbox, cropping_ratio, width, height):
    """
    Crop the top 1/n of the image and resize the image to desired size.
    The bbox are preprocessed accordingly.
    """
    w, h = image.size
    image = image.crop((0, h * cropping_ratio, w, h))
    # rescale
    image_new = image.resize((width, height), PIL.Image.BICUBIC)
    # compute new bbox
    scale_x = width / w
    scale_y = height / h / (1 - cropping_ratio)
    x1 = bbox[0] * scale_x
    y1 = (bbox[1] - h * cropping_ratio) * scale_y
    x2 = bbox[2] * scale_x
    y2 = (bbox[3] - h * cropping_ratio) * scale_y
    bbox_new = [int(x1), int(y1), int(x2), int(y2)]

    return image_new, bbox_new

def random_flip(image, bbox, probability):
    """
    Flip the image horizontally with given probability.
    When the image is flipped,
    the bbox annotation wll also be transformed correspondingly.
    """
    if float(torch.rand(1).item()) < probability:
        image = image.transpose(PIL.Image.FLIP_LEFT_RIGHT)
        w, h = image.size
        # box_w = abs(bbox[0] - bbox[2])
        x_max = w - bbox[0]
        x_min = w - bbox[2]
        bbox[0] = x_min
        bbox[2] = x_max

    return image, bbox


class Pizza:
    '''
    Pizza class to define Pizza Objects
    '''

    def __init__(self,id,title,overview,image,vote_average,vote_count):
        self.id = id
        self.title= title
        self.overview = overview
        self.vote_average = vote_average
        self.vote_count = vote_count


# Scrapy settings for acvo_org project
#
# For simplicity, this file contains only the most important settings by
# default. All the other settings are documented here:
#
#     http://doc.scrapy.org/en/latest/topics/settings.html
#

BOT_NAME = 'acvo_org'

SPIDER_MODULES = ['acvo_org.spiders']
NEWSPIDER_MODULE = 'acvo_org.spiders'

ITEM_PIPELINES = {
    'acvo_org.pipelines.AcvoOrgPipeline': 300,
}

MONGODB_SERVER = "localhost"
MONGODB_PORT = 27017
MONGODB_DB = "acvo_14"
MONGODB_COLLECTION = "diplomates"
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'acvo_org (+http://www.yourdomain.com)'


from rest_framework.fields import IntegerField
from rest_framework.serializers import ModelSerializer, Serializer

from post.models import Post, PostLike

class PostCreateSerializer(ModelSerializer):
    class Meta:
        model = Post
        fields = (
            'id',
            'text',
            'author_id',
        )
        extra_kwargs = {
            'author_id': {'write_only': True},
        }

class PostLikeSerializer(ModelSerializer):
    class Meta:
        model = PostLike
        fields = (
            'id',
            'post',
            'user_id',
        )
        extra_kwargs = {
            'user_id': {'write_only': True},
        }

class PostLikeAnalyticsSerializer(Serializer):
    number_of_likes = IntegerField()


#!Python-2.7.11/bin/python
import os,sys
import argparse
import numpy as np
import copy
sys.path.append('Python-2.7.11/lib/python2.7/site-packages')
from ete2 import Tree,TreeStyle,TextFace,NodeStyle

parser = argparse.ArgumentParser(description='Phylogenetic Tree analysis for Cancer Evolution.')
parser.add_argument('-i', '--maf', help='Maf file', required=True)
parser.add_argument('-g', '--germline', help='Germline mutation file', default=None)
parser.add_argument('-n', '--root-number', help='Root number for construction tree, default None', type=int, default=None)
parser.add_argument('-s', '--samplelist', help='Samplelist', default=None)
parser.add_argument('-o', '--outdir', help='Output dir', required=True)
parser.add_argument('-p', '--patient', help='Patient ID', default="patient")
parser.add_argument('-f', '--func', help='Function filter, default: coding',default='coding',choices=['coding','non-synonymous','exon','gene','all'])
parser.add_argument('-c', '--chr', help='Chr column index for Germline, default=0', default=0,type=int)
parser.add_argument('-t', '--position', help='Position column index for Germline, default=1', default=1,type=int)
parser.add_argument('-r', '--ref', help='Ref-base column index for Germline, default=3', default=3,type=int)
parser.add_argument('-a', '--alt', help='Alt-base column index for Germline, default=4', default=4,type=int)
parser.add_argument('-x', '--gene', help='Gene column index for Germline, default=6', default=6,type=int)
parser.add_argument('-l', '--indel', help='Wether INDEL used for tree construction', action='store_true')
parser.add_argument('-d', '--drivergene', help='Driver Genes for marke, default None', default=None)
args = parser.parse_args()

def filter_funcs(func, type='coding'):
	coding_funcs  = ['Missense_Mutation', 'Nonsense_Mutation', 'Nonstop_Mutation', 'Silent', 'Splice_Site', \
		'Frame_Shift_Del', 'Frame_Shift_Ins', 'In_Frame_Del', 'In_Frame_Ins']
	nonsyn_funcs  = ['Missense_Mutation', 'Nonsense_Mutation', 'Nonstop_Mutation', 'Splice_Site', \
		'Frame_Shift_Del', 'Frame_Shift_Ins', 'In_Frame_Del', 'In_Frame_Ins']
	exon_funcs    = ['3\'UTR','5\'UTR','Missense_Mutation','Nonsense_Mutation','Nonstop_Mutation','Silent','Splicing_Site',\
		'Frame_Shift_Del', 'Frame_Shift_Ins', 'In_Frame_Del', 'In_Frame_Ins']
	notgene_funcs = ['3\'Flank','5\'Flank','IGR']
	if type == 'all':
		return True
	if type == 'coding':
		return func in coding_funcs
	if type == 'non-synonymous':
		return func in nonsyn_funcs
	if type == 'exon':
		return func in exon_funcs
	if type == 'gene':
		return func not in notgene_funcs
	return True

if not os.path.exists(args.outdir):
	assert not os.system('mkdir %s'%args.outdir)
## user defined samples and genes
samples_use = []
if args.samplelist:
	if ',' in args.samplelist:
		samples_use = args.samplelist.split(',')
	else:
		samples_use = [each.split()[0] for each in open(args.samplelist).read().strip().split('\n')]

matrix = {}
mutation = {}
ids_maf = []
samples_maf = []

i = 1
for line in open(args.maf):
	if i == 1:
		i += 1
		continue
	array = line.strip().split('\t')
	if array[15] not in samples_use and args.samplelist:
		continue
	## INDEL filter
	if (not args.indel) and (array[9] == 'INS' or  array[9] == 'DEL' or  array[9] == 'INDEL'):
		continue
	## func filter
	if not filter_funcs(array[8], args.func):
		continue
	id = '%s:%s:%s>%s' % (array[4], array[5], array[10], array[12])
	if id not in ids_maf:
		ids_maf.append(id)
		matrix[id] = {}
		mutation[id] = {'ref':array[10], 'gene':array[0]}
	mutation[id][array[15]] = array[12]
	matrix[id][array[15]] = float(array[33])/(int(array[33])+int(array[32])) ## AF
	if array[15] not in samples_maf:
		samples_maf.append(array[15])

samples = samples_use or samples_maf
samples.insert(0,args.patient)

ids_germline = []
if args.germline:
	i = 1
	for line in open(args.germline):
		if line.startswith('##'):
			continue
		if i == 1:
			i += 1
			continue
		array = line.strip().split('\t')
		id = '%s:%s:%s>%s' % (array[args.chr], array[args.position], array[args.ref], array[args.alt])
		if id in mutation:
			continue
		ids_germline.append(id)
		mutation[id] = {'ref':array[args.ref], 'gene':array[args.gene]}
		mutation[id][args.patient] = array[args.alt]
		matrix[id] = {args.patient:1}
#		mutation[id][args.patient] = array[args.alt]
#		matrix[id] = {args.patient:1}
		for each in samples[1:]:
			mutation[id][each] = array[args.alt]
			matrix[id][each] = 0

prefix = os.path.join(args.outdir,args.patient+'.'+args.func)

matrix_cln = open(prefix+'.clean.matrix.txt','w')
matrix_cln.write('ID\t'+'\t'.join(samples)+'\n')
data = []

N = 0
if args.root_number:
	N = args.root_number

id_select = []
for id in ids_maf:
	line = [matrix[id].get(each,0) for each in samples]
	if len([each for each in line if each>0]) == 0:
		continue
	if not (args.germline and args.root_number):
		line[0] = int(len([each for each in line[1:] if each>0]) == len(line[1:]))
	data.append(line)
	matrix_cln.write('\t'.join([id]+[str(each) for each in line])+'\n')
	id_select.append(id)

for id in ids_germline:
	line = [matrix[id].get(each,0) for each in samples]
	data.append(line)
	matrix_cln.write('\t'.join([id]+[str(each) for each in line])+'\n')
	id_select.append(id)
	if args.root_number and N <= 0:
		break
	N = N -1
if args.root_number and N > 0:
	for n in range(N):
		id = 'G:%d'%n
		ref = ['A','T','C','G'][n%4]
		alt = ['A','T','C','G'][(n+1)%4]
		mutation[id] = {'ref':ref, 'gene':'NA'}
		mutation[id][args.patient] = alt
		matrix[id] = {args.patient:1}
		for each in samples[1:]:
			mutation[id][each] = ref
			matrix[id][each] = 0
		data.append([1]+[0]*(len(samples)-1))
		id_select.append(id)
		matrix_cln.write(id+'\t1\t'+'\t'.join(['0']*(len(samples)-1))+'\n')
matrix_cln.close()

## root
order_ids = np.array(id_select)
order_samples = np.array(samples)
d = np.array(data)
f = np.array((d>0).tolist(),dtype=np.int)

## add root
#root_stat = np.array([each.sum()==e.shape[1] for each in e], dtype=np.int)
#f = e.transpose().tolist()
#f.insert(0,root_stat)
#f = np.array(f).transpose()
cols = range(f.shape[1])
rows = range(f.shape[0])

## mutual exclude sort
for x in cols[::-1]:
	index = copy.deepcopy(rows)
	index.sort(key = lambda i : -f[i,x])
	f = f[index, ::]
	order_ids = order_ids[index]

stat_file = open(prefix+'.stat','w')
meg_file = open(prefix+'.nucl.meg','w')
meg_file.write('#mega\n!Title evolution;\n!Format DataType=DNA indel=-\n\n')
stat_file.write('\t%d\t%d\n' % (f.shape[1], f.shape[0]))
sample_mapping = {}

for i in np.arange(f.shape[1]):
	column = [str(each) for each in f[::,i].tolist()]
	sample_mapping['sample%d'%i] = order_samples[i]
	sample = 'sample%d                '%i
	stat_file.write('%s%s\n' %(sample[0:10], ''.join(column)))
	meg_file.write('#'+order_samples[i]+'\n')
	seq = ''.join([mutation[each].get(order_samples[i],mutation[each]['ref']) for each in order_ids])
	meg_file.write('\n'.join([seq[each*60:min((each+1)*60,len(seq))] for each in range(len(seq)/60+1)])+'\n')
#	meg_file.write(''.join([mutation[each].get(order_samples[i],mutation[each]['ref']) for each in order_ids])+'\n')
stat_file.close()
meg_file.close()

## phylip
open(os.path.join(args.outdir,'pars.params.'+args.func),'w').write('%s\nY\n'%(prefix+'.stat'))
phylip = '''
log=%s/log
cd %s && \\
cp Evolution/phylip-3.695/src/font1 fontfile && \\
if [ -f outtree ];then rm -f outtree; fi && \\
if [ -f outfile ];then rm -f outfile; fi && \\
if [ -f plotfile ];then rm -f plotfile; fi && \\
echo %s ... pars infer tree ... > $log
echo ........................................... >> $log
Evolution/phylip-3.695/exe/pars < pars.params.%s >> $log && \\
mv -f outfile %s.pars_out && \\
mv -f outtree %s.tree
#echo %s ... drawtree ... > $log
#echo ........................................... >> $log
#Evolution/phylip-3.695/exe/drawtree < drawtree.params.%s >> $log && \\
#mv -f plotfile %s.tree.pdf && \\
#convert %s.tree.pdf %s.tree.png
''' % (args.outdir, args.outdir, args.patient, args.func, prefix, prefix, args.patient, args.func, prefix, prefix, prefix)
open(os.path.join(args.outdir,args.func+'.createTree.sh'),'w').write(phylip)
assert not os.system('sh %s' % (os.path.join(args.outdir,args.func+'.createTree.sh')))

t = Tree(prefix+'.tree')
for node in t:
	node.name = sample_mapping.get(node.name,node.name)
t.write(format=1,outfile=prefix+'.tree.nk')

open(os.path.join(args.outdir,'drawtree.params.'+args.func),'w').write('%s\nY\n'%(prefix+'.tree.nk'))
drawtree = '''
cd %s && \
drawtree < drawtree.params.%s && \
mv -f plotfile %s.phylip.tree.pdf && \
convert %s.phylip.tree.pdf %s.phylip.tree.png
''' % (args.outdir, args.func, prefix, prefix, prefix)
open(os.path.join(args.outdir,args.func+'.drawTree.sh'),'w').write(drawtree)
assert not os.system('sh %s' % (os.path.join(args.outdir,args.func+'.drawTree.sh')))

t.name = 'node0'
t.set_outgroup(args.patient)

i = 1
for node in t.traverse("levelorder"):
	if node.name == "":
		node.name = "node%d"%i
		i += 1

node2labels = t.get_cached_content(store_attr="name")
drivergenes = []
if args.drivergene:
	drivergenes = [each.split()[0] for each in open(args.drivergene).read().strip().split('\n')]
branches = ['Germline','Germline-node1']
pat2branch = {'1'+'0'*(len(order_samples)-1):'Germline', '1'*len(order_samples):'Germline-node1'}
edge_muts = {'Germline':{'id':[],'driver':[]}, 'Germline-node1':{'id':[],'driver':[]}}

for node in t.iter_descendants("preorder"):
	if node.name == 'node0' or node.up.name == 'node0':
		continue
	pattern = ''.join([str(int(each in node2labels[t&node.name])) for each in order_samples])
	branches.append('%s-%s'%(node.up.name,node.name))
	pat2branch[pattern] = '%s-%s'%(node.up.name,node.name)
	edge_muts['%s-%s'%(node.up.name,node.name)] = {'id':[],'driver':[]}

branches.append('Out-of-tree')
pat2branch['0'*len(order_samples)] = 'Out-of-tree'
edge_muts['Out-of-tree'] = {'id':[],'driver':[]}

for i,id in enumerate(order_ids):
	pattern = ''.join(np.array(f[i],dtype=np.str))
	if pattern in pat2branch:
		edge_muts[pat2branch[pattern]]['id'].append(id)
		if mutation[id]['gene'] in drivergenes:
			edge_muts[pat2branch[pattern]]['driver'].append(id)
	else:
		edge_muts['Out-of-tree']['id'].append(id)
		if mutation[id]['gene'] in drivergenes:
			edge_muts['Out-of-tree']['driver'].append(id)

mutation_in_tree = open(prefix+'.tree_mutations.xls','w')
mutation_in_tree.write('Branches\t'+'\t'.join(['%s.Mutation\t%s.Gene'%(each,each) for each in branches])+'\n')
mutation_in_tree.write('Count\t'+'\t'.join(['%d\t%d' %(len(edge_muts[each]['id']),\
	len(set([mutation[x]['gene'] for x in edge_muts[each]['id']]))) for each in branches])+'\n')

driver_in_tree = open(prefix+'.tree_mutations.driver.xls','w')
driver_in_tree.write('Branches\t'+'\t'.join(['%s.Mutation\t%s.Gene'%(each,each) for each in branches])+'\n')
driver_in_tree.write('Count\t'+'\t'.join(['%d\t%d' %(len(edge_muts[each]['driver']),\
	len(set([mutation[x]['gene'] for x in edge_muts[each]['driver']]))) for each in branches])+'\n')

i = 0
while True:
	lines = ['']
	if i == 0:
		lines[0] = 'Mutations'
	flag = False
	for each in branches:
		if len(edge_muts[each]['id']) > i:
			lines.append(edge_muts[each]['id'][i])
			lines.append(mutation[edge_muts[each]['id'][i]]['gene'])
			flag = True
		else:
			lines.append('')
			lines.append('')
	i += 1
	if not flag:
		break
	mutation_in_tree.write('\t'.join(lines)+'\n')
mutation_in_tree.close()
i = 0
while True:
	lines = ['']
	if i == 0:
		lines[0] = 'Mutations'
	flag = False
	for each in branches:
		if len(edge_muts[each]['driver']) > i:
			lines.append(edge_muts[each]['driver'][i])
			lines.append(mutation[edge_muts[each]['driver'][i]]['gene'])
			flag = True
		else:
			lines.append('')
			lines.append('')
	i += 1
	if not flag:
		break
	driver_in_tree.write('\t'.join(lines)+'\n')
driver_in_tree.close()

ts = TreeStyle()
ts.show_leaf_name = True
ts.margin_bottom = 40
ts.margin_top = 40
ts.margin_left = 10
ts.margin_right = 10
ts.show_scale = False
ts.scale=0.3

style1 = NodeStyle()
style1['size'] = 4
style1['fgcolor'] = '#5500ff'
style1['hz_line_color'] = '#55aa00'
style1['vt_line_color'] = '#55aa00'
style1['hz_line_width'] = 2
style1['vt_line_width'] = 2
style2 = NodeStyle()
style2['size'] = 6
style2['fgcolor'] = '#0055ff'
style2['hz_line_color'] = '#55aa00'
style2['vt_line_color'] = '#55aa00'
style2['hz_line_width'] = 2
style2['vt_line_width'] = 2
t0 = t&'node1'
t0.add_face(TextFace('1'),column=0,position='branch-right')
t0.img_style = style1
for node in t0.iter_descendants("preorder"):
	if node.name.startswith('node'):
		(t0&node.name).add_face(TextFace(node.name.replace('node','')),column=0,position='branch-right')
		node.img_style = style1
	else:
		node.img_style = style2

t0.render(prefix+'.ete.tree.png',dpi=300,tree_style=ts,w=2000)
#t0.render(prefix+'.ete.tree.pdf',tree_style=ts,w=2000)
'''
t0 = t&'node1'
t0.render(prefix+'.ete.tree.png',dpi=300,w=1000,h=600)
t0.render(prefix+'.ete.tree.pdf',w=1000,h=600)
'''

Rscript='''
setwd('%s')
dat<-read.table("%s",head=T,row.names=1)
dat[dat>0] <- 1
dat$%s<-as.numeric(sapply(1:nrow(dat),function(x) all(dat[x,]>0)))
d<-data.matrix(dat)
h<-hclust(dist(t(d)))
png("%s",type="cairo-png")
plot(h,hang=-1,xlab="",ylab="",axes=FALSE,sub="")
dev.off()
'''%(args.outdir, prefix+'.clean.matrix.txt', args.patient, args.patient+'.tree.png')
#open(os.path.join(args.outdir,'hclust.R'),'w').write(Rscript)
#assert not os.system('Rscript %s' % (os.path.join(args.outdir,'hclust.R')))


from django.http import HttpResponseRedirect, HttpResponse
from django.core.mail import send_mail
from django.shortcuts import render
from contacto.forms import FormularioContactos

from django.template import loader
# Create your views here.

def contactos(request):
    #form=FormularioContactos()
    if request.method == 'POST':
        form=FormularioContactos(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['asunto'],
                cd['mensaje'],
                cd.get('email', 'noreply@example.com'),['siteowner@example.com'],
            )
            return HttpResponseRedirect('/contactos/gracias/')
    else:
        form = FormularioContactos(initial={'asunto':'adoro!'})
    template = loader.get_template('contactos/formulario_contactos.html')
    #return render(request, 'contactos/formulario_contactos.html', {'form': form})
    return HttpResponse(template.render({'form': form}, request))


# -*- coding: utf-8 -*-
"""Base controller to interact with the Sofa scene.
"""

__authors__ = "emenager, tnavez"
__contact__ = "etienne.menager@inria.fr, tanguy.navez@inria.fr"
__version__ = "1.0.0"
__copyright__ = "(c) 2020, Inria"
__date__ = "Jul 29 2022"

import Sofa

class BaseVisualizationController(Sofa.Core.Controller):
    def __init__(self, *args, **kwargs):
        """Classical initialization of a python class.

        Note:
        ----
            The kwargs argument must containe:
                - root: to recover the action Controller of the scene.
                - config: link to config
        """
        assert kwargs.get("name") == "VisualizationController"
        Sofa.Core.Controller.__init__(self, *args, **kwargs)
        self.root =  kwargs["root"]
        self.config = kwargs["config"]

        self.it = 0
        self.n_eq_dt = self.config.get_n_eq_dt()
        self.n_dt = self.config.get_n_dt()
        self.post_sim_n_eq_dt = self.config.get_post_sim_n_eq_dt()

        self.n_robot = self.config.n_robot

    # def lambda_c_from_displacement(act_disp, cont_disp, xc):
    #     W = self.root.Controller.get_compliance_matrice_in_constraint_space()
    #     dfree = self.root.Controller.get_dfree()

    #     n_constraint = len(act_disp) + len(cont_disp)
    #     n_act_constraint = len(act_disp)

    #     for i in range(self.n_robot):
    #         # Get mechanical matrices from SOFA
    #         Waa_i = W_t[i * n_constraint: i * n_constraint + n_act_constraint, i * n_constraint: i * n_constraint + n_act_constraint]
    #         Wca_i = W_t[i * n_constraint + n_act_constraint :(i + 1) * n_constraint,
    #                 i * n_constraint: i * n_constraint + n_act_constraint]
    #         Wcc_i = W_t[i * n_constraint + n_act_constraint: (i + 1) * n_constraint,
    #                 i * n_constraint + n_act_constraint: (i + 1) * n_constraint]

    #         dfree_a_i = dfree_t[i * n_constraint: i * n_constraint + n_act_constraint]
    #         dfree_c_i = dfree_t[i * n_constraint + n_act_constraint: (i + 1) * n_constraint]

    #         # Compute lambda_c from wanted actuation and contact displacements
    #         Wcc_i_inv = np.linalg.inv(Wcc_i)
    #         Waa_i_inv = np.linalg.inv(Waa_i)

    #         inter_1 = np.linalg(np.linalg(Wca_i, Waa_i_inv), np.array(act_disp) - dfree_a_i)
    #         inter_2 = np.array(cont_disp)
    #         lambda_c_i = np.linald(Wcc_i_inv, )

    def onAnimateBeginEvent(self, event):
        # Reach Equilibrium
        if self.it < self.n_eq_dt:
            n_vars = len(self.config.get_actuators_variables()) + len(self.config.get_contacts_variables())
            null_action = (n_vars*[0])*self.n_robot
            self.root.Controller.apply_actions(null_action)

        # Apply actions
        elif (self.it >= self.n_eq_dt) and (self.it < self.n_eq_dt + self.n_dt):

            if self.it == self.n_eq_dt:
                print("Equilibrium reached. Starting applying actuation")
            step = self.it - self.n_eq_dt
            init_actuation = [list(self.config.get_actuators_variables().values())[i][0] for i in range(len(list(self.config.get_actuators_variables().values())))]
            init_contact = [list(self.config.get_contacts_variables().values())[i][0] for i in range(len(list(self.config.get_contacts_variables().values())))]

            # Apply gradually actuation/contact forces
            interpolated_vars = []
            for robot in range(self.n_robot):
                interpolated_vars += [min((step + 1) * v/(self.n_dt), v) for v in init_actuation]
                interpolated_vars += [min((step + 1) * v/(self.n_dt), v) for v in init_contact]

            self.root.Controller.apply_actions(interpolated_vars)

        else:
            if self.it == self.n_eq_dt + self.n_dt:
                print("Action applied")

            if self.it == self.n_eq_dt + self.n_dt + self.post_sim_n_eq_dt:
                print("Equilibrium reached")

            init_vars = []
            for robot in range(self.n_robot):
                init_vars += [list(self.config.get_actuators_variables().values())[i][0] for i in range(len(list(self.config.get_actuators_variables().values())))]
                init_vars += [list(self.config.get_contacts_variables().values())[i][0] for i in range(len(list(self.config.get_contacts_variables().values())))]

            self.root.Controller.apply_actions(init_vars)

        self.it += 1


nums = '123456789'
def pandigital(str):
    """Check whether 'str' contains ALL of the chars in 'nums'"""
    return 0 not in [c in str for c in nums]

def multiples(num,n):
	tor = ""
	for i in range(1,n+1):
		tor += str(num*i)
	return tor

print(multiples(192,3))
print(pandigital(multiples(192,3)))

largest = 0
p = ""
n = 2
num = 0
going = True
while going:
	num += 1
	if n > 9:
			going = False
	p = multiples(num,n)
	if len(p) > 9:
		p = ""
		n += 1
		num = 1
	if(pandigital(p)):
		if int(p) > largest: largest = int(p)

print(largest)


# %%
import torch
import torch.nn as nn
import torch.optim as optim

import torchtext
from torchtext.data import Field, BucketIterator,  TabularDataset
from torchtext.data.functional import sentencepiece_tokenizer, load_sp_model
from pathlib import Path

import dill
import numpy as np

import os
import random
import re

# %%
# path where the model and training data (for vocabulary) is saved
# name of the model: "model.pt"
path = Path("model/sentencepiece/20200728-152255/english_pretraining_with_sp/")

INPUT = "Das Wetter ist sehr schön."

# %%

sp_deu = load_sp_model(str(path / "spm_deu.model"))
sp_nds = load_sp_model(str(path / "spm_nds.model"))

#%%

SRC = Field(use_vocab = False, tokenize = sp_deu.encode,
            init_token = sp_deu.bos_id(),
            eos_token = sp_deu.eos_id(),
            pad_token = sp_deu.pad_id(),
            batch_first = True
            )

TRG = Field(use_vocab = False, tokenize = sp_nds.encode,
            init_token = sp_nds.bos_id(),
            eos_token = sp_nds.eos_id(),
            pad_token = sp_nds.pad_id(),
            batch_first = True
            )
print(SRC)
# %%

class Encoder(nn.Module):
    def __init__(self,
                 input_dim,
                 hid_dim,
                 n_layers,
                 n_heads,
                 pf_dim,
                 dropout,
                 device,
                 max_length = 100):
        super().__init__()

        self.device = device

        self.tok_embedding = nn.Embedding(input_dim, hid_dim)
        self.pos_embedding = nn.Embedding(max_length, hid_dim)

        self.layers = nn.ModuleList([EncoderLayer(hid_dim,
                                                  n_heads,
                                                  pf_dim,
                                                  dropout,
                                                  device)
                                     for _ in range(n_layers)])

        self.dropout = nn.Dropout(dropout)

        self.scale = torch.sqrt(torch.FloatTensor([hid_dim])).to(device)

    def forward(self, src, src_mask):

        #src = [batch size, src len]
        #src_mask = [batch size, src len]

        batch_size = src.shape[0]
        src_len = src.shape[1]

        pos = torch.arange(0, src_len).unsqueeze(0).repeat(batch_size, 1).to(self.device)

        #pos = [batch size, src len]

        src = self.dropout((self.tok_embedding(src) * self.scale) + self.pos_embedding(pos))

        #src = [batch size, src len, hid dim]

        for layer in self.layers:
            src = layer(src, src_mask)

        #src = [batch size, src len, hid dim]

        return src

#
# %%
class EncoderLayer(nn.Module):
    def __init__(self,
                 hid_dim,
                 n_heads,
                 pf_dim,
                 dropout,
                 device):
        super().__init__()

        self.self_attn_layer_norm = nn.LayerNorm(hid_dim)
        self.ff_layer_norm = nn.LayerNorm(hid_dim)
        self.self_attention = MultiHeadAttentionLayer(hid_dim, n_heads, dropout, device)
        self.positionwise_feedforward = PositionwiseFeedforwardLayer(hid_dim,
                                                                     pf_dim,
                                                                     dropout)
        self.dropout = nn.Dropout(dropout)

    def forward(self, src, src_mask):

        #src = [batch size, src len, hid dim]
        #src_mask = [batch size, src len]

        #self attention
        _src, _ = self.self_attention(src, src, src, src_mask)

        #dropout, residual connection and layer norm
        src = self.self_attn_layer_norm(src + self.dropout(_src))

        #src = [batch size, src len, hid dim]

        #positionwise feedforward
        _src = self.positionwise_feedforward(src)

        #dropout, residual and layer norm
        src = self.ff_layer_norm(src + self.dropout(_src))

        #src = [batch size, src len, hid dim]

        return src

# %%
class MultiHeadAttentionLayer(nn.Module):
    def __init__(self, hid_dim, n_heads, dropout, device):
        super().__init__()

        assert hid_dim % n_heads == 0

        self.hid_dim = hid_dim
        self.n_heads = n_heads
        self.head_dim = hid_dim // n_heads

        self.fc_q = nn.Linear(hid_dim, hid_dim)
        self.fc_k = nn.Linear(hid_dim, hid_dim)
        self.fc_v = nn.Linear(hid_dim, hid_dim)

        self.fc_o = nn.Linear(hid_dim, hid_dim)

        self.dropout = nn.Dropout(dropout)

        self.scale = torch.sqrt(torch.FloatTensor([self.head_dim])).to(device)

    def forward(self, query, key, value, mask = None):

        batch_size = query.shape[0]

        #query = [batch size, query len, hid dim]
        #key = [batch size, key len, hid dim]
        #value = [batch size, value len, hid dim]

        Q = self.fc_q(query)
        K = self.fc_k(key)
        V = self.fc_v(value)

        #Q = [batch size, query len, hid dim]
        #K = [batch size, key len, hid dim]
        #V = [batch size, value len, hid dim]

        Q = Q.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
        K = K.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
        V = V.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)

        #Q = [batch size, n heads, query len, head dim]
        #K = [batch size, n heads, key len, head dim]
        #V = [batch size, n heads, value len, head dim]

        energy = torch.matmul(Q, K.permute(0, 1, 3, 2)) / self.scale

        #energy = [batch size, n heads, query len, key len]

        if mask is not None:
            energy = energy.masked_fill(mask == 0, -1e10)

        attention = torch.softmax(energy, dim = -1)

        #attention = [batch size, n heads, query len, key len]

        x = torch.matmul(self.dropout(attention), V)

        #x = [batch size, n heads, query len, head dim]

        x = x.permute(0, 2, 1, 3).contiguous()

        #x = [batch size, query len, n heads, head dim]

        x = x.view(batch_size, -1, self.hid_dim)

        #x = [batch size, query len, hid dim]

        x = self.fc_o(x)

        #x = [batch size, query len, hid dim]

        return x, attention

# %%
class PositionwiseFeedforwardLayer(nn.Module):
    def __init__(self, hid_dim, pf_dim, dropout):
        super().__init__()

        self.fc_1 = nn.Linear(hid_dim, pf_dim)
        self.fc_2 = nn.Linear(pf_dim, hid_dim)

        self.dropout = nn.Dropout(dropout)

    def forward(self, x):

        #x = [batch size, seq len, hid dim]

        x = self.dropout(torch.relu(self.fc_1(x)))

        #x = [batch size, seq len, pf dim]

        x = self.fc_2(x)

        #x = [batch size, seq len, hid dim]

        return x

# %%
class Decoder(nn.Module):
    def __init__(self,
                 output_dim,
                 hid_dim,
                 n_layers,
                 n_heads,
                 pf_dim,
                 dropout,
                 device,
                 max_length = 100):
        super().__init__()

        self.device = device

        self.tok_embedding = nn.Embedding(output_dim, hid_dim)
        self.pos_embedding = nn.Embedding(max_length, hid_dim)

        self.layers = nn.ModuleList([DecoderLayer(hid_dim,
                                                  n_heads,
                                                  pf_dim,
                                                  dropout,
                                                  device)
                                     for _ in range(n_layers)])

        self.fc_out = nn.Linear(hid_dim, output_dim)

        self.dropout = nn.Dropout(dropout)

        self.scale = torch.sqrt(torch.FloatTensor([hid_dim])).to(device)

    def forward(self, trg, enc_src, trg_mask, src_mask):

        #trg = [batch size, trg len]
        #enc_src = [batch size, src len, hid dim]
        #trg_mask = [batch size, trg len]
        #src_mask = [batch size, src len]

        batch_size = trg.shape[0]
        trg_len = trg.shape[1]

        pos = torch.arange(0, trg_len).unsqueeze(0).repeat(batch_size, 1).to(self.device)

        #pos = [batch size, trg len]

        trg = self.dropout((self.tok_embedding(trg) * self.scale) + self.pos_embedding(pos))

        #trg = [batch size, trg len, hid dim]

        for layer in self.layers:
            trg, attention = layer(trg, enc_src, trg_mask, src_mask)

        #trg = [batch size, trg len, hid dim]
        #attention = [batch size, n heads, trg len, src len]

        output = self.fc_out(trg)

        #output = [batch size, trg len, output dim]

        return output, attention

# %%
class DecoderLayer(nn.Module):
    def __init__(self,
                 hid_dim,
                 n_heads,
                 pf_dim,
                 dropout,
                 device):
        super().__init__()

        self.self_attn_layer_norm = nn.LayerNorm(hid_dim)
        self.enc_attn_layer_norm = nn.LayerNorm(hid_dim)
        self.ff_layer_norm = nn.LayerNorm(hid_dim)
        self.self_attention = MultiHeadAttentionLayer(hid_dim, n_heads, dropout, device)
        self.encoder_attention = MultiHeadAttentionLayer(hid_dim, n_heads, dropout, device)
        self.positionwise_feedforward = PositionwiseFeedforwardLayer(hid_dim,
                                                                     pf_dim,
                                                                     dropout)
        self.dropout = nn.Dropout(dropout)

    def forward(self, trg, enc_src, trg_mask, src_mask):

        #trg = [batch size, trg len, hid dim]
        #enc_src = [batch size, src len, hid dim]
        #trg_mask = [batch size, trg len]
        #src_mask = [batch size, src len]

        #self attention
        _trg, _ = self.self_attention(trg, trg, trg, trg_mask)

        #dropout, residual connection and layer norm
        trg = self.self_attn_layer_norm(trg + self.dropout(_trg))

        #trg = [batch size, trg len, hid dim]

        #encoder attention
        _trg, attention = self.encoder_attention(trg, enc_src, enc_src, src_mask)

        #dropout, residual connection and layer norm
        trg = self.enc_attn_layer_norm(trg + self.dropout(_trg))

        #trg = [batch size, trg len, hid dim]

        #positionwise feedforward
        _trg = self.positionwise_feedforward(trg)

        #dropout, residual and layer norm
        trg = self.ff_layer_norm(trg + self.dropout(_trg))

        #trg = [batch size, trg len, hid dim]
        #attention = [batch size, n heads, trg len, src len]

        return trg, attention

# %% [markdown]
# ### Seq2Seq
#

# %%
class Seq2Seq(nn.Module):
    def __init__(self,
                 encoder,
                 decoder,
                 src_pad_idx,
                 trg_pad_idx,
                 device):
        super().__init__()

        self.encoder = encoder
        self.decoder = decoder
        self.src_pad_idx = src_pad_idx
        self.trg_pad_idx = trg_pad_idx
        self.device = device

    def make_src_mask(self, src):

        #src = [batch size, src len]

        src_mask = (src != self.src_pad_idx).unsqueeze(1).unsqueeze(2)

        #src_mask = [batch size, 1, 1, src len]

        return src_mask

    def make_trg_mask(self, trg):

        #trg = [batch size, trg len]

        trg_pad_mask = (trg != self.trg_pad_idx).unsqueeze(1).unsqueeze(2)

        #trg_pad_mask = [batch size, 1, 1, trg len]

        trg_len = trg.shape[1]

        trg_sub_mask = torch.tril(torch.ones((trg_len, trg_len), device = self.device)).bool()

        #trg_sub_mask = [trg len, trg len]

        trg_mask = trg_pad_mask & trg_sub_mask

        #trg_mask = [batch size, 1, trg len, trg len]

        return trg_mask

    def forward(self, src, trg):

        #src = [batch size, src len]
        #trg = [batch size, trg len]

        src_mask = self.make_src_mask(src)
        trg_mask = self.make_trg_mask(trg)

        #src_mask = [batch size, 1, 1, src len]
        #trg_mask = [batch size, 1, trg len, trg len]

        enc_src = self.encoder(src, src_mask)

        #enc_src = [batch size, src len, hid dim]

        output, attention = self.decoder(trg, enc_src, trg_mask, src_mask)

        #output = [batch size, trg len, output dim]
        #attention = [batch size, n heads, trg len, src len]

        return output, attention

def evaluate(model, iterator, criterion):

    model.eval()

    epoch_loss = 0

    with torch.no_grad():

        for i, batch in enumerate(iterator):

            src = batch.src
            trg = batch.trg

            output, _ = model(src, trg[:,:-1])

            #output = [batch size, trg len - 1, output dim]
            #trg = [batch size, trg len]

            output_dim = output.shape[-1]

            output = output.contiguous().view(-1, output_dim)
            trg = trg[:,1:].contiguous().view(-1)

            #output = [batch size * trg len - 1, output dim]
            #trg = [batch size * trg len - 1]

            loss = criterion(output, trg)

            epoch_loss += loss.item()

    return epoch_loss / len(iterator)

# %%

def instantiate_objects(SRC,TRG, vocab_size=vocab_size):
    INPUT_DIM = vocab_size
    OUTPUT_DIM = vocab_size
    # hidden_dim was 256 before
    HID_DIM = 256
    ENC_LAYERS = 3
    DEC_LAYERS = 3
    ENC_HEADS = 8
    DEC_HEADS = 8
    ENC_PF_DIM = 512
    DEC_PF_DIM = 512
    ENC_DROPOUT = 0.1
    DEC_DROPOUT = 0.1

    enc = Encoder(INPUT_DIM,
                HID_DIM,
                ENC_LAYERS,
                ENC_HEADS,
                ENC_PF_DIM,
                ENC_DROPOUT,
                device)

    dec = Decoder(OUTPUT_DIM,
                HID_DIM,
                DEC_LAYERS,
                DEC_HEADS,
                DEC_PF_DIM,
                DEC_DROPOUT,
                device)
    return enc, dec

# %%
device = torch.device("cpu")

# %%

def translate_sentence(sentence, src_field, trg_field, model, device, max_len = 50):

    model.eval()

    if isinstance(sentence, str):
        tokens = sp_deu.encode(sentence)

    else:
        tokens = [token for token in sentence]

    if tokens[0] != src_field.init_token and tokens[-1] != src_field.eos_token:
        tokens = [src_field.init_token] + tokens + [src_field.eos_token]

    src_indexes = tokens

    src_tensor = torch.LongTensor(src_indexes).unsqueeze(0).to(device)

    src_mask = model.make_src_mask(src_tensor)

    with torch.no_grad():
        enc_src = model.encoder(src_tensor, src_mask)

    trg_indexes = [trg_field.init_token]

    for i in range(max_len):

        trg_tensor = torch.LongTensor(trg_indexes).unsqueeze(0).to(device)

        trg_mask = model.make_trg_mask(trg_tensor)

        with torch.no_grad():
            output, attention = model.decoder(trg_tensor, enc_src, trg_mask, src_mask)

        pred_token = output.argmax(2)[:,-1].item()

        trg_indexes.append(pred_token)

        if pred_token == trg_field.eos_token:
            break

    trg_tokens = sp_nds.decode(trg_indexes)

    return trg_tokens

# %%

enc , dec = instantiate_objects(SRC,TRG)

SRC_PAD_IDX = SRC.pad_token
TRG_PAD_IDX = TRG.pad_token

model = Seq2Seq(enc, dec, SRC_PAD_IDX, TRG_PAD_IDX, device).to(device)

criterion = nn.CrossEntropyLoss(ignore_index = TRG_PAD_IDX)

model.load_state_dict(torch.load(path / 'model.pt', map_location=torch.device(device)))

def translate_to_platt(input_string):
    translation = translate_sentence(input_string, SRC, TRG, model, device)

    #translation = ''.join(translation[:-1]).replace('▁', ' ')
    return translation

# %%
import pandas as pd
untranslated = pd.read_csv("preprocessed_data/tatoeba/untranslated_sentences.csv", index_col = 0)
untranslated = untranslated[["deu"]]
def get_length(df):
    df_output = df.copy()
    # split by a simple tokenizer which uses regex and return the length of tokens
    df_output.deu = df_output.deu.str.split(r"[\s.,;:?!-\"\']+")
    return df_output.applymap(len)

def get_range(df, start, end):
    df_length = get_length(df)
    df_length = df_length[df_length.deu.ge(start) & df_length.deu.le(end)]

    return df.loc[df_length.index,:]
untranslated = get_range(untranslated,1,30)
#%%
sentence = untranslated["deu"].sample().item()
print(sentence)

print(translate_to_platt(sentence))

# %%
ai_comparison = False
if ai_comparison:
    ai_translation = untranslated[["deu"]].sample(50, random_state=42)
    ai_translation["ai_nds"] = untranslated["deu"].sample(50, random_state=42).apply(translate_to_platt)
    ai_translation.to_csv("preprocessed_data/tatoeba/ai_translations_model_pre_training_en_sentencepiece.csv")

# %%

# %%

#sentence = "Ich habe während der Mittagspause ein wenig geschlafen, weil ich so müde war."
sentence = "Nachdem wir die Erdbeeren gepflückt haben, backen wir einen leckeren Kuchen."
#sentence = "Das Wetter wird übermorgen sehr schön."
sentence = "Mein Handy muss aufgeladen werden."
translate_to_platt(sp_deu.encode(sentence))

# %%
if __name__ == "__main__":
    print(translate_to_platt(INPUT))