import json
import os

DATA_FILE = "expense_data.json"

def load_data():
    # Load existing data from the file or initialize a new data structure
    if os.path.exists(DATA_FILE):
        with open(DATA_FILE, "r") as file:
            return json.load(file)
    else:
        # Default data structure with predefined categories
        return {"expenses": [], "categories": ["Groceries", "Transportation", "Entertainment", "Utilities"]}

def save_data(data):
    # Save the updated data to the file
    with open(DATA_FILE, "w") as file:
        json.dump(data, file, indent=2)

def display_menu():
    # Display the main menu options
    print("\nExpense Tracker Menu:")
    print("1. Add Expense")
    print("2. View Expenses")
    print("3. View Category Summaries")
    print("4. Manage Categories")
    print("5. Exit")

def add_expense(data):
    # Allow the user to input details for a new expense, including category selection
    print("\nEnter Expense Details:")
    description = input("Description: ")
    amount = float(input("Amount: "))

    # Display categories for user selection
    print("\nSelect a Category:")
    for i, category in enumerate(data["categories"], start=1):
        print(f"{i}. {category}")

    # Get user input for category and validate
    category_index = int(input("Enter the number corresponding to the category: ")) - 1
    category = data["categories"][category_index]

    # Create a new expense and append it to the data
    expense = {"description": description, "amount": amount, "category": category}
    data["expenses"].append(expense)
    save_data(data)
    print("Expense added successfully.")

def view_expenses(data):
    # Display all recorded expenses
    if not data["expenses"]:
        print("No expenses recorded.")
        return

    print("\nAll Expenses:")
    for expense in data["expenses"]:
        print(f"{expense['description']} - {expense['amount']}/- - {expense['category']}")

def view_category_summaries(data):
    # Display summaries for each expense category
    if not data["expenses"]:
        print("No expenses recorded.")
        return

    print("\nCategory Summaries:")
    for category in data["categories"]:
        expenses_in_category = [expense["amount"] for expense in data["expenses"] if expense["category"] == category]
        total_spending = sum(expenses_in_category)
        average_spending = total_spending / len(expenses_in_category) if expenses_in_category else 0
        print(f"{category}: Total - {total_spending}/-, Average - {average_spending:.2f}/-")

def manage_categories(data):
    # Allow the user to manage expense categories
    print("\nManage Categories:")
    print("1. Add Category")
    print("2. Edit Category")
    print("3. Remove Category")
    print("4. Back to Main Menu")

    choice = input("Enter your choice (1-4): ")

    if choice == "1":
        add_category(data)
    elif choice == "2":
        edit_category(data)
    elif choice == "3":
        remove_category(data)
    elif choice == "4":
        return
    else:
        print("Invalid choice. Please try again.")

def add_category(data):
    # Allow the user to add a new category
    new_category = input("Enter the new category name: ")
    data["categories"].append(new_category)
    save_data(data)
    print(f"Category '{new_category}' added successfully.")

def edit_category(data):
    # Allow the user to edit an existing category
    print("\nExisting Categories:")
    for i, category in enumerate(data["categories"], start=1):
        print(f"{i}. {category}")

    category_index = int(input("Enter the number corresponding to the category to edit: ")) - 1

    new_category = input("Enter the new category name: ")
    data["categories"][category_index] = new_category
    save_data(data)
    print("Category edited successfully.")

def remove_category(data):
    # Allow the user to remove an existing category
    print("\nExisting Categories:")
    for i, category in enumerate(data["categories"], start=1):
        print(f"{i}. {category}")

    category_index = int(input("Enter the number corresponding to the category to remove: ")) - 1
    removed_category = data["categories"].pop(category_index)

    # Remove the category from all expenses
    data["expenses"] = [expense for expense in data["expenses"] if expense["category"] != removed_category]

    save_data(data)
    print(f"Category '{removed_category}' removed successfully.")

if __name__ == "__main__":
    # Load existing data or initialize a new data structure
    expense_data = load_data()

    while True:
        display_menu()
        choice = input("Enter your choice (1-5): ")

        if choice == "1":
            add_expense(expense_data)
        elif choice == "2":
            view_expenses(expense_data)
        elif choice == "3":
            view_category_summaries(expense_data)
        elif choice == "4":
            manage_categories(expense_data)
        elif choice == "5":
            print("Exiting the Expense Tracker. Goodbye!")
            break
        else:
            print("Invalid choice. Please try again.")


"""
BluePrint for seller inbox route and logic

Shows the User their products they have posted and
Allows them to mark them as sold

Very Simple, written By Alex Kohanim

Please contact if questions arise

"""

from flask import Blueprint, render_template, session, request, abort, redirect
import gatorProduct as product  # class made by alex

from queries import query
from dbCursor import getCursor
from filterData import filter_data
import bleach

userDashboard_blueprint = Blueprint('userDashboard', __name__)

@userDashboard_blueprint.route("/user-dashboard")
def user_dashboard():
    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']

    categories = []

    if sessionUser == "":
        abort(404)
    cursor = getCursor()[1]
    # To fetch items for a user from db
    cursor.execute(query().PRODUCTS_FOR_USER(sessionUser['u_id']))
    data = cursor.fetchall()
    # To fetch categories from db
    cursor.execute(query().fetchAllCategories())
    allCategories = cursor.fetchall()
    categories = [allCategories[i][0] for i in range(len(allCategories))]

    productList = []

    for d in data:
        if len(d) == 16:
            productObject = product.makeProduct(d)
            productList.append(productObject)

    currentSearch = ""
    categoryName = "All"
    session['categories'] = categories

    return render_template("user-dashboard.html", sessionUser=sessionUser, productList=productList,
                           currentSearch=currentSearch, categoryName=categoryName, categories=categories)

@userDashboard_blueprint.route("/user-dashboard/<item_id>/<action>")
def contact_seller_action(item_id, action):
    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']

    if sessionUser == "":
        abort(404)

    if action == "sold":
        db, cursor = getCursor()
        cursor.execute(query().SELL_ITEM(item_id))
        db.commit()
        return redirect("/user-dashboard")
    else:
        abort(404)


from behave import *
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from configuration.config import TestData
from pages.LoginPage import LoginPage
from pages.DashboardPage import DashboardPage
from pages.ProfilePage import ProfilePage
from pages.MoviePage import MoviePage
from webdriver_manager.chrome import ChromeDriverManager
from basestep import BaseStep

class LoginSteps(BaseStep):

    @given(u'Launch the browser')
    def launch_browser(context):
        context.driver = webdriver.Chrome(ChromeDriverManager().install())
        context.driver.maximize_window()

    @when(u'Open the https://play.watch.tv.br/login/ website')
    def open_login_page(context):
        try:
            context.driver.get(TestData.URL)
            context.loginPage = LoginPage(context.driver)
            context.profilePage = ProfilePage(context.driver)
            context.dashboardPage = DashboardPage(context.driver)
            context.moviePage = MoviePage(context.driver)

        except:
            context.driver.close()
            assert False,"Test is failed in open login page section"

    @then(u'The login portal has been opened')
    def validate_login_page(context):
        try:
            context.loginPage.validate_login_page()
        except:
            context.driver.close()
            assert False, "Test is failed in validate login page title"

    @then(u'Provide the username "{user}" and password "{pwd}"')
    def enter_login_creds(context, user, pwd):
        try:
            context.loginPage.enter_login_credentials(user,pwd)
        except:
            context.driver.close()
            assert False, "Test is failed in enter login credentials"

    @then(u'Click on the Login button')
    def enter_login(context):
        try:
            context.loginPage.enter_login()
        except:
            context.driver.close()
            assert False, "Test is failed in enter login"

    @then(u'Login is failed and invalid credential error is displayed')
    def validate_invalid_login(context):
        try:
            context.loginPage.validate_invalid_login()
        except:
            context.driver.close()
            assert False, "Test is failed in validating valid login"

    @then(u'Provide the username "{user}"')
    def enter_login_creds(context, user):
        try:
            context.loginPage.enter_username(user)
        except:
            context.driver.close()
            assert False, "Test is failed in enter username"

    @then(u'Provide the password "{pwd}"')
    def enter_login_creds(context, pwd):
        try:
            context.loginPage.enter_password(pwd)
        except:
            context.driver.close()
            assert False, "Test is failed in enter password"

    @then(u'Login is failed and disable button is displayed')
    def validate_empty_username(context):
        try:
            context.loginPage.validade_disabled_login_button()
        except:
            context.driver.close()
            assert False, "Test is failed in validate empty username"

    @then(u'Select test profile')
    def select_test_profile(context):
        try:
            context.profilePage.select_test_profile()
        except:
            context.driver.close()
            assert False, "Test is failed in selecting test profile"

    @then(u'Select movie')
    def select_movie(context):
        try:
            context.dashboardPage.select_movie()
        except:
            context.driver.close()
            assert False, "Test has failed in selecting movie in dashboard"

    @then(u'Play Movie')
    def play_movie(context):
        try:
            context.moviePage.play_movie()
        except:
            context.driver.close()
            assert False, "Test has failed in playing movie in movie page"

    @then(u'Go to previous page')
    def previous_page(context):
        try:
            context.driver.execute_script("window.history.go(-1)")
        except:
            context.driver.close()
            assert False, "Test has failed in going to previous page"

    @then(u'Logout')
    def logout(context):
        try:
            context.dashboardPage.logout()
        except:
            context.driver.close()
            assert False, "Test has failed in logout"

    @then(u'Close the browser')
    def step_impl(context):
        context.driver.close()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('main', '0004_show_eps'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='show',
            name='episodes',
        ),
    ]


#!/usr/bin/python
from asyncio.subprocess import PIPE
from email.mime import base
import sys
import os
import subprocess
import base64
import json
import pprint
import argparse

# Assumes environment is set up properly with ETCDCTL_ENDPOINTS, ETCDCTL_CERT, ETCDCTL_CACERT, ETCDCTL_KEY
parser = argparse.ArgumentParser(description="Inventory an etcd instance and report count and size of all key/value pairs")
parser.add_argument('--depth', type=int, help="Maximum depth of results to report")
parser.add_argument('--prefix', help="Report only on matching prefixes")
parser.add_argument('--filter', help="Report only on keys containing filter")
parser.add_argument('input_file', type=argparse.FileType('r'), nargs="?", help="Use JSON formmated input file instead of querying etcd")

arguments = parser.parse_args()

if arguments.input_file is None:
    etcd = subprocess.run(['etcdctl', 'get', '--prefix', '/', '--write-out=json', '--dial-timeout=5m', '--command-timeout=5m'],
                      check=True, stdout=PIPE)
    etcd_out = etcd.stdout.decode()
else:
    etcd_file = open(sys.argv[1], "r")
    etcd_out = etcd_file.read()
    etcd_file.close()
etcd_keys = json.loads(etcd_out)

totals = {}

for entry in etcd_keys['kvs']:
    key = base64.b64decode(entry['key']).decode()
    size = len(base64.b64decode(entry['value']))
    split_key = key.split('/')
    key_so_far = ""
    depth = -1
    for nesting in split_key:
        depth += 1
        key_so_far += nesting
        if depth < len(split_key):
            key_so_far += "/"
        if key_so_far not in totals.keys():
            totals[key_so_far] = {}
            totals[key_so_far]['count'] = 0
            totals[key_so_far]['size']  = 0
            totals[key_so_far]['depth'] = depth
        totals[key_so_far]['count'] += 1
        totals[key_so_far]['size']  += size

if arguments.depth is not None:
    totals = { key:attribs for (key,attribs) in totals.items() if attribs['depth'] <= arguments.depth }

if arguments.prefix is not None:
    totals = { key:attribs for (key,attribs) in totals.items() if key.startswith(arguments.prefix)  }

if arguments.filter is not None:
    totals = { key:attribs for (key,attribs) in totals.items() if arguments.filter in key }

pprint.pprint(totals)


import torch.nn as nn
import torch

def conv(in_channels, out_channels):
    return nn.Sequential(
        nn.Conv2d(in_channels, out_channels, 3, padding=1),  # 3은 kernel size
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True),
        nn.Conv2d(out_channels, out_channels, 3, padding=1),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )

class plain_unet(nn.Module):
    def __init__(self, n_classes=50):
        super(plain_unet, self).__init__()
        self.n_classes = n_classes
        self.convDown1 = conv(9, 64)
        self.convDown2 = conv(64, 128)
        self.convDown3 = conv(128, 256)
        self.convDown4 = conv(256, 512)
        self.convDown5 = conv(512, 1024)
        self.maxpool = nn.MaxPool2d(2, stride=2)

        self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
        self.convUp4 = conv(1024+512, 512)
        self.convUp3 = conv(512+256, 256)
        self.convUp2 = conv(256+128, 128)
        self.convUp1 = conv(128+64, 64)
        self.convUp_fin = nn.Conv2d(64, self.n_classes, 1)

    def forward(self, x):
        conv1 = self.convDown1(x)
        x = self.maxpool(conv1)
        conv2 = self.convDown2(x)
        x = self.maxpool(conv2)
        conv3 = self.convDown3(x)
        x = self.maxpool(conv3)
        conv4 = self.convDown4(x)
        x = self.maxpool(conv4)

        conv5 = self.convDown5(x)
        print("conv5: ",conv5.shape)
        x = self.upsample(conv5)
        print("after upsample: ", x.shape)
        x = torch.cat((x,conv4), dim=1)

        x = self.convUp4(x)
        print("conv5: ",conv5.shape)
        x = self.upsample(x) #1, 512, 64, 64
        x = torch.cat((x,conv3), dim=1)

        x = self.convUp3(x)
        x = self.upsample(x) #1, 256, 128, 128
        x = torch.cat((x,conv2), dim=1)

        x = self.convUp2(x)
        x = self.upsample(x) #1, 128, 256, 256
        x = torch.cat((x,conv1), dim=1)

        x = self.convUp1(x) #1, 64, 256, 256

        out = self.convUp_fin(x) #1, 50, 256, 256
        return out


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import typing as tg
import numpy as np

from modules import *

class MLP(object):
    """
    This class implements a Multi-layer Perceptron in NumPy.
    It handles the different layers and parameters of the model.
    Once initialized an MLP object can perform forward and backward.
    """

    def __init__(self, n_inputs: int, n_hidden: tg.List[int], n_classes: int):
        """
        Initializes MLP object.

        Parameters
        -----------
        n_inputs : int
            number of inputs.
        n_hidden : list of int
            specifies the number of units in each linear layer.
            If the list is empty, the MLP will not have any
            linear layers, and the model will simply perform
            a multinomial logistic regression.
        n_classes: int
            number of classes of the classification problem.
            This number is required in order to specify the
            output dimensions of the MLP
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        self.modules = []
        # getting all dims
        dims = [n_inputs] + n_hidden + [n_classes]
        # handle first layer specifically
        self.modules.append(LinearModule(n_inputs, dims[1], True))
        # handle hidden layers + output
        for i in range(1, len(dims) - 1):
            self.modules.append(ReLUModule())
            self.modules.append(LinearModule(dims[i], dims[i + 1]))
        # handle last layer (diff activation)
        self.modules.append(SoftMaxModule())
        #######################
        # END OF YOUR CODE    #
        #######################

    def forward(self, x: np.ndarray):
        """
        Performs forward pass of the input.
        Here an input tensor x is transformed through
        several layer transformations.

        Parameters
        ----------
        x : np.ndarray
            (n_samples, n_input) input to the network

        Returns
        -------
        out : np.ndarray
            (n_samples, n_output) outputs of the network
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        x = x.reshape(x.shape[0], -1)
        for module in self.modules:
            x = module.forward(x)
        out = x
        #######################
        # END OF YOUR CODE    #
        #######################

        return out

    def backward(self, dout: np.ndarray):
        """
        Performs backward pass given the gradients of the loss.

        Parameters
        ----------
        dout : np.ndarray
            gradients of the loss
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        for module in reversed(self.modules):
            dout = module.backward(dout)
        #######################
        # END OF YOUR CODE    #
        #######################

    def clear_cache(self):
        """
        Remove any saved tensors for the backward pass from any module.
        Used to clean-up model from any remaining input data when we want to save it.
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        for module in self.modules:
            module.clear_cache()
        #######################
        # END OF YOUR CODE    #
        #######################

# if __name__ == "__main__":
#     mlp = MLP(784, [3, 5, 6], 10)
#     x = np.random.randn(7, 784)
#     y = mlp.forward(x)
#     # simulate backprop
#     dout = np.random.randn(7, 10)
#     mlp.backward(dout)
#     print(f"forward output shape: {y.shape}")
#     print(f"forward output: {y}")
#     print(f"forward output sum over axis 1: {y.sum(axis=1)}")
#     print(f"first layer bias shape {mlp.modules[2].params['bias'].shape}")
#     print(f"first layer bias grad shape {mlp.modules[2].grads['bias'].shape}")
#     print(f"first layer weight shape {mlp.modules[2].params['weight'].shape}")
#     print(f"first layer weight grad shape {mlp.modules[2].grads['weight'].shape}")


from __future__ import absolute_import

import numpy as np
import warnings

from .base import Model

class PyTorchModel(Model):
    """Creates a :class:`Model` instance from a `PyTorch` module.
    Parameters
    ----------
    model : `torch.nn.Module`
        The PyTorch model that should be attacked.
    num_classes : int
        Number of classes for which the model will output predictions.
    channel_axis : int
        The index of the axis that represents color channels.
    device : string
        A string specifying the device to do computation on.
        If None, will default to "cuda:0" if torch.cuda.is_available()
        or "cpu" if not.
    preprocessing: 2-element tuple with floats or numpy arrays
        Elementwises preprocessing of input; we first subtract the first
        element of preprocessing from the input and then divide the input by
        the second element.
    """

    def __init__(
            self,
            model,
            num_classes,
            device=None):

        # lazy import
        import torch

        super(PyTorchModel, self).__init__()

        self._num_classes = num_classes

        if device is None:
            self.device = torch.device(
                "cuda:0" if torch.cuda.is_available() else "cpu")
        elif isinstance(device, str):
            self.device = torch.device(device)
        else:
            self.device = device
        self._model = model.eval().to(self.device)

        if model.training:
            warnings.warn(
                'The PyTorch model is in training mode and therefore might'
                ' not be deterministic. Call the eval() method to set it in'
                ' evaluation mode if this is not intended.')

    def forward(self, inputs):
        # lazy import
        import torch

        # inputs, _ = self._process_input(inputs)
        n = len(inputs)
        inputs = torch.from_numpy(inputs).to(self.device)

        predictions = self._model(inputs)
        predictions = predictions.detach().cpu().numpy()

        assert predictions.ndim == 2
        assert predictions.shape == (n, self.num_classes())
        return predictions

    def evaluate(self, test_dataset):
        pass

    def num_classes(self):
        return self._num_classes


from django.urls import path
from .views import *

urlpatterns = [
	path('', crud_index, name='crud_index'),
	path('nuevoAlumno', crud_nuevoAlumno, name='crud_nuevoAlumno'),
	path('updateAlumno/<int:rutIn>/', crud_updateAlumno, name='crud_updateAlumno'),
	path('deleteAlumno/<int:rutIn>/', crud_deleteAlumno, name='crud_deleteAlumno'),

	path('nuevoProfesor', crud_nuevoProfesor, name='crud_nuevoProfesor'),
	path('updateProfesor/<int:rutIn>/', crud_updateProfesor, name='crud_updateProfesor'),
	path('deleteProfesor/<int:rutIn>/', crud_deleteProfesor, name='crud_deleteProfesor'),

	path('nuevaPrueba', crud_nuevaPrueba, name='crud_nuevaPrueba'),
	path('updatePrueba/<int:idPruebaIn>/', crud_updatePrueba, name='crud_updatePrueba'),
	path('deletePrueba/<int:idPruebaIn>/', crud_deletePrueba, name='crud_deletePrueba'),

	path('nuevoCurso', crud_nuevoCurso, name='crud_nuevoCurso'),
	path('updateCurso/<int:idCursoIn>/', crud_updateCurso, name='crud_updateCurso'),
	path('deleteCurso/<int:idCursoIn>/', crud_deleteCurso, name='crud_deleteCurso'),

	path('nuevoCursoAlumno', crud_nuevoCursoAlumno, name='crud_nuevoCursoAlumno'),
	path('updateCursoAlumno/<int:idCAIn>/', crud_updateCursoAlumno, name='crud_updateCursoAlumno'),
	path('deleteCursoAlumno/<int:idCAIn>/', crud_deleteCursoAlumno, name='crud_deleteCursoAlumno'),

	path('nuevoCursoPruebaAlumno', crud_nuevoCursoPruebaAlumno, name='crud_nuevoCursoPruebaAlumno'),
	path('updateCursoPruebaAlumno/<int:idCursoAlumnoAIn>/', crud_updateCursoPruebaAlumno, name='crud_updateCursoPruebaAlumno'),
	path('deleteCursoPruebaAlumno/<int:idCursoAlumnoAIn>/', crud_deleteCursoPruebaAlumno, name='crud_deleteCursoPruebaAlumno'),

	path('listarAlumnosPromedio', listaPromedio, name='listaPromedio'),
	path('listarPromedioRojo', listaPromediosRojos, name='listaPromediosRojos'),
]


#==========================================
# Author: Shierene Cervantes
#==========================================
# python script

import pandas as pd
tr_data = pd.read_csv('categorization_data.csv', encoding="latin1")

tr_data['title_and_description'] = tr_data[['Title', 'Description']].apply(tuple, axis=1)

tr_data.to_csv('categorizationdata_withtad.csv')

from io import StringIO
col = ['CATEGORY', 'title_and_description']
tr_data = tr_data[col]
tr_data = tr_data[pd.notnull(tr_data['title_and_description'])]
tr_data.columns = ['CATEGORY', 'title_and_description']
tr_data['Number'] = tr_data['CATEGORY'].factorize()[0]
category_id_df = tr_data[['CATEGORY', 'Number']].drop_duplicates().sort_values('Number')
category_to_id = dict(category_id_df.values)
id_to_category = dict(category_id_df[['Number', 'CATEGORY']].values)
tr_data.head()

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,6))
tr_data.groupby('CATEGORY').title_and_description.count().plot.bar(ylim=0)
plt.show()

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
features = tfidf.fit_transform(tr_data.title_and_description).toarray()
labels = tr_data.Number
features.shape

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC

X_train, X_test, y_train, y_test = train_test_split(tr_data['title_and_description'], tr_data['CATEGORY'], random_state = 0)
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
clf = MultinomialNB().fit(X_train_tfidf, y_train)
svc = LinearSVC().fit(X_train_tfidf, y_train)

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from sklearn.model_selection import cross_val_score
models = [
    RandomForestClassifier(n_estimators=200, max_depth=3, random_state=0),
    LinearSVC(),
    MultinomialNB(),
    LogisticRegression(random_state=0),
]
CV = 2
cv_df = pd.DataFrame(index=range(CV * len(models)))
entries = []
for model in models:
  model_name = model.__class__.__name__
  accuracies = cross_val_score(model, features, labels, scoring='accuracy', cv=CV)
  for fold_idx, accuracy in enumerate(accuracies):
    entries.append((model_name, fold_idx, accuracy))
cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'])
import seaborn as sns
sns.boxplot(x='model_name', y='accuracy', data=cv_df)
sns.stripplot(x='model_name', y='accuracy', data=cv_df,
              size=8, jitter=True, edgecolor="gray", linewidth=2)
plt.show()

cv_df.groupby('model_name').accuracy.mean()

model = LinearSVC()
X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, tr_data.index, test_size=0.4, random_state=0)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
fig, ax = plt.subplots(figsize=(10,10))
sns.heatmap(conf_mat, annot=True, fmt='d',
            xticklabels=category_id_df.CATEGORY.values, yticklabels=category_id_df.CATEGORY.values)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show()

from sklearn import metrics
print(metrics.classification_report(y_test, y_pred, target_names=tr_data['CATEGORY'].unique()))

test_set = pd.read_csv('categorizationdata_withtad.csv', encoding="latin1")
test_set

test_set['Combined MNB New Predicted Values'] = clf.predict(count_vect.transform(test_set['title_and_description'].values.astype('U')))

test_set['Combined SVC New Predicted Values'] = svc.predict(count_vect.transform(test_set['title_and_description'].values.astype('U')))

test_set['Title MNB New Predicted Values'] = clf.predict(count_vect.transform(test_set['Title'].values.astype('U')))

test_set['Title SVC New Predicted Values'] = svc.predict(count_vect.transform(test_set['Title'].values.astype('U')))

test_set['comparetitlesvc'] = test_set['Combined SVC New Predicted Values'] == test_set['CATEGORY']

test_set['comparetitlesvc'].value_counts(normalize=True)*100

test_set.to_csv('ModelComparison_SVC_MNB.csv')


import scipy
import numpy as np
import pandas as pd

from matplotlib.pylab import plt

def testPandas():
    df = pd.DataFrame()
    df['x'] = [1,2,3,4,5]
    xPlot = df['x']  # pandas series

    plt.plot(xPlot)
    plt.show()

def runFilter():

    # make sample data
    xRaw = np.random.normal(size=1000)
    yRaw = np.random.normal(size=1000)

    # make a tmp dataframe (like line annotations)
    df = pd.DataFrame()
    df['x'] = xRaw
    df['y'] = yRaw

    # this is what your code is doing
    xPlot = df['x']
    yPlot = df['y']

    # given two 1D arrays, stack them and transpoe to get
    # two columns corresponding to (x,y)
    xyData = np.vstack((xPlot, yPlot)).T

    print(xyData.shape) # (1000,2)

    width = 5  # box with for median filter (must be odd)
    yFiltered = scipy.ndimage.median_filter(xyData, width)

    plt.plot(xPlot, yPlot, '.-k')  # raw data
    #plt.plot(yFiltered, '.-r')
    plt.show()

def filterOneSegment(segmentID : int, medianFilterWidth : int = 5):
    """

    Args:
        width: box with for median filter (must be odd)
            # width is the number of points (pixels) to consider for each median filer
    """

    import pymapmanager
    path = '../PyMapManager-Data/one-timepoint/rr30a_s0_ch2.tif'
    stack = pymapmanager.stack(path)
    la = stack.getLineAnnotations()

    dfSegment = la.getSegment(segmentID)

    xPlot = dfSegment['x']
    yPlot = dfSegment['y']

    # transpose rotates to the "right"
    # by convention, to the "right" is -90
    #xyData = np.vstack((xPlot, yPlot)).T

    xPlotFiltered = scipy.ndimage.median_filter(xPlot, medianFilterWidth)
    yPlotFiltered = scipy.ndimage.median_filter(yPlot, medianFilterWidth)

    #xPlotFiltered = xyFiltered[:,1]
    #yPlotFiltered = xyFiltered[:,0]

    # now continue your calculations of (xLeft, yLeft) and (xRight, yRight)

    plt.plot(xPlot, yPlot, '.-k')  # raw data
    plt.plot(xPlotFiltered, yPlotFiltered, '.-r')  # filtered data
    plt.show()

if __name__ == '__main__':
    #runFilter()
    #testPandas()

    segmentID = 0
    medianFilterWidth = 9
    filterOneSegment(segmentID, medianFilterWidth)


#!/usr/bin/env python3
import spacy
import sqlite3
import ast
import csv

def write_to_csv(fields, data, filename):
    with open(filename, 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fields)
        writer.writeheader()
        writer.writerows(data)

if __name__ == "__main__":
    conn = sqlite3.connect('/home/leander/Desktop/automatic_KB_construction/recipes/old_recipes/recipes1.db')
    c = conn.cursor()
    c.execute("SELECT * FROM Recipes;")
    rows = c.fetchall()
    nlp = spacy.load('en_core_web_trf')
    verbs = {}
    nouns = {}

    for recipe in rows:
        dictionary = ast.literal_eval(recipe[7])

        for key in dictionary:
            step = nlp(dictionary[key])
            sentences = list(step.sents)

            for sentence in sentences:
                for token in sentence:
                    word = token.lemma_.lower()

                    if token.pos_ == "VERB":
                        if word in verbs:
                            verbs[word] += 1
                        else:
                            verbs[word] = 1

                    elif token.pos_ == "NOUN":
                        if word in nouns:
                            nouns[word] += 1
                        else:
                            nouns[word] = 1

    sorted_verbs = dict(sorted(verbs.items(), key=lambda item: item[1], reverse=True))
    csv_verb = []
    for verb_k in sorted_verbs:
        csv_verb.append({'Verb': verb_k, 'Counter': sorted_verbs[verb_k]})

    sorted_nouns = dict(sorted(nouns.items(), key=lambda item: item[1], reverse=True))
    csv_noun = []
    for noun_k in sorted_nouns:
        csv_noun.append({'Noun': noun_k, 'Counter': sorted_nouns[noun_k]})

    write_to_csv(list(csv_verb[0].keys()), csv_verb, 'verbs_in_all_recipes.csv')
    write_to_csv(list(csv_noun[0].keys()), csv_noun, 'nouns_in_all_recipes.csv')


import librosa
import pandas as pd
import soundfile as sf
import matplotlib.pyplot as plt
import numpy as np
import librosa.display as ld
import os
import yaml

df = pd.read_csv('./data/file_dictionary.csv')

with open('./data/looper.yaml', 'r') as yams:
    storps = yaml.full_load(yams)

start = storps['start']
end = storps['end']

df =  df.loc[start:end, :]

dic = {}
dic['start'] = (start + 5)
dic['end'] = (end + 5)

with open('./data/looper.yaml', 'w') as yams:
    yaml.dump(dic, yams)

def make_jpg(data, path, title, new_file_name):
    fig = plt.figure(figsize=[1,1])
    ax = fig.add_subplot(111)
    ax.axes.get_xaxis().set_visible(False)
    ax.axes.get_yaxis().set_visible(False)
    ax.set_frame_on(False)
    D = librosa.amplitude_to_db(np.abs(librosa.stft(data)), ref=np.max)
    ld.specshow(D, y_axis='linear')
    try:
        os.makedirs(f'data/audio_images/{path}/{title}/')
    except:
        pass
    plt.savefig(f'data/audio_images/{path}/{title}/{new_file_name}', dpi=500, bbox_inches='tight',pad_inches=0)
    plt.close()
    del D, fig
def generate_data(language, title, file):
    if language == 1:
        path = 'English/english'
    else:
        path = 'German/german'
    try:
        filename = f'./data/{path}/{title}/{file}'
        data, samplerate = sf.read(filename, dtype='float32')
        sf.close()
    except:
        pass
    try:
        data = data[:, 1]
    except:
        pass
    return data, samplerate, path
def generate_time_slice(sec, data1, samplerate):
    data2 = data1[(sec*15)*samplerate : ((sec+1)*15)*samplerate]
    data2 = np.asfortranarray(data2)
    return data2
def generate_path(file, sec):
    if file.endswith('o.ogg'):
        marker = 0
    elif file.endswith('1.ogg'):
        marker = 1
    else:
        marker = 2
    new_file_name = f'{marker}_{sec*15}-{sec*15 + 15}_audio_to_img.jpg'
    return new_file_name

for j, row in df.iterrows():

    file = df['Filename'][j]
    language = df['Language'][j]
    title = df['Title'][j]

    try:
        data, samplerate, path = generate_data(language, title, file)

        try:
            count = 0
            for i in range(20):
                data2 = generate_time_slice(i, data, samplerate)
                new_file_name = generate_path(file, i)

                if len(data2/samplerate) > 10:
                    make_jpg(data2, path, title, new_file_name)
                    count += 1
                else:
                    pass
                del data2
            print(f'Count={count}, File: {title}, new Index: df.loc[{j+1}:, :]')

        except:
            print(f'File: {title}, No usable data')
        del data, samplerate, path
    except:
        print(f'Data for {title} corrupted or unusuable')


# -*- coding:utf-8 -*-
# @Author : Bo Zhao
# @Time   : 12/1/2022 6:21 PM

import requests
from bs4 import BeautifulSoup
import json

def scrape_country(country,url,day = 'today'):

    print('Scraping...')
    '''
    request the whole website content in url
    '''
    response = requests.post(url).text
    print('Scraping finished!')
    print('--------------------')
    identity = 'main_table_countries_' + day  # id of desired contents
    soup = BeautifulSoup(response, 'html.parser')    # parsing the HTML documents using html.parser
    today = soup.find('table', attrs={'id': identity})  # extract the contents that has covid data
    data_today = today.tbody.find_all('tr')  # extract all data from 'tr' subsection

    data = [] # detailed data of all country
    for index in range(1, len(data_today)):
        r = []
        for tr in data_today[index].find_all('td'):
            r.append(tr.text.replace('\n', '').strip())
        data.append(r)
    data = data[7:]

    head = today.thead.find_all('th') # find all the header of the table
    header = []
    for i in head:
        header.append(i.text.replace('\n', '').strip())
    '''
    use table header as key and detailed covid data as value to creat a dictionary for each country
    put every dictionary into a list
    '''
    data_dic_all = []
    for i in data:
        data_dic = {}
        for j in range(len(header)):
            data_dic[header[j]] = i[j]
            if len(data_dic) == 22:
                data_dic['TotCases/1M pop'] = data_dic['Tot Cases/1M pop']
                del data_dic['Tot Cases/1M pop']
        data_dic_all.append(data_dic)
    '''
    find out the data of the input country
    print out the raw data and normalized data
    '''
    total_num = 0
    total_num_relative = 0
    country_name = ''
    country_data = {}
    for i in data_dic_all:
        if i['Country,Other'].upper().replace(' ','').strip() == country.upper().replace(' ','').strip():
            total_num = i['TotalCases']
            country_name = i['Country,Other']
            #total_num_relative = i['Tot Cases/1M pop']
            total_num_relative = i['TotCases/1M pop']
            country_data = i

    del country_data['#']
    # print(country_name,':')
    # print('Total Cases:',total_num)
    # print('Total Cases / 1M Population:',total_num_relative)
    '''
    save the json file
    '''
    if day == 'today':
        with open('today.json','w') as f:
            f.write(json.dumps(data_dic_all))
    if day == 'yesterday':
        with open('yesterday.json','w') as f:
            f.write(json.dumps(data_dic_all))
    if day == 'yesterday2':
        with open('two_days_ago.json','w') as f:
            f.write(json.dumps(data_dic_all))
    '''return a dictionary of input country'''
    return country_data

if __name__ == '__main__':
    url = 'https://www.worldometers.info/coronavirus/'
    country = 'Turkey'
    day = 'today'  # valid input of day: 'today'(default)/ 'yesterday'/ 'yesterday2'
    data = scrape_country(country, url, day)
    # print(data)


from plugins import query, admin

from pyrogram import Client, filters
from pyrogram.enums import ChatAction, ParseMode

@Client.on_message(filters.private & filters.user(admin) & filters.command("help"))
async def help_message(client, message):
    chat_id = message.chat.id

    help_message = """⚡ **اپشن های موجود** ⚡
        \n☔ اضافه کردن پیام به ربات با کامند     /add_msg
        \n☔ حذف کردن پیام از ربات با کامند       /del_msg
        \n☔ نمایش جمسملات موجود با کامند       /show_msg
        \n☔ نمایش وضعیت ربات     /info
        \n☔ چک کردن وضعیت ربات با کامند      /status
        \n☔ فوروارد کردن پیام       /forward
        \b☔ جوین شدن داخل گروه مورد نظر     /join_chat
        \n☔ ست کردن پیام بعد از جوین شدن ربات در گروه       /join_msg
        \n.
    """

    await client.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)
    await message.reply(text=help_message,parse_mode=ParseMode.HTML)

@Client.on_message(filters.private & filters.user(admin) & filters.command("add_msg"))
async def add_msg(client, message):
    chat_id = message.chat.id
    messages = """⚡ **اضافه کردن پیام** ⚡
        \nلطفا جمله یا جملات خود را ارسال کنید و در انتها کامند زیر را ارسال کنید:
        \n/save
    """

    await client.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)
    await message.reply(text=messages)

    query.setbit("add:msg", 0, 1)

@Client.on_message(filters.private & filters.user(admin), group=1)
async def get_input(client, message):
    msg = message.text

    if query.getbit("add:msg", 0) == 1 and msg and not msg.startswith("/"):
        msg_counter = query.incr("New:Msg")
        query.sadd("Messages", f"{msg_counter}:{msg}")

    elif msg == "/save":
        chat_id = message.chat.id
        await client.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)
        await message.reply("""⚡ **اضافه کردن پیام** ⚡
            \n✅ جمله های شما با موفقیت ذخیره شد
            \n.
        """)

        query.setbit("add:msg", 0, 0)

@Client.on_message(filters.private & filters.user(admin) & filters.command("show_msg"))
async def show_msg(client, message):
    chat_id = message.chat.id

    await client.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)

    if query.exists("Messages"):
        messages = "⚡ **نمایش جمله ها** ⚡\n"
        data = sorted(query.smembers("Messages"), key=lambda x: x.split(":")[0])

        for item in data:
            item = item.split(":", 1)
            separator = "➖" * 12
            messages += f"`{item[0]}`:{item[1]}\n{separator}\n"
        await message.reply(messages)

    else:
        await message.reply("❌جمله ای یافت نشد❌")

# @Client.on_message(filters.private & filters.user(admin) & filters.command("del_msg"))
# async def del_msg(c, m):
#     chat_id = m.chat.id

#     await c.send_chat_action(chat_id=chat_id, action="typing")

#     if r.exists("Messages"):

#         message = """⚡ **حذف کردن جمه ها** ⚡
#             \nلطفا شماره جمله مورد نظر خود ارسال کنید. 🗑
#             \nو یا برای حذف همه جمله ها  `*` را ارسال نمایید
#             \n.
#         """

#         await c.send_message(chat_id=chat_id, text=message)

#         r.setbit("del:msg", 0, 1)

#     else:
#         await c.send_message(chat_id=chat_id, text="❌جمله ای یافت نشد❌")

# @Client.on_message(filters.private & filters.user(admin), group=2)
# async def get_message_id(c, m):

#     msg = m.text

#     if r.getbit("del:msg", 0) == 1 and msg and not msg.startswith("/"):
#         chat_id = m.chat.id
#         await c.send_chat_action(chat_id=chat_id, action="typing")

#         if msg == "*":
#             r.delete("Messages")
#             r.setbit("del:msg", 0, 0)
#             await c.send_message(chat_id=chat_id, text="""⚡ **حذف همه پیام ها** ⚡\n
#                 \nتمامی جملات شما با موفقیت حذف شد ✅"""
#             )

#         else:
#             msg_number = r.sscan("Messages", match=f"{msg}:*")[1]

#             if msg_number:
#                 r.srem("Messages", msg_number[0])
#                 r.setbit("del:msg", 0, 0)
#                 await c.send_message(chat_id=chat_id, text="""⚡ **حذف پیام** ⚡\n
#                     \n جمله شما با موفقیت حذف شد ✅"""
#                 )

#             else:
#                 messages = "⚡ **حذف پیام** ⚡\n\n❌جمله ای یافت نشد❌"

#                 await c.send_message(chat_id=chat_id, text=messages)


import numpy as np
import pandas as pd
import sys, os, click, glob, tqdm, shutil

@click.command()
@click.option(
    "-u",
    "--run_id",
    "run_id",
    required=True,
    envvar="COVID_RUN_INDEX",
    help="run_id_to convert from",
)
@click.option(
    "-f",
    "--input_folder",
    "input_folder",
    required=True,
    help="Folder where the initial run is synced. There should be a model_ouput in it",
)
def cli(run_id, input_folder):
    return run_id, input_folder

# https://stackoverflow.com/questions/2793789/create-destination-path-for-shutil-copy-files
def copy_path(*, src, dst, dir_mode=0o777, follow_symlinks: bool = True):
    """
    Copy a source filesystem path to a destination path, creating parent
    directories if they don't exist.

    Args:
        src: The source filesystem path to copy. This must exist on the
            filesystem.

        dst: The destination to copy to. If the parent directories for this
            path do not exist, we will create them.

        dir_mode: The Unix permissions to set for any newly created
            directories.

        follow_symlinks: Whether to follow symlinks during the copy.

    Returns:
        Returns the destination path.
    """
    try:
        return shutil.copy2(src=src, dst=dst, follow_symlinks=follow_symlinks)
    except FileNotFoundError as exc:
        if exc.filename == dst and exc.filename2 is None:
            parent = os.path.dirname(dst)
            os.makedirs(name=parent, mode=dir_mode, exist_ok=True)
            return shutil.copy2(
                src=src,
                dst=dst,
                follow_symlinks=follow_symlinks,
            )
        raise

def detect_old_run_id(fp):
    return fp.split("/inference/med/")[-1].split("/")[0]

if __name__ == "__main__":
    # standalone_mode: so click doesn't exit, see
    # https://stackoverflow.com/questions/60319832/how-to-continue-execution-of-python-script-after-evaluating-a-click-cli-function
    run_id, input_folder = cli(standalone_mode=False)
    print(run_id, input_folder)
    if not os.path.isdir(f"{input_folder}/model_output"):
        raise ValueError(
            f"Could not find folder {input_folder}/model_output, are you sure -f option is set correctly ?"
        )
    files = glob.glob(input_folder + "/**/*.parquet", recursive=True)
    os.makedirs("model_output/cont", exist_ok=True)

    fn = files[0]
    old_run_id = detect_old_run_id(fn)
    new_name = (
        fn.replace("seir", "cont").replace(f"{input_folder}/model_output", "model_output").replace(old_run_id, run_id)
    )

    print(f"detected old_run_id: {old_run_id} which will be replaced by user provided run_id: {run_id}")
    empty_str = "°" * len(input_folder)
    print(f"file: \n OLD NAME: {fn}\n NEW NAME: {empty_str}{new_name}")
    for fn in tqdm.tqdm(files):
        old_run_id = detect_old_run_id(fn)
        new_name = (
            fn.replace("seir", "cont")
            .replace(f"{input_folder}/model_output", "model_output")
            .replace(old_run_id, run_id)
        )
        copy_path(src=fn, dst=new_name)

    # os.makedirs(outdir, exist_ok=True)


# pylint:disable=missing-class-docstring
import angr

from ...sim_type import SimStruct, SimTypeLong, SimTypeFixedSizeArray, SimTypeShort, SimTypeInt

# struct sysinfo {
#    long uptime;             /* Seconds since boot */
#    unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
#    unsigned long totalram;  /* Total usable main memory size */
#    unsigned long freeram;   /* Available memory size */
#    unsigned long sharedram; /* Amount of shared memory */
#    unsigned long bufferram; /* Memory used by buffers */
#    unsigned long totalswap; /* Total swap space size */
#    unsigned long freeswap;  /* Swap space still available */
#    unsigned short procs;    /* Number of current processes */
#    unsigned long totalhigh; /* Total high memory size */
#    unsigned long freehigh;  /* Available high memory size */
#    unsigned int mem_unit;   /* Memory unit size in bytes */
# }
sysinfo_ty = SimStruct(
    {
        "uptime": SimTypeLong(signed=True),
        "loads": SimTypeFixedSizeArray(SimTypeLong(signed=False), 3),
        "totalram": SimTypeLong(signed=False),
        "freeram": SimTypeLong(signed=False),
        "sharedram": SimTypeLong(signed=False),
        "bufferram": SimTypeLong(signed=False),
        "totalswap": SimTypeLong(signed=False),
        "freeswap": SimTypeLong(signed=False),
        "procs": SimTypeShort(signed=False),
        "totalhigh": SimTypeLong(signed=False),
        "freehigh": SimTypeLong(signed=False),
        "mem_unit": SimTypeInt(signed=False),
    },
    name="sysinfo",
    pack=False,
    align=None,
)

class sysinfo(angr.SimProcedure):
    def run(self, info):  # pylint: disable=arguments-differ
        value = {
            "uptime": 1234567,
            "loads": [20100, 22000, 15000],
            "totalram": 1024**2,
            "freeram": 1024**2 // 4,
            "sharedram": 1024**2 // 4,
            "bufferram": 1024**2 // 4,
            "totalswap": 1024**2,
            "freeswap": 1024**2 // 2,
            "procs": 533,
            "totalhigh": 11,
            "freehigh": 12,
            "mem_unit": 13,
        }
        sysinfo_ty.with_arch(self.arch).store(self.state, info, value)
        return 0


from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
url = 'https://spa2.scrape.center'
browser.get(url)
logo = browser.find_element(By.CLASS_NAME, 'logo-image')
print(logo)
print(logo.get_attribute('src'))

JavaScript动态渲染页面爬取/Selenium/获取节点信息/获取属性信息.py


from flask import url_for
from flask_testing import TestCase
from application import app, db
from application.models import users, games

class TestBase(TestCase):
    def create_app(self):
        app.config.update(
            SQLALCHEMY_DATABASE_URI = 'sqlite:///test.sqlite3',
            SECRET_KEY = 'testsecretkey',
            DEBUG_MODE = True,
            WTF_CSRF_ENABLED = False
        )
        return app

    def setUp(self):
        db.create_all()
        test_user = users(name="Andrei", email="andrei.chirovici@gmail.com")
        db.session.add(test_user)
        db.session.commit()

def tearDown(self):
        db.session.remove()
        db.drop_all()

class TestHome(TestBase):
    def test_home_get(self):
        response = self.client.get(url_for('home'))
        self.assert200(response)

class TestAddUser(TestBase):
    def test_register_page(self):
        response = self.client.get(url_for('register'))
        self.assert200(response)

    def test_register_user(self):
        response = self.client.post(
            url_for('register'),
            data = dict(name="Andrei", email="andrei.chirovici@gmail.com"),
            follow_redirects = True
        )
        self.assert200(response)


import numpy as np

from matplotlib import pyplot as plt
from scipy.optimize import curve_fit, root_scalar

def linear_fit(x, m, h):
    return m * x + h

data = np.loadtxt("exercises/scipy/data.txt")

x, linear, exponential = data.T

linear_params, linear_cov = curve_fit(linear_fit, x, linear)
log_params, log_cov = curve_fit(linear_fit, x, np.log(exponential))

def root_func(x):
    return linear_fit(x, *linear_params) - np.exp(linear_fit(x, *log_params))

res = root_scalar(root_func, x0=3.5, x1=4)

plt.plot(x, linear,
         linestyle="none",
         marker="o",
         label="Linear data")

plt.plot(x, exponential,
         linestyle="none",
         marker="s",
         label="Exponential data")

plt.plot(x, linear_fit(x, *linear_params),
         label="Linear fit")

plt.plot(x, np.exp(linear_fit(x, *log_params)),
         label="Exponential fit")

plt.annotate("Intersection",
             xy=[res.root, linear_fit(res.root, *linear_params)],
             xytext=[2, 8],
             arrowprops=dict(facecolor='black'))

plt.legend()
plt.show()


import requests
from bs4 import BeautifulSoup
import csv
import re
import string
import datetime

def get_url(result):
	for title in result.find_all('h3', class_="r"):
		for link in title.find_all('a'):
			url = link.get('href')
	return url

def get_title(result):
	for heading in result.find_all('h3', class_="r"):
		for link in heading.find_all('a'):
			title = link.text
			#title = str(title)
			title = title.encode("utf-8")
	return title

def get_description(result):
	description = result.find_all('span', class_="st")
	description = re.sub('<[^<]+?>','',str(description))
	#description = description.encode("utf-8")
	return description

prod_list_file = '/Users/ascodel/Google Drive/Google Product Scrape/prod_list.csv'

with open(prod_list_file, 'rb') as f:
	reader = csv.reader(f)
	#prod_cats = list(reader)
	prod_cats = map(tuple, reader)

date = datetime.date.today()

terms = []

for product in prod_cats:
	product_search = []

	for word in product:
		word = str(word)
		product_words = word.split()

		for word in product_words:
			word = word + "+"
			product_search.append(word)

	term = ''.join(product_search)
	term_1 = term + "federal+requirement"
	terms.append(term_1)
	#term_2 = term + "federal+purchasing"
	#terms.append(term_2)
	#term_3 = term +"FEMP"
	#terms.append(term_3)

output_file = '/Users/ascodel/Google Drive/SFO Group Files/FEMP EEPP/google_search_results_' + str(date) + '.csv'
dictionary = {'term': '', 'url': '', 'title': '', 'description':'','date': ''}
'''
term = 'femp'
r = requests.get("https://www.google.com/search?q=" + term)
soup = BeautifulSoup(r.text)
for result in soup.find_all('div',class_='g'):
	print result
'''

for term in terms:
	r = requests.get("https://www.google.com/search?q=" + term)
	soup = BeautifulSoup(r.text)

	#print term
	#url = "http://www.google.com/" + term
	#print url

	for result in soup.find_all('div', class_='g'):
		dictionary['date'] = date
		dictionary['term'] = term
		dictionary['url'] = get_url(result)
		dictionary['title'] = get_title(result)
		dictionary['description'] = get_description(result)

		with open(output_file, 'a') as csvfile:
			writer = csv.writer(csvfile)
			writer.writerow(dictionary.values())

		#with open(output_file, 'a') as csvfile:
		#	writer = csv.writer(csvfile)
		#	writer.writerow(dictionary.values())
			#writer = UnicodeWriter(csvfile)
			#writer.writerow(dictionary.values())
'''

	for result in soup.find_all('li', class_="_g"):
		dictionary['date'] = date
		dictionary['term'] = term
		dictionary['url'] = get_url(result)
		dictionary['title'] = get_title(result)
		dictionary['description'] = get_description(result)

		with open(output_file, 'a') as csvfile:
			writer = csv.writer(csvfile)
			writer.writerow(dictionary.values())

#print terms
	#product_words = str(product)
	#print product_words
	#product_words = product_words.split()
	#print product_words

	#product_search = []

	#for word in product_words:
		#print word
	#	word = word + "+"
	#	#print word
	#	product_search.append(word)

		#print product_search
		#print ''.join(product_search)
	#terms.append(''.join(product_search))
'''


from setuptools import setup

with open('README.md', 'r') as f:
    long_description = f.read()

setup(
  name='nu',
  version='0.2.0',
  packages=['nu'],
  author='Nyan Of The Moon',
  author_email='nyanofthemoon@gmail.com',
  description='Framework for creating Cozmo skills using multiple sensory inputs.',
  long_description=long_description,
  url='https://hotchiwawa.com',
  license='MIT',
  entry_points={
      'console_scripts': [
          'nu = nu.__main__:main'
      ]
  },
  install_requires=[
      'argparse',
      'configparser',
      'redis',
      'hiredis',
      'sense-hat',
      'cozmo[camera]',
      'SpeechRecognition',
      'PyAudio',
      'PocketSphinx',
      'python-Levenshtein',
      'google-api-python-client',
      'textblob',
      'flask',
      'pyopenssl'
  ],
  zip_safe=False
)


#!/usr/bin/env python
import vtk

# Enable printing for debugging
debugPrint = 0

# Test the ability to pack segmentation labels into
# contiguous runs. This is useful for discrete isocontouring
# filters such as vtkSurfaceNets3D, vtkDiscreteMarchingCubes,
# and vtkDiscreteFlyingEdges.

# Create a small volume with non-packed voxel values. The output
# data should be packed into unsigned char since there are less
# than 256 labels.
VTK_UCHAR = 3
VTK_INT = 6
xDim = 3
yDim = 3
zDim = 3

numVoxels = xDim * yDim * zDim
image = vtk.vtkImageData()
image.SetDimensions(xDim,yDim,zDim)
image.AllocateScalars(VTK_INT,1)

scalars = image.GetPointData().GetScalars()

# Populate the volume. Create a weird ordering,
# with label gaps and non-contiguous values.
for i in range(0,numVoxels,3):
    idx = numVoxels - i - 1;
    val = 100 + idx
    scalars.SetTuple1(idx,val)
    scalars.SetTuple1(idx-1,val)
    scalars.SetTuple1(idx-2,val)

scalars.SetTuple1(26,0) # make non-contiguous and non-increasing
scalars.SetTuple1(25,0)
scalars.SetTuple1(24,0)

# Print out data as constructed
if debugPrint:
    print("Input to pack labels:")
    for i in range(0,numVoxels):
        print(i,scalars.GetTuple1(i))

# Pack the labels. This should automatically pack into unsigned char.
pack = vtk.vtkPackLabels()
pack.SetInputData(image)
pack.Update()

# See what kind of label we have
labels = pack.GetLabels()
numLabels = labels.GetNumberOfTuples()
if debugPrint:
    print("\nNumber of resulting labels: ",labels.GetNumberOfTuples())
    for i in range(0,numLabels):
        print("Label: ", labels.GetTuple1(i))

# Look at the resulting volume
outScalars = pack.GetOutput().GetPointData().GetScalars()
dataType = outScalars.GetDataType()
if debugPrint:
    print("\nPoint scalars: ")
    for i in range(0,numVoxels):
        print(i,outScalars.GetTuple1(i))

# Now check for proper output
assert(dataType == VTK_UCHAR)
assert(numLabels == 9)
assert(labels.GetTuple1(0) == 0)
assert(labels.GetTuple1(numLabels-1) == 123)
assert(outScalars.GetTuple1(0) == 1)
assert(outScalars.GetTuple1(numVoxels-1) == 0)

# Now test sorting of packed labels by frequency count,
# and make sure that packing works for cell data as well.
numVoxelCells = (xDim-1) * (yDim-1) * (zDim-1)

cellScalars = vtk.vtkShortArray()
cellScalars.SetName("cellScalars")
cellScalars.SetNumberOfTuples(numVoxelCells)
cellScalars.Fill(0)
cellScalars.SetTuple1(7,1)
cellScalars.SetTuple1(6,2)
cellScalars.SetTuple1(5,2)
cellScalars.SetTuple1(3,3)
cellScalars.SetTuple1(2,3)
cellScalars.SetTuple1(1,3)

image.GetCellData().SetScalars(cellScalars)

if debugPrint:
    print("\nCell scalars input to pack labels:")
    for i in range(0,numVoxelCells):
        print(i,cellScalars.GetTuple1(i))

pack.SetInputArrayToProcess(0,0,0,vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS,"cellScalars")
pack.SortByLabelCount()
pack.Update()
outCellScalars = pack.GetOutput().GetCellData().GetScalars()
dataType = outCellScalars.GetDataType()

labels = pack.GetLabels()
counts = pack.GetLabelsCount()
numLabels = labels.GetNumberOfTuples()

if debugPrint:
    print("\nNumber of sorted cell labels with counts: ",labels.GetNumberOfTuples())
    for i in range(0,numLabels):
        print("Label: ", labels.GetTuple1(i), ", count: ", counts.GetTuple1(i))

if debugPrint:
    print("\nOutput cell scalars:")
    for i in range(0,numVoxelCells):
        print(i,outCellScalars.GetTuple1(i))

assert(dataType == VTK_UCHAR)
assert(numLabels == 4)
assert(counts.GetTuple1(0) == 3)
assert(counts.GetTuple1(3) == 1)
assert(labels.GetTuple1(0) == 3)
assert(labels.GetTuple1(3) == 1)


from flask import Flask
from flask_restful import Api,Resource

app = Flask(__name__)
api = Api(app)

weather_data = [
    {
        "location": "New York",
        "temperature": 12.3,
        "humidity": 60,
        "wind_speed": 6.7,
        "wind_direction": "NW",
        "description": "Cloudy"
    },
    {
        "location": "Los Angeles",
        "temperature": 22.8,
        "humidity": 45,
        "wind_speed": 3.2,
        "wind_direction": "SE",
        "description": "Sunny"
    },
    {
        "location": "London",
        "temperature": 8.9,
        "humidity": 75,
        "wind_speed": 9.4,
        "wind_direction": "SW",
        "description": "Rainy"
    },
    {
        "location": "Paris",
        "temperature": 13.5,
        "humidity": 55,
        "wind_speed": 5.6,
        "wind_direction": "NE",
        "description": "Partly cloudy"
    },
    {
        "location": "Tokyo",
        "temperature": 18.2,
        "humidity": 50,
        "wind_speed": 2.8,
        "wind_direction": "E",
        "description": "Clear"
    },
    {
        "location": "Sydney",
        "temperature": 26.7,
        "humidity": 35,
        "wind_speed": 10.2,
        "wind_direction": "W",
        "description": "Mostly sunny"
    },
    {
        "location": "Moscow",
        "temperature": -2.8,
        "humidity": 85,
        "wind_speed": 12.3,
        "wind_direction": "N",
        "description": "Snowy"
    },
    {
        "location": "Rio de Janeiro",
        "temperature": 29.5,
        "humidity": 70,
        "wind_speed": 4.5,
        "wind_direction": "SE",
        "description": "Partly cloudy"
    },
    {
        "location": "Cape Town",
        "temperature": 19.8,
        "humidity": 65,
        "wind_speed": 6.2,
        "wind_direction": "SW",
        "description": "Sunny"
    },
    {
        "location": "Toronto",
        "temperature": -5.6,
        "humidity": 50,
        "wind_speed": 8.9,
        "wind_direction": "NW",
        "description": "Snowy"
    }
]

class Weather(Resource):
    def get(self) :
        result = weather_data
        return result

#call
api.add_resource(Weather,"/Weather")

if __name__ == "__main__" :
    # Not recommend using in a big environment
    app.run(debug=True)


#coding: utf-8

import argparse
from readDataset import readDataset
from individual  import Individual
from population  import Population
from select      import Select
from crossover   import Crossover
from mutation    import Mutation
import six
if (six.PY2):
    from ga4py2  import GA
if (six.PY3):
    from ga4py3  import GA
from copy        import deepcopy

class Onemax(GA):
    def evaluate(self, ind, args):
        fitness = sum(ind)
        return (fitness)

    def revolution(self, args):
        #config
        if   (args.crossmode=="one"):
            cross_func = Crossover.onePoint
        elif (args.crossmode=="two"):
            cross_func = Crossover.twoPoints
        else:
            cross_func = Crossover.randomPoints

        # Prepare dataset
        dataset = readDataset(args.dpath)

        # Create individuals & population
        ppl = Population()
        for i in range(args.individuals):
            individual = Individual()
            individual.createGene(dataset, args.gene)
            ppl.addInd(individual)

        # Evolution
        for i in range(args.revolution):
            # Evaluation population in individuals
            ppl.calcFitness(self.evaluate)
            if ((i%10)==0):
                ppl.show()

            # Select parents
            if (args.elite):
                parents = Select.Elite(ppl, args.esize, args.mode)
                parents.extend(Select.Tournament(ppl, args.individuals-args.esize, args.tornsize, args.mode))
            else:
                parents = Select.Tournament(ppl, args.individuals, args.tornsize, args.mode)

            # Clossover
            children = cross_func(parents, args.individuals, args.gene, args.crossrate)

            # Mutation
            children = Mutation.mutation(children, args.mutaterate, dataset)
            # Swap children
            ppl.setPpl(children)

        # show result
        ppl.calcFitness(self.evaluate)
        ppl.show()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-d",        "--dpath",       type=str, default="./dataset/binary.txt", help="dataset path")
    parser.add_argument("-ind",      "--individuals", type=int, default=50,                     help="Number of individuals")
    parser.add_argument("-gene",     "--gene",        type=int, default=10,                     help="Length of gene")
    parser.add_argument("-revo" ,    "--revolution",  type=int, default=100,                    help="Number of revolution")
    parser.add_argument("-elite",    "--elite",                 default=False,                  help="Use elite selection", action="store_true")
    parser.add_argument("-esize",    "--esize",       type=int, default=2,                      help="Size of elite")
    parser.add_argument("-tornsize", "--tornsize",    type=int, default=3,                      help="Size of tournament")
    parser.add_argument("-mode",     "--mode",        type=str, default="max",                  help="Max or min")
    parser.add_argument("-crate",    "--crossrate",   type=float , default=0.7,                 help="probability of crossover")
    parser.add_argument("-cmode",    "--crossmode",   type=str, default="one",                  help="one or two or random")
    parser.add_argument("-mrate",    "--mutaterate",  type=float,  default=0.05,                 help="probability of mutation")
    args   = parser.parse_args()
    print("[argments list]")
    print(args)
    onemax = Onemax()
    onemax.revolution(args)


import copy
import itertools

def count_live_neighbors(w, x, y, z, layout):
    return sum(live(w+dw,x+dx,y+dy,z+dz,layout) if (dw != 0 or dx != 0 or dy != 0 or dz != 0) else 0 for dw,dx,dy,dz in itertools.product([-1,0,1], repeat=4))

def live(w, x,y,z,layout):
    if w >= len(layout) or w < 0:
        return 0
    if x >= len(layout[w]) or x < 0:
        return 0
    if y >= len(layout[w][x]) or y < 0:
        return 0
    if z >= len(layout[w][x][y]) or z < 0:
        return 0
    return 1 if layout[w][x][y][z] == '#' else 0

def prettyprint(layout):
    for layer in layout:
        print("LAYER")
        for sublayer in layer:
            print("SUBLAYER")
            for string in sublayer:
                print(string)

with open("input.txt") as inp:
    layout = [[[line.strip() for line in inp.readlines()]]]

for n in range(6):
    print(n)
    #prettyprint(layout)
    empty_layer = []
    for _ in range(len(layout)):
        empty_layer.append(["."*len(layout[0][0][0])]*len(layout[0][0]))
    layout.insert(0,copy.deepcopy(empty_layer))
    layout.append(copy.deepcopy(empty_layer))
    for layer in layout:
        layer.insert(0,copy.deepcopy(empty_layer[0]))
        layer.append(copy.deepcopy(empty_layer[0]))
    for layer in layout:
        for sublayer in layer:
            sublayer.insert(0,copy.deepcopy(empty_layer[0][0]))
            sublayer.append(copy.deepcopy(empty_layer[0][0]))
    for i,layer in enumerate(layout):
        for j,sublayer in enumerate(layer):
            for k,row in enumerate(sublayer):
                layout[i][j][k] = '.' + row + '.'

    new_layout = copy.deepcopy(layout)
    #prettyprint(layout)
    for i,w in enumerate(new_layout):
        #print("w {}", i)
        for j,x in enumerate(w):
            #print ("x {}", j)
            for k,y in enumerate(x):
                #print ("y {}", k)
                for l,z in enumerate(y):
                    neighbors = count_live_neighbors(i,j,k,l,layout)
                    if z == '#' and neighbors != 2 and neighbors != 3:
                        new_layout[i][j][k] = new_layout[i][j][k][:l] + '.' + new_layout[i][j][k][l+1:]
                    elif z != '#' and neighbors == 3:
                        new_layout[i][j][k] = new_layout[i][j][k][:l] + '#' + new_layout[i][j][k][l+1:]

    layout = new_layout

print(sum(sum(sum(y.count('#') for y in x) for x in w) for w in layout))


# -*- coding: utf-8 -*-
"""
Created on Sat Jan 29 10:09:16 2022

@author: 45414
"""

# Imports
import tensorflow as tf
import pandas as pd
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import splitfolders
from tensorflow.keras.layers import Dropout, Flatten, Dense, BatchNormalization
import matplotlib.pyplot as plt

path = 'C:/Users/45414/Desktop/Anvendt maskinelæring/DIDA/'

df = pd.read_csv(path + 'DIDA_12000_String_Digit_Labels.csv', names=["Index","Label"])

#%%
def append_ext(fn):
    return str(fn) + ".jpg"

df["nr"]=df["Label"].apply(append_ext)

# Create new labels for CC-D-Y models
# 18 century (0) or not (1)
def label_CC(label):

    x = str(label)
    if len(x) == 4:
        if x[0] == "1" and x[1] == "8":
            return str(0)
        else:
            return str(1)
    return str(1)

# Decade or residual (10)
def label_D(label):

    x = str(label)
    if len(x) != 4:
        return str(10)
    else:
        return str(x[2])

# Year or residual (10)
def label_Y(label):

    x = str(label)
    if len(x) != 4:
        return str(10)
    else:
        return str(x[3])

def filename(index):
   return str(index) + '.jpg'

df["CC"]=df["Label"].apply(label_CC)
df["D"]=df["Label"].apply(label_D)
df["Y"]=df["Label"].apply(label_Y)
df["Filename"]=df["Index"].apply(filename)

# Check the ratio of samples in each class:
print(df["CC"].value_counts()) # how many are there in each category
print(df["D"].value_counts()) # how many are there in each category
print(df["Y"].value_counts()) # how many are there in each category

input_folder = path + 'DIDA_12000_String_Digit_Images/'
splitfolders.ratio(input_folder, output= path + 'cell_image',
                   seed=42, ratio=(.7, .2, .1),
                   group_prefix=None) # default values

#%% CC
datagen=ImageDataGenerator(
    rescale=1./255.,
    rotation_range=5,
    zoom_range=0.2,
    horizontal_flip=True,
    vertical_flip=False
    )

datagen_test=ImageDataGenerator(rescale=1./255.)

train_generator_CC=datagen.flow_from_dataframe(
dataframe=df,
directory=path + "cell_image/train/DIDA_1/",
x_col="Filename",
y_col="CC",
batch_size=200,
seed=42,
shuffle=True,
class_mode="binary",
target_size=(32,64))

valid_generator_CC=datagen_test.flow_from_dataframe( #maybe use augmentation on val also?
dataframe=df,
directory= path + "cell_image/val/DIDA_1/",
x_col="Filename",
y_col="CC",
#subset="validation",
batch_size=32,
seed=42,
shuffle=True,
class_mode="binary",
target_size=(32,64))

test_generator_CC=datagen_test.flow_from_dataframe(
dataframe=df,
directory= path + "cell_image/test/DIDA_1/",
x_col="Filename",
y_col="CC",
batch_size=1200,
seed=42,
shuffle=False,
class_mode="binary",
target_size=(32,64))

#%% TRANSFER LEARNING MODEL CC

pre_trained_model = tf.keras.applications.MobileNetV2(
    input_shape=(32, 64, 3), # refers to the shape we transfer from
    include_top=False, # cut off the head
    weights='imagenet', # pretrained on the ImageNet data
)
for layer in pre_trained_model.layers: #freeze the base model to not train it
    layer.trainable=False
pre_trained_model.summary()
#%%

model_CC = tf.keras.models.Sequential([
    pre_trained_model, # the pre-trained part
    tf.keras.layers.Flatten(), # flatten before fully connected part
    tf.keras.layers.Dense(64, activation='relu'),
    BatchNormalization(),
    Dropout(0.5),
    tf.keras.layers.Dense(2, activation='sigmoid'),
    ])
model_CC.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
    )
model_CC.summary()
#%%

from tensorflow.keras import layers
STEP_SIZE_TRAIN=train_generator_CC.n//train_generator_CC.batch_size
STEP_SIZE_VALID=valid_generator_CC.n//valid_generator_CC.batch_size
STEP_SIZE_TEST=test_generator_CC.n//test_generator_CC.batch_size

history = model_CC.fit_generator(generator=train_generator_CC,
                    steps_per_epoch=STEP_SIZE_TRAIN,
                    validation_data=valid_generator_CC,
                    validation_steps=STEP_SIZE_VALID,
                    epochs=1
)
#%% D
train_generator_D=datagen.flow_from_dataframe(
dataframe=df,
directory= path + "cell_image/train/DIDA_1/",
x_col="Filename",
y_col="D",
batch_size=200,
seed=42,
shuffle=True,
class_mode="sparse",
target_size=(32,64))

valid_generator_D=datagen_test.flow_from_dataframe(
dataframe=df,
directory= path + "cell_image/val/DIDA_1/",
x_col="Filename",
y_col="D",
batch_size=32,
seed=42,
shuffle=True,
class_mode="sparse",
target_size=(32,64))

test_generator_D=datagen_test.flow_from_dataframe(
dataframe=df,
directory= path + "cell_image/test/DIDA_1/",
x_col="Filename",
y_col="D",
batch_size=1200,
seed=42,
shuffle=False,
class_mode="sparse",
target_size=(32,64))

#%% TRANSFER LEARNING MODEL D

pre_trained_model = tf.keras.applications.MobileNetV2(
    input_shape=(32, 64, 3), # refers to the shape we transfer from
    include_top=False, # cut off the head
    weights='imagenet', # pretrained on the ImageNet data
)
for layer in pre_trained_model.layers: #freeze the base model to not train it
    layer.trainable=False
pre_trained_model.summary()
#%%
import tensorflow as tf
from tensorflow.keras.layers import Dropout, Flatten, Dense, BatchNormalization

model_D = tf.keras.models.Sequential([
    pre_trained_model, # the pre-trained part
    tf.keras.layers.Flatten(), # flatten before fully connected part
    tf.keras.layers.Dense(64, activation='relu'),
    BatchNormalization(),
    Dropout(0.5),
    tf.keras.layers.Dense(11, activation='softmax'),
    ])
model_D.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
    )
model_D.summary()
#%%

STEP_SIZE_TRAIN=train_generator_D.n//train_generator_D.batch_size
STEP_SIZE_VALID=valid_generator_D.n//valid_generator_D.batch_size
STEP_SIZE_TEST=test_generator_D.n//test_generator_D.batch_size

historyD = model_D.fit_generator(generator=train_generator_D,
                    steps_per_epoch=STEP_SIZE_TRAIN,
                    validation_data=valid_generator_D,
                    validation_steps=STEP_SIZE_VALID,
                    epochs=1
)

#%% Y
train_generator_Y=datagen.flow_from_dataframe(
dataframe=df,
directory= path + "cell_image/train/DIDA_1/",
x_col="Filename",
y_col="Y",
batch_size=200,
seed=42,
shuffle=True,
class_mode="sparse",
target_size=(32,64)
)

valid_generator_Y=datagen_test.flow_from_dataframe(
dataframe=df,
directory= path + "cell_image/val/DIDA_1/",
x_col="Filename",
y_col="Y",
batch_size=32,
seed=42,
shuffle=True,
class_mode="sparse",
target_size=(32,64)
)

test_generator_Y=datagen_test.flow_from_dataframe(
dataframe=df,
directory= path + "cell_image/test/DIDA_1/",
x_col="Filename",
y_col="Y",
batch_size=1200,
seed=42,
shuffle=False,
class_mode="sparse",
target_size=(32,64)
)
#%% TRANSFER LEARNING MODEL Y

pre_trained_model = tf.keras.applications.MobileNetV2(
    input_shape=(32, 64, 3), # refers to the shape we transfer from
    include_top=False, # cut off the head
    weights='imagenet', # pretrained on the ImageNet data
)
for layer in pre_trained_model.layers: #freeze the base model to not train it
    layer.trainable=False
pre_trained_model.summary()
#%%
import tensorflow as tf
from tensorflow.keras.layers import Dropout, Flatten, Dense, BatchNormalization

model_Y = tf.keras.models.Sequential([
    pre_trained_model, # the pre-trained part
    tf.keras.layers.Flatten(), # flatten before fully connected part
    tf.keras.layers.Dense(64, activation='relu'),
    BatchNormalization(),
    Dropout(0.5),
    tf.keras.layers.Dense(11, activation='softmax'),
    ])
model_Y.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
    )
model_Y.summary()
#%%

STEP_SIZE_TRAIN=train_generator_Y.n//train_generator_Y.batch_size
STEP_SIZE_VALID=valid_generator_Y.n//valid_generator_Y.batch_size
STEP_SIZE_TEST=test_generator_Y.n//test_generator_Y.batch_size

historyY = model_Y.fit(train_generator_Y,
                    steps_per_epoch=STEP_SIZE_TRAIN,
                    validation_data=valid_generator_Y,
                    validation_steps=STEP_SIZE_VALID,
                    epochs=1
)

#%%
import numpy as np
# FOR CC
X_test, y_test = next(test_generator_CC)
real_labels_CC = y_test
real_labels_CC=real_labels_CC.astype(int)
predictions = model_CC.predict(X_test)
print(predictions)
predict_labels_CC = np.argmax(predictions, axis=-1)
print('predict for CC',predict_labels_CC)

# FOR D
X_test, y_test = next(test_generator_D)
real_labels_D = y_test
real_labels_D=real_labels_D.astype(int)
predictions = model_D.predict(X_test)
print(predictions)
predict_labels_D = np.argmax(predictions, axis=-1)
print('predict for D',predict_labels_D)

# FOR Y
X_test, y_test = next(test_generator_Y)
real_labels_Y = y_test
real_labels_Y=real_labels_Y.astype(int)
predictions = model_Y.predict(X_test)
print(predictions)
predict_labels_Y = np.argmax(predictions, axis=-1)
print('predict for Y',predict_labels_Y)

#%%

p1=(predict_labels_CC==real_labels_CC)
p2=(predict_labels_D==real_labels_D)
p3=(predict_labels_Y==real_labels_Y)

CNN_p_CC = 0
for i in range(len(predict_labels_CC)):
    if p1[i] == True:
        CNN_p_CC = CNN_p_CC + 0.33
CNN_p_D = 0
for i in range(len(predict_labels_CC)):
    if p2[i] == True:
        CNN_p_D = CNN_p_D + 0.33
CNN_p_Y = 0
for i in range(len(predict_labels_CC)):
    if p3[i] == True:
        CNN_p_Y = CNN_p_Y + 0.3

CNN_p_sequence = 0
for i in range(len(p1)):
    if p1[i] == p2[i] == p3[i] == True:
       CNN_p_sequence = CNN_p_sequence + 1
#%%
print('point for CC-model:', round(CNN_p_CC))
print('point for D-model:', round(CNN_p_D))
print('point for Y-model:', round(CNN_p_Y))
print('point for character acc:', round(CNN_p_CC + CNN_p_D + CNN_p_Y), 'out of', len(real_labels_CC))
print('point for correct sequence:', CNN_p_sequence, 'out of', len(real_labels_CC))

#%% QUESTION 3, 2

'''
2. Investigate if your model’s performance is
particularly good or bad at correctly
classifying certain classes (i.e., it might be very good
at correctly classifying centuries
but not years, or it might be good at correctly classifying
some decades but not certain other decades). Does it mix up
certain classes? If yes, does this surprise you (explain why or why not)?
'''

print(real_labels_CC)
print(real_labels_D)
print(real_labels_Y)
print('predict for CC',predict_labels_CC)
print('predict for Y',predict_labels_Y)
print('predict for D',predict_labels_D)

#%%
n_list = []
for i in range(len(real_labels_CC)):
    if predict_labels_CC[i] != real_labels_CC[i]:
        n_list.append(predict_labels_CC[i])

print(n_list)
#%%
nn_list = []
for i in range(len(real_labels_D)):
    if predict_labels_D[i] != real_labels_D[i]:
        nn_list.append(predict_labels_D[i])

print(nn_list)

#%%
nnn_list = []
for i in range(len(real_labels_Y)):
    if predict_labels_Y[i] != real_labels_Y[i]:
        nnn_list.append(predict_labels_Y[i])

print(nnn_list)

#%%
from collections import Counter
print(Counter(n_list))
print(Counter(nn_list))
print(Counter(nnn_list))


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse
from django import template
import json
import os.path
from datetime import timedelta, datetime
import time
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/home/optimizacion_final')

from elementos_modelo import mantenimiento as mto
from elementos_modelo import clientes as clt
import ACO
import PSO
import GA
import SA
import Agenda as ag
# import Funcion_objetivo as fo
# funcion=fo

mant=mto.generar_mantenimiento_planificado()
tareas=clt.generar_tareas_aleatorias()
consulta={}
consulta['query']={'tipo':'','hinicio':'','duracion':'','nombre':'','empleado':'','insumos':''}

@login_required(login_url="/login/")
def index(request):

    context = {}
    context['segment'] = 'index'

    html_template = loader.get_template( 'index.html' )
    return HttpResponse(html_template.render(context, request))

@login_required(login_url="/login/")
def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    try:

        load_template      = request.path.split('/')[-1]

        #print(cargar_registros())
        if request.method == "POST":
            data=request.POST
            #print(data)
        else:
            data=" "

        ejecutar_form(load_template,data)

        #print(data)

        context['segment'] = load_template
        context['tareas']=ctareas
        context['mantenimiento']=cmantenimiento
        context['empleados']=cpersonal
        context['almacen']=calmacen
        lista=separar_por_tipos(ctareas)
        context['listas']=lista
        context['estados']=cargar_estados()
        #registros=cargar_registros()
        context['dias']=recursos_por_Fecha()
        context['consultas']=consulta['query']
        context['chartga']=graficas['GA']
        context['chartsa']=graficas['SA']
        context['chartpso']=graficas['PSO']
        context['chartaco']=graficas['ACO']
        #print(graficas['GA'])

        html_template = loader.get_template( load_template )
        return HttpResponse(html_template.render(context, request))

    except template.TemplateDoesNotExist:

        html_template = loader.get_template( 'page-404.html' )
        return HttpResponse(html_template.render(context, request))

    except:

        html_template = loader.get_template( 'page-500.html' )
        return HttpResponse(html_template.render(context, request))

def ejecutar_form(a,data):
    if(a=='forma-tareas.html'):
        if(data!= " "):
            if(data['nombre']!= " " and data['contratista']!= " " and data['recepcion']!= " " and data['cantidad']!= " "):
                fecha=datetime.strptime(data['recepcion'],'%Y-%m-%d')
                limite=timedelta(days=7)
                limite=fecha+limite
                limite=limite.strftime("%m %d %Y")
                recepcion=fecha.strftime("%m %d %Y")
                datos={"id": 0, "tipo": data['tipot']+" "+data['tipol']+data['tipob'], "cantidad": data['cantidad'], "nombre": data['nombre'],
                "descripcion": data['descripcion'], "contratista": data['contratista'], "fecha_inicio": "---",
                "fecha_finalizacion": "---", "fecha_limite": limite, "fecha_recepcion": recepcion, "penalizacion": 1000,
                "comentarios": data['comment']}
                #print(datos)
                agregar_tarea(datos)

    if(a=='forma-mantenimiento.html'):
        if(data!= " "):
            if(data['encargado']!= " " and data['eresp']!= " " and data['fechaplan']!= " " and data['fuentecost']!= " "):
                fecha=datetime.strptime(data['fechaplan'],'%Y-%m-%d')
                fecha=fecha.strftime("%m %d %Y")
                datos={"id": 0, "tipo": data['tipo'], "objeto": data['estacion'],
                "encargado": data['encargado'], "descripcion": "informacion a agregar",
                "actividades": data['actividades'], "fecha planificada": fecha,
                "fuente de costos": data['fuentecost'], "empresa_responsable": data['eresp']}

                #print(datos)
                agregar_orden_mto(datos)

    if(a=='Horarios-Personal.html'):
        print('hp')
    if(a=='opt-ga.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['tc']!= " " and data['tm']!= " " and data['ts']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"tc":data['tc'],"tm":data['tm'],"ts":data['ts'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'ga')
                print('ga')
    if(a=='opt-SA.html'):
        if(data!= " "):
            if(data['nv']!= " " and data['maxk']!= " "):
                datos={"nv":data['nv'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'sa')
                print('sa')
    if(a=='opt-PSO.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['w']!= " " and data['c1']!= " " and data['c2']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"w":data['w'],"c1":data['c1'],"c2":data['c2'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'pso')
                print('pso')
    if(a=='opt-ACO.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'aco')
                print('aco')
    if(a=='opt-NSGA.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['tc']!= " " and data['tm']!= " " and data['ts']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"tc":data['tc'],"tm":data['tm'],"ts":data['ts'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'nsga')
                print('nsga')
    if(a=='Registros-totales.html'):
        if(data!= " "):
            if(data['tipof']!= " "):
                lista=[]
                for i in libro.bookR:

                    if(i.fecha.strftime("%m %d %Y") == data['tipof']):
                        print('si lo encontro')
                        print(i.fecha.strftime("%m %d %Y"))
                        print(i.recursoA)
                        print(i.recursoB1)
                        print(i.recursoB2)
                        print(i.recursoB3)
                        print(i.recursoB4)
                        print(i.recursoC)
                        if(i.recursoA[0]['TAREA']!={}):
                            for x in i.recursoA:
                                if(x['EMPLEADO']==0):
                                    empleado=0
                                else:
                                    empleado=x['EMPLEADO']['NOMBRE']
                                lista.append({'tipo':x['TIPO'],'hinicio':x['HINICIO'],'duracion':x['HORAS'],'nombre':x['TAREA']['nombre'],'empleado':empleado,'insumos':x['INSUMOS']})
                        if(i.recursoB1['TAREA']!={}):
                            lista.append({'tipo':i.recursoB1['TIPO'],'hinicio':i.recursoB1['HINICIO'],'duracion':i.recursoB1['HORAS'],'nombre':i.recursoB1['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB1['INSUMOS']})
                        if(i.recursoB2['TAREA']!={}):
                            lista.append({'tipo':i.recursoB2['TIPO'],'hinicio':i.recursoB2['HINICIO'],'duracion':i.recursoB2['HORAS'],'nombre':i.recursoB2['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB2['INSUMOS']})
                        if(i.recursoB3['TAREA']!={}):
                            lista.append({'tipo':i.recursoB3['TIPO'],'hinicio':i.recursoB3['HINICIO'],'duracion':i.recursoB3['HORAS'],'nombre':i.recursoB3['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB3['INSUMOS']})
                        if(i.recursoB4['TAREA']!={}):
                            lista.append({'tipo':i.recursoB4['TIPO'],'hinicio':i.recursoB4['HINICIO'],'duracion':i.recursoB4['HORAS'],'nombre':i.recursoB4['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB4['INSUMOS']})
                        if(i.recursoC[0]['TAREA']!={}):

                            for x in i.recursoC:
                                if(x['EMPLEADO']==0):
                                    empleado=0
                                else:
                                    empleado=x['EMPLEADO']['NOMBRE']
                                lista.append({'tipo':x['TIPO'],'hinicio':x['HINICIO'],'duracion':x['HORAS'],'nombre':x['TAREA']['nombre'],'empleado':empleado,'insumos':x['INSUMOS']})
                if (lista == []):
                    lista.append({'tipo':'','hinicio':'','duracion':'','nombre':'','empleado':'','insumos':''})
                consulta['query']=lista
                print(lista)
    if(a=='opt-solver.html'):
        if(data!= " "):
            botones=['btnGA','btnSA','btnPSO','btnACO']
            sel=['selGA','selSA','selPSO','selACO']
            labels=['GA','SA','PSO','ACO']
            ind=0
            for x in botones:
                if x in data.keys():
                    ejecutando_algoritmo(labels[ind])
                ind+=1

            ind=0
            for x in sel:
                if x in data.keys():
                    selecionar_Resultado(labels[ind])
                ind+=1

def selecionar_Resultado(a):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'estado'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        #se le pasa el vector a la funcion planificador
        x=ctareas
        vector=data[a]['resultado']
        cargar_nueva_configuracion(vector,x)

def cargar_nueva_configuracion(state,tareas):

    x=ordenar_Tareas(state,tareas)
    a=funcion.Calcular_Costo(x,True)

def ordenar_Tareas(state,tareas):
    h={}
    newtareas=[]
    for i in state:
        for x in tareas:
            if(i == x['id']):
                newtareas.append(x)
    h['pedidos']=newtareas
    return h

def ejecutando_algoritmo(a):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'estado'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        data[a]['estado']='ejecutando'

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

        if(a=='GA'):
            GA.ejecutarGA()

        if(a=='SA'):
            SA.ejecutarSA()

        if(a=='PSO'):
            PSO.ejecutarPSO()

        if(a=='ACO'):
            ACO.ejecutarACO()

        #se ejecuta el algoritmo en cuestion

def cargar_estados():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'estado'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data

def cargar_tareas():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'data'

        completeName = os.path.join(save_path, name_of_file+".json")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['pedidos']

def separar_por_tipos(tareas):

    vino=[]
    agua=[]
    jugo=[]
    #print(tareas)
    for val in tareas:
        tipo=val['tipo'].split()
        if(tipo[0]=='vino'):
            for x in range(val['cantidad']):
                k=val.copy()
                k['cantidad']=1
                vino.append(k)
            #print('v')
        if(tipo[0]=='agua'):
            for x in range(val['cantidad']):
                k=val.copy()
                k['cantidad']=1
                agua.append(k)
            #print('a')
        if(tipo[0]=='banano' or tipo[0]=='guanabana'):
            for x in range(val['cantidad']):
                k=val.copy()
                k['cantidad']=1
                jugo.append(k)

    nummax=len(vino)
    if(nummax < len(jugo)):
        nummax=len(jugo)
    if(nummax < len(agua)):
        nummax=len(agua)

    lista=[]
    for i in range(nummax):
        #print(i)

        if(i>=(len(jugo)-1)):
            jugov=""
        else:
            jugov=jugo[i]

        if(i>=(len(vino)-1)):
            vinov=""
        else:
            vinov=vino[i]

        if(i>=(len(agua)-1)):
            aguav=""
        else:
            aguav=agua[i]

        lista.append([jugov,vinov,aguav])

    return lista

def cargar_registros():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'registro'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data

def actualizar_opt_Settings(datos,name):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'optsettings'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        data['opt'][name]=datos

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

def agregar_tarea(datos):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'data'

        completeName = os.path.join(save_path, name_of_file+".json")

        with open(completeName) as json_file:
            data = json.load(json_file)

        maxvalue = max(data['pedidos'], key=lambda x:x['id'])

        datos['id']=maxvalue['id']+1

        data['pedidos'].append(datos)

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

def agregar_orden_mto(datos):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'datam'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        maxvalue = max(data['preventivo'], key=lambda x:x['id'])

        datos['id']=maxvalue['id']+1

        data['preventivo'].append(datos)

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

def cargar_mantenimiento():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'datam'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['preventivo']

def cargar_personal():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'datae'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['empleados']

def cargar_almacen():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'Punta Delicia'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['racks']
def cargar_book():
    save_path = '/home/optimizacion_final/datos_json'
    name_of_file = 'book_pedidos'
    completeName = os.path.join(save_path, name_of_file+".json")
    with open(completeName) as json_file:
        pedidos = json.load(json_file)

    name_of_file = 'book_recursos'
    completeName = os.path.join(save_path, name_of_file+".json")
    with open(completeName) as json_file:
        recursos = json.load(json_file)

    return pedidos,recursos

def cargar_datos():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'charts'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        pasoga=int(len(data['GA']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['GA']['y'])):
            if(cont==pasoga):
                y.append(data['GA']['y'][i])
                x.append(data['GA']['x'][i])
                cont=0
            cont+=1
        data['GA']['y']=y
        data['GA']['x']=x

        pasoga=int(len(data['SA']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['SA']['y'])):
            if(cont==pasoga):
                y.append(data['SA']['y'][i])
                x.append(data['SA']['x'][i])
                cont=0
            cont+=1
        data['SA']['y']=y
        data['SA']['x']=x

        pasoga=int(len(data['PSO']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['PSO']['y'])):
            if(cont==pasoga):
                y.append(data['PSO']['y'][i])
                x.append(data['PSO']['x'][i])
                cont=0
            cont+=1
        data['PSO']['y']=y
        data['PSO']['x']=x

        pasoga=int(len(data['ACO']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['ACO']['y'])):
            if(cont==pasoga):
                y.append(data['ACO']['y'][i])
                x.append(data['ACO']['x'][i])
                cont=0
            cont+=1
        data['ACO']['y']=y
        data['ACO']['x']=x

        return data

def recursos_por_Fecha():
    inicio=datetime.strptime(brecursos['calendario'][0]['fecha'],'%m %d %Y')
    final=datetime.strptime(brecursos['calendario'][-1]['fecha'],'%m %d %Y')
    # inicio=datetime.strptime(registros['fecha_inicio'],'%m %d %Y')
    # final=datetime.strptime(registros['fecha_final'],'%m %d %Y')
    valor=final-inicio
    lista=[]
    for i in range(valor.days+1):
        dia=inicio + timedelta(days=(i))
        lista.append(dia.strftime("%m %d %Y"))

    return lista

registros=cargar_registros()
ctareas=cargar_tareas()
cmantenimiento=cargar_mantenimiento()
cpersonal=cargar_personal()
calmacen=cargar_almacen()
graficas=cargar_datos()
bpedidos,brecursos=cargar_book()
diao=datetime.strptime(brecursos['calendario'][0]['fecha'],'%m %d %Y')
diaf=datetime.strptime(brecursos['calendario'][-1]['fecha'],'%m %d %Y')
libro=ag.agenda()
libro.crear_AgendaR(diao,diaf)
libro.crear_AgendaP(diao,diaf)
libro.cargarP(bpedidos['calendario'])
libro.cargarR(brecursos['calendario'])


import requests
import sys
sys.path.extend(['./app/db'])

import schemas_pydantic
host = 'localhost'
port = '13371'

def authenticate(user,pw):
    token_response = requests.post(f"http://{host}:{port}/token", data={"username": user, "password": pw, "grant_type": "password"},
                                   headers={"content-type": "application/x-www-form-urlencoded"})

    token_response = token_response.json()
    token = token_response['access_token']

    auth_header = {'Authorization': 'Bearer {}'.format(token)}

    return auth_header

auth_header = authenticate("kit", "KIT_huipuischui_23")

#test chemical
dmc = schemas_pydantic.Chemical(smiles='COC(=O)OC', name='DMC', reference='DMC_Elyte_2020')
ans_ = requests.post(f"http://{host}:{port}/api/broker/post/chemical",
                     data=dmc.json(),headers=auth_header).json()

#test compound
dmc_compound = schemas_pydantic.Compound(chemicals=[schemas_pydantic.Chemical(smiles='COC(=O)OC', name='DMC', reference='DMC_Elyte_2020')],
    amounts=[schemas_pydantic.Amount(value=1.0, unit='mol')],name='DMC')
ans_ = requests.post(f"http://{host}:{port}/api/broker/post/compound",data=dmc_compound.json(),headers=auth_header).json()

#test compound complex
mixed_compound = schemas_pydantic.Compound(chemicals=[
    schemas_pydantic.Chemical(smiles='COC(=O)OC', name='DMC', reference='DMC_Elyte_2020'),
    schemas_pydantic.Chemical(smiles='[Li+].F[P-](F)(F)(F)(F)F', name='LiPF6', reference='LiPF6_Elyte_2020')],
    amounts=[schemas_pydantic.Amount(value=1.0, unit='mol'), schemas_pydantic.Amount(value=0.2, unit='mol')],
    name='LDMC_LIPF6')

ans_ = requests.post(f"http://{host}:{port}/api/broker/post/compound",data=mixed_compound.json(),headers=auth_header).json()

#test measurement
form = schemas_pydantic.Formulation(chemicals=[schemas_pydantic.Chemical(smiles='COC(=O)OC', name='DMC', reference='DMC_Elyte_2020'),
                                                schemas_pydantic.Chemical(smiles='[Li+].F[P-](F)(F)(F)(F)F', name='LiPF6', reference='LiPF6_Elyte_2020')],
    amounts=[schemas_pydantic.Amount(value=1.0, unit='mol'),schemas_pydantic.Amount(value=0.1, unit='mol')],
    ratio_method='molal')
orig = schemas_pydantic.Origin(origin='simulation', what='density')
data = schemas_pydantic.FomData(values=[1.23],unit='g/cm**2',dim=1,
                                name='density',origin=orig,internalReference='aTest',
                                fail=False, message='My Message', rating=1)
data_nd = schemas_pydantic.FomData(values=[1.23,234,12,13,56,26],unit='g/cm**2',dim=6,
                                name='vectorial',origin=orig,internalReference='aTest',
                                fail=False, message='My Message', rating=1)
temp = schemas_pydantic.Temperature(value=300,unit='K')
meas = schemas_pydantic.Measurement(formulation=form,temperature=temp,pending=False,fom_data=[data_nd],kind=orig)

ans_ = requests.post(f"http://{host}:{port}/api/broker/post/measurement",data=meas.json(),headers=auth_header)
ans_.json()