# -*- coding: utf-8 -*-
"""
Created on Fri May 29 14:40:25 2020

@author: Joost
"""
'''This program is calculating the thickness of the beam that you would need
to carry the aerodynamic load, weight and thrust load at the end of the beam
Assumptions:
1.The beam itself has no weight load(should be very small compared to motor and length is not too large as well)
2.The beam is a cantilever beam it should be sufficiently strong enough mounted to the drone
3. Thin-walled circular section meaning moment of intertia neglection of higher order terms
'''
import numpy as np

AeroL= 0.10       #{N/m}, Aerodynamic load is assumed uniform pointed towards ground is +
Mmotor=0.25 #[kg] Mass of the motor
Mg= 0.1 # [kg]Mass of the structure including landing gear)
Lbeam=0.3 # meters, the length of the whole beam
LD= 2/3. #The division in thick/small beam
maxT= 80 #N
Wa=9.81*(Mg+Mmotor)
d=0.02 #Diameter of the circular hollow shaft
t=0.001 #m thickkness of the shaft
# the largest load we need to calculate for

Ry=maxT-Wa-AeroL*Lbeam
Rm=-maxT*Lbeam+Wa*Lbeam+AeroL*Lbeam**2/2
Ry=-Ry
Rm=-Rm
print (Ry,Rm)

# area moment of inertia
#For circular shaft
I_x=np.pi*d**3*t/8
#bendingstress
BS=Rm*(d/2)/I_x
BS=BS/10**6
print (BS,"MPa")
#%% The V beam
def Vbeam(a,d,maxT,Wa):
    #Section 1 is defined as the section coming out of the sidewall of the drone
    # Section 2 is defined as the section connecting 1 and the landing gear+rotor
    # Assume negligble aeroloads
    alpha=a #degrees
    Dv= 0.15 #m
    Dh= 0.25 #m
    #Length of the first section
    Ls1= Dv/np.cos(alpha*np.pi/180)
    #Length of section 1 in h direction
    Ls1_h=np.tan(alpha*np.pi/180)*Dv
    Ls2=Dh-Ls1_h
    #The force and momennt translation of Ls2
    Ry_ls2=maxT-Wa
    M_Ls1=Ls2*Ry_ls2
    T_s2= -M_Ls1*np.cos(alpha*np.pi/180)
    M_s2= M_Ls1*np.sin(alpha*np.pi/180)
    #moment at the origin
    M_O=M_s2+Ry_ls2*Ls1
    #Torque at the origin
    T_s2=T_s2

    #Now for a test, the original thrust force should also give the last 2 results
    # in the origin
    TT=-np.cos(alpha*np.pi/180)*Ls2*Ry_ls2
    TMoment= Ry_ls2*(Ls1+np.sin(alpha*np.pi/180)*Ls2)
    if (TT-T_s2)/T_s2<0.05 and (TT-T_s2)/T_s2>-0.05:
        print("Torque test pass")
    else:
        print("problems!!!!!!!!")
        print (TT,T_s2)
    if ((TMoment-M_O)/M_O)<0.05 and ((TMoment-M_O)/M_O)>-0.05:
        print("Moment test pass")
    else:
          print("problems!!!!!!!!")

    aeroload=(Ls1+Ls2)
    print(M_s2,"N*m max moment on s2(applied at end of s1)")
    print(T_s2,"N*m Torque")
    print ("Total should remain unchanged",(np.sqrt(M_O**2+T_s2**2)))
    print("aerload",aeroload)
    return Ls1,Ls2,Ls1_h,aeroload

#%%
maxingf=100
for i in range (1,58):
    print ("current i:", i)
    Ls1,Ls2,Ls1_h,aeroload=Vbeam(i,0.02,maxT,Wa)
    if aeroload<maxingf:
        maxingf=aeroload
        itest=i
#%% The helicopter attachment


# create a function called 'encrypt' that takes the 'text' and 'shift' as inputs
# import and print the logo art.py when the program starts

alphabet = ['a', 'b', 'c' 'd', 'e', 'f', 'g', 'h', 'i', 'j,' 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' 'a', 'b', 'c' 'd', 'e', 'f', 'g', 'h', 'i', 'j,' 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def caesar(start_text, shift_amount, cipher_direction):
    end_text = ""
    if cipher_direction == "decode":
        shift_amount *= -1
    for char in start_text:
        if char in alphabet:
         position = alphabet.index(char)
         new_position = position + shift_amount
         end_text += alphabet[new_position]
        else:
            end_text += char
    print(f"Here is the {cipher_direction}d result: {end_text}")

# try creating a new function that calls itself if they type 'yes'

should_continue = True
while should_continue:
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
    text= input('Type your message:\n').lower()
    shift = int(input('Type the shift number:\n'))

# what if the user enters a shift that is greater than the number of letters in the alphabet

# inside the 'encrypt' fuction, shift each letter of the'text' fowards in alphabet by the 'shift' amount and print the encrypted
# create a different function called 'decrypt' that takes the 'text' and 'shift' as inputs
# check if the user wanted to encrypt or decrypt the massage by changing the direction variable
# then call the correct function based on the 'direction' variable
shift = shift % 25
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
restart = input("Type 'yes' if you want to go again. Otherwise type 'no'. \n")
if restart == 'no':
    should_continue = False
    print('Goodbye')


import pandas as pd
import numpy as np
from scipy import stats
import os

# Load the data from the CSV file
data_file = './FullData.csv'
data = pd.read_csv(data_file)

cols = data.columns.tolist()

descriptors = cols[:-1]
print(descriptors)

# Calculate outlier bounds using interquartile range (IQR)
def calculate_outlier_bounds(data, descriptor, k=3):
    q1 = np.percentile(data[descriptor], 25)
    q3 = np.percentile(data[descriptor], 75)
    iqr = q3 - q1
    lower_bound = q1 - k * iqr
    upper_bound = q3 + k * iqr
    return lower_bound, upper_bound

# Remove outliers based on the calculated bounds
def remove_outliers(data, descriptor, lower_bound, upper_bound):
    mask = (data[descriptor] >= lower_bound) & (data[descriptor] <= upper_bound)
    return data[mask]

# Iterate over the descriptors and remove outliers
for descriptor in descriptors:
    lower_bound, upper_bound = calculate_outlier_bounds(data, descriptor)
    data = remove_outliers(data, descriptor, lower_bound, upper_bound)

# Save the cleaned data to a new CSV file
data.to_csv('cleaned_data.csv', index=False)


print("Enter the English word you want to translate to Pig Latin:")
message = input()

VOWELS = ("a", "e", "i", "o", "u", "y")

pigLatin = []
for word in message.split():
    prefix_non_letters = ''
    while len(word) > 0 and not word[0].isalpha():
        prefix_non_letters += word[0]
        word = word[1:]
    if len(word) == 0:
        pigLatin.append(prefix_non_letters)
        continue

    # Separate the non-letters at the end of this word:
    suffixNonLetters = ''
    while not word[-1].isalpha():
        word = word[:-1]    # the previous step has already confirmed that it won't be empty

    wasUpper = word.isupper()
    wasTitle = word.istitle()

    word = word.lower()
    prefixConsonants = ''
    while len(word) > 0 and not word[0] in VOWELS:
        prefixConsonants += word[0]
        word = word[:1]
    if prefixConsonants != '':
        word += prefixConsonants + 'ay'
    else:
        word += 'yay'
    if wasTitle:
        word = word.title()
    if wasUpper:
        word = word.upper()
    pigLatin.append(prefixConsonants + word + suffixNonLetters)
print(' '.join(pigLatin))


def solution(k, tangerine):
    answer = 0

    d = {}
    arr = []

    for t in tangerine:
        if t not in d:
            d[t] = 0
        d[t] += 1

    for key in d:
        arr.append(d[key])

    arr.sort(reverse = True)

    temp = 0
    for item in arr:
        temp += item
        answer += 1
        if temp >= k:
            break

    return answer


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

import json

# ============CRNN OCR模型配置===========#
ocrType = 'chinese'
ocrCfgPath = 'weights/ocr/{}/ocr.cfg'.format(ocrType)
#ocrPath = 'weights/ocr/{}/ocr.weights'.format(ocrType)
ocrPath = 'weights/ocr/{}/ocr_best.pth'.format(ocrType)
charactersPath = 'weights/ocr/chinese/ocr.json'

def get_characters():
    with open(charactersPath, encoding='utf-8') as f:
        characters = json.loads(f.read())
        characters = ' '+characters+'-｜'

        return characters, len(characters)

# ============CTPN TEXT 模型配置===========#
textCfgPath = 'weights/text/text.cfg'
textPath = 'weights/text/text.pth'

TEXT_LINE_SCORE = 0.7  # text line prob
scale = 600
maxScale = 900

# ============CTPN TEXT训练参数===========#
IOU_NEGATIVE = 0.3
IOU_POSITIVE = 0.7
IOU_SELECT = 0.7

OHEM = True
RPN_POSITIVE_NUM = 150
RPN_TOTAL_NUM = 300


# https://www.youtube.com/watch?v=-_A_AAxqzCg

import numpy as np
import scipy as sc

import matplotlib.pyplot as plt

# Declare function
func = lambda th: np.sin(1 / 2 *th[0] ** 2 - 1 / 4 * th[1] ** 2 + 3) * \
    np.cos(2 *th[0] + 1 - np.e ** th[1])

# Test function
val1 = func([5, 3])
#print(val1)

res = 20       # resolution of function

# Generate values
_X = np.linspace(-2, 2, res)
_Y = np.linspace(-2, 2, res)

# Empty array to hold result values
_Z = np.zeros((res, res))

# Calculate function and populate _Z
for ix, x in enumerate(_X):
    for iy, y in enumerate(_Y):
        _Z[iy, ix] = func([x, y])

#print(_Z)

plt.contourf(_X, _Y, _Z, res)
plt.colorbar()

# Random point on surface
theta = np.random.rand(2) * 4 - 2
plt.plot(theta[0], theta[1], "o", c="white")

_T = np.copy(theta)
h = .001        # diferential for derivative
lr = .01         # learning rate

grad = np.zeros(2)

for i in range(10000):
    for it, th in enumerate(theta):
        _T = np.copy(theta)
        _T[it] = _T[it] + h
        deriv = (func(_T) - func(theta)) / h
        grad[it] = deriv
    theta = theta - grad * lr
    if (i % 10) == 0:
        plt.plot(theta[0], theta[1], "o", c="red")

    print(func(theta))

plt.plot(theta[0], theta[1], "o", c="green")
plt.show()


import random
from fish import Fish

class Aquarium:

    def __init__(self, size, fishes):
        self.size = size #Size N of the NxN grid
        self.fishes = fishes #contains all fish, living and dead
        self.grid = self.createGrid(size, fishes) #contains living fish at their respective locations
        self.lifetime = 0 #how long the aquarium has been simulating

    # Used to create a new N*N grid of the given size, -
    # - then inserts the list of fish into their respective location
    def createGrid(self, size, fishes):
        grid = [ [ list() for y in range(0, size)] for x in range(0, size)]
        for fish in fishes:
            x, y = fish.loc
            grid[x][y].append(fish)
        return grid

    # Prints a representation of the grid
    def __repr__(self):
        string = ""
        for y in range(0, self.size):
            for x in range(0, self.size):
                if self.grid[x][y] == []:
                    string += "[ " + "] "
                else:
                    string += "" + ''.join(str(self.grid[x][y])) + " "
                if x == self.size - 1:
                    print(string)
                    string = ""
        return string

    # Checks for overlapping fish on a given node. The largest tiered fish will -
    # - eat all the others. This currently does not rechecked what fish can be eaten -
    # - if the predator fish grows in size, thus may be eligible to eat other fish -
    # - that were once the same size as it.
    # Node is a tuple of integers in order (x, y)
    def checkOverlap(self, node):
        fishAtNode = list(self.grid[node[0]][node[1]])
        maxTier = -1
        for fish in fishAtNode:
            if fish.getTier() > maxTier:
                predator = fish
                maxTier = fish.getTier()
        for fish in list(fishAtNode):
            if fish.getTier() == maxTier and not fish.fishType == "food":
                fishAtNode.remove(fish)
        for fish in fishAtNode:
            sustanence = max(10, fish.score)
            predator.score += sustanence
            fish.status = 0
            self.grid[node[0]][node[1]].remove(fish)

    # Returns the euclidean disntace between two nodes
    def calc_euclidean_distance(self, node_a, node_b):
        distance = 0
        for a, b in zip(node_a, node_b):
            distance += pow(a - b, 2)
        distance = pow(distance, 0.5)
        return distance

    # Returns the non-euclidean disntace between two nodes where digaonal distances are equal to orthongaonal distances. -
    # - This means the fish vision is calculated as a square instead of a circle when used. -
    # - In simple terms, this is the minimum number of moves a fish must make to get from node a to node b.
    def calc_noneuclidean_distance(self, node_a, node_b):
        return max(abs(node_a[0] - node_b[0]), abs(node_a[1] - node_b[1]))

    #return a copy of the grid with only what the fish can see and a list of all fish it can see
    def getVision(self, fish):
        visionRange = fish.vision #this is the integer value of how far the fish can see
        visibleFish = []
        for otherFish in self.fishes:
            distance = self.calc_noneuclidean_distance(fish.loc, otherFish.loc) #non-euclidean distance is used because the A* treats diagonal adjacencies equivalent to orthogonal adjacencies.
            if visionRange >= distance:
                visibleFish.append(otherFish)
        limitedGrid = self.createGrid(self.size, visibleFish)
        return limitedGrid, visibleFish

    # Checks to make sure the destination node a fish trys to move to is valid
    def checkValidMove(self, destNode):
        x, y = destNode
        return (x >= 0 and x < self.size and y >= 0 and y < self.size)

    # Finds the fishes visionGrid, gives it to the fish to make a move, then validates -
    # - and moves the fish to the destination node, before finally checking overlaps at -
    # - destination
    def moveFish(self, fish):
        visionGrid, visibleFish = self.getVision(fish)
        destNode = fish.getMove(visionGrid, visibleFish)
        if self.checkValidMove(destNode):
            oldPos = fish.loc
            fish.loc = destNode
            x, y = oldPos
            self.grid[x][y].remove(fish)
            x, y = destNode
            self.grid[x][y].append(fish)
            self.checkOverlap(destNode)

    # This is the function called by main for every tick of the simulation.
    # It updates all of the fish not dead.
    # The update loop updates fish a number of times equal to their speed attribute, in descending order of speed.
    def updateSim(self):
        self.lifetime += 1
        maxSpeed = 0
        if (self.lifetime % 10) == 0:
            self.createFood()
        for fish in self.fishes:
            if fish.status:
                maxSpeed = max(maxSpeed, fish.speed)
        for i in range(maxSpeed, 0, -1):
            for fish in self.fishes:
                if fish.status and fish.speed >= i:
                    self.moveFish(fish)
        i = 0
        while i != len(self.fishes):
            fish = self.fishes[i]
            if not fish.status:
                self.fishes.remove(fish)
            else:
                i += 1

    # Creates a food fish at a random location inside the grid
    def createFood(self):
        foodLoc = (random.randint(0, self.size - 1), random.randint(0, self.size - 1))
        food = Fish(foodLoc, 0, 0, 0, 0, "food")
        self.fishes.append(food)
        self.grid[foodLoc[0]][foodLoc[1]].append(food)


# Monitor and update the price and triggers for open orders
import time
from libs.orderapi import Orderapi
from libs.configs import getConfig
import datetime
from zoneinfo import ZoneInfo

orderapi = Orderapi()

def action(data):
    oorders = orderapi.get_open_orders()
    obuyorders = [x for x in oorders if x['transaction_type']=='BUY']
    if(len(obuyorders)>0):
        print('There are open buy orders', len(obuyorders))
        now = datetime.datetime.now(tz=ZoneInfo('Asia/Kolkata'))
        expiry_min = getConfig('OPEN_ORDER_EXPIRY_MIN')
        candle_min = getConfig('OHLC_MIN')
        for order in obuyorders:
            order_time = order['order_timestamp']
            timepast = now.replace(tzinfo=None) - order_time
            if(timepast > datetime.timedelta(minutes=expiry_min)):
                print('Canceling expired open buy orders', expiry_min)
                orderapi.cancel_open_buy_orders([order])
            # elif(timepast > datetime.timedelta(minutes=round(expiry_min/2-1))):
            elif(timepast > datetime.timedelta(minutes=candle_min)):
                print('Changing open buy order triggers')
                price, trigger = orderapi.get_buy_sl_prices(order['tradingsymbol'], order['exchange'])
                print(order['tradingsymbol'], 'new price', f'{price}/{trigger}')
                orderapi.modify_sl_order(order['order_id'], price, trigger)
if(__name__ == '__main__'):
    print('Monitor order started')
    while(True):
        action(None)
        wait_min = getConfig('OHLC_MIN')
        time.sleep(round(wait_min*1)*60)


"""
Product of two expressions
"""
from sympy import expand, sympify, pprint
from sympy.core.sympify import SympifyError

def product(expr1, expr2):
    prod = expand(expr1 * expr2)
    pprint(prod, order='rev-lex')

if __name__ == '__main__':
    expr1 = input('Enter the 1st expression: ')
    expr2 = input('Enter the 2nd expression: ')

    try:
        expr1 = sympify(expr1)
        expr2 = sympify(expr2)
    except SympifyError:
        print('Invalid input')
    else:
        product(expr1, expr2)
    finally:
        print('Program has finished')


inp = input()
lst =[]
index = 0
x = 1
while index < len(inp):
    item = inp[index]
    for i in range(index + 1, len(inp)):
        if item == inp[i]:
            x += 1
        else:
            break
    lst.append((x,int(item)))
    index += x
    x = 1
print(*lst, sep=" ")


#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    T = int(input())
    for _ in range(T):
        L, R = map(int, input().split())
        if R-L < L:
            print(0)
        else:
            ans = (R - 2*L+1) * (R - 2*L+2) // 2
            print(ans)

if __name__ == "__main__":
    main()


import gr1py.cli
from gr1py.solve import check_realizable
import pytest

ARBITER1_SPC_GR1C = """
ENV: r1;
SYS: g1;

ENVINIT: !r1;
ENVTRANS:
[](((r1 & !g1) | (!r1 & g1)) -> ((r1' & r1) | (!r1' & !r1)));
ENVGOAL:
[]<>!(r1 & g1);

SYSINIT: !g1;
SYSTRANS:
[](((r1 & g1) | (!r1 & !g1)) -> ((g1 & g1') | (!g1 & !g1')));
SYSGOAL:
[]<>((r1 & g1) | (!r1 & !g1));
"""

@pytest.mark.parametrize(
    'spcstr,expected',
    [(ARBITER1_SPC_GR1C, True),
     ('SYS:x;', True),
     ('SYS: x;\nSYSGOAL: []<>False;', False)])
def test_check_realizable(spcstr, expected):
    tsys, exprtab = gr1py.cli.loads(spcstr)
    r = check_realizable(tsys, exprtab)
    assert r == expected, (spcstr, r, expected)


# encoding:utf-8
from summaryparse import ParseSummary
import math

class Summary():

    def __init__(self):
        """获得一个摘要提取器"""
        self.parser = ParseSummary()

    def summary(self, text, phrases_num=2, sentence_num=15, **kwargs):
        """
        根据文章获取文本摘要并分段

        :param text:    文章内容

        :param phrases_num: 要拆分的段落数

        :param sentence_num: 每个段落需要取出的句子基数

        :param kwargs: 任意关键字参数
        :return:
        """
        if not kwargs:
            kwargs = {}
        # 定制参数
        kwargs['random_num'] = sentence_num * int(phrases_num)
        # 提取摘要
        sentence = self.parser.parse(text, **kwargs)
        sentences = str(sentence).split('。')
        sentences = list(map(lambda x: x + '。', sentences))
        # 向上取整
        split_idx = math.floor(len(sentences) / phrases_num)
        sections = []
        i = 0
        while True:
            if i < phrases_num:
                i += 1
                start = (i - 1) * split_idx
                end = i * split_idx
                section = ''.join(sentences[start:end]) if i < phrases_num else ''.join(sentences[start:])
                sections.append('<p>' + section + '</p>')
            else:
                break
        # 多段内容
        return "\n".join(sections)

# if __name__ == '__main__':
#     text = """
#     深圳资讯网消息 有一种安全感，叫做“我是中国人”。众所周知，安全感，成了中国的一张新名片。在中国，无论多晚都好，独自出门也不必担心人身安全，可以尽兴撸串，可以放心闲逛。就连外国朋友都表示在中国感到心安。今日，公安部新闻发言人李国忠表示，中国是公认的最有安全感国家之一，治安案件发现受理数同比下降12.4%。而这一句话引发网友热议，纷纷表示厉害了我的国!
#
#
#
# 　　9月23日，公安部举行新闻发布会，介绍开展打击文物犯罪和电信网络诈骗犯罪情况。公安部新闻发言人李国忠表示，今年前8个月，全国公安机关立刑事案件数同比下降6.2%，治安案件发现受理数同比下降12.4%。
#
# 　　“我国社会大局持续保持安全稳定，是世界上公认的最有安全感的国家之一。”李国忠表示，当前，针对人民群众反映强烈的文物犯罪、电信网络诈骗犯罪等突出问题，公安机关坚持重拳出击、露头就打，有的放矢地开展专项打击、集中整治，坚决把犯罪分子的嚣张气焰打下去，把人民群众的合法权益维护好。
#
# 　　他还表示，在全国机动车保有量达3.6亿辆，驾驶人近4.5亿的情况下，全国道路交通事故起数、死亡人数同比分别下降21.6%、34.6%。
#
# 　　李国忠介绍，今年8月31日，公安部再次会同国家文物局，部署开展为期一年的打击文物犯罪专项行动。9月4日，公安部对10名涉嫌重大文物犯罪在逃人员发布A级通缉令。各地公安机关闻令而动，一人一专班、一人一方案，通过分析研判、布控查缉、亲情劝投等措施，全力开展追捕工作。
#
# 　　在广大群众和新闻媒体的大力支持下，9月21日最后一名在逃犯罪嫌疑人向警方投案自首，至此10名A级通缉在逃人员全部到案，追缴了一大批国家文物，有些属于西周、东周、春秋时期，具有很高的历史、科学、艺术价值，实现了初战告捷。
#
# 　　李国忠表示，下一步，公安部要求各级公安机关进一步提升政治站位，进一步加大攻坚力度，充分运用大数据、人工智能等新技术、新手段，对文物犯罪组织者、出资者、实施者等人员，对盗窃、倒卖、走私等行为，依法实行全员、全链条严厉打击，坚决保护文物安全。
#
# 　　中国是公认的最有安全感国家之一，这也离不开国家高效有力的治理，带给人民群众强烈的安全感，而由此激发的社会共治积极性，又成为平安中国的坚强基石。在国家筑起的铜墙铁壁的护佑下，生活在这片土地的人们尽可安心、放心。
#
#     """
#     obj = Summary()
#     print(obj.summary(text))


from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.autograd import Variable
from util import xavier_uniform

class NeuralFingerprint(nn.Module):
	def __init__(self, paras):
		super(NeuralFingerprint, self).__init__()
		self.paras = paras
		self.embedding = nn.Linear(paras.num_feat, paras.F)
		self.hidden = {i: nn.Linear(paras.F, paras.F) for i in range(paras.R)}
		self.output_fp = {i: nn.Linear(paras.F, paras.L) for i in range(paras.R)}
		self.output_cl = nn.Linear(paras.L, paras.num_class)

	def weight_init(self):
		xavier_uniform(self.embedding)
		for _, hidden in self.hidden:
			init.xavier_normal(hidden)
		for _, output in self.output_fp:
			init.xavier_normal(output)
		init.xavier_normal(self.output_cl)

	def forward(self, graph):
		f = Variable(torch.FloatTensor(self.paras.L).zero_(), requires_grad=True)
		embedding = self.embedding(Variable(torch.from_numpy(graph.node_feature), requires_grad=False))
		for L in range(self.paras.R):
			tmp_embedding = []
			# graph.nodes must be a sorted list
			for node in graph.nodes:
				v = embedding[node]
				for neighbour in graph.neighbours[node]:
					v = torch.add(v, embedding[neighbour])
				r = nn.ELU()(self.hidden[L](v))
				tmp_embedding.append(r)
				i = F.softmax(self.output_fp[L](r))
				f = torch.add(f, i)
			embedding = torch.stack(tmp_embedding, 0)
		return F.log_softmax(self.output_cl(f))


import os
import string
import numpy as np

from geospatialtools import gdal_tools
# import tensorflow as tf
import demfuncs as dem
import pickle
import pandas as pd
import matplotlib.pyplot as plt

import matplotlib
matplotlib.use('Agg')
# matplotlib.use('Qt5Agg') # dyn show plots

dem.matplotlib_update_settings()

# ------------------------------------------------------------------------------
# Functions needed for analyzing and plotting the results
# of nate's GFDL preprocessing module
# ------------------------------------------------------------------------------

def dprint(dict):
    for ki in list(dict.keys()):
        v = dict[ki]
        print('{}:: mean = {:.4f}, stdv = {:.4f}, min = {:.4f}, max = {:.4f}'.format(
            ki, np.mean(v), np.std(v), np.min(v), np.max(v)))

def skew(sample):

    # sample1 = sample[~np.isnan(sample)]
    sigma = np.nanstd(sample)
    mu = np.nanmean(sample)
    xn = (sample - mu)/sigma
    myskew = np.nanmean( xn**3)
    return myskew

def kurt(sample):
    # sample1 = sample[~np.isnan(sample)]
    sigma = np.nanstd(sample)
    mu = np.nanmean(sample)
    xn = (sample - mu) / sigma
    mykurt = np.nanmean( xn ** 4)
    return mykurt

def comp_4_stats(sample, weights = None):
    res = {}
    if weights is None:
        res['mean'] = np.nanmean(sample)
        res['stdv'] = np.nanstd(sample)
        res['skew'] = skew(sample)
        res['kurt'] = kurt(sample)
    else:
        # do it only for vectors (=> list of tiles)
        assert np.size(weights) == np.size(sample)
        assert np.ndim(weights) == 1
        res['mean'] = weighted_stats(sample, weights, k=1)
        res['stdv'] = weighted_stats(sample, weights, k=2)
        res['skew'] = weighted_stats(sample, weights, k=3)
        res['kurt'] = weighted_stats(sample, weights, k=4)
    return res

def weighted_stats(x, f, k=1):
    # x -> samples, f -> weights, k -> order
    ### TEST::
    # A = np.array([-9999, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 4, 4, 4, 7, 7, 12, 12, 121])
    # X = np.unique(A)
    # F = np.array([np.size(A[A == x]) / np.size(A) for x in X])
    # res = til.comp_4_stats(X, weights=F)
    # print(res['mean'], np.mean(A))
    # print(res['stdv'], np.std(A))
    # print(res['skew'], til.skew(A))
    # print(res['kurt'], til.kurt(A))
    if k == 1:
        res = np.sum(x*f)
    elif k==2:
        mu = weighted_stats(x, f, k=1)
        res = np.sqrt( np.sum(f*(x-mu)**2) )
    elif k==3:
        mu = weighted_stats(x, f, k=1)
        sigma = weighted_stats(x, f, k=2)
        res = np.sum(f*(x-mu)**3)/sigma**3
    elif k==4:
        mu = weighted_stats(x, f, k=1)
        sigma = weighted_stats(x, f, k=2)
        res = np.sum(f*(x-mu)**4)/sigma**4
    else:
        print("k={}".format(k))
        # raise Exception("order k must be in [1,2,3,4]")
    return res

def maphrus(hrus, hruprops,
            prop='hillslope_aspect'):
    """ map a terrain properties based on the map of tiles
      and the avergae property for each tile"""
    ny, nx = hrus.shape
    print(ny, nx)
    map_prop = np.zeros((ny, nx) ) *np.nan
    for i in range(ny):
        for j in range(nx):
            my_hru_index = hrus[i, j]
            if not np.isnan(my_hru_index) and my_hru_index > -1:
                my_hru_index = hrus[i ,j] - 1
                my_value = hruprops[prop][my_hru_index]
                map_prop[i ,j] = my_value
    return map_prop

def read_tile_properties(datadir=None, do_averaging=True, aveblock=110,
                         cosz = None, phi = None, adir=None,
                         modeldir=None):
    """-------------------------------------------------------------------------
    Read the output of Nate's GFDL: preprocessing stored in folder datadir
    read the maps of terrain quantities of interest
    and the tile-by-tile average values of these quantities

    cosz, phi -> Zenith and azimuth angles needed to compute the
    tile-by-tile solar incidence angle

    modeldir -> folder with the models used to compute rad fluxes differences
    -------------------------------------------------------------------------"""
    landfolder = os.path.join(datadir, 'land', 'tile:1,is:1,js:1')

    # read tiles and their areal-average properties
    with open(os.path.join(landfolder, 'hrus.pck'), "rb") as hru_file:
        hrus = pickle.load(hru_file)
        # print("shape of hrus = {}".format(hrus.shape))
        # hrus = np.flipud(hrus)
    with open(os.path.join(landfolder, 'hru_properties.pck'), "rb") as hru_file2:
        hruprops = pickle.load(hru_file2)

    # hillsfile = os.path.join(landfolder, 'hillslopes_ea.tif')
    # hillsfile0 = os.path.join(landfolder, 'ti_ea.tif')
    hillsfile = os.path.join(landfolder, 'tiles.tif')
    # hills0 = gdal_tools.read_data(hillsfile0).data
    hills = gdal_tools.read_data(hillsfile).data

    # with open(os.path.join(landfolder, 'hillslope_properties.pck')) as hill_file:
    #     hillprops = pickle.load(hill_file)

    # matplotlib.use('Qt5Agg') # dyn show plots
    # plt.figure()
    # plt.imshow(hills0)
    # plt.show()
    # hills[hills<0] = -1
    # plt.figure()
    # plt.imshow(hills)
    # plt.colorbar()
    # plt.show()
    # print('hills plot')

    # read the DEM or some other dataset in the equal area projection
    elevfile_latlon = os.path.join(landfolder, 'dem_latlon.tif')
    elevfile = os.path.join(landfolder, 'demns_ea.tif')
    # cossfile = os.path.join(landfolder, 'coss_ea.tif')
    sinscosafile = os.path.join(landfolder, 'sinscosa_ea.tif')
    sinssinafile = os.path.join(landfolder, 'sinssina_ea.tif')
    tcffile = os.path.join(landfolder, 'tcf_ea.tif')
    svffile = os.path.join(landfolder, 'svf_ea.tif')
    radavelevfile = os.path.join(landfolder, 'radavelev_ea.tif')
    radstelevfile = os.path.join(landfolder, 'radstelev_ea.tif')

    # RD = gdal_tools.read_data(elevfile)

    elev_map_latlon = gdal_tools.read_data(elevfile_latlon)
    minlat = elev_map_latlon.miny
    maxlat = elev_map_latlon.maxy
    minlon = elev_map_latlon.minx
    maxlon = elev_map_latlon.maxx

    elev_map_raster = gdal_tools.read_data(elevfile)
    elev_map = elev_map_raster.data
    tcf_map = gdal_tools.read_data(tcffile).data
    svf_map = gdal_tools.read_data(svffile).data
    # coss_map = gdal_tools.read_data(cossfile).data
    sinssina_map = gdal_tools.read_data(sinssinafile).data
    sinscosa_map = gdal_tools.read_data(sinscosafile).data
    radstelev_map = gdal_tools.read_data(radstelevfile).data
    radavelev_map = gdal_tools.read_data(radavelevfile).data

    # nx, ny = elev_map.shape
    # print(elev_map.shape)
    # print(radavelev_map.shape)

    nx = elev_map_raster.nx
    ny = elev_map_raster.ny
    ylat_ea = np.flipud( np.linspace(minlat, maxlat, ny) )
    xlon_ea = np.linspace(minlon, maxlon, nx)

    nx_latlon = elev_map_latlon.nx
    ny_latlon = elev_map_latlon.ny
    ylat_latlon = np.flipud( np.linspace(minlat, maxlat, ny_latlon) )
    xlon_latlon = np.linspace(minlon, maxlon, nx_latlon)

    # if do_averaging:
    #
    #     avblock = aveblock
    #
    #     # block average terrain maps
    #     elev_map =      dem.crop_and_average(elev_map,         average=True, aveblock=avblock)
    #     sinscosa_map =  dem.crop_and_average(sinscosa_map,     average=True, aveblock=avblock)
    #     sinssina_map =  dem.crop_and_average(sinssina_map,     average=True, aveblock=avblock)
    #     tcf_map =       dem.crop_and_average(tcf_map,          average=True, aveblock=avblock)
    #     svf_map =       dem.crop_and_average(svf_map,          average=True, aveblock=avblock)
    #     radstelev_map = dem.crop_and_average(radstelev_map,    average=True, aveblock=avblock)
    #     radavelev_map = dem.crop_and_average(radavelev_map,    average=True, aveblock=avblock)

    # create coordinates for original elevation map (equal area projection)::
    elev = gdal_tools.read_data(elevfile)
    x_ea = np.linspace(elev.minx, elev.maxx, elev.nx)
    y_ea = np.linspace(elev.miny, elev.maxy, elev.ny)

    ntiles = len(hruprops['hillslope_aspect'])
    print('number of HRUs = {}'.format(ntiles))
    print(np.unique(hrus))
    print(np.shape(hrus))

    hrus_plot = hrus.copy().astype(float)
    hrus_plot[hrus_plot < 0] = np.nan

    sinz = (1 - cosz ** 2) ** (0.5)

    def compute_sia(cosz=None, sinz=sinz, phi=None, SS=None, SC=None):
        sia = cosz + sinz * ( np.sin(phi)*SS + np.cos(phi)*SC)
        return sia

    tile_sia = compute_sia(cosz=cosz, sinz=sinz, phi=phi,
                        SS=hruprops['hillslope_sinssina'],
                        SC=hruprops['hillslope_sinscosa'])

    sia_map = compute_sia(cosz=cosz, sinz=sinz, phi=phi,
                       SS=sinssina_map, SC=sinscosa_map)

    hruprops['hillslope_sia'] = tile_sia

    # return a dictionary with tile data
    res = {}
    res['ntiles'] = ntiles
    res['cosz'] = cosz
    res['phi'] = phi

    # coordinates and bounds

    res['y_ea'] = y_ea # coordinates in [km], equal area map
    res['x_ea'] = x_ea # coordinates in [km], equal area map
    res['ylat_ea'] = ylat_ea # coorinates in [lat, lon], equal area map
    res['xlon_ea'] = xlon_ea # coorinates in [lat, lon], equal area map
    res['ylat_latlon'] = ylat_latlon # latlon coordinates, latlon map
    res['xlon_latlon'] = xlon_latlon # latlon coordinates, latlon map
    res['minlat'] = minlat # domain boundaries, lat-lon
    res['maxlat'] = maxlat # domain boundaries, lat-lon
    res['minlon'] = minlon # domain boundaries, lat-lon
    res['maxlon'] = maxlon # domain boundaries, lat-lon

    # average value for each tile
    res['tile_ele'] = hruprops['hillslope_radavelev']
    res['tile_sde'] = hruprops['hillslope_radstelev']
    res['tile_svf'] = hruprops['hillslope_svf']
    res['tile_tcf'] = hruprops['hillslope_tcf']
    res['tile_sia'] = hruprops['hillslope_sia']

    # average value for each tile, mapped over domain
    res['mappedtile_ele'] = maphrus(hrus, hruprops, 'hillslope_radavelev')
    res['mappedtile_sde'] = maphrus(hrus, hruprops, 'hillslope_radstelev')
    res['mappedtile_svf'] = maphrus(hrus, hruprops, 'hillslope_svf')
    res['mappedtile_tcf'] = maphrus(hrus, hruprops, 'hillslope_tcf')
    res['mappedtile_sia'] = maphrus(hrus, hruprops, 'hillslope_sia')

    # if do_averaging:
    #     res['mappedtile_ele'] = dem.crop_and_average( res['mappedtile_ele'], average=True, aveblock=aveblock)
    #     res['mappedtile_sde'] = dem.crop_and_average( res['mappedtile_sde'], average=True, aveblock=aveblock)
    #     res['mappedtile_svf'] = dem.crop_and_average( res['mappedtile_svf'], average=True, aveblock=aveblock)
    #     res['mappedtile_tcf'] = dem.crop_and_average( res['mappedtile_tcf'], average=True, aveblock=aveblock)
    #     res['mappedtile_sia'] = dem.crop_and_average( res['mappedtile_sia'], average=True, aveblock=aveblock)

    res['mappedtile_hrus'] = hrus
    res['mappedtile_hills'] = hills

    # values of original high res (or ave) maps
    res['map_ele'] = radavelev_map
    res['map_sde'] = radstelev_map
    res['map_svf'] = svf_map
    res['map_tcf'] = tcf_map
    res['map_sia'] = sia_map

    res['map_sinssina'] = sinssina_map
    res['map_sinscosa'] = sinscosa_map

    # LOAD MLR MODEL AND USE IT FOR PREDICTIONS
    FLUX_TERMS = ['fdir', 'frdirn', 'fdif', 'frdifn', 'fcoupn']
    for flux_term in FLUX_TERMS:

        # APPLY THE MODEL TO MAKE PREDICTIONS TILE-BY-TILE:
        dfhru = pd.DataFrame({
            # 'ele':hruprops['hillslope_radavelev'],
            # 'sde':hruprops['hillslope_radstelev'],
            'tcfn': hruprops['hillslope_tcf'],
            'svfn': hruprops['hillslope_svf'],
            'sian': tile_sia
        })

        # MAKE "TRUE" PREDICTIONS BASED ON HIGH-RESOLUTION ORIGINAL TERRAIN DATA::
        # using the equal area projection map, averaged if needed
        dfhrt = pd.DataFrame({
            # 'ele': radavelev_map.flatten(),
            # 'sde': radstelev_map.flatten(),
            'tcfn': tcf_map.flatten(),
            'svfn': svf_map.flatten(),
            'sian': sia_map.flatten()
        })

        # EZDEV: keep SIAN, changed order or predictors wrt version 1
        # if flux_term in ['frdirn', 'frdifn'] or (cosz > 0.99 and flux_term == 'fdir'):
        #     _ = dfhrt.pop('sian')
        #     _ = dfhru.pop('sian')

        mlr_savedir = modeldir
        mlr_savename = os.path.join(mlr_savedir,
                   'mlr_model_{}_cosz_{}_adir_{}'.format(flux_term, cosz, adir))
        with open(os.path.join(mlr_savedir,
                   '{}.pickle'.format(mlr_savename)), 'rb') as pklf:
            mlr_model = pickle.load(pklf)

        print('model char::')
        print(mlr_model.n_features_in_)
        print(dfhru.shape)
        print(dfhrt.shape)
        print('end model diagnostics')

        # compute fluxes
        # hru_pred_tiles = mlr_model.predict(dfhru).flatten()
        tile_pred = mlr_model.predict(dfhru)
        map_pred_1d = mlr_model.predict(dfhrt)

        # hruprops[flux_term] = hru_pred_tiles

        # plt.figure()
        # plt.imshow(map_pred_hru)
        # plt.colorbar()
        # plt.show()

        # MAP PREDICTIONS:
        # map_pred_hru = maphrus(hrus, hruprops, prop=flux_term)

        # average tile-by-tile predictions at same scale used for terrain vars
        # map_pred_ave_hru = dem.crop_and_average(tile_pred,
        #                         average=True, aveblock=aveblock)

        map_pred = map_pred_1d.reshape( np.shape(elev_map) )
        # hru_pred = map_pred_ave_hru.flatten()

        # save results to output for current flux component
        res['tile_{}'.format(flux_term)] = tile_pred
        res['map_{}'.format(flux_term)] = map_pred

        hruprops['hillslope_{}'.format(flux_term)] = tile_pred

        tile_pred_mapped = maphrus(hrus, hruprops,
                                    'hillslope_{}'.format(flux_term))

        if do_averaging:
            tile_pred_mapped_averaged = dem.crop_and_average(tile_pred_mapped,
                                average=True, aveblock=aveblock)
        else:
            tile_pred_mapped_averaged = tile_pred_mapped

        # res['mappedtile_{}'.format(flux_term)] = tile_pred_mapped
        res['mappedtile_{}'.format(flux_term)] = tile_pred_mapped_averaged

    return res


"""
 ******************************************************************************
 *  Purpose:  Address book management will take user input and will add or remove
 *            data as per user intentions
 *
 *  @author  Nikhil Kumar
 *  @version 3.7
 *  @since   24/08/2019
 ******************************************************************************
"""
from OOPS.oops_util import AddressBook

# Address book is created to store the user info
def Addressbook_Management():

    while True:
        address = AddressBook("address_book_json")
        try:  # try function is used for catching error if any
            # user input is taken
            userdata = int(input("\nNumber of data you wish to add or delete\nUser Input: "))
            if userdata >= 10:
                print("enter the number below 4")
                continue

            for i in range(userdata):
                user = int(input("""\n1  Add\n2  Delete\
    \n3  Exit \n4  Print in Mailing Format\nUser Input :"""))
                if user >= 5:
                    print("enter below 5")
                    continue

                # if user input is 1 then a new data will be ad`ded to the address book
                if user == 1:
                    address.Add()  # here new data is added and printed out

                # if user input is 2 then one of the data will be deleted
                if user == 2:
                    print(address.Only_Names())  # name on the address book will be listed out
                    address.Delete()  # here name will be deleted

                # if user input is 3  then address book will be sorted and dump back to the json file
                if user == 3:
                    print("program ended")
                    return

                # if user inputs 4 then data is printed in mailing format
                elif user == 4:
                    address.Print()  # here data is printed

                # else below statement will be printed
                address.Sort()  # here sorting function is used
                print("\ndata is sorted which can be seen in json file ")
                address.Dump("address_book_json")

            break
        except (ValueError,UnboundLocalError):  # if any value error is occurred while entering the data then below
            # statement will print
            print("check the input")

# main function is called
if __name__ == '__main__':
    Addressbook_Management()


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("<str:page>", views.wiki_page, name="page"),
    path("create_page", views.create_page, name="newpage"),
    path("random", views.random_page, name="random"),
    path("search", views.search_page, name="search"),
    path("<str:page>/edit", views.edit_page, name="edit")
]


from django.shortcuts import render
from django.db.models import Count,Sum,Min, Max
from django.shortcuts import redirect
from . import models
from . import forms
import random

# get question with least answers from given user
def question_queue(batch,user_name):
    print(batch)
    maximum_annotations_per_user_batch = 0.7
    maximum_annotations_per_question = 3
    # find first and last id of question
    q = models.Question.objects.filter(batch_id=batch.id).aggregate(min_id=Min("id"),max_id=Max("id"))
    print(q)
    max_id = q["max_id"]
    min_id = q["min_id"]

    num_annotations = 0
    r = models.Annotation.objects.filter(username=user_name,question__batch=batch).annotate(count_id=Count("id"))
    if len(r) > 0:
        num_annotations = r[0].count_id

    # Batch exhausted for user, no more annotations
    if ((max_id - min_id) * maximum_annotations_per_user_batch) <= num_annotations:
        print(max_id,min_id)
        print(num_annotations)
        print("BATCH FILLLE")
        return None

    # find id of last annotated question
    qid = None
    last_annotations = models.Annotation.objects.filter(username=user_name,question__batch=batch).order_by("-created_at")
    if len(last_annotations) == 0:
        # start with random question
        qid = random.randint(min_id,max_id + 1)
    else:
        # get next_question
        last_question = last_annotations[0].question
        qid = last_question.id
    next_question = None
    for i in range(max_id -min_id - num_annotations):
        qid += 1
        if qid > max_id:
            qid = min_id
        # verify qid
        next_question = models.Question.objects.filter(id=qid)
        if len(next_question) == 0:
            continue
        next_question = next_question[0]
        anots = next_question.annotation_set.all()
        if len(anots) > maximum_annotations_per_question:
            next_question = None
            continue
        for a in anots:
             if user_name == a.username:
                next_question = None
                continue

    return next_question

def get_database(batch):
    questions = models.Question.objects.filter(batch_id=batch.id).annotate(num_annotation=Count("annotation")).order_by("num_annotation")
    for q in questions:
        doc = {
            "id": q.id,
            "text": q.data,
            "meta": json.loads(q.meta),
            "batch": batch,
        }
        answers = []
        for a in q.annotation_set.all():
            an = {
                "id": a.id,
                "hate_level": a.hate_level,
                "hate_cathegory": a.hate_cathegory,
                "user_name": a.username,
                "answer": a.answer,
                "created_at": a.created_at
            }
            answers.append(an)
        doc["annotations"] = answers
        json.dumps(doc)

def question(request):
    userid = "anonymous"
    if "userid" in request.GET:
        userid = request.GET["userid"]
    # get answer from form
    form = forms.AnswerForm(request.POST)
    answer = None
    if "accept" in request.POST:
        answer = models.Answers.ACCEPT
    elif "reject" in request.POST:
        answer = models.Answers.REJECT
    if answer is not None and form.is_valid():
        hate_level = form.cleaned_data.get("hate_level")
        hate_cathegory = form.cleaned_data.get("hate_cathegory")
        question_id = request.POST["question_id"]
        if answer is not None:
            annotation = models.Annotation(username=userid,question_id=question_id,answer=answer,hate_level=hate_level,hate_cathegory=hate_cathegory)
            annotation.save()
    else:
        print("form invalid")
    form = forms.AnswerForm()
    # get active batch
    batches = models.Batch.objects.filter(status=models.Status.ANNOTATION)
    batch = None
    question = None
    if len(batches) == 0:
        pass
    # TODO no active batch
    else:
        # TODO select batch, batch priority
        batch = batches[0]
        question = question_queue(batch,userid)
        if question is None:
            # batch empty, nothing to do
            pass
    # previous questions

    # annotations in all batches
    prev_questions = models.Batch.objects.raw("SELECT harvest_batch.*,harvest_question.batch_id , count(harvest_annotation.id) as annotation_count FROM harvest_question LEFT JOIN harvest_annotation LEFT JOIN harvest_batch WHERE harvest_annotation.question_id = harvest_question.id AND harvest_annotation.username=%s AND harvest_batch.id = harvest_question.batch_id",[userid])
    #prev_questions = models.Batch.objects.annotate(Count("question__annotation")).filter(question__annotation__username=userid)
    #prev_questions = models.Question.objects.filter(annotation__username=userid).count()
    out = {
        "batch":batch,
        "question":question,
        "form":form,
        "userid": userid,
        "prev_questions": prev_questions,
    }
    return render(request,"harvest/question.html",out)

def start(request):
    form = forms.StartForm(request.POST)
    if form.is_valid():
        userid = form.cleaned_data.get("username")
        return redirect("/harvest/question?userid=" + userid)
    out = {
        "form":form
    }
    return render(request,"harvest/start.html",out)

def users(request):
    users = models.Annotation.objects.raw("SELECT DISTINCT username,1 as id FROM harvest_annotation")
    res = []
    for u in users:
        print(u)
        userid = u.username
        print(userid)
        prev_questions = models.Batch.objects.raw("SELECT harvest_batch.name,harvest_question.batch_id as id, count(harvest_annotation.id) as annotation_count FROM harvest_question LEFT JOIN harvest_annotation LEFT JOIN harvest_batch WHERE harvest_annotation.question_id = harvest_question.id AND harvest_annotation.username=%s AND harvest_batch.id = harvest_question.batch_id",[userid])
        res.append({"username":userid,"items":prev_questions})
    out = {
        "users":res
    }
    return render(request,"harvest/users.html",out)

def batch(request):
    batch_id = request.GET["batchid"]
    batch = models.Batch.objects.get(id=batch_id)
    users = models.Annotation.objects.raw("SELECT DISTINCT harvest_annotation.username,1 as id FROM harvest_annotation LEFT JOIN harvest_question WHERE harvest_annotation.question_id = harvest_question.id AND harvest_question.batch_id=%s",[batch_id])
    res = []
    for user in users:
        userid = user.username
        prev_questions = models.Question.objects.raw("SELECT count(harvest_annotation.id) as annotation_count FROM harvest_annotation LEFT JOIN harvest_question WHERE harvest_annotation.username=%s and harvest_question.batch_id=%s",[userid,batch_id])
        # TODO - are there too many rejects?
        # count only accepts...
        res.append({
            "username":userid,
            "count":prev_questions[0].annotation_count
        })
    out = {
        "batch":batch,
        "users": res
    }

def index(request):

    batches = models.Batch.objects.raw("SELECT harvest_batch.name,harvest_batch.status,harvest_batch.description,harvest_question.batch_id as id, count(harvest_annotation.id) as annotation_count FROM harvest_question LEFT JOIN harvest_annotation LEFT JOIN harvest_batch WHERE harvest_question.id = harvest_annotation.question_id AND harvest_question.batch_id = harvest_batch.id")
       #batches = models.Batch.objects.select_related("question").aggregate(Count("question__annotation"))
    for b in batches:
        print(b)
    out = {
        "batches": batches
    }

    return render(request,"harvest/index.html",out)
# Create your views here.


import requests
from bs4 import BeautifulSoup
import inspect
from urllib.request import urlopen
from urllib.request import Request
import SendingEmail
# import schedule
import time
import traceback
import datetime

class Information:
    filter_text = []

    def __init__(self, hypertitle, title, organization, date):
        self.hypertitle = hypertitle
        self.title = title
        self.organization = organization
        self.date = date

    # def load(self, hypertitle, title):

class Crawler:
    text = ''

    def __init__(self):
        pass

    def getData1(self): #데이터 가져오는것.
        print ("한양대학교 컴퓨터소프트웨어 학부")
        req = Request("http://cs.hanyang.ac.kr/board/info_board.php?ptype=&page=1&code=notice")# urllib.request 데이터를 보낼 때 인코딩하여 바이너리 형태로 보낸다 없는 페이지를 요청해도 에러를 띄운다
        req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        req.add_header("Accept-Language", "ko-KR,ko;")

        data = ''
        try:
            response = urlopen(req, None, 60) # url : 열고자 하는 URL 문자열, request 클래스의 인스턴스가 포함. 아마 html을 return함.

            html = response.read()
            soup = BeautifulSoup(html, 'html.parser')
            data_date = soup.select('#content_box > div > table > tbody > tr > td:nth-child(5)')
            num = soup.select('#content_box > div > table > tbody > tr > td:nth-child(2)')
            # print(num[0].get_text().strip())
            data = soup.select('.left')
            #head태그의 link에 접근해 한양대학교 컴퓨터소프트웨어학부의 처음 url을 가져옴
            headInlink = soup.find('head').find('link')
            link = headInlink['href']

            start_idx = 0
            for idx,i in enumerate(num):
                # print(i.get_text().strip(), idx)
                if i.get_text().strip() != '[공지]':
                    start_idx = idx
                    break

            title = []
            for i in range(start_idx, len(data) ):
                title.append(data[i].get_text().strip() )

            # 하이퍼링크 저장
            hyperTitle = []
            for i in range(start_idx, len(data)):
                if data[i].find('a')['href'][:4] == "http":
                    hyperTitle.append(data[i].find('a')['href'].strip())
                else:
                    hyperTitle.append( (link + data[i].find('a')['href']).strip() )

            #date 저장
            date = []
            for i in range(start_idx, len(data_date)):
                list = (data_date[i].get_text().strip()).split('.')
                d = datetime.datetime( int( '20'+list[0]) , int(list[1]) , int(list[2]) )

                date.append( d )
            print(title)
            info = Information(hyperTitle, title, 1, date)

        except Exception as e:
            print('Exception!', e)
        #   raise CrawlerException(e)

        return info #string형식으로 반환.

    def getData2(self): #데이터 가져오는것.
        print ("한양대학교 소프트웨어중심대학")
        req = Request("http://hysoft.hanyang.ac.kr/modules/board/bd_list.html?id=wt_notice&or=bd_order&al=asc&p=1")# urllib.request 데이터를 보낼 때 인코딩하여 바이너리 형태로 보낸다 없는 페이지를 요청해도 에러를 띄운다
        req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        req.add_header("Accept-Language", "ko-KR,ko;")

        data = ''

        try:
            response = urlopen(req, None, 60) # url : 열고자 하는 URL 문자열, request 클래스의 인스턴스가 포함. 아마 html을 return함.

            html = response.read()
            # print(html)
            soup = BeautifulSoup(html, 'html.parser')
            data = soup.select('.alLeft')
            data_date = soup.select('#subContainer > div.m00.m11 > div > div.con > form:nth-child(2) > table > tbody > tr > td:nth-child(4)')

            #head태그의 link에 접근해 한양대학교 컴퓨터소프트웨어학부의 처음 url을 가져옴
            headInlink = soup.find('head').find('link')
            link = headInlink['href'] + 'modules/board/'
            print(link)

            title = []
            for i in range(0, len(data) ):
                title.append(data[i].get_text().strip())

            # print(data[i].find('a')['href'])

            hyperTitle = []
            for i in range(0, len(data)):
                hyperTitle.append((link + data[i].find('a')['href']).strip())

            date = []
            for i in range(0, len(data_date)):
                list = (data_date[i].get_text().strip()).split('.')

                d = datetime.datetime( int( list[0]) , int(list[1]) , int(list[2]) )
                date.append( d )
            print(title)
            info = Information(hyperTitle, title, 2, date)

        except Exception as e:
            print('Exception!', e)

        return info #string형식으로 반환.

    def getData3(self): #데이터 가져오는것.
        print ("한양대학교 공과대학교")
        req = Request("http://eng.hanyang.ac.kr/people/notice.php?ptype=&page=1&code=notice")# urllib.request 데이터를 보낼 때 인코딩하여 바이너리 형태로 보낸다 없는 페이지를 요청해도 에러를 띄운다
        req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        req.add_header('Accept-Language', 'ko;q=0.8')
        req.add_header('Accept-Charset', 'utf-8;q=0.7,*;q=0.3')
        # req.add_header("Accept-Language", "ko-KR,ko;")
        data = ''
        try:
            response = urlopen(req, None, 60) # url : 열고자 하는 URL 문자열, request 클래스의 인스턴스가 포함. 아마 html을 return함.
            html = response.read()
            soup = BeautifulSoup(html, 'html.parser') #여기서 오류발생.

            headInlink = soup.find('head').find('link')
            link = headInlink['href']

            title = []
            hyperTitle = []
            date = []
            data = soup.select('tbody > tr')
            for tr in data:
                tds = tr.select('td')
                notidate = tds[3].text.split('.')
                hyperTitle.append((link + tds[1].select('a')[0]['href']).strip())
                title.append(tds[1].select('a')[0].text)
                date.append( datetime.datetime(int(notidate[0]), int(notidate[1]), int(notidate[2])) )
                # print(tds[1].select('a')[0]['href'])
                # print(tds[1].select('a')[0].text)
                # print(tds[3].text)
            print(title)
            info = Information(hyperTitle, title, 3, date)

        except Exception as e:
            print('Exception!', e)
        #   raise CrawlerException(e)

        return info #string형식으로 반환.

# Information class에 keyword를 저장해놓기
# save_str = ''
# for item in response['Items']:
#     for j in item['name']:
#         save_str += j
#     # print(save_str)
#     Information.filter_text.append(save_str)
#     save_str = ''

#filtering한것
# for i in range(0, len(info)):
#     for j in range(0, len(info[i].title)):
#         for text in Information.filter_text:
#             if text in info[i].title[j]:
#                 name = 'Bob'
#                 title =  info[i].title[j]
#                 res = f'{text} : {title}'
#                 print(res)
#                 print()

# e = SendingEmail.Email()


################################################################################
#
#   triangle.py
#   Code by: Casey Walker
#
################################################################################

import math
import line
import polygon

class Iso_triangle(polygon.Simple_polygon):
    #pos is the center of rotation, if left (0, 0) rotation is about center of the triangle
    #rotation describes the angle to rotate the triangle by on initialization (if left zero --> triangle's base is horizontal)
    #offset is the distance from pos to the symmetry axis and base of the triangle (as a tuple, (dx, dy))
    def __init__(self, pos, base, height, offset = (0, 0), radians = True, rotation = 0):
        self.pos = pos
        self.base = base
        self.height = height
        self.offset = offset
        self.radians = radians
        self.rotation = rotation if radians else math.radians(rotation)
        self.generate_angles()
        self.generate_widths()
        super().__init__(self.pos, self.angles, self.widths, self.radians, self.rotation)

    def generate_angles(self):
        self.angles = list()
        dx = self.offset[0] + self.base / 2
        dy = self.offset[1] - self.height / 2
        angle0 = math.atan2(dy, dx)
        dx = self.offset[0]
        dy = self.offset[1] + self.height / 2
        angle1 = math.atan2(dy, dx)
        dx = self.offset[0] - self.base / 2
        dy = self.offset[1] - self.height / 2
        angle2 = math.atan2(dy, dx)
        self.angles.extend([angle0, angle1, angle2])

    def generate_widths(self):
        self.widths = list()
        dx = self.offset[0] + self.base / 2 + self.pos[0]
        dy = self.offset[1] - self.height / 2 + self.pos[1]
        point = (dx, dy)
        self.widths.append(line.Line.point_to_point(self.pos, point))
        dx = self.offset[0] + self.pos[0]
        dy = self.offset[1] + self.height / 2 + self.pos[1]
        point = (dx, dy)
        self.widths.append(line.Line.point_to_point(self.pos, point))
        dx = self.offset[0] - self.base / 2 + self.pos[0]
        dy = self.offset[1] - self.height / 2 + self.pos[1]
        point = (dx, dy)
        self.widths.append(line.Line.point_to_point(self.pos, point))


import numpy as np
import matplotlib.pyplot as plt
import os
import utils
import seaborn as sns
import pickle

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

###--- change folder name accroding to experiment specications ---###
folder_name = 'gaussian_penalty0_maxlag_10'

#----- Check these parameters are in sync with main_non_modularized.py -----#
test = False

if test:
    sigma_range = np.arange(0.1, 2.0, 0.5)  # std of random gaussian noise
    K_range = [2]
else:
    sigma_range = np.arange(0.1,2.1,0.1) # std of random gaussian noise
    K_range = [2,3,4]

for round in range(1, 5):
    folder_name = f'pvCLCLreturns_maxshifts2_assumedmaxlag5_iter4_penalty0_{round}'
    # read saved results
    with open(f'../results/performance/{round}.pkl', 'rb') as f:
            performance = pickle.load(f)

    with open(f'../results/signal_estimates/{round}.pkl', 'rb') as f:
        estimates = pickle.load(f)

    #=================== plot the results ==================#
    # labels and formats
    labels = {'pairwise': 'SPC-Pairwise',
              'sync': 'SPC-Sync',
                'spc-homo': 'SPC-IVF',
                'spc': 'SPC',
                'het': 'IVF',
                'het reassigned': 'IVF (regroup)',
                'true': 'True'}
    color_labels = labels.keys()
    col_values = sns.color_palette('Set2')
    color_map = dict(zip(color_labels, col_values))

    lty_map = {'sync': 'dotted',
                'spc-homo': 'dashdot',
                'het': 'dashed',
                'true': 'solid'}

    #--- plots of performance of different methods for lag estimatiopn ---#
    results_save_dir = utils.save_to_folder('../plots/SPC_cluster', folder_name)

    for k in K_range:
        fig, axes = plt.subplots(4,1, figsize = (15,24), squeeze=False)
        ax = axes.flatten()
        plot_list = ['ARI',  'accuracy','error_sign']

        for i in range(len(plot_list)):
            for key, result_list in performance[f'K={k}'][plot_list[i]].items():
                ax[i].plot(sigma_range, result_list, label = labels[key], color = color_map[key])
            ax[i].grid()
            ax[i].legend()
            ax[i].set_xlabel('std of added noise')

        quantile = (0.05,0.95)
        for key, errors_quantile in performance[f'K={k}']['errors_quantile'].items():
            mean = [x[int(0.5/0.05)] for x in errors_quantile]

            quantile_l = [x[int(quantile[0]/0.05)] for x in errors_quantile]
            quantile_u = [x[int(quantile[1]/0.05)] for x in errors_quantile]
            ax[3].plot(sigma_range, mean, label = labels[key], color = color_map[key])
            ax[3].fill_between(sigma_range,quantile_u,quantile_l,color = color_map[key], alpha=0.5, label = f'{labels[key]}: {quantile[0]:.0%} to {quantile[1]:.0%}')
            ax[3].grid(visible = True)
            ax[3].legend()
            ax[3].set_xlabel('std of added noise')

        ax[0].set_title(f'Ajusted Rand Index of Clustering, K = {k}')
        ax[1].set_title(f'Lag Prediction Accuracy %')
        ax[2].set_title(f'Average Direction Prediction Error')
        ax[3].set_title(f'Average Lag Prediction Error (shaded area is the {quantile[0]:.0%} to {quantile[1]:.0%} percentile of errors)')

        plt.savefig(results_save_dir + f'/acc_err_ARI_K={k}')

    #--- plots of signal estimates of different methods ---#
    results_save_dir_2 = results_save_dir + '/signal_estimates'
    os.makedirs(results_save_dir_2)

    # align the estimated signals to ground truth signals
    for k in K_range:
        os.makedirs(results_save_dir_2 + f'/K={k}')
        i = 0
        for sigma in sigma_range:
            # # calculate the estimated mixing probabilities

            # for key, classes in classes_estimates[f'K={k}'][f'sigma={sigma:.2g}'].items():
            #     p_est[key] = np.zeros(k)
            #     for c in classes.unique():
            #         p_est[key][c] = np.mean(classes==c)
            estimates_i = estimates[f'K={k}'][f'sigma={sigma:.2g}']
            fig, ax = plt.subplots(k, 1, figsize = (10,5*k))
            X_true =estimates_i['signals']['true']
            # for key, X_estimates in estimates_i['signals'].items():
            #     if key != 'true':
            #         X_estimates, perm = utils.align_to_ref_het(X_estimates, X_true)
            #         signal_estimates[f'K={k}'][f'sigma={sigma:.2g}'][key] = X_estimates
            #         rel_errors = {}
            #         rel_errors_str = ''
            #         p_est_str = ''
            for j in range(k):
                rel_errors_str = []
                p_est_str = []
                for key, X_estimates in estimates_i['signals'].items():
                    ax[j].plot(X_estimates[:,j],
                                label = labels[key],
                                color = color_map[key],
                                linestyle = lty_map[key])
                    if key != 'true':
                        rel_err = np.linalg.norm(X_estimates[:,j]-X_true[:,j])/np.linalg.norm(X_true[:,j])
                        rel_errors_str.append( f'{labels[key]} {rel_err:.3f}')

                for key_p, value_p in estimates_i['probabilities'].items():
                    p_est_str.append(f'{labels[key_p]} {value_p[j]:.3g}')

                title = 'rel. err.: ' + ';'.join(rel_errors_str) + '\nmix. prob.: ' + '; '.join(p_est_str)
                ax[j].set_title(title)
                ax[j].grid()
                ax[j].legend()

            fig.suptitle(f'Comparison of the True and Estimated Signals, K = {k}, noise = {sigma:.2g}')
            plt.savefig(results_save_dir_2 + f'/K={k}/{int(i)}')
            plt.close()
            i+=1

def extract_quantile(q,values):
    """
    Extract quantile from a list of values of percentiles at 0, 5, 10, ..., 100.
    Args:
        q: quantiles to extract, valid inputs are [0, 0.05, 0.1,..., 1]
        values: ndarray of length 21. quantiles values at 0.05 interval

    Returns:

    """
    ind = int(q/0.05)
    return values[ind]


"""NASDAQ Data Link Model"""
__docformat__ = "numpy"

import argparse
import logging
import os
from typing import List

import pandas as pd
import requests

from openbb_terminal.config_terminal import API_KEY_QUANDL
from openbb_terminal.decorators import log_start_end
from openbb_terminal.rich_config import console

logger = logging.getLogger(__name__)

@log_start_end(log=logger)
def check_country_code_type(list_of_codes: str) -> List[str]:
    """Check that codes are valid for NASDAQ API"""
    nasdaq_codes = list(
        pd.read_csv(os.path.join(os.path.dirname(__file__), "NASDAQ_CountryCodes.csv"))[
            "Code"
        ]
    )
    valid_codes = []
    for code in list_of_codes.split(","):
        if code.upper() in nasdaq_codes:
            valid_codes.append(code.upper())
    if valid_codes:
        return valid_codes
    raise argparse.ArgumentTypeError("No valid codes provided.")

@log_start_end(log=logger)
def get_country_codes() -> List[str]:
    """Get available country codes for Bigmac index

    Returns
    -------
    List[str]
        List of ISO-3 letter country codes.
    """
    file = os.path.join(os.path.dirname(__file__), "NASDAQ_CountryCodes.csv")
    codes = pd.read_csv(file, index_col=0)
    return codes

@log_start_end(log=logger)
def get_big_mac_index(country_code: str) -> pd.DataFrame:
    """Gets the Big Mac index calculated by the Economist

    Parameters
    ----------
    country_code : str
        ISO-3 letter country code to retrieve. Codes available through get_country_codes().

    Returns
    -------
    pd.DataFrame
        Dataframe with Big Mac index converted to USD equivalent.
    """
    URL = f"https://data.nasdaq.com/api/v3/datasets/ECONOMIST/BIGMAC_{country_code}"
    URL += f"?column_index=3&api_key={API_KEY_QUANDL}"
    r = requests.get(URL)

    df = pd.DataFrame()

    if r.status_code == 200:
        response_json = r.json()
        df = pd.DataFrame(response_json["dataset"]["data"])
        df.columns = response_json["dataset"]["column_names"]
        df["Date"] = pd.to_datetime(df["Date"])

    # Wrong API Key
    elif r.status_code == 400:
        console.print(r.text)
    # Premium Feature
    elif r.status_code == 403:
        console.print(r.text)
    # Catching other exception
    elif r.status_code != 200:
        console.print(r.text)

    return df

@log_start_end(log=logger)
def get_big_mac_indices(country_codes: List[str]) -> pd.DataFrame:
    """Display Big Mac Index for given countries

    Parameters
    ----------
    country_codes : List[str]
        List of country codes (ISO-3 letter country code). Codes available through get_country_codes().

    Returns
    -------
    pd.DataFrame
        Dataframe with Big Mac indices converted to USD equivalent.
    """

    df_cols = ["Date"]
    df_cols.extend(country_codes)
    big_mac = pd.DataFrame(columns=df_cols)
    for country in country_codes:
        df1 = get_big_mac_index(country)
        if not df1.empty:
            big_mac[country] = df1["dollar_price"]
            big_mac["Date"] = df1["Date"]
    big_mac.set_index("Date", inplace=True)

    return big_mac


import sys
import argparse
import cv2
import numpy as np
from PIL import Image, ImageDraw
import torch
import torch.nn.functional as F
from dataset import get_loader
from model import *
from model.vgg_cfg import cfg as vgg_config
from utils import AverageMeter, load_model, Hooks
from explain.rap.modules.vgg import vgg16, vgg16_bn, vgg16_bn_voc
from torch.autograd import Variable
import torch.nn as nn
#from explains.ebp.exc_bp import ExcitationBackpropExplainer
import os
from sklearn.metrics import roc_curve, auc

def main():
    parser = argparse.ArgumentParser(description='')

    parser.add_argument('--gpu', action='store_true')
    parser.add_argument('--data_path', default="./data/VOCdevkit/VOC2012", type=str,
                        help="Data path, here for segmentation ground truth")
    parser.add_argument('--path', default='', type=str,
                        help="Saved Model Path, ref load_model in utils.py")
    parser.add_argument('--arch', default='VGG16_VOC', type=str,
                        help="Predefined Architecture for knowledge distillation" +
                        "For Structure Pruning and Unstructure Pruning, Keep VGG16_VOC")
    parser.add_argument('--iter', default=1, type=int,
                        help="how many iteration for Structure Pruning"+
                            "We use only one-shot pruning in the paper")
    parser.add_argument('--prune_rate', default=0.7, type=float,
                        help="Pruning ratio for each iteration")
    parser.add_argument('--compression', default='', type=str,
                        help='Choose in [kd | unstructured | structured]')
    parser.add_argument('--method', default='', type=str,
                        help='Choose in [gcam | ebp | lrp | rap]')
    parser.add_argument('--metric', default='auc', type=str,
                        help='Choose in [miou | iou | auc]')
    parser.add_argument('--save', default='result.txt', type=str,
                        help="Results Recording file")

    args = parser.parse_args()
    compression = args.compression
    method = args.method
    metric = args.metric

    print("Attribution Map Method : {}".format(method))
    print("Compression Method : {}".format(compression))
    print("Evaluation Metric : {}".format(metric))

    if (method == 'gcam') or (method =='ebp'):
        if compression == 'structured':
            layerwise_prune_rate = [args.prune_rate, 0.0,
                            0.0, 0.0,
                            0.0, 0.0, 0.0,
                            args.prune_rate, args.prune_rate, args.prune_rate,
                            args.prune_rate, args.prune_rate, args.prune_rate]
            model_config = prune_vgg_cfg(vgg_config['VGG16_VOC'], layerwise_prune_rate, args.iter)
            model = VGG(None, input_dims=(3,227,227), specific_cfg=model_config)

        elif compression == 'kd':
            model = VGG(args.arch, input_dims=(3, 227, 227))

        elif compression == 'unstructured':
            model = VGG(args.arch, input_dims=(3,227,227))
        else:
            raise NotImplementedError

    elif (method == 'lrp') or (method == 'rap'):
        if compression == 'structured':
            layerwise_prune_rate = [args.prune_rate, 0.0,
                        0.0, 0.0,
                        0.0, 0.0, 0.0,
                        args.prune_rate, args.prune_rate, args.prune_rate,
                        args.prune_rate, args.prune_rate, args.prune_rate]
            model_config = prune_vgg_cfg(vgg_config['VGG16_VOC'], layerwise_prune_rate, args.iter)
            model = vgg16_bn_voc(config=model_config)
        elif compression == 'kd':
            if args.arch == 'VGG16_VOC':
                r = 1
            elif args.arch == 'VGG16_VOC_x2':
                r = 2
            elif args.arch == 'VGG16_VOC_x4':
                r = 4
            else:
                r = 8
            model = vgg16_bn_voc(config=None, reduce=r, pretrained=False)
        elif compression == 'unstructured':
            r = 1
            model = vgg16_bn_voc(config=None, reduce=r, pretrained=False)
        else:
            raise NotImplementedError

    print('Number of model parameters: {}'.format(sum([p.data.nelement() for p in model.parameters()])))

    if args.gpu:
        model = torch.nn.DataParallel(model).cuda()
    else:
        model = torch.nn.DataParallel(model)

    checkpoint = args.path

    _, best_mAP = load_model(checkpoint, model)

    ## Batch size should be 1
    vocsegment = get_loader(data_name='VOC-seg',
                data_path= args.data_path,
                split= 'val',
                batch_size=1)

    if method == 'gcam':
        features = Hooks(model, ['42'])
    elif method == 'ebp':
        layer = model.module.features._modules['42']
        explainer = ExcitationBackpropExplainer(model, layer)
    else:
        model = model.module

    model.eval()
    auc_list = []
    correct_point = 0
    incorrect_point = 0
    pred_true = 0
    pred_false = 0

    for idx, (images, targets, labels) in enumerate(vocsegment):
        if args.gpu:
            images = images.cuda()
            labels = labels.cuda()

        if method == 'gcam':
            features.reset_()
            output, _, _ = model(images)
            activation = features.activations[0]

        elif method == 'ebp':
            output, _, _ = model(images)

        elif (method == 'lrp') or (method=='rap'):
            output = model(images)

        ## 0 index : BackGround
        lbl = (labels[:,1:].squeeze(0)).nonzero(as_tuple=False).cpu().data.numpy() #
        predictions = (output.squeeze()>0).nonzero(as_tuple=False).cpu().data.numpy() #
        pointacc_labels = []

        ## Class-wise heatmap
        for i, lb in enumerate(lbl):
            ## Class-wise Segmentation Map
            target = targets.squeeze().data.numpy()
            seg_mask = np.zeros((target.shape[0], target.shape[1]), dtype=np.uint8)
            whe = np.where(target == (lb+1))
            seg_mask[whe] = 1
            for m in range(len(whe[0])):
                pointacc_labels.append([whe[0][m], whe[1][m]])

            grad_out = output.data.clone()
            grad_out.fill_(0.0)
            grad_out[0, lb] = 1

            if method == 'gcam':
                model.zero_grad()
                output.backward(grad_out, retain_graph=True)
                gradient = features.gradients[i]
                linearization = torch.sum(torch.sum(gradient, 3, keepdim=True), 2, keepdim=True)
                channels = linearization * activation
                grad_cam = torch.sum(channels, 1, keepdim=True)
                grad_cam = F.relu(grad_cam)
                heatmap = grad_cam.squeeze().cpu().data.numpy()
                heatmap = cv2.resize(heatmap, (227, 227))

            elif method == 'ebp':
                ebp_heatmap = explainer.explain(images, grad_out)
                heatmap = ebp_heatmap.squeeze().cpu().data.numpy()
                heatmap = cv2.resize(heatmap, (227, 227))

            elif method == 'lrp':
                Res = model.relprop(R=output * grad_out, alpha=1).sum(dim=1, keepdim=True)
                heatmap = Res.squeeze().cpu().data.numpy()
                if output[0, lab] < 0:
                    heatmap = -1 * heatmap

            elif method == 'rap':
                RAP = model.RAP_relprop(R=grad_out)
                Res = (RAP).sum(dim=1, keepdim=True)
                heatmap = Res.squeeze().cpu().data.numpy()
                if output[0, lab] < 0:
                    heatmap = -1 * heatmap
                mask = np.int32(heatmap > 0)
                heatmap = heatmap * mask

            if np.isnan(heatmap).any():
                continue
            ##For Point Acc, maxpoint
            max_index = np.where(heatmap == np.max(heatmap))
            max_point = [max_index[0][0], max_index[1][0]]

            ##Get AUC
            h_max = np.nanmax(heatmap)
            h_min = np.nanmin(heatmap)
            heatmap = (heatmap - h_min) / (h_max - h_min + 10e-6)
            auc_value = get_auc(heatmap, seg_mask)

            if lb not in predictions:
                pred_false += 1
                continue

            if max_point in pointacc_labels:
                correct_point += 1
            else:
                incorrect_point += 1
            pred_true += 1
            auc_list.append(auc_value)

        if idx %100 == 0:
            print("Processing Image(Current|Total) {}|{}".format(idx, len(vocsegment)))

    meanAUC = sum(auc_list) / len(auc_list)
    print("Correct Prediction Samples: {}".format(pred_true))
    print("Incorrect Prediction Samples: {}".format(pred_false))
    print("Pointing game correct samples in correct prediction: {}".format(correct_point))
    print("Pointing game [(Point Correct and Prediction Correct)/(Prediction Correct)] : {}".format(float(correct_point/pred_true)))
    print("Metric AUC : {}".format(meanAUC))

    save_file = args.save
    with open(save_file, 'w') as f:
        f.write(checkpoint + '\t' + 'Metric AUC : {} \n'.format(float(meanAUC)))
        f.write(checkpoint + '\t' + 'Point Game Acc : {}'.format(float(correct_point/pred_true)))

def get_auc(pred, gt):
    pred = pred.reshape(-1)
    gt = gt.reshape(-1)
    flasePositive, truePositive, _ = roc_curve(gt, pred)
    roc_auc = auc(flasePositive, truePositive)
    return roc_auc

def get_miou(pred, gt, n_classes=2):
    pred_tmp = pred
    gt_tmp = gt
    intersect = [0] * n_classes
    union = [0] * n_classes
    for j in range(n_classes):
        # match = (pred_tmp == j) + (gt_tmp == j)
        match = (pred_tmp == j).astype(np.float) + (gt_tmp == j).astype(np.float)
        it = np.sum(match == 2)
        un = np.sum(match > 0)
        intersect[j] += it
        union[j] += un
    iou = []
    for k in range(n_classes):
        if union[k] == 0:
            continue
        iou.append(intersect[k] / union[k])
    miou = (sum(iou) / len(iou))
    return miou

def get_iou(pred, gt):
    pred_tmp = pred
    gt_tmp = gt
    match = (pred_tmp == 1).astype(np.float) + (gt_tmp == 1).astype(np.float)
    intersect = np.sum(match == 2)
    union = np.sum(match > 0)
    iou = intersect/union
    return iou

def thresh(a, coef):
    return coef * (np.max(a))

def denormalize(image):
    image[0,:,:] = image[0,:,:] * 0.2239 + 0.4589
    image[1,:,:] = image[1,:,:] * 0.2186 + 0.4355
    image[2,:,:] = image[2,:,:] * 0.2206 + 0.4032
    return image

def visualize(map):
    r = map.copy()
    g = map.copy()
    b = map.copy()
    r[np.where(map == 1)] = 255
    g[np.where(map == 1)] = 255
    b[np.where(map == 1)] = 255
    rgb = np.zeros((map.shape[0], map.shape[1], 3))
    rgb[:, :, 0] = r
    rgb[:, :, 1] = g
    rgb[:, :, 2] = b
    return rgb

def compute_pred(output):
    pred = output.data.max(1, keepdim=True)[1]  # get the index of the max log-probability
    print('Pred cls : '+str(pred))
    T = pred.squeeze().cpu().numpy()
    T = np.expand_dims(T, 0)
    T = (T[:, np.newaxis] == np.arange(20)) * 1.0
    T = torch.from_numpy(T).type(torch.FloatTensor)
    Tt = Variable(T).cuda()
    return Tt

def prune_vgg_cfg(cfg, prune_rate, iter):
    for idx in range(iter):
        # print('iter: {0}'.format(idx))
        conv_id = 0
        new_features = []
        for layer in cfg['features']:
            if isinstance(layer, int):
                new_features.append(int(layer * (1 - prune_rate[conv_id])))
                conv_id += 1
            elif layer == 'M':
                new_features.append('M')
            else:
                assert False
        cfg = {'features': new_features, 'classifier': cfg['classifier']}
    return {'features': new_features, 'classifier': cfg['classifier']}

if __name__ == '__main__':
    main()


"""
You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

Example:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

Return 3. The paths that sum to 8 are:

1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11
"""
"""
算法思路：相当于对每个节点做dfs，且sum=8，而针对某个节点的时候利用dfs，寻找sum=8：相当于两层递归：外层用来递归的寻找解，另外一层寻找递归找解
dfs是对一个根节点找路径和是sum的递归，将这个递归用在每个节点上，就能递归得到所有的解
"""

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def __init__(self):
        self.res = 0
    def pathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: int
        """
        #判断跟节点是否为空
        if not root:
            return False
        self.dfs(root, sum)
        self.pathSum(root.left, sum)
        self.pathSum(root.right, sum)
        return self.res
    def dfs(self,root,sum):
        if not root:
            return False
        if sum == root.val:
            self.res = self.res+1
        self.dfs(root.left, sum-root.val)
        self.dfs(root.right, sum-root.val)


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

# Самостоятельно изучите работу со стандартным пакетом Python timeit . Оцените с помощью этого модуля скорость
# работы итеративной и рекурсивной версий функций factorial и fib . Во сколько раз измениться скорость работы
# рекурсивных версий функций factorial и fib при использовании декоратора lru_cache ? Приведите в отчет и обоснуйте
# полученные результаты

from functools import lru_cache
import timeit

def fib():
    def iterative(n):
        a, b = 0, 1

        while n > 0:
            a, b = b, a + b
            n -= 1

        return a

    @lru_cache
    def recursive(n):
        if n == 0 or n == 1:
            return n

        else:
            return recursive(n - 2) + recursive(n - 1)

    return {'iterative': iterative, 'recursive': recursive}

def factorial():
    def iterative(n):
        product = 1
        while n > 1:
            product *= n
            n -= 1

        return product

    @lru_cache
    def recursive(n):
        if n == 0:
            return 1

        elif n == 1:
            return 1

        else:
            return n * recursive(n - 1)

    return {'iterative': iterative, 'recursive': recursive}

def test(fun, end, step, start=0):
    arr = []
    for i in range(start, end, step):
        start_timer = timeit.default_timer()
        fun(i)
        arr.append(timeit.default_timer() - start_timer)

    return arr

def filter_float(list_timer, timer=1000000):
    s = []
    for item_timer in list_timer:
        s.append(float(f"{(item_timer * timer):0.2f}"))

    return s

if __name__ == '__main__':
    x = 10000
    lim_x = 20

    print("\nfib[iterative]: (microsecond)")
    print(filter_float(test(fib()['iterative'], x, int(x/lim_x))))

    print("\nfib[recursive]: (microsecond)")
    print(filter_float(test(fib()['recursive'], x, int(x/lim_x))))

    print("\nfactorial[iterative]: (microsecond)")
    print(filter_float(test(factorial()['iterative'], x, int(x/lim_x))))

    print("\nfactorial[recursive]: (microsecond)")
    print(filter_float(test(factorial()['recursive'], x, int(x/lim_x))))


#from dirg_util.dict import LDAPDict
#ldap_settings = {
#    "ldapuri": "ldaps://ldap.test.umu.se",
#    "base": "dc=umu, dc=se",
#    "filter_pattern": "(uid=%s)",
#    "user": "",
#    "passwd": "",
#    "attr": [
#        "eduPersonScopedAffiliation",
#        "eduPersonAffiliation",
#        "eduPersonPrincipalName",
#        "givenName",
#        "sn",
#        "mail",
#        "uid",
#        "o",
#        "c",
#        "labeledURI",
#        "ou",
#        "displayName",
#        "norEduPersonLIN"
#    ],
#    "keymap": {
#        "mail": "email",
#        "labeledURI": "labeledURL",
#    },
#    "static_values": {
#        "eduPersonTargetedID": "one!for!all",
#    },
#    "exact_match": True,
#    "firstonly_len1": True,
#    "timeout": 15,
#}
#Uncomment to use a LDAP directory instead.
#USERS = LDAPDict(**ldap_settings)

USERS = {
    "testuser@test.com": {
        "sn": "Testsson",
        "givenName": "Test",
        "eduPersonAffiliation": "student",
        "eduPersonScopedAffiliation": "student@example.com",
        "eduPersonPrincipalName": "test@example.com",
        "uid": "testuser",
        "eduPersonTargetedID": "one!for!all",
        "c": "SE",
        "o": "Example Co.",
        "ou": "IT",
        "initials": "P",
        "schacHomeOrganization": "example.com",
        "email": "test@example.com",
        "displayName": "Test Testsson",
        "labeledURL": "http://www.example.com/test My homepage",
        "norEduPersonNIN": "SE199012315555"
    },
    "XXX@XX.com": {
        "sn": "de ",
        "givenName": "JXXose",
        "email": "XXX@XX.com",

    },
    "babs": {
        "surname": "Babs",
        "givenName": "Ozzie",
        "eduPersonAffiliation": "affiliate"
    },
    "upper": {
        "surname": "Jeter",
        "givenName": "Derek",
        "eduPersonAffiliation": "affiliate"
    },
}

EXTRA = {}


import logging
from typing import Tuple
import torch

EPS = 1e-16
METHODS = ("component_max", "overall_max", "jet_E")

def normalize_p4(
    p4: torch.Tensor, method: str = "overall_max"
) -> Tuple[torch.Tensor, torch.Tensor]:
    if method.lower().replace(" ", "_").replace("-", "_") == "component_max":
        return normalize_p4_component_max(p4)
    elif method.lower().replace(" ", "_").replace("-", "_") == "overall_max":
        return normalize_p4_overall_max(p4)
    elif method.lower().replace(" ", "_").replace("-", "_") == "jet_E".lower():
        return normalize_p4_jet_E(p4)
    else:
        logging.warning(
            f"Normalization method {method} not recognized. Using component_max."
        )
        return normalize_p4_overall_max(p4)

def normalize_p4_component_max(p4: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
    """Normalization using :math:`p^\mu \to p^\mu \max_{p \in J} (|p^\mu|)`.
    That is, we normalize each component by the component-wise maximum of the 4-momenta within the jet.

    :param p4: 4-momentum tensor of shape (num_jets, num_particles, 4).
    :type p4: torch.Tensor
    :return: (p4_norm, factor)
    :rtype: Tuple[torch.Tensor, torch.Tensor]
    """
    normalize_p4_factor = torch.abs(p4).amax(dim=-2, keepdim=True) + EPS
    p4 = p4 / normalize_p4_factor
    return p4, normalize_p4_factor

def normalize_p4_overall_max(p4: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
    """Normalization using :math:`p \to p / \max_{\mu \in \{0,1,2,3\}} \max_{p \in J} (|p^\mu|)`.
    That is, we divide each entry of the 4-momentum by the overall maximum of the 4-momenta within the jet.

    :param p4: 4-momentum tensor of shape (num_jets, num_particles, 4).
    :type p4: torch.Tensor
    :return: (p4_norm, factor)
    :rtype: Tuple[torch.Tensor, torch.Tensor]
    """
    normalize_p4_factor = (
        torch.abs(p4).amax(dim=-1, keepdim=True).amax(dim=-2, keepdim=True) + EPS
    )
    p4 = p4 / normalize_p4_factor
    return p4, normalize_p4_factor

def normalize_p4_jet_E(p4: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
    """Normalization using the jet energy: :math:`p \to p / (\sum_{p \in J} p^0)`.

    :param p4: 4-momentum tensor of shape (num_jets, num_particles, 4).
    :type p4: torch.Tensor
    :return: (p4_norm, factor)
    :rtype: Tuple[torch.Tensor, torch.Tensor]
    """
    jet_p = p4.sum(dim=-2, keepdim=True)
    normalize_p4_factor = jet_p[..., 0].unsqueeze(-1).unsqueeze(-1) + EPS
    p4 = p4 / normalize_p4_factor
    return p4, normalize_p4_factor


"""A setuptools based setup module.
"""
import setuptools
# To use a consistent encoding
import codecs
import os

__author__ = 'Tomasz J. Kotarba <tomasz@kotarba.net>'
__copyright__ = 'Copyright (c) 2016, Tomasz J. Kotarba. All rights reserved.'
__maintainer__ = 'Tomasz J. Kotarba'
__email__ = 'tomasz@kotarba.net'

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

install_requires = [
    'rapidpro-python>=2.1.6',
    'rapidsms>=0.18.0',
    'django>=1.7,<1.9'  # rapidsms incompatible with 1.9 at the time
]

tests_require = [
    'mock',
]

# Get the long description from the README.rst file
with codecs.open(os.path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()

setuptools.setup(
    name='rapidpro-for-rapidsms',

    version='1.0.3',

    description='A RapidPro backend for RapidSMS.',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/system7ltd/rapidpro-for-rapidsms',

    # Author details
    author='Tomasz J. Kotarba',
    author_email='tomasz@kotarba.net',

    license='BSD',

    classifiers=[
        'Development Status :: 5 - Production/Stable',

        'Intended Audience :: Developers',
        'Intended Audience :: System Administrators',
        'Intended Audience :: Telecommunications Industry',
        'Intended Audience :: Information Technology',

        'Topic :: Communications',
        'Topic :: Internet',
        'Topic :: Software Development :: Libraries',
        'Topic :: Software Development :: Libraries :: Application Frameworks',

        'Framework :: Django',

        'Operating System :: OS Independent',

        'License :: OSI Approved :: BSD License',

        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],

    keywords='rapidsms rapidpro sms django textit',

    # packages=setuptools.find_packages(exclude=['tests']),
    packages=setuptools.find_packages(),

    install_requires=install_requires,

    tests_require=tests_require,

    test_suite='run_tests.main',

    extras_require={'development': tests_require},
)


from django.db import models
from django.db.models import Q
from django.db.models.query import QuerySet
from django.utils import timezone

class AbstractQuerySet(QuerySet):
    def update(self, **kwargs):
        if "updated_at" not in kwargs:
            kwargs["updated_at"] = timezone.now()
        return super().update(**kwargs)

    def delete(self):
        return self.update(is_active=False)

    def force_delete(self):
        return super().delete()

class AbstractManager(models.Manager):
    def get_queryset(self, enforce_filter=True):
        qs = AbstractQuerySet(self.model, using=self._db)
        return qs.filter(is_active=True) if enforce_filter else qs

    def unfiltered(self):
        return self.get_queryset(enforce_filter=False)

class AbstractModel(models.Model):
    objects = AbstractManager()

    created_at = models.DateTimeField(
        default=timezone.now,
        help_text="Time of creation of this object",
        editable=False,
    )
    updated_at = models.DateTimeField(
        auto_now=True, help_text="Time of updation of this object", editable=False
    )
    is_active = models.BooleanField(
        default=True,
        help_text="Denotes if the object is active or not. Inactive objects behave similar to how a deleted object "
        "works.",
    )

    class Meta:
        abstract = True

    def update_fields(self, **params):
        self.updated_at = timezone.now()
        update_fields = ["updated_at"]

        for key, value in params.items():
            setattr(self, key, value)
            update_fields.append(key)

        self.save(update_fields=update_fields)

    def delete(self, using=None, keep_parents=False):
        return self.update_fields(is_active=False)


import sys

try:

 file = open(sys.argv[2],"r")
except :
    print("errore")

dizionario ={}
for linea in file:
 frase = file.readline()         # leggo ogni linea
 parole = frase.split()          # creo una lista con ogni parola della linea

 for i in range(len(parole)):     # per ogni parola nella lista

  for x in range(len(parole[i])):   # per ogni lettera nella parola
      if parole[i][x] in dizionario:
          dizionario[parole[i][x]] += 1    #se c'è l'aumento
      else:
          dizionario[parole[i][x]] = 1     # se non è nel dizionario la aggiungo

file.close()
print(dizionario)


#colorlist1=input("enter list:")
#colorlist2=input("enter list2:")
#list1=colorlist1.split(",")
#list2=colorlist2.split(",")
#print(list1)
#print(list2)
#l=set(list1).difference(list2)
#print(l)

line=input("enter line of words:")
dict={}
list=line.split()
for word in list:
    if word in dict:
        dict[word]+=1
    else:
        dict[word]=1
for x,y in dict.items():
    print(x,y)


import numpy as np
import pandas as pd

import matplotlib.pyplot as pyt

STRATEGIES = [
     'LCE-LFU',             # Leave Copy Everywhere
     'LCE-LRU',             # Leave Copy Everywhere
     # # 'NO_CACHE',        # No caching, shorest-path routing
     # # 'HR_SYMM',         # Symmetric hash-routing
     # # 'HR_ASYMM',        # Asymmetric hash-routing
     # # 'HR_MULTICAST',    # Multicast hash-routing
     # # 'HR_HYBRID_AM',    # Hybrid Asymm-Multicast hash-routing
     # # 'HR_HYBRID_SM',    # Hybrid Symm-Multicast hash-routing
     # # 'CL4M',            # Cache less for more
     # 'PROB_CACHE',      # ProbCache
     'WLRU',
     'WLFU',
     'GRD',
	 # 'PD',
             ]

SCEN = {
	'alpha': 'Zipf Exponent Alpha',
	'cache_ratio': 'Cache to Population Ratio (%)',
	# 'std': 'Change Stardard Derivation',
}

STYLE = {
		 'WLRU':         'b-o',
		 'WLFU':        'g-D',
		 'GRD':    'm--^',
		 'PD':    'c-s',
		 'LCE-LRU':    'r--v',
		 'LCE-LFU':  'k--*',
		 'PROB_CACHE':  'y--X',
}

STRATEGY_LABEL = {
		 'WLRU':         'U-LRU',
		 'WLFU':        'U-LFU',
		 'GRD':    'GRD',
		 'PD':    'Offline',
		 'LCE-LRU':    'LCE-LRU',
		 'LCE-LFU':  'LCE-LFU',
		 'PROB_CACHE':  'ProbCache',
}

TOPO = [
	'GEANT',
	'TISCALI',
]

# print results
data = pd.read_csv('multi_data.csv')

fig = pyt.figure(figsize=(16, 3))
pyt.subplots_adjust(wspace=0.3)

for j, metric in enumerate(SCEN.keys()):
	fixed_metric = SCEN.keys()[1 - j]
	fixed_value = data.groupby(fixed_metric).size().argmax()
	for i, topo in enumerate(TOPO):
		ax = pyt.subplot(1,len(SCEN)+len(TOPO),i*len(TOPO)+j+1)
		res = data.loc[(data[fixed_metric] == fixed_value) & (data['topology']==topo)].sort_values(by=metric)
		# print res
		x = res[metric]
		for method in STRATEGIES:
			y = []
			y_up = []
			y_down = []
			for x_value in x:
				this_strategy = res.loc[(res[metric]==x_value) & (res['strategy']==method)][['round', 'weight']]
				nocache = res.loc[(res[metric]==x_value) & (res['strategy']=='NOCACHE')][['round', 'weight']]\
					.rename(columns={'weight':'nocache'})
				screen_out = pd.merge(this_strategy, nocache,on='round')
				screen_out = screen_out['nocache']-screen_out['weight']
				# print np.mean(screen_out)
				y.append(np.mean(screen_out))
				# y_up.append(np.percentile(screen_out,95)-y[-1])
				# y_down.append(y[-1]-np.percentile(screen_out,5))
			pyt.plot(x*100 if metric == 'cache_ratio' else x, y, STYLE[method], label=STRATEGY_LABEL[method])
			# pyt.errorbar(x, y, yerr=[y_down,y_up], fmt=STYLE[method], label=STRATEGY_LABEL[method])
		pyt.xlabel(SCEN[metric])
		pyt.ylabel("Caching Gain")
		pyt.title("(%c) Topology=%s" % (chr(ord('a')+i*len(TOPO)+j), topo))
		if j + i == 0:
			pyt.legend(loc="upper left", ncol=5, bbox_to_anchor=(-0.2,1.2, 5, 0.05), mode='expand')
# pyt.show()
pyt.savefig("multi.pdf", bbox_inches='tight')
pyt.savefig("multi.png", bbox_inches='tight')
pyt.close()


from random import random

def parabol_area(N): # N là số điểm gieo mô phỏng
    points = [(random(), random()) for _ in range(N)]
    N_in = sum([y <= x**2 for x, y in points])
    return N_in/N

if __name__ == "__main__":
    N = int(input("Bạn muốn gieo bao nhiêu điểm: "))
    print(f"Diện tích vùng dưới parabol là {parabol_area(N):.4f}")


import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mtick
import pandas

data = pandas.read_csv('eff_test1.csv', sep=',')

# Data for plotting
Vin = data["Vin"]
Iin = data["Iin"]
Vout = data["Vout"]
Iout = data["Iout"]

Eff = (Vout*Iout)/(Vin*Iin)

fig, ax = plt.subplots()
ax.plot(Iout[1:], Eff[1:], linestyle='-', color="tab:blue", alpha=0.9, linewidth=3.0)
#ax.tight_layout()
ax.yaxis.set_major_formatter(mtick.PercentFormatter(1.0, 1))
#ax.set_ylabel('$\eta$',rotation=0, fontsize=18, labelpad=10)
ax.set_xlabel(r'Output Current (A)', fontsize=18)
plt.title('Open PMIC\nEfficiency vs Output Current (3.3V in, 1.8V out)', fontsize=14)
ax.grid(alpha=0.15)
plt.ylim([0.6, 0.95])
plt.xlim([0.0, 0.5])
fig.align_ylabels()
fig.tight_layout()

fig.savefig("eff1.png", dpi=300)
plt.show()


import os

import feedparser
from django.http import HttpRequest
from django.shortcuts import render
from datetime import datetime

# Create your views here.

from lxml import etree
from tp1.settings import BASE_DIR

fname = 'mondial.xml'
pname = os.path.join(BASE_DIR, 'app/data/' + fname)
tree = etree.parse(pname)
root = tree.getroot()

def home(request):
    tparams = {
        'title': 'Home Page',
        'year': datetime.now().year,
    }
    return render(request, 'index.html', tparams)

def about(request):
    tparams = {
        'title': 'About',
        'message': 'Acerca do projecto',
        'year': datetime.now().year,
    }
    return render(request, 'about.html', tparams)

def list(request, selection):
    #print(selection)
    xsl = "simpleList.xsl"

    if selection == "continent" or selection == "country" or selection == "organization":
        xpath = f'//{selection}'

    elif ("cty" in selection) or ("city" in selection) or ("stadt" in selection):
        xpath = f'//*[@id="{selection}"]'
        xsl = 'city.xsl'

    elif ("europe" in selection) or ("asia" in selection) or ("africa" in selection) or ("america" in selection) or ("australia" in selection):
        xpath = f'//country/encompassed[@continent="{selection}"]/parent::*'

    elif "org" in selection:
        xpath = f'//organization[@id="{selection}"]'
        xsl = 'organization.xsl'

    else:
        xpath = f'//country[@id="{selection}"]'
        xsl = 'country.xsl'
    #print(xpath)

    xslt_tree = etree.parse(os.path.join(BASE_DIR, 'app/data/' + xsl))
    transform = etree.XSLT(xslt_tree)
    print(transform(root, selection=xpath))
    tparams = {
        'year': datetime.now().year,
        'current': selection,
        'page': transform(root, selection=xpath),
    }

    return render(request, 'xsltDisplay.html', tparams)

def rss(request):
    feeds = feedparser.parse('https://news.google.com/rss?hl=en-US&gl=US&ceid=US%3Aen&x=1571747254.2933')

    tparams = {
        'title': 'RSS',
        'message': 'Noticias da Imprensa Americana',
        'year': datetime.now().year,
        'feeds': feeds,
    }
    return render(request, 'rss.html', tparams)