import logging
import logging.config
from os import path

from aiogram import types
from aiogram.dispatcher import Dispatcher, FSMContext

from bot_app.conversation.handlers.common_functions import set_or_update_config
from bot_app.dialogs.buttons import PlanningButtons, NotifierButtons, DbButtons, confirm_or_not_confirm_kb
from bot_app.dialogs.dialogs import buttons_names, buttons_callbacks, msg, confirmation_callbacks
from bot_app.states.insert_results_state import InsertResultState
from bot_app.states.notifier_states import NotifierStates
from db.db_data_handler import DbDataHandler
from db.db_functions import DbFunctions
from redis_repository.redis_cache import cache

def init_notifier_handler(dp: Dispatcher, db: DbFunctions, db_buttons: DbButtons,
                          db_data_handler: DbDataHandler):
    log_file_path = path.join(path.dirname(path.abspath("__file__")), 'logging.ini')
    logging.config.fileConfig(log_file_path, disable_existing_loggers=False)
    logger = logging.getLogger(__name__)
    logger.info("Start tasks notifier handler")

    @dp.message_handler(lambda m: m.text == buttons_names.back_to_menu, state="*")
    async def go_to_main_menu(message: types.Message, state: FSMContext):
        await state.reset_state()
        await message.answer(msg.back_to_menu_text,
                             reply_markup=PlanningButtons.main_kb())

    @dp.message_handler(lambda m: m.text == buttons_names.notifier_functionality, state="*")
    async def start_notifier_handler(message: types.Message, state: FSMContext):
        await state.reset_state()
        await message.bot.send_message(chat_id=message.chat.id,
                                       text=msg.back_to_menu_text,
                                       reply_markup=PlanningButtons.back_to_menu())
        await message.answer(text=msg.notifier_message, reply_markup=NotifierButtons.notifier_kb())
        cache.setex(f"last_msg_{message.from_user.id}", 60 * 60 * 24,
                    message.message_id+3)
        await NotifierStates.notifier_state.set()

    @dp.callback_query_handler(lambda c: c.data == buttons_callbacks.activate_aims,
                               state=NotifierStates.notifier_state)
    async def activate_aims(callback: types.CallbackQuery):
        await callback.message.answer(text=msg.notifier_choose_aim_to_activate,
                                      reply_markup=db_buttons.get_active_or_not_active_aims_names_kb(
                                          active=False)
                                      )

    @dp.callback_query_handler(lambda c: c.data.startswith("edit_config"),
                               state=NotifierStates.notifier_state)
    async def update_notifier_active_aims(callback: types.CallbackQuery):
        await set_or_update_config(dp=dp, db_function=db_buttons.get_active_or_not_active_aims_names_kb,
                                   callback=callback, text=msg.notifier_choose_aim_to_activate, active=False)

    @dp.callback_query_handler(lambda c: c.data.startswith("aim_name"), state=NotifierStates.notifier_state)
    async def update_aim_status(callback: types.CallbackQuery, state: FSMContext):
        aim_id = int(callback.data.split("#")[-1])
        async with state.proxy() as data:
            data["aim_id"] = aim_id
        aim_name = db.set_aim_status_active(aim_id)
        await callback.message.bot.send_message(chat_id=callback.message.chat.id,
                                                text=f"Статус цели '{aim_name}' изменен на активный. "
                                                     f"Вы будете получать уведомления по этой цели.")
        await callback.message.answer(text=msg.notifier_update_task_msg,
                                      reply_markup=db_buttons.get_tasks_names_kb(aim_id=aim_id))

    @dp.callback_query_handler(lambda c: c.data == buttons_callbacks.active_aims_list, state="*")
    async def get_active_aims_list(callback: types.CallbackQuery, state: FSMContext):
        await state.finish()
        await callback.message.bot.send_message(chat_id=callback.message.chat.id,
                                                text=msg.back_to_menu_text,
                                                reply_markup=PlanningButtons.back_to_menu())
        await callback.message.answer(text=msg.notifier_active_aim_msg,
                                      reply_markup=NotifierButtons.active_aims_kb())
        await NotifierStates.notifier_state.set()

    @dp.callback_query_handler(lambda c: c.data == buttons_callbacks.notifier_active_aims,
                               state=NotifierStates.notifier_state)
    async def get_active_tasks_list(callback: types.CallbackQuery):
        await callback.message.bot.send_message(chat_id=callback.message.chat.id,
                                                text=msg.notifier_active_aims_list)
        await callback.message.bot.send_message(chat_id=callback.message.chat.id,
                                                text=db_data_handler.get_active_aims_string())

    @dp.callback_query_handler(lambda c: c.data == buttons_callbacks.notifier_add_active_task_by_aim,
                               state=NotifierStates.notifier_state)
    async def get_active_aims_to_update_tasks(callback: types.CallbackQuery):
        await callback.message.answer(msg.notifier_choose_aim_for_task_activate,
                                      reply_markup=db_buttons.get_active_aims_names_kb())

    @dp.callback_query_handler(lambda c: c.data.startswith("active_aim_name"),
                               state=NotifierStates.notifier_state)
    async def add_active_task_for_active_aim(callback: types.CallbackQuery):
        aim_id = int(callback.data.split("#")[-1])
        await callback.message.answer(text=msg.activate_task_for_active_aim,
                                      reply_markup=db_buttons.get_active_and_not_active_tasks_names_kb(
                                          aim_id=aim_id, active=False))

    @dp.callback_query_handler(lambda c: c.data.startswith("task_name"),
                               state=NotifierStates.notifier_state)
    async def activate_task(callback: types.CallbackQuery, state: FSMContext):
        task_id = int(callback.data.split("#")[-1])
        db.set_task_status(task_id=task_id, active=True)
        await callback.bot.send_message(chat_id=callback.message.chat.id,
                                        text=msg.notifier_aim_status_to_active)
        await dp.bot.send_message(chat_id=callback.message.chat.id, text=msg.main,
                                  reply_markup=PlanningButtons.main_kb())
        await state.finish()

    @dp.callback_query_handler(lambda c: c.data == buttons_callbacks.disable_active_aims,
                               state=NotifierStates.notifier_state)
    async def start_disable_active_aim(callback: types.CallbackQuery):
        await callback.message.answer(text=msg.notifier_disable_active_aim,
                                      reply_markup=db_buttons.get_active_or_not_active_aims_names_kb(
                                          active=True
                                      ))
        await NotifierStates.notifier_state_disable_aims.set()

    @dp.callback_query_handler(lambda c: c.data.startswith("aim_name"),
                               state=NotifierStates.notifier_state_disable_aims)
    async def disable_active_aim(callback: types.CallbackQuery):
        aim_id = int(callback.data.split("#")[-1])
        db.set_aim_status_not_active(aim_id=aim_id)
        await dp.bot.send_message(chat_id=callback.message.chat.id,
                                  text=msg.notifier_aim_become_disables)
        db.set_task_status_not_active_by_aim_id(aim_id=aim_id)
        await dp.bot.send_message(chat_id=callback.message.chat.id,
                                  text=msg.notifier_tasks_by_aim_disables)

    @dp.callback_query_handler(lambda c: c.data == buttons_callbacks.disable_active_tasks,
                               state=NotifierStates.notifier_state)
    async def start_disable_active_task(callback: types.CallbackQuery):
        await callback.message.answer(text=msg.notifier_task_disable_choose_aim,
                                      reply_markup=db_buttons.get_active_or_not_active_aims_names_kb(
                                          active=True
                                      ))
        await NotifierStates.notifier_state_disable_tasks.set()

    async def active_task_aim_choose(callback: types.CallbackQuery, text: str):
        aim_id = int(callback.data.split("#")[-1])
        await callback.message.answer(text=text,
                                      reply_markup=db_buttons.get_active_and_not_active_tasks_names_kb(
                                          aim_id=aim_id, active=True)
                                      )

    @dp.callback_query_handler(lambda c: c.data.startswith("aim_name"),
                               state=NotifierStates.notifier_state_disable_tasks)
    async def disable_active_task_aim_choose(callback: types.CallbackQuery):
        await active_task_aim_choose(callback=callback, text=msg.notifier_disable_active_task)

    @dp.callback_query_handler(lambda c: c.data.startswith("task_name_activate"),
                               state=NotifierStates.notifier_state_disable_tasks)
    async def disable_active_task(callback: types.CallbackQuery):
        task_id = int(callback.data.split("#")[-1])
        db.set_task_status(task_id=task_id, active=False)
        await dp.bot.send_message(chat_id=callback.message.chat.id,
                                  text=msg.notifier_task_become_disable)

    @dp.callback_query_handler(lambda c: c.data == buttons_callbacks.active_tasks,
                               state=NotifierStates.notifier_state)
    async def start_get_active_tasks(callback: types.CallbackQuery):
        await dp.bot.send_message(chat_id=callback.message.chat.id,
                                  text=db_data_handler.get_active_tasks_string())

    @dp.message_handler(lambda m: m.text == buttons_names.insert_task_results, state="*")
    async def start_insert_task_results(message: types.Message):
        await dp.bot.send_message(chat_id=message.chat.id, text=msg.tasks_back_to_menu,
                                  reply_markup=PlanningButtons.back_to_menu())
        await message.answer(text=msg.insert_results_start, reply_markup=db_buttons.get_active_aims_names_kb())
        await InsertResultState.insert_results_state.set()

    @dp.callback_query_handler(lambda c: c.data.startswith("active_aim_name"),
                               state=InsertResultState.insert_results_state)
    async def choose_task_to_insert_results(callback: types.CallbackQuery):
        await active_task_aim_choose(callback=callback, text=msg.insert_results_choose_task)

    @dp.callback_query_handler(lambda c: c.data.startswith("task_name_activate"),
                               state=InsertResultState.insert_results_state)
    async def get_task_id_for_active_task(callback: types.CallbackQuery, state: FSMContext):
        task_id = int(callback.data.split("#")[-1])
        async with state.proxy() as data:
            data["task_id"] = task_id
        await InsertResultState.next()
        await callback.message.answer(text=msg.insert_results_is_digit,
                                      reply_markup=confirm_or_not_confirm_kb(
                                          confirm=confirmation_callbacks.insert_task_digit_confirm,
                                          not_confirm=confirmation_callbacks.insert_task_digit_not_confirm
                                      ))

    @dp.callback_query_handler(lambda c: c.data == confirmation_callbacks.insert_task_digit_confirm,
                               state=InsertResultState.get_result_state)
    async def add_result_value(callback: types.CallbackQuery):
        await callback.message.answer(text=msg.insert_results_insert_result)

    @dp.message_handler(state=InsertResultState.get_result_state)
    async def write_result(message: types.Message, state: FSMContext):
        result_value = message.text
        if result_value.isdigit():
            async with state.proxy() as data:
                data["result"] = result_value
        else:
            await message.answer(text=msg.digit_error_message)
            return
        await InsertResultState.insert_info.set()
        await insert_result_active_task(message=message, state=state)

    @dp.message_handler(state=InsertResultState.insert_info)
    async def insert_result_active_task(message: types.Message, state: FSMContext):
        state_data = dict(await state.get_data())
        task_id = state_data.get("task_id")
        result = state_data.get("result")
        db.insert_result_for_active_task(task_id=task_id, result=result)
        db.insert_into_task_results_by_day(task_id=task_id, result=result)
        await dp.bot.send_message(chat_id=message.chat.id,
                                  text=msg.insert_results_success)
        check_task_complete = db_data_handler.check_status_for_complete(task_id=task_id)
        if check_task_complete:
            db_data_handler.close_completed_task(task_id=task_id)
            await message.answer(text=msg.insert_results_digit_task_complete,
                                 reply_markup=PlanningButtons.main_kb())
            await state.finish()
        else:
            await state.finish()

    @dp.callback_query_handler(lambda c: c.data == confirmation_callbacks.insert_task_digit_not_confirm,
                               state=InsertResultState.get_result_state)
    async def not_confirm_digit_to_insert(callback: types.CallbackQuery, state: FSMContext):
        state_data = dict(await state.get_data())
        task_id = state_data.get("task_id")
        check = db_data_handler.check_target_value_is_digit(task_id=task_id)
        if check:
            await dp.bot.send_message(chat_id=callback.message.chat.id,
                                      text=msg.insert_task_close_nd_task_error_msg)
            await start_insert_task_results(message=callback.message)
        else:
            await callback.message.answer(text=msg.insert_results_not_digit_msg,
                                          reply_markup=confirm_or_not_confirm_kb(
                                              confirm=confirmation_callbacks.insert_task_close_nd_task_confirm,
                                              not_confirm=confirmation_callbacks.insert_task_close_nd_task_not_confirm)
                                          )
            await InsertResultState.close_not_digit_task.set()

    @dp.callback_query_handler(lambda c: c.data == confirmation_callbacks.insert_task_close_nd_task_confirm,
                               state=InsertResultState.close_not_digit_task)
    async def close_not_digit_task(callback: types.CallbackQuery, state: FSMContext):
        state_data = dict(await state.get_data())
        task_id = state_data.get("task_id")
        db.update_deadline_date_end(task_id=task_id)
        db.insert_into_task_results_by_day(task_id=task_id)
        db.set_task_status(task_id=task_id, active=False)
        await dp.bot.send_message(chat_id=callback.message.chat.id, text=msg.insert_results_close_nd_task)
        await state.finish()


import socket
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
	instance_id = socket.gethostname()
	return f'Hello from instance : {instance_id}\n'

@app.route('/healthz')
def get_healthz():
	return 'Ok'

if __name__ == '__main__':
	app.run(port=5000)


import socket
import os
import tqdm
import time

clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '192.168.136.34'
port = 1000
clientsocket.connect((host, port))

command = input("Enter command get/post: ")
clientsocket.send(command.encode())

if command == "get":
    # get file name from user
    filename = input("Enter file name: ")
    clientsocket.send(filename.encode())

    # receive file size from server
    filesize = int(clientsocket.recv(1024).decode())
    print(f"File size: {filesize} bytes")

    progress=tqdm.tqdm(range(filesize), f"sending {filename}", unit='B', unit_scale=True, unit_divisor=1024)
    # receive file data from server
    with open(filename, 'wb') as f:
        while filesize > 0:
            data = clientsocket.recv(1024)
            f.write(data)
            filesize -= len(data)
            progress.update(len(data))

    print(f"Received file: {filename}")

elif command == "post":
    # get file name from user
    filename = input("Enter file name: ")
    #filename=os.path.basename(filename)
    clientsocket.send(f"{filename}".strip().encode('utf-8'))
    # send file size to server
    filesize = int(os.path.getsize(filename))
    #clientsocket.send(str(filesize).strip().encode())
    time.sleep(2)

    progress=tqdm.tqdm(range(filesize), f"sending {filename}", unit='B', unit_scale=True, unit_divisor=1024)
    # send file data to server
    with open(filename, 'rb') as f:
        while True:
            data = f.read(1024)
            if not data:
                break
            clientsocket.sendall(data)
            progress.update(len(data))
    clientsocket.close()

    print(f"Sent file: {filename}")
else:
    print("Unknown command")

clientsocket.close()


"""
You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.
For each index i, names[i] and heights[i] denote the name and height of the ith person.
Return names sorted in descending order by the people's heights.
"""
def sortPeople(names,heights):
    res, l = {}, []
    for i, x in zip(names, heights):
        res[x] = i
    t = sorted(res.keys(), reverse=True)
    for i in t:
        l.append(res[i])
    return l

names = ["Mary","John","Emma"]
heights = [180,165,170]
res =  sortPeople(names, heights)
print(res)


from datetime import datetime
#import split_csv
#import table2VecModel
import makeVectorIndex
#import bm25Model
#import scoreQuery

file_name = "sp_table2VecW1.csv"
split_filename = "table2VecW.csv"
model_name = "model_table2VecW"
index_files = "table2VecW.idx"
query_file_name = "queries.txt"
ranked_file  = "bm25_table2VecW.idx"
output_rank_file = "table2VecW.out"

print("Path of the file is : /scratch/cse/phd/anz208486/col873_project/NLP_project/Dataset/"+file_name)
print("Output folder path : /scratch/cse/phd/anz208486/col873_project/NLP_project/finished_output")

output_folder = "/scratch/cse/phd/anz208486/col873_project/NLP_project/finished_output"
input_folder = "/scratch/cse/phd/anz208486/col873_project/NLP_project/Dataset/"

now = datetime.now()
print("Vectorization started at:",now.strftime("%H:%M:%S"))

vector_file = makeVectorIndex.indexing(index_files,input_folder,file_name,model_name,output_folder )

now = datetime.now()
print("Vectorization completed at:",now.strftime("%H:%M:%S"))


#!/usr/bin/env python3
#Author: lnotspotl

import numpy as np
from math import sqrt, atan2, sin, cos, pi
from RoboticsUtilities.Transformations import homog_transform_inverse,homog_transform

class InverseKinematics(object):
    def __init__(self, bodyDimensions, legDimensions):

        # Body dimensions
        self.bodyLength = bodyDimensions[0]
        self.bodyWidth = bodyDimensions[1]

        # Leg dimensions
        self.l1 = legDimensions[0]
        self.l2 = legDimensions[1]
        self.l3 = legDimensions[2]
        self.l4 = legDimensions[3]

    def get_local_positions(self,leg_positions,dx,dy,dz,roll,pitch,yaw):
        """
        Compute the positions of the end points in the shoulder frames.
        """

        leg_positions = (np.block([[leg_positions],[np.array([1,1,1,1])]])).T

        # Transformation matrix, base_link_world => base_link
        T_blwbl = homog_transform(dx,dy,dz,roll,pitch,yaw)

        # Transformation matrix, base_link_world => FR1
        T_blwFR1 = np.dot(T_blwbl, homog_transform(+0.5*self.bodyLength,
                          -0.5*self.bodyWidth,0,pi/2,-pi/2,0))

        # Transformation matrix, base_link_world => FL1
        T_blwFL1 = np.dot(T_blwbl, homog_transform(+0.5*self.bodyLength,
                          +0.5*self.bodyWidth,0,pi/2,-pi/2,0))

        # Transformation matrix, base_link_world => RR1
        T_blwRR1 = np.dot(T_blwbl, homog_transform(-0.5*self.bodyLength,
                          -0.5*self.bodyWidth,0,pi/2,-pi/2,0))

        # Transformation matrix, base_link_world => RL1
        T_blwRL1 = np.dot(T_blwbl, homog_transform(-0.5*self.bodyLength,
                          +0.5*self.bodyWidth,0,pi/2,-pi/2,0))

        # Local coordinates
        pos_FR = np.dot(homog_transform_inverse(T_blwFR1),leg_positions[0])
        pos_FL = np.dot(homog_transform_inverse(T_blwFL1),leg_positions[1])
        pos_RR = np.dot(homog_transform_inverse(T_blwRR1),leg_positions[2])
        pos_RL = np.dot(homog_transform_inverse(T_blwRL1),leg_positions[3])

        return(np.array([pos_FR[:3],pos_FL[:3],pos_RR[:3],pos_RL[:3]]))

    def inverse_kinematics(self,leg_positions,dx,dy,dz,roll,pitch,yaw):
        """
        Compute the inverse kinematics for all the legs.
        """
        positions = self.get_local_positions(leg_positions,dx,dy,dz,
                                                            roll,pitch,yaw)
        angles = []

        for i in range(4):

            x = positions[i][0]
            y = positions[i][1]
            z = positions[i][2]

            F = sqrt(x**2 + y**2 - self.l2**2)
            G = F - self.l1
            H = sqrt(G**2 + z**2)

            theta1 = atan2(y,x) + atan2(F,self.l2 * (-1)**i)

            D = (H**2 - self.l3**2 - self.l4**2)/(2*self.l3*self.l4)

            theta4 = -atan2((sqrt(1-D**2)),D)

            theta3 = atan2(z,G) - atan2(self.l4*sin(theta4),
                                          self.l3 + self.l4*cos(theta4))

            angles.append(theta1)
            angles.append(theta3)
            angles.append(theta4)

        # Return joint angles in radians - FR, FL, RR, RL
        return angles


import time
from functools import wraps
from flask import jsonify
from app.exceptions import (
    Exception
)

def api_handler(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()

        payloads = {}
        status_code = 200
        try:
            result = func(*args, **kwargs)
            payloads.update({"success": 1, "result": result})
        except Exception as e:
            payloads.update(
                {
                    "success": 0,
                    "errorCode": e.error_code,
                    "errorMessage": e.error_message
                }
            )
            status_code = e.status_code

        if kwargs:
            payloads.update(kwargs)

        elapsed_time = round((time.time() - start_time) * 1000, 1)
        payloads["elapsed_time"] = f"{elapsed_time}ms"

        return jsonify(payloads), status_code

    return wrapper


from __future__ import unicode_literals, print_function, division
import time
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

def makePlot(points):
    print('showPlot')
    plt.figure()
    fig, ax = plt.subplots()
    # this locater puts ticks at regular intervals
    loc = ticker.MultipleLocator(base=0.2)
    ax.yaxis.set_major_locator(loc)
    plt.plot(points)
    figure_1 = plt.gcf()
    plt.draw()
    img_path = '/home/arpit9295_2/Ayushi_Workspace/Seq2Seq_with_Attention/img/point_plot'
    figure_1.savefig(img_path)
    plt.show()

def timeLapsed(since, percent):
    now = time.time()
    s = now - since
    es = s / (percent)
    rs = es - s

    m = s // 60
    s -= m * 60
    return '%dm %ds' % (m, s)

def wordIndexesFromSentence(lang, sentence):
    return [lang.word2index[word] for word in sentence.split(' ')]


# coding=utf-8

"""Definitions relating to hazards."""

from safe.definitions.caveats import (
    caveat_simulation, caveat_local_conditions, caveat_analysis_extent, )
from safe.definitions.concepts import concepts
from safe.definitions.earthquake import EARTHQUAKE_FUNCTIONS
from safe.definitions.exposure import (
    exposure_place, exposure_land_cover, exposure_road)
from safe.definitions.extra_keywords import (
    ash_extra_keywords, earthquake_extra_keywords, flood_extra_keywords)
from safe.definitions.fields import (
    hazard_name_field, hazard_fields, hazard_value_field)
from safe.definitions.hazard_classifications import (
    generic_hazard_classes,
    volcano_hazard_classes,
    earthquake_mmi_scale,
    flood_hazard_classes,
    flood_petabencana_hazard_classes,
    tsunami_hazard_classes,
    tsunami_hazard_population_classes,
    tsunami_hazard_classes_ITB,
    tsunami_hazard_population_classes_ITB,
    ash_hazard_classes,
    cyclone_au_bom_hazard_classes,
    cyclone_sshws_hazard_classes,
    inundation_dam_class)
from safe.definitions.layer_modes import (
    layer_mode_classified, layer_mode_continuous)
from safe.definitions.units import (
    unit_feet,
    unit_generic,
    unit_kilogram_per_meter_square,
    unit_kilometres,
    unit_metres,
    unit_millimetres,
    unit_centimetres,
    unit_mmi,
    unit_miles_per_hour,
    unit_kilometres_per_hour,
    unit_knots,
    unit_metres_per_second)
from safe.utilities.i18n import tr

__copyright__ = "Copyright 2016, The InaSAFE Project"
__license__ = "GPL version 3"
__email__ = "info@inasafe.org"
__revision__ = '1a0dd4852cc92d080bf32eb73bc0487259083a8c'

continuous_hazard_unit = {
    'key': 'continuous_hazard_unit',
    'name': tr('Unit'),
    'description': tr(
        'Hazard units are used for continuous data. Examples of hazard units '
        'include metres and feet.'),
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'types': [
        unit_feet,
        unit_generic,
        unit_kilogram_per_meter_square,
        unit_kilometres,
        unit_metres,
        unit_millimetres,
        unit_centimetres,
        unit_mmi,
        unit_kilometres_per_hour,
        unit_miles_per_hour,
        unit_knots,
        unit_metres_per_second
    ]
}
continuous_hazard_unit_all = continuous_hazard_unit['types']
hazard_generic = {
    'key': 'hazard_generic',
    'name': tr('Generic Hazard'),
    'description': tr(
        'A generic hazard can be used for any type of hazard where the '
        'data have been classified or generalised. For example: earthquake, '
        'flood, volcano, tsunami, landslide, smoke haze or strong wind. '
        'You can use the generic hazard functionality in InaSAFE to carry '
        'out an assessment for hazard data that are not explicitly supported '
        'yet in InaSAFE.'),
    'notes': [
        {
            'item_category': 'generic_hazard_general',
            'item_header': tr('generic hazard general notes'),
            'item_list': [
                # additional generic notes for generic
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'continuous_notes': [  # notes specific to continuous data
    ],
    'classified_notes': [  # notes specific to classified data
    ],
    'single_event_notes': [  # notes specific to single event data
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'actions': [  # these are additional generic actions

    ],
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [],
    'allowed_geometries': [
        'polygon',
        'raster'
    ],
    'classifications': [generic_hazard_classes],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [],
    'field_groups': [],
    'layer_modes': [layer_mode_classified],
    'disabled_exposures': [exposure_place]
}
hazard_earthquake = {
    'key': 'earthquake',
    'name': tr('Earthquake'),
    'description': tr(
        'An earthquake describes the sudden violent shaking of the '
        'ground that occurs as a result of volcanic activity or movement '
        'in the earth\'s crust.'),
    'notes': [
        {
            'item_category': 'earthquake_general',
            'item_header': tr('earthquake general notes'),
            'item_list': [
                # additional generic notes for earthquake
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'continuous_notes': [  # notes specific to continuous data
    ],
    'classified_notes': [  # notes specific to classified data
    ],
    'single_event_notes': [  # notes specific to single event data
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'actions': [  # these are additional generic actions
    ],
    'earthquake_fatality_models': EARTHQUAKE_FUNCTIONS,  # Only for EQ
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [unit_mmi, unit_generic],
    'allowed_geometries': [
        'polygon',
        'raster'
    ],
    'classifications': [generic_hazard_classes, earthquake_mmi_scale],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [],
    'field_groups': [],
    'layer_modes': [layer_mode_classified, layer_mode_continuous],
    'disabled_exposures': [
        # exposure_place,  We want to be able to run some EQ realtime analysis.
        exposure_land_cover,
    ],
    'extra_keywords': earthquake_extra_keywords
}
hazard_flood = {
    'key': 'flood',
    'name': tr('Flood'),
    'description': tr(
        'A flood describes the inundation of land that is normally dry '
        'by a large amount of water. For example: A flood can occur '
        'after heavy rainfall, when a river overflows its banks or when a '
        'dam breaks. The effect of a flood is for land that is '
        'normally dry to become wet.'),
    'notes': [
        {
            'item_category': 'flood_general',
            'item_header': tr('flood general notes'),
            'item_list': [
                # additional generic notes for flood
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'continuous_notes': [  # notes specific to continuous data
    ],
    'classified_notes': [  # notes specific to classified data
    ],
    'single_event_notes': [  # notes specific to single event data
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'actions': [  # these are additional generic actions

    ],
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [unit_feet, unit_metres, unit_generic],
    'allowed_geometries': [
        'polygon',
        'raster'
    ],
    'classifications': [
        flood_hazard_classes,
        flood_petabencana_hazard_classes,
        generic_hazard_classes],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [],
    'field_groups': [],
    'layer_modes': [layer_mode_classified, layer_mode_continuous],
    'disabled_exposures': [exposure_place],
    'extra_keywords': flood_extra_keywords,
}
hazard_dam_break = {
    'key': 'dam_break',
    'name': tr('Dam Break'),
    'description': tr(
        'A Dam Break is a catastrophic type of failure '
        'characterized by the sudden, rapid, and uncontrolled '
        'release of impounded water as a result of structural '
        'failures or deficiencies in the dam. '
        'Dam Break can range from fairly minor to '
        'catastrophic, and can possibly harm human life and property '
        'downstream from the failure.'),
    'notes': [
        {
            'item_category': 'dam_break_general',
            'item_header': tr('dam break general notes'),
            'item_list': [
                # additional generic notes for flood
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'continuous_notes': [  # notes specific to continuous data
    ],
    'classified_notes': [  # notes specific to classified data
    ],
    'single_event_notes': [  # notes specific to single event data
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'actions': [  # these are additional generic actions

    ],
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [unit_feet, unit_metres, unit_generic],
    'allowed_geometries': [
        'polygon',
        'raster'
    ],
    'classifications': [
        inundation_dam_class,
        generic_hazard_classes],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [],
    'field_groups': [],
    'layer_modes': [layer_mode_classified, layer_mode_continuous],
    'disabled_exposures': [exposure_place]
}
hazard_cyclone = {
    'key': 'cyclone',
    'name': tr('Cyclone'),
    'description': tr(
        'A Tropical Cyclone is a rapidly rotating storm system '
        'characterised by a low-pressure centre, a closed low-level '
        'atmospheric circulation, strong winds, and a spiral arrangement '
        'of thunderstorms that produce heavy rain. It is also referred '
        'to as hurricane in the Atlantic Ocean or typhoon '
        'in the North West Pacific Ocean.'),
    'notes': [
        {
            'item_category': 'cyclone_general',
            'item_header': tr('cyclone general notes'),
            'item_list': [  # additional generic notes for flood - IF has more
                tr('The analysis performed here only considers the impact '
                   'of severe winds from tropical cyclones. The impact '
                   'of other associated hazards (storm surge inundation, '
                   'flood) must be analysed separately.'),
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'continuous_notes': [  # notes specific to continuous data
        tr(
            'Continuous data are normally used to represent the gust wind '
            'speed of the cyclone, representing the 10-m above ground wind '
            'speed.'
        )
    ],
    'classified_notes': [  # notes specific to classified data
        tr('Classified cyclone hazard data is not presently supported.')
    ],
    'single_event_notes': [  # notes specific to single event data
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'actions': [  # these are additional generic actions

    ],
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [
        unit_miles_per_hour,
        unit_kilometres_per_hour,
        unit_knots,
        unit_metres_per_second
    ],
    'allowed_geometries': [
        'polygon',
        'raster'
    ],
    'classifications': [
        cyclone_au_bom_hazard_classes,
        cyclone_sshws_hazard_classes,
        generic_hazard_classes
    ],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [],
    'field_groups': [],
    'layer_modes': [layer_mode_classified, layer_mode_continuous],
    'disabled_exposures': [
        exposure_road
    ]
}

hazard_volcanic_ash = {
    'key': 'volcanic_ash',
    'name': tr('Volcanic ash'),
    'description': tr(
        'Volcanic ash describes fragments of pulverized rock, minerals '
        'and volcanic glass, ejected into the atmosphere during volcanic '
        'eruptions.'),
    'notes': [
        {
            'item_category': 'volcanic_ash_general',
            'item_header': tr('volcanic ash general notes'),
            'item_list': [
                # additional generic notes for volcanic ash
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'continuous_notes': [  # notes specific to continuous data
    ],
    'classified_notes': [  # notes specific to classified data
    ],
    'single_event_notes': [  # notes specific to single event data
        tr('Volcanic ash is modelled hazard data estimating the thickness of '
           'ash on the ground following a volcanic eruption.')
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'actions': [
        {
            'item_category': 'additional_volcanic_ash',
            'item_header': tr('volcanic ash specific'),
            'item_list': [
                # these are additional volcanic ash actions
                tr('What action can be taken to secure water supplies and '
                   'protect crops?')
            ]
        }
    ],
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [unit_centimetres],
    'allowed_geometries': [
        'polygon',
        'raster'
    ],
    'classifications': [ash_hazard_classes, generic_hazard_classes],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [],
    'field_groups': [],
    'layer_modes': [layer_mode_classified, layer_mode_continuous],
    'disabled_exposures': [],
    'extra_keywords': ash_extra_keywords
}
hazard_tsunami = {
    'key': 'tsunami',
    'name': tr('Tsunami'),
    'description': tr(
        'A tsunami describes a large ocean wave or series or '
        'waves usually caused by an underwater earthquake or volcano. '
        'A tsunami at sea may go unnoticed but a tsunami '
        'wave that strikes land may cause massive destruction and '
        'flooding.'),
    'notes': [
        {
            'item_category': 'tsunami_general',
            'item_header': tr('tsunami general notes'),
            'item_list': [
                # additional generic notes for tsunami
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'continuous_notes': [  # notes specific to continuous data
    ],
    'classified_notes': [  # notes specific to classified data
    ],
    'single_event_notes': [  # notes specific to single event data
        tr('Tsunami hazard scenarios estimate the maximum extent of tsunami '
           'waves on land.')
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'actions': [  # these are additional tsunami actions

    ],
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [unit_feet, unit_metres],
    'allowed_geometries': [
        'polygon',
        'raster'
    ],
    'classifications': [
        tsunami_hazard_classes,
        tsunami_hazard_population_classes,
        tsunami_hazard_classes_ITB,
        tsunami_hazard_population_classes_ITB,
    ],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [],
    'field_groups': [],
    'layer_modes': [layer_mode_classified, layer_mode_continuous],
    'disabled_exposures': [exposure_place]
}
hazard_volcano = {
    'key': 'volcano',
    'name': tr('Volcano'),
    'description': tr(
        'A volcano describes a mountain which has a vent through '
        'which rock fragments, ash, lava, steam and gases can be ejected '
        'from below the earth\'s surface. The type of material ejected '
        'depends on the type of volcano.'),
    'notes': [
        {
            'item_category': 'volcano_general',
            'item_header': tr('volcano general notes'),
            'item_list': [
                # additional generic notes for volcano
                caveat_simulation,
                caveat_local_conditions,
                caveat_analysis_extent,
            ]
        }
    ],
    'actions': [  # these are additional volcano actions

    ],
    'continuous_notes': [  # notes specific to continuous data
    ],
    'classified_notes': [  # notes specific to classified data
    ],
    'single_event_notes': [  # notes specific to single event data
    ],
    'multi_event_notes': [  # notes specific to multi event data
    ],
    'citations': [
        {
            'text': None,
            'link': None
        }
    ],
    'continuous_hazard_units': [],
    'allowed_geometries': [
        'polygon',
        # 'raster'  # Disable per #3600
    ],
    'classifications': [volcano_hazard_classes, generic_hazard_classes],
    'compulsory_fields': [hazard_value_field],
    'fields': hazard_fields,
    'extra_fields': [hazard_name_field],
    'field_groups': [],
    'layer_modes': [layer_mode_classified],
    'disabled_exposures': [exposure_place]
}
hazard_all = [
    hazard_flood,
    hazard_tsunami,
    hazard_earthquake,
    hazard_volcano,
    hazard_volcanic_ash,
    hazard_cyclone,
    hazard_generic,
    hazard_dam_break
]
hazards = {
    'key': 'hazards',
    'name': tr('Hazards'),
    'description': concepts['hazard']['description'],
    'types': hazard_all,
    'citations': concepts['hazard']['citations']
}


import torch.nn as nn
import torch.nn.functional as F

class DQN(nn.Module):
    def __init__(self, n_state, n_action):
        super().__init__()
        self.emb = nn.Embedding(n_state, 64)
        self.ll1 = nn.Linear(in_features=64, out_features=64)
        self.ll2 = nn.Linear(in_features=64, out_features=64)
        self.out_features = nn.Linear(in_features=64, out_features=n_action)

    def forward(self, input_t):
        input_t = F.relu(self.ll1(self.emb(input_t)))
        input_t = F.relu(self.ll2(input_t))
        input_t = self.out_features(input_t)
        return input_t


from django.http import HttpResponseRedirect

class OneToManyMixin(object):
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk', None)
        self.object = self.model.objects.get(pk=pk) if pk else None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        formset = self.formset_class()
        return self.render_to_response(
            self.get_context_data(form=form, formset=formset))

    def post(self, request, *args, **kwargs):
        pk = kwargs.get('pk', None)
        self.object = self.model.objects.get(pk=pk) if pk else None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        formset = self.formset_class()

        # Form and formset objects are saved iff form and formset BOTH valid.
        if form.is_valid():
            self.object = form.save(commit=False)
            formset = self.formset_class(request.POST, instance=self.object)
            if formset.is_valid():
                return self.form_valid(form, formset)
        else:
            formset = self.formset_class(request.POST)
        return self.form_invalid(form, formset)

    def form_valid(self, form, formset):
        self.object = form.save()
        formset.save()
        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, form, formset):
        return self.render_to_response(self.get_context_data(form=form,
                                                             formset=formset))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 21 11:25:14 2019
@author: polo
"""

'https://www.machinelearningplus.com/nlp/topic-modeling-gensim-python/'

import re

# spacy for lemmatization
import spacy

# Gensim
import gensim
import gensim.corpora as corpora
from gensim.utils import simple_preprocess
from gensim.models import CoherenceModel

# NLTK Stop words
from nltk.corpus import stopwords

# Enable logging for gensim - optional
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.ERROR)

import warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)

Swears = ['arse', 'arsehole', 'ass', 'ass-hat', 'ass-jabber', 'ass-pirate', 'assbag', 'assbandit',
          'assbanger', 'assbite', 'assclown', 'asscock', 'asscracker', 'asses', 'assface', 'assfuck',
          'assfucker', 'assgoblin', 'asshat', 'asshead', 'asshole', 'asshopper', 'assjacker', 'asslick',
          'asslicker', 'assmonkey', 'assmunch', 'assmuncher', 'assnigger', 'asspirate', 'assshit',
          'assshole', 'asssucker', 'asswad', 'asswipe', 'axwound', 'balls', 'bastard', 'beaver',
          'beef curtains', 'bellend', 'bint', 'bitch', 'bloodclaat', 'bloody', 'bollocks', 'bugger',
          'bullshit', 'child-fucker', 'christ on a bike', 'christ on a cracker', 'clunge', 'cock',
          'crap', 'cunt', 'damn', 'dick', 'dickhead', 'fanny', 'feck', 'flaps', 'frigger', 'fuck', 'fucking','gash',
          'ginger', 'git', 'goddam', 'goddamn', 'godsdamn', 'hell', 'holy shit', 'jesus', 'jesus christ',
          'jesus h. christ', 'jesus harold christ', 'jesus wept', 'jesus, mary and joseph', 'judas priest',
          'knob', 'minge', 'minger', 'motherfucker', 'munter', 'nigga', 'nigger', 'pissed', 'pissed off',
          'prick', 'punani', 'pussy', 'shit', 'shit ass', 'shitass', 'snatch', 'sod-off', 'son of a bitch',
          'son of a motherless goat', 'son of a whore', 'sweet jesus', 'tits ', 'twat ', 'twatanus']

stop_words = stopwords.words('english')
#______________________________________________________________________________

def lemmatization(texts, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']):
    """https://spacy.io/api/annotation"""
    nlp = spacy.load('en', disable=['parser', 'ner'])

    texts_out = []
    for sent in texts:
        doc = nlp(" ".join(sent))
        texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags])
    return texts_out
#______________________________________________________________________________

def Preprocessing(Text):
    # Remove Urls
    CleanText = re.sub(r'http\S+', '', Text)
    # Remove Emails
    CleanText = re.sub('\S*@\S*\s?', '', CleanText)

    words = []
    # Remove StopWords ,Swears andtoken less than 3 chars
    for t in simple_preprocess(CleanText, deacc=True):
        if t not in stop_words and t not in Swears:
           if len(t)>2:
              words.append(t)

    CleanText = " ".join(words)

    return CleanText

#______________________________________________________________________________

def PrepareData(DocList):

    Clean_DocList = []
    All_Words = []
    Data_lemma = []

    for doc in DocList:
        CleanText = Preprocessing(doc)
        if CleanText:
            Clean_DocList.append(CleanText)
            words = CleanText.split()
            All_Words.append(words)
    # Do lemmatization keeping only noun, adj, vb, adv
    Data_lemma = lemmatization(All_Words, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV'])
    Data_lemma = list(filter(None, Data_lemma))
    return Data_lemma

#______________________________________________________________________________

def LDA(data_lemmatized,num_topics):
    # Create Dictionary
    id2word = corpora.Dictionary(data_lemmatized)

    # Create Corpus
    texts = data_lemmatized

    # Term Document Frequency
    corpus = [id2word.doc2bow(text) for text in texts]

    # Build LDA model
    lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
                                                id2word=id2word,
                                                num_topics=num_topics,
                                                random_state=100,
                                                update_every=1,
                                                chunksize=100,
                                                passes=10,
                                                alpha='auto',
                                                per_word_topics=True)

    return lda_model,id2word,corpus

#______________________________________________________________________________

def Topics_Words(lda_model,num_words):
    Topic_Words = []
    #for index, topic in lda_model.show_topics(formatted=False, num_words= 30):
        #print('Topic: {} \nWords: {}'.format(index, [w[0] for w in topic]))
    for index, topic in lda_model.show_topics(formatted=False, num_words= num_words):
        Topic_Words.append([w[0] for w in topic])
    return Topic_Words

#______________________________________________________________________________

def Perplexity_Coherence(lda_model,id2word,corpus,data_lemmatized):
    # Compute Perplexity
    Perplexity = lda_model.log_perplexity(corpus)# a measure of how good the model is. lower the better.

    # Compute Coherence Score
    coherence_model_lda = CoherenceModel(model=lda_model, texts=data_lemmatized, dictionary=id2word, coherence='c_v')
    coherence_lda = coherence_model_lda.get_coherence()

    return Perplexity,coherence_lda


import torch
import torch.nn as nn
from StyleTransfer.lib.lst_dependency import LSTEncoder5, LSTEncoder4, TransLayer, LayerwiseLoss
from StyleTransfer.lib.vgg19_parts import Encoder3, Decoder3, Decoder4
from StyleTransfer.config import get_encoder_model, get_decoder_model, get_parts
from StyleTransfer.lib.SPN.SPN import SPN

class LinearStyleTransfer(nn.Module):
    def __init__(self, cfg):
        super(LinearStyleTransfer, self).__init__()
        self.cfg = cfg
        disable_transfer = cfg.MODEL.LST.DISABLE_TRANSFER
        load_loss_net = cfg.MODEL.LST.LOAD_LOSS_NET
        disable_spn = cfg.MODEL.LST.DISABLE_SPN

        if cfg.MODEL.LST.LAYER == 'r31':
            self.encoder = Encoder3()
            self.decoder = Decoder3()
            if not disable_transfer:
                self.trans_layer = TransLayer(layer='r31', matrix_size=cfg.MODEL.LST.MATRIX_SIZE)
        elif cfg.MODEL.LST.LAYER == 'r41':
            self.encoder = LSTEncoder4()
            self.decoder = Decoder4()
            if not disable_transfer:
                self.trans_layer = TransLayer(layer='r41', matrix_size=cfg.MODEL.LST.MATRIX_SIZE)
        else:
            raise NotImplementedError('Invalid argument setting of MODEL.LST.LAYER: only r31 and r41 is supported.')

        # VGG loss module
        if load_loss_net:
            self.loss_network = LSTEncoder5()

        # load pretrained model
        encoder_paths = get_encoder_model(cfg)
        self.encoder.load_state_dict(torch.load(encoder_paths[0]))
        if load_loss_net:
            self.loss_network.load_state_dict(torch.load(encoder_paths[1]))

        decoder_paths = get_decoder_model(cfg)
        self.decoder.load_state_dict(torch.load(decoder_paths[0]))

        # disable training of encoder, decoder, loss_network
        # we only train trans_layer
        for param in self.encoder.parameters():
            param.requries_grad = False

        for param in self.decoder.parameters():
            param.requires_grad = False

        if load_loss_net:
            for param in self.loss_network.parameters():
                param.requires_grad = False

            self.criterion = LayerwiseLoss(
                cfg.MODEL.LST.STYLE_LAYERS,
                cfg.MODEL.LST.CONTENT_LAYERS,
                cfg.MODEL.LST.STYLE_WEIGHT,
                cfg.MODEL.LST.CONTENT_WEIGHT
            )

        if not disable_spn:
            self.SPN = SPN(spn=cfg.MODEL.LST.SPN_NUM)
            self.criterion_spn = nn.MSELoss(reduction='sum')

    def forward_with_trans(self, content, style):
        f_c = self.encoder(content)  # (N, C, H1, W1)
        f_s = self.encoder(style)  # (N, C, H1, W1)
        if self.cfg.MODEL.LST.LAYER == 'r41':
            feature, trans_matrix = self.trans_layer(f_c[self.cfg.MODEL.LST.LAYER], f_s[self.cfg.MODEL.LST.LAYER])
        else:
            feature, trans_matrix = self.trans_layer(f_c, f_s)
        t = self.decoder(feature)

        return t

    def cal_trans_loss(self, t, content, style):
        f_t = self.loss_network(t)
        f_s = self.loss_network(style)
        f_c = self.loss_network(content)

        loss, loss_style, loss_content = self.criterion(f_t, f_s, f_c)

        return loss, loss_style, loss_content

    def forward_with_no_trans(self, x):
        feat = self.encoder(x)
        x_prime = self.decoder(feat)
        return x_prime

    def forward_spn(self, distorted, target):
        return self.SPN(distorted, target)

    def cal_spn_loss(self, input, target):
        return self.criterion_spn(input, target)

    def load(self, cfg):
        parts_paths = get_parts(cfg)
        idx = 0
        if not cfg.MODEL.LST.DISABLE_TRANSFER:
            # load trans layer
            self.trans_layer.load_state_dict(torch.load(parts_paths[idx]))
            idx += 1
        if not cfg.MODEL.LST.DISABLE_SPN:
            # load spn
            self.SPN.load_state_dict(torch.load(parts_paths[idx]))


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

import sys

from torchvision import transforms

def get_data_transform(data: str):
    if data == 'cifar':
        train_transform = transforms.Compose([
            transforms.RandomCrop(32, padding=4),   # input arguments: length&width of a figure
            #transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),  # convert PIL image or numpy.ndarray to tensor
            #transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
            # transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
        ])

        test_transform = transforms.Compose([
            transforms.RandomCrop(32, padding=4),
            transforms.ToTensor(),
            #transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
            # transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
        ])
    else:
        print('Data must be {} or {} !'.format('mnist', 'cifar'))
        sys.exit(-1)

    return train_transform, test_transform


from bs4 import BeautifulSoup
from park_api.util import convert_date, utc_now
from park_api.geodata import GeoData
import datetime

geodata = GeoData(__file__)

def parse_html(html):
    soup = BeautifulSoup(html, "html.parser")

    # last update time (UTC)
    # Konstanz does not support the last_updated yet. I hope they will inform me when it's added
    # as the data seems accurate I will return the current time and date
    data = {
        "last_updated": utc_now(),
        "lots": []
    }

    # get all tables with lots
    parken = soup.find_all( "table", class_="parken")

    # get all lots
    for park_lot in parken :
        td = park_lot.find_all("td")
        parking_name = td[0].text.strip()
        if parking_name == "Parkmöglichkeit":
            continue
        # work-around for the Umlaute-problem: ugly but working
        if ( 'Marktst' in parking_name) : parking_name = 'Marktstätte'
        elif ( 'bele' in parking_name) : parking_name = 'Döbele'
        # get the data
        lot = geodata.lot(parking_name)
        # look for free lots
        parking_state = 'open'
        parking_free  = 0
        try:
            parking_free = int(td[1].text)
        except:
            parking_state = 'nodata'

        data["lots"].append({
            "name":     parking_name,
            "free":     parking_free,
            "total":    lot.total,
            "address":  lot.address,
            "coords":   lot.coords,
            "state":    parking_state,
            "lot_type": lot.type,
            "id":       lot.id,
            "forecast": False
        })

    return data


# Задайте натуральное число N.
# Напишите программу, которая составит список простых множителей числа N.

def Find_miltipliers (num, count, list):
    while count < num+1:
        if num % count != 0:
            count += 1
        elif num % count == 0:
            list.append(count)
            count += 1
    return list
num = int(input('Введите натуральное число: '))
print(f'Простые множители числа {num}: {Find_miltipliers(num, 1, [])}')


# Melhore o jogo do desafio 028 onde o computador vai "pensar" em um número entre 0 e 10.
# só que agora o jogador vai tentar adivinhar até acertar.
# Mostre no final quantos palpites foram necessários para vencer.

from random import randint
from time import sleep
print('\033[;35mMY NAME IS THIAGO MACHINE...\033[m')
sleep(2)
pc = randint(0, 10)
print('\033[;35mAcabei de pensar em um número entre 0 e 10\033[m')
sleep(2)
print('\033[;35mSerá que você consegue adivinhar?\033[m')
sleep(2)
nome = str(input('\033[;36mQual o seu nome?\033[m')).strip().capitalize()
eu = ''
tentativas = 0
while pc != eu:
    eu = int(input('\033[;33mQual o seu chute? '))
    tentativas += 1
    if pc == eu:
        sleep(2)
        print('PARABÉNS, {}! Você acertou, também pensei no número {}'.format(nome, pc))
        print('Você acertou com {} tentativas'.format(tentativas))
    if pc > eu:
        sleep(2)
        print('Mais... Tente novamente.')
    if pc < eu:
        sleep(2)
        print('Menos... Tente novamente.')


from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.views import APIView
from django.db.models import Q

from .models import MeasuringData, MeasuringStands, MeasuringStation, IndexQuality, City

from .serializers import (
    IndexQualitySerializer,
    MeasuringDataSerializer,
    MeasuringStandsSerializer,
    MeasuringStationSerializer,
)

class StationsList(APIView):
    def get(self, request, format=None):
        stations = MeasuringStation.objects.all()
        serializer = MeasuringStationSerializer(stations, many=True)

        return Response(serializer.data)

class CityStations(APIView):
    def get(self, request, format=None):

        cities = City.objects.all()
        name = self.request.query_params.get("search", None)
        if name:
            cities = City.objects.filter(
                Q(name__icontains=name)
                | Q(commune_name__icontains=name)
                | Q(district_name__icontains=name)
                | Q(province_name__icontains=name)
            )

        stations_group = [
            MeasuringStation.objects.filter(cities=city.id) for city in cities
        ]

        response = []
        for stations in stations_group:
            for station in stations:
                serializer = MeasuringStationSerializer(station)
                response.append(serializer.data)

        return Response(response)

class MeasuringStandsList(APIView):
    def get(self, request, pk, format=None):
        measuring_stands = MeasuringStands.objects.filter(stations=pk)
        serializer = MeasuringStandsSerializer(measuring_stands, many=True)

        return Response(serializer.data)

class MeasuringDataList(APIView):
    def get(self, request, pk, format=None):
        measuring_data = MeasuringData.objects.filter(sensors=pk).order_by("-date")
        serializer = MeasuringDataSerializer(measuring_data, many=True)

        return Response(serializer.data)

class QualityIndicators(APIView):
    def get(self, request, pk, format=None):

        index_quality = IndexQuality.objects.get(pk=pk)
        serializer = IndexQualitySerializer(index_quality)

        return Response(serializer.data)

# class PrivateStation():
#     def post(self):
#         json_data = request.get_json(force=True)
#         name = json_data['stationName']
#         print(name)


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 19 16:38:10 2020

@author: PRANIKP

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

Input: [3,2,3]
Output: 3
Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

Time Complexity:O(n)
Space Complexity:O(n)
"""

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        dt ={}

        for x in nums:

            if x in dt:
                dt[x] += 1
            else:
                dt[x] = 1

        for x in dt:

            if dt[x]> len(nums)//2:
                return x


# ===================================
# sudo-code

# BFS(G, start_v)
#   let Q be a queue
#    label start_v as discovered
#     Q.enqueue(start_v)
#     while Q is not empty do
#         v := Q.dequeue()
#         if v is the goal then
#             return v
#         for all edges from v to w in G.adjacentEdges(v) do
#             if w is not labeled as discovered then
#                 label w as discovered
#                 w.parent := v
#                 Q.enqueue(w)

def iterative_bfs(start_v):
    discovered = [start_v]
    queue = [start_v]
    while queue:
        v = queue.pop(0)
        for w in graph[v]:
            if w not in discovered:
                discovered.append(w)
                queue.append(w)

    return discovered


"""
Title:      Create Oligos
Author:     Jakob Jung
Date:       10-06-2020
Details:    functions which take in fasta & gff file and create specific oligo sequences  in FASTA format
"""

from BCBio import GFF
from Bio.SeqFeature import FeatureLocation
from Bio import SeqIO
import os
import re
import pandas as pd

def chromosomes_parsed(in_gff, ref_dict):
    """Parse gff output, generating SeqRecord and SeqFeatures for chromosome & plasmids of fasta
    """
    for rec in GFF.parse(in_gff,  base_dict=ref_dict):
        yield rec

def get_seqdict(fasta_file):
    """returns sequence dictionary of inputted fasta file for later use.

    :param fasta_file: input file of chromosomes in fasta format
    :return: returns sequence dictionary in dict format
    """
    with open(fasta_file) as fasta:
        seq_dict = SeqIO.to_dict(SeqIO.parse(fasta, "fasta"))
    return seq_dict

def make_oligos(cds, feature,  record, file, length_prom, length_orf):
    """ appends an oligo to fasta file per cds

    :param length_orf: length of ORF (we use 120)
    :param length_prom: lengt of promoter (we use 30)
    :param feature: feature of sequence (usually gene)
    :param cds: coding sequence, SeqRecord
    :param record: record (of chromosome), SeqRecord
    :param file: fasta file, path
    """
    # only extract cds regions:
    if cds.type != "CDS":
        raise KeyError

    # create sequence start and end depending on strandedness:
    if feature.strand == -1:
        end = cds.location.end + length_prom
        start = cds.location.end - 3 - length_orf
        start_codon = FeatureLocation(cds.location.end - 3, cds.location.end).extract(record.seq).reverse_complement()
    else:
        start = cds.location.start
        end = start + 3 + length_orf
        start = start - length_prom
        start_codon = FeatureLocation(cds.location.start, cds.location.start + 3).extract(record.seq)
    # do warning if start codon is not usual (ATG/GTG/TTG)
    if start_codon not in ["ATG", "GTG", "TTG", "ATT", "CTG"]:
        print("Start codon for {} not ATG, GTG, TTG, ATT and CTG!!!".format(cds.qualifiers['gene'][0]))
    # get locus tags and gene name (if name is available)
    locus_tag = feature.qualifiers["locus_tag"][0]
    try:
        gene = cds.qualifiers['gene'][0]
    except KeyError:
        gene = "unknown gene name"
    # generate possible sequences, which have to include start codon (3 nt), orf (120 nt) and promoter (30 nt):
    sequence = record.seq
    f = open(file, "a")
    oligo_location = FeatureLocation(start, end)
    oligo_target = oligo_location.extract(sequence)
    if feature.strand == -1:
        oligo_target = oligo_target.reverse_complement()
    oligo_rec = SeqIO.SeqRecord(oligo_target, id="{}; {}".format(locus_tag, gene),
                              name="{}".format(locus_tag), description="{}".format(feature.strand))
    # add T7 promoter, stop codon and 3'UTR:
    t7 = "GTTTTTTTTAATACGACTCACTATAGGG"
    stop_utr_3 = "TAATCTTTTGTAGATTGCACTTGCTTAAAAT"
    oligo_final = t7 + oligo_rec + stop_utr_3
    #SeqIO.write(oligo_final, f, "fasta")
    f.write("JVOpool-001" +"\t" + str(oligo_final.seq)+"\n")
    # check length (if it is as wanted):
    if len(oligo_final) != (len(t7 + stop_utr_3) + 3 + length_orf + length_prom):
        print("length of cds {} not normal: {}".format(locus_tag, len(oligo_final)))
    f.close()

def create_oligos(in_gff, in_fasta, output_file, length_prom, length_orf):
    """creates specific oligos_2 from gff file and saves them in a fasta file

    :param length_orf: length of ORF (we use 120)
    :param length_prom: lengt of promoter (we use 30)
    :param output_file: file to save the foutput fasta file
    :param in_gff: gff annotation file of genes, path
    :param in_fasta: fasta files of chromosomes, path
    """
    # delete output file if it already exists:
    if os.path.exists(output_file):
        os.remove(output_file)
    # create header for output tabfile:
    with open(output_file, "w") as f:
        f.write("Pool name" + "\t" + "Sequence" + "\n")

    seqdict = get_seqdict(in_fasta)
    with open(in_gff) as in_handle:
        chroms = chromosomes_parsed(in_handle, seqdict)
        genes = 0
        # loop through all chromosome records within the gff file:
        for record in chroms:
            # loop through all features (genes/sRNAs/...) for each chromosome record (record):
            for feature in record.features:
                # now loop through all sub-features (cds) of the genes:
                for cds in feature.sub_features:
                    # I use try/except for the case that there's no locus tag available (-> KeyError):
                    try:
                        make_oligos(cds, feature, record, output_file, length_prom, length_orf)
                        genes += 1
                    except KeyError:
                        continue
    print("{} oligos_2 created and written to fasta file: {}".format(genes, output_file))

if __name__ == '__main__':
    # define paths for annotation GFF file and FASTA (should be input by user):
    ecoli_fa = "../data/e_coli_ref_seqs/NC_000913.3.fasta"
    ecoli_gff = "../data/e_coli_ref_seqs/NC_000913.3.gff"
    # create the fasta file with oligo sequences
    create_oligos(ecoli_gff, ecoli_fa, "../data/oligo_order/oligos_ecoli.tab", 30, 150)

    with open("../data/e_coli_ref_seqs/NC_000913.3.gff", 'r') as gff:
        with open("../data/oligos_cds.gff", 'w') as new_gff:
            for line in gff:
                line_splitted = line.split("\t")
                #print(line_splitted)
                if len(line_splitted) != 9:
                    continue
                line_splitted[3] = "1"
                line_splitted[4] = "183"
                annotations = line_splitted[8]
                match = ".*;locus_tag=(b\\d\\d\\d\\d).*"
                m = re.search(match, annotations)
                if m is None:
                    continue
                if not line_splitted[2] == "gene":
                    continue
                lt = m.group(1)
                print(lt)
                if line_splitted[6] == "+":
                    id = lt + " " + "1"
                else:
                    id = lt + " " + "-1"
                print(id)
                line_splitted[0] = id
                #print(annotations)
                #print(line_splitted)
                newline = "\t".join(line_splitted)
                new_gff.write(newline)


from src.vgg16 import Vgg16
from src.utils import get_logger

logger = get_logger()

batch_size = 4
epoch = 2
path = 'data/dogscats/'

vgg = Vgg16(file_path='models/', vgg_weights='vgg16.h5')
batches = vgg.get_batches(path + 'train', batch_size=batch_size)
val_batches = vgg.get_batches(path + 'valid', batch_size=batch_size * 2)
vgg.finetune(batches)
vgg.fit(batches, val_batches, epoch=epoch)

model_path = 'models/dogs_cats_epoch{}.h5'.format(epoch)
logger.info('Saving model to {} ...'.format(model_path))
vgg.model.save(model_path)
logger.info('Done.')


from gbbs_lib import *
import gbbs_lib
import os

def loadGraph(graphPath="",undirected=True, compressed=False, binary=False):
  if (graphPath == ""):
    print("Expect a non-empty path to the graph input file")
    exit(0)
  if (undirected):
    if (compressed):
      return gbbs_lib.readCompressedSymmetricUnweightedGraph(graphPath)
    else:
      return gbbs_lib.readSymmetricUnweightedGraph(graphPath, binary)
  else:
    if (compressed):
      return gbbs_lib.readCompressedAsymmetricUnweightedGraph(graphPath)
    else:
      return gbbs_lib.readAsymmetricUnweightedGraph(graphPath, binary)

def loadFloatGraph(graphPath="", undirected=True):
  if (graphPath == ""):
    print("Expect a non-empty path to the graph input file")
    exit(0)
  if (undirected):
    return gbbs_lib.readSymmetricFloatWeightedGraph(graphPath, binary)
  else:
    return gbbs_lib.readAsymmetricFloatWeightedGraph(graphPath, binary)

def loadSnap(graphPath="", undirected=True):
  if (graphPath == ""):
    print("Expect a non-empty path to the graph input file")
    exit(0)
  graphName = os.path.splitext(graphPath)[0]+'.adj'
  if (undirected):
    return gbbs_lib.loadSymmetricEdgeListAsGraph(graphPath, graphName)
  else:
    return gbbs_lib.loadAsymmetricEdgeListAsGraph(graphPath, graphName)
  print("Unsupported options")
  exit(0)

def loadFloatSnap(graphPath="", undirected=True):
  if (graphPath == ""):
    print("Expect a non-empty path to the graph input file")
    exit(0)
  if (undirected):
    return gbbs_lib.loadSymmetricFloatEdgeListAsGraph(graphPath)
  else:
    return gbbs_lib.loadAsymmetricFloatEdgeListAsGraph(graphPath)
  print("Unsupported options")
  exit(0)

def loadFromEdgeList(edges, symmetric=True, weighted=False):
  if (symmetric):
    if (not weighted):
      return gbbs_lib.numpyEdgeListToSymmetricUnweightedGraph(edges)
    else:
      if (edges.dtype == "float64" or edges.dtype == "float32"):
        return gbbs_lib.numpyFloatEdgeListToSymmetricWeightedGraph(edges)
      elif (edges.dtype == "uint64"):
        return gbbs_lib.numpyUintEdgeListToSymmetricWeightedGraph(edges)
      else:
        print("unknown edge type")
        print(edges.dtype)
        exit(0)
  else:
    exit(0)

#def test_numpy_array(np_arr):
#  return gbbs_lib.testNumpyArray(np_arr)


import random
import math

def rand(n):
  count = 0
  x = 0
  for i in range(n):
    xnew = random.random()
    if xnew > x:
      x = xnew
      count += 1
    else:
      x = xnew
  percent = (count / n) * 100
  print("Percent: ", percent, "%")

def mers(s, n):
# This program calculates Mersenne primes
# between 0 and 1
# Test randomness of the values by calculating the
# the percentage of numbers greater than the previous
# n = number of primes needed
# s = seed
  k = 16807            #7^5
  j = 2147483647       #2^31-1
  x = 0                #intialize x
  count = 0            #count larger numbers

  for i in range(n):   #Repeat n times
    s = k * s % j
    xnew = s / j
    if xnew > x:       #If the new value is greater
      x = xnew         #Sets x to the value of the new value
      count += 1       #Add 1 to the count of larger numbers
    else:
      x = xnew
  percent = (count / n) * 100  #Calculates % of larger numbers
  print("Percent: ", percent, "%")

rand(10000)
mers(2, 10000)

def loaded(n):
  count = []
  count.append(0)
  count.append(0)
  count.append(0)
  count.append(0)
  count.append(0)
  count.append(0)

  for i in range(n):
    x = random.random()
    if x < 0.15:
      count[0] += 1
    elif x < 0.35:
      count[1] += 1
    elif x < 0.60:
      count[2] += 1
    elif x < 0.75:
      count[3] += 1
    elif x < 0.85:
      count[4] += 1
    elif x < 1:
      count[5] += 1
  percent = []
  percent.append((count[0] / n) * 100)
  percent.append((count[1] / n) * 100)
  percent.append((count[2] / n) * 100)
  percent.append((count[3] / n) * 100)
  percent.append((count[4] / n) * 100)
  percent.append((count[5] / n) * 100)
  print(percent)

loaded(1500)

def drunk(n):
  count = []
  count.append(0)  # x coordinate
  count.append(0)  # y coordinate
  outsideOf20 = 0
  for i in range(n):
    for i in range (50):
      x = random.random()
      if x < (1/6):
        count[0] += 1
      elif x < ((1/6) + (1/4)):
        count[1] += 1
      elif x < ((1/6) + (1/2)):
        count[1] -= 1
      elif x < 1:
        count[0] -= 1
      dis = math.sqrt(count[0] ** 2 + count[1] ** 2)
      if dis <= 20:
        outsideOf20 += 1
    print(outsideOf20)
  print((outsideOf20 / n) * 100, "%")

drunk(100)


from jarvis import Ui_jarvisui
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QMovie
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt , QTimer ,QDate
from PyQt5.uic import loadUiType
import v1
import os
import sys

class MainThread(QThread):

    def __init__(self):
        super(MainThread,self).__init__()

    def run(self):
        v1
startexec=MainThread()
class Gui_Start(QMainWindow):
    def __init__(self):
        super().__init__()

        self.gui=Ui_jarvisui()
        self.gui.setupUi(self)
        self.gui.start.clicked.connect(self.starttask)
        self.gui.terminate.clicked.connect(self.close)
    def starttask(self):
        self.gui.label1=QtGui.QMovie("voi.gif")
        self.gui.gif_1.setMovie(self.gui.label1)
        self.gui.label1.start()

        self.gui.label2=QtGui.QMovie("init.gif")
        self.gui.gif_2.setMovie(self.gui.label2)
        self.gui.label2.start()

        timer=QTimer(self)
        timer.timeout.connect(self.showTimeLive)
        timer.start(999)
        startexec.start()
    def showTimeLive(self):
        t=QTime.currentTime()
        time=t.toString()
        d=QDate.currentDate()
        date=d.toString()
        label_time="Time: " + time
        label_date="Date: " + date
        self.gui.text_time.setText(label_time)
        self.gui.text_date.setText(label_date)
guiapp=QApplication(sys.argv)
jarvis_gui=Gui_Start()
jarvis_gui.show()
exit(guiapp.exec_())


#!/usr/bin/python3

import os, platform
if platform.system() == "Linux" or platform.system() == "Darwin":
    os.environ["KIVY_VIDEO"] = "ffpyplayer"

from pysimbotlib.core import PySimbotApp, Robot

class GoStraightRobot(Robot):

    def update(self):
        self.move(5)

class TurnAroundRobot(Robot):

    def update(self):
        self.turn(5)

def create_robots():
    robot1 = GoStraightRobot()
    robot2 = TurnAroundRobot()
    return [robot1, robot2]

if __name__ == '__main__':
    # customfn_create_robots will override the default create_robots function which is implemented by [robot_cls() for _ in range(num_robots)]
    app = PySimbotApp(customfn_create_robots=create_robots)
    app.run()


from sklearn import svm
from .SklearnClf import SklearnClf

class SVM(SklearnClf):
    def __init__(self, C, kernel="poly"):
        super().__init__(name="SVM")
        self.clf = svm.SVC(C=C, kernel=kernel,probability=True)

    PARAMS_TO_OPTIMIZE = {
        'test_size': {
            'init': .2,
            'type': 'float',
            'range': (.1, .3)
        },
        'is_fixed': {
            'init': True,
            'type': 'bool',
            'range': [True, False]
        },
        'C': {
            'init': 1.0,
            'type': 'float',
            'range': (.0001,1000.0)
        },
        'kernel': {
            'init': 2,
            'type': 'enum',
            'range': ['linear', 'poly', 'rbf', 'sigmoid', 'precomputed']
        },
        'degree': {
            'init': 3,
            'type': 'int',
            'range': (2,5)
        },
        'gamma': {
            'init': 0,
            'type': 'enum',
            'range': ['scale', 'auto']
        },
        'shrinking': {
            'init': True,
            'type': 'bool',
            'range': [True, False]
        },
        'probability': {
            'init': True,
            'type': 'bool',
            'range': [True, False]
        }
    }


import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.patches as patchs
from matplotlib.animation import FuncAnimation
from triplestagesolver import Tsolver, Csolver
from nanovna import NanoVNA
from time import sleep
import threading
import numpy as np
import math
import random
import utils

freqlist = utils.loaddatafile(".calibrate")[0]

MYRED = "#FF6347"
MYBLUE = "#87CEFA"
MYDEEPBLUE = "#1E90FF"
MYBLACK = "#000000"
MYGRAY = "#C0C0C0"

moisture = True
moi = lambda x:MYBLUE if x else MYGRAY

moisturecolor = moi(moisture)
Z22value = utils.loaddatafile(".calibrate")[1]
slider = 0.5
sliderheight = 0.1
pressfill = [False, True, False]
presscolor = [MYGRAY, MYBLUE, MYGRAY]
pressstyle = ["dashed", "", "dashed"]
presstext = ["", "medium", ""]
#construct figure
plt.rcParams["toolbar"] = "None"
fig = plt.figure(constrained_layout=True, figsize=(12, 6))
fig.canvas.set_window_title("   ")
gs = gridspec.GridSpec(nrows=12, ncols=10, figure=fig)

axZ22 = fig.add_subplot(gs[:, :6])
axSld = fig.add_subplot(gs[:, 6:8])
axMoi = fig.add_subplot(gs[:4, 8:10])
axPre = fig.add_subplot(gs[4:12, 8:10])

axZ22.spines["right"].set_visible(False)
axZ22.spines["top"].set_visible(False)
axZ22.spines["left"].set_position(("data", 0))
axZ22.xaxis.set_ticks_position("bottom")
Z22, = axZ22.plot(freqlist, np.absolute(Z22value), animated=True)
axZ22.set_xticks([*np.arange(0, 41e6, 5e6), 1e6])
axZ22.set_xticklabels([*np.arange(0, 41, 5), 1])
axZ22.set_ylim(0, 1.05)
axZ22.set_yticks(np.arange(0, 1.05, 0.2))
axZ22.set_title("NanoVNA monitor")
axZ22.set_xlabel("frequency(MHz)")
axZ22.set_ylabel("S11 parameter")
axZ22.fill_betweenx([1.05, 0], 1e6, 7e6, color=MYBLUE, alpha=0.1)
axZ22.fill_betweenx([1.05, 0], 8e6, 13e6, color=MYBLUE, alpha=0.2)
axZ22.fill_betweenx([1.05, 0], 14e6, 20e6, color=MYBLUE, alpha=0.3)
axZ22.grid(linestyle="-.")

Sld = []
axSld.set_title("slider", y=-0.06)
axSld.spines["left"].set_visible(False)
axSld.spines["right"].set_visible(False)
axSld.spines["top"].set_visible(False)
axSld.spines["bottom"].set_visible(False)
axSld.spines["bottom"].set_position(("data", 0))
axSld.axes.xaxis.set_visible(False)
axSld.axes.yaxis.set_visible(False)
axSld.set_ylim(0, 1.05)
axSld.set_xlim(-0.3, 1.3)
Sld = []
Sld.append(patchs.Rectangle((0.2, 0), 0.6, slider - sliderheight, color=MYGRAY, animated=True))
Sld.append(patchs.Rectangle((0.2, slider - sliderheight), 0.6, 2 * sliderheight, color=MYBLUE, animated=True))
Sld.append(patchs.Rectangle((0.2, slider + sliderheight), 0.6, 1 - slider - sliderheight, color=MYGRAY, animated=True))
for item in Sld:
        axSld.add_patch(item)

serp_height = 0.2
serp_len = 0.5
serp_turn = 4
serp_intv = 0.08
serp_off = 0.02
serpentine_x = []
serpentine_y = []
serpentine_x.append(0.5 - serp_len/2)
serpentine_y.append(serp_height)
for i in range(0, 2 * serp_turn, 2):
        serpentine_x.append(0.5 + serp_len/2)
        serpentine_y.append(serp_height + i * serp_intv)

        serpentine_x.append(0.5 + serp_len/2 + serp_off)
        serpentine_y.append(serp_height + (i + 0.3) * serp_intv)
        serpentine_x.append(0.5 + serp_len/2 + serp_off)
        serpentine_y.append(serp_height + (i + 0.7) * serp_intv)

        serpentine_x.append(0.5 + serp_len/2)
        serpentine_y.append(serp_height + (i + 1) * serp_intv)
        serpentine_x.append(0.5 - serp_len/2)
        serpentine_y.append(serp_height + (i + 1) * serp_intv)

        serpentine_x.append(0.5 - serp_len/2 - serp_off)
        serpentine_y.append(serp_height + (i + 1.3) * serp_intv)
        serpentine_x.append(0.5 - serp_len/2 - serp_off)
        serpentine_y.append(serp_height + (i + 1.7) * serp_intv)

        serpentine_x.append(0.5 - serp_len/2)
        serpentine_y.append(serp_height + (i + 2) * serp_intv)
serpentine_x.append(0.5 + serp_len/2)
serpentine_y.append(serp_height + serp_turn * 2 * serp_intv)

axMoi.set_title("moisture sensor", y=-0.1)
axMoi.spines["left"].set_visible(False)
axMoi.spines["right"].set_visible(False)
axMoi.spines["top"].set_visible(False)
axMoi.spines["bottom"].set_visible(False)
axMoi.axes.xaxis.set_visible(False)
axMoi.axes.yaxis.set_visible(False)
axMoi.set_xlim(0, 1)
axMoi.set_ylim(0, 1)
Moi, = axMoi.plot(serpentine_x, serpentine_y, solid_capstyle="round", linewidth=6, color=moisturecolor, animated=True)

axPre.set_title("pressure sensor", y=-0.09)
axPre.spines["left"].set_visible(False)
axPre.spines["right"].set_visible(False)
axPre.spines["top"].set_visible(False)
axPre.spines["bottom"].set_visible(False)
axPre.axes.xaxis.set_visible(False)
axPre.axes.yaxis.set_visible(False)
axPre.set_xlim(0, 1)
axPre.set_ylim(0, 1.2)
Pre = []
Pre.append(patchs.FancyBboxPatch((0.25, 0.05), 0.5, 0.3, boxstyle="round, pad=0.01", fill=pressfill[0], color=presscolor[0], linewidth=3, linestyle=pressstyle[0], animated=True))
Pre.append(patchs.FancyBboxPatch((0.25, 0.4), 0.5, 0.3, boxstyle="round, pad=0.01", fill=pressfill[1], color=presscolor[1], linewidth=3, linestyle=pressstyle[1], animated=True))
Pre.append(patchs.FancyBboxPatch((0.25, 0.75), 0.5, 0.3, boxstyle="round, pad=0.01", fill=pressfill[2], color=presscolor[2], linewidth=3, linestyle=pressstyle[2], animated=True))
for item in Pre:
        axPre.add_patch(item)
Pre.append(axPre.text(0.18, 0.55, presstext[1], va="center", ha="center", rotation=90, font={"weight":1000, "size":10}))

# construct end

guessvalue = []

def data_fetch():
        global nv, C, T, guessvalue
        while True:
                try:
                        nv.fetch_frequencies()
                        data = nv.data(0)
                        unpacked = C.unpack(data)
                        T.loaddata(unpacked)
                        T.initiateguess()
                        res = T.solve(True)
                        guessvalue = res
                except:
                        pass
                print(guessvalue, end="\r")
                cvalue = guessvalue[0]
                moid = False
                slider = 0.1
                sliderheight = 0
                weight = 0
                if cvalue[0] > 100e-12:
                       moid = True
                yield moid, slider, sliderheight, weight, data

def update(data):
        #update value
        global Sld, Moi, Pre, Z22
        global bt, freqlist
        moid, slider, sliderheight, weight, Z22value = data
        colorMoi = MYBLUE if moid == True else MYGRAY
        pressfill = [False, False, False]
        presscolor = [MYGRAY, MYGRAY, MYGRAY]
        pressstyle = ["dashed", "dashed", "dashed"]
        pressheight = [0.2, 0.55, 0.9]
        presstext = ["light", "medium", "heavy"]

        pressfill[weight] = True
        presscolor[weight] = MYBLUE
        pressstyle[weight] = ""

        Z22.set_data(freqlist, Z22value)
        Sld[0].set_height(slider - sliderheight)     #bar1
        Sld[1].set_y(slider - sliderheight)          #bar2
        Sld[2].set_y(slider + sliderheight)          #bar3
        Sld[2].set_height(1 - slider - sliderheight)

        for i in range(3):
                Pre[i].set(fill=pressfill[i], color=presscolor[i], linestyle=pressstyle[i])
        Pre[3].set_position((0.18, pressheight[weight]))
        Pre[3].set_text(presstext[weight])

        Moi.set_color(colorMoi)

        return [*Sld, *Pre, Moi, Z22]

print("initialize")
nv = NanoVNA()
nv.set_sweep(1e6, 40e6)
nv.set_frequencies(1e6, 40e6)

C = Csolver()
T = Tsolver()
input("calibrate")
nv.fetch_frequencies()
data = nv.data(0)
C.calibrate(data)
print(C.Csma, C.Lsma, C.Lxfmr1, C.Rl)
T.prepare()
param = {"lxfmr1":C.Lxfmr1, "lxfmr2":2.996e-6, "mask":[[1, 1, 1, 1], [10.6e-6, 4.811e-6, 2.699e-6, 0]]}
T.setparams(param)
input("start")
ani = FuncAnimation(fig, update, data_fetch, interval=150, blit=True)
plt.show()


import numpy as np
import matplotlib.pyplot as plt
from random import randint
import math
import copy

class EM:
    dim = (100, 1200)

    def __init__(self, dimension, k, points, sigma):
        self.dimension = dimension
        self.k = k
        self.points = points
        self.sigma = sigma
        self.dataSize = points * k

        # Generating data
        self.generators = []
        for i in range(k):
            generator = np.array([np.random.normal(randint(self.dim[0], self.dim[1]), self.sigma, points) for d in range(dimension)])
            self.generators.append(generator.transpose())

        self.data = np.asarray(self.generators).reshape(k * points, dimension)

        # Generating kernel
        self.kernels = []
        for i in range(k):
            mu = [randint(self.dim[0], self.dim[1]) for i in range(dimension)]
            sigma = [self.sigma for i in range(dimension)]
            self.kernels.append({'mu': mu, 'sigma': sigma, 'prior': 1.0/k})
        self.oldKernels = None

    def iteration(self):
        def probability_kernel(x, kernel):
            probability = np.float64(1)
            for d in range(self.dimension):
                probability *= (np.float64(1) / math.sqrt(np.float64(2) * math.pi * kernel['sigma'][d])) * math.exp(-(x[d] - kernel['mu'][d]) ** 2 / (np.float64(2) * kernel['sigma'][d]))
            return probability

        def probability_sum(x, kernels):
            sum = np.float64(0)
            for kernel in kernels:
                sum += probability_kernel(x, kernel) * kernel['prior']
            return sum

        def weight(x, kernel, kernels):
            denominator = probability_sum(x, kernels)
            # Division 0
            if denominator == 0:
                return np.float64(0)
            return (probability_kernel(x, kernel) * kernel['prior']) / denominator

        # Calcul des poids d'appartenance
        weights = [[weight(x, kernel, self.kernels) for x in self.data] for kernel in self.kernels]

        # Calcul des nouveaux kernels
        self.oldKernels = copy.deepcopy(self.kernels)
        for i in range(self.k):
            kernel = self.kernels[i]
            if sum(weights[i]) != 0:
                for d in range(self.dimension):
                    kernel['mu'][d] = (1 / sum(weights[i])) * sum([weights[i][x] * self.data[x][d] for x in range(self.dataSize)])
                    kernel['sigma'][d] = (1 / sum(weights[i])) * sum([weights[i][x] * abs(self.data[x][d] - kernel['mu'][d]) for x in range(self.dataSize)])
                # kernel['prior'] = sum(weights[i]) / self.dataSize

    def draw_class_1d(self):
        plt.style.use('bmh')
        fig, ax = plt.subplots()
        colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
        c = 0

        for generator in self.generators:
            x = generator.transpose()[0]
            ax.scatter(x, np.zeros(len(x)), color=colors[c], label = 'Class ' + str(c))
            c += 1

        ax.set_title("Points by class")
        ax.legend(loc='upper right')
        plt.show()

    def draw_class_2d(self):
        plt.style.use('bmh')
        fig, ax = plt.subplots()
        colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
        c = 0

        for generator in self.generators:
            x = generator.transpose()[0]
            y = generator.transpose()[1]
            ax.scatter(x, y, color=colors[c], label = 'Class ' + str(c))
            c += 1

        ax.set_title("Points by class")
        ax.legend(loc='upper right')
        plt.show()

    def draw_class(self):
        if self.dimension == 1:
            self.draw_class_1d()
        elif self.dimension == 2:
            self.draw_class_2d()
        else:
            print("Dimension not allowed xplotting")

    def draw_cluster_1d(self, epoch):
        plt.style.use('bmh')
        fig, ax = plt.subplots()
        colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
        c = 0
        data = []

        for generator in self.generators:
            x = generator.transpose()[0]
            ax.scatter(x, np.full(len(x), -20), color=colors[c])
            c += 1

        for kernel in self.kernels:
            data.append(np.random.normal(kernel['mu'][0], kernel['sigma'][0], 10000))

        ax.hist(data, 40, alpha=0.8, label=['Cluster ' + str(i) for i in range(self.k)])

        ax.set_title("Epoch " + str(epoch))
        ax.legend(loc='upper right')
        plt.show()

    def draw_cluster_2d(self, epoch):
        plt.style.use('bmh')
        fig, ax = plt.subplots()
        colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
        c = 0
        i = 0

        for kernel in self.kernels:
            ax.scatter(kernel['mu'][0], kernel['mu'][1], s=300, label='Cluster ' + str(i))
            i += 1

        for generator in self.generators:
            x = generator.transpose()[0]
            y = generator.transpose()[1]
            ax.scatter(x, y, color=colors[c])
            c += 1

        ax.set_title("Epoch " + str(epoch))
        ax.legend(loc='upper right', prop={'size': 5})
        plt.show()

    def draw_cluster(self, epoch):
        if self.dimension == 1:
            self.draw_cluster_1d(epoch)
        elif self.dimension == 2:
            self.draw_cluster_2d(epoch)

    def has_converged(self):
        if self.oldKernels is None:
            return False

        for i in range(self.k):
            kernel = self.kernels[i]
            oldKernel = self.oldKernels[i]
            if kernel != oldKernel:
                return False
        return True

    def run(self):
        epoch = 0
        self.draw_class()

        while not self.has_converged():
            print("Epoch ", epoch, " : ")
            for i, kernel in enumerate(self.kernels):
                print("Kernel", i, ": mu : ", kernel['mu'], "    sigma : ", kernel['sigma'])
            print()

            self.draw_cluster(epoch)
            self.iteration()
            epoch += 1

        print("The algorithm has converged.")

em = EM(1, 2, 30, 100)
em.run()


##Write a Python program to find the list with maximum and minimum length.
##Original list:[[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]]
##List with maximum length of lists:
##(3, [13, 15, 17])
##List with minimum length of lists:
##(1, [0])

a=[[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]]

i=0
b=len([0])
c=len([0])
while i<len(a):
    if b<len(a[i]):
        b=len(a[i])
    if c>len(a[i]):
        c=len(a[i])
    i=i+1
print(b)
print(c)

print(len(a[i]),b )


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
from math import sqrt

seg=pd.read_csv('/home/swat/Desktop/seg.csv')

seg=seg.sort_values(['T_block','Loc_Cluster']).reset_index(drop=True)

d1=seg[(seg['T_block']==1) & (seg['Loc_Cluster']==0)].reset_index(drop=True)

d1['Date'] = pd.to_datetime(d1.Date , format = '%Y-%m-%d')
data = d1.drop(['Date','T_block','Loc_Cluster'], axis=1)
data.index = d1.Date

#creating the train and validation set
train = data[:int(0.8*(len(data)))]
valid = data[int(0.8*(len(data))):]

#fit the model
from statsmodels.tsa.vector_ar.var_model import VAR

model = VAR(endog=train)
model_fit = model.fit()

# make prediction on validation
prediction = model_fit.forecast(model_fit.y, steps=len(valid))

#converting predictions to dataframe
cols = data.columns
pred = pd.DataFrame(index=valid.index,columns=[cols])
for j in range(0,2):
    for i in range(0, len(prediction)):
       pred.iloc[i][j] = prediction[i][j]

#check rmse
for i in cols:
    print('rmse value for', i, 'is : ', round(sqrt(mean_squared_error(pred[i], valid[i])),2))

#make final predictions
model = VAR(endog=data)
model_fit = model.fit()
yhat = model_fit.forecast(model_fit.y, steps=5)
print(yhat)

plt.plot(train['Count'])
plt.plot(pred['Count'])
plt.plot(valid['Count'])
plt.xlabel('Date')
plt.ylabel('Count')
plt.title('No of rides requested')
plt.legend()
#plt.savefig('3.png')
plt.show()

plt.plot(train['Distance'])
plt.plot(pred['Distance'])
plt.plot(valid['Distance'])
plt.xlabel('Date')
plt.ylabel('Distance')
plt.title('Total distance covered in rides')
plt.legend()
#plt.savefig('4.png')
plt.show()


from os import listdir, mkdir, getcwd, environ
from os.path import join, dirname, realpath
from os import environ
import sys

import pandas as pd
import numpy as np
import xarray as xr
import math

import datetime

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import rcParams
from cartopy import feature as cf
sys.path.append('.')
import config as config
config.SCALE = config.PRES_SCALE
config.BASE_WIDTH = config.PRES_WIDTH
config.BASE_HEIGHT = config.PRES_HEIGHT

# config.TITLE_PAD /= 2
import format_plots as fp

# from fastkml import kml
# from lxml import etree
# from simplekml import (Kml, OverlayXY, ScreenXY, Units, RotationXY,
#                        AltitudeMode, Camera)

import cartopy.crs as ccrs
import cartopy

import shapefile as shp

# Tell matplotlib not to look for an X-window
# environ['QT_QPA_PLATFORM']='offscreen'

colors = plt.cm.get_cmap('inferno', lut=8)

# rcParams['font.family'] = 'sans-serif'
# Other font details
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = 'AppleGothic'
rcParams['font.size'] = config.LABEL_FONTSIZE*config.SCALE
rcParams['text.usetex'] = True
rcParams['mathtext.fontset'] = 'stixsans'
rcParams['text.latex.preamble'] = r'\usepackage{cmbright}'
rcParams['axes.titlepad'] = config.TITLE_PAD/2

# some global variables
season_dict = {'DJF' : 'Winter', 'MAM' :  'Spring', 'JJA' : 'Summer',
               'SON' : 'Fall'}

def plot_TROPOMI(data, latlim, lonlim, res, figax=None, title='', genkml=False, vals='xch4', **plot_options):
    # Set default plot options]
    if 'vmin' not in plot_options:
        plot_options['vmin'] = 1700
    if 'vmax' not in plot_options:
        plot_options['vmax'] = 1900
    if 'cmap' not in plot_options:
        plot_options['cmap'] = 'inferno'

    res_prec = len(str(res).split('.')[1])

    # Define edges
    latlim = [round(l, res_prec) for l in latlim]
    lat_steps = int((latlim[1]-latlim[0])/res) + 1

    lonlim = [round(l, res_prec) for l in lonlim]
    lon_steps = int((lonlim[1]-lonlim[0])/res) + 1

    lats = np.around(np.linspace(latlim[0], latlim[1], lat_steps) + res/2,
                     res_prec + 1)
    lats_s = pd.DataFrame({'idx' : np.ones(len(lats)-1), 'lat' : lats[:-1]})
    lons = np.around(np.linspace(lonlim[0], lonlim[1], lon_steps) + res/2,
                     res_prec + 1)
    lons_s = pd.DataFrame({'idx' : np.ones(len(lons)-1), 'lon' : lons[:-1]})

    df = pd.merge(lats_s, lons_s, on='idx').drop(columns='idx')
    data = pd.merge(df, data, on=['lat', 'lon'], how='left')

    d_p = data.pivot(index='lat', columns='lon', values=vals)
    lon, lat = np.meshgrid(lons-res/2, lats-res/2)

    if figax is None:
        fig, ax = plt.subplots(figsize=(10,10), subplot_kw={'projection' : ccrs.PlateCarree()})
    else:
        fig, ax = figax

    c = ax.pcolormesh(lons, lats, d_p,
                      vmin=plot_options['vmin'], vmax=plot_options['vmax'],
                      cmap=plot_options['cmap'], edgecolors=None, snap=True)

    return fig, ax, c

if __name__ == '__main__':
    # DATA_DIR = sys.argv[1]
    # REGION   = str(sys.argv[2].split(',')[0])
    DATA_DIR = '../observations/oversampling'
    PLOT_DIR = '../plots/'
    REGION = 'northamerica'
    print('Processing %s' % REGION)

    # latlim = np.array(sys.argv[2].split(',')[1:3]).astype(float)
    # lonlim = np.array(sys.argv[2].split(',')[3:5]).astype(float)
    latlim = np.array([10.375, 59.375]).astype(float)
    lonlim = np.array([-129.21875, -60.78125]).astype(float)
    res = 0.01
    count_min = 1

    files = listdir(DATA_DIR)
    files = [f for f in files if f[-3:] == 'csv']
    files.sort()

    for f in files:
        data = pd.read_csv(join(DATA_DIR, f))
        data = data[(data['lon'] >= lonlim[0]) &
                    (data['lon'] <= lonlim[1]) &
                    (data['lat'] >= latlim[0]) &
                    (data['lat'] <= latlim[1])]

        print('Plotting %s' % f)
        full_date = f.split('_')[0]
        year = full_date[:4]
        if len(full_date) == 6:
            month = full_date[-2:]
            month_name = datetime.date(1900, int(month), 1).strftime('%B')
            title = f'{month_name} {year}'
        else:
            continue
            # title = f'{season_dict[full_date[-3:]]} {year}'

        fig, ax = fp.get_figax(maps=True, lats=latlim, lons=lonlim)
        ax = fp.format_map(ax, latlim, lonlim, draw_labels=False)

        # calculate vmin and vmax
        vmin = 1800
        vmax = 1900
        plot_options = {'vmin' : vmin,
                        'vmax' : vmax,
                        'cmap' : 'plasma'}

        fig, ax, c = plot_TROPOMI(data, latlim, lonlim, res,
                                  figax=[fig, ax],
                                  vals='xch4',
                                  **plot_options)

        cax = fp.add_cax(fig, ax)
        cbar = fig.colorbar(c, cax=cax)
        cbar = fp.format_cbar(cbar, 'XCH4 (ppb)')
        fp.add_title(ax, title=title)

        # Save plot
        fp.save_fig(fig, PLOT_DIR, f'{REGION}_{full_date}')
        plt.close()
        print('\n')


"""
Project Euler Archive Practice
https://projecteuler.net/problem=1

Problem 1 - Multiples of 3 and 5
SOLVED & VERIFIED VIA PROJECT EULER

If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
"""

def summingMultiples():

    multiples = []  # Create a blank list to store values

    for x in range(0, 1000):    # Loop through numbers 0-1000

        if x % 3 ==0 or x % 5 == 0: # Check if each number is divisible by 3 or 5

            multiples.append(x) # Add number to list

    return print(sum(multiples))    # Return the sum of the list elements

summingMultiples()  # Call function


from ase.build import bulk
from ase.dft.kpoints import ibz_points, bandpath
from ase.phonons import Phonons
from ase.io import read, write
from ase.lattice.hexagonal import HexagonalClosedPacked
from ase.optimize import BFGS

import model
# Setup crystal and EMT calculator
c_over_a = 1.588
a0 = 2.95
#a0 = 2.95

# set up the a
atoms = HexagonalClosedPacked(symbol='Ti', latticeconstant=(a0,a0*c_over_a))

#atoms.rattle(0.001)

#atoms.set_calculator(model.calculator)

#dyn = BFGS(atoms)
#dyn.run(fmax=0.0001)

#atoms = read("/Users/Cas/gits/gap-testing-framework/tests/Ti/phonon_hcp/hcp_relaxed.xyz")

#atoms = read("/Users/Cas/gits/gap-testing-framework/tests/Ti/phonon_hcp/hcp_init_relaxed.xyz")

# Phonon calculator
N = 3
ph = Phonons(atoms, model.calculator, supercell=(N, N, N), delta=0.01)
ph.run()

# Read forces and assemble the dynamical matrix
ph.read(acoustic=True)

FCM = ph.get_force_constant()

# High-symmetry points in the Brillouin zone
points = ibz_points['hexagonal']
G = points['Gamma']
K = points['K']
M = points['M']
A = points['A']

point_names = ['$\Gamma$', 'K', 'M', '$\Gamma$', 'A']
path = [G, K, M, G, A]

path_kc, q, Q = bandpath(path, atoms.cell, 100)
omega_kn = 1000 * ph.band_structure(path_kc)

properties = {"omega_kn" : omega_kn.tolist(), "point_names" : point_names, "path" : path, "Q" : list(Q), "q" : list(q), "FCM" : FCM.tolist() }